Import 2.1.116pre2
[davej-history.git] / fs / ext2 / namei.c
blobddf72ec4cc0cfb2ac34b9d71f6591113fc135cc8
1 /*
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)
9 * from
11 * linux/fs/minix/namei.c
13 * Copyright (C) 1991, 1992 Linus Torvalds
15 * Big-endian to little-endian byte-swapping/bitmaps by
16 * David S. Miller (davem@caip.rutgers.edu), 1995
17 * Directory entry file type support and forward compatibility hooks
18 * for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998
21 #include <asm/uaccess.h>
23 #include <linux/errno.h>
24 #include <linux/fs.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)
52 return 0;
53 if (!de->inode)
54 return 0;
55 return !memcmp(name, de->name, len);
59 * ext2_find_entry()
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];
73 unsigned long offset;
74 int block, toread, i, err;
76 *res_dir = NULL;
77 if (!dir)
78 return NULL;
79 sb = dir->i_sb;
81 if (namelen > EXT2_NAME_LEN)
82 return NULL;
84 memset (bh_use, 0, sizeof (bh_use));
85 toread = 0;
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)
90 break;
91 bh = ext2_getblk (dir, block, 0, &err);
92 bh_use[block] = bh;
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;
100 char * dlimit;
102 if ((block % NAMEI_RA_BLOCKS) == 0 && toread) {
103 ll_rw_block (READ, toread, bh_read);
104 toread = 0;
106 bh = bh_use[block % NAMEI_RA_SIZE];
107 if (!bh) {
108 #if 0
109 ext2_error (sb, "ext2_find_entry",
110 "directory #%lu contains a hole at offset %lu",
111 dir->i_ino, offset);
112 #endif
113 offset += sb->s_blocksize;
114 continue;
116 wait_on_buffer (bh);
117 if (!buffer_uptodate(bh)) {
119 * read error: all bets are off
121 break;
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' */
129 int de_len;
131 if ((char *) de + namelen <= dlimit &&
132 ext2_match (namelen, name, de)) {
133 /* found a match -
134 just to be sure, do a full check */
135 if (!ext2_check_dir_entry("ext2_find_entry",
136 dir, de, bh, offset))
137 goto failure;
138 for (i = 0; i < NAMEI_RA_SIZE; ++i) {
139 if (bh_use[i] != bh)
140 brelse (bh_use[i]);
142 *res_dir = de;
143 return bh;
145 /* prevent looping on a bad block */
146 de_len = le16_to_cpu(de->rec_len);
147 if (de_len <= 0)
148 goto failure;
149 offset += de_len;
150 de = (struct ext2_dir_entry_2 *)
151 ((char *) de + de_len);
154 brelse (bh);
155 if (((block + NAMEI_RA_SIZE) << EXT2_BLOCK_SIZE_BITS (sb)) >=
156 dir->i_size)
157 bh = NULL;
158 else
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;
165 failure:
166 for (i = 0; i < NAMEI_RA_SIZE; ++i)
167 brelse (bh_use[i]);
168 return NULL;
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);
181 inode = NULL;
182 if (bh) {
183 unsigned long ino = le32_to_cpu(de->inode);
184 brelse (bh);
185 inode = iget(dir->i_sb, ino);
187 if (!inode)
188 return -EACCES;
190 d_add(dentry, inode);
191 return 0;
195 * ext2_add_entry()
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,
207 int *err)
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;
215 *err = -EINVAL;
216 *res_dir = NULL;
217 if (!dir || !dir->i_nlink)
218 return NULL;
219 sb = dir->i_sb;
221 if (namelen > EXT2_NAME_LEN)
223 *err = -ENAMETOOLONG;
224 return NULL;
227 if (!namelen)
228 return NULL;
230 * Is this a busy deleted directory? Can't create new files if so
232 if (dir->i_size == 0)
234 *err = -ENOENT;
235 return NULL;
237 bh = ext2_bread (dir, 0, 0, err);
238 if (!bh)
239 return NULL;
240 rec_len = EXT2_DIR_REC_LEN(namelen);
241 offset = 0;
242 de = (struct ext2_dir_entry_2 *) bh->b_data;
243 *err = -ENOSPC;
244 while (1) {
245 if ((char *)de >= sb->s_blocksize + bh->b_data) {
246 brelse (bh);
247 bh = NULL;
248 bh = ext2_bread (dir, offset >> EXT2_BLOCK_SIZE_BITS(sb), 1, err);
249 if (!bh)
250 return NULL;
251 if (dir->i_size <= offset) {
252 if (dir->i_size == 0) {
253 *err = -ENOENT;
254 return NULL;
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);
265 } else {
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,
273 offset)) {
274 *err = -ENOENT;
275 brelse (bh);
276 return NULL;
278 if (ext2_match (namelen, name, de)) {
279 *err = -EEXIST;
280 brelse (bh);
281 return NULL;
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));
292 de = de1;
294 de->inode = cpu_to_le32(0);
295 de->name_len = namelen;
296 de->file_type = 0;
297 memcpy (de->name, name, namelen);
299 * XXX shouldn't update any times until successful
300 * completion of syscall, but too many callers depend
301 * on this.
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);
314 *res_dir = de;
315 *err = 0;
316 return bh;
318 offset += le16_to_cpu(de->rec_len);
319 de = (struct ext2_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
321 brelse (bh);
322 return NULL;
326 * ext2_delete_entry deletes a directory entry by merging it with the
327 * previous entry
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;
333 int i;
335 i = 0;
336 pde = NULL;
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,
340 de, bh, i))
341 return -EIO;
342 if (de == dir) {
343 if (pde)
344 pde->rec_len =
345 cpu_to_le16(le16_to_cpu(pde->rec_len) +
346 le16_to_cpu(dir->rec_len));
347 dir->inode = le32_to_cpu(0);
348 return 0;
350 i += le16_to_cpu(de->rec_len);
351 pde = de;
352 de = (struct ext2_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
354 return -ENOENT;
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;
370 int err = -EIO;
373 * N.B. Several error exits in ext2_new_inode don't set err.
375 inode = ext2_new_inode (dir, mode, &err);
376 if (!inode)
377 return 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);
383 if (!bh) {
384 inode->i_nlink--;
385 mark_inode_dirty(inode);
386 iput (inode);
387 return err;
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);
395 if (IS_SYNC(dir)) {
396 ll_rw_block (WRITE, 1, &bh);
397 wait_on_buffer (bh);
399 brelse (bh);
400 d_instantiate(dentry, inode);
401 return 0;
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;
409 int err = -EIO;
411 err = -ENAMETOOLONG;
412 if (dentry->d_name.len > EXT2_NAME_LEN)
413 goto out;
415 inode = ext2_new_inode (dir, mode, &err);
416 if (!inode)
417 goto out;
419 inode->i_uid = current->fsuid;
420 inode->i_mode = mode;
421 inode->i_op = NULL;
422 bh = ext2_add_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, &err);
423 if (!bh)
424 goto out_no_entry;
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)) {
456 init_fifo(inode);
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);
465 if (IS_SYNC(dir)) {
466 ll_rw_block (WRITE, 1, &bh);
467 wait_on_buffer (bh);
469 d_instantiate(dentry, inode);
470 brelse(bh);
471 err = 0;
472 out:
473 return err;
475 out_no_entry:
476 inode->i_nlink--;
477 mark_inode_dirty(inode);
478 iput(inode);
479 goto out;
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;
487 int err;
489 err = -ENAMETOOLONG;
490 if (dentry->d_name.len > EXT2_NAME_LEN)
491 goto out;
493 err = -EMLINK;
494 if (dir->i_nlink >= EXT2_LINK_MAX)
495 goto out;
497 err = -EIO;
498 inode = ext2_new_inode (dir, S_IFDIR, &err);
499 if (!inode)
500 goto out;
502 inode->i_op = &ext2_dir_inode_operations;
503 inode->i_size = inode->i_sb->s_blocksize;
504 inode->i_blocks = 0;
505 dir_block = ext2_bread (inode, 0, 1, &err);
506 if (!dir_block) {
507 inode->i_nlink--; /* is this nlink == 0? */
508 mark_inode_dirty(inode);
509 iput (inode);
510 return err;
512 de = (struct ext2_dir_entry_2 *) dir_block->b_data;
513 de->inode = cpu_to_le32(inode->i_ino);
514 de->name_len = 1;
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));
523 de->name_len = 2;
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;
528 inode->i_nlink = 2;
529 mark_buffer_dirty(dir_block, 1);
530 brelse (dir_block);
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);
536 if (!bh)
537 goto out_no_entry;
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);
544 if (IS_SYNC(dir)) {
545 ll_rw_block (WRITE, 1, &bh);
546 wait_on_buffer (bh);
548 dir->i_nlink++;
549 dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
550 mark_inode_dirty(dir);
551 d_instantiate(dentry, inode);
552 brelse (bh);
553 err = 0;
554 out:
555 return err;
557 out_no_entry:
558 inode->i_nlink = 0;
559 mark_inode_dirty(inode);
560 iput (inode);
561 goto out;
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;
573 int err;
575 sb = inode->i_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",
580 inode->i_ino);
581 return 1;
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 `..'",
589 inode->i_ino);
590 brelse (bh);
591 return 1;
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)) {
597 brelse (bh);
598 bh = ext2_bread (inode, offset >> EXT2_BLOCK_SIZE_BITS(sb), 1, &err);
599 if (!bh) {
600 #if 0
601 ext2_error (sb, "empty_dir",
602 "directory #%lu contains a hole at offset %lu",
603 inode->i_ino, offset);
604 #endif
605 offset += sb->s_blocksize;
606 continue;
608 de = (struct ext2_dir_entry_2 *) bh->b_data;
610 if (!ext2_check_dir_entry ("empty_dir", inode, de, bh,
611 offset)) {
612 brelse (bh);
613 return 1;
615 if (le32_to_cpu(de->inode)) {
616 brelse (bh);
617 return 0;
619 offset += le16_to_cpu(de->rec_len);
620 de = (struct ext2_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
622 brelse (bh);
623 return 1;
626 int ext2_rmdir (struct inode * dir, struct dentry *dentry)
628 int retval;
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)
635 goto out;
637 retval = -ENOENT;
638 bh = ext2_find_entry (dir, dentry->d_name.name, dentry->d_name.len, &de);
639 if (!bh)
640 goto end_rmdir;
642 inode = dentry->d_inode;
643 DQUOT_INIT(inode);
645 retval = -EPERM;
646 if ((dir->i_mode & S_ISVTX) &&
647 current->fsuid != inode->i_uid &&
648 current->fsuid != dir->i_uid && !capable(CAP_FOWNER))
649 goto end_rmdir;
650 if (inode == dir) /* we may not delete ".", but "../dir" is ok */
651 goto end_rmdir;
653 retval = -ENOTDIR;
654 if (!S_ISDIR(inode->i_mode))
655 goto end_rmdir;
657 retval = -EIO;
658 if (inode->i_dev != dir->i_dev)
659 goto end_rmdir;
660 if (le32_to_cpu(de->inode) != inode->i_ino)
661 goto end_rmdir;
663 down(&inode->i_sem);
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))
671 retval = -ENOTEMPTY;
672 else if (le32_to_cpu(de->inode) != inode->i_ino)
673 retval = -ENOENT;
674 else {
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.
683 inode->i_size = 0;
685 retval = ext2_delete_entry (de, bh);
686 dir->i_version = ++event;
688 up(&inode->i_sem);
689 if (retval)
690 goto end_rmdir;
691 mark_buffer_dirty(bh, 1);
692 if (IS_SYNC(dir)) {
693 ll_rw_block (WRITE, 1, &bh);
694 wait_on_buffer (bh);
696 if (inode->i_nlink != 2)
697 ext2_warning (inode->i_sb, "ext2_rmdir",
698 "empty directory has nlink!=2 (%d)",
699 inode->i_nlink);
700 inode->i_version = ++event;
701 inode->i_nlink = 0;
702 mark_inode_dirty(inode);
703 dir->i_nlink--;
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);
707 d_delete(dentry);
709 end_rmdir:
710 brelse (bh);
711 out:
712 return retval;
715 int ext2_unlink(struct inode * dir, struct dentry *dentry)
717 int retval;
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)
724 goto out;
726 retval = -ENOENT;
727 bh = ext2_find_entry (dir, dentry->d_name.name, dentry->d_name.len, &de);
728 if (!bh)
729 goto end_unlink;
731 inode = dentry->d_inode;
732 DQUOT_INIT(inode);
734 retval = -EPERM;
735 if (S_ISDIR(inode->i_mode))
736 goto end_unlink;
737 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
738 goto end_unlink;
739 if ((dir->i_mode & S_ISVTX) &&
740 current->fsuid != inode->i_uid &&
741 current->fsuid != dir->i_uid && !capable(CAP_FOWNER))
742 goto end_unlink;
744 retval = -EIO;
745 if (le32_to_cpu(de->inode) != inode->i_ino)
746 goto end_unlink;
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);
752 inode->i_nlink = 1;
754 retval = ext2_delete_entry (de, bh);
755 if (retval)
756 goto end_unlink;
757 dir->i_version = ++event;
758 mark_buffer_dirty(bh, 1);
759 if (IS_SYNC(dir)) {
760 ll_rw_block (WRITE, 1, &bh);
761 wait_on_buffer (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);
766 inode->i_nlink--;
767 mark_inode_dirty(inode);
768 inode->i_ctime = dir->i_ctime;
769 retval = 0;
770 d_delete(dentry); /* This also frees the inode */
772 end_unlink:
773 brelse (bh);
774 out:
775 return retval;
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;
783 char * link;
784 int i, l, err = -EIO;
785 char c;
787 if (!(inode = ext2_new_inode (dir, S_IFLNK, &err))) {
788 return 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 &&
793 symname [l]; l++)
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);
800 if (!name_block) {
801 inode->i_nlink--;
802 mark_inode_dirty(inode);
803 iput (inode);
804 return err;
806 link = name_block->b_data;
807 } else {
808 link = (char *) inode->u.ext2_i.i_data;
810 ext2_debug ("l=%d, fast symlink\n", l);
813 i = 0;
814 while (i < inode->i_sb->s_blocksize - 1 && (c = *(symname++)))
815 link[i++] = c;
816 link[i] = 0;
817 if (name_block) {
818 mark_buffer_dirty(name_block, 1);
819 brelse (name_block);
821 inode->i_size = i;
822 mark_inode_dirty(inode);
824 bh = ext2_add_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, &err);
825 if (!bh)
826 goto out_no_entry;
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);
833 if (IS_SYNC(dir)) {
834 ll_rw_block (WRITE, 1, &bh);
835 wait_on_buffer (bh);
837 brelse (bh);
838 d_instantiate(dentry, inode);
839 err = 0;
840 out:
841 return err;
843 out_no_entry:
844 inode->i_nlink--;
845 mark_inode_dirty(inode);
846 iput (inode);
847 goto out;
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;
856 int err;
858 if (S_ISDIR(inode->i_mode))
859 return -EPERM;
861 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
862 return -EPERM;
864 if (inode->i_nlink >= EXT2_LINK_MAX)
865 return -EMLINK;
867 bh = ext2_add_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, &err);
868 if (!bh)
869 return 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);
889 if (IS_SYNC(dir)) {
890 ll_rw_block (WRITE, 1, &bh);
891 wait_on_buffer (bh);
893 brelse (bh);
894 inode->i_nlink++;
895 inode->i_ctime = CURRENT_TIME;
896 mark_inode_dirty(inode);
897 inode->i_count++;
898 d_instantiate(dentry, inode);
899 return 0;
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
908 * minimal.
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;
923 int retval;
925 old_bh = new_bh = dir_bh = NULL;
926 retval = -ENAMETOOLONG;
927 if (old_dentry->d_name.len > EXT2_NAME_LEN)
928 goto end_rename;
930 old_bh = ext2_find_entry (old_dir, old_dentry->d_name.name, old_dentry->d_name.len, &old_de);
931 retval = -ENOENT;
932 if (!old_bh)
933 goto end_rename;
934 old_inode = old_dentry->d_inode;
936 retval = -EPERM;
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))
940 goto end_rename;
941 if (IS_APPEND(old_inode) || IS_IMMUTABLE(old_inode))
942 goto end_rename;
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);
947 if (new_bh) {
948 if (!new_inode) {
949 brelse (new_bh);
950 new_bh = NULL;
951 } else {
952 DQUOT_INIT(new_inode);
955 retval = 0;
956 if (new_inode == old_inode)
957 goto end_rename;
958 if (new_inode && S_ISDIR(new_inode->i_mode)) {
959 retval = -EISDIR;
960 if (!S_ISDIR(old_inode->i_mode))
961 goto end_rename;
962 retval = -EINVAL;
963 if (is_subdir(new_dentry, old_dentry))
964 goto end_rename;
965 /* Prune any children before testing for busy */
966 if (new_dentry->d_count > 1)
967 shrink_dcache_parent(new_dentry);
968 retval = -ENOTEMPTY;
969 if (!empty_dir (new_inode))
970 goto end_rename;
971 retval = -EBUSY;
972 if (new_dentry->d_count > 1)
973 goto end_rename;
975 retval = -EPERM;
976 if (new_inode) {
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))
980 goto end_rename;
981 if (IS_APPEND(new_inode) || IS_IMMUTABLE(new_inode))
982 goto end_rename;
984 if (S_ISDIR(old_inode->i_mode)) {
985 retval = -ENOTDIR;
986 if (new_inode && !S_ISDIR(new_inode->i_mode))
987 goto end_rename;
988 retval = -EINVAL;
989 if (is_subdir(new_dentry, old_dentry))
990 goto end_rename;
991 dir_bh = ext2_bread (old_inode, 0, 0, &retval);
992 if (!dir_bh)
993 goto end_rename;
994 if (le32_to_cpu(PARENT_INO(dir_bh->b_data)) != old_dir->i_ino)
995 goto end_rename;
996 retval = -EMLINK;
997 if (!new_inode && new_dir->i_nlink >= EXT2_LINK_MAX)
998 goto end_rename;
1000 if (!new_bh) {
1001 new_bh = ext2_add_entry (new_dir, new_dentry->d_name.name,
1002 new_dentry->d_name.len, &new_de,
1003 &retval);
1004 if (!new_bh)
1005 goto end_rename;
1007 new_dir->i_version = ++event;
1010 * ok, that's it
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;
1020 if (new_inode) {
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);
1028 if (dir_bh) {
1029 PARENT_INO(dir_bh->b_data) = le32_to_cpu(new_dir->i_ino);
1030 mark_buffer_dirty(dir_bh, 1);
1031 old_dir->i_nlink--;
1032 mark_inode_dirty(old_dir);
1033 if (new_inode) {
1034 new_inode->i_nlink--;
1035 mark_inode_dirty(new_inode);
1036 } else {
1037 new_dir->i_nlink++;
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);
1055 retval = 0;
1057 end_rename:
1058 brelse (dir_bh);
1059 brelse (old_bh);
1060 brelse (new_bh);
1061 return retval;
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)
1080 int result;
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);
1088 return result;