super.c 27 KB

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