Import 2.4.0-test2pre7
[davej-history.git] / fs / coda / dir.c
blob0faf296631aa7e29cf95402378c65b10f5103e84
2 /*
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
6 *
7 * Carnegie Mellon encourages users to contribute improvements to
8 * the Coda project. Contact Peter Braam (coda@cs.cmu.edu).
9 */
11 #include <linux/types.h>
12 #include <linux/kernel.h>
13 #include <linux/sched.h>
14 #include <linux/fs.h>
15 #include <linux/stat.h>
16 #include <linux/errno.h>
17 #include <linux/locks.h>
18 #include <asm/segment.h>
19 #include <asm/uaccess.h>
20 #include <linux/string.h>
22 #include <linux/coda.h>
23 #include <linux/coda_linux.h>
24 #include <linux/coda_psdev.h>
25 #include <linux/coda_fs_i.h>
26 #include <linux/coda_cache.h>
27 #include <linux/coda_proc.h>
29 /* dir inode-ops */
30 static int coda_create(struct inode *dir, struct dentry *new, int mode);
31 static int coda_mknod(struct inode *dir, struct dentry *new, int mode, int rdev);
32 static struct dentry *coda_lookup(struct inode *dir, struct dentry *target);
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,
37 const char *symname);
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);
43 /* dir file-ops */
44 static int coda_readdir(struct file *file, void *dirent, filldir_t filldir);
46 /* dentry ops */
47 static int coda_dentry_revalidate(struct dentry *de, int);
48 static int coda_dentry_delete(struct dentry *);
50 /* support routines */
51 static void coda_prepare_fakefile(struct inode *coda_inode,
52 struct file *coda_file,
53 struct inode *open_inode,
54 struct file *open_file,
55 struct dentry *open_dentry);
56 static int coda_venus_readdir(struct file *filp, void *dirent,
57 filldir_t filldir);
58 int coda_fsync(struct file *, struct dentry *dentry, int);
60 int coda_hasmknod = 0;
62 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,
72 link: coda_link,
73 unlink: coda_unlink,
74 symlink: coda_symlink,
75 mkdir: coda_mkdir,
76 rmdir: coda_rmdir,
77 mknod: coda_mknod,
78 rename: coda_rename,
79 permission: coda_permission,
80 revalidate: coda_revalidate_inode,
81 setattr: coda_notify_change,
84 struct file_operations coda_dir_operations = {
85 read: generic_read_dir,
86 readdir: coda_readdir,
87 open: coda_open,
88 release: coda_release,
89 fsync: coda_fsync,
93 /* inode operations for directories */
94 /* acces routines: lookup, readlink, permission */
95 static struct dentry *coda_lookup(struct inode *dir, struct dentry *entry)
97 struct inode *res_inode = NULL;
98 struct ViceFid resfid = {0,0,0};
99 int dropme = 0; /* to indicate entry should not be cached */
100 int type = 0;
101 int error = 0;
102 const char *name = entry->d_name.name;
103 size_t length = entry->d_name.len;
105 ENTRY;
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);
113 CDEBUG(D_INODE, "name %s, len %ld in ino %ld, fid %s\n",
114 name, (long)length, dir->i_ino, coda_i2s(dir));
116 /* control object, create inode on the fly */
117 if (coda_isroot(dir) && coda_iscontrol(name, length)) {
118 error = coda_cnode_makectl(&res_inode, dir->i_sb);
119 CDEBUG(D_SPECIAL,
120 "Lookup on CTL object; dir ino %ld, count %d\n",
121 dir->i_ino, atomic_read(&dir->i_count));
122 dropme = 1;
123 goto exit;
126 error = venus_lookup(dir->i_sb, coda_i2f(dir),
127 (const char *)name, length, &type, &resfid);
129 res_inode = NULL;
130 if (!error) {
131 if (type & CODA_NOCACHE) {
132 type &= (~CODA_NOCACHE);
133 CDEBUG(D_INODE, "dropme set for %s\n",
134 coda_f2s(&resfid));
135 dropme = 1;
138 error = coda_cnode_make(&res_inode, &resfid, dir->i_sb);
139 if (error) return ERR_PTR(error);
141 /* make sure we drop unexpected weird fid's */
142 if (coda_f2i(&resfid) != res_inode->i_ino &&
143 !coda_fid_is_weird(&resfid))
144 dropme = 1;
145 } else if (error != -ENOENT) {
146 CDEBUG(D_INODE, "error for %s(%*s)%d\n",
147 coda_i2s(dir), (int)length, name, error);
148 return ERR_PTR(error);
150 CDEBUG(D_INODE, "lookup: %s is (%s), type %d result %d, dropme %d\n",
151 name, coda_f2s(&resfid), type, error, dropme);
153 exit:
154 entry->d_time = 0;
155 entry->d_op = &coda_dentry_operations;
156 d_add(entry, res_inode);
157 if ( dropme ) {
158 d_drop(entry);
159 coda_flag_inode(res_inode, C_VATTR);
161 EXIT;
162 return NULL;
166 int coda_permission(struct inode *inode, int mask)
168 int error;
170 ENTRY;
171 coda_vfs_stat.permission++;
172 coda_permission_stat.count++;
174 if ( mask == 0 )
175 return 0;
177 if ( coda_access_cache == 1 ) {
178 if ( coda_cache_check(inode, mask) ) {
179 coda_permission_stat.hit_count++;
180 return 0;
184 CDEBUG(D_INODE, "mask is %o\n", mask);
185 error = venus_access(inode->i_sb, coda_i2f(inode), mask);
187 CDEBUG(D_INODE, "fid: %s, ino: %ld (mask: %o) error: %d\n",
188 coda_i2s(inode), inode->i_ino, mask, error);
190 if (!error)
191 coda_cache_enter(inode, mask);
193 return error;
197 static inline void coda_dir_changed(struct inode *dir, int link)
199 #ifdef REQUERY_VENUS_FOR_MTIME
200 /* invalidate the directory cnode's attributes so we refetch the
201 * attributes from venus next time the inode is referenced */
202 coda_flag_inode(dir, C_VATTR);
203 #else
204 /* optimistically we can also act as if our nose bleeds. The
205 * granularity of the mtime is coarse anyways so we might actually be
206 * right most of the time. Note: we only do this for directories. */
207 dir->i_mtime = CURRENT_TIME;
208 #endif
209 if (link)
210 dir->i_nlink += link;
213 /* creation routines: create, mknod, mkdir, link, symlink */
214 static int coda_create(struct inode *dir, struct dentry *de, int mode)
216 int error=0;
217 const char *name=de->d_name.name;
218 int length=de->d_name.len;
219 struct inode *result = NULL;
220 struct ViceFid newfid;
221 struct coda_vattr attrs;
223 ENTRY;
224 coda_vfs_stat.create++;
226 CDEBUG(D_INODE, "name: %s, length %d, mode %o\n", name, length, mode);
228 if (coda_isroot(dir) && coda_iscontrol(name, length))
229 return -EPERM;
231 error = venus_create(dir->i_sb, coda_i2f(dir), name, length,
232 0, mode, 0, &newfid, &attrs);
234 if ( error ) {
235 CDEBUG(D_INODE, "create: %s, result %d\n",
236 coda_f2s(&newfid), error);
237 d_drop(de);
238 return error;
241 error = coda_cnode_make(&result, &newfid, dir->i_sb);
242 if ( error ) {
243 d_drop(de);
244 result = NULL;
245 return error;
248 /* invalidate the directory cnode's attributes */
249 coda_dir_changed(dir, 0);
250 d_instantiate(de, result);
251 return 0;
254 static int coda_mknod(struct inode *dir, struct dentry *de, int mode, int rdev)
256 int error=0;
257 const char *name=de->d_name.name;
258 int length=de->d_name.len;
259 struct inode *result = NULL;
260 struct ViceFid newfid;
261 struct coda_vattr attrs;
263 if ( coda_hasmknod == 0 )
264 return -EIO;
266 coda_vfs_stat.create++;
268 CDEBUG(D_INODE, "name: %s, length %d, mode %o, rdev %x\n",
269 name, length, mode, rdev);
271 if (coda_isroot(dir) && coda_iscontrol(name, length))
272 return -EPERM;
274 error = venus_create(dir->i_sb, coda_i2f(dir), name, length,
275 0, mode, rdev, &newfid, &attrs);
277 if ( error ) {
278 CDEBUG(D_INODE, "mknod: %s, result %d\n",
279 coda_f2s(&newfid), error);
280 d_drop(de);
281 return error;
284 error = coda_cnode_make(&result, &newfid, dir->i_sb);
285 if ( error ) {
286 d_drop(de);
287 result = NULL;
288 return error;
291 /* invalidate the directory cnode's attributes */
292 coda_dir_changed(dir, 0);
293 d_instantiate(de, result);
294 return 0;
297 static int coda_mkdir(struct inode *dir, struct dentry *de, int mode)
299 struct inode *inode;
300 struct coda_vattr attr;
301 const char *name = de->d_name.name;
302 int len = de->d_name.len;
303 int error;
304 struct ViceFid newfid;
306 ENTRY;
307 coda_vfs_stat.mkdir++;
309 if (coda_isroot(dir) && coda_iscontrol(name, len))
310 return -EPERM;
312 CDEBUG(D_INODE, "mkdir %s (len %d) in %s, mode %o.\n",
313 name, len, coda_i2s(dir), mode);
315 attr.va_mode = mode;
316 error = venus_mkdir(dir->i_sb, coda_i2f(dir),
317 name, len, &newfid, &attr);
319 if ( error ) {
320 CDEBUG(D_INODE, "mkdir error: %s result %d\n",
321 coda_f2s(&newfid), error);
322 d_drop(de);
323 return error;
326 CDEBUG(D_INODE, "mkdir: new dir has fid %s.\n",
327 coda_f2s(&newfid));
329 error = coda_cnode_make(&inode, &newfid, dir->i_sb);
330 if ( error ) {
331 d_drop(de);
332 return error;
335 /* invalidate the directory cnode's attributes */
336 coda_dir_changed(dir, 1);
337 d_instantiate(de, inode);
338 return 0;
341 /* try to make de an entry in dir_inodde linked to source_de */
342 static int coda_link(struct dentry *source_de, struct inode *dir_inode,
343 struct dentry *de)
345 struct inode *inode = source_de->d_inode;
346 const char * name = de->d_name.name;
347 int len = de->d_name.len;
348 int error;
350 ENTRY;
351 coda_vfs_stat.link++;
353 if (coda_isroot(dir_inode) && coda_iscontrol(name, len))
354 return -EPERM;
356 CDEBUG(D_INODE, "old: fid: %s\n", coda_i2s(inode));
357 CDEBUG(D_INODE, "directory: %s\n", coda_i2s(dir_inode));
359 error = venus_link(dir_inode->i_sb, coda_i2f(inode),
360 coda_i2f(dir_inode), (const char *)name, len);
362 if (error) {
363 d_drop(de);
364 goto out;
367 coda_dir_changed(dir_inode, 0);
368 atomic_inc(&inode->i_count);
369 d_instantiate(de, inode);
370 inode->i_nlink++;
372 out:
373 CDEBUG(D_INODE, "link result %d\n",error);
374 EXIT;
375 return(error);
379 static int coda_symlink(struct inode *dir_inode, struct dentry *de,
380 const char *symname)
382 const char *name = de->d_name.name;
383 int len = de->d_name.len;
384 int symlen;
385 int error=0;
387 ENTRY;
388 coda_vfs_stat.symlink++;
390 if (coda_isroot(dir_inode) && coda_iscontrol(name, len))
391 return -EPERM;
393 symlen = strlen(symname);
394 if ( symlen > CODA_MAXPATHLEN )
395 return -ENAMETOOLONG;
397 CDEBUG(D_INODE, "symname: %s, length: %d\n", symname, symlen);
400 * This entry is now negative. Since we do not create
401 * an inode for the entry we have to drop it.
403 d_drop(de);
404 error = venus_symlink(dir_inode->i_sb, coda_i2f(dir_inode), name, len,
405 symname, symlen);
407 /* mtime is no good anymore */
408 if ( !error )
409 coda_dir_changed(dir_inode, 0);
411 CDEBUG(D_INODE, "in symlink result %d\n",error);
412 EXIT;
413 return error;
416 /* destruction routines: unlink, rmdir */
417 int coda_unlink(struct inode *dir, struct dentry *de)
419 int error;
420 const char *name = de->d_name.name;
421 int len = de->d_name.len;
423 ENTRY;
424 coda_vfs_stat.unlink++;
426 CDEBUG(D_INODE, " %s in %s, dirino %ld\n", name ,
427 coda_i2s(dir), dir->i_ino);
429 error = venus_remove(dir->i_sb, coda_i2f(dir), name, len);
430 if ( error ) {
431 CDEBUG(D_INODE, "upc returned error %d\n", error);
432 return error;
435 coda_dir_changed(dir, 0);
436 de->d_inode->i_nlink--;
438 return 0;
441 int coda_rmdir(struct inode *dir, struct dentry *de)
443 const char *name = de->d_name.name;
444 int len = de->d_name.len;
445 int error;
447 ENTRY;
448 coda_vfs_stat.rmdir++;
450 if (!d_unhashed(de))
451 return -EBUSY;
452 error = venus_rmdir(dir->i_sb, coda_i2f(dir), name, len);
454 if ( error ) {
455 CDEBUG(D_INODE, "upc returned error %d\n", error);
456 return error;
459 coda_dir_changed(dir, -1);
460 de->d_inode->i_nlink--;
461 d_delete(de);
463 return 0;
466 /* rename */
467 static int coda_rename(struct inode *old_dir, struct dentry *old_dentry,
468 struct inode *new_dir, struct dentry *new_dentry)
470 const char *old_name = old_dentry->d_name.name;
471 const char *new_name = new_dentry->d_name.name;
472 int old_length = old_dentry->d_name.len;
473 int new_length = new_dentry->d_name.len;
474 int error;
476 ENTRY;
477 coda_vfs_stat.rename++;
479 CDEBUG(D_INODE, "old: %s, (%d length), new: %s"
480 "(%d length). old:d_count: %d, new:d_count: %d\n",
481 old_name, old_length, new_name, new_length,
482 old_dentry->d_count, new_dentry->d_count);
484 error = venus_rename(old_dir->i_sb, coda_i2f(old_dir),
485 coda_i2f(new_dir), old_length, new_length,
486 (const char *) old_name, (const char *)new_name);
488 if ( !error ) {
489 if ( new_dentry->d_inode ) {
490 if ( S_ISDIR(new_dentry->d_inode->i_mode) ) {
491 old_dir->i_nlink--;
492 new_dir->i_nlink++;
494 coda_flag_inode(new_dentry->d_inode, C_VATTR);
497 /* coda_flag_inode(old_dir, C_VATTR); */
498 /* coda_flag_inode(new_dir, C_VATTR); */
499 old_dir->i_mtime = new_dir->i_mtime = CURRENT_TIME;
502 CDEBUG(D_INODE, "result %d\n", error);
504 EXIT;
505 return error;
509 /* file operations for directories */
510 int coda_readdir(struct file *file, void *dirent, filldir_t filldir)
512 int result = 0;
513 struct file open_file;
514 struct dentry open_dentry;
515 struct inode *inode=file->f_dentry->d_inode, *container;
517 ENTRY;
518 coda_vfs_stat.readdir++;
520 if ( inode->i_mapping == &inode->i_data ) {
521 CDEBUG(D_FILE, "no container inode.\n");
522 return -EIO;
525 container = (struct inode *)inode->i_mapping->host;
527 coda_prepare_fakefile(inode, file, container, &open_file, &open_dentry);
529 if ( S_ISREG(container->i_mode) ) {
530 /* Venus: we must read Venus dirents from the file */
531 result = coda_venus_readdir(&open_file, dirent, filldir);
532 } else {
533 /* potemkin case: we are handed a directory inode */
534 result = vfs_readdir(&open_file, filldir, dirent);
537 /* we only have to restore the file position (and f_version?) */
538 file->f_pos = open_file.f_pos;
539 file->f_version = open_file.f_version;
541 EXIT;
542 return result;
545 /* grab the ext2 inode of the container file */
546 static int coda_inode_grab(dev_t dev, ino_t ino, struct inode **ind)
548 struct super_block *sbptr;
550 sbptr = get_super(dev);
552 if ( !sbptr ) {
553 printk("coda_inode_grab: coda_find_super returns NULL.\n");
554 return -ENXIO;
557 *ind = NULL;
558 *ind = iget(sbptr, ino);
560 if ( *ind == NULL ) {
561 printk("coda_inode_grab: iget(dev: %d, ino: %ld) "
562 "returns NULL.\n", dev, (long)ino);
563 return -ENOENT;
565 CDEBUG(D_FILE, "ino: %ld, ops at %p\n", (long)ino, (*ind)->i_op);
566 return 0;
569 /* ask venus to cache the file and return the inode of the container file,
570 put this inode pointer in the cnode for future read/writes */
571 int coda_open(struct inode *i, struct file *f)
573 ino_t ino;
574 dev_t dev;
575 int error = 0;
576 struct inode *cont_inode = NULL, *old_container;
577 unsigned short flags = f->f_flags & (~O_EXCL);
578 unsigned short coda_flags = coda_flags_to_cflags(flags);
579 struct coda_cred *cred;
581 ENTRY;
582 coda_vfs_stat.open++;
584 CDEBUG(D_SPECIAL, "OPEN inode number: %ld, count %d, flags %o.\n",
585 f->f_dentry->d_inode->i_ino, f->f_dentry->d_count, flags);
587 error = venus_open(i->i_sb, coda_i2f(i), coda_flags, &ino, &dev);
588 if (error) {
589 CDEBUG(D_FILE, "venus: dev %d, inode %ld, out->result %d\n",
590 dev, (long)ino, error);
591 return error;
594 /* coda_upcall returns ino number of cached object, get inode */
595 CDEBUG(D_FILE, "cache file dev %d, ino %ld\n", dev, (long)ino);
596 error = coda_inode_grab(dev, ino, &cont_inode);
598 if ( error || !cont_inode ){
599 printk("coda_open: coda_inode_grab error %d.", error);
600 if (cont_inode)
601 iput(cont_inode);
602 return error;
605 CODA_ALLOC(cred, struct coda_cred *, sizeof(*cred));
606 coda_load_creds(cred);
607 f->private_data = cred;
609 if ( i->i_mapping != &i->i_data ) {
610 old_container = (struct inode *)i->i_mapping->host;
611 i->i_mapping = &i->i_data;
612 iput(old_container);
614 i->i_mapping = cont_inode->i_mapping;
616 CDEBUG(D_FILE, "result %d, coda i->i_count is %d for ino %ld\n",
617 error, atomic_read(&i->i_count), i->i_ino);
618 CDEBUG(D_FILE, "cache ino: %ld, count %d, ops %p\n",
619 cont_inode->i_ino, atomic_read(&cont_inode->i_count),
620 cont_inode->i_op);
621 EXIT;
622 return 0;
625 int coda_release(struct inode *i, struct file *f)
627 struct inode *container = NULL;
628 int error = 0;
629 unsigned short flags = (f->f_flags) & (~O_EXCL);
630 unsigned short cflags = coda_flags_to_cflags(flags);
631 struct coda_cred *cred;
633 ENTRY;
634 coda_vfs_stat.release++;
636 cred = (struct coda_cred *)f->private_data;
638 if (i->i_mapping != &i->i_data)
639 container = (struct inode *)i->i_mapping->host;
641 CDEBUG(D_FILE, "RELEASE coda (ino %ld, ct %d) cache (ino %ld, ct %d)\n",
642 i->i_ino, atomic_read(&i->i_count),
643 (container ? container->i_ino : 0),
644 (container ? atomic_read(&container->i_count) : -99));
646 error = venus_release(i->i_sb, coda_i2f(i), cflags, cred);
648 f->private_data = NULL;
649 if (cred)
650 CODA_FREE(cred, sizeof(*cred));
652 CDEBUG(D_FILE, "coda_release: result: %d\n", error);
653 return error;
656 /* support routines */
658 /* instantiate a fake file and dentry to pass to coda_venus_readdir */
659 static void coda_prepare_fakefile(struct inode *i, struct file *coda_file,
660 struct inode *cont_inode,
661 struct file *cont_file,
662 struct dentry *cont_dentry)
664 cont_file->f_dentry = cont_dentry;
665 cont_file->f_dentry->d_inode = cont_inode;
666 cont_file->f_pos = coda_file->f_pos;
667 cont_file->f_version = coda_file->f_version;
668 cont_file->f_op = cont_inode->i_fop;
669 return ;
673 * this structure is manipulated by filldir in vfs layer.
674 * the count holds the remaining amount of space in the getdents buffer,
675 * beyond the current_dir pointer.
677 * What structure is this comment referring to?? -JH
680 /* should be big enough to hold any single directory entry */
681 #define DIR_BUFSIZE 2048
683 static int coda_venus_readdir(struct file *filp, void *getdent,
684 filldir_t filldir)
686 int bufsize;
687 int offset = filp->f_pos; /* offset in the directory file */
688 int count = 0;
689 int pos = 0; /* offset in the block we read */
690 int result = 0; /* either an error or # of entries returned */
691 int errfill;
692 char *buff = NULL;
693 struct venus_dirent *vdirent;
694 int string_offset = (int) (&((struct venus_dirent *)(0))->d_name);
695 int i;
697 ENTRY;
699 CODA_ALLOC(buff, char *, DIR_BUFSIZE);
700 if ( !buff ) {
701 printk("coda_venus_readdir: out of memory.\n");
702 return -ENOMEM;
705 /* we use this routine to read the file into our buffer */
706 bufsize = kernel_read(filp, filp->f_pos, buff, DIR_BUFSIZE);
707 if ( bufsize < 0) {
708 printk("coda_venus_readdir: cannot read directory %d.\n",
709 bufsize);
710 result = bufsize;
711 goto exit;
713 if ( bufsize == 0) {
714 result = 0;
715 goto exit;
718 /* Parse and write into user space. Filldir tells us when done! */
719 CDEBUG(D_FILE, "buffsize: %d offset %d, count %d.\n",
720 bufsize, offset, count);
722 i = 0;
723 result = 0;
724 while ( pos + string_offset < bufsize && i < 1024) {
725 vdirent = (struct venus_dirent *) (buff + pos);
727 /* test if the name is fully in the buffer */
728 if ( pos + string_offset + (int) vdirent->d_namlen >= bufsize ){
729 if ( result == 0 )
730 printk("CODA: Invalid directory cfino: %ld\n",
731 filp->f_dentry->d_inode->i_ino);
732 break;
734 /* now we are certain that we can read the entry from buff */
736 /* if we don't have a null entry, copy it */
737 if ( vdirent->d_fileno && vdirent->d_reclen ) {
738 int namlen = vdirent->d_namlen;
739 off_t offs = filp->f_pos;
740 ino_t ino = vdirent->d_fileno;
741 char *name = vdirent->d_name;
743 errfill = filldir(getdent, name, namlen,
744 offs, ino);
745 CDEBUG(D_FILE, "entry %d: ino %ld, namlen %d, reclen %d, type %d, pos %d, string_offs %d, name %*s, offset %d, result: %d, errfill: %d.\n", i,vdirent->d_fileno, vdirent->d_namlen, vdirent->d_reclen, vdirent->d_type, pos, string_offset, vdirent->d_namlen, vdirent->d_name, (u_int) offs, result, errfill);
746 /* errfill means no space for filling in this round */
747 if ( errfill < 0 ) {
748 result = 0;
749 break;
751 /* adjust count */
752 result++;
754 /* next one */
755 filp->f_pos += vdirent->d_reclen;
756 if ( filp->f_pos > filp->f_dentry->d_inode->i_size )
757 break;
758 if ( !vdirent->d_reclen ) {
759 printk("CODA: Invalid directory, cfino: %ld\n",
760 filp->f_dentry->d_inode->i_ino);
761 result = -EINVAL;
762 break;
764 pos += (unsigned int) vdirent->d_reclen;
765 i++;
768 if ( i >= 1024 ) {
769 printk("Repeating too much in readdir %ld\n",
770 filp->f_dentry->d_inode->i_ino);
771 result = -EINVAL;
774 exit:
775 CODA_FREE(buff, DIR_BUFSIZE);
776 return result;
779 /* called when a cache lookup succeeds */
780 static int coda_dentry_revalidate(struct dentry *de, int flags)
782 int valid = 1;
783 struct inode *inode = de->d_inode;
784 struct coda_inode_info *cii;
785 ENTRY;
787 if (!inode)
788 return 1;
789 if (coda_isroot(inode))
790 return 1;
791 if (is_bad_inode(inode))
792 return 0;
794 cii = ITOC(de->d_inode);
795 if (! (cii->c_flags & (C_PURGE | C_FLUSH)) )
796 return valid;
798 shrink_dcache_parent(de);
800 /* propagate for a flush */
801 if (cii->c_flags & C_FLUSH)
802 coda_flag_inode_children(inode, C_FLUSH);
804 if (de->d_count > 1) {
805 /* pretend it's valid, but don't change the flags */
806 CDEBUG(D_DOWNCALL, "BOOM for: ino %ld, %s\n",
807 de->d_inode->i_ino, coda_f2s(&cii->c_fid));
808 return 1;
811 /* clear the flags. */
812 cii->c_flags &= ~(C_VATTR | C_PURGE | C_FLUSH);
814 return 0;
818 * This is the callback from dput() when d_count is going to 0.
819 * We use this to unhash dentries with bad inodes.
821 static int coda_dentry_delete(struct dentry * dentry)
823 int flags;
825 if (!dentry->d_inode)
826 return 0;
828 flags = (ITOC(dentry->d_inode)->c_flags) & C_PURGE;
829 if (is_bad_inode(dentry->d_inode) || flags) {
830 CDEBUG(D_DOWNCALL, "bad inode, unhashing %s/%s, %ld\n",
831 dentry->d_parent->d_name.name, dentry->d_name.name,
832 dentry->d_inode->i_ino);
833 return 1;
835 return 0;
841 * This is called when we want to check if the inode has
842 * changed on the server. Coda makes this easy since the
843 * cache manager Venus issues a downcall to the kernel when this
844 * happens
846 int coda_revalidate_inode(struct dentry *dentry)
848 struct coda_vattr attr;
849 int error = 0;
850 int old_mode;
851 ino_t old_ino;
852 struct inode *inode = dentry->d_inode, *container;
853 struct coda_inode_info *cii = ITOC(inode);
855 ENTRY;
856 CDEBUG(D_INODE, "revalidating: %*s/%*s\n",
857 dentry->d_name.len, dentry->d_name.name,
858 dentry->d_parent->d_name.len, dentry->d_parent->d_name.name);
860 if ( cii->c_flags == 0 )
861 return 0;
863 if (cii->c_flags & (C_VATTR | C_PURGE | C_FLUSH)) {
864 error = venus_getattr(inode->i_sb, &(cii->c_fid), &attr);
865 if ( error )
866 goto return_bad_inode;
868 /* this inode may be lost if:
869 - it's ino changed
870 - type changes must be permitted for repair and
871 missing mount points.
873 old_mode = inode->i_mode;
874 old_ino = inode->i_ino;
875 coda_vattr_to_iattr(inode, &attr);
877 if ((old_mode & S_IFMT) != (inode->i_mode & S_IFMT)) {
878 printk("Coda: inode %ld, fid %s changed type!\n",
879 inode->i_ino, coda_f2s(&(cii->c_fid)));
882 /* the following can happen when a local fid is replaced
883 with a global one, here we lose and declare the inode bad */
884 if (inode->i_ino != old_ino)
885 goto return_bad_inode;
887 if ( cii->c_flags )
888 coda_flag_inode_children(inode, C_FLUSH);
890 cii->c_flags &= ~(C_VATTR | C_PURGE | C_FLUSH);
893 return 0;
895 return_bad_inode:
896 if ( inode->i_mapping != &inode->i_data ) {
897 container = (struct inode *)inode->i_mapping->host;
898 inode->i_mapping = &inode->i_data;
899 iput(container);
901 make_bad_inode(inode);
902 return -EIO;