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>
38 #include <linux/obd_support.h>
39 #include <linux/obdfs.h>
42 * define how far ahead to read directories while searching them.
44 #define NAMEI_RA_CHUNKS 2
45 #define NAMEI_RA_BLOCKS 4
46 #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
47 #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
50 * NOTE! unlike strncmp, ext2_match returns 1 for success, 0 for failure.
52 * `len <= EXT2_NAME_LEN' is guaranteed by caller.
53 * `de != NULL' is guaranteed by caller.
55 static inline int ext2_match (int len, const char * const name,
56 struct ext2_dir_entry_2 * de)
58 if (len != de->name_len)
62 return !memcmp(name, de->name, len);
68 * finds an entry in the specified directory with the wanted name. It
69 * returns the cache buffer in which the entry was found, and the entry
70 * itself (as a parameter - res_dir). It does NOT read the inode of the
71 * entry - you'll have to do that yourself if you want to.
73 struct page * obdfs_find_entry (struct inode * dir,
74 const char * const name, int namelen,
75 struct ext2_dir_entry_2 ** res_dir, int lock)
77 struct super_block * sb;
81 CDEBUG(D_INODE, "find entry for %*s\n", namelen, name);
86 if (namelen > EXT2_NAME_LEN)
89 CDEBUG(D_INODE, "dirsize is %ld\n", dir->i_size);
93 while ( offset < dir->i_size ) {
94 struct ext2_dir_entry_2 * de;
97 page = obdfs_getpage(dir, offset, 0, lock);
100 CDEBUG(D_INODE, "No page, offset %lx\n", offset);
104 de = (struct ext2_dir_entry_2 *) page_address(page);
105 dlimit = (char *)page_address(page) + PAGE_SIZE;
106 while ((char *) de < dlimit) {
107 /* this code is executed quadratically often */
108 /* do minimal checking `by hand' */
110 CDEBUG(D_INODE, "Entry %p len %d, page at %#lx - %#lx , offset %lx\n",
111 de, le16_to_cpu(de->rec_len), page_address(page), page_address(page) +
114 if ((char *) de + namelen <= dlimit &&
115 ext2_match (namelen, name, de)) {
117 just to be sure, do a full check */
118 if (!obdfs_check_dir_entry("ext2_find_entry",
119 dir, de, page, offset))
125 /* prevent looping on a bad block */
126 de_len = le16_to_cpu(de->rec_len);
128 CDEBUG(D_INODE, "Bad entry: at %p len %d\n",
133 de = (struct ext2_dir_entry_2 *)
134 ((char *) de + de_len);
135 CDEBUG(D_INODE, "Next while %lx\n", offset);
139 page_cache_release(page);
141 CDEBUG(D_INODE, "Next for %lx\n", offset);
145 CDEBUG(D_INODE, "Negative case, page %p, offset %ld\n", page, offset);
149 page_cache_release(page);
155 struct dentry *obdfs_lookup(struct inode * dir, struct dentry *dentry)
157 struct inode * inode;
158 struct ext2_dir_entry_2 * de;
162 if (dentry->d_name.len > EXT2_NAME_LEN)
163 return ERR_PTR(-ENAMETOOLONG);
165 page = obdfs_find_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, LOCKED);
168 CDEBUG(D_INODE, "No page - negative entry.\n");
170 CDEBUG(D_INODE, "Danger: PAGE but de.\n");
171 return ERR_PTR(-ENOENT);
174 unsigned long ino = le32_to_cpu(de->inode);
176 page_cache_release(page);
177 inode = iget(dir->i_sb, ino);
180 CDEBUG(D_INODE, "No inode.\n");
182 return ERR_PTR(-EACCES);
185 d_add(dentry, inode);
194 * adds a file entry to the specified directory, using the same
195 * semantics as ext2_find_entry(). It returns NULL if it failed.
197 * NOTE!! The inode part of 'de' is left at 0 - which means you
198 * may not sleep between calling this and putting something into
199 * the entry, as someone else might have used it while you slept.
201 * returns a locked and held page upon success
203 static struct page *obdfs_add_entry (struct inode * dir,
204 const char * name, int namelen,
205 struct ext2_dir_entry_2 ** res_dir,
208 unsigned long offset;
209 unsigned short rec_len;
211 struct ext2_dir_entry_2 * de, * de1;
212 struct super_block * sb;
217 if (!dir || !dir->i_nlink) {
228 * Is this a busy deleted directory? Can't create new files if so
230 if (dir->i_size == 0)
236 page = obdfs_getpage(dir, 0, 0, LOCKED);
241 rec_len = EXT2_DIR_REC_LEN(namelen);
242 CDEBUG(D_INODE, "reclen: %d\n", rec_len);
244 de = (struct ext2_dir_entry_2 *) page_address(page);
247 CDEBUG(D_INODE, "Considering entry at %p, (page at %#lx - %#lx), offset %ld\n",
248 de, page_address(page), page_address(page) + PAGE_SIZE, offset);
249 if ((char *)de >= PAGE_SIZE + (char *)page_address(page)) {
251 page_cache_release(page);
252 page = obdfs_getpage(dir, offset, 1, LOCKED);
257 if (dir->i_size <= offset) {
258 if (dir->i_size == 0) {
264 CDEBUG(D_INODE, "creating next block\n");
266 de = (struct ext2_dir_entry_2 *) page_address(page);
268 de->rec_len = le16_to_cpu(PAGE_SIZE);
269 dir->i_size = offset + PAGE_SIZE;
270 dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
271 mark_inode_dirty(dir);
274 ext2_debug ("skipping to next block\n");
276 de = (struct ext2_dir_entry_2 *) page_address(page);
279 CDEBUG(D_INODE, "\n");
280 if (!obdfs_check_dir_entry ("ext2_add_entry", dir, de, page,
284 page_cache_release(page);
288 CDEBUG(D_INODE, "\n");
289 if (ext2_match (namelen, name, de)) {
292 page_cache_release(page);
296 CDEBUG(D_INODE, "Testing for enough space at de %p\n", de);
297 if ( (le32_to_cpu(de->inode) == 0 &&
298 le16_to_cpu(de->rec_len) >= rec_len) ||
299 (le16_to_cpu(de->rec_len) >=
300 EXT2_DIR_REC_LEN(de->name_len) + rec_len)) {
301 offset += le16_to_cpu(de->rec_len);
302 CDEBUG(D_INODE, "Found enough space de %p, offset %#lx\n", de, offset);
303 if (le32_to_cpu(de->inode)) {
304 CDEBUG(D_INODE, "Inserting new in %p\n", de);
305 de1 = (struct ext2_dir_entry_2 *) ((char *) de +
306 EXT2_DIR_REC_LEN(de->name_len));
307 CDEBUG(D_INODE, "-- de1 at %p\n", de1);
308 de1->rec_len = cpu_to_le16(le16_to_cpu(de->rec_len) -
309 EXT2_DIR_REC_LEN(de->name_len));
310 de->rec_len = cpu_to_le16(EXT2_DIR_REC_LEN(de->name_len));
313 CDEBUG(D_INODE, "Reclen adjusted; copy %d bytes to %p, page at %#lx EOP at %#lx\n", namelen, de->name, page_address(page), page_address(page) + PAGE_SIZE);
315 de->name_len = namelen;
317 memcpy (de->name, name, namelen);
318 CDEBUG(D_INODE, "Copy done\n");
320 * XXX shouldn't update any times until successful
321 * completion of syscall, but too many callers depend
324 * XXX similarly, too many callers depend on
325 * ext2_new_inode() setting the times, but error
326 * recovery deletes the inode, so the worst that can
327 * happen is that the times are slightly out of date
328 * and/or different from the directory change time.
330 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
331 dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
332 mark_inode_dirty(dir);
333 dir->i_version = ++event;
336 PDEBUG(page, "addentry");
337 CDEBUG(D_INODE, "Regular exit from add_entry");
341 CDEBUG(D_INODE, "\n");
342 offset += le16_to_cpu(de->rec_len);
343 de = (struct ext2_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
346 CDEBUG(D_INODE, "\n");
349 page_cache_release(page);
350 PDEBUG(page, "addentry");
356 * ext2_delete_entry deletes a directory entry by merging it with the
359 static int obdfs_delete_entry (struct ext2_dir_entry_2 * dir,
362 struct ext2_dir_entry_2 * de, * pde;
367 de = (struct ext2_dir_entry_2 *) page_address(page);
368 while (i < PAGE_SIZE) {
369 if (!obdfs_check_dir_entry ("ext2_delete_entry", NULL,
375 cpu_to_le16(le16_to_cpu(pde->rec_len) +
376 le16_to_cpu(dir->rec_len));
381 i += le16_to_cpu(de->rec_len);
383 de = (struct ext2_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
389 static inline void ext2_set_de_type(struct super_block *sb,
390 struct ext2_dir_entry_2 *de,
392 if (!EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_FILETYPE))
395 de->file_type = EXT2_FT_CHRDEV;
396 else if (S_ISBLK(mode))
397 de->file_type = EXT2_FT_BLKDEV;
398 else if (S_ISFIFO(mode))
399 de->file_type = EXT2_FT_FIFO;
400 else if (S_ISLNK(mode))
401 de->file_type = EXT2_FT_SYMLINK;
402 else if (S_ISREG(mode))
403 de->file_type = EXT2_FT_REG_FILE;
404 else if (S_ISDIR(mode))
405 de->file_type = EXT2_FT_DIR;
410 * Display all dentries holding the specified inode.
412 static void show_dentry(struct list_head * dlist, int subdirs)
414 struct list_head *tmp = dlist;
416 while ((tmp = tmp->next) != dlist) {
417 struct dentry * dentry;
418 const char * unhashed = "";
421 dentry = list_entry(tmp, struct dentry, d_child);
423 dentry = list_entry(tmp, struct dentry, d_alias);
425 if (list_empty(&dentry->d_hash))
426 unhashed = "(unhashed)";
428 if ( dentry->d_inode )
429 printk("show_dentry: %s/%s, d_count=%d%s (ino %ld, dev %d, ct %d)\n",
430 dentry->d_parent->d_name.name,
431 dentry->d_name.name, dentry->d_count,
432 unhashed, dentry->d_inode->i_ino,
433 dentry->d_inode->i_dev,
434 dentry->d_inode->i_count);
436 printk("show_dentry: %s/%s, d_count=%d%s \n",
437 dentry->d_parent->d_name.name,
438 dentry->d_name.name, dentry->d_count,
445 * By the time this is called, we already have created
446 * the directory cache entry for the new file, but it
447 * is so far negative - it has no inode.
449 * If the create succeeds, we fill in the inode information
450 * with d_instantiate().
452 int obdfs_create (struct inode * dir, struct dentry * dentry, int mode)
454 struct inode * inode;
456 struct ext2_dir_entry_2 * de;
458 struct obdfs_sb_info *sbi;
463 sbi = dir->i_sb->u.generic_sbp;
465 * N.B. Several error exits in ext2_new_inode don't set err.
467 ino = sbi->osi_ops->o_create(sbi->osi_conn_info.conn_id, 0, &err);
471 inode = iget(dir->i_sb, ino);
472 if (!inode || !list_empty(&inode->i_dentry)) {
473 CDEBUG(D_INODE, "No inode, ino %ld\n", ino);
478 inode->i_op = &obdfs_inode_ops;
479 inode->i_mode = mode;
480 mark_inode_dirty(inode);
481 page = obdfs_add_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, &err);
484 mark_inode_dirty(inode);
489 de->inode = cpu_to_le32(inode->i_ino);
490 ext2_set_de_type(dir->i_sb, de, S_IFREG);
491 dir->i_version = ++event;
492 sbi->osi_ops->o_brw(WRITE, sbi->osi_conn_info.conn_id,
497 ll_rw_block (WRITE, 1, &bh);
501 page_cache_release(page);
502 d_instantiate(dentry, inode);
507 int obdfs_mknod (struct inode * dir, struct dentry *dentry, int mode, int rdev)
509 struct inode * inode;
511 struct ext2_dir_entry_2 * de;
513 struct obdfs_sb_info *sbi;
519 sbi = dir->i_sb->u.generic_sbp;
521 * N.B. Several error exits in ext2_new_inode don't set err.
523 ino = sbi->osi_ops->o_create(sbi->osi_conn_info.conn_id, 0, &err);
526 inode = iget(dir->i_sb, ino);
530 inode->i_uid = current->fsuid;
531 init_special_inode(inode, mode, rdev);
532 page = obdfs_add_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, &err);
535 de->inode = cpu_to_le32(inode->i_ino);
536 dir->i_version = ++event;
537 ext2_set_de_type(dir->i_sb, de, inode->i_mode);
538 mark_inode_dirty(inode);
539 sbi->osi_ops->o_brw(WRITE, sbi->osi_conn_info.conn_id,
545 ll_rw_block (WRITE, 1, &bh);
549 d_instantiate(dentry, inode);
550 page_cache_release(page);
557 mark_inode_dirty(inode);
562 int obdfs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
564 struct inode * child;
565 struct page *page, *child_page;
566 struct ext2_dir_entry_2 * de;
567 struct obdfs_sb_info *sbi;
572 sbi = dir->i_sb->u.generic_sbp;
575 if (dir->i_nlink >= EXT2_LINK_MAX)
579 ino = iops(dir)->o_create(sbi->osi_conn_info.conn_id, 0, &err);
582 child = iget(dir->i_sb, ino);
587 child->i_op = &obdfs_inode_ops;
588 child->i_size = PAGE_SIZE;
590 child_page = obdfs_getpage(child, 0, 1, LOCKED);
592 child->i_nlink--; /* is this nlink == 0? */
593 mark_inode_dirty(child);
597 child->i_blocks = PAGE_SIZE/child->i_sb->s_blocksize;
599 /* create . and .. */
600 de = (struct ext2_dir_entry_2 *) page_address(child_page);
601 de->inode = cpu_to_le32(child->i_ino);
603 de->rec_len = cpu_to_le16(EXT2_DIR_REC_LEN(de->name_len));
604 strcpy (de->name, ".");
605 ext2_set_de_type(dir->i_sb, de, S_IFDIR);
607 de = (struct ext2_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
608 de->inode = cpu_to_le32(dir->i_ino);
609 de->rec_len = cpu_to_le16(PAGE_SIZE - EXT2_DIR_REC_LEN(1));
611 strcpy (de->name, "..");
612 ext2_set_de_type(dir->i_sb, de, S_IFDIR);
614 iops(dir)->o_brw(WRITE, sbi->osi_conn_info.conn_id, child, child_page, 1);
615 UnlockPage(child_page);
616 page_cache_release(child_page);
619 child->i_mode = S_IFDIR | mode;
620 if (dir->i_mode & S_ISGID)
621 child->i_mode |= S_ISGID;
622 mark_inode_dirty(child);
624 /* now deal with the parent */
625 page = obdfs_add_entry(dir, dentry->d_name.name, dentry->d_name.len, &de, &err);
630 de->inode = cpu_to_le32(child->i_ino);
631 ext2_set_de_type(dir->i_sb, de, S_IFDIR);
632 dir->i_version = ++event;
636 ll_rw_block (WRITE, 1, &bh);
641 dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
642 mark_inode_dirty(dir);
643 iops(dir)->o_brw(WRITE, sbi->osi_conn_info.conn_id, dir, page, 1);
645 page_cache_release(page);
646 d_instantiate(dentry, child);
654 mark_inode_dirty(child);
662 * routine to check that the specified directory is empty (for rmdir)
664 static int empty_dir (struct inode * inode)
666 unsigned long offset;
668 struct ext2_dir_entry_2 * de, * de1;
669 struct super_block * sb;
672 if (inode->i_size < EXT2_DIR_REC_LEN(1) + EXT2_DIR_REC_LEN(2) ||
673 !(page = obdfs_getpage (inode, 0, 0, LOCKED))) {
674 ext2_warning (inode->i_sb, "empty_dir",
675 "bad directory (dir #%lu) - no data block",
679 de = (struct ext2_dir_entry_2 *) page_address(page);
680 de1 = (struct ext2_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
681 if (le32_to_cpu(de->inode) != inode->i_ino || !le32_to_cpu(de1->inode) ||
682 strcmp (".", de->name) || strcmp ("..", de1->name)) {
683 ext2_warning (inode->i_sb, "empty_dir",
684 "bad directory (dir #%lu) - no `.' or `..'",
686 page_cache_release(page);
689 offset = le16_to_cpu(de->rec_len) + le16_to_cpu(de1->rec_len);
690 de = (struct ext2_dir_entry_2 *) ((char *) de1 + le16_to_cpu(de1->rec_len));
691 while (offset < inode->i_size ) {
692 if (!page || (void *) de >= (void *) (page_address(page) + PAGE_SIZE)) {
695 page_cache_release(page);
697 page = obdfs_getpage(inode, offset, 0, LOCKED);
700 ext2_error (sb, "empty_dir",
701 "directory #%lu contains a hole at offset %lu",
702 inode->i_ino, offset);
704 offset += sb->s_blocksize;
707 de = (struct ext2_dir_entry_2 *) page_address(page);
709 if (!obdfs_check_dir_entry ("empty_dir", inode, de, page,
712 page_cache_release(page);
715 if (le32_to_cpu(de->inode)) {
717 page_cache_release(page);
720 offset += le16_to_cpu(de->rec_len);
721 de = (struct ext2_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
724 page_cache_release(page);
728 int obdfs_rmdir (struct inode * dir, struct dentry *dentry)
731 struct inode * inode;
733 struct ext2_dir_entry_2 * de;
734 struct obdfs_sb_info *sbi;
737 sbi = dir->i_sb->u.generic_sbp;
740 page = obdfs_find_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, LOCKED);
744 inode = dentry->d_inode;
748 if (le32_to_cpu(de->inode) != inode->i_ino)
752 if (!empty_dir (inode))
755 retval = obdfs_delete_entry (de, page);
756 dir->i_version = ++event;
759 sbi->osi_ops->o_brw(WRITE, sbi->osi_conn_info.conn_id, dir, page, 0);
763 ll_rw_block (WRITE, 1, &bh);
767 if (inode->i_nlink != 2)
768 ext2_warning (inode->i_sb, "ext2_rmdir",
769 "empty directory has nlink!=2 (%d)",
771 inode->i_version = ++event;
774 mark_inode_dirty(inode);
776 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
777 dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
778 mark_inode_dirty(dir);
783 page_cache_release(page);
788 int obdfs_unlink(struct inode * dir, struct dentry *dentry)
791 struct inode * inode;
793 struct ext2_dir_entry_2 * de;
794 struct obdfs_sb_info *sbi;
797 sbi = dir->i_sb->u.generic_sbp;
800 page = obdfs_find_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, LOCKED);
804 inode = dentry->d_inode;
808 if (le32_to_cpu(de->inode) != inode->i_ino)
811 if (!inode->i_nlink) {
812 ext2_warning (inode->i_sb, "ext2_unlink",
813 "Deleting nonexistent file (%lu), %d",
814 inode->i_ino, inode->i_nlink);
817 retval = obdfs_delete_entry (de, page);
820 dir->i_version = ++event;
821 sbi->osi_ops->o_brw(WRITE, sbi->osi_conn_info.conn_id, dir, page, 0);
825 ll_rw_block (WRITE, 1, &bh);
829 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
830 dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
831 mark_inode_dirty(dir);
833 mark_inode_dirty(inode);
834 inode->i_ctime = dir->i_ctime;
836 d_delete(dentry); /* This also frees the inode */
840 page_cache_release(page);
845 int obdfs_symlink (struct inode * dir, struct dentry *dentry, const char * symname)
847 struct ext2_dir_entry_2 * de;
848 struct inode * inode;
849 struct page* page = NULL, * name_page = NULL;
851 int i, l, err = -EIO;
853 struct obdfs_sb_info *sbi;
858 sbi = dir->i_sb->u.generic_sbp;
860 * N.B. Several error exits in ext2_new_inode don't set err.
862 ino = sbi->osi_ops->o_create(sbi->osi_conn_info.conn_id, 0, &err);
865 inode = iget(dir->i_sb, ino);
869 inode->i_mode = S_IFLNK | S_IRWXUGO;
870 inode->i_op = &obdfs_inode_ops;
871 for (l = 0; l < inode->i_sb->s_blocksize - 1 &&
874 if (l >= sizeof (inode->u.ext2_i.i_data)) {
876 CDEBUG(D_INODE, "l=%d, normal symlink\n", l);
878 name_page = obdfs_getpage(inode, 0, 1, LOCKED);
881 mark_inode_dirty(inode);
885 link = (char *)page_address(name_page);
887 link = (char *) inode->u.ext2_i.i_data;
889 CDEBUG(D_INODE, "l=%d, fast symlink\n", l);
893 while (i < inode->i_sb->s_blocksize - 1 && (c = *(symname++)))
897 sbi->osi_ops->o_brw(WRITE, sbi->osi_conn_info.conn_id,
898 inode, name_page, 1);
899 PDEBUG(name_page, "symlink");
900 UnlockPage(name_page);
901 page_cache_release(name_page);
904 mark_inode_dirty(inode);
906 page = obdfs_add_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, &err);
909 de->inode = cpu_to_le32(inode->i_ino);
910 ext2_set_de_type(dir->i_sb, de, S_IFLNK);
911 dir->i_version = ++event;
912 sbi->osi_ops->o_brw(WRITE, sbi->osi_conn_info.conn_id,
917 ll_rw_block (WRITE, 1, &bh);
921 d_instantiate(dentry, inode);
928 mark_inode_dirty(inode);
933 int obdfs_link (struct dentry * old_dentry,
934 struct inode * dir, struct dentry *dentry)
936 struct inode *inode = old_dentry->d_inode;
937 struct ext2_dir_entry_2 * de;
940 struct obdfs_sb_info *sbi;
943 sbi = dir->i_sb->u.generic_sbp;
945 if (S_ISDIR(inode->i_mode))
948 if (inode->i_nlink >= EXT2_LINK_MAX)
951 page = obdfs_add_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, &err);
955 de->inode = cpu_to_le32(inode->i_ino);
956 ext2_set_de_type(dir->i_sb, de, inode->i_mode);
957 dir->i_version = ++event;
958 sbi->osi_ops->o_brw(WRITE, sbi->osi_conn_info.conn_id,
964 ll_rw_block (WRITE, 1, &bh);
968 page_cache_release(page);
970 inode->i_ctime = CURRENT_TIME;
971 mark_inode_dirty(inode);
973 d_instantiate(dentry, inode);
977 #define PARENT_INO(buffer) \
978 ((struct ext2_dir_entry_2 *) ((char *) buffer + \
979 le16_to_cpu(((struct ext2_dir_entry_2 *) buffer)->rec_len)))->inode
982 * Anybody can rename anything with this: the permission checks are left to the
983 * higher-level routines.
985 int obdfs_rename (struct inode * old_dir, struct dentry *old_dentry,
986 struct inode * new_dir, struct dentry *new_dentry)
988 struct inode * old_inode, * new_inode;
989 struct page * old_page, * new_page, * dir_page;
990 struct ext2_dir_entry_2 * old_de, * new_de;
992 struct obdfs_sb_info *sbi;
995 sbi = old_dir->i_sb->u.generic_sbp;
997 new_page = dir_page = NULL;
999 /* does the old entry exist? - if not get out */
1000 old_page = obdfs_find_entry (old_dir, old_dentry->d_name.name, old_dentry->d_name.len, &old_de, NOLOCK);
1001 PDEBUG(old_page, "rename - old page");
1003 * Check for inode number is _not_ due to possible IO errors.
1004 * We might rmdir the source, keep it as pwd of some process
1005 * and merrily kill the link to whatever was created under the
1006 * same name. Goodbye sticky bit ;-<
1008 old_inode = old_dentry->d_inode;
1010 if (!old_page || le32_to_cpu(old_de->inode) != old_inode->i_ino)
1013 /* find new inode */
1014 new_inode = new_dentry->d_inode;
1015 new_page = obdfs_find_entry (new_dir, new_dentry->d_name.name,
1016 new_dentry->d_name.len, &new_de, NOLOCK);
1017 PDEBUG(new_page, "rename - new page ");
1020 page_cache_release(new_page);
1023 DQUOT_INIT(new_inode);
1026 /* in this case we to check more ... */
1027 if (S_ISDIR(old_inode->i_mode)) {
1028 /* can only rename into empty new directory */
1030 retval = -ENOTEMPTY;
1031 if (!empty_dir (new_inode))
1035 dir_page= obdfs_getpage (old_inode, 0, 0, LOCKED);
1036 PDEBUG(dir_page, "rename dir page");
1040 if (le32_to_cpu(PARENT_INO(page_address(dir_page))) != old_dir->i_ino)
1043 if (!new_inode && new_dir!=old_dir &&
1044 new_dir->i_nlink >= EXT2_LINK_MAX)
1047 /* create the target dir entry */
1049 new_page = obdfs_add_entry (new_dir, new_dentry->d_name.name,
1050 new_dentry->d_name.len, &new_de,
1052 PDEBUG(new_page, "rename new page");
1056 new_dir->i_version = ++event;
1059 * remove the old entry
1061 new_de->inode = le32_to_cpu(old_inode->i_ino);
1062 if (EXT2_HAS_INCOMPAT_FEATURE(new_dir->i_sb,
1063 EXT2_FEATURE_INCOMPAT_FILETYPE))
1064 new_de->file_type = old_de->file_type;
1066 obdfs_delete_entry (old_de, old_page);
1068 old_dir->i_version = ++event;
1070 new_inode->i_nlink--;
1071 new_inode->i_ctime = CURRENT_TIME;
1072 mark_inode_dirty(new_inode);
1074 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
1075 old_dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
1076 mark_inode_dirty(old_dir);
1078 PARENT_INO(page_address(dir_page)) = le32_to_cpu(new_dir->i_ino);
1079 sbi->osi_ops->o_brw(WRITE, sbi->osi_conn_info.conn_id,
1080 old_inode, dir_page, 0);
1082 mark_inode_dirty(old_dir);
1084 new_inode->i_nlink--;
1085 mark_inode_dirty(new_inode);
1088 new_dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
1089 mark_inode_dirty(new_dir);
1092 if ( old_page != new_page ) {
1093 unsigned long offset = old_page->offset;
1094 /* lock the old_page and release unlocked copy */
1095 CDEBUG(D_INODE, "old_page at %p\n", old_page);
1096 page_cache_release(old_page);
1097 old_page = obdfs_getpage(old_dir, offset, 0, LOCKED);
1098 CDEBUG(D_INODE, "old_page at %p\n", old_page);
1099 sbi->osi_ops->o_brw(WRITE, sbi->osi_conn_info.conn_id,
1100 old_dir, old_page, 0);
1103 if (IS_SYNC(old_dir)) {
1104 ll_rw_block (WRITE, 1, &old_bh);
1105 wait_on_buffer (old_bh);
1108 sbi->osi_ops->o_brw(WRITE, sbi->osi_conn_info.conn_id,
1109 new_dir, new_page, 0);
1111 if (IS_SYNC(new_dir)) {
1112 ll_rw_block (WRITE, 1, &new_bh);
1113 wait_on_buffer (new_bh);
1120 if (old_page && PageLocked(old_page) )
1121 UnlockPage(old_page);
1123 page_cache_release(old_page);
1124 if (new_page && PageLocked(new_page) )
1125 UnlockPage(new_page);
1127 page_cache_release(new_page);
1128 if (dir_page && PageLocked(dir_page) )
1129 UnlockPage(dir_page);
1131 page_cache_release(dir_page);