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