1ec26815aSDavid Howells /* AFS superblock handling
2ec26815aSDavid Howells *
313fcc683SDavid Howells * Copyright (c) 2002, 2007, 2018 Red Hat, Inc. All rights reserved.
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * This software may be freely redistributed under the terms of the
61da177e4SLinus Torvalds * GNU General Public License.
71da177e4SLinus Torvalds *
81da177e4SLinus Torvalds * You should have received a copy of the GNU General Public License
91da177e4SLinus Torvalds * along with this program; if not, write to the Free Software
101da177e4SLinus Torvalds * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
111da177e4SLinus Torvalds *
121da177e4SLinus Torvalds * Authors: David Howells <dhowells@redhat.com>
1344d1b980SDavid Woodhouse * David Woodhouse <dwmw2@infradead.org>
141da177e4SLinus Torvalds *
151da177e4SLinus Torvalds */
161da177e4SLinus Torvalds
171da177e4SLinus Torvalds #include <linux/kernel.h>
181da177e4SLinus Torvalds #include <linux/module.h>
19bec5eb61Swanglei #include <linux/mount.h>
201da177e4SLinus Torvalds #include <linux/init.h>
211da177e4SLinus Torvalds #include <linux/slab.h>
221da177e4SLinus Torvalds #include <linux/fs.h>
231da177e4SLinus Torvalds #include <linux/pagemap.h>
2413fcc683SDavid Howells #include <linux/fs_parser.h>
2545222b9eSDavid Howells #include <linux/statfs.h>
26e8edc6e0SAlexey Dobriyan #include <linux/sched.h>
27f74f70f8SEric W. Biederman #include <linux/nsproxy.h>
28f044c884SDavid Howells #include <linux/magic.h>
29f74f70f8SEric W. Biederman #include <net/net_namespace.h>
301da177e4SLinus Torvalds #include "internal.h"
311da177e4SLinus Torvalds
3251cc5068SAlexey Dobriyan static void afs_i_init_once(void *foo);
33dde194a6SAl Viro static void afs_kill_super(struct super_block *sb);
341da177e4SLinus Torvalds static struct inode *afs_alloc_inode(struct super_block *sb);
351da177e4SLinus Torvalds static void afs_destroy_inode(struct inode *inode);
3651b9fe48SAl Viro static void afs_free_inode(struct inode *inode);
3745222b9eSDavid Howells static int afs_statfs(struct dentry *dentry, struct kstatfs *buf);
38677018a6SDavid Howells static int afs_show_devname(struct seq_file *m, struct dentry *root);
39677018a6SDavid Howells static int afs_show_options(struct seq_file *m, struct dentry *root);
4013fcc683SDavid Howells static int afs_init_fs_context(struct fs_context *fc);
41d7167b14SAl Viro static const struct fs_parameter_spec afs_fs_parameters[];
421da177e4SLinus Torvalds
431f5ce9e9STrond Myklebust struct file_system_type afs_fs_type = {
441da177e4SLinus Torvalds .owner = THIS_MODULE,
451da177e4SLinus Torvalds .name = "afs",
4613fcc683SDavid Howells .init_fs_context = afs_init_fs_context,
47d7167b14SAl Viro .parameters = afs_fs_parameters,
48dde194a6SAl Viro .kill_sb = afs_kill_super,
4979ddbfa5SDavid Howells .fs_flags = FS_RENAME_DOES_D_MOVE,
501da177e4SLinus Torvalds };
517f78e035SEric W. Biederman MODULE_ALIAS_FS("afs");
521da177e4SLinus Torvalds
535b86d4ffSDavid Howells int afs_net_id;
545b86d4ffSDavid Howells
55ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations afs_super_ops = {
5645222b9eSDavid Howells .statfs = afs_statfs,
571da177e4SLinus Torvalds .alloc_inode = afs_alloc_inode,
58c7f75ef3SDavid Howells .write_inode = afs_write_inode,
59bec5eb61Swanglei .drop_inode = afs_drop_inode,
601da177e4SLinus Torvalds .destroy_inode = afs_destroy_inode,
6151b9fe48SAl Viro .free_inode = afs_free_inode,
62b57922d9SAl Viro .evict_inode = afs_evict_inode,
63677018a6SDavid Howells .show_devname = afs_show_devname,
64677018a6SDavid Howells .show_options = afs_show_options,
651da177e4SLinus Torvalds };
661da177e4SLinus Torvalds
67e18b890bSChristoph Lameter static struct kmem_cache *afs_inode_cachep;
681da177e4SLinus Torvalds static atomic_t afs_count_active_inodes;
691da177e4SLinus Torvalds
7013fcc683SDavid Howells enum afs_param {
7113fcc683SDavid Howells Opt_autocell,
7213fcc683SDavid Howells Opt_dyn,
736c6c1d63SDavid Howells Opt_flock,
7413fcc683SDavid Howells Opt_source,
7580c72fe4SDavid Howells };
7680c72fe4SDavid Howells
775eede625SAl Viro static const struct constant_table afs_param_flock[] = {
782710c957SAl Viro {"local", afs_flock_mode_local },
792710c957SAl Viro {"openafs", afs_flock_mode_openafs },
802710c957SAl Viro {"strict", afs_flock_mode_strict },
812710c957SAl Viro {"write", afs_flock_mode_write },
8213fcc683SDavid Howells {}
8313fcc683SDavid Howells };
8413fcc683SDavid Howells
85d7167b14SAl Viro static const struct fs_parameter_spec afs_fs_parameters[] = {
862710c957SAl Viro fsparam_flag ("autocell", Opt_autocell),
872710c957SAl Viro fsparam_flag ("dyn", Opt_dyn),
882710c957SAl Viro fsparam_enum ("flock", Opt_flock, afs_param_flock),
892710c957SAl Viro fsparam_string("source", Opt_source),
906c6c1d63SDavid Howells {}
916c6c1d63SDavid Howells };
926c6c1d63SDavid Howells
931da177e4SLinus Torvalds /*
941da177e4SLinus Torvalds * initialise the filesystem
951da177e4SLinus Torvalds */
afs_fs_init(void)961da177e4SLinus Torvalds int __init afs_fs_init(void)
971da177e4SLinus Torvalds {
981da177e4SLinus Torvalds int ret;
991da177e4SLinus Torvalds
1001da177e4SLinus Torvalds _enter("");
1011da177e4SLinus Torvalds
1021da177e4SLinus Torvalds /* create ourselves an inode cache */
1031da177e4SLinus Torvalds atomic_set(&afs_count_active_inodes, 0);
1041da177e4SLinus Torvalds
1051da177e4SLinus Torvalds ret = -ENOMEM;
1061da177e4SLinus Torvalds afs_inode_cachep = kmem_cache_create("afs_inode_cache",
1071da177e4SLinus Torvalds sizeof(struct afs_vnode),
1081da177e4SLinus Torvalds 0,
1095d097056SVladimir Davydov SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT,
11020c2df83SPaul Mundt afs_i_init_once);
1111da177e4SLinus Torvalds if (!afs_inode_cachep) {
1121da177e4SLinus Torvalds printk(KERN_NOTICE "kAFS: Failed to allocate inode cache\n");
1131da177e4SLinus Torvalds return ret;
1141da177e4SLinus Torvalds }
1151da177e4SLinus Torvalds
1161da177e4SLinus Torvalds /* now export our filesystem to lesser mortals */
1171da177e4SLinus Torvalds ret = register_filesystem(&afs_fs_type);
1181da177e4SLinus Torvalds if (ret < 0) {
1191da177e4SLinus Torvalds kmem_cache_destroy(afs_inode_cachep);
12008e0e7c8SDavid Howells _leave(" = %d", ret);
1211da177e4SLinus Torvalds return ret;
1221da177e4SLinus Torvalds }
1231da177e4SLinus Torvalds
12408e0e7c8SDavid Howells _leave(" = 0");
1251da177e4SLinus Torvalds return 0;
126ec26815aSDavid Howells }
1271da177e4SLinus Torvalds
1281da177e4SLinus Torvalds /*
1291da177e4SLinus Torvalds * clean up the filesystem
1301da177e4SLinus Torvalds */
afs_fs_exit(void)1315b86d4ffSDavid Howells void afs_fs_exit(void)
1321da177e4SLinus Torvalds {
13308e0e7c8SDavid Howells _enter("");
13408e0e7c8SDavid Howells
13508e0e7c8SDavid Howells afs_mntpt_kill_timer();
1361da177e4SLinus Torvalds unregister_filesystem(&afs_fs_type);
1371da177e4SLinus Torvalds
1381da177e4SLinus Torvalds if (atomic_read(&afs_count_active_inodes) != 0) {
1391da177e4SLinus Torvalds printk("kAFS: %d active inode objects still present\n",
1401da177e4SLinus Torvalds atomic_read(&afs_count_active_inodes));
1411da177e4SLinus Torvalds BUG();
1421da177e4SLinus Torvalds }
1431da177e4SLinus Torvalds
1448c0a8537SKirill A. Shutemov /*
1458c0a8537SKirill A. Shutemov * Make sure all delayed rcu free inodes are flushed before we
1468c0a8537SKirill A. Shutemov * destroy cache.
1478c0a8537SKirill A. Shutemov */
1488c0a8537SKirill A. Shutemov rcu_barrier();
1491da177e4SLinus Torvalds kmem_cache_destroy(afs_inode_cachep);
15008e0e7c8SDavid Howells _leave("");
151ec26815aSDavid Howells }
1521da177e4SLinus Torvalds
1531da177e4SLinus Torvalds /*
154677018a6SDavid Howells * Display the mount device name in /proc/mounts.
155677018a6SDavid Howells */
afs_show_devname(struct seq_file * m,struct dentry * root)156677018a6SDavid Howells static int afs_show_devname(struct seq_file *m, struct dentry *root)
157677018a6SDavid Howells {
158d2ddc776SDavid Howells struct afs_super_info *as = AFS_FS_S(root->d_sb);
159677018a6SDavid Howells struct afs_volume *volume = as->volume;
160d2ddc776SDavid Howells struct afs_cell *cell = as->cell;
161677018a6SDavid Howells const char *suf = "";
162677018a6SDavid Howells char pref = '%';
163677018a6SDavid Howells
1644d673da1SDavid Howells if (as->dyn_root) {
1654d673da1SDavid Howells seq_puts(m, "none");
1664d673da1SDavid Howells return 0;
1674d673da1SDavid Howells }
1684d673da1SDavid Howells
169677018a6SDavid Howells switch (volume->type) {
170677018a6SDavid Howells case AFSVL_RWVOL:
171677018a6SDavid Howells break;
172677018a6SDavid Howells case AFSVL_ROVOL:
173677018a6SDavid Howells pref = '#';
174677018a6SDavid Howells if (volume->type_force)
175677018a6SDavid Howells suf = ".readonly";
176677018a6SDavid Howells break;
177677018a6SDavid Howells case AFSVL_BACKVOL:
178677018a6SDavid Howells pref = '#';
179677018a6SDavid Howells suf = ".backup";
180677018a6SDavid Howells break;
181677018a6SDavid Howells }
182677018a6SDavid Howells
183d2ddc776SDavid Howells seq_printf(m, "%c%s:%s%s", pref, cell->name, volume->name, suf);
184677018a6SDavid Howells return 0;
185677018a6SDavid Howells }
186677018a6SDavid Howells
187677018a6SDavid Howells /*
188677018a6SDavid Howells * Display the mount options in /proc/mounts.
189677018a6SDavid Howells */
afs_show_options(struct seq_file * m,struct dentry * root)190677018a6SDavid Howells static int afs_show_options(struct seq_file *m, struct dentry *root)
191677018a6SDavid Howells {
1924d673da1SDavid Howells struct afs_super_info *as = AFS_FS_S(root->d_sb);
1936c6c1d63SDavid Howells const char *p = NULL;
1944d673da1SDavid Howells
1954d673da1SDavid Howells if (as->dyn_root)
1964d673da1SDavid Howells seq_puts(m, ",dyn");
197677018a6SDavid Howells if (test_bit(AFS_VNODE_AUTOCELL, &AFS_FS_I(d_inode(root))->flags))
1984d673da1SDavid Howells seq_puts(m, ",autocell");
1996c6c1d63SDavid Howells switch (as->flock_mode) {
2006c6c1d63SDavid Howells case afs_flock_mode_unset: break;
2016c6c1d63SDavid Howells case afs_flock_mode_local: p = "local"; break;
2026c6c1d63SDavid Howells case afs_flock_mode_openafs: p = "openafs"; break;
2036c6c1d63SDavid Howells case afs_flock_mode_strict: p = "strict"; break;
2046c6c1d63SDavid Howells case afs_flock_mode_write: p = "write"; break;
2056c6c1d63SDavid Howells }
2066c6c1d63SDavid Howells if (p)
2076c6c1d63SDavid Howells seq_printf(m, ",flock=%s", p);
2086c6c1d63SDavid Howells
209677018a6SDavid Howells return 0;
210677018a6SDavid Howells }
211677018a6SDavid Howells
212677018a6SDavid Howells /*
21313fcc683SDavid Howells * Parse the source name to get cell name, volume name, volume type and R/W
21413fcc683SDavid Howells * selector.
21513fcc683SDavid Howells *
21613fcc683SDavid Howells * This can be one of the following:
21700d3b7a4SDavid Howells * "%[cell:]volume[.]" R/W volume
218c99c2171SDavid Howells * "#[cell:]volume[.]" R/O or R/W volume (R/O parent),
219c99c2171SDavid Howells * or R/W (R/W parent) volume
22000d3b7a4SDavid Howells * "%[cell:]volume.readonly" R/O volume
22100d3b7a4SDavid Howells * "#[cell:]volume.readonly" R/O volume
22200d3b7a4SDavid Howells * "%[cell:]volume.backup" Backup volume
22300d3b7a4SDavid Howells * "#[cell:]volume.backup" Backup volume
22400d3b7a4SDavid Howells */
afs_parse_source(struct fs_context * fc,struct fs_parameter * param)22513fcc683SDavid Howells static int afs_parse_source(struct fs_context *fc, struct fs_parameter *param)
22600d3b7a4SDavid Howells {
22713fcc683SDavid Howells struct afs_fs_context *ctx = fc->fs_private;
22800d3b7a4SDavid Howells struct afs_cell *cell;
22913fcc683SDavid Howells const char *cellname, *suffix, *name = param->string;
23000d3b7a4SDavid Howells int cellnamesz;
23100d3b7a4SDavid Howells
23200d3b7a4SDavid Howells _enter(",%s", name);
23300d3b7a4SDavid Howells
2344cb68296SDavid Howells if (fc->source)
2354cb68296SDavid Howells return invalf(fc, "kAFS: Multiple sources not supported");
2364cb68296SDavid Howells
23700d3b7a4SDavid Howells if (!name) {
23800d3b7a4SDavid Howells printk(KERN_ERR "kAFS: no volume name specified\n");
23900d3b7a4SDavid Howells return -EINVAL;
24000d3b7a4SDavid Howells }
24100d3b7a4SDavid Howells
24200d3b7a4SDavid Howells if ((name[0] != '%' && name[0] != '#') || !name[1]) {
24313fcc683SDavid Howells /* To use dynroot, we don't want to have to provide a source */
24413fcc683SDavid Howells if (strcmp(name, "none") == 0) {
24513fcc683SDavid Howells ctx->no_cell = true;
24613fcc683SDavid Howells return 0;
24713fcc683SDavid Howells }
24800d3b7a4SDavid Howells printk(KERN_ERR "kAFS: unparsable volume name\n");
24900d3b7a4SDavid Howells return -EINVAL;
25000d3b7a4SDavid Howells }
25100d3b7a4SDavid Howells
25200d3b7a4SDavid Howells /* determine the type of volume we're looking for */
253c99c2171SDavid Howells if (name[0] == '%') {
25413fcc683SDavid Howells ctx->type = AFSVL_RWVOL;
25513fcc683SDavid Howells ctx->force = true;
25600d3b7a4SDavid Howells }
25700d3b7a4SDavid Howells name++;
25800d3b7a4SDavid Howells
25900d3b7a4SDavid Howells /* split the cell name out if there is one */
26013fcc683SDavid Howells ctx->volname = strchr(name, ':');
26113fcc683SDavid Howells if (ctx->volname) {
26200d3b7a4SDavid Howells cellname = name;
26313fcc683SDavid Howells cellnamesz = ctx->volname - name;
26413fcc683SDavid Howells ctx->volname++;
26500d3b7a4SDavid Howells } else {
26613fcc683SDavid Howells ctx->volname = name;
26700d3b7a4SDavid Howells cellname = NULL;
26800d3b7a4SDavid Howells cellnamesz = 0;
26900d3b7a4SDavid Howells }
27000d3b7a4SDavid Howells
27100d3b7a4SDavid Howells /* the volume type is further affected by a possible suffix */
27213fcc683SDavid Howells suffix = strrchr(ctx->volname, '.');
27300d3b7a4SDavid Howells if (suffix) {
27400d3b7a4SDavid Howells if (strcmp(suffix, ".readonly") == 0) {
27513fcc683SDavid Howells ctx->type = AFSVL_ROVOL;
27613fcc683SDavid Howells ctx->force = true;
27700d3b7a4SDavid Howells } else if (strcmp(suffix, ".backup") == 0) {
27813fcc683SDavid Howells ctx->type = AFSVL_BACKVOL;
27913fcc683SDavid Howells ctx->force = true;
28000d3b7a4SDavid Howells } else if (suffix[1] == 0) {
28100d3b7a4SDavid Howells } else {
28200d3b7a4SDavid Howells suffix = NULL;
28300d3b7a4SDavid Howells }
28400d3b7a4SDavid Howells }
28500d3b7a4SDavid Howells
28613fcc683SDavid Howells ctx->volnamesz = suffix ?
28713fcc683SDavid Howells suffix - ctx->volname : strlen(ctx->volname);
28800d3b7a4SDavid Howells
28900d3b7a4SDavid Howells _debug("cell %*.*s [%p]",
29013fcc683SDavid Howells cellnamesz, cellnamesz, cellname ?: "", ctx->cell);
29100d3b7a4SDavid Howells
29200d3b7a4SDavid Howells /* lookup the cell record */
29313fcc683SDavid Howells if (cellname) {
29413fcc683SDavid Howells cell = afs_lookup_cell(ctx->net, cellname, cellnamesz,
295989782dcSDavid Howells NULL, false);
29600d3b7a4SDavid Howells if (IS_ERR(cell)) {
29713fcc683SDavid Howells pr_err("kAFS: unable to lookup cell '%*.*s'\n",
298bec5eb61Swanglei cellnamesz, cellnamesz, cellname ?: "");
29900d3b7a4SDavid Howells return PTR_ERR(cell);
30000d3b7a4SDavid Howells }
301dca54a7bSDavid Howells afs_unuse_cell(ctx->net, ctx->cell, afs_cell_trace_unuse_parse);
302dca54a7bSDavid Howells afs_see_cell(cell, afs_cell_trace_see_source);
30313fcc683SDavid Howells ctx->cell = cell;
30400d3b7a4SDavid Howells }
30500d3b7a4SDavid Howells
30600d3b7a4SDavid Howells _debug("CELL:%s [%p] VOLUME:%*.*s SUFFIX:%s TYPE:%d%s",
30713fcc683SDavid Howells ctx->cell->name, ctx->cell,
30813fcc683SDavid Howells ctx->volnamesz, ctx->volnamesz, ctx->volname,
30913fcc683SDavid Howells suffix ?: "-", ctx->type, ctx->force ? " FORCE" : "");
31013fcc683SDavid Howells
31113fcc683SDavid Howells fc->source = param->string;
31213fcc683SDavid Howells param->string = NULL;
31313fcc683SDavid Howells return 0;
31413fcc683SDavid Howells }
31513fcc683SDavid Howells
31613fcc683SDavid Howells /*
31713fcc683SDavid Howells * Parse a single mount parameter.
31813fcc683SDavid Howells */
afs_parse_param(struct fs_context * fc,struct fs_parameter * param)31913fcc683SDavid Howells static int afs_parse_param(struct fs_context *fc, struct fs_parameter *param)
32013fcc683SDavid Howells {
32113fcc683SDavid Howells struct fs_parse_result result;
32213fcc683SDavid Howells struct afs_fs_context *ctx = fc->fs_private;
32313fcc683SDavid Howells int opt;
32413fcc683SDavid Howells
325d7167b14SAl Viro opt = fs_parse(fc, afs_fs_parameters, param, &result);
32613fcc683SDavid Howells if (opt < 0)
32713fcc683SDavid Howells return opt;
32813fcc683SDavid Howells
32913fcc683SDavid Howells switch (opt) {
33013fcc683SDavid Howells case Opt_source:
33113fcc683SDavid Howells return afs_parse_source(fc, param);
33213fcc683SDavid Howells
33313fcc683SDavid Howells case Opt_autocell:
33413fcc683SDavid Howells ctx->autocell = true;
33513fcc683SDavid Howells break;
33613fcc683SDavid Howells
33713fcc683SDavid Howells case Opt_dyn:
33813fcc683SDavid Howells ctx->dyn_root = true;
33913fcc683SDavid Howells break;
34013fcc683SDavid Howells
3416c6c1d63SDavid Howells case Opt_flock:
3426c6c1d63SDavid Howells ctx->flock_mode = result.uint_32;
3436c6c1d63SDavid Howells break;
3446c6c1d63SDavid Howells
34513fcc683SDavid Howells default:
34613fcc683SDavid Howells return -EINVAL;
34713fcc683SDavid Howells }
34813fcc683SDavid Howells
34913fcc683SDavid Howells _leave(" = 0");
35013fcc683SDavid Howells return 0;
35113fcc683SDavid Howells }
35213fcc683SDavid Howells
35313fcc683SDavid Howells /*
35413fcc683SDavid Howells * Validate the options, get the cell key and look up the volume.
35513fcc683SDavid Howells */
afs_validate_fc(struct fs_context * fc)35613fcc683SDavid Howells static int afs_validate_fc(struct fs_context *fc)
35713fcc683SDavid Howells {
35813fcc683SDavid Howells struct afs_fs_context *ctx = fc->fs_private;
35913fcc683SDavid Howells struct afs_volume *volume;
3608a070a96SDavid Howells struct afs_cell *cell;
36113fcc683SDavid Howells struct key *key;
3628a070a96SDavid Howells int ret;
36313fcc683SDavid Howells
36413fcc683SDavid Howells if (!ctx->dyn_root) {
36513fcc683SDavid Howells if (ctx->no_cell) {
36613fcc683SDavid Howells pr_warn("kAFS: Can only specify source 'none' with -o dyn\n");
36713fcc683SDavid Howells return -EINVAL;
36813fcc683SDavid Howells }
36913fcc683SDavid Howells
37013fcc683SDavid Howells if (!ctx->cell) {
37113fcc683SDavid Howells pr_warn("kAFS: No cell specified\n");
37213fcc683SDavid Howells return -EDESTADDRREQ;
37313fcc683SDavid Howells }
37413fcc683SDavid Howells
3758a070a96SDavid Howells reget_key:
37613fcc683SDavid Howells /* We try to do the mount securely. */
37713fcc683SDavid Howells key = afs_request_key(ctx->cell);
37813fcc683SDavid Howells if (IS_ERR(key))
37913fcc683SDavid Howells return PTR_ERR(key);
38013fcc683SDavid Howells
38113fcc683SDavid Howells ctx->key = key;
38213fcc683SDavid Howells
38313fcc683SDavid Howells if (ctx->volume) {
384cca37d45SDavid Howells afs_put_volume(ctx->net, ctx->volume,
385cca37d45SDavid Howells afs_volume_trace_put_validate_fc);
38613fcc683SDavid Howells ctx->volume = NULL;
38713fcc683SDavid Howells }
38813fcc683SDavid Howells
3898a070a96SDavid Howells if (test_bit(AFS_CELL_FL_CHECK_ALIAS, &ctx->cell->flags)) {
3908a070a96SDavid Howells ret = afs_cell_detect_alias(ctx->cell, key);
3918a070a96SDavid Howells if (ret < 0)
3928a070a96SDavid Howells return ret;
3938a070a96SDavid Howells if (ret == 1) {
3948a070a96SDavid Howells _debug("switch to alias");
3958a070a96SDavid Howells key_put(ctx->key);
3968a070a96SDavid Howells ctx->key = NULL;
397dca54a7bSDavid Howells cell = afs_use_cell(ctx->cell->alias_of,
398dca54a7bSDavid Howells afs_cell_trace_use_fc_alias);
399dca54a7bSDavid Howells afs_unuse_cell(ctx->net, ctx->cell, afs_cell_trace_unuse_fc);
4008a070a96SDavid Howells ctx->cell = cell;
4018a070a96SDavid Howells goto reget_key;
4028a070a96SDavid Howells }
4038a070a96SDavid Howells }
4048a070a96SDavid Howells
40513fcc683SDavid Howells volume = afs_create_volume(ctx);
40613fcc683SDavid Howells if (IS_ERR(volume))
40713fcc683SDavid Howells return PTR_ERR(volume);
40813fcc683SDavid Howells
40913fcc683SDavid Howells ctx->volume = volume;
410*ceca54f1SDavid Howells if (volume->type != AFSVL_RWVOL)
411*ceca54f1SDavid Howells ctx->flock_mode = afs_flock_mode_local;
41213fcc683SDavid Howells }
41300d3b7a4SDavid Howells
41400d3b7a4SDavid Howells return 0;
41500d3b7a4SDavid Howells }
41600d3b7a4SDavid Howells
41700d3b7a4SDavid Howells /*
4181da177e4SLinus Torvalds * check a superblock to see if it's the one we're looking for
4191da177e4SLinus Torvalds */
afs_test_super(struct super_block * sb,struct fs_context * fc)42013fcc683SDavid Howells static int afs_test_super(struct super_block *sb, struct fs_context *fc)
4211da177e4SLinus Torvalds {
42213fcc683SDavid Howells struct afs_fs_context *ctx = fc->fs_private;
423d2ddc776SDavid Howells struct afs_super_info *as = AFS_FS_S(sb);
4241da177e4SLinus Torvalds
42513fcc683SDavid Howells return (as->net_ns == fc->net_ns &&
4264d673da1SDavid Howells as->volume &&
42713fcc683SDavid Howells as->volume->vid == ctx->volume->vid &&
428106bc798SDavid Howells as->cell == ctx->cell &&
4290da0b7fdSDavid Howells !as->dyn_root);
4304d673da1SDavid Howells }
4314d673da1SDavid Howells
afs_dynroot_test_super(struct super_block * sb,struct fs_context * fc)43213fcc683SDavid Howells static int afs_dynroot_test_super(struct super_block *sb, struct fs_context *fc)
4334d673da1SDavid Howells {
4340da0b7fdSDavid Howells struct afs_super_info *as = AFS_FS_S(sb);
4350da0b7fdSDavid Howells
43613fcc683SDavid Howells return (as->net_ns == fc->net_ns &&
4370da0b7fdSDavid Howells as->dyn_root);
438dde194a6SAl Viro }
439dde194a6SAl Viro
afs_set_super(struct super_block * sb,struct fs_context * fc)44013fcc683SDavid Howells static int afs_set_super(struct super_block *sb, struct fs_context *fc)
441dde194a6SAl Viro {
442dde194a6SAl Viro return set_anon_super(sb, NULL);
443ec26815aSDavid Howells }
4441da177e4SLinus Torvalds
4451da177e4SLinus Torvalds /*
4461da177e4SLinus Torvalds * fill in the superblock
4471da177e4SLinus Torvalds */
afs_fill_super(struct super_block * sb,struct afs_fs_context * ctx)44813fcc683SDavid Howells static int afs_fill_super(struct super_block *sb, struct afs_fs_context *ctx)
4491da177e4SLinus Torvalds {
450d2ddc776SDavid Howells struct afs_super_info *as = AFS_FS_S(sb);
4511da177e4SLinus Torvalds struct inode *inode = NULL;
4521da177e4SLinus Torvalds int ret;
4531da177e4SLinus Torvalds
45408e0e7c8SDavid Howells _enter("");
4551da177e4SLinus Torvalds
4561da177e4SLinus Torvalds /* fill in the superblock */
45709cbfeafSKirill A. Shutemov sb->s_blocksize = PAGE_SIZE;
45809cbfeafSKirill A. Shutemov sb->s_blocksize_bits = PAGE_SHIFT;
459b485275fSMarc Dionne sb->s_maxbytes = MAX_LFS_FILESIZE;
4601da177e4SLinus Torvalds sb->s_magic = AFS_FS_MAGIC;
4611da177e4SLinus Torvalds sb->s_op = &afs_super_ops;
4624d673da1SDavid Howells if (!as->dyn_root)
463d3e3b7eaSDavid Howells sb->s_xattr = afs_xattr_handlers;
464edd3ba94SJan Kara ret = super_setup_bdi(sb);
465edd3ba94SJan Kara if (ret)
466edd3ba94SJan Kara return ret;
4671da177e4SLinus Torvalds
4681da177e4SLinus Torvalds /* allocate the root inode and dentry */
4694d673da1SDavid Howells if (as->dyn_root) {
4704d673da1SDavid Howells inode = afs_iget_pseudo_dir(sb, true);
4714d673da1SDavid Howells } else {
4723b6492dfSDavid Howells sprintf(sb->s_id, "%llu", as->volume->vid);
4734d673da1SDavid Howells afs_activate_volume(as->volume);
474e49c7b2fSDavid Howells inode = afs_root_iget(sb, ctx->key);
4754d673da1SDavid Howells }
4764d673da1SDavid Howells
47708e0e7c8SDavid Howells if (IS_ERR(inode))
478dde194a6SAl Viro return PTR_ERR(inode);
4791da177e4SLinus Torvalds
48013fcc683SDavid Howells if (ctx->autocell || as->dyn_root)
481bec5eb61Swanglei set_bit(AFS_VNODE_AUTOCELL, &AFS_FS_I(inode)->flags);
482bec5eb61Swanglei
4831da177e4SLinus Torvalds ret = -ENOMEM;
48448fde701SAl Viro sb->s_root = d_make_root(inode);
48548fde701SAl Viro if (!sb->s_root)
4861da177e4SLinus Torvalds goto error;
4871da177e4SLinus Torvalds
4880da0b7fdSDavid Howells if (as->dyn_root) {
48966c7e1d3SDavid Howells sb->s_d_op = &afs_dynroot_dentry_operations;
4900da0b7fdSDavid Howells ret = afs_dynroot_populate(sb);
4910da0b7fdSDavid Howells if (ret < 0)
4920da0b7fdSDavid Howells goto error;
4930da0b7fdSDavid Howells } else {
494d61dcce2SAl Viro sb->s_d_op = &afs_fs_dentry_operations;
49520325960SDavid Howells rcu_assign_pointer(as->volume->sb, sb);
4960da0b7fdSDavid Howells }
4971da177e4SLinus Torvalds
49808e0e7c8SDavid Howells _leave(" = 0");
4991da177e4SLinus Torvalds return 0;
5001da177e4SLinus Torvalds
5011da177e4SLinus Torvalds error:
50208e0e7c8SDavid Howells _leave(" = %d", ret);
5031da177e4SLinus Torvalds return ret;
504ec26815aSDavid Howells }
5051da177e4SLinus Torvalds
afs_alloc_sbi(struct fs_context * fc)50613fcc683SDavid Howells static struct afs_super_info *afs_alloc_sbi(struct fs_context *fc)
50749566f6fSDavid Howells {
50813fcc683SDavid Howells struct afs_fs_context *ctx = fc->fs_private;
50949566f6fSDavid Howells struct afs_super_info *as;
51049566f6fSDavid Howells
51149566f6fSDavid Howells as = kzalloc(sizeof(struct afs_super_info), GFP_KERNEL);
51249566f6fSDavid Howells if (as) {
51313fcc683SDavid Howells as->net_ns = get_net(fc->net_ns);
5146c6c1d63SDavid Howells as->flock_mode = ctx->flock_mode;
51513fcc683SDavid Howells if (ctx->dyn_root) {
5164d673da1SDavid Howells as->dyn_root = true;
51713fcc683SDavid Howells } else {
518dca54a7bSDavid Howells as->cell = afs_use_cell(ctx->cell, afs_cell_trace_use_sbi);
519cca37d45SDavid Howells as->volume = afs_get_volume(ctx->volume,
520cca37d45SDavid Howells afs_volume_trace_get_alloc_sbi);
52113fcc683SDavid Howells }
52249566f6fSDavid Howells }
52349566f6fSDavid Howells return as;
52449566f6fSDavid Howells }
52549566f6fSDavid Howells
afs_destroy_sbi(struct afs_super_info * as)52649566f6fSDavid Howells static void afs_destroy_sbi(struct afs_super_info *as)
52749566f6fSDavid Howells {
52849566f6fSDavid Howells if (as) {
529e49c7b2fSDavid Howells struct afs_net *net = afs_net(as->net_ns);
530cca37d45SDavid Howells afs_put_volume(net, as->volume, afs_volume_trace_put_destroy_sbi);
531dca54a7bSDavid Howells afs_unuse_cell(net, as->cell, afs_cell_trace_unuse_sbi);
5325b86d4ffSDavid Howells put_net(as->net_ns);
53349566f6fSDavid Howells kfree(as);
53449566f6fSDavid Howells }
53549566f6fSDavid Howells }
53649566f6fSDavid Howells
afs_kill_super(struct super_block * sb)5370da0b7fdSDavid Howells static void afs_kill_super(struct super_block *sb)
5380da0b7fdSDavid Howells {
5390da0b7fdSDavid Howells struct afs_super_info *as = AFS_FS_S(sb);
5400da0b7fdSDavid Howells
5410da0b7fdSDavid Howells if (as->dyn_root)
5420da0b7fdSDavid Howells afs_dynroot_depopulate(sb);
5430da0b7fdSDavid Howells
5440da0b7fdSDavid Howells /* Clear the callback interests (which will do ilookup5) before
5450da0b7fdSDavid Howells * deactivating the superblock.
5460da0b7fdSDavid Howells */
5470da0b7fdSDavid Howells if (as->volume)
54820325960SDavid Howells rcu_assign_pointer(as->volume->sb, NULL);
5490da0b7fdSDavid Howells kill_anon_super(sb);
5500da0b7fdSDavid Howells if (as->volume)
5510da0b7fdSDavid Howells afs_deactivate_volume(as->volume);
5520da0b7fdSDavid Howells afs_destroy_sbi(as);
5530da0b7fdSDavid Howells }
5540da0b7fdSDavid Howells
5551da177e4SLinus Torvalds /*
55613fcc683SDavid Howells * Get an AFS superblock and root directory.
5571da177e4SLinus Torvalds */
afs_get_tree(struct fs_context * fc)55813fcc683SDavid Howells static int afs_get_tree(struct fs_context *fc)
5591da177e4SLinus Torvalds {
56013fcc683SDavid Howells struct afs_fs_context *ctx = fc->fs_private;
5611da177e4SLinus Torvalds struct super_block *sb;
562dde194a6SAl Viro struct afs_super_info *as;
5631da177e4SLinus Torvalds int ret;
5641da177e4SLinus Torvalds
56513fcc683SDavid Howells ret = afs_validate_fc(fc);
56613fcc683SDavid Howells if (ret)
56700d3b7a4SDavid Howells goto error;
56800d3b7a4SDavid Howells
56913fcc683SDavid Howells _enter("");
57000d3b7a4SDavid Howells
57149566f6fSDavid Howells /* allocate a superblock info record */
57249566f6fSDavid Howells ret = -ENOMEM;
57313fcc683SDavid Howells as = afs_alloc_sbi(fc);
57449566f6fSDavid Howells if (!as)
57513fcc683SDavid Howells goto error;
57613fcc683SDavid Howells fc->s_fs_info = as;
5771da177e4SLinus Torvalds
5781da177e4SLinus Torvalds /* allocate a deviceless superblock */
57913fcc683SDavid Howells sb = sget_fc(fc,
5804d673da1SDavid Howells as->dyn_root ? afs_dynroot_test_super : afs_test_super,
58113fcc683SDavid Howells afs_set_super);
58208e0e7c8SDavid Howells if (IS_ERR(sb)) {
58308e0e7c8SDavid Howells ret = PTR_ERR(sb);
58413fcc683SDavid Howells goto error;
58508e0e7c8SDavid Howells }
5861da177e4SLinus Torvalds
587436058a4SDavid Howells if (!sb->s_root) {
588436058a4SDavid Howells /* initial superblock/root creation */
589436058a4SDavid Howells _debug("create");
59013fcc683SDavid Howells ret = afs_fill_super(sb, ctx);
591f044c884SDavid Howells if (ret < 0)
592f044c884SDavid Howells goto error_sb;
5931751e8a6SLinus Torvalds sb->s_flags |= SB_ACTIVE;
594436058a4SDavid Howells } else {
595436058a4SDavid Howells _debug("reuse");
5961751e8a6SLinus Torvalds ASSERTCMP(sb->s_flags, &, SB_ACTIVE);
597436058a4SDavid Howells }
5981da177e4SLinus Torvalds
59913fcc683SDavid Howells fc->root = dget(sb->s_root);
60080548b03SDavid Howells trace_afs_get_tree(as->cell, as->volume);
60108e0e7c8SDavid Howells _leave(" = 0 [%p]", sb);
60213fcc683SDavid Howells return 0;
6031da177e4SLinus Torvalds
604f044c884SDavid Howells error_sb:
605f044c884SDavid Howells deactivate_locked_super(sb);
6061da177e4SLinus Torvalds error:
6071da177e4SLinus Torvalds _leave(" = %d", ret);
60813fcc683SDavid Howells return ret;
60913fcc683SDavid Howells }
61013fcc683SDavid Howells
afs_free_fc(struct fs_context * fc)61113fcc683SDavid Howells static void afs_free_fc(struct fs_context *fc)
61213fcc683SDavid Howells {
61313fcc683SDavid Howells struct afs_fs_context *ctx = fc->fs_private;
61413fcc683SDavid Howells
61513fcc683SDavid Howells afs_destroy_sbi(fc->s_fs_info);
616cca37d45SDavid Howells afs_put_volume(ctx->net, ctx->volume, afs_volume_trace_put_free_fc);
617dca54a7bSDavid Howells afs_unuse_cell(ctx->net, ctx->cell, afs_cell_trace_unuse_fc);
61813fcc683SDavid Howells key_put(ctx->key);
61913fcc683SDavid Howells kfree(ctx);
62013fcc683SDavid Howells }
62113fcc683SDavid Howells
62213fcc683SDavid Howells static const struct fs_context_operations afs_context_ops = {
62313fcc683SDavid Howells .free = afs_free_fc,
62413fcc683SDavid Howells .parse_param = afs_parse_param,
62513fcc683SDavid Howells .get_tree = afs_get_tree,
62613fcc683SDavid Howells };
62713fcc683SDavid Howells
62813fcc683SDavid Howells /*
62913fcc683SDavid Howells * Set up the filesystem mount context.
63013fcc683SDavid Howells */
afs_init_fs_context(struct fs_context * fc)63113fcc683SDavid Howells static int afs_init_fs_context(struct fs_context *fc)
63213fcc683SDavid Howells {
63313fcc683SDavid Howells struct afs_fs_context *ctx;
63413fcc683SDavid Howells struct afs_cell *cell;
63513fcc683SDavid Howells
63613fcc683SDavid Howells ctx = kzalloc(sizeof(struct afs_fs_context), GFP_KERNEL);
63713fcc683SDavid Howells if (!ctx)
63813fcc683SDavid Howells return -ENOMEM;
63913fcc683SDavid Howells
64013fcc683SDavid Howells ctx->type = AFSVL_ROVOL;
64113fcc683SDavid Howells ctx->net = afs_net(fc->net_ns);
64213fcc683SDavid Howells
64313fcc683SDavid Howells /* Default to the workstation cell. */
644dca54a7bSDavid Howells cell = afs_find_cell(ctx->net, NULL, 0, afs_cell_trace_use_fc);
64513fcc683SDavid Howells if (IS_ERR(cell))
64613fcc683SDavid Howells cell = NULL;
64713fcc683SDavid Howells ctx->cell = cell;
64813fcc683SDavid Howells
64913fcc683SDavid Howells fc->fs_private = ctx;
65013fcc683SDavid Howells fc->ops = &afs_context_ops;
65113fcc683SDavid Howells return 0;
652ec26815aSDavid Howells }
6531da177e4SLinus Torvalds
6541da177e4SLinus Torvalds /*
655f8de483eSDavid Howells * Initialise an inode cache slab element prior to any use. Note that
656f8de483eSDavid Howells * afs_alloc_inode() *must* reset anything that could incorrectly leak from one
657f8de483eSDavid Howells * inode to another.
6581da177e4SLinus Torvalds */
afs_i_init_once(void * _vnode)65951cc5068SAlexey Dobriyan static void afs_i_init_once(void *_vnode)
6601da177e4SLinus Torvalds {
661ec26815aSDavid Howells struct afs_vnode *vnode = _vnode;
6621da177e4SLinus Torvalds
6631da177e4SLinus Torvalds memset(vnode, 0, sizeof(*vnode));
664874c8ca1SDavid Howells inode_init_once(&vnode->netfs.inode);
665d2ddc776SDavid Howells mutex_init(&vnode->io_lock);
666b61f7dcfSDavid Howells init_rwsem(&vnode->validate_lock);
6674343d008SDavid Howells spin_lock_init(&vnode->wb_lock);
6681da177e4SLinus Torvalds spin_lock_init(&vnode->lock);
6694343d008SDavid Howells INIT_LIST_HEAD(&vnode->wb_keys);
670e8d6c554SDavid Howells INIT_LIST_HEAD(&vnode->pending_locks);
671e8d6c554SDavid Howells INIT_LIST_HEAD(&vnode->granted_locks);
672e8d6c554SDavid Howells INIT_DELAYED_WORK(&vnode->lock_work, afs_lock_work);
6731744a22aSDavid Howells INIT_LIST_HEAD(&vnode->cb_mmap_link);
674c435ee34SDavid Howells seqlock_init(&vnode->cb_lock);
6751da177e4SLinus Torvalds }
6761da177e4SLinus Torvalds
6771da177e4SLinus Torvalds /*
6781da177e4SLinus Torvalds * allocate an AFS inode struct from our slab cache
6791da177e4SLinus Torvalds */
afs_alloc_inode(struct super_block * sb)6801da177e4SLinus Torvalds static struct inode *afs_alloc_inode(struct super_block *sb)
6811da177e4SLinus Torvalds {
6821da177e4SLinus Torvalds struct afs_vnode *vnode;
6831da177e4SLinus Torvalds
684fd60b288SMuchun Song vnode = alloc_inode_sb(sb, afs_inode_cachep, GFP_KERNEL);
6851da177e4SLinus Torvalds if (!vnode)
6861da177e4SLinus Torvalds return NULL;
6871da177e4SLinus Torvalds
6881da177e4SLinus Torvalds atomic_inc(&afs_count_active_inodes);
6891da177e4SLinus Torvalds
690f8de483eSDavid Howells /* Reset anything that shouldn't leak from one inode to the next. */
6911da177e4SLinus Torvalds memset(&vnode->fid, 0, sizeof(vnode->fid));
6921da177e4SLinus Torvalds memset(&vnode->status, 0, sizeof(vnode->status));
693bc899ee1SDavid Howells afs_vnode_set_cache(vnode, NULL);
6941da177e4SLinus Torvalds
6951da177e4SLinus Torvalds vnode->volume = NULL;
696f8de483eSDavid Howells vnode->lock_key = NULL;
697f8de483eSDavid Howells vnode->permit_cache = NULL;
698f8de483eSDavid Howells
699260a9803SDavid Howells vnode->flags = 1 << AFS_VNODE_UNSET;
700f8de483eSDavid Howells vnode->lock_state = AFS_VNODE_LOCK_NONE;
7011da177e4SLinus Torvalds
70279ddbfa5SDavid Howells init_rwsem(&vnode->rmdir_lock);
7036e0e99d5SDavid Howells INIT_WORK(&vnode->cb_work, afs_invalidate_mmap_work);
70479ddbfa5SDavid Howells
705874c8ca1SDavid Howells _leave(" = %p", &vnode->netfs.inode);
706874c8ca1SDavid Howells return &vnode->netfs.inode;
707ec26815aSDavid Howells }
7081da177e4SLinus Torvalds
afs_free_inode(struct inode * inode)70951b9fe48SAl Viro static void afs_free_inode(struct inode *inode)
710fa0d7e3dSNick Piggin {
71151b9fe48SAl Viro kmem_cache_free(afs_inode_cachep, AFS_FS_I(inode));
712fa0d7e3dSNick Piggin }
713fa0d7e3dSNick Piggin
7141da177e4SLinus Torvalds /*
7151da177e4SLinus Torvalds * destroy an AFS inode struct
7161da177e4SLinus Torvalds */
afs_destroy_inode(struct inode * inode)7171da177e4SLinus Torvalds static void afs_destroy_inode(struct inode *inode)
7181da177e4SLinus Torvalds {
71908e0e7c8SDavid Howells struct afs_vnode *vnode = AFS_FS_I(inode);
72008e0e7c8SDavid Howells
7213b6492dfSDavid Howells _enter("%p{%llx:%llu}", inode, vnode->fid.vid, vnode->fid.vnode);
7221da177e4SLinus Torvalds
72308e0e7c8SDavid Howells _debug("DESTROY INODE %p", inode);
72408e0e7c8SDavid Howells
7251da177e4SLinus Torvalds atomic_dec(&afs_count_active_inodes);
726ec26815aSDavid Howells }
72745222b9eSDavid Howells
afs_get_volume_status_success(struct afs_operation * op)728e49c7b2fSDavid Howells static void afs_get_volume_status_success(struct afs_operation *op)
729e49c7b2fSDavid Howells {
730e49c7b2fSDavid Howells struct afs_volume_status *vs = &op->volstatus.vs;
731e49c7b2fSDavid Howells struct kstatfs *buf = op->volstatus.buf;
732e49c7b2fSDavid Howells
733e49c7b2fSDavid Howells if (vs->max_quota == 0)
734e49c7b2fSDavid Howells buf->f_blocks = vs->part_max_blocks;
735e49c7b2fSDavid Howells else
736e49c7b2fSDavid Howells buf->f_blocks = vs->max_quota;
737f11a016aSDavid Howells
738f11a016aSDavid Howells if (buf->f_blocks > vs->blocks_in_use)
739f11a016aSDavid Howells buf->f_bavail = buf->f_bfree =
740f11a016aSDavid Howells buf->f_blocks - vs->blocks_in_use;
741e49c7b2fSDavid Howells }
742e49c7b2fSDavid Howells
743e49c7b2fSDavid Howells static const struct afs_operation_ops afs_get_volume_status_operation = {
744e49c7b2fSDavid Howells .issue_afs_rpc = afs_fs_get_volume_status,
745e49c7b2fSDavid Howells .issue_yfs_rpc = yfs_fs_get_volume_status,
746e49c7b2fSDavid Howells .success = afs_get_volume_status_success,
747e49c7b2fSDavid Howells };
748e49c7b2fSDavid Howells
74945222b9eSDavid Howells /*
75045222b9eSDavid Howells * return information about an AFS volume
75145222b9eSDavid Howells */
afs_statfs(struct dentry * dentry,struct kstatfs * buf)75245222b9eSDavid Howells static int afs_statfs(struct dentry *dentry, struct kstatfs *buf)
75345222b9eSDavid Howells {
7544d673da1SDavid Howells struct afs_super_info *as = AFS_FS_S(dentry->d_sb);
755e49c7b2fSDavid Howells struct afs_operation *op;
7562b0143b5SDavid Howells struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
75745222b9eSDavid Howells
7584d673da1SDavid Howells buf->f_type = dentry->d_sb->s_magic;
7594d673da1SDavid Howells buf->f_bsize = AFS_BLOCK_SIZE;
7604d673da1SDavid Howells buf->f_namelen = AFSNAMEMAX - 1;
7614d673da1SDavid Howells
7624d673da1SDavid Howells if (as->dyn_root) {
7634d673da1SDavid Howells buf->f_blocks = 1;
7644d673da1SDavid Howells buf->f_bavail = 0;
7654d673da1SDavid Howells buf->f_bfree = 0;
7664d673da1SDavid Howells return 0;
7674d673da1SDavid Howells }
7684d673da1SDavid Howells
769e49c7b2fSDavid Howells op = afs_alloc_operation(NULL, as->volume);
770e49c7b2fSDavid Howells if (IS_ERR(op))
771e49c7b2fSDavid Howells return PTR_ERR(op);
77245222b9eSDavid Howells
773e49c7b2fSDavid Howells afs_op_set_vnode(op, 0, vnode);
774e49c7b2fSDavid Howells op->nr_files = 1;
775e49c7b2fSDavid Howells op->volstatus.buf = buf;
776e49c7b2fSDavid Howells op->ops = &afs_get_volume_status_operation;
777e49c7b2fSDavid Howells return afs_do_sync_operation(op);
77845222b9eSDavid Howells }
779