super.c 28 KB

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