super.c 26 KB

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