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