super.c 27 KB

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