super.c 21 KB

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