~mathiaz/+junk/ceph-new-pkg-review

« back to all changes in this revision

Viewing changes to src/libceph.cc

  • Committer: Mathias Gug
  • Date: 2010-07-29 03:10:42 UTC
  • Revision ID: mathias.gug@canonical.com-20100729031042-n9n8kky962qb4onb
Import ceph_0.21-0ubuntu1 from https://launchpad.net/~clint-fewbar/+archive/ceph/+packages.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "client/libceph.h"
 
2
 
 
3
#include <string.h>
 
4
#include <fcntl.h>
 
5
#include <iostream>
 
6
 
 
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"
 
12
 
 
13
/* ************* ************* ************* *************
 
14
 * C interface
 
15
 */
 
16
 
 
17
extern "C" const char *ceph_version(int *pmajor, int *pminor, int *ppatch)
 
18
{
 
19
  int major, minor, patch;
 
20
 
 
21
  sscanf(VERSION, "%d.%d.%d", &major, &minor, &patch);
 
22
  if (pmajor)
 
23
    *pmajor = major;
 
24
  if (pminor)
 
25
    *pminor = minor;
 
26
  if (ppatch)
 
27
    *ppatch = patch;
 
28
  return VERSION;
 
29
}
 
30
 
 
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;
 
37
 
 
38
extern "C" int ceph_initialize(int argc, const char **argv)
 
39
{
 
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();
 
48
    //monmap
 
49
    monclient = new MonClient();
 
50
    if (monclient->build_initial_monmap() < 0) {
 
51
      delete monclient;
 
52
      return -1; //error!
 
53
    }
 
54
    //network connection
 
55
    messenger = new SimpleMessenger();
 
56
    messenger->register_entity(entity_name_t::CLIENT());
 
57
 
 
58
    //at last the client
 
59
    client = new Client(messenger, monclient);
 
60
 
 
61
    messenger->start();
 
62
 
 
63
    client->init();
 
64
  }
 
65
  ++client_initialized;
 
66
  ceph_client_mutex.Unlock();
 
67
  return 0;
 
68
}
 
69
 
 
70
extern "C" void ceph_deinitialize()
 
71
{
 
72
  ceph_client_mutex.Lock();
 
73
  --client_initialized;
 
74
  if(!client_initialized) {
 
75
    if(client_mount) {
 
76
      client_mount = 0;
 
77
      client->unmount();
 
78
    }
 
79
    client->shutdown();
 
80
    delete client;
 
81
    messenger->wait();
 
82
    messenger->destroy();
 
83
    delete monclient;
 
84
  }
 
85
  ceph_client_mutex.Unlock();
 
86
}
 
87
 
 
88
extern "C" int ceph_mount()
 
89
{
 
90
  int ret;
 
91
  Mutex::Locker lock(ceph_client_mutex);
 
92
  if(!client_mount) {
 
93
     ret = client->mount();
 
94
     if (ret!=0)
 
95
       return ret;
 
96
  }
 
97
  ++client_mount;
 
98
  return 0;
 
99
}
 
100
 
 
101
extern "C" int ceph_umount()
 
102
{
 
103
  Mutex::Locker lock(ceph_client_mutex);
 
104
  --client_mount;
 
105
  if (!client_mount)
 
106
    return client->unmount();
 
107
  return 0;
 
108
}
 
109
 
 
110
extern "C" int ceph_statfs(const char *path, struct statvfs *stbuf)
 
111
{
 
112
  return client->statfs(path, stbuf);
 
113
}
 
114
 
 
115
extern "C" int ceph_get_local_osd()
 
116
{
 
117
  return client->get_local_osd();
 
118
}
 
119
 
 
120
extern "C" int ceph_getcwd(char *buf, int buflen)
 
121
{
 
122
  string cwd;
 
123
  client->getcwd(cwd);
 
124
  int size = cwd.size()+1; //need space for null character
 
125
  if (size > buflen) {
 
126
    if (buflen == 0) return size;
 
127
    else return -ERANGE;
 
128
  }
 
129
  size = cwd.copy(buf, size);
 
130
  buf[size] = '\0'; //fill in null character
 
131
  return 0;
 
132
}
 
133
 
 
134
extern "C" int ceph_chdir (const char *s)
 
135
{
 
136
  return client->chdir(s);
 
137
}
 
138
 
 
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)
 
143
{
 
144
  client->getcwd(cwd);
 
145
}
 
146
 
 
147
extern "C" int ceph_opendir(const char *name, DIR **dirpp)
 
148
{
 
149
  return client->opendir(name, dirpp);
 
150
}
 
151
 
 
152
extern "C" int ceph_closedir(DIR *dirp)
 
153
{
 
154
  return client->closedir(dirp);
 
155
}
 
156
 
 
157
extern "C" int ceph_readdir_r(DIR *dirp, struct dirent *de)
 
158
{
 
159
  return client->readdir_r(dirp, de);
 
160
}
 
161
 
 
162
extern "C" int ceph_readdirplus_r(DIR *dirp, struct dirent *de, struct stat *st, int *stmask)
 
163
{
 
164
  return client->readdirplus_r(dirp, de, st, stmask);
 
165
}
 
166
 
 
167
extern "C" int ceph_getdents(DIR *dirp, char *buf, int buflen)
 
168
{
 
169
  return client->getdents(dirp, buf, buflen);
 
170
}
 
171
 
 
172
extern "C" int ceph_getdnames(DIR *dirp, char *buf, int buflen)
 
173
{
 
174
  return client->getdnames(dirp, buf, buflen);
 
175
}
 
176
 
 
177
extern "C" void ceph_rewinddir(DIR *dirp)
 
178
{
 
179
  client->rewinddir(dirp);
 
180
}
 
181
 
 
182
extern "C" loff_t ceph_telldir(DIR *dirp)
 
183
{
 
184
  return client->telldir(dirp);
 
185
}
 
186
 
 
187
extern "C" void ceph_seekdir(DIR *dirp, loff_t offset)
 
188
{
 
189
  client->seekdir(dirp, offset);
 
190
}
 
191
 
 
192
extern "C" int ceph_link (const char *existing, const char *newname)
 
193
{
 
194
  return client->link(existing, newname);
 
195
}
 
196
 
 
197
extern "C" int ceph_unlink (const char *path)
 
198
{
 
199
  return client->unlink(path);
 
200
}
 
201
 
 
202
extern "C" int ceph_rename(const char *from, const char *to)
 
203
{
 
204
  return client->rename(from, to);
 
205
}
 
206
 
 
207
// dirs
 
208
extern "C" int ceph_mkdir(const char *path, mode_t mode)
 
209
{
 
210
  return client->mkdir(path, mode);
 
211
}
 
212
 
 
213
extern "C" int ceph_mkdirs(const char *path, mode_t mode)
 
214
{
 
215
  return client->mkdirs(path, mode);
 
216
}
 
217
 
 
218
extern "C" int ceph_rmdir(const char *path)
 
219
{
 
220
  return client->rmdir(path);
 
221
}
 
222
 
 
223
// symlinks
 
224
extern "C" int ceph_readlink(const char *path, char *buf, loff_t size)
 
225
{
 
226
  return client->readlink(path, buf, size);
 
227
}
 
228
 
 
229
extern "C" int ceph_symlink(const char *existing, const char *newname)
 
230
{
 
231
  return client->symlink(existing, newname);
 
232
}
 
233
 
 
234
// inode stuff
 
235
extern "C" int ceph_lstat(const char *path, struct stat *stbuf)
 
236
{
 
237
  return client->lstat(path, stbuf);
 
238
}
 
239
 
 
240
extern "C" int ceph_lstat_precise(const char *path, stat_precise *stbuf)
 
241
{
 
242
  return client->lstat_precise(path, (Client::stat_precise*)stbuf);
 
243
}
 
244
 
 
245
extern "C" int ceph_setattr(const char *relpath, struct stat *attr, int mask)
 
246
{
 
247
  Client::stat_precise p_attr = Client::stat_precise(*attr);
 
248
  return client->setattr(relpath, &p_attr, mask);
 
249
}
 
250
 
 
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);
 
254
}
 
255
 
 
256
extern "C" int ceph_chmod(const char *path, mode_t mode)
 
257
{
 
258
  return client->chmod(path, mode);
 
259
}
 
260
extern "C" int ceph_chown(const char *path, uid_t uid, gid_t gid)
 
261
{
 
262
  return client->chown(path, uid, gid);
 
263
}
 
264
 
 
265
extern "C" int ceph_utime(const char *path, struct utimbuf *buf)
 
266
{
 
267
  return client->utime(path, buf);
 
268
}
 
269
 
 
270
extern "C" int ceph_truncate(const char *path, loff_t size)
 
271
{
 
272
  return client->truncate(path, size);
 
273
}
 
274
 
 
275
// file ops
 
276
extern "C" int ceph_mknod(const char *path, mode_t mode, dev_t rdev)
 
277
{
 
278
  return client->mknod(path, mode, rdev);
 
279
}
 
280
 
 
281
extern "C" int ceph_open(const char *path, int flags, mode_t mode)
 
282
{
 
283
  return client->open(path, flags, mode);
 
284
}
 
285
 
 
286
extern "C" int ceph_close(int fd)
 
287
{
 
288
  return client->close(fd);
 
289
}
 
290
 
 
291
extern "C" loff_t ceph_lseek(int fd, loff_t offset, int whence)
 
292
{
 
293
  return client->lseek(fd, offset, whence);
 
294
}
 
295
 
 
296
extern "C" int ceph_read(int fd, char *buf, loff_t size, loff_t offset)
 
297
{
 
298
  return client->read(fd, buf, size, offset);
 
299
}
 
300
 
 
301
extern "C" int ceph_write(int fd, const char *buf, loff_t size, loff_t offset)
 
302
{
 
303
  return client->write(fd, buf, size, offset);
 
304
}
 
305
 
 
306
extern "C" int ceph_ftruncate(int fd, loff_t size)
 
307
{
 
308
  return client->ftruncate(fd, size);
 
309
}
 
310
 
 
311
extern "C" int ceph_fsync(int fd, bool syncdataonly)
 
312
{
 
313
  return client->fsync(fd, syncdataonly);
 
314
}
 
315
 
 
316
extern "C" int ceph_fstat(int fd, struct stat *stbuf)
 
317
{
 
318
  return client->fstat(fd, stbuf);
 
319
}
 
320
 
 
321
extern "C" int ceph_sync_fs()
 
322
{
 
323
  return client->sync_fs();
 
324
}
 
325
 
 
326
extern "C" int ceph_get_file_stripe_unit(int fh)
 
327
{
 
328
  return client->get_file_stripe_unit(fh);
 
329
}
 
330
 
 
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);
 
334
  client->close(fd);
 
335
  return rep;
 
336
}
 
337
 
 
338
extern "C" int ceph_get_default_preferred_pg(int fd)
 
339
{
 
340
  return client->get_default_preferred_pg(fd);
 
341
}
 
342
 
 
343
extern "C" int ceph_set_default_file_stripe_unit(int stripe)
 
344
{
 
345
  client->set_default_file_stripe_unit(stripe);
 
346
  return 0;
 
347
}
 
348
 
 
349
extern "C" int ceph_set_default_file_stripe_count(int count)
 
350
{
 
351
  client->set_default_file_stripe_unit(count);
 
352
  return 0;
 
353
}
 
354
 
 
355
extern "C" int ceph_set_default_object_size(int size)
 
356
{
 
357
  client->set_default_object_size(size);
 
358
  return 0;
 
359
}
 
360
 
 
361
extern "C" int ceph_set_default_file_replication(int replication)
 
362
{
 
363
  client->set_default_file_replication(replication);
 
364
  return 0;
 
365
}
 
366
 
 
367
extern "C" int ceph_set_default_preferred_pg(int pg)
 
368
{
 
369
  client->set_default_preferred_pg(pg);
 
370
  return 0;
 
371
}
 
372
 
 
373
extern "C" int ceph_get_file_stripe_address(int fh, loff_t offset, char *buf, int buflen)
 
374
{
 
375
  string address;
 
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;
 
380
  if (len > buflen) {
 
381
    if (buflen == 0) return len;
 
382
    else return -ERANGE;
 
383
  }
 
384
  len = address.copy(buf, len, 0);
 
385
  buf[len] = '\0'; // write a null char to terminate c-style string
 
386
  return 0;
 
387
}