/
yfs_client.cc
369 lines (330 loc) · 9.16 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
// yfs client. implements FS operations using extent and lock server
#include "yfs_client.h"
#include "extent_client.h"
#include "lock_client_cache.h"
#include <sstream>
#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <cmath>
#include <thread>
#include <chrono>
void
custom_lock_release_user::dorelease(lock_protocol::lockid_t lid) {
int ret;
while ((ret = ec->flush(lid)) != extent_protocol::OK);
}
yfs_client::yfs_client(std::string extent_dst, std::string lock_dst)
{
ec = new extent_client(extent_dst);
custom_lock_release_user *newlc = new custom_lock_release_user(ec);
lc = new lock_client_cache(lock_dst, newlc);
}
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();
}
std::string yfs_client::serialize(dirent_lst_t lst) {
// encode as 'inum:folder1/folder2/../filename' separated by \n
std::string retval;
for (auto it: lst) {
retval += "" + filename(it.inum) + ":" + it.name + "\n";
}
return retval;
}
yfs_client::dirent_lst_t yfs_client::unserialize(std::string serialized) {
yfs_client::dirent_lst_t retval;
std::stringstream ss(serialized);
std::string part;
while (getline(ss, part, '\n')) {
// id:filename
int pos_of_delim;
for (int i=0; i<((int)part.size()); i++) {
if (part[i] == ':') {
pos_of_delim = i;
break;
}
}
unsigned long long inum = n2i(part.substr(0, pos_of_delim));
std::string name = part.substr(pos_of_delim+1, part.size());
dirent ent = dirent(name, inum);
retval.push_back(ent);
}
return retval;
}
bool
yfs_client::isfile(inum inum)
{
if(inum & 0x80000000)
return true;
return false;
}
bool
yfs_client::isdir(inum inum)
{
return ! isfile(inum);
}
int
yfs_client::getfile(inum inum, fileinfo &fin)
{
acquire_lock(inum);
printf("getfile %016llx\n", inum);
extent_protocol::attr a;
if (ec->getattr(inum, a) != extent_protocol::OK) {
release_lock(inum);
return IOERR;
}
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_lock(inum);
return OK;
}
int
yfs_client::getdir(inum inum, dirinfo &din)
{
acquire_lock(inum);
printf("getdir %016llx\n", inum);
extent_protocol::attr a;
if (ec->getattr(inum, a) != extent_protocol::OK) {
release_lock(inum);
return IOERR;
}
din.atime = a.atime;
din.mtime = a.mtime;
din.ctime = a.ctime;
release_lock(inum);
return OK;
}
int yfs_client::get_all_in_dir(yfs_client::inum parent, dirent_lst_t& dirent_lst) {
if (!isdir(parent))
return IOERR;
std::string serialized;
if (ec->get(parent, serialized) != OK) {
return NOENT;
}
dirent_lst = yfs_client::unserialize(serialized);
return OK;
}
int yfs_client::put_all_in_dir(inum parent, dirent_lst_t dirent_lst) {
if (!isdir(parent))
return IOERR;
std::string serialized = yfs_client::serialize(dirent_lst);
return ec->put(parent, serialized);
}
yfs_client::inum yfs_client::create_random_inum(bool is_dir) {
unsigned long long upper32bits = rand();
unsigned long long lower32bits = rand();
unsigned long long random64bits = ((upper32bits << 32) | lower32bits);
if (!is_dir) {
// file: set the 32nd bit to 1
return random64bits | 0x80000000;
}
// dir: set the 32nd bit to 0
return random64bits & (~(1L << 31));
}
int yfs_client::create(inum parent, const char *name, int is_dir, inum &inum) {
acquire_lock(parent);
if(isfile(parent)){
release_lock(parent);
return IOERR;
}
// 0. check if file/folder already exists
dirent_lst_t dirent_lst_check;
auto get_ret = get_all_in_dir(parent, dirent_lst_check);
if (get_ret != OK) {
printf("ERROR! yfs_client::create get_ret failed! inum = %016llx name = %s\n\n", inum, name);
release_lock(parent);
return get_ret;
}
for (auto it: dirent_lst_check) {
if (it.name == name) {
if (is_dir){
release_lock(parent);
return NOENT;
} else {
inum = it.inum;
release_lock(parent);
return OK;
}
}
}
// 1. save new file/folder as a node
inum = create_random_inum(is_dir);
acquire_lock(inum);
auto put_ret = ec->put(inum, "");
release_lock(inum);
if (put_ret != OK) {
printf("ERROR! yfs_client::create put_ret failed! inum = %016llx name = %s\n\n", inum, name);
release_lock(parent);
return put_ret;
}
// 2. update the parent directory to reflect the new file
dirent_lst_t dirent_lst;
auto all_dir_ret = get_all_in_dir(parent, dirent_lst);
if (all_dir_ret != OK) {
printf("ERROR! yfs_client::create get_all_in_dir failed! parent = %016llx\n\n", parent);
release_lock(parent);
return all_dir_ret;
}
dirent_lst.push_back(dirent(name, inum));
auto put_all_ret = put_all_in_dir(parent, dirent_lst);
if (put_all_ret != OK) {
printf("ERROR! yfs_client::create put_all_in_dir failed! parent = %016llx\n\n", parent);
release_lock(parent);
return put_all_ret;
}
release_lock(parent);
return OK;
}
int yfs_client::lookup(inum parent, const char *name, inum &inum) {
acquire_lock(parent);
dirent_lst_t dirent_lst;
auto ret = get_all_in_dir(parent, dirent_lst);
if (ret != OK) {
printf("ERROR! yfs_client::lookup get_all_in_dir failed! parent = %016llx\n\n", parent);
release_lock(parent);
return ret;
}
for (auto it: dirent_lst) {
if (it.name == name) {
inum = it.inum;
release_lock(parent);
return OK;
}
}
release_lock(parent);
return NOENT;
}
int yfs_client::readdir(inum parent, dirent_lst_t& dirent_lst) {
acquire_lock(parent);
auto ret = get_all_in_dir(parent, dirent_lst);
if (ret != OK) {
printf("ERROR! yfs_client::readdir get_all_in_dir failed! parent = %016llx\n\n", parent);
release_lock(parent);
return ret;
}
release_lock(parent);
return OK;
}
int yfs_client::read(inum inum, off_t offset, size_t size, std::string& data) {
acquire_lock(inum);
std::string content;
auto ret = ec->get(inum, content);
if (ret != OK) {
printf("ERROR! yfs_client::read ec->get failed! inum = %016llx\n\n", inum);
release_lock(inum);
return ret;
}
if (offset + size <= content.size()) {
data = content.substr(offset, size);
} else {
data = content;
data.resize(size, '\0');
}
release_lock(inum);
return OK;
}
int yfs_client::write(inum inum, off_t offset, size_t size, std::string data) {
acquire_lock(inum);
std::string content;
auto ret = ec->get(inum, content);
if (ret != OK) {
printf("ERROR! yfs_client::write ec->get failed! inum = %016llx\n\n", inum);
release_lock(inum);
return ret;
}
if (content.size() < offset + size) {
content.resize(offset + size);
}
content.replace(offset, size, data.substr(0, std::min(data.size(), size)));
// replace messes up the earlier resize
if (content.size() < offset + size) {
content.resize(offset + size);
}
auto put_ret = ec->put(inum, content);
if (put_ret != OK) {
printf("ERROR! yfs_client::write ec->put failed! inum = %016llx\n\n", inum);
release_lock(inum);
return put_ret;
}
release_lock(inum);
return OK;
}
int yfs_client::resize(inum inum, int size) {
acquire_lock(inum);
std::string content;
auto ret = ec->get(inum, content);
if (ret != OK) {
printf("ERROR! yfs_client::resize ec->get failed! inum = %016llx\n\n", inum);
release_lock(inum);
return ret;
}
content.resize(size, '\0');
auto put_ret = ec->put(inum, content);
if (put_ret != OK) {
printf("ERROR! yfs_client::resize ec->put failed! inum = %016llx\n\n", inum);
release_lock(inum);
return put_ret;
}
release_lock(inum);
return OK;
}
int yfs_client::unlink(yfs_client::inum parent, const char *name) {
acquire_lock(parent);
std::string buffer;
auto get_ret = ec->get(parent, buffer);
if (get_ret != extent_protocol::OK) {
release_lock(parent);
return get_ret;
}
yfs_client::dirent_lst_t folder_contents = unserialize(buffer);
for (auto it = folder_contents.begin(); it != folder_contents.end(); it++) {
if (it->name == name) {
auto file_inum = it->inum;
folder_contents.erase(it);
// delete file
acquire_lock(file_inum);
auto remove_ret = ec->remove(file_inum);
release_lock(file_inum);
if (remove_ret != extent_protocol::OK) {
printf("ERROR! yfs_client::unlink ec->remove failed! inum = %016llx\n\n", file_inum);
release_lock(parent);
return remove_ret;
}
// update parent folder
std::string serialized = serialize(folder_contents);
auto put_ret = ec->put(parent, serialized);
if (put_ret != extent_protocol::OK) {
printf("ERROR! yfs_client::unlink ec->put failed! inum = %016llx\n\n", parent);
release_lock(parent);
return put_ret;
}
break;
}
}
release_lock(parent);
return OK;
}
void yfs_client::acquire_lock(yfs_client::inum inum) {
lc->acquire(inum);
}
void yfs_client::release_lock(yfs_client::inum inum) {
lc->release(inum);
}