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
|
#!/usr/bin/python
from __future__ import print_function
import os
import sys
curdir = os.path.dirname(sys.argv[0])
gendir = os.path.join(curdir, '../../../../libglusterfs/src')
sys.path.append(gendir)
from generator import ops, fop_subs, cbk_subs, generate
FD_DATA_MODIFYING_OP_FOP_TEMPLATE = """
int32_t
cs_@NAME@ (call_frame_t *frame, xlator_t *this,
@LONG_ARGS@)
{
int op_errno = -1;
cs_local_t *local = NULL;
int ret = 0;
cs_inode_ctx_t *ctx = NULL;
gf_cs_obj_state state = -1;
VALIDATE_OR_GOTO (frame, err);
VALIDATE_OR_GOTO (this, err);
VALIDATE_OR_GOTO (fd, err);
local = cs_local_init (this, frame, NULL, fd, GF_FOP_@UPNAME@);
if (!local) {
gf_msg (this->name, GF_LOG_ERROR, 0, 0, "local init failed");
op_errno = ENOMEM;
goto err;
}
__cs_inode_ctx_get (this, fd->inode, &ctx);
if (ctx)
state = __cs_get_file_state (this, fd->inode, ctx);
else
state = GF_CS_LOCAL;
local->xattr_req = xdata ? dict_ref (xdata) : (xdata = dict_new ());
ret = dict_set_uint32 (local->xattr_req, GF_CS_OBJECT_STATUS, 1);
if (ret) {
gf_msg (this->name, GF_LOG_ERROR, 0, 0, "dict_set failed key:"
" %s", GF_CS_OBJECT_STATUS);
goto err;
}
local->stub = fop_@NAME@_stub (frame, cs_resume_@NAME@,
@SHORT_ARGS@);
if (!local->stub) {
gf_msg (this->name, GF_LOG_ERROR, 0, 0, "insufficient memory");
op_errno = ENOMEM;
goto err;
}
if (state == GF_CS_LOCAL) {
STACK_WIND (frame, cs_@NAME@_cbk,
FIRST_CHILD(this), FIRST_CHILD(this)->fops->@NAME@,
@SHORT_ARGS@);
} else {
local->call_cnt++;
ret = locate_and_execute (frame);
if (ret) {
op_errno = ENOMEM;
goto err;
}
}
return 0;
err:
CS_STACK_UNWIND (@NAME@, frame, -1, op_errno, @CBK_ERROR_ARGS@);
return 0;
}
"""
FD_DATA_MODIFYING_RESUME_OP_FOP_TEMPLATE = """
int32_t
cs_resume_@NAME@ (call_frame_t *frame, xlator_t *this,
@LONG_ARGS@)
{
int ret = 0;
ret = cs_resume_postprocess (this, frame, fd->inode);
if (ret) {
goto unwind;
}
cs_inodelk_unlock (frame);
STACK_WIND (frame, cs_@NAME@_cbk,
FIRST_CHILD(this), FIRST_CHILD(this)->fops->@NAME@,
@SHORT_ARGS@);
return 0;
unwind:
cs_inodelk_unlock (frame);
cs_common_cbk (frame);
return 0;
}
"""
FD_DATA_MODIFYING_OP_FOP_CBK_TEMPLATE = """
int32_t
cs_@NAME@_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
int32_t op_ret, int32_t op_errno,
@LONG_ARGS@)
{
cs_local_t *local = NULL;
int ret = 0;
uint64_t val = 0;
fd_t *fd = NULL;
local = frame->local;
fd = local->fd;
/* Do we need lock here? */
local->call_cnt++;
if (op_ret == -1) {
ret = dict_get_uint64 (xdata, GF_CS_OBJECT_STATUS, &val);
if (ret == 0) {
if (val == GF_CS_ERROR) {
gf_msg (this->name, GF_LOG_ERROR, 0, 0,
"could not get file state, unwinding");
op_ret = -1;
op_errno = EIO;
goto unwind;
} else {
__cs_inode_ctx_update (this, fd->inode, val);
gf_msg (this->name, GF_LOG_INFO, 0, 0,
" state = %ld", val);
if (local->call_cnt == 1 &&
(val == GF_CS_REMOTE ||
val == GF_CS_DOWNLOADING)) {
gf_msg (this->name, GF_LOG_INFO, 0,
0, " will repair and download "
"the file, current state : %ld",
val);
goto repair;
} else {
gf_msg (this->name, GF_LOG_ERROR, 0, 0,
"second @NAME@, Unwinding");
goto unwind;
}
}
} else {
gf_msg (this->name, GF_LOG_ERROR, 0, 0, "file state "
"could not be figured, unwinding");
goto unwind;
}
} else {
/* successful @NAME@ => file is local */
__cs_inode_ctx_update (this, fd->inode, GF_CS_LOCAL);
gf_msg (this->name, GF_LOG_INFO, 0, 0, "state : GF_CS_LOCAL"
", @NAME@ successful");
goto unwind;
}
repair:
ret = locate_and_execute (frame);
if (ret) {
goto unwind;
}
return 0;
unwind:
CS_STACK_UNWIND (@NAME@, frame, op_ret, op_errno, @SHORT_ARGS@);
return 0;
}
"""
LOC_STAT_OP_FOP_TEMPLATE = """
int32_t
cs_@NAME@ (call_frame_t *frame, xlator_t *this,
@LONG_ARGS@)
{
cs_local_t *local = NULL;
int ret = 0;
local = cs_local_init (this, frame, loc, NULL, GF_FOP_@UPNAME@);
if (!local) {
gf_msg (this->name, GF_LOG_ERROR, 0, 0, "local is NULL");
goto err;
}
if (loc->inode->ia_type == IA_IFDIR)
goto wind;
local->xattr_req = xdata ? dict_ref (xdata) : dict_new ();
ret = dict_set_uint32 (local->xattr_req, GF_CS_OBJECT_STATUS, 1);
if (ret) {
gf_msg (this->name, GF_LOG_ERROR, 0, 0, "dict_set failed key:"
" %s", GF_CS_OBJECT_STATUS);
goto err;
}
wind:
STACK_WIND (frame, cs_@NAME@_cbk, FIRST_CHILD(this),
FIRST_CHILD(this)->fops->@NAME@,
@SHORT_ARGS@);
return 0;
err:
CS_STACK_UNWIND (@NAME@, frame, -1, errno, @CBK_ERROR_ARGS@);
return 0;
}
"""
LOC_STAT_OP_FOP_CBK_TEMPLATE = """
int32_t
cs_@NAME@_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
int32_t op_ret, int32_t op_errno,
@LONG_ARGS@)
{
int ret = 0;
uint64_t val = 0;
loc_t *loc = NULL;
cs_local_t *local = NULL;
local = frame->local;
loc = &local->loc;
if (op_ret == 0) {
ret = dict_get_uint64 (xdata, GF_CS_OBJECT_STATUS, &val);
if (!ret) {
ret = __cs_inode_ctx_update (this, loc->inode, val);
if (ret) {
gf_msg (this->name, GF_LOG_ERROR, 0, 0,
"ctx update failed");
}
}
} else {
cs_inode_ctx_reset (this, loc->inode);
}
CS_STACK_UNWIND (@NAME@, frame, op_ret, op_errno, @SHORT_ARGS@);
return 0;
}
"""
# All xlator FOPs are covered in the following section just to create a clarity
# The lists themselves are not used.
entry_ops = ['mknod', 'mkdir', 'unlink', 'rmdir', 'symlink', 'rename', 'link',
'create']
special_ops = ['statfs', 'lookup', 'ipc', 'compound', 'icreate', 'namelink']
ignored_ops = ['getspec']
inode_ops = ['stat', 'readlink', 'truncate', 'open', 'setxattr', 'getxattr',
'removexattr', 'opendir', 'access', 'inodelk', 'entrylk',
'xattrop', 'setattr', 'lease', 'getactivelk', 'setactivelk',
'discover']
fd_ops = ['readv', 'writev', 'flush', 'fsync', 'fsyncdir', 'ftruncate',
'fstat', 'lk', 'readdir', 'finodelk', 'fentrylk', 'fxattrop',
'fsetxattr', 'fgetxattr', 'rchecksum', 'fsetattr', 'readdirp',
'fremovexattr', 'fallocate', 'discard', 'zerofill', 'seek']
# These are the current actual lists used to generate the code
# The following list contains fops which are fd based that modifies data
fd_data_modify_op_fop_template = ['readv', 'writev', 'flush', 'fsync',
'ftruncate', 'rchecksum', 'fallocate',
'discard', 'zerofill', 'seek']
# The following list contains fops which are entry based that does not change
# data
loc_stat_op_fop_template = ['lookup', 'stat', 'discover', 'access', 'setattr',
'getattr']
# These fops need a separate implementation
special_fops = ['readdirp', 'statfs', 'setxattr', 'unlink', 'getxattr',
'truncate', 'fstat']
def gen_defaults():
for name in ops:
if name in fd_data_modify_op_fop_template:
print(generate(FD_DATA_MODIFYING_OP_FOP_CBK_TEMPLATE, name, cbk_subs))
print(generate(FD_DATA_MODIFYING_RESUME_OP_FOP_TEMPLATE, name, fop_subs))
print(generate(FD_DATA_MODIFYING_OP_FOP_TEMPLATE, name, fop_subs))
elif name in loc_stat_op_fop_template:
print(generate(LOC_STAT_OP_FOP_CBK_TEMPLATE, name, cbk_subs))
print(generate(LOC_STAT_OP_FOP_TEMPLATE, name, fop_subs))
for l in open(sys.argv[1], 'r').readlines():
if l.find('#pragma generate') != -1:
print("/* BEGIN GENERATED CODE - DO NOT MODIFY */")
gen_defaults()
print("/* END GENERATED CODE */")
else:
print(l[:-1])
|