]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - fs/ext4/namei.c
ext4: make ext4_delete_entry generic
[mirror_ubuntu-zesty-kernel.git] / fs / ext4 / namei.c
1 /*
2 * linux/fs/ext4/namei.c
3 *
4 * Copyright (C) 1992, 1993, 1994, 1995
5 * Remy Card (card@masi.ibp.fr)
6 * Laboratoire MASI - Institut Blaise Pascal
7 * Universite Pierre et Marie Curie (Paris VI)
8 *
9 * from
10 *
11 * linux/fs/minix/namei.c
12 *
13 * Copyright (C) 1991, 1992 Linus Torvalds
14 *
15 * Big-endian to little-endian byte-swapping/bitmaps by
16 * David S. Miller (davem@caip.rutgers.edu), 1995
17 * Directory entry file type support and forward compatibility hooks
18 * for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998
19 * Hash Tree Directory indexing (c)
20 * Daniel Phillips, 2001
21 * Hash Tree Directory indexing porting
22 * Christopher Li, 2002
23 * Hash Tree Directory indexing cleanup
24 * Theodore Ts'o, 2002
25 */
26
27 #include <linux/fs.h>
28 #include <linux/pagemap.h>
29 #include <linux/jbd2.h>
30 #include <linux/time.h>
31 #include <linux/fcntl.h>
32 #include <linux/stat.h>
33 #include <linux/string.h>
34 #include <linux/quotaops.h>
35 #include <linux/buffer_head.h>
36 #include <linux/bio.h>
37 #include "ext4.h"
38 #include "ext4_jbd2.h"
39
40 #include "xattr.h"
41 #include "acl.h"
42
43 #include <trace/events/ext4.h>
44 /*
45 * define how far ahead to read directories while searching them.
46 */
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))
51
52 static struct buffer_head *ext4_append(handle_t *handle,
53 struct inode *inode,
54 ext4_lblk_t *block, int *err)
55 {
56 struct buffer_head *bh;
57
58 if (unlikely(EXT4_SB(inode->i_sb)->s_max_dir_size_kb &&
59 ((inode->i_size >> 10) >=
60 EXT4_SB(inode->i_sb)->s_max_dir_size_kb))) {
61 *err = -ENOSPC;
62 return NULL;
63 }
64
65 *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
66
67 bh = ext4_bread(handle, inode, *block, 1, err);
68 if (bh) {
69 inode->i_size += inode->i_sb->s_blocksize;
70 EXT4_I(inode)->i_disksize = inode->i_size;
71 *err = ext4_journal_get_write_access(handle, bh);
72 if (*err) {
73 brelse(bh);
74 bh = NULL;
75 }
76 }
77 if (!bh && !(*err)) {
78 *err = -EIO;
79 ext4_error(inode->i_sb,
80 "Directory hole detected on inode %lu\n",
81 inode->i_ino);
82 }
83 return bh;
84 }
85
86 #ifndef assert
87 #define assert(test) J_ASSERT(test)
88 #endif
89
90 #ifdef DX_DEBUG
91 #define dxtrace(command) command
92 #else
93 #define dxtrace(command)
94 #endif
95
96 struct fake_dirent
97 {
98 __le32 inode;
99 __le16 rec_len;
100 u8 name_len;
101 u8 file_type;
102 };
103
104 struct dx_countlimit
105 {
106 __le16 limit;
107 __le16 count;
108 };
109
110 struct dx_entry
111 {
112 __le32 hash;
113 __le32 block;
114 };
115
116 /*
117 * dx_root_info is laid out so that if it should somehow get overlaid by a
118 * dirent the two low bits of the hash version will be zero. Therefore, the
119 * hash version mod 4 should never be 0. Sincerely, the paranoia department.
120 */
121
122 struct dx_root
123 {
124 struct fake_dirent dot;
125 char dot_name[4];
126 struct fake_dirent dotdot;
127 char dotdot_name[4];
128 struct dx_root_info
129 {
130 __le32 reserved_zero;
131 u8 hash_version;
132 u8 info_length; /* 8 */
133 u8 indirect_levels;
134 u8 unused_flags;
135 }
136 info;
137 struct dx_entry entries[0];
138 };
139
140 struct dx_node
141 {
142 struct fake_dirent fake;
143 struct dx_entry entries[0];
144 };
145
146
147 struct dx_frame
148 {
149 struct buffer_head *bh;
150 struct dx_entry *entries;
151 struct dx_entry *at;
152 };
153
154 struct dx_map_entry
155 {
156 u32 hash;
157 u16 offs;
158 u16 size;
159 };
160
161 /*
162 * This goes at the end of each htree block.
163 */
164 struct dx_tail {
165 u32 dt_reserved;
166 __le32 dt_checksum; /* crc32c(uuid+inum+dirblock) */
167 };
168
169 static inline ext4_lblk_t dx_get_block(struct dx_entry *entry);
170 static void dx_set_block(struct dx_entry *entry, ext4_lblk_t value);
171 static inline unsigned dx_get_hash(struct dx_entry *entry);
172 static void dx_set_hash(struct dx_entry *entry, unsigned value);
173 static unsigned dx_get_count(struct dx_entry *entries);
174 static unsigned dx_get_limit(struct dx_entry *entries);
175 static void dx_set_count(struct dx_entry *entries, unsigned value);
176 static void dx_set_limit(struct dx_entry *entries, unsigned value);
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,
180 struct inode *dir,
181 struct dx_hash_info *hinfo,
182 struct dx_frame *frame,
183 int *err);
184 static void dx_release(struct dx_frame *frames);
185 static int dx_make_map(struct ext4_dir_entry_2 *de, unsigned blocksize,
186 struct dx_hash_info *hinfo, struct dx_map_entry map[]);
187 static void dx_sort_map(struct dx_map_entry *map, unsigned count);
188 static struct ext4_dir_entry_2 *dx_move_dirents(char *from, char *to,
189 struct dx_map_entry *offsets, int count, unsigned blocksize);
190 static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize);
191 static void dx_insert_block(struct dx_frame *frame,
192 u32 hash, ext4_lblk_t block);
193 static int ext4_htree_next_block(struct inode *dir, __u32 hash,
194 struct dx_frame *frame,
195 struct dx_frame *frames,
196 __u32 *start_hash);
197 static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
198 const struct qstr *d_name,
199 struct ext4_dir_entry_2 **res_dir,
200 int *err);
201 static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
202 struct inode *inode);
203
204 /* checksumming functions */
205 void initialize_dirent_tail(struct ext4_dir_entry_tail *t,
206 unsigned int blocksize)
207 {
208 memset(t, 0, sizeof(struct ext4_dir_entry_tail));
209 t->det_rec_len = ext4_rec_len_to_disk(
210 sizeof(struct ext4_dir_entry_tail), blocksize);
211 t->det_reserved_ft = EXT4_FT_DIR_CSUM;
212 }
213
214 /* Walk through a dirent block to find a checksum "dirent" at the tail */
215 static struct ext4_dir_entry_tail *get_dirent_tail(struct inode *inode,
216 struct ext4_dir_entry *de)
217 {
218 struct ext4_dir_entry_tail *t;
219
220 #ifdef PARANOID
221 struct ext4_dir_entry *d, *top;
222
223 d = de;
224 top = (struct ext4_dir_entry *)(((void *)de) +
225 (EXT4_BLOCK_SIZE(inode->i_sb) -
226 sizeof(struct ext4_dir_entry_tail)));
227 while (d < top && d->rec_len)
228 d = (struct ext4_dir_entry *)(((void *)d) +
229 le16_to_cpu(d->rec_len));
230
231 if (d != top)
232 return NULL;
233
234 t = (struct ext4_dir_entry_tail *)d;
235 #else
236 t = EXT4_DIRENT_TAIL(de, EXT4_BLOCK_SIZE(inode->i_sb));
237 #endif
238
239 if (t->det_reserved_zero1 ||
240 le16_to_cpu(t->det_rec_len) != sizeof(struct ext4_dir_entry_tail) ||
241 t->det_reserved_zero2 ||
242 t->det_reserved_ft != EXT4_FT_DIR_CSUM)
243 return NULL;
244
245 return t;
246 }
247
248 static __le32 ext4_dirent_csum(struct inode *inode,
249 struct ext4_dir_entry *dirent, int size)
250 {
251 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
252 struct ext4_inode_info *ei = EXT4_I(inode);
253 __u32 csum;
254
255 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
256 return cpu_to_le32(csum);
257 }
258
259 static void warn_no_space_for_csum(struct inode *inode)
260 {
261 ext4_warning(inode->i_sb, "no space in directory inode %lu leaf for "
262 "checksum. Please run e2fsck -D.", inode->i_ino);
263 }
264
265 int ext4_dirent_csum_verify(struct inode *inode, struct ext4_dir_entry *dirent)
266 {
267 struct ext4_dir_entry_tail *t;
268
269 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
270 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
271 return 1;
272
273 t = get_dirent_tail(inode, dirent);
274 if (!t) {
275 warn_no_space_for_csum(inode);
276 return 0;
277 }
278
279 if (t->det_checksum != ext4_dirent_csum(inode, dirent,
280 (void *)t - (void *)dirent))
281 return 0;
282
283 return 1;
284 }
285
286 static void ext4_dirent_csum_set(struct inode *inode,
287 struct ext4_dir_entry *dirent)
288 {
289 struct ext4_dir_entry_tail *t;
290
291 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
292 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
293 return;
294
295 t = get_dirent_tail(inode, dirent);
296 if (!t) {
297 warn_no_space_for_csum(inode);
298 return;
299 }
300
301 t->det_checksum = ext4_dirent_csum(inode, dirent,
302 (void *)t - (void *)dirent);
303 }
304
305 int ext4_handle_dirty_dirent_node(handle_t *handle,
306 struct inode *inode,
307 struct buffer_head *bh)
308 {
309 ext4_dirent_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
310 return ext4_handle_dirty_metadata(handle, inode, bh);
311 }
312
313 static struct dx_countlimit *get_dx_countlimit(struct inode *inode,
314 struct ext4_dir_entry *dirent,
315 int *offset)
316 {
317 struct ext4_dir_entry *dp;
318 struct dx_root_info *root;
319 int count_offset;
320
321 if (le16_to_cpu(dirent->rec_len) == EXT4_BLOCK_SIZE(inode->i_sb))
322 count_offset = 8;
323 else if (le16_to_cpu(dirent->rec_len) == 12) {
324 dp = (struct ext4_dir_entry *)(((void *)dirent) + 12);
325 if (le16_to_cpu(dp->rec_len) !=
326 EXT4_BLOCK_SIZE(inode->i_sb) - 12)
327 return NULL;
328 root = (struct dx_root_info *)(((void *)dp + 12));
329 if (root->reserved_zero ||
330 root->info_length != sizeof(struct dx_root_info))
331 return NULL;
332 count_offset = 32;
333 } else
334 return NULL;
335
336 if (offset)
337 *offset = count_offset;
338 return (struct dx_countlimit *)(((void *)dirent) + count_offset);
339 }
340
341 static __le32 ext4_dx_csum(struct inode *inode, struct ext4_dir_entry *dirent,
342 int count_offset, int count, struct dx_tail *t)
343 {
344 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
345 struct ext4_inode_info *ei = EXT4_I(inode);
346 __u32 csum, old_csum;
347 int size;
348
349 size = count_offset + (count * sizeof(struct dx_entry));
350 old_csum = t->dt_checksum;
351 t->dt_checksum = 0;
352 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
353 csum = ext4_chksum(sbi, csum, (__u8 *)t, sizeof(struct dx_tail));
354 t->dt_checksum = old_csum;
355
356 return cpu_to_le32(csum);
357 }
358
359 static int ext4_dx_csum_verify(struct inode *inode,
360 struct ext4_dir_entry *dirent)
361 {
362 struct dx_countlimit *c;
363 struct dx_tail *t;
364 int count_offset, limit, count;
365
366 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
367 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
368 return 1;
369
370 c = get_dx_countlimit(inode, dirent, &count_offset);
371 if (!c) {
372 EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D.");
373 return 1;
374 }
375 limit = le16_to_cpu(c->limit);
376 count = le16_to_cpu(c->count);
377 if (count_offset + (limit * sizeof(struct dx_entry)) >
378 EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
379 warn_no_space_for_csum(inode);
380 return 1;
381 }
382 t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
383
384 if (t->dt_checksum != ext4_dx_csum(inode, dirent, count_offset,
385 count, t))
386 return 0;
387 return 1;
388 }
389
390 static void ext4_dx_csum_set(struct inode *inode, struct ext4_dir_entry *dirent)
391 {
392 struct dx_countlimit *c;
393 struct dx_tail *t;
394 int count_offset, limit, count;
395
396 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
397 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
398 return;
399
400 c = get_dx_countlimit(inode, dirent, &count_offset);
401 if (!c) {
402 EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D.");
403 return;
404 }
405 limit = le16_to_cpu(c->limit);
406 count = le16_to_cpu(c->count);
407 if (count_offset + (limit * sizeof(struct dx_entry)) >
408 EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
409 warn_no_space_for_csum(inode);
410 return;
411 }
412 t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
413
414 t->dt_checksum = ext4_dx_csum(inode, dirent, count_offset, count, t);
415 }
416
417 static inline int ext4_handle_dirty_dx_node(handle_t *handle,
418 struct inode *inode,
419 struct buffer_head *bh)
420 {
421 ext4_dx_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
422 return ext4_handle_dirty_metadata(handle, inode, bh);
423 }
424
425 /*
426 * p is at least 6 bytes before the end of page
427 */
428 static inline struct ext4_dir_entry_2 *
429 ext4_next_entry(struct ext4_dir_entry_2 *p, unsigned long blocksize)
430 {
431 return (struct ext4_dir_entry_2 *)((char *)p +
432 ext4_rec_len_from_disk(p->rec_len, blocksize));
433 }
434
435 /*
436 * Future: use high four bits of block for coalesce-on-delete flags
437 * Mask them off for now.
438 */
439
440 static inline ext4_lblk_t dx_get_block(struct dx_entry *entry)
441 {
442 return le32_to_cpu(entry->block) & 0x00ffffff;
443 }
444
445 static inline void dx_set_block(struct dx_entry *entry, ext4_lblk_t value)
446 {
447 entry->block = cpu_to_le32(value);
448 }
449
450 static inline unsigned dx_get_hash(struct dx_entry *entry)
451 {
452 return le32_to_cpu(entry->hash);
453 }
454
455 static inline void dx_set_hash(struct dx_entry *entry, unsigned value)
456 {
457 entry->hash = cpu_to_le32(value);
458 }
459
460 static inline unsigned dx_get_count(struct dx_entry *entries)
461 {
462 return le16_to_cpu(((struct dx_countlimit *) entries)->count);
463 }
464
465 static inline unsigned dx_get_limit(struct dx_entry *entries)
466 {
467 return le16_to_cpu(((struct dx_countlimit *) entries)->limit);
468 }
469
470 static inline void dx_set_count(struct dx_entry *entries, unsigned value)
471 {
472 ((struct dx_countlimit *) entries)->count = cpu_to_le16(value);
473 }
474
475 static inline void dx_set_limit(struct dx_entry *entries, unsigned value)
476 {
477 ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
478 }
479
480 static inline unsigned dx_root_limit(struct inode *dir, unsigned infosize)
481 {
482 unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(1) -
483 EXT4_DIR_REC_LEN(2) - infosize;
484
485 if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb,
486 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
487 entry_space -= sizeof(struct dx_tail);
488 return entry_space / sizeof(struct dx_entry);
489 }
490
491 static inline unsigned dx_node_limit(struct inode *dir)
492 {
493 unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(0);
494
495 if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb,
496 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
497 entry_space -= sizeof(struct dx_tail);
498 return entry_space / sizeof(struct dx_entry);
499 }
500
501 /*
502 * Debug
503 */
504 #ifdef DX_DEBUG
505 static void dx_show_index(char * label, struct dx_entry *entries)
506 {
507 int i, n = dx_get_count (entries);
508 printk(KERN_DEBUG "%s index ", label);
509 for (i = 0; i < n; i++) {
510 printk("%x->%lu ", i ? dx_get_hash(entries + i) :
511 0, (unsigned long)dx_get_block(entries + i));
512 }
513 printk("\n");
514 }
515
516 struct stats
517 {
518 unsigned names;
519 unsigned space;
520 unsigned bcount;
521 };
522
523 static struct stats dx_show_leaf(struct dx_hash_info *hinfo, struct ext4_dir_entry_2 *de,
524 int size, int show_names)
525 {
526 unsigned names = 0, space = 0;
527 char *base = (char *) de;
528 struct dx_hash_info h = *hinfo;
529
530 printk("names: ");
531 while ((char *) de < base + size)
532 {
533 if (de->inode)
534 {
535 if (show_names)
536 {
537 int len = de->name_len;
538 char *name = de->name;
539 while (len--) printk("%c", *name++);
540 ext4fs_dirhash(de->name, de->name_len, &h);
541 printk(":%x.%u ", h.hash,
542 (unsigned) ((char *) de - base));
543 }
544 space += EXT4_DIR_REC_LEN(de->name_len);
545 names++;
546 }
547 de = ext4_next_entry(de, size);
548 }
549 printk("(%i)\n", names);
550 return (struct stats) { names, space, 1 };
551 }
552
553 struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
554 struct dx_entry *entries, int levels)
555 {
556 unsigned blocksize = dir->i_sb->s_blocksize;
557 unsigned count = dx_get_count(entries), names = 0, space = 0, i;
558 unsigned bcount = 0;
559 struct buffer_head *bh;
560 int err;
561 printk("%i indexed blocks...\n", count);
562 for (i = 0; i < count; i++, entries++)
563 {
564 ext4_lblk_t block = dx_get_block(entries);
565 ext4_lblk_t hash = i ? dx_get_hash(entries): 0;
566 u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
567 struct stats stats;
568 printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range);
569 if (!(bh = ext4_bread (NULL,dir, block, 0,&err))) continue;
570 stats = levels?
571 dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
572 dx_show_leaf(hinfo, (struct ext4_dir_entry_2 *) bh->b_data, blocksize, 0);
573 names += stats.names;
574 space += stats.space;
575 bcount += stats.bcount;
576 brelse(bh);
577 }
578 if (bcount)
579 printk(KERN_DEBUG "%snames %u, fullness %u (%u%%)\n",
580 levels ? "" : " ", names, space/bcount,
581 (space/bcount)*100/blocksize);
582 return (struct stats) { names, space, bcount};
583 }
584 #endif /* DX_DEBUG */
585
586 /*
587 * Probe for a directory leaf block to search.
588 *
589 * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
590 * error in the directory index, and the caller should fall back to
591 * searching the directory normally. The callers of dx_probe **MUST**
592 * check for this error code, and make sure it never gets reflected
593 * back to userspace.
594 */
595 static struct dx_frame *
596 dx_probe(const struct qstr *d_name, struct inode *dir,
597 struct dx_hash_info *hinfo, struct dx_frame *frame_in, int *err)
598 {
599 unsigned count, indirect;
600 struct dx_entry *at, *entries, *p, *q, *m;
601 struct dx_root *root;
602 struct buffer_head *bh;
603 struct dx_frame *frame = frame_in;
604 u32 hash;
605
606 frame->bh = NULL;
607 if (!(bh = ext4_bread(NULL, dir, 0, 0, err))) {
608 if (*err == 0)
609 *err = ERR_BAD_DX_DIR;
610 goto fail;
611 }
612 root = (struct dx_root *) bh->b_data;
613 if (root->info.hash_version != DX_HASH_TEA &&
614 root->info.hash_version != DX_HASH_HALF_MD4 &&
615 root->info.hash_version != DX_HASH_LEGACY) {
616 ext4_warning(dir->i_sb, "Unrecognised inode hash code %d",
617 root->info.hash_version);
618 brelse(bh);
619 *err = ERR_BAD_DX_DIR;
620 goto fail;
621 }
622 hinfo->hash_version = root->info.hash_version;
623 if (hinfo->hash_version <= DX_HASH_TEA)
624 hinfo->hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
625 hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed;
626 if (d_name)
627 ext4fs_dirhash(d_name->name, d_name->len, hinfo);
628 hash = hinfo->hash;
629
630 if (root->info.unused_flags & 1) {
631 ext4_warning(dir->i_sb, "Unimplemented inode hash flags: %#06x",
632 root->info.unused_flags);
633 brelse(bh);
634 *err = ERR_BAD_DX_DIR;
635 goto fail;
636 }
637
638 if ((indirect = root->info.indirect_levels) > 1) {
639 ext4_warning(dir->i_sb, "Unimplemented inode hash depth: %#06x",
640 root->info.indirect_levels);
641 brelse(bh);
642 *err = ERR_BAD_DX_DIR;
643 goto fail;
644 }
645
646 if (!buffer_verified(bh) &&
647 !ext4_dx_csum_verify(dir, (struct ext4_dir_entry *)bh->b_data)) {
648 ext4_warning(dir->i_sb, "Root failed checksum");
649 brelse(bh);
650 *err = ERR_BAD_DX_DIR;
651 goto fail;
652 }
653 set_buffer_verified(bh);
654
655 entries = (struct dx_entry *) (((char *)&root->info) +
656 root->info.info_length);
657
658 if (dx_get_limit(entries) != dx_root_limit(dir,
659 root->info.info_length)) {
660 ext4_warning(dir->i_sb, "dx entry: limit != root limit");
661 brelse(bh);
662 *err = ERR_BAD_DX_DIR;
663 goto fail;
664 }
665
666 dxtrace(printk("Look up %x", hash));
667 while (1)
668 {
669 count = dx_get_count(entries);
670 if (!count || count > dx_get_limit(entries)) {
671 ext4_warning(dir->i_sb,
672 "dx entry: no count or count > limit");
673 brelse(bh);
674 *err = ERR_BAD_DX_DIR;
675 goto fail2;
676 }
677
678 p = entries + 1;
679 q = entries + count - 1;
680 while (p <= q)
681 {
682 m = p + (q - p)/2;
683 dxtrace(printk("."));
684 if (dx_get_hash(m) > hash)
685 q = m - 1;
686 else
687 p = m + 1;
688 }
689
690 if (0) // linear search cross check
691 {
692 unsigned n = count - 1;
693 at = entries;
694 while (n--)
695 {
696 dxtrace(printk(","));
697 if (dx_get_hash(++at) > hash)
698 {
699 at--;
700 break;
701 }
702 }
703 assert (at == p - 1);
704 }
705
706 at = p - 1;
707 dxtrace(printk(" %x->%u\n", at == entries? 0: dx_get_hash(at), dx_get_block(at)));
708 frame->bh = bh;
709 frame->entries = entries;
710 frame->at = at;
711 if (!indirect--) return frame;
712 if (!(bh = ext4_bread(NULL, dir, dx_get_block(at), 0, err))) {
713 if (!(*err))
714 *err = ERR_BAD_DX_DIR;
715 goto fail2;
716 }
717 at = entries = ((struct dx_node *) bh->b_data)->entries;
718
719 if (!buffer_verified(bh) &&
720 !ext4_dx_csum_verify(dir,
721 (struct ext4_dir_entry *)bh->b_data)) {
722 ext4_warning(dir->i_sb, "Node failed checksum");
723 brelse(bh);
724 *err = ERR_BAD_DX_DIR;
725 goto fail;
726 }
727 set_buffer_verified(bh);
728
729 if (dx_get_limit(entries) != dx_node_limit (dir)) {
730 ext4_warning(dir->i_sb,
731 "dx entry: limit != node limit");
732 brelse(bh);
733 *err = ERR_BAD_DX_DIR;
734 goto fail2;
735 }
736 frame++;
737 frame->bh = NULL;
738 }
739 fail2:
740 while (frame >= frame_in) {
741 brelse(frame->bh);
742 frame--;
743 }
744 fail:
745 if (*err == ERR_BAD_DX_DIR)
746 ext4_warning(dir->i_sb,
747 "Corrupt dir inode %lu, running e2fsck is "
748 "recommended.", dir->i_ino);
749 return NULL;
750 }
751
752 static void dx_release (struct dx_frame *frames)
753 {
754 if (frames[0].bh == NULL)
755 return;
756
757 if (((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels)
758 brelse(frames[1].bh);
759 brelse(frames[0].bh);
760 }
761
762 /*
763 * This function increments the frame pointer to search the next leaf
764 * block, and reads in the necessary intervening nodes if the search
765 * should be necessary. Whether or not the search is necessary is
766 * controlled by the hash parameter. If the hash value is even, then
767 * the search is only continued if the next block starts with that
768 * hash value. This is used if we are searching for a specific file.
769 *
770 * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
771 *
772 * This function returns 1 if the caller should continue to search,
773 * or 0 if it should not. If there is an error reading one of the
774 * index blocks, it will a negative error code.
775 *
776 * If start_hash is non-null, it will be filled in with the starting
777 * hash of the next page.
778 */
779 static int ext4_htree_next_block(struct inode *dir, __u32 hash,
780 struct dx_frame *frame,
781 struct dx_frame *frames,
782 __u32 *start_hash)
783 {
784 struct dx_frame *p;
785 struct buffer_head *bh;
786 int err, num_frames = 0;
787 __u32 bhash;
788
789 p = frame;
790 /*
791 * Find the next leaf page by incrementing the frame pointer.
792 * If we run out of entries in the interior node, loop around and
793 * increment pointer in the parent node. When we break out of
794 * this loop, num_frames indicates the number of interior
795 * nodes need to be read.
796 */
797 while (1) {
798 if (++(p->at) < p->entries + dx_get_count(p->entries))
799 break;
800 if (p == frames)
801 return 0;
802 num_frames++;
803 p--;
804 }
805
806 /*
807 * If the hash is 1, then continue only if the next page has a
808 * continuation hash of any value. This is used for readdir
809 * handling. Otherwise, check to see if the hash matches the
810 * desired contiuation hash. If it doesn't, return since
811 * there's no point to read in the successive index pages.
812 */
813 bhash = dx_get_hash(p->at);
814 if (start_hash)
815 *start_hash = bhash;
816 if ((hash & 1) == 0) {
817 if ((bhash & ~1) != hash)
818 return 0;
819 }
820 /*
821 * If the hash is HASH_NB_ALWAYS, we always go to the next
822 * block so no check is necessary
823 */
824 while (num_frames--) {
825 if (!(bh = ext4_bread(NULL, dir, dx_get_block(p->at),
826 0, &err))) {
827 if (!err) {
828 ext4_error(dir->i_sb,
829 "Directory hole detected on inode %lu\n",
830 dir->i_ino);
831 return -EIO;
832 }
833 return err; /* Failure */
834 }
835
836 if (!buffer_verified(bh) &&
837 !ext4_dx_csum_verify(dir,
838 (struct ext4_dir_entry *)bh->b_data)) {
839 ext4_warning(dir->i_sb, "Node failed checksum");
840 return -EIO;
841 }
842 set_buffer_verified(bh);
843
844 p++;
845 brelse(p->bh);
846 p->bh = bh;
847 p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
848 }
849 return 1;
850 }
851
852
853 /*
854 * This function fills a red-black tree with information from a
855 * directory block. It returns the number directory entries loaded
856 * into the tree. If there is an error it is returned in err.
857 */
858 static int htree_dirblock_to_tree(struct file *dir_file,
859 struct inode *dir, ext4_lblk_t block,
860 struct dx_hash_info *hinfo,
861 __u32 start_hash, __u32 start_minor_hash)
862 {
863 struct buffer_head *bh;
864 struct ext4_dir_entry_2 *de, *top;
865 int err = 0, count = 0;
866
867 dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n",
868 (unsigned long)block));
869 if (!(bh = ext4_bread(NULL, dir, block, 0, &err))) {
870 if (!err) {
871 err = -EIO;
872 ext4_error(dir->i_sb,
873 "Directory hole detected on inode %lu\n",
874 dir->i_ino);
875 }
876 return err;
877 }
878
879 if (!buffer_verified(bh) &&
880 !ext4_dirent_csum_verify(dir, (struct ext4_dir_entry *)bh->b_data))
881 return -EIO;
882 set_buffer_verified(bh);
883
884 de = (struct ext4_dir_entry_2 *) bh->b_data;
885 top = (struct ext4_dir_entry_2 *) ((char *) de +
886 dir->i_sb->s_blocksize -
887 EXT4_DIR_REC_LEN(0));
888 for (; de < top; de = ext4_next_entry(de, dir->i_sb->s_blocksize)) {
889 if (ext4_check_dir_entry(dir, NULL, de, bh,
890 bh->b_data, bh->b_size,
891 (block<<EXT4_BLOCK_SIZE_BITS(dir->i_sb))
892 + ((char *)de - bh->b_data))) {
893 /* On error, skip the f_pos to the next block. */
894 dir_file->f_pos = (dir_file->f_pos |
895 (dir->i_sb->s_blocksize - 1)) + 1;
896 brelse(bh);
897 return count;
898 }
899 ext4fs_dirhash(de->name, de->name_len, hinfo);
900 if ((hinfo->hash < start_hash) ||
901 ((hinfo->hash == start_hash) &&
902 (hinfo->minor_hash < start_minor_hash)))
903 continue;
904 if (de->inode == 0)
905 continue;
906 if ((err = ext4_htree_store_dirent(dir_file,
907 hinfo->hash, hinfo->minor_hash, de)) != 0) {
908 brelse(bh);
909 return err;
910 }
911 count++;
912 }
913 brelse(bh);
914 return count;
915 }
916
917
918 /*
919 * This function fills a red-black tree with information from a
920 * directory. We start scanning the directory in hash order, starting
921 * at start_hash and start_minor_hash.
922 *
923 * This function returns the number of entries inserted into the tree,
924 * or a negative error code.
925 */
926 int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
927 __u32 start_minor_hash, __u32 *next_hash)
928 {
929 struct dx_hash_info hinfo;
930 struct ext4_dir_entry_2 *de;
931 struct dx_frame frames[2], *frame;
932 struct inode *dir;
933 ext4_lblk_t block;
934 int count = 0;
935 int ret, err;
936 __u32 hashval;
937
938 dxtrace(printk(KERN_DEBUG "In htree_fill_tree, start hash: %x:%x\n",
939 start_hash, start_minor_hash));
940 dir = dir_file->f_path.dentry->d_inode;
941 if (!(ext4_test_inode_flag(dir, EXT4_INODE_INDEX))) {
942 hinfo.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
943 if (hinfo.hash_version <= DX_HASH_TEA)
944 hinfo.hash_version +=
945 EXT4_SB(dir->i_sb)->s_hash_unsigned;
946 hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
947 count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
948 start_hash, start_minor_hash);
949 *next_hash = ~0;
950 return count;
951 }
952 hinfo.hash = start_hash;
953 hinfo.minor_hash = 0;
954 frame = dx_probe(NULL, dir, &hinfo, frames, &err);
955 if (!frame)
956 return err;
957
958 /* Add '.' and '..' from the htree header */
959 if (!start_hash && !start_minor_hash) {
960 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
961 if ((err = ext4_htree_store_dirent(dir_file, 0, 0, de)) != 0)
962 goto errout;
963 count++;
964 }
965 if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
966 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
967 de = ext4_next_entry(de, dir->i_sb->s_blocksize);
968 if ((err = ext4_htree_store_dirent(dir_file, 2, 0, de)) != 0)
969 goto errout;
970 count++;
971 }
972
973 while (1) {
974 block = dx_get_block(frame->at);
975 ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
976 start_hash, start_minor_hash);
977 if (ret < 0) {
978 err = ret;
979 goto errout;
980 }
981 count += ret;
982 hashval = ~0;
983 ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
984 frame, frames, &hashval);
985 *next_hash = hashval;
986 if (ret < 0) {
987 err = ret;
988 goto errout;
989 }
990 /*
991 * Stop if: (a) there are no more entries, or
992 * (b) we have inserted at least one entry and the
993 * next hash value is not a continuation
994 */
995 if ((ret == 0) ||
996 (count && ((hashval & 1) == 0)))
997 break;
998 }
999 dx_release(frames);
1000 dxtrace(printk(KERN_DEBUG "Fill tree: returned %d entries, "
1001 "next hash: %x\n", count, *next_hash));
1002 return count;
1003 errout:
1004 dx_release(frames);
1005 return (err);
1006 }
1007
1008 static inline int search_dirblock(struct buffer_head *bh,
1009 struct inode *dir,
1010 const struct qstr *d_name,
1011 unsigned int offset,
1012 struct ext4_dir_entry_2 **res_dir)
1013 {
1014 return search_dir(bh, bh->b_data, dir->i_sb->s_blocksize, dir,
1015 d_name, offset, res_dir);
1016 }
1017
1018 /*
1019 * Directory block splitting, compacting
1020 */
1021
1022 /*
1023 * Create map of hash values, offsets, and sizes, stored at end of block.
1024 * Returns number of entries mapped.
1025 */
1026 static int dx_make_map(struct ext4_dir_entry_2 *de, unsigned blocksize,
1027 struct dx_hash_info *hinfo,
1028 struct dx_map_entry *map_tail)
1029 {
1030 int count = 0;
1031 char *base = (char *) de;
1032 struct dx_hash_info h = *hinfo;
1033
1034 while ((char *) de < base + blocksize) {
1035 if (de->name_len && de->inode) {
1036 ext4fs_dirhash(de->name, de->name_len, &h);
1037 map_tail--;
1038 map_tail->hash = h.hash;
1039 map_tail->offs = ((char *) de - base)>>2;
1040 map_tail->size = le16_to_cpu(de->rec_len);
1041 count++;
1042 cond_resched();
1043 }
1044 /* XXX: do we need to check rec_len == 0 case? -Chris */
1045 de = ext4_next_entry(de, blocksize);
1046 }
1047 return count;
1048 }
1049
1050 /* Sort map by hash value */
1051 static void dx_sort_map (struct dx_map_entry *map, unsigned count)
1052 {
1053 struct dx_map_entry *p, *q, *top = map + count - 1;
1054 int more;
1055 /* Combsort until bubble sort doesn't suck */
1056 while (count > 2) {
1057 count = count*10/13;
1058 if (count - 9 < 2) /* 9, 10 -> 11 */
1059 count = 11;
1060 for (p = top, q = p - count; q >= map; p--, q--)
1061 if (p->hash < q->hash)
1062 swap(*p, *q);
1063 }
1064 /* Garden variety bubble sort */
1065 do {
1066 more = 0;
1067 q = top;
1068 while (q-- > map) {
1069 if (q[1].hash >= q[0].hash)
1070 continue;
1071 swap(*(q+1), *q);
1072 more = 1;
1073 }
1074 } while(more);
1075 }
1076
1077 static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
1078 {
1079 struct dx_entry *entries = frame->entries;
1080 struct dx_entry *old = frame->at, *new = old + 1;
1081 int count = dx_get_count(entries);
1082
1083 assert(count < dx_get_limit(entries));
1084 assert(old < entries + count);
1085 memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
1086 dx_set_hash(new, hash);
1087 dx_set_block(new, block);
1088 dx_set_count(entries, count + 1);
1089 }
1090
1091 /*
1092 * NOTE! unlike strncmp, ext4_match returns 1 for success, 0 for failure.
1093 *
1094 * `len <= EXT4_NAME_LEN' is guaranteed by caller.
1095 * `de != NULL' is guaranteed by caller.
1096 */
1097 static inline int ext4_match (int len, const char * const name,
1098 struct ext4_dir_entry_2 * de)
1099 {
1100 if (len != de->name_len)
1101 return 0;
1102 if (!de->inode)
1103 return 0;
1104 return !memcmp(name, de->name, len);
1105 }
1106
1107 /*
1108 * Returns 0 if not found, -1 on failure, and 1 on success
1109 */
1110 int search_dir(struct buffer_head *bh,
1111 char *search_buf,
1112 int buf_size,
1113 struct inode *dir,
1114 const struct qstr *d_name,
1115 unsigned int offset,
1116 struct ext4_dir_entry_2 **res_dir)
1117 {
1118 struct ext4_dir_entry_2 * de;
1119 char * dlimit;
1120 int de_len;
1121 const char *name = d_name->name;
1122 int namelen = d_name->len;
1123
1124 de = (struct ext4_dir_entry_2 *)search_buf;
1125 dlimit = search_buf + buf_size;
1126 while ((char *) de < dlimit) {
1127 /* this code is executed quadratically often */
1128 /* do minimal checking `by hand' */
1129
1130 if ((char *) de + namelen <= dlimit &&
1131 ext4_match (namelen, name, de)) {
1132 /* found a match - just to be sure, do a full check */
1133 if (ext4_check_dir_entry(dir, NULL, de, bh, bh->b_data,
1134 bh->b_size, offset))
1135 return -1;
1136 *res_dir = de;
1137 return 1;
1138 }
1139 /* prevent looping on a bad block */
1140 de_len = ext4_rec_len_from_disk(de->rec_len,
1141 dir->i_sb->s_blocksize);
1142 if (de_len <= 0)
1143 return -1;
1144 offset += de_len;
1145 de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
1146 }
1147 return 0;
1148 }
1149
1150 static int is_dx_internal_node(struct inode *dir, ext4_lblk_t block,
1151 struct ext4_dir_entry *de)
1152 {
1153 struct super_block *sb = dir->i_sb;
1154
1155 if (!is_dx(dir))
1156 return 0;
1157 if (block == 0)
1158 return 1;
1159 if (de->inode == 0 &&
1160 ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) ==
1161 sb->s_blocksize)
1162 return 1;
1163 return 0;
1164 }
1165
1166 /*
1167 * ext4_find_entry()
1168 *
1169 * finds an entry in the specified directory with the wanted name. It
1170 * returns the cache buffer in which the entry was found, and the entry
1171 * itself (as a parameter - res_dir). It does NOT read the inode of the
1172 * entry - you'll have to do that yourself if you want to.
1173 *
1174 * The returned buffer_head has ->b_count elevated. The caller is expected
1175 * to brelse() it when appropriate.
1176 */
1177 static struct buffer_head * ext4_find_entry (struct inode *dir,
1178 const struct qstr *d_name,
1179 struct ext4_dir_entry_2 ** res_dir)
1180 {
1181 struct super_block *sb;
1182 struct buffer_head *bh_use[NAMEI_RA_SIZE];
1183 struct buffer_head *bh, *ret = NULL;
1184 ext4_lblk_t start, block, b;
1185 const u8 *name = d_name->name;
1186 int ra_max = 0; /* Number of bh's in the readahead
1187 buffer, bh_use[] */
1188 int ra_ptr = 0; /* Current index into readahead
1189 buffer */
1190 int num = 0;
1191 ext4_lblk_t nblocks;
1192 int i, err;
1193 int namelen;
1194
1195 *res_dir = NULL;
1196 sb = dir->i_sb;
1197 namelen = d_name->len;
1198 if (namelen > EXT4_NAME_LEN)
1199 return NULL;
1200
1201 if (ext4_has_inline_data(dir)) {
1202 int has_inline_data = 1;
1203 ret = ext4_find_inline_entry(dir, d_name, res_dir,
1204 &has_inline_data);
1205 if (has_inline_data)
1206 return ret;
1207 }
1208
1209 if ((namelen <= 2) && (name[0] == '.') &&
1210 (name[1] == '.' || name[1] == '\0')) {
1211 /*
1212 * "." or ".." will only be in the first block
1213 * NFS may look up ".."; "." should be handled by the VFS
1214 */
1215 block = start = 0;
1216 nblocks = 1;
1217 goto restart;
1218 }
1219 if (is_dx(dir)) {
1220 bh = ext4_dx_find_entry(dir, d_name, res_dir, &err);
1221 /*
1222 * On success, or if the error was file not found,
1223 * return. Otherwise, fall back to doing a search the
1224 * old fashioned way.
1225 */
1226 if (bh || (err != ERR_BAD_DX_DIR))
1227 return bh;
1228 dxtrace(printk(KERN_DEBUG "ext4_find_entry: dx failed, "
1229 "falling back\n"));
1230 }
1231 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
1232 start = EXT4_I(dir)->i_dir_start_lookup;
1233 if (start >= nblocks)
1234 start = 0;
1235 block = start;
1236 restart:
1237 do {
1238 /*
1239 * We deal with the read-ahead logic here.
1240 */
1241 if (ra_ptr >= ra_max) {
1242 /* Refill the readahead buffer */
1243 ra_ptr = 0;
1244 b = block;
1245 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
1246 /*
1247 * Terminate if we reach the end of the
1248 * directory and must wrap, or if our
1249 * search has finished at this block.
1250 */
1251 if (b >= nblocks || (num && block == start)) {
1252 bh_use[ra_max] = NULL;
1253 break;
1254 }
1255 num++;
1256 bh = ext4_getblk(NULL, dir, b++, 0, &err);
1257 bh_use[ra_max] = bh;
1258 if (bh)
1259 ll_rw_block(READ | REQ_META | REQ_PRIO,
1260 1, &bh);
1261 }
1262 }
1263 if ((bh = bh_use[ra_ptr++]) == NULL)
1264 goto next;
1265 wait_on_buffer(bh);
1266 if (!buffer_uptodate(bh)) {
1267 /* read error, skip block & hope for the best */
1268 EXT4_ERROR_INODE(dir, "reading directory lblock %lu",
1269 (unsigned long) block);
1270 brelse(bh);
1271 goto next;
1272 }
1273 if (!buffer_verified(bh) &&
1274 !is_dx_internal_node(dir, block,
1275 (struct ext4_dir_entry *)bh->b_data) &&
1276 !ext4_dirent_csum_verify(dir,
1277 (struct ext4_dir_entry *)bh->b_data)) {
1278 EXT4_ERROR_INODE(dir, "checksumming directory "
1279 "block %lu", (unsigned long)block);
1280 brelse(bh);
1281 goto next;
1282 }
1283 set_buffer_verified(bh);
1284 i = search_dirblock(bh, dir, d_name,
1285 block << EXT4_BLOCK_SIZE_BITS(sb), res_dir);
1286 if (i == 1) {
1287 EXT4_I(dir)->i_dir_start_lookup = block;
1288 ret = bh;
1289 goto cleanup_and_exit;
1290 } else {
1291 brelse(bh);
1292 if (i < 0)
1293 goto cleanup_and_exit;
1294 }
1295 next:
1296 if (++block >= nblocks)
1297 block = 0;
1298 } while (block != start);
1299
1300 /*
1301 * If the directory has grown while we were searching, then
1302 * search the last part of the directory before giving up.
1303 */
1304 block = nblocks;
1305 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
1306 if (block < nblocks) {
1307 start = 0;
1308 goto restart;
1309 }
1310
1311 cleanup_and_exit:
1312 /* Clean up the read-ahead blocks */
1313 for (; ra_ptr < ra_max; ra_ptr++)
1314 brelse(bh_use[ra_ptr]);
1315 return ret;
1316 }
1317
1318 static struct buffer_head * ext4_dx_find_entry(struct inode *dir, const struct qstr *d_name,
1319 struct ext4_dir_entry_2 **res_dir, int *err)
1320 {
1321 struct super_block * sb = dir->i_sb;
1322 struct dx_hash_info hinfo;
1323 struct dx_frame frames[2], *frame;
1324 struct buffer_head *bh;
1325 ext4_lblk_t block;
1326 int retval;
1327
1328 if (!(frame = dx_probe(d_name, dir, &hinfo, frames, err)))
1329 return NULL;
1330 do {
1331 block = dx_get_block(frame->at);
1332 if (!(bh = ext4_bread(NULL, dir, block, 0, err))) {
1333 if (!(*err)) {
1334 *err = -EIO;
1335 ext4_error(dir->i_sb,
1336 "Directory hole detected on inode %lu\n",
1337 dir->i_ino);
1338 }
1339 goto errout;
1340 }
1341
1342 if (!buffer_verified(bh) &&
1343 !ext4_dirent_csum_verify(dir,
1344 (struct ext4_dir_entry *)bh->b_data)) {
1345 EXT4_ERROR_INODE(dir, "checksumming directory "
1346 "block %lu", (unsigned long)block);
1347 brelse(bh);
1348 *err = -EIO;
1349 goto errout;
1350 }
1351 set_buffer_verified(bh);
1352 retval = search_dirblock(bh, dir, d_name,
1353 block << EXT4_BLOCK_SIZE_BITS(sb),
1354 res_dir);
1355 if (retval == 1) { /* Success! */
1356 dx_release(frames);
1357 return bh;
1358 }
1359 brelse(bh);
1360 if (retval == -1) {
1361 *err = ERR_BAD_DX_DIR;
1362 goto errout;
1363 }
1364
1365 /* Check to see if we should continue to search */
1366 retval = ext4_htree_next_block(dir, hinfo.hash, frame,
1367 frames, NULL);
1368 if (retval < 0) {
1369 ext4_warning(sb,
1370 "error reading index page in directory #%lu",
1371 dir->i_ino);
1372 *err = retval;
1373 goto errout;
1374 }
1375 } while (retval == 1);
1376
1377 *err = -ENOENT;
1378 errout:
1379 dxtrace(printk(KERN_DEBUG "%s not found\n", d_name->name));
1380 dx_release (frames);
1381 return NULL;
1382 }
1383
1384 static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
1385 {
1386 struct inode *inode;
1387 struct ext4_dir_entry_2 *de;
1388 struct buffer_head *bh;
1389
1390 if (dentry->d_name.len > EXT4_NAME_LEN)
1391 return ERR_PTR(-ENAMETOOLONG);
1392
1393 bh = ext4_find_entry(dir, &dentry->d_name, &de);
1394 inode = NULL;
1395 if (bh) {
1396 __u32 ino = le32_to_cpu(de->inode);
1397 brelse(bh);
1398 if (!ext4_valid_inum(dir->i_sb, ino)) {
1399 EXT4_ERROR_INODE(dir, "bad inode number: %u", ino);
1400 return ERR_PTR(-EIO);
1401 }
1402 if (unlikely(ino == dir->i_ino)) {
1403 EXT4_ERROR_INODE(dir, "'%.*s' linked to parent dir",
1404 dentry->d_name.len,
1405 dentry->d_name.name);
1406 return ERR_PTR(-EIO);
1407 }
1408 inode = ext4_iget(dir->i_sb, ino);
1409 if (inode == ERR_PTR(-ESTALE)) {
1410 EXT4_ERROR_INODE(dir,
1411 "deleted inode referenced: %u",
1412 ino);
1413 return ERR_PTR(-EIO);
1414 }
1415 }
1416 return d_splice_alias(inode, dentry);
1417 }
1418
1419
1420 struct dentry *ext4_get_parent(struct dentry *child)
1421 {
1422 __u32 ino;
1423 static const struct qstr dotdot = QSTR_INIT("..", 2);
1424 struct ext4_dir_entry_2 * de;
1425 struct buffer_head *bh;
1426
1427 bh = ext4_find_entry(child->d_inode, &dotdot, &de);
1428 if (!bh)
1429 return ERR_PTR(-ENOENT);
1430 ino = le32_to_cpu(de->inode);
1431 brelse(bh);
1432
1433 if (!ext4_valid_inum(child->d_inode->i_sb, ino)) {
1434 EXT4_ERROR_INODE(child->d_inode,
1435 "bad parent inode number: %u", ino);
1436 return ERR_PTR(-EIO);
1437 }
1438
1439 return d_obtain_alias(ext4_iget(child->d_inode->i_sb, ino));
1440 }
1441
1442 #define S_SHIFT 12
1443 static unsigned char ext4_type_by_mode[S_IFMT >> S_SHIFT] = {
1444 [S_IFREG >> S_SHIFT] = EXT4_FT_REG_FILE,
1445 [S_IFDIR >> S_SHIFT] = EXT4_FT_DIR,
1446 [S_IFCHR >> S_SHIFT] = EXT4_FT_CHRDEV,
1447 [S_IFBLK >> S_SHIFT] = EXT4_FT_BLKDEV,
1448 [S_IFIFO >> S_SHIFT] = EXT4_FT_FIFO,
1449 [S_IFSOCK >> S_SHIFT] = EXT4_FT_SOCK,
1450 [S_IFLNK >> S_SHIFT] = EXT4_FT_SYMLINK,
1451 };
1452
1453 static inline void ext4_set_de_type(struct super_block *sb,
1454 struct ext4_dir_entry_2 *de,
1455 umode_t mode) {
1456 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FILETYPE))
1457 de->file_type = ext4_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
1458 }
1459
1460 /*
1461 * Move count entries from end of map between two memory locations.
1462 * Returns pointer to last entry moved.
1463 */
1464 static struct ext4_dir_entry_2 *
1465 dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count,
1466 unsigned blocksize)
1467 {
1468 unsigned rec_len = 0;
1469
1470 while (count--) {
1471 struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *)
1472 (from + (map->offs<<2));
1473 rec_len = EXT4_DIR_REC_LEN(de->name_len);
1474 memcpy (to, de, rec_len);
1475 ((struct ext4_dir_entry_2 *) to)->rec_len =
1476 ext4_rec_len_to_disk(rec_len, blocksize);
1477 de->inode = 0;
1478 map++;
1479 to += rec_len;
1480 }
1481 return (struct ext4_dir_entry_2 *) (to - rec_len);
1482 }
1483
1484 /*
1485 * Compact each dir entry in the range to the minimal rec_len.
1486 * Returns pointer to last entry in range.
1487 */
1488 static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize)
1489 {
1490 struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base;
1491 unsigned rec_len = 0;
1492
1493 prev = to = de;
1494 while ((char*)de < base + blocksize) {
1495 next = ext4_next_entry(de, blocksize);
1496 if (de->inode && de->name_len) {
1497 rec_len = EXT4_DIR_REC_LEN(de->name_len);
1498 if (de > to)
1499 memmove(to, de, rec_len);
1500 to->rec_len = ext4_rec_len_to_disk(rec_len, blocksize);
1501 prev = to;
1502 to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len);
1503 }
1504 de = next;
1505 }
1506 return prev;
1507 }
1508
1509 /*
1510 * Split a full leaf block to make room for a new dir entry.
1511 * Allocate a new block, and move entries so that they are approx. equally full.
1512 * Returns pointer to de in block into which the new entry will be inserted.
1513 */
1514 static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
1515 struct buffer_head **bh,struct dx_frame *frame,
1516 struct dx_hash_info *hinfo, int *error)
1517 {
1518 unsigned blocksize = dir->i_sb->s_blocksize;
1519 unsigned count, continued;
1520 struct buffer_head *bh2;
1521 ext4_lblk_t newblock;
1522 u32 hash2;
1523 struct dx_map_entry *map;
1524 char *data1 = (*bh)->b_data, *data2;
1525 unsigned split, move, size;
1526 struct ext4_dir_entry_2 *de = NULL, *de2;
1527 struct ext4_dir_entry_tail *t;
1528 int csum_size = 0;
1529 int err = 0, i;
1530
1531 if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb,
1532 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
1533 csum_size = sizeof(struct ext4_dir_entry_tail);
1534
1535 bh2 = ext4_append (handle, dir, &newblock, &err);
1536 if (!(bh2)) {
1537 brelse(*bh);
1538 *bh = NULL;
1539 goto errout;
1540 }
1541
1542 BUFFER_TRACE(*bh, "get_write_access");
1543 err = ext4_journal_get_write_access(handle, *bh);
1544 if (err)
1545 goto journal_error;
1546
1547 BUFFER_TRACE(frame->bh, "get_write_access");
1548 err = ext4_journal_get_write_access(handle, frame->bh);
1549 if (err)
1550 goto journal_error;
1551
1552 data2 = bh2->b_data;
1553
1554 /* create map in the end of data2 block */
1555 map = (struct dx_map_entry *) (data2 + blocksize);
1556 count = dx_make_map((struct ext4_dir_entry_2 *) data1,
1557 blocksize, hinfo, map);
1558 map -= count;
1559 dx_sort_map(map, count);
1560 /* Split the existing block in the middle, size-wise */
1561 size = 0;
1562 move = 0;
1563 for (i = count-1; i >= 0; i--) {
1564 /* is more than half of this entry in 2nd half of the block? */
1565 if (size + map[i].size/2 > blocksize/2)
1566 break;
1567 size += map[i].size;
1568 move++;
1569 }
1570 /* map index at which we will split */
1571 split = count - move;
1572 hash2 = map[split].hash;
1573 continued = hash2 == map[split - 1].hash;
1574 dxtrace(printk(KERN_INFO "Split block %lu at %x, %i/%i\n",
1575 (unsigned long)dx_get_block(frame->at),
1576 hash2, split, count-split));
1577
1578 /* Fancy dance to stay within two buffers */
1579 de2 = dx_move_dirents(data1, data2, map + split, count - split, blocksize);
1580 de = dx_pack_dirents(data1, blocksize);
1581 de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
1582 (char *) de,
1583 blocksize);
1584 de2->rec_len = ext4_rec_len_to_disk(data2 + (blocksize - csum_size) -
1585 (char *) de2,
1586 blocksize);
1587 if (csum_size) {
1588 t = EXT4_DIRENT_TAIL(data2, blocksize);
1589 initialize_dirent_tail(t, blocksize);
1590
1591 t = EXT4_DIRENT_TAIL(data1, blocksize);
1592 initialize_dirent_tail(t, blocksize);
1593 }
1594
1595 dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data1, blocksize, 1));
1596 dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data2, blocksize, 1));
1597
1598 /* Which block gets the new entry? */
1599 if (hinfo->hash >= hash2)
1600 {
1601 swap(*bh, bh2);
1602 de = de2;
1603 }
1604 dx_insert_block(frame, hash2 + continued, newblock);
1605 err = ext4_handle_dirty_dirent_node(handle, dir, bh2);
1606 if (err)
1607 goto journal_error;
1608 err = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
1609 if (err)
1610 goto journal_error;
1611 brelse(bh2);
1612 dxtrace(dx_show_index("frame", frame->entries));
1613 return de;
1614
1615 journal_error:
1616 brelse(*bh);
1617 brelse(bh2);
1618 *bh = NULL;
1619 ext4_std_error(dir->i_sb, err);
1620 errout:
1621 *error = err;
1622 return NULL;
1623 }
1624
1625 int ext4_find_dest_de(struct inode *dir, struct inode *inode,
1626 struct buffer_head *bh,
1627 void *buf, int buf_size,
1628 const char *name, int namelen,
1629 struct ext4_dir_entry_2 **dest_de)
1630 {
1631 struct ext4_dir_entry_2 *de;
1632 unsigned short reclen = EXT4_DIR_REC_LEN(namelen);
1633 int nlen, rlen;
1634 unsigned int offset = 0;
1635 char *top;
1636
1637 de = (struct ext4_dir_entry_2 *)buf;
1638 top = buf + buf_size - reclen;
1639 while ((char *) de <= top) {
1640 if (ext4_check_dir_entry(dir, NULL, de, bh,
1641 buf, buf_size, offset))
1642 return -EIO;
1643 if (ext4_match(namelen, name, de))
1644 return -EEXIST;
1645 nlen = EXT4_DIR_REC_LEN(de->name_len);
1646 rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
1647 if ((de->inode ? rlen - nlen : rlen) >= reclen)
1648 break;
1649 de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
1650 offset += rlen;
1651 }
1652 if ((char *) de > top)
1653 return -ENOSPC;
1654
1655 *dest_de = de;
1656 return 0;
1657 }
1658
1659 void ext4_insert_dentry(struct inode *inode,
1660 struct ext4_dir_entry_2 *de,
1661 int buf_size,
1662 const char *name, int namelen)
1663 {
1664
1665 int nlen, rlen;
1666
1667 nlen = EXT4_DIR_REC_LEN(de->name_len);
1668 rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
1669 if (de->inode) {
1670 struct ext4_dir_entry_2 *de1 =
1671 (struct ext4_dir_entry_2 *)((char *)de + nlen);
1672 de1->rec_len = ext4_rec_len_to_disk(rlen - nlen, buf_size);
1673 de->rec_len = ext4_rec_len_to_disk(nlen, buf_size);
1674 de = de1;
1675 }
1676 de->file_type = EXT4_FT_UNKNOWN;
1677 de->inode = cpu_to_le32(inode->i_ino);
1678 ext4_set_de_type(inode->i_sb, de, inode->i_mode);
1679 de->name_len = namelen;
1680 memcpy(de->name, name, namelen);
1681 }
1682 /*
1683 * Add a new entry into a directory (leaf) block. If de is non-NULL,
1684 * it points to a directory entry which is guaranteed to be large
1685 * enough for new directory entry. If de is NULL, then
1686 * add_dirent_to_buf will attempt search the directory block for
1687 * space. It will return -ENOSPC if no space is available, and -EIO
1688 * and -EEXIST if directory entry already exists.
1689 */
1690 static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry,
1691 struct inode *inode, struct ext4_dir_entry_2 *de,
1692 struct buffer_head *bh)
1693 {
1694 struct inode *dir = dentry->d_parent->d_inode;
1695 const char *name = dentry->d_name.name;
1696 int namelen = dentry->d_name.len;
1697 unsigned int blocksize = dir->i_sb->s_blocksize;
1698 unsigned short reclen;
1699 int csum_size = 0;
1700 int err;
1701
1702 if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
1703 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
1704 csum_size = sizeof(struct ext4_dir_entry_tail);
1705
1706 reclen = EXT4_DIR_REC_LEN(namelen);
1707 if (!de) {
1708 err = ext4_find_dest_de(dir, inode,
1709 bh, bh->b_data, blocksize - csum_size,
1710 name, namelen, &de);
1711 if (err)
1712 return err;
1713 }
1714 BUFFER_TRACE(bh, "get_write_access");
1715 err = ext4_journal_get_write_access(handle, bh);
1716 if (err) {
1717 ext4_std_error(dir->i_sb, err);
1718 return err;
1719 }
1720
1721 /* By now the buffer is marked for journaling */
1722 ext4_insert_dentry(inode, de, blocksize, name, namelen);
1723
1724 /*
1725 * XXX shouldn't update any times until successful
1726 * completion of syscall, but too many callers depend
1727 * on this.
1728 *
1729 * XXX similarly, too many callers depend on
1730 * ext4_new_inode() setting the times, but error
1731 * recovery deletes the inode, so the worst that can
1732 * happen is that the times are slightly out of date
1733 * and/or different from the directory change time.
1734 */
1735 dir->i_mtime = dir->i_ctime = ext4_current_time(dir);
1736 ext4_update_dx_flag(dir);
1737 dir->i_version++;
1738 ext4_mark_inode_dirty(handle, dir);
1739 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
1740 err = ext4_handle_dirty_dirent_node(handle, dir, bh);
1741 if (err)
1742 ext4_std_error(dir->i_sb, err);
1743 return 0;
1744 }
1745
1746 /*
1747 * This converts a one block unindexed directory to a 3 block indexed
1748 * directory, and adds the dentry to the indexed directory.
1749 */
1750 static int make_indexed_dir(handle_t *handle, struct dentry *dentry,
1751 struct inode *inode, struct buffer_head *bh)
1752 {
1753 struct inode *dir = dentry->d_parent->d_inode;
1754 const char *name = dentry->d_name.name;
1755 int namelen = dentry->d_name.len;
1756 struct buffer_head *bh2;
1757 struct dx_root *root;
1758 struct dx_frame frames[2], *frame;
1759 struct dx_entry *entries;
1760 struct ext4_dir_entry_2 *de, *de2;
1761 struct ext4_dir_entry_tail *t;
1762 char *data1, *top;
1763 unsigned len;
1764 int retval;
1765 unsigned blocksize;
1766 struct dx_hash_info hinfo;
1767 ext4_lblk_t block;
1768 struct fake_dirent *fde;
1769 int csum_size = 0;
1770
1771 if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
1772 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
1773 csum_size = sizeof(struct ext4_dir_entry_tail);
1774
1775 blocksize = dir->i_sb->s_blocksize;
1776 dxtrace(printk(KERN_DEBUG "Creating index: inode %lu\n", dir->i_ino));
1777 retval = ext4_journal_get_write_access(handle, bh);
1778 if (retval) {
1779 ext4_std_error(dir->i_sb, retval);
1780 brelse(bh);
1781 return retval;
1782 }
1783 root = (struct dx_root *) bh->b_data;
1784
1785 /* The 0th block becomes the root, move the dirents out */
1786 fde = &root->dotdot;
1787 de = (struct ext4_dir_entry_2 *)((char *)fde +
1788 ext4_rec_len_from_disk(fde->rec_len, blocksize));
1789 if ((char *) de >= (((char *) root) + blocksize)) {
1790 EXT4_ERROR_INODE(dir, "invalid rec_len for '..'");
1791 brelse(bh);
1792 return -EIO;
1793 }
1794 len = ((char *) root) + (blocksize - csum_size) - (char *) de;
1795
1796 /* Allocate new block for the 0th block's dirents */
1797 bh2 = ext4_append(handle, dir, &block, &retval);
1798 if (!(bh2)) {
1799 brelse(bh);
1800 return retval;
1801 }
1802 ext4_set_inode_flag(dir, EXT4_INODE_INDEX);
1803 data1 = bh2->b_data;
1804
1805 memcpy (data1, de, len);
1806 de = (struct ext4_dir_entry_2 *) data1;
1807 top = data1 + len;
1808 while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top)
1809 de = de2;
1810 de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
1811 (char *) de,
1812 blocksize);
1813
1814 if (csum_size) {
1815 t = EXT4_DIRENT_TAIL(data1, blocksize);
1816 initialize_dirent_tail(t, blocksize);
1817 }
1818
1819 /* Initialize the root; the dot dirents already exist */
1820 de = (struct ext4_dir_entry_2 *) (&root->dotdot);
1821 de->rec_len = ext4_rec_len_to_disk(blocksize - EXT4_DIR_REC_LEN(2),
1822 blocksize);
1823 memset (&root->info, 0, sizeof(root->info));
1824 root->info.info_length = sizeof(root->info);
1825 root->info.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
1826 entries = root->entries;
1827 dx_set_block(entries, 1);
1828 dx_set_count(entries, 1);
1829 dx_set_limit(entries, dx_root_limit(dir, sizeof(root->info)));
1830
1831 /* Initialize as for dx_probe */
1832 hinfo.hash_version = root->info.hash_version;
1833 if (hinfo.hash_version <= DX_HASH_TEA)
1834 hinfo.hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
1835 hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
1836 ext4fs_dirhash(name, namelen, &hinfo);
1837 frame = frames;
1838 frame->entries = entries;
1839 frame->at = entries;
1840 frame->bh = bh;
1841 bh = bh2;
1842
1843 ext4_handle_dirty_dx_node(handle, dir, frame->bh);
1844 ext4_handle_dirty_dirent_node(handle, dir, bh);
1845
1846 de = do_split(handle,dir, &bh, frame, &hinfo, &retval);
1847 if (!de) {
1848 /*
1849 * Even if the block split failed, we have to properly write
1850 * out all the changes we did so far. Otherwise we can end up
1851 * with corrupted filesystem.
1852 */
1853 ext4_mark_inode_dirty(handle, dir);
1854 dx_release(frames);
1855 return retval;
1856 }
1857 dx_release(frames);
1858
1859 retval = add_dirent_to_buf(handle, dentry, inode, de, bh);
1860 brelse(bh);
1861 return retval;
1862 }
1863
1864 /*
1865 * ext4_add_entry()
1866 *
1867 * adds a file entry to the specified directory, using the same
1868 * semantics as ext4_find_entry(). It returns NULL if it failed.
1869 *
1870 * NOTE!! The inode part of 'de' is left at 0 - which means you
1871 * may not sleep between calling this and putting something into
1872 * the entry, as someone else might have used it while you slept.
1873 */
1874 static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
1875 struct inode *inode)
1876 {
1877 struct inode *dir = dentry->d_parent->d_inode;
1878 struct buffer_head *bh;
1879 struct ext4_dir_entry_2 *de;
1880 struct ext4_dir_entry_tail *t;
1881 struct super_block *sb;
1882 int retval;
1883 int dx_fallback=0;
1884 unsigned blocksize;
1885 ext4_lblk_t block, blocks;
1886 int csum_size = 0;
1887
1888 if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
1889 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
1890 csum_size = sizeof(struct ext4_dir_entry_tail);
1891
1892 sb = dir->i_sb;
1893 blocksize = sb->s_blocksize;
1894 if (!dentry->d_name.len)
1895 return -EINVAL;
1896
1897 if (ext4_has_inline_data(dir)) {
1898 retval = ext4_try_add_inline_entry(handle, dentry, inode);
1899 if (retval < 0)
1900 return retval;
1901 if (retval == 1) {
1902 retval = 0;
1903 return retval;
1904 }
1905 }
1906
1907 if (is_dx(dir)) {
1908 retval = ext4_dx_add_entry(handle, dentry, inode);
1909 if (!retval || (retval != ERR_BAD_DX_DIR))
1910 return retval;
1911 ext4_clear_inode_flag(dir, EXT4_INODE_INDEX);
1912 dx_fallback++;
1913 ext4_mark_inode_dirty(handle, dir);
1914 }
1915 blocks = dir->i_size >> sb->s_blocksize_bits;
1916 for (block = 0; block < blocks; block++) {
1917 if (!(bh = ext4_bread(handle, dir, block, 0, &retval))) {
1918 if (!retval) {
1919 retval = -EIO;
1920 ext4_error(inode->i_sb,
1921 "Directory hole detected on inode %lu\n",
1922 inode->i_ino);
1923 }
1924 return retval;
1925 }
1926 if (!buffer_verified(bh) &&
1927 !ext4_dirent_csum_verify(dir,
1928 (struct ext4_dir_entry *)bh->b_data))
1929 return -EIO;
1930 set_buffer_verified(bh);
1931 retval = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
1932 if (retval != -ENOSPC) {
1933 brelse(bh);
1934 return retval;
1935 }
1936
1937 if (blocks == 1 && !dx_fallback &&
1938 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_DIR_INDEX))
1939 return make_indexed_dir(handle, dentry, inode, bh);
1940 brelse(bh);
1941 }
1942 bh = ext4_append(handle, dir, &block, &retval);
1943 if (!bh)
1944 return retval;
1945 de = (struct ext4_dir_entry_2 *) bh->b_data;
1946 de->inode = 0;
1947 de->rec_len = ext4_rec_len_to_disk(blocksize - csum_size, blocksize);
1948
1949 if (csum_size) {
1950 t = EXT4_DIRENT_TAIL(bh->b_data, blocksize);
1951 initialize_dirent_tail(t, blocksize);
1952 }
1953
1954 retval = add_dirent_to_buf(handle, dentry, inode, de, bh);
1955 brelse(bh);
1956 if (retval == 0)
1957 ext4_set_inode_state(inode, EXT4_STATE_NEWENTRY);
1958 return retval;
1959 }
1960
1961 /*
1962 * Returns 0 for success, or a negative error value
1963 */
1964 static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
1965 struct inode *inode)
1966 {
1967 struct dx_frame frames[2], *frame;
1968 struct dx_entry *entries, *at;
1969 struct dx_hash_info hinfo;
1970 struct buffer_head *bh;
1971 struct inode *dir = dentry->d_parent->d_inode;
1972 struct super_block *sb = dir->i_sb;
1973 struct ext4_dir_entry_2 *de;
1974 int err;
1975
1976 frame = dx_probe(&dentry->d_name, dir, &hinfo, frames, &err);
1977 if (!frame)
1978 return err;
1979 entries = frame->entries;
1980 at = frame->at;
1981
1982 if (!(bh = ext4_bread(handle, dir, dx_get_block(frame->at), 0, &err))) {
1983 if (!err) {
1984 err = -EIO;
1985 ext4_error(dir->i_sb,
1986 "Directory hole detected on inode %lu\n",
1987 dir->i_ino);
1988 }
1989 goto cleanup;
1990 }
1991
1992 if (!buffer_verified(bh) &&
1993 !ext4_dirent_csum_verify(dir, (struct ext4_dir_entry *)bh->b_data))
1994 goto journal_error;
1995 set_buffer_verified(bh);
1996
1997 BUFFER_TRACE(bh, "get_write_access");
1998 err = ext4_journal_get_write_access(handle, bh);
1999 if (err)
2000 goto journal_error;
2001
2002 err = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
2003 if (err != -ENOSPC)
2004 goto cleanup;
2005
2006 /* Block full, should compress but for now just split */
2007 dxtrace(printk(KERN_DEBUG "using %u of %u node entries\n",
2008 dx_get_count(entries), dx_get_limit(entries)));
2009 /* Need to split index? */
2010 if (dx_get_count(entries) == dx_get_limit(entries)) {
2011 ext4_lblk_t newblock;
2012 unsigned icount = dx_get_count(entries);
2013 int levels = frame - frames;
2014 struct dx_entry *entries2;
2015 struct dx_node *node2;
2016 struct buffer_head *bh2;
2017
2018 if (levels && (dx_get_count(frames->entries) ==
2019 dx_get_limit(frames->entries))) {
2020 ext4_warning(sb, "Directory index full!");
2021 err = -ENOSPC;
2022 goto cleanup;
2023 }
2024 bh2 = ext4_append (handle, dir, &newblock, &err);
2025 if (!(bh2))
2026 goto cleanup;
2027 node2 = (struct dx_node *)(bh2->b_data);
2028 entries2 = node2->entries;
2029 memset(&node2->fake, 0, sizeof(struct fake_dirent));
2030 node2->fake.rec_len = ext4_rec_len_to_disk(sb->s_blocksize,
2031 sb->s_blocksize);
2032 BUFFER_TRACE(frame->bh, "get_write_access");
2033 err = ext4_journal_get_write_access(handle, frame->bh);
2034 if (err)
2035 goto journal_error;
2036 if (levels) {
2037 unsigned icount1 = icount/2, icount2 = icount - icount1;
2038 unsigned hash2 = dx_get_hash(entries + icount1);
2039 dxtrace(printk(KERN_DEBUG "Split index %i/%i\n",
2040 icount1, icount2));
2041
2042 BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
2043 err = ext4_journal_get_write_access(handle,
2044 frames[0].bh);
2045 if (err)
2046 goto journal_error;
2047
2048 memcpy((char *) entries2, (char *) (entries + icount1),
2049 icount2 * sizeof(struct dx_entry));
2050 dx_set_count(entries, icount1);
2051 dx_set_count(entries2, icount2);
2052 dx_set_limit(entries2, dx_node_limit(dir));
2053
2054 /* Which index block gets the new entry? */
2055 if (at - entries >= icount1) {
2056 frame->at = at = at - entries - icount1 + entries2;
2057 frame->entries = entries = entries2;
2058 swap(frame->bh, bh2);
2059 }
2060 dx_insert_block(frames + 0, hash2, newblock);
2061 dxtrace(dx_show_index("node", frames[1].entries));
2062 dxtrace(dx_show_index("node",
2063 ((struct dx_node *) bh2->b_data)->entries));
2064 err = ext4_handle_dirty_dx_node(handle, dir, bh2);
2065 if (err)
2066 goto journal_error;
2067 brelse (bh2);
2068 } else {
2069 dxtrace(printk(KERN_DEBUG
2070 "Creating second level index...\n"));
2071 memcpy((char *) entries2, (char *) entries,
2072 icount * sizeof(struct dx_entry));
2073 dx_set_limit(entries2, dx_node_limit(dir));
2074
2075 /* Set up root */
2076 dx_set_count(entries, 1);
2077 dx_set_block(entries + 0, newblock);
2078 ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
2079
2080 /* Add new access path frame */
2081 frame = frames + 1;
2082 frame->at = at = at - entries + entries2;
2083 frame->entries = entries = entries2;
2084 frame->bh = bh2;
2085 err = ext4_journal_get_write_access(handle,
2086 frame->bh);
2087 if (err)
2088 goto journal_error;
2089 }
2090 err = ext4_handle_dirty_dx_node(handle, dir, frames[0].bh);
2091 if (err) {
2092 ext4_std_error(inode->i_sb, err);
2093 goto cleanup;
2094 }
2095 }
2096 de = do_split(handle, dir, &bh, frame, &hinfo, &err);
2097 if (!de)
2098 goto cleanup;
2099 err = add_dirent_to_buf(handle, dentry, inode, de, bh);
2100 goto cleanup;
2101
2102 journal_error:
2103 ext4_std_error(dir->i_sb, err);
2104 cleanup:
2105 if (bh)
2106 brelse(bh);
2107 dx_release(frames);
2108 return err;
2109 }
2110
2111 /*
2112 * ext4_generic_delete_entry deletes a directory entry by merging it
2113 * with the previous entry
2114 */
2115 int ext4_generic_delete_entry(handle_t *handle,
2116 struct inode *dir,
2117 struct ext4_dir_entry_2 *de_del,
2118 struct buffer_head *bh,
2119 void *entry_buf,
2120 int buf_size,
2121 int csum_size)
2122 {
2123 struct ext4_dir_entry_2 *de, *pde;
2124 unsigned int blocksize = dir->i_sb->s_blocksize;
2125 int i;
2126
2127 i = 0;
2128 pde = NULL;
2129 de = (struct ext4_dir_entry_2 *)entry_buf;
2130 while (i < buf_size - csum_size) {
2131 if (ext4_check_dir_entry(dir, NULL, de, bh,
2132 bh->b_data, bh->b_size, i))
2133 return -EIO;
2134 if (de == de_del) {
2135 if (pde)
2136 pde->rec_len = ext4_rec_len_to_disk(
2137 ext4_rec_len_from_disk(pde->rec_len,
2138 blocksize) +
2139 ext4_rec_len_from_disk(de->rec_len,
2140 blocksize),
2141 blocksize);
2142 else
2143 de->inode = 0;
2144 dir->i_version++;
2145 return 0;
2146 }
2147 i += ext4_rec_len_from_disk(de->rec_len, blocksize);
2148 pde = de;
2149 de = ext4_next_entry(de, blocksize);
2150 }
2151 return -ENOENT;
2152 }
2153
2154 static int ext4_delete_entry(handle_t *handle,
2155 struct inode *dir,
2156 struct ext4_dir_entry_2 *de_del,
2157 struct buffer_head *bh)
2158 {
2159 int err, csum_size = 0;
2160
2161 if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb,
2162 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
2163 csum_size = sizeof(struct ext4_dir_entry_tail);
2164
2165 BUFFER_TRACE(bh, "get_write_access");
2166 err = ext4_journal_get_write_access(handle, bh);
2167 if (unlikely(err))
2168 goto out;
2169
2170 err = ext4_generic_delete_entry(handle, dir, de_del,
2171 bh, bh->b_data,
2172 dir->i_sb->s_blocksize, csum_size);
2173 if (err)
2174 goto out;
2175
2176 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
2177 err = ext4_handle_dirty_dirent_node(handle, dir, bh);
2178 if (unlikely(err))
2179 goto out;
2180
2181 return 0;
2182 out:
2183 if (err != -ENOENT)
2184 ext4_std_error(dir->i_sb, err);
2185 return err;
2186 }
2187
2188 /*
2189 * DIR_NLINK feature is set if 1) nlinks > EXT4_LINK_MAX or 2) nlinks == 2,
2190 * since this indicates that nlinks count was previously 1.
2191 */
2192 static void ext4_inc_count(handle_t *handle, struct inode *inode)
2193 {
2194 inc_nlink(inode);
2195 if (is_dx(inode) && inode->i_nlink > 1) {
2196 /* limit is 16-bit i_links_count */
2197 if (inode->i_nlink >= EXT4_LINK_MAX || inode->i_nlink == 2) {
2198 set_nlink(inode, 1);
2199 EXT4_SET_RO_COMPAT_FEATURE(inode->i_sb,
2200 EXT4_FEATURE_RO_COMPAT_DIR_NLINK);
2201 }
2202 }
2203 }
2204
2205 /*
2206 * If a directory had nlink == 1, then we should let it be 1. This indicates
2207 * directory has >EXT4_LINK_MAX subdirs.
2208 */
2209 static void ext4_dec_count(handle_t *handle, struct inode *inode)
2210 {
2211 if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2)
2212 drop_nlink(inode);
2213 }
2214
2215
2216 static int ext4_add_nondir(handle_t *handle,
2217 struct dentry *dentry, struct inode *inode)
2218 {
2219 int err = ext4_add_entry(handle, dentry, inode);
2220 if (!err) {
2221 ext4_mark_inode_dirty(handle, inode);
2222 unlock_new_inode(inode);
2223 d_instantiate(dentry, inode);
2224 return 0;
2225 }
2226 drop_nlink(inode);
2227 unlock_new_inode(inode);
2228 iput(inode);
2229 return err;
2230 }
2231
2232 /*
2233 * By the time this is called, we already have created
2234 * the directory cache entry for the new file, but it
2235 * is so far negative - it has no inode.
2236 *
2237 * If the create succeeds, we fill in the inode information
2238 * with d_instantiate().
2239 */
2240 static int ext4_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2241 bool excl)
2242 {
2243 handle_t *handle;
2244 struct inode *inode;
2245 int err, retries = 0;
2246
2247 dquot_initialize(dir);
2248
2249 retry:
2250 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2251 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
2252 EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb));
2253 if (IS_ERR(handle))
2254 return PTR_ERR(handle);
2255
2256 if (IS_DIRSYNC(dir))
2257 ext4_handle_sync(handle);
2258
2259 inode = ext4_new_inode(handle, dir, mode, &dentry->d_name, 0, NULL);
2260 err = PTR_ERR(inode);
2261 if (!IS_ERR(inode)) {
2262 inode->i_op = &ext4_file_inode_operations;
2263 inode->i_fop = &ext4_file_operations;
2264 ext4_set_aops(inode);
2265 err = ext4_add_nondir(handle, dentry, inode);
2266 }
2267 ext4_journal_stop(handle);
2268 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2269 goto retry;
2270 return err;
2271 }
2272
2273 static int ext4_mknod(struct inode *dir, struct dentry *dentry,
2274 umode_t mode, dev_t rdev)
2275 {
2276 handle_t *handle;
2277 struct inode *inode;
2278 int err, retries = 0;
2279
2280 if (!new_valid_dev(rdev))
2281 return -EINVAL;
2282
2283 dquot_initialize(dir);
2284
2285 retry:
2286 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2287 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
2288 EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb));
2289 if (IS_ERR(handle))
2290 return PTR_ERR(handle);
2291
2292 if (IS_DIRSYNC(dir))
2293 ext4_handle_sync(handle);
2294
2295 inode = ext4_new_inode(handle, dir, mode, &dentry->d_name, 0, NULL);
2296 err = PTR_ERR(inode);
2297 if (!IS_ERR(inode)) {
2298 init_special_inode(inode, inode->i_mode, rdev);
2299 inode->i_op = &ext4_special_inode_operations;
2300 err = ext4_add_nondir(handle, dentry, inode);
2301 }
2302 ext4_journal_stop(handle);
2303 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2304 goto retry;
2305 return err;
2306 }
2307
2308 struct ext4_dir_entry_2 *ext4_init_dot_dotdot(struct inode *inode,
2309 struct ext4_dir_entry_2 *de,
2310 int blocksize, int csum_size,
2311 unsigned int parent_ino, int dotdot_real_len)
2312 {
2313 de->inode = cpu_to_le32(inode->i_ino);
2314 de->name_len = 1;
2315 de->rec_len = ext4_rec_len_to_disk(EXT4_DIR_REC_LEN(de->name_len),
2316 blocksize);
2317 strcpy(de->name, ".");
2318 ext4_set_de_type(inode->i_sb, de, S_IFDIR);
2319
2320 de = ext4_next_entry(de, blocksize);
2321 de->inode = cpu_to_le32(parent_ino);
2322 de->name_len = 2;
2323 if (!dotdot_real_len)
2324 de->rec_len = ext4_rec_len_to_disk(blocksize -
2325 (csum_size + EXT4_DIR_REC_LEN(1)),
2326 blocksize);
2327 else
2328 de->rec_len = ext4_rec_len_to_disk(
2329 EXT4_DIR_REC_LEN(de->name_len), blocksize);
2330 strcpy(de->name, "..");
2331 ext4_set_de_type(inode->i_sb, de, S_IFDIR);
2332
2333 return ext4_next_entry(de, blocksize);
2334 }
2335
2336 static int ext4_init_new_dir(handle_t *handle, struct inode *dir,
2337 struct inode *inode)
2338 {
2339 struct buffer_head *dir_block = NULL;
2340 struct ext4_dir_entry_2 *de;
2341 struct ext4_dir_entry_tail *t;
2342 unsigned int blocksize = dir->i_sb->s_blocksize;
2343 int csum_size = 0;
2344 int err;
2345
2346 if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb,
2347 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
2348 csum_size = sizeof(struct ext4_dir_entry_tail);
2349
2350 if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
2351 err = ext4_try_create_inline_dir(handle, dir, inode);
2352 if (err < 0 && err != -ENOSPC)
2353 goto out;
2354 if (!err)
2355 goto out;
2356 }
2357
2358 inode->i_size = EXT4_I(inode)->i_disksize = blocksize;
2359 dir_block = ext4_bread(handle, inode, 0, 1, &err);
2360 if (!(dir_block = ext4_bread(handle, inode, 0, 1, &err))) {
2361 if (!err) {
2362 err = -EIO;
2363 ext4_error(inode->i_sb,
2364 "Directory hole detected on inode %lu\n",
2365 inode->i_ino);
2366 }
2367 goto out;
2368 }
2369 BUFFER_TRACE(dir_block, "get_write_access");
2370 err = ext4_journal_get_write_access(handle, dir_block);
2371 if (err)
2372 goto out;
2373 de = (struct ext4_dir_entry_2 *)dir_block->b_data;
2374 ext4_init_dot_dotdot(inode, de, blocksize, csum_size, dir->i_ino, 0);
2375 set_nlink(inode, 2);
2376 if (csum_size) {
2377 t = EXT4_DIRENT_TAIL(dir_block->b_data, blocksize);
2378 initialize_dirent_tail(t, blocksize);
2379 }
2380
2381 BUFFER_TRACE(dir_block, "call ext4_handle_dirty_metadata");
2382 err = ext4_handle_dirty_dirent_node(handle, inode, dir_block);
2383 if (err)
2384 goto out;
2385 set_buffer_verified(dir_block);
2386 out:
2387 brelse(dir_block);
2388 return err;
2389 }
2390
2391 static int ext4_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
2392 {
2393 handle_t *handle;
2394 struct inode *inode;
2395 int err, retries = 0;
2396
2397 if (EXT4_DIR_LINK_MAX(dir))
2398 return -EMLINK;
2399
2400 dquot_initialize(dir);
2401
2402 retry:
2403 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2404 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
2405 EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb));
2406 if (IS_ERR(handle))
2407 return PTR_ERR(handle);
2408
2409 if (IS_DIRSYNC(dir))
2410 ext4_handle_sync(handle);
2411
2412 inode = ext4_new_inode(handle, dir, S_IFDIR | mode,
2413 &dentry->d_name, 0, NULL);
2414 err = PTR_ERR(inode);
2415 if (IS_ERR(inode))
2416 goto out_stop;
2417
2418 inode->i_op = &ext4_dir_inode_operations;
2419 inode->i_fop = &ext4_dir_operations;
2420 err = ext4_init_new_dir(handle, dir, inode);
2421 if (err)
2422 goto out_clear_inode;
2423 err = ext4_mark_inode_dirty(handle, inode);
2424 if (!err)
2425 err = ext4_add_entry(handle, dentry, inode);
2426 if (err) {
2427 out_clear_inode:
2428 clear_nlink(inode);
2429 unlock_new_inode(inode);
2430 ext4_mark_inode_dirty(handle, inode);
2431 iput(inode);
2432 goto out_stop;
2433 }
2434 ext4_inc_count(handle, dir);
2435 ext4_update_dx_flag(dir);
2436 err = ext4_mark_inode_dirty(handle, dir);
2437 if (err)
2438 goto out_clear_inode;
2439 unlock_new_inode(inode);
2440 d_instantiate(dentry, inode);
2441 out_stop:
2442 ext4_journal_stop(handle);
2443 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2444 goto retry;
2445 return err;
2446 }
2447
2448 /*
2449 * routine to check that the specified directory is empty (for rmdir)
2450 */
2451 static int empty_dir(struct inode *inode)
2452 {
2453 unsigned int offset;
2454 struct buffer_head *bh;
2455 struct ext4_dir_entry_2 *de, *de1;
2456 struct super_block *sb;
2457 int err = 0;
2458
2459 sb = inode->i_sb;
2460 if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2) ||
2461 !(bh = ext4_bread(NULL, inode, 0, 0, &err))) {
2462 if (err)
2463 EXT4_ERROR_INODE(inode,
2464 "error %d reading directory lblock 0", err);
2465 else
2466 ext4_warning(inode->i_sb,
2467 "bad directory (dir #%lu) - no data block",
2468 inode->i_ino);
2469 return 1;
2470 }
2471 if (!buffer_verified(bh) &&
2472 !ext4_dirent_csum_verify(inode,
2473 (struct ext4_dir_entry *)bh->b_data)) {
2474 EXT4_ERROR_INODE(inode, "checksum error reading directory "
2475 "lblock 0");
2476 return -EIO;
2477 }
2478 set_buffer_verified(bh);
2479 de = (struct ext4_dir_entry_2 *) bh->b_data;
2480 de1 = ext4_next_entry(de, sb->s_blocksize);
2481 if (le32_to_cpu(de->inode) != inode->i_ino ||
2482 !le32_to_cpu(de1->inode) ||
2483 strcmp(".", de->name) ||
2484 strcmp("..", de1->name)) {
2485 ext4_warning(inode->i_sb,
2486 "bad directory (dir #%lu) - no `.' or `..'",
2487 inode->i_ino);
2488 brelse(bh);
2489 return 1;
2490 }
2491 offset = ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) +
2492 ext4_rec_len_from_disk(de1->rec_len, sb->s_blocksize);
2493 de = ext4_next_entry(de1, sb->s_blocksize);
2494 while (offset < inode->i_size) {
2495 if (!bh ||
2496 (void *) de >= (void *) (bh->b_data+sb->s_blocksize)) {
2497 unsigned int lblock;
2498 err = 0;
2499 brelse(bh);
2500 lblock = offset >> EXT4_BLOCK_SIZE_BITS(sb);
2501 bh = ext4_bread(NULL, inode, lblock, 0, &err);
2502 if (!bh) {
2503 if (err)
2504 EXT4_ERROR_INODE(inode,
2505 "error %d reading directory "
2506 "lblock %u", err, lblock);
2507 else
2508 ext4_warning(inode->i_sb,
2509 "bad directory (dir #%lu) - no data block",
2510 inode->i_ino);
2511
2512 offset += sb->s_blocksize;
2513 continue;
2514 }
2515 if (!buffer_verified(bh) &&
2516 !ext4_dirent_csum_verify(inode,
2517 (struct ext4_dir_entry *)bh->b_data)) {
2518 EXT4_ERROR_INODE(inode, "checksum error "
2519 "reading directory lblock 0");
2520 return -EIO;
2521 }
2522 set_buffer_verified(bh);
2523 de = (struct ext4_dir_entry_2 *) bh->b_data;
2524 }
2525 if (ext4_check_dir_entry(inode, NULL, de, bh,
2526 bh->b_data, bh->b_size, offset)) {
2527 de = (struct ext4_dir_entry_2 *)(bh->b_data +
2528 sb->s_blocksize);
2529 offset = (offset | (sb->s_blocksize - 1)) + 1;
2530 continue;
2531 }
2532 if (le32_to_cpu(de->inode)) {
2533 brelse(bh);
2534 return 0;
2535 }
2536 offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
2537 de = ext4_next_entry(de, sb->s_blocksize);
2538 }
2539 brelse(bh);
2540 return 1;
2541 }
2542
2543 /* ext4_orphan_add() links an unlinked or truncated inode into a list of
2544 * such inodes, starting at the superblock, in case we crash before the
2545 * file is closed/deleted, or in case the inode truncate spans multiple
2546 * transactions and the last transaction is not recovered after a crash.
2547 *
2548 * At filesystem recovery time, we walk this list deleting unlinked
2549 * inodes and truncating linked inodes in ext4_orphan_cleanup().
2550 */
2551 int ext4_orphan_add(handle_t *handle, struct inode *inode)
2552 {
2553 struct super_block *sb = inode->i_sb;
2554 struct ext4_iloc iloc;
2555 int err = 0, rc;
2556
2557 if (!EXT4_SB(sb)->s_journal)
2558 return 0;
2559
2560 mutex_lock(&EXT4_SB(sb)->s_orphan_lock);
2561 if (!list_empty(&EXT4_I(inode)->i_orphan))
2562 goto out_unlock;
2563
2564 /*
2565 * Orphan handling is only valid for files with data blocks
2566 * being truncated, or files being unlinked. Note that we either
2567 * hold i_mutex, or the inode can not be referenced from outside,
2568 * so i_nlink should not be bumped due to race
2569 */
2570 J_ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
2571 S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
2572
2573 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
2574 err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
2575 if (err)
2576 goto out_unlock;
2577
2578 err = ext4_reserve_inode_write(handle, inode, &iloc);
2579 if (err)
2580 goto out_unlock;
2581 /*
2582 * Due to previous errors inode may be already a part of on-disk
2583 * orphan list. If so skip on-disk list modification.
2584 */
2585 if (NEXT_ORPHAN(inode) && NEXT_ORPHAN(inode) <=
2586 (le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count)))
2587 goto mem_insert;
2588
2589 /* Insert this inode at the head of the on-disk orphan list... */
2590 NEXT_ORPHAN(inode) = le32_to_cpu(EXT4_SB(sb)->s_es->s_last_orphan);
2591 EXT4_SB(sb)->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
2592 err = ext4_handle_dirty_super(handle, sb);
2593 rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
2594 if (!err)
2595 err = rc;
2596
2597 /* Only add to the head of the in-memory list if all the
2598 * previous operations succeeded. If the orphan_add is going to
2599 * fail (possibly taking the journal offline), we can't risk
2600 * leaving the inode on the orphan list: stray orphan-list
2601 * entries can cause panics at unmount time.
2602 *
2603 * This is safe: on error we're going to ignore the orphan list
2604 * anyway on the next recovery. */
2605 mem_insert:
2606 if (!err)
2607 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
2608
2609 jbd_debug(4, "superblock will point to %lu\n", inode->i_ino);
2610 jbd_debug(4, "orphan inode %lu will point to %d\n",
2611 inode->i_ino, NEXT_ORPHAN(inode));
2612 out_unlock:
2613 mutex_unlock(&EXT4_SB(sb)->s_orphan_lock);
2614 ext4_std_error(inode->i_sb, err);
2615 return err;
2616 }
2617
2618 /*
2619 * ext4_orphan_del() removes an unlinked or truncated inode from the list
2620 * of such inodes stored on disk, because it is finally being cleaned up.
2621 */
2622 int ext4_orphan_del(handle_t *handle, struct inode *inode)
2623 {
2624 struct list_head *prev;
2625 struct ext4_inode_info *ei = EXT4_I(inode);
2626 struct ext4_sb_info *sbi;
2627 __u32 ino_next;
2628 struct ext4_iloc iloc;
2629 int err = 0;
2630
2631 if (!EXT4_SB(inode->i_sb)->s_journal)
2632 return 0;
2633
2634 mutex_lock(&EXT4_SB(inode->i_sb)->s_orphan_lock);
2635 if (list_empty(&ei->i_orphan))
2636 goto out;
2637
2638 ino_next = NEXT_ORPHAN(inode);
2639 prev = ei->i_orphan.prev;
2640 sbi = EXT4_SB(inode->i_sb);
2641
2642 jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
2643
2644 list_del_init(&ei->i_orphan);
2645
2646 /* If we're on an error path, we may not have a valid
2647 * transaction handle with which to update the orphan list on
2648 * disk, but we still need to remove the inode from the linked
2649 * list in memory. */
2650 if (!handle)
2651 goto out;
2652
2653 err = ext4_reserve_inode_write(handle, inode, &iloc);
2654 if (err)
2655 goto out_err;
2656
2657 if (prev == &sbi->s_orphan) {
2658 jbd_debug(4, "superblock will point to %u\n", ino_next);
2659 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
2660 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
2661 if (err)
2662 goto out_brelse;
2663 sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
2664 err = ext4_handle_dirty_super(handle, inode->i_sb);
2665 } else {
2666 struct ext4_iloc iloc2;
2667 struct inode *i_prev =
2668 &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode;
2669
2670 jbd_debug(4, "orphan inode %lu will point to %u\n",
2671 i_prev->i_ino, ino_next);
2672 err = ext4_reserve_inode_write(handle, i_prev, &iloc2);
2673 if (err)
2674 goto out_brelse;
2675 NEXT_ORPHAN(i_prev) = ino_next;
2676 err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2);
2677 }
2678 if (err)
2679 goto out_brelse;
2680 NEXT_ORPHAN(inode) = 0;
2681 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
2682
2683 out_err:
2684 ext4_std_error(inode->i_sb, err);
2685 out:
2686 mutex_unlock(&EXT4_SB(inode->i_sb)->s_orphan_lock);
2687 return err;
2688
2689 out_brelse:
2690 brelse(iloc.bh);
2691 goto out_err;
2692 }
2693
2694 static int ext4_rmdir(struct inode *dir, struct dentry *dentry)
2695 {
2696 int retval;
2697 struct inode *inode;
2698 struct buffer_head *bh;
2699 struct ext4_dir_entry_2 *de;
2700 handle_t *handle;
2701
2702 /* Initialize quotas before so that eventual writes go in
2703 * separate transaction */
2704 dquot_initialize(dir);
2705 dquot_initialize(dentry->d_inode);
2706
2707 handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb));
2708 if (IS_ERR(handle))
2709 return PTR_ERR(handle);
2710
2711 retval = -ENOENT;
2712 bh = ext4_find_entry(dir, &dentry->d_name, &de);
2713 if (!bh)
2714 goto end_rmdir;
2715
2716 if (IS_DIRSYNC(dir))
2717 ext4_handle_sync(handle);
2718
2719 inode = dentry->d_inode;
2720
2721 retval = -EIO;
2722 if (le32_to_cpu(de->inode) != inode->i_ino)
2723 goto end_rmdir;
2724
2725 retval = -ENOTEMPTY;
2726 if (!empty_dir(inode))
2727 goto end_rmdir;
2728
2729 retval = ext4_delete_entry(handle, dir, de, bh);
2730 if (retval)
2731 goto end_rmdir;
2732 if (!EXT4_DIR_LINK_EMPTY(inode))
2733 ext4_warning(inode->i_sb,
2734 "empty directory has too many links (%d)",
2735 inode->i_nlink);
2736 inode->i_version++;
2737 clear_nlink(inode);
2738 /* There's no need to set i_disksize: the fact that i_nlink is
2739 * zero will ensure that the right thing happens during any
2740 * recovery. */
2741 inode->i_size = 0;
2742 ext4_orphan_add(handle, inode);
2743 inode->i_ctime = dir->i_ctime = dir->i_mtime = ext4_current_time(inode);
2744 ext4_mark_inode_dirty(handle, inode);
2745 ext4_dec_count(handle, dir);
2746 ext4_update_dx_flag(dir);
2747 ext4_mark_inode_dirty(handle, dir);
2748
2749 end_rmdir:
2750 ext4_journal_stop(handle);
2751 brelse(bh);
2752 return retval;
2753 }
2754
2755 static int ext4_unlink(struct inode *dir, struct dentry *dentry)
2756 {
2757 int retval;
2758 struct inode *inode;
2759 struct buffer_head *bh;
2760 struct ext4_dir_entry_2 *de;
2761 handle_t *handle;
2762
2763 trace_ext4_unlink_enter(dir, dentry);
2764 /* Initialize quotas before so that eventual writes go
2765 * in separate transaction */
2766 dquot_initialize(dir);
2767 dquot_initialize(dentry->d_inode);
2768
2769 handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb));
2770 if (IS_ERR(handle))
2771 return PTR_ERR(handle);
2772
2773 if (IS_DIRSYNC(dir))
2774 ext4_handle_sync(handle);
2775
2776 retval = -ENOENT;
2777 bh = ext4_find_entry(dir, &dentry->d_name, &de);
2778 if (!bh)
2779 goto end_unlink;
2780
2781 inode = dentry->d_inode;
2782
2783 retval = -EIO;
2784 if (le32_to_cpu(de->inode) != inode->i_ino)
2785 goto end_unlink;
2786
2787 if (!inode->i_nlink) {
2788 ext4_warning(inode->i_sb,
2789 "Deleting nonexistent file (%lu), %d",
2790 inode->i_ino, inode->i_nlink);
2791 set_nlink(inode, 1);
2792 }
2793 retval = ext4_delete_entry(handle, dir, de, bh);
2794 if (retval)
2795 goto end_unlink;
2796 dir->i_ctime = dir->i_mtime = ext4_current_time(dir);
2797 ext4_update_dx_flag(dir);
2798 ext4_mark_inode_dirty(handle, dir);
2799 drop_nlink(inode);
2800 if (!inode->i_nlink)
2801 ext4_orphan_add(handle, inode);
2802 inode->i_ctime = ext4_current_time(inode);
2803 ext4_mark_inode_dirty(handle, inode);
2804 retval = 0;
2805
2806 end_unlink:
2807 ext4_journal_stop(handle);
2808 brelse(bh);
2809 trace_ext4_unlink_exit(dentry, retval);
2810 return retval;
2811 }
2812
2813 static int ext4_symlink(struct inode *dir,
2814 struct dentry *dentry, const char *symname)
2815 {
2816 handle_t *handle;
2817 struct inode *inode;
2818 int l, err, retries = 0;
2819 int credits;
2820
2821 l = strlen(symname)+1;
2822 if (l > dir->i_sb->s_blocksize)
2823 return -ENAMETOOLONG;
2824
2825 dquot_initialize(dir);
2826
2827 if (l > EXT4_N_BLOCKS * 4) {
2828 /*
2829 * For non-fast symlinks, we just allocate inode and put it on
2830 * orphan list in the first transaction => we need bitmap,
2831 * group descriptor, sb, inode block, quota blocks, and
2832 * possibly selinux xattr blocks.
2833 */
2834 credits = 4 + EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
2835 EXT4_XATTR_TRANS_BLOCKS;
2836 } else {
2837 /*
2838 * Fast symlink. We have to add entry to directory
2839 * (EXT4_DATA_TRANS_BLOCKS + EXT4_INDEX_EXTRA_TRANS_BLOCKS),
2840 * allocate new inode (bitmap, group descriptor, inode block,
2841 * quota blocks, sb is already counted in previous macros).
2842 */
2843 credits = EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2844 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
2845 EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb);
2846 }
2847 retry:
2848 handle = ext4_journal_start(dir, credits);
2849 if (IS_ERR(handle))
2850 return PTR_ERR(handle);
2851
2852 if (IS_DIRSYNC(dir))
2853 ext4_handle_sync(handle);
2854
2855 inode = ext4_new_inode(handle, dir, S_IFLNK|S_IRWXUGO,
2856 &dentry->d_name, 0, NULL);
2857 err = PTR_ERR(inode);
2858 if (IS_ERR(inode))
2859 goto out_stop;
2860
2861 if (l > EXT4_N_BLOCKS * 4) {
2862 inode->i_op = &ext4_symlink_inode_operations;
2863 ext4_set_aops(inode);
2864 /*
2865 * We cannot call page_symlink() with transaction started
2866 * because it calls into ext4_write_begin() which can wait
2867 * for transaction commit if we are running out of space
2868 * and thus we deadlock. So we have to stop transaction now
2869 * and restart it when symlink contents is written.
2870 *
2871 * To keep fs consistent in case of crash, we have to put inode
2872 * to orphan list in the mean time.
2873 */
2874 drop_nlink(inode);
2875 err = ext4_orphan_add(handle, inode);
2876 ext4_journal_stop(handle);
2877 if (err)
2878 goto err_drop_inode;
2879 err = __page_symlink(inode, symname, l, 1);
2880 if (err)
2881 goto err_drop_inode;
2882 /*
2883 * Now inode is being linked into dir (EXT4_DATA_TRANS_BLOCKS
2884 * + EXT4_INDEX_EXTRA_TRANS_BLOCKS), inode is also modified
2885 */
2886 handle = ext4_journal_start(dir,
2887 EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2888 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 1);
2889 if (IS_ERR(handle)) {
2890 err = PTR_ERR(handle);
2891 goto err_drop_inode;
2892 }
2893 set_nlink(inode, 1);
2894 err = ext4_orphan_del(handle, inode);
2895 if (err) {
2896 ext4_journal_stop(handle);
2897 clear_nlink(inode);
2898 goto err_drop_inode;
2899 }
2900 } else {
2901 /* clear the extent format for fast symlink */
2902 ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
2903 inode->i_op = &ext4_fast_symlink_inode_operations;
2904 memcpy((char *)&EXT4_I(inode)->i_data, symname, l);
2905 inode->i_size = l-1;
2906 }
2907 EXT4_I(inode)->i_disksize = inode->i_size;
2908 err = ext4_add_nondir(handle, dentry, inode);
2909 out_stop:
2910 ext4_journal_stop(handle);
2911 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2912 goto retry;
2913 return err;
2914 err_drop_inode:
2915 unlock_new_inode(inode);
2916 iput(inode);
2917 return err;
2918 }
2919
2920 static int ext4_link(struct dentry *old_dentry,
2921 struct inode *dir, struct dentry *dentry)
2922 {
2923 handle_t *handle;
2924 struct inode *inode = old_dentry->d_inode;
2925 int err, retries = 0;
2926
2927 if (inode->i_nlink >= EXT4_LINK_MAX)
2928 return -EMLINK;
2929
2930 dquot_initialize(dir);
2931
2932 retry:
2933 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2934 EXT4_INDEX_EXTRA_TRANS_BLOCKS);
2935 if (IS_ERR(handle))
2936 return PTR_ERR(handle);
2937
2938 if (IS_DIRSYNC(dir))
2939 ext4_handle_sync(handle);
2940
2941 inode->i_ctime = ext4_current_time(inode);
2942 ext4_inc_count(handle, inode);
2943 ihold(inode);
2944
2945 err = ext4_add_entry(handle, dentry, inode);
2946 if (!err) {
2947 ext4_mark_inode_dirty(handle, inode);
2948 d_instantiate(dentry, inode);
2949 } else {
2950 drop_nlink(inode);
2951 iput(inode);
2952 }
2953 ext4_journal_stop(handle);
2954 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2955 goto retry;
2956 return err;
2957 }
2958
2959 #define PARENT_INO(buffer, size) \
2960 (ext4_next_entry((struct ext4_dir_entry_2 *)(buffer), size)->inode)
2961
2962 /*
2963 * Anybody can rename anything with this: the permission checks are left to the
2964 * higher-level routines.
2965 */
2966 static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
2967 struct inode *new_dir, struct dentry *new_dentry)
2968 {
2969 handle_t *handle;
2970 struct inode *old_inode, *new_inode;
2971 struct buffer_head *old_bh, *new_bh, *dir_bh;
2972 struct ext4_dir_entry_2 *old_de, *new_de;
2973 int retval, force_da_alloc = 0;
2974
2975 dquot_initialize(old_dir);
2976 dquot_initialize(new_dir);
2977
2978 old_bh = new_bh = dir_bh = NULL;
2979
2980 /* Initialize quotas before so that eventual writes go
2981 * in separate transaction */
2982 if (new_dentry->d_inode)
2983 dquot_initialize(new_dentry->d_inode);
2984 handle = ext4_journal_start(old_dir, 2 *
2985 EXT4_DATA_TRANS_BLOCKS(old_dir->i_sb) +
2986 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2);
2987 if (IS_ERR(handle))
2988 return PTR_ERR(handle);
2989
2990 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
2991 ext4_handle_sync(handle);
2992
2993 old_bh = ext4_find_entry(old_dir, &old_dentry->d_name, &old_de);
2994 /*
2995 * Check for inode number is _not_ due to possible IO errors.
2996 * We might rmdir the source, keep it as pwd of some process
2997 * and merrily kill the link to whatever was created under the
2998 * same name. Goodbye sticky bit ;-<
2999 */
3000 old_inode = old_dentry->d_inode;
3001 retval = -ENOENT;
3002 if (!old_bh || le32_to_cpu(old_de->inode) != old_inode->i_ino)
3003 goto end_rename;
3004
3005 new_inode = new_dentry->d_inode;
3006 new_bh = ext4_find_entry(new_dir, &new_dentry->d_name, &new_de);
3007 if (new_bh) {
3008 if (!new_inode) {
3009 brelse(new_bh);
3010 new_bh = NULL;
3011 }
3012 }
3013 if (S_ISDIR(old_inode->i_mode)) {
3014 if (new_inode) {
3015 retval = -ENOTEMPTY;
3016 if (!empty_dir(new_inode))
3017 goto end_rename;
3018 }
3019 retval = -EIO;
3020 if (!(dir_bh = ext4_bread(handle, old_inode, 0, 0, &retval))) {
3021 if (!retval) {
3022 retval = -EIO;
3023 ext4_error(old_inode->i_sb,
3024 "Directory hole detected on inode %lu\n",
3025 old_inode->i_ino);
3026 }
3027 goto end_rename;
3028 }
3029 if (!buffer_verified(dir_bh) &&
3030 !ext4_dirent_csum_verify(old_inode,
3031 (struct ext4_dir_entry *)dir_bh->b_data))
3032 goto end_rename;
3033 set_buffer_verified(dir_bh);
3034 if (le32_to_cpu(PARENT_INO(dir_bh->b_data,
3035 old_dir->i_sb->s_blocksize)) != old_dir->i_ino)
3036 goto end_rename;
3037 retval = -EMLINK;
3038 if (!new_inode && new_dir != old_dir &&
3039 EXT4_DIR_LINK_MAX(new_dir))
3040 goto end_rename;
3041 BUFFER_TRACE(dir_bh, "get_write_access");
3042 retval = ext4_journal_get_write_access(handle, dir_bh);
3043 if (retval)
3044 goto end_rename;
3045 }
3046 if (!new_bh) {
3047 retval = ext4_add_entry(handle, new_dentry, old_inode);
3048 if (retval)
3049 goto end_rename;
3050 } else {
3051 BUFFER_TRACE(new_bh, "get write access");
3052 retval = ext4_journal_get_write_access(handle, new_bh);
3053 if (retval)
3054 goto end_rename;
3055 new_de->inode = cpu_to_le32(old_inode->i_ino);
3056 if (EXT4_HAS_INCOMPAT_FEATURE(new_dir->i_sb,
3057 EXT4_FEATURE_INCOMPAT_FILETYPE))
3058 new_de->file_type = old_de->file_type;
3059 new_dir->i_version++;
3060 new_dir->i_ctime = new_dir->i_mtime =
3061 ext4_current_time(new_dir);
3062 ext4_mark_inode_dirty(handle, new_dir);
3063 BUFFER_TRACE(new_bh, "call ext4_handle_dirty_metadata");
3064 retval = ext4_handle_dirty_dirent_node(handle, new_dir, new_bh);
3065 if (unlikely(retval)) {
3066 ext4_std_error(new_dir->i_sb, retval);
3067 goto end_rename;
3068 }
3069 brelse(new_bh);
3070 new_bh = NULL;
3071 }
3072
3073 /*
3074 * Like most other Unix systems, set the ctime for inodes on a
3075 * rename.
3076 */
3077 old_inode->i_ctime = ext4_current_time(old_inode);
3078 ext4_mark_inode_dirty(handle, old_inode);
3079
3080 /*
3081 * ok, that's it
3082 */
3083 if (le32_to_cpu(old_de->inode) != old_inode->i_ino ||
3084 old_de->name_len != old_dentry->d_name.len ||
3085 strncmp(old_de->name, old_dentry->d_name.name, old_de->name_len) ||
3086 (retval = ext4_delete_entry(handle, old_dir,
3087 old_de, old_bh)) == -ENOENT) {
3088 /* old_de could have moved from under us during htree split, so
3089 * make sure that we are deleting the right entry. We might
3090 * also be pointing to a stale entry in the unused part of
3091 * old_bh so just checking inum and the name isn't enough. */
3092 struct buffer_head *old_bh2;
3093 struct ext4_dir_entry_2 *old_de2;
3094
3095 old_bh2 = ext4_find_entry(old_dir, &old_dentry->d_name, &old_de2);
3096 if (old_bh2) {
3097 retval = ext4_delete_entry(handle, old_dir,
3098 old_de2, old_bh2);
3099 brelse(old_bh2);
3100 }
3101 }
3102 if (retval) {
3103 ext4_warning(old_dir->i_sb,
3104 "Deleting old file (%lu), %d, error=%d",
3105 old_dir->i_ino, old_dir->i_nlink, retval);
3106 }
3107
3108 if (new_inode) {
3109 ext4_dec_count(handle, new_inode);
3110 new_inode->i_ctime = ext4_current_time(new_inode);
3111 }
3112 old_dir->i_ctime = old_dir->i_mtime = ext4_current_time(old_dir);
3113 ext4_update_dx_flag(old_dir);
3114 if (dir_bh) {
3115 PARENT_INO(dir_bh->b_data, new_dir->i_sb->s_blocksize) =
3116 cpu_to_le32(new_dir->i_ino);
3117 BUFFER_TRACE(dir_bh, "call ext4_handle_dirty_metadata");
3118 if (is_dx(old_inode)) {
3119 retval = ext4_handle_dirty_dx_node(handle,
3120 old_inode,
3121 dir_bh);
3122 } else {
3123 retval = ext4_handle_dirty_dirent_node(handle,
3124 old_inode,
3125 dir_bh);
3126 }
3127 if (retval) {
3128 ext4_std_error(old_dir->i_sb, retval);
3129 goto end_rename;
3130 }
3131 ext4_dec_count(handle, old_dir);
3132 if (new_inode) {
3133 /* checked empty_dir above, can't have another parent,
3134 * ext4_dec_count() won't work for many-linked dirs */
3135 clear_nlink(new_inode);
3136 } else {
3137 ext4_inc_count(handle, new_dir);
3138 ext4_update_dx_flag(new_dir);
3139 ext4_mark_inode_dirty(handle, new_dir);
3140 }
3141 }
3142 ext4_mark_inode_dirty(handle, old_dir);
3143 if (new_inode) {
3144 ext4_mark_inode_dirty(handle, new_inode);
3145 if (!new_inode->i_nlink)
3146 ext4_orphan_add(handle, new_inode);
3147 if (!test_opt(new_dir->i_sb, NO_AUTO_DA_ALLOC))
3148 force_da_alloc = 1;
3149 }
3150 retval = 0;
3151
3152 end_rename:
3153 brelse(dir_bh);
3154 brelse(old_bh);
3155 brelse(new_bh);
3156 ext4_journal_stop(handle);
3157 if (retval == 0 && force_da_alloc)
3158 ext4_alloc_da_blocks(old_inode);
3159 return retval;
3160 }
3161
3162 /*
3163 * directories can handle most operations...
3164 */
3165 const struct inode_operations ext4_dir_inode_operations = {
3166 .create = ext4_create,
3167 .lookup = ext4_lookup,
3168 .link = ext4_link,
3169 .unlink = ext4_unlink,
3170 .symlink = ext4_symlink,
3171 .mkdir = ext4_mkdir,
3172 .rmdir = ext4_rmdir,
3173 .mknod = ext4_mknod,
3174 .rename = ext4_rename,
3175 .setattr = ext4_setattr,
3176 #ifdef CONFIG_EXT4_FS_XATTR
3177 .setxattr = generic_setxattr,
3178 .getxattr = generic_getxattr,
3179 .listxattr = ext4_listxattr,
3180 .removexattr = generic_removexattr,
3181 #endif
3182 .get_acl = ext4_get_acl,
3183 .fiemap = ext4_fiemap,
3184 };
3185
3186 const struct inode_operations ext4_special_inode_operations = {
3187 .setattr = ext4_setattr,
3188 #ifdef CONFIG_EXT4_FS_XATTR
3189 .setxattr = generic_setxattr,
3190 .getxattr = generic_getxattr,
3191 .listxattr = ext4_listxattr,
3192 .removexattr = generic_removexattr,
3193 #endif
3194 .get_acl = ext4_get_acl,
3195 };