super.c 26 KB

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