super.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*
  2. * Copyright (c) 2002 Red Hat, Inc. All rights reserved.
  3. *
  4. * This software may be freely redistributed under the terms of the
  5. * GNU General Public License.
  6. *
  7. * You should have received a copy of the GNU General Public License
  8. * along with this program; if not, write to the Free Software
  9. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  10. *
  11. * Authors: David Howells <dhowells@redhat.com>
  12. * David Woodhouse <dwmw2@cambridge.redhat.com>
  13. *
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/slab.h>
  19. #include <linux/fs.h>
  20. #include <linux/pagemap.h>
  21. #include "vnode.h"
  22. #include "volume.h"
  23. #include "cell.h"
  24. #include "cmservice.h"
  25. #include "fsclient.h"
  26. #include "super.h"
  27. #include "internal.h"
  28. #define AFS_FS_MAGIC 0x6B414653 /* 'kAFS' */
  29. struct afs_mount_params {
  30. int rwpath;
  31. struct afs_cell *default_cell;
  32. struct afs_volume *volume;
  33. };
  34. static void afs_i_init_once(void *foo, kmem_cache_t *cachep,
  35. unsigned long flags);
  36. static struct super_block *afs_get_sb(struct file_system_type *fs_type,
  37. int flags, const char *dev_name,
  38. void *data);
  39. static struct inode *afs_alloc_inode(struct super_block *sb);
  40. static void afs_put_super(struct super_block *sb);
  41. static void afs_destroy_inode(struct inode *inode);
  42. static struct file_system_type afs_fs_type = {
  43. .owner = THIS_MODULE,
  44. .name = "afs",
  45. .get_sb = afs_get_sb,
  46. .kill_sb = kill_anon_super,
  47. .fs_flags = FS_BINARY_MOUNTDATA,
  48. };
  49. static struct super_operations afs_super_ops = {
  50. .statfs = simple_statfs,
  51. .alloc_inode = afs_alloc_inode,
  52. .drop_inode = generic_delete_inode,
  53. .destroy_inode = afs_destroy_inode,
  54. .clear_inode = afs_clear_inode,
  55. .put_super = afs_put_super,
  56. };
  57. static kmem_cache_t *afs_inode_cachep;
  58. static atomic_t afs_count_active_inodes;
  59. /*****************************************************************************/
  60. /*
  61. * initialise the filesystem
  62. */
  63. int __init afs_fs_init(void)
  64. {
  65. int ret;
  66. _enter("");
  67. afs_timer_init(&afs_mntpt_expiry_timer, &afs_mntpt_expiry_timer_ops);
  68. /* create ourselves an inode cache */
  69. atomic_set(&afs_count_active_inodes, 0);
  70. ret = -ENOMEM;
  71. afs_inode_cachep = kmem_cache_create("afs_inode_cache",
  72. sizeof(struct afs_vnode),
  73. 0,
  74. SLAB_HWCACHE_ALIGN,
  75. afs_i_init_once,
  76. NULL);
  77. if (!afs_inode_cachep) {
  78. printk(KERN_NOTICE "kAFS: Failed to allocate inode cache\n");
  79. return ret;
  80. }
  81. /* now export our filesystem to lesser mortals */
  82. ret = register_filesystem(&afs_fs_type);
  83. if (ret < 0) {
  84. kmem_cache_destroy(afs_inode_cachep);
  85. kleave(" = %d", ret);
  86. return ret;
  87. }
  88. kleave(" = 0");
  89. return 0;
  90. } /* end afs_fs_init() */
  91. /*****************************************************************************/
  92. /*
  93. * clean up the filesystem
  94. */
  95. void __exit afs_fs_exit(void)
  96. {
  97. unregister_filesystem(&afs_fs_type);
  98. if (atomic_read(&afs_count_active_inodes) != 0) {
  99. printk("kAFS: %d active inode objects still present\n",
  100. atomic_read(&afs_count_active_inodes));
  101. BUG();
  102. }
  103. kmem_cache_destroy(afs_inode_cachep);
  104. } /* end afs_fs_exit() */
  105. /*****************************************************************************/
  106. /*
  107. * check that an argument has a value
  108. */
  109. static int want_arg(char **_value, const char *option)
  110. {
  111. if (!_value || !*_value || !**_value) {
  112. printk(KERN_NOTICE "kAFS: %s: argument missing\n", option);
  113. return 0;
  114. }
  115. return 1;
  116. } /* end want_arg() */
  117. /*****************************************************************************/
  118. /*
  119. * check that there's no subsequent value
  120. */
  121. static int want_no_value(char *const *_value, const char *option)
  122. {
  123. if (*_value && **_value) {
  124. printk(KERN_NOTICE "kAFS: %s: Invalid argument: %s\n",
  125. option, *_value);
  126. return 0;
  127. }
  128. return 1;
  129. } /* end want_no_value() */
  130. /*****************************************************************************/
  131. /*
  132. * parse the mount options
  133. * - this function has been shamelessly adapted from the ext3 fs which
  134. * shamelessly adapted it from the msdos fs
  135. */
  136. static int afs_super_parse_options(struct afs_mount_params *params,
  137. char *options,
  138. const char **devname)
  139. {
  140. char *key, *value;
  141. int ret;
  142. _enter("%s", options);
  143. options[PAGE_SIZE - 1] = 0;
  144. ret = 0;
  145. while ((key = strsep(&options, ",")) != 0)
  146. {
  147. value = strchr(key, '=');
  148. if (value)
  149. *value++ = 0;
  150. printk("kAFS: KEY: %s, VAL:%s\n", key, value ?: "-");
  151. if (strcmp(key, "rwpath") == 0) {
  152. if (!want_no_value(&value, "rwpath"))
  153. return -EINVAL;
  154. params->rwpath = 1;
  155. continue;
  156. }
  157. else if (strcmp(key, "vol") == 0) {
  158. if (!want_arg(&value, "vol"))
  159. return -EINVAL;
  160. *devname = value;
  161. continue;
  162. }
  163. else if (strcmp(key, "cell") == 0) {
  164. if (!want_arg(&value, "cell"))
  165. return -EINVAL;
  166. afs_put_cell(params->default_cell);
  167. ret = afs_cell_lookup(value,
  168. strlen(value),
  169. &params->default_cell);
  170. if (ret < 0)
  171. return -EINVAL;
  172. continue;
  173. }
  174. printk("kAFS: Unknown mount option: '%s'\n", key);
  175. ret = -EINVAL;
  176. goto error;
  177. }
  178. ret = 0;
  179. error:
  180. _leave(" = %d", ret);
  181. return ret;
  182. } /* end afs_super_parse_options() */
  183. /*****************************************************************************/
  184. /*
  185. * check a superblock to see if it's the one we're looking for
  186. */
  187. static int afs_test_super(struct super_block *sb, void *data)
  188. {
  189. struct afs_mount_params *params = data;
  190. struct afs_super_info *as = sb->s_fs_info;
  191. return as->volume == params->volume;
  192. } /* end afs_test_super() */
  193. /*****************************************************************************/
  194. /*
  195. * fill in the superblock
  196. */
  197. static int afs_fill_super(struct super_block *sb, void *data, int silent)
  198. {
  199. struct afs_mount_params *params = data;
  200. struct afs_super_info *as = NULL;
  201. struct afs_fid fid;
  202. struct dentry *root = NULL;
  203. struct inode *inode = NULL;
  204. int ret;
  205. kenter("");
  206. /* allocate a superblock info record */
  207. as = kmalloc(sizeof(struct afs_super_info), GFP_KERNEL);
  208. if (!as) {
  209. _leave(" = -ENOMEM");
  210. return -ENOMEM;
  211. }
  212. memset(as, 0, sizeof(struct afs_super_info));
  213. afs_get_volume(params->volume);
  214. as->volume = params->volume;
  215. /* fill in the superblock */
  216. sb->s_blocksize = PAGE_CACHE_SIZE;
  217. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  218. sb->s_magic = AFS_FS_MAGIC;
  219. sb->s_op = &afs_super_ops;
  220. sb->s_fs_info = as;
  221. /* allocate the root inode and dentry */
  222. fid.vid = as->volume->vid;
  223. fid.vnode = 1;
  224. fid.unique = 1;
  225. ret = afs_iget(sb, &fid, &inode);
  226. if (ret < 0)
  227. goto error;
  228. ret = -ENOMEM;
  229. root = d_alloc_root(inode);
  230. if (!root)
  231. goto error;
  232. sb->s_root = root;
  233. kleave(" = 0");
  234. return 0;
  235. error:
  236. iput(inode);
  237. afs_put_volume(as->volume);
  238. kfree(as);
  239. sb->s_fs_info = NULL;
  240. kleave(" = %d", ret);
  241. return ret;
  242. } /* end afs_fill_super() */
  243. /*****************************************************************************/
  244. /*
  245. * get an AFS superblock
  246. * - TODO: don't use get_sb_nodev(), but rather call sget() directly
  247. */
  248. static struct super_block *afs_get_sb(struct file_system_type *fs_type,
  249. int flags,
  250. const char *dev_name,
  251. void *options)
  252. {
  253. struct afs_mount_params params;
  254. struct super_block *sb;
  255. int ret;
  256. _enter(",,%s,%p", dev_name, options);
  257. memset(&params, 0, sizeof(params));
  258. /* start the cache manager */
  259. ret = afscm_start();
  260. if (ret < 0) {
  261. _leave(" = %d", ret);
  262. return ERR_PTR(ret);
  263. }
  264. /* parse the options */
  265. if (options) {
  266. ret = afs_super_parse_options(&params, options, &dev_name);
  267. if (ret < 0)
  268. goto error;
  269. if (!dev_name) {
  270. printk("kAFS: no volume name specified\n");
  271. ret = -EINVAL;
  272. goto error;
  273. }
  274. }
  275. /* parse the device name */
  276. ret = afs_volume_lookup(dev_name,
  277. params.default_cell,
  278. params.rwpath,
  279. &params.volume);
  280. if (ret < 0)
  281. goto error;
  282. /* allocate a deviceless superblock */
  283. sb = sget(fs_type, afs_test_super, set_anon_super, &params);
  284. if (IS_ERR(sb))
  285. goto error;
  286. sb->s_flags = flags;
  287. ret = afs_fill_super(sb, &params, flags & MS_VERBOSE ? 1 : 0);
  288. if (ret < 0) {
  289. up_write(&sb->s_umount);
  290. deactivate_super(sb);
  291. goto error;
  292. }
  293. sb->s_flags |= MS_ACTIVE;
  294. afs_put_volume(params.volume);
  295. afs_put_cell(params.default_cell);
  296. _leave(" = %p", sb);
  297. return sb;
  298. error:
  299. afs_put_volume(params.volume);
  300. afs_put_cell(params.default_cell);
  301. afscm_stop();
  302. _leave(" = %d", ret);
  303. return ERR_PTR(ret);
  304. } /* end afs_get_sb() */
  305. /*****************************************************************************/
  306. /*
  307. * finish the unmounting process on the superblock
  308. */
  309. static void afs_put_super(struct super_block *sb)
  310. {
  311. struct afs_super_info *as = sb->s_fs_info;
  312. _enter("");
  313. afs_put_volume(as->volume);
  314. afscm_stop();
  315. _leave("");
  316. } /* end afs_put_super() */
  317. /*****************************************************************************/
  318. /*
  319. * initialise an inode cache slab element prior to any use
  320. */
  321. static void afs_i_init_once(void *_vnode, kmem_cache_t *cachep,
  322. unsigned long flags)
  323. {
  324. struct afs_vnode *vnode = (struct afs_vnode *) _vnode;
  325. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  326. SLAB_CTOR_CONSTRUCTOR) {
  327. memset(vnode, 0, sizeof(*vnode));
  328. inode_init_once(&vnode->vfs_inode);
  329. init_waitqueue_head(&vnode->update_waitq);
  330. spin_lock_init(&vnode->lock);
  331. INIT_LIST_HEAD(&vnode->cb_link);
  332. INIT_LIST_HEAD(&vnode->cb_hash_link);
  333. afs_timer_init(&vnode->cb_timeout,
  334. &afs_vnode_cb_timed_out_ops);
  335. }
  336. } /* end afs_i_init_once() */
  337. /*****************************************************************************/
  338. /*
  339. * allocate an AFS inode struct from our slab cache
  340. */
  341. static struct inode *afs_alloc_inode(struct super_block *sb)
  342. {
  343. struct afs_vnode *vnode;
  344. vnode = (struct afs_vnode *)
  345. kmem_cache_alloc(afs_inode_cachep, SLAB_KERNEL);
  346. if (!vnode)
  347. return NULL;
  348. atomic_inc(&afs_count_active_inodes);
  349. memset(&vnode->fid, 0, sizeof(vnode->fid));
  350. memset(&vnode->status, 0, sizeof(vnode->status));
  351. vnode->volume = NULL;
  352. vnode->update_cnt = 0;
  353. vnode->flags = 0;
  354. return &vnode->vfs_inode;
  355. } /* end afs_alloc_inode() */
  356. /*****************************************************************************/
  357. /*
  358. * destroy an AFS inode struct
  359. */
  360. static void afs_destroy_inode(struct inode *inode)
  361. {
  362. _enter("{%lu}", inode->i_ino);
  363. kmem_cache_free(afs_inode_cachep, AFS_FS_I(inode));
  364. atomic_dec(&afs_count_active_inodes);
  365. } /* end afs_destroy_inode() */