super.c 24 KB

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