super.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. struct afs_mount_params {
  25. int rwpath;
  26. struct afs_cell *default_cell;
  27. struct afs_volume *volume;
  28. };
  29. static void afs_i_init_once(void *foo, struct kmem_cache *cachep,
  30. unsigned long flags);
  31. static int afs_get_sb(struct file_system_type *fs_type,
  32. int flags, const char *dev_name,
  33. void *data, struct vfsmount *mnt);
  34. static struct inode *afs_alloc_inode(struct super_block *sb);
  35. static void afs_put_super(struct super_block *sb);
  36. static void afs_destroy_inode(struct inode *inode);
  37. struct file_system_type afs_fs_type = {
  38. .owner = THIS_MODULE,
  39. .name = "afs",
  40. .get_sb = afs_get_sb,
  41. .kill_sb = kill_anon_super,
  42. .fs_flags = FS_BINARY_MOUNTDATA,
  43. };
  44. static const struct super_operations afs_super_ops = {
  45. .statfs = simple_statfs,
  46. .alloc_inode = afs_alloc_inode,
  47. .drop_inode = generic_delete_inode,
  48. .destroy_inode = afs_destroy_inode,
  49. .clear_inode = afs_clear_inode,
  50. .umount_begin = afs_umount_begin,
  51. .put_super = afs_put_super,
  52. };
  53. static struct kmem_cache *afs_inode_cachep;
  54. static atomic_t afs_count_active_inodes;
  55. /*
  56. * initialise the filesystem
  57. */
  58. int __init afs_fs_init(void)
  59. {
  60. int ret;
  61. _enter("");
  62. /* create ourselves an inode cache */
  63. atomic_set(&afs_count_active_inodes, 0);
  64. ret = -ENOMEM;
  65. afs_inode_cachep = kmem_cache_create("afs_inode_cache",
  66. sizeof(struct afs_vnode),
  67. 0,
  68. SLAB_HWCACHE_ALIGN,
  69. afs_i_init_once,
  70. NULL);
  71. if (!afs_inode_cachep) {
  72. printk(KERN_NOTICE "kAFS: Failed to allocate inode cache\n");
  73. return ret;
  74. }
  75. /* now export our filesystem to lesser mortals */
  76. ret = register_filesystem(&afs_fs_type);
  77. if (ret < 0) {
  78. kmem_cache_destroy(afs_inode_cachep);
  79. _leave(" = %d", ret);
  80. return ret;
  81. }
  82. _leave(" = 0");
  83. return 0;
  84. }
  85. /*
  86. * clean up the filesystem
  87. */
  88. void __exit afs_fs_exit(void)
  89. {
  90. _enter("");
  91. afs_mntpt_kill_timer();
  92. unregister_filesystem(&afs_fs_type);
  93. if (atomic_read(&afs_count_active_inodes) != 0) {
  94. printk("kAFS: %d active inode objects still present\n",
  95. atomic_read(&afs_count_active_inodes));
  96. BUG();
  97. }
  98. kmem_cache_destroy(afs_inode_cachep);
  99. _leave("");
  100. }
  101. /*
  102. * check that an argument has a value
  103. */
  104. static int want_arg(char **_value, const char *option)
  105. {
  106. if (!_value || !*_value || !**_value) {
  107. printk(KERN_NOTICE "kAFS: %s: argument missing\n", option);
  108. return 0;
  109. }
  110. return 1;
  111. }
  112. /*
  113. * check that there's no subsequent value
  114. */
  115. static int want_no_value(char *const *_value, const char *option)
  116. {
  117. if (*_value && **_value) {
  118. printk(KERN_NOTICE "kAFS: %s: Invalid argument: %s\n",
  119. option, *_value);
  120. return 0;
  121. }
  122. return 1;
  123. }
  124. /*
  125. * parse the mount options
  126. * - this function has been shamelessly adapted from the ext3 fs which
  127. * shamelessly adapted it from the msdos fs
  128. */
  129. static int afs_super_parse_options(struct afs_mount_params *params,
  130. char *options, const char **devname)
  131. {
  132. struct afs_cell *cell;
  133. char *key, *value;
  134. int ret;
  135. _enter("%s", options);
  136. options[PAGE_SIZE - 1] = 0;
  137. ret = 0;
  138. while ((key = strsep(&options, ","))) {
  139. value = strchr(key, '=');
  140. if (value)
  141. *value++ = 0;
  142. _debug("kAFS: KEY: %s, VAL:%s", key, value ?: "-");
  143. if (strcmp(key, "rwpath") == 0) {
  144. if (!want_no_value(&value, "rwpath"))
  145. return -EINVAL;
  146. params->rwpath = 1;
  147. } else if (strcmp(key, "vol") == 0) {
  148. if (!want_arg(&value, "vol"))
  149. return -EINVAL;
  150. *devname = value;
  151. } else if (strcmp(key, "cell") == 0) {
  152. if (!want_arg(&value, "cell"))
  153. return -EINVAL;
  154. cell = afs_cell_lookup(value, strlen(value));
  155. if (IS_ERR(cell))
  156. return PTR_ERR(cell);
  157. afs_put_cell(params->default_cell);
  158. params->default_cell = cell;
  159. } else {
  160. printk("kAFS: Unknown mount option: '%s'\n", key);
  161. ret = -EINVAL;
  162. goto error;
  163. }
  164. }
  165. ret = 0;
  166. error:
  167. _leave(" = %d", ret);
  168. return ret;
  169. }
  170. /*
  171. * check a superblock to see if it's the one we're looking for
  172. */
  173. static int afs_test_super(struct super_block *sb, void *data)
  174. {
  175. struct afs_mount_params *params = data;
  176. struct afs_super_info *as = sb->s_fs_info;
  177. return as->volume == params->volume;
  178. }
  179. /*
  180. * fill in the superblock
  181. */
  182. static int afs_fill_super(struct super_block *sb, void *data)
  183. {
  184. struct afs_mount_params *params = data;
  185. struct afs_super_info *as = NULL;
  186. struct afs_fid fid;
  187. struct dentry *root = NULL;
  188. struct inode *inode = NULL;
  189. int ret;
  190. _enter("");
  191. /* allocate a superblock info record */
  192. as = kzalloc(sizeof(struct afs_super_info), GFP_KERNEL);
  193. if (!as) {
  194. _leave(" = -ENOMEM");
  195. return -ENOMEM;
  196. }
  197. afs_get_volume(params->volume);
  198. as->volume = params->volume;
  199. /* fill in the superblock */
  200. sb->s_blocksize = PAGE_CACHE_SIZE;
  201. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  202. sb->s_magic = AFS_FS_MAGIC;
  203. sb->s_op = &afs_super_ops;
  204. sb->s_fs_info = as;
  205. /* allocate the root inode and dentry */
  206. fid.vid = as->volume->vid;
  207. fid.vnode = 1;
  208. fid.unique = 1;
  209. inode = afs_iget(sb, &fid);
  210. if (IS_ERR(inode))
  211. goto error_inode;
  212. ret = -ENOMEM;
  213. root = d_alloc_root(inode);
  214. if (!root)
  215. goto error;
  216. sb->s_root = root;
  217. _leave(" = 0");
  218. return 0;
  219. error_inode:
  220. ret = PTR_ERR(inode);
  221. inode = NULL;
  222. error:
  223. iput(inode);
  224. afs_put_volume(as->volume);
  225. kfree(as);
  226. sb->s_fs_info = NULL;
  227. _leave(" = %d", ret);
  228. return ret;
  229. }
  230. /*
  231. * get an AFS superblock
  232. * - TODO: don't use get_sb_nodev(), but rather call sget() directly
  233. */
  234. static int afs_get_sb(struct file_system_type *fs_type,
  235. int flags,
  236. const char *dev_name,
  237. void *options,
  238. struct vfsmount *mnt)
  239. {
  240. struct afs_mount_params params;
  241. struct super_block *sb;
  242. struct afs_volume *vol;
  243. int ret;
  244. _enter(",,%s,%p", dev_name, options);
  245. memset(&params, 0, sizeof(params));
  246. /* parse the options */
  247. if (options) {
  248. ret = afs_super_parse_options(&params, options, &dev_name);
  249. if (ret < 0)
  250. goto error;
  251. if (!dev_name) {
  252. printk("kAFS: no volume name specified\n");
  253. ret = -EINVAL;
  254. goto error;
  255. }
  256. }
  257. /* parse the device name */
  258. vol = afs_volume_lookup(dev_name, params.default_cell, params.rwpath);
  259. if (IS_ERR(vol)) {
  260. ret = PTR_ERR(vol);
  261. goto error;
  262. }
  263. params.volume = vol;
  264. /* allocate a deviceless superblock */
  265. sb = sget(fs_type, afs_test_super, set_anon_super, &params);
  266. if (IS_ERR(sb)) {
  267. ret = PTR_ERR(sb);
  268. goto error;
  269. }
  270. if (!sb->s_root) {
  271. /* initial superblock/root creation */
  272. _debug("create");
  273. sb->s_flags = flags;
  274. ret = afs_fill_super(sb, &params);
  275. if (ret < 0) {
  276. up_write(&sb->s_umount);
  277. deactivate_super(sb);
  278. goto error;
  279. }
  280. sb->s_flags |= MS_ACTIVE;
  281. } else {
  282. _debug("reuse");
  283. ASSERTCMP(sb->s_flags, &, MS_ACTIVE);
  284. }
  285. simple_set_mnt(mnt, sb);
  286. afs_put_volume(params.volume);
  287. afs_put_cell(params.default_cell);
  288. _leave(" = 0 [%p]", sb);
  289. return 0;
  290. error:
  291. afs_put_volume(params.volume);
  292. afs_put_cell(params.default_cell);
  293. _leave(" = %d", ret);
  294. return ret;
  295. }
  296. /*
  297. * finish the unmounting process on the superblock
  298. */
  299. static void afs_put_super(struct super_block *sb)
  300. {
  301. struct afs_super_info *as = sb->s_fs_info;
  302. _enter("");
  303. afs_put_volume(as->volume);
  304. _leave("");
  305. }
  306. /*
  307. * initialise an inode cache slab element prior to any use
  308. */
  309. static void afs_i_init_once(void *_vnode, struct kmem_cache *cachep,
  310. unsigned long flags)
  311. {
  312. struct afs_vnode *vnode = _vnode;
  313. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  314. SLAB_CTOR_CONSTRUCTOR) {
  315. memset(vnode, 0, sizeof(*vnode));
  316. inode_init_once(&vnode->vfs_inode);
  317. init_waitqueue_head(&vnode->update_waitq);
  318. spin_lock_init(&vnode->lock);
  319. INIT_WORK(&vnode->cb_broken_work, afs_broken_callback_work);
  320. mutex_init(&vnode->cb_broken_lock);
  321. }
  322. }
  323. /*
  324. * allocate an AFS inode struct from our slab cache
  325. */
  326. static struct inode *afs_alloc_inode(struct super_block *sb)
  327. {
  328. struct afs_vnode *vnode;
  329. vnode = kmem_cache_alloc(afs_inode_cachep, GFP_KERNEL);
  330. if (!vnode)
  331. return NULL;
  332. atomic_inc(&afs_count_active_inodes);
  333. memset(&vnode->fid, 0, sizeof(vnode->fid));
  334. memset(&vnode->status, 0, sizeof(vnode->status));
  335. vnode->volume = NULL;
  336. vnode->update_cnt = 0;
  337. vnode->flags = 0;
  338. vnode->cb_promised = false;
  339. return &vnode->vfs_inode;
  340. }
  341. /*
  342. * destroy an AFS inode struct
  343. */
  344. static void afs_destroy_inode(struct inode *inode)
  345. {
  346. struct afs_vnode *vnode = AFS_FS_I(inode);
  347. _enter("{%lu}", inode->i_ino);
  348. _debug("DESTROY INODE %p", inode);
  349. ASSERTCMP(vnode->server, ==, NULL);
  350. kmem_cache_free(afs_inode_cachep, vnode);
  351. atomic_dec(&afs_count_active_inodes);
  352. }