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/time.h>
15 #include <linux/file.h>
16 #include <linux/stat.h>
17 #include <linux/errno.h>
18 #include <linux/string.h>
19 #include <linux/smp_lock.h>
21 #include <asm/uaccess.h>
23 #include <linux/coda.h>
24 #include <linux/coda_linux.h>
25 #include <linux/coda_psdev.h>
26 #include <linux/coda_fs_i.h>
27 #include <linux/coda_cache.h>
28 #include <linux/coda_proc.h>
31 static int coda_create(struct inode *dir, struct dentry *new, int mode, struct nameidata *nd);
32 static struct dentry *coda_lookup(struct inode *dir, struct dentry *target, struct nameidata *nd);
33 static int coda_link(struct dentry *old_dentry, struct inode *dir_inode,
34 struct dentry *entry);
35 static int coda_unlink(struct inode *dir_inode, struct dentry *entry);
36 static int coda_symlink(struct inode *dir_inode, struct dentry *entry,
38 static int coda_mkdir(struct inode *dir_inode, struct dentry *entry, int mode);
39 static int coda_rmdir(struct inode *dir_inode, struct dentry *entry);
40 static int coda_rename(struct inode *old_inode, struct dentry *old_dentry,
41 struct inode *new_inode, struct dentry *new_dentry);
44 static int coda_readdir(struct file *file, void *dirent, filldir_t filldir);
47 static int coda_dentry_revalidate(struct dentry *de, struct nameidata *nd);
48 static int coda_dentry_delete(struct dentry *);
50 /* support routines */
51 static int coda_venus_readdir(struct file *filp, filldir_t filldir,
52 void *dirent, struct dentry *dir);
53 int coda_fsync(struct file *, struct dentry *dentry, int datasync);
55 /* same as fs/bad_inode.c */
56 static int coda_return_EIO(void)
60 #define CODA_EIO_ERROR ((void *) (coda_return_EIO))
62 static struct dentry_operations coda_dentry_operations =
64 .d_revalidate = coda_dentry_revalidate,
65 .d_delete = coda_dentry_delete,
68 struct inode_operations coda_dir_inode_operations =
70 .create = coda_create,
71 .lookup = coda_lookup,
73 .unlink = coda_unlink,
74 .symlink = coda_symlink,
77 .mknod = CODA_EIO_ERROR,
78 .rename = coda_rename,
79 .permission = coda_permission,
80 .getattr = coda_getattr,
81 .setattr = coda_setattr,
84 struct file_operations coda_dir_operations = {
85 .llseek = generic_file_llseek,
86 .read = generic_read_dir,
87 .readdir = coda_readdir,
90 .release = coda_release,
95 /* inode operations for directories */
96 /* access routines: lookup, readlink, permission */
97 static struct dentry *coda_lookup(struct inode *dir, struct dentry *entry, struct nameidata *nd)
99 struct inode *res_inode = NULL;
100 struct CodaFid resfid = { { 0, } };
101 int dropme = 0; /* to indicate entry should not be cached */
104 const char *name = entry->d_name.name;
105 size_t length = entry->d_name.len;
107 if ( length > CODA_MAXNAMLEN ) {
108 printk("name too long: lookup, %s (%*s)\n",
109 coda_i2s(dir), (int)length, name);
110 return ERR_PTR(-ENAMETOOLONG);
114 /* control object, create inode on the fly */
115 if (coda_isroot(dir) && coda_iscontrol(name, length)) {
116 error = coda_cnode_makectl(&res_inode, dir->i_sb);
121 error = venus_lookup(dir->i_sb, coda_i2f(dir),
122 (const char *)name, length, &type, &resfid);
126 if (type & CODA_NOCACHE) {
127 type &= (~CODA_NOCACHE);
131 error = coda_cnode_make(&res_inode, &resfid, dir->i_sb);
134 return ERR_PTR(error);
136 } else if (error != -ENOENT) {
138 return ERR_PTR(error);
143 entry->d_op = &coda_dentry_operations;
144 d_add(entry, res_inode);
147 coda_flag_inode(res_inode, C_VATTR);
154 int coda_permission(struct inode *inode, int mask, struct nameidata *nd)
163 coda_vfs_stat.permission++;
165 if (coda_cache_check(inode, mask))
168 error = venus_access(inode->i_sb, coda_i2f(inode), mask);
171 coda_cache_enter(inode, mask);
180 static inline void coda_dir_changed(struct inode *dir, int link)
182 #ifdef REQUERY_VENUS_FOR_MTIME
183 /* invalidate the directory cnode's attributes so we refetch the
184 * attributes from venus next time the inode is referenced */
185 coda_flag_inode(dir, C_VATTR);
187 /* optimistically we can also act as if our nose bleeds. The
188 * granularity of the mtime is coarse anyways so we might actually be
189 * right most of the time. Note: we only do this for directories. */
190 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
193 dir->i_nlink += link;
196 /* creation routines: create, mknod, mkdir, link, symlink */
197 static int coda_create(struct inode *dir, struct dentry *de, int mode, struct nameidata *nd)
200 const char *name=de->d_name.name;
201 int length=de->d_name.len;
203 struct CodaFid newfid;
204 struct coda_vattr attrs;
207 coda_vfs_stat.create++;
209 if (coda_isroot(dir) && coda_iscontrol(name, length)) {
214 error = venus_create(dir->i_sb, coda_i2f(dir), name, length,
215 0, mode, &newfid, &attrs);
223 inode = coda_iget(dir->i_sb, &newfid, &attrs);
224 if ( IS_ERR(inode) ) {
227 return PTR_ERR(inode);
230 /* invalidate the directory cnode's attributes */
231 coda_dir_changed(dir, 0);
233 d_instantiate(de, inode);
237 static int coda_mkdir(struct inode *dir, struct dentry *de, int mode)
240 struct coda_vattr attrs;
241 const char *name = de->d_name.name;
242 int len = de->d_name.len;
244 struct CodaFid newfid;
247 coda_vfs_stat.mkdir++;
249 if (coda_isroot(dir) && coda_iscontrol(name, len)) {
254 attrs.va_mode = mode;
255 error = venus_mkdir(dir->i_sb, coda_i2f(dir),
256 name, len, &newfid, &attrs);
264 inode = coda_iget(dir->i_sb, &newfid, &attrs);
265 if ( IS_ERR(inode) ) {
268 return PTR_ERR(inode);
271 /* invalidate the directory cnode's attributes */
272 coda_dir_changed(dir, 1);
274 d_instantiate(de, inode);
278 /* try to make de an entry in dir_inodde linked to source_de */
279 static int coda_link(struct dentry *source_de, struct inode *dir_inode,
282 struct inode *inode = source_de->d_inode;
283 const char * name = de->d_name.name;
284 int len = de->d_name.len;
288 coda_vfs_stat.link++;
290 if (coda_isroot(dir_inode) && coda_iscontrol(name, len)) {
295 error = venus_link(dir_inode->i_sb, coda_i2f(inode),
296 coda_i2f(dir_inode), (const char *)name, len);
303 coda_dir_changed(dir_inode, 0);
304 atomic_inc(&inode->i_count);
305 d_instantiate(de, inode);
314 static int coda_symlink(struct inode *dir_inode, struct dentry *de,
317 const char *name = de->d_name.name;
318 int len = de->d_name.len;
323 coda_vfs_stat.symlink++;
325 if (coda_isroot(dir_inode) && coda_iscontrol(name, len)) {
330 symlen = strlen(symname);
331 if ( symlen > CODA_MAXPATHLEN ) {
333 return -ENAMETOOLONG;
337 * This entry is now negative. Since we do not create
338 * an inode for the entry we have to drop it.
341 error = venus_symlink(dir_inode->i_sb, coda_i2f(dir_inode), name, len,
344 /* mtime is no good anymore */
346 coda_dir_changed(dir_inode, 0);
352 /* destruction routines: unlink, rmdir */
353 int coda_unlink(struct inode *dir, struct dentry *de)
356 const char *name = de->d_name.name;
357 int len = de->d_name.len;
360 coda_vfs_stat.unlink++;
362 error = venus_remove(dir->i_sb, coda_i2f(dir), name, len);
368 coda_dir_changed(dir, 0);
369 de->d_inode->i_nlink--;
375 int coda_rmdir(struct inode *dir, struct dentry *de)
377 const char *name = de->d_name.name;
378 int len = de->d_name.len;
382 coda_vfs_stat.rmdir++;
384 if (!d_unhashed(de)) {
388 error = venus_rmdir(dir->i_sb, coda_i2f(dir), name, len);
395 coda_dir_changed(dir, -1);
396 de->d_inode->i_nlink--;
404 static int coda_rename(struct inode *old_dir, struct dentry *old_dentry,
405 struct inode *new_dir, struct dentry *new_dentry)
407 const char *old_name = old_dentry->d_name.name;
408 const char *new_name = new_dentry->d_name.name;
409 int old_length = old_dentry->d_name.len;
410 int new_length = new_dentry->d_name.len;
415 coda_vfs_stat.rename++;
417 error = venus_rename(old_dir->i_sb, coda_i2f(old_dir),
418 coda_i2f(new_dir), old_length, new_length,
419 (const char *) old_name, (const char *)new_name);
422 if ( new_dentry->d_inode ) {
423 if ( S_ISDIR(new_dentry->d_inode->i_mode) )
426 coda_dir_changed(old_dir, -link_adjust);
427 coda_dir_changed(new_dir, link_adjust);
428 coda_flag_inode(new_dentry->d_inode, C_VATTR);
430 coda_flag_inode(old_dir, C_VATTR);
431 coda_flag_inode(new_dir, C_VATTR);
440 /* file operations for directories */
441 int coda_readdir(struct file *coda_file, void *dirent, filldir_t filldir)
443 struct dentry *coda_dentry = coda_file->f_dentry;
444 struct coda_file_info *cfi;
445 struct file *host_file;
446 struct inode *host_inode;
449 cfi = CODA_FTOC(coda_file);
450 BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
451 host_file = cfi->cfi_container;
453 coda_vfs_stat.readdir++;
455 host_inode = host_file->f_dentry->d_inode;
456 down(&host_inode->i_sem);
457 host_file->f_pos = coda_file->f_pos;
459 if (!host_file->f_op->readdir) {
460 /* Venus: we must read Venus dirents from the file */
461 ret = coda_venus_readdir(host_file, filldir, dirent, coda_dentry);
463 /* potemkin case: we were handed a directory inode. */
464 /* Yuk, we can't call vfs_readdir because we are already
465 * holding the inode semaphore. */
467 if (!host_file->f_op || !host_file->f_op->readdir)
471 if (!IS_DEADDIR(host_inode)) {
472 ret = host_file->f_op->readdir(host_file, filldir, dirent);
473 file_accessed(host_file);
477 coda_file->f_pos = host_file->f_pos;
478 up(&host_inode->i_sem);
483 static inline unsigned int CDT2DT(unsigned char cdt)
488 case CDT_UNKNOWN: dt = DT_UNKNOWN; break;
489 case CDT_FIFO: dt = DT_FIFO; break;
490 case CDT_CHR: dt = DT_CHR; break;
491 case CDT_DIR: dt = DT_DIR; break;
492 case CDT_BLK: dt = DT_BLK; break;
493 case CDT_REG: dt = DT_REG; break;
494 case CDT_LNK: dt = DT_LNK; break;
495 case CDT_SOCK: dt = DT_SOCK; break;
496 case CDT_WHT: dt = DT_WHT; break;
497 default: dt = DT_UNKNOWN; break;
502 /* support routines */
503 static int coda_venus_readdir(struct file *filp, filldir_t filldir,
504 void *dirent, struct dentry *dir)
506 int result = 0; /* # of entries returned */
507 struct venus_dirent *vdir;
508 unsigned long vdir_size =
509 (unsigned long)(&((struct venus_dirent *)0)->d_name);
515 vdir = (struct venus_dirent *)kmalloc(sizeof(*vdir), GFP_KERNEL);
516 if (!vdir) return -ENOMEM;
521 ret = filldir(dirent, ".", 1, 0, dir->d_inode->i_ino, DT_DIR);
527 ret = filldir(dirent, "..", 2, 1, dir->d_parent->d_inode->i_ino, DT_DIR);
534 /* read entries from the directory file */
535 ret = kernel_read(filp, filp->f_pos - 2, (char *)vdir,
538 printk("coda_venus_readdir: read dir failed %d\n", ret);
541 if (ret == 0) break; /* end of directory file reached */
543 /* catch truncated reads */
544 if (ret < vdir_size || ret < vdir_size + vdir->d_namlen) {
545 printk("coda_venus_readdir: short read: %ld\n",
546 filp->f_dentry->d_inode->i_ino);
550 /* validate whether the directory file actually makes sense */
551 if (vdir->d_reclen < vdir_size + vdir->d_namlen) {
552 printk("coda_venus_readdir: Invalid dir: %ld\n",
553 filp->f_dentry->d_inode->i_ino);
558 name.len = vdir->d_namlen;
559 name.name = vdir->d_name;
561 /* Make sure we skip '.' and '..', we already got those */
562 if (name.name[0] == '.' && (name.len == 1 ||
563 (vdir->d_name[1] == '.' && name.len == 2)))
564 vdir->d_fileno = name.len = 0;
566 /* skip null entries */
567 if (vdir->d_fileno && name.len) {
568 /* try to look up this entry in the dcache, that way
569 * userspace doesn't have to worry about breaking
570 * getcwd by having mismatched inode numbers for
571 * internal volume mountpoints. */
572 ino = find_inode_number(dir, &name);
573 if (!ino) ino = vdir->d_fileno;
575 type = CDT2DT(vdir->d_type);
576 ret = filldir(dirent, name.name, name.len, filp->f_pos,
578 /* failure means no space for filling in this round */
582 /* we'll always have progress because d_reclen is unsigned and
583 * we've already established it is non-zero. */
584 filp->f_pos += vdir->d_reclen;
588 return result ? result : ret;
591 /* called when a cache lookup succeeds */
592 static int coda_dentry_revalidate(struct dentry *de, struct nameidata *nd)
594 struct inode *inode = de->d_inode;
595 struct coda_inode_info *cii;
600 if (coda_isroot(inode))
602 if (is_bad_inode(inode))
605 cii = ITOC(de->d_inode);
606 if (!(cii->c_flags & (C_PURGE | C_FLUSH)))
609 shrink_dcache_parent(de);
611 /* propagate for a flush */
612 if (cii->c_flags & C_FLUSH)
613 coda_flag_inode_children(inode, C_FLUSH);
615 if (atomic_read(&de->d_count) > 1)
616 /* pretend it's valid, but don't change the flags */
619 /* clear the flags. */
620 cii->c_flags &= ~(C_VATTR | C_PURGE | C_FLUSH);
631 * This is the callback from dput() when d_count is going to 0.
632 * We use this to unhash dentries with bad inodes.
634 static int coda_dentry_delete(struct dentry * dentry)
638 if (!dentry->d_inode)
641 flags = (ITOC(dentry->d_inode)->c_flags) & C_PURGE;
642 if (is_bad_inode(dentry->d_inode) || flags) {
651 * This is called when we want to check if the inode has
652 * changed on the server. Coda makes this easy since the
653 * cache manager Venus issues a downcall to the kernel when this
656 int coda_revalidate_inode(struct dentry *dentry)
658 struct coda_vattr attr;
662 struct inode *inode = dentry->d_inode;
663 struct coda_inode_info *cii = ITOC(inode);
669 if (cii->c_flags & (C_VATTR | C_PURGE | C_FLUSH)) {
670 error = venus_getattr(inode->i_sb, &(cii->c_fid), &attr);
674 /* this inode may be lost if:
676 - type changes must be permitted for repair and
677 missing mount points.
679 old_mode = inode->i_mode;
680 old_ino = inode->i_ino;
681 coda_vattr_to_iattr(inode, &attr);
683 if ((old_mode & S_IFMT) != (inode->i_mode & S_IFMT)) {
684 printk("Coda: inode %ld, fid %s changed type!\n",
685 inode->i_ino, coda_f2s(&(cii->c_fid)));
688 /* the following can happen when a local fid is replaced
689 with a global one, here we lose and declare the inode bad */
690 if (inode->i_ino != old_ino)
693 coda_flag_inode_children(inode, C_FLUSH);
694 cii->c_flags &= ~(C_VATTR | C_PURGE | C_FLUSH);