3 * Directory operations for Coda filesystem
4 * Original version: (C) 1996 P. Braam and M. Callahan
5 * Rewritten for Linux 2.1. (C) 1997 Carnegie Mellon University
7 * Carnegie Mellon encourages users to contribute improvements to
8 * the Coda project. Contact Peter Braam (coda@cs.cmu.edu).
11 #include <linux/types.h>
12 #include <linux/kernel.h>
13 #include <linux/sched.h>
15 #include <linux/file.h>
16 #include <linux/stat.h>
17 #include <linux/errno.h>
18 #include <linux/locks.h>
19 #include <linux/string.h>
20 #include <linux/smp_lock.h>
22 #include <asm/uaccess.h>
24 #include <linux/coda.h>
25 #include <linux/coda_linux.h>
26 #include <linux/coda_psdev.h>
27 #include <linux/coda_fs_i.h>
28 #include <linux/coda_cache.h>
29 #include <linux/coda_proc.h>
32 static int coda_create(struct inode *dir, struct dentry *new, int mode);
33 static int coda_mknod(struct inode *dir, struct dentry *new, int mode, int rdev);
34 static struct dentry *coda_lookup(struct inode *dir, struct dentry *target);
35 static int coda_link(struct dentry *old_dentry, struct inode *dir_inode,
36 struct dentry *entry);
37 static int coda_unlink(struct inode *dir_inode, struct dentry *entry);
38 static int coda_symlink(struct inode *dir_inode, struct dentry *entry,
40 static int coda_mkdir(struct inode *dir_inode, struct dentry *entry, int mode);
41 static int coda_rmdir(struct inode *dir_inode, struct dentry *entry);
42 static int coda_rename(struct inode *old_inode, struct dentry *old_dentry,
43 struct inode *new_inode, struct dentry *new_dentry);
46 static int coda_readdir(struct file *file, void *dirent, filldir_t filldir);
49 static int coda_dentry_revalidate(struct dentry *de, int);
50 static int coda_dentry_delete(struct dentry *);
52 /* support routines */
53 static int coda_venus_readdir(struct file *filp, filldir_t filldir,
54 void *dirent, struct dentry *dir);
55 int coda_fsync(struct file *, struct dentry *dentry, int datasync);
59 struct dentry_operations coda_dentry_operations =
61 d_revalidate: coda_dentry_revalidate,
62 d_delete: coda_dentry_delete,
65 struct inode_operations coda_dir_inode_operations =
71 symlink: coda_symlink,
76 permission: coda_permission,
77 revalidate: coda_revalidate_inode,
78 setattr: coda_notify_change,
81 struct file_operations coda_dir_operations = {
82 read: generic_read_dir,
83 readdir: coda_readdir,
86 release: coda_release,
91 /* inode operations for directories */
92 /* access routines: lookup, readlink, permission */
93 static struct dentry *coda_lookup(struct inode *dir, struct dentry *entry)
95 struct inode *res_inode = NULL;
96 struct ViceFid resfid = {0,0,0};
97 int dropme = 0; /* to indicate entry should not be cached */
100 const char *name = entry->d_name.name;
101 size_t length = entry->d_name.len;
103 if ( length > CODA_MAXNAMLEN ) {
104 printk("name too long: lookup, %s (%*s)\n",
105 coda_i2s(dir), (int)length, name);
106 return ERR_PTR(-ENAMETOOLONG);
109 CDEBUG(D_INODE, "name %s, len %ld in ino %ld, fid %s\n",
110 name, (long)length, dir->i_ino, coda_i2s(dir));
112 /* control object, create inode on the fly */
113 if (coda_isroot(dir) && coda_iscontrol(name, length)) {
114 error = coda_cnode_makectl(&res_inode, dir->i_sb);
116 "Lookup on CTL object; dir ino %ld, count %d\n",
117 dir->i_ino, atomic_read(&dir->i_count));
122 error = venus_lookup(dir->i_sb, coda_i2f(dir),
123 (const char *)name, length, &type, &resfid);
127 if (type & CODA_NOCACHE) {
128 type &= (~CODA_NOCACHE);
129 CDEBUG(D_INODE, "dropme set for %s\n",
134 error = coda_cnode_make(&res_inode, &resfid, dir->i_sb);
135 if (error) return ERR_PTR(error);
136 } else if (error != -ENOENT) {
137 CDEBUG(D_INODE, "error for %s(%*s)%d\n",
138 coda_i2s(dir), (int)length, name, error);
139 return ERR_PTR(error);
141 CDEBUG(D_INODE, "lookup: %s is (%s), type %d result %d, dropme %d\n",
142 name, coda_f2s(&resfid), type, error, dropme);
146 entry->d_op = &coda_dentry_operations;
147 d_add(entry, res_inode);
150 coda_flag_inode(res_inode, C_VATTR);
156 int coda_permission(struct inode *inode, int mask)
160 coda_vfs_stat.permission++;
165 if ( coda_access_cache ) {
166 coda_permission_stat.count++;
168 if ( coda_cache_check(inode, mask) ) {
169 coda_permission_stat.hit_count++;
174 CDEBUG(D_INODE, "mask is %o\n", mask);
175 error = venus_access(inode->i_sb, coda_i2f(inode), mask);
177 CDEBUG(D_INODE, "fid: %s, ino: %ld (mask: %o) error: %d\n",
178 coda_i2s(inode), inode->i_ino, mask, error);
181 coda_cache_enter(inode, mask);
187 static inline void coda_dir_changed(struct inode *dir, int link)
189 #ifdef REQUERY_VENUS_FOR_MTIME
190 /* invalidate the directory cnode's attributes so we refetch the
191 * attributes from venus next time the inode is referenced */
192 coda_flag_inode(dir, C_VATTR);
194 /* optimistically we can also act as if our nose bleeds. The
195 * granularity of the mtime is coarse anyways so we might actually be
196 * right most of the time. Note: we only do this for directories. */
197 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
200 dir->i_nlink += link;
203 /* creation routines: create, mknod, mkdir, link, symlink */
204 static int coda_create(struct inode *dir, struct dentry *de, int mode)
207 const char *name=de->d_name.name;
208 int length=de->d_name.len;
210 struct ViceFid newfid;
211 struct coda_vattr attrs;
213 coda_vfs_stat.create++;
215 CDEBUG(D_INODE, "name: %s, length %d, mode %o\n", name, length, mode);
217 if (coda_isroot(dir) && coda_iscontrol(name, length))
220 error = venus_create(dir->i_sb, coda_i2f(dir), name, length,
221 0, mode, 0, &newfid, &attrs);
224 CDEBUG(D_INODE, "create: %s, result %d\n",
225 coda_f2s(&newfid), error);
230 inode = coda_iget(dir->i_sb, &newfid, &attrs);
231 if ( IS_ERR(inode) ) {
233 return PTR_ERR(inode);
236 /* invalidate the directory cnode's attributes */
237 coda_dir_changed(dir, 0);
238 d_instantiate(de, inode);
242 static int coda_mknod(struct inode *dir, struct dentry *de, int mode, int rdev)
245 const char *name=de->d_name.name;
246 int length=de->d_name.len;
248 struct ViceFid newfid;
249 struct coda_vattr attrs;
251 if ( coda_hasmknod == 0 )
254 coda_vfs_stat.create++;
256 CDEBUG(D_INODE, "name: %s, length %d, mode %o, rdev %x\n",
257 name, length, mode, rdev);
259 if (coda_isroot(dir) && coda_iscontrol(name, length))
262 error = venus_create(dir->i_sb, coda_i2f(dir), name, length,
263 0, mode, rdev, &newfid, &attrs);
266 CDEBUG(D_INODE, "mknod: %s, result %d\n",
267 coda_f2s(&newfid), error);
272 inode = coda_iget(dir->i_sb, &newfid, &attrs);
273 if ( IS_ERR(inode) ) {
275 return PTR_ERR(inode);
278 /* invalidate the directory cnode's attributes */
279 coda_dir_changed(dir, 0);
280 d_instantiate(de, inode);
284 static int coda_mkdir(struct inode *dir, struct dentry *de, int mode)
287 struct coda_vattr attrs;
288 const char *name = de->d_name.name;
289 int len = de->d_name.len;
291 struct ViceFid newfid;
293 coda_vfs_stat.mkdir++;
295 if (coda_isroot(dir) && coda_iscontrol(name, len))
298 CDEBUG(D_INODE, "mkdir %s (len %d) in %s, mode %o.\n",
299 name, len, coda_i2s(dir), mode);
301 attrs.va_mode = mode;
302 error = venus_mkdir(dir->i_sb, coda_i2f(dir),
303 name, len, &newfid, &attrs);
306 CDEBUG(D_INODE, "mkdir error: %s result %d\n",
307 coda_f2s(&newfid), error);
312 CDEBUG(D_INODE, "mkdir: new dir has fid %s.\n",
315 inode = coda_iget(dir->i_sb, &newfid, &attrs);
316 if ( IS_ERR(inode) ) {
318 return PTR_ERR(inode);
321 /* invalidate the directory cnode's attributes */
322 coda_dir_changed(dir, 1);
323 d_instantiate(de, inode);
327 /* try to make de an entry in dir_inodde linked to source_de */
328 static int coda_link(struct dentry *source_de, struct inode *dir_inode,
331 struct inode *inode = source_de->d_inode;
332 const char * name = de->d_name.name;
333 int len = de->d_name.len;
336 coda_vfs_stat.link++;
338 if (coda_isroot(dir_inode) && coda_iscontrol(name, len))
341 CDEBUG(D_INODE, "old: fid: %s\n", coda_i2s(inode));
342 CDEBUG(D_INODE, "directory: %s\n", coda_i2s(dir_inode));
344 error = venus_link(dir_inode->i_sb, coda_i2f(inode),
345 coda_i2f(dir_inode), (const char *)name, len);
352 coda_dir_changed(dir_inode, 0);
353 atomic_inc(&inode->i_count);
354 d_instantiate(de, inode);
358 CDEBUG(D_INODE, "link result %d\n",error);
363 static int coda_symlink(struct inode *dir_inode, struct dentry *de,
366 const char *name = de->d_name.name;
367 int len = de->d_name.len;
371 coda_vfs_stat.symlink++;
373 if (coda_isroot(dir_inode) && coda_iscontrol(name, len))
376 symlen = strlen(symname);
377 if ( symlen > CODA_MAXPATHLEN )
378 return -ENAMETOOLONG;
380 CDEBUG(D_INODE, "symname: %s, length: %d\n", symname, symlen);
383 * This entry is now negative. Since we do not create
384 * an inode for the entry we have to drop it.
387 error = venus_symlink(dir_inode->i_sb, coda_i2f(dir_inode), name, len,
390 /* mtime is no good anymore */
392 coda_dir_changed(dir_inode, 0);
394 CDEBUG(D_INODE, "in symlink result %d\n",error);
398 /* destruction routines: unlink, rmdir */
399 int coda_unlink(struct inode *dir, struct dentry *de)
402 const char *name = de->d_name.name;
403 int len = de->d_name.len;
405 coda_vfs_stat.unlink++;
407 CDEBUG(D_INODE, " %s in %s, dirino %ld\n", name ,
408 coda_i2s(dir), dir->i_ino);
410 error = venus_remove(dir->i_sb, coda_i2f(dir), name, len);
412 CDEBUG(D_INODE, "upc returned error %d\n", error);
416 coda_dir_changed(dir, 0);
417 de->d_inode->i_nlink--;
422 int coda_rmdir(struct inode *dir, struct dentry *de)
424 const char *name = de->d_name.name;
425 int len = de->d_name.len;
428 coda_vfs_stat.rmdir++;
432 error = venus_rmdir(dir->i_sb, coda_i2f(dir), name, len);
435 CDEBUG(D_INODE, "upc returned error %d\n", error);
439 coda_dir_changed(dir, -1);
440 de->d_inode->i_nlink--;
447 static int coda_rename(struct inode *old_dir, struct dentry *old_dentry,
448 struct inode *new_dir, struct dentry *new_dentry)
450 const char *old_name = old_dentry->d_name.name;
451 const char *new_name = new_dentry->d_name.name;
452 int old_length = old_dentry->d_name.len;
453 int new_length = new_dentry->d_name.len;
457 coda_vfs_stat.rename++;
459 CDEBUG(D_INODE, "old: %s, (%d length), new: %s"
460 "(%d length). old:d_count: %d, new:d_count: %d\n",
461 old_name, old_length, new_name, new_length,
462 atomic_read(&old_dentry->d_count), atomic_read(&new_dentry->d_count));
464 error = venus_rename(old_dir->i_sb, coda_i2f(old_dir),
465 coda_i2f(new_dir), old_length, new_length,
466 (const char *) old_name, (const char *)new_name);
469 if ( new_dentry->d_inode ) {
470 if ( S_ISDIR(new_dentry->d_inode->i_mode) )
473 coda_dir_changed(old_dir, -link_adjust);
474 coda_dir_changed(new_dir, link_adjust);
475 coda_flag_inode(new_dentry->d_inode, C_VATTR);
477 coda_flag_inode(old_dir, C_VATTR);
478 coda_flag_inode(new_dir, C_VATTR);
482 CDEBUG(D_INODE, "result %d\n", error);
488 /* file operations for directories */
489 int coda_readdir(struct file *coda_file, void *dirent, filldir_t filldir)
491 struct dentry *coda_dentry = coda_file->f_dentry;
492 struct coda_file_info *cfi;
493 struct file *host_file;
496 cfi = CODA_FTOC(coda_file);
497 if (!cfi || cfi->cfi_magic != CODA_MAGIC) BUG();
498 host_file = cfi->cfi_container;
500 coda_vfs_stat.readdir++;
502 down(&host_file->f_dentry->d_inode->i_sem);
503 host_file->f_pos = coda_file->f_pos;
505 if ( !host_file->f_op->readdir ) {
506 /* Venus: we must read Venus dirents from the file */
507 ret = coda_venus_readdir(host_file, filldir, dirent, coda_dentry);
509 /* potemkin case: we were handed a directory inode */
510 ret = vfs_readdir(host_file, filldir, dirent);
513 coda_file->f_pos = host_file->f_pos;
514 up(&host_file->f_dentry->d_inode->i_sem);
519 static inline unsigned int CDT2DT(unsigned char cdt)
524 case CDT_UNKNOWN: dt = DT_UNKNOWN; break;
525 case CDT_FIFO: dt = DT_FIFO; break;
526 case CDT_CHR: dt = DT_CHR; break;
527 case CDT_DIR: dt = DT_DIR; break;
528 case CDT_BLK: dt = DT_BLK; break;
529 case CDT_REG: dt = DT_REG; break;
530 case CDT_LNK: dt = DT_LNK; break;
531 case CDT_SOCK: dt = DT_SOCK; break;
532 case CDT_WHT: dt = DT_WHT; break;
533 default: dt = DT_UNKNOWN; break;
538 /* support routines */
539 static int coda_venus_readdir(struct file *filp, filldir_t filldir,
540 void *dirent, struct dentry *dir)
542 int result = 0; /* # of entries returned */
543 struct venus_dirent *vdir;
544 unsigned long vdir_size =
545 (unsigned long)(&((struct venus_dirent *)0)->d_name);
551 vdir = (struct venus_dirent *)kmalloc(sizeof(*vdir), GFP_KERNEL);
552 if (!vdir) return -ENOMEM;
557 ret = filldir(dirent, ".", 1, 0, dir->d_inode->i_ino, DT_DIR);
563 ret = filldir(dirent, "..", 2, 1, dir->d_parent->d_inode->i_ino, DT_DIR);
570 /* read entries from the directory file */
571 ret = kernel_read(filp, filp->f_pos - 2, (char *)vdir,
574 printk("coda_venus_readdir: read dir failed %d\n", ret);
577 if (ret == 0) break; /* end of directory file reached */
579 /* catch truncated reads */
580 if (ret < vdir_size || ret < vdir_size + vdir->d_namlen) {
581 printk("coda_venus_readdir: short read: %ld\n",
582 filp->f_dentry->d_inode->i_ino);
586 /* validate whether the directory file actually makes sense */
587 if (vdir->d_reclen < vdir_size + vdir->d_namlen ||
588 vdir->d_namlen > CODA_MAXNAMLEN) {
589 printk("coda_venus_readdir: Invalid dir: %ld\n",
590 filp->f_dentry->d_inode->i_ino);
595 name.len = vdir->d_namlen;
596 name.name = vdir->d_name;
598 /* Make sure we skip '.' and '..', we already got those */
599 if (name.name[0] == '.' && (name.len == 1 ||
600 (vdir->d_name[1] == '.' && name.len == 2)))
601 vdir->d_fileno = name.len = 0;
603 /* skip null entries */
604 if (vdir->d_fileno && name.len) {
605 /* try to look up this entry in the dcache, that way
606 * userspace doesn't have to worry about breaking
607 * getcwd by having mismatched inode numbers for
608 * internal volume mountpoints. */
609 ino = find_inode_number(dir, &name);
610 if (!ino) ino = vdir->d_fileno;
612 type = CDT2DT(vdir->d_type);
613 ret = filldir(dirent, name.name, name.len, filp->f_pos,
615 /* failure means no space for filling in this round */
619 /* we'll always have progress because d_reclen is unsigned and
620 * we've already established it is non-zero. */
621 filp->f_pos += vdir->d_reclen;
625 return result ? result : ret;
628 /* called when a cache lookup succeeds */
629 static int coda_dentry_revalidate(struct dentry *de, int flags)
631 struct inode *inode = de->d_inode;
632 struct coda_inode_info *cii;
637 if (coda_isroot(inode))
639 if (is_bad_inode(inode))
642 cii = ITOC(de->d_inode);
643 if (!(cii->c_flags & (C_PURGE | C_FLUSH)))
646 shrink_dcache_parent(de);
648 /* propagate for a flush */
649 if (cii->c_flags & C_FLUSH)
650 coda_flag_inode_children(inode, C_FLUSH);
652 if (atomic_read(&de->d_count) > 1) {
653 /* pretend it's valid, but don't change the flags */
654 CDEBUG(D_DOWNCALL, "BOOM for: ino %ld, %s\n",
655 de->d_inode->i_ino, coda_f2s(&cii->c_fid));
659 /* clear the flags. */
660 cii->c_flags &= ~(C_VATTR | C_PURGE | C_FLUSH);
671 * This is the callback from dput() when d_count is going to 0.
672 * We use this to unhash dentries with bad inodes.
674 static int coda_dentry_delete(struct dentry * dentry)
678 if (!dentry->d_inode)
681 flags = (ITOC(dentry->d_inode)->c_flags) & C_PURGE;
682 if (is_bad_inode(dentry->d_inode) || flags) {
683 CDEBUG(D_DOWNCALL, "bad inode, unhashing %s/%s, %ld\n",
684 dentry->d_parent->d_name.name, dentry->d_name.name,
685 dentry->d_inode->i_ino);
694 * This is called when we want to check if the inode has
695 * changed on the server. Coda makes this easy since the
696 * cache manager Venus issues a downcall to the kernel when this
699 int coda_revalidate_inode(struct dentry *dentry)
701 struct coda_vattr attr;
705 struct inode *inode = dentry->d_inode;
706 struct coda_inode_info *cii = ITOC(inode);
708 CDEBUG(D_INODE, "revalidating: %*s/%*s\n",
709 dentry->d_name.len, dentry->d_name.name,
710 dentry->d_parent->d_name.len, dentry->d_parent->d_name.name);
716 if (cii->c_flags & (C_VATTR | C_PURGE | C_FLUSH)) {
717 error = venus_getattr(inode->i_sb, &(cii->c_fid), &attr);
719 goto return_bad_inode;
721 /* this inode may be lost if:
723 - type changes must be permitted for repair and
724 missing mount points.
726 old_mode = inode->i_mode;
727 old_ino = inode->i_ino;
728 coda_vattr_to_iattr(inode, &attr);
730 if ((old_mode & S_IFMT) != (inode->i_mode & S_IFMT)) {
731 printk("Coda: inode %ld, fid %s changed type!\n",
732 inode->i_ino, coda_f2s(&(cii->c_fid)));
735 /* the following can happen when a local fid is replaced
736 with a global one, here we lose and declare the inode bad */
737 if (inode->i_ino != old_ino)
738 goto return_bad_inode;
740 coda_flag_inode_children(inode, C_FLUSH);
741 cii->c_flags &= ~(C_VATTR | C_PURGE | C_FLUSH);