2 * linux/fs/ext2/namei.c
4 * Copyright (C) 1992, 1993, 1994, 1995
5 * Remy Card (card@masi.ibp.fr)
6 * Laboratoire MASI - Institut Blaise Pascal
7 * Universite Pierre et Marie Curie (Paris VI)
11 * linux/fs/minix/namei.c
13 * Copyright (C) 1991, 1992 Linus Torvalds
15 * Big-endian to little-endian byte-swapping/bitmaps by
16 * David S. Miller (davem@caip.rutgers.edu), 1995
17 * Directory entry file type support and forward compatibility hooks
18 * for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998
20 * Changes for use in OBDFS
21 * Copyright (c) 1999, Seagate Technology Inc.
25 #include <asm/uaccess.h>
27 #include <linux/errno.h>
29 #include <linux/ext2_fs.h>
30 #include <linux/fcntl.h>
31 #include <linux/sched.h>
32 #include <linux/stat.h>
33 #include <linux/string.h>
34 #include <linux/locks.h>
35 #include <linux/quotaops.h>
36 #include <linux/iobuf.h>
40 * define how far ahead to read directories while searching them.
42 #define NAMEI_RA_CHUNKS 2
43 #define NAMEI_RA_BLOCKS 4
44 #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
45 #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
48 * NOTE! unlike strncmp, ext2_match returns 1 for success, 0 for failure.
50 * `len <= EXT2_NAME_LEN' is guaranteed by caller.
51 * `de != NULL' is guaranteed by caller.
53 static inline int ext2_match (int len, const char * const name,
54 struct ext2_dir_entry_2 * de)
56 if (len != de->name_len)
60 return !memcmp(name, de->name, len);
66 * finds an entry in the specified directory with the wanted name. It
67 * returns the cache buffer in which the entry was found, and the entry
68 * itself (as a parameter - res_dir). It does NOT read the inode of the
69 * entry - you'll have to do that yourself if you want to.
71 struct page * obdfs_find_entry (struct inode * dir,
72 const char * const name, int namelen,
73 struct ext2_dir_entry_2 ** res_dir)
75 struct super_block * sb;
82 if (namelen > EXT2_NAME_LEN)
85 for (page = 0, offset = 0; offset < dir->i_size; page++) {
86 struct ext2_dir_entry_2 * de;
89 page = obdfs_getpage(dir, offset);
91 de = (struct ext2_dir_entry_2 *) page_address(page);
92 dlimit = (char *)page_address(page) + PAGE_SIZE;
93 while ((char *) de < dlimit) {
94 /* this code is executed quadratically often */
95 /* do minimal checking `by hand' */
98 if ((char *) de + namelen <= dlimit &&
99 ext2_match (namelen, name, de)) {
101 just to be sure, do a full check */
103 if (!ext2_check_dir_entry("ext2_find_entry",
104 dir, de, bh, offset))
110 /* prevent looping on a bad block */
111 de_len = le16_to_cpu(de->rec_len);
115 de = (struct ext2_dir_entry_2 *)
116 ((char *) de + de_len);
118 page_cache_release(page);
125 struct dentry *obdfs_lookup(struct inode * dir, struct dentry *dentry)
127 struct inode * inode;
128 struct ext2_dir_entry_2 * de;
131 if (dentry->d_name.len > EXT2_NAME_LEN)
132 return ERR_PTR(-ENAMETOOLONG);
134 page = obdfs_find_entry (dir, dentry->d_name.name, dentry->d_name.len, &de);
137 unsigned long ino = le32_to_cpu(de->inode);
138 page_cache_release(page);
139 inode = iget(dir->i_sb, ino);
142 return ERR_PTR(-EACCES);
144 d_add(dentry, inode);
153 * adds a file entry to the specified directory, using the same
154 * semantics as ext2_find_entry(). It returns NULL if it failed.
156 * NOTE!! The inode part of 'de' is left at 0 - which means you
157 * may not sleep between calling this and putting something into
158 * the entry, as someone else might have used it while you slept.
160 static struct buffer_head * ext2_add_entry (struct inode * dir,
161 const char * name, int namelen,
162 struct ext2_dir_entry_2 ** res_dir,
165 unsigned long offset;
166 unsigned short rec_len;
167 struct buffer_head * bh;
168 struct ext2_dir_entry_2 * de, * de1;
169 struct super_block * sb;
173 if (!dir || !dir->i_nlink)
180 * Is this a busy deleted directory? Can't create new files if so
182 if (dir->i_size == 0)
187 bh = ext2_bread (dir, 0, 0, err);
190 rec_len = EXT2_DIR_REC_LEN(namelen);
192 de = (struct ext2_dir_entry_2 *) bh->b_data;
195 if ((char *)de >= sb->s_blocksize + bh->b_data) {
198 bh = ext2_bread (dir, offset >> EXT2_BLOCK_SIZE_BITS(sb), 1, err);
201 if (dir->i_size <= offset) {
202 if (dir->i_size == 0) {
207 ext2_debug ("creating next block\n");
209 de = (struct ext2_dir_entry_2 *) bh->b_data;
211 de->rec_len = le16_to_cpu(sb->s_blocksize);
212 dir->i_size = offset + sb->s_blocksize;
213 dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
214 mark_inode_dirty(dir);
217 ext2_debug ("skipping to next block\n");
219 de = (struct ext2_dir_entry_2 *) bh->b_data;
222 if (!ext2_check_dir_entry ("ext2_add_entry", dir, de, bh,
228 if (ext2_match (namelen, name, de)) {
233 if ((le32_to_cpu(de->inode) == 0 && le16_to_cpu(de->rec_len) >= rec_len) ||
234 (le16_to_cpu(de->rec_len) >= EXT2_DIR_REC_LEN(de->name_len) + rec_len)) {
235 offset += le16_to_cpu(de->rec_len);
236 if (le32_to_cpu(de->inode)) {
237 de1 = (struct ext2_dir_entry_2 *) ((char *) de +
238 EXT2_DIR_REC_LEN(de->name_len));
239 de1->rec_len = cpu_to_le16(le16_to_cpu(de->rec_len) -
240 EXT2_DIR_REC_LEN(de->name_len));
241 de->rec_len = cpu_to_le16(EXT2_DIR_REC_LEN(de->name_len));
245 de->name_len = namelen;
247 memcpy (de->name, name, namelen);
249 * XXX shouldn't update any times until successful
250 * completion of syscall, but too many callers depend
253 * XXX similarly, too many callers depend on
254 * ext2_new_inode() setting the times, but error
255 * recovery deletes the inode, so the worst that can
256 * happen is that the times are slightly out of date
257 * and/or different from the directory change time.
259 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
260 dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
261 mark_inode_dirty(dir);
262 dir->i_version = ++event;
263 mark_buffer_dirty(bh, 1);
268 offset += le16_to_cpu(de->rec_len);
269 de = (struct ext2_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
276 * ext2_delete_entry deletes a directory entry by merging it with the
279 static int ext2_delete_entry (struct ext2_dir_entry_2 * dir,
280 struct buffer_head * bh)
282 struct ext2_dir_entry_2 * de, * pde;
287 de = (struct ext2_dir_entry_2 *) bh->b_data;
288 while (i < bh->b_size) {
289 if (!ext2_check_dir_entry ("ext2_delete_entry", NULL,
295 cpu_to_le16(le16_to_cpu(pde->rec_len) +
296 le16_to_cpu(dir->rec_len));
301 i += le16_to_cpu(de->rec_len);
303 de = (struct ext2_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
308 static inline void ext2_set_de_type(struct super_block *sb,
309 struct ext2_dir_entry_2 *de,
311 if (!EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_FILETYPE))
314 de->file_type = EXT2_FT_CHRDEV;
315 else if (S_ISBLK(mode))
316 de->file_type = EXT2_FT_BLKDEV;
317 else if (S_ISFIFO(mode))
318 de->file_type = EXT2_FT_FIFO;
319 else if (S_ISLNK(mode))
320 de->file_type = EXT2_FT_SYMLINK;
321 else if (S_ISREG(mode))
322 de->file_type = EXT2_FT_REG_FILE;
323 else if (S_ISDIR(mode))
324 de->file_type = EXT2_FT_DIR;
328 * By the time this is called, we already have created
329 * the directory cache entry for the new file, but it
330 * is so far negative - it has no inode.
332 * If the create succeeds, we fill in the inode information
333 * with d_instantiate().
335 int ext2_create (struct inode * dir, struct dentry * dentry, int mode)
337 struct inode * inode;
338 struct buffer_head * bh;
339 struct ext2_dir_entry_2 * de;
343 * N.B. Several error exits in ext2_new_inode don't set err.
345 inode = ext2_new_inode (dir, mode, &err);
349 inode->i_op = &ext2_file_inode_operations;
350 inode->i_mode = mode;
351 mark_inode_dirty(inode);
352 bh = ext2_add_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, &err);
355 mark_inode_dirty(inode);
359 de->inode = cpu_to_le32(inode->i_ino);
360 ext2_set_de_type(dir->i_sb, de, S_IFREG);
361 dir->i_version = ++event;
362 mark_buffer_dirty(bh, 1);
364 ll_rw_block (WRITE, 1, &bh);
368 d_instantiate(dentry, inode);
372 int ext2_mknod (struct inode * dir, struct dentry *dentry, int mode, int rdev)
374 struct inode * inode;
375 struct buffer_head * bh;
376 struct ext2_dir_entry_2 * de;
379 inode = ext2_new_inode (dir, mode, &err);
383 inode->i_uid = current->fsuid;
384 init_special_inode(inode, mode, rdev);
385 bh = ext2_add_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, &err);
388 de->inode = cpu_to_le32(inode->i_ino);
389 dir->i_version = ++event;
390 ext2_set_de_type(dir->i_sb, de, inode->i_mode);
391 mark_inode_dirty(inode);
392 mark_buffer_dirty(bh, 1);
394 ll_rw_block (WRITE, 1, &bh);
397 d_instantiate(dentry, inode);
405 mark_inode_dirty(inode);
410 int ext2_mkdir(struct inode * dir, struct dentry * dentry, int mode)
412 struct inode * inode;
413 struct buffer_head * bh, * dir_block;
414 struct ext2_dir_entry_2 * de;
418 if (dir->i_nlink >= EXT2_LINK_MAX)
422 inode = ext2_new_inode (dir, S_IFDIR, &err);
426 inode->i_op = &ext2_dir_inode_operations;
427 inode->i_size = inode->i_sb->s_blocksize;
429 dir_block = ext2_bread (inode, 0, 1, &err);
431 inode->i_nlink--; /* is this nlink == 0? */
432 mark_inode_dirty(inode);
436 de = (struct ext2_dir_entry_2 *) dir_block->b_data;
437 de->inode = cpu_to_le32(inode->i_ino);
439 de->rec_len = cpu_to_le16(EXT2_DIR_REC_LEN(de->name_len));
440 strcpy (de->name, ".");
441 ext2_set_de_type(dir->i_sb, de, S_IFDIR);
442 de = (struct ext2_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
443 de->inode = cpu_to_le32(dir->i_ino);
444 de->rec_len = cpu_to_le16(inode->i_sb->s_blocksize - EXT2_DIR_REC_LEN(1));
446 strcpy (de->name, "..");
447 ext2_set_de_type(dir->i_sb, de, S_IFDIR);
449 mark_buffer_dirty(dir_block, 1);
451 inode->i_mode = S_IFDIR | mode;
452 if (dir->i_mode & S_ISGID)
453 inode->i_mode |= S_ISGID;
454 mark_inode_dirty(inode);
455 bh = ext2_add_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, &err);
458 de->inode = cpu_to_le32(inode->i_ino);
459 ext2_set_de_type(dir->i_sb, de, S_IFDIR);
460 dir->i_version = ++event;
461 mark_buffer_dirty(bh, 1);
463 ll_rw_block (WRITE, 1, &bh);
467 dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
468 mark_inode_dirty(dir);
469 d_instantiate(dentry, inode);
477 mark_inode_dirty(inode);
483 * routine to check that the specified directory is empty (for rmdir)
485 static int empty_dir (struct inode * inode)
487 unsigned long offset;
488 struct buffer_head * bh;
489 struct ext2_dir_entry_2 * de, * de1;
490 struct super_block * sb;
494 if (inode->i_size < EXT2_DIR_REC_LEN(1) + EXT2_DIR_REC_LEN(2) ||
495 !(bh = ext2_bread (inode, 0, 0, &err))) {
496 ext2_warning (inode->i_sb, "empty_dir",
497 "bad directory (dir #%lu) - no data block",
501 de = (struct ext2_dir_entry_2 *) bh->b_data;
502 de1 = (struct ext2_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
503 if (le32_to_cpu(de->inode) != inode->i_ino || !le32_to_cpu(de1->inode) ||
504 strcmp (".", de->name) || strcmp ("..", de1->name)) {
505 ext2_warning (inode->i_sb, "empty_dir",
506 "bad directory (dir #%lu) - no `.' or `..'",
511 offset = le16_to_cpu(de->rec_len) + le16_to_cpu(de1->rec_len);
512 de = (struct ext2_dir_entry_2 *) ((char *) de1 + le16_to_cpu(de1->rec_len));
513 while (offset < inode->i_size ) {
514 if (!bh || (void *) de >= (void *) (bh->b_data + sb->s_blocksize)) {
516 bh = ext2_bread (inode, offset >> EXT2_BLOCK_SIZE_BITS(sb), 0, &err);
519 ext2_error (sb, "empty_dir",
520 "directory #%lu contains a hole at offset %lu",
521 inode->i_ino, offset);
523 offset += sb->s_blocksize;
526 de = (struct ext2_dir_entry_2 *) bh->b_data;
528 if (!ext2_check_dir_entry ("empty_dir", inode, de, bh,
533 if (le32_to_cpu(de->inode)) {
537 offset += le16_to_cpu(de->rec_len);
538 de = (struct ext2_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
544 int ext2_rmdir (struct inode * dir, struct dentry *dentry)
547 struct inode * inode;
548 struct buffer_head * bh;
549 struct ext2_dir_entry_2 * de;
552 bh = ext2_find_entry (dir, dentry->d_name.name, dentry->d_name.len, &de);
556 inode = dentry->d_inode;
560 if (le32_to_cpu(de->inode) != inode->i_ino)
564 if (!empty_dir (inode))
567 retval = ext2_delete_entry (de, bh);
568 dir->i_version = ++event;
571 mark_buffer_dirty(bh, 1);
573 ll_rw_block (WRITE, 1, &bh);
576 if (inode->i_nlink != 2)
577 ext2_warning (inode->i_sb, "ext2_rmdir",
578 "empty directory has nlink!=2 (%d)",
580 inode->i_version = ++event;
583 mark_inode_dirty(inode);
585 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
586 dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
587 mark_inode_dirty(dir);
595 int ext2_unlink(struct inode * dir, struct dentry *dentry)
598 struct inode * inode;
599 struct buffer_head * bh;
600 struct ext2_dir_entry_2 * de;
603 bh = ext2_find_entry (dir, dentry->d_name.name, dentry->d_name.len, &de);
607 inode = dentry->d_inode;
611 if (le32_to_cpu(de->inode) != inode->i_ino)
614 if (!inode->i_nlink) {
615 ext2_warning (inode->i_sb, "ext2_unlink",
616 "Deleting nonexistent file (%lu), %d",
617 inode->i_ino, inode->i_nlink);
620 retval = ext2_delete_entry (de, bh);
623 dir->i_version = ++event;
624 mark_buffer_dirty(bh, 1);
626 ll_rw_block (WRITE, 1, &bh);
629 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
630 dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
631 mark_inode_dirty(dir);
633 mark_inode_dirty(inode);
634 inode->i_ctime = dir->i_ctime;
636 d_delete(dentry); /* This also frees the inode */
643 int ext2_symlink (struct inode * dir, struct dentry *dentry, const char * symname)
645 struct ext2_dir_entry_2 * de;
646 struct inode * inode;
647 struct buffer_head * bh = NULL, * name_block = NULL;
649 int i, l, err = -EIO;
652 if (!(inode = ext2_new_inode (dir, S_IFLNK, &err))) {
655 inode->i_mode = S_IFLNK | S_IRWXUGO;
656 inode->i_op = &ext2_symlink_inode_operations;
657 for (l = 0; l < inode->i_sb->s_blocksize - 1 &&
660 if (l >= sizeof (inode->u.ext2_i.i_data)) {
662 ext2_debug ("l=%d, normal symlink\n", l);
664 name_block = ext2_bread (inode, 0, 1, &err);
667 mark_inode_dirty(inode);
671 link = name_block->b_data;
673 link = (char *) inode->u.ext2_i.i_data;
675 ext2_debug ("l=%d, fast symlink\n", l);
679 while (i < inode->i_sb->s_blocksize - 1 && (c = *(symname++)))
683 mark_buffer_dirty(name_block, 1);
687 mark_inode_dirty(inode);
689 bh = ext2_add_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, &err);
692 de->inode = cpu_to_le32(inode->i_ino);
693 ext2_set_de_type(dir->i_sb, de, S_IFLNK);
694 dir->i_version = ++event;
695 mark_buffer_dirty(bh, 1);
697 ll_rw_block (WRITE, 1, &bh);
701 d_instantiate(dentry, inode);
708 mark_inode_dirty(inode);
713 int ext2_link (struct dentry * old_dentry,
714 struct inode * dir, struct dentry *dentry)
716 struct inode *inode = old_dentry->d_inode;
717 struct ext2_dir_entry_2 * de;
718 struct buffer_head * bh;
721 if (S_ISDIR(inode->i_mode))
724 if (inode->i_nlink >= EXT2_LINK_MAX)
727 bh = ext2_add_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, &err);
731 de->inode = cpu_to_le32(inode->i_ino);
732 ext2_set_de_type(dir->i_sb, de, inode->i_mode);
733 dir->i_version = ++event;
734 mark_buffer_dirty(bh, 1);
736 ll_rw_block (WRITE, 1, &bh);
741 inode->i_ctime = CURRENT_TIME;
742 mark_inode_dirty(inode);
744 d_instantiate(dentry, inode);
748 #define PARENT_INO(buffer) \
749 ((struct ext2_dir_entry_2 *) ((char *) buffer + \
750 le16_to_cpu(((struct ext2_dir_entry_2 *) buffer)->rec_len)))->inode
753 * Anybody can rename anything with this: the permission checks are left to the
754 * higher-level routines.
756 int ext2_rename (struct inode * old_dir, struct dentry *old_dentry,
757 struct inode * new_dir,struct dentry *new_dentry)
759 struct inode * old_inode, * new_inode;
760 struct buffer_head * old_bh, * new_bh, * dir_bh;
761 struct ext2_dir_entry_2 * old_de, * new_de;
764 old_bh = new_bh = dir_bh = NULL;
766 old_bh = ext2_find_entry (old_dir, old_dentry->d_name.name, old_dentry->d_name.len, &old_de);
768 * Check for inode number is _not_ due to possible IO errors.
769 * We might rmdir the source, keep it as pwd of some process
770 * and merrily kill the link to whatever was created under the
771 * same name. Goodbye sticky bit ;-<
773 old_inode = old_dentry->d_inode;
775 if (!old_bh || le32_to_cpu(old_de->inode) != old_inode->i_ino)
778 new_inode = new_dentry->d_inode;
779 new_bh = ext2_find_entry (new_dir, new_dentry->d_name.name,
780 new_dentry->d_name.len, &new_de);
786 DQUOT_INIT(new_inode);
789 if (S_ISDIR(old_inode->i_mode)) {
792 if (!empty_dir (new_inode))
796 dir_bh = ext2_bread (old_inode, 0, 0, &retval);
799 if (le32_to_cpu(PARENT_INO(dir_bh->b_data)) != old_dir->i_ino)
802 if (!new_inode && new_dir!=old_dir &&
803 new_dir->i_nlink >= EXT2_LINK_MAX)
807 new_bh = ext2_add_entry (new_dir, new_dentry->d_name.name,
808 new_dentry->d_name.len, &new_de,
813 new_dir->i_version = ++event;
818 new_de->inode = le32_to_cpu(old_inode->i_ino);
819 if (EXT2_HAS_INCOMPAT_FEATURE(new_dir->i_sb,
820 EXT2_FEATURE_INCOMPAT_FILETYPE))
821 new_de->file_type = old_de->file_type;
823 ext2_delete_entry (old_de, old_bh);
825 old_dir->i_version = ++event;
827 new_inode->i_nlink--;
828 new_inode->i_ctime = CURRENT_TIME;
829 mark_inode_dirty(new_inode);
831 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
832 old_dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
833 mark_inode_dirty(old_dir);
835 PARENT_INO(dir_bh->b_data) = le32_to_cpu(new_dir->i_ino);
836 mark_buffer_dirty(dir_bh, 1);
838 mark_inode_dirty(old_dir);
840 new_inode->i_nlink--;
841 mark_inode_dirty(new_inode);
844 new_dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
845 mark_inode_dirty(new_dir);
848 mark_buffer_dirty(old_bh, 1);
849 if (IS_SYNC(old_dir)) {
850 ll_rw_block (WRITE, 1, &old_bh);
851 wait_on_buffer (old_bh);
853 mark_buffer_dirty(new_bh, 1);
854 if (IS_SYNC(new_dir)) {
855 ll_rw_block (WRITE, 1, &new_bh);
856 wait_on_buffer (new_bh);