super.c 26 KB

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