super.c 27 KB

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