super.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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 "internal.h"
  23. #define AFS_FS_MAGIC 0x6B414653 /* 'kAFS' */
  24. static void afs_i_init_once(void *foo, struct kmem_cache *cachep,
  25. unsigned long flags);
  26. static int afs_get_sb(struct file_system_type *fs_type,
  27. int flags, const char *dev_name,
  28. void *data, struct vfsmount *mnt);
  29. static struct inode *afs_alloc_inode(struct super_block *sb);
  30. static void afs_put_super(struct super_block *sb);
  31. static void afs_destroy_inode(struct inode *inode);
  32. struct file_system_type afs_fs_type = {
  33. .owner = THIS_MODULE,
  34. .name = "afs",
  35. .get_sb = afs_get_sb,
  36. .kill_sb = kill_anon_super,
  37. .fs_flags = FS_BINARY_MOUNTDATA,
  38. };
  39. static const struct super_operations afs_super_ops = {
  40. .statfs = simple_statfs,
  41. .alloc_inode = afs_alloc_inode,
  42. .drop_inode = generic_delete_inode,
  43. .destroy_inode = afs_destroy_inode,
  44. .clear_inode = afs_clear_inode,
  45. .umount_begin = afs_umount_begin,
  46. .put_super = afs_put_super,
  47. };
  48. static struct kmem_cache *afs_inode_cachep;
  49. static atomic_t afs_count_active_inodes;
  50. /*
  51. * initialise the filesystem
  52. */
  53. int __init afs_fs_init(void)
  54. {
  55. int ret;
  56. _enter("");
  57. /* create ourselves an inode cache */
  58. atomic_set(&afs_count_active_inodes, 0);
  59. ret = -ENOMEM;
  60. afs_inode_cachep = kmem_cache_create("afs_inode_cache",
  61. sizeof(struct afs_vnode),
  62. 0,
  63. SLAB_HWCACHE_ALIGN,
  64. afs_i_init_once,
  65. NULL);
  66. if (!afs_inode_cachep) {
  67. printk(KERN_NOTICE "kAFS: Failed to allocate inode cache\n");
  68. return ret;
  69. }
  70. /* now export our filesystem to lesser mortals */
  71. ret = register_filesystem(&afs_fs_type);
  72. if (ret < 0) {
  73. kmem_cache_destroy(afs_inode_cachep);
  74. _leave(" = %d", ret);
  75. return ret;
  76. }
  77. _leave(" = 0");
  78. return 0;
  79. }
  80. /*
  81. * clean up the filesystem
  82. */
  83. void __exit afs_fs_exit(void)
  84. {
  85. _enter("");
  86. afs_mntpt_kill_timer();
  87. unregister_filesystem(&afs_fs_type);
  88. if (atomic_read(&afs_count_active_inodes) != 0) {
  89. printk("kAFS: %d active inode objects still present\n",
  90. atomic_read(&afs_count_active_inodes));
  91. BUG();
  92. }
  93. kmem_cache_destroy(afs_inode_cachep);
  94. _leave("");
  95. }
  96. /*
  97. * check that an argument has a value
  98. */
  99. static int want_arg(char **_value, const char *option)
  100. {
  101. if (!_value || !*_value || !**_value) {
  102. printk(KERN_NOTICE "kAFS: %s: argument missing\n", option);
  103. return 0;
  104. }
  105. return 1;
  106. }
  107. /*
  108. * check that there's no subsequent value
  109. */
  110. static int want_no_value(char *const *_value, const char *option)
  111. {
  112. if (*_value && **_value) {
  113. printk(KERN_NOTICE "kAFS: %s: Invalid argument: %s\n",
  114. option, *_value);
  115. return 0;
  116. }
  117. return 1;
  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. char *key, *value;
  129. int ret;
  130. _enter("%s", options);
  131. options[PAGE_SIZE - 1] = 0;
  132. ret = 0;
  133. while ((key = strsep(&options, ","))) {
  134. value = strchr(key, '=');
  135. if (value)
  136. *value++ = 0;
  137. _debug("kAFS: KEY: %s, VAL:%s", key, value ?: "-");
  138. if (strcmp(key, "rwpath") == 0) {
  139. if (!want_no_value(&value, "rwpath"))
  140. return -EINVAL;
  141. params->rwpath = 1;
  142. } else if (strcmp(key, "vol") == 0) {
  143. if (!want_arg(&value, "vol"))
  144. return -EINVAL;
  145. *devname = value;
  146. } else if (strcmp(key, "cell") == 0) {
  147. if (!want_arg(&value, "cell"))
  148. return -EINVAL;
  149. cell = afs_cell_lookup(value, strlen(value));
  150. if (IS_ERR(cell))
  151. return PTR_ERR(cell);
  152. afs_put_cell(params->cell);
  153. params->cell = cell;
  154. } else {
  155. printk("kAFS: Unknown mount option: '%s'\n", key);
  156. ret = -EINVAL;
  157. goto error;
  158. }
  159. }
  160. ret = 0;
  161. error:
  162. _leave(" = %d", ret);
  163. return ret;
  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);
  232. if (IS_ERR(cell)) {
  233. printk(KERN_ERR "kAFS: unable to lookup cell '%s'\n",
  234. 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_mount_params *params = data;
  252. struct afs_super_info *as = sb->s_fs_info;
  253. return as->volume == params->volume;
  254. }
  255. /*
  256. * fill in the superblock
  257. */
  258. static int afs_fill_super(struct super_block *sb, void *data)
  259. {
  260. struct afs_mount_params *params = data;
  261. struct afs_super_info *as = NULL;
  262. struct afs_fid fid;
  263. struct dentry *root = NULL;
  264. struct inode *inode = NULL;
  265. int ret;
  266. _enter("");
  267. /* allocate a superblock info record */
  268. as = kzalloc(sizeof(struct afs_super_info), GFP_KERNEL);
  269. if (!as) {
  270. _leave(" = -ENOMEM");
  271. return -ENOMEM;
  272. }
  273. afs_get_volume(params->volume);
  274. as->volume = params->volume;
  275. /* fill in the superblock */
  276. sb->s_blocksize = PAGE_CACHE_SIZE;
  277. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  278. sb->s_magic = AFS_FS_MAGIC;
  279. sb->s_op = &afs_super_ops;
  280. sb->s_fs_info = as;
  281. /* allocate the root inode and dentry */
  282. fid.vid = as->volume->vid;
  283. fid.vnode = 1;
  284. fid.unique = 1;
  285. inode = afs_iget(sb, params->key, &fid);
  286. if (IS_ERR(inode))
  287. goto error_inode;
  288. ret = -ENOMEM;
  289. root = d_alloc_root(inode);
  290. if (!root)
  291. goto error;
  292. sb->s_root = root;
  293. _leave(" = 0");
  294. return 0;
  295. error_inode:
  296. ret = PTR_ERR(inode);
  297. inode = NULL;
  298. error:
  299. iput(inode);
  300. afs_put_volume(as->volume);
  301. kfree(as);
  302. sb->s_fs_info = NULL;
  303. _leave(" = %d", ret);
  304. return ret;
  305. }
  306. /*
  307. * get an AFS superblock
  308. * - TODO: don't use get_sb_nodev(), but rather call sget() directly
  309. */
  310. static int afs_get_sb(struct file_system_type *fs_type,
  311. int flags,
  312. const char *dev_name,
  313. void *options,
  314. struct vfsmount *mnt)
  315. {
  316. struct afs_mount_params params;
  317. struct super_block *sb;
  318. struct afs_volume *vol;
  319. struct key *key;
  320. int ret;
  321. _enter(",,%s,%p", dev_name, options);
  322. memset(&params, 0, sizeof(params));
  323. /* parse the options and device name */
  324. if (options) {
  325. ret = afs_parse_options(&params, options, &dev_name);
  326. if (ret < 0)
  327. goto error;
  328. }
  329. ret = afs_parse_device_name(&params, dev_name);
  330. if (ret < 0)
  331. goto error;
  332. /* try and do the mount securely */
  333. key = afs_request_key(params.cell);
  334. if (IS_ERR(key)) {
  335. _leave(" = %ld [key]", PTR_ERR(key));
  336. ret = PTR_ERR(key);
  337. goto error;
  338. }
  339. params.key = key;
  340. /* parse the device name */
  341. vol = afs_volume_lookup(&params);
  342. if (IS_ERR(vol)) {
  343. ret = PTR_ERR(vol);
  344. goto error;
  345. }
  346. params.volume = vol;
  347. /* allocate a deviceless superblock */
  348. sb = sget(fs_type, afs_test_super, set_anon_super, &params);
  349. if (IS_ERR(sb)) {
  350. ret = PTR_ERR(sb);
  351. goto error;
  352. }
  353. if (!sb->s_root) {
  354. /* initial superblock/root creation */
  355. _debug("create");
  356. sb->s_flags = flags;
  357. ret = afs_fill_super(sb, &params);
  358. if (ret < 0) {
  359. up_write(&sb->s_umount);
  360. deactivate_super(sb);
  361. goto error;
  362. }
  363. sb->s_flags |= MS_ACTIVE;
  364. } else {
  365. _debug("reuse");
  366. ASSERTCMP(sb->s_flags, &, MS_ACTIVE);
  367. }
  368. simple_set_mnt(mnt, sb);
  369. afs_put_volume(params.volume);
  370. afs_put_cell(params.cell);
  371. _leave(" = 0 [%p]", sb);
  372. return 0;
  373. error:
  374. afs_put_volume(params.volume);
  375. afs_put_cell(params.cell);
  376. key_put(params.key);
  377. _leave(" = %d", ret);
  378. return ret;
  379. }
  380. /*
  381. * finish the unmounting process on the superblock
  382. */
  383. static void afs_put_super(struct super_block *sb)
  384. {
  385. struct afs_super_info *as = sb->s_fs_info;
  386. _enter("");
  387. afs_put_volume(as->volume);
  388. _leave("");
  389. }
  390. /*
  391. * initialise an inode cache slab element prior to any use
  392. */
  393. static void afs_i_init_once(void *_vnode, struct kmem_cache *cachep,
  394. unsigned long flags)
  395. {
  396. struct afs_vnode *vnode = _vnode;
  397. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  398. SLAB_CTOR_CONSTRUCTOR) {
  399. memset(vnode, 0, sizeof(*vnode));
  400. inode_init_once(&vnode->vfs_inode);
  401. init_waitqueue_head(&vnode->update_waitq);
  402. mutex_init(&vnode->permits_lock);
  403. spin_lock_init(&vnode->lock);
  404. INIT_WORK(&vnode->cb_broken_work, afs_broken_callback_work);
  405. mutex_init(&vnode->cb_broken_lock);
  406. }
  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 = 0;
  423. vnode->cb_promised = false;
  424. return &vnode->vfs_inode;
  425. }
  426. /*
  427. * destroy an AFS inode struct
  428. */
  429. static void afs_destroy_inode(struct inode *inode)
  430. {
  431. struct afs_vnode *vnode = AFS_FS_I(inode);
  432. _enter("{%lu}", inode->i_ino);
  433. _debug("DESTROY INODE %p", inode);
  434. ASSERTCMP(vnode->server, ==, NULL);
  435. kmem_cache_free(afs_inode_cachep, vnode);
  436. atomic_dec(&afs_count_active_inodes);
  437. }