super.c 14 KB

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