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