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 int coda_mknod(struct inode *dir, struct dentry *new, int mode, dev_t rdev);
33 static struct dentry *coda_lookup(struct inode *dir, struct dentry *target, struct nameidata *nd);
34 static int coda_link(struct dentry *old_dentry, struct inode *dir_inode,
35 struct dentry *entry);
36 static int coda_unlink(struct inode *dir_inode, struct dentry *entry);
37 static int coda_symlink(struct inode *dir_inode, struct dentry *entry,
39 static int coda_mkdir(struct inode *dir_inode, struct dentry *entry, int mode);
40 static int coda_rmdir(struct inode *dir_inode, struct dentry *entry);
41 static int coda_rename(struct inode *old_inode, struct dentry *old_dentry,
42 struct inode *new_inode, struct dentry *new_dentry);
45 static int coda_readdir(struct file *file, void *dirent, filldir_t filldir);
48 static int coda_dentry_revalidate(struct dentry *de, struct nameidata *nd);
49 static int coda_dentry_delete(struct dentry *);
51 /* support routines */
52 static int coda_venus_readdir(struct file *filp, filldir_t filldir,
53 void *dirent, struct dentry *dir);
54 int coda_fsync(struct file *, struct dentry *dentry, int datasync);
58 struct dentry_operations coda_dentry_operations =
60 .d_revalidate = coda_dentry_revalidate,
61 .d_delete = coda_dentry_delete,
64 struct inode_operations coda_dir_inode_operations =
66 .create = coda_create,
67 .lookup = coda_lookup,
69 .unlink = coda_unlink,
70 .symlink = coda_symlink,
74 .rename = coda_rename,
75 .permission = coda_permission,
76 .getattr = coda_getattr,
77 .setattr = coda_setattr,
80 struct file_operations coda_dir_operations = {
81 .llseek = generic_file_llseek,
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, struct nameidata *nd)
95 struct inode *res_inode = NULL;
96 struct CodaFid resfid = { { 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);
110 /* control object, create inode on the fly */
111 if (coda_isroot(dir) && coda_iscontrol(name, length)) {
112 error = coda_cnode_makectl(&res_inode, dir->i_sb);
117 error = venus_lookup(dir->i_sb, coda_i2f(dir),
118 (const char *)name, length, &type, &resfid);
122 if (type & CODA_NOCACHE) {
123 type &= (~CODA_NOCACHE);
127 error = coda_cnode_make(&res_inode, &resfid, dir->i_sb);
130 return ERR_PTR(error);
132 } else if (error != -ENOENT) {
134 return ERR_PTR(error);
139 entry->d_op = &coda_dentry_operations;
140 d_add(entry, res_inode);
143 coda_flag_inode(res_inode, C_VATTR);
150 int coda_permission(struct inode *inode, int mask, struct nameidata *nd)
159 coda_vfs_stat.permission++;
161 if (coda_cache_check(inode, mask))
164 error = venus_access(inode->i_sb, coda_i2f(inode), mask);
167 coda_cache_enter(inode, mask);
176 static inline void coda_dir_changed(struct inode *dir, int link)
178 #ifdef REQUERY_VENUS_FOR_MTIME
179 /* invalidate the directory cnode's attributes so we refetch the
180 * attributes from venus next time the inode is referenced */
181 coda_flag_inode(dir, C_VATTR);
183 /* optimistically we can also act as if our nose bleeds. The
184 * granularity of the mtime is coarse anyways so we might actually be
185 * right most of the time. Note: we only do this for directories. */
186 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
189 dir->i_nlink += link;
192 /* creation routines: create, mknod, mkdir, link, symlink */
193 static int coda_create(struct inode *dir, struct dentry *de, int mode, struct nameidata *nd)
196 const char *name=de->d_name.name;
197 int length=de->d_name.len;
199 struct CodaFid newfid;
200 struct coda_vattr attrs;
203 coda_vfs_stat.create++;
205 if (coda_isroot(dir) && coda_iscontrol(name, length)) {
210 error = venus_create(dir->i_sb, coda_i2f(dir), name, length,
211 0, mode, 0, &newfid, &attrs);
219 inode = coda_iget(dir->i_sb, &newfid, &attrs);
220 if ( IS_ERR(inode) ) {
223 return PTR_ERR(inode);
226 /* invalidate the directory cnode's attributes */
227 coda_dir_changed(dir, 0);
229 d_instantiate(de, inode);
233 static int coda_mknod(struct inode *dir, struct dentry *de, int mode, dev_t rdev)
236 const char *name=de->d_name.name;
237 int length=de->d_name.len;
239 struct CodaFid newfid;
240 struct coda_vattr attrs;
242 if ( coda_hasmknod == 0 )
245 if (!old_valid_dev(rdev))
249 coda_vfs_stat.create++;
251 if (coda_isroot(dir) && coda_iscontrol(name, length)) {
256 error = venus_create(dir->i_sb, coda_i2f(dir), name, length,
257 0, mode, rdev, &newfid, &attrs);
265 inode = coda_iget(dir->i_sb, &newfid, &attrs);
266 if ( IS_ERR(inode) ) {
269 return PTR_ERR(inode);
272 /* invalidate the directory cnode's attributes */
273 coda_dir_changed(dir, 0);
275 d_instantiate(de, inode);
279 static int coda_mkdir(struct inode *dir, struct dentry *de, int mode)
282 struct coda_vattr attrs;
283 const char *name = de->d_name.name;
284 int len = de->d_name.len;
286 struct CodaFid newfid;
289 coda_vfs_stat.mkdir++;
291 if (coda_isroot(dir) && coda_iscontrol(name, len)) {
296 attrs.va_mode = mode;
297 error = venus_mkdir(dir->i_sb, coda_i2f(dir),
298 name, len, &newfid, &attrs);
306 inode = coda_iget(dir->i_sb, &newfid, &attrs);
307 if ( IS_ERR(inode) ) {
310 return PTR_ERR(inode);
313 /* invalidate the directory cnode's attributes */
314 coda_dir_changed(dir, 1);
316 d_instantiate(de, inode);
320 /* try to make de an entry in dir_inodde linked to source_de */
321 static int coda_link(struct dentry *source_de, struct inode *dir_inode,
324 struct inode *inode = source_de->d_inode;
325 const char * name = de->d_name.name;
326 int len = de->d_name.len;
330 coda_vfs_stat.link++;
332 if (coda_isroot(dir_inode) && coda_iscontrol(name, len)) {
337 error = venus_link(dir_inode->i_sb, coda_i2f(inode),
338 coda_i2f(dir_inode), (const char *)name, len);
345 coda_dir_changed(dir_inode, 0);
346 atomic_inc(&inode->i_count);
347 d_instantiate(de, inode);
356 static int coda_symlink(struct inode *dir_inode, struct dentry *de,
359 const char *name = de->d_name.name;
360 int len = de->d_name.len;
365 coda_vfs_stat.symlink++;
367 if (coda_isroot(dir_inode) && coda_iscontrol(name, len)) {
372 symlen = strlen(symname);
373 if ( symlen > CODA_MAXPATHLEN ) {
375 return -ENAMETOOLONG;
379 * This entry is now negative. Since we do not create
380 * an inode for the entry we have to drop it.
383 error = venus_symlink(dir_inode->i_sb, coda_i2f(dir_inode), name, len,
386 /* mtime is no good anymore */
388 coda_dir_changed(dir_inode, 0);
394 /* destruction routines: unlink, rmdir */
395 int coda_unlink(struct inode *dir, struct dentry *de)
398 const char *name = de->d_name.name;
399 int len = de->d_name.len;
402 coda_vfs_stat.unlink++;
404 error = venus_remove(dir->i_sb, coda_i2f(dir), name, len);
410 coda_dir_changed(dir, 0);
411 de->d_inode->i_nlink--;
417 int coda_rmdir(struct inode *dir, struct dentry *de)
419 const char *name = de->d_name.name;
420 int len = de->d_name.len;
424 coda_vfs_stat.rmdir++;
426 if (!d_unhashed(de)) {
430 error = venus_rmdir(dir->i_sb, coda_i2f(dir), name, len);
437 coda_dir_changed(dir, -1);
438 de->d_inode->i_nlink--;
446 static int coda_rename(struct inode *old_dir, struct dentry *old_dentry,
447 struct inode *new_dir, struct dentry *new_dentry)
449 const char *old_name = old_dentry->d_name.name;
450 const char *new_name = new_dentry->d_name.name;
451 int old_length = old_dentry->d_name.len;
452 int new_length = new_dentry->d_name.len;
457 coda_vfs_stat.rename++;
459 error = venus_rename(old_dir->i_sb, coda_i2f(old_dir),
460 coda_i2f(new_dir), old_length, new_length,
461 (const char *) old_name, (const char *)new_name);
464 if ( new_dentry->d_inode ) {
465 if ( S_ISDIR(new_dentry->d_inode->i_mode) )
468 coda_dir_changed(old_dir, -link_adjust);
469 coda_dir_changed(new_dir, link_adjust);
470 coda_flag_inode(new_dentry->d_inode, C_VATTR);
472 coda_flag_inode(old_dir, C_VATTR);
473 coda_flag_inode(new_dir, C_VATTR);
482 /* file operations for directories */
483 int coda_readdir(struct file *coda_file, void *dirent, filldir_t filldir)
485 struct dentry *coda_dentry = coda_file->f_dentry;
486 struct coda_file_info *cfi;
487 struct file *host_file;
488 struct inode *host_inode;
491 cfi = CODA_FTOC(coda_file);
492 BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
493 host_file = cfi->cfi_container;
495 coda_vfs_stat.readdir++;
497 host_inode = host_file->f_dentry->d_inode;
498 down(&host_inode->i_sem);
499 host_file->f_pos = coda_file->f_pos;
501 if (!host_file->f_op->readdir) {
502 /* Venus: we must read Venus dirents from the file */
503 ret = coda_venus_readdir(host_file, filldir, dirent, coda_dentry);
505 /* potemkin case: we were handed a directory inode. */
506 /* Yuk, we can't call vfs_readdir because we are already
507 * holding the inode semaphore. */
509 if (!host_file->f_op || !host_file->f_op->readdir)
513 if (!IS_DEADDIR(host_inode)) {
514 ret = host_file->f_op->readdir(host_file, filldir, dirent);
515 file_accessed(host_file);
519 coda_file->f_pos = host_file->f_pos;
520 up(&host_inode->i_sem);
525 static inline unsigned int CDT2DT(unsigned char cdt)
530 case CDT_UNKNOWN: dt = DT_UNKNOWN; break;
531 case CDT_FIFO: dt = DT_FIFO; break;
532 case CDT_CHR: dt = DT_CHR; break;
533 case CDT_DIR: dt = DT_DIR; break;
534 case CDT_BLK: dt = DT_BLK; break;
535 case CDT_REG: dt = DT_REG; break;
536 case CDT_LNK: dt = DT_LNK; break;
537 case CDT_SOCK: dt = DT_SOCK; break;
538 case CDT_WHT: dt = DT_WHT; break;
539 default: dt = DT_UNKNOWN; break;
544 /* support routines */
545 static int coda_venus_readdir(struct file *filp, filldir_t filldir,
546 void *dirent, struct dentry *dir)
548 int result = 0; /* # of entries returned */
549 struct venus_dirent *vdir;
550 unsigned long vdir_size =
551 (unsigned long)(&((struct venus_dirent *)0)->d_name);
557 vdir = (struct venus_dirent *)kmalloc(sizeof(*vdir), GFP_KERNEL);
558 if (!vdir) return -ENOMEM;
563 ret = filldir(dirent, ".", 1, 0, dir->d_inode->i_ino, DT_DIR);
569 ret = filldir(dirent, "..", 2, 1, dir->d_parent->d_inode->i_ino, DT_DIR);
576 /* read entries from the directory file */
577 ret = kernel_read(filp, filp->f_pos - 2, (char *)vdir,
580 printk("coda_venus_readdir: read dir failed %d\n", ret);
583 if (ret == 0) break; /* end of directory file reached */
585 /* catch truncated reads */
586 if (ret < vdir_size || ret < vdir_size + vdir->d_namlen) {
587 printk("coda_venus_readdir: short read: %ld\n",
588 filp->f_dentry->d_inode->i_ino);
592 /* validate whether the directory file actually makes sense */
593 if (vdir->d_reclen < vdir_size + vdir->d_namlen) {
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, struct nameidata *nd)
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 */
661 /* clear the flags. */
662 cii->c_flags &= ~(C_VATTR | C_PURGE | C_FLUSH);
673 * This is the callback from dput() when d_count is going to 0.
674 * We use this to unhash dentries with bad inodes.
676 static int coda_dentry_delete(struct dentry * dentry)
680 if (!dentry->d_inode)
683 flags = (ITOC(dentry->d_inode)->c_flags) & C_PURGE;
684 if (is_bad_inode(dentry->d_inode) || flags) {
693 * This is called when we want to check if the inode has
694 * changed on the server. Coda makes this easy since the
695 * cache manager Venus issues a downcall to the kernel when this
698 int coda_revalidate_inode(struct dentry *dentry)
700 struct coda_vattr attr;
704 struct inode *inode = dentry->d_inode;
705 struct coda_inode_info *cii = ITOC(inode);
711 if (cii->c_flags & (C_VATTR | C_PURGE | C_FLUSH)) {
712 error = venus_getattr(inode->i_sb, &(cii->c_fid), &attr);
716 /* this inode may be lost if:
718 - type changes must be permitted for repair and
719 missing mount points.
721 old_mode = inode->i_mode;
722 old_ino = inode->i_ino;
723 coda_vattr_to_iattr(inode, &attr);
725 if ((old_mode & S_IFMT) != (inode->i_mode & S_IFMT)) {
726 printk("Coda: inode %ld, fid %s changed type!\n",
727 inode->i_ino, coda_f2s(&(cii->c_fid)));
730 /* the following can happen when a local fid is replaced
731 with a global one, here we lose and declare the inode bad */
732 if (inode->i_ino != old_ino)
735 coda_flag_inode_children(inode, C_FLUSH);
736 cii->c_flags &= ~(C_VATTR | C_PURGE | C_FLUSH);