Linux 2.3.0
[davej-history.git] / fs / ext2 / namei.c
blob4a8c4a7d6a522620144f80f03c81d5dd155c785e
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 sb = dir->i_sb;
79 if (namelen > EXT2_NAME_LEN)
80 return NULL;
82 memset (bh_use, 0, sizeof (bh_use));
83 toread = 0;
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)
88 break;
89 bh = ext2_getblk (dir, block, 0, &err);
90 bh_use[block] = bh;
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;
98 char * dlimit;
100 if ((block % NAMEI_RA_BLOCKS) == 0 && toread) {
101 ll_rw_block (READ, toread, bh_read);
102 toread = 0;
104 bh = bh_use[block % NAMEI_RA_SIZE];
105 if (!bh) {
106 #if 0
107 ext2_error (sb, "ext2_find_entry",
108 "directory #%lu contains a hole at offset %lu",
109 dir->i_ino, offset);
110 #endif
111 offset += sb->s_blocksize;
112 continue;
114 wait_on_buffer (bh);
115 if (!buffer_uptodate(bh)) {
117 * read error: all bets are off
119 break;
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' */
127 int de_len;
129 if ((char *) de + namelen <= dlimit &&
130 ext2_match (namelen, name, de)) {
131 /* found a match -
132 just to be sure, do a full check */
133 if (!ext2_check_dir_entry("ext2_find_entry",
134 dir, de, bh, offset))
135 goto failure;
136 for (i = 0; i < NAMEI_RA_SIZE; ++i) {
137 if (bh_use[i] != bh)
138 brelse (bh_use[i]);
140 *res_dir = de;
141 return bh;
143 /* prevent looping on a bad block */
144 de_len = le16_to_cpu(de->rec_len);
145 if (de_len <= 0)
146 goto failure;
147 offset += de_len;
148 de = (struct ext2_dir_entry_2 *)
149 ((char *) de + de_len);
152 brelse (bh);
153 if (((block + NAMEI_RA_SIZE) << EXT2_BLOCK_SIZE_BITS (sb)) >=
154 dir->i_size)
155 bh = NULL;
156 else
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;
163 failure:
164 for (i = 0; i < NAMEI_RA_SIZE; ++i)
165 brelse (bh_use[i]);
166 return NULL;
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);
179 inode = NULL;
180 if (bh) {
181 unsigned long ino = le32_to_cpu(de->inode);
182 brelse (bh);
183 inode = iget(dir->i_sb, ino);
185 if (!inode)
186 return ERR_PTR(-EACCES);
188 d_add(dentry, inode);
189 return NULL;
193 * ext2_add_entry()
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,
205 int *err)
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;
213 *err = -EINVAL;
214 *res_dir = NULL;
215 if (!dir || !dir->i_nlink)
216 return NULL;
217 sb = dir->i_sb;
219 if (!namelen)
220 return NULL;
222 * Is this a busy deleted directory? Can't create new files if so
224 if (dir->i_size == 0)
226 *err = -ENOENT;
227 return NULL;
229 bh = ext2_bread (dir, 0, 0, err);
230 if (!bh)
231 return NULL;
232 rec_len = EXT2_DIR_REC_LEN(namelen);
233 offset = 0;
234 de = (struct ext2_dir_entry_2 *) bh->b_data;
235 *err = -ENOSPC;
236 while (1) {
237 if ((char *)de >= sb->s_blocksize + bh->b_data) {
238 brelse (bh);
239 bh = NULL;
240 bh = ext2_bread (dir, offset >> EXT2_BLOCK_SIZE_BITS(sb), 1, err);
241 if (!bh)
242 return NULL;
243 if (dir->i_size <= offset) {
244 if (dir->i_size == 0) {
245 *err = -ENOENT;
246 return NULL;
249 ext2_debug ("creating next block\n");
251 de = (struct ext2_dir_entry_2 *) bh->b_data;
252 de->inode = 0;
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);
257 } else {
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,
265 offset)) {
266 *err = -ENOENT;
267 brelse (bh);
268 return NULL;
270 if (ext2_match (namelen, name, de)) {
271 *err = -EEXIST;
272 brelse (bh);
273 return NULL;
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));
284 de = de1;
286 de->inode = 0;
287 de->name_len = namelen;
288 de->file_type = 0;
289 memcpy (de->name, name, namelen);
291 * XXX shouldn't update any times until successful
292 * completion of syscall, but too many callers depend
293 * on this.
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);
306 *res_dir = de;
307 *err = 0;
308 return bh;
310 offset += le16_to_cpu(de->rec_len);
311 de = (struct ext2_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
313 brelse (bh);
314 return NULL;
318 * ext2_delete_entry deletes a directory entry by merging it with the
319 * previous entry
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;
325 int i;
327 i = 0;
328 pde = NULL;
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,
332 de, bh, i))
333 return -EIO;
334 if (de == dir) {
335 if (pde)
336 pde->rec_len =
337 cpu_to_le16(le16_to_cpu(pde->rec_len) +
338 le16_to_cpu(dir->rec_len));
339 else
340 dir->inode = 0;
341 return 0;
343 i += le16_to_cpu(de->rec_len);
344 pde = de;
345 de = (struct ext2_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
347 return -ENOENT;
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;
363 int err = -EIO;
366 * N.B. Several error exits in ext2_new_inode don't set err.
368 inode = ext2_new_inode (dir, mode, &err);
369 if (!inode)
370 return 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);
376 if (!bh) {
377 inode->i_nlink--;
378 mark_inode_dirty(inode);
379 iput (inode);
380 return err;
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);
388 if (IS_SYNC(dir)) {
389 ll_rw_block (WRITE, 1, &bh);
390 wait_on_buffer (bh);
392 brelse (bh);
393 d_instantiate(dentry, inode);
394 return 0;
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;
402 int err = -EIO;
404 inode = ext2_new_inode (dir, mode, &err);
405 if (!inode)
406 goto out;
408 inode->i_uid = current->fsuid;
409 inode->i_mode = mode;
410 inode->i_op = NULL;
411 bh = ext2_add_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, &err);
412 if (!bh)
413 goto out_no_entry;
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)) {
432 init_fifo(inode);
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);
441 if (IS_SYNC(dir)) {
442 ll_rw_block (WRITE, 1, &bh);
443 wait_on_buffer (bh);
445 d_instantiate(dentry, inode);
446 brelse(bh);
447 err = 0;
448 out:
449 return err;
451 out_no_entry:
452 inode->i_nlink--;
453 mark_inode_dirty(inode);
454 iput(inode);
455 goto out;
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;
463 int err;
465 err = -EMLINK;
466 if (dir->i_nlink >= EXT2_LINK_MAX)
467 goto out;
469 err = -EIO;
470 inode = ext2_new_inode (dir, S_IFDIR, &err);
471 if (!inode)
472 goto out;
474 inode->i_op = &ext2_dir_inode_operations;
475 inode->i_size = inode->i_sb->s_blocksize;
476 inode->i_blocks = 0;
477 dir_block = ext2_bread (inode, 0, 1, &err);
478 if (!dir_block) {
479 inode->i_nlink--; /* is this nlink == 0? */
480 mark_inode_dirty(inode);
481 iput (inode);
482 return err;
484 de = (struct ext2_dir_entry_2 *) dir_block->b_data;
485 de->inode = cpu_to_le32(inode->i_ino);
486 de->name_len = 1;
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));
495 de->name_len = 2;
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;
500 inode->i_nlink = 2;
501 mark_buffer_dirty(dir_block, 1);
502 brelse (dir_block);
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);
508 if (!bh)
509 goto out_no_entry;
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);
516 if (IS_SYNC(dir)) {
517 ll_rw_block (WRITE, 1, &bh);
518 wait_on_buffer (bh);
520 dir->i_nlink++;
521 dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
522 mark_inode_dirty(dir);
523 d_instantiate(dentry, inode);
524 brelse (bh);
525 err = 0;
526 out:
527 return err;
529 out_no_entry:
530 inode->i_nlink = 0;
531 mark_inode_dirty(inode);
532 iput (inode);
533 goto out;
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;
545 int err;
547 sb = inode->i_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",
552 inode->i_ino);
553 return 1;
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 `..'",
561 inode->i_ino);
562 brelse (bh);
563 return 1;
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)) {
569 brelse (bh);
570 bh = ext2_bread (inode, offset >> EXT2_BLOCK_SIZE_BITS(sb), 1, &err);
571 if (!bh) {
572 #if 0
573 ext2_error (sb, "empty_dir",
574 "directory #%lu contains a hole at offset %lu",
575 inode->i_ino, offset);
576 #endif
577 offset += sb->s_blocksize;
578 continue;
580 de = (struct ext2_dir_entry_2 *) bh->b_data;
582 if (!ext2_check_dir_entry ("empty_dir", inode, de, bh,
583 offset)) {
584 brelse (bh);
585 return 1;
587 if (le32_to_cpu(de->inode)) {
588 brelse (bh);
589 return 0;
591 offset += le16_to_cpu(de->rec_len);
592 de = (struct ext2_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
594 brelse (bh);
595 return 1;
598 int ext2_rmdir (struct inode * dir, struct dentry *dentry)
600 int retval;
601 struct inode * inode;
602 struct buffer_head * bh;
603 struct ext2_dir_entry_2 * de;
605 retval = -ENOENT;
606 bh = ext2_find_entry (dir, dentry->d_name.name, dentry->d_name.len, &de);
607 if (!bh)
608 goto end_rmdir;
610 inode = dentry->d_inode;
611 DQUOT_INIT(inode);
613 retval = -EIO;
614 if (le32_to_cpu(de->inode) != inode->i_ino)
615 goto end_rmdir;
617 retval = -ENOTEMPTY;
618 if (!empty_dir (inode))
619 goto end_rmdir;
621 retval = ext2_delete_entry (de, bh);
622 dir->i_version = ++event;
623 if (retval)
624 goto end_rmdir;
625 mark_buffer_dirty(bh, 1);
626 if (IS_SYNC(dir)) {
627 ll_rw_block (WRITE, 1, &bh);
628 wait_on_buffer (bh);
630 if (inode->i_nlink != 2)
631 ext2_warning (inode->i_sb, "ext2_rmdir",
632 "empty directory has nlink!=2 (%d)",
633 inode->i_nlink);
634 inode->i_version = ++event;
635 inode->i_nlink = 0;
636 inode->i_size = 0;
637 mark_inode_dirty(inode);
638 dir->i_nlink--;
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);
642 d_delete(dentry);
644 end_rmdir:
645 brelse (bh);
646 return retval;
649 int ext2_unlink(struct inode * dir, struct dentry *dentry)
651 int retval;
652 struct inode * inode;
653 struct buffer_head * bh;
654 struct ext2_dir_entry_2 * de;
656 retval = -ENOENT;
657 bh = ext2_find_entry (dir, dentry->d_name.name, dentry->d_name.len, &de);
658 if (!bh)
659 goto end_unlink;
661 inode = dentry->d_inode;
662 DQUOT_INIT(inode);
664 retval = -EIO;
665 if (le32_to_cpu(de->inode) != inode->i_ino)
666 goto end_unlink;
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);
672 inode->i_nlink = 1;
674 retval = ext2_delete_entry (de, bh);
675 if (retval)
676 goto end_unlink;
677 dir->i_version = ++event;
678 mark_buffer_dirty(bh, 1);
679 if (IS_SYNC(dir)) {
680 ll_rw_block (WRITE, 1, &bh);
681 wait_on_buffer (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);
686 inode->i_nlink--;
687 mark_inode_dirty(inode);
688 inode->i_ctime = dir->i_ctime;
689 retval = 0;
690 d_delete(dentry); /* This also frees the inode */
692 end_unlink:
693 brelse (bh);
694 return retval;
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;
702 char * link;
703 int i, l, err = -EIO;
704 char c;
706 if (!(inode = ext2_new_inode (dir, S_IFLNK, &err))) {
707 return 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 &&
712 symname [l]; l++)
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);
719 if (!name_block) {
720 inode->i_nlink--;
721 mark_inode_dirty(inode);
722 iput (inode);
723 return err;
725 link = name_block->b_data;
726 } else {
727 link = (char *) inode->u.ext2_i.i_data;
729 ext2_debug ("l=%d, fast symlink\n", l);
732 i = 0;
733 while (i < inode->i_sb->s_blocksize - 1 && (c = *(symname++)))
734 link[i++] = c;
735 link[i] = 0;
736 if (name_block) {
737 mark_buffer_dirty(name_block, 1);
738 brelse (name_block);
740 inode->i_size = i;
741 mark_inode_dirty(inode);
743 bh = ext2_add_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, &err);
744 if (!bh)
745 goto out_no_entry;
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);
752 if (IS_SYNC(dir)) {
753 ll_rw_block (WRITE, 1, &bh);
754 wait_on_buffer (bh);
756 brelse (bh);
757 d_instantiate(dentry, inode);
758 err = 0;
759 out:
760 return err;
762 out_no_entry:
763 inode->i_nlink--;
764 mark_inode_dirty(inode);
765 iput (inode);
766 goto out;
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;
775 int err;
777 if (S_ISDIR(inode->i_mode))
778 return -EPERM;
780 if (inode->i_nlink >= EXT2_LINK_MAX)
781 return -EMLINK;
783 bh = ext2_add_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, &err);
784 if (!bh)
785 return 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);
805 if (IS_SYNC(dir)) {
806 ll_rw_block (WRITE, 1, &bh);
807 wait_on_buffer (bh);
809 brelse (bh);
810 inode->i_nlink++;
811 inode->i_ctime = CURRENT_TIME;
812 mark_inode_dirty(inode);
813 inode->i_count++;
814 d_instantiate(dentry, inode);
815 return 0;
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;
832 int retval;
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;
844 retval = -ENOENT;
845 if (!old_bh || le32_to_cpu(old_de->inode) != old_inode->i_ino)
846 goto end_rename;
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);
851 if (new_bh) {
852 if (!new_inode) {
853 brelse (new_bh);
854 new_bh = NULL;
855 } else {
856 DQUOT_INIT(new_inode);
859 if (S_ISDIR(old_inode->i_mode)) {
860 if (new_inode) {
861 retval = -ENOTEMPTY;
862 if (!empty_dir (new_inode))
863 goto end_rename;
865 retval = -EIO;
866 dir_bh = ext2_bread (old_inode, 0, 0, &retval);
867 if (!dir_bh)
868 goto end_rename;
869 if (le32_to_cpu(PARENT_INO(dir_bh->b_data)) != old_dir->i_ino)
870 goto end_rename;
871 retval = -EMLINK;
872 if (!new_inode && new_dir->i_nlink >= EXT2_LINK_MAX)
873 goto end_rename;
875 if (!new_bh) {
876 new_bh = ext2_add_entry (new_dir, new_dentry->d_name.name,
877 new_dentry->d_name.len, &new_de,
878 &retval);
879 if (!new_bh)
880 goto end_rename;
882 new_dir->i_version = ++event;
885 * ok, that's it
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;
895 if (new_inode) {
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);
903 if (dir_bh) {
904 PARENT_INO(dir_bh->b_data) = le32_to_cpu(new_dir->i_ino);
905 mark_buffer_dirty(dir_bh, 1);
906 old_dir->i_nlink--;
907 mark_inode_dirty(old_dir);
908 if (new_inode) {
909 new_inode->i_nlink--;
910 mark_inode_dirty(new_inode);
911 } else {
912 new_dir->i_nlink++;
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);
928 retval = 0;
930 end_rename:
931 brelse (dir_bh);
932 brelse (old_bh);
933 brelse (new_bh);
934 return retval;