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