-
Notifications
You must be signed in to change notification settings - Fork 508
/
Copy pathobj.c
2940 lines (2400 loc) · 64.7 KB
/
obj.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
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2014-2023, Intel Corporation */
/*
* obj.c -- transactional object store implementation
*/
#include <inttypes.h>
#include <limits.h>
#include <wchar.h>
#include <stdbool.h>
#include "valgrind_internal.h"
#include "libpmem.h"
#include "memblock.h"
#include "critnib.h"
#include "list.h"
#include "mmap.h"
#include "obj.h"
#include "ctl_global.h"
#include "ravl.h"
#include "heap_layout.h"
#include "os.h"
#include "os_thread.h"
#include "pmemops.h"
#include "set.h"
#include "sync.h"
#include "tx.h"
#include "sys_util.h"
/*
* The variable from which the config is directly loaded. The string
* cannot contain any comments or extraneous white characters.
*/
#define OBJ_CONFIG_ENV_VARIABLE "PMEMOBJ_CONF"
/*
* The variable that points to a config file from which the config is loaded.
*/
#define OBJ_CONFIG_FILE_ENV_VARIABLE "PMEMOBJ_CONF_FILE"
/*
* The variable which overwrites a number of lanes available at runtime.
*/
#define OBJ_NLANES_ENV_VARIABLE "PMEMOBJ_NLANES"
#define OBJ_X_VALID_FLAGS PMEMOBJ_F_RELAXED
static const struct pool_attr Obj_create_attr = {
OBJ_HDR_SIG,
OBJ_FORMAT_MAJOR,
OBJ_FORMAT_FEAT_DEFAULT,
{0}, {0}, {0}, {0}, {0}
};
static const struct pool_attr Obj_open_attr = {
OBJ_HDR_SIG,
OBJ_FORMAT_MAJOR,
OBJ_FORMAT_FEAT_CHECK,
{0}, {0}, {0}, {0}, {0}
};
static struct critnib *pools_ht; /* hash table used for searching by UUID */
static struct critnib *pools_tree; /* tree used for searching by address */
int _pobj_cache_invalidate;
static os_mutex_t pools_mutex;
__thread struct _pobj_pcache _pobj_cached_pool;
/*
* pmemobj_direct -- returns the direct pointer of an object
*/
void *
pmemobj_direct(PMEMoid oid)
{
return pmemobj_direct_inline(oid);
}
/*
* obj_ctl_init_and_load -- (static) initializes CTL and loads configuration
* from env variable and file
*/
static int
obj_ctl_init_and_load(PMEMobjpool *pop)
{
LOG(3, "pop %p", pop);
if (pop != NULL && (pop->ctl = ctl_new()) == NULL) {
LOG(2, "!ctl_new");
return -1;
}
if (pop) {
tx_ctl_register(pop);
pmalloc_ctl_register(pop);
stats_ctl_register(pop);
debug_ctl_register(pop);
}
char *env_config = os_getenv(OBJ_CONFIG_ENV_VARIABLE);
if (env_config != NULL) {
if (ctl_load_config_from_string(pop ? pop->ctl : NULL,
pop, env_config) != 0) {
LOG(2, "unable to parse config stored in %s "
"environment variable",
OBJ_CONFIG_ENV_VARIABLE);
goto err;
}
}
char *env_config_file = os_getenv(OBJ_CONFIG_FILE_ENV_VARIABLE);
if (env_config_file != NULL && env_config_file[0] != '\0') {
if (ctl_load_config_from_file(pop ? pop->ctl : NULL,
pop, env_config_file) != 0) {
LOG(2, "unable to parse config stored in %s "
"file (from %s environment variable)",
env_config_file,
OBJ_CONFIG_FILE_ENV_VARIABLE);
goto err;
}
}
return 0;
err:
if (pop)
ctl_delete(pop->ctl);
return -1;
}
/*
* obj_pool_init -- (internal) allocate global structs holding all opened pools
*
* This is invoked on a first call to pmemobj_open() or pmemobj_create().
* Memory is released in library destructor.
*
* This function needs to be threadsafe.
*/
static void
obj_pool_init(void)
{
LOG(3, NULL);
struct critnib *c;
if (pools_ht == NULL) {
c = critnib_new();
if (c == NULL)
FATAL("!critnib_new for pools_ht");
if (!util_bool_compare_and_swap64(&pools_ht, NULL, c))
critnib_delete(c);
}
if (pools_tree == NULL) {
c = critnib_new();
if (c == NULL)
FATAL("!critnib_new for pools_tree");
if (!util_bool_compare_and_swap64(&pools_tree, NULL, c))
critnib_delete(c);
}
}
/*
* pmemobj_oid -- return a PMEMoid based on the virtual address
*
* If the address does not belong to any pool OID_NULL is returned.
*/
PMEMoid
pmemobj_oid(const void *addr)
{
PMEMobjpool *pop = pmemobj_pool_by_ptr(addr);
if (pop == NULL)
return OID_NULL;
PMEMoid oid = {pop->uuid_lo, (uintptr_t)addr - (uintptr_t)pop};
return oid;
}
/*
* obj_init -- initialization of obj
*
* Called by constructor.
*/
void
obj_init(void)
{
LOG(3, NULL);
COMPILE_ERROR_ON(sizeof(struct pmemobjpool) !=
POOL_HDR_SIZE + POOL_DESC_SIZE);
COMPILE_ERROR_ON(PMEMOBJ_F_MEM_NODRAIN != PMEM_F_MEM_NODRAIN);
COMPILE_ERROR_ON(PMEMOBJ_F_MEM_NONTEMPORAL != PMEM_F_MEM_NONTEMPORAL);
COMPILE_ERROR_ON(PMEMOBJ_F_MEM_TEMPORAL != PMEM_F_MEM_TEMPORAL);
COMPILE_ERROR_ON(PMEMOBJ_F_MEM_WC != PMEM_F_MEM_WC);
COMPILE_ERROR_ON(PMEMOBJ_F_MEM_WB != PMEM_F_MEM_WB);
COMPILE_ERROR_ON(PMEMOBJ_F_MEM_NOFLUSH != PMEM_F_MEM_NOFLUSH);
os_mutex_init(&pools_mutex);
/*
* Load global config, ignore any issues. They will be caught on the
* subsequent call to this function for individual pools.
*/
ctl_global_register();
pmalloc_global_ctl_register();
if (obj_ctl_init_and_load(NULL))
FATAL("error: %s", pmemobj_errormsg());
lane_info_boot();
}
/*
* obj_fini -- cleanup of obj
*
* Called by destructor.
*/
void
obj_fini(void)
{
LOG(3, NULL);
if (pools_ht)
critnib_delete(pools_ht);
if (pools_tree)
critnib_delete(pools_tree);
lane_info_destroy();
os_mutex_destroy(&pools_mutex);
}
/*
* obj_drain_empty -- (internal) empty function for drain on non-pmem memory
*/
static void
obj_drain_empty(void)
{
/* do nothing */
}
/*
* obj_msync_nofail -- (internal) pmem_msync wrapper that never fails from
* caller's perspective
*/
static void
obj_msync_nofail(const void *addr, size_t size)
{
if (pmem_msync(addr, size))
FATAL("!pmem_msync");
}
/*
* obj_nopmem_memcpy -- (internal) memcpy followed by an msync
*/
static void *
obj_nopmem_memcpy(void *dest, const void *src, size_t len, unsigned flags)
{
LOG(15, "dest %p src %p len %zu flags 0x%x", dest, src, len, flags);
/*
* Use pmem_memcpy instead of memcpy, because pmemobj_memcpy is supposed
* to guarantee that multiple of 8 byte stores to 8 byte aligned
* addresses are fail safe atomic. pmem_memcpy guarantees that, while
* libc memcpy does not.
*/
pmem_memcpy(dest, src, len, PMEM_F_MEM_NOFLUSH);
obj_msync_nofail(dest, len);
return dest;
}
/*
* obj_nopmem_memmove -- (internal) memmove followed by an msync
*/
static void *
obj_nopmem_memmove(void *dest, const void *src, size_t len, unsigned flags)
{
LOG(15, "dest %p src %p len %zu flags 0x%x", dest, src, len, flags);
/* see comment in obj_nopmem_memcpy */
pmem_memmove(dest, src, len, PMEM_F_MEM_NOFLUSH);
obj_msync_nofail(dest, len);
return dest;
}
/*
* obj_nopmem_memset -- (internal) memset followed by an msync
*/
static void *
obj_nopmem_memset(void *dest, int c, size_t len, unsigned flags)
{
LOG(15, "dest %p c 0x%02x len %zu flags 0x%x", dest, c, len, flags);
/* see comment in obj_nopmem_memcpy */
pmem_memset(dest, c, len, PMEM_F_MEM_NOFLUSH);
obj_msync_nofail(dest, len);
return dest;
}
/*
* XXX - Consider removing obj_norep_*() wrappers to call *_local()
* functions directly. Alternatively, always use obj_rep_*(), even
* if there are no replicas. Verify the performance penalty.
*/
/*
* obj_norep_memcpy -- (internal) memcpy w/o replication
*/
static void *
obj_norep_memcpy(void *ctx, void *dest, const void *src, size_t len,
unsigned flags)
{
PMEMobjpool *pop = ctx;
LOG(15, "pop %p dest %p src %p len %zu flags 0x%x", pop, dest, src, len,
flags);
return pop->memcpy_local(dest, src, len,
flags & PMEM_F_MEM_VALID_FLAGS);
}
/*
* obj_norep_memmove -- (internal) memmove w/o replication
*/
static void *
obj_norep_memmove(void *ctx, void *dest, const void *src, size_t len,
unsigned flags)
{
PMEMobjpool *pop = ctx;
LOG(15, "pop %p dest %p src %p len %zu flags 0x%x", pop, dest, src, len,
flags);
return pop->memmove_local(dest, src, len,
flags & PMEM_F_MEM_VALID_FLAGS);
}
/*
* obj_norep_memset -- (internal) memset w/o replication
*/
static void *
obj_norep_memset(void *ctx, void *dest, int c, size_t len, unsigned flags)
{
PMEMobjpool *pop = ctx;
LOG(15, "pop %p dest %p c 0x%02x len %zu flags 0x%x", pop, dest, c, len,
flags);
return pop->memset_local(dest, c, len, flags & PMEM_F_MEM_VALID_FLAGS);
}
/*
* obj_norep_persist -- (internal) persist w/o replication
*/
static int
obj_norep_persist(void *ctx, const void *addr, size_t len, unsigned flags)
{
/* suppress unused-parameter errors */
SUPPRESS_UNUSED(flags);
PMEMobjpool *pop = ctx;
LOG(15, "pop %p addr %p len %zu", pop, addr, len);
pop->persist_local(addr, len);
return 0;
}
/*
* obj_norep_flush -- (internal) flush w/o replication
*/
static int
obj_norep_flush(void *ctx, const void *addr, size_t len, unsigned flags)
{
/* suppress unused-parameter errors */
SUPPRESS_UNUSED(flags);
PMEMobjpool *pop = ctx;
LOG(15, "pop %p addr %p len %zu", pop, addr, len);
pop->flush_local(addr, len);
return 0;
}
/*
* obj_norep_drain -- (internal) drain w/o replication
*/
static void
obj_norep_drain(void *ctx)
{
PMEMobjpool *pop = ctx;
LOG(15, "pop %p", pop);
pop->drain_local();
}
static void obj_pool_cleanup(PMEMobjpool *pop);
/*
* obj_rep_memcpy -- (internal) memcpy with replication
*/
static void *
obj_rep_memcpy(void *ctx, void *dest, const void *src, size_t len,
unsigned flags)
{
PMEMobjpool *pop = ctx;
LOG(15, "pop %p dest %p src %p len %zu flags 0x%x", pop, dest, src, len,
flags);
void *ret = pop->memcpy_local(dest, src, len, flags);
PMEMobjpool *rep = pop->replica;
while (rep) {
void *rdest = (char *)rep + (uintptr_t)dest - (uintptr_t)pop;
rep->memcpy_local(rdest, src, len,
flags & PMEM_F_MEM_VALID_FLAGS);
rep = rep->replica;
}
return ret;
}
/*
* obj_rep_memmove -- (internal) memmove with replication
*/
static void *
obj_rep_memmove(void *ctx, void *dest, const void *src, size_t len,
unsigned flags)
{
PMEMobjpool *pop = ctx;
LOG(15, "pop %p dest %p src %p len %zu flags 0x%x", pop, dest, src, len,
flags);
void *ret = pop->memmove_local(dest, src, len, flags);
PMEMobjpool *rep = pop->replica;
while (rep) {
void *rdest = (char *)rep + (uintptr_t)dest - (uintptr_t)pop;
rep->memmove_local(rdest, src, len,
flags & PMEM_F_MEM_VALID_FLAGS);
rep = rep->replica;
}
return ret;
}
/*
* obj_rep_memset -- (internal) memset with replication
*/
static void *
obj_rep_memset(void *ctx, void *dest, int c, size_t len, unsigned flags)
{
PMEMobjpool *pop = ctx;
LOG(15, "pop %p dest %p c 0x%02x len %zu flags 0x%x", pop, dest, c, len,
flags);
void *ret = pop->memset_local(dest, c, len, flags);
PMEMobjpool *rep = pop->replica;
while (rep) {
void *rdest = (char *)rep + (uintptr_t)dest - (uintptr_t)pop;
rep->memset_local(rdest, c, len,
flags & PMEM_F_MEM_VALID_FLAGS);
rep = rep->replica;
}
return ret;
}
/*
* obj_rep_persist -- (internal) persist with replication
*/
static int
obj_rep_persist(void *ctx, const void *addr, size_t len, unsigned flags)
{
/* suppress unused-parameter errors */
SUPPRESS_UNUSED(flags);
PMEMobjpool *pop = ctx;
LOG(15, "pop %p addr %p len %zu", pop, addr, len);
pop->persist_local(addr, len);
PMEMobjpool *rep = pop->replica;
while (rep) {
void *raddr = (char *)rep + (uintptr_t)addr - (uintptr_t)pop;
rep->memcpy_local(raddr, addr, len, 0);
rep = rep->replica;
}
return 0;
}
/*
* obj_rep_flush -- (internal) flush with replication
*/
static int
obj_rep_flush(void *ctx, const void *addr, size_t len, unsigned flags)
{
/* suppress unused-parameter errors */
SUPPRESS_UNUSED(flags);
PMEMobjpool *pop = ctx;
LOG(15, "pop %p addr %p len %zu", pop, addr, len);
pop->flush_local(addr, len);
PMEMobjpool *rep = pop->replica;
while (rep) {
void *raddr = (char *)rep + (uintptr_t)addr - (uintptr_t)pop;
rep->memcpy_local(raddr, addr, len, PMEM_F_MEM_NODRAIN);
rep = rep->replica;
}
return 0;
}
/*
* obj_rep_drain -- (internal) drain with replication
*/
static void
obj_rep_drain(void *ctx)
{
PMEMobjpool *pop = ctx;
LOG(15, "pop %p", pop);
pop->drain_local();
PMEMobjpool *rep = pop->replica;
while (rep) {
rep->drain_local();
rep = rep->replica;
}
}
#if VG_MEMCHECK_ENABLED
/*
* Arbitrary value. When there's more undefined regions than MAX_UNDEFS, it's
* not worth reporting everything - developer should fix the code.
*/
#define MAX_UNDEFS 1000
/*
* obj_vg_check_no_undef -- (internal) check whether there are any undefined
* regions
*/
static void
obj_vg_check_no_undef(struct pmemobjpool *pop)
{
LOG(4, "pop %p", pop);
static struct {
void *start, *end;
} undefs[MAX_UNDEFS];
int num_undefs = 0;
VALGRIND_DO_DISABLE_ERROR_REPORTING;
char *addr_start = pop->addr;
char *addr_end = addr_start + pop->set->poolsize;
while (addr_start < addr_end) {
char *noaccess = (char *)VALGRIND_CHECK_MEM_IS_ADDRESSABLE(
addr_start, addr_end - addr_start);
if (noaccess == NULL)
noaccess = addr_end;
while (addr_start < noaccess) {
char *undefined =
(char *)VALGRIND_CHECK_MEM_IS_DEFINED(
addr_start, noaccess - addr_start);
if (undefined) {
addr_start = undefined;
#ifdef VALGRIND_CHECK_MEM_IS_UNDEFINED
addr_start = (char *)
VALGRIND_CHECK_MEM_IS_UNDEFINED(
addr_start, noaccess - addr_start);
if (addr_start == NULL)
addr_start = noaccess;
#else
while (addr_start < noaccess &&
VALGRIND_CHECK_MEM_IS_DEFINED(
addr_start, 1))
addr_start++;
#endif
if (num_undefs < MAX_UNDEFS) {
undefs[num_undefs].start = undefined;
undefs[num_undefs].end = addr_start - 1;
num_undefs++;
}
} else
addr_start = noaccess;
}
#ifdef VALGRIND_CHECK_MEM_IS_UNADDRESSABLE
addr_start = (char *)VALGRIND_CHECK_MEM_IS_UNADDRESSABLE(
addr_start, addr_end - addr_start);
if (addr_start == NULL)
addr_start = addr_end;
#else
while (addr_start < addr_end &&
(char *)VALGRIND_CHECK_MEM_IS_ADDRESSABLE(
addr_start, 1) == addr_start)
addr_start++;
#endif
}
VALGRIND_DO_ENABLE_ERROR_REPORTING;
if (num_undefs) {
/*
* How to resolve this error:
* If it's part of the free space Valgrind should be told about
* it by VALGRIND_DO_MAKE_MEM_NOACCESS request. If it's
* allocated - initialize it or use VALGRIND_DO_MAKE_MEM_DEFINED
* request.
*/
VALGRIND_PRINTF("Part of the pool is left in undefined state on"
" boot. This is pmemobj's bug.\nUndefined"
" regions: [pool address: %p]\n", pop);
for (int i = 0; i < num_undefs; ++i)
VALGRIND_PRINTF(" [%p, %p]\n", undefs[i].start,
undefs[i].end);
if (num_undefs == MAX_UNDEFS)
VALGRIND_PRINTF(" ...\n");
/* Trigger error. */
VALGRIND_CHECK_MEM_IS_DEFINED(undefs[0].start, 1);
}
}
/*
* obj_vg_boot -- (internal) notify Valgrind about pool objects
*/
static void
obj_vg_boot(struct pmemobjpool *pop)
{
if (!On_memcheck)
return;
LOG(4, "pop %p", pop);
if (os_getenv("PMEMOBJ_VG_CHECK_UNDEF"))
obj_vg_check_no_undef(pop);
}
#endif /* VG_MEMCHECK_ENABLED */
/*
* obj_runtime_init_common -- (internal) runtime initialization
*
* Common routine for create/open and check.
*/
static int
obj_runtime_init_common(PMEMobjpool *pop)
{
LOG(3, "pop %p", pop);
if ((errno = lane_boot(pop)) != 0) {
ERR("!lane_boot");
return errno;
}
if ((errno = lane_recover_and_section_boot(pop)) != 0) {
ERR("!lane_recover_and_section_boot");
return errno;
}
pop->conversion_flags = 0;
pmemops_persist(&pop->p_ops,
&pop->conversion_flags, sizeof(pop->conversion_flags));
return 0;
}
/*
* obj_runtime_cleanup_common -- (internal) runtime cleanup
*
* Common routine for create/open and check
*/
static void
obj_runtime_cleanup_common(PMEMobjpool *pop)
{
lane_section_cleanup(pop);
lane_cleanup(pop);
}
/*
* obj_descr_create -- (internal) create obj pool descriptor
*/
static int
obj_descr_create(PMEMobjpool *pop, const char *layout, size_t poolsize)
{
LOG(3, "pop %p layout %s poolsize %zu", pop, layout, poolsize);
ASSERTeq(poolsize % Pagesize, 0);
/* opaque info lives at the beginning of mapped memory pool */
void *dscp = (void *)((uintptr_t)pop + sizeof(struct pool_hdr));
/* create the persistent part of pool's descriptor */
memset(dscp, 0, OBJ_DSC_P_SIZE);
if (layout)
strncpy(pop->layout, layout, PMEMOBJ_MAX_LAYOUT - 1);
struct pmem_ops *p_ops = &pop->p_ops;
pop->lanes_offset = OBJ_LANES_OFFSET;
pop->nlanes = OBJ_NLANES;
/* zero all lanes */
lane_init_data(pop);
pop->heap_offset = pop->lanes_offset +
pop->nlanes * sizeof(struct lane_layout);
pop->heap_offset = (pop->heap_offset + Pagesize - 1) & ~(Pagesize - 1);
size_t heap_size = pop->set->poolsize - pop->heap_offset;
/* initialize heap prior to storing the checksum */
errno = palloc_init((char *)pop + pop->heap_offset, heap_size,
&pop->heap_size, p_ops);
if (errno != 0) {
ERR("!palloc_init");
return -1;
}
util_checksum(dscp, OBJ_DSC_P_SIZE, &pop->checksum, 1, 0);
/*
* store the persistent part of pool's descriptor (2kB)
*
* It's safe to use PMEMOBJ_F_RELAXED flag because the entire
* structure is protected by checksum.
*/
pmemops_xpersist(p_ops, dscp, OBJ_DSC_P_SIZE, PMEMOBJ_F_RELAXED);
/* initialize run_id, it will be incremented later */
pop->run_id = 0;
pmemops_persist(p_ops, &pop->run_id, sizeof(pop->run_id));
pop->root_offset = 0;
pmemops_persist(p_ops, &pop->root_offset, sizeof(pop->root_offset));
pop->root_size = 0;
pmemops_persist(p_ops, &pop->root_size, sizeof(pop->root_size));
pop->conversion_flags = 0;
pmemops_persist(p_ops, &pop->conversion_flags,
sizeof(pop->conversion_flags));
/*
* It's safe to use PMEMOBJ_F_RELAXED flag because the reserved
* area must be entirely zeroed.
*/
pmemops_memset(p_ops, pop->pmem_reserved, 0,
sizeof(pop->pmem_reserved), PMEMOBJ_F_RELAXED);
return 0;
}
/*
* obj_descr_check -- (internal) validate obj pool descriptor
*/
static int
obj_descr_check(PMEMobjpool *pop, const char *layout, size_t poolsize)
{
LOG(3, "pop %p layout %s poolsize %zu", pop, layout, poolsize);
void *dscp = (void *)((uintptr_t)pop + sizeof(struct pool_hdr));
if (!util_checksum(dscp, OBJ_DSC_P_SIZE, &pop->checksum, 0, 0)) {
ERR("invalid checksum of pool descriptor");
errno = EINVAL;
return -1;
}
if (layout &&
strncmp(pop->layout, layout, PMEMOBJ_MAX_LAYOUT)) {
ERR("wrong layout (\"%s\"), "
"pool created with layout \"%s\"",
layout, pop->layout);
errno = EINVAL;
return -1;
}
if (pop->heap_offset % Pagesize) {
ERR("unaligned heap: off %" PRIu64, pop->heap_offset);
errno = EINVAL;
return -1;
}
return 0;
}
/*
* obj_replica_init_local -- (internal) initialize runtime part
* of the local replicas
*/
static int
obj_replica_init_local(PMEMobjpool *rep, int is_pmem, size_t resvsize)
{
LOG(3, "rep %p is_pmem %d resvsize %zu", rep, is_pmem, resvsize);
/*
* Use some of the memory pool area for run-time info. This
* run-time state is never loaded from the file, it is always
* created here, so no need to worry about byte-order.
*/
rep->is_pmem = is_pmem;
/*
* All replicas, except for master, are ignored as far as valgrind is
* concerned. This is to save CPU time and lessen the complexity of
* instrumentation.
*/
if (!rep->is_master_replica)
VALGRIND_ADD_TO_GLOBAL_TX_IGNORE(rep, resvsize);
if (rep->is_pmem) {
rep->persist_local = pmem_persist;
rep->flush_local = pmem_flush;
rep->drain_local = pmem_drain;
rep->memcpy_local = pmem_memcpy;
rep->memmove_local = pmem_memmove;
rep->memset_local = pmem_memset;
} else {
rep->persist_local = obj_msync_nofail;
rep->flush_local = obj_msync_nofail;
rep->drain_local = obj_drain_empty;
rep->memcpy_local = obj_nopmem_memcpy;
rep->memmove_local = obj_nopmem_memmove;
rep->memset_local = obj_nopmem_memset;
}
return 0;
}
/*
* obj_replica_init -- (internal) initialize runtime part of the replica
*/
static int
obj_replica_init(PMEMobjpool *rep, struct pool_set *set, unsigned repidx)
{
struct pool_replica *repset = set->replica[repidx];
if (repidx == 0) {
/* master replica */
rep->is_master_replica = 1;
if (set->nreplicas > 1) {
rep->p_ops.persist = obj_rep_persist;
rep->p_ops.flush = obj_rep_flush;
rep->p_ops.drain = obj_rep_drain;
rep->p_ops.memcpy = obj_rep_memcpy;
rep->p_ops.memmove = obj_rep_memmove;
rep->p_ops.memset = obj_rep_memset;
} else {
rep->p_ops.persist = obj_norep_persist;
rep->p_ops.flush = obj_norep_flush;
rep->p_ops.drain = obj_norep_drain;
rep->p_ops.memcpy = obj_norep_memcpy;
rep->p_ops.memmove = obj_norep_memmove;
rep->p_ops.memset = obj_norep_memset;
}
rep->p_ops.base = rep;
} else {
/* non-master replicas */
rep->is_master_replica = 0;
rep->p_ops.persist = NULL;
rep->p_ops.flush = NULL;
rep->p_ops.drain = NULL;
rep->p_ops.memcpy = NULL;
rep->p_ops.memmove = NULL;
rep->p_ops.memset = NULL;
rep->p_ops.base = NULL;
}
rep->is_dev_dax = set->replica[repidx]->part[0].is_dev_dax;
int ret;
ret = obj_replica_init_local(rep, repset->is_pmem,
set->resvsize);
if (ret)
return ret;
return 0;
}
/*
* obj_replica_fini -- (internal) deinitialize replica
*/
static void
obj_replica_fini(struct pool_replica *repset)
{
LOG(3, "repset %p", repset);
}
/*
* obj_runtime_init -- (internal) initialize runtime part of the pool header
*/
static int
obj_runtime_init(PMEMobjpool *pop, int rdonly, int boot, unsigned nlanes)
{
LOG(3, "pop %p rdonly %d boot %d", pop, rdonly, boot);
struct pmem_ops *p_ops = &pop->p_ops;
/* run_id is made unique by incrementing the previous value */
pop->run_id += 2;
if (pop->run_id == 0)
pop->run_id += 2;
pmemops_persist(p_ops, &pop->run_id, sizeof(pop->run_id));
/*
* Use some of the memory pool area for run-time info. This
* run-time state is never loaded from the file, it is always
* created here, so no need to worry about byte-order.
*/
pop->rdonly = rdonly;
pop->uuid_lo = pmemobj_get_uuid_lo(pop);
pop->lanes_desc.runtime_nlanes = nlanes;
pop->tx_params = tx_params_new();
if (pop->tx_params == NULL)
goto err_tx_params;
pop->stats = stats_new(pop);
if (pop->stats == NULL)
goto err_stat;
pop->user_data = NULL;
VALGRIND_REMOVE_PMEM_MAPPING(&pop->mutex_head,
sizeof(pop->mutex_head));
VALGRIND_REMOVE_PMEM_MAPPING(&pop->rwlock_head,
sizeof(pop->rwlock_head));
VALGRIND_REMOVE_PMEM_MAPPING(&pop->cond_head,
sizeof(pop->cond_head));
pop->mutex_head = NULL;
pop->rwlock_head = NULL;
pop->cond_head = NULL;
if (boot) {
if ((errno = obj_runtime_init_common(pop)) != 0)
goto err_boot;
#if VG_MEMCHECK_ENABLED
if (On_memcheck) {
/* mark unused part of the pool as not accessible */
void *end = palloc_heap_end(&pop->heap);
VALGRIND_DO_MAKE_MEM_NOACCESS(end,
(char *)pop + pop->set->poolsize - (char *)end);
}
#endif
obj_pool_init();
if ((errno = critnib_insert(pools_ht, pop->uuid_lo, pop))) {
ERR("!critnib_insert to pools_ht");
goto err_critnib_insert;
}
if ((errno = critnib_insert(pools_tree, (uint64_t)pop, pop))) {
ERR("!critnib_insert to pools_tree");
goto err_tree_insert;
}
}
if (obj_ctl_init_and_load(pop) != 0) {
errno = EINVAL;
goto err_ctl;
}
util_mutex_init(&pop->ulog_user_buffers.lock);
pop->ulog_user_buffers.map = ravl_new_sized(
operation_user_buffer_range_cmp,
sizeof(struct user_buffer_def));
if (pop->ulog_user_buffers.map == NULL) {
ERR("!ravl_new_sized");
goto err_user_buffers_map;
}
pop->ulog_user_buffers.verify = 0;
/*
* If possible, turn off all permissions on the pool header page.
*
* The prototype PMFS doesn't allow this when large pages are in
* use. It is not considered an error if this fails.
*/
RANGE_NONE(pop->addr, sizeof(struct pool_hdr), pop->is_dev_dax);
return 0;
err_user_buffers_map: