-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathset.c
More file actions
3369 lines (2850 loc) · 80.7 KB
/
set.c
File metadata and controls
3369 lines (2850 loc) · 80.7 KB
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
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2015-2024, Intel Corporation */
/* Copyright 2025, Hewlett Packard Enterprise Development LP */
/*
* Copyright (c) 2016, Microsoft Corporation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* set.c -- pool set utilities
*/
#if !defined(_GNU_SOURCE)
#define _GNU_SOURCE
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdint.h>
#include <endian.h>
#include <errno.h>
#include <stddef.h>
#include <time.h>
#include <ctype.h>
#include <linux/limits.h>
#include <sys/mman.h>
#include "libpmem.h"
#include "set.h"
#include "file.h"
#include "os.h"
#include "mmap.h"
#include "util.h"
#include "out.h"
#include "dlsym.h"
#include "valgrind_internal.h"
#include "sys_util.h"
#include "util_pmem.h"
#include "fs.h"
#include "os_deep.h"
#include "set_badblocks.h"
#define SIZE_AUTODETECT_STR "AUTO"
#define PMEM_EXT ".pmem"
#define PMEM_EXT_LEN sizeof(PMEM_EXT)
#define PMEM_FILE_PADDING 6
#define PMEM_FILE_NAME_MAX_LEN 20
#define PMEM_FILE_MAX_LEN (PMEM_FILE_NAME_MAX_LEN + PMEM_FILE_PADDING)
int Prefault_at_open = 0;
int Prefault_at_create = 0;
int SDS_at_create = POOL_FEAT_INCOMPAT_DEFAULT & POOL_E_FEAT_SDS ? 1 : 0;
int Fallocate_at_create = 1;
int COW_at_open = 0;
/* list of pool set option names and flags */
static const struct pool_set_option Options[] = {
{ "SINGLEHDR", OPTION_SINGLEHDR },
{ "NOHDRS", OPTION_NOHDRS },
{ NULL, OPTION_UNKNOWN }
};
/* reserve space for size, path and some whitespace and/or comment */
enum parser_codes {
PARSER_CONTINUE = 0,
PARSER_PMEMPOOLSET,
PARSER_REPLICA,
PARSER_INVALID_TOKEN,
PARSER_REMOTE_REPLICA_EXPECTED,
PARSER_WRONG_SIZE,
PARSER_CANNOT_READ_SIZE,
PARSER_ABSOLUTE_PATH_EXPECTED,
PARSER_RELATIVE_PATH_EXPECTED,
PARSER_SET_NO_PARTS,
PARSER_REP_NO_PARTS,
PARSER_SIZE_MISMATCH,
PARSER_OUT_OF_MEMORY,
PARSER_OPTION_UNKNOWN,
PARSER_OPTION_EXPECTED,
PARSER_FORMAT_OK,
PARSER_MAX_CODE
};
static const char *parser_errstr[PARSER_MAX_CODE] = {
"", /* parsing */
"the first line must be exactly 'PMEMPOOLSET'",
"exactly 'REPLICA' expected",
"invalid token found in the current line",
"remote nodes are not supported",
"incorrect format of size",
"cannot determine size of a part",
"incorrect path (must be an absolute one)",
"incorrect descriptor (must be a relative path)",
"no pool set parts",
"no replica parts",
"sizes of pool set and replica mismatch",
"allocating memory failed",
"unknown option",
"missing option name",
"" /* format correct */
};
/*
* util_replica_force_page_allocation - (internal) forces page allocation for
* replica
*/
static void
util_replica_force_page_allocation(struct pool_replica *rep)
{
volatile char *cur_addr = rep->part[0].addr;
char *addr_end = (char *)cur_addr + rep->resvsize;
for (; cur_addr < addr_end; cur_addr += Pagesize) {
*cur_addr = *cur_addr;
VALGRIND_SET_CLEAN(cur_addr, 1);
}
}
/*
* util_map_hdr -- map a header of a pool set
*/
int
util_map_hdr(struct pool_set_part *part, int flags, int rdonly)
{
LOG(3, "part %p flags %d", part, flags);
COMPILE_ERROR_ON(POOL_HDR_SIZE == 0);
ASSERTeq(POOL_HDR_SIZE % Pagesize, 0);
/*
* Workaround for Device DAX not allowing to map a portion
* of the device if offset/length are not aligned to the internal
* device alignment (page size). I.e. if the device alignment
* is 2M, we cannot map the 4K header, but need to align the mapping
* length to 2M.
*
* According to mmap(2), system should automatically align mapping
* length to be a multiple of the underlying page size, but it's
* not true for Device DAX.
*/
size_t hdrsize = part->alignment > POOL_HDR_SIZE
? part->alignment : POOL_HDR_SIZE;
void *addr = NULL;
#if VG_MEMCHECK_ENABLED
if (On_valgrind) {
/* this is required only for Device DAX & memcheck */
addr = util_map_hint(hdrsize, hdrsize);
if (addr == MAP_FAILED) {
CORE_LOG_ERROR(
"cannot find a contiguous region of given size");
/* there's nothing we can do */
return -1;
}
}
#endif
int prot = rdonly ? PROT_READ : PROT_READ|PROT_WRITE;
void *hdrp = util_map_sync(addr, hdrsize, prot, flags,
part->fd, 0, &part->hdr_map_sync);
if (hdrp == MAP_FAILED) {
ERR_W_ERRNO("mmap: %s", part->path);
return -1;
}
part->hdrsize = hdrsize;
part->hdr = hdrp;
VALGRIND_REGISTER_PMEM_MAPPING(part->hdr, part->hdrsize);
VALGRIND_REGISTER_PMEM_FILE(part->fd, part->hdr, part->hdrsize, 0);
return 0;
}
/*
* util_unmap_hdr -- unmap pool set part header
*/
void
util_unmap_hdr(struct pool_set_part *part)
{
if (part->hdr == NULL || part->hdrsize == 0)
return;
LOG(4, "munmap: addr %p size %zu", part->hdr, part->hdrsize);
VALGRIND_REMOVE_PMEM_MAPPING(part->hdr, part->hdrsize);
if (munmap(part->hdr, part->hdrsize) != 0)
/* this means there's a bug on the caller side */
CORE_LOG_FATAL_W_ERRNO("munmap: %s", part->path);
part->hdr = NULL;
part->hdrsize = 0;
}
/*
* util_map_part -- map a part of a pool set
*/
int
util_map_part(struct pool_set_part *part, void *addr, size_t size,
size_t offset, int flags, int rdonly)
{
LOG(3, "part %p addr %p size %zu offset %zu flags %d",
part, addr, size, offset, flags);
ASSERTeq((uintptr_t)addr % Mmap_align, 0);
ASSERTeq(offset % Mmap_align, 0);
ASSERTeq(size % Mmap_align, 0);
ASSERT(((os_off_t)offset) >= 0);
ASSERTeq(offset % part->alignment, 0);
ASSERT(offset < part->filesize);
if (!size)
size = (part->filesize - offset) & ~(part->alignment - 1);
else
size = roundup(size, part->alignment);
int prot = rdonly ? PROT_READ : PROT_READ | PROT_WRITE;
void *addrp = util_map_sync(addr, size, prot, flags, part->fd,
(os_off_t)offset, &part->map_sync);
if (addrp == MAP_FAILED) {
ERR_W_ERRNO("mmap: %s", part->path);
return -1;
}
if (addr != NULL && (flags & MAP_FIXED) && addrp != addr) {
ERR_WO_ERRNO("unable to map at requested address %p", addr);
munmap(addrp, size);
return -1;
}
part->addr = addrp;
part->size = size;
VALGRIND_REGISTER_PMEM_MAPPING(part->addr, part->size);
VALGRIND_REGISTER_PMEM_FILE(part->fd, part->addr, part->size, offset);
return 0;
}
/*
* util_unmap_part -- unmap a part of a pool set
*/
int
util_unmap_part(struct pool_set_part *part)
{
LOG(3, "part %p", part);
if (part->addr != NULL && part->size != 0) {
LOG(4, "munmap: addr %p size %zu", part->addr, part->size);
VALGRIND_REMOVE_PMEM_MAPPING(part->addr, part->size);
if (munmap(part->addr, part->size) != 0) {
ERR_W_ERRNO("munmap: %s", part->path);
}
part->addr = NULL;
part->size = 0;
}
return 0;
}
/*
* util_unmap_parts -- unmap parts from start_index to the end_index
*/
int
util_unmap_parts(struct pool_replica *rep, unsigned start_index,
unsigned end_index)
{
LOG(3, "rep: %p, start_index: %u, end_index: %u", rep, start_index,
end_index);
for (unsigned p = start_index; p <= end_index; p++)
util_unmap_part(&rep->part[p]);
return 0;
}
/*
* util_poolset_free -- free pool set info
*/
void
util_poolset_free(struct pool_set *set)
{
LOG(3, "set %p", set);
for (unsigned r = 0; r < set->nreplicas; r++) {
struct pool_replica *rep = set->replica[r];
for (unsigned p = 0; p < rep->nallocated; p++) {
Free((void *)(rep->part[p].path));
}
struct pool_set_directory *d;
VEC_FOREACH_BY_PTR(d, &rep->directory) {
Free((void *)d->path);
}
VEC_DELETE(&rep->directory);
Free(set->replica[r]);
}
Free(set->path);
Free(set);
}
/*
* util_poolset_open -- open all replicas from a poolset
*/
int
util_poolset_open(struct pool_set *set)
{
for (unsigned r = 0; r < set->nreplicas; ++r) {
if (util_replica_open(set, r, MAP_SHARED)) {
CORE_LOG_ERROR("replica open failed: replica %u", r);
errno = EINVAL;
return -1;
}
}
return 0;
}
/*
* util_replica_close_local -- close local replica, optionally delete the
* replica's parts
*/
int
util_replica_close_local(struct pool_replica *rep, unsigned repn,
enum del_parts_mode del)
{
for (unsigned p = 0; p < rep->nparts; p++) {
if (rep->part[p].fd != -1)
(void) os_close(rep->part[p].fd);
if ((del == DELETE_CREATED_PARTS && rep->part[p].created) ||
del == DELETE_ALL_PARTS) {
LOG(4, "unlink %s", rep->part[p].path);
int olderrno = errno;
if (util_unlink(rep->part[p].path) && errno != ENOENT) {
ERR_W_ERRNO(
"unlink %s failed (part %u, replica %u)",
rep->part[p].path, p, repn);
return -1;
}
errno = olderrno;
}
}
return 0;
}
/*
* util_poolset_close -- unmap and close all the parts of the pool set,
* optionally delete parts
*/
void
util_poolset_close(struct pool_set *set, enum del_parts_mode del)
{
LOG(3, "set %p del %d", set, del);
int oerrno = errno;
for (unsigned r = 0; r < set->nreplicas; r++) {
util_replica_close(set, r);
struct pool_replica *rep = set->replica[r];
(void) util_replica_close_local(rep, r, del);
}
util_poolset_free(set);
errno = oerrno;
}
/*
* util_poolset_chmod -- change mode for all created files related to pool set
*/
int
util_poolset_chmod(struct pool_set *set, mode_t mode)
{
LOG(3, "set %p mode %o", set, mode);
for (unsigned r = 0; r < set->nreplicas; r++) {
struct pool_replica *rep = set->replica[r];
for (unsigned p = 0; p < rep->nparts; p++) {
struct pool_set_part *part = &rep->part[p];
/* skip not created or closed parts */
if (!part->created || part->fd == -1)
continue;
os_stat_t stbuf;
if (os_fstat(part->fd, &stbuf) != 0) {
ERR_W_ERRNO("fstat %d %s", part->fd,
part->path);
return -1;
}
if (stbuf.st_mode & ~(unsigned)S_IFMT) {
CORE_LOG_WARNING(
"file permissions changed during pool initialization, file: %s (%o)",
part->path,
stbuf.st_mode & ~(unsigned)S_IFMT);
}
if (os_chmod(part->path, mode)) {
ERR_W_ERRNO("chmod %u/%u/%s", r, p, part->path);
return -1;
}
}
}
return 0;
}
/*
* util_poolset_fdclose_always -- close file descriptors related to pool set
*/
void
util_poolset_fdclose_always(struct pool_set *set)
{
LOG(3, "set %p", set);
for (unsigned r = 0; r < set->nreplicas; r++)
util_replica_fdclose(set->replica[r]);
}
/*
* util_poolset_fdclose -- close pool set file descriptors
*/
void
util_poolset_fdclose(struct pool_set *set)
{
util_poolset_fdclose_always(set);
}
/*
* util_autodetect_size -- (internal) retrieves size of an existing file
*/
static ssize_t
util_autodetect_size(const char *path)
{
enum file_type type = util_file_get_type(path);
if (type < 0)
return -1;
if (type == TYPE_NORMAL) {
ERR_WO_ERRNO(
"size autodetection is supported only for device dax");
return -1;
}
return util_file_get_size(path);
}
/*
* parser_read_line -- (internal) read line and validate size and path
* from a pool set file
*/
static enum parser_codes
parser_read_line(char *line, size_t *size, char **path)
{
int ret;
char *size_str;
char *path_str;
char *rest_str;
char *saveptr = NULL; /* must be NULL initialized on Windows */
size_str = strtok_r(line, " \t", &saveptr);
path_str = strtok_r(NULL, " \t", &saveptr);
rest_str = strtok_r(NULL, " \t", &saveptr);
if (!size_str || !path_str || rest_str)
return PARSER_INVALID_TOKEN;
LOG(10, "size '%s' path '%s'", size_str, path_str);
/*
* A format of the size is checked in detail. As regards the path,
* it is checked only if the read path is an absolute path.
* The rest should be checked during creating/opening the file.
*/
/* check if the read path is an absolute path */
if (!util_is_absolute_path(path_str))
return PARSER_ABSOLUTE_PATH_EXPECTED;
*path = Strdup(path_str);
if (!(*path)) {
ERR_W_ERRNO("Strdup");
return PARSER_OUT_OF_MEMORY;
}
if (strcmp(SIZE_AUTODETECT_STR, size_str) == 0) {
/*
* XXX: this should be done after the parsing completes, but
* currently this operation is performed in simply too many
* places in the code to move this someplace else.
*/
ssize_t s = util_autodetect_size(path_str);
if (s < 0) {
Free(*path);
*path = NULL;
return PARSER_CANNOT_READ_SIZE;
}
*size = (size_t)s;
return PARSER_CONTINUE;
}
ret = util_parse_size(size_str, size);
if (ret != 0 || *size == 0) {
Free(*path);
*path = NULL;
return PARSER_WRONG_SIZE;
}
return PARSER_CONTINUE;
}
/*
* parser_read_options -- (internal) read line and validate options
*/
static enum parser_codes
parser_read_options(char *line, unsigned *options)
{
LOG(3, "line '%s'", line);
int opt_cnt = 0;
char *saveptr = NULL; /* must be NULL initialized on Windows */
char *opt_str = strtok_r(line, " \t", &saveptr);
while (opt_str != NULL) {
LOG(4, "option '%s'", opt_str);
int i = 0;
while (Options[i].name && strcmp(opt_str, Options[i].name) != 0)
i++;
if (Options[i].name == NULL) {
LOG(4, "unknown option '%s'", opt_str);
return PARSER_OPTION_UNKNOWN;
}
if (*options & Options[i].flag)
LOG(4, "duplicated option '%s'", opt_str);
*options |= Options[i].flag;
opt_cnt++;
opt_str = strtok_r(NULL, " \t", &saveptr);
}
if (opt_cnt == 0)
return PARSER_OPTION_EXPECTED;
return PARSER_CONTINUE;
}
/*
* parser_read_replica -- (internal) read line and validate remote replica
* from a pool set file
*/
static enum parser_codes
parser_read_replica(char *line, char **node_addr, char **pool_desc)
{
char *addr_str;
char *desc_str;
char *rest_str;
char *saveptr = NULL; /* must be NULL initialized on Windows */
addr_str = strtok_r(line, " \t", &saveptr);
desc_str = strtok_r(NULL, " \t", &saveptr);
rest_str = strtok_r(NULL, " \t", &saveptr);
if (!addr_str || !desc_str)
return PARSER_REMOTE_REPLICA_EXPECTED;
if (rest_str)
return PARSER_INVALID_TOKEN;
LOG(10, "node address '%s' pool set descriptor '%s'",
addr_str, desc_str);
/* check if the descriptor is a relative path */
if (util_is_absolute_path(desc_str))
return PARSER_RELATIVE_PATH_EXPECTED;
*node_addr = Strdup(addr_str);
*pool_desc = Strdup(desc_str);
if (!(*node_addr) || !(*pool_desc)) {
ERR_W_ERRNO("Strdup");
if (*node_addr)
Free(*node_addr);
if (*pool_desc)
Free(*pool_desc);
return PARSER_OUT_OF_MEMORY;
}
return PARSER_CONTINUE;
}
/*
* util_replica_reserve -- reserves part slots capacity in a replica
*/
static int
util_replica_reserve(struct pool_replica **repp, unsigned n)
{
LOG(3, "replica %p n %u", *repp, n);
struct pool_replica *rep = *repp;
if (rep->nallocated >= n)
return 0;
rep = Realloc(rep, sizeof(struct pool_replica) +
(n) * sizeof(struct pool_set_part));
if (rep == NULL) {
ERR_W_ERRNO("Realloc");
return -1;
}
size_t nsize = sizeof(struct pool_set_part) * (n - rep->nallocated);
memset(rep->part + rep->nallocated, 0, nsize);
rep->nallocated = n;
*repp = rep;
return 0;
}
/*
* util_replica_add_part_by_idx -- (internal) allocates, initializes and adds a
* part structure at the provided location in the replica info
*/
static int
util_replica_add_part_by_idx(struct pool_replica **repp,
const char *path, size_t filesize, unsigned p)
{
LOG(3, "replica %p path %s filesize %zu", *repp, path, filesize);
if (util_replica_reserve(repp, p + 1) != 0)
return -1;
struct pool_replica *rep = *repp;
ASSERTne(rep, NULL);
int is_dev_dax = 0;
if (path != NULL) {
enum file_type type = util_file_get_type(path);
if (type == OTHER_ERROR)
return -1;
is_dev_dax = type == TYPE_DEVDAX;
}
rep->part[p].path = path;
rep->part[p].filesize = filesize;
rep->part[p].fd = -1;
rep->part[p].is_dev_dax = is_dev_dax;
rep->part[p].created = 0;
rep->part[p].hdr = NULL;
rep->part[p].addr = NULL;
rep->part[p].has_bad_blocks = 0;
if (is_dev_dax)
rep->part[p].alignment = util_file_device_dax_alignment(path);
else
rep->part[p].alignment = Mmap_align;
ASSERTne(rep->part[p].alignment, 0);
rep->nparts += 1;
return 0;
}
/*
* util_replica_add_part -- adds a next part in replica info
*/
static int
util_replica_add_part(struct pool_replica **repp,
const char *path, size_t filesize)
{
LOG(3, "replica %p path \"%s\" filesize %zu", *repp, path, filesize);
return util_replica_add_part_by_idx(repp, path,
filesize, (*repp)->nparts);
}
/*
* util_parse_add_part -- (internal) add a new part file to the replica info
*/
static int
util_parse_add_part(struct pool_set *set, const char *path, size_t filesize)
{
LOG(3, "set %p path %s filesize %zu", set, path, filesize);
ASSERTne(set, NULL);
if (set->directory_based) {
ERR_WO_ERRNO("cannot mix directories and files in a set");
errno = EINVAL;
return -1;
}
return util_replica_add_part(&set->replica[set->nreplicas - 1],
path, filesize);
}
/*
* util_parse_add_directory --
* (internal) add a new directory to the replica info
*/
static int
util_parse_add_directory(struct pool_set *set, const char *path,
size_t filesize)
{
LOG(3, "set %p path %s filesize %zu", set, path, filesize);
ASSERTne(set, NULL);
struct pool_replica *rep = set->replica[set->nreplicas - 1];
ASSERTne(rep, NULL);
if (set->directory_based == 0) {
if (rep->nparts > 0 || set->nreplicas > 1) {
ERR_WO_ERRNO(
"cannot mix directories and files in a set");
errno = EINVAL;
return -1;
}
set->directory_based = 1;
}
char *rpath = util_part_realpath(path);
if (rpath == NULL) {
ERR_WO_ERRNO("cannot resolve realpath of new directory");
return -1;
}
for (unsigned i = 0; i < set->nreplicas; ++i) {
struct pool_replica *r = set->replica[i];
struct pool_set_directory *dir;
char *dpath = NULL;
VEC_FOREACH_BY_PTR(dir, &r->directory) {
dpath = util_part_realpath(dir->path);
ASSERTne(dpath, NULL); /* must have been resolved */
if (strcmp(rpath, dpath) == 0) {
ERR_WO_ERRNO(
"cannot use the same directory twice");
errno = EEXIST;
free(dpath);
free(rpath);
return -1;
}
free(dpath);
}
}
free(rpath);
struct pool_set_directory d;
d.path = path;
d.resvsize = filesize;
if (VEC_PUSH_BACK(&rep->directory, d) != 0)
return -1;
rep->resvsize += filesize;
return 0;
}
/*
* util_parse_add_element --
* (internal) add a new element to the replica info
*/
static int
util_parse_add_element(struct pool_set *set, const char *path, size_t filesize)
{
LOG(3, "set %p path %s filesize %zu", set, path, filesize);
os_stat_t stat;
int olderrno = errno;
if (os_stat(path, &stat) == 0 && S_ISDIR(stat.st_mode))
return util_parse_add_directory(set, path, filesize);
errno = olderrno;
return util_parse_add_part(set, path, filesize);
}
/*
* util_parse_add_replica -- (internal) add a new replica to the pool set info
*/
static int
util_parse_add_replica(struct pool_set **setp)
{
LOG(3, "setp %p", setp);
ASSERTne(setp, NULL);
struct pool_set *set = *setp;
ASSERTne(set, NULL);
set = Realloc(set, sizeof(struct pool_set) +
(set->nreplicas + 1) * sizeof(struct pool_replica *));
if (set == NULL) {
ERR_W_ERRNO("Realloc");
return -1;
}
*setp = set;
struct pool_replica *rep;
rep = Zalloc(sizeof(struct pool_replica));
if (rep == NULL) {
ERR_W_ERRNO("Zalloc");
return -1;
}
VEC_INIT(&rep->directory);
unsigned r = set->nreplicas++;
set->replica[r] = rep;
return 0;
}
/*
* util_replica_check_map_sync -- (internal) check MAP_SYNC restrictions
*/
static int
util_replica_check_map_sync(struct pool_set *set, unsigned repidx,
int check_hdr)
{
LOG(3, "set %p repidx %u", set, repidx);
struct pool_replica *rep = set->replica[repidx];
int map_sync = rep->part[0].map_sync;
for (unsigned p = 1; p < rep->nparts; p++) {
if (map_sync != rep->part[p].map_sync) {
ERR_WO_ERRNO(
"replica #%u part %u %smapped with MAP_SYNC",
repidx, p, rep->part[p].map_sync ? "" : "not");
return -1;
}
}
if (check_hdr) {
for (unsigned p = 0; p < rep->nhdrs; p++) {
if (map_sync != rep->part[p].hdr_map_sync) {
ERR_WO_ERRNO(
"replica #%u part %u header %smapped with MAP_SYNC",
repidx, p, rep->part[p].hdr_map_sync ?
"" : "not");
return -1;
}
}
}
return 0;
}
/*
* util_poolset_check_devdax -- (internal) check Device DAX restrictions
*/
static int
util_poolset_check_devdax(struct pool_set *set)
{
LOG(3, "set %p", set);
if (set->directory_based)
return 0;
for (unsigned r = 0; r < set->nreplicas; r++) {
struct pool_replica *rep = set->replica[r];
int is_dev_dax = rep->part[0].is_dev_dax;
for (unsigned p = 0; p < rep->nparts; p++) {
if (rep->part[p].is_dev_dax != is_dev_dax) {
ERR_WO_ERRNO(
"either all the parts must be Device DAX or none");
return -1;
}
if (is_dev_dax && rep->nparts > 1 &&
(set->options & (OPTION_SINGLEHDR |
OPTION_NOHDRS)) == 0 &&
util_file_device_dax_alignment(rep->part[p].path)
!= Pagesize) {
ERR_WO_ERRNO(
"Multiple DAX devices with alignment other than 4KB. Use the SINGLEHDR poolset option.");
return -1;
}
}
}
return 0;
}
/*
* util_poolset_check_options -- (internal) check if poolset options are
* admissible
*/
static int
util_poolset_check_options(struct pool_set *set)
{
LOG(3, "set %p", set);
if ((set->options & OPTION_SINGLEHDR) &&
(set->options & OPTION_NOHDRS)) {
ERR_WO_ERRNO(
"both SINGLEHDR and NOHDR poolset options used at the same time");
return -1;
}
return 0;
}
/*
* util_poolset_set_size -- (internal) calculate pool size
*/
static void
util_poolset_set_size(struct pool_set *set)
{
LOG(3, "set %p", set);
set->poolsize = SIZE_MAX;
set->resvsize = SIZE_MAX;
for (unsigned r = 0; r < set->nreplicas; r++) {
struct pool_replica *rep = set->replica[r];
if (set->options & OPTION_SINGLEHDR)
rep->nhdrs = 1;
else if (set->options & OPTION_NOHDRS)
rep->nhdrs = 0;
else
rep->nhdrs = rep->nparts;
rep->repsize = 0;
for (unsigned p = 0; p < rep->nparts; p++) {
rep->repsize +=
(rep->part[p].filesize & ~(Mmap_align - 1));
}
if (rep->nhdrs > 0)
rep->repsize -= (rep->nhdrs - 1) * Mmap_align;
if (rep->resvsize == 0)
rep->resvsize = rep->repsize;
/*
* Calculate pool size - choose the smallest replica size.
*/
if (rep->repsize < set->poolsize)
set->poolsize = rep->repsize;
if (rep->resvsize < set->resvsize)
set->resvsize = rep->resvsize;
}
LOG(3, "pool size set to %zu", set->poolsize);
}
/*
* util_part_idx_by_file_name -- (internal) retrieves the part index from a
* name of the file that is an element of a directory poolset
*/