super.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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. /******************************************************************************
  41. * MOUNT OPTIONS
  42. *****************************************************************************/
  43. /*
  44. * struct to hold what we get from mount options
  45. */
  46. struct exofs_mountopt {
  47. const char *dev_name;
  48. uint64_t pid;
  49. int timeout;
  50. };
  51. /*
  52. * exofs-specific mount-time options.
  53. */
  54. enum { Opt_pid, Opt_to, Opt_mkfs, Opt_format, Opt_err };
  55. /*
  56. * Our mount-time options. These should ideally be 64-bit unsigned, but the
  57. * kernel's parsing functions do not currently support that. 32-bit should be
  58. * sufficient for most applications now.
  59. */
  60. static match_table_t tokens = {
  61. {Opt_pid, "pid=%u"},
  62. {Opt_to, "to=%u"},
  63. {Opt_err, NULL}
  64. };
  65. /*
  66. * The main option parsing method. Also makes sure that all of the mandatory
  67. * mount options were set.
  68. */
  69. static int parse_options(char *options, struct exofs_mountopt *opts)
  70. {
  71. char *p;
  72. substring_t args[MAX_OPT_ARGS];
  73. int option;
  74. bool s_pid = false;
  75. EXOFS_DBGMSG("parse_options %s\n", options);
  76. /* defaults */
  77. memset(opts, 0, sizeof(*opts));
  78. opts->timeout = BLK_DEFAULT_SG_TIMEOUT;
  79. while ((p = strsep(&options, ",")) != NULL) {
  80. int token;
  81. char str[32];
  82. if (!*p)
  83. continue;
  84. token = match_token(p, tokens, args);
  85. switch (token) {
  86. case Opt_pid:
  87. if (0 == match_strlcpy(str, &args[0], sizeof(str)))
  88. return -EINVAL;
  89. opts->pid = simple_strtoull(str, NULL, 0);
  90. if (opts->pid < EXOFS_MIN_PID) {
  91. EXOFS_ERR("Partition ID must be >= %u",
  92. EXOFS_MIN_PID);
  93. return -EINVAL;
  94. }
  95. s_pid = 1;
  96. break;
  97. case Opt_to:
  98. if (match_int(&args[0], &option))
  99. return -EINVAL;
  100. if (option <= 0) {
  101. EXOFS_ERR("Timout must be > 0");
  102. return -EINVAL;
  103. }
  104. opts->timeout = option * HZ;
  105. break;
  106. }
  107. }
  108. if (!s_pid) {
  109. EXOFS_ERR("Need to specify the following options:\n");
  110. EXOFS_ERR(" -o pid=pid_no_to_use\n");
  111. return -EINVAL;
  112. }
  113. return 0;
  114. }
  115. /******************************************************************************
  116. * INODE CACHE
  117. *****************************************************************************/
  118. /*
  119. * Our inode cache. Isn't it pretty?
  120. */
  121. static struct kmem_cache *exofs_inode_cachep;
  122. /*
  123. * Allocate an inode in the cache
  124. */
  125. static struct inode *exofs_alloc_inode(struct super_block *sb)
  126. {
  127. struct exofs_i_info *oi;
  128. oi = kmem_cache_alloc(exofs_inode_cachep, GFP_KERNEL);
  129. if (!oi)
  130. return NULL;
  131. oi->vfs_inode.i_version = 1;
  132. return &oi->vfs_inode;
  133. }
  134. static void exofs_i_callback(struct rcu_head *head)
  135. {
  136. struct inode *inode = container_of(head, struct inode, i_rcu);
  137. INIT_LIST_HEAD(&inode->i_dentry);
  138. kmem_cache_free(exofs_inode_cachep, exofs_i(inode));
  139. }
  140. /*
  141. * Remove an inode from the cache
  142. */
  143. static void exofs_destroy_inode(struct inode *inode)
  144. {
  145. call_rcu(&inode->i_rcu, exofs_i_callback);
  146. }
  147. /*
  148. * Initialize the inode
  149. */
  150. static void exofs_init_once(void *foo)
  151. {
  152. struct exofs_i_info *oi = foo;
  153. inode_init_once(&oi->vfs_inode);
  154. }
  155. /*
  156. * Create and initialize the inode cache
  157. */
  158. static int init_inodecache(void)
  159. {
  160. exofs_inode_cachep = kmem_cache_create("exofs_inode_cache",
  161. sizeof(struct exofs_i_info), 0,
  162. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
  163. exofs_init_once);
  164. if (exofs_inode_cachep == NULL)
  165. return -ENOMEM;
  166. return 0;
  167. }
  168. /*
  169. * Destroy the inode cache
  170. */
  171. static void destroy_inodecache(void)
  172. {
  173. kmem_cache_destroy(exofs_inode_cachep);
  174. }
  175. /******************************************************************************
  176. * SUPERBLOCK FUNCTIONS
  177. *****************************************************************************/
  178. static const struct super_operations exofs_sops;
  179. static const struct export_operations exofs_export_ops;
  180. /*
  181. * Write the superblock to the OSD
  182. */
  183. int exofs_sync_fs(struct super_block *sb, int wait)
  184. {
  185. struct exofs_sb_info *sbi;
  186. struct exofs_fscb *fscb;
  187. struct exofs_io_state *ios;
  188. int ret = -ENOMEM;
  189. lock_super(sb);
  190. sbi = sb->s_fs_info;
  191. fscb = &sbi->s_fscb;
  192. ret = exofs_get_io_state(&sbi->layout, &ios);
  193. if (ret)
  194. goto out;
  195. /* Note: We only write the changing part of the fscb. .i.e upto the
  196. * the fscb->s_dev_table_oid member. There is no read-modify-write
  197. * here.
  198. */
  199. ios->length = offsetof(struct exofs_fscb, s_dev_table_oid);
  200. memset(fscb, 0, ios->length);
  201. fscb->s_nextid = cpu_to_le64(sbi->s_nextid);
  202. fscb->s_numfiles = cpu_to_le32(sbi->s_numfiles);
  203. fscb->s_magic = cpu_to_le16(sb->s_magic);
  204. fscb->s_newfs = 0;
  205. fscb->s_version = EXOFS_FSCB_VER;
  206. ios->obj.id = EXOFS_SUPER_ID;
  207. ios->offset = 0;
  208. ios->kern_buff = fscb;
  209. ios->cred = sbi->s_cred;
  210. ret = exofs_sbi_write(ios);
  211. if (unlikely(ret)) {
  212. EXOFS_ERR("%s: exofs_sbi_write failed.\n", __func__);
  213. goto out;
  214. }
  215. sb->s_dirt = 0;
  216. out:
  217. EXOFS_DBGMSG("s_nextid=0x%llx ret=%d\n", _LLU(sbi->s_nextid), ret);
  218. exofs_put_io_state(ios);
  219. unlock_super(sb);
  220. return ret;
  221. }
  222. static void exofs_write_super(struct super_block *sb)
  223. {
  224. if (!(sb->s_flags & MS_RDONLY))
  225. exofs_sync_fs(sb, 1);
  226. else
  227. sb->s_dirt = 0;
  228. }
  229. static void _exofs_print_device(const char *msg, const char *dev_path,
  230. struct osd_dev *od, u64 pid)
  231. {
  232. const struct osd_dev_info *odi = osduld_device_info(od);
  233. printk(KERN_NOTICE "exofs: %s %s osd_name-%s pid-0x%llx\n",
  234. msg, dev_path ?: "", odi->osdname, _LLU(pid));
  235. }
  236. void exofs_free_sbi(struct exofs_sb_info *sbi)
  237. {
  238. while (sbi->layout.s_numdevs) {
  239. int i = --sbi->layout.s_numdevs;
  240. struct osd_dev *od = sbi->layout.s_ods[i];
  241. if (od) {
  242. sbi->layout.s_ods[i] = NULL;
  243. osduld_put_device(od);
  244. }
  245. }
  246. kfree(sbi);
  247. }
  248. /*
  249. * This function is called when the vfs is freeing the superblock. We just
  250. * need to free our own part.
  251. */
  252. static void exofs_put_super(struct super_block *sb)
  253. {
  254. int num_pend;
  255. struct exofs_sb_info *sbi = sb->s_fs_info;
  256. if (sb->s_dirt)
  257. exofs_write_super(sb);
  258. /* make sure there are no pending commands */
  259. for (num_pend = atomic_read(&sbi->s_curr_pending); num_pend > 0;
  260. num_pend = atomic_read(&sbi->s_curr_pending)) {
  261. wait_queue_head_t wq;
  262. init_waitqueue_head(&wq);
  263. wait_event_timeout(wq,
  264. (atomic_read(&sbi->s_curr_pending) == 0),
  265. msecs_to_jiffies(100));
  266. }
  267. _exofs_print_device("Unmounting", NULL, sbi->layout.s_ods[0],
  268. sbi->layout.s_pid);
  269. bdi_destroy(&sbi->bdi);
  270. exofs_free_sbi(sbi);
  271. sb->s_fs_info = NULL;
  272. }
  273. static int _read_and_match_data_map(struct exofs_sb_info *sbi, unsigned numdevs,
  274. struct exofs_device_table *dt)
  275. {
  276. u64 stripe_length;
  277. sbi->data_map.odm_num_comps =
  278. le32_to_cpu(dt->dt_data_map.cb_num_comps);
  279. sbi->data_map.odm_stripe_unit =
  280. le64_to_cpu(dt->dt_data_map.cb_stripe_unit);
  281. sbi->data_map.odm_group_width =
  282. le32_to_cpu(dt->dt_data_map.cb_group_width);
  283. sbi->data_map.odm_group_depth =
  284. le32_to_cpu(dt->dt_data_map.cb_group_depth);
  285. sbi->data_map.odm_mirror_cnt =
  286. le32_to_cpu(dt->dt_data_map.cb_mirror_cnt);
  287. sbi->data_map.odm_raid_algorithm =
  288. le32_to_cpu(dt->dt_data_map.cb_raid_algorithm);
  289. /* FIXME: Only raid0 for now. if not so, do not mount */
  290. if (sbi->data_map.odm_num_comps != numdevs) {
  291. EXOFS_ERR("odm_num_comps(%u) != numdevs(%u)\n",
  292. sbi->data_map.odm_num_comps, numdevs);
  293. return -EINVAL;
  294. }
  295. if (sbi->data_map.odm_raid_algorithm != PNFS_OSD_RAID_0) {
  296. EXOFS_ERR("Only RAID_0 for now\n");
  297. return -EINVAL;
  298. }
  299. if (0 != (numdevs % (sbi->data_map.odm_mirror_cnt + 1))) {
  300. EXOFS_ERR("Data Map wrong, numdevs=%d mirrors=%d\n",
  301. numdevs, sbi->data_map.odm_mirror_cnt);
  302. return -EINVAL;
  303. }
  304. if (0 != (sbi->data_map.odm_stripe_unit & ~PAGE_MASK)) {
  305. EXOFS_ERR("Stripe Unit(0x%llx)"
  306. " must be Multples of PAGE_SIZE(0x%lx)\n",
  307. _LLU(sbi->data_map.odm_stripe_unit), PAGE_SIZE);
  308. return -EINVAL;
  309. }
  310. sbi->layout.stripe_unit = sbi->data_map.odm_stripe_unit;
  311. sbi->layout.mirrors_p1 = sbi->data_map.odm_mirror_cnt + 1;
  312. if (sbi->data_map.odm_group_width) {
  313. sbi->layout.group_width = sbi->data_map.odm_group_width;
  314. sbi->layout.group_depth = sbi->data_map.odm_group_depth;
  315. if (!sbi->layout.group_depth) {
  316. EXOFS_ERR("group_depth == 0 && group_width != 0\n");
  317. return -EINVAL;
  318. }
  319. sbi->layout.group_count = sbi->data_map.odm_num_comps /
  320. sbi->layout.mirrors_p1 /
  321. sbi->data_map.odm_group_width;
  322. } else {
  323. if (sbi->data_map.odm_group_depth) {
  324. printk(KERN_NOTICE "Warning: group_depth ignored "
  325. "group_width == 0 && group_depth == %d\n",
  326. sbi->data_map.odm_group_depth);
  327. sbi->data_map.odm_group_depth = 0;
  328. }
  329. sbi->layout.group_width = sbi->data_map.odm_num_comps /
  330. sbi->layout.mirrors_p1;
  331. sbi->layout.group_depth = -1;
  332. sbi->layout.group_count = 1;
  333. }
  334. stripe_length = (u64)sbi->layout.group_width * sbi->layout.stripe_unit;
  335. if (stripe_length >= (1ULL << 32)) {
  336. EXOFS_ERR("Total Stripe length(0x%llx)"
  337. " >= 32bit is not supported\n", _LLU(stripe_length));
  338. return -EINVAL;
  339. }
  340. return 0;
  341. }
  342. /* @odi is valid only as long as @fscb_dev is valid */
  343. static int exofs_devs_2_odi(struct exofs_dt_device_info *dt_dev,
  344. struct osd_dev_info *odi)
  345. {
  346. odi->systemid_len = le32_to_cpu(dt_dev->systemid_len);
  347. memcpy(odi->systemid, dt_dev->systemid, odi->systemid_len);
  348. odi->osdname_len = le32_to_cpu(dt_dev->osdname_len);
  349. odi->osdname = dt_dev->osdname;
  350. /* FIXME support long names. Will need a _put function */
  351. if (dt_dev->long_name_offset)
  352. return -EINVAL;
  353. /* Make sure osdname is printable!
  354. * mkexofs should give us space for a null-terminator else the
  355. * device-table is invalid.
  356. */
  357. if (unlikely(odi->osdname_len >= sizeof(dt_dev->osdname)))
  358. odi->osdname_len = sizeof(dt_dev->osdname) - 1;
  359. dt_dev->osdname[odi->osdname_len] = 0;
  360. /* If it's all zeros something is bad we read past end-of-obj */
  361. return !(odi->systemid_len || odi->osdname_len);
  362. }
  363. static int exofs_read_lookup_dev_table(struct exofs_sb_info **psbi,
  364. unsigned table_count)
  365. {
  366. struct exofs_sb_info *sbi = *psbi;
  367. struct osd_dev *fscb_od;
  368. struct osd_obj_id obj = {.partition = sbi->layout.s_pid,
  369. .id = EXOFS_DEVTABLE_ID};
  370. struct exofs_device_table *dt;
  371. unsigned table_bytes = table_count * sizeof(dt->dt_dev_table[0]) +
  372. sizeof(*dt);
  373. unsigned numdevs, i;
  374. int ret;
  375. dt = kmalloc(table_bytes, GFP_KERNEL);
  376. if (unlikely(!dt)) {
  377. EXOFS_ERR("ERROR: allocating %x bytes for device table\n",
  378. table_bytes);
  379. return -ENOMEM;
  380. }
  381. fscb_od = sbi->layout.s_ods[0];
  382. sbi->layout.s_ods[0] = NULL;
  383. sbi->layout.s_numdevs = 0;
  384. ret = exofs_read_kern(fscb_od, sbi->s_cred, &obj, 0, dt, table_bytes);
  385. if (unlikely(ret)) {
  386. EXOFS_ERR("ERROR: reading device table\n");
  387. goto out;
  388. }
  389. numdevs = le64_to_cpu(dt->dt_num_devices);
  390. if (unlikely(!numdevs)) {
  391. ret = -EINVAL;
  392. goto out;
  393. }
  394. WARN_ON(table_count != numdevs);
  395. ret = _read_and_match_data_map(sbi, numdevs, dt);
  396. if (unlikely(ret))
  397. goto out;
  398. if (likely(numdevs > 1)) {
  399. unsigned size = numdevs * sizeof(sbi->layout.s_ods[0]);
  400. sbi = krealloc(sbi, sizeof(*sbi) + size, GFP_KERNEL);
  401. if (unlikely(!sbi)) {
  402. ret = -ENOMEM;
  403. goto out;
  404. }
  405. memset(&sbi->layout.s_ods[1], 0,
  406. size - sizeof(sbi->layout.s_ods[0]));
  407. *psbi = sbi;
  408. }
  409. for (i = 0; i < numdevs; i++) {
  410. struct exofs_fscb fscb;
  411. struct osd_dev_info odi;
  412. struct osd_dev *od;
  413. if (exofs_devs_2_odi(&dt->dt_dev_table[i], &odi)) {
  414. EXOFS_ERR("ERROR: Read all-zeros device entry\n");
  415. ret = -EINVAL;
  416. goto out;
  417. }
  418. printk(KERN_NOTICE "Add device[%d]: osd_name-%s\n",
  419. i, odi.osdname);
  420. /* On all devices the device table is identical. The user can
  421. * specify any one of the participating devices on the command
  422. * line. We always keep them in device-table order.
  423. */
  424. if (fscb_od && osduld_device_same(fscb_od, &odi)) {
  425. sbi->layout.s_ods[i] = fscb_od;
  426. ++sbi->layout.s_numdevs;
  427. fscb_od = NULL;
  428. continue;
  429. }
  430. od = osduld_info_lookup(&odi);
  431. if (unlikely(IS_ERR(od))) {
  432. ret = PTR_ERR(od);
  433. EXOFS_ERR("ERROR: device requested is not found "
  434. "osd_name-%s =>%d\n", odi.osdname, ret);
  435. goto out;
  436. }
  437. sbi->layout.s_ods[i] = od;
  438. ++sbi->layout.s_numdevs;
  439. /* Read the fscb of the other devices to make sure the FS
  440. * partition is there.
  441. */
  442. ret = exofs_read_kern(od, sbi->s_cred, &obj, 0, &fscb,
  443. sizeof(fscb));
  444. if (unlikely(ret)) {
  445. EXOFS_ERR("ERROR: Malformed participating device "
  446. "error reading fscb osd_name-%s\n",
  447. odi.osdname);
  448. goto out;
  449. }
  450. /* TODO: verify other information is correct and FS-uuid
  451. * matches. Benny what did you say about device table
  452. * generation and old devices?
  453. */
  454. }
  455. out:
  456. kfree(dt);
  457. if (unlikely(!ret && fscb_od)) {
  458. EXOFS_ERR(
  459. "ERROR: Bad device-table container device not present\n");
  460. osduld_put_device(fscb_od);
  461. ret = -EINVAL;
  462. }
  463. return ret;
  464. }
  465. /*
  466. * Read the superblock from the OSD and fill in the fields
  467. */
  468. static int exofs_fill_super(struct super_block *sb, void *data, int silent)
  469. {
  470. struct inode *root;
  471. struct exofs_mountopt *opts = data;
  472. struct exofs_sb_info *sbi; /*extended info */
  473. struct osd_dev *od; /* Master device */
  474. struct exofs_fscb fscb; /*on-disk superblock info */
  475. struct osd_obj_id obj;
  476. unsigned table_count;
  477. int ret;
  478. sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
  479. if (!sbi)
  480. return -ENOMEM;
  481. ret = bdi_setup_and_register(&sbi->bdi, "exofs", BDI_CAP_MAP_COPY);
  482. if (ret)
  483. goto free_bdi;
  484. /* use mount options to fill superblock */
  485. od = osduld_path_lookup(opts->dev_name);
  486. if (IS_ERR(od)) {
  487. ret = PTR_ERR(od);
  488. goto free_sbi;
  489. }
  490. /* Default layout in case we do not have a device-table */
  491. sbi->layout.stripe_unit = PAGE_SIZE;
  492. sbi->layout.mirrors_p1 = 1;
  493. sbi->layout.group_width = 1;
  494. sbi->layout.group_depth = -1;
  495. sbi->layout.group_count = 1;
  496. sbi->layout.s_ods[0] = od;
  497. sbi->layout.s_numdevs = 1;
  498. sbi->layout.s_pid = opts->pid;
  499. sbi->s_timeout = opts->timeout;
  500. /* fill in some other data by hand */
  501. memset(sb->s_id, 0, sizeof(sb->s_id));
  502. strcpy(sb->s_id, "exofs");
  503. sb->s_blocksize = EXOFS_BLKSIZE;
  504. sb->s_blocksize_bits = EXOFS_BLKSHIFT;
  505. sb->s_maxbytes = MAX_LFS_FILESIZE;
  506. atomic_set(&sbi->s_curr_pending, 0);
  507. sb->s_bdev = NULL;
  508. sb->s_dev = 0;
  509. obj.partition = sbi->layout.s_pid;
  510. obj.id = EXOFS_SUPER_ID;
  511. exofs_make_credential(sbi->s_cred, &obj);
  512. ret = exofs_read_kern(od, sbi->s_cred, &obj, 0, &fscb, sizeof(fscb));
  513. if (unlikely(ret))
  514. goto free_sbi;
  515. sb->s_magic = le16_to_cpu(fscb.s_magic);
  516. sbi->s_nextid = le64_to_cpu(fscb.s_nextid);
  517. sbi->s_numfiles = le32_to_cpu(fscb.s_numfiles);
  518. /* make sure what we read from the object store is correct */
  519. if (sb->s_magic != EXOFS_SUPER_MAGIC) {
  520. if (!silent)
  521. EXOFS_ERR("ERROR: Bad magic value\n");
  522. ret = -EINVAL;
  523. goto free_sbi;
  524. }
  525. if (le32_to_cpu(fscb.s_version) != EXOFS_FSCB_VER) {
  526. EXOFS_ERR("ERROR: Bad FSCB version expected-%d got-%d\n",
  527. EXOFS_FSCB_VER, le32_to_cpu(fscb.s_version));
  528. ret = -EINVAL;
  529. goto free_sbi;
  530. }
  531. /* start generation numbers from a random point */
  532. get_random_bytes(&sbi->s_next_generation, sizeof(u32));
  533. spin_lock_init(&sbi->s_next_gen_lock);
  534. table_count = le64_to_cpu(fscb.s_dev_table_count);
  535. if (table_count) {
  536. ret = exofs_read_lookup_dev_table(&sbi, table_count);
  537. if (unlikely(ret))
  538. goto free_sbi;
  539. }
  540. /* set up operation vectors */
  541. sb->s_bdi = &sbi->bdi;
  542. sb->s_fs_info = sbi;
  543. sb->s_op = &exofs_sops;
  544. sb->s_export_op = &exofs_export_ops;
  545. root = exofs_iget(sb, EXOFS_ROOT_ID - EXOFS_OBJ_OFF);
  546. if (IS_ERR(root)) {
  547. EXOFS_ERR("ERROR: exofs_iget failed\n");
  548. ret = PTR_ERR(root);
  549. goto free_sbi;
  550. }
  551. sb->s_root = d_alloc_root(root);
  552. if (!sb->s_root) {
  553. iput(root);
  554. EXOFS_ERR("ERROR: get root inode failed\n");
  555. ret = -ENOMEM;
  556. goto free_sbi;
  557. }
  558. if (!S_ISDIR(root->i_mode)) {
  559. dput(sb->s_root);
  560. sb->s_root = NULL;
  561. EXOFS_ERR("ERROR: corrupt root inode (mode = %hd)\n",
  562. root->i_mode);
  563. ret = -EINVAL;
  564. goto free_sbi;
  565. }
  566. _exofs_print_device("Mounting", opts->dev_name, sbi->layout.s_ods[0],
  567. sbi->layout.s_pid);
  568. return 0;
  569. free_sbi:
  570. bdi_destroy(&sbi->bdi);
  571. free_bdi:
  572. EXOFS_ERR("Unable to mount exofs on %s pid=0x%llx err=%d\n",
  573. opts->dev_name, sbi->layout.s_pid, ret);
  574. exofs_free_sbi(sbi);
  575. return ret;
  576. }
  577. /*
  578. * Set up the superblock (calls exofs_fill_super eventually)
  579. */
  580. static struct dentry *exofs_mount(struct file_system_type *type,
  581. int flags, const char *dev_name,
  582. void *data)
  583. {
  584. struct exofs_mountopt opts;
  585. int ret;
  586. ret = parse_options(data, &opts);
  587. if (ret)
  588. return ERR_PTR(ret);
  589. opts.dev_name = dev_name;
  590. return mount_nodev(type, flags, &opts, exofs_fill_super);
  591. }
  592. /*
  593. * Return information about the file system state in the buffer. This is used
  594. * by the 'df' command, for example.
  595. */
  596. static int exofs_statfs(struct dentry *dentry, struct kstatfs *buf)
  597. {
  598. struct super_block *sb = dentry->d_sb;
  599. struct exofs_sb_info *sbi = sb->s_fs_info;
  600. struct exofs_io_state *ios;
  601. struct osd_attr attrs[] = {
  602. ATTR_DEF(OSD_APAGE_PARTITION_QUOTAS,
  603. OSD_ATTR_PQ_CAPACITY_QUOTA, sizeof(__be64)),
  604. ATTR_DEF(OSD_APAGE_PARTITION_INFORMATION,
  605. OSD_ATTR_PI_USED_CAPACITY, sizeof(__be64)),
  606. };
  607. uint64_t capacity = ULLONG_MAX;
  608. uint64_t used = ULLONG_MAX;
  609. uint8_t cred_a[OSD_CAP_LEN];
  610. int ret;
  611. ret = exofs_get_io_state(&sbi->layout, &ios);
  612. if (ret) {
  613. EXOFS_DBGMSG("exofs_get_io_state failed.\n");
  614. return ret;
  615. }
  616. exofs_make_credential(cred_a, &ios->obj);
  617. ios->cred = sbi->s_cred;
  618. ios->in_attr = attrs;
  619. ios->in_attr_len = ARRAY_SIZE(attrs);
  620. ret = exofs_sbi_read(ios);
  621. if (unlikely(ret))
  622. goto out;
  623. ret = extract_attr_from_ios(ios, &attrs[0]);
  624. if (likely(!ret)) {
  625. capacity = get_unaligned_be64(attrs[0].val_ptr);
  626. if (unlikely(!capacity))
  627. capacity = ULLONG_MAX;
  628. } else
  629. EXOFS_DBGMSG("exofs_statfs: get capacity failed.\n");
  630. ret = extract_attr_from_ios(ios, &attrs[1]);
  631. if (likely(!ret))
  632. used = get_unaligned_be64(attrs[1].val_ptr);
  633. else
  634. EXOFS_DBGMSG("exofs_statfs: get used-space failed.\n");
  635. /* fill in the stats buffer */
  636. buf->f_type = EXOFS_SUPER_MAGIC;
  637. buf->f_bsize = EXOFS_BLKSIZE;
  638. buf->f_blocks = capacity >> 9;
  639. buf->f_bfree = (capacity - used) >> 9;
  640. buf->f_bavail = buf->f_bfree;
  641. buf->f_files = sbi->s_numfiles;
  642. buf->f_ffree = EXOFS_MAX_ID - sbi->s_numfiles;
  643. buf->f_namelen = EXOFS_NAME_LEN;
  644. out:
  645. exofs_put_io_state(ios);
  646. return ret;
  647. }
  648. static const struct super_operations exofs_sops = {
  649. .alloc_inode = exofs_alloc_inode,
  650. .destroy_inode = exofs_destroy_inode,
  651. .write_inode = exofs_write_inode,
  652. .evict_inode = exofs_evict_inode,
  653. .put_super = exofs_put_super,
  654. .write_super = exofs_write_super,
  655. .sync_fs = exofs_sync_fs,
  656. .statfs = exofs_statfs,
  657. };
  658. /******************************************************************************
  659. * EXPORT OPERATIONS
  660. *****************************************************************************/
  661. struct dentry *exofs_get_parent(struct dentry *child)
  662. {
  663. unsigned long ino = exofs_parent_ino(child);
  664. if (!ino)
  665. return NULL;
  666. return d_obtain_alias(exofs_iget(child->d_inode->i_sb, ino));
  667. }
  668. static struct inode *exofs_nfs_get_inode(struct super_block *sb,
  669. u64 ino, u32 generation)
  670. {
  671. struct inode *inode;
  672. inode = exofs_iget(sb, ino);
  673. if (IS_ERR(inode))
  674. return ERR_CAST(inode);
  675. if (generation && inode->i_generation != generation) {
  676. /* we didn't find the right inode.. */
  677. iput(inode);
  678. return ERR_PTR(-ESTALE);
  679. }
  680. return inode;
  681. }
  682. static struct dentry *exofs_fh_to_dentry(struct super_block *sb,
  683. struct fid *fid, int fh_len, int fh_type)
  684. {
  685. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  686. exofs_nfs_get_inode);
  687. }
  688. static struct dentry *exofs_fh_to_parent(struct super_block *sb,
  689. struct fid *fid, int fh_len, int fh_type)
  690. {
  691. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  692. exofs_nfs_get_inode);
  693. }
  694. static const struct export_operations exofs_export_ops = {
  695. .fh_to_dentry = exofs_fh_to_dentry,
  696. .fh_to_parent = exofs_fh_to_parent,
  697. .get_parent = exofs_get_parent,
  698. };
  699. /******************************************************************************
  700. * INSMOD/RMMOD
  701. *****************************************************************************/
  702. /*
  703. * struct that describes this file system
  704. */
  705. static struct file_system_type exofs_type = {
  706. .owner = THIS_MODULE,
  707. .name = "exofs",
  708. .mount = exofs_mount,
  709. .kill_sb = generic_shutdown_super,
  710. };
  711. static int __init init_exofs(void)
  712. {
  713. int err;
  714. err = init_inodecache();
  715. if (err)
  716. goto out;
  717. err = register_filesystem(&exofs_type);
  718. if (err)
  719. goto out_d;
  720. return 0;
  721. out_d:
  722. destroy_inodecache();
  723. out:
  724. return err;
  725. }
  726. static void __exit exit_exofs(void)
  727. {
  728. unregister_filesystem(&exofs_type);
  729. destroy_inodecache();
  730. }
  731. MODULE_AUTHOR("Avishay Traeger <avishay@gmail.com>");
  732. MODULE_DESCRIPTION("exofs");
  733. MODULE_LICENSE("GPL");
  734. module_init(init_exofs)
  735. module_exit(exit_exofs)