blob: 505a8894cfa1424ac38523cb60fe49bee1783182 [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
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +090025static struct kmem_cache *discard_entry_slab;
26
Changman Lee9a7f1432013-11-15 10:42:51 +090027/*
28 * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since
29 * MSB and LSB are reversed in a byte by f2fs_set_bit.
30 */
31static inline unsigned long __reverse_ffs(unsigned long word)
32{
33 int num = 0;
34
35#if BITS_PER_LONG == 64
36 if ((word & 0xffffffff) == 0) {
37 num += 32;
38 word >>= 32;
39 }
40#endif
41 if ((word & 0xffff) == 0) {
42 num += 16;
43 word >>= 16;
44 }
45 if ((word & 0xff) == 0) {
46 num += 8;
47 word >>= 8;
48 }
49 if ((word & 0xf0) == 0)
50 num += 4;
51 else
52 word >>= 4;
53 if ((word & 0xc) == 0)
54 num += 2;
55 else
56 word >>= 2;
57 if ((word & 0x2) == 0)
58 num += 1;
59 return num;
60}
61
62/*
63 * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c becasue
64 * f2fs_set_bit makes MSB and LSB reversed in a byte.
65 * Example:
66 * LSB <--> MSB
67 * f2fs_set_bit(0, bitmap) => 0000 0001
68 * f2fs_set_bit(7, bitmap) => 1000 0000
69 */
70static unsigned long __find_rev_next_bit(const unsigned long *addr,
71 unsigned long size, unsigned long offset)
72{
73 const unsigned long *p = addr + BIT_WORD(offset);
74 unsigned long result = offset & ~(BITS_PER_LONG - 1);
75 unsigned long tmp;
76 unsigned long mask, submask;
77 unsigned long quot, rest;
78
79 if (offset >= size)
80 return size;
81
82 size -= result;
83 offset %= BITS_PER_LONG;
84 if (!offset)
85 goto aligned;
86
87 tmp = *(p++);
88 quot = (offset >> 3) << 3;
89 rest = offset & 0x7;
90 mask = ~0UL << quot;
91 submask = (unsigned char)(0xff << rest) >> rest;
92 submask <<= quot;
93 mask &= submask;
94 tmp &= mask;
95 if (size < BITS_PER_LONG)
96 goto found_first;
97 if (tmp)
98 goto found_middle;
99
100 size -= BITS_PER_LONG;
101 result += BITS_PER_LONG;
102aligned:
103 while (size & ~(BITS_PER_LONG-1)) {
104 tmp = *(p++);
105 if (tmp)
106 goto found_middle;
107 result += BITS_PER_LONG;
108 size -= BITS_PER_LONG;
109 }
110 if (!size)
111 return result;
112 tmp = *p;
113found_first:
114 tmp &= (~0UL >> (BITS_PER_LONG - size));
115 if (tmp == 0UL) /* Are any bits set? */
116 return result + size; /* Nope. */
117found_middle:
118 return result + __reverse_ffs(tmp);
119}
120
121static unsigned long __find_rev_next_zero_bit(const unsigned long *addr,
122 unsigned long size, unsigned long offset)
123{
124 const unsigned long *p = addr + BIT_WORD(offset);
125 unsigned long result = offset & ~(BITS_PER_LONG - 1);
126 unsigned long tmp;
127 unsigned long mask, submask;
128 unsigned long quot, rest;
129
130 if (offset >= size)
131 return size;
132
133 size -= result;
134 offset %= BITS_PER_LONG;
135 if (!offset)
136 goto aligned;
137
138 tmp = *(p++);
139 quot = (offset >> 3) << 3;
140 rest = offset & 0x7;
141 mask = ~(~0UL << quot);
142 submask = (unsigned char)~((unsigned char)(0xff << rest) >> rest);
143 submask <<= quot;
144 mask += submask;
145 tmp |= mask;
146 if (size < BITS_PER_LONG)
147 goto found_first;
148 if (~tmp)
149 goto found_middle;
150
151 size -= BITS_PER_LONG;
152 result += BITS_PER_LONG;
153aligned:
154 while (size & ~(BITS_PER_LONG - 1)) {
155 tmp = *(p++);
156 if (~tmp)
157 goto found_middle;
158 result += BITS_PER_LONG;
159 size -= BITS_PER_LONG;
160 }
161 if (!size)
162 return result;
163 tmp = *p;
164
165found_first:
166 tmp |= ~0UL << size;
167 if (tmp == ~0UL) /* Are any bits zero? */
168 return result + size; /* Nope. */
169found_middle:
170 return result + __reverse_ffz(tmp);
171}
172
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900173/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900174 * This function balances dirty node and dentry pages.
175 * In addition, it controls garbage collection.
176 */
177void f2fs_balance_fs(struct f2fs_sb_info *sbi)
178{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900179 /*
Jaegeuk Kim029cd282012-12-21 17:20:21 +0900180 * We should do GC or end up with checkpoint, if there are so many dirty
181 * dir/node pages without enough free segments.
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900182 */
Jaegeuk Kim43727522013-02-04 15:11:17 +0900183 if (has_not_enough_free_secs(sbi, 0)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900184 mutex_lock(&sbi->gc_mutex);
Jaegeuk Kim408e9372013-01-03 17:55:52 +0900185 f2fs_gc(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900186 }
187}
188
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900189void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi)
190{
191 /* check the # of cached NAT entries and prefree segments */
192 if (try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK) ||
193 excess_prefree_segs(sbi))
194 f2fs_sync_fs(sbi->sb, true);
195}
196
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900197static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
198 enum dirty_type dirty_type)
199{
200 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
201
202 /* need not be added */
203 if (IS_CURSEG(sbi, segno))
204 return;
205
206 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
207 dirty_i->nr_dirty[dirty_type]++;
208
209 if (dirty_type == DIRTY) {
210 struct seg_entry *sentry = get_seg_entry(sbi, segno);
Changman Lee4625d6a2013-10-25 17:31:57 +0900211 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900212
Changman Lee4625d6a2013-10-25 17:31:57 +0900213 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
214 dirty_i->nr_dirty[t]++;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900215 }
216}
217
218static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
219 enum dirty_type dirty_type)
220{
221 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
222
223 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
224 dirty_i->nr_dirty[dirty_type]--;
225
226 if (dirty_type == DIRTY) {
Changman Lee4625d6a2013-10-25 17:31:57 +0900227 struct seg_entry *sentry = get_seg_entry(sbi, segno);
228 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900229
Changman Lee4625d6a2013-10-25 17:31:57 +0900230 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
231 dirty_i->nr_dirty[t]--;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900232
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +0900233 if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0)
234 clear_bit(GET_SECNO(sbi, segno),
235 dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900236 }
237}
238
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900239/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900240 * Should not occur error such as -ENOMEM.
241 * Adding dirty entry into seglist is not critical operation.
242 * If a given segment is one of current working segments, it won't be added.
243 */
Haicheng Li8d8451a2013-06-13 16:59:28 +0800244static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900245{
246 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
247 unsigned short valid_blocks;
248
249 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
250 return;
251
252 mutex_lock(&dirty_i->seglist_lock);
253
254 valid_blocks = get_valid_blocks(sbi, segno, 0);
255
256 if (valid_blocks == 0) {
257 __locate_dirty_segment(sbi, segno, PRE);
258 __remove_dirty_segment(sbi, segno, DIRTY);
259 } else if (valid_blocks < sbi->blocks_per_seg) {
260 __locate_dirty_segment(sbi, segno, DIRTY);
261 } else {
262 /* Recovery routine with SSR needs this */
263 __remove_dirty_segment(sbi, segno, DIRTY);
264 }
265
266 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900267}
268
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900269static void add_discard_addrs(struct f2fs_sb_info *sbi,
270 unsigned int segno, struct seg_entry *se)
271{
272 struct list_head *head = &SM_I(sbi)->discard_list;
273 struct discard_entry *new;
274 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
275 int max_blocks = sbi->blocks_per_seg;
276 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
277 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
278 unsigned long dmap[entries];
279 unsigned int start = 0, end = -1;
280 int i;
281
282 if (!test_opt(sbi, DISCARD))
283 return;
284
285 /* zero block will be discarded through the prefree list */
286 if (!se->valid_blocks || se->valid_blocks == max_blocks)
287 return;
288
289 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
290 for (i = 0; i < entries; i++)
291 dmap[i] = (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
292
293 while (SM_I(sbi)->nr_discards <= SM_I(sbi)->max_discards) {
294 start = __find_rev_next_bit(dmap, max_blocks, end + 1);
295 if (start >= max_blocks)
296 break;
297
298 end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
299
300 new = f2fs_kmem_cache_alloc(discard_entry_slab, GFP_NOFS);
301 INIT_LIST_HEAD(&new->list);
302 new->blkaddr = START_BLOCK(sbi, segno) + start;
303 new->len = end - start;
304
305 list_add_tail(&new->list, head);
306 SM_I(sbi)->nr_discards += end - start;
307 }
308}
309
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900310/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900311 * Should call clear_prefree_segments after checkpoint is done.
312 */
313static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
314{
315 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Haicheng Li81fb5e82013-05-14 18:20:28 +0800316 unsigned int segno = -1;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900317 unsigned int total_segs = TOTAL_SEGS(sbi);
318
319 mutex_lock(&dirty_i->seglist_lock);
320 while (1) {
321 segno = find_next_bit(dirty_i->dirty_segmap[PRE], total_segs,
Haicheng Li81fb5e82013-05-14 18:20:28 +0800322 segno + 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900323 if (segno >= total_segs)
324 break;
325 __set_test_and_free(sbi, segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900326 }
327 mutex_unlock(&dirty_i->seglist_lock);
328}
329
330void clear_prefree_segments(struct f2fs_sb_info *sbi)
331{
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900332 struct list_head *head = &(SM_I(sbi)->discard_list);
333 struct list_head *this, *next;
334 struct discard_entry *entry;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900335 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Changman Lee29e59c12013-11-11 09:24:37 +0900336 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900337 unsigned int total_segs = TOTAL_SEGS(sbi);
Changman Lee29e59c12013-11-11 09:24:37 +0900338 unsigned int start = 0, end = -1;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900339
340 mutex_lock(&dirty_i->seglist_lock);
Changman Lee29e59c12013-11-11 09:24:37 +0900341
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900342 while (1) {
Changman Lee29e59c12013-11-11 09:24:37 +0900343 int i;
344 start = find_next_bit(prefree_map, total_segs, end + 1);
345 if (start >= total_segs)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900346 break;
Changman Lee29e59c12013-11-11 09:24:37 +0900347 end = find_next_zero_bit(prefree_map, total_segs, start + 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900348
Changman Lee29e59c12013-11-11 09:24:37 +0900349 for (i = start; i < end; i++)
350 clear_bit(i, prefree_map);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900351
Changman Lee29e59c12013-11-11 09:24:37 +0900352 dirty_i->nr_dirty[PRE] -= end - start;
353
354 if (!test_opt(sbi, DISCARD))
355 continue;
356
357 blkdev_issue_discard(sbi->sb->s_bdev,
358 START_BLOCK(sbi, start) <<
359 sbi->log_sectors_per_block,
360 (1 << (sbi->log_sectors_per_block +
361 sbi->log_blocks_per_seg)) * (end - start),
362 GFP_NOFS, 0);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900363 }
364 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900365
366 /* send small discards */
367 list_for_each_safe(this, next, head) {
368 entry = list_entry(this, struct discard_entry, list);
369 blkdev_issue_discard(sbi->sb->s_bdev,
370 entry->blkaddr << sbi->log_sectors_per_block,
371 (1 << sbi->log_sectors_per_block) * entry->len,
372 GFP_NOFS, 0);
373 list_del(&entry->list);
374 SM_I(sbi)->nr_discards -= entry->len;
375 kmem_cache_free(discard_entry_slab, entry);
376 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900377}
378
379static void __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
380{
381 struct sit_info *sit_i = SIT_I(sbi);
382 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap))
383 sit_i->dirty_sentries++;
384}
385
386static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
387 unsigned int segno, int modified)
388{
389 struct seg_entry *se = get_seg_entry(sbi, segno);
390 se->type = type;
391 if (modified)
392 __mark_sit_entry_dirty(sbi, segno);
393}
394
395static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
396{
397 struct seg_entry *se;
398 unsigned int segno, offset;
399 long int new_vblocks;
400
401 segno = GET_SEGNO(sbi, blkaddr);
402
403 se = get_seg_entry(sbi, segno);
404 new_vblocks = se->valid_blocks + del;
405 offset = GET_SEGOFF_FROM_SEG0(sbi, blkaddr) & (sbi->blocks_per_seg - 1);
406
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900407 f2fs_bug_on((new_vblocks >> (sizeof(unsigned short) << 3) ||
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900408 (new_vblocks > sbi->blocks_per_seg)));
409
410 se->valid_blocks = new_vblocks;
411 se->mtime = get_mtime(sbi);
412 SIT_I(sbi)->max_mtime = se->mtime;
413
414 /* Update valid block bitmap */
415 if (del > 0) {
416 if (f2fs_set_bit(offset, se->cur_valid_map))
417 BUG();
418 } else {
419 if (!f2fs_clear_bit(offset, se->cur_valid_map))
420 BUG();
421 }
422 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
423 se->ckpt_valid_blocks += del;
424
425 __mark_sit_entry_dirty(sbi, segno);
426
427 /* update total number of valid blocks to be written in ckpt area */
428 SIT_I(sbi)->written_valid_blocks += del;
429
430 if (sbi->segs_per_sec > 1)
431 get_sec_entry(sbi, segno)->valid_blocks += del;
432}
433
434static void refresh_sit_entry(struct f2fs_sb_info *sbi,
435 block_t old_blkaddr, block_t new_blkaddr)
436{
437 update_sit_entry(sbi, new_blkaddr, 1);
438 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
439 update_sit_entry(sbi, old_blkaddr, -1);
440}
441
442void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
443{
444 unsigned int segno = GET_SEGNO(sbi, addr);
445 struct sit_info *sit_i = SIT_I(sbi);
446
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900447 f2fs_bug_on(addr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900448 if (addr == NEW_ADDR)
449 return;
450
451 /* add it into sit main buffer */
452 mutex_lock(&sit_i->sentry_lock);
453
454 update_sit_entry(sbi, addr, -1);
455
456 /* add it into dirty seglist */
457 locate_dirty_segment(sbi, segno);
458
459 mutex_unlock(&sit_i->sentry_lock);
460}
461
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900462/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900463 * This function should be resided under the curseg_mutex lock
464 */
465static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
Haicheng Lie79efe32013-06-13 16:59:27 +0800466 struct f2fs_summary *sum)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900467{
468 struct curseg_info *curseg = CURSEG_I(sbi, type);
469 void *addr = curseg->sum_blk;
Haicheng Lie79efe32013-06-13 16:59:27 +0800470 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900471 memcpy(addr, sum, sizeof(struct f2fs_summary));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900472}
473
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900474/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900475 * Calculate the number of current summary pages for writing
476 */
477int npages_for_summary_flush(struct f2fs_sb_info *sbi)
478{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900479 int valid_sum_count = 0;
Fan Li9a479382013-10-29 16:21:47 +0800480 int i, sum_in_page;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900481
482 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
483 if (sbi->ckpt->alloc_type[i] == SSR)
484 valid_sum_count += sbi->blocks_per_seg;
485 else
486 valid_sum_count += curseg_blkoff(sbi, i);
487 }
488
Fan Li9a479382013-10-29 16:21:47 +0800489 sum_in_page = (PAGE_CACHE_SIZE - 2 * SUM_JOURNAL_SIZE -
490 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
491 if (valid_sum_count <= sum_in_page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900492 return 1;
Fan Li9a479382013-10-29 16:21:47 +0800493 else if ((valid_sum_count - sum_in_page) <=
494 (PAGE_CACHE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900495 return 2;
496 return 3;
497}
498
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900499/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900500 * Caller should put this summary page
501 */
502struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
503{
504 return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
505}
506
507static void write_sum_page(struct f2fs_sb_info *sbi,
508 struct f2fs_summary_block *sum_blk, block_t blk_addr)
509{
510 struct page *page = grab_meta_page(sbi, blk_addr);
511 void *kaddr = page_address(page);
512 memcpy(kaddr, sum_blk, PAGE_CACHE_SIZE);
513 set_page_dirty(page);
514 f2fs_put_page(page, 1);
515}
516
Jaegeuk Kim60374682013-03-31 13:58:51 +0900517static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
518{
519 struct curseg_info *curseg = CURSEG_I(sbi, type);
Haicheng Li81fb5e82013-05-14 18:20:28 +0800520 unsigned int segno = curseg->segno + 1;
Jaegeuk Kim60374682013-03-31 13:58:51 +0900521 struct free_segmap_info *free_i = FREE_I(sbi);
522
Haicheng Li81fb5e82013-05-14 18:20:28 +0800523 if (segno < TOTAL_SEGS(sbi) && segno % sbi->segs_per_sec)
524 return !test_bit(segno, free_i->free_segmap);
Jaegeuk Kim60374682013-03-31 13:58:51 +0900525 return 0;
526}
527
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900528/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900529 * Find a new segment from the free segments bitmap to right order
530 * This function should be returned with success, otherwise BUG
531 */
532static void get_new_segment(struct f2fs_sb_info *sbi,
533 unsigned int *newseg, bool new_sec, int dir)
534{
535 struct free_segmap_info *free_i = FREE_I(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900536 unsigned int segno, secno, zoneno;
Jaegeuk Kim53cf9522013-03-31 12:39:49 +0900537 unsigned int total_zones = TOTAL_SECS(sbi) / sbi->secs_per_zone;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900538 unsigned int hint = *newseg / sbi->segs_per_sec;
539 unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
540 unsigned int left_start = hint;
541 bool init = true;
542 int go_left = 0;
543 int i;
544
545 write_lock(&free_i->segmap_lock);
546
547 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
548 segno = find_next_zero_bit(free_i->free_segmap,
549 TOTAL_SEGS(sbi), *newseg + 1);
Jaegeuk Kim33afa7f2013-03-31 12:59:53 +0900550 if (segno - *newseg < sbi->segs_per_sec -
551 (*newseg % sbi->segs_per_sec))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900552 goto got_it;
553 }
554find_other_zone:
Jaegeuk Kim53cf9522013-03-31 12:39:49 +0900555 secno = find_next_zero_bit(free_i->free_secmap, TOTAL_SECS(sbi), hint);
556 if (secno >= TOTAL_SECS(sbi)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900557 if (dir == ALLOC_RIGHT) {
558 secno = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim53cf9522013-03-31 12:39:49 +0900559 TOTAL_SECS(sbi), 0);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900560 f2fs_bug_on(secno >= TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900561 } else {
562 go_left = 1;
563 left_start = hint - 1;
564 }
565 }
566 if (go_left == 0)
567 goto skip_left;
568
569 while (test_bit(left_start, free_i->free_secmap)) {
570 if (left_start > 0) {
571 left_start--;
572 continue;
573 }
574 left_start = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim53cf9522013-03-31 12:39:49 +0900575 TOTAL_SECS(sbi), 0);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900576 f2fs_bug_on(left_start >= TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900577 break;
578 }
579 secno = left_start;
580skip_left:
581 hint = secno;
582 segno = secno * sbi->segs_per_sec;
583 zoneno = secno / sbi->secs_per_zone;
584
585 /* give up on finding another zone */
586 if (!init)
587 goto got_it;
588 if (sbi->secs_per_zone == 1)
589 goto got_it;
590 if (zoneno == old_zoneno)
591 goto got_it;
592 if (dir == ALLOC_LEFT) {
593 if (!go_left && zoneno + 1 >= total_zones)
594 goto got_it;
595 if (go_left && zoneno == 0)
596 goto got_it;
597 }
598 for (i = 0; i < NR_CURSEG_TYPE; i++)
599 if (CURSEG_I(sbi, i)->zone == zoneno)
600 break;
601
602 if (i < NR_CURSEG_TYPE) {
603 /* zone is in user, try another */
604 if (go_left)
605 hint = zoneno * sbi->secs_per_zone - 1;
606 else if (zoneno + 1 >= total_zones)
607 hint = 0;
608 else
609 hint = (zoneno + 1) * sbi->secs_per_zone;
610 init = false;
611 goto find_other_zone;
612 }
613got_it:
614 /* set it as dirty segment in free segmap */
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900615 f2fs_bug_on(test_bit(segno, free_i->free_segmap));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900616 __set_inuse(sbi, segno);
617 *newseg = segno;
618 write_unlock(&free_i->segmap_lock);
619}
620
621static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
622{
623 struct curseg_info *curseg = CURSEG_I(sbi, type);
624 struct summary_footer *sum_footer;
625
626 curseg->segno = curseg->next_segno;
627 curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
628 curseg->next_blkoff = 0;
629 curseg->next_segno = NULL_SEGNO;
630
631 sum_footer = &(curseg->sum_blk->footer);
632 memset(sum_footer, 0, sizeof(struct summary_footer));
633 if (IS_DATASEG(type))
634 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
635 if (IS_NODESEG(type))
636 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
637 __set_sit_entry_type(sbi, type, curseg->segno, modified);
638}
639
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900640/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900641 * Allocate a current working segment.
642 * This function always allocates a free segment in LFS manner.
643 */
644static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
645{
646 struct curseg_info *curseg = CURSEG_I(sbi, type);
647 unsigned int segno = curseg->segno;
648 int dir = ALLOC_LEFT;
649
650 write_sum_page(sbi, curseg->sum_blk,
Haicheng Li81fb5e82013-05-14 18:20:28 +0800651 GET_SUM_BLOCK(sbi, segno));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900652 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
653 dir = ALLOC_RIGHT;
654
655 if (test_opt(sbi, NOHEAP))
656 dir = ALLOC_RIGHT;
657
658 get_new_segment(sbi, &segno, new_sec, dir);
659 curseg->next_segno = segno;
660 reset_curseg(sbi, type, 1);
661 curseg->alloc_type = LFS;
662}
663
664static void __next_free_blkoff(struct f2fs_sb_info *sbi,
665 struct curseg_info *seg, block_t start)
666{
667 struct seg_entry *se = get_seg_entry(sbi, seg->segno);
Changman Leee81c93c2013-11-15 13:21:16 +0900668 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
669 unsigned long target_map[entries];
670 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
671 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
672 int i, pos;
673
674 for (i = 0; i < entries; i++)
675 target_map[i] = ckpt_map[i] | cur_map[i];
676
677 pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
678
679 seg->next_blkoff = pos;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900680}
681
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900682/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900683 * If a segment is written by LFS manner, next block offset is just obtained
684 * by increasing the current block offset. However, if a segment is written by
685 * SSR manner, next block offset obtained by calling __next_free_blkoff
686 */
687static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
688 struct curseg_info *seg)
689{
690 if (seg->alloc_type == SSR)
691 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
692 else
693 seg->next_blkoff++;
694}
695
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900696/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900697 * This function always allocates a used segment (from dirty seglist) by SSR
698 * manner, so it should recover the existing segment information of valid blocks
699 */
700static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
701{
702 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
703 struct curseg_info *curseg = CURSEG_I(sbi, type);
704 unsigned int new_segno = curseg->next_segno;
705 struct f2fs_summary_block *sum_node;
706 struct page *sum_page;
707
708 write_sum_page(sbi, curseg->sum_blk,
709 GET_SUM_BLOCK(sbi, curseg->segno));
710 __set_test_and_inuse(sbi, new_segno);
711
712 mutex_lock(&dirty_i->seglist_lock);
713 __remove_dirty_segment(sbi, new_segno, PRE);
714 __remove_dirty_segment(sbi, new_segno, DIRTY);
715 mutex_unlock(&dirty_i->seglist_lock);
716
717 reset_curseg(sbi, type, 1);
718 curseg->alloc_type = SSR;
719 __next_free_blkoff(sbi, curseg, 0);
720
721 if (reuse) {
722 sum_page = get_sum_page(sbi, new_segno);
723 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
724 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
725 f2fs_put_page(sum_page, 1);
726 }
727}
728
Jaegeuk Kim43727522013-02-04 15:11:17 +0900729static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
730{
731 struct curseg_info *curseg = CURSEG_I(sbi, type);
732 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
733
734 if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0))
735 return v_ops->get_victim(sbi,
736 &(curseg)->next_segno, BG_GC, type, SSR);
737
738 /* For data segments, let's do SSR more intensively */
739 for (; type >= CURSEG_HOT_DATA; type--)
740 if (v_ops->get_victim(sbi, &(curseg)->next_segno,
741 BG_GC, type, SSR))
742 return 1;
743 return 0;
744}
745
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900746/*
747 * flush out current segment and replace it with new segment
748 * This function should be returned with success, otherwise BUG
749 */
750static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
751 int type, bool force)
752{
753 struct curseg_info *curseg = CURSEG_I(sbi, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900754
Gu Zheng7b405272013-08-19 09:41:15 +0800755 if (force)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900756 new_curseg(sbi, type, true);
Gu Zheng7b405272013-08-19 09:41:15 +0800757 else if (type == CURSEG_WARM_NODE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900758 new_curseg(sbi, type, false);
Jaegeuk Kim60374682013-03-31 13:58:51 +0900759 else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
760 new_curseg(sbi, type, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900761 else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
762 change_curseg(sbi, type, true);
763 else
764 new_curseg(sbi, type, false);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +0900765
766 stat_inc_seg_type(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900767}
768
769void allocate_new_segments(struct f2fs_sb_info *sbi)
770{
771 struct curseg_info *curseg;
772 unsigned int old_curseg;
773 int i;
774
775 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
776 curseg = CURSEG_I(sbi, i);
777 old_curseg = curseg->segno;
778 SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
779 locate_dirty_segment(sbi, old_curseg);
780 }
781}
782
783static const struct segment_allocation default_salloc_ops = {
784 .allocate_segment = allocate_segment_by_default,
785};
786
787static void f2fs_end_io_write(struct bio *bio, int err)
788{
789 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
790 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
791 struct bio_private *p = bio->bi_private;
792
793 do {
794 struct page *page = bvec->bv_page;
795
796 if (--bvec >= bio->bi_io_vec)
797 prefetchw(&bvec->bv_page->flags);
798 if (!uptodate) {
799 SetPageError(page);
800 if (page->mapping)
801 set_bit(AS_EIO, &page->mapping->flags);
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900802 set_ckpt_flags(p->sbi->ckpt, CP_ERROR_FLAG);
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900803 p->sbi->sb->s_flags |= MS_RDONLY;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900804 }
805 end_page_writeback(page);
806 dec_page_count(p->sbi, F2FS_WRITEBACK);
807 } while (bvec >= bio->bi_io_vec);
808
809 if (p->is_sync)
810 complete(p->wait);
Gu Zhenge2340882013-10-14 18:45:56 +0800811
Changman Leefb51b5e2013-11-07 12:48:25 +0900812 if (!get_pages(p->sbi, F2FS_WRITEBACK) &&
813 !list_empty(&p->sbi->cp_wait.task_list))
814 wake_up(&p->sbi->cp_wait);
Gu Zhenge2340882013-10-14 18:45:56 +0800815
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900816 kfree(p);
817 bio_put(bio);
818}
819
Jaegeuk Kim3cd8a232012-12-10 09:26:05 +0900820struct bio *f2fs_bio_alloc(struct block_device *bdev, int npages)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900821{
822 struct bio *bio;
Jaegeuk Kim3cd8a232012-12-10 09:26:05 +0900823
824 /* No failure on bio allocation */
825 bio = bio_alloc(GFP_NOIO, npages);
826 bio->bi_bdev = bdev;
Gu Zhengd8207f62013-07-25 11:30:01 +0800827 bio->bi_private = NULL;
828
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900829 return bio;
830}
831
832static void do_submit_bio(struct f2fs_sb_info *sbi,
833 enum page_type type, bool sync)
834{
835 int rw = sync ? WRITE_SYNC : WRITE;
836 enum page_type btype = type > META ? META : type;
837
838 if (type >= META_FLUSH)
839 rw = WRITE_FLUSH_FUA;
840
Namjae Jeon86804412013-04-25 11:45:21 +0900841 if (btype == META)
842 rw |= REQ_META;
843
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900844 if (sbi->bio[btype]) {
845 struct bio_private *p = sbi->bio[btype]->bi_private;
846 p->sbi = sbi;
847 sbi->bio[btype]->bi_end_io = f2fs_end_io_write;
Namjae Jeon6ec178d2013-04-23 17:51:43 +0900848
849 trace_f2fs_do_submit_bio(sbi->sb, btype, sync, sbi->bio[btype]);
850
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900851 if (type == META_FLUSH) {
852 DECLARE_COMPLETION_ONSTACK(wait);
853 p->is_sync = true;
854 p->wait = &wait;
855 submit_bio(rw, sbi->bio[btype]);
856 wait_for_completion(&wait);
857 } else {
858 p->is_sync = false;
859 submit_bio(rw, sbi->bio[btype]);
860 }
861 sbi->bio[btype] = NULL;
862 }
863}
864
865void f2fs_submit_bio(struct f2fs_sb_info *sbi, enum page_type type, bool sync)
866{
867 down_write(&sbi->bio_sem);
868 do_submit_bio(sbi, type, sync);
869 up_write(&sbi->bio_sem);
870}
871
872static void submit_write_page(struct f2fs_sb_info *sbi, struct page *page,
873 block_t blk_addr, enum page_type type)
874{
875 struct block_device *bdev = sbi->sb->s_bdev;
Chao Yucc7b1bb2013-09-22 15:50:50 +0800876 int bio_blocks;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900877
878 verify_block_addr(sbi, blk_addr);
879
880 down_write(&sbi->bio_sem);
881
882 inc_page_count(sbi, F2FS_WRITEBACK);
883
884 if (sbi->bio[type] && sbi->last_block_in_bio[type] != blk_addr - 1)
885 do_submit_bio(sbi, type, false);
886alloc_new:
Jaegeuk Kim3cd8a232012-12-10 09:26:05 +0900887 if (sbi->bio[type] == NULL) {
Gu Zhengd8207f62013-07-25 11:30:01 +0800888 struct bio_private *priv;
889retry:
890 priv = kmalloc(sizeof(struct bio_private), GFP_NOFS);
891 if (!priv) {
892 cond_resched();
893 goto retry;
894 }
895
Chao Yucc7b1bb2013-09-22 15:50:50 +0800896 bio_blocks = MAX_BIO_BLOCKS(max_hw_blocks(sbi));
897 sbi->bio[type] = f2fs_bio_alloc(bdev, bio_blocks);
Jaegeuk Kim3cd8a232012-12-10 09:26:05 +0900898 sbi->bio[type]->bi_sector = SECTOR_FROM_BLOCK(sbi, blk_addr);
Gu Zhengd8207f62013-07-25 11:30:01 +0800899 sbi->bio[type]->bi_private = priv;
Jaegeuk Kim3cd8a232012-12-10 09:26:05 +0900900 /*
901 * The end_io will be assigned at the sumbission phase.
902 * Until then, let bio_add_page() merge consecutive IOs as much
903 * as possible.
904 */
905 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900906
907 if (bio_add_page(sbi->bio[type], page, PAGE_CACHE_SIZE, 0) <
908 PAGE_CACHE_SIZE) {
909 do_submit_bio(sbi, type, false);
910 goto alloc_new;
911 }
912
913 sbi->last_block_in_bio[type] = blk_addr;
914
915 up_write(&sbi->bio_sem);
Namjae Jeon6ec178d2013-04-23 17:51:43 +0900916 trace_f2fs_submit_write_page(page, blk_addr, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900917}
918
Jin Xua5694692013-08-05 20:02:04 +0800919void f2fs_wait_on_page_writeback(struct page *page,
920 enum page_type type, bool sync)
921{
922 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
923 if (PageWriteback(page)) {
924 f2fs_submit_bio(sbi, type, sync);
925 wait_on_page_writeback(page);
926 }
927}
928
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900929static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
930{
931 struct curseg_info *curseg = CURSEG_I(sbi, type);
932 if (curseg->next_blkoff < sbi->blocks_per_seg)
933 return true;
934 return false;
935}
936
937static int __get_segment_type_2(struct page *page, enum page_type p_type)
938{
939 if (p_type == DATA)
940 return CURSEG_HOT_DATA;
941 else
942 return CURSEG_HOT_NODE;
943}
944
945static int __get_segment_type_4(struct page *page, enum page_type p_type)
946{
947 if (p_type == DATA) {
948 struct inode *inode = page->mapping->host;
949
950 if (S_ISDIR(inode->i_mode))
951 return CURSEG_HOT_DATA;
952 else
953 return CURSEG_COLD_DATA;
954 } else {
955 if (IS_DNODE(page) && !is_cold_node(page))
956 return CURSEG_HOT_NODE;
957 else
958 return CURSEG_COLD_NODE;
959 }
960}
961
962static int __get_segment_type_6(struct page *page, enum page_type p_type)
963{
964 if (p_type == DATA) {
965 struct inode *inode = page->mapping->host;
966
967 if (S_ISDIR(inode->i_mode))
968 return CURSEG_HOT_DATA;
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900969 else if (is_cold_data(page) || file_is_cold(inode))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900970 return CURSEG_COLD_DATA;
971 else
972 return CURSEG_WARM_DATA;
973 } else {
974 if (IS_DNODE(page))
975 return is_cold_node(page) ? CURSEG_WARM_NODE :
976 CURSEG_HOT_NODE;
977 else
978 return CURSEG_COLD_NODE;
979 }
980}
981
982static int __get_segment_type(struct page *page, enum page_type p_type)
983{
984 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
985 switch (sbi->active_logs) {
986 case 2:
987 return __get_segment_type_2(page, p_type);
988 case 4:
989 return __get_segment_type_4(page, p_type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900990 }
Jaegeuk Kim12a67142012-12-21 11:47:05 +0900991 /* NR_CURSEG_TYPE(6) logs by default */
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900992 f2fs_bug_on(sbi->active_logs != NR_CURSEG_TYPE);
Jaegeuk Kim12a67142012-12-21 11:47:05 +0900993 return __get_segment_type_6(page, p_type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900994}
995
996static void do_write_page(struct f2fs_sb_info *sbi, struct page *page,
997 block_t old_blkaddr, block_t *new_blkaddr,
998 struct f2fs_summary *sum, enum page_type p_type)
999{
1000 struct sit_info *sit_i = SIT_I(sbi);
1001 struct curseg_info *curseg;
1002 unsigned int old_cursegno;
1003 int type;
1004
1005 type = __get_segment_type(page, p_type);
1006 curseg = CURSEG_I(sbi, type);
1007
1008 mutex_lock(&curseg->curseg_mutex);
1009
1010 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
1011 old_cursegno = curseg->segno;
1012
1013 /*
1014 * __add_sum_entry should be resided under the curseg_mutex
1015 * because, this function updates a summary entry in the
1016 * current summary block.
1017 */
Haicheng Lie79efe32013-06-13 16:59:27 +08001018 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001019
1020 mutex_lock(&sit_i->sentry_lock);
1021 __refresh_next_blkoff(sbi, curseg);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +09001022
1023 stat_inc_block_count(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001024
1025 /*
1026 * SIT information should be updated before segment allocation,
1027 * since SSR needs latest valid block information.
1028 */
1029 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
1030
1031 if (!__has_curseg_space(sbi, type))
1032 sit_i->s_ops->allocate_segment(sbi, type, false);
1033
1034 locate_dirty_segment(sbi, old_cursegno);
1035 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
1036 mutex_unlock(&sit_i->sentry_lock);
1037
1038 if (p_type == NODE)
1039 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
1040
1041 /* writeout dirty page into bdev */
1042 submit_write_page(sbi, page, *new_blkaddr, p_type);
1043
1044 mutex_unlock(&curseg->curseg_mutex);
1045}
1046
Jaegeuk Kim577e3492013-01-24 19:56:11 +09001047void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001048{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001049 set_page_writeback(page);
1050 submit_write_page(sbi, page, page->index, META);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001051}
1052
1053void write_node_page(struct f2fs_sb_info *sbi, struct page *page,
1054 unsigned int nid, block_t old_blkaddr, block_t *new_blkaddr)
1055{
1056 struct f2fs_summary sum;
1057 set_summary(&sum, nid, 0, 0);
1058 do_write_page(sbi, page, old_blkaddr, new_blkaddr, &sum, NODE);
1059}
1060
1061void write_data_page(struct inode *inode, struct page *page,
1062 struct dnode_of_data *dn, block_t old_blkaddr,
1063 block_t *new_blkaddr)
1064{
1065 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1066 struct f2fs_summary sum;
1067 struct node_info ni;
1068
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001069 f2fs_bug_on(old_blkaddr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001070 get_node_info(sbi, dn->nid, &ni);
1071 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1072
1073 do_write_page(sbi, page, old_blkaddr,
1074 new_blkaddr, &sum, DATA);
1075}
1076
1077void rewrite_data_page(struct f2fs_sb_info *sbi, struct page *page,
1078 block_t old_blk_addr)
1079{
1080 submit_write_page(sbi, page, old_blk_addr, DATA);
1081}
1082
1083void recover_data_page(struct f2fs_sb_info *sbi,
1084 struct page *page, struct f2fs_summary *sum,
1085 block_t old_blkaddr, block_t new_blkaddr)
1086{
1087 struct sit_info *sit_i = SIT_I(sbi);
1088 struct curseg_info *curseg;
1089 unsigned int segno, old_cursegno;
1090 struct seg_entry *se;
1091 int type;
1092
1093 segno = GET_SEGNO(sbi, new_blkaddr);
1094 se = get_seg_entry(sbi, segno);
1095 type = se->type;
1096
1097 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
1098 if (old_blkaddr == NULL_ADDR)
1099 type = CURSEG_COLD_DATA;
1100 else
1101 type = CURSEG_WARM_DATA;
1102 }
1103 curseg = CURSEG_I(sbi, type);
1104
1105 mutex_lock(&curseg->curseg_mutex);
1106 mutex_lock(&sit_i->sentry_lock);
1107
1108 old_cursegno = curseg->segno;
1109
1110 /* change the current segment */
1111 if (segno != curseg->segno) {
1112 curseg->next_segno = segno;
1113 change_curseg(sbi, type, true);
1114 }
1115
1116 curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, new_blkaddr) &
1117 (sbi->blocks_per_seg - 1);
Haicheng Lie79efe32013-06-13 16:59:27 +08001118 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001119
1120 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
1121
1122 locate_dirty_segment(sbi, old_cursegno);
1123 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
1124
1125 mutex_unlock(&sit_i->sentry_lock);
1126 mutex_unlock(&curseg->curseg_mutex);
1127}
1128
1129void rewrite_node_page(struct f2fs_sb_info *sbi,
1130 struct page *page, struct f2fs_summary *sum,
1131 block_t old_blkaddr, block_t new_blkaddr)
1132{
1133 struct sit_info *sit_i = SIT_I(sbi);
1134 int type = CURSEG_WARM_NODE;
1135 struct curseg_info *curseg;
1136 unsigned int segno, old_cursegno;
1137 block_t next_blkaddr = next_blkaddr_of_node(page);
1138 unsigned int next_segno = GET_SEGNO(sbi, next_blkaddr);
1139
1140 curseg = CURSEG_I(sbi, type);
1141
1142 mutex_lock(&curseg->curseg_mutex);
1143 mutex_lock(&sit_i->sentry_lock);
1144
1145 segno = GET_SEGNO(sbi, new_blkaddr);
1146 old_cursegno = curseg->segno;
1147
1148 /* change the current segment */
1149 if (segno != curseg->segno) {
1150 curseg->next_segno = segno;
1151 change_curseg(sbi, type, true);
1152 }
1153 curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, new_blkaddr) &
1154 (sbi->blocks_per_seg - 1);
Haicheng Lie79efe32013-06-13 16:59:27 +08001155 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001156
1157 /* change the current log to the next block addr in advance */
1158 if (next_segno != segno) {
1159 curseg->next_segno = next_segno;
1160 change_curseg(sbi, type, true);
1161 }
1162 curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, next_blkaddr) &
1163 (sbi->blocks_per_seg - 1);
1164
1165 /* rewrite node page */
1166 set_page_writeback(page);
1167 submit_write_page(sbi, page, new_blkaddr, NODE);
1168 f2fs_submit_bio(sbi, NODE, true);
1169 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
1170
1171 locate_dirty_segment(sbi, old_cursegno);
1172 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
1173
1174 mutex_unlock(&sit_i->sentry_lock);
1175 mutex_unlock(&curseg->curseg_mutex);
1176}
1177
1178static int read_compacted_summaries(struct f2fs_sb_info *sbi)
1179{
1180 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1181 struct curseg_info *seg_i;
1182 unsigned char *kaddr;
1183 struct page *page;
1184 block_t start;
1185 int i, j, offset;
1186
1187 start = start_sum_block(sbi);
1188
1189 page = get_meta_page(sbi, start++);
1190 kaddr = (unsigned char *)page_address(page);
1191
1192 /* Step 1: restore nat cache */
1193 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1194 memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
1195
1196 /* Step 2: restore sit cache */
1197 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1198 memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
1199 SUM_JOURNAL_SIZE);
1200 offset = 2 * SUM_JOURNAL_SIZE;
1201
1202 /* Step 3: restore summary entries */
1203 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1204 unsigned short blk_off;
1205 unsigned int segno;
1206
1207 seg_i = CURSEG_I(sbi, i);
1208 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
1209 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
1210 seg_i->next_segno = segno;
1211 reset_curseg(sbi, i, 0);
1212 seg_i->alloc_type = ckpt->alloc_type[i];
1213 seg_i->next_blkoff = blk_off;
1214
1215 if (seg_i->alloc_type == SSR)
1216 blk_off = sbi->blocks_per_seg;
1217
1218 for (j = 0; j < blk_off; j++) {
1219 struct f2fs_summary *s;
1220 s = (struct f2fs_summary *)(kaddr + offset);
1221 seg_i->sum_blk->entries[j] = *s;
1222 offset += SUMMARY_SIZE;
1223 if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1224 SUM_FOOTER_SIZE)
1225 continue;
1226
1227 f2fs_put_page(page, 1);
1228 page = NULL;
1229
1230 page = get_meta_page(sbi, start++);
1231 kaddr = (unsigned char *)page_address(page);
1232 offset = 0;
1233 }
1234 }
1235 f2fs_put_page(page, 1);
1236 return 0;
1237}
1238
1239static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
1240{
1241 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1242 struct f2fs_summary_block *sum;
1243 struct curseg_info *curseg;
1244 struct page *new;
1245 unsigned short blk_off;
1246 unsigned int segno = 0;
1247 block_t blk_addr = 0;
1248
1249 /* get segment number and block addr */
1250 if (IS_DATASEG(type)) {
1251 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
1252 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
1253 CURSEG_HOT_DATA]);
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001254 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001255 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
1256 else
1257 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
1258 } else {
1259 segno = le32_to_cpu(ckpt->cur_node_segno[type -
1260 CURSEG_HOT_NODE]);
1261 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
1262 CURSEG_HOT_NODE]);
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001263 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001264 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
1265 type - CURSEG_HOT_NODE);
1266 else
1267 blk_addr = GET_SUM_BLOCK(sbi, segno);
1268 }
1269
1270 new = get_meta_page(sbi, blk_addr);
1271 sum = (struct f2fs_summary_block *)page_address(new);
1272
1273 if (IS_NODESEG(type)) {
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001274 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001275 struct f2fs_summary *ns = &sum->entries[0];
1276 int i;
1277 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
1278 ns->version = 0;
1279 ns->ofs_in_node = 0;
1280 }
1281 } else {
1282 if (restore_node_summary(sbi, segno, sum)) {
1283 f2fs_put_page(new, 1);
1284 return -EINVAL;
1285 }
1286 }
1287 }
1288
1289 /* set uncompleted segment to curseg */
1290 curseg = CURSEG_I(sbi, type);
1291 mutex_lock(&curseg->curseg_mutex);
1292 memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
1293 curseg->next_segno = segno;
1294 reset_curseg(sbi, type, 0);
1295 curseg->alloc_type = ckpt->alloc_type[type];
1296 curseg->next_blkoff = blk_off;
1297 mutex_unlock(&curseg->curseg_mutex);
1298 f2fs_put_page(new, 1);
1299 return 0;
1300}
1301
1302static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
1303{
1304 int type = CURSEG_HOT_DATA;
1305
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001306 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001307 /* restore for compacted data summary */
1308 if (read_compacted_summaries(sbi))
1309 return -EINVAL;
1310 type = CURSEG_HOT_NODE;
1311 }
1312
1313 for (; type <= CURSEG_COLD_NODE; type++)
1314 if (read_normal_summaries(sbi, type))
1315 return -EINVAL;
1316 return 0;
1317}
1318
1319static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
1320{
1321 struct page *page;
1322 unsigned char *kaddr;
1323 struct f2fs_summary *summary;
1324 struct curseg_info *seg_i;
1325 int written_size = 0;
1326 int i, j;
1327
1328 page = grab_meta_page(sbi, blkaddr++);
1329 kaddr = (unsigned char *)page_address(page);
1330
1331 /* Step 1: write nat cache */
1332 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1333 memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
1334 written_size += SUM_JOURNAL_SIZE;
1335
1336 /* Step 2: write sit cache */
1337 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1338 memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
1339 SUM_JOURNAL_SIZE);
1340 written_size += SUM_JOURNAL_SIZE;
1341
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001342 /* Step 3: write summary entries */
1343 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1344 unsigned short blkoff;
1345 seg_i = CURSEG_I(sbi, i);
1346 if (sbi->ckpt->alloc_type[i] == SSR)
1347 blkoff = sbi->blocks_per_seg;
1348 else
1349 blkoff = curseg_blkoff(sbi, i);
1350
1351 for (j = 0; j < blkoff; j++) {
1352 if (!page) {
1353 page = grab_meta_page(sbi, blkaddr++);
1354 kaddr = (unsigned char *)page_address(page);
1355 written_size = 0;
1356 }
1357 summary = (struct f2fs_summary *)(kaddr + written_size);
1358 *summary = seg_i->sum_blk->entries[j];
1359 written_size += SUMMARY_SIZE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001360
1361 if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1362 SUM_FOOTER_SIZE)
1363 continue;
1364
Chao Yue8d61a72013-10-24 15:08:28 +08001365 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001366 f2fs_put_page(page, 1);
1367 page = NULL;
1368 }
1369 }
Chao Yue8d61a72013-10-24 15:08:28 +08001370 if (page) {
1371 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001372 f2fs_put_page(page, 1);
Chao Yue8d61a72013-10-24 15:08:28 +08001373 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001374}
1375
1376static void write_normal_summaries(struct f2fs_sb_info *sbi,
1377 block_t blkaddr, int type)
1378{
1379 int i, end;
1380 if (IS_DATASEG(type))
1381 end = type + NR_CURSEG_DATA_TYPE;
1382 else
1383 end = type + NR_CURSEG_NODE_TYPE;
1384
1385 for (i = type; i < end; i++) {
1386 struct curseg_info *sum = CURSEG_I(sbi, i);
1387 mutex_lock(&sum->curseg_mutex);
1388 write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
1389 mutex_unlock(&sum->curseg_mutex);
1390 }
1391}
1392
1393void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1394{
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001395 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001396 write_compacted_summaries(sbi, start_blk);
1397 else
1398 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
1399}
1400
1401void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1402{
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001403 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001404 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001405}
1406
1407int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
1408 unsigned int val, int alloc)
1409{
1410 int i;
1411
1412 if (type == NAT_JOURNAL) {
1413 for (i = 0; i < nats_in_cursum(sum); i++) {
1414 if (le32_to_cpu(nid_in_journal(sum, i)) == val)
1415 return i;
1416 }
1417 if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
1418 return update_nats_in_cursum(sum, 1);
1419 } else if (type == SIT_JOURNAL) {
1420 for (i = 0; i < sits_in_cursum(sum); i++)
1421 if (le32_to_cpu(segno_in_journal(sum, i)) == val)
1422 return i;
1423 if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
1424 return update_sits_in_cursum(sum, 1);
1425 }
1426 return -1;
1427}
1428
1429static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
1430 unsigned int segno)
1431{
1432 struct sit_info *sit_i = SIT_I(sbi);
1433 unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno);
1434 block_t blk_addr = sit_i->sit_base_addr + offset;
1435
1436 check_seg_range(sbi, segno);
1437
1438 /* calculate sit block address */
1439 if (f2fs_test_bit(offset, sit_i->sit_bitmap))
1440 blk_addr += sit_i->sit_blocks;
1441
1442 return get_meta_page(sbi, blk_addr);
1443}
1444
1445static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
1446 unsigned int start)
1447{
1448 struct sit_info *sit_i = SIT_I(sbi);
1449 struct page *src_page, *dst_page;
1450 pgoff_t src_off, dst_off;
1451 void *src_addr, *dst_addr;
1452
1453 src_off = current_sit_addr(sbi, start);
1454 dst_off = next_sit_addr(sbi, src_off);
1455
1456 /* get current sit block page without lock */
1457 src_page = get_meta_page(sbi, src_off);
1458 dst_page = grab_meta_page(sbi, dst_off);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001459 f2fs_bug_on(PageDirty(src_page));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001460
1461 src_addr = page_address(src_page);
1462 dst_addr = page_address(dst_page);
1463 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
1464
1465 set_page_dirty(dst_page);
1466 f2fs_put_page(src_page, 1);
1467
1468 set_to_next_sit(sit_i, start);
1469
1470 return dst_page;
1471}
1472
1473static bool flush_sits_in_journal(struct f2fs_sb_info *sbi)
1474{
1475 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1476 struct f2fs_summary_block *sum = curseg->sum_blk;
1477 int i;
1478
1479 /*
1480 * If the journal area in the current summary is full of sit entries,
1481 * all the sit entries will be flushed. Otherwise the sit entries
1482 * are not able to replace with newly hot sit entries.
1483 */
1484 if (sits_in_cursum(sum) >= SIT_JOURNAL_ENTRIES) {
1485 for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
1486 unsigned int segno;
1487 segno = le32_to_cpu(segno_in_journal(sum, i));
1488 __mark_sit_entry_dirty(sbi, segno);
1489 }
1490 update_sits_in_cursum(sum, -sits_in_cursum(sum));
Haicheng Licffbfa62013-10-18 17:24:07 +08001491 return true;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001492 }
Haicheng Licffbfa62013-10-18 17:24:07 +08001493 return false;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001494}
1495
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001496/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001497 * CP calls this function, which flushes SIT entries including sit_journal,
1498 * and moves prefree segs to free segs.
1499 */
1500void flush_sit_entries(struct f2fs_sb_info *sbi)
1501{
1502 struct sit_info *sit_i = SIT_I(sbi);
1503 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
1504 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1505 struct f2fs_summary_block *sum = curseg->sum_blk;
1506 unsigned long nsegs = TOTAL_SEGS(sbi);
1507 struct page *page = NULL;
1508 struct f2fs_sit_block *raw_sit = NULL;
1509 unsigned int start = 0, end = 0;
1510 unsigned int segno = -1;
1511 bool flushed;
1512
1513 mutex_lock(&curseg->curseg_mutex);
1514 mutex_lock(&sit_i->sentry_lock);
1515
1516 /*
1517 * "flushed" indicates whether sit entries in journal are flushed
1518 * to the SIT area or not.
1519 */
1520 flushed = flush_sits_in_journal(sbi);
1521
1522 while ((segno = find_next_bit(bitmap, nsegs, segno + 1)) < nsegs) {
1523 struct seg_entry *se = get_seg_entry(sbi, segno);
1524 int sit_offset, offset;
1525
1526 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
1527
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001528 /* add discard candidates */
1529 if (SM_I(sbi)->nr_discards < SM_I(sbi)->max_discards)
1530 add_discard_addrs(sbi, segno, se);
1531
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001532 if (flushed)
1533 goto to_sit_page;
1534
1535 offset = lookup_journal_in_cursum(sum, SIT_JOURNAL, segno, 1);
1536 if (offset >= 0) {
1537 segno_in_journal(sum, offset) = cpu_to_le32(segno);
1538 seg_info_to_raw_sit(se, &sit_in_journal(sum, offset));
1539 goto flush_done;
1540 }
1541to_sit_page:
1542 if (!page || (start > segno) || (segno > end)) {
1543 if (page) {
1544 f2fs_put_page(page, 1);
1545 page = NULL;
1546 }
1547
1548 start = START_SEGNO(sit_i, segno);
1549 end = start + SIT_ENTRY_PER_BLOCK - 1;
1550
1551 /* read sit block that will be updated */
1552 page = get_next_sit_page(sbi, start);
1553 raw_sit = page_address(page);
1554 }
1555
1556 /* udpate entry in SIT block */
1557 seg_info_to_raw_sit(se, &raw_sit->entries[sit_offset]);
1558flush_done:
1559 __clear_bit(segno, bitmap);
1560 sit_i->dirty_sentries--;
1561 }
1562 mutex_unlock(&sit_i->sentry_lock);
1563 mutex_unlock(&curseg->curseg_mutex);
1564
1565 /* writeout last modified SIT block */
1566 f2fs_put_page(page, 1);
1567
1568 set_prefree_as_free_segments(sbi);
1569}
1570
1571static int build_sit_info(struct f2fs_sb_info *sbi)
1572{
1573 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1574 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1575 struct sit_info *sit_i;
1576 unsigned int sit_segs, start;
1577 char *src_bitmap, *dst_bitmap;
1578 unsigned int bitmap_size;
1579
1580 /* allocate memory for SIT information */
1581 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
1582 if (!sit_i)
1583 return -ENOMEM;
1584
1585 SM_I(sbi)->sit_info = sit_i;
1586
1587 sit_i->sentries = vzalloc(TOTAL_SEGS(sbi) * sizeof(struct seg_entry));
1588 if (!sit_i->sentries)
1589 return -ENOMEM;
1590
1591 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1592 sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1593 if (!sit_i->dirty_sentries_bitmap)
1594 return -ENOMEM;
1595
1596 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1597 sit_i->sentries[start].cur_valid_map
1598 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1599 sit_i->sentries[start].ckpt_valid_map
1600 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1601 if (!sit_i->sentries[start].cur_valid_map
1602 || !sit_i->sentries[start].ckpt_valid_map)
1603 return -ENOMEM;
1604 }
1605
1606 if (sbi->segs_per_sec > 1) {
Jaegeuk Kim53cf9522013-03-31 12:39:49 +09001607 sit_i->sec_entries = vzalloc(TOTAL_SECS(sbi) *
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001608 sizeof(struct sec_entry));
1609 if (!sit_i->sec_entries)
1610 return -ENOMEM;
1611 }
1612
1613 /* get information related with SIT */
1614 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
1615
1616 /* setup SIT bitmap from ckeckpoint pack */
1617 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
1618 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
1619
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001620 dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001621 if (!dst_bitmap)
1622 return -ENOMEM;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001623
1624 /* init SIT information */
1625 sit_i->s_ops = &default_salloc_ops;
1626
1627 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
1628 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
1629 sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
1630 sit_i->sit_bitmap = dst_bitmap;
1631 sit_i->bitmap_size = bitmap_size;
1632 sit_i->dirty_sentries = 0;
1633 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
1634 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
1635 sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
1636 mutex_init(&sit_i->sentry_lock);
1637 return 0;
1638}
1639
1640static int build_free_segmap(struct f2fs_sb_info *sbi)
1641{
1642 struct f2fs_sm_info *sm_info = SM_I(sbi);
1643 struct free_segmap_info *free_i;
1644 unsigned int bitmap_size, sec_bitmap_size;
1645
1646 /* allocate memory for free segmap information */
1647 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
1648 if (!free_i)
1649 return -ENOMEM;
1650
1651 SM_I(sbi)->free_info = free_i;
1652
1653 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1654 free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL);
1655 if (!free_i->free_segmap)
1656 return -ENOMEM;
1657
Jaegeuk Kim53cf9522013-03-31 12:39:49 +09001658 sec_bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001659 free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL);
1660 if (!free_i->free_secmap)
1661 return -ENOMEM;
1662
1663 /* set all segments as dirty temporarily */
1664 memset(free_i->free_segmap, 0xff, bitmap_size);
1665 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
1666
1667 /* init free segmap information */
1668 free_i->start_segno =
1669 (unsigned int) GET_SEGNO_FROM_SEG0(sbi, sm_info->main_blkaddr);
1670 free_i->free_segments = 0;
1671 free_i->free_sections = 0;
1672 rwlock_init(&free_i->segmap_lock);
1673 return 0;
1674}
1675
1676static int build_curseg(struct f2fs_sb_info *sbi)
1677{
Namjae Jeon1042d602012-12-01 10:56:13 +09001678 struct curseg_info *array;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001679 int i;
1680
1681 array = kzalloc(sizeof(*array) * NR_CURSEG_TYPE, GFP_KERNEL);
1682 if (!array)
1683 return -ENOMEM;
1684
1685 SM_I(sbi)->curseg_array = array;
1686
1687 for (i = 0; i < NR_CURSEG_TYPE; i++) {
1688 mutex_init(&array[i].curseg_mutex);
1689 array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
1690 if (!array[i].sum_blk)
1691 return -ENOMEM;
1692 array[i].segno = NULL_SEGNO;
1693 array[i].next_blkoff = 0;
1694 }
1695 return restore_curseg_summaries(sbi);
1696}
1697
1698static void build_sit_entries(struct f2fs_sb_info *sbi)
1699{
1700 struct sit_info *sit_i = SIT_I(sbi);
1701 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1702 struct f2fs_summary_block *sum = curseg->sum_blk;
1703 unsigned int start;
1704
1705 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1706 struct seg_entry *se = &sit_i->sentries[start];
1707 struct f2fs_sit_block *sit_blk;
1708 struct f2fs_sit_entry sit;
1709 struct page *page;
1710 int i;
1711
1712 mutex_lock(&curseg->curseg_mutex);
1713 for (i = 0; i < sits_in_cursum(sum); i++) {
1714 if (le32_to_cpu(segno_in_journal(sum, i)) == start) {
1715 sit = sit_in_journal(sum, i);
1716 mutex_unlock(&curseg->curseg_mutex);
1717 goto got_it;
1718 }
1719 }
1720 mutex_unlock(&curseg->curseg_mutex);
1721 page = get_current_sit_page(sbi, start);
1722 sit_blk = (struct f2fs_sit_block *)page_address(page);
1723 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
1724 f2fs_put_page(page, 1);
1725got_it:
1726 check_block_count(sbi, start, &sit);
1727 seg_info_from_raw_sit(se, &sit);
1728 if (sbi->segs_per_sec > 1) {
1729 struct sec_entry *e = get_sec_entry(sbi, start);
1730 e->valid_blocks += se->valid_blocks;
1731 }
1732 }
1733}
1734
1735static void init_free_segmap(struct f2fs_sb_info *sbi)
1736{
1737 unsigned int start;
1738 int type;
1739
1740 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1741 struct seg_entry *sentry = get_seg_entry(sbi, start);
1742 if (!sentry->valid_blocks)
1743 __set_free(sbi, start);
1744 }
1745
1746 /* set use the current segments */
1747 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
1748 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
1749 __set_test_and_inuse(sbi, curseg_t->segno);
1750 }
1751}
1752
1753static void init_dirty_segmap(struct f2fs_sb_info *sbi)
1754{
1755 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1756 struct free_segmap_info *free_i = FREE_I(sbi);
Namjae Jeon8736fbf2013-06-16 09:49:11 +09001757 unsigned int segno = 0, offset = 0, total_segs = TOTAL_SEGS(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001758 unsigned short valid_blocks;
1759
Namjae Jeon8736fbf2013-06-16 09:49:11 +09001760 while (1) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001761 /* find dirty segment based on free segmap */
Namjae Jeon8736fbf2013-06-16 09:49:11 +09001762 segno = find_next_inuse(free_i, total_segs, offset);
1763 if (segno >= total_segs)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001764 break;
1765 offset = segno + 1;
1766 valid_blocks = get_valid_blocks(sbi, segno, 0);
1767 if (valid_blocks >= sbi->blocks_per_seg || !valid_blocks)
1768 continue;
1769 mutex_lock(&dirty_i->seglist_lock);
1770 __locate_dirty_segment(sbi, segno, DIRTY);
1771 mutex_unlock(&dirty_i->seglist_lock);
1772 }
1773}
1774
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001775static int init_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001776{
1777 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001778 unsigned int bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001779
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001780 dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL);
1781 if (!dirty_i->victim_secmap)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001782 return -ENOMEM;
1783 return 0;
1784}
1785
1786static int build_dirty_segmap(struct f2fs_sb_info *sbi)
1787{
1788 struct dirty_seglist_info *dirty_i;
1789 unsigned int bitmap_size, i;
1790
1791 /* allocate memory for dirty segments list information */
1792 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
1793 if (!dirty_i)
1794 return -ENOMEM;
1795
1796 SM_I(sbi)->dirty_info = dirty_i;
1797 mutex_init(&dirty_i->seglist_lock);
1798
1799 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1800
1801 for (i = 0; i < NR_DIRTY_TYPE; i++) {
1802 dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001803 if (!dirty_i->dirty_segmap[i])
1804 return -ENOMEM;
1805 }
1806
1807 init_dirty_segmap(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001808 return init_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001809}
1810
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001811/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001812 * Update min, max modified time for cost-benefit GC algorithm
1813 */
1814static void init_min_max_mtime(struct f2fs_sb_info *sbi)
1815{
1816 struct sit_info *sit_i = SIT_I(sbi);
1817 unsigned int segno;
1818
1819 mutex_lock(&sit_i->sentry_lock);
1820
1821 sit_i->min_mtime = LLONG_MAX;
1822
1823 for (segno = 0; segno < TOTAL_SEGS(sbi); segno += sbi->segs_per_sec) {
1824 unsigned int i;
1825 unsigned long long mtime = 0;
1826
1827 for (i = 0; i < sbi->segs_per_sec; i++)
1828 mtime += get_seg_entry(sbi, segno + i)->mtime;
1829
1830 mtime = div_u64(mtime, sbi->segs_per_sec);
1831
1832 if (sit_i->min_mtime > mtime)
1833 sit_i->min_mtime = mtime;
1834 }
1835 sit_i->max_mtime = get_mtime(sbi);
1836 mutex_unlock(&sit_i->sentry_lock);
1837}
1838
1839int build_segment_manager(struct f2fs_sb_info *sbi)
1840{
1841 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1842 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Namjae Jeon1042d602012-12-01 10:56:13 +09001843 struct f2fs_sm_info *sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001844 int err;
1845
1846 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
1847 if (!sm_info)
1848 return -ENOMEM;
1849
1850 /* init sm info */
1851 sbi->sm_info = sm_info;
1852 INIT_LIST_HEAD(&sm_info->wblist_head);
1853 spin_lock_init(&sm_info->wblist_lock);
1854 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
1855 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
1856 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
1857 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
1858 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
1859 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
1860 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
Jaegeuk Kim81eb8d62013-10-24 13:31:34 +09001861 sm_info->rec_prefree_segments = DEF_RECLAIM_PREFREE_SEGMENTS;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001862
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09001863 INIT_LIST_HEAD(&sm_info->discard_list);
1864 sm_info->nr_discards = 0;
1865 sm_info->max_discards = 0;
1866
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001867 err = build_sit_info(sbi);
1868 if (err)
1869 return err;
1870 err = build_free_segmap(sbi);
1871 if (err)
1872 return err;
1873 err = build_curseg(sbi);
1874 if (err)
1875 return err;
1876
1877 /* reinit free segmap based on SIT */
1878 build_sit_entries(sbi);
1879
1880 init_free_segmap(sbi);
1881 err = build_dirty_segmap(sbi);
1882 if (err)
1883 return err;
1884
1885 init_min_max_mtime(sbi);
1886 return 0;
1887}
1888
1889static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
1890 enum dirty_type dirty_type)
1891{
1892 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1893
1894 mutex_lock(&dirty_i->seglist_lock);
1895 kfree(dirty_i->dirty_segmap[dirty_type]);
1896 dirty_i->nr_dirty[dirty_type] = 0;
1897 mutex_unlock(&dirty_i->seglist_lock);
1898}
1899
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001900static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001901{
1902 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001903 kfree(dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001904}
1905
1906static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
1907{
1908 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1909 int i;
1910
1911 if (!dirty_i)
1912 return;
1913
1914 /* discard pre-free/dirty segments list */
1915 for (i = 0; i < NR_DIRTY_TYPE; i++)
1916 discard_dirty_segmap(sbi, i);
1917
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001918 destroy_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001919 SM_I(sbi)->dirty_info = NULL;
1920 kfree(dirty_i);
1921}
1922
1923static void destroy_curseg(struct f2fs_sb_info *sbi)
1924{
1925 struct curseg_info *array = SM_I(sbi)->curseg_array;
1926 int i;
1927
1928 if (!array)
1929 return;
1930 SM_I(sbi)->curseg_array = NULL;
1931 for (i = 0; i < NR_CURSEG_TYPE; i++)
1932 kfree(array[i].sum_blk);
1933 kfree(array);
1934}
1935
1936static void destroy_free_segmap(struct f2fs_sb_info *sbi)
1937{
1938 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
1939 if (!free_i)
1940 return;
1941 SM_I(sbi)->free_info = NULL;
1942 kfree(free_i->free_segmap);
1943 kfree(free_i->free_secmap);
1944 kfree(free_i);
1945}
1946
1947static void destroy_sit_info(struct f2fs_sb_info *sbi)
1948{
1949 struct sit_info *sit_i = SIT_I(sbi);
1950 unsigned int start;
1951
1952 if (!sit_i)
1953 return;
1954
1955 if (sit_i->sentries) {
1956 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1957 kfree(sit_i->sentries[start].cur_valid_map);
1958 kfree(sit_i->sentries[start].ckpt_valid_map);
1959 }
1960 }
1961 vfree(sit_i->sentries);
1962 vfree(sit_i->sec_entries);
1963 kfree(sit_i->dirty_sentries_bitmap);
1964
1965 SM_I(sbi)->sit_info = NULL;
1966 kfree(sit_i->sit_bitmap);
1967 kfree(sit_i);
1968}
1969
1970void destroy_segment_manager(struct f2fs_sb_info *sbi)
1971{
1972 struct f2fs_sm_info *sm_info = SM_I(sbi);
Chao Yu3b03f722013-11-06 09:12:04 +08001973 if (!sm_info)
1974 return;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001975 destroy_dirty_segmap(sbi);
1976 destroy_curseg(sbi);
1977 destroy_free_segmap(sbi);
1978 destroy_sit_info(sbi);
1979 sbi->sm_info = NULL;
1980 kfree(sm_info);
1981}
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09001982
1983int __init create_segment_manager_caches(void)
1984{
1985 discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
1986 sizeof(struct discard_entry), NULL);
1987 if (!discard_entry_slab)
1988 return -ENOMEM;
1989 return 0;
1990}
1991
1992void destroy_segment_manager_caches(void)
1993{
1994 kmem_cache_destroy(discard_entry_slab);
1995}