1 // SPDX-License-Identifier: GPL-2.0-only
3 #include <linux/ceph/ceph_debug.h>
5 #include <linux/backing-dev.h>
6 #include <linux/ctype.h>
8 #include <linux/inet.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>
21 #include "mds_client.h"
24 #include <linux/ceph/ceph_features.h>
25 #include <linux/ceph/decode.h>
26 #include <linux/ceph/mon_client.h>
27 #include <linux/ceph/auth.h>
28 #include <linux/ceph/debugfs.h>
30 static DEFINE_SPINLOCK(ceph_fsc_lock);
31 static LIST_HEAD(ceph_fsc_list);
34 * Ceph superblock operations
36 * Handle the basics of mounting, unmounting.
42 static void ceph_put_super(struct super_block *s)
44 struct ceph_fs_client *fsc = ceph_sb_to_client(s);
47 ceph_mdsc_close_sessions(fsc->mdsc);
50 static int ceph_statfs(struct dentry *dentry, struct kstatfs *buf)
52 struct ceph_fs_client *fsc = ceph_inode_to_client(d_inode(dentry));
53 struct ceph_mon_client *monc = &fsc->client->monc;
54 struct ceph_statfs st;
58 if (fsc->mdsc->mdsmap->m_num_data_pg_pools == 1) {
59 data_pool = fsc->mdsc->mdsmap->m_data_pg_pools[0];
61 data_pool = CEPH_NOPOOL;
65 err = ceph_monc_do_statfs(monc, data_pool, &st);
70 buf->f_type = CEPH_SUPER_MAGIC; /* ?? */
73 * express utilization in terms of large blocks to avoid
74 * overflow on 32-bit machines.
76 * NOTE: for the time being, we make bsize == frsize to humor
77 * not-yet-ancient versions of glibc that are broken.
78 * Someday, we will probably want to report a real block
79 * size... whatever that may mean for a network file system!
81 buf->f_bsize = 1 << CEPH_BLOCK_SHIFT;
82 buf->f_frsize = 1 << CEPH_BLOCK_SHIFT;
85 * By default use root quota for stats; fallback to overall filesystem
86 * usage if using 'noquotadf' mount option or if the root dir doesn't
87 * have max_bytes quota set.
89 if (ceph_test_mount_opt(fsc, NOQUOTADF) ||
90 !ceph_quota_update_statfs(fsc, buf)) {
91 buf->f_blocks = le64_to_cpu(st.kb) >> (CEPH_BLOCK_SHIFT-10);
92 buf->f_bfree = le64_to_cpu(st.kb_avail) >> (CEPH_BLOCK_SHIFT-10);
93 buf->f_bavail = le64_to_cpu(st.kb_avail) >> (CEPH_BLOCK_SHIFT-10);
96 buf->f_files = le64_to_cpu(st.num_objects);
98 buf->f_namelen = NAME_MAX;
100 /* Must convert the fsid, for consistent values across arches */
101 buf->f_fsid.val[0] = 0;
102 mutex_lock(&monc->mutex);
103 for (i = 0 ; i < sizeof(monc->monmap->fsid) / sizeof(__le32) ; ++i)
104 buf->f_fsid.val[0] ^= le32_to_cpu(((__le32 *)&monc->monmap->fsid)[i]);
105 mutex_unlock(&monc->mutex);
107 /* fold the fs_cluster_id into the upper bits */
108 buf->f_fsid.val[1] = monc->fs_cluster_id;
113 static int ceph_sync_fs(struct super_block *sb, int wait)
115 struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
118 dout("sync_fs (non-blocking)\n");
119 ceph_flush_dirty_caps(fsc->mdsc);
120 dout("sync_fs (non-blocking) done\n");
124 dout("sync_fs (blocking)\n");
125 ceph_osdc_sync(&fsc->client->osdc);
126 ceph_mdsc_sync(fsc->mdsc);
127 dout("sync_fs (blocking) done\n");
138 Opt_caps_wanted_delay_min,
139 Opt_caps_wanted_delay_max,
141 Opt_readdir_max_entries,
142 Opt_readdir_max_bytes,
150 /* string args above */
158 Opt_require_active_mds,
165 enum ceph_recover_session_mode {
166 ceph_recover_session_no,
167 ceph_recover_session_clean
170 static const struct constant_table ceph_param_recover[] = {
171 { "no", ceph_recover_session_no },
172 { "clean", ceph_recover_session_clean },
176 static const struct fs_parameter_spec ceph_mount_parameters[] = {
177 fsparam_flag_no ("acl", Opt_acl),
178 fsparam_flag_no ("asyncreaddir", Opt_asyncreaddir),
179 fsparam_s32 ("caps_max", Opt_caps_max),
180 fsparam_u32 ("caps_wanted_delay_max", Opt_caps_wanted_delay_max),
181 fsparam_u32 ("caps_wanted_delay_min", Opt_caps_wanted_delay_min),
182 fsparam_u32 ("write_congestion_kb", Opt_congestion_kb),
183 fsparam_flag_no ("copyfrom", Opt_copyfrom),
184 fsparam_flag_no ("dcache", Opt_dcache),
185 fsparam_flag_no ("dirstat", Opt_dirstat),
186 fsparam_flag_no ("fsc", Opt_fscache), // fsc|nofsc
187 fsparam_string ("fsc", Opt_fscache), // fsc=...
188 fsparam_flag_no ("ino32", Opt_ino32),
189 fsparam_string ("mds_namespace", Opt_mds_namespace),
190 fsparam_flag_no ("poolperm", Opt_poolperm),
191 fsparam_flag_no ("quotadf", Opt_quotadf),
192 fsparam_u32 ("rasize", Opt_rasize),
193 fsparam_flag_no ("rbytes", Opt_rbytes),
194 fsparam_u32 ("readdir_max_bytes", Opt_readdir_max_bytes),
195 fsparam_u32 ("readdir_max_entries", Opt_readdir_max_entries),
196 fsparam_enum ("recover_session", Opt_recover_session, ceph_param_recover),
197 fsparam_flag_no ("require_active_mds", Opt_require_active_mds),
198 fsparam_u32 ("rsize", Opt_rsize),
199 fsparam_string ("snapdirname", Opt_snapdirname),
200 fsparam_string ("source", Opt_source),
201 fsparam_string ("mon_addr", Opt_mon_addr),
202 fsparam_u32 ("wsize", Opt_wsize),
203 fsparam_flag_no ("wsync", Opt_wsync),
207 struct ceph_parse_opts_ctx {
208 struct ceph_options *copts;
209 struct ceph_mount_options *opts;
213 * Remove adjacent slashes and then the trailing slash, unless it is
214 * the only remaining character.
216 * E.g. "//dir1////dir2///" --> "/dir1/dir2", "///" --> "/".
218 static void canonicalize_path(char *path)
222 for (i = 0; path[i] != '\0'; i++) {
223 if (path[i] != '/' || j < 1 || path[j - 1] != '/')
227 if (j > 1 && path[j - 1] == '/')
233 * Check if the mds namespace in ceph_mount_options matches
234 * the passed in namespace string. First time match (when
235 * ->mds_namespace is NULL) is treated specially, since
236 * ->mds_namespace needs to be initialized by the caller.
238 static int namespace_equals(struct ceph_mount_options *fsopt,
239 const char *namespace, size_t len)
241 return !(fsopt->mds_namespace &&
242 (strlen(fsopt->mds_namespace) != len ||
243 strncmp(fsopt->mds_namespace, namespace, len)));
246 static int ceph_parse_old_source(const char *dev_name, const char *dev_name_end,
247 struct fs_context *fc)
250 struct ceph_parse_opts_ctx *pctx = fc->fs_private;
251 struct ceph_mount_options *fsopt = pctx->opts;
253 if (*dev_name_end != ':')
254 return invalfc(fc, "separator ':' missing in source");
256 r = ceph_parse_mon_ips(dev_name, dev_name_end - dev_name,
257 pctx->copts, fc->log.log, ',');
261 fsopt->new_dev_syntax = false;
265 static int ceph_parse_new_source(const char *dev_name, const char *dev_name_end,
266 struct fs_context *fc)
269 struct ceph_fsid fsid;
270 struct ceph_parse_opts_ctx *pctx = fc->fs_private;
271 struct ceph_mount_options *fsopt = pctx->opts;
272 char *fsid_start, *fs_name_start;
274 if (*dev_name_end != '=') {
275 dout("separator '=' missing in source");
279 fsid_start = strchr(dev_name, '@');
281 return invalfc(fc, "missing cluster fsid");
282 ++fsid_start; /* start of cluster fsid */
284 fs_name_start = strchr(fsid_start, '.');
286 return invalfc(fc, "missing file system name");
288 if (ceph_parse_fsid(fsid_start, &fsid))
289 return invalfc(fc, "Invalid FSID");
291 ++fs_name_start; /* start of file system name */
292 len = dev_name_end - fs_name_start;
294 if (!namespace_equals(fsopt, fs_name_start, len))
295 return invalfc(fc, "Mismatching mds_namespace");
296 kfree(fsopt->mds_namespace);
297 fsopt->mds_namespace = kstrndup(fs_name_start, len, GFP_KERNEL);
298 if (!fsopt->mds_namespace)
300 dout("file system (mds namespace) '%s'\n", fsopt->mds_namespace);
302 fsopt->new_dev_syntax = true;
307 * Parse the source parameter for new device format. Distinguish the device
308 * spec from the path. Try parsing new device format and fallback to old
311 * New device syntax will looks like:
312 * <device_spec>=/<path>
314 * <device_spec> is name@fsid.fsname
315 * <path> is optional, but if present must begin with '/'
316 * (monitor addresses are passed via mount option)
318 * Old device syntax is:
319 * <server_spec>[,<server_spec>...]:[<path>]
321 * <server_spec> is <ip>[:<port>]
322 * <path> is optional, but if present must begin with '/'
324 static int ceph_parse_source(struct fs_parameter *param, struct fs_context *fc)
326 struct ceph_parse_opts_ctx *pctx = fc->fs_private;
327 struct ceph_mount_options *fsopt = pctx->opts;
328 char *dev_name = param->string, *dev_name_end;
331 dout("%s '%s'\n", __func__, dev_name);
332 if (!dev_name || !*dev_name)
333 return invalfc(fc, "Empty source");
335 dev_name_end = strchr(dev_name, '/');
338 * The server_path will include the whole chars from userland
339 * including the leading '/'.
341 kfree(fsopt->server_path);
342 fsopt->server_path = kstrdup(dev_name_end, GFP_KERNEL);
343 if (!fsopt->server_path)
346 canonicalize_path(fsopt->server_path);
348 dev_name_end = dev_name + strlen(dev_name);
351 dev_name_end--; /* back up to separator */
352 if (dev_name_end < dev_name)
353 return invalfc(fc, "Path missing in source");
355 dout("device name '%.*s'\n", (int)(dev_name_end - dev_name), dev_name);
356 if (fsopt->server_path)
357 dout("server path '%s'\n", fsopt->server_path);
359 dout("trying new device syntax");
360 ret = ceph_parse_new_source(dev_name, dev_name_end, fc);
364 dout("trying old device syntax");
365 ret = ceph_parse_old_source(dev_name, dev_name_end, fc);
370 fc->source = param->string;
371 param->string = NULL;
375 static int ceph_parse_mon_addr(struct fs_parameter *param,
376 struct fs_context *fc)
378 struct ceph_parse_opts_ctx *pctx = fc->fs_private;
379 struct ceph_mount_options *fsopt = pctx->opts;
381 kfree(fsopt->mon_addr);
382 fsopt->mon_addr = param->string;
383 param->string = NULL;
385 return ceph_parse_mon_ips(fsopt->mon_addr, strlen(fsopt->mon_addr),
386 pctx->copts, fc->log.log, '/');
389 static int ceph_parse_mount_param(struct fs_context *fc,
390 struct fs_parameter *param)
392 struct ceph_parse_opts_ctx *pctx = fc->fs_private;
393 struct ceph_mount_options *fsopt = pctx->opts;
394 struct fs_parse_result result;
398 ret = ceph_parse_param(param, pctx->copts, fc->log.log);
399 if (ret != -ENOPARAM)
402 token = fs_parse(fc, ceph_mount_parameters, param, &result);
403 dout("%s fs_parse '%s' token %d\n", __func__, param->key, token);
408 case Opt_snapdirname:
409 kfree(fsopt->snapdir_name);
410 fsopt->snapdir_name = param->string;
411 param->string = NULL;
413 case Opt_mds_namespace:
414 if (!namespace_equals(fsopt, param->string, strlen(param->string)))
415 return invalfc(fc, "Mismatching mds_namespace");
416 kfree(fsopt->mds_namespace);
417 fsopt->mds_namespace = param->string;
418 param->string = NULL;
420 case Opt_recover_session:
421 mode = result.uint_32;
422 if (mode == ceph_recover_session_no)
423 fsopt->flags &= ~CEPH_MOUNT_OPT_CLEANRECOVER;
424 else if (mode == ceph_recover_session_clean)
425 fsopt->flags |= CEPH_MOUNT_OPT_CLEANRECOVER;
431 return invalfc(fc, "Multiple sources specified");
432 return ceph_parse_source(param, fc);
434 return ceph_parse_mon_addr(param, fc);
436 if (result.uint_32 < PAGE_SIZE ||
437 result.uint_32 > CEPH_MAX_WRITE_SIZE)
439 fsopt->wsize = ALIGN(result.uint_32, PAGE_SIZE);
442 if (result.uint_32 < PAGE_SIZE ||
443 result.uint_32 > CEPH_MAX_READ_SIZE)
445 fsopt->rsize = ALIGN(result.uint_32, PAGE_SIZE);
448 fsopt->rasize = ALIGN(result.uint_32, PAGE_SIZE);
450 case Opt_caps_wanted_delay_min:
451 if (result.uint_32 < 1)
453 fsopt->caps_wanted_delay_min = result.uint_32;
455 case Opt_caps_wanted_delay_max:
456 if (result.uint_32 < 1)
458 fsopt->caps_wanted_delay_max = result.uint_32;
461 if (result.int_32 < 0)
463 fsopt->caps_max = result.int_32;
465 case Opt_readdir_max_entries:
466 if (result.uint_32 < 1)
468 fsopt->max_readdir = result.uint_32;
470 case Opt_readdir_max_bytes:
471 if (result.uint_32 < PAGE_SIZE && result.uint_32 != 0)
473 fsopt->max_readdir_bytes = result.uint_32;
475 case Opt_congestion_kb:
476 if (result.uint_32 < 1024) /* at least 1M */
478 fsopt->congestion_kb = result.uint_32;
482 fsopt->flags |= CEPH_MOUNT_OPT_DIRSTAT;
484 fsopt->flags &= ~CEPH_MOUNT_OPT_DIRSTAT;
488 fsopt->flags |= CEPH_MOUNT_OPT_RBYTES;
490 fsopt->flags &= ~CEPH_MOUNT_OPT_RBYTES;
492 case Opt_asyncreaddir:
494 fsopt->flags &= ~CEPH_MOUNT_OPT_NOASYNCREADDIR;
496 fsopt->flags |= CEPH_MOUNT_OPT_NOASYNCREADDIR;
500 fsopt->flags |= CEPH_MOUNT_OPT_DCACHE;
502 fsopt->flags &= ~CEPH_MOUNT_OPT_DCACHE;
506 fsopt->flags |= CEPH_MOUNT_OPT_INO32;
508 fsopt->flags &= ~CEPH_MOUNT_OPT_INO32;
512 #ifdef CONFIG_CEPH_FSCACHE
513 kfree(fsopt->fscache_uniq);
514 fsopt->fscache_uniq = NULL;
515 if (result.negated) {
516 fsopt->flags &= ~CEPH_MOUNT_OPT_FSCACHE;
518 fsopt->flags |= CEPH_MOUNT_OPT_FSCACHE;
519 fsopt->fscache_uniq = param->string;
520 param->string = NULL;
524 return invalfc(fc, "fscache support is disabled");
528 fsopt->flags &= ~CEPH_MOUNT_OPT_NOPOOLPERM;
530 fsopt->flags |= CEPH_MOUNT_OPT_NOPOOLPERM;
532 case Opt_require_active_mds:
534 fsopt->flags &= ~CEPH_MOUNT_OPT_MOUNTWAIT;
536 fsopt->flags |= CEPH_MOUNT_OPT_MOUNTWAIT;
540 fsopt->flags &= ~CEPH_MOUNT_OPT_NOQUOTADF;
542 fsopt->flags |= CEPH_MOUNT_OPT_NOQUOTADF;
546 fsopt->flags &= ~CEPH_MOUNT_OPT_NOCOPYFROM;
548 fsopt->flags |= CEPH_MOUNT_OPT_NOCOPYFROM;
551 if (!result.negated) {
552 #ifdef CONFIG_CEPH_FS_POSIX_ACL
553 fc->sb_flags |= SB_POSIXACL;
555 return invalfc(fc, "POSIX ACL support is disabled");
558 fc->sb_flags &= ~SB_POSIXACL;
563 fsopt->flags &= ~CEPH_MOUNT_OPT_ASYNC_DIROPS;
565 fsopt->flags |= CEPH_MOUNT_OPT_ASYNC_DIROPS;
573 return invalfc(fc, "%s out of range", param->key);
576 static void destroy_mount_options(struct ceph_mount_options *args)
578 dout("destroy_mount_options %p\n", args);
582 kfree(args->snapdir_name);
583 kfree(args->mds_namespace);
584 kfree(args->server_path);
585 kfree(args->fscache_uniq);
586 kfree(args->mon_addr);
590 static int strcmp_null(const char *s1, const char *s2)
598 return strcmp(s1, s2);
601 static int compare_mount_options(struct ceph_mount_options *new_fsopt,
602 struct ceph_options *new_opt,
603 struct ceph_fs_client *fsc)
605 struct ceph_mount_options *fsopt1 = new_fsopt;
606 struct ceph_mount_options *fsopt2 = fsc->mount_options;
607 int ofs = offsetof(struct ceph_mount_options, snapdir_name);
610 ret = memcmp(fsopt1, fsopt2, ofs);
614 ret = strcmp_null(fsopt1->snapdir_name, fsopt2->snapdir_name);
618 ret = strcmp_null(fsopt1->mds_namespace, fsopt2->mds_namespace);
622 ret = strcmp_null(fsopt1->server_path, fsopt2->server_path);
626 ret = strcmp_null(fsopt1->fscache_uniq, fsopt2->fscache_uniq);
630 ret = strcmp_null(fsopt1->mon_addr, fsopt2->mon_addr);
634 return ceph_compare_options(new_opt, fsc->client);
638 * ceph_show_options - Show mount options in /proc/mounts
639 * @m: seq_file to write to
640 * @root: root of that (sub)tree
642 static int ceph_show_options(struct seq_file *m, struct dentry *root)
644 struct ceph_fs_client *fsc = ceph_sb_to_client(root->d_sb);
645 struct ceph_mount_options *fsopt = fsc->mount_options;
649 /* a comma between MNT/MS and client options */
653 ret = ceph_print_client_options(m, fsc->client, false);
657 /* retract our comma if no client options */
661 if (fsopt->flags & CEPH_MOUNT_OPT_DIRSTAT)
662 seq_puts(m, ",dirstat");
663 if ((fsopt->flags & CEPH_MOUNT_OPT_RBYTES))
664 seq_puts(m, ",rbytes");
665 if (fsopt->flags & CEPH_MOUNT_OPT_NOASYNCREADDIR)
666 seq_puts(m, ",noasyncreaddir");
667 if ((fsopt->flags & CEPH_MOUNT_OPT_DCACHE) == 0)
668 seq_puts(m, ",nodcache");
669 if (fsopt->flags & CEPH_MOUNT_OPT_INO32)
670 seq_puts(m, ",ino32");
671 if (fsopt->flags & CEPH_MOUNT_OPT_FSCACHE) {
672 seq_show_option(m, "fsc", fsopt->fscache_uniq);
674 if (fsopt->flags & CEPH_MOUNT_OPT_NOPOOLPERM)
675 seq_puts(m, ",nopoolperm");
676 if (fsopt->flags & CEPH_MOUNT_OPT_NOQUOTADF)
677 seq_puts(m, ",noquotadf");
679 #ifdef CONFIG_CEPH_FS_POSIX_ACL
680 if (root->d_sb->s_flags & SB_POSIXACL)
683 seq_puts(m, ",noacl");
686 if ((fsopt->flags & CEPH_MOUNT_OPT_NOCOPYFROM) == 0)
687 seq_puts(m, ",copyfrom");
689 /* dump mds_namespace when old device syntax is in use */
690 if (fsopt->mds_namespace && !fsopt->new_dev_syntax)
691 seq_show_option(m, "mds_namespace", fsopt->mds_namespace);
694 seq_printf(m, ",mon_addr=%s", fsopt->mon_addr);
696 if (fsopt->flags & CEPH_MOUNT_OPT_CLEANRECOVER)
697 seq_show_option(m, "recover_session", "clean");
699 if (!(fsopt->flags & CEPH_MOUNT_OPT_ASYNC_DIROPS))
700 seq_puts(m, ",wsync");
702 if (fsopt->wsize != CEPH_MAX_WRITE_SIZE)
703 seq_printf(m, ",wsize=%u", fsopt->wsize);
704 if (fsopt->rsize != CEPH_MAX_READ_SIZE)
705 seq_printf(m, ",rsize=%u", fsopt->rsize);
706 if (fsopt->rasize != CEPH_RASIZE_DEFAULT)
707 seq_printf(m, ",rasize=%u", fsopt->rasize);
708 if (fsopt->congestion_kb != default_congestion_kb())
709 seq_printf(m, ",write_congestion_kb=%u", fsopt->congestion_kb);
711 seq_printf(m, ",caps_max=%d", fsopt->caps_max);
712 if (fsopt->caps_wanted_delay_min != CEPH_CAPS_WANTED_DELAY_MIN_DEFAULT)
713 seq_printf(m, ",caps_wanted_delay_min=%u",
714 fsopt->caps_wanted_delay_min);
715 if (fsopt->caps_wanted_delay_max != CEPH_CAPS_WANTED_DELAY_MAX_DEFAULT)
716 seq_printf(m, ",caps_wanted_delay_max=%u",
717 fsopt->caps_wanted_delay_max);
718 if (fsopt->max_readdir != CEPH_MAX_READDIR_DEFAULT)
719 seq_printf(m, ",readdir_max_entries=%u", fsopt->max_readdir);
720 if (fsopt->max_readdir_bytes != CEPH_MAX_READDIR_BYTES_DEFAULT)
721 seq_printf(m, ",readdir_max_bytes=%u", fsopt->max_readdir_bytes);
722 if (strcmp(fsopt->snapdir_name, CEPH_SNAPDIRNAME_DEFAULT))
723 seq_show_option(m, "snapdirname", fsopt->snapdir_name);
729 * handle any mon messages the standard library doesn't understand.
730 * return error if we don't either.
732 static int extra_mon_dispatch(struct ceph_client *client, struct ceph_msg *msg)
734 struct ceph_fs_client *fsc = client->private;
735 int type = le16_to_cpu(msg->hdr.type);
738 case CEPH_MSG_MDS_MAP:
739 ceph_mdsc_handle_mdsmap(fsc->mdsc, msg);
741 case CEPH_MSG_FS_MAP_USER:
742 ceph_mdsc_handle_fsmap(fsc->mdsc, msg);
750 * create a new fs client
752 * Success or not, this function consumes @fsopt and @opt.
754 static struct ceph_fs_client *create_fs_client(struct ceph_mount_options *fsopt,
755 struct ceph_options *opt)
757 struct ceph_fs_client *fsc;
760 fsc = kzalloc(sizeof(*fsc), GFP_KERNEL);
766 fsc->client = ceph_create_client(opt, fsc);
767 if (IS_ERR(fsc->client)) {
768 err = PTR_ERR(fsc->client);
771 opt = NULL; /* fsc->client now owns this */
773 fsc->client->extra_mon_dispatch = extra_mon_dispatch;
774 ceph_set_opt(fsc->client, ABORT_ON_FULL);
776 if (!fsopt->mds_namespace) {
777 ceph_monc_want_map(&fsc->client->monc, CEPH_SUB_MDSMAP,
780 ceph_monc_want_map(&fsc->client->monc, CEPH_SUB_FSMAP,
784 fsc->mount_options = fsopt;
787 fsc->mount_state = CEPH_MOUNT_MOUNTING;
789 fsc->have_copy_from2 = true;
791 atomic_long_set(&fsc->writeback_count, 0);
795 * The number of concurrent works can be high but they don't need
796 * to be processed in parallel, limit concurrency.
798 fsc->inode_wq = alloc_workqueue("ceph-inode", WQ_UNBOUND, 0);
801 fsc->cap_wq = alloc_workqueue("ceph-cap", 0, 1);
805 spin_lock(&ceph_fsc_lock);
806 list_add_tail(&fsc->metric_wakeup, &ceph_fsc_list);
807 spin_unlock(&ceph_fsc_lock);
812 destroy_workqueue(fsc->inode_wq);
814 ceph_destroy_client(fsc->client);
818 ceph_destroy_options(opt);
819 destroy_mount_options(fsopt);
823 static void flush_fs_workqueues(struct ceph_fs_client *fsc)
825 flush_workqueue(fsc->inode_wq);
826 flush_workqueue(fsc->cap_wq);
829 static void destroy_fs_client(struct ceph_fs_client *fsc)
831 dout("destroy_fs_client %p\n", fsc);
833 spin_lock(&ceph_fsc_lock);
834 list_del(&fsc->metric_wakeup);
835 spin_unlock(&ceph_fsc_lock);
837 ceph_mdsc_destroy(fsc);
838 destroy_workqueue(fsc->inode_wq);
839 destroy_workqueue(fsc->cap_wq);
841 destroy_mount_options(fsc->mount_options);
843 ceph_destroy_client(fsc->client);
846 dout("destroy_fs_client %p done\n", fsc);
852 struct kmem_cache *ceph_inode_cachep;
853 struct kmem_cache *ceph_cap_cachep;
854 struct kmem_cache *ceph_cap_flush_cachep;
855 struct kmem_cache *ceph_dentry_cachep;
856 struct kmem_cache *ceph_file_cachep;
857 struct kmem_cache *ceph_dir_file_cachep;
858 struct kmem_cache *ceph_mds_request_cachep;
859 mempool_t *ceph_wb_pagevec_pool;
861 static void ceph_inode_init_once(void *foo)
863 struct ceph_inode_info *ci = foo;
864 inode_init_once(&ci->vfs_inode);
867 static int __init init_caches(void)
871 ceph_inode_cachep = kmem_cache_create("ceph_inode_info",
872 sizeof(struct ceph_inode_info),
873 __alignof__(struct ceph_inode_info),
874 SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD|
875 SLAB_ACCOUNT, ceph_inode_init_once);
876 if (!ceph_inode_cachep)
879 ceph_cap_cachep = KMEM_CACHE(ceph_cap, SLAB_MEM_SPREAD);
880 if (!ceph_cap_cachep)
882 ceph_cap_flush_cachep = KMEM_CACHE(ceph_cap_flush,
883 SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD);
884 if (!ceph_cap_flush_cachep)
887 ceph_dentry_cachep = KMEM_CACHE(ceph_dentry_info,
888 SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD);
889 if (!ceph_dentry_cachep)
892 ceph_file_cachep = KMEM_CACHE(ceph_file_info, SLAB_MEM_SPREAD);
893 if (!ceph_file_cachep)
896 ceph_dir_file_cachep = KMEM_CACHE(ceph_dir_file_info, SLAB_MEM_SPREAD);
897 if (!ceph_dir_file_cachep)
900 ceph_mds_request_cachep = KMEM_CACHE(ceph_mds_request, SLAB_MEM_SPREAD);
901 if (!ceph_mds_request_cachep)
904 ceph_wb_pagevec_pool = mempool_create_kmalloc_pool(10, CEPH_MAX_WRITE_SIZE >> PAGE_SHIFT);
905 if (!ceph_wb_pagevec_pool)
906 goto bad_pagevec_pool;
908 error = ceph_fscache_register();
915 kmem_cache_destroy(ceph_mds_request_cachep);
917 mempool_destroy(ceph_wb_pagevec_pool);
919 kmem_cache_destroy(ceph_dir_file_cachep);
921 kmem_cache_destroy(ceph_file_cachep);
923 kmem_cache_destroy(ceph_dentry_cachep);
925 kmem_cache_destroy(ceph_cap_flush_cachep);
927 kmem_cache_destroy(ceph_cap_cachep);
929 kmem_cache_destroy(ceph_inode_cachep);
933 static void destroy_caches(void)
936 * Make sure all delayed rcu free inodes are flushed before we
941 kmem_cache_destroy(ceph_inode_cachep);
942 kmem_cache_destroy(ceph_cap_cachep);
943 kmem_cache_destroy(ceph_cap_flush_cachep);
944 kmem_cache_destroy(ceph_dentry_cachep);
945 kmem_cache_destroy(ceph_file_cachep);
946 kmem_cache_destroy(ceph_dir_file_cachep);
947 kmem_cache_destroy(ceph_mds_request_cachep);
948 mempool_destroy(ceph_wb_pagevec_pool);
950 ceph_fscache_unregister();
953 static void __ceph_umount_begin(struct ceph_fs_client *fsc)
955 ceph_osdc_abort_requests(&fsc->client->osdc, -EIO);
956 ceph_mdsc_force_umount(fsc->mdsc);
957 fsc->filp_gen++; // invalidate open files
961 * ceph_umount_begin - initiate forced umount. Tear down the
962 * mount, skipping steps that may hang while waiting for server(s).
964 void ceph_umount_begin(struct super_block *sb)
966 struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
968 dout("ceph_umount_begin - starting forced umount\n");
971 fsc->mount_state = CEPH_MOUNT_SHUTDOWN;
972 __ceph_umount_begin(fsc);
975 static const struct super_operations ceph_super_ops = {
976 .alloc_inode = ceph_alloc_inode,
977 .free_inode = ceph_free_inode,
978 .write_inode = ceph_write_inode,
979 .drop_inode = generic_delete_inode,
980 .evict_inode = ceph_evict_inode,
981 .sync_fs = ceph_sync_fs,
982 .put_super = ceph_put_super,
983 .show_options = ceph_show_options,
984 .statfs = ceph_statfs,
985 .umount_begin = ceph_umount_begin,
989 * Bootstrap mount by opening the root directory. Note the mount
990 * @started time from caller, and time out if this takes too long.
992 static struct dentry *open_root_dentry(struct ceph_fs_client *fsc,
994 unsigned long started)
996 struct ceph_mds_client *mdsc = fsc->mdsc;
997 struct ceph_mds_request *req = NULL;
1002 dout("open_root_inode opening '%s'\n", path);
1003 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS);
1005 return ERR_CAST(req);
1006 req->r_path1 = kstrdup(path, GFP_NOFS);
1007 if (!req->r_path1) {
1008 root = ERR_PTR(-ENOMEM);
1012 req->r_ino1.ino = CEPH_INO_ROOT;
1013 req->r_ino1.snap = CEPH_NOSNAP;
1014 req->r_started = started;
1015 req->r_timeout = fsc->client->options->mount_timeout;
1016 req->r_args.getattr.mask = cpu_to_le32(CEPH_STAT_CAP_INODE);
1017 req->r_num_caps = 2;
1018 err = ceph_mdsc_do_request(mdsc, NULL, req);
1020 struct inode *inode = req->r_target_inode;
1021 req->r_target_inode = NULL;
1022 dout("open_root_inode success\n");
1023 root = d_make_root(inode);
1025 root = ERR_PTR(-ENOMEM);
1028 dout("open_root_inode success, root dentry is %p\n", root);
1030 root = ERR_PTR(err);
1033 ceph_mdsc_put_request(req);
1038 * mount: join the ceph cluster, and open root directory.
1040 static struct dentry *ceph_real_mount(struct ceph_fs_client *fsc,
1041 struct fs_context *fc)
1044 unsigned long started = jiffies; /* note the start time */
1045 struct dentry *root;
1047 dout("mount start %p\n", fsc);
1048 mutex_lock(&fsc->client->mount_mutex);
1050 if (!fsc->sb->s_root) {
1051 const char *path = fsc->mount_options->server_path ?
1052 fsc->mount_options->server_path + 1 : "";
1054 err = __ceph_open_session(fsc->client, started);
1059 if (fsc->mount_options->flags & CEPH_MOUNT_OPT_FSCACHE) {
1060 err = ceph_fscache_register_fs(fsc, fc);
1065 dout("mount opening path '%s'\n", path);
1067 ceph_fs_debugfs_init(fsc);
1069 root = open_root_dentry(fsc, path, started);
1071 err = PTR_ERR(root);
1074 fsc->sb->s_root = dget(root);
1076 root = dget(fsc->sb->s_root);
1079 fsc->mount_state = CEPH_MOUNT_MOUNTED;
1080 dout("mount success\n");
1081 mutex_unlock(&fsc->client->mount_mutex);
1085 mutex_unlock(&fsc->client->mount_mutex);
1086 return ERR_PTR(err);
1089 static int ceph_set_super(struct super_block *s, struct fs_context *fc)
1091 struct ceph_fs_client *fsc = s->s_fs_info;
1094 dout("set_super %p\n", s);
1096 s->s_maxbytes = MAX_LFS_FILESIZE;
1098 s->s_xattr = ceph_xattr_handlers;
1100 fsc->max_file_size = 1ULL << 40; /* temp value until we get mdsmap */
1102 s->s_op = &ceph_super_ops;
1103 s->s_d_op = &ceph_dentry_ops;
1104 s->s_export_op = &ceph_export_ops;
1108 s->s_time_max = U32_MAX;
1110 ret = set_anon_super_fc(s, fc);
1117 * share superblock if same fs AND options
1119 static int ceph_compare_super(struct super_block *sb, struct fs_context *fc)
1121 struct ceph_fs_client *new = fc->s_fs_info;
1122 struct ceph_mount_options *fsopt = new->mount_options;
1123 struct ceph_options *opt = new->client->options;
1124 struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
1126 dout("ceph_compare_super %p\n", sb);
1128 if (compare_mount_options(fsopt, opt, fsc)) {
1129 dout("monitor(s)/mount options don't match\n");
1132 if ((opt->flags & CEPH_OPT_FSID) &&
1133 ceph_fsid_compare(&opt->fsid, &fsc->client->fsid)) {
1134 dout("fsid doesn't match\n");
1137 if (fc->sb_flags != (sb->s_flags & ~SB_BORN)) {
1138 dout("flags differ\n");
1142 if (fsc->blocklisted && !ceph_test_mount_opt(fsc, CLEANRECOVER)) {
1143 dout("client is blocklisted (and CLEANRECOVER is not set)\n");
1147 if (fsc->mount_state == CEPH_MOUNT_SHUTDOWN) {
1148 dout("client has been forcibly unmounted\n");
1156 * construct our own bdi so we can control readahead, etc.
1158 static atomic_long_t bdi_seq = ATOMIC_LONG_INIT(0);
1160 static int ceph_setup_bdi(struct super_block *sb, struct ceph_fs_client *fsc)
1164 err = super_setup_bdi_name(sb, "ceph-%ld",
1165 atomic_long_inc_return(&bdi_seq));
1169 /* set ra_pages based on rasize mount option? */
1170 sb->s_bdi->ra_pages = fsc->mount_options->rasize >> PAGE_SHIFT;
1172 /* set io_pages based on max osd read size */
1173 sb->s_bdi->io_pages = fsc->mount_options->rsize >> PAGE_SHIFT;
1178 static int ceph_get_tree(struct fs_context *fc)
1180 struct ceph_parse_opts_ctx *pctx = fc->fs_private;
1181 struct ceph_mount_options *fsopt = pctx->opts;
1182 struct super_block *sb;
1183 struct ceph_fs_client *fsc;
1185 int (*compare_super)(struct super_block *, struct fs_context *) =
1189 dout("ceph_get_tree\n");
1192 return invalfc(fc, "No source");
1193 if (fsopt->new_dev_syntax && !fsopt->mon_addr)
1194 return invalfc(fc, "No monitor address");
1196 /* create client (which we may/may not use) */
1197 fsc = create_fs_client(pctx->opts, pctx->copts);
1205 err = ceph_mdsc_init(fsc);
1209 if (ceph_test_opt(fsc->client, NOSHARE))
1210 compare_super = NULL;
1212 fc->s_fs_info = fsc;
1213 sb = sget_fc(fc, compare_super, ceph_set_super);
1214 fc->s_fs_info = NULL;
1220 if (ceph_sb_to_client(sb) != fsc) {
1221 destroy_fs_client(fsc);
1222 fsc = ceph_sb_to_client(sb);
1223 dout("get_sb got existing client %p\n", fsc);
1225 dout("get_sb using new client %p\n", fsc);
1226 err = ceph_setup_bdi(sb, fsc);
1231 res = ceph_real_mount(fsc, fc);
1236 dout("root %p inode %p ino %llx.%llx\n", res,
1237 d_inode(res), ceph_vinop(d_inode(res)));
1238 fc->root = fsc->sb->s_root;
1242 if (!ceph_mdsmap_is_cluster_available(fsc->mdsc->mdsmap)) {
1243 pr_info("No mds server is up or the cluster is laggy\n");
1244 err = -EHOSTUNREACH;
1247 ceph_mdsc_close_sessions(fsc->mdsc);
1248 deactivate_locked_super(sb);
1252 destroy_fs_client(fsc);
1254 dout("ceph_get_tree fail %d\n", err);
1258 static void ceph_free_fc(struct fs_context *fc)
1260 struct ceph_parse_opts_ctx *pctx = fc->fs_private;
1263 destroy_mount_options(pctx->opts);
1264 ceph_destroy_options(pctx->copts);
1269 static int ceph_reconfigure_fc(struct fs_context *fc)
1271 struct ceph_parse_opts_ctx *pctx = fc->fs_private;
1272 struct ceph_mount_options *fsopt = pctx->opts;
1273 struct ceph_fs_client *fsc = ceph_sb_to_client(fc->root->d_sb);
1275 if (fsopt->flags & CEPH_MOUNT_OPT_ASYNC_DIROPS)
1276 ceph_set_mount_opt(fsc, ASYNC_DIROPS);
1278 ceph_clear_mount_opt(fsc, ASYNC_DIROPS);
1280 sync_filesystem(fc->root->d_sb);
1284 static const struct fs_context_operations ceph_context_ops = {
1285 .free = ceph_free_fc,
1286 .parse_param = ceph_parse_mount_param,
1287 .get_tree = ceph_get_tree,
1288 .reconfigure = ceph_reconfigure_fc,
1292 * Set up the filesystem mount context.
1294 static int ceph_init_fs_context(struct fs_context *fc)
1296 struct ceph_parse_opts_ctx *pctx;
1297 struct ceph_mount_options *fsopt;
1299 pctx = kzalloc(sizeof(*pctx), GFP_KERNEL);
1303 pctx->copts = ceph_alloc_options();
1307 pctx->opts = kzalloc(sizeof(*pctx->opts), GFP_KERNEL);
1312 fsopt->flags = CEPH_MOUNT_OPT_DEFAULT;
1314 fsopt->wsize = CEPH_MAX_WRITE_SIZE;
1315 fsopt->rsize = CEPH_MAX_READ_SIZE;
1316 fsopt->rasize = CEPH_RASIZE_DEFAULT;
1317 fsopt->snapdir_name = kstrdup(CEPH_SNAPDIRNAME_DEFAULT, GFP_KERNEL);
1318 if (!fsopt->snapdir_name)
1321 fsopt->caps_wanted_delay_min = CEPH_CAPS_WANTED_DELAY_MIN_DEFAULT;
1322 fsopt->caps_wanted_delay_max = CEPH_CAPS_WANTED_DELAY_MAX_DEFAULT;
1323 fsopt->max_readdir = CEPH_MAX_READDIR_DEFAULT;
1324 fsopt->max_readdir_bytes = CEPH_MAX_READDIR_BYTES_DEFAULT;
1325 fsopt->congestion_kb = default_congestion_kb();
1327 #ifdef CONFIG_CEPH_FS_POSIX_ACL
1328 fc->sb_flags |= SB_POSIXACL;
1331 fc->fs_private = pctx;
1332 fc->ops = &ceph_context_ops;
1336 destroy_mount_options(pctx->opts);
1337 ceph_destroy_options(pctx->copts);
1342 static void ceph_kill_sb(struct super_block *s)
1344 struct ceph_fs_client *fsc = ceph_sb_to_client(s);
1346 dout("kill_sb %p\n", s);
1348 ceph_mdsc_pre_umount(fsc->mdsc);
1349 flush_fs_workqueues(fsc);
1353 fsc->client->extra_mon_dispatch = NULL;
1354 ceph_fs_debugfs_cleanup(fsc);
1356 ceph_fscache_unregister_fs(fsc);
1358 destroy_fs_client(fsc);
1361 static struct file_system_type ceph_fs_type = {
1362 .owner = THIS_MODULE,
1364 .init_fs_context = ceph_init_fs_context,
1365 .kill_sb = ceph_kill_sb,
1366 .fs_flags = FS_RENAME_DOES_D_MOVE,
1368 MODULE_ALIAS_FS("ceph");
1370 int ceph_force_reconnect(struct super_block *sb)
1372 struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
1375 fsc->mount_state = CEPH_MOUNT_RECOVER;
1376 __ceph_umount_begin(fsc);
1378 /* Make sure all page caches get invalidated.
1379 * see remove_session_caps_cb() */
1380 flush_workqueue(fsc->inode_wq);
1382 /* In case that we were blocklisted. This also reset
1383 * all mon/osd connections */
1384 ceph_reset_client_addr(fsc->client);
1386 ceph_osdc_clear_abort_err(&fsc->client->osdc);
1388 fsc->blocklisted = false;
1389 fsc->mount_state = CEPH_MOUNT_MOUNTED;
1392 err = __ceph_do_getattr(d_inode(sb->s_root), NULL,
1393 CEPH_STAT_CAP_INODE, true);
1398 static int __init init_ceph(void)
1400 int ret = init_caches();
1405 ret = register_filesystem(&ceph_fs_type);
1409 pr_info("loaded (mds proto %d)\n", CEPH_MDSC_PROTOCOL);
1419 static void __exit exit_ceph(void)
1421 dout("exit_ceph\n");
1422 unregister_filesystem(&ceph_fs_type);
1426 static int param_set_metrics(const char *val, const struct kernel_param *kp)
1428 struct ceph_fs_client *fsc;
1431 ret = param_set_bool(val, kp);
1433 pr_err("Failed to parse sending metrics switch value '%s'\n",
1436 } else if (!disable_send_metrics) {
1437 // wake up all the mds clients
1438 spin_lock(&ceph_fsc_lock);
1439 list_for_each_entry(fsc, &ceph_fsc_list, metric_wakeup) {
1440 metric_schedule_delayed(&fsc->mdsc->metric);
1442 spin_unlock(&ceph_fsc_lock);
1448 static const struct kernel_param_ops param_ops_metrics = {
1449 .set = param_set_metrics,
1450 .get = param_get_bool,
1453 bool disable_send_metrics = false;
1454 module_param_cb(disable_send_metrics, ¶m_ops_metrics, &disable_send_metrics, 0644);
1455 MODULE_PARM_DESC(disable_send_metrics, "Enable sending perf metrics to ceph cluster (default: on)");
1457 module_init(init_ceph);
1458 module_exit(exit_ceph);
1460 MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
1461 MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
1462 MODULE_AUTHOR("Patience Warnick <patience@newdream.net>");
1463 MODULE_DESCRIPTION("Ceph filesystem for Linux");
1464 MODULE_LICENSE("GPL");