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