1
#include "client/libceph.h"
7
#include "common/Mutex.h"
8
#include "messages/MMonMap.h"
9
#include "common/common_init.h"
10
#include "msg/SimpleMessenger.h"
11
#include "client/Client.h"
13
/* ************* ************* ************* *************
17
extern "C" const char *ceph_version(int *pmajor, int *pminor, int *ppatch)
19
int major, minor, patch;
21
sscanf(VERSION, "%d.%d.%d", &major, &minor, &patch);
31
static Mutex ceph_client_mutex("ceph_client");
32
static int client_initialized = 0;
33
static int client_mount = 0;
34
static Client *client = NULL;
35
static MonClient *monclient = NULL;
36
static SimpleMessenger *messenger = NULL;
38
extern "C" int ceph_initialize(int argc, const char **argv)
40
ceph_client_mutex.Lock();
41
if (!client_initialized) {
42
//create everything to start a client
43
vector<const char*> args;
44
argv_to_vec(argc, argv, args);
45
common_set_defaults(false);
46
common_init(args, "libceph", true);
47
if (g_conf.clock_tare) g_clock.tare();
49
monclient = new MonClient();
50
if (monclient->build_initial_monmap() < 0) {
55
messenger = new SimpleMessenger();
56
messenger->register_entity(entity_name_t::CLIENT());
59
client = new Client(messenger, monclient);
66
ceph_client_mutex.Unlock();
70
extern "C" void ceph_deinitialize()
72
ceph_client_mutex.Lock();
74
if(!client_initialized) {
85
ceph_client_mutex.Unlock();
88
extern "C" int ceph_mount()
91
Mutex::Locker lock(ceph_client_mutex);
93
ret = client->mount();
101
extern "C" int ceph_umount()
103
Mutex::Locker lock(ceph_client_mutex);
106
return client->unmount();
110
extern "C" int ceph_statfs(const char *path, struct statvfs *stbuf)
112
return client->statfs(path, stbuf);
115
extern "C" int ceph_get_local_osd()
117
return client->get_local_osd();
120
extern "C" int ceph_getcwd(char *buf, int buflen)
124
int size = cwd.size()+1; //need space for null character
126
if (buflen == 0) return size;
129
size = cwd.copy(buf, size);
130
buf[size] = '\0'; //fill in null character
134
extern "C" int ceph_chdir (const char *s)
136
return client->chdir(s);
139
/*if we want to extern C this, we need to convert it to const char*,
140
which will mean storing it somewhere or else making the caller
141
responsible for delete-ing a c-string they didn't create*/
142
void ceph_getcwd(string& cwd)
147
extern "C" int ceph_opendir(const char *name, DIR **dirpp)
149
return client->opendir(name, dirpp);
152
extern "C" int ceph_closedir(DIR *dirp)
154
return client->closedir(dirp);
157
extern "C" int ceph_readdir_r(DIR *dirp, struct dirent *de)
159
return client->readdir_r(dirp, de);
162
extern "C" int ceph_readdirplus_r(DIR *dirp, struct dirent *de, struct stat *st, int *stmask)
164
return client->readdirplus_r(dirp, de, st, stmask);
167
extern "C" int ceph_getdents(DIR *dirp, char *buf, int buflen)
169
return client->getdents(dirp, buf, buflen);
172
extern "C" int ceph_getdnames(DIR *dirp, char *buf, int buflen)
174
return client->getdnames(dirp, buf, buflen);
177
extern "C" void ceph_rewinddir(DIR *dirp)
179
client->rewinddir(dirp);
182
extern "C" loff_t ceph_telldir(DIR *dirp)
184
return client->telldir(dirp);
187
extern "C" void ceph_seekdir(DIR *dirp, loff_t offset)
189
client->seekdir(dirp, offset);
192
extern "C" int ceph_link (const char *existing, const char *newname)
194
return client->link(existing, newname);
197
extern "C" int ceph_unlink (const char *path)
199
return client->unlink(path);
202
extern "C" int ceph_rename(const char *from, const char *to)
204
return client->rename(from, to);
208
extern "C" int ceph_mkdir(const char *path, mode_t mode)
210
return client->mkdir(path, mode);
213
extern "C" int ceph_mkdirs(const char *path, mode_t mode)
215
return client->mkdirs(path, mode);
218
extern "C" int ceph_rmdir(const char *path)
220
return client->rmdir(path);
224
extern "C" int ceph_readlink(const char *path, char *buf, loff_t size)
226
return client->readlink(path, buf, size);
229
extern "C" int ceph_symlink(const char *existing, const char *newname)
231
return client->symlink(existing, newname);
235
extern "C" int ceph_lstat(const char *path, struct stat *stbuf)
237
return client->lstat(path, stbuf);
240
extern "C" int ceph_lstat_precise(const char *path, stat_precise *stbuf)
242
return client->lstat_precise(path, (Client::stat_precise*)stbuf);
245
extern "C" int ceph_setattr(const char *relpath, struct stat *attr, int mask)
247
Client::stat_precise p_attr = Client::stat_precise(*attr);
248
return client->setattr(relpath, &p_attr, mask);
251
extern "C" int ceph_setattr_precise(const char *relpath,
252
struct stat_precise *attr, int mask) {
253
return client->setattr(relpath, (Client::stat_precise*)attr, mask);
256
extern "C" int ceph_chmod(const char *path, mode_t mode)
258
return client->chmod(path, mode);
260
extern "C" int ceph_chown(const char *path, uid_t uid, gid_t gid)
262
return client->chown(path, uid, gid);
265
extern "C" int ceph_utime(const char *path, struct utimbuf *buf)
267
return client->utime(path, buf);
270
extern "C" int ceph_truncate(const char *path, loff_t size)
272
return client->truncate(path, size);
276
extern "C" int ceph_mknod(const char *path, mode_t mode, dev_t rdev)
278
return client->mknod(path, mode, rdev);
281
extern "C" int ceph_open(const char *path, int flags, mode_t mode)
283
return client->open(path, flags, mode);
286
extern "C" int ceph_close(int fd)
288
return client->close(fd);
291
extern "C" loff_t ceph_lseek(int fd, loff_t offset, int whence)
293
return client->lseek(fd, offset, whence);
296
extern "C" int ceph_read(int fd, char *buf, loff_t size, loff_t offset)
298
return client->read(fd, buf, size, offset);
301
extern "C" int ceph_write(int fd, const char *buf, loff_t size, loff_t offset)
303
return client->write(fd, buf, size, offset);
306
extern "C" int ceph_ftruncate(int fd, loff_t size)
308
return client->ftruncate(fd, size);
311
extern "C" int ceph_fsync(int fd, bool syncdataonly)
313
return client->fsync(fd, syncdataonly);
316
extern "C" int ceph_fstat(int fd, struct stat *stbuf)
318
return client->fstat(fd, stbuf);
321
extern "C" int ceph_sync_fs()
323
return client->sync_fs();
326
extern "C" int ceph_get_file_stripe_unit(int fh)
328
return client->get_file_stripe_unit(fh);
331
extern "C" int ceph_get_file_replication(const char *path) {
332
int fd = client->open(path, O_RDONLY);
333
int rep = client->get_file_replication(fd);
338
extern "C" int ceph_get_default_preferred_pg(int fd)
340
return client->get_default_preferred_pg(fd);
343
extern "C" int ceph_set_default_file_stripe_unit(int stripe)
345
client->set_default_file_stripe_unit(stripe);
349
extern "C" int ceph_set_default_file_stripe_count(int count)
351
client->set_default_file_stripe_unit(count);
355
extern "C" int ceph_set_default_object_size(int size)
357
client->set_default_object_size(size);
361
extern "C" int ceph_set_default_file_replication(int replication)
363
client->set_default_file_replication(replication);
367
extern "C" int ceph_set_default_preferred_pg(int pg)
369
client->set_default_preferred_pg(pg);
373
extern "C" int ceph_get_file_stripe_address(int fh, loff_t offset, char *buf, int buflen)
376
int r = client->get_file_stripe_address(fh, offset, address);
377
if (r != 0) return r; //at time of writing, method ONLY returns
378
// 0 or -EINVAL if there are no known osds
379
int len = address.size()+1;
381
if (buflen == 0) return len;
384
len = address.copy(buf, len, 0);
385
buf[len] = '\0'; // write a null char to terminate c-style string