blob: aa1d30d76719ca11a79ad2f56a07e7d71cea45ef [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
Changman Lee9a7f1432013-11-15 10:42:51 +090023#define __reverse_ffz(x) __reverse_ffs(~(x))
24
25/*
26 * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since
27 * MSB and LSB are reversed in a byte by f2fs_set_bit.
28 */
29static inline unsigned long __reverse_ffs(unsigned long word)
30{
31 int num = 0;
32
33#if BITS_PER_LONG == 64
34 if ((word & 0xffffffff) == 0) {
35 num += 32;
36 word >>= 32;
37 }
38#endif
39 if ((word & 0xffff) == 0) {
40 num += 16;
41 word >>= 16;
42 }
43 if ((word & 0xff) == 0) {
44 num += 8;
45 word >>= 8;
46 }
47 if ((word & 0xf0) == 0)
48 num += 4;
49 else
50 word >>= 4;
51 if ((word & 0xc) == 0)
52 num += 2;
53 else
54 word >>= 2;
55 if ((word & 0x2) == 0)
56 num += 1;
57 return num;
58}
59
60/*
61 * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c becasue
62 * f2fs_set_bit makes MSB and LSB reversed in a byte.
63 * Example:
64 * LSB <--> MSB
65 * f2fs_set_bit(0, bitmap) => 0000 0001
66 * f2fs_set_bit(7, bitmap) => 1000 0000
67 */
68static unsigned long __find_rev_next_bit(const unsigned long *addr,
69 unsigned long size, unsigned long offset)
70{
71 const unsigned long *p = addr + BIT_WORD(offset);
72 unsigned long result = offset & ~(BITS_PER_LONG - 1);
73 unsigned long tmp;
74 unsigned long mask, submask;
75 unsigned long quot, rest;
76
77 if (offset >= size)
78 return size;
79
80 size -= result;
81 offset %= BITS_PER_LONG;
82 if (!offset)
83 goto aligned;
84
85 tmp = *(p++);
86 quot = (offset >> 3) << 3;
87 rest = offset & 0x7;
88 mask = ~0UL << quot;
89 submask = (unsigned char)(0xff << rest) >> rest;
90 submask <<= quot;
91 mask &= submask;
92 tmp &= mask;
93 if (size < BITS_PER_LONG)
94 goto found_first;
95 if (tmp)
96 goto found_middle;
97
98 size -= BITS_PER_LONG;
99 result += BITS_PER_LONG;
100aligned:
101 while (size & ~(BITS_PER_LONG-1)) {
102 tmp = *(p++);
103 if (tmp)
104 goto found_middle;
105 result += BITS_PER_LONG;
106 size -= BITS_PER_LONG;
107 }
108 if (!size)
109 return result;
110 tmp = *p;
111found_first:
112 tmp &= (~0UL >> (BITS_PER_LONG - size));
113 if (tmp == 0UL) /* Are any bits set? */
114 return result + size; /* Nope. */
115found_middle:
116 return result + __reverse_ffs(tmp);
117}
118
119static unsigned long __find_rev_next_zero_bit(const unsigned long *addr,
120 unsigned long size, unsigned long offset)
121{
122 const unsigned long *p = addr + BIT_WORD(offset);
123 unsigned long result = offset & ~(BITS_PER_LONG - 1);
124 unsigned long tmp;
125 unsigned long mask, submask;
126 unsigned long quot, rest;
127
128 if (offset >= size)
129 return size;
130
131 size -= result;
132 offset %= BITS_PER_LONG;
133 if (!offset)
134 goto aligned;
135
136 tmp = *(p++);
137 quot = (offset >> 3) << 3;
138 rest = offset & 0x7;
139 mask = ~(~0UL << quot);
140 submask = (unsigned char)~((unsigned char)(0xff << rest) >> rest);
141 submask <<= quot;
142 mask += submask;
143 tmp |= mask;
144 if (size < BITS_PER_LONG)
145 goto found_first;
146 if (~tmp)
147 goto found_middle;
148
149 size -= BITS_PER_LONG;
150 result += BITS_PER_LONG;
151aligned:
152 while (size & ~(BITS_PER_LONG - 1)) {
153 tmp = *(p++);
154 if (~tmp)
155 goto found_middle;
156 result += BITS_PER_LONG;
157 size -= BITS_PER_LONG;
158 }
159 if (!size)
160 return result;
161 tmp = *p;
162
163found_first:
164 tmp |= ~0UL << size;
165 if (tmp == ~0UL) /* Are any bits zero? */
166 return result + size; /* Nope. */
167found_middle:
168 return result + __reverse_ffz(tmp);
169}
170
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900171/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900172 * This function balances dirty node and dentry pages.
173 * In addition, it controls garbage collection.
174 */
175void f2fs_balance_fs(struct f2fs_sb_info *sbi)
176{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900177 /*
Jaegeuk Kim029cd282012-12-21 17:20:21 +0900178 * We should do GC or end up with checkpoint, if there are so many dirty
179 * dir/node pages without enough free segments.
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900180 */
Jaegeuk Kim43727522013-02-04 15:11:17 +0900181 if (has_not_enough_free_secs(sbi, 0)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900182 mutex_lock(&sbi->gc_mutex);
Jaegeuk Kim408e9372013-01-03 17:55:52 +0900183 f2fs_gc(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900184 }
185}
186
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900187void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi)
188{
189 /* check the # of cached NAT entries and prefree segments */
190 if (try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK) ||
191 excess_prefree_segs(sbi))
192 f2fs_sync_fs(sbi->sb, true);
193}
194
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900195static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
196 enum dirty_type dirty_type)
197{
198 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
199
200 /* need not be added */
201 if (IS_CURSEG(sbi, segno))
202 return;
203
204 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
205 dirty_i->nr_dirty[dirty_type]++;
206
207 if (dirty_type == DIRTY) {
208 struct seg_entry *sentry = get_seg_entry(sbi, segno);
Changman Lee4625d6a2013-10-25 17:31:57 +0900209 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900210
Changman Lee4625d6a2013-10-25 17:31:57 +0900211 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
212 dirty_i->nr_dirty[t]++;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900213 }
214}
215
216static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
217 enum dirty_type dirty_type)
218{
219 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
220
221 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
222 dirty_i->nr_dirty[dirty_type]--;
223
224 if (dirty_type == DIRTY) {
Changman Lee4625d6a2013-10-25 17:31:57 +0900225 struct seg_entry *sentry = get_seg_entry(sbi, segno);
226 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900227
Changman Lee4625d6a2013-10-25 17:31:57 +0900228 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
229 dirty_i->nr_dirty[t]--;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900230
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +0900231 if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0)
232 clear_bit(GET_SECNO(sbi, segno),
233 dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900234 }
235}
236
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900237/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900238 * Should not occur error such as -ENOMEM.
239 * Adding dirty entry into seglist is not critical operation.
240 * If a given segment is one of current working segments, it won't be added.
241 */
Haicheng Li8d8451a2013-06-13 16:59:28 +0800242static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900243{
244 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
245 unsigned short valid_blocks;
246
247 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
248 return;
249
250 mutex_lock(&dirty_i->seglist_lock);
251
252 valid_blocks = get_valid_blocks(sbi, segno, 0);
253
254 if (valid_blocks == 0) {
255 __locate_dirty_segment(sbi, segno, PRE);
256 __remove_dirty_segment(sbi, segno, DIRTY);
257 } else if (valid_blocks < sbi->blocks_per_seg) {
258 __locate_dirty_segment(sbi, segno, DIRTY);
259 } else {
260 /* Recovery routine with SSR needs this */
261 __remove_dirty_segment(sbi, segno, DIRTY);
262 }
263
264 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900265}
266
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900267/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900268 * Should call clear_prefree_segments after checkpoint is done.
269 */
270static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
271{
272 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Haicheng Li81fb5e82013-05-14 18:20:28 +0800273 unsigned int segno = -1;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900274 unsigned int total_segs = TOTAL_SEGS(sbi);
275
276 mutex_lock(&dirty_i->seglist_lock);
277 while (1) {
278 segno = find_next_bit(dirty_i->dirty_segmap[PRE], total_segs,
Haicheng Li81fb5e82013-05-14 18:20:28 +0800279 segno + 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900280 if (segno >= total_segs)
281 break;
282 __set_test_and_free(sbi, segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900283 }
284 mutex_unlock(&dirty_i->seglist_lock);
285}
286
287void clear_prefree_segments(struct f2fs_sb_info *sbi)
288{
289 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Changman Lee29e59c12013-11-11 09:24:37 +0900290 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900291 unsigned int total_segs = TOTAL_SEGS(sbi);
Changman Lee29e59c12013-11-11 09:24:37 +0900292 unsigned int start = 0, end = -1;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900293
294 mutex_lock(&dirty_i->seglist_lock);
Changman Lee29e59c12013-11-11 09:24:37 +0900295
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900296 while (1) {
Changman Lee29e59c12013-11-11 09:24:37 +0900297 int i;
298 start = find_next_bit(prefree_map, total_segs, end + 1);
299 if (start >= total_segs)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900300 break;
Changman Lee29e59c12013-11-11 09:24:37 +0900301 end = find_next_zero_bit(prefree_map, total_segs, start + 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900302
Changman Lee29e59c12013-11-11 09:24:37 +0900303 for (i = start; i < end; i++)
304 clear_bit(i, prefree_map);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900305
Changman Lee29e59c12013-11-11 09:24:37 +0900306 dirty_i->nr_dirty[PRE] -= end - start;
307
308 if (!test_opt(sbi, DISCARD))
309 continue;
310
311 blkdev_issue_discard(sbi->sb->s_bdev,
312 START_BLOCK(sbi, start) <<
313 sbi->log_sectors_per_block,
314 (1 << (sbi->log_sectors_per_block +
315 sbi->log_blocks_per_seg)) * (end - start),
316 GFP_NOFS, 0);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900317 }
318 mutex_unlock(&dirty_i->seglist_lock);
319}
320
321static void __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
322{
323 struct sit_info *sit_i = SIT_I(sbi);
324 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap))
325 sit_i->dirty_sentries++;
326}
327
328static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
329 unsigned int segno, int modified)
330{
331 struct seg_entry *se = get_seg_entry(sbi, segno);
332 se->type = type;
333 if (modified)
334 __mark_sit_entry_dirty(sbi, segno);
335}
336
337static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
338{
339 struct seg_entry *se;
340 unsigned int segno, offset;
341 long int new_vblocks;
342
343 segno = GET_SEGNO(sbi, blkaddr);
344
345 se = get_seg_entry(sbi, segno);
346 new_vblocks = se->valid_blocks + del;
347 offset = GET_SEGOFF_FROM_SEG0(sbi, blkaddr) & (sbi->blocks_per_seg - 1);
348
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900349 f2fs_bug_on((new_vblocks >> (sizeof(unsigned short) << 3) ||
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900350 (new_vblocks > sbi->blocks_per_seg)));
351
352 se->valid_blocks = new_vblocks;
353 se->mtime = get_mtime(sbi);
354 SIT_I(sbi)->max_mtime = se->mtime;
355
356 /* Update valid block bitmap */
357 if (del > 0) {
358 if (f2fs_set_bit(offset, se->cur_valid_map))
359 BUG();
360 } else {
361 if (!f2fs_clear_bit(offset, se->cur_valid_map))
362 BUG();
363 }
364 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
365 se->ckpt_valid_blocks += del;
366
367 __mark_sit_entry_dirty(sbi, segno);
368
369 /* update total number of valid blocks to be written in ckpt area */
370 SIT_I(sbi)->written_valid_blocks += del;
371
372 if (sbi->segs_per_sec > 1)
373 get_sec_entry(sbi, segno)->valid_blocks += del;
374}
375
376static void refresh_sit_entry(struct f2fs_sb_info *sbi,
377 block_t old_blkaddr, block_t new_blkaddr)
378{
379 update_sit_entry(sbi, new_blkaddr, 1);
380 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
381 update_sit_entry(sbi, old_blkaddr, -1);
382}
383
384void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
385{
386 unsigned int segno = GET_SEGNO(sbi, addr);
387 struct sit_info *sit_i = SIT_I(sbi);
388
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900389 f2fs_bug_on(addr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900390 if (addr == NEW_ADDR)
391 return;
392
393 /* add it into sit main buffer */
394 mutex_lock(&sit_i->sentry_lock);
395
396 update_sit_entry(sbi, addr, -1);
397
398 /* add it into dirty seglist */
399 locate_dirty_segment(sbi, segno);
400
401 mutex_unlock(&sit_i->sentry_lock);
402}
403
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900404/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900405 * This function should be resided under the curseg_mutex lock
406 */
407static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
Haicheng Lie79efe32013-06-13 16:59:27 +0800408 struct f2fs_summary *sum)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900409{
410 struct curseg_info *curseg = CURSEG_I(sbi, type);
411 void *addr = curseg->sum_blk;
Haicheng Lie79efe32013-06-13 16:59:27 +0800412 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900413 memcpy(addr, sum, sizeof(struct f2fs_summary));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900414}
415
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900416/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900417 * Calculate the number of current summary pages for writing
418 */
419int npages_for_summary_flush(struct f2fs_sb_info *sbi)
420{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900421 int valid_sum_count = 0;
Fan Li9a479382013-10-29 16:21:47 +0800422 int i, sum_in_page;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900423
424 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
425 if (sbi->ckpt->alloc_type[i] == SSR)
426 valid_sum_count += sbi->blocks_per_seg;
427 else
428 valid_sum_count += curseg_blkoff(sbi, i);
429 }
430
Fan Li9a479382013-10-29 16:21:47 +0800431 sum_in_page = (PAGE_CACHE_SIZE - 2 * SUM_JOURNAL_SIZE -
432 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
433 if (valid_sum_count <= sum_in_page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900434 return 1;
Fan Li9a479382013-10-29 16:21:47 +0800435 else if ((valid_sum_count - sum_in_page) <=
436 (PAGE_CACHE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900437 return 2;
438 return 3;
439}
440
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900441/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900442 * Caller should put this summary page
443 */
444struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
445{
446 return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
447}
448
449static void write_sum_page(struct f2fs_sb_info *sbi,
450 struct f2fs_summary_block *sum_blk, block_t blk_addr)
451{
452 struct page *page = grab_meta_page(sbi, blk_addr);
453 void *kaddr = page_address(page);
454 memcpy(kaddr, sum_blk, PAGE_CACHE_SIZE);
455 set_page_dirty(page);
456 f2fs_put_page(page, 1);
457}
458
Jaegeuk Kim60374682013-03-31 13:58:51 +0900459static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
460{
461 struct curseg_info *curseg = CURSEG_I(sbi, type);
Haicheng Li81fb5e82013-05-14 18:20:28 +0800462 unsigned int segno = curseg->segno + 1;
Jaegeuk Kim60374682013-03-31 13:58:51 +0900463 struct free_segmap_info *free_i = FREE_I(sbi);
464
Haicheng Li81fb5e82013-05-14 18:20:28 +0800465 if (segno < TOTAL_SEGS(sbi) && segno % sbi->segs_per_sec)
466 return !test_bit(segno, free_i->free_segmap);
Jaegeuk Kim60374682013-03-31 13:58:51 +0900467 return 0;
468}
469
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900470/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900471 * Find a new segment from the free segments bitmap to right order
472 * This function should be returned with success, otherwise BUG
473 */
474static void get_new_segment(struct f2fs_sb_info *sbi,
475 unsigned int *newseg, bool new_sec, int dir)
476{
477 struct free_segmap_info *free_i = FREE_I(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900478 unsigned int segno, secno, zoneno;
Jaegeuk Kim53cf9522013-03-31 12:39:49 +0900479 unsigned int total_zones = TOTAL_SECS(sbi) / sbi->secs_per_zone;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900480 unsigned int hint = *newseg / sbi->segs_per_sec;
481 unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
482 unsigned int left_start = hint;
483 bool init = true;
484 int go_left = 0;
485 int i;
486
487 write_lock(&free_i->segmap_lock);
488
489 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
490 segno = find_next_zero_bit(free_i->free_segmap,
491 TOTAL_SEGS(sbi), *newseg + 1);
Jaegeuk Kim33afa7f2013-03-31 12:59:53 +0900492 if (segno - *newseg < sbi->segs_per_sec -
493 (*newseg % sbi->segs_per_sec))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900494 goto got_it;
495 }
496find_other_zone:
Jaegeuk Kim53cf9522013-03-31 12:39:49 +0900497 secno = find_next_zero_bit(free_i->free_secmap, TOTAL_SECS(sbi), hint);
498 if (secno >= TOTAL_SECS(sbi)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900499 if (dir == ALLOC_RIGHT) {
500 secno = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim53cf9522013-03-31 12:39:49 +0900501 TOTAL_SECS(sbi), 0);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900502 f2fs_bug_on(secno >= TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900503 } else {
504 go_left = 1;
505 left_start = hint - 1;
506 }
507 }
508 if (go_left == 0)
509 goto skip_left;
510
511 while (test_bit(left_start, free_i->free_secmap)) {
512 if (left_start > 0) {
513 left_start--;
514 continue;
515 }
516 left_start = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim53cf9522013-03-31 12:39:49 +0900517 TOTAL_SECS(sbi), 0);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900518 f2fs_bug_on(left_start >= TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900519 break;
520 }
521 secno = left_start;
522skip_left:
523 hint = secno;
524 segno = secno * sbi->segs_per_sec;
525 zoneno = secno / sbi->secs_per_zone;
526
527 /* give up on finding another zone */
528 if (!init)
529 goto got_it;
530 if (sbi->secs_per_zone == 1)
531 goto got_it;
532 if (zoneno == old_zoneno)
533 goto got_it;
534 if (dir == ALLOC_LEFT) {
535 if (!go_left && zoneno + 1 >= total_zones)
536 goto got_it;
537 if (go_left && zoneno == 0)
538 goto got_it;
539 }
540 for (i = 0; i < NR_CURSEG_TYPE; i++)
541 if (CURSEG_I(sbi, i)->zone == zoneno)
542 break;
543
544 if (i < NR_CURSEG_TYPE) {
545 /* zone is in user, try another */
546 if (go_left)
547 hint = zoneno * sbi->secs_per_zone - 1;
548 else if (zoneno + 1 >= total_zones)
549 hint = 0;
550 else
551 hint = (zoneno + 1) * sbi->secs_per_zone;
552 init = false;
553 goto find_other_zone;
554 }
555got_it:
556 /* set it as dirty segment in free segmap */
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900557 f2fs_bug_on(test_bit(segno, free_i->free_segmap));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900558 __set_inuse(sbi, segno);
559 *newseg = segno;
560 write_unlock(&free_i->segmap_lock);
561}
562
563static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
564{
565 struct curseg_info *curseg = CURSEG_I(sbi, type);
566 struct summary_footer *sum_footer;
567
568 curseg->segno = curseg->next_segno;
569 curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
570 curseg->next_blkoff = 0;
571 curseg->next_segno = NULL_SEGNO;
572
573 sum_footer = &(curseg->sum_blk->footer);
574 memset(sum_footer, 0, sizeof(struct summary_footer));
575 if (IS_DATASEG(type))
576 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
577 if (IS_NODESEG(type))
578 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
579 __set_sit_entry_type(sbi, type, curseg->segno, modified);
580}
581
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900582/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900583 * Allocate a current working segment.
584 * This function always allocates a free segment in LFS manner.
585 */
586static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
587{
588 struct curseg_info *curseg = CURSEG_I(sbi, type);
589 unsigned int segno = curseg->segno;
590 int dir = ALLOC_LEFT;
591
592 write_sum_page(sbi, curseg->sum_blk,
Haicheng Li81fb5e82013-05-14 18:20:28 +0800593 GET_SUM_BLOCK(sbi, segno));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900594 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
595 dir = ALLOC_RIGHT;
596
597 if (test_opt(sbi, NOHEAP))
598 dir = ALLOC_RIGHT;
599
600 get_new_segment(sbi, &segno, new_sec, dir);
601 curseg->next_segno = segno;
602 reset_curseg(sbi, type, 1);
603 curseg->alloc_type = LFS;
604}
605
606static void __next_free_blkoff(struct f2fs_sb_info *sbi,
607 struct curseg_info *seg, block_t start)
608{
609 struct seg_entry *se = get_seg_entry(sbi, seg->segno);
610 block_t ofs;
611 for (ofs = start; ofs < sbi->blocks_per_seg; ofs++) {
612 if (!f2fs_test_bit(ofs, se->ckpt_valid_map)
613 && !f2fs_test_bit(ofs, se->cur_valid_map))
614 break;
615 }
616 seg->next_blkoff = ofs;
617}
618
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900619/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900620 * If a segment is written by LFS manner, next block offset is just obtained
621 * by increasing the current block offset. However, if a segment is written by
622 * SSR manner, next block offset obtained by calling __next_free_blkoff
623 */
624static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
625 struct curseg_info *seg)
626{
627 if (seg->alloc_type == SSR)
628 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
629 else
630 seg->next_blkoff++;
631}
632
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900633/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900634 * This function always allocates a used segment (from dirty seglist) by SSR
635 * manner, so it should recover the existing segment information of valid blocks
636 */
637static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
638{
639 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
640 struct curseg_info *curseg = CURSEG_I(sbi, type);
641 unsigned int new_segno = curseg->next_segno;
642 struct f2fs_summary_block *sum_node;
643 struct page *sum_page;
644
645 write_sum_page(sbi, curseg->sum_blk,
646 GET_SUM_BLOCK(sbi, curseg->segno));
647 __set_test_and_inuse(sbi, new_segno);
648
649 mutex_lock(&dirty_i->seglist_lock);
650 __remove_dirty_segment(sbi, new_segno, PRE);
651 __remove_dirty_segment(sbi, new_segno, DIRTY);
652 mutex_unlock(&dirty_i->seglist_lock);
653
654 reset_curseg(sbi, type, 1);
655 curseg->alloc_type = SSR;
656 __next_free_blkoff(sbi, curseg, 0);
657
658 if (reuse) {
659 sum_page = get_sum_page(sbi, new_segno);
660 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
661 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
662 f2fs_put_page(sum_page, 1);
663 }
664}
665
Jaegeuk Kim43727522013-02-04 15:11:17 +0900666static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
667{
668 struct curseg_info *curseg = CURSEG_I(sbi, type);
669 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
670
671 if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0))
672 return v_ops->get_victim(sbi,
673 &(curseg)->next_segno, BG_GC, type, SSR);
674
675 /* For data segments, let's do SSR more intensively */
676 for (; type >= CURSEG_HOT_DATA; type--)
677 if (v_ops->get_victim(sbi, &(curseg)->next_segno,
678 BG_GC, type, SSR))
679 return 1;
680 return 0;
681}
682
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900683/*
684 * flush out current segment and replace it with new segment
685 * This function should be returned with success, otherwise BUG
686 */
687static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
688 int type, bool force)
689{
690 struct curseg_info *curseg = CURSEG_I(sbi, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900691
Gu Zheng7b405272013-08-19 09:41:15 +0800692 if (force)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900693 new_curseg(sbi, type, true);
Gu Zheng7b405272013-08-19 09:41:15 +0800694 else if (type == CURSEG_WARM_NODE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900695 new_curseg(sbi, type, false);
Jaegeuk Kim60374682013-03-31 13:58:51 +0900696 else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
697 new_curseg(sbi, type, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900698 else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
699 change_curseg(sbi, type, true);
700 else
701 new_curseg(sbi, type, false);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +0900702
703 stat_inc_seg_type(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900704}
705
706void allocate_new_segments(struct f2fs_sb_info *sbi)
707{
708 struct curseg_info *curseg;
709 unsigned int old_curseg;
710 int i;
711
712 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
713 curseg = CURSEG_I(sbi, i);
714 old_curseg = curseg->segno;
715 SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
716 locate_dirty_segment(sbi, old_curseg);
717 }
718}
719
720static const struct segment_allocation default_salloc_ops = {
721 .allocate_segment = allocate_segment_by_default,
722};
723
724static void f2fs_end_io_write(struct bio *bio, int err)
725{
726 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
727 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
728 struct bio_private *p = bio->bi_private;
729
730 do {
731 struct page *page = bvec->bv_page;
732
733 if (--bvec >= bio->bi_io_vec)
734 prefetchw(&bvec->bv_page->flags);
735 if (!uptodate) {
736 SetPageError(page);
737 if (page->mapping)
738 set_bit(AS_EIO, &page->mapping->flags);
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900739 set_ckpt_flags(p->sbi->ckpt, CP_ERROR_FLAG);
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900740 p->sbi->sb->s_flags |= MS_RDONLY;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900741 }
742 end_page_writeback(page);
743 dec_page_count(p->sbi, F2FS_WRITEBACK);
744 } while (bvec >= bio->bi_io_vec);
745
746 if (p->is_sync)
747 complete(p->wait);
Gu Zhenge2340882013-10-14 18:45:56 +0800748
Changman Leefb51b5e2013-11-07 12:48:25 +0900749 if (!get_pages(p->sbi, F2FS_WRITEBACK) &&
750 !list_empty(&p->sbi->cp_wait.task_list))
751 wake_up(&p->sbi->cp_wait);
Gu Zhenge2340882013-10-14 18:45:56 +0800752
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900753 kfree(p);
754 bio_put(bio);
755}
756
Jaegeuk Kim3cd8a232012-12-10 09:26:05 +0900757struct bio *f2fs_bio_alloc(struct block_device *bdev, int npages)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900758{
759 struct bio *bio;
Jaegeuk Kim3cd8a232012-12-10 09:26:05 +0900760
761 /* No failure on bio allocation */
762 bio = bio_alloc(GFP_NOIO, npages);
763 bio->bi_bdev = bdev;
Gu Zhengd8207f62013-07-25 11:30:01 +0800764 bio->bi_private = NULL;
765
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900766 return bio;
767}
768
769static void do_submit_bio(struct f2fs_sb_info *sbi,
770 enum page_type type, bool sync)
771{
772 int rw = sync ? WRITE_SYNC : WRITE;
773 enum page_type btype = type > META ? META : type;
774
775 if (type >= META_FLUSH)
776 rw = WRITE_FLUSH_FUA;
777
Namjae Jeon86804412013-04-25 11:45:21 +0900778 if (btype == META)
779 rw |= REQ_META;
780
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900781 if (sbi->bio[btype]) {
782 struct bio_private *p = sbi->bio[btype]->bi_private;
783 p->sbi = sbi;
784 sbi->bio[btype]->bi_end_io = f2fs_end_io_write;
Namjae Jeon6ec178d2013-04-23 17:51:43 +0900785
786 trace_f2fs_do_submit_bio(sbi->sb, btype, sync, sbi->bio[btype]);
787
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900788 if (type == META_FLUSH) {
789 DECLARE_COMPLETION_ONSTACK(wait);
790 p->is_sync = true;
791 p->wait = &wait;
792 submit_bio(rw, sbi->bio[btype]);
793 wait_for_completion(&wait);
794 } else {
795 p->is_sync = false;
796 submit_bio(rw, sbi->bio[btype]);
797 }
798 sbi->bio[btype] = NULL;
799 }
800}
801
802void f2fs_submit_bio(struct f2fs_sb_info *sbi, enum page_type type, bool sync)
803{
804 down_write(&sbi->bio_sem);
805 do_submit_bio(sbi, type, sync);
806 up_write(&sbi->bio_sem);
807}
808
809static void submit_write_page(struct f2fs_sb_info *sbi, struct page *page,
810 block_t blk_addr, enum page_type type)
811{
812 struct block_device *bdev = sbi->sb->s_bdev;
Chao Yucc7b1bb2013-09-22 15:50:50 +0800813 int bio_blocks;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900814
815 verify_block_addr(sbi, blk_addr);
816
817 down_write(&sbi->bio_sem);
818
819 inc_page_count(sbi, F2FS_WRITEBACK);
820
821 if (sbi->bio[type] && sbi->last_block_in_bio[type] != blk_addr - 1)
822 do_submit_bio(sbi, type, false);
823alloc_new:
Jaegeuk Kim3cd8a232012-12-10 09:26:05 +0900824 if (sbi->bio[type] == NULL) {
Gu Zhengd8207f62013-07-25 11:30:01 +0800825 struct bio_private *priv;
826retry:
827 priv = kmalloc(sizeof(struct bio_private), GFP_NOFS);
828 if (!priv) {
829 cond_resched();
830 goto retry;
831 }
832
Chao Yucc7b1bb2013-09-22 15:50:50 +0800833 bio_blocks = MAX_BIO_BLOCKS(max_hw_blocks(sbi));
834 sbi->bio[type] = f2fs_bio_alloc(bdev, bio_blocks);
Jaegeuk Kim3cd8a232012-12-10 09:26:05 +0900835 sbi->bio[type]->bi_sector = SECTOR_FROM_BLOCK(sbi, blk_addr);
Gu Zhengd8207f62013-07-25 11:30:01 +0800836 sbi->bio[type]->bi_private = priv;
Jaegeuk Kim3cd8a232012-12-10 09:26:05 +0900837 /*
838 * The end_io will be assigned at the sumbission phase.
839 * Until then, let bio_add_page() merge consecutive IOs as much
840 * as possible.
841 */
842 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900843
844 if (bio_add_page(sbi->bio[type], page, PAGE_CACHE_SIZE, 0) <
845 PAGE_CACHE_SIZE) {
846 do_submit_bio(sbi, type, false);
847 goto alloc_new;
848 }
849
850 sbi->last_block_in_bio[type] = blk_addr;
851
852 up_write(&sbi->bio_sem);
Namjae Jeon6ec178d2013-04-23 17:51:43 +0900853 trace_f2fs_submit_write_page(page, blk_addr, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900854}
855
Jin Xua5694692013-08-05 20:02:04 +0800856void f2fs_wait_on_page_writeback(struct page *page,
857 enum page_type type, bool sync)
858{
859 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
860 if (PageWriteback(page)) {
861 f2fs_submit_bio(sbi, type, sync);
862 wait_on_page_writeback(page);
863 }
864}
865
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900866static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
867{
868 struct curseg_info *curseg = CURSEG_I(sbi, type);
869 if (curseg->next_blkoff < sbi->blocks_per_seg)
870 return true;
871 return false;
872}
873
874static int __get_segment_type_2(struct page *page, enum page_type p_type)
875{
876 if (p_type == DATA)
877 return CURSEG_HOT_DATA;
878 else
879 return CURSEG_HOT_NODE;
880}
881
882static int __get_segment_type_4(struct page *page, enum page_type p_type)
883{
884 if (p_type == DATA) {
885 struct inode *inode = page->mapping->host;
886
887 if (S_ISDIR(inode->i_mode))
888 return CURSEG_HOT_DATA;
889 else
890 return CURSEG_COLD_DATA;
891 } else {
892 if (IS_DNODE(page) && !is_cold_node(page))
893 return CURSEG_HOT_NODE;
894 else
895 return CURSEG_COLD_NODE;
896 }
897}
898
899static int __get_segment_type_6(struct page *page, enum page_type p_type)
900{
901 if (p_type == DATA) {
902 struct inode *inode = page->mapping->host;
903
904 if (S_ISDIR(inode->i_mode))
905 return CURSEG_HOT_DATA;
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900906 else if (is_cold_data(page) || file_is_cold(inode))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900907 return CURSEG_COLD_DATA;
908 else
909 return CURSEG_WARM_DATA;
910 } else {
911 if (IS_DNODE(page))
912 return is_cold_node(page) ? CURSEG_WARM_NODE :
913 CURSEG_HOT_NODE;
914 else
915 return CURSEG_COLD_NODE;
916 }
917}
918
919static int __get_segment_type(struct page *page, enum page_type p_type)
920{
921 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
922 switch (sbi->active_logs) {
923 case 2:
924 return __get_segment_type_2(page, p_type);
925 case 4:
926 return __get_segment_type_4(page, p_type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900927 }
Jaegeuk Kim12a67142012-12-21 11:47:05 +0900928 /* NR_CURSEG_TYPE(6) logs by default */
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900929 f2fs_bug_on(sbi->active_logs != NR_CURSEG_TYPE);
Jaegeuk Kim12a67142012-12-21 11:47:05 +0900930 return __get_segment_type_6(page, p_type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900931}
932
933static void do_write_page(struct f2fs_sb_info *sbi, struct page *page,
934 block_t old_blkaddr, block_t *new_blkaddr,
935 struct f2fs_summary *sum, enum page_type p_type)
936{
937 struct sit_info *sit_i = SIT_I(sbi);
938 struct curseg_info *curseg;
939 unsigned int old_cursegno;
940 int type;
941
942 type = __get_segment_type(page, p_type);
943 curseg = CURSEG_I(sbi, type);
944
945 mutex_lock(&curseg->curseg_mutex);
946
947 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
948 old_cursegno = curseg->segno;
949
950 /*
951 * __add_sum_entry should be resided under the curseg_mutex
952 * because, this function updates a summary entry in the
953 * current summary block.
954 */
Haicheng Lie79efe32013-06-13 16:59:27 +0800955 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900956
957 mutex_lock(&sit_i->sentry_lock);
958 __refresh_next_blkoff(sbi, curseg);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +0900959
960 stat_inc_block_count(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900961
962 /*
963 * SIT information should be updated before segment allocation,
964 * since SSR needs latest valid block information.
965 */
966 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
967
968 if (!__has_curseg_space(sbi, type))
969 sit_i->s_ops->allocate_segment(sbi, type, false);
970
971 locate_dirty_segment(sbi, old_cursegno);
972 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
973 mutex_unlock(&sit_i->sentry_lock);
974
975 if (p_type == NODE)
976 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
977
978 /* writeout dirty page into bdev */
979 submit_write_page(sbi, page, *new_blkaddr, p_type);
980
981 mutex_unlock(&curseg->curseg_mutex);
982}
983
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900984void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900985{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900986 set_page_writeback(page);
987 submit_write_page(sbi, page, page->index, META);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900988}
989
990void write_node_page(struct f2fs_sb_info *sbi, struct page *page,
991 unsigned int nid, block_t old_blkaddr, block_t *new_blkaddr)
992{
993 struct f2fs_summary sum;
994 set_summary(&sum, nid, 0, 0);
995 do_write_page(sbi, page, old_blkaddr, new_blkaddr, &sum, NODE);
996}
997
998void write_data_page(struct inode *inode, struct page *page,
999 struct dnode_of_data *dn, block_t old_blkaddr,
1000 block_t *new_blkaddr)
1001{
1002 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1003 struct f2fs_summary sum;
1004 struct node_info ni;
1005
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001006 f2fs_bug_on(old_blkaddr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001007 get_node_info(sbi, dn->nid, &ni);
1008 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1009
1010 do_write_page(sbi, page, old_blkaddr,
1011 new_blkaddr, &sum, DATA);
1012}
1013
1014void rewrite_data_page(struct f2fs_sb_info *sbi, struct page *page,
1015 block_t old_blk_addr)
1016{
1017 submit_write_page(sbi, page, old_blk_addr, DATA);
1018}
1019
1020void recover_data_page(struct f2fs_sb_info *sbi,
1021 struct page *page, struct f2fs_summary *sum,
1022 block_t old_blkaddr, block_t new_blkaddr)
1023{
1024 struct sit_info *sit_i = SIT_I(sbi);
1025 struct curseg_info *curseg;
1026 unsigned int segno, old_cursegno;
1027 struct seg_entry *se;
1028 int type;
1029
1030 segno = GET_SEGNO(sbi, new_blkaddr);
1031 se = get_seg_entry(sbi, segno);
1032 type = se->type;
1033
1034 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
1035 if (old_blkaddr == NULL_ADDR)
1036 type = CURSEG_COLD_DATA;
1037 else
1038 type = CURSEG_WARM_DATA;
1039 }
1040 curseg = CURSEG_I(sbi, type);
1041
1042 mutex_lock(&curseg->curseg_mutex);
1043 mutex_lock(&sit_i->sentry_lock);
1044
1045 old_cursegno = curseg->segno;
1046
1047 /* change the current segment */
1048 if (segno != curseg->segno) {
1049 curseg->next_segno = segno;
1050 change_curseg(sbi, type, true);
1051 }
1052
1053 curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, new_blkaddr) &
1054 (sbi->blocks_per_seg - 1);
Haicheng Lie79efe32013-06-13 16:59:27 +08001055 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001056
1057 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
1058
1059 locate_dirty_segment(sbi, old_cursegno);
1060 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
1061
1062 mutex_unlock(&sit_i->sentry_lock);
1063 mutex_unlock(&curseg->curseg_mutex);
1064}
1065
1066void rewrite_node_page(struct f2fs_sb_info *sbi,
1067 struct page *page, struct f2fs_summary *sum,
1068 block_t old_blkaddr, block_t new_blkaddr)
1069{
1070 struct sit_info *sit_i = SIT_I(sbi);
1071 int type = CURSEG_WARM_NODE;
1072 struct curseg_info *curseg;
1073 unsigned int segno, old_cursegno;
1074 block_t next_blkaddr = next_blkaddr_of_node(page);
1075 unsigned int next_segno = GET_SEGNO(sbi, next_blkaddr);
1076
1077 curseg = CURSEG_I(sbi, type);
1078
1079 mutex_lock(&curseg->curseg_mutex);
1080 mutex_lock(&sit_i->sentry_lock);
1081
1082 segno = GET_SEGNO(sbi, new_blkaddr);
1083 old_cursegno = curseg->segno;
1084
1085 /* change the current segment */
1086 if (segno != curseg->segno) {
1087 curseg->next_segno = segno;
1088 change_curseg(sbi, type, true);
1089 }
1090 curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, new_blkaddr) &
1091 (sbi->blocks_per_seg - 1);
Haicheng Lie79efe32013-06-13 16:59:27 +08001092 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001093
1094 /* change the current log to the next block addr in advance */
1095 if (next_segno != segno) {
1096 curseg->next_segno = next_segno;
1097 change_curseg(sbi, type, true);
1098 }
1099 curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, next_blkaddr) &
1100 (sbi->blocks_per_seg - 1);
1101
1102 /* rewrite node page */
1103 set_page_writeback(page);
1104 submit_write_page(sbi, page, new_blkaddr, NODE);
1105 f2fs_submit_bio(sbi, NODE, true);
1106 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
1107
1108 locate_dirty_segment(sbi, old_cursegno);
1109 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
1110
1111 mutex_unlock(&sit_i->sentry_lock);
1112 mutex_unlock(&curseg->curseg_mutex);
1113}
1114
1115static int read_compacted_summaries(struct f2fs_sb_info *sbi)
1116{
1117 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1118 struct curseg_info *seg_i;
1119 unsigned char *kaddr;
1120 struct page *page;
1121 block_t start;
1122 int i, j, offset;
1123
1124 start = start_sum_block(sbi);
1125
1126 page = get_meta_page(sbi, start++);
1127 kaddr = (unsigned char *)page_address(page);
1128
1129 /* Step 1: restore nat cache */
1130 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1131 memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
1132
1133 /* Step 2: restore sit cache */
1134 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1135 memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
1136 SUM_JOURNAL_SIZE);
1137 offset = 2 * SUM_JOURNAL_SIZE;
1138
1139 /* Step 3: restore summary entries */
1140 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1141 unsigned short blk_off;
1142 unsigned int segno;
1143
1144 seg_i = CURSEG_I(sbi, i);
1145 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
1146 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
1147 seg_i->next_segno = segno;
1148 reset_curseg(sbi, i, 0);
1149 seg_i->alloc_type = ckpt->alloc_type[i];
1150 seg_i->next_blkoff = blk_off;
1151
1152 if (seg_i->alloc_type == SSR)
1153 blk_off = sbi->blocks_per_seg;
1154
1155 for (j = 0; j < blk_off; j++) {
1156 struct f2fs_summary *s;
1157 s = (struct f2fs_summary *)(kaddr + offset);
1158 seg_i->sum_blk->entries[j] = *s;
1159 offset += SUMMARY_SIZE;
1160 if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1161 SUM_FOOTER_SIZE)
1162 continue;
1163
1164 f2fs_put_page(page, 1);
1165 page = NULL;
1166
1167 page = get_meta_page(sbi, start++);
1168 kaddr = (unsigned char *)page_address(page);
1169 offset = 0;
1170 }
1171 }
1172 f2fs_put_page(page, 1);
1173 return 0;
1174}
1175
1176static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
1177{
1178 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1179 struct f2fs_summary_block *sum;
1180 struct curseg_info *curseg;
1181 struct page *new;
1182 unsigned short blk_off;
1183 unsigned int segno = 0;
1184 block_t blk_addr = 0;
1185
1186 /* get segment number and block addr */
1187 if (IS_DATASEG(type)) {
1188 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
1189 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
1190 CURSEG_HOT_DATA]);
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001191 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001192 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
1193 else
1194 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
1195 } else {
1196 segno = le32_to_cpu(ckpt->cur_node_segno[type -
1197 CURSEG_HOT_NODE]);
1198 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
1199 CURSEG_HOT_NODE]);
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001200 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001201 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
1202 type - CURSEG_HOT_NODE);
1203 else
1204 blk_addr = GET_SUM_BLOCK(sbi, segno);
1205 }
1206
1207 new = get_meta_page(sbi, blk_addr);
1208 sum = (struct f2fs_summary_block *)page_address(new);
1209
1210 if (IS_NODESEG(type)) {
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001211 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001212 struct f2fs_summary *ns = &sum->entries[0];
1213 int i;
1214 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
1215 ns->version = 0;
1216 ns->ofs_in_node = 0;
1217 }
1218 } else {
1219 if (restore_node_summary(sbi, segno, sum)) {
1220 f2fs_put_page(new, 1);
1221 return -EINVAL;
1222 }
1223 }
1224 }
1225
1226 /* set uncompleted segment to curseg */
1227 curseg = CURSEG_I(sbi, type);
1228 mutex_lock(&curseg->curseg_mutex);
1229 memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
1230 curseg->next_segno = segno;
1231 reset_curseg(sbi, type, 0);
1232 curseg->alloc_type = ckpt->alloc_type[type];
1233 curseg->next_blkoff = blk_off;
1234 mutex_unlock(&curseg->curseg_mutex);
1235 f2fs_put_page(new, 1);
1236 return 0;
1237}
1238
1239static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
1240{
1241 int type = CURSEG_HOT_DATA;
1242
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001243 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001244 /* restore for compacted data summary */
1245 if (read_compacted_summaries(sbi))
1246 return -EINVAL;
1247 type = CURSEG_HOT_NODE;
1248 }
1249
1250 for (; type <= CURSEG_COLD_NODE; type++)
1251 if (read_normal_summaries(sbi, type))
1252 return -EINVAL;
1253 return 0;
1254}
1255
1256static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
1257{
1258 struct page *page;
1259 unsigned char *kaddr;
1260 struct f2fs_summary *summary;
1261 struct curseg_info *seg_i;
1262 int written_size = 0;
1263 int i, j;
1264
1265 page = grab_meta_page(sbi, blkaddr++);
1266 kaddr = (unsigned char *)page_address(page);
1267
1268 /* Step 1: write nat cache */
1269 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1270 memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
1271 written_size += SUM_JOURNAL_SIZE;
1272
1273 /* Step 2: write sit cache */
1274 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1275 memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
1276 SUM_JOURNAL_SIZE);
1277 written_size += SUM_JOURNAL_SIZE;
1278
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001279 /* Step 3: write summary entries */
1280 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1281 unsigned short blkoff;
1282 seg_i = CURSEG_I(sbi, i);
1283 if (sbi->ckpt->alloc_type[i] == SSR)
1284 blkoff = sbi->blocks_per_seg;
1285 else
1286 blkoff = curseg_blkoff(sbi, i);
1287
1288 for (j = 0; j < blkoff; j++) {
1289 if (!page) {
1290 page = grab_meta_page(sbi, blkaddr++);
1291 kaddr = (unsigned char *)page_address(page);
1292 written_size = 0;
1293 }
1294 summary = (struct f2fs_summary *)(kaddr + written_size);
1295 *summary = seg_i->sum_blk->entries[j];
1296 written_size += SUMMARY_SIZE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001297
1298 if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1299 SUM_FOOTER_SIZE)
1300 continue;
1301
Chao Yue8d61a72013-10-24 15:08:28 +08001302 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001303 f2fs_put_page(page, 1);
1304 page = NULL;
1305 }
1306 }
Chao Yue8d61a72013-10-24 15:08:28 +08001307 if (page) {
1308 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001309 f2fs_put_page(page, 1);
Chao Yue8d61a72013-10-24 15:08:28 +08001310 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001311}
1312
1313static void write_normal_summaries(struct f2fs_sb_info *sbi,
1314 block_t blkaddr, int type)
1315{
1316 int i, end;
1317 if (IS_DATASEG(type))
1318 end = type + NR_CURSEG_DATA_TYPE;
1319 else
1320 end = type + NR_CURSEG_NODE_TYPE;
1321
1322 for (i = type; i < end; i++) {
1323 struct curseg_info *sum = CURSEG_I(sbi, i);
1324 mutex_lock(&sum->curseg_mutex);
1325 write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
1326 mutex_unlock(&sum->curseg_mutex);
1327 }
1328}
1329
1330void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1331{
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001332 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001333 write_compacted_summaries(sbi, start_blk);
1334 else
1335 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
1336}
1337
1338void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1339{
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001340 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001341 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001342}
1343
1344int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
1345 unsigned int val, int alloc)
1346{
1347 int i;
1348
1349 if (type == NAT_JOURNAL) {
1350 for (i = 0; i < nats_in_cursum(sum); i++) {
1351 if (le32_to_cpu(nid_in_journal(sum, i)) == val)
1352 return i;
1353 }
1354 if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
1355 return update_nats_in_cursum(sum, 1);
1356 } else if (type == SIT_JOURNAL) {
1357 for (i = 0; i < sits_in_cursum(sum); i++)
1358 if (le32_to_cpu(segno_in_journal(sum, i)) == val)
1359 return i;
1360 if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
1361 return update_sits_in_cursum(sum, 1);
1362 }
1363 return -1;
1364}
1365
1366static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
1367 unsigned int segno)
1368{
1369 struct sit_info *sit_i = SIT_I(sbi);
1370 unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno);
1371 block_t blk_addr = sit_i->sit_base_addr + offset;
1372
1373 check_seg_range(sbi, segno);
1374
1375 /* calculate sit block address */
1376 if (f2fs_test_bit(offset, sit_i->sit_bitmap))
1377 blk_addr += sit_i->sit_blocks;
1378
1379 return get_meta_page(sbi, blk_addr);
1380}
1381
1382static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
1383 unsigned int start)
1384{
1385 struct sit_info *sit_i = SIT_I(sbi);
1386 struct page *src_page, *dst_page;
1387 pgoff_t src_off, dst_off;
1388 void *src_addr, *dst_addr;
1389
1390 src_off = current_sit_addr(sbi, start);
1391 dst_off = next_sit_addr(sbi, src_off);
1392
1393 /* get current sit block page without lock */
1394 src_page = get_meta_page(sbi, src_off);
1395 dst_page = grab_meta_page(sbi, dst_off);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001396 f2fs_bug_on(PageDirty(src_page));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001397
1398 src_addr = page_address(src_page);
1399 dst_addr = page_address(dst_page);
1400 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
1401
1402 set_page_dirty(dst_page);
1403 f2fs_put_page(src_page, 1);
1404
1405 set_to_next_sit(sit_i, start);
1406
1407 return dst_page;
1408}
1409
1410static bool flush_sits_in_journal(struct f2fs_sb_info *sbi)
1411{
1412 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1413 struct f2fs_summary_block *sum = curseg->sum_blk;
1414 int i;
1415
1416 /*
1417 * If the journal area in the current summary is full of sit entries,
1418 * all the sit entries will be flushed. Otherwise the sit entries
1419 * are not able to replace with newly hot sit entries.
1420 */
1421 if (sits_in_cursum(sum) >= SIT_JOURNAL_ENTRIES) {
1422 for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
1423 unsigned int segno;
1424 segno = le32_to_cpu(segno_in_journal(sum, i));
1425 __mark_sit_entry_dirty(sbi, segno);
1426 }
1427 update_sits_in_cursum(sum, -sits_in_cursum(sum));
Haicheng Licffbfa62013-10-18 17:24:07 +08001428 return true;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001429 }
Haicheng Licffbfa62013-10-18 17:24:07 +08001430 return false;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001431}
1432
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001433/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001434 * CP calls this function, which flushes SIT entries including sit_journal,
1435 * and moves prefree segs to free segs.
1436 */
1437void flush_sit_entries(struct f2fs_sb_info *sbi)
1438{
1439 struct sit_info *sit_i = SIT_I(sbi);
1440 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
1441 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1442 struct f2fs_summary_block *sum = curseg->sum_blk;
1443 unsigned long nsegs = TOTAL_SEGS(sbi);
1444 struct page *page = NULL;
1445 struct f2fs_sit_block *raw_sit = NULL;
1446 unsigned int start = 0, end = 0;
1447 unsigned int segno = -1;
1448 bool flushed;
1449
1450 mutex_lock(&curseg->curseg_mutex);
1451 mutex_lock(&sit_i->sentry_lock);
1452
1453 /*
1454 * "flushed" indicates whether sit entries in journal are flushed
1455 * to the SIT area or not.
1456 */
1457 flushed = flush_sits_in_journal(sbi);
1458
1459 while ((segno = find_next_bit(bitmap, nsegs, segno + 1)) < nsegs) {
1460 struct seg_entry *se = get_seg_entry(sbi, segno);
1461 int sit_offset, offset;
1462
1463 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
1464
1465 if (flushed)
1466 goto to_sit_page;
1467
1468 offset = lookup_journal_in_cursum(sum, SIT_JOURNAL, segno, 1);
1469 if (offset >= 0) {
1470 segno_in_journal(sum, offset) = cpu_to_le32(segno);
1471 seg_info_to_raw_sit(se, &sit_in_journal(sum, offset));
1472 goto flush_done;
1473 }
1474to_sit_page:
1475 if (!page || (start > segno) || (segno > end)) {
1476 if (page) {
1477 f2fs_put_page(page, 1);
1478 page = NULL;
1479 }
1480
1481 start = START_SEGNO(sit_i, segno);
1482 end = start + SIT_ENTRY_PER_BLOCK - 1;
1483
1484 /* read sit block that will be updated */
1485 page = get_next_sit_page(sbi, start);
1486 raw_sit = page_address(page);
1487 }
1488
1489 /* udpate entry in SIT block */
1490 seg_info_to_raw_sit(se, &raw_sit->entries[sit_offset]);
1491flush_done:
1492 __clear_bit(segno, bitmap);
1493 sit_i->dirty_sentries--;
1494 }
1495 mutex_unlock(&sit_i->sentry_lock);
1496 mutex_unlock(&curseg->curseg_mutex);
1497
1498 /* writeout last modified SIT block */
1499 f2fs_put_page(page, 1);
1500
1501 set_prefree_as_free_segments(sbi);
1502}
1503
1504static int build_sit_info(struct f2fs_sb_info *sbi)
1505{
1506 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1507 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1508 struct sit_info *sit_i;
1509 unsigned int sit_segs, start;
1510 char *src_bitmap, *dst_bitmap;
1511 unsigned int bitmap_size;
1512
1513 /* allocate memory for SIT information */
1514 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
1515 if (!sit_i)
1516 return -ENOMEM;
1517
1518 SM_I(sbi)->sit_info = sit_i;
1519
1520 sit_i->sentries = vzalloc(TOTAL_SEGS(sbi) * sizeof(struct seg_entry));
1521 if (!sit_i->sentries)
1522 return -ENOMEM;
1523
1524 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1525 sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1526 if (!sit_i->dirty_sentries_bitmap)
1527 return -ENOMEM;
1528
1529 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1530 sit_i->sentries[start].cur_valid_map
1531 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1532 sit_i->sentries[start].ckpt_valid_map
1533 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1534 if (!sit_i->sentries[start].cur_valid_map
1535 || !sit_i->sentries[start].ckpt_valid_map)
1536 return -ENOMEM;
1537 }
1538
1539 if (sbi->segs_per_sec > 1) {
Jaegeuk Kim53cf9522013-03-31 12:39:49 +09001540 sit_i->sec_entries = vzalloc(TOTAL_SECS(sbi) *
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001541 sizeof(struct sec_entry));
1542 if (!sit_i->sec_entries)
1543 return -ENOMEM;
1544 }
1545
1546 /* get information related with SIT */
1547 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
1548
1549 /* setup SIT bitmap from ckeckpoint pack */
1550 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
1551 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
1552
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001553 dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001554 if (!dst_bitmap)
1555 return -ENOMEM;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001556
1557 /* init SIT information */
1558 sit_i->s_ops = &default_salloc_ops;
1559
1560 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
1561 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
1562 sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
1563 sit_i->sit_bitmap = dst_bitmap;
1564 sit_i->bitmap_size = bitmap_size;
1565 sit_i->dirty_sentries = 0;
1566 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
1567 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
1568 sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
1569 mutex_init(&sit_i->sentry_lock);
1570 return 0;
1571}
1572
1573static int build_free_segmap(struct f2fs_sb_info *sbi)
1574{
1575 struct f2fs_sm_info *sm_info = SM_I(sbi);
1576 struct free_segmap_info *free_i;
1577 unsigned int bitmap_size, sec_bitmap_size;
1578
1579 /* allocate memory for free segmap information */
1580 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
1581 if (!free_i)
1582 return -ENOMEM;
1583
1584 SM_I(sbi)->free_info = free_i;
1585
1586 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1587 free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL);
1588 if (!free_i->free_segmap)
1589 return -ENOMEM;
1590
Jaegeuk Kim53cf9522013-03-31 12:39:49 +09001591 sec_bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001592 free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL);
1593 if (!free_i->free_secmap)
1594 return -ENOMEM;
1595
1596 /* set all segments as dirty temporarily */
1597 memset(free_i->free_segmap, 0xff, bitmap_size);
1598 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
1599
1600 /* init free segmap information */
1601 free_i->start_segno =
1602 (unsigned int) GET_SEGNO_FROM_SEG0(sbi, sm_info->main_blkaddr);
1603 free_i->free_segments = 0;
1604 free_i->free_sections = 0;
1605 rwlock_init(&free_i->segmap_lock);
1606 return 0;
1607}
1608
1609static int build_curseg(struct f2fs_sb_info *sbi)
1610{
Namjae Jeon1042d602012-12-01 10:56:13 +09001611 struct curseg_info *array;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001612 int i;
1613
1614 array = kzalloc(sizeof(*array) * NR_CURSEG_TYPE, GFP_KERNEL);
1615 if (!array)
1616 return -ENOMEM;
1617
1618 SM_I(sbi)->curseg_array = array;
1619
1620 for (i = 0; i < NR_CURSEG_TYPE; i++) {
1621 mutex_init(&array[i].curseg_mutex);
1622 array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
1623 if (!array[i].sum_blk)
1624 return -ENOMEM;
1625 array[i].segno = NULL_SEGNO;
1626 array[i].next_blkoff = 0;
1627 }
1628 return restore_curseg_summaries(sbi);
1629}
1630
1631static void build_sit_entries(struct f2fs_sb_info *sbi)
1632{
1633 struct sit_info *sit_i = SIT_I(sbi);
1634 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1635 struct f2fs_summary_block *sum = curseg->sum_blk;
1636 unsigned int start;
1637
1638 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1639 struct seg_entry *se = &sit_i->sentries[start];
1640 struct f2fs_sit_block *sit_blk;
1641 struct f2fs_sit_entry sit;
1642 struct page *page;
1643 int i;
1644
1645 mutex_lock(&curseg->curseg_mutex);
1646 for (i = 0; i < sits_in_cursum(sum); i++) {
1647 if (le32_to_cpu(segno_in_journal(sum, i)) == start) {
1648 sit = sit_in_journal(sum, i);
1649 mutex_unlock(&curseg->curseg_mutex);
1650 goto got_it;
1651 }
1652 }
1653 mutex_unlock(&curseg->curseg_mutex);
1654 page = get_current_sit_page(sbi, start);
1655 sit_blk = (struct f2fs_sit_block *)page_address(page);
1656 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
1657 f2fs_put_page(page, 1);
1658got_it:
1659 check_block_count(sbi, start, &sit);
1660 seg_info_from_raw_sit(se, &sit);
1661 if (sbi->segs_per_sec > 1) {
1662 struct sec_entry *e = get_sec_entry(sbi, start);
1663 e->valid_blocks += se->valid_blocks;
1664 }
1665 }
1666}
1667
1668static void init_free_segmap(struct f2fs_sb_info *sbi)
1669{
1670 unsigned int start;
1671 int type;
1672
1673 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1674 struct seg_entry *sentry = get_seg_entry(sbi, start);
1675 if (!sentry->valid_blocks)
1676 __set_free(sbi, start);
1677 }
1678
1679 /* set use the current segments */
1680 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
1681 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
1682 __set_test_and_inuse(sbi, curseg_t->segno);
1683 }
1684}
1685
1686static void init_dirty_segmap(struct f2fs_sb_info *sbi)
1687{
1688 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1689 struct free_segmap_info *free_i = FREE_I(sbi);
Namjae Jeon8736fbf2013-06-16 09:49:11 +09001690 unsigned int segno = 0, offset = 0, total_segs = TOTAL_SEGS(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001691 unsigned short valid_blocks;
1692
Namjae Jeon8736fbf2013-06-16 09:49:11 +09001693 while (1) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001694 /* find dirty segment based on free segmap */
Namjae Jeon8736fbf2013-06-16 09:49:11 +09001695 segno = find_next_inuse(free_i, total_segs, offset);
1696 if (segno >= total_segs)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001697 break;
1698 offset = segno + 1;
1699 valid_blocks = get_valid_blocks(sbi, segno, 0);
1700 if (valid_blocks >= sbi->blocks_per_seg || !valid_blocks)
1701 continue;
1702 mutex_lock(&dirty_i->seglist_lock);
1703 __locate_dirty_segment(sbi, segno, DIRTY);
1704 mutex_unlock(&dirty_i->seglist_lock);
1705 }
1706}
1707
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001708static int init_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001709{
1710 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001711 unsigned int bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001712
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001713 dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL);
1714 if (!dirty_i->victim_secmap)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001715 return -ENOMEM;
1716 return 0;
1717}
1718
1719static int build_dirty_segmap(struct f2fs_sb_info *sbi)
1720{
1721 struct dirty_seglist_info *dirty_i;
1722 unsigned int bitmap_size, i;
1723
1724 /* allocate memory for dirty segments list information */
1725 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
1726 if (!dirty_i)
1727 return -ENOMEM;
1728
1729 SM_I(sbi)->dirty_info = dirty_i;
1730 mutex_init(&dirty_i->seglist_lock);
1731
1732 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1733
1734 for (i = 0; i < NR_DIRTY_TYPE; i++) {
1735 dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001736 if (!dirty_i->dirty_segmap[i])
1737 return -ENOMEM;
1738 }
1739
1740 init_dirty_segmap(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001741 return init_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001742}
1743
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001744/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001745 * Update min, max modified time for cost-benefit GC algorithm
1746 */
1747static void init_min_max_mtime(struct f2fs_sb_info *sbi)
1748{
1749 struct sit_info *sit_i = SIT_I(sbi);
1750 unsigned int segno;
1751
1752 mutex_lock(&sit_i->sentry_lock);
1753
1754 sit_i->min_mtime = LLONG_MAX;
1755
1756 for (segno = 0; segno < TOTAL_SEGS(sbi); segno += sbi->segs_per_sec) {
1757 unsigned int i;
1758 unsigned long long mtime = 0;
1759
1760 for (i = 0; i < sbi->segs_per_sec; i++)
1761 mtime += get_seg_entry(sbi, segno + i)->mtime;
1762
1763 mtime = div_u64(mtime, sbi->segs_per_sec);
1764
1765 if (sit_i->min_mtime > mtime)
1766 sit_i->min_mtime = mtime;
1767 }
1768 sit_i->max_mtime = get_mtime(sbi);
1769 mutex_unlock(&sit_i->sentry_lock);
1770}
1771
1772int build_segment_manager(struct f2fs_sb_info *sbi)
1773{
1774 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1775 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Namjae Jeon1042d602012-12-01 10:56:13 +09001776 struct f2fs_sm_info *sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001777 int err;
1778
1779 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
1780 if (!sm_info)
1781 return -ENOMEM;
1782
1783 /* init sm info */
1784 sbi->sm_info = sm_info;
1785 INIT_LIST_HEAD(&sm_info->wblist_head);
1786 spin_lock_init(&sm_info->wblist_lock);
1787 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
1788 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
1789 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
1790 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
1791 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
1792 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
1793 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
Jaegeuk Kim81eb8d62013-10-24 13:31:34 +09001794 sm_info->rec_prefree_segments = DEF_RECLAIM_PREFREE_SEGMENTS;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001795
1796 err = build_sit_info(sbi);
1797 if (err)
1798 return err;
1799 err = build_free_segmap(sbi);
1800 if (err)
1801 return err;
1802 err = build_curseg(sbi);
1803 if (err)
1804 return err;
1805
1806 /* reinit free segmap based on SIT */
1807 build_sit_entries(sbi);
1808
1809 init_free_segmap(sbi);
1810 err = build_dirty_segmap(sbi);
1811 if (err)
1812 return err;
1813
1814 init_min_max_mtime(sbi);
1815 return 0;
1816}
1817
1818static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
1819 enum dirty_type dirty_type)
1820{
1821 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1822
1823 mutex_lock(&dirty_i->seglist_lock);
1824 kfree(dirty_i->dirty_segmap[dirty_type]);
1825 dirty_i->nr_dirty[dirty_type] = 0;
1826 mutex_unlock(&dirty_i->seglist_lock);
1827}
1828
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001829static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001830{
1831 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001832 kfree(dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001833}
1834
1835static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
1836{
1837 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1838 int i;
1839
1840 if (!dirty_i)
1841 return;
1842
1843 /* discard pre-free/dirty segments list */
1844 for (i = 0; i < NR_DIRTY_TYPE; i++)
1845 discard_dirty_segmap(sbi, i);
1846
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001847 destroy_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001848 SM_I(sbi)->dirty_info = NULL;
1849 kfree(dirty_i);
1850}
1851
1852static void destroy_curseg(struct f2fs_sb_info *sbi)
1853{
1854 struct curseg_info *array = SM_I(sbi)->curseg_array;
1855 int i;
1856
1857 if (!array)
1858 return;
1859 SM_I(sbi)->curseg_array = NULL;
1860 for (i = 0; i < NR_CURSEG_TYPE; i++)
1861 kfree(array[i].sum_blk);
1862 kfree(array);
1863}
1864
1865static void destroy_free_segmap(struct f2fs_sb_info *sbi)
1866{
1867 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
1868 if (!free_i)
1869 return;
1870 SM_I(sbi)->free_info = NULL;
1871 kfree(free_i->free_segmap);
1872 kfree(free_i->free_secmap);
1873 kfree(free_i);
1874}
1875
1876static void destroy_sit_info(struct f2fs_sb_info *sbi)
1877{
1878 struct sit_info *sit_i = SIT_I(sbi);
1879 unsigned int start;
1880
1881 if (!sit_i)
1882 return;
1883
1884 if (sit_i->sentries) {
1885 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1886 kfree(sit_i->sentries[start].cur_valid_map);
1887 kfree(sit_i->sentries[start].ckpt_valid_map);
1888 }
1889 }
1890 vfree(sit_i->sentries);
1891 vfree(sit_i->sec_entries);
1892 kfree(sit_i->dirty_sentries_bitmap);
1893
1894 SM_I(sbi)->sit_info = NULL;
1895 kfree(sit_i->sit_bitmap);
1896 kfree(sit_i);
1897}
1898
1899void destroy_segment_manager(struct f2fs_sb_info *sbi)
1900{
1901 struct f2fs_sm_info *sm_info = SM_I(sbi);
Chao Yu3b03f722013-11-06 09:12:04 +08001902 if (!sm_info)
1903 return;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001904 destroy_dirty_segmap(sbi);
1905 destroy_curseg(sbi);
1906 destroy_free_segmap(sbi);
1907 destroy_sit_info(sbi);
1908 sbi->sm_info = NULL;
1909 kfree(sm_info);
1910}