super.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  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. int first = 0; /* first vfsmount for this super_block */
  721. dout("mount start %p\n", fsc);
  722. mutex_lock(&fsc->client->mount_mutex);
  723. if (!fsc->sb->s_root) {
  724. const char *path;
  725. err = __ceph_open_session(fsc->client, started);
  726. if (err < 0)
  727. goto out;
  728. if (!fsc->mount_options->server_path) {
  729. path = "";
  730. dout("mount opening path \\t\n");
  731. } else {
  732. path = fsc->mount_options->server_path + 1;
  733. dout("mount opening path %s\n", path);
  734. }
  735. root = open_root_dentry(fsc, path, started);
  736. if (IS_ERR(root)) {
  737. err = PTR_ERR(root);
  738. goto out;
  739. }
  740. fsc->sb->s_root = dget(root);
  741. first = 1;
  742. err = ceph_fs_debugfs_init(fsc);
  743. if (err < 0)
  744. goto fail;
  745. } else {
  746. root = dget(fsc->sb->s_root);
  747. }
  748. fsc->mount_state = CEPH_MOUNT_MOUNTED;
  749. dout("mount success\n");
  750. mutex_unlock(&fsc->client->mount_mutex);
  751. return root;
  752. fail:
  753. if (first) {
  754. dput(fsc->sb->s_root);
  755. fsc->sb->s_root = NULL;
  756. }
  757. out:
  758. mutex_unlock(&fsc->client->mount_mutex);
  759. return ERR_PTR(err);
  760. }
  761. static int ceph_set_super(struct super_block *s, void *data)
  762. {
  763. struct ceph_fs_client *fsc = data;
  764. int ret;
  765. dout("set_super %p data %p\n", s, data);
  766. s->s_flags = fsc->mount_options->sb_flags;
  767. s->s_maxbytes = 1ULL << 40; /* temp value until we get mdsmap */
  768. s->s_xattr = ceph_xattr_handlers;
  769. s->s_fs_info = fsc;
  770. fsc->sb = s;
  771. s->s_op = &ceph_super_ops;
  772. s->s_export_op = &ceph_export_ops;
  773. s->s_time_gran = 1000; /* 1000 ns == 1 us */
  774. ret = set_anon_super(s, NULL); /* what is that second arg for? */
  775. if (ret != 0)
  776. goto fail;
  777. return ret;
  778. fail:
  779. s->s_fs_info = NULL;
  780. fsc->sb = NULL;
  781. return ret;
  782. }
  783. /*
  784. * share superblock if same fs AND options
  785. */
  786. static int ceph_compare_super(struct super_block *sb, void *data)
  787. {
  788. struct ceph_fs_client *new = data;
  789. struct ceph_mount_options *fsopt = new->mount_options;
  790. struct ceph_options *opt = new->client->options;
  791. struct ceph_fs_client *other = ceph_sb_to_client(sb);
  792. dout("ceph_compare_super %p\n", sb);
  793. if (compare_mount_options(fsopt, opt, other)) {
  794. dout("monitor(s)/mount options don't match\n");
  795. return 0;
  796. }
  797. if ((opt->flags & CEPH_OPT_FSID) &&
  798. ceph_fsid_compare(&opt->fsid, &other->client->fsid)) {
  799. dout("fsid doesn't match\n");
  800. return 0;
  801. }
  802. if (fsopt->sb_flags != other->mount_options->sb_flags) {
  803. dout("flags differ\n");
  804. return 0;
  805. }
  806. return 1;
  807. }
  808. /*
  809. * construct our own bdi so we can control readahead, etc.
  810. */
  811. static atomic_long_t bdi_seq = ATOMIC_LONG_INIT(0);
  812. static int ceph_register_bdi(struct super_block *sb,
  813. struct ceph_fs_client *fsc)
  814. {
  815. int err;
  816. /* set ra_pages based on rasize mount option? */
  817. if (fsc->mount_options->rasize >= PAGE_SIZE)
  818. fsc->backing_dev_info.ra_pages =
  819. (fsc->mount_options->rasize + PAGE_SIZE - 1)
  820. >> PAGE_SHIFT;
  821. else
  822. fsc->backing_dev_info.ra_pages =
  823. VM_MAX_READAHEAD * 1024 / PAGE_SIZE;
  824. err = bdi_register(&fsc->backing_dev_info, NULL, "ceph-%ld",
  825. atomic_long_inc_return(&bdi_seq));
  826. if (!err)
  827. sb->s_bdi = &fsc->backing_dev_info;
  828. return err;
  829. }
  830. static struct dentry *ceph_mount(struct file_system_type *fs_type,
  831. int flags, const char *dev_name, void *data)
  832. {
  833. struct super_block *sb;
  834. struct ceph_fs_client *fsc;
  835. struct dentry *res;
  836. int err;
  837. int (*compare_super)(struct super_block *, void *) = ceph_compare_super;
  838. struct ceph_mount_options *fsopt = NULL;
  839. struct ceph_options *opt = NULL;
  840. dout("ceph_mount\n");
  841. #ifdef CONFIG_CEPH_FS_POSIX_ACL
  842. flags |= MS_POSIXACL;
  843. #endif
  844. err = parse_mount_options(&fsopt, &opt, flags, data, dev_name);
  845. if (err < 0) {
  846. res = ERR_PTR(err);
  847. goto out_final;
  848. }
  849. /* create client (which we may/may not use) */
  850. fsc = create_fs_client(fsopt, opt);
  851. if (IS_ERR(fsc)) {
  852. res = ERR_CAST(fsc);
  853. destroy_mount_options(fsopt);
  854. ceph_destroy_options(opt);
  855. goto out_final;
  856. }
  857. err = ceph_mdsc_init(fsc);
  858. if (err < 0) {
  859. res = ERR_PTR(err);
  860. goto out;
  861. }
  862. if (ceph_test_opt(fsc->client, NOSHARE))
  863. compare_super = NULL;
  864. sb = sget(fs_type, compare_super, ceph_set_super, flags, fsc);
  865. if (IS_ERR(sb)) {
  866. res = ERR_CAST(sb);
  867. goto out;
  868. }
  869. if (ceph_sb_to_client(sb) != fsc) {
  870. ceph_mdsc_destroy(fsc);
  871. destroy_fs_client(fsc);
  872. fsc = ceph_sb_to_client(sb);
  873. dout("get_sb got existing client %p\n", fsc);
  874. } else {
  875. dout("get_sb using new client %p\n", fsc);
  876. err = ceph_register_bdi(sb, fsc);
  877. if (err < 0) {
  878. res = ERR_PTR(err);
  879. goto out_splat;
  880. }
  881. }
  882. res = ceph_real_mount(fsc);
  883. if (IS_ERR(res))
  884. goto out_splat;
  885. dout("root %p inode %p ino %llx.%llx\n", res,
  886. d_inode(res), ceph_vinop(d_inode(res)));
  887. return res;
  888. out_splat:
  889. ceph_mdsc_close_sessions(fsc->mdsc);
  890. deactivate_locked_super(sb);
  891. goto out_final;
  892. out:
  893. ceph_mdsc_destroy(fsc);
  894. destroy_fs_client(fsc);
  895. out_final:
  896. dout("ceph_mount fail %ld\n", PTR_ERR(res));
  897. return res;
  898. }
  899. static void ceph_kill_sb(struct super_block *s)
  900. {
  901. struct ceph_fs_client *fsc = ceph_sb_to_client(s);
  902. dev_t dev = s->s_dev;
  903. dout("kill_sb %p\n", s);
  904. ceph_mdsc_pre_umount(fsc->mdsc);
  905. generic_shutdown_super(s);
  906. ceph_mdsc_destroy(fsc);
  907. destroy_fs_client(fsc);
  908. free_anon_bdev(dev);
  909. }
  910. static struct file_system_type ceph_fs_type = {
  911. .owner = THIS_MODULE,
  912. .name = "ceph",
  913. .mount = ceph_mount,
  914. .kill_sb = ceph_kill_sb,
  915. .fs_flags = FS_RENAME_DOES_D_MOVE,
  916. };
  917. MODULE_ALIAS_FS("ceph");
  918. static int __init init_ceph(void)
  919. {
  920. int ret = init_caches();
  921. if (ret)
  922. goto out;
  923. ceph_flock_init();
  924. ceph_xattr_init();
  925. ret = register_filesystem(&ceph_fs_type);
  926. if (ret)
  927. goto out_xattr;
  928. pr_info("loaded (mds proto %d)\n", CEPH_MDSC_PROTOCOL);
  929. return 0;
  930. out_xattr:
  931. ceph_xattr_exit();
  932. destroy_caches();
  933. out:
  934. return ret;
  935. }
  936. static void __exit exit_ceph(void)
  937. {
  938. dout("exit_ceph\n");
  939. unregister_filesystem(&ceph_fs_type);
  940. ceph_xattr_exit();
  941. destroy_caches();
  942. }
  943. module_init(init_ceph);
  944. module_exit(exit_ceph);
  945. MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
  946. MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
  947. MODULE_AUTHOR("Patience Warnick <patience@newdream.net>");
  948. MODULE_DESCRIPTION("Ceph filesystem for Linux");
  949. MODULE_LICENSE("GPL");