super.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  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 unsigned supported_features =
  441. CEPH_FEATURE_FLOCK |
  442. CEPH_FEATURE_DIRLAYOUTHASH;
  443. const unsigned 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. .sync_fs = ceph_sync_fs,
  600. .put_super = ceph_put_super,
  601. .show_options = ceph_show_options,
  602. .statfs = ceph_statfs,
  603. .umount_begin = ceph_umount_begin,
  604. };
  605. /*
  606. * Bootstrap mount by opening the root directory. Note the mount
  607. * @started time from caller, and time out if this takes too long.
  608. */
  609. static struct dentry *open_root_dentry(struct ceph_fs_client *fsc,
  610. const char *path,
  611. unsigned long started)
  612. {
  613. struct ceph_mds_client *mdsc = fsc->mdsc;
  614. struct ceph_mds_request *req = NULL;
  615. int err;
  616. struct dentry *root;
  617. /* open dir */
  618. dout("open_root_inode opening '%s'\n", path);
  619. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS);
  620. if (IS_ERR(req))
  621. return ERR_CAST(req);
  622. req->r_path1 = kstrdup(path, GFP_NOFS);
  623. req->r_ino1.ino = CEPH_INO_ROOT;
  624. req->r_ino1.snap = CEPH_NOSNAP;
  625. req->r_started = started;
  626. req->r_timeout = fsc->client->options->mount_timeout * HZ;
  627. req->r_args.getattr.mask = cpu_to_le32(CEPH_STAT_CAP_INODE);
  628. req->r_num_caps = 2;
  629. err = ceph_mdsc_do_request(mdsc, NULL, req);
  630. if (err == 0) {
  631. struct inode *inode = req->r_target_inode;
  632. req->r_target_inode = NULL;
  633. dout("open_root_inode success\n");
  634. if (ceph_ino(inode) == CEPH_INO_ROOT &&
  635. fsc->sb->s_root == NULL) {
  636. root = d_make_root(inode);
  637. if (!root) {
  638. root = ERR_PTR(-ENOMEM);
  639. goto out;
  640. }
  641. } else {
  642. root = d_obtain_alias(inode);
  643. }
  644. ceph_init_dentry(root);
  645. dout("open_root_inode success, root dentry is %p\n", root);
  646. } else {
  647. root = ERR_PTR(err);
  648. }
  649. out:
  650. ceph_mdsc_put_request(req);
  651. return root;
  652. }
  653. /*
  654. * mount: join the ceph cluster, and open root directory.
  655. */
  656. static struct dentry *ceph_real_mount(struct ceph_fs_client *fsc,
  657. const char *path)
  658. {
  659. int err;
  660. unsigned long started = jiffies; /* note the start time */
  661. struct dentry *root;
  662. int first = 0; /* first vfsmount for this super_block */
  663. dout("mount start\n");
  664. mutex_lock(&fsc->client->mount_mutex);
  665. err = __ceph_open_session(fsc->client, started);
  666. if (err < 0)
  667. goto out;
  668. dout("mount opening root\n");
  669. root = open_root_dentry(fsc, "", started);
  670. if (IS_ERR(root)) {
  671. err = PTR_ERR(root);
  672. goto out;
  673. }
  674. if (fsc->sb->s_root) {
  675. dput(root);
  676. } else {
  677. fsc->sb->s_root = root;
  678. first = 1;
  679. err = ceph_fs_debugfs_init(fsc);
  680. if (err < 0)
  681. goto fail;
  682. }
  683. if (path[0] == 0) {
  684. dget(root);
  685. } else {
  686. dout("mount opening base mountpoint\n");
  687. root = open_root_dentry(fsc, path, started);
  688. if (IS_ERR(root)) {
  689. err = PTR_ERR(root);
  690. goto fail;
  691. }
  692. }
  693. fsc->mount_state = CEPH_MOUNT_MOUNTED;
  694. dout("mount success\n");
  695. mutex_unlock(&fsc->client->mount_mutex);
  696. return root;
  697. out:
  698. mutex_unlock(&fsc->client->mount_mutex);
  699. return ERR_PTR(err);
  700. fail:
  701. if (first) {
  702. dput(fsc->sb->s_root);
  703. fsc->sb->s_root = NULL;
  704. }
  705. goto out;
  706. }
  707. static int ceph_set_super(struct super_block *s, void *data)
  708. {
  709. struct ceph_fs_client *fsc = data;
  710. int ret;
  711. dout("set_super %p data %p\n", s, data);
  712. s->s_flags = fsc->mount_options->sb_flags;
  713. s->s_maxbytes = 1ULL << 40; /* temp value until we get mdsmap */
  714. s->s_fs_info = fsc;
  715. fsc->sb = s;
  716. s->s_op = &ceph_super_ops;
  717. s->s_export_op = &ceph_export_ops;
  718. s->s_time_gran = 1000; /* 1000 ns == 1 us */
  719. ret = set_anon_super(s, NULL); /* what is that second arg for? */
  720. if (ret != 0)
  721. goto fail;
  722. return ret;
  723. fail:
  724. s->s_fs_info = NULL;
  725. fsc->sb = NULL;
  726. return ret;
  727. }
  728. /*
  729. * share superblock if same fs AND options
  730. */
  731. static int ceph_compare_super(struct super_block *sb, void *data)
  732. {
  733. struct ceph_fs_client *new = data;
  734. struct ceph_mount_options *fsopt = new->mount_options;
  735. struct ceph_options *opt = new->client->options;
  736. struct ceph_fs_client *other = ceph_sb_to_client(sb);
  737. dout("ceph_compare_super %p\n", sb);
  738. if (compare_mount_options(fsopt, opt, other)) {
  739. dout("monitor(s)/mount options don't match\n");
  740. return 0;
  741. }
  742. if ((opt->flags & CEPH_OPT_FSID) &&
  743. ceph_fsid_compare(&opt->fsid, &other->client->fsid)) {
  744. dout("fsid doesn't match\n");
  745. return 0;
  746. }
  747. if (fsopt->sb_flags != other->mount_options->sb_flags) {
  748. dout("flags differ\n");
  749. return 0;
  750. }
  751. return 1;
  752. }
  753. /*
  754. * construct our own bdi so we can control readahead, etc.
  755. */
  756. static atomic_long_t bdi_seq = ATOMIC_LONG_INIT(0);
  757. static int ceph_register_bdi(struct super_block *sb,
  758. struct ceph_fs_client *fsc)
  759. {
  760. int err;
  761. /* set ra_pages based on rasize mount option? */
  762. if (fsc->mount_options->rasize >= PAGE_CACHE_SIZE)
  763. fsc->backing_dev_info.ra_pages =
  764. (fsc->mount_options->rasize + PAGE_CACHE_SIZE - 1)
  765. >> PAGE_SHIFT;
  766. else
  767. fsc->backing_dev_info.ra_pages =
  768. default_backing_dev_info.ra_pages;
  769. err = bdi_register(&fsc->backing_dev_info, NULL, "ceph-%ld",
  770. atomic_long_inc_return(&bdi_seq));
  771. if (!err)
  772. sb->s_bdi = &fsc->backing_dev_info;
  773. return err;
  774. }
  775. static struct dentry *ceph_mount(struct file_system_type *fs_type,
  776. int flags, const char *dev_name, void *data)
  777. {
  778. struct super_block *sb;
  779. struct ceph_fs_client *fsc;
  780. struct dentry *res;
  781. int err;
  782. int (*compare_super)(struct super_block *, void *) = ceph_compare_super;
  783. const char *path = NULL;
  784. struct ceph_mount_options *fsopt = NULL;
  785. struct ceph_options *opt = NULL;
  786. dout("ceph_mount\n");
  787. err = parse_mount_options(&fsopt, &opt, flags, data, dev_name, &path);
  788. if (err < 0) {
  789. res = ERR_PTR(err);
  790. goto out_final;
  791. }
  792. /* create client (which we may/may not use) */
  793. fsc = create_fs_client(fsopt, opt);
  794. if (IS_ERR(fsc)) {
  795. res = ERR_CAST(fsc);
  796. destroy_mount_options(fsopt);
  797. ceph_destroy_options(opt);
  798. goto out_final;
  799. }
  800. err = ceph_mdsc_init(fsc);
  801. if (err < 0) {
  802. res = ERR_PTR(err);
  803. goto out;
  804. }
  805. if (ceph_test_opt(fsc->client, NOSHARE))
  806. compare_super = NULL;
  807. sb = sget(fs_type, compare_super, ceph_set_super, flags, fsc);
  808. if (IS_ERR(sb)) {
  809. res = ERR_CAST(sb);
  810. goto out;
  811. }
  812. if (ceph_sb_to_client(sb) != fsc) {
  813. ceph_mdsc_destroy(fsc);
  814. destroy_fs_client(fsc);
  815. fsc = ceph_sb_to_client(sb);
  816. dout("get_sb got existing client %p\n", fsc);
  817. } else {
  818. dout("get_sb using new client %p\n", fsc);
  819. err = ceph_register_bdi(sb, fsc);
  820. if (err < 0) {
  821. res = ERR_PTR(err);
  822. goto out_splat;
  823. }
  824. }
  825. res = ceph_real_mount(fsc, path);
  826. if (IS_ERR(res))
  827. goto out_splat;
  828. dout("root %p inode %p ino %llx.%llx\n", res,
  829. res->d_inode, ceph_vinop(res->d_inode));
  830. return res;
  831. out_splat:
  832. ceph_mdsc_close_sessions(fsc->mdsc);
  833. deactivate_locked_super(sb);
  834. goto out_final;
  835. out:
  836. ceph_mdsc_destroy(fsc);
  837. destroy_fs_client(fsc);
  838. out_final:
  839. dout("ceph_mount fail %ld\n", PTR_ERR(res));
  840. return res;
  841. }
  842. static void ceph_kill_sb(struct super_block *s)
  843. {
  844. struct ceph_fs_client *fsc = ceph_sb_to_client(s);
  845. dout("kill_sb %p\n", s);
  846. ceph_mdsc_pre_umount(fsc->mdsc);
  847. kill_anon_super(s); /* will call put_super after sb is r/o */
  848. ceph_mdsc_destroy(fsc);
  849. destroy_fs_client(fsc);
  850. }
  851. static struct file_system_type ceph_fs_type = {
  852. .owner = THIS_MODULE,
  853. .name = "ceph",
  854. .mount = ceph_mount,
  855. .kill_sb = ceph_kill_sb,
  856. .fs_flags = FS_RENAME_DOES_D_MOVE,
  857. };
  858. MODULE_ALIAS_FS("ceph");
  859. #define _STRINGIFY(x) #x
  860. #define STRINGIFY(x) _STRINGIFY(x)
  861. static int __init init_ceph(void)
  862. {
  863. int ret = init_caches();
  864. if (ret)
  865. goto out;
  866. ceph_xattr_init();
  867. ret = register_filesystem(&ceph_fs_type);
  868. if (ret)
  869. goto out_icache;
  870. pr_info("loaded (mds proto %d)\n", CEPH_MDSC_PROTOCOL);
  871. return 0;
  872. out_icache:
  873. ceph_xattr_exit();
  874. destroy_caches();
  875. out:
  876. return ret;
  877. }
  878. static void __exit exit_ceph(void)
  879. {
  880. dout("exit_ceph\n");
  881. unregister_filesystem(&ceph_fs_type);
  882. ceph_xattr_exit();
  883. destroy_caches();
  884. }
  885. module_init(init_ceph);
  886. module_exit(exit_ceph);
  887. MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
  888. MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
  889. MODULE_AUTHOR("Patience Warnick <patience@newdream.net>");
  890. MODULE_DESCRIPTION("Ceph filesystem for Linux");
  891. MODULE_LICENSE("GPL");