blob: 68e344f9e04274c41f48d3eb7caa1f689eb82625 [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"
Namjae Jeon6ec178d2013-04-23 17:51:43 +090021#include <trace/events/f2fs.h>
Jaegeuk Kim351df4b2012-11-02 17:09:16 +090022
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +090023/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +090024 * This function balances dirty node and dentry pages.
25 * In addition, it controls garbage collection.
26 */
27void f2fs_balance_fs(struct f2fs_sb_info *sbi)
28{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +090029 /*
Jaegeuk Kim029cd282012-12-21 17:20:21 +090030 * We should do GC or end up with checkpoint, if there are so many dirty
31 * dir/node pages without enough free segments.
Jaegeuk Kim351df4b2012-11-02 17:09:16 +090032 */
Jaegeuk Kim43727522013-02-04 15:11:17 +090033 if (has_not_enough_free_secs(sbi, 0)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +090034 mutex_lock(&sbi->gc_mutex);
Jaegeuk Kim408e9372013-01-03 17:55:52 +090035 f2fs_gc(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +090036 }
37}
38
39static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
40 enum dirty_type dirty_type)
41{
42 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
43
44 /* need not be added */
45 if (IS_CURSEG(sbi, segno))
46 return;
47
48 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
49 dirty_i->nr_dirty[dirty_type]++;
50
51 if (dirty_type == DIRTY) {
52 struct seg_entry *sentry = get_seg_entry(sbi, segno);
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +090053 enum dirty_type t = DIRTY_HOT_DATA;
54
Jaegeuk Kim351df4b2012-11-02 17:09:16 +090055 dirty_type = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +090056
Jaegeuk Kim351df4b2012-11-02 17:09:16 +090057 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
58 dirty_i->nr_dirty[dirty_type]++;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +090059
60 /* Only one bitmap should be set */
61 for (; t <= DIRTY_COLD_NODE; t++) {
62 if (t == dirty_type)
63 continue;
64 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
65 dirty_i->nr_dirty[t]--;
66 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +090067 }
68}
69
70static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
71 enum dirty_type dirty_type)
72{
73 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
74
75 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
76 dirty_i->nr_dirty[dirty_type]--;
77
78 if (dirty_type == DIRTY) {
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +090079 enum dirty_type t = DIRTY_HOT_DATA;
80
81 /* clear all the bitmaps */
82 for (; t <= DIRTY_COLD_NODE; t++)
83 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
84 dirty_i->nr_dirty[t]--;
85
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +090086 if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0)
87 clear_bit(GET_SECNO(sbi, segno),
88 dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +090089 }
90}
91
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +090092/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +090093 * Should not occur error such as -ENOMEM.
94 * Adding dirty entry into seglist is not critical operation.
95 * If a given segment is one of current working segments, it won't be added.
96 */
Haicheng Li8d8451a2013-06-13 16:59:28 +080097static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +090098{
99 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
100 unsigned short valid_blocks;
101
102 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
103 return;
104
105 mutex_lock(&dirty_i->seglist_lock);
106
107 valid_blocks = get_valid_blocks(sbi, segno, 0);
108
109 if (valid_blocks == 0) {
110 __locate_dirty_segment(sbi, segno, PRE);
111 __remove_dirty_segment(sbi, segno, DIRTY);
112 } else if (valid_blocks < sbi->blocks_per_seg) {
113 __locate_dirty_segment(sbi, segno, DIRTY);
114 } else {
115 /* Recovery routine with SSR needs this */
116 __remove_dirty_segment(sbi, segno, DIRTY);
117 }
118
119 mutex_unlock(&dirty_i->seglist_lock);
120 return;
121}
122
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900123/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900124 * Should call clear_prefree_segments after checkpoint is done.
125 */
126static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
127{
128 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Haicheng Li81fb5e82013-05-14 18:20:28 +0800129 unsigned int segno = -1;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900130 unsigned int total_segs = TOTAL_SEGS(sbi);
131
132 mutex_lock(&dirty_i->seglist_lock);
133 while (1) {
134 segno = find_next_bit(dirty_i->dirty_segmap[PRE], total_segs,
Haicheng Li81fb5e82013-05-14 18:20:28 +0800135 segno + 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900136 if (segno >= total_segs)
137 break;
138 __set_test_and_free(sbi, segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900139 }
140 mutex_unlock(&dirty_i->seglist_lock);
141}
142
143void clear_prefree_segments(struct f2fs_sb_info *sbi)
144{
145 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Haicheng Li81fb5e82013-05-14 18:20:28 +0800146 unsigned int segno = -1;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900147 unsigned int total_segs = TOTAL_SEGS(sbi);
148
149 mutex_lock(&dirty_i->seglist_lock);
150 while (1) {
151 segno = find_next_bit(dirty_i->dirty_segmap[PRE], total_segs,
Haicheng Li81fb5e82013-05-14 18:20:28 +0800152 segno + 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900153 if (segno >= total_segs)
154 break;
155
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900156 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[PRE]))
157 dirty_i->nr_dirty[PRE]--;
158
159 /* Let's use trim */
160 if (test_opt(sbi, DISCARD))
161 blkdev_issue_discard(sbi->sb->s_bdev,
162 START_BLOCK(sbi, segno) <<
163 sbi->log_sectors_per_block,
164 1 << (sbi->log_sectors_per_block +
165 sbi->log_blocks_per_seg),
166 GFP_NOFS, 0);
167 }
168 mutex_unlock(&dirty_i->seglist_lock);
169}
170
171static void __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
172{
173 struct sit_info *sit_i = SIT_I(sbi);
174 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap))
175 sit_i->dirty_sentries++;
176}
177
178static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
179 unsigned int segno, int modified)
180{
181 struct seg_entry *se = get_seg_entry(sbi, segno);
182 se->type = type;
183 if (modified)
184 __mark_sit_entry_dirty(sbi, segno);
185}
186
187static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
188{
189 struct seg_entry *se;
190 unsigned int segno, offset;
191 long int new_vblocks;
192
193 segno = GET_SEGNO(sbi, blkaddr);
194
195 se = get_seg_entry(sbi, segno);
196 new_vblocks = se->valid_blocks + del;
197 offset = GET_SEGOFF_FROM_SEG0(sbi, blkaddr) & (sbi->blocks_per_seg - 1);
198
199 BUG_ON((new_vblocks >> (sizeof(unsigned short) << 3) ||
200 (new_vblocks > sbi->blocks_per_seg)));
201
202 se->valid_blocks = new_vblocks;
203 se->mtime = get_mtime(sbi);
204 SIT_I(sbi)->max_mtime = se->mtime;
205
206 /* Update valid block bitmap */
207 if (del > 0) {
208 if (f2fs_set_bit(offset, se->cur_valid_map))
209 BUG();
210 } else {
211 if (!f2fs_clear_bit(offset, se->cur_valid_map))
212 BUG();
213 }
214 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
215 se->ckpt_valid_blocks += del;
216
217 __mark_sit_entry_dirty(sbi, segno);
218
219 /* update total number of valid blocks to be written in ckpt area */
220 SIT_I(sbi)->written_valid_blocks += del;
221
222 if (sbi->segs_per_sec > 1)
223 get_sec_entry(sbi, segno)->valid_blocks += del;
224}
225
226static void refresh_sit_entry(struct f2fs_sb_info *sbi,
227 block_t old_blkaddr, block_t new_blkaddr)
228{
229 update_sit_entry(sbi, new_blkaddr, 1);
230 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
231 update_sit_entry(sbi, old_blkaddr, -1);
232}
233
234void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
235{
236 unsigned int segno = GET_SEGNO(sbi, addr);
237 struct sit_info *sit_i = SIT_I(sbi);
238
239 BUG_ON(addr == NULL_ADDR);
240 if (addr == NEW_ADDR)
241 return;
242
243 /* add it into sit main buffer */
244 mutex_lock(&sit_i->sentry_lock);
245
246 update_sit_entry(sbi, addr, -1);
247
248 /* add it into dirty seglist */
249 locate_dirty_segment(sbi, segno);
250
251 mutex_unlock(&sit_i->sentry_lock);
252}
253
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900254/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900255 * This function should be resided under the curseg_mutex lock
256 */
257static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
Haicheng Lie79efe32013-06-13 16:59:27 +0800258 struct f2fs_summary *sum)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900259{
260 struct curseg_info *curseg = CURSEG_I(sbi, type);
261 void *addr = curseg->sum_blk;
Haicheng Lie79efe32013-06-13 16:59:27 +0800262 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900263 memcpy(addr, sum, sizeof(struct f2fs_summary));
264 return;
265}
266
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900267/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900268 * Calculate the number of current summary pages for writing
269 */
270int npages_for_summary_flush(struct f2fs_sb_info *sbi)
271{
272 int total_size_bytes = 0;
273 int valid_sum_count = 0;
274 int i, sum_space;
275
276 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
277 if (sbi->ckpt->alloc_type[i] == SSR)
278 valid_sum_count += sbi->blocks_per_seg;
279 else
280 valid_sum_count += curseg_blkoff(sbi, i);
281 }
282
283 total_size_bytes = valid_sum_count * (SUMMARY_SIZE + 1)
284 + sizeof(struct nat_journal) + 2
285 + sizeof(struct sit_journal) + 2;
286 sum_space = PAGE_CACHE_SIZE - SUM_FOOTER_SIZE;
287 if (total_size_bytes < sum_space)
288 return 1;
289 else if (total_size_bytes < 2 * sum_space)
290 return 2;
291 return 3;
292}
293
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900294/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900295 * Caller should put this summary page
296 */
297struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
298{
299 return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
300}
301
302static void write_sum_page(struct f2fs_sb_info *sbi,
303 struct f2fs_summary_block *sum_blk, block_t blk_addr)
304{
305 struct page *page = grab_meta_page(sbi, blk_addr);
306 void *kaddr = page_address(page);
307 memcpy(kaddr, sum_blk, PAGE_CACHE_SIZE);
308 set_page_dirty(page);
309 f2fs_put_page(page, 1);
310}
311
Jaegeuk Kim60374682013-03-31 13:58:51 +0900312static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
313{
314 struct curseg_info *curseg = CURSEG_I(sbi, type);
Haicheng Li81fb5e82013-05-14 18:20:28 +0800315 unsigned int segno = curseg->segno + 1;
Jaegeuk Kim60374682013-03-31 13:58:51 +0900316 struct free_segmap_info *free_i = FREE_I(sbi);
317
Haicheng Li81fb5e82013-05-14 18:20:28 +0800318 if (segno < TOTAL_SEGS(sbi) && segno % sbi->segs_per_sec)
319 return !test_bit(segno, free_i->free_segmap);
Jaegeuk Kim60374682013-03-31 13:58:51 +0900320 return 0;
321}
322
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900323/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900324 * Find a new segment from the free segments bitmap to right order
325 * This function should be returned with success, otherwise BUG
326 */
327static void get_new_segment(struct f2fs_sb_info *sbi,
328 unsigned int *newseg, bool new_sec, int dir)
329{
330 struct free_segmap_info *free_i = FREE_I(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900331 unsigned int segno, secno, zoneno;
Jaegeuk Kim53cf9522013-03-31 12:39:49 +0900332 unsigned int total_zones = TOTAL_SECS(sbi) / sbi->secs_per_zone;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900333 unsigned int hint = *newseg / sbi->segs_per_sec;
334 unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
335 unsigned int left_start = hint;
336 bool init = true;
337 int go_left = 0;
338 int i;
339
340 write_lock(&free_i->segmap_lock);
341
342 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
343 segno = find_next_zero_bit(free_i->free_segmap,
344 TOTAL_SEGS(sbi), *newseg + 1);
Jaegeuk Kim33afa7f2013-03-31 12:59:53 +0900345 if (segno - *newseg < sbi->segs_per_sec -
346 (*newseg % sbi->segs_per_sec))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900347 goto got_it;
348 }
349find_other_zone:
Jaegeuk Kim53cf9522013-03-31 12:39:49 +0900350 secno = find_next_zero_bit(free_i->free_secmap, TOTAL_SECS(sbi), hint);
351 if (secno >= TOTAL_SECS(sbi)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900352 if (dir == ALLOC_RIGHT) {
353 secno = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim53cf9522013-03-31 12:39:49 +0900354 TOTAL_SECS(sbi), 0);
355 BUG_ON(secno >= TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900356 } else {
357 go_left = 1;
358 left_start = hint - 1;
359 }
360 }
361 if (go_left == 0)
362 goto skip_left;
363
364 while (test_bit(left_start, free_i->free_secmap)) {
365 if (left_start > 0) {
366 left_start--;
367 continue;
368 }
369 left_start = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim53cf9522013-03-31 12:39:49 +0900370 TOTAL_SECS(sbi), 0);
371 BUG_ON(left_start >= TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900372 break;
373 }
374 secno = left_start;
375skip_left:
376 hint = secno;
377 segno = secno * sbi->segs_per_sec;
378 zoneno = secno / sbi->secs_per_zone;
379
380 /* give up on finding another zone */
381 if (!init)
382 goto got_it;
383 if (sbi->secs_per_zone == 1)
384 goto got_it;
385 if (zoneno == old_zoneno)
386 goto got_it;
387 if (dir == ALLOC_LEFT) {
388 if (!go_left && zoneno + 1 >= total_zones)
389 goto got_it;
390 if (go_left && zoneno == 0)
391 goto got_it;
392 }
393 for (i = 0; i < NR_CURSEG_TYPE; i++)
394 if (CURSEG_I(sbi, i)->zone == zoneno)
395 break;
396
397 if (i < NR_CURSEG_TYPE) {
398 /* zone is in user, try another */
399 if (go_left)
400 hint = zoneno * sbi->secs_per_zone - 1;
401 else if (zoneno + 1 >= total_zones)
402 hint = 0;
403 else
404 hint = (zoneno + 1) * sbi->secs_per_zone;
405 init = false;
406 goto find_other_zone;
407 }
408got_it:
409 /* set it as dirty segment in free segmap */
410 BUG_ON(test_bit(segno, free_i->free_segmap));
411 __set_inuse(sbi, segno);
412 *newseg = segno;
413 write_unlock(&free_i->segmap_lock);
414}
415
416static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
417{
418 struct curseg_info *curseg = CURSEG_I(sbi, type);
419 struct summary_footer *sum_footer;
420
421 curseg->segno = curseg->next_segno;
422 curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
423 curseg->next_blkoff = 0;
424 curseg->next_segno = NULL_SEGNO;
425
426 sum_footer = &(curseg->sum_blk->footer);
427 memset(sum_footer, 0, sizeof(struct summary_footer));
428 if (IS_DATASEG(type))
429 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
430 if (IS_NODESEG(type))
431 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
432 __set_sit_entry_type(sbi, type, curseg->segno, modified);
433}
434
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900435/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900436 * Allocate a current working segment.
437 * This function always allocates a free segment in LFS manner.
438 */
439static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
440{
441 struct curseg_info *curseg = CURSEG_I(sbi, type);
442 unsigned int segno = curseg->segno;
443 int dir = ALLOC_LEFT;
444
445 write_sum_page(sbi, curseg->sum_blk,
Haicheng Li81fb5e82013-05-14 18:20:28 +0800446 GET_SUM_BLOCK(sbi, segno));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900447 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
448 dir = ALLOC_RIGHT;
449
450 if (test_opt(sbi, NOHEAP))
451 dir = ALLOC_RIGHT;
452
453 get_new_segment(sbi, &segno, new_sec, dir);
454 curseg->next_segno = segno;
455 reset_curseg(sbi, type, 1);
456 curseg->alloc_type = LFS;
457}
458
459static void __next_free_blkoff(struct f2fs_sb_info *sbi,
460 struct curseg_info *seg, block_t start)
461{
462 struct seg_entry *se = get_seg_entry(sbi, seg->segno);
463 block_t ofs;
464 for (ofs = start; ofs < sbi->blocks_per_seg; ofs++) {
465 if (!f2fs_test_bit(ofs, se->ckpt_valid_map)
466 && !f2fs_test_bit(ofs, se->cur_valid_map))
467 break;
468 }
469 seg->next_blkoff = ofs;
470}
471
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900472/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900473 * If a segment is written by LFS manner, next block offset is just obtained
474 * by increasing the current block offset. However, if a segment is written by
475 * SSR manner, next block offset obtained by calling __next_free_blkoff
476 */
477static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
478 struct curseg_info *seg)
479{
480 if (seg->alloc_type == SSR)
481 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
482 else
483 seg->next_blkoff++;
484}
485
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900486/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900487 * This function always allocates a used segment (from dirty seglist) by SSR
488 * manner, so it should recover the existing segment information of valid blocks
489 */
490static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
491{
492 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
493 struct curseg_info *curseg = CURSEG_I(sbi, type);
494 unsigned int new_segno = curseg->next_segno;
495 struct f2fs_summary_block *sum_node;
496 struct page *sum_page;
497
498 write_sum_page(sbi, curseg->sum_blk,
499 GET_SUM_BLOCK(sbi, curseg->segno));
500 __set_test_and_inuse(sbi, new_segno);
501
502 mutex_lock(&dirty_i->seglist_lock);
503 __remove_dirty_segment(sbi, new_segno, PRE);
504 __remove_dirty_segment(sbi, new_segno, DIRTY);
505 mutex_unlock(&dirty_i->seglist_lock);
506
507 reset_curseg(sbi, type, 1);
508 curseg->alloc_type = SSR;
509 __next_free_blkoff(sbi, curseg, 0);
510
511 if (reuse) {
512 sum_page = get_sum_page(sbi, new_segno);
513 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
514 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
515 f2fs_put_page(sum_page, 1);
516 }
517}
518
Jaegeuk Kim43727522013-02-04 15:11:17 +0900519static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
520{
521 struct curseg_info *curseg = CURSEG_I(sbi, type);
522 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
523
524 if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0))
525 return v_ops->get_victim(sbi,
526 &(curseg)->next_segno, BG_GC, type, SSR);
527
528 /* For data segments, let's do SSR more intensively */
529 for (; type >= CURSEG_HOT_DATA; type--)
530 if (v_ops->get_victim(sbi, &(curseg)->next_segno,
531 BG_GC, type, SSR))
532 return 1;
533 return 0;
534}
535
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900536/*
537 * flush out current segment and replace it with new segment
538 * This function should be returned with success, otherwise BUG
539 */
540static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
541 int type, bool force)
542{
543 struct curseg_info *curseg = CURSEG_I(sbi, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900544
545 if (force) {
546 new_curseg(sbi, type, true);
547 goto out;
548 }
549
Jaegeuk Kim763bfe12013-06-27 09:59:40 +0900550 if (type == CURSEG_WARM_NODE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900551 new_curseg(sbi, type, false);
Jaegeuk Kim60374682013-03-31 13:58:51 +0900552 else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
553 new_curseg(sbi, type, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900554 else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
555 change_curseg(sbi, type, true);
556 else
557 new_curseg(sbi, type, false);
558out:
Namjae Jeon35b09d82013-05-23 22:57:53 +0900559#ifdef CONFIG_F2FS_STAT_FS
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900560 sbi->segment_count[curseg->alloc_type]++;
Namjae Jeon35b09d82013-05-23 22:57:53 +0900561#endif
562 return;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900563}
564
565void allocate_new_segments(struct f2fs_sb_info *sbi)
566{
567 struct curseg_info *curseg;
568 unsigned int old_curseg;
569 int i;
570
571 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
572 curseg = CURSEG_I(sbi, i);
573 old_curseg = curseg->segno;
574 SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
575 locate_dirty_segment(sbi, old_curseg);
576 }
577}
578
579static const struct segment_allocation default_salloc_ops = {
580 .allocate_segment = allocate_segment_by_default,
581};
582
583static void f2fs_end_io_write(struct bio *bio, int err)
584{
585 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
586 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
587 struct bio_private *p = bio->bi_private;
588
589 do {
590 struct page *page = bvec->bv_page;
591
592 if (--bvec >= bio->bi_io_vec)
593 prefetchw(&bvec->bv_page->flags);
594 if (!uptodate) {
595 SetPageError(page);
596 if (page->mapping)
597 set_bit(AS_EIO, &page->mapping->flags);
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900598 set_ckpt_flags(p->sbi->ckpt, CP_ERROR_FLAG);
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900599 p->sbi->sb->s_flags |= MS_RDONLY;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900600 }
601 end_page_writeback(page);
602 dec_page_count(p->sbi, F2FS_WRITEBACK);
603 } while (bvec >= bio->bi_io_vec);
604
605 if (p->is_sync)
606 complete(p->wait);
607 kfree(p);
608 bio_put(bio);
609}
610
Jaegeuk Kim3cd8a232012-12-10 09:26:05 +0900611struct bio *f2fs_bio_alloc(struct block_device *bdev, int npages)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900612{
613 struct bio *bio;
Jaegeuk Kim3cd8a232012-12-10 09:26:05 +0900614
615 /* No failure on bio allocation */
616 bio = bio_alloc(GFP_NOIO, npages);
617 bio->bi_bdev = bdev;
Gu Zhengd8207f62013-07-25 11:30:01 +0800618 bio->bi_private = NULL;
619
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900620 return bio;
621}
622
623static void do_submit_bio(struct f2fs_sb_info *sbi,
624 enum page_type type, bool sync)
625{
626 int rw = sync ? WRITE_SYNC : WRITE;
627 enum page_type btype = type > META ? META : type;
628
629 if (type >= META_FLUSH)
630 rw = WRITE_FLUSH_FUA;
631
Namjae Jeon86804412013-04-25 11:45:21 +0900632 if (btype == META)
633 rw |= REQ_META;
634
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900635 if (sbi->bio[btype]) {
636 struct bio_private *p = sbi->bio[btype]->bi_private;
637 p->sbi = sbi;
638 sbi->bio[btype]->bi_end_io = f2fs_end_io_write;
Namjae Jeon6ec178d2013-04-23 17:51:43 +0900639
640 trace_f2fs_do_submit_bio(sbi->sb, btype, sync, sbi->bio[btype]);
641
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900642 if (type == META_FLUSH) {
643 DECLARE_COMPLETION_ONSTACK(wait);
644 p->is_sync = true;
645 p->wait = &wait;
646 submit_bio(rw, sbi->bio[btype]);
647 wait_for_completion(&wait);
648 } else {
649 p->is_sync = false;
650 submit_bio(rw, sbi->bio[btype]);
651 }
652 sbi->bio[btype] = NULL;
653 }
654}
655
656void f2fs_submit_bio(struct f2fs_sb_info *sbi, enum page_type type, bool sync)
657{
658 down_write(&sbi->bio_sem);
659 do_submit_bio(sbi, type, sync);
660 up_write(&sbi->bio_sem);
661}
662
663static void submit_write_page(struct f2fs_sb_info *sbi, struct page *page,
664 block_t blk_addr, enum page_type type)
665{
666 struct block_device *bdev = sbi->sb->s_bdev;
667
668 verify_block_addr(sbi, blk_addr);
669
670 down_write(&sbi->bio_sem);
671
672 inc_page_count(sbi, F2FS_WRITEBACK);
673
674 if (sbi->bio[type] && sbi->last_block_in_bio[type] != blk_addr - 1)
675 do_submit_bio(sbi, type, false);
676alloc_new:
Jaegeuk Kim3cd8a232012-12-10 09:26:05 +0900677 if (sbi->bio[type] == NULL) {
Gu Zhengd8207f62013-07-25 11:30:01 +0800678 struct bio_private *priv;
679retry:
680 priv = kmalloc(sizeof(struct bio_private), GFP_NOFS);
681 if (!priv) {
682 cond_resched();
683 goto retry;
684 }
685
Jaegeuk Kimac5d1562013-04-29 16:58:39 +0900686 sbi->bio[type] = f2fs_bio_alloc(bdev, max_hw_blocks(sbi));
Jaegeuk Kim3cd8a232012-12-10 09:26:05 +0900687 sbi->bio[type]->bi_sector = SECTOR_FROM_BLOCK(sbi, blk_addr);
Gu Zhengd8207f62013-07-25 11:30:01 +0800688 sbi->bio[type]->bi_private = priv;
Jaegeuk Kim3cd8a232012-12-10 09:26:05 +0900689 /*
690 * The end_io will be assigned at the sumbission phase.
691 * Until then, let bio_add_page() merge consecutive IOs as much
692 * as possible.
693 */
694 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900695
696 if (bio_add_page(sbi->bio[type], page, PAGE_CACHE_SIZE, 0) <
697 PAGE_CACHE_SIZE) {
698 do_submit_bio(sbi, type, false);
699 goto alloc_new;
700 }
701
702 sbi->last_block_in_bio[type] = blk_addr;
703
704 up_write(&sbi->bio_sem);
Namjae Jeon6ec178d2013-04-23 17:51:43 +0900705 trace_f2fs_submit_write_page(page, blk_addr, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900706}
707
Jin Xua5694692013-08-05 20:02:04 +0800708void f2fs_wait_on_page_writeback(struct page *page,
709 enum page_type type, bool sync)
710{
711 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
712 if (PageWriteback(page)) {
713 f2fs_submit_bio(sbi, type, sync);
714 wait_on_page_writeback(page);
715 }
716}
717
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900718static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
719{
720 struct curseg_info *curseg = CURSEG_I(sbi, type);
721 if (curseg->next_blkoff < sbi->blocks_per_seg)
722 return true;
723 return false;
724}
725
726static int __get_segment_type_2(struct page *page, enum page_type p_type)
727{
728 if (p_type == DATA)
729 return CURSEG_HOT_DATA;
730 else
731 return CURSEG_HOT_NODE;
732}
733
734static int __get_segment_type_4(struct page *page, enum page_type p_type)
735{
736 if (p_type == DATA) {
737 struct inode *inode = page->mapping->host;
738
739 if (S_ISDIR(inode->i_mode))
740 return CURSEG_HOT_DATA;
741 else
742 return CURSEG_COLD_DATA;
743 } else {
744 if (IS_DNODE(page) && !is_cold_node(page))
745 return CURSEG_HOT_NODE;
746 else
747 return CURSEG_COLD_NODE;
748 }
749}
750
751static int __get_segment_type_6(struct page *page, enum page_type p_type)
752{
753 if (p_type == DATA) {
754 struct inode *inode = page->mapping->host;
755
756 if (S_ISDIR(inode->i_mode))
757 return CURSEG_HOT_DATA;
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900758 else if (is_cold_data(page) || file_is_cold(inode))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900759 return CURSEG_COLD_DATA;
760 else
761 return CURSEG_WARM_DATA;
762 } else {
763 if (IS_DNODE(page))
764 return is_cold_node(page) ? CURSEG_WARM_NODE :
765 CURSEG_HOT_NODE;
766 else
767 return CURSEG_COLD_NODE;
768 }
769}
770
771static int __get_segment_type(struct page *page, enum page_type p_type)
772{
773 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
774 switch (sbi->active_logs) {
775 case 2:
776 return __get_segment_type_2(page, p_type);
777 case 4:
778 return __get_segment_type_4(page, p_type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900779 }
Jaegeuk Kim12a67142012-12-21 11:47:05 +0900780 /* NR_CURSEG_TYPE(6) logs by default */
781 BUG_ON(sbi->active_logs != NR_CURSEG_TYPE);
782 return __get_segment_type_6(page, p_type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900783}
784
785static void do_write_page(struct f2fs_sb_info *sbi, struct page *page,
786 block_t old_blkaddr, block_t *new_blkaddr,
787 struct f2fs_summary *sum, enum page_type p_type)
788{
789 struct sit_info *sit_i = SIT_I(sbi);
790 struct curseg_info *curseg;
791 unsigned int old_cursegno;
792 int type;
793
794 type = __get_segment_type(page, p_type);
795 curseg = CURSEG_I(sbi, type);
796
797 mutex_lock(&curseg->curseg_mutex);
798
799 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
800 old_cursegno = curseg->segno;
801
802 /*
803 * __add_sum_entry should be resided under the curseg_mutex
804 * because, this function updates a summary entry in the
805 * current summary block.
806 */
Haicheng Lie79efe32013-06-13 16:59:27 +0800807 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900808
809 mutex_lock(&sit_i->sentry_lock);
810 __refresh_next_blkoff(sbi, curseg);
Namjae Jeon35b09d82013-05-23 22:57:53 +0900811#ifdef CONFIG_F2FS_STAT_FS
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900812 sbi->block_count[curseg->alloc_type]++;
Namjae Jeon35b09d82013-05-23 22:57:53 +0900813#endif
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900814
815 /*
816 * SIT information should be updated before segment allocation,
817 * since SSR needs latest valid block information.
818 */
819 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
820
821 if (!__has_curseg_space(sbi, type))
822 sit_i->s_ops->allocate_segment(sbi, type, false);
823
824 locate_dirty_segment(sbi, old_cursegno);
825 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
826 mutex_unlock(&sit_i->sentry_lock);
827
828 if (p_type == NODE)
829 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
830
831 /* writeout dirty page into bdev */
832 submit_write_page(sbi, page, *new_blkaddr, p_type);
833
834 mutex_unlock(&curseg->curseg_mutex);
835}
836
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900837void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900838{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900839 set_page_writeback(page);
840 submit_write_page(sbi, page, page->index, META);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900841}
842
843void write_node_page(struct f2fs_sb_info *sbi, struct page *page,
844 unsigned int nid, block_t old_blkaddr, block_t *new_blkaddr)
845{
846 struct f2fs_summary sum;
847 set_summary(&sum, nid, 0, 0);
848 do_write_page(sbi, page, old_blkaddr, new_blkaddr, &sum, NODE);
849}
850
851void write_data_page(struct inode *inode, struct page *page,
852 struct dnode_of_data *dn, block_t old_blkaddr,
853 block_t *new_blkaddr)
854{
855 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
856 struct f2fs_summary sum;
857 struct node_info ni;
858
859 BUG_ON(old_blkaddr == NULL_ADDR);
860 get_node_info(sbi, dn->nid, &ni);
861 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
862
863 do_write_page(sbi, page, old_blkaddr,
864 new_blkaddr, &sum, DATA);
865}
866
867void rewrite_data_page(struct f2fs_sb_info *sbi, struct page *page,
868 block_t old_blk_addr)
869{
870 submit_write_page(sbi, page, old_blk_addr, DATA);
871}
872
873void recover_data_page(struct f2fs_sb_info *sbi,
874 struct page *page, struct f2fs_summary *sum,
875 block_t old_blkaddr, block_t new_blkaddr)
876{
877 struct sit_info *sit_i = SIT_I(sbi);
878 struct curseg_info *curseg;
879 unsigned int segno, old_cursegno;
880 struct seg_entry *se;
881 int type;
882
883 segno = GET_SEGNO(sbi, new_blkaddr);
884 se = get_seg_entry(sbi, segno);
885 type = se->type;
886
887 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
888 if (old_blkaddr == NULL_ADDR)
889 type = CURSEG_COLD_DATA;
890 else
891 type = CURSEG_WARM_DATA;
892 }
893 curseg = CURSEG_I(sbi, type);
894
895 mutex_lock(&curseg->curseg_mutex);
896 mutex_lock(&sit_i->sentry_lock);
897
898 old_cursegno = curseg->segno;
899
900 /* change the current segment */
901 if (segno != curseg->segno) {
902 curseg->next_segno = segno;
903 change_curseg(sbi, type, true);
904 }
905
906 curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, new_blkaddr) &
907 (sbi->blocks_per_seg - 1);
Haicheng Lie79efe32013-06-13 16:59:27 +0800908 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900909
910 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
911
912 locate_dirty_segment(sbi, old_cursegno);
913 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
914
915 mutex_unlock(&sit_i->sentry_lock);
916 mutex_unlock(&curseg->curseg_mutex);
917}
918
919void rewrite_node_page(struct f2fs_sb_info *sbi,
920 struct page *page, struct f2fs_summary *sum,
921 block_t old_blkaddr, block_t new_blkaddr)
922{
923 struct sit_info *sit_i = SIT_I(sbi);
924 int type = CURSEG_WARM_NODE;
925 struct curseg_info *curseg;
926 unsigned int segno, old_cursegno;
927 block_t next_blkaddr = next_blkaddr_of_node(page);
928 unsigned int next_segno = GET_SEGNO(sbi, next_blkaddr);
929
930 curseg = CURSEG_I(sbi, type);
931
932 mutex_lock(&curseg->curseg_mutex);
933 mutex_lock(&sit_i->sentry_lock);
934
935 segno = GET_SEGNO(sbi, new_blkaddr);
936 old_cursegno = curseg->segno;
937
938 /* change the current segment */
939 if (segno != curseg->segno) {
940 curseg->next_segno = segno;
941 change_curseg(sbi, type, true);
942 }
943 curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, new_blkaddr) &
944 (sbi->blocks_per_seg - 1);
Haicheng Lie79efe32013-06-13 16:59:27 +0800945 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900946
947 /* change the current log to the next block addr in advance */
948 if (next_segno != segno) {
949 curseg->next_segno = next_segno;
950 change_curseg(sbi, type, true);
951 }
952 curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, next_blkaddr) &
953 (sbi->blocks_per_seg - 1);
954
955 /* rewrite node page */
956 set_page_writeback(page);
957 submit_write_page(sbi, page, new_blkaddr, NODE);
958 f2fs_submit_bio(sbi, NODE, true);
959 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
960
961 locate_dirty_segment(sbi, old_cursegno);
962 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
963
964 mutex_unlock(&sit_i->sentry_lock);
965 mutex_unlock(&curseg->curseg_mutex);
966}
967
968static int read_compacted_summaries(struct f2fs_sb_info *sbi)
969{
970 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
971 struct curseg_info *seg_i;
972 unsigned char *kaddr;
973 struct page *page;
974 block_t start;
975 int i, j, offset;
976
977 start = start_sum_block(sbi);
978
979 page = get_meta_page(sbi, start++);
980 kaddr = (unsigned char *)page_address(page);
981
982 /* Step 1: restore nat cache */
983 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
984 memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
985
986 /* Step 2: restore sit cache */
987 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
988 memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
989 SUM_JOURNAL_SIZE);
990 offset = 2 * SUM_JOURNAL_SIZE;
991
992 /* Step 3: restore summary entries */
993 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
994 unsigned short blk_off;
995 unsigned int segno;
996
997 seg_i = CURSEG_I(sbi, i);
998 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
999 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
1000 seg_i->next_segno = segno;
1001 reset_curseg(sbi, i, 0);
1002 seg_i->alloc_type = ckpt->alloc_type[i];
1003 seg_i->next_blkoff = blk_off;
1004
1005 if (seg_i->alloc_type == SSR)
1006 blk_off = sbi->blocks_per_seg;
1007
1008 for (j = 0; j < blk_off; j++) {
1009 struct f2fs_summary *s;
1010 s = (struct f2fs_summary *)(kaddr + offset);
1011 seg_i->sum_blk->entries[j] = *s;
1012 offset += SUMMARY_SIZE;
1013 if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1014 SUM_FOOTER_SIZE)
1015 continue;
1016
1017 f2fs_put_page(page, 1);
1018 page = NULL;
1019
1020 page = get_meta_page(sbi, start++);
1021 kaddr = (unsigned char *)page_address(page);
1022 offset = 0;
1023 }
1024 }
1025 f2fs_put_page(page, 1);
1026 return 0;
1027}
1028
1029static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
1030{
1031 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1032 struct f2fs_summary_block *sum;
1033 struct curseg_info *curseg;
1034 struct page *new;
1035 unsigned short blk_off;
1036 unsigned int segno = 0;
1037 block_t blk_addr = 0;
1038
1039 /* get segment number and block addr */
1040 if (IS_DATASEG(type)) {
1041 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
1042 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
1043 CURSEG_HOT_DATA]);
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001044 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001045 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
1046 else
1047 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
1048 } else {
1049 segno = le32_to_cpu(ckpt->cur_node_segno[type -
1050 CURSEG_HOT_NODE]);
1051 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
1052 CURSEG_HOT_NODE]);
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001053 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001054 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
1055 type - CURSEG_HOT_NODE);
1056 else
1057 blk_addr = GET_SUM_BLOCK(sbi, segno);
1058 }
1059
1060 new = get_meta_page(sbi, blk_addr);
1061 sum = (struct f2fs_summary_block *)page_address(new);
1062
1063 if (IS_NODESEG(type)) {
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001064 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001065 struct f2fs_summary *ns = &sum->entries[0];
1066 int i;
1067 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
1068 ns->version = 0;
1069 ns->ofs_in_node = 0;
1070 }
1071 } else {
1072 if (restore_node_summary(sbi, segno, sum)) {
1073 f2fs_put_page(new, 1);
1074 return -EINVAL;
1075 }
1076 }
1077 }
1078
1079 /* set uncompleted segment to curseg */
1080 curseg = CURSEG_I(sbi, type);
1081 mutex_lock(&curseg->curseg_mutex);
1082 memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
1083 curseg->next_segno = segno;
1084 reset_curseg(sbi, type, 0);
1085 curseg->alloc_type = ckpt->alloc_type[type];
1086 curseg->next_blkoff = blk_off;
1087 mutex_unlock(&curseg->curseg_mutex);
1088 f2fs_put_page(new, 1);
1089 return 0;
1090}
1091
1092static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
1093{
1094 int type = CURSEG_HOT_DATA;
1095
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001096 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001097 /* restore for compacted data summary */
1098 if (read_compacted_summaries(sbi))
1099 return -EINVAL;
1100 type = CURSEG_HOT_NODE;
1101 }
1102
1103 for (; type <= CURSEG_COLD_NODE; type++)
1104 if (read_normal_summaries(sbi, type))
1105 return -EINVAL;
1106 return 0;
1107}
1108
1109static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
1110{
1111 struct page *page;
1112 unsigned char *kaddr;
1113 struct f2fs_summary *summary;
1114 struct curseg_info *seg_i;
1115 int written_size = 0;
1116 int i, j;
1117
1118 page = grab_meta_page(sbi, blkaddr++);
1119 kaddr = (unsigned char *)page_address(page);
1120
1121 /* Step 1: write nat cache */
1122 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1123 memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
1124 written_size += SUM_JOURNAL_SIZE;
1125
1126 /* Step 2: write sit cache */
1127 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1128 memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
1129 SUM_JOURNAL_SIZE);
1130 written_size += SUM_JOURNAL_SIZE;
1131
1132 set_page_dirty(page);
1133
1134 /* Step 3: write summary entries */
1135 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1136 unsigned short blkoff;
1137 seg_i = CURSEG_I(sbi, i);
1138 if (sbi->ckpt->alloc_type[i] == SSR)
1139 blkoff = sbi->blocks_per_seg;
1140 else
1141 blkoff = curseg_blkoff(sbi, i);
1142
1143 for (j = 0; j < blkoff; j++) {
1144 if (!page) {
1145 page = grab_meta_page(sbi, blkaddr++);
1146 kaddr = (unsigned char *)page_address(page);
1147 written_size = 0;
1148 }
1149 summary = (struct f2fs_summary *)(kaddr + written_size);
1150 *summary = seg_i->sum_blk->entries[j];
1151 written_size += SUMMARY_SIZE;
1152 set_page_dirty(page);
1153
1154 if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1155 SUM_FOOTER_SIZE)
1156 continue;
1157
1158 f2fs_put_page(page, 1);
1159 page = NULL;
1160 }
1161 }
1162 if (page)
1163 f2fs_put_page(page, 1);
1164}
1165
1166static void write_normal_summaries(struct f2fs_sb_info *sbi,
1167 block_t blkaddr, int type)
1168{
1169 int i, end;
1170 if (IS_DATASEG(type))
1171 end = type + NR_CURSEG_DATA_TYPE;
1172 else
1173 end = type + NR_CURSEG_NODE_TYPE;
1174
1175 for (i = type; i < end; i++) {
1176 struct curseg_info *sum = CURSEG_I(sbi, i);
1177 mutex_lock(&sum->curseg_mutex);
1178 write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
1179 mutex_unlock(&sum->curseg_mutex);
1180 }
1181}
1182
1183void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1184{
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001185 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001186 write_compacted_summaries(sbi, start_blk);
1187 else
1188 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
1189}
1190
1191void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1192{
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001193 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001194 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
1195 return;
1196}
1197
1198int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
1199 unsigned int val, int alloc)
1200{
1201 int i;
1202
1203 if (type == NAT_JOURNAL) {
1204 for (i = 0; i < nats_in_cursum(sum); i++) {
1205 if (le32_to_cpu(nid_in_journal(sum, i)) == val)
1206 return i;
1207 }
1208 if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
1209 return update_nats_in_cursum(sum, 1);
1210 } else if (type == SIT_JOURNAL) {
1211 for (i = 0; i < sits_in_cursum(sum); i++)
1212 if (le32_to_cpu(segno_in_journal(sum, i)) == val)
1213 return i;
1214 if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
1215 return update_sits_in_cursum(sum, 1);
1216 }
1217 return -1;
1218}
1219
1220static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
1221 unsigned int segno)
1222{
1223 struct sit_info *sit_i = SIT_I(sbi);
1224 unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno);
1225 block_t blk_addr = sit_i->sit_base_addr + offset;
1226
1227 check_seg_range(sbi, segno);
1228
1229 /* calculate sit block address */
1230 if (f2fs_test_bit(offset, sit_i->sit_bitmap))
1231 blk_addr += sit_i->sit_blocks;
1232
1233 return get_meta_page(sbi, blk_addr);
1234}
1235
1236static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
1237 unsigned int start)
1238{
1239 struct sit_info *sit_i = SIT_I(sbi);
1240 struct page *src_page, *dst_page;
1241 pgoff_t src_off, dst_off;
1242 void *src_addr, *dst_addr;
1243
1244 src_off = current_sit_addr(sbi, start);
1245 dst_off = next_sit_addr(sbi, src_off);
1246
1247 /* get current sit block page without lock */
1248 src_page = get_meta_page(sbi, src_off);
1249 dst_page = grab_meta_page(sbi, dst_off);
1250 BUG_ON(PageDirty(src_page));
1251
1252 src_addr = page_address(src_page);
1253 dst_addr = page_address(dst_page);
1254 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
1255
1256 set_page_dirty(dst_page);
1257 f2fs_put_page(src_page, 1);
1258
1259 set_to_next_sit(sit_i, start);
1260
1261 return dst_page;
1262}
1263
1264static bool flush_sits_in_journal(struct f2fs_sb_info *sbi)
1265{
1266 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1267 struct f2fs_summary_block *sum = curseg->sum_blk;
1268 int i;
1269
1270 /*
1271 * If the journal area in the current summary is full of sit entries,
1272 * all the sit entries will be flushed. Otherwise the sit entries
1273 * are not able to replace with newly hot sit entries.
1274 */
1275 if (sits_in_cursum(sum) >= SIT_JOURNAL_ENTRIES) {
1276 for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
1277 unsigned int segno;
1278 segno = le32_to_cpu(segno_in_journal(sum, i));
1279 __mark_sit_entry_dirty(sbi, segno);
1280 }
1281 update_sits_in_cursum(sum, -sits_in_cursum(sum));
1282 return 1;
1283 }
1284 return 0;
1285}
1286
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001287/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001288 * CP calls this function, which flushes SIT entries including sit_journal,
1289 * and moves prefree segs to free segs.
1290 */
1291void flush_sit_entries(struct f2fs_sb_info *sbi)
1292{
1293 struct sit_info *sit_i = SIT_I(sbi);
1294 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
1295 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1296 struct f2fs_summary_block *sum = curseg->sum_blk;
1297 unsigned long nsegs = TOTAL_SEGS(sbi);
1298 struct page *page = NULL;
1299 struct f2fs_sit_block *raw_sit = NULL;
1300 unsigned int start = 0, end = 0;
1301 unsigned int segno = -1;
1302 bool flushed;
1303
1304 mutex_lock(&curseg->curseg_mutex);
1305 mutex_lock(&sit_i->sentry_lock);
1306
1307 /*
1308 * "flushed" indicates whether sit entries in journal are flushed
1309 * to the SIT area or not.
1310 */
1311 flushed = flush_sits_in_journal(sbi);
1312
1313 while ((segno = find_next_bit(bitmap, nsegs, segno + 1)) < nsegs) {
1314 struct seg_entry *se = get_seg_entry(sbi, segno);
1315 int sit_offset, offset;
1316
1317 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
1318
1319 if (flushed)
1320 goto to_sit_page;
1321
1322 offset = lookup_journal_in_cursum(sum, SIT_JOURNAL, segno, 1);
1323 if (offset >= 0) {
1324 segno_in_journal(sum, offset) = cpu_to_le32(segno);
1325 seg_info_to_raw_sit(se, &sit_in_journal(sum, offset));
1326 goto flush_done;
1327 }
1328to_sit_page:
1329 if (!page || (start > segno) || (segno > end)) {
1330 if (page) {
1331 f2fs_put_page(page, 1);
1332 page = NULL;
1333 }
1334
1335 start = START_SEGNO(sit_i, segno);
1336 end = start + SIT_ENTRY_PER_BLOCK - 1;
1337
1338 /* read sit block that will be updated */
1339 page = get_next_sit_page(sbi, start);
1340 raw_sit = page_address(page);
1341 }
1342
1343 /* udpate entry in SIT block */
1344 seg_info_to_raw_sit(se, &raw_sit->entries[sit_offset]);
1345flush_done:
1346 __clear_bit(segno, bitmap);
1347 sit_i->dirty_sentries--;
1348 }
1349 mutex_unlock(&sit_i->sentry_lock);
1350 mutex_unlock(&curseg->curseg_mutex);
1351
1352 /* writeout last modified SIT block */
1353 f2fs_put_page(page, 1);
1354
1355 set_prefree_as_free_segments(sbi);
1356}
1357
1358static int build_sit_info(struct f2fs_sb_info *sbi)
1359{
1360 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1361 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1362 struct sit_info *sit_i;
1363 unsigned int sit_segs, start;
1364 char *src_bitmap, *dst_bitmap;
1365 unsigned int bitmap_size;
1366
1367 /* allocate memory for SIT information */
1368 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
1369 if (!sit_i)
1370 return -ENOMEM;
1371
1372 SM_I(sbi)->sit_info = sit_i;
1373
1374 sit_i->sentries = vzalloc(TOTAL_SEGS(sbi) * sizeof(struct seg_entry));
1375 if (!sit_i->sentries)
1376 return -ENOMEM;
1377
1378 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1379 sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1380 if (!sit_i->dirty_sentries_bitmap)
1381 return -ENOMEM;
1382
1383 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1384 sit_i->sentries[start].cur_valid_map
1385 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1386 sit_i->sentries[start].ckpt_valid_map
1387 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1388 if (!sit_i->sentries[start].cur_valid_map
1389 || !sit_i->sentries[start].ckpt_valid_map)
1390 return -ENOMEM;
1391 }
1392
1393 if (sbi->segs_per_sec > 1) {
Jaegeuk Kim53cf9522013-03-31 12:39:49 +09001394 sit_i->sec_entries = vzalloc(TOTAL_SECS(sbi) *
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001395 sizeof(struct sec_entry));
1396 if (!sit_i->sec_entries)
1397 return -ENOMEM;
1398 }
1399
1400 /* get information related with SIT */
1401 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
1402
1403 /* setup SIT bitmap from ckeckpoint pack */
1404 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
1405 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
1406
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001407 dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001408 if (!dst_bitmap)
1409 return -ENOMEM;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001410
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
Jaegeuk Kim53cf9522013-03-31 12:39:49 +09001445 sec_bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001446 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);
Namjae Jeon8736fbf2013-06-16 09:49:11 +09001544 unsigned int segno = 0, offset = 0, total_segs = TOTAL_SEGS(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001545 unsigned short valid_blocks;
1546
Namjae Jeon8736fbf2013-06-16 09:49:11 +09001547 while (1) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001548 /* find dirty segment based on free segmap */
Namjae Jeon8736fbf2013-06-16 09:49:11 +09001549 segno = find_next_inuse(free_i, total_segs, offset);
1550 if (segno >= total_segs)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001551 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
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001562static int init_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001563{
1564 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001565 unsigned int bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001566
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001567 dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL);
1568 if (!dirty_i->victim_secmap)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001569 return -ENOMEM;
1570 return 0;
1571}
1572
1573static int build_dirty_segmap(struct f2fs_sb_info *sbi)
1574{
1575 struct dirty_seglist_info *dirty_i;
1576 unsigned int bitmap_size, i;
1577
1578 /* allocate memory for dirty segments list information */
1579 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
1580 if (!dirty_i)
1581 return -ENOMEM;
1582
1583 SM_I(sbi)->dirty_info = dirty_i;
1584 mutex_init(&dirty_i->seglist_lock);
1585
1586 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1587
1588 for (i = 0; i < NR_DIRTY_TYPE; i++) {
1589 dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001590 if (!dirty_i->dirty_segmap[i])
1591 return -ENOMEM;
1592 }
1593
1594 init_dirty_segmap(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001595 return init_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001596}
1597
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001598/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001599 * Update min, max modified time for cost-benefit GC algorithm
1600 */
1601static void init_min_max_mtime(struct f2fs_sb_info *sbi)
1602{
1603 struct sit_info *sit_i = SIT_I(sbi);
1604 unsigned int segno;
1605
1606 mutex_lock(&sit_i->sentry_lock);
1607
1608 sit_i->min_mtime = LLONG_MAX;
1609
1610 for (segno = 0; segno < TOTAL_SEGS(sbi); segno += sbi->segs_per_sec) {
1611 unsigned int i;
1612 unsigned long long mtime = 0;
1613
1614 for (i = 0; i < sbi->segs_per_sec; i++)
1615 mtime += get_seg_entry(sbi, segno + i)->mtime;
1616
1617 mtime = div_u64(mtime, sbi->segs_per_sec);
1618
1619 if (sit_i->min_mtime > mtime)
1620 sit_i->min_mtime = mtime;
1621 }
1622 sit_i->max_mtime = get_mtime(sbi);
1623 mutex_unlock(&sit_i->sentry_lock);
1624}
1625
1626int build_segment_manager(struct f2fs_sb_info *sbi)
1627{
1628 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1629 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Namjae Jeon1042d602012-12-01 10:56:13 +09001630 struct f2fs_sm_info *sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001631 int err;
1632
1633 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
1634 if (!sm_info)
1635 return -ENOMEM;
1636
1637 /* init sm info */
1638 sbi->sm_info = sm_info;
1639 INIT_LIST_HEAD(&sm_info->wblist_head);
1640 spin_lock_init(&sm_info->wblist_lock);
1641 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
1642 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
1643 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
1644 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
1645 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
1646 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
1647 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
1648
1649 err = build_sit_info(sbi);
1650 if (err)
1651 return err;
1652 err = build_free_segmap(sbi);
1653 if (err)
1654 return err;
1655 err = build_curseg(sbi);
1656 if (err)
1657 return err;
1658
1659 /* reinit free segmap based on SIT */
1660 build_sit_entries(sbi);
1661
1662 init_free_segmap(sbi);
1663 err = build_dirty_segmap(sbi);
1664 if (err)
1665 return err;
1666
1667 init_min_max_mtime(sbi);
1668 return 0;
1669}
1670
1671static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
1672 enum dirty_type dirty_type)
1673{
1674 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1675
1676 mutex_lock(&dirty_i->seglist_lock);
1677 kfree(dirty_i->dirty_segmap[dirty_type]);
1678 dirty_i->nr_dirty[dirty_type] = 0;
1679 mutex_unlock(&dirty_i->seglist_lock);
1680}
1681
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001682static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001683{
1684 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001685 kfree(dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001686}
1687
1688static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
1689{
1690 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1691 int i;
1692
1693 if (!dirty_i)
1694 return;
1695
1696 /* discard pre-free/dirty segments list */
1697 for (i = 0; i < NR_DIRTY_TYPE; i++)
1698 discard_dirty_segmap(sbi, i);
1699
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001700 destroy_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001701 SM_I(sbi)->dirty_info = NULL;
1702 kfree(dirty_i);
1703}
1704
1705static void destroy_curseg(struct f2fs_sb_info *sbi)
1706{
1707 struct curseg_info *array = SM_I(sbi)->curseg_array;
1708 int i;
1709
1710 if (!array)
1711 return;
1712 SM_I(sbi)->curseg_array = NULL;
1713 for (i = 0; i < NR_CURSEG_TYPE; i++)
1714 kfree(array[i].sum_blk);
1715 kfree(array);
1716}
1717
1718static void destroy_free_segmap(struct f2fs_sb_info *sbi)
1719{
1720 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
1721 if (!free_i)
1722 return;
1723 SM_I(sbi)->free_info = NULL;
1724 kfree(free_i->free_segmap);
1725 kfree(free_i->free_secmap);
1726 kfree(free_i);
1727}
1728
1729static void destroy_sit_info(struct f2fs_sb_info *sbi)
1730{
1731 struct sit_info *sit_i = SIT_I(sbi);
1732 unsigned int start;
1733
1734 if (!sit_i)
1735 return;
1736
1737 if (sit_i->sentries) {
1738 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1739 kfree(sit_i->sentries[start].cur_valid_map);
1740 kfree(sit_i->sentries[start].ckpt_valid_map);
1741 }
1742 }
1743 vfree(sit_i->sentries);
1744 vfree(sit_i->sec_entries);
1745 kfree(sit_i->dirty_sentries_bitmap);
1746
1747 SM_I(sbi)->sit_info = NULL;
1748 kfree(sit_i->sit_bitmap);
1749 kfree(sit_i);
1750}
1751
1752void destroy_segment_manager(struct f2fs_sb_info *sbi)
1753{
1754 struct f2fs_sm_info *sm_info = SM_I(sbi);
1755 destroy_dirty_segmap(sbi);
1756 destroy_curseg(sbi);
1757 destroy_free_segmap(sbi);
1758 destroy_sit_info(sbi);
1759 sbi->sm_info = NULL;
1760 kfree(sm_info);
1761}