summaryrefslogtreecommitdiffstats
path: root/xlators/experimental/jbr-client/src/jbrc.c
blob: 9bb9346c5c0dc598d4a5de82418049791b77714c (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
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
/*
  Copyright (c) 2013 Red Hat, Inc. <http://www.redhat.com>

  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.
*/

#ifndef _CONFIG_H
#define _CONFIG_H
#include "config.h"
#endif

#include "call-stub.h"
#include "defaults.h"
#include "timer.h"
#include "xlator.h"
#include "jbr-messages.h"
#include "jbrc.h"
#include "statedump.h"

#define SCAR_LIMIT      20
#define HILITE(x)       (""x"")

/*
 * The fops are actually generated by gen-fops.py; the rest was mostly copied
 * from defaults.c (commit cd253754 on 27 August 2013).
 */

enum gf_dht_mem_types_ {
        gf_mt_jbrc_private_t = gf_common_mt_end + 1,
        gf_mt_jbrc_end
};

char            *JBRC_XATTR     = "user.jbr.active";

static inline
xlator_t *
ACTIVE_CHILD (xlator_t *parent)
{
        jbrc_private_t  *priv   = parent->private;

        return priv ? priv->active : FIRST_CHILD(parent);
}

xlator_t *
next_xlator (xlator_t *this, xlator_t *prev)
{
        xlator_list_t   *trav;

        for (trav = this->children; trav; trav = trav->next) {
                if (trav->xlator == prev) {
                        return trav->next ? trav->next->xlator
                                          : this->children->xlator;
                }
        }

        return NULL;
}

void
jbrc_retry_cb (void *cb_arg)
{
        jbrc_local_t    *local  = cb_arg;

        gf_msg (__func__, GF_LOG_INFO, 0, J_MSG_RETRY_MSG,
                HILITE("retrying %p"), local);
        call_resume_wind(local->stub);
}

#pragma generate

int32_t
jbrc_forget (xlator_t *this, inode_t *inode)
{
        gf_msg_callingfn (this->name, GF_LOG_WARNING, 0, J_MSG_INIT_FAIL,
                          "xlator does not implement forget_cbk");
        return 0;
}


int32_t
jbrc_releasedir (xlator_t *this, fd_t *fd)
{
        gf_msg_callingfn (this->name, GF_LOG_WARNING, 0, J_MSG_INIT_FAIL,
                          "xlator does not implement releasedir_cbk");
        return 0;
}

int32_t
jbrc_release (xlator_t *this, fd_t *fd)
{
        gf_msg_callingfn (this->name, GF_LOG_WARNING, 0, J_MSG_INIT_FAIL,
                          "xlator does not implement release_cbk");
        return 0;
}

struct xlator_fops fops = {
        .lookup         = jbrc_lookup,
        .stat           = jbrc_stat,
        .fstat          = jbrc_fstat,
        .truncate       = jbrc_truncate,
        .ftruncate      = jbrc_ftruncate,
        .access         = jbrc_access,
        .readlink       = jbrc_readlink,
        .mknod          = jbrc_mknod,
        .mkdir          = jbrc_mkdir,
        .unlink         = jbrc_unlink,
        .rmdir          = jbrc_rmdir,
        .symlink        = jbrc_symlink,
        .rename         = jbrc_rename,
        .link           = jbrc_link,
        .create         = jbrc_create,
        .open           = jbrc_open,
        .readv          = jbrc_readv,
        .writev         = jbrc_writev,
        .flush          = jbrc_flush,
        .fsync          = jbrc_fsync,
        .opendir        = jbrc_opendir,
        .readdir        = jbrc_readdir,
        .readdirp       = jbrc_readdirp,
        .fsyncdir       = jbrc_fsyncdir,
        .statfs         = jbrc_statfs,
        .setxattr       = jbrc_setxattr,
        .getxattr       = jbrc_getxattr,
        .fsetxattr      = jbrc_fsetxattr,
        .fgetxattr      = jbrc_fgetxattr,
        .removexattr    = jbrc_removexattr,
        .fremovexattr   = jbrc_fremovexattr,
        .lk             = jbrc_lk,
        .inodelk        = jbrc_inodelk,
        .finodelk       = jbrc_finodelk,
        .entrylk        = jbrc_entrylk,
        .fentrylk       = jbrc_fentrylk,
        .rchecksum      = jbrc_rchecksum,
        .xattrop        = jbrc_xattrop,
        .fxattrop       = jbrc_fxattrop,
        .setattr        = jbrc_setattr,
        .fsetattr       = jbrc_fsetattr,
	.fallocate	= jbrc_fallocate,
	.discard        = jbrc_discard,
};

struct xlator_cbks cbks = {
};


int32_t
mem_acct_init (xlator_t *this)
{
        int     ret = -1;

        GF_VALIDATE_OR_GOTO ("jbrc", this, out);

        ret = xlator_mem_acct_init (this, gf_mt_jbrc_end + 1);

        if (ret != 0) {
                gf_msg (this->name, GF_LOG_ERROR, ENOMEM, J_MSG_MEM_ERR,
                        "Memory accounting init failed");
                return ret;
        }
out:
        return ret;
}


int32_t
jbrc_init (xlator_t *this)
{
        jbrc_private_t  *priv   = NULL;
        xlator_list_t   *trav   = NULL;

        this->local_pool = mem_pool_new (jbrc_local_t, 128);
        if (!this->local_pool) {
                gf_msg (this->name, GF_LOG_ERROR, ENOMEM, J_MSG_MEM_ERR,
                        "failed to create jbrc_local_t pool");
                goto err;
        }

        priv = GF_CALLOC (1, sizeof (*priv), gf_mt_jbrc_private_t);
        if (!priv) {
                goto err;
        }

        for (trav = this->children; trav; trav = trav->next) {
                ++(priv->n_children);
        }

        priv->active = FIRST_CHILD(this);
        this->private = priv;
        return 0;

err:
        if (priv) {
                GF_FREE(priv);
        }
        return -1;
}

void
jbrc_fini (xlator_t *this)
{
        GF_FREE(this->private);
}

int
jbrc_get_child_index (xlator_t *this, xlator_t *kid)
{
        xlator_list_t   *trav;
        int             retval = -1;

        for (trav = this->children; trav; trav = trav->next) {
                ++retval;
                if (trav->xlator == kid) {
                        return retval;
                }
        }

        return -1;
}

uint8_t
jbrc_count_up_kids (jbrc_private_t *priv)
{
        uint8_t         retval  = 0;
        uint8_t         i;

        for (i = 0; i < priv->n_children; ++i) {
                if (priv->kid_state & (1 << i)) {
                        ++retval;
                }
        }

        return retval;
}

int32_t
jbrc_notify (xlator_t *this, int32_t event, void *data, ...)
{
        int32_t           ret        = 0;
        int32_t           index      = 0;
        jbrc_private_t   *priv       = NULL;

        GF_VALIDATE_OR_GOTO (THIS->name, this, out);
        priv = this->private;
        GF_VALIDATE_OR_GOTO (this->name, priv, out);

        switch (event) {
        case GF_EVENT_CHILD_UP:
                index = jbrc_get_child_index(this, data);
                if (index >= 0) {
                        priv->kid_state |= (1 << index);
                        priv->up_children = jbrc_count_up_kids(priv);
                        gf_msg (this->name, GF_LOG_INFO, 0, J_MSG_GENERIC,
                                "got CHILD_UP for %s, now %u kids",
                                ((xlator_t *)data)->name,
                                priv->up_children);
                }
                ret = default_notify (this, event, data);
                break;
        case GF_EVENT_CHILD_DOWN:
                index = jbrc_get_child_index(this, data);
                if (index >= 0) {
                        priv->kid_state &= ~(1 << index);
                        priv->up_children = jbrc_count_up_kids(priv);
                        gf_msg (this->name, GF_LOG_INFO, 0, J_MSG_GENERIC,
                                "got CHILD_DOWN for %s, now %u kids",
                                ((xlator_t *)data)->name,
                                priv->up_children);
                }
                break;
        default:
                ret = default_notify (this, event, data);
        }

out:
        return ret;
}

int
jbrc_priv_dump (xlator_t *this)
{
        jbrc_private_t     *priv = NULL;
        char                key_prefix[GF_DUMP_MAX_BUF_LEN];
        xlator_list_t      *trav = NULL;
        int32_t             i    = -1;

        GF_VALIDATE_OR_GOTO (THIS->name, this, out);
        priv = this->private;
        GF_VALIDATE_OR_GOTO (this->name, priv, out);

        snprintf(key_prefix, GF_DUMP_MAX_BUF_LEN, "%s.%s",
                 this->type, this->name);
        gf_proc_dump_add_section(key_prefix);

        gf_proc_dump_write("up_children", "%u", priv->up_children);

        for (trav = this->children, i = 0; trav; trav = trav->next, i++) {
                snprintf(key_prefix, GF_DUMP_MAX_BUF_LEN, "child_%d", i);
                gf_proc_dump_write(key_prefix, "%s", trav->xlator->name);
        }

out:
        return 0;
}

struct xlator_dumpops dumpops = {
        .priv       = jbrc_priv_dump,
};

class_methods_t class_methods = {
        .init           = jbrc_init,
        .fini           = jbrc_fini,
        .notify         = jbrc_notify,
};

struct volume_options options[] = {
	{ .key = {NULL} },
};