]> Git Repo - linux.git/blob - fs/afs/super.c
afs: Lay the groundwork for supporting network namespaces
[linux.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 <[email protected]>
13  *          David Woodhouse <[email protected]>
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 static const struct super_operations afs_super_ops = {
52         .statfs         = afs_statfs,
53         .alloc_inode    = afs_alloc_inode,
54         .drop_inode     = afs_drop_inode,
55         .destroy_inode  = afs_destroy_inode,
56         .evict_inode    = afs_evict_inode,
57         .show_devname   = afs_show_devname,
58         .show_options   = afs_show_options,
59 };
60
61 static struct kmem_cache *afs_inode_cachep;
62 static atomic_t afs_count_active_inodes;
63
64 enum {
65         afs_no_opt,
66         afs_opt_cell,
67         afs_opt_rwpath,
68         afs_opt_vol,
69         afs_opt_autocell,
70 };
71
72 static const match_table_t afs_options_list = {
73         { afs_opt_cell,         "cell=%s"       },
74         { afs_opt_rwpath,       "rwpath"        },
75         { afs_opt_vol,          "vol=%s"        },
76         { afs_opt_autocell,     "autocell"      },
77         { afs_no_opt,           NULL            },
78 };
79
80 /*
81  * initialise the filesystem
82  */
83 int __init afs_fs_init(void)
84 {
85         int ret;
86
87         _enter("");
88
89         /* create ourselves an inode cache */
90         atomic_set(&afs_count_active_inodes, 0);
91
92         ret = -ENOMEM;
93         afs_inode_cachep = kmem_cache_create("afs_inode_cache",
94                                              sizeof(struct afs_vnode),
95                                              0,
96                                              SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT,
97                                              afs_i_init_once);
98         if (!afs_inode_cachep) {
99                 printk(KERN_NOTICE "kAFS: Failed to allocate inode cache\n");
100                 return ret;
101         }
102
103         /* now export our filesystem to lesser mortals */
104         ret = register_filesystem(&afs_fs_type);
105         if (ret < 0) {
106                 kmem_cache_destroy(afs_inode_cachep);
107                 _leave(" = %d", ret);
108                 return ret;
109         }
110
111         _leave(" = 0");
112         return 0;
113 }
114
115 /*
116  * clean up the filesystem
117  */
118 void __exit afs_fs_exit(void)
119 {
120         _enter("");
121
122         afs_mntpt_kill_timer();
123         unregister_filesystem(&afs_fs_type);
124
125         if (atomic_read(&afs_count_active_inodes) != 0) {
126                 printk("kAFS: %d active inode objects still present\n",
127                        atomic_read(&afs_count_active_inodes));
128                 BUG();
129         }
130
131         /*
132          * Make sure all delayed rcu free inodes are flushed before we
133          * destroy cache.
134          */
135         rcu_barrier();
136         kmem_cache_destroy(afs_inode_cachep);
137         _leave("");
138 }
139
140 /*
141  * Display the mount device name in /proc/mounts.
142  */
143 static int afs_show_devname(struct seq_file *m, struct dentry *root)
144 {
145         struct afs_super_info *as = root->d_sb->s_fs_info;
146         struct afs_volume *volume = as->volume;
147         struct afs_cell *cell = volume->cell;
148         const char *suf = "";
149         char pref = '%';
150
151         switch (volume->type) {
152         case AFSVL_RWVOL:
153                 break;
154         case AFSVL_ROVOL:
155                 pref = '#';
156                 if (volume->type_force)
157                         suf = ".readonly";
158                 break;
159         case AFSVL_BACKVOL:
160                 pref = '#';
161                 suf = ".backup";
162                 break;
163         }
164
165         seq_printf(m, "%c%s:%s%s", pref, cell->name, volume->vlocation->vldb.name, suf);
166         return 0;
167 }
168
169 /*
170  * Display the mount options in /proc/mounts.
171  */
172 static int afs_show_options(struct seq_file *m, struct dentry *root)
173 {
174         if (test_bit(AFS_VNODE_AUTOCELL, &AFS_FS_I(d_inode(root))->flags))
175                 seq_puts(m, "autocell");
176         return 0;
177 }
178
179 /*
180  * parse the mount options
181  * - this function has been shamelessly adapted from the ext3 fs which
182  *   shamelessly adapted it from the msdos fs
183  */
184 static int afs_parse_options(struct afs_mount_params *params,
185                              char *options, const char **devname)
186 {
187         struct afs_cell *cell;
188         substring_t args[MAX_OPT_ARGS];
189         char *p;
190         int token;
191
192         _enter("%s", options);
193
194         options[PAGE_SIZE - 1] = 0;
195
196         while ((p = strsep(&options, ","))) {
197                 if (!*p)
198                         continue;
199
200                 token = match_token(p, afs_options_list, args);
201                 switch (token) {
202                 case afs_opt_cell:
203                         cell = afs_cell_lookup(params->net,
204                                                args[0].from,
205                                                args[0].to - args[0].from,
206                                                false);
207                         if (IS_ERR(cell))
208                                 return PTR_ERR(cell);
209                         afs_put_cell(params->cell);
210                         params->cell = cell;
211                         break;
212
213                 case afs_opt_rwpath:
214                         params->rwpath = 1;
215                         break;
216
217                 case afs_opt_vol:
218                         *devname = args[0].from;
219                         break;
220
221                 case afs_opt_autocell:
222                         params->autocell = 1;
223                         break;
224
225                 default:
226                         printk(KERN_ERR "kAFS:"
227                                " Unknown or invalid mount option: '%s'\n", p);
228                         return -EINVAL;
229                 }
230         }
231
232         _leave(" = 0");
233         return 0;
234 }
235
236 /*
237  * parse a device name to get cell name, volume name, volume type and R/W
238  * selector
239  * - this can be one of the following:
240  *      "%[cell:]volume[.]"             R/W volume
241  *      "#[cell:]volume[.]"             R/O or R/W volume (rwpath=0),
242  *                                       or R/W (rwpath=1) volume
243  *      "%[cell:]volume.readonly"       R/O volume
244  *      "#[cell:]volume.readonly"       R/O volume
245  *      "%[cell:]volume.backup"         Backup volume
246  *      "#[cell:]volume.backup"         Backup volume
247  */
248 static int afs_parse_device_name(struct afs_mount_params *params,
249                                  const char *name)
250 {
251         struct afs_cell *cell;
252         const char *cellname, *suffix;
253         int cellnamesz;
254
255         _enter(",%s", name);
256
257         if (!name) {
258                 printk(KERN_ERR "kAFS: no volume name specified\n");
259                 return -EINVAL;
260         }
261
262         if ((name[0] != '%' && name[0] != '#') || !name[1]) {
263                 printk(KERN_ERR "kAFS: unparsable volume name\n");
264                 return -EINVAL;
265         }
266
267         /* determine the type of volume we're looking for */
268         params->type = AFSVL_ROVOL;
269         params->force = false;
270         if (params->rwpath || name[0] == '%') {
271                 params->type = AFSVL_RWVOL;
272                 params->force = true;
273         }
274         name++;
275
276         /* split the cell name out if there is one */
277         params->volname = strchr(name, ':');
278         if (params->volname) {
279                 cellname = name;
280                 cellnamesz = params->volname - name;
281                 params->volname++;
282         } else {
283                 params->volname = name;
284                 cellname = NULL;
285                 cellnamesz = 0;
286         }
287
288         /* the volume type is further affected by a possible suffix */
289         suffix = strrchr(params->volname, '.');
290         if (suffix) {
291                 if (strcmp(suffix, ".readonly") == 0) {
292                         params->type = AFSVL_ROVOL;
293                         params->force = true;
294                 } else if (strcmp(suffix, ".backup") == 0) {
295                         params->type = AFSVL_BACKVOL;
296                         params->force = true;
297                 } else if (suffix[1] == 0) {
298                 } else {
299                         suffix = NULL;
300                 }
301         }
302
303         params->volnamesz = suffix ?
304                 suffix - params->volname : strlen(params->volname);
305
306         _debug("cell %*.*s [%p]",
307                cellnamesz, cellnamesz, cellname ?: "", params->cell);
308
309         /* lookup the cell record */
310         if (cellname || !params->cell) {
311                 cell = afs_cell_lookup(params->net, cellname, cellnamesz, true);
312                 if (IS_ERR(cell)) {
313                         printk(KERN_ERR "kAFS: unable to lookup cell '%*.*s'\n",
314                                cellnamesz, cellnamesz, cellname ?: "");
315                         return PTR_ERR(cell);
316                 }
317                 afs_put_cell(params->cell);
318                 params->cell = cell;
319         }
320
321         _debug("CELL:%s [%p] VOLUME:%*.*s SUFFIX:%s TYPE:%d%s",
322                params->cell->name, params->cell,
323                params->volnamesz, params->volnamesz, params->volname,
324                suffix ?: "-", params->type, params->force ? " FORCE" : "");
325
326         return 0;
327 }
328
329 /*
330  * check a superblock to see if it's the one we're looking for
331  */
332 static int afs_test_super(struct super_block *sb, void *data)
333 {
334         struct afs_super_info *as1 = data;
335         struct afs_super_info *as = sb->s_fs_info;
336
337         return as->net == as1->net && as->volume == as1->volume;
338 }
339
340 static int afs_set_super(struct super_block *sb, void *data)
341 {
342         sb->s_fs_info = data;
343         return set_anon_super(sb, NULL);
344 }
345
346 /*
347  * fill in the superblock
348  */
349 static int afs_fill_super(struct super_block *sb,
350                           struct afs_mount_params *params)
351 {
352         struct afs_super_info *as = sb->s_fs_info;
353         struct afs_fid fid;
354         struct inode *inode = NULL;
355         int ret;
356
357         _enter("");
358
359         /* fill in the superblock */
360         sb->s_blocksize         = PAGE_SIZE;
361         sb->s_blocksize_bits    = PAGE_SHIFT;
362         sb->s_magic             = AFS_FS_MAGIC;
363         sb->s_op                = &afs_super_ops;
364         sb->s_xattr             = afs_xattr_handlers;
365         ret = super_setup_bdi(sb);
366         if (ret)
367                 return ret;
368         sb->s_bdi->ra_pages     = VM_MAX_READAHEAD * 1024 / PAGE_SIZE;
369         strlcpy(sb->s_id, as->volume->vlocation->vldb.name, sizeof(sb->s_id));
370
371         /* allocate the root inode and dentry */
372         fid.vid         = as->volume->vid;
373         fid.vnode       = 1;
374         fid.unique      = 1;
375         inode = afs_iget(sb, params->key, &fid, NULL, NULL);
376         if (IS_ERR(inode))
377                 return PTR_ERR(inode);
378
379         if (params->autocell)
380                 set_bit(AFS_VNODE_AUTOCELL, &AFS_FS_I(inode)->flags);
381
382         ret = -ENOMEM;
383         sb->s_root = d_make_root(inode);
384         if (!sb->s_root)
385                 goto error;
386
387         sb->s_d_op = &afs_fs_dentry_operations;
388
389         _leave(" = 0");
390         return 0;
391
392 error:
393         _leave(" = %d", ret);
394         return ret;
395 }
396
397 /*
398  * get an AFS superblock
399  */
400 static struct dentry *afs_mount(struct file_system_type *fs_type,
401                       int flags, const char *dev_name, void *options)
402 {
403         struct afs_mount_params params;
404         struct super_block *sb;
405         struct afs_volume *vol;
406         struct key *key;
407         char *new_opts = kstrdup(options, GFP_KERNEL);
408         struct afs_super_info *as;
409         int ret;
410
411         _enter(",,%s,%p", dev_name, options);
412
413         memset(&params, 0, sizeof(params));
414         params.net = &__afs_net;
415
416         ret = -EINVAL;
417         if (current->nsproxy->net_ns != &init_net)
418                 goto error;
419
420         /* parse the options and device name */
421         if (options) {
422                 ret = afs_parse_options(&params, options, &dev_name);
423                 if (ret < 0)
424                         goto error;
425         }
426
427         ret = afs_parse_device_name(&params, dev_name);
428         if (ret < 0)
429                 goto error;
430
431         /* try and do the mount securely */
432         key = afs_request_key(params.cell);
433         if (IS_ERR(key)) {
434                 _leave(" = %ld [key]", PTR_ERR(key));
435                 ret = PTR_ERR(key);
436                 goto error;
437         }
438         params.key = key;
439
440         /* parse the device name */
441         vol = afs_volume_lookup(&params);
442         if (IS_ERR(vol)) {
443                 ret = PTR_ERR(vol);
444                 goto error;
445         }
446
447         /* allocate a superblock info record */
448         ret = -ENOMEM;
449         as = kzalloc(sizeof(struct afs_super_info), GFP_KERNEL);
450         if (!as)
451                 goto error_vol;
452
453         as->net = afs_get_net(params.net);
454         as->volume = vol;
455
456         /* allocate a deviceless superblock */
457         sb = sget(fs_type, afs_test_super, afs_set_super, flags, as);
458         if (IS_ERR(sb)) {
459                 ret = PTR_ERR(sb);
460                 goto error_as;
461         }
462
463         if (!sb->s_root) {
464                 /* initial superblock/root creation */
465                 _debug("create");
466                 ret = afs_fill_super(sb, &params);
467                 if (ret < 0)
468                         goto error_sb;
469                 sb->s_flags |= MS_ACTIVE;
470         } else {
471                 _debug("reuse");
472                 ASSERTCMP(sb->s_flags, &, MS_ACTIVE);
473                 afs_put_volume(params.net, vol);
474                 kfree(as);
475         }
476
477         afs_put_cell(params.cell);
478         kfree(new_opts);
479         _leave(" = 0 [%p]", sb);
480         return dget(sb->s_root);
481
482 error_sb:
483         deactivate_locked_super(sb);
484         goto error;
485 error_as:
486         afs_put_net(as->net);
487         kfree(as);
488 error_vol:
489         afs_put_volume(params.net, vol);
490 error:
491         afs_put_cell(params.cell);
492         key_put(params.key);
493         kfree(new_opts);
494         _leave(" = %d", ret);
495         return ERR_PTR(ret);
496 }
497
498 static void afs_kill_super(struct super_block *sb)
499 {
500         struct afs_super_info *as = sb->s_fs_info;
501         struct afs_net *net = as->net;
502
503         kill_anon_super(sb);
504         afs_put_volume(net, as->volume);
505         kfree(as);
506 }
507
508 /*
509  * initialise an inode cache slab element prior to any use
510  */
511 static void afs_i_init_once(void *_vnode)
512 {
513         struct afs_vnode *vnode = _vnode;
514
515         memset(vnode, 0, sizeof(*vnode));
516         inode_init_once(&vnode->vfs_inode);
517         init_waitqueue_head(&vnode->update_waitq);
518         mutex_init(&vnode->permits_lock);
519         mutex_init(&vnode->validate_lock);
520         spin_lock_init(&vnode->writeback_lock);
521         spin_lock_init(&vnode->lock);
522         INIT_LIST_HEAD(&vnode->writebacks);
523         INIT_LIST_HEAD(&vnode->pending_locks);
524         INIT_LIST_HEAD(&vnode->granted_locks);
525         INIT_DELAYED_WORK(&vnode->lock_work, afs_lock_work);
526         INIT_WORK(&vnode->cb_broken_work, afs_broken_callback_work);
527 }
528
529 /*
530  * allocate an AFS inode struct from our slab cache
531  */
532 static struct inode *afs_alloc_inode(struct super_block *sb)
533 {
534         struct afs_vnode *vnode;
535
536         vnode = kmem_cache_alloc(afs_inode_cachep, GFP_KERNEL);
537         if (!vnode)
538                 return NULL;
539
540         atomic_inc(&afs_count_active_inodes);
541
542         memset(&vnode->fid, 0, sizeof(vnode->fid));
543         memset(&vnode->status, 0, sizeof(vnode->status));
544
545         vnode->volume           = NULL;
546         vnode->update_cnt       = 0;
547         vnode->flags            = 1 << AFS_VNODE_UNSET;
548         vnode->cb_promised      = false;
549
550         _leave(" = %p", &vnode->vfs_inode);
551         return &vnode->vfs_inode;
552 }
553
554 static void afs_i_callback(struct rcu_head *head)
555 {
556         struct inode *inode = container_of(head, struct inode, i_rcu);
557         struct afs_vnode *vnode = AFS_FS_I(inode);
558         kmem_cache_free(afs_inode_cachep, vnode);
559 }
560
561 /*
562  * destroy an AFS inode struct
563  */
564 static void afs_destroy_inode(struct inode *inode)
565 {
566         struct afs_vnode *vnode = AFS_FS_I(inode);
567
568         _enter("%p{%x:%u}", inode, vnode->fid.vid, vnode->fid.vnode);
569
570         _debug("DESTROY INODE %p", inode);
571
572         ASSERTCMP(vnode->server, ==, NULL);
573
574         call_rcu(&inode->i_rcu, afs_i_callback);
575         atomic_dec(&afs_count_active_inodes);
576 }
577
578 /*
579  * return information about an AFS volume
580  */
581 static int afs_statfs(struct dentry *dentry, struct kstatfs *buf)
582 {
583         struct afs_volume_status vs;
584         struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
585         struct key *key;
586         int ret;
587
588         key = afs_request_key(vnode->volume->cell);
589         if (IS_ERR(key))
590                 return PTR_ERR(key);
591
592         ret = afs_vnode_get_volume_status(vnode, key, &vs);
593         key_put(key);
594         if (ret < 0) {
595                 _leave(" = %d", ret);
596                 return ret;
597         }
598
599         buf->f_type     = dentry->d_sb->s_magic;
600         buf->f_bsize    = AFS_BLOCK_SIZE;
601         buf->f_namelen  = AFSNAMEMAX - 1;
602
603         if (vs.max_quota == 0)
604                 buf->f_blocks = vs.part_max_blocks;
605         else
606                 buf->f_blocks = vs.max_quota;
607         buf->f_bavail = buf->f_bfree = buf->f_blocks - vs.blocks_in_use;
608         return 0;
609 }
This page took 0.066689 seconds and 4 git commands to generate.