30 #include <linux/time.h>
31 #include <linux/fcntl.h>
32 #include <linux/stat.h>
33 #include <linux/string.h>
47 #define NAMEI_RA_CHUNKS 2
48 #define NAMEI_RA_BLOCKS 4
49 #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
50 #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
52 static struct buffer_head *ext4_append(handle_t *
handle,
56 struct buffer_head *bh;
65 *block = inode->
i_size >> inode->
i_sb->s_blocksize_bits;
67 bh =
ext4_bread(handle, inode, *block, 1, err);
79 ext4_error(inode->
i_sb,
80 "Directory hole detected on inode %lu\n",
87 #define assert(test) J_ASSERT(test)
91 #define dxtrace(command) command
93 #define dxtrace(command)
149 struct buffer_head *
bh;
177 static unsigned dx_root_limit(
struct inode *dir,
unsigned infosize);
178 static unsigned dx_node_limit(
struct inode *dir);
179 static struct dx_frame *dx_probe(
const struct qstr *d_name,
184 static void dx_release(
struct dx_frame *frames);
190 static struct ext4_dir_entry_2* dx_pack_dirents(
char *base,
unsigned blocksize);
197 static struct buffer_head * ext4_dx_find_entry(
struct inode *dir,
198 const struct qstr *d_name,
205 #define EXT4_DIRENT_TAIL(block, blocksize) \
206 ((struct ext4_dir_entry_tail *)(((void *)(block)) + \
208 sizeof(struct ext4_dir_entry_tail))))
211 unsigned int blocksize)
253 static __le32 ext4_dirent_csum(
struct inode *inode,
272 t = get_dirent_tail(inode, dirent);
275 "leaf for checksum. Please run e2fsck -D.");
280 (
void *)t - (
void *)dirent))
286 static void ext4_dirent_csum_set(
struct inode *inode,
295 t = get_dirent_tail(inode, dirent);
298 "leaf for checksum. Please run e2fsck -D.");
303 (
void *)t - (
void *)dirent);
306 static inline int ext4_handle_dirty_dirent_node(handle_t *
handle,
308 struct buffer_head *bh)
314 static struct dx_countlimit *get_dx_countlimit(
struct inode *inode,
319 struct dx_root_info *
root;
329 root = (
struct dx_root_info *)(((
void *)dp + 12));
330 if (root->reserved_zero ||
331 root->info_length !=
sizeof(
struct dx_root_info))
338 *offset = count_offset;
339 return (
struct dx_countlimit *)(((
void *)dirent) + count_offset);
350 size = count_offset + (count *
sizeof(
struct dx_entry));
354 csum = ext4_chksum(sbi, csum, (
__u8 *)t,
sizeof(
struct dx_tail));
360 static int ext4_dx_csum_verify(
struct inode *inode,
371 c = get_dx_countlimit(inode, dirent, &count_offset);
378 if (count_offset + (limit *
sizeof(
struct dx_entry)) >
381 "tree checksum found. Run e2fsck -D.");
386 if (t->
dt_checksum != ext4_dx_csum(inode, dirent, count_offset,
392 static void ext4_dx_csum_set(
struct inode *inode,
struct ext4_dir_entry *dirent)
402 c = get_dx_countlimit(inode, dirent, &count_offset);
409 if (count_offset + (limit *
sizeof(
struct dx_entry)) >
412 "tree checksum. Run e2fsck -D.");
417 t->
dt_checksum = ext4_dx_csum(inode, dirent, count_offset, count, t);
420 static inline int ext4_handle_dirty_dx_node(handle_t *handle,
422 struct buffer_head *bh)
435 ext4_rec_len_from_disk(p->
rec_len, blocksize));
468 static inline unsigned dx_get_limit(
struct dx_entry *entries)
473 static inline void dx_set_count(
struct dx_entry *entries,
unsigned value)
478 static inline void dx_set_limit(
struct dx_entry *entries,
unsigned value)
483 static inline unsigned dx_root_limit(
struct inode *dir,
unsigned infosize)
490 entry_space -=
sizeof(
struct dx_tail);
491 return entry_space /
sizeof(
struct dx_entry);
494 static inline unsigned dx_node_limit(
struct inode *dir)
500 entry_space -=
sizeof(
struct dx_tail);
501 return entry_space /
sizeof(
struct dx_entry);
508 static void dx_show_index(
char *
label,
struct dx_entry *entries)
510 int i,
n = dx_get_count (entries);
512 for (i = 0; i <
n; i++) {
513 printk(
"%x->%lu ", i ? dx_get_hash(entries + i) :
514 0, (
unsigned long)dx_get_block(entries + i));
527 int size,
int show_names)
529 unsigned names = 0, space = 0;
530 char *
base = (
char *) de;
534 while ((
char *) de < base + size)
542 while (len--)
printk(
"%c", *name++);
545 (
unsigned) ((
char *) de - base));
550 de = ext4_next_entry(de, size);
553 return (
struct stats) { names, space, 1 };
559 unsigned blocksize = dir->
i_sb->s_blocksize;
560 unsigned count = dx_get_count(entries), names = 0, space = 0,
i;
562 struct buffer_head *bh;
564 printk(
"%i indexed blocks...\n", count);
565 for (i = 0; i <
count; i++, entries++)
569 u32 range = i < count - 1? (dx_get_hash(entries + 1) -
hash): ~hash;
571 printk(
"%s%3u:%03u hash %8x/%8x ",levels?
"":
" ", i, block, hash, range);
574 dx_show_entries(hinfo, dir, ((
struct dx_node *) bh->b_data)->entries, levels - 1):
576 names +=
stats.names;
577 space +=
stats.space;
578 bcount +=
stats.bcount;
583 levels ?
"" :
" ", names, space/bcount,
584 (space/bcount)*100/blocksize);
585 return (
struct stats) { names, space, bcount};
599 dx_probe(
const struct qstr *d_name,
struct inode *dir,
602 unsigned count, indirect;
605 struct buffer_head *bh;
615 root = (
struct dx_root *) bh->b_data;
619 ext4_warning(dir->
i_sb,
"Unrecognised inode hash code %d",
620 root->
info.hash_version);
633 if (root->
info.unused_flags & 1) {
634 ext4_warning(dir->
i_sb,
"Unimplemented inode hash flags: %#06x",
635 root->
info.unused_flags);
641 if ((indirect = root->
info.indirect_levels) > 1) {
642 ext4_warning(dir->
i_sb,
"Unimplemented inode hash depth: %#06x",
643 root->
info.indirect_levels);
649 if (!buffer_verified(bh) &&
651 ext4_warning(dir->
i_sb,
"Root failed checksum");
656 set_buffer_verified(bh);
659 root->
info.info_length);
661 if (dx_get_limit(entries) != dx_root_limit(dir,
662 root->
info.info_length)) {
663 ext4_warning(dir->
i_sb,
"dx entry: limit != root limit");
672 count = dx_get_count(entries);
673 if (!count || count > dx_get_limit(entries)) {
674 ext4_warning(dir->
i_sb,
675 "dx entry: no count or count > limit");
682 q = entries + count - 1;
687 if (dx_get_hash(m) > hash)
695 unsigned n = count - 1;
700 if (dx_get_hash(++at) > hash)
710 dxtrace(
printk(
" %x->%u\n", at == entries? 0: dx_get_hash(at), dx_get_block(at)));
714 if (!indirect--)
return frame;
720 at = entries = ((
struct dx_node *) bh->b_data)->entries;
722 if (!buffer_verified(bh) &&
723 !ext4_dx_csum_verify(dir,
725 ext4_warning(dir->
i_sb,
"Node failed checksum");
730 set_buffer_verified(bh);
732 if (dx_get_limit(entries) != dx_node_limit (dir)) {
733 ext4_warning(dir->
i_sb,
734 "dx entry: limit != node limit");
743 while (frame >= frame_in) {
749 ext4_warning(dir->
i_sb,
750 "Corrupt dir inode %lu, running e2fsck is "
751 "recommended.", dir->
i_ino);
755 static void dx_release (
struct dx_frame *frames)
757 if (frames[0].bh ==
NULL)
760 if (((
struct dx_root *) frames[0].bh->b_data)->info.indirect_levels)
761 brelse(frames[1].bh);
762 brelse(frames[0].bh);
782 static int ext4_htree_next_block(
struct inode *dir,
__u32 hash,
788 struct buffer_head *bh;
789 int err, num_frames = 0;
816 bhash = dx_get_hash(p->
at);
819 if ((hash & 1) == 0) {
820 if ((bhash & ~1) != hash)
827 while (num_frames--) {
831 ext4_error(dir->
i_sb,
832 "Directory hole detected on inode %lu\n",
839 if (!buffer_verified(bh) &&
840 !ext4_dx_csum_verify(dir,
842 ext4_warning(dir->
i_sb,
"Node failed checksum");
845 set_buffer_verified(bh);
861 static int htree_dirblock_to_tree(
struct file *dir_file,
866 struct buffer_head *bh;
868 int err = 0, count = 0;
871 (
unsigned long)block));
875 ext4_error(dir->
i_sb,
876 "Directory hole detected on inode %lu\n",
882 if (!buffer_verified(bh) &&
885 set_buffer_verified(bh);
889 dir->
i_sb->s_blocksize -
891 for (; de <
top; de = ext4_next_entry(de, dir->
i_sb->s_blocksize)) {
892 if (ext4_check_dir_entry(dir,
NULL, de, bh,
894 + ((
char *)de - bh->b_data))) {
897 (dir->
i_sb->s_blocksize - 1)) + 1;
902 if ((hinfo->
hash < start_hash) ||
903 ((hinfo->
hash == start_hash) &&
941 start_hash, start_minor_hash));
942 dir = dir_file->
f_path.dentry->d_inode;
949 count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
950 start_hash, start_minor_hash);
954 hinfo.
hash = start_hash;
956 frame = dx_probe(
NULL, dir, &hinfo, frames, &err);
961 if (!start_hash && !start_minor_hash) {
967 if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
969 de = ext4_next_entry(de, dir->
i_sb->s_blocksize);
976 block = dx_get_block(frame->
at);
977 ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
978 start_hash, start_minor_hash);
986 frame, frames, &hashval);
987 *next_hash = hashval;
998 (count && ((hashval & 1) == 0)))
1003 "next hash: %x\n", count, *next_hash));
1024 char *base = (
char *) de;
1027 while ((
char *) de < base + blocksize) {
1032 map_tail->
offs = ((
char *) de - base)>>2;
1038 de = ext4_next_entry(de, blocksize);
1050 count = count*10/13;
1053 for (p = top, q = p - count; q >=
map; p--, q--)
1062 if (q[1].hash >= q[0].hash)
1073 struct dx_entry *old = frame->
at, *
new = old + 1;
1074 int count = dx_get_count(entries);
1076 assert(count < dx_get_limit(entries));
1077 assert(old < entries + count);
1078 memmove(
new + 1,
new, (
char *)(entries + count) - (
char *)(
new));
1079 dx_set_hash(
new, hash);
1080 dx_set_block(
new, block);
1081 dx_set_count(entries, count + 1);
1084 static void ext4_update_dx_flag(
struct inode *inode)
1097 static inline int ext4_match (
int len,
const char *
const name,
1110 static inline int search_dirblock(
struct buffer_head *bh,
1112 const struct qstr *d_name,
1113 unsigned int offset,
1119 const char *name = d_name->
name;
1123 dlimit = bh->b_data + dir->
i_sb->s_blocksize;
1124 while ((
char *) de < dlimit) {
1128 if ((
char *) de + namelen <= dlimit &&
1129 ext4_match (namelen, name, de)) {
1131 if (ext4_check_dir_entry(dir,
NULL, de, bh, offset))
1137 de_len = ext4_rec_len_from_disk(de->
rec_len,
1138 dir->
i_sb->s_blocksize);
1159 static struct buffer_head * ext4_find_entry (
struct inode *dir,
1160 const struct qstr *d_name,
1165 struct buffer_head *bh, *
ret =
NULL;
1167 const u8 *name = d_name->
name;
1179 namelen = d_name->len;
1182 if ((namelen <= 2) && (name[0] ==
'.') &&
1183 (name[1] ==
'.' || name[1] ==
'\0')) {
1193 bh = ext4_dx_find_entry(dir, d_name, res_dir, &err);
1205 start =
EXT4_I(dir)->i_dir_start_lookup;
1206 if (start >= nblocks)
1214 if (ra_ptr >= ra_max) {
1224 if (b >= nblocks || (num && block == start)) {
1225 bh_use[ra_max] =
NULL;
1230 bh_use[ra_max] = bh;
1236 if ((bh = bh_use[ra_ptr++]) ==
NULL)
1239 if (!buffer_uptodate(bh)) {
1242 (
unsigned long) block);
1246 if (!buffer_verified(bh) &&
1250 "block %lu", (
unsigned long)block);
1254 set_buffer_verified(bh);
1255 i = search_dirblock(bh, dir, d_name,
1260 goto cleanup_and_exit;
1264 goto cleanup_and_exit;
1267 if (++block >= nblocks)
1269 }
while (block != start);
1277 if (block < nblocks) {
1284 for (; ra_ptr < ra_max; ra_ptr++)
1285 brelse(bh_use[ra_ptr]);
1289 static struct buffer_head * ext4_dx_find_entry(
struct inode *dir,
const struct qstr *d_name,
1295 struct buffer_head *bh;
1299 if (!(frame = dx_probe(d_name, dir, &hinfo, frames, err)))
1302 block = dx_get_block(frame->
at);
1306 ext4_error(dir->
i_sb,
1307 "Directory hole detected on inode %lu\n",
1313 if (!buffer_verified(bh) &&
1317 "block %lu", (
unsigned long)block);
1322 set_buffer_verified(bh);
1323 retval = search_dirblock(bh, dir, d_name,
1337 retval = ext4_htree_next_block(dir, hinfo.
hash, frame,
1341 "error reading index page in directory #%lu",
1346 }
while (retval == 1);
1351 dx_release (frames);
1357 struct inode *
inode;
1359 struct buffer_head *bh;
1364 bh = ext4_find_entry(dir, &dentry->
d_name, &de);
1369 if (!ext4_valid_inum(dir->
i_sb, ino)) {
1371 return ERR_PTR(-
EIO);
1377 return ERR_PTR(-
EIO);
1380 if (inode == ERR_PTR(-
ESTALE)) {
1382 "deleted inode referenced: %u",
1384 return ERR_PTR(-
EIO);
1396 struct buffer_head *bh;
1398 bh = ext4_find_entry(child->
d_inode, &dotdot, &de);
1404 if (!ext4_valid_inum(child->
d_inode->i_sb, ino)) {
1406 "bad parent inode number: %u", ino);
1407 return ERR_PTR(-
EIO);
1414 static unsigned char ext4_type_by_mode[
S_IFMT >>
S_SHIFT] = {
1424 static inline void ext4_set_de_type(
struct super_block *sb,
1439 unsigned rec_len = 0;
1443 (from + (map->
offs<<2));
1445 memcpy (to, de, rec_len);
1447 ext4_rec_len_to_disk(rec_len, blocksize);
1459 static struct ext4_dir_entry_2* dx_pack_dirents(
char *base,
unsigned blocksize)
1462 unsigned rec_len = 0;
1465 while ((
char*)de < base + blocksize) {
1466 next = ext4_next_entry(de, blocksize);
1471 to->
rec_len = ext4_rec_len_to_disk(rec_len, blocksize);
1485 static struct ext4_dir_entry_2 *do_split(handle_t *handle,
struct inode *dir,
1486 struct buffer_head **bh,
struct dx_frame *frame,
1489 unsigned blocksize = dir->
i_sb->s_blocksize;
1490 unsigned count, continued;
1491 struct buffer_head *bh2;
1506 bh2 = ext4_append (handle, dir, &newblock, &err);
1513 BUFFER_TRACE(*bh,
"get_write_access");
1518 BUFFER_TRACE(frame->
bh,
"get_write_access");
1523 data2 = bh2->b_data;
1528 blocksize, hinfo, map);
1530 dx_sort_map(map, count);
1534 for (i = count-1; i >= 0; i--) {
1536 if (size + map[i].size/2 > blocksize/2)
1538 size += map[
i].
size;
1542 split = count - move;
1544 continued = hash2 == map[split - 1].
hash;
1546 (
unsigned long)dx_get_block(frame->
at),
1547 hash2, split, count-split));
1550 de2 = dx_move_dirents(data1,
data2, map + split, count - split, blocksize);
1551 de = dx_pack_dirents(data1, blocksize);
1552 de->
rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
1555 de2->rec_len = ext4_rec_len_to_disk(
data2 + (blocksize - csum_size) -
1560 initialize_dirent_tail(t, blocksize);
1563 initialize_dirent_tail(t, blocksize);
1570 if (hinfo->
hash >= hash2)
1575 dx_insert_block(frame, hash2 + continued, newblock);
1576 err = ext4_handle_dirty_dirent_node(handle, dir, bh2);
1579 err = ext4_handle_dirty_dx_node(handle, dir, frame->
bh);
1590 ext4_std_error(dir->
i_sb, err);
1604 static int add_dirent_to_buf(handle_t *handle,
struct dentry *dentry,
1606 struct buffer_head *bh)
1608 struct inode *dir = dentry->
d_parent->d_inode;
1609 const char *name = dentry->
d_name.name;
1610 int namelen = dentry->
d_name.len;
1611 unsigned int offset = 0;
1612 unsigned int blocksize = dir->
i_sb->s_blocksize;
1613 unsigned short reclen;
1614 int nlen, rlen,
err;
1625 top = bh->b_data + (blocksize - csum_size) - reclen;
1626 while ((
char *) de <=
top) {
1627 if (ext4_check_dir_entry(dir,
NULL, de, bh, offset))
1629 if (ext4_match(namelen, name, de))
1632 rlen = ext4_rec_len_from_disk(de->
rec_len, blocksize);
1633 if ((de->
inode? rlen - nlen: rlen) >= reclen)
1638 if ((
char *) de >
top)
1641 BUFFER_TRACE(bh,
"get_write_access");
1644 ext4_std_error(dir->
i_sb, err);
1650 rlen = ext4_rec_len_from_disk(de->
rec_len, blocksize);
1653 de1->
rec_len = ext4_rec_len_to_disk(rlen - nlen, blocksize);
1654 de->
rec_len = ext4_rec_len_to_disk(nlen, blocksize);
1659 ext4_set_de_type(dir->
i_sb, de, inode->
i_mode);
1674 ext4_update_dx_flag(dir);
1677 BUFFER_TRACE(bh,
"call ext4_handle_dirty_metadata");
1678 err = ext4_handle_dirty_dirent_node(handle, dir, bh);
1680 ext4_std_error(dir->
i_sb, err);
1688 static int make_indexed_dir(handle_t *handle,
struct dentry *dentry,
1689 struct inode *inode,
struct buffer_head *bh)
1691 struct inode *dir = dentry->
d_parent->d_inode;
1692 const char *name = dentry->
d_name.name;
1693 int namelen = dentry->
d_name.len;
1694 struct buffer_head *bh2;
1713 blocksize = dir->
i_sb->s_blocksize;
1717 ext4_std_error(dir->
i_sb, retval);
1721 root = (
struct dx_root *) bh->b_data;
1726 ext4_rec_len_from_disk(fde->
rec_len, blocksize));
1727 if ((
char *) de >= (((
char *) root) + blocksize)) {
1732 len = ((
char *) root) + (blocksize - csum_size) - (
char *) de;
1735 bh2 = ext4_append(handle, dir, &block, &retval);
1741 data1 = bh2->b_data;
1746 while ((
char *)(de2 = ext4_next_entry(de, blocksize)) < top)
1748 de->
rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
1754 initialize_dirent_tail(t, blocksize);
1762 root->
info.info_length =
sizeof(root->
info);
1765 dx_set_block(entries, 1);
1766 dx_set_count(entries, 1);
1767 dx_set_limit(entries, dx_root_limit(dir,
sizeof(root->
info)));
1781 ext4_handle_dirty_dx_node(handle, dir, frame->
bh);
1782 ext4_handle_dirty_dirent_node(handle, dir, bh);
1784 de = do_split(handle,dir, &bh, frame, &hinfo, &retval);
1797 retval = add_dirent_to_buf(handle, dentry, inode, de, bh);
1812 static int ext4_add_entry(handle_t *handle,
struct dentry *dentry,
1813 struct inode *inode)
1815 struct inode *dir = dentry->
d_parent->d_inode;
1816 struct buffer_head *bh;
1835 retval = ext4_dx_add_entry(handle, dentry, inode);
1843 for (block = 0; block < blocks; block++) {
1844 if (!(bh =
ext4_bread(handle, dir, block, 0, &retval))) {
1847 ext4_error(inode->
i_sb,
1848 "Directory hole detected on inode %lu\n",
1853 if (!buffer_verified(bh) &&
1857 set_buffer_verified(bh);
1858 retval = add_dirent_to_buf(handle, dentry, inode,
NULL, bh);
1864 if (blocks == 1 && !dx_fallback &&
1866 return make_indexed_dir(handle, dentry, inode, bh);
1869 bh = ext4_append(handle, dir, &block, &retval);
1874 de->
rec_len = ext4_rec_len_to_disk(blocksize - csum_size, blocksize);
1878 initialize_dirent_tail(t, blocksize);
1881 retval = add_dirent_to_buf(handle, dentry, inode, de, bh);
1884 ext4_set_inode_state(inode, EXT4_STATE_NEWENTRY);
1891 static int ext4_dx_add_entry(handle_t *handle,
struct dentry *dentry,
1892 struct inode *inode)
1897 struct buffer_head *bh;
1898 struct inode *dir = dentry->
d_parent->d_inode;
1903 frame = dx_probe(&dentry->
d_name, dir, &hinfo, frames, &err);
1909 if (!(bh =
ext4_bread(handle, dir, dx_get_block(frame->
at), 0, &err))) {
1912 ext4_error(dir->
i_sb,
1913 "Directory hole detected on inode %lu\n",
1919 if (!buffer_verified(bh) &&
1922 set_buffer_verified(bh);
1924 BUFFER_TRACE(bh,
"get_write_access");
1929 err = add_dirent_to_buf(handle, dentry, inode,
NULL, bh);
1935 dx_get_count(entries), dx_get_limit(entries)));
1937 if (dx_get_count(entries) == dx_get_limit(entries)) {
1939 unsigned icount = dx_get_count(entries);
1940 int levels = frame - frames;
1943 struct buffer_head *bh2;
1945 if (levels && (dx_get_count(frames->entries) ==
1946 dx_get_limit(frames->entries))) {
1947 ext4_warning(sb,
"Directory index full!");
1951 bh2 = ext4_append (handle, dir, &newblock, &err);
1954 node2 = (
struct dx_node *)(bh2->b_data);
1959 BUFFER_TRACE(frame->
bh,
"get_write_access");
1964 unsigned icount1 = icount/2, icount2 = icount - icount1;
1965 unsigned hash2 = dx_get_hash(entries + icount1);
1969 BUFFER_TRACE(frame->
bh,
"get_write_access");
1975 memcpy((
char *) entries2, (
char *) (entries + icount1),
1976 icount2 *
sizeof(
struct dx_entry));
1977 dx_set_count(entries, icount1);
1978 dx_set_count(entries2, icount2);
1979 dx_set_limit(entries2, dx_node_limit(dir));
1982 if (at - entries >= icount1) {
1983 frame->
at = at = at - entries - icount1 + entries2;
1984 frame->
entries = entries = entries2;
1987 dx_insert_block(frames + 0, hash2, newblock);
1988 dxtrace(dx_show_index(
"node", frames[1].entries));
1990 ((
struct dx_node *) bh2->b_data)->entries));
1991 err = ext4_handle_dirty_dx_node(handle, dir, bh2);
1997 "Creating second level index...\n"));
1998 memcpy((
char *) entries2, (
char *) entries,
2000 dx_set_limit(entries2, dx_node_limit(dir));
2003 dx_set_count(entries, 1);
2004 dx_set_block(entries + 0, newblock);
2005 ((
struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
2009 frame->
at = at = at - entries + entries2;
2010 frame->
entries = entries = entries2;
2017 err = ext4_handle_dirty_dx_node(handle, dir, frames[0].bh);
2019 ext4_std_error(inode->
i_sb, err);
2023 de = do_split(handle, dir, &bh, frame, &hinfo, &err);
2026 err = add_dirent_to_buf(handle, dentry, inode, de, bh);
2030 ext4_std_error(dir->
i_sb, err);
2042 static int ext4_delete_entry(handle_t *handle,
2045 struct buffer_head *bh)
2048 unsigned int blocksize = dir->
i_sb->s_blocksize;
2059 while (i < bh->b_size - csum_size) {
2060 if (ext4_check_dir_entry(dir,
NULL, de, bh, i))
2063 BUFFER_TRACE(bh,
"get_write_access");
2066 ext4_std_error(dir->
i_sb, err);
2070 pde->
rec_len = ext4_rec_len_to_disk(
2071 ext4_rec_len_from_disk(pde->
rec_len,
2073 ext4_rec_len_from_disk(de->
rec_len,
2079 BUFFER_TRACE(bh,
"call ext4_handle_dirty_metadata");
2080 err = ext4_handle_dirty_dirent_node(handle, dir, bh);
2082 ext4_std_error(dir->
i_sb, err);
2087 i += ext4_rec_len_from_disk(de->
rec_len, blocksize);
2089 de = ext4_next_entry(de, blocksize);
2098 static void ext4_inc_count(handle_t *handle,
struct inode *inode)
2115 static void ext4_dec_count(handle_t *handle,
struct inode *inode)
2122 static int ext4_add_nondir(handle_t *handle,
2123 struct dentry *dentry,
struct inode *inode)
2125 int err = ext4_add_entry(handle, dentry, inode);
2146 static int ext4_create(
struct inode *dir,
struct dentry *dentry,
umode_t mode,
2150 struct inode *
inode;
2160 return PTR_ERR(handle);
2163 ext4_handle_sync(handle);
2166 err = PTR_ERR(inode);
2167 if (!IS_ERR(inode)) {
2171 err = ext4_add_nondir(handle, dentry, inode);
2179 static int ext4_mknod(
struct inode *dir,
struct dentry *dentry,
2183 struct inode *
inode;
2184 int err, retries = 0;
2186 if (!new_valid_dev(rdev))
2196 return PTR_ERR(handle);
2199 ext4_handle_sync(handle);
2202 err = PTR_ERR(inode);
2203 if (!IS_ERR(inode)) {
2206 err = ext4_add_nondir(handle, dentry, inode);
2214 static int ext4_mkdir(
struct inode *dir,
struct dentry *dentry,
umode_t mode)
2217 struct inode *
inode;
2218 struct buffer_head *dir_block =
NULL;
2221 unsigned int blocksize = dir->
i_sb->s_blocksize;
2223 int err, retries = 0;
2239 return PTR_ERR(handle);
2242 ext4_handle_sync(handle);
2246 err = PTR_ERR(inode);
2253 if (!(dir_block =
ext4_bread(handle, inode, 0, 1, &err))) {
2256 ext4_error(inode->
i_sb,
2257 "Directory hole detected on inode %lu\n",
2260 goto out_clear_inode;
2262 BUFFER_TRACE(dir_block,
"get_write_access");
2265 goto out_clear_inode;
2273 de = ext4_next_entry(de, blocksize);
2275 de->
rec_len = ext4_rec_len_to_disk(blocksize -
2285 initialize_dirent_tail(t, blocksize);
2288 BUFFER_TRACE(dir_block,
"call ext4_handle_dirty_metadata");
2289 err = ext4_handle_dirty_dirent_node(handle, inode, dir_block);
2291 goto out_clear_inode;
2292 set_buffer_verified(dir_block);
2295 err = ext4_add_entry(handle, dentry, inode);
2304 ext4_inc_count(handle, dir);
2305 ext4_update_dx_flag(dir);
2308 goto out_clear_inode;
2322 static int empty_dir(
struct inode *inode)
2325 struct buffer_head *bh;
2335 "error %d reading directory lblock 0", err);
2337 ext4_warning(inode->
i_sb,
2338 "bad directory (dir #%lu) - no data block",
2342 if (!buffer_verified(bh) &&
2349 set_buffer_verified(bh);
2356 ext4_warning(inode->
i_sb,
2357 "bad directory (dir #%lu) - no `.' or `..'",
2367 (
void *) de >= (
void *) (bh->b_data+sb->
s_blocksize)) {
2368 unsigned int lblock;
2376 "error %d reading directory "
2377 "lblock %u", err, lblock);
2379 ext4_warning(inode->
i_sb,
2380 "bad directory (dir #%lu) - no data block",
2386 if (!buffer_verified(bh) &&
2390 "reading directory lblock 0");
2393 set_buffer_verified(bh);
2396 if (ext4_check_dir_entry(inode,
NULL, de, bh, offset)) {
2424 struct ext4_iloc iloc;
2431 if (!list_empty(&
EXT4_I(inode)->i_orphan))
2443 BUFFER_TRACE(
EXT4_SB(sb)->s_sbh,
"get_write_access");
2480 jbd_debug(4,
"orphan inode %lu will point to %d\n",
2484 ext4_std_error(inode->
i_sb, err);
2496 struct ext4_sb_info *sbi;
2498 struct ext4_iloc iloc;
2512 jbd_debug(4,
"remove inode %lu from orphan list\n", inode->
i_ino);
2527 if (prev == &sbi->s_orphan) {
2528 jbd_debug(4,
"superblock will point to %u\n", ino_next);
2529 BUFFER_TRACE(sbi->s_sbh,
"get_write_access");
2536 struct ext4_iloc iloc2;
2537 struct inode *i_prev =
2540 jbd_debug(4,
"orphan inode %lu will point to %u\n",
2541 i_prev->
i_ino, ino_next);
2554 ext4_std_error(inode->
i_sb, err);
2564 static int ext4_rmdir(
struct inode *dir,
struct dentry *dentry)
2567 struct inode *
inode;
2568 struct buffer_head *bh;
2579 return PTR_ERR(handle);
2582 bh = ext4_find_entry(dir, &dentry->
d_name, &de);
2587 ext4_handle_sync(handle);
2596 if (!empty_dir(inode))
2599 retval = ext4_delete_entry(handle, dir, de, bh);
2603 ext4_warning(inode->
i_sb,
2604 "empty directory has too many links (%d)",
2615 ext4_dec_count(handle, dir);
2616 ext4_update_dx_flag(dir);
2625 static int ext4_unlink(
struct inode *dir,
struct dentry *dentry)
2628 struct inode *
inode;
2629 struct buffer_head *bh;
2633 trace_ext4_unlink_enter(dir, dentry);
2641 return PTR_ERR(handle);
2644 ext4_handle_sync(handle);
2647 bh = ext4_find_entry(dir, &dentry->
d_name, &de);
2658 ext4_warning(inode->
i_sb,
2659 "Deleting nonexistent file (%lu), %d",
2663 retval = ext4_delete_entry(handle, dir, de, bh);
2667 ext4_update_dx_flag(dir);
2672 inode->
i_ctime = ext4_current_time(inode);
2679 trace_ext4_unlink_exit(dentry, retval);
2683 static int ext4_symlink(
struct inode *dir,
2684 struct dentry *dentry,
const char *symname)
2687 struct inode *
inode;
2688 int l,
err, retries = 0;
2692 if (l > dir->
i_sb->s_blocksize)
2718 handle = ext4_journal_start(dir, credits);
2720 return PTR_ERR(handle);
2723 ext4_handle_sync(handle);
2727 err = PTR_ERR(inode);
2748 goto err_drop_inode;
2751 goto err_drop_inode;
2756 handle = ext4_journal_start(dir,
2759 if (IS_ERR(handle)) {
2760 err = PTR_ERR(handle);
2761 goto err_drop_inode;
2768 goto err_drop_inode;
2778 err = ext4_add_nondir(handle, dentry, inode);
2790 static int ext4_link(
struct dentry *old_dentry,
2791 struct inode *dir,
struct dentry *dentry)
2794 struct inode *inode = old_dentry->
d_inode;
2795 int err, retries = 0;
2806 return PTR_ERR(handle);
2809 ext4_handle_sync(handle);
2811 inode->
i_ctime = ext4_current_time(inode);
2812 ext4_inc_count(handle, inode);
2815 err = ext4_add_entry(handle, dentry, inode);
2829 #define PARENT_INO(buffer, size) \
2830 (ext4_next_entry((struct ext4_dir_entry_2 *)(buffer), size)->inode)
2836 static int ext4_rename(
struct inode *old_dir,
struct dentry *old_dentry,
2837 struct inode *new_dir,
struct dentry *new_dentry)
2841 struct buffer_head *old_bh, *new_bh, *dir_bh;
2843 int retval, force_da_alloc = 0;
2848 old_bh = new_bh = dir_bh =
NULL;
2854 handle = ext4_journal_start(old_dir, 2 *
2858 return PTR_ERR(handle);
2861 ext4_handle_sync(handle);
2863 old_bh = ext4_find_entry(old_dir, &old_dentry->
d_name, &old_de);
2870 old_inode = old_dentry->
d_inode;
2875 new_inode = new_dentry->
d_inode;
2876 new_bh = ext4_find_entry(new_dir, &new_dentry->
d_name, &new_de);
2886 if (!empty_dir(new_inode))
2890 if (!(dir_bh =
ext4_bread(handle, old_inode, 0, 0, &retval))) {
2893 ext4_error(old_inode->
i_sb,
2894 "Directory hole detected on inode %lu\n",
2899 if (!buffer_verified(dir_bh) &&
2903 set_buffer_verified(dir_bh);
2905 old_dir->
i_sb->s_blocksize)) != old_dir->
i_ino)
2908 if (!new_inode && new_dir != old_dir &&
2911 BUFFER_TRACE(dir_bh,
"get_write_access");
2917 retval = ext4_add_entry(handle, new_dentry, old_inode);
2921 BUFFER_TRACE(new_bh,
"get write access");
2931 ext4_current_time(new_dir);
2933 BUFFER_TRACE(new_bh,
"call ext4_handle_dirty_metadata");
2934 retval = ext4_handle_dirty_dirent_node(handle, new_dir, new_bh);
2936 ext4_std_error(new_dir->
i_sb, retval);
2947 old_inode->
i_ctime = ext4_current_time(old_inode);
2956 (retval = ext4_delete_entry(handle, old_dir,
2957 old_de, old_bh)) == -
ENOENT) {
2962 struct buffer_head *old_bh2;
2965 old_bh2 = ext4_find_entry(old_dir, &old_dentry->
d_name, &old_de2);
2967 retval = ext4_delete_entry(handle, old_dir,
2973 ext4_warning(old_dir->
i_sb,
2974 "Deleting old file (%lu), %d, error=%d",
2979 ext4_dec_count(handle, new_inode);
2980 new_inode->
i_ctime = ext4_current_time(new_inode);
2982 old_dir->
i_ctime = old_dir->
i_mtime = ext4_current_time(old_dir);
2983 ext4_update_dx_flag(old_dir);
2987 BUFFER_TRACE(dir_bh,
"call ext4_handle_dirty_metadata");
2988 if (
is_dx(old_inode)) {
2989 retval = ext4_handle_dirty_dx_node(handle,
2993 retval = ext4_handle_dirty_dirent_node(handle,
2998 ext4_std_error(old_dir->
i_sb, retval);
3001 ext4_dec_count(handle, old_dir);
3007 ext4_inc_count(handle, new_dir);
3008 ext4_update_dx_flag(new_dir);
3027 if (retval == 0 && force_da_alloc)
3036 .create = ext4_create,
3037 .lookup = ext4_lookup,
3039 .unlink = ext4_unlink,
3040 .symlink = ext4_symlink,
3041 .mkdir = ext4_mkdir,
3042 .rmdir = ext4_rmdir,
3043 .mknod = ext4_mknod,
3044 .rename = ext4_rename,
3046 #ifdef CONFIG_EXT4_FS_XATTR
3058 #ifdef CONFIG_EXT4_FS_XATTR