summaryrefslogtreecommitdiffstats
path: root/libglusterfs/src/gfdb/gfdb_data_store_helper.c
blob: ff85e17169d6f07aaf436318f9ea18369e322a0f (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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
#include "gfdb_data_store_helper.h"


/*Create a single link info structure*/
gfdb_link_info_t*
gfdb_link_info_new ()
{
        gfdb_link_info_t *link_info = NULL;

        link_info = GF_CALLOC (1, sizeof(gfdb_link_info_t),
                                        gf_mt_gfdb_link_info_t);
        if (!link_info) {
                gf_msg (GFDB_DATA_STORE, GF_LOG_ERROR, ENOMEM,
                        LG_MSG_NO_MEMORY, "Memory allocation failed for "
                        "link_info ");
                goto out;
        }

        INIT_LIST_HEAD (&link_info->list);

out:

        return link_info;
}

/*Destroy a link info structure*/
void
gfdb_link_info_free(gfdb_link_info_t *link_info)
{
        GF_FREE (link_info);
}


/*Function to create the query_record*/
gfdb_query_record_t *
gfdb_query_record_new()
{
        int ret = -1;
        gfdb_query_record_t *query_record = NULL;

        query_record = GF_CALLOC (1, sizeof(gfdb_query_record_t),
                                        gf_mt_gfdb_query_record_t);
        if (!query_record) {
                gf_msg (GFDB_DATA_STORE, GF_LOG_ERROR, ENOMEM,
                        LG_MSG_NO_MEMORY, "Memory allocation failed for "
                        "query_record ");
                goto out;
        }

        INIT_LIST_HEAD (&query_record->link_list);

        ret = 0;
out:
        if (ret == -1) {
                GF_FREE (query_record);
        }
        return query_record;
}


/*Function to delete a single linkinfo from list*/
static void
gfdb_delete_linkinfo_from_list (gfdb_link_info_t **link_info)
{
        GF_VALIDATE_OR_GOTO (GFDB_DATA_STORE, link_info, out);
        GF_VALIDATE_OR_GOTO (GFDB_DATA_STORE, *link_info, out);

        /*Remove hard link from list*/
        list_del(&(*link_info)->list);
        gfdb_link_info_free (*link_info);
        link_info = NULL;
out:
        return;
}


/*Function to destroy link_info list*/
void
gfdb_free_link_info_list (gfdb_query_record_t *query_record)
{
        gfdb_link_info_t        *link_info = NULL;
        gfdb_link_info_t        *temp = NULL;

        GF_VALIDATE_OR_GOTO (GFDB_DATA_STORE, query_record, out);

        list_for_each_entry_safe(link_info, temp,
                        &query_record->link_list, list)
        {
                gfdb_delete_linkinfo_from_list (&link_info);
                link_info = NULL;
        }

out:
        return;
}



/* Function to add linkinfo to the query record */
int
gfdb_add_link_to_query_record (gfdb_query_record_t      *query_record,
                           uuid_t                   pgfid,
                           char               *base_name)
{
        int ret                                 = -1;
        gfdb_link_info_t *link_info             = NULL;
        int base_name_len                       = 0;

        GF_VALIDATE_OR_GOTO (GFDB_DATA_STORE, query_record, out);
        GF_VALIDATE_OR_GOTO (GFDB_DATA_STORE, pgfid, out);
        GF_VALIDATE_OR_GOTO (GFDB_DATA_STORE, base_name, out);

        link_info = gfdb_link_info_new ();
        if (!link_info) {
                goto out;
        }

        gf_uuid_copy (link_info->pargfid, pgfid);
        base_name_len = strlen (base_name);
        memcpy (link_info->file_name, base_name, base_name_len);
        link_info->file_name[base_name_len] = '\0';

        list_add_tail (&link_info->list,
                        &query_record->link_list);

        query_record->link_count++;

        ret = 0;
out:
        if (ret) {
                gfdb_link_info_free (link_info);
                link_info = NULL;
        }
        return ret;
}



/*Function to destroy query record*/
void
gfdb_query_record_free(gfdb_query_record_t *query_record)
{
        if (query_record) {
                gfdb_free_link_info_list (query_record);
                GF_FREE (query_record);
        }
}


/******************************************************************************
                SERIALIZATION/DE-SERIALIZATION OF QUERY RECORD
*******************************************************************************/
/******************************************************************************
 The on disk format of query record is as follows,

+---------------------------------------------------------------------------+
| Length of serialized query record |       Serialized Query Record         |
+---------------------------------------------------------------------------+
             4 bytes                     Length of serialized query record
                                                      |
                                                      |
     -------------------------------------------------|
     |
     |
     V
   Serialized Query Record Format:
   +---------------------------------------------------------------------------+
   | GFID |  Link count   |  <LINK INFO>  |.....                      | FOOTER |
   +---------------------------------------------------------------------------+
     16 B        4 B         Link Length                                  4 B
                                |                                          |
                                |                                          |
   -----------------------------|                                          |
   |                                                                       |
   |                                                                       |
   V                                                                       |
   Each <Link Info> will be serialized as                                  |
   +-----------------------------------------------+                       |
   | PGID | BASE_NAME_LENGTH |      BASE_NAME      |                       |
   +-----------------------------------------------+                       |
     16 B       4 B             BASE_NAME_LENGTH                           |
                                                                           |
                                                                           |
   ------------------------------------------------------------------------|
   |
   |
   V
   FOOTER is a magic number 0xBAADF00D indicating the end of the record.
   This also serves as a serialized schema validator.
 * ****************************************************************************/

#define GFDB_QUERY_RECORD_FOOTER 0xBAADF00D
#define UUID_LEN                 16

/*Function to get the potential length of the serialized buffer*/
static int32_t
gfdb_query_record_serialized_length (gfdb_query_record_t *query_record)
{
        int32_t len                         = -1;
        gfdb_link_info_t *link_info     = NULL;

        GF_VALIDATE_OR_GOTO (GFDB_DATA_STORE, query_record, out);

        /* Length of GFID */
        len = UUID_LEN;

        /* length of number of links*/
        len += sizeof (int32_t);

        list_for_each_entry (link_info, &query_record->link_list, list) {

                /* length of PFID */
                len += UUID_LEN;

                /* Add size of base name length*/
                len += sizeof (int32_t);

                /* Length of base_name */
                len += strlen (link_info->file_name);

        }

        /* length of footer */
        len += sizeof (int32_t);
out:
        return len;
}

/* Function for serializing query record.
 *
 * Query Record Serialization Format
 * +---------------------------------------------------------------------------+
 * | GFID |  Link count   |  <LINK INFO>  |.....                      | FOOTER |
 * +---------------------------------------------------------------------------+
 *   16 B        4 B         Link Length                                  4 B
 *
 *
 * Each <Link Info> will be serialized as
 * +-----------------------------------------------+
 * | PGID | BASE_NAME_LENGTH |      BASE_NAME      |
 * +-----------------------------------------------+
 *   16 B       4 B             BASE_NAME_LENGTH
 *
 *
 * FOOTER is a magic number 0xBAADF00D indicating the end of the record.
 * This also serves as a serialized schema validator.
 *
 * The function will allocate memory to the serialized buffer,
 * the caller needs to free it.
 * Returns the length of the serialized buffer on success
 * or -1 on failure.
 *
 * */
static int
gfdb_query_record_serialize (gfdb_query_record_t *query_record,
                             char **in_buffer)
{
        gfdb_link_info_t *link_info     = NULL;
        int              count          = -1;
        int              base_name_len  = 0;
        int              buffer_length  = 0;
        int              footer         = GFDB_QUERY_RECORD_FOOTER;
        char             *buffer        = NULL;
        char             *ret_buffer    = NULL;

        GF_VALIDATE_OR_GOTO (GFDB_DATA_STORE, query_record, out);
        GF_VALIDATE_OR_GOTO (GFDB_DATA_STORE,
                             (query_record->link_count > 0), out);
        GF_VALIDATE_OR_GOTO (GFDB_DATA_STORE, in_buffer, out);


        /* Calculate the total length of the serialized buffer */
        buffer_length = gfdb_query_record_serialized_length (query_record);
        if (buffer_length <= 0) {
                gf_msg (GFDB_DATA_STORE, GF_LOG_ERROR, 0,
                        LG_MSG_DB_ERROR, "Failed to calculate the length of "
                        "serialized buffer");
                goto out;
        }

        /* Allocate memory to the serialized buffer */
        ret_buffer = GF_CALLOC (1, buffer_length,  gf_common_mt_char);
        if (!ret_buffer) {
                gf_msg (GFDB_DATA_STORE, GF_LOG_ERROR, 0,
                        LG_MSG_DB_ERROR, "Memory allocation failed for "
                        "serialized buffer.");
                goto out;
        }

        buffer = ret_buffer;

        count = 0;

        /* Copying the GFID */
        memcpy (buffer, query_record->gfid, UUID_LEN);
        buffer += UUID_LEN;
        count += UUID_LEN;

        /* Copying the number of links */
        memcpy (buffer, &query_record->link_count, sizeof (int32_t));
        buffer += sizeof (int32_t);
        count += sizeof (int32_t);

        list_for_each_entry (link_info, &query_record->link_list, list) {

                /* Copying the PFID */
                memcpy(buffer, link_info->pargfid, UUID_LEN);
                buffer += UUID_LEN;
                count += UUID_LEN;

                /* Copying base name length*/
                base_name_len = strlen (link_info->file_name);
                memcpy (buffer, &base_name_len, sizeof (int32_t));
                buffer += sizeof (int32_t);
                count += sizeof (int32_t);

                /* Length of base_name */
                memcpy(buffer, link_info->file_name, base_name_len);
                buffer += base_name_len;
                count += base_name_len;

        }

        /* Copying the Footer of the record */
        memcpy (buffer, &footer, sizeof (int32_t));
        buffer += sizeof (int32_t);
        count += sizeof (int32_t);

out:
        if (count < 0) {
                GF_FREE (ret_buffer);
                ret_buffer = NULL;
        }
        *in_buffer = ret_buffer;
        return count;
}

static gf_boolean_t
is_serialized_buffer_valid (char *in_buffer, int buffer_length) {
        gf_boolean_t    ret        = _gf_false;
        int             footer     = 0;

        /* Read the footer */
        in_buffer += (buffer_length - sizeof (int32_t));
        memcpy (&footer, in_buffer, sizeof (int32_t));

        /*
         * if the footer is not GFDB_QUERY_RECORD_FOOTER
         * then the serialized record is invalid
         *
         * */
        if (footer != GFDB_QUERY_RECORD_FOOTER) {
                goto out;
        }

        ret = _gf_true;
out:
        return ret;
}


static int
gfdb_query_record_deserialize (char *in_buffer,
                               int buffer_length,
                               gfdb_query_record_t **query_record)
{
        int ret                                 = -1;
        char *buffer                            = NULL;
        int i                                   = 0;
        gfdb_link_info_t *link_info             = NULL;
        int count                               = 0;
        int base_name_len                       = 0;
        gfdb_query_record_t *ret_qrecord        = NULL;

        GF_VALIDATE_OR_GOTO (GFDB_DATA_STORE, in_buffer, out);
        GF_VALIDATE_OR_GOTO (GFDB_DATA_STORE, query_record, out);
        GF_VALIDATE_OR_GOTO (GFDB_DATA_STORE, (buffer_length > 0), out);

        if (!is_serialized_buffer_valid (in_buffer, buffer_length)) {
                gf_msg (GFDB_DATA_STORE, GF_LOG_ERROR, 0,
                        LG_MSG_DB_ERROR, "Invalid serialized query record");
                goto out;
        }

        buffer = in_buffer;

        ret_qrecord = gfdb_query_record_new ();
        if (!ret_qrecord) {
                gf_msg (GFDB_DATA_STORE, GF_LOG_ERROR, 0,
                        LG_MSG_DB_ERROR, "Failed to allocate space to "
                        "gfdb_query_record_t");
                goto out;
        }

        /* READ GFID */
        memcpy ((ret_qrecord)->gfid, buffer, UUID_LEN);
        buffer += UUID_LEN;
        count += UUID_LEN;

        /* Read the number of link */
        memcpy (&(ret_qrecord->link_count), buffer, sizeof (int32_t));
        buffer += sizeof (int32_t);
        count += sizeof (int32_t);

        /* Read all the links */
        for (i = 0; i < ret_qrecord->link_count; i++) {
                if (count >= buffer_length) {
                        gf_msg (GFDB_DATA_STORE, GF_LOG_ERROR, 0,
                                LG_MSG_DB_ERROR, "Invalid serialized "
                                "query record");
                        ret = -1;
                        goto out;
                }

                link_info = gfdb_link_info_new ();
                if (!link_info) {
                        gf_msg (GFDB_DATA_STORE, GF_LOG_ERROR, 0,
                                LG_MSG_DB_ERROR, "Failed to create link_info");
                        goto out;
                }

                /* READ PGFID */
                memcpy (link_info->pargfid, buffer, UUID_LEN);
                buffer += UUID_LEN;
                count += UUID_LEN;

                /* Read base name length */
                memcpy (&base_name_len, buffer, sizeof (int32_t));
                buffer += sizeof (int32_t);
                count += sizeof (int32_t);

                /* READ basename */
                memcpy (link_info->file_name, buffer, base_name_len);
                buffer += base_name_len;
                count += base_name_len;
                link_info->file_name[base_name_len] = '\0';

                /* Add link_info to the list */
                list_add_tail (&link_info->list,
                               &(ret_qrecord->link_list));

                /* Reseting link_info */
                link_info = NULL;
        }

        ret = 0;
out:
        if (ret) {
                gfdb_query_record_free (ret_qrecord);
                ret_qrecord = NULL;
        }
        *query_record = ret_qrecord;
        return ret;
}





/* Function to write query record to file
 *
 * Disk format
 * +---------------------------------------------------------------------------+
 * | Length of serialized query record |       Serialized Query Record         |
 * +---------------------------------------------------------------------------+
 *            4 bytes                     Length of serialized query record
 *
 * Please refer gfdb_query_record_serialize () for format of
 * Serialized Query Record
 *
 * */
int
gfdb_write_query_record (int fd,
                        gfdb_query_record_t *query_record)
{
        int ret                 = -1;
        int buffer_len          = 0;
        char *buffer            = NULL;
        int write_len           = 0;
        char *write_buffer      = NULL;

        GF_VALIDATE_OR_GOTO (GFDB_DATA_STORE, (fd >= 0), out);
        GF_VALIDATE_OR_GOTO (GFDB_DATA_STORE, query_record, out);

        buffer_len = gfdb_query_record_serialize (query_record, &buffer);
        if (buffer_len < 0) {
                gf_msg (GFDB_DATA_STORE, GF_LOG_ERROR, 0,
                        LG_MSG_DB_ERROR, "Failed to serialize query record");
                goto out;
        }

        /* Serialize the buffer length and write to file */
        ret = write (fd, &buffer_len, sizeof (int32_t));
        if (ret < 0) {
                gf_msg (GFDB_DATA_STORE, GF_LOG_ERROR, 0,
                                LG_MSG_DB_ERROR, "Failed to write buffer length"
                                " to file");
                goto out;
        }

        /* Write the serialized query record to file */
        write_len = buffer_len;
        write_buffer = buffer;
        while ((ret = write (fd, write_buffer, write_len)) <  write_len) {
                if (ret < 0) {
                        gf_msg (GFDB_DATA_STORE, GF_LOG_ERROR, errno,
                                LG_MSG_DB_ERROR, "Failed to write serialized "
                                "query record to file");
                        goto out;
                }

                write_buffer += ret;
                write_len -= ret;
        }

        ret = 0;
out:
        GF_FREE (buffer);
        return ret;
}



/* Function to read query record from file.
 * Allocates memory to query record and
 * returns length of serialized query record when successful
 * Return -1 when failed.
 * Return 0 when reached EOF.
 * */
int
gfdb_read_query_record (int fd,
                        gfdb_query_record_t **query_record)
{
        int ret                 = -1;
        int buffer_len          = 0;
        int read_len            = 0;
        char *buffer            = NULL;
        char *read_buffer       = NULL;

        GF_VALIDATE_OR_GOTO (GFDB_DATA_STORE, (fd >= 0), out);
        GF_VALIDATE_OR_GOTO (GFDB_DATA_STORE, query_record, out);


        /* Read serialized query record length from the file*/
        ret = read (fd, &buffer_len, sizeof (int32_t));
        if (ret < 0) {
                gf_msg (GFDB_DATA_STORE, GF_LOG_ERROR, 0,
                                LG_MSG_DB_ERROR, "Failed reading buffer length"
                                " from file");
                goto out;
        }
        /* EOF */
        else if (ret == 0) {
                ret = 0;
                goto out;
        }

        /* Allocating memory to the serialization buffer */
        buffer = GF_CALLOC (1, buffer_len,  gf_common_mt_char);
        if (!buffer) {
                gf_msg (GFDB_DATA_STORE, GF_LOG_ERROR, 0,
                        LG_MSG_DB_ERROR, "Failed to allocate space to "
                        "serialized buffer");
                goto out;
        }


        /* Read the serialized query record from file */
        read_len = buffer_len;
        read_buffer = buffer;
        while ((ret = read (fd, read_buffer, read_len)) < read_len) {

                /*Any error */
                if (ret < 0) {
                        gf_msg (GFDB_DATA_STORE, GF_LOG_ERROR, errno,
                                LG_MSG_DB_ERROR, "Failed to read serialized "
                                "query record from file");
                        goto out;
                }
                /* EOF */
                else if (ret == 0) {
                        gf_msg (GFDB_DATA_STORE, GF_LOG_ERROR, 0,
                                LG_MSG_DB_ERROR, "Invalid query record or "
                                "corrupted query file");
                        ret = -1;
                        goto out;
                }

                read_buffer += ret;
                read_len -= ret;
        }

        ret = gfdb_query_record_deserialize (buffer, buffer_len,
                                             query_record);
        if (ret) {
                gf_msg (GFDB_DATA_STORE, GF_LOG_ERROR, 0,
                        LG_MSG_DB_ERROR, "Failed to de-serialize query record");
                goto out;
        }

        ret = buffer_len;
out:
        GF_FREE (buffer);
        return ret;
}