super.c 11 KB

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