]> git.proxmox.com Git - ceph.git/blob - ceph/src/ceph_fuse.cc
115856f381cfd9cbfcd0bf7d8cee99b44677fce9
[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 auto test_result = client->test_dentry_handling(can_invalidate_dentries);
208 int tr = test_result.first;
209 bool abort_on_failure = test_result.second;
210 bool client_die_on_failed_dentry_invalidate = g_conf().get_val<bool>(
211 "client_die_on_failed_dentry_invalidate");
212 if (tr != 0 && client_die_on_failed_dentry_invalidate) {
213 cerr << "ceph-fuse[" << getpid()
214 << "]: fuse failed dentry invalidate/remount test with error "
215 << cpp_strerror(tr) << ", stopping" << std::endl;
216
217 char buf[5050];
218 string mountpoint = cfuse->get_mount_point();
219 snprintf(buf, sizeof(buf), "fusermount -u -z %s", mountpoint.c_str());
220 int umount_r = system(buf);
221 if (umount_r) {
222 if (umount_r != -1) {
223 if (WIFEXITED(umount_r)) {
224 umount_r = WEXITSTATUS(umount_r);
225 cerr << "got error " << umount_r
226 << " when unmounting Ceph on failed remount test!" << std::endl;
227 } else {
228 cerr << "attempt to umount on failed remount test failed (on a signal?)" << std::endl;
229 }
230 } else {
231 cerr << "system() invocation failed during remount test" << std::endl;
232 }
233 }
234 }
235 if(abort_on_failure) {
236 ceph_abort();
237 }
238 return reinterpret_cast<void*>(tr);
239 #else
240 return reinterpret_cast<void*>(0);
241 #endif
242 }
243 } tester;
244
245
246 // get monmap
247 Messenger *messenger = nullptr;
248 StandaloneClient *client;
249 CephFuse *cfuse;
250 UserPerm perms;
251 int tester_r = 0;
252 void *tester_rp = nullptr;
253
254 icp.start(cct->_conf.get_val<std::uint64_t>("client_asio_thread_count"));
255 MonClient *mc = new MonClient(g_ceph_context, icp);
256 int r = mc->build_initial_monmap();
257 if (r == -EINVAL) {
258 cerr << "failed to generate initial mon list" << std::endl;
259 exit(1);
260 }
261 if (r < 0)
262 goto out_mc_start_failed;
263
264 // start up network
265 messenger = Messenger::create_client_messenger(g_ceph_context, "client");
266 messenger->set_default_policy(Messenger::Policy::lossy_client(0));
267 messenger->set_policy(entity_name_t::TYPE_MDS,
268 Messenger::Policy::lossless_client(0));
269
270 client = new StandaloneClient(messenger, mc, icp);
271 if (filer_flags) {
272 client->set_filer_flags(filer_flags);
273 }
274
275 cfuse = new CephFuse(client, forker.get_signal_fd());
276
277 r = cfuse->init(newargc, newargv);
278 if (r != 0) {
279 cerr << "ceph-fuse[" << getpid() << "]: fuse failed to initialize" << std::endl;
280 goto out_messenger_start_failed;
281 }
282
283 cerr << "ceph-fuse[" << getpid() << "]: starting ceph client" << std::endl;
284 r = messenger->start();
285 if (r < 0) {
286 cerr << "ceph-fuse[" << getpid() << "]: ceph messenger failed with " << cpp_strerror(-r) << std::endl;
287 goto out_messenger_start_failed;
288 }
289
290 // start client
291 r = client->init();
292 if (r < 0) {
293 cerr << "ceph-fuse[" << getpid() << "]: ceph client failed with " << cpp_strerror(-r) << std::endl;
294 goto out_init_failed;
295 }
296
297 client->update_metadata("mount_point", cfuse->get_mount_point());
298 perms = client->pick_my_perms();
299 {
300 // start up fuse
301 // use my argc, argv (make sure you pass a mount point!)
302 auto client_mountpoint = g_conf().get_val<std::string>(
303 "client_mountpoint");
304 auto mountpoint = client_mountpoint.c_str();
305 auto fuse_require_active_mds = g_conf().get_val<bool>(
306 "fuse_require_active_mds");
307 r = client->mount(mountpoint, perms, fuse_require_active_mds);
308 if (r < 0) {
309 if (r == CEPH_FUSE_NO_MDS_UP) {
310 cerr << "ceph-fuse[" << getpid() << "]: probably no MDS server is up?" << std::endl;
311 }
312 cerr << "ceph-fuse[" << getpid() << "]: ceph mount failed with " << cpp_strerror(-r) << std::endl;
313 r = EXIT_FAILURE;
314 goto out_shutdown;
315 }
316 }
317
318 r = cfuse->start();
319 if (r != 0) {
320 cerr << "ceph-fuse[" << getpid() << "]: fuse failed to start" << std::endl;
321 goto out_client_unmount;
322 }
323
324 cerr << "ceph-fuse[" << getpid() << "]: starting fuse" << std::endl;
325 tester.init(cfuse, client);
326 tester.create("tester");
327 r = cfuse->loop();
328 tester.join(&tester_rp);
329 tester_r = static_cast<int>(reinterpret_cast<uint64_t>(tester_rp));
330 cerr << "ceph-fuse[" << getpid() << "]: fuse finished with error " << r
331 << " and tester_r " << tester_r <<std::endl;
332
333 out_client_unmount:
334 client->unmount();
335 cfuse->finalize();
336 out_shutdown:
337 icp.stop();
338 client->shutdown();
339 out_init_failed:
340 unregister_async_signal_handler(SIGHUP, sighup_handler);
341 shutdown_async_signal_handler();
342
343 // wait for messenger to finish
344 messenger->shutdown();
345 messenger->wait();
346 out_messenger_start_failed:
347 delete cfuse;
348 cfuse = nullptr;
349 delete client;
350 client = nullptr;
351 delete messenger;
352 messenger = nullptr;
353 out_mc_start_failed:
354 free(newargv);
355 delete mc;
356 mc = nullptr;
357 //cout << "child done" << std::endl;
358 return forker.signal_exit(r);
359 }
360 }