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