1
2
3
4
5
6
7
8
9
10
11
12
13
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/fs_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
32static void afs_i_init_once(void *foo);
33static void afs_kill_super(struct super_block *sb);
34static struct inode *afs_alloc_inode(struct super_block *sb);
35static void afs_destroy_inode(struct inode *inode);
36static void afs_free_inode(struct inode *inode);
37static int afs_statfs(struct dentry *dentry, struct kstatfs *buf);
38static int afs_show_devname(struct seq_file *m, struct dentry *root);
39static int afs_show_options(struct seq_file *m, struct dentry *root);
40static int afs_init_fs_context(struct fs_context *fc);
41static const struct fs_parameter_spec afs_fs_parameters[];
42
43struct file_system_type afs_fs_type = {
44 .owner = THIS_MODULE,
45 .name = "afs",
46 .init_fs_context = afs_init_fs_context,
47 .parameters = afs_fs_parameters,
48 .kill_sb = afs_kill_super,
49 .fs_flags = FS_RENAME_DOES_D_MOVE,
50};
51MODULE_ALIAS_FS("afs");
52
53int afs_net_id;
54
55static const struct super_operations afs_super_ops = {
56 .statfs = afs_statfs,
57 .alloc_inode = afs_alloc_inode,
58 .drop_inode = afs_drop_inode,
59 .destroy_inode = afs_destroy_inode,
60 .free_inode = afs_free_inode,
61 .evict_inode = afs_evict_inode,
62 .show_devname = afs_show_devname,
63 .show_options = afs_show_options,
64};
65
66static struct kmem_cache *afs_inode_cachep;
67static atomic_t afs_count_active_inodes;
68
69enum afs_param {
70 Opt_autocell,
71 Opt_dyn,
72 Opt_flock,
73 Opt_source,
74};
75
76static const struct constant_table afs_param_flock[] = {
77 {"local", afs_flock_mode_local },
78 {"openafs", afs_flock_mode_openafs },
79 {"strict", afs_flock_mode_strict },
80 {"write", afs_flock_mode_write },
81 {}
82};
83
84static const struct fs_parameter_spec afs_fs_parameters[] = {
85 fsparam_flag ("autocell", Opt_autocell),
86 fsparam_flag ("dyn", Opt_dyn),
87 fsparam_enum ("flock", Opt_flock, afs_param_flock),
88 fsparam_string("source", Opt_source),
89 {}
90};
91
92
93
94
95int __init afs_fs_init(void)
96{
97 int ret;
98
99 _enter("");
100
101
102 atomic_set(&afs_count_active_inodes, 0);
103
104 ret = -ENOMEM;
105 afs_inode_cachep = kmem_cache_create("afs_inode_cache",
106 sizeof(struct afs_vnode),
107 0,
108 SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT,
109 afs_i_init_once);
110 if (!afs_inode_cachep) {
111 printk(KERN_NOTICE "kAFS: Failed to allocate inode cache\n");
112 return ret;
113 }
114
115
116 ret = register_filesystem(&afs_fs_type);
117 if (ret < 0) {
118 kmem_cache_destroy(afs_inode_cachep);
119 _leave(" = %d", ret);
120 return ret;
121 }
122
123 _leave(" = 0");
124 return 0;
125}
126
127
128
129
130void afs_fs_exit(void)
131{
132 _enter("");
133
134 afs_mntpt_kill_timer();
135 unregister_filesystem(&afs_fs_type);
136
137 if (atomic_read(&afs_count_active_inodes) != 0) {
138 printk("kAFS: %d active inode objects still present\n",
139 atomic_read(&afs_count_active_inodes));
140 BUG();
141 }
142
143
144
145
146
147 rcu_barrier();
148 kmem_cache_destroy(afs_inode_cachep);
149 _leave("");
150}
151
152
153
154
155static int afs_show_devname(struct seq_file *m, struct dentry *root)
156{
157 struct afs_super_info *as = AFS_FS_S(root->d_sb);
158 struct afs_volume *volume = as->volume;
159 struct afs_cell *cell = as->cell;
160 const char *suf = "";
161 char pref = '%';
162
163 if (as->dyn_root) {
164 seq_puts(m, "none");
165 return 0;
166 }
167
168 switch (volume->type) {
169 case AFSVL_RWVOL:
170 break;
171 case AFSVL_ROVOL:
172 pref = '#';
173 if (volume->type_force)
174 suf = ".readonly";
175 break;
176 case AFSVL_BACKVOL:
177 pref = '#';
178 suf = ".backup";
179 break;
180 }
181
182 seq_printf(m, "%c%s:%s%s", pref, cell->name, volume->name, suf);
183 return 0;
184}
185
186
187
188
189static int afs_show_options(struct seq_file *m, struct dentry *root)
190{
191 struct afs_super_info *as = AFS_FS_S(root->d_sb);
192 const char *p = NULL;
193
194 if (as->dyn_root)
195 seq_puts(m, ",dyn");
196 if (test_bit(AFS_VNODE_AUTOCELL, &AFS_FS_I(d_inode(root))->flags))
197 seq_puts(m, ",autocell");
198 switch (as->flock_mode) {
199 case afs_flock_mode_unset: break;
200 case afs_flock_mode_local: p = "local"; break;
201 case afs_flock_mode_openafs: p = "openafs"; break;
202 case afs_flock_mode_strict: p = "strict"; break;
203 case afs_flock_mode_write: p = "write"; break;
204 }
205 if (p)
206 seq_printf(m, ",flock=%s", p);
207
208 return 0;
209}
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224static int afs_parse_source(struct fs_context *fc, struct fs_parameter *param)
225{
226 struct afs_fs_context *ctx = fc->fs_private;
227 struct afs_cell *cell;
228 const char *cellname, *suffix, *name = param->string;
229 int cellnamesz;
230
231 _enter(",%s", name);
232
233 if (!name) {
234 printk(KERN_ERR "kAFS: no volume name specified\n");
235 return -EINVAL;
236 }
237
238 if ((name[0] != '%' && name[0] != '#') || !name[1]) {
239
240 if (strcmp(name, "none") == 0) {
241 ctx->no_cell = true;
242 return 0;
243 }
244 printk(KERN_ERR "kAFS: unparsable volume name\n");
245 return -EINVAL;
246 }
247
248
249 if (name[0] == '%') {
250 ctx->type = AFSVL_RWVOL;
251 ctx->force = true;
252 }
253 name++;
254
255
256 ctx->volname = strchr(name, ':');
257 if (ctx->volname) {
258 cellname = name;
259 cellnamesz = ctx->volname - name;
260 ctx->volname++;
261 } else {
262 ctx->volname = name;
263 cellname = NULL;
264 cellnamesz = 0;
265 }
266
267
268 suffix = strrchr(ctx->volname, '.');
269 if (suffix) {
270 if (strcmp(suffix, ".readonly") == 0) {
271 ctx->type = AFSVL_ROVOL;
272 ctx->force = true;
273 } else if (strcmp(suffix, ".backup") == 0) {
274 ctx->type = AFSVL_BACKVOL;
275 ctx->force = true;
276 } else if (suffix[1] == 0) {
277 } else {
278 suffix = NULL;
279 }
280 }
281
282 ctx->volnamesz = suffix ?
283 suffix - ctx->volname : strlen(ctx->volname);
284
285 _debug("cell %*.*s [%p]",
286 cellnamesz, cellnamesz, cellname ?: "", ctx->cell);
287
288
289 if (cellname) {
290 cell = afs_lookup_cell(ctx->net, cellname, cellnamesz,
291 NULL, false);
292 if (IS_ERR(cell)) {
293 pr_err("kAFS: unable to lookup cell '%*.*s'\n",
294 cellnamesz, cellnamesz, cellname ?: "");
295 return PTR_ERR(cell);
296 }
297 afs_put_cell(ctx->net, ctx->cell);
298 ctx->cell = cell;
299 }
300
301 _debug("CELL:%s [%p] VOLUME:%*.*s SUFFIX:%s TYPE:%d%s",
302 ctx->cell->name, ctx->cell,
303 ctx->volnamesz, ctx->volnamesz, ctx->volname,
304 suffix ?: "-", ctx->type, ctx->force ? " FORCE" : "");
305
306 fc->source = param->string;
307 param->string = NULL;
308 return 0;
309}
310
311
312
313
314static int afs_parse_param(struct fs_context *fc, struct fs_parameter *param)
315{
316 struct fs_parse_result result;
317 struct afs_fs_context *ctx = fc->fs_private;
318 int opt;
319
320 opt = fs_parse(fc, afs_fs_parameters, param, &result);
321 if (opt < 0)
322 return opt;
323
324 switch (opt) {
325 case Opt_source:
326 return afs_parse_source(fc, param);
327
328 case Opt_autocell:
329 ctx->autocell = true;
330 break;
331
332 case Opt_dyn:
333 ctx->dyn_root = true;
334 break;
335
336 case Opt_flock:
337 ctx->flock_mode = result.uint_32;
338 break;
339
340 default:
341 return -EINVAL;
342 }
343
344 _leave(" = 0");
345 return 0;
346}
347
348
349
350
351static int afs_validate_fc(struct fs_context *fc)
352{
353 struct afs_fs_context *ctx = fc->fs_private;
354 struct afs_volume *volume;
355 struct key *key;
356
357 if (!ctx->dyn_root) {
358 if (ctx->no_cell) {
359 pr_warn("kAFS: Can only specify source 'none' with -o dyn\n");
360 return -EINVAL;
361 }
362
363 if (!ctx->cell) {
364 pr_warn("kAFS: No cell specified\n");
365 return -EDESTADDRREQ;
366 }
367
368
369 key = afs_request_key(ctx->cell);
370 if (IS_ERR(key))
371 return PTR_ERR(key);
372
373 ctx->key = key;
374
375 if (ctx->volume) {
376 afs_put_volume(ctx->cell, ctx->volume);
377 ctx->volume = NULL;
378 }
379
380 volume = afs_create_volume(ctx);
381 if (IS_ERR(volume))
382 return PTR_ERR(volume);
383
384 ctx->volume = volume;
385 }
386
387 return 0;
388}
389
390
391
392
393static int afs_test_super(struct super_block *sb, struct fs_context *fc)
394{
395 struct afs_fs_context *ctx = fc->fs_private;
396 struct afs_super_info *as = AFS_FS_S(sb);
397
398 return (as->net_ns == fc->net_ns &&
399 as->volume &&
400 as->volume->vid == ctx->volume->vid &&
401 as->cell == ctx->cell &&
402 !as->dyn_root);
403}
404
405static int afs_dynroot_test_super(struct super_block *sb, struct fs_context *fc)
406{
407 struct afs_super_info *as = AFS_FS_S(sb);
408
409 return (as->net_ns == fc->net_ns &&
410 as->dyn_root);
411}
412
413static int afs_set_super(struct super_block *sb, struct fs_context *fc)
414{
415 return set_anon_super(sb, NULL);
416}
417
418
419
420
421static int afs_fill_super(struct super_block *sb, struct afs_fs_context *ctx)
422{
423 struct afs_super_info *as = AFS_FS_S(sb);
424 struct afs_iget_data iget_data;
425 struct inode *inode = NULL;
426 int ret;
427
428 _enter("");
429
430
431 sb->s_blocksize = PAGE_SIZE;
432 sb->s_blocksize_bits = PAGE_SHIFT;
433 sb->s_maxbytes = MAX_LFS_FILESIZE;
434 sb->s_magic = AFS_FS_MAGIC;
435 sb->s_op = &afs_super_ops;
436 if (!as->dyn_root)
437 sb->s_xattr = afs_xattr_handlers;
438 ret = super_setup_bdi(sb);
439 if (ret)
440 return ret;
441 sb->s_bdi->ra_pages = VM_READAHEAD_PAGES;
442
443
444 if (as->dyn_root) {
445 inode = afs_iget_pseudo_dir(sb, true);
446 } else {
447 sprintf(sb->s_id, "%llu", as->volume->vid);
448 afs_activate_volume(as->volume);
449 iget_data.fid.vid = as->volume->vid;
450 iget_data.fid.vnode = 1;
451 iget_data.fid.vnode_hi = 0;
452 iget_data.fid.unique = 1;
453 iget_data.cb_v_break = as->volume->cb_v_break;
454 iget_data.cb_s_break = 0;
455 inode = afs_iget(sb, ctx->key, &iget_data, NULL, NULL, NULL);
456 }
457
458 if (IS_ERR(inode))
459 return PTR_ERR(inode);
460
461 if (ctx->autocell || as->dyn_root)
462 set_bit(AFS_VNODE_AUTOCELL, &AFS_FS_I(inode)->flags);
463
464 ret = -ENOMEM;
465 sb->s_root = d_make_root(inode);
466 if (!sb->s_root)
467 goto error;
468
469 if (as->dyn_root) {
470 sb->s_d_op = &afs_dynroot_dentry_operations;
471 ret = afs_dynroot_populate(sb);
472 if (ret < 0)
473 goto error;
474 } else {
475 sb->s_d_op = &afs_fs_dentry_operations;
476 }
477
478 _leave(" = 0");
479 return 0;
480
481error:
482 _leave(" = %d", ret);
483 return ret;
484}
485
486static struct afs_super_info *afs_alloc_sbi(struct fs_context *fc)
487{
488 struct afs_fs_context *ctx = fc->fs_private;
489 struct afs_super_info *as;
490
491 as = kzalloc(sizeof(struct afs_super_info), GFP_KERNEL);
492 if (as) {
493 as->net_ns = get_net(fc->net_ns);
494 as->flock_mode = ctx->flock_mode;
495 if (ctx->dyn_root) {
496 as->dyn_root = true;
497 } else {
498 as->cell = afs_get_cell(ctx->cell);
499 as->volume = __afs_get_volume(ctx->volume);
500 }
501 }
502 return as;
503}
504
505static void afs_destroy_sbi(struct afs_super_info *as)
506{
507 if (as) {
508 afs_put_volume(as->cell, as->volume);
509 afs_put_cell(afs_net(as->net_ns), as->cell);
510 put_net(as->net_ns);
511 kfree(as);
512 }
513}
514
515static void afs_kill_super(struct super_block *sb)
516{
517 struct afs_super_info *as = AFS_FS_S(sb);
518 struct afs_net *net = afs_net(as->net_ns);
519
520 if (as->dyn_root)
521 afs_dynroot_depopulate(sb);
522
523
524
525
526 if (as->volume)
527 afs_clear_callback_interests(net, as->volume->servers);
528 kill_anon_super(sb);
529 if (as->volume)
530 afs_deactivate_volume(as->volume);
531 afs_destroy_sbi(as);
532}
533
534
535
536
537static int afs_get_tree(struct fs_context *fc)
538{
539 struct afs_fs_context *ctx = fc->fs_private;
540 struct super_block *sb;
541 struct afs_super_info *as;
542 int ret;
543
544 ret = afs_validate_fc(fc);
545 if (ret)
546 goto error;
547
548 _enter("");
549
550
551 ret = -ENOMEM;
552 as = afs_alloc_sbi(fc);
553 if (!as)
554 goto error;
555 fc->s_fs_info = as;
556
557
558 sb = sget_fc(fc,
559 as->dyn_root ? afs_dynroot_test_super : afs_test_super,
560 afs_set_super);
561 if (IS_ERR(sb)) {
562 ret = PTR_ERR(sb);
563 goto error;
564 }
565
566 if (!sb->s_root) {
567
568 _debug("create");
569 ret = afs_fill_super(sb, ctx);
570 if (ret < 0)
571 goto error_sb;
572 sb->s_flags |= SB_ACTIVE;
573 } else {
574 _debug("reuse");
575 ASSERTCMP(sb->s_flags, &, SB_ACTIVE);
576 }
577
578 fc->root = dget(sb->s_root);
579 trace_afs_get_tree(as->cell, as->volume);
580 _leave(" = 0 [%p]", sb);
581 return 0;
582
583error_sb:
584 deactivate_locked_super(sb);
585error:
586 _leave(" = %d", ret);
587 return ret;
588}
589
590static void afs_free_fc(struct fs_context *fc)
591{
592 struct afs_fs_context *ctx = fc->fs_private;
593
594 afs_destroy_sbi(fc->s_fs_info);
595 afs_put_volume(ctx->cell, ctx->volume);
596 afs_put_cell(ctx->net, ctx->cell);
597 key_put(ctx->key);
598 kfree(ctx);
599}
600
601static const struct fs_context_operations afs_context_ops = {
602 .free = afs_free_fc,
603 .parse_param = afs_parse_param,
604 .get_tree = afs_get_tree,
605};
606
607
608
609
610static int afs_init_fs_context(struct fs_context *fc)
611{
612 struct afs_fs_context *ctx;
613 struct afs_cell *cell;
614
615 ctx = kzalloc(sizeof(struct afs_fs_context), GFP_KERNEL);
616 if (!ctx)
617 return -ENOMEM;
618
619 ctx->type = AFSVL_ROVOL;
620 ctx->net = afs_net(fc->net_ns);
621
622
623 rcu_read_lock();
624 cell = afs_lookup_cell_rcu(ctx->net, NULL, 0);
625 rcu_read_unlock();
626 if (IS_ERR(cell))
627 cell = NULL;
628 ctx->cell = cell;
629
630 fc->fs_private = ctx;
631 fc->ops = &afs_context_ops;
632 return 0;
633}
634
635
636
637
638
639
640static void afs_i_init_once(void *_vnode)
641{
642 struct afs_vnode *vnode = _vnode;
643
644 memset(vnode, 0, sizeof(*vnode));
645 inode_init_once(&vnode->vfs_inode);
646 mutex_init(&vnode->io_lock);
647 init_rwsem(&vnode->validate_lock);
648 spin_lock_init(&vnode->wb_lock);
649 spin_lock_init(&vnode->lock);
650 INIT_LIST_HEAD(&vnode->wb_keys);
651 INIT_LIST_HEAD(&vnode->pending_locks);
652 INIT_LIST_HEAD(&vnode->granted_locks);
653 INIT_DELAYED_WORK(&vnode->lock_work, afs_lock_work);
654 seqlock_init(&vnode->cb_lock);
655}
656
657
658
659
660static struct inode *afs_alloc_inode(struct super_block *sb)
661{
662 struct afs_vnode *vnode;
663
664 vnode = kmem_cache_alloc(afs_inode_cachep, GFP_KERNEL);
665 if (!vnode)
666 return NULL;
667
668 atomic_inc(&afs_count_active_inodes);
669
670
671 memset(&vnode->fid, 0, sizeof(vnode->fid));
672 memset(&vnode->status, 0, sizeof(vnode->status));
673
674 vnode->volume = NULL;
675 vnode->lock_key = NULL;
676 vnode->permit_cache = NULL;
677 RCU_INIT_POINTER(vnode->cb_interest, NULL);
678#ifdef CONFIG_AFS_FSCACHE
679 vnode->cache = NULL;
680#endif
681
682 vnode->flags = 1 << AFS_VNODE_UNSET;
683 vnode->lock_state = AFS_VNODE_LOCK_NONE;
684
685 init_rwsem(&vnode->rmdir_lock);
686
687 _leave(" = %p", &vnode->vfs_inode);
688 return &vnode->vfs_inode;
689}
690
691static void afs_free_inode(struct inode *inode)
692{
693 kmem_cache_free(afs_inode_cachep, AFS_FS_I(inode));
694}
695
696
697
698
699static void afs_destroy_inode(struct inode *inode)
700{
701 struct afs_vnode *vnode = AFS_FS_I(inode);
702
703 _enter("%p{%llx:%llu}", inode, vnode->fid.vid, vnode->fid.vnode);
704
705 _debug("DESTROY INODE %p", inode);
706
707 ASSERTCMP(rcu_access_pointer(vnode->cb_interest), ==, NULL);
708
709 atomic_dec(&afs_count_active_inodes);
710}
711
712
713
714
715static int afs_statfs(struct dentry *dentry, struct kstatfs *buf)
716{
717 struct afs_super_info *as = AFS_FS_S(dentry->d_sb);
718 struct afs_fs_cursor fc;
719 struct afs_volume_status vs;
720 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
721 struct key *key;
722 int ret;
723
724 buf->f_type = dentry->d_sb->s_magic;
725 buf->f_bsize = AFS_BLOCK_SIZE;
726 buf->f_namelen = AFSNAMEMAX - 1;
727
728 if (as->dyn_root) {
729 buf->f_blocks = 1;
730 buf->f_bavail = 0;
731 buf->f_bfree = 0;
732 return 0;
733 }
734
735 key = afs_request_key(vnode->volume->cell);
736 if (IS_ERR(key))
737 return PTR_ERR(key);
738
739 ret = -ERESTARTSYS;
740 if (afs_begin_vnode_operation(&fc, vnode, key, true)) {
741 fc.flags |= AFS_FS_CURSOR_NO_VSLEEP;
742 while (afs_select_fileserver(&fc)) {
743 fc.cb_break = afs_calc_vnode_cb_break(vnode);
744 afs_fs_get_volume_status(&fc, &vs);
745 }
746
747 afs_check_for_remote_deletion(&fc, fc.vnode);
748 ret = afs_end_vnode_operation(&fc);
749 }
750
751 key_put(key);
752
753 if (ret == 0) {
754 if (vs.max_quota == 0)
755 buf->f_blocks = vs.part_max_blocks;
756 else
757 buf->f_blocks = vs.max_quota;
758 buf->f_bavail = buf->f_bfree = buf->f_blocks - vs.blocks_in_use;
759 }
760
761 return ret;
762}
763