-
Notifications
You must be signed in to change notification settings - Fork 5
/
yfs_client.cc
384 lines (307 loc) · 7.41 KB
/
yfs_client.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// yfs client. implements FS operations using extent and lock server
#include "yfs_client.h"
#include "extent_client.h"
#include <sstream>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
// RAII for yfs distributed lock.
template <typename L>
class scoped_lock_impl {
private:
L *lc;
lock_protocol::lockid_t lid;
bool flush;
public:
scoped_lock_impl(L *lc, lock_protocol::lockid_t lid, bool flush = false)
: lc(lc), lid(lid), flush(flush) {
while (lc->acquire(lid) != lock_protocol::OK) {
printf("yfs_client: acquiring lock failed, try again.\n");
}
}
~scoped_lock_impl() {
while (lc->release(lid, flush) != lock_protocol::OK) {
printf("yfs_client: releasing lock failed, try again.\n");
}
}
};
#ifdef RSM
using scoped_lock = scoped_lock_impl<lock_client_cache_rsm>;
#else
using scoped_lock = scoped_lock_impl<lock_client_cache>;
#endif
class lock_release_user_impl : public lock_release_user {
public:
lock_release_user_impl(extent_client *ec) : ec(ec) { }
virtual void dorelease(lock_protocol::lockid_t id) {
VERIFY(ec->flush(id) == extent_protocol::OK);
}
private:
extent_client *ec;
};
yfs_client::yfs_client(std::string extent_dst, std::string lock_dst)
: generator(time(NULL)), distribution(2, (1u << 31) - 1)
{
// It will cause disaster if we run two concurrent yfs_clients with the same seed!
// TODO: GC.
ec = new extent_client(extent_dst);
#ifdef RSM
lc = new lock_client_cache_rsm(lock_dst, new lock_release_user_impl(ec));
#else
lc = new lock_client_cache(lock_dst, new lock_release_user_impl(ec));
#endif
}
yfs_client::inum
yfs_client::n2i(std::string n)
{
std::istringstream ist(n);
unsigned long long finum;
ist >> finum;
return finum;
}
std::string
yfs_client::filename(inum inum)
{
std::ostringstream ost;
ost << inum;
return ost.str();
}
// Pray that we have no collision!
yfs_client::inum
yfs_client::new_inum(bool is_file)
{
return distribution(generator) | (is_file ? 0x80000000 : 0x0);
}
bool
yfs_client::isfile(inum inum)
{
if (inum & 0x80000000)
return true;
return false;
}
bool
yfs_client::isdir(inum inum)
{
return !isfile(inum);
}
yfs_client::status
yfs_client::getfile(inum inum, fileinfo &fin)
{
scoped_lock sl(lc, inum);
yfs_client::status r = OK;
printf("getfile %016llx\n", inum);
extent_protocol::attr a;
if (ec->getattr(inum, a) != extent_protocol::OK) {
r = IOERR;
goto release;
}
fin.atime = a.atime;
fin.mtime = a.mtime;
fin.ctime = a.ctime;
fin.size = a.size;
printf("getfile %016llx -> sz %llu\n", inum, fin.size);
release:
return r;
}
yfs_client::status
yfs_client::getdir(inum inum, dirinfo &din)
{
scoped_lock sl(lc, inum);
yfs_client::status r = OK;
printf("getdir %016llx\n", inum);
extent_protocol::attr a;
if (ec->getattr(inum, a) != extent_protocol::OK) {
r = IOERR;
goto release;
}
din.atime = a.atime;
din.mtime = a.mtime;
din.ctime = a.ctime;
release:
return r;
}
yfs_client::status
yfs_client::read(inum inum, size_t size, off_t offset, std::string &output)
{
if (!isfile(inum)) {
return NOENT;
}
scoped_lock sl(lc, inum);
extent_protocol::status status;
std::string buf;
status = ec->get(inum, buf);
if (status != OK) {
return status;
}
// Adjust the size to read of fit the file.
if (offset >= (off_t) buf.size()) {
size = 0;
} else {
size = std::min(size, buf.size() - offset);
}
output = buf.substr(offset, size);
return OK;
}
yfs_client::status
yfs_client::write(inum inum, const char *input, size_t size, off_t offset)
{
if (!isfile(inum)) {
return NOENT;
}
scoped_lock sl(lc, inum);
extent_protocol::status status;
std::string buf;
status = ec->get(inum, buf);
if (status != OK) {
return status;
}
// Grow the file if necessary.
if (buf.size() < size + offset) {
buf.resize(size + offset);
}
for (size_t i = 0; i < size; ++i) {
buf[offset + i] = input[i];
}
return ec->put(inum, buf);
}
yfs_client::status
yfs_client::setattr(inum inum, size_t size)
{
if (!isfile(inum)) {
return NOENT;
}
scoped_lock sl(lc, inum);
extent_protocol::status status;
std::string buf;
status = ec->get(inum, buf);
if (status != OK) {
return status;
}
buf.resize(size);
return ec->put(inum, buf);
}
//
// The directory content is stored in the following format:
// /file_1/inum_1/file_2/inum_2/.../file_n/inum_n.
//
yfs_client::status
yfs_client::readdir(inum parent, std::vector<dirent> &ents)
{
if (!isdir(parent)) {
return NOENT;
}
scoped_lock sl(lc, parent);
extent_protocol::status status;
std::string buf;
status = ec->get(parent, buf);
if (status != OK) {
return status;
}
ents.clear();
size_t last_slash = 0;
while (true) {
size_t slash_1, slash_2;
slash_1 = buf.find("/", last_slash + 1);
if (slash_1 == std::string::npos) {
return IOERR; // corrupted data
}
slash_2 = buf.find("/", slash_1 + 1);
if (slash_2 == std::string::npos) {
slash_2 = buf.size();
}
dirent ent;
ent.name = buf.substr(last_slash + 1, slash_1 - last_slash - 1);
ent.inum = n2i(buf.substr(slash_1 + 1, slash_2 - slash_1 - 1));
ents.emplace_back(std::move(ent));
if (slash_2 == buf.size()) {
break;
} else {
last_slash = slash_2;
}
}
return OK;
}
// Return OK if found.
yfs_client::status
yfs_client::lookup(inum parent, const char *name, inum &child)
{
std::vector<dirent> ents;
yfs_client::status status;
status = readdir(parent, ents);
if (status != OK) {
return status;
}
for (std::vector<dirent>::iterator it = ents.begin(); it != ents.end(); ++it) {
if (it->name == name) {
child = it->inum;
return OK;
}
}
return NOENT;
}
yfs_client::status
yfs_client::create(inum parent, bool is_file, const char *name, inum &child)
{
if (!isdir(parent)) {
return NOENT;
}
scoped_lock sl(lc, parent);
extent_protocol::status status;
std::string buf;
status = ec->get(parent, buf);
if (status != OK) {
return status;
}
if (buf.find(std::string("/") + name + "/") != std::string::npos) {
return EXIST;
}
child = new_inum(is_file);
status = ec->put(child, "");
if (status != OK) {
return status;
}
buf.append("/");
buf.append(name);
buf.append("/");
buf.append(filename(child));
return ec->put(parent, buf);
}
yfs_client::status
yfs_client::unlink(inum parent, const char *name)
{
// Do *not* allow unlinking of a directory.
if (!isdir(parent)) {
return NOENT;
}
scoped_lock sl(lc, parent);
extent_protocol::status status;
std::string buf;
status = ec->get(parent, buf);
if (status != OK) {
return status;
}
size_t file_begin = buf.find(std::string("/") + name + "/");
size_t file_mid, file_end;
if (file_begin == std::string::npos) {
return NOENT;
}
file_mid = file_begin + strlen(name) + 2;
file_end = buf.find("/", file_mid);
if (file_end == std::string::npos) {
file_end = buf.size();
}
inum inum = n2i(buf.substr(file_mid, file_end - file_mid));
// Flush the deleted file (i.e. return the lock to server).
// Otherwise the deleted file will lost tracking.
scoped_lock sl_2(lc, inum, true /* flush */);
status = ec->remove(inum);
if (status != OK) {
return status;
}
buf.erase(file_begin, file_end - file_begin);
return ec->put(parent, buf);
}