-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathshfs_fio.c
459 lines (395 loc) · 10.9 KB
/
shfs_fio.c
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
* Simple hash filesystem (SHFS)
*
* Authors: Simon Kuenzer <[email protected]>
*
*
* Copyright (c) 2013-2017, NEC Europe Ltd., NEC Corporation All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <target/sys.h>
#include "likely.h"
#include "shfs_fio.h"
#include "shfs.h"
#include "shfs_btable.h"
#include "shfs_cache.h"
#ifdef SHFS_STATS
#include "shfs_stats.h"
#endif
/*
* Register open on bentry and return it on success
*/
static inline SHFS_FD _shfs_fio_open_bentry(struct shfs_bentry *bentry)
{
#ifdef SHFS_STATS
struct shfs_el_stats *estats;
#endif
if (bentry->update) {
/* entry update in progress */
#ifdef SHFS_STATS
++shfs_vol.mstats.e;
#endif
errno = EBUSY;
return NULL;
}
++shfs_nb_open;
if (bentry->refcount == 0) {
trydown(&bentry->updatelock); /* lock file for updates */
shfs_fio_clear_cookie(bentry);
}
++bentry->refcount;
#ifdef SHFS_STATS
estats = shfs_stats_from_bentry(bentry);
estats->laccess = gettimestamp_s();
++estats->h;
#endif
return (SHFS_FD) bentry;
}
static inline __attribute__((always_inline))
struct shfs_bentry *_shfs_lookup_bentry_by_hash(hash512_t h)
{
struct shfs_bentry *bentry;
#ifdef SHFS_STATS
struct shfs_el_stats *estats;
#endif
bentry = shfs_btable_lookup(shfs_vol.bt, h);
#ifdef SHFS_STATS
if (unlikely(!bentry)) {
estats = shfs_stats_from_mstats(h);
if (likely(estats != NULL)) {
estats->laccess = gettimestamp_s();
++estats->m;
}
}
#endif
return bentry;
}
/*
* As long as we do not any operation that might call
* schedule() (e.g., printf()), we do not need to
* down/up the shfs_mount_lock semaphore -> coop.
* scheduler
*
* Note: This function should never be called from interrupt context
*/
SHFS_FD shfs_fio_open(const char *path)
{
hash512_t h;
struct shfs_bentry *bentry;
if (unlikely(!shfs_mounted)) {
errno = ENODEV;
return NULL;
}
/* lookup bentry (either by name or hash) */
if ((path[0] == SHFS_HASH_INDICATOR_PREFIX) &&
(path[1] != '\0')) {
if (hash_parse(path + 1, h, shfs_vol.hlen) < 0) {
#ifdef SHFS_STATS
++shfs_vol.mstats.i;
#endif
return NULL;
}
bentry = _shfs_lookup_bentry_by_hash(h);
} else {
if ((path[0] == '\0') ||
(path[0] == SHFS_HASH_INDICATOR_PREFIX && path[1] == '\0')) {
/* empty filename -> use default file */
bentry = shfs_vol.def_bentry;
#ifdef SHFS_STATS
if (!bentry)
++shfs_vol.mstats.i;
#endif
} else {
#ifdef SHFS_OPENBYNAME
bentry = shfs_btable_lookup_byname(shfs_vol.bt, shfs_vol.htable_chunk_cache, path);
#else
bentry = NULL;
#ifdef SHFS_STATS
if (unlikely(!bentry))
++shfs_vol.mstats.i;
#endif
#endif
}
}
if (!bentry) {
errno = ENOENT;
return NULL;
}
return _shfs_fio_open_bentry(bentry);
}
SHFS_FD shfs_fio_openh(hash512_t h)
{
struct shfs_bentry *bentry;
bentry = _shfs_lookup_bentry_by_hash(h);
if (!bentry) {
errno = ENOENT;
return NULL;
}
return _shfs_fio_open_bentry(bentry);
}
/*
* Opens a clone of an already opened file descriptor
* This clone has to be closed by shfs_fio_close(), too.
*/
SHFS_FD shfs_fio_openf(SHFS_FD f)
{
if (!f) {
errno = EINVAL;
return NULL;
}
if (!shfs_mounted) {
errno = ENODEV;
return NULL;
}
++f->refcount;
++shfs_nb_open;
return f;
}
/*
* Note: This function should never be called from interrupt context
*/
void shfs_fio_close(SHFS_FD f)
{
struct shfs_bentry *bentry = (struct shfs_bentry *) f;
--bentry->refcount;
if (bentry->refcount == 0) /* unlock file for updates */
up(&bentry->updatelock);
--shfs_nb_open;
}
void shfs_fio_name(SHFS_FD f, char *out, size_t outlen)
{
struct shfs_bentry *bentry = (struct shfs_bentry *) f;
struct shfs_hentry *hentry = bentry->hentry;
outlen = min(outlen, sizeof(hentry->name) + 1);
strncpy(out, hentry->name, outlen - 1);
out[outlen - 1] = '\0';
}
void shfs_fio_mime(SHFS_FD f, char *out, size_t outlen)
{
struct shfs_bentry *bentry = (struct shfs_bentry *) f;
struct shfs_hentry *hentry = bentry->hentry;
outlen = min(outlen, sizeof(hentry->f_attr.mime) + 1);
strncpy(out, hentry->f_attr.mime, outlen - 1);
out[outlen - 1] = '\0';
}
void shfs_fio_size(SHFS_FD f, uint64_t *out)
{
struct shfs_bentry *bentry = (struct shfs_bentry *) f;
struct shfs_hentry *hentry = bentry->hentry;
*out = SHFS_HENTRY_ISLINK(hentry) ? 0 : hentry->f_attr.len;
}
void shfs_fio_hash(SHFS_FD f, hash512_t out)
{
struct shfs_bentry *bentry = (struct shfs_bentry *) f;
struct shfs_hentry *hentry = bentry->hentry;
hash_copy(out, hentry->hash, shfs_vol.hlen);
}
void shfs_fio_link_rpath(SHFS_FD f, char *out, size_t outlen)
{
struct shfs_bentry *bentry = (struct shfs_bentry *) f;
struct shfs_hentry *hentry = bentry->hentry;
outlen = min(outlen, sizeof(hentry->l_attr.rpath) + 1);
strncpy(out, hentry->l_attr.rpath, outlen - 1);
out[outlen - 1] = '\0';
}
/*
* Slow sync I/O file read functions
* Warning: These functions are using busy-waiting
*/
int shfs_fio_read(SHFS_FD f, uint64_t offset, void *buf, uint64_t len)
{
struct shfs_bentry *bentry = (struct shfs_bentry *) f;
struct shfs_hentry *hentry = bentry->hentry;
void *chk_buf;
chk_t chk_off;
uint64_t byt_off;
uint64_t buf_off;
uint64_t left;
uint64_t rlen;
int ret = 0;
/* check if entry is link to remote file */
if (SHFS_HENTRY_ISLINK(hentry))
return -EINVAL;
/* check boundaries */
if ((offset > hentry->f_attr.len) ||
((offset + len) > hentry->f_attr.len))
return -EINVAL;
/* pick chunk I/O buffer from pool */
chk_buf = target_malloc(shfs_vol.ioalign, shfs_vol.chunksize);
if (!chk_buf)
return -ENOMEM;
/* perform the I/O chunk-wise */
chk_off = shfs_volchk_foff(f, offset);
byt_off = shfs_volchkoff_foff(f, offset);
left = len;
buf_off = 0;
while (left) {
ret = shfs_read_chunk(chk_off, 1, chk_buf);
if (ret < 0)
goto out;
rlen = min(shfs_vol.chunksize - byt_off, left);
shfs_memcpy((uint8_t *) buf + buf_off,
(uint8_t *) chk_buf + byt_off,
rlen);
left -= rlen;
++chk_off; /* go to next chunk */
byt_off = 0; /* byte offset is set on the first chunk only */
buf_off += rlen;
}
out:
target_free(chk_buf);
return ret;
}
int shfs_fio_read_nosched(SHFS_FD f, uint64_t offset, void *buf, uint64_t len)
{
struct shfs_bentry *bentry = (struct shfs_bentry *) f;
struct shfs_hentry *hentry = bentry->hentry;
void *chk_buf;
chk_t chk_off;
uint64_t byt_off;
uint64_t buf_off;
uint64_t left;
uint64_t rlen;
int ret = 0;
/* check if entry is link to remote file */
if (SHFS_HENTRY_ISLINK(hentry))
return -EINVAL;
/* check boundaries */
if ((offset > hentry->f_attr.len) ||
((offset + len) > hentry->f_attr.len))
return -EINVAL;
/* pick chunk I/O buffer from pool */
chk_buf = target_malloc(shfs_vol.ioalign, shfs_vol.chunksize);
if (!chk_buf)
return -ENOMEM;
/* perform the I/O chunk-wise */
chk_off = shfs_volchk_foff(f, offset);
byt_off = shfs_volchkoff_foff(f, offset);
left = len;
buf_off = 0;
while (left) {
ret = shfs_read_chunk_nosched(chk_off, 1, chk_buf);
if (ret < 0)
goto out;
rlen = min(shfs_vol.chunksize - byt_off, left);
shfs_memcpy((uint8_t *) buf + buf_off,
(uint8_t *) chk_buf + byt_off,
rlen);
left -= rlen;
++chk_off; /* go to next chunk */
byt_off = 0; /* byte offset is set on the first chunk only */
buf_off += rlen;
}
out:
target_free(chk_buf);
return ret;
}
int shfs_fio_cache_read(SHFS_FD f, uint64_t offset, void *buf, uint64_t len)
{
struct shfs_bentry *bentry = (struct shfs_bentry *) f;
struct shfs_hentry *hentry = bentry->hentry;
struct shfs_cache_entry *cce;
chk_t chk_off;
uint64_t byt_off;
uint64_t buf_off;
uint64_t left;
uint64_t rlen;
int ret = 0;
/* check if entry is link to remote file */
if (SHFS_HENTRY_ISLINK(hentry))
return -EINVAL;
/* check boundaries */
if ((offset > hentry->f_attr.len) ||
((offset + len) > hentry->f_attr.len))
return -EINVAL;
/* perform the I/O chunk-wise */
chk_off = shfs_volchk_foff(f, offset);
byt_off = shfs_volchkoff_foff(f, offset);
left = len;
buf_off = 0;
while (left) {
cce = shfs_cache_read(chk_off);
if (!cce) {
ret = -errno;
goto out;
}
rlen = min(shfs_vol.chunksize - byt_off, left);
shfs_memcpy((uint8_t *) buf + buf_off,
(uint8_t *) cce->buffer + byt_off,
rlen);
left -= rlen;
shfs_cache_release(cce);
++chk_off; /* go to next chunk */
byt_off = 0; /* byte offset is set on the first chunk only */
buf_off += rlen;
}
out:
return ret;
}
int shfs_fio_cache_read_nosched(SHFS_FD f, uint64_t offset, void *buf, uint64_t len)
{
struct shfs_bentry *bentry = (struct shfs_bentry *) f;
struct shfs_hentry *hentry = bentry->hentry;
struct shfs_cache_entry *cce;
chk_t chk_off;
uint64_t byt_off;
uint64_t buf_off;
uint64_t left;
uint64_t rlen;
int ret = 0;
/* check if entry is link to remote file */
if (SHFS_HENTRY_ISLINK(hentry))
return -EINVAL;
/* check boundaries */
if ((offset > hentry->f_attr.len) ||
((offset + len) > hentry->f_attr.len))
return -EINVAL;
/* perform the I/O chunk-wise */
chk_off = shfs_volchk_foff(f, offset);
byt_off = shfs_volchkoff_foff(f, offset);
left = len;
buf_off = 0;
while (left) {
cce = shfs_cache_read_nosched(chk_off);
if (!cce) {
ret = -errno;
goto out;
}
rlen = min(shfs_vol.chunksize - byt_off, left);
shfs_memcpy((uint8_t *) buf + buf_off,
(uint8_t *) cce->buffer + byt_off,
rlen);
left -= rlen;
shfs_cache_release(cce);
++chk_off; /* go to next chunk */
byt_off = 0; /* byte offset is set on the first chunk only */
buf_off += rlen;
}
out:
return ret;
}