2007-10-17 23:34:54 +00:00
|
|
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
|
|
|
|
// vim: ts=8 sw=2 smarttab
|
|
|
|
/*
|
|
|
|
* Ceph - scalable distributed file system
|
|
|
|
*
|
|
|
|
* Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
|
|
|
|
*
|
|
|
|
* This is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License version 2.1, as published by the Free Software
|
|
|
|
* Foundation. See file COPYING.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
using namespace std;
|
|
|
|
|
2011-02-22 16:38:45 +00:00
|
|
|
#include "common/config.h"
|
2011-09-21 17:47:47 +00:00
|
|
|
#include "common/errno.h"
|
2007-10-17 23:34:54 +00:00
|
|
|
|
|
|
|
#include "client/Client.h"
|
|
|
|
#include "client/fuse_ll.h"
|
|
|
|
|
2012-03-14 23:33:39 +00:00
|
|
|
#include "msg/Messenger.h"
|
2007-10-17 23:34:54 +00:00
|
|
|
|
2008-03-10 15:54:57 +00:00
|
|
|
#include "mon/MonClient.h"
|
|
|
|
|
2007-10-17 23:34:54 +00:00
|
|
|
#include "common/Timer.h"
|
2011-02-20 17:18:03 +00:00
|
|
|
#include "common/ceph_argparse.h"
|
2011-06-17 03:15:24 +00:00
|
|
|
#include "global/global_init.h"
|
2011-02-10 13:52:44 +00:00
|
|
|
#include "common/safe_io.h"
|
2007-10-17 23:34:54 +00:00
|
|
|
|
|
|
|
#ifndef DARWIN
|
|
|
|
#include <envz.h>
|
|
|
|
#endif // DARWIN
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
2011-01-15 00:57:33 +00:00
|
|
|
void usage()
|
|
|
|
{
|
2011-09-21 23:28:43 +00:00
|
|
|
cerr << "usage: ceph-fuse [-m mon-ip-addr:mon-port] <mount point>" << std::endl;
|
2011-01-15 00:57:33 +00:00
|
|
|
}
|
|
|
|
|
2008-01-01 22:04:31 +00:00
|
|
|
int main(int argc, const char **argv, const char *envp[]) {
|
2011-01-31 12:12:58 +00:00
|
|
|
int filer_flags = 0;
|
2011-09-21 23:28:43 +00:00
|
|
|
//cerr << "ceph-fuse starting " << myrank << "/" << world << std::endl;
|
2008-01-01 22:04:31 +00:00
|
|
|
vector<const char*> args;
|
2007-10-17 23:34:54 +00:00
|
|
|
argv_to_vec(argc, argv, args);
|
2008-11-05 22:54:13 +00:00
|
|
|
env_to_vec(args);
|
2010-07-01 16:25:43 +00:00
|
|
|
|
2012-03-12 20:15:50 +00:00
|
|
|
global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_DAEMON,
|
2011-03-14 14:24:32 +00:00
|
|
|
CINIT_FLAG_UNPRIVILEGED_DAEMON_DEFAULTS);
|
2011-09-06 22:16:09 +00:00
|
|
|
for (std::vector<const char*>::iterator i = args.begin(); i != args.end(); ) {
|
|
|
|
if (ceph_argparse_double_dash(args, i)) {
|
|
|
|
break;
|
|
|
|
} else if (ceph_argparse_flag(args, i, "--localize-reads", (char*)NULL)) {
|
2011-01-31 12:12:58 +00:00
|
|
|
cerr << "setting CEPH_OSD_FLAG_LOCALIZE_READS" << std::endl;
|
|
|
|
filer_flags |= CEPH_OSD_FLAG_LOCALIZE_READS;
|
2011-09-06 22:16:09 +00:00
|
|
|
} else {
|
|
|
|
++i;
|
2011-01-31 12:12:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-17 23:34:54 +00:00
|
|
|
// args for fuse
|
2012-11-09 19:32:44 +00:00
|
|
|
const char **newargv;
|
|
|
|
int newargc;
|
|
|
|
vec_to_argv(argv[0], args, &newargc, &newargv);
|
2007-10-17 23:34:54 +00:00
|
|
|
|
|
|
|
// FUSE will chdir("/"); be ready.
|
2011-09-09 16:32:47 +00:00
|
|
|
g_ceph_context->_conf->set_val("chdir", "/");
|
|
|
|
g_ceph_context->_conf->apply_changes(NULL);
|
2007-10-17 23:34:54 +00:00
|
|
|
|
2008-06-18 14:11:20 +00:00
|
|
|
// check for 32-bit arch
|
|
|
|
if (sizeof(long) == 4) {
|
|
|
|
cerr << std::endl;
|
|
|
|
cerr << "WARNING: Ceph inode numbers are 64 bits wide, and FUSE on 32-bit kernels does" << std::endl;
|
|
|
|
cerr << " not cope well with that situation. Expect to crash shortly." << std::endl;
|
|
|
|
cerr << std::endl;
|
|
|
|
}
|
|
|
|
|
2010-07-01 18:15:39 +00:00
|
|
|
// we need to handle the forking ourselves.
|
|
|
|
int fd[2] = {0, 0}; // parent's, child's
|
|
|
|
pid_t childpid = 0;
|
2012-04-03 23:33:05 +00:00
|
|
|
bool restart_log = false;
|
2011-06-07 17:38:54 +00:00
|
|
|
if (g_conf->daemonize) {
|
2010-07-01 18:15:39 +00:00
|
|
|
int r = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
|
|
|
|
if (r < 0) {
|
2011-09-21 17:47:47 +00:00
|
|
|
cerr << "ceph-fuse[" << getpid() << "]: unable to create socketpair: " << cpp_strerror(errno) << std::endl;
|
2010-07-01 18:15:39 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2010-05-26 17:01:49 +00:00
|
|
|
|
2012-03-27 17:41:12 +00:00
|
|
|
g_ceph_context->_log->stop();
|
2012-04-03 23:33:05 +00:00
|
|
|
restart_log = true;
|
2012-03-27 17:41:12 +00:00
|
|
|
|
2010-07-01 18:15:39 +00:00
|
|
|
childpid = fork();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (childpid == 0) {
|
2012-04-19 04:58:14 +00:00
|
|
|
common_init_finish(g_ceph_context);
|
|
|
|
|
2010-07-01 18:15:39 +00:00
|
|
|
//cout << "child, mounting" << std::endl;
|
|
|
|
::close(fd[0]);
|
|
|
|
|
2012-04-03 23:33:05 +00:00
|
|
|
if (restart_log)
|
|
|
|
g_ceph_context->_log->start();
|
2012-03-27 17:41:12 +00:00
|
|
|
|
2013-06-03 04:21:51 +00:00
|
|
|
// get monmap
|
2013-06-14 01:13:34 +00:00
|
|
|
Messenger *messenger = NULL;
|
2013-06-03 04:21:51 +00:00
|
|
|
Client *client;
|
|
|
|
CephFuse *cfuse;
|
|
|
|
|
|
|
|
MonClient mc(g_ceph_context);
|
|
|
|
int r = mc.build_initial_monmap();
|
|
|
|
if (r == -EINVAL)
|
|
|
|
usage();
|
|
|
|
if (r < 0)
|
|
|
|
goto out_mc_start_failed;
|
|
|
|
|
|
|
|
// start up network
|
|
|
|
messenger = Messenger::create(g_ceph_context,
|
|
|
|
entity_name_t::CLIENT(), "client",
|
|
|
|
getpid());
|
|
|
|
messenger->set_default_policy(Messenger::Policy::lossy_client(0, 0));
|
|
|
|
messenger->set_policy(entity_name_t::TYPE_MDS,
|
|
|
|
Messenger::Policy::lossless_client(0, 0));
|
|
|
|
|
|
|
|
client = new Client(messenger, &mc);
|
|
|
|
if (filer_flags) {
|
|
|
|
client->set_filer_flags(filer_flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
cfuse = new CephFuse(client, fd[1]);
|
2012-12-06 05:21:12 +00:00
|
|
|
|
2011-09-21 23:28:43 +00:00
|
|
|
cout << "ceph-fuse[" << getpid() << "]: starting ceph client" << std::endl;
|
2013-06-03 04:21:51 +00:00
|
|
|
r = messenger->start();
|
2012-09-24 16:55:25 +00:00
|
|
|
if (r < 0) {
|
|
|
|
cerr << "ceph-fuse[" << getpid() << "]: ceph mount failed with " << cpp_strerror(-r) << std::endl;
|
|
|
|
goto out_messenger_start_failed;
|
|
|
|
}
|
2010-07-01 18:15:39 +00:00
|
|
|
|
|
|
|
// start client
|
2012-09-24 16:55:25 +00:00
|
|
|
r = client->init();
|
|
|
|
if (r < 0) {
|
|
|
|
cerr << "ceph-fuse[" << getpid() << "]: ceph mount failed with " << cpp_strerror(-r) << std::endl;
|
|
|
|
goto out_init_failed;
|
|
|
|
}
|
2007-10-17 23:34:54 +00:00
|
|
|
|
2010-07-01 18:15:39 +00:00
|
|
|
// start up fuse
|
|
|
|
// use my argc, argv (make sure you pass a mount point!)
|
2012-09-24 16:55:25 +00:00
|
|
|
r = client->mount(g_conf->client_mountpoint.c_str());
|
2010-07-01 18:15:39 +00:00
|
|
|
if (r < 0) {
|
2011-09-21 17:47:47 +00:00
|
|
|
cerr << "ceph-fuse[" << getpid() << "]: ceph mount failed with " << cpp_strerror(-r) << std::endl;
|
2010-07-01 18:15:39 +00:00
|
|
|
goto out_shutdown;
|
|
|
|
}
|
2012-12-06 05:21:12 +00:00
|
|
|
|
2013-06-03 04:21:51 +00:00
|
|
|
r = cfuse->init(newargc, newargv);
|
2013-01-02 22:07:13 +00:00
|
|
|
if (r != 0) {
|
2012-12-06 05:21:12 +00:00
|
|
|
cerr << "ceph-fuse[" << getpid() << "]: fuse failed to initialize" << std::endl;
|
2013-01-02 22:07:13 +00:00
|
|
|
goto out_client_unmount;
|
2012-12-06 05:21:12 +00:00
|
|
|
}
|
2011-09-21 23:28:43 +00:00
|
|
|
cerr << "ceph-fuse[" << getpid() << "]: starting fuse" << std::endl;
|
2013-06-03 04:21:51 +00:00
|
|
|
r = cfuse->loop();
|
2011-09-21 23:28:43 +00:00
|
|
|
cerr << "ceph-fuse[" << getpid() << "]: fuse finished with error " << r << std::endl;
|
2012-12-06 05:21:12 +00:00
|
|
|
|
2013-01-02 22:07:13 +00:00
|
|
|
out_client_unmount:
|
2010-07-01 18:15:39 +00:00
|
|
|
client->unmount();
|
|
|
|
//cout << "unmounted" << std::endl;
|
2012-12-06 05:21:12 +00:00
|
|
|
|
2013-06-03 04:21:51 +00:00
|
|
|
cfuse->finalize();
|
|
|
|
delete cfuse;
|
2012-12-28 19:58:39 +00:00
|
|
|
|
2010-07-01 18:15:39 +00:00
|
|
|
out_shutdown:
|
|
|
|
client->shutdown();
|
2012-09-24 16:55:25 +00:00
|
|
|
out_init_failed:
|
2010-07-01 18:15:39 +00:00
|
|
|
// wait for messenger to finish
|
2012-09-24 20:31:15 +00:00
|
|
|
messenger->shutdown();
|
2010-07-01 18:15:39 +00:00
|
|
|
messenger->wait();
|
2012-09-24 16:55:25 +00:00
|
|
|
out_messenger_start_failed:
|
|
|
|
delete client;
|
2013-06-03 04:21:51 +00:00
|
|
|
out_mc_start_failed:
|
2012-11-09 17:52:08 +00:00
|
|
|
|
2011-06-07 17:38:54 +00:00
|
|
|
if (g_conf->daemonize) {
|
2010-07-01 18:15:39 +00:00
|
|
|
//cout << "child signalling parent with " << r << std::endl;
|
2011-06-07 23:14:26 +00:00
|
|
|
static int foo = 0;
|
|
|
|
foo += ::write(fd[1], &r, sizeof(r));
|
2010-07-01 18:15:39 +00:00
|
|
|
}
|
|
|
|
|
2012-11-09 17:52:08 +00:00
|
|
|
delete messenger;
|
|
|
|
g_ceph_context->put();
|
2012-11-09 19:32:44 +00:00
|
|
|
free(newargv);
|
2012-11-09 17:52:08 +00:00
|
|
|
|
2010-07-01 18:15:39 +00:00
|
|
|
//cout << "child done" << std::endl;
|
|
|
|
return r;
|
|
|
|
} else {
|
|
|
|
// i am the parent
|
|
|
|
//cout << "parent, waiting for signal" << std::endl;
|
|
|
|
::close(fd[1]);
|
|
|
|
|
|
|
|
int r = -1;
|
2011-02-12 06:57:29 +00:00
|
|
|
int err = safe_read_exact(fd[0], &r, sizeof(r));
|
|
|
|
if (err == 0 && r == 0) {
|
2010-07-01 18:15:39 +00:00
|
|
|
// close stdout, etc.
|
|
|
|
//cout << "success" << std::endl;
|
|
|
|
::close(0);
|
|
|
|
::close(1);
|
|
|
|
::close(2);
|
2011-09-21 17:47:47 +00:00
|
|
|
} else if (err) {
|
|
|
|
cerr << "ceph-fuse[" << getpid() << "]: mount failed: " << cpp_strerror(-err) << std::endl;
|
|
|
|
} else {
|
|
|
|
cerr << "ceph-fuse[" << getpid() << "]: mount failed: " << cpp_strerror(-r) << std::endl;
|
|
|
|
}
|
2010-07-01 18:15:39 +00:00
|
|
|
return r;
|
|
|
|
}
|
2007-10-17 23:34:54 +00:00
|
|
|
}
|
|
|
|
|