]>
Commit | Line | Data |
---|---|---|
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 "include/ceph_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 | common_init_finish(g_ceph_context); | |
171 | ||
172 | init_async_signal_handler(); | |
173 | register_async_signal_handler(SIGHUP, sighup_handler); | |
174 | ||
175 | //cout << "child, mounting" << std::endl; | |
176 | class RemountTest : public Thread { | |
177 | public: | |
178 | CephFuse *cfuse; | |
179 | Client *client; | |
180 | RemountTest() : cfuse(nullptr), client(nullptr) {} | |
181 | void init(CephFuse *cf, Client *cl) { | |
182 | cfuse = cf; | |
183 | client = cl; | |
184 | } | |
185 | ~RemountTest() override {} | |
186 | void *entry() override { | |
187 | #if defined(__linux__) | |
188 | int ver = get_linux_version(); | |
189 | ceph_assert(ver != 0); | |
190 | bool client_try_dentry_invalidate = g_conf().get_val<bool>( | |
191 | "client_try_dentry_invalidate"); | |
192 | bool can_invalidate_dentries = | |
193 | client_try_dentry_invalidate && ver < KERNEL_VERSION(3, 18, 0); | |
194 | int tr = client->test_dentry_handling(can_invalidate_dentries); | |
195 | bool client_die_on_failed_dentry_invalidate = g_conf().get_val<bool>( | |
196 | "client_die_on_failed_dentry_invalidate"); | |
197 | if (tr != 0 && client_die_on_failed_dentry_invalidate) { | |
198 | cerr << "ceph-fuse[" << getpid() | |
199 | << "]: fuse failed dentry invalidate/remount test with error " | |
200 | << cpp_strerror(tr) << ", stopping" << std::endl; | |
201 | ||
202 | char buf[5050]; | |
203 | string mountpoint = cfuse->get_mount_point(); | |
204 | snprintf(buf, sizeof(buf), "fusermount -u -z %s", mountpoint.c_str()); | |
205 | int umount_r = system(buf); | |
206 | if (umount_r) { | |
207 | if (umount_r != -1) { | |
208 | if (WIFEXITED(umount_r)) { | |
209 | umount_r = WEXITSTATUS(umount_r); | |
210 | cerr << "got error " << umount_r | |
211 | << " when unmounting Ceph on failed remount test!" << std::endl; | |
212 | } else { | |
213 | cerr << "attempt to umount on failed remount test failed (on a signal?)" << std::endl; | |
214 | } | |
215 | } else { | |
216 | cerr << "system() invocation failed during remount test" << std::endl; | |
217 | } | |
218 | } | |
219 | } | |
220 | return reinterpret_cast<void*>(tr); | |
221 | #else | |
222 | return reinterpret_cast<void*>(0); | |
223 | #endif | |
224 | } | |
225 | } tester; | |
226 | ||
227 | ||
228 | // get monmap | |
229 | Messenger *messenger = nullptr; | |
230 | StandaloneClient *client; | |
231 | CephFuse *cfuse; | |
232 | UserPerm perms; | |
233 | int tester_r = 0; | |
234 | void *tester_rp = nullptr; | |
235 | ||
236 | MonClient *mc = new MonClient(g_ceph_context); | |
237 | int r = mc->build_initial_monmap(); | |
238 | if (r == -EINVAL) { | |
239 | cerr << "failed to generate initial mon list" << std::endl; | |
240 | exit(1); | |
241 | } | |
242 | if (r < 0) | |
243 | goto out_mc_start_failed; | |
244 | ||
245 | // start up network | |
246 | messenger = Messenger::create_client_messenger(g_ceph_context, "client"); | |
247 | messenger->set_default_policy(Messenger::Policy::lossy_client(0)); | |
248 | messenger->set_policy(entity_name_t::TYPE_MDS, | |
249 | Messenger::Policy::lossless_client(0)); | |
250 | ||
251 | client = new StandaloneClient(messenger, mc); | |
252 | if (filer_flags) { | |
253 | client->set_filer_flags(filer_flags); | |
254 | } | |
255 | ||
256 | cfuse = new CephFuse(client, forker.get_signal_fd()); | |
257 | ||
258 | r = cfuse->init(newargc, newargv); | |
259 | if (r != 0) { | |
260 | cerr << "ceph-fuse[" << getpid() << "]: fuse failed to initialize" << std::endl; | |
261 | goto out_messenger_start_failed; | |
262 | } | |
263 | ||
264 | cerr << "ceph-fuse[" << getpid() << "]: starting ceph client" << std::endl; | |
265 | r = messenger->start(); | |
266 | if (r < 0) { | |
267 | cerr << "ceph-fuse[" << getpid() << "]: ceph messenger failed with " << cpp_strerror(-r) << std::endl; | |
268 | goto out_messenger_start_failed; | |
269 | } | |
270 | ||
271 | // start client | |
272 | r = client->init(); | |
273 | if (r < 0) { | |
274 | cerr << "ceph-fuse[" << getpid() << "]: ceph client failed with " << cpp_strerror(-r) << std::endl; | |
275 | goto out_init_failed; | |
276 | } | |
277 | ||
278 | client->update_metadata("mount_point", cfuse->get_mount_point()); | |
279 | perms = client->pick_my_perms(); | |
280 | { | |
281 | // start up fuse | |
282 | // use my argc, argv (make sure you pass a mount point!) | |
283 | auto client_mountpoint = g_conf().get_val<std::string>( | |
284 | "client_mountpoint"); | |
285 | auto mountpoint = client_mountpoint.c_str(); | |
286 | auto fuse_require_active_mds = g_conf().get_val<bool>( | |
287 | "fuse_require_active_mds"); | |
288 | r = client->mount(mountpoint, perms, fuse_require_active_mds); | |
289 | if (r < 0) { | |
290 | if (r == CEPH_FUSE_NO_MDS_UP) { | |
291 | cerr << "ceph-fuse[" << getpid() << "]: probably no MDS server is up?" << std::endl; | |
292 | } | |
293 | cerr << "ceph-fuse[" << getpid() << "]: ceph mount failed with " << cpp_strerror(-r) << std::endl; | |
294 | r = EXIT_FAILURE; | |
295 | goto out_shutdown; | |
296 | } | |
297 | } | |
298 | ||
299 | r = cfuse->start(); | |
300 | if (r != 0) { | |
301 | cerr << "ceph-fuse[" << getpid() << "]: fuse failed to start" << std::endl; | |
302 | goto out_client_unmount; | |
303 | } | |
304 | ||
305 | cerr << "ceph-fuse[" << getpid() << "]: starting fuse" << std::endl; | |
306 | tester.init(cfuse, client); | |
307 | tester.create("tester"); | |
308 | r = cfuse->loop(); | |
309 | tester.join(&tester_rp); | |
310 | tester_r = static_cast<int>(reinterpret_cast<uint64_t>(tester_rp)); | |
311 | cerr << "ceph-fuse[" << getpid() << "]: fuse finished with error " << r | |
312 | << " and tester_r " << tester_r <<std::endl; | |
313 | ||
314 | out_client_unmount: | |
315 | client->unmount(); | |
316 | cfuse->finalize(); | |
317 | out_shutdown: | |
318 | client->shutdown(); | |
319 | out_init_failed: | |
320 | unregister_async_signal_handler(SIGHUP, sighup_handler); | |
321 | shutdown_async_signal_handler(); | |
322 | ||
323 | // wait for messenger to finish | |
324 | messenger->shutdown(); | |
325 | messenger->wait(); | |
326 | out_messenger_start_failed: | |
327 | delete cfuse; | |
328 | cfuse = nullptr; | |
329 | delete client; | |
330 | client = nullptr; | |
331 | delete messenger; | |
332 | messenger = nullptr; | |
333 | out_mc_start_failed: | |
334 | free(newargv); | |
335 | delete mc; | |
336 | mc = nullptr; | |
337 | //cout << "child done" << std::endl; | |
338 | return forker.signal_exit(r); | |
339 | } | |
340 | } |