forked from wesedens/fs-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.c
2404 lines (1844 loc) · 64.4 KB
/
cache.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
/*
This file contains the global device cache for the BeOS. All
file system I/O comes through here. The cache can handle blocks
of different sizes for multiple different underlying physical
devices.
The cache is organized as a hash table (for lookups by device
and block number) and two doubly-linked lists. The normal
list is for "normal" blocks which are either clean or dirty.
The locked list is for blocks that are locked in the cache by
BFS. The lists are LRU ordered.
Most of the work happens in the function cache_block_io() which
is quite lengthy. The other functions of interest are get_ents()
which picks victims to be kicked out of the cache; flush_ents()
which does the work of flushing them; and set_blocks_info() which
handles cloning blocks and setting callbacks so that the BFS
journal will work properly. If you want to modify this code it
will take some study but it's not too bad. Do not think about
separating the list of clean and dirty blocks into two lists as
I did that already and it's slower.
Originally this cache code was written while listening to the album
"Ride the Lightning" by Metallica. The current version was written
while listening to "Welcome to SkyValley" by Kyuss as well as an
ambient collection on the German label FAX. Music helps a lot when
writing code.
THIS CODE COPYRIGHT DOMINIC GIAMPAOLO. NO WARRANTY IS EXPRESSED
OR IMPLIED. YOU MAY USE THIS CODE AND FREELY DISTRIBUTE IT FOR
NON-COMMERCIAL USE AS LONG AS THIS NOTICE REMAINS ATTACHED.
FOR COMMERCIAL USE, CONTACT DOMINIC GIAMPAOLO (dbg@be.com).
Dominic Giampaolo
dbg@be.com
*/
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
#include "compat.h"
#include "lock.h"
#include "cache.h"
#define kprintf printf
/* forward prototypes */
static int flush_ents(cache_ent **ents, int n_ents);
static int do_dump(int argc, char **argv);
static int do_find_block(int argc, char **argv);
static int do_find_data(int argc, char **argv);
static void cache_flusher(void *arg, int phase);
int chatty_io = 0;
#define CHUNK (512 * 1024) /* a hack to work around scsi driver bugs */
size_t
read_phys_blocks(int fd, fs_off_t bnum, void *data, uint num_blocks, int bsize)
{
size_t ret = 0;
size_t sum;
if (chatty_io)
printf("R: %8ld : %3d\n", bnum, num_blocks);
if (num_blocks * bsize < CHUNK)
ret = read_pos(fd, bnum * bsize, data, num_blocks * bsize);
else {
for(sum=0; (sum + CHUNK) <= (num_blocks * bsize); sum += CHUNK) {
ret = read_pos(fd, (bnum * bsize) + sum, data, CHUNK);
if (ret != CHUNK)
break;
data = (void *)((char *)data + CHUNK);
}
if (ret == CHUNK && ((num_blocks * bsize) - sum) > 0) {
ret = read_pos(fd, (bnum * bsize) + sum, data,
(num_blocks * bsize) - sum);
if (ret == (num_blocks * bsize) - sum)
ret = num_blocks * bsize;
} else if (ret == CHUNK) {
ret = num_blocks * bsize;
}
}
if (ret == num_blocks * bsize)
return 0;
else
return EBADF;
}
size_t
write_phys_blocks(int fd, fs_off_t bnum, void *data, uint num_blocks, int bsize)
{
size_t ret = 0;
size_t sum;
if (chatty_io)
printf("W: %8ld : %3d\n", bnum, num_blocks);
if (num_blocks * bsize < CHUNK)
ret = write_pos(fd, bnum * bsize, data, num_blocks * bsize);
else {
for(sum=0; (sum + CHUNK) <= (num_blocks * bsize); sum += CHUNK) {
ret = write_pos(fd, (bnum * bsize) + sum, data, CHUNK);
if (ret != CHUNK)
break;
data = (void *)((char *)data + CHUNK);
}
if (ret == CHUNK && ((num_blocks * bsize) - sum) > 0) {
ret = write_pos(fd, (bnum * bsize) + sum, data,
(num_blocks * bsize) - sum);
if (ret == (num_blocks * bsize) - sum)
ret = num_blocks * bsize;
} else if (ret == CHUNK) {
ret = num_blocks * bsize;
}
}
if (ret == num_blocks * bsize)
return 0;
else
return EBADF;
}
static int
init_hash_table(hash_table *ht)
{
ht->max = HT_DEFAULT_MAX;
ht->mask = ht->max - 1;
ht->num_elements = 0;
ht->table = (hash_ent **)calloc(ht->max, sizeof(hash_ent *));
if (ht->table == NULL)
return ENOMEM;
return 0;
}
static void
shutdown_hash_table(hash_table *ht)
{
int i, hash_len;
hash_ent *he, *next;
for(i=0; i < ht->max; i++) {
he = ht->table[i];
for(hash_len=0; he; hash_len++, he=next) {
next = he->next;
free(he);
}
}
if (ht->table)
free(ht->table);
ht->table = NULL;
}
static void
print_hash_stats(hash_table *ht)
{
int i, hash_len, max = -1, sum = 0;
hash_ent *he, *next;
for(i=0; i < ht->max; i++) {
he = ht->table[i];
for(hash_len=0; he; hash_len++, he=next) {
next = he->next;
}
if (hash_len)
printf("bucket %3d : %3d\n", i, hash_len);
sum += hash_len;
if (hash_len > max)
max = hash_len;
}
printf("max # of chains: %d, average chain length %d\n", max,sum/ht->max);
}
#define HASH(d, b) ((((fs_off_t)d) << (sizeof(fs_off_t)*8 - 6)) | (b))
static hash_ent *
new_hash_ent(int dev, fs_off_t bnum, void *data)
{
hash_ent *he;
he = (hash_ent *)malloc(sizeof(*he));
if (he == NULL)
return NULL;
he->hash_val = HASH(dev, bnum);
he->dev = dev;
he->bnum = bnum;
he->data = data;
he->next = NULL;
return he;
}
static int
grow_hash_table(hash_table *ht)
{
int i, omax, newsize, newmask;
fs_off_t hash;
hash_ent **new_table, *he, *next;
if (ht->max & ht->mask) {
printf("*** hashtable size %d or mask %d looks weird!\n", ht->max,
ht->mask);
}
omax = ht->max;
newsize = omax * 2; /* have to grow in powers of two */
newmask = newsize - 1;
new_table = (hash_ent **)calloc(newsize, sizeof(hash_ent *));
if (new_table == NULL)
return ENOMEM;
for(i=0; i < omax; i++) {
for(he=ht->table[i]; he; he=next) {
hash = he->hash_val & newmask;
next = he->next;
he->next = new_table[hash];
new_table[hash] = he;
}
}
free(ht->table);
ht->table = new_table;
ht->max = newsize;
ht->mask = newmask;
return 0;
}
static int
hash_insert(hash_table *ht, int dev, fs_off_t bnum, void *data)
{
fs_off_t hash;
hash_ent *he, *curr;
hash = HASH(dev, bnum) & ht->mask;
curr = ht->table[hash];
for(; curr != NULL; curr=curr->next)
if (curr->dev == dev && curr->bnum == bnum)
break;
if (curr && curr->dev == dev && curr->bnum == bnum) {
printf("entry %d:%ld already in the hash table!\n", dev, bnum);
return EEXIST;
}
he = new_hash_ent(dev, bnum, data);
if (he == NULL)
return ENOMEM;
he->next = ht->table[hash];
ht->table[hash] = he;
ht->num_elements++;
if (ht->num_elements >= ((ht->max * 3) / 4)) {
if (grow_hash_table(ht) != 0)
return ENOMEM;
}
return 0;
}
static void *
hash_lookup(hash_table *ht, int dev, fs_off_t bnum)
{
hash_ent *he;
he = ht->table[HASH(dev, bnum) & ht->mask];
for(; he != NULL; he=he->next) {
if (he->dev == dev && he->bnum == bnum)
break;
}
if (he)
return he->data;
else
return NULL;
}
static void *
hash_delete(hash_table *ht, int dev, fs_off_t bnum)
{
void *data;
fs_off_t hash;
hash_ent *he, *prev = NULL;
hash = HASH(dev, bnum) & ht->mask;
he = ht->table[hash];
for(; he != NULL; prev=he,he=he->next) {
if (he->dev == dev && he->bnum == bnum)
break;
}
if (he == NULL) {
printf("*** hash_delete: tried to delete non-existent block %d:%ld\n",
dev, bnum);
return NULL;
}
data = he->data;
if (ht->table[hash] == he)
ht->table[hash] = he->next;
else if (prev)
prev->next = he->next;
else
panic("hash table is inconsistent\n");
free(he);
ht->num_elements--;
return data;
}
/*
These are the global variables for the cache.
*/
static block_cache bc;
#define MAX_IOVECS 64 /* # of iovecs for use by cache code */
static lock iovec_lock;
static struct iovec *iovec_pool[MAX_IOVECS]; /* each ptr is to an array of iovecs */
static int iovec_used[MAX_IOVECS]; /* non-zero == iovec is in use */
#define NUM_FLUSH_BLOCKS 64 /* size of the iovec array pointed by each ptr */
#define DEFAULT_READ_AHEAD_SIZE (32 * 1024)
static int read_ahead_size = DEFAULT_READ_AHEAD_SIZE;
/* this array stores the size of each device so we can error check requests */
#define MAX_DEVICES 256
fs_off_t max_device_blocks[MAX_DEVICES];
/* has the time of the last cache access so cache flushing doesn't interfere */
static bigtime_t last_cache_access = 0;
int
init_block_cache(int max_blocks, int flags)
{
memset(&bc, 0, sizeof(bc));
memset(iovec_pool, 0, sizeof(iovec_pool));
memset(iovec_used, 0, sizeof(iovec_used));
memset(&max_device_blocks, 0, sizeof(max_device_blocks));
if (init_hash_table(&bc.ht) != 0)
return ENOMEM;
bc.lock.s = iovec_lock.s = -1;
bc.max_blocks = max_blocks;
bc.flags = flags;
if (new_lock(&bc.lock, "bollockcache") != 0)
goto err;
if (new_lock(&iovec_lock, "iovec_lock") != 0)
goto err;
/* allocate two of these up front so vm won't accidently re-enter itself */
iovec_pool[0] = (struct iovec *)malloc(sizeof(struct iovec)*NUM_FLUSH_BLOCKS);
iovec_pool[1] = (struct iovec *)malloc(sizeof(struct iovec)*NUM_FLUSH_BLOCKS);
#ifdef DEBUG
add_debugger_command("bcache", do_dump, "dump the block cache list");
add_debugger_command("fblock", do_find_block, "find a block in the cache");
add_debugger_command("fdata", do_find_data, "find a data block ptr in the cache");
#endif
return 0;
err:
if (bc.lock.s >= 0)
free_lock(&bc.lock);
if (iovec_lock.s >= 0)
free_lock(&iovec_lock);
shutdown_hash_table(&bc.ht);
memset((void *)&bc, 0, sizeof(bc));
return ENOMEM;
}
static struct iovec *
get_iovec_array(void)
{
int i;
struct iovec *iov;
LOCK(iovec_lock);
for(i=0; i < MAX_IOVECS; i++) {
if (iovec_used[i] == 0)
break;
}
if (i >= MAX_IOVECS) /* uh-oh */
panic("cache: ran out of iovecs (pool 0x%x, used 0x%x)!\n",
&iovec_pool[0], &iovec_used[0]);
if (iovec_pool[i] == NULL) {
iovec_pool[i] = (struct iovec *)malloc(sizeof(struct iovec)*NUM_FLUSH_BLOCKS);
if (iovec_pool == NULL)
panic("can't allocate an iovec!\n");
}
iov = iovec_pool[i];
iovec_used[i] = 1;
UNLOCK(iovec_lock);
return iov;
}
static void
release_iovec_array(struct iovec *iov)
{
int i;
LOCK(iovec_lock);
for(i=0; i < MAX_IOVECS; i++) {
if (iov == iovec_pool[i])
break;
}
if (i < MAX_IOVECS)
iovec_used[i] = 0;
else /* uh-oh */
printf("cache: released an iovec I don't own (iov 0x%x)\n", iov);
UNLOCK(iovec_lock);
}
static void
real_dump_cache_list(cache_ent_list *cel)
{
cache_ent *ce;
kprintf("starting from LRU end:\n");
for(ce=cel->lru; ce; ce=ce->next) {
kprintf("ce 0x%.8lx dev %2d bnum %6ld lock %d flag %d arg 0x%.8lx "
"clone 0x%.8lx\n", (ulong)ce, ce->dev, ce->block_num,ce->lock,
ce->flags, (ulong)ce->arg, (ulong)ce->clone);
}
kprintf("MRU end\n");
}
static void
dump_cache_list(void)
{
kprintf("NORMAL BLOCKS\n");
real_dump_cache_list(&bc.normal);
kprintf("LOCKED BLOCKS\n");
real_dump_cache_list(&bc.locked);
kprintf("cur blocks %d, max blocks %d ht @ 0x%lx\n", bc.cur_blocks,
bc.max_blocks, (ulong)&bc.ht);
}
static void
check_bcache(char *str)
{
int count = 0;
cache_ent *ce, *prev = NULL;
LOCK(bc.lock);
for(ce=bc.normal.lru; ce; prev=ce, ce=ce->next) {
count++;
}
for(ce=bc.locked.lru; ce; prev=ce, ce=ce->next) {
count++;
}
if (count != bc.cur_blocks) {
if (count < bc.cur_blocks - 16)
panic("%s: count == %d, cur_blocks %d, prev 0x%x\n",
str, count, bc.cur_blocks, prev);
else
printf("%s: count == %d, cur_blocks %d, prev 0x%x\n",
str, count, bc.cur_blocks, prev);
}
UNLOCK(bc.lock);
}
static void
dump_lists(void)
{
cache_ent *nce;
printf("LOCKED 0x%x (tail 0x%x, head 0x%x)\n", &bc.locked,
bc.locked.lru, bc.locked.mru);
for(nce=bc.locked.lru; nce; nce=nce->next)
printf("nce @ 0x%x dev %d bnum %ld flags %d lock %d clone 0x%x func 0x%x\n",
nce, nce->dev, nce->block_num, nce->flags, nce->lock, nce->clone,
nce->func);
printf("NORMAL 0x%x (tail 0x%x, head 0x%x)\n", &bc.normal,
bc.normal.lru, bc.normal.mru);
for(nce=bc.normal.lru; nce; nce=nce->next)
printf("nce @ 0x%x dev %d bnum %ld flags %d lock %d clone 0x%x func 0x%x\n",
nce, nce->dev, nce->block_num, nce->flags, nce->lock, nce->clone,
nce->func);
}
static void
check_lists(void)
{
cache_ent *ce, *prev, *oce;
cache_ent_list *cel;
cel = &bc.normal;
for(ce=cel->lru,prev=NULL; ce; prev=ce, ce=ce->next) {
for(oce=bc.locked.lru; oce; oce=oce->next) {
if (oce == ce) {
dump_lists();
panic("1:ce @ 0x%x is in two lists(cel 0x%x &LOCKED)\n",ce,cel);
}
}
}
if (prev && prev != cel->mru) {
dump_lists();
panic("*** last element in list != cel mru (ce 0x%x, cel 0x%x)\n",
prev, cel);
}
cel = &bc.locked;
for(ce=cel->lru,prev=NULL; ce; prev=ce, ce=ce->next) {
for(oce=bc.normal.lru; oce; oce=oce->next) {
if (oce == ce) {
dump_lists();
panic("3:ce @ 0x%x is in two lists(cel 0x%x & DIRTY)\n",ce,cel);
}
}
}
if (prev && prev != cel->mru) {
dump_lists();
panic("*** last element in list != cel mru (ce 0x%x, cel 0x%x)\n",
prev, cel);
}
}
#ifdef DEBUG
static int
do_dump(int argc, char **argv)
{
dump_cache_list();
return 1;
}
static int
do_find_block(int argc, char **argv)
{
int i;
fs_off_t bnum;
cache_ent *ce;
if (argc < 2) {
kprintf("%s: needs a block # argument\n", argv[0]);
return 1;
}
for(i=1; i < argc; i++) {
bnum = strtoul(argv[i], NULL, 0);
for(ce=bc.normal.lru; ce; ce=ce->next) {
if (ce->block_num == bnum) {
kprintf("found clean bnum %ld @ 0x%lx (data @ 0x%lx)\n",
bnum, ce, ce->data);
}
}
for(ce=bc.locked.lru; ce; ce=ce->next) {
if (ce->block_num == bnum) {
kprintf("found locked bnum %ld @ 0x%lx (data @ 0x%lx)\n",
bnum, ce, ce->data);
}
}
}
return 0;
}
static int
do_find_data(int argc, char **argv)
{
int i;
void *data;
cache_ent *ce;
if (argc < 2) {
kprintf("%s: needs a block # argument\n", argv[0]);
return 1;
}
for(i=1; i < argc; i++) {
data = (void *)strtoul(argv[i], NULL, 0);
for(ce=bc.normal.lru; ce; ce=ce->next) {
if (ce->data == data) {
kprintf("found normal data ptr for bnum %ld @ ce 0x%lx\n",
ce->block_num, ce);
}
}
for(ce=bc.locked.lru; ce; ce=ce->next) {
if (ce->data == data) {
kprintf("found locked data ptr for bnum %ld @ ce 0x%lx\n",
ce->block_num, ce);
}
}
}
return 0;
}
#endif /* DEBUG */
/*
this function detaches the cache_ent from the list.
*/
static void
delete_from_list(cache_ent_list *cel, cache_ent *ce)
{
if (ce->next)
ce->next->prev = ce->prev;
if (ce->prev)
ce->prev->next = ce->next;
if (cel->lru == ce)
cel->lru = ce->next;
if (cel->mru == ce)
cel->mru = ce->prev;
ce->next = NULL;
ce->prev = NULL;
}
/*
this function adds the cache_ent ce to the head of the
list (i.e. the MRU end). the cache_ent should *not*
be in any lists.
*/
static void
add_to_head(cache_ent_list *cel, cache_ent *ce)
{
if (ce->next != NULL || ce->prev != NULL) {
panic("*** ath: ce has non-null next/prev ptr (ce 0x%x nxt 0x%x, prv 0x%x)\n",
ce, ce->next, ce->prev);
}
ce->next = NULL;
ce->prev = cel->mru;
if (cel->mru)
cel->mru->next = ce;
cel->mru = ce;
if (cel->lru == NULL)
cel->lru = ce;
}
/*
this function adds the cache_ent ce to the tail of the
list (i.e. the MRU end). the cache_ent should *not*
be in any lists.
*/
static void
add_to_tail(cache_ent_list *cel, cache_ent *ce)
{
if (ce->next != NULL || ce->prev != NULL) {
panic("*** att: ce has non-null next/prev ptr (ce 0x%x nxt 0x%x, prv 0x%x)\n",
ce, ce->next, ce->prev);
}
ce->next = cel->lru;
ce->prev = NULL;
if (cel->lru)
cel->lru->prev = ce;
cel->lru = ce;
if (cel->mru == NULL)
cel->mru = ce;
}
static int
cache_ent_cmp(const void *a, const void *b)
{
fs_off_t diff;
cache_ent *p1 = *(cache_ent **)a, *p2 = *(cache_ent **)b;
if (p1 == NULL || p2 == NULL)
panic("cache_ent pointers are null?!? (a 0x%lx, b 0x%lx\n)\n", a, b);
if (p1->dev == p2->dev) {
diff = p1->block_num - p2->block_num;
return (int)diff;
} else {
return p1->dev - p2->dev;
}
}
static void
cache_flusher(void *arg, int phase)
{
int i, num_ents, err;
bigtime_t now = system_time();
static cache_ent *ce = NULL;
static cache_ent *ents[NUM_FLUSH_BLOCKS];
/*
if someone else was in the cache recently then just bail out so
we don't lock them out unnecessarily
*/
if ((now - last_cache_access) < 1000000)
return;
LOCK(bc.lock);
ce = bc.normal.lru;
for(num_ents=0; ce && num_ents < NUM_FLUSH_BLOCKS; ce=ce->next) {
if (ce->flags & CE_BUSY)
continue;
if ((ce->flags & CE_DIRTY) == 0 && ce->clone == NULL)
continue;
ents[num_ents] = ce;
ents[num_ents]->flags |= CE_BUSY;
num_ents++;
}
/* if we've got some room left over, look for cloned locked blocks */
if (num_ents < NUM_FLUSH_BLOCKS) {
ce = bc.locked.lru;
for(; num_ents < NUM_FLUSH_BLOCKS;) {
for(;
ce && ((ce->flags & CE_BUSY) || ce->clone == NULL);
ce=ce->next)
/* skip ents that meet the above criteria */;
if (ce == NULL)
break;
ents[num_ents] = ce;
ents[num_ents]->flags |= CE_BUSY;
ce = ce->next;
num_ents++;
}
}
UNLOCK(bc.lock);
if (num_ents == 0)
return;
qsort(ents, num_ents, sizeof(cache_ent **), cache_ent_cmp);
if ((err = flush_ents(ents, num_ents)) != 0) {
printf("flush ents failed (ents @ 0x%lx, num_ents %d!\n",
(ulong)ents, num_ents);
}
for(i=0; i < num_ents; i++) { /* clear the busy bit on each of ent */
ents[i]->flags &= ~CE_BUSY;
}
}
static int
flush_cache_ent(cache_ent *ce)
{
int ret = 0;
void *data;
/* if true, then there's nothing to flush */
if ((ce->flags & CE_DIRTY) == 0 && ce->clone == NULL)
return 0;
/* same thing here */
if (ce->clone == NULL && ce->lock != 0)
return 0;
restart:
if (ce->clone)
data = ce->clone;
else
data = ce->data;
/* printf("flush: %7d\n", ce->block_num); */
ret = write_phys_blocks(ce->dev, ce->block_num, data, 1, ce->bsize);
if (ce->func) {
ce->func(ce->logged_bnum, 1, ce->arg);
ce->func = NULL;
}
if (ce->clone) {
free(ce->clone);
ce->clone = NULL;
if (ce->lock == 0 && (ce->flags & CE_DIRTY))
goto restart; /* also write the real data ptr */
} else {
ce->flags &= ~CE_DIRTY;
}
return ret;
}
static int
flush_ents(cache_ent **ents, int n_ents)
{
int i, j, k, ret = 0, bsize, iocnt, do_again = 0;
fs_off_t start_bnum;
struct iovec *iov;
iov = get_iovec_array();
if (iov == NULL)
return ENOMEM;
restart:
for(i=0; i < n_ents; i++) {
/* if true, then there's nothing to flush */
if ((ents[i]->flags & CE_DIRTY) == 0 && ents[i]->clone == NULL)
continue;
/* if true we can't touch the dirty data yet because it's locked */
if (ents[i]->clone == NULL && ents[i]->lock != 0)
continue;
bsize = ents[i]->bsize;
start_bnum = ents[i]->block_num;
for(j=i+1; j < n_ents && (j - i) < NUM_FLUSH_BLOCKS; j++) {
if (ents[j]->dev != ents[i]->dev ||
ents[j]->block_num != start_bnum + (j - i))
break;
if (ents[j]->clone == NULL && ents[j]->lock != 0)
break;
}
if (j == i+1) { /* only one block, just flush it directly */
if ((ret = flush_cache_ent(ents[i])) != 0)
break;
continue;
}
for(k=i,iocnt=0; k < j; k++,iocnt++) {
if (ents[k]->clone)
iov[iocnt].iov_base = ents[k]->clone;
else
iov[iocnt].iov_base = ents[k]->data;
iov[iocnt].iov_len = bsize;
}
/* printf("writev @ %ld for %d blocks", start_bnum, iocnt); */
ret = writev_pos(ents[i]->dev, start_bnum * (fs_off_t)bsize,
&iov[0], iocnt);
if (ret != iocnt*bsize) {
int idx;
printf("flush_ents: writev failed: iocnt %d start bnum %ld "
"bsize %d, ret %d\n", iocnt, start_bnum, bsize, ret);
for(idx=0; idx < iocnt; idx++)
printf("iov[%2d] = 0x%8x :: %d\n", idx, iov[idx].iov_base,
iov[idx].iov_len);
printf("error %s writing blocks %ld:%d (%d != %d)\n",
strerror(errno), start_bnum, iocnt, ret, iocnt*bsize);
ret = EINVAL;
break;
}
ret = 0;
for(k=i; k < j; k++) {
if (ents[k]->func) {
ents[k]->func(ents[k]->logged_bnum, 1, ents[k]->arg);
ents[k]->func = NULL;
}
if (ents[k]->clone) {
free(ents[k]->clone);
ents[k]->clone = NULL;
} else {
ents[k]->flags &= ~CE_DIRTY;
}
}
i = j - 1; /* i gets incremented by the outer for loop */
}
/*
here we have to go back through and flush any blocks that are
still dirty. with an arched brow you astutely ask, "but how
could this happen given the above loop?" Ahhh young grasshopper
I say, the path through the cache is long and twisty and fraught
with peril. The reason it can happen is that a block can be both
cloned and dirty. The above loop would only flush the cloned half
of the data, not the main dirty block. So we have to go back
through and see if there are any blocks that are still dirty. If
there are we go back to the top of the function and do the whole
thing over. Kind of grody but it is necessary to insure the
correctness of the log for the Be file system.
*/
if (do_again == 0) {
for(i=0; i < n_ents; i++) {
if ((ents[i]->flags & CE_DIRTY) == 0 || ents[i]->lock)
continue;
do_again = 1;
break;
}
if (do_again)
goto restart;
}