blob: 555ae7693ea090d6e09c9ba42487c1975ebc2ffe [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>
Chao Yu74de5932013-11-22 09:09:59 +080017#include <linux/swap.h>
Jaegeuk Kim351df4b2012-11-02 17:09:16 +090018
19#include "f2fs.h"
20#include "segment.h"
21#include "node.h"
Namjae Jeon6ec178d2013-04-23 17:51:43 +090022#include <trace/events/f2fs.h>
Jaegeuk Kim351df4b2012-11-02 17:09:16 +090023
Changman Lee9a7f1432013-11-15 10:42:51 +090024#define __reverse_ffz(x) __reverse_ffs(~(x))
25
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +090026static struct kmem_cache *discard_entry_slab;
27
Changman Lee9a7f1432013-11-15 10:42:51 +090028/*
29 * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since
30 * MSB and LSB are reversed in a byte by f2fs_set_bit.
31 */
32static inline unsigned long __reverse_ffs(unsigned long word)
33{
34 int num = 0;
35
36#if BITS_PER_LONG == 64
37 if ((word & 0xffffffff) == 0) {
38 num += 32;
39 word >>= 32;
40 }
41#endif
42 if ((word & 0xffff) == 0) {
43 num += 16;
44 word >>= 16;
45 }
46 if ((word & 0xff) == 0) {
47 num += 8;
48 word >>= 8;
49 }
50 if ((word & 0xf0) == 0)
51 num += 4;
52 else
53 word >>= 4;
54 if ((word & 0xc) == 0)
55 num += 2;
56 else
57 word >>= 2;
58 if ((word & 0x2) == 0)
59 num += 1;
60 return num;
61}
62
63/*
64 * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c becasue
65 * f2fs_set_bit makes MSB and LSB reversed in a byte.
66 * Example:
67 * LSB <--> MSB
68 * f2fs_set_bit(0, bitmap) => 0000 0001
69 * f2fs_set_bit(7, bitmap) => 1000 0000
70 */
71static unsigned long __find_rev_next_bit(const unsigned long *addr,
72 unsigned long size, unsigned long offset)
73{
74 const unsigned long *p = addr + BIT_WORD(offset);
75 unsigned long result = offset & ~(BITS_PER_LONG - 1);
76 unsigned long tmp;
77 unsigned long mask, submask;
78 unsigned long quot, rest;
79
80 if (offset >= size)
81 return size;
82
83 size -= result;
84 offset %= BITS_PER_LONG;
85 if (!offset)
86 goto aligned;
87
88 tmp = *(p++);
89 quot = (offset >> 3) << 3;
90 rest = offset & 0x7;
91 mask = ~0UL << quot;
92 submask = (unsigned char)(0xff << rest) >> rest;
93 submask <<= quot;
94 mask &= submask;
95 tmp &= mask;
96 if (size < BITS_PER_LONG)
97 goto found_first;
98 if (tmp)
99 goto found_middle;
100
101 size -= BITS_PER_LONG;
102 result += BITS_PER_LONG;
103aligned:
104 while (size & ~(BITS_PER_LONG-1)) {
105 tmp = *(p++);
106 if (tmp)
107 goto found_middle;
108 result += BITS_PER_LONG;
109 size -= BITS_PER_LONG;
110 }
111 if (!size)
112 return result;
113 tmp = *p;
114found_first:
115 tmp &= (~0UL >> (BITS_PER_LONG - size));
116 if (tmp == 0UL) /* Are any bits set? */
117 return result + size; /* Nope. */
118found_middle:
119 return result + __reverse_ffs(tmp);
120}
121
122static unsigned long __find_rev_next_zero_bit(const unsigned long *addr,
123 unsigned long size, unsigned long offset)
124{
125 const unsigned long *p = addr + BIT_WORD(offset);
126 unsigned long result = offset & ~(BITS_PER_LONG - 1);
127 unsigned long tmp;
128 unsigned long mask, submask;
129 unsigned long quot, rest;
130
131 if (offset >= size)
132 return size;
133
134 size -= result;
135 offset %= BITS_PER_LONG;
136 if (!offset)
137 goto aligned;
138
139 tmp = *(p++);
140 quot = (offset >> 3) << 3;
141 rest = offset & 0x7;
142 mask = ~(~0UL << quot);
143 submask = (unsigned char)~((unsigned char)(0xff << rest) >> rest);
144 submask <<= quot;
145 mask += submask;
146 tmp |= mask;
147 if (size < BITS_PER_LONG)
148 goto found_first;
149 if (~tmp)
150 goto found_middle;
151
152 size -= BITS_PER_LONG;
153 result += BITS_PER_LONG;
154aligned:
155 while (size & ~(BITS_PER_LONG - 1)) {
156 tmp = *(p++);
157 if (~tmp)
158 goto found_middle;
159 result += BITS_PER_LONG;
160 size -= BITS_PER_LONG;
161 }
162 if (!size)
163 return result;
164 tmp = *p;
165
166found_first:
167 tmp |= ~0UL << size;
168 if (tmp == ~0UL) /* Are any bits zero? */
169 return result + size; /* Nope. */
170found_middle:
171 return result + __reverse_ffz(tmp);
172}
173
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900174/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900175 * This function balances dirty node and dentry pages.
176 * In addition, it controls garbage collection.
177 */
178void f2fs_balance_fs(struct f2fs_sb_info *sbi)
179{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900180 /*
Jaegeuk Kim029cd282012-12-21 17:20:21 +0900181 * We should do GC or end up with checkpoint, if there are so many dirty
182 * dir/node pages without enough free segments.
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900183 */
Jaegeuk Kim43727522013-02-04 15:11:17 +0900184 if (has_not_enough_free_secs(sbi, 0)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900185 mutex_lock(&sbi->gc_mutex);
Jaegeuk Kim408e9372013-01-03 17:55:52 +0900186 f2fs_gc(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900187 }
188}
189
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900190void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi)
191{
192 /* check the # of cached NAT entries and prefree segments */
193 if (try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK) ||
194 excess_prefree_segs(sbi))
195 f2fs_sync_fs(sbi->sb, true);
196}
197
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900198static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
199 enum dirty_type dirty_type)
200{
201 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
202
203 /* need not be added */
204 if (IS_CURSEG(sbi, segno))
205 return;
206
207 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
208 dirty_i->nr_dirty[dirty_type]++;
209
210 if (dirty_type == DIRTY) {
211 struct seg_entry *sentry = get_seg_entry(sbi, segno);
Changman Lee4625d6a2013-10-25 17:31:57 +0900212 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900213
Changman Lee4625d6a2013-10-25 17:31:57 +0900214 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
215 dirty_i->nr_dirty[t]++;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900216 }
217}
218
219static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
220 enum dirty_type dirty_type)
221{
222 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
223
224 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
225 dirty_i->nr_dirty[dirty_type]--;
226
227 if (dirty_type == DIRTY) {
Changman Lee4625d6a2013-10-25 17:31:57 +0900228 struct seg_entry *sentry = get_seg_entry(sbi, segno);
229 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900230
Changman Lee4625d6a2013-10-25 17:31:57 +0900231 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
232 dirty_i->nr_dirty[t]--;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900233
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +0900234 if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0)
235 clear_bit(GET_SECNO(sbi, segno),
236 dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900237 }
238}
239
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900240/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900241 * Should not occur error such as -ENOMEM.
242 * Adding dirty entry into seglist is not critical operation.
243 * If a given segment is one of current working segments, it won't be added.
244 */
Haicheng Li8d8451a2013-06-13 16:59:28 +0800245static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900246{
247 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
248 unsigned short valid_blocks;
249
250 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
251 return;
252
253 mutex_lock(&dirty_i->seglist_lock);
254
255 valid_blocks = get_valid_blocks(sbi, segno, 0);
256
257 if (valid_blocks == 0) {
258 __locate_dirty_segment(sbi, segno, PRE);
259 __remove_dirty_segment(sbi, segno, DIRTY);
260 } else if (valid_blocks < sbi->blocks_per_seg) {
261 __locate_dirty_segment(sbi, segno, DIRTY);
262 } else {
263 /* Recovery routine with SSR needs this */
264 __remove_dirty_segment(sbi, segno, DIRTY);
265 }
266
267 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900268}
269
Jaegeuk Kim37208872013-11-12 16:55:17 +0900270static void f2fs_issue_discard(struct f2fs_sb_info *sbi,
271 block_t blkstart, block_t blklen)
272{
Jaegeuk Kimf9a4e6d2013-11-28 12:44:05 +0900273 sector_t start = SECTOR_FROM_BLOCK(sbi, blkstart);
274 sector_t len = SECTOR_FROM_BLOCK(sbi, blklen);
Jaegeuk Kim37208872013-11-12 16:55:17 +0900275 blkdev_issue_discard(sbi->sb->s_bdev, start, len, GFP_NOFS, 0);
Jaegeuk Kim1661d072013-11-12 17:01:00 +0900276 trace_f2fs_issue_discard(sbi->sb, blkstart, blklen);
Jaegeuk Kim37208872013-11-12 16:55:17 +0900277}
278
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900279static void add_discard_addrs(struct f2fs_sb_info *sbi,
280 unsigned int segno, struct seg_entry *se)
281{
282 struct list_head *head = &SM_I(sbi)->discard_list;
283 struct discard_entry *new;
284 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
285 int max_blocks = sbi->blocks_per_seg;
286 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
287 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
288 unsigned long dmap[entries];
289 unsigned int start = 0, end = -1;
290 int i;
291
292 if (!test_opt(sbi, DISCARD))
293 return;
294
295 /* zero block will be discarded through the prefree list */
296 if (!se->valid_blocks || se->valid_blocks == max_blocks)
297 return;
298
299 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
300 for (i = 0; i < entries; i++)
301 dmap[i] = (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
302
303 while (SM_I(sbi)->nr_discards <= SM_I(sbi)->max_discards) {
304 start = __find_rev_next_bit(dmap, max_blocks, end + 1);
305 if (start >= max_blocks)
306 break;
307
308 end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
309
310 new = f2fs_kmem_cache_alloc(discard_entry_slab, GFP_NOFS);
311 INIT_LIST_HEAD(&new->list);
312 new->blkaddr = START_BLOCK(sbi, segno) + start;
313 new->len = end - start;
314
315 list_add_tail(&new->list, head);
316 SM_I(sbi)->nr_discards += end - start;
317 }
318}
319
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900320/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900321 * Should call clear_prefree_segments after checkpoint is done.
322 */
323static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
324{
325 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Haicheng Li81fb5e82013-05-14 18:20:28 +0800326 unsigned int segno = -1;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900327 unsigned int total_segs = TOTAL_SEGS(sbi);
328
329 mutex_lock(&dirty_i->seglist_lock);
330 while (1) {
331 segno = find_next_bit(dirty_i->dirty_segmap[PRE], total_segs,
Haicheng Li81fb5e82013-05-14 18:20:28 +0800332 segno + 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900333 if (segno >= total_segs)
334 break;
335 __set_test_and_free(sbi, segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900336 }
337 mutex_unlock(&dirty_i->seglist_lock);
338}
339
340void clear_prefree_segments(struct f2fs_sb_info *sbi)
341{
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900342 struct list_head *head = &(SM_I(sbi)->discard_list);
343 struct list_head *this, *next;
344 struct discard_entry *entry;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900345 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Changman Lee29e59c12013-11-11 09:24:37 +0900346 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900347 unsigned int total_segs = TOTAL_SEGS(sbi);
Changman Lee29e59c12013-11-11 09:24:37 +0900348 unsigned int start = 0, end = -1;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900349
350 mutex_lock(&dirty_i->seglist_lock);
Changman Lee29e59c12013-11-11 09:24:37 +0900351
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900352 while (1) {
Changman Lee29e59c12013-11-11 09:24:37 +0900353 int i;
354 start = find_next_bit(prefree_map, total_segs, end + 1);
355 if (start >= total_segs)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900356 break;
Changman Lee29e59c12013-11-11 09:24:37 +0900357 end = find_next_zero_bit(prefree_map, total_segs, start + 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900358
Changman Lee29e59c12013-11-11 09:24:37 +0900359 for (i = start; i < end; i++)
360 clear_bit(i, prefree_map);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900361
Changman Lee29e59c12013-11-11 09:24:37 +0900362 dirty_i->nr_dirty[PRE] -= end - start;
363
364 if (!test_opt(sbi, DISCARD))
365 continue;
366
Jaegeuk Kim37208872013-11-12 16:55:17 +0900367 f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
368 (end - start) << sbi->log_blocks_per_seg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900369 }
370 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900371
372 /* send small discards */
373 list_for_each_safe(this, next, head) {
374 entry = list_entry(this, struct discard_entry, list);
Jaegeuk Kim37208872013-11-12 16:55:17 +0900375 f2fs_issue_discard(sbi, entry->blkaddr, entry->len);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900376 list_del(&entry->list);
377 SM_I(sbi)->nr_discards -= entry->len;
378 kmem_cache_free(discard_entry_slab, entry);
379 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900380}
381
382static void __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
383{
384 struct sit_info *sit_i = SIT_I(sbi);
385 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap))
386 sit_i->dirty_sentries++;
387}
388
389static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
390 unsigned int segno, int modified)
391{
392 struct seg_entry *se = get_seg_entry(sbi, segno);
393 se->type = type;
394 if (modified)
395 __mark_sit_entry_dirty(sbi, segno);
396}
397
398static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
399{
400 struct seg_entry *se;
401 unsigned int segno, offset;
402 long int new_vblocks;
403
404 segno = GET_SEGNO(sbi, blkaddr);
405
406 se = get_seg_entry(sbi, segno);
407 new_vblocks = se->valid_blocks + del;
408 offset = GET_SEGOFF_FROM_SEG0(sbi, blkaddr) & (sbi->blocks_per_seg - 1);
409
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900410 f2fs_bug_on((new_vblocks >> (sizeof(unsigned short) << 3) ||
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900411 (new_vblocks > sbi->blocks_per_seg)));
412
413 se->valid_blocks = new_vblocks;
414 se->mtime = get_mtime(sbi);
415 SIT_I(sbi)->max_mtime = se->mtime;
416
417 /* Update valid block bitmap */
418 if (del > 0) {
419 if (f2fs_set_bit(offset, se->cur_valid_map))
420 BUG();
421 } else {
422 if (!f2fs_clear_bit(offset, se->cur_valid_map))
423 BUG();
424 }
425 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
426 se->ckpt_valid_blocks += del;
427
428 __mark_sit_entry_dirty(sbi, segno);
429
430 /* update total number of valid blocks to be written in ckpt area */
431 SIT_I(sbi)->written_valid_blocks += del;
432
433 if (sbi->segs_per_sec > 1)
434 get_sec_entry(sbi, segno)->valid_blocks += del;
435}
436
437static void refresh_sit_entry(struct f2fs_sb_info *sbi,
438 block_t old_blkaddr, block_t new_blkaddr)
439{
440 update_sit_entry(sbi, new_blkaddr, 1);
441 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
442 update_sit_entry(sbi, old_blkaddr, -1);
443}
444
445void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
446{
447 unsigned int segno = GET_SEGNO(sbi, addr);
448 struct sit_info *sit_i = SIT_I(sbi);
449
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900450 f2fs_bug_on(addr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900451 if (addr == NEW_ADDR)
452 return;
453
454 /* add it into sit main buffer */
455 mutex_lock(&sit_i->sentry_lock);
456
457 update_sit_entry(sbi, addr, -1);
458
459 /* add it into dirty seglist */
460 locate_dirty_segment(sbi, segno);
461
462 mutex_unlock(&sit_i->sentry_lock);
463}
464
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900465/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900466 * This function should be resided under the curseg_mutex lock
467 */
468static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
Haicheng Lie79efe32013-06-13 16:59:27 +0800469 struct f2fs_summary *sum)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900470{
471 struct curseg_info *curseg = CURSEG_I(sbi, type);
472 void *addr = curseg->sum_blk;
Haicheng Lie79efe32013-06-13 16:59:27 +0800473 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900474 memcpy(addr, sum, sizeof(struct f2fs_summary));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900475}
476
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900477/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900478 * Calculate the number of current summary pages for writing
479 */
480int npages_for_summary_flush(struct f2fs_sb_info *sbi)
481{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900482 int valid_sum_count = 0;
Fan Li9a479382013-10-29 16:21:47 +0800483 int i, sum_in_page;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900484
485 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
486 if (sbi->ckpt->alloc_type[i] == SSR)
487 valid_sum_count += sbi->blocks_per_seg;
488 else
489 valid_sum_count += curseg_blkoff(sbi, i);
490 }
491
Fan Li9a479382013-10-29 16:21:47 +0800492 sum_in_page = (PAGE_CACHE_SIZE - 2 * SUM_JOURNAL_SIZE -
493 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
494 if (valid_sum_count <= sum_in_page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900495 return 1;
Fan Li9a479382013-10-29 16:21:47 +0800496 else if ((valid_sum_count - sum_in_page) <=
497 (PAGE_CACHE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900498 return 2;
499 return 3;
500}
501
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900502/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900503 * Caller should put this summary page
504 */
505struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
506{
507 return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
508}
509
510static void write_sum_page(struct f2fs_sb_info *sbi,
511 struct f2fs_summary_block *sum_blk, block_t blk_addr)
512{
513 struct page *page = grab_meta_page(sbi, blk_addr);
514 void *kaddr = page_address(page);
515 memcpy(kaddr, sum_blk, PAGE_CACHE_SIZE);
516 set_page_dirty(page);
517 f2fs_put_page(page, 1);
518}
519
Jaegeuk Kim60374682013-03-31 13:58:51 +0900520static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
521{
522 struct curseg_info *curseg = CURSEG_I(sbi, type);
Haicheng Li81fb5e82013-05-14 18:20:28 +0800523 unsigned int segno = curseg->segno + 1;
Jaegeuk Kim60374682013-03-31 13:58:51 +0900524 struct free_segmap_info *free_i = FREE_I(sbi);
525
Haicheng Li81fb5e82013-05-14 18:20:28 +0800526 if (segno < TOTAL_SEGS(sbi) && segno % sbi->segs_per_sec)
527 return !test_bit(segno, free_i->free_segmap);
Jaegeuk Kim60374682013-03-31 13:58:51 +0900528 return 0;
529}
530
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900531/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900532 * Find a new segment from the free segments bitmap to right order
533 * This function should be returned with success, otherwise BUG
534 */
535static void get_new_segment(struct f2fs_sb_info *sbi,
536 unsigned int *newseg, bool new_sec, int dir)
537{
538 struct free_segmap_info *free_i = FREE_I(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900539 unsigned int segno, secno, zoneno;
Jaegeuk Kim53cf9522013-03-31 12:39:49 +0900540 unsigned int total_zones = TOTAL_SECS(sbi) / sbi->secs_per_zone;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900541 unsigned int hint = *newseg / sbi->segs_per_sec;
542 unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
543 unsigned int left_start = hint;
544 bool init = true;
545 int go_left = 0;
546 int i;
547
548 write_lock(&free_i->segmap_lock);
549
550 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
551 segno = find_next_zero_bit(free_i->free_segmap,
552 TOTAL_SEGS(sbi), *newseg + 1);
Jaegeuk Kim33afa7f2013-03-31 12:59:53 +0900553 if (segno - *newseg < sbi->segs_per_sec -
554 (*newseg % sbi->segs_per_sec))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900555 goto got_it;
556 }
557find_other_zone:
Jaegeuk Kim53cf9522013-03-31 12:39:49 +0900558 secno = find_next_zero_bit(free_i->free_secmap, TOTAL_SECS(sbi), hint);
559 if (secno >= TOTAL_SECS(sbi)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900560 if (dir == ALLOC_RIGHT) {
561 secno = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim53cf9522013-03-31 12:39:49 +0900562 TOTAL_SECS(sbi), 0);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900563 f2fs_bug_on(secno >= TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900564 } else {
565 go_left = 1;
566 left_start = hint - 1;
567 }
568 }
569 if (go_left == 0)
570 goto skip_left;
571
572 while (test_bit(left_start, free_i->free_secmap)) {
573 if (left_start > 0) {
574 left_start--;
575 continue;
576 }
577 left_start = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim53cf9522013-03-31 12:39:49 +0900578 TOTAL_SECS(sbi), 0);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900579 f2fs_bug_on(left_start >= TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900580 break;
581 }
582 secno = left_start;
583skip_left:
584 hint = secno;
585 segno = secno * sbi->segs_per_sec;
586 zoneno = secno / sbi->secs_per_zone;
587
588 /* give up on finding another zone */
589 if (!init)
590 goto got_it;
591 if (sbi->secs_per_zone == 1)
592 goto got_it;
593 if (zoneno == old_zoneno)
594 goto got_it;
595 if (dir == ALLOC_LEFT) {
596 if (!go_left && zoneno + 1 >= total_zones)
597 goto got_it;
598 if (go_left && zoneno == 0)
599 goto got_it;
600 }
601 for (i = 0; i < NR_CURSEG_TYPE; i++)
602 if (CURSEG_I(sbi, i)->zone == zoneno)
603 break;
604
605 if (i < NR_CURSEG_TYPE) {
606 /* zone is in user, try another */
607 if (go_left)
608 hint = zoneno * sbi->secs_per_zone - 1;
609 else if (zoneno + 1 >= total_zones)
610 hint = 0;
611 else
612 hint = (zoneno + 1) * sbi->secs_per_zone;
613 init = false;
614 goto find_other_zone;
615 }
616got_it:
617 /* set it as dirty segment in free segmap */
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900618 f2fs_bug_on(test_bit(segno, free_i->free_segmap));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900619 __set_inuse(sbi, segno);
620 *newseg = segno;
621 write_unlock(&free_i->segmap_lock);
622}
623
624static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
625{
626 struct curseg_info *curseg = CURSEG_I(sbi, type);
627 struct summary_footer *sum_footer;
628
629 curseg->segno = curseg->next_segno;
630 curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
631 curseg->next_blkoff = 0;
632 curseg->next_segno = NULL_SEGNO;
633
634 sum_footer = &(curseg->sum_blk->footer);
635 memset(sum_footer, 0, sizeof(struct summary_footer));
636 if (IS_DATASEG(type))
637 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
638 if (IS_NODESEG(type))
639 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
640 __set_sit_entry_type(sbi, type, curseg->segno, modified);
641}
642
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900643/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900644 * Allocate a current working segment.
645 * This function always allocates a free segment in LFS manner.
646 */
647static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
648{
649 struct curseg_info *curseg = CURSEG_I(sbi, type);
650 unsigned int segno = curseg->segno;
651 int dir = ALLOC_LEFT;
652
653 write_sum_page(sbi, curseg->sum_blk,
Haicheng Li81fb5e82013-05-14 18:20:28 +0800654 GET_SUM_BLOCK(sbi, segno));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900655 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
656 dir = ALLOC_RIGHT;
657
658 if (test_opt(sbi, NOHEAP))
659 dir = ALLOC_RIGHT;
660
661 get_new_segment(sbi, &segno, new_sec, dir);
662 curseg->next_segno = segno;
663 reset_curseg(sbi, type, 1);
664 curseg->alloc_type = LFS;
665}
666
667static void __next_free_blkoff(struct f2fs_sb_info *sbi,
668 struct curseg_info *seg, block_t start)
669{
670 struct seg_entry *se = get_seg_entry(sbi, seg->segno);
Changman Leee81c93c2013-11-15 13:21:16 +0900671 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
672 unsigned long target_map[entries];
673 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
674 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
675 int i, pos;
676
677 for (i = 0; i < entries; i++)
678 target_map[i] = ckpt_map[i] | cur_map[i];
679
680 pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
681
682 seg->next_blkoff = pos;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900683}
684
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900685/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900686 * If a segment is written by LFS manner, next block offset is just obtained
687 * by increasing the current block offset. However, if a segment is written by
688 * SSR manner, next block offset obtained by calling __next_free_blkoff
689 */
690static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
691 struct curseg_info *seg)
692{
693 if (seg->alloc_type == SSR)
694 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
695 else
696 seg->next_blkoff++;
697}
698
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900699/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900700 * This function always allocates a used segment (from dirty seglist) by SSR
701 * manner, so it should recover the existing segment information of valid blocks
702 */
703static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
704{
705 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
706 struct curseg_info *curseg = CURSEG_I(sbi, type);
707 unsigned int new_segno = curseg->next_segno;
708 struct f2fs_summary_block *sum_node;
709 struct page *sum_page;
710
711 write_sum_page(sbi, curseg->sum_blk,
712 GET_SUM_BLOCK(sbi, curseg->segno));
713 __set_test_and_inuse(sbi, new_segno);
714
715 mutex_lock(&dirty_i->seglist_lock);
716 __remove_dirty_segment(sbi, new_segno, PRE);
717 __remove_dirty_segment(sbi, new_segno, DIRTY);
718 mutex_unlock(&dirty_i->seglist_lock);
719
720 reset_curseg(sbi, type, 1);
721 curseg->alloc_type = SSR;
722 __next_free_blkoff(sbi, curseg, 0);
723
724 if (reuse) {
725 sum_page = get_sum_page(sbi, new_segno);
726 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
727 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
728 f2fs_put_page(sum_page, 1);
729 }
730}
731
Jaegeuk Kim43727522013-02-04 15:11:17 +0900732static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
733{
734 struct curseg_info *curseg = CURSEG_I(sbi, type);
735 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
736
737 if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0))
738 return v_ops->get_victim(sbi,
739 &(curseg)->next_segno, BG_GC, type, SSR);
740
741 /* For data segments, let's do SSR more intensively */
742 for (; type >= CURSEG_HOT_DATA; type--)
743 if (v_ops->get_victim(sbi, &(curseg)->next_segno,
744 BG_GC, type, SSR))
745 return 1;
746 return 0;
747}
748
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900749/*
750 * flush out current segment and replace it with new segment
751 * This function should be returned with success, otherwise BUG
752 */
753static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
754 int type, bool force)
755{
756 struct curseg_info *curseg = CURSEG_I(sbi, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900757
Gu Zheng7b405272013-08-19 09:41:15 +0800758 if (force)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900759 new_curseg(sbi, type, true);
Gu Zheng7b405272013-08-19 09:41:15 +0800760 else if (type == CURSEG_WARM_NODE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900761 new_curseg(sbi, type, false);
Jaegeuk Kim60374682013-03-31 13:58:51 +0900762 else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
763 new_curseg(sbi, type, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900764 else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
765 change_curseg(sbi, type, true);
766 else
767 new_curseg(sbi, type, false);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +0900768
769 stat_inc_seg_type(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900770}
771
772void allocate_new_segments(struct f2fs_sb_info *sbi)
773{
774 struct curseg_info *curseg;
775 unsigned int old_curseg;
776 int i;
777
778 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
779 curseg = CURSEG_I(sbi, i);
780 old_curseg = curseg->segno;
781 SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
782 locate_dirty_segment(sbi, old_curseg);
783 }
784}
785
786static const struct segment_allocation default_salloc_ops = {
787 .allocate_segment = allocate_segment_by_default,
788};
789
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900790static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
791{
792 struct curseg_info *curseg = CURSEG_I(sbi, type);
793 if (curseg->next_blkoff < sbi->blocks_per_seg)
794 return true;
795 return false;
796}
797
798static int __get_segment_type_2(struct page *page, enum page_type p_type)
799{
800 if (p_type == DATA)
801 return CURSEG_HOT_DATA;
802 else
803 return CURSEG_HOT_NODE;
804}
805
806static int __get_segment_type_4(struct page *page, enum page_type p_type)
807{
808 if (p_type == DATA) {
809 struct inode *inode = page->mapping->host;
810
811 if (S_ISDIR(inode->i_mode))
812 return CURSEG_HOT_DATA;
813 else
814 return CURSEG_COLD_DATA;
815 } else {
816 if (IS_DNODE(page) && !is_cold_node(page))
817 return CURSEG_HOT_NODE;
818 else
819 return CURSEG_COLD_NODE;
820 }
821}
822
823static int __get_segment_type_6(struct page *page, enum page_type p_type)
824{
825 if (p_type == DATA) {
826 struct inode *inode = page->mapping->host;
827
828 if (S_ISDIR(inode->i_mode))
829 return CURSEG_HOT_DATA;
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900830 else if (is_cold_data(page) || file_is_cold(inode))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900831 return CURSEG_COLD_DATA;
832 else
833 return CURSEG_WARM_DATA;
834 } else {
835 if (IS_DNODE(page))
836 return is_cold_node(page) ? CURSEG_WARM_NODE :
837 CURSEG_HOT_NODE;
838 else
839 return CURSEG_COLD_NODE;
840 }
841}
842
843static int __get_segment_type(struct page *page, enum page_type p_type)
844{
845 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
846 switch (sbi->active_logs) {
847 case 2:
848 return __get_segment_type_2(page, p_type);
849 case 4:
850 return __get_segment_type_4(page, p_type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900851 }
Jaegeuk Kim12a67142012-12-21 11:47:05 +0900852 /* NR_CURSEG_TYPE(6) logs by default */
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900853 f2fs_bug_on(sbi->active_logs != NR_CURSEG_TYPE);
Jaegeuk Kim12a67142012-12-21 11:47:05 +0900854 return __get_segment_type_6(page, p_type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900855}
856
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900857void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
858 block_t old_blkaddr, block_t *new_blkaddr,
859 struct f2fs_summary *sum, int type)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900860{
861 struct sit_info *sit_i = SIT_I(sbi);
862 struct curseg_info *curseg;
863 unsigned int old_cursegno;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900864
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900865 curseg = CURSEG_I(sbi, type);
866
867 mutex_lock(&curseg->curseg_mutex);
868
869 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
870 old_cursegno = curseg->segno;
871
872 /*
873 * __add_sum_entry should be resided under the curseg_mutex
874 * because, this function updates a summary entry in the
875 * current summary block.
876 */
Haicheng Lie79efe32013-06-13 16:59:27 +0800877 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900878
879 mutex_lock(&sit_i->sentry_lock);
880 __refresh_next_blkoff(sbi, curseg);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +0900881
882 stat_inc_block_count(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900883
884 /*
885 * SIT information should be updated before segment allocation,
886 * since SSR needs latest valid block information.
887 */
888 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
889
890 if (!__has_curseg_space(sbi, type))
891 sit_i->s_ops->allocate_segment(sbi, type, false);
892
893 locate_dirty_segment(sbi, old_cursegno);
894 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
895 mutex_unlock(&sit_i->sentry_lock);
896
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900897 if (page && IS_NODESEG(type))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900898 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
899
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900900 mutex_unlock(&curseg->curseg_mutex);
901}
902
903static void do_write_page(struct f2fs_sb_info *sbi, struct page *page,
904 block_t old_blkaddr, block_t *new_blkaddr,
905 struct f2fs_summary *sum, struct f2fs_io_info *fio)
906{
907 int type = __get_segment_type(page, fio->type);
908
909 allocate_data_block(sbi, page, old_blkaddr, new_blkaddr, sum, type);
910
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900911 /* writeout dirty page into bdev */
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900912 f2fs_submit_page_mbio(sbi, page, *new_blkaddr, fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900913}
914
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900915void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900916{
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900917 struct f2fs_io_info fio = {
918 .type = META,
Gu Zheng7e8f2302013-12-20 18:17:49 +0800919 .rw = WRITE_SYNC | REQ_META | REQ_PRIO
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900920 };
921
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900922 set_page_writeback(page);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900923 f2fs_submit_page_mbio(sbi, page, page->index, &fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900924}
925
926void write_node_page(struct f2fs_sb_info *sbi, struct page *page,
927 unsigned int nid, block_t old_blkaddr, block_t *new_blkaddr)
928{
929 struct f2fs_summary sum;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900930 struct f2fs_io_info fio = {
931 .type = NODE,
932 .rw = WRITE_SYNC,
Fan Li63a0b7cb2013-12-09 16:09:00 +0800933 };
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900934
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900935 set_summary(&sum, nid, 0, 0);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900936 do_write_page(sbi, page, old_blkaddr, new_blkaddr, &sum, &fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900937}
938
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900939void write_data_page(struct page *page, struct dnode_of_data *dn,
940 block_t *new_blkaddr, struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900941{
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900942 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900943 struct f2fs_summary sum;
944 struct node_info ni;
945
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900946 f2fs_bug_on(dn->data_blkaddr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900947 get_node_info(sbi, dn->nid, &ni);
948 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
949
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900950 do_write_page(sbi, page, dn->data_blkaddr, new_blkaddr, &sum, fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900951}
952
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900953void rewrite_data_page(struct page *page, block_t old_blkaddr, struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900954{
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900955 struct inode *inode = page->mapping->host;
956 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
957 f2fs_submit_page_mbio(sbi, page, old_blkaddr, fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900958}
959
960void recover_data_page(struct f2fs_sb_info *sbi,
961 struct page *page, struct f2fs_summary *sum,
962 block_t old_blkaddr, block_t new_blkaddr)
963{
964 struct sit_info *sit_i = SIT_I(sbi);
965 struct curseg_info *curseg;
966 unsigned int segno, old_cursegno;
967 struct seg_entry *se;
968 int type;
969
970 segno = GET_SEGNO(sbi, new_blkaddr);
971 se = get_seg_entry(sbi, segno);
972 type = se->type;
973
974 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
975 if (old_blkaddr == NULL_ADDR)
976 type = CURSEG_COLD_DATA;
977 else
978 type = CURSEG_WARM_DATA;
979 }
980 curseg = CURSEG_I(sbi, type);
981
982 mutex_lock(&curseg->curseg_mutex);
983 mutex_lock(&sit_i->sentry_lock);
984
985 old_cursegno = curseg->segno;
986
987 /* change the current segment */
988 if (segno != curseg->segno) {
989 curseg->next_segno = segno;
990 change_curseg(sbi, type, true);
991 }
992
993 curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, new_blkaddr) &
994 (sbi->blocks_per_seg - 1);
Haicheng Lie79efe32013-06-13 16:59:27 +0800995 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900996
997 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
998
999 locate_dirty_segment(sbi, old_cursegno);
1000 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
1001
1002 mutex_unlock(&sit_i->sentry_lock);
1003 mutex_unlock(&curseg->curseg_mutex);
1004}
1005
1006void rewrite_node_page(struct f2fs_sb_info *sbi,
1007 struct page *page, struct f2fs_summary *sum,
1008 block_t old_blkaddr, block_t new_blkaddr)
1009{
1010 struct sit_info *sit_i = SIT_I(sbi);
1011 int type = CURSEG_WARM_NODE;
1012 struct curseg_info *curseg;
1013 unsigned int segno, old_cursegno;
1014 block_t next_blkaddr = next_blkaddr_of_node(page);
1015 unsigned int next_segno = GET_SEGNO(sbi, next_blkaddr);
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001016 struct f2fs_io_info fio = {
1017 .type = NODE,
1018 .rw = WRITE_SYNC,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001019 };
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001020
1021 curseg = CURSEG_I(sbi, type);
1022
1023 mutex_lock(&curseg->curseg_mutex);
1024 mutex_lock(&sit_i->sentry_lock);
1025
1026 segno = GET_SEGNO(sbi, new_blkaddr);
1027 old_cursegno = curseg->segno;
1028
1029 /* change the current segment */
1030 if (segno != curseg->segno) {
1031 curseg->next_segno = segno;
1032 change_curseg(sbi, type, true);
1033 }
1034 curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, new_blkaddr) &
1035 (sbi->blocks_per_seg - 1);
Haicheng Lie79efe32013-06-13 16:59:27 +08001036 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001037
1038 /* change the current log to the next block addr in advance */
1039 if (next_segno != segno) {
1040 curseg->next_segno = next_segno;
1041 change_curseg(sbi, type, true);
1042 }
1043 curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, next_blkaddr) &
1044 (sbi->blocks_per_seg - 1);
1045
1046 /* rewrite node page */
1047 set_page_writeback(page);
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001048 f2fs_submit_page_mbio(sbi, page, new_blkaddr, &fio);
1049 f2fs_submit_merged_bio(sbi, NODE, WRITE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001050 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
1051
1052 locate_dirty_segment(sbi, old_cursegno);
1053 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
1054
1055 mutex_unlock(&sit_i->sentry_lock);
1056 mutex_unlock(&curseg->curseg_mutex);
1057}
1058
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001059void f2fs_wait_on_page_writeback(struct page *page,
1060 enum page_type type, bool sync)
1061{
1062 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
1063 if (PageWriteback(page)) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001064 f2fs_submit_merged_bio(sbi, type, WRITE);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001065 wait_on_page_writeback(page);
1066 }
1067}
1068
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001069static int read_compacted_summaries(struct f2fs_sb_info *sbi)
1070{
1071 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1072 struct curseg_info *seg_i;
1073 unsigned char *kaddr;
1074 struct page *page;
1075 block_t start;
1076 int i, j, offset;
1077
1078 start = start_sum_block(sbi);
1079
1080 page = get_meta_page(sbi, start++);
1081 kaddr = (unsigned char *)page_address(page);
1082
1083 /* Step 1: restore nat cache */
1084 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1085 memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
1086
1087 /* Step 2: restore sit cache */
1088 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1089 memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
1090 SUM_JOURNAL_SIZE);
1091 offset = 2 * SUM_JOURNAL_SIZE;
1092
1093 /* Step 3: restore summary entries */
1094 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1095 unsigned short blk_off;
1096 unsigned int segno;
1097
1098 seg_i = CURSEG_I(sbi, i);
1099 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
1100 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
1101 seg_i->next_segno = segno;
1102 reset_curseg(sbi, i, 0);
1103 seg_i->alloc_type = ckpt->alloc_type[i];
1104 seg_i->next_blkoff = blk_off;
1105
1106 if (seg_i->alloc_type == SSR)
1107 blk_off = sbi->blocks_per_seg;
1108
1109 for (j = 0; j < blk_off; j++) {
1110 struct f2fs_summary *s;
1111 s = (struct f2fs_summary *)(kaddr + offset);
1112 seg_i->sum_blk->entries[j] = *s;
1113 offset += SUMMARY_SIZE;
1114 if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1115 SUM_FOOTER_SIZE)
1116 continue;
1117
1118 f2fs_put_page(page, 1);
1119 page = NULL;
1120
1121 page = get_meta_page(sbi, start++);
1122 kaddr = (unsigned char *)page_address(page);
1123 offset = 0;
1124 }
1125 }
1126 f2fs_put_page(page, 1);
1127 return 0;
1128}
1129
1130static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
1131{
1132 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1133 struct f2fs_summary_block *sum;
1134 struct curseg_info *curseg;
1135 struct page *new;
1136 unsigned short blk_off;
1137 unsigned int segno = 0;
1138 block_t blk_addr = 0;
1139
1140 /* get segment number and block addr */
1141 if (IS_DATASEG(type)) {
1142 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
1143 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
1144 CURSEG_HOT_DATA]);
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001145 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001146 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
1147 else
1148 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
1149 } else {
1150 segno = le32_to_cpu(ckpt->cur_node_segno[type -
1151 CURSEG_HOT_NODE]);
1152 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
1153 CURSEG_HOT_NODE]);
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001154 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001155 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
1156 type - CURSEG_HOT_NODE);
1157 else
1158 blk_addr = GET_SUM_BLOCK(sbi, segno);
1159 }
1160
1161 new = get_meta_page(sbi, blk_addr);
1162 sum = (struct f2fs_summary_block *)page_address(new);
1163
1164 if (IS_NODESEG(type)) {
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001165 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001166 struct f2fs_summary *ns = &sum->entries[0];
1167 int i;
1168 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
1169 ns->version = 0;
1170 ns->ofs_in_node = 0;
1171 }
1172 } else {
1173 if (restore_node_summary(sbi, segno, sum)) {
1174 f2fs_put_page(new, 1);
1175 return -EINVAL;
1176 }
1177 }
1178 }
1179
1180 /* set uncompleted segment to curseg */
1181 curseg = CURSEG_I(sbi, type);
1182 mutex_lock(&curseg->curseg_mutex);
1183 memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
1184 curseg->next_segno = segno;
1185 reset_curseg(sbi, type, 0);
1186 curseg->alloc_type = ckpt->alloc_type[type];
1187 curseg->next_blkoff = blk_off;
1188 mutex_unlock(&curseg->curseg_mutex);
1189 f2fs_put_page(new, 1);
1190 return 0;
1191}
1192
1193static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
1194{
1195 int type = CURSEG_HOT_DATA;
1196
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001197 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001198 /* restore for compacted data summary */
1199 if (read_compacted_summaries(sbi))
1200 return -EINVAL;
1201 type = CURSEG_HOT_NODE;
1202 }
1203
1204 for (; type <= CURSEG_COLD_NODE; type++)
1205 if (read_normal_summaries(sbi, type))
1206 return -EINVAL;
1207 return 0;
1208}
1209
1210static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
1211{
1212 struct page *page;
1213 unsigned char *kaddr;
1214 struct f2fs_summary *summary;
1215 struct curseg_info *seg_i;
1216 int written_size = 0;
1217 int i, j;
1218
1219 page = grab_meta_page(sbi, blkaddr++);
1220 kaddr = (unsigned char *)page_address(page);
1221
1222 /* Step 1: write nat cache */
1223 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1224 memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
1225 written_size += SUM_JOURNAL_SIZE;
1226
1227 /* Step 2: write sit cache */
1228 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1229 memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
1230 SUM_JOURNAL_SIZE);
1231 written_size += SUM_JOURNAL_SIZE;
1232
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001233 /* Step 3: write summary entries */
1234 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1235 unsigned short blkoff;
1236 seg_i = CURSEG_I(sbi, i);
1237 if (sbi->ckpt->alloc_type[i] == SSR)
1238 blkoff = sbi->blocks_per_seg;
1239 else
1240 blkoff = curseg_blkoff(sbi, i);
1241
1242 for (j = 0; j < blkoff; j++) {
1243 if (!page) {
1244 page = grab_meta_page(sbi, blkaddr++);
1245 kaddr = (unsigned char *)page_address(page);
1246 written_size = 0;
1247 }
1248 summary = (struct f2fs_summary *)(kaddr + written_size);
1249 *summary = seg_i->sum_blk->entries[j];
1250 written_size += SUMMARY_SIZE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001251
1252 if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1253 SUM_FOOTER_SIZE)
1254 continue;
1255
Chao Yue8d61a72013-10-24 15:08:28 +08001256 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001257 f2fs_put_page(page, 1);
1258 page = NULL;
1259 }
1260 }
Chao Yue8d61a72013-10-24 15:08:28 +08001261 if (page) {
1262 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001263 f2fs_put_page(page, 1);
Chao Yue8d61a72013-10-24 15:08:28 +08001264 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001265}
1266
1267static void write_normal_summaries(struct f2fs_sb_info *sbi,
1268 block_t blkaddr, int type)
1269{
1270 int i, end;
1271 if (IS_DATASEG(type))
1272 end = type + NR_CURSEG_DATA_TYPE;
1273 else
1274 end = type + NR_CURSEG_NODE_TYPE;
1275
1276 for (i = type; i < end; i++) {
1277 struct curseg_info *sum = CURSEG_I(sbi, i);
1278 mutex_lock(&sum->curseg_mutex);
1279 write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
1280 mutex_unlock(&sum->curseg_mutex);
1281 }
1282}
1283
1284void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1285{
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001286 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001287 write_compacted_summaries(sbi, start_blk);
1288 else
1289 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
1290}
1291
1292void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1293{
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001294 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001295 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001296}
1297
1298int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
1299 unsigned int val, int alloc)
1300{
1301 int i;
1302
1303 if (type == NAT_JOURNAL) {
1304 for (i = 0; i < nats_in_cursum(sum); i++) {
1305 if (le32_to_cpu(nid_in_journal(sum, i)) == val)
1306 return i;
1307 }
1308 if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
1309 return update_nats_in_cursum(sum, 1);
1310 } else if (type == SIT_JOURNAL) {
1311 for (i = 0; i < sits_in_cursum(sum); i++)
1312 if (le32_to_cpu(segno_in_journal(sum, i)) == val)
1313 return i;
1314 if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
1315 return update_sits_in_cursum(sum, 1);
1316 }
1317 return -1;
1318}
1319
1320static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
1321 unsigned int segno)
1322{
1323 struct sit_info *sit_i = SIT_I(sbi);
1324 unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno);
1325 block_t blk_addr = sit_i->sit_base_addr + offset;
1326
1327 check_seg_range(sbi, segno);
1328
1329 /* calculate sit block address */
1330 if (f2fs_test_bit(offset, sit_i->sit_bitmap))
1331 blk_addr += sit_i->sit_blocks;
1332
1333 return get_meta_page(sbi, blk_addr);
1334}
1335
1336static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
1337 unsigned int start)
1338{
1339 struct sit_info *sit_i = SIT_I(sbi);
1340 struct page *src_page, *dst_page;
1341 pgoff_t src_off, dst_off;
1342 void *src_addr, *dst_addr;
1343
1344 src_off = current_sit_addr(sbi, start);
1345 dst_off = next_sit_addr(sbi, src_off);
1346
1347 /* get current sit block page without lock */
1348 src_page = get_meta_page(sbi, src_off);
1349 dst_page = grab_meta_page(sbi, dst_off);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001350 f2fs_bug_on(PageDirty(src_page));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001351
1352 src_addr = page_address(src_page);
1353 dst_addr = page_address(dst_page);
1354 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
1355
1356 set_page_dirty(dst_page);
1357 f2fs_put_page(src_page, 1);
1358
1359 set_to_next_sit(sit_i, start);
1360
1361 return dst_page;
1362}
1363
1364static bool flush_sits_in_journal(struct f2fs_sb_info *sbi)
1365{
1366 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1367 struct f2fs_summary_block *sum = curseg->sum_blk;
1368 int i;
1369
1370 /*
1371 * If the journal area in the current summary is full of sit entries,
1372 * all the sit entries will be flushed. Otherwise the sit entries
1373 * are not able to replace with newly hot sit entries.
1374 */
1375 if (sits_in_cursum(sum) >= SIT_JOURNAL_ENTRIES) {
1376 for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
1377 unsigned int segno;
1378 segno = le32_to_cpu(segno_in_journal(sum, i));
1379 __mark_sit_entry_dirty(sbi, segno);
1380 }
1381 update_sits_in_cursum(sum, -sits_in_cursum(sum));
Haicheng Licffbfa62013-10-18 17:24:07 +08001382 return true;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001383 }
Haicheng Licffbfa62013-10-18 17:24:07 +08001384 return false;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001385}
1386
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001387/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001388 * CP calls this function, which flushes SIT entries including sit_journal,
1389 * and moves prefree segs to free segs.
1390 */
1391void flush_sit_entries(struct f2fs_sb_info *sbi)
1392{
1393 struct sit_info *sit_i = SIT_I(sbi);
1394 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
1395 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1396 struct f2fs_summary_block *sum = curseg->sum_blk;
1397 unsigned long nsegs = TOTAL_SEGS(sbi);
1398 struct page *page = NULL;
1399 struct f2fs_sit_block *raw_sit = NULL;
1400 unsigned int start = 0, end = 0;
1401 unsigned int segno = -1;
1402 bool flushed;
1403
1404 mutex_lock(&curseg->curseg_mutex);
1405 mutex_lock(&sit_i->sentry_lock);
1406
1407 /*
1408 * "flushed" indicates whether sit entries in journal are flushed
1409 * to the SIT area or not.
1410 */
1411 flushed = flush_sits_in_journal(sbi);
1412
1413 while ((segno = find_next_bit(bitmap, nsegs, segno + 1)) < nsegs) {
1414 struct seg_entry *se = get_seg_entry(sbi, segno);
1415 int sit_offset, offset;
1416
1417 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
1418
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001419 /* add discard candidates */
1420 if (SM_I(sbi)->nr_discards < SM_I(sbi)->max_discards)
1421 add_discard_addrs(sbi, segno, se);
1422
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001423 if (flushed)
1424 goto to_sit_page;
1425
1426 offset = lookup_journal_in_cursum(sum, SIT_JOURNAL, segno, 1);
1427 if (offset >= 0) {
1428 segno_in_journal(sum, offset) = cpu_to_le32(segno);
1429 seg_info_to_raw_sit(se, &sit_in_journal(sum, offset));
1430 goto flush_done;
1431 }
1432to_sit_page:
1433 if (!page || (start > segno) || (segno > end)) {
1434 if (page) {
1435 f2fs_put_page(page, 1);
1436 page = NULL;
1437 }
1438
1439 start = START_SEGNO(sit_i, segno);
1440 end = start + SIT_ENTRY_PER_BLOCK - 1;
1441
1442 /* read sit block that will be updated */
1443 page = get_next_sit_page(sbi, start);
1444 raw_sit = page_address(page);
1445 }
1446
1447 /* udpate entry in SIT block */
1448 seg_info_to_raw_sit(se, &raw_sit->entries[sit_offset]);
1449flush_done:
1450 __clear_bit(segno, bitmap);
1451 sit_i->dirty_sentries--;
1452 }
1453 mutex_unlock(&sit_i->sentry_lock);
1454 mutex_unlock(&curseg->curseg_mutex);
1455
1456 /* writeout last modified SIT block */
1457 f2fs_put_page(page, 1);
1458
1459 set_prefree_as_free_segments(sbi);
1460}
1461
1462static int build_sit_info(struct f2fs_sb_info *sbi)
1463{
1464 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1465 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1466 struct sit_info *sit_i;
1467 unsigned int sit_segs, start;
1468 char *src_bitmap, *dst_bitmap;
1469 unsigned int bitmap_size;
1470
1471 /* allocate memory for SIT information */
1472 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
1473 if (!sit_i)
1474 return -ENOMEM;
1475
1476 SM_I(sbi)->sit_info = sit_i;
1477
1478 sit_i->sentries = vzalloc(TOTAL_SEGS(sbi) * sizeof(struct seg_entry));
1479 if (!sit_i->sentries)
1480 return -ENOMEM;
1481
1482 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1483 sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1484 if (!sit_i->dirty_sentries_bitmap)
1485 return -ENOMEM;
1486
1487 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1488 sit_i->sentries[start].cur_valid_map
1489 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1490 sit_i->sentries[start].ckpt_valid_map
1491 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1492 if (!sit_i->sentries[start].cur_valid_map
1493 || !sit_i->sentries[start].ckpt_valid_map)
1494 return -ENOMEM;
1495 }
1496
1497 if (sbi->segs_per_sec > 1) {
Jaegeuk Kim53cf9522013-03-31 12:39:49 +09001498 sit_i->sec_entries = vzalloc(TOTAL_SECS(sbi) *
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001499 sizeof(struct sec_entry));
1500 if (!sit_i->sec_entries)
1501 return -ENOMEM;
1502 }
1503
1504 /* get information related with SIT */
1505 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
1506
1507 /* setup SIT bitmap from ckeckpoint pack */
1508 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
1509 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
1510
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001511 dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001512 if (!dst_bitmap)
1513 return -ENOMEM;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001514
1515 /* init SIT information */
1516 sit_i->s_ops = &default_salloc_ops;
1517
1518 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
1519 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
1520 sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
1521 sit_i->sit_bitmap = dst_bitmap;
1522 sit_i->bitmap_size = bitmap_size;
1523 sit_i->dirty_sentries = 0;
1524 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
1525 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
1526 sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
1527 mutex_init(&sit_i->sentry_lock);
1528 return 0;
1529}
1530
1531static int build_free_segmap(struct f2fs_sb_info *sbi)
1532{
1533 struct f2fs_sm_info *sm_info = SM_I(sbi);
1534 struct free_segmap_info *free_i;
1535 unsigned int bitmap_size, sec_bitmap_size;
1536
1537 /* allocate memory for free segmap information */
1538 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
1539 if (!free_i)
1540 return -ENOMEM;
1541
1542 SM_I(sbi)->free_info = free_i;
1543
1544 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1545 free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL);
1546 if (!free_i->free_segmap)
1547 return -ENOMEM;
1548
Jaegeuk Kim53cf9522013-03-31 12:39:49 +09001549 sec_bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001550 free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL);
1551 if (!free_i->free_secmap)
1552 return -ENOMEM;
1553
1554 /* set all segments as dirty temporarily */
1555 memset(free_i->free_segmap, 0xff, bitmap_size);
1556 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
1557
1558 /* init free segmap information */
1559 free_i->start_segno =
1560 (unsigned int) GET_SEGNO_FROM_SEG0(sbi, sm_info->main_blkaddr);
1561 free_i->free_segments = 0;
1562 free_i->free_sections = 0;
1563 rwlock_init(&free_i->segmap_lock);
1564 return 0;
1565}
1566
1567static int build_curseg(struct f2fs_sb_info *sbi)
1568{
Namjae Jeon1042d602012-12-01 10:56:13 +09001569 struct curseg_info *array;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001570 int i;
1571
1572 array = kzalloc(sizeof(*array) * NR_CURSEG_TYPE, GFP_KERNEL);
1573 if (!array)
1574 return -ENOMEM;
1575
1576 SM_I(sbi)->curseg_array = array;
1577
1578 for (i = 0; i < NR_CURSEG_TYPE; i++) {
1579 mutex_init(&array[i].curseg_mutex);
1580 array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
1581 if (!array[i].sum_blk)
1582 return -ENOMEM;
1583 array[i].segno = NULL_SEGNO;
1584 array[i].next_blkoff = 0;
1585 }
1586 return restore_curseg_summaries(sbi);
1587}
1588
Chao Yu74de5932013-11-22 09:09:59 +08001589static int ra_sit_pages(struct f2fs_sb_info *sbi, int start, int nrpages)
1590{
1591 struct address_space *mapping = sbi->meta_inode->i_mapping;
1592 struct page *page;
1593 block_t blk_addr, prev_blk_addr = 0;
1594 int sit_blk_cnt = SIT_BLK_CNT(sbi);
1595 int blkno = start;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001596 struct f2fs_io_info fio = {
1597 .type = META,
Gu Zheng7e8f2302013-12-20 18:17:49 +08001598 .rw = READ_SYNC | REQ_META | REQ_PRIO
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001599 };
Chao Yu74de5932013-11-22 09:09:59 +08001600
1601 for (; blkno < start + nrpages && blkno < sit_blk_cnt; blkno++) {
1602
1603 blk_addr = current_sit_addr(sbi, blkno * SIT_ENTRY_PER_BLOCK);
1604
1605 if (blkno != start && prev_blk_addr + 1 != blk_addr)
1606 break;
1607 prev_blk_addr = blk_addr;
1608repeat:
1609 page = grab_cache_page(mapping, blk_addr);
1610 if (!page) {
1611 cond_resched();
1612 goto repeat;
1613 }
1614 if (PageUptodate(page)) {
1615 mark_page_accessed(page);
1616 f2fs_put_page(page, 1);
1617 continue;
1618 }
1619
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001620 f2fs_submit_page_mbio(sbi, page, blk_addr, &fio);
Chao Yu74de5932013-11-22 09:09:59 +08001621
1622 mark_page_accessed(page);
1623 f2fs_put_page(page, 0);
1624 }
1625
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001626 f2fs_submit_merged_bio(sbi, META, READ);
Chao Yu74de5932013-11-22 09:09:59 +08001627 return blkno - start;
1628}
1629
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001630static void build_sit_entries(struct f2fs_sb_info *sbi)
1631{
1632 struct sit_info *sit_i = SIT_I(sbi);
1633 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1634 struct f2fs_summary_block *sum = curseg->sum_blk;
Chao Yu74de5932013-11-22 09:09:59 +08001635 int sit_blk_cnt = SIT_BLK_CNT(sbi);
1636 unsigned int i, start, end;
1637 unsigned int readed, start_blk = 0;
1638 int nrpages = MAX_BIO_BLOCKS(max_hw_blocks(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001639
Chao Yu74de5932013-11-22 09:09:59 +08001640 do {
1641 readed = ra_sit_pages(sbi, start_blk, nrpages);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001642
Chao Yu74de5932013-11-22 09:09:59 +08001643 start = start_blk * sit_i->sents_per_block;
1644 end = (start_blk + readed) * sit_i->sents_per_block;
1645
1646 for (; start < end && start < TOTAL_SEGS(sbi); start++) {
1647 struct seg_entry *se = &sit_i->sentries[start];
1648 struct f2fs_sit_block *sit_blk;
1649 struct f2fs_sit_entry sit;
1650 struct page *page;
1651
1652 mutex_lock(&curseg->curseg_mutex);
1653 for (i = 0; i < sits_in_cursum(sum); i++) {
1654 if (le32_to_cpu(segno_in_journal(sum, i)) == start) {
1655 sit = sit_in_journal(sum, i);
1656 mutex_unlock(&curseg->curseg_mutex);
1657 goto got_it;
1658 }
1659 }
1660 mutex_unlock(&curseg->curseg_mutex);
1661
1662 page = get_current_sit_page(sbi, start);
1663 sit_blk = (struct f2fs_sit_block *)page_address(page);
1664 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
1665 f2fs_put_page(page, 1);
1666got_it:
1667 check_block_count(sbi, start, &sit);
1668 seg_info_from_raw_sit(se, &sit);
1669 if (sbi->segs_per_sec > 1) {
1670 struct sec_entry *e = get_sec_entry(sbi, start);
1671 e->valid_blocks += se->valid_blocks;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001672 }
1673 }
Chao Yu74de5932013-11-22 09:09:59 +08001674 start_blk += readed;
1675 } while (start_blk < sit_blk_cnt);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001676}
1677
1678static void init_free_segmap(struct f2fs_sb_info *sbi)
1679{
1680 unsigned int start;
1681 int type;
1682
1683 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1684 struct seg_entry *sentry = get_seg_entry(sbi, start);
1685 if (!sentry->valid_blocks)
1686 __set_free(sbi, start);
1687 }
1688
1689 /* set use the current segments */
1690 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
1691 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
1692 __set_test_and_inuse(sbi, curseg_t->segno);
1693 }
1694}
1695
1696static void init_dirty_segmap(struct f2fs_sb_info *sbi)
1697{
1698 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1699 struct free_segmap_info *free_i = FREE_I(sbi);
Namjae Jeon8736fbf2013-06-16 09:49:11 +09001700 unsigned int segno = 0, offset = 0, total_segs = TOTAL_SEGS(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001701 unsigned short valid_blocks;
1702
Namjae Jeon8736fbf2013-06-16 09:49:11 +09001703 while (1) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001704 /* find dirty segment based on free segmap */
Namjae Jeon8736fbf2013-06-16 09:49:11 +09001705 segno = find_next_inuse(free_i, total_segs, offset);
1706 if (segno >= total_segs)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001707 break;
1708 offset = segno + 1;
1709 valid_blocks = get_valid_blocks(sbi, segno, 0);
1710 if (valid_blocks >= sbi->blocks_per_seg || !valid_blocks)
1711 continue;
1712 mutex_lock(&dirty_i->seglist_lock);
1713 __locate_dirty_segment(sbi, segno, DIRTY);
1714 mutex_unlock(&dirty_i->seglist_lock);
1715 }
1716}
1717
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001718static int init_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001719{
1720 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001721 unsigned int bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001722
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001723 dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL);
1724 if (!dirty_i->victim_secmap)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001725 return -ENOMEM;
1726 return 0;
1727}
1728
1729static int build_dirty_segmap(struct f2fs_sb_info *sbi)
1730{
1731 struct dirty_seglist_info *dirty_i;
1732 unsigned int bitmap_size, i;
1733
1734 /* allocate memory for dirty segments list information */
1735 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
1736 if (!dirty_i)
1737 return -ENOMEM;
1738
1739 SM_I(sbi)->dirty_info = dirty_i;
1740 mutex_init(&dirty_i->seglist_lock);
1741
1742 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1743
1744 for (i = 0; i < NR_DIRTY_TYPE; i++) {
1745 dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001746 if (!dirty_i->dirty_segmap[i])
1747 return -ENOMEM;
1748 }
1749
1750 init_dirty_segmap(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001751 return init_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001752}
1753
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001754/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001755 * Update min, max modified time for cost-benefit GC algorithm
1756 */
1757static void init_min_max_mtime(struct f2fs_sb_info *sbi)
1758{
1759 struct sit_info *sit_i = SIT_I(sbi);
1760 unsigned int segno;
1761
1762 mutex_lock(&sit_i->sentry_lock);
1763
1764 sit_i->min_mtime = LLONG_MAX;
1765
1766 for (segno = 0; segno < TOTAL_SEGS(sbi); segno += sbi->segs_per_sec) {
1767 unsigned int i;
1768 unsigned long long mtime = 0;
1769
1770 for (i = 0; i < sbi->segs_per_sec; i++)
1771 mtime += get_seg_entry(sbi, segno + i)->mtime;
1772
1773 mtime = div_u64(mtime, sbi->segs_per_sec);
1774
1775 if (sit_i->min_mtime > mtime)
1776 sit_i->min_mtime = mtime;
1777 }
1778 sit_i->max_mtime = get_mtime(sbi);
1779 mutex_unlock(&sit_i->sentry_lock);
1780}
1781
1782int build_segment_manager(struct f2fs_sb_info *sbi)
1783{
1784 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1785 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Namjae Jeon1042d602012-12-01 10:56:13 +09001786 struct f2fs_sm_info *sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001787 int err;
1788
1789 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
1790 if (!sm_info)
1791 return -ENOMEM;
1792
1793 /* init sm info */
1794 sbi->sm_info = sm_info;
1795 INIT_LIST_HEAD(&sm_info->wblist_head);
1796 spin_lock_init(&sm_info->wblist_lock);
1797 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
1798 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
1799 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
1800 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
1801 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
1802 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
1803 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
Jaegeuk Kim81eb8d62013-10-24 13:31:34 +09001804 sm_info->rec_prefree_segments = DEF_RECLAIM_PREFREE_SEGMENTS;
Jaegeuk Kim216fbd62013-11-07 13:13:42 +09001805 sm_info->ipu_policy = F2FS_IPU_DISABLE;
1806 sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001807
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09001808 INIT_LIST_HEAD(&sm_info->discard_list);
1809 sm_info->nr_discards = 0;
1810 sm_info->max_discards = 0;
1811
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001812 err = build_sit_info(sbi);
1813 if (err)
1814 return err;
1815 err = build_free_segmap(sbi);
1816 if (err)
1817 return err;
1818 err = build_curseg(sbi);
1819 if (err)
1820 return err;
1821
1822 /* reinit free segmap based on SIT */
1823 build_sit_entries(sbi);
1824
1825 init_free_segmap(sbi);
1826 err = build_dirty_segmap(sbi);
1827 if (err)
1828 return err;
1829
1830 init_min_max_mtime(sbi);
1831 return 0;
1832}
1833
1834static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
1835 enum dirty_type dirty_type)
1836{
1837 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1838
1839 mutex_lock(&dirty_i->seglist_lock);
1840 kfree(dirty_i->dirty_segmap[dirty_type]);
1841 dirty_i->nr_dirty[dirty_type] = 0;
1842 mutex_unlock(&dirty_i->seglist_lock);
1843}
1844
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001845static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001846{
1847 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001848 kfree(dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001849}
1850
1851static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
1852{
1853 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1854 int i;
1855
1856 if (!dirty_i)
1857 return;
1858
1859 /* discard pre-free/dirty segments list */
1860 for (i = 0; i < NR_DIRTY_TYPE; i++)
1861 discard_dirty_segmap(sbi, i);
1862
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001863 destroy_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001864 SM_I(sbi)->dirty_info = NULL;
1865 kfree(dirty_i);
1866}
1867
1868static void destroy_curseg(struct f2fs_sb_info *sbi)
1869{
1870 struct curseg_info *array = SM_I(sbi)->curseg_array;
1871 int i;
1872
1873 if (!array)
1874 return;
1875 SM_I(sbi)->curseg_array = NULL;
1876 for (i = 0; i < NR_CURSEG_TYPE; i++)
1877 kfree(array[i].sum_blk);
1878 kfree(array);
1879}
1880
1881static void destroy_free_segmap(struct f2fs_sb_info *sbi)
1882{
1883 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
1884 if (!free_i)
1885 return;
1886 SM_I(sbi)->free_info = NULL;
1887 kfree(free_i->free_segmap);
1888 kfree(free_i->free_secmap);
1889 kfree(free_i);
1890}
1891
1892static void destroy_sit_info(struct f2fs_sb_info *sbi)
1893{
1894 struct sit_info *sit_i = SIT_I(sbi);
1895 unsigned int start;
1896
1897 if (!sit_i)
1898 return;
1899
1900 if (sit_i->sentries) {
1901 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1902 kfree(sit_i->sentries[start].cur_valid_map);
1903 kfree(sit_i->sentries[start].ckpt_valid_map);
1904 }
1905 }
1906 vfree(sit_i->sentries);
1907 vfree(sit_i->sec_entries);
1908 kfree(sit_i->dirty_sentries_bitmap);
1909
1910 SM_I(sbi)->sit_info = NULL;
1911 kfree(sit_i->sit_bitmap);
1912 kfree(sit_i);
1913}
1914
1915void destroy_segment_manager(struct f2fs_sb_info *sbi)
1916{
1917 struct f2fs_sm_info *sm_info = SM_I(sbi);
Chao Yu3b03f722013-11-06 09:12:04 +08001918 if (!sm_info)
1919 return;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001920 destroy_dirty_segmap(sbi);
1921 destroy_curseg(sbi);
1922 destroy_free_segmap(sbi);
1923 destroy_sit_info(sbi);
1924 sbi->sm_info = NULL;
1925 kfree(sm_info);
1926}
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09001927
1928int __init create_segment_manager_caches(void)
1929{
1930 discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
1931 sizeof(struct discard_entry), NULL);
1932 if (!discard_entry_slab)
1933 return -ENOMEM;
1934 return 0;
1935}
1936
1937void destroy_segment_manager_caches(void)
1938{
1939 kmem_cache_destroy(discard_entry_slab);
1940}