Import 2.3.25pre1
[davej-history.git] / fs / ext2 / namei.c
blob8bc7532de6e169d98a52d71e3c54693d94ce8534
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 <linux/module.h>
22 #include <linux/fs.h>
23 #include <linux/locks.h>
24 #include <linux/quotaops.h>
29 * define how far ahead to read directories while searching them.
31 #define NAMEI_RA_CHUNKS 2
32 #define NAMEI_RA_BLOCKS 4
33 #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
34 #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
37 * NOTE! unlike strncmp, ext2_match returns 1 for success, 0 for failure.
39 * `len <= EXT2_NAME_LEN' is guaranteed by caller.
40 * `de != NULL' is guaranteed by caller.
42 static inline int ext2_match (int len, const char * const name,
43 struct ext2_dir_entry_2 * de)
45 if (len != de->name_len)
46 return 0;
47 if (!de->inode)
48 return 0;
49 return !memcmp(name, de->name, len);
53 * ext2_find_entry()
55 * finds an entry in the specified directory with the wanted name. It
56 * returns the cache buffer in which the entry was found, and the entry
57 * itself (as a parameter - res_dir). It does NOT read the inode of the
58 * entry - you'll have to do that yourself if you want to.
60 static struct buffer_head * ext2_find_entry (struct inode * dir,
61 const char * const name, int namelen,
62 struct ext2_dir_entry_2 ** res_dir)
64 struct super_block * sb;
65 struct buffer_head * bh_use[NAMEI_RA_SIZE];
66 struct buffer_head * bh_read[NAMEI_RA_SIZE];
67 unsigned long offset;
68 int block, toread, i, err;
70 *res_dir = NULL;
71 sb = dir->i_sb;
73 if (namelen > EXT2_NAME_LEN)
74 return NULL;
76 memset (bh_use, 0, sizeof (bh_use));
77 toread = 0;
78 for (block = 0; block < NAMEI_RA_SIZE; ++block) {
79 struct buffer_head * bh;
81 if ((block << EXT2_BLOCK_SIZE_BITS (sb)) >= dir->i_size)
82 break;
83 bh = ext2_getblk (dir, block, 0, &err);
84 bh_use[block] = bh;
85 if (bh && !buffer_uptodate(bh))
86 bh_read[toread++] = bh;
89 for (block = 0, offset = 0; offset < dir->i_size; block++) {
90 struct buffer_head * bh;
91 struct ext2_dir_entry_2 * de;
92 char * dlimit;
94 if ((block % NAMEI_RA_BLOCKS) == 0 && toread) {
95 ll_rw_block (READ, toread, bh_read);
96 toread = 0;
98 bh = bh_use[block % NAMEI_RA_SIZE];
99 if (!bh) {
100 #if 0
101 ext2_error (sb, "ext2_find_entry",
102 "directory #%lu contains a hole at offset %lu",
103 dir->i_ino, offset);
104 #endif
105 offset += sb->s_blocksize;
106 continue;
108 wait_on_buffer (bh);
109 if (!buffer_uptodate(bh)) {
111 * read error: all bets are off
113 break;
116 de = (struct ext2_dir_entry_2 *) bh->b_data;
117 dlimit = bh->b_data + sb->s_blocksize;
118 while ((char *) de < dlimit) {
119 /* this code is executed quadratically often */
120 /* do minimal checking `by hand' */
121 int de_len;
123 if ((char *) de + namelen <= dlimit &&
124 ext2_match (namelen, name, de)) {
125 /* found a match -
126 just to be sure, do a full check */
127 if (!ext2_check_dir_entry("ext2_find_entry",
128 dir, de, bh, offset))
129 goto failure;
130 for (i = 0; i < NAMEI_RA_SIZE; ++i) {
131 if (bh_use[i] != bh)
132 brelse (bh_use[i]);
134 *res_dir = de;
135 return bh;
137 /* prevent looping on a bad block */
138 de_len = le16_to_cpu(de->rec_len);
139 if (de_len <= 0)
140 goto failure;
141 offset += de_len;
142 de = (struct ext2_dir_entry_2 *)
143 ((char *) de + de_len);
146 brelse (bh);
147 if (((block + NAMEI_RA_SIZE) << EXT2_BLOCK_SIZE_BITS (sb)) >=
148 dir->i_size)
149 bh = NULL;
150 else
151 bh = ext2_getblk (dir, block + NAMEI_RA_SIZE, 0, &err);
152 bh_use[block % NAMEI_RA_SIZE] = bh;
153 if (bh && !buffer_uptodate(bh))
154 bh_read[toread++] = bh;
157 failure:
158 for (i = 0; i < NAMEI_RA_SIZE; ++i)
159 brelse (bh_use[i]);
160 return NULL;
163 struct dentry *ext2_lookup(struct inode * dir, struct dentry *dentry)
165 struct inode * inode;
166 struct ext2_dir_entry_2 * de;
167 struct buffer_head * bh;
169 if (dentry->d_name.len > EXT2_NAME_LEN)
170 return ERR_PTR(-ENAMETOOLONG);
172 bh = ext2_find_entry (dir, dentry->d_name.name, dentry->d_name.len, &de);
173 inode = NULL;
174 if (bh) {
175 unsigned long ino = le32_to_cpu(de->inode);
176 brelse (bh);
177 inode = iget(dir->i_sb, ino);
179 if (!inode)
180 return ERR_PTR(-EACCES);
182 d_add(dentry, inode);
183 return NULL;
187 * ext2_add_entry()
189 * adds a file entry to the specified directory, using the same
190 * semantics as ext2_find_entry(). It returns NULL if it failed.
192 * NOTE!! The inode part of 'de' is left at 0 - which means you
193 * may not sleep between calling this and putting something into
194 * the entry, as someone else might have used it while you slept.
196 static struct buffer_head * ext2_add_entry (struct inode * dir,
197 const char * name, int namelen,
198 struct ext2_dir_entry_2 ** res_dir,
199 int *err)
201 unsigned long offset;
202 unsigned short rec_len;
203 struct buffer_head * bh;
204 struct ext2_dir_entry_2 * de, * de1;
205 struct super_block * sb;
207 *err = -EINVAL;
208 *res_dir = NULL;
209 if (!dir || !dir->i_nlink)
210 return NULL;
211 sb = dir->i_sb;
213 if (!namelen)
214 return NULL;
216 * Is this a busy deleted directory? Can't create new files if so
218 if (dir->i_size == 0)
220 *err = -ENOENT;
221 return NULL;
223 bh = ext2_bread (dir, 0, 0, err);
224 if (!bh)
225 return NULL;
226 rec_len = EXT2_DIR_REC_LEN(namelen);
227 offset = 0;
228 de = (struct ext2_dir_entry_2 *) bh->b_data;
229 *err = -ENOSPC;
230 while (1) {
231 if ((char *)de >= sb->s_blocksize + bh->b_data) {
232 brelse (bh);
233 bh = NULL;
234 bh = ext2_bread (dir, offset >> EXT2_BLOCK_SIZE_BITS(sb), 1, err);
235 if (!bh)
236 return NULL;
237 if (dir->i_size <= offset) {
238 if (dir->i_size == 0) {
239 *err = -ENOENT;
240 return NULL;
243 ext2_debug ("creating next block\n");
245 de = (struct ext2_dir_entry_2 *) bh->b_data;
246 de->inode = 0;
247 de->rec_len = le16_to_cpu(sb->s_blocksize);
248 dir->i_size = offset + sb->s_blocksize;
249 dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
250 mark_inode_dirty(dir);
251 } else {
253 ext2_debug ("skipping to next block\n");
255 de = (struct ext2_dir_entry_2 *) bh->b_data;
258 if (!ext2_check_dir_entry ("ext2_add_entry", dir, de, bh,
259 offset)) {
260 *err = -ENOENT;
261 brelse (bh);
262 return NULL;
264 if (ext2_match (namelen, name, de)) {
265 *err = -EEXIST;
266 brelse (bh);
267 return NULL;
269 if ((le32_to_cpu(de->inode) == 0 && le16_to_cpu(de->rec_len) >= rec_len) ||
270 (le16_to_cpu(de->rec_len) >= EXT2_DIR_REC_LEN(de->name_len) + rec_len)) {
271 offset += le16_to_cpu(de->rec_len);
272 if (le32_to_cpu(de->inode)) {
273 de1 = (struct ext2_dir_entry_2 *) ((char *) de +
274 EXT2_DIR_REC_LEN(de->name_len));
275 de1->rec_len = cpu_to_le16(le16_to_cpu(de->rec_len) -
276 EXT2_DIR_REC_LEN(de->name_len));
277 de->rec_len = cpu_to_le16(EXT2_DIR_REC_LEN(de->name_len));
278 de = de1;
280 de->inode = 0;
281 de->name_len = namelen;
282 de->file_type = 0;
283 memcpy (de->name, name, namelen);
285 * XXX shouldn't update any times until successful
286 * completion of syscall, but too many callers depend
287 * on this.
289 * XXX similarly, too many callers depend on
290 * ext2_new_inode() setting the times, but error
291 * recovery deletes the inode, so the worst that can
292 * happen is that the times are slightly out of date
293 * and/or different from the directory change time.
295 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
296 dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
297 mark_inode_dirty(dir);
298 dir->i_version = ++event;
299 mark_buffer_dirty(bh, 1);
300 *res_dir = de;
301 *err = 0;
302 return bh;
304 offset += le16_to_cpu(de->rec_len);
305 de = (struct ext2_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
307 brelse (bh);
308 return NULL;
312 * ext2_delete_entry deletes a directory entry by merging it with the
313 * previous entry
315 static int ext2_delete_entry (struct ext2_dir_entry_2 * dir,
316 struct buffer_head * bh)
318 struct ext2_dir_entry_2 * de, * pde;
319 int i;
321 i = 0;
322 pde = NULL;
323 de = (struct ext2_dir_entry_2 *) bh->b_data;
324 while (i < bh->b_size) {
325 if (!ext2_check_dir_entry ("ext2_delete_entry", NULL,
326 de, bh, i))
327 return -EIO;
328 if (de == dir) {
329 if (pde)
330 pde->rec_len =
331 cpu_to_le16(le16_to_cpu(pde->rec_len) +
332 le16_to_cpu(dir->rec_len));
333 else
334 dir->inode = 0;
335 return 0;
337 i += le16_to_cpu(de->rec_len);
338 pde = de;
339 de = (struct ext2_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
341 return -ENOENT;
344 static inline void ext2_set_de_type(struct super_block *sb,
345 struct ext2_dir_entry_2 *de,
346 umode_t mode) {
347 if (!EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_FILETYPE))
348 return;
349 if (S_ISCHR(mode))
350 de->file_type = EXT2_FT_CHRDEV;
351 else if (S_ISBLK(mode))
352 de->file_type = EXT2_FT_BLKDEV;
353 else if (S_ISFIFO(mode))
354 de->file_type = EXT2_FT_FIFO;
355 else if (S_ISLNK(mode))
356 de->file_type = EXT2_FT_SYMLINK;
357 else if (S_ISREG(mode))
358 de->file_type = EXT2_FT_REG_FILE;
359 else if (S_ISDIR(mode))
360 de->file_type = EXT2_FT_DIR;
364 * By the time this is called, we already have created
365 * the directory cache entry for the new file, but it
366 * is so far negative - it has no inode.
368 * If the create succeeds, we fill in the inode information
369 * with d_instantiate().
371 int ext2_create (struct inode * dir, struct dentry * dentry, int mode)
373 struct inode * inode;
374 struct buffer_head * bh;
375 struct ext2_dir_entry_2 * de;
376 int err = -EIO;
379 * N.B. Several error exits in ext2_new_inode don't set err.
381 inode = ext2_new_inode (dir, mode, &err);
382 if (!inode)
383 return err;
385 inode->i_op = &ext2_file_inode_operations;
386 inode->i_mode = mode;
387 mark_inode_dirty(inode);
388 bh = ext2_add_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, &err);
389 if (!bh) {
390 inode->i_nlink--;
391 mark_inode_dirty(inode);
392 iput (inode);
393 return err;
395 de->inode = cpu_to_le32(inode->i_ino);
396 ext2_set_de_type(dir->i_sb, de, S_IFREG);
397 dir->i_version = ++event;
398 mark_buffer_dirty(bh, 1);
399 if (IS_SYNC(dir)) {
400 ll_rw_block (WRITE, 1, &bh);
401 wait_on_buffer (bh);
403 brelse (bh);
404 d_instantiate(dentry, inode);
405 return 0;
408 int ext2_mknod (struct inode * dir, struct dentry *dentry, int mode, int rdev)
410 struct inode * inode;
411 struct buffer_head * bh;
412 struct ext2_dir_entry_2 * de;
413 int err = -EIO;
415 inode = ext2_new_inode (dir, mode, &err);
416 if (!inode)
417 goto out;
419 inode->i_uid = current->fsuid;
420 init_special_inode(inode, mode, rdev);
421 bh = ext2_add_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, &err);
422 if (!bh)
423 goto out_no_entry;
424 de->inode = cpu_to_le32(inode->i_ino);
425 dir->i_version = ++event;
426 ext2_set_de_type(dir->i_sb, de, inode->i_mode);
427 mark_inode_dirty(inode);
428 mark_buffer_dirty(bh, 1);
429 if (IS_SYNC(dir)) {
430 ll_rw_block (WRITE, 1, &bh);
431 wait_on_buffer (bh);
433 d_instantiate(dentry, inode);
434 brelse(bh);
435 err = 0;
436 out:
437 return err;
439 out_no_entry:
440 inode->i_nlink--;
441 mark_inode_dirty(inode);
442 iput(inode);
443 goto out;
446 int ext2_mkdir(struct inode * dir, struct dentry * dentry, int mode)
448 struct inode * inode;
449 struct buffer_head * bh, * dir_block;
450 struct ext2_dir_entry_2 * de;
451 int err;
453 err = -EMLINK;
454 if (dir->i_nlink >= EXT2_LINK_MAX)
455 goto out;
457 err = -EIO;
458 inode = ext2_new_inode (dir, S_IFDIR, &err);
459 if (!inode)
460 goto out;
462 inode->i_op = &ext2_dir_inode_operations;
463 inode->i_size = inode->i_sb->s_blocksize;
464 inode->i_blocks = 0;
465 dir_block = ext2_bread (inode, 0, 1, &err);
466 if (!dir_block) {
467 inode->i_nlink--; /* is this nlink == 0? */
468 mark_inode_dirty(inode);
469 iput (inode);
470 return err;
472 de = (struct ext2_dir_entry_2 *) dir_block->b_data;
473 de->inode = cpu_to_le32(inode->i_ino);
474 de->name_len = 1;
475 de->rec_len = cpu_to_le16(EXT2_DIR_REC_LEN(de->name_len));
476 strcpy (de->name, ".");
477 ext2_set_de_type(dir->i_sb, de, S_IFDIR);
478 de = (struct ext2_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
479 de->inode = cpu_to_le32(dir->i_ino);
480 de->rec_len = cpu_to_le16(inode->i_sb->s_blocksize - EXT2_DIR_REC_LEN(1));
481 de->name_len = 2;
482 strcpy (de->name, "..");
483 ext2_set_de_type(dir->i_sb, de, S_IFDIR);
484 inode->i_nlink = 2;
485 mark_buffer_dirty(dir_block, 1);
486 brelse (dir_block);
487 inode->i_mode = S_IFDIR | mode;
488 if (dir->i_mode & S_ISGID)
489 inode->i_mode |= S_ISGID;
490 mark_inode_dirty(inode);
491 bh = ext2_add_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, &err);
492 if (!bh)
493 goto out_no_entry;
494 de->inode = cpu_to_le32(inode->i_ino);
495 ext2_set_de_type(dir->i_sb, de, S_IFDIR);
496 dir->i_version = ++event;
497 mark_buffer_dirty(bh, 1);
498 if (IS_SYNC(dir)) {
499 ll_rw_block (WRITE, 1, &bh);
500 wait_on_buffer (bh);
502 dir->i_nlink++;
503 dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
504 mark_inode_dirty(dir);
505 d_instantiate(dentry, inode);
506 brelse (bh);
507 err = 0;
508 out:
509 return err;
511 out_no_entry:
512 inode->i_nlink = 0;
513 mark_inode_dirty(inode);
514 iput (inode);
515 goto out;
519 * routine to check that the specified directory is empty (for rmdir)
521 static int empty_dir (struct inode * inode)
523 unsigned long offset;
524 struct buffer_head * bh;
525 struct ext2_dir_entry_2 * de, * de1;
526 struct super_block * sb;
527 int err;
529 sb = inode->i_sb;
530 if (inode->i_size < EXT2_DIR_REC_LEN(1) + EXT2_DIR_REC_LEN(2) ||
531 !(bh = ext2_bread (inode, 0, 0, &err))) {
532 ext2_warning (inode->i_sb, "empty_dir",
533 "bad directory (dir #%lu) - no data block",
534 inode->i_ino);
535 return 1;
537 de = (struct ext2_dir_entry_2 *) bh->b_data;
538 de1 = (struct ext2_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
539 if (le32_to_cpu(de->inode) != inode->i_ino || !le32_to_cpu(de1->inode) ||
540 strcmp (".", de->name) || strcmp ("..", de1->name)) {
541 ext2_warning (inode->i_sb, "empty_dir",
542 "bad directory (dir #%lu) - no `.' or `..'",
543 inode->i_ino);
544 brelse (bh);
545 return 1;
547 offset = le16_to_cpu(de->rec_len) + le16_to_cpu(de1->rec_len);
548 de = (struct ext2_dir_entry_2 *) ((char *) de1 + le16_to_cpu(de1->rec_len));
549 while (offset < inode->i_size ) {
550 if (!bh || (void *) de >= (void *) (bh->b_data + sb->s_blocksize)) {
551 brelse (bh);
552 bh = ext2_bread (inode, offset >> EXT2_BLOCK_SIZE_BITS(sb), 0, &err);
553 if (!bh) {
554 #if 0
555 ext2_error (sb, "empty_dir",
556 "directory #%lu contains a hole at offset %lu",
557 inode->i_ino, offset);
558 #endif
559 offset += sb->s_blocksize;
560 continue;
562 de = (struct ext2_dir_entry_2 *) bh->b_data;
564 if (!ext2_check_dir_entry ("empty_dir", inode, de, bh,
565 offset)) {
566 brelse (bh);
567 return 1;
569 if (le32_to_cpu(de->inode)) {
570 brelse (bh);
571 return 0;
573 offset += le16_to_cpu(de->rec_len);
574 de = (struct ext2_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
576 brelse (bh);
577 return 1;
580 int ext2_rmdir (struct inode * dir, struct dentry *dentry)
582 int retval;
583 struct inode * inode;
584 struct buffer_head * bh;
585 struct ext2_dir_entry_2 * de;
587 retval = -ENOENT;
588 bh = ext2_find_entry (dir, dentry->d_name.name, dentry->d_name.len, &de);
589 if (!bh)
590 goto end_rmdir;
592 inode = dentry->d_inode;
593 DQUOT_INIT(inode);
595 retval = -EIO;
596 if (le32_to_cpu(de->inode) != inode->i_ino)
597 goto end_rmdir;
599 retval = -ENOTEMPTY;
600 if (!empty_dir (inode))
601 goto end_rmdir;
603 retval = ext2_delete_entry (de, bh);
604 dir->i_version = ++event;
605 if (retval)
606 goto end_rmdir;
607 mark_buffer_dirty(bh, 1);
608 if (IS_SYNC(dir)) {
609 ll_rw_block (WRITE, 1, &bh);
610 wait_on_buffer (bh);
612 if (inode->i_nlink != 2)
613 ext2_warning (inode->i_sb, "ext2_rmdir",
614 "empty directory has nlink!=2 (%d)",
615 inode->i_nlink);
616 inode->i_version = ++event;
617 inode->i_nlink = 0;
618 inode->i_size = 0;
619 mark_inode_dirty(inode);
620 dir->i_nlink--;
621 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
622 dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
623 mark_inode_dirty(dir);
624 d_delete(dentry);
626 end_rmdir:
627 brelse (bh);
628 return retval;
631 int ext2_unlink(struct inode * dir, struct dentry *dentry)
633 int retval;
634 struct inode * inode;
635 struct buffer_head * bh;
636 struct ext2_dir_entry_2 * de;
638 retval = -ENOENT;
639 bh = ext2_find_entry (dir, dentry->d_name.name, dentry->d_name.len, &de);
640 if (!bh)
641 goto end_unlink;
643 inode = dentry->d_inode;
644 DQUOT_INIT(inode);
646 retval = -EIO;
647 if (le32_to_cpu(de->inode) != inode->i_ino)
648 goto end_unlink;
650 if (!inode->i_nlink) {
651 ext2_warning (inode->i_sb, "ext2_unlink",
652 "Deleting nonexistent file (%lu), %d",
653 inode->i_ino, inode->i_nlink);
654 inode->i_nlink = 1;
656 retval = ext2_delete_entry (de, bh);
657 if (retval)
658 goto end_unlink;
659 dir->i_version = ++event;
660 mark_buffer_dirty(bh, 1);
661 if (IS_SYNC(dir)) {
662 ll_rw_block (WRITE, 1, &bh);
663 wait_on_buffer (bh);
665 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
666 dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
667 mark_inode_dirty(dir);
668 inode->i_nlink--;
669 mark_inode_dirty(inode);
670 inode->i_ctime = dir->i_ctime;
671 retval = 0;
672 d_delete(dentry); /* This also frees the inode */
674 end_unlink:
675 brelse (bh);
676 return retval;
679 int ext2_symlink (struct inode * dir, struct dentry *dentry, const char * symname)
681 struct ext2_dir_entry_2 * de;
682 struct inode * inode;
683 struct buffer_head * bh = NULL, * name_block = NULL;
684 char * link;
685 int i, l, err = -EIO;
686 char c;
688 if (!(inode = ext2_new_inode (dir, S_IFLNK, &err))) {
689 return err;
691 inode->i_mode = S_IFLNK | S_IRWXUGO;
692 inode->i_op = &ext2_symlink_inode_operations;
693 for (l = 0; l < inode->i_sb->s_blocksize - 1 &&
694 symname [l]; l++)
696 if (l >= sizeof (inode->u.ext2_i.i_data)) {
698 ext2_debug ("l=%d, normal symlink\n", l);
700 name_block = ext2_bread (inode, 0, 1, &err);
701 if (!name_block) {
702 inode->i_nlink--;
703 mark_inode_dirty(inode);
704 iput (inode);
705 return err;
707 link = name_block->b_data;
708 } else {
709 link = (char *) inode->u.ext2_i.i_data;
711 ext2_debug ("l=%d, fast symlink\n", l);
714 i = 0;
715 while (i < inode->i_sb->s_blocksize - 1 && (c = *(symname++)))
716 link[i++] = c;
717 link[i] = 0;
718 if (name_block) {
719 mark_buffer_dirty(name_block, 1);
720 brelse (name_block);
722 inode->i_size = i;
723 mark_inode_dirty(inode);
725 bh = ext2_add_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, &err);
726 if (!bh)
727 goto out_no_entry;
728 de->inode = cpu_to_le32(inode->i_ino);
729 ext2_set_de_type(dir->i_sb, de, S_IFLNK);
730 dir->i_version = ++event;
731 mark_buffer_dirty(bh, 1);
732 if (IS_SYNC(dir)) {
733 ll_rw_block (WRITE, 1, &bh);
734 wait_on_buffer (bh);
736 brelse (bh);
737 d_instantiate(dentry, inode);
738 err = 0;
739 out:
740 return err;
742 out_no_entry:
743 inode->i_nlink--;
744 mark_inode_dirty(inode);
745 iput (inode);
746 goto out;
749 int ext2_link (struct dentry * old_dentry,
750 struct inode * dir, struct dentry *dentry)
752 struct inode *inode = old_dentry->d_inode;
753 struct ext2_dir_entry_2 * de;
754 struct buffer_head * bh;
755 int err;
757 if (S_ISDIR(inode->i_mode))
758 return -EPERM;
760 if (inode->i_nlink >= EXT2_LINK_MAX)
761 return -EMLINK;
763 bh = ext2_add_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, &err);
764 if (!bh)
765 return err;
767 de->inode = cpu_to_le32(inode->i_ino);
768 ext2_set_de_type(dir->i_sb, de, inode->i_mode);
769 dir->i_version = ++event;
770 mark_buffer_dirty(bh, 1);
771 if (IS_SYNC(dir)) {
772 ll_rw_block (WRITE, 1, &bh);
773 wait_on_buffer (bh);
775 brelse (bh);
776 inode->i_nlink++;
777 inode->i_ctime = CURRENT_TIME;
778 mark_inode_dirty(inode);
779 inode->i_count++;
780 d_instantiate(dentry, inode);
781 return 0;
784 #define PARENT_INO(buffer) \
785 ((struct ext2_dir_entry_2 *) ((char *) buffer + \
786 le16_to_cpu(((struct ext2_dir_entry_2 *) buffer)->rec_len)))->inode
789 * Anybody can rename anything with this: the permission checks are left to the
790 * higher-level routines.
792 int ext2_rename (struct inode * old_dir, struct dentry *old_dentry,
793 struct inode * new_dir,struct dentry *new_dentry)
795 struct inode * old_inode, * new_inode;
796 struct buffer_head * old_bh, * new_bh, * dir_bh;
797 struct ext2_dir_entry_2 * old_de, * new_de;
798 int retval;
800 old_bh = new_bh = dir_bh = NULL;
802 old_bh = ext2_find_entry (old_dir, old_dentry->d_name.name, old_dentry->d_name.len, &old_de);
804 * Check for inode number is _not_ due to possible IO errors.
805 * We might rmdir the source, keep it as pwd of some process
806 * and merrily kill the link to whatever was created under the
807 * same name. Goodbye sticky bit ;-<
809 old_inode = old_dentry->d_inode;
810 retval = -ENOENT;
811 if (!old_bh || le32_to_cpu(old_de->inode) != old_inode->i_ino)
812 goto end_rename;
814 new_inode = new_dentry->d_inode;
815 new_bh = ext2_find_entry (new_dir, new_dentry->d_name.name,
816 new_dentry->d_name.len, &new_de);
817 if (new_bh) {
818 if (!new_inode) {
819 brelse (new_bh);
820 new_bh = NULL;
821 } else {
822 DQUOT_INIT(new_inode);
825 if (S_ISDIR(old_inode->i_mode)) {
826 if (new_inode) {
827 retval = -ENOTEMPTY;
828 if (!empty_dir (new_inode))
829 goto end_rename;
831 retval = -EIO;
832 dir_bh = ext2_bread (old_inode, 0, 0, &retval);
833 if (!dir_bh)
834 goto end_rename;
835 if (le32_to_cpu(PARENT_INO(dir_bh->b_data)) != old_dir->i_ino)
836 goto end_rename;
837 retval = -EMLINK;
838 if (!new_inode && new_dir!=old_dir &&
839 new_dir->i_nlink >= EXT2_LINK_MAX)
840 goto end_rename;
842 if (!new_bh) {
843 new_bh = ext2_add_entry (new_dir, new_dentry->d_name.name,
844 new_dentry->d_name.len, &new_de,
845 &retval);
846 if (!new_bh)
847 goto end_rename;
849 new_dir->i_version = ++event;
852 * ok, that's it
854 new_de->inode = le32_to_cpu(old_inode->i_ino);
855 if (EXT2_HAS_INCOMPAT_FEATURE(new_dir->i_sb,
856 EXT2_FEATURE_INCOMPAT_FILETYPE))
857 new_de->file_type = old_de->file_type;
859 ext2_delete_entry (old_de, old_bh);
861 old_dir->i_version = ++event;
862 if (new_inode) {
863 new_inode->i_nlink--;
864 new_inode->i_ctime = CURRENT_TIME;
865 mark_inode_dirty(new_inode);
867 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
868 old_dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
869 mark_inode_dirty(old_dir);
870 if (dir_bh) {
871 PARENT_INO(dir_bh->b_data) = le32_to_cpu(new_dir->i_ino);
872 mark_buffer_dirty(dir_bh, 1);
873 old_dir->i_nlink--;
874 mark_inode_dirty(old_dir);
875 if (new_inode) {
876 new_inode->i_nlink--;
877 mark_inode_dirty(new_inode);
878 } else {
879 new_dir->i_nlink++;
880 new_dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
881 mark_inode_dirty(new_dir);
884 mark_buffer_dirty(old_bh, 1);
885 if (IS_SYNC(old_dir)) {
886 ll_rw_block (WRITE, 1, &old_bh);
887 wait_on_buffer (old_bh);
889 mark_buffer_dirty(new_bh, 1);
890 if (IS_SYNC(new_dir)) {
891 ll_rw_block (WRITE, 1, &new_bh);
892 wait_on_buffer (new_bh);
895 retval = 0;
897 end_rename:
898 brelse (dir_bh);
899 brelse (old_bh);
900 brelse (new_bh);
901 return retval;