super.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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 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. .mount = afs_mount,
  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 = afs_drop_inode,
  46. .destroy_inode = afs_destroy_inode,
  47. .evict_inode = afs_evict_inode,
  48. .put_super = afs_put_super,
  49. .show_options = generic_show_options,
  50. };
  51. static struct kmem_cache *afs_inode_cachep;
  52. static atomic_t afs_count_active_inodes;
  53. enum {
  54. afs_no_opt,
  55. afs_opt_cell,
  56. afs_opt_rwpath,
  57. afs_opt_vol,
  58. afs_opt_autocell,
  59. };
  60. static const 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_opt_autocell, "autocell" },
  65. { afs_no_opt, NULL },
  66. };
  67. /*
  68. * initialise the filesystem
  69. */
  70. int __init afs_fs_init(void)
  71. {
  72. int ret;
  73. _enter("");
  74. /* create ourselves an inode cache */
  75. atomic_set(&afs_count_active_inodes, 0);
  76. ret = -ENOMEM;
  77. afs_inode_cachep = kmem_cache_create("afs_inode_cache",
  78. sizeof(struct afs_vnode),
  79. 0,
  80. SLAB_HWCACHE_ALIGN,
  81. afs_i_init_once);
  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. false);
  135. if (IS_ERR(cell))
  136. return PTR_ERR(cell);
  137. afs_put_cell(params->cell);
  138. params->cell = cell;
  139. break;
  140. case afs_opt_rwpath:
  141. params->rwpath = 1;
  142. break;
  143. case afs_opt_vol:
  144. *devname = args[0].from;
  145. break;
  146. case afs_opt_autocell:
  147. params->autocell = 1;
  148. break;
  149. default:
  150. printk(KERN_ERR "kAFS:"
  151. " Unknown or invalid mount option: '%s'\n", p);
  152. return -EINVAL;
  153. }
  154. }
  155. _leave(" = 0");
  156. return 0;
  157. }
  158. /*
  159. * parse a device name to get cell name, volume name, volume type and R/W
  160. * selector
  161. * - this can be one of the following:
  162. * "%[cell:]volume[.]" R/W volume
  163. * "#[cell:]volume[.]" R/O or R/W volume (rwpath=0),
  164. * or R/W (rwpath=1) volume
  165. * "%[cell:]volume.readonly" R/O volume
  166. * "#[cell:]volume.readonly" R/O volume
  167. * "%[cell:]volume.backup" Backup volume
  168. * "#[cell:]volume.backup" Backup volume
  169. */
  170. static int afs_parse_device_name(struct afs_mount_params *params,
  171. const char *name)
  172. {
  173. struct afs_cell *cell;
  174. const char *cellname, *suffix;
  175. int cellnamesz;
  176. _enter(",%s", name);
  177. if (!name) {
  178. printk(KERN_ERR "kAFS: no volume name specified\n");
  179. return -EINVAL;
  180. }
  181. if ((name[0] != '%' && name[0] != '#') || !name[1]) {
  182. printk(KERN_ERR "kAFS: unparsable volume name\n");
  183. return -EINVAL;
  184. }
  185. /* determine the type of volume we're looking for */
  186. params->type = AFSVL_ROVOL;
  187. params->force = false;
  188. if (params->rwpath || name[0] == '%') {
  189. params->type = AFSVL_RWVOL;
  190. params->force = true;
  191. }
  192. name++;
  193. /* split the cell name out if there is one */
  194. params->volname = strchr(name, ':');
  195. if (params->volname) {
  196. cellname = name;
  197. cellnamesz = params->volname - name;
  198. params->volname++;
  199. } else {
  200. params->volname = name;
  201. cellname = NULL;
  202. cellnamesz = 0;
  203. }
  204. /* the volume type is further affected by a possible suffix */
  205. suffix = strrchr(params->volname, '.');
  206. if (suffix) {
  207. if (strcmp(suffix, ".readonly") == 0) {
  208. params->type = AFSVL_ROVOL;
  209. params->force = true;
  210. } else if (strcmp(suffix, ".backup") == 0) {
  211. params->type = AFSVL_BACKVOL;
  212. params->force = true;
  213. } else if (suffix[1] == 0) {
  214. } else {
  215. suffix = NULL;
  216. }
  217. }
  218. params->volnamesz = suffix ?
  219. suffix - params->volname : strlen(params->volname);
  220. _debug("cell %*.*s [%p]",
  221. cellnamesz, cellnamesz, cellname ?: "", params->cell);
  222. /* lookup the cell record */
  223. if (cellname || !params->cell) {
  224. cell = afs_cell_lookup(cellname, cellnamesz, true);
  225. if (IS_ERR(cell)) {
  226. printk(KERN_ERR "kAFS: unable to lookup cell '%*.*s'\n",
  227. cellnamesz, cellnamesz, cellname ?: "");
  228. return PTR_ERR(cell);
  229. }
  230. afs_put_cell(params->cell);
  231. params->cell = cell;
  232. }
  233. _debug("CELL:%s [%p] VOLUME:%*.*s SUFFIX:%s TYPE:%d%s",
  234. params->cell->name, params->cell,
  235. params->volnamesz, params->volnamesz, params->volname,
  236. suffix ?: "-", params->type, params->force ? " FORCE" : "");
  237. return 0;
  238. }
  239. /*
  240. * check a superblock to see if it's the one we're looking for
  241. */
  242. static int afs_test_super(struct super_block *sb, void *data)
  243. {
  244. struct afs_mount_params *params = data;
  245. struct afs_super_info *as = sb->s_fs_info;
  246. return as->volume == params->volume;
  247. }
  248. /*
  249. * fill in the superblock
  250. */
  251. static int afs_fill_super(struct super_block *sb, void *data)
  252. {
  253. struct afs_mount_params *params = data;
  254. struct afs_super_info *as = NULL;
  255. struct afs_fid fid;
  256. struct dentry *root = NULL;
  257. struct inode *inode = NULL;
  258. int ret;
  259. _enter("");
  260. /* allocate a superblock info record */
  261. as = kzalloc(sizeof(struct afs_super_info), GFP_KERNEL);
  262. if (!as) {
  263. _leave(" = -ENOMEM");
  264. return -ENOMEM;
  265. }
  266. afs_get_volume(params->volume);
  267. as->volume = params->volume;
  268. /* fill in the superblock */
  269. sb->s_blocksize = PAGE_CACHE_SIZE;
  270. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  271. sb->s_magic = AFS_FS_MAGIC;
  272. sb->s_op = &afs_super_ops;
  273. sb->s_fs_info = as;
  274. sb->s_bdi = &as->volume->bdi;
  275. /* allocate the root inode and dentry */
  276. fid.vid = as->volume->vid;
  277. fid.vnode = 1;
  278. fid.unique = 1;
  279. inode = afs_iget(sb, params->key, &fid, NULL, NULL);
  280. if (IS_ERR(inode))
  281. goto error_inode;
  282. if (params->autocell)
  283. set_bit(AFS_VNODE_AUTOCELL, &AFS_FS_I(inode)->flags);
  284. ret = -ENOMEM;
  285. root = d_alloc_root(inode);
  286. if (!root)
  287. goto error;
  288. sb->s_d_op = &afs_fs_dentry_operations;
  289. sb->s_root = root;
  290. _leave(" = 0");
  291. return 0;
  292. error_inode:
  293. ret = PTR_ERR(inode);
  294. inode = NULL;
  295. error:
  296. iput(inode);
  297. afs_put_volume(as->volume);
  298. kfree(as);
  299. sb->s_fs_info = NULL;
  300. _leave(" = %d", ret);
  301. return ret;
  302. }
  303. /*
  304. * get an AFS superblock
  305. */
  306. static struct dentry *afs_mount(struct file_system_type *fs_type,
  307. int flags, const char *dev_name, void *options)
  308. {
  309. struct afs_mount_params params;
  310. struct super_block *sb;
  311. struct afs_volume *vol;
  312. struct key *key;
  313. char *new_opts = kstrdup(options, GFP_KERNEL);
  314. int ret;
  315. _enter(",,%s,%p", dev_name, options);
  316. memset(&params, 0, sizeof(params));
  317. /* parse the options and device name */
  318. if (options) {
  319. ret = afs_parse_options(&params, options, &dev_name);
  320. if (ret < 0)
  321. goto error;
  322. }
  323. ret = afs_parse_device_name(&params, dev_name);
  324. if (ret < 0)
  325. goto error;
  326. /* try and do the mount securely */
  327. key = afs_request_key(params.cell);
  328. if (IS_ERR(key)) {
  329. _leave(" = %ld [key]", PTR_ERR(key));
  330. ret = PTR_ERR(key);
  331. goto error;
  332. }
  333. params.key = key;
  334. /* parse the device name */
  335. vol = afs_volume_lookup(&params);
  336. if (IS_ERR(vol)) {
  337. ret = PTR_ERR(vol);
  338. goto error;
  339. }
  340. params.volume = vol;
  341. /* allocate a deviceless superblock */
  342. sb = sget(fs_type, afs_test_super, set_anon_super, &params);
  343. if (IS_ERR(sb)) {
  344. ret = PTR_ERR(sb);
  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. }
  362. afs_put_volume(params.volume);
  363. afs_put_cell(params.cell);
  364. kfree(new_opts);
  365. _leave(" = 0 [%p]", sb);
  366. return dget(sb->s_root);
  367. error:
  368. afs_put_volume(params.volume);
  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. /*
  376. * finish the unmounting process on the superblock
  377. */
  378. static void afs_put_super(struct super_block *sb)
  379. {
  380. struct afs_super_info *as = sb->s_fs_info;
  381. _enter("");
  382. afs_put_volume(as->volume);
  383. _leave("");
  384. }
  385. /*
  386. * initialise an inode cache slab element prior to any use
  387. */
  388. static void afs_i_init_once(void *_vnode)
  389. {
  390. struct afs_vnode *vnode = _vnode;
  391. memset(vnode, 0, sizeof(*vnode));
  392. inode_init_once(&vnode->vfs_inode);
  393. init_waitqueue_head(&vnode->update_waitq);
  394. mutex_init(&vnode->permits_lock);
  395. mutex_init(&vnode->validate_lock);
  396. spin_lock_init(&vnode->writeback_lock);
  397. spin_lock_init(&vnode->lock);
  398. INIT_LIST_HEAD(&vnode->writebacks);
  399. INIT_LIST_HEAD(&vnode->pending_locks);
  400. INIT_LIST_HEAD(&vnode->granted_locks);
  401. INIT_DELAYED_WORK(&vnode->lock_work, afs_lock_work);
  402. INIT_WORK(&vnode->cb_broken_work, afs_broken_callback_work);
  403. }
  404. /*
  405. * allocate an AFS inode struct from our slab cache
  406. */
  407. static struct inode *afs_alloc_inode(struct super_block *sb)
  408. {
  409. struct afs_vnode *vnode;
  410. vnode = kmem_cache_alloc(afs_inode_cachep, GFP_KERNEL);
  411. if (!vnode)
  412. return NULL;
  413. atomic_inc(&afs_count_active_inodes);
  414. memset(&vnode->fid, 0, sizeof(vnode->fid));
  415. memset(&vnode->status, 0, sizeof(vnode->status));
  416. vnode->volume = NULL;
  417. vnode->update_cnt = 0;
  418. vnode->flags = 1 << AFS_VNODE_UNSET;
  419. vnode->cb_promised = false;
  420. _leave(" = %p", &vnode->vfs_inode);
  421. return &vnode->vfs_inode;
  422. }
  423. static void afs_i_callback(struct rcu_head *head)
  424. {
  425. struct inode *inode = container_of(head, struct inode, i_rcu);
  426. struct afs_vnode *vnode = AFS_FS_I(inode);
  427. INIT_LIST_HEAD(&inode->i_dentry);
  428. kmem_cache_free(afs_inode_cachep, vnode);
  429. }
  430. /*
  431. * destroy an AFS inode struct
  432. */
  433. static void afs_destroy_inode(struct inode *inode)
  434. {
  435. struct afs_vnode *vnode = AFS_FS_I(inode);
  436. _enter("%p{%x:%u}", inode, vnode->fid.vid, vnode->fid.vnode);
  437. _debug("DESTROY INODE %p", inode);
  438. ASSERTCMP(vnode->server, ==, NULL);
  439. call_rcu(&inode->i_rcu, afs_i_callback);
  440. atomic_dec(&afs_count_active_inodes);
  441. }
  442. /*
  443. * return information about an AFS volume
  444. */
  445. static int afs_statfs(struct dentry *dentry, struct kstatfs *buf)
  446. {
  447. struct afs_volume_status vs;
  448. struct afs_vnode *vnode = AFS_FS_I(dentry->d_inode);
  449. struct key *key;
  450. int ret;
  451. key = afs_request_key(vnode->volume->cell);
  452. if (IS_ERR(key))
  453. return PTR_ERR(key);
  454. ret = afs_vnode_get_volume_status(vnode, key, &vs);
  455. key_put(key);
  456. if (ret < 0) {
  457. _leave(" = %d", ret);
  458. return ret;
  459. }
  460. buf->f_type = dentry->d_sb->s_magic;
  461. buf->f_bsize = AFS_BLOCK_SIZE;
  462. buf->f_namelen = AFSNAMEMAX - 1;
  463. if (vs.max_quota == 0)
  464. buf->f_blocks = vs.part_max_blocks;
  465. else
  466. buf->f_blocks = vs.max_quota;
  467. buf->f_bavail = buf->f_bfree = buf->f_blocks - vs.blocks_in_use;
  468. return 0;
  469. }