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