super.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  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. 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->oc, &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->oc, &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. static 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 oc;
  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(&oc, &one_comp, sbi, EXOFS_SUPER_ID);
  328. ret = ore_get_io_state(&sbi->layout, &oc, &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_le64(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. static void exofs_free_sbi(struct exofs_sb_info *sbi)
  368. {
  369. unsigned numdevs = sbi->oc.numdevs;
  370. while (numdevs) {
  371. unsigned i = --numdevs;
  372. struct osd_dev *od = ore_comp_dev(&sbi->oc, i);
  373. if (od) {
  374. ore_comp_set_dev(&sbi->oc, i, NULL);
  375. osduld_put_device(od);
  376. }
  377. }
  378. kfree(sbi->oc.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, ore_comp_dev(&sbi->oc, 0),
  402. sbi->one_comp.obj.partition);
  403. exofs_sysfs_sb_del(sbi);
  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. if (likely(odi->systemid_len))
  453. memcpy(odi->systemid, dt_dev->systemid, OSD_SYSTEMID_LEN);
  454. odi->osdname_len = le32_to_cpu(dt_dev->osdname_len);
  455. odi->osdname = dt_dev->osdname;
  456. /* FIXME support long names. Will need a _put function */
  457. if (dt_dev->long_name_offset)
  458. return -EINVAL;
  459. /* Make sure osdname is printable!
  460. * mkexofs should give us space for a null-terminator else the
  461. * device-table is invalid.
  462. */
  463. if (unlikely(odi->osdname_len >= sizeof(dt_dev->osdname)))
  464. odi->osdname_len = sizeof(dt_dev->osdname) - 1;
  465. dt_dev->osdname[odi->osdname_len] = 0;
  466. /* If it's all zeros something is bad we read past end-of-obj */
  467. return !(odi->systemid_len || odi->osdname_len);
  468. }
  469. int __alloc_dev_table(struct exofs_sb_info *sbi, unsigned numdevs,
  470. struct exofs_dev **peds)
  471. {
  472. struct __alloc_ore_devs_and_exofs_devs {
  473. /* Twice bigger table: See exofs_init_comps() and comment at
  474. * exofs_read_lookup_dev_table()
  475. */
  476. struct ore_dev *oreds[numdevs * 2 - 1];
  477. struct exofs_dev eds[numdevs];
  478. } *aoded;
  479. struct exofs_dev *eds;
  480. unsigned i;
  481. aoded = kzalloc(sizeof(*aoded), GFP_KERNEL);
  482. if (unlikely(!aoded)) {
  483. EXOFS_ERR("ERROR: failed allocating Device array[%d]\n",
  484. numdevs);
  485. return -ENOMEM;
  486. }
  487. sbi->oc.ods = aoded->oreds;
  488. *peds = eds = aoded->eds;
  489. for (i = 0; i < numdevs; ++i)
  490. aoded->oreds[i] = &eds[i].ored;
  491. return 0;
  492. }
  493. static int exofs_read_lookup_dev_table(struct exofs_sb_info *sbi,
  494. struct osd_dev *fscb_od,
  495. unsigned table_count)
  496. {
  497. struct ore_comp comp;
  498. struct exofs_device_table *dt;
  499. struct exofs_dev *eds;
  500. unsigned table_bytes = table_count * sizeof(dt->dt_dev_table[0]) +
  501. sizeof(*dt);
  502. unsigned numdevs, i;
  503. int ret;
  504. dt = kmalloc(table_bytes, GFP_KERNEL);
  505. if (unlikely(!dt)) {
  506. EXOFS_ERR("ERROR: allocating %x bytes for device table\n",
  507. table_bytes);
  508. return -ENOMEM;
  509. }
  510. sbi->oc.numdevs = 0;
  511. comp.obj.partition = sbi->one_comp.obj.partition;
  512. comp.obj.id = EXOFS_DEVTABLE_ID;
  513. exofs_make_credential(comp.cred, &comp.obj);
  514. ret = exofs_read_kern(fscb_od, comp.cred, &comp.obj, 0, dt,
  515. table_bytes);
  516. if (unlikely(ret)) {
  517. EXOFS_ERR("ERROR: reading device table\n");
  518. goto out;
  519. }
  520. numdevs = le64_to_cpu(dt->dt_num_devices);
  521. if (unlikely(!numdevs)) {
  522. ret = -EINVAL;
  523. goto out;
  524. }
  525. WARN_ON(table_count != numdevs);
  526. ret = _read_and_match_data_map(sbi, numdevs, dt);
  527. if (unlikely(ret))
  528. goto out;
  529. ret = __alloc_dev_table(sbi, numdevs, &eds);
  530. if (unlikely(ret))
  531. goto out;
  532. /* exofs round-robins the device table view according to inode
  533. * number. We hold a: twice bigger table hence inodes can point
  534. * to any device and have a sequential view of the table
  535. * starting at this device. See exofs_init_comps()
  536. */
  537. memcpy(&sbi->oc.ods[numdevs], &sbi->oc.ods[0],
  538. (numdevs - 1) * sizeof(sbi->oc.ods[0]));
  539. /* create sysfs subdir under which we put the device table
  540. * And cluster layout. A Superblock is identified by the string:
  541. * "dev[0].osdname"_"pid"
  542. */
  543. exofs_sysfs_sb_add(sbi, &dt->dt_dev_table[0]);
  544. for (i = 0; i < numdevs; i++) {
  545. struct exofs_fscb fscb;
  546. struct osd_dev_info odi;
  547. struct osd_dev *od;
  548. if (exofs_devs_2_odi(&dt->dt_dev_table[i], &odi)) {
  549. EXOFS_ERR("ERROR: Read all-zeros device entry\n");
  550. ret = -EINVAL;
  551. goto out;
  552. }
  553. printk(KERN_NOTICE "Add device[%d]: osd_name-%s\n",
  554. i, odi.osdname);
  555. /* the exofs id is currently the table index */
  556. eds[i].did = i;
  557. /* On all devices the device table is identical. The user can
  558. * specify any one of the participating devices on the command
  559. * line. We always keep them in device-table order.
  560. */
  561. if (fscb_od && osduld_device_same(fscb_od, &odi)) {
  562. eds[i].ored.od = fscb_od;
  563. ++sbi->oc.numdevs;
  564. fscb_od = NULL;
  565. exofs_sysfs_odev_add(&eds[i], sbi);
  566. continue;
  567. }
  568. od = osduld_info_lookup(&odi);
  569. if (IS_ERR(od)) {
  570. ret = PTR_ERR(od);
  571. EXOFS_ERR("ERROR: device requested is not found "
  572. "osd_name-%s =>%d\n", odi.osdname, ret);
  573. goto out;
  574. }
  575. eds[i].ored.od = od;
  576. ++sbi->oc.numdevs;
  577. /* Read the fscb of the other devices to make sure the FS
  578. * partition is there.
  579. */
  580. ret = exofs_read_kern(od, comp.cred, &comp.obj, 0, &fscb,
  581. sizeof(fscb));
  582. if (unlikely(ret)) {
  583. EXOFS_ERR("ERROR: Malformed participating device "
  584. "error reading fscb osd_name-%s\n",
  585. odi.osdname);
  586. goto out;
  587. }
  588. exofs_sysfs_odev_add(&eds[i], sbi);
  589. /* TODO: verify other information is correct and FS-uuid
  590. * matches. Benny what did you say about device table
  591. * generation and old devices?
  592. */
  593. }
  594. out:
  595. kfree(dt);
  596. if (unlikely(fscb_od && !ret)) {
  597. EXOFS_ERR("ERROR: Bad device-table container device not present\n");
  598. osduld_put_device(fscb_od);
  599. return -EINVAL;
  600. }
  601. return ret;
  602. }
  603. /*
  604. * Read the superblock from the OSD and fill in the fields
  605. */
  606. static int exofs_fill_super(struct super_block *sb, void *data, int silent)
  607. {
  608. struct inode *root;
  609. struct exofs_mountopt *opts = data;
  610. struct exofs_sb_info *sbi; /*extended info */
  611. struct osd_dev *od; /* Master device */
  612. struct exofs_fscb fscb; /*on-disk superblock info */
  613. struct ore_comp comp;
  614. unsigned table_count;
  615. int ret;
  616. sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
  617. if (!sbi)
  618. return -ENOMEM;
  619. /* use mount options to fill superblock */
  620. if (opts->is_osdname) {
  621. struct osd_dev_info odi = {.systemid_len = 0};
  622. odi.osdname_len = strlen(opts->dev_name);
  623. odi.osdname = (u8 *)opts->dev_name;
  624. od = osduld_info_lookup(&odi);
  625. kfree(opts->dev_name);
  626. opts->dev_name = NULL;
  627. } else {
  628. od = osduld_path_lookup(opts->dev_name);
  629. }
  630. if (IS_ERR(od)) {
  631. ret = -EINVAL;
  632. goto free_sbi;
  633. }
  634. /* Default layout in case we do not have a device-table */
  635. sbi->layout.stripe_unit = PAGE_SIZE;
  636. sbi->layout.mirrors_p1 = 1;
  637. sbi->layout.group_width = 1;
  638. sbi->layout.group_depth = -1;
  639. sbi->layout.group_count = 1;
  640. sbi->s_timeout = opts->timeout;
  641. sbi->one_comp.obj.partition = opts->pid;
  642. sbi->one_comp.obj.id = 0;
  643. exofs_make_credential(sbi->one_comp.cred, &sbi->one_comp.obj);
  644. sbi->oc.single_comp = EC_SINGLE_COMP;
  645. sbi->oc.comps = &sbi->one_comp;
  646. /* fill in some other data by hand */
  647. memset(sb->s_id, 0, sizeof(sb->s_id));
  648. strcpy(sb->s_id, "exofs");
  649. sb->s_blocksize = EXOFS_BLKSIZE;
  650. sb->s_blocksize_bits = EXOFS_BLKSHIFT;
  651. sb->s_maxbytes = MAX_LFS_FILESIZE;
  652. sb->s_max_links = EXOFS_LINK_MAX;
  653. atomic_set(&sbi->s_curr_pending, 0);
  654. sb->s_bdev = NULL;
  655. sb->s_dev = 0;
  656. comp.obj.partition = sbi->one_comp.obj.partition;
  657. comp.obj.id = EXOFS_SUPER_ID;
  658. exofs_make_credential(comp.cred, &comp.obj);
  659. ret = exofs_read_kern(od, comp.cred, &comp.obj, 0, &fscb, sizeof(fscb));
  660. if (unlikely(ret))
  661. goto free_sbi;
  662. sb->s_magic = le16_to_cpu(fscb.s_magic);
  663. /* NOTE: we read below to be backward compatible with old versions */
  664. sbi->s_nextid = le64_to_cpu(fscb.s_nextid);
  665. sbi->s_numfiles = le32_to_cpu(fscb.s_numfiles);
  666. /* make sure what we read from the object store is correct */
  667. if (sb->s_magic != EXOFS_SUPER_MAGIC) {
  668. if (!silent)
  669. EXOFS_ERR("ERROR: Bad magic value\n");
  670. ret = -EINVAL;
  671. goto free_sbi;
  672. }
  673. if (le32_to_cpu(fscb.s_version) > EXOFS_FSCB_VER) {
  674. EXOFS_ERR("ERROR: Bad FSCB version expected-%d got-%d\n",
  675. EXOFS_FSCB_VER, le32_to_cpu(fscb.s_version));
  676. ret = -EINVAL;
  677. goto free_sbi;
  678. }
  679. /* start generation numbers from a random point */
  680. get_random_bytes(&sbi->s_next_generation, sizeof(u32));
  681. spin_lock_init(&sbi->s_next_gen_lock);
  682. table_count = le64_to_cpu(fscb.s_dev_table_count);
  683. if (table_count) {
  684. ret = exofs_read_lookup_dev_table(sbi, od, table_count);
  685. if (unlikely(ret))
  686. goto free_sbi;
  687. } else {
  688. struct exofs_dev *eds;
  689. ret = __alloc_dev_table(sbi, 1, &eds);
  690. if (unlikely(ret))
  691. goto free_sbi;
  692. ore_comp_set_dev(&sbi->oc, 0, od);
  693. sbi->oc.numdevs = 1;
  694. }
  695. __sbi_read_stats(sbi);
  696. /* set up operation vectors */
  697. sbi->bdi.ra_pages = __ra_pages(&sbi->layout);
  698. sb->s_bdi = &sbi->bdi;
  699. sb->s_fs_info = sbi;
  700. sb->s_op = &exofs_sops;
  701. sb->s_export_op = &exofs_export_ops;
  702. root = exofs_iget(sb, EXOFS_ROOT_ID - EXOFS_OBJ_OFF);
  703. if (IS_ERR(root)) {
  704. EXOFS_ERR("ERROR: exofs_iget failed\n");
  705. ret = PTR_ERR(root);
  706. goto free_sbi;
  707. }
  708. sb->s_root = d_make_root(root);
  709. if (!sb->s_root) {
  710. EXOFS_ERR("ERROR: get root inode failed\n");
  711. ret = -ENOMEM;
  712. goto free_sbi;
  713. }
  714. if (!S_ISDIR(root->i_mode)) {
  715. dput(sb->s_root);
  716. sb->s_root = NULL;
  717. EXOFS_ERR("ERROR: corrupt root inode (mode = %hd)\n",
  718. root->i_mode);
  719. ret = -EINVAL;
  720. goto free_sbi;
  721. }
  722. ret = bdi_setup_and_register(&sbi->bdi, "exofs", BDI_CAP_MAP_COPY);
  723. if (ret) {
  724. EXOFS_DBGMSG("Failed to bdi_setup_and_register\n");
  725. dput(sb->s_root);
  726. sb->s_root = NULL;
  727. goto free_sbi;
  728. }
  729. exofs_sysfs_dbg_print();
  730. _exofs_print_device("Mounting", opts->dev_name,
  731. ore_comp_dev(&sbi->oc, 0),
  732. sbi->one_comp.obj.partition);
  733. return 0;
  734. free_sbi:
  735. EXOFS_ERR("Unable to mount exofs on %s pid=0x%llx err=%d\n",
  736. opts->dev_name, sbi->one_comp.obj.partition, ret);
  737. exofs_free_sbi(sbi);
  738. return ret;
  739. }
  740. /*
  741. * Set up the superblock (calls exofs_fill_super eventually)
  742. */
  743. static struct dentry *exofs_mount(struct file_system_type *type,
  744. int flags, const char *dev_name,
  745. void *data)
  746. {
  747. struct exofs_mountopt opts;
  748. int ret;
  749. ret = parse_options(data, &opts);
  750. if (ret)
  751. return ERR_PTR(ret);
  752. if (!opts.dev_name)
  753. opts.dev_name = dev_name;
  754. return mount_nodev(type, flags, &opts, exofs_fill_super);
  755. }
  756. /*
  757. * Return information about the file system state in the buffer. This is used
  758. * by the 'df' command, for example.
  759. */
  760. static int exofs_statfs(struct dentry *dentry, struct kstatfs *buf)
  761. {
  762. struct super_block *sb = dentry->d_sb;
  763. struct exofs_sb_info *sbi = sb->s_fs_info;
  764. struct ore_io_state *ios;
  765. struct osd_attr attrs[] = {
  766. ATTR_DEF(OSD_APAGE_PARTITION_QUOTAS,
  767. OSD_ATTR_PQ_CAPACITY_QUOTA, sizeof(__be64)),
  768. ATTR_DEF(OSD_APAGE_PARTITION_INFORMATION,
  769. OSD_ATTR_PI_USED_CAPACITY, sizeof(__be64)),
  770. };
  771. uint64_t capacity = ULLONG_MAX;
  772. uint64_t used = ULLONG_MAX;
  773. int ret;
  774. ret = ore_get_io_state(&sbi->layout, &sbi->oc, &ios);
  775. if (ret) {
  776. EXOFS_DBGMSG("ore_get_io_state failed.\n");
  777. return ret;
  778. }
  779. ios->in_attr = attrs;
  780. ios->in_attr_len = ARRAY_SIZE(attrs);
  781. ret = ore_read(ios);
  782. if (unlikely(ret))
  783. goto out;
  784. ret = extract_attr_from_ios(ios, &attrs[0]);
  785. if (likely(!ret)) {
  786. capacity = get_unaligned_be64(attrs[0].val_ptr);
  787. if (unlikely(!capacity))
  788. capacity = ULLONG_MAX;
  789. } else
  790. EXOFS_DBGMSG("exofs_statfs: get capacity failed.\n");
  791. ret = extract_attr_from_ios(ios, &attrs[1]);
  792. if (likely(!ret))
  793. used = get_unaligned_be64(attrs[1].val_ptr);
  794. else
  795. EXOFS_DBGMSG("exofs_statfs: get used-space failed.\n");
  796. /* fill in the stats buffer */
  797. buf->f_type = EXOFS_SUPER_MAGIC;
  798. buf->f_bsize = EXOFS_BLKSIZE;
  799. buf->f_blocks = capacity >> 9;
  800. buf->f_bfree = (capacity - used) >> 9;
  801. buf->f_bavail = buf->f_bfree;
  802. buf->f_files = sbi->s_numfiles;
  803. buf->f_ffree = EXOFS_MAX_ID - sbi->s_numfiles;
  804. buf->f_namelen = EXOFS_NAME_LEN;
  805. out:
  806. ore_put_io_state(ios);
  807. return ret;
  808. }
  809. static const struct super_operations exofs_sops = {
  810. .alloc_inode = exofs_alloc_inode,
  811. .destroy_inode = exofs_destroy_inode,
  812. .write_inode = exofs_write_inode,
  813. .evict_inode = exofs_evict_inode,
  814. .put_super = exofs_put_super,
  815. .write_super = exofs_write_super,
  816. .sync_fs = exofs_sync_fs,
  817. .statfs = exofs_statfs,
  818. };
  819. /******************************************************************************
  820. * EXPORT OPERATIONS
  821. *****************************************************************************/
  822. static struct dentry *exofs_get_parent(struct dentry *child)
  823. {
  824. unsigned long ino = exofs_parent_ino(child);
  825. if (!ino)
  826. return ERR_PTR(-ESTALE);
  827. return d_obtain_alias(exofs_iget(child->d_inode->i_sb, ino));
  828. }
  829. static struct inode *exofs_nfs_get_inode(struct super_block *sb,
  830. u64 ino, u32 generation)
  831. {
  832. struct inode *inode;
  833. inode = exofs_iget(sb, ino);
  834. if (IS_ERR(inode))
  835. return ERR_CAST(inode);
  836. if (generation && inode->i_generation != generation) {
  837. /* we didn't find the right inode.. */
  838. iput(inode);
  839. return ERR_PTR(-ESTALE);
  840. }
  841. return inode;
  842. }
  843. static struct dentry *exofs_fh_to_dentry(struct super_block *sb,
  844. struct fid *fid, int fh_len, int fh_type)
  845. {
  846. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  847. exofs_nfs_get_inode);
  848. }
  849. static struct dentry *exofs_fh_to_parent(struct super_block *sb,
  850. struct fid *fid, int fh_len, int fh_type)
  851. {
  852. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  853. exofs_nfs_get_inode);
  854. }
  855. static const struct export_operations exofs_export_ops = {
  856. .fh_to_dentry = exofs_fh_to_dentry,
  857. .fh_to_parent = exofs_fh_to_parent,
  858. .get_parent = exofs_get_parent,
  859. };
  860. /******************************************************************************
  861. * INSMOD/RMMOD
  862. *****************************************************************************/
  863. /*
  864. * struct that describes this file system
  865. */
  866. static struct file_system_type exofs_type = {
  867. .owner = THIS_MODULE,
  868. .name = "exofs",
  869. .mount = exofs_mount,
  870. .kill_sb = generic_shutdown_super,
  871. };
  872. static int __init init_exofs(void)
  873. {
  874. int err;
  875. err = init_inodecache();
  876. if (err)
  877. goto out;
  878. err = register_filesystem(&exofs_type);
  879. if (err)
  880. goto out_d;
  881. /* We don't fail if sysfs creation failed */
  882. exofs_sysfs_init();
  883. return 0;
  884. out_d:
  885. destroy_inodecache();
  886. out:
  887. return err;
  888. }
  889. static void __exit exit_exofs(void)
  890. {
  891. exofs_sysfs_uninit();
  892. unregister_filesystem(&exofs_type);
  893. destroy_inodecache();
  894. }
  895. MODULE_AUTHOR("Avishay Traeger <avishay@gmail.com>");
  896. MODULE_DESCRIPTION("exofs");
  897. MODULE_LICENSE("GPL");
  898. module_init(init_exofs)
  899. module_exit(exit_exofs)