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