super.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. /*
  2. * Copyright (C) 2005, 2006
  3. * Avishay Traeger (avishay@gmail.com) (avishay@il.ibm.com)
  4. * Copyright (C) 2005, 2006
  5. * International Business Machines
  6. * Copyright (C) 2008, 2009
  7. * Boaz Harrosh <bharrosh@panasas.com>
  8. *
  9. * Copyrights for code taken from ext2:
  10. * Copyright (C) 1992, 1993, 1994, 1995
  11. * Remy Card (card@masi.ibp.fr)
  12. * Laboratoire MASI - Institut Blaise Pascal
  13. * Universite Pierre et Marie Curie (Paris VI)
  14. * from
  15. * linux/fs/minix/inode.c
  16. * Copyright (C) 1991, 1992 Linus Torvalds
  17. *
  18. * This file is part of exofs.
  19. *
  20. * exofs is free software; you can redistribute it and/or modify
  21. * it under the terms of the GNU General Public License as published by
  22. * the Free Software Foundation. Since it is based on ext2, and the only
  23. * valid version of GPL for the Linux kernel is version 2, the only valid
  24. * version of GPL for exofs is version 2.
  25. *
  26. * exofs is distributed in the hope that it will be useful,
  27. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. * GNU General Public License for more details.
  30. *
  31. * You should have received a copy of the GNU General Public License
  32. * along with exofs; if not, write to the Free Software
  33. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  34. */
  35. #include <linux/string.h>
  36. #include <linux/parser.h>
  37. #include <linux/vfs.h>
  38. #include <linux/random.h>
  39. #include <linux/exportfs.h>
  40. #include "exofs.h"
  41. /******************************************************************************
  42. * MOUNT OPTIONS
  43. *****************************************************************************/
  44. /*
  45. * struct to hold what we get from mount options
  46. */
  47. struct exofs_mountopt {
  48. const char *dev_name;
  49. uint64_t pid;
  50. int timeout;
  51. };
  52. /*
  53. * exofs-specific mount-time options.
  54. */
  55. enum { Opt_pid, Opt_to, Opt_mkfs, Opt_format, Opt_err };
  56. /*
  57. * Our mount-time options. These should ideally be 64-bit unsigned, but the
  58. * kernel's parsing functions do not currently support that. 32-bit should be
  59. * sufficient for most applications now.
  60. */
  61. static match_table_t tokens = {
  62. {Opt_pid, "pid=%u"},
  63. {Opt_to, "to=%u"},
  64. {Opt_err, NULL}
  65. };
  66. /*
  67. * The main option parsing method. Also makes sure that all of the mandatory
  68. * mount options were set.
  69. */
  70. static int parse_options(char *options, struct exofs_mountopt *opts)
  71. {
  72. char *p;
  73. substring_t args[MAX_OPT_ARGS];
  74. int option;
  75. bool s_pid = false;
  76. EXOFS_DBGMSG("parse_options %s\n", options);
  77. /* defaults */
  78. memset(opts, 0, sizeof(*opts));
  79. opts->timeout = BLK_DEFAULT_SG_TIMEOUT;
  80. while ((p = strsep(&options, ",")) != NULL) {
  81. int token;
  82. char str[32];
  83. if (!*p)
  84. continue;
  85. token = match_token(p, tokens, args);
  86. switch (token) {
  87. case Opt_pid:
  88. if (0 == match_strlcpy(str, &args[0], sizeof(str)))
  89. return -EINVAL;
  90. opts->pid = simple_strtoull(str, NULL, 0);
  91. if (opts->pid < EXOFS_MIN_PID) {
  92. EXOFS_ERR("Partition ID must be >= %u",
  93. EXOFS_MIN_PID);
  94. return -EINVAL;
  95. }
  96. s_pid = 1;
  97. break;
  98. case Opt_to:
  99. if (match_int(&args[0], &option))
  100. return -EINVAL;
  101. if (option <= 0) {
  102. EXOFS_ERR("Timout must be > 0");
  103. return -EINVAL;
  104. }
  105. opts->timeout = option * HZ;
  106. break;
  107. }
  108. }
  109. if (!s_pid) {
  110. EXOFS_ERR("Need to specify the following options:\n");
  111. EXOFS_ERR(" -o pid=pid_no_to_use\n");
  112. return -EINVAL;
  113. }
  114. return 0;
  115. }
  116. /******************************************************************************
  117. * INODE CACHE
  118. *****************************************************************************/
  119. /*
  120. * Our inode cache. Isn't it pretty?
  121. */
  122. static struct kmem_cache *exofs_inode_cachep;
  123. /*
  124. * Allocate an inode in the cache
  125. */
  126. static struct inode *exofs_alloc_inode(struct super_block *sb)
  127. {
  128. struct exofs_i_info *oi;
  129. oi = kmem_cache_alloc(exofs_inode_cachep, GFP_KERNEL);
  130. if (!oi)
  131. return NULL;
  132. oi->vfs_inode.i_version = 1;
  133. return &oi->vfs_inode;
  134. }
  135. /*
  136. * Remove an inode from the cache
  137. */
  138. static void exofs_destroy_inode(struct inode *inode)
  139. {
  140. kmem_cache_free(exofs_inode_cachep, exofs_i(inode));
  141. }
  142. /*
  143. * Initialize the inode
  144. */
  145. static void exofs_init_once(void *foo)
  146. {
  147. struct exofs_i_info *oi = foo;
  148. inode_init_once(&oi->vfs_inode);
  149. }
  150. /*
  151. * Create and initialize the inode cache
  152. */
  153. static int init_inodecache(void)
  154. {
  155. exofs_inode_cachep = kmem_cache_create("exofs_inode_cache",
  156. sizeof(struct exofs_i_info), 0,
  157. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
  158. exofs_init_once);
  159. if (exofs_inode_cachep == NULL)
  160. return -ENOMEM;
  161. return 0;
  162. }
  163. /*
  164. * Destroy the inode cache
  165. */
  166. static void destroy_inodecache(void)
  167. {
  168. kmem_cache_destroy(exofs_inode_cachep);
  169. }
  170. /******************************************************************************
  171. * SUPERBLOCK FUNCTIONS
  172. *****************************************************************************/
  173. static const struct super_operations exofs_sops;
  174. static const struct export_operations exofs_export_ops;
  175. /*
  176. * Write the superblock to the OSD
  177. */
  178. static void exofs_write_super(struct super_block *sb)
  179. {
  180. struct exofs_sb_info *sbi;
  181. struct exofs_fscb *fscb;
  182. struct osd_request *or;
  183. struct osd_obj_id obj;
  184. int ret;
  185. fscb = kzalloc(sizeof(struct exofs_fscb), GFP_KERNEL);
  186. if (!fscb) {
  187. EXOFS_ERR("exofs_write_super: memory allocation failed.\n");
  188. return;
  189. }
  190. lock_super(sb);
  191. lock_kernel();
  192. sbi = sb->s_fs_info;
  193. fscb->s_nextid = cpu_to_le64(sbi->s_nextid);
  194. fscb->s_numfiles = cpu_to_le32(sbi->s_numfiles);
  195. fscb->s_magic = cpu_to_le16(sb->s_magic);
  196. fscb->s_newfs = 0;
  197. or = osd_start_request(sbi->s_dev, GFP_KERNEL);
  198. if (unlikely(!or)) {
  199. EXOFS_ERR("exofs_write_super: osd_start_request failed.\n");
  200. goto out;
  201. }
  202. obj.partition = sbi->s_pid;
  203. obj.id = EXOFS_SUPER_ID;
  204. ret = osd_req_write_kern(or, &obj, 0, fscb, sizeof(*fscb));
  205. if (unlikely(ret)) {
  206. EXOFS_ERR("exofs_write_super: osd_req_write_kern failed.\n");
  207. goto out;
  208. }
  209. ret = exofs_sync_op(or, sbi->s_timeout, sbi->s_cred);
  210. if (unlikely(ret)) {
  211. EXOFS_ERR("exofs_write_super: exofs_sync_op failed.\n");
  212. goto out;
  213. }
  214. sb->s_dirt = 0;
  215. out:
  216. if (or)
  217. osd_end_request(or);
  218. unlock_kernel();
  219. unlock_super(sb);
  220. kfree(fscb);
  221. }
  222. /*
  223. * This function is called when the vfs is freeing the superblock. We just
  224. * need to free our own part.
  225. */
  226. static void exofs_put_super(struct super_block *sb)
  227. {
  228. int num_pend;
  229. struct exofs_sb_info *sbi = sb->s_fs_info;
  230. lock_kernel();
  231. if (sb->s_dirt)
  232. exofs_write_super(sb);
  233. /* make sure there are no pending commands */
  234. for (num_pend = atomic_read(&sbi->s_curr_pending); num_pend > 0;
  235. num_pend = atomic_read(&sbi->s_curr_pending)) {
  236. wait_queue_head_t wq;
  237. init_waitqueue_head(&wq);
  238. wait_event_timeout(wq,
  239. (atomic_read(&sbi->s_curr_pending) == 0),
  240. msecs_to_jiffies(100));
  241. }
  242. osduld_put_device(sbi->s_dev);
  243. kfree(sb->s_fs_info);
  244. sb->s_fs_info = NULL;
  245. unlock_kernel();
  246. }
  247. /*
  248. * Read the superblock from the OSD and fill in the fields
  249. */
  250. static int exofs_fill_super(struct super_block *sb, void *data, int silent)
  251. {
  252. struct inode *root;
  253. struct exofs_mountopt *opts = data;
  254. struct exofs_sb_info *sbi; /*extended info */
  255. struct exofs_fscb fscb; /*on-disk superblock info */
  256. struct osd_request *or = NULL;
  257. struct osd_obj_id obj;
  258. int ret;
  259. sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
  260. if (!sbi)
  261. return -ENOMEM;
  262. sb->s_fs_info = sbi;
  263. /* use mount options to fill superblock */
  264. sbi->s_dev = osduld_path_lookup(opts->dev_name);
  265. if (IS_ERR(sbi->s_dev)) {
  266. ret = PTR_ERR(sbi->s_dev);
  267. sbi->s_dev = NULL;
  268. goto free_sbi;
  269. }
  270. sbi->s_pid = opts->pid;
  271. sbi->s_timeout = opts->timeout;
  272. /* fill in some other data by hand */
  273. memset(sb->s_id, 0, sizeof(sb->s_id));
  274. strcpy(sb->s_id, "exofs");
  275. sb->s_blocksize = EXOFS_BLKSIZE;
  276. sb->s_blocksize_bits = EXOFS_BLKSHIFT;
  277. sb->s_maxbytes = MAX_LFS_FILESIZE;
  278. atomic_set(&sbi->s_curr_pending, 0);
  279. sb->s_bdev = NULL;
  280. sb->s_dev = 0;
  281. /* read data from on-disk superblock object */
  282. obj.partition = sbi->s_pid;
  283. obj.id = EXOFS_SUPER_ID;
  284. exofs_make_credential(sbi->s_cred, &obj);
  285. or = osd_start_request(sbi->s_dev, GFP_KERNEL);
  286. if (unlikely(!or)) {
  287. if (!silent)
  288. EXOFS_ERR(
  289. "exofs_fill_super: osd_start_request failed.\n");
  290. ret = -ENOMEM;
  291. goto free_sbi;
  292. }
  293. ret = osd_req_read_kern(or, &obj, 0, &fscb, sizeof(fscb));
  294. if (unlikely(ret)) {
  295. if (!silent)
  296. EXOFS_ERR(
  297. "exofs_fill_super: osd_req_read_kern failed.\n");
  298. ret = -ENOMEM;
  299. goto free_sbi;
  300. }
  301. ret = exofs_sync_op(or, sbi->s_timeout, sbi->s_cred);
  302. if (unlikely(ret)) {
  303. if (!silent)
  304. EXOFS_ERR("exofs_fill_super: exofs_sync_op failed.\n");
  305. ret = -EIO;
  306. goto free_sbi;
  307. }
  308. sb->s_magic = le16_to_cpu(fscb.s_magic);
  309. sbi->s_nextid = le64_to_cpu(fscb.s_nextid);
  310. sbi->s_numfiles = le32_to_cpu(fscb.s_numfiles);
  311. /* make sure what we read from the object store is correct */
  312. if (sb->s_magic != EXOFS_SUPER_MAGIC) {
  313. if (!silent)
  314. EXOFS_ERR("ERROR: Bad magic value\n");
  315. ret = -EINVAL;
  316. goto free_sbi;
  317. }
  318. /* start generation numbers from a random point */
  319. get_random_bytes(&sbi->s_next_generation, sizeof(u32));
  320. spin_lock_init(&sbi->s_next_gen_lock);
  321. /* set up operation vectors */
  322. sb->s_op = &exofs_sops;
  323. sb->s_export_op = &exofs_export_ops;
  324. root = exofs_iget(sb, EXOFS_ROOT_ID - EXOFS_OBJ_OFF);
  325. if (IS_ERR(root)) {
  326. EXOFS_ERR("ERROR: exofs_iget failed\n");
  327. ret = PTR_ERR(root);
  328. goto free_sbi;
  329. }
  330. sb->s_root = d_alloc_root(root);
  331. if (!sb->s_root) {
  332. iput(root);
  333. EXOFS_ERR("ERROR: get root inode failed\n");
  334. ret = -ENOMEM;
  335. goto free_sbi;
  336. }
  337. if (!S_ISDIR(root->i_mode)) {
  338. dput(sb->s_root);
  339. sb->s_root = NULL;
  340. EXOFS_ERR("ERROR: corrupt root inode (mode = %hd)\n",
  341. root->i_mode);
  342. ret = -EINVAL;
  343. goto free_sbi;
  344. }
  345. ret = 0;
  346. out:
  347. if (or)
  348. osd_end_request(or);
  349. return ret;
  350. free_sbi:
  351. osduld_put_device(sbi->s_dev); /* NULL safe */
  352. kfree(sbi);
  353. goto out;
  354. }
  355. /*
  356. * Set up the superblock (calls exofs_fill_super eventually)
  357. */
  358. static int exofs_get_sb(struct file_system_type *type,
  359. int flags, const char *dev_name,
  360. void *data, struct vfsmount *mnt)
  361. {
  362. struct exofs_mountopt opts;
  363. int ret;
  364. ret = parse_options(data, &opts);
  365. if (ret)
  366. return ret;
  367. opts.dev_name = dev_name;
  368. return get_sb_nodev(type, flags, &opts, exofs_fill_super, mnt);
  369. }
  370. /*
  371. * Return information about the file system state in the buffer. This is used
  372. * by the 'df' command, for example.
  373. */
  374. static int exofs_statfs(struct dentry *dentry, struct kstatfs *buf)
  375. {
  376. struct super_block *sb = dentry->d_sb;
  377. struct exofs_sb_info *sbi = sb->s_fs_info;
  378. struct osd_obj_id obj = {sbi->s_pid, 0};
  379. struct osd_attr attrs[] = {
  380. ATTR_DEF(OSD_APAGE_PARTITION_QUOTAS,
  381. OSD_ATTR_PQ_CAPACITY_QUOTA, sizeof(__be64)),
  382. ATTR_DEF(OSD_APAGE_PARTITION_INFORMATION,
  383. OSD_ATTR_PI_USED_CAPACITY, sizeof(__be64)),
  384. };
  385. uint64_t capacity = ULLONG_MAX;
  386. uint64_t used = ULLONG_MAX;
  387. struct osd_request *or;
  388. uint8_t cred_a[OSD_CAP_LEN];
  389. int ret;
  390. /* get used/capacity attributes */
  391. exofs_make_credential(cred_a, &obj);
  392. or = osd_start_request(sbi->s_dev, GFP_KERNEL);
  393. if (unlikely(!or)) {
  394. EXOFS_DBGMSG("exofs_statfs: osd_start_request failed.\n");
  395. return -ENOMEM;
  396. }
  397. osd_req_get_attributes(or, &obj);
  398. osd_req_add_get_attr_list(or, attrs, ARRAY_SIZE(attrs));
  399. ret = exofs_sync_op(or, sbi->s_timeout, cred_a);
  400. if (unlikely(ret))
  401. goto out;
  402. ret = extract_attr_from_req(or, &attrs[0]);
  403. if (likely(!ret))
  404. capacity = get_unaligned_be64(attrs[0].val_ptr);
  405. else
  406. EXOFS_DBGMSG("exofs_statfs: get capacity failed.\n");
  407. ret = extract_attr_from_req(or, &attrs[1]);
  408. if (likely(!ret))
  409. used = get_unaligned_be64(attrs[1].val_ptr);
  410. else
  411. EXOFS_DBGMSG("exofs_statfs: get used-space failed.\n");
  412. /* fill in the stats buffer */
  413. buf->f_type = EXOFS_SUPER_MAGIC;
  414. buf->f_bsize = EXOFS_BLKSIZE;
  415. buf->f_blocks = (capacity >> EXOFS_BLKSHIFT);
  416. buf->f_bfree = ((capacity - used) >> EXOFS_BLKSHIFT);
  417. buf->f_bavail = buf->f_bfree;
  418. buf->f_files = sbi->s_numfiles;
  419. buf->f_ffree = EXOFS_MAX_ID - sbi->s_numfiles;
  420. buf->f_namelen = EXOFS_NAME_LEN;
  421. out:
  422. osd_end_request(or);
  423. return ret;
  424. }
  425. static const struct super_operations exofs_sops = {
  426. .alloc_inode = exofs_alloc_inode,
  427. .destroy_inode = exofs_destroy_inode,
  428. .write_inode = exofs_write_inode,
  429. .delete_inode = exofs_delete_inode,
  430. .put_super = exofs_put_super,
  431. .write_super = exofs_write_super,
  432. .statfs = exofs_statfs,
  433. };
  434. /******************************************************************************
  435. * EXPORT OPERATIONS
  436. *****************************************************************************/
  437. struct dentry *exofs_get_parent(struct dentry *child)
  438. {
  439. unsigned long ino = exofs_parent_ino(child);
  440. if (!ino)
  441. return NULL;
  442. return d_obtain_alias(exofs_iget(child->d_inode->i_sb, ino));
  443. }
  444. static struct inode *exofs_nfs_get_inode(struct super_block *sb,
  445. u64 ino, u32 generation)
  446. {
  447. struct inode *inode;
  448. inode = exofs_iget(sb, ino);
  449. if (IS_ERR(inode))
  450. return ERR_CAST(inode);
  451. if (generation && inode->i_generation != generation) {
  452. /* we didn't find the right inode.. */
  453. iput(inode);
  454. return ERR_PTR(-ESTALE);
  455. }
  456. return inode;
  457. }
  458. static struct dentry *exofs_fh_to_dentry(struct super_block *sb,
  459. struct fid *fid, int fh_len, int fh_type)
  460. {
  461. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  462. exofs_nfs_get_inode);
  463. }
  464. static struct dentry *exofs_fh_to_parent(struct super_block *sb,
  465. struct fid *fid, int fh_len, int fh_type)
  466. {
  467. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  468. exofs_nfs_get_inode);
  469. }
  470. static const struct export_operations exofs_export_ops = {
  471. .fh_to_dentry = exofs_fh_to_dentry,
  472. .fh_to_parent = exofs_fh_to_parent,
  473. .get_parent = exofs_get_parent,
  474. };
  475. /******************************************************************************
  476. * INSMOD/RMMOD
  477. *****************************************************************************/
  478. /*
  479. * struct that describes this file system
  480. */
  481. static struct file_system_type exofs_type = {
  482. .owner = THIS_MODULE,
  483. .name = "exofs",
  484. .get_sb = exofs_get_sb,
  485. .kill_sb = generic_shutdown_super,
  486. };
  487. static int __init init_exofs(void)
  488. {
  489. int err;
  490. err = init_inodecache();
  491. if (err)
  492. goto out;
  493. err = register_filesystem(&exofs_type);
  494. if (err)
  495. goto out_d;
  496. return 0;
  497. out_d:
  498. destroy_inodecache();
  499. out:
  500. return err;
  501. }
  502. static void __exit exit_exofs(void)
  503. {
  504. unregister_filesystem(&exofs_type);
  505. destroy_inodecache();
  506. }
  507. MODULE_AUTHOR("Avishay Traeger <avishay@gmail.com>");
  508. MODULE_DESCRIPTION("exofs");
  509. MODULE_LICENSE("GPL");
  510. module_init(init_exofs)
  511. module_exit(exit_exofs)