super.c 12 KB

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