super.c 25 KB

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