super.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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 int exofs_sync_fs(struct super_block *sb, int wait)
  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 = -ENOMEM;
  185. fscb = kzalloc(sizeof(struct exofs_fscb), GFP_KERNEL);
  186. if (!fscb) {
  187. EXOFS_ERR("exofs_write_super: memory allocation failed.\n");
  188. return -ENOMEM;
  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. return ret;
  222. }
  223. static void exofs_write_super(struct super_block *sb)
  224. {
  225. if (!(sb->s_flags & MS_RDONLY))
  226. exofs_sync_fs(sb, 1);
  227. else
  228. sb->s_dirt = 0;
  229. }
  230. /*
  231. * This function is called when the vfs is freeing the superblock. We just
  232. * need to free our own part.
  233. */
  234. static void exofs_put_super(struct super_block *sb)
  235. {
  236. int num_pend;
  237. struct exofs_sb_info *sbi = sb->s_fs_info;
  238. lock_kernel();
  239. if (sb->s_dirt)
  240. exofs_write_super(sb);
  241. /* make sure there are no pending commands */
  242. for (num_pend = atomic_read(&sbi->s_curr_pending); num_pend > 0;
  243. num_pend = atomic_read(&sbi->s_curr_pending)) {
  244. wait_queue_head_t wq;
  245. init_waitqueue_head(&wq);
  246. wait_event_timeout(wq,
  247. (atomic_read(&sbi->s_curr_pending) == 0),
  248. msecs_to_jiffies(100));
  249. }
  250. osduld_put_device(sbi->s_dev);
  251. kfree(sb->s_fs_info);
  252. sb->s_fs_info = NULL;
  253. unlock_kernel();
  254. }
  255. /*
  256. * Read the superblock from the OSD and fill in the fields
  257. */
  258. static int exofs_fill_super(struct super_block *sb, void *data, int silent)
  259. {
  260. struct inode *root;
  261. struct exofs_mountopt *opts = data;
  262. struct exofs_sb_info *sbi; /*extended info */
  263. struct exofs_fscb fscb; /*on-disk superblock info */
  264. struct osd_request *or = NULL;
  265. struct osd_obj_id obj;
  266. int ret;
  267. sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
  268. if (!sbi)
  269. return -ENOMEM;
  270. sb->s_fs_info = sbi;
  271. /* use mount options to fill superblock */
  272. sbi->s_dev = osduld_path_lookup(opts->dev_name);
  273. if (IS_ERR(sbi->s_dev)) {
  274. ret = PTR_ERR(sbi->s_dev);
  275. sbi->s_dev = NULL;
  276. goto free_sbi;
  277. }
  278. sbi->s_pid = opts->pid;
  279. sbi->s_timeout = opts->timeout;
  280. /* fill in some other data by hand */
  281. memset(sb->s_id, 0, sizeof(sb->s_id));
  282. strcpy(sb->s_id, "exofs");
  283. sb->s_blocksize = EXOFS_BLKSIZE;
  284. sb->s_blocksize_bits = EXOFS_BLKSHIFT;
  285. sb->s_maxbytes = MAX_LFS_FILESIZE;
  286. atomic_set(&sbi->s_curr_pending, 0);
  287. sb->s_bdev = NULL;
  288. sb->s_dev = 0;
  289. /* read data from on-disk superblock object */
  290. obj.partition = sbi->s_pid;
  291. obj.id = EXOFS_SUPER_ID;
  292. exofs_make_credential(sbi->s_cred, &obj);
  293. or = osd_start_request(sbi->s_dev, GFP_KERNEL);
  294. if (unlikely(!or)) {
  295. if (!silent)
  296. EXOFS_ERR(
  297. "exofs_fill_super: osd_start_request failed.\n");
  298. ret = -ENOMEM;
  299. goto free_sbi;
  300. }
  301. ret = osd_req_read_kern(or, &obj, 0, &fscb, sizeof(fscb));
  302. if (unlikely(ret)) {
  303. if (!silent)
  304. EXOFS_ERR(
  305. "exofs_fill_super: osd_req_read_kern failed.\n");
  306. ret = -ENOMEM;
  307. goto free_sbi;
  308. }
  309. ret = exofs_sync_op(or, sbi->s_timeout, sbi->s_cred);
  310. if (unlikely(ret)) {
  311. if (!silent)
  312. EXOFS_ERR("exofs_fill_super: exofs_sync_op failed.\n");
  313. ret = -EIO;
  314. goto free_sbi;
  315. }
  316. sb->s_magic = le16_to_cpu(fscb.s_magic);
  317. sbi->s_nextid = le64_to_cpu(fscb.s_nextid);
  318. sbi->s_numfiles = le32_to_cpu(fscb.s_numfiles);
  319. /* make sure what we read from the object store is correct */
  320. if (sb->s_magic != EXOFS_SUPER_MAGIC) {
  321. if (!silent)
  322. EXOFS_ERR("ERROR: Bad magic value\n");
  323. ret = -EINVAL;
  324. goto free_sbi;
  325. }
  326. /* start generation numbers from a random point */
  327. get_random_bytes(&sbi->s_next_generation, sizeof(u32));
  328. spin_lock_init(&sbi->s_next_gen_lock);
  329. /* set up operation vectors */
  330. sb->s_op = &exofs_sops;
  331. sb->s_export_op = &exofs_export_ops;
  332. root = exofs_iget(sb, EXOFS_ROOT_ID - EXOFS_OBJ_OFF);
  333. if (IS_ERR(root)) {
  334. EXOFS_ERR("ERROR: exofs_iget failed\n");
  335. ret = PTR_ERR(root);
  336. goto free_sbi;
  337. }
  338. sb->s_root = d_alloc_root(root);
  339. if (!sb->s_root) {
  340. iput(root);
  341. EXOFS_ERR("ERROR: get root inode failed\n");
  342. ret = -ENOMEM;
  343. goto free_sbi;
  344. }
  345. if (!S_ISDIR(root->i_mode)) {
  346. dput(sb->s_root);
  347. sb->s_root = NULL;
  348. EXOFS_ERR("ERROR: corrupt root inode (mode = %hd)\n",
  349. root->i_mode);
  350. ret = -EINVAL;
  351. goto free_sbi;
  352. }
  353. ret = 0;
  354. out:
  355. if (or)
  356. osd_end_request(or);
  357. return ret;
  358. free_sbi:
  359. osduld_put_device(sbi->s_dev); /* NULL safe */
  360. kfree(sbi);
  361. goto out;
  362. }
  363. /*
  364. * Set up the superblock (calls exofs_fill_super eventually)
  365. */
  366. static int exofs_get_sb(struct file_system_type *type,
  367. int flags, const char *dev_name,
  368. void *data, struct vfsmount *mnt)
  369. {
  370. struct exofs_mountopt opts;
  371. int ret;
  372. ret = parse_options(data, &opts);
  373. if (ret)
  374. return ret;
  375. opts.dev_name = dev_name;
  376. return get_sb_nodev(type, flags, &opts, exofs_fill_super, mnt);
  377. }
  378. /*
  379. * Return information about the file system state in the buffer. This is used
  380. * by the 'df' command, for example.
  381. */
  382. static int exofs_statfs(struct dentry *dentry, struct kstatfs *buf)
  383. {
  384. struct super_block *sb = dentry->d_sb;
  385. struct exofs_sb_info *sbi = sb->s_fs_info;
  386. struct osd_obj_id obj = {sbi->s_pid, 0};
  387. struct osd_attr attrs[] = {
  388. ATTR_DEF(OSD_APAGE_PARTITION_QUOTAS,
  389. OSD_ATTR_PQ_CAPACITY_QUOTA, sizeof(__be64)),
  390. ATTR_DEF(OSD_APAGE_PARTITION_INFORMATION,
  391. OSD_ATTR_PI_USED_CAPACITY, sizeof(__be64)),
  392. };
  393. uint64_t capacity = ULLONG_MAX;
  394. uint64_t used = ULLONG_MAX;
  395. struct osd_request *or;
  396. uint8_t cred_a[OSD_CAP_LEN];
  397. int ret;
  398. /* get used/capacity attributes */
  399. exofs_make_credential(cred_a, &obj);
  400. or = osd_start_request(sbi->s_dev, GFP_KERNEL);
  401. if (unlikely(!or)) {
  402. EXOFS_DBGMSG("exofs_statfs: osd_start_request failed.\n");
  403. return -ENOMEM;
  404. }
  405. osd_req_get_attributes(or, &obj);
  406. osd_req_add_get_attr_list(or, attrs, ARRAY_SIZE(attrs));
  407. ret = exofs_sync_op(or, sbi->s_timeout, cred_a);
  408. if (unlikely(ret))
  409. goto out;
  410. ret = extract_attr_from_req(or, &attrs[0]);
  411. if (likely(!ret))
  412. capacity = get_unaligned_be64(attrs[0].val_ptr);
  413. else
  414. EXOFS_DBGMSG("exofs_statfs: get capacity failed.\n");
  415. ret = extract_attr_from_req(or, &attrs[1]);
  416. if (likely(!ret))
  417. used = get_unaligned_be64(attrs[1].val_ptr);
  418. else
  419. EXOFS_DBGMSG("exofs_statfs: get used-space failed.\n");
  420. /* fill in the stats buffer */
  421. buf->f_type = EXOFS_SUPER_MAGIC;
  422. buf->f_bsize = EXOFS_BLKSIZE;
  423. buf->f_blocks = (capacity >> EXOFS_BLKSHIFT);
  424. buf->f_bfree = ((capacity - used) >> EXOFS_BLKSHIFT);
  425. buf->f_bavail = buf->f_bfree;
  426. buf->f_files = sbi->s_numfiles;
  427. buf->f_ffree = EXOFS_MAX_ID - sbi->s_numfiles;
  428. buf->f_namelen = EXOFS_NAME_LEN;
  429. out:
  430. osd_end_request(or);
  431. return ret;
  432. }
  433. static const struct super_operations exofs_sops = {
  434. .alloc_inode = exofs_alloc_inode,
  435. .destroy_inode = exofs_destroy_inode,
  436. .write_inode = exofs_write_inode,
  437. .delete_inode = exofs_delete_inode,
  438. .put_super = exofs_put_super,
  439. .write_super = exofs_write_super,
  440. .sync_fs = exofs_sync_fs,
  441. .statfs = exofs_statfs,
  442. };
  443. /******************************************************************************
  444. * EXPORT OPERATIONS
  445. *****************************************************************************/
  446. struct dentry *exofs_get_parent(struct dentry *child)
  447. {
  448. unsigned long ino = exofs_parent_ino(child);
  449. if (!ino)
  450. return NULL;
  451. return d_obtain_alias(exofs_iget(child->d_inode->i_sb, ino));
  452. }
  453. static struct inode *exofs_nfs_get_inode(struct super_block *sb,
  454. u64 ino, u32 generation)
  455. {
  456. struct inode *inode;
  457. inode = exofs_iget(sb, ino);
  458. if (IS_ERR(inode))
  459. return ERR_CAST(inode);
  460. if (generation && inode->i_generation != generation) {
  461. /* we didn't find the right inode.. */
  462. iput(inode);
  463. return ERR_PTR(-ESTALE);
  464. }
  465. return inode;
  466. }
  467. static struct dentry *exofs_fh_to_dentry(struct super_block *sb,
  468. struct fid *fid, int fh_len, int fh_type)
  469. {
  470. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  471. exofs_nfs_get_inode);
  472. }
  473. static struct dentry *exofs_fh_to_parent(struct super_block *sb,
  474. struct fid *fid, int fh_len, int fh_type)
  475. {
  476. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  477. exofs_nfs_get_inode);
  478. }
  479. static const struct export_operations exofs_export_ops = {
  480. .fh_to_dentry = exofs_fh_to_dentry,
  481. .fh_to_parent = exofs_fh_to_parent,
  482. .get_parent = exofs_get_parent,
  483. };
  484. /******************************************************************************
  485. * INSMOD/RMMOD
  486. *****************************************************************************/
  487. /*
  488. * struct that describes this file system
  489. */
  490. static struct file_system_type exofs_type = {
  491. .owner = THIS_MODULE,
  492. .name = "exofs",
  493. .get_sb = exofs_get_sb,
  494. .kill_sb = generic_shutdown_super,
  495. };
  496. static int __init init_exofs(void)
  497. {
  498. int err;
  499. err = init_inodecache();
  500. if (err)
  501. goto out;
  502. err = register_filesystem(&exofs_type);
  503. if (err)
  504. goto out_d;
  505. return 0;
  506. out_d:
  507. destroy_inodecache();
  508. out:
  509. return err;
  510. }
  511. static void __exit exit_exofs(void)
  512. {
  513. unregister_filesystem(&exofs_type);
  514. destroy_inodecache();
  515. }
  516. MODULE_AUTHOR("Avishay Traeger <avishay@gmail.com>");
  517. MODULE_DESCRIPTION("exofs");
  518. MODULE_LICENSE("GPL");
  519. module_init(init_exofs)
  520. module_exit(exit_exofs)