super.c 21 KB

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