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 llseek: generic_file_llseek,
83 read: generic_read_dir,
84 readdir: coda_readdir,
87 release: coda_release,
92 /* inode operations for directories */
93 /* access routines: lookup, readlink, permission */
94 static struct dentry *coda_lookup(struct inode *dir, struct dentry *entry)
96 struct inode *res_inode = NULL;
97 struct ViceFid resfid = {0,0,0};
98 int dropme = 0; /* to indicate entry should not be cached */
101 const char *name = entry->d_name.name;
102 size_t length = entry->d_name.len;
104 if ( length > CODA_MAXNAMLEN ) {
105 printk("name too long: lookup, %s (%*s)\n",
106 coda_i2s(dir), (int)length, name);
107 return ERR_PTR(-ENAMETOOLONG);
110 CDEBUG(D_INODE, "name %s, len %ld in ino %ld, fid %s\n",
111 name, (long)length, dir->i_ino, coda_i2s(dir));
113 /* control object, create inode on the fly */
114 if (coda_isroot(dir) && coda_iscontrol(name, length)) {
115 error = coda_cnode_makectl(&res_inode, dir->i_sb);
117 "Lookup on CTL object; dir ino %ld, count %d\n",
118 dir->i_ino, atomic_read(&dir->i_count));
123 error = venus_lookup(dir->i_sb, coda_i2f(dir),
124 (const char *)name, length, &type, &resfid);
128 if (type & CODA_NOCACHE) {
129 type &= (~CODA_NOCACHE);
130 CDEBUG(D_INODE, "dropme set for %s\n",
135 error = coda_cnode_make(&res_inode, &resfid, dir->i_sb);
136 if (error) return ERR_PTR(error);
137 } else if (error != -ENOENT) {
138 CDEBUG(D_INODE, "error for %s(%*s)%d\n",
139 coda_i2s(dir), (int)length, name, error);
140 return ERR_PTR(error);
142 CDEBUG(D_INODE, "lookup: %s is (%s), type %d result %d, dropme %d\n",
143 name, coda_f2s(&resfid), type, error, dropme);
147 entry->d_op = &coda_dentry_operations;
148 d_add(entry, res_inode);
151 coda_flag_inode(res_inode, C_VATTR);
157 int coda_permission(struct inode *inode, int mask)
161 coda_vfs_stat.permission++;
166 if ( coda_access_cache ) {
167 coda_permission_stat.count++;
169 if ( coda_cache_check(inode, mask) ) {
170 coda_permission_stat.hit_count++;
175 CDEBUG(D_INODE, "mask is %o\n", mask);
176 error = venus_access(inode->i_sb, coda_i2f(inode), mask);
178 CDEBUG(D_INODE, "fid: %s, ino: %ld (mask: %o) error: %d\n",
179 coda_i2s(inode), inode->i_ino, mask, error);
182 coda_cache_enter(inode, mask);
188 static inline void coda_dir_changed(struct inode *dir, int link)
190 #ifdef REQUERY_VENUS_FOR_MTIME
191 /* invalidate the directory cnode's attributes so we refetch the
192 * attributes from venus next time the inode is referenced */
193 coda_flag_inode(dir, C_VATTR);
195 /* optimistically we can also act as if our nose bleeds. The
196 * granularity of the mtime is coarse anyways so we might actually be
197 * right most of the time. Note: we only do this for directories. */
198 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
201 dir->i_nlink += link;
204 /* creation routines: create, mknod, mkdir, link, symlink */
205 static int coda_create(struct inode *dir, struct dentry *de, int mode)
208 const char *name=de->d_name.name;
209 int length=de->d_name.len;
211 struct ViceFid newfid;
212 struct coda_vattr attrs;
214 coda_vfs_stat.create++;
216 CDEBUG(D_INODE, "name: %s, length %d, mode %o\n", name, length, mode);
218 if (coda_isroot(dir) && coda_iscontrol(name, length))
221 error = venus_create(dir->i_sb, coda_i2f(dir), name, length,
222 0, mode, 0, &newfid, &attrs);
225 CDEBUG(D_INODE, "create: %s, result %d\n",
226 coda_f2s(&newfid), error);
231 inode = coda_iget(dir->i_sb, &newfid, &attrs);
232 if ( IS_ERR(inode) ) {
234 return PTR_ERR(inode);
237 /* invalidate the directory cnode's attributes */
238 coda_dir_changed(dir, 0);
239 d_instantiate(de, inode);
243 static int coda_mknod(struct inode *dir, struct dentry *de, int mode, int rdev)
246 const char *name=de->d_name.name;
247 int length=de->d_name.len;
249 struct ViceFid newfid;
250 struct coda_vattr attrs;
252 if ( coda_hasmknod == 0 )
255 coda_vfs_stat.create++;
257 CDEBUG(D_INODE, "name: %s, length %d, mode %o, rdev %x\n",
258 name, length, mode, rdev);
260 if (coda_isroot(dir) && coda_iscontrol(name, length))
263 error = venus_create(dir->i_sb, coda_i2f(dir), name, length,
264 0, mode, rdev, &newfid, &attrs);
267 CDEBUG(D_INODE, "mknod: %s, result %d\n",
268 coda_f2s(&newfid), error);
273 inode = coda_iget(dir->i_sb, &newfid, &attrs);
274 if ( IS_ERR(inode) ) {
276 return PTR_ERR(inode);
279 /* invalidate the directory cnode's attributes */
280 coda_dir_changed(dir, 0);
281 d_instantiate(de, inode);
285 static int coda_mkdir(struct inode *dir, struct dentry *de, int mode)
288 struct coda_vattr attrs;
289 const char *name = de->d_name.name;
290 int len = de->d_name.len;
292 struct ViceFid newfid;
294 coda_vfs_stat.mkdir++;
296 if (coda_isroot(dir) && coda_iscontrol(name, len))
299 CDEBUG(D_INODE, "mkdir %s (len %d) in %s, mode %o.\n",
300 name, len, coda_i2s(dir), mode);
302 attrs.va_mode = mode;
303 error = venus_mkdir(dir->i_sb, coda_i2f(dir),
304 name, len, &newfid, &attrs);
307 CDEBUG(D_INODE, "mkdir error: %s result %d\n",
308 coda_f2s(&newfid), error);
313 CDEBUG(D_INODE, "mkdir: new dir has fid %s.\n",
316 inode = coda_iget(dir->i_sb, &newfid, &attrs);
317 if ( IS_ERR(inode) ) {
319 return PTR_ERR(inode);
322 /* invalidate the directory cnode's attributes */
323 coda_dir_changed(dir, 1);
324 d_instantiate(de, inode);
328 /* try to make de an entry in dir_inodde linked to source_de */
329 static int coda_link(struct dentry *source_de, struct inode *dir_inode,
332 struct inode *inode = source_de->d_inode;
333 const char * name = de->d_name.name;
334 int len = de->d_name.len;
337 coda_vfs_stat.link++;
339 if (coda_isroot(dir_inode) && coda_iscontrol(name, len))
342 CDEBUG(D_INODE, "old: fid: %s\n", coda_i2s(inode));
343 CDEBUG(D_INODE, "directory: %s\n", coda_i2s(dir_inode));
345 error = venus_link(dir_inode->i_sb, coda_i2f(inode),
346 coda_i2f(dir_inode), (const char *)name, len);
353 coda_dir_changed(dir_inode, 0);
354 atomic_inc(&inode->i_count);
355 d_instantiate(de, inode);
359 CDEBUG(D_INODE, "link result %d\n",error);
364 static int coda_symlink(struct inode *dir_inode, struct dentry *de,
367 const char *name = de->d_name.name;
368 int len = de->d_name.len;
372 coda_vfs_stat.symlink++;
374 if (coda_isroot(dir_inode) && coda_iscontrol(name, len))
377 symlen = strlen(symname);
378 if ( symlen > CODA_MAXPATHLEN )
379 return -ENAMETOOLONG;
381 CDEBUG(D_INODE, "symname: %s, length: %d\n", symname, symlen);
384 * This entry is now negative. Since we do not create
385 * an inode for the entry we have to drop it.
388 error = venus_symlink(dir_inode->i_sb, coda_i2f(dir_inode), name, len,
391 /* mtime is no good anymore */
393 coda_dir_changed(dir_inode, 0);
395 CDEBUG(D_INODE, "in symlink result %d\n",error);
399 /* destruction routines: unlink, rmdir */
400 int coda_unlink(struct inode *dir, struct dentry *de)
403 const char *name = de->d_name.name;
404 int len = de->d_name.len;
406 coda_vfs_stat.unlink++;
408 CDEBUG(D_INODE, " %s in %s, dirino %ld\n", name ,
409 coda_i2s(dir), dir->i_ino);
411 error = venus_remove(dir->i_sb, coda_i2f(dir), name, len);
413 CDEBUG(D_INODE, "upc returned error %d\n", error);
417 coda_dir_changed(dir, 0);
418 de->d_inode->i_nlink--;
423 int coda_rmdir(struct inode *dir, struct dentry *de)
425 const char *name = de->d_name.name;
426 int len = de->d_name.len;
429 coda_vfs_stat.rmdir++;
433 error = venus_rmdir(dir->i_sb, coda_i2f(dir), name, len);
436 CDEBUG(D_INODE, "upc returned error %d\n", error);
440 coda_dir_changed(dir, -1);
441 de->d_inode->i_nlink--;
448 static int coda_rename(struct inode *old_dir, struct dentry *old_dentry,
449 struct inode *new_dir, struct dentry *new_dentry)
451 const char *old_name = old_dentry->d_name.name;
452 const char *new_name = new_dentry->d_name.name;
453 int old_length = old_dentry->d_name.len;
454 int new_length = new_dentry->d_name.len;
458 coda_vfs_stat.rename++;
460 CDEBUG(D_INODE, "old: %s, (%d length), new: %s"
461 "(%d length). old:d_count: %d, new:d_count: %d\n",
462 old_name, old_length, new_name, new_length,
463 atomic_read(&old_dentry->d_count), atomic_read(&new_dentry->d_count));
465 error = venus_rename(old_dir->i_sb, coda_i2f(old_dir),
466 coda_i2f(new_dir), old_length, new_length,
467 (const char *) old_name, (const char *)new_name);
470 if ( new_dentry->d_inode ) {
471 if ( S_ISDIR(new_dentry->d_inode->i_mode) )
474 coda_dir_changed(old_dir, -link_adjust);
475 coda_dir_changed(new_dir, link_adjust);
476 coda_flag_inode(new_dentry->d_inode, C_VATTR);
478 coda_flag_inode(old_dir, C_VATTR);
479 coda_flag_inode(new_dir, C_VATTR);
483 CDEBUG(D_INODE, "result %d\n", error);
489 /* file operations for directories */
490 int coda_readdir(struct file *coda_file, void *dirent, filldir_t filldir)
492 struct dentry *coda_dentry = coda_file->f_dentry;
493 struct coda_file_info *cfi;
494 struct file *host_file;
497 cfi = CODA_FTOC(coda_file);
498 if (!cfi || cfi->cfi_magic != CODA_MAGIC) BUG();
499 host_file = cfi->cfi_container;
501 coda_vfs_stat.readdir++;
503 down(&host_file->f_dentry->d_inode->i_sem);
504 host_file->f_pos = coda_file->f_pos;
506 if ( !host_file->f_op->readdir ) {
507 /* Venus: we must read Venus dirents from the file */
508 ret = coda_venus_readdir(host_file, filldir, dirent, coda_dentry);
510 /* potemkin case: we were handed a directory inode */
511 /* We can't call vfs_readdir because we are already holding
512 * the inode semaphore. */
514 if (!IS_DEADDIR(host_file->f_dentry->d_inode))
515 ret = host_file->f_op->readdir(host_file, filldir, dirent);
518 coda_file->f_pos = host_file->f_pos;
519 up(&host_file->f_dentry->d_inode->i_sem);
524 static inline unsigned int CDT2DT(unsigned char cdt)
529 case CDT_UNKNOWN: dt = DT_UNKNOWN; break;
530 case CDT_FIFO: dt = DT_FIFO; break;
531 case CDT_CHR: dt = DT_CHR; break;
532 case CDT_DIR: dt = DT_DIR; break;
533 case CDT_BLK: dt = DT_BLK; break;
534 case CDT_REG: dt = DT_REG; break;
535 case CDT_LNK: dt = DT_LNK; break;
536 case CDT_SOCK: dt = DT_SOCK; break;
537 case CDT_WHT: dt = DT_WHT; break;
538 default: dt = DT_UNKNOWN; break;
543 /* support routines */
544 static int coda_venus_readdir(struct file *filp, filldir_t filldir,
545 void *dirent, struct dentry *dir)
547 int result = 0; /* # of entries returned */
548 struct venus_dirent *vdir;
549 unsigned long vdir_size =
550 (unsigned long)(&((struct venus_dirent *)0)->d_name);
556 vdir = (struct venus_dirent *)kmalloc(sizeof(*vdir), GFP_KERNEL);
557 if (!vdir) return -ENOMEM;
562 ret = filldir(dirent, ".", 1, 0, dir->d_inode->i_ino, DT_DIR);
568 ret = filldir(dirent, "..", 2, 1, dir->d_parent->d_inode->i_ino, DT_DIR);
575 /* read entries from the directory file */
576 ret = kernel_read(filp, filp->f_pos - 2, (char *)vdir,
579 printk("coda_venus_readdir: read dir failed %d\n", ret);
582 if (ret == 0) break; /* end of directory file reached */
584 /* catch truncated reads */
585 if (ret < vdir_size || ret < vdir_size + vdir->d_namlen) {
586 printk("coda_venus_readdir: short read: %ld\n",
587 filp->f_dentry->d_inode->i_ino);
591 /* validate whether the directory file actually makes sense */
592 if (vdir->d_reclen < vdir_size + vdir->d_namlen ||
593 vdir->d_namlen > CODA_MAXNAMLEN) {
594 printk("coda_venus_readdir: Invalid dir: %ld\n",
595 filp->f_dentry->d_inode->i_ino);
600 name.len = vdir->d_namlen;
601 name.name = vdir->d_name;
603 /* Make sure we skip '.' and '..', we already got those */
604 if (name.name[0] == '.' && (name.len == 1 ||
605 (vdir->d_name[1] == '.' && name.len == 2)))
606 vdir->d_fileno = name.len = 0;
608 /* skip null entries */
609 if (vdir->d_fileno && name.len) {
610 /* try to look up this entry in the dcache, that way
611 * userspace doesn't have to worry about breaking
612 * getcwd by having mismatched inode numbers for
613 * internal volume mountpoints. */
614 ino = find_inode_number(dir, &name);
615 if (!ino) ino = vdir->d_fileno;
617 type = CDT2DT(vdir->d_type);
618 ret = filldir(dirent, name.name, name.len, filp->f_pos,
620 /* failure means no space for filling in this round */
624 /* we'll always have progress because d_reclen is unsigned and
625 * we've already established it is non-zero. */
626 filp->f_pos += vdir->d_reclen;
630 return result ? result : ret;
633 /* called when a cache lookup succeeds */
634 static int coda_dentry_revalidate(struct dentry *de, int flags)
636 struct inode *inode = de->d_inode;
637 struct coda_inode_info *cii;
642 if (coda_isroot(inode))
644 if (is_bad_inode(inode))
647 cii = ITOC(de->d_inode);
648 if (!(cii->c_flags & (C_PURGE | C_FLUSH)))
651 shrink_dcache_parent(de);
653 /* propagate for a flush */
654 if (cii->c_flags & C_FLUSH)
655 coda_flag_inode_children(inode, C_FLUSH);
657 if (atomic_read(&de->d_count) > 1) {
658 /* pretend it's valid, but don't change the flags */
659 CDEBUG(D_DOWNCALL, "BOOM for: ino %ld, %s\n",
660 de->d_inode->i_ino, coda_f2s(&cii->c_fid));
664 /* clear the flags. */
665 cii->c_flags &= ~(C_VATTR | C_PURGE | C_FLUSH);
676 * This is the callback from dput() when d_count is going to 0.
677 * We use this to unhash dentries with bad inodes.
679 static int coda_dentry_delete(struct dentry * dentry)
683 if (!dentry->d_inode)
686 flags = (ITOC(dentry->d_inode)->c_flags) & C_PURGE;
687 if (is_bad_inode(dentry->d_inode) || flags) {
688 CDEBUG(D_DOWNCALL, "bad inode, unhashing %s/%s, %ld\n",
689 dentry->d_parent->d_name.name, dentry->d_name.name,
690 dentry->d_inode->i_ino);
699 * This is called when we want to check if the inode has
700 * changed on the server. Coda makes this easy since the
701 * cache manager Venus issues a downcall to the kernel when this
704 int coda_revalidate_inode(struct dentry *dentry)
706 struct coda_vattr attr;
710 struct inode *inode = dentry->d_inode;
711 struct coda_inode_info *cii = ITOC(inode);
713 CDEBUG(D_INODE, "revalidating: %*s/%*s\n",
714 dentry->d_name.len, dentry->d_name.name,
715 dentry->d_parent->d_name.len, dentry->d_parent->d_name.name);
721 if (cii->c_flags & (C_VATTR | C_PURGE | C_FLUSH)) {
722 error = venus_getattr(inode->i_sb, &(cii->c_fid), &attr);
724 goto return_bad_inode;
726 /* this inode may be lost if:
728 - type changes must be permitted for repair and
729 missing mount points.
731 old_mode = inode->i_mode;
732 old_ino = inode->i_ino;
733 coda_vattr_to_iattr(inode, &attr);
735 if ((old_mode & S_IFMT) != (inode->i_mode & S_IFMT)) {
736 printk("Coda: inode %ld, fid %s changed type!\n",
737 inode->i_ino, coda_f2s(&(cii->c_fid)));
740 /* the following can happen when a local fid is replaced
741 with a global one, here we lose and declare the inode bad */
742 if (inode->i_ino != old_ino)
743 goto return_bad_inode;
745 coda_flag_inode_children(inode, C_FLUSH);
746 cii->c_flags &= ~(C_VATTR | C_PURGE | C_FLUSH);