super.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  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/string.h>
  34. #include <linux/parser.h>
  35. #include <linux/vfs.h>
  36. #include <linux/random.h>
  37. #include <linux/exportfs.h>
  38. #include <linux/slab.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. bool is_osdname;
  48. const char *dev_name;
  49. uint64_t pid;
  50. int timeout;
  51. };
  52. /*
  53. * exofs-specific mount-time options.
  54. */
  55. enum { Opt_name, Opt_pid, Opt_to, 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_name, "osdname=%s"},
  63. {Opt_pid, "pid=%u"},
  64. {Opt_to, "to=%u"},
  65. {Opt_err, NULL}
  66. };
  67. /*
  68. * The main option parsing method. Also makes sure that all of the mandatory
  69. * mount options were set.
  70. */
  71. static int parse_options(char *options, struct exofs_mountopt *opts)
  72. {
  73. char *p;
  74. substring_t args[MAX_OPT_ARGS];
  75. int option;
  76. bool s_pid = false;
  77. EXOFS_DBGMSG("parse_options %s\n", options);
  78. /* defaults */
  79. memset(opts, 0, sizeof(*opts));
  80. opts->timeout = BLK_DEFAULT_SG_TIMEOUT;
  81. while ((p = strsep(&options, ",")) != NULL) {
  82. int token;
  83. char str[32];
  84. if (!*p)
  85. continue;
  86. token = match_token(p, tokens, args);
  87. switch (token) {
  88. case Opt_name:
  89. opts->dev_name = match_strdup(&args[0]);
  90. if (unlikely(!opts->dev_name)) {
  91. EXOFS_ERR("Error allocating dev_name");
  92. return -ENOMEM;
  93. }
  94. opts->is_osdname = true;
  95. break;
  96. case Opt_pid:
  97. if (0 == match_strlcpy(str, &args[0], sizeof(str)))
  98. return -EINVAL;
  99. opts->pid = simple_strtoull(str, NULL, 0);
  100. if (opts->pid < EXOFS_MIN_PID) {
  101. EXOFS_ERR("Partition ID must be >= %u",
  102. EXOFS_MIN_PID);
  103. return -EINVAL;
  104. }
  105. s_pid = 1;
  106. break;
  107. case Opt_to:
  108. if (match_int(&args[0], &option))
  109. return -EINVAL;
  110. if (option <= 0) {
  111. EXOFS_ERR("Timout must be > 0");
  112. return -EINVAL;
  113. }
  114. opts->timeout = option * HZ;
  115. break;
  116. }
  117. }
  118. if (!s_pid) {
  119. EXOFS_ERR("Need to specify the following options:\n");
  120. EXOFS_ERR(" -o pid=pid_no_to_use\n");
  121. return -EINVAL;
  122. }
  123. return 0;
  124. }
  125. /******************************************************************************
  126. * INODE CACHE
  127. *****************************************************************************/
  128. /*
  129. * Our inode cache. Isn't it pretty?
  130. */
  131. static struct kmem_cache *exofs_inode_cachep;
  132. /*
  133. * Allocate an inode in the cache
  134. */
  135. static struct inode *exofs_alloc_inode(struct super_block *sb)
  136. {
  137. struct exofs_i_info *oi;
  138. oi = kmem_cache_alloc(exofs_inode_cachep, GFP_KERNEL);
  139. if (!oi)
  140. return NULL;
  141. oi->vfs_inode.i_version = 1;
  142. return &oi->vfs_inode;
  143. }
  144. static void exofs_i_callback(struct rcu_head *head)
  145. {
  146. struct inode *inode = container_of(head, struct inode, i_rcu);
  147. INIT_LIST_HEAD(&inode->i_dentry);
  148. kmem_cache_free(exofs_inode_cachep, exofs_i(inode));
  149. }
  150. /*
  151. * Remove an inode from the cache
  152. */
  153. static void exofs_destroy_inode(struct inode *inode)
  154. {
  155. call_rcu(&inode->i_rcu, exofs_i_callback);
  156. }
  157. /*
  158. * Initialize the inode
  159. */
  160. static void exofs_init_once(void *foo)
  161. {
  162. struct exofs_i_info *oi = foo;
  163. inode_init_once(&oi->vfs_inode);
  164. }
  165. /*
  166. * Create and initialize the inode cache
  167. */
  168. static int init_inodecache(void)
  169. {
  170. exofs_inode_cachep = kmem_cache_create("exofs_inode_cache",
  171. sizeof(struct exofs_i_info), 0,
  172. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
  173. exofs_init_once);
  174. if (exofs_inode_cachep == NULL)
  175. return -ENOMEM;
  176. return 0;
  177. }
  178. /*
  179. * Destroy the inode cache
  180. */
  181. static void destroy_inodecache(void)
  182. {
  183. kmem_cache_destroy(exofs_inode_cachep);
  184. }
  185. /******************************************************************************
  186. * SUPERBLOCK FUNCTIONS
  187. *****************************************************************************/
  188. static const struct super_operations exofs_sops;
  189. static const struct export_operations exofs_export_ops;
  190. /*
  191. * Write the superblock to the OSD
  192. */
  193. int exofs_sync_fs(struct super_block *sb, int wait)
  194. {
  195. struct exofs_sb_info *sbi;
  196. struct exofs_fscb *fscb;
  197. struct exofs_io_state *ios;
  198. int ret = -ENOMEM;
  199. lock_super(sb);
  200. sbi = sb->s_fs_info;
  201. fscb = &sbi->s_fscb;
  202. ret = exofs_get_io_state(&sbi->layout, &ios);
  203. if (ret)
  204. goto out;
  205. /* Note: We only write the changing part of the fscb. .i.e upto the
  206. * the fscb->s_dev_table_oid member. There is no read-modify-write
  207. * here.
  208. */
  209. ios->length = offsetof(struct exofs_fscb, s_dev_table_oid);
  210. memset(fscb, 0, ios->length);
  211. fscb->s_nextid = cpu_to_le64(sbi->s_nextid);
  212. fscb->s_numfiles = cpu_to_le32(sbi->s_numfiles);
  213. fscb->s_magic = cpu_to_le16(sb->s_magic);
  214. fscb->s_newfs = 0;
  215. fscb->s_version = EXOFS_FSCB_VER;
  216. ios->obj.id = EXOFS_SUPER_ID;
  217. ios->offset = 0;
  218. ios->kern_buff = fscb;
  219. ios->cred = sbi->s_cred;
  220. ret = exofs_sbi_write(ios);
  221. if (unlikely(ret)) {
  222. EXOFS_ERR("%s: exofs_sbi_write failed.\n", __func__);
  223. goto out;
  224. }
  225. sb->s_dirt = 0;
  226. out:
  227. EXOFS_DBGMSG("s_nextid=0x%llx ret=%d\n", _LLU(sbi->s_nextid), ret);
  228. exofs_put_io_state(ios);
  229. unlock_super(sb);
  230. return ret;
  231. }
  232. static void exofs_write_super(struct super_block *sb)
  233. {
  234. if (!(sb->s_flags & MS_RDONLY))
  235. exofs_sync_fs(sb, 1);
  236. else
  237. sb->s_dirt = 0;
  238. }
  239. static void _exofs_print_device(const char *msg, const char *dev_path,
  240. struct osd_dev *od, u64 pid)
  241. {
  242. const struct osd_dev_info *odi = osduld_device_info(od);
  243. printk(KERN_NOTICE "exofs: %s %s osd_name-%s pid-0x%llx\n",
  244. msg, dev_path ?: "", odi->osdname, _LLU(pid));
  245. }
  246. void exofs_free_sbi(struct exofs_sb_info *sbi)
  247. {
  248. while (sbi->layout.s_numdevs) {
  249. int i = --sbi->layout.s_numdevs;
  250. struct osd_dev *od = sbi->layout.s_ods[i];
  251. if (od) {
  252. sbi->layout.s_ods[i] = NULL;
  253. osduld_put_device(od);
  254. }
  255. }
  256. kfree(sbi);
  257. }
  258. /*
  259. * This function is called when the vfs is freeing the superblock. We just
  260. * need to free our own part.
  261. */
  262. static void exofs_put_super(struct super_block *sb)
  263. {
  264. int num_pend;
  265. struct exofs_sb_info *sbi = sb->s_fs_info;
  266. if (sb->s_dirt)
  267. exofs_write_super(sb);
  268. /* make sure there are no pending commands */
  269. for (num_pend = atomic_read(&sbi->s_curr_pending); num_pend > 0;
  270. num_pend = atomic_read(&sbi->s_curr_pending)) {
  271. wait_queue_head_t wq;
  272. init_waitqueue_head(&wq);
  273. wait_event_timeout(wq,
  274. (atomic_read(&sbi->s_curr_pending) == 0),
  275. msecs_to_jiffies(100));
  276. }
  277. _exofs_print_device("Unmounting", NULL, sbi->layout.s_ods[0],
  278. sbi->layout.s_pid);
  279. bdi_destroy(&sbi->bdi);
  280. exofs_free_sbi(sbi);
  281. sb->s_fs_info = NULL;
  282. }
  283. static int _read_and_match_data_map(struct exofs_sb_info *sbi, unsigned numdevs,
  284. struct exofs_device_table *dt)
  285. {
  286. u64 stripe_length;
  287. sbi->data_map.odm_num_comps =
  288. le32_to_cpu(dt->dt_data_map.cb_num_comps);
  289. sbi->data_map.odm_stripe_unit =
  290. le64_to_cpu(dt->dt_data_map.cb_stripe_unit);
  291. sbi->data_map.odm_group_width =
  292. le32_to_cpu(dt->dt_data_map.cb_group_width);
  293. sbi->data_map.odm_group_depth =
  294. le32_to_cpu(dt->dt_data_map.cb_group_depth);
  295. sbi->data_map.odm_mirror_cnt =
  296. le32_to_cpu(dt->dt_data_map.cb_mirror_cnt);
  297. sbi->data_map.odm_raid_algorithm =
  298. le32_to_cpu(dt->dt_data_map.cb_raid_algorithm);
  299. /* FIXME: Only raid0 for now. if not so, do not mount */
  300. if (sbi->data_map.odm_num_comps != numdevs) {
  301. EXOFS_ERR("odm_num_comps(%u) != numdevs(%u)\n",
  302. sbi->data_map.odm_num_comps, numdevs);
  303. return -EINVAL;
  304. }
  305. if (sbi->data_map.odm_raid_algorithm != PNFS_OSD_RAID_0) {
  306. EXOFS_ERR("Only RAID_0 for now\n");
  307. return -EINVAL;
  308. }
  309. if (0 != (numdevs % (sbi->data_map.odm_mirror_cnt + 1))) {
  310. EXOFS_ERR("Data Map wrong, numdevs=%d mirrors=%d\n",
  311. numdevs, sbi->data_map.odm_mirror_cnt);
  312. return -EINVAL;
  313. }
  314. if (0 != (sbi->data_map.odm_stripe_unit & ~PAGE_MASK)) {
  315. EXOFS_ERR("Stripe Unit(0x%llx)"
  316. " must be Multples of PAGE_SIZE(0x%lx)\n",
  317. _LLU(sbi->data_map.odm_stripe_unit), PAGE_SIZE);
  318. return -EINVAL;
  319. }
  320. sbi->layout.stripe_unit = sbi->data_map.odm_stripe_unit;
  321. sbi->layout.mirrors_p1 = sbi->data_map.odm_mirror_cnt + 1;
  322. if (sbi->data_map.odm_group_width) {
  323. sbi->layout.group_width = sbi->data_map.odm_group_width;
  324. sbi->layout.group_depth = sbi->data_map.odm_group_depth;
  325. if (!sbi->layout.group_depth) {
  326. EXOFS_ERR("group_depth == 0 && group_width != 0\n");
  327. return -EINVAL;
  328. }
  329. sbi->layout.group_count = sbi->data_map.odm_num_comps /
  330. sbi->layout.mirrors_p1 /
  331. sbi->data_map.odm_group_width;
  332. } else {
  333. if (sbi->data_map.odm_group_depth) {
  334. printk(KERN_NOTICE "Warning: group_depth ignored "
  335. "group_width == 0 && group_depth == %d\n",
  336. sbi->data_map.odm_group_depth);
  337. sbi->data_map.odm_group_depth = 0;
  338. }
  339. sbi->layout.group_width = sbi->data_map.odm_num_comps /
  340. sbi->layout.mirrors_p1;
  341. sbi->layout.group_depth = -1;
  342. sbi->layout.group_count = 1;
  343. }
  344. stripe_length = (u64)sbi->layout.group_width * sbi->layout.stripe_unit;
  345. if (stripe_length >= (1ULL << 32)) {
  346. EXOFS_ERR("Total Stripe length(0x%llx)"
  347. " >= 32bit is not supported\n", _LLU(stripe_length));
  348. return -EINVAL;
  349. }
  350. return 0;
  351. }
  352. static unsigned __ra_pages(struct exofs_layout *layout)
  353. {
  354. const unsigned _MIN_RA = 32; /* min 128K read-ahead */
  355. unsigned ra_pages = layout->group_width * layout->stripe_unit /
  356. PAGE_SIZE;
  357. unsigned max_io_pages = exofs_max_io_pages(layout, ~0);
  358. ra_pages *= 2; /* two stripes */
  359. if (ra_pages < _MIN_RA)
  360. ra_pages = roundup(_MIN_RA, ra_pages / 2);
  361. if (ra_pages > max_io_pages)
  362. ra_pages = max_io_pages;
  363. return ra_pages;
  364. }
  365. /* @odi is valid only as long as @fscb_dev is valid */
  366. static int exofs_devs_2_odi(struct exofs_dt_device_info *dt_dev,
  367. struct osd_dev_info *odi)
  368. {
  369. odi->systemid_len = le32_to_cpu(dt_dev->systemid_len);
  370. memcpy(odi->systemid, dt_dev->systemid, odi->systemid_len);
  371. odi->osdname_len = le32_to_cpu(dt_dev->osdname_len);
  372. odi->osdname = dt_dev->osdname;
  373. /* FIXME support long names. Will need a _put function */
  374. if (dt_dev->long_name_offset)
  375. return -EINVAL;
  376. /* Make sure osdname is printable!
  377. * mkexofs should give us space for a null-terminator else the
  378. * device-table is invalid.
  379. */
  380. if (unlikely(odi->osdname_len >= sizeof(dt_dev->osdname)))
  381. odi->osdname_len = sizeof(dt_dev->osdname) - 1;
  382. dt_dev->osdname[odi->osdname_len] = 0;
  383. /* If it's all zeros something is bad we read past end-of-obj */
  384. return !(odi->systemid_len || odi->osdname_len);
  385. }
  386. static int exofs_read_lookup_dev_table(struct exofs_sb_info **psbi,
  387. unsigned table_count)
  388. {
  389. struct exofs_sb_info *sbi = *psbi;
  390. struct osd_dev *fscb_od;
  391. struct osd_obj_id obj = {.partition = sbi->layout.s_pid,
  392. .id = EXOFS_DEVTABLE_ID};
  393. struct exofs_device_table *dt;
  394. unsigned table_bytes = table_count * sizeof(dt->dt_dev_table[0]) +
  395. sizeof(*dt);
  396. unsigned numdevs, i;
  397. int ret;
  398. dt = kmalloc(table_bytes, GFP_KERNEL);
  399. if (unlikely(!dt)) {
  400. EXOFS_ERR("ERROR: allocating %x bytes for device table\n",
  401. table_bytes);
  402. return -ENOMEM;
  403. }
  404. fscb_od = sbi->layout.s_ods[0];
  405. sbi->layout.s_ods[0] = NULL;
  406. sbi->layout.s_numdevs = 0;
  407. ret = exofs_read_kern(fscb_od, sbi->s_cred, &obj, 0, dt, table_bytes);
  408. if (unlikely(ret)) {
  409. EXOFS_ERR("ERROR: reading device table\n");
  410. goto out;
  411. }
  412. numdevs = le64_to_cpu(dt->dt_num_devices);
  413. if (unlikely(!numdevs)) {
  414. ret = -EINVAL;
  415. goto out;
  416. }
  417. WARN_ON(table_count != numdevs);
  418. ret = _read_and_match_data_map(sbi, numdevs, dt);
  419. if (unlikely(ret))
  420. goto out;
  421. if (likely(numdevs > 1)) {
  422. unsigned size = numdevs * sizeof(sbi->layout.s_ods[0]);
  423. sbi = krealloc(sbi, sizeof(*sbi) + size, GFP_KERNEL);
  424. if (unlikely(!sbi)) {
  425. ret = -ENOMEM;
  426. goto out;
  427. }
  428. memset(&sbi->layout.s_ods[1], 0,
  429. size - sizeof(sbi->layout.s_ods[0]));
  430. *psbi = sbi;
  431. }
  432. for (i = 0; i < numdevs; i++) {
  433. struct exofs_fscb fscb;
  434. struct osd_dev_info odi;
  435. struct osd_dev *od;
  436. if (exofs_devs_2_odi(&dt->dt_dev_table[i], &odi)) {
  437. EXOFS_ERR("ERROR: Read all-zeros device entry\n");
  438. ret = -EINVAL;
  439. goto out;
  440. }
  441. printk(KERN_NOTICE "Add device[%d]: osd_name-%s\n",
  442. i, odi.osdname);
  443. /* On all devices the device table is identical. The user can
  444. * specify any one of the participating devices on the command
  445. * line. We always keep them in device-table order.
  446. */
  447. if (fscb_od && osduld_device_same(fscb_od, &odi)) {
  448. sbi->layout.s_ods[i] = fscb_od;
  449. ++sbi->layout.s_numdevs;
  450. fscb_od = NULL;
  451. continue;
  452. }
  453. od = osduld_info_lookup(&odi);
  454. if (IS_ERR(od)) {
  455. ret = PTR_ERR(od);
  456. EXOFS_ERR("ERROR: device requested is not found "
  457. "osd_name-%s =>%d\n", odi.osdname, ret);
  458. goto out;
  459. }
  460. sbi->layout.s_ods[i] = od;
  461. ++sbi->layout.s_numdevs;
  462. /* Read the fscb of the other devices to make sure the FS
  463. * partition is there.
  464. */
  465. ret = exofs_read_kern(od, sbi->s_cred, &obj, 0, &fscb,
  466. sizeof(fscb));
  467. if (unlikely(ret)) {
  468. EXOFS_ERR("ERROR: Malformed participating device "
  469. "error reading fscb osd_name-%s\n",
  470. odi.osdname);
  471. goto out;
  472. }
  473. /* TODO: verify other information is correct and FS-uuid
  474. * matches. Benny what did you say about device table
  475. * generation and old devices?
  476. */
  477. }
  478. out:
  479. kfree(dt);
  480. if (unlikely(!ret && fscb_od)) {
  481. EXOFS_ERR(
  482. "ERROR: Bad device-table container device not present\n");
  483. osduld_put_device(fscb_od);
  484. ret = -EINVAL;
  485. }
  486. return ret;
  487. }
  488. /*
  489. * Read the superblock from the OSD and fill in the fields
  490. */
  491. static int exofs_fill_super(struct super_block *sb, void *data, int silent)
  492. {
  493. struct inode *root;
  494. struct exofs_mountopt *opts = data;
  495. struct exofs_sb_info *sbi; /*extended info */
  496. struct osd_dev *od; /* Master device */
  497. struct exofs_fscb fscb; /*on-disk superblock info */
  498. struct osd_obj_id obj;
  499. unsigned table_count;
  500. int ret;
  501. sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
  502. if (!sbi)
  503. return -ENOMEM;
  504. ret = bdi_setup_and_register(&sbi->bdi, "exofs", BDI_CAP_MAP_COPY);
  505. if (ret)
  506. goto free_bdi;
  507. /* use mount options to fill superblock */
  508. if (opts->is_osdname) {
  509. struct osd_dev_info odi = {.systemid_len = 0};
  510. odi.osdname_len = strlen(opts->dev_name);
  511. odi.osdname = (u8 *)opts->dev_name;
  512. od = osduld_info_lookup(&odi);
  513. } else {
  514. od = osduld_path_lookup(opts->dev_name);
  515. }
  516. if (IS_ERR(od)) {
  517. ret = -EINVAL;
  518. goto free_sbi;
  519. }
  520. /* Default layout in case we do not have a device-table */
  521. sbi->layout.stripe_unit = PAGE_SIZE;
  522. sbi->layout.mirrors_p1 = 1;
  523. sbi->layout.group_width = 1;
  524. sbi->layout.group_depth = -1;
  525. sbi->layout.group_count = 1;
  526. sbi->layout.s_ods[0] = od;
  527. sbi->layout.s_numdevs = 1;
  528. sbi->layout.s_pid = opts->pid;
  529. sbi->s_timeout = opts->timeout;
  530. /* fill in some other data by hand */
  531. memset(sb->s_id, 0, sizeof(sb->s_id));
  532. strcpy(sb->s_id, "exofs");
  533. sb->s_blocksize = EXOFS_BLKSIZE;
  534. sb->s_blocksize_bits = EXOFS_BLKSHIFT;
  535. sb->s_maxbytes = MAX_LFS_FILESIZE;
  536. atomic_set(&sbi->s_curr_pending, 0);
  537. sb->s_bdev = NULL;
  538. sb->s_dev = 0;
  539. obj.partition = sbi->layout.s_pid;
  540. obj.id = EXOFS_SUPER_ID;
  541. exofs_make_credential(sbi->s_cred, &obj);
  542. ret = exofs_read_kern(od, sbi->s_cred, &obj, 0, &fscb, sizeof(fscb));
  543. if (unlikely(ret))
  544. goto free_sbi;
  545. sb->s_magic = le16_to_cpu(fscb.s_magic);
  546. sbi->s_nextid = le64_to_cpu(fscb.s_nextid);
  547. sbi->s_numfiles = le32_to_cpu(fscb.s_numfiles);
  548. /* make sure what we read from the object store is correct */
  549. if (sb->s_magic != EXOFS_SUPER_MAGIC) {
  550. if (!silent)
  551. EXOFS_ERR("ERROR: Bad magic value\n");
  552. ret = -EINVAL;
  553. goto free_sbi;
  554. }
  555. if (le32_to_cpu(fscb.s_version) != EXOFS_FSCB_VER) {
  556. EXOFS_ERR("ERROR: Bad FSCB version expected-%d got-%d\n",
  557. EXOFS_FSCB_VER, le32_to_cpu(fscb.s_version));
  558. ret = -EINVAL;
  559. goto free_sbi;
  560. }
  561. /* start generation numbers from a random point */
  562. get_random_bytes(&sbi->s_next_generation, sizeof(u32));
  563. spin_lock_init(&sbi->s_next_gen_lock);
  564. table_count = le64_to_cpu(fscb.s_dev_table_count);
  565. if (table_count) {
  566. ret = exofs_read_lookup_dev_table(&sbi, table_count);
  567. if (unlikely(ret))
  568. goto free_sbi;
  569. }
  570. /* set up operation vectors */
  571. sbi->bdi.ra_pages = __ra_pages(&sbi->layout);
  572. sb->s_bdi = &sbi->bdi;
  573. sb->s_fs_info = sbi;
  574. sb->s_op = &exofs_sops;
  575. sb->s_export_op = &exofs_export_ops;
  576. root = exofs_iget(sb, EXOFS_ROOT_ID - EXOFS_OBJ_OFF);
  577. if (IS_ERR(root)) {
  578. EXOFS_ERR("ERROR: exofs_iget failed\n");
  579. ret = PTR_ERR(root);
  580. goto free_sbi;
  581. }
  582. sb->s_root = d_alloc_root(root);
  583. if (!sb->s_root) {
  584. iput(root);
  585. EXOFS_ERR("ERROR: get root inode failed\n");
  586. ret = -ENOMEM;
  587. goto free_sbi;
  588. }
  589. if (!S_ISDIR(root->i_mode)) {
  590. dput(sb->s_root);
  591. sb->s_root = NULL;
  592. EXOFS_ERR("ERROR: corrupt root inode (mode = %hd)\n",
  593. root->i_mode);
  594. ret = -EINVAL;
  595. goto free_sbi;
  596. }
  597. _exofs_print_device("Mounting", opts->dev_name, sbi->layout.s_ods[0],
  598. sbi->layout.s_pid);
  599. if (opts->is_osdname)
  600. kfree(opts->dev_name);
  601. return 0;
  602. free_sbi:
  603. bdi_destroy(&sbi->bdi);
  604. free_bdi:
  605. EXOFS_ERR("Unable to mount exofs on %s pid=0x%llx err=%d\n",
  606. opts->dev_name, sbi->layout.s_pid, ret);
  607. exofs_free_sbi(sbi);
  608. if (opts->is_osdname)
  609. kfree(opts->dev_name);
  610. return ret;
  611. }
  612. /*
  613. * Set up the superblock (calls exofs_fill_super eventually)
  614. */
  615. static struct dentry *exofs_mount(struct file_system_type *type,
  616. int flags, const char *dev_name,
  617. void *data)
  618. {
  619. struct exofs_mountopt opts;
  620. int ret;
  621. ret = parse_options(data, &opts);
  622. if (ret)
  623. return ERR_PTR(ret);
  624. if (!opts.dev_name)
  625. opts.dev_name = dev_name;
  626. return mount_nodev(type, flags, &opts, exofs_fill_super);
  627. }
  628. /*
  629. * Return information about the file system state in the buffer. This is used
  630. * by the 'df' command, for example.
  631. */
  632. static int exofs_statfs(struct dentry *dentry, struct kstatfs *buf)
  633. {
  634. struct super_block *sb = dentry->d_sb;
  635. struct exofs_sb_info *sbi = sb->s_fs_info;
  636. struct exofs_io_state *ios;
  637. struct osd_attr attrs[] = {
  638. ATTR_DEF(OSD_APAGE_PARTITION_QUOTAS,
  639. OSD_ATTR_PQ_CAPACITY_QUOTA, sizeof(__be64)),
  640. ATTR_DEF(OSD_APAGE_PARTITION_INFORMATION,
  641. OSD_ATTR_PI_USED_CAPACITY, sizeof(__be64)),
  642. };
  643. uint64_t capacity = ULLONG_MAX;
  644. uint64_t used = ULLONG_MAX;
  645. uint8_t cred_a[OSD_CAP_LEN];
  646. int ret;
  647. ret = exofs_get_io_state(&sbi->layout, &ios);
  648. if (ret) {
  649. EXOFS_DBGMSG("exofs_get_io_state failed.\n");
  650. return ret;
  651. }
  652. exofs_make_credential(cred_a, &ios->obj);
  653. ios->cred = sbi->s_cred;
  654. ios->in_attr = attrs;
  655. ios->in_attr_len = ARRAY_SIZE(attrs);
  656. ret = exofs_sbi_read(ios);
  657. if (unlikely(ret))
  658. goto out;
  659. ret = extract_attr_from_ios(ios, &attrs[0]);
  660. if (likely(!ret)) {
  661. capacity = get_unaligned_be64(attrs[0].val_ptr);
  662. if (unlikely(!capacity))
  663. capacity = ULLONG_MAX;
  664. } else
  665. EXOFS_DBGMSG("exofs_statfs: get capacity failed.\n");
  666. ret = extract_attr_from_ios(ios, &attrs[1]);
  667. if (likely(!ret))
  668. used = get_unaligned_be64(attrs[1].val_ptr);
  669. else
  670. EXOFS_DBGMSG("exofs_statfs: get used-space failed.\n");
  671. /* fill in the stats buffer */
  672. buf->f_type = EXOFS_SUPER_MAGIC;
  673. buf->f_bsize = EXOFS_BLKSIZE;
  674. buf->f_blocks = capacity >> 9;
  675. buf->f_bfree = (capacity - used) >> 9;
  676. buf->f_bavail = buf->f_bfree;
  677. buf->f_files = sbi->s_numfiles;
  678. buf->f_ffree = EXOFS_MAX_ID - sbi->s_numfiles;
  679. buf->f_namelen = EXOFS_NAME_LEN;
  680. out:
  681. exofs_put_io_state(ios);
  682. return ret;
  683. }
  684. static const struct super_operations exofs_sops = {
  685. .alloc_inode = exofs_alloc_inode,
  686. .destroy_inode = exofs_destroy_inode,
  687. .write_inode = exofs_write_inode,
  688. .evict_inode = exofs_evict_inode,
  689. .put_super = exofs_put_super,
  690. .write_super = exofs_write_super,
  691. .sync_fs = exofs_sync_fs,
  692. .statfs = exofs_statfs,
  693. };
  694. /******************************************************************************
  695. * EXPORT OPERATIONS
  696. *****************************************************************************/
  697. struct dentry *exofs_get_parent(struct dentry *child)
  698. {
  699. unsigned long ino = exofs_parent_ino(child);
  700. if (!ino)
  701. return NULL;
  702. return d_obtain_alias(exofs_iget(child->d_inode->i_sb, ino));
  703. }
  704. static struct inode *exofs_nfs_get_inode(struct super_block *sb,
  705. u64 ino, u32 generation)
  706. {
  707. struct inode *inode;
  708. inode = exofs_iget(sb, ino);
  709. if (IS_ERR(inode))
  710. return ERR_CAST(inode);
  711. if (generation && inode->i_generation != generation) {
  712. /* we didn't find the right inode.. */
  713. iput(inode);
  714. return ERR_PTR(-ESTALE);
  715. }
  716. return inode;
  717. }
  718. static struct dentry *exofs_fh_to_dentry(struct super_block *sb,
  719. struct fid *fid, int fh_len, int fh_type)
  720. {
  721. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  722. exofs_nfs_get_inode);
  723. }
  724. static struct dentry *exofs_fh_to_parent(struct super_block *sb,
  725. struct fid *fid, int fh_len, int fh_type)
  726. {
  727. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  728. exofs_nfs_get_inode);
  729. }
  730. static const struct export_operations exofs_export_ops = {
  731. .fh_to_dentry = exofs_fh_to_dentry,
  732. .fh_to_parent = exofs_fh_to_parent,
  733. .get_parent = exofs_get_parent,
  734. };
  735. /******************************************************************************
  736. * INSMOD/RMMOD
  737. *****************************************************************************/
  738. /*
  739. * struct that describes this file system
  740. */
  741. static struct file_system_type exofs_type = {
  742. .owner = THIS_MODULE,
  743. .name = "exofs",
  744. .mount = exofs_mount,
  745. .kill_sb = generic_shutdown_super,
  746. };
  747. static int __init init_exofs(void)
  748. {
  749. int err;
  750. err = init_inodecache();
  751. if (err)
  752. goto out;
  753. err = register_filesystem(&exofs_type);
  754. if (err)
  755. goto out_d;
  756. return 0;
  757. out_d:
  758. destroy_inodecache();
  759. out:
  760. return err;
  761. }
  762. static void __exit exit_exofs(void)
  763. {
  764. unregister_filesystem(&exofs_type);
  765. destroy_inodecache();
  766. }
  767. MODULE_AUTHOR("Avishay Traeger <avishay@gmail.com>");
  768. MODULE_DESCRIPTION("exofs");
  769. MODULE_LICENSE("GPL");
  770. module_init(init_exofs)
  771. module_exit(exit_exofs)