]> git.proxmox.com Git - ceph.git/blame - ceph/src/ceph_fuse.cc
buildsys: switch source download to quincy
[ceph.git] / ceph / src / ceph_fuse.cc
CommitLineData
7c673cae
FG
1// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2// vim: ts=8 sw=2 smarttab
3/*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 *
13 */
14
15#include <sys/stat.h>
16#include <sys/utsname.h>
17#include <iostream>
18#include <string>
f67539c2 19#include <optional>
7c673cae 20
f67539c2 21#include "common/async/context_pool.h"
7c673cae
FG
22#include "common/config.h"
23#include "common/errno.h"
24
25#include "client/Client.h"
26#include "client/fuse_ll.h"
27
28#include "msg/Messenger.h"
29
30#include "mon/MonClient.h"
31
32#include "common/Timer.h"
33#include "common/ceph_argparse.h"
34#if defined(__linux__)
35#include "common/linux_version.h"
36#endif
37#include "global/global_init.h"
38#include "global/signal_handler.h"
39#include "common/Preforker.h"
40#include "common/safe_io.h"
41
42#include <sys/types.h>
43#include <fcntl.h>
44
e306af50 45#include "include/ceph_fuse.h"
1911f103 46#include <fuse_lowlevel.h>
7c673cae
FG
47
48#define dout_context g_ceph_context
49
f67539c2
TL
50ceph::async::io_context_pool icp;
51
7c673cae
FG
52static void fuse_usage()
53{
54 const char* argv[] = {
55 "ceph-fuse",
56 "-h",
57 };
58 struct fuse_args args = FUSE_ARGS_INIT(2, (char**)argv);
1911f103
TL
59#if FUSE_VERSION >= FUSE_MAKE_VERSION(3, 0)
60 struct fuse_cmdline_opts opts = {};
f67539c2
TL
61 if (fuse_parse_cmdline(&args, &opts) != -1) {
62 if (opts.show_help) {
63 cout << "usage: " << argv[0] << " [options] <mountpoint>\n\n";
64 cout << "FUSE options:\n";
65 fuse_cmdline_help();
66 fuse_lowlevel_help();
67 cout << "\n";
68 }
69 } else {
1911f103 70#else
11fdf7f2 71 if (fuse_parse_cmdline(&args, nullptr, nullptr, nullptr) == -1) {
1911f103 72#endif
7c673cae
FG
73 derr << "fuse_parse_cmdline failed." << dendl;
74 }
11fdf7f2 75 ceph_assert(args.allocated);
7c673cae
FG
76 fuse_opt_free_args(&args);
77}
78
79void usage()
80{
81 cout <<
c07f9fc5
FG
82"usage: ceph-fuse [-n client.username] [-m mon-ip-addr:mon-port] <mount point> [OPTIONS]\n"
83" --client_mountpoint/-r <sub_directory>\n"
84" use sub_directory as the mounted root, rather than the full Ceph tree.\n"
7c673cae
FG
85"\n";
86 fuse_usage();
87 generic_client_usage();
88}
89
90int main(int argc, const char **argv, const char *envp[]) {
91 int filer_flags = 0;
92 //cerr << "ceph-fuse starting " << myrank << "/" << world << std::endl;
93 std::vector<const char*> args;
94 argv_to_vec(argc, argv, args);
95 if (args.empty()) {
11fdf7f2
TL
96 cerr << argv[0] << ": -h or --help for usage" << std::endl;
97 exit(1);
98 }
99 if (ceph_argparse_need_usage(args)) {
7c673cae 100 usage();
11fdf7f2 101 exit(0);
7c673cae 102 }
7c673cae 103
11fdf7f2
TL
104 std::map<std::string,std::string> defaults = {
105 { "pid_file", "" },
106 { "chdir", "/" } // FUSE will chdir("/"); be ready.
107 };
7c673cae 108
11fdf7f2 109 auto cct = global_init(&defaults, args, CEPH_ENTITY_TYPE_CLIENT,
7c673cae
FG
110 CODE_ENVIRONMENT_DAEMON,
111 CINIT_FLAG_UNPRIVILEGED_DAEMON_DEFAULTS);
11fdf7f2
TL
112
113 for (auto i = args.begin(); i != args.end();) {
7c673cae
FG
114 if (ceph_argparse_double_dash(args, i)) {
115 break;
11fdf7f2 116 } else if (ceph_argparse_flag(args, i, "--localize-reads", (char*)nullptr)) {
7c673cae
FG
117 cerr << "setting CEPH_OSD_FLAG_LOCALIZE_READS" << std::endl;
118 filer_flags |= CEPH_OSD_FLAG_LOCALIZE_READS;
28e407b8
AA
119 } else if (ceph_argparse_flag(args, i, "-V", (char*)nullptr)) {
120 const char* tmpargv[] = {
121 "ceph-fuse",
122 "-V"
123 };
124
125 struct fuse_args fargs = FUSE_ARGS_INIT(2, (char**)tmpargv);
1911f103
TL
126#if FUSE_VERSION >= FUSE_MAKE_VERSION(3, 0)
127 struct fuse_cmdline_opts opts = {};
128 if (fuse_parse_cmdline(&fargs, &opts) == -1) {
129#else
28e407b8 130 if (fuse_parse_cmdline(&fargs, nullptr, nullptr, nullptr) == -1) {
1911f103 131#endif
28e407b8
AA
132 derr << "fuse_parse_cmdline failed." << dendl;
133 }
11fdf7f2 134 ceph_assert(fargs.allocated);
28e407b8
AA
135 fuse_opt_free_args(&fargs);
136 exit(0);
7c673cae
FG
137 } else {
138 ++i;
139 }
140 }
141
142 // args for fuse
143 const char **newargv;
144 int newargc;
145 vec_to_argv(argv[0], args, &newargc, &newargv);
146
7c673cae
FG
147 // check for 32-bit arch
148#ifndef __LP64__
149 cerr << std::endl;
150 cerr << "WARNING: Ceph inode numbers are 64 bits wide, and FUSE on 32-bit kernels does" << std::endl;
151 cerr << " not cope well with that situation. Expect to crash shortly." << std::endl;
152 cerr << std::endl;
153#endif
154
155 Preforker forker;
11fdf7f2
TL
156 auto daemonize = g_conf().get_val<bool>("daemonize");
157 if (daemonize) {
7c673cae
FG
158 global_init_prefork(g_ceph_context);
159 int r;
160 string err;
161 r = forker.prefork(err);
162 if (r < 0 || forker.is_parent()) {
163 // Start log if current process is about to exit. Otherwise, we hit an assert
164 // in the Ceph context destructor.
165 g_ceph_context->_log->start();
166 }
167 if (r < 0) {
168 cerr << "ceph-fuse " << err << std::endl;
169 return r;
170 }
171 if (forker.is_parent()) {
172 r = forker.parent_wait(err);
173 if (r < 0) {
174 cerr << "ceph-fuse " << err << std::endl;
175 }
176 return r;
177 }
178 global_init_postfork_start(cct.get());
179 }
180
181 {
182 common_init_finish(g_ceph_context);
adb31ebb 183
11fdf7f2
TL
184 init_async_signal_handler();
185 register_async_signal_handler(SIGHUP, sighup_handler);
7c673cae
FG
186
187 //cout << "child, mounting" << std::endl;
188 class RemountTest : public Thread {
189 public:
190 CephFuse *cfuse;
191 Client *client;
11fdf7f2 192 RemountTest() : cfuse(nullptr), client(nullptr) {}
7c673cae
FG
193 void init(CephFuse *cf, Client *cl) {
194 cfuse = cf;
195 client = cl;
196 }
197 ~RemountTest() override {}
198 void *entry() override {
199#if defined(__linux__)
200 int ver = get_linux_version();
11fdf7f2
TL
201 ceph_assert(ver != 0);
202 bool client_try_dentry_invalidate = g_conf().get_val<bool>(
b32b8144
FG
203 "client_try_dentry_invalidate");
204 bool can_invalidate_dentries =
205 client_try_dentry_invalidate && ver < KERNEL_VERSION(3, 18, 0);
7c673cae 206 int tr = client->test_dentry_handling(can_invalidate_dentries);
11fdf7f2 207 bool client_die_on_failed_dentry_invalidate = g_conf().get_val<bool>(
b32b8144
FG
208 "client_die_on_failed_dentry_invalidate");
209 if (tr != 0 && client_die_on_failed_dentry_invalidate) {
7c673cae
FG
210 cerr << "ceph-fuse[" << getpid()
211 << "]: fuse failed dentry invalidate/remount test with error "
212 << cpp_strerror(tr) << ", stopping" << std::endl;
213
214 char buf[5050];
215 string mountpoint = cfuse->get_mount_point();
216 snprintf(buf, sizeof(buf), "fusermount -u -z %s", mountpoint.c_str());
217 int umount_r = system(buf);
218 if (umount_r) {
219 if (umount_r != -1) {
220 if (WIFEXITED(umount_r)) {
221 umount_r = WEXITSTATUS(umount_r);
222 cerr << "got error " << umount_r
223 << " when unmounting Ceph on failed remount test!" << std::endl;
224 } else {
225 cerr << "attempt to umount on failed remount test failed (on a signal?)" << std::endl;
226 }
227 } else {
228 cerr << "system() invocation failed during remount test" << std::endl;
229 }
230 }
231 }
232 return reinterpret_cast<void*>(tr);
233#else
234 return reinterpret_cast<void*>(0);
235#endif
236 }
237 } tester;
238
239
240 // get monmap
11fdf7f2 241 Messenger *messenger = nullptr;
7c673cae
FG
242 StandaloneClient *client;
243 CephFuse *cfuse;
244 UserPerm perms;
245 int tester_r = 0;
11fdf7f2 246 void *tester_rp = nullptr;
7c673cae 247
f67539c2
TL
248 icp.start(cct->_conf.get_val<std::uint64_t>("client_asio_thread_count"));
249 MonClient *mc = new MonClient(g_ceph_context, icp);
7c673cae 250 int r = mc->build_initial_monmap();
11fdf7f2
TL
251 if (r == -EINVAL) {
252 cerr << "failed to generate initial mon list" << std::endl;
253 exit(1);
254 }
7c673cae
FG
255 if (r < 0)
256 goto out_mc_start_failed;
257
258 // start up network
259 messenger = Messenger::create_client_messenger(g_ceph_context, "client");
260 messenger->set_default_policy(Messenger::Policy::lossy_client(0));
261 messenger->set_policy(entity_name_t::TYPE_MDS,
262 Messenger::Policy::lossless_client(0));
263
f67539c2 264 client = new StandaloneClient(messenger, mc, icp);
7c673cae
FG
265 if (filer_flags) {
266 client->set_filer_flags(filer_flags);
267 }
268
269 cfuse = new CephFuse(client, forker.get_signal_fd());
270
271 r = cfuse->init(newargc, newargv);
272 if (r != 0) {
273 cerr << "ceph-fuse[" << getpid() << "]: fuse failed to initialize" << std::endl;
274 goto out_messenger_start_failed;
275 }
276
277 cerr << "ceph-fuse[" << getpid() << "]: starting ceph client" << std::endl;
278 r = messenger->start();
279 if (r < 0) {
280 cerr << "ceph-fuse[" << getpid() << "]: ceph messenger failed with " << cpp_strerror(-r) << std::endl;
281 goto out_messenger_start_failed;
282 }
283
7c673cae
FG
284 // start client
285 r = client->init();
286 if (r < 0) {
287 cerr << "ceph-fuse[" << getpid() << "]: ceph client failed with " << cpp_strerror(-r) << std::endl;
288 goto out_init_failed;
289 }
290
291 client->update_metadata("mount_point", cfuse->get_mount_point());
292 perms = client->pick_my_perms();
11fdf7f2
TL
293 {
294 // start up fuse
295 // use my argc, argv (make sure you pass a mount point!)
296 auto client_mountpoint = g_conf().get_val<std::string>(
297 "client_mountpoint");
298 auto mountpoint = client_mountpoint.c_str();
299 auto fuse_require_active_mds = g_conf().get_val<bool>(
300 "fuse_require_active_mds");
301 r = client->mount(mountpoint, perms, fuse_require_active_mds);
302 if (r < 0) {
303 if (r == CEPH_FUSE_NO_MDS_UP) {
304 cerr << "ceph-fuse[" << getpid() << "]: probably no MDS server is up?" << std::endl;
305 }
306 cerr << "ceph-fuse[" << getpid() << "]: ceph mount failed with " << cpp_strerror(-r) << std::endl;
307 r = EXIT_FAILURE;
308 goto out_shutdown;
28e407b8 309 }
7c673cae
FG
310 }
311
312 r = cfuse->start();
313 if (r != 0) {
314 cerr << "ceph-fuse[" << getpid() << "]: fuse failed to start" << std::endl;
315 goto out_client_unmount;
316 }
317
318 cerr << "ceph-fuse[" << getpid() << "]: starting fuse" << std::endl;
319 tester.init(cfuse, client);
320 tester.create("tester");
321 r = cfuse->loop();
322 tester.join(&tester_rp);
323 tester_r = static_cast<int>(reinterpret_cast<uint64_t>(tester_rp));
324 cerr << "ceph-fuse[" << getpid() << "]: fuse finished with error " << r
325 << " and tester_r " << tester_r <<std::endl;
11fdf7f2 326
7c673cae
FG
327 out_client_unmount:
328 client->unmount();
329 cfuse->finalize();
330 out_shutdown:
f67539c2 331 icp.stop();
7c673cae
FG
332 client->shutdown();
333 out_init_failed:
334 unregister_async_signal_handler(SIGHUP, sighup_handler);
335 shutdown_async_signal_handler();
336
337 // wait for messenger to finish
338 messenger->shutdown();
339 messenger->wait();
340 out_messenger_start_failed:
341 delete cfuse;
11fdf7f2 342 cfuse = nullptr;
7c673cae 343 delete client;
11fdf7f2 344 client = nullptr;
7c673cae 345 delete messenger;
11fdf7f2 346 messenger = nullptr;
7c673cae
FG
347 out_mc_start_failed:
348 free(newargv);
349 delete mc;
11fdf7f2 350 mc = nullptr;
7c673cae
FG
351 //cout << "child done" << std::endl;
352 return forker.signal_exit(r);
353 }
354}