pcmcia: add socket register data to sysfs for yenta devices
[linux-2.6.git] / fs / coda / dir.c
blob2391766e9c7c206f3d5a0b67d0a0da17f5a47fde
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/time.h>
14 #include <linux/fs.h>
15 #include <linux/file.h>
16 #include <linux/stat.h>
17 #include <linux/errno.h>
18 #include <linux/string.h>
19 #include <linux/smp_lock.h>
21 #include <asm/uaccess.h>
23 #include <linux/coda.h>
24 #include <linux/coda_linux.h>
25 #include <linux/coda_psdev.h>
26 #include <linux/coda_fs_i.h>
27 #include <linux/coda_cache.h>
28 #include <linux/coda_proc.h>
30 /* dir inode-ops */
31 static int coda_create(struct inode *dir, struct dentry *new, int mode, struct nameidata *nd);
32 static struct dentry *coda_lookup(struct inode *dir, struct dentry *target, struct nameidata *nd);
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, struct nameidata *nd);
48 static int coda_dentry_delete(struct dentry *);
50 /* support routines */
51 static int coda_venus_readdir(struct file *filp, filldir_t filldir,
52 void *dirent, struct dentry *dir);
53 int coda_fsync(struct file *, struct dentry *dentry, int datasync);
55 /* same as fs/bad_inode.c */
56 static int coda_return_EIO(void)
58 return -EIO;
60 #define CODA_EIO_ERROR ((void *) (coda_return_EIO))
62 static struct dentry_operations coda_dentry_operations =
64 .d_revalidate = coda_dentry_revalidate,
65 .d_delete = coda_dentry_delete,
68 struct inode_operations coda_dir_inode_operations =
70 .create = coda_create,
71 .lookup = coda_lookup,
72 .link = coda_link,
73 .unlink = coda_unlink,
74 .symlink = coda_symlink,
75 .mkdir = coda_mkdir,
76 .rmdir = coda_rmdir,
77 .mknod = CODA_EIO_ERROR,
78 .rename = coda_rename,
79 .permission = coda_permission,
80 .getattr = coda_getattr,
81 .setattr = coda_setattr,
84 struct file_operations coda_dir_operations = {
85 .llseek = generic_file_llseek,
86 .read = generic_read_dir,
87 .readdir = coda_readdir,
88 .open = coda_open,
89 .flush = coda_flush,
90 .release = coda_release,
91 .fsync = coda_fsync,
95 /* inode operations for directories */
96 /* access routines: lookup, readlink, permission */
97 static struct dentry *coda_lookup(struct inode *dir, struct dentry *entry, struct nameidata *nd)
99 struct inode *res_inode = NULL;
100 struct CodaFid resfid = { { 0, } };
101 int dropme = 0; /* to indicate entry should not be cached */
102 int type = 0;
103 int error = 0;
104 const char *name = entry->d_name.name;
105 size_t length = entry->d_name.len;
107 if ( length > CODA_MAXNAMLEN ) {
108 printk("name too long: lookup, %s (%*s)\n",
109 coda_i2s(dir), (int)length, name);
110 return ERR_PTR(-ENAMETOOLONG);
113 lock_kernel();
114 /* control object, create inode on the fly */
115 if (coda_isroot(dir) && coda_iscontrol(name, length)) {
116 error = coda_cnode_makectl(&res_inode, dir->i_sb);
117 dropme = 1;
118 goto exit;
121 error = venus_lookup(dir->i_sb, coda_i2f(dir),
122 (const char *)name, length, &type, &resfid);
124 res_inode = NULL;
125 if (!error) {
126 if (type & CODA_NOCACHE) {
127 type &= (~CODA_NOCACHE);
128 dropme = 1;
131 error = coda_cnode_make(&res_inode, &resfid, dir->i_sb);
132 if (error) {
133 unlock_kernel();
134 return ERR_PTR(error);
136 } else if (error != -ENOENT) {
137 unlock_kernel();
138 return ERR_PTR(error);
141 exit:
142 entry->d_time = 0;
143 entry->d_op = &coda_dentry_operations;
144 d_add(entry, res_inode);
145 if ( dropme ) {
146 d_drop(entry);
147 coda_flag_inode(res_inode, C_VATTR);
149 unlock_kernel();
150 return NULL;
154 int coda_permission(struct inode *inode, int mask, struct nameidata *nd)
156 int error = 0;
158 if (!mask)
159 return 0;
161 lock_kernel();
163 coda_vfs_stat.permission++;
165 if (coda_cache_check(inode, mask))
166 goto out;
168 error = venus_access(inode->i_sb, coda_i2f(inode), mask);
170 if (!error)
171 coda_cache_enter(inode, mask);
173 out:
174 unlock_kernel();
176 return error;
180 static inline void coda_dir_changed(struct inode *dir, int link)
182 #ifdef REQUERY_VENUS_FOR_MTIME
183 /* invalidate the directory cnode's attributes so we refetch the
184 * attributes from venus next time the inode is referenced */
185 coda_flag_inode(dir, C_VATTR);
186 #else
187 /* optimistically we can also act as if our nose bleeds. The
188 * granularity of the mtime is coarse anyways so we might actually be
189 * right most of the time. Note: we only do this for directories. */
190 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
191 #endif
192 if (link)
193 dir->i_nlink += link;
196 /* creation routines: create, mknod, mkdir, link, symlink */
197 static int coda_create(struct inode *dir, struct dentry *de, int mode, struct nameidata *nd)
199 int error=0;
200 const char *name=de->d_name.name;
201 int length=de->d_name.len;
202 struct inode *inode;
203 struct CodaFid newfid;
204 struct coda_vattr attrs;
206 lock_kernel();
207 coda_vfs_stat.create++;
209 if (coda_isroot(dir) && coda_iscontrol(name, length)) {
210 unlock_kernel();
211 return -EPERM;
214 error = venus_create(dir->i_sb, coda_i2f(dir), name, length,
215 0, mode, &newfid, &attrs);
217 if ( error ) {
218 unlock_kernel();
219 d_drop(de);
220 return error;
223 inode = coda_iget(dir->i_sb, &newfid, &attrs);
224 if ( IS_ERR(inode) ) {
225 unlock_kernel();
226 d_drop(de);
227 return PTR_ERR(inode);
230 /* invalidate the directory cnode's attributes */
231 coda_dir_changed(dir, 0);
232 unlock_kernel();
233 d_instantiate(de, inode);
234 return 0;
237 static int coda_mkdir(struct inode *dir, struct dentry *de, int mode)
239 struct inode *inode;
240 struct coda_vattr attrs;
241 const char *name = de->d_name.name;
242 int len = de->d_name.len;
243 int error;
244 struct CodaFid newfid;
246 lock_kernel();
247 coda_vfs_stat.mkdir++;
249 if (coda_isroot(dir) && coda_iscontrol(name, len)) {
250 unlock_kernel();
251 return -EPERM;
254 attrs.va_mode = mode;
255 error = venus_mkdir(dir->i_sb, coda_i2f(dir),
256 name, len, &newfid, &attrs);
258 if ( error ) {
259 unlock_kernel();
260 d_drop(de);
261 return error;
264 inode = coda_iget(dir->i_sb, &newfid, &attrs);
265 if ( IS_ERR(inode) ) {
266 unlock_kernel();
267 d_drop(de);
268 return PTR_ERR(inode);
271 /* invalidate the directory cnode's attributes */
272 coda_dir_changed(dir, 1);
273 unlock_kernel();
274 d_instantiate(de, inode);
275 return 0;
278 /* try to make de an entry in dir_inodde linked to source_de */
279 static int coda_link(struct dentry *source_de, struct inode *dir_inode,
280 struct dentry *de)
282 struct inode *inode = source_de->d_inode;
283 const char * name = de->d_name.name;
284 int len = de->d_name.len;
285 int error;
287 lock_kernel();
288 coda_vfs_stat.link++;
290 if (coda_isroot(dir_inode) && coda_iscontrol(name, len)) {
291 unlock_kernel();
292 return -EPERM;
295 error = venus_link(dir_inode->i_sb, coda_i2f(inode),
296 coda_i2f(dir_inode), (const char *)name, len);
298 if (error) {
299 d_drop(de);
300 goto out;
303 coda_dir_changed(dir_inode, 0);
304 atomic_inc(&inode->i_count);
305 d_instantiate(de, inode);
306 inode->i_nlink++;
308 out:
309 unlock_kernel();
310 return(error);
314 static int coda_symlink(struct inode *dir_inode, struct dentry *de,
315 const char *symname)
317 const char *name = de->d_name.name;
318 int len = de->d_name.len;
319 int symlen;
320 int error=0;
322 lock_kernel();
323 coda_vfs_stat.symlink++;
325 if (coda_isroot(dir_inode) && coda_iscontrol(name, len)) {
326 unlock_kernel();
327 return -EPERM;
330 symlen = strlen(symname);
331 if ( symlen > CODA_MAXPATHLEN ) {
332 unlock_kernel();
333 return -ENAMETOOLONG;
337 * This entry is now negative. Since we do not create
338 * an inode for the entry we have to drop it.
340 d_drop(de);
341 error = venus_symlink(dir_inode->i_sb, coda_i2f(dir_inode), name, len,
342 symname, symlen);
344 /* mtime is no good anymore */
345 if ( !error )
346 coda_dir_changed(dir_inode, 0);
348 unlock_kernel();
349 return error;
352 /* destruction routines: unlink, rmdir */
353 int coda_unlink(struct inode *dir, struct dentry *de)
355 int error;
356 const char *name = de->d_name.name;
357 int len = de->d_name.len;
359 lock_kernel();
360 coda_vfs_stat.unlink++;
362 error = venus_remove(dir->i_sb, coda_i2f(dir), name, len);
363 if ( error ) {
364 unlock_kernel();
365 return error;
368 coda_dir_changed(dir, 0);
369 de->d_inode->i_nlink--;
370 unlock_kernel();
372 return 0;
375 int coda_rmdir(struct inode *dir, struct dentry *de)
377 const char *name = de->d_name.name;
378 int len = de->d_name.len;
379 int error;
381 lock_kernel();
382 coda_vfs_stat.rmdir++;
384 if (!d_unhashed(de)) {
385 unlock_kernel();
386 return -EBUSY;
388 error = venus_rmdir(dir->i_sb, coda_i2f(dir), name, len);
390 if ( error ) {
391 unlock_kernel();
392 return error;
395 coda_dir_changed(dir, -1);
396 de->d_inode->i_nlink--;
397 d_delete(de);
398 unlock_kernel();
400 return 0;
403 /* rename */
404 static int coda_rename(struct inode *old_dir, struct dentry *old_dentry,
405 struct inode *new_dir, struct dentry *new_dentry)
407 const char *old_name = old_dentry->d_name.name;
408 const char *new_name = new_dentry->d_name.name;
409 int old_length = old_dentry->d_name.len;
410 int new_length = new_dentry->d_name.len;
411 int link_adjust = 0;
412 int error;
414 lock_kernel();
415 coda_vfs_stat.rename++;
417 error = venus_rename(old_dir->i_sb, coda_i2f(old_dir),
418 coda_i2f(new_dir), old_length, new_length,
419 (const char *) old_name, (const char *)new_name);
421 if ( !error ) {
422 if ( new_dentry->d_inode ) {
423 if ( S_ISDIR(new_dentry->d_inode->i_mode) )
424 link_adjust = 1;
426 coda_dir_changed(old_dir, -link_adjust);
427 coda_dir_changed(new_dir, link_adjust);
428 coda_flag_inode(new_dentry->d_inode, C_VATTR);
429 } else {
430 coda_flag_inode(old_dir, C_VATTR);
431 coda_flag_inode(new_dir, C_VATTR);
434 unlock_kernel();
436 return error;
440 /* file operations for directories */
441 int coda_readdir(struct file *coda_file, void *dirent, filldir_t filldir)
443 struct dentry *coda_dentry = coda_file->f_dentry;
444 struct coda_file_info *cfi;
445 struct file *host_file;
446 struct inode *host_inode;
447 int ret;
449 cfi = CODA_FTOC(coda_file);
450 BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
451 host_file = cfi->cfi_container;
453 coda_vfs_stat.readdir++;
455 host_inode = host_file->f_dentry->d_inode;
456 down(&host_inode->i_sem);
457 host_file->f_pos = coda_file->f_pos;
459 if (!host_file->f_op->readdir) {
460 /* Venus: we must read Venus dirents from the file */
461 ret = coda_venus_readdir(host_file, filldir, dirent, coda_dentry);
462 } else {
463 /* potemkin case: we were handed a directory inode. */
464 /* Yuk, we can't call vfs_readdir because we are already
465 * holding the inode semaphore. */
466 ret = -ENOTDIR;
467 if (!host_file->f_op || !host_file->f_op->readdir)
468 goto out;
470 ret = -ENOENT;
471 if (!IS_DEADDIR(host_inode)) {
472 ret = host_file->f_op->readdir(host_file, filldir, dirent);
473 file_accessed(host_file);
476 out:
477 coda_file->f_pos = host_file->f_pos;
478 up(&host_inode->i_sem);
480 return ret;
483 static inline unsigned int CDT2DT(unsigned char cdt)
485 unsigned int dt;
487 switch(cdt) {
488 case CDT_UNKNOWN: dt = DT_UNKNOWN; break;
489 case CDT_FIFO: dt = DT_FIFO; break;
490 case CDT_CHR: dt = DT_CHR; break;
491 case CDT_DIR: dt = DT_DIR; break;
492 case CDT_BLK: dt = DT_BLK; break;
493 case CDT_REG: dt = DT_REG; break;
494 case CDT_LNK: dt = DT_LNK; break;
495 case CDT_SOCK: dt = DT_SOCK; break;
496 case CDT_WHT: dt = DT_WHT; break;
497 default: dt = DT_UNKNOWN; break;
499 return dt;
502 /* support routines */
503 static int coda_venus_readdir(struct file *filp, filldir_t filldir,
504 void *dirent, struct dentry *dir)
506 int result = 0; /* # of entries returned */
507 struct venus_dirent *vdir;
508 unsigned long vdir_size =
509 (unsigned long)(&((struct venus_dirent *)0)->d_name);
510 unsigned int type;
511 struct qstr name;
512 ino_t ino;
513 int ret, i;
515 vdir = (struct venus_dirent *)kmalloc(sizeof(*vdir), GFP_KERNEL);
516 if (!vdir) return -ENOMEM;
518 i = filp->f_pos;
519 switch(i) {
520 case 0:
521 ret = filldir(dirent, ".", 1, 0, dir->d_inode->i_ino, DT_DIR);
522 if (ret < 0) break;
523 result++;
524 filp->f_pos++;
525 /* fallthrough */
526 case 1:
527 ret = filldir(dirent, "..", 2, 1, dir->d_parent->d_inode->i_ino, DT_DIR);
528 if (ret < 0) break;
529 result++;
530 filp->f_pos++;
531 /* fallthrough */
532 default:
533 while (1) {
534 /* read entries from the directory file */
535 ret = kernel_read(filp, filp->f_pos - 2, (char *)vdir,
536 sizeof(*vdir));
537 if (ret < 0) {
538 printk("coda_venus_readdir: read dir failed %d\n", ret);
539 break;
541 if (ret == 0) break; /* end of directory file reached */
543 /* catch truncated reads */
544 if (ret < vdir_size || ret < vdir_size + vdir->d_namlen) {
545 printk("coda_venus_readdir: short read: %ld\n",
546 filp->f_dentry->d_inode->i_ino);
547 ret = -EBADF;
548 break;
550 /* validate whether the directory file actually makes sense */
551 if (vdir->d_reclen < vdir_size + vdir->d_namlen) {
552 printk("coda_venus_readdir: Invalid dir: %ld\n",
553 filp->f_dentry->d_inode->i_ino);
554 ret = -EBADF;
555 break;
558 name.len = vdir->d_namlen;
559 name.name = vdir->d_name;
561 /* Make sure we skip '.' and '..', we already got those */
562 if (name.name[0] == '.' && (name.len == 1 ||
563 (vdir->d_name[1] == '.' && name.len == 2)))
564 vdir->d_fileno = name.len = 0;
566 /* skip null entries */
567 if (vdir->d_fileno && name.len) {
568 /* try to look up this entry in the dcache, that way
569 * userspace doesn't have to worry about breaking
570 * getcwd by having mismatched inode numbers for
571 * internal volume mountpoints. */
572 ino = find_inode_number(dir, &name);
573 if (!ino) ino = vdir->d_fileno;
575 type = CDT2DT(vdir->d_type);
576 ret = filldir(dirent, name.name, name.len, filp->f_pos,
577 ino, type);
578 /* failure means no space for filling in this round */
579 if (ret < 0) break;
580 result++;
582 /* we'll always have progress because d_reclen is unsigned and
583 * we've already established it is non-zero. */
584 filp->f_pos += vdir->d_reclen;
587 kfree(vdir);
588 return result ? result : ret;
591 /* called when a cache lookup succeeds */
592 static int coda_dentry_revalidate(struct dentry *de, struct nameidata *nd)
594 struct inode *inode = de->d_inode;
595 struct coda_inode_info *cii;
597 if (!inode)
598 return 1;
599 lock_kernel();
600 if (coda_isroot(inode))
601 goto out;
602 if (is_bad_inode(inode))
603 goto bad;
605 cii = ITOC(de->d_inode);
606 if (!(cii->c_flags & (C_PURGE | C_FLUSH)))
607 goto out;
609 shrink_dcache_parent(de);
611 /* propagate for a flush */
612 if (cii->c_flags & C_FLUSH)
613 coda_flag_inode_children(inode, C_FLUSH);
615 if (atomic_read(&de->d_count) > 1)
616 /* pretend it's valid, but don't change the flags */
617 goto out;
619 /* clear the flags. */
620 cii->c_flags &= ~(C_VATTR | C_PURGE | C_FLUSH);
622 bad:
623 unlock_kernel();
624 return 0;
625 out:
626 unlock_kernel();
627 return 1;
631 * This is the callback from dput() when d_count is going to 0.
632 * We use this to unhash dentries with bad inodes.
634 static int coda_dentry_delete(struct dentry * dentry)
636 int flags;
638 if (!dentry->d_inode)
639 return 0;
641 flags = (ITOC(dentry->d_inode)->c_flags) & C_PURGE;
642 if (is_bad_inode(dentry->d_inode) || flags) {
643 return 1;
645 return 0;
651 * This is called when we want to check if the inode has
652 * changed on the server. Coda makes this easy since the
653 * cache manager Venus issues a downcall to the kernel when this
654 * happens
656 int coda_revalidate_inode(struct dentry *dentry)
658 struct coda_vattr attr;
659 int error = 0;
660 int old_mode;
661 ino_t old_ino;
662 struct inode *inode = dentry->d_inode;
663 struct coda_inode_info *cii = ITOC(inode);
665 lock_kernel();
666 if ( !cii->c_flags )
667 goto ok;
669 if (cii->c_flags & (C_VATTR | C_PURGE | C_FLUSH)) {
670 error = venus_getattr(inode->i_sb, &(cii->c_fid), &attr);
671 if ( error )
672 goto return_bad;
674 /* this inode may be lost if:
675 - it's ino changed
676 - type changes must be permitted for repair and
677 missing mount points.
679 old_mode = inode->i_mode;
680 old_ino = inode->i_ino;
681 coda_vattr_to_iattr(inode, &attr);
683 if ((old_mode & S_IFMT) != (inode->i_mode & S_IFMT)) {
684 printk("Coda: inode %ld, fid %s changed type!\n",
685 inode->i_ino, coda_f2s(&(cii->c_fid)));
688 /* the following can happen when a local fid is replaced
689 with a global one, here we lose and declare the inode bad */
690 if (inode->i_ino != old_ino)
691 goto return_bad;
693 coda_flag_inode_children(inode, C_FLUSH);
694 cii->c_flags &= ~(C_VATTR | C_PURGE | C_FLUSH);
698 unlock_kernel();
699 return 0;
701 return_bad:
702 unlock_kernel();
703 return -EIO;