super.c 25 KB

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