ia64/kvm: compilation fix. export account_system_vtime.
[pv_ops_mirror.git] / fs / ext4 / namei.c
blobab16beaa830d3814250146d044f8d2c4a59c7dd4
1 /*
2 * linux/fs/ext4/namei.c
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)
9 * from
11 * linux/fs/minix/namei.c
13 * Copyright (C) 1991, 1992 Linus Torvalds
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
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"
40 #include "namei.h"
41 #include "xattr.h"
42 #include "acl.h"
45 * define how far ahead to read directories while searching them.
47 #define NAMEI_RA_CHUNKS 2
48 #define NAMEI_RA_BLOCKS 4
49 #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
50 #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
52 static struct buffer_head *ext4_append(handle_t *handle,
53 struct inode *inode,
54 ext4_lblk_t *block, int *err)
56 struct buffer_head *bh;
58 *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
60 bh = ext4_bread(handle, inode, *block, 1, err);
61 if (bh) {
62 inode->i_size += inode->i_sb->s_blocksize;
63 EXT4_I(inode)->i_disksize = inode->i_size;
64 *err = ext4_journal_get_write_access(handle, bh);
65 if (*err) {
66 brelse(bh);
67 bh = NULL;
70 return bh;
73 #ifndef assert
74 #define assert(test) J_ASSERT(test)
75 #endif
77 #ifndef swap
78 #define swap(x, y) do { typeof(x) z = x; x = y; y = z; } while (0)
79 #endif
81 #ifdef DX_DEBUG
82 #define dxtrace(command) command
83 #else
84 #define dxtrace(command)
85 #endif
87 struct fake_dirent
89 __le32 inode;
90 __le16 rec_len;
91 u8 name_len;
92 u8 file_type;
95 struct dx_countlimit
97 __le16 limit;
98 __le16 count;
101 struct dx_entry
103 __le32 hash;
104 __le32 block;
108 * dx_root_info is laid out so that if it should somehow get overlaid by a
109 * dirent the two low bits of the hash version will be zero. Therefore, the
110 * hash version mod 4 should never be 0. Sincerely, the paranoia department.
113 struct dx_root
115 struct fake_dirent dot;
116 char dot_name[4];
117 struct fake_dirent dotdot;
118 char dotdot_name[4];
119 struct dx_root_info
121 __le32 reserved_zero;
122 u8 hash_version;
123 u8 info_length; /* 8 */
124 u8 indirect_levels;
125 u8 unused_flags;
127 info;
128 struct dx_entry entries[0];
131 struct dx_node
133 struct fake_dirent fake;
134 struct dx_entry entries[0];
138 struct dx_frame
140 struct buffer_head *bh;
141 struct dx_entry *entries;
142 struct dx_entry *at;
145 struct dx_map_entry
147 u32 hash;
148 u16 offs;
149 u16 size;
152 static inline ext4_lblk_t dx_get_block(struct dx_entry *entry);
153 static void dx_set_block(struct dx_entry *entry, ext4_lblk_t value);
154 static inline unsigned dx_get_hash (struct dx_entry *entry);
155 static void dx_set_hash (struct dx_entry *entry, unsigned value);
156 static unsigned dx_get_count (struct dx_entry *entries);
157 static unsigned dx_get_limit (struct dx_entry *entries);
158 static void dx_set_count (struct dx_entry *entries, unsigned value);
159 static void dx_set_limit (struct dx_entry *entries, unsigned value);
160 static unsigned dx_root_limit (struct inode *dir, unsigned infosize);
161 static unsigned dx_node_limit (struct inode *dir);
162 static struct dx_frame *dx_probe(struct dentry *dentry,
163 struct inode *dir,
164 struct dx_hash_info *hinfo,
165 struct dx_frame *frame,
166 int *err);
167 static void dx_release (struct dx_frame *frames);
168 static int dx_make_map (struct ext4_dir_entry_2 *de, int size,
169 struct dx_hash_info *hinfo, struct dx_map_entry map[]);
170 static void dx_sort_map(struct dx_map_entry *map, unsigned count);
171 static struct ext4_dir_entry_2 *dx_move_dirents (char *from, char *to,
172 struct dx_map_entry *offsets, int count);
173 static struct ext4_dir_entry_2* dx_pack_dirents (char *base, int size);
174 static void dx_insert_block(struct dx_frame *frame,
175 u32 hash, ext4_lblk_t block);
176 static int ext4_htree_next_block(struct inode *dir, __u32 hash,
177 struct dx_frame *frame,
178 struct dx_frame *frames,
179 __u32 *start_hash);
180 static struct buffer_head * ext4_dx_find_entry(struct dentry *dentry,
181 struct ext4_dir_entry_2 **res_dir, int *err);
182 static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
183 struct inode *inode);
186 * Future: use high four bits of block for coalesce-on-delete flags
187 * Mask them off for now.
190 static inline ext4_lblk_t dx_get_block(struct dx_entry *entry)
192 return le32_to_cpu(entry->block) & 0x00ffffff;
195 static inline void dx_set_block(struct dx_entry *entry, ext4_lblk_t value)
197 entry->block = cpu_to_le32(value);
200 static inline unsigned dx_get_hash (struct dx_entry *entry)
202 return le32_to_cpu(entry->hash);
205 static inline void dx_set_hash (struct dx_entry *entry, unsigned value)
207 entry->hash = cpu_to_le32(value);
210 static inline unsigned dx_get_count (struct dx_entry *entries)
212 return le16_to_cpu(((struct dx_countlimit *) entries)->count);
215 static inline unsigned dx_get_limit (struct dx_entry *entries)
217 return le16_to_cpu(((struct dx_countlimit *) entries)->limit);
220 static inline void dx_set_count (struct dx_entry *entries, unsigned value)
222 ((struct dx_countlimit *) entries)->count = cpu_to_le16(value);
225 static inline void dx_set_limit (struct dx_entry *entries, unsigned value)
227 ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
230 static inline unsigned dx_root_limit (struct inode *dir, unsigned infosize)
232 unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(1) -
233 EXT4_DIR_REC_LEN(2) - infosize;
234 return 0? 20: entry_space / sizeof(struct dx_entry);
237 static inline unsigned dx_node_limit (struct inode *dir)
239 unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(0);
240 return 0? 22: entry_space / sizeof(struct dx_entry);
244 * Debug
246 #ifdef DX_DEBUG
247 static void dx_show_index (char * label, struct dx_entry *entries)
249 int i, n = dx_get_count (entries);
250 printk("%s index ", label);
251 for (i = 0; i < n; i++) {
252 printk("%x->%lu ", i? dx_get_hash(entries + i) :
253 0, (unsigned long)dx_get_block(entries + i));
255 printk("\n");
258 struct stats
260 unsigned names;
261 unsigned space;
262 unsigned bcount;
265 static struct stats dx_show_leaf(struct dx_hash_info *hinfo, struct ext4_dir_entry_2 *de,
266 int size, int show_names)
268 unsigned names = 0, space = 0;
269 char *base = (char *) de;
270 struct dx_hash_info h = *hinfo;
272 printk("names: ");
273 while ((char *) de < base + size)
275 if (de->inode)
277 if (show_names)
279 int len = de->name_len;
280 char *name = de->name;
281 while (len--) printk("%c", *name++);
282 ext4fs_dirhash(de->name, de->name_len, &h);
283 printk(":%x.%u ", h.hash,
284 ((char *) de - base));
286 space += EXT4_DIR_REC_LEN(de->name_len);
287 names++;
289 de = ext4_next_entry(de);
291 printk("(%i)\n", names);
292 return (struct stats) { names, space, 1 };
295 struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
296 struct dx_entry *entries, int levels)
298 unsigned blocksize = dir->i_sb->s_blocksize;
299 unsigned count = dx_get_count (entries), names = 0, space = 0, i;
300 unsigned bcount = 0;
301 struct buffer_head *bh;
302 int err;
303 printk("%i indexed blocks...\n", count);
304 for (i = 0; i < count; i++, entries++)
306 ext4_lblk_t block = dx_get_block(entries);
307 ext4_lblk_t hash = i ? dx_get_hash(entries): 0;
308 u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
309 struct stats stats;
310 printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range);
311 if (!(bh = ext4_bread (NULL,dir, block, 0,&err))) continue;
312 stats = levels?
313 dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
314 dx_show_leaf(hinfo, (struct ext4_dir_entry_2 *) bh->b_data, blocksize, 0);
315 names += stats.names;
316 space += stats.space;
317 bcount += stats.bcount;
318 brelse (bh);
320 if (bcount)
321 printk("%snames %u, fullness %u (%u%%)\n", levels?"":" ",
322 names, space/bcount,(space/bcount)*100/blocksize);
323 return (struct stats) { names, space, bcount};
325 #endif /* DX_DEBUG */
328 * Probe for a directory leaf block to search.
330 * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
331 * error in the directory index, and the caller should fall back to
332 * searching the directory normally. The callers of dx_probe **MUST**
333 * check for this error code, and make sure it never gets reflected
334 * back to userspace.
336 static struct dx_frame *
337 dx_probe(struct dentry *dentry, struct inode *dir,
338 struct dx_hash_info *hinfo, struct dx_frame *frame_in, int *err)
340 unsigned count, indirect;
341 struct dx_entry *at, *entries, *p, *q, *m;
342 struct dx_root *root;
343 struct buffer_head *bh;
344 struct dx_frame *frame = frame_in;
345 u32 hash;
347 frame->bh = NULL;
348 if (dentry)
349 dir = dentry->d_parent->d_inode;
350 if (!(bh = ext4_bread (NULL,dir, 0, 0, err)))
351 goto fail;
352 root = (struct dx_root *) bh->b_data;
353 if (root->info.hash_version != DX_HASH_TEA &&
354 root->info.hash_version != DX_HASH_HALF_MD4 &&
355 root->info.hash_version != DX_HASH_LEGACY) {
356 ext4_warning(dir->i_sb, __func__,
357 "Unrecognised inode hash code %d",
358 root->info.hash_version);
359 brelse(bh);
360 *err = ERR_BAD_DX_DIR;
361 goto fail;
363 hinfo->hash_version = root->info.hash_version;
364 hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed;
365 if (dentry)
366 ext4fs_dirhash(dentry->d_name.name, dentry->d_name.len, hinfo);
367 hash = hinfo->hash;
369 if (root->info.unused_flags & 1) {
370 ext4_warning(dir->i_sb, __func__,
371 "Unimplemented inode hash flags: %#06x",
372 root->info.unused_flags);
373 brelse(bh);
374 *err = ERR_BAD_DX_DIR;
375 goto fail;
378 if ((indirect = root->info.indirect_levels) > 1) {
379 ext4_warning(dir->i_sb, __func__,
380 "Unimplemented inode hash depth: %#06x",
381 root->info.indirect_levels);
382 brelse(bh);
383 *err = ERR_BAD_DX_DIR;
384 goto fail;
387 entries = (struct dx_entry *) (((char *)&root->info) +
388 root->info.info_length);
390 if (dx_get_limit(entries) != dx_root_limit(dir,
391 root->info.info_length)) {
392 ext4_warning(dir->i_sb, __func__,
393 "dx entry: limit != root limit");
394 brelse(bh);
395 *err = ERR_BAD_DX_DIR;
396 goto fail;
399 dxtrace (printk("Look up %x", hash));
400 while (1)
402 count = dx_get_count(entries);
403 if (!count || count > dx_get_limit(entries)) {
404 ext4_warning(dir->i_sb, __func__,
405 "dx entry: no count or count > limit");
406 brelse(bh);
407 *err = ERR_BAD_DX_DIR;
408 goto fail2;
411 p = entries + 1;
412 q = entries + count - 1;
413 while (p <= q)
415 m = p + (q - p)/2;
416 dxtrace(printk("."));
417 if (dx_get_hash(m) > hash)
418 q = m - 1;
419 else
420 p = m + 1;
423 if (0) // linear search cross check
425 unsigned n = count - 1;
426 at = entries;
427 while (n--)
429 dxtrace(printk(","));
430 if (dx_get_hash(++at) > hash)
432 at--;
433 break;
436 assert (at == p - 1);
439 at = p - 1;
440 dxtrace(printk(" %x->%u\n", at == entries? 0: dx_get_hash(at), dx_get_block(at)));
441 frame->bh = bh;
442 frame->entries = entries;
443 frame->at = at;
444 if (!indirect--) return frame;
445 if (!(bh = ext4_bread (NULL,dir, dx_get_block(at), 0, err)))
446 goto fail2;
447 at = entries = ((struct dx_node *) bh->b_data)->entries;
448 if (dx_get_limit(entries) != dx_node_limit (dir)) {
449 ext4_warning(dir->i_sb, __func__,
450 "dx entry: limit != node limit");
451 brelse(bh);
452 *err = ERR_BAD_DX_DIR;
453 goto fail2;
455 frame++;
456 frame->bh = NULL;
458 fail2:
459 while (frame >= frame_in) {
460 brelse(frame->bh);
461 frame--;
463 fail:
464 if (*err == ERR_BAD_DX_DIR)
465 ext4_warning(dir->i_sb, __func__,
466 "Corrupt dir inode %ld, running e2fsck is "
467 "recommended.", dir->i_ino);
468 return NULL;
471 static void dx_release (struct dx_frame *frames)
473 if (frames[0].bh == NULL)
474 return;
476 if (((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels)
477 brelse(frames[1].bh);
478 brelse(frames[0].bh);
482 * This function increments the frame pointer to search the next leaf
483 * block, and reads in the necessary intervening nodes if the search
484 * should be necessary. Whether or not the search is necessary is
485 * controlled by the hash parameter. If the hash value is even, then
486 * the search is only continued if the next block starts with that
487 * hash value. This is used if we are searching for a specific file.
489 * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
491 * This function returns 1 if the caller should continue to search,
492 * or 0 if it should not. If there is an error reading one of the
493 * index blocks, it will a negative error code.
495 * If start_hash is non-null, it will be filled in with the starting
496 * hash of the next page.
498 static int ext4_htree_next_block(struct inode *dir, __u32 hash,
499 struct dx_frame *frame,
500 struct dx_frame *frames,
501 __u32 *start_hash)
503 struct dx_frame *p;
504 struct buffer_head *bh;
505 int err, num_frames = 0;
506 __u32 bhash;
508 p = frame;
510 * Find the next leaf page by incrementing the frame pointer.
511 * If we run out of entries in the interior node, loop around and
512 * increment pointer in the parent node. When we break out of
513 * this loop, num_frames indicates the number of interior
514 * nodes need to be read.
516 while (1) {
517 if (++(p->at) < p->entries + dx_get_count(p->entries))
518 break;
519 if (p == frames)
520 return 0;
521 num_frames++;
522 p--;
526 * If the hash is 1, then continue only if the next page has a
527 * continuation hash of any value. This is used for readdir
528 * handling. Otherwise, check to see if the hash matches the
529 * desired contiuation hash. If it doesn't, return since
530 * there's no point to read in the successive index pages.
532 bhash = dx_get_hash(p->at);
533 if (start_hash)
534 *start_hash = bhash;
535 if ((hash & 1) == 0) {
536 if ((bhash & ~1) != hash)
537 return 0;
540 * If the hash is HASH_NB_ALWAYS, we always go to the next
541 * block so no check is necessary
543 while (num_frames--) {
544 if (!(bh = ext4_bread(NULL, dir, dx_get_block(p->at),
545 0, &err)))
546 return err; /* Failure */
547 p++;
548 brelse (p->bh);
549 p->bh = bh;
550 p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
552 return 1;
557 * p is at least 6 bytes before the end of page
559 static inline struct ext4_dir_entry_2 *ext4_next_entry(struct ext4_dir_entry_2 *p)
561 return (struct ext4_dir_entry_2 *)((char *)p +
562 ext4_rec_len_from_disk(p->rec_len));
566 * This function fills a red-black tree with information from a
567 * directory block. It returns the number directory entries loaded
568 * into the tree. If there is an error it is returned in err.
570 static int htree_dirblock_to_tree(struct file *dir_file,
571 struct inode *dir, ext4_lblk_t block,
572 struct dx_hash_info *hinfo,
573 __u32 start_hash, __u32 start_minor_hash)
575 struct buffer_head *bh;
576 struct ext4_dir_entry_2 *de, *top;
577 int err, count = 0;
579 dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n",
580 (unsigned long)block));
581 if (!(bh = ext4_bread (NULL, dir, block, 0, &err)))
582 return err;
584 de = (struct ext4_dir_entry_2 *) bh->b_data;
585 top = (struct ext4_dir_entry_2 *) ((char *) de +
586 dir->i_sb->s_blocksize -
587 EXT4_DIR_REC_LEN(0));
588 for (; de < top; de = ext4_next_entry(de)) {
589 if (!ext4_check_dir_entry("htree_dirblock_to_tree", dir, de, bh,
590 (block<<EXT4_BLOCK_SIZE_BITS(dir->i_sb))
591 +((char *)de - bh->b_data))) {
592 /* On error, skip the f_pos to the next block. */
593 dir_file->f_pos = (dir_file->f_pos |
594 (dir->i_sb->s_blocksize - 1)) + 1;
595 brelse (bh);
596 return count;
598 ext4fs_dirhash(de->name, de->name_len, hinfo);
599 if ((hinfo->hash < start_hash) ||
600 ((hinfo->hash == start_hash) &&
601 (hinfo->minor_hash < start_minor_hash)))
602 continue;
603 if (de->inode == 0)
604 continue;
605 if ((err = ext4_htree_store_dirent(dir_file,
606 hinfo->hash, hinfo->minor_hash, de)) != 0) {
607 brelse(bh);
608 return err;
610 count++;
612 brelse(bh);
613 return count;
618 * This function fills a red-black tree with information from a
619 * directory. We start scanning the directory in hash order, starting
620 * at start_hash and start_minor_hash.
622 * This function returns the number of entries inserted into the tree,
623 * or a negative error code.
625 int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
626 __u32 start_minor_hash, __u32 *next_hash)
628 struct dx_hash_info hinfo;
629 struct ext4_dir_entry_2 *de;
630 struct dx_frame frames[2], *frame;
631 struct inode *dir;
632 ext4_lblk_t block;
633 int count = 0;
634 int ret, err;
635 __u32 hashval;
637 dxtrace(printk("In htree_fill_tree, start hash: %x:%x\n", start_hash,
638 start_minor_hash));
639 dir = dir_file->f_path.dentry->d_inode;
640 if (!(EXT4_I(dir)->i_flags & EXT4_INDEX_FL)) {
641 hinfo.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
642 hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
643 count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
644 start_hash, start_minor_hash);
645 *next_hash = ~0;
646 return count;
648 hinfo.hash = start_hash;
649 hinfo.minor_hash = 0;
650 frame = dx_probe(NULL, dir_file->f_path.dentry->d_inode, &hinfo, frames, &err);
651 if (!frame)
652 return err;
654 /* Add '.' and '..' from the htree header */
655 if (!start_hash && !start_minor_hash) {
656 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
657 if ((err = ext4_htree_store_dirent(dir_file, 0, 0, de)) != 0)
658 goto errout;
659 count++;
661 if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
662 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
663 de = ext4_next_entry(de);
664 if ((err = ext4_htree_store_dirent(dir_file, 2, 0, de)) != 0)
665 goto errout;
666 count++;
669 while (1) {
670 block = dx_get_block(frame->at);
671 ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
672 start_hash, start_minor_hash);
673 if (ret < 0) {
674 err = ret;
675 goto errout;
677 count += ret;
678 hashval = ~0;
679 ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
680 frame, frames, &hashval);
681 *next_hash = hashval;
682 if (ret < 0) {
683 err = ret;
684 goto errout;
687 * Stop if: (a) there are no more entries, or
688 * (b) we have inserted at least one entry and the
689 * next hash value is not a continuation
691 if ((ret == 0) ||
692 (count && ((hashval & 1) == 0)))
693 break;
695 dx_release(frames);
696 dxtrace(printk("Fill tree: returned %d entries, next hash: %x\n",
697 count, *next_hash));
698 return count;
699 errout:
700 dx_release(frames);
701 return (err);
706 * Directory block splitting, compacting
710 * Create map of hash values, offsets, and sizes, stored at end of block.
711 * Returns number of entries mapped.
713 static int dx_make_map (struct ext4_dir_entry_2 *de, int size,
714 struct dx_hash_info *hinfo, struct dx_map_entry *map_tail)
716 int count = 0;
717 char *base = (char *) de;
718 struct dx_hash_info h = *hinfo;
720 while ((char *) de < base + size)
722 if (de->name_len && de->inode) {
723 ext4fs_dirhash(de->name, de->name_len, &h);
724 map_tail--;
725 map_tail->hash = h.hash;
726 map_tail->offs = (u16) ((char *) de - base);
727 map_tail->size = le16_to_cpu(de->rec_len);
728 count++;
729 cond_resched();
731 /* XXX: do we need to check rec_len == 0 case? -Chris */
732 de = ext4_next_entry(de);
734 return count;
737 /* Sort map by hash value */
738 static void dx_sort_map (struct dx_map_entry *map, unsigned count)
740 struct dx_map_entry *p, *q, *top = map + count - 1;
741 int more;
742 /* Combsort until bubble sort doesn't suck */
743 while (count > 2) {
744 count = count*10/13;
745 if (count - 9 < 2) /* 9, 10 -> 11 */
746 count = 11;
747 for (p = top, q = p - count; q >= map; p--, q--)
748 if (p->hash < q->hash)
749 swap(*p, *q);
751 /* Garden variety bubble sort */
752 do {
753 more = 0;
754 q = top;
755 while (q-- > map) {
756 if (q[1].hash >= q[0].hash)
757 continue;
758 swap(*(q+1), *q);
759 more = 1;
761 } while(more);
764 static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
766 struct dx_entry *entries = frame->entries;
767 struct dx_entry *old = frame->at, *new = old + 1;
768 int count = dx_get_count(entries);
770 assert(count < dx_get_limit(entries));
771 assert(old < entries + count);
772 memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
773 dx_set_hash(new, hash);
774 dx_set_block(new, block);
775 dx_set_count(entries, count + 1);
778 static void ext4_update_dx_flag(struct inode *inode)
780 if (!EXT4_HAS_COMPAT_FEATURE(inode->i_sb,
781 EXT4_FEATURE_COMPAT_DIR_INDEX))
782 EXT4_I(inode)->i_flags &= ~EXT4_INDEX_FL;
786 * NOTE! unlike strncmp, ext4_match returns 1 for success, 0 for failure.
788 * `len <= EXT4_NAME_LEN' is guaranteed by caller.
789 * `de != NULL' is guaranteed by caller.
791 static inline int ext4_match (int len, const char * const name,
792 struct ext4_dir_entry_2 * de)
794 if (len != de->name_len)
795 return 0;
796 if (!de->inode)
797 return 0;
798 return !memcmp(name, de->name, len);
802 * Returns 0 if not found, -1 on failure, and 1 on success
804 static inline int search_dirblock(struct buffer_head * bh,
805 struct inode *dir,
806 struct dentry *dentry,
807 unsigned long offset,
808 struct ext4_dir_entry_2 ** res_dir)
810 struct ext4_dir_entry_2 * de;
811 char * dlimit;
812 int de_len;
813 const char *name = dentry->d_name.name;
814 int namelen = dentry->d_name.len;
816 de = (struct ext4_dir_entry_2 *) bh->b_data;
817 dlimit = bh->b_data + dir->i_sb->s_blocksize;
818 while ((char *) de < dlimit) {
819 /* this code is executed quadratically often */
820 /* do minimal checking `by hand' */
822 if ((char *) de + namelen <= dlimit &&
823 ext4_match (namelen, name, de)) {
824 /* found a match - just to be sure, do a full check */
825 if (!ext4_check_dir_entry("ext4_find_entry",
826 dir, de, bh, offset))
827 return -1;
828 *res_dir = de;
829 return 1;
831 /* prevent looping on a bad block */
832 de_len = ext4_rec_len_from_disk(de->rec_len);
833 if (de_len <= 0)
834 return -1;
835 offset += de_len;
836 de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
838 return 0;
843 * ext4_find_entry()
845 * finds an entry in the specified directory with the wanted name. It
846 * returns the cache buffer in which the entry was found, and the entry
847 * itself (as a parameter - res_dir). It does NOT read the inode of the
848 * entry - you'll have to do that yourself if you want to.
850 * The returned buffer_head has ->b_count elevated. The caller is expected
851 * to brelse() it when appropriate.
853 static struct buffer_head * ext4_find_entry (struct dentry *dentry,
854 struct ext4_dir_entry_2 ** res_dir)
856 struct super_block * sb;
857 struct buffer_head * bh_use[NAMEI_RA_SIZE];
858 struct buffer_head * bh, *ret = NULL;
859 ext4_lblk_t start, block, b;
860 int ra_max = 0; /* Number of bh's in the readahead
861 buffer, bh_use[] */
862 int ra_ptr = 0; /* Current index into readahead
863 buffer */
864 int num = 0;
865 ext4_lblk_t nblocks;
866 int i, err;
867 struct inode *dir = dentry->d_parent->d_inode;
868 int namelen;
870 *res_dir = NULL;
871 sb = dir->i_sb;
872 namelen = dentry->d_name.len;
873 if (namelen > EXT4_NAME_LEN)
874 return NULL;
875 if (is_dx(dir)) {
876 bh = ext4_dx_find_entry(dentry, res_dir, &err);
878 * On success, or if the error was file not found,
879 * return. Otherwise, fall back to doing a search the
880 * old fashioned way.
882 if (bh || (err != ERR_BAD_DX_DIR))
883 return bh;
884 dxtrace(printk("ext4_find_entry: dx failed, falling back\n"));
886 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
887 start = EXT4_I(dir)->i_dir_start_lookup;
888 if (start >= nblocks)
889 start = 0;
890 block = start;
891 restart:
892 do {
894 * We deal with the read-ahead logic here.
896 if (ra_ptr >= ra_max) {
897 /* Refill the readahead buffer */
898 ra_ptr = 0;
899 b = block;
900 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
902 * Terminate if we reach the end of the
903 * directory and must wrap, or if our
904 * search has finished at this block.
906 if (b >= nblocks || (num && block == start)) {
907 bh_use[ra_max] = NULL;
908 break;
910 num++;
911 bh = ext4_getblk(NULL, dir, b++, 0, &err);
912 bh_use[ra_max] = bh;
913 if (bh)
914 ll_rw_block(READ_META, 1, &bh);
917 if ((bh = bh_use[ra_ptr++]) == NULL)
918 goto next;
919 wait_on_buffer(bh);
920 if (!buffer_uptodate(bh)) {
921 /* read error, skip block & hope for the best */
922 ext4_error(sb, __func__, "reading directory #%lu "
923 "offset %lu", dir->i_ino,
924 (unsigned long)block);
925 brelse(bh);
926 goto next;
928 i = search_dirblock(bh, dir, dentry,
929 block << EXT4_BLOCK_SIZE_BITS(sb), res_dir);
930 if (i == 1) {
931 EXT4_I(dir)->i_dir_start_lookup = block;
932 ret = bh;
933 goto cleanup_and_exit;
934 } else {
935 brelse(bh);
936 if (i < 0)
937 goto cleanup_and_exit;
939 next:
940 if (++block >= nblocks)
941 block = 0;
942 } while (block != start);
945 * If the directory has grown while we were searching, then
946 * search the last part of the directory before giving up.
948 block = nblocks;
949 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
950 if (block < nblocks) {
951 start = 0;
952 goto restart;
955 cleanup_and_exit:
956 /* Clean up the read-ahead blocks */
957 for (; ra_ptr < ra_max; ra_ptr++)
958 brelse (bh_use[ra_ptr]);
959 return ret;
962 static struct buffer_head * ext4_dx_find_entry(struct dentry *dentry,
963 struct ext4_dir_entry_2 **res_dir, int *err)
965 struct super_block * sb;
966 struct dx_hash_info hinfo;
967 u32 hash;
968 struct dx_frame frames[2], *frame;
969 struct ext4_dir_entry_2 *de, *top;
970 struct buffer_head *bh;
971 ext4_lblk_t block;
972 int retval;
973 int namelen = dentry->d_name.len;
974 const u8 *name = dentry->d_name.name;
975 struct inode *dir = dentry->d_parent->d_inode;
977 sb = dir->i_sb;
978 /* NFS may look up ".." - look at dx_root directory block */
979 if (namelen > 2 || name[0] != '.'||(name[1] != '.' && name[1] != '\0')){
980 if (!(frame = dx_probe(dentry, NULL, &hinfo, frames, err)))
981 return NULL;
982 } else {
983 frame = frames;
984 frame->bh = NULL; /* for dx_release() */
985 frame->at = (struct dx_entry *)frames; /* hack for zero entry*/
986 dx_set_block(frame->at, 0); /* dx_root block is 0 */
988 hash = hinfo.hash;
989 do {
990 block = dx_get_block(frame->at);
991 if (!(bh = ext4_bread (NULL,dir, block, 0, err)))
992 goto errout;
993 de = (struct ext4_dir_entry_2 *) bh->b_data;
994 top = (struct ext4_dir_entry_2 *) ((char *) de + sb->s_blocksize -
995 EXT4_DIR_REC_LEN(0));
996 for (; de < top; de = ext4_next_entry(de))
997 if (ext4_match (namelen, name, de)) {
998 if (!ext4_check_dir_entry("ext4_find_entry",
999 dir, de, bh,
1000 (block<<EXT4_BLOCK_SIZE_BITS(sb))
1001 +((char *)de - bh->b_data))) {
1002 brelse (bh);
1003 *err = ERR_BAD_DX_DIR;
1004 goto errout;
1006 *res_dir = de;
1007 dx_release (frames);
1008 return bh;
1010 brelse (bh);
1011 /* Check to see if we should continue to search */
1012 retval = ext4_htree_next_block(dir, hash, frame,
1013 frames, NULL);
1014 if (retval < 0) {
1015 ext4_warning(sb, __func__,
1016 "error reading index page in directory #%lu",
1017 dir->i_ino);
1018 *err = retval;
1019 goto errout;
1021 } while (retval == 1);
1023 *err = -ENOENT;
1024 errout:
1025 dxtrace(printk("%s not found\n", name));
1026 dx_release (frames);
1027 return NULL;
1030 static struct dentry *ext4_lookup(struct inode * dir, struct dentry *dentry, struct nameidata *nd)
1032 struct inode * inode;
1033 struct ext4_dir_entry_2 * de;
1034 struct buffer_head * bh;
1036 if (dentry->d_name.len > EXT4_NAME_LEN)
1037 return ERR_PTR(-ENAMETOOLONG);
1039 bh = ext4_find_entry(dentry, &de);
1040 inode = NULL;
1041 if (bh) {
1042 unsigned long ino = le32_to_cpu(de->inode);
1043 brelse (bh);
1044 if (!ext4_valid_inum(dir->i_sb, ino)) {
1045 ext4_error(dir->i_sb, "ext4_lookup",
1046 "bad inode number: %lu", ino);
1047 return ERR_PTR(-EIO);
1049 inode = ext4_iget(dir->i_sb, ino);
1050 if (IS_ERR(inode))
1051 return ERR_CAST(inode);
1053 return d_splice_alias(inode, dentry);
1057 struct dentry *ext4_get_parent(struct dentry *child)
1059 unsigned long ino;
1060 struct dentry *parent;
1061 struct inode *inode;
1062 struct dentry dotdot;
1063 struct ext4_dir_entry_2 * de;
1064 struct buffer_head *bh;
1066 dotdot.d_name.name = "..";
1067 dotdot.d_name.len = 2;
1068 dotdot.d_parent = child; /* confusing, isn't it! */
1070 bh = ext4_find_entry(&dotdot, &de);
1071 inode = NULL;
1072 if (!bh)
1073 return ERR_PTR(-ENOENT);
1074 ino = le32_to_cpu(de->inode);
1075 brelse(bh);
1077 if (!ext4_valid_inum(child->d_inode->i_sb, ino)) {
1078 ext4_error(child->d_inode->i_sb, "ext4_get_parent",
1079 "bad inode number: %lu", ino);
1080 return ERR_PTR(-EIO);
1083 inode = ext4_iget(child->d_inode->i_sb, ino);
1084 if (IS_ERR(inode))
1085 return ERR_CAST(inode);
1087 parent = d_alloc_anon(inode);
1088 if (!parent) {
1089 iput(inode);
1090 parent = ERR_PTR(-ENOMEM);
1092 return parent;
1095 #define S_SHIFT 12
1096 static unsigned char ext4_type_by_mode[S_IFMT >> S_SHIFT] = {
1097 [S_IFREG >> S_SHIFT] = EXT4_FT_REG_FILE,
1098 [S_IFDIR >> S_SHIFT] = EXT4_FT_DIR,
1099 [S_IFCHR >> S_SHIFT] = EXT4_FT_CHRDEV,
1100 [S_IFBLK >> S_SHIFT] = EXT4_FT_BLKDEV,
1101 [S_IFIFO >> S_SHIFT] = EXT4_FT_FIFO,
1102 [S_IFSOCK >> S_SHIFT] = EXT4_FT_SOCK,
1103 [S_IFLNK >> S_SHIFT] = EXT4_FT_SYMLINK,
1106 static inline void ext4_set_de_type(struct super_block *sb,
1107 struct ext4_dir_entry_2 *de,
1108 umode_t mode) {
1109 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FILETYPE))
1110 de->file_type = ext4_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
1114 * Move count entries from end of map between two memory locations.
1115 * Returns pointer to last entry moved.
1117 static struct ext4_dir_entry_2 *
1118 dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count)
1120 unsigned rec_len = 0;
1122 while (count--) {
1123 struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *) (from + map->offs);
1124 rec_len = EXT4_DIR_REC_LEN(de->name_len);
1125 memcpy (to, de, rec_len);
1126 ((struct ext4_dir_entry_2 *) to)->rec_len =
1127 ext4_rec_len_to_disk(rec_len);
1128 de->inode = 0;
1129 map++;
1130 to += rec_len;
1132 return (struct ext4_dir_entry_2 *) (to - rec_len);
1136 * Compact each dir entry in the range to the minimal rec_len.
1137 * Returns pointer to last entry in range.
1139 static struct ext4_dir_entry_2* dx_pack_dirents(char *base, int size)
1141 struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base;
1142 unsigned rec_len = 0;
1144 prev = to = de;
1145 while ((char*)de < base + size) {
1146 next = ext4_next_entry(de);
1147 if (de->inode && de->name_len) {
1148 rec_len = EXT4_DIR_REC_LEN(de->name_len);
1149 if (de > to)
1150 memmove(to, de, rec_len);
1151 to->rec_len = ext4_rec_len_to_disk(rec_len);
1152 prev = to;
1153 to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len);
1155 de = next;
1157 return prev;
1161 * Split a full leaf block to make room for a new dir entry.
1162 * Allocate a new block, and move entries so that they are approx. equally full.
1163 * Returns pointer to de in block into which the new entry will be inserted.
1165 static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
1166 struct buffer_head **bh,struct dx_frame *frame,
1167 struct dx_hash_info *hinfo, int *error)
1169 unsigned blocksize = dir->i_sb->s_blocksize;
1170 unsigned count, continued;
1171 struct buffer_head *bh2;
1172 ext4_lblk_t newblock;
1173 u32 hash2;
1174 struct dx_map_entry *map;
1175 char *data1 = (*bh)->b_data, *data2;
1176 unsigned split, move, size, i;
1177 struct ext4_dir_entry_2 *de = NULL, *de2;
1178 int err = 0;
1180 bh2 = ext4_append (handle, dir, &newblock, &err);
1181 if (!(bh2)) {
1182 brelse(*bh);
1183 *bh = NULL;
1184 goto errout;
1187 BUFFER_TRACE(*bh, "get_write_access");
1188 err = ext4_journal_get_write_access(handle, *bh);
1189 if (err)
1190 goto journal_error;
1192 BUFFER_TRACE(frame->bh, "get_write_access");
1193 err = ext4_journal_get_write_access(handle, frame->bh);
1194 if (err)
1195 goto journal_error;
1197 data2 = bh2->b_data;
1199 /* create map in the end of data2 block */
1200 map = (struct dx_map_entry *) (data2 + blocksize);
1201 count = dx_make_map ((struct ext4_dir_entry_2 *) data1,
1202 blocksize, hinfo, map);
1203 map -= count;
1204 dx_sort_map (map, count);
1205 /* Split the existing block in the middle, size-wise */
1206 size = 0;
1207 move = 0;
1208 for (i = count-1; i >= 0; i--) {
1209 /* is more than half of this entry in 2nd half of the block? */
1210 if (size + map[i].size/2 > blocksize/2)
1211 break;
1212 size += map[i].size;
1213 move++;
1215 /* map index at which we will split */
1216 split = count - move;
1217 hash2 = map[split].hash;
1218 continued = hash2 == map[split - 1].hash;
1219 dxtrace(printk(KERN_INFO "Split block %lu at %x, %i/%i\n",
1220 (unsigned long)dx_get_block(frame->at),
1221 hash2, split, count-split));
1223 /* Fancy dance to stay within two buffers */
1224 de2 = dx_move_dirents(data1, data2, map + split, count - split);
1225 de = dx_pack_dirents(data1,blocksize);
1226 de->rec_len = ext4_rec_len_to_disk(data1 + blocksize - (char *) de);
1227 de2->rec_len = ext4_rec_len_to_disk(data2 + blocksize - (char *) de2);
1228 dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data1, blocksize, 1));
1229 dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data2, blocksize, 1));
1231 /* Which block gets the new entry? */
1232 if (hinfo->hash >= hash2)
1234 swap(*bh, bh2);
1235 de = de2;
1237 dx_insert_block (frame, hash2 + continued, newblock);
1238 err = ext4_journal_dirty_metadata (handle, bh2);
1239 if (err)
1240 goto journal_error;
1241 err = ext4_journal_dirty_metadata (handle, frame->bh);
1242 if (err)
1243 goto journal_error;
1244 brelse (bh2);
1245 dxtrace(dx_show_index ("frame", frame->entries));
1246 return de;
1248 journal_error:
1249 brelse(*bh);
1250 brelse(bh2);
1251 *bh = NULL;
1252 ext4_std_error(dir->i_sb, err);
1253 errout:
1254 *error = err;
1255 return NULL;
1259 * Add a new entry into a directory (leaf) block. If de is non-NULL,
1260 * it points to a directory entry which is guaranteed to be large
1261 * enough for new directory entry. If de is NULL, then
1262 * add_dirent_to_buf will attempt search the directory block for
1263 * space. It will return -ENOSPC if no space is available, and -EIO
1264 * and -EEXIST if directory entry already exists.
1266 * NOTE! bh is NOT released in the case where ENOSPC is returned. In
1267 * all other cases bh is released.
1269 static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry,
1270 struct inode *inode, struct ext4_dir_entry_2 *de,
1271 struct buffer_head * bh)
1273 struct inode *dir = dentry->d_parent->d_inode;
1274 const char *name = dentry->d_name.name;
1275 int namelen = dentry->d_name.len;
1276 unsigned long offset = 0;
1277 unsigned short reclen;
1278 int nlen, rlen, err;
1279 char *top;
1281 reclen = EXT4_DIR_REC_LEN(namelen);
1282 if (!de) {
1283 de = (struct ext4_dir_entry_2 *)bh->b_data;
1284 top = bh->b_data + dir->i_sb->s_blocksize - reclen;
1285 while ((char *) de <= top) {
1286 if (!ext4_check_dir_entry("ext4_add_entry", dir, de,
1287 bh, offset)) {
1288 brelse (bh);
1289 return -EIO;
1291 if (ext4_match (namelen, name, de)) {
1292 brelse (bh);
1293 return -EEXIST;
1295 nlen = EXT4_DIR_REC_LEN(de->name_len);
1296 rlen = ext4_rec_len_from_disk(de->rec_len);
1297 if ((de->inode? rlen - nlen: rlen) >= reclen)
1298 break;
1299 de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
1300 offset += rlen;
1302 if ((char *) de > top)
1303 return -ENOSPC;
1305 BUFFER_TRACE(bh, "get_write_access");
1306 err = ext4_journal_get_write_access(handle, bh);
1307 if (err) {
1308 ext4_std_error(dir->i_sb, err);
1309 brelse(bh);
1310 return err;
1313 /* By now the buffer is marked for journaling */
1314 nlen = EXT4_DIR_REC_LEN(de->name_len);
1315 rlen = ext4_rec_len_from_disk(de->rec_len);
1316 if (de->inode) {
1317 struct ext4_dir_entry_2 *de1 = (struct ext4_dir_entry_2 *)((char *)de + nlen);
1318 de1->rec_len = ext4_rec_len_to_disk(rlen - nlen);
1319 de->rec_len = ext4_rec_len_to_disk(nlen);
1320 de = de1;
1322 de->file_type = EXT4_FT_UNKNOWN;
1323 if (inode) {
1324 de->inode = cpu_to_le32(inode->i_ino);
1325 ext4_set_de_type(dir->i_sb, de, inode->i_mode);
1326 } else
1327 de->inode = 0;
1328 de->name_len = namelen;
1329 memcpy (de->name, name, namelen);
1331 * XXX shouldn't update any times until successful
1332 * completion of syscall, but too many callers depend
1333 * on this.
1335 * XXX similarly, too many callers depend on
1336 * ext4_new_inode() setting the times, but error
1337 * recovery deletes the inode, so the worst that can
1338 * happen is that the times are slightly out of date
1339 * and/or different from the directory change time.
1341 dir->i_mtime = dir->i_ctime = ext4_current_time(dir);
1342 ext4_update_dx_flag(dir);
1343 dir->i_version++;
1344 ext4_mark_inode_dirty(handle, dir);
1345 BUFFER_TRACE(bh, "call ext4_journal_dirty_metadata");
1346 err = ext4_journal_dirty_metadata(handle, bh);
1347 if (err)
1348 ext4_std_error(dir->i_sb, err);
1349 brelse(bh);
1350 return 0;
1354 * This converts a one block unindexed directory to a 3 block indexed
1355 * directory, and adds the dentry to the indexed directory.
1357 static int make_indexed_dir(handle_t *handle, struct dentry *dentry,
1358 struct inode *inode, struct buffer_head *bh)
1360 struct inode *dir = dentry->d_parent->d_inode;
1361 const char *name = dentry->d_name.name;
1362 int namelen = dentry->d_name.len;
1363 struct buffer_head *bh2;
1364 struct dx_root *root;
1365 struct dx_frame frames[2], *frame;
1366 struct dx_entry *entries;
1367 struct ext4_dir_entry_2 *de, *de2;
1368 char *data1, *top;
1369 unsigned len;
1370 int retval;
1371 unsigned blocksize;
1372 struct dx_hash_info hinfo;
1373 ext4_lblk_t block;
1374 struct fake_dirent *fde;
1376 blocksize = dir->i_sb->s_blocksize;
1377 dxtrace(printk("Creating index\n"));
1378 retval = ext4_journal_get_write_access(handle, bh);
1379 if (retval) {
1380 ext4_std_error(dir->i_sb, retval);
1381 brelse(bh);
1382 return retval;
1384 root = (struct dx_root *) bh->b_data;
1386 bh2 = ext4_append (handle, dir, &block, &retval);
1387 if (!(bh2)) {
1388 brelse(bh);
1389 return retval;
1391 EXT4_I(dir)->i_flags |= EXT4_INDEX_FL;
1392 data1 = bh2->b_data;
1394 /* The 0th block becomes the root, move the dirents out */
1395 fde = &root->dotdot;
1396 de = (struct ext4_dir_entry_2 *)((char *)fde +
1397 ext4_rec_len_from_disk(fde->rec_len));
1398 len = ((char *) root) + blocksize - (char *) de;
1399 memcpy (data1, de, len);
1400 de = (struct ext4_dir_entry_2 *) data1;
1401 top = data1 + len;
1402 while ((char *)(de2 = ext4_next_entry(de)) < top)
1403 de = de2;
1404 de->rec_len = ext4_rec_len_to_disk(data1 + blocksize - (char *) de);
1405 /* Initialize the root; the dot dirents already exist */
1406 de = (struct ext4_dir_entry_2 *) (&root->dotdot);
1407 de->rec_len = ext4_rec_len_to_disk(blocksize - EXT4_DIR_REC_LEN(2));
1408 memset (&root->info, 0, sizeof(root->info));
1409 root->info.info_length = sizeof(root->info);
1410 root->info.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
1411 entries = root->entries;
1412 dx_set_block (entries, 1);
1413 dx_set_count (entries, 1);
1414 dx_set_limit (entries, dx_root_limit(dir, sizeof(root->info)));
1416 /* Initialize as for dx_probe */
1417 hinfo.hash_version = root->info.hash_version;
1418 hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
1419 ext4fs_dirhash(name, namelen, &hinfo);
1420 frame = frames;
1421 frame->entries = entries;
1422 frame->at = entries;
1423 frame->bh = bh;
1424 bh = bh2;
1425 de = do_split(handle,dir, &bh, frame, &hinfo, &retval);
1426 dx_release (frames);
1427 if (!(de))
1428 return retval;
1430 return add_dirent_to_buf(handle, dentry, inode, de, bh);
1434 * ext4_add_entry()
1436 * adds a file entry to the specified directory, using the same
1437 * semantics as ext4_find_entry(). It returns NULL if it failed.
1439 * NOTE!! The inode part of 'de' is left at 0 - which means you
1440 * may not sleep between calling this and putting something into
1441 * the entry, as someone else might have used it while you slept.
1443 static int ext4_add_entry (handle_t *handle, struct dentry *dentry,
1444 struct inode *inode)
1446 struct inode *dir = dentry->d_parent->d_inode;
1447 unsigned long offset;
1448 struct buffer_head * bh;
1449 struct ext4_dir_entry_2 *de;
1450 struct super_block * sb;
1451 int retval;
1452 int dx_fallback=0;
1453 unsigned blocksize;
1454 ext4_lblk_t block, blocks;
1456 sb = dir->i_sb;
1457 blocksize = sb->s_blocksize;
1458 if (!dentry->d_name.len)
1459 return -EINVAL;
1460 if (is_dx(dir)) {
1461 retval = ext4_dx_add_entry(handle, dentry, inode);
1462 if (!retval || (retval != ERR_BAD_DX_DIR))
1463 return retval;
1464 EXT4_I(dir)->i_flags &= ~EXT4_INDEX_FL;
1465 dx_fallback++;
1466 ext4_mark_inode_dirty(handle, dir);
1468 blocks = dir->i_size >> sb->s_blocksize_bits;
1469 for (block = 0, offset = 0; block < blocks; block++) {
1470 bh = ext4_bread(handle, dir, block, 0, &retval);
1471 if(!bh)
1472 return retval;
1473 retval = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
1474 if (retval != -ENOSPC)
1475 return retval;
1477 if (blocks == 1 && !dx_fallback &&
1478 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_DIR_INDEX))
1479 return make_indexed_dir(handle, dentry, inode, bh);
1480 brelse(bh);
1482 bh = ext4_append(handle, dir, &block, &retval);
1483 if (!bh)
1484 return retval;
1485 de = (struct ext4_dir_entry_2 *) bh->b_data;
1486 de->inode = 0;
1487 de->rec_len = ext4_rec_len_to_disk(blocksize);
1488 return add_dirent_to_buf(handle, dentry, inode, de, bh);
1492 * Returns 0 for success, or a negative error value
1494 static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
1495 struct inode *inode)
1497 struct dx_frame frames[2], *frame;
1498 struct dx_entry *entries, *at;
1499 struct dx_hash_info hinfo;
1500 struct buffer_head * bh;
1501 struct inode *dir = dentry->d_parent->d_inode;
1502 struct super_block * sb = dir->i_sb;
1503 struct ext4_dir_entry_2 *de;
1504 int err;
1506 frame = dx_probe(dentry, NULL, &hinfo, frames, &err);
1507 if (!frame)
1508 return err;
1509 entries = frame->entries;
1510 at = frame->at;
1512 if (!(bh = ext4_bread(handle,dir, dx_get_block(frame->at), 0, &err)))
1513 goto cleanup;
1515 BUFFER_TRACE(bh, "get_write_access");
1516 err = ext4_journal_get_write_access(handle, bh);
1517 if (err)
1518 goto journal_error;
1520 err = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
1521 if (err != -ENOSPC) {
1522 bh = NULL;
1523 goto cleanup;
1526 /* Block full, should compress but for now just split */
1527 dxtrace(printk("using %u of %u node entries\n",
1528 dx_get_count(entries), dx_get_limit(entries)));
1529 /* Need to split index? */
1530 if (dx_get_count(entries) == dx_get_limit(entries)) {
1531 ext4_lblk_t newblock;
1532 unsigned icount = dx_get_count(entries);
1533 int levels = frame - frames;
1534 struct dx_entry *entries2;
1535 struct dx_node *node2;
1536 struct buffer_head *bh2;
1538 if (levels && (dx_get_count(frames->entries) ==
1539 dx_get_limit(frames->entries))) {
1540 ext4_warning(sb, __func__,
1541 "Directory index full!");
1542 err = -ENOSPC;
1543 goto cleanup;
1545 bh2 = ext4_append (handle, dir, &newblock, &err);
1546 if (!(bh2))
1547 goto cleanup;
1548 node2 = (struct dx_node *)(bh2->b_data);
1549 entries2 = node2->entries;
1550 node2->fake.rec_len = ext4_rec_len_to_disk(sb->s_blocksize);
1551 node2->fake.inode = 0;
1552 BUFFER_TRACE(frame->bh, "get_write_access");
1553 err = ext4_journal_get_write_access(handle, frame->bh);
1554 if (err)
1555 goto journal_error;
1556 if (levels) {
1557 unsigned icount1 = icount/2, icount2 = icount - icount1;
1558 unsigned hash2 = dx_get_hash(entries + icount1);
1559 dxtrace(printk("Split index %i/%i\n", icount1, icount2));
1561 BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
1562 err = ext4_journal_get_write_access(handle,
1563 frames[0].bh);
1564 if (err)
1565 goto journal_error;
1567 memcpy ((char *) entries2, (char *) (entries + icount1),
1568 icount2 * sizeof(struct dx_entry));
1569 dx_set_count (entries, icount1);
1570 dx_set_count (entries2, icount2);
1571 dx_set_limit (entries2, dx_node_limit(dir));
1573 /* Which index block gets the new entry? */
1574 if (at - entries >= icount1) {
1575 frame->at = at = at - entries - icount1 + entries2;
1576 frame->entries = entries = entries2;
1577 swap(frame->bh, bh2);
1579 dx_insert_block (frames + 0, hash2, newblock);
1580 dxtrace(dx_show_index ("node", frames[1].entries));
1581 dxtrace(dx_show_index ("node",
1582 ((struct dx_node *) bh2->b_data)->entries));
1583 err = ext4_journal_dirty_metadata(handle, bh2);
1584 if (err)
1585 goto journal_error;
1586 brelse (bh2);
1587 } else {
1588 dxtrace(printk("Creating second level index...\n"));
1589 memcpy((char *) entries2, (char *) entries,
1590 icount * sizeof(struct dx_entry));
1591 dx_set_limit(entries2, dx_node_limit(dir));
1593 /* Set up root */
1594 dx_set_count(entries, 1);
1595 dx_set_block(entries + 0, newblock);
1596 ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
1598 /* Add new access path frame */
1599 frame = frames + 1;
1600 frame->at = at = at - entries + entries2;
1601 frame->entries = entries = entries2;
1602 frame->bh = bh2;
1603 err = ext4_journal_get_write_access(handle,
1604 frame->bh);
1605 if (err)
1606 goto journal_error;
1608 ext4_journal_dirty_metadata(handle, frames[0].bh);
1610 de = do_split(handle, dir, &bh, frame, &hinfo, &err);
1611 if (!de)
1612 goto cleanup;
1613 err = add_dirent_to_buf(handle, dentry, inode, de, bh);
1614 bh = NULL;
1615 goto cleanup;
1617 journal_error:
1618 ext4_std_error(dir->i_sb, err);
1619 cleanup:
1620 if (bh)
1621 brelse(bh);
1622 dx_release(frames);
1623 return err;
1627 * ext4_delete_entry deletes a directory entry by merging it with the
1628 * previous entry
1630 static int ext4_delete_entry (handle_t *handle,
1631 struct inode * dir,
1632 struct ext4_dir_entry_2 * de_del,
1633 struct buffer_head * bh)
1635 struct ext4_dir_entry_2 * de, * pde;
1636 int i;
1638 i = 0;
1639 pde = NULL;
1640 de = (struct ext4_dir_entry_2 *) bh->b_data;
1641 while (i < bh->b_size) {
1642 if (!ext4_check_dir_entry("ext4_delete_entry", dir, de, bh, i))
1643 return -EIO;
1644 if (de == de_del) {
1645 BUFFER_TRACE(bh, "get_write_access");
1646 ext4_journal_get_write_access(handle, bh);
1647 if (pde)
1648 pde->rec_len = ext4_rec_len_to_disk(
1649 ext4_rec_len_from_disk(pde->rec_len) +
1650 ext4_rec_len_from_disk(de->rec_len));
1651 else
1652 de->inode = 0;
1653 dir->i_version++;
1654 BUFFER_TRACE(bh, "call ext4_journal_dirty_metadata");
1655 ext4_journal_dirty_metadata(handle, bh);
1656 return 0;
1658 i += ext4_rec_len_from_disk(de->rec_len);
1659 pde = de;
1660 de = ext4_next_entry(de);
1662 return -ENOENT;
1666 * DIR_NLINK feature is set if 1) nlinks > EXT4_LINK_MAX or 2) nlinks == 2,
1667 * since this indicates that nlinks count was previously 1.
1669 static void ext4_inc_count(handle_t *handle, struct inode *inode)
1671 inc_nlink(inode);
1672 if (is_dx(inode) && inode->i_nlink > 1) {
1673 /* limit is 16-bit i_links_count */
1674 if (inode->i_nlink >= EXT4_LINK_MAX || inode->i_nlink == 2) {
1675 inode->i_nlink = 1;
1676 EXT4_SET_RO_COMPAT_FEATURE(inode->i_sb,
1677 EXT4_FEATURE_RO_COMPAT_DIR_NLINK);
1683 * If a directory had nlink == 1, then we should let it be 1. This indicates
1684 * directory has >EXT4_LINK_MAX subdirs.
1686 static void ext4_dec_count(handle_t *handle, struct inode *inode)
1688 drop_nlink(inode);
1689 if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0)
1690 inc_nlink(inode);
1694 static int ext4_add_nondir(handle_t *handle,
1695 struct dentry *dentry, struct inode *inode)
1697 int err = ext4_add_entry(handle, dentry, inode);
1698 if (!err) {
1699 ext4_mark_inode_dirty(handle, inode);
1700 d_instantiate(dentry, inode);
1701 return 0;
1703 drop_nlink(inode);
1704 iput(inode);
1705 return err;
1709 * By the time this is called, we already have created
1710 * the directory cache entry for the new file, but it
1711 * is so far negative - it has no inode.
1713 * If the create succeeds, we fill in the inode information
1714 * with d_instantiate().
1716 static int ext4_create (struct inode * dir, struct dentry * dentry, int mode,
1717 struct nameidata *nd)
1719 handle_t *handle;
1720 struct inode * inode;
1721 int err, retries = 0;
1723 retry:
1724 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
1725 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
1726 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb));
1727 if (IS_ERR(handle))
1728 return PTR_ERR(handle);
1730 if (IS_DIRSYNC(dir))
1731 handle->h_sync = 1;
1733 inode = ext4_new_inode (handle, dir, mode);
1734 err = PTR_ERR(inode);
1735 if (!IS_ERR(inode)) {
1736 inode->i_op = &ext4_file_inode_operations;
1737 inode->i_fop = &ext4_file_operations;
1738 ext4_set_aops(inode);
1739 err = ext4_add_nondir(handle, dentry, inode);
1741 ext4_journal_stop(handle);
1742 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
1743 goto retry;
1744 return err;
1747 static int ext4_mknod (struct inode * dir, struct dentry *dentry,
1748 int mode, dev_t rdev)
1750 handle_t *handle;
1751 struct inode *inode;
1752 int err, retries = 0;
1754 if (!new_valid_dev(rdev))
1755 return -EINVAL;
1757 retry:
1758 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
1759 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
1760 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb));
1761 if (IS_ERR(handle))
1762 return PTR_ERR(handle);
1764 if (IS_DIRSYNC(dir))
1765 handle->h_sync = 1;
1767 inode = ext4_new_inode (handle, dir, mode);
1768 err = PTR_ERR(inode);
1769 if (!IS_ERR(inode)) {
1770 init_special_inode(inode, inode->i_mode, rdev);
1771 #ifdef CONFIG_EXT4DEV_FS_XATTR
1772 inode->i_op = &ext4_special_inode_operations;
1773 #endif
1774 err = ext4_add_nondir(handle, dentry, inode);
1776 ext4_journal_stop(handle);
1777 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
1778 goto retry;
1779 return err;
1782 static int ext4_mkdir(struct inode * dir, struct dentry * dentry, int mode)
1784 handle_t *handle;
1785 struct inode * inode;
1786 struct buffer_head * dir_block;
1787 struct ext4_dir_entry_2 * de;
1788 int err, retries = 0;
1790 if (EXT4_DIR_LINK_MAX(dir))
1791 return -EMLINK;
1793 retry:
1794 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
1795 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
1796 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb));
1797 if (IS_ERR(handle))
1798 return PTR_ERR(handle);
1800 if (IS_DIRSYNC(dir))
1801 handle->h_sync = 1;
1803 inode = ext4_new_inode (handle, dir, S_IFDIR | mode);
1804 err = PTR_ERR(inode);
1805 if (IS_ERR(inode))
1806 goto out_stop;
1808 inode->i_op = &ext4_dir_inode_operations;
1809 inode->i_fop = &ext4_dir_operations;
1810 inode->i_size = EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize;
1811 dir_block = ext4_bread (handle, inode, 0, 1, &err);
1812 if (!dir_block)
1813 goto out_clear_inode;
1814 BUFFER_TRACE(dir_block, "get_write_access");
1815 ext4_journal_get_write_access(handle, dir_block);
1816 de = (struct ext4_dir_entry_2 *) dir_block->b_data;
1817 de->inode = cpu_to_le32(inode->i_ino);
1818 de->name_len = 1;
1819 de->rec_len = ext4_rec_len_to_disk(EXT4_DIR_REC_LEN(de->name_len));
1820 strcpy (de->name, ".");
1821 ext4_set_de_type(dir->i_sb, de, S_IFDIR);
1822 de = ext4_next_entry(de);
1823 de->inode = cpu_to_le32(dir->i_ino);
1824 de->rec_len = ext4_rec_len_to_disk(inode->i_sb->s_blocksize -
1825 EXT4_DIR_REC_LEN(1));
1826 de->name_len = 2;
1827 strcpy (de->name, "..");
1828 ext4_set_de_type(dir->i_sb, de, S_IFDIR);
1829 inode->i_nlink = 2;
1830 BUFFER_TRACE(dir_block, "call ext4_journal_dirty_metadata");
1831 ext4_journal_dirty_metadata(handle, dir_block);
1832 brelse (dir_block);
1833 ext4_mark_inode_dirty(handle, inode);
1834 err = ext4_add_entry (handle, dentry, inode);
1835 if (err) {
1836 out_clear_inode:
1837 clear_nlink(inode);
1838 ext4_mark_inode_dirty(handle, inode);
1839 iput (inode);
1840 goto out_stop;
1842 ext4_inc_count(handle, dir);
1843 ext4_update_dx_flag(dir);
1844 ext4_mark_inode_dirty(handle, dir);
1845 d_instantiate(dentry, inode);
1846 out_stop:
1847 ext4_journal_stop(handle);
1848 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
1849 goto retry;
1850 return err;
1854 * routine to check that the specified directory is empty (for rmdir)
1856 static int empty_dir (struct inode * inode)
1858 unsigned long offset;
1859 struct buffer_head * bh;
1860 struct ext4_dir_entry_2 * de, * de1;
1861 struct super_block * sb;
1862 int err = 0;
1864 sb = inode->i_sb;
1865 if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2) ||
1866 !(bh = ext4_bread (NULL, inode, 0, 0, &err))) {
1867 if (err)
1868 ext4_error(inode->i_sb, __func__,
1869 "error %d reading directory #%lu offset 0",
1870 err, inode->i_ino);
1871 else
1872 ext4_warning(inode->i_sb, __func__,
1873 "bad directory (dir #%lu) - no data block",
1874 inode->i_ino);
1875 return 1;
1877 de = (struct ext4_dir_entry_2 *) bh->b_data;
1878 de1 = ext4_next_entry(de);
1879 if (le32_to_cpu(de->inode) != inode->i_ino ||
1880 !le32_to_cpu(de1->inode) ||
1881 strcmp (".", de->name) ||
1882 strcmp ("..", de1->name)) {
1883 ext4_warning (inode->i_sb, "empty_dir",
1884 "bad directory (dir #%lu) - no `.' or `..'",
1885 inode->i_ino);
1886 brelse (bh);
1887 return 1;
1889 offset = ext4_rec_len_from_disk(de->rec_len) +
1890 ext4_rec_len_from_disk(de1->rec_len);
1891 de = ext4_next_entry(de1);
1892 while (offset < inode->i_size ) {
1893 if (!bh ||
1894 (void *) de >= (void *) (bh->b_data+sb->s_blocksize)) {
1895 err = 0;
1896 brelse (bh);
1897 bh = ext4_bread (NULL, inode,
1898 offset >> EXT4_BLOCK_SIZE_BITS(sb), 0, &err);
1899 if (!bh) {
1900 if (err)
1901 ext4_error(sb, __func__,
1902 "error %d reading directory"
1903 " #%lu offset %lu",
1904 err, inode->i_ino, offset);
1905 offset += sb->s_blocksize;
1906 continue;
1908 de = (struct ext4_dir_entry_2 *) bh->b_data;
1910 if (!ext4_check_dir_entry("empty_dir", inode, de, bh, offset)) {
1911 de = (struct ext4_dir_entry_2 *)(bh->b_data +
1912 sb->s_blocksize);
1913 offset = (offset | (sb->s_blocksize - 1)) + 1;
1914 continue;
1916 if (le32_to_cpu(de->inode)) {
1917 brelse (bh);
1918 return 0;
1920 offset += ext4_rec_len_from_disk(de->rec_len);
1921 de = ext4_next_entry(de);
1923 brelse (bh);
1924 return 1;
1927 /* ext4_orphan_add() links an unlinked or truncated inode into a list of
1928 * such inodes, starting at the superblock, in case we crash before the
1929 * file is closed/deleted, or in case the inode truncate spans multiple
1930 * transactions and the last transaction is not recovered after a crash.
1932 * At filesystem recovery time, we walk this list deleting unlinked
1933 * inodes and truncating linked inodes in ext4_orphan_cleanup().
1935 int ext4_orphan_add(handle_t *handle, struct inode *inode)
1937 struct super_block *sb = inode->i_sb;
1938 struct ext4_iloc iloc;
1939 int err = 0, rc;
1941 lock_super(sb);
1942 if (!list_empty(&EXT4_I(inode)->i_orphan))
1943 goto out_unlock;
1945 /* Orphan handling is only valid for files with data blocks
1946 * being truncated, or files being unlinked. */
1948 /* @@@ FIXME: Observation from aviro:
1949 * I think I can trigger J_ASSERT in ext4_orphan_add(). We block
1950 * here (on lock_super()), so race with ext4_link() which might bump
1951 * ->i_nlink. For, say it, character device. Not a regular file,
1952 * not a directory, not a symlink and ->i_nlink > 0.
1954 J_ASSERT ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1955 S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
1957 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
1958 err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
1959 if (err)
1960 goto out_unlock;
1962 err = ext4_reserve_inode_write(handle, inode, &iloc);
1963 if (err)
1964 goto out_unlock;
1966 /* Insert this inode at the head of the on-disk orphan list... */
1967 NEXT_ORPHAN(inode) = le32_to_cpu(EXT4_SB(sb)->s_es->s_last_orphan);
1968 EXT4_SB(sb)->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
1969 err = ext4_journal_dirty_metadata(handle, EXT4_SB(sb)->s_sbh);
1970 rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
1971 if (!err)
1972 err = rc;
1974 /* Only add to the head of the in-memory list if all the
1975 * previous operations succeeded. If the orphan_add is going to
1976 * fail (possibly taking the journal offline), we can't risk
1977 * leaving the inode on the orphan list: stray orphan-list
1978 * entries can cause panics at unmount time.
1980 * This is safe: on error we're going to ignore the orphan list
1981 * anyway on the next recovery. */
1982 if (!err)
1983 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
1985 jbd_debug(4, "superblock will point to %lu\n", inode->i_ino);
1986 jbd_debug(4, "orphan inode %lu will point to %d\n",
1987 inode->i_ino, NEXT_ORPHAN(inode));
1988 out_unlock:
1989 unlock_super(sb);
1990 ext4_std_error(inode->i_sb, err);
1991 return err;
1995 * ext4_orphan_del() removes an unlinked or truncated inode from the list
1996 * of such inodes stored on disk, because it is finally being cleaned up.
1998 int ext4_orphan_del(handle_t *handle, struct inode *inode)
2000 struct list_head *prev;
2001 struct ext4_inode_info *ei = EXT4_I(inode);
2002 struct ext4_sb_info *sbi;
2003 unsigned long ino_next;
2004 struct ext4_iloc iloc;
2005 int err = 0;
2007 lock_super(inode->i_sb);
2008 if (list_empty(&ei->i_orphan)) {
2009 unlock_super(inode->i_sb);
2010 return 0;
2013 ino_next = NEXT_ORPHAN(inode);
2014 prev = ei->i_orphan.prev;
2015 sbi = EXT4_SB(inode->i_sb);
2017 jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
2019 list_del_init(&ei->i_orphan);
2021 /* If we're on an error path, we may not have a valid
2022 * transaction handle with which to update the orphan list on
2023 * disk, but we still need to remove the inode from the linked
2024 * list in memory. */
2025 if (!handle)
2026 goto out;
2028 err = ext4_reserve_inode_write(handle, inode, &iloc);
2029 if (err)
2030 goto out_err;
2032 if (prev == &sbi->s_orphan) {
2033 jbd_debug(4, "superblock will point to %lu\n", ino_next);
2034 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
2035 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
2036 if (err)
2037 goto out_brelse;
2038 sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
2039 err = ext4_journal_dirty_metadata(handle, sbi->s_sbh);
2040 } else {
2041 struct ext4_iloc iloc2;
2042 struct inode *i_prev =
2043 &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode;
2045 jbd_debug(4, "orphan inode %lu will point to %lu\n",
2046 i_prev->i_ino, ino_next);
2047 err = ext4_reserve_inode_write(handle, i_prev, &iloc2);
2048 if (err)
2049 goto out_brelse;
2050 NEXT_ORPHAN(i_prev) = ino_next;
2051 err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2);
2053 if (err)
2054 goto out_brelse;
2055 NEXT_ORPHAN(inode) = 0;
2056 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
2058 out_err:
2059 ext4_std_error(inode->i_sb, err);
2060 out:
2061 unlock_super(inode->i_sb);
2062 return err;
2064 out_brelse:
2065 brelse(iloc.bh);
2066 goto out_err;
2069 static int ext4_rmdir (struct inode * dir, struct dentry *dentry)
2071 int retval;
2072 struct inode * inode;
2073 struct buffer_head * bh;
2074 struct ext4_dir_entry_2 * de;
2075 handle_t *handle;
2077 /* Initialize quotas before so that eventual writes go in
2078 * separate transaction */
2079 DQUOT_INIT(dentry->d_inode);
2080 handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb));
2081 if (IS_ERR(handle))
2082 return PTR_ERR(handle);
2084 retval = -ENOENT;
2085 bh = ext4_find_entry (dentry, &de);
2086 if (!bh)
2087 goto end_rmdir;
2089 if (IS_DIRSYNC(dir))
2090 handle->h_sync = 1;
2092 inode = dentry->d_inode;
2094 retval = -EIO;
2095 if (le32_to_cpu(de->inode) != inode->i_ino)
2096 goto end_rmdir;
2098 retval = -ENOTEMPTY;
2099 if (!empty_dir (inode))
2100 goto end_rmdir;
2102 retval = ext4_delete_entry(handle, dir, de, bh);
2103 if (retval)
2104 goto end_rmdir;
2105 if (!EXT4_DIR_LINK_EMPTY(inode))
2106 ext4_warning (inode->i_sb, "ext4_rmdir",
2107 "empty directory has too many links (%d)",
2108 inode->i_nlink);
2109 inode->i_version++;
2110 clear_nlink(inode);
2111 /* There's no need to set i_disksize: the fact that i_nlink is
2112 * zero will ensure that the right thing happens during any
2113 * recovery. */
2114 inode->i_size = 0;
2115 ext4_orphan_add(handle, inode);
2116 inode->i_ctime = dir->i_ctime = dir->i_mtime = ext4_current_time(inode);
2117 ext4_mark_inode_dirty(handle, inode);
2118 ext4_dec_count(handle, dir);
2119 ext4_update_dx_flag(dir);
2120 ext4_mark_inode_dirty(handle, dir);
2122 end_rmdir:
2123 ext4_journal_stop(handle);
2124 brelse (bh);
2125 return retval;
2128 static int ext4_unlink(struct inode * dir, struct dentry *dentry)
2130 int retval;
2131 struct inode * inode;
2132 struct buffer_head * bh;
2133 struct ext4_dir_entry_2 * de;
2134 handle_t *handle;
2136 /* Initialize quotas before so that eventual writes go
2137 * in separate transaction */
2138 DQUOT_INIT(dentry->d_inode);
2139 handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb));
2140 if (IS_ERR(handle))
2141 return PTR_ERR(handle);
2143 if (IS_DIRSYNC(dir))
2144 handle->h_sync = 1;
2146 retval = -ENOENT;
2147 bh = ext4_find_entry (dentry, &de);
2148 if (!bh)
2149 goto end_unlink;
2151 inode = dentry->d_inode;
2153 retval = -EIO;
2154 if (le32_to_cpu(de->inode) != inode->i_ino)
2155 goto end_unlink;
2157 if (!inode->i_nlink) {
2158 ext4_warning (inode->i_sb, "ext4_unlink",
2159 "Deleting nonexistent file (%lu), %d",
2160 inode->i_ino, inode->i_nlink);
2161 inode->i_nlink = 1;
2163 retval = ext4_delete_entry(handle, dir, de, bh);
2164 if (retval)
2165 goto end_unlink;
2166 dir->i_ctime = dir->i_mtime = ext4_current_time(dir);
2167 ext4_update_dx_flag(dir);
2168 ext4_mark_inode_dirty(handle, dir);
2169 drop_nlink(inode);
2170 if (!inode->i_nlink)
2171 ext4_orphan_add(handle, inode);
2172 inode->i_ctime = ext4_current_time(inode);
2173 ext4_mark_inode_dirty(handle, inode);
2174 retval = 0;
2176 end_unlink:
2177 ext4_journal_stop(handle);
2178 brelse (bh);
2179 return retval;
2182 static int ext4_symlink (struct inode * dir,
2183 struct dentry *dentry, const char * symname)
2185 handle_t *handle;
2186 struct inode * inode;
2187 int l, err, retries = 0;
2189 l = strlen(symname)+1;
2190 if (l > dir->i_sb->s_blocksize)
2191 return -ENAMETOOLONG;
2193 retry:
2194 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2195 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 5 +
2196 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb));
2197 if (IS_ERR(handle))
2198 return PTR_ERR(handle);
2200 if (IS_DIRSYNC(dir))
2201 handle->h_sync = 1;
2203 inode = ext4_new_inode (handle, dir, S_IFLNK|S_IRWXUGO);
2204 err = PTR_ERR(inode);
2205 if (IS_ERR(inode))
2206 goto out_stop;
2208 if (l > sizeof (EXT4_I(inode)->i_data)) {
2209 inode->i_op = &ext4_symlink_inode_operations;
2210 ext4_set_aops(inode);
2212 * page_symlink() calls into ext4_prepare/commit_write.
2213 * We have a transaction open. All is sweetness. It also sets
2214 * i_size in generic_commit_write().
2216 err = __page_symlink(inode, symname, l,
2217 mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS);
2218 if (err) {
2219 clear_nlink(inode);
2220 ext4_mark_inode_dirty(handle, inode);
2221 iput (inode);
2222 goto out_stop;
2224 } else {
2225 /* clear the extent format for fast symlink */
2226 EXT4_I(inode)->i_flags &= ~EXT4_EXTENTS_FL;
2227 inode->i_op = &ext4_fast_symlink_inode_operations;
2228 memcpy((char*)&EXT4_I(inode)->i_data,symname,l);
2229 inode->i_size = l-1;
2231 EXT4_I(inode)->i_disksize = inode->i_size;
2232 err = ext4_add_nondir(handle, dentry, inode);
2233 out_stop:
2234 ext4_journal_stop(handle);
2235 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2236 goto retry;
2237 return err;
2240 static int ext4_link (struct dentry * old_dentry,
2241 struct inode * dir, struct dentry *dentry)
2243 handle_t *handle;
2244 struct inode *inode = old_dentry->d_inode;
2245 int err, retries = 0;
2247 if (EXT4_DIR_LINK_MAX(inode))
2248 return -EMLINK;
2251 * Return -ENOENT if we've raced with unlink and i_nlink is 0. Doing
2252 * otherwise has the potential to corrupt the orphan inode list.
2254 if (inode->i_nlink == 0)
2255 return -ENOENT;
2257 retry:
2258 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2259 EXT4_INDEX_EXTRA_TRANS_BLOCKS);
2260 if (IS_ERR(handle))
2261 return PTR_ERR(handle);
2263 if (IS_DIRSYNC(dir))
2264 handle->h_sync = 1;
2266 inode->i_ctime = ext4_current_time(inode);
2267 ext4_inc_count(handle, inode);
2268 atomic_inc(&inode->i_count);
2270 err = ext4_add_nondir(handle, dentry, inode);
2271 ext4_journal_stop(handle);
2272 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2273 goto retry;
2274 return err;
2277 #define PARENT_INO(buffer) \
2278 (ext4_next_entry((struct ext4_dir_entry_2 *)(buffer))->inode)
2281 * Anybody can rename anything with this: the permission checks are left to the
2282 * higher-level routines.
2284 static int ext4_rename (struct inode * old_dir, struct dentry *old_dentry,
2285 struct inode * new_dir,struct dentry *new_dentry)
2287 handle_t *handle;
2288 struct inode * old_inode, * new_inode;
2289 struct buffer_head * old_bh, * new_bh, * dir_bh;
2290 struct ext4_dir_entry_2 * old_de, * new_de;
2291 int retval;
2293 old_bh = new_bh = dir_bh = NULL;
2295 /* Initialize quotas before so that eventual writes go
2296 * in separate transaction */
2297 if (new_dentry->d_inode)
2298 DQUOT_INIT(new_dentry->d_inode);
2299 handle = ext4_journal_start(old_dir, 2 *
2300 EXT4_DATA_TRANS_BLOCKS(old_dir->i_sb) +
2301 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2);
2302 if (IS_ERR(handle))
2303 return PTR_ERR(handle);
2305 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
2306 handle->h_sync = 1;
2308 old_bh = ext4_find_entry (old_dentry, &old_de);
2310 * Check for inode number is _not_ due to possible IO errors.
2311 * We might rmdir the source, keep it as pwd of some process
2312 * and merrily kill the link to whatever was created under the
2313 * same name. Goodbye sticky bit ;-<
2315 old_inode = old_dentry->d_inode;
2316 retval = -ENOENT;
2317 if (!old_bh || le32_to_cpu(old_de->inode) != old_inode->i_ino)
2318 goto end_rename;
2320 new_inode = new_dentry->d_inode;
2321 new_bh = ext4_find_entry (new_dentry, &new_de);
2322 if (new_bh) {
2323 if (!new_inode) {
2324 brelse (new_bh);
2325 new_bh = NULL;
2328 if (S_ISDIR(old_inode->i_mode)) {
2329 if (new_inode) {
2330 retval = -ENOTEMPTY;
2331 if (!empty_dir (new_inode))
2332 goto end_rename;
2334 retval = -EIO;
2335 dir_bh = ext4_bread (handle, old_inode, 0, 0, &retval);
2336 if (!dir_bh)
2337 goto end_rename;
2338 if (le32_to_cpu(PARENT_INO(dir_bh->b_data)) != old_dir->i_ino)
2339 goto end_rename;
2340 retval = -EMLINK;
2341 if (!new_inode && new_dir!=old_dir &&
2342 new_dir->i_nlink >= EXT4_LINK_MAX)
2343 goto end_rename;
2345 if (!new_bh) {
2346 retval = ext4_add_entry (handle, new_dentry, old_inode);
2347 if (retval)
2348 goto end_rename;
2349 } else {
2350 BUFFER_TRACE(new_bh, "get write access");
2351 ext4_journal_get_write_access(handle, new_bh);
2352 new_de->inode = cpu_to_le32(old_inode->i_ino);
2353 if (EXT4_HAS_INCOMPAT_FEATURE(new_dir->i_sb,
2354 EXT4_FEATURE_INCOMPAT_FILETYPE))
2355 new_de->file_type = old_de->file_type;
2356 new_dir->i_version++;
2357 new_dir->i_ctime = new_dir->i_mtime =
2358 ext4_current_time(new_dir);
2359 ext4_mark_inode_dirty(handle, new_dir);
2360 BUFFER_TRACE(new_bh, "call ext4_journal_dirty_metadata");
2361 ext4_journal_dirty_metadata(handle, new_bh);
2362 brelse(new_bh);
2363 new_bh = NULL;
2367 * Like most other Unix systems, set the ctime for inodes on a
2368 * rename.
2370 old_inode->i_ctime = ext4_current_time(old_inode);
2371 ext4_mark_inode_dirty(handle, old_inode);
2374 * ok, that's it
2376 if (le32_to_cpu(old_de->inode) != old_inode->i_ino ||
2377 old_de->name_len != old_dentry->d_name.len ||
2378 strncmp(old_de->name, old_dentry->d_name.name, old_de->name_len) ||
2379 (retval = ext4_delete_entry(handle, old_dir,
2380 old_de, old_bh)) == -ENOENT) {
2381 /* old_de could have moved from under us during htree split, so
2382 * make sure that we are deleting the right entry. We might
2383 * also be pointing to a stale entry in the unused part of
2384 * old_bh so just checking inum and the name isn't enough. */
2385 struct buffer_head *old_bh2;
2386 struct ext4_dir_entry_2 *old_de2;
2388 old_bh2 = ext4_find_entry(old_dentry, &old_de2);
2389 if (old_bh2) {
2390 retval = ext4_delete_entry(handle, old_dir,
2391 old_de2, old_bh2);
2392 brelse(old_bh2);
2395 if (retval) {
2396 ext4_warning(old_dir->i_sb, "ext4_rename",
2397 "Deleting old file (%lu), %d, error=%d",
2398 old_dir->i_ino, old_dir->i_nlink, retval);
2401 if (new_inode) {
2402 ext4_dec_count(handle, new_inode);
2403 new_inode->i_ctime = ext4_current_time(new_inode);
2405 old_dir->i_ctime = old_dir->i_mtime = ext4_current_time(old_dir);
2406 ext4_update_dx_flag(old_dir);
2407 if (dir_bh) {
2408 BUFFER_TRACE(dir_bh, "get_write_access");
2409 ext4_journal_get_write_access(handle, dir_bh);
2410 PARENT_INO(dir_bh->b_data) = cpu_to_le32(new_dir->i_ino);
2411 BUFFER_TRACE(dir_bh, "call ext4_journal_dirty_metadata");
2412 ext4_journal_dirty_metadata(handle, dir_bh);
2413 ext4_dec_count(handle, old_dir);
2414 if (new_inode) {
2415 /* checked empty_dir above, can't have another parent,
2416 * ext4_dec_count() won't work for many-linked dirs */
2417 new_inode->i_nlink = 0;
2418 } else {
2419 ext4_inc_count(handle, new_dir);
2420 ext4_update_dx_flag(new_dir);
2421 ext4_mark_inode_dirty(handle, new_dir);
2424 ext4_mark_inode_dirty(handle, old_dir);
2425 if (new_inode) {
2426 ext4_mark_inode_dirty(handle, new_inode);
2427 if (!new_inode->i_nlink)
2428 ext4_orphan_add(handle, new_inode);
2430 retval = 0;
2432 end_rename:
2433 brelse (dir_bh);
2434 brelse (old_bh);
2435 brelse (new_bh);
2436 ext4_journal_stop(handle);
2437 return retval;
2441 * directories can handle most operations...
2443 const struct inode_operations ext4_dir_inode_operations = {
2444 .create = ext4_create,
2445 .lookup = ext4_lookup,
2446 .link = ext4_link,
2447 .unlink = ext4_unlink,
2448 .symlink = ext4_symlink,
2449 .mkdir = ext4_mkdir,
2450 .rmdir = ext4_rmdir,
2451 .mknod = ext4_mknod,
2452 .rename = ext4_rename,
2453 .setattr = ext4_setattr,
2454 #ifdef CONFIG_EXT4DEV_FS_XATTR
2455 .setxattr = generic_setxattr,
2456 .getxattr = generic_getxattr,
2457 .listxattr = ext4_listxattr,
2458 .removexattr = generic_removexattr,
2459 #endif
2460 .permission = ext4_permission,
2463 const struct inode_operations ext4_special_inode_operations = {
2464 .setattr = ext4_setattr,
2465 #ifdef CONFIG_EXT4DEV_FS_XATTR
2466 .setxattr = generic_setxattr,
2467 .getxattr = generic_getxattr,
2468 .listxattr = ext4_listxattr,
2469 .removexattr = generic_removexattr,
2470 #endif
2471 .permission = ext4_permission,