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)
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
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>
38 #include "ext4_jbd2.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
,
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
);
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
);
74 #define assert(test) J_ASSERT(test)
78 #define swap(x, y) do { typeof(x) z = x; x = y; y = z; } while (0)
82 #define dxtrace(command) command
84 #define dxtrace(command)
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.
115 struct fake_dirent dot
;
117 struct fake_dirent dotdot
;
121 __le32 reserved_zero
;
123 u8 info_length
; /* 8 */
128 struct dx_entry entries
[0];
133 struct fake_dirent fake
;
134 struct dx_entry entries
[0];
140 struct buffer_head
*bh
;
141 struct dx_entry
*entries
;
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
,
164 struct dx_hash_info
*hinfo
,
165 struct dx_frame
*frame
,
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
,
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
);
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
));
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
;
273 while ((char *) de
< base
+ size
)
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
);
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
;
301 struct buffer_head
*bh
;
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
;
310 printk("%s%3u:%03u hash %8x/%8x ",levels
?"":" ", i
, block
, hash
, range
);
311 if (!(bh
= ext4_bread (NULL
,dir
, block
, 0,&err
))) continue;
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
;
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
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
;
349 dir
= dentry
->d_parent
->d_inode
;
350 if (!(bh
= ext4_bread (NULL
,dir
, 0, 0, err
)))
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
);
360 *err
= ERR_BAD_DX_DIR
;
363 hinfo
->hash_version
= root
->info
.hash_version
;
364 hinfo
->seed
= EXT4_SB(dir
->i_sb
)->s_hash_seed
;
366 ext4fs_dirhash(dentry
->d_name
.name
, dentry
->d_name
.len
, hinfo
);
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
);
374 *err
= ERR_BAD_DX_DIR
;
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
);
383 *err
= ERR_BAD_DX_DIR
;
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");
395 *err
= ERR_BAD_DX_DIR
;
399 dxtrace (printk("Look up %x", hash
));
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");
407 *err
= ERR_BAD_DX_DIR
;
412 q
= entries
+ count
- 1;
416 dxtrace(printk("."));
417 if (dx_get_hash(m
) > hash
)
423 if (0) // linear search cross check
425 unsigned n
= count
- 1;
429 dxtrace(printk(","));
430 if (dx_get_hash(++at
) > hash
)
436 assert (at
== p
- 1);
440 dxtrace(printk(" %x->%u\n", at
== entries
? 0: dx_get_hash(at
), dx_get_block(at
)));
442 frame
->entries
= entries
;
444 if (!indirect
--) return frame
;
445 if (!(bh
= ext4_bread (NULL
,dir
, dx_get_block(at
), 0, err
)))
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");
452 *err
= ERR_BAD_DX_DIR
;
459 while (frame
>= frame_in
) {
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
);
471 static void dx_release (struct dx_frame
*frames
)
473 if (frames
[0].bh
== NULL
)
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
,
504 struct buffer_head
*bh
;
505 int err
, num_frames
= 0;
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.
517 if (++(p
->at
) < p
->entries
+ dx_get_count(p
->entries
))
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
);
535 if ((hash
& 1) == 0) {
536 if ((bhash
& ~1) != hash
)
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
),
546 return err
; /* Failure */
550 p
->at
= p
->entries
= ((struct dx_node
*) bh
->b_data
)->entries
;
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
;
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
)))
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;
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
)))
605 if ((err
= ext4_htree_store_dirent(dir_file
,
606 hinfo
->hash
, hinfo
->minor_hash
, de
)) != 0) {
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
;
637 dxtrace(printk("In htree_fill_tree, start hash: %x:%x\n", start_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
);
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
);
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)
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)
670 block
= dx_get_block(frame
->at
);
671 ret
= htree_dirblock_to_tree(dir_file
, dir
, block
, &hinfo
,
672 start_hash
, start_minor_hash
);
679 ret
= ext4_htree_next_block(dir
, HASH_NB_ALWAYS
,
680 frame
, frames
, &hashval
);
681 *next_hash
= hashval
;
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
692 (count
&& ((hashval
& 1) == 0)))
696 dxtrace(printk("Fill tree: returned %d entries, next hash: %x\n",
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
)
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
);
725 map_tail
->hash
= h
.hash
;
726 map_tail
->offs
= (u16
) ((char *) de
- base
);
727 map_tail
->size
= le16_to_cpu(de
->rec_len
);
731 /* XXX: do we need to check rec_len == 0 case? -Chris */
732 de
= ext4_next_entry(de
);
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;
742 /* Combsort until bubble sort doesn't suck */
745 if (count
- 9 < 2) /* 9, 10 -> 11 */
747 for (p
= top
, q
= p
- count
; q
>= map
; p
--, q
--)
748 if (p
->hash
< q
->hash
)
751 /* Garden variety bubble sort */
756 if (q
[1].hash
>= q
[0].hash
)
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
)
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
,
806 struct dentry
*dentry
,
807 unsigned long offset
,
808 struct ext4_dir_entry_2
** res_dir
)
810 struct ext4_dir_entry_2
* de
;
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
))
831 /* prevent looping on a bad block */
832 de_len
= ext4_rec_len_from_disk(de
->rec_len
);
836 de
= (struct ext4_dir_entry_2
*) ((char *) de
+ de_len
);
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
862 int ra_ptr
= 0; /* Current index into readahead
867 struct inode
*dir
= dentry
->d_parent
->d_inode
;
872 namelen
= dentry
->d_name
.len
;
873 if (namelen
> EXT4_NAME_LEN
)
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
882 if (bh
|| (err
!= ERR_BAD_DX_DIR
))
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
)
894 * We deal with the read-ahead logic here.
896 if (ra_ptr
>= ra_max
) {
897 /* Refill the readahead buffer */
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
;
911 bh
= ext4_getblk(NULL
, dir
, b
++, 0, &err
);
914 ll_rw_block(READ_META
, 1, &bh
);
917 if ((bh
= bh_use
[ra_ptr
++]) == NULL
)
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
);
928 i
= search_dirblock(bh
, dir
, dentry
,
929 block
<< EXT4_BLOCK_SIZE_BITS(sb
), res_dir
);
931 EXT4_I(dir
)->i_dir_start_lookup
= block
;
933 goto cleanup_and_exit
;
937 goto cleanup_and_exit
;
940 if (++block
>= nblocks
)
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.
949 nblocks
= dir
->i_size
>> EXT4_BLOCK_SIZE_BITS(sb
);
950 if (block
< nblocks
) {
956 /* Clean up the read-ahead blocks */
957 for (; ra_ptr
< ra_max
; ra_ptr
++)
958 brelse (bh_use
[ra_ptr
]);
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
;
968 struct dx_frame frames
[2], *frame
;
969 struct ext4_dir_entry_2
*de
, *top
;
970 struct buffer_head
*bh
;
973 int namelen
= dentry
->d_name
.len
;
974 const u8
*name
= dentry
->d_name
.name
;
975 struct inode
*dir
= dentry
->d_parent
->d_inode
;
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
)))
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 */
990 block
= dx_get_block(frame
->at
);
991 if (!(bh
= ext4_bread (NULL
,dir
, block
, 0, err
)))
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",
1000 (block
<<EXT4_BLOCK_SIZE_BITS(sb
))
1001 +((char *)de
- bh
->b_data
))) {
1003 *err
= ERR_BAD_DX_DIR
;
1007 dx_release (frames
);
1011 /* Check to see if we should continue to search */
1012 retval
= ext4_htree_next_block(dir
, hash
, frame
,
1015 ext4_warning(sb
, __func__
,
1016 "error reading index page in directory #%lu",
1021 } while (retval
== 1);
1025 dxtrace(printk("%s not found\n", name
));
1026 dx_release (frames
);
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
);
1042 unsigned long ino
= le32_to_cpu(de
->inode
);
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
);
1051 return ERR_CAST(inode
);
1053 return d_splice_alias(inode
, dentry
);
1057 struct dentry
*ext4_get_parent(struct dentry
*child
)
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
);
1073 return ERR_PTR(-ENOENT
);
1074 ino
= le32_to_cpu(de
->inode
);
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
);
1085 return ERR_CAST(inode
);
1087 parent
= d_alloc_anon(inode
);
1090 parent
= ERR_PTR(-ENOMEM
);
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
,
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;
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
);
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;
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
);
1150 memmove(to
, de
, rec_len
);
1151 to
->rec_len
= ext4_rec_len_to_disk(rec_len
);
1153 to
= (struct ext4_dir_entry_2
*) (((char *) to
) + rec_len
);
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
;
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
;
1180 bh2
= ext4_append (handle
, dir
, &newblock
, &err
);
1187 BUFFER_TRACE(*bh
, "get_write_access");
1188 err
= ext4_journal_get_write_access(handle
, *bh
);
1192 BUFFER_TRACE(frame
->bh
, "get_write_access");
1193 err
= ext4_journal_get_write_access(handle
, frame
->bh
);
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
);
1204 dx_sort_map (map
, count
);
1205 /* Split the existing block in the middle, size-wise */
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)
1212 size
+= map
[i
].size
;
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
)
1237 dx_insert_block (frame
, hash2
+ continued
, newblock
);
1238 err
= ext4_journal_dirty_metadata (handle
, bh2
);
1241 err
= ext4_journal_dirty_metadata (handle
, frame
->bh
);
1245 dxtrace(dx_show_index ("frame", frame
->entries
));
1252 ext4_std_error(dir
->i_sb
, err
);
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
;
1281 reclen
= EXT4_DIR_REC_LEN(namelen
);
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
,
1291 if (ext4_match (namelen
, name
, de
)) {
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
)
1299 de
= (struct ext4_dir_entry_2
*)((char *)de
+ rlen
);
1302 if ((char *) de
> top
)
1305 BUFFER_TRACE(bh
, "get_write_access");
1306 err
= ext4_journal_get_write_access(handle
, bh
);
1308 ext4_std_error(dir
->i_sb
, 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
);
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
);
1322 de
->file_type
= EXT4_FT_UNKNOWN
;
1324 de
->inode
= cpu_to_le32(inode
->i_ino
);
1325 ext4_set_de_type(dir
->i_sb
, de
, inode
->i_mode
);
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
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
);
1344 ext4_mark_inode_dirty(handle
, dir
);
1345 BUFFER_TRACE(bh
, "call ext4_journal_dirty_metadata");
1346 err
= ext4_journal_dirty_metadata(handle
, bh
);
1348 ext4_std_error(dir
->i_sb
, err
);
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
;
1372 struct dx_hash_info hinfo
;
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
);
1380 ext4_std_error(dir
->i_sb
, retval
);
1384 root
= (struct dx_root
*) bh
->b_data
;
1386 bh2
= ext4_append (handle
, dir
, &block
, &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
;
1402 while ((char *)(de2
= ext4_next_entry(de
)) < top
)
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
);
1421 frame
->entries
= entries
;
1422 frame
->at
= entries
;
1425 de
= do_split(handle
,dir
, &bh
, frame
, &hinfo
, &retval
);
1426 dx_release (frames
);
1430 return add_dirent_to_buf(handle
, dentry
, inode
, de
, bh
);
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
;
1454 ext4_lblk_t block
, blocks
;
1457 blocksize
= sb
->s_blocksize
;
1458 if (!dentry
->d_name
.len
)
1461 retval
= ext4_dx_add_entry(handle
, dentry
, inode
);
1462 if (!retval
|| (retval
!= ERR_BAD_DX_DIR
))
1464 EXT4_I(dir
)->i_flags
&= ~EXT4_INDEX_FL
;
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
);
1473 retval
= add_dirent_to_buf(handle
, dentry
, inode
, NULL
, bh
);
1474 if (retval
!= -ENOSPC
)
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
);
1482 bh
= ext4_append(handle
, dir
, &block
, &retval
);
1485 de
= (struct ext4_dir_entry_2
*) bh
->b_data
;
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
;
1506 frame
= dx_probe(dentry
, NULL
, &hinfo
, frames
, &err
);
1509 entries
= frame
->entries
;
1512 if (!(bh
= ext4_bread(handle
,dir
, dx_get_block(frame
->at
), 0, &err
)))
1515 BUFFER_TRACE(bh
, "get_write_access");
1516 err
= ext4_journal_get_write_access(handle
, bh
);
1520 err
= add_dirent_to_buf(handle
, dentry
, inode
, NULL
, bh
);
1521 if (err
!= -ENOSPC
) {
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!");
1545 bh2
= ext4_append (handle
, dir
, &newblock
, &err
);
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
);
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
,
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
);
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
));
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 */
1600 frame
->at
= at
= at
- entries
+ entries2
;
1601 frame
->entries
= entries
= entries2
;
1603 err
= ext4_journal_get_write_access(handle
,
1608 ext4_journal_dirty_metadata(handle
, frames
[0].bh
);
1610 de
= do_split(handle
, dir
, &bh
, frame
, &hinfo
, &err
);
1613 err
= add_dirent_to_buf(handle
, dentry
, inode
, de
, bh
);
1618 ext4_std_error(dir
->i_sb
, err
);
1627 * ext4_delete_entry deletes a directory entry by merging it with the
1630 static int ext4_delete_entry (handle_t
*handle
,
1632 struct ext4_dir_entry_2
* de_del
,
1633 struct buffer_head
* bh
)
1635 struct ext4_dir_entry_2
* de
, * pde
;
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
))
1645 BUFFER_TRACE(bh
, "get_write_access");
1646 ext4_journal_get_write_access(handle
, bh
);
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
));
1654 BUFFER_TRACE(bh
, "call ext4_journal_dirty_metadata");
1655 ext4_journal_dirty_metadata(handle
, bh
);
1658 i
+= ext4_rec_len_from_disk(de
->rec_len
);
1660 de
= ext4_next_entry(de
);
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
)
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) {
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
)
1689 if (S_ISDIR(inode
->i_mode
) && inode
->i_nlink
== 0)
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
);
1699 ext4_mark_inode_dirty(handle
, inode
);
1700 d_instantiate(dentry
, inode
);
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
)
1720 struct inode
* inode
;
1721 int err
, retries
= 0;
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
));
1728 return PTR_ERR(handle
);
1730 if (IS_DIRSYNC(dir
))
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
))
1747 static int ext4_mknod (struct inode
* dir
, struct dentry
*dentry
,
1748 int mode
, dev_t rdev
)
1751 struct inode
*inode
;
1752 int err
, retries
= 0;
1754 if (!new_valid_dev(rdev
))
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
));
1762 return PTR_ERR(handle
);
1764 if (IS_DIRSYNC(dir
))
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
;
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
))
1782 static int ext4_mkdir(struct inode
* dir
, struct dentry
* dentry
, int mode
)
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
))
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
));
1798 return PTR_ERR(handle
);
1800 if (IS_DIRSYNC(dir
))
1803 inode
= ext4_new_inode (handle
, dir
, S_IFDIR
| mode
);
1804 err
= PTR_ERR(inode
);
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
);
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
);
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));
1827 strcpy (de
->name
, "..");
1828 ext4_set_de_type(dir
->i_sb
, de
, S_IFDIR
);
1830 BUFFER_TRACE(dir_block
, "call ext4_journal_dirty_metadata");
1831 ext4_journal_dirty_metadata(handle
, dir_block
);
1833 ext4_mark_inode_dirty(handle
, inode
);
1834 err
= ext4_add_entry (handle
, dentry
, inode
);
1838 ext4_mark_inode_dirty(handle
, inode
);
1842 ext4_inc_count(handle
, dir
);
1843 ext4_update_dx_flag(dir
);
1844 ext4_mark_inode_dirty(handle
, dir
);
1845 d_instantiate(dentry
, inode
);
1847 ext4_journal_stop(handle
);
1848 if (err
== -ENOSPC
&& ext4_should_retry_alloc(dir
->i_sb
, &retries
))
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
;
1865 if (inode
->i_size
< EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2) ||
1866 !(bh
= ext4_bread (NULL
, inode
, 0, 0, &err
))) {
1868 ext4_error(inode
->i_sb
, __func__
,
1869 "error %d reading directory #%lu offset 0",
1872 ext4_warning(inode
->i_sb
, __func__
,
1873 "bad directory (dir #%lu) - no data block",
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 `..'",
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
) {
1894 (void *) de
>= (void *) (bh
->b_data
+sb
->s_blocksize
)) {
1897 bh
= ext4_bread (NULL
, inode
,
1898 offset
>> EXT4_BLOCK_SIZE_BITS(sb
), 0, &err
);
1901 ext4_error(sb
, __func__
,
1902 "error %d reading directory"
1904 err
, inode
->i_ino
, offset
);
1905 offset
+= sb
->s_blocksize
;
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
+
1913 offset
= (offset
| (sb
->s_blocksize
- 1)) + 1;
1916 if (le32_to_cpu(de
->inode
)) {
1920 offset
+= ext4_rec_len_from_disk(de
->rec_len
);
1921 de
= ext4_next_entry(de
);
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
;
1942 if (!list_empty(&EXT4_I(inode
)->i_orphan
))
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
);
1962 err
= ext4_reserve_inode_write(handle
, inode
, &iloc
);
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
);
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. */
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
));
1990 ext4_std_error(inode
->i_sb
, 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
;
2007 lock_super(inode
->i_sb
);
2008 if (list_empty(&ei
->i_orphan
)) {
2009 unlock_super(inode
->i_sb
);
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. */
2028 err
= ext4_reserve_inode_write(handle
, inode
, &iloc
);
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
);
2038 sbi
->s_es
->s_last_orphan
= cpu_to_le32(ino_next
);
2039 err
= ext4_journal_dirty_metadata(handle
, sbi
->s_sbh
);
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
);
2050 NEXT_ORPHAN(i_prev
) = ino_next
;
2051 err
= ext4_mark_iloc_dirty(handle
, i_prev
, &iloc2
);
2055 NEXT_ORPHAN(inode
) = 0;
2056 err
= ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
2059 ext4_std_error(inode
->i_sb
, err
);
2061 unlock_super(inode
->i_sb
);
2069 static int ext4_rmdir (struct inode
* dir
, struct dentry
*dentry
)
2072 struct inode
* inode
;
2073 struct buffer_head
* bh
;
2074 struct ext4_dir_entry_2
* de
;
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
));
2082 return PTR_ERR(handle
);
2085 bh
= ext4_find_entry (dentry
, &de
);
2089 if (IS_DIRSYNC(dir
))
2092 inode
= dentry
->d_inode
;
2095 if (le32_to_cpu(de
->inode
) != inode
->i_ino
)
2098 retval
= -ENOTEMPTY
;
2099 if (!empty_dir (inode
))
2102 retval
= ext4_delete_entry(handle
, dir
, de
, bh
);
2105 if (!EXT4_DIR_LINK_EMPTY(inode
))
2106 ext4_warning (inode
->i_sb
, "ext4_rmdir",
2107 "empty directory has too many links (%d)",
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
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
);
2123 ext4_journal_stop(handle
);
2128 static int ext4_unlink(struct inode
* dir
, struct dentry
*dentry
)
2131 struct inode
* inode
;
2132 struct buffer_head
* bh
;
2133 struct ext4_dir_entry_2
* de
;
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
));
2141 return PTR_ERR(handle
);
2143 if (IS_DIRSYNC(dir
))
2147 bh
= ext4_find_entry (dentry
, &de
);
2151 inode
= dentry
->d_inode
;
2154 if (le32_to_cpu(de
->inode
) != inode
->i_ino
)
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
);
2163 retval
= ext4_delete_entry(handle
, dir
, de
, bh
);
2166 dir
->i_ctime
= dir
->i_mtime
= ext4_current_time(dir
);
2167 ext4_update_dx_flag(dir
);
2168 ext4_mark_inode_dirty(handle
, dir
);
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
);
2177 ext4_journal_stop(handle
);
2182 static int ext4_symlink (struct inode
* dir
,
2183 struct dentry
*dentry
, const char * symname
)
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
;
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
));
2198 return PTR_ERR(handle
);
2200 if (IS_DIRSYNC(dir
))
2203 inode
= ext4_new_inode (handle
, dir
, S_IFLNK
|S_IRWXUGO
);
2204 err
= PTR_ERR(inode
);
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
);
2220 ext4_mark_inode_dirty(handle
, inode
);
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
);
2234 ext4_journal_stop(handle
);
2235 if (err
== -ENOSPC
&& ext4_should_retry_alloc(dir
->i_sb
, &retries
))
2240 static int ext4_link (struct dentry
* old_dentry
,
2241 struct inode
* dir
, struct dentry
*dentry
)
2244 struct inode
*inode
= old_dentry
->d_inode
;
2245 int err
, retries
= 0;
2247 if (EXT4_DIR_LINK_MAX(inode
))
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)
2258 handle
= ext4_journal_start(dir
, EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
) +
2259 EXT4_INDEX_EXTRA_TRANS_BLOCKS
);
2261 return PTR_ERR(handle
);
2263 if (IS_DIRSYNC(dir
))
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
))
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
)
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
;
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);
2303 return PTR_ERR(handle
);
2305 if (IS_DIRSYNC(old_dir
) || IS_DIRSYNC(new_dir
))
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
;
2317 if (!old_bh
|| le32_to_cpu(old_de
->inode
) != old_inode
->i_ino
)
2320 new_inode
= new_dentry
->d_inode
;
2321 new_bh
= ext4_find_entry (new_dentry
, &new_de
);
2328 if (S_ISDIR(old_inode
->i_mode
)) {
2330 retval
= -ENOTEMPTY
;
2331 if (!empty_dir (new_inode
))
2335 dir_bh
= ext4_bread (handle
, old_inode
, 0, 0, &retval
);
2338 if (le32_to_cpu(PARENT_INO(dir_bh
->b_data
)) != old_dir
->i_ino
)
2341 if (!new_inode
&& new_dir
!=old_dir
&&
2342 new_dir
->i_nlink
>= EXT4_LINK_MAX
)
2346 retval
= ext4_add_entry (handle
, new_dentry
, old_inode
);
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
);
2367 * Like most other Unix systems, set the ctime for inodes on a
2370 old_inode
->i_ctime
= ext4_current_time(old_inode
);
2371 ext4_mark_inode_dirty(handle
, old_inode
);
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
);
2390 retval
= ext4_delete_entry(handle
, old_dir
,
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
);
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
);
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
);
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;
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
);
2426 ext4_mark_inode_dirty(handle
, new_inode
);
2427 if (!new_inode
->i_nlink
)
2428 ext4_orphan_add(handle
, new_inode
);
2436 ext4_journal_stop(handle
);
2441 * directories can handle most operations...
2443 const struct inode_operations ext4_dir_inode_operations
= {
2444 .create
= ext4_create
,
2445 .lookup
= ext4_lookup
,
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
,
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
,
2471 .permission
= ext4_permission
,