OSDN Git Service

8707d867334ee212ee738f0d3619fb8432b30e57
[tomoyo/tomoyo-test1.git] / fs / afs / super.c
1 /* AFS superblock handling
2  *
3  * Copyright (c) 2002, 2007 Red Hat, Inc. All rights reserved.
4  *
5  * This software may be freely redistributed under the terms of the
6  * GNU General Public License.
7  *
8  * You should have received a copy of the GNU General Public License
9  * along with this program; if not, write to the Free Software
10  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
11  *
12  * Authors: David Howells <dhowells@redhat.com>
13  *          David Woodhouse <dwmw2@infradead.org>
14  *
15  */
16
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/mount.h>
20 #include <linux/init.h>
21 #include <linux/slab.h>
22 #include <linux/fs.h>
23 #include <linux/pagemap.h>
24 #include <linux/parser.h>
25 #include <linux/statfs.h>
26 #include <linux/sched.h>
27 #include <linux/nsproxy.h>
28 #include <linux/magic.h>
29 #include <net/net_namespace.h>
30 #include "internal.h"
31
32 static void afs_i_init_once(void *foo);
33 static struct dentry *afs_mount(struct file_system_type *fs_type,
34                       int flags, const char *dev_name, void *data);
35 static void afs_kill_super(struct super_block *sb);
36 static struct inode *afs_alloc_inode(struct super_block *sb);
37 static void afs_destroy_inode(struct inode *inode);
38 static int afs_statfs(struct dentry *dentry, struct kstatfs *buf);
39 static int afs_show_devname(struct seq_file *m, struct dentry *root);
40 static int afs_show_options(struct seq_file *m, struct dentry *root);
41
42 struct file_system_type afs_fs_type = {
43         .owner          = THIS_MODULE,
44         .name           = "afs",
45         .mount          = afs_mount,
46         .kill_sb        = afs_kill_super,
47         .fs_flags       = 0,
48 };
49 MODULE_ALIAS_FS("afs");
50
51 int afs_net_id;
52
53 static const struct super_operations afs_super_ops = {
54         .statfs         = afs_statfs,
55         .alloc_inode    = afs_alloc_inode,
56         .drop_inode     = afs_drop_inode,
57         .destroy_inode  = afs_destroy_inode,
58         .evict_inode    = afs_evict_inode,
59         .show_devname   = afs_show_devname,
60         .show_options   = afs_show_options,
61 };
62
63 static struct kmem_cache *afs_inode_cachep;
64 static atomic_t afs_count_active_inodes;
65
66 enum {
67         afs_no_opt,
68         afs_opt_cell,
69         afs_opt_dyn,
70         afs_opt_rwpath,
71         afs_opt_vol,
72         afs_opt_autocell,
73 };
74
75 static const match_table_t afs_options_list = {
76         { afs_opt_cell,         "cell=%s"       },
77         { afs_opt_dyn,          "dyn"           },
78         { afs_opt_rwpath,       "rwpath"        },
79         { afs_opt_vol,          "vol=%s"        },
80         { afs_opt_autocell,     "autocell"      },
81         { afs_no_opt,           NULL            },
82 };
83
84 /*
85  * initialise the filesystem
86  */
87 int __init afs_fs_init(void)
88 {
89         int ret;
90
91         _enter("");
92
93         /* create ourselves an inode cache */
94         atomic_set(&afs_count_active_inodes, 0);
95
96         ret = -ENOMEM;
97         afs_inode_cachep = kmem_cache_create("afs_inode_cache",
98                                              sizeof(struct afs_vnode),
99                                              0,
100                                              SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT,
101                                              afs_i_init_once);
102         if (!afs_inode_cachep) {
103                 printk(KERN_NOTICE "kAFS: Failed to allocate inode cache\n");
104                 return ret;
105         }
106
107         /* now export our filesystem to lesser mortals */
108         ret = register_filesystem(&afs_fs_type);
109         if (ret < 0) {
110                 kmem_cache_destroy(afs_inode_cachep);
111                 _leave(" = %d", ret);
112                 return ret;
113         }
114
115         _leave(" = 0");
116         return 0;
117 }
118
119 /*
120  * clean up the filesystem
121  */
122 void afs_fs_exit(void)
123 {
124         _enter("");
125
126         afs_mntpt_kill_timer();
127         unregister_filesystem(&afs_fs_type);
128
129         if (atomic_read(&afs_count_active_inodes) != 0) {
130                 printk("kAFS: %d active inode objects still present\n",
131                        atomic_read(&afs_count_active_inodes));
132                 BUG();
133         }
134
135         /*
136          * Make sure all delayed rcu free inodes are flushed before we
137          * destroy cache.
138          */
139         rcu_barrier();
140         kmem_cache_destroy(afs_inode_cachep);
141         _leave("");
142 }
143
144 /*
145  * Display the mount device name in /proc/mounts.
146  */
147 static int afs_show_devname(struct seq_file *m, struct dentry *root)
148 {
149         struct afs_super_info *as = AFS_FS_S(root->d_sb);
150         struct afs_volume *volume = as->volume;
151         struct afs_cell *cell = as->cell;
152         const char *suf = "";
153         char pref = '%';
154
155         if (as->dyn_root) {
156                 seq_puts(m, "none");
157                 return 0;
158         }
159
160         switch (volume->type) {
161         case AFSVL_RWVOL:
162                 break;
163         case AFSVL_ROVOL:
164                 pref = '#';
165                 if (volume->type_force)
166                         suf = ".readonly";
167                 break;
168         case AFSVL_BACKVOL:
169                 pref = '#';
170                 suf = ".backup";
171                 break;
172         }
173
174         seq_printf(m, "%c%s:%s%s", pref, cell->name, volume->name, suf);
175         return 0;
176 }
177
178 /*
179  * Display the mount options in /proc/mounts.
180  */
181 static int afs_show_options(struct seq_file *m, struct dentry *root)
182 {
183         struct afs_super_info *as = AFS_FS_S(root->d_sb);
184
185         if (as->dyn_root)
186                 seq_puts(m, ",dyn");
187         if (test_bit(AFS_VNODE_AUTOCELL, &AFS_FS_I(d_inode(root))->flags))
188                 seq_puts(m, ",autocell");
189         return 0;
190 }
191
192 /*
193  * parse the mount options
194  * - this function has been shamelessly adapted from the ext3 fs which
195  *   shamelessly adapted it from the msdos fs
196  */
197 static int afs_parse_options(struct afs_mount_params *params,
198                              char *options, const char **devname)
199 {
200         struct afs_cell *cell;
201         substring_t args[MAX_OPT_ARGS];
202         char *p;
203         int token;
204
205         _enter("%s", options);
206
207         options[PAGE_SIZE - 1] = 0;
208
209         while ((p = strsep(&options, ","))) {
210                 if (!*p)
211                         continue;
212
213                 token = match_token(p, afs_options_list, args);
214                 switch (token) {
215                 case afs_opt_cell:
216                         rcu_read_lock();
217                         cell = afs_lookup_cell_rcu(params->net,
218                                                    args[0].from,
219                                                    args[0].to - args[0].from);
220                         rcu_read_unlock();
221                         if (IS_ERR(cell))
222                                 return PTR_ERR(cell);
223                         afs_put_cell(params->net, params->cell);
224                         params->cell = cell;
225                         break;
226
227                 case afs_opt_rwpath:
228                         params->rwpath = true;
229                         break;
230
231                 case afs_opt_vol:
232                         *devname = args[0].from;
233                         break;
234
235                 case afs_opt_autocell:
236                         params->autocell = true;
237                         break;
238
239                 case afs_opt_dyn:
240                         params->dyn_root = true;
241                         break;
242
243                 default:
244                         printk(KERN_ERR "kAFS:"
245                                " Unknown or invalid mount option: '%s'\n", p);
246                         return -EINVAL;
247                 }
248         }
249
250         _leave(" = 0");
251         return 0;
252 }
253
254 /*
255  * parse a device name to get cell name, volume name, volume type and R/W
256  * selector
257  * - this can be one of the following:
258  *      "%[cell:]volume[.]"             R/W volume
259  *      "#[cell:]volume[.]"             R/O or R/W volume (rwpath=0),
260  *                                       or R/W (rwpath=1) volume
261  *      "%[cell:]volume.readonly"       R/O volume
262  *      "#[cell:]volume.readonly"       R/O volume
263  *      "%[cell:]volume.backup"         Backup volume
264  *      "#[cell:]volume.backup"         Backup volume
265  */
266 static int afs_parse_device_name(struct afs_mount_params *params,
267                                  const char *name)
268 {
269         struct afs_cell *cell;
270         const char *cellname, *suffix;
271         int cellnamesz;
272
273         _enter(",%s", name);
274
275         if (!name) {
276                 printk(KERN_ERR "kAFS: no volume name specified\n");
277                 return -EINVAL;
278         }
279
280         if ((name[0] != '%' && name[0] != '#') || !name[1]) {
281                 printk(KERN_ERR "kAFS: unparsable volume name\n");
282                 return -EINVAL;
283         }
284
285         /* determine the type of volume we're looking for */
286         params->type = AFSVL_ROVOL;
287         params->force = false;
288         if (params->rwpath || name[0] == '%') {
289                 params->type = AFSVL_RWVOL;
290                 params->force = true;
291         }
292         name++;
293
294         /* split the cell name out if there is one */
295         params->volname = strchr(name, ':');
296         if (params->volname) {
297                 cellname = name;
298                 cellnamesz = params->volname - name;
299                 params->volname++;
300         } else {
301                 params->volname = name;
302                 cellname = NULL;
303                 cellnamesz = 0;
304         }
305
306         /* the volume type is further affected by a possible suffix */
307         suffix = strrchr(params->volname, '.');
308         if (suffix) {
309                 if (strcmp(suffix, ".readonly") == 0) {
310                         params->type = AFSVL_ROVOL;
311                         params->force = true;
312                 } else if (strcmp(suffix, ".backup") == 0) {
313                         params->type = AFSVL_BACKVOL;
314                         params->force = true;
315                 } else if (suffix[1] == 0) {
316                 } else {
317                         suffix = NULL;
318                 }
319         }
320
321         params->volnamesz = suffix ?
322                 suffix - params->volname : strlen(params->volname);
323
324         _debug("cell %*.*s [%p]",
325                cellnamesz, cellnamesz, cellname ?: "", params->cell);
326
327         /* lookup the cell record */
328         if (cellname || !params->cell) {
329                 cell = afs_lookup_cell(params->net, cellname, cellnamesz,
330                                        NULL, false);
331                 if (IS_ERR(cell)) {
332                         printk(KERN_ERR "kAFS: unable to lookup cell '%*.*s'\n",
333                                cellnamesz, cellnamesz, cellname ?: "");
334                         return PTR_ERR(cell);
335                 }
336                 afs_put_cell(params->net, params->cell);
337                 params->cell = cell;
338         }
339
340         _debug("CELL:%s [%p] VOLUME:%*.*s SUFFIX:%s TYPE:%d%s",
341                params->cell->name, params->cell,
342                params->volnamesz, params->volnamesz, params->volname,
343                suffix ?: "-", params->type, params->force ? " FORCE" : "");
344
345         return 0;
346 }
347
348 /*
349  * check a superblock to see if it's the one we're looking for
350  */
351 static int afs_test_super(struct super_block *sb, void *data)
352 {
353         struct afs_super_info *as1 = data;
354         struct afs_super_info *as = AFS_FS_S(sb);
355
356         return (as->net_ns == as1->net_ns &&
357                 as->volume &&
358                 as->volume->vid == as1->volume->vid);
359 }
360
361 static int afs_dynroot_test_super(struct super_block *sb, void *data)
362 {
363         return false;
364 }
365
366 static int afs_set_super(struct super_block *sb, void *data)
367 {
368         struct afs_super_info *as = data;
369
370         sb->s_fs_info = as;
371         return set_anon_super(sb, NULL);
372 }
373
374 /*
375  * fill in the superblock
376  */
377 static int afs_fill_super(struct super_block *sb,
378                           struct afs_mount_params *params)
379 {
380         struct afs_super_info *as = AFS_FS_S(sb);
381         struct afs_fid fid;
382         struct inode *inode = NULL;
383         int ret;
384
385         _enter("");
386
387         /* fill in the superblock */
388         sb->s_blocksize         = PAGE_SIZE;
389         sb->s_blocksize_bits    = PAGE_SHIFT;
390         sb->s_magic             = AFS_FS_MAGIC;
391         sb->s_op                = &afs_super_ops;
392         if (!as->dyn_root)
393                 sb->s_xattr     = afs_xattr_handlers;
394         ret = super_setup_bdi(sb);
395         if (ret)
396                 return ret;
397         sb->s_bdi->ra_pages     = VM_MAX_READAHEAD * 1024 / PAGE_SIZE;
398
399         /* allocate the root inode and dentry */
400         if (as->dyn_root) {
401                 inode = afs_iget_pseudo_dir(sb, true);
402                 sb->s_flags     |= SB_RDONLY;
403         } else {
404                 sprintf(sb->s_id, "%u", as->volume->vid);
405                 afs_activate_volume(as->volume);
406                 fid.vid         = as->volume->vid;
407                 fid.vnode       = 1;
408                 fid.unique      = 1;
409                 inode = afs_iget(sb, params->key, &fid, NULL, NULL, NULL);
410         }
411
412         if (IS_ERR(inode))
413                 return PTR_ERR(inode);
414
415         if (params->autocell || params->dyn_root)
416                 set_bit(AFS_VNODE_AUTOCELL, &AFS_FS_I(inode)->flags);
417
418         ret = -ENOMEM;
419         sb->s_root = d_make_root(inode);
420         if (!sb->s_root)
421                 goto error;
422
423         if (params->dyn_root)
424                 sb->s_d_op = &afs_dynroot_dentry_operations;
425         else
426                 sb->s_d_op = &afs_fs_dentry_operations;
427
428         _leave(" = 0");
429         return 0;
430
431 error:
432         _leave(" = %d", ret);
433         return ret;
434 }
435
436 static struct afs_super_info *afs_alloc_sbi(struct afs_mount_params *params)
437 {
438         struct afs_super_info *as;
439
440         as = kzalloc(sizeof(struct afs_super_info), GFP_KERNEL);
441         if (as) {
442                 as->net_ns = get_net(params->net_ns);
443                 if (params->dyn_root)
444                         as->dyn_root = true;
445                 else
446                         as->cell = afs_get_cell(params->cell);
447         }
448         return as;
449 }
450
451 static void afs_destroy_sbi(struct afs_super_info *as)
452 {
453         if (as) {
454                 afs_put_volume(as->cell, as->volume);
455                 afs_put_cell(afs_net(as->net_ns), as->cell);
456                 put_net(as->net_ns);
457                 kfree(as);
458         }
459 }
460
461 /*
462  * get an AFS superblock
463  */
464 static struct dentry *afs_mount(struct file_system_type *fs_type,
465                                 int flags, const char *dev_name, void *options)
466 {
467         struct afs_mount_params params;
468         struct super_block *sb;
469         struct afs_volume *candidate;
470         struct key *key;
471         struct afs_super_info *as;
472         int ret;
473
474         _enter(",,%s,%p", dev_name, options);
475
476         memset(&params, 0, sizeof(params));
477
478         ret = -EINVAL;
479         if (current->nsproxy->net_ns != &init_net)
480                 goto error;
481         params.net_ns = current->nsproxy->net_ns;
482         params.net = afs_net(params.net_ns);
483         
484         /* parse the options and device name */
485         if (options) {
486                 ret = afs_parse_options(&params, options, &dev_name);
487                 if (ret < 0)
488                         goto error;
489         }
490
491         if (!params.dyn_root) {
492                 ret = afs_parse_device_name(&params, dev_name);
493                 if (ret < 0)
494                         goto error;
495
496                 /* try and do the mount securely */
497                 key = afs_request_key(params.cell);
498                 if (IS_ERR(key)) {
499                         _leave(" = %ld [key]", PTR_ERR(key));
500                         ret = PTR_ERR(key);
501                         goto error;
502                 }
503                 params.key = key;
504         }
505
506         /* allocate a superblock info record */
507         ret = -ENOMEM;
508         as = afs_alloc_sbi(&params);
509         if (!as)
510                 goto error_key;
511
512         if (!params.dyn_root) {
513                 /* Assume we're going to need a volume record; at the very
514                  * least we can use it to update the volume record if we have
515                  * one already.  This checks that the volume exists within the
516                  * cell.
517                  */
518                 candidate = afs_create_volume(&params);
519                 if (IS_ERR(candidate)) {
520                         ret = PTR_ERR(candidate);
521                         goto error_as;
522                 }
523
524                 as->volume = candidate;
525         }
526
527         /* allocate a deviceless superblock */
528         sb = sget(fs_type,
529                   as->dyn_root ? afs_dynroot_test_super : afs_test_super,
530                   afs_set_super, flags, as);
531         if (IS_ERR(sb)) {
532                 ret = PTR_ERR(sb);
533                 goto error_as;
534         }
535
536         if (!sb->s_root) {
537                 /* initial superblock/root creation */
538                 _debug("create");
539                 ret = afs_fill_super(sb, &params);
540                 if (ret < 0)
541                         goto error_sb;
542                 as = NULL;
543                 sb->s_flags |= SB_ACTIVE;
544         } else {
545                 _debug("reuse");
546                 ASSERTCMP(sb->s_flags, &, SB_ACTIVE);
547                 afs_destroy_sbi(as);
548                 as = NULL;
549         }
550
551         afs_put_cell(params.net, params.cell);
552         key_put(params.key);
553         _leave(" = 0 [%p]", sb);
554         return dget(sb->s_root);
555
556 error_sb:
557         deactivate_locked_super(sb);
558         goto error_key;
559 error_as:
560         afs_destroy_sbi(as);
561 error_key:
562         key_put(params.key);
563 error:
564         afs_put_cell(params.net, params.cell);
565         _leave(" = %d", ret);
566         return ERR_PTR(ret);
567 }
568
569 static void afs_kill_super(struct super_block *sb)
570 {
571         struct afs_super_info *as = AFS_FS_S(sb);
572
573         /* Clear the callback interests (which will do ilookup5) before
574          * deactivating the superblock.
575          */
576         if (as->volume)
577                 afs_clear_callback_interests(afs_net(as->net_ns),
578                                              as->volume->servers);
579         kill_anon_super(sb);
580         if (as->volume)
581                 afs_deactivate_volume(as->volume);
582         afs_destroy_sbi(as);
583 }
584
585 /*
586  * Initialise an inode cache slab element prior to any use.  Note that
587  * afs_alloc_inode() *must* reset anything that could incorrectly leak from one
588  * inode to another.
589  */
590 static void afs_i_init_once(void *_vnode)
591 {
592         struct afs_vnode *vnode = _vnode;
593
594         memset(vnode, 0, sizeof(*vnode));
595         inode_init_once(&vnode->vfs_inode);
596         mutex_init(&vnode->io_lock);
597         init_rwsem(&vnode->validate_lock);
598         spin_lock_init(&vnode->wb_lock);
599         spin_lock_init(&vnode->lock);
600         INIT_LIST_HEAD(&vnode->wb_keys);
601         INIT_LIST_HEAD(&vnode->pending_locks);
602         INIT_LIST_HEAD(&vnode->granted_locks);
603         INIT_DELAYED_WORK(&vnode->lock_work, afs_lock_work);
604         seqlock_init(&vnode->cb_lock);
605 }
606
607 /*
608  * allocate an AFS inode struct from our slab cache
609  */
610 static struct inode *afs_alloc_inode(struct super_block *sb)
611 {
612         struct afs_vnode *vnode;
613
614         vnode = kmem_cache_alloc(afs_inode_cachep, GFP_KERNEL);
615         if (!vnode)
616                 return NULL;
617
618         atomic_inc(&afs_count_active_inodes);
619
620         /* Reset anything that shouldn't leak from one inode to the next. */
621         memset(&vnode->fid, 0, sizeof(vnode->fid));
622         memset(&vnode->status, 0, sizeof(vnode->status));
623
624         vnode->volume           = NULL;
625         vnode->lock_key         = NULL;
626         vnode->permit_cache     = NULL;
627         vnode->cb_interest      = NULL;
628 #ifdef CONFIG_AFS_FSCACHE
629         vnode->cache            = NULL;
630 #endif
631
632         vnode->flags            = 1 << AFS_VNODE_UNSET;
633         vnode->cb_type          = 0;
634         vnode->lock_state       = AFS_VNODE_LOCK_NONE;
635
636         _leave(" = %p", &vnode->vfs_inode);
637         return &vnode->vfs_inode;
638 }
639
640 static void afs_i_callback(struct rcu_head *head)
641 {
642         struct inode *inode = container_of(head, struct inode, i_rcu);
643         struct afs_vnode *vnode = AFS_FS_I(inode);
644         kmem_cache_free(afs_inode_cachep, vnode);
645 }
646
647 /*
648  * destroy an AFS inode struct
649  */
650 static void afs_destroy_inode(struct inode *inode)
651 {
652         struct afs_vnode *vnode = AFS_FS_I(inode);
653
654         _enter("%p{%x:%u}", inode, vnode->fid.vid, vnode->fid.vnode);
655
656         _debug("DESTROY INODE %p", inode);
657
658         ASSERTCMP(vnode->cb_interest, ==, NULL);
659
660         call_rcu(&inode->i_rcu, afs_i_callback);
661         atomic_dec(&afs_count_active_inodes);
662 }
663
664 /*
665  * return information about an AFS volume
666  */
667 static int afs_statfs(struct dentry *dentry, struct kstatfs *buf)
668 {
669         struct afs_super_info *as = AFS_FS_S(dentry->d_sb);
670         struct afs_fs_cursor fc;
671         struct afs_volume_status vs;
672         struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
673         struct key *key;
674         int ret;
675
676         buf->f_type     = dentry->d_sb->s_magic;
677         buf->f_bsize    = AFS_BLOCK_SIZE;
678         buf->f_namelen  = AFSNAMEMAX - 1;
679
680         if (as->dyn_root) {
681                 buf->f_blocks   = 1;
682                 buf->f_bavail   = 0;
683                 buf->f_bfree    = 0;
684                 return 0;
685         }
686
687         key = afs_request_key(vnode->volume->cell);
688         if (IS_ERR(key))
689                 return PTR_ERR(key);
690
691         ret = -ERESTARTSYS;
692         if (afs_begin_vnode_operation(&fc, vnode, key)) {
693                 fc.flags |= AFS_FS_CURSOR_NO_VSLEEP;
694                 while (afs_select_fileserver(&fc)) {
695                         fc.cb_break = afs_calc_vnode_cb_break(vnode);
696                         afs_fs_get_volume_status(&fc, &vs);
697                 }
698
699                 afs_check_for_remote_deletion(&fc, fc.vnode);
700                 afs_vnode_commit_status(&fc, vnode, fc.cb_break);
701                 ret = afs_end_vnode_operation(&fc);
702         }
703
704         key_put(key);
705
706         if (ret == 0) {
707                 if (vs.max_quota == 0)
708                         buf->f_blocks = vs.part_max_blocks;
709                 else
710                         buf->f_blocks = vs.max_quota;
711                 buf->f_bavail = buf->f_bfree = buf->f_blocks - vs.blocks_in_use;
712         }
713
714         return ret;
715 }