forked from Seagate/cortx-motr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
m0hsm_api.c
2877 lines (2402 loc) · 70.3 KB
/
m0hsm_api.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
/*
* Copyright (c) 2018-2020 Seagate Technology LLC and/or its Affiliates
* COPYRIGHT 2017-2018 CEA[1] and SAGE partners
*
* 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.
*
* For any questions about this software or licensing,
* please email opensource@seagate.com or cortx-questions@seagate.com.
*
* [1]Commissariat a l'energie atomique et aux energies alternatives
*
* Original author: Thomas Leibovici <thomas.leibovici@cea.fr>
*/
/* HSM invariants:
* - There is always a writable layer with the highest priority
* to protect object copies (source or target) to be modified while they are moved.
* - Applications always read the last written data from composite objects,
* whereever it extents are located.
*/
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/time.h>
#include <getopt.h>
#include <stdarg.h>
#include "lib/trace.h"
#include "conf/obj.h"
#include "fid/fid.h"
#include "motr/idx.h"
#include "motr/layout.h"
#include "m0hsm_api.h"
#define MAX_BLOCK_COUNT (200)
/** batch create + set_layout operation? */
/** How many extents we get in a batch from the extent index (arbitrary) */
#define HSM_EXTENT_SCAN_BATCH 8
/* global variables (set by m0hsm_init())*/
struct m0hsm_options options = {
.trace_level = LOG_INFO,
.op_timeout = 10,
.log_stream = NULL, /* default will be set by m0hsm_init() */
};
static struct m0_client *m0_instance;
static struct m0_realm *m0_uber_realm;
/* logging macros */
#define ERROR(_fmt, ...) if (options.trace_level >= LOG_ERROR) \
fprintf(options.log_stream, _fmt, ##__VA_ARGS__)
#define INFO(_fmt, ...) if (options.trace_level >= LOG_INFO) \
fprintf(options.log_stream, _fmt, ##__VA_ARGS__)
#define VERB(_fmt, ...) if (options.trace_level >= LOG_VERB) \
fprintf(options.log_stream, _fmt, ##__VA_ARGS__)
#define DBG(_fmt, ...) if (options.trace_level >= LOG_DEBUG) \
fprintf(options.log_stream, _fmt, ##__VA_ARGS__)
#define ENTRY DBG("> ENTERING %s()\n", __func__)
#define RETURN(_rc) do { DBG("< LEAVING %s() line %d, rc=%d\n", \
__func__, __LINE__, (_rc)); \
return (_rc); } while(0)
/** internal type to handle extents */
struct extent {
off_t off;
size_t len;
};
/** largest extent possible */
static const struct extent EXT_FULLRANGE = {
.off = 0,
.len = M0_BCOUNT_MAX,
};
enum {
MAX_LEN=128,
MAX_POOLS = 16,
};
struct param {
char name[MAX_LEN];
char value[MAX_LEN];
};
static struct param hsm_rc_params[128];
static struct m0_fid hsm_pools[MAX_POOLS] = {};
static int read_params(FILE *in, struct param *p, int max_params)
{
int ln, n=0;
char s[MAX_LEN];
for (ln=1; max_params > 0 && fgets(s, MAX_LEN, in); ln++) {
/* Value of MAX_LEN is hardcoded because inside
* double quotes we will not able to use macro MAX_LEN
*/
if (sscanf(s, " %128[#\n\r]", p->name))
continue; /* skip emty line or comment */
if (sscanf(s, " %128[a-z_A-Z0-9] = %128[^#\n\r]",
p->name, p->value) < 2) {
ERROR("m0hsm: %s: error at line %d: %s\n", __func__,
ln, s);
return -1;
}
DBG("%s: %d: name='%s' value='%s'\n", __func__,
ln, p->name, p->value);
p++, max_params--, n++;
}
RETURN(n);
}
static int hsm_pool_fid_set(struct param *p)
{
int i;
char pname[32];
for (i = 0; i < MAX_POOLS; i++) {
sprintf(pname, "M0_POOL_TIER%d", i + 1);
if (strcmp(p->name, pname) == 0) {
if (m0_fid_sscanf(p->value, hsm_pools + i) != 0) {
ERROR("%s: failed to parse FID of %s\n",
__func__, pname);
return -1;
}
return 1;
}
}
return 0;
}
static int hsm_pools_fids_set(struct param p[], int n)
{
int i, rc;
for (i = 0; n > 0; n--, p++, i += rc) {
rc = hsm_pool_fid_set(p);
DBG("%s: rc=%d\n", __func__, rc);
if (rc < 0)
return rc;
}
if (i < 1) {
ERROR("m0hsm: no pools configured\n");
return -1;
}
return 0;
}
int m0hsm_init(struct m0_client *instance, struct m0_realm *uber_realm,
const struct m0hsm_options *in_options)
{
int rc;
options.log_stream = stderr; /* set default */
/* set options */
if (in_options)
options = *in_options;
if (!instance || !uber_realm) {
ERROR("Missing instance or realm argument to %s()\n", __func__);
return -EINVAL;
}
m0_instance = instance;
m0_uber_realm = uber_realm;
if ((rc = read_params(options.rcfile, hsm_rc_params,
ARRAY_SIZE(hsm_rc_params))) < 0) {
ERROR("%s: failed to read parameters\n", __func__);
return -EINVAL;
}
DBG("%s: read %d params\n", __func__, rc);
if (hsm_pools_fids_set(hsm_rc_params, rc) < 0) {
ERROR("%s: failed to configure pools\n", __func__);
return -EINVAL;
}
return 0;
}
/** Special value meaning any tier. */
#define HSM_ANY_TIER UINT8_MAX
/* Keep high 32 bits reserved for motr. Use 2^31 for HSM */
#define HSM_ID_MASK_HI (1LL<<31)
static bool is_hsm_reserved(struct m0_uint128 id)
{
return (id.u_hi & HSM_ID_MASK_HI);
}
/* HSM priority is composed of:
* 24 bits for generation, 8 bits for tier priority
* Priority is in descending order (the smaller the value,
* the higher priority), so we must reverse generation
* numbering (from the higher value down to 0).
*/
/**
* Build layer priority from generation number and tier number
*/
static uint32_t hsm_prio(uint32_t generation, uint8_t tier_idx)
{
/* generation prio is the opposite of generation */
uint32_t gen_prio;
/* generation must fit on 24bits */
M0_ASSERT(generation <= 0x00FFFFFF);
gen_prio = 0x00FFFFFF - generation;
return (gen_prio << 8) | tier_idx;
}
/**
* Build subobject id from parent object id, generation number and tier number.
*/
static struct m0_uint128 hsm_subobj_id(struct m0_uint128 id, uint32_t gen,
uint8_t tier)
{
struct m0_uint128 newid = id;
newid.u_hi <<= 32;
newid.u_hi |= HSM_ID_MASK_HI;
newid.u_hi |= hsm_prio(gen, tier);
return newid;
}
/**
* Extract generation number from layer priority.
*/
static uint32_t hsm_prio2gen(uint32_t priority)
{
return 0x00FFFFFF - (priority >> 8);
}
/**
* Extract tier index from layer priority.
*/
static uint8_t hsm_prio2tier(uint32_t priority)
{
return priority & 0xFF;
}
/**
* Returns a pointer to a pool fid for the given tier index.
*/
static struct m0_fid *hsm_tier2pool(uint8_t tier_idx)
{
if (tier_idx < 1 || tier_idx > MAX_POOLS)
return NULL;
return hsm_pools + tier_idx - 1;
}
/** Helper to open an object entity */
static int open_entity(struct m0_entity *entity)
{
struct m0_op *ops[1] = {NULL};
int rc;
ENTRY;
m0_entity_open(entity, &ops[0]);
m0_op_launch(ops, 1);
rc = m0_op_wait(ops[0], M0_BITS(M0_OS_FAILED,
M0_OS_STABLE),
m0_time_from_now(options.op_timeout,0)) ?:
m0_rc(ops[0]);
m0_op_fini(ops[0]);
m0_op_free(ops[0]);
RETURN(rc);
}
/**
* Helper to create an object with the given id.
* @param tier_idx defines the object location
* (HSM_ANY_TIER to leave it unspecified)
*/
static int create_obj(struct m0_uint128 id, struct m0_obj *obj,
bool close_entity, uint8_t tier_idx)
{
struct m0_op *ops[1] = {NULL};
struct m0_fid *pool = NULL;
int rc;
ENTRY;
DBG("creating id=%" PRIx64 ":%" PRIx64 "\n", id.u_hi, id.u_lo);
/* first create the main object with a default layout */
m0_obj_init(obj, m0_uber_realm, &id, 9 /* XXX: 1MB */);
/* m0_client_layout_id(m0_instance)); */
rc = open_entity(&obj->ob_entity);
if (rc == 0) {
ERROR("Object %" PRIx64 ":%" PRIx64 " already exists\n", id.u_hi,
id.u_lo);
RETURN(-EEXIST);
} else if (rc != -ENOENT) {
ERROR("Failed to create object %" PRIx64 ":%" PRIx64 ": rc=%d\n",
id.u_hi, id.u_lo, rc);
RETURN(rc);
}
if (tier_idx != HSM_ANY_TIER) {
pool = hsm_tier2pool(tier_idx);
DBG("%s: got pool "FID_F"\n", __func__, FID_P(pool));
if (pool == NULL || !m0_fid_is_set(pool)) {
ERROR("m0hsm: pool index %d is not configured\n", tier_idx);
return -EINVAL;
}
}
m0_entity_create(pool, &obj->ob_entity, &ops[0]);
m0_op_launch(ops, ARRAY_SIZE(ops));
rc = m0_op_wait(
ops[0], M0_BITS(M0_OS_FAILED, M0_OS_STABLE),
m0_time_from_now(options.op_timeout,0));
m0_op_fini(ops[0]);
m0_op_free(ops[0]);
if (close_entity)
m0_entity_fini(&obj->ob_entity);
RETURN(rc);
}
/**
* Helper to delete an object.
* (actually unused because we batch delete of subobject
* + update of parent layout in a single operation).
*/
static int delete_obj(struct m0_uint128 id) __attribute__((unused));
static int delete_obj(struct m0_uint128 id)
{
struct m0_op *ops[1] = {NULL};
struct m0_obj obj;
int rc;
ENTRY;
memset(&obj, 0, sizeof(struct m0_obj));
DBG("deleting id=%" PRIx64 ":%" PRIx64 "\n", id.u_hi, id.u_lo);
m0_obj_init(&obj, m0_uber_realm, &id, m0_client_layout_id(m0_instance));
m0_entity_delete(&obj.ob_entity, &ops[0]);
m0_op_launch(ops, ARRAY_SIZE(ops));
rc = m0_op_wait(
ops[0], M0_BITS(M0_OS_FAILED, M0_OS_STABLE),
m0_time_from_now(options.op_timeout,0));
m0_op_fini(ops[0]);
m0_op_free(ops[0]);
m0_entity_fini(&obj.ob_entity);
RETURN(rc);
}
/**
* Create an object with the given layout.
*/
static int create_obj_with_layout(struct m0_uint128 id,
struct m0_obj *obj,
struct m0_client_layout *layout,
bool close_entity)
{
struct m0_op *ops[2] = {NULL};
int rc;
ENTRY;
DBG("creating id=%" PRIx64 ":%" PRIx64 "\n", id.u_hi, id.u_lo);
/* first create the main object with a default layout */
m0_obj_init(obj, m0_uber_realm, &id, m0_client_layout_id(m0_instance));
/* set first operation of batch */
m0_entity_create(NULL, &obj->ob_entity, &ops[0]);
/* set second operation of batch */
rc = m0_client_layout_op(obj, M0_EO_LAYOUT_SET, layout, &ops[1]);
if (rc) {
m0_op_free(ops[0]);
RETURN(rc);
}
/* launch them both */
m0_op_launch(ops, ARRAY_SIZE(ops));
/* wait for first to complete */
rc = m0_op_wait(
ops[0], M0_BITS(M0_OS_FAILED, M0_OS_STABLE),
m0_time_from_now(options.op_timeout,0)) ?: m0_rc(ops[0]);
m0_op_fini(ops[0]);
m0_op_free(ops[0]);
/* wait for second to complete */
rc = rc ?: m0_op_wait(
ops[1], M0_BITS(M0_OS_FAILED, M0_OS_STABLE),
m0_time_from_now(options.op_timeout,0)) ?: m0_rc(ops[1]);
m0_op_fini(ops[1]);
m0_op_free(ops[1]);
if (close_entity)
m0_entity_fini(&obj->ob_entity);
RETURN(rc);
}
/** Combine a subobject deletion + setting parent layout */
static int delete_obj_set_parent_layout(struct m0_uint128 id,
struct m0_uint128 parent_id,
struct m0_client_layout *parent_layout)
{
struct m0_op *ops[2] = {NULL};
struct m0_obj parent_obj;
struct m0_obj obj;
int rc;
ENTRY;
DBG("deleting id=%" PRIx64 ":%" PRIx64 "\n", id.u_hi, id.u_lo);
memset(&obj, 0, sizeof(struct m0_obj));
memset(&parent_obj, 0, sizeof(struct m0_obj));
m0_obj_init(&obj, m0_uber_realm, &id, m0_client_layout_id(m0_instance));
m0_obj_init(&parent_obj, m0_uber_realm, &parent_id,
m0_client_layout_id(m0_instance));
/* open the entities */
rc = open_entity(&obj.ob_entity);
if (rc)
RETURN(rc);
rc = open_entity(&parent_obj.ob_entity);
if (rc)
RETURN(rc);
/* set first operation of batch */
rc = m0_entity_delete(&obj.ob_entity, &ops[0]);
if (rc)
RETURN(rc);
/* set second operation of batch */
rc = m0_client_layout_op(&parent_obj, M0_EO_LAYOUT_SET,
parent_layout, &ops[1]);
if (rc) {
m0_op_free(ops[0]);
RETURN(rc);
}
/* launch them both */
m0_op_launch(ops, ARRAY_SIZE(ops));
/* wait for first op to complete */
rc = m0_op_wait(
ops[0], M0_BITS(M0_OS_FAILED, M0_OS_STABLE),
m0_time_from_now(options.op_timeout,0)) ?: m0_rc(ops[0]);
m0_op_fini(ops[0]);
m0_op_free(ops[0]);
/* wait for second op to complete */
rc = rc ?: m0_op_wait(
ops[1], M0_BITS(M0_OS_FAILED, M0_OS_STABLE),
m0_time_from_now(options.op_timeout,0)) ?: m0_rc(ops[1]);
m0_op_fini(ops[1]);
m0_op_free(ops[1]);
/* close entities */
m0_entity_fini(&obj.ob_entity);
m0_entity_fini(&parent_obj.ob_entity);
RETURN(rc);
}
/**
* Get the layout of an object
*/
static int obj_layout_get(struct m0_obj *obj,
struct m0_client_layout **layout)
{
struct m0_op *ops[1] = {NULL};
int rc;
ENTRY;
*layout = m0_client_layout_alloc(M0_LT_COMPOSITE);
if (*layout == NULL)
RETURN(-ENOMEM);
m0_client_layout_op(obj, M0_EO_LAYOUT_GET, *layout, &ops[0]);
m0_op_launch(ops, 1);
rc = m0_op_wait(
ops[0], M0_BITS(M0_OS_FAILED, M0_OS_STABLE),
m0_time_from_now(options.op_timeout,0));
m0_op_fini(ops[0]);
m0_op_free(ops[0]);
RETURN(rc);
}
/**
* Get the layout of an object designated by its fid.
*/
static int layout_get(struct m0_uint128 id, struct m0_client_layout **layout)
{
struct m0_obj obj;
int rc;
ENTRY;
/* instanciate the object to be handled */
M0_SET0(&obj);
m0_obj_init(&obj, m0_uber_realm, &id, m0_client_layout_id(m0_instance));
rc = open_entity(&obj.ob_entity);
if (rc)
RETURN(rc);
rc = obj_layout_get(&obj, layout);
/* close it */
m0_entity_fini(&obj.ob_entity);
RETURN(rc);
}
/** Set the layout of an object */
static int obj_layout_set(struct m0_obj *obj,
struct m0_client_layout *layout)
{
struct m0_op *ops[1] = {NULL};
int rc;
ENTRY;
rc = m0_client_layout_op(obj, M0_EO_LAYOUT_SET, layout, &ops[0]);
if (rc)
RETURN(rc);
m0_op_launch(ops, 1);
rc = m0_op_wait(
ops[0], M0_BITS(M0_OS_FAILED, M0_OS_STABLE),
m0_time_from_now(options.op_timeout,0));
m0_op_fini(ops[0]);
m0_op_free(ops[0]);
RETURN(rc);
}
/** Set the layout of an object designated by its fid */
static int layout_set(struct m0_uint128 id, struct m0_client_layout *layout)
{
struct m0_obj obj;
int rc;
ENTRY;
/* open the object */
memset(&obj, 0, sizeof(obj));
m0_obj_init(&obj, m0_uber_realm, &id, m0_client_layout_id(m0_instance));
rc = open_entity(&obj.ob_entity);
if (rc)
RETURN(rc);
/* update its layout */
rc = obj_layout_set(&obj, layout);
/* close it */
m0_entity_fini(&obj.ob_entity);
RETURN(rc);
}
/* The definitions above allow iterating on layers and extent lists */
#include "motr/magic.h"
M0_TL_DESCR_DEFINE(clayer, "composite layout layers",
static, struct m0_composite_layer,
ccr_tlink, ccr_tlink_magic,
M0_CLAYER_TL_MAGIC, M0_CLAYER_TL_MAGIC);
M0_TL_DEFINE(clayer, static, struct m0_composite_layer);
M0_TL_DESCR_DEFINE(cext, "composite layout extents",
static, struct m0_composite_extent,
ce_tlink, ce_tlink_magic,
M0_CEXT_TL_MAGIC, M0_CEXT_TL_MAGIC);
M0_TL_DEFINE(cext, static, struct m0_composite_extent);
/** Wrap a single "get next" operation on the extent index
* (read a batch of extents).
*/
static int get_next_extents(struct m0_idx *idx,
struct m0_bufvec *keys,
struct m0_bufvec *vals,
int *rc_list, int32_t flags)
{
int rc;
struct m0_op *ops[1] = {NULL};
ENTRY;
m0_idx_op(idx, M0_IC_NEXT,
keys, vals, rc_list, flags, &ops[0]);
m0_op_launch(ops, 1);
rc = m0_op_wait(ops[0],
M0_BITS(M0_OS_FAILED,
M0_OS_STABLE),
m0_time_from_now(options.op_timeout,0));
rc = rc ? rc : ops[0]->op_sm.sm_rc;
/* fini and release */
m0_op_fini(ops[0]);
m0_op_free(ops[0]);
RETURN(rc);
}
/**
* Read returned keys and add them to the extent list
* @return The number of read extents, or a negative error code.
*/
static int read_extent_keys(struct m0_uint128 subobjid,
struct m0_bufvec *keys,
struct m0_bufvec *vals,
int *rc_list,
struct m0_tl *ext_list,
struct m0_composite_layer_idx_key *last_key)
{
struct m0_composite_layer_idx_key key;
struct m0_composite_layer_idx_val val;
struct m0_composite_extent *ext;
int i;
ENTRY;
for (i = 0; i < keys->ov_vec.v_nr; i++) {
/* Reach the end of index. */
if (keys->ov_buf[i] == NULL ||
vals->ov_buf[i] == NULL || rc_list[i] != 0)
break;
/* Have retrieved all kv pairs for a layer. */
m0_composite_layer_idx_key_from_buf(
&key, keys->ov_buf[i]);
if (!m0_uint128_eq(&key.cek_layer_id, &subobjid))
break;
m0_composite_layer_idx_val_from_buf(
&val, vals->ov_buf[i]);
/* Add a new extent. */
M0_ALLOC_PTR(ext);
if (ext == NULL)
RETURN(-ENOMEM);
ext->ce_id = key.cek_layer_id;
ext->ce_off = key.cek_off;
ext->ce_len = val.cev_len;
DBG("%s: extent %#" PRIx64 ":%#"PRIx64
" [%#" PRIx64 "-%#" PRIx64 "]\n", __func__,
ext->ce_id.u_hi, ext->ce_id.u_lo, key.cek_off,
key.cek_off + ext->ce_len - 1);
/* The extents are in increasing order of offset. */
cext_tlink_init_at_tail(ext, ext_list);
}
ext = cext_tlist_tail(ext_list);
if (ext != NULL) {
last_key->cek_layer_id = ext->ce_id;
last_key->cek_off = ext->ce_off;
}
RETURN(i);
}
/** Free all extents in an extent list */
static void extent_list_free(struct m0_tl *ext_list)
{
struct m0_composite_extent *ext;
m0_tl_teardown(cext, ext_list, ext)
m0_free(ext);
}
/** Reset a list of buffers */
static void reset_bufvec(struct m0_bufvec *keys, int count)
{
int i;
for (i = 0; i < count; i++) {
m0_free(keys->ov_buf[i]);
keys->ov_buf[i] = NULL;
keys->ov_vec.v_count[i] = 0;
}
}
/**
* Helper to load a list of extents for a given layer.
* @param subobjid Layer id
* @param write Whether to load the read or write extents
* (true for write).
* @param ext_list List of extents to be populated.
*/
static int layer_load_extent_list(struct m0_uint128 subobjid, bool write,
struct m0_tl *ext_list)
{
struct m0_idx idx = {{0}};
struct m0_composite_layer_idx_key curr_key;
struct m0_bufvec keys;
struct m0_bufvec vals;
int *rc_list = NULL;
int32_t flags = 0;
int rc, nb;
ENTRY;
if (!cext_tlist_is_empty(ext_list))
/* already loaded */
RETURN(0);
/* Allocate argument parameters */
rc = m0_bufvec_empty_alloc(&keys, HSM_EXTENT_SCAN_BATCH);
if (rc)
return rc;
rc = m0_bufvec_empty_alloc(&vals, HSM_EXTENT_SCAN_BATCH);
if (rc)
goto out_free;
M0_ALLOC_ARR(rc_list, HSM_EXTENT_SCAN_BATCH);
if (rc_list == NULL) {
rc = -ENOMEM;
goto out_free;
}
curr_key.cek_layer_id = subobjid;
curr_key.cek_off = 0;
m0_composite_layer_idx(subobjid, write, &idx);
while (true) {
/* convert current key to idx buffer */
rc = m0_composite_layer_idx_key_to_buf(
&curr_key, &keys.ov_buf[0], &keys.ov_vec.v_count[0]);
if (rc)
goto out_free;
rc = get_next_extents(&idx, &keys, &vals, rc_list, flags);
if (rc)
goto out_free;
nb = read_extent_keys(subobjid, &keys, &vals, rc_list, ext_list,
&curr_key);
if (nb < HSM_EXTENT_SCAN_BATCH)
break;
/* Reset keys and vals. */
reset_bufvec(&keys, HSM_EXTENT_SCAN_BATCH);
reset_bufvec(&vals, HSM_EXTENT_SCAN_BATCH);
flags = M0_OIF_EXCLUDE_START_KEY;
}
out_free:
m0_idx_fini(&idx);
m0_bufvec_free(&keys);
m0_bufvec_free(&vals);
m0_free0(&rc_list);
RETURN(rc);
}
static void print_extents(FILE *stream, const struct m0_tl *ext_list,
bool details)
{
struct m0_composite_extent *ext;
bool is_first = true;
m0_tl_for(cext, ext_list, ext) {
if (details)
fprintf(stream, "%s<%#" PRIx64 ":%#" PRIx64 ">:"
"[%#" PRIx64 "->%#" PRIx64 "]",
is_first ? "" : " ",
ext->ce_id.u_hi, ext->ce_id.u_lo,
ext->ce_off, ext->ce_off + ext->ce_len - 1);
else
fprintf(stream, "%s[%#" PRIx64 "->%#" PRIx64 "]",
is_first ? "" : " ",
ext->ce_off, ext->ce_off + ext->ce_len - 1);
is_first = false;
} m0_tl_endfor;
if (details)
fprintf(stream, "\n");
}
static void print_layer(FILE *stream, struct m0_composite_layer *layer,
bool details)
{
if (details) {
fprintf(stream, "subobj=<%#" PRIx64 ":%#" PRIx64 ">\n",
layer->ccr_subobj.u_hi, layer->ccr_subobj.u_lo);
fprintf(stream, "lid=%" PRIu64 "\n", layer->ccr_lid);
fprintf(stream, "priority=%#x (gen=%u, tier=%hhu)\n",
layer->ccr_priority,
hsm_prio2gen(layer->ccr_priority),
hsm_prio2tier(layer->ccr_priority));
} else {
fprintf(stream, "gen %u, tier %hhu, extents: ",
hsm_prio2gen(layer->ccr_priority),
hsm_prio2tier(layer->ccr_priority));
}
/* load read extents for this layer */
layer_load_extent_list(layer->ccr_subobj, false, &layer->ccr_rd_exts);
/* load write extents for this layer */
layer_load_extent_list(layer->ccr_subobj, true,
&layer->ccr_wr_exts);
if (details)
fprintf(stream, "R extents:\n");
print_extents(stream, &layer->ccr_rd_exts, details);
if (details) {
fprintf(stream, "W extents:\n");
print_extents(stream, &layer->ccr_wr_exts, details);
} else {
if (!cext_tlist_is_empty(&layer->ccr_wr_exts))
fprintf(stream, " (writable)\n");
else
fprintf(stream, "\n");
}
}
static void print_layout(FILE *stream, const struct m0_client_layout *layout,
bool details)
{
struct m0_client_composite_layout *clayout;
struct m0_composite_layer *layer;
int i;
clayout = M0_AMB(clayout, layout, ccl_layout);
M0_ASSERT(clayout != NULL);
if (details)
fprintf(stream, "%" PRIu64 " layers:\n", clayout->ccl_nr_layers);
/* iterate on all layers and display their extents */
i = 0;
m0_tl_for(clayer, &clayout->ccl_layers, layer) {
if (details)
fprintf(stream, "==== layer #%d ====\n", i);
else
fprintf(stream, " - ");
print_layer(stream, layer, details);
i++;
} m0_tl_endfor;
}
int m0hsm_dump(FILE *stream, struct m0_uint128 id, bool details)
{
struct m0_client_layout *layout = NULL;
int rc;
ENTRY;
if (is_hsm_reserved(id))
RETURN(-EINVAL);
rc = layout_get(id, &layout);
if (rc)
RETURN(rc);
M0_ASSERT(layout != NULL);
print_layout(stream, layout, details);
RETURN(0);
}
/**
* Add an extent to a layer.
* @param subobjid Layer id.
* @param ext Extent to be added.
* @param write Whether to set a read or write extents
* (true for write).
* @param overwrite Whether to overwrite a previous extent
* starting at the same offset.
*/
static int layer_extent_add(struct m0_uint128 subobjid,
const struct extent *ext,
bool write, bool overwrite)
{
struct m0_bufvec keys;
struct m0_bufvec vals;
struct m0_composite_layer_idx_key key;
struct m0_composite_layer_idx_val val;
int *rcs = NULL;
struct m0_op *ops[1] = {NULL};
struct m0_idx idx;
int rc;
ENTRY;
rc = m0_bufvec_empty_alloc(&keys, 1);
if (rc)
RETURN(rc);
rc = m0_bufvec_empty_alloc(&vals, 1);
if (rc)
goto free_keys;
/* Set key and value. */
key.cek_layer_id = subobjid;
key.cek_off = ext->off;
val.cev_len = ext->len;
rc = m0_composite_layer_idx_key_to_buf(
&key, &keys.ov_buf[0], &keys.ov_vec.v_count[0]);
if (rc)
goto free_vals;
rc = m0_composite_layer_idx_val_to_buf(
&val, &vals.ov_buf[0], &vals.ov_vec.v_count[0]);
if (rc)
return rc;
/* now set the key/value */
memset(&idx, 0, sizeof idx);
M0_ALLOC_ARR(rcs, 1);
if (rcs == NULL) {
rc = -ENOMEM;
goto out_free;
}
DBG("%s %s extent for <%" PRIx64 ":%" PRIx64 ">: "
"[%#" PRIx64 "-%#" PRIx64 "]\n",
overwrite ? "Changing" : "Adding",
write ? "write" : "read",
subobjid.u_hi, subobjid.u_lo, ext->off,
ext->off + ext->len - 1);
ops[0] = NULL;
m0_composite_layer_idx(subobjid, write, &idx);
m0_idx_op(&idx, M0_IC_PUT, &keys, &vals, rcs,
overwrite ? M0_OIF_OVERWRITE : 0, &ops[0]);
m0_op_launch(ops, 1);
rc = m0_op_wait(ops[0],
M0_BITS(M0_OS_FAILED, M0_OS_STABLE),
m0_time_from_now(options.op_timeout,0));
if (rc)
goto out_free;
rc = ops[0]->op_sm.sm_rc;
/* fini and release */
m0_op_fini(ops[0]);
m0_op_free(ops[0]);
m0_entity_fini(&idx.in_entity);
out_free:
m0_free0(&rcs);
free_vals:
m0_bufvec_free(&vals);
free_keys:
m0_bufvec_free(&keys);
RETURN(rc);
}
static int layer_extent_del(struct m0_uint128 subobjid, off_t off, bool write)
{
struct m0_bufvec keys;
struct m0_composite_layer_idx_key key;
int *rcs = NULL;
struct m0_op *ops[1] = {NULL};
struct m0_idx idx;
int rc;
ENTRY;
rc = m0_bufvec_empty_alloc(&keys, 1);
if (rc)
RETURN(rc);
/* Set key and value. */
key.cek_layer_id = subobjid;
key.cek_off = off;
rc = m0_composite_layer_idx_key_to_buf(