super.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  1. /*
  2. * Copyright (C) 2005, 2006
  3. * Avishay Traeger (avishay@gmail.com)
  4. * Copyright (C) 2008, 2009
  5. * Boaz Harrosh <ooo@electrozaur.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/module.h>
  38. #include <linux/exportfs.h>
  39. #include <linux/slab.h>
  40. #include <linux/iversion.h>
  41. #include "exofs.h"
  42. #define EXOFS_DBGMSG2(M...) do {} while (0)
  43. /******************************************************************************
  44. * MOUNT OPTIONS
  45. *****************************************************************************/
  46. /*
  47. * struct to hold what we get from mount options
  48. */
  49. struct exofs_mountopt {
  50. bool is_osdname;
  51. const char *dev_name;
  52. uint64_t pid;
  53. int timeout;
  54. };
  55. /*
  56. * exofs-specific mount-time options.
  57. */
  58. enum { Opt_name, Opt_pid, Opt_to, Opt_err };
  59. /*
  60. * Our mount-time options. These should ideally be 64-bit unsigned, but the
  61. * kernel's parsing functions do not currently support that. 32-bit should be
  62. * sufficient for most applications now.
  63. */
  64. static match_table_t tokens = {
  65. {Opt_name, "osdname=%s"},
  66. {Opt_pid, "pid=%u"},
  67. {Opt_to, "to=%u"},
  68. {Opt_err, NULL}
  69. };
  70. /*
  71. * The main option parsing method. Also makes sure that all of the mandatory
  72. * mount options were set.
  73. */
  74. static int parse_options(char *options, struct exofs_mountopt *opts)
  75. {
  76. char *p;
  77. substring_t args[MAX_OPT_ARGS];
  78. int option;
  79. bool s_pid = false;
  80. EXOFS_DBGMSG("parse_options %s\n", options);
  81. /* defaults */
  82. memset(opts, 0, sizeof(*opts));
  83. opts->timeout = BLK_DEFAULT_SG_TIMEOUT;
  84. while ((p = strsep(&options, ",")) != NULL) {
  85. int token;
  86. char str[32];
  87. if (!*p)
  88. continue;
  89. token = match_token(p, tokens, args);
  90. switch (token) {
  91. case Opt_name:
  92. kfree(opts->dev_name);
  93. opts->dev_name = match_strdup(&args[0]);
  94. if (unlikely(!opts->dev_name)) {
  95. EXOFS_ERR("Error allocating dev_name");
  96. return -ENOMEM;
  97. }
  98. opts->is_osdname = true;
  99. break;
  100. case Opt_pid:
  101. if (0 == match_strlcpy(str, &args[0], sizeof(str)))
  102. return -EINVAL;
  103. opts->pid = simple_strtoull(str, NULL, 0);
  104. if (opts->pid < EXOFS_MIN_PID) {
  105. EXOFS_ERR("Partition ID must be >= %u",
  106. EXOFS_MIN_PID);
  107. return -EINVAL;
  108. }
  109. s_pid = 1;
  110. break;
  111. case Opt_to:
  112. if (match_int(&args[0], &option))
  113. return -EINVAL;
  114. if (option <= 0) {
  115. EXOFS_ERR("Timeout must be > 0");
  116. return -EINVAL;
  117. }
  118. opts->timeout = option * HZ;
  119. break;
  120. }
  121. }
  122. if (!s_pid) {
  123. EXOFS_ERR("Need to specify the following options:\n");
  124. EXOFS_ERR(" -o pid=pid_no_to_use\n");
  125. return -EINVAL;
  126. }
  127. return 0;
  128. }
  129. /******************************************************************************
  130. * INODE CACHE
  131. *****************************************************************************/
  132. /*
  133. * Our inode cache. Isn't it pretty?
  134. */
  135. static struct kmem_cache *exofs_inode_cachep;
  136. /*
  137. * Allocate an inode in the cache
  138. */
  139. static struct inode *exofs_alloc_inode(struct super_block *sb)
  140. {
  141. struct exofs_i_info *oi;
  142. oi = kmem_cache_alloc(exofs_inode_cachep, GFP_KERNEL);
  143. if (!oi)
  144. return NULL;
  145. inode_set_iversion(&oi->vfs_inode, 1);
  146. return &oi->vfs_inode;
  147. }
  148. static void exofs_i_callback(struct rcu_head *head)
  149. {
  150. struct inode *inode = container_of(head, struct inode, i_rcu);
  151. kmem_cache_free(exofs_inode_cachep, exofs_i(inode));
  152. }
  153. /*
  154. * Remove an inode from the cache
  155. */
  156. static void exofs_destroy_inode(struct inode *inode)
  157. {
  158. call_rcu(&inode->i_rcu, exofs_i_callback);
  159. }
  160. /*
  161. * Initialize the inode
  162. */
  163. static void exofs_init_once(void *foo)
  164. {
  165. struct exofs_i_info *oi = foo;
  166. inode_init_once(&oi->vfs_inode);
  167. }
  168. /*
  169. * Create and initialize the inode cache
  170. */
  171. static int init_inodecache(void)
  172. {
  173. exofs_inode_cachep = kmem_cache_create_usercopy("exofs_inode_cache",
  174. sizeof(struct exofs_i_info), 0,
  175. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD |
  176. SLAB_ACCOUNT,
  177. offsetof(struct exofs_i_info, i_data),
  178. sizeof_field(struct exofs_i_info, i_data),
  179. exofs_init_once);
  180. if (exofs_inode_cachep == NULL)
  181. return -ENOMEM;
  182. return 0;
  183. }
  184. /*
  185. * Destroy the inode cache
  186. */
  187. static void destroy_inodecache(void)
  188. {
  189. /*
  190. * Make sure all delayed rcu free inodes are flushed before we
  191. * destroy cache.
  192. */
  193. rcu_barrier();
  194. kmem_cache_destroy(exofs_inode_cachep);
  195. }
  196. /******************************************************************************
  197. * Some osd helpers
  198. *****************************************************************************/
  199. void exofs_make_credential(u8 cred_a[OSD_CAP_LEN], const struct osd_obj_id *obj)
  200. {
  201. osd_sec_init_nosec_doall_caps(cred_a, obj, false, true);
  202. }
  203. static int exofs_read_kern(struct osd_dev *od, u8 *cred, struct osd_obj_id *obj,
  204. u64 offset, void *p, unsigned length)
  205. {
  206. struct osd_request *or = osd_start_request(od);
  207. /* struct osd_sense_info osi = {.key = 0};*/
  208. int ret;
  209. if (unlikely(!or)) {
  210. EXOFS_DBGMSG("%s: osd_start_request failed.\n", __func__);
  211. return -ENOMEM;
  212. }
  213. ret = osd_req_read_kern(or, obj, offset, p, length);
  214. if (unlikely(ret)) {
  215. EXOFS_DBGMSG("%s: osd_req_read_kern failed.\n", __func__);
  216. goto out;
  217. }
  218. ret = osd_finalize_request(or, 0, cred, NULL);
  219. if (unlikely(ret)) {
  220. EXOFS_DBGMSG("Failed to osd_finalize_request() => %d\n", ret);
  221. goto out;
  222. }
  223. ret = osd_execute_request(or);
  224. if (unlikely(ret))
  225. EXOFS_DBGMSG("osd_execute_request() => %d\n", ret);
  226. /* osd_req_decode_sense(or, ret); */
  227. out:
  228. osd_end_request(or);
  229. EXOFS_DBGMSG2("read_kern(0x%llx) offset=0x%llx "
  230. "length=0x%llx dev=%p ret=>%d\n",
  231. _LLU(obj->id), _LLU(offset), _LLU(length), od, ret);
  232. return ret;
  233. }
  234. static const struct osd_attr g_attr_sb_stats = ATTR_DEF(
  235. EXOFS_APAGE_SB_DATA,
  236. EXOFS_ATTR_SB_STATS,
  237. sizeof(struct exofs_sb_stats));
  238. static int __sbi_read_stats(struct exofs_sb_info *sbi)
  239. {
  240. struct osd_attr attrs[] = {
  241. [0] = g_attr_sb_stats,
  242. };
  243. struct ore_io_state *ios;
  244. int ret;
  245. ret = ore_get_io_state(&sbi->layout, &sbi->oc, &ios);
  246. if (unlikely(ret)) {
  247. EXOFS_ERR("%s: ore_get_io_state failed.\n", __func__);
  248. return ret;
  249. }
  250. ios->in_attr = attrs;
  251. ios->in_attr_len = ARRAY_SIZE(attrs);
  252. ret = ore_read(ios);
  253. if (unlikely(ret)) {
  254. EXOFS_ERR("Error reading super_block stats => %d\n", ret);
  255. goto out;
  256. }
  257. ret = extract_attr_from_ios(ios, &attrs[0]);
  258. if (ret) {
  259. EXOFS_ERR("%s: extract_attr of sb_stats failed\n", __func__);
  260. goto out;
  261. }
  262. if (attrs[0].len) {
  263. struct exofs_sb_stats *ess;
  264. if (unlikely(attrs[0].len != sizeof(*ess))) {
  265. EXOFS_ERR("%s: Wrong version of exofs_sb_stats "
  266. "size(%d) != expected(%zd)\n",
  267. __func__, attrs[0].len, sizeof(*ess));
  268. goto out;
  269. }
  270. ess = attrs[0].val_ptr;
  271. sbi->s_nextid = le64_to_cpu(ess->s_nextid);
  272. sbi->s_numfiles = le32_to_cpu(ess->s_numfiles);
  273. }
  274. out:
  275. ore_put_io_state(ios);
  276. return ret;
  277. }
  278. static void stats_done(struct ore_io_state *ios, void *p)
  279. {
  280. ore_put_io_state(ios);
  281. /* Good thanks nothing to do anymore */
  282. }
  283. /* Asynchronously write the stats attribute */
  284. int exofs_sbi_write_stats(struct exofs_sb_info *sbi)
  285. {
  286. struct osd_attr attrs[] = {
  287. [0] = g_attr_sb_stats,
  288. };
  289. struct ore_io_state *ios;
  290. int ret;
  291. ret = ore_get_io_state(&sbi->layout, &sbi->oc, &ios);
  292. if (unlikely(ret)) {
  293. EXOFS_ERR("%s: ore_get_io_state failed.\n", __func__);
  294. return ret;
  295. }
  296. sbi->s_ess.s_nextid = cpu_to_le64(sbi->s_nextid);
  297. sbi->s_ess.s_numfiles = cpu_to_le64(sbi->s_numfiles);
  298. attrs[0].val_ptr = &sbi->s_ess;
  299. ios->done = stats_done;
  300. ios->private = sbi;
  301. ios->out_attr = attrs;
  302. ios->out_attr_len = ARRAY_SIZE(attrs);
  303. ret = ore_write(ios);
  304. if (unlikely(ret)) {
  305. EXOFS_ERR("%s: ore_write failed.\n", __func__);
  306. ore_put_io_state(ios);
  307. }
  308. return ret;
  309. }
  310. /******************************************************************************
  311. * SUPERBLOCK FUNCTIONS
  312. *****************************************************************************/
  313. static const struct super_operations exofs_sops;
  314. static const struct export_operations exofs_export_ops;
  315. /*
  316. * Write the superblock to the OSD
  317. */
  318. static int exofs_sync_fs(struct super_block *sb, int wait)
  319. {
  320. struct exofs_sb_info *sbi;
  321. struct exofs_fscb *fscb;
  322. struct ore_comp one_comp;
  323. struct ore_components oc;
  324. struct ore_io_state *ios;
  325. int ret = -ENOMEM;
  326. fscb = kmalloc(sizeof(*fscb), GFP_KERNEL);
  327. if (unlikely(!fscb))
  328. return -ENOMEM;
  329. sbi = sb->s_fs_info;
  330. /* NOTE: We no longer dirty the super_block anywhere in exofs. The
  331. * reason we write the fscb here on unmount is so we can stay backwards
  332. * compatible with fscb->s_version == 1. (What we are not compatible
  333. * with is if a new version FS crashed and then we try to mount an old
  334. * version). Otherwise the exofs_fscb is read-only from mkfs time. All
  335. * the writeable info is set in exofs_sbi_write_stats() above.
  336. */
  337. exofs_init_comps(&oc, &one_comp, sbi, EXOFS_SUPER_ID);
  338. ret = ore_get_io_state(&sbi->layout, &oc, &ios);
  339. if (unlikely(ret))
  340. goto out;
  341. ios->length = offsetof(struct exofs_fscb, s_dev_table_oid);
  342. memset(fscb, 0, ios->length);
  343. fscb->s_nextid = cpu_to_le64(sbi->s_nextid);
  344. fscb->s_numfiles = cpu_to_le64(sbi->s_numfiles);
  345. fscb->s_magic = cpu_to_le16(sb->s_magic);
  346. fscb->s_newfs = 0;
  347. fscb->s_version = EXOFS_FSCB_VER;
  348. ios->offset = 0;
  349. ios->kern_buff = fscb;
  350. ret = ore_write(ios);
  351. if (unlikely(ret))
  352. EXOFS_ERR("%s: ore_write failed.\n", __func__);
  353. out:
  354. EXOFS_DBGMSG("s_nextid=0x%llx ret=%d\n", _LLU(sbi->s_nextid), ret);
  355. ore_put_io_state(ios);
  356. kfree(fscb);
  357. return ret;
  358. }
  359. static void _exofs_print_device(const char *msg, const char *dev_path,
  360. struct osd_dev *od, u64 pid)
  361. {
  362. const struct osd_dev_info *odi = osduld_device_info(od);
  363. printk(KERN_NOTICE "exofs: %s %s osd_name-%s pid-0x%llx\n",
  364. msg, dev_path ?: "", odi->osdname, _LLU(pid));
  365. }
  366. static void exofs_free_sbi(struct exofs_sb_info *sbi)
  367. {
  368. unsigned numdevs = sbi->oc.numdevs;
  369. while (numdevs) {
  370. unsigned i = --numdevs;
  371. struct osd_dev *od = ore_comp_dev(&sbi->oc, i);
  372. if (od) {
  373. ore_comp_set_dev(&sbi->oc, i, NULL);
  374. osduld_put_device(od);
  375. }
  376. }
  377. kfree(sbi->oc.ods);
  378. kfree(sbi);
  379. }
  380. /*
  381. * This function is called when the vfs is freeing the superblock. We just
  382. * need to free our own part.
  383. */
  384. static void exofs_put_super(struct super_block *sb)
  385. {
  386. int num_pend;
  387. struct exofs_sb_info *sbi = sb->s_fs_info;
  388. /* make sure there are no pending commands */
  389. for (num_pend = atomic_read(&sbi->s_curr_pending); num_pend > 0;
  390. num_pend = atomic_read(&sbi->s_curr_pending)) {
  391. wait_queue_head_t wq;
  392. printk(KERN_NOTICE "%s: !!Pending operations in flight. "
  393. "This is a BUG. please report to osd-dev@open-osd.org\n",
  394. __func__);
  395. init_waitqueue_head(&wq);
  396. wait_event_timeout(wq,
  397. (atomic_read(&sbi->s_curr_pending) == 0),
  398. msecs_to_jiffies(100));
  399. }
  400. _exofs_print_device("Unmounting", NULL, ore_comp_dev(&sbi->oc, 0),
  401. sbi->one_comp.obj.partition);
  402. exofs_sysfs_sb_del(sbi);
  403. exofs_free_sbi(sbi);
  404. sb->s_fs_info = NULL;
  405. }
  406. static int _read_and_match_data_map(struct exofs_sb_info *sbi, unsigned numdevs,
  407. struct exofs_device_table *dt)
  408. {
  409. int ret;
  410. sbi->layout.stripe_unit =
  411. le64_to_cpu(dt->dt_data_map.cb_stripe_unit);
  412. sbi->layout.group_width =
  413. le32_to_cpu(dt->dt_data_map.cb_group_width);
  414. sbi->layout.group_depth =
  415. le32_to_cpu(dt->dt_data_map.cb_group_depth);
  416. sbi->layout.mirrors_p1 =
  417. le32_to_cpu(dt->dt_data_map.cb_mirror_cnt) + 1;
  418. sbi->layout.raid_algorithm =
  419. le32_to_cpu(dt->dt_data_map.cb_raid_algorithm);
  420. ret = ore_verify_layout(numdevs, &sbi->layout);
  421. EXOFS_DBGMSG("exofs: layout: "
  422. "num_comps=%u stripe_unit=0x%x group_width=%u "
  423. "group_depth=0x%llx mirrors_p1=%u raid_algorithm=%u\n",
  424. numdevs,
  425. sbi->layout.stripe_unit,
  426. sbi->layout.group_width,
  427. _LLU(sbi->layout.group_depth),
  428. sbi->layout.mirrors_p1,
  429. sbi->layout.raid_algorithm);
  430. return ret;
  431. }
  432. static unsigned __ra_pages(struct ore_layout *layout)
  433. {
  434. const unsigned _MIN_RA = 32; /* min 128K read-ahead */
  435. unsigned ra_pages = layout->group_width * layout->stripe_unit /
  436. PAGE_SIZE;
  437. unsigned max_io_pages = exofs_max_io_pages(layout, ~0);
  438. ra_pages *= 2; /* two stripes */
  439. if (ra_pages < _MIN_RA)
  440. ra_pages = roundup(_MIN_RA, ra_pages / 2);
  441. if (ra_pages > max_io_pages)
  442. ra_pages = max_io_pages;
  443. return ra_pages;
  444. }
  445. /* @odi is valid only as long as @fscb_dev is valid */
  446. static int exofs_devs_2_odi(struct exofs_dt_device_info *dt_dev,
  447. struct osd_dev_info *odi)
  448. {
  449. odi->systemid_len = le32_to_cpu(dt_dev->systemid_len);
  450. if (likely(odi->systemid_len))
  451. memcpy(odi->systemid, dt_dev->systemid, OSD_SYSTEMID_LEN);
  452. odi->osdname_len = le32_to_cpu(dt_dev->osdname_len);
  453. odi->osdname = dt_dev->osdname;
  454. /* FIXME support long names. Will need a _put function */
  455. if (dt_dev->long_name_offset)
  456. return -EINVAL;
  457. /* Make sure osdname is printable!
  458. * mkexofs should give us space for a null-terminator else the
  459. * device-table is invalid.
  460. */
  461. if (unlikely(odi->osdname_len >= sizeof(dt_dev->osdname)))
  462. odi->osdname_len = sizeof(dt_dev->osdname) - 1;
  463. dt_dev->osdname[odi->osdname_len] = 0;
  464. /* If it's all zeros something is bad we read past end-of-obj */
  465. return !(odi->systemid_len || odi->osdname_len);
  466. }
  467. static int __alloc_dev_table(struct exofs_sb_info *sbi, unsigned numdevs,
  468. struct exofs_dev **peds)
  469. {
  470. /* Twice bigger table: See exofs_init_comps() and comment at
  471. * exofs_read_lookup_dev_table()
  472. */
  473. const size_t numores = numdevs * 2 - 1;
  474. struct exofs_dev *eds;
  475. unsigned i;
  476. sbi->oc.ods = kzalloc(numores * sizeof(struct ore_dev *) +
  477. numdevs * sizeof(struct exofs_dev), GFP_KERNEL);
  478. if (unlikely(!sbi->oc.ods)) {
  479. EXOFS_ERR("ERROR: failed allocating Device array[%d]\n",
  480. numdevs);
  481. return -ENOMEM;
  482. }
  483. /* Start of allocated struct exofs_dev entries */
  484. *peds = eds = (void *)sbi->oc.ods[numores];
  485. /* Initialize pointers into struct exofs_dev */
  486. for (i = 0; i < numdevs; ++i)
  487. sbi->oc.ods[i] = &eds[i].ored;
  488. return 0;
  489. }
  490. static int exofs_read_lookup_dev_table(struct exofs_sb_info *sbi,
  491. struct osd_dev *fscb_od,
  492. unsigned table_count)
  493. {
  494. struct ore_comp comp;
  495. struct exofs_device_table *dt;
  496. struct exofs_dev *eds;
  497. unsigned table_bytes = table_count * sizeof(dt->dt_dev_table[0]) +
  498. sizeof(*dt);
  499. unsigned numdevs, i;
  500. int ret;
  501. dt = kmalloc(table_bytes, GFP_KERNEL);
  502. if (unlikely(!dt)) {
  503. EXOFS_ERR("ERROR: allocating %x bytes for device table\n",
  504. table_bytes);
  505. return -ENOMEM;
  506. }
  507. sbi->oc.numdevs = 0;
  508. comp.obj.partition = sbi->one_comp.obj.partition;
  509. comp.obj.id = EXOFS_DEVTABLE_ID;
  510. exofs_make_credential(comp.cred, &comp.obj);
  511. ret = exofs_read_kern(fscb_od, comp.cred, &comp.obj, 0, dt,
  512. table_bytes);
  513. if (unlikely(ret)) {
  514. EXOFS_ERR("ERROR: reading device table\n");
  515. goto out;
  516. }
  517. numdevs = le64_to_cpu(dt->dt_num_devices);
  518. if (unlikely(!numdevs)) {
  519. ret = -EINVAL;
  520. goto out;
  521. }
  522. WARN_ON(table_count != numdevs);
  523. ret = _read_and_match_data_map(sbi, numdevs, dt);
  524. if (unlikely(ret))
  525. goto out;
  526. ret = __alloc_dev_table(sbi, numdevs, &eds);
  527. if (unlikely(ret))
  528. goto out;
  529. /* exofs round-robins the device table view according to inode
  530. * number. We hold a: twice bigger table hence inodes can point
  531. * to any device and have a sequential view of the table
  532. * starting at this device. See exofs_init_comps()
  533. */
  534. memcpy(&sbi->oc.ods[numdevs], &sbi->oc.ods[0],
  535. (numdevs - 1) * sizeof(sbi->oc.ods[0]));
  536. /* create sysfs subdir under which we put the device table
  537. * And cluster layout. A Superblock is identified by the string:
  538. * "dev[0].osdname"_"pid"
  539. */
  540. exofs_sysfs_sb_add(sbi, &dt->dt_dev_table[0]);
  541. for (i = 0; i < numdevs; i++) {
  542. struct exofs_fscb fscb;
  543. struct osd_dev_info odi;
  544. struct osd_dev *od;
  545. if (exofs_devs_2_odi(&dt->dt_dev_table[i], &odi)) {
  546. EXOFS_ERR("ERROR: Read all-zeros device entry\n");
  547. ret = -EINVAL;
  548. goto out;
  549. }
  550. printk(KERN_NOTICE "Add device[%d]: osd_name-%s\n",
  551. i, odi.osdname);
  552. /* the exofs id is currently the table index */
  553. eds[i].did = i;
  554. /* On all devices the device table is identical. The user can
  555. * specify any one of the participating devices on the command
  556. * line. We always keep them in device-table order.
  557. */
  558. if (fscb_od && osduld_device_same(fscb_od, &odi)) {
  559. eds[i].ored.od = fscb_od;
  560. ++sbi->oc.numdevs;
  561. fscb_od = NULL;
  562. exofs_sysfs_odev_add(&eds[i], sbi);
  563. continue;
  564. }
  565. od = osduld_info_lookup(&odi);
  566. if (IS_ERR(od)) {
  567. ret = PTR_ERR(od);
  568. EXOFS_ERR("ERROR: device requested is not found "
  569. "osd_name-%s =>%d\n", odi.osdname, ret);
  570. goto out;
  571. }
  572. eds[i].ored.od = od;
  573. ++sbi->oc.numdevs;
  574. /* Read the fscb of the other devices to make sure the FS
  575. * partition is there.
  576. */
  577. ret = exofs_read_kern(od, comp.cred, &comp.obj, 0, &fscb,
  578. sizeof(fscb));
  579. if (unlikely(ret)) {
  580. EXOFS_ERR("ERROR: Malformed participating device "
  581. "error reading fscb osd_name-%s\n",
  582. odi.osdname);
  583. goto out;
  584. }
  585. exofs_sysfs_odev_add(&eds[i], sbi);
  586. /* TODO: verify other information is correct and FS-uuid
  587. * matches. Benny what did you say about device table
  588. * generation and old devices?
  589. */
  590. }
  591. out:
  592. kfree(dt);
  593. if (unlikely(fscb_od && !ret)) {
  594. EXOFS_ERR("ERROR: Bad device-table container device not present\n");
  595. osduld_put_device(fscb_od);
  596. return -EINVAL;
  597. }
  598. return ret;
  599. }
  600. /*
  601. * Read the superblock from the OSD and fill in the fields
  602. */
  603. static int exofs_fill_super(struct super_block *sb,
  604. struct exofs_mountopt *opts,
  605. struct exofs_sb_info *sbi,
  606. int silent)
  607. {
  608. struct inode *root;
  609. struct osd_dev *od; /* Master device */
  610. struct exofs_fscb fscb; /*on-disk superblock info */
  611. struct ore_comp comp;
  612. unsigned table_count;
  613. int ret;
  614. /* use mount options to fill superblock */
  615. if (opts->is_osdname) {
  616. struct osd_dev_info odi = {.systemid_len = 0};
  617. odi.osdname_len = strlen(opts->dev_name);
  618. odi.osdname = (u8 *)opts->dev_name;
  619. od = osduld_info_lookup(&odi);
  620. kfree(opts->dev_name);
  621. opts->dev_name = NULL;
  622. } else {
  623. od = osduld_path_lookup(opts->dev_name);
  624. }
  625. if (IS_ERR(od)) {
  626. ret = -EINVAL;
  627. goto free_sbi;
  628. }
  629. /* Default layout in case we do not have a device-table */
  630. sbi->layout.stripe_unit = PAGE_SIZE;
  631. sbi->layout.mirrors_p1 = 1;
  632. sbi->layout.group_width = 1;
  633. sbi->layout.group_depth = -1;
  634. sbi->layout.group_count = 1;
  635. sbi->s_timeout = opts->timeout;
  636. sbi->one_comp.obj.partition = opts->pid;
  637. sbi->one_comp.obj.id = 0;
  638. exofs_make_credential(sbi->one_comp.cred, &sbi->one_comp.obj);
  639. sbi->oc.single_comp = EC_SINGLE_COMP;
  640. sbi->oc.comps = &sbi->one_comp;
  641. /* fill in some other data by hand */
  642. memset(sb->s_id, 0, sizeof(sb->s_id));
  643. strcpy(sb->s_id, "exofs");
  644. sb->s_blocksize = EXOFS_BLKSIZE;
  645. sb->s_blocksize_bits = EXOFS_BLKSHIFT;
  646. sb->s_maxbytes = MAX_LFS_FILESIZE;
  647. sb->s_max_links = EXOFS_LINK_MAX;
  648. atomic_set(&sbi->s_curr_pending, 0);
  649. sb->s_bdev = NULL;
  650. sb->s_dev = 0;
  651. comp.obj.partition = sbi->one_comp.obj.partition;
  652. comp.obj.id = EXOFS_SUPER_ID;
  653. exofs_make_credential(comp.cred, &comp.obj);
  654. ret = exofs_read_kern(od, comp.cred, &comp.obj, 0, &fscb, sizeof(fscb));
  655. if (unlikely(ret))
  656. goto free_sbi;
  657. sb->s_magic = le16_to_cpu(fscb.s_magic);
  658. /* NOTE: we read below to be backward compatible with old versions */
  659. sbi->s_nextid = le64_to_cpu(fscb.s_nextid);
  660. sbi->s_numfiles = le32_to_cpu(fscb.s_numfiles);
  661. /* make sure what we read from the object store is correct */
  662. if (sb->s_magic != EXOFS_SUPER_MAGIC) {
  663. if (!silent)
  664. EXOFS_ERR("ERROR: Bad magic value\n");
  665. ret = -EINVAL;
  666. goto free_sbi;
  667. }
  668. if (le32_to_cpu(fscb.s_version) > EXOFS_FSCB_VER) {
  669. EXOFS_ERR("ERROR: Bad FSCB version expected-%d got-%d\n",
  670. EXOFS_FSCB_VER, le32_to_cpu(fscb.s_version));
  671. ret = -EINVAL;
  672. goto free_sbi;
  673. }
  674. /* start generation numbers from a random point */
  675. get_random_bytes(&sbi->s_next_generation, sizeof(u32));
  676. spin_lock_init(&sbi->s_next_gen_lock);
  677. table_count = le64_to_cpu(fscb.s_dev_table_count);
  678. if (table_count) {
  679. ret = exofs_read_lookup_dev_table(sbi, od, table_count);
  680. if (unlikely(ret))
  681. goto free_sbi;
  682. } else {
  683. struct exofs_dev *eds;
  684. ret = __alloc_dev_table(sbi, 1, &eds);
  685. if (unlikely(ret))
  686. goto free_sbi;
  687. ore_comp_set_dev(&sbi->oc, 0, od);
  688. sbi->oc.numdevs = 1;
  689. }
  690. __sbi_read_stats(sbi);
  691. /* set up operation vectors */
  692. ret = super_setup_bdi(sb);
  693. if (ret) {
  694. EXOFS_DBGMSG("Failed to super_setup_bdi\n");
  695. goto free_sbi;
  696. }
  697. sb->s_bdi->ra_pages = __ra_pages(&sbi->layout);
  698. sb->s_fs_info = sbi;
  699. sb->s_op = &exofs_sops;
  700. sb->s_export_op = &exofs_export_ops;
  701. root = exofs_iget(sb, EXOFS_ROOT_ID - EXOFS_OBJ_OFF);
  702. if (IS_ERR(root)) {
  703. EXOFS_ERR("ERROR: exofs_iget failed\n");
  704. ret = PTR_ERR(root);
  705. goto free_sbi;
  706. }
  707. sb->s_root = d_make_root(root);
  708. if (!sb->s_root) {
  709. EXOFS_ERR("ERROR: get root inode failed\n");
  710. ret = -ENOMEM;
  711. goto free_sbi;
  712. }
  713. if (!S_ISDIR(root->i_mode)) {
  714. dput(sb->s_root);
  715. sb->s_root = NULL;
  716. EXOFS_ERR("ERROR: corrupt root inode (mode = %hd)\n",
  717. root->i_mode);
  718. ret = -EINVAL;
  719. goto free_sbi;
  720. }
  721. exofs_sysfs_dbg_print();
  722. _exofs_print_device("Mounting", opts->dev_name,
  723. ore_comp_dev(&sbi->oc, 0),
  724. sbi->one_comp.obj.partition);
  725. return 0;
  726. free_sbi:
  727. EXOFS_ERR("Unable to mount exofs on %s pid=0x%llx err=%d\n",
  728. opts->dev_name, sbi->one_comp.obj.partition, ret);
  729. exofs_free_sbi(sbi);
  730. return ret;
  731. }
  732. /*
  733. * Set up the superblock (calls exofs_fill_super eventually)
  734. */
  735. static struct dentry *exofs_mount(struct file_system_type *type,
  736. int flags, const char *dev_name,
  737. void *data)
  738. {
  739. struct super_block *s;
  740. struct exofs_mountopt opts;
  741. struct exofs_sb_info *sbi;
  742. int ret;
  743. ret = parse_options(data, &opts);
  744. if (ret) {
  745. kfree(opts.dev_name);
  746. return ERR_PTR(ret);
  747. }
  748. sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
  749. if (!sbi) {
  750. kfree(opts.dev_name);
  751. return ERR_PTR(-ENOMEM);
  752. }
  753. s = sget(type, NULL, set_anon_super, flags, NULL);
  754. if (IS_ERR(s)) {
  755. kfree(opts.dev_name);
  756. kfree(sbi);
  757. return ERR_CAST(s);
  758. }
  759. if (!opts.dev_name)
  760. opts.dev_name = dev_name;
  761. ret = exofs_fill_super(s, &opts, sbi, flags & SB_SILENT ? 1 : 0);
  762. if (ret) {
  763. deactivate_locked_super(s);
  764. return ERR_PTR(ret);
  765. }
  766. s->s_flags |= SB_ACTIVE;
  767. return dget(s->s_root);
  768. }
  769. /*
  770. * Return information about the file system state in the buffer. This is used
  771. * by the 'df' command, for example.
  772. */
  773. static int exofs_statfs(struct dentry *dentry, struct kstatfs *buf)
  774. {
  775. struct super_block *sb = dentry->d_sb;
  776. struct exofs_sb_info *sbi = sb->s_fs_info;
  777. struct ore_io_state *ios;
  778. struct osd_attr attrs[] = {
  779. ATTR_DEF(OSD_APAGE_PARTITION_QUOTAS,
  780. OSD_ATTR_PQ_CAPACITY_QUOTA, sizeof(__be64)),
  781. ATTR_DEF(OSD_APAGE_PARTITION_INFORMATION,
  782. OSD_ATTR_PI_USED_CAPACITY, sizeof(__be64)),
  783. };
  784. uint64_t capacity = ULLONG_MAX;
  785. uint64_t used = ULLONG_MAX;
  786. int ret;
  787. ret = ore_get_io_state(&sbi->layout, &sbi->oc, &ios);
  788. if (ret) {
  789. EXOFS_DBGMSG("ore_get_io_state failed.\n");
  790. return ret;
  791. }
  792. ios->in_attr = attrs;
  793. ios->in_attr_len = ARRAY_SIZE(attrs);
  794. ret = ore_read(ios);
  795. if (unlikely(ret))
  796. goto out;
  797. ret = extract_attr_from_ios(ios, &attrs[0]);
  798. if (likely(!ret)) {
  799. capacity = get_unaligned_be64(attrs[0].val_ptr);
  800. if (unlikely(!capacity))
  801. capacity = ULLONG_MAX;
  802. } else
  803. EXOFS_DBGMSG("exofs_statfs: get capacity failed.\n");
  804. ret = extract_attr_from_ios(ios, &attrs[1]);
  805. if (likely(!ret))
  806. used = get_unaligned_be64(attrs[1].val_ptr);
  807. else
  808. EXOFS_DBGMSG("exofs_statfs: get used-space failed.\n");
  809. /* fill in the stats buffer */
  810. buf->f_type = EXOFS_SUPER_MAGIC;
  811. buf->f_bsize = EXOFS_BLKSIZE;
  812. buf->f_blocks = capacity >> 9;
  813. buf->f_bfree = (capacity - used) >> 9;
  814. buf->f_bavail = buf->f_bfree;
  815. buf->f_files = sbi->s_numfiles;
  816. buf->f_ffree = EXOFS_MAX_ID - sbi->s_numfiles;
  817. buf->f_namelen = EXOFS_NAME_LEN;
  818. out:
  819. ore_put_io_state(ios);
  820. return ret;
  821. }
  822. static const struct super_operations exofs_sops = {
  823. .alloc_inode = exofs_alloc_inode,
  824. .destroy_inode = exofs_destroy_inode,
  825. .write_inode = exofs_write_inode,
  826. .evict_inode = exofs_evict_inode,
  827. .put_super = exofs_put_super,
  828. .sync_fs = exofs_sync_fs,
  829. .statfs = exofs_statfs,
  830. };
  831. /******************************************************************************
  832. * EXPORT OPERATIONS
  833. *****************************************************************************/
  834. static struct dentry *exofs_get_parent(struct dentry *child)
  835. {
  836. unsigned long ino = exofs_parent_ino(child);
  837. if (!ino)
  838. return ERR_PTR(-ESTALE);
  839. return d_obtain_alias(exofs_iget(child->d_sb, ino));
  840. }
  841. static struct inode *exofs_nfs_get_inode(struct super_block *sb,
  842. u64 ino, u32 generation)
  843. {
  844. struct inode *inode;
  845. inode = exofs_iget(sb, ino);
  846. if (IS_ERR(inode))
  847. return ERR_CAST(inode);
  848. if (generation && inode->i_generation != generation) {
  849. /* we didn't find the right inode.. */
  850. iput(inode);
  851. return ERR_PTR(-ESTALE);
  852. }
  853. return inode;
  854. }
  855. static struct dentry *exofs_fh_to_dentry(struct super_block *sb,
  856. struct fid *fid, int fh_len, int fh_type)
  857. {
  858. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  859. exofs_nfs_get_inode);
  860. }
  861. static struct dentry *exofs_fh_to_parent(struct super_block *sb,
  862. struct fid *fid, int fh_len, int fh_type)
  863. {
  864. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  865. exofs_nfs_get_inode);
  866. }
  867. static const struct export_operations exofs_export_ops = {
  868. .fh_to_dentry = exofs_fh_to_dentry,
  869. .fh_to_parent = exofs_fh_to_parent,
  870. .get_parent = exofs_get_parent,
  871. };
  872. /******************************************************************************
  873. * INSMOD/RMMOD
  874. *****************************************************************************/
  875. /*
  876. * struct that describes this file system
  877. */
  878. static struct file_system_type exofs_type = {
  879. .owner = THIS_MODULE,
  880. .name = "exofs",
  881. .mount = exofs_mount,
  882. .kill_sb = generic_shutdown_super,
  883. };
  884. MODULE_ALIAS_FS("exofs");
  885. static int __init init_exofs(void)
  886. {
  887. int err;
  888. err = init_inodecache();
  889. if (err)
  890. goto out;
  891. err = register_filesystem(&exofs_type);
  892. if (err)
  893. goto out_d;
  894. /* We don't fail if sysfs creation failed */
  895. exofs_sysfs_init();
  896. return 0;
  897. out_d:
  898. destroy_inodecache();
  899. out:
  900. return err;
  901. }
  902. static void __exit exit_exofs(void)
  903. {
  904. exofs_sysfs_uninit();
  905. unregister_filesystem(&exofs_type);
  906. destroy_inodecache();
  907. }
  908. MODULE_AUTHOR("Avishay Traeger <avishay@gmail.com>");
  909. MODULE_DESCRIPTION("exofs");
  910. MODULE_LICENSE("GPL");
  911. module_init(init_exofs)
  912. module_exit(exit_exofs)