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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
|
/*
Copyright (c) 2006-2009 Z RESEARCH, Inc. <http://www.zresearch.com>
This file is part of GlusterFS.
GlusterFS is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
by the Free Software Foundation; either version 3 of the License,
or (at your option) any later version.
GlusterFS is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see
<http://www.gnu.org/licenses/>.
*/
#ifndef _CONFIG_H
#define _CONFIG_H
#include "config.h"
#endif
#include "glusterfs.h"
#include "logging.h"
#include "dict.h"
#include "xlator.h"
#include "read-ahead.h"
#include <assert.h>
ra_page_t *
ra_page_get (ra_file_t *file,
off_t offset)
{
ra_page_t *page = NULL;
off_t rounded_offset = 0;
page = file->pages.next;
rounded_offset = floor (offset, file->page_size);
while (page != &file->pages && page->offset < rounded_offset)
page = page->next;
if (page == &file->pages || page->offset != rounded_offset)
page = NULL;
return page;
}
ra_page_t *
ra_page_create (ra_file_t *file, off_t offset)
{
ra_page_t *page = NULL;
off_t rounded_offset = 0;
ra_page_t *newpage = NULL;
page = file->pages.next;
rounded_offset = floor (offset, file->page_size);
while (page != &file->pages && page->offset < rounded_offset)
page = page->next;
if (page == &file->pages || page->offset != rounded_offset) {
newpage = CALLOC (1, sizeof (*newpage));
if (!newpage)
return NULL;
newpage->offset = rounded_offset;
newpage->prev = page->prev;
newpage->next = page;
newpage->file = file;
page->prev->next = newpage;
page->prev = newpage;
page = newpage;
}
return page;
}
void
ra_wait_on_page (ra_page_t *page, call_frame_t *frame)
{
ra_waitq_t *waitq = NULL;
ra_local_t *local = NULL;
local = frame->local;
waitq = CALLOC (1, sizeof (*waitq));
if (!waitq) {
gf_log (frame->this->name, GF_LOG_ERROR,
"out of memory :(");
return;
}
waitq->data = frame;
waitq->next = page->waitq;
page->waitq = waitq;
ra_local_lock (local);
{
local->wait_count++;
}
ra_local_unlock (local);
}
void
ra_waitq_return (ra_waitq_t *waitq)
{
ra_waitq_t *trav = NULL;
ra_waitq_t *next = NULL;
call_frame_t *frame = NULL;
for (trav = waitq; trav; trav = next) {
next = trav->next;
frame = trav->data;
ra_frame_return (frame);
free (trav);
}
}
int
ra_fault_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
int32_t op_ret, int32_t op_errno, struct iovec *vector,
int32_t count, struct stat *stbuf)
{
ra_local_t *local = NULL;
off_t pending_offset = 0;
ra_file_t *file = NULL;
ra_page_t *page = NULL;
off_t trav_offset = 0;
size_t payload_size = 0;
ra_waitq_t *waitq = NULL;
fd_t *fd = NULL;
int ret = 0;
uint64_t tmp_file = 0;
local = frame->local;
fd = local->fd;
ret = fd_ctx_get (fd, this, &tmp_file);
file = (ra_file_t *)(long)tmp_file;
pending_offset = local->pending_offset;
trav_offset = pending_offset;
payload_size = op_ret;
ra_file_lock (file);
{
if (op_ret >= 0)
file->stbuf = *stbuf;
if (op_ret < 0) {
page = ra_page_get (file, pending_offset);
if (page)
waitq = ra_page_error (page, op_ret, op_errno);
goto unlock;
}
page = ra_page_get (file, pending_offset);
if (!page) {
gf_log (this->name, GF_LOG_DEBUG,
"wasted copy: %"PRId64"[+%"PRId64"] file=%p",
pending_offset, file->page_size, file);
goto unlock;
}
if (page->vector) {
dict_unref (page->ref);
free (page->vector);
}
page->vector = iov_dup (vector, count);
page->count = count;
page->ref = dict_ref (frame->root->rsp_refs);
page->ready = 1;
page->size = iov_length (vector, count);
waitq = ra_page_wakeup (page);
}
unlock:
ra_file_unlock (file);
ra_waitq_return (waitq);
fd_unref (local->fd);
free (frame->local);
frame->local = NULL;
STACK_DESTROY (frame->root);
return 0;
}
void
ra_page_fault (ra_file_t *file,
call_frame_t *frame,
off_t offset)
{
call_frame_t *fault_frame = NULL;
ra_local_t *fault_local = NULL;
fault_frame = copy_frame (frame);
fault_local = CALLOC (1, sizeof (ra_local_t));
fault_frame->local = fault_local;
fault_local->pending_offset = offset;
fault_local->pending_size = file->page_size;
fault_local->fd = fd_ref (file->fd);
STACK_WIND (fault_frame, ra_fault_cbk,
FIRST_CHILD (fault_frame->this),
FIRST_CHILD (fault_frame->this)->fops->readv,
file->fd, file->page_size, offset);
return;
}
void
ra_frame_fill (ra_page_t *page, call_frame_t *frame)
{
ra_local_t *local = NULL;
ra_fill_t *fill = NULL;
off_t src_offset = 0;
off_t dst_offset = 0;
ssize_t copy_size = 0;
ra_fill_t *new = NULL;
local = frame->local;
fill = &local->fill;
if (local->op_ret != -1 && page->size) {
if (local->offset > page->offset)
src_offset = local->offset - page->offset;
else
dst_offset = page->offset - local->offset;
copy_size = min (page->size - src_offset,
local->size - dst_offset);
if (copy_size < 0) {
/* if page contains fewer bytes and the required offset
is beyond the page size in the page */
copy_size = src_offset = 0;
}
fill = fill->next;
while (fill != &local->fill) {
if (fill->offset > page->offset) {
break;
}
fill = fill->next;
}
new = CALLOC (1, sizeof (*new));
new->offset = page->offset;
new->size = copy_size;
new->refs = dict_ref (page->ref);
new->count = iov_subset (page->vector, page->count,
src_offset, src_offset+copy_size,
NULL);
new->vector = CALLOC (new->count, sizeof (struct iovec));
new->count = iov_subset (page->vector, page->count,
src_offset, src_offset+copy_size,
new->vector);
new->next = fill;
new->prev = new->next->prev;
new->next->prev = new;
new->prev->next = new;
local->op_ret += copy_size;
}
}
void
ra_frame_unwind (call_frame_t *frame)
{
ra_local_t *local = NULL;
ra_fill_t *fill = NULL;
int32_t count = 0;
struct iovec *vector;
int32_t copied = 0;
dict_t *refs = NULL;
ra_fill_t *next = NULL;
fd_t *fd = NULL;
ra_file_t *file = NULL;
int ret = 0;
uint64_t tmp_file = 0;
local = frame->local;
fill = local->fill.next;
refs = get_new_dict ();
frame->local = NULL;
while (fill != &local->fill) {
count += fill->count;
fill = fill->next;
}
vector = CALLOC (count, sizeof (*vector));
fill = local->fill.next;
while (fill != &local->fill) {
next = fill->next;
memcpy (((char *)vector) + copied, fill->vector,
fill->count * sizeof (*vector));
copied += (fill->count * sizeof (*vector));
dict_copy (fill->refs, refs);
fill->next->prev = fill->prev;
fill->prev->next = fill->prev;
dict_unref (fill->refs);
free (fill->vector);
free (fill);
fill = next;
}
frame->root->rsp_refs = dict_ref (refs);
fd = local->fd;
ret = fd_ctx_get (fd, frame->this, &tmp_file);
file = (ra_file_t *)(long)tmp_file;
STACK_UNWIND (frame, local->op_ret, local->op_errno,
vector, count, &file->stbuf);
dict_unref (refs);
pthread_mutex_destroy (&local->local_lock);
free (local);
free (vector);
return;
}
/*
* ra_frame_return -
* @frame:
*
*/
void
ra_frame_return (call_frame_t *frame)
{
ra_local_t *local = NULL;
int32_t wait_count = 0;
local = frame->local;
assert (local->wait_count > 0);
ra_local_lock (local);
{
wait_count = --local->wait_count;
}
ra_local_unlock (local);
if (!wait_count)
ra_frame_unwind (frame);
return;
}
/*
* ra_page_wakeup -
* @page:
*
*/
ra_waitq_t *
ra_page_wakeup (ra_page_t *page)
{
ra_waitq_t *waitq = NULL, *trav = NULL;
call_frame_t *frame;
waitq = page->waitq;
page->waitq = NULL;
trav = waitq;
for (trav = waitq; trav; trav = trav->next) {
frame = trav->data;
ra_frame_fill (page, frame);
}
return waitq;
}
/*
* ra_page_purge -
* @page:
*
*/
void
ra_page_purge (ra_page_t *page)
{
page->prev->next = page->next;
page->next->prev = page->prev;
if (page->ref) {
dict_unref (page->ref);
}
free (page->vector);
free (page);
}
/*
* ra_page_error -
* @page:
* @op_ret:
* @op_errno:
*
*/
ra_waitq_t *
ra_page_error (ra_page_t *page, int32_t op_ret, int32_t op_errno)
{
ra_waitq_t *waitq = NULL;
ra_waitq_t *trav = NULL;
call_frame_t *frame = NULL;
ra_local_t *local = NULL;
waitq = page->waitq;
page->waitq = NULL;
trav = waitq;
for (trav = waitq; trav; trav = trav->next) {
frame = trav->data;
local = frame->local;
if (local->op_ret != -1) {
local->op_ret = op_ret;
local->op_errno = op_errno;
}
}
ra_page_purge (page);
return waitq;
}
/*
* ra_file_destroy -
* @file:
*
*/
void
ra_file_destroy (ra_file_t *file)
{
ra_conf_t *conf = NULL;
ra_page_t *trav = NULL;
conf = file->conf;
ra_conf_lock (conf);
{
file->prev->next = file->next;
file->next->prev = file->prev;
}
ra_conf_unlock (conf);
trav = file->pages.next;
while (trav != &file->pages) {
ra_page_error (trav, -1, EINVAL);
trav = file->pages.next;
}
pthread_mutex_destroy (&file->file_lock);
free (file);
}
|