super.c 22 KB

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