super.c 26 KB

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