super.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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@infradead.org>
  14. *
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/mount.h>
  19. #include <linux/init.h>
  20. #include <linux/slab.h>
  21. #include <linux/fs.h>
  22. #include <linux/pagemap.h>
  23. #include <linux/parser.h>
  24. #include <linux/statfs.h>
  25. #include <linux/sched.h>
  26. #include "internal.h"
  27. #define AFS_FS_MAGIC 0x6B414653 /* 'kAFS' */
  28. static void afs_i_init_once(void *foo);
  29. static struct dentry *afs_mount(struct file_system_type *fs_type,
  30. int flags, const char *dev_name, void *data);
  31. static void afs_kill_super(struct super_block *sb);
  32. static struct inode *afs_alloc_inode(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. .mount = afs_mount,
  39. .kill_sb = afs_kill_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 = afs_drop_inode,
  46. .destroy_inode = afs_destroy_inode,
  47. .evict_inode = afs_evict_inode,
  48. .show_options = generic_show_options,
  49. };
  50. static struct kmem_cache *afs_inode_cachep;
  51. static atomic_t afs_count_active_inodes;
  52. enum {
  53. afs_no_opt,
  54. afs_opt_cell,
  55. afs_opt_rwpath,
  56. afs_opt_vol,
  57. afs_opt_autocell,
  58. };
  59. static const match_table_t afs_options_list = {
  60. { afs_opt_cell, "cell=%s" },
  61. { afs_opt_rwpath, "rwpath" },
  62. { afs_opt_vol, "vol=%s" },
  63. { afs_opt_autocell, "autocell" },
  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. if (!afs_inode_cachep) {
  82. printk(KERN_NOTICE "kAFS: Failed to allocate inode cache\n");
  83. return ret;
  84. }
  85. /* now export our filesystem to lesser mortals */
  86. ret = register_filesystem(&afs_fs_type);
  87. if (ret < 0) {
  88. kmem_cache_destroy(afs_inode_cachep);
  89. _leave(" = %d", ret);
  90. return ret;
  91. }
  92. _leave(" = 0");
  93. return 0;
  94. }
  95. /*
  96. * clean up the filesystem
  97. */
  98. void __exit afs_fs_exit(void)
  99. {
  100. _enter("");
  101. afs_mntpt_kill_timer();
  102. unregister_filesystem(&afs_fs_type);
  103. if (atomic_read(&afs_count_active_inodes) != 0) {
  104. printk("kAFS: %d active inode objects still present\n",
  105. atomic_read(&afs_count_active_inodes));
  106. BUG();
  107. }
  108. kmem_cache_destroy(afs_inode_cachep);
  109. _leave("");
  110. }
  111. /*
  112. * parse the mount options
  113. * - this function has been shamelessly adapted from the ext3 fs which
  114. * shamelessly adapted it from the msdos fs
  115. */
  116. static int afs_parse_options(struct afs_mount_params *params,
  117. char *options, const char **devname)
  118. {
  119. struct afs_cell *cell;
  120. substring_t args[MAX_OPT_ARGS];
  121. char *p;
  122. int token;
  123. _enter("%s", options);
  124. options[PAGE_SIZE - 1] = 0;
  125. while ((p = strsep(&options, ","))) {
  126. if (!*p)
  127. continue;
  128. token = match_token(p, afs_options_list, args);
  129. switch (token) {
  130. case afs_opt_cell:
  131. cell = afs_cell_lookup(args[0].from,
  132. args[0].to - args[0].from,
  133. false);
  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. case afs_opt_autocell:
  146. params->autocell = 1;
  147. break;
  148. default:
  149. printk(KERN_ERR "kAFS:"
  150. " Unknown or invalid mount option: '%s'\n", p);
  151. return -EINVAL;
  152. }
  153. }
  154. _leave(" = 0");
  155. return 0;
  156. }
  157. /*
  158. * parse a device name to get cell name, volume name, volume type and R/W
  159. * selector
  160. * - this can be one of the following:
  161. * "%[cell:]volume[.]" R/W volume
  162. * "#[cell:]volume[.]" R/O or R/W volume (rwpath=0),
  163. * or R/W (rwpath=1) volume
  164. * "%[cell:]volume.readonly" R/O volume
  165. * "#[cell:]volume.readonly" R/O volume
  166. * "%[cell:]volume.backup" Backup volume
  167. * "#[cell:]volume.backup" Backup volume
  168. */
  169. static int afs_parse_device_name(struct afs_mount_params *params,
  170. const char *name)
  171. {
  172. struct afs_cell *cell;
  173. const char *cellname, *suffix;
  174. int cellnamesz;
  175. _enter(",%s", name);
  176. if (!name) {
  177. printk(KERN_ERR "kAFS: no volume name specified\n");
  178. return -EINVAL;
  179. }
  180. if ((name[0] != '%' && name[0] != '#') || !name[1]) {
  181. printk(KERN_ERR "kAFS: unparsable volume name\n");
  182. return -EINVAL;
  183. }
  184. /* determine the type of volume we're looking for */
  185. params->type = AFSVL_ROVOL;
  186. params->force = false;
  187. if (params->rwpath || name[0] == '%') {
  188. params->type = AFSVL_RWVOL;
  189. params->force = true;
  190. }
  191. name++;
  192. /* split the cell name out if there is one */
  193. params->volname = strchr(name, ':');
  194. if (params->volname) {
  195. cellname = name;
  196. cellnamesz = params->volname - name;
  197. params->volname++;
  198. } else {
  199. params->volname = name;
  200. cellname = NULL;
  201. cellnamesz = 0;
  202. }
  203. /* the volume type is further affected by a possible suffix */
  204. suffix = strrchr(params->volname, '.');
  205. if (suffix) {
  206. if (strcmp(suffix, ".readonly") == 0) {
  207. params->type = AFSVL_ROVOL;
  208. params->force = true;
  209. } else if (strcmp(suffix, ".backup") == 0) {
  210. params->type = AFSVL_BACKVOL;
  211. params->force = true;
  212. } else if (suffix[1] == 0) {
  213. } else {
  214. suffix = NULL;
  215. }
  216. }
  217. params->volnamesz = suffix ?
  218. suffix - params->volname : strlen(params->volname);
  219. _debug("cell %*.*s [%p]",
  220. cellnamesz, cellnamesz, cellname ?: "", params->cell);
  221. /* lookup the cell record */
  222. if (cellname || !params->cell) {
  223. cell = afs_cell_lookup(cellname, cellnamesz, true);
  224. if (IS_ERR(cell)) {
  225. printk(KERN_ERR "kAFS: unable to lookup cell '%*.*s'\n",
  226. cellnamesz, cellnamesz, cellname ?: "");
  227. return PTR_ERR(cell);
  228. }
  229. afs_put_cell(params->cell);
  230. params->cell = cell;
  231. }
  232. _debug("CELL:%s [%p] VOLUME:%*.*s SUFFIX:%s TYPE:%d%s",
  233. params->cell->name, params->cell,
  234. params->volnamesz, params->volnamesz, params->volname,
  235. suffix ?: "-", params->type, params->force ? " FORCE" : "");
  236. return 0;
  237. }
  238. /*
  239. * check a superblock to see if it's the one we're looking for
  240. */
  241. static int afs_test_super(struct super_block *sb, void *data)
  242. {
  243. struct afs_super_info *as1 = data;
  244. struct afs_super_info *as = sb->s_fs_info;
  245. return as->volume == as1->volume;
  246. }
  247. static int afs_set_super(struct super_block *sb, void *data)
  248. {
  249. sb->s_fs_info = data;
  250. return set_anon_super(sb, NULL);
  251. }
  252. /*
  253. * fill in the superblock
  254. */
  255. static int afs_fill_super(struct super_block *sb,
  256. struct afs_mount_params *params)
  257. {
  258. struct afs_super_info *as = sb->s_fs_info;
  259. struct afs_fid fid;
  260. struct dentry *root = NULL;
  261. struct inode *inode = NULL;
  262. int ret;
  263. _enter("");
  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_bdi = &as->volume->bdi;
  270. strlcpy(sb->s_id, as->volume->vlocation->vldb.name, sizeof(sb->s_id));
  271. /* allocate the root inode and dentry */
  272. fid.vid = as->volume->vid;
  273. fid.vnode = 1;
  274. fid.unique = 1;
  275. inode = afs_iget(sb, params->key, &fid, NULL, NULL);
  276. if (IS_ERR(inode))
  277. return PTR_ERR(inode);
  278. if (params->autocell)
  279. set_bit(AFS_VNODE_AUTOCELL, &AFS_FS_I(inode)->flags);
  280. ret = -ENOMEM;
  281. root = d_alloc_root(inode);
  282. if (!root)
  283. goto error;
  284. sb->s_d_op = &afs_fs_dentry_operations;
  285. sb->s_root = root;
  286. _leave(" = 0");
  287. return 0;
  288. error:
  289. iput(inode);
  290. _leave(" = %d", ret);
  291. return ret;
  292. }
  293. /*
  294. * get an AFS superblock
  295. */
  296. static struct dentry *afs_mount(struct file_system_type *fs_type,
  297. int flags, const char *dev_name, void *options)
  298. {
  299. struct afs_mount_params params;
  300. struct super_block *sb;
  301. struct afs_volume *vol;
  302. struct key *key;
  303. char *new_opts = kstrdup(options, GFP_KERNEL);
  304. struct afs_super_info *as;
  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. /* allocate a superblock info record */
  332. as = kzalloc(sizeof(struct afs_super_info), GFP_KERNEL);
  333. if (!as) {
  334. ret = -ENOMEM;
  335. afs_put_volume(vol);
  336. goto error;
  337. }
  338. as->volume = vol;
  339. /* allocate a deviceless superblock */
  340. sb = sget(fs_type, afs_test_super, afs_set_super, as);
  341. if (IS_ERR(sb)) {
  342. ret = PTR_ERR(sb);
  343. afs_put_volume(vol);
  344. kfree(as);
  345. goto error;
  346. }
  347. if (!sb->s_root) {
  348. /* initial superblock/root creation */
  349. _debug("create");
  350. sb->s_flags = flags;
  351. ret = afs_fill_super(sb, &params);
  352. if (ret < 0) {
  353. deactivate_locked_super(sb);
  354. goto error;
  355. }
  356. save_mount_options(sb, new_opts);
  357. sb->s_flags |= MS_ACTIVE;
  358. } else {
  359. _debug("reuse");
  360. ASSERTCMP(sb->s_flags, &, MS_ACTIVE);
  361. afs_put_volume(vol);
  362. kfree(as);
  363. }
  364. afs_put_cell(params.cell);
  365. kfree(new_opts);
  366. _leave(" = 0 [%p]", sb);
  367. return dget(sb->s_root);
  368. error:
  369. afs_put_cell(params.cell);
  370. key_put(params.key);
  371. kfree(new_opts);
  372. _leave(" = %d", ret);
  373. return ERR_PTR(ret);
  374. }
  375. static void afs_kill_super(struct super_block *sb)
  376. {
  377. struct afs_super_info *as = sb->s_fs_info;
  378. kill_anon_super(sb);
  379. afs_put_volume(as->volume);
  380. kfree(as);
  381. }
  382. /*
  383. * initialise an inode cache slab element prior to any use
  384. */
  385. static void afs_i_init_once(void *_vnode)
  386. {
  387. struct afs_vnode *vnode = _vnode;
  388. memset(vnode, 0, sizeof(*vnode));
  389. inode_init_once(&vnode->vfs_inode);
  390. init_waitqueue_head(&vnode->update_waitq);
  391. mutex_init(&vnode->permits_lock);
  392. mutex_init(&vnode->validate_lock);
  393. spin_lock_init(&vnode->writeback_lock);
  394. spin_lock_init(&vnode->lock);
  395. INIT_LIST_HEAD(&vnode->writebacks);
  396. INIT_LIST_HEAD(&vnode->pending_locks);
  397. INIT_LIST_HEAD(&vnode->granted_locks);
  398. INIT_DELAYED_WORK(&vnode->lock_work, afs_lock_work);
  399. INIT_WORK(&vnode->cb_broken_work, afs_broken_callback_work);
  400. }
  401. /*
  402. * allocate an AFS inode struct from our slab cache
  403. */
  404. static struct inode *afs_alloc_inode(struct super_block *sb)
  405. {
  406. struct afs_vnode *vnode;
  407. vnode = kmem_cache_alloc(afs_inode_cachep, GFP_KERNEL);
  408. if (!vnode)
  409. return NULL;
  410. atomic_inc(&afs_count_active_inodes);
  411. memset(&vnode->fid, 0, sizeof(vnode->fid));
  412. memset(&vnode->status, 0, sizeof(vnode->status));
  413. vnode->volume = NULL;
  414. vnode->update_cnt = 0;
  415. vnode->flags = 1 << AFS_VNODE_UNSET;
  416. vnode->cb_promised = false;
  417. _leave(" = %p", &vnode->vfs_inode);
  418. return &vnode->vfs_inode;
  419. }
  420. static void afs_i_callback(struct rcu_head *head)
  421. {
  422. struct inode *inode = container_of(head, struct inode, i_rcu);
  423. struct afs_vnode *vnode = AFS_FS_I(inode);
  424. INIT_LIST_HEAD(&inode->i_dentry);
  425. kmem_cache_free(afs_inode_cachep, vnode);
  426. }
  427. /*
  428. * destroy an AFS inode struct
  429. */
  430. static void afs_destroy_inode(struct inode *inode)
  431. {
  432. struct afs_vnode *vnode = AFS_FS_I(inode);
  433. _enter("%p{%x:%u}", inode, vnode->fid.vid, vnode->fid.vnode);
  434. _debug("DESTROY INODE %p", inode);
  435. ASSERTCMP(vnode->server, ==, NULL);
  436. call_rcu(&inode->i_rcu, afs_i_callback);
  437. atomic_dec(&afs_count_active_inodes);
  438. }
  439. /*
  440. * return information about an AFS volume
  441. */
  442. static int afs_statfs(struct dentry *dentry, struct kstatfs *buf)
  443. {
  444. struct afs_volume_status vs;
  445. struct afs_vnode *vnode = AFS_FS_I(dentry->d_inode);
  446. struct key *key;
  447. int ret;
  448. key = afs_request_key(vnode->volume->cell);
  449. if (IS_ERR(key))
  450. return PTR_ERR(key);
  451. ret = afs_vnode_get_volume_status(vnode, key, &vs);
  452. key_put(key);
  453. if (ret < 0) {
  454. _leave(" = %d", ret);
  455. return ret;
  456. }
  457. buf->f_type = dentry->d_sb->s_magic;
  458. buf->f_bsize = AFS_BLOCK_SIZE;
  459. buf->f_namelen = AFSNAMEMAX - 1;
  460. if (vs.max_quota == 0)
  461. buf->f_blocks = vs.part_max_blocks;
  462. else
  463. buf->f_blocks = vs.max_quota;
  464. buf->f_bavail = buf->f_bfree = buf->f_blocks - vs.blocks_in_use;
  465. return 0;
  466. }