1 /*
2 * fs/f2fs/segment.c
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11 #include <linux/fs.h>
12 #include <linux/f2fs_fs.h>
13 #include <linux/bio.h>
14 #include <linux/blkdev.h>
15 #include <linux/prefetch.h>
16 #include <linux/vmalloc.h>
17
18 #include "f2fs.h"
19 #include "segment.h"
20 #include "node.h"
21 #include <trace/events/f2fs.h>
22
23 /*
24 * This function balances dirty node and dentry pages.
25 * In addition, it controls garbage collection.
26 */
f2fs_balance_fs(struct f2fs_sb_info * sbi)27 void f2fs_balance_fs(struct f2fs_sb_info *sbi)
28 {
29 /*
30 * We should do GC or end up with checkpoint, if there are so many dirty
31 * dir/node pages without enough free segments.
32 */
33 if (has_not_enough_free_secs(sbi, 0)) {
34 mutex_lock(&sbi->gc_mutex);
35 f2fs_gc(sbi);
36 }
37 }
38
__locate_dirty_segment(struct f2fs_sb_info * sbi,unsigned int segno,enum dirty_type dirty_type)39 static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
40 enum dirty_type dirty_type)
41 {
42 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
43
44 /* need not be added */
45 if (IS_CURSEG(sbi, segno))
46 return;
47
48 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
49 dirty_i->nr_dirty[dirty_type]++;
50
51 if (dirty_type == DIRTY) {
52 struct seg_entry *sentry = get_seg_entry(sbi, segno);
53 enum dirty_type t = DIRTY_HOT_DATA;
54
55 dirty_type = sentry->type;
56
57 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
58 dirty_i->nr_dirty[dirty_type]++;
59
60 /* Only one bitmap should be set */
61 for (; t <= DIRTY_COLD_NODE; t++) {
62 if (t == dirty_type)
63 continue;
64 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
65 dirty_i->nr_dirty[t]--;
66 }
67 }
68 }
69
__remove_dirty_segment(struct f2fs_sb_info * sbi,unsigned int segno,enum dirty_type dirty_type)70 static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
71 enum dirty_type dirty_type)
72 {
73 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
74
75 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
76 dirty_i->nr_dirty[dirty_type]--;
77
78 if (dirty_type == DIRTY) {
79 enum dirty_type t = DIRTY_HOT_DATA;
80
81 /* clear all the bitmaps */
82 for (; t <= DIRTY_COLD_NODE; t++)
83 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
84 dirty_i->nr_dirty[t]--;
85
86 if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0)
87 clear_bit(GET_SECNO(sbi, segno),
88 dirty_i->victim_secmap);
89 }
90 }
91
92 /*
93 * Should not occur error such as -ENOMEM.
94 * Adding dirty entry into seglist is not critical operation.
95 * If a given segment is one of current working segments, it won't be added.
96 */
locate_dirty_segment(struct f2fs_sb_info * sbi,unsigned int segno)97 void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
98 {
99 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
100 unsigned short valid_blocks;
101
102 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
103 return;
104
105 mutex_lock(&dirty_i->seglist_lock);
106
107 valid_blocks = get_valid_blocks(sbi, segno, 0);
108
109 if (valid_blocks == 0) {
110 __locate_dirty_segment(sbi, segno, PRE);
111 __remove_dirty_segment(sbi, segno, DIRTY);
112 } else if (valid_blocks < sbi->blocks_per_seg) {
113 __locate_dirty_segment(sbi, segno, DIRTY);
114 } else {
115 /* Recovery routine with SSR needs this */
116 __remove_dirty_segment(sbi, segno, DIRTY);
117 }
118
119 mutex_unlock(&dirty_i->seglist_lock);
120 return;
121 }
122
123 /*
124 * Should call clear_prefree_segments after checkpoint is done.
125 */
set_prefree_as_free_segments(struct f2fs_sb_info * sbi)126 static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
127 {
128 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
129 unsigned int segno, offset = 0;
130 unsigned int total_segs = TOTAL_SEGS(sbi);
131
132 mutex_lock(&dirty_i->seglist_lock);
133 while (1) {
134 segno = find_next_bit(dirty_i->dirty_segmap[PRE], total_segs,
135 offset);
136 if (segno >= total_segs)
137 break;
138 __set_test_and_free(sbi, segno);
139 offset = segno + 1;
140 }
141 mutex_unlock(&dirty_i->seglist_lock);
142 }
143
clear_prefree_segments(struct f2fs_sb_info * sbi)144 void clear_prefree_segments(struct f2fs_sb_info *sbi)
145 {
146 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
147 unsigned int segno, offset = 0;
148 unsigned int total_segs = TOTAL_SEGS(sbi);
149
150 mutex_lock(&dirty_i->seglist_lock);
151 while (1) {
152 segno = find_next_bit(dirty_i->dirty_segmap[PRE], total_segs,
153 offset);
154 if (segno >= total_segs)
155 break;
156
157 offset = segno + 1;
158 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[PRE]))
159 dirty_i->nr_dirty[PRE]--;
160
161 /* Let's use trim */
162 if (test_opt(sbi, DISCARD))
163 blkdev_issue_discard(sbi->sb->s_bdev,
164 START_BLOCK(sbi, segno) <<
165 sbi->log_sectors_per_block,
166 1 << (sbi->log_sectors_per_block +
167 sbi->log_blocks_per_seg),
168 GFP_NOFS, 0);
169 }
170 mutex_unlock(&dirty_i->seglist_lock);
171 }
172
__mark_sit_entry_dirty(struct f2fs_sb_info * sbi,unsigned int segno)173 static void __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
174 {
175 struct sit_info *sit_i = SIT_I(sbi);
176 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap))
177 sit_i->dirty_sentries++;
178 }
179
__set_sit_entry_type(struct f2fs_sb_info * sbi,int type,unsigned int segno,int modified)180 static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
181 unsigned int segno, int modified)
182 {
183 struct seg_entry *se = get_seg_entry(sbi, segno);
184 se->type = type;
185 if (modified)
186 __mark_sit_entry_dirty(sbi, segno);
187 }
188
update_sit_entry(struct f2fs_sb_info * sbi,block_t blkaddr,int del)189 static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
190 {
191 struct seg_entry *se;
192 unsigned int segno, offset;
193 long int new_vblocks;
194
195 segno = GET_SEGNO(sbi, blkaddr);
196
197 se = get_seg_entry(sbi, segno);
198 new_vblocks = se->valid_blocks + del;
199 offset = GET_SEGOFF_FROM_SEG0(sbi, blkaddr) & (sbi->blocks_per_seg - 1);
200
201 BUG_ON((new_vblocks >> (sizeof(unsigned short) << 3) ||
202 (new_vblocks > sbi->blocks_per_seg)));
203
204 se->valid_blocks = new_vblocks;
205 se->mtime = get_mtime(sbi);
206 SIT_I(sbi)->max_mtime = se->mtime;
207
208 /* Update valid block bitmap */
209 if (del > 0) {
210 if (f2fs_set_bit(offset, se->cur_valid_map))
211 BUG();
212 } else {
213 if (!f2fs_clear_bit(offset, se->cur_valid_map))
214 BUG();
215 }
216 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
217 se->ckpt_valid_blocks += del;
218
219 __mark_sit_entry_dirty(sbi, segno);
220
221 /* update total number of valid blocks to be written in ckpt area */
222 SIT_I(sbi)->written_valid_blocks += del;
223
224 if (sbi->segs_per_sec > 1)
225 get_sec_entry(sbi, segno)->valid_blocks += del;
226 }
227
refresh_sit_entry(struct f2fs_sb_info * sbi,block_t old_blkaddr,block_t new_blkaddr)228 static void refresh_sit_entry(struct f2fs_sb_info *sbi,
229 block_t old_blkaddr, block_t new_blkaddr)
230 {
231 update_sit_entry(sbi, new_blkaddr, 1);
232 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
233 update_sit_entry(sbi, old_blkaddr, -1);
234 }
235
invalidate_blocks(struct f2fs_sb_info * sbi,block_t addr)236 void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
237 {
238 unsigned int segno = GET_SEGNO(sbi, addr);
239 struct sit_info *sit_i = SIT_I(sbi);
240
241 BUG_ON(addr == NULL_ADDR);
242 if (addr == NEW_ADDR)
243 return;
244
245 /* add it into sit main buffer */
246 mutex_lock(&sit_i->sentry_lock);
247
248 update_sit_entry(sbi, addr, -1);
249
250 /* add it into dirty seglist */
251 locate_dirty_segment(sbi, segno);
252
253 mutex_unlock(&sit_i->sentry_lock);
254 }
255
256 /*
257 * This function should be resided under the curseg_mutex lock
258 */
__add_sum_entry(struct f2fs_sb_info * sbi,int type,struct f2fs_summary * sum,unsigned short offset)259 static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
260 struct f2fs_summary *sum, unsigned short offset)
261 {
262 struct curseg_info *curseg = CURSEG_I(sbi, type);
263 void *addr = curseg->sum_blk;
264 addr += offset * sizeof(struct f2fs_summary);
265 memcpy(addr, sum, sizeof(struct f2fs_summary));
266 return;
267 }
268
269 /*
270 * Calculate the number of current summary pages for writing
271 */
npages_for_summary_flush(struct f2fs_sb_info * sbi)272 int npages_for_summary_flush(struct f2fs_sb_info *sbi)
273 {
274 int total_size_bytes = 0;
275 int valid_sum_count = 0;
276 int i, sum_space;
277
278 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
279 if (sbi->ckpt->alloc_type[i] == SSR)
280 valid_sum_count += sbi->blocks_per_seg;
281 else
282 valid_sum_count += curseg_blkoff(sbi, i);
283 }
284
285 total_size_bytes = valid_sum_count * (SUMMARY_SIZE + 1)
286 + sizeof(struct nat_journal) + 2
287 + sizeof(struct sit_journal) + 2;
288 sum_space = PAGE_CACHE_SIZE - SUM_FOOTER_SIZE;
289 if (total_size_bytes < sum_space)
290 return 1;
291 else if (total_size_bytes < 2 * sum_space)
292 return 2;
293 return 3;
294 }
295
296 /*
297 * Caller should put this summary page
298 */
get_sum_page(struct f2fs_sb_info * sbi,unsigned int segno)299 struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
300 {
301 return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
302 }
303
write_sum_page(struct f2fs_sb_info * sbi,struct f2fs_summary_block * sum_blk,block_t blk_addr)304 static void write_sum_page(struct f2fs_sb_info *sbi,
305 struct f2fs_summary_block *sum_blk, block_t blk_addr)
306 {
307 struct page *page = grab_meta_page(sbi, blk_addr);
308 void *kaddr = page_address(page);
309 memcpy(kaddr, sum_blk, PAGE_CACHE_SIZE);
310 set_page_dirty(page);
311 f2fs_put_page(page, 1);
312 }
313
check_prefree_segments(struct f2fs_sb_info * sbi,int type)314 static unsigned int check_prefree_segments(struct f2fs_sb_info *sbi, int type)
315 {
316 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
317 unsigned long *prefree_segmap = dirty_i->dirty_segmap[PRE];
318 unsigned int segno;
319 unsigned int ofs = 0;
320
321 /*
322 * If there is not enough reserved sections,
323 * we should not reuse prefree segments.
324 */
325 if (has_not_enough_free_secs(sbi, 0))
326 return NULL_SEGNO;
327
328 /*
329 * NODE page should not reuse prefree segment,
330 * since those information is used for SPOR.
331 */
332 if (IS_NODESEG(type))
333 return NULL_SEGNO;
334 next:
335 segno = find_next_bit(prefree_segmap, TOTAL_SEGS(sbi), ofs);
336 ofs += sbi->segs_per_sec;
337
338 if (segno < TOTAL_SEGS(sbi)) {
339 int i;
340
341 /* skip intermediate segments in a section */
342 if (segno % sbi->segs_per_sec)
343 goto next;
344
345 /* skip if the section is currently used */
346 if (sec_usage_check(sbi, GET_SECNO(sbi, segno)))
347 goto next;
348
349 /* skip if whole section is not prefree */
350 for (i = 1; i < sbi->segs_per_sec; i++)
351 if (!test_bit(segno + i, prefree_segmap))
352 goto next;
353
354 /* skip if whole section was not free at the last checkpoint */
355 for (i = 0; i < sbi->segs_per_sec; i++)
356 if (get_seg_entry(sbi, segno + i)->ckpt_valid_blocks)
357 goto next;
358
359 return segno;
360 }
361 return NULL_SEGNO;
362 }
363
is_next_segment_free(struct f2fs_sb_info * sbi,int type)364 static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
365 {
366 struct curseg_info *curseg = CURSEG_I(sbi, type);
367 unsigned int segno = curseg->segno;
368 struct free_segmap_info *free_i = FREE_I(sbi);
369
370 if (segno + 1 < TOTAL_SEGS(sbi) && (segno + 1) % sbi->segs_per_sec)
371 return !test_bit(segno + 1, free_i->free_segmap);
372 return 0;
373 }
374
375 /*
376 * Find a new segment from the free segments bitmap to right order
377 * This function should be returned with success, otherwise BUG
378 */
get_new_segment(struct f2fs_sb_info * sbi,unsigned int * newseg,bool new_sec,int dir)379 static void get_new_segment(struct f2fs_sb_info *sbi,
380 unsigned int *newseg, bool new_sec, int dir)
381 {
382 struct free_segmap_info *free_i = FREE_I(sbi);
383 unsigned int segno, secno, zoneno;
384 unsigned int total_zones = TOTAL_SECS(sbi) / sbi->secs_per_zone;
385 unsigned int hint = *newseg / sbi->segs_per_sec;
386 unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
387 unsigned int left_start = hint;
388 bool init = true;
389 int go_left = 0;
390 int i;
391
392 write_lock(&free_i->segmap_lock);
393
394 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
395 segno = find_next_zero_bit(free_i->free_segmap,
396 TOTAL_SEGS(sbi), *newseg + 1);
397 if (segno - *newseg < sbi->segs_per_sec -
398 (*newseg % sbi->segs_per_sec))
399 goto got_it;
400 }
401 find_other_zone:
402 secno = find_next_zero_bit(free_i->free_secmap, TOTAL_SECS(sbi), hint);
403 if (secno >= TOTAL_SECS(sbi)) {
404 if (dir == ALLOC_RIGHT) {
405 secno = find_next_zero_bit(free_i->free_secmap,
406 TOTAL_SECS(sbi), 0);
407 BUG_ON(secno >= TOTAL_SECS(sbi));
408 } else {
409 go_left = 1;
410 left_start = hint - 1;
411 }
412 }
413 if (go_left == 0)
414 goto skip_left;
415
416 while (test_bit(left_start, free_i->free_secmap)) {
417 if (left_start > 0) {
418 left_start--;
419 continue;
420 }
421 left_start = find_next_zero_bit(free_i->free_secmap,
422 TOTAL_SECS(sbi), 0);
423 BUG_ON(left_start >= TOTAL_SECS(sbi));
424 break;
425 }
426 secno = left_start;
427 skip_left:
428 hint = secno;
429 segno = secno * sbi->segs_per_sec;
430 zoneno = secno / sbi->secs_per_zone;
431
432 /* give up on finding another zone */
433 if (!init)
434 goto got_it;
435 if (sbi->secs_per_zone == 1)
436 goto got_it;
437 if (zoneno == old_zoneno)
438 goto got_it;
439 if (dir == ALLOC_LEFT) {
440 if (!go_left && zoneno + 1 >= total_zones)
441 goto got_it;
442 if (go_left && zoneno == 0)
443 goto got_it;
444 }
445 for (i = 0; i < NR_CURSEG_TYPE; i++)
446 if (CURSEG_I(sbi, i)->zone == zoneno)
447 break;
448
449 if (i < NR_CURSEG_TYPE) {
450 /* zone is in user, try another */
451 if (go_left)
452 hint = zoneno * sbi->secs_per_zone - 1;
453 else if (zoneno + 1 >= total_zones)
454 hint = 0;
455 else
456 hint = (zoneno + 1) * sbi->secs_per_zone;
457 init = false;
458 goto find_other_zone;
459 }
460 got_it:
461 /* set it as dirty segment in free segmap */
462 BUG_ON(test_bit(segno, free_i->free_segmap));
463 __set_inuse(sbi, segno);
464 *newseg = segno;
465 write_unlock(&free_i->segmap_lock);
466 }
467
reset_curseg(struct f2fs_sb_info * sbi,int type,int modified)468 static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
469 {
470 struct curseg_info *curseg = CURSEG_I(sbi, type);
471 struct summary_footer *sum_footer;
472
473 curseg->segno = curseg->next_segno;
474 curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
475 curseg->next_blkoff = 0;
476 curseg->next_segno = NULL_SEGNO;
477
478 sum_footer = &(curseg->sum_blk->footer);
479 memset(sum_footer, 0, sizeof(struct summary_footer));
480 if (IS_DATASEG(type))
481 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
482 if (IS_NODESEG(type))
483 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
484 __set_sit_entry_type(sbi, type, curseg->segno, modified);
485 }
486
487 /*
488 * Allocate a current working segment.
489 * This function always allocates a free segment in LFS manner.
490 */
new_curseg(struct f2fs_sb_info * sbi,int type,bool new_sec)491 static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
492 {
493 struct curseg_info *curseg = CURSEG_I(sbi, type);
494 unsigned int segno = curseg->segno;
495 int dir = ALLOC_LEFT;
496
497 write_sum_page(sbi, curseg->sum_blk,
498 GET_SUM_BLOCK(sbi, curseg->segno));
499 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
500 dir = ALLOC_RIGHT;
501
502 if (test_opt(sbi, NOHEAP))
503 dir = ALLOC_RIGHT;
504
505 get_new_segment(sbi, &segno, new_sec, dir);
506 curseg->next_segno = segno;
507 reset_curseg(sbi, type, 1);
508 curseg->alloc_type = LFS;
509 }
510
__next_free_blkoff(struct f2fs_sb_info * sbi,struct curseg_info * seg,block_t start)511 static void __next_free_blkoff(struct f2fs_sb_info *sbi,
512 struct curseg_info *seg, block_t start)
513 {
514 struct seg_entry *se = get_seg_entry(sbi, seg->segno);
515 block_t ofs;
516 for (ofs = start; ofs < sbi->blocks_per_seg; ofs++) {
517 if (!f2fs_test_bit(ofs, se->ckpt_valid_map)
518 && !f2fs_test_bit(ofs, se->cur_valid_map))
519 break;
520 }
521 seg->next_blkoff = ofs;
522 }
523
524 /*
525 * If a segment is written by LFS manner, next block offset is just obtained
526 * by increasing the current block offset. However, if a segment is written by
527 * SSR manner, next block offset obtained by calling __next_free_blkoff
528 */
__refresh_next_blkoff(struct f2fs_sb_info * sbi,struct curseg_info * seg)529 static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
530 struct curseg_info *seg)
531 {
532 if (seg->alloc_type == SSR)
533 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
534 else
535 seg->next_blkoff++;
536 }
537
538 /*
539 * This function always allocates a used segment (from dirty seglist) by SSR
540 * manner, so it should recover the existing segment information of valid blocks
541 */
change_curseg(struct f2fs_sb_info * sbi,int type,bool reuse)542 static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
543 {
544 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
545 struct curseg_info *curseg = CURSEG_I(sbi, type);
546 unsigned int new_segno = curseg->next_segno;
547 struct f2fs_summary_block *sum_node;
548 struct page *sum_page;
549
550 write_sum_page(sbi, curseg->sum_blk,
551 GET_SUM_BLOCK(sbi, curseg->segno));
552 __set_test_and_inuse(sbi, new_segno);
553
554 mutex_lock(&dirty_i->seglist_lock);
555 __remove_dirty_segment(sbi, new_segno, PRE);
556 __remove_dirty_segment(sbi, new_segno, DIRTY);
557 mutex_unlock(&dirty_i->seglist_lock);
558
559 reset_curseg(sbi, type, 1);
560 curseg->alloc_type = SSR;
561 __next_free_blkoff(sbi, curseg, 0);
562
563 if (reuse) {
564 sum_page = get_sum_page(sbi, new_segno);
565 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
566 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
567 f2fs_put_page(sum_page, 1);
568 }
569 }
570
get_ssr_segment(struct f2fs_sb_info * sbi,int type)571 static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
572 {
573 struct curseg_info *curseg = CURSEG_I(sbi, type);
574 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
575
576 if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0))
577 return v_ops->get_victim(sbi,
578 &(curseg)->next_segno, BG_GC, type, SSR);
579
580 /* For data segments, let's do SSR more intensively */
581 for (; type >= CURSEG_HOT_DATA; type--)
582 if (v_ops->get_victim(sbi, &(curseg)->next_segno,
583 BG_GC, type, SSR))
584 return 1;
585 return 0;
586 }
587
588 /*
589 * flush out current segment and replace it with new segment
590 * This function should be returned with success, otherwise BUG
591 */
allocate_segment_by_default(struct f2fs_sb_info * sbi,int type,bool force)592 static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
593 int type, bool force)
594 {
595 struct curseg_info *curseg = CURSEG_I(sbi, type);
596
597 if (force) {
598 new_curseg(sbi, type, true);
599 goto out;
600 }
601
602 curseg->next_segno = check_prefree_segments(sbi, type);
603
604 if (curseg->next_segno != NULL_SEGNO)
605 change_curseg(sbi, type, false);
606 else if (type == CURSEG_WARM_NODE)
607 new_curseg(sbi, type, false);
608 else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
609 new_curseg(sbi, type, false);
610 else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
611 change_curseg(sbi, type, true);
612 else
613 new_curseg(sbi, type, false);
614 out:
615 sbi->segment_count[curseg->alloc_type]++;
616 }
617
allocate_new_segments(struct f2fs_sb_info * sbi)618 void allocate_new_segments(struct f2fs_sb_info *sbi)
619 {
620 struct curseg_info *curseg;
621 unsigned int old_curseg;
622 int i;
623
624 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
625 curseg = CURSEG_I(sbi, i);
626 old_curseg = curseg->segno;
627 SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
628 locate_dirty_segment(sbi, old_curseg);
629 }
630 }
631
632 static const struct segment_allocation default_salloc_ops = {
633 .allocate_segment = allocate_segment_by_default,
634 };
635
f2fs_end_io_write(struct bio * bio,int err)636 static void f2fs_end_io_write(struct bio *bio, int err)
637 {
638 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
639 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
640 struct bio_private *p = bio->bi_private;
641
642 do {
643 struct page *page = bvec->bv_page;
644
645 if (--bvec >= bio->bi_io_vec)
646 prefetchw(&bvec->bv_page->flags);
647 if (!uptodate) {
648 SetPageError(page);
649 if (page->mapping)
650 set_bit(AS_EIO, &page->mapping->flags);
651 set_ckpt_flags(p->sbi->ckpt, CP_ERROR_FLAG);
652 p->sbi->sb->s_flags |= MS_RDONLY;
653 }
654 end_page_writeback(page);
655 dec_page_count(p->sbi, F2FS_WRITEBACK);
656 } while (bvec >= bio->bi_io_vec);
657
658 if (p->is_sync)
659 complete(p->wait);
660 kfree(p);
661 bio_put(bio);
662 }
663
f2fs_bio_alloc(struct block_device * bdev,int npages)664 struct bio *f2fs_bio_alloc(struct block_device *bdev, int npages)
665 {
666 struct bio *bio;
667 struct bio_private *priv;
668 retry:
669 priv = kmalloc(sizeof(struct bio_private), GFP_NOFS);
670 if (!priv) {
671 cond_resched();
672 goto retry;
673 }
674
675 /* No failure on bio allocation */
676 bio = bio_alloc(GFP_NOIO, npages);
677 bio->bi_bdev = bdev;
678 bio->bi_private = priv;
679 return bio;
680 }
681
do_submit_bio(struct f2fs_sb_info * sbi,enum page_type type,bool sync)682 static void do_submit_bio(struct f2fs_sb_info *sbi,
683 enum page_type type, bool sync)
684 {
685 int rw = sync ? WRITE_SYNC : WRITE;
686 enum page_type btype = type > META ? META : type;
687
688 if (type >= META_FLUSH)
689 rw = WRITE_FLUSH_FUA;
690
691 if (btype == META)
692 rw |= REQ_META;
693
694 if (sbi->bio[btype]) {
695 struct bio_private *p = sbi->bio[btype]->bi_private;
696 p->sbi = sbi;
697 sbi->bio[btype]->bi_end_io = f2fs_end_io_write;
698
699 trace_f2fs_do_submit_bio(sbi->sb, btype, sync, sbi->bio[btype]);
700
701 if (type == META_FLUSH) {
702 DECLARE_COMPLETION_ONSTACK(wait);
703 p->is_sync = true;
704 p->wait = &wait;
705 submit_bio(rw, sbi->bio[btype]);
706 wait_for_completion(&wait);
707 } else {
708 p->is_sync = false;
709 submit_bio(rw, sbi->bio[btype]);
710 }
711 sbi->bio[btype] = NULL;
712 }
713 }
714
f2fs_submit_bio(struct f2fs_sb_info * sbi,enum page_type type,bool sync)715 void f2fs_submit_bio(struct f2fs_sb_info *sbi, enum page_type type, bool sync)
716 {
717 down_write(&sbi->bio_sem);
718 do_submit_bio(sbi, type, sync);
719 up_write(&sbi->bio_sem);
720 }
721
submit_write_page(struct f2fs_sb_info * sbi,struct page * page,block_t blk_addr,enum page_type type)722 static void submit_write_page(struct f2fs_sb_info *sbi, struct page *page,
723 block_t blk_addr, enum page_type type)
724 {
725 struct block_device *bdev = sbi->sb->s_bdev;
726
727 verify_block_addr(sbi, blk_addr);
728
729 down_write(&sbi->bio_sem);
730
731 inc_page_count(sbi, F2FS_WRITEBACK);
732
733 if (sbi->bio[type] && sbi->last_block_in_bio[type] != blk_addr - 1)
734 do_submit_bio(sbi, type, false);
735 alloc_new:
736 if (sbi->bio[type] == NULL) {
737 sbi->bio[type] = f2fs_bio_alloc(bdev, max_hw_blocks(sbi));
738 sbi->bio[type]->bi_sector = SECTOR_FROM_BLOCK(sbi, blk_addr);
739 /*
740 * The end_io will be assigned at the sumbission phase.
741 * Until then, let bio_add_page() merge consecutive IOs as much
742 * as possible.
743 */
744 }
745
746 if (bio_add_page(sbi->bio[type], page, PAGE_CACHE_SIZE, 0) <
747 PAGE_CACHE_SIZE) {
748 do_submit_bio(sbi, type, false);
749 goto alloc_new;
750 }
751
752 sbi->last_block_in_bio[type] = blk_addr;
753
754 up_write(&sbi->bio_sem);
755 trace_f2fs_submit_write_page(page, blk_addr, type);
756 }
757
__has_curseg_space(struct f2fs_sb_info * sbi,int type)758 static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
759 {
760 struct curseg_info *curseg = CURSEG_I(sbi, type);
761 if (curseg->next_blkoff < sbi->blocks_per_seg)
762 return true;
763 return false;
764 }
765
__get_segment_type_2(struct page * page,enum page_type p_type)766 static int __get_segment_type_2(struct page *page, enum page_type p_type)
767 {
768 if (p_type == DATA)
769 return CURSEG_HOT_DATA;
770 else
771 return CURSEG_HOT_NODE;
772 }
773
__get_segment_type_4(struct page * page,enum page_type p_type)774 static int __get_segment_type_4(struct page *page, enum page_type p_type)
775 {
776 if (p_type == DATA) {
777 struct inode *inode = page->mapping->host;
778
779 if (S_ISDIR(inode->i_mode))
780 return CURSEG_HOT_DATA;
781 else
782 return CURSEG_COLD_DATA;
783 } else {
784 if (IS_DNODE(page) && !is_cold_node(page))
785 return CURSEG_HOT_NODE;
786 else
787 return CURSEG_COLD_NODE;
788 }
789 }
790
__get_segment_type_6(struct page * page,enum page_type p_type)791 static int __get_segment_type_6(struct page *page, enum page_type p_type)
792 {
793 if (p_type == DATA) {
794 struct inode *inode = page->mapping->host;
795
796 if (S_ISDIR(inode->i_mode))
797 return CURSEG_HOT_DATA;
798 else if (is_cold_data(page) || is_cold_file(inode))
799 return CURSEG_COLD_DATA;
800 else
801 return CURSEG_WARM_DATA;
802 } else {
803 if (IS_DNODE(page))
804 return is_cold_node(page) ? CURSEG_WARM_NODE :
805 CURSEG_HOT_NODE;
806 else
807 return CURSEG_COLD_NODE;
808 }
809 }
810
__get_segment_type(struct page * page,enum page_type p_type)811 static int __get_segment_type(struct page *page, enum page_type p_type)
812 {
813 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
814 switch (sbi->active_logs) {
815 case 2:
816 return __get_segment_type_2(page, p_type);
817 case 4:
818 return __get_segment_type_4(page, p_type);
819 }
820 /* NR_CURSEG_TYPE(6) logs by default */
821 BUG_ON(sbi->active_logs != NR_CURSEG_TYPE);
822 return __get_segment_type_6(page, p_type);
823 }
824
do_write_page(struct f2fs_sb_info * sbi,struct page * page,block_t old_blkaddr,block_t * new_blkaddr,struct f2fs_summary * sum,enum page_type p_type)825 static void do_write_page(struct f2fs_sb_info *sbi, struct page *page,
826 block_t old_blkaddr, block_t *new_blkaddr,
827 struct f2fs_summary *sum, enum page_type p_type)
828 {
829 struct sit_info *sit_i = SIT_I(sbi);
830 struct curseg_info *curseg;
831 unsigned int old_cursegno;
832 int type;
833
834 type = __get_segment_type(page, p_type);
835 curseg = CURSEG_I(sbi, type);
836
837 mutex_lock(&curseg->curseg_mutex);
838
839 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
840 old_cursegno = curseg->segno;
841
842 /*
843 * __add_sum_entry should be resided under the curseg_mutex
844 * because, this function updates a summary entry in the
845 * current summary block.
846 */
847 __add_sum_entry(sbi, type, sum, curseg->next_blkoff);
848
849 mutex_lock(&sit_i->sentry_lock);
850 __refresh_next_blkoff(sbi, curseg);
851 sbi->block_count[curseg->alloc_type]++;
852
853 /*
854 * SIT information should be updated before segment allocation,
855 * since SSR needs latest valid block information.
856 */
857 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
858
859 if (!__has_curseg_space(sbi, type))
860 sit_i->s_ops->allocate_segment(sbi, type, false);
861
862 locate_dirty_segment(sbi, old_cursegno);
863 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
864 mutex_unlock(&sit_i->sentry_lock);
865
866 if (p_type == NODE)
867 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
868
869 /* writeout dirty page into bdev */
870 submit_write_page(sbi, page, *new_blkaddr, p_type);
871
872 mutex_unlock(&curseg->curseg_mutex);
873 }
874
write_meta_page(struct f2fs_sb_info * sbi,struct page * page)875 void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
876 {
877 set_page_writeback(page);
878 submit_write_page(sbi, page, page->index, META);
879 }
880
write_node_page(struct f2fs_sb_info * sbi,struct page * page,unsigned int nid,block_t old_blkaddr,block_t * new_blkaddr)881 void write_node_page(struct f2fs_sb_info *sbi, struct page *page,
882 unsigned int nid, block_t old_blkaddr, block_t *new_blkaddr)
883 {
884 struct f2fs_summary sum;
885 set_summary(&sum, nid, 0, 0);
886 do_write_page(sbi, page, old_blkaddr, new_blkaddr, &sum, NODE);
887 }
888
write_data_page(struct inode * inode,struct page * page,struct dnode_of_data * dn,block_t old_blkaddr,block_t * new_blkaddr)889 void write_data_page(struct inode *inode, struct page *page,
890 struct dnode_of_data *dn, block_t old_blkaddr,
891 block_t *new_blkaddr)
892 {
893 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
894 struct f2fs_summary sum;
895 struct node_info ni;
896
897 BUG_ON(old_blkaddr == NULL_ADDR);
898 get_node_info(sbi, dn->nid, &ni);
899 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
900
901 do_write_page(sbi, page, old_blkaddr,
902 new_blkaddr, &sum, DATA);
903 }
904
rewrite_data_page(struct f2fs_sb_info * sbi,struct page * page,block_t old_blk_addr)905 void rewrite_data_page(struct f2fs_sb_info *sbi, struct page *page,
906 block_t old_blk_addr)
907 {
908 submit_write_page(sbi, page, old_blk_addr, DATA);
909 }
910
recover_data_page(struct f2fs_sb_info * sbi,struct page * page,struct f2fs_summary * sum,block_t old_blkaddr,block_t new_blkaddr)911 void recover_data_page(struct f2fs_sb_info *sbi,
912 struct page *page, struct f2fs_summary *sum,
913 block_t old_blkaddr, block_t new_blkaddr)
914 {
915 struct sit_info *sit_i = SIT_I(sbi);
916 struct curseg_info *curseg;
917 unsigned int segno, old_cursegno;
918 struct seg_entry *se;
919 int type;
920
921 segno = GET_SEGNO(sbi, new_blkaddr);
922 se = get_seg_entry(sbi, segno);
923 type = se->type;
924
925 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
926 if (old_blkaddr == NULL_ADDR)
927 type = CURSEG_COLD_DATA;
928 else
929 type = CURSEG_WARM_DATA;
930 }
931 curseg = CURSEG_I(sbi, type);
932
933 mutex_lock(&curseg->curseg_mutex);
934 mutex_lock(&sit_i->sentry_lock);
935
936 old_cursegno = curseg->segno;
937
938 /* change the current segment */
939 if (segno != curseg->segno) {
940 curseg->next_segno = segno;
941 change_curseg(sbi, type, true);
942 }
943
944 curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, new_blkaddr) &
945 (sbi->blocks_per_seg - 1);
946 __add_sum_entry(sbi, type, sum, curseg->next_blkoff);
947
948 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
949
950 locate_dirty_segment(sbi, old_cursegno);
951 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
952
953 mutex_unlock(&sit_i->sentry_lock);
954 mutex_unlock(&curseg->curseg_mutex);
955 }
956
rewrite_node_page(struct f2fs_sb_info * sbi,struct page * page,struct f2fs_summary * sum,block_t old_blkaddr,block_t new_blkaddr)957 void rewrite_node_page(struct f2fs_sb_info *sbi,
958 struct page *page, struct f2fs_summary *sum,
959 block_t old_blkaddr, block_t new_blkaddr)
960 {
961 struct sit_info *sit_i = SIT_I(sbi);
962 int type = CURSEG_WARM_NODE;
963 struct curseg_info *curseg;
964 unsigned int segno, old_cursegno;
965 block_t next_blkaddr = next_blkaddr_of_node(page);
966 unsigned int next_segno = GET_SEGNO(sbi, next_blkaddr);
967
968 curseg = CURSEG_I(sbi, type);
969
970 mutex_lock(&curseg->curseg_mutex);
971 mutex_lock(&sit_i->sentry_lock);
972
973 segno = GET_SEGNO(sbi, new_blkaddr);
974 old_cursegno = curseg->segno;
975
976 /* change the current segment */
977 if (segno != curseg->segno) {
978 curseg->next_segno = segno;
979 change_curseg(sbi, type, true);
980 }
981 curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, new_blkaddr) &
982 (sbi->blocks_per_seg - 1);
983 __add_sum_entry(sbi, type, sum, curseg->next_blkoff);
984
985 /* change the current log to the next block addr in advance */
986 if (next_segno != segno) {
987 curseg->next_segno = next_segno;
988 change_curseg(sbi, type, true);
989 }
990 curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, next_blkaddr) &
991 (sbi->blocks_per_seg - 1);
992
993 /* rewrite node page */
994 set_page_writeback(page);
995 submit_write_page(sbi, page, new_blkaddr, NODE);
996 f2fs_submit_bio(sbi, NODE, true);
997 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
998
999 locate_dirty_segment(sbi, old_cursegno);
1000 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
1001
1002 mutex_unlock(&sit_i->sentry_lock);
1003 mutex_unlock(&curseg->curseg_mutex);
1004 }
1005
read_compacted_summaries(struct f2fs_sb_info * sbi)1006 static int read_compacted_summaries(struct f2fs_sb_info *sbi)
1007 {
1008 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1009 struct curseg_info *seg_i;
1010 unsigned char *kaddr;
1011 struct page *page;
1012 block_t start;
1013 int i, j, offset;
1014
1015 start = start_sum_block(sbi);
1016
1017 page = get_meta_page(sbi, start++);
1018 kaddr = (unsigned char *)page_address(page);
1019
1020 /* Step 1: restore nat cache */
1021 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1022 memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
1023
1024 /* Step 2: restore sit cache */
1025 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1026 memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
1027 SUM_JOURNAL_SIZE);
1028 offset = 2 * SUM_JOURNAL_SIZE;
1029
1030 /* Step 3: restore summary entries */
1031 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1032 unsigned short blk_off;
1033 unsigned int segno;
1034
1035 seg_i = CURSEG_I(sbi, i);
1036 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
1037 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
1038 seg_i->next_segno = segno;
1039 reset_curseg(sbi, i, 0);
1040 seg_i->alloc_type = ckpt->alloc_type[i];
1041 seg_i->next_blkoff = blk_off;
1042
1043 if (seg_i->alloc_type == SSR)
1044 blk_off = sbi->blocks_per_seg;
1045
1046 for (j = 0; j < blk_off; j++) {
1047 struct f2fs_summary *s;
1048 s = (struct f2fs_summary *)(kaddr + offset);
1049 seg_i->sum_blk->entries[j] = *s;
1050 offset += SUMMARY_SIZE;
1051 if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1052 SUM_FOOTER_SIZE)
1053 continue;
1054
1055 f2fs_put_page(page, 1);
1056 page = NULL;
1057
1058 page = get_meta_page(sbi, start++);
1059 kaddr = (unsigned char *)page_address(page);
1060 offset = 0;
1061 }
1062 }
1063 f2fs_put_page(page, 1);
1064 return 0;
1065 }
1066
read_normal_summaries(struct f2fs_sb_info * sbi,int type)1067 static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
1068 {
1069 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1070 struct f2fs_summary_block *sum;
1071 struct curseg_info *curseg;
1072 struct page *new;
1073 unsigned short blk_off;
1074 unsigned int segno = 0;
1075 block_t blk_addr = 0;
1076
1077 /* get segment number and block addr */
1078 if (IS_DATASEG(type)) {
1079 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
1080 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
1081 CURSEG_HOT_DATA]);
1082 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
1083 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
1084 else
1085 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
1086 } else {
1087 segno = le32_to_cpu(ckpt->cur_node_segno[type -
1088 CURSEG_HOT_NODE]);
1089 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
1090 CURSEG_HOT_NODE]);
1091 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
1092 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
1093 type - CURSEG_HOT_NODE);
1094 else
1095 blk_addr = GET_SUM_BLOCK(sbi, segno);
1096 }
1097
1098 new = get_meta_page(sbi, blk_addr);
1099 sum = (struct f2fs_summary_block *)page_address(new);
1100
1101 if (IS_NODESEG(type)) {
1102 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG)) {
1103 struct f2fs_summary *ns = &sum->entries[0];
1104 int i;
1105 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
1106 ns->version = 0;
1107 ns->ofs_in_node = 0;
1108 }
1109 } else {
1110 if (restore_node_summary(sbi, segno, sum)) {
1111 f2fs_put_page(new, 1);
1112 return -EINVAL;
1113 }
1114 }
1115 }
1116
1117 /* set uncompleted segment to curseg */
1118 curseg = CURSEG_I(sbi, type);
1119 mutex_lock(&curseg->curseg_mutex);
1120 memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
1121 curseg->next_segno = segno;
1122 reset_curseg(sbi, type, 0);
1123 curseg->alloc_type = ckpt->alloc_type[type];
1124 curseg->next_blkoff = blk_off;
1125 mutex_unlock(&curseg->curseg_mutex);
1126 f2fs_put_page(new, 1);
1127 return 0;
1128 }
1129
restore_curseg_summaries(struct f2fs_sb_info * sbi)1130 static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
1131 {
1132 struct f2fs_summary_block *s_sits =
1133 CURSEG_I(sbi, CURSEG_COLD_DATA)->sum_blk;
1134 struct f2fs_summary_block *s_nats =
1135 CURSEG_I(sbi, CURSEG_HOT_DATA)->sum_blk;
1136 int type = CURSEG_HOT_DATA;
1137
1138 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
1139 /* restore for compacted data summary */
1140 if (read_compacted_summaries(sbi))
1141 return -EINVAL;
1142 type = CURSEG_HOT_NODE;
1143 }
1144
1145 for (; type <= CURSEG_COLD_NODE; type++)
1146 if (read_normal_summaries(sbi, type))
1147 return -EINVAL;
1148
1149 /* sanity check for summary blocks */
1150 if (nats_in_cursum(s_nats) > NAT_JOURNAL_ENTRIES ||
1151 sits_in_cursum(s_sits) > SIT_JOURNAL_ENTRIES)
1152 return -EINVAL;
1153
1154 return 0;
1155 }
1156
write_compacted_summaries(struct f2fs_sb_info * sbi,block_t blkaddr)1157 static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
1158 {
1159 struct page *page;
1160 unsigned char *kaddr;
1161 struct f2fs_summary *summary;
1162 struct curseg_info *seg_i;
1163 int written_size = 0;
1164 int i, j;
1165
1166 page = grab_meta_page(sbi, blkaddr++);
1167 kaddr = (unsigned char *)page_address(page);
1168
1169 /* Step 1: write nat cache */
1170 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1171 memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
1172 written_size += SUM_JOURNAL_SIZE;
1173
1174 /* Step 2: write sit cache */
1175 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1176 memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
1177 SUM_JOURNAL_SIZE);
1178 written_size += SUM_JOURNAL_SIZE;
1179
1180 set_page_dirty(page);
1181
1182 /* Step 3: write summary entries */
1183 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1184 unsigned short blkoff;
1185 seg_i = CURSEG_I(sbi, i);
1186 if (sbi->ckpt->alloc_type[i] == SSR)
1187 blkoff = sbi->blocks_per_seg;
1188 else
1189 blkoff = curseg_blkoff(sbi, i);
1190
1191 for (j = 0; j < blkoff; j++) {
1192 if (!page) {
1193 page = grab_meta_page(sbi, blkaddr++);
1194 kaddr = (unsigned char *)page_address(page);
1195 written_size = 0;
1196 }
1197 summary = (struct f2fs_summary *)(kaddr + written_size);
1198 *summary = seg_i->sum_blk->entries[j];
1199 written_size += SUMMARY_SIZE;
1200 set_page_dirty(page);
1201
1202 if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1203 SUM_FOOTER_SIZE)
1204 continue;
1205
1206 f2fs_put_page(page, 1);
1207 page = NULL;
1208 }
1209 }
1210 if (page)
1211 f2fs_put_page(page, 1);
1212 }
1213
write_normal_summaries(struct f2fs_sb_info * sbi,block_t blkaddr,int type)1214 static void write_normal_summaries(struct f2fs_sb_info *sbi,
1215 block_t blkaddr, int type)
1216 {
1217 int i, end;
1218 if (IS_DATASEG(type))
1219 end = type + NR_CURSEG_DATA_TYPE;
1220 else
1221 end = type + NR_CURSEG_NODE_TYPE;
1222
1223 for (i = type; i < end; i++) {
1224 struct curseg_info *sum = CURSEG_I(sbi, i);
1225 mutex_lock(&sum->curseg_mutex);
1226 write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
1227 mutex_unlock(&sum->curseg_mutex);
1228 }
1229 }
1230
write_data_summaries(struct f2fs_sb_info * sbi,block_t start_blk)1231 void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1232 {
1233 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
1234 write_compacted_summaries(sbi, start_blk);
1235 else
1236 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
1237 }
1238
write_node_summaries(struct f2fs_sb_info * sbi,block_t start_blk)1239 void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1240 {
1241 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG))
1242 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
1243 return;
1244 }
1245
lookup_journal_in_cursum(struct f2fs_summary_block * sum,int type,unsigned int val,int alloc)1246 int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
1247 unsigned int val, int alloc)
1248 {
1249 int i;
1250
1251 if (type == NAT_JOURNAL) {
1252 for (i = 0; i < nats_in_cursum(sum); i++) {
1253 if (le32_to_cpu(nid_in_journal(sum, i)) == val)
1254 return i;
1255 }
1256 if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
1257 return update_nats_in_cursum(sum, 1);
1258 } else if (type == SIT_JOURNAL) {
1259 for (i = 0; i < sits_in_cursum(sum); i++)
1260 if (le32_to_cpu(segno_in_journal(sum, i)) == val)
1261 return i;
1262 if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
1263 return update_sits_in_cursum(sum, 1);
1264 }
1265 return -1;
1266 }
1267
get_current_sit_page(struct f2fs_sb_info * sbi,unsigned int segno)1268 static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
1269 unsigned int segno)
1270 {
1271 struct sit_info *sit_i = SIT_I(sbi);
1272 unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno);
1273 block_t blk_addr = sit_i->sit_base_addr + offset;
1274
1275 check_seg_range(sbi, segno);
1276
1277 /* calculate sit block address */
1278 if (f2fs_test_bit(offset, sit_i->sit_bitmap))
1279 blk_addr += sit_i->sit_blocks;
1280
1281 return get_meta_page(sbi, blk_addr);
1282 }
1283
get_next_sit_page(struct f2fs_sb_info * sbi,unsigned int start)1284 static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
1285 unsigned int start)
1286 {
1287 struct sit_info *sit_i = SIT_I(sbi);
1288 struct page *src_page, *dst_page;
1289 pgoff_t src_off, dst_off;
1290 void *src_addr, *dst_addr;
1291
1292 src_off = current_sit_addr(sbi, start);
1293 dst_off = next_sit_addr(sbi, src_off);
1294
1295 /* get current sit block page without lock */
1296 src_page = get_meta_page(sbi, src_off);
1297 dst_page = grab_meta_page(sbi, dst_off);
1298 BUG_ON(PageDirty(src_page));
1299
1300 src_addr = page_address(src_page);
1301 dst_addr = page_address(dst_page);
1302 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
1303
1304 set_page_dirty(dst_page);
1305 f2fs_put_page(src_page, 1);
1306
1307 set_to_next_sit(sit_i, start);
1308
1309 return dst_page;
1310 }
1311
flush_sits_in_journal(struct f2fs_sb_info * sbi)1312 static bool flush_sits_in_journal(struct f2fs_sb_info *sbi)
1313 {
1314 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1315 struct f2fs_summary_block *sum = curseg->sum_blk;
1316 int i;
1317
1318 /*
1319 * If the journal area in the current summary is full of sit entries,
1320 * all the sit entries will be flushed. Otherwise the sit entries
1321 * are not able to replace with newly hot sit entries.
1322 */
1323 if (sits_in_cursum(sum) >= SIT_JOURNAL_ENTRIES) {
1324 for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
1325 unsigned int segno;
1326 segno = le32_to_cpu(segno_in_journal(sum, i));
1327 __mark_sit_entry_dirty(sbi, segno);
1328 }
1329 update_sits_in_cursum(sum, -sits_in_cursum(sum));
1330 return 1;
1331 }
1332 return 0;
1333 }
1334
1335 /*
1336 * CP calls this function, which flushes SIT entries including sit_journal,
1337 * and moves prefree segs to free segs.
1338 */
flush_sit_entries(struct f2fs_sb_info * sbi)1339 void flush_sit_entries(struct f2fs_sb_info *sbi)
1340 {
1341 struct sit_info *sit_i = SIT_I(sbi);
1342 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
1343 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1344 struct f2fs_summary_block *sum = curseg->sum_blk;
1345 unsigned long nsegs = TOTAL_SEGS(sbi);
1346 struct page *page = NULL;
1347 struct f2fs_sit_block *raw_sit = NULL;
1348 unsigned int start = 0, end = 0;
1349 unsigned int segno = -1;
1350 bool flushed;
1351
1352 mutex_lock(&curseg->curseg_mutex);
1353 mutex_lock(&sit_i->sentry_lock);
1354
1355 /*
1356 * "flushed" indicates whether sit entries in journal are flushed
1357 * to the SIT area or not.
1358 */
1359 flushed = flush_sits_in_journal(sbi);
1360
1361 while ((segno = find_next_bit(bitmap, nsegs, segno + 1)) < nsegs) {
1362 struct seg_entry *se = get_seg_entry(sbi, segno);
1363 int sit_offset, offset;
1364
1365 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
1366
1367 if (flushed)
1368 goto to_sit_page;
1369
1370 offset = lookup_journal_in_cursum(sum, SIT_JOURNAL, segno, 1);
1371 if (offset >= 0) {
1372 segno_in_journal(sum, offset) = cpu_to_le32(segno);
1373 seg_info_to_raw_sit(se, &sit_in_journal(sum, offset));
1374 goto flush_done;
1375 }
1376 to_sit_page:
1377 if (!page || (start > segno) || (segno > end)) {
1378 if (page) {
1379 f2fs_put_page(page, 1);
1380 page = NULL;
1381 }
1382
1383 start = START_SEGNO(sit_i, segno);
1384 end = start + SIT_ENTRY_PER_BLOCK - 1;
1385
1386 /* read sit block that will be updated */
1387 page = get_next_sit_page(sbi, start);
1388 raw_sit = page_address(page);
1389 }
1390
1391 /* udpate entry in SIT block */
1392 seg_info_to_raw_sit(se, &raw_sit->entries[sit_offset]);
1393 flush_done:
1394 __clear_bit(segno, bitmap);
1395 sit_i->dirty_sentries--;
1396 }
1397 mutex_unlock(&sit_i->sentry_lock);
1398 mutex_unlock(&curseg->curseg_mutex);
1399
1400 /* writeout last modified SIT block */
1401 f2fs_put_page(page, 1);
1402
1403 set_prefree_as_free_segments(sbi);
1404 }
1405
build_sit_info(struct f2fs_sb_info * sbi)1406 static int build_sit_info(struct f2fs_sb_info *sbi)
1407 {
1408 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1409 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1410 struct sit_info *sit_i;
1411 unsigned int sit_segs, start;
1412 char *src_bitmap, *dst_bitmap;
1413 unsigned int bitmap_size;
1414
1415 /* allocate memory for SIT information */
1416 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
1417 if (!sit_i)
1418 return -ENOMEM;
1419
1420 SM_I(sbi)->sit_info = sit_i;
1421
1422 sit_i->sentries = vzalloc(TOTAL_SEGS(sbi) * sizeof(struct seg_entry));
1423 if (!sit_i->sentries)
1424 return -ENOMEM;
1425
1426 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1427 sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1428 if (!sit_i->dirty_sentries_bitmap)
1429 return -ENOMEM;
1430
1431 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1432 sit_i->sentries[start].cur_valid_map
1433 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1434 sit_i->sentries[start].ckpt_valid_map
1435 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1436 if (!sit_i->sentries[start].cur_valid_map
1437 || !sit_i->sentries[start].ckpt_valid_map)
1438 return -ENOMEM;
1439 }
1440
1441 if (sbi->segs_per_sec > 1) {
1442 sit_i->sec_entries = vzalloc(TOTAL_SECS(sbi) *
1443 sizeof(struct sec_entry));
1444 if (!sit_i->sec_entries)
1445 return -ENOMEM;
1446 }
1447
1448 /* get information related with SIT */
1449 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
1450
1451 /* setup SIT bitmap from ckeckpoint pack */
1452 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
1453 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
1454
1455 dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
1456 if (!dst_bitmap)
1457 return -ENOMEM;
1458
1459 /* init SIT information */
1460 sit_i->s_ops = &default_salloc_ops;
1461
1462 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
1463 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
1464 sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
1465 sit_i->sit_bitmap = dst_bitmap;
1466 sit_i->bitmap_size = bitmap_size;
1467 sit_i->dirty_sentries = 0;
1468 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
1469 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
1470 sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
1471 mutex_init(&sit_i->sentry_lock);
1472 return 0;
1473 }
1474
build_free_segmap(struct f2fs_sb_info * sbi)1475 static int build_free_segmap(struct f2fs_sb_info *sbi)
1476 {
1477 struct f2fs_sm_info *sm_info = SM_I(sbi);
1478 struct free_segmap_info *free_i;
1479 unsigned int bitmap_size, sec_bitmap_size;
1480
1481 /* allocate memory for free segmap information */
1482 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
1483 if (!free_i)
1484 return -ENOMEM;
1485
1486 SM_I(sbi)->free_info = free_i;
1487
1488 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1489 free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL);
1490 if (!free_i->free_segmap)
1491 return -ENOMEM;
1492
1493 sec_bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
1494 free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL);
1495 if (!free_i->free_secmap)
1496 return -ENOMEM;
1497
1498 /* set all segments as dirty temporarily */
1499 memset(free_i->free_segmap, 0xff, bitmap_size);
1500 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
1501
1502 /* init free segmap information */
1503 free_i->start_segno =
1504 (unsigned int) GET_SEGNO_FROM_SEG0(sbi, sm_info->main_blkaddr);
1505 free_i->free_segments = 0;
1506 free_i->free_sections = 0;
1507 rwlock_init(&free_i->segmap_lock);
1508 return 0;
1509 }
1510
build_curseg(struct f2fs_sb_info * sbi)1511 static int build_curseg(struct f2fs_sb_info *sbi)
1512 {
1513 struct curseg_info *array;
1514 int i;
1515
1516 array = kzalloc(sizeof(*array) * NR_CURSEG_TYPE, GFP_KERNEL);
1517 if (!array)
1518 return -ENOMEM;
1519
1520 SM_I(sbi)->curseg_array = array;
1521
1522 for (i = 0; i < NR_CURSEG_TYPE; i++) {
1523 mutex_init(&array[i].curseg_mutex);
1524 array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
1525 if (!array[i].sum_blk)
1526 return -ENOMEM;
1527 array[i].segno = NULL_SEGNO;
1528 array[i].next_blkoff = 0;
1529 }
1530 return restore_curseg_summaries(sbi);
1531 }
1532
build_sit_entries(struct f2fs_sb_info * sbi)1533 static void build_sit_entries(struct f2fs_sb_info *sbi)
1534 {
1535 struct sit_info *sit_i = SIT_I(sbi);
1536 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1537 struct f2fs_summary_block *sum = curseg->sum_blk;
1538 unsigned int start;
1539
1540 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1541 struct seg_entry *se = &sit_i->sentries[start];
1542 struct f2fs_sit_block *sit_blk;
1543 struct f2fs_sit_entry sit;
1544 struct page *page;
1545 int i;
1546
1547 mutex_lock(&curseg->curseg_mutex);
1548 for (i = 0; i < sits_in_cursum(sum); i++) {
1549 if (le32_to_cpu(segno_in_journal(sum, i)) == start) {
1550 sit = sit_in_journal(sum, i);
1551 mutex_unlock(&curseg->curseg_mutex);
1552 goto got_it;
1553 }
1554 }
1555 mutex_unlock(&curseg->curseg_mutex);
1556 page = get_current_sit_page(sbi, start);
1557 sit_blk = (struct f2fs_sit_block *)page_address(page);
1558 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
1559 f2fs_put_page(page, 1);
1560 got_it:
1561 check_block_count(sbi, start, &sit);
1562 seg_info_from_raw_sit(se, &sit);
1563 if (sbi->segs_per_sec > 1) {
1564 struct sec_entry *e = get_sec_entry(sbi, start);
1565 e->valid_blocks += se->valid_blocks;
1566 }
1567 }
1568 }
1569
init_free_segmap(struct f2fs_sb_info * sbi)1570 static void init_free_segmap(struct f2fs_sb_info *sbi)
1571 {
1572 unsigned int start;
1573 int type;
1574
1575 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1576 struct seg_entry *sentry = get_seg_entry(sbi, start);
1577 if (!sentry->valid_blocks)
1578 __set_free(sbi, start);
1579 }
1580
1581 /* set use the current segments */
1582 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
1583 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
1584 __set_test_and_inuse(sbi, curseg_t->segno);
1585 }
1586 }
1587
init_dirty_segmap(struct f2fs_sb_info * sbi)1588 static void init_dirty_segmap(struct f2fs_sb_info *sbi)
1589 {
1590 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1591 struct free_segmap_info *free_i = FREE_I(sbi);
1592 unsigned int segno = 0, offset = 0;
1593 unsigned short valid_blocks;
1594
1595 while (segno < TOTAL_SEGS(sbi)) {
1596 /* find dirty segment based on free segmap */
1597 segno = find_next_inuse(free_i, TOTAL_SEGS(sbi), offset);
1598 if (segno >= TOTAL_SEGS(sbi))
1599 break;
1600 offset = segno + 1;
1601 valid_blocks = get_valid_blocks(sbi, segno, 0);
1602 if (valid_blocks >= sbi->blocks_per_seg || !valid_blocks)
1603 continue;
1604 mutex_lock(&dirty_i->seglist_lock);
1605 __locate_dirty_segment(sbi, segno, DIRTY);
1606 mutex_unlock(&dirty_i->seglist_lock);
1607 }
1608 }
1609
init_victim_secmap(struct f2fs_sb_info * sbi)1610 static int init_victim_secmap(struct f2fs_sb_info *sbi)
1611 {
1612 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1613 unsigned int bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
1614
1615 dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL);
1616 if (!dirty_i->victim_secmap)
1617 return -ENOMEM;
1618 return 0;
1619 }
1620
build_dirty_segmap(struct f2fs_sb_info * sbi)1621 static int build_dirty_segmap(struct f2fs_sb_info *sbi)
1622 {
1623 struct dirty_seglist_info *dirty_i;
1624 unsigned int bitmap_size, i;
1625
1626 /* allocate memory for dirty segments list information */
1627 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
1628 if (!dirty_i)
1629 return -ENOMEM;
1630
1631 SM_I(sbi)->dirty_info = dirty_i;
1632 mutex_init(&dirty_i->seglist_lock);
1633
1634 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1635
1636 for (i = 0; i < NR_DIRTY_TYPE; i++) {
1637 dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
1638 if (!dirty_i->dirty_segmap[i])
1639 return -ENOMEM;
1640 }
1641
1642 init_dirty_segmap(sbi);
1643 return init_victim_secmap(sbi);
1644 }
1645
1646 /*
1647 * Update min, max modified time for cost-benefit GC algorithm
1648 */
init_min_max_mtime(struct f2fs_sb_info * sbi)1649 static void init_min_max_mtime(struct f2fs_sb_info *sbi)
1650 {
1651 struct sit_info *sit_i = SIT_I(sbi);
1652 unsigned int segno;
1653
1654 mutex_lock(&sit_i->sentry_lock);
1655
1656 sit_i->min_mtime = LLONG_MAX;
1657
1658 for (segno = 0; segno < TOTAL_SEGS(sbi); segno += sbi->segs_per_sec) {
1659 unsigned int i;
1660 unsigned long long mtime = 0;
1661
1662 for (i = 0; i < sbi->segs_per_sec; i++)
1663 mtime += get_seg_entry(sbi, segno + i)->mtime;
1664
1665 mtime = div_u64(mtime, sbi->segs_per_sec);
1666
1667 if (sit_i->min_mtime > mtime)
1668 sit_i->min_mtime = mtime;
1669 }
1670 sit_i->max_mtime = get_mtime(sbi);
1671 mutex_unlock(&sit_i->sentry_lock);
1672 }
1673
build_segment_manager(struct f2fs_sb_info * sbi)1674 int build_segment_manager(struct f2fs_sb_info *sbi)
1675 {
1676 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1677 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1678 struct f2fs_sm_info *sm_info;
1679 int err;
1680
1681 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
1682 if (!sm_info)
1683 return -ENOMEM;
1684
1685 /* init sm info */
1686 sbi->sm_info = sm_info;
1687 INIT_LIST_HEAD(&sm_info->wblist_head);
1688 spin_lock_init(&sm_info->wblist_lock);
1689 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
1690 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
1691 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
1692 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
1693 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
1694 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
1695 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
1696
1697 err = build_sit_info(sbi);
1698 if (err)
1699 return err;
1700 err = build_free_segmap(sbi);
1701 if (err)
1702 return err;
1703 err = build_curseg(sbi);
1704 if (err)
1705 return err;
1706
1707 /* reinit free segmap based on SIT */
1708 build_sit_entries(sbi);
1709
1710 init_free_segmap(sbi);
1711 err = build_dirty_segmap(sbi);
1712 if (err)
1713 return err;
1714
1715 init_min_max_mtime(sbi);
1716 return 0;
1717 }
1718
discard_dirty_segmap(struct f2fs_sb_info * sbi,enum dirty_type dirty_type)1719 static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
1720 enum dirty_type dirty_type)
1721 {
1722 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1723
1724 mutex_lock(&dirty_i->seglist_lock);
1725 kfree(dirty_i->dirty_segmap[dirty_type]);
1726 dirty_i->nr_dirty[dirty_type] = 0;
1727 mutex_unlock(&dirty_i->seglist_lock);
1728 }
1729
destroy_victim_secmap(struct f2fs_sb_info * sbi)1730 static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
1731 {
1732 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1733 kfree(dirty_i->victim_secmap);
1734 }
1735
destroy_dirty_segmap(struct f2fs_sb_info * sbi)1736 static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
1737 {
1738 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1739 int i;
1740
1741 if (!dirty_i)
1742 return;
1743
1744 /* discard pre-free/dirty segments list */
1745 for (i = 0; i < NR_DIRTY_TYPE; i++)
1746 discard_dirty_segmap(sbi, i);
1747
1748 destroy_victim_secmap(sbi);
1749 SM_I(sbi)->dirty_info = NULL;
1750 kfree(dirty_i);
1751 }
1752
destroy_curseg(struct f2fs_sb_info * sbi)1753 static void destroy_curseg(struct f2fs_sb_info *sbi)
1754 {
1755 struct curseg_info *array = SM_I(sbi)->curseg_array;
1756 int i;
1757
1758 if (!array)
1759 return;
1760 SM_I(sbi)->curseg_array = NULL;
1761 for (i = 0; i < NR_CURSEG_TYPE; i++)
1762 kfree(array[i].sum_blk);
1763 kfree(array);
1764 }
1765
destroy_free_segmap(struct f2fs_sb_info * sbi)1766 static void destroy_free_segmap(struct f2fs_sb_info *sbi)
1767 {
1768 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
1769 if (!free_i)
1770 return;
1771 SM_I(sbi)->free_info = NULL;
1772 kfree(free_i->free_segmap);
1773 kfree(free_i->free_secmap);
1774 kfree(free_i);
1775 }
1776
destroy_sit_info(struct f2fs_sb_info * sbi)1777 static void destroy_sit_info(struct f2fs_sb_info *sbi)
1778 {
1779 struct sit_info *sit_i = SIT_I(sbi);
1780 unsigned int start;
1781
1782 if (!sit_i)
1783 return;
1784
1785 if (sit_i->sentries) {
1786 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1787 kfree(sit_i->sentries[start].cur_valid_map);
1788 kfree(sit_i->sentries[start].ckpt_valid_map);
1789 }
1790 }
1791 vfree(sit_i->sentries);
1792 vfree(sit_i->sec_entries);
1793 kfree(sit_i->dirty_sentries_bitmap);
1794
1795 SM_I(sbi)->sit_info = NULL;
1796 kfree(sit_i->sit_bitmap);
1797 kfree(sit_i);
1798 }
1799
destroy_segment_manager(struct f2fs_sb_info * sbi)1800 void destroy_segment_manager(struct f2fs_sb_info *sbi)
1801 {
1802 struct f2fs_sm_info *sm_info = SM_I(sbi);
1803 destroy_dirty_segmap(sbi);
1804 destroy_curseg(sbi);
1805 destroy_free_segmap(sbi);
1806 destroy_sit_info(sbi);
1807 sbi->sm_info = NULL;
1808 kfree(sm_info);
1809 }
1810