forked from xmirror/moosefs
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfilesystem.c
8005 lines (7545 loc) · 214 KB
/
filesystem.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) 2015 Jakub Kruszona-Zawadzki, Core Technology Sp. z o.o.
*
* This file is part of MooseFS.
*
* MooseFS is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 2 (only).
*
* MooseFS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MooseFS; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* or visit http://www.gnu.org/licenses/gpl-2.0.html
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_MMAP
#define BUCKETS_MMAP_ALLOC 1
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <syslog.h>
#include <unistd.h>
#include <sys/types.h>
#ifdef HAVE_PWD_H
#include <pwd.h>
#endif
#include <sys/stat.h>
#include <inttypes.h>
#include <errno.h>
#ifdef HAVE_SYS_MMAN_H
#include <sys/mman.h>
#endif
#include "MFSCommunication.h"
#include "matoclserv.h"
#include "matocsserv.h"
#include "sessions.h"
#include "csdb.h"
#include "chunks.h"
#include "filesystem.h"
#include "openfiles.h"
#include "xattr.h"
#include "posixacl.h"
#include "bio.h"
#include "metadata.h"
#include "datapack.h"
#include "slogger.h"
#include "massert.h"
#include "hashfn.h"
#include "datacachemgr.h"
#include "cfg.h"
#include "main.h"
#include "changelog.h"
#include "buckets.h"
#include "clocks.h"
#include "labelsets.h"
#include "missinglog.h"
#define HASHTAB_LOBITS 24
#define HASHTAB_HISIZE (0x80000000>>(HASHTAB_LOBITS))
#define HASHTAB_LOSIZE (1<<HASHTAB_LOBITS)
#define HASHTAB_MASK (HASHTAB_LOSIZE-1)
#define HASHTAB_MOVEFACTOR 5
#define DEFAULT_LSETID 1
#define DEFAULT_TRASHTIME 86400
#define MAXFNAMELENG 255
#define MAX_INDEX 0x7FFFFFFF
#define EDGEID_MAX UINT64_C(0x7FFFFFFFFFFFFFFF)
#define EDGEID_HASHSIZE 65536
#define CHIDS_NO 0
#define CHIDS_YES 1
#define CHIDS_AUTO 2
typedef struct _bstnode {
uint32_t val,count;
struct _bstnode *left,*right;
} bstnode;
struct _fsnode;
typedef struct _fsedge {
struct _fsnode *child,*parent;
struct _fsedge *nextchild,*nextparent;
struct _fsedge **prevchild,**prevparent;
struct _fsedge *next;
uint64_t edgeid;
uint32_t hashval;
uint16_t nleng;
const uint8_t name[1];
} fsedge;
typedef struct _statsrecord {
uint32_t inodes;
uint32_t dirs;
uint32_t files;
uint32_t chunks;
uint64_t length;
uint64_t size;
uint64_t realsize;
} statsrecord;
typedef struct _quotanode {
uint32_t graceperiod;
uint8_t exceeded; // hard quota exceeded or soft quota reached time limit
uint8_t flags;
uint32_t stimestamp; // time when soft quota exceeded
uint32_t sinodes,hinodes;
uint64_t slength,hlength;
uint64_t ssize,hsize;
uint64_t srealsize,hrealsize;
struct _fsnode *node;
struct _quotanode *next,**prev;
} quotanode;
static quotanode *quotahead;
static uint32_t QuotaDefaultGracePeriod;
typedef struct _fsnode {
uint32_t id;
uint32_t ctime,mtime,atime;
unsigned xattrflag:1;
unsigned aclpermflag:1;
unsigned acldefflag:1;
unsigned flags:5;
unsigned lsetid:8;
unsigned type:4;
unsigned mode:12;
uint32_t uid;
uint32_t gid;
uint32_t trashtime;
fsedge *parents;
struct _fsnode *next;
union _data {
struct _ddata { // type==TYPE_DIRECTORY
fsedge *children;
uint32_t nlink;
uint32_t elements;
statsrecord stats;
quotanode *quota;
uint8_t end;
} ddata;
struct _sdata { // type==TYPE_SYMLINK
uint8_t *path;
uint32_t pleng;
uint8_t end;
} sdata;
struct _devdata {
uint32_t rdev; // type==TYPE_BLOCKDEV ; type==TYPE_CHARDEV
uint8_t end;
} devdata;
struct _fdata { // type==TYPE_FILE ; type==TYPE_TRASH ; type==TYPE_SUSTAINED
uint64_t length;
uint64_t *chunktab;
uint32_t chunks;
uint8_t end;
} fdata;
} data;
} fsnode;
typedef struct _freenode {
uint32_t id;
uint32_t ftime;
struct _freenode *next;
} freenode;
static uint32_t *freebitmask;
static uint32_t bitmasksize;
static uint32_t searchpos;
static freenode *freelist,**freetail;
static fsedge *trash;
static fsedge *sustained;
static fsnode *root;
static fsedge **edgehashtab[HASHTAB_HISIZE];
static uint32_t edgerehashpos;
static uint32_t edgehashsize;
static uint32_t edgehashelem;
static fsnode **nodehashtab[HASHTAB_HISIZE];
static uint32_t noderehashpos;
static uint32_t nodehashsize;
static uint32_t nodehashelem;
static uint32_t hashelements;
static uint32_t maxnodeid;
static uint32_t nodes;
static uint64_t nextedgeid;
static uint64_t trashspace;
static uint64_t sustainedspace;
static uint32_t trashnodes;
static uint32_t sustainednodes;
static uint32_t filenodes;
static uint32_t dirnodes;
static uint64_t *edgeid_id_hashtab;
static fsedge **edgeid_ptr_hashtab;
#define MSGBUFFSIZE 1000000
static uint32_t fsinfo_files=0;
static uint32_t fsinfo_ugfiles=0;
static uint32_t fsinfo_mfiles=0;
static uint32_t fsinfo_mtfiles=0;
static uint32_t fsinfo_msfiles=0;
static uint32_t fsinfo_chunks=0;
static uint32_t fsinfo_ugchunks=0;
static uint32_t fsinfo_mchunks=0;
static char *fsinfo_msgbuff=NULL;
static uint32_t fsinfo_msgbuffleng=0;
static uint32_t fsinfo_loopstart=0;
static uint32_t fsinfo_loopend=0;
static uint32_t test_start_time;
static uint32_t stats_statfs=0;
static uint32_t stats_getattr=0;
static uint32_t stats_setattr=0;
static uint32_t stats_lookup=0;
static uint32_t stats_mkdir=0;
static uint32_t stats_rmdir=0;
static uint32_t stats_symlink=0;
static uint32_t stats_readlink=0;
static uint32_t stats_mknod=0;
static uint32_t stats_unlink=0;
static uint32_t stats_rename=0;
static uint32_t stats_link=0;
static uint32_t stats_readdir=0;
static uint32_t stats_open=0;
static uint32_t stats_read=0;
static uint32_t stats_write=0;
void fs_stats(uint32_t stats[16]) {
stats[0] = stats_statfs;
stats[1] = stats_getattr;
stats[2] = stats_setattr;
stats[3] = stats_lookup;
stats[4] = stats_mkdir;
stats[5] = stats_rmdir;
stats[6] = stats_symlink;
stats[7] = stats_readlink;
stats[8] = stats_mknod;
stats[9] = stats_unlink;
stats[10] = stats_rename;
stats[11] = stats_link;
stats[12] = stats_readdir;
stats[13] = stats_open;
stats[14] = stats_read;
stats[15] = stats_write;
stats_statfs=0;
stats_getattr=0;
stats_setattr=0;
stats_lookup=0;
stats_mkdir=0;
stats_rmdir=0;
stats_symlink=0;
stats_readlink=0;
stats_mknod=0;
stats_unlink=0;
stats_rename=0;
stats_link=0;
stats_readdir=0;
stats_open=0;
stats_read=0;
stats_write=0;
}
CREATE_BUCKET_ALLOCATOR(freenode,freenode,5000)
CREATE_BUCKET_ALLOCATOR(quotanode,quotanode,500)
#define fsnode_dir_malloc() fsnode_malloc(0)
#define fsnode_file_malloc() fsnode_malloc(1)
#define fsnode_symlink_malloc() fsnode_malloc(2)
#define fsnode_dev_malloc() fsnode_malloc(3)
#define fsnode_other_malloc() fsnode_malloc(4)
#define fsnode_dir_free(n) fsnode_free(n,0)
#define fsnode_file_free(n) fsnode_free(n,1)
#define fsnode_symlink_free(n) fsnode_free(n,2)
#define fsnode_dev_free(n) fsnode_free(n,3)
#define fsnode_other_free(n) fsnode_free(n,4)
#define NODE_BUCKET_SIZE 65500
#define NODE_MAX_INDX 5
typedef struct _fsnode_bucket {
uint32_t firstfree;
struct _fsnode_bucket *next;
uint8_t bucket[1];
} fsnode_bucket;
static fsnode_bucket *nrbheads[NODE_MAX_INDX];
static fsnode *nrbfreeheads[NODE_MAX_INDX];
static uint32_t nrbucketsize[NODE_MAX_INDX];
static uint32_t nrelemsize[NODE_MAX_INDX];
static uint64_t fsnode_allocated;
static uint64_t fsnode_used;
static inline void fsnode_init(void) {
uint32_t i;
nrelemsize[0] = offsetof(fsnode,data)+offsetof(struct _ddata,end);
nrelemsize[1] = offsetof(fsnode,data)+offsetof(struct _fdata,end);
nrelemsize[2] = offsetof(fsnode,data)+offsetof(struct _sdata,end);
nrelemsize[3] = offsetof(fsnode,data)+offsetof(struct _devdata,end);
nrelemsize[4] = offsetof(fsnode,data);
for (i=0 ; i<NODE_MAX_INDX ; i++) {
nrbheads[i] = NULL;
nrbfreeheads[i] = NULL;
nrbucketsize[i] = (NODE_BUCKET_SIZE / nrelemsize[i]) * nrelemsize[i];
// fprintf(stderr,"%u %u %u\n",i,nrelemsize[i],nrbucketsize[i]);
}
fsnode_allocated=0;
fsnode_used=0;
}
static inline void fsnode_cleanup(void) {
fsnode_bucket *nrb,*nnrb;
uint32_t i;
for (i=0 ; i<NODE_MAX_INDX ; i++) {
for (nrb = nrbheads[i] ; nrb ; nrb=nnrb) {
nnrb = nrb->next;
#ifdef BUCKETS_MMAP_ALLOC
munmap(nrb,offsetof(fsnode_bucket,bucket)+nrbucketsize[i]);
#else
free(nrb);
#endif
}
nrbheads[i] = NULL;
nrbfreeheads[i] = NULL;
}
fsnode_allocated=0;
fsnode_used=0;
}
static inline fsnode* fsnode_malloc(uint8_t indx) {
fsnode_bucket *nrb;
fsnode *ret;
sassert(indx<NODE_MAX_INDX);
if (nrbfreeheads[indx]) {
ret = nrbfreeheads[indx];
nrbfreeheads[indx] = ret->next;
fsnode_used += nrelemsize[indx];
return ret;
}
if (nrbheads[indx]==NULL || nrbheads[indx]->firstfree + nrelemsize[indx] > nrbucketsize[indx]) {
#ifdef BUCKETS_MMAP_ALLOC
nrb = (fsnode_bucket*)mmap(NULL,offsetof(fsnode_bucket,bucket)+nrbucketsize[indx],PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,-1,0);
#else
nrb = (fsnode_bucket*)malloc(offsetof(fsnode_bucket)+nrbucketsize[indx]);
#endif
passert(nrb);
nrb->next = nrbheads[indx];
nrb->firstfree = 0;
nrbheads[indx] = nrb;
fsnode_allocated += (offsetof(fsnode_bucket,bucket)+nrbucketsize[indx]);
}
ret = (fsnode*)((nrbheads[indx]->bucket) + (nrbheads[indx]->firstfree));
nrbheads[indx]->firstfree += nrelemsize[indx];
fsnode_used += nrelemsize[indx];
return ret;
}
static inline void fsnode_free(fsnode *n,uint8_t indx) {
n->next = nrbfreeheads[indx];
nrbfreeheads[indx] = n;
fsnode_used -= nrelemsize[indx];
}
static inline void fsnode_getusage(uint64_t *allocated,uint64_t *used) {
*allocated = fsnode_allocated;
*used = fsnode_used;
}
#define EDGE_BUCKET_SIZE 65500
#define EDGE_MAX_INDX (MFS_PATH_MAX/8)
#define EDGE_REC_INDX(nleng) (((nleng)-1)/8)
#define EDGE_REC_SIZE(indx) (((indx)+1)*8 + ((offsetof(fsedge,name)+7)&UINT32_C(0xFFFFFFF8)))
//#define EDGE_REC_SIZE(nleng) (((offsetof(fsedge,name)+(nleng))+7)&UINT32_C(0xFFFFFFF8))
typedef struct _fsedge_bucket {
uint32_t firstfree;
struct _fsedge_bucket *next;
uint8_t bucket[1];
} fsedge_bucket;
static fsedge_bucket *erbheads[EDGE_MAX_INDX];
static fsedge *erbfreeheads[EDGE_MAX_INDX];
static uint32_t erbucketsize[EDGE_MAX_INDX];
static uint64_t fsedge_allocated;
static uint64_t fsedge_used;
static inline void fsedge_init(void) {
uint32_t i;
uint32_t recsize;
for (i=0 ; i<EDGE_MAX_INDX ; i++) {
erbheads[i] = NULL;
erbfreeheads[i] = NULL;
recsize = EDGE_REC_SIZE(i);
erbucketsize[i] = (EDGE_BUCKET_SIZE / recsize) * recsize;
}
fsedge_allocated=0;
fsedge_used=0;
}
static inline void fsedge_cleanup(void) {
fsedge_bucket *erb,*nerb;
uint32_t i;
for (i=0 ; i<EDGE_MAX_INDX ; i++) {
for (erb = erbheads[i] ; erb ; erb=nerb) {
nerb = erb->next;
#ifdef BUCKETS_MMAP_ALLOC
munmap(erb,offsetof(fsedge_bucket,bucket)+erbucketsize[i]);
#else
free(erb);
#endif
}
erbheads[i] = NULL;
erbfreeheads[i] = NULL;
}
fsedge_allocated=0;
fsedge_used=0;
}
static inline fsedge* fsedge_malloc(uint16_t nleng) {
fsedge_bucket *erb;
fsedge *ret;
uint16_t indx = EDGE_REC_INDX(nleng);
sassert(indx<EDGE_MAX_INDX);
if (erbfreeheads[indx]) {
ret = erbfreeheads[indx];
erbfreeheads[indx] = ret->next;
fsedge_used += EDGE_REC_SIZE(indx);
return ret;
}
if (erbheads[indx]==NULL || erbheads[indx]->firstfree + EDGE_REC_SIZE(nleng) > erbucketsize[indx]) {
#ifdef BUCKETS_MMAP_ALLOC
erb = (fsedge_bucket*)mmap(NULL,offsetof(fsedge_bucket,bucket)+erbucketsize[indx],PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,-1,0);
#else
erb = (fsedge_bucket*)malloc(offsetof(fsedge_bucket)+erbucketsize[indx]);
#endif
passert(erb);
erb->next = erbheads[indx];
erb->firstfree = 0;
erbheads[indx] = erb;
fsedge_allocated += (offsetof(fsedge_bucket,bucket)+erbucketsize[indx]);
}
ret = (fsedge*)((erbheads[indx]->bucket) + (erbheads[indx]->firstfree));
erbheads[indx]->firstfree += EDGE_REC_SIZE(indx);
fsedge_used += EDGE_REC_SIZE(indx);
return ret;
}
static inline void fsedge_free(fsedge *e,uint16_t nleng) {
uint16_t indx = EDGE_REC_INDX(nleng);
e->next = erbfreeheads[indx];
erbfreeheads[indx] = e;
fsedge_used -= EDGE_REC_SIZE(indx);
}
static inline void fsedge_getusage(uint64_t *allocated,uint64_t *used) {
*allocated = fsedge_allocated;
*used = fsedge_used;
}
#define SYMLINK_BUCKET_SIZE 65500
#define SYMLINK_MAX_INDX (MFS_SYMLINK_MAX/8)
#define SYMLINK_REC_INDX(pathleng) (((pathleng)-1)/8)
#define SYMLINK_REC_SIZE(indx) (((indx)+1)*8)
typedef struct _symlink_bucket {
uint32_t firstfree;
struct _symlink_bucket *next;
uint8_t bucket[1];
} symlink_bucket;
static symlink_bucket *stbheads[SYMLINK_MAX_INDX];
static uint8_t *stbfreeheads[SYMLINK_MAX_INDX];
static uint32_t stbucketsize[SYMLINK_MAX_INDX];
static uint64_t symlink_allocated;
static uint64_t symlink_used;
static inline void symlink_init(void) {
uint32_t i;
uint32_t recsize;
for (i=0 ; i<SYMLINK_MAX_INDX ; i++) {
stbheads[i] = NULL;
stbfreeheads[i] = NULL;
recsize = SYMLINK_REC_SIZE(i);
stbucketsize[i] = (SYMLINK_BUCKET_SIZE / recsize) * recsize;
}
symlink_allocated = 0;
symlink_used = 0;
}
static inline void symlink_cleanup(void) {
symlink_bucket *stb,*nstb;
uint32_t i;
for (i=0 ; i<SYMLINK_MAX_INDX ; i++) {
for (stb = stbheads[i] ; stb ; stb=nstb) {
nstb = stb->next;
#ifdef BUCKETS_MMAP_ALLOC
munmap(stb,offsetof(symlink_bucket,bucket)+stbucketsize[i]);
#else
free(stb);
#endif
}
stbheads[i] = NULL;
stbfreeheads[i] = NULL;
}
symlink_allocated = 0;
symlink_used = 0;
}
static inline uint8_t* symlink_malloc(uint16_t pathleng) {
symlink_bucket *stb;
uint8_t *ret;
uint16_t indx = SYMLINK_REC_INDX(pathleng);
sassert(indx<SYMLINK_MAX_INDX);
if (stbfreeheads[indx]) {
ret = stbfreeheads[indx];
stbfreeheads[indx] = *((uint8_t**)ret);
symlink_used += SYMLINK_REC_SIZE(indx);
return ret;
}
if (stbheads[indx]==NULL || stbheads[indx]->firstfree + SYMLINK_REC_SIZE(indx) > stbucketsize[indx]) {
#ifdef BUCKETS_MMAP_ALLOC
stb = (symlink_bucket*)mmap(NULL,offsetof(symlink_bucket,bucket)+stbucketsize[indx],PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,-1,0);
#else
stb = (symlink_bucket*)malloc(offsetof(symlink_bucket,bucket)+stbucketsize[indx]);
#endif
passert(stb);
stb->next = stbheads[indx];
stb->firstfree = 0;
stbheads[indx] = stb;
symlink_allocated += (offsetof(symlink_bucket,bucket)+stbucketsize[indx]);
}
ret = (uint8_t*)((stbheads[indx]->bucket) + (stbheads[indx]->firstfree));
stbheads[indx]->firstfree += SYMLINK_REC_SIZE(indx);
symlink_used += SYMLINK_REC_SIZE(indx);
return ret;
}
static inline void symlink_free(uint8_t *p,uint16_t pathleng) {
uint16_t indx = SYMLINK_REC_INDX(pathleng);
*((uint8_t**)p) = stbfreeheads[indx];
stbfreeheads[indx] = p;
symlink_used -= SYMLINK_REC_SIZE(indx);
}
static inline void symlink_getusage(uint64_t *allocated,uint64_t *used) {
*allocated = symlink_allocated;
*used = symlink_used;
}
#define CHUNKTAB_BUCKET_SIZE 100000
#define CHUNKTAB_MAX_INDX 121
#define CHUNKTAB_ELEMENT_SIZE(chunks) ((chunks)*sizeof(uint64_t))
#define CHUNKTAB_REC_INDX(chunks) (((chunks)<=0x10)?(chunks)-1:((chunks)<=0x100)?(((chunks)+0xF)/0x10)+0xE:((chunks)<=0x1000)?(((chunks)+0xFF)/0x100)+0x1D:((chunks)<=0x10000)?(((chunks)+0xFFF)/0x1000)+0x2C:((chunks)<=0x100000)?(((chunks)+0xFFFF)/0x10000)+0x3B:((chunks)<=0x1000000)?(((chunks)+0xFFFFF)/0x100000)+0x4A:((chunks)<=0x10000000)?(((chunks)+0xFFFFFF)/0x1000000)+0x59:(((chunks)+UINT64_C(0xFFFFFFF))/0x10000000)+0x68)
#define CHUNKTAB_REC_SIZE(indx) ((((indx)<0x10)?(indx+1):((indx)<0x1F)?((indx)-0xE)*0x10:((indx)<0x2E)?((indx)-0x1D)*0x100:((indx)<0x3D)?((indx)-0x2C)*0x1000:((indx)<0x4C)?((indx)-0x3B)*0x10000:((indx)<0x5B)?((indx)-0x4A)*0x100000:((indx)<0x6A)?((indx)-0x59)*0x1000000:((indx)-0x68)*UINT64_C(0x10000000))*sizeof(uint64_t))
typedef struct _chunktab_bucket {
uint64_t firstfree;
struct _chunktab_bucket *next;
uint8_t bucket[1];
} chunktab_bucket;
static chunktab_bucket *ctbheads[CHUNKTAB_MAX_INDX];
static uint64_t *ctbfreeheads[CHUNKTAB_MAX_INDX];
static uint64_t ctbucketsize[CHUNKTAB_MAX_INDX];
static uint64_t chunktabsize[CHUNKTAB_MAX_INDX];
static uint64_t chunktab_allocated;
static uint64_t chunktab_used;
static inline void chunktab_init(void) {
uint32_t i;
for (i=0 ; i<CHUNKTAB_MAX_INDX ; i++) {
ctbheads[i] = NULL;
ctbfreeheads[i] = NULL;
chunktabsize[i] = CHUNKTAB_REC_SIZE(i);
ctbucketsize[i] = ((CHUNKTAB_BUCKET_SIZE / chunktabsize[i])+1) * chunktabsize[i];
}
chunktab_allocated = 0;
chunktab_used = 0;
}
static inline void chunktab_cleanup(void) {
chunktab_bucket *ctb,*nctb;
uint32_t i;
for (i=0 ; i<CHUNKTAB_MAX_INDX ; i++) {
for (ctb = ctbheads[i] ; ctb ; ctb=nctb) {
nctb = ctb->next;
#ifdef BUCKETS_MMAP_ALLOC
munmap(ctb,(offsetof(chunktab_bucket,bucket)+ctbucketsize[i]));
#else
free(ctb);
#endif
}
ctbheads[i] = NULL;
ctbfreeheads[i] = NULL;
}
chunktab_allocated = 0;
chunktab_used = 0;
}
static inline uint64_t* chunktab_indx_malloc(uint8_t indx) {
chunktab_bucket *ctb;
uint64_t *ret;
if (ctbfreeheads[indx]) {
ret = ctbfreeheads[indx];
ctbfreeheads[indx] = *((uint64_t**)ret);
return ret;
}
if (ctbheads[indx]==NULL || ctbheads[indx]->firstfree + chunktabsize[indx] > ctbucketsize[indx]) {
#ifdef BUCKETS_MMAP_ALLOC
ctb = (chunktab_bucket*)mmap(NULL,(offsetof(chunktab_bucket,bucket)+ctbucketsize[indx]),PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,-1,0);
#else
ctb = (chunktab_bucket*)malloc(offsetof(chunktab_bucket,bucket)+ctbucketsize[indx]);
#endif
passert(ctb);
ctb->next = ctbheads[indx];
ctb->firstfree = 0;
ctbheads[indx] = ctb;
chunktab_allocated += (offsetof(chunktab_bucket,bucket)+ctbucketsize[indx]);
}
ret = (uint64_t*)((ctbheads[indx]->bucket) + (ctbheads[indx]->firstfree));
ctbheads[indx]->firstfree += chunktabsize[indx];
return ret;
}
static inline void chunktab_indx_free(uint64_t *chunktab,uint8_t indx) {
*((uint64_t**)chunktab) = ctbfreeheads[indx];
ctbfreeheads[indx] = chunktab;
}
static inline uint64_t* chunktab_malloc(uint32_t chunks) {
uint8_t indx = CHUNKTAB_REC_INDX(chunks);
if (chunks==0 || indx>=CHUNKTAB_MAX_INDX) {
return NULL;
}
chunktab_used+=CHUNKTAB_ELEMENT_SIZE(chunks);
return chunktab_indx_malloc(indx);
}
static inline void chunktab_free(uint64_t *chunktab,uint32_t chunks) {
uint8_t indx = CHUNKTAB_REC_INDX(chunks);
if (chunks==0 || indx>=CHUNKTAB_MAX_INDX) {
return;
}
chunktab_used-=CHUNKTAB_ELEMENT_SIZE(chunks);
chunktab_indx_free(chunktab,indx);
}
static inline uint64_t* chunktab_realloc(uint64_t *oldchunktab,uint32_t oldchunks,uint32_t newchunks) {
uint64_t *newchunktab;
uint8_t oldindx,newindx;
oldindx = CHUNKTAB_REC_INDX(oldchunks);
newindx = CHUNKTAB_REC_INDX(newchunks);
if (oldindx==newindx) {
chunktab_used+=CHUNKTAB_ELEMENT_SIZE(newchunks);
chunktab_used-=CHUNKTAB_ELEMENT_SIZE(oldchunks);
return oldchunktab;
} else {
if (newindx>=CHUNKTAB_MAX_INDX) {
newchunktab = NULL;
} else {
newchunktab = chunktab_indx_malloc(newindx);
if (oldchunktab!=NULL && oldchunks>0) {
if (newchunks>oldchunks) {
memcpy(newchunktab,oldchunktab,sizeof(uint64_t)*oldchunks);
} else {
memcpy(newchunktab,oldchunktab,sizeof(uint64_t)*newchunks);
}
}
chunktab_used+=CHUNKTAB_ELEMENT_SIZE(newchunks);
}
if (oldchunktab!=NULL && oldindx<CHUNKTAB_MAX_INDX) {
chunktab_indx_free(oldchunktab,oldindx);
chunktab_used-=CHUNKTAB_ELEMENT_SIZE(oldchunks);
}
return newchunktab;
}
}
static inline void chunktab_getusage(uint64_t *allocated,uint64_t *used) {
*allocated = chunktab_allocated;
*used = chunktab_used;
}
void fs_get_memusage(uint64_t allocated[8],uint64_t used[8]) {
allocated[0] = sizeof(fsedge*)*edgerehashpos;
used[0] = sizeof(fsedge*)*edgehashelem;
fsedge_getusage(allocated+1,used+1);
allocated[2] = sizeof(fsnode*)*noderehashpos;
used[2] = sizeof(fsnode*)*nodehashelem;
fsnode_getusage(allocated+3,used+3);
freenode_getusage(allocated+4,used+4);
chunktab_getusage(allocated+5,used+5);
symlink_getusage(allocated+6,used+6);
quotanode_getusage(allocated+7,used+7);
// statsrec_getusage(allocated+7,used+7);
}
uint32_t fsnodes_get_next_id() {
uint32_t i,mask;
while (searchpos<bitmasksize && freebitmask[searchpos]==0xFFFFFFFF) {
searchpos++;
}
if (searchpos==bitmasksize) { // no more freeinodes
uint32_t *tmpfbm;
bitmasksize+=0x80;
tmpfbm = freebitmask;
freebitmask = (uint32_t*)realloc(freebitmask,bitmasksize*sizeof(uint32_t));
if (freebitmask==NULL) {
free(tmpfbm); // pro forma - satisfy cppcheck
}
passert(freebitmask);
memset(freebitmask+searchpos,0,0x80*sizeof(uint32_t));
}
mask = freebitmask[searchpos];
i=0;
while (mask&1) {
i++;
mask>>=1;
}
mask = 1<<i;
freebitmask[searchpos] |= mask;
i+=(searchpos<<5);
if (i>maxnodeid) {
maxnodeid=i;
}
return i;
}
void fsnodes_free_id(uint32_t id,uint32_t ts) {
freenode *n;
n = freenode_malloc();
n->id = id;
n->ftime = ts;
n->next = NULL;
*freetail = n;
freetail = &(n->next);
}
uint8_t fs_univ_freeinodes(uint32_t ts,uint8_t sesflags,uint32_t freeinodes) {
uint32_t fi,pos,mask;
freenode *n,*an;
fi = 0;
n = freelist;
while (n && n->ftime+86400<ts) {
fi++;
pos = (n->id >> 5);
mask = 1<<(n->id&0x1F);
freebitmask[pos] &= ~mask;
if (pos<searchpos) {
searchpos = pos;
}
an = n->next;
freenode_free(n);
n = an;
}
if (n) {
freelist = n;
} else {
freelist = NULL;
freetail = &(freelist);
}
if ((sesflags&SESFLAG_METARESTORE)==0) {
if (fi>0) {
changelog("%" PRIu32 "|FREEINODES():%" PRIu32,ts,fi);
}
} else {
meta_version_inc();
if (freeinodes!=fi) {
return 1;
}
}
return 0;
}
void fsnodes_freeinodes(void) {
fs_univ_freeinodes(main_time(),0,0);
}
uint8_t fs_mr_freeinodes(uint32_t ts,uint32_t freeinodes) {
return fs_univ_freeinodes(ts,SESFLAG_METARESTORE,freeinodes);
}
void fsnodes_init_freebitmask (void) {
bitmasksize = 0x100+(((maxnodeid)>>5)&0xFFFFFF80U);
freebitmask = (uint32_t*)malloc(bitmasksize*sizeof(uint32_t));
passert(freebitmask);
memset(freebitmask,0,bitmasksize*sizeof(uint32_t));
freebitmask[0]=1; // reserve inode 0
searchpos = 0;
}
void fsnodes_used_inode (uint32_t id) {
uint32_t pos,mask;
pos = id>>5;
mask = 1<<(id&0x1F);
freebitmask[pos]|=mask;
}
static inline uint32_t fsnodes_hash(uint32_t parentid,uint16_t nleng,const uint8_t *name) {
uint32_t hash,i;
hash = ((parentid * 0x5F2318BD) + nleng);
for (i=0 ; i<nleng ; i++) {
hash = hash*33+name[i];
}
return hash;
}
static inline uint32_t fsnodes_calc_hash_size(uint32_t elements) {
uint32_t res=1;
while (elements) {
elements>>=1;
res<<=1;
}
if (res==0) {
res = UINT32_C(0x80000000);
}
if (res<HASHTAB_LOSIZE) {
return HASHTAB_LOSIZE;
}
return res;
}
static inline void fsnodes_edge_hash_init(void) {
uint16_t i;
edgehashsize = 0;
edgehashelem = 0;
edgerehashpos = 0;
for (i=0 ; i<HASHTAB_HISIZE ; i++) {
edgehashtab[i] = NULL;
}
}
static inline void fsnodes_edge_hash_cleanup(void) {
uint16_t i;
edgehashelem = 0;
edgehashsize = 0;
edgerehashpos = 0;
for (i=0 ; i<HASHTAB_HISIZE ; i++) {
if (edgehashtab[i]!=NULL) {
#ifdef HAVE_MMAP
munmap(edgehashtab[i],sizeof(fsedge*)*HASHTAB_LOSIZE);
#else
free(edgehashtab[i]);
#endif
}
edgehashtab[i] = NULL;
}
}
static inline void fsnodes_edge_hash_rehash(void) {
uint16_t i;
edgerehashpos = edgehashsize;
edgehashsize *= 2;
for (i=(edgehashsize>>HASHTAB_LOBITS)/2 ; i<edgehashsize>>HASHTAB_LOBITS ; i++) {
#ifdef HAVE_MMAP
edgehashtab[i] = mmap(NULL,sizeof(fsedge*)*HASHTAB_LOSIZE,PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,-1,0);
#else
edgehashtab[i] = malloc(sizeof(fsedge*)*HASHTAB_LOSIZE);
#endif
passert(edgehashtab[i]);
}
}
static inline void fsnodes_edge_hash_move(void) {
uint32_t hash;
uint32_t mask;
uint32_t moved=0;
fsedge **ehptr,**ehptralt,*e;
mask = edgehashsize-1;
do {
if (edgerehashpos>=edgehashsize) { // rehash complete
edgerehashpos = edgehashsize;
return;
}
ehptr = edgehashtab[(edgerehashpos - (edgehashsize/2)) >> HASHTAB_LOBITS] + (edgerehashpos & HASHTAB_MASK);
ehptralt = edgehashtab[edgerehashpos >> HASHTAB_LOBITS] + (edgerehashpos & HASHTAB_MASK);
*ehptralt = NULL;
while ((e=*ehptr)!=NULL) {
hash = e->hashval & mask;
if (hash==edgerehashpos) {
*ehptralt = e;
*ehptr = e->next;
ehptralt = &(e->next);
e->next = NULL;
} else {
ehptr = &(e->next);
}
moved++;
}
edgerehashpos++;
} while (moved<HASHTAB_MOVEFACTOR);
}
static inline fsedge* fsnodes_edge_find(fsnode *node,uint16_t nleng,const uint8_t *name) {
fsedge *e;
uint32_t hash;
uint32_t hashval;
if (edgehashsize==0) {
return NULL;
}
hashval = fsnodes_hash(node->id,nleng,name);
hash = hashval & (edgehashsize-1);
if (edgerehashpos<edgehashsize) {
fsnodes_edge_hash_move();
if (hash >= edgerehashpos) {
hash -= edgehashsize/2;
}
}
for (e=edgehashtab[hash>>HASHTAB_LOBITS][hash&HASHTAB_MASK] ; e ; e=e->next) {
if (e->parent==node && e->hashval==hashval && e->nleng==nleng && memcmp((char*)(e->name),(char*)name,nleng)==0) {
return e;
}
}
return NULL;
}
static inline void fsnodes_edge_delete(fsedge *e) {
fsedge **ehptr,*eit;
uint32_t hash;
if (edgehashsize==0) {
return;
}
hash = (e->hashval) & (edgehashsize-1);
if (edgerehashpos<edgehashsize) {
fsnodes_edge_hash_move();
if (hash >= edgerehashpos) {
hash -= edgehashsize/2;
}
}
ehptr = edgehashtab[hash>>HASHTAB_LOBITS] + (hash&HASHTAB_MASK);
while ((eit=*ehptr)!=NULL) {
if (eit==e) {
*ehptr = e->next;
edgehashelem--;
return;
}
ehptr = &(eit->next);
}
}
static inline void fsnodes_edge_add(fsedge *e) {
uint16_t i;
uint32_t hash;
if (edgehashsize==0) {
edgehashsize = fsnodes_calc_hash_size(hashelements);
edgerehashpos = edgehashsize;
edgehashelem = 0;
for (i=0 ; i<edgehashsize>>HASHTAB_LOBITS ; i++) {
#ifdef HAVE_MMAP
edgehashtab[i] = mmap(NULL,sizeof(fsedge*)*HASHTAB_LOSIZE,PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,-1,0);
#else
edgehashtab[i] = malloc(sizeof(fsedge*)*HASHTAB_LOSIZE);
#endif
passert(edgehashtab[i]);
memset(edgehashtab[i],0,sizeof(fsedge*));
if (edgehashtab[i][0]==NULL) {
memset(edgehashtab[i],0,sizeof(fsedge*)*HASHTAB_LOSIZE);
} else {
for (hash=0 ; hash<HASHTAB_LOSIZE ; hash++) {
edgehashtab[i][hash] = NULL;
}
}
}
}