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