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