glusterfs

Форк
0
/
cloudsync-fops-c.py 
324 строки · 10.5 Кб
1
#!/usr/bin/python3
2

3
from __future__ import print_function
4
import os
5
import sys
6

7
curdir = os.path.dirname(sys.argv[0])
8
gendir = os.path.join(curdir, '../../../../libglusterfs/src')
9
sys.path.append(gendir)
10
from generator import ops, fop_subs, cbk_subs, generate
11

12
FD_DATA_MODIFYING_OP_FOP_TEMPLATE = """
13
int32_t
14
cs_@NAME@ (call_frame_t *frame, xlator_t *this,
15
           @LONG_ARGS@)
16
{
17
        int                         op_errno        = EINVAL ;
18
        cs_local_t                 *local           = NULL;
19
        int                         ret             = 0;
20
        cs_inode_ctx_t             *ctx             = NULL;
21
        gf_cs_obj_state             state           = -1;
22

23
        VALIDATE_OR_GOTO (frame, err);
24
        VALIDATE_OR_GOTO (this, err);
25
        VALIDATE_OR_GOTO (fd, err);
26

27
        local = cs_local_init (this, frame, NULL, fd, GF_FOP_@UPNAME@);
28
        if (!local) {
29

30
                gf_msg (this->name, GF_LOG_ERROR, 0, 0, "local init failed");
31
                op_errno = ENOMEM;
32
                goto err;
33
        }
34

35
        __cs_inode_ctx_get (this, fd->inode, &ctx);
36

37
        if (ctx)
38
                state = __cs_get_file_state (fd->inode, ctx);
39
        else
40
                state = GF_CS_LOCAL;
41

42
        xdata = xdata ? dict_ref (xdata) : dict_new ();
43

44
        if (!xdata) {
45
                gf_msg (this->name, GF_LOG_ERROR, 0, 0, "insufficient memory");
46
                op_errno = ENOMEM;
47
                goto err;
48
        }
49

50
        local->xattr_req = xdata;
51

52
        ret = dict_set_uint32 (local->xattr_req, GF_CS_OBJECT_STATUS, 1);
53
        if (ret) {
54
                gf_msg (this->name, GF_LOG_ERROR, 0, 0, "dict_set failed key:"
55
                        " %s", GF_CS_OBJECT_STATUS);
56
                goto err;
57
        }
58

59
        local->stub = fop_@NAME@_stub (frame, cs_resume_@NAME@,
60
                                       @SHORT_ARGS@);
61
        if (!local->stub) {
62
                gf_msg (this->name, GF_LOG_ERROR, 0, 0, "insufficient memory");
63
                op_errno = ENOMEM;
64
                goto err;
65
        }
66

67

68
        if (state == GF_CS_LOCAL) {
69
                STACK_WIND (frame, cs_@NAME@_cbk,
70
                            FIRST_CHILD(this), FIRST_CHILD(this)->fops->@NAME@,
71
                            @SHORT_ARGS@);
72
        } else {
73
                local->call_cnt++;
74
                ret = locate_and_execute (frame);
75
                if (ret) {
76
                        op_errno = ENOMEM;
77
                        goto err;
78
                }
79
        }
80

81
        return 0;
82

83
err:
84
        CS_STACK_UNWIND (@NAME@, frame, -1, op_errno, @CBK_ERROR_ARGS@);
85

86
        return 0;
87
}
88
"""
89

90
FD_DATA_MODIFYING_RESUME_OP_FOP_TEMPLATE = """
91
int32_t
92
cs_resume_@NAME@ (call_frame_t *frame, xlator_t *this,
93
                  @LONG_ARGS@)
94
{
95
        int              ret    = 0;
96

97
        ret = cs_resume_postprocess (this, frame, fd->inode);
98
        if (ret) {
99
                goto unwind;
100
        }
101

102
        cs_inodelk_unlock (frame);
103

104
        STACK_WIND (frame, cs_@NAME@_cbk,
105
                    FIRST_CHILD(this), FIRST_CHILD(this)->fops->@NAME@,
106
                    @SHORT_ARGS@);
107

108
        return 0;
109

110
unwind:
111

112
        cs_inodelk_unlock (frame);
113

114
        cs_common_cbk (frame);
115

116
        return 0;
117
}
118
"""
119
FD_DATA_MODIFYING_OP_FOP_CBK_TEMPLATE = """
120
int32_t
121
cs_@NAME@_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
122
               int32_t op_ret, int32_t op_errno,
123
               @LONG_ARGS@)
124
{
125
        cs_local_t      *local = NULL;
126
        int              ret = 0;
127
        uint64_t         val = 0;
128
        fd_t            *fd = NULL;
129

130
        local = frame->local;
131
        fd = local->fd;
132

133
        /* Do we need lock here? */
134
        local->call_cnt++;
135

136
        if (op_ret == -1) {
137
                ret = dict_get_uint64 (xdata, GF_CS_OBJECT_STATUS, &val);
138
                if (ret == 0) {
139
                        if (val == GF_CS_ERROR) {
140
                                gf_msg (this->name, GF_LOG_ERROR, 0, 0,
141
                                        "could not get file state, unwinding");
142
                                op_ret = -1;
143
                                op_errno = EIO;
144
                                goto unwind;
145
                        } else {
146
                                __cs_inode_ctx_update (this, fd->inode, val);
147
                                gf_msg (this->name, GF_LOG_INFO, 0, 0,
148
                                        " state = %" PRIu64, val);
149

150
                                if (local->call_cnt == 1 &&
151
                                    (val == GF_CS_REMOTE ||
152
                                     val == GF_CS_DOWNLOADING))  {
153
                                        gf_msg (this->name, GF_LOG_INFO, 0,
154
                                                0, " will repair and download "
155
                                                "the file, current state : %"
156
                                                PRIu64, val);
157
                                        goto repair;
158
                                } else {
159
                                        gf_msg (this->name, GF_LOG_ERROR, 0, 0,
160
                                                "second @NAME@, Unwinding");
161
                                        goto unwind;
162
                                }
163
                        }
164
                } else {
165
                        gf_msg (this->name, GF_LOG_ERROR, 0, 0, "file state "
166
                                "could not be figured, unwinding");
167
                        goto unwind;
168
                }
169
        } else {
170
                /* successful @NAME@ => file is local */
171
                __cs_inode_ctx_update (this, fd->inode, GF_CS_LOCAL);
172
                gf_msg (this->name, GF_LOG_INFO, 0, 0, "state : GF_CS_LOCAL"
173
                        ", @NAME@ successful");
174

175
                goto unwind;
176
        }
177

178
repair:
179
        ret = locate_and_execute (frame);
180
        if (ret) {
181
                goto unwind;
182
        }
183

184
        return 0;
185

186
unwind:
187
        CS_STACK_UNWIND (@NAME@, frame, op_ret, op_errno, @SHORT_ARGS@);
188

189
        return 0;
190
}
191
"""
192

193
LOC_STAT_OP_FOP_TEMPLATE = """
194
int32_t
195
cs_@NAME@ (call_frame_t *frame, xlator_t *this,
196
           @LONG_ARGS@)
197
{
198
        int              op_errno = EINVAL;
199
        cs_local_t      *local = NULL;
200
        int              ret   = 0;
201

202
        local = cs_local_init (this, frame, loc, NULL, GF_FOP_@UPNAME@);
203
        if (!local) {
204
                gf_msg (this->name, GF_LOG_ERROR, 0, 0, "local is NULL");
205
                op_errno = ENOMEM;
206
                goto err;
207
        }
208

209
        if (loc->inode->ia_type == IA_IFDIR)
210
                goto wind;
211

212
        xdata = xdata ? dict_ref (xdata) : dict_new ();
213

214
        if (!xdata) {
215
                gf_msg (this->name, GF_LOG_ERROR, 0, 0, "insufficient memory");
216
                op_errno = ENOMEM;
217
                goto err;
218
        }
219

220
        local->xattr_req = xdata;
221

222
        ret = dict_set_uint32 (local->xattr_req, GF_CS_OBJECT_STATUS, 1);
223
        if (ret) {
224
                gf_msg (this->name, GF_LOG_ERROR, 0, 0, "dict_set failed key:"
225
                        " %s", GF_CS_OBJECT_STATUS);
226
                goto err;
227
        }
228

229
wind:
230
        STACK_WIND (frame, cs_@NAME@_cbk, FIRST_CHILD(this),
231
                    FIRST_CHILD(this)->fops->@NAME@,
232
                    @SHORT_ARGS@);
233

234
        return 0;
235
err:
236
        CS_STACK_UNWIND (@NAME@, frame, -1, op_errno, @CBK_ERROR_ARGS@);
237

238
        return 0;
239
}
240
"""
241

242
LOC_STAT_OP_FOP_CBK_TEMPLATE = """
243
int32_t
244
cs_@NAME@_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
245
               int32_t op_ret, int32_t op_errno,
246
               @LONG_ARGS@)
247
{
248
        int              ret = 0;
249
        uint64_t         val = 0;
250
        loc_t           *loc = NULL;
251
        cs_local_t      *local = NULL;
252

253
        local = frame->local;
254

255
        loc = &local->loc;
256

257
        if (op_ret == 0) {
258
                ret = dict_get_uint64 (xdata, GF_CS_OBJECT_STATUS, &val);
259
                if (!ret) {
260
                        ret = __cs_inode_ctx_update (this, loc->inode, val);
261
                        if (ret) {
262
                                gf_msg (this->name, GF_LOG_ERROR, 0, 0,
263
                                        "ctx update failed");
264
                        }
265
                }
266
        } else {
267
                cs_inode_ctx_reset (this, loc->inode);
268
        }
269

270
        CS_STACK_UNWIND (@NAME@, frame, op_ret, op_errno, @SHORT_ARGS@);
271

272
        return 0;
273
}
274
"""
275

276
# All xlator FOPs are covered in the following section just to create a clarity
277
# The lists themselves are not used.
278
entry_ops = ['mknod', 'mkdir', 'unlink', 'rmdir', 'symlink', 'rename', 'link',
279
             'create']
280
special_ops = ['statfs', 'lookup', 'ipc', 'compound', 'icreate', 'namelink']
281
ignored_ops = ['getspec']
282
inode_ops = ['stat', 'readlink', 'truncate', 'open', 'setxattr', 'getxattr',
283
             'removexattr', 'opendir', 'access', 'inodelk', 'entrylk',
284
             'xattrop', 'setattr', 'lease', 'getactivelk', 'setactivelk',
285
             'discover']
286
fd_ops = ['readv', 'writev', 'flush', 'fsync', 'fsyncdir', 'ftruncate',
287
          'fstat', 'lk', 'readdir', 'finodelk', 'fentrylk', 'fxattrop',
288
          'fsetxattr', 'fgetxattr', 'rchecksum', 'fsetattr', 'readdirp',
289
          'fremovexattr', 'fallocate', 'discard', 'zerofill', 'seek']
290

291

292
# These are the current actual lists used to generate the code
293

294
# The following list contains fops which are fd based that modifies data
295
fd_data_modify_op_fop_template = ['writev', 'flush', 'fsync',
296
                                  'ftruncate', 'rchecksum', 'fallocate',
297
                                  'discard', 'zerofill', 'seek']
298

299
# The following list contains fops which are entry based that does not change
300
# data
301
loc_stat_op_fop_template = ['lookup', 'stat', 'discover', 'access', 'setattr',
302
                            'getattr']
303

304
# These fops need a separate implementation
305
special_fops = ['statfs', 'setxattr', 'unlink', 'getxattr',
306
                'truncate', 'fstat', 'readv', 'readdirp']
307

308
def gen_defaults():
309
    for name in ops:
310
        if name in fd_data_modify_op_fop_template:
311
            print(generate(FD_DATA_MODIFYING_OP_FOP_CBK_TEMPLATE, name, cbk_subs))
312
            print(generate(FD_DATA_MODIFYING_RESUME_OP_FOP_TEMPLATE, name, fop_subs))
313
            print(generate(FD_DATA_MODIFYING_OP_FOP_TEMPLATE, name, fop_subs))
314
        elif name in loc_stat_op_fop_template:
315
            print(generate(LOC_STAT_OP_FOP_CBK_TEMPLATE, name, cbk_subs))
316
            print(generate(LOC_STAT_OP_FOP_TEMPLATE, name, fop_subs))
317

318
for l in open(sys.argv[1], 'r').readlines():
319
    if l.find('#pragma generate') != -1:
320
        print("/* BEGIN GENERATED CODE - DO NOT MODIFY */")
321
        gen_defaults()
322
        print("/* END GENERATED CODE */")
323
    else:
324
        print(l[:-1])
325

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.