super.c 22 KB

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