super.c 12 KB

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