super.c 13 KB

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