super.c 15 KB

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