summaryrefslogtreecommitdiffstats
path: root/xlators/features/quota/src/quotad-aggregator.c
blob: 681fea4cb746ba98dc62391548c001eeea6e5b43 (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
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
/*
   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.
*/

#include "cli1-xdr.h"
#include "quota.h"
#include "quotad-helpers.h"
#include "quotad-aggregator.h"

struct rpcsvc_program quotad_aggregator_prog;

struct iobuf *
quotad_serialize_reply (rpcsvc_request_t *req, void *arg, struct iovec *outmsg,
                        xdrproc_t xdrproc)
{
        struct iobuf *iob      = NULL;
        ssize_t       retlen   = 0;
        ssize_t       xdr_size = 0;

        GF_VALIDATE_OR_GOTO ("server", req, ret);

        /* First, get the io buffer into which the reply in arg will
         * be serialized.
         */
        if (arg && xdrproc) {
                xdr_size = xdr_sizeof (xdrproc, arg);
                iob = iobuf_get2 (req->svc->ctx->iobuf_pool, xdr_size);
                if (!iob) {
                        gf_log_callingfn (THIS->name, GF_LOG_ERROR,
                                          "Failed to get iobuf");
                        goto ret;
                };

                iobuf_to_iovec (iob, outmsg);
                /* Use the given serializer to translate the give C structure in arg
                 * to XDR format which will be written into the buffer in outmsg.
                 */
                /* retlen is used to received the error since size_t is unsigned and we
                 * need -1 for error notification during encoding.
                 */

                retlen = xdr_serialize_generic (*outmsg, arg, xdrproc);
                if (retlen == -1) {
                        /* Failed to Encode 'GlusterFS' msg in RPC is not exactly
                           failure of RPC return values.. client should get
                           notified about this, so there are no missing frames */
                        gf_log_callingfn ("", GF_LOG_ERROR, "Failed to encode message");
                        req->rpc_err = GARBAGE_ARGS;
                        retlen = 0;
                }
        }
        outmsg->iov_len = retlen;
ret:
        if (retlen == -1) {
                iobuf_unref (iob);
                iob = NULL;
        }

        return iob;
}

int
quotad_aggregator_submit_reply (call_frame_t *frame, rpcsvc_request_t *req,
                                void *arg, struct iovec *payload,
                                int payloadcount, struct iobref *iobref,
                                xdrproc_t xdrproc)
{
        struct iobuf              *iob        = NULL;
        int                        ret        = -1;
        struct iovec               rsp        = {0,};
        quotad_aggregator_state_t *state      = NULL;
        char                       new_iobref = 0;

        GF_VALIDATE_OR_GOTO ("server", req, ret);

        if (frame) {
                state = frame->root->state;
                frame->local = NULL;
        }

        if (!iobref) {
                iobref = iobref_new ();
                if (!iobref) {
                        goto ret;
                }

                new_iobref = 1;
        }

        iob = quotad_serialize_reply (req, arg, &rsp, xdrproc);
        if (!iob) {
                gf_log ("", GF_LOG_ERROR, "Failed to serialize reply");
                goto ret;
        }

        iobref_add (iobref, iob);

        ret = rpcsvc_submit_generic (req, &rsp, 1, payload, payloadcount,
                                     iobref);

        iobuf_unref (iob);

        ret = 0;
ret:
        if (state) {
                quotad_aggregator_free_state (state);
        }

        if (frame)
                STACK_DESTROY (frame->root);

        if (new_iobref) {
                iobref_unref (iobref);
        }

        return ret;
}

int
quotad_aggregator_getlimit_cbk (xlator_t *this, call_frame_t *frame,
                                void *lookup_rsp)
{
        gfs3_lookup_rsp            *rsp    = lookup_rsp;
        gf_cli_rsp                 cli_rsp = {0,};
        dict_t                     *xdata  = NULL;
        quotad_aggregator_state_t  *state  = NULL;
        int                         ret    = -1;
        int                         type   = 0;

        GF_PROTOCOL_DICT_UNSERIALIZE (frame->this, xdata,
                                      (rsp->xdata.xdata_val),
                                      (rsp->xdata.xdata_len), rsp->op_ret,
                                      rsp->op_errno, out);

        if (xdata) {
                state = frame->root->state;
                ret = dict_get_int32 (state->xdata, "type", &type);
                if (ret < 0)
                        goto out;

                ret = dict_set_int32 (xdata, "type", type);
                if (ret < 0)
                        goto out;
        }

        ret = 0;
out:
        rsp->op_ret = ret;
        if (ret) {
                gf_log (this->name, GF_LOG_ERROR, "failed to unserialize "
                        "nameless lookup rsp");
                goto reply;
        }
        cli_rsp.op_ret = rsp->op_ret;
        cli_rsp.op_errno = rsp->op_errno;
        cli_rsp.op_errstr = "";
        if (xdata) {
                GF_PROTOCOL_DICT_SERIALIZE (frame->this, xdata,
                                            (&cli_rsp.dict.dict_val),
                                            (cli_rsp.dict.dict_len),
                                            cli_rsp.op_errno, reply);
        }

reply:
        quotad_aggregator_submit_reply (frame, frame->local, (void*)&cli_rsp, NULL, 0,
                                        NULL, (xdrproc_t)xdr_gf_cli_rsp);

        dict_unref (xdata);
        GF_FREE (cli_rsp.dict.dict_val);
        return 0;
}

int
quotad_aggregator_getlimit (rpcsvc_request_t *req)
{
        call_frame_t              *frame = NULL;
        gf_cli_req                 cli_req = {{0}, };
        gf_cli_rsp                 cli_rsp = {0};
        gfs3_lookup_req            args  = {{0,},};
        gfs3_lookup_rsp            rsp   = {0,};
        quotad_aggregator_state_t *state = NULL;
        xlator_t                  *this  = NULL;
        dict_t                    *dict  = NULL;
        int                        ret   = -1, op_errno = 0;
        char                      *gfid_str = NULL;
        uuid_t                     gfid = {0};

        GF_VALIDATE_OR_GOTO ("quotad-aggregator", req, err);

        this = THIS;

        ret = xdr_to_generic (req->msg[0], &cli_req, (xdrproc_t)xdr_gf_cli_req);
        if (ret < 0)  {
                //failed to decode msg;
                gf_log ("", GF_LOG_ERROR, "xdr decoding error");
                req->rpc_err = GARBAGE_ARGS;
                goto err;
        }

        if (cli_req.dict.dict_len) {
                dict = dict_new ();
                ret = dict_unserialize (cli_req.dict.dict_val,
                                        cli_req.dict.dict_len, &dict);
                if (ret < 0) {
                        gf_log (this->name, GF_LOG_ERROR, "Failed to "
                                "unserialize req-buffer to dictionary");
                        goto err;
                }
        }

        ret = dict_get_str (dict, "gfid", &gfid_str);
        if (ret) {
                goto err;
        }

        gf_uuid_parse ((const char*)gfid_str, gfid);

        frame = quotad_aggregator_get_frame_from_req (req);
        if (frame == NULL) {
                rsp.op_errno = ENOMEM;
                goto err;
        }
        state = frame->root->state;
        state->xdata = dict;

        ret = dict_set_int32 (state->xdata, QUOTA_LIMIT_KEY, 42);
        if (ret)
                goto err;

        ret = dict_set_int32 (state->xdata, QUOTA_LIMIT_OBJECTS_KEY, 42);
        if (ret) {
                gf_log (this->name, GF_LOG_ERROR, "Failed to set "
                        "QUOTA_LIMIT_OBJECTS_KEY");
                goto err;
        }

        ret = dict_set_int32 (state->xdata, QUOTA_SIZE_KEY, 42);
        if (ret)
                goto err;

        ret = dict_set_int32 (state->xdata, GET_ANCESTRY_PATH_KEY, 42);
        if (ret)
                goto err;

        memcpy (&args.gfid, &gfid, 16);

        args.bname           = alloca (req->msg[0].iov_len);
        args.xdata.xdata_val = alloca (req->msg[0].iov_len);

        ret = qd_nameless_lookup (this, frame, &args, state->xdata,
                                  quotad_aggregator_getlimit_cbk);
        if (ret) {
                rsp.op_errno = ret;
                goto err;
        }

        return ret;

err:
        cli_rsp.op_ret = -1;
        cli_rsp.op_errno = op_errno;
        cli_rsp.op_errstr = "";

        quotad_aggregator_getlimit_cbk (this, frame, &cli_rsp);
        if (dict)
                dict_unref (dict);

        return ret;
}

int
quotad_aggregator_lookup_cbk (xlator_t *this, call_frame_t *frame,
                              void *rsp)
{
        quotad_aggregator_submit_reply (frame, frame->local, rsp, NULL, 0, NULL,
                                        (xdrproc_t)xdr_gfs3_lookup_rsp);

        return 0;
}


int
quotad_aggregator_lookup (rpcsvc_request_t *req)
{
        call_frame_t              *frame = NULL;
        gfs3_lookup_req            args  = {{0,},};
        int                        ret   = -1, op_errno = 0;
        gfs3_lookup_rsp            rsp   = {0,};
        quotad_aggregator_state_t *state = NULL;
        xlator_t                  *this  = NULL;

        GF_VALIDATE_OR_GOTO ("quotad-aggregator", req, err);

        this = THIS;

        args.bname           = alloca (req->msg[0].iov_len);
        args.xdata.xdata_val = alloca (req->msg[0].iov_len);

        ret = xdr_to_generic (req->msg[0], &args,
                              (xdrproc_t)xdr_gfs3_lookup_req);
        if (ret < 0) {
                rsp.op_errno = EINVAL;
                goto err;
        }

        frame = quotad_aggregator_get_frame_from_req (req);
        if (frame == NULL) {
                rsp.op_errno = ENOMEM;
                goto err;
        }

        state = frame->root->state;

        GF_PROTOCOL_DICT_UNSERIALIZE (this, state->xdata,
                                      (args.xdata.xdata_val),
                                      (args.xdata.xdata_len), ret,
                                      op_errno, err);


        ret = qd_nameless_lookup (this, frame, &args, state->xdata,
                                  quotad_aggregator_lookup_cbk);
        if (ret) {
                rsp.op_errno = ret;
                goto err;
        }

        return ret;

err:
        rsp.op_ret = -1;
        rsp.op_errno = op_errno;

        quotad_aggregator_lookup_cbk (this, frame, &rsp);
        return ret;
}

int
quotad_aggregator_rpc_notify (rpcsvc_t *rpc, void *xl, rpcsvc_event_t event,
                              void *data)
{
        if (!xl || !data) {
                gf_log_callingfn ("server", GF_LOG_WARNING,
                                  "Calling rpc_notify without initializing");
                goto out;
        }

        switch (event) {
        case RPCSVC_EVENT_ACCEPT:
                break;

        case RPCSVC_EVENT_DISCONNECT:
                break;

        default:
                break;
        }

out:
        return 0;
}

int
quotad_aggregator_init (xlator_t *this)
{
        quota_priv_t *priv = NULL;
        int           ret  = -1;

        priv = this->private;

        ret = dict_set_str (this->options, "transport.address-family", "unix");
        if (ret)
                goto out;

        ret = dict_set_str (this->options, "transport-type", "socket");
        if (ret)
                goto out;

        ret = dict_set_str (this->options, "transport.socket.listen-path",
                            "/var/run/gluster/quotad.socket");
        if (ret)
                goto out;

        /* RPC related */
        priv->rpcsvc = rpcsvc_init (this, this->ctx, this->options, 0);
        if (priv->rpcsvc == NULL) {
                gf_log (this->name, GF_LOG_WARNING,
                        "creation of rpcsvc failed");
                ret = -1;
                goto out;
        }

        ret = rpcsvc_create_listeners (priv->rpcsvc, this->options,
                                       this->name);
        if (ret < 1) {
                gf_log (this->name, GF_LOG_WARNING,
                        "creation of listener failed");
                ret = -1;
                goto out;
        }

        priv->quotad_aggregator = &quotad_aggregator_prog;
        quotad_aggregator_prog.options = this->options;

        ret = rpcsvc_program_register (priv->rpcsvc, &quotad_aggregator_prog);
        if (ret) {
                gf_log (this->name, GF_LOG_WARNING,
                        "registration of program (name:%s, prognum:%d, "
                        "progver:%d) failed", quotad_aggregator_prog.progname,
                        quotad_aggregator_prog.prognum,
                        quotad_aggregator_prog.progver);
                goto out;
        }

        ret = 0;
out:
        return ret;
}

rpcsvc_actor_t quotad_aggregator_actors[GF_AGGREGATOR_MAXVALUE] = {
        [GF_AGGREGATOR_NULL]     = {"NULL", GF_AGGREGATOR_NULL, NULL, NULL, 0,
                                    DRC_NA},
        [GF_AGGREGATOR_LOOKUP]   = {"LOOKUP", GF_AGGREGATOR_NULL,
                                    quotad_aggregator_lookup, NULL, 0, DRC_NA},
        [GF_AGGREGATOR_GETLIMIT] = {"GETLIMIT", GF_AGGREGATOR_GETLIMIT,
                                   quotad_aggregator_getlimit, NULL, 0, DRC_NA},
};


struct rpcsvc_program quotad_aggregator_prog = {
        .progname  = "GlusterFS 3.3",
        .prognum   = GLUSTER_AGGREGATOR_PROGRAM,
        .progver   = GLUSTER_AGGREGATOR_VERSION,
        .numactors = GF_AGGREGATOR_MAXVALUE,
        .actors    = quotad_aggregator_actors
};