super.c 23 KB

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