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
));
351 * By the time this is called, we already have created
352 * the directory cache entry for the new file, but it
353 * is so far negative - it has no inode.
355 * If the create succeeds, we fill in the inode information
356 * with d_instantiate().
358 int ext2_create (struct inode
* dir
, struct dentry
* dentry
, int mode
)
360 struct inode
* inode
;
361 struct buffer_head
* bh
;
362 struct ext2_dir_entry_2
* de
;
366 * N.B. Several error exits in ext2_new_inode don't set err.
368 inode
= ext2_new_inode (dir
, mode
, &err
);
372 inode
->i_op
= &ext2_file_inode_operations
;
373 inode
->i_mode
= mode
;
374 mark_inode_dirty(inode
);
375 bh
= ext2_add_entry (dir
, dentry
->d_name
.name
, dentry
->d_name
.len
, &de
, &err
);
378 mark_inode_dirty(inode
);
382 de
->inode
= cpu_to_le32(inode
->i_ino
);
383 if (EXT2_HAS_INCOMPAT_FEATURE(dir
->i_sb
,
384 EXT2_FEATURE_INCOMPAT_FILETYPE
))
385 de
->file_type
= EXT2_FT_REG_FILE
;
386 dir
->i_version
= ++event
;
387 mark_buffer_dirty(bh
, 1);
389 ll_rw_block (WRITE
, 1, &bh
);
393 d_instantiate(dentry
, inode
);
397 int ext2_mknod (struct inode
* dir
, struct dentry
*dentry
, int mode
, int rdev
)
399 struct inode
* inode
;
400 struct buffer_head
* bh
;
401 struct ext2_dir_entry_2
* de
;
404 inode
= ext2_new_inode (dir
, mode
, &err
);
408 inode
->i_uid
= current
->fsuid
;
409 inode
->i_mode
= mode
;
411 bh
= ext2_add_entry (dir
, dentry
->d_name
.name
, dentry
->d_name
.len
, &de
, &err
);
414 de
->inode
= cpu_to_le32(inode
->i_ino
);
415 dir
->i_version
= ++event
;
416 if (S_ISREG(inode
->i_mode
)) {
417 inode
->i_op
= &ext2_file_inode_operations
;
418 if (EXT2_HAS_INCOMPAT_FEATURE(dir
->i_sb
,
419 EXT2_FEATURE_INCOMPAT_FILETYPE
))
420 de
->file_type
= EXT2_FT_REG_FILE
;
421 } else if (S_ISCHR(inode
->i_mode
)) {
422 inode
->i_op
= &chrdev_inode_operations
;
423 if (EXT2_HAS_INCOMPAT_FEATURE(dir
->i_sb
,
424 EXT2_FEATURE_INCOMPAT_FILETYPE
))
425 de
->file_type
= EXT2_FT_CHRDEV
;
426 } else if (S_ISBLK(inode
->i_mode
)) {
427 inode
->i_op
= &blkdev_inode_operations
;
428 if (EXT2_HAS_INCOMPAT_FEATURE(dir
->i_sb
,
429 EXT2_FEATURE_INCOMPAT_FILETYPE
))
430 de
->file_type
= EXT2_FT_BLKDEV
;
431 } else if (S_ISFIFO(inode
->i_mode
)) {
433 if (EXT2_HAS_INCOMPAT_FEATURE(dir
->i_sb
,
434 EXT2_FEATURE_INCOMPAT_FILETYPE
))
435 de
->file_type
= EXT2_FT_FIFO
;
437 if (S_ISBLK(mode
) || S_ISCHR(mode
))
438 inode
->i_rdev
= to_kdev_t(rdev
);
439 mark_inode_dirty(inode
);
440 mark_buffer_dirty(bh
, 1);
442 ll_rw_block (WRITE
, 1, &bh
);
445 d_instantiate(dentry
, inode
);
453 mark_inode_dirty(inode
);
458 int ext2_mkdir(struct inode
* dir
, struct dentry
* dentry
, int mode
)
460 struct inode
* inode
;
461 struct buffer_head
* bh
, * dir_block
;
462 struct ext2_dir_entry_2
* de
;
466 if (dir
->i_nlink
>= EXT2_LINK_MAX
)
470 inode
= ext2_new_inode (dir
, S_IFDIR
, &err
);
474 inode
->i_op
= &ext2_dir_inode_operations
;
475 inode
->i_size
= inode
->i_sb
->s_blocksize
;
477 dir_block
= ext2_bread (inode
, 0, 1, &err
);
479 inode
->i_nlink
--; /* is this nlink == 0? */
480 mark_inode_dirty(inode
);
484 de
= (struct ext2_dir_entry_2
*) dir_block
->b_data
;
485 de
->inode
= cpu_to_le32(inode
->i_ino
);
487 de
->rec_len
= cpu_to_le16(EXT2_DIR_REC_LEN(de
->name_len
));
488 strcpy (de
->name
, ".");
489 if (EXT2_HAS_INCOMPAT_FEATURE(dir
->i_sb
,
490 EXT2_FEATURE_INCOMPAT_FILETYPE
))
491 de
->file_type
= EXT2_FT_DIR
;
492 de
= (struct ext2_dir_entry_2
*) ((char *) de
+ le16_to_cpu(de
->rec_len
));
493 de
->inode
= cpu_to_le32(dir
->i_ino
);
494 de
->rec_len
= cpu_to_le16(inode
->i_sb
->s_blocksize
- EXT2_DIR_REC_LEN(1));
496 strcpy (de
->name
, "..");
497 if (EXT2_HAS_INCOMPAT_FEATURE(dir
->i_sb
,
498 EXT2_FEATURE_INCOMPAT_FILETYPE
))
499 de
->file_type
= EXT2_FT_DIR
;
501 mark_buffer_dirty(dir_block
, 1);
503 inode
->i_mode
= S_IFDIR
| (mode
& (S_IRWXUGO
|S_ISVTX
) & ~current
->fs
->umask
);
504 if (dir
->i_mode
& S_ISGID
)
505 inode
->i_mode
|= S_ISGID
;
506 mark_inode_dirty(inode
);
507 bh
= ext2_add_entry (dir
, dentry
->d_name
.name
, dentry
->d_name
.len
, &de
, &err
);
510 de
->inode
= cpu_to_le32(inode
->i_ino
);
511 if (EXT2_HAS_INCOMPAT_FEATURE(dir
->i_sb
,
512 EXT2_FEATURE_INCOMPAT_FILETYPE
))
513 de
->file_type
= EXT2_FT_DIR
;
514 dir
->i_version
= ++event
;
515 mark_buffer_dirty(bh
, 1);
517 ll_rw_block (WRITE
, 1, &bh
);
521 dir
->u
.ext2_i
.i_flags
&= ~EXT2_BTREE_FL
;
522 mark_inode_dirty(dir
);
523 d_instantiate(dentry
, inode
);
531 mark_inode_dirty(inode
);
537 * routine to check that the specified directory is empty (for rmdir)
539 static int empty_dir (struct inode
* inode
)
541 unsigned long offset
;
542 struct buffer_head
* bh
;
543 struct ext2_dir_entry_2
* de
, * de1
;
544 struct super_block
* sb
;
548 if (inode
->i_size
< EXT2_DIR_REC_LEN(1) + EXT2_DIR_REC_LEN(2) ||
549 !(bh
= ext2_bread (inode
, 0, 0, &err
))) {
550 ext2_warning (inode
->i_sb
, "empty_dir",
551 "bad directory (dir #%lu) - no data block",
555 de
= (struct ext2_dir_entry_2
*) bh
->b_data
;
556 de1
= (struct ext2_dir_entry_2
*) ((char *) de
+ le16_to_cpu(de
->rec_len
));
557 if (le32_to_cpu(de
->inode
) != inode
->i_ino
|| !le32_to_cpu(de1
->inode
) ||
558 strcmp (".", de
->name
) || strcmp ("..", de1
->name
)) {
559 ext2_warning (inode
->i_sb
, "empty_dir",
560 "bad directory (dir #%lu) - no `.' or `..'",
565 offset
= le16_to_cpu(de
->rec_len
) + le16_to_cpu(de1
->rec_len
);
566 de
= (struct ext2_dir_entry_2
*) ((char *) de1
+ le16_to_cpu(de1
->rec_len
));
567 while (offset
< inode
->i_size
) {
568 if (!bh
|| (void *) de
>= (void *) (bh
->b_data
+ sb
->s_blocksize
)) {
570 bh
= ext2_bread (inode
, offset
>> EXT2_BLOCK_SIZE_BITS(sb
), 1, &err
);
573 ext2_error (sb
, "empty_dir",
574 "directory #%lu contains a hole at offset %lu",
575 inode
->i_ino
, offset
);
577 offset
+= sb
->s_blocksize
;
580 de
= (struct ext2_dir_entry_2
*) bh
->b_data
;
582 if (!ext2_check_dir_entry ("empty_dir", inode
, de
, bh
,
587 if (le32_to_cpu(de
->inode
)) {
591 offset
+= le16_to_cpu(de
->rec_len
);
592 de
= (struct ext2_dir_entry_2
*) ((char *) de
+ le16_to_cpu(de
->rec_len
));
598 int ext2_rmdir (struct inode
* dir
, struct dentry
*dentry
)
601 struct inode
* inode
;
602 struct buffer_head
* bh
;
603 struct ext2_dir_entry_2
* de
;
606 bh
= ext2_find_entry (dir
, dentry
->d_name
.name
, dentry
->d_name
.len
, &de
);
610 inode
= dentry
->d_inode
;
614 if (le32_to_cpu(de
->inode
) != inode
->i_ino
)
618 if (!empty_dir (inode
))
621 retval
= ext2_delete_entry (de
, bh
);
622 dir
->i_version
= ++event
;
625 mark_buffer_dirty(bh
, 1);
627 ll_rw_block (WRITE
, 1, &bh
);
630 if (inode
->i_nlink
!= 2)
631 ext2_warning (inode
->i_sb
, "ext2_rmdir",
632 "empty directory has nlink!=2 (%d)",
634 inode
->i_version
= ++event
;
637 mark_inode_dirty(inode
);
639 inode
->i_ctime
= dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME
;
640 dir
->u
.ext2_i
.i_flags
&= ~EXT2_BTREE_FL
;
641 mark_inode_dirty(dir
);
649 int ext2_unlink(struct inode
* dir
, struct dentry
*dentry
)
652 struct inode
* inode
;
653 struct buffer_head
* bh
;
654 struct ext2_dir_entry_2
* de
;
657 bh
= ext2_find_entry (dir
, dentry
->d_name
.name
, dentry
->d_name
.len
, &de
);
661 inode
= dentry
->d_inode
;
665 if (le32_to_cpu(de
->inode
) != inode
->i_ino
)
668 if (!inode
->i_nlink
) {
669 ext2_warning (inode
->i_sb
, "ext2_unlink",
670 "Deleting nonexistent file (%lu), %d",
671 inode
->i_ino
, inode
->i_nlink
);
674 retval
= ext2_delete_entry (de
, bh
);
677 dir
->i_version
= ++event
;
678 mark_buffer_dirty(bh
, 1);
680 ll_rw_block (WRITE
, 1, &bh
);
683 dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME
;
684 dir
->u
.ext2_i
.i_flags
&= ~EXT2_BTREE_FL
;
685 mark_inode_dirty(dir
);
687 mark_inode_dirty(inode
);
688 inode
->i_ctime
= dir
->i_ctime
;
690 d_delete(dentry
); /* This also frees the inode */
697 int ext2_symlink (struct inode
* dir
, struct dentry
*dentry
, const char * symname
)
699 struct ext2_dir_entry_2
* de
;
700 struct inode
* inode
;
701 struct buffer_head
* bh
= NULL
, * name_block
= NULL
;
703 int i
, l
, err
= -EIO
;
706 if (!(inode
= ext2_new_inode (dir
, S_IFLNK
, &err
))) {
709 inode
->i_mode
= S_IFLNK
| S_IRWXUGO
;
710 inode
->i_op
= &ext2_symlink_inode_operations
;
711 for (l
= 0; l
< inode
->i_sb
->s_blocksize
- 1 &&
714 if (l
>= sizeof (inode
->u
.ext2_i
.i_data
)) {
716 ext2_debug ("l=%d, normal symlink\n", l
);
718 name_block
= ext2_bread (inode
, 0, 1, &err
);
721 mark_inode_dirty(inode
);
725 link
= name_block
->b_data
;
727 link
= (char *) inode
->u
.ext2_i
.i_data
;
729 ext2_debug ("l=%d, fast symlink\n", l
);
733 while (i
< inode
->i_sb
->s_blocksize
- 1 && (c
= *(symname
++)))
737 mark_buffer_dirty(name_block
, 1);
741 mark_inode_dirty(inode
);
743 bh
= ext2_add_entry (dir
, dentry
->d_name
.name
, dentry
->d_name
.len
, &de
, &err
);
746 de
->inode
= cpu_to_le32(inode
->i_ino
);
747 if (EXT2_HAS_INCOMPAT_FEATURE(dir
->i_sb
,
748 EXT2_FEATURE_INCOMPAT_FILETYPE
))
749 de
->file_type
= EXT2_FT_SYMLINK
;
750 dir
->i_version
= ++event
;
751 mark_buffer_dirty(bh
, 1);
753 ll_rw_block (WRITE
, 1, &bh
);
757 d_instantiate(dentry
, inode
);
764 mark_inode_dirty(inode
);
769 int ext2_link (struct dentry
* old_dentry
,
770 struct inode
* dir
, struct dentry
*dentry
)
772 struct inode
*inode
= old_dentry
->d_inode
;
773 struct ext2_dir_entry_2
* de
;
774 struct buffer_head
* bh
;
777 if (S_ISDIR(inode
->i_mode
))
780 if (inode
->i_nlink
>= EXT2_LINK_MAX
)
783 bh
= ext2_add_entry (dir
, dentry
->d_name
.name
, dentry
->d_name
.len
, &de
, &err
);
787 de
->inode
= cpu_to_le32(inode
->i_ino
);
788 if (EXT2_HAS_INCOMPAT_FEATURE(inode
->i_sb
,
789 EXT2_FEATURE_INCOMPAT_FILETYPE
)) {
790 if (S_ISREG(inode
->i_mode
))
791 de
->file_type
= EXT2_FT_REG_FILE
;
792 else if (S_ISDIR(inode
->i_mode
))
793 de
->file_type
= EXT2_FT_DIR
;
794 else if (S_ISLNK(inode
->i_mode
))
795 de
->file_type
= EXT2_FT_SYMLINK
;
796 else if (S_ISCHR(inode
->i_mode
))
797 de
->file_type
= EXT2_FT_CHRDEV
;
798 else if (S_ISBLK(inode
->i_mode
))
799 de
->file_type
= EXT2_FT_BLKDEV
;
800 else if (S_ISFIFO(inode
->i_mode
))
801 de
->file_type
= EXT2_FT_FIFO
;
803 dir
->i_version
= ++event
;
804 mark_buffer_dirty(bh
, 1);
806 ll_rw_block (WRITE
, 1, &bh
);
811 inode
->i_ctime
= CURRENT_TIME
;
812 mark_inode_dirty(inode
);
814 d_instantiate(dentry
, inode
);
818 #define PARENT_INO(buffer) \
819 ((struct ext2_dir_entry_2 *) ((char *) buffer + \
820 le16_to_cpu(((struct ext2_dir_entry_2 *) buffer)->rec_len)))->inode
823 * Anybody can rename anything with this: the permission checks are left to the
824 * higher-level routines.
826 int ext2_rename (struct inode
* old_dir
, struct dentry
*old_dentry
,
827 struct inode
* new_dir
,struct dentry
*new_dentry
)
829 struct inode
* old_inode
, * new_inode
;
830 struct buffer_head
* old_bh
, * new_bh
, * dir_bh
;
831 struct ext2_dir_entry_2
* old_de
, * new_de
;
834 old_bh
= new_bh
= dir_bh
= NULL
;
836 old_bh
= ext2_find_entry (old_dir
, old_dentry
->d_name
.name
, old_dentry
->d_name
.len
, &old_de
);
838 * Check for inode number is _not_ due to possible IO errors.
839 * We might rmdir the source, keep it as pwd of some process
840 * and merrily kill the link to whatever was created under the
841 * same name. Goodbye sticky bit ;-<
843 old_inode
= old_dentry
->d_inode
;
845 if (!old_bh
|| le32_to_cpu(old_de
->inode
) != old_inode
->i_ino
)
848 new_inode
= new_dentry
->d_inode
;
849 new_bh
= ext2_find_entry (new_dir
, new_dentry
->d_name
.name
,
850 new_dentry
->d_name
.len
, &new_de
);
856 DQUOT_INIT(new_inode
);
859 if (S_ISDIR(old_inode
->i_mode
)) {
862 if (!empty_dir (new_inode
))
866 dir_bh
= ext2_bread (old_inode
, 0, 0, &retval
);
869 if (le32_to_cpu(PARENT_INO(dir_bh
->b_data
)) != old_dir
->i_ino
)
872 if (!new_inode
&& new_dir
->i_nlink
>= EXT2_LINK_MAX
)
876 new_bh
= ext2_add_entry (new_dir
, new_dentry
->d_name
.name
,
877 new_dentry
->d_name
.len
, &new_de
,
882 new_dir
->i_version
= ++event
;
887 new_de
->inode
= le32_to_cpu(old_inode
->i_ino
);
888 if (EXT2_HAS_INCOMPAT_FEATURE(new_dir
->i_sb
,
889 EXT2_FEATURE_INCOMPAT_FILETYPE
))
890 new_de
->file_type
= old_de
->file_type
;
892 ext2_delete_entry (old_de
, old_bh
);
894 old_dir
->i_version
= ++event
;
896 new_inode
->i_nlink
--;
897 new_inode
->i_ctime
= CURRENT_TIME
;
898 mark_inode_dirty(new_inode
);
900 old_dir
->i_ctime
= old_dir
->i_mtime
= CURRENT_TIME
;
901 old_dir
->u
.ext2_i
.i_flags
&= ~EXT2_BTREE_FL
;
902 mark_inode_dirty(old_dir
);
904 PARENT_INO(dir_bh
->b_data
) = le32_to_cpu(new_dir
->i_ino
);
905 mark_buffer_dirty(dir_bh
, 1);
907 mark_inode_dirty(old_dir
);
909 new_inode
->i_nlink
--;
910 mark_inode_dirty(new_inode
);
913 new_dir
->u
.ext2_i
.i_flags
&= ~EXT2_BTREE_FL
;
914 mark_inode_dirty(new_dir
);
917 mark_buffer_dirty(old_bh
, 1);
918 if (IS_SYNC(old_dir
)) {
919 ll_rw_block (WRITE
, 1, &old_bh
);
920 wait_on_buffer (old_bh
);
922 mark_buffer_dirty(new_bh
, 1);
923 if (IS_SYNC(new_dir
)) {
924 ll_rw_block (WRITE
, 1, &new_bh
);
925 wait_on_buffer (new_bh
);