forked from dorimanx/exfat-nofuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exfat_cache.c
770 lines (591 loc) · 19.3 KB
/
exfat_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
/************************************************************************/
/* */
/* PROJECT : exFAT & FAT12/16/32 File System */
/* FILE : exfat_cache.c */
/* PURPOSE : exFAT Cache Manager */
/* (FAT Cache & Buffer Cache) */
/* */
/*----------------------------------------------------------------------*/
/* NOTES */
/* */
/*----------------------------------------------------------------------*/
/* REVISION HISTORY (Ver 0.9) */
/* */
/* - 2010.11.15 [Sung-Kwan Kim] : first writing */
/* */
/************************************************************************/
#include "exfat_config.h"
#include "exfat_global.h"
#include "exfat_data.h"
#include "exfat_cache.h"
#include "exfat_super.h"
#include "exfat.h"
/*----------------------------------------------------------------------*/
/* Global Variable Definitions */
/*----------------------------------------------------------------------*/
extern FS_STRUCT_T fs_struct[];
/* All buffer structures are protected w/ p_fs->v_sem */
#if 0
extern struct semaphore f_sem;
extern struct semaphore b_sem;
#else
#define sm_P(s)
#define sm_V(s)
#endif
#if 0
/*----------------------------------------------------------------------*/
/* Local Variable Definitions */
/*----------------------------------------------------------------------*/
extern BUF_CACHE_T FAT_cache_array[];
extern BUF_CACHE_T FAT_cache_lru_list;
extern BUF_CACHE_T FAT_cache_hash_list[];
extern BUF_CACHE_T buf_cache_array[];
extern BUF_CACHE_T buf_cache_lru_list;
extern BUF_CACHE_T buf_cache_hash_list[];
#endif
/*----------------------------------------------------------------------*/
/* Local Function Declarations */
/*----------------------------------------------------------------------*/
static INT32 __FAT_read(struct super_block *sb, UINT32 loc, UINT32 *content);
static void __FAT_write(struct super_block *sb, UINT32 loc, UINT32 content);
static BUF_CACHE_T *FAT_cache_find(struct super_block *sb, UINT32 sec);
static BUF_CACHE_T *FAT_cache_get(struct super_block *sb, UINT32 sec);
static void FAT_cache_insert_hash(struct super_block *sb, BUF_CACHE_T *bp);
static void FAT_cache_remove_hash(BUF_CACHE_T *bp);
static UINT8 *__buf_getblk(struct super_block *sb, UINT32 sec);
static BUF_CACHE_T *buf_cache_find(struct super_block *sb, UINT32 sec);
static BUF_CACHE_T *buf_cache_get(struct super_block *sb, UINT32 sec);
static void buf_cache_insert_hash(struct super_block *sb, BUF_CACHE_T *bp);
static void buf_cache_remove_hash(BUF_CACHE_T *bp);
static void push_to_mru(BUF_CACHE_T *bp, BUF_CACHE_T *list);
static void push_to_lru(BUF_CACHE_T *bp, BUF_CACHE_T *list);
static void move_to_mru(BUF_CACHE_T *bp, BUF_CACHE_T *list);
static void move_to_lru(BUF_CACHE_T *bp, BUF_CACHE_T *list);
/*======================================================================*/
/* Cache Initialization Functions */
/*======================================================================*/
INT32 buf_init(struct super_block *sb)
{
FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
INT32 i;
/* LRU list */
p_fs->FAT_cache_lru_list.next = p_fs->FAT_cache_lru_list.prev = &p_fs->FAT_cache_lru_list;
for (i = 0; i < FAT_CACHE_SIZE; i++) {
p_fs->FAT_cache_array[i].drv = -1;
p_fs->FAT_cache_array[i].sec = ~0;
p_fs->FAT_cache_array[i].flag = 0;
p_fs->FAT_cache_array[i].buf_bh = NULL;
p_fs->FAT_cache_array[i].prev = p_fs->FAT_cache_array[i].next = NULL;
push_to_mru(&(p_fs->FAT_cache_array[i]), &p_fs->FAT_cache_lru_list);
}
p_fs->buf_cache_lru_list.next = p_fs->buf_cache_lru_list.prev = &p_fs->buf_cache_lru_list;
for (i = 0; i < BUF_CACHE_SIZE; i++) {
p_fs->buf_cache_array[i].drv = -1;
p_fs->buf_cache_array[i].sec = ~0;
p_fs->buf_cache_array[i].flag = 0;
p_fs->buf_cache_array[i].buf_bh = NULL;
p_fs->buf_cache_array[i].prev = p_fs->buf_cache_array[i].next = NULL;
push_to_mru(&(p_fs->buf_cache_array[i]), &p_fs->buf_cache_lru_list);
}
/* HASH list */
for (i = 0; i < FAT_CACHE_HASH_SIZE; i++) {
p_fs->FAT_cache_hash_list[i].drv = -1;
p_fs->FAT_cache_hash_list[i].sec = ~0;
p_fs->FAT_cache_hash_list[i].hash_next = p_fs->FAT_cache_hash_list[i].hash_prev = &(p_fs->FAT_cache_hash_list[i]);
}
for (i = 0; i < FAT_CACHE_SIZE; i++) {
FAT_cache_insert_hash(sb, &(p_fs->FAT_cache_array[i]));
}
for (i = 0; i < BUF_CACHE_HASH_SIZE; i++) {
p_fs->buf_cache_hash_list[i].drv = -1;
p_fs->buf_cache_hash_list[i].sec = ~0;
p_fs->buf_cache_hash_list[i].hash_next = p_fs->buf_cache_hash_list[i].hash_prev = &(p_fs->buf_cache_hash_list[i]);
}
for (i = 0; i < BUF_CACHE_SIZE; i++) {
buf_cache_insert_hash(sb, &(p_fs->buf_cache_array[i]));
}
return(FFS_SUCCESS);
} /* end of buf_init */
INT32 buf_shutdown(struct super_block *sb)
{
return(FFS_SUCCESS);
} /* end of buf_shutdown */
/*======================================================================*/
/* FAT Read/Write Functions */
/*======================================================================*/
/* in : sb, loc
* out: content
* returns 0 on success
* -1 on error
*/
INT32 FAT_read(struct super_block *sb, UINT32 loc, UINT32 *content)
{
INT32 ret;
sm_P(&f_sem);
ret = __FAT_read(sb, loc, content);
sm_V(&f_sem);
return(ret);
} /* end of FAT_read */
void FAT_write(struct super_block *sb, UINT32 loc, UINT32 content)
{
sm_P(&f_sem);
__FAT_write(sb, loc, content);
sm_V(&f_sem);
} /* end of FAT_write */
static INT32 __FAT_read(struct super_block *sb, UINT32 loc, UINT32 *content)
{
INT32 off;
UINT32 sec, _content;
UINT8 *fat_sector, *fat_entry;
FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
BD_INFO_T *p_bd = &(EXFAT_SB(sb)->bd_info);
if (p_fs->vol_type == FAT12) {
sec = p_fs->FAT1_start_sector + ((loc + (loc >> 1)) >> p_bd->sector_size_bits);
off = (loc + (loc >> 1)) & p_bd->sector_size_mask;
if (off == (p_bd->sector_size-1)) {
fat_sector = FAT_getblk(sb, sec);
if
(!fat_sector)
return -1;
_content = (UINT32) fat_sector[off];
fat_sector = FAT_getblk(sb, ++sec);
if
(!fat_sector)
return -1;
_content |= (UINT32) fat_sector[0] << 8;
} else {
fat_sector = FAT_getblk(sb, sec);
if
(!fat_sector)
return -1;
fat_entry = &(fat_sector[off]);
_content = GET16(fat_entry);
}
if (loc & 1) _content >>= 4;
_content &= 0x00000FFF;
if (_content >= CLUSTER_16(0x0FF8)) {
// return(CLUSTER_32(~0)); // return 0xFFFFFFFF to simplify code
*content = CLUSTER_32(~0);
return 0;
} else {
//return(CLUSTER_32(_content));
*content = CLUSTER_32(_content);
return 0;
}
} else if (p_fs->vol_type == FAT16) {
sec = p_fs->FAT1_start_sector + (loc >> (p_bd->sector_size_bits-1));
off = (loc << 1) & p_bd->sector_size_mask;
fat_sector = FAT_getblk(sb, sec);
if
(!fat_sector)
return -1;
fat_entry = &(fat_sector[off]);
_content = GET16_A(fat_entry);
_content &= 0x0000FFFF;
if (_content >= CLUSTER_16(0xFFF8)) {
//return(CLUSTER_32(~0)); // return 0xFFFFFFFF to simplify code
*content = CLUSTER_32(~0);
return 0;
} else {
//return(CLUSTER_32(_content));
*content = CLUSTER_32(_content);
return 0;
}
} else if (p_fs->vol_type == FAT32) {
sec = p_fs->FAT1_start_sector + (loc >> (p_bd->sector_size_bits-2));
off = (loc << 2) & p_bd->sector_size_mask;
fat_sector = FAT_getblk(sb, sec);
if
(!fat_sector)
return -1;
fat_entry = &(fat_sector[off]);
_content = GET32_A(fat_entry);
_content &= 0x0FFFFFFF;
if (_content >= CLUSTER_32(0x0FFFFFF8)) {
//return(CLUSTER_32(~0)); // return 0xFFFFFFFF to simplify code
*content = CLUSTER_32(~0);
return 0;
} else {
//return(CLUSTER_32(_content));
*content = CLUSTER_32(_content);
return 0;
}
} else {
/* p_fs->vol_type == EXFAT */
sec = p_fs->FAT1_start_sector + (loc >> (p_bd->sector_size_bits-2));
off = (loc << 2) & p_bd->sector_size_mask;
fat_sector = FAT_getblk(sb, sec);
if
(!fat_sector)
return -1;
fat_entry = &(fat_sector[off]);
_content = GET32_A(fat_entry);
if (_content >= CLUSTER_32(0xFFFFFFF8)) {
//return(CLUSTER_32(~0)); // return 0xFFFFFFFF to simplify code
*content = CLUSTER_32(~0);
return 0;
} else {
//return(CLUSTER_32(_content));
*content = CLUSTER_32(_content);
return 0;
}
}
//return(CLUSTER_32(~0));
*content = CLUSTER_32(~0);
return 0;
} /* end of __FAT_read */
static void __FAT_write(struct super_block *sb, UINT32 loc, UINT32 content)
{
INT32 off;
UINT32 sec;
UINT8 *fat_sector, *fat_entry;
FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
BD_INFO_T *p_bd = &(EXFAT_SB(sb)->bd_info);
if (p_fs->vol_type == FAT12) {
content &= 0x00000FFF;
sec = p_fs->FAT1_start_sector + ((loc + (loc >> 1)) >> p_bd->sector_size_bits);
off = (loc + (loc >> 1)) & p_bd->sector_size_mask;
fat_sector = FAT_getblk(sb, sec);
if (loc & 1) { /* odd */
content <<= 4;
if (off == (p_bd->sector_size-1)) {
fat_sector[off] = (UINT8)(content | (fat_sector[off] & 0x0F));
FAT_modify(sb, sec);
fat_sector = FAT_getblk(sb, ++sec);
fat_sector[0] = (UINT8)(content >> 8);
} else {
fat_entry = &(fat_sector[off]);
content |= GET16(fat_entry) & 0x000F;
SET16(fat_entry, content);
}
} else { /* even */
fat_sector[off] = (UINT8)(content);
if (off == (p_bd->sector_size-1)) {
fat_sector[off] = (UINT8)(content);
FAT_modify(sb, sec);
fat_sector = FAT_getblk(sb, ++sec);
fat_sector[0] = (UINT8)((fat_sector[0] & 0xF0) | (content >> 8));
} else {
fat_entry = &(fat_sector[off]);
content |= GET16(fat_entry) & 0xF000;
SET16(fat_entry, content);
}
}
}
else if (p_fs->vol_type == FAT16) {
content &= 0x0000FFFF;
sec = p_fs->FAT1_start_sector + (loc >> (p_bd->sector_size_bits-1));
off = (loc << 1) & p_bd->sector_size_mask;
fat_sector = FAT_getblk(sb, sec);
fat_entry = &(fat_sector[off]);
SET16_A(fat_entry, content);
}
else if (p_fs->vol_type == FAT32) {
content &= 0x0FFFFFFF;
sec = p_fs->FAT1_start_sector + (loc >> (p_bd->sector_size_bits-2));
off = (loc << 2) & p_bd->sector_size_mask;
fat_sector = FAT_getblk(sb, sec);
fat_entry = &(fat_sector[off]);
content |= GET32_A(fat_entry) & 0xF0000000;
SET32_A(fat_entry, content);
}
else { /* p_fs->vol_type == EXFAT */
sec = p_fs->FAT1_start_sector + (loc >> (p_bd->sector_size_bits-2));
off = (loc << 2) & p_bd->sector_size_mask;
fat_sector = FAT_getblk(sb, sec);
fat_entry = &(fat_sector[off]);
SET32_A(fat_entry, content);
}
FAT_modify(sb, sec);
} /* end of __FAT_write */
UINT8 *FAT_getblk(struct super_block *sb, UINT32 sec)
{
BUF_CACHE_T *bp;
FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
bp = FAT_cache_find(sb, sec);
if (bp != NULL) {
move_to_mru(bp, &p_fs->FAT_cache_lru_list);
return(bp->buf_bh->b_data);
}
bp = FAT_cache_get(sb, sec);
FAT_cache_remove_hash(bp);
bp->drv = p_fs->drv;
bp->sec = sec;
bp->flag = 0;
FAT_cache_insert_hash(sb, bp);
if (sector_read(sb, sec, &(bp->buf_bh), 1) != FFS_SUCCESS)
return NULL;
return(bp->buf_bh->b_data);
} /* end of FAT_getblk */
void FAT_modify(struct super_block *sb, UINT32 sec)
{
BUF_CACHE_T *bp;
bp = FAT_cache_find(sb, sec);
if (bp != NULL) {
// bp->flag |= DIRTYBIT;
sector_write(sb, sec, bp->buf_bh, 0);
}
} /* end of FAT_modify */
void FAT_release_all(struct super_block *sb)
{
BUF_CACHE_T *bp;
FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
sm_P(&f_sem);
bp = p_fs->FAT_cache_lru_list.next;
while (bp != &p_fs->FAT_cache_lru_list) {
if (bp->drv == p_fs->drv) {
bp->drv = -1;
bp->sec = ~0;
bp->flag = 0;
__brelse(bp->buf_bh);
bp->buf_bh = NULL;
}
bp = bp->next;
}
sm_V(&f_sem);
} /* end of FAT_release_all */
void FAT_sync(struct super_block *sb)
{
BUF_CACHE_T *bp;
FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
sm_P(&f_sem);
bp = p_fs->FAT_cache_lru_list.next;
while (bp != &p_fs->FAT_cache_lru_list) {
if ((bp->drv == p_fs->drv) && (bp->flag & DIRTYBIT)) {
sync_dirty_buffer(bp->buf_bh);
bp->flag &= ~(DIRTYBIT);
}
bp = bp->next;
}
sm_V(&f_sem);
} /* end of FAT_sync */
static BUF_CACHE_T *FAT_cache_find(struct super_block *sb, UINT32 sec)
{
INT32 off;
BUF_CACHE_T *bp, *hp;
FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
off = (sec + (sec >> p_fs->sectors_per_clu_bits)) & (FAT_CACHE_HASH_SIZE - 1);
hp = &(p_fs->FAT_cache_hash_list[off]);
for (bp = hp->hash_next; bp != hp; bp = bp->hash_next) {
if ((bp->drv == p_fs->drv) && (bp->sec == sec)) {
touch_buffer(bp->buf_bh);
return(bp);
}
}
return(NULL);
} /* end of FAT_cache_find */
static BUF_CACHE_T *FAT_cache_get(struct super_block *sb, UINT32 sec)
{
BUF_CACHE_T *bp;
FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
bp = p_fs->FAT_cache_lru_list.prev;
// if (bp->flag & DIRTYBIT)
// sync_dirty_buffer(bp->buf_bh);
move_to_mru(bp, &p_fs->FAT_cache_lru_list);
return(bp);
} /* end of FAT_cache_get */
static void FAT_cache_insert_hash(struct super_block *sb, BUF_CACHE_T *bp)
{
INT32 off;
BUF_CACHE_T *hp;
FS_INFO_T *p_fs;
p_fs = &(EXFAT_SB(sb)->fs_info);
off = (bp->sec + (bp->sec >> p_fs->sectors_per_clu_bits)) & (FAT_CACHE_HASH_SIZE-1);
hp = &(p_fs->FAT_cache_hash_list[off]);
bp->hash_next = hp->hash_next;
bp->hash_prev = hp;
hp->hash_next->hash_prev = bp;
hp->hash_next = bp;
} /* end of FAT_cache_insert_hash */
static void FAT_cache_remove_hash(BUF_CACHE_T *bp)
{
(bp->hash_prev)->hash_next = bp->hash_next;
(bp->hash_next)->hash_prev = bp->hash_prev;
} /* end of FAT_cache_remove_hash */
/*======================================================================*/
/* Buffer Read/Write Functions */
/*======================================================================*/
UINT8 *buf_getblk(struct super_block *sb, UINT32 sec)
{
UINT8 *buf;
sm_P(&b_sem);
buf = __buf_getblk(sb, sec);
sm_V(&b_sem);
return(buf);
} /* end of buf_getblk */
static UINT8 *__buf_getblk(struct super_block *sb, UINT32 sec)
{
BUF_CACHE_T *bp;
FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
bp = buf_cache_find(sb, sec);
if (bp != NULL) {
move_to_mru(bp, &p_fs->buf_cache_lru_list);
return(bp->buf_bh->b_data);
}
bp = buf_cache_get(sb, sec);
buf_cache_remove_hash(bp);
bp->drv = p_fs->drv;
bp->sec = sec;
bp->flag = 0;
buf_cache_insert_hash(sb, bp);
if (sector_read(sb, sec, &(bp->buf_bh), 1) != FFS_SUCCESS) {
buf_cache_remove_hash(bp);
bp->drv = -1;
bp->sec = ~0;
bp->flag = 0;
bp->buf_bh = NULL;
move_to_lru(bp, &p_fs->buf_cache_lru_list);
return NULL;
}
return(bp->buf_bh->b_data);
} /* end of __buf_getblk */
void buf_modify(struct super_block *sb, UINT32 sec)
{
BUF_CACHE_T *bp;
sm_P(&b_sem);
bp = buf_cache_find(sb, sec);
if (bp != NULL) {
// bp->flag |= DIRTYBIT;
sector_write(sb, sec, bp->buf_bh, 0);
}
sm_V(&b_sem);
} /* end of buf_modify */
void buf_lock(struct super_block *sb, UINT32 sec)
{
BUF_CACHE_T *bp;
sm_P(&b_sem);
bp = buf_cache_find(sb, sec);
if (bp != NULL) bp->flag |= LOCKBIT;
sm_V(&b_sem);
} /* end of buf_lock */
void buf_unlock(struct super_block *sb, UINT32 sec)
{
BUF_CACHE_T *bp;
sm_P(&b_sem);
bp = buf_cache_find(sb, sec);
if (bp != NULL) bp->flag &= ~(LOCKBIT);
sm_V(&b_sem);
} /* end of buf_unlock */
void buf_release(struct super_block *sb, UINT32 sec)
{
BUF_CACHE_T *bp;
FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
sm_P(&b_sem);
bp = buf_cache_find(sb, sec);
if (bp != NULL) {
bp->drv = -1;
bp->sec = ~0;
bp->flag = 0;
__brelse(bp->buf_bh);
bp->buf_bh = NULL;
move_to_lru(bp, &p_fs->buf_cache_lru_list);
}
sm_V(&b_sem);
} /* end of buf_release */
void buf_release_all(struct super_block *sb)
{
BUF_CACHE_T *bp;
FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
sm_P(&b_sem);
bp = p_fs->buf_cache_lru_list.next;
while (bp != &p_fs->buf_cache_lru_list) {
if (bp->drv == p_fs->drv) {
bp->drv = -1;
bp->sec = ~0;
bp->flag = 0;
__brelse(bp->buf_bh);
bp->buf_bh = NULL;
}
bp = bp->next;
}
sm_V(&b_sem);
} /* end of buf_release_all */
void buf_sync(struct super_block *sb)
{
BUF_CACHE_T *bp;
FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
sm_P(&b_sem);
bp = p_fs->buf_cache_lru_list.next;
while (bp != &p_fs->buf_cache_lru_list) {
if ((bp->drv == p_fs->drv) && (bp->flag & DIRTYBIT)) {
sync_dirty_buffer(bp->buf_bh);
bp->flag &= ~(DIRTYBIT);
}
bp = bp->next;
}
sm_V(&b_sem);
} /* end of buf_sync */
static BUF_CACHE_T *buf_cache_find(struct super_block *sb, UINT32 sec)
{
INT32 off;
BUF_CACHE_T *bp, *hp;
FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
off = (sec + (sec >> p_fs->sectors_per_clu_bits)) & (BUF_CACHE_HASH_SIZE - 1);
hp = &(p_fs->buf_cache_hash_list[off]);
for (bp = hp->hash_next; bp != hp; bp = bp->hash_next) {
if ((bp->drv == p_fs->drv) && (bp->sec == sec)) {
touch_buffer(bp->buf_bh);
return(bp);
}
}
return(NULL);
} /* end of buf_cache_find */
static BUF_CACHE_T *buf_cache_get(struct super_block *sb, UINT32 sec)
{
BUF_CACHE_T *bp;
FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
bp = p_fs->buf_cache_lru_list.prev;
while (bp->flag & LOCKBIT) bp = bp->prev;
// if (bp->flag & DIRTYBIT)
// sync_dirty_buffer(bp->buf_bh);
move_to_mru(bp, &p_fs->buf_cache_lru_list);
return(bp);
} /* end of buf_cache_get */
static void buf_cache_insert_hash(struct super_block *sb, BUF_CACHE_T *bp)
{
INT32 off;
BUF_CACHE_T *hp;
FS_INFO_T *p_fs;
p_fs = &(EXFAT_SB(sb)->fs_info);
off = (bp->sec + (bp->sec >> p_fs->sectors_per_clu_bits)) & (BUF_CACHE_HASH_SIZE-1);
hp = &(p_fs->buf_cache_hash_list[off]);
bp->hash_next = hp->hash_next;
bp->hash_prev = hp;
hp->hash_next->hash_prev = bp;
hp->hash_next = bp;
} /* end of buf_cache_insert_hash */
static void buf_cache_remove_hash(BUF_CACHE_T *bp)
{
(bp->hash_prev)->hash_next = bp->hash_next;
(bp->hash_next)->hash_prev = bp->hash_prev;
} /* end of buf_cache_remove_hash */
/*======================================================================*/
/* Local Function Definitions */
/*======================================================================*/
static void push_to_mru(BUF_CACHE_T *bp, BUF_CACHE_T *list)
{
bp->next = list->next;
bp->prev = list;
list->next->prev = bp;
list->next = bp;
} /* end of buf_cache_push_to_mru */
static void push_to_lru(BUF_CACHE_T *bp, BUF_CACHE_T *list)
{
bp->prev = list->prev;
bp->next = list;
list->prev->next = bp;
list->prev = bp;
} /* end of buf_cache_push_to_lru */
static void move_to_mru(BUF_CACHE_T *bp, BUF_CACHE_T *list)
{
bp->prev->next = bp->next;
bp->next->prev = bp->prev;
push_to_mru(bp, list);
} /* end of buf_cache_move_to_mru */
static void move_to_lru(BUF_CACHE_T *bp, BUF_CACHE_T *list)
{
bp->prev->next = bp->next;
bp->next->prev = bp->prev;
push_to_lru(bp, list);
} /* end of buf_cache_move_to_lru */
/* end of exfat_cache.c */