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/parser.h>
13 #include <linux/sched.h>
14 #include <linux/seq_file.h>
15 #include <linux/slab.h>
16 #include <linux/statfs.h>
17 #include <linux/string.h>
20 #include "mds_client.h"
23 #include <linux/ceph/ceph_features.h>
24 #include <linux/ceph/decode.h>
25 #include <linux/ceph/mon_client.h>
26 #include <linux/ceph/auth.h>
27 #include <linux/ceph/debugfs.h>
30 * Ceph superblock operations
32 * Handle the basics of mounting, unmounting.
38 static void ceph_put_super(struct super_block *s)
40 struct ceph_fs_client *fsc = ceph_sb_to_client(s);
43 ceph_mdsc_close_sessions(fsc->mdsc);
46 static int ceph_statfs(struct dentry *dentry, struct kstatfs *buf)
48 struct ceph_fs_client *fsc = ceph_inode_to_client(d_inode(dentry));
49 struct ceph_mon_client *monc = &fsc->client->monc;
50 struct ceph_statfs st;
55 if (fsc->mdsc->mdsmap->m_num_data_pg_pools == 1) {
56 data_pool = fsc->mdsc->mdsmap->m_data_pg_pools[0];
58 data_pool = CEPH_NOPOOL;
62 err = ceph_monc_do_statfs(monc, data_pool, &st);
67 buf->f_type = CEPH_SUPER_MAGIC; /* ?? */
70 * express utilization in terms of large blocks to avoid
71 * overflow on 32-bit machines.
73 * NOTE: for the time being, we make bsize == frsize to humor
74 * not-yet-ancient versions of glibc that are broken.
75 * Someday, we will probably want to report a real block
76 * size... whatever that may mean for a network file system!
78 buf->f_bsize = 1 << CEPH_BLOCK_SHIFT;
79 buf->f_frsize = 1 << CEPH_BLOCK_SHIFT;
82 * By default use root quota for stats; fallback to overall filesystem
83 * usage if using 'noquotadf' mount option or if the root dir doesn't
84 * have max_bytes quota set.
86 if (ceph_test_mount_opt(fsc, NOQUOTADF) ||
87 !ceph_quota_update_statfs(fsc, buf)) {
88 buf->f_blocks = le64_to_cpu(st.kb) >> (CEPH_BLOCK_SHIFT-10);
89 buf->f_bfree = le64_to_cpu(st.kb_avail) >> (CEPH_BLOCK_SHIFT-10);
90 buf->f_bavail = le64_to_cpu(st.kb_avail) >> (CEPH_BLOCK_SHIFT-10);
93 buf->f_files = le64_to_cpu(st.num_objects);
95 buf->f_namelen = NAME_MAX;
97 /* Must convert the fsid, for consistent values across arches */
98 mutex_lock(&monc->mutex);
99 fsid = le64_to_cpu(*(__le64 *)(&monc->monmap->fsid)) ^
100 le64_to_cpu(*((__le64 *)&monc->monmap->fsid + 1));
101 mutex_unlock(&monc->mutex);
103 buf->f_fsid.val[0] = fsid & 0xffffffff;
104 buf->f_fsid.val[1] = fsid >> 32;
109 static int ceph_sync_fs(struct super_block *sb, int wait)
111 struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
114 dout("sync_fs (non-blocking)\n");
115 ceph_flush_dirty_caps(fsc->mdsc);
116 dout("sync_fs (non-blocking) done\n");
120 dout("sync_fs (blocking)\n");
121 ceph_osdc_sync(&fsc->client->osdc);
122 ceph_mdsc_sync(fsc->mdsc);
123 dout("sync_fs (blocking) done\n");
134 Opt_caps_wanted_delay_min,
135 Opt_caps_wanted_delay_max,
137 Opt_readdir_max_entries,
138 Opt_readdir_max_bytes,
147 /* string args above */
162 Opt_require_active_mds,
163 Opt_norequire_active_mds,
164 #ifdef CONFIG_CEPH_FS_POSIX_ACL
174 static match_table_t fsopt_tokens = {
175 {Opt_wsize, "wsize=%d"},
176 {Opt_rsize, "rsize=%d"},
177 {Opt_rasize, "rasize=%d"},
178 {Opt_caps_wanted_delay_min, "caps_wanted_delay_min=%d"},
179 {Opt_caps_wanted_delay_max, "caps_wanted_delay_max=%d"},
180 {Opt_caps_max, "caps_max=%d"},
181 {Opt_readdir_max_entries, "readdir_max_entries=%d"},
182 {Opt_readdir_max_bytes, "readdir_max_bytes=%d"},
183 {Opt_congestion_kb, "write_congestion_kb=%d"},
185 {Opt_snapdirname, "snapdirname=%s"},
186 {Opt_mds_namespace, "mds_namespace=%s"},
187 {Opt_recover_session, "recover_session=%s"},
188 {Opt_fscache_uniq, "fsc=%s"},
189 /* string args above */
190 {Opt_dirstat, "dirstat"},
191 {Opt_nodirstat, "nodirstat"},
192 {Opt_rbytes, "rbytes"},
193 {Opt_norbytes, "norbytes"},
194 {Opt_asyncreaddir, "asyncreaddir"},
195 {Opt_noasyncreaddir, "noasyncreaddir"},
196 {Opt_dcache, "dcache"},
197 {Opt_nodcache, "nodcache"},
198 {Opt_ino32, "ino32"},
199 {Opt_noino32, "noino32"},
200 {Opt_fscache, "fsc"},
201 {Opt_nofscache, "nofsc"},
202 {Opt_poolperm, "poolperm"},
203 {Opt_nopoolperm, "nopoolperm"},
204 {Opt_require_active_mds, "require_active_mds"},
205 {Opt_norequire_active_mds, "norequire_active_mds"},
206 #ifdef CONFIG_CEPH_FS_POSIX_ACL
209 {Opt_noacl, "noacl"},
210 {Opt_quotadf, "quotadf"},
211 {Opt_noquotadf, "noquotadf"},
212 {Opt_copyfrom, "copyfrom"},
213 {Opt_nocopyfrom, "nocopyfrom"},
218 * Remove adjacent slashes and then the trailing slash, unless it is
219 * the only remaining character.
221 * E.g. "//dir1////dir2///" --> "/dir1/dir2", "///" --> "/".
223 static void canonicalize_path(char *path)
227 for (i = 0; path[i] != '\0'; i++) {
228 if (path[i] != '/' || j < 1 || path[j - 1] != '/')
232 if (j > 1 && path[j - 1] == '/')
237 static int parse_fsopt_token(char *c, void *private)
239 struct ceph_mount_options *fsopt = private;
240 substring_t argstr[MAX_OPT_ARGS];
241 int token, intval, ret;
243 token = match_token((char *)c, fsopt_tokens, argstr);
247 if (token < Opt_last_int) {
248 ret = match_int(&argstr[0], &intval);
250 pr_err("bad option arg (not int) at '%s'\n", c);
253 dout("got int token %d val %d\n", token, intval);
254 } else if (token > Opt_last_int && token < Opt_last_string) {
255 dout("got string token %d val %s\n", token,
258 dout("got token %d\n", token);
262 case Opt_snapdirname:
263 kfree(fsopt->snapdir_name);
264 fsopt->snapdir_name = kstrndup(argstr[0].from,
265 argstr[0].to-argstr[0].from,
267 if (!fsopt->snapdir_name)
270 case Opt_mds_namespace:
271 kfree(fsopt->mds_namespace);
272 fsopt->mds_namespace = kstrndup(argstr[0].from,
273 argstr[0].to-argstr[0].from,
275 if (!fsopt->mds_namespace)
278 case Opt_recover_session:
279 if (!strncmp(argstr[0].from, "no",
280 argstr[0].to - argstr[0].from)) {
281 fsopt->flags &= ~CEPH_MOUNT_OPT_CLEANRECOVER;
282 } else if (!strncmp(argstr[0].from, "clean",
283 argstr[0].to - argstr[0].from)) {
284 fsopt->flags |= CEPH_MOUNT_OPT_CLEANRECOVER;
289 case Opt_fscache_uniq:
290 #ifdef CONFIG_CEPH_FSCACHE
291 kfree(fsopt->fscache_uniq);
292 fsopt->fscache_uniq = kstrndup(argstr[0].from,
293 argstr[0].to-argstr[0].from,
295 if (!fsopt->fscache_uniq)
297 fsopt->flags |= CEPH_MOUNT_OPT_FSCACHE;
300 pr_err("fscache support is disabled\n");
304 if (intval < (int)PAGE_SIZE || intval > CEPH_MAX_WRITE_SIZE)
306 fsopt->wsize = ALIGN(intval, PAGE_SIZE);
309 if (intval < (int)PAGE_SIZE || intval > CEPH_MAX_READ_SIZE)
311 fsopt->rsize = ALIGN(intval, PAGE_SIZE);
316 fsopt->rasize = ALIGN(intval, PAGE_SIZE);
318 case Opt_caps_wanted_delay_min:
321 fsopt->caps_wanted_delay_min = intval;
323 case Opt_caps_wanted_delay_max:
326 fsopt->caps_wanted_delay_max = intval;
331 fsopt->caps_max = intval;
333 case Opt_readdir_max_entries:
336 fsopt->max_readdir = intval;
338 case Opt_readdir_max_bytes:
339 if (intval < (int)PAGE_SIZE && intval != 0)
341 fsopt->max_readdir_bytes = intval;
343 case Opt_congestion_kb:
344 if (intval < 1024) /* at least 1M */
346 fsopt->congestion_kb = intval;
349 fsopt->flags |= CEPH_MOUNT_OPT_DIRSTAT;
352 fsopt->flags &= ~CEPH_MOUNT_OPT_DIRSTAT;
355 fsopt->flags |= CEPH_MOUNT_OPT_RBYTES;
358 fsopt->flags &= ~CEPH_MOUNT_OPT_RBYTES;
360 case Opt_asyncreaddir:
361 fsopt->flags &= ~CEPH_MOUNT_OPT_NOASYNCREADDIR;
363 case Opt_noasyncreaddir:
364 fsopt->flags |= CEPH_MOUNT_OPT_NOASYNCREADDIR;
367 fsopt->flags |= CEPH_MOUNT_OPT_DCACHE;
370 fsopt->flags &= ~CEPH_MOUNT_OPT_DCACHE;
373 fsopt->flags |= CEPH_MOUNT_OPT_INO32;
376 fsopt->flags &= ~CEPH_MOUNT_OPT_INO32;
379 #ifdef CONFIG_CEPH_FSCACHE
380 fsopt->flags |= CEPH_MOUNT_OPT_FSCACHE;
381 kfree(fsopt->fscache_uniq);
382 fsopt->fscache_uniq = NULL;
385 pr_err("fscache support is disabled\n");
389 fsopt->flags &= ~CEPH_MOUNT_OPT_FSCACHE;
390 kfree(fsopt->fscache_uniq);
391 fsopt->fscache_uniq = NULL;
394 fsopt->flags &= ~CEPH_MOUNT_OPT_NOPOOLPERM;
397 fsopt->flags |= CEPH_MOUNT_OPT_NOPOOLPERM;
399 case Opt_require_active_mds:
400 fsopt->flags &= ~CEPH_MOUNT_OPT_MOUNTWAIT;
402 case Opt_norequire_active_mds:
403 fsopt->flags |= CEPH_MOUNT_OPT_MOUNTWAIT;
406 fsopt->flags &= ~CEPH_MOUNT_OPT_NOQUOTADF;
409 fsopt->flags |= CEPH_MOUNT_OPT_NOQUOTADF;
412 fsopt->flags &= ~CEPH_MOUNT_OPT_NOCOPYFROM;
415 fsopt->flags |= CEPH_MOUNT_OPT_NOCOPYFROM;
417 #ifdef CONFIG_CEPH_FS_POSIX_ACL
419 fsopt->sb_flags |= SB_POSIXACL;
423 fsopt->sb_flags &= ~SB_POSIXACL;
431 static void destroy_mount_options(struct ceph_mount_options *args)
433 dout("destroy_mount_options %p\n", args);
434 kfree(args->snapdir_name);
435 kfree(args->mds_namespace);
436 kfree(args->server_path);
437 kfree(args->fscache_uniq);
441 static int strcmp_null(const char *s1, const char *s2)
449 return strcmp(s1, s2);
452 static int compare_mount_options(struct ceph_mount_options *new_fsopt,
453 struct ceph_options *new_opt,
454 struct ceph_fs_client *fsc)
456 struct ceph_mount_options *fsopt1 = new_fsopt;
457 struct ceph_mount_options *fsopt2 = fsc->mount_options;
458 int ofs = offsetof(struct ceph_mount_options, snapdir_name);
461 ret = memcmp(fsopt1, fsopt2, ofs);
465 ret = strcmp_null(fsopt1->snapdir_name, fsopt2->snapdir_name);
469 ret = strcmp_null(fsopt1->mds_namespace, fsopt2->mds_namespace);
473 ret = strcmp_null(fsopt1->server_path, fsopt2->server_path);
477 ret = strcmp_null(fsopt1->fscache_uniq, fsopt2->fscache_uniq);
481 return ceph_compare_options(new_opt, fsc->client);
484 static int parse_mount_options(struct ceph_mount_options **pfsopt,
485 struct ceph_options **popt,
486 int flags, char *options,
487 const char *dev_name)
489 struct ceph_mount_options *fsopt;
490 const char *dev_name_end;
493 if (!dev_name || !*dev_name)
496 fsopt = kzalloc(sizeof(*fsopt), GFP_KERNEL);
500 dout("parse_mount_options %p, dev_name '%s'\n", fsopt, dev_name);
502 fsopt->sb_flags = flags;
503 fsopt->flags = CEPH_MOUNT_OPT_DEFAULT;
505 fsopt->wsize = CEPH_MAX_WRITE_SIZE;
506 fsopt->rsize = CEPH_MAX_READ_SIZE;
507 fsopt->rasize = CEPH_RASIZE_DEFAULT;
508 fsopt->snapdir_name = kstrdup(CEPH_SNAPDIRNAME_DEFAULT, GFP_KERNEL);
509 if (!fsopt->snapdir_name) {
514 fsopt->caps_wanted_delay_min = CEPH_CAPS_WANTED_DELAY_MIN_DEFAULT;
515 fsopt->caps_wanted_delay_max = CEPH_CAPS_WANTED_DELAY_MAX_DEFAULT;
516 fsopt->max_readdir = CEPH_MAX_READDIR_DEFAULT;
517 fsopt->max_readdir_bytes = CEPH_MAX_READDIR_BYTES_DEFAULT;
518 fsopt->congestion_kb = default_congestion_kb();
521 * Distinguish the server list from the path in "dev_name".
522 * Internally we do not include the leading '/' in the path.
524 * "dev_name" will look like:
525 * <server_spec>[,<server_spec>...]:[<path>]
527 * <server_spec> is <ip>[:<port>]
528 * <path> is optional, but if present must begin with '/'
530 dev_name_end = strchr(dev_name, '/');
533 * The server_path will include the whole chars from userland
534 * including the leading '/'.
536 fsopt->server_path = kstrdup(dev_name_end, GFP_KERNEL);
537 if (!fsopt->server_path) {
542 canonicalize_path(fsopt->server_path);
544 dev_name_end = dev_name + strlen(dev_name);
547 dev_name_end--; /* back up to ':' separator */
548 if (dev_name_end < dev_name || *dev_name_end != ':') {
549 pr_err("device name is missing path (no : separator in %s)\n",
553 dout("device name '%.*s'\n", (int)(dev_name_end - dev_name), dev_name);
554 if (fsopt->server_path)
555 dout("server path '%s'\n", fsopt->server_path);
557 *popt = ceph_parse_options(options, dev_name, dev_name_end,
558 parse_fsopt_token, (void *)fsopt);
560 err = PTR_ERR(*popt);
569 destroy_mount_options(fsopt);
574 * ceph_show_options - Show mount options in /proc/mounts
575 * @m: seq_file to write to
576 * @root: root of that (sub)tree
578 static int ceph_show_options(struct seq_file *m, struct dentry *root)
580 struct ceph_fs_client *fsc = ceph_sb_to_client(root->d_sb);
581 struct ceph_mount_options *fsopt = fsc->mount_options;
585 /* a comma between MNT/MS and client options */
589 ret = ceph_print_client_options(m, fsc->client, false);
593 /* retract our comma if no client options */
597 if (fsopt->flags & CEPH_MOUNT_OPT_DIRSTAT)
598 seq_puts(m, ",dirstat");
599 if ((fsopt->flags & CEPH_MOUNT_OPT_RBYTES))
600 seq_puts(m, ",rbytes");
601 if (fsopt->flags & CEPH_MOUNT_OPT_NOASYNCREADDIR)
602 seq_puts(m, ",noasyncreaddir");
603 if ((fsopt->flags & CEPH_MOUNT_OPT_DCACHE) == 0)
604 seq_puts(m, ",nodcache");
605 if (fsopt->flags & CEPH_MOUNT_OPT_INO32)
606 seq_puts(m, ",ino32");
607 if (fsopt->flags & CEPH_MOUNT_OPT_FSCACHE) {
608 seq_show_option(m, "fsc", fsopt->fscache_uniq);
610 if (fsopt->flags & CEPH_MOUNT_OPT_NOPOOLPERM)
611 seq_puts(m, ",nopoolperm");
612 if (fsopt->flags & CEPH_MOUNT_OPT_NOQUOTADF)
613 seq_puts(m, ",noquotadf");
615 #ifdef CONFIG_CEPH_FS_POSIX_ACL
616 if (fsopt->sb_flags & SB_POSIXACL)
619 seq_puts(m, ",noacl");
622 if ((fsopt->flags & CEPH_MOUNT_OPT_NOCOPYFROM) == 0)
623 seq_puts(m, ",copyfrom");
625 if (fsopt->mds_namespace)
626 seq_show_option(m, "mds_namespace", fsopt->mds_namespace);
628 if (fsopt->flags & CEPH_MOUNT_OPT_CLEANRECOVER)
629 seq_show_option(m, "recover_session", "clean");
631 if (fsopt->wsize != CEPH_MAX_WRITE_SIZE)
632 seq_printf(m, ",wsize=%d", fsopt->wsize);
633 if (fsopt->rsize != CEPH_MAX_READ_SIZE)
634 seq_printf(m, ",rsize=%d", fsopt->rsize);
635 if (fsopt->rasize != CEPH_RASIZE_DEFAULT)
636 seq_printf(m, ",rasize=%d", fsopt->rasize);
637 if (fsopt->congestion_kb != default_congestion_kb())
638 seq_printf(m, ",write_congestion_kb=%d", fsopt->congestion_kb);
640 seq_printf(m, ",caps_max=%d", fsopt->caps_max);
641 if (fsopt->caps_wanted_delay_min != CEPH_CAPS_WANTED_DELAY_MIN_DEFAULT)
642 seq_printf(m, ",caps_wanted_delay_min=%d",
643 fsopt->caps_wanted_delay_min);
644 if (fsopt->caps_wanted_delay_max != CEPH_CAPS_WANTED_DELAY_MAX_DEFAULT)
645 seq_printf(m, ",caps_wanted_delay_max=%d",
646 fsopt->caps_wanted_delay_max);
647 if (fsopt->max_readdir != CEPH_MAX_READDIR_DEFAULT)
648 seq_printf(m, ",readdir_max_entries=%d", fsopt->max_readdir);
649 if (fsopt->max_readdir_bytes != CEPH_MAX_READDIR_BYTES_DEFAULT)
650 seq_printf(m, ",readdir_max_bytes=%d", fsopt->max_readdir_bytes);
651 if (strcmp(fsopt->snapdir_name, CEPH_SNAPDIRNAME_DEFAULT))
652 seq_show_option(m, "snapdirname", fsopt->snapdir_name);
658 * handle any mon messages the standard library doesn't understand.
659 * return error if we don't either.
661 static int extra_mon_dispatch(struct ceph_client *client, struct ceph_msg *msg)
663 struct ceph_fs_client *fsc = client->private;
664 int type = le16_to_cpu(msg->hdr.type);
667 case CEPH_MSG_MDS_MAP:
668 ceph_mdsc_handle_mdsmap(fsc->mdsc, msg);
670 case CEPH_MSG_FS_MAP_USER:
671 ceph_mdsc_handle_fsmap(fsc->mdsc, msg);
679 * create a new fs client
681 * Success or not, this function consumes @fsopt and @opt.
683 static struct ceph_fs_client *create_fs_client(struct ceph_mount_options *fsopt,
684 struct ceph_options *opt)
686 struct ceph_fs_client *fsc;
691 fsc = kzalloc(sizeof(*fsc), GFP_KERNEL);
697 fsc->client = ceph_create_client(opt, fsc);
698 if (IS_ERR(fsc->client)) {
699 err = PTR_ERR(fsc->client);
702 opt = NULL; /* fsc->client now owns this */
704 fsc->client->extra_mon_dispatch = extra_mon_dispatch;
705 ceph_set_opt(fsc->client, ABORT_ON_FULL);
707 if (!fsopt->mds_namespace) {
708 ceph_monc_want_map(&fsc->client->monc, CEPH_SUB_MDSMAP,
711 ceph_monc_want_map(&fsc->client->monc, CEPH_SUB_FSMAP,
715 fsc->mount_options = fsopt;
718 fsc->mount_state = CEPH_MOUNT_MOUNTING;
721 atomic_long_set(&fsc->writeback_count, 0);
725 * The number of concurrent works can be high but they don't need
726 * to be processed in parallel, limit concurrency.
728 fsc->inode_wq = alloc_workqueue("ceph-inode", WQ_UNBOUND, 0);
731 fsc->cap_wq = alloc_workqueue("ceph-cap", 0, 1);
735 /* set up mempools */
737 page_count = fsc->mount_options->wsize >> PAGE_SHIFT;
738 size = sizeof (struct page *) * (page_count ? page_count : 1);
739 fsc->wb_pagevec_pool = mempool_create_kmalloc_pool(10, size);
740 if (!fsc->wb_pagevec_pool)
746 destroy_workqueue(fsc->cap_wq);
748 destroy_workqueue(fsc->inode_wq);
750 ceph_destroy_client(fsc->client);
754 ceph_destroy_options(opt);
755 destroy_mount_options(fsopt);
759 static void flush_fs_workqueues(struct ceph_fs_client *fsc)
761 flush_workqueue(fsc->inode_wq);
762 flush_workqueue(fsc->cap_wq);
765 static void destroy_fs_client(struct ceph_fs_client *fsc)
767 dout("destroy_fs_client %p\n", fsc);
769 ceph_mdsc_destroy(fsc);
770 destroy_workqueue(fsc->inode_wq);
771 destroy_workqueue(fsc->cap_wq);
773 mempool_destroy(fsc->wb_pagevec_pool);
775 destroy_mount_options(fsc->mount_options);
777 ceph_destroy_client(fsc->client);
780 dout("destroy_fs_client %p done\n", fsc);
786 struct kmem_cache *ceph_inode_cachep;
787 struct kmem_cache *ceph_cap_cachep;
788 struct kmem_cache *ceph_cap_flush_cachep;
789 struct kmem_cache *ceph_dentry_cachep;
790 struct kmem_cache *ceph_file_cachep;
791 struct kmem_cache *ceph_dir_file_cachep;
793 static void ceph_inode_init_once(void *foo)
795 struct ceph_inode_info *ci = foo;
796 inode_init_once(&ci->vfs_inode);
799 static int __init init_caches(void)
803 ceph_inode_cachep = kmem_cache_create("ceph_inode_info",
804 sizeof(struct ceph_inode_info),
805 __alignof__(struct ceph_inode_info),
806 SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD|
807 SLAB_ACCOUNT, ceph_inode_init_once);
808 if (!ceph_inode_cachep)
811 ceph_cap_cachep = KMEM_CACHE(ceph_cap, SLAB_MEM_SPREAD);
812 if (!ceph_cap_cachep)
814 ceph_cap_flush_cachep = KMEM_CACHE(ceph_cap_flush,
815 SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD);
816 if (!ceph_cap_flush_cachep)
819 ceph_dentry_cachep = KMEM_CACHE(ceph_dentry_info,
820 SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD);
821 if (!ceph_dentry_cachep)
824 ceph_file_cachep = KMEM_CACHE(ceph_file_info, SLAB_MEM_SPREAD);
825 if (!ceph_file_cachep)
828 ceph_dir_file_cachep = KMEM_CACHE(ceph_dir_file_info, SLAB_MEM_SPREAD);
829 if (!ceph_dir_file_cachep)
832 error = ceph_fscache_register();
839 kmem_cache_destroy(ceph_dir_file_cachep);
841 kmem_cache_destroy(ceph_file_cachep);
843 kmem_cache_destroy(ceph_dentry_cachep);
845 kmem_cache_destroy(ceph_cap_flush_cachep);
847 kmem_cache_destroy(ceph_cap_cachep);
849 kmem_cache_destroy(ceph_inode_cachep);
853 static void destroy_caches(void)
856 * Make sure all delayed rcu free inodes are flushed before we
861 kmem_cache_destroy(ceph_inode_cachep);
862 kmem_cache_destroy(ceph_cap_cachep);
863 kmem_cache_destroy(ceph_cap_flush_cachep);
864 kmem_cache_destroy(ceph_dentry_cachep);
865 kmem_cache_destroy(ceph_file_cachep);
866 kmem_cache_destroy(ceph_dir_file_cachep);
868 ceph_fscache_unregister();
872 * ceph_umount_begin - initiate forced umount. Tear down down the
873 * mount, skipping steps that may hang while waiting for server(s).
875 static void ceph_umount_begin(struct super_block *sb)
877 struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
879 dout("ceph_umount_begin - starting forced umount\n");
882 fsc->mount_state = CEPH_MOUNT_SHUTDOWN;
883 ceph_osdc_abort_requests(&fsc->client->osdc, -EIO);
884 ceph_mdsc_force_umount(fsc->mdsc);
885 fsc->filp_gen++; // invalidate open files
888 static int ceph_remount(struct super_block *sb, int *flags, char *data)
894 static const struct super_operations ceph_super_ops = {
895 .alloc_inode = ceph_alloc_inode,
896 .free_inode = ceph_free_inode,
897 .write_inode = ceph_write_inode,
898 .drop_inode = generic_delete_inode,
899 .evict_inode = ceph_evict_inode,
900 .sync_fs = ceph_sync_fs,
901 .put_super = ceph_put_super,
902 .remount_fs = ceph_remount,
903 .show_options = ceph_show_options,
904 .statfs = ceph_statfs,
905 .umount_begin = ceph_umount_begin,
909 * Bootstrap mount by opening the root directory. Note the mount
910 * @started time from caller, and time out if this takes too long.
912 static struct dentry *open_root_dentry(struct ceph_fs_client *fsc,
914 unsigned long started)
916 struct ceph_mds_client *mdsc = fsc->mdsc;
917 struct ceph_mds_request *req = NULL;
922 dout("open_root_inode opening '%s'\n", path);
923 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS);
925 return ERR_CAST(req);
926 req->r_path1 = kstrdup(path, GFP_NOFS);
928 root = ERR_PTR(-ENOMEM);
932 req->r_ino1.ino = CEPH_INO_ROOT;
933 req->r_ino1.snap = CEPH_NOSNAP;
934 req->r_started = started;
935 req->r_timeout = fsc->client->options->mount_timeout;
936 req->r_args.getattr.mask = cpu_to_le32(CEPH_STAT_CAP_INODE);
938 err = ceph_mdsc_do_request(mdsc, NULL, req);
940 struct inode *inode = req->r_target_inode;
941 req->r_target_inode = NULL;
942 dout("open_root_inode success\n");
943 root = d_make_root(inode);
945 root = ERR_PTR(-ENOMEM);
948 dout("open_root_inode success, root dentry is %p\n", root);
953 ceph_mdsc_put_request(req);
958 * mount: join the ceph cluster, and open root directory.
960 static struct dentry *ceph_real_mount(struct ceph_fs_client *fsc)
963 unsigned long started = jiffies; /* note the start time */
966 dout("mount start %p\n", fsc);
967 mutex_lock(&fsc->client->mount_mutex);
969 if (!fsc->sb->s_root) {
970 const char *path = fsc->mount_options->server_path ?
971 fsc->mount_options->server_path + 1 : "";
973 err = __ceph_open_session(fsc->client, started);
978 if (fsc->mount_options->flags & CEPH_MOUNT_OPT_FSCACHE) {
979 err = ceph_fscache_register_fs(fsc);
984 dout("mount opening path '%s'\n", path);
986 ceph_fs_debugfs_init(fsc);
988 root = open_root_dentry(fsc, path, started);
993 fsc->sb->s_root = dget(root);
995 root = dget(fsc->sb->s_root);
998 fsc->mount_state = CEPH_MOUNT_MOUNTED;
999 dout("mount success\n");
1000 mutex_unlock(&fsc->client->mount_mutex);
1004 mutex_unlock(&fsc->client->mount_mutex);
1005 return ERR_PTR(err);
1008 static int ceph_set_super(struct super_block *s, void *data)
1010 struct ceph_fs_client *fsc = data;
1013 dout("set_super %p data %p\n", s, data);
1015 s->s_flags = fsc->mount_options->sb_flags;
1016 s->s_maxbytes = MAX_LFS_FILESIZE;
1018 s->s_xattr = ceph_xattr_handlers;
1021 fsc->max_file_size = 1ULL << 40; /* temp value until we get mdsmap */
1023 s->s_op = &ceph_super_ops;
1024 s->s_d_op = &ceph_dentry_ops;
1025 s->s_export_op = &ceph_export_ops;
1029 s->s_time_max = U32_MAX;
1031 ret = set_anon_super(s, NULL); /* what is that second arg for? */
1038 s->s_fs_info = NULL;
1044 * share superblock if same fs AND options
1046 static int ceph_compare_super(struct super_block *sb, void *data)
1048 struct ceph_fs_client *new = data;
1049 struct ceph_mount_options *fsopt = new->mount_options;
1050 struct ceph_options *opt = new->client->options;
1051 struct ceph_fs_client *other = ceph_sb_to_client(sb);
1053 dout("ceph_compare_super %p\n", sb);
1055 if (compare_mount_options(fsopt, opt, other)) {
1056 dout("monitor(s)/mount options don't match\n");
1059 if ((opt->flags & CEPH_OPT_FSID) &&
1060 ceph_fsid_compare(&opt->fsid, &other->client->fsid)) {
1061 dout("fsid doesn't match\n");
1064 if (fsopt->sb_flags != other->mount_options->sb_flags) {
1065 dout("flags differ\n");
1072 * construct our own bdi so we can control readahead, etc.
1074 static atomic_long_t bdi_seq = ATOMIC_LONG_INIT(0);
1076 static int ceph_setup_bdi(struct super_block *sb, struct ceph_fs_client *fsc)
1080 err = super_setup_bdi_name(sb, "ceph-%ld",
1081 atomic_long_inc_return(&bdi_seq));
1085 /* set ra_pages based on rasize mount option? */
1086 sb->s_bdi->ra_pages = fsc->mount_options->rasize >> PAGE_SHIFT;
1088 /* set io_pages based on max osd read size */
1089 sb->s_bdi->io_pages = fsc->mount_options->rsize >> PAGE_SHIFT;
1094 static struct dentry *ceph_mount(struct file_system_type *fs_type,
1095 int flags, const char *dev_name, void *data)
1097 struct super_block *sb;
1098 struct ceph_fs_client *fsc;
1101 int (*compare_super)(struct super_block *, void *) = ceph_compare_super;
1102 struct ceph_mount_options *fsopt = NULL;
1103 struct ceph_options *opt = NULL;
1105 dout("ceph_mount\n");
1107 #ifdef CONFIG_CEPH_FS_POSIX_ACL
1108 flags |= SB_POSIXACL;
1110 err = parse_mount_options(&fsopt, &opt, flags, data, dev_name);
1116 /* create client (which we may/may not use) */
1117 fsc = create_fs_client(fsopt, opt);
1119 res = ERR_CAST(fsc);
1123 err = ceph_mdsc_init(fsc);
1129 if (ceph_test_opt(fsc->client, NOSHARE))
1130 compare_super = NULL;
1131 sb = sget(fs_type, compare_super, ceph_set_super, flags, fsc);
1137 if (ceph_sb_to_client(sb) != fsc) {
1138 destroy_fs_client(fsc);
1139 fsc = ceph_sb_to_client(sb);
1140 dout("get_sb got existing client %p\n", fsc);
1142 dout("get_sb using new client %p\n", fsc);
1143 err = ceph_setup_bdi(sb, fsc);
1150 res = ceph_real_mount(fsc);
1153 dout("root %p inode %p ino %llx.%llx\n", res,
1154 d_inode(res), ceph_vinop(d_inode(res)));
1158 if (!ceph_mdsmap_is_cluster_available(fsc->mdsc->mdsmap)) {
1159 pr_info("No mds server is up or the cluster is laggy\n");
1160 err = -EHOSTUNREACH;
1163 ceph_mdsc_close_sessions(fsc->mdsc);
1164 deactivate_locked_super(sb);
1168 destroy_fs_client(fsc);
1170 dout("ceph_mount fail %ld\n", PTR_ERR(res));
1174 static void ceph_kill_sb(struct super_block *s)
1176 struct ceph_fs_client *fsc = ceph_sb_to_client(s);
1178 dout("kill_sb %p\n", s);
1180 ceph_mdsc_pre_umount(fsc->mdsc);
1181 flush_fs_workqueues(fsc);
1184 * Though the kill_anon_super() will finally trigger the
1185 * sync_filesystem() anyway, we still need to do it here
1186 * and then bump the stage of shutdown to stop the work
1187 * queue as earlier as possible.
1191 fsc->mdsc->stopping = CEPH_MDSC_STOPPING_FLUSHED;
1195 fsc->client->extra_mon_dispatch = NULL;
1196 ceph_fs_debugfs_cleanup(fsc);
1198 ceph_fscache_unregister_fs(fsc);
1200 destroy_fs_client(fsc);
1203 static struct file_system_type ceph_fs_type = {
1204 .owner = THIS_MODULE,
1206 .mount = ceph_mount,
1207 .kill_sb = ceph_kill_sb,
1208 .fs_flags = FS_RENAME_DOES_D_MOVE,
1210 MODULE_ALIAS_FS("ceph");
1212 int ceph_force_reconnect(struct super_block *sb)
1214 struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
1217 ceph_umount_begin(sb);
1219 /* Make sure all page caches get invalidated.
1220 * see remove_session_caps_cb() */
1221 flush_workqueue(fsc->inode_wq);
1223 /* In case that we were blacklisted. This also reset
1224 * all mon/osd connections */
1225 ceph_reset_client_addr(fsc->client);
1227 ceph_osdc_clear_abort_err(&fsc->client->osdc);
1229 fsc->blacklisted = false;
1230 fsc->mount_state = CEPH_MOUNT_MOUNTED;
1233 err = __ceph_do_getattr(d_inode(sb->s_root), NULL,
1234 CEPH_STAT_CAP_INODE, true);
1239 static int __init init_ceph(void)
1241 int ret = init_caches();
1246 ret = register_filesystem(&ceph_fs_type);
1250 pr_info("loaded (mds proto %d)\n", CEPH_MDSC_PROTOCOL);
1260 static void __exit exit_ceph(void)
1262 dout("exit_ceph\n");
1263 unregister_filesystem(&ceph_fs_type);
1267 module_init(init_ceph);
1268 module_exit(exit_ceph);
1270 MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
1271 MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
1272 MODULE_AUTHOR("Patience Warnick <patience@newdream.net>");
1273 MODULE_DESCRIPTION("Ceph filesystem for Linux");
1274 MODULE_LICENSE("GPL");