summaryrefslogtreecommitdiffstats
path: root/xlators/features/changelog/src/changelog-helpers.h
blob: f8f254cf649f08f74b9edd0b377d6d8510fbe6d9 (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
571
572
573
574
575
576
/*
   Copyright (c) 2013 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.
*/

#ifndef _CHANGELOG_HELPERS_H
#define _CHANGELOG_HELPERS_H

#include "locking.h"
#include "timer.h"
#include "pthread.h"
#include "iobuf.h"

#include "changelog-misc.h"

/**
 * structures representing the changelog entries
 */
typedef struct changelog_write_data {
        /**
         * sincd gfid is _always_ a necessity, it's not a part
         * of the iobuf. by doing this we do not add any overhead
         * for data and metadata related fops.
         */
        uuid_t        cwd_gfid;

        /**
         * iobufs are used for optionals records: pargfid, path,
         * write offsets etc.. It's the fop implementers job
         * to allocate (iobuf_get() in the fop) and get unref'ed
         * in the callback (CHANGELOG_STACK_UNWIND).
         */
        struct iobuf *cwd_iobuf;

        /**
         * after allocation you can point this to the length of
         * usable data, but make sure it does not exceed the
         * the size of the requested iobuf.
         */
        size_t        cwd_iobuf_len;
        #define cwd_ptr     cwd_iobuf->ptr
        #define cwd_ptr_len cwd_iobuf_len

        /**
         * number of optional records
         */
        int cwd_xtra_records;
} changelog_write_data_t;

typedef struct changelog_rollover_data {
        /**
         * need a changelog reopen?
         */
        gf_boolean_t crd_finale;

        /**
         * changelog file name to be opened after a rollover
         */
        char crd_changelog_name[PATH_MAX];

        /**
         * changelog file name before rollover
         */
        char crd_changelog_oname[PATH_MAX];

        /**
         * use @crd_roll_key as suffix during roll-over
         */
        gf_boolean_t crd_use_suffix;

        /**
         * suffix used when rolling a changelog
         */
        unsigned long crd_roll_key;

        /**
         * preallocation? if yes, how much?
         */
        off_t crd_prealloc_size;
} changelog_rollover_data_t;

/**
 * the changelog entry: structure representing the type of entry
 * and a union encapsulating the above declared structures.
 */
typedef struct changelog_log_data {
        /**
         * type of the log data entry
         */
        changelog_log_type cld_type;

        /**
         * union for the type of changelog operations. @fsync() does
         * not have a corresponding entry in this union as it just
         * performs and @fsync() on ->changelog_fd.
         */
        union {
                changelog_write_data_t    cld_wdata;
                changelog_rollover_data_t cld_roll;
        };
} changelog_log_data_t;

typedef struct changelog_local changelog_local_t;

/**
 * holder for dispatch function and private data
 */

typedef struct changelog_priv changelog_priv_t;

typedef struct changelog_dispatcher {
        void *cd_data;
        int (*dispatchfn) (xlator_t *,
                           changelog_priv_t *, void *,
                           changelog_local_t *, changelog_log_data_t *);
} changelog_dispatcher_t;

struct changelog_bootstrap {
        changelog_mode_t mode;
        int (*ctor) (xlator_t *, changelog_dispatcher_t *, gf_boolean_t);
        int (*dtor) (xlator_t *, changelog_dispatcher_t *);
};

struct changelog_encoder {
        changelog_encoder_t encoder;
        int (*encode) (xlator_t *,
                       changelog_local_t *, changelog_log_data_t *);
};

struct changelog_ops {
        /* changelog open */
        int (*open) (xlator_t *, changelog_priv_t *,
                     void *, char *, gf_boolean_t);

        /* changelog close */
        int (*close) (xlator_t *, changelog_priv_t *, void *);

        /* changelog rollover */
        int (*rollover) (xlator_t *,
                         changelog_priv_t *,
                         void  *, char *, gf_boolean_t);

        int (*sync) (xlator_t *, changelog_priv_t *, void *);

        /* changelog write */
        int (*write) (xlator_t *,
                      changelog_priv_t *, void *,
                      changelog_local_t *, changelog_log_type);

        /* changelog read */
        int (*read) (xlator_t *,
                     changelog_priv_t *, void *, char *);

        int (*unlink) (xlator_t *,
                       changelog_priv_t *, void *, char *);

        /* {get|set} offset */
        off_t  (*get_offset) (xlator_t *this,
                           changelog_priv_t *, void *, changelog_local_t *);

        void (*set_offset) (xlator_t *this,
                            changelog_priv_t *, void *,
                            changelog_local_t *, off_t);

        void (*reset_offset) (xlator_t *this, changelog_priv_t *,
                              void *, changelog_local_t *);
};

/**
 * This structure is _filled_ by the policy init (@init_policy) routine.
 * Default @fops and @cops are passed to the init routine, which can
 * choose to override the file operation or changelog operation behaviour.
 * Just by _replacing_ the function pointers, a policy can change it's
 * file and changelog operation behaviour. Kind of inheritance...
 */
struct changelog_logpolicy {
        /* current changelog name */
        char changelog_name[PATH_MAX];

        /* private data */
        void *cpriv;

        /* file ops for the policy */
        struct xlator_fops     *fops;

        /* changelog operations for the policy */
        struct changelog_ops   *cops;

        /* current active policy */
        changelog_log_policy_t  policy;

        int (*init_policy) (xlator_t *,
                            changelog_priv_t *priv,
                            struct changelog_logpolicy *);
        int (*fini_policy) (xlator_t *, struct changelog_logpolicy *);
};

#define CHANGELOG_FNAME_FROM_POLICY(c) c->changelog_name

#define CHANGELOG_INVOKE_FOP(priv,fop,...) priv->cp->fops->fop (__VA_ARGS__)

#define CHANGELOG_INVOKE_CFOP(this,priv,fop,...)                        \
        priv->cp->cops->fop (this, priv, priv->cp->cpriv, ##__VA_ARGS__)

/* xlator private */

typedef struct changelog_time_slice {
        /**
         * just in case we need nanosecond granularity some day.
         * field is unused as of now (maybe we'd need it later).
         */
        struct timeval tv_start;

        /**
         * version of changelog file, incremented each time changes
         * rollover.
         */
        unsigned long changelog_version[CHANGELOG_MAX_TYPE];
} changelog_time_slice_t;

typedef struct changelog_rollover {
        /* rollover thread */
        pthread_t rollover_th;

        xlator_t *this;
} changelog_rollover_t;

typedef struct changelog_fsync {
        /* fsync() thread */
        pthread_t fsync_th;

        xlator_t *this;
} changelog_fsync_t;

# define CHANGELOG_MAX_CLIENTS  5
typedef struct changelog_notify {
        /* reader end of the pipe */
        int rfd;

        /* notifier thread */
        pthread_t notify_th;

        /* unique socket path */
        char sockpath[UNIX_PATH_MAX];

        int socket_fd;

        /**
         * simple array of accept()'ed fds. Not scalable at all
         * for large number of clients, but it's okay as we have
         * a ahrd limit in this version (@CHANGELOG_MAX_CLIENTS).
         */
        int client_fd[CHANGELOG_MAX_CLIENTS];

        xlator_t *this;
} changelog_notify_t;

struct changelog_priv {
        gf_boolean_t active;

        /**
         * write the record header?
         */
        gf_boolean_t no_gfid_hdr;

        gf_boolean_t lockless_update;

        /* to generate unique socket file per brick */
        char *changelog_brick;

        /* logging directory */
        char *changelog_dir;

        /* one file for all changelog types */
        int changelog_fd;

        gf_lock_t lock;

        /* writen end of the pipe */
        int wfd;

        /* rollover time */
        int32_t rollover_time;

        /* fsync() interval */
        int32_t fsync_interval;

        /* changelog type maps */
        const char *maps[CHANGELOG_MAX_TYPE];

        /* time slicer */
        changelog_time_slice_t slice;

        /* context of the updater */
        changelog_dispatcher_t cd;

        /* context of the rollover thread */
        changelog_rollover_t cr;

        /* context of fsync thread */
        changelog_fsync_t cf;

        /* context of the notifier thread */
        changelog_notify_t cn;

        /* operation mode */
        changelog_mode_t op_mode;

        /* bootstrap routine for 'current' logger */
        struct changelog_bootstrap *cb;

        /* encoder mode */
        changelog_encoder_t encode_mode;

        /* encoder */
        struct changelog_encoder *ce;

        /* logging policy */
        changelog_log_policy_t policy;

        /* policy logger */
        struct changelog_logpolicy *cp;

        /* current NSR term */
        uint32_t term;
};

struct changelog_local {
        inode_t              *inode;

        /**
         * fops that do not need inode version checks
         */
        gf_boolean_t          update_no_check;

        /**
         * the log data entry
         */
        changelog_log_data_t  cld;

        /**
         * number of bytes written: used for continuation
         */
        off_t nr_bytes;

        /**
         * temporary scratch pads
         */
        union {
                void          *ptr;
                unsigned long  val;
        } lu;
};

/* inode version is stored in inode ctx */
typedef struct changelog_inode_ctx {
        unsigned long iversion[CHANGELOG_MAX_TYPE];
} changelog_inode_ctx_t;

#define CHANGELOG_INODE_VERSION_TYPE(ctx, type)  &(ctx->iversion[type])

/**
 * Optional Records:
 *  fops that need to save additional information request a array of
 *  @changelog_opt_t struct. The array is allocated via @iobufs.
 */
typedef enum {
        CHANGELOG_OPT_REC_FOP,
        CHANGELOG_OPT_REC_ULL,
        CHANGELOG_OPT_REC_UUID,
        CHANGELOG_OPT_REC_NAME,
        CHANGELOG_OPT_REC_ENTRY,
        CHANGELOG_OPT_REC_INT32,
        CHANGELOG_OPT_REC_UINT32,
} changelog_optional_rec_type_t;

struct changelog_entry_fields {
        uuid_t  cef_uuid;
        char   *cef_bname;
};

typedef struct {
        /**
         * @co_covert can be used to do post-processing of the record before
         * it's persisted to the CHANGELOG. If this is NULL, then the record
         * is persisted as per it's in memory format.
         */
        size_t (*co_convert) (void *data, char *buffer, gf_boolean_t encode);

        /* release routines */
        void (*co_free) (void *data);

        /* type of the field */
        changelog_optional_rec_type_t co_type;

        /**
         * sizeof of the 'valid' field in the union. This field is not used if
         * @co_convert is specified.
         */
        size_t co_len;

        union {
                uuid_t                        co_uuid;
                glusterfs_fop_t               co_fop;
                int                           co_int32;
                unsigned int                  co_uint32;
                unsigned long long            co_number;
                struct changelog_entry_fields co_entry;
        };
} changelog_opt_t;

#define CHANGELOG_OPT_RECORD_LEN  sizeof (changelog_opt_t)

/**
 * helpers routines
 */

void
changelog_thread_cleanup (xlator_t *this, pthread_t thr_id);
inline void *
changelog_get_usable_buffer (changelog_local_t *local);
inline void
changelog_set_usable_record_and_length (changelog_local_t *local,
                                        size_t len, int xr);
void
changelog_local_cleanup (xlator_t *xl, changelog_local_t *local);
changelog_local_t *
changelog_local_init (xlator_t *this, inode_t *inode, uuid_t gfid,
                      int xtra_records, gf_boolean_t update_flag);
int
changelog_inject_single_event (xlator_t *this,
                               changelog_priv_t *priv,
                               changelog_local_t *local,
                               changelog_log_data_t *cld);
inline size_t
changelog_entry_length ();
inline int
changelog_write (int fd, char *buffer, size_t len);
int
changelog_write_change (xlator_t *this, changelog_priv_t *priv,
                        changelog_local_t *local, char *buffer, size_t len);
inline int
changelog_handle_change (xlator_t *this,
                         changelog_priv_t *priv,
                         changelog_local_t *local, changelog_log_data_t *cld);
inline void
changelog_update (xlator_t *this,
                  changelog_priv_t *priv,
                  changelog_local_t *local,
                  changelog_log_type type);
void *
changelog_rollover (void *data);
void *
changelog_fsync_thread (void *data);
int
changelog_forget (xlator_t *this, inode_t *inode);

/* macros */

#define CHANGELOG_STACK_UNWIND(fop, frame, params ...) do {             \
                changelog_local_t *__local = NULL;                      \
                xlator_t          *__xl    = NULL;                      \
                if (frame) {                                            \
                        __local      = frame->local;                    \
                        __xl         = frame->this;                     \
                        frame->local = NULL;                            \
                }                                                       \
                STACK_UNWIND_STRICT (fop, frame, params);               \
                changelog_local_cleanup (__xl, __local);                \
        } while (0)

#define CHANGELOG_IOBUF_REF(iobuf) do {         \
                if (iobuf)                      \
                        iobuf_ref (iobuf);      \
        } while (0)

#define CHANGELOG_IOBUF_UNREF(iobuf) do {       \
                if (iobuf)                      \
                        iobuf_unref (iobuf);    \
        } while (0)

#define CHANGELOG_FILL_BUFFER(buffer, off, val, len) do {       \
                memcpy (buffer + off, val, len);                \
                off += len;                                     \
        } while (0)

#define SLICE_VERSION_UPDATE(slice) do {                \
                int i = 0;                              \
                for (; i < CHANGELOG_MAX_TYPE; i++) {   \
                        slice->changelog_version[i]++;  \
                }                                       \
        } while (0)

#define CHANGELOG_FILL_INT32(co, number, converter, xlen) do {  \
                co->co_convert = converter;                     \
                co->co_free = NULL;                             \
                co->co_type = CHANGELOG_OPT_REC_INT32;          \
                co->co_int32 = number;                          \
                xlen += sizeof (int);                           \
        } while (0)

#define CHANGELOG_FILL_UINT32(co, number, converter, xlen) do { \
                co->co_convert = converter;                     \
                co->co_free = NULL;                             \
                co->co_type = CHANGELOG_OPT_REC_UINT32;         \
                co->co_uint32 = number;                         \
                xlen += sizeof (unsigned int);                  \
        } while (0)

#define CHANGELOG_FILL_FOP_NUMBER(co, fop, converter, xlen) do { \
                co->co_convert = converter;                      \
                co->co_free = NULL;                              \
                co->co_type = CHANGELOG_OPT_REC_FOP;             \
                co->co_fop = fop;                                \
                xlen += sizeof (fop);                            \
        } while (0)

#define CHANGELOG_FILL_NAME(co, name, freefn, xlen, label)       \
        do {                                                     \
                co->co_convert = NULL;                           \
                co->co_free = freefn;                            \
                co->co_type = CHANGELOG_OPT_REC_NAME;            \
                co->co_entry.cef_bname = gf_strdup(name);        \
                if (!co->co_entry.cef_bname)                     \
                        goto label;                              \
                co->co_len = strlen (name);                      \
                xlen += co->co_len;                              \
        } while(0)                                               \

#define CHANGELOG_FILL_ENTRY(co, pargfid, bname,                        \
                             converter, freefn, xlen, label)            \
        do {                                                            \
                co->co_convert = converter;                             \
                co->co_free = freefn;                                   \
                co->co_type = CHANGELOG_OPT_REC_ENTRY;                  \
                uuid_copy (co->co_entry.cef_uuid, pargfid);             \
                co->co_entry.cef_bname = gf_strdup(bname);              \
                if (!co->co_entry.cef_bname)                            \
                        goto label;                                     \
                xlen += (UUID_CANONICAL_FORM_LEN + strlen (bname));     \
        } while (0)

#define CHANGELOG_INIT(this, local, inode, gfid, xrec)                  \
        local = changelog_local_init (this, inode, gfid, xrec, _gf_false)

#define CHANGELOG_INIT_NOCHECK(this, local, inode, gfid, xrec)          \
        local = changelog_local_init (this, inode, gfid, xrec, _gf_true)

#define CHANGELOG_NOT_ACTIVE_THEN_GOTO(frame, priv, label) do { \
                if (!priv->active)                              \
                        goto label;                             \
                /* ignore rebalance process's activity. */      \
                if (frame->root->pid == GF_CLIENT_PID_DEFRAG)   \
                        goto label;                             \
        } while (0)

/* ignore internal fops */
#define CHANGELOG_IF_INTERNAL_FOP_THEN_GOTO(dict, label) do {           \
                if (dict && dict_get (dict, GLUSTERFS_INTERNAL_FOP_KEY)) \
                        goto label;                                     \
        } while (0)

#define CHANGELOG_COND_GOTO(priv, cond, label) do {                    \
                if (!priv->active || cond)                             \
                        goto label;                                    \
        } while (0)

int
changelog_open (xlator_t *this, changelog_priv_t *priv, changelog_local_t *local, changelog_rollover_data_t *crd);

#endif /* _CHANGELOG_HELPERS_H */