super.c 25 KB

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