super.c 26 KB

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