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
;
81 if (namelen
> EXT2_NAME_LEN
)
84 memset (bh_use
, 0, sizeof (bh_use
));
86 for (block
= 0; block
< NAMEI_RA_SIZE
; ++block
) {
87 struct buffer_head
* bh
;
89 if ((block
<< EXT2_BLOCK_SIZE_BITS (sb
)) >= dir
->i_size
)
91 bh
= ext2_getblk (dir
, block
, 0, &err
);
93 if (bh
&& !buffer_uptodate(bh
))
94 bh_read
[toread
++] = bh
;
97 for (block
= 0, offset
= 0; offset
< dir
->i_size
; block
++) {
98 struct buffer_head
* bh
;
99 struct ext2_dir_entry_2
* de
;
102 if ((block
% NAMEI_RA_BLOCKS
) == 0 && toread
) {
103 ll_rw_block (READ
, toread
, bh_read
);
106 bh
= bh_use
[block
% NAMEI_RA_SIZE
];
109 ext2_error (sb
, "ext2_find_entry",
110 "directory #%lu contains a hole at offset %lu",
113 offset
+= sb
->s_blocksize
;
117 if (!buffer_uptodate(bh
)) {
119 * read error: all bets are off
124 de
= (struct ext2_dir_entry_2
*) bh
->b_data
;
125 dlimit
= bh
->b_data
+ sb
->s_blocksize
;
126 while ((char *) de
< dlimit
) {
127 /* this code is executed quadratically often */
128 /* do minimal checking `by hand' */
131 if ((char *) de
+ namelen
<= dlimit
&&
132 ext2_match (namelen
, name
, de
)) {
134 just to be sure, do a full check */
135 if (!ext2_check_dir_entry("ext2_find_entry",
136 dir
, de
, bh
, offset
))
138 for (i
= 0; i
< NAMEI_RA_SIZE
; ++i
) {
145 /* prevent looping on a bad block */
146 de_len
= le16_to_cpu(de
->rec_len
);
150 de
= (struct ext2_dir_entry_2
*)
151 ((char *) de
+ de_len
);
155 if (((block
+ NAMEI_RA_SIZE
) << EXT2_BLOCK_SIZE_BITS (sb
)) >=
159 bh
= ext2_getblk (dir
, block
+ NAMEI_RA_SIZE
, 0, &err
);
160 bh_use
[block
% NAMEI_RA_SIZE
] = bh
;
161 if (bh
&& !buffer_uptodate(bh
))
162 bh_read
[toread
++] = bh
;
166 for (i
= 0; i
< NAMEI_RA_SIZE
; ++i
)
171 int ext2_lookup(struct inode
* dir
, struct dentry
*dentry
)
173 struct inode
* inode
;
174 struct ext2_dir_entry_2
* de
;
175 struct buffer_head
* bh
;
177 if (dentry
->d_name
.len
> EXT2_NAME_LEN
)
178 return -ENAMETOOLONG
;
180 bh
= ext2_find_entry (dir
, dentry
->d_name
.name
, dentry
->d_name
.len
, &de
);
183 unsigned long ino
= le32_to_cpu(de
->inode
);
185 inode
= iget(dir
->i_sb
, ino
);
190 d_add(dentry
, inode
);
197 * adds a file entry to the specified directory, using the same
198 * semantics as ext2_find_entry(). It returns NULL if it failed.
200 * NOTE!! The inode part of 'de' is left at 0 - which means you
201 * may not sleep between calling this and putting something into
202 * the entry, as someone else might have used it while you slept.
204 static struct buffer_head
* ext2_add_entry (struct inode
* dir
,
205 const char * name
, int namelen
,
206 struct ext2_dir_entry_2
** res_dir
,
209 unsigned long offset
;
210 unsigned short rec_len
;
211 struct buffer_head
* bh
;
212 struct ext2_dir_entry_2
* de
, * de1
;
213 struct super_block
* sb
;
217 if (!dir
|| !dir
->i_nlink
)
221 if (namelen
> EXT2_NAME_LEN
)
223 *err
= -ENAMETOOLONG
;
230 * Is this a busy deleted directory? Can't create new files if so
232 if (dir
->i_size
== 0)
237 bh
= ext2_bread (dir
, 0, 0, err
);
240 rec_len
= EXT2_DIR_REC_LEN(namelen
);
242 de
= (struct ext2_dir_entry_2
*) bh
->b_data
;
245 if ((char *)de
>= sb
->s_blocksize
+ bh
->b_data
) {
248 bh
= ext2_bread (dir
, offset
>> EXT2_BLOCK_SIZE_BITS(sb
), 1, err
);
251 if (dir
->i_size
<= offset
) {
252 if (dir
->i_size
== 0) {
257 ext2_debug ("creating next block\n");
259 de
= (struct ext2_dir_entry_2
*) bh
->b_data
;
260 de
->inode
= le32_to_cpu(0);
261 de
->rec_len
= le16_to_cpu(sb
->s_blocksize
);
262 dir
->i_size
= offset
+ sb
->s_blocksize
;
263 dir
->u
.ext2_i
.i_flags
&= ~EXT2_BTREE_FL
;
264 mark_inode_dirty(dir
);
267 ext2_debug ("skipping to next block\n");
269 de
= (struct ext2_dir_entry_2
*) bh
->b_data
;
272 if (!ext2_check_dir_entry ("ext2_add_entry", dir
, de
, bh
,
278 if (ext2_match (namelen
, name
, de
)) {
283 if ((le32_to_cpu(de
->inode
) == 0 && le16_to_cpu(de
->rec_len
) >= rec_len
) ||
284 (le16_to_cpu(de
->rec_len
) >= EXT2_DIR_REC_LEN(de
->name_len
) + rec_len
)) {
285 offset
+= le16_to_cpu(de
->rec_len
);
286 if (le32_to_cpu(de
->inode
)) {
287 de1
= (struct ext2_dir_entry_2
*) ((char *) de
+
288 EXT2_DIR_REC_LEN(de
->name_len
));
289 de1
->rec_len
= cpu_to_le16(le16_to_cpu(de
->rec_len
) -
290 EXT2_DIR_REC_LEN(de
->name_len
));
291 de
->rec_len
= cpu_to_le16(EXT2_DIR_REC_LEN(de
->name_len
));
294 de
->inode
= cpu_to_le32(0);
295 de
->name_len
= namelen
;
297 memcpy (de
->name
, name
, namelen
);
299 * XXX shouldn't update any times until successful
300 * completion of syscall, but too many callers depend
303 * XXX similarly, too many callers depend on
304 * ext2_new_inode() setting the times, but error
305 * recovery deletes the inode, so the worst that can
306 * happen is that the times are slightly out of date
307 * and/or different from the directory change time.
309 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
310 dir
->u
.ext2_i
.i_flags
&= ~EXT2_BTREE_FL
;
311 mark_inode_dirty(dir
);
312 dir
->i_version
= ++event
;
313 mark_buffer_dirty(bh
, 1);
318 offset
+= le16_to_cpu(de
->rec_len
);
319 de
= (struct ext2_dir_entry_2
*) ((char *) de
+ le16_to_cpu(de
->rec_len
));
326 * ext2_delete_entry deletes a directory entry by merging it with the
329 static int ext2_delete_entry (struct ext2_dir_entry_2
* dir
,
330 struct buffer_head
* bh
)
332 struct ext2_dir_entry_2
* de
, * pde
;
337 de
= (struct ext2_dir_entry_2
*) bh
->b_data
;
338 while (i
< bh
->b_size
) {
339 if (!ext2_check_dir_entry ("ext2_delete_entry", NULL
,
345 cpu_to_le16(le16_to_cpu(pde
->rec_len
) +
346 le16_to_cpu(dir
->rec_len
));
347 dir
->inode
= le32_to_cpu(0);
350 i
+= le16_to_cpu(de
->rec_len
);
352 de
= (struct ext2_dir_entry_2
*) ((char *) de
+ le16_to_cpu(de
->rec_len
));
358 * By the time this is called, we already have created
359 * the directory cache entry for the new file, but it
360 * is so far negative - it has no inode.
362 * If the create succeeds, we fill in the inode information
363 * with d_instantiate().
365 int ext2_create (struct inode
* dir
, struct dentry
* dentry
, int mode
)
367 struct inode
* inode
;
368 struct buffer_head
* bh
;
369 struct ext2_dir_entry_2
* de
;
373 * N.B. Several error exits in ext2_new_inode don't set err.
375 inode
= ext2_new_inode (dir
, mode
, &err
);
379 inode
->i_op
= &ext2_file_inode_operations
;
380 inode
->i_mode
= mode
;
381 mark_inode_dirty(inode
);
382 bh
= ext2_add_entry (dir
, dentry
->d_name
.name
, dentry
->d_name
.len
, &de
, &err
);
385 mark_inode_dirty(inode
);
389 de
->inode
= cpu_to_le32(inode
->i_ino
);
390 if (EXT2_HAS_INCOMPAT_FEATURE(dir
->i_sb
,
391 EXT2_FEATURE_INCOMPAT_FILETYPE
))
392 de
->file_type
= EXT2_FT_REG_FILE
;
393 dir
->i_version
= ++event
;
394 mark_buffer_dirty(bh
, 1);
396 ll_rw_block (WRITE
, 1, &bh
);
400 d_instantiate(dentry
, inode
);
404 int ext2_mknod (struct inode
* dir
, struct dentry
*dentry
, int mode
, int rdev
)
406 struct inode
* inode
;
407 struct buffer_head
* bh
;
408 struct ext2_dir_entry_2
* de
;
412 if (dentry
->d_name
.len
> EXT2_NAME_LEN
)
415 inode
= ext2_new_inode (dir
, mode
, &err
);
419 inode
->i_uid
= current
->fsuid
;
420 inode
->i_mode
= mode
;
422 bh
= ext2_add_entry (dir
, dentry
->d_name
.name
, dentry
->d_name
.len
, &de
, &err
);
425 de
->inode
= cpu_to_le32(inode
->i_ino
);
426 dir
->i_version
= ++event
;
427 if (S_ISREG(inode
->i_mode
)) {
428 inode
->i_op
= &ext2_file_inode_operations
;
429 if (EXT2_HAS_INCOMPAT_FEATURE(dir
->i_sb
,
430 EXT2_FEATURE_INCOMPAT_FILETYPE
))
431 de
->file_type
= EXT2_FT_REG_FILE
;
432 } else if (S_ISDIR(inode
->i_mode
)) {
433 inode
->i_op
= &ext2_dir_inode_operations
;
434 if (dir
->i_mode
& S_ISGID
)
435 inode
->i_mode
|= S_ISGID
;
436 if (EXT2_HAS_INCOMPAT_FEATURE(dir
->i_sb
,
437 EXT2_FEATURE_INCOMPAT_FILETYPE
))
438 de
->file_type
= EXT2_FT_DIR
;
440 else if (S_ISLNK(inode
->i_mode
)) {
441 inode
->i_op
= &ext2_symlink_inode_operations
;
442 if (EXT2_HAS_INCOMPAT_FEATURE(dir
->i_sb
,
443 EXT2_FEATURE_INCOMPAT_FILETYPE
))
444 de
->file_type
= EXT2_FT_SYMLINK
;
445 } else if (S_ISCHR(inode
->i_mode
)) {
446 inode
->i_op
= &chrdev_inode_operations
;
447 if (EXT2_HAS_INCOMPAT_FEATURE(dir
->i_sb
,
448 EXT2_FEATURE_INCOMPAT_FILETYPE
))
449 de
->file_type
= EXT2_FT_CHRDEV
;
450 } else if (S_ISBLK(inode
->i_mode
)) {
451 inode
->i_op
= &blkdev_inode_operations
;
452 if (EXT2_HAS_INCOMPAT_FEATURE(dir
->i_sb
,
453 EXT2_FEATURE_INCOMPAT_FILETYPE
))
454 de
->file_type
= EXT2_FT_BLKDEV
;
455 } else if (S_ISFIFO(inode
->i_mode
)) {
457 if (EXT2_HAS_INCOMPAT_FEATURE(dir
->i_sb
,
458 EXT2_FEATURE_INCOMPAT_FILETYPE
))
459 de
->file_type
= EXT2_FT_FIFO
;
461 if (S_ISBLK(mode
) || S_ISCHR(mode
))
462 inode
->i_rdev
= to_kdev_t(rdev
);
463 mark_inode_dirty(inode
);
464 mark_buffer_dirty(bh
, 1);
466 ll_rw_block (WRITE
, 1, &bh
);
469 d_instantiate(dentry
, inode
);
477 mark_inode_dirty(inode
);
482 int ext2_mkdir(struct inode
* dir
, struct dentry
* dentry
, int mode
)
484 struct inode
* inode
;
485 struct buffer_head
* bh
, * dir_block
;
486 struct ext2_dir_entry_2
* de
;
490 if (dentry
->d_name
.len
> EXT2_NAME_LEN
)
494 if (dir
->i_nlink
>= EXT2_LINK_MAX
)
498 inode
= ext2_new_inode (dir
, S_IFDIR
, &err
);
502 inode
->i_op
= &ext2_dir_inode_operations
;
503 inode
->i_size
= inode
->i_sb
->s_blocksize
;
505 dir_block
= ext2_bread (inode
, 0, 1, &err
);
507 inode
->i_nlink
--; /* is this nlink == 0? */
508 mark_inode_dirty(inode
);
512 de
= (struct ext2_dir_entry_2
*) dir_block
->b_data
;
513 de
->inode
= cpu_to_le32(inode
->i_ino
);
515 de
->rec_len
= cpu_to_le16(EXT2_DIR_REC_LEN(de
->name_len
));
516 strcpy (de
->name
, ".");
517 if (EXT2_HAS_INCOMPAT_FEATURE(dir
->i_sb
,
518 EXT2_FEATURE_INCOMPAT_FILETYPE
))
519 de
->file_type
= EXT2_FT_DIR
;
520 de
= (struct ext2_dir_entry_2
*) ((char *) de
+ le16_to_cpu(de
->rec_len
));
521 de
->inode
= cpu_to_le32(dir
->i_ino
);
522 de
->rec_len
= cpu_to_le16(inode
->i_sb
->s_blocksize
- EXT2_DIR_REC_LEN(1));
524 strcpy (de
->name
, "..");
525 if (EXT2_HAS_INCOMPAT_FEATURE(dir
->i_sb
,
526 EXT2_FEATURE_INCOMPAT_FILETYPE
))
527 de
->file_type
= EXT2_FT_DIR
;
529 mark_buffer_dirty(dir_block
, 1);
531 inode
->i_mode
= S_IFDIR
| (mode
& (S_IRWXUGO
|S_ISVTX
) & ~current
->fs
->umask
);
532 if (dir
->i_mode
& S_ISGID
)
533 inode
->i_mode
|= S_ISGID
;
534 mark_inode_dirty(inode
);
535 bh
= ext2_add_entry (dir
, dentry
->d_name
.name
, dentry
->d_name
.len
, &de
, &err
);
538 de
->inode
= cpu_to_le32(inode
->i_ino
);
539 if (EXT2_HAS_INCOMPAT_FEATURE(dir
->i_sb
,
540 EXT2_FEATURE_INCOMPAT_FILETYPE
))
541 de
->file_type
= EXT2_FT_DIR
;
542 dir
->i_version
= ++event
;
543 mark_buffer_dirty(bh
, 1);
545 ll_rw_block (WRITE
, 1, &bh
);
549 dir
->u
.ext2_i
.i_flags
&= ~EXT2_BTREE_FL
;
550 mark_inode_dirty(dir
);
551 d_instantiate(dentry
, inode
);
559 mark_inode_dirty(inode
);
565 * routine to check that the specified directory is empty (for rmdir)
567 static int empty_dir (struct inode
* inode
)
569 unsigned long offset
;
570 struct buffer_head
* bh
;
571 struct ext2_dir_entry_2
* de
, * de1
;
572 struct super_block
* sb
;
576 if (inode
->i_size
< EXT2_DIR_REC_LEN(1) + EXT2_DIR_REC_LEN(2) ||
577 !(bh
= ext2_bread (inode
, 0, 0, &err
))) {
578 ext2_warning (inode
->i_sb
, "empty_dir",
579 "bad directory (dir #%lu) - no data block",
583 de
= (struct ext2_dir_entry_2
*) bh
->b_data
;
584 de1
= (struct ext2_dir_entry_2
*) ((char *) de
+ le16_to_cpu(de
->rec_len
));
585 if (le32_to_cpu(de
->inode
) != inode
->i_ino
|| !le32_to_cpu(de1
->inode
) ||
586 strcmp (".", de
->name
) || strcmp ("..", de1
->name
)) {
587 ext2_warning (inode
->i_sb
, "empty_dir",
588 "bad directory (dir #%lu) - no `.' or `..'",
593 offset
= le16_to_cpu(de
->rec_len
) + le16_to_cpu(de1
->rec_len
);
594 de
= (struct ext2_dir_entry_2
*) ((char *) de1
+ le16_to_cpu(de1
->rec_len
));
595 while (offset
< inode
->i_size
) {
596 if (!bh
|| (void *) de
>= (void *) (bh
->b_data
+ sb
->s_blocksize
)) {
598 bh
= ext2_bread (inode
, offset
>> EXT2_BLOCK_SIZE_BITS(sb
), 1, &err
);
601 ext2_error (sb
, "empty_dir",
602 "directory #%lu contains a hole at offset %lu",
603 inode
->i_ino
, offset
);
605 offset
+= sb
->s_blocksize
;
608 de
= (struct ext2_dir_entry_2
*) bh
->b_data
;
610 if (!ext2_check_dir_entry ("empty_dir", inode
, de
, bh
,
615 if (le32_to_cpu(de
->inode
)) {
619 offset
+= le16_to_cpu(de
->rec_len
);
620 de
= (struct ext2_dir_entry_2
*) ((char *) de
+ le16_to_cpu(de
->rec_len
));
626 int ext2_rmdir (struct inode
* dir
, struct dentry
*dentry
)
629 struct inode
* inode
;
630 struct buffer_head
* bh
;
631 struct ext2_dir_entry_2
* de
;
633 retval
= -ENAMETOOLONG
;
634 if (dentry
->d_name
.len
> EXT2_NAME_LEN
)
638 bh
= ext2_find_entry (dir
, dentry
->d_name
.name
, dentry
->d_name
.len
, &de
);
642 inode
= dentry
->d_inode
;
646 if ((dir
->i_mode
& S_ISVTX
) &&
647 current
->fsuid
!= inode
->i_uid
&&
648 current
->fsuid
!= dir
->i_uid
&& !capable(CAP_FOWNER
))
650 if (inode
== dir
) /* we may not delete ".", but "../dir" is ok */
654 if (!S_ISDIR(inode
->i_mode
))
658 if (inode
->i_dev
!= dir
->i_dev
)
660 if (le32_to_cpu(de
->inode
) != inode
->i_ino
)
665 * Prune any child dentries so that this dentry becomes negative.
667 if (dentry
->d_count
> 1)
668 shrink_dcache_parent(dentry
);
670 if (!empty_dir (inode
))
672 else if (le32_to_cpu(de
->inode
) != inode
->i_ino
)
675 if (dentry
->d_count
> 1) {
677 * Are we deleting the last instance of a busy directory?
678 * Better clean up if so.
680 * Make directory empty (it will be truncated when finally
681 * dereferenced). This also inhibits ext2_add_entry.
685 retval
= ext2_delete_entry (de
, bh
);
686 dir
->i_version
= ++event
;
691 mark_buffer_dirty(bh
, 1);
693 ll_rw_block (WRITE
, 1, &bh
);
696 if (inode
->i_nlink
!= 2)
697 ext2_warning (inode
->i_sb
, "ext2_rmdir",
698 "empty directory has nlink!=2 (%d)",
700 inode
->i_version
= ++event
;
702 mark_inode_dirty(inode
);
704 inode
->i_ctime
= dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME
;
705 dir
->u
.ext2_i
.i_flags
&= ~EXT2_BTREE_FL
;
706 mark_inode_dirty(dir
);
715 int ext2_unlink(struct inode
* dir
, struct dentry
*dentry
)
718 struct inode
* inode
;
719 struct buffer_head
* bh
;
720 struct ext2_dir_entry_2
* de
;
722 retval
= -ENAMETOOLONG
;
723 if (dentry
->d_name
.len
> EXT2_NAME_LEN
)
727 bh
= ext2_find_entry (dir
, dentry
->d_name
.name
, dentry
->d_name
.len
, &de
);
731 inode
= dentry
->d_inode
;
735 if (S_ISDIR(inode
->i_mode
))
737 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
739 if ((dir
->i_mode
& S_ISVTX
) &&
740 current
->fsuid
!= inode
->i_uid
&&
741 current
->fsuid
!= dir
->i_uid
&& !capable(CAP_FOWNER
))
745 if (le32_to_cpu(de
->inode
) != inode
->i_ino
)
748 if (!inode
->i_nlink
) {
749 ext2_warning (inode
->i_sb
, "ext2_unlink",
750 "Deleting nonexistent file (%lu), %d",
751 inode
->i_ino
, inode
->i_nlink
);
754 retval
= ext2_delete_entry (de
, bh
);
757 dir
->i_version
= ++event
;
758 mark_buffer_dirty(bh
, 1);
760 ll_rw_block (WRITE
, 1, &bh
);
763 dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME
;
764 dir
->u
.ext2_i
.i_flags
&= ~EXT2_BTREE_FL
;
765 mark_inode_dirty(dir
);
767 mark_inode_dirty(inode
);
768 inode
->i_ctime
= dir
->i_ctime
;
770 d_delete(dentry
); /* This also frees the inode */
778 int ext2_symlink (struct inode
* dir
, struct dentry
*dentry
, const char * symname
)
780 struct ext2_dir_entry_2
* de
;
781 struct inode
* inode
;
782 struct buffer_head
* bh
= NULL
, * name_block
= NULL
;
784 int i
, l
, err
= -EIO
;
787 if (!(inode
= ext2_new_inode (dir
, S_IFLNK
, &err
))) {
790 inode
->i_mode
= S_IFLNK
| S_IRWXUGO
;
791 inode
->i_op
= &ext2_symlink_inode_operations
;
792 for (l
= 0; l
< inode
->i_sb
->s_blocksize
- 1 &&
795 if (l
>= sizeof (inode
->u
.ext2_i
.i_data
)) {
797 ext2_debug ("l=%d, normal symlink\n", l
);
799 name_block
= ext2_bread (inode
, 0, 1, &err
);
802 mark_inode_dirty(inode
);
806 link
= name_block
->b_data
;
808 link
= (char *) inode
->u
.ext2_i
.i_data
;
810 ext2_debug ("l=%d, fast symlink\n", l
);
814 while (i
< inode
->i_sb
->s_blocksize
- 1 && (c
= *(symname
++)))
818 mark_buffer_dirty(name_block
, 1);
822 mark_inode_dirty(inode
);
824 bh
= ext2_add_entry (dir
, dentry
->d_name
.name
, dentry
->d_name
.len
, &de
, &err
);
827 de
->inode
= cpu_to_le32(inode
->i_ino
);
828 if (EXT2_HAS_INCOMPAT_FEATURE(dir
->i_sb
,
829 EXT2_FEATURE_INCOMPAT_FILETYPE
))
830 de
->file_type
= EXT2_FT_SYMLINK
;
831 dir
->i_version
= ++event
;
832 mark_buffer_dirty(bh
, 1);
834 ll_rw_block (WRITE
, 1, &bh
);
838 d_instantiate(dentry
, inode
);
845 mark_inode_dirty(inode
);
850 int ext2_link (struct dentry
* old_dentry
,
851 struct inode
* dir
, struct dentry
*dentry
)
853 struct inode
*inode
= old_dentry
->d_inode
;
854 struct ext2_dir_entry_2
* de
;
855 struct buffer_head
* bh
;
858 if (S_ISDIR(inode
->i_mode
))
861 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
864 if (inode
->i_nlink
>= EXT2_LINK_MAX
)
867 bh
= ext2_add_entry (dir
, dentry
->d_name
.name
, dentry
->d_name
.len
, &de
, &err
);
871 de
->inode
= cpu_to_le32(inode
->i_ino
);
872 if (EXT2_HAS_INCOMPAT_FEATURE(inode
->i_sb
,
873 EXT2_FEATURE_INCOMPAT_FILETYPE
)) {
874 if (S_ISREG(inode
->i_mode
))
875 de
->file_type
= EXT2_FT_REG_FILE
;
876 else if (S_ISDIR(inode
->i_mode
))
877 de
->file_type
= EXT2_FT_DIR
;
878 else if (S_ISLNK(inode
->i_mode
))
879 de
->file_type
= EXT2_FT_SYMLINK
;
880 else if (S_ISCHR(inode
->i_mode
))
881 de
->file_type
= EXT2_FT_CHRDEV
;
882 else if (S_ISBLK(inode
->i_mode
))
883 de
->file_type
= EXT2_FT_BLKDEV
;
884 else if (S_ISFIFO(inode
->i_mode
))
885 de
->file_type
= EXT2_FT_FIFO
;
887 dir
->i_version
= ++event
;
888 mark_buffer_dirty(bh
, 1);
890 ll_rw_block (WRITE
, 1, &bh
);
895 inode
->i_ctime
= CURRENT_TIME
;
896 mark_inode_dirty(inode
);
898 d_instantiate(dentry
, inode
);
902 #define PARENT_INO(buffer) \
903 ((struct ext2_dir_entry_2 *) ((char *) buffer + \
904 le16_to_cpu(((struct ext2_dir_entry_2 *) buffer)->rec_len)))->inode
907 * rename uses retrying to avoid race-conditions: at least they should be
909 * it tries to allocate all the blocks, then sanity-checks, and if the sanity-
910 * checks fail, it tries to restart itself again. Very practical - no changes
911 * are done until we know everything works ok.. and then all the changes can be
912 * done in one fell swoop when we have claimed all the buffers needed.
914 * Anybody can rename anything with this: the permission checks are left to the
915 * higher-level routines.
917 static int do_ext2_rename (struct inode
* old_dir
, struct dentry
*old_dentry
,
918 struct inode
* new_dir
,struct dentry
*new_dentry
)
920 struct inode
* old_inode
, * new_inode
;
921 struct buffer_head
* old_bh
, * new_bh
, * dir_bh
;
922 struct ext2_dir_entry_2
* old_de
, * new_de
;
925 old_bh
= new_bh
= dir_bh
= NULL
;
926 retval
= -ENAMETOOLONG
;
927 if (old_dentry
->d_name
.len
> EXT2_NAME_LEN
)
930 old_bh
= ext2_find_entry (old_dir
, old_dentry
->d_name
.name
, old_dentry
->d_name
.len
, &old_de
);
934 old_inode
= old_dentry
->d_inode
;
937 if ((old_dir
->i_mode
& S_ISVTX
) &&
938 current
->fsuid
!= old_inode
->i_uid
&&
939 current
->fsuid
!= old_dir
->i_uid
&& !capable(CAP_FOWNER
))
941 if (IS_APPEND(old_inode
) || IS_IMMUTABLE(old_inode
))
944 new_inode
= new_dentry
->d_inode
;
945 new_bh
= ext2_find_entry (new_dir
, new_dentry
->d_name
.name
,
946 new_dentry
->d_name
.len
, &new_de
);
952 DQUOT_INIT(new_inode
);
956 if (new_inode
== old_inode
)
958 if (new_inode
&& S_ISDIR(new_inode
->i_mode
)) {
960 if (!S_ISDIR(old_inode
->i_mode
))
963 if (is_subdir(new_dentry
, old_dentry
))
965 /* Prune any children before testing for busy */
966 if (new_dentry
->d_count
> 1)
967 shrink_dcache_parent(new_dentry
);
969 if (!empty_dir (new_inode
))
972 if (new_dentry
->d_count
> 1)
977 if ((new_dir
->i_mode
& S_ISVTX
) &&
978 current
->fsuid
!= new_inode
->i_uid
&&
979 current
->fsuid
!= new_dir
->i_uid
&& !capable(CAP_FOWNER
))
981 if (IS_APPEND(new_inode
) || IS_IMMUTABLE(new_inode
))
984 if (S_ISDIR(old_inode
->i_mode
)) {
986 if (new_inode
&& !S_ISDIR(new_inode
->i_mode
))
989 if (is_subdir(new_dentry
, old_dentry
))
991 dir_bh
= ext2_bread (old_inode
, 0, 0, &retval
);
994 if (le32_to_cpu(PARENT_INO(dir_bh
->b_data
)) != old_dir
->i_ino
)
997 if (!new_inode
&& new_dir
->i_nlink
>= EXT2_LINK_MAX
)
1001 new_bh
= ext2_add_entry (new_dir
, new_dentry
->d_name
.name
,
1002 new_dentry
->d_name
.len
, &new_de
,
1007 new_dir
->i_version
= ++event
;
1012 new_de
->inode
= le32_to_cpu(old_inode
->i_ino
);
1013 if (EXT2_HAS_INCOMPAT_FEATURE(new_dir
->i_sb
,
1014 EXT2_FEATURE_INCOMPAT_FILETYPE
))
1015 new_de
->file_type
= old_de
->file_type
;
1017 ext2_delete_entry (old_de
, old_bh
);
1019 old_dir
->i_version
= ++event
;
1021 new_inode
->i_nlink
--;
1022 new_inode
->i_ctime
= CURRENT_TIME
;
1023 mark_inode_dirty(new_inode
);
1025 old_dir
->i_ctime
= old_dir
->i_mtime
= CURRENT_TIME
;
1026 old_dir
->u
.ext2_i
.i_flags
&= ~EXT2_BTREE_FL
;
1027 mark_inode_dirty(old_dir
);
1029 PARENT_INO(dir_bh
->b_data
) = le32_to_cpu(new_dir
->i_ino
);
1030 mark_buffer_dirty(dir_bh
, 1);
1032 mark_inode_dirty(old_dir
);
1034 new_inode
->i_nlink
--;
1035 mark_inode_dirty(new_inode
);
1038 new_dir
->u
.ext2_i
.i_flags
&= ~EXT2_BTREE_FL
;
1039 mark_inode_dirty(new_dir
);
1042 mark_buffer_dirty(old_bh
, 1);
1043 if (IS_SYNC(old_dir
)) {
1044 ll_rw_block (WRITE
, 1, &old_bh
);
1045 wait_on_buffer (old_bh
);
1047 mark_buffer_dirty(new_bh
, 1);
1048 if (IS_SYNC(new_dir
)) {
1049 ll_rw_block (WRITE
, 1, &new_bh
);
1050 wait_on_buffer (new_bh
);
1053 /* Update the dcache */
1054 d_move(old_dentry
, new_dentry
);
1065 * Ok, rename also locks out other renames, as they can change the parent of
1066 * a directory, and we don't want any races. Other races are checked for by
1067 * "do_rename()", which restarts if there are inconsistencies.
1069 * Note that there is no race between different filesystems: it's only within
1070 * the same device that races occur: many renames can happen at once, as long
1071 * as they are on different partitions.
1073 * In the second extended file system, we use a lock flag stored in the memory
1074 * super-block. This way, we really lock other renames only if they occur
1075 * on the same file system
1077 int ext2_rename (struct inode
* old_dir
, struct dentry
*old_dentry
,
1078 struct inode
* new_dir
, struct dentry
*new_dentry
)
1082 while (old_dir
->i_sb
->u
.ext2_sb
.s_rename_lock
)
1083 sleep_on (&old_dir
->i_sb
->u
.ext2_sb
.s_rename_wait
);
1084 old_dir
->i_sb
->u
.ext2_sb
.s_rename_lock
= 1;
1085 result
= do_ext2_rename (old_dir
, old_dentry
, new_dir
, new_dentry
);
1086 old_dir
->i_sb
->u
.ext2_sb
.s_rename_lock
= 0;
1087 wake_up (&old_dir
->i_sb
->u
.ext2_sb
.s_rename_wait
);