Linux 2.2.0
[davej-history.git] / fs / coda / dir.c
blob7ba943cd61bcd0add3f6b76ac80a1cbbf2859c94
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 int 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);
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 int 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 -ENAMETOOLONG;
132 if (!dir || !S_ISDIR(dir->i_mode)) {
133 printk("coda_lookup: inode is NULL or not a directory\n");
134 return -ENOTDIR;
138 CDEBUG(D_INODE, "name %s, len %d in ino %ld, fid %s\n",
139 name, length, dir->i_ino, coda_f2s(&dircnp->c_fid));
141 /* control object, create inode on the fly */
142 if (coda_isroot(dir) && coda_iscontrol(name, length)) {
143 error = coda_cnode_makectl(&res_inode, dir->i_sb);
144 CDEBUG(D_SPECIAL,
145 "Lookup on CTL object; dir ino %ld, count %d\n",
146 dir->i_ino, dir->i_count);
147 goto exit;
150 error = venus_lookup(dir->i_sb, &(dircnp->c_fid),
151 (const char *)name, length, &type, &resfid);
153 res_inode = NULL;
154 if (!error) {
155 if (type & CODA_NOCACHE) {
156 type &= (~CODA_NOCACHE);
157 CDEBUG(D_INODE, "dropme set for %s\n",
158 coda_f2s(&resfid));
159 dropme = 1;
161 error = coda_cnode_make(&res_inode, &resfid, dir->i_sb);
162 if (error)
163 return error;
164 } else if (error != -ENOENT) {
165 CDEBUG(D_INODE, "error for %s(%*s)%d\n",
166 coda_f2s(&dircnp->c_fid), length, name, error);
167 return error;
169 CDEBUG(D_INODE, "lookup: %s is (%s), type %d result %d, dropme %d\n",
170 name, coda_f2s(&resfid), type, error, dropme);
172 exit:
173 entry->d_time = 0;
174 entry->d_op = &coda_dentry_operations;
175 d_add(entry, res_inode);
176 if ( dropme ) {
177 d_drop(entry);
178 ITOC(res_inode)->c_flags |= C_VATTR;
180 EXIT;
181 return 0;
185 int coda_permission(struct inode *inode, int mask)
187 struct coda_inode_info *cp = ITOC(inode);
188 int error;
190 ENTRY;
191 coda_vfs_stat.permission++;
192 coda_permission_stat.count++;
194 if ( mask == 0 ) {
195 return 0;
198 if ( coda_access_cache == 1 ) {
199 if ( coda_cache_check(inode, mask) ) {
200 coda_permission_stat.hit_count++;
201 return 0;
205 cp = ITOC(inode);
207 CDEBUG(D_INODE, "mask is %o\n", mask);
208 error = venus_access(inode->i_sb, &(cp->c_fid), mask);
210 CDEBUG(D_INODE, "fid: %s, ino: %ld (mask: %o) error: %d\n",
211 coda_f2s(&(cp->c_fid)), inode->i_ino, mask, error);
213 if ( error == 0 ) {
214 coda_cache_enter(inode, mask);
217 return error;
222 /* creation routines: create, mknod, mkdir, link, symlink */
224 static int coda_create(struct inode *dir, struct dentry *de, int mode)
226 int error=0;
227 struct coda_inode_info *dircnp;
228 const char *name=de->d_name.name;
229 int length=de->d_name.len;
230 struct inode *result = NULL;
231 struct ViceFid newfid;
232 struct coda_vattr attrs;
234 ENTRY;
235 coda_vfs_stat.create++;
237 CDEBUG(D_INODE, "name: %s, length %d, mode %o\n",name, length, mode);
239 if (!dir || !S_ISDIR(dir->i_mode)) {
240 printk("coda_create: inode is null or not a directory\n");
241 return -ENOENT;
244 if (coda_isroot(dir) && coda_iscontrol(name, length))
245 return -EPERM;
247 dircnp = ITOC(dir);
249 if ( length > CODA_MAXNAMLEN ) {
250 printk("name too long: create, %s(%s)\n",
251 coda_f2s(&dircnp->c_fid), name);
252 return -ENAMETOOLONG;
255 error = venus_create(dir->i_sb, &(dircnp->c_fid), name, length,
256 0, mode, 0, &newfid, &attrs);
258 if ( error ) {
259 CDEBUG(D_INODE, "create: %s, result %d\n",
260 coda_f2s(&newfid), error);
261 d_drop(de);
262 return error;
265 error = coda_cnode_make(&result, &newfid, dir->i_sb);
266 if ( error ) {
267 d_drop(de);
268 result = NULL;
269 return error;
272 /* invalidate the directory cnode's attributes */
273 dircnp->c_flags |= C_VATTR;
274 d_instantiate(de, result);
275 return 0;
278 static int coda_mknod(struct inode *dir, struct dentry *de, int mode, int rdev)
280 int error=0;
281 struct coda_inode_info *dircnp;
282 const char *name=de->d_name.name;
283 int length=de->d_name.len;
284 struct inode *result = NULL;
285 struct ViceFid newfid;
286 struct coda_vattr attrs;
288 if ( coda_hasmknod == 0 )
289 return -EIO;
291 coda_vfs_stat.create++;
293 CDEBUG(D_INODE, "name: %s, length %d, mode %o, rdev %x\n",name, length, mode, rdev);
295 if (!dir || !S_ISDIR(dir->i_mode)) {
296 printk("coda_mknod: inode is null or not a directory\n");
297 return -ENOENT;
300 if (coda_isroot(dir) && coda_iscontrol(name, length))
301 return -EPERM;
303 dircnp = ITOC(dir);
305 if ( length > CODA_MAXNAMLEN ) {
306 printk("name too long: mknod, %s(%s)\n",
307 coda_f2s(&dircnp->c_fid), name);
308 return -ENAMETOOLONG;
311 error = venus_create(dir->i_sb, &(dircnp->c_fid), name, length,
312 0, mode, rdev, &newfid, &attrs);
314 if ( error ) {
315 CDEBUG(D_INODE, "mknod: %s, result %d\n",
316 coda_f2s(&newfid), error);
317 d_drop(de);
318 return error;
321 error = coda_cnode_make(&result, &newfid, dir->i_sb);
322 if ( error ) {
323 d_drop(de);
324 result = NULL;
325 return error;
328 /* invalidate the directory cnode's attributes */
329 dircnp->c_flags |= C_VATTR;
330 d_instantiate(de, result);
331 return 0;
334 static int coda_mkdir(struct inode *dir, struct dentry *de, int mode)
336 struct coda_inode_info *dircnp;
337 struct inode *inode;
338 struct coda_vattr attr;
339 const char *name = de->d_name.name;
340 int len = de->d_name.len;
341 int error;
342 struct ViceFid newfid;
344 ENTRY;
345 coda_vfs_stat.mkdir++;
347 if (!dir || !S_ISDIR(dir->i_mode)) {
348 printk("coda_mkdir: inode is NULL or not a directory\n");
349 return -ENOENT;
352 if ( len > CODA_MAXNAMLEN )
353 return -ENAMETOOLONG;
355 if (coda_isroot(dir) && coda_iscontrol(name, len))
356 return -EPERM;
358 dircnp = ITOC(dir);
360 CDEBUG(D_INODE, "mkdir %s (len %d) in %s, mode %o.\n",
361 name, len, coda_f2s(&(dircnp->c_fid)), mode);
363 attr.va_mode = mode;
364 error = venus_mkdir(dir->i_sb, &(dircnp->c_fid),
365 name, len, &newfid, &attr);
367 if ( error ) {
368 CDEBUG(D_INODE, "mkdir error: %s result %d\n",
369 coda_f2s(&newfid), error);
370 d_drop(de);
371 return error;
374 CDEBUG(D_INODE, "mkdir: new dir has fid %s.\n",
375 coda_f2s(&newfid));
377 error = coda_cnode_make(&inode, &newfid, dir->i_sb);
378 if ( error ) {
379 d_drop(de);
380 return error;
383 /* invalidate the directory cnode's attributes */
384 dircnp->c_flags |= C_VATTR;
385 dir->i_nlink++;
386 d_instantiate(de, inode);
387 return 0;
390 /* try to make de an entry in dir_inodde linked to source_de */
391 static int coda_link(struct dentry *source_de, struct inode *dir_inode,
392 struct dentry *de)
394 struct inode *inode = source_de->d_inode;
395 const char * name = de->d_name.name;
396 int len = de->d_name.len;
397 struct coda_inode_info *dir_cnp, *cnp;
398 int error;
400 ENTRY;
401 coda_vfs_stat.link++;
403 if (coda_isroot(dir_inode) && coda_iscontrol(name, len))
404 return -EPERM;
406 dir_cnp = ITOC(dir_inode);
407 cnp = ITOC(inode);
409 CDEBUG(D_INODE, "old: fid: %s\n", coda_f2s(&(cnp->c_fid)));
410 CDEBUG(D_INODE, "directory: %s\n", coda_f2s(&(dir_cnp->c_fid)));
412 if ( len > CODA_MAXNAMLEN ) {
413 printk("coda_link: name too long. \n");
414 return -ENAMETOOLONG;
417 error = venus_link(dir_inode->i_sb,&(cnp->c_fid), &(dir_cnp->c_fid),
418 (const char *)name, len);
420 if ( ! error ) {
421 dir_cnp->c_flags |= C_VATTR;
422 ++inode->i_count;
423 d_instantiate(de, inode);
424 inode->i_nlink++;
425 } else {
426 d_drop(de);
427 return error;
430 CDEBUG(D_INODE, "link result %d\n",error);
431 EXIT;
432 return(error);
436 static int coda_symlink(struct inode *dir_inode, struct dentry *de,
437 const char *symname)
439 const char *name = de->d_name.name;
440 int len = de->d_name.len;
441 struct coda_inode_info *dir_cnp = ITOC(dir_inode);
442 int symlen;
443 int error=0;
445 ENTRY;
446 coda_vfs_stat.symlink++;
448 if (coda_isroot(dir_inode) && coda_iscontrol(name, len))
449 return -EPERM;
451 if ( len > CODA_MAXNAMLEN )
452 return -ENAMETOOLONG;
454 symlen = strlen(symname);
455 if ( symlen > CODA_MAXPATHLEN )
456 return -ENAMETOOLONG;
458 CDEBUG(D_INODE, "symname: %s, length: %d\n", symname, symlen);
461 * This entry is now negative. Since we do not create
462 * an inode for the entry we have to drop it.
464 d_drop(de);
465 error = venus_symlink(dir_inode->i_sb, &(dir_cnp->c_fid), name, len,
466 symname, symlen);
468 /* mtime is no good anymore */
469 if ( !error ) {
470 dir_cnp->c_flags |= C_VATTR;
473 CDEBUG(D_INODE, "in symlink result %d\n",error);
474 EXIT;
475 return error;
478 /* destruction routines: unlink, rmdir */
479 int coda_unlink(struct inode *dir, struct dentry *de)
481 struct coda_inode_info *dircnp = ITOC(dir);
482 int error;
483 const char *name = de->d_name.name;
484 int len = de->d_name.len;
486 ENTRY;
487 coda_vfs_stat.unlink++;
489 CDEBUG(D_INODE, " %s in %s, dirino %ld\n", name ,
490 coda_f2s(&(dircnp->c_fid)), dir->i_ino);
492 error = venus_remove(dir->i_sb, &(dircnp->c_fid), name, len);
493 if ( error ) {
494 CDEBUG(D_INODE, "upc returned error %d\n", error);
495 return error;
498 /* cache management: mtime has changed, ask Venus */
499 dircnp->c_flags |= C_VATTR;
500 de->d_inode->i_nlink--;
501 d_delete(de);
503 return 0;
506 int coda_rmdir(struct inode *dir, struct dentry *de)
508 struct coda_inode_info *dircnp;
509 const char *name = de->d_name.name;
510 int len = de->d_name.len;
511 int error;
513 ENTRY;
514 coda_vfs_stat.rmdir++;
516 if (!dir || !S_ISDIR(dir->i_mode)) {
517 printk("coda_rmdir: inode is NULL or not a directory\n");
518 return -ENOENT;
520 dircnp = ITOC(dir);
522 if (len > CODA_MAXNAMLEN)
523 return -ENAMETOOLONG;
525 if (!list_empty(&de->d_hash))
526 return -EBUSY;
527 error = venus_rmdir(dir->i_sb, &(dircnp->c_fid), name, len);
529 if ( error ) {
530 CDEBUG(D_INODE, "upc returned error %d\n", error);
531 return error;
534 if (de->d_inode->i_nlink)
535 de->d_inode->i_nlink --;
537 return 0;
540 /* rename */
541 static int coda_rename(struct inode *old_dir, struct dentry *old_dentry,
542 struct inode *new_dir, struct dentry *new_dentry)
544 const char *old_name = old_dentry->d_name.name;
545 const char *new_name = new_dentry->d_name.name;
546 int old_length = old_dentry->d_name.len;
547 int new_length = new_dentry->d_name.len;
548 struct inode *old_inode = old_dentry->d_inode;
549 struct inode *new_inode = new_dentry->d_inode;
550 struct coda_inode_info *new_cnp, *old_cnp;
551 int error;
553 ENTRY;
554 coda_vfs_stat.rename++;
556 if ( (old_length > CODA_MAXNAMLEN) || new_length > CODA_MAXNAMLEN ) {
557 return -ENAMETOOLONG;
560 old_cnp = ITOC(old_dir);
561 new_cnp = ITOC(new_dir);
563 CDEBUG(D_INODE, "old: %s, (%d length, %d strlen), new: %s"
564 "(%d length, %d strlen).old:d_count: %d, new:d_count: %d\n",
565 old_name, old_length, strlen(old_name), new_name, new_length,
566 strlen(new_name),old_dentry->d_count, new_dentry->d_count);
568 if (new_inode == old_inode)
569 return 0;
571 /* make sure target is not in use */
572 if (new_inode && S_ISDIR(new_inode->i_mode)) {
574 * Prune any children before testing for busy.
576 if (new_dentry->d_count > 1)
577 shrink_dcache_parent(new_dentry);
579 if (new_dentry->d_count > 1)
580 return -EBUSY;
583 /* the C library will do unlink/create etc */
584 if ( coda_crossvol_rename == 0 &&
585 old_cnp->c_fid.Volume != new_cnp->c_fid.Volume )
586 return -EXDEV;
588 error = venus_rename(old_dir->i_sb, &(old_cnp->c_fid),
589 &(new_cnp->c_fid), old_length, new_length,
590 (const char *) old_name, (const char *)new_name);
592 if ( error ) {
593 CDEBUG(D_INODE, "returned error %d\n", error);
594 return error;
597 coda_flag_inode(new_inode, C_VATTR);
598 coda_flag_inode(old_dir, C_VATTR);
599 coda_flag_inode(new_dir, C_VATTR);
601 CDEBUG(D_INODE, "result %d\n", error);
602 d_move(old_dentry, new_dentry);
604 EXIT;
605 return 0;
610 /* file operations for directories */
611 int coda_readdir(struct file *file, void *dirent, filldir_t filldir)
613 int result = 0;
614 struct coda_inode_info *cnp;
615 struct file open_file;
616 struct dentry open_dentry;
617 struct inode *inode=file->f_dentry->d_inode;
619 ENTRY;
620 coda_vfs_stat.readdir++;
622 if (!inode || !inode->i_sb || !S_ISDIR(inode->i_mode)) {
623 printk("coda_readdir: inode is NULL or not a directory\n");
624 return -EBADF;
627 cnp = ITOC(inode);
628 if ( !cnp->c_ovp ) {
629 CDEBUG(D_FILE, "open inode pointer = NULL.\n");
630 return -EIO;
633 coda_prepare_openfile(inode, file, cnp->c_ovp, &open_file,
634 &open_dentry);
635 if ( S_ISREG(cnp->c_ovp->i_mode) ) {
636 /* Venus: we must read Venus dirents from the file */
637 result = coda_venus_readdir(&open_file, dirent, filldir);
638 } else {
639 /* potemkin case: we are handed a directory inode */
640 result = open_file.f_op->readdir(&open_file, dirent, filldir);
642 coda_restore_codafile(inode, file, cnp->c_ovp, &open_file);
643 EXIT;
644 return result;
647 /* ask venus to cache the file and return the inode of the container file,
648 put this inode pointer in the cnode for future read/writes */
649 int coda_open(struct inode *i, struct file *f)
651 ino_t ino;
652 dev_t dev;
653 struct coda_inode_info *cnp;
654 int error = 0;
655 struct inode *cont_inode = NULL;
656 unsigned short flags = f->f_flags & (~O_EXCL);
657 unsigned short coda_flags = coda_flags_to_cflags(flags);
658 struct coda_cred *cred;
660 ENTRY;
661 coda_vfs_stat.open++;
663 CDEBUG(D_SPECIAL, "OPEN inode number: %ld, count %d, flags %o.\n",
664 f->f_dentry->d_inode->i_ino, f->f_dentry->d_count, flags);
666 cnp = ITOC(i);
669 error = venus_open(i->i_sb, &(cnp->c_fid), coda_flags, &ino, &dev);
670 if (error) {
671 CDEBUG(D_FILE, "venus: dev %d, inode %ld, out->result %d\n",
672 dev, ino, error);
673 return error;
676 /* coda_upcall returns ino number of cached object, get inode */
677 CDEBUG(D_FILE, "cache file dev %d, ino %ld\n", dev, ino);
678 error = coda_inode_grab(dev, ino, &cont_inode);
680 if ( error || !cont_inode ){
681 printk("coda_open: coda_inode_grab error %d.", error);
682 if (cont_inode)
683 iput(cont_inode);
684 return error;
687 CODA_ALLOC(cred, struct coda_cred *, sizeof(*cred));
688 coda_load_creds(cred);
689 f->private_data = cred;
691 if ( cnp->c_ovp ) {
692 iput(cnp->c_ovp);
693 cnp->c_ovp = NULL;
695 cnp->c_ovp = cont_inode;
696 cnp->c_ocount++;
698 CDEBUG(D_FILE, "result %d, coda i->i_count is %d for ino %ld\n",
699 error, i->i_count, i->i_ino);
700 CDEBUG(D_FILE, "cache ino: %ld, count %d, ops %x\n",
701 cnp->c_ovp->i_ino, cnp->c_ovp->i_count,
702 (int)(cnp->c_ovp->i_op));
703 EXIT;
704 return 0;
707 int coda_release(struct inode *i, struct file *f)
709 struct coda_inode_info *cnp;
710 int error;
711 unsigned short flags = (f->f_flags) & (~O_EXCL);
712 unsigned short cflags = coda_flags_to_cflags(flags);
713 struct coda_cred *cred;
715 ENTRY;
716 coda_vfs_stat.release++;
718 cred = (struct coda_cred *)f->private_data;
720 cnp =ITOC(i);
721 CHECK_CNODE(cnp);
722 CDEBUG(D_FILE,
723 "RELEASE coda (ino %ld, ct %d) cache (ino %ld, ct %d)\n",
724 i->i_ino, i->i_count, (cnp->c_ovp ? cnp->c_ovp->i_ino : 0),
725 (cnp->c_ovp ? cnp->c_ovp->i_count : -99));
728 /* even when c_ocount=0 we cannot put c_ovp to
729 * NULL since the file may be mmapped.
730 * See code in inode.c (coda_put_inode) for
731 * further handling of close.
734 --cnp->c_ocount;
736 if (flags & (O_WRONLY | O_RDWR)) {
737 --cnp->c_owrite;
740 error = venus_release(i->i_sb, &(cnp->c_fid), cflags, cred);
742 CODA_FREE(cred, sizeof(*cred));
743 f->private_data = NULL;
745 CDEBUG(D_FILE, "coda_release: result: %d\n", error);
746 return error;
749 /* support routines */
751 * this structure is manipulated by filldir in vfs layer.
752 * the count holds the remaining amount of space in the getdents buffer,
753 * beyond the current_dir pointer.
757 /* should be big enough to hold any single directory entry */
758 #define DIR_BUFSIZE 2048
760 static int coda_venus_readdir(struct file *filp, void *getdent,
761 filldir_t filldir)
763 int bufsize;
764 int offset = filp->f_pos; /* offset in the directory file */
765 int count = 0;
766 int pos = 0; /* offset in the block we read */
767 int result = 0; /* either an error or # of entries returned */
768 int errfill;
769 char *buff = NULL;
770 struct venus_dirent *vdirent;
771 int string_offset = (int) (&((struct venus_dirent *)(0))->d_name);
772 int i;
774 ENTRY;
776 CODA_ALLOC(buff, char *, DIR_BUFSIZE);
777 if ( !buff ) {
778 printk("coda_venus_readdir: out of memory.\n");
779 return -ENOMEM;
782 /* we use this routine to read the file into our buffer */
783 bufsize = read_exec(filp->f_dentry, filp->f_pos, buff, DIR_BUFSIZE, 1);
784 if ( bufsize < 0) {
785 printk("coda_venus_readdir: cannot read directory %d.\n",
786 bufsize);
787 result = bufsize;
788 goto exit;
790 if ( bufsize == 0) {
791 result = 0;
792 goto exit;
795 /* Parse and write into user space. Filldir tells us when done! */
796 CDEBUG(D_FILE, "buffsize: %d offset %d, count %d.\n",
797 bufsize, offset, count);
799 i = 0;
800 result = 0;
801 while ( pos + string_offset < bufsize && i < 1024) {
802 vdirent = (struct venus_dirent *) (buff + pos);
804 /* test if the name is fully in the buffer */
805 if ( pos + string_offset + (int) vdirent->d_namlen >= bufsize ){
806 if ( result == 0 )
807 printk("CODA: Invalid directory cfino: %ld\n",
808 filp->f_dentry->d_inode->i_ino);
809 break;
811 /* now we are certain that we can read the entry from buff */
813 /* if we don't have a null entry, copy it */
814 if ( vdirent->d_fileno && vdirent->d_reclen ) {
815 int namlen = vdirent->d_namlen;
816 off_t offs = filp->f_pos;
817 ino_t ino = vdirent->d_fileno;
818 char *name = vdirent->d_name;
820 errfill = filldir(getdent, name, namlen,
821 offs, ino);
822 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);
823 /* errfill means no space for filling in this round */
824 if ( errfill < 0 ) {
825 result = 0;
826 break;
828 /* adjust count */
829 result++;
831 /* next one */
832 filp->f_pos += vdirent->d_reclen;
833 if ( filp->f_pos > filp->f_dentry->d_inode->i_size )
834 break;
835 if ( !vdirent->d_reclen ) {
836 printk("CODA: Invalid directory, cfino: %ld\n",
837 filp->f_dentry->d_inode->i_ino);
838 result = -EINVAL;
839 break;
841 pos += (unsigned int) vdirent->d_reclen;
842 i++;
845 if ( i >= 1024 ) {
846 printk("Repeating too much in readdir %ld\n",
847 filp->f_dentry->d_inode->i_ino);
848 result = -EINVAL;
851 exit:
852 CODA_FREE(buff, DIR_BUFSIZE);
853 return result;
856 /* called when a cache lookup succeeds */
857 static int coda_dentry_revalidate(struct dentry *de)
859 int valid = 1;
860 struct inode *inode = de->d_inode;
861 struct coda_inode_info *cii;
862 ENTRY;
864 if (!inode)
865 return 1;
867 cii = ITOC(de->d_inode);
868 if (coda_isroot(inode))
869 return 1;
870 if (is_bad_inode(inode))
871 return 0;
873 if (! (cii->c_flags & (C_PURGE | C_FLUSH)) )
874 return valid;
876 shrink_dcache_parent(de);
878 if (de->d_count > 1) {
879 /* pretend it's valid, but don't change the flags */
880 CDEBUG(D_DOWNCALL, "BOOM for: ino %ld, %s\n",
881 de->d_inode->i_ino, coda_f2s(&cii->c_fid));
882 return 1;
885 /* propagate for a flush */
886 if (cii->c_flags & C_FLUSH)
887 coda_flag_inode_children(inode, C_FLUSH);
889 /* clear the flags. */
890 cii->c_flags &= ~(C_VATTR | C_PURGE | C_FLUSH);
892 return 0;
896 * This is the callback from dput() when d_count is going to 0.
897 * We use this to unhash dentries with bad inodes.
899 static void coda_dentry_delete(struct dentry * dentry)
901 int flags;
903 if (!dentry->d_inode)
904 return ;
906 flags = (ITOC(dentry->d_inode)->c_flags) & C_PURGE;
907 if (is_bad_inode(dentry->d_inode) || flags) {
908 CDEBUG(D_DOWNCALL, "bad inode, unhashing %s/%s, %ld\n",
909 dentry->d_parent->d_name.name, dentry->d_name.name,
910 dentry->d_inode->i_ino);
911 d_drop(dentry);
918 * This is called when we want to check if the inode has
919 * changed on the server. Coda makes this easy since the
920 * cache manager Venus issues a downcall to the kernel when this
921 * happens
924 int coda_revalidate_inode(struct dentry *dentry)
926 struct coda_vattr attr;
927 int error = 0;
928 int old_mode;
929 ino_t old_ino;
930 struct inode *inode = dentry->d_inode;
931 struct coda_inode_info *cii = ITOC(inode);
933 ENTRY;
934 CDEBUG(D_INODE, "revalidating: %*s/%*s\n",
935 dentry->d_name.len, dentry->d_name.name,
936 dentry->d_parent->d_name.len, dentry->d_parent->d_name.name);
938 if ( cii->c_flags == 0 )
939 return 0;
941 /* Venus closed the device .... */
942 if ( cii->c_flags & C_DYING ) {
943 make_bad_inode(inode);
944 return -EIO;
948 if (cii->c_flags & (C_VATTR | C_PURGE | C_FLUSH)) {
949 error = venus_getattr(inode->i_sb, &(cii->c_fid), &attr);
950 if ( error ) {
951 make_bad_inode(inode);
952 return -EIO;
955 /* this inode may be lost if:
956 - it's ino changed
957 - type changes must be permitted for repair and
958 missing mount points.
960 old_mode = inode->i_mode;
961 old_ino = inode->i_ino;
962 coda_vattr_to_iattr(inode, &attr);
965 if ((old_mode & S_IFMT) != (inode->i_mode & S_IFMT)) {
966 printk("Coda: inode %ld, fid %s changed type!\n",
967 inode->i_ino, coda_f2s(&(cii->c_fid)));
970 /* the following can happen when a local fid is replaced
971 with a global one, here we lose and declar the inode bad */
972 if (inode->i_ino != old_ino) {
973 make_bad_inode(inode);
974 inode->i_mode = old_mode;
975 return -EIO;
978 if ( cii->c_flags )
979 coda_flag_inode_children(inode, C_FLUSH);
981 cii->c_flags &= ~(C_VATTR | C_PURGE | C_FLUSH);
984 return 0;