Import 2.3.1pre2
[davej-history.git] / fs / ext2 / namei.c
blob6bfa7041d5f13771e30220d83fe78358d4befdea
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;
350 static inline void ext2_set_de_type(struct super_block *sb,
351 struct ext2_dir_entry_2 *de,
352 umode_t mode) {
353 if (!EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_FILETYPE))
354 return;
355 if (S_ISCHR(mode))
356 de->file_type = EXT2_FT_CHRDEV;
357 else if (S_ISBLK(mode))
358 de->file_type = EXT2_FT_BLKDEV;
359 else if (S_ISFIFO(mode))
360 de->file_type = EXT2_FT_FIFO;
361 else if (S_ISLNK(mode))
362 de->file_type = EXT2_FT_SYMLINK;
363 else if (S_ISREG(mode))
364 de->file_type = EXT2_FT_REG_FILE;
365 else if (S_ISDIR(mode))
366 de->file_type = EXT2_FT_DIR;
370 * By the time this is called, we already have created
371 * the directory cache entry for the new file, but it
372 * is so far negative - it has no inode.
374 * If the create succeeds, we fill in the inode information
375 * with d_instantiate().
377 int ext2_create (struct inode * dir, struct dentry * dentry, int mode)
379 struct inode * inode;
380 struct buffer_head * bh;
381 struct ext2_dir_entry_2 * de;
382 int err = -EIO;
385 * N.B. Several error exits in ext2_new_inode don't set err.
387 inode = ext2_new_inode (dir, mode, &err);
388 if (!inode)
389 return err;
391 inode->i_op = &ext2_file_inode_operations;
392 inode->i_mode = mode;
393 mark_inode_dirty(inode);
394 bh = ext2_add_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, &err);
395 if (!bh) {
396 inode->i_nlink--;
397 mark_inode_dirty(inode);
398 iput (inode);
399 return err;
401 de->inode = cpu_to_le32(inode->i_ino);
402 ext2_set_de_type(dir->i_sb, de, S_IFREG);
403 dir->i_version = ++event;
404 mark_buffer_dirty(bh, 1);
405 if (IS_SYNC(dir)) {
406 ll_rw_block (WRITE, 1, &bh);
407 wait_on_buffer (bh);
409 brelse (bh);
410 d_instantiate(dentry, inode);
411 return 0;
414 int ext2_mknod (struct inode * dir, struct dentry *dentry, int mode, int rdev)
416 struct inode * inode;
417 struct buffer_head * bh;
418 struct ext2_dir_entry_2 * de;
419 int err = -EIO;
421 inode = ext2_new_inode (dir, mode, &err);
422 if (!inode)
423 goto out;
425 inode->i_uid = current->fsuid;
426 init_special_inode(inode, mode, rdev);
427 bh = ext2_add_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, &err);
428 if (!bh)
429 goto out_no_entry;
430 de->inode = cpu_to_le32(inode->i_ino);
431 dir->i_version = ++event;
432 ext2_set_de_type(dir->i_sb, de, inode->i_mode);
433 mark_inode_dirty(inode);
434 mark_buffer_dirty(bh, 1);
435 if (IS_SYNC(dir)) {
436 ll_rw_block (WRITE, 1, &bh);
437 wait_on_buffer (bh);
439 d_instantiate(dentry, inode);
440 brelse(bh);
441 err = 0;
442 out:
443 return err;
445 out_no_entry:
446 inode->i_nlink--;
447 mark_inode_dirty(inode);
448 iput(inode);
449 goto out;
452 int ext2_mkdir(struct inode * dir, struct dentry * dentry, int mode)
454 struct inode * inode;
455 struct buffer_head * bh, * dir_block;
456 struct ext2_dir_entry_2 * de;
457 int err;
459 err = -EMLINK;
460 if (dir->i_nlink >= EXT2_LINK_MAX)
461 goto out;
463 err = -EIO;
464 inode = ext2_new_inode (dir, S_IFDIR, &err);
465 if (!inode)
466 goto out;
468 inode->i_op = &ext2_dir_inode_operations;
469 inode->i_size = inode->i_sb->s_blocksize;
470 inode->i_blocks = 0;
471 dir_block = ext2_bread (inode, 0, 1, &err);
472 if (!dir_block) {
473 inode->i_nlink--; /* is this nlink == 0? */
474 mark_inode_dirty(inode);
475 iput (inode);
476 return err;
478 de = (struct ext2_dir_entry_2 *) dir_block->b_data;
479 de->inode = cpu_to_le32(inode->i_ino);
480 de->name_len = 1;
481 de->rec_len = cpu_to_le16(EXT2_DIR_REC_LEN(de->name_len));
482 strcpy (de->name, ".");
483 ext2_set_de_type(dir->i_sb, de, S_IFDIR);
484 de = (struct ext2_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
485 de->inode = cpu_to_le32(dir->i_ino);
486 de->rec_len = cpu_to_le16(inode->i_sb->s_blocksize - EXT2_DIR_REC_LEN(1));
487 de->name_len = 2;
488 strcpy (de->name, "..");
489 ext2_set_de_type(dir->i_sb, de, S_IFDIR);
490 inode->i_nlink = 2;
491 mark_buffer_dirty(dir_block, 1);
492 brelse (dir_block);
493 inode->i_mode = S_IFDIR | (mode & (S_IRWXUGO|S_ISVTX) & ~current->fs->umask);
494 if (dir->i_mode & S_ISGID)
495 inode->i_mode |= S_ISGID;
496 mark_inode_dirty(inode);
497 bh = ext2_add_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, &err);
498 if (!bh)
499 goto out_no_entry;
500 de->inode = cpu_to_le32(inode->i_ino);
501 ext2_set_de_type(dir->i_sb, de, S_IFDIR);
502 dir->i_version = ++event;
503 mark_buffer_dirty(bh, 1);
504 if (IS_SYNC(dir)) {
505 ll_rw_block (WRITE, 1, &bh);
506 wait_on_buffer (bh);
508 dir->i_nlink++;
509 dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
510 mark_inode_dirty(dir);
511 d_instantiate(dentry, inode);
512 brelse (bh);
513 err = 0;
514 out:
515 return err;
517 out_no_entry:
518 inode->i_nlink = 0;
519 mark_inode_dirty(inode);
520 iput (inode);
521 goto out;
525 * routine to check that the specified directory is empty (for rmdir)
527 static int empty_dir (struct inode * inode)
529 unsigned long offset;
530 struct buffer_head * bh;
531 struct ext2_dir_entry_2 * de, * de1;
532 struct super_block * sb;
533 int err;
535 sb = inode->i_sb;
536 if (inode->i_size < EXT2_DIR_REC_LEN(1) + EXT2_DIR_REC_LEN(2) ||
537 !(bh = ext2_bread (inode, 0, 0, &err))) {
538 ext2_warning (inode->i_sb, "empty_dir",
539 "bad directory (dir #%lu) - no data block",
540 inode->i_ino);
541 return 1;
543 de = (struct ext2_dir_entry_2 *) bh->b_data;
544 de1 = (struct ext2_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
545 if (le32_to_cpu(de->inode) != inode->i_ino || !le32_to_cpu(de1->inode) ||
546 strcmp (".", de->name) || strcmp ("..", de1->name)) {
547 ext2_warning (inode->i_sb, "empty_dir",
548 "bad directory (dir #%lu) - no `.' or `..'",
549 inode->i_ino);
550 brelse (bh);
551 return 1;
553 offset = le16_to_cpu(de->rec_len) + le16_to_cpu(de1->rec_len);
554 de = (struct ext2_dir_entry_2 *) ((char *) de1 + le16_to_cpu(de1->rec_len));
555 while (offset < inode->i_size ) {
556 if (!bh || (void *) de >= (void *) (bh->b_data + sb->s_blocksize)) {
557 brelse (bh);
558 bh = ext2_bread (inode, offset >> EXT2_BLOCK_SIZE_BITS(sb), 1, &err);
559 if (!bh) {
560 #if 0
561 ext2_error (sb, "empty_dir",
562 "directory #%lu contains a hole at offset %lu",
563 inode->i_ino, offset);
564 #endif
565 offset += sb->s_blocksize;
566 continue;
568 de = (struct ext2_dir_entry_2 *) bh->b_data;
570 if (!ext2_check_dir_entry ("empty_dir", inode, de, bh,
571 offset)) {
572 brelse (bh);
573 return 1;
575 if (le32_to_cpu(de->inode)) {
576 brelse (bh);
577 return 0;
579 offset += le16_to_cpu(de->rec_len);
580 de = (struct ext2_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
582 brelse (bh);
583 return 1;
586 int ext2_rmdir (struct inode * dir, struct dentry *dentry)
588 int retval;
589 struct inode * inode;
590 struct buffer_head * bh;
591 struct ext2_dir_entry_2 * de;
593 retval = -ENOENT;
594 bh = ext2_find_entry (dir, dentry->d_name.name, dentry->d_name.len, &de);
595 if (!bh)
596 goto end_rmdir;
598 inode = dentry->d_inode;
599 DQUOT_INIT(inode);
601 retval = -EIO;
602 if (le32_to_cpu(de->inode) != inode->i_ino)
603 goto end_rmdir;
605 retval = -ENOTEMPTY;
606 if (!empty_dir (inode))
607 goto end_rmdir;
609 retval = ext2_delete_entry (de, bh);
610 dir->i_version = ++event;
611 if (retval)
612 goto end_rmdir;
613 mark_buffer_dirty(bh, 1);
614 if (IS_SYNC(dir)) {
615 ll_rw_block (WRITE, 1, &bh);
616 wait_on_buffer (bh);
618 if (inode->i_nlink != 2)
619 ext2_warning (inode->i_sb, "ext2_rmdir",
620 "empty directory has nlink!=2 (%d)",
621 inode->i_nlink);
622 inode->i_version = ++event;
623 inode->i_nlink = 0;
624 inode->i_size = 0;
625 mark_inode_dirty(inode);
626 dir->i_nlink--;
627 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
628 dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
629 mark_inode_dirty(dir);
630 d_delete(dentry);
632 end_rmdir:
633 brelse (bh);
634 return retval;
637 int ext2_unlink(struct inode * dir, struct dentry *dentry)
639 int retval;
640 struct inode * inode;
641 struct buffer_head * bh;
642 struct ext2_dir_entry_2 * de;
644 retval = -ENOENT;
645 bh = ext2_find_entry (dir, dentry->d_name.name, dentry->d_name.len, &de);
646 if (!bh)
647 goto end_unlink;
649 inode = dentry->d_inode;
650 DQUOT_INIT(inode);
652 retval = -EIO;
653 if (le32_to_cpu(de->inode) != inode->i_ino)
654 goto end_unlink;
656 if (!inode->i_nlink) {
657 ext2_warning (inode->i_sb, "ext2_unlink",
658 "Deleting nonexistent file (%lu), %d",
659 inode->i_ino, inode->i_nlink);
660 inode->i_nlink = 1;
662 retval = ext2_delete_entry (de, bh);
663 if (retval)
664 goto end_unlink;
665 dir->i_version = ++event;
666 mark_buffer_dirty(bh, 1);
667 if (IS_SYNC(dir)) {
668 ll_rw_block (WRITE, 1, &bh);
669 wait_on_buffer (bh);
671 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
672 dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
673 mark_inode_dirty(dir);
674 inode->i_nlink--;
675 mark_inode_dirty(inode);
676 inode->i_ctime = dir->i_ctime;
677 retval = 0;
678 d_delete(dentry); /* This also frees the inode */
680 end_unlink:
681 brelse (bh);
682 return retval;
685 int ext2_symlink (struct inode * dir, struct dentry *dentry, const char * symname)
687 struct ext2_dir_entry_2 * de;
688 struct inode * inode;
689 struct buffer_head * bh = NULL, * name_block = NULL;
690 char * link;
691 int i, l, err = -EIO;
692 char c;
694 if (!(inode = ext2_new_inode (dir, S_IFLNK, &err))) {
695 return err;
697 inode->i_mode = S_IFLNK | S_IRWXUGO;
698 inode->i_op = &ext2_symlink_inode_operations;
699 for (l = 0; l < inode->i_sb->s_blocksize - 1 &&
700 symname [l]; l++)
702 if (l >= sizeof (inode->u.ext2_i.i_data)) {
704 ext2_debug ("l=%d, normal symlink\n", l);
706 name_block = ext2_bread (inode, 0, 1, &err);
707 if (!name_block) {
708 inode->i_nlink--;
709 mark_inode_dirty(inode);
710 iput (inode);
711 return err;
713 link = name_block->b_data;
714 } else {
715 link = (char *) inode->u.ext2_i.i_data;
717 ext2_debug ("l=%d, fast symlink\n", l);
720 i = 0;
721 while (i < inode->i_sb->s_blocksize - 1 && (c = *(symname++)))
722 link[i++] = c;
723 link[i] = 0;
724 if (name_block) {
725 mark_buffer_dirty(name_block, 1);
726 brelse (name_block);
728 inode->i_size = i;
729 mark_inode_dirty(inode);
731 bh = ext2_add_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, &err);
732 if (!bh)
733 goto out_no_entry;
734 de->inode = cpu_to_le32(inode->i_ino);
735 ext2_set_de_type(dir->i_sb, de, S_IFLNK);
736 dir->i_version = ++event;
737 mark_buffer_dirty(bh, 1);
738 if (IS_SYNC(dir)) {
739 ll_rw_block (WRITE, 1, &bh);
740 wait_on_buffer (bh);
742 brelse (bh);
743 d_instantiate(dentry, inode);
744 err = 0;
745 out:
746 return err;
748 out_no_entry:
749 inode->i_nlink--;
750 mark_inode_dirty(inode);
751 iput (inode);
752 goto out;
755 int ext2_link (struct dentry * old_dentry,
756 struct inode * dir, struct dentry *dentry)
758 struct inode *inode = old_dentry->d_inode;
759 struct ext2_dir_entry_2 * de;
760 struct buffer_head * bh;
761 int err;
763 if (S_ISDIR(inode->i_mode))
764 return -EPERM;
766 if (inode->i_nlink >= EXT2_LINK_MAX)
767 return -EMLINK;
769 bh = ext2_add_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, &err);
770 if (!bh)
771 return err;
773 de->inode = cpu_to_le32(inode->i_ino);
774 ext2_set_de_type(dir->i_sb, de, inode->i_mode);
775 dir->i_version = ++event;
776 mark_buffer_dirty(bh, 1);
777 if (IS_SYNC(dir)) {
778 ll_rw_block (WRITE, 1, &bh);
779 wait_on_buffer (bh);
781 brelse (bh);
782 inode->i_nlink++;
783 inode->i_ctime = CURRENT_TIME;
784 mark_inode_dirty(inode);
785 inode->i_count++;
786 d_instantiate(dentry, inode);
787 return 0;
790 #define PARENT_INO(buffer) \
791 ((struct ext2_dir_entry_2 *) ((char *) buffer + \
792 le16_to_cpu(((struct ext2_dir_entry_2 *) buffer)->rec_len)))->inode
795 * Anybody can rename anything with this: the permission checks are left to the
796 * higher-level routines.
798 int ext2_rename (struct inode * old_dir, struct dentry *old_dentry,
799 struct inode * new_dir,struct dentry *new_dentry)
801 struct inode * old_inode, * new_inode;
802 struct buffer_head * old_bh, * new_bh, * dir_bh;
803 struct ext2_dir_entry_2 * old_de, * new_de;
804 int retval;
806 old_bh = new_bh = dir_bh = NULL;
808 old_bh = ext2_find_entry (old_dir, old_dentry->d_name.name, old_dentry->d_name.len, &old_de);
810 * Check for inode number is _not_ due to possible IO errors.
811 * We might rmdir the source, keep it as pwd of some process
812 * and merrily kill the link to whatever was created under the
813 * same name. Goodbye sticky bit ;-<
815 old_inode = old_dentry->d_inode;
816 retval = -ENOENT;
817 if (!old_bh || le32_to_cpu(old_de->inode) != old_inode->i_ino)
818 goto end_rename;
820 new_inode = new_dentry->d_inode;
821 new_bh = ext2_find_entry (new_dir, new_dentry->d_name.name,
822 new_dentry->d_name.len, &new_de);
823 if (new_bh) {
824 if (!new_inode) {
825 brelse (new_bh);
826 new_bh = NULL;
827 } else {
828 DQUOT_INIT(new_inode);
831 if (S_ISDIR(old_inode->i_mode)) {
832 if (new_inode) {
833 retval = -ENOTEMPTY;
834 if (!empty_dir (new_inode))
835 goto end_rename;
837 retval = -EIO;
838 dir_bh = ext2_bread (old_inode, 0, 0, &retval);
839 if (!dir_bh)
840 goto end_rename;
841 if (le32_to_cpu(PARENT_INO(dir_bh->b_data)) != old_dir->i_ino)
842 goto end_rename;
843 retval = -EMLINK;
844 if (!new_inode && new_dir->i_nlink >= EXT2_LINK_MAX)
845 goto end_rename;
847 if (!new_bh) {
848 new_bh = ext2_add_entry (new_dir, new_dentry->d_name.name,
849 new_dentry->d_name.len, &new_de,
850 &retval);
851 if (!new_bh)
852 goto end_rename;
854 new_dir->i_version = ++event;
857 * ok, that's it
859 new_de->inode = le32_to_cpu(old_inode->i_ino);
860 if (EXT2_HAS_INCOMPAT_FEATURE(new_dir->i_sb,
861 EXT2_FEATURE_INCOMPAT_FILETYPE))
862 new_de->file_type = old_de->file_type;
864 ext2_delete_entry (old_de, old_bh);
866 old_dir->i_version = ++event;
867 if (new_inode) {
868 new_inode->i_nlink--;
869 new_inode->i_ctime = CURRENT_TIME;
870 mark_inode_dirty(new_inode);
872 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
873 old_dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
874 mark_inode_dirty(old_dir);
875 if (dir_bh) {
876 PARENT_INO(dir_bh->b_data) = le32_to_cpu(new_dir->i_ino);
877 mark_buffer_dirty(dir_bh, 1);
878 old_dir->i_nlink--;
879 mark_inode_dirty(old_dir);
880 if (new_inode) {
881 new_inode->i_nlink--;
882 mark_inode_dirty(new_inode);
883 } else {
884 new_dir->i_nlink++;
885 new_dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
886 mark_inode_dirty(new_dir);
889 mark_buffer_dirty(old_bh, 1);
890 if (IS_SYNC(old_dir)) {
891 ll_rw_block (WRITE, 1, &old_bh);
892 wait_on_buffer (old_bh);
894 mark_buffer_dirty(new_bh, 1);
895 if (IS_SYNC(new_dir)) {
896 ll_rw_block (WRITE, 1, &new_bh);
897 wait_on_buffer (new_bh);
900 retval = 0;
902 end_rename:
903 brelse (dir_bh);
904 brelse (old_bh);
905 brelse (new_bh);
906 return retval;