super.c 44 KB

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