1ec26815aSDavid Howells /* AFS superblock handling 2ec26815aSDavid Howells * 3*13fcc683SDavid 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> 24*13fcc683SDavid 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); 3645222b9eSDavid Howells static int afs_statfs(struct dentry *dentry, struct kstatfs *buf); 37677018a6SDavid Howells static int afs_show_devname(struct seq_file *m, struct dentry *root); 38677018a6SDavid Howells static int afs_show_options(struct seq_file *m, struct dentry *root); 39*13fcc683SDavid Howells static int afs_init_fs_context(struct fs_context *fc); 40*13fcc683SDavid Howells static const struct fs_parameter_description afs_fs_parameters; 411da177e4SLinus Torvalds 421f5ce9e9STrond Myklebust struct file_system_type afs_fs_type = { 431da177e4SLinus Torvalds .owner = THIS_MODULE, 441da177e4SLinus Torvalds .name = "afs", 45*13fcc683SDavid Howells .init_fs_context = afs_init_fs_context, 46*13fcc683SDavid Howells .parameters = &afs_fs_parameters, 47dde194a6SAl Viro .kill_sb = afs_kill_super, 4880c72fe4SDavid Howells .fs_flags = 0, 491da177e4SLinus Torvalds }; 507f78e035SEric W. Biederman MODULE_ALIAS_FS("afs"); 511da177e4SLinus Torvalds 525b86d4ffSDavid Howells int afs_net_id; 535b86d4ffSDavid Howells 54ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations afs_super_ops = { 5545222b9eSDavid Howells .statfs = afs_statfs, 561da177e4SLinus Torvalds .alloc_inode = afs_alloc_inode, 57bec5eb61Swanglei .drop_inode = afs_drop_inode, 581da177e4SLinus Torvalds .destroy_inode = afs_destroy_inode, 59b57922d9SAl Viro .evict_inode = afs_evict_inode, 60677018a6SDavid Howells .show_devname = afs_show_devname, 61677018a6SDavid Howells .show_options = afs_show_options, 621da177e4SLinus Torvalds }; 631da177e4SLinus Torvalds 64e18b890bSChristoph Lameter static struct kmem_cache *afs_inode_cachep; 651da177e4SLinus Torvalds static atomic_t afs_count_active_inodes; 661da177e4SLinus Torvalds 67*13fcc683SDavid Howells enum afs_param { 68*13fcc683SDavid Howells Opt_autocell, 69*13fcc683SDavid Howells Opt_cell, 70*13fcc683SDavid Howells Opt_dyn, 71*13fcc683SDavid Howells Opt_rwpath, 72*13fcc683SDavid Howells Opt_source, 73*13fcc683SDavid Howells Opt_vol, 7480c72fe4SDavid Howells }; 7580c72fe4SDavid Howells 76*13fcc683SDavid Howells static const struct fs_parameter_spec afs_param_specs[] = { 77*13fcc683SDavid Howells fsparam_flag ("autocell", Opt_autocell), 78*13fcc683SDavid Howells fsparam_string("cell", Opt_cell), 79*13fcc683SDavid Howells fsparam_flag ("dyn", Opt_dyn), 80*13fcc683SDavid Howells fsparam_flag ("rwpath", Opt_rwpath), 81*13fcc683SDavid Howells fsparam_string("source", Opt_source), 82*13fcc683SDavid Howells fsparam_string("vol", Opt_vol), 83*13fcc683SDavid Howells {} 84*13fcc683SDavid Howells }; 85*13fcc683SDavid Howells 86*13fcc683SDavid Howells static const struct fs_parameter_description afs_fs_parameters = { 87*13fcc683SDavid Howells .name = "kAFS", 88*13fcc683SDavid Howells .specs = afs_param_specs, 8980c72fe4SDavid Howells }; 9080c72fe4SDavid Howells 911da177e4SLinus Torvalds /* 921da177e4SLinus Torvalds * initialise the filesystem 931da177e4SLinus Torvalds */ 941da177e4SLinus Torvalds int __init afs_fs_init(void) 951da177e4SLinus Torvalds { 961da177e4SLinus Torvalds int ret; 971da177e4SLinus Torvalds 981da177e4SLinus Torvalds _enter(""); 991da177e4SLinus Torvalds 1001da177e4SLinus Torvalds /* create ourselves an inode cache */ 1011da177e4SLinus Torvalds atomic_set(&afs_count_active_inodes, 0); 1021da177e4SLinus Torvalds 1031da177e4SLinus Torvalds ret = -ENOMEM; 1041da177e4SLinus Torvalds afs_inode_cachep = kmem_cache_create("afs_inode_cache", 1051da177e4SLinus Torvalds sizeof(struct afs_vnode), 1061da177e4SLinus Torvalds 0, 1075d097056SVladimir Davydov SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT, 10820c2df83SPaul Mundt afs_i_init_once); 1091da177e4SLinus Torvalds if (!afs_inode_cachep) { 1101da177e4SLinus Torvalds printk(KERN_NOTICE "kAFS: Failed to allocate inode cache\n"); 1111da177e4SLinus Torvalds return ret; 1121da177e4SLinus Torvalds } 1131da177e4SLinus Torvalds 1141da177e4SLinus Torvalds /* now export our filesystem to lesser mortals */ 1151da177e4SLinus Torvalds ret = register_filesystem(&afs_fs_type); 1161da177e4SLinus Torvalds if (ret < 0) { 1171da177e4SLinus Torvalds kmem_cache_destroy(afs_inode_cachep); 11808e0e7c8SDavid Howells _leave(" = %d", ret); 1191da177e4SLinus Torvalds return ret; 1201da177e4SLinus Torvalds } 1211da177e4SLinus Torvalds 12208e0e7c8SDavid Howells _leave(" = 0"); 1231da177e4SLinus Torvalds return 0; 124ec26815aSDavid Howells } 1251da177e4SLinus Torvalds 1261da177e4SLinus Torvalds /* 1271da177e4SLinus Torvalds * clean up the filesystem 1281da177e4SLinus Torvalds */ 1295b86d4ffSDavid Howells void afs_fs_exit(void) 1301da177e4SLinus Torvalds { 13108e0e7c8SDavid Howells _enter(""); 13208e0e7c8SDavid Howells 13308e0e7c8SDavid Howells afs_mntpt_kill_timer(); 1341da177e4SLinus Torvalds unregister_filesystem(&afs_fs_type); 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds if (atomic_read(&afs_count_active_inodes) != 0) { 1371da177e4SLinus Torvalds printk("kAFS: %d active inode objects still present\n", 1381da177e4SLinus Torvalds atomic_read(&afs_count_active_inodes)); 1391da177e4SLinus Torvalds BUG(); 1401da177e4SLinus Torvalds } 1411da177e4SLinus Torvalds 1428c0a8537SKirill A. Shutemov /* 1438c0a8537SKirill A. Shutemov * Make sure all delayed rcu free inodes are flushed before we 1448c0a8537SKirill A. Shutemov * destroy cache. 1458c0a8537SKirill A. Shutemov */ 1468c0a8537SKirill A. Shutemov rcu_barrier(); 1471da177e4SLinus Torvalds kmem_cache_destroy(afs_inode_cachep); 14808e0e7c8SDavid Howells _leave(""); 149ec26815aSDavid Howells } 1501da177e4SLinus Torvalds 1511da177e4SLinus Torvalds /* 152677018a6SDavid Howells * Display the mount device name in /proc/mounts. 153677018a6SDavid Howells */ 154677018a6SDavid Howells static int afs_show_devname(struct seq_file *m, struct dentry *root) 155677018a6SDavid Howells { 156d2ddc776SDavid Howells struct afs_super_info *as = AFS_FS_S(root->d_sb); 157677018a6SDavid Howells struct afs_volume *volume = as->volume; 158d2ddc776SDavid Howells struct afs_cell *cell = as->cell; 159677018a6SDavid Howells const char *suf = ""; 160677018a6SDavid Howells char pref = '%'; 161677018a6SDavid Howells 1624d673da1SDavid Howells if (as->dyn_root) { 1634d673da1SDavid Howells seq_puts(m, "none"); 1644d673da1SDavid Howells return 0; 1654d673da1SDavid Howells } 1664d673da1SDavid Howells 167677018a6SDavid Howells switch (volume->type) { 168677018a6SDavid Howells case AFSVL_RWVOL: 169677018a6SDavid Howells break; 170677018a6SDavid Howells case AFSVL_ROVOL: 171677018a6SDavid Howells pref = '#'; 172677018a6SDavid Howells if (volume->type_force) 173677018a6SDavid Howells suf = ".readonly"; 174677018a6SDavid Howells break; 175677018a6SDavid Howells case AFSVL_BACKVOL: 176677018a6SDavid Howells pref = '#'; 177677018a6SDavid Howells suf = ".backup"; 178677018a6SDavid Howells break; 179677018a6SDavid Howells } 180677018a6SDavid Howells 181d2ddc776SDavid Howells seq_printf(m, "%c%s:%s%s", pref, cell->name, volume->name, suf); 182677018a6SDavid Howells return 0; 183677018a6SDavid Howells } 184677018a6SDavid Howells 185677018a6SDavid Howells /* 186677018a6SDavid Howells * Display the mount options in /proc/mounts. 187677018a6SDavid Howells */ 188677018a6SDavid Howells static int afs_show_options(struct seq_file *m, struct dentry *root) 189677018a6SDavid Howells { 1904d673da1SDavid Howells struct afs_super_info *as = AFS_FS_S(root->d_sb); 1914d673da1SDavid Howells 1924d673da1SDavid Howells if (as->dyn_root) 1934d673da1SDavid Howells seq_puts(m, ",dyn"); 194677018a6SDavid Howells if (test_bit(AFS_VNODE_AUTOCELL, &AFS_FS_I(d_inode(root))->flags)) 1954d673da1SDavid Howells seq_puts(m, ",autocell"); 196677018a6SDavid Howells return 0; 197677018a6SDavid Howells } 198677018a6SDavid Howells 199677018a6SDavid Howells /* 200*13fcc683SDavid Howells * Parse the source name to get cell name, volume name, volume type and R/W 201*13fcc683SDavid Howells * selector. 202*13fcc683SDavid Howells * 203*13fcc683SDavid Howells * This can be one of the following: 20400d3b7a4SDavid Howells * "%[cell:]volume[.]" R/W volume 20500d3b7a4SDavid Howells * "#[cell:]volume[.]" R/O or R/W volume (rwpath=0), 20600d3b7a4SDavid Howells * or R/W (rwpath=1) volume 20700d3b7a4SDavid Howells * "%[cell:]volume.readonly" R/O volume 20800d3b7a4SDavid Howells * "#[cell:]volume.readonly" R/O volume 20900d3b7a4SDavid Howells * "%[cell:]volume.backup" Backup volume 21000d3b7a4SDavid Howells * "#[cell:]volume.backup" Backup volume 21100d3b7a4SDavid Howells */ 212*13fcc683SDavid Howells static int afs_parse_source(struct fs_context *fc, struct fs_parameter *param) 21300d3b7a4SDavid Howells { 214*13fcc683SDavid Howells struct afs_fs_context *ctx = fc->fs_private; 21500d3b7a4SDavid Howells struct afs_cell *cell; 216*13fcc683SDavid Howells const char *cellname, *suffix, *name = param->string; 21700d3b7a4SDavid Howells int cellnamesz; 21800d3b7a4SDavid Howells 21900d3b7a4SDavid Howells _enter(",%s", name); 22000d3b7a4SDavid Howells 22100d3b7a4SDavid Howells if (!name) { 22200d3b7a4SDavid Howells printk(KERN_ERR "kAFS: no volume name specified\n"); 22300d3b7a4SDavid Howells return -EINVAL; 22400d3b7a4SDavid Howells } 22500d3b7a4SDavid Howells 22600d3b7a4SDavid Howells if ((name[0] != '%' && name[0] != '#') || !name[1]) { 227*13fcc683SDavid Howells /* To use dynroot, we don't want to have to provide a source */ 228*13fcc683SDavid Howells if (strcmp(name, "none") == 0) { 229*13fcc683SDavid Howells ctx->no_cell = true; 230*13fcc683SDavid Howells return 0; 231*13fcc683SDavid Howells } 23200d3b7a4SDavid Howells printk(KERN_ERR "kAFS: unparsable volume name\n"); 23300d3b7a4SDavid Howells return -EINVAL; 23400d3b7a4SDavid Howells } 23500d3b7a4SDavid Howells 23600d3b7a4SDavid Howells /* determine the type of volume we're looking for */ 237*13fcc683SDavid Howells ctx->type = AFSVL_ROVOL; 238*13fcc683SDavid Howells ctx->force = false; 239*13fcc683SDavid Howells if (ctx->rwpath || name[0] == '%') { 240*13fcc683SDavid Howells ctx->type = AFSVL_RWVOL; 241*13fcc683SDavid Howells ctx->force = true; 24200d3b7a4SDavid Howells } 24300d3b7a4SDavid Howells name++; 24400d3b7a4SDavid Howells 24500d3b7a4SDavid Howells /* split the cell name out if there is one */ 246*13fcc683SDavid Howells ctx->volname = strchr(name, ':'); 247*13fcc683SDavid Howells if (ctx->volname) { 24800d3b7a4SDavid Howells cellname = name; 249*13fcc683SDavid Howells cellnamesz = ctx->volname - name; 250*13fcc683SDavid Howells ctx->volname++; 25100d3b7a4SDavid Howells } else { 252*13fcc683SDavid Howells ctx->volname = name; 25300d3b7a4SDavid Howells cellname = NULL; 25400d3b7a4SDavid Howells cellnamesz = 0; 25500d3b7a4SDavid Howells } 25600d3b7a4SDavid Howells 25700d3b7a4SDavid Howells /* the volume type is further affected by a possible suffix */ 258*13fcc683SDavid Howells suffix = strrchr(ctx->volname, '.'); 25900d3b7a4SDavid Howells if (suffix) { 26000d3b7a4SDavid Howells if (strcmp(suffix, ".readonly") == 0) { 261*13fcc683SDavid Howells ctx->type = AFSVL_ROVOL; 262*13fcc683SDavid Howells ctx->force = true; 26300d3b7a4SDavid Howells } else if (strcmp(suffix, ".backup") == 0) { 264*13fcc683SDavid Howells ctx->type = AFSVL_BACKVOL; 265*13fcc683SDavid Howells ctx->force = true; 26600d3b7a4SDavid Howells } else if (suffix[1] == 0) { 26700d3b7a4SDavid Howells } else { 26800d3b7a4SDavid Howells suffix = NULL; 26900d3b7a4SDavid Howells } 27000d3b7a4SDavid Howells } 27100d3b7a4SDavid Howells 272*13fcc683SDavid Howells ctx->volnamesz = suffix ? 273*13fcc683SDavid Howells suffix - ctx->volname : strlen(ctx->volname); 27400d3b7a4SDavid Howells 27500d3b7a4SDavid Howells _debug("cell %*.*s [%p]", 276*13fcc683SDavid Howells cellnamesz, cellnamesz, cellname ?: "", ctx->cell); 27700d3b7a4SDavid Howells 27800d3b7a4SDavid Howells /* lookup the cell record */ 279*13fcc683SDavid Howells if (cellname) { 280*13fcc683SDavid Howells cell = afs_lookup_cell(ctx->net, cellname, cellnamesz, 281989782dcSDavid Howells NULL, false); 28200d3b7a4SDavid Howells if (IS_ERR(cell)) { 283*13fcc683SDavid Howells pr_err("kAFS: unable to lookup cell '%*.*s'\n", 284bec5eb61Swanglei cellnamesz, cellnamesz, cellname ?: ""); 28500d3b7a4SDavid Howells return PTR_ERR(cell); 28600d3b7a4SDavid Howells } 287*13fcc683SDavid Howells afs_put_cell(ctx->net, ctx->cell); 288*13fcc683SDavid Howells ctx->cell = cell; 28900d3b7a4SDavid Howells } 29000d3b7a4SDavid Howells 29100d3b7a4SDavid Howells _debug("CELL:%s [%p] VOLUME:%*.*s SUFFIX:%s TYPE:%d%s", 292*13fcc683SDavid Howells ctx->cell->name, ctx->cell, 293*13fcc683SDavid Howells ctx->volnamesz, ctx->volnamesz, ctx->volname, 294*13fcc683SDavid Howells suffix ?: "-", ctx->type, ctx->force ? " FORCE" : ""); 295*13fcc683SDavid Howells 296*13fcc683SDavid Howells fc->source = param->string; 297*13fcc683SDavid Howells param->string = NULL; 298*13fcc683SDavid Howells return 0; 299*13fcc683SDavid Howells } 300*13fcc683SDavid Howells 301*13fcc683SDavid Howells /* 302*13fcc683SDavid Howells * Parse a single mount parameter. 303*13fcc683SDavid Howells */ 304*13fcc683SDavid Howells static int afs_parse_param(struct fs_context *fc, struct fs_parameter *param) 305*13fcc683SDavid Howells { 306*13fcc683SDavid Howells struct fs_parse_result result; 307*13fcc683SDavid Howells struct afs_fs_context *ctx = fc->fs_private; 308*13fcc683SDavid Howells struct afs_cell *cell; 309*13fcc683SDavid Howells int opt; 310*13fcc683SDavid Howells 311*13fcc683SDavid Howells opt = fs_parse(fc, &afs_fs_parameters, param, &result); 312*13fcc683SDavid Howells if (opt < 0) 313*13fcc683SDavid Howells return opt; 314*13fcc683SDavid Howells 315*13fcc683SDavid Howells switch (opt) { 316*13fcc683SDavid Howells case Opt_cell: 317*13fcc683SDavid Howells if (param->size <= 0) 318*13fcc683SDavid Howells return -EINVAL; 319*13fcc683SDavid Howells if (param->size > AFS_MAXCELLNAME) 320*13fcc683SDavid Howells return -ENAMETOOLONG; 321*13fcc683SDavid Howells 322*13fcc683SDavid Howells rcu_read_lock(); 323*13fcc683SDavid Howells cell = afs_lookup_cell_rcu(ctx->net, param->string, param->size); 324*13fcc683SDavid Howells rcu_read_unlock(); 325*13fcc683SDavid Howells if (IS_ERR(cell)) 326*13fcc683SDavid Howells return PTR_ERR(cell); 327*13fcc683SDavid Howells afs_put_cell(ctx->net, ctx->cell); 328*13fcc683SDavid Howells ctx->cell = cell; 329*13fcc683SDavid Howells break; 330*13fcc683SDavid Howells 331*13fcc683SDavid Howells case Opt_source: 332*13fcc683SDavid Howells return afs_parse_source(fc, param); 333*13fcc683SDavid Howells 334*13fcc683SDavid Howells case Opt_autocell: 335*13fcc683SDavid Howells ctx->autocell = true; 336*13fcc683SDavid Howells break; 337*13fcc683SDavid Howells 338*13fcc683SDavid Howells case Opt_dyn: 339*13fcc683SDavid Howells ctx->dyn_root = true; 340*13fcc683SDavid Howells break; 341*13fcc683SDavid Howells 342*13fcc683SDavid Howells case Opt_rwpath: 343*13fcc683SDavid Howells ctx->rwpath = true; 344*13fcc683SDavid Howells break; 345*13fcc683SDavid Howells 346*13fcc683SDavid Howells case Opt_vol: 347*13fcc683SDavid Howells return invalf(fc, "'vol' param is obsolete"); 348*13fcc683SDavid Howells 349*13fcc683SDavid Howells default: 350*13fcc683SDavid Howells return -EINVAL; 351*13fcc683SDavid Howells } 352*13fcc683SDavid Howells 353*13fcc683SDavid Howells _leave(" = 0"); 354*13fcc683SDavid Howells return 0; 355*13fcc683SDavid Howells } 356*13fcc683SDavid Howells 357*13fcc683SDavid Howells /* 358*13fcc683SDavid Howells * Validate the options, get the cell key and look up the volume. 359*13fcc683SDavid Howells */ 360*13fcc683SDavid Howells static int afs_validate_fc(struct fs_context *fc) 361*13fcc683SDavid Howells { 362*13fcc683SDavid Howells struct afs_fs_context *ctx = fc->fs_private; 363*13fcc683SDavid Howells struct afs_volume *volume; 364*13fcc683SDavid Howells struct key *key; 365*13fcc683SDavid Howells 366*13fcc683SDavid Howells if (!ctx->dyn_root) { 367*13fcc683SDavid Howells if (ctx->no_cell) { 368*13fcc683SDavid Howells pr_warn("kAFS: Can only specify source 'none' with -o dyn\n"); 369*13fcc683SDavid Howells return -EINVAL; 370*13fcc683SDavid Howells } 371*13fcc683SDavid Howells 372*13fcc683SDavid Howells if (!ctx->cell) { 373*13fcc683SDavid Howells pr_warn("kAFS: No cell specified\n"); 374*13fcc683SDavid Howells return -EDESTADDRREQ; 375*13fcc683SDavid Howells } 376*13fcc683SDavid Howells 377*13fcc683SDavid Howells /* We try to do the mount securely. */ 378*13fcc683SDavid Howells key = afs_request_key(ctx->cell); 379*13fcc683SDavid Howells if (IS_ERR(key)) 380*13fcc683SDavid Howells return PTR_ERR(key); 381*13fcc683SDavid Howells 382*13fcc683SDavid Howells ctx->key = key; 383*13fcc683SDavid Howells 384*13fcc683SDavid Howells if (ctx->volume) { 385*13fcc683SDavid Howells afs_put_volume(ctx->cell, ctx->volume); 386*13fcc683SDavid Howells ctx->volume = NULL; 387*13fcc683SDavid Howells } 388*13fcc683SDavid Howells 389*13fcc683SDavid Howells volume = afs_create_volume(ctx); 390*13fcc683SDavid Howells if (IS_ERR(volume)) 391*13fcc683SDavid Howells return PTR_ERR(volume); 392*13fcc683SDavid Howells 393*13fcc683SDavid Howells ctx->volume = volume; 394*13fcc683SDavid Howells } 39500d3b7a4SDavid Howells 39600d3b7a4SDavid Howells return 0; 39700d3b7a4SDavid Howells } 39800d3b7a4SDavid Howells 39900d3b7a4SDavid Howells /* 4001da177e4SLinus Torvalds * check a superblock to see if it's the one we're looking for 4011da177e4SLinus Torvalds */ 402*13fcc683SDavid Howells static int afs_test_super(struct super_block *sb, struct fs_context *fc) 4031da177e4SLinus Torvalds { 404*13fcc683SDavid Howells struct afs_fs_context *ctx = fc->fs_private; 405d2ddc776SDavid Howells struct afs_super_info *as = AFS_FS_S(sb); 4061da177e4SLinus Torvalds 407*13fcc683SDavid Howells return (as->net_ns == fc->net_ns && 4084d673da1SDavid Howells as->volume && 409*13fcc683SDavid Howells as->volume->vid == ctx->volume->vid && 4100da0b7fdSDavid Howells !as->dyn_root); 4114d673da1SDavid Howells } 4124d673da1SDavid Howells 413*13fcc683SDavid Howells static int afs_dynroot_test_super(struct super_block *sb, struct fs_context *fc) 4144d673da1SDavid Howells { 4150da0b7fdSDavid Howells struct afs_super_info *as = AFS_FS_S(sb); 4160da0b7fdSDavid Howells 417*13fcc683SDavid Howells return (as->net_ns == fc->net_ns && 4180da0b7fdSDavid Howells as->dyn_root); 419dde194a6SAl Viro } 420dde194a6SAl Viro 421*13fcc683SDavid Howells static int afs_set_super(struct super_block *sb, struct fs_context *fc) 422dde194a6SAl Viro { 423dde194a6SAl Viro return set_anon_super(sb, NULL); 424ec26815aSDavid Howells } 4251da177e4SLinus Torvalds 4261da177e4SLinus Torvalds /* 4271da177e4SLinus Torvalds * fill in the superblock 4281da177e4SLinus Torvalds */ 429*13fcc683SDavid Howells static int afs_fill_super(struct super_block *sb, struct afs_fs_context *ctx) 4301da177e4SLinus Torvalds { 431d2ddc776SDavid Howells struct afs_super_info *as = AFS_FS_S(sb); 4321da177e4SLinus Torvalds struct afs_fid fid; 4331da177e4SLinus Torvalds struct inode *inode = NULL; 4341da177e4SLinus Torvalds int ret; 4351da177e4SLinus Torvalds 43608e0e7c8SDavid Howells _enter(""); 4371da177e4SLinus Torvalds 4381da177e4SLinus Torvalds /* fill in the superblock */ 43909cbfeafSKirill A. Shutemov sb->s_blocksize = PAGE_SIZE; 44009cbfeafSKirill A. Shutemov sb->s_blocksize_bits = PAGE_SHIFT; 4411da177e4SLinus Torvalds sb->s_magic = AFS_FS_MAGIC; 4421da177e4SLinus Torvalds sb->s_op = &afs_super_ops; 4434d673da1SDavid Howells if (!as->dyn_root) 444d3e3b7eaSDavid Howells sb->s_xattr = afs_xattr_handlers; 445edd3ba94SJan Kara ret = super_setup_bdi(sb); 446edd3ba94SJan Kara if (ret) 447edd3ba94SJan Kara return ret; 448edd3ba94SJan Kara sb->s_bdi->ra_pages = VM_MAX_READAHEAD * 1024 / PAGE_SIZE; 4491da177e4SLinus Torvalds 4501da177e4SLinus Torvalds /* allocate the root inode and dentry */ 4514d673da1SDavid Howells if (as->dyn_root) { 4524d673da1SDavid Howells inode = afs_iget_pseudo_dir(sb, true); 4534d673da1SDavid Howells sb->s_flags |= SB_RDONLY; 4544d673da1SDavid Howells } else { 4553b6492dfSDavid Howells sprintf(sb->s_id, "%llu", as->volume->vid); 4564d673da1SDavid Howells afs_activate_volume(as->volume); 4571da177e4SLinus Torvalds fid.vid = as->volume->vid; 4581da177e4SLinus Torvalds fid.vnode = 1; 4593b6492dfSDavid Howells fid.vnode_hi = 0; 4601da177e4SLinus Torvalds fid.unique = 1; 461*13fcc683SDavid Howells inode = afs_iget(sb, ctx->key, &fid, NULL, NULL, NULL); 4624d673da1SDavid Howells } 4634d673da1SDavid Howells 46408e0e7c8SDavid Howells if (IS_ERR(inode)) 465dde194a6SAl Viro return PTR_ERR(inode); 4661da177e4SLinus Torvalds 467*13fcc683SDavid Howells if (ctx->autocell || as->dyn_root) 468bec5eb61Swanglei set_bit(AFS_VNODE_AUTOCELL, &AFS_FS_I(inode)->flags); 469bec5eb61Swanglei 4701da177e4SLinus Torvalds ret = -ENOMEM; 47148fde701SAl Viro sb->s_root = d_make_root(inode); 47248fde701SAl Viro if (!sb->s_root) 4731da177e4SLinus Torvalds goto error; 4741da177e4SLinus Torvalds 4750da0b7fdSDavid Howells if (as->dyn_root) { 47666c7e1d3SDavid Howells sb->s_d_op = &afs_dynroot_dentry_operations; 4770da0b7fdSDavid Howells ret = afs_dynroot_populate(sb); 4780da0b7fdSDavid Howells if (ret < 0) 4790da0b7fdSDavid Howells goto error; 4800da0b7fdSDavid Howells } else { 481d61dcce2SAl Viro sb->s_d_op = &afs_fs_dentry_operations; 4820da0b7fdSDavid Howells } 4831da177e4SLinus Torvalds 48408e0e7c8SDavid Howells _leave(" = 0"); 4851da177e4SLinus Torvalds return 0; 4861da177e4SLinus Torvalds 4871da177e4SLinus Torvalds error: 48808e0e7c8SDavid Howells _leave(" = %d", ret); 4891da177e4SLinus Torvalds return ret; 490ec26815aSDavid Howells } 4911da177e4SLinus Torvalds 492*13fcc683SDavid Howells static struct afs_super_info *afs_alloc_sbi(struct fs_context *fc) 49349566f6fSDavid Howells { 494*13fcc683SDavid Howells struct afs_fs_context *ctx = fc->fs_private; 49549566f6fSDavid Howells struct afs_super_info *as; 49649566f6fSDavid Howells 49749566f6fSDavid Howells as = kzalloc(sizeof(struct afs_super_info), GFP_KERNEL); 49849566f6fSDavid Howells if (as) { 499*13fcc683SDavid Howells as->net_ns = get_net(fc->net_ns); 500*13fcc683SDavid Howells if (ctx->dyn_root) { 5014d673da1SDavid Howells as->dyn_root = true; 502*13fcc683SDavid Howells } else { 503*13fcc683SDavid Howells as->cell = afs_get_cell(ctx->cell); 504*13fcc683SDavid Howells as->volume = __afs_get_volume(ctx->volume); 505*13fcc683SDavid Howells } 50649566f6fSDavid Howells } 50749566f6fSDavid Howells return as; 50849566f6fSDavid Howells } 50949566f6fSDavid Howells 51049566f6fSDavid Howells static void afs_destroy_sbi(struct afs_super_info *as) 51149566f6fSDavid Howells { 51249566f6fSDavid Howells if (as) { 5139ed900b1SDavid Howells afs_put_volume(as->cell, as->volume); 5145b86d4ffSDavid Howells afs_put_cell(afs_net(as->net_ns), as->cell); 5155b86d4ffSDavid Howells put_net(as->net_ns); 51649566f6fSDavid Howells kfree(as); 51749566f6fSDavid Howells } 51849566f6fSDavid Howells } 51949566f6fSDavid Howells 5200da0b7fdSDavid Howells static void afs_kill_super(struct super_block *sb) 5210da0b7fdSDavid Howells { 5220da0b7fdSDavid Howells struct afs_super_info *as = AFS_FS_S(sb); 5230da0b7fdSDavid Howells struct afs_net *net = afs_net(as->net_ns); 5240da0b7fdSDavid Howells 5250da0b7fdSDavid Howells if (as->dyn_root) 5260da0b7fdSDavid Howells afs_dynroot_depopulate(sb); 5270da0b7fdSDavid Howells 5280da0b7fdSDavid Howells /* Clear the callback interests (which will do ilookup5) before 5290da0b7fdSDavid Howells * deactivating the superblock. 5300da0b7fdSDavid Howells */ 5310da0b7fdSDavid Howells if (as->volume) 5320da0b7fdSDavid Howells afs_clear_callback_interests(net, as->volume->servers); 5330da0b7fdSDavid Howells kill_anon_super(sb); 5340da0b7fdSDavid Howells if (as->volume) 5350da0b7fdSDavid Howells afs_deactivate_volume(as->volume); 5360da0b7fdSDavid Howells afs_destroy_sbi(as); 5370da0b7fdSDavid Howells } 5380da0b7fdSDavid Howells 5391da177e4SLinus Torvalds /* 540*13fcc683SDavid Howells * Get an AFS superblock and root directory. 5411da177e4SLinus Torvalds */ 542*13fcc683SDavid Howells static int afs_get_tree(struct fs_context *fc) 5431da177e4SLinus Torvalds { 544*13fcc683SDavid Howells struct afs_fs_context *ctx = fc->fs_private; 5451da177e4SLinus Torvalds struct super_block *sb; 546dde194a6SAl Viro struct afs_super_info *as; 5471da177e4SLinus Torvalds int ret; 5481da177e4SLinus Torvalds 549*13fcc683SDavid Howells ret = afs_validate_fc(fc); 550*13fcc683SDavid Howells if (ret) 55100d3b7a4SDavid Howells goto error; 55200d3b7a4SDavid Howells 553*13fcc683SDavid Howells _enter(""); 55400d3b7a4SDavid Howells 55549566f6fSDavid Howells /* allocate a superblock info record */ 55649566f6fSDavid Howells ret = -ENOMEM; 557*13fcc683SDavid Howells as = afs_alloc_sbi(fc); 55849566f6fSDavid Howells if (!as) 559*13fcc683SDavid Howells goto error; 560*13fcc683SDavid Howells fc->s_fs_info = as; 5611da177e4SLinus Torvalds 5621da177e4SLinus Torvalds /* allocate a deviceless superblock */ 563*13fcc683SDavid Howells sb = sget_fc(fc, 5644d673da1SDavid Howells as->dyn_root ? afs_dynroot_test_super : afs_test_super, 565*13fcc683SDavid Howells afs_set_super); 56608e0e7c8SDavid Howells if (IS_ERR(sb)) { 56708e0e7c8SDavid Howells ret = PTR_ERR(sb); 568*13fcc683SDavid Howells goto error; 56908e0e7c8SDavid Howells } 5701da177e4SLinus Torvalds 571436058a4SDavid Howells if (!sb->s_root) { 572436058a4SDavid Howells /* initial superblock/root creation */ 573436058a4SDavid Howells _debug("create"); 574*13fcc683SDavid Howells ret = afs_fill_super(sb, ctx); 575f044c884SDavid Howells if (ret < 0) 576f044c884SDavid Howells goto error_sb; 5771751e8a6SLinus Torvalds sb->s_flags |= SB_ACTIVE; 578436058a4SDavid Howells } else { 579436058a4SDavid Howells _debug("reuse"); 5801751e8a6SLinus Torvalds ASSERTCMP(sb->s_flags, &, SB_ACTIVE); 581436058a4SDavid Howells } 5821da177e4SLinus Torvalds 583*13fcc683SDavid Howells fc->root = dget(sb->s_root); 58408e0e7c8SDavid Howells _leave(" = 0 [%p]", sb); 585*13fcc683SDavid Howells return 0; 5861da177e4SLinus Torvalds 587f044c884SDavid Howells error_sb: 588f044c884SDavid Howells deactivate_locked_super(sb); 5891da177e4SLinus Torvalds error: 5901da177e4SLinus Torvalds _leave(" = %d", ret); 591*13fcc683SDavid Howells return ret; 592*13fcc683SDavid Howells } 593*13fcc683SDavid Howells 594*13fcc683SDavid Howells static void afs_free_fc(struct fs_context *fc) 595*13fcc683SDavid Howells { 596*13fcc683SDavid Howells struct afs_fs_context *ctx = fc->fs_private; 597*13fcc683SDavid Howells 598*13fcc683SDavid Howells afs_destroy_sbi(fc->s_fs_info); 599*13fcc683SDavid Howells afs_put_volume(ctx->cell, ctx->volume); 600*13fcc683SDavid Howells afs_put_cell(ctx->net, ctx->cell); 601*13fcc683SDavid Howells key_put(ctx->key); 602*13fcc683SDavid Howells kfree(ctx); 603*13fcc683SDavid Howells } 604*13fcc683SDavid Howells 605*13fcc683SDavid Howells static const struct fs_context_operations afs_context_ops = { 606*13fcc683SDavid Howells .free = afs_free_fc, 607*13fcc683SDavid Howells .parse_param = afs_parse_param, 608*13fcc683SDavid Howells .get_tree = afs_get_tree, 609*13fcc683SDavid Howells }; 610*13fcc683SDavid Howells 611*13fcc683SDavid Howells /* 612*13fcc683SDavid Howells * Set up the filesystem mount context. 613*13fcc683SDavid Howells */ 614*13fcc683SDavid Howells static int afs_init_fs_context(struct fs_context *fc) 615*13fcc683SDavid Howells { 616*13fcc683SDavid Howells struct afs_fs_context *ctx; 617*13fcc683SDavid Howells struct afs_cell *cell; 618*13fcc683SDavid Howells 619*13fcc683SDavid Howells if (current->nsproxy->net_ns != &init_net) 620*13fcc683SDavid Howells return -EINVAL; 621*13fcc683SDavid Howells 622*13fcc683SDavid Howells ctx = kzalloc(sizeof(struct afs_fs_context), GFP_KERNEL); 623*13fcc683SDavid Howells if (!ctx) 624*13fcc683SDavid Howells return -ENOMEM; 625*13fcc683SDavid Howells 626*13fcc683SDavid Howells ctx->type = AFSVL_ROVOL; 627*13fcc683SDavid Howells ctx->net = afs_net(fc->net_ns); 628*13fcc683SDavid Howells 629*13fcc683SDavid Howells /* Default to the workstation cell. */ 630*13fcc683SDavid Howells rcu_read_lock(); 631*13fcc683SDavid Howells cell = afs_lookup_cell_rcu(ctx->net, NULL, 0); 632*13fcc683SDavid Howells rcu_read_unlock(); 633*13fcc683SDavid Howells if (IS_ERR(cell)) 634*13fcc683SDavid Howells cell = NULL; 635*13fcc683SDavid Howells ctx->cell = cell; 636*13fcc683SDavid Howells 637*13fcc683SDavid Howells fc->fs_private = ctx; 638*13fcc683SDavid Howells fc->ops = &afs_context_ops; 639*13fcc683SDavid Howells return 0; 640ec26815aSDavid Howells } 6411da177e4SLinus Torvalds 6421da177e4SLinus Torvalds /* 643f8de483eSDavid Howells * Initialise an inode cache slab element prior to any use. Note that 644f8de483eSDavid Howells * afs_alloc_inode() *must* reset anything that could incorrectly leak from one 645f8de483eSDavid Howells * inode to another. 6461da177e4SLinus Torvalds */ 64751cc5068SAlexey Dobriyan static void afs_i_init_once(void *_vnode) 6481da177e4SLinus Torvalds { 649ec26815aSDavid Howells struct afs_vnode *vnode = _vnode; 6501da177e4SLinus Torvalds 6511da177e4SLinus Torvalds memset(vnode, 0, sizeof(*vnode)); 6521da177e4SLinus Torvalds inode_init_once(&vnode->vfs_inode); 653d2ddc776SDavid Howells mutex_init(&vnode->io_lock); 654b61f7dcfSDavid Howells init_rwsem(&vnode->validate_lock); 6554343d008SDavid Howells spin_lock_init(&vnode->wb_lock); 6561da177e4SLinus Torvalds spin_lock_init(&vnode->lock); 6574343d008SDavid Howells INIT_LIST_HEAD(&vnode->wb_keys); 658e8d6c554SDavid Howells INIT_LIST_HEAD(&vnode->pending_locks); 659e8d6c554SDavid Howells INIT_LIST_HEAD(&vnode->granted_locks); 660e8d6c554SDavid Howells INIT_DELAYED_WORK(&vnode->lock_work, afs_lock_work); 661c435ee34SDavid Howells seqlock_init(&vnode->cb_lock); 6621da177e4SLinus Torvalds } 6631da177e4SLinus Torvalds 6641da177e4SLinus Torvalds /* 6651da177e4SLinus Torvalds * allocate an AFS inode struct from our slab cache 6661da177e4SLinus Torvalds */ 6671da177e4SLinus Torvalds static struct inode *afs_alloc_inode(struct super_block *sb) 6681da177e4SLinus Torvalds { 6691da177e4SLinus Torvalds struct afs_vnode *vnode; 6701da177e4SLinus Torvalds 671ec26815aSDavid Howells vnode = kmem_cache_alloc(afs_inode_cachep, GFP_KERNEL); 6721da177e4SLinus Torvalds if (!vnode) 6731da177e4SLinus Torvalds return NULL; 6741da177e4SLinus Torvalds 6751da177e4SLinus Torvalds atomic_inc(&afs_count_active_inodes); 6761da177e4SLinus Torvalds 677f8de483eSDavid Howells /* Reset anything that shouldn't leak from one inode to the next. */ 6781da177e4SLinus Torvalds memset(&vnode->fid, 0, sizeof(vnode->fid)); 6791da177e4SLinus Torvalds memset(&vnode->status, 0, sizeof(vnode->status)); 6801da177e4SLinus Torvalds 6811da177e4SLinus Torvalds vnode->volume = NULL; 682f8de483eSDavid Howells vnode->lock_key = NULL; 683f8de483eSDavid Howells vnode->permit_cache = NULL; 684f8de483eSDavid Howells vnode->cb_interest = NULL; 685f8de483eSDavid Howells #ifdef CONFIG_AFS_FSCACHE 686f8de483eSDavid Howells vnode->cache = NULL; 687f8de483eSDavid Howells #endif 688f8de483eSDavid Howells 689260a9803SDavid Howells vnode->flags = 1 << AFS_VNODE_UNSET; 690f8de483eSDavid Howells vnode->cb_type = 0; 691f8de483eSDavid Howells vnode->lock_state = AFS_VNODE_LOCK_NONE; 6921da177e4SLinus Torvalds 6930f300ca9SDavid Howells _leave(" = %p", &vnode->vfs_inode); 6941da177e4SLinus Torvalds return &vnode->vfs_inode; 695ec26815aSDavid Howells } 6961da177e4SLinus Torvalds 697fa0d7e3dSNick Piggin static void afs_i_callback(struct rcu_head *head) 698fa0d7e3dSNick Piggin { 699fa0d7e3dSNick Piggin struct inode *inode = container_of(head, struct inode, i_rcu); 700fa0d7e3dSNick Piggin struct afs_vnode *vnode = AFS_FS_I(inode); 701fa0d7e3dSNick Piggin kmem_cache_free(afs_inode_cachep, vnode); 702fa0d7e3dSNick Piggin } 703fa0d7e3dSNick Piggin 7041da177e4SLinus Torvalds /* 7051da177e4SLinus Torvalds * destroy an AFS inode struct 7061da177e4SLinus Torvalds */ 7071da177e4SLinus Torvalds static void afs_destroy_inode(struct inode *inode) 7081da177e4SLinus Torvalds { 70908e0e7c8SDavid Howells struct afs_vnode *vnode = AFS_FS_I(inode); 71008e0e7c8SDavid Howells 7113b6492dfSDavid Howells _enter("%p{%llx:%llu}", inode, vnode->fid.vid, vnode->fid.vnode); 7121da177e4SLinus Torvalds 71308e0e7c8SDavid Howells _debug("DESTROY INODE %p", inode); 71408e0e7c8SDavid Howells 715c435ee34SDavid Howells ASSERTCMP(vnode->cb_interest, ==, NULL); 71608e0e7c8SDavid Howells 717fa0d7e3dSNick Piggin call_rcu(&inode->i_rcu, afs_i_callback); 7181da177e4SLinus Torvalds atomic_dec(&afs_count_active_inodes); 719ec26815aSDavid Howells } 72045222b9eSDavid Howells 72145222b9eSDavid Howells /* 72245222b9eSDavid Howells * return information about an AFS volume 72345222b9eSDavid Howells */ 72445222b9eSDavid Howells static int afs_statfs(struct dentry *dentry, struct kstatfs *buf) 72545222b9eSDavid Howells { 7264d673da1SDavid Howells struct afs_super_info *as = AFS_FS_S(dentry->d_sb); 727d2ddc776SDavid Howells struct afs_fs_cursor fc; 72845222b9eSDavid Howells struct afs_volume_status vs; 7292b0143b5SDavid Howells struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry)); 73045222b9eSDavid Howells struct key *key; 73145222b9eSDavid Howells int ret; 73245222b9eSDavid Howells 7334d673da1SDavid Howells buf->f_type = dentry->d_sb->s_magic; 7344d673da1SDavid Howells buf->f_bsize = AFS_BLOCK_SIZE; 7354d673da1SDavid Howells buf->f_namelen = AFSNAMEMAX - 1; 7364d673da1SDavid Howells 7374d673da1SDavid Howells if (as->dyn_root) { 7384d673da1SDavid Howells buf->f_blocks = 1; 7394d673da1SDavid Howells buf->f_bavail = 0; 7404d673da1SDavid Howells buf->f_bfree = 0; 7414d673da1SDavid Howells return 0; 7424d673da1SDavid Howells } 7434d673da1SDavid Howells 74445222b9eSDavid Howells key = afs_request_key(vnode->volume->cell); 74545222b9eSDavid Howells if (IS_ERR(key)) 74645222b9eSDavid Howells return PTR_ERR(key); 74745222b9eSDavid Howells 748d2ddc776SDavid Howells ret = -ERESTARTSYS; 749d2ddc776SDavid Howells if (afs_begin_vnode_operation(&fc, vnode, key)) { 750d2ddc776SDavid Howells fc.flags |= AFS_FS_CURSOR_NO_VSLEEP; 751d2ddc776SDavid Howells while (afs_select_fileserver(&fc)) { 75268251f0aSDavid Howells fc.cb_break = afs_calc_vnode_cb_break(vnode); 753d2ddc776SDavid Howells afs_fs_get_volume_status(&fc, &vs); 75445222b9eSDavid Howells } 75545222b9eSDavid Howells 756d2ddc776SDavid Howells afs_check_for_remote_deletion(&fc, fc.vnode); 757d2ddc776SDavid Howells afs_vnode_commit_status(&fc, vnode, fc.cb_break); 758d2ddc776SDavid Howells ret = afs_end_vnode_operation(&fc); 759d2ddc776SDavid Howells } 760d2ddc776SDavid Howells 761d2ddc776SDavid Howells key_put(key); 762d2ddc776SDavid Howells 763d2ddc776SDavid Howells if (ret == 0) { 76445222b9eSDavid Howells if (vs.max_quota == 0) 76545222b9eSDavid Howells buf->f_blocks = vs.part_max_blocks; 76645222b9eSDavid Howells else 76745222b9eSDavid Howells buf->f_blocks = vs.max_quota; 76845222b9eSDavid Howells buf->f_bavail = buf->f_bfree = buf->f_blocks - vs.blocks_in_use; 769d2ddc776SDavid Howells } 770d2ddc776SDavid Howells 771d2ddc776SDavid Howells return ret; 77245222b9eSDavid Howells } 773