super.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  1. #include <linux/ceph/ceph_debug.h>
  2. #include <linux/backing-dev.h>
  3. #include <linux/ctype.h>
  4. #include <linux/fs.h>
  5. #include <linux/inet.h>
  6. #include <linux/in6.h>
  7. #include <linux/module.h>
  8. #include <linux/mount.h>
  9. #include <linux/parser.h>
  10. #include <linux/sched.h>
  11. #include <linux/seq_file.h>
  12. #include <linux/slab.h>
  13. #include <linux/statfs.h>
  14. #include <linux/string.h>
  15. #include "super.h"
  16. #include "mds_client.h"
  17. #include <linux/ceph/decode.h>
  18. #include <linux/ceph/mon_client.h>
  19. #include <linux/ceph/auth.h>
  20. #include <linux/ceph/debugfs.h>
  21. /*
  22. * Ceph superblock operations
  23. *
  24. * Handle the basics of mounting, unmounting.
  25. */
  26. /*
  27. * super ops
  28. */
  29. static void ceph_put_super(struct super_block *s)
  30. {
  31. struct ceph_fs_client *fsc = ceph_sb_to_client(s);
  32. dout("put_super\n");
  33. ceph_mdsc_close_sessions(fsc->mdsc);
  34. /*
  35. * ensure we release the bdi before put_anon_super releases
  36. * the device name.
  37. */
  38. if (s->s_bdi == &fsc->backing_dev_info) {
  39. bdi_unregister(&fsc->backing_dev_info);
  40. s->s_bdi = NULL;
  41. }
  42. return;
  43. }
  44. static int ceph_statfs(struct dentry *dentry, struct kstatfs *buf)
  45. {
  46. struct ceph_fs_client *fsc = ceph_inode_to_client(dentry->d_inode);
  47. struct ceph_monmap *monmap = fsc->client->monc.monmap;
  48. struct ceph_statfs st;
  49. u64 fsid;
  50. int err;
  51. dout("statfs\n");
  52. err = ceph_monc_do_statfs(&fsc->client->monc, &st);
  53. if (err < 0)
  54. return err;
  55. /* fill in kstatfs */
  56. buf->f_type = CEPH_SUPER_MAGIC; /* ?? */
  57. /*
  58. * express utilization in terms of large blocks to avoid
  59. * overflow on 32-bit machines.
  60. */
  61. buf->f_bsize = 1 << CEPH_BLOCK_SHIFT;
  62. buf->f_blocks = le64_to_cpu(st.kb) >> (CEPH_BLOCK_SHIFT-10);
  63. buf->f_bfree = le64_to_cpu(st.kb_avail) >> (CEPH_BLOCK_SHIFT-10);
  64. buf->f_bavail = le64_to_cpu(st.kb_avail) >> (CEPH_BLOCK_SHIFT-10);
  65. buf->f_files = le64_to_cpu(st.num_objects);
  66. buf->f_ffree = -1;
  67. buf->f_namelen = NAME_MAX;
  68. buf->f_frsize = PAGE_CACHE_SIZE;
  69. /* leave fsid little-endian, regardless of host endianness */
  70. fsid = *(u64 *)(&monmap->fsid) ^ *((u64 *)&monmap->fsid + 1);
  71. buf->f_fsid.val[0] = fsid & 0xffffffff;
  72. buf->f_fsid.val[1] = fsid >> 32;
  73. return 0;
  74. }
  75. static int ceph_sync_fs(struct super_block *sb, int wait)
  76. {
  77. struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
  78. if (!wait) {
  79. dout("sync_fs (non-blocking)\n");
  80. ceph_flush_dirty_caps(fsc->mdsc);
  81. dout("sync_fs (non-blocking) done\n");
  82. return 0;
  83. }
  84. dout("sync_fs (blocking)\n");
  85. ceph_osdc_sync(&fsc->client->osdc);
  86. ceph_mdsc_sync(fsc->mdsc);
  87. dout("sync_fs (blocking) done\n");
  88. return 0;
  89. }
  90. /*
  91. * mount options
  92. */
  93. enum {
  94. Opt_wsize,
  95. Opt_rsize,
  96. Opt_rasize,
  97. Opt_caps_wanted_delay_min,
  98. Opt_caps_wanted_delay_max,
  99. Opt_cap_release_safety,
  100. Opt_readdir_max_entries,
  101. Opt_readdir_max_bytes,
  102. Opt_congestion_kb,
  103. Opt_last_int,
  104. /* int args above */
  105. Opt_snapdirname,
  106. Opt_last_string,
  107. /* string args above */
  108. Opt_dirstat,
  109. Opt_nodirstat,
  110. Opt_rbytes,
  111. Opt_norbytes,
  112. Opt_noasyncreaddir,
  113. Opt_ino32,
  114. };
  115. static match_table_t fsopt_tokens = {
  116. {Opt_wsize, "wsize=%d"},
  117. {Opt_rsize, "rsize=%d"},
  118. {Opt_rasize, "rasize=%d"},
  119. {Opt_caps_wanted_delay_min, "caps_wanted_delay_min=%d"},
  120. {Opt_caps_wanted_delay_max, "caps_wanted_delay_max=%d"},
  121. {Opt_cap_release_safety, "cap_release_safety=%d"},
  122. {Opt_readdir_max_entries, "readdir_max_entries=%d"},
  123. {Opt_readdir_max_bytes, "readdir_max_bytes=%d"},
  124. {Opt_congestion_kb, "write_congestion_kb=%d"},
  125. /* int args above */
  126. {Opt_snapdirname, "snapdirname=%s"},
  127. /* string args above */
  128. {Opt_dirstat, "dirstat"},
  129. {Opt_nodirstat, "nodirstat"},
  130. {Opt_rbytes, "rbytes"},
  131. {Opt_norbytes, "norbytes"},
  132. {Opt_noasyncreaddir, "noasyncreaddir"},
  133. {Opt_ino32, "ino32"},
  134. {-1, NULL}
  135. };
  136. static int parse_fsopt_token(char *c, void *private)
  137. {
  138. struct ceph_mount_options *fsopt = private;
  139. substring_t argstr[MAX_OPT_ARGS];
  140. int token, intval, ret;
  141. token = match_token((char *)c, fsopt_tokens, argstr);
  142. if (token < 0)
  143. return -EINVAL;
  144. if (token < Opt_last_int) {
  145. ret = match_int(&argstr[0], &intval);
  146. if (ret < 0) {
  147. pr_err("bad mount option arg (not int) "
  148. "at '%s'\n", c);
  149. return ret;
  150. }
  151. dout("got int token %d val %d\n", token, intval);
  152. } else if (token > Opt_last_int && token < Opt_last_string) {
  153. dout("got string token %d val %s\n", token,
  154. argstr[0].from);
  155. } else {
  156. dout("got token %d\n", token);
  157. }
  158. switch (token) {
  159. case Opt_snapdirname:
  160. kfree(fsopt->snapdir_name);
  161. fsopt->snapdir_name = kstrndup(argstr[0].from,
  162. argstr[0].to-argstr[0].from,
  163. GFP_KERNEL);
  164. if (!fsopt->snapdir_name)
  165. return -ENOMEM;
  166. break;
  167. /* misc */
  168. case Opt_wsize:
  169. fsopt->wsize = intval;
  170. break;
  171. case Opt_rsize:
  172. fsopt->rsize = intval;
  173. break;
  174. case Opt_rasize:
  175. fsopt->rasize = intval;
  176. break;
  177. case Opt_caps_wanted_delay_min:
  178. fsopt->caps_wanted_delay_min = intval;
  179. break;
  180. case Opt_caps_wanted_delay_max:
  181. fsopt->caps_wanted_delay_max = intval;
  182. break;
  183. case Opt_readdir_max_entries:
  184. fsopt->max_readdir = intval;
  185. break;
  186. case Opt_readdir_max_bytes:
  187. fsopt->max_readdir_bytes = intval;
  188. break;
  189. case Opt_congestion_kb:
  190. fsopt->congestion_kb = intval;
  191. break;
  192. case Opt_dirstat:
  193. fsopt->flags |= CEPH_MOUNT_OPT_DIRSTAT;
  194. break;
  195. case Opt_nodirstat:
  196. fsopt->flags &= ~CEPH_MOUNT_OPT_DIRSTAT;
  197. break;
  198. case Opt_rbytes:
  199. fsopt->flags |= CEPH_MOUNT_OPT_RBYTES;
  200. break;
  201. case Opt_norbytes:
  202. fsopt->flags &= ~CEPH_MOUNT_OPT_RBYTES;
  203. break;
  204. case Opt_noasyncreaddir:
  205. fsopt->flags |= CEPH_MOUNT_OPT_NOASYNCREADDIR;
  206. break;
  207. case Opt_ino32:
  208. fsopt->flags |= CEPH_MOUNT_OPT_INO32;
  209. break;
  210. default:
  211. BUG_ON(token);
  212. }
  213. return 0;
  214. }
  215. static void destroy_mount_options(struct ceph_mount_options *args)
  216. {
  217. dout("destroy_mount_options %p\n", args);
  218. kfree(args->snapdir_name);
  219. kfree(args);
  220. }
  221. static int strcmp_null(const char *s1, const char *s2)
  222. {
  223. if (!s1 && !s2)
  224. return 0;
  225. if (s1 && !s2)
  226. return -1;
  227. if (!s1 && s2)
  228. return 1;
  229. return strcmp(s1, s2);
  230. }
  231. static int compare_mount_options(struct ceph_mount_options *new_fsopt,
  232. struct ceph_options *new_opt,
  233. struct ceph_fs_client *fsc)
  234. {
  235. struct ceph_mount_options *fsopt1 = new_fsopt;
  236. struct ceph_mount_options *fsopt2 = fsc->mount_options;
  237. int ofs = offsetof(struct ceph_mount_options, snapdir_name);
  238. int ret;
  239. ret = memcmp(fsopt1, fsopt2, ofs);
  240. if (ret)
  241. return ret;
  242. ret = strcmp_null(fsopt1->snapdir_name, fsopt2->snapdir_name);
  243. if (ret)
  244. return ret;
  245. return ceph_compare_options(new_opt, fsc->client);
  246. }
  247. static int parse_mount_options(struct ceph_mount_options **pfsopt,
  248. struct ceph_options **popt,
  249. int flags, char *options,
  250. const char *dev_name,
  251. const char **path)
  252. {
  253. struct ceph_mount_options *fsopt;
  254. const char *dev_name_end;
  255. int err = -ENOMEM;
  256. fsopt = kzalloc(sizeof(*fsopt), GFP_KERNEL);
  257. if (!fsopt)
  258. return -ENOMEM;
  259. dout("parse_mount_options %p, dev_name '%s'\n", fsopt, dev_name);
  260. fsopt->sb_flags = flags;
  261. fsopt->flags = CEPH_MOUNT_OPT_DEFAULT;
  262. fsopt->rsize = CEPH_RSIZE_DEFAULT;
  263. fsopt->rasize = CEPH_RASIZE_DEFAULT;
  264. fsopt->snapdir_name = kstrdup(CEPH_SNAPDIRNAME_DEFAULT, GFP_KERNEL);
  265. fsopt->caps_wanted_delay_min = CEPH_CAPS_WANTED_DELAY_MIN_DEFAULT;
  266. fsopt->caps_wanted_delay_max = CEPH_CAPS_WANTED_DELAY_MAX_DEFAULT;
  267. fsopt->cap_release_safety = CEPH_CAP_RELEASE_SAFETY_DEFAULT;
  268. fsopt->max_readdir = CEPH_MAX_READDIR_DEFAULT;
  269. fsopt->max_readdir_bytes = CEPH_MAX_READDIR_BYTES_DEFAULT;
  270. fsopt->congestion_kb = default_congestion_kb();
  271. /* ip1[:port1][,ip2[:port2]...]:/subdir/in/fs */
  272. err = -EINVAL;
  273. if (!dev_name)
  274. goto out;
  275. *path = strstr(dev_name, ":/");
  276. if (*path == NULL) {
  277. pr_err("device name is missing path (no :/ in %s)\n",
  278. dev_name);
  279. goto out;
  280. }
  281. dev_name_end = *path;
  282. dout("device name '%.*s'\n", (int)(dev_name_end - dev_name), dev_name);
  283. /* path on server */
  284. *path += 2;
  285. dout("server path '%s'\n", *path);
  286. err = ceph_parse_options(popt, options, dev_name, dev_name_end,
  287. parse_fsopt_token, (void *)fsopt);
  288. if (err)
  289. goto out;
  290. /* success */
  291. *pfsopt = fsopt;
  292. return 0;
  293. out:
  294. destroy_mount_options(fsopt);
  295. return err;
  296. }
  297. /**
  298. * ceph_show_options - Show mount options in /proc/mounts
  299. * @m: seq_file to write to
  300. * @mnt: mount descriptor
  301. */
  302. static int ceph_show_options(struct seq_file *m, struct vfsmount *mnt)
  303. {
  304. struct ceph_fs_client *fsc = ceph_sb_to_client(mnt->mnt_sb);
  305. struct ceph_mount_options *fsopt = fsc->mount_options;
  306. struct ceph_options *opt = fsc->client->options;
  307. if (opt->flags & CEPH_OPT_FSID)
  308. seq_printf(m, ",fsid=%pU", &opt->fsid);
  309. if (opt->flags & CEPH_OPT_NOSHARE)
  310. seq_puts(m, ",noshare");
  311. if (opt->flags & CEPH_OPT_NOCRC)
  312. seq_puts(m, ",nocrc");
  313. if (opt->name)
  314. seq_printf(m, ",name=%s", opt->name);
  315. if (opt->key)
  316. seq_puts(m, ",secret=<hidden>");
  317. if (opt->mount_timeout != CEPH_MOUNT_TIMEOUT_DEFAULT)
  318. seq_printf(m, ",mount_timeout=%d", opt->mount_timeout);
  319. if (opt->osd_idle_ttl != CEPH_OSD_IDLE_TTL_DEFAULT)
  320. seq_printf(m, ",osd_idle_ttl=%d", opt->osd_idle_ttl);
  321. if (opt->osd_timeout != CEPH_OSD_TIMEOUT_DEFAULT)
  322. seq_printf(m, ",osdtimeout=%d", opt->osd_timeout);
  323. if (opt->osd_keepalive_timeout != CEPH_OSD_KEEPALIVE_DEFAULT)
  324. seq_printf(m, ",osdkeepalivetimeout=%d",
  325. opt->osd_keepalive_timeout);
  326. if (fsopt->flags & CEPH_MOUNT_OPT_DIRSTAT)
  327. seq_puts(m, ",dirstat");
  328. if ((fsopt->flags & CEPH_MOUNT_OPT_RBYTES) == 0)
  329. seq_puts(m, ",norbytes");
  330. if (fsopt->flags & CEPH_MOUNT_OPT_NOASYNCREADDIR)
  331. seq_puts(m, ",noasyncreaddir");
  332. if (fsopt->wsize)
  333. seq_printf(m, ",wsize=%d", fsopt->wsize);
  334. if (fsopt->rsize != CEPH_RSIZE_DEFAULT)
  335. seq_printf(m, ",rsize=%d", fsopt->rsize);
  336. if (fsopt->rasize != CEPH_RASIZE_DEFAULT)
  337. seq_printf(m, ",rasize=%d", fsopt->rsize);
  338. if (fsopt->congestion_kb != default_congestion_kb())
  339. seq_printf(m, ",write_congestion_kb=%d", fsopt->congestion_kb);
  340. if (fsopt->caps_wanted_delay_min != CEPH_CAPS_WANTED_DELAY_MIN_DEFAULT)
  341. seq_printf(m, ",caps_wanted_delay_min=%d",
  342. fsopt->caps_wanted_delay_min);
  343. if (fsopt->caps_wanted_delay_max != CEPH_CAPS_WANTED_DELAY_MAX_DEFAULT)
  344. seq_printf(m, ",caps_wanted_delay_max=%d",
  345. fsopt->caps_wanted_delay_max);
  346. if (fsopt->cap_release_safety != CEPH_CAP_RELEASE_SAFETY_DEFAULT)
  347. seq_printf(m, ",cap_release_safety=%d",
  348. fsopt->cap_release_safety);
  349. if (fsopt->max_readdir != CEPH_MAX_READDIR_DEFAULT)
  350. seq_printf(m, ",readdir_max_entries=%d", fsopt->max_readdir);
  351. if (fsopt->max_readdir_bytes != CEPH_MAX_READDIR_BYTES_DEFAULT)
  352. seq_printf(m, ",readdir_max_bytes=%d", fsopt->max_readdir_bytes);
  353. if (strcmp(fsopt->snapdir_name, CEPH_SNAPDIRNAME_DEFAULT))
  354. seq_printf(m, ",snapdirname=%s", fsopt->snapdir_name);
  355. return 0;
  356. }
  357. /*
  358. * handle any mon messages the standard library doesn't understand.
  359. * return error if we don't either.
  360. */
  361. static int extra_mon_dispatch(struct ceph_client *client, struct ceph_msg *msg)
  362. {
  363. struct ceph_fs_client *fsc = client->private;
  364. int type = le16_to_cpu(msg->hdr.type);
  365. switch (type) {
  366. case CEPH_MSG_MDS_MAP:
  367. ceph_mdsc_handle_map(fsc->mdsc, msg);
  368. return 0;
  369. default:
  370. return -1;
  371. }
  372. }
  373. /*
  374. * create a new fs client
  375. */
  376. static struct ceph_fs_client *create_fs_client(struct ceph_mount_options *fsopt,
  377. struct ceph_options *opt)
  378. {
  379. struct ceph_fs_client *fsc;
  380. const unsigned supported_features =
  381. CEPH_FEATURE_FLOCK |
  382. CEPH_FEATURE_DIRLAYOUTHASH;
  383. const unsigned required_features = 0;
  384. int err = -ENOMEM;
  385. fsc = kzalloc(sizeof(*fsc), GFP_KERNEL);
  386. if (!fsc)
  387. return ERR_PTR(-ENOMEM);
  388. fsc->client = ceph_create_client(opt, fsc, supported_features,
  389. required_features);
  390. if (IS_ERR(fsc->client)) {
  391. err = PTR_ERR(fsc->client);
  392. goto fail;
  393. }
  394. fsc->client->extra_mon_dispatch = extra_mon_dispatch;
  395. fsc->client->monc.want_mdsmap = 1;
  396. fsc->mount_options = fsopt;
  397. fsc->sb = NULL;
  398. fsc->mount_state = CEPH_MOUNT_MOUNTING;
  399. atomic_long_set(&fsc->writeback_count, 0);
  400. err = bdi_init(&fsc->backing_dev_info);
  401. if (err < 0)
  402. goto fail_client;
  403. err = -ENOMEM;
  404. /*
  405. * The number of concurrent works can be high but they don't need
  406. * to be processed in parallel, limit concurrency.
  407. */
  408. fsc->wb_wq = alloc_workqueue("ceph-writeback", 0, 1);
  409. if (fsc->wb_wq == NULL)
  410. goto fail_bdi;
  411. fsc->pg_inv_wq = alloc_workqueue("ceph-pg-invalid", 0, 1);
  412. if (fsc->pg_inv_wq == NULL)
  413. goto fail_wb_wq;
  414. fsc->trunc_wq = alloc_workqueue("ceph-trunc", 0, 1);
  415. if (fsc->trunc_wq == NULL)
  416. goto fail_pg_inv_wq;
  417. /* set up mempools */
  418. err = -ENOMEM;
  419. fsc->wb_pagevec_pool = mempool_create_kmalloc_pool(10,
  420. fsc->mount_options->wsize >> PAGE_CACHE_SHIFT);
  421. if (!fsc->wb_pagevec_pool)
  422. goto fail_trunc_wq;
  423. /* caps */
  424. fsc->min_caps = fsopt->max_readdir;
  425. return fsc;
  426. fail_trunc_wq:
  427. destroy_workqueue(fsc->trunc_wq);
  428. fail_pg_inv_wq:
  429. destroy_workqueue(fsc->pg_inv_wq);
  430. fail_wb_wq:
  431. destroy_workqueue(fsc->wb_wq);
  432. fail_bdi:
  433. bdi_destroy(&fsc->backing_dev_info);
  434. fail_client:
  435. ceph_destroy_client(fsc->client);
  436. fail:
  437. kfree(fsc);
  438. return ERR_PTR(err);
  439. }
  440. static void destroy_fs_client(struct ceph_fs_client *fsc)
  441. {
  442. dout("destroy_fs_client %p\n", fsc);
  443. destroy_workqueue(fsc->wb_wq);
  444. destroy_workqueue(fsc->pg_inv_wq);
  445. destroy_workqueue(fsc->trunc_wq);
  446. bdi_destroy(&fsc->backing_dev_info);
  447. mempool_destroy(fsc->wb_pagevec_pool);
  448. destroy_mount_options(fsc->mount_options);
  449. ceph_fs_debugfs_cleanup(fsc);
  450. ceph_destroy_client(fsc->client);
  451. kfree(fsc);
  452. dout("destroy_fs_client %p done\n", fsc);
  453. }
  454. /*
  455. * caches
  456. */
  457. struct kmem_cache *ceph_inode_cachep;
  458. struct kmem_cache *ceph_cap_cachep;
  459. struct kmem_cache *ceph_dentry_cachep;
  460. struct kmem_cache *ceph_file_cachep;
  461. static void ceph_inode_init_once(void *foo)
  462. {
  463. struct ceph_inode_info *ci = foo;
  464. inode_init_once(&ci->vfs_inode);
  465. }
  466. static int __init init_caches(void)
  467. {
  468. ceph_inode_cachep = kmem_cache_create("ceph_inode_info",
  469. sizeof(struct ceph_inode_info),
  470. __alignof__(struct ceph_inode_info),
  471. (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD),
  472. ceph_inode_init_once);
  473. if (ceph_inode_cachep == NULL)
  474. return -ENOMEM;
  475. ceph_cap_cachep = KMEM_CACHE(ceph_cap,
  476. SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD);
  477. if (ceph_cap_cachep == NULL)
  478. goto bad_cap;
  479. ceph_dentry_cachep = KMEM_CACHE(ceph_dentry_info,
  480. SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD);
  481. if (ceph_dentry_cachep == NULL)
  482. goto bad_dentry;
  483. ceph_file_cachep = KMEM_CACHE(ceph_file_info,
  484. SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD);
  485. if (ceph_file_cachep == NULL)
  486. goto bad_file;
  487. return 0;
  488. bad_file:
  489. kmem_cache_destroy(ceph_dentry_cachep);
  490. bad_dentry:
  491. kmem_cache_destroy(ceph_cap_cachep);
  492. bad_cap:
  493. kmem_cache_destroy(ceph_inode_cachep);
  494. return -ENOMEM;
  495. }
  496. static void destroy_caches(void)
  497. {
  498. kmem_cache_destroy(ceph_inode_cachep);
  499. kmem_cache_destroy(ceph_cap_cachep);
  500. kmem_cache_destroy(ceph_dentry_cachep);
  501. kmem_cache_destroy(ceph_file_cachep);
  502. }
  503. /*
  504. * ceph_umount_begin - initiate forced umount. Tear down down the
  505. * mount, skipping steps that may hang while waiting for server(s).
  506. */
  507. static void ceph_umount_begin(struct super_block *sb)
  508. {
  509. struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
  510. dout("ceph_umount_begin - starting forced umount\n");
  511. if (!fsc)
  512. return;
  513. fsc->mount_state = CEPH_MOUNT_SHUTDOWN;
  514. return;
  515. }
  516. static const struct super_operations ceph_super_ops = {
  517. .alloc_inode = ceph_alloc_inode,
  518. .destroy_inode = ceph_destroy_inode,
  519. .write_inode = ceph_write_inode,
  520. .sync_fs = ceph_sync_fs,
  521. .put_super = ceph_put_super,
  522. .show_options = ceph_show_options,
  523. .statfs = ceph_statfs,
  524. .umount_begin = ceph_umount_begin,
  525. };
  526. /*
  527. * Bootstrap mount by opening the root directory. Note the mount
  528. * @started time from caller, and time out if this takes too long.
  529. */
  530. static struct dentry *open_root_dentry(struct ceph_fs_client *fsc,
  531. const char *path,
  532. unsigned long started)
  533. {
  534. struct ceph_mds_client *mdsc = fsc->mdsc;
  535. struct ceph_mds_request *req = NULL;
  536. int err;
  537. struct dentry *root;
  538. /* open dir */
  539. dout("open_root_inode opening '%s'\n", path);
  540. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS);
  541. if (IS_ERR(req))
  542. return ERR_CAST(req);
  543. req->r_path1 = kstrdup(path, GFP_NOFS);
  544. req->r_ino1.ino = CEPH_INO_ROOT;
  545. req->r_ino1.snap = CEPH_NOSNAP;
  546. req->r_started = started;
  547. req->r_timeout = fsc->client->options->mount_timeout * HZ;
  548. req->r_args.getattr.mask = cpu_to_le32(CEPH_STAT_CAP_INODE);
  549. req->r_num_caps = 2;
  550. err = ceph_mdsc_do_request(mdsc, NULL, req);
  551. if (err == 0) {
  552. dout("open_root_inode success\n");
  553. if (ceph_ino(req->r_target_inode) == CEPH_INO_ROOT &&
  554. fsc->sb->s_root == NULL)
  555. root = d_alloc_root(req->r_target_inode);
  556. else
  557. root = d_obtain_alias(req->r_target_inode);
  558. req->r_target_inode = NULL;
  559. dout("open_root_inode success, root dentry is %p\n", root);
  560. } else {
  561. root = ERR_PTR(err);
  562. }
  563. ceph_mdsc_put_request(req);
  564. return root;
  565. }
  566. /*
  567. * mount: join the ceph cluster, and open root directory.
  568. */
  569. static struct dentry *ceph_real_mount(struct ceph_fs_client *fsc,
  570. const char *path)
  571. {
  572. int err;
  573. unsigned long started = jiffies; /* note the start time */
  574. struct dentry *root;
  575. int first = 0; /* first vfsmount for this super_block */
  576. dout("mount start\n");
  577. mutex_lock(&fsc->client->mount_mutex);
  578. err = __ceph_open_session(fsc->client, started);
  579. if (err < 0)
  580. goto out;
  581. dout("mount opening root\n");
  582. root = open_root_dentry(fsc, "", started);
  583. if (IS_ERR(root)) {
  584. err = PTR_ERR(root);
  585. goto out;
  586. }
  587. if (fsc->sb->s_root) {
  588. dput(root);
  589. } else {
  590. fsc->sb->s_root = root;
  591. first = 1;
  592. err = ceph_fs_debugfs_init(fsc);
  593. if (err < 0)
  594. goto fail;
  595. }
  596. if (path[0] == 0) {
  597. dget(root);
  598. } else {
  599. dout("mount opening base mountpoint\n");
  600. root = open_root_dentry(fsc, path, started);
  601. if (IS_ERR(root)) {
  602. err = PTR_ERR(root);
  603. goto fail;
  604. }
  605. }
  606. fsc->mount_state = CEPH_MOUNT_MOUNTED;
  607. dout("mount success\n");
  608. mutex_unlock(&fsc->client->mount_mutex);
  609. return root;
  610. out:
  611. mutex_unlock(&fsc->client->mount_mutex);
  612. return ERR_PTR(err);
  613. fail:
  614. if (first) {
  615. dput(fsc->sb->s_root);
  616. fsc->sb->s_root = NULL;
  617. }
  618. goto out;
  619. }
  620. static int ceph_set_super(struct super_block *s, void *data)
  621. {
  622. struct ceph_fs_client *fsc = data;
  623. int ret;
  624. dout("set_super %p data %p\n", s, data);
  625. s->s_flags = fsc->mount_options->sb_flags;
  626. s->s_maxbytes = 1ULL << 40; /* temp value until we get mdsmap */
  627. s->s_fs_info = fsc;
  628. fsc->sb = s;
  629. s->s_op = &ceph_super_ops;
  630. s->s_export_op = &ceph_export_ops;
  631. s->s_time_gran = 1000; /* 1000 ns == 1 us */
  632. ret = set_anon_super(s, NULL); /* what is that second arg for? */
  633. if (ret != 0)
  634. goto fail;
  635. return ret;
  636. fail:
  637. s->s_fs_info = NULL;
  638. fsc->sb = NULL;
  639. return ret;
  640. }
  641. /*
  642. * share superblock if same fs AND options
  643. */
  644. static int ceph_compare_super(struct super_block *sb, void *data)
  645. {
  646. struct ceph_fs_client *new = data;
  647. struct ceph_mount_options *fsopt = new->mount_options;
  648. struct ceph_options *opt = new->client->options;
  649. struct ceph_fs_client *other = ceph_sb_to_client(sb);
  650. dout("ceph_compare_super %p\n", sb);
  651. if (compare_mount_options(fsopt, opt, other)) {
  652. dout("monitor(s)/mount options don't match\n");
  653. return 0;
  654. }
  655. if ((opt->flags & CEPH_OPT_FSID) &&
  656. ceph_fsid_compare(&opt->fsid, &other->client->fsid)) {
  657. dout("fsid doesn't match\n");
  658. return 0;
  659. }
  660. if (fsopt->sb_flags != other->mount_options->sb_flags) {
  661. dout("flags differ\n");
  662. return 0;
  663. }
  664. return 1;
  665. }
  666. /*
  667. * construct our own bdi so we can control readahead, etc.
  668. */
  669. static atomic_long_t bdi_seq = ATOMIC_LONG_INIT(0);
  670. static int ceph_register_bdi(struct super_block *sb,
  671. struct ceph_fs_client *fsc)
  672. {
  673. int err;
  674. /* set ra_pages based on rasize mount option? */
  675. if (fsc->mount_options->rasize >= PAGE_CACHE_SIZE)
  676. fsc->backing_dev_info.ra_pages =
  677. (fsc->mount_options->rasize + PAGE_CACHE_SIZE - 1)
  678. >> PAGE_SHIFT;
  679. else
  680. fsc->backing_dev_info.ra_pages =
  681. default_backing_dev_info.ra_pages;
  682. err = bdi_register(&fsc->backing_dev_info, NULL, "ceph-%d",
  683. atomic_long_inc_return(&bdi_seq));
  684. if (!err)
  685. sb->s_bdi = &fsc->backing_dev_info;
  686. return err;
  687. }
  688. static struct dentry *ceph_mount(struct file_system_type *fs_type,
  689. int flags, const char *dev_name, void *data)
  690. {
  691. struct super_block *sb;
  692. struct ceph_fs_client *fsc;
  693. struct dentry *res;
  694. int err;
  695. int (*compare_super)(struct super_block *, void *) = ceph_compare_super;
  696. const char *path = NULL;
  697. struct ceph_mount_options *fsopt = NULL;
  698. struct ceph_options *opt = NULL;
  699. dout("ceph_mount\n");
  700. err = parse_mount_options(&fsopt, &opt, flags, data, dev_name, &path);
  701. if (err < 0) {
  702. res = ERR_PTR(err);
  703. goto out_final;
  704. }
  705. /* create client (which we may/may not use) */
  706. fsc = create_fs_client(fsopt, opt);
  707. if (IS_ERR(fsc)) {
  708. res = ERR_CAST(fsc);
  709. destroy_mount_options(fsopt);
  710. ceph_destroy_options(opt);
  711. goto out_final;
  712. }
  713. err = ceph_mdsc_init(fsc);
  714. if (err < 0) {
  715. res = ERR_PTR(err);
  716. goto out;
  717. }
  718. if (ceph_test_opt(fsc->client, NOSHARE))
  719. compare_super = NULL;
  720. sb = sget(fs_type, compare_super, ceph_set_super, fsc);
  721. if (IS_ERR(sb)) {
  722. res = ERR_CAST(sb);
  723. goto out;
  724. }
  725. if (ceph_sb_to_client(sb) != fsc) {
  726. ceph_mdsc_destroy(fsc);
  727. destroy_fs_client(fsc);
  728. fsc = ceph_sb_to_client(sb);
  729. dout("get_sb got existing client %p\n", fsc);
  730. } else {
  731. dout("get_sb using new client %p\n", fsc);
  732. err = ceph_register_bdi(sb, fsc);
  733. if (err < 0) {
  734. res = ERR_PTR(err);
  735. goto out_splat;
  736. }
  737. }
  738. res = ceph_real_mount(fsc, path);
  739. if (IS_ERR(res))
  740. goto out_splat;
  741. dout("root %p inode %p ino %llx.%llx\n", res,
  742. res->d_inode, ceph_vinop(res->d_inode));
  743. return res;
  744. out_splat:
  745. ceph_mdsc_close_sessions(fsc->mdsc);
  746. deactivate_locked_super(sb);
  747. goto out_final;
  748. out:
  749. ceph_mdsc_destroy(fsc);
  750. destroy_fs_client(fsc);
  751. out_final:
  752. dout("ceph_mount fail %ld\n", PTR_ERR(res));
  753. return res;
  754. }
  755. static void ceph_kill_sb(struct super_block *s)
  756. {
  757. struct ceph_fs_client *fsc = ceph_sb_to_client(s);
  758. dout("kill_sb %p\n", s);
  759. ceph_mdsc_pre_umount(fsc->mdsc);
  760. kill_anon_super(s); /* will call put_super after sb is r/o */
  761. ceph_mdsc_destroy(fsc);
  762. destroy_fs_client(fsc);
  763. }
  764. static struct file_system_type ceph_fs_type = {
  765. .owner = THIS_MODULE,
  766. .name = "ceph",
  767. .mount = ceph_mount,
  768. .kill_sb = ceph_kill_sb,
  769. .fs_flags = FS_RENAME_DOES_D_MOVE,
  770. };
  771. #define _STRINGIFY(x) #x
  772. #define STRINGIFY(x) _STRINGIFY(x)
  773. static int __init init_ceph(void)
  774. {
  775. int ret = init_caches();
  776. if (ret)
  777. goto out;
  778. ret = register_filesystem(&ceph_fs_type);
  779. if (ret)
  780. goto out_icache;
  781. pr_info("loaded (mds proto %d)\n", CEPH_MDSC_PROTOCOL);
  782. return 0;
  783. out_icache:
  784. destroy_caches();
  785. out:
  786. return ret;
  787. }
  788. static void __exit exit_ceph(void)
  789. {
  790. dout("exit_ceph\n");
  791. unregister_filesystem(&ceph_fs_type);
  792. destroy_caches();
  793. }
  794. module_init(init_ceph);
  795. module_exit(exit_ceph);
  796. MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
  797. MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
  798. MODULE_AUTHOR("Patience Warnick <patience@newdream.net>");
  799. MODULE_DESCRIPTION("Ceph filesystem for Linux");
  800. MODULE_LICENSE("GPL");