Import 2.3.7pre9
[davej-history.git] / fs / coda / dir.c
blobb3c0195a3bea43e58182926a7b0743ecb5c4c504
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 void coda_dentry_delete(struct dentry *);
50 /* support routines */
51 static int coda_venus_readdir(struct file *filp, void *dirent,
52 filldir_t filldir);
53 int coda_fsync(struct file *, struct dentry *dentry);
55 int coda_crossvol_rename = 0;
56 int coda_hasmknod = 0;
58 struct dentry_operations coda_dentry_operations =
60 coda_dentry_revalidate, /* revalidate */
61 NULL, /* hash */
62 NULL, /* compare */
63 coda_dentry_delete /* delete */
66 struct inode_operations coda_dir_inode_operations =
68 &coda_dir_operations,
69 coda_create, /* create */
70 coda_lookup, /* lookup */
71 coda_link, /* link */
72 coda_unlink, /* unlink */
73 coda_symlink, /* symlink */
74 coda_mkdir, /* mkdir */
75 coda_rmdir, /* rmdir */
76 coda_mknod, /* mknod */
77 coda_rename, /* rename */
78 NULL, /* readlink */
79 NULL, /* follow_link */
80 NULL, /* readpage */
81 NULL, /* writepage */
82 NULL, /* bmap */
83 NULL, /* truncate */
84 coda_permission, /* permission */
85 NULL, /* smap */
86 NULL, /* update page */
87 coda_revalidate_inode /* revalidate */
90 struct file_operations coda_dir_operations = {
91 NULL, /* lseek */
92 NULL, /* read -- bad */
93 NULL, /* write */
94 coda_readdir, /* readdir */
95 NULL, /* select */
96 NULL, /* ioctl */
97 NULL, /* mmap */
98 coda_open, /* open */
99 NULL,
100 coda_release, /* release */
101 coda_fsync, /* fsync */
102 NULL,
103 NULL,
104 NULL
108 /* inode operations for directories */
109 /* acces routines: lookup, readlink, permission */
110 static struct dentry *coda_lookup(struct inode *dir, struct dentry *entry)
112 struct coda_inode_info *dircnp;
113 struct inode *res_inode = NULL;
114 struct ViceFid resfid;
115 int dropme = 0; /* to indicate entry should not be cached */
116 int type;
117 int error = 0;
118 const char *name = entry->d_name.name;
119 size_t length = entry->d_name.len;
121 ENTRY;
123 dircnp = ITOC(dir);
125 if ( length > CODA_MAXNAMLEN ) {
126 printk("name too long: lookup, %s (%*s)\n",
127 coda_f2s(&dircnp->c_fid), length, name);
128 return ERR_PTR(-ENAMETOOLONG);
131 CDEBUG(D_INODE, "name %s, len %d in ino %ld, fid %s\n",
132 name, length, dir->i_ino, coda_f2s(&dircnp->c_fid));
134 /* control object, create inode on the fly */
135 if (coda_isroot(dir) && coda_iscontrol(name, length)) {
136 error = coda_cnode_makectl(&res_inode, dir->i_sb);
137 CDEBUG(D_SPECIAL,
138 "Lookup on CTL object; dir ino %ld, count %d\n",
139 dir->i_ino, dir->i_count);
140 goto exit;
143 error = venus_lookup(dir->i_sb, &(dircnp->c_fid),
144 (const char *)name, length, &type, &resfid);
146 res_inode = NULL;
147 if (!error) {
148 if (type & CODA_NOCACHE) {
149 type &= (~CODA_NOCACHE);
150 CDEBUG(D_INODE, "dropme set for %s\n",
151 coda_f2s(&resfid));
152 dropme = 1;
154 error = coda_cnode_make(&res_inode, &resfid, dir->i_sb);
155 if (error)
156 return ERR_PTR(error);
157 } else if (error != -ENOENT) {
158 CDEBUG(D_INODE, "error for %s(%*s)%d\n",
159 coda_f2s(&dircnp->c_fid), length, name, error);
160 return ERR_PTR(error);
162 CDEBUG(D_INODE, "lookup: %s is (%s), type %d result %d, dropme %d\n",
163 name, coda_f2s(&resfid), type, error, dropme);
165 exit:
166 entry->d_time = 0;
167 entry->d_op = &coda_dentry_operations;
168 d_add(entry, res_inode);
169 if ( dropme ) {
170 d_drop(entry);
171 ITOC(res_inode)->c_flags |= C_VATTR;
173 EXIT;
174 return NULL;
178 int coda_permission(struct inode *inode, int mask)
180 struct coda_inode_info *cp = ITOC(inode);
181 int error;
183 ENTRY;
184 coda_vfs_stat.permission++;
185 coda_permission_stat.count++;
187 if ( mask == 0 ) {
188 return 0;
191 if ( coda_access_cache == 1 ) {
192 if ( coda_cache_check(inode, mask) ) {
193 coda_permission_stat.hit_count++;
194 return 0;
198 cp = ITOC(inode);
200 CDEBUG(D_INODE, "mask is %o\n", mask);
201 error = venus_access(inode->i_sb, &(cp->c_fid), mask);
203 CDEBUG(D_INODE, "fid: %s, ino: %ld (mask: %o) error: %d\n",
204 coda_f2s(&(cp->c_fid)), inode->i_ino, mask, error);
206 if ( error == 0 ) {
207 coda_cache_enter(inode, mask);
210 return error;
215 /* creation routines: create, mknod, mkdir, link, symlink */
217 static int coda_create(struct inode *dir, struct dentry *de, int mode)
219 int error=0;
220 struct coda_inode_info *dircnp;
221 const char *name=de->d_name.name;
222 int length=de->d_name.len;
223 struct inode *result = NULL;
224 struct ViceFid newfid;
225 struct coda_vattr attrs;
227 ENTRY;
228 coda_vfs_stat.create++;
230 CDEBUG(D_INODE, "name: %s, length %d, mode %o\n",name, length, mode);
232 if (coda_isroot(dir) && coda_iscontrol(name, length))
233 return -EPERM;
235 dircnp = ITOC(dir);
237 error = venus_create(dir->i_sb, &(dircnp->c_fid), name, length,
238 0, mode, 0, &newfid, &attrs);
240 if ( error ) {
241 CDEBUG(D_INODE, "create: %s, result %d\n",
242 coda_f2s(&newfid), error);
243 d_drop(de);
244 return error;
247 error = coda_cnode_make(&result, &newfid, dir->i_sb);
248 if ( error ) {
249 d_drop(de);
250 result = NULL;
251 return error;
254 /* invalidate the directory cnode's attributes */
255 dircnp->c_flags |= C_VATTR;
256 d_instantiate(de, result);
257 return 0;
260 static int coda_mknod(struct inode *dir, struct dentry *de, int mode, int rdev)
262 int error=0;
263 struct coda_inode_info *dircnp;
264 const char *name=de->d_name.name;
265 int length=de->d_name.len;
266 struct inode *result = NULL;
267 struct ViceFid newfid;
268 struct coda_vattr attrs;
270 if ( coda_hasmknod == 0 )
271 return -EIO;
273 coda_vfs_stat.create++;
275 CDEBUG(D_INODE, "name: %s, length %d, mode %o, rdev %x\n",name, length, mode, rdev);
277 if (coda_isroot(dir) && coda_iscontrol(name, length))
278 return -EPERM;
280 dircnp = ITOC(dir);
282 error = venus_create(dir->i_sb, &(dircnp->c_fid), name, length,
283 0, mode, rdev, &newfid, &attrs);
285 if ( error ) {
286 CDEBUG(D_INODE, "mknod: %s, result %d\n",
287 coda_f2s(&newfid), error);
288 d_drop(de);
289 return error;
292 error = coda_cnode_make(&result, &newfid, dir->i_sb);
293 if ( error ) {
294 d_drop(de);
295 result = NULL;
296 return error;
299 /* invalidate the directory cnode's attributes */
300 dircnp->c_flags |= C_VATTR;
301 d_instantiate(de, result);
302 return 0;
305 static int coda_mkdir(struct inode *dir, struct dentry *de, int mode)
307 struct coda_inode_info *dircnp;
308 struct inode *inode;
309 struct coda_vattr attr;
310 const char *name = de->d_name.name;
311 int len = de->d_name.len;
312 int error;
313 struct ViceFid newfid;
315 ENTRY;
316 coda_vfs_stat.mkdir++;
318 if (coda_isroot(dir) && coda_iscontrol(name, len))
319 return -EPERM;
321 dircnp = ITOC(dir);
323 CDEBUG(D_INODE, "mkdir %s (len %d) in %s, mode %o.\n",
324 name, len, coda_f2s(&(dircnp->c_fid)), mode);
326 attr.va_mode = mode;
327 error = venus_mkdir(dir->i_sb, &(dircnp->c_fid),
328 name, len, &newfid, &attr);
330 if ( error ) {
331 CDEBUG(D_INODE, "mkdir error: %s result %d\n",
332 coda_f2s(&newfid), error);
333 d_drop(de);
334 return error;
337 CDEBUG(D_INODE, "mkdir: new dir has fid %s.\n",
338 coda_f2s(&newfid));
340 error = coda_cnode_make(&inode, &newfid, dir->i_sb);
341 if ( error ) {
342 d_drop(de);
343 return error;
346 /* invalidate the directory cnode's attributes */
347 dircnp->c_flags |= C_VATTR;
348 dir->i_nlink++;
349 d_instantiate(de, inode);
350 return 0;
353 /* try to make de an entry in dir_inodde linked to source_de */
354 static int coda_link(struct dentry *source_de, struct inode *dir_inode,
355 struct dentry *de)
357 struct inode *inode = source_de->d_inode;
358 const char * name = de->d_name.name;
359 int len = de->d_name.len;
360 struct coda_inode_info *dir_cnp, *cnp;
361 int error;
363 ENTRY;
364 coda_vfs_stat.link++;
366 if (coda_isroot(dir_inode) && coda_iscontrol(name, len))
367 return -EPERM;
369 dir_cnp = ITOC(dir_inode);
370 cnp = ITOC(inode);
372 CDEBUG(D_INODE, "old: fid: %s\n", coda_f2s(&(cnp->c_fid)));
373 CDEBUG(D_INODE, "directory: %s\n", coda_f2s(&(dir_cnp->c_fid)));
375 error = venus_link(dir_inode->i_sb,&(cnp->c_fid), &(dir_cnp->c_fid),
376 (const char *)name, len);
378 if ( ! error ) {
379 dir_cnp->c_flags |= C_VATTR;
380 ++inode->i_count;
381 d_instantiate(de, inode);
382 inode->i_nlink++;
383 } else {
384 d_drop(de);
385 return error;
388 CDEBUG(D_INODE, "link result %d\n",error);
389 EXIT;
390 return(error);
394 static int coda_symlink(struct inode *dir_inode, struct dentry *de,
395 const char *symname)
397 const char *name = de->d_name.name;
398 int len = de->d_name.len;
399 struct coda_inode_info *dir_cnp = ITOC(dir_inode);
400 int symlen;
401 int error=0;
403 ENTRY;
404 coda_vfs_stat.symlink++;
406 if (coda_isroot(dir_inode) && coda_iscontrol(name, len))
407 return -EPERM;
409 symlen = strlen(symname);
410 if ( symlen > CODA_MAXPATHLEN )
411 return -ENAMETOOLONG;
413 CDEBUG(D_INODE, "symname: %s, length: %d\n", symname, symlen);
416 * This entry is now negative. Since we do not create
417 * an inode for the entry we have to drop it.
419 d_drop(de);
420 error = venus_symlink(dir_inode->i_sb, &(dir_cnp->c_fid), name, len,
421 symname, symlen);
423 /* mtime is no good anymore */
424 if ( !error ) {
425 dir_cnp->c_flags |= C_VATTR;
428 CDEBUG(D_INODE, "in symlink result %d\n",error);
429 EXIT;
430 return error;
433 /* destruction routines: unlink, rmdir */
434 int coda_unlink(struct inode *dir, struct dentry *de)
436 struct coda_inode_info *dircnp = ITOC(dir);
437 int error;
438 const char *name = de->d_name.name;
439 int len = de->d_name.len;
441 ENTRY;
442 coda_vfs_stat.unlink++;
444 CDEBUG(D_INODE, " %s in %s, dirino %ld\n", name ,
445 coda_f2s(&(dircnp->c_fid)), dir->i_ino);
447 error = venus_remove(dir->i_sb, &(dircnp->c_fid), name, len);
448 if ( error ) {
449 CDEBUG(D_INODE, "upc returned error %d\n", error);
450 return error;
453 /* cache management: mtime has changed, ask Venus */
454 dircnp->c_flags |= C_VATTR;
455 de->d_inode->i_nlink--;
456 d_delete(de);
458 return 0;
461 int coda_rmdir(struct inode *dir, struct dentry *de)
463 struct coda_inode_info *dircnp;
464 const char *name = de->d_name.name;
465 int len = de->d_name.len;
466 int error;
468 ENTRY;
469 coda_vfs_stat.rmdir++;
471 dircnp = ITOC(dir);
473 if (!list_empty(&de->d_hash))
474 return -EBUSY;
475 error = venus_rmdir(dir->i_sb, &(dircnp->c_fid), name, len);
477 if ( error ) {
478 CDEBUG(D_INODE, "upc returned error %d\n", error);
479 return error;
482 if (de->d_inode->i_nlink)
483 de->d_inode->i_nlink --;
485 return 0;
488 /* rename */
489 static int coda_rename(struct inode *old_dir, struct dentry *old_dentry,
490 struct inode *new_dir, struct dentry *new_dentry)
492 const char *old_name = old_dentry->d_name.name;
493 const char *new_name = new_dentry->d_name.name;
494 int old_length = old_dentry->d_name.len;
495 int new_length = new_dentry->d_name.len;
496 struct inode *new_inode = new_dentry->d_inode;
497 struct coda_inode_info *new_cnp, *old_cnp;
498 int error;
500 ENTRY;
501 coda_vfs_stat.rename++;
503 old_cnp = ITOC(old_dir);
504 new_cnp = ITOC(new_dir);
506 CDEBUG(D_INODE, "old: %s, (%d length, %d strlen), new: %s"
507 "(%d length, %d strlen).old:d_count: %d, new:d_count: %d\n",
508 old_name, old_length, strlen(old_name), new_name, new_length,
509 strlen(new_name),old_dentry->d_count, new_dentry->d_count);
511 /* the C library will do unlink/create etc */
512 if ( coda_crossvol_rename == 0 &&
513 old_cnp->c_fid.Volume != new_cnp->c_fid.Volume )
514 return -EXDEV;
516 error = venus_rename(old_dir->i_sb, &(old_cnp->c_fid),
517 &(new_cnp->c_fid), old_length, new_length,
518 (const char *) old_name, (const char *)new_name);
520 if ( error ) {
521 CDEBUG(D_INODE, "returned error %d\n", error);
522 return error;
525 coda_flag_inode(new_inode, C_VATTR);
526 coda_flag_inode(old_dir, C_VATTR);
527 coda_flag_inode(new_dir, C_VATTR);
529 CDEBUG(D_INODE, "result %d\n", error);
531 EXIT;
532 return 0;
537 /* file operations for directories */
538 int coda_readdir(struct file *file, void *dirent, filldir_t filldir)
540 int result = 0;
541 struct coda_inode_info *cnp;
542 struct file open_file;
543 struct dentry open_dentry;
544 struct inode *inode=file->f_dentry->d_inode;
546 ENTRY;
547 coda_vfs_stat.readdir++;
549 cnp = ITOC(inode);
550 if ( !cnp->c_ovp ) {
551 CDEBUG(D_FILE, "open inode pointer = NULL.\n");
552 return -EIO;
555 coda_prepare_openfile(inode, file, cnp->c_ovp, &open_file,
556 &open_dentry);
557 if ( S_ISREG(cnp->c_ovp->i_mode) ) {
558 /* Venus: we must read Venus dirents from the file */
559 result = coda_venus_readdir(&open_file, dirent, filldir);
560 } else {
561 /* potemkin case: we are handed a directory inode */
562 down(&cnp->c_ovp->i_sem);
563 result = open_file.f_op->readdir(&open_file, dirent, filldir);
564 up(&cnp->c_ovp->i_sem);
566 coda_restore_codafile(inode, file, cnp->c_ovp, &open_file);
567 EXIT;
568 return result;
571 /* ask venus to cache the file and return the inode of the container file,
572 put this inode pointer in the cnode for future read/writes */
573 int coda_open(struct inode *i, struct file *f)
575 ino_t ino;
576 dev_t dev;
577 struct coda_inode_info *cnp;
578 int error = 0;
579 struct inode *cont_inode = NULL;
580 unsigned short flags = f->f_flags & (~O_EXCL);
581 unsigned short coda_flags = coda_flags_to_cflags(flags);
582 struct coda_cred *cred;
584 ENTRY;
585 coda_vfs_stat.open++;
587 CDEBUG(D_SPECIAL, "OPEN inode number: %ld, count %d, flags %o.\n",
588 f->f_dentry->d_inode->i_ino, f->f_dentry->d_count, flags);
590 cnp = ITOC(i);
593 error = venus_open(i->i_sb, &(cnp->c_fid), coda_flags, &ino, &dev);
594 if (error) {
595 CDEBUG(D_FILE, "venus: dev %d, inode %ld, out->result %d\n",
596 dev, ino, error);
597 return error;
600 /* coda_upcall returns ino number of cached object, get inode */
601 CDEBUG(D_FILE, "cache file dev %d, ino %ld\n", dev, ino);
602 error = coda_inode_grab(dev, ino, &cont_inode);
604 if ( error || !cont_inode ){
605 printk("coda_open: coda_inode_grab error %d.", error);
606 if (cont_inode)
607 iput(cont_inode);
608 return error;
611 CODA_ALLOC(cred, struct coda_cred *, sizeof(*cred));
612 coda_load_creds(cred);
613 f->private_data = cred;
615 if ( cnp->c_ovp ) {
616 iput(cnp->c_ovp);
617 cnp->c_ovp = NULL;
619 cnp->c_ovp = cont_inode;
620 cnp->c_ocount++;
622 CDEBUG(D_FILE, "result %d, coda i->i_count is %d for ino %ld\n",
623 error, i->i_count, i->i_ino);
624 CDEBUG(D_FILE, "cache ino: %ld, count %d, ops %x\n",
625 cnp->c_ovp->i_ino, cnp->c_ovp->i_count,
626 (int)(cnp->c_ovp->i_op));
627 EXIT;
628 return 0;
631 int coda_release(struct inode *i, struct file *f)
633 struct coda_inode_info *cnp;
634 int error;
635 unsigned short flags = (f->f_flags) & (~O_EXCL);
636 unsigned short cflags = coda_flags_to_cflags(flags);
637 struct coda_cred *cred;
639 ENTRY;
640 coda_vfs_stat.release++;
642 cred = (struct coda_cred *)f->private_data;
644 cnp =ITOC(i);
645 CHECK_CNODE(cnp);
646 CDEBUG(D_FILE,
647 "RELEASE coda (ino %ld, ct %d) cache (ino %ld, ct %d)\n",
648 i->i_ino, i->i_count, (cnp->c_ovp ? cnp->c_ovp->i_ino : 0),
649 (cnp->c_ovp ? cnp->c_ovp->i_count : -99));
652 /* even when c_ocount=0 we cannot put c_ovp to
653 * NULL since the file may be mmapped.
654 * See code in inode.c (coda_put_inode) for
655 * further handling of close.
658 --cnp->c_ocount;
660 if (flags & (O_WRONLY | O_RDWR)) {
661 --cnp->c_owrite;
664 error = venus_release(i->i_sb, &(cnp->c_fid), cflags, cred);
666 CODA_FREE(cred, sizeof(*cred));
667 f->private_data = NULL;
669 CDEBUG(D_FILE, "coda_release: result: %d\n", error);
670 return error;
673 /* support routines */
675 * this structure is manipulated by filldir in vfs layer.
676 * the count holds the remaining amount of space in the getdents buffer,
677 * beyond the current_dir pointer.
681 /* should be big enough to hold any single directory entry */
682 #define DIR_BUFSIZE 2048
684 static int coda_venus_readdir(struct file *filp, void *getdent,
685 filldir_t filldir)
687 int bufsize;
688 int offset = filp->f_pos; /* offset in the directory file */
689 int count = 0;
690 int pos = 0; /* offset in the block we read */
691 int result = 0; /* either an error or # of entries returned */
692 int errfill;
693 char *buff = NULL;
694 struct venus_dirent *vdirent;
695 int string_offset = (int) (&((struct venus_dirent *)(0))->d_name);
696 int i;
698 ENTRY;
700 CODA_ALLOC(buff, char *, DIR_BUFSIZE);
701 if ( !buff ) {
702 printk("coda_venus_readdir: out of memory.\n");
703 return -ENOMEM;
706 /* we use this routine to read the file into our buffer */
707 bufsize = read_exec(filp->f_dentry, filp->f_pos, buff, DIR_BUFSIZE, 1);
708 if ( bufsize < 0) {
709 printk("coda_venus_readdir: cannot read directory %d.\n",
710 bufsize);
711 result = bufsize;
712 goto exit;
714 if ( bufsize == 0) {
715 result = 0;
716 goto exit;
719 /* Parse and write into user space. Filldir tells us when done! */
720 CDEBUG(D_FILE, "buffsize: %d offset %d, count %d.\n",
721 bufsize, offset, count);
723 i = 0;
724 result = 0;
725 while ( pos + string_offset < bufsize && i < 1024) {
726 vdirent = (struct venus_dirent *) (buff + pos);
728 /* test if the name is fully in the buffer */
729 if ( pos + string_offset + (int) vdirent->d_namlen >= bufsize ){
730 if ( result == 0 )
731 printk("CODA: Invalid directory cfino: %ld\n",
732 filp->f_dentry->d_inode->i_ino);
733 break;
735 /* now we are certain that we can read the entry from buff */
737 /* if we don't have a null entry, copy it */
738 if ( vdirent->d_fileno && vdirent->d_reclen ) {
739 int namlen = vdirent->d_namlen;
740 off_t offs = filp->f_pos;
741 ino_t ino = vdirent->d_fileno;
742 char *name = vdirent->d_name;
744 errfill = filldir(getdent, name, namlen,
745 offs, ino);
746 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);
747 /* errfill means no space for filling in this round */
748 if ( errfill < 0 ) {
749 result = 0;
750 break;
752 /* adjust count */
753 result++;
755 /* next one */
756 filp->f_pos += vdirent->d_reclen;
757 if ( filp->f_pos > filp->f_dentry->d_inode->i_size )
758 break;
759 if ( !vdirent->d_reclen ) {
760 printk("CODA: Invalid directory, cfino: %ld\n",
761 filp->f_dentry->d_inode->i_ino);
762 result = -EINVAL;
763 break;
765 pos += (unsigned int) vdirent->d_reclen;
766 i++;
769 if ( i >= 1024 ) {
770 printk("Repeating too much in readdir %ld\n",
771 filp->f_dentry->d_inode->i_ino);
772 result = -EINVAL;
775 exit:
776 CODA_FREE(buff, DIR_BUFSIZE);
777 return result;
780 /* called when a cache lookup succeeds */
781 static int coda_dentry_revalidate(struct dentry *de, int flags)
783 int valid = 1;
784 struct inode *inode = de->d_inode;
785 struct coda_inode_info *cii;
786 ENTRY;
788 if (!inode)
789 return 1;
791 cii = ITOC(de->d_inode);
792 if (coda_isroot(inode))
793 return 1;
794 if (is_bad_inode(inode))
795 return 0;
797 if (! (cii->c_flags & (C_PURGE | C_FLUSH)) )
798 return valid;
800 shrink_dcache_parent(de);
802 if (de->d_count > 1) {
803 /* pretend it's valid, but don't change the flags */
804 CDEBUG(D_DOWNCALL, "BOOM for: ino %ld, %s\n",
805 de->d_inode->i_ino, coda_f2s(&cii->c_fid));
806 return 1;
809 /* propagate for a flush */
810 if (cii->c_flags & C_FLUSH)
811 coda_flag_inode_children(inode, C_FLUSH);
813 /* clear the flags. */
814 cii->c_flags &= ~(C_VATTR | C_PURGE | C_FLUSH);
816 return 0;
820 * This is the callback from dput() when d_count is going to 0.
821 * We use this to unhash dentries with bad inodes.
823 static void coda_dentry_delete(struct dentry * dentry)
825 int flags;
827 if (!dentry->d_inode)
828 return ;
830 flags = (ITOC(dentry->d_inode)->c_flags) & C_PURGE;
831 if (is_bad_inode(dentry->d_inode) || flags) {
832 CDEBUG(D_DOWNCALL, "bad inode, unhashing %s/%s, %ld\n",
833 dentry->d_parent->d_name.name, dentry->d_name.name,
834 dentry->d_inode->i_ino);
835 d_drop(dentry);
842 * This is called when we want to check if the inode has
843 * changed on the server. Coda makes this easy since the
844 * cache manager Venus issues a downcall to the kernel when this
845 * happens
848 int coda_revalidate_inode(struct dentry *dentry)
850 struct coda_vattr attr;
851 int error = 0;
852 int old_mode;
853 ino_t old_ino;
854 struct inode *inode = dentry->d_inode;
855 struct coda_inode_info *cii = ITOC(inode);
857 ENTRY;
858 CDEBUG(D_INODE, "revalidating: %*s/%*s\n",
859 dentry->d_name.len, dentry->d_name.name,
860 dentry->d_parent->d_name.len, dentry->d_parent->d_name.name);
862 if ( cii->c_flags == 0 )
863 return 0;
865 /* Venus closed the device .... */
866 if ( cii->c_flags & C_DYING ) {
867 make_bad_inode(inode);
868 return -EIO;
872 if (cii->c_flags & (C_VATTR | C_PURGE | C_FLUSH)) {
873 error = venus_getattr(inode->i_sb, &(cii->c_fid), &attr);
874 if ( error ) {
875 make_bad_inode(inode);
876 return -EIO;
879 /* this inode may be lost if:
880 - it's ino changed
881 - type changes must be permitted for repair and
882 missing mount points.
884 old_mode = inode->i_mode;
885 old_ino = inode->i_ino;
886 coda_vattr_to_iattr(inode, &attr);
889 if ((old_mode & S_IFMT) != (inode->i_mode & S_IFMT)) {
890 printk("Coda: inode %ld, fid %s changed type!\n",
891 inode->i_ino, coda_f2s(&(cii->c_fid)));
894 /* the following can happen when a local fid is replaced
895 with a global one, here we lose and declar the inode bad */
896 if (inode->i_ino != old_ino) {
897 make_bad_inode(inode);
898 inode->i_mode = old_mode;
899 return -EIO;
902 if ( cii->c_flags )
903 coda_flag_inode_children(inode, C_FLUSH);
905 cii->c_flags &= ~(C_VATTR | C_PURGE | C_FLUSH);
908 return 0;