ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / fs / coda / dir.c
1
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  */
10
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>
20
21 #include <asm/uaccess.h>
22
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>
29
30 /* dir inode-ops */
31 static int coda_create(struct inode *dir, struct dentry *new, int mode, struct nameidata *nd);
32 static int coda_mknod(struct inode *dir, struct dentry *new, int mode, dev_t rdev);
33 static struct dentry *coda_lookup(struct inode *dir, struct dentry *target, struct nameidata *nd);
34 static int coda_link(struct dentry *old_dentry, struct inode *dir_inode, 
35                      struct dentry *entry);
36 static int coda_unlink(struct inode *dir_inode, struct dentry *entry);
37 static int coda_symlink(struct inode *dir_inode, struct dentry *entry,
38                         const char *symname);
39 static int coda_mkdir(struct inode *dir_inode, struct dentry *entry, int mode);
40 static int coda_rmdir(struct inode *dir_inode, struct dentry *entry);
41 static int coda_rename(struct inode *old_inode, struct dentry *old_dentry, 
42                        struct inode *new_inode, struct dentry *new_dentry);
43
44 /* dir file-ops */
45 static int coda_readdir(struct file *file, void *dirent, filldir_t filldir);
46
47 /* dentry ops */
48 static int coda_dentry_revalidate(struct dentry *de, struct nameidata *nd);
49 static int coda_dentry_delete(struct dentry *);
50
51 /* support routines */
52 static int coda_venus_readdir(struct file *filp, filldir_t filldir,
53                               void *dirent, struct dentry *dir);
54 int coda_fsync(struct file *, struct dentry *dentry, int datasync);
55
56 int coda_hasmknod;
57
58 struct dentry_operations coda_dentry_operations =
59 {
60         .d_revalidate   = coda_dentry_revalidate,
61         .d_delete       = coda_dentry_delete,
62 };
63
64 struct inode_operations coda_dir_inode_operations =
65 {
66         .create         = coda_create,
67         .lookup         = coda_lookup,
68         .link           = coda_link,
69         .unlink         = coda_unlink,
70         .symlink        = coda_symlink,
71         .mkdir          = coda_mkdir,
72         .rmdir          = coda_rmdir,
73         .mknod          = coda_mknod,
74         .rename         = coda_rename,
75         .permission     = coda_permission,
76         .getattr        = coda_getattr,
77         .setattr        = coda_setattr,
78 };
79
80 struct file_operations coda_dir_operations = {
81         .llseek         = generic_file_llseek,
82         .read           = generic_read_dir,
83         .readdir        = coda_readdir,
84         .open           = coda_open,
85         .flush          = coda_flush,
86         .release        = coda_release,
87         .fsync          = coda_fsync,
88 };
89
90
91 /* inode operations for directories */
92 /* access routines: lookup, readlink, permission */
93 static struct dentry *coda_lookup(struct inode *dir, struct dentry *entry, struct nameidata *nd)
94 {
95         struct inode *res_inode = NULL;
96         struct CodaFid resfid = { { 0, } };
97         int dropme = 0; /* to indicate entry should not be cached */
98         int type = 0;
99         int error = 0;
100         const char *name = entry->d_name.name;
101         size_t length = entry->d_name.len;
102         
103         if ( length > CODA_MAXNAMLEN ) {
104                 printk("name too long: lookup, %s (%*s)\n", 
105                        coda_i2s(dir), (int)length, name);
106                 return ERR_PTR(-ENAMETOOLONG);
107         }
108
109         lock_kernel();
110         /* control object, create inode on the fly */
111         if (coda_isroot(dir) && coda_iscontrol(name, length)) {
112                 error = coda_cnode_makectl(&res_inode, dir->i_sb);
113                 dropme = 1;
114                 goto exit;
115         }
116
117         error = venus_lookup(dir->i_sb, coda_i2f(dir), 
118                              (const char *)name, length, &type, &resfid);
119
120         res_inode = NULL;
121         if (!error) {
122                 if (type & CODA_NOCACHE) {
123                         type &= (~CODA_NOCACHE);
124                         dropme = 1;
125                 }
126
127                 error = coda_cnode_make(&res_inode, &resfid, dir->i_sb);
128                 if (error) {
129                         unlock_kernel();
130                         return ERR_PTR(error);
131                 }
132         } else if (error != -ENOENT) {
133                 unlock_kernel();
134                 return ERR_PTR(error);
135         }
136
137 exit:
138         entry->d_time = 0;
139         entry->d_op = &coda_dentry_operations;
140         d_add(entry, res_inode);
141         if ( dropme ) {
142                 d_drop(entry);
143                 coda_flag_inode(res_inode, C_VATTR);
144         }
145         unlock_kernel();
146         return NULL;
147 }
148
149
150 int coda_permission(struct inode *inode, int mask, struct nameidata *nd)
151 {
152         int error = 0;
153  
154         if (!mask)
155                 return 0; 
156
157         lock_kernel();
158
159         coda_vfs_stat.permission++;
160
161         if (coda_cache_check(inode, mask))
162                 goto out; 
163
164         error = venus_access(inode->i_sb, coda_i2f(inode), mask);
165     
166         if (!error)
167                 coda_cache_enter(inode, mask);
168
169  out:
170         unlock_kernel();
171
172         return error; 
173 }
174
175
176 static inline void coda_dir_changed(struct inode *dir, int link)
177 {
178 #ifdef REQUERY_VENUS_FOR_MTIME
179         /* invalidate the directory cnode's attributes so we refetch the
180          * attributes from venus next time the inode is referenced */
181         coda_flag_inode(dir, C_VATTR);
182 #else
183         /* optimistically we can also act as if our nose bleeds. The
184          * granularity of the mtime is coarse anyways so we might actually be
185          * right most of the time. Note: we only do this for directories. */
186         dir->i_mtime = dir->i_ctime = CURRENT_TIME;
187 #endif
188         if (link)
189                 dir->i_nlink += link;
190 }
191
192 /* creation routines: create, mknod, mkdir, link, symlink */
193 static int coda_create(struct inode *dir, struct dentry *de, int mode, struct nameidata *nd)
194 {
195         int error=0;
196         const char *name=de->d_name.name;
197         int length=de->d_name.len;
198         struct inode *inode;
199         struct CodaFid newfid;
200         struct coda_vattr attrs;
201
202         lock_kernel();
203         coda_vfs_stat.create++;
204
205         if (coda_isroot(dir) && coda_iscontrol(name, length)) {
206                 unlock_kernel();
207                 return -EPERM;
208         }
209
210         error = venus_create(dir->i_sb, coda_i2f(dir), name, length, 
211                                 0, mode, 0, &newfid, &attrs);
212
213         if ( error ) {
214                 unlock_kernel();
215                 d_drop(de);
216                 return error;
217         }
218
219         inode = coda_iget(dir->i_sb, &newfid, &attrs);
220         if ( IS_ERR(inode) ) {
221                 unlock_kernel();
222                 d_drop(de);
223                 return PTR_ERR(inode);
224         }
225
226         /* invalidate the directory cnode's attributes */
227         coda_dir_changed(dir, 0);
228         unlock_kernel();
229         d_instantiate(de, inode);
230         return 0;
231 }
232
233 static int coda_mknod(struct inode *dir, struct dentry *de, int mode, dev_t rdev)
234 {
235         int error=0;
236         const char *name=de->d_name.name;
237         int length=de->d_name.len;
238         struct inode *inode;
239         struct CodaFid newfid;
240         struct coda_vattr attrs;
241
242         if ( coda_hasmknod == 0 )
243                 return -EIO;
244
245         if (!old_valid_dev(rdev))
246                 return -EINVAL;
247
248         lock_kernel();
249         coda_vfs_stat.create++;
250
251         if (coda_isroot(dir) && coda_iscontrol(name, length)) {
252                 unlock_kernel();
253                 return -EPERM;
254         }
255
256         error = venus_create(dir->i_sb, coda_i2f(dir), name, length, 
257                                 0, mode, rdev, &newfid, &attrs);
258
259         if ( error ) {
260                 unlock_kernel();
261                 d_drop(de);
262                 return error;
263         }
264
265         inode = coda_iget(dir->i_sb, &newfid, &attrs);
266         if ( IS_ERR(inode) ) {
267                 unlock_kernel();
268                 d_drop(de);
269                 return PTR_ERR(inode);
270         }
271
272         /* invalidate the directory cnode's attributes */
273         coda_dir_changed(dir, 0);
274         unlock_kernel();
275         d_instantiate(de, inode);
276         return 0;
277 }                            
278
279 static int coda_mkdir(struct inode *dir, struct dentry *de, int mode)
280 {
281         struct inode *inode;
282         struct coda_vattr attrs;
283         const char *name = de->d_name.name;
284         int len = de->d_name.len;
285         int error;
286         struct CodaFid newfid;
287
288         lock_kernel();
289         coda_vfs_stat.mkdir++;
290
291         if (coda_isroot(dir) && coda_iscontrol(name, len)) {
292                 unlock_kernel();
293                 return -EPERM;
294         }
295
296         attrs.va_mode = mode;
297         error = venus_mkdir(dir->i_sb, coda_i2f(dir), 
298                                name, len, &newfid, &attrs);
299         
300         if ( error ) {
301                 unlock_kernel();
302                 d_drop(de);
303                 return error;
304         }
305          
306         inode = coda_iget(dir->i_sb, &newfid, &attrs);
307         if ( IS_ERR(inode) ) {
308                 unlock_kernel();
309                 d_drop(de);
310                 return PTR_ERR(inode);
311         }
312         
313         /* invalidate the directory cnode's attributes */
314         coda_dir_changed(dir, 1);
315         unlock_kernel();
316         d_instantiate(de, inode);
317         return 0;
318 }
319
320 /* try to make de an entry in dir_inodde linked to source_de */ 
321 static int coda_link(struct dentry *source_de, struct inode *dir_inode, 
322           struct dentry *de)
323 {
324         struct inode *inode = source_de->d_inode;
325         const char * name = de->d_name.name;
326         int len = de->d_name.len;
327         int error;
328
329         lock_kernel();
330         coda_vfs_stat.link++;
331
332         if (coda_isroot(dir_inode) && coda_iscontrol(name, len)) {
333                 unlock_kernel();
334                 return -EPERM;
335         }
336
337         error = venus_link(dir_inode->i_sb, coda_i2f(inode),
338                            coda_i2f(dir_inode), (const char *)name, len);
339
340         if (error) { 
341                 d_drop(de);
342                 goto out;
343         }
344
345         coda_dir_changed(dir_inode, 0);
346         atomic_inc(&inode->i_count);
347         d_instantiate(de, inode);
348         inode->i_nlink++;
349         
350 out:
351         unlock_kernel();
352         return(error);
353 }
354
355
356 static int coda_symlink(struct inode *dir_inode, struct dentry *de,
357                         const char *symname)
358 {
359         const char *name = de->d_name.name;
360         int len = de->d_name.len;
361         int symlen;
362         int error=0;
363         
364         lock_kernel();
365         coda_vfs_stat.symlink++;
366
367         if (coda_isroot(dir_inode) && coda_iscontrol(name, len)) {
368                 unlock_kernel();
369                 return -EPERM;
370         }
371
372         symlen = strlen(symname);
373         if ( symlen > CODA_MAXPATHLEN ) {
374                 unlock_kernel();
375                 return -ENAMETOOLONG;
376         }
377
378         /*
379          * This entry is now negative. Since we do not create
380          * an inode for the entry we have to drop it. 
381          */
382         d_drop(de);
383         error = venus_symlink(dir_inode->i_sb, coda_i2f(dir_inode), name, len, 
384                               symname, symlen);
385
386         /* mtime is no good anymore */
387         if ( !error )
388                 coda_dir_changed(dir_inode, 0);
389
390         unlock_kernel();
391         return error;
392 }
393
394 /* destruction routines: unlink, rmdir */
395 int coda_unlink(struct inode *dir, struct dentry *de)
396 {
397         int error;
398         const char *name = de->d_name.name;
399         int len = de->d_name.len;
400
401         lock_kernel();
402         coda_vfs_stat.unlink++;
403
404         error = venus_remove(dir->i_sb, coda_i2f(dir), name, len);
405         if ( error ) {
406                 unlock_kernel();
407                 return error;
408         }
409
410         coda_dir_changed(dir, 0);
411         de->d_inode->i_nlink--;
412         unlock_kernel();
413
414         return 0;
415 }
416
417 int coda_rmdir(struct inode *dir, struct dentry *de)
418 {
419         const char *name = de->d_name.name;
420         int len = de->d_name.len;
421         int error;
422
423         lock_kernel();
424         coda_vfs_stat.rmdir++;
425
426         if (!d_unhashed(de)) {
427                 unlock_kernel();
428                 return -EBUSY;
429         }
430         error = venus_rmdir(dir->i_sb, coda_i2f(dir), name, len);
431
432         if ( error ) {
433                 unlock_kernel();
434                 return error;
435         }
436
437         coda_dir_changed(dir, -1);
438         de->d_inode->i_nlink--;
439         d_delete(de);
440         unlock_kernel();
441
442         return 0;
443 }
444
445 /* rename */
446 static int coda_rename(struct inode *old_dir, struct dentry *old_dentry, 
447                        struct inode *new_dir, struct dentry *new_dentry)
448 {
449         const char *old_name = old_dentry->d_name.name;
450         const char *new_name = new_dentry->d_name.name;
451         int old_length = old_dentry->d_name.len;
452         int new_length = new_dentry->d_name.len;
453         int link_adjust = 0;
454         int error;
455
456         lock_kernel();
457         coda_vfs_stat.rename++;
458
459         error = venus_rename(old_dir->i_sb, coda_i2f(old_dir), 
460                              coda_i2f(new_dir), old_length, new_length, 
461                              (const char *) old_name, (const char *)new_name);
462
463         if ( !error ) {
464                 if ( new_dentry->d_inode ) {
465                         if ( S_ISDIR(new_dentry->d_inode->i_mode) )
466                                 link_adjust = 1;
467
468                         coda_dir_changed(old_dir, -link_adjust);
469                         coda_dir_changed(new_dir,  link_adjust);
470                         coda_flag_inode(new_dentry->d_inode, C_VATTR);
471                 } else {
472                         coda_flag_inode(old_dir, C_VATTR);
473                         coda_flag_inode(new_dir, C_VATTR);
474                 }
475         }
476         unlock_kernel();
477
478         return error;
479 }
480
481
482 /* file operations for directories */
483 int coda_readdir(struct file *coda_file, void *dirent, filldir_t filldir)
484 {
485         struct dentry *coda_dentry = coda_file->f_dentry;
486         struct coda_file_info *cfi;
487         struct file *host_file;
488         struct inode *host_inode;
489         int ret;
490
491         cfi = CODA_FTOC(coda_file);
492         BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
493         host_file = cfi->cfi_container;
494
495         coda_vfs_stat.readdir++;
496
497         host_inode = host_file->f_dentry->d_inode;
498         down(&host_inode->i_sem);
499         host_file->f_pos = coda_file->f_pos;
500
501         if (!host_file->f_op->readdir) {
502                 /* Venus: we must read Venus dirents from the file */
503                 ret = coda_venus_readdir(host_file, filldir, dirent, coda_dentry);
504         } else {
505                 /* potemkin case: we were handed a directory inode. */
506                 /* Yuk, we can't call vfs_readdir because we are already
507                  * holding the inode semaphore. */
508                 ret = -ENOTDIR;
509                 if (!host_file->f_op || !host_file->f_op->readdir)
510                         goto out;
511
512                 ret = -ENOENT;
513                 if (!IS_DEADDIR(host_inode)) {
514                         ret = host_file->f_op->readdir(host_file, filldir, dirent);
515                         file_accessed(host_file);
516                 }
517         }
518 out:
519         coda_file->f_pos = host_file->f_pos;
520         up(&host_inode->i_sem);
521
522         return ret;
523 }
524
525 static inline unsigned int CDT2DT(unsigned char cdt)
526 {
527         unsigned int dt;
528
529         switch(cdt) {
530         case CDT_UNKNOWN: dt = DT_UNKNOWN; break;
531         case CDT_FIFO:    dt = DT_FIFO;    break;
532         case CDT_CHR:     dt = DT_CHR;     break;
533         case CDT_DIR:     dt = DT_DIR;     break;
534         case CDT_BLK:     dt = DT_BLK;     break;
535         case CDT_REG:     dt = DT_REG;     break;
536         case CDT_LNK:     dt = DT_LNK;     break;
537         case CDT_SOCK:    dt = DT_SOCK;    break;
538         case CDT_WHT:     dt = DT_WHT;     break;
539         default:          dt = DT_UNKNOWN; break;
540         }
541         return dt;
542 }
543
544 /* support routines */
545 static int coda_venus_readdir(struct file *filp, filldir_t filldir,
546                               void *dirent, struct dentry *dir)
547 {
548         int result = 0; /* # of entries returned */
549         struct venus_dirent *vdir;
550         unsigned long vdir_size =
551             (unsigned long)(&((struct venus_dirent *)0)->d_name);
552         unsigned int type;
553         struct qstr name;
554         ino_t ino;
555         int ret, i;
556
557         vdir = (struct venus_dirent *)kmalloc(sizeof(*vdir), GFP_KERNEL);
558         if (!vdir) return -ENOMEM;
559
560         i = filp->f_pos;
561         switch(i) {
562         case 0:
563                 ret = filldir(dirent, ".", 1, 0, dir->d_inode->i_ino, DT_DIR);
564                 if (ret < 0) break;
565                 result++;
566                 filp->f_pos++;
567                 /* fallthrough */
568         case 1:
569                 ret = filldir(dirent, "..", 2, 1, dir->d_parent->d_inode->i_ino, DT_DIR);
570                 if (ret < 0) break;
571                 result++;
572                 filp->f_pos++;
573                 /* fallthrough */
574         default:
575         while (1) {
576                 /* read entries from the directory file */
577                 ret = kernel_read(filp, filp->f_pos - 2, (char *)vdir,
578                                   sizeof(*vdir));
579                 if (ret < 0) {
580                         printk("coda_venus_readdir: read dir failed %d\n", ret);
581                         break;
582                 }
583                 if (ret == 0) break; /* end of directory file reached */
584
585                 /* catch truncated reads */
586                 if (ret < vdir_size || ret < vdir_size + vdir->d_namlen) {
587                         printk("coda_venus_readdir: short read: %ld\n",
588                                filp->f_dentry->d_inode->i_ino);
589                         ret = -EBADF;
590                         break;
591                 }
592                 /* validate whether the directory file actually makes sense */
593                 if (vdir->d_reclen < vdir_size + vdir->d_namlen) {
594                         printk("coda_venus_readdir: Invalid dir: %ld\n",
595                                filp->f_dentry->d_inode->i_ino);
596                         ret = -EBADF;
597                         break;
598                 }
599
600                 name.len = vdir->d_namlen;
601                 name.name = vdir->d_name;
602
603                 /* Make sure we skip '.' and '..', we already got those */
604                 if (name.name[0] == '.' && (name.len == 1 ||
605                     (vdir->d_name[1] == '.' && name.len == 2)))
606                         vdir->d_fileno = name.len = 0;
607
608                 /* skip null entries */
609                 if (vdir->d_fileno && name.len) {
610                         /* try to look up this entry in the dcache, that way
611                          * userspace doesn't have to worry about breaking
612                          * getcwd by having mismatched inode numbers for
613                          * internal volume mountpoints. */
614                         ino = find_inode_number(dir, &name);
615                         if (!ino) ino = vdir->d_fileno;
616
617                         type = CDT2DT(vdir->d_type);
618                         ret = filldir(dirent, name.name, name.len, filp->f_pos,
619                                       ino, type); 
620                         /* failure means no space for filling in this round */
621                         if (ret < 0) break;
622                         result++;
623                 }
624                 /* we'll always have progress because d_reclen is unsigned and
625                  * we've already established it is non-zero. */
626                 filp->f_pos += vdir->d_reclen;
627         }
628         } 
629         kfree(vdir);
630         return result ? result : ret;
631 }
632
633 /* called when a cache lookup succeeds */
634 static int coda_dentry_revalidate(struct dentry *de, struct nameidata *nd)
635 {
636         struct inode *inode = de->d_inode;
637         struct coda_inode_info *cii;
638
639         if (!inode)
640                 return 1;
641         lock_kernel();
642         if (coda_isroot(inode))
643                 goto out;
644         if (is_bad_inode(inode))
645                 goto bad;
646
647         cii = ITOC(de->d_inode);
648         if (!(cii->c_flags & (C_PURGE | C_FLUSH)))
649                 goto out;
650
651         shrink_dcache_parent(de);
652
653         /* propagate for a flush */
654         if (cii->c_flags & C_FLUSH) 
655                 coda_flag_inode_children(inode, C_FLUSH);
656
657         if (atomic_read(&de->d_count) > 1)
658                 /* pretend it's valid, but don't change the flags */
659                 goto out;
660
661         /* clear the flags. */
662         cii->c_flags &= ~(C_VATTR | C_PURGE | C_FLUSH);
663
664 bad:
665         unlock_kernel();
666         return 0;
667 out:
668         unlock_kernel();
669         return 1;
670 }
671
672 /*
673  * This is the callback from dput() when d_count is going to 0.
674  * We use this to unhash dentries with bad inodes.
675  */
676 static int coda_dentry_delete(struct dentry * dentry)
677 {
678         int flags;
679
680         if (!dentry->d_inode) 
681                 return 0;
682
683         flags = (ITOC(dentry->d_inode)->c_flags) & C_PURGE;
684         if (is_bad_inode(dentry->d_inode) || flags) {
685                 return 1;
686         }
687         return 0;
688 }
689
690
691
692 /*
693  * This is called when we want to check if the inode has
694  * changed on the server.  Coda makes this easy since the
695  * cache manager Venus issues a downcall to the kernel when this 
696  * happens 
697  */
698 int coda_revalidate_inode(struct dentry *dentry)
699 {
700         struct coda_vattr attr;
701         int error = 0;
702         int old_mode;
703         ino_t old_ino;
704         struct inode *inode = dentry->d_inode;
705         struct coda_inode_info *cii = ITOC(inode);
706
707         lock_kernel();
708         if ( !cii->c_flags )
709                 goto ok;
710
711         if (cii->c_flags & (C_VATTR | C_PURGE | C_FLUSH)) {
712                 error = venus_getattr(inode->i_sb, &(cii->c_fid), &attr);
713                 if ( error )
714                         goto return_bad;
715
716                 /* this inode may be lost if:
717                    - it's ino changed 
718                    - type changes must be permitted for repair and
719                    missing mount points.
720                 */
721                 old_mode = inode->i_mode;
722                 old_ino = inode->i_ino;
723                 coda_vattr_to_iattr(inode, &attr);
724
725                 if ((old_mode & S_IFMT) != (inode->i_mode & S_IFMT)) {
726                         printk("Coda: inode %ld, fid %s changed type!\n",
727                                inode->i_ino, coda_f2s(&(cii->c_fid)));
728                 }
729
730                 /* the following can happen when a local fid is replaced 
731                    with a global one, here we lose and declare the inode bad */
732                 if (inode->i_ino != old_ino)
733                         goto return_bad;
734                 
735                 coda_flag_inode_children(inode, C_FLUSH);
736                 cii->c_flags &= ~(C_VATTR | C_PURGE | C_FLUSH);
737         }
738
739 ok:
740         unlock_kernel();
741         return 0;
742
743 return_bad:
744         unlock_kernel();
745         return -EIO;
746 }