Import 2.3.10pre5
[davej-history.git] / fs / coda / dir.c
blob10b1cb171d6656152e3f77a1c232e0ae908bff5b
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, /* get_block */
81 NULL, /* readpage */
82 NULL, /* writepage */
83 NULL, /* flushpage */
84 NULL, /* truncate */
85 coda_permission, /* permission */
86 NULL, /* smap */
87 NULL, /* update page */
88 coda_revalidate_inode /* revalidate */
91 struct file_operations coda_dir_operations = {
92 NULL, /* lseek */
93 NULL, /* read -- bad */
94 NULL, /* write */
95 coda_readdir, /* readdir */
96 NULL, /* select */
97 NULL, /* ioctl */
98 NULL, /* mmap */
99 coda_open, /* open */
100 NULL,
101 coda_release, /* release */
102 coda_fsync, /* fsync */
103 NULL,
104 NULL,
105 NULL
109 /* inode operations for directories */
110 /* acces routines: lookup, readlink, permission */
111 static struct dentry *coda_lookup(struct inode *dir, struct dentry *entry)
113 struct coda_inode_info *dircnp;
114 struct inode *res_inode = NULL;
115 struct ViceFid resfid;
116 int dropme = 0; /* to indicate entry should not be cached */
117 int type;
118 int error = 0;
119 const char *name = entry->d_name.name;
120 size_t length = entry->d_name.len;
122 ENTRY;
124 dircnp = ITOC(dir);
126 if ( length > CODA_MAXNAMLEN ) {
127 printk("name too long: lookup, %s (%*s)\n",
128 coda_f2s(&dircnp->c_fid), length, name);
129 return ERR_PTR(-ENAMETOOLONG);
132 CDEBUG(D_INODE, "name %s, len %d in ino %ld, fid %s\n",
133 name, length, dir->i_ino, coda_f2s(&dircnp->c_fid));
135 /* control object, create inode on the fly */
136 if (coda_isroot(dir) && coda_iscontrol(name, length)) {
137 error = coda_cnode_makectl(&res_inode, dir->i_sb);
138 CDEBUG(D_SPECIAL,
139 "Lookup on CTL object; dir ino %ld, count %d\n",
140 dir->i_ino, dir->i_count);
141 goto exit;
144 error = venus_lookup(dir->i_sb, &(dircnp->c_fid),
145 (const char *)name, length, &type, &resfid);
147 res_inode = NULL;
148 if (!error) {
149 if (type & CODA_NOCACHE) {
150 type &= (~CODA_NOCACHE);
151 CDEBUG(D_INODE, "dropme set for %s\n",
152 coda_f2s(&resfid));
153 dropme = 1;
155 error = coda_cnode_make(&res_inode, &resfid, dir->i_sb);
156 if (error)
157 return ERR_PTR(error);
158 } else if (error != -ENOENT) {
159 CDEBUG(D_INODE, "error for %s(%*s)%d\n",
160 coda_f2s(&dircnp->c_fid), length, name, error);
161 return ERR_PTR(error);
163 CDEBUG(D_INODE, "lookup: %s is (%s), type %d result %d, dropme %d\n",
164 name, coda_f2s(&resfid), type, error, dropme);
166 exit:
167 entry->d_time = 0;
168 entry->d_op = &coda_dentry_operations;
169 d_add(entry, res_inode);
170 if ( dropme ) {
171 d_drop(entry);
172 ITOC(res_inode)->c_flags |= C_VATTR;
174 EXIT;
175 return NULL;
179 int coda_permission(struct inode *inode, int mask)
181 struct coda_inode_info *cp = ITOC(inode);
182 int error;
184 ENTRY;
185 coda_vfs_stat.permission++;
186 coda_permission_stat.count++;
188 if ( mask == 0 ) {
189 return 0;
192 if ( coda_access_cache == 1 ) {
193 if ( coda_cache_check(inode, mask) ) {
194 coda_permission_stat.hit_count++;
195 return 0;
199 cp = ITOC(inode);
201 CDEBUG(D_INODE, "mask is %o\n", mask);
202 error = venus_access(inode->i_sb, &(cp->c_fid), mask);
204 CDEBUG(D_INODE, "fid: %s, ino: %ld (mask: %o) error: %d\n",
205 coda_f2s(&(cp->c_fid)), inode->i_ino, mask, error);
207 if ( error == 0 ) {
208 coda_cache_enter(inode, mask);
211 return error;
216 /* creation routines: create, mknod, mkdir, link, symlink */
218 static int coda_create(struct inode *dir, struct dentry *de, int mode)
220 int error=0;
221 struct coda_inode_info *dircnp;
222 const char *name=de->d_name.name;
223 int length=de->d_name.len;
224 struct inode *result = NULL;
225 struct ViceFid newfid;
226 struct coda_vattr attrs;
228 ENTRY;
229 coda_vfs_stat.create++;
231 CDEBUG(D_INODE, "name: %s, length %d, mode %o\n",name, length, mode);
233 if (coda_isroot(dir) && coda_iscontrol(name, length))
234 return -EPERM;
236 dircnp = ITOC(dir);
238 error = venus_create(dir->i_sb, &(dircnp->c_fid), name, length,
239 0, mode, 0, &newfid, &attrs);
241 if ( error ) {
242 CDEBUG(D_INODE, "create: %s, result %d\n",
243 coda_f2s(&newfid), error);
244 d_drop(de);
245 return error;
248 error = coda_cnode_make(&result, &newfid, dir->i_sb);
249 if ( error ) {
250 d_drop(de);
251 result = NULL;
252 return error;
255 /* invalidate the directory cnode's attributes */
256 dircnp->c_flags |= C_VATTR;
257 d_instantiate(de, result);
258 return 0;
261 static int coda_mknod(struct inode *dir, struct dentry *de, int mode, int rdev)
263 int error=0;
264 struct coda_inode_info *dircnp;
265 const char *name=de->d_name.name;
266 int length=de->d_name.len;
267 struct inode *result = NULL;
268 struct ViceFid newfid;
269 struct coda_vattr attrs;
271 if ( coda_hasmknod == 0 )
272 return -EIO;
274 coda_vfs_stat.create++;
276 CDEBUG(D_INODE, "name: %s, length %d, mode %o, rdev %x\n",name, length, mode, rdev);
278 if (coda_isroot(dir) && coda_iscontrol(name, length))
279 return -EPERM;
281 dircnp = ITOC(dir);
283 error = venus_create(dir->i_sb, &(dircnp->c_fid), name, length,
284 0, mode, rdev, &newfid, &attrs);
286 if ( error ) {
287 CDEBUG(D_INODE, "mknod: %s, result %d\n",
288 coda_f2s(&newfid), error);
289 d_drop(de);
290 return error;
293 error = coda_cnode_make(&result, &newfid, dir->i_sb);
294 if ( error ) {
295 d_drop(de);
296 result = NULL;
297 return error;
300 /* invalidate the directory cnode's attributes */
301 dircnp->c_flags |= C_VATTR;
302 d_instantiate(de, result);
303 return 0;
306 static int coda_mkdir(struct inode *dir, struct dentry *de, int mode)
308 struct coda_inode_info *dircnp;
309 struct inode *inode;
310 struct coda_vattr attr;
311 const char *name = de->d_name.name;
312 int len = de->d_name.len;
313 int error;
314 struct ViceFid newfid;
316 ENTRY;
317 coda_vfs_stat.mkdir++;
319 if (coda_isroot(dir) && coda_iscontrol(name, len))
320 return -EPERM;
322 dircnp = ITOC(dir);
324 CDEBUG(D_INODE, "mkdir %s (len %d) in %s, mode %o.\n",
325 name, len, coda_f2s(&(dircnp->c_fid)), mode);
327 attr.va_mode = mode;
328 error = venus_mkdir(dir->i_sb, &(dircnp->c_fid),
329 name, len, &newfid, &attr);
331 if ( error ) {
332 CDEBUG(D_INODE, "mkdir error: %s result %d\n",
333 coda_f2s(&newfid), error);
334 d_drop(de);
335 return error;
338 CDEBUG(D_INODE, "mkdir: new dir has fid %s.\n",
339 coda_f2s(&newfid));
341 error = coda_cnode_make(&inode, &newfid, dir->i_sb);
342 if ( error ) {
343 d_drop(de);
344 return error;
347 /* invalidate the directory cnode's attributes */
348 dircnp->c_flags |= C_VATTR;
349 dir->i_nlink++;
350 d_instantiate(de, inode);
351 return 0;
354 /* try to make de an entry in dir_inodde linked to source_de */
355 static int coda_link(struct dentry *source_de, struct inode *dir_inode,
356 struct dentry *de)
358 struct inode *inode = source_de->d_inode;
359 const char * name = de->d_name.name;
360 int len = de->d_name.len;
361 struct coda_inode_info *dir_cnp, *cnp;
362 int error;
364 ENTRY;
365 coda_vfs_stat.link++;
367 if (coda_isroot(dir_inode) && coda_iscontrol(name, len))
368 return -EPERM;
370 dir_cnp = ITOC(dir_inode);
371 cnp = ITOC(inode);
373 CDEBUG(D_INODE, "old: fid: %s\n", coda_f2s(&(cnp->c_fid)));
374 CDEBUG(D_INODE, "directory: %s\n", coda_f2s(&(dir_cnp->c_fid)));
376 error = venus_link(dir_inode->i_sb,&(cnp->c_fid), &(dir_cnp->c_fid),
377 (const char *)name, len);
379 if ( ! error ) {
380 dir_cnp->c_flags |= C_VATTR;
381 ++inode->i_count;
382 d_instantiate(de, inode);
383 inode->i_nlink++;
384 } else {
385 d_drop(de);
386 return error;
389 CDEBUG(D_INODE, "link result %d\n",error);
390 EXIT;
391 return(error);
395 static int coda_symlink(struct inode *dir_inode, struct dentry *de,
396 const char *symname)
398 const char *name = de->d_name.name;
399 int len = de->d_name.len;
400 struct coda_inode_info *dir_cnp = ITOC(dir_inode);
401 int symlen;
402 int error=0;
404 ENTRY;
405 coda_vfs_stat.symlink++;
407 if (coda_isroot(dir_inode) && coda_iscontrol(name, len))
408 return -EPERM;
410 symlen = strlen(symname);
411 if ( symlen > CODA_MAXPATHLEN )
412 return -ENAMETOOLONG;
414 CDEBUG(D_INODE, "symname: %s, length: %d\n", symname, symlen);
417 * This entry is now negative. Since we do not create
418 * an inode for the entry we have to drop it.
420 d_drop(de);
421 error = venus_symlink(dir_inode->i_sb, &(dir_cnp->c_fid), name, len,
422 symname, symlen);
424 /* mtime is no good anymore */
425 if ( !error ) {
426 dir_cnp->c_flags |= C_VATTR;
429 CDEBUG(D_INODE, "in symlink result %d\n",error);
430 EXIT;
431 return error;
434 /* destruction routines: unlink, rmdir */
435 int coda_unlink(struct inode *dir, struct dentry *de)
437 struct coda_inode_info *dircnp = ITOC(dir);
438 int error;
439 const char *name = de->d_name.name;
440 int len = de->d_name.len;
442 ENTRY;
443 coda_vfs_stat.unlink++;
445 CDEBUG(D_INODE, " %s in %s, dirino %ld\n", name ,
446 coda_f2s(&(dircnp->c_fid)), dir->i_ino);
448 error = venus_remove(dir->i_sb, &(dircnp->c_fid), name, len);
449 if ( error ) {
450 CDEBUG(D_INODE, "upc returned error %d\n", error);
451 return error;
454 /* cache management: mtime has changed, ask Venus */
455 dircnp->c_flags |= C_VATTR;
456 de->d_inode->i_nlink--;
457 d_delete(de);
459 return 0;
462 int coda_rmdir(struct inode *dir, struct dentry *de)
464 struct coda_inode_info *dircnp;
465 const char *name = de->d_name.name;
466 int len = de->d_name.len;
467 int error;
469 ENTRY;
470 coda_vfs_stat.rmdir++;
472 dircnp = ITOC(dir);
474 if (!list_empty(&de->d_hash))
475 return -EBUSY;
476 error = venus_rmdir(dir->i_sb, &(dircnp->c_fid), name, len);
478 if ( error ) {
479 CDEBUG(D_INODE, "upc returned error %d\n", error);
480 return error;
483 if (de->d_inode->i_nlink)
484 de->d_inode->i_nlink --;
486 return 0;
489 /* rename */
490 static int coda_rename(struct inode *old_dir, struct dentry *old_dentry,
491 struct inode *new_dir, struct dentry *new_dentry)
493 const char *old_name = old_dentry->d_name.name;
494 const char *new_name = new_dentry->d_name.name;
495 int old_length = old_dentry->d_name.len;
496 int new_length = new_dentry->d_name.len;
497 struct inode *new_inode = new_dentry->d_inode;
498 struct coda_inode_info *new_cnp, *old_cnp;
499 int error;
501 ENTRY;
502 coda_vfs_stat.rename++;
504 old_cnp = ITOC(old_dir);
505 new_cnp = ITOC(new_dir);
507 CDEBUG(D_INODE, "old: %s, (%d length, %d strlen), new: %s"
508 "(%d length, %d strlen).old:d_count: %d, new:d_count: %d\n",
509 old_name, old_length, strlen(old_name), new_name, new_length,
510 strlen(new_name),old_dentry->d_count, new_dentry->d_count);
512 /* the C library will do unlink/create etc */
513 if ( coda_crossvol_rename == 0 &&
514 old_cnp->c_fid.Volume != new_cnp->c_fid.Volume )
515 return -EXDEV;
517 error = venus_rename(old_dir->i_sb, &(old_cnp->c_fid),
518 &(new_cnp->c_fid), old_length, new_length,
519 (const char *) old_name, (const char *)new_name);
521 if ( error ) {
522 CDEBUG(D_INODE, "returned error %d\n", error);
523 return error;
526 coda_flag_inode(new_inode, C_VATTR);
527 coda_flag_inode(old_dir, C_VATTR);
528 coda_flag_inode(new_dir, C_VATTR);
530 CDEBUG(D_INODE, "result %d\n", error);
532 EXIT;
533 return 0;
538 /* file operations for directories */
539 int coda_readdir(struct file *file, void *dirent, filldir_t filldir)
541 int result = 0;
542 struct coda_inode_info *cnp;
543 struct file open_file;
544 struct dentry open_dentry;
545 struct inode *inode=file->f_dentry->d_inode;
547 ENTRY;
548 coda_vfs_stat.readdir++;
550 cnp = ITOC(inode);
551 if ( !cnp->c_ovp ) {
552 CDEBUG(D_FILE, "open inode pointer = NULL.\n");
553 return -EIO;
556 coda_prepare_openfile(inode, file, cnp->c_ovp, &open_file,
557 &open_dentry);
558 if ( S_ISREG(cnp->c_ovp->i_mode) ) {
559 /* Venus: we must read Venus dirents from the file */
560 result = coda_venus_readdir(&open_file, dirent, filldir);
561 } else {
562 /* potemkin case: we are handed a directory inode */
563 down(&cnp->c_ovp->i_sem);
564 result = open_file.f_op->readdir(&open_file, dirent, filldir);
565 up(&cnp->c_ovp->i_sem);
567 coda_restore_codafile(inode, file, cnp->c_ovp, &open_file);
568 EXIT;
569 return result;
572 /* ask venus to cache the file and return the inode of the container file,
573 put this inode pointer in the cnode for future read/writes */
574 int coda_open(struct inode *i, struct file *f)
576 ino_t ino;
577 dev_t dev;
578 struct coda_inode_info *cnp;
579 int error = 0;
580 struct inode *cont_inode = NULL;
581 unsigned short flags = f->f_flags & (~O_EXCL);
582 unsigned short coda_flags = coda_flags_to_cflags(flags);
583 struct coda_cred *cred;
585 ENTRY;
586 coda_vfs_stat.open++;
588 CDEBUG(D_SPECIAL, "OPEN inode number: %ld, count %d, flags %o.\n",
589 f->f_dentry->d_inode->i_ino, f->f_dentry->d_count, flags);
591 cnp = ITOC(i);
594 error = venus_open(i->i_sb, &(cnp->c_fid), coda_flags, &ino, &dev);
595 if (error) {
596 CDEBUG(D_FILE, "venus: dev %d, inode %ld, out->result %d\n",
597 dev, ino, error);
598 return error;
601 /* coda_upcall returns ino number of cached object, get inode */
602 CDEBUG(D_FILE, "cache file dev %d, ino %ld\n", dev, ino);
603 error = coda_inode_grab(dev, ino, &cont_inode);
605 if ( error || !cont_inode ){
606 printk("coda_open: coda_inode_grab error %d.", error);
607 if (cont_inode)
608 iput(cont_inode);
609 return error;
612 CODA_ALLOC(cred, struct coda_cred *, sizeof(*cred));
613 coda_load_creds(cred);
614 f->private_data = cred;
616 if ( cnp->c_ovp ) {
617 iput(cnp->c_ovp);
618 cnp->c_ovp = NULL;
620 cnp->c_ovp = cont_inode;
621 cnp->c_ocount++;
623 CDEBUG(D_FILE, "result %d, coda i->i_count is %d for ino %ld\n",
624 error, i->i_count, i->i_ino);
625 CDEBUG(D_FILE, "cache ino: %ld, count %d, ops %x\n",
626 cnp->c_ovp->i_ino, cnp->c_ovp->i_count,
627 (int)(cnp->c_ovp->i_op));
628 EXIT;
629 return 0;
632 int coda_release(struct inode *i, struct file *f)
634 struct coda_inode_info *cnp;
635 int error;
636 unsigned short flags = (f->f_flags) & (~O_EXCL);
637 unsigned short cflags = coda_flags_to_cflags(flags);
638 struct coda_cred *cred;
640 ENTRY;
641 coda_vfs_stat.release++;
643 cred = (struct coda_cred *)f->private_data;
645 cnp =ITOC(i);
646 CHECK_CNODE(cnp);
647 CDEBUG(D_FILE,
648 "RELEASE coda (ino %ld, ct %d) cache (ino %ld, ct %d)\n",
649 i->i_ino, i->i_count, (cnp->c_ovp ? cnp->c_ovp->i_ino : 0),
650 (cnp->c_ovp ? cnp->c_ovp->i_count : -99));
653 /* even when c_ocount=0 we cannot put c_ovp to
654 * NULL since the file may be mmapped.
655 * See code in inode.c (coda_put_inode) for
656 * further handling of close.
659 --cnp->c_ocount;
661 if (flags & (O_WRONLY | O_RDWR)) {
662 --cnp->c_owrite;
665 error = venus_release(i->i_sb, &(cnp->c_fid), cflags, cred);
667 CODA_FREE(cred, sizeof(*cred));
668 f->private_data = NULL;
670 CDEBUG(D_FILE, "coda_release: result: %d\n", error);
671 return error;
674 /* support routines */
676 * this structure is manipulated by filldir in vfs layer.
677 * the count holds the remaining amount of space in the getdents buffer,
678 * beyond the current_dir pointer.
682 /* should be big enough to hold any single directory entry */
683 #define DIR_BUFSIZE 2048
685 static int coda_venus_readdir(struct file *filp, void *getdent,
686 filldir_t filldir)
688 int bufsize;
689 int offset = filp->f_pos; /* offset in the directory file */
690 int count = 0;
691 int pos = 0; /* offset in the block we read */
692 int result = 0; /* either an error or # of entries returned */
693 int errfill;
694 char *buff = NULL;
695 struct venus_dirent *vdirent;
696 int string_offset = (int) (&((struct venus_dirent *)(0))->d_name);
697 int i;
699 ENTRY;
701 CODA_ALLOC(buff, char *, DIR_BUFSIZE);
702 if ( !buff ) {
703 printk("coda_venus_readdir: out of memory.\n");
704 return -ENOMEM;
707 /* we use this routine to read the file into our buffer */
708 bufsize = read_exec(filp->f_dentry, filp->f_pos, buff, DIR_BUFSIZE, 1);
709 if ( bufsize < 0) {
710 printk("coda_venus_readdir: cannot read directory %d.\n",
711 bufsize);
712 result = bufsize;
713 goto exit;
715 if ( bufsize == 0) {
716 result = 0;
717 goto exit;
720 /* Parse and write into user space. Filldir tells us when done! */
721 CDEBUG(D_FILE, "buffsize: %d offset %d, count %d.\n",
722 bufsize, offset, count);
724 i = 0;
725 result = 0;
726 while ( pos + string_offset < bufsize && i < 1024) {
727 vdirent = (struct venus_dirent *) (buff + pos);
729 /* test if the name is fully in the buffer */
730 if ( pos + string_offset + (int) vdirent->d_namlen >= bufsize ){
731 if ( result == 0 )
732 printk("CODA: Invalid directory cfino: %ld\n",
733 filp->f_dentry->d_inode->i_ino);
734 break;
736 /* now we are certain that we can read the entry from buff */
738 /* if we don't have a null entry, copy it */
739 if ( vdirent->d_fileno && vdirent->d_reclen ) {
740 int namlen = vdirent->d_namlen;
741 off_t offs = filp->f_pos;
742 ino_t ino = vdirent->d_fileno;
743 char *name = vdirent->d_name;
745 errfill = filldir(getdent, name, namlen,
746 offs, ino);
747 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);
748 /* errfill means no space for filling in this round */
749 if ( errfill < 0 ) {
750 result = 0;
751 break;
753 /* adjust count */
754 result++;
756 /* next one */
757 filp->f_pos += vdirent->d_reclen;
758 if ( filp->f_pos > filp->f_dentry->d_inode->i_size )
759 break;
760 if ( !vdirent->d_reclen ) {
761 printk("CODA: Invalid directory, cfino: %ld\n",
762 filp->f_dentry->d_inode->i_ino);
763 result = -EINVAL;
764 break;
766 pos += (unsigned int) vdirent->d_reclen;
767 i++;
770 if ( i >= 1024 ) {
771 printk("Repeating too much in readdir %ld\n",
772 filp->f_dentry->d_inode->i_ino);
773 result = -EINVAL;
776 exit:
777 CODA_FREE(buff, DIR_BUFSIZE);
778 return result;
781 /* called when a cache lookup succeeds */
782 static int coda_dentry_revalidate(struct dentry *de, int flags)
784 int valid = 1;
785 struct inode *inode = de->d_inode;
786 struct coda_inode_info *cii;
787 ENTRY;
789 if (!inode)
790 return 1;
792 cii = ITOC(de->d_inode);
793 if (coda_isroot(inode))
794 return 1;
795 if (is_bad_inode(inode))
796 return 0;
798 if (! (cii->c_flags & (C_PURGE | C_FLUSH)) )
799 return valid;
801 shrink_dcache_parent(de);
803 /* propagate for a flush */
804 if (cii->c_flags & C_FLUSH)
805 coda_flag_inode_children(inode, C_FLUSH);
807 if (de->d_count > 1) {
808 /* pretend it's valid, but don't change the flags */
809 CDEBUG(D_DOWNCALL, "BOOM for: ino %ld, %s\n",
810 de->d_inode->i_ino, coda_f2s(&cii->c_fid));
811 return 1;
814 /* clear the flags. */
815 cii->c_flags &= ~(C_VATTR | C_PURGE | C_FLUSH);
817 return 0;
821 * This is the callback from dput() when d_count is going to 0.
822 * We use this to unhash dentries with bad inodes.
824 static void coda_dentry_delete(struct dentry * dentry)
826 int flags;
828 if (!dentry->d_inode)
829 return ;
831 flags = (ITOC(dentry->d_inode)->c_flags) & C_PURGE;
832 if (is_bad_inode(dentry->d_inode) || flags) {
833 CDEBUG(D_DOWNCALL, "bad inode, unhashing %s/%s, %ld\n",
834 dentry->d_parent->d_name.name, dentry->d_name.name,
835 dentry->d_inode->i_ino);
836 d_drop(dentry);
843 * This is called when we want to check if the inode has
844 * changed on the server. Coda makes this easy since the
845 * cache manager Venus issues a downcall to the kernel when this
846 * happens
849 int coda_revalidate_inode(struct dentry *dentry)
851 struct coda_vattr attr;
852 int error = 0;
853 int old_mode;
854 ino_t old_ino;
855 struct inode *inode = dentry->d_inode;
856 struct coda_inode_info *cii = ITOC(inode);
858 ENTRY;
859 CDEBUG(D_INODE, "revalidating: %*s/%*s\n",
860 dentry->d_name.len, dentry->d_name.name,
861 dentry->d_parent->d_name.len, dentry->d_parent->d_name.name);
863 if ( cii->c_flags == 0 )
864 return 0;
866 /* Venus closed the device .... */
867 if ( cii->c_flags & C_DYING ) {
868 make_bad_inode(inode);
869 return -EIO;
873 if (cii->c_flags & (C_VATTR | C_PURGE | C_FLUSH)) {
874 error = venus_getattr(inode->i_sb, &(cii->c_fid), &attr);
875 if ( error ) {
876 make_bad_inode(inode);
877 return -EIO;
880 /* this inode may be lost if:
881 - it's ino changed
882 - type changes must be permitted for repair and
883 missing mount points.
885 old_mode = inode->i_mode;
886 old_ino = inode->i_ino;
887 coda_vattr_to_iattr(inode, &attr);
890 if ((old_mode & S_IFMT) != (inode->i_mode & S_IFMT)) {
891 printk("Coda: inode %ld, fid %s changed type!\n",
892 inode->i_ino, coda_f2s(&(cii->c_fid)));
895 /* the following can happen when a local fid is replaced
896 with a global one, here we lose and declar the inode bad */
897 if (inode->i_ino != old_ino) {
898 make_bad_inode(inode);
899 inode->i_mode = old_mode;
900 return -EIO;
903 if ( cii->c_flags )
904 coda_flag_inode_children(inode, C_FLUSH);
906 cii->c_flags &= ~(C_VATTR | C_PURGE | C_FLUSH);
909 return 0;