super.c 23 KB

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