summaryrefslogtreecommitdiffstats
path: root/libglusterfs/src/stack.h
blob: e7c1cbd152db6dc90edb36cd6edc1c1a440b058d (plain)
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
/*
  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/>.
*/

/*
  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_t;
typedef struct _call_pool_t call_pool_t;

#include "xlator.h"
#include "dict.h"
#include "list.h"
#include "common-utils.h"
#include "globals.h"


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_t {
	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 _call_frame_t {
	call_stack_t *root;        /* stack root */
	call_frame_t *parent;      /* previous BP */
	call_frame_t *next;
	call_frame_t *prev;        /* maintainence 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 */
};

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;
	void                         *trans;
	uint64_t                      unique;
	void                         *state;  /* pointer to request state */
	uid_t                         uid;
	gid_t                         gid;
	pid_t                         pid;
	call_frame_t                  frames;

	int32_t                       op;
	int8_t                        type;
};


static inline void
FRAME_DESTROY (call_frame_t *frame)
{
	if (frame->next)
		frame->next->prev = frame->prev;
	if (frame->prev)
		frame->prev->next = frame->next;
	if (frame->local)
		FREE (frame->local);
	LOCK_DESTROY (&frame->lock);
	FREE (frame);
}


static inline void
STACK_DESTROY (call_stack_t *stack)
{
	LOCK (&stack->pool->lock);
	{
		list_del_init (&stack->all_frames);
		stack->pool->cnt--;
	}
	UNLOCK (&stack->pool->lock);

	if (stack->frames.local)
		FREE (stack->frames.local);

	LOCK_DESTROY (&stack->frames.lock);

	while (stack->frames.next) {
		FRAME_DESTROY (stack->frames.next);
	}
	FREE (stack);
}


#define cbk(x) cbk_##x


/* make a call */
#define STACK_WIND(frame, rfn, obj, fn, params ...)			\
	do {								\
		call_frame_t *_new = NULL;				\
                xlator_t     *old_THIS = NULL;                          \
		                                                        \
                _new = CALLOC (1, sizeof (call_frame_t));	        \
		ERR_ABORT (_new);					\
		typeof(fn##_cbk) tmp_cbk = rfn;				\
		_new->root = frame->root;				\
		_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;			\
		_new->this = obj;					\
		_new->ret = (ret_fn_t) tmp_cbk;				\
		_new->parent = frame;					\
		_new->cookie = _new;					\
		LOCK_INIT (&_new->lock);				\
		frame->ref_count++;					\
									\
                old_THIS = THIS;                                        \
                THIS = obj;                                             \
		fn (_new, 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 = CALLOC (1, sizeof (call_frame_t));               \
		ERR_ABORT (_new);					\
		typeof(fn##_cbk) tmp_cbk = rfn;				\
		_new->root = frame->root;				\
		_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;			\
		_new->this = obj;					\
		_new->ret = (ret_fn_t) tmp_cbk;				\
		_new->parent = frame;					\
		_new->cookie = cky;					\
		LOCK_INIT (&_new->lock);				\
		frame->ref_count++;					\
		fn##_cbk = rfn;						\
									\
                old_THIS = THIS;                                        \
                THIS = obj;                                             \
		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;                          \
                                                                        \
                fn = frame->ret;                                        \
                _parent = frame->parent;                                \
		_parent->ref_count--;					\
                old_THIS = THIS;                                        \
                THIS = _parent->this;                                   \
		fn (_parent, frame->cookie, _parent->this, params);	\
                THIS = old_THIS;                                        \
	} while (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 = (void *) CALLOC (1, sizeof (*newstack));
        if (newstack == NULL) {
                return NULL;
        }

	oldstack = frame->root;

	newstack->uid = oldstack->uid;
	newstack->gid = oldstack->gid;
	newstack->pid = oldstack->pid;
	newstack->unique = oldstack->unique;

	newstack->frames.this = frame->this;
	newstack->frames.root = newstack;
	newstack->pool = oldstack->pool;

	LOCK_INIT (&newstack->frames.lock);

	LOCK (&oldstack->pool->lock);
	{
		list_add (&newstack->all_frames, &oldstack->all_frames);
		newstack->pool->cnt++;
		
	}
	UNLOCK (&oldstack->pool->lock);

	return &newstack->frames;
}

static inline call_frame_t *
create_frame (xlator_t *xl, call_pool_t *pool)
{
	call_stack_t *stack = NULL;

	if (!xl || !pool) {
		return NULL;
	}

	stack = CALLOC (1, sizeof (*stack));
	if (!stack)
		return NULL;

	stack->pool = pool;
	stack->frames.root = stack;
	stack->frames.this = xl;

	LOCK (&pool->lock);
	{
		list_add (&stack->all_frames, &pool->all_frames);
		pool->cnt++;
	}
	UNLOCK (&pool->lock);

	LOCK_INIT (&stack->frames.lock);

	return &stack->frames;
}


#endif /* _STACK_H */