super.c 26 KB

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