1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * fs/f2fs/gc.c
4 *
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
7 */
8 #include <linux/fs.h>
9 #include <linux/module.h>
10 #include <linux/mount.h>
11 #include <linux/backing-dev.h>
12 #include <linux/init.h>
13 #include <linux/f2fs_fs.h>
14 #include <linux/kthread.h>
15 #include <linux/delay.h>
16 #include <linux/freezer.h>
17 #include <linux/sched/signal.h>
18
19 #include "f2fs.h"
20 #include "node.h"
21 #include "segment.h"
22 #include "gc.h"
23 #include <trace/events/f2fs.h>
24
25 static struct kmem_cache *victim_entry_slab;
26
27 static unsigned int count_bits(const unsigned long *addr,
28 unsigned int offset, unsigned int len);
29
gc_thread_func(void * data)30 static int gc_thread_func(void *data)
31 {
32 struct f2fs_sb_info *sbi = data;
33 struct f2fs_gc_kthread *gc_th = sbi->gc_thread;
34 wait_queue_head_t *wq = &sbi->gc_thread->gc_wait_queue_head;
35 wait_queue_head_t *fggc_wq = &sbi->gc_thread->fggc_wq;
36 unsigned int wait_ms;
37
38 wait_ms = gc_th->min_sleep_time;
39
40 set_freezable();
41 do {
42 bool sync_mode, foreground = false;
43
44 wait_event_interruptible_timeout(*wq,
45 kthread_should_stop() || freezing(current) ||
46 waitqueue_active(fggc_wq) ||
47 gc_th->gc_wake,
48 msecs_to_jiffies(wait_ms));
49
50 if (test_opt(sbi, GC_MERGE) && waitqueue_active(fggc_wq))
51 foreground = true;
52
53 /* give it a try one time */
54 if (gc_th->gc_wake)
55 gc_th->gc_wake = 0;
56
57 if (try_to_freeze()) {
58 stat_other_skip_bggc_count(sbi);
59 continue;
60 }
61 if (kthread_should_stop())
62 break;
63
64 if (sbi->sb->s_writers.frozen >= SB_FREEZE_WRITE) {
65 increase_sleep_time(gc_th, &wait_ms);
66 stat_other_skip_bggc_count(sbi);
67 continue;
68 }
69
70 if (time_to_inject(sbi, FAULT_CHECKPOINT)) {
71 f2fs_show_injection_info(sbi, FAULT_CHECKPOINT);
72 f2fs_stop_checkpoint(sbi, false,
73 STOP_CP_REASON_FAULT_INJECT);
74 }
75
76 if (!sb_start_write_trylock(sbi->sb)) {
77 stat_other_skip_bggc_count(sbi);
78 continue;
79 }
80
81 /*
82 * [GC triggering condition]
83 * 0. GC is not conducted currently.
84 * 1. There are enough dirty segments.
85 * 2. IO subsystem is idle by checking the # of writeback pages.
86 * 3. IO subsystem is idle by checking the # of requests in
87 * bdev's request list.
88 *
89 * Note) We have to avoid triggering GCs frequently.
90 * Because it is possible that some segments can be
91 * invalidated soon after by user update or deletion.
92 * So, I'd like to wait some time to collect dirty segments.
93 */
94 if (sbi->gc_mode == GC_URGENT_HIGH ||
95 sbi->gc_mode == GC_URGENT_MID) {
96 wait_ms = gc_th->urgent_sleep_time;
97 f2fs_down_write(&sbi->gc_lock);
98 goto do_gc;
99 }
100
101 if (foreground) {
102 f2fs_down_write(&sbi->gc_lock);
103 goto do_gc;
104 } else if (!f2fs_down_write_trylock(&sbi->gc_lock)) {
105 stat_other_skip_bggc_count(sbi);
106 goto next;
107 }
108
109 if (!is_idle(sbi, GC_TIME)) {
110 increase_sleep_time(gc_th, &wait_ms);
111 f2fs_up_write(&sbi->gc_lock);
112 stat_io_skip_bggc_count(sbi);
113 goto next;
114 }
115
116 if (has_enough_invalid_blocks(sbi))
117 decrease_sleep_time(gc_th, &wait_ms);
118 else
119 increase_sleep_time(gc_th, &wait_ms);
120 do_gc:
121 if (!foreground)
122 stat_inc_bggc_count(sbi->stat_info);
123
124 sync_mode = F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_SYNC;
125
126 /* foreground GC was been triggered via f2fs_balance_fs() */
127 if (foreground)
128 sync_mode = false;
129
130 /* if return value is not zero, no victim was selected */
131 if (f2fs_gc(sbi, sync_mode, !foreground, false, NULL_SEGNO))
132 wait_ms = gc_th->no_gc_sleep_time;
133
134 if (foreground)
135 wake_up_all(&gc_th->fggc_wq);
136
137 trace_f2fs_background_gc(sbi->sb, wait_ms,
138 prefree_segments(sbi), free_segments(sbi));
139
140 /* balancing f2fs's metadata periodically */
141 f2fs_balance_fs_bg(sbi, true);
142 next:
143 sb_end_write(sbi->sb);
144
145 } while (!kthread_should_stop());
146 return 0;
147 }
148
f2fs_start_gc_thread(struct f2fs_sb_info * sbi)149 int f2fs_start_gc_thread(struct f2fs_sb_info *sbi)
150 {
151 struct f2fs_gc_kthread *gc_th;
152 dev_t dev = sbi->sb->s_bdev->bd_dev;
153 int err = 0;
154
155 gc_th = f2fs_kmalloc(sbi, sizeof(struct f2fs_gc_kthread), GFP_KERNEL);
156 if (!gc_th) {
157 err = -ENOMEM;
158 goto out;
159 }
160
161 gc_th->urgent_sleep_time = DEF_GC_THREAD_URGENT_SLEEP_TIME;
162 gc_th->min_sleep_time = DEF_GC_THREAD_MIN_SLEEP_TIME;
163 gc_th->max_sleep_time = DEF_GC_THREAD_MAX_SLEEP_TIME;
164 gc_th->no_gc_sleep_time = DEF_GC_THREAD_NOGC_SLEEP_TIME;
165
166 gc_th->gc_wake = 0;
167
168 sbi->gc_thread = gc_th;
169 init_waitqueue_head(&sbi->gc_thread->gc_wait_queue_head);
170 init_waitqueue_head(&sbi->gc_thread->fggc_wq);
171 sbi->gc_thread->f2fs_gc_task = kthread_run(gc_thread_func, sbi,
172 "f2fs_gc-%u:%u", MAJOR(dev), MINOR(dev));
173 if (IS_ERR(gc_th->f2fs_gc_task)) {
174 err = PTR_ERR(gc_th->f2fs_gc_task);
175 kfree(gc_th);
176 sbi->gc_thread = NULL;
177 }
178 out:
179 return err;
180 }
181
f2fs_stop_gc_thread(struct f2fs_sb_info * sbi)182 void f2fs_stop_gc_thread(struct f2fs_sb_info *sbi)
183 {
184 struct f2fs_gc_kthread *gc_th = sbi->gc_thread;
185
186 if (!gc_th)
187 return;
188 kthread_stop(gc_th->f2fs_gc_task);
189 wake_up_all(&gc_th->fggc_wq);
190 kfree(gc_th);
191 sbi->gc_thread = NULL;
192 }
193
select_gc_type(struct f2fs_sb_info * sbi,int gc_type)194 static int select_gc_type(struct f2fs_sb_info *sbi, int gc_type)
195 {
196 int gc_mode;
197
198 if (gc_type == BG_GC) {
199 if (sbi->am.atgc_enabled)
200 gc_mode = GC_AT;
201 else
202 gc_mode = GC_CB;
203 } else {
204 gc_mode = GC_GREEDY;
205 }
206
207 switch (sbi->gc_mode) {
208 case GC_IDLE_CB:
209 gc_mode = GC_CB;
210 break;
211 case GC_IDLE_GREEDY:
212 case GC_URGENT_HIGH:
213 gc_mode = GC_GREEDY;
214 break;
215 case GC_IDLE_AT:
216 gc_mode = GC_AT;
217 break;
218 }
219
220 return gc_mode;
221 }
222
select_policy(struct f2fs_sb_info * sbi,int gc_type,int type,struct victim_sel_policy * p)223 static void select_policy(struct f2fs_sb_info *sbi, int gc_type,
224 int type, struct victim_sel_policy *p)
225 {
226 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
227
228 if (p->alloc_mode == SSR) {
229 p->gc_mode = GC_GREEDY;
230 p->dirty_bitmap = dirty_i->dirty_segmap[type];
231 p->max_search = dirty_i->nr_dirty[type];
232 p->ofs_unit = 1;
233 } else if (p->alloc_mode == AT_SSR) {
234 p->gc_mode = GC_GREEDY;
235 p->dirty_bitmap = dirty_i->dirty_segmap[type];
236 p->max_search = dirty_i->nr_dirty[type];
237 p->ofs_unit = 1;
238 } else {
239 p->gc_mode = select_gc_type(sbi, gc_type);
240 p->ofs_unit = sbi->segs_per_sec;
241 if (__is_large_section(sbi)) {
242 p->dirty_bitmap = dirty_i->dirty_secmap;
243 p->max_search = count_bits(p->dirty_bitmap,
244 0, MAIN_SECS(sbi));
245 } else {
246 p->dirty_bitmap = dirty_i->dirty_segmap[DIRTY];
247 p->max_search = dirty_i->nr_dirty[DIRTY];
248 }
249 }
250
251 /*
252 * adjust candidates range, should select all dirty segments for
253 * foreground GC and urgent GC cases.
254 */
255 if (gc_type != FG_GC &&
256 (sbi->gc_mode != GC_URGENT_HIGH) &&
257 (p->gc_mode != GC_AT && p->alloc_mode != AT_SSR) &&
258 p->max_search > sbi->max_victim_search)
259 p->max_search = sbi->max_victim_search;
260
261 /* let's select beginning hot/small space first in no_heap mode*/
262 if (test_opt(sbi, NOHEAP) &&
263 (type == CURSEG_HOT_DATA || IS_NODESEG(type)))
264 p->offset = 0;
265 else
266 p->offset = SIT_I(sbi)->last_victim[p->gc_mode];
267 }
268
get_max_cost(struct f2fs_sb_info * sbi,struct victim_sel_policy * p)269 static unsigned int get_max_cost(struct f2fs_sb_info *sbi,
270 struct victim_sel_policy *p)
271 {
272 /* SSR allocates in a segment unit */
273 if (p->alloc_mode == SSR)
274 return sbi->blocks_per_seg;
275 else if (p->alloc_mode == AT_SSR)
276 return UINT_MAX;
277
278 /* LFS */
279 if (p->gc_mode == GC_GREEDY)
280 return 2 * sbi->blocks_per_seg * p->ofs_unit;
281 else if (p->gc_mode == GC_CB)
282 return UINT_MAX;
283 else if (p->gc_mode == GC_AT)
284 return UINT_MAX;
285 else /* No other gc_mode */
286 return 0;
287 }
288
check_bg_victims(struct f2fs_sb_info * sbi)289 static unsigned int check_bg_victims(struct f2fs_sb_info *sbi)
290 {
291 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
292 unsigned int secno;
293
294 /*
295 * If the gc_type is FG_GC, we can select victim segments
296 * selected by background GC before.
297 * Those segments guarantee they have small valid blocks.
298 */
299 for_each_set_bit(secno, dirty_i->victim_secmap, MAIN_SECS(sbi)) {
300 if (sec_usage_check(sbi, secno))
301 continue;
302 clear_bit(secno, dirty_i->victim_secmap);
303 return GET_SEG_FROM_SEC(sbi, secno);
304 }
305 return NULL_SEGNO;
306 }
307
get_cb_cost(struct f2fs_sb_info * sbi,unsigned int segno)308 static unsigned int get_cb_cost(struct f2fs_sb_info *sbi, unsigned int segno)
309 {
310 struct sit_info *sit_i = SIT_I(sbi);
311 unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
312 unsigned int start = GET_SEG_FROM_SEC(sbi, secno);
313 unsigned long long mtime = 0;
314 unsigned int vblocks;
315 unsigned char age = 0;
316 unsigned char u;
317 unsigned int i;
318 unsigned int usable_segs_per_sec = f2fs_usable_segs_in_sec(sbi, segno);
319
320 for (i = 0; i < usable_segs_per_sec; i++)
321 mtime += get_seg_entry(sbi, start + i)->mtime;
322 vblocks = get_valid_blocks(sbi, segno, true);
323
324 mtime = div_u64(mtime, usable_segs_per_sec);
325 vblocks = div_u64(vblocks, usable_segs_per_sec);
326
327 u = (vblocks * 100) >> sbi->log_blocks_per_seg;
328
329 /* Handle if the system time has changed by the user */
330 if (mtime < sit_i->min_mtime)
331 sit_i->min_mtime = mtime;
332 if (mtime > sit_i->max_mtime)
333 sit_i->max_mtime = mtime;
334 if (sit_i->max_mtime != sit_i->min_mtime)
335 age = 100 - div64_u64(100 * (mtime - sit_i->min_mtime),
336 sit_i->max_mtime - sit_i->min_mtime);
337
338 return UINT_MAX - ((100 * (100 - u) * age) / (100 + u));
339 }
340
get_gc_cost(struct f2fs_sb_info * sbi,unsigned int segno,struct victim_sel_policy * p)341 static inline unsigned int get_gc_cost(struct f2fs_sb_info *sbi,
342 unsigned int segno, struct victim_sel_policy *p)
343 {
344 if (p->alloc_mode == SSR)
345 return get_seg_entry(sbi, segno)->ckpt_valid_blocks;
346
347 /* alloc_mode == LFS */
348 if (p->gc_mode == GC_GREEDY)
349 return get_valid_blocks(sbi, segno, true);
350 else if (p->gc_mode == GC_CB)
351 return get_cb_cost(sbi, segno);
352
353 f2fs_bug_on(sbi, 1);
354 return 0;
355 }
356
count_bits(const unsigned long * addr,unsigned int offset,unsigned int len)357 static unsigned int count_bits(const unsigned long *addr,
358 unsigned int offset, unsigned int len)
359 {
360 unsigned int end = offset + len, sum = 0;
361
362 while (offset < end) {
363 if (test_bit(offset++, addr))
364 ++sum;
365 }
366 return sum;
367 }
368
attach_victim_entry(struct f2fs_sb_info * sbi,unsigned long long mtime,unsigned int segno,struct rb_node * parent,struct rb_node ** p,bool left_most)369 static struct victim_entry *attach_victim_entry(struct f2fs_sb_info *sbi,
370 unsigned long long mtime, unsigned int segno,
371 struct rb_node *parent, struct rb_node **p,
372 bool left_most)
373 {
374 struct atgc_management *am = &sbi->am;
375 struct victim_entry *ve;
376
377 ve = f2fs_kmem_cache_alloc(victim_entry_slab, GFP_NOFS);
378
379 ve->mtime = mtime;
380 ve->segno = segno;
381
382 rb_link_node(&ve->rb_node, parent, p);
383 rb_insert_color_cached(&ve->rb_node, &am->root, left_most);
384
385 list_add_tail(&ve->list, &am->victim_list);
386
387 am->victim_count++;
388
389 return ve;
390 }
391
insert_victim_entry(struct f2fs_sb_info * sbi,unsigned long long mtime,unsigned int segno)392 static void insert_victim_entry(struct f2fs_sb_info *sbi,
393 unsigned long long mtime, unsigned int segno)
394 {
395 struct atgc_management *am = &sbi->am;
396 struct rb_node **p;
397 struct rb_node *parent = NULL;
398 bool left_most = true;
399
400 p = f2fs_lookup_rb_tree_ext(sbi, &am->root, &parent, mtime, &left_most);
401 attach_victim_entry(sbi, mtime, segno, parent, p, left_most);
402 }
403
add_victim_entry(struct f2fs_sb_info * sbi,struct victim_sel_policy * p,unsigned int segno)404 static void add_victim_entry(struct f2fs_sb_info *sbi,
405 struct victim_sel_policy *p, unsigned int segno)
406 {
407 struct sit_info *sit_i = SIT_I(sbi);
408 unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
409 unsigned int start = GET_SEG_FROM_SEC(sbi, secno);
410 unsigned long long mtime = 0;
411 unsigned int i;
412
413 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
414 if (p->gc_mode == GC_AT &&
415 get_valid_blocks(sbi, segno, true) == 0)
416 return;
417 }
418
419 for (i = 0; i < sbi->segs_per_sec; i++)
420 mtime += get_seg_entry(sbi, start + i)->mtime;
421 mtime = div_u64(mtime, sbi->segs_per_sec);
422
423 /* Handle if the system time has changed by the user */
424 if (mtime < sit_i->min_mtime)
425 sit_i->min_mtime = mtime;
426 if (mtime > sit_i->max_mtime)
427 sit_i->max_mtime = mtime;
428 if (mtime < sit_i->dirty_min_mtime)
429 sit_i->dirty_min_mtime = mtime;
430 if (mtime > sit_i->dirty_max_mtime)
431 sit_i->dirty_max_mtime = mtime;
432
433 /* don't choose young section as candidate */
434 if (sit_i->dirty_max_mtime - mtime < p->age_threshold)
435 return;
436
437 insert_victim_entry(sbi, mtime, segno);
438 }
439
lookup_central_victim(struct f2fs_sb_info * sbi,struct victim_sel_policy * p)440 static struct rb_node *lookup_central_victim(struct f2fs_sb_info *sbi,
441 struct victim_sel_policy *p)
442 {
443 struct atgc_management *am = &sbi->am;
444 struct rb_node *parent = NULL;
445 bool left_most;
446
447 f2fs_lookup_rb_tree_ext(sbi, &am->root, &parent, p->age, &left_most);
448
449 return parent;
450 }
451
atgc_lookup_victim(struct f2fs_sb_info * sbi,struct victim_sel_policy * p)452 static void atgc_lookup_victim(struct f2fs_sb_info *sbi,
453 struct victim_sel_policy *p)
454 {
455 struct sit_info *sit_i = SIT_I(sbi);
456 struct atgc_management *am = &sbi->am;
457 struct rb_root_cached *root = &am->root;
458 struct rb_node *node;
459 struct rb_entry *re;
460 struct victim_entry *ve;
461 unsigned long long total_time;
462 unsigned long long age, u, accu;
463 unsigned long long max_mtime = sit_i->dirty_max_mtime;
464 unsigned long long min_mtime = sit_i->dirty_min_mtime;
465 unsigned int sec_blocks = BLKS_PER_SEC(sbi);
466 unsigned int vblocks;
467 unsigned int dirty_threshold = max(am->max_candidate_count,
468 am->candidate_ratio *
469 am->victim_count / 100);
470 unsigned int age_weight = am->age_weight;
471 unsigned int cost;
472 unsigned int iter = 0;
473
474 if (max_mtime < min_mtime)
475 return;
476
477 max_mtime += 1;
478 total_time = max_mtime - min_mtime;
479
480 accu = div64_u64(ULLONG_MAX, total_time);
481 accu = min_t(unsigned long long, div_u64(accu, 100),
482 DEFAULT_ACCURACY_CLASS);
483
484 node = rb_first_cached(root);
485 next:
486 re = rb_entry_safe(node, struct rb_entry, rb_node);
487 if (!re)
488 return;
489
490 ve = (struct victim_entry *)re;
491
492 if (ve->mtime >= max_mtime || ve->mtime < min_mtime)
493 goto skip;
494
495 /* age = 10000 * x% * 60 */
496 age = div64_u64(accu * (max_mtime - ve->mtime), total_time) *
497 age_weight;
498
499 vblocks = get_valid_blocks(sbi, ve->segno, true);
500 f2fs_bug_on(sbi, !vblocks || vblocks == sec_blocks);
501
502 /* u = 10000 * x% * 40 */
503 u = div64_u64(accu * (sec_blocks - vblocks), sec_blocks) *
504 (100 - age_weight);
505
506 f2fs_bug_on(sbi, age + u >= UINT_MAX);
507
508 cost = UINT_MAX - (age + u);
509 iter++;
510
511 if (cost < p->min_cost ||
512 (cost == p->min_cost && age > p->oldest_age)) {
513 p->min_cost = cost;
514 p->oldest_age = age;
515 p->min_segno = ve->segno;
516 }
517 skip:
518 if (iter < dirty_threshold) {
519 node = rb_next(node);
520 goto next;
521 }
522 }
523
524 /*
525 * select candidates around source section in range of
526 * [target - dirty_threshold, target + dirty_threshold]
527 */
atssr_lookup_victim(struct f2fs_sb_info * sbi,struct victim_sel_policy * p)528 static void atssr_lookup_victim(struct f2fs_sb_info *sbi,
529 struct victim_sel_policy *p)
530 {
531 struct sit_info *sit_i = SIT_I(sbi);
532 struct atgc_management *am = &sbi->am;
533 struct rb_node *node;
534 struct rb_entry *re;
535 struct victim_entry *ve;
536 unsigned long long age;
537 unsigned long long max_mtime = sit_i->dirty_max_mtime;
538 unsigned long long min_mtime = sit_i->dirty_min_mtime;
539 unsigned int seg_blocks = sbi->blocks_per_seg;
540 unsigned int vblocks;
541 unsigned int dirty_threshold = max(am->max_candidate_count,
542 am->candidate_ratio *
543 am->victim_count / 100);
544 unsigned int cost;
545 unsigned int iter = 0;
546 int stage = 0;
547
548 if (max_mtime < min_mtime)
549 return;
550 max_mtime += 1;
551 next_stage:
552 node = lookup_central_victim(sbi, p);
553 next_node:
554 re = rb_entry_safe(node, struct rb_entry, rb_node);
555 if (!re) {
556 if (stage == 0)
557 goto skip_stage;
558 return;
559 }
560
561 ve = (struct victim_entry *)re;
562
563 if (ve->mtime >= max_mtime || ve->mtime < min_mtime)
564 goto skip_node;
565
566 age = max_mtime - ve->mtime;
567
568 vblocks = get_seg_entry(sbi, ve->segno)->ckpt_valid_blocks;
569 f2fs_bug_on(sbi, !vblocks);
570
571 /* rare case */
572 if (vblocks == seg_blocks)
573 goto skip_node;
574
575 iter++;
576
577 age = max_mtime - abs(p->age - age);
578 cost = UINT_MAX - vblocks;
579
580 if (cost < p->min_cost ||
581 (cost == p->min_cost && age > p->oldest_age)) {
582 p->min_cost = cost;
583 p->oldest_age = age;
584 p->min_segno = ve->segno;
585 }
586 skip_node:
587 if (iter < dirty_threshold) {
588 if (stage == 0)
589 node = rb_prev(node);
590 else if (stage == 1)
591 node = rb_next(node);
592 goto next_node;
593 }
594 skip_stage:
595 if (stage < 1) {
596 stage++;
597 iter = 0;
598 goto next_stage;
599 }
600 }
lookup_victim_by_age(struct f2fs_sb_info * sbi,struct victim_sel_policy * p)601 static void lookup_victim_by_age(struct f2fs_sb_info *sbi,
602 struct victim_sel_policy *p)
603 {
604 f2fs_bug_on(sbi, !f2fs_check_rb_tree_consistence(sbi,
605 &sbi->am.root, true));
606
607 if (p->gc_mode == GC_AT)
608 atgc_lookup_victim(sbi, p);
609 else if (p->alloc_mode == AT_SSR)
610 atssr_lookup_victim(sbi, p);
611 else
612 f2fs_bug_on(sbi, 1);
613 }
614
release_victim_entry(struct f2fs_sb_info * sbi)615 static void release_victim_entry(struct f2fs_sb_info *sbi)
616 {
617 struct atgc_management *am = &sbi->am;
618 struct victim_entry *ve, *tmp;
619
620 list_for_each_entry_safe(ve, tmp, &am->victim_list, list) {
621 list_del(&ve->list);
622 kmem_cache_free(victim_entry_slab, ve);
623 am->victim_count--;
624 }
625
626 am->root = RB_ROOT_CACHED;
627
628 f2fs_bug_on(sbi, am->victim_count);
629 f2fs_bug_on(sbi, !list_empty(&am->victim_list));
630 }
631
f2fs_pin_section(struct f2fs_sb_info * sbi,unsigned int segno)632 static bool f2fs_pin_section(struct f2fs_sb_info *sbi, unsigned int segno)
633 {
634 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
635 unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
636
637 if (!dirty_i->enable_pin_section)
638 return false;
639 if (!test_and_set_bit(secno, dirty_i->pinned_secmap))
640 dirty_i->pinned_secmap_cnt++;
641 return true;
642 }
643
f2fs_pinned_section_exists(struct dirty_seglist_info * dirty_i)644 static bool f2fs_pinned_section_exists(struct dirty_seglist_info *dirty_i)
645 {
646 return dirty_i->pinned_secmap_cnt;
647 }
648
f2fs_section_is_pinned(struct dirty_seglist_info * dirty_i,unsigned int secno)649 static bool f2fs_section_is_pinned(struct dirty_seglist_info *dirty_i,
650 unsigned int secno)
651 {
652 return dirty_i->enable_pin_section &&
653 f2fs_pinned_section_exists(dirty_i) &&
654 test_bit(secno, dirty_i->pinned_secmap);
655 }
656
f2fs_unpin_all_sections(struct f2fs_sb_info * sbi,bool enable)657 static void f2fs_unpin_all_sections(struct f2fs_sb_info *sbi, bool enable)
658 {
659 unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
660
661 if (f2fs_pinned_section_exists(DIRTY_I(sbi))) {
662 memset(DIRTY_I(sbi)->pinned_secmap, 0, bitmap_size);
663 DIRTY_I(sbi)->pinned_secmap_cnt = 0;
664 }
665 DIRTY_I(sbi)->enable_pin_section = enable;
666 }
667
f2fs_gc_pinned_control(struct inode * inode,int gc_type,unsigned int segno)668 static int f2fs_gc_pinned_control(struct inode *inode, int gc_type,
669 unsigned int segno)
670 {
671 if (!f2fs_is_pinned_file(inode))
672 return 0;
673 if (gc_type != FG_GC)
674 return -EBUSY;
675 if (!f2fs_pin_section(F2FS_I_SB(inode), segno))
676 f2fs_pin_file_control(inode, true);
677 return -EAGAIN;
678 }
679
680 /*
681 * This function is called from two paths.
682 * One is garbage collection and the other is SSR segment selection.
683 * When it is called during GC, it just gets a victim segment
684 * and it does not remove it from dirty seglist.
685 * When it is called from SSR segment selection, it finds a segment
686 * which has minimum valid blocks and removes it from dirty seglist.
687 */
get_victim_by_default(struct f2fs_sb_info * sbi,unsigned int * result,int gc_type,int type,char alloc_mode,unsigned long long age)688 static int get_victim_by_default(struct f2fs_sb_info *sbi,
689 unsigned int *result, int gc_type, int type,
690 char alloc_mode, unsigned long long age)
691 {
692 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
693 struct sit_info *sm = SIT_I(sbi);
694 struct victim_sel_policy p;
695 unsigned int secno, last_victim;
696 unsigned int last_segment;
697 unsigned int nsearched;
698 bool is_atgc;
699 int ret = 0;
700
701 mutex_lock(&dirty_i->seglist_lock);
702 last_segment = MAIN_SECS(sbi) * sbi->segs_per_sec;
703
704 p.alloc_mode = alloc_mode;
705 p.age = age;
706 p.age_threshold = sbi->am.age_threshold;
707
708 retry:
709 select_policy(sbi, gc_type, type, &p);
710 p.min_segno = NULL_SEGNO;
711 p.oldest_age = 0;
712 p.min_cost = get_max_cost(sbi, &p);
713
714 is_atgc = (p.gc_mode == GC_AT || p.alloc_mode == AT_SSR);
715 nsearched = 0;
716
717 if (is_atgc)
718 SIT_I(sbi)->dirty_min_mtime = ULLONG_MAX;
719
720 if (*result != NULL_SEGNO) {
721 if (!get_valid_blocks(sbi, *result, false)) {
722 ret = -ENODATA;
723 goto out;
724 }
725
726 if (sec_usage_check(sbi, GET_SEC_FROM_SEG(sbi, *result)))
727 ret = -EBUSY;
728 else
729 p.min_segno = *result;
730 goto out;
731 }
732
733 ret = -ENODATA;
734 if (p.max_search == 0)
735 goto out;
736
737 if (__is_large_section(sbi) && p.alloc_mode == LFS) {
738 if (sbi->next_victim_seg[BG_GC] != NULL_SEGNO) {
739 p.min_segno = sbi->next_victim_seg[BG_GC];
740 *result = p.min_segno;
741 sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
742 goto got_result;
743 }
744 if (gc_type == FG_GC &&
745 sbi->next_victim_seg[FG_GC] != NULL_SEGNO) {
746 p.min_segno = sbi->next_victim_seg[FG_GC];
747 *result = p.min_segno;
748 sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
749 goto got_result;
750 }
751 }
752
753 last_victim = sm->last_victim[p.gc_mode];
754 if (p.alloc_mode == LFS && gc_type == FG_GC) {
755 p.min_segno = check_bg_victims(sbi);
756 if (p.min_segno != NULL_SEGNO)
757 goto got_it;
758 }
759
760 while (1) {
761 unsigned long cost, *dirty_bitmap;
762 unsigned int unit_no, segno;
763
764 dirty_bitmap = p.dirty_bitmap;
765 unit_no = find_next_bit(dirty_bitmap,
766 last_segment / p.ofs_unit,
767 p.offset / p.ofs_unit);
768 segno = unit_no * p.ofs_unit;
769 if (segno >= last_segment) {
770 if (sm->last_victim[p.gc_mode]) {
771 last_segment =
772 sm->last_victim[p.gc_mode];
773 sm->last_victim[p.gc_mode] = 0;
774 p.offset = 0;
775 continue;
776 }
777 break;
778 }
779
780 p.offset = segno + p.ofs_unit;
781 nsearched++;
782
783 #ifdef CONFIG_F2FS_CHECK_FS
784 /*
785 * skip selecting the invalid segno (that is failed due to block
786 * validity check failure during GC) to avoid endless GC loop in
787 * such cases.
788 */
789 if (test_bit(segno, sm->invalid_segmap))
790 goto next;
791 #endif
792
793 secno = GET_SEC_FROM_SEG(sbi, segno);
794
795 if (sec_usage_check(sbi, secno))
796 goto next;
797
798 /* Don't touch checkpointed data */
799 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
800 if (p.alloc_mode == LFS) {
801 /*
802 * LFS is set to find source section during GC.
803 * The victim should have no checkpointed data.
804 */
805 if (get_ckpt_valid_blocks(sbi, segno, true))
806 goto next;
807 } else {
808 /*
809 * SSR | AT_SSR are set to find target segment
810 * for writes which can be full by checkpointed
811 * and newly written blocks.
812 */
813 if (!f2fs_segment_has_free_slot(sbi, segno))
814 goto next;
815 }
816 }
817
818 if (gc_type == BG_GC && test_bit(secno, dirty_i->victim_secmap))
819 goto next;
820
821 if (gc_type == FG_GC && f2fs_section_is_pinned(dirty_i, secno))
822 goto next;
823
824 if (is_atgc) {
825 add_victim_entry(sbi, &p, segno);
826 goto next;
827 }
828
829 cost = get_gc_cost(sbi, segno, &p);
830
831 if (p.min_cost > cost) {
832 p.min_segno = segno;
833 p.min_cost = cost;
834 }
835 next:
836 if (nsearched >= p.max_search) {
837 if (!sm->last_victim[p.gc_mode] && segno <= last_victim)
838 sm->last_victim[p.gc_mode] =
839 last_victim + p.ofs_unit;
840 else
841 sm->last_victim[p.gc_mode] = segno + p.ofs_unit;
842 sm->last_victim[p.gc_mode] %=
843 (MAIN_SECS(sbi) * sbi->segs_per_sec);
844 break;
845 }
846 }
847
848 /* get victim for GC_AT/AT_SSR */
849 if (is_atgc) {
850 lookup_victim_by_age(sbi, &p);
851 release_victim_entry(sbi);
852 }
853
854 if (is_atgc && p.min_segno == NULL_SEGNO &&
855 sm->elapsed_time < p.age_threshold) {
856 p.age_threshold = 0;
857 goto retry;
858 }
859
860 if (p.min_segno != NULL_SEGNO) {
861 got_it:
862 *result = (p.min_segno / p.ofs_unit) * p.ofs_unit;
863 got_result:
864 if (p.alloc_mode == LFS) {
865 secno = GET_SEC_FROM_SEG(sbi, p.min_segno);
866 if (gc_type == FG_GC)
867 sbi->cur_victim_sec = secno;
868 else
869 set_bit(secno, dirty_i->victim_secmap);
870 }
871 ret = 0;
872
873 }
874 out:
875 if (p.min_segno != NULL_SEGNO)
876 trace_f2fs_get_victim(sbi->sb, type, gc_type, &p,
877 sbi->cur_victim_sec,
878 prefree_segments(sbi), free_segments(sbi));
879 mutex_unlock(&dirty_i->seglist_lock);
880
881 return ret;
882 }
883
884 static const struct victim_selection default_v_ops = {
885 .get_victim = get_victim_by_default,
886 };
887
find_gc_inode(struct gc_inode_list * gc_list,nid_t ino)888 static struct inode *find_gc_inode(struct gc_inode_list *gc_list, nid_t ino)
889 {
890 struct inode_entry *ie;
891
892 ie = radix_tree_lookup(&gc_list->iroot, ino);
893 if (ie)
894 return ie->inode;
895 return NULL;
896 }
897
add_gc_inode(struct gc_inode_list * gc_list,struct inode * inode)898 static void add_gc_inode(struct gc_inode_list *gc_list, struct inode *inode)
899 {
900 struct inode_entry *new_ie;
901
902 if (inode == find_gc_inode(gc_list, inode->i_ino)) {
903 iput(inode);
904 return;
905 }
906 new_ie = f2fs_kmem_cache_alloc(f2fs_inode_entry_slab, GFP_NOFS);
907 new_ie->inode = inode;
908
909 f2fs_radix_tree_insert(&gc_list->iroot, inode->i_ino, new_ie);
910 list_add_tail(&new_ie->list, &gc_list->ilist);
911 }
912
put_gc_inode(struct gc_inode_list * gc_list)913 static void put_gc_inode(struct gc_inode_list *gc_list)
914 {
915 struct inode_entry *ie, *next_ie;
916
917 list_for_each_entry_safe(ie, next_ie, &gc_list->ilist, list) {
918 radix_tree_delete(&gc_list->iroot, ie->inode->i_ino);
919 iput(ie->inode);
920 list_del(&ie->list);
921 kmem_cache_free(f2fs_inode_entry_slab, ie);
922 }
923 }
924
check_valid_map(struct f2fs_sb_info * sbi,unsigned int segno,int offset)925 static int check_valid_map(struct f2fs_sb_info *sbi,
926 unsigned int segno, int offset)
927 {
928 struct sit_info *sit_i = SIT_I(sbi);
929 struct seg_entry *sentry;
930 int ret;
931
932 down_read(&sit_i->sentry_lock);
933 sentry = get_seg_entry(sbi, segno);
934 ret = f2fs_test_bit(offset, sentry->cur_valid_map);
935 up_read(&sit_i->sentry_lock);
936 return ret;
937 }
938
939 /*
940 * This function compares node address got in summary with that in NAT.
941 * On validity, copy that node with cold status, otherwise (invalid node)
942 * ignore that.
943 */
gc_node_segment(struct f2fs_sb_info * sbi,struct f2fs_summary * sum,unsigned int segno,int gc_type)944 static int gc_node_segment(struct f2fs_sb_info *sbi,
945 struct f2fs_summary *sum, unsigned int segno, int gc_type)
946 {
947 struct f2fs_summary *entry;
948 block_t start_addr;
949 int off;
950 int phase = 0;
951 bool fggc = (gc_type == FG_GC);
952 int submitted = 0;
953 unsigned int usable_blks_in_seg = f2fs_usable_blks_in_seg(sbi, segno);
954
955 start_addr = START_BLOCK(sbi, segno);
956
957 next_step:
958 entry = sum;
959
960 if (fggc && phase == 2)
961 atomic_inc(&sbi->wb_sync_req[NODE]);
962
963 for (off = 0; off < usable_blks_in_seg; off++, entry++) {
964 nid_t nid = le32_to_cpu(entry->nid);
965 struct page *node_page;
966 struct node_info ni;
967 int err;
968
969 /* stop BG_GC if there is not enough free sections. */
970 if (gc_type == BG_GC && has_not_enough_free_secs(sbi, 0, 0))
971 return submitted;
972
973 if (check_valid_map(sbi, segno, off) == 0)
974 continue;
975
976 if (phase == 0) {
977 f2fs_ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), 1,
978 META_NAT, true);
979 continue;
980 }
981
982 if (phase == 1) {
983 f2fs_ra_node_page(sbi, nid);
984 continue;
985 }
986
987 /* phase == 2 */
988 node_page = f2fs_get_node_page(sbi, nid);
989 if (IS_ERR(node_page))
990 continue;
991
992 /* block may become invalid during f2fs_get_node_page */
993 if (check_valid_map(sbi, segno, off) == 0) {
994 f2fs_put_page(node_page, 1);
995 continue;
996 }
997
998 if (f2fs_get_node_info(sbi, nid, &ni, false)) {
999 f2fs_put_page(node_page, 1);
1000 continue;
1001 }
1002
1003 if (ni.blk_addr != start_addr + off) {
1004 f2fs_put_page(node_page, 1);
1005 continue;
1006 }
1007
1008 err = f2fs_move_node_page(node_page, gc_type);
1009 if (!err && gc_type == FG_GC)
1010 submitted++;
1011 stat_inc_node_blk_count(sbi, 1, gc_type);
1012 }
1013
1014 if (++phase < 3)
1015 goto next_step;
1016
1017 if (fggc)
1018 atomic_dec(&sbi->wb_sync_req[NODE]);
1019 return submitted;
1020 }
1021
1022 /*
1023 * Calculate start block index indicating the given node offset.
1024 * Be careful, caller should give this node offset only indicating direct node
1025 * blocks. If any node offsets, which point the other types of node blocks such
1026 * as indirect or double indirect node blocks, are given, it must be a caller's
1027 * bug.
1028 */
f2fs_start_bidx_of_node(unsigned int node_ofs,struct inode * inode)1029 block_t f2fs_start_bidx_of_node(unsigned int node_ofs, struct inode *inode)
1030 {
1031 unsigned int indirect_blks = 2 * NIDS_PER_BLOCK + 4;
1032 unsigned int bidx;
1033
1034 if (node_ofs == 0)
1035 return 0;
1036
1037 if (node_ofs <= 2) {
1038 bidx = node_ofs - 1;
1039 } else if (node_ofs <= indirect_blks) {
1040 int dec = (node_ofs - 4) / (NIDS_PER_BLOCK + 1);
1041
1042 bidx = node_ofs - 2 - dec;
1043 } else {
1044 int dec = (node_ofs - indirect_blks - 3) / (NIDS_PER_BLOCK + 1);
1045
1046 bidx = node_ofs - 5 - dec;
1047 }
1048 return bidx * ADDRS_PER_BLOCK(inode) + ADDRS_PER_INODE(inode);
1049 }
1050
is_alive(struct f2fs_sb_info * sbi,struct f2fs_summary * sum,struct node_info * dni,block_t blkaddr,unsigned int * nofs)1051 static bool is_alive(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
1052 struct node_info *dni, block_t blkaddr, unsigned int *nofs)
1053 {
1054 struct page *node_page;
1055 nid_t nid;
1056 unsigned int ofs_in_node, max_addrs, base;
1057 block_t source_blkaddr;
1058
1059 nid = le32_to_cpu(sum->nid);
1060 ofs_in_node = le16_to_cpu(sum->ofs_in_node);
1061
1062 node_page = f2fs_get_node_page(sbi, nid);
1063 if (IS_ERR(node_page))
1064 return false;
1065
1066 if (f2fs_get_node_info(sbi, nid, dni, false)) {
1067 f2fs_put_page(node_page, 1);
1068 return false;
1069 }
1070
1071 if (sum->version != dni->version) {
1072 f2fs_warn(sbi, "%s: valid data with mismatched node version.",
1073 __func__);
1074 set_sbi_flag(sbi, SBI_NEED_FSCK);
1075 }
1076
1077 if (f2fs_check_nid_range(sbi, dni->ino)) {
1078 f2fs_put_page(node_page, 1);
1079 return false;
1080 }
1081
1082 if (IS_INODE(node_page)) {
1083 base = offset_in_addr(F2FS_INODE(node_page));
1084 max_addrs = DEF_ADDRS_PER_INODE;
1085 } else {
1086 base = 0;
1087 max_addrs = DEF_ADDRS_PER_BLOCK;
1088 }
1089
1090 if (base + ofs_in_node >= max_addrs) {
1091 f2fs_err(sbi, "Inconsistent blkaddr offset: base:%u, ofs_in_node:%u, max:%u, ino:%u, nid:%u",
1092 base, ofs_in_node, max_addrs, dni->ino, dni->nid);
1093 f2fs_put_page(node_page, 1);
1094 return false;
1095 }
1096
1097 *nofs = ofs_of_node(node_page);
1098 source_blkaddr = data_blkaddr(NULL, node_page, ofs_in_node);
1099 f2fs_put_page(node_page, 1);
1100
1101 if (source_blkaddr != blkaddr) {
1102 #ifdef CONFIG_F2FS_CHECK_FS
1103 unsigned int segno = GET_SEGNO(sbi, blkaddr);
1104 unsigned long offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
1105
1106 if (unlikely(check_valid_map(sbi, segno, offset))) {
1107 if (!test_and_set_bit(segno, SIT_I(sbi)->invalid_segmap)) {
1108 f2fs_err(sbi, "mismatched blkaddr %u (source_blkaddr %u) in seg %u",
1109 blkaddr, source_blkaddr, segno);
1110 f2fs_bug_on(sbi, 1);
1111 }
1112 }
1113 #endif
1114 return false;
1115 }
1116 return true;
1117 }
1118
ra_data_block(struct inode * inode,pgoff_t index)1119 static int ra_data_block(struct inode *inode, pgoff_t index)
1120 {
1121 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1122 struct address_space *mapping = inode->i_mapping;
1123 struct dnode_of_data dn;
1124 struct page *page;
1125 struct extent_info ei = {0, };
1126 struct f2fs_io_info fio = {
1127 .sbi = sbi,
1128 .ino = inode->i_ino,
1129 .type = DATA,
1130 .temp = COLD,
1131 .op = REQ_OP_READ,
1132 .op_flags = 0,
1133 .encrypted_page = NULL,
1134 .in_list = false,
1135 .retry = false,
1136 };
1137 int err;
1138
1139 page = f2fs_grab_cache_page(mapping, index, true);
1140 if (!page)
1141 return -ENOMEM;
1142
1143 if (f2fs_lookup_read_extent_cache(inode, index, &ei)) {
1144 dn.data_blkaddr = ei.blk + index - ei.fofs;
1145 if (unlikely(!f2fs_is_valid_blkaddr(sbi, dn.data_blkaddr,
1146 DATA_GENERIC_ENHANCE_READ))) {
1147 err = -EFSCORRUPTED;
1148 goto put_page;
1149 }
1150 goto got_it;
1151 }
1152
1153 set_new_dnode(&dn, inode, NULL, NULL, 0);
1154 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
1155 if (err)
1156 goto put_page;
1157 f2fs_put_dnode(&dn);
1158
1159 if (!__is_valid_data_blkaddr(dn.data_blkaddr)) {
1160 err = -ENOENT;
1161 goto put_page;
1162 }
1163 if (unlikely(!f2fs_is_valid_blkaddr(sbi, dn.data_blkaddr,
1164 DATA_GENERIC_ENHANCE))) {
1165 err = -EFSCORRUPTED;
1166 goto put_page;
1167 }
1168 got_it:
1169 /* read page */
1170 fio.page = page;
1171 fio.new_blkaddr = fio.old_blkaddr = dn.data_blkaddr;
1172
1173 /*
1174 * don't cache encrypted data into meta inode until previous dirty
1175 * data were writebacked to avoid racing between GC and flush.
1176 */
1177 f2fs_wait_on_page_writeback(page, DATA, true, true);
1178
1179 f2fs_wait_on_block_writeback(inode, dn.data_blkaddr);
1180
1181 fio.encrypted_page = f2fs_pagecache_get_page(META_MAPPING(sbi),
1182 dn.data_blkaddr,
1183 FGP_LOCK | FGP_CREAT, GFP_NOFS);
1184 if (!fio.encrypted_page) {
1185 err = -ENOMEM;
1186 goto put_page;
1187 }
1188
1189 err = f2fs_submit_page_bio(&fio);
1190 if (err)
1191 goto put_encrypted_page;
1192 f2fs_put_page(fio.encrypted_page, 0);
1193 f2fs_put_page(page, 1);
1194
1195 f2fs_update_iostat(sbi, FS_DATA_READ_IO, F2FS_BLKSIZE);
1196 f2fs_update_iostat(sbi, FS_GDATA_READ_IO, F2FS_BLKSIZE);
1197
1198 return 0;
1199 put_encrypted_page:
1200 f2fs_put_page(fio.encrypted_page, 1);
1201 put_page:
1202 f2fs_put_page(page, 1);
1203 return err;
1204 }
1205
1206 /*
1207 * Move data block via META_MAPPING while keeping locked data page.
1208 * This can be used to move blocks, aka LBAs, directly on disk.
1209 */
move_data_block(struct inode * inode,block_t bidx,int gc_type,unsigned int segno,int off)1210 static int move_data_block(struct inode *inode, block_t bidx,
1211 int gc_type, unsigned int segno, int off)
1212 {
1213 struct f2fs_io_info fio = {
1214 .sbi = F2FS_I_SB(inode),
1215 .ino = inode->i_ino,
1216 .type = DATA,
1217 .temp = COLD,
1218 .op = REQ_OP_READ,
1219 .op_flags = 0,
1220 .encrypted_page = NULL,
1221 .in_list = false,
1222 .retry = false,
1223 };
1224 struct dnode_of_data dn;
1225 struct f2fs_summary sum;
1226 struct node_info ni;
1227 struct page *page, *mpage;
1228 block_t newaddr;
1229 int err = 0;
1230 bool lfs_mode = f2fs_lfs_mode(fio.sbi);
1231 int type = fio.sbi->am.atgc_enabled && (gc_type == BG_GC) &&
1232 (fio.sbi->gc_mode != GC_URGENT_HIGH) ?
1233 CURSEG_ALL_DATA_ATGC : CURSEG_COLD_DATA;
1234
1235 /* do not read out */
1236 page = f2fs_grab_cache_page(inode->i_mapping, bidx, false);
1237 if (!page)
1238 return -ENOMEM;
1239
1240 if (!check_valid_map(F2FS_I_SB(inode), segno, off)) {
1241 err = -ENOENT;
1242 goto out;
1243 }
1244
1245 if (f2fs_is_atomic_file(inode)) {
1246 F2FS_I(inode)->i_gc_failures[GC_FAILURE_ATOMIC]++;
1247 F2FS_I_SB(inode)->skipped_atomic_files[gc_type]++;
1248 err = -EAGAIN;
1249 goto out;
1250 }
1251
1252 err = f2fs_gc_pinned_control(inode, gc_type, segno);
1253 if (err)
1254 goto out;
1255
1256 set_new_dnode(&dn, inode, NULL, NULL, 0);
1257 err = f2fs_get_dnode_of_data(&dn, bidx, LOOKUP_NODE);
1258 if (err)
1259 goto out;
1260
1261 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
1262 ClearPageUptodate(page);
1263 err = -ENOENT;
1264 goto put_out;
1265 }
1266
1267 /*
1268 * don't cache encrypted data into meta inode until previous dirty
1269 * data were writebacked to avoid racing between GC and flush.
1270 */
1271 f2fs_wait_on_page_writeback(page, DATA, true, true);
1272
1273 f2fs_wait_on_block_writeback(inode, dn.data_blkaddr);
1274
1275 err = f2fs_get_node_info(fio.sbi, dn.nid, &ni, false);
1276 if (err)
1277 goto put_out;
1278
1279 /* read page */
1280 fio.page = page;
1281 fio.new_blkaddr = fio.old_blkaddr = dn.data_blkaddr;
1282
1283 if (lfs_mode)
1284 f2fs_down_write(&fio.sbi->io_order_lock);
1285
1286 mpage = f2fs_grab_cache_page(META_MAPPING(fio.sbi),
1287 fio.old_blkaddr, false);
1288 if (!mpage) {
1289 err = -ENOMEM;
1290 goto up_out;
1291 }
1292
1293 fio.encrypted_page = mpage;
1294
1295 /* read source block in mpage */
1296 if (!PageUptodate(mpage)) {
1297 err = f2fs_submit_page_bio(&fio);
1298 if (err) {
1299 f2fs_put_page(mpage, 1);
1300 goto up_out;
1301 }
1302
1303 f2fs_update_iostat(fio.sbi, FS_DATA_READ_IO, F2FS_BLKSIZE);
1304 f2fs_update_iostat(fio.sbi, FS_GDATA_READ_IO, F2FS_BLKSIZE);
1305
1306 lock_page(mpage);
1307 if (unlikely(mpage->mapping != META_MAPPING(fio.sbi) ||
1308 !PageUptodate(mpage))) {
1309 err = -EIO;
1310 f2fs_put_page(mpage, 1);
1311 goto up_out;
1312 }
1313 }
1314
1315 set_summary(&sum, dn.nid, dn.ofs_in_node, ni.version);
1316
1317 /* allocate block address */
1318 f2fs_allocate_data_block(fio.sbi, NULL, fio.old_blkaddr, &newaddr,
1319 &sum, type, NULL);
1320
1321 fio.encrypted_page = f2fs_pagecache_get_page(META_MAPPING(fio.sbi),
1322 newaddr, FGP_LOCK | FGP_CREAT, GFP_NOFS);
1323 if (!fio.encrypted_page) {
1324 err = -ENOMEM;
1325 f2fs_put_page(mpage, 1);
1326 goto recover_block;
1327 }
1328
1329 /* write target block */
1330 f2fs_wait_on_page_writeback(fio.encrypted_page, DATA, true, true);
1331 memcpy(page_address(fio.encrypted_page),
1332 page_address(mpage), PAGE_SIZE);
1333 f2fs_put_page(mpage, 1);
1334 invalidate_mapping_pages(META_MAPPING(fio.sbi),
1335 fio.old_blkaddr, fio.old_blkaddr);
1336 f2fs_invalidate_compress_page(fio.sbi, fio.old_blkaddr);
1337
1338 set_page_dirty(fio.encrypted_page);
1339 if (clear_page_dirty_for_io(fio.encrypted_page))
1340 dec_page_count(fio.sbi, F2FS_DIRTY_META);
1341
1342 set_page_writeback(fio.encrypted_page);
1343 ClearPageError(page);
1344
1345 fio.op = REQ_OP_WRITE;
1346 fio.op_flags = REQ_SYNC;
1347 fio.new_blkaddr = newaddr;
1348 f2fs_submit_page_write(&fio);
1349 if (fio.retry) {
1350 err = -EAGAIN;
1351 if (PageWriteback(fio.encrypted_page))
1352 end_page_writeback(fio.encrypted_page);
1353 goto put_page_out;
1354 }
1355
1356 f2fs_update_iostat(fio.sbi, FS_GC_DATA_IO, F2FS_BLKSIZE);
1357
1358 f2fs_update_data_blkaddr(&dn, newaddr);
1359 set_inode_flag(inode, FI_APPEND_WRITE);
1360 if (page->index == 0)
1361 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
1362 put_page_out:
1363 f2fs_put_page(fio.encrypted_page, 1);
1364 recover_block:
1365 if (err)
1366 f2fs_do_replace_block(fio.sbi, &sum, newaddr, fio.old_blkaddr,
1367 true, true, true);
1368 up_out:
1369 if (lfs_mode)
1370 f2fs_up_write(&fio.sbi->io_order_lock);
1371 put_out:
1372 f2fs_put_dnode(&dn);
1373 out:
1374 f2fs_put_page(page, 1);
1375 return err;
1376 }
1377
move_data_page(struct inode * inode,block_t bidx,int gc_type,unsigned int segno,int off)1378 static int move_data_page(struct inode *inode, block_t bidx, int gc_type,
1379 unsigned int segno, int off)
1380 {
1381 struct page *page;
1382 int err = 0;
1383
1384 page = f2fs_get_lock_data_page(inode, bidx, true);
1385 if (IS_ERR(page))
1386 return PTR_ERR(page);
1387
1388 if (!check_valid_map(F2FS_I_SB(inode), segno, off)) {
1389 err = -ENOENT;
1390 goto out;
1391 }
1392
1393 if (f2fs_is_atomic_file(inode)) {
1394 F2FS_I(inode)->i_gc_failures[GC_FAILURE_ATOMIC]++;
1395 F2FS_I_SB(inode)->skipped_atomic_files[gc_type]++;
1396 err = -EAGAIN;
1397 goto out;
1398 }
1399 err = f2fs_gc_pinned_control(inode, gc_type, segno);
1400 if (err)
1401 goto out;
1402
1403 if (gc_type == BG_GC) {
1404 if (PageWriteback(page)) {
1405 err = -EAGAIN;
1406 goto out;
1407 }
1408 set_page_dirty(page);
1409 set_page_private_gcing(page);
1410 } else {
1411 struct f2fs_io_info fio = {
1412 .sbi = F2FS_I_SB(inode),
1413 .ino = inode->i_ino,
1414 .type = DATA,
1415 .temp = COLD,
1416 .op = REQ_OP_WRITE,
1417 .op_flags = REQ_SYNC,
1418 .old_blkaddr = NULL_ADDR,
1419 .page = page,
1420 .encrypted_page = NULL,
1421 .need_lock = LOCK_REQ,
1422 .io_type = FS_GC_DATA_IO,
1423 };
1424 bool is_dirty = PageDirty(page);
1425
1426 retry:
1427 f2fs_wait_on_page_writeback(page, DATA, true, true);
1428
1429 set_page_dirty(page);
1430 if (clear_page_dirty_for_io(page)) {
1431 inode_dec_dirty_pages(inode);
1432 f2fs_remove_dirty_inode(inode);
1433 }
1434
1435 set_page_private_gcing(page);
1436
1437 err = f2fs_do_write_data_page(&fio);
1438 if (err) {
1439 clear_page_private_gcing(page);
1440 if (err == -ENOMEM) {
1441 congestion_wait(BLK_RW_ASYNC,
1442 DEFAULT_IO_TIMEOUT);
1443 goto retry;
1444 }
1445 if (is_dirty)
1446 set_page_dirty(page);
1447 }
1448 }
1449 out:
1450 f2fs_put_page(page, 1);
1451 return err;
1452 }
1453
1454 /*
1455 * This function tries to get parent node of victim data block, and identifies
1456 * data block validity. If the block is valid, copy that with cold status and
1457 * modify parent node.
1458 * If the parent node is not valid or the data block address is different,
1459 * the victim data block is ignored.
1460 */
gc_data_segment(struct f2fs_sb_info * sbi,struct f2fs_summary * sum,struct gc_inode_list * gc_list,unsigned int segno,int gc_type,bool force_migrate)1461 static int gc_data_segment(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
1462 struct gc_inode_list *gc_list, unsigned int segno, int gc_type,
1463 bool force_migrate)
1464 {
1465 struct super_block *sb = sbi->sb;
1466 struct f2fs_summary *entry;
1467 block_t start_addr;
1468 int off;
1469 int phase = 0;
1470 int submitted = 0;
1471 unsigned int usable_blks_in_seg = f2fs_usable_blks_in_seg(sbi, segno);
1472
1473 start_addr = START_BLOCK(sbi, segno);
1474
1475 next_step:
1476 entry = sum;
1477
1478 for (off = 0; off < usable_blks_in_seg; off++, entry++) {
1479 struct page *data_page;
1480 struct inode *inode;
1481 struct node_info dni; /* dnode info for the data */
1482 unsigned int ofs_in_node, nofs;
1483 block_t start_bidx;
1484 nid_t nid = le32_to_cpu(entry->nid);
1485
1486 /*
1487 * stop BG_GC if there is not enough free sections.
1488 * Or, stop GC if the segment becomes fully valid caused by
1489 * race condition along with SSR block allocation.
1490 */
1491 if ((gc_type == BG_GC && has_not_enough_free_secs(sbi, 0, 0)) ||
1492 (!force_migrate && get_valid_blocks(sbi, segno, true) ==
1493 BLKS_PER_SEC(sbi)))
1494 return submitted;
1495
1496 if (check_valid_map(sbi, segno, off) == 0)
1497 continue;
1498
1499 if (phase == 0) {
1500 f2fs_ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), 1,
1501 META_NAT, true);
1502 continue;
1503 }
1504
1505 if (phase == 1) {
1506 f2fs_ra_node_page(sbi, nid);
1507 continue;
1508 }
1509
1510 /* Get an inode by ino with checking validity */
1511 if (!is_alive(sbi, entry, &dni, start_addr + off, &nofs))
1512 continue;
1513
1514 if (phase == 2) {
1515 f2fs_ra_node_page(sbi, dni.ino);
1516 continue;
1517 }
1518
1519 ofs_in_node = le16_to_cpu(entry->ofs_in_node);
1520
1521 if (phase == 3) {
1522 int err;
1523
1524 inode = f2fs_iget(sb, dni.ino);
1525 if (IS_ERR(inode) || is_bad_inode(inode) ||
1526 special_file(inode->i_mode))
1527 continue;
1528
1529 err = f2fs_gc_pinned_control(inode, gc_type, segno);
1530 if (err == -EAGAIN) {
1531 iput(inode);
1532 return submitted;
1533 }
1534
1535 if (!f2fs_down_write_trylock(
1536 &F2FS_I(inode)->i_gc_rwsem[WRITE])) {
1537 iput(inode);
1538 sbi->skipped_gc_rwsem++;
1539 continue;
1540 }
1541
1542 start_bidx = f2fs_start_bidx_of_node(nofs, inode) +
1543 ofs_in_node;
1544
1545 if (f2fs_post_read_required(inode)) {
1546 int err = ra_data_block(inode, start_bidx);
1547
1548 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1549 if (err) {
1550 iput(inode);
1551 continue;
1552 }
1553 add_gc_inode(gc_list, inode);
1554 continue;
1555 }
1556
1557 data_page = f2fs_get_read_data_page(inode,
1558 start_bidx, REQ_RAHEAD, true);
1559 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1560 if (IS_ERR(data_page)) {
1561 iput(inode);
1562 continue;
1563 }
1564
1565 f2fs_put_page(data_page, 0);
1566 add_gc_inode(gc_list, inode);
1567 continue;
1568 }
1569
1570 /* phase 4 */
1571 inode = find_gc_inode(gc_list, dni.ino);
1572 if (inode) {
1573 struct f2fs_inode_info *fi = F2FS_I(inode);
1574 bool locked = false;
1575 int err;
1576
1577 if (S_ISREG(inode->i_mode)) {
1578 if (!f2fs_down_write_trylock(&fi->i_gc_rwsem[READ])) {
1579 sbi->skipped_gc_rwsem++;
1580 continue;
1581 }
1582 if (!f2fs_down_write_trylock(
1583 &fi->i_gc_rwsem[WRITE])) {
1584 sbi->skipped_gc_rwsem++;
1585 f2fs_up_write(&fi->i_gc_rwsem[READ]);
1586 continue;
1587 }
1588 locked = true;
1589
1590 /* wait for all inflight aio data */
1591 inode_dio_wait(inode);
1592 }
1593
1594 start_bidx = f2fs_start_bidx_of_node(nofs, inode)
1595 + ofs_in_node;
1596 if (f2fs_post_read_required(inode))
1597 err = move_data_block(inode, start_bidx,
1598 gc_type, segno, off);
1599 else
1600 err = move_data_page(inode, start_bidx, gc_type,
1601 segno, off);
1602
1603 if (!err && (gc_type == FG_GC ||
1604 f2fs_post_read_required(inode)))
1605 submitted++;
1606
1607 if (locked) {
1608 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
1609 f2fs_up_write(&fi->i_gc_rwsem[READ]);
1610 }
1611
1612 stat_inc_data_blk_count(sbi, 1, gc_type);
1613 }
1614 }
1615
1616 if (++phase < 5)
1617 goto next_step;
1618
1619 return submitted;
1620 }
1621
__get_victim(struct f2fs_sb_info * sbi,unsigned int * victim,int gc_type)1622 static int __get_victim(struct f2fs_sb_info *sbi, unsigned int *victim,
1623 int gc_type)
1624 {
1625 struct sit_info *sit_i = SIT_I(sbi);
1626 int ret;
1627
1628 down_write(&sit_i->sentry_lock);
1629 ret = DIRTY_I(sbi)->v_ops->get_victim(sbi, victim, gc_type,
1630 NO_CHECK_TYPE, LFS, 0);
1631 up_write(&sit_i->sentry_lock);
1632 return ret;
1633 }
1634
do_garbage_collect(struct f2fs_sb_info * sbi,unsigned int start_segno,struct gc_inode_list * gc_list,int gc_type,bool force_migrate)1635 static int do_garbage_collect(struct f2fs_sb_info *sbi,
1636 unsigned int start_segno,
1637 struct gc_inode_list *gc_list, int gc_type,
1638 bool force_migrate)
1639 {
1640 struct page *sum_page;
1641 struct f2fs_summary_block *sum;
1642 struct blk_plug plug;
1643 unsigned int segno = start_segno;
1644 unsigned int end_segno = start_segno + sbi->segs_per_sec;
1645 int seg_freed = 0, migrated = 0;
1646 unsigned char type = IS_DATASEG(get_seg_entry(sbi, segno)->type) ?
1647 SUM_TYPE_DATA : SUM_TYPE_NODE;
1648 int submitted = 0;
1649
1650 if (__is_large_section(sbi))
1651 end_segno = rounddown(end_segno, sbi->segs_per_sec);
1652
1653 /*
1654 * zone-capacity can be less than zone-size in zoned devices,
1655 * resulting in less than expected usable segments in the zone,
1656 * calculate the end segno in the zone which can be garbage collected
1657 */
1658 if (f2fs_sb_has_blkzoned(sbi))
1659 end_segno -= sbi->segs_per_sec -
1660 f2fs_usable_segs_in_sec(sbi, segno);
1661
1662 sanity_check_seg_type(sbi, get_seg_entry(sbi, segno)->type);
1663
1664 /* readahead multi ssa blocks those have contiguous address */
1665 if (__is_large_section(sbi))
1666 f2fs_ra_meta_pages(sbi, GET_SUM_BLOCK(sbi, segno),
1667 end_segno - segno, META_SSA, true);
1668
1669 /* reference all summary page */
1670 while (segno < end_segno) {
1671 sum_page = f2fs_get_sum_page(sbi, segno++);
1672 if (IS_ERR(sum_page)) {
1673 int err = PTR_ERR(sum_page);
1674
1675 end_segno = segno - 1;
1676 for (segno = start_segno; segno < end_segno; segno++) {
1677 sum_page = find_get_page(META_MAPPING(sbi),
1678 GET_SUM_BLOCK(sbi, segno));
1679 f2fs_put_page(sum_page, 0);
1680 f2fs_put_page(sum_page, 0);
1681 }
1682 return err;
1683 }
1684 unlock_page(sum_page);
1685 }
1686
1687 blk_start_plug(&plug);
1688
1689 for (segno = start_segno; segno < end_segno; segno++) {
1690
1691 /* find segment summary of victim */
1692 sum_page = find_get_page(META_MAPPING(sbi),
1693 GET_SUM_BLOCK(sbi, segno));
1694 f2fs_put_page(sum_page, 0);
1695
1696 if (get_valid_blocks(sbi, segno, false) == 0)
1697 goto freed;
1698 if (gc_type == BG_GC && __is_large_section(sbi) &&
1699 migrated >= sbi->migration_granularity)
1700 goto skip;
1701 if (!PageUptodate(sum_page) || unlikely(f2fs_cp_error(sbi)))
1702 goto skip;
1703
1704 sum = page_address(sum_page);
1705 if (type != GET_SUM_TYPE((&sum->footer))) {
1706 f2fs_err(sbi, "Inconsistent segment (%u) type [%d, %d] in SSA and SIT",
1707 segno, type, GET_SUM_TYPE((&sum->footer)));
1708 set_sbi_flag(sbi, SBI_NEED_FSCK);
1709 f2fs_stop_checkpoint(sbi, false,
1710 STOP_CP_REASON_CORRUPTED_SUMMARY);
1711 goto skip;
1712 }
1713
1714 /*
1715 * this is to avoid deadlock:
1716 * - lock_page(sum_page) - f2fs_replace_block
1717 * - check_valid_map() - down_write(sentry_lock)
1718 * - down_read(sentry_lock) - change_curseg()
1719 * - lock_page(sum_page)
1720 */
1721 if (type == SUM_TYPE_NODE)
1722 submitted += gc_node_segment(sbi, sum->entries, segno,
1723 gc_type);
1724 else
1725 submitted += gc_data_segment(sbi, sum->entries, gc_list,
1726 segno, gc_type,
1727 force_migrate);
1728
1729 stat_inc_seg_count(sbi, type, gc_type);
1730 sbi->gc_reclaimed_segs[sbi->gc_mode]++;
1731 migrated++;
1732
1733 freed:
1734 if (gc_type == FG_GC &&
1735 get_valid_blocks(sbi, segno, false) == 0)
1736 seg_freed++;
1737
1738 if (__is_large_section(sbi))
1739 sbi->next_victim_seg[gc_type] =
1740 (segno + 1 < end_segno) ? segno + 1 : NULL_SEGNO;
1741 skip:
1742 f2fs_put_page(sum_page, 0);
1743 }
1744
1745 if (submitted)
1746 f2fs_submit_merged_write(sbi,
1747 (type == SUM_TYPE_NODE) ? NODE : DATA);
1748
1749 blk_finish_plug(&plug);
1750
1751 stat_inc_call_count(sbi->stat_info);
1752
1753 return seg_freed;
1754 }
1755
f2fs_gc(struct f2fs_sb_info * sbi,bool sync,bool background,bool force,unsigned int segno)1756 int f2fs_gc(struct f2fs_sb_info *sbi, bool sync,
1757 bool background, bool force, unsigned int segno)
1758 {
1759 int gc_type = sync ? FG_GC : BG_GC;
1760 int sec_freed = 0, seg_freed = 0, total_freed = 0;
1761 int ret = 0;
1762 struct cp_control cpc;
1763 unsigned int init_segno = segno;
1764 struct gc_inode_list gc_list = {
1765 .ilist = LIST_HEAD_INIT(gc_list.ilist),
1766 .iroot = RADIX_TREE_INIT(gc_list.iroot, GFP_NOFS),
1767 };
1768 unsigned long long last_skipped = sbi->skipped_atomic_files[FG_GC];
1769 unsigned long long first_skipped;
1770 unsigned int skipped_round = 0, round = 0;
1771
1772 trace_f2fs_gc_begin(sbi->sb, sync, background,
1773 get_pages(sbi, F2FS_DIRTY_NODES),
1774 get_pages(sbi, F2FS_DIRTY_DENTS),
1775 get_pages(sbi, F2FS_DIRTY_IMETA),
1776 free_sections(sbi),
1777 free_segments(sbi),
1778 reserved_segments(sbi),
1779 prefree_segments(sbi));
1780
1781 cpc.reason = __get_cp_reason(sbi);
1782 sbi->skipped_gc_rwsem = 0;
1783 first_skipped = last_skipped;
1784 gc_more:
1785 if (unlikely(!(sbi->sb->s_flags & SB_ACTIVE))) {
1786 ret = -EINVAL;
1787 goto stop;
1788 }
1789 if (unlikely(f2fs_cp_error(sbi))) {
1790 ret = -EIO;
1791 goto stop;
1792 }
1793
1794 if (gc_type == BG_GC && has_not_enough_free_secs(sbi, 0, 0)) {
1795 /*
1796 * For example, if there are many prefree_segments below given
1797 * threshold, we can make them free by checkpoint. Then, we
1798 * secure free segments which doesn't need fggc any more.
1799 */
1800 if (prefree_segments(sbi) &&
1801 !is_sbi_flag_set(sbi, SBI_CP_DISABLED)) {
1802 ret = f2fs_write_checkpoint(sbi, &cpc);
1803 if (ret)
1804 goto stop;
1805 }
1806 if (has_not_enough_free_secs(sbi, 0, 0))
1807 gc_type = FG_GC;
1808 }
1809
1810 /* f2fs_balance_fs doesn't need to do BG_GC in critical path. */
1811 if (gc_type == BG_GC && !background) {
1812 ret = -EINVAL;
1813 goto stop;
1814 }
1815 retry:
1816 ret = __get_victim(sbi, &segno, gc_type);
1817 if (ret) {
1818 /* allow to search victim from sections has pinned data */
1819 if (ret == -ENODATA && gc_type == FG_GC &&
1820 f2fs_pinned_section_exists(DIRTY_I(sbi))) {
1821 f2fs_unpin_all_sections(sbi, false);
1822 goto retry;
1823 }
1824 goto stop;
1825 }
1826
1827 seg_freed = do_garbage_collect(sbi, segno, &gc_list, gc_type, force);
1828 if (gc_type == FG_GC &&
1829 seg_freed == f2fs_usable_segs_in_sec(sbi, segno))
1830 sec_freed++;
1831 total_freed += seg_freed;
1832
1833 if (gc_type == FG_GC) {
1834 if (sbi->skipped_atomic_files[FG_GC] > last_skipped ||
1835 sbi->skipped_gc_rwsem)
1836 skipped_round++;
1837 last_skipped = sbi->skipped_atomic_files[FG_GC];
1838 round++;
1839 }
1840
1841 if (gc_type == FG_GC && seg_freed)
1842 sbi->cur_victim_sec = NULL_SEGNO;
1843
1844 if (sync)
1845 goto stop;
1846
1847 if (!has_not_enough_free_secs(sbi, sec_freed, 0))
1848 goto stop;
1849
1850 if (skipped_round <= MAX_SKIP_GC_COUNT || skipped_round * 2 < round) {
1851
1852 /* Write checkpoint to reclaim prefree segments */
1853 if (free_sections(sbi) < NR_CURSEG_PERSIST_TYPE &&
1854 prefree_segments(sbi) &&
1855 !is_sbi_flag_set(sbi, SBI_CP_DISABLED)) {
1856 ret = f2fs_write_checkpoint(sbi, &cpc);
1857 if (ret)
1858 goto stop;
1859 }
1860 segno = NULL_SEGNO;
1861 goto gc_more;
1862 }
1863 if (first_skipped < last_skipped &&
1864 (last_skipped - first_skipped) >
1865 sbi->skipped_gc_rwsem) {
1866 f2fs_drop_inmem_pages_all(sbi, true);
1867 segno = NULL_SEGNO;
1868 goto gc_more;
1869 }
1870 if (gc_type == FG_GC && !is_sbi_flag_set(sbi, SBI_CP_DISABLED))
1871 ret = f2fs_write_checkpoint(sbi, &cpc);
1872 stop:
1873 SIT_I(sbi)->last_victim[ALLOC_NEXT] = 0;
1874 SIT_I(sbi)->last_victim[FLUSH_DEVICE] = init_segno;
1875
1876 if (gc_type == FG_GC)
1877 f2fs_unpin_all_sections(sbi, true);
1878
1879 trace_f2fs_gc_end(sbi->sb, ret, total_freed, sec_freed,
1880 get_pages(sbi, F2FS_DIRTY_NODES),
1881 get_pages(sbi, F2FS_DIRTY_DENTS),
1882 get_pages(sbi, F2FS_DIRTY_IMETA),
1883 free_sections(sbi),
1884 free_segments(sbi),
1885 reserved_segments(sbi),
1886 prefree_segments(sbi));
1887
1888 f2fs_up_write(&sbi->gc_lock);
1889
1890 put_gc_inode(&gc_list);
1891
1892 if (sync && !ret)
1893 ret = sec_freed ? 0 : -EAGAIN;
1894 return ret;
1895 }
1896
f2fs_create_garbage_collection_cache(void)1897 int __init f2fs_create_garbage_collection_cache(void)
1898 {
1899 victim_entry_slab = f2fs_kmem_cache_create("f2fs_victim_entry",
1900 sizeof(struct victim_entry));
1901 if (!victim_entry_slab)
1902 return -ENOMEM;
1903 return 0;
1904 }
1905
f2fs_destroy_garbage_collection_cache(void)1906 void f2fs_destroy_garbage_collection_cache(void)
1907 {
1908 kmem_cache_destroy(victim_entry_slab);
1909 }
1910
init_atgc_management(struct f2fs_sb_info * sbi)1911 static void init_atgc_management(struct f2fs_sb_info *sbi)
1912 {
1913 struct atgc_management *am = &sbi->am;
1914
1915 if (test_opt(sbi, ATGC) &&
1916 SIT_I(sbi)->elapsed_time >= DEF_GC_THREAD_AGE_THRESHOLD)
1917 am->atgc_enabled = true;
1918
1919 am->root = RB_ROOT_CACHED;
1920 INIT_LIST_HEAD(&am->victim_list);
1921 am->victim_count = 0;
1922
1923 am->candidate_ratio = DEF_GC_THREAD_CANDIDATE_RATIO;
1924 am->max_candidate_count = DEF_GC_THREAD_MAX_CANDIDATE_COUNT;
1925 am->age_weight = DEF_GC_THREAD_AGE_WEIGHT;
1926 am->age_threshold = DEF_GC_THREAD_AGE_THRESHOLD;
1927 }
1928
f2fs_build_gc_manager(struct f2fs_sb_info * sbi)1929 void f2fs_build_gc_manager(struct f2fs_sb_info *sbi)
1930 {
1931 DIRTY_I(sbi)->v_ops = &default_v_ops;
1932
1933 sbi->gc_pin_file_threshold = DEF_GC_FAILED_PINNED_FILES;
1934
1935 /* give warm/cold data area from slower device */
1936 if (f2fs_is_multi_device(sbi) && !__is_large_section(sbi))
1937 SIT_I(sbi)->last_victim[ALLOC_NEXT] =
1938 GET_SEGNO(sbi, FDEV(0).end_blk) + 1;
1939
1940 init_atgc_management(sbi);
1941 }
1942
free_segment_range(struct f2fs_sb_info * sbi,unsigned int secs,bool gc_only)1943 static int free_segment_range(struct f2fs_sb_info *sbi,
1944 unsigned int secs, bool gc_only)
1945 {
1946 unsigned int segno, next_inuse, start, end;
1947 struct cp_control cpc = { CP_RESIZE, 0, 0, 0 };
1948 int gc_mode, gc_type;
1949 int err = 0;
1950 int type;
1951
1952 /* Force block allocation for GC */
1953 MAIN_SECS(sbi) -= secs;
1954 start = MAIN_SECS(sbi) * sbi->segs_per_sec;
1955 end = MAIN_SEGS(sbi) - 1;
1956
1957 mutex_lock(&DIRTY_I(sbi)->seglist_lock);
1958 for (gc_mode = 0; gc_mode < MAX_GC_POLICY; gc_mode++)
1959 if (SIT_I(sbi)->last_victim[gc_mode] >= start)
1960 SIT_I(sbi)->last_victim[gc_mode] = 0;
1961
1962 for (gc_type = BG_GC; gc_type <= FG_GC; gc_type++)
1963 if (sbi->next_victim_seg[gc_type] >= start)
1964 sbi->next_victim_seg[gc_type] = NULL_SEGNO;
1965 mutex_unlock(&DIRTY_I(sbi)->seglist_lock);
1966
1967 /* Move out cursegs from the target range */
1968 for (type = CURSEG_HOT_DATA; type < NR_CURSEG_PERSIST_TYPE; type++)
1969 f2fs_allocate_segment_for_resize(sbi, type, start, end);
1970
1971 /* do GC to move out valid blocks in the range */
1972 for (segno = start; segno <= end; segno += sbi->segs_per_sec) {
1973 struct gc_inode_list gc_list = {
1974 .ilist = LIST_HEAD_INIT(gc_list.ilist),
1975 .iroot = RADIX_TREE_INIT(gc_list.iroot, GFP_NOFS),
1976 };
1977
1978 do_garbage_collect(sbi, segno, &gc_list, FG_GC, true);
1979 put_gc_inode(&gc_list);
1980
1981 if (!gc_only && get_valid_blocks(sbi, segno, true)) {
1982 err = -EAGAIN;
1983 goto out;
1984 }
1985 if (fatal_signal_pending(current)) {
1986 err = -ERESTARTSYS;
1987 goto out;
1988 }
1989 }
1990 if (gc_only)
1991 goto out;
1992
1993 err = f2fs_write_checkpoint(sbi, &cpc);
1994 if (err)
1995 goto out;
1996
1997 next_inuse = find_next_inuse(FREE_I(sbi), end + 1, start);
1998 if (next_inuse <= end) {
1999 f2fs_err(sbi, "segno %u should be free but still inuse!",
2000 next_inuse);
2001 f2fs_bug_on(sbi, 1);
2002 }
2003 out:
2004 MAIN_SECS(sbi) += secs;
2005 return err;
2006 }
2007
update_sb_metadata(struct f2fs_sb_info * sbi,int secs)2008 static void update_sb_metadata(struct f2fs_sb_info *sbi, int secs)
2009 {
2010 struct f2fs_super_block *raw_sb = F2FS_RAW_SUPER(sbi);
2011 int section_count;
2012 int segment_count;
2013 int segment_count_main;
2014 long long block_count;
2015 int segs = secs * sbi->segs_per_sec;
2016
2017 f2fs_down_write(&sbi->sb_lock);
2018
2019 section_count = le32_to_cpu(raw_sb->section_count);
2020 segment_count = le32_to_cpu(raw_sb->segment_count);
2021 segment_count_main = le32_to_cpu(raw_sb->segment_count_main);
2022 block_count = le64_to_cpu(raw_sb->block_count);
2023
2024 raw_sb->section_count = cpu_to_le32(section_count + secs);
2025 raw_sb->segment_count = cpu_to_le32(segment_count + segs);
2026 raw_sb->segment_count_main = cpu_to_le32(segment_count_main + segs);
2027 raw_sb->block_count = cpu_to_le64(block_count +
2028 (long long)segs * sbi->blocks_per_seg);
2029 if (f2fs_is_multi_device(sbi)) {
2030 int last_dev = sbi->s_ndevs - 1;
2031 int dev_segs =
2032 le32_to_cpu(raw_sb->devs[last_dev].total_segments);
2033
2034 raw_sb->devs[last_dev].total_segments =
2035 cpu_to_le32(dev_segs + segs);
2036 }
2037
2038 f2fs_up_write(&sbi->sb_lock);
2039 }
2040
update_fs_metadata(struct f2fs_sb_info * sbi,int secs)2041 static void update_fs_metadata(struct f2fs_sb_info *sbi, int secs)
2042 {
2043 int segs = secs * sbi->segs_per_sec;
2044 long long blks = (long long)segs * sbi->blocks_per_seg;
2045 long long user_block_count =
2046 le64_to_cpu(F2FS_CKPT(sbi)->user_block_count);
2047
2048 SM_I(sbi)->segment_count = (int)SM_I(sbi)->segment_count + segs;
2049 MAIN_SEGS(sbi) = (int)MAIN_SEGS(sbi) + segs;
2050 MAIN_SECS(sbi) += secs;
2051 FREE_I(sbi)->free_sections = (int)FREE_I(sbi)->free_sections + secs;
2052 FREE_I(sbi)->free_segments = (int)FREE_I(sbi)->free_segments + segs;
2053 F2FS_CKPT(sbi)->user_block_count = cpu_to_le64(user_block_count + blks);
2054
2055 if (f2fs_is_multi_device(sbi)) {
2056 int last_dev = sbi->s_ndevs - 1;
2057
2058 FDEV(last_dev).total_segments =
2059 (int)FDEV(last_dev).total_segments + segs;
2060 FDEV(last_dev).end_blk =
2061 (long long)FDEV(last_dev).end_blk + blks;
2062 #ifdef CONFIG_BLK_DEV_ZONED
2063 FDEV(last_dev).nr_blkz = (int)FDEV(last_dev).nr_blkz +
2064 (int)(blks >> sbi->log_blocks_per_blkz);
2065 #endif
2066 }
2067 }
2068
f2fs_resize_fs(struct file * filp,__u64 block_count)2069 int f2fs_resize_fs(struct file *filp, __u64 block_count)
2070 {
2071 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
2072 __u64 old_block_count, shrunk_blocks;
2073 struct cp_control cpc = { CP_RESIZE, 0, 0, 0 };
2074 unsigned int secs;
2075 int err = 0;
2076 __u32 rem;
2077
2078 old_block_count = le64_to_cpu(F2FS_RAW_SUPER(sbi)->block_count);
2079 if (block_count > old_block_count)
2080 return -EINVAL;
2081
2082 if (f2fs_is_multi_device(sbi)) {
2083 int last_dev = sbi->s_ndevs - 1;
2084 __u64 last_segs = FDEV(last_dev).total_segments;
2085
2086 if (block_count + last_segs * sbi->blocks_per_seg <=
2087 old_block_count)
2088 return -EINVAL;
2089 }
2090
2091 /* new fs size should align to section size */
2092 div_u64_rem(block_count, BLKS_PER_SEC(sbi), &rem);
2093 if (rem)
2094 return -EINVAL;
2095
2096 if (block_count == old_block_count)
2097 return 0;
2098
2099 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) {
2100 f2fs_err(sbi, "Should run fsck to repair first.");
2101 return -EFSCORRUPTED;
2102 }
2103
2104 if (test_opt(sbi, DISABLE_CHECKPOINT)) {
2105 f2fs_err(sbi, "Checkpoint should be enabled.");
2106 return -EINVAL;
2107 }
2108
2109 err = mnt_want_write_file(filp);
2110 if (err)
2111 return err;
2112
2113 shrunk_blocks = old_block_count - block_count;
2114 secs = div_u64(shrunk_blocks, BLKS_PER_SEC(sbi));
2115
2116 /* stop other GC */
2117 if (!f2fs_down_write_trylock(&sbi->gc_lock)) {
2118 err = -EAGAIN;
2119 goto out_drop_write;
2120 }
2121
2122 /* stop CP to protect MAIN_SEC in free_segment_range */
2123 f2fs_lock_op(sbi);
2124
2125 spin_lock(&sbi->stat_lock);
2126 if (shrunk_blocks + valid_user_blocks(sbi) +
2127 sbi->current_reserved_blocks + sbi->unusable_block_count +
2128 F2FS_OPTION(sbi).root_reserved_blocks > sbi->user_block_count)
2129 err = -ENOSPC;
2130 spin_unlock(&sbi->stat_lock);
2131
2132 if (err)
2133 goto out_unlock;
2134
2135 err = free_segment_range(sbi, secs, true);
2136
2137 out_unlock:
2138 f2fs_unlock_op(sbi);
2139 f2fs_up_write(&sbi->gc_lock);
2140 out_drop_write:
2141 mnt_drop_write_file(filp);
2142 if (err)
2143 return err;
2144
2145 freeze_super(sbi->sb);
2146
2147 if (f2fs_readonly(sbi->sb)) {
2148 thaw_super(sbi->sb);
2149 return -EROFS;
2150 }
2151
2152 f2fs_down_write(&sbi->gc_lock);
2153 f2fs_down_write(&sbi->cp_global_sem);
2154
2155 spin_lock(&sbi->stat_lock);
2156 if (shrunk_blocks + valid_user_blocks(sbi) +
2157 sbi->current_reserved_blocks + sbi->unusable_block_count +
2158 F2FS_OPTION(sbi).root_reserved_blocks > sbi->user_block_count)
2159 err = -ENOSPC;
2160 else
2161 sbi->user_block_count -= shrunk_blocks;
2162 spin_unlock(&sbi->stat_lock);
2163 if (err)
2164 goto out_err;
2165
2166 set_sbi_flag(sbi, SBI_IS_RESIZEFS);
2167 err = free_segment_range(sbi, secs, false);
2168 if (err)
2169 goto recover_out;
2170
2171 update_sb_metadata(sbi, -secs);
2172
2173 err = f2fs_commit_super(sbi, false);
2174 if (err) {
2175 update_sb_metadata(sbi, secs);
2176 goto recover_out;
2177 }
2178
2179 update_fs_metadata(sbi, -secs);
2180 clear_sbi_flag(sbi, SBI_IS_RESIZEFS);
2181 set_sbi_flag(sbi, SBI_IS_DIRTY);
2182
2183 err = f2fs_write_checkpoint(sbi, &cpc);
2184 if (err) {
2185 update_fs_metadata(sbi, secs);
2186 update_sb_metadata(sbi, secs);
2187 f2fs_commit_super(sbi, false);
2188 }
2189 recover_out:
2190 clear_sbi_flag(sbi, SBI_IS_RESIZEFS);
2191 if (err) {
2192 set_sbi_flag(sbi, SBI_NEED_FSCK);
2193 f2fs_err(sbi, "resize_fs failed, should run fsck to repair!");
2194
2195 spin_lock(&sbi->stat_lock);
2196 sbi->user_block_count += shrunk_blocks;
2197 spin_unlock(&sbi->stat_lock);
2198 }
2199 out_err:
2200 f2fs_up_write(&sbi->cp_global_sem);
2201 f2fs_up_write(&sbi->gc_lock);
2202 thaw_super(sbi->sb);
2203 return err;
2204 }
2205