2 * linux/fs/ext2/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
21 #include <asm/uaccess.h>
23 #include <linux/errno.h>
25 #include <linux/ext2_fs.h>
26 #include <linux/fcntl.h>
27 #include <linux/sched.h>
28 #include <linux/stat.h>
29 #include <linux/string.h>
30 #include <linux/locks.h>
31 #include <linux/quotaops.h>
35 * define how far ahead to read directories while searching them.
37 #define NAMEI_RA_CHUNKS 2
38 #define NAMEI_RA_BLOCKS 4
39 #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
40 #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
43 * NOTE! unlike strncmp, ext2_match returns 1 for success, 0 for failure.
45 * `len <= EXT2_NAME_LEN' is guaranteed by caller.
46 * `de != NULL' is guaranteed by caller.
48 static inline int ext2_match (int len
, const char * const name
,
49 struct ext2_dir_entry_2
* de
)
51 if (len
!= de
->name_len
)
55 return !memcmp(name
, de
->name
, len
);
61 * finds an entry in the specified directory with the wanted name. It
62 * returns the cache buffer in which the entry was found, and the entry
63 * itself (as a parameter - res_dir). It does NOT read the inode of the
64 * entry - you'll have to do that yourself if you want to.
66 static struct buffer_head
* ext2_find_entry (struct inode
* dir
,
67 const char * const name
, int namelen
,
68 struct ext2_dir_entry_2
** res_dir
)
70 struct super_block
* sb
;
71 struct buffer_head
* bh_use
[NAMEI_RA_SIZE
];
72 struct buffer_head
* bh_read
[NAMEI_RA_SIZE
];
74 int block
, toread
, i
, err
;
79 if (namelen
> EXT2_NAME_LEN
)
82 memset (bh_use
, 0, sizeof (bh_use
));
84 for (block
= 0; block
< NAMEI_RA_SIZE
; ++block
) {
85 struct buffer_head
* bh
;
87 if ((block
<< EXT2_BLOCK_SIZE_BITS (sb
)) >= dir
->i_size
)
89 bh
= ext2_getblk (dir
, block
, 0, &err
);
91 if (bh
&& !buffer_uptodate(bh
))
92 bh_read
[toread
++] = bh
;
95 for (block
= 0, offset
= 0; offset
< dir
->i_size
; block
++) {
96 struct buffer_head
* bh
;
97 struct ext2_dir_entry_2
* de
;
100 if ((block
% NAMEI_RA_BLOCKS
) == 0 && toread
) {
101 ll_rw_block (READ
, toread
, bh_read
);
104 bh
= bh_use
[block
% NAMEI_RA_SIZE
];
107 ext2_error (sb
, "ext2_find_entry",
108 "directory #%lu contains a hole at offset %lu",
111 offset
+= sb
->s_blocksize
;
115 if (!buffer_uptodate(bh
)) {
117 * read error: all bets are off
122 de
= (struct ext2_dir_entry_2
*) bh
->b_data
;
123 dlimit
= bh
->b_data
+ sb
->s_blocksize
;
124 while ((char *) de
< dlimit
) {
125 /* this code is executed quadratically often */
126 /* do minimal checking `by hand' */
129 if ((char *) de
+ namelen
<= dlimit
&&
130 ext2_match (namelen
, name
, de
)) {
132 just to be sure, do a full check */
133 if (!ext2_check_dir_entry("ext2_find_entry",
134 dir
, de
, bh
, offset
))
136 for (i
= 0; i
< NAMEI_RA_SIZE
; ++i
) {
143 /* prevent looping on a bad block */
144 de_len
= le16_to_cpu(de
->rec_len
);
148 de
= (struct ext2_dir_entry_2
*)
149 ((char *) de
+ de_len
);
153 if (((block
+ NAMEI_RA_SIZE
) << EXT2_BLOCK_SIZE_BITS (sb
)) >=
157 bh
= ext2_getblk (dir
, block
+ NAMEI_RA_SIZE
, 0, &err
);
158 bh_use
[block
% NAMEI_RA_SIZE
] = bh
;
159 if (bh
&& !buffer_uptodate(bh
))
160 bh_read
[toread
++] = bh
;
164 for (i
= 0; i
< NAMEI_RA_SIZE
; ++i
)
169 struct dentry
*ext2_lookup(struct inode
* dir
, struct dentry
*dentry
)
171 struct inode
* inode
;
172 struct ext2_dir_entry_2
* de
;
173 struct buffer_head
* bh
;
175 if (dentry
->d_name
.len
> EXT2_NAME_LEN
)
176 return ERR_PTR(-ENAMETOOLONG
);
178 bh
= ext2_find_entry (dir
, dentry
->d_name
.name
, dentry
->d_name
.len
, &de
);
181 unsigned long ino
= le32_to_cpu(de
->inode
);
183 inode
= iget(dir
->i_sb
, ino
);
186 return ERR_PTR(-EACCES
);
188 d_add(dentry
, inode
);
195 * adds a file entry to the specified directory, using the same
196 * semantics as ext2_find_entry(). It returns NULL if it failed.
198 * NOTE!! The inode part of 'de' is left at 0 - which means you
199 * may not sleep between calling this and putting something into
200 * the entry, as someone else might have used it while you slept.
202 static struct buffer_head
* ext2_add_entry (struct inode
* dir
,
203 const char * name
, int namelen
,
204 struct ext2_dir_entry_2
** res_dir
,
207 unsigned long offset
;
208 unsigned short rec_len
;
209 struct buffer_head
* bh
;
210 struct ext2_dir_entry_2
* de
, * de1
;
211 struct super_block
* sb
;
215 if (!dir
|| !dir
->i_nlink
)
222 * Is this a busy deleted directory? Can't create new files if so
224 if (dir
->i_size
== 0)
229 bh
= ext2_bread (dir
, 0, 0, err
);
232 rec_len
= EXT2_DIR_REC_LEN(namelen
);
234 de
= (struct ext2_dir_entry_2
*) bh
->b_data
;
237 if ((char *)de
>= sb
->s_blocksize
+ bh
->b_data
) {
240 bh
= ext2_bread (dir
, offset
>> EXT2_BLOCK_SIZE_BITS(sb
), 1, err
);
243 if (dir
->i_size
<= offset
) {
244 if (dir
->i_size
== 0) {
249 ext2_debug ("creating next block\n");
251 de
= (struct ext2_dir_entry_2
*) bh
->b_data
;
253 de
->rec_len
= le16_to_cpu(sb
->s_blocksize
);
254 dir
->i_size
= offset
+ sb
->s_blocksize
;
255 dir
->u
.ext2_i
.i_flags
&= ~EXT2_BTREE_FL
;
256 mark_inode_dirty(dir
);
259 ext2_debug ("skipping to next block\n");
261 de
= (struct ext2_dir_entry_2
*) bh
->b_data
;
264 if (!ext2_check_dir_entry ("ext2_add_entry", dir
, de
, bh
,
270 if (ext2_match (namelen
, name
, de
)) {
275 if ((le32_to_cpu(de
->inode
) == 0 && le16_to_cpu(de
->rec_len
) >= rec_len
) ||
276 (le16_to_cpu(de
->rec_len
) >= EXT2_DIR_REC_LEN(de
->name_len
) + rec_len
)) {
277 offset
+= le16_to_cpu(de
->rec_len
);
278 if (le32_to_cpu(de
->inode
)) {
279 de1
= (struct ext2_dir_entry_2
*) ((char *) de
+
280 EXT2_DIR_REC_LEN(de
->name_len
));
281 de1
->rec_len
= cpu_to_le16(le16_to_cpu(de
->rec_len
) -
282 EXT2_DIR_REC_LEN(de
->name_len
));
283 de
->rec_len
= cpu_to_le16(EXT2_DIR_REC_LEN(de
->name_len
));
287 de
->name_len
= namelen
;
289 memcpy (de
->name
, name
, namelen
);
291 * XXX shouldn't update any times until successful
292 * completion of syscall, but too many callers depend
295 * XXX similarly, too many callers depend on
296 * ext2_new_inode() setting the times, but error
297 * recovery deletes the inode, so the worst that can
298 * happen is that the times are slightly out of date
299 * and/or different from the directory change time.
301 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
302 dir
->u
.ext2_i
.i_flags
&= ~EXT2_BTREE_FL
;
303 mark_inode_dirty(dir
);
304 dir
->i_version
= ++event
;
305 mark_buffer_dirty(bh
, 1);
310 offset
+= le16_to_cpu(de
->rec_len
);
311 de
= (struct ext2_dir_entry_2
*) ((char *) de
+ le16_to_cpu(de
->rec_len
));
318 * ext2_delete_entry deletes a directory entry by merging it with the
321 static int ext2_delete_entry (struct ext2_dir_entry_2
* dir
,
322 struct buffer_head
* bh
)
324 struct ext2_dir_entry_2
* de
, * pde
;
329 de
= (struct ext2_dir_entry_2
*) bh
->b_data
;
330 while (i
< bh
->b_size
) {
331 if (!ext2_check_dir_entry ("ext2_delete_entry", NULL
,
337 cpu_to_le16(le16_to_cpu(pde
->rec_len
) +
338 le16_to_cpu(dir
->rec_len
));
343 i
+= le16_to_cpu(de
->rec_len
);
345 de
= (struct ext2_dir_entry_2
*) ((char *) de
+ le16_to_cpu(de
->rec_len
));
350 static inline void ext2_set_de_type(struct super_block
*sb
,
351 struct ext2_dir_entry_2
*de
,
353 if (!EXT2_HAS_INCOMPAT_FEATURE(sb
, EXT2_FEATURE_INCOMPAT_FILETYPE
))
356 de
->file_type
= EXT2_FT_CHRDEV
;
357 else if (S_ISBLK(mode
))
358 de
->file_type
= EXT2_FT_BLKDEV
;
359 else if (S_ISFIFO(mode
))
360 de
->file_type
= EXT2_FT_FIFO
;
361 else if (S_ISLNK(mode
))
362 de
->file_type
= EXT2_FT_SYMLINK
;
363 else if (S_ISREG(mode
))
364 de
->file_type
= EXT2_FT_REG_FILE
;
365 else if (S_ISDIR(mode
))
366 de
->file_type
= EXT2_FT_DIR
;
370 * By the time this is called, we already have created
371 * the directory cache entry for the new file, but it
372 * is so far negative - it has no inode.
374 * If the create succeeds, we fill in the inode information
375 * with d_instantiate().
377 int ext2_create (struct inode
* dir
, struct dentry
* dentry
, int mode
)
379 struct inode
* inode
;
380 struct buffer_head
* bh
;
381 struct ext2_dir_entry_2
* de
;
385 * N.B. Several error exits in ext2_new_inode don't set err.
387 inode
= ext2_new_inode (dir
, mode
, &err
);
391 inode
->i_op
= &ext2_file_inode_operations
;
392 inode
->i_mode
= mode
;
393 mark_inode_dirty(inode
);
394 bh
= ext2_add_entry (dir
, dentry
->d_name
.name
, dentry
->d_name
.len
, &de
, &err
);
397 mark_inode_dirty(inode
);
401 de
->inode
= cpu_to_le32(inode
->i_ino
);
402 ext2_set_de_type(dir
->i_sb
, de
, S_IFREG
);
403 dir
->i_version
= ++event
;
404 mark_buffer_dirty(bh
, 1);
406 ll_rw_block (WRITE
, 1, &bh
);
410 d_instantiate(dentry
, inode
);
414 int ext2_mknod (struct inode
* dir
, struct dentry
*dentry
, int mode
, int rdev
)
416 struct inode
* inode
;
417 struct buffer_head
* bh
;
418 struct ext2_dir_entry_2
* de
;
421 inode
= ext2_new_inode (dir
, mode
, &err
);
425 inode
->i_uid
= current
->fsuid
;
426 init_special_inode(inode
, mode
, rdev
);
427 bh
= ext2_add_entry (dir
, dentry
->d_name
.name
, dentry
->d_name
.len
, &de
, &err
);
430 de
->inode
= cpu_to_le32(inode
->i_ino
);
431 dir
->i_version
= ++event
;
432 ext2_set_de_type(dir
->i_sb
, de
, inode
->i_mode
);
433 mark_inode_dirty(inode
);
434 mark_buffer_dirty(bh
, 1);
436 ll_rw_block (WRITE
, 1, &bh
);
439 d_instantiate(dentry
, inode
);
447 mark_inode_dirty(inode
);
452 int ext2_mkdir(struct inode
* dir
, struct dentry
* dentry
, int mode
)
454 struct inode
* inode
;
455 struct buffer_head
* bh
, * dir_block
;
456 struct ext2_dir_entry_2
* de
;
460 if (dir
->i_nlink
>= EXT2_LINK_MAX
)
464 inode
= ext2_new_inode (dir
, S_IFDIR
, &err
);
468 inode
->i_op
= &ext2_dir_inode_operations
;
469 inode
->i_size
= inode
->i_sb
->s_blocksize
;
471 dir_block
= ext2_bread (inode
, 0, 1, &err
);
473 inode
->i_nlink
--; /* is this nlink == 0? */
474 mark_inode_dirty(inode
);
478 de
= (struct ext2_dir_entry_2
*) dir_block
->b_data
;
479 de
->inode
= cpu_to_le32(inode
->i_ino
);
481 de
->rec_len
= cpu_to_le16(EXT2_DIR_REC_LEN(de
->name_len
));
482 strcpy (de
->name
, ".");
483 ext2_set_de_type(dir
->i_sb
, de
, S_IFDIR
);
484 de
= (struct ext2_dir_entry_2
*) ((char *) de
+ le16_to_cpu(de
->rec_len
));
485 de
->inode
= cpu_to_le32(dir
->i_ino
);
486 de
->rec_len
= cpu_to_le16(inode
->i_sb
->s_blocksize
- EXT2_DIR_REC_LEN(1));
488 strcpy (de
->name
, "..");
489 ext2_set_de_type(dir
->i_sb
, de
, S_IFDIR
);
491 mark_buffer_dirty(dir_block
, 1);
493 inode
->i_mode
= S_IFDIR
| (mode
& (S_IRWXUGO
|S_ISVTX
) & ~current
->fs
->umask
);
494 if (dir
->i_mode
& S_ISGID
)
495 inode
->i_mode
|= S_ISGID
;
496 mark_inode_dirty(inode
);
497 bh
= ext2_add_entry (dir
, dentry
->d_name
.name
, dentry
->d_name
.len
, &de
, &err
);
500 de
->inode
= cpu_to_le32(inode
->i_ino
);
501 ext2_set_de_type(dir
->i_sb
, de
, S_IFDIR
);
502 dir
->i_version
= ++event
;
503 mark_buffer_dirty(bh
, 1);
505 ll_rw_block (WRITE
, 1, &bh
);
509 dir
->u
.ext2_i
.i_flags
&= ~EXT2_BTREE_FL
;
510 mark_inode_dirty(dir
);
511 d_instantiate(dentry
, inode
);
519 mark_inode_dirty(inode
);
525 * routine to check that the specified directory is empty (for rmdir)
527 static int empty_dir (struct inode
* inode
)
529 unsigned long offset
;
530 struct buffer_head
* bh
;
531 struct ext2_dir_entry_2
* de
, * de1
;
532 struct super_block
* sb
;
536 if (inode
->i_size
< EXT2_DIR_REC_LEN(1) + EXT2_DIR_REC_LEN(2) ||
537 !(bh
= ext2_bread (inode
, 0, 0, &err
))) {
538 ext2_warning (inode
->i_sb
, "empty_dir",
539 "bad directory (dir #%lu) - no data block",
543 de
= (struct ext2_dir_entry_2
*) bh
->b_data
;
544 de1
= (struct ext2_dir_entry_2
*) ((char *) de
+ le16_to_cpu(de
->rec_len
));
545 if (le32_to_cpu(de
->inode
) != inode
->i_ino
|| !le32_to_cpu(de1
->inode
) ||
546 strcmp (".", de
->name
) || strcmp ("..", de1
->name
)) {
547 ext2_warning (inode
->i_sb
, "empty_dir",
548 "bad directory (dir #%lu) - no `.' or `..'",
553 offset
= le16_to_cpu(de
->rec_len
) + le16_to_cpu(de1
->rec_len
);
554 de
= (struct ext2_dir_entry_2
*) ((char *) de1
+ le16_to_cpu(de1
->rec_len
));
555 while (offset
< inode
->i_size
) {
556 if (!bh
|| (void *) de
>= (void *) (bh
->b_data
+ sb
->s_blocksize
)) {
558 bh
= ext2_bread (inode
, offset
>> EXT2_BLOCK_SIZE_BITS(sb
), 1, &err
);
561 ext2_error (sb
, "empty_dir",
562 "directory #%lu contains a hole at offset %lu",
563 inode
->i_ino
, offset
);
565 offset
+= sb
->s_blocksize
;
568 de
= (struct ext2_dir_entry_2
*) bh
->b_data
;
570 if (!ext2_check_dir_entry ("empty_dir", inode
, de
, bh
,
575 if (le32_to_cpu(de
->inode
)) {
579 offset
+= le16_to_cpu(de
->rec_len
);
580 de
= (struct ext2_dir_entry_2
*) ((char *) de
+ le16_to_cpu(de
->rec_len
));
586 int ext2_rmdir (struct inode
* dir
, struct dentry
*dentry
)
589 struct inode
* inode
;
590 struct buffer_head
* bh
;
591 struct ext2_dir_entry_2
* de
;
594 bh
= ext2_find_entry (dir
, dentry
->d_name
.name
, dentry
->d_name
.len
, &de
);
598 inode
= dentry
->d_inode
;
602 if (le32_to_cpu(de
->inode
) != inode
->i_ino
)
606 if (!empty_dir (inode
))
609 retval
= ext2_delete_entry (de
, bh
);
610 dir
->i_version
= ++event
;
613 mark_buffer_dirty(bh
, 1);
615 ll_rw_block (WRITE
, 1, &bh
);
618 if (inode
->i_nlink
!= 2)
619 ext2_warning (inode
->i_sb
, "ext2_rmdir",
620 "empty directory has nlink!=2 (%d)",
622 inode
->i_version
= ++event
;
625 mark_inode_dirty(inode
);
627 inode
->i_ctime
= dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME
;
628 dir
->u
.ext2_i
.i_flags
&= ~EXT2_BTREE_FL
;
629 mark_inode_dirty(dir
);
637 int ext2_unlink(struct inode
* dir
, struct dentry
*dentry
)
640 struct inode
* inode
;
641 struct buffer_head
* bh
;
642 struct ext2_dir_entry_2
* de
;
645 bh
= ext2_find_entry (dir
, dentry
->d_name
.name
, dentry
->d_name
.len
, &de
);
649 inode
= dentry
->d_inode
;
653 if (le32_to_cpu(de
->inode
) != inode
->i_ino
)
656 if (!inode
->i_nlink
) {
657 ext2_warning (inode
->i_sb
, "ext2_unlink",
658 "Deleting nonexistent file (%lu), %d",
659 inode
->i_ino
, inode
->i_nlink
);
662 retval
= ext2_delete_entry (de
, bh
);
665 dir
->i_version
= ++event
;
666 mark_buffer_dirty(bh
, 1);
668 ll_rw_block (WRITE
, 1, &bh
);
671 dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME
;
672 dir
->u
.ext2_i
.i_flags
&= ~EXT2_BTREE_FL
;
673 mark_inode_dirty(dir
);
675 mark_inode_dirty(inode
);
676 inode
->i_ctime
= dir
->i_ctime
;
678 d_delete(dentry
); /* This also frees the inode */
685 int ext2_symlink (struct inode
* dir
, struct dentry
*dentry
, const char * symname
)
687 struct ext2_dir_entry_2
* de
;
688 struct inode
* inode
;
689 struct buffer_head
* bh
= NULL
, * name_block
= NULL
;
691 int i
, l
, err
= -EIO
;
694 if (!(inode
= ext2_new_inode (dir
, S_IFLNK
, &err
))) {
697 inode
->i_mode
= S_IFLNK
| S_IRWXUGO
;
698 inode
->i_op
= &ext2_symlink_inode_operations
;
699 for (l
= 0; l
< inode
->i_sb
->s_blocksize
- 1 &&
702 if (l
>= sizeof (inode
->u
.ext2_i
.i_data
)) {
704 ext2_debug ("l=%d, normal symlink\n", l
);
706 name_block
= ext2_bread (inode
, 0, 1, &err
);
709 mark_inode_dirty(inode
);
713 link
= name_block
->b_data
;
715 link
= (char *) inode
->u
.ext2_i
.i_data
;
717 ext2_debug ("l=%d, fast symlink\n", l
);
721 while (i
< inode
->i_sb
->s_blocksize
- 1 && (c
= *(symname
++)))
725 mark_buffer_dirty(name_block
, 1);
729 mark_inode_dirty(inode
);
731 bh
= ext2_add_entry (dir
, dentry
->d_name
.name
, dentry
->d_name
.len
, &de
, &err
);
734 de
->inode
= cpu_to_le32(inode
->i_ino
);
735 ext2_set_de_type(dir
->i_sb
, de
, S_IFLNK
);
736 dir
->i_version
= ++event
;
737 mark_buffer_dirty(bh
, 1);
739 ll_rw_block (WRITE
, 1, &bh
);
743 d_instantiate(dentry
, inode
);
750 mark_inode_dirty(inode
);
755 int ext2_link (struct dentry
* old_dentry
,
756 struct inode
* dir
, struct dentry
*dentry
)
758 struct inode
*inode
= old_dentry
->d_inode
;
759 struct ext2_dir_entry_2
* de
;
760 struct buffer_head
* bh
;
763 if (S_ISDIR(inode
->i_mode
))
766 if (inode
->i_nlink
>= EXT2_LINK_MAX
)
769 bh
= ext2_add_entry (dir
, dentry
->d_name
.name
, dentry
->d_name
.len
, &de
, &err
);
773 de
->inode
= cpu_to_le32(inode
->i_ino
);
774 ext2_set_de_type(dir
->i_sb
, de
, inode
->i_mode
);
775 dir
->i_version
= ++event
;
776 mark_buffer_dirty(bh
, 1);
778 ll_rw_block (WRITE
, 1, &bh
);
783 inode
->i_ctime
= CURRENT_TIME
;
784 mark_inode_dirty(inode
);
786 d_instantiate(dentry
, inode
);
790 #define PARENT_INO(buffer) \
791 ((struct ext2_dir_entry_2 *) ((char *) buffer + \
792 le16_to_cpu(((struct ext2_dir_entry_2 *) buffer)->rec_len)))->inode
795 * Anybody can rename anything with this: the permission checks are left to the
796 * higher-level routines.
798 int ext2_rename (struct inode
* old_dir
, struct dentry
*old_dentry
,
799 struct inode
* new_dir
,struct dentry
*new_dentry
)
801 struct inode
* old_inode
, * new_inode
;
802 struct buffer_head
* old_bh
, * new_bh
, * dir_bh
;
803 struct ext2_dir_entry_2
* old_de
, * new_de
;
806 old_bh
= new_bh
= dir_bh
= NULL
;
808 old_bh
= ext2_find_entry (old_dir
, old_dentry
->d_name
.name
, old_dentry
->d_name
.len
, &old_de
);
810 * Check for inode number is _not_ due to possible IO errors.
811 * We might rmdir the source, keep it as pwd of some process
812 * and merrily kill the link to whatever was created under the
813 * same name. Goodbye sticky bit ;-<
815 old_inode
= old_dentry
->d_inode
;
817 if (!old_bh
|| le32_to_cpu(old_de
->inode
) != old_inode
->i_ino
)
820 new_inode
= new_dentry
->d_inode
;
821 new_bh
= ext2_find_entry (new_dir
, new_dentry
->d_name
.name
,
822 new_dentry
->d_name
.len
, &new_de
);
828 DQUOT_INIT(new_inode
);
831 if (S_ISDIR(old_inode
->i_mode
)) {
834 if (!empty_dir (new_inode
))
838 dir_bh
= ext2_bread (old_inode
, 0, 0, &retval
);
841 if (le32_to_cpu(PARENT_INO(dir_bh
->b_data
)) != old_dir
->i_ino
)
844 if (!new_inode
&& new_dir
->i_nlink
>= EXT2_LINK_MAX
)
848 new_bh
= ext2_add_entry (new_dir
, new_dentry
->d_name
.name
,
849 new_dentry
->d_name
.len
, &new_de
,
854 new_dir
->i_version
= ++event
;
859 new_de
->inode
= le32_to_cpu(old_inode
->i_ino
);
860 if (EXT2_HAS_INCOMPAT_FEATURE(new_dir
->i_sb
,
861 EXT2_FEATURE_INCOMPAT_FILETYPE
))
862 new_de
->file_type
= old_de
->file_type
;
864 ext2_delete_entry (old_de
, old_bh
);
866 old_dir
->i_version
= ++event
;
868 new_inode
->i_nlink
--;
869 new_inode
->i_ctime
= CURRENT_TIME
;
870 mark_inode_dirty(new_inode
);
872 old_dir
->i_ctime
= old_dir
->i_mtime
= CURRENT_TIME
;
873 old_dir
->u
.ext2_i
.i_flags
&= ~EXT2_BTREE_FL
;
874 mark_inode_dirty(old_dir
);
876 PARENT_INO(dir_bh
->b_data
) = le32_to_cpu(new_dir
->i_ino
);
877 mark_buffer_dirty(dir_bh
, 1);
879 mark_inode_dirty(old_dir
);
881 new_inode
->i_nlink
--;
882 mark_inode_dirty(new_inode
);
885 new_dir
->u
.ext2_i
.i_flags
&= ~EXT2_BTREE_FL
;
886 mark_inode_dirty(new_dir
);
889 mark_buffer_dirty(old_bh
, 1);
890 if (IS_SYNC(old_dir
)) {
891 ll_rw_block (WRITE
, 1, &old_bh
);
892 wait_on_buffer (old_bh
);
894 mark_buffer_dirty(new_bh
, 1);
895 if (IS_SYNC(new_dir
)) {
896 ll_rw_block (WRITE
, 1, &new_bh
);
897 wait_on_buffer (new_bh
);