summaryrefslogtreecommitdiffstats
path: root/libglusterfs/src/graph.c
blob: a0c4c122fe7cfce3e224e4585134cc043c060fbd (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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
/*
  Copyright (c) 2010 Gluster, Inc. <http://www.gluster.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/>.
*/

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

#include "xlator.h"
#include <dlfcn.h>
#include <netdb.h>
#include <fnmatch.h>
#include "defaults.h"




#if 0
static void
_gf_dump_details (int argc, char **argv)
{
        extern FILE *gf_log_logfile;
        int          i = 0;
        char         timestr[256];
        time_t       utime = 0;
        struct tm   *tm = NULL;
        pid_t        mypid = 0;
        struct utsname uname_buf = {{0, }, };
        int            uname_ret = -1;

        utime = time (NULL);
        tm    = localtime (&utime);
        mypid = getpid ();
        uname_ret   = uname (&uname_buf);

        /* Which git? What time? */
        strftime (timestr, 256, "%Y-%m-%d %H:%M:%S", tm);
        fprintf (gf_log_logfile,
                 "========================================"
                 "========================================\n");
        fprintf (gf_log_logfile, "Version      : %s %s built on %s %s\n",
                 PACKAGE_NAME, PACKAGE_VERSION, __DATE__, __TIME__);
        fprintf (gf_log_logfile, "git: %s\n",
                 GLUSTERFS_REPOSITORY_REVISION);
        fprintf (gf_log_logfile, "Starting Time: %s\n", timestr);
        fprintf (gf_log_logfile, "Command line : ");
        for (i = 0; i < argc; i++) {
                fprintf (gf_log_logfile, "%s ", argv[i]);
        }

        fprintf (gf_log_logfile, "\nPID          : %d\n", mypid);

        if (uname_ret == 0) {
                fprintf (gf_log_logfile, "System name  : %s\n",
                         uname_buf.sysname);
                fprintf (gf_log_logfile, "Nodename     : %s\n",
                         uname_buf.nodename);
                fprintf (gf_log_logfile, "Kernel Release : %s\n",
                         uname_buf.release);
                fprintf (gf_log_logfile, "Hardware Identifier: %s\n",
                         uname_buf.machine);
        }


        fprintf (gf_log_logfile, "\n");
        fflush (gf_log_logfile);
}
#endif


static int
_log_if_option_is_invalid (xlator_t *xl, data_pair_t *pair)
{
        volume_opt_list_t *vol_opt = NULL;
        volume_option_t   *opt     = NULL;
        int                i     = 0;
        int                index = 0;
        int                found = 0;

        /* Get the first volume_option */
        list_for_each_entry (vol_opt, &xl->volume_options, list) {
                /* Warn for extra option */
                if (!vol_opt->given_opt)
                        break;

                opt = vol_opt->given_opt;
                for (index = 0;
                     ((index < ZR_OPTION_MAX_ARRAY_SIZE) &&
                      (opt[index].key && opt[index].key[0]));  index++)
                        for (i = 0; (i < ZR_VOLUME_MAX_NUM_KEY) &&
                                     opt[index].key[i]; i++) {
                                if (fnmatch (opt[index].key[i],
                                             pair->key,
                                             FNM_NOESCAPE) == 0) {
                                        found = 1;
                                        break;
                                }
                        }
        }

        if (!found) {
                gf_log (xl->name, GF_LOG_WARNING,
                        "option '%s' is not recognized",
                        pair->key);
        }
        return 0;
}


int
glusterfs_graph_insert (glusterfs_graph_t *graph, glusterfs_ctx_t *ctx,
                        const char *type, const char *name)
{
        xlator_t        *ixl = NULL;
        xlator_list_t   *xlchild = NULL;
        xlator_list_t   *xlparent = NULL;

        if (!ctx->master) {
                gf_log ("glusterfs", GF_LOG_ERROR,
                        "volume \"%s\" can be added from command line only "
                        "on client side", type);

                return -1;
        }

        ixl = GF_CALLOC (1, sizeof (*ixl), gf_common_mt_xlator_t);
        if (!ixl)
                return -1;

        ixl->ctx      = ctx;
        ixl->graph    = graph;
        ixl->options  = get_new_dict ();
        if (!ixl->options)
                goto err;

        ixl->name  = gf_strdup (name);
        if (!ixl->name)
                goto err;

        if (xlator_set_type (ixl, type) == -1) {
                gf_log ("glusterfs", GF_LOG_ERROR,
                        "%s (%s) initialization failed",
                        name, type);
                return -1;
        }


        /* children */
        xlchild = GF_CALLOC (sizeof (*xlchild), 1, gf_common_mt_xlator_list_t);
        if (!xlchild)
                goto err;
        xlchild->xlator = graph->top;
        ixl->children = xlchild; xlchild = NULL;


        /* parent */
        xlparent = GF_CALLOC (sizeof (*xlparent), 1,
                              gf_common_mt_xlator_list_t);
        if (!xlparent)
                goto err;
        xlparent->xlator = ixl;

        ixl->next = graph->first;
        graph->first = ixl;

        xlparent->next = ((xlator_t *)graph->top)->parents;
        ((xlator_t *)graph->top)->parents = xlparent;

        graph->top = ixl;

        graph->xl_count++;

        return 0;
err:
        xlator_destroy (ixl);
        return -1;
}


int
glusterfs_set_master (glusterfs_graph_t *graph, glusterfs_ctx_t *ctx)
{
        xlator_t *trav = NULL;
        xlator_list_t *chp = NULL, **chpp = NULL;

        for (trav = graph->first; trav; trav = trav->next) {
                if (strcmp (trav->type, "mount/fuse") != 0)
                        continue;

                if (ctx->master) {
                        gf_log ("graph", GF_LOG_ERROR,
                                "there can be at most one fuse volume");

                        return -1;
                }

                if (!trav->children || trav->children->next) {
                        gf_log ("graph", GF_LOG_ERROR,
                                "fuse volume not configured with exactly one "
                                "child");
                        return -1;
                }

                if (xlator_has_parent (trav)) {
                        gf_log ("graph", GF_LOG_ERROR,
                                "fuse volume cannot have parents");

                        return -1;
                }

                ctx->master = trav;
                if (trav == graph->top)
                        graph->top = trav->children->xlator;
                for (chpp = &trav->children->xlator->parents;
                     *chpp;
                     chpp = &(*chpp)->next) {
                        if ((*chpp)->xlator == trav) {
                                chp = *chpp;
                                *chpp = chp->next;
                                GF_FREE (chp);
                                if (!*chpp)
                                        break;
                        }
                }
                if (trav == graph->first)
                        graph->first = trav->next;
                if (trav->prev)
                        trav->prev->next = trav->next;
        }

        return 0;
}


int
glusterfs_graph_readonly (glusterfs_graph_t *graph, glusterfs_ctx_t *ctx)
{
        int ret = 0;
        cmd_args_t      *cmd_args = NULL;

        cmd_args = &ctx->cmd_args;

        if (!cmd_args->read_only)
                return 0;

        ret = glusterfs_graph_insert (graph, ctx, "features/read-only",
                                      "readonly-autoload");
        return ret;
}


int
glusterfs_graph_mac_compat (glusterfs_graph_t *graph, glusterfs_ctx_t *ctx)
{
        int ret = 0;
        cmd_args_t      *cmd_args = NULL;

        cmd_args = &ctx->cmd_args;

        if (cmd_args->mac_compat == GF_OPTION_DISABLE)
                return 0;

        ret = glusterfs_graph_insert (graph, ctx, "features/mac-compat",
                                      "mac-compat-autoload");

        return ret;
}


static void
gf_add_cmdline_options (glusterfs_graph_t *graph, cmd_args_t *cmd_args)
{
        int                      ret = 0;
        xlator_t                *trav = NULL;
        xlator_cmdline_option_t *cmd_option = NULL;

        trav = graph->first;

        while (trav) {
                list_for_each_entry (cmd_option,
                                     &cmd_args->xlator_options, cmd_args) {
                        if (!fnmatch (cmd_option->volume,
                                      trav->name, FNM_NOESCAPE)) {
                                ret = dict_set_str (trav->options,
                                                    cmd_option->key,
                                                    cmd_option->value);
                                if (ret == 0) {
                                        gf_log (trav->name, GF_LOG_WARNING,
                                                "adding option '%s' for "
                                                "volume '%s' with value '%s'",
                                                cmd_option->key, trav->name,
                                                cmd_option->value);
                                } else {
                                        gf_log (trav->name, GF_LOG_WARNING,
                                                "adding option '%s' for "
                                                "volume '%s' failed: %s",
                                                cmd_option->key, trav->name,
                                                strerror (-ret));
                                }
                        }
                }
                trav = trav->next;
        }
}


int
glusterfs_graph_validate_options (glusterfs_graph_t *graph)
{
        volume_opt_list_t  *vol_opt = NULL;
        xlator_t           *trav = NULL;
        int                 ret = -1;

        trav = graph->first;

        while (trav) {
                if (list_empty (&trav->volume_options))
                        continue;

                vol_opt = list_entry (trav->volume_options.next,
                                      volume_opt_list_t, list);

                ret = validate_xlator_volume_options (trav,
                                                      vol_opt->given_opt);
                if (ret) {
                        gf_log (trav->name, GF_LOG_ERROR,
                                "validating translator failed");
                        return ret;
                }
                trav = trav->next;
        }

        return 0;
}


int
glusterfs_graph_init (glusterfs_graph_t *graph)
{
        xlator_t           *trav = NULL;
        int                 ret = -1;

        trav = graph->first;

        while (trav) {
                ret = xlator_init (trav);
                if (ret) {
                        gf_log (trav->name, GF_LOG_ERROR,
                                "initializing translator failed");
                        return ret;
                }
                trav = trav->next;
        }

        return 0;
}


int
glusterfs_graph_unknown_options (glusterfs_graph_t *graph)
{
        data_pair_t        *pair = NULL;
        xlator_t           *trav = NULL;

        trav = graph->first;

        /* Validate again phase */
        while (trav) {
                pair = trav->options->members_list;
                while (pair) {
                        _log_if_option_is_invalid (trav, pair);
                        pair = pair->next;
                }
                trav = trav->next;
        }

        return 0;
}


void
fill_uuid (char *uuid, int size)
{
        char           hostname[256] = {0,};
        struct timeval tv = {0,};
        struct tm      now = {0, };
        char           now_str[32];

        if (gettimeofday (&tv, NULL) == -1) {
                gf_log ("graph", GF_LOG_ERROR,
                        "gettimeofday: failed %s",
                        strerror (errno));
        }

        if (gethostname (hostname, 256) == -1) {
                gf_log ("graph", GF_LOG_ERROR,
                        "gethostname: failed %s",
                        strerror (errno));
        }

        localtime_r (&tv.tv_sec, &now);
        strftime (now_str, 32, "%Y/%m/%d-%H:%M:%S", &now);
        snprintf (uuid, size, "%s-%d-%s:%"GF_PRI_SUSECONDS,
                  hostname, getpid(), now_str, tv.tv_usec);

        return;
}


int
glusterfs_graph_settop (glusterfs_graph_t *graph, glusterfs_ctx_t *ctx)
{
        const char *volume_name = NULL;
        xlator_t   *trav = NULL;

        volume_name = ctx->cmd_args.volume_name;

        if (!volume_name) {
                graph->top = graph->first;
                return 0;
        }

        for (trav = graph->first; trav; trav = trav->next) {
                if (strcmp (trav->name, volume_name) == 0) {
                        if (strcmp (trav->type, "mount/fuse") == 0) {
                                gf_log ("graph", GF_LOG_ERROR,
                                        "fuse volume cannot be set as top");
                                break;
                        }
                        graph->top = trav;
                        return 0;
                }
        }

        return -1;
}


int
glusterfs_graph_parent_up (glusterfs_graph_t *graph)
{
        xlator_t *trav = NULL;
        int       ret = -1;

        trav = graph->first;

        while (trav) {
                if (!xlator_has_parent (trav)) {
                        ret = xlator_notify (trav, GF_EVENT_PARENT_UP, trav);
                }

                if (ret)
                        break;

                trav = trav->next;
        }

        return ret;
}


int
glusterfs_graph_prepare (glusterfs_graph_t *graph, glusterfs_ctx_t *ctx)
{
        xlator_t    *trav = NULL;
        int          ret = 0;

        /* XXX: CHECKSUM */

        /* XXX: attach to -n volname */
        ret = glusterfs_graph_settop (graph, ctx);
        if (ret)
                return -1;

        /* XXX: take fuse from volfile  */
        ret = glusterfs_set_master (graph, ctx);
        if (ret)
                return -1;

        /* XXX: RO VOLUME */
        ret = glusterfs_graph_readonly (graph, ctx);
        if (ret)
                return -1;

        /* XXX: MAC COMPAT */
        ret = glusterfs_graph_mac_compat (graph, ctx);
        if (ret)
                return -1;

        /* XXX: this->ctx setting */
        for (trav = graph->first; trav; trav = trav->next) {
                trav->ctx = ctx;
        }

        /* XXX: DOB setting */
        gettimeofday (&graph->dob, NULL);

        fill_uuid (graph->graph_uuid, 128);

        /* XXX: --xlator-option additions */
        gf_add_cmdline_options (graph, &ctx->cmd_args);


        return 0;
}


int
glusterfs_graph_activate (glusterfs_graph_t *graph, glusterfs_ctx_t *ctx)
{
        int ret = 0;

        /* XXX: all xlator options validation */
        ret = glusterfs_graph_validate_options (graph);
        if (ret)
                return ret;

        /* XXX: perform init () */
        ret = glusterfs_graph_init (graph);
        if (ret)
                return ret;

        ret = glusterfs_graph_unknown_options (graph);
        if (ret)
                return ret;

        /* XXX: log full graph (_gf_dump_details) */

        list_add (&graph->list, &ctx->graphs);
        ctx->active = graph;

        /* XXX: attach to master and set active pointer */
        if (ctx->master)
                ret = xlator_notify (ctx->master, GF_EVENT_GRAPH_NEW, graph);
        if (ret)
                return ret;

        /* XXX: perform parent up */
        ret = glusterfs_graph_parent_up (graph);
        if (ret)
                return ret;


        return 0;
}


int
glusterfs_graph_destroy (glusterfs_graph_t *graph)
{
        return 0;
}