-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource.cpp
1961 lines (1571 loc) · 66.2 KB
/
resource.cpp
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
/*
Advanced Computing Center for Research and Education Proprietary License
Version 1.0 (April 2006)
Copyright (c) 2006, Advanced Computing Center for Research and Education,
Vanderbilt University, All rights reserved.
This Work is the sole and exclusive property of the Advanced Computing Center
for Research and Education department at Vanderbilt University. No right to
disclose or otherwise disseminate any of the information contained herein is
granted by virtue of your possession of this software except in accordance with
the terms and conditions of a separate License Agreement entered into with
Vanderbilt University.
THE AUTHOR OR COPYRIGHT HOLDERS PROVIDES THE "WORK" ON AN "AS IS" BASIS,
WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, TITLE, FITNESS FOR A PARTICULAR
PURPOSE, AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Vanderbilt University
Advanced Computing Center for Research and Education
230 Appleton Place
Nashville, TN 37203
http://www.accre.vanderbilt.edu
*/
//***************************************************************************
//***************************************************************************
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <glib.h>
#include "resource.h"
#include "log.h"
#include "debug.h"
#include "fmttypes.h"
#define _RESOURCE_BUF_SIZE 1048576
char *_blanks[_RESOURCE_BUF_SIZE];
#define _RESOURCE_STATE_GOOD 0
#define _RESOURCE_STATE_BAD 1
typedef struct { //*** Used to store usage info on a resource to keep from having to rescan the DB
int version;
int state;
Rsize_t used_space[2];
Rsize_t n_allocs;
Rsize_t n_alias;
} Usage_file_t;
typedef struct { //** Internal resource iterator
int mode;
DB_iterator_t *dbi;
void *fsi;
Resource_t *r;
Allocation_t a;
} res_iterator_t;
void *resource_cleanup_thread(void *data);
int _remove_allocation_for_make_free(Resource_t *r, Allocation_t *alloc, DB_iterator_t *it);
//***************************************************************************
// rid2str - Converts the RID to a printable format
//***************************************************************************
char *rid2str(RID_t *rid, char *name, int n_size)
{
name[0] = '\0';
snprintf(name, n_size, "%d", rid->id);
return(name);
// return((char *)inet_ntop(AF_INET6, (void *)&(rid->rid), name, n_size));
}
//***************************************************************************
// str2rid - Converts the string form of the RID to the network version
// Could be a single integer. Is so we add "::" to it
//***************************************************************************
int str2rid(const char *name, RID_t *rid)
{
int err;
err = sscanf(name, "%d", &(rid->id));
//*********************
// err = inet_pton(AF_INET6, name, (void *)&(rid->rid));
// if (err <= 0) { //** Single number so right-shift the numbers by adding ::
// char token[256];
// snprintf(token, sizeof(token), "::%s", name);
// token[sizeof(token)-1] = '\0';
// err = inet_pton(AF_INET6, token, (void *)&(rid->rid));
// }
//*********************
if (err == 1) {
return(0);
} else {
return(1);
}
}
//***************************************************************************
// empty_rid - Creates a blank RID
//***************************************************************************
void empty_rid(RID_t *rid)
{
rid->id = 0;
// memset(&(rid->rid), 0, sizeof(rid->rid));
}
//***************************************************************************
// is_empty_rid - Determines if an RID is blank
//***************************************************************************
int is_empty_rid(RID_t *rid)
{
return(rid->id == 0);
// RID_t blank;
// memset(&(blank.rid), 0, sizeof(blank.rid));
// return(memcmp(&(blank.rid), &(rid->rid), sizeof(blank.rid)) == 0);
}
//***************************************************************************
// compare_rid - Similar to strcmp but for RID's
//***************************************************************************
int compare_rid(RID_t *rid1, RID_t *rid2)
{
int i;
if (rid1->id > rid2->id) {
i = 1;
} else if (rid1->id == rid2->id) {
i = 0;
} else {
i = -1;
}
return(i);
}
//***************************************************************************
// write_usage_file - Writes the usage file
//***************************************************************************
int write_usage_file(char *fname, Resource_t *r, int state)
{
FILE *fd;
Usage_file_t usage;
log_printf(10, "write_usage_file: fname=%s\n", fname);
assert((fd = fopen(fname, "w")) != NULL);
usage.version = _RESOURCE_VERSION;
usage.used_space[0] = r->used_space[0];
usage.used_space[1] = r->used_space[1];
usage.n_allocs = r->n_allocs;
usage.n_alias = r->n_alias;
usage.state = state;
fwrite((void *)&usage, sizeof(usage), 1, fd);
fclose(fd);
Rsize_t mb;
log_printf(10, "write_usage_file: rid=%s\n",r->name);
mb = r->used_space[ALLOC_SOFT]/1024/1024; log_printf(10, "\n#soft_used = " LU " mb\n", mb);
mb = r->used_space[ALLOC_HARD]/1024/1024; log_printf(10, "#hard_used = " LU " mb\n", mb);
mb = r->used_space[ALLOC_SOFT]; log_printf(10, "#soft_used = " LU " b\n", mb);
mb = r->used_space[ALLOC_HARD]; log_printf(10, "#hard_used = " LU " b\n", mb);
log_printf(10, "#n_allocations = " LU "\n", r->n_allocs);
log_printf(10, "#n_alias = " LU "\n", r->n_alias);
return(0);
}
//***************************************************************************
// read_usage_file - reads the usage file
//***************************************************************************
int read_usage_file(char *fname, Resource_t *r)
{
FILE *fd;
Usage_file_t usage;
int n;
int docalc = 1;
log_printf(10, "read_usage_file: fname=%s\n", fname);
fd = fopen(fname, "r");
if (fd == NULL) {
log_printf(10, "read_usage_file: File not round %s\n", fname);
return(docalc);
}
n = fread((void *)&usage, sizeof(usage), 1, fd);
fclose(fd);
if (n != 1) {
log_printf(10, "read_usage_file: Can't read whole record! file: %s\n", fname);
return(docalc);
}
if (usage.version == _RESOURCE_VERSION) {
if (usage.state == _RESOURCE_STATE_GOOD) {
docalc = 0;
r->used_space[0] = usage.used_space[0];
r->used_space[1] = usage.used_space[1];
r->n_allocs = usage.n_allocs;
r->n_alias = usage.n_alias;
Rsize_t mb;
log_printf(10, "read_usage_file: rid=%s\n",r->name);
mb = r->used_space[ALLOC_SOFT]/1024/1024; log_printf(10, "\n#soft_used = " LU "\n", mb);
mb = r->used_space[ALLOC_HARD]/1024/1024; log_printf(10, "#hard_used = " LU "\n", mb);
log_printf(10, "#n_allocations = " LU "\n", r->n_allocs);
log_printf(10, "#n_alias = " LU "\n", r->n_alias);
}
}
//docalc=1;
return(docalc);
}
//***************************************************************************
// print_resource_usage - Prints the usage stats to the fd
//***************************************************************************
int print_resource_usage(Resource_t *r, FILE *fd)
{
fprintf(fd, "n_allocs = " LU "\n", r->n_allocs);
fprintf(fd, "n_alias = " LU "\n", r->n_alias);
fprintf(fd, "hard_usage = " LU "\n", r->used_space[ALLOC_HARD]);
fprintf(fd, "soft_usage = " LU "\n", r->used_space[ALLOC_SOFT]);
print_db(&(r->db), fd);
return(0);
}
//***************************************************************************
// mkfs_resource - Creates a new resource
//***************************************************************************
int mkfs_resource(RID_t rid, char *dev_type, char *device_name, char *db_location)
{
FILE *fd;
int err;
char rname[256];
char dname[2048];
char fname[2048];
DIR *dir;
Resource_t res;
char kgroup[100], name[100];
struct statfs stat;
if (strlen(db_location) > 1900) {
printf("mkfs_resource: Can't make fname. location and device too long\n");
printf("mkfs_resource: DB location: %s\n", db_location);
printf("mkfs_resource: device: %s\n", device_name);
abort();
}
//*** Fill in defaults for everything ***
snprintf(kgroup, sizeof(kgroup), "resource %s", rid2str(&rid, rname, 256)); res.keygroup = kgroup;
res.name = rid2str(&rid, name, sizeof(name));
res.rid = rid;
res.max_duration = 31536000; //default to 1 year
res.device_type = dev_type;
res.device = device_name;
res.location = db_location;
res.preallocate = 0;
res.minfree = 100*1024*1024; //Default to 100MB free
res.update_alloc = 1;
res.enable_read_history = 1;
res.enable_write_history = 1;
res.enable_manage_history = 1;
res.enable_alias_history = 1;
//**Make the directory for the DB if needed
snprintf(dname, sizeof(dname), "%s", db_location);
res.location = strdup(dname);
mkdir(dname, S_IRWXU);
assert((dir = opendir(dname)) != NULL); //Make sure I can open it
closedir(dir);
//**Open the Keyfile for storing resource data into
snprintf(fname, sizeof(fname), "%s/config", dname);
assert((fd = fopen(fname, "w")) != NULL);
//**Create the DB
assert(mkfs_db(dname, fd) == 0);
//**Create the device
if (strcmp("dir", dev_type)==0) {
DIR *dir;
assert((dir = opendir(device_name)) != NULL);
closedir(dir);
::statfs(device_name, &stat);
} else {
res.fs = new osd_ebofs(device_name, 1);
res.fs->statfs(&stat);
}
res.max_size[ALLOC_HARD] = stat.f_bavail*stat.f_bsize;
res.max_size[ALLOC_SOFT] = stat.f_bavail*stat.f_bsize;
res.max_size[ALLOC_TOTAL] = stat.f_bavail*stat.f_bsize;
res.used_space[ALLOC_HARD] = 0; res.used_space[ALLOC_SOFT] = 0;
res.n_allocs = 0; res.n_alias = 0;
err = create_history_table(&res);
if (err != 0) {
printf("mkfs_resource: Can't create the history table. err=%d\n", err);
abort();
}
//*** Print everything out to the screen for the user to use ***
print_resource(&res, stdout);
fclose(fd); //Close the ini file
return(0);
}
//***************************************************************************
// rebuild_remove_iter - Removes the current record
//***************************************************************************
int rebuild_remove_iter(res_iterator_t *ri)
{
int err = 0;
if (ri->mode == 1) {
err = remove_alloc_iter_db(ri->dbi);
}
if (err == 0) {
err = ri->r->fs->remove(ri->a.id);
//log_printf(10, "rebuild_put_iter: id=" LU " is_alias=%d\n", ri->a.id, ri->a.is_alias);
// if (ri->a.is_alias == 0) err = ri->r->fs->remove(ri->a.id);
}
return(err);
}
//***************************************************************************
// rebuild_modify_iter - Modifies the current record
// IF the mode == 2 then the rebuild app buffers the allocations and writes
// them in bulk using rebuild_put_alloc. In this case I do nothing
//***************************************************************************
int rebuild_modify_iter(res_iterator_t *ri, Allocation_t *a)
{
int err = 0;
if (ri->mode == 1) {
err = modify_alloc_iter_db(ri->dbi, a);
}
return(err);
}
//***************************************************************************
// rebuild_put_iter - Stores the current record
// If mode == 1 then nothing needs to be done. Otherwise if mode == 2/3 then
// I need to 1st set the a.size to the size of the file minus the header
// before storing it in the DB
//***************************************************************************
int rebuild_put_iter(res_iterator_t *ri, Allocation_t *a)
{
int err = 0;
if (ri->mode != 1) {
a->size = ri->r->fs->size(a->id) - ALLOC_HEADER;
log_printf(10, "rebuild_put_iter: id=" LU " size=" LU "\n", a->id, a->size);
err = _put_alloc_db(&(ri->r->db), a);
}
return(err);
}
//***************************************************************************
// rebuild_begin - Creates the rebuild iterator
//***************************************************************************
res_iterator_t *rebuild_begin(Resource_t *r, int wipe_clean)
{
res_iterator_t *ri = (res_iterator_t *)malloc(sizeof(res_iterator_t));
assert(ri != NULL);
dbr_lock(&(r->db));
ri->mode = wipe_clean;
ri->fsi = NULL;
ri->dbi = NULL;
ri->r = r;
if (wipe_clean == 1) {
ri->dbi = id_iterator(&(r->db));
assert(ri->dbi != NULL);
} else {
ri->fsi = r->fs->new_iterator();
assert(ri->fsi != NULL);
}
return(ri);
}
//***************************************************************************
// rebuild_end - Destroys the rebuild iterator
//***************************************************************************
void rebuild_end(res_iterator_t *ri)
{
if (ri->mode == 1) {
db_iterator_end(ri->dbi);
} else {
ri->r->fs->destroy_iterator(ri->fsi);
}
dbr_unlock(&(ri->r->db));
free(ri);
}
//***************************************************************************
// rebuild_get_next - Retreives the next record for rebuilding
//***************************************************************************
int rebuild_get_next(res_iterator_t *ri, Allocation_t *a)
{
int err;
osd_id_t id;
if (ri->mode != 1) {
err = ri->r->fs->iterator_next(ri->fsi, &id);
while (err == 0) {
// log_printf(15, "rebuild_get_next: r=%s id=" LU " err=%d\n", ri->r->name, id, err);
err = ri->r->fs->read(id, 0, sizeof(Allocation_t), a);
// log_printf(15, "rebuild_get_next: r=%s id=" LU " read err=%d sizeof(a)=%d\n", ri->r->name, id, err, sizeof(Allocation_t));
if (err == 0) { //** Nothing there so delete the filename
log_printf(0, "rebuild_get_next: rid=%s Empty allocation id=" LU ". Removing it....\n", ri->r->name, id);
flush_log();
err = ri->r->fs->remove(id);
err = ri->r->fs->iterator_next(ri->fsi, &id);
} else if (err != sizeof(Allocation_t)) {
log_printf(0, "rebuild_get_next: rid=%s Can't read id=" LU ". Skipping...nbytes=%d\n", ri->r->name, id, err);
flush_log();
err = ri->r->fs->iterator_next(ri->fsi, &id);
} else if (id != a->id) { //** ID mismatch.. throw warning and skip
log_printf(0, "rebuild_get_next: rid=%s ID mismatch so skipping!!!! fs entry id=" LU ". a.id=" LU "\n", ri->r->name, id,a->id);
flush_log();
err = ri->r->fs->iterator_next(ri->fsi, &id);
}
}
} else {
err = db_iterator_next(ri->dbi, DB_NEXT, a);
// log_printf(15, "rebuild_get_next: DB r=%s id=" LU " err=%d\n", ri->r->name, a->id, err);
}
ri->a = *a; //** Keep my copy for mods
if (err == sizeof(Allocation_t)) err = 0;
return(err);
}
//***************************************************************************
// rebuild_resource - Rebuilds the resource
//
// if wipe_clean=1 the ID database is not wiped. Instead it is iterated
// through to create the secondary indices and also verify the allocation
// exists on the resource if the size > 0. This method preserves
// any blank or unused allocations unlike the next method.
// If wipe_clean=2 the resource is walked to generate the new DB. This is
// significantly slower than wipe_clean=1 for a full depot.
// if wipe_clean=3 the resource is walked to generate the new DB and all
// allocations duration are extended to the max. Even for expired allocations.
//
// --NOTE: Any blank allocations will be lost!!! --
//***************************************************************************
int rebuild_resource(Resource_t *r, DB_env_t *env, int remove_expired, int wipe_clean, int truncate_expiration)
{
char fname[2048];
int i, nbuff, cnt, ecnt, pcnt, err, estate;
res_iterator_t *iter;
Allocation_t *a;
const int a_size = 1024;
Allocation_t alist[a_size];
time_t t, max_expiration, t1, t2;
osd_id_t id;
char print_time[128];
t = time(NULL);
ctime_r(&t, print_time); print_time[strlen(print_time)-1] = '\0';
log_printf(0, "rebuild_resource(rid=%s): Rebuilding Resource rid=%s. Starting at %s remove_expired=%d wipe_clean=%d truncate_expiration=%d\n",
r->name, r->name, print_time, remove_expired, wipe_clean, truncate_expiration);
if (wipe_clean == 1) {
log_printf(0, "rebuild_resource(rid=%s): wipe_clean == 1 so exiting\n", r->name);
return(0);
}
//** Mount it
GError *error=NULL;
GKeyFile *kfd;
GKeyFileFlags flags;
flags = G_KEY_FILE_NONE;
//* Load the GKeyFile from keyfile.conf or return.
assert((kfd = g_key_file_new()) != NULL);
snprintf(fname, 2048, "%s/config", r->location);
if (!g_key_file_load_from_file (kfd, fname, flags, &error)) {
g_error (error->message);
abort();
}
i = wipe_clean;
if (wipe_clean == 3) i = 2;
mount_db_generic(kfd, env, &(r->db), i); //**Mount the DBes
g_key_file_free(kfd);
//*** Now we have to fill it ***
r->used_space[0] = 0; r->used_space[1] = 0;
r->n_allocs = 0; r->n_alias = 0;
t = time(NULL);
if (wipe_clean == 3) t = 0; //** Nothing gets deleted in this mode
cnt = 0; pcnt = 0;
ecnt = 0;
nbuff = 0;
max_expiration = time(0) + r->max_duration;
a = &(alist[nbuff]);
iter = rebuild_begin(r, wipe_clean);
err = rebuild_get_next(iter, a);
while (err == 0) {
id = a->id;
if (a->expiration < time(NULL)) {
estate = -1;
} else {
estate = (a->expiration > max_expiration) ? 1 : 0;
}
if ((a->expiration < t) && (remove_expired == 1)) {
ecnt++;
log_printf(5, "rebuild_resource(rid=%s): Removing expired record with id: " LU " * estate: %d (remove count:%d)\n", r->name, id, estate, ecnt);
if ((err = rebuild_remove_iter(iter)) != 0) {
log_printf(0, "rebuild_resource(rid=%s): Error Removing id " LU " from DB Error=%d\n", r->name, id, err);
}
} else { //*** Adding the record
if (((a->expiration > max_expiration) && (truncate_expiration == 1)) || (wipe_clean == 3)) {
t1 = a->expiration; t2 = max_expiration;
log_printf(5, "rebuild_resource(rid=%s, wc=%d): Adding record %d with id: " LU " but truncating expiration curr:" TT " * new:" TT " * estate: %d\n",r->name, wipe_clean, cnt, id, t1, t2, estate);
a->expiration = max_expiration;
if ((err = rebuild_modify_iter(iter, a)) != 0) {
log_printf(0, "rebuild_resource(rid=%s): Error Adding id " LU " to primary DB Error=%d\n", r->name, a->id, err);
}
} else {
log_printf(5, "rebuild_resource(rid=%s): Adding record %d with id: " LU " * estate: %d\n",r->name, cnt, id, estate);
}
r->used_space[a->reliability] += a->max_size;
nbuff++;
cnt++;
if (a->is_alias) pcnt++;
}
//**** Buffer is full so update the DB ****
if (nbuff >= a_size) {
for (i=0; i<nbuff; i++) {
if ((err = rebuild_put_iter(iter, &(alist[i]))) != 0) {
log_printf(0, "rebuild_resource(rid=%s): Error Adding id " LU " to DB Error=%d\n", r->name, alist[i].id, err);
}
}
nbuff = 0;
}
a = &(alist[nbuff]);
err = rebuild_get_next(iter, a); //** Get the next record
}
//**** Push whatever is left into the DB ****
for (i=0; i<nbuff; i++) {
if ((err = rebuild_put_iter(iter, &(alist[i]))) != 0) {
log_printf(0, "rebuild_resource(rid=%s): Error Adding id " LU " to DB Error=%d\n", r->name, alist[i].id, err);
}
}
rebuild_end(iter);
r->n_allocs = cnt;
r->n_alias = pcnt;
t = time(NULL);
log_printf(0, "\nrebuild_resource(rid=%s): %d allocations added\n", r->name, cnt);
log_printf(0, "rebuild_resource(rid=%s): %d alias allocations added\n", r->name, pcnt);
log_printf(0, "rebuild_resource(rid=%s): %d allocations removed\n", r->name, ecnt);
Rsize_t mb;
mb = r->used_space[ALLOC_SOFT]/1024/1024; log_printf(0, "#(rid=%s) soft_used = " LU "\n", r->name, mb);
mb = r->used_space[ALLOC_HARD]/1024/1024; log_printf(0, "#(rid=%s) hard_used = " LU "\n", r->name, mb);
log_printf(0, "\nrebuild_resource(rid=%s): Finished Rebuilding RID %s at %s\n", r->name, r->name, ctime(&t));
flush_log();
return(0);
}
//---------------------------------------------------------------------------
//***************************************************************************
// calc_usage - Cycles through all the records to calcualte the used hard
// and soft space. This should only be used if the resource was not
// unmounted cleanly.
//***************************************************************************
int calc_usage(Resource_t *r)
{
DB_iterator_t *dbi;
Allocation_t a;
log_printf(15, "calc_usage(rid=%s): Recalculating usage form scratch\n", r->name);
r->used_space[0] = 0; r->used_space[1] = 0;
r->n_allocs = 0; r->n_alias = 0;
dbi = id_iterator(&(r->db));
while (db_iterator_next(dbi, DB_NEXT, &a) == 0) {
log_printf(10, "calc_usage(rid=%s): n=" LU " ------------- id=" LU "\n", r->name, r->n_allocs, a.id);
//print_allocation_resource(r, stdout, &a);
r->used_space[a.reliability] += a.max_size;
r->n_allocs++;
if (a.is_alias == 1) r->n_alias++;
}
db_iterator_end(dbi);
log_printf(15, "calc_usage(rid=%s): finished... n_allocs= "LU " n_alias=" LU "\n", r->name, r->n_allocs, r->n_alias);
return(0);
}
//***************************************************************************
// perform_truncate - Adjusts all allocations to the given max
// duration. It will also remove any expired allocations.
//***************************************************************************
int perform_truncate(Resource_t *r)
{
DB_iterator_t *dbi;
Allocation_t *a;
const int a_size = 1024;
Allocation_t alist[a_size];
time_t max_expiration, t1, t2;
int estate, err, cnt, ecnt, nbuff, i;
log_printf(15, "calc_usage(rid=%s): Recalculating usage form scratch\n", r->name);
max_expiration = time(0) + r->max_duration;
cnt = 0; ecnt = 0; nbuff = 0;
dbi = id_iterator(&(r->db));
a = &(alist[nbuff]);
while (db_iterator_next(dbi, DB_NEXT, a) == 0) {
if (a->expiration < time(NULL)) {
estate = -1;
} else {
estate = (a->expiration > max_expiration) ? 1 : 0;
}
switch (estate) {
case -1: //** Expired allocation
ecnt++;
log_printf(5, "perform_truncate(rid=%s): Remove cnt=%d id=" LU " * estate: %d\n",r->name, cnt, a->id, estate);
err = _remove_allocation_for_make_free(r, a, dbi);
break;
case 0: //** Ok allocation
cnt++;
log_printf(5, "perform_truncate(rid=%s): cnt=%d id=" LU " * estate: %d\n",r->name, cnt, a->id, estate);
break;
case 1: //** Truncate expiration
cnt++;
t1 = a->expiration;
a->expiration = time(0) + r->max_duration;
t2 = a->expiration;
log_printf(5, "perform_truncate(rid=%s): Truncating duration for record %d with id: " LU " expiration curr:" TT " * new:" TT " * estate: %d\n",r->name, cnt, a->id, t1, t2, estate);
if ((err = modify_alloc_iter_db(dbi, a)) != 0) {
log_printf(0, "perform_truncate(rid=%s): Error modifying id " LU " to primary DB Error=%d\n", r->name, a->id, err);
}
if (nbuff >= (a_size-1)) {
log_printf(5, "perform_truncate(rid=%s): Dumping buffer=%d\n",r->name, nbuff);
if (r->update_alloc == 1) {
for (i=0; i<=nbuff; i++) {
a = &(alist[i]);
if (a->is_alias == 0) {
r->fs->write(a->id, 0, sizeof(Allocation_t), a);
} else if (r->enable_alias_history) {
r->fs->write(a->id, 0, sizeof(Allocation_t), a);
}
}
}
nbuff = 0;
} else {
nbuff++;
}
a = &(alist[nbuff]);
break;
}
}
db_iterator_end(dbi);
if (nbuff > 0) {
log_printf(5, "perform_truncate(rid=%s): Dumping buffer=%d\n",r->name, nbuff);
if (r->update_alloc == 1) {
for (i=0; i<nbuff; i++) {
a = &(alist[i]);
if (a->is_alias == 0) {
r->fs->write(a->id, 0, sizeof(Allocation_t), a);
} else if (r->enable_alias_history) {
r->fs->write(a->id, 0, sizeof(Allocation_t), a);
}
}
}
}
log_printf(15, "perform_truncate(rid=%s): finished... n_allocs=%d n_removed=%d\n", r->name, cnt, ecnt);
return(0);
}
//***************************************************************************
// parse_resource - Parses the resource Keyfile
//***************************************************************************
int parse_resource(Resource_t *res, GKeyFile *keyfile, char *group)
{
GKeyFileFlags flags;
char *str;
flags = G_KEY_FILE_NONE;
res->keygroup = group;
res->name = g_key_file_get_string(keyfile, group, "rid", NULL);
if (res->name == NULL) {
printf("parse_resource: (%s) Missing resource ID\n",group);
abort();
}
str2rid(res->name, &(res->rid));
res->preallocate = g_key_file_get_integer(keyfile, group, "preallocate", NULL);
res->update_alloc = g_key_file_get_integer(keyfile, group, "update_alloc", NULL);
res->enable_write_history = g_key_file_get_integer(keyfile, group, "enable_write_history", NULL);
res->enable_read_history = g_key_file_get_integer(keyfile, group, "enable_read_history", NULL);
res->enable_manage_history = g_key_file_get_integer(keyfile, group, "enable_manage_history", NULL);
res->enable_alias_history = g_key_file_get_integer(keyfile, group, "enable_alias_history", NULL);
res->max_duration = g_key_file_get_integer(keyfile, group, "max_duration", NULL);
if (res->max_duration == 0) {
printf("parse_resource: (%s) Missing max duration: %d\n",group, res->max_duration);
abort();
}
res->device_type = g_key_file_get_string(keyfile, group, "resource_type", NULL);
if ((strcmp(res->device_type, DEVICE_DIR) != 0) &&
(strcmp(res->device_type, DEVICE_EBOFS) != 0)) {
printf("parse_resource: (%s) Invalid device type: %s\n",group, res->device_type);
abort();
}
res->device = g_key_file_get_string(keyfile, group, "device", NULL);
if (res->device == NULL) {
printf("parse_resource: (%s) Missing resource device\n",group);
abort();
}
res->location = g_key_file_get_string(keyfile, group, "db_location", NULL);
if (res->location == NULL) {
printf("parse_resource: (%s) Missing resource device location for DB and other files\n",group);
abort();
}
str = g_key_file_get_string(keyfile, group, "max_size", NULL);
if (str == NULL) {
printf("parse_resource: (%s) Missing max_size for resource\n",group);
abort();
}
sscanf(str, "" LU "", &(res->max_size[ALLOC_TOTAL]));
res->max_size[ALLOC_TOTAL] *= 1024*1024;
free(str);
str = g_key_file_get_string(keyfile, group, "soft_size", NULL);
if (str == NULL) {
printf("parse_resource: (%s) Missing soft_size for resource\n",group);
abort();
}
sscanf(str, "" LU "", &(res->max_size[ALLOC_SOFT]));
res->max_size[ALLOC_SOFT] *= 1024*1024;
free(str);
str = g_key_file_get_string(keyfile, group, "hard_size", NULL);
if (str == NULL) {
printf("parse_resource: (%s) Missing hard_size for resource\n",group);
abort();
}
sscanf(str, "" LU "", &(res->max_size[ALLOC_HARD]));
res->max_size[ALLOC_HARD] *= 1024*1024;
free(str);
str = g_key_file_get_string(keyfile, group, "minfree_size", NULL);
if (str == NULL) {
printf("parse_resource: (%s) Missing minfreesize for resource\n",group);
abort();
}
sscanf(str, "" LU "", &(res->minfree));
res->minfree *= 1024*1024;
free(str);
return(0);
}
//***************************************************************************
// mount_resource - Mounts a resource for use
//***************************************************************************
int mount_resource(Resource_t *res, GKeyFile *keyfile, char *group, DB_env_t *dbenv,
int force_rebuild, int lazy_allocate, int truncate_expiration)
{
int err;
memset(_blanks, 0, _RESOURCE_BUF_SIZE); //** Thisi s done multiple times and it doesn't have to be but is trivial
//*** Load the resource data ***
assert(parse_resource(res, keyfile, group) == 0);
res->pending = 0;
log_printf(15, "mount_resource: rid=%s force_rebuild=%d\n", res->name, force_rebuild);
res->lazy_allocate = lazy_allocate;
//*** Now mount the device ***
if (strcmp("dir", res->device_type)==0) {
DIR *dir;
assert((dir = opendir(res->device)) != NULL);
closedir(dir);
assert((res->fs = new osd_fs(res->device)) != NULL);
} else {
assert((res->fs = new osd_ebofs(res->device, 0)) != NULL);
}
//*** and also mount the DB ***
GError *error=NULL;
GKeyFile *kfd;
GKeyFileFlags flags;
char fname[2048];
flags = G_KEY_FILE_NONE;
/* Load the GKeyFile from keyfile.conf or return. */
assert((kfd = g_key_file_new()) != NULL);
snprintf(fname, sizeof(fname), "%s/config", res->location);
if (!g_key_file_load_from_file (kfd, fname, flags, &error))
{
g_error (error->message);
abort();
}
//** Init the lock **
pthread_mutex_init(&(res->mutex), NULL);
pthread_mutex_unlock(&(res->mutex));
pthread_mutex_init(&(res->cleanup_lock), NULL);
pthread_mutex_unlock(&(res->cleanup_lock));
pthread_cond_init(&(res->cleanup_cond), NULL);
res->cleanup_shutdown = -1;
//** Rebuild the DB or mount it here **
snprintf(fname, sizeof(fname), "%s/usage", res->location);
if (force_rebuild) {
switch (force_rebuild) {
case 1:
err = mount_db_generic(kfd, dbenv, &(res->db), 1);
if (err == 0) {
calc_usage(res);
if (truncate_expiration == 1) perform_truncate(res);
}
break;
default:
err = rebuild_resource(res, dbenv, 1, force_rebuild, truncate_expiration);
}
} else if (read_usage_file(fname, res) == 1) {
err = mount_db_generic(kfd, dbenv, &(res->db), 1);
calc_usage(res);
// err = rebuild_resource(res, dbenv, 1, 1, truncate_expiration);
} else {
err = mount_db_generic(kfd, dbenv, &(res->db), 0);
}
if (err != 0) return(err);
err = mount_history_table(res);
if (err != 0) {
log_printf(0, "mount_resource: Error mounting history table! res=%s err=%d\n", res->name, err);
return(err);
}
if (err != 0) return(err);
//*** clean up ***
g_key_file_free(kfd);
//** Update the usage **
write_usage_file(fname, res, _RESOURCE_STATE_BAD); //**Mark it as dirty
//** Launch the cleanup thread
// pthread_create(&(res->cleanup_thread), NULL, resource_cleanup_thread, (void *)res);
return(err);
}
//***************************************************************************
// umount_resource - Unmounts the given resource
//***************************************************************************
int umount_resource(Resource_t *res)
{
char fname[2048];
void *dummy;
log_printf(15, "umount_resource: Unmounting resource %s\n", res->name); flush_log();
//** Kill the cleanup thread
if (res->cleanup_shutdown == 0) {
pthread_mutex_lock(&(res->cleanup_lock));
res->cleanup_shutdown = 1;
pthread_cond_signal(&(res->cleanup_cond));
pthread_mutex_unlock(&(res->cleanup_lock));
pthread_join(res->cleanup_thread, &dummy);
}
umount_db(&(res->db));
umount_history_table(res);
res->fs->umount();
snprintf(fname, sizeof(fname), "%s/usage", res->location);
write_usage_file(fname, res, _RESOURCE_STATE_GOOD);
return(0);
}
//---------------------------------------------------------------------------
//***************************************************************************
// print_resource - Prints the resource information out to fd.
//***************************************************************************
int print_resource(Resource_t *res, FILE *fd)
{
Rsize_t n;
fprintf(fd, "[%s]\n", res->keygroup);
fprintf(fd, "rid = %s\n", res->name);
fprintf(fd, "max_duration = %d\n", res->max_duration);
fprintf(fd, "resource_type = %s\n", res->device_type);
fprintf(fd, "device = %s\n", res->device);
fprintf(fd, "db_location = %s\n", res->location);
fprintf(fd, "update_alloc = %d\n", res->update_alloc);
fprintf(fd, "enable_read_history = %d\n", res->enable_read_history);
fprintf(fd, "enable_write_history = %d\n", res->enable_write_history);
fprintf(fd, "enable_manage_history = %d\n", res->enable_manage_history);
fprintf(fd, "enable_alias_history = %d\n", res->enable_alias_history);
n = res->max_size[ALLOC_TOTAL]/1024/1024; fprintf(fd, "max_size = " LU "\n", n);
n = res->max_size[ALLOC_SOFT]/1024/1024; fprintf(fd, "soft_size = " LU "\n", n);
n = res->max_size[ALLOC_HARD]/1024/1024; fprintf(fd, "hard_size = " LU "\n", n);
n = res->minfree/1024/1024; fprintf(fd, "minfree_size = " LU "\n", n);
fprintf(fd, "preallocate = %d\n", res->preallocate);
fprintf(fd, "\n");