super.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/ceph/ceph_debug.h>
  3. #include <linux/backing-dev.h>
  4. #include <linux/ctype.h>
  5. #include <linux/fs.h>
  6. #include <linux/inet.h>
  7. #include <linux/in6.h>
  8. #include <linux/module.h>
  9. #include <linux/mount.h>
  10. #include <linux/fs_context.h>
  11. #include <linux/fs_parser.h>
  12. #include <linux/sched.h>
  13. #include <linux/seq_file.h>
  14. #include <linux/slab.h>
  15. #include <linux/statfs.h>
  16. #include <linux/string.h>
  17. #include "super.h"
  18. #include "mds_client.h"
  19. #include "cache.h"
  20. #include <linux/ceph/ceph_features.h>
  21. #include <linux/ceph/decode.h>
  22. #include <linux/ceph/mon_client.h>
  23. #include <linux/ceph/auth.h>
  24. #include <linux/ceph/debugfs.h>
  25. #include <uapi/linux/magic.h>
  26. static DEFINE_SPINLOCK(ceph_fsc_lock);
  27. static LIST_HEAD(ceph_fsc_list);
  28. /*
  29. * Ceph superblock operations
  30. *
  31. * Handle the basics of mounting, unmounting.
  32. */
  33. /*
  34. * super ops
  35. */
  36. static void ceph_put_super(struct super_block *s)
  37. {
  38. struct ceph_fs_client *fsc = ceph_sb_to_client(s);
  39. dout("put_super\n");
  40. ceph_mdsc_close_sessions(fsc->mdsc);
  41. }
  42. static int ceph_statfs(struct dentry *dentry, struct kstatfs *buf)
  43. {
  44. struct ceph_fs_client *fsc = ceph_inode_to_client(d_inode(dentry));
  45. struct ceph_mon_client *monc = &fsc->client->monc;
  46. struct ceph_statfs st;
  47. int i, err;
  48. u64 data_pool;
  49. if (fsc->mdsc->mdsmap->m_num_data_pg_pools == 1) {
  50. data_pool = fsc->mdsc->mdsmap->m_data_pg_pools[0];
  51. } else {
  52. data_pool = CEPH_NOPOOL;
  53. }
  54. dout("statfs\n");
  55. err = ceph_monc_do_statfs(monc, data_pool, &st);
  56. if (err < 0)
  57. return err;
  58. /* fill in kstatfs */
  59. buf->f_type = CEPH_SUPER_MAGIC; /* ?? */
  60. /*
  61. * Express utilization in terms of large blocks to avoid
  62. * overflow on 32-bit machines.
  63. */
  64. buf->f_frsize = 1 << CEPH_BLOCK_SHIFT;
  65. /*
  66. * By default use root quota for stats; fallback to overall filesystem
  67. * usage if using 'noquotadf' mount option or if the root dir doesn't
  68. * have max_bytes quota set.
  69. */
  70. if (ceph_test_mount_opt(fsc, NOQUOTADF) ||
  71. !ceph_quota_update_statfs(fsc, buf)) {
  72. buf->f_blocks = le64_to_cpu(st.kb) >> (CEPH_BLOCK_SHIFT-10);
  73. buf->f_bfree = le64_to_cpu(st.kb_avail) >> (CEPH_BLOCK_SHIFT-10);
  74. buf->f_bavail = le64_to_cpu(st.kb_avail) >> (CEPH_BLOCK_SHIFT-10);
  75. }
  76. /*
  77. * NOTE: for the time being, we make bsize == frsize to humor
  78. * not-yet-ancient versions of glibc that are broken.
  79. * Someday, we will probably want to report a real block
  80. * size... whatever that may mean for a network file system!
  81. */
  82. buf->f_bsize = buf->f_frsize;
  83. buf->f_files = le64_to_cpu(st.num_objects);
  84. buf->f_ffree = -1;
  85. buf->f_namelen = NAME_MAX;
  86. /* Must convert the fsid, for consistent values across arches */
  87. buf->f_fsid.val[0] = 0;
  88. mutex_lock(&monc->mutex);
  89. for (i = 0 ; i < sizeof(monc->monmap->fsid) / sizeof(__le32) ; ++i)
  90. buf->f_fsid.val[0] ^= le32_to_cpu(((__le32 *)&monc->monmap->fsid)[i]);
  91. mutex_unlock(&monc->mutex);
  92. /* fold the fs_cluster_id into the upper bits */
  93. buf->f_fsid.val[1] = monc->fs_cluster_id;
  94. return 0;
  95. }
  96. static int ceph_sync_fs(struct super_block *sb, int wait)
  97. {
  98. struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
  99. if (!wait) {
  100. dout("sync_fs (non-blocking)\n");
  101. ceph_flush_dirty_caps(fsc->mdsc);
  102. dout("sync_fs (non-blocking) done\n");
  103. return 0;
  104. }
  105. dout("sync_fs (blocking)\n");
  106. ceph_osdc_sync(&fsc->client->osdc);
  107. ceph_mdsc_sync(fsc->mdsc);
  108. dout("sync_fs (blocking) done\n");
  109. return 0;
  110. }
  111. /*
  112. * mount options
  113. */
  114. enum {
  115. Opt_wsize,
  116. Opt_rsize,
  117. Opt_rasize,
  118. Opt_caps_wanted_delay_min,
  119. Opt_caps_wanted_delay_max,
  120. Opt_caps_max,
  121. Opt_readdir_max_entries,
  122. Opt_readdir_max_bytes,
  123. Opt_congestion_kb,
  124. /* int args above */
  125. Opt_snapdirname,
  126. Opt_mds_namespace,
  127. Opt_recover_session,
  128. Opt_source,
  129. Opt_mon_addr,
  130. /* string args above */
  131. Opt_dirstat,
  132. Opt_rbytes,
  133. Opt_asyncreaddir,
  134. Opt_dcache,
  135. Opt_ino32,
  136. Opt_fscache,
  137. Opt_poolperm,
  138. Opt_require_active_mds,
  139. Opt_acl,
  140. Opt_quotadf,
  141. Opt_copyfrom,
  142. Opt_wsync,
  143. Opt_pagecache,
  144. };
  145. enum ceph_recover_session_mode {
  146. ceph_recover_session_no,
  147. ceph_recover_session_clean
  148. };
  149. static const struct constant_table ceph_param_recover[] = {
  150. { "no", ceph_recover_session_no },
  151. { "clean", ceph_recover_session_clean },
  152. {}
  153. };
  154. static const struct fs_parameter_spec ceph_mount_parameters[] = {
  155. fsparam_flag_no ("acl", Opt_acl),
  156. fsparam_flag_no ("asyncreaddir", Opt_asyncreaddir),
  157. fsparam_s32 ("caps_max", Opt_caps_max),
  158. fsparam_u32 ("caps_wanted_delay_max", Opt_caps_wanted_delay_max),
  159. fsparam_u32 ("caps_wanted_delay_min", Opt_caps_wanted_delay_min),
  160. fsparam_u32 ("write_congestion_kb", Opt_congestion_kb),
  161. fsparam_flag_no ("copyfrom", Opt_copyfrom),
  162. fsparam_flag_no ("dcache", Opt_dcache),
  163. fsparam_flag_no ("dirstat", Opt_dirstat),
  164. fsparam_flag_no ("fsc", Opt_fscache), // fsc|nofsc
  165. fsparam_string ("fsc", Opt_fscache), // fsc=...
  166. fsparam_flag_no ("ino32", Opt_ino32),
  167. fsparam_string ("mds_namespace", Opt_mds_namespace),
  168. fsparam_flag_no ("poolperm", Opt_poolperm),
  169. fsparam_flag_no ("quotadf", Opt_quotadf),
  170. fsparam_u32 ("rasize", Opt_rasize),
  171. fsparam_flag_no ("rbytes", Opt_rbytes),
  172. fsparam_u32 ("readdir_max_bytes", Opt_readdir_max_bytes),
  173. fsparam_u32 ("readdir_max_entries", Opt_readdir_max_entries),
  174. fsparam_enum ("recover_session", Opt_recover_session, ceph_param_recover),
  175. fsparam_flag_no ("require_active_mds", Opt_require_active_mds),
  176. fsparam_u32 ("rsize", Opt_rsize),
  177. fsparam_string ("snapdirname", Opt_snapdirname),
  178. fsparam_string ("source", Opt_source),
  179. fsparam_string ("mon_addr", Opt_mon_addr),
  180. fsparam_u32 ("wsize", Opt_wsize),
  181. fsparam_flag_no ("wsync", Opt_wsync),
  182. fsparam_flag_no ("pagecache", Opt_pagecache),
  183. {}
  184. };
  185. struct ceph_parse_opts_ctx {
  186. struct ceph_options *copts;
  187. struct ceph_mount_options *opts;
  188. };
  189. /*
  190. * Remove adjacent slashes and then the trailing slash, unless it is
  191. * the only remaining character.
  192. *
  193. * E.g. "//dir1////dir2///" --> "/dir1/dir2", "///" --> "/".
  194. */
  195. static void canonicalize_path(char *path)
  196. {
  197. int i, j = 0;
  198. for (i = 0; path[i] != '\0'; i++) {
  199. if (path[i] != '/' || j < 1 || path[j - 1] != '/')
  200. path[j++] = path[i];
  201. }
  202. if (j > 1 && path[j - 1] == '/')
  203. j--;
  204. path[j] = '\0';
  205. }
  206. /*
  207. * Check if the mds namespace in ceph_mount_options matches
  208. * the passed in namespace string. First time match (when
  209. * ->mds_namespace is NULL) is treated specially, since
  210. * ->mds_namespace needs to be initialized by the caller.
  211. */
  212. static int namespace_equals(struct ceph_mount_options *fsopt,
  213. const char *namespace, size_t len)
  214. {
  215. return !(fsopt->mds_namespace &&
  216. (strlen(fsopt->mds_namespace) != len ||
  217. strncmp(fsopt->mds_namespace, namespace, len)));
  218. }
  219. static int ceph_parse_old_source(const char *dev_name, const char *dev_name_end,
  220. struct fs_context *fc)
  221. {
  222. int r;
  223. struct ceph_parse_opts_ctx *pctx = fc->fs_private;
  224. struct ceph_mount_options *fsopt = pctx->opts;
  225. if (*dev_name_end != ':')
  226. return invalfc(fc, "separator ':' missing in source");
  227. r = ceph_parse_mon_ips(dev_name, dev_name_end - dev_name,
  228. pctx->copts, fc->log.log, ',');
  229. if (r)
  230. return r;
  231. fsopt->new_dev_syntax = false;
  232. return 0;
  233. }
  234. static int ceph_parse_new_source(const char *dev_name, const char *dev_name_end,
  235. struct fs_context *fc)
  236. {
  237. size_t len;
  238. struct ceph_fsid fsid;
  239. struct ceph_parse_opts_ctx *pctx = fc->fs_private;
  240. struct ceph_mount_options *fsopt = pctx->opts;
  241. char *fsid_start, *fs_name_start;
  242. if (*dev_name_end != '=') {
  243. dout("separator '=' missing in source");
  244. return -EINVAL;
  245. }
  246. fsid_start = strchr(dev_name, '@');
  247. if (!fsid_start)
  248. return invalfc(fc, "missing cluster fsid");
  249. ++fsid_start; /* start of cluster fsid */
  250. fs_name_start = strchr(fsid_start, '.');
  251. if (!fs_name_start)
  252. return invalfc(fc, "missing file system name");
  253. if (ceph_parse_fsid(fsid_start, &fsid))
  254. return invalfc(fc, "Invalid FSID");
  255. ++fs_name_start; /* start of file system name */
  256. len = dev_name_end - fs_name_start;
  257. if (!namespace_equals(fsopt, fs_name_start, len))
  258. return invalfc(fc, "Mismatching mds_namespace");
  259. kfree(fsopt->mds_namespace);
  260. fsopt->mds_namespace = kstrndup(fs_name_start, len, GFP_KERNEL);
  261. if (!fsopt->mds_namespace)
  262. return -ENOMEM;
  263. dout("file system (mds namespace) '%s'\n", fsopt->mds_namespace);
  264. fsopt->new_dev_syntax = true;
  265. return 0;
  266. }
  267. /*
  268. * Parse the source parameter for new device format. Distinguish the device
  269. * spec from the path. Try parsing new device format and fallback to old
  270. * format if needed.
  271. *
  272. * New device syntax will looks like:
  273. * <device_spec>=/<path>
  274. * where
  275. * <device_spec> is [email protected]
  276. * <path> is optional, but if present must begin with '/'
  277. * (monitor addresses are passed via mount option)
  278. *
  279. * Old device syntax is:
  280. * <server_spec>[,<server_spec>...]:[<path>]
  281. * where
  282. * <server_spec> is <ip>[:<port>]
  283. * <path> is optional, but if present must begin with '/'
  284. */
  285. static int ceph_parse_source(struct fs_parameter *param, struct fs_context *fc)
  286. {
  287. struct ceph_parse_opts_ctx *pctx = fc->fs_private;
  288. struct ceph_mount_options *fsopt = pctx->opts;
  289. char *dev_name = param->string, *dev_name_end;
  290. int ret;
  291. dout("%s '%s'\n", __func__, dev_name);
  292. if (!dev_name || !*dev_name)
  293. return invalfc(fc, "Empty source");
  294. dev_name_end = strchr(dev_name, '/');
  295. if (dev_name_end) {
  296. /*
  297. * The server_path will include the whole chars from userland
  298. * including the leading '/'.
  299. */
  300. kfree(fsopt->server_path);
  301. fsopt->server_path = kstrdup(dev_name_end, GFP_KERNEL);
  302. if (!fsopt->server_path)
  303. return -ENOMEM;
  304. canonicalize_path(fsopt->server_path);
  305. } else {
  306. dev_name_end = dev_name + strlen(dev_name);
  307. }
  308. dev_name_end--; /* back up to separator */
  309. if (dev_name_end < dev_name)
  310. return invalfc(fc, "Path missing in source");
  311. dout("device name '%.*s'\n", (int)(dev_name_end - dev_name), dev_name);
  312. if (fsopt->server_path)
  313. dout("server path '%s'\n", fsopt->server_path);
  314. dout("trying new device syntax");
  315. ret = ceph_parse_new_source(dev_name, dev_name_end, fc);
  316. if (ret) {
  317. if (ret != -EINVAL)
  318. return ret;
  319. dout("trying old device syntax");
  320. ret = ceph_parse_old_source(dev_name, dev_name_end, fc);
  321. if (ret)
  322. return ret;
  323. }
  324. fc->source = param->string;
  325. param->string = NULL;
  326. return 0;
  327. }
  328. static int ceph_parse_mon_addr(struct fs_parameter *param,
  329. struct fs_context *fc)
  330. {
  331. struct ceph_parse_opts_ctx *pctx = fc->fs_private;
  332. struct ceph_mount_options *fsopt = pctx->opts;
  333. kfree(fsopt->mon_addr);
  334. fsopt->mon_addr = param->string;
  335. param->string = NULL;
  336. return ceph_parse_mon_ips(fsopt->mon_addr, strlen(fsopt->mon_addr),
  337. pctx->copts, fc->log.log, '/');
  338. }
  339. static int ceph_parse_mount_param(struct fs_context *fc,
  340. struct fs_parameter *param)
  341. {
  342. struct ceph_parse_opts_ctx *pctx = fc->fs_private;
  343. struct ceph_mount_options *fsopt = pctx->opts;
  344. struct fs_parse_result result;
  345. unsigned int mode;
  346. int token, ret;
  347. ret = ceph_parse_param(param, pctx->copts, fc->log.log);
  348. if (ret != -ENOPARAM)
  349. return ret;
  350. token = fs_parse(fc, ceph_mount_parameters, param, &result);
  351. dout("%s fs_parse '%s' token %d\n", __func__, param->key, token);
  352. if (token < 0)
  353. return token;
  354. switch (token) {
  355. case Opt_snapdirname:
  356. kfree(fsopt->snapdir_name);
  357. fsopt->snapdir_name = param->string;
  358. param->string = NULL;
  359. break;
  360. case Opt_mds_namespace:
  361. if (!namespace_equals(fsopt, param->string, strlen(param->string)))
  362. return invalfc(fc, "Mismatching mds_namespace");
  363. kfree(fsopt->mds_namespace);
  364. fsopt->mds_namespace = param->string;
  365. param->string = NULL;
  366. break;
  367. case Opt_recover_session:
  368. mode = result.uint_32;
  369. if (mode == ceph_recover_session_no)
  370. fsopt->flags &= ~CEPH_MOUNT_OPT_CLEANRECOVER;
  371. else if (mode == ceph_recover_session_clean)
  372. fsopt->flags |= CEPH_MOUNT_OPT_CLEANRECOVER;
  373. else
  374. BUG();
  375. break;
  376. case Opt_source:
  377. if (fc->source)
  378. return invalfc(fc, "Multiple sources specified");
  379. return ceph_parse_source(param, fc);
  380. case Opt_mon_addr:
  381. return ceph_parse_mon_addr(param, fc);
  382. case Opt_wsize:
  383. if (result.uint_32 < PAGE_SIZE ||
  384. result.uint_32 > CEPH_MAX_WRITE_SIZE)
  385. goto out_of_range;
  386. fsopt->wsize = ALIGN(result.uint_32, PAGE_SIZE);
  387. break;
  388. case Opt_rsize:
  389. if (result.uint_32 < PAGE_SIZE ||
  390. result.uint_32 > CEPH_MAX_READ_SIZE)
  391. goto out_of_range;
  392. fsopt->rsize = ALIGN(result.uint_32, PAGE_SIZE);
  393. break;
  394. case Opt_rasize:
  395. fsopt->rasize = ALIGN(result.uint_32, PAGE_SIZE);
  396. break;
  397. case Opt_caps_wanted_delay_min:
  398. if (result.uint_32 < 1)
  399. goto out_of_range;
  400. fsopt->caps_wanted_delay_min = result.uint_32;
  401. break;
  402. case Opt_caps_wanted_delay_max:
  403. if (result.uint_32 < 1)
  404. goto out_of_range;
  405. fsopt->caps_wanted_delay_max = result.uint_32;
  406. break;
  407. case Opt_caps_max:
  408. if (result.int_32 < 0)
  409. goto out_of_range;
  410. fsopt->caps_max = result.int_32;
  411. break;
  412. case Opt_readdir_max_entries:
  413. if (result.uint_32 < 1)
  414. goto out_of_range;
  415. fsopt->max_readdir = result.uint_32;
  416. break;
  417. case Opt_readdir_max_bytes:
  418. if (result.uint_32 < PAGE_SIZE && result.uint_32 != 0)
  419. goto out_of_range;
  420. fsopt->max_readdir_bytes = result.uint_32;
  421. break;
  422. case Opt_congestion_kb:
  423. if (result.uint_32 < 1024) /* at least 1M */
  424. goto out_of_range;
  425. fsopt->congestion_kb = result.uint_32;
  426. break;
  427. case Opt_dirstat:
  428. if (!result.negated)
  429. fsopt->flags |= CEPH_MOUNT_OPT_DIRSTAT;
  430. else
  431. fsopt->flags &= ~CEPH_MOUNT_OPT_DIRSTAT;
  432. break;
  433. case Opt_rbytes:
  434. if (!result.negated)
  435. fsopt->flags |= CEPH_MOUNT_OPT_RBYTES;
  436. else
  437. fsopt->flags &= ~CEPH_MOUNT_OPT_RBYTES;
  438. break;
  439. case Opt_asyncreaddir:
  440. if (!result.negated)
  441. fsopt->flags &= ~CEPH_MOUNT_OPT_NOASYNCREADDIR;
  442. else
  443. fsopt->flags |= CEPH_MOUNT_OPT_NOASYNCREADDIR;
  444. break;
  445. case Opt_dcache:
  446. if (!result.negated)
  447. fsopt->flags |= CEPH_MOUNT_OPT_DCACHE;
  448. else
  449. fsopt->flags &= ~CEPH_MOUNT_OPT_DCACHE;
  450. break;
  451. case Opt_ino32:
  452. if (!result.negated)
  453. fsopt->flags |= CEPH_MOUNT_OPT_INO32;
  454. else
  455. fsopt->flags &= ~CEPH_MOUNT_OPT_INO32;
  456. break;
  457. case Opt_fscache:
  458. #ifdef CONFIG_CEPH_FSCACHE
  459. kfree(fsopt->fscache_uniq);
  460. fsopt->fscache_uniq = NULL;
  461. if (result.negated) {
  462. fsopt->flags &= ~CEPH_MOUNT_OPT_FSCACHE;
  463. } else {
  464. fsopt->flags |= CEPH_MOUNT_OPT_FSCACHE;
  465. fsopt->fscache_uniq = param->string;
  466. param->string = NULL;
  467. }
  468. break;
  469. #else
  470. return invalfc(fc, "fscache support is disabled");
  471. #endif
  472. case Opt_poolperm:
  473. if (!result.negated)
  474. fsopt->flags &= ~CEPH_MOUNT_OPT_NOPOOLPERM;
  475. else
  476. fsopt->flags |= CEPH_MOUNT_OPT_NOPOOLPERM;
  477. break;
  478. case Opt_require_active_mds:
  479. if (!result.negated)
  480. fsopt->flags &= ~CEPH_MOUNT_OPT_MOUNTWAIT;
  481. else
  482. fsopt->flags |= CEPH_MOUNT_OPT_MOUNTWAIT;
  483. break;
  484. case Opt_quotadf:
  485. if (!result.negated)
  486. fsopt->flags &= ~CEPH_MOUNT_OPT_NOQUOTADF;
  487. else
  488. fsopt->flags |= CEPH_MOUNT_OPT_NOQUOTADF;
  489. break;
  490. case Opt_copyfrom:
  491. if (!result.negated)
  492. fsopt->flags &= ~CEPH_MOUNT_OPT_NOCOPYFROM;
  493. else
  494. fsopt->flags |= CEPH_MOUNT_OPT_NOCOPYFROM;
  495. break;
  496. case Opt_acl:
  497. if (!result.negated) {
  498. #ifdef CONFIG_CEPH_FS_POSIX_ACL
  499. fc->sb_flags |= SB_POSIXACL;
  500. #else
  501. return invalfc(fc, "POSIX ACL support is disabled");
  502. #endif
  503. } else {
  504. fc->sb_flags &= ~SB_POSIXACL;
  505. }
  506. break;
  507. case Opt_wsync:
  508. if (!result.negated)
  509. fsopt->flags &= ~CEPH_MOUNT_OPT_ASYNC_DIROPS;
  510. else
  511. fsopt->flags |= CEPH_MOUNT_OPT_ASYNC_DIROPS;
  512. break;
  513. case Opt_pagecache:
  514. if (result.negated)
  515. fsopt->flags |= CEPH_MOUNT_OPT_NOPAGECACHE;
  516. else
  517. fsopt->flags &= ~CEPH_MOUNT_OPT_NOPAGECACHE;
  518. break;
  519. default:
  520. BUG();
  521. }
  522. return 0;
  523. out_of_range:
  524. return invalfc(fc, "%s out of range", param->key);
  525. }
  526. static void destroy_mount_options(struct ceph_mount_options *args)
  527. {
  528. dout("destroy_mount_options %p\n", args);
  529. if (!args)
  530. return;
  531. kfree(args->snapdir_name);
  532. kfree(args->mds_namespace);
  533. kfree(args->server_path);
  534. kfree(args->fscache_uniq);
  535. kfree(args->mon_addr);
  536. kfree(args);
  537. }
  538. static int strcmp_null(const char *s1, const char *s2)
  539. {
  540. if (!s1 && !s2)
  541. return 0;
  542. if (s1 && !s2)
  543. return -1;
  544. if (!s1 && s2)
  545. return 1;
  546. return strcmp(s1, s2);
  547. }
  548. static int compare_mount_options(struct ceph_mount_options *new_fsopt,
  549. struct ceph_options *new_opt,
  550. struct ceph_fs_client *fsc)
  551. {
  552. struct ceph_mount_options *fsopt1 = new_fsopt;
  553. struct ceph_mount_options *fsopt2 = fsc->mount_options;
  554. int ofs = offsetof(struct ceph_mount_options, snapdir_name);
  555. int ret;
  556. ret = memcmp(fsopt1, fsopt2, ofs);
  557. if (ret)
  558. return ret;
  559. ret = strcmp_null(fsopt1->snapdir_name, fsopt2->snapdir_name);
  560. if (ret)
  561. return ret;
  562. ret = strcmp_null(fsopt1->mds_namespace, fsopt2->mds_namespace);
  563. if (ret)
  564. return ret;
  565. ret = strcmp_null(fsopt1->server_path, fsopt2->server_path);
  566. if (ret)
  567. return ret;
  568. ret = strcmp_null(fsopt1->fscache_uniq, fsopt2->fscache_uniq);
  569. if (ret)
  570. return ret;
  571. ret = strcmp_null(fsopt1->mon_addr, fsopt2->mon_addr);
  572. if (ret)
  573. return ret;
  574. return ceph_compare_options(new_opt, fsc->client);
  575. }
  576. /**
  577. * ceph_show_options - Show mount options in /proc/mounts
  578. * @m: seq_file to write to
  579. * @root: root of that (sub)tree
  580. */
  581. static int ceph_show_options(struct seq_file *m, struct dentry *root)
  582. {
  583. struct ceph_fs_client *fsc = ceph_sb_to_client(root->d_sb);
  584. struct ceph_mount_options *fsopt = fsc->mount_options;
  585. size_t pos;
  586. int ret;
  587. /* a comma between MNT/MS and client options */
  588. seq_putc(m, ',');
  589. pos = m->count;
  590. ret = ceph_print_client_options(m, fsc->client, false);
  591. if (ret)
  592. return ret;
  593. /* retract our comma if no client options */
  594. if (m->count == pos)
  595. m->count--;
  596. if (fsopt->flags & CEPH_MOUNT_OPT_DIRSTAT)
  597. seq_puts(m, ",dirstat");
  598. if ((fsopt->flags & CEPH_MOUNT_OPT_RBYTES))
  599. seq_puts(m, ",rbytes");
  600. if (fsopt->flags & CEPH_MOUNT_OPT_NOASYNCREADDIR)
  601. seq_puts(m, ",noasyncreaddir");
  602. if ((fsopt->flags & CEPH_MOUNT_OPT_DCACHE) == 0)
  603. seq_puts(m, ",nodcache");
  604. if (fsopt->flags & CEPH_MOUNT_OPT_INO32)
  605. seq_puts(m, ",ino32");
  606. if (fsopt->flags & CEPH_MOUNT_OPT_FSCACHE) {
  607. seq_show_option(m, "fsc", fsopt->fscache_uniq);
  608. }
  609. if (fsopt->flags & CEPH_MOUNT_OPT_NOPOOLPERM)
  610. seq_puts(m, ",nopoolperm");
  611. if (fsopt->flags & CEPH_MOUNT_OPT_NOQUOTADF)
  612. seq_puts(m, ",noquotadf");
  613. #ifdef CONFIG_CEPH_FS_POSIX_ACL
  614. if (root->d_sb->s_flags & SB_POSIXACL)
  615. seq_puts(m, ",acl");
  616. else
  617. seq_puts(m, ",noacl");
  618. #endif
  619. if ((fsopt->flags & CEPH_MOUNT_OPT_NOCOPYFROM) == 0)
  620. seq_puts(m, ",copyfrom");
  621. /* dump mds_namespace when old device syntax is in use */
  622. if (fsopt->mds_namespace && !fsopt->new_dev_syntax)
  623. seq_show_option(m, "mds_namespace", fsopt->mds_namespace);
  624. if (fsopt->mon_addr)
  625. seq_printf(m, ",mon_addr=%s", fsopt->mon_addr);
  626. if (fsopt->flags & CEPH_MOUNT_OPT_CLEANRECOVER)
  627. seq_show_option(m, "recover_session", "clean");
  628. if (!(fsopt->flags & CEPH_MOUNT_OPT_ASYNC_DIROPS))
  629. seq_puts(m, ",wsync");
  630. if (fsopt->flags & CEPH_MOUNT_OPT_NOPAGECACHE)
  631. seq_puts(m, ",nopagecache");
  632. if (fsopt->wsize != CEPH_MAX_WRITE_SIZE)
  633. seq_printf(m, ",wsize=%u", fsopt->wsize);
  634. if (fsopt->rsize != CEPH_MAX_READ_SIZE)
  635. seq_printf(m, ",rsize=%u", fsopt->rsize);
  636. if (fsopt->rasize != CEPH_RASIZE_DEFAULT)
  637. seq_printf(m, ",rasize=%u", fsopt->rasize);
  638. if (fsopt->congestion_kb != default_congestion_kb())
  639. seq_printf(m, ",write_congestion_kb=%u", fsopt->congestion_kb);
  640. if (fsopt->caps_max)
  641. seq_printf(m, ",caps_max=%d", fsopt->caps_max);
  642. if (fsopt->caps_wanted_delay_min != CEPH_CAPS_WANTED_DELAY_MIN_DEFAULT)
  643. seq_printf(m, ",caps_wanted_delay_min=%u",
  644. fsopt->caps_wanted_delay_min);
  645. if (fsopt->caps_wanted_delay_max != CEPH_CAPS_WANTED_DELAY_MAX_DEFAULT)
  646. seq_printf(m, ",caps_wanted_delay_max=%u",
  647. fsopt->caps_wanted_delay_max);
  648. if (fsopt->max_readdir != CEPH_MAX_READDIR_DEFAULT)
  649. seq_printf(m, ",readdir_max_entries=%u", fsopt->max_readdir);
  650. if (fsopt->max_readdir_bytes != CEPH_MAX_READDIR_BYTES_DEFAULT)
  651. seq_printf(m, ",readdir_max_bytes=%u", fsopt->max_readdir_bytes);
  652. if (strcmp(fsopt->snapdir_name, CEPH_SNAPDIRNAME_DEFAULT))
  653. seq_show_option(m, "snapdirname", fsopt->snapdir_name);
  654. return 0;
  655. }
  656. /*
  657. * handle any mon messages the standard library doesn't understand.
  658. * return error if we don't either.
  659. */
  660. static int extra_mon_dispatch(struct ceph_client *client, struct ceph_msg *msg)
  661. {
  662. struct ceph_fs_client *fsc = client->private;
  663. int type = le16_to_cpu(msg->hdr.type);
  664. switch (type) {
  665. case CEPH_MSG_MDS_MAP:
  666. ceph_mdsc_handle_mdsmap(fsc->mdsc, msg);
  667. return 0;
  668. case CEPH_MSG_FS_MAP_USER:
  669. ceph_mdsc_handle_fsmap(fsc->mdsc, msg);
  670. return 0;
  671. default:
  672. return -1;
  673. }
  674. }
  675. /*
  676. * create a new fs client
  677. *
  678. * Success or not, this function consumes @fsopt and @opt.
  679. */
  680. static struct ceph_fs_client *create_fs_client(struct ceph_mount_options *fsopt,
  681. struct ceph_options *opt)
  682. {
  683. struct ceph_fs_client *fsc;
  684. int err;
  685. fsc = kzalloc(sizeof(*fsc), GFP_KERNEL);
  686. if (!fsc) {
  687. err = -ENOMEM;
  688. goto fail;
  689. }
  690. fsc->client = ceph_create_client(opt, fsc);
  691. if (IS_ERR(fsc->client)) {
  692. err = PTR_ERR(fsc->client);
  693. goto fail;
  694. }
  695. opt = NULL; /* fsc->client now owns this */
  696. fsc->client->extra_mon_dispatch = extra_mon_dispatch;
  697. ceph_set_opt(fsc->client, ABORT_ON_FULL);
  698. if (!fsopt->mds_namespace) {
  699. ceph_monc_want_map(&fsc->client->monc, CEPH_SUB_MDSMAP,
  700. 0, true);
  701. } else {
  702. ceph_monc_want_map(&fsc->client->monc, CEPH_SUB_FSMAP,
  703. 0, false);
  704. }
  705. fsc->mount_options = fsopt;
  706. fsc->sb = NULL;
  707. fsc->mount_state = CEPH_MOUNT_MOUNTING;
  708. fsc->filp_gen = 1;
  709. fsc->have_copy_from2 = true;
  710. atomic_long_set(&fsc->writeback_count, 0);
  711. fsc->write_congested = false;
  712. err = -ENOMEM;
  713. /*
  714. * The number of concurrent works can be high but they don't need
  715. * to be processed in parallel, limit concurrency.
  716. */
  717. fsc->inode_wq = alloc_workqueue("ceph-inode", WQ_UNBOUND, 0);
  718. if (!fsc->inode_wq)
  719. goto fail_client;
  720. fsc->cap_wq = alloc_workqueue("ceph-cap", 0, 1);
  721. if (!fsc->cap_wq)
  722. goto fail_inode_wq;
  723. hash_init(fsc->async_unlink_conflict);
  724. spin_lock_init(&fsc->async_unlink_conflict_lock);
  725. spin_lock(&ceph_fsc_lock);
  726. list_add_tail(&fsc->metric_wakeup, &ceph_fsc_list);
  727. spin_unlock(&ceph_fsc_lock);
  728. return fsc;
  729. fail_inode_wq:
  730. destroy_workqueue(fsc->inode_wq);
  731. fail_client:
  732. ceph_destroy_client(fsc->client);
  733. fail:
  734. kfree(fsc);
  735. if (opt)
  736. ceph_destroy_options(opt);
  737. destroy_mount_options(fsopt);
  738. return ERR_PTR(err);
  739. }
  740. static void flush_fs_workqueues(struct ceph_fs_client *fsc)
  741. {
  742. flush_workqueue(fsc->inode_wq);
  743. flush_workqueue(fsc->cap_wq);
  744. }
  745. static void destroy_fs_client(struct ceph_fs_client *fsc)
  746. {
  747. dout("destroy_fs_client %p\n", fsc);
  748. spin_lock(&ceph_fsc_lock);
  749. list_del(&fsc->metric_wakeup);
  750. spin_unlock(&ceph_fsc_lock);
  751. ceph_mdsc_destroy(fsc);
  752. destroy_workqueue(fsc->inode_wq);
  753. destroy_workqueue(fsc->cap_wq);
  754. destroy_mount_options(fsc->mount_options);
  755. ceph_destroy_client(fsc->client);
  756. kfree(fsc);
  757. dout("destroy_fs_client %p done\n", fsc);
  758. }
  759. /*
  760. * caches
  761. */
  762. struct kmem_cache *ceph_inode_cachep;
  763. struct kmem_cache *ceph_cap_cachep;
  764. struct kmem_cache *ceph_cap_snap_cachep;
  765. struct kmem_cache *ceph_cap_flush_cachep;
  766. struct kmem_cache *ceph_dentry_cachep;
  767. struct kmem_cache *ceph_file_cachep;
  768. struct kmem_cache *ceph_dir_file_cachep;
  769. struct kmem_cache *ceph_mds_request_cachep;
  770. mempool_t *ceph_wb_pagevec_pool;
  771. static void ceph_inode_init_once(void *foo)
  772. {
  773. struct ceph_inode_info *ci = foo;
  774. inode_init_once(&ci->netfs.inode);
  775. }
  776. static int __init init_caches(void)
  777. {
  778. int error = -ENOMEM;
  779. ceph_inode_cachep = kmem_cache_create("ceph_inode_info",
  780. sizeof(struct ceph_inode_info),
  781. __alignof__(struct ceph_inode_info),
  782. SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD|
  783. SLAB_ACCOUNT, ceph_inode_init_once);
  784. if (!ceph_inode_cachep)
  785. return -ENOMEM;
  786. ceph_cap_cachep = KMEM_CACHE(ceph_cap, SLAB_MEM_SPREAD);
  787. if (!ceph_cap_cachep)
  788. goto bad_cap;
  789. ceph_cap_snap_cachep = KMEM_CACHE(ceph_cap_snap, SLAB_MEM_SPREAD);
  790. if (!ceph_cap_snap_cachep)
  791. goto bad_cap_snap;
  792. ceph_cap_flush_cachep = KMEM_CACHE(ceph_cap_flush,
  793. SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD);
  794. if (!ceph_cap_flush_cachep)
  795. goto bad_cap_flush;
  796. ceph_dentry_cachep = KMEM_CACHE(ceph_dentry_info,
  797. SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD);
  798. if (!ceph_dentry_cachep)
  799. goto bad_dentry;
  800. ceph_file_cachep = KMEM_CACHE(ceph_file_info, SLAB_MEM_SPREAD);
  801. if (!ceph_file_cachep)
  802. goto bad_file;
  803. ceph_dir_file_cachep = KMEM_CACHE(ceph_dir_file_info, SLAB_MEM_SPREAD);
  804. if (!ceph_dir_file_cachep)
  805. goto bad_dir_file;
  806. ceph_mds_request_cachep = KMEM_CACHE(ceph_mds_request, SLAB_MEM_SPREAD);
  807. if (!ceph_mds_request_cachep)
  808. goto bad_mds_req;
  809. ceph_wb_pagevec_pool = mempool_create_kmalloc_pool(10, CEPH_MAX_WRITE_SIZE >> PAGE_SHIFT);
  810. if (!ceph_wb_pagevec_pool)
  811. goto bad_pagevec_pool;
  812. return 0;
  813. bad_pagevec_pool:
  814. kmem_cache_destroy(ceph_mds_request_cachep);
  815. bad_mds_req:
  816. kmem_cache_destroy(ceph_dir_file_cachep);
  817. bad_dir_file:
  818. kmem_cache_destroy(ceph_file_cachep);
  819. bad_file:
  820. kmem_cache_destroy(ceph_dentry_cachep);
  821. bad_dentry:
  822. kmem_cache_destroy(ceph_cap_flush_cachep);
  823. bad_cap_flush:
  824. kmem_cache_destroy(ceph_cap_snap_cachep);
  825. bad_cap_snap:
  826. kmem_cache_destroy(ceph_cap_cachep);
  827. bad_cap:
  828. kmem_cache_destroy(ceph_inode_cachep);
  829. return error;
  830. }
  831. static void destroy_caches(void)
  832. {
  833. /*
  834. * Make sure all delayed rcu free inodes are flushed before we
  835. * destroy cache.
  836. */
  837. rcu_barrier();
  838. kmem_cache_destroy(ceph_inode_cachep);
  839. kmem_cache_destroy(ceph_cap_cachep);
  840. kmem_cache_destroy(ceph_cap_snap_cachep);
  841. kmem_cache_destroy(ceph_cap_flush_cachep);
  842. kmem_cache_destroy(ceph_dentry_cachep);
  843. kmem_cache_destroy(ceph_file_cachep);
  844. kmem_cache_destroy(ceph_dir_file_cachep);
  845. kmem_cache_destroy(ceph_mds_request_cachep);
  846. mempool_destroy(ceph_wb_pagevec_pool);
  847. }
  848. static void __ceph_umount_begin(struct ceph_fs_client *fsc)
  849. {
  850. ceph_osdc_abort_requests(&fsc->client->osdc, -EIO);
  851. ceph_mdsc_force_umount(fsc->mdsc);
  852. fsc->filp_gen++; // invalidate open files
  853. }
  854. /*
  855. * ceph_umount_begin - initiate forced umount. Tear down the
  856. * mount, skipping steps that may hang while waiting for server(s).
  857. */
  858. void ceph_umount_begin(struct super_block *sb)
  859. {
  860. struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
  861. dout("ceph_umount_begin - starting forced umount\n");
  862. if (!fsc)
  863. return;
  864. fsc->mount_state = CEPH_MOUNT_SHUTDOWN;
  865. __ceph_umount_begin(fsc);
  866. }
  867. static const struct super_operations ceph_super_ops = {
  868. .alloc_inode = ceph_alloc_inode,
  869. .free_inode = ceph_free_inode,
  870. .write_inode = ceph_write_inode,
  871. .drop_inode = generic_delete_inode,
  872. .evict_inode = ceph_evict_inode,
  873. .sync_fs = ceph_sync_fs,
  874. .put_super = ceph_put_super,
  875. .show_options = ceph_show_options,
  876. .statfs = ceph_statfs,
  877. .umount_begin = ceph_umount_begin,
  878. };
  879. /*
  880. * Bootstrap mount by opening the root directory. Note the mount
  881. * @started time from caller, and time out if this takes too long.
  882. */
  883. static struct dentry *open_root_dentry(struct ceph_fs_client *fsc,
  884. const char *path,
  885. unsigned long started)
  886. {
  887. struct ceph_mds_client *mdsc = fsc->mdsc;
  888. struct ceph_mds_request *req = NULL;
  889. int err;
  890. struct dentry *root;
  891. /* open dir */
  892. dout("open_root_inode opening '%s'\n", path);
  893. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS);
  894. if (IS_ERR(req))
  895. return ERR_CAST(req);
  896. req->r_path1 = kstrdup(path, GFP_NOFS);
  897. if (!req->r_path1) {
  898. root = ERR_PTR(-ENOMEM);
  899. goto out;
  900. }
  901. req->r_ino1.ino = CEPH_INO_ROOT;
  902. req->r_ino1.snap = CEPH_NOSNAP;
  903. req->r_started = started;
  904. req->r_timeout = fsc->client->options->mount_timeout;
  905. req->r_args.getattr.mask = cpu_to_le32(CEPH_STAT_CAP_INODE);
  906. req->r_num_caps = 2;
  907. err = ceph_mdsc_do_request(mdsc, NULL, req);
  908. if (err == 0) {
  909. struct inode *inode = req->r_target_inode;
  910. req->r_target_inode = NULL;
  911. dout("open_root_inode success\n");
  912. root = d_make_root(inode);
  913. if (!root) {
  914. root = ERR_PTR(-ENOMEM);
  915. goto out;
  916. }
  917. dout("open_root_inode success, root dentry is %p\n", root);
  918. } else {
  919. root = ERR_PTR(err);
  920. }
  921. out:
  922. ceph_mdsc_put_request(req);
  923. return root;
  924. }
  925. /*
  926. * mount: join the ceph cluster, and open root directory.
  927. */
  928. static struct dentry *ceph_real_mount(struct ceph_fs_client *fsc,
  929. struct fs_context *fc)
  930. {
  931. int err;
  932. unsigned long started = jiffies; /* note the start time */
  933. struct dentry *root;
  934. dout("mount start %p\n", fsc);
  935. mutex_lock(&fsc->client->mount_mutex);
  936. if (!fsc->sb->s_root) {
  937. const char *path = fsc->mount_options->server_path ?
  938. fsc->mount_options->server_path + 1 : "";
  939. err = __ceph_open_session(fsc->client, started);
  940. if (err < 0)
  941. goto out;
  942. /* setup fscache */
  943. if (fsc->mount_options->flags & CEPH_MOUNT_OPT_FSCACHE) {
  944. err = ceph_fscache_register_fs(fsc, fc);
  945. if (err < 0)
  946. goto out;
  947. }
  948. dout("mount opening path '%s'\n", path);
  949. ceph_fs_debugfs_init(fsc);
  950. root = open_root_dentry(fsc, path, started);
  951. if (IS_ERR(root)) {
  952. err = PTR_ERR(root);
  953. goto out;
  954. }
  955. fsc->sb->s_root = dget(root);
  956. } else {
  957. root = dget(fsc->sb->s_root);
  958. }
  959. fsc->mount_state = CEPH_MOUNT_MOUNTED;
  960. dout("mount success\n");
  961. mutex_unlock(&fsc->client->mount_mutex);
  962. return root;
  963. out:
  964. mutex_unlock(&fsc->client->mount_mutex);
  965. return ERR_PTR(err);
  966. }
  967. static int ceph_set_super(struct super_block *s, struct fs_context *fc)
  968. {
  969. struct ceph_fs_client *fsc = s->s_fs_info;
  970. int ret;
  971. dout("set_super %p\n", s);
  972. s->s_maxbytes = MAX_LFS_FILESIZE;
  973. s->s_xattr = ceph_xattr_handlers;
  974. fsc->sb = s;
  975. fsc->max_file_size = 1ULL << 40; /* temp value until we get mdsmap */
  976. s->s_op = &ceph_super_ops;
  977. s->s_d_op = &ceph_dentry_ops;
  978. s->s_export_op = &ceph_export_ops;
  979. s->s_time_gran = 1;
  980. s->s_time_min = 0;
  981. s->s_time_max = U32_MAX;
  982. s->s_flags |= SB_NODIRATIME | SB_NOATIME;
  983. ret = set_anon_super_fc(s, fc);
  984. if (ret != 0)
  985. fsc->sb = NULL;
  986. return ret;
  987. }
  988. /*
  989. * share superblock if same fs AND options
  990. */
  991. static int ceph_compare_super(struct super_block *sb, struct fs_context *fc)
  992. {
  993. struct ceph_fs_client *new = fc->s_fs_info;
  994. struct ceph_mount_options *fsopt = new->mount_options;
  995. struct ceph_options *opt = new->client->options;
  996. struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
  997. dout("ceph_compare_super %p\n", sb);
  998. if (compare_mount_options(fsopt, opt, fsc)) {
  999. dout("monitor(s)/mount options don't match\n");
  1000. return 0;
  1001. }
  1002. if ((opt->flags & CEPH_OPT_FSID) &&
  1003. ceph_fsid_compare(&opt->fsid, &fsc->client->fsid)) {
  1004. dout("fsid doesn't match\n");
  1005. return 0;
  1006. }
  1007. if (fc->sb_flags != (sb->s_flags & ~SB_BORN)) {
  1008. dout("flags differ\n");
  1009. return 0;
  1010. }
  1011. if (fsc->blocklisted && !ceph_test_mount_opt(fsc, CLEANRECOVER)) {
  1012. dout("client is blocklisted (and CLEANRECOVER is not set)\n");
  1013. return 0;
  1014. }
  1015. if (fsc->mount_state == CEPH_MOUNT_SHUTDOWN) {
  1016. dout("client has been forcibly unmounted\n");
  1017. return 0;
  1018. }
  1019. return 1;
  1020. }
  1021. /*
  1022. * construct our own bdi so we can control readahead, etc.
  1023. */
  1024. static atomic_long_t bdi_seq = ATOMIC_LONG_INIT(0);
  1025. static int ceph_setup_bdi(struct super_block *sb, struct ceph_fs_client *fsc)
  1026. {
  1027. int err;
  1028. err = super_setup_bdi_name(sb, "ceph-%ld",
  1029. atomic_long_inc_return(&bdi_seq));
  1030. if (err)
  1031. return err;
  1032. /* set ra_pages based on rasize mount option? */
  1033. sb->s_bdi->ra_pages = fsc->mount_options->rasize >> PAGE_SHIFT;
  1034. /* set io_pages based on max osd read size */
  1035. sb->s_bdi->io_pages = fsc->mount_options->rsize >> PAGE_SHIFT;
  1036. return 0;
  1037. }
  1038. static int ceph_get_tree(struct fs_context *fc)
  1039. {
  1040. struct ceph_parse_opts_ctx *pctx = fc->fs_private;
  1041. struct ceph_mount_options *fsopt = pctx->opts;
  1042. struct super_block *sb;
  1043. struct ceph_fs_client *fsc;
  1044. struct dentry *res;
  1045. int (*compare_super)(struct super_block *, struct fs_context *) =
  1046. ceph_compare_super;
  1047. int err;
  1048. dout("ceph_get_tree\n");
  1049. if (!fc->source)
  1050. return invalfc(fc, "No source");
  1051. if (fsopt->new_dev_syntax && !fsopt->mon_addr)
  1052. return invalfc(fc, "No monitor address");
  1053. /* create client (which we may/may not use) */
  1054. fsc = create_fs_client(pctx->opts, pctx->copts);
  1055. pctx->opts = NULL;
  1056. pctx->copts = NULL;
  1057. if (IS_ERR(fsc)) {
  1058. err = PTR_ERR(fsc);
  1059. goto out_final;
  1060. }
  1061. err = ceph_mdsc_init(fsc);
  1062. if (err < 0)
  1063. goto out;
  1064. if (ceph_test_opt(fsc->client, NOSHARE))
  1065. compare_super = NULL;
  1066. fc->s_fs_info = fsc;
  1067. sb = sget_fc(fc, compare_super, ceph_set_super);
  1068. fc->s_fs_info = NULL;
  1069. if (IS_ERR(sb)) {
  1070. err = PTR_ERR(sb);
  1071. goto out;
  1072. }
  1073. if (ceph_sb_to_client(sb) != fsc) {
  1074. destroy_fs_client(fsc);
  1075. fsc = ceph_sb_to_client(sb);
  1076. dout("get_sb got existing client %p\n", fsc);
  1077. } else {
  1078. dout("get_sb using new client %p\n", fsc);
  1079. err = ceph_setup_bdi(sb, fsc);
  1080. if (err < 0)
  1081. goto out_splat;
  1082. }
  1083. res = ceph_real_mount(fsc, fc);
  1084. if (IS_ERR(res)) {
  1085. err = PTR_ERR(res);
  1086. goto out_splat;
  1087. }
  1088. dout("root %p inode %p ino %llx.%llx\n", res,
  1089. d_inode(res), ceph_vinop(d_inode(res)));
  1090. fc->root = fsc->sb->s_root;
  1091. return 0;
  1092. out_splat:
  1093. if (!ceph_mdsmap_is_cluster_available(fsc->mdsc->mdsmap)) {
  1094. pr_info("No mds server is up or the cluster is laggy\n");
  1095. err = -EHOSTUNREACH;
  1096. }
  1097. ceph_mdsc_close_sessions(fsc->mdsc);
  1098. deactivate_locked_super(sb);
  1099. goto out_final;
  1100. out:
  1101. destroy_fs_client(fsc);
  1102. out_final:
  1103. dout("ceph_get_tree fail %d\n", err);
  1104. return err;
  1105. }
  1106. static void ceph_free_fc(struct fs_context *fc)
  1107. {
  1108. struct ceph_parse_opts_ctx *pctx = fc->fs_private;
  1109. if (pctx) {
  1110. destroy_mount_options(pctx->opts);
  1111. ceph_destroy_options(pctx->copts);
  1112. kfree(pctx);
  1113. }
  1114. }
  1115. static int ceph_reconfigure_fc(struct fs_context *fc)
  1116. {
  1117. struct ceph_parse_opts_ctx *pctx = fc->fs_private;
  1118. struct ceph_mount_options *fsopt = pctx->opts;
  1119. struct ceph_fs_client *fsc = ceph_sb_to_client(fc->root->d_sb);
  1120. if (fsopt->flags & CEPH_MOUNT_OPT_ASYNC_DIROPS)
  1121. ceph_set_mount_opt(fsc, ASYNC_DIROPS);
  1122. else
  1123. ceph_clear_mount_opt(fsc, ASYNC_DIROPS);
  1124. if (strcmp_null(fsc->mount_options->mon_addr, fsopt->mon_addr)) {
  1125. kfree(fsc->mount_options->mon_addr);
  1126. fsc->mount_options->mon_addr = fsopt->mon_addr;
  1127. fsopt->mon_addr = NULL;
  1128. pr_notice("ceph: monitor addresses recorded, but not used for reconnection");
  1129. }
  1130. sync_filesystem(fc->root->d_sb);
  1131. return 0;
  1132. }
  1133. static const struct fs_context_operations ceph_context_ops = {
  1134. .free = ceph_free_fc,
  1135. .parse_param = ceph_parse_mount_param,
  1136. .get_tree = ceph_get_tree,
  1137. .reconfigure = ceph_reconfigure_fc,
  1138. };
  1139. /*
  1140. * Set up the filesystem mount context.
  1141. */
  1142. static int ceph_init_fs_context(struct fs_context *fc)
  1143. {
  1144. struct ceph_parse_opts_ctx *pctx;
  1145. struct ceph_mount_options *fsopt;
  1146. pctx = kzalloc(sizeof(*pctx), GFP_KERNEL);
  1147. if (!pctx)
  1148. return -ENOMEM;
  1149. pctx->copts = ceph_alloc_options();
  1150. if (!pctx->copts)
  1151. goto nomem;
  1152. pctx->opts = kzalloc(sizeof(*pctx->opts), GFP_KERNEL);
  1153. if (!pctx->opts)
  1154. goto nomem;
  1155. fsopt = pctx->opts;
  1156. fsopt->flags = CEPH_MOUNT_OPT_DEFAULT;
  1157. fsopt->wsize = CEPH_MAX_WRITE_SIZE;
  1158. fsopt->rsize = CEPH_MAX_READ_SIZE;
  1159. fsopt->rasize = CEPH_RASIZE_DEFAULT;
  1160. fsopt->snapdir_name = kstrdup(CEPH_SNAPDIRNAME_DEFAULT, GFP_KERNEL);
  1161. if (!fsopt->snapdir_name)
  1162. goto nomem;
  1163. fsopt->caps_wanted_delay_min = CEPH_CAPS_WANTED_DELAY_MIN_DEFAULT;
  1164. fsopt->caps_wanted_delay_max = CEPH_CAPS_WANTED_DELAY_MAX_DEFAULT;
  1165. fsopt->max_readdir = CEPH_MAX_READDIR_DEFAULT;
  1166. fsopt->max_readdir_bytes = CEPH_MAX_READDIR_BYTES_DEFAULT;
  1167. fsopt->congestion_kb = default_congestion_kb();
  1168. #ifdef CONFIG_CEPH_FS_POSIX_ACL
  1169. fc->sb_flags |= SB_POSIXACL;
  1170. #endif
  1171. fc->fs_private = pctx;
  1172. fc->ops = &ceph_context_ops;
  1173. return 0;
  1174. nomem:
  1175. destroy_mount_options(pctx->opts);
  1176. ceph_destroy_options(pctx->copts);
  1177. kfree(pctx);
  1178. return -ENOMEM;
  1179. }
  1180. /*
  1181. * Return true if it successfully increases the blocker counter,
  1182. * or false if the mdsc is in stopping and flushed state.
  1183. */
  1184. static bool __inc_stopping_blocker(struct ceph_mds_client *mdsc)
  1185. {
  1186. spin_lock(&mdsc->stopping_lock);
  1187. if (mdsc->stopping >= CEPH_MDSC_STOPPING_FLUSHING) {
  1188. spin_unlock(&mdsc->stopping_lock);
  1189. return false;
  1190. }
  1191. atomic_inc(&mdsc->stopping_blockers);
  1192. spin_unlock(&mdsc->stopping_lock);
  1193. return true;
  1194. }
  1195. static void __dec_stopping_blocker(struct ceph_mds_client *mdsc)
  1196. {
  1197. spin_lock(&mdsc->stopping_lock);
  1198. if (!atomic_dec_return(&mdsc->stopping_blockers) &&
  1199. mdsc->stopping >= CEPH_MDSC_STOPPING_FLUSHING)
  1200. complete_all(&mdsc->stopping_waiter);
  1201. spin_unlock(&mdsc->stopping_lock);
  1202. }
  1203. /* For metadata IO requests */
  1204. bool ceph_inc_mds_stopping_blocker(struct ceph_mds_client *mdsc,
  1205. struct ceph_mds_session *session)
  1206. {
  1207. mutex_lock(&session->s_mutex);
  1208. inc_session_sequence(session);
  1209. mutex_unlock(&session->s_mutex);
  1210. return __inc_stopping_blocker(mdsc);
  1211. }
  1212. void ceph_dec_mds_stopping_blocker(struct ceph_mds_client *mdsc)
  1213. {
  1214. __dec_stopping_blocker(mdsc);
  1215. }
  1216. static void ceph_kill_sb(struct super_block *s)
  1217. {
  1218. struct ceph_fs_client *fsc = ceph_sb_to_client(s);
  1219. struct ceph_mds_client *mdsc = fsc->mdsc;
  1220. bool wait;
  1221. dout("kill_sb %p\n", s);
  1222. ceph_mdsc_pre_umount(mdsc);
  1223. flush_fs_workqueues(fsc);
  1224. /*
  1225. * Though the kill_anon_super() will finally trigger the
  1226. * sync_filesystem() anyway, we still need to do it here and
  1227. * then bump the stage of shutdown. This will allow us to
  1228. * drop any further message, which will increase the inodes'
  1229. * i_count reference counters but makes no sense any more,
  1230. * from MDSs.
  1231. *
  1232. * Without this when evicting the inodes it may fail in the
  1233. * kill_anon_super(), which will trigger a warning when
  1234. * destroying the fscrypt keyring and then possibly trigger
  1235. * a further crash in ceph module when the iput() tries to
  1236. * evict the inodes later.
  1237. */
  1238. sync_filesystem(s);
  1239. spin_lock(&mdsc->stopping_lock);
  1240. mdsc->stopping = CEPH_MDSC_STOPPING_FLUSHING;
  1241. wait = !!atomic_read(&mdsc->stopping_blockers);
  1242. spin_unlock(&mdsc->stopping_lock);
  1243. if (wait && atomic_read(&mdsc->stopping_blockers)) {
  1244. long timeleft = wait_for_completion_killable_timeout(
  1245. &mdsc->stopping_waiter,
  1246. fsc->client->options->mount_timeout);
  1247. if (!timeleft) /* timed out */
  1248. pr_warn("umount timed out, %ld\n", timeleft);
  1249. else if (timeleft < 0) /* killed */
  1250. pr_warn("umount was killed, %ld\n", timeleft);
  1251. }
  1252. mdsc->stopping = CEPH_MDSC_STOPPING_FLUSHED;
  1253. kill_anon_super(s);
  1254. fsc->client->extra_mon_dispatch = NULL;
  1255. ceph_fs_debugfs_cleanup(fsc);
  1256. ceph_fscache_unregister_fs(fsc);
  1257. destroy_fs_client(fsc);
  1258. }
  1259. static struct file_system_type ceph_fs_type = {
  1260. .owner = THIS_MODULE,
  1261. .name = "ceph",
  1262. .init_fs_context = ceph_init_fs_context,
  1263. .kill_sb = ceph_kill_sb,
  1264. .fs_flags = FS_RENAME_DOES_D_MOVE,
  1265. };
  1266. MODULE_ALIAS_FS("ceph");
  1267. int ceph_force_reconnect(struct super_block *sb)
  1268. {
  1269. struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
  1270. int err = 0;
  1271. fsc->mount_state = CEPH_MOUNT_RECOVER;
  1272. __ceph_umount_begin(fsc);
  1273. /* Make sure all page caches get invalidated.
  1274. * see remove_session_caps_cb() */
  1275. flush_workqueue(fsc->inode_wq);
  1276. /* In case that we were blocklisted. This also reset
  1277. * all mon/osd connections */
  1278. ceph_reset_client_addr(fsc->client);
  1279. ceph_osdc_clear_abort_err(&fsc->client->osdc);
  1280. fsc->blocklisted = false;
  1281. fsc->mount_state = CEPH_MOUNT_MOUNTED;
  1282. if (sb->s_root) {
  1283. err = __ceph_do_getattr(d_inode(sb->s_root), NULL,
  1284. CEPH_STAT_CAP_INODE, true);
  1285. }
  1286. return err;
  1287. }
  1288. static int __init init_ceph(void)
  1289. {
  1290. int ret = init_caches();
  1291. if (ret)
  1292. goto out;
  1293. ceph_flock_init();
  1294. ret = register_filesystem(&ceph_fs_type);
  1295. if (ret)
  1296. goto out_caches;
  1297. pr_info("loaded (mds proto %d)\n", CEPH_MDSC_PROTOCOL);
  1298. return 0;
  1299. out_caches:
  1300. destroy_caches();
  1301. out:
  1302. return ret;
  1303. }
  1304. static void __exit exit_ceph(void)
  1305. {
  1306. dout("exit_ceph\n");
  1307. unregister_filesystem(&ceph_fs_type);
  1308. destroy_caches();
  1309. }
  1310. static int param_set_metrics(const char *val, const struct kernel_param *kp)
  1311. {
  1312. struct ceph_fs_client *fsc;
  1313. int ret;
  1314. ret = param_set_bool(val, kp);
  1315. if (ret) {
  1316. pr_err("Failed to parse sending metrics switch value '%s'\n",
  1317. val);
  1318. return ret;
  1319. } else if (!disable_send_metrics) {
  1320. // wake up all the mds clients
  1321. spin_lock(&ceph_fsc_lock);
  1322. list_for_each_entry(fsc, &ceph_fsc_list, metric_wakeup) {
  1323. metric_schedule_delayed(&fsc->mdsc->metric);
  1324. }
  1325. spin_unlock(&ceph_fsc_lock);
  1326. }
  1327. return 0;
  1328. }
  1329. static const struct kernel_param_ops param_ops_metrics = {
  1330. .set = param_set_metrics,
  1331. .get = param_get_bool,
  1332. };
  1333. bool disable_send_metrics = false;
  1334. module_param_cb(disable_send_metrics, &param_ops_metrics, &disable_send_metrics, 0644);
  1335. MODULE_PARM_DESC(disable_send_metrics, "Enable sending perf metrics to ceph cluster (default: on)");
  1336. /* for both v1 and v2 syntax */
  1337. static bool mount_support = true;
  1338. static const struct kernel_param_ops param_ops_mount_syntax = {
  1339. .get = param_get_bool,
  1340. };
  1341. module_param_cb(mount_syntax_v1, &param_ops_mount_syntax, &mount_support, 0444);
  1342. module_param_cb(mount_syntax_v2, &param_ops_mount_syntax, &mount_support, 0444);
  1343. module_init(init_ceph);
  1344. module_exit(exit_ceph);
  1345. MODULE_AUTHOR("Sage Weil <[email protected]>");
  1346. MODULE_AUTHOR("Yehuda Sadeh <[email protected]>");
  1347. MODULE_AUTHOR("Patience Warnick <[email protected]>");
  1348. MODULE_DESCRIPTION("Ceph filesystem for Linux");
  1349. MODULE_LICENSE("GPL");