super.c 25 KB

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