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
|
/*
Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com>
This file is part of GlusterFS.
This file is licensed to you under your choice of the GNU Lesser
General Public License, version 3 or any later version (LGPLv3 or
later), or the GNU General Public License, version 2 (GPLv2), in all
cases as published by the Free Software Foundation.
*/
/*
This file defines MACROS and static inlines used to emulate a function
call over asynchronous communication with remote server
*/
#ifndef _STACK_H
#define _STACK_H
#ifndef _CONFIG_H
#define _CONFIG_H
#include "config.h"
#endif
struct _call_stack_t;
typedef struct _call_stack_t call_stack_t;
struct _call_frame_t;
typedef struct _call_frame_t call_frame_t;
struct call_pool;
typedef struct call_pool call_pool_t;
#include <sys/time.h>
#include "xlator.h"
#include "dict.h"
#include "list.h"
#include "common-utils.h"
#include "globals.h"
#include "lkowner.h"
#include "client_t.h"
#define NFS_PID 1
#define LOW_PRIO_PROC_PID -1
typedef int32_t (*ret_fn_t) (call_frame_t *frame,
call_frame_t *prev_frame,
xlator_t *this,
int32_t op_ret,
int32_t op_errno,
...);
struct call_pool {
union {
struct list_head all_frames;
struct {
call_stack_t *next_call;
call_stack_t *prev_call;
} all_stacks;
};
int64_t cnt;
gf_lock_t lock;
struct mem_pool *frame_mem_pool;
struct mem_pool *stack_mem_pool;
};
struct _call_frame_t {
call_stack_t *root; /* stack root */
call_frame_t *parent; /* previous BP */
call_frame_t *next;
call_frame_t *prev; /* maintenance list */
void *local; /* local variables */
xlator_t *this; /* implicit object */
ret_fn_t ret; /* op_return address */
int32_t ref_count;
gf_lock_t lock;
void *cookie; /* unique cookie */
gf_boolean_t complete;
glusterfs_fop_t op;
struct timeval begin; /* when this frame was created */
struct timeval end; /* when this frame completed */
const char *wind_from;
const char *wind_to;
const char *unwind_from;
const char *unwind_to;
};
#define SMALL_GROUP_COUNT 128
struct _call_stack_t {
union {
struct list_head all_frames;
struct {
call_stack_t *next_call;
call_stack_t *prev_call;
};
};
call_pool_t *pool;
gf_lock_t stack_lock;
client_t *client;
uint64_t unique;
void *state; /* pointer to request state */
uid_t uid;
gid_t gid;
pid_t pid;
uint16_t ngrps;
uint32_t groups_small[SMALL_GROUP_COUNT];
uint32_t *groups_large;
uint32_t *groups;
gf_lkowner_t lk_owner;
glusterfs_ctx_t *ctx;
call_frame_t frames;
int32_t op;
int8_t type;
struct timeval tv;
};
#define frame_set_uid_gid(frm, u, g) \
do { \
if (frm) { \
(frm)->root->uid = u; \
(frm)->root->gid = g; \
(frm)->root->ngrps = 0; \
} \
} while (0); \
struct xlator_fops;
void
gf_latency_begin (call_frame_t *frame, void *fn);
void
gf_latency_end (call_frame_t *frame);
static inline void
FRAME_DESTROY (call_frame_t *frame)
{
void *local = NULL;
if (frame->next)
frame->next->prev = frame->prev;
if (frame->prev)
frame->prev->next = frame->next;
if (frame->local) {
local = frame->local;
frame->local = NULL;
}
LOCK_DESTROY (&frame->lock);
mem_put (frame);
if (local)
mem_put (local);
}
static inline void
STACK_DESTROY (call_stack_t *stack)
{
void *local = NULL;
LOCK (&stack->pool->lock);
{
list_del_init (&stack->all_frames);
stack->pool->cnt--;
}
UNLOCK (&stack->pool->lock);
if (stack->frames.local) {
local = stack->frames.local;
stack->frames.local = NULL;
}
LOCK_DESTROY (&stack->frames.lock);
LOCK_DESTROY (&stack->stack_lock);
while (stack->frames.next) {
FRAME_DESTROY (stack->frames.next);
}
GF_FREE (stack->groups_large);
mem_put (stack);
if (local)
mem_put (local);
}
static inline void
STACK_RESET (call_stack_t *stack)
{
void *local = NULL;
if (stack->frames.local) {
local = stack->frames.local;
stack->frames.local = NULL;
}
while (stack->frames.next) {
FRAME_DESTROY (stack->frames.next);
}
if (local)
mem_put (local);
}
#define cbk(x) cbk_##x
#define FRAME_SU_DO(frm, local_type) \
do { \
local_type *__local = (frm)->local; \
__local->uid = frm->root->uid; \
__local->gid = frm->root->gid; \
frm->root->uid = 0; \
frm->root->gid = 0; \
} while (0); \
#define FRAME_SU_UNDO(frm, local_type) \
do { \
local_type *__local = (frm)->local; \
frm->root->uid = __local->uid; \
frm->root->gid = __local->gid; \
} while (0); \
/* make a call */
#define STACK_WIND(frame, rfn, obj, fn, params ...) \
do { \
call_frame_t *_new = NULL; \
xlator_t *old_THIS = NULL; \
\
_new = mem_get0 (frame->root->pool->frame_mem_pool); \
if (!_new) { \
gf_log ("stack", GF_LOG_ERROR, "alloc failed"); \
break; \
} \
typeof(fn##_cbk) tmp_cbk = rfn; \
_new->root = frame->root; \
_new->this = obj; \
_new->ret = (ret_fn_t) tmp_cbk; \
_new->parent = frame; \
_new->cookie = _new; \
_new->wind_from = __FUNCTION__; \
_new->wind_to = #fn; \
_new->unwind_to = #rfn; \
\
LOCK_INIT (&_new->lock); \
LOCK(&frame->root->stack_lock); \
{ \
_new->next = frame->root->frames.next; \
_new->prev = &frame->root->frames; \
if (frame->root->frames.next) \
frame->root->frames.next->prev = _new; \
frame->root->frames.next = _new; \
frame->ref_count++; \
} \
UNLOCK(&frame->root->stack_lock); \
old_THIS = THIS; \
THIS = obj; \
if (frame->this->ctx->measure_latency) \
gf_latency_begin (_new, fn); \
fn (_new, obj, params); \
THIS = old_THIS; \
} while (0)
/* make a call without switching frames */
#define STACK_WIND_TAIL(frame, obj, fn, params ...) \
do { \
xlator_t *old_THIS = NULL; \
\
frame->this = obj; \
frame->wind_to = #fn; \
old_THIS = THIS; \
THIS = obj; \
fn (frame, obj, params); \
THIS = old_THIS; \
} while (0)
/* make a call with a cookie */
#define STACK_WIND_COOKIE(frame, rfn, cky, obj, fn, params ...) \
do { \
call_frame_t *_new = NULL; \
xlator_t *old_THIS = NULL; \
\
_new = mem_get0 (frame->root->pool->frame_mem_pool); \
if (!_new) { \
gf_log ("stack", GF_LOG_ERROR, "alloc failed"); \
break; \
} \
typeof(fn##_cbk) tmp_cbk = rfn; \
_new->root = frame->root; \
_new->this = obj; \
_new->ret = (ret_fn_t) tmp_cbk; \
_new->parent = frame; \
_new->cookie = cky; \
_new->wind_from = __FUNCTION__; \
_new->wind_to = #fn; \
_new->unwind_to = #rfn; \
LOCK_INIT (&_new->lock); \
LOCK(&frame->root->stack_lock); \
{ \
frame->ref_count++; \
_new->next = frame->root->frames.next; \
_new->prev = &frame->root->frames; \
if (frame->root->frames.next) \
frame->root->frames.next->prev = _new; \
frame->root->frames.next = _new; \
} \
UNLOCK(&frame->root->stack_lock); \
fn##_cbk = rfn; \
old_THIS = THIS; \
THIS = obj; \
if (obj->ctx->measure_latency) \
gf_latency_begin (_new, fn); \
fn (_new, obj, params); \
THIS = old_THIS; \
} while (0)
/* return from function */
#define STACK_UNWIND(frame, params ...) \
do { \
ret_fn_t fn = NULL; \
call_frame_t *_parent = NULL; \
xlator_t *old_THIS = NULL; \
if (!frame) { \
gf_log ("stack", GF_LOG_CRITICAL, "!frame"); \
break; \
} \
fn = frame->ret; \
_parent = frame->parent; \
LOCK(&frame->root->stack_lock); \
{ \
_parent->ref_count--; \
} \
UNLOCK(&frame->root->stack_lock); \
old_THIS = THIS; \
THIS = _parent->this; \
frame->complete = _gf_true; \
frame->unwind_from = __FUNCTION__; \
if (frame->this->ctx->measure_latency) \
gf_latency_end (frame); \
fn (_parent, frame->cookie, _parent->this, params); \
THIS = old_THIS; \
} while (0)
/* return from function in type-safe way */
#define STACK_UNWIND_STRICT(op, frame, params ...) \
do { \
fop_##op##_cbk_t fn = NULL; \
call_frame_t *_parent = NULL; \
xlator_t *old_THIS = NULL; \
\
if (!frame) { \
gf_log ("stack", GF_LOG_CRITICAL, "!frame"); \
break; \
} \
fn = (fop_##op##_cbk_t )frame->ret; \
_parent = frame->parent; \
LOCK(&frame->root->stack_lock); \
{ \
_parent->ref_count--; \
} \
UNLOCK(&frame->root->stack_lock); \
old_THIS = THIS; \
THIS = _parent->this; \
frame->complete = _gf_true; \
frame->unwind_from = __FUNCTION__; \
if (frame->this->ctx->measure_latency) \
gf_latency_end (frame); \
fn (_parent, frame->cookie, _parent->this, params); \
THIS = old_THIS; \
} while (0)
static inline int
call_stack_alloc_groups (call_stack_t *stack, int ngrps)
{
if (ngrps <= SMALL_GROUP_COUNT) {
stack->groups = stack->groups_small;
} else {
stack->groups_large = GF_CALLOC (sizeof (gid_t), ngrps,
gf_common_mt_groups_t);
if (!stack->groups_large)
return -1;
stack->groups = stack->groups_large;
}
stack->ngrps = ngrps;
return 0;
}
static inline call_frame_t *
copy_frame (call_frame_t *frame)
{
call_stack_t *newstack = NULL;
call_stack_t *oldstack = NULL;
if (!frame) {
return NULL;
}
newstack = mem_get0 (frame->root->pool->stack_mem_pool);
if (newstack == NULL) {
return NULL;
}
oldstack = frame->root;
newstack->uid = oldstack->uid;
newstack->gid = oldstack->gid;
newstack->pid = oldstack->pid;
newstack->ngrps = oldstack->ngrps;
newstack->op = oldstack->op;
newstack->type = oldstack->type;
if (call_stack_alloc_groups (newstack, oldstack->ngrps) != 0) {
mem_put (newstack);
return NULL;
}
memcpy (newstack->groups, oldstack->groups,
sizeof (gid_t) * oldstack->ngrps);
newstack->unique = oldstack->unique;
newstack->frames.this = frame->this;
newstack->frames.root = newstack;
newstack->pool = oldstack->pool;
newstack->lk_owner = oldstack->lk_owner;
newstack->ctx = oldstack->ctx;
if (newstack->ctx->measure_latency) {
if (gettimeofday (&newstack->tv, NULL) == -1)
gf_log ("stack", GF_LOG_ERROR, "gettimeofday () failed."
" (%s)", strerror (errno));
memcpy (&newstack->frames.begin, &newstack->tv,
sizeof (newstack->tv));
}
LOCK_INIT (&newstack->frames.lock);
LOCK_INIT (&newstack->stack_lock);
LOCK (&oldstack->pool->lock);
{
list_add (&newstack->all_frames, &oldstack->all_frames);
newstack->pool->cnt++;
}
UNLOCK (&oldstack->pool->lock);
return &newstack->frames;
}
void gf_proc_dump_pending_frames(call_pool_t *call_pool);
void gf_proc_dump_pending_frames_to_dict (call_pool_t *call_pool,
dict_t *dict);
call_frame_t *create_frame (xlator_t *xl, call_pool_t *pool);
gf_boolean_t __is_fuse_call (call_frame_t *frame);
#endif /* _STACK_H */
|