super.c 24 KB

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