forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-08 01573e231f18eb2d99162747186f59511f56b64d
kernel/fs/f2fs/gc.c
....@@ -21,23 +21,33 @@
2121 #include "gc.h"
2222 #include <trace/events/f2fs.h>
2323
24
+static struct kmem_cache *victim_entry_slab;
25
+
26
+static unsigned int count_bits(const unsigned long *addr,
27
+ unsigned int offset, unsigned int len);
28
+
2429 static int gc_thread_func(void *data)
2530 {
2631 struct f2fs_sb_info *sbi = data;
2732 struct f2fs_gc_kthread *gc_th = sbi->gc_thread;
2833 wait_queue_head_t *wq = &sbi->gc_thread->gc_wait_queue_head;
34
+ wait_queue_head_t *fggc_wq = &sbi->gc_thread->fggc_wq;
2935 unsigned int wait_ms;
3036
3137 wait_ms = gc_th->min_sleep_time;
3238
3339 set_freezable();
3440 do {
35
- bool sync_mode;
41
+ bool sync_mode, foreground = false;
3642
3743 wait_event_interruptible_timeout(*wq,
3844 kthread_should_stop() || freezing(current) ||
45
+ waitqueue_active(fggc_wq) ||
3946 gc_th->gc_wake,
4047 msecs_to_jiffies(wait_ms));
48
+
49
+ if (test_opt(sbi, GC_MERGE) && waitqueue_active(fggc_wq))
50
+ foreground = true;
4151
4252 /* give it a try one time */
4353 if (gc_th->gc_wake)
....@@ -58,7 +68,8 @@
5868
5969 if (time_to_inject(sbi, FAULT_CHECKPOINT)) {
6070 f2fs_show_injection_info(sbi, FAULT_CHECKPOINT);
61
- f2fs_stop_checkpoint(sbi, false);
71
+ f2fs_stop_checkpoint(sbi, false,
72
+ STOP_CP_REASON_FAULT_INJECT);
6273 }
6374
6475 if (!sb_start_write_trylock(sbi->sb)) {
....@@ -79,20 +90,23 @@
7990 * invalidated soon after by user update or deletion.
8091 * So, I'd like to wait some time to collect dirty segments.
8192 */
82
- if (sbi->gc_mode == GC_URGENT) {
93
+ if (sbi->gc_mode == GC_URGENT_HIGH) {
8394 wait_ms = gc_th->urgent_sleep_time;
84
- down_write(&sbi->gc_lock);
95
+ f2fs_down_write(&sbi->gc_lock);
8596 goto do_gc;
8697 }
8798
88
- if (!down_write_trylock(&sbi->gc_lock)) {
99
+ if (foreground) {
100
+ f2fs_down_write(&sbi->gc_lock);
101
+ goto do_gc;
102
+ } else if (!f2fs_down_write_trylock(&sbi->gc_lock)) {
89103 stat_other_skip_bggc_count(sbi);
90104 goto next;
91105 }
92106
93107 if (!is_idle(sbi, GC_TIME)) {
94108 increase_sleep_time(gc_th, &wait_ms);
95
- up_write(&sbi->gc_lock);
109
+ f2fs_up_write(&sbi->gc_lock);
96110 stat_io_skip_bggc_count(sbi);
97111 goto next;
98112 }
....@@ -102,13 +116,21 @@
102116 else
103117 increase_sleep_time(gc_th, &wait_ms);
104118 do_gc:
105
- stat_inc_bggc_count(sbi->stat_info);
119
+ if (!foreground)
120
+ stat_inc_bggc_count(sbi->stat_info);
106121
107122 sync_mode = F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_SYNC;
108123
124
+ /* foreground GC was been triggered via f2fs_balance_fs() */
125
+ if (foreground)
126
+ sync_mode = false;
127
+
109128 /* if return value is not zero, no victim was selected */
110
- if (f2fs_gc(sbi, sync_mode, true, NULL_SEGNO))
129
+ if (f2fs_gc(sbi, sync_mode, !foreground, false, NULL_SEGNO))
111130 wait_ms = gc_th->no_gc_sleep_time;
131
+
132
+ if (foreground)
133
+ wake_up_all(&gc_th->fggc_wq);
112134
113135 trace_f2fs_background_gc(sbi->sb, wait_ms,
114136 prefree_segments(sbi), free_segments(sbi));
....@@ -139,15 +161,16 @@
139161 gc_th->max_sleep_time = DEF_GC_THREAD_MAX_SLEEP_TIME;
140162 gc_th->no_gc_sleep_time = DEF_GC_THREAD_NOGC_SLEEP_TIME;
141163
142
- gc_th->gc_wake= 0;
164
+ gc_th->gc_wake = 0;
143165
144166 sbi->gc_thread = gc_th;
145167 init_waitqueue_head(&sbi->gc_thread->gc_wait_queue_head);
168
+ init_waitqueue_head(&sbi->gc_thread->fggc_wq);
146169 sbi->gc_thread->f2fs_gc_task = kthread_run(gc_thread_func, sbi,
147170 "f2fs_gc-%u:%u", MAJOR(dev), MINOR(dev));
148171 if (IS_ERR(gc_th->f2fs_gc_task)) {
149172 err = PTR_ERR(gc_th->f2fs_gc_task);
150
- kvfree(gc_th);
173
+ kfree(gc_th);
151174 sbi->gc_thread = NULL;
152175 }
153176 out:
....@@ -157,26 +180,41 @@
157180 void f2fs_stop_gc_thread(struct f2fs_sb_info *sbi)
158181 {
159182 struct f2fs_gc_kthread *gc_th = sbi->gc_thread;
183
+
160184 if (!gc_th)
161185 return;
162186 kthread_stop(gc_th->f2fs_gc_task);
163
- kvfree(gc_th);
187
+ wake_up_all(&gc_th->fggc_wq);
188
+ kfree(gc_th);
164189 sbi->gc_thread = NULL;
165190 }
166191
167192 static int select_gc_type(struct f2fs_sb_info *sbi, int gc_type)
168193 {
169
- int gc_mode = (gc_type == BG_GC) ? GC_CB : GC_GREEDY;
194
+ int gc_mode;
195
+
196
+ if (gc_type == BG_GC) {
197
+ if (sbi->am.atgc_enabled)
198
+ gc_mode = GC_AT;
199
+ else
200
+ gc_mode = GC_CB;
201
+ } else {
202
+ gc_mode = GC_GREEDY;
203
+ }
170204
171205 switch (sbi->gc_mode) {
172206 case GC_IDLE_CB:
173207 gc_mode = GC_CB;
174208 break;
175209 case GC_IDLE_GREEDY:
176
- case GC_URGENT:
210
+ case GC_URGENT_HIGH:
177211 gc_mode = GC_GREEDY;
178212 break;
213
+ case GC_IDLE_AT:
214
+ gc_mode = GC_AT;
215
+ break;
179216 }
217
+
180218 return gc_mode;
181219 }
182220
....@@ -187,14 +225,25 @@
187225
188226 if (p->alloc_mode == SSR) {
189227 p->gc_mode = GC_GREEDY;
190
- p->dirty_segmap = dirty_i->dirty_segmap[type];
228
+ p->dirty_bitmap = dirty_i->dirty_segmap[type];
229
+ p->max_search = dirty_i->nr_dirty[type];
230
+ p->ofs_unit = 1;
231
+ } else if (p->alloc_mode == AT_SSR) {
232
+ p->gc_mode = GC_GREEDY;
233
+ p->dirty_bitmap = dirty_i->dirty_segmap[type];
191234 p->max_search = dirty_i->nr_dirty[type];
192235 p->ofs_unit = 1;
193236 } else {
194237 p->gc_mode = select_gc_type(sbi, gc_type);
195
- p->dirty_segmap = dirty_i->dirty_segmap[DIRTY];
196
- p->max_search = dirty_i->nr_dirty[DIRTY];
197238 p->ofs_unit = sbi->segs_per_sec;
239
+ if (__is_large_section(sbi)) {
240
+ p->dirty_bitmap = dirty_i->dirty_secmap;
241
+ p->max_search = count_bits(p->dirty_bitmap,
242
+ 0, MAIN_SECS(sbi));
243
+ } else {
244
+ p->dirty_bitmap = dirty_i->dirty_segmap[DIRTY];
245
+ p->max_search = dirty_i->nr_dirty[DIRTY];
246
+ }
198247 }
199248
200249 /*
....@@ -202,7 +251,8 @@
202251 * foreground GC and urgent GC cases.
203252 */
204253 if (gc_type != FG_GC &&
205
- (sbi->gc_mode != GC_URGENT) &&
254
+ (sbi->gc_mode != GC_URGENT_HIGH) &&
255
+ (p->gc_mode != GC_AT && p->alloc_mode != AT_SSR) &&
206256 p->max_search > sbi->max_victim_search)
207257 p->max_search = sbi->max_victim_search;
208258
....@@ -220,9 +270,15 @@
220270 /* SSR allocates in a segment unit */
221271 if (p->alloc_mode == SSR)
222272 return sbi->blocks_per_seg;
273
+ else if (p->alloc_mode == AT_SSR)
274
+ return UINT_MAX;
275
+
276
+ /* LFS */
223277 if (p->gc_mode == GC_GREEDY)
224278 return 2 * sbi->blocks_per_seg * p->ofs_unit;
225279 else if (p->gc_mode == GC_CB)
280
+ return UINT_MAX;
281
+ else if (p->gc_mode == GC_AT)
226282 return UINT_MAX;
227283 else /* No other gc_mode */
228284 return 0;
....@@ -257,13 +313,14 @@
257313 unsigned char age = 0;
258314 unsigned char u;
259315 unsigned int i;
316
+ unsigned int usable_segs_per_sec = f2fs_usable_segs_in_sec(sbi, segno);
260317
261
- for (i = 0; i < sbi->segs_per_sec; i++)
318
+ for (i = 0; i < usable_segs_per_sec; i++)
262319 mtime += get_seg_entry(sbi, start + i)->mtime;
263320 vblocks = get_valid_blocks(sbi, segno, true);
264321
265
- mtime = div_u64(mtime, sbi->segs_per_sec);
266
- vblocks = div_u64(vblocks, sbi->segs_per_sec);
322
+ mtime = div_u64(mtime, usable_segs_per_sec);
323
+ vblocks = div_u64(vblocks, usable_segs_per_sec);
267324
268325 u = (vblocks * 100) >> sbi->log_blocks_per_seg;
269326
....@@ -288,8 +345,11 @@
288345 /* alloc_mode == LFS */
289346 if (p->gc_mode == GC_GREEDY)
290347 return get_valid_blocks(sbi, segno, true);
291
- else
348
+ else if (p->gc_mode == GC_CB)
292349 return get_cb_cost(sbi, segno);
350
+
351
+ f2fs_bug_on(sbi, 1);
352
+ return 0;
293353 }
294354
295355 static unsigned int count_bits(const unsigned long *addr,
....@@ -304,6 +364,269 @@
304364 return sum;
305365 }
306366
367
+static struct victim_entry *attach_victim_entry(struct f2fs_sb_info *sbi,
368
+ unsigned long long mtime, unsigned int segno,
369
+ struct rb_node *parent, struct rb_node **p,
370
+ bool left_most)
371
+{
372
+ struct atgc_management *am = &sbi->am;
373
+ struct victim_entry *ve;
374
+
375
+ ve = f2fs_kmem_cache_alloc(victim_entry_slab, GFP_NOFS);
376
+
377
+ ve->mtime = mtime;
378
+ ve->segno = segno;
379
+
380
+ rb_link_node(&ve->rb_node, parent, p);
381
+ rb_insert_color_cached(&ve->rb_node, &am->root, left_most);
382
+
383
+ list_add_tail(&ve->list, &am->victim_list);
384
+
385
+ am->victim_count++;
386
+
387
+ return ve;
388
+}
389
+
390
+static void insert_victim_entry(struct f2fs_sb_info *sbi,
391
+ unsigned long long mtime, unsigned int segno)
392
+{
393
+ struct atgc_management *am = &sbi->am;
394
+ struct rb_node **p;
395
+ struct rb_node *parent = NULL;
396
+ bool left_most = true;
397
+
398
+ p = f2fs_lookup_rb_tree_ext(sbi, &am->root, &parent, mtime, &left_most);
399
+ attach_victim_entry(sbi, mtime, segno, parent, p, left_most);
400
+}
401
+
402
+static void add_victim_entry(struct f2fs_sb_info *sbi,
403
+ struct victim_sel_policy *p, unsigned int segno)
404
+{
405
+ struct sit_info *sit_i = SIT_I(sbi);
406
+ unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
407
+ unsigned int start = GET_SEG_FROM_SEC(sbi, secno);
408
+ unsigned long long mtime = 0;
409
+ unsigned int i;
410
+
411
+ if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
412
+ if (p->gc_mode == GC_AT &&
413
+ get_valid_blocks(sbi, segno, true) == 0)
414
+ return;
415
+ }
416
+
417
+ for (i = 0; i < sbi->segs_per_sec; i++)
418
+ mtime += get_seg_entry(sbi, start + i)->mtime;
419
+ mtime = div_u64(mtime, sbi->segs_per_sec);
420
+
421
+ /* Handle if the system time has changed by the user */
422
+ if (mtime < sit_i->min_mtime)
423
+ sit_i->min_mtime = mtime;
424
+ if (mtime > sit_i->max_mtime)
425
+ sit_i->max_mtime = mtime;
426
+ if (mtime < sit_i->dirty_min_mtime)
427
+ sit_i->dirty_min_mtime = mtime;
428
+ if (mtime > sit_i->dirty_max_mtime)
429
+ sit_i->dirty_max_mtime = mtime;
430
+
431
+ /* don't choose young section as candidate */
432
+ if (sit_i->dirty_max_mtime - mtime < p->age_threshold)
433
+ return;
434
+
435
+ insert_victim_entry(sbi, mtime, segno);
436
+}
437
+
438
+static struct rb_node *lookup_central_victim(struct f2fs_sb_info *sbi,
439
+ struct victim_sel_policy *p)
440
+{
441
+ struct atgc_management *am = &sbi->am;
442
+ struct rb_node *parent = NULL;
443
+ bool left_most;
444
+
445
+ f2fs_lookup_rb_tree_ext(sbi, &am->root, &parent, p->age, &left_most);
446
+
447
+ return parent;
448
+}
449
+
450
+static void atgc_lookup_victim(struct f2fs_sb_info *sbi,
451
+ struct victim_sel_policy *p)
452
+{
453
+ struct sit_info *sit_i = SIT_I(sbi);
454
+ struct atgc_management *am = &sbi->am;
455
+ struct rb_root_cached *root = &am->root;
456
+ struct rb_node *node;
457
+ struct rb_entry *re;
458
+ struct victim_entry *ve;
459
+ unsigned long long total_time;
460
+ unsigned long long age, u, accu;
461
+ unsigned long long max_mtime = sit_i->dirty_max_mtime;
462
+ unsigned long long min_mtime = sit_i->dirty_min_mtime;
463
+ unsigned int sec_blocks = BLKS_PER_SEC(sbi);
464
+ unsigned int vblocks;
465
+ unsigned int dirty_threshold = max(am->max_candidate_count,
466
+ am->candidate_ratio *
467
+ am->victim_count / 100);
468
+ unsigned int age_weight = am->age_weight;
469
+ unsigned int cost;
470
+ unsigned int iter = 0;
471
+
472
+ if (max_mtime < min_mtime)
473
+ return;
474
+
475
+ max_mtime += 1;
476
+ total_time = max_mtime - min_mtime;
477
+
478
+ accu = div64_u64(ULLONG_MAX, total_time);
479
+ accu = min_t(unsigned long long, div_u64(accu, 100),
480
+ DEFAULT_ACCURACY_CLASS);
481
+
482
+ node = rb_first_cached(root);
483
+next:
484
+ re = rb_entry_safe(node, struct rb_entry, rb_node);
485
+ if (!re)
486
+ return;
487
+
488
+ ve = (struct victim_entry *)re;
489
+
490
+ if (ve->mtime >= max_mtime || ve->mtime < min_mtime)
491
+ goto skip;
492
+
493
+ /* age = 10000 * x% * 60 */
494
+ age = div64_u64(accu * (max_mtime - ve->mtime), total_time) *
495
+ age_weight;
496
+
497
+ vblocks = get_valid_blocks(sbi, ve->segno, true);
498
+ f2fs_bug_on(sbi, !vblocks || vblocks == sec_blocks);
499
+
500
+ /* u = 10000 * x% * 40 */
501
+ u = div64_u64(accu * (sec_blocks - vblocks), sec_blocks) *
502
+ (100 - age_weight);
503
+
504
+ f2fs_bug_on(sbi, age + u >= UINT_MAX);
505
+
506
+ cost = UINT_MAX - (age + u);
507
+ iter++;
508
+
509
+ if (cost < p->min_cost ||
510
+ (cost == p->min_cost && age > p->oldest_age)) {
511
+ p->min_cost = cost;
512
+ p->oldest_age = age;
513
+ p->min_segno = ve->segno;
514
+ }
515
+skip:
516
+ if (iter < dirty_threshold) {
517
+ node = rb_next(node);
518
+ goto next;
519
+ }
520
+}
521
+
522
+/*
523
+ * select candidates around source section in range of
524
+ * [target - dirty_threshold, target + dirty_threshold]
525
+ */
526
+static void atssr_lookup_victim(struct f2fs_sb_info *sbi,
527
+ struct victim_sel_policy *p)
528
+{
529
+ struct sit_info *sit_i = SIT_I(sbi);
530
+ struct atgc_management *am = &sbi->am;
531
+ struct rb_node *node;
532
+ struct rb_entry *re;
533
+ struct victim_entry *ve;
534
+ unsigned long long age;
535
+ unsigned long long max_mtime = sit_i->dirty_max_mtime;
536
+ unsigned long long min_mtime = sit_i->dirty_min_mtime;
537
+ unsigned int seg_blocks = sbi->blocks_per_seg;
538
+ unsigned int vblocks;
539
+ unsigned int dirty_threshold = max(am->max_candidate_count,
540
+ am->candidate_ratio *
541
+ am->victim_count / 100);
542
+ unsigned int cost;
543
+ unsigned int iter = 0;
544
+ int stage = 0;
545
+
546
+ if (max_mtime < min_mtime)
547
+ return;
548
+ max_mtime += 1;
549
+next_stage:
550
+ node = lookup_central_victim(sbi, p);
551
+next_node:
552
+ re = rb_entry_safe(node, struct rb_entry, rb_node);
553
+ if (!re) {
554
+ if (stage == 0)
555
+ goto skip_stage;
556
+ return;
557
+ }
558
+
559
+ ve = (struct victim_entry *)re;
560
+
561
+ if (ve->mtime >= max_mtime || ve->mtime < min_mtime)
562
+ goto skip_node;
563
+
564
+ age = max_mtime - ve->mtime;
565
+
566
+ vblocks = get_seg_entry(sbi, ve->segno)->ckpt_valid_blocks;
567
+ f2fs_bug_on(sbi, !vblocks);
568
+
569
+ /* rare case */
570
+ if (vblocks == seg_blocks)
571
+ goto skip_node;
572
+
573
+ iter++;
574
+
575
+ age = max_mtime - abs(p->age - age);
576
+ cost = UINT_MAX - vblocks;
577
+
578
+ if (cost < p->min_cost ||
579
+ (cost == p->min_cost && age > p->oldest_age)) {
580
+ p->min_cost = cost;
581
+ p->oldest_age = age;
582
+ p->min_segno = ve->segno;
583
+ }
584
+skip_node:
585
+ if (iter < dirty_threshold) {
586
+ if (stage == 0)
587
+ node = rb_prev(node);
588
+ else if (stage == 1)
589
+ node = rb_next(node);
590
+ goto next_node;
591
+ }
592
+skip_stage:
593
+ if (stage < 1) {
594
+ stage++;
595
+ iter = 0;
596
+ goto next_stage;
597
+ }
598
+}
599
+static void lookup_victim_by_age(struct f2fs_sb_info *sbi,
600
+ struct victim_sel_policy *p)
601
+{
602
+ f2fs_bug_on(sbi, !f2fs_check_rb_tree_consistence(sbi,
603
+ &sbi->am.root, true));
604
+
605
+ if (p->gc_mode == GC_AT)
606
+ atgc_lookup_victim(sbi, p);
607
+ else if (p->alloc_mode == AT_SSR)
608
+ atssr_lookup_victim(sbi, p);
609
+ else
610
+ f2fs_bug_on(sbi, 1);
611
+}
612
+
613
+static void release_victim_entry(struct f2fs_sb_info *sbi)
614
+{
615
+ struct atgc_management *am = &sbi->am;
616
+ struct victim_entry *ve, *tmp;
617
+
618
+ list_for_each_entry_safe(ve, tmp, &am->victim_list, list) {
619
+ list_del(&ve->list);
620
+ kmem_cache_free(victim_entry_slab, ve);
621
+ am->victim_count--;
622
+ }
623
+
624
+ am->root = RB_ROOT_CACHED;
625
+
626
+ f2fs_bug_on(sbi, am->victim_count);
627
+ f2fs_bug_on(sbi, !list_empty(&am->victim_list));
628
+}
629
+
307630 /*
308631 * This function is called from two paths.
309632 * One is garbage collection and the other is SSR segment selection.
....@@ -313,31 +636,51 @@
313636 * which has minimum valid blocks and removes it from dirty seglist.
314637 */
315638 static int get_victim_by_default(struct f2fs_sb_info *sbi,
316
- unsigned int *result, int gc_type, int type, char alloc_mode)
639
+ unsigned int *result, int gc_type, int type,
640
+ char alloc_mode, unsigned long long age)
317641 {
318642 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
319643 struct sit_info *sm = SIT_I(sbi);
320644 struct victim_sel_policy p;
321645 unsigned int secno, last_victim;
322646 unsigned int last_segment;
323
- unsigned int nsearched = 0;
647
+ unsigned int nsearched;
648
+ bool is_atgc;
649
+ int ret = 0;
324650
325651 mutex_lock(&dirty_i->seglist_lock);
326652 last_segment = MAIN_SECS(sbi) * sbi->segs_per_sec;
327653
328654 p.alloc_mode = alloc_mode;
329
- select_policy(sbi, gc_type, type, &p);
655
+ p.age = age;
656
+ p.age_threshold = sbi->am.age_threshold;
330657
658
+retry:
659
+ select_policy(sbi, gc_type, type, &p);
331660 p.min_segno = NULL_SEGNO;
661
+ p.oldest_age = 0;
332662 p.min_cost = get_max_cost(sbi, &p);
333663
664
+ is_atgc = (p.gc_mode == GC_AT || p.alloc_mode == AT_SSR);
665
+ nsearched = 0;
666
+
667
+ if (is_atgc)
668
+ SIT_I(sbi)->dirty_min_mtime = ULLONG_MAX;
669
+
334670 if (*result != NULL_SEGNO) {
335
- if (get_valid_blocks(sbi, *result, false) &&
336
- !sec_usage_check(sbi, GET_SEC_FROM_SEG(sbi, *result)))
671
+ if (!get_valid_blocks(sbi, *result, false)) {
672
+ ret = -ENODATA;
673
+ goto out;
674
+ }
675
+
676
+ if (sec_usage_check(sbi, GET_SEC_FROM_SEG(sbi, *result)))
677
+ ret = -EBUSY;
678
+ else
337679 p.min_segno = *result;
338680 goto out;
339681 }
340682
683
+ ret = -ENODATA;
341684 if (p.max_search == 0)
342685 goto out;
343686
....@@ -365,10 +708,14 @@
365708 }
366709
367710 while (1) {
368
- unsigned long cost;
369
- unsigned int segno;
711
+ unsigned long cost, *dirty_bitmap;
712
+ unsigned int unit_no, segno;
370713
371
- segno = find_next_bit(p.dirty_segmap, last_segment, p.offset);
714
+ dirty_bitmap = p.dirty_bitmap;
715
+ unit_no = find_next_bit(dirty_bitmap,
716
+ last_segment / p.ofs_unit,
717
+ p.offset / p.ofs_unit);
718
+ segno = unit_no * p.ofs_unit;
372719 if (segno >= last_segment) {
373720 if (sm->last_victim[p.gc_mode]) {
374721 last_segment =
....@@ -381,14 +728,7 @@
381728 }
382729
383730 p.offset = segno + p.ofs_unit;
384
- if (p.ofs_unit > 1) {
385
- p.offset -= segno % p.ofs_unit;
386
- nsearched += count_bits(p.dirty_segmap,
387
- p.offset - p.ofs_unit,
388
- p.ofs_unit);
389
- } else {
390
- nsearched++;
391
- }
731
+ nsearched++;
392732
393733 #ifdef CONFIG_F2FS_CHECK_FS
394734 /*
....@@ -404,13 +744,34 @@
404744
405745 if (sec_usage_check(sbi, secno))
406746 goto next;
747
+
407748 /* Don't touch checkpointed data */
408
- if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
409
- get_ckpt_valid_blocks(sbi, segno) &&
410
- p.alloc_mode != SSR))
411
- goto next;
749
+ if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
750
+ if (p.alloc_mode == LFS) {
751
+ /*
752
+ * LFS is set to find source section during GC.
753
+ * The victim should have no checkpointed data.
754
+ */
755
+ if (get_ckpt_valid_blocks(sbi, segno, true))
756
+ goto next;
757
+ } else {
758
+ /*
759
+ * SSR | AT_SSR are set to find target segment
760
+ * for writes which can be full by checkpointed
761
+ * and newly written blocks.
762
+ */
763
+ if (!f2fs_segment_has_free_slot(sbi, segno))
764
+ goto next;
765
+ }
766
+ }
767
+
412768 if (gc_type == BG_GC && test_bit(secno, dirty_i->victim_secmap))
413769 goto next;
770
+
771
+ if (is_atgc) {
772
+ add_victim_entry(sbi, &p, segno);
773
+ goto next;
774
+ }
414775
415776 cost = get_gc_cost(sbi, segno, &p);
416777
....@@ -421,14 +782,28 @@
421782 next:
422783 if (nsearched >= p.max_search) {
423784 if (!sm->last_victim[p.gc_mode] && segno <= last_victim)
424
- sm->last_victim[p.gc_mode] = last_victim + 1;
785
+ sm->last_victim[p.gc_mode] =
786
+ last_victim + p.ofs_unit;
425787 else
426
- sm->last_victim[p.gc_mode] = segno + 1;
788
+ sm->last_victim[p.gc_mode] = segno + p.ofs_unit;
427789 sm->last_victim[p.gc_mode] %=
428790 (MAIN_SECS(sbi) * sbi->segs_per_sec);
429791 break;
430792 }
431793 }
794
+
795
+ /* get victim for GC_AT/AT_SSR */
796
+ if (is_atgc) {
797
+ lookup_victim_by_age(sbi, &p);
798
+ release_victim_entry(sbi);
799
+ }
800
+
801
+ if (is_atgc && p.min_segno == NULL_SEGNO &&
802
+ sm->elapsed_time < p.age_threshold) {
803
+ p.age_threshold = 0;
804
+ goto retry;
805
+ }
806
+
432807 if (p.min_segno != NULL_SEGNO) {
433808 got_it:
434809 *result = (p.min_segno / p.ofs_unit) * p.ofs_unit;
....@@ -440,6 +815,7 @@
440815 else
441816 set_bit(secno, dirty_i->victim_secmap);
442817 }
818
+ ret = 0;
443819
444820 }
445821 out:
....@@ -449,7 +825,7 @@
449825 prefree_segments(sbi), free_segments(sbi));
450826 mutex_unlock(&dirty_i->seglist_lock);
451827
452
- return (p.min_segno == NULL_SEGNO) ? 0 : 1;
828
+ return ret;
453829 }
454830
455831 static const struct victim_selection default_v_ops = {
....@@ -484,6 +860,7 @@
484860 static void put_gc_inode(struct gc_inode_list *gc_list)
485861 {
486862 struct inode_entry *ie, *next_ie;
863
+
487864 list_for_each_entry_safe(ie, next_ie, &gc_list->ilist, list) {
488865 radix_tree_delete(&gc_list->iroot, ie->inode->i_ino);
489866 iput(ie->inode);
....@@ -520,6 +897,7 @@
520897 int phase = 0;
521898 bool fggc = (gc_type == FG_GC);
522899 int submitted = 0;
900
+ unsigned int usable_blks_in_seg = f2fs_usable_blks_in_seg(sbi, segno);
523901
524902 start_addr = START_BLOCK(sbi, segno);
525903
....@@ -529,7 +907,7 @@
529907 if (fggc && phase == 2)
530908 atomic_inc(&sbi->wb_sync_req[NODE]);
531909
532
- for (off = 0; off < sbi->blocks_per_seg; off++, entry++) {
910
+ for (off = 0; off < usable_blks_in_seg; off++, entry++) {
533911 nid_t nid = le32_to_cpu(entry->nid);
534912 struct page *node_page;
535913 struct node_info ni;
....@@ -564,7 +942,7 @@
564942 continue;
565943 }
566944
567
- if (f2fs_get_node_info(sbi, nid, &ni)) {
945
+ if (f2fs_get_node_info(sbi, nid, &ni, false)) {
568946 f2fs_put_page(node_page, 1);
569947 continue;
570948 }
....@@ -607,9 +985,11 @@
607985 bidx = node_ofs - 1;
608986 } else if (node_ofs <= indirect_blks) {
609987 int dec = (node_ofs - 4) / (NIDS_PER_BLOCK + 1);
988
+
610989 bidx = node_ofs - 2 - dec;
611990 } else {
612991 int dec = (node_ofs - indirect_blks - 3) / (NIDS_PER_BLOCK + 1);
992
+
613993 bidx = node_ofs - 5 - dec;
614994 }
615995 return bidx * ADDRS_PER_BLOCK(inode) + ADDRS_PER_INODE(inode);
....@@ -620,7 +1000,7 @@
6201000 {
6211001 struct page *node_page;
6221002 nid_t nid;
623
- unsigned int ofs_in_node;
1003
+ unsigned int ofs_in_node, max_addrs;
6241004 block_t source_blkaddr;
6251005
6261006 nid = le32_to_cpu(sum->nid);
....@@ -630,7 +1010,7 @@
6301010 if (IS_ERR(node_page))
6311011 return false;
6321012
633
- if (f2fs_get_node_info(sbi, nid, dni)) {
1013
+ if (f2fs_get_node_info(sbi, nid, dni, false)) {
6341014 f2fs_put_page(node_page, 1);
6351015 return false;
6361016 }
....@@ -641,8 +1021,18 @@
6411021 set_sbi_flag(sbi, SBI_NEED_FSCK);
6421022 }
6431023
644
- if (f2fs_check_nid_range(sbi, dni->ino))
1024
+ if (f2fs_check_nid_range(sbi, dni->ino)) {
1025
+ f2fs_put_page(node_page, 1);
6451026 return false;
1027
+ }
1028
+
1029
+ max_addrs = IS_INODE(node_page) ? DEF_ADDRS_PER_INODE :
1030
+ DEF_ADDRS_PER_BLOCK;
1031
+ if (ofs_in_node >= max_addrs) {
1032
+ f2fs_err(sbi, "Inconsistent ofs_in_node:%u in summary, ino:%u, nid:%u, max:%u",
1033
+ ofs_in_node, dni->ino, dni->nid, max_addrs);
1034
+ return false;
1035
+ }
6461036
6471037 *nofs = ofs_of_node(node_page);
6481038 source_blkaddr = data_blkaddr(NULL, node_page, ofs_in_node);
....@@ -655,8 +1045,8 @@
6551045
6561046 if (unlikely(check_valid_map(sbi, segno, offset))) {
6571047 if (!test_and_set_bit(segno, SIT_I(sbi)->invalid_segmap)) {
658
- f2fs_err(sbi, "mismatched blkaddr %u (source_blkaddr %u) in seg %u\n",
659
- blkaddr, source_blkaddr, segno);
1048
+ f2fs_err(sbi, "mismatched blkaddr %u (source_blkaddr %u) in seg %u",
1049
+ blkaddr, source_blkaddr, segno);
6601050 f2fs_bug_on(sbi, 1);
6611051 }
6621052 }
....@@ -672,7 +1062,7 @@
6721062 struct address_space *mapping = inode->i_mapping;
6731063 struct dnode_of_data dn;
6741064 struct page *page;
675
- struct extent_info ei = {0, 0, 0};
1065
+ struct extent_info ei = {0, };
6761066 struct f2fs_io_info fio = {
6771067 .sbi = sbi,
6781068 .ino = inode->i_ino,
....@@ -690,7 +1080,7 @@
6901080 if (!page)
6911081 return -ENOMEM;
6921082
693
- if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1083
+ if (f2fs_lookup_read_extent_cache(inode, index, &ei)) {
6941084 dn.data_blkaddr = ei.blk + index - ei.fofs;
6951085 if (unlikely(!f2fs_is_valid_blkaddr(sbi, dn.data_blkaddr,
6961086 DATA_GENERIC_ENHANCE_READ))) {
....@@ -778,6 +1168,9 @@
7781168 block_t newaddr;
7791169 int err = 0;
7801170 bool lfs_mode = f2fs_lfs_mode(fio.sbi);
1171
+ int type = fio.sbi->am.atgc_enabled && (gc_type == BG_GC) &&
1172
+ (fio.sbi->gc_mode != GC_URGENT_HIGH) ?
1173
+ CURSEG_ALL_DATA_ATGC : CURSEG_COLD_DATA;
7811174
7821175 /* do not read out */
7831176 page = f2fs_grab_cache_page(inode->i_mapping, bidx, false);
....@@ -797,7 +1190,8 @@
7971190 }
7981191
7991192 if (f2fs_is_pinned_file(inode)) {
800
- f2fs_pin_file_control(inode, true);
1193
+ if (gc_type == FG_GC)
1194
+ f2fs_pin_file_control(inode, true);
8011195 err = -EAGAIN;
8021196 goto out;
8031197 }
....@@ -821,23 +1215,23 @@
8211215
8221216 f2fs_wait_on_block_writeback(inode, dn.data_blkaddr);
8231217
824
- err = f2fs_get_node_info(fio.sbi, dn.nid, &ni);
1218
+ err = f2fs_get_node_info(fio.sbi, dn.nid, &ni, false);
8251219 if (err)
8261220 goto put_out;
827
-
828
- set_summary(&sum, dn.nid, dn.ofs_in_node, ni.version);
8291221
8301222 /* read page */
8311223 fio.page = page;
8321224 fio.new_blkaddr = fio.old_blkaddr = dn.data_blkaddr;
8331225
8341226 if (lfs_mode)
835
- down_write(&fio.sbi->io_order_lock);
1227
+ f2fs_down_write(&fio.sbi->io_order_lock);
8361228
8371229 mpage = f2fs_grab_cache_page(META_MAPPING(fio.sbi),
8381230 fio.old_blkaddr, false);
839
- if (!mpage)
1231
+ if (!mpage) {
1232
+ err = -ENOMEM;
8401233 goto up_out;
1234
+ }
8411235
8421236 fio.encrypted_page = mpage;
8431237
....@@ -861,8 +1255,11 @@
8611255 }
8621256 }
8631257
1258
+ set_summary(&sum, dn.nid, dn.ofs_in_node, ni.version);
1259
+
1260
+ /* allocate block address */
8641261 f2fs_allocate_data_block(fio.sbi, NULL, fio.old_blkaddr, &newaddr,
865
- &sum, CURSEG_COLD_DATA, NULL, false);
1262
+ &sum, type, NULL);
8661263
8671264 fio.encrypted_page = f2fs_pagecache_get_page(META_MAPPING(fio.sbi),
8681265 newaddr, FGP_LOCK | FGP_CREAT, GFP_NOFS);
....@@ -879,6 +1276,7 @@
8791276 f2fs_put_page(mpage, 1);
8801277 invalidate_mapping_pages(META_MAPPING(fio.sbi),
8811278 fio.old_blkaddr, fio.old_blkaddr);
1279
+ f2fs_invalidate_compress_page(fio.sbi, fio.old_blkaddr);
8821280
8831281 set_page_dirty(fio.encrypted_page);
8841282 if (clear_page_dirty_for_io(fio.encrypted_page))
....@@ -886,9 +1284,6 @@
8861284
8871285 set_page_writeback(fio.encrypted_page);
8881286 ClearPageError(page);
889
-
890
- /* allocate block address */
891
- f2fs_wait_on_page_writeback(dn.node_page, NODE, true, true);
8921287
8931288 fio.op = REQ_OP_WRITE;
8941289 fio.op_flags = REQ_SYNC;
....@@ -912,10 +1307,10 @@
9121307 recover_block:
9131308 if (err)
9141309 f2fs_do_replace_block(fio.sbi, &sum, newaddr, fio.old_blkaddr,
915
- true, true);
1310
+ true, true, true);
9161311 up_out:
9171312 if (lfs_mode)
918
- up_write(&fio.sbi->io_order_lock);
1313
+ f2fs_up_write(&fio.sbi->io_order_lock);
9191314 put_out:
9201315 f2fs_put_dnode(&dn);
9211316 out:
....@@ -957,7 +1352,7 @@
9571352 goto out;
9581353 }
9591354 set_page_dirty(page);
960
- set_cold_data(page);
1355
+ set_page_private_gcing(page);
9611356 } else {
9621357 struct f2fs_io_info fio = {
9631358 .sbi = F2FS_I_SB(inode),
....@@ -983,11 +1378,11 @@
9831378 f2fs_remove_dirty_inode(inode);
9841379 }
9851380
986
- set_cold_data(page);
1381
+ set_page_private_gcing(page);
9871382
9881383 err = f2fs_do_write_data_page(&fio);
9891384 if (err) {
990
- clear_cold_data(page);
1385
+ clear_page_private_gcing(page);
9911386 if (err == -ENOMEM) {
9921387 congestion_wait(BLK_RW_ASYNC,
9931388 DEFAULT_IO_TIMEOUT);
....@@ -1010,7 +1405,8 @@
10101405 * the victim data block is ignored.
10111406 */
10121407 static int gc_data_segment(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
1013
- struct gc_inode_list *gc_list, unsigned int segno, int gc_type)
1408
+ struct gc_inode_list *gc_list, unsigned int segno, int gc_type,
1409
+ bool force_migrate)
10141410 {
10151411 struct super_block *sb = sbi->sb;
10161412 struct f2fs_summary *entry;
....@@ -1018,13 +1414,14 @@
10181414 int off;
10191415 int phase = 0;
10201416 int submitted = 0;
1417
+ unsigned int usable_blks_in_seg = f2fs_usable_blks_in_seg(sbi, segno);
10211418
10221419 start_addr = START_BLOCK(sbi, segno);
10231420
10241421 next_step:
10251422 entry = sum;
10261423
1027
- for (off = 0; off < sbi->blocks_per_seg; off++, entry++) {
1424
+ for (off = 0; off < usable_blks_in_seg; off++, entry++) {
10281425 struct page *data_page;
10291426 struct inode *inode;
10301427 struct node_info dni; /* dnode info for the data */
....@@ -1038,8 +1435,8 @@
10381435 * race condition along with SSR block allocation.
10391436 */
10401437 if ((gc_type == BG_GC && has_not_enough_free_secs(sbi, 0, 0)) ||
1041
- get_valid_blocks(sbi, segno, true) ==
1042
- BLKS_PER_SEC(sbi))
1438
+ (!force_migrate && get_valid_blocks(sbi, segno, true) ==
1439
+ BLKS_PER_SEC(sbi)))
10431440 return submitted;
10441441
10451442 if (check_valid_map(sbi, segno, off) == 0)
....@@ -1069,12 +1466,10 @@
10691466
10701467 if (phase == 3) {
10711468 inode = f2fs_iget(sb, dni.ino);
1072
- if (IS_ERR(inode) || is_bad_inode(inode)) {
1073
- set_sbi_flag(sbi, SBI_NEED_FSCK);
1469
+ if (IS_ERR(inode) || is_bad_inode(inode))
10741470 continue;
1075
- }
10761471
1077
- if (!down_write_trylock(
1472
+ if (!f2fs_down_write_trylock(
10781473 &F2FS_I(inode)->i_gc_rwsem[WRITE])) {
10791474 iput(inode);
10801475 sbi->skipped_gc_rwsem++;
....@@ -1087,7 +1482,7 @@
10871482 if (f2fs_post_read_required(inode)) {
10881483 int err = ra_data_block(inode, start_bidx);
10891484
1090
- up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1485
+ f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
10911486 if (err) {
10921487 iput(inode);
10931488 continue;
....@@ -1098,7 +1493,7 @@
10981493
10991494 data_page = f2fs_get_read_data_page(inode,
11001495 start_bidx, REQ_RAHEAD, true);
1101
- up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1496
+ f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
11021497 if (IS_ERR(data_page)) {
11031498 iput(inode);
11041499 continue;
....@@ -1117,14 +1512,14 @@
11171512 int err;
11181513
11191514 if (S_ISREG(inode->i_mode)) {
1120
- if (!down_write_trylock(&fi->i_gc_rwsem[READ])) {
1515
+ if (!f2fs_down_write_trylock(&fi->i_gc_rwsem[READ])) {
11211516 sbi->skipped_gc_rwsem++;
11221517 continue;
11231518 }
1124
- if (!down_write_trylock(
1519
+ if (!f2fs_down_write_trylock(
11251520 &fi->i_gc_rwsem[WRITE])) {
11261521 sbi->skipped_gc_rwsem++;
1127
- up_write(&fi->i_gc_rwsem[READ]);
1522
+ f2fs_up_write(&fi->i_gc_rwsem[READ]);
11281523 continue;
11291524 }
11301525 locked = true;
....@@ -1147,8 +1542,8 @@
11471542 submitted++;
11481543
11491544 if (locked) {
1150
- up_write(&fi->i_gc_rwsem[WRITE]);
1151
- up_write(&fi->i_gc_rwsem[READ]);
1545
+ f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
1546
+ f2fs_up_write(&fi->i_gc_rwsem[READ]);
11521547 }
11531548
11541549 stat_inc_data_blk_count(sbi, 1, gc_type);
....@@ -1169,14 +1564,15 @@
11691564
11701565 down_write(&sit_i->sentry_lock);
11711566 ret = DIRTY_I(sbi)->v_ops->get_victim(sbi, victim, gc_type,
1172
- NO_CHECK_TYPE, LFS);
1567
+ NO_CHECK_TYPE, LFS, 0);
11731568 up_write(&sit_i->sentry_lock);
11741569 return ret;
11751570 }
11761571
11771572 static int do_garbage_collect(struct f2fs_sb_info *sbi,
11781573 unsigned int start_segno,
1179
- struct gc_inode_list *gc_list, int gc_type)
1574
+ struct gc_inode_list *gc_list, int gc_type,
1575
+ bool force_migrate)
11801576 {
11811577 struct page *sum_page;
11821578 struct f2fs_summary_block *sum;
....@@ -1190,6 +1586,17 @@
11901586
11911587 if (__is_large_section(sbi))
11921588 end_segno = rounddown(end_segno, sbi->segs_per_sec);
1589
+
1590
+ /*
1591
+ * zone-capacity can be less than zone-size in zoned devices,
1592
+ * resulting in less than expected usable segments in the zone,
1593
+ * calculate the end segno in the zone which can be garbage collected
1594
+ */
1595
+ if (f2fs_sb_has_blkzoned(sbi))
1596
+ end_segno -= sbi->segs_per_sec -
1597
+ f2fs_usable_segs_in_sec(sbi, segno);
1598
+
1599
+ sanity_check_seg_type(sbi, get_seg_entry(sbi, segno)->type);
11931600
11941601 /* readahead multi ssa blocks those have contiguous address */
11951602 if (__is_large_section(sbi))
....@@ -1236,7 +1643,8 @@
12361643 f2fs_err(sbi, "Inconsistent segment (%u) type [%d, %d] in SSA and SIT",
12371644 segno, type, GET_SUM_TYPE((&sum->footer)));
12381645 set_sbi_flag(sbi, SBI_NEED_FSCK);
1239
- f2fs_stop_checkpoint(sbi, false);
1646
+ f2fs_stop_checkpoint(sbi, false,
1647
+ STOP_CP_REASON_CORRUPTED_SUMMARY);
12401648 goto skip;
12411649 }
12421650
....@@ -1252,9 +1660,11 @@
12521660 gc_type);
12531661 else
12541662 submitted += gc_data_segment(sbi, sum->entries, gc_list,
1255
- segno, gc_type);
1663
+ segno, gc_type,
1664
+ force_migrate);
12561665
12571666 stat_inc_seg_count(sbi, type, gc_type);
1667
+ sbi->gc_reclaimed_segs[sbi->gc_mode]++;
12581668 migrated++;
12591669
12601670 freed:
....@@ -1280,7 +1690,7 @@
12801690 }
12811691
12821692 int f2fs_gc(struct f2fs_sb_info *sbi, bool sync,
1283
- bool background, unsigned int segno)
1693
+ bool background, bool force, unsigned int segno)
12841694 {
12851695 int gc_type = sync ? FG_GC : BG_GC;
12861696 int sec_freed = 0, seg_freed = 0, total_freed = 0;
....@@ -1338,13 +1748,13 @@
13381748 ret = -EINVAL;
13391749 goto stop;
13401750 }
1341
- if (!__get_victim(sbi, &segno, gc_type)) {
1342
- ret = -ENODATA;
1751
+ ret = __get_victim(sbi, &segno, gc_type);
1752
+ if (ret)
13431753 goto stop;
1344
- }
13451754
1346
- seg_freed = do_garbage_collect(sbi, segno, &gc_list, gc_type);
1347
- if (gc_type == FG_GC && seg_freed == sbi->segs_per_sec)
1755
+ seg_freed = do_garbage_collect(sbi, segno, &gc_list, gc_type, force);
1756
+ if (gc_type == FG_GC &&
1757
+ seg_freed == f2fs_usable_segs_in_sec(sbi, segno))
13481758 sec_freed++;
13491759 total_freed += seg_freed;
13501760
....@@ -1362,23 +1772,31 @@
13621772 if (sync)
13631773 goto stop;
13641774
1365
- if (has_not_enough_free_secs(sbi, sec_freed, 0)) {
1366
- if (skipped_round <= MAX_SKIP_GC_COUNT ||
1367
- skipped_round * 2 < round) {
1368
- segno = NULL_SEGNO;
1369
- goto gc_more;
1370
- }
1775
+ if (!has_not_enough_free_secs(sbi, sec_freed, 0))
1776
+ goto stop;
13711777
1372
- if (first_skipped < last_skipped &&
1373
- (last_skipped - first_skipped) >
1374
- sbi->skipped_gc_rwsem) {
1375
- f2fs_drop_inmem_pages_all(sbi, true);
1376
- segno = NULL_SEGNO;
1377
- goto gc_more;
1378
- }
1379
- if (gc_type == FG_GC && !is_sbi_flag_set(sbi, SBI_CP_DISABLED))
1778
+ if (skipped_round <= MAX_SKIP_GC_COUNT || skipped_round * 2 < round) {
1779
+
1780
+ /* Write checkpoint to reclaim prefree segments */
1781
+ if (free_sections(sbi) < NR_CURSEG_PERSIST_TYPE &&
1782
+ prefree_segments(sbi) &&
1783
+ !is_sbi_flag_set(sbi, SBI_CP_DISABLED)) {
13801784 ret = f2fs_write_checkpoint(sbi, &cpc);
1785
+ if (ret)
1786
+ goto stop;
1787
+ }
1788
+ segno = NULL_SEGNO;
1789
+ goto gc_more;
13811790 }
1791
+ if (first_skipped < last_skipped &&
1792
+ (last_skipped - first_skipped) >
1793
+ sbi->skipped_gc_rwsem) {
1794
+ f2fs_drop_inmem_pages_all(sbi, true);
1795
+ segno = NULL_SEGNO;
1796
+ goto gc_more;
1797
+ }
1798
+ if (gc_type == FG_GC && !is_sbi_flag_set(sbi, SBI_CP_DISABLED))
1799
+ ret = f2fs_write_checkpoint(sbi, &cpc);
13821800 stop:
13831801 SIT_I(sbi)->last_victim[ALLOC_NEXT] = 0;
13841802 SIT_I(sbi)->last_victim[FLUSH_DEVICE] = init_segno;
....@@ -1392,13 +1810,45 @@
13921810 reserved_segments(sbi),
13931811 prefree_segments(sbi));
13941812
1395
- up_write(&sbi->gc_lock);
1813
+ f2fs_up_write(&sbi->gc_lock);
13961814
13971815 put_gc_inode(&gc_list);
13981816
13991817 if (sync && !ret)
14001818 ret = sec_freed ? 0 : -EAGAIN;
14011819 return ret;
1820
+}
1821
+
1822
+int __init f2fs_create_garbage_collection_cache(void)
1823
+{
1824
+ victim_entry_slab = f2fs_kmem_cache_create("f2fs_victim_entry",
1825
+ sizeof(struct victim_entry));
1826
+ if (!victim_entry_slab)
1827
+ return -ENOMEM;
1828
+ return 0;
1829
+}
1830
+
1831
+void f2fs_destroy_garbage_collection_cache(void)
1832
+{
1833
+ kmem_cache_destroy(victim_entry_slab);
1834
+}
1835
+
1836
+static void init_atgc_management(struct f2fs_sb_info *sbi)
1837
+{
1838
+ struct atgc_management *am = &sbi->am;
1839
+
1840
+ if (test_opt(sbi, ATGC) &&
1841
+ SIT_I(sbi)->elapsed_time >= DEF_GC_THREAD_AGE_THRESHOLD)
1842
+ am->atgc_enabled = true;
1843
+
1844
+ am->root = RB_ROOT_CACHED;
1845
+ INIT_LIST_HEAD(&am->victim_list);
1846
+ am->victim_count = 0;
1847
+
1848
+ am->candidate_ratio = DEF_GC_THREAD_CANDIDATE_RATIO;
1849
+ am->max_candidate_count = DEF_GC_THREAD_MAX_CANDIDATE_COUNT;
1850
+ am->age_weight = DEF_GC_THREAD_AGE_WEIGHT;
1851
+ am->age_threshold = DEF_GC_THREAD_AGE_THRESHOLD;
14021852 }
14031853
14041854 void f2fs_build_gc_manager(struct f2fs_sb_info *sbi)
....@@ -1411,6 +1861,8 @@
14111861 if (f2fs_is_multi_device(sbi) && !__is_large_section(sbi))
14121862 SIT_I(sbi)->last_victim[ALLOC_NEXT] =
14131863 GET_SEGNO(sbi, FDEV(0).end_blk) + 1;
1864
+
1865
+ init_atgc_management(sbi);
14141866 }
14151867
14161868 static int free_segment_range(struct f2fs_sb_info *sbi,
....@@ -1438,8 +1890,8 @@
14381890 mutex_unlock(&DIRTY_I(sbi)->seglist_lock);
14391891
14401892 /* Move out cursegs from the target range */
1441
- for (type = CURSEG_HOT_DATA; type < NR_CURSEG_TYPE; type++)
1442
- allocate_segment_for_resize(sbi, type, start, end);
1893
+ for (type = CURSEG_HOT_DATA; type < NR_CURSEG_PERSIST_TYPE; type++)
1894
+ f2fs_allocate_segment_for_resize(sbi, type, start, end);
14431895
14441896 /* do GC to move out valid blocks in the range */
14451897 for (segno = start; segno <= end; segno += sbi->segs_per_sec) {
....@@ -1448,7 +1900,7 @@
14481900 .iroot = RADIX_TREE_INIT(gc_list.iroot, GFP_NOFS),
14491901 };
14501902
1451
- do_garbage_collect(sbi, segno, &gc_list, FG_GC);
1903
+ do_garbage_collect(sbi, segno, &gc_list, FG_GC, true);
14521904 put_gc_inode(&gc_list);
14531905
14541906 if (!gc_only && get_valid_blocks(sbi, segno, true)) {
....@@ -1487,7 +1939,7 @@
14871939 long long block_count;
14881940 int segs = secs * sbi->segs_per_sec;
14891941
1490
- down_write(&sbi->sb_lock);
1942
+ f2fs_down_write(&sbi->sb_lock);
14911943
14921944 section_count = le32_to_cpu(raw_sb->section_count);
14931945 segment_count = le32_to_cpu(raw_sb->segment_count);
....@@ -1508,7 +1960,7 @@
15081960 cpu_to_le32(dev_segs + segs);
15091961 }
15101962
1511
- up_write(&sbi->sb_lock);
1963
+ f2fs_up_write(&sbi->sb_lock);
15121964 }
15131965
15141966 static void update_fs_metadata(struct f2fs_sb_info *sbi, int secs)
....@@ -1582,22 +2034,35 @@
15822034 secs = div_u64(shrunk_blocks, BLKS_PER_SEC(sbi));
15832035
15842036 /* stop other GC */
1585
- if (!down_write_trylock(&sbi->gc_lock))
2037
+ if (!f2fs_down_write_trylock(&sbi->gc_lock))
15862038 return -EAGAIN;
15872039
15882040 /* stop CP to protect MAIN_SEC in free_segment_range */
15892041 f2fs_lock_op(sbi);
2042
+
2043
+ spin_lock(&sbi->stat_lock);
2044
+ if (shrunk_blocks + valid_user_blocks(sbi) +
2045
+ sbi->current_reserved_blocks + sbi->unusable_block_count +
2046
+ F2FS_OPTION(sbi).root_reserved_blocks > sbi->user_block_count)
2047
+ err = -ENOSPC;
2048
+ spin_unlock(&sbi->stat_lock);
2049
+
2050
+ if (err)
2051
+ goto out_unlock;
2052
+
15902053 err = free_segment_range(sbi, secs, true);
2054
+
2055
+out_unlock:
15912056 f2fs_unlock_op(sbi);
1592
- up_write(&sbi->gc_lock);
2057
+ f2fs_up_write(&sbi->gc_lock);
15932058 if (err)
15942059 return err;
15952060
15962061 set_sbi_flag(sbi, SBI_IS_RESIZEFS);
15972062
15982063 freeze_super(sbi->sb);
1599
- down_write(&sbi->gc_lock);
1600
- mutex_lock(&sbi->cp_mutex);
2064
+ f2fs_down_write(&sbi->gc_lock);
2065
+ f2fs_down_write(&sbi->cp_global_sem);
16012066
16022067 spin_lock(&sbi->stat_lock);
16032068 if (shrunk_blocks + valid_user_blocks(sbi) +
....@@ -1642,8 +2107,8 @@
16422107 spin_unlock(&sbi->stat_lock);
16432108 }
16442109 out_err:
1645
- mutex_unlock(&sbi->cp_mutex);
1646
- up_write(&sbi->gc_lock);
2110
+ f2fs_up_write(&sbi->cp_global_sem);
2111
+ f2fs_up_write(&sbi->gc_lock);
16472112 thaw_super(sbi->sb);
16482113 clear_sbi_flag(sbi, SBI_IS_RESIZEFS);
16492114 return err;