Import 2.3.48
[davej-history.git] / fs / coda / dir.c
blobdac8e8884fc568ebc812601d61bdb9653e47565e
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 create: coda_create,
69 lookup: coda_lookup,
70 link: coda_link,
71 unlink: coda_unlink,
72 symlink: coda_symlink,
73 mkdir: coda_mkdir,
74 rmdir: coda_rmdir,
75 mknod: coda_mknod,
76 rename: coda_rename,
77 permission: coda_permission,
78 revalidate: coda_revalidate_inode,
79 setattr: coda_notify_change,
82 struct file_operations coda_dir_operations = {
83 read: generic_read_dir,
84 readdir: coda_readdir,
85 open: coda_open,
86 release: coda_release,
87 fsync: coda_fsync,
91 /* inode operations for directories */
92 /* acces routines: lookup, readlink, permission */
93 static struct dentry *coda_lookup(struct inode *dir, struct dentry *entry)
95 struct coda_inode_info *dircnp;
96 struct inode *res_inode = NULL;
97 struct ViceFid resfid;
98 int dropme = 0; /* to indicate entry should not be cached */
99 int type;
100 int error = 0;
101 const char *name = entry->d_name.name;
102 size_t length = entry->d_name.len;
104 ENTRY;
106 dircnp = ITOC(dir);
108 if ( length > CODA_MAXNAMLEN ) {
109 printk("name too long: lookup, %s (%*s)\n",
110 coda_f2s(&dircnp->c_fid), (int)length, name);
111 return ERR_PTR(-ENAMETOOLONG);
114 CDEBUG(D_INODE, "name %s, len %ld in ino %ld, fid %s\n",
115 name, (long)length, dir->i_ino, coda_f2s(&dircnp->c_fid));
117 /* control object, create inode on the fly */
118 if (coda_isroot(dir) && coda_iscontrol(name, length)) {
119 error = coda_cnode_makectl(&res_inode, dir->i_sb);
120 CDEBUG(D_SPECIAL,
121 "Lookup on CTL object; dir ino %ld, count %d\n",
122 dir->i_ino, dir->i_count);
123 goto exit;
126 error = venus_lookup(dir->i_sb, &(dircnp->c_fid),
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;
137 error = coda_cnode_make(&res_inode, &resfid, dir->i_sb);
138 if (error)
139 return ERR_PTR(error);
140 } else if (error != -ENOENT) {
141 CDEBUG(D_INODE, "error for %s(%*s)%d\n",
142 coda_f2s(&dircnp->c_fid), (int)length, name, error);
143 return ERR_PTR(error);
145 CDEBUG(D_INODE, "lookup: %s is (%s), type %d result %d, dropme %d\n",
146 name, coda_f2s(&resfid), type, error, dropme);
148 exit:
149 entry->d_time = 0;
150 entry->d_op = &coda_dentry_operations;
151 d_add(entry, res_inode);
152 if ( dropme ) {
153 d_drop(entry);
154 ITOC(res_inode)->c_flags |= C_VATTR;
156 EXIT;
157 return NULL;
161 int coda_permission(struct inode *inode, int mask)
163 struct coda_inode_info *cp = ITOC(inode);
164 int error;
166 ENTRY;
167 coda_vfs_stat.permission++;
168 coda_permission_stat.count++;
170 if ( mask == 0 ) {
171 return 0;
174 if ( coda_access_cache == 1 ) {
175 if ( coda_cache_check(inode, mask) ) {
176 coda_permission_stat.hit_count++;
177 return 0;
181 cp = ITOC(inode);
183 CDEBUG(D_INODE, "mask is %o\n", mask);
184 error = venus_access(inode->i_sb, &(cp->c_fid), mask);
186 CDEBUG(D_INODE, "fid: %s, ino: %ld (mask: %o) error: %d\n",
187 coda_f2s(&(cp->c_fid)), inode->i_ino, mask, error);
189 if ( error == 0 ) {
190 coda_cache_enter(inode, mask);
193 return error;
197 /* creation routines: create, mknod, mkdir, link, symlink */
199 static int coda_create(struct inode *dir, struct dentry *de, int mode)
201 int error=0;
202 struct coda_inode_info *dircnp;
203 const char *name=de->d_name.name;
204 int length=de->d_name.len;
205 struct inode *result = NULL;
206 struct ViceFid newfid;
207 struct coda_vattr attrs;
209 ENTRY;
210 coda_vfs_stat.create++;
212 CDEBUG(D_INODE, "name: %s, length %d, mode %o\n",name, length, mode);
214 if (coda_isroot(dir) && coda_iscontrol(name, length))
215 return -EPERM;
217 dircnp = ITOC(dir);
219 error = venus_create(dir->i_sb, &(dircnp->c_fid), name, length,
220 0, mode, 0, &newfid, &attrs);
222 if ( error ) {
223 CDEBUG(D_INODE, "create: %s, result %d\n",
224 coda_f2s(&newfid), error);
225 d_drop(de);
226 return error;
229 error = coda_cnode_make(&result, &newfid, dir->i_sb);
230 if ( error ) {
231 d_drop(de);
232 result = NULL;
233 return error;
236 /* invalidate the directory cnode's attributes */
237 dircnp->c_flags |= C_VATTR;
238 d_instantiate(de, result);
239 return 0;
242 static int coda_mknod(struct inode *dir, struct dentry *de, int mode, int rdev)
244 int error=0;
245 struct coda_inode_info *dircnp;
246 const char *name=de->d_name.name;
247 int length=de->d_name.len;
248 struct inode *result = NULL;
249 struct ViceFid newfid;
250 struct coda_vattr attrs;
252 if ( coda_hasmknod == 0 )
253 return -EIO;
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))
261 return -EPERM;
263 dircnp = ITOC(dir);
265 error = venus_create(dir->i_sb, &(dircnp->c_fid), name, length,
266 0, mode, rdev, &newfid, &attrs);
268 if ( error ) {
269 CDEBUG(D_INODE, "mknod: %s, result %d\n",
270 coda_f2s(&newfid), error);
271 d_drop(de);
272 return error;
275 error = coda_cnode_make(&result, &newfid, dir->i_sb);
276 if ( error ) {
277 d_drop(de);
278 result = NULL;
279 return error;
282 /* invalidate the directory cnode's attributes */
283 dircnp->c_flags |= C_VATTR;
284 d_instantiate(de, result);
285 return 0;
288 static int coda_mkdir(struct inode *dir, struct dentry *de, int mode)
290 struct coda_inode_info *dircnp;
291 struct inode *inode;
292 struct coda_vattr attr;
293 const char *name = de->d_name.name;
294 int len = de->d_name.len;
295 int error;
296 struct ViceFid newfid;
298 ENTRY;
299 coda_vfs_stat.mkdir++;
301 if (coda_isroot(dir) && coda_iscontrol(name, len))
302 return -EPERM;
304 dircnp = ITOC(dir);
306 CDEBUG(D_INODE, "mkdir %s (len %d) in %s, mode %o.\n",
307 name, len, coda_f2s(&(dircnp->c_fid)), mode);
309 attr.va_mode = mode;
310 error = venus_mkdir(dir->i_sb, &(dircnp->c_fid),
311 name, len, &newfid, &attr);
313 if ( error ) {
314 CDEBUG(D_INODE, "mkdir error: %s result %d\n",
315 coda_f2s(&newfid), error);
316 d_drop(de);
317 return error;
320 CDEBUG(D_INODE, "mkdir: new dir has fid %s.\n",
321 coda_f2s(&newfid));
323 error = coda_cnode_make(&inode, &newfid, dir->i_sb);
324 if ( error ) {
325 d_drop(de);
326 return error;
329 /* invalidate the directory cnode's attributes */
330 dircnp->c_flags |= C_VATTR;
331 dir->i_nlink++;
332 d_instantiate(de, inode);
333 return 0;
336 /* try to make de an entry in dir_inodde linked to source_de */
337 static int coda_link(struct dentry *source_de, struct inode *dir_inode,
338 struct dentry *de)
340 struct inode *inode = source_de->d_inode;
341 const char * name = de->d_name.name;
342 int len = de->d_name.len;
343 struct coda_inode_info *dir_cnp, *cnp;
344 int error;
346 ENTRY;
347 coda_vfs_stat.link++;
349 if (coda_isroot(dir_inode) && coda_iscontrol(name, len))
350 return -EPERM;
352 dir_cnp = ITOC(dir_inode);
353 cnp = ITOC(inode);
355 CDEBUG(D_INODE, "old: fid: %s\n", coda_f2s(&(cnp->c_fid)));
356 CDEBUG(D_INODE, "directory: %s\n", coda_f2s(&(dir_cnp->c_fid)));
358 error = venus_link(dir_inode->i_sb,&(cnp->c_fid), &(dir_cnp->c_fid),
359 (const char *)name, len);
361 if ( ! error ) {
362 dir_cnp->c_flags |= C_VATTR;
363 ++inode->i_count;
364 d_instantiate(de, inode);
365 inode->i_nlink++;
366 } else {
367 d_drop(de);
368 return error;
371 CDEBUG(D_INODE, "link result %d\n",error);
372 EXIT;
373 return(error);
377 static int coda_symlink(struct inode *dir_inode, struct dentry *de,
378 const char *symname)
380 const char *name = de->d_name.name;
381 int len = de->d_name.len;
382 struct coda_inode_info *dir_cnp = ITOC(dir_inode);
383 int symlen;
384 int error=0;
386 ENTRY;
387 coda_vfs_stat.symlink++;
389 if (coda_isroot(dir_inode) && coda_iscontrol(name, len))
390 return -EPERM;
392 symlen = strlen(symname);
393 if ( symlen > CODA_MAXPATHLEN )
394 return -ENAMETOOLONG;
396 CDEBUG(D_INODE, "symname: %s, length: %d\n", symname, symlen);
399 * This entry is now negative. Since we do not create
400 * an inode for the entry we have to drop it.
402 d_drop(de);
403 error = venus_symlink(dir_inode->i_sb, &(dir_cnp->c_fid), name, len,
404 symname, symlen);
406 /* mtime is no good anymore */
407 if ( !error ) {
408 dir_cnp->c_flags |= C_VATTR;
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 struct coda_inode_info *dircnp = ITOC(dir);
420 int error;
421 const char *name = de->d_name.name;
422 int len = de->d_name.len;
424 ENTRY;
425 coda_vfs_stat.unlink++;
427 CDEBUG(D_INODE, " %s in %s, dirino %ld\n", name ,
428 coda_f2s(&(dircnp->c_fid)), dir->i_ino);
430 error = venus_remove(dir->i_sb, &(dircnp->c_fid), name, len);
431 if ( error ) {
432 CDEBUG(D_INODE, "upc returned error %d\n", error);
433 return error;
436 /* cache management: mtime has changed, ask Venus */
437 dircnp->c_flags |= C_VATTR;
438 de->d_inode->i_nlink--;
439 d_delete(de);
441 return 0;
444 int coda_rmdir(struct inode *dir, struct dentry *de)
446 struct coda_inode_info *dircnp;
447 const char *name = de->d_name.name;
448 int len = de->d_name.len;
449 int error;
451 ENTRY;
452 coda_vfs_stat.rmdir++;
454 dircnp = ITOC(dir);
456 if (!list_empty(&de->d_hash))
457 return -EBUSY;
458 error = venus_rmdir(dir->i_sb, &(dircnp->c_fid), name, len);
460 if ( error ) {
461 CDEBUG(D_INODE, "upc returned error %d\n", error);
462 return error;
465 if (de->d_inode->i_nlink)
466 de->d_inode->i_nlink --;
468 return 0;
471 /* rename */
472 static int coda_rename(struct inode *old_dir, struct dentry *old_dentry,
473 struct inode *new_dir, struct dentry *new_dentry)
475 const char *old_name = old_dentry->d_name.name;
476 const char *new_name = new_dentry->d_name.name;
477 int old_length = old_dentry->d_name.len;
478 int new_length = new_dentry->d_name.len;
479 struct inode *new_inode = new_dentry->d_inode;
480 struct coda_inode_info *new_cnp, *old_cnp;
481 int error;
483 ENTRY;
484 coda_vfs_stat.rename++;
486 old_cnp = ITOC(old_dir);
487 new_cnp = ITOC(new_dir);
489 CDEBUG(D_INODE, "old: %s, (%d length, %ld strlen), new: %s"
490 "(%d length, %ld strlen).old:d_count: %d, new:d_count: %d\n",
491 old_name, old_length, (long)strlen(old_name), new_name, new_length,
492 (long)strlen(new_name),old_dentry->d_count, new_dentry->d_count);
494 /* the C library will do unlink/create etc */
495 if ( coda_crossvol_rename == 0 &&
496 old_cnp->c_fid.Volume != new_cnp->c_fid.Volume )
497 return -EXDEV;
499 error = venus_rename(old_dir->i_sb, &(old_cnp->c_fid),
500 &(new_cnp->c_fid), old_length, new_length,
501 (const char *) old_name, (const char *)new_name);
503 if ( error ) {
504 CDEBUG(D_INODE, "returned error %d\n", error);
505 return error;
508 coda_flag_inode(new_inode, C_VATTR);
509 coda_flag_inode(old_dir, C_VATTR);
510 coda_flag_inode(new_dir, C_VATTR);
512 CDEBUG(D_INODE, "result %d\n", error);
514 EXIT;
515 return 0;
519 /* file operations for directories */
520 int coda_readdir(struct file *file, void *dirent, filldir_t filldir)
522 int result = 0;
523 struct coda_inode_info *cnp;
524 struct file open_file;
525 struct dentry open_dentry;
526 struct inode *inode=file->f_dentry->d_inode;
528 ENTRY;
529 coda_vfs_stat.readdir++;
531 cnp = ITOC(inode);
532 if ( !cnp->c_ovp ) {
533 CDEBUG(D_FILE, "open inode pointer = NULL.\n");
534 return -EIO;
537 coda_prepare_openfile(inode, file, cnp->c_ovp, &open_file,
538 &open_dentry);
539 if ( S_ISREG(cnp->c_ovp->i_mode) ) {
540 /* Venus: we must read Venus dirents from the file */
541 result = coda_venus_readdir(&open_file, dirent, filldir);
542 } else {
543 /* potemkin case: we are handed a directory inode */
544 down(&cnp->c_ovp->i_sem);
545 result = open_file.f_op->readdir(&open_file, dirent, filldir);
546 up(&cnp->c_ovp->i_sem);
548 coda_restore_codafile(inode, file, cnp->c_ovp, &open_file);
549 EXIT;
550 return result;
553 /* ask venus to cache the file and return the inode of the container file,
554 put this inode pointer in the cnode for future read/writes */
555 int coda_open(struct inode *i, struct file *f)
557 ino_t ino;
558 dev_t dev;
559 struct coda_inode_info *cnp;
560 int error = 0;
561 struct inode *cont_inode = NULL;
562 unsigned short flags = f->f_flags & (~O_EXCL);
563 unsigned short coda_flags = coda_flags_to_cflags(flags);
564 struct coda_cred *cred;
566 ENTRY;
567 coda_vfs_stat.open++;
569 CDEBUG(D_SPECIAL, "OPEN inode number: %ld, count %d, flags %o.\n",
570 f->f_dentry->d_inode->i_ino, f->f_dentry->d_count, flags);
572 cnp = ITOC(i);
575 error = venus_open(i->i_sb, &(cnp->c_fid), coda_flags, &ino, &dev);
576 if (error) {
577 CDEBUG(D_FILE, "venus: dev %d, inode %ld, out->result %d\n",
578 dev, (long)ino, error);
579 return error;
582 /* coda_upcall returns ino number of cached object, get inode */
583 CDEBUG(D_FILE, "cache file dev %d, ino %ld\n", dev, (long)ino);
584 error = coda_inode_grab(dev, ino, &cont_inode);
586 if ( error || !cont_inode ){
587 printk("coda_open: coda_inode_grab error %d.", error);
588 if (cont_inode)
589 iput(cont_inode);
590 return error;
593 CODA_ALLOC(cred, struct coda_cred *, sizeof(*cred));
594 coda_load_creds(cred);
595 f->private_data = cred;
597 if ( cnp->c_ovp )
598 iput(cnp->c_ovp);
600 cnp->c_ovp = cont_inode;
601 i->i_mapping = cont_inode->i_mapping;
602 cnp->c_ocount++;
604 CDEBUG(D_FILE, "result %d, coda i->i_count is %d for ino %ld\n",
605 error, i->i_count, i->i_ino);
606 CDEBUG(D_FILE, "cache ino: %ld, count %d, ops %p\n",
607 cnp->c_ovp->i_ino, cnp->c_ovp->i_count,
608 (cnp->c_ovp->i_op));
609 EXIT;
610 return 0;
613 int coda_release(struct inode *i, struct file *f)
615 struct coda_inode_info *cnp;
616 int error = 0;
617 unsigned short flags = (f->f_flags) & (~O_EXCL);
618 unsigned short cflags = coda_flags_to_cflags(flags);
619 struct coda_cred *cred;
621 ENTRY;
622 coda_vfs_stat.release++;
624 cred = (struct coda_cred *)f->private_data;
626 cnp =ITOC(i);
627 CHECK_CNODE(cnp);
628 CDEBUG(D_FILE,
629 "RELEASE coda (ino %ld, ct %d) cache (ino %ld, ct %d)\n",
630 i->i_ino, i->i_count, (cnp->c_ovp ? cnp->c_ovp->i_ino : 0),
631 (cnp->c_ovp ? cnp->c_ovp->i_count : -99));
634 /* even when c_ocount=0 we cannot put c_ovp to
635 * NULL since the file may be mmapped.
636 * See code in inode.c (coda_put_inode) for
637 * further handling of close.
640 --cnp->c_ocount;
642 if ( flags & (O_WRONLY | O_RDWR) )
643 --cnp->c_owrite;
645 /* Venus closing a container file? don't bother making the upcall. */
646 if ( current->pid != coda_upc_comm.vc_pid ) {
647 error = venus_release(i->i_sb, &(cnp->c_fid), cflags, cred);
650 f->private_data = NULL;
651 if (cred)
652 CODA_FREE(cred, sizeof(*cred));
654 CDEBUG(D_FILE, "coda_release: result: %d\n", error);
655 return error;
658 /* support routines */
660 * this structure is manipulated by filldir in vfs layer.
661 * the count holds the remaining amount of space in the getdents buffer,
662 * beyond the current_dir pointer.
666 /* should be big enough to hold any single directory entry */
667 #define DIR_BUFSIZE 2048
669 static int coda_venus_readdir(struct file *filp, void *getdent,
670 filldir_t filldir)
672 int bufsize;
673 int offset = filp->f_pos; /* offset in the directory file */
674 int count = 0;
675 int pos = 0; /* offset in the block we read */
676 int result = 0; /* either an error or # of entries returned */
677 int errfill;
678 char *buff = NULL;
679 struct venus_dirent *vdirent;
680 int string_offset = (int) (&((struct venus_dirent *)(0))->d_name);
681 int i;
683 ENTRY;
685 CODA_ALLOC(buff, char *, DIR_BUFSIZE);
686 if ( !buff ) {
687 printk("coda_venus_readdir: out of memory.\n");
688 return -ENOMEM;
691 /* we use this routine to read the file into our buffer */
692 bufsize = read_exec(filp->f_dentry, filp->f_pos, buff, DIR_BUFSIZE, 1);
693 if ( bufsize < 0) {
694 printk("coda_venus_readdir: cannot read directory %d.\n",
695 bufsize);
696 result = bufsize;
697 goto exit;
699 if ( bufsize == 0) {
700 result = 0;
701 goto exit;
704 /* Parse and write into user space. Filldir tells us when done! */
705 CDEBUG(D_FILE, "buffsize: %d offset %d, count %d.\n",
706 bufsize, offset, count);
708 i = 0;
709 result = 0;
710 while ( pos + string_offset < bufsize && i < 1024) {
711 vdirent = (struct venus_dirent *) (buff + pos);
713 /* test if the name is fully in the buffer */
714 if ( pos + string_offset + (int) vdirent->d_namlen >= bufsize ){
715 if ( result == 0 )
716 printk("CODA: Invalid directory cfino: %ld\n",
717 filp->f_dentry->d_inode->i_ino);
718 break;
720 /* now we are certain that we can read the entry from buff */
722 /* if we don't have a null entry, copy it */
723 if ( vdirent->d_fileno && vdirent->d_reclen ) {
724 int namlen = vdirent->d_namlen;
725 off_t offs = filp->f_pos;
726 ino_t ino = vdirent->d_fileno;
727 char *name = vdirent->d_name;
729 errfill = filldir(getdent, name, namlen,
730 offs, ino);
731 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);
732 /* errfill means no space for filling in this round */
733 if ( errfill < 0 ) {
734 result = 0;
735 break;
737 /* adjust count */
738 result++;
740 /* next one */
741 filp->f_pos += vdirent->d_reclen;
742 if ( filp->f_pos > filp->f_dentry->d_inode->i_size )
743 break;
744 if ( !vdirent->d_reclen ) {
745 printk("CODA: Invalid directory, cfino: %ld\n",
746 filp->f_dentry->d_inode->i_ino);
747 result = -EINVAL;
748 break;
750 pos += (unsigned int) vdirent->d_reclen;
751 i++;
754 if ( i >= 1024 ) {
755 printk("Repeating too much in readdir %ld\n",
756 filp->f_dentry->d_inode->i_ino);
757 result = -EINVAL;
760 exit:
761 CODA_FREE(buff, DIR_BUFSIZE);
762 return result;
765 /* called when a cache lookup succeeds */
766 static int coda_dentry_revalidate(struct dentry *de, int flags)
768 int valid = 1;
769 struct inode *inode = de->d_inode;
770 struct coda_inode_info *cii;
771 ENTRY;
773 if (!inode)
774 return 1;
776 cii = ITOC(de->d_inode);
777 if (coda_isroot(inode))
778 return 1;
779 if (is_bad_inode(inode))
780 return 0;
782 if (! (cii->c_flags & (C_PURGE | C_FLUSH)) )
783 return valid;
785 shrink_dcache_parent(de);
787 /* propagate for a flush */
788 if (cii->c_flags & C_FLUSH)
789 coda_flag_inode_children(inode, C_FLUSH);
791 if (de->d_count > 1) {
792 /* pretend it's valid, but don't change the flags */
793 CDEBUG(D_DOWNCALL, "BOOM for: ino %ld, %s\n",
794 de->d_inode->i_ino, coda_f2s(&cii->c_fid));
795 return 1;
798 /* clear the flags. */
799 cii->c_flags &= ~(C_VATTR | C_PURGE | C_FLUSH);
801 return 0;
805 * This is the callback from dput() when d_count is going to 0.
806 * We use this to unhash dentries with bad inodes.
808 static void coda_dentry_delete(struct dentry * dentry)
810 int flags;
812 if (!dentry->d_inode)
813 return ;
815 flags = (ITOC(dentry->d_inode)->c_flags) & C_PURGE;
816 if (is_bad_inode(dentry->d_inode) || flags) {
817 CDEBUG(D_DOWNCALL, "bad inode, unhashing %s/%s, %ld\n",
818 dentry->d_parent->d_name.name, dentry->d_name.name,
819 dentry->d_inode->i_ino);
820 d_drop(dentry);
827 * This is called when we want to check if the inode has
828 * changed on the server. Coda makes this easy since the
829 * cache manager Venus issues a downcall to the kernel when this
830 * happens
833 int coda_revalidate_inode(struct dentry *dentry)
835 struct coda_vattr attr;
836 int error = 0;
837 int old_mode;
838 ino_t old_ino;
839 struct inode *inode = dentry->d_inode;
840 struct coda_inode_info *cii = ITOC(inode);
842 ENTRY;
843 CDEBUG(D_INODE, "revalidating: %*s/%*s\n",
844 dentry->d_name.len, dentry->d_name.name,
845 dentry->d_parent->d_name.len, dentry->d_parent->d_name.name);
847 if ( cii->c_flags == 0 )
848 return 0;
850 /* Venus accessing a container file, don't try to revalidate */
851 if ( current->pid == coda_upc_comm.vc_pid )
852 return 0;
854 /* Venus closed the device .... */
855 if ( cii->c_flags & C_DYING )
856 goto return_bad_inode;
858 if (cii->c_flags & (C_VATTR | C_PURGE | C_FLUSH)) {
859 error = venus_getattr(inode->i_sb, &(cii->c_fid), &attr);
860 if ( error )
861 goto return_bad_inode;
863 /* this inode may be lost if:
864 - it's ino changed
865 - type changes must be permitted for repair and
866 missing mount points.
868 old_mode = inode->i_mode;
869 old_ino = inode->i_ino;
870 coda_vattr_to_iattr(inode, &attr);
873 if ((old_mode & S_IFMT) != (inode->i_mode & S_IFMT)) {
874 printk("Coda: inode %ld, fid %s changed type!\n",
875 inode->i_ino, coda_f2s(&(cii->c_fid)));
878 /* the following can happen when a local fid is replaced
879 with a global one, here we lose and declare the inode bad */
880 if (inode->i_ino != old_ino)
881 goto return_bad_inode;
883 if ( cii->c_flags )
884 coda_flag_inode_children(inode, C_FLUSH);
886 cii->c_flags &= ~(C_VATTR | C_PURGE | C_FLUSH);
889 return 0;
891 return_bad_inode:
892 if ( cii->c_ovp ) {
893 iput(cii->c_ovp);
894 inode->i_mapping = &inode->i_data;
895 cii->c_ovp = NULL;
897 make_bad_inode(inode);
898 return -EIO;