This repository has been archived by the owner on Apr 15, 2020. It is now read-only.
forked from cryptomilk/kernel-sdfat
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dfr.c
1372 lines (1164 loc) · 33.6 KB
/
dfr.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) 2012-2013 Samsung Electronics Co., Ltd.
*
* This program 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; either version 2
* of the License, or (at your option) any later version.
*
* This program 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 this program; if not, see <http://www.gnu.org/licenses/>.
*/
/************************************************************************/
/* */
/* @PROJECT : exFAT & FAT12/16/32 File System */
/* @FILE : dfr.c */
/* @PURPOSE : Defragmentation support for SDFAT32 */
/* */
/*----------------------------------------------------------------------*/
/* NOTES */
/* */
/* */
/************************************************************************/
#include <linux/version.h>
#include <linux/list.h>
#include <linux/blkdev.h>
#include "sdfat.h"
#include "core.h"
#include "amap_smart.h"
#ifdef CONFIG_SDFAT_DFR
/**
* @fn defrag_get_info
* @brief get HW params for defrag daemon
* @return 0 on success, -errno otherwise
* @param sb super block
* @param arg defrag info arguments
* @remark protected by super_block
*/
int
defrag_get_info(
IN struct super_block *sb,
OUT struct defrag_info_arg *arg)
{
FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
AMAP_T *amap = SDFAT_SB(sb)->fsi.amap;
if (!arg)
return -EINVAL;
arg->sec_sz = sb->s_blocksize;
arg->clus_sz = fsi->cluster_size;
arg->total_sec = fsi->num_sectors;
arg->fat_offset_sec = fsi->FAT1_start_sector;
arg->fat_sz_sec = fsi->num_FAT_sectors;
arg->n_fat = (fsi->FAT1_start_sector == fsi->FAT2_start_sector) ? 1 : 2;
arg->sec_per_au = amap->option.au_size;
arg->hidden_sectors = amap->option.au_align_factor % amap->option.au_size;
return 0;
}
static int
__defrag_scan_dir(
IN struct super_block *sb,
IN DOS_DENTRY_T *dos_ep,
IN loff_t i_pos,
OUT struct defrag_trav_arg *arg)
{
FS_INFO_T *fsi = NULL;
UNI_NAME_T uniname;
unsigned int type = 0, start_clus = 0;
int err = -EPERM;
/* Check params */
ERR_HANDLE2((!sb || !dos_ep || !i_pos || !arg), err, -EINVAL);
fsi = &(SDFAT_SB(sb)->fsi);
/* Get given entry's type */
type = fsi->fs_func->get_entry_type((DENTRY_T *) dos_ep);
/* Check dos_ep */
if (!strncmp(dos_ep->name, DOS_CUR_DIR_NAME, DOS_NAME_LENGTH)) {
;
} else if (!strncmp(dos_ep->name, DOS_PAR_DIR_NAME, DOS_NAME_LENGTH)) {
;
} else if ((type == TYPE_DIR) || (type == TYPE_FILE)) {
/* Set start_clus */
SET32_HI(start_clus, le16_to_cpu(dos_ep->start_clu_hi));
SET32_LO(start_clus, le16_to_cpu(dos_ep->start_clu_lo));
arg->start_clus = start_clus;
/* Set type & i_pos */
if (type == TYPE_DIR)
arg->type = DFR_TRAV_TYPE_DIR;
else
arg->type = DFR_TRAV_TYPE_FILE;
arg->i_pos = i_pos;
/* Set name */
memset(&uniname, 0, sizeof(UNI_NAME_T));
get_uniname_from_dos_entry(sb, dos_ep, &uniname, 0x1);
/* FIXME :
* we should think that whether the size of arg->name
* is enough or not
*/
nls_uni16s_to_vfsname(sb, &uniname,
arg->name, sizeof(arg->name));
err = 0;
/* End case */
} else if (type == TYPE_UNUSED) {
err = -ENOENT;
} else {
;
}
error:
return err;
}
/**
* @fn defrag_scan_dir
* @brief scan given directory
* @return 0 on success, -errno otherwise
* @param sb super block
* @param args traverse args
* @remark protected by inode_lock, super_block and volume lock
*/
int
defrag_scan_dir(
IN struct super_block *sb,
INOUT struct defrag_trav_arg *args)
{
struct sdfat_sb_info *sbi = NULL;
FS_INFO_T *fsi = NULL;
struct defrag_trav_header *header = NULL;
DOS_DENTRY_T *dos_ep;
CHAIN_T chain;
int dot_found = 0, args_idx = DFR_TRAV_HEADER_IDX + 1, clus = 0, index = 0;
int err = 0, j = 0;
/* Check params */
ERR_HANDLE2((!sb || !args), err, -EINVAL);
sbi = SDFAT_SB(sb);
fsi = &(sbi->fsi);
header = (struct defrag_trav_header *) args;
/* Exceptional case for ROOT */
if (header->i_pos == DFR_TRAV_ROOT_IPOS) {
header->start_clus = fsi->root_dir;
dfr_debug("IOC_DFR_TRAV for ROOT: start_clus %08x", header->start_clus);
dot_found = 1;
}
chain.dir = header->start_clus;
chain.size = 0;
chain.flags = 0;
/* Check if this is directory */
if (!dot_found) {
FAT32_CHECK_CLUSTER(fsi, chain.dir, err);
ERR_HANDLE(err);
dos_ep = (DOS_DENTRY_T *) get_dentry_in_dir(sb, &chain, 0, NULL);
ERR_HANDLE2(!dos_ep, err, -EIO);
if (strncmp(dos_ep->name, DOS_CUR_DIR_NAME, DOS_NAME_LENGTH)) {
err = -EINVAL;
dfr_err("Scan: Not a directory, err %d", err);
goto error;
}
}
/* For more-scan case */
if ((header->stat == DFR_TRAV_STAT_MORE) &&
(header->start_clus == sbi->dfr_hint_clus) &&
(sbi->dfr_hint_idx > 0)) {
index = sbi->dfr_hint_idx;
for (j = 0; j < (sbi->dfr_hint_idx / fsi->dentries_per_clu); j++) {
/* Follow FAT-chain */
FAT32_CHECK_CLUSTER(fsi, chain.dir, err);
ERR_HANDLE(err);
err = fat_ent_get(sb, chain.dir, &(chain.dir));
ERR_HANDLE(err);
if (!IS_CLUS_EOF(chain.dir)) {
clus++;
index -= fsi->dentries_per_clu;
} else {
/**
* This directory modified. Stop scanning.
*/
err = -EINVAL;
dfr_err("Scan: SCAN_MORE failed, err %d", err);
goto error;
}
}
/* For first-scan case */
} else {
clus = 0;
index = 0;
}
scan_fat_chain:
/* Scan given directory and get info of children */
for ( ; index < fsi->dentries_per_clu; index++) {
DOS_DENTRY_T *dos_ep = NULL;
loff_t i_pos = 0;
/* Get dos_ep */
FAT32_CHECK_CLUSTER(fsi, chain.dir, err);
ERR_HANDLE(err);
dos_ep = (DOS_DENTRY_T *) get_dentry_in_dir(sb, &chain, index, NULL);
ERR_HANDLE2(!dos_ep, err, -EIO);
/* Make i_pos for this entry */
SET64_HI(i_pos, header->start_clus);
SET64_LO(i_pos, clus * fsi->dentries_per_clu + index);
err = __defrag_scan_dir(sb, dos_ep, i_pos, &args[args_idx]);
if (!err) {
/* More-scan case */
if (++args_idx >= (PAGE_SIZE / sizeof(struct defrag_trav_arg))) {
sbi->dfr_hint_clus = header->start_clus;
sbi->dfr_hint_idx = clus * fsi->dentries_per_clu + index + 1;
header->stat = DFR_TRAV_STAT_MORE;
header->nr_entries = args_idx;
goto error;
}
/* Error case */
} else if (err == -EINVAL) {
sbi->dfr_hint_clus = sbi->dfr_hint_idx = 0;
dfr_err("Scan: err %d", err);
goto error;
/* End case */
} else if (err == -ENOENT) {
sbi->dfr_hint_clus = sbi->dfr_hint_idx = 0;
err = 0;
goto done;
} else {
/* DO NOTHING */
}
err = 0;
}
/* Follow FAT-chain */
FAT32_CHECK_CLUSTER(fsi, chain.dir, err);
ERR_HANDLE(err);
err = fat_ent_get(sb, chain.dir, &(chain.dir));
ERR_HANDLE(err);
if (!IS_CLUS_EOF(chain.dir)) {
index = 0;
clus++;
goto scan_fat_chain;
}
done:
/* Update header */
header->stat = DFR_TRAV_STAT_DONE;
header->nr_entries = args_idx;
error:
return err;
}
static int
__defrag_validate_cluster_prev(
IN struct super_block *sb,
IN struct defrag_chunk_info *chunk)
{
FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
CHAIN_T dir;
DENTRY_T *ep = NULL;
unsigned int entry = 0, clus = 0;
int err = 0;
if (chunk->prev_clus == 0) {
/* For the first cluster of a file */
dir.dir = GET64_HI(chunk->i_pos);
dir.flags = 0x1; // Assume non-continuous
entry = GET64_LO(chunk->i_pos);
FAT32_CHECK_CLUSTER(fsi, dir.dir, err);
ERR_HANDLE(err);
ep = get_dentry_in_dir(sb, &dir, entry, NULL);
if (!ep) {
err = -EPERM;
goto error;
}
/* should call fat_get_entry_clu0(ep) */
clus = fsi->fs_func->get_entry_clu0(ep);
if (clus != chunk->d_clus) {
err = -ENXIO;
goto error;
}
} else {
/* Normal case */
FAT32_CHECK_CLUSTER(fsi, chunk->prev_clus, err);
ERR_HANDLE(err);
err = fat_ent_get(sb, chunk->prev_clus, &clus);
if (err)
goto error;
if (chunk->d_clus != clus)
err = -ENXIO;
}
error:
return err;
}
static int
__defrag_validate_cluster_next(
IN struct super_block *sb,
IN struct defrag_chunk_info *chunk)
{
FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
unsigned int clus = 0;
int err = 0;
/* Check next_clus */
FAT32_CHECK_CLUSTER(fsi, (chunk->d_clus + chunk->nr_clus - 1), err);
ERR_HANDLE(err);
err = fat_ent_get(sb, (chunk->d_clus + chunk->nr_clus - 1), &clus);
if (err)
goto error;
if (chunk->next_clus != (clus & FAT32_EOF))
err = -ENXIO;
error:
return err;
}
/**
* @fn __defrag_check_au
* @brief check if this AU is in use
* @return 0 if idle, 1 if busy
* @param sb super block
* @param clus physical cluster num
* @param limit # of used clusters from daemon
*/
static int
__defrag_check_au(
struct super_block *sb,
u32 clus,
u32 limit)
{
unsigned int nr_free = amap_get_freeclus(sb, clus);
#if defined(CONFIG_SDFAT_DFR_DEBUG) && defined(CONFIG_SDFAT_DBG_MSG)
if (nr_free < limit) {
AMAP_T *amap = SDFAT_SB(sb)->fsi.amap;
AU_INFO_T *au = GET_AU(amap, i_AU_of_CLU(amap, clus));
dfr_debug("AU[%d] nr_free %d, limit %d", au->idx, nr_free, limit);
}
#endif
return ((nr_free < limit) ? 1 : 0);
}
/**
* @fn defrag_validate_cluster
* @brief validate cluster info of given chunk
* @return 0 on success, -errno otherwise
* @param inode inode of given chunk
* @param chunk given chunk
* @param skip_prev flag to skip checking previous cluster info
* @remark protected by super_block and volume lock
*/
int
defrag_validate_cluster(
IN struct inode *inode,
IN struct defrag_chunk_info *chunk,
IN int skip_prev)
{
struct super_block *sb = inode->i_sb;
FILE_ID_T *fid = &(SDFAT_I(inode)->fid);
unsigned int clus = 0;
int err = 0, i = 0;
/* If this inode is unlink-ed, skip it */
if (fid->dir.dir == DIR_DELETED)
return -ENOENT;
/* Skip working-AU */
err = amap_check_working(sb, chunk->d_clus);
if (err)
return -EBUSY;
/* Check # of free_clus of belonged AU */
err = __defrag_check_au(inode->i_sb, chunk->d_clus, CLUS_PER_AU(sb) - chunk->au_clus);
if (err)
return -EINVAL;
/* Check chunk's clusters */
for (i = 0; i < chunk->nr_clus; i++) {
err = fsapi_map_clus(inode, chunk->f_clus + i, &clus, ALLOC_NOWHERE);
if (err || (chunk->d_clus + i != clus)) {
if (!err)
err = -ENXIO;
goto error;
}
}
/* Check next_clus */
err = __defrag_validate_cluster_next(sb, chunk);
ERR_HANDLE(err);
if (!skip_prev) {
/* Check prev_clus */
err = __defrag_validate_cluster_prev(sb, chunk);
ERR_HANDLE(err);
}
error:
return err;
}
/**
* @fn defrag_reserve_clusters
* @brief reserve clusters for defrag
* @return 0 on success, -errno otherwise
* @param sb super block
* @param nr_clus # of clusters to reserve
* @remark protected by super_block and volume lock
*/
int
defrag_reserve_clusters(
INOUT struct super_block *sb,
IN int nr_clus)
{
struct sdfat_sb_info *sbi = SDFAT_SB(sb);
FS_INFO_T *fsi = &(sbi->fsi);
if (!(sbi->options.improved_allocation & SDFAT_ALLOC_DELAY))
/* Nothing to do */
return 0;
/* Check error case */
if (fsi->used_clusters + fsi->reserved_clusters + nr_clus >= fsi->num_clusters - 2) {
return -ENOSPC;
} else if (fsi->reserved_clusters + nr_clus < 0) {
dfr_err("Reserve count: reserved_clusters %d, nr_clus %d",
fsi->reserved_clusters, nr_clus);
BUG_ON(fsi->reserved_clusters + nr_clus < 0);
}
sbi->dfr_reserved_clus += nr_clus;
fsi->reserved_clusters += nr_clus;
return 0;
}
/**
* @fn defrag_mark_ignore
* @brief mark corresponding AU to be ignored
* @return 0 on success, -errno otherwise
* @param sb super block
* @param clus given cluster num
* @remark protected by super_block
*/
int
defrag_mark_ignore(
INOUT struct super_block *sb,
IN unsigned int clus)
{
int err = 0;
if (SDFAT_SB(sb)->options.improved_allocation & SDFAT_ALLOC_SMART)
err = amap_mark_ignore(sb, clus);
if (err)
dfr_debug("err %d", err);
return err;
}
/**
* @fn defrag_unmark_ignore_all
* @brief unmark all ignored AUs
* @return void
* @param sb super block
* @remark protected by super_block
*/
void
defrag_unmark_ignore_all(struct super_block *sb)
{
if (SDFAT_SB(sb)->options.improved_allocation & SDFAT_ALLOC_SMART)
amap_unmark_ignore_all(sb);
}
/**
* @fn defrag_map_cluster
* @brief get_block function for defrag dests
* @return 0 on success, -errno otherwise
* @param inode inode
* @param clu_offset logical cluster offset
* @param clu mapped cluster (physical)
* @remark protected by super_block and volume lock
*/
int
defrag_map_cluster(
struct inode *inode,
unsigned int clu_offset,
unsigned int *clu)
{
struct super_block *sb = inode->i_sb;
struct sdfat_sb_info *sbi = SDFAT_SB(sb);
FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
#ifdef CONFIG_SDFAT_DFR_PACKING
AMAP_T *amap = SDFAT_SB(sb)->fsi.amap;
#endif
FILE_ID_T *fid = &(SDFAT_I(inode)->fid);
struct defrag_info *ino_dfr = &(SDFAT_I(inode)->dfr_info);
struct defrag_chunk_info *chunk = NULL;
CHAIN_T new_clu;
int i = 0, nr_new = 0, err = 0;
/* Get corresponding chunk */
for (i = 0; i < ino_dfr->nr_chunks; i++) {
chunk = &(ino_dfr->chunks[i]);
if ((chunk->f_clus <= clu_offset) && (clu_offset < chunk->f_clus + chunk->nr_clus)) {
/* For already allocated new_clus */
if (sbi->dfr_new_clus[chunk->new_idx + clu_offset - chunk->f_clus]) {
*clu = sbi->dfr_new_clus[chunk->new_idx + clu_offset - chunk->f_clus];
return 0;
}
break;
}
}
BUG_ON(!chunk);
fscore_set_vol_flags(sb, VOL_DIRTY, 0);
new_clu.dir = CLUS_EOF;
new_clu.size = 0;
new_clu.flags = fid->flags;
/* Allocate new cluster */
#ifdef CONFIG_SDFAT_DFR_PACKING
if (amap->n_clean_au * DFR_FULL_RATIO <= amap->n_au * DFR_DEFAULT_PACKING_RATIO)
err = fsi->fs_func->alloc_cluster(sb, 1, &new_clu, ALLOC_COLD_PACKING);
else
err = fsi->fs_func->alloc_cluster(sb, 1, &new_clu, ALLOC_COLD_ALIGNED);
#else
err = fsi->fs_func->alloc_cluster(sb, 1, &new_clu, ALLOC_COLD_ALIGNED);
#endif
if (err) {
dfr_err("Map: 1 %d", 0);
return err;
}
/* Decrease reserved cluster count */
defrag_reserve_clusters(sb, -1);
/* Add new_clus info in ino_dfr */
sbi->dfr_new_clus[chunk->new_idx + clu_offset - chunk->f_clus] = new_clu.dir;
/* Make FAT-chain for new_clus */
for (i = 0; i < chunk->nr_clus; i++) {
#if 0
if (sbi->dfr_new_clus[chunk->new_idx + i])
nr_new++;
else
break;
#else
if (!sbi->dfr_new_clus[chunk->new_idx + i])
break;
nr_new++;
#endif
}
if (nr_new == chunk->nr_clus) {
for (i = 0; i < chunk->nr_clus - 1; i++) {
FAT32_CHECK_CLUSTER(fsi, sbi->dfr_new_clus[chunk->new_idx + i], err);
BUG_ON(err);
if (fat_ent_set(sb,
sbi->dfr_new_clus[chunk->new_idx + i],
sbi->dfr_new_clus[chunk->new_idx + i + 1]))
return -EIO;
}
}
*clu = new_clu.dir;
return 0;
}
/**
* @fn defrag_writepage_end_io
* @brief check WB status of requested page
* @return void
* @param page page
*/
void
defrag_writepage_end_io(
INOUT struct page *page)
{
struct super_block *sb = page->mapping->host->i_sb;
struct sdfat_sb_info *sbi = SDFAT_SB(sb);
struct defrag_info *ino_dfr = &(SDFAT_I(page->mapping->host)->dfr_info);
unsigned int clus_start = 0, clus_end = 0;
int i = 0;
/* Check if this inode is on defrag */
if (atomic_read(&ino_dfr->stat) != DFR_INO_STAT_REQ)
return;
clus_start = page->index / PAGES_PER_CLUS(sb);
clus_end = clus_start + 1;
/* Check each chunk in given inode */
for (i = 0; i < ino_dfr->nr_chunks; i++) {
struct defrag_chunk_info *chunk = &(ino_dfr->chunks[i]);
unsigned int chunk_start = 0, chunk_end = 0;
chunk_start = chunk->f_clus;
chunk_end = chunk->f_clus + chunk->nr_clus;
if ((clus_start >= chunk_start) && (clus_end <= chunk_end)) {
int off = clus_start - chunk_start;
clear_bit((page->index & (PAGES_PER_CLUS(sb) - 1)),
(volatile unsigned long *)&(sbi->dfr_page_wb[chunk->new_idx + off]));
}
}
}
/**
* @fn __defrag_check_wb
* @brief check if WB for given chunk completed
* @return 0 on success, -errno otherwise
* @param sbi super block info
* @param chunk given chunk
*/
static int
__defrag_check_wb(
IN struct sdfat_sb_info *sbi,
IN struct defrag_chunk_info *chunk)
{
int err = 0, wb_i = 0, i = 0, nr_new = 0;
if (!sbi || !chunk)
return -EINVAL;
/* Check WB complete status first */
for (wb_i = 0; wb_i < chunk->nr_clus; wb_i++) {
if (atomic_read((atomic_t *)&(sbi->dfr_page_wb[chunk->new_idx + wb_i]))) {
err = -EBUSY;
break;
}
}
/**
* Check NEW_CLUS status.
* writepage_end_io cannot check whole WB complete status,
* so we need to check NEW_CLUS status.
*/
for (i = 0; i < chunk->nr_clus; i++)
if (sbi->dfr_new_clus[chunk->new_idx + i])
nr_new++;
if (nr_new == chunk->nr_clus) {
err = 0;
if ((wb_i != chunk->nr_clus) && (wb_i != chunk->nr_clus - 1))
dfr_debug("submit_fullpage_bio() called on a page (nr_clus %d, wb_i %d)",
chunk->nr_clus, wb_i);
BUG_ON(nr_new > chunk->nr_clus);
} else {
dfr_debug("nr_new %d, nr_clus %d", nr_new, chunk->nr_clus);
err = -EBUSY;
}
/* Update chunk's state */
if (!err)
chunk->stat |= DFR_CHUNK_STAT_WB;
return err;
}
static void
__defrag_check_fat_old(
IN struct super_block *sb,
IN struct inode *inode,
IN struct defrag_chunk_info *chunk)
{
FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
unsigned int clus = 0;
int err = 0, idx = 0, max_idx = 0;
/* Get start_clus */
clus = SDFAT_I(inode)->fid.start_clu;
/* Follow FAT-chain */
#define num_clusters(val) ((val) ? (s32)((val - 1) >> fsi->cluster_size_bits) + 1 : 0)
max_idx = num_clusters(SDFAT_I(inode)->i_size_ondisk);
for (idx = 0; idx < max_idx; idx++) {
FAT32_CHECK_CLUSTER(fsi, clus, err);
ERR_HANDLE(err);
err = fat_ent_get(sb, clus, &clus);
ERR_HANDLE(err);
if ((idx < max_idx - 1) && (IS_CLUS_EOF(clus) || IS_CLUS_FREE(clus))) {
dfr_err("FAT: inode %p, max_idx %d, idx %d, clus %08x, "
"f_clus %d, nr_clus %d", inode, max_idx,
idx, clus, chunk->f_clus, chunk->nr_clus);
BUG_ON(idx < max_idx - 1);
goto error;
}
}
error:
return;
}
static void
__defrag_check_fat_new(
IN struct super_block *sb,
IN struct inode *inode,
IN struct defrag_chunk_info *chunk)
{
struct sdfat_sb_info *sbi = SDFAT_SB(sb);
FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
unsigned int clus = 0;
int i = 0, err = 0;
/* Check start of FAT-chain */
if (chunk->prev_clus) {
FAT32_CHECK_CLUSTER(fsi, chunk->prev_clus, err);
BUG_ON(err);
err = fat_ent_get(sb, chunk->prev_clus, &clus);
BUG_ON(err);
} else {
clus = SDFAT_I(inode)->fid.start_clu;
}
if (sbi->dfr_new_clus[chunk->new_idx] != clus) {
dfr_err("FAT: inode %p, start_clus %08x, read_clus %08x",
inode, sbi->dfr_new_clus[chunk->new_idx], clus);
err = EIO;
goto error;
}
/* Check inside of FAT-chain */
if (chunk->nr_clus > 1) {
for (i = 0; i < chunk->nr_clus - 1; i++) {
FAT32_CHECK_CLUSTER(fsi, sbi->dfr_new_clus[chunk->new_idx + i], err);
BUG_ON(err);
err = fat_ent_get(sb, sbi->dfr_new_clus[chunk->new_idx + i], &clus);
BUG_ON(err);
if (sbi->dfr_new_clus[chunk->new_idx + i + 1] != clus) {
dfr_err("FAT: inode %p, new_clus %08x, read_clus %08x",
inode, sbi->dfr_new_clus[chunk->new_idx], clus);
err = EIO;
goto error;
}
}
clus = 0;
}
/* Check end of FAT-chain */
FAT32_CHECK_CLUSTER(fsi, sbi->dfr_new_clus[chunk->new_idx + chunk->nr_clus - 1], err);
BUG_ON(err);
err = fat_ent_get(sb, sbi->dfr_new_clus[chunk->new_idx + chunk->nr_clus - 1], &clus);
BUG_ON(err);
if ((chunk->next_clus & 0x0FFFFFFF) != (clus & 0x0FFFFFFF)) {
dfr_err("FAT: inode %p, next_clus %08x, read_clus %08x", inode, chunk->next_clus, clus);
err = EIO;
}
error:
BUG_ON(err);
}
/**
* @fn __defrag_update_dirent
* @brief update DIR entry for defrag req
* @return void
* @param sb super block
* @param chunk given chunk
*/
static void
__defrag_update_dirent(
struct super_block *sb,
struct defrag_chunk_info *chunk)
{
struct sdfat_sb_info *sbi = SDFAT_SB(sb);
FS_INFO_T *fsi = &SDFAT_SB(sb)->fsi;
CHAIN_T dir;
DOS_DENTRY_T *dos_ep;
unsigned int entry = 0;
unsigned long long sector = 0;
unsigned short hi = 0, lo = 0;
int err = 0;
dir.dir = GET64_HI(chunk->i_pos);
dir.flags = 0x1; // Assume non-continuous
entry = GET64_LO(chunk->i_pos);
FAT32_CHECK_CLUSTER(fsi, dir.dir, err);
BUG_ON(err);
dos_ep = (DOS_DENTRY_T *) get_dentry_in_dir(sb, &dir, entry, §or);
hi = GET32_HI(sbi->dfr_new_clus[chunk->new_idx]);
lo = GET32_LO(sbi->dfr_new_clus[chunk->new_idx]);
dos_ep->start_clu_hi = cpu_to_le16(hi);
dos_ep->start_clu_lo = cpu_to_le16(lo);
dcache_modify(sb, sector);
}
/**
* @fn defrag_update_fat_prev
* @brief update FAT chain for defrag requests
* @return void
* @param sb super block
* @param force flag to force FAT update
* @remark protected by super_block and volume lock
*/
void
defrag_update_fat_prev(
struct super_block *sb,
int force)
{
struct sdfat_sb_info *sbi = SDFAT_SB(sb);
FS_INFO_T *fsi = &(sbi->fsi);
struct defrag_info *sb_dfr = &sbi->dfr_info, *ino_dfr = NULL;
int skip = 0, done = 0;
/* Check if FS_ERROR occurred */
if (SDFAT_IS_SB_RDONLY(sb)) {
dfr_err("RDONLY partition (err %d)", -EPERM);
goto out;
}
list_for_each_entry(ino_dfr, &sb_dfr->entry, entry) {
struct inode *inode = &(container_of(ino_dfr, struct sdfat_inode_info, dfr_info)->vfs_inode);
struct sdfat_inode_info *ino_info = SDFAT_I(inode);
struct defrag_chunk_info *chunk_prev = NULL;
int i = 0, j = 0;
mutex_lock(&ino_dfr->lock);
BUG_ON(atomic_read(&ino_dfr->stat) != DFR_INO_STAT_REQ);
for (i = 0; i < ino_dfr->nr_chunks; i++) {
struct defrag_chunk_info *chunk = NULL;
int err = 0;
chunk = &(ino_dfr->chunks[i]);
BUG_ON(!chunk);
/* Do nothing for already passed chunk */
if (chunk->stat == DFR_CHUNK_STAT_PASS) {
done++;
continue;
}
/* Handle error case */
if (chunk->stat == DFR_CHUNK_STAT_ERR) {
err = -EINVAL;
goto error;
}
/* Double-check clusters */
if (chunk_prev &&
(chunk->f_clus == chunk_prev->f_clus + chunk_prev->nr_clus) &&
(chunk_prev->stat == DFR_CHUNK_STAT_PASS)) {
err = defrag_validate_cluster(inode, chunk, 1);
/* Handle continuous chunks in a file */
if (!err) {
chunk->prev_clus =
sbi->dfr_new_clus[chunk_prev->new_idx + chunk_prev->nr_clus - 1];
dfr_debug("prev->f_clus %d, prev->nr_clus %d, chunk->f_clus %d",
chunk_prev->f_clus, chunk_prev->nr_clus, chunk->f_clus);
}
} else {
err = defrag_validate_cluster(inode, chunk, 0);
}
if (err) {
dfr_err("Cluster validation: inode %p, chunk->f_clus %d, err %d",
inode, chunk->f_clus, err);
goto error;
}
/**
* Skip update_fat_prev if WB or update_fat_next not completed.
* Go to error case if FORCE set.
*/
if (__defrag_check_wb(sbi, chunk) || (chunk->stat != DFR_CHUNK_STAT_PREP)) {
if (force) {
err = -EPERM;
dfr_err("Skip case: inode %p, stat %x, f_clus %d, err %d",
inode, chunk->stat, chunk->f_clus, err);
goto error;
}
skip++;
continue;
}
#ifdef CONFIG_SDFAT_DFR_DEBUG
/* SPO test */
defrag_spo_test(sb, DFR_SPO_RANDOM, __func__);
#endif
/* Update chunk's previous cluster */
if (chunk->prev_clus == 0) {
/* For the first cluster of a file */
/* Update ino_info->fid.start_clu */
ino_info->fid.start_clu = sbi->dfr_new_clus[chunk->new_idx];
__defrag_update_dirent(sb, chunk);
} else {
FAT32_CHECK_CLUSTER(fsi, chunk->prev_clus, err);
BUG_ON(err);
if (fat_ent_set(sb,
chunk->prev_clus,
sbi->dfr_new_clus[chunk->new_idx])) {
err = -EIO;
goto error;
}
}
/* Clear extent cache */
extent_cache_inval_inode(inode);
/* Update FID info */
ino_info->fid.hint_bmap.off = CLUS_EOF;
ino_info->fid.hint_bmap.clu = 0;
/* Clear old FAT-chain */
for (j = 0; j < chunk->nr_clus; j++)
defrag_free_cluster(sb, chunk->d_clus + j);
/* Mark this chunk PASS */
chunk->stat = DFR_CHUNK_STAT_PASS;
__defrag_check_fat_new(sb, inode, chunk);
done++;
error:
if (err) {
/**
* chunk->new_idx != 0 means this chunk needs to be cleaned up
*/
if (chunk->new_idx) {
/* Free already allocated clusters */
for (j = 0; j < chunk->nr_clus; j++) {
if (sbi->dfr_new_clus[chunk->new_idx + j]) {
defrag_free_cluster(sb, sbi->dfr_new_clus[chunk->new_idx + j]);
sbi->dfr_new_clus[chunk->new_idx + j] = 0;
}
}
__defrag_check_fat_old(sb, inode, chunk);
}
/**
* chunk->new_idx == 0 means this chunk already cleaned up
*/
chunk->new_idx = 0;
chunk->stat = DFR_CHUNK_STAT_ERR;
}
chunk_prev = chunk;
}
BUG_ON(!mutex_is_locked(&ino_dfr->lock));