Skip to content

Commit 2cbe7f3

Browse files
committed
'30functiondone'
1 parent ff7269f commit 2cbe7f3

File tree

1 file changed

+89
-2
lines changed

1 file changed

+89
-2
lines changed

fs/hmfs/gc.c

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@ static void move_xdata_block(struct hmfs_sb_info *sbi, seg_t src_segno,
527527
}
528528
/*
529529
*cc10 move_node_block:移动node块信息
530+
*@sbi:指向超级块信息的指针实例
530531
*@src_segno:源段号类型
531532
*@src_sum:源summary信息
532533
*/
@@ -589,6 +590,7 @@ static void move_node_block(struct hmfs_sb_info *sbi, seg_t src_segno,
589590
}
590591
/*
591592
*cc11 move_nat_block:移动NAT块
593+
*@sbi:指向超级块信息的指针实例
592594
*@src_segno:源段号类型
593595
*@src_sum:源summary信息
594596
*/
@@ -659,6 +661,7 @@ static void move_nat_block(struct hmfs_sb_info *sbi, seg_t src_segno, int src_of
659661
/* Orphan blocks is not shared */
660662
/*
661663
*cc12 move_orphan_block:移动孤立的块
664+
*@sbi:指向超级块信息的指针实例
662665
*@src_segno:源段号类型
663666
*@src_sum:源summary信息
664667
*/
@@ -680,7 +683,12 @@ static void move_orphan_block(struct hmfs_sb_info *sbi, seg_t src_segno,
680683

681684
update_dest_summary(src_sum, args.dest_sum);
682685
}
683-
686+
/*
687+
*cc13 move_checkpoint_block:迁移检查点块
688+
*@sbi:指向超级块信息的指针实例
689+
*@src_segno:源段号类型
690+
*@src_sum:源summary信息
691+
*/
684692
static void move_checkpoint_block(struct hmfs_sb_info *sbi, seg_t src_segno,
685693
int src_off, struct hmfs_summary *src_sum)
686694
{
@@ -693,18 +701,30 @@ static void move_checkpoint_block(struct hmfs_sb_info *sbi, seg_t src_segno,
693701

694702
prepare_move_argument(&args, sbi, src_segno, src_off, src_sum,
695703
TYPE_NODE);
704+
/*
705+
*获取当前检查点信息
706+
*/
696707

697708
cp_i = get_checkpoint_info(sbi, args.start_version, false);
698709
hmfs_bug_on(sbi, !cp_i);
699710

700711
this_cp = HMFS_CHECKPOINT(args.src);
712+
/*
713+
* 分别获取前一个和下一个检查点的地址,并用指针指向她们
714+
*/
701715
next_cp = ADDR(sbi, le64_to_cpu(this_cp->next_cp_addr));
702716
prev_cp = ADDR(sbi, le64_to_cpu(this_cp->prev_cp_addr));
703717

718+
/*
719+
*将前后检查点的地址信息放入目标地址中
720+
*/
704721
hmfs_memcpy_atomic(&next_cp->prev_cp_addr, &args.dest_addr, 8);
705722
hmfs_memcpy_atomic(&prev_cp->next_cp_addr, &args.dest_addr, 8);
706723
cp_i->cp = HMFS_CHECKPOINT(args.dest);
707724

725+
/*
726+
*遍历要在两个孤立块中写的地址,同时对孤立地址也写到目的地址中
727+
*/
708728
for (i = 0; i < NUM_ORPHAN_BLOCKS; i++) {
709729
orphan_addr = le64_to_cpu(this_cp->orphan_addrs[i]);
710730
if (orphan_addr == NULL_ADDR)
@@ -716,20 +736,35 @@ static void move_checkpoint_block(struct hmfs_sb_info *sbi, seg_t src_segno,
716736
update_dest_summary(src_sum, args.dest_sum);
717737
}
718738

739+
/*
740+
*cc14 garbage_collect:进行垃圾收集
741+
*@sbi:指向超级块信息的指针实例
742+
*@src_segno:源段号类型
743+
*@segno:段号的类型
744+
*/
719745
static void garbage_collect(struct hmfs_sb_info *sbi, seg_t segno)
720746
{
721747
int off = 0;
722748
struct hmfs_cm_info *cm_i = CM_I(sbi);
723749
bool is_current, none_valid;
724750
nid_t nid;
751+
/*
752+
*定义summary的块的实例
753+
*/
725754
struct hmfs_summary_block *sum_blk;
726755
struct hmfs_summary *sum;
727756

757+
/*
758+
* 判断当前段中是否有有效块
759+
*/
728760
none_valid = !get_seg_entry(sbi, segno)->valid_blocks;
729761

730762
if (none_valid)
731763
goto recycle;
732764

765+
/*
766+
* 返回summary入口地址
767+
*/
733768
sum_blk = get_summary_block(sbi, segno);
734769
sum = sum_blk->entries;
735770

@@ -742,6 +777,9 @@ static void garbage_collect(struct hmfs_sb_info *sbi, seg_t segno)
742777
* - invalid blocks in older version
743778
* - newest blocks in newest version(checkpoint is not written)
744779
*/
780+
/*
781+
* 如果是旧版本的无效块,或者最新版本中最新的块(即检查点还未完成则不清理)
782+
*/
745783
if (!get_summary_valid_bit(sum) && !is_current)
746784
continue;
747785

@@ -752,6 +790,9 @@ static void garbage_collect(struct hmfs_sb_info *sbi, seg_t segno)
752790
}
753791
}
754792

793+
/*
794+
*根据summaryr入口地址的信息获取summary表的类型,并且在清理段之前作相应的转移工作,比如迁移有效的块和数据
795+
*/
755796
hmfs_bug_on(sbi, get_summary_valid_bit(sum) && is_current);
756797
switch (get_summary_type(sum)) {
757798
case SUM_TYPE_DATA:
@@ -762,18 +803,30 @@ static void garbage_collect(struct hmfs_sb_info *sbi, seg_t segno)
762803
break;
763804
case SUM_TYPE_INODE:
764805
case SUM_TYPE_DN:
806+
/*
807+
*如果是不直接的块进行节点迁移处理
808+
*/
765809
case SUM_TYPE_IDN:
766810
move_node_block(sbi, segno, off, sum);
767811
break;
768812
case SUM_TYPE_NATN:
813+
/*
814+
* 如果是NAT的数据块,也进行相应移动NAT块的处理
815+
*/
769816
case SUM_TYPE_NATD:
770817
hmfs_bug_on(sbi, is_current);
771818
move_nat_block(sbi, segno, off, sum);
772819
continue;
820+
/*
821+
*处理迁移孤立的块
822+
*/
773823
case SUM_TYPE_ORPHAN:
774824
hmfs_bug_on(sbi, is_current);
775825
move_orphan_block(sbi, segno, off, sum);
776826
continue;
827+
/*
828+
* 处理进行检查点迁移的块
829+
*/
777830
case SUM_TYPE_CP:
778831
hmfs_bug_on(sbi, is_current);
779832
move_checkpoint_block(sbi, segno, off, sum);
@@ -785,9 +838,16 @@ static void garbage_collect(struct hmfs_sb_info *sbi, seg_t segno)
785838
}
786839

787840
recycle:
841+
/*
842+
*回收无效的块
843+
*/
788844
recycle_segment(sbi, segno, none_valid);
789845
}
790-
846+
/*
847+
*hmfs_gc:
848+
*@sbi:指向超级块信息的指针实例
849+
*@gc_type:垃圾回收的类型
850+
*/
791851
int hmfs_gc(struct hmfs_sb_info *sbi, int gc_type)
792852
{
793853
int ret = -1;
@@ -796,16 +856,25 @@ int hmfs_gc(struct hmfs_sb_info *sbi, int gc_type)
796856
bool do_cp = false;
797857
int total_segs = TOTAL_SEGS(sbi);
798858
int time_retry = 0;
859+
/*
860+
* 定义最大的要尝试回收的段数
861+
*/
799862
int max_retry = (total_segs + MAX_SEG_SEARCH - 1) / MAX_SEG_SEARCH;
800863

801864
hmfs_dbg("Enter GC\n");
802865
INC_GC_TRY(STAT_I(sbi));
803866
if (!(sbi->sb->s_flags & MS_ACTIVE))
804867
goto out;
805868

869+
/*
870+
*设置文件系统收集的类型
871+
*/
806872
if (hmfs_cp->state == HMFS_NONE)
807873
set_fs_state(hmfs_cp, HMFS_GC);
808874

875+
/*
876+
* 处理垃圾收集的类型是BG或者没有足够的空闲的段
877+
*/
809878
if (gc_type == BG_GC && has_not_enough_free_segs(sbi)) {
810879
gc_type = FG_GC;
811880
}
@@ -814,6 +883,9 @@ int hmfs_gc(struct hmfs_sb_info *sbi, int gc_type)
814883
hmfs_dbg("Before get victim:%ld %ld %ld\n", (unsigned long)total_valid_blocks(sbi),
815884
(unsigned long)CM_I(sbi)->alloc_block_count,
816885
(unsigned long)CM_I(sbi)->valid_block_count);
886+
/*
887+
* 获取段里面victim的块
888+
*/
817889
if (!get_victim(sbi, &segno, gc_type))
818890
goto out;
819891
ret = 0;
@@ -826,13 +898,19 @@ int hmfs_gc(struct hmfs_sb_info *sbi, int gc_type)
826898
* need to set it as PREFREE. And we could reuse it right now, which
827899
* could improve GC efficiency
828900
*/
901+
/*
902+
*根据段的入口块获取有效的块数,垃圾收集时的日志域加1,已经收集的段数也加1
903+
*/
829904
if (get_seg_entry(sbi, segno)->valid_blocks) {
830905
hmfs_memcpy_atomic(sbi->gc_logs, &segno, 4);
831906
sbi->gc_logs++;
832907
sbi->nr_gc_segs++;
833908
hmfs_memcpy_atomic(&hmfs_cp->nr_gc_segs, &sbi->nr_gc_segs, 4);
834909
}
835910

911+
/*
912+
*统计当前超级块实例下每段中要进行垃圾收集的块数,并且收集它们
913+
*/
836914
COUNT_GC_BLOCKS(STAT_I(sbi), HMFS_PAGE_PER_SEG -
837915
get_valid_blocks(sbi, segno));
838916

@@ -848,6 +926,9 @@ int hmfs_gc(struct hmfs_sb_info *sbi, int gc_type)
848926
start_segno = segno;
849927

850928
/* If space is limited, we might need to scan the whole NVM */
929+
/*
930+
* 如果空间有限,则重新扫描整个NVM
931+
*/
851932
if (need_deep_scan(sbi)) {
852933
do_cp = true;
853934
time_retry++;
@@ -857,10 +938,16 @@ int hmfs_gc(struct hmfs_sb_info *sbi, int gc_type)
857938
}
858939

859940
/* In FG_GC, we atmost scan sbi->nr_max_fg_segs segments */
941+
/*
942+
*在FG类型的垃圾收集总,最多扫描当前环境下一定数量的段
943+
*/
860944
if (has_not_enough_free_segs(sbi) && need_more_scan(sbi, segno, start_segno))
861945
goto gc_more;
862946

863947
out:
948+
/*
949+
*处理victim的段,进行检查点信息的设置
950+
*/
864951
if (do_cp) {
865952
ret= write_checkpoint(sbi, true);
866953
hmfs_bug_on(sbi, ret);

0 commit comments

Comments
 (0)