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