super.c 27 KB

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