-
Notifications
You must be signed in to change notification settings - Fork 17
/
cio_file.c
1188 lines (1006 loc) · 30.5 KB
/
cio_file.c
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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* Chunk I/O
* =========
* Copyright 2018-2019 Eduardo Silva <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <limits.h>
#include <chunkio/chunkio_compat.h>
#include <chunkio/chunkio.h>
#include <chunkio/cio_crc32.h>
#include <chunkio/cio_chunk.h>
#include <chunkio/cio_file.h>
#include <chunkio/cio_file_st.h>
#include <chunkio/cio_log.h>
#include <chunkio/cio_stream.h>
char cio_file_init_bytes[] = {
/* file type (2 bytes) */
CIO_FILE_ID_00, CIO_FILE_ID_01,
/* crc32 (4 bytes) in network byte order */
0xff, 0x12, 0xd9, 0x41,
/* padding bytes (we have 16 extra bytes) */
0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00,
0x00,
/* metadata length (2 bytes) */
0x00, 0x00
};
#define ROUND_UP(N, S) ((((N) + (S) - 1) / (S)) * (S))
/* Get the number of bytes in the Content section */
static size_t content_len(struct cio_file *cf)
{
int meta;
size_t len;
meta = cio_file_st_get_meta_len(cf->map);
len = 2 + meta + cf->data_size;
return len;
}
/* Calculate content checksum in a variable */
void cio_file_calculate_checksum(struct cio_file *cf, crc_t *out)
{
crc_t val;
size_t len;
unsigned char *in_data;
len = content_len(cf);
in_data = (unsigned char *) cf->map + CIO_FILE_CONTENT_OFFSET;
val = cio_crc32_update(cf->crc_cur, in_data, len);
*out = val;
}
/* Update crc32 checksum into the memory map */
static void update_checksum(struct cio_file *cf,
unsigned char *data, size_t len)
{
crc_t crc;
crc = cio_crc32_update(cf->crc_cur, data, len);
memcpy(cf->map + 2, &crc, sizeof(crc));
cf->crc_cur = crc;
}
/* Finalize CRC32 context and update the memory map */
static void finalize_checksum(struct cio_file *cf)
{
crc_t crc;
crc = cio_crc32_finalize(cf->crc_cur);
crc = htonl(crc);
memcpy(cf->map + 2, &crc, sizeof(crc));
}
/*
* adjust_layout: if metadata has changed, we need to adjust the content
* data and reference pointers.
*/
static int adjust_layout(struct cio_chunk *ch,
struct cio_file *cf, size_t meta_size)
{
cio_file_st_set_meta_len(cf->map, (uint16_t) meta_size);
/* Update checksum */
if (ch->ctx->flags & CIO_CHECKSUM) {
/* reset current crc since we are calculating from zero */
cf->crc_cur = cio_crc32_init();
cio_file_calculate_checksum(cf, &cf->crc_cur);
}
/* Sync changes to disk */
cf->synced = CIO_FALSE;
return 0;
}
/* Initialize Chunk header & structure */
static void write_init_header(struct cio_chunk *ch, struct cio_file *cf)
{
memcpy(cf->map, cio_file_init_bytes, sizeof(cio_file_init_bytes));
/* If no checksum is enabled, reset the initial crc32 bytes */
if (!(ch->ctx->flags & CIO_CHECKSUM)) {
cf->map[2] = 0;
cf->map[3] = 0;
cf->map[4] = 0;
cf->map[5] = 0;
}
}
/* Return the available size in the file map to write data */
static size_t get_available_size(struct cio_file *cf, int *meta_len)
{
size_t av;
int len;
/* Get metadata length */
len = cio_file_st_get_meta_len(cf->map);
av = cf->alloc_size - cf->data_size;
av -= (CIO_FILE_HEADER_MIN + len);
*meta_len = len;
return av;
}
/*
* For the recently opened or created file, check the structure format
* and validate relevant fields.
*/
static int cio_file_format_check(struct cio_chunk *ch,
struct cio_file *cf, int flags)
{
unsigned char *p;
crc_t crc_check;
crc_t crc;
p = (unsigned char *) cf->map;
/* If the file is empty, put the structure on it */
if (cf->fs_size == 0) {
/* check we have write permissions */
if ((cf->flags & CIO_OPEN) == 0) {
cio_log_warn(ch->ctx,
"[cio file] cannot initialize chunk (read-only)");
return -1;
}
/* at least we need 24 bytes as allocated space */
if (cf->alloc_size < CIO_FILE_HEADER_MIN) {
cio_log_warn(ch->ctx, "[cio file] cannot initialize chunk");
return -1;
}
/* Initialize init bytes */
write_init_header(ch, cf);
/* Write checksum in context (note: crc32 not finalized) */
if (ch->ctx->flags & CIO_CHECKSUM) {
cio_file_calculate_checksum(cf, &cf->crc_cur);
}
}
else {
/* Check first two bytes */
if (p[0] != CIO_FILE_ID_00 || p[1] != CIO_FILE_ID_01) {
cio_log_debug(ch->ctx, "[cio file] invalid header at %s",
ch->name);
return -1;
}
/* Checksum */
if (ch->ctx->flags & CIO_CHECKSUM) {
/* Initialize CRC variable */
cf->crc_cur = cio_crc32_init();
/* Get checksum stored in the mmap */
p = (unsigned char *) cio_file_st_get_hash(cf->map);
/* Calculate content checksum */
cio_file_calculate_checksum(cf, &crc);
/* Compare */
crc_check = cio_crc32_finalize(crc);
crc_check = htonl(crc_check);
if (memcmp(p, &crc_check, sizeof(crc_check)) != 0) {
cio_log_debug(ch->ctx, "[cio file] invalid crc32 at %s/%s",
ch->name, cf->path);
return -1;
}
cf->crc_cur = crc;
}
}
return 0;
}
/*
* Unmap the memory for the opened file in question. It make sure
* to sync changes to disk first.
*/
static int munmap_file(struct cio_ctx *ctx, struct cio_chunk *ch)
{
int ret;
struct cio_file *cf;
cf = (struct cio_file *) ch->backend;
if (!cf) {
return -1;
}
/* File not mapped */
if (cf->map == NULL) {
return -1;
}
/* Sync pending changes to disk */
if (cf->synced == CIO_FALSE) {
ret = cio_file_sync(ch);
if (ret == -1) {
cio_log_error(ch->ctx,
"[cio file] error syncing file at "
"%s:%s", ch->st->name, ch->name);
}
}
/* Unmap file */
munmap(cf->map, cf->alloc_size);
cf->map = NULL;
cf->data_size = 0;
cf->alloc_size = 0;
/* Adjust counters */
cio_chunk_counter_total_up_sub(ctx);
return 0;
}
/*
* This function creates the memory map for the open file descriptor plus
* setup the chunk structure reference.
*/
static int mmap_file(struct cio_ctx *ctx, struct cio_chunk *ch, size_t size)
{
int ret;
int oflags = 0;
size_t fs_size = 0;
ssize_t content_size;
struct stat fst;
struct cio_file *cf;
cf = (struct cio_file *) ch->backend;
if (cf->map != NULL) {
return CIO_OK;
}
/*
* 'size' value represents the value of a previous fstat(2) set by a previous
* caller. If the value is greater than zero, just use it, otherwise do a new
* fstat(2) of the file descriptor.
*/
if (size > 0) {
fs_size = size;
}
else {
ret = fstat(cf->fd, &fst);
if (ret == -1) {
cio_errno();
return CIO_ERROR;
}
/* Get file size from the file system */
fs_size = fst.st_size;
}
/* Mmap */
if (cf->flags & CIO_OPEN_RW) {
oflags = PROT_READ | PROT_WRITE;
}
else if (cf->flags & CIO_OPEN_RD) {
oflags = PROT_READ;
}
/* If the file is not empty, use file size for the memory map */
if (fs_size > 0) {
size = fs_size;
cf->synced = CIO_TRUE;
}
else if (fs_size == 0) {
/* We can only prepare a file if it has been opened in RW mode */
if ((cf->flags & CIO_OPEN_RW) == 0) {
return CIO_CORRUPTED;
}
cf->synced = CIO_FALSE;
/* Adjust size to make room for headers */
if (size < CIO_FILE_HEADER_MIN) {
size += CIO_FILE_HEADER_MIN;
}
/* For empty files, make room in the file system */
size = ROUND_UP(size, ctx->page_size);
ret = cio_file_fs_size_change(cf, size);
if (ret == -1) {
cio_errno();
cio_log_error(ctx, "cannot adjust chunk size '%s' to %lu bytes",
cf->path, size);
return CIO_ERROR;
}
cio_log_debug(ctx, "%s:%s adjusting size OK", ch->st->name, ch->name);
}
/* Map the file */
size = ROUND_UP(size, ctx->page_size);
cf->map = mmap(0, size, oflags, MAP_SHARED, cf->fd, 0);
if (cf->map == MAP_FAILED) {
cio_errno();
cf->map = NULL;
cio_log_error(ctx, "cannot mmap/read chunk '%s'", cf->path);
return CIO_ERROR;
}
cf->alloc_size = size;
/* check content data size */
if (fs_size > 0) {
content_size = cio_file_st_get_content_size(cf->map, fs_size);
if (content_size == -1) {
cio_log_error(ctx, "invalid content size %s", cf->path);
munmap(cf->map, cf->alloc_size);
cf->map = NULL;
cf->data_size = 0;
cf->alloc_size = 0;
return CIO_CORRUPTED;
}
cf->data_size = content_size;
cf->fs_size = fs_size;
}
else {
cf->data_size = 0;
cf->fs_size = 0;
}
ret = cio_file_format_check(ch, cf, cf->flags);
if (ret == -1) {
cio_log_error(ctx, "format check failed: %s/%s",
ch->st->name, ch->name);
munmap(cf->map, cf->alloc_size);
cf->map = NULL;
cf->data_size = 0;
cf->alloc_size = 0;
return CIO_CORRUPTED;
}
cf->st_content = cio_file_st_get_content(cf->map);
cio_log_debug(ctx, "%s:%s mapped OK", ch->st->name, ch->name);
/* The mmap succeeded, adjust the counters */
cio_chunk_counter_total_up_add(ctx);
return CIO_OK;
}
/* Open file system file, set file descriptor and file size */
static int file_open(struct cio_ctx *ctx, struct cio_file *cf)
{
int ret;
struct stat st;
if (cf->map || cf->fd > 0) {
return -1;
}
/* Open file descriptor */
if (cf->flags & CIO_OPEN_RW) {
cf->fd = open(cf->path, O_RDWR | O_CREAT, (mode_t) 0600);
}
else if (cf->flags & CIO_OPEN_RD) {
cf->fd = open(cf->path, O_RDONLY);
}
if (cf->fd == -1) {
cio_errno();
cio_log_error(ctx, "cannot open/create %s", cf->path);
return -1;
}
/* Store the current real size */
ret = fstat(cf->fd, &st);
if (ret == -1) {
cio_errno();
close(cf->fd);
cf->fd = -1;
return -1;
}
cf->fs_size = st.st_size;
return 0;
}
int cio_file_read_prepare(struct cio_ctx *ctx, struct cio_chunk *ch)
{
int ret;
struct cio_file *cf = ch->backend;
if (!cf->map) {
ret = mmap_file(ctx, ch, 0);
return ret;
}
return 0;
}
int cio_file_content_copy(struct cio_chunk *ch,
void **out_buf, size_t *out_size)
{
int ret;
int set_down = CIO_FALSE;
char *buf;
char *data = NULL;
size_t size;
struct cio_file *cf = ch->backend;
/* If the file content is already up, just do a copy of the memory map */
if (cio_chunk_is_up(ch) == CIO_FALSE) {
ret = cio_chunk_up_force(ch);
if (ret != CIO_OK ){
return CIO_ERROR;
}
set_down = CIO_TRUE;
}
size = cf->data_size;
data = cio_file_st_get_content(cf->map);
if (!data) {
if (set_down == CIO_TRUE) {
cio_chunk_down(ch);
}
return CIO_ERROR;
}
buf = malloc(size + 1);
if (!buf) {
cio_errno();
if (set_down == CIO_TRUE) {
cio_chunk_down(ch);
}
return CIO_ERROR;
}
memcpy(buf, data, size);
buf[size] = '\0';
*out_buf = buf;
*out_size = size;
if (set_down == CIO_TRUE) {
cio_chunk_down(ch);
}
return CIO_OK;
}
/*
* If the maximum number of 'up' chunks is reached, put this chunk
* down (only at open time).
*/
static inline int open_and_up(struct cio_ctx *ctx)
{
if (ctx->total_chunks_up >= ctx->max_chunks_up) {
return CIO_FALSE;
}
return CIO_TRUE;
}
/*
* Fetch the file size regardless of if we opened this file or not.
*/
size_t cio_file_real_size(struct cio_file *cf)
{
int ret;
struct stat st;
/* Store the current real size */
ret = stat(cf->path, &st);
if (ret == -1) {
cio_errno();
return 0;
}
return st.st_size;
}
/*
* Open or create a data file: the following behavior is expected depending
* of the passed flags:
*
* CIO_OPEN | CIO_OPEN_RW:
* - Open for read/write, if the file don't exist, it's created and the
* memory map size is assigned to the given value on 'size'.
*
* CIO_OPEN_RD:
* - If file exists, open it in read-only mode.
*/
struct cio_file *cio_file_open(struct cio_ctx *ctx,
struct cio_stream *st,
struct cio_chunk *ch,
int flags,
size_t size, int *err)
{
int psize;
int ret;
int len;
char *path;
struct stat f_st;
struct cio_file *cf;
len = strlen(ch->name);
if (len == 1 && (ch->name[0] == '.' || ch->name[0] == '/')) {
cio_log_error(ctx, "[cio file] invalid file name");
return NULL;
}
/* Compose path for the file */
psize = strlen(ctx->root_path) + strlen(st->name) + strlen(ch->name);
psize += 8;
path = malloc(psize);
if (!path) {
cio_errno();
return NULL;
}
ret = snprintf(path, psize, "%s/%s/%s",
ctx->root_path, st->name, ch->name);
if (ret == -1) {
cio_errno();
free(path);
return NULL;
}
/* Create file context */
cf = calloc(1, sizeof(struct cio_file));
if (!cf) {
cio_errno();
free(path);
return NULL;
}
cf->fd = -1;
cf->flags = flags;
cf->realloc_size = getpagesize() * 8;
cf->st_content = NULL;
cf->crc_cur = cio_crc32_init();
cf->path = path;
cf->map = NULL;
ch->backend = cf;
/* Should we open and put this file up ? */
ret = open_and_up(ctx);
if (ret == CIO_FALSE) {
/* make sure to set the file size before to return */
ret = stat(cf->path, &f_st);
if (ret == 0) {
cf->fs_size = f_st.st_size;
}
/* we reached our limit, let the file 'down' */
return cf;
}
/* Open file (file descriptor and set file size) */
ret = file_open(ctx, cf);
if (ret == -1) {
cio_file_close(ch, CIO_FALSE);
*err = CIO_ERROR;
return NULL;
}
/* Map the file */
ret = mmap_file(ctx, ch, cf->fs_size);
if (ret == CIO_ERROR || ret == CIO_CORRUPTED || ret == CIO_RETRY) {
cio_file_close(ch, CIO_FALSE);
*err = ret;
return NULL;
}
*err = CIO_OK;
return cf;
}
/*
* Put a file content back into memory, only IF it has been set 'down'
* before.
*/
static int _cio_file_up(struct cio_chunk *ch, int enforced)
{
int ret;
struct cio_file *cf = (struct cio_file *) ch->backend;
if (cf->map) {
cio_log_error(ch->ctx, "[cio file] file is already mapped: %s/%s",
ch->st->name, ch->name);
return CIO_ERROR;
}
if (cf->fd > 0) {
cio_log_error(ch->ctx, "[cio file] file descriptor already exists: "
"[fd=%i] %s:%s", cf->fd, ch->st->name, ch->name);
return CIO_ERROR;
}
/*
* Enforced mechanism provides safety based on Chunk I/O storage
* pre-set limits.
*/
if (enforced == CIO_TRUE) {
ret = open_and_up(ch->ctx);
if (ret == CIO_FALSE) {
return CIO_ERROR;
}
}
/* Open file */
ret = file_open(ch->ctx, cf);
if (ret == -1) {
cio_log_error(ch->ctx, "[cio file] cannot open chunk: %s/%s",
ch->st->name, ch->name);
return CIO_ERROR;
}
/*
* Map content:
*
* return values = CIO_OK, CIO_ERROR, CIO_CORRUPTED or CIO_RETRY
*/
ret = mmap_file(ch->ctx, ch, cf->fs_size);
if (ret == CIO_ERROR) {
cio_log_error(ch->ctx, "[cio file] cannot map chunk: %s/%s",
ch->st->name, ch->name);
}
return ret;
}
/*
* Load a file using 'enforced' mode: do not load the file in memory
* if we already passed memory or max_chunks_up restrictions.
*/
int cio_file_up(struct cio_chunk *ch)
{
return _cio_file_up(ch, CIO_TRUE);
}
/* Load a file in non-enforced mode. This means it will load the file
* in memory skipping restrictions set by configuration.
*
* The use case of this call is when the caller needs to write data
* to a file which is down due to restrictions. But then the caller
* must put the chunk 'down' again if that was it original status.
*/
int cio_file_up_force(struct cio_chunk *ch)
{
return _cio_file_up(ch, CIO_FALSE);
}
/* Release memory and file descriptor resources but keep context */
int cio_file_down(struct cio_chunk *ch)
{
int ret;
struct stat st;
struct cio_file *cf = (struct cio_file *) ch->backend;
if (!cf->map) {
cio_log_error(ch->ctx, "[cio file] file is not mapped: %s/%s",
ch->st->name, ch->name);
return -1;
}
/* unmap memory */
munmap_file(ch->ctx, ch);
/* Allocated map size is zero */
cf->alloc_size = 0;
/* Get file size */
ret = fstat(cf->fd, &st);
if (ret == -1) {
cio_errno();
cf->fs_size = 0;
}
else {
cf->fs_size = st.st_size;
}
/* Close file descriptor */
close(cf->fd);
cf->fd = -1;
cf->map = NULL;
return 0;
}
void cio_file_close(struct cio_chunk *ch, int delete)
{
int ret;
struct cio_file *cf = (struct cio_file *) ch->backend;
if (!cf) {
return;
}
/* Safe unmap of the file content */
munmap_file(ch->ctx, ch);
/* Should we delete the content from the file system ? */
if (delete == CIO_TRUE) {
ret = unlink(cf->path);
if (ret == -1) {
cio_errno();
cio_log_error(ch->ctx,
"[cio file] error deleting file at close %s:%s",
ch->st->name, ch->name);
}
}
/* Close file descriptor */
if (cf->fd > 0) {
close(cf->fd);
}
free(cf->path);
free(cf);
}
int cio_file_write(struct cio_chunk *ch, const void *buf, size_t count)
{
int ret;
int meta_len;
int pre_content;
void *tmp;
size_t av_size;
size_t new_size;
struct cio_file *cf = (struct cio_file *) ch->backend;
if (count == 0) {
/* do nothing */
return 0;
}
if (!ch) {
return -1;
}
if (cio_chunk_is_up(ch) == CIO_FALSE) {
cio_log_error(ch->ctx, "[cio file] file is not mmap()ed: %s:%s",
ch->st->name, ch->name);
return -1;
}
/* get available size */
av_size = get_available_size(cf, &meta_len);
/* validate there is enough space, otherwise resize */
if (av_size < count) {
/* Set the pre-content size (chunk header + metadata) */
pre_content = (CIO_FILE_HEADER_MIN + meta_len);
new_size = cf->alloc_size + cf->realloc_size;
while (new_size < (pre_content + cf->data_size + count)) {
new_size += cf->realloc_size;
}
new_size = ROUND_UP(new_size, ch->ctx->page_size);
ret = cio_file_fs_size_change(cf, new_size);
if (ret == -1) {
cio_errno();
cio_log_error(ch->ctx,
"[cio_file] error setting new file size on write");
return -1;
}
/* OSX mman does not implement mremap or MREMAP_MAYMOVE. */
#ifndef MREMAP_MAYMOVE
if (munmap(cf->map, cf->alloc_size) == -1) {
cio_errno();
return -1;
}
tmp = mmap(0, new_size, PROT_READ | PROT_WRITE, MAP_SHARED, cf->fd, 0);
#else
tmp = mremap(cf->map, cf->alloc_size,
new_size, MREMAP_MAYMOVE);
#endif
if (tmp == MAP_FAILED) {
cio_errno();
cio_log_error(ch->ctx,
"[cio file] data exceeds available space "
"(alloc=%lu current_size=%lu write_size=%lu)",
cf->alloc_size, cf->data_size, count);
return -1;
}
cio_log_debug(ch->ctx,
"[cio file] alloc_size from %lu to %lu",
cf->alloc_size, new_size);
cf->map = tmp;
cf->alloc_size = new_size;
}
if (ch->ctx->flags & CIO_CHECKSUM) {
update_checksum(cf, (unsigned char *) buf, count);
}
cf->st_content = cio_file_st_get_content(cf->map);
memcpy(cf->st_content + cf->data_size, buf, count);
cf->data_size += count;
cf->synced = CIO_FALSE;
return 0;
}
int cio_file_write_metadata(struct cio_chunk *ch, char *buf, size_t size)
{
int ret;
char *meta;
char *cur_content_data;
char *new_content_data;
size_t new_size;
size_t content_av;
size_t meta_av;
void *tmp;
struct cio_file *cf = ch->backend;
if (cio_file_is_up(ch, cf) == CIO_FALSE) {
return -1;
}
/* Get metadata pointer */
meta = cio_file_st_get_meta(cf->map);
/* Check if meta already have some space available to overwrite */
meta_av = cio_file_st_get_meta_len(cf->map);
/* If there is some space available, just overwrite */
if (meta_av >= size) {
/* copy new metadata */
memcpy(meta, buf, size);
/* there are some remaining bytes, adjust.. */
cur_content_data = cio_file_st_get_content(cf->map);
new_content_data = meta + size;
memmove(new_content_data, cur_content_data, cf->data_size);
adjust_layout(ch, cf, size);
return 0;
}
/*
* The optimal case is if there is no content data, the non-optimal case
* where we need to increase the memory map size, move the content area
* bytes to a different position and write the metadata.
*
* Calculate the available space in the content area.
*/
content_av = cf->alloc_size - cf->data_size;
/* If there is no enough space, increase the file size and it memory map */
if (content_av < size) {
new_size = (size - meta_av) + cf->data_size + CIO_FILE_HEADER_MIN;
/* OSX mman does not implement mremap or MREMAP_MAYMOVE. */
#ifndef MREMAP_MAYMOVE
if (munmap(cf->map, cf->alloc_size) == -1) {
cio_errno();
return -1;
}
tmp = mmap(0, new_size, PROT_READ | PROT_WRITE, MAP_SHARED, cf->fd, 0);
#else
/* Increase memory map size */
tmp = mremap(cf->map, cf->alloc_size, new_size, MREMAP_MAYMOVE);
#endif
if (tmp == MAP_FAILED) {
cio_errno();
cio_log_error(ch->ctx,
"[cio meta] data exceeds available space "
"(alloc=%lu current_size=%lu meta_size=%lu)",
cf->alloc_size, cf->data_size, size);
return -1;
}
cf->map = tmp;
cf->alloc_size = new_size;
/* Alter file size (file system) */
ret = cio_file_fs_size_change(cf, new_size);
if (ret == -1) {
cio_errno();
return -1;
}
}
/* get meta reference again in case the map address has changed */
meta = cio_file_st_get_meta(cf->map);
/* set new position for the content data */
cur_content_data = cio_file_st_get_content(cf->map);
new_content_data = meta + size;
memmove(new_content_data, cur_content_data, size);
/* copy new metadata */
memcpy(meta, buf, size);
adjust_layout(ch, cf, size);
return 0;
}
int cio_file_sync(struct cio_chunk *ch)
{
int ret;
int meta_len;
int sync_mode;
void *tmp;
size_t old_size;
size_t av_size;
size_t size;
struct stat fst;
struct cio_file *cf = (struct cio_file *) ch->backend;
if (cf->flags & CIO_OPEN_RD) {
return 0;
}
if (cf->synced == CIO_TRUE) {
return 0;
}
ret = fstat(cf->fd, &fst);
if (ret == -1) {
cio_errno();
return -1;
}
/* Save current mmap size */
old_size = cf->alloc_size;
/* If there are extra space, truncate the file size */
av_size = get_available_size(cf, &meta_len);
if (av_size > 0) {
size = cf->alloc_size - av_size;
ret = cio_file_fs_size_change(cf, size);
if (ret == -1) {
cio_errno();
cio_log_error(ch->ctx,
"[cio file sync] error adjusting size at: "
" %s/%s", ch->st->name, ch->name);
}
cf->alloc_size = size;
}
else if (cf->alloc_size > fst.st_size) {
ret = cio_file_fs_size_change(cf, cf->alloc_size);
if (ret == -1) {
cio_errno();
cio_log_error(ch->ctx,
"[cio file sync] error adjusting size at: "
" %s/%s", ch->st->name, ch->name);
}