super.c 14 KB

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