super.c 23 KB

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