blob: c51fa4bee60bf087ea2e4482bc58807c7a3ba56b [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 Kim37208872013-11-12 16:55:17 +0900269static void f2fs_issue_discard(struct f2fs_sb_info *sbi,
270 block_t blkstart, block_t blklen)
271{
272 sector_t start = ((sector_t)blkstart) << sbi->log_sectors_per_block;
273 sector_t len = ((sector_t)blklen) << sbi->log_sectors_per_block;
274 blkdev_issue_discard(sbi->sb->s_bdev, start, len, GFP_NOFS, 0);
275}
276
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900277static void add_discard_addrs(struct f2fs_sb_info *sbi,
278 unsigned int segno, struct seg_entry *se)
279{
280 struct list_head *head = &SM_I(sbi)->discard_list;
281 struct discard_entry *new;
282 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
283 int max_blocks = sbi->blocks_per_seg;
284 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
285 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
286 unsigned long dmap[entries];
287 unsigned int start = 0, end = -1;
288 int i;
289
290 if (!test_opt(sbi, DISCARD))
291 return;
292
293 /* zero block will be discarded through the prefree list */
294 if (!se->valid_blocks || se->valid_blocks == max_blocks)
295 return;
296
297 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
298 for (i = 0; i < entries; i++)
299 dmap[i] = (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
300
301 while (SM_I(sbi)->nr_discards <= SM_I(sbi)->max_discards) {
302 start = __find_rev_next_bit(dmap, max_blocks, end + 1);
303 if (start >= max_blocks)
304 break;
305
306 end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
307
308 new = f2fs_kmem_cache_alloc(discard_entry_slab, GFP_NOFS);
309 INIT_LIST_HEAD(&new->list);
310 new->blkaddr = START_BLOCK(sbi, segno) + start;
311 new->len = end - start;
312
313 list_add_tail(&new->list, head);
314 SM_I(sbi)->nr_discards += end - start;
315 }
316}
317
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900318/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900319 * Should call clear_prefree_segments after checkpoint is done.
320 */
321static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
322{
323 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Haicheng Li81fb5e82013-05-14 18:20:28 +0800324 unsigned int segno = -1;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900325 unsigned int total_segs = TOTAL_SEGS(sbi);
326
327 mutex_lock(&dirty_i->seglist_lock);
328 while (1) {
329 segno = find_next_bit(dirty_i->dirty_segmap[PRE], total_segs,
Haicheng Li81fb5e82013-05-14 18:20:28 +0800330 segno + 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900331 if (segno >= total_segs)
332 break;
333 __set_test_and_free(sbi, segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900334 }
335 mutex_unlock(&dirty_i->seglist_lock);
336}
337
338void clear_prefree_segments(struct f2fs_sb_info *sbi)
339{
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900340 struct list_head *head = &(SM_I(sbi)->discard_list);
341 struct list_head *this, *next;
342 struct discard_entry *entry;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900343 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Changman Lee29e59c12013-11-11 09:24:37 +0900344 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900345 unsigned int total_segs = TOTAL_SEGS(sbi);
Changman Lee29e59c12013-11-11 09:24:37 +0900346 unsigned int start = 0, end = -1;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900347
348 mutex_lock(&dirty_i->seglist_lock);
Changman Lee29e59c12013-11-11 09:24:37 +0900349
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900350 while (1) {
Changman Lee29e59c12013-11-11 09:24:37 +0900351 int i;
352 start = find_next_bit(prefree_map, total_segs, end + 1);
353 if (start >= total_segs)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900354 break;
Changman Lee29e59c12013-11-11 09:24:37 +0900355 end = find_next_zero_bit(prefree_map, total_segs, start + 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900356
Changman Lee29e59c12013-11-11 09:24:37 +0900357 for (i = start; i < end; i++)
358 clear_bit(i, prefree_map);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900359
Changman Lee29e59c12013-11-11 09:24:37 +0900360 dirty_i->nr_dirty[PRE] -= end - start;
361
362 if (!test_opt(sbi, DISCARD))
363 continue;
364
Jaegeuk Kim37208872013-11-12 16:55:17 +0900365 f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
366 (end - start) << sbi->log_blocks_per_seg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900367 }
368 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900369
370 /* send small discards */
371 list_for_each_safe(this, next, head) {
372 entry = list_entry(this, struct discard_entry, list);
Jaegeuk Kim37208872013-11-12 16:55:17 +0900373 f2fs_issue_discard(sbi, entry->blkaddr, entry->len);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900374 list_del(&entry->list);
375 SM_I(sbi)->nr_discards -= entry->len;
376 kmem_cache_free(discard_entry_slab, entry);
377 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900378}
379
380static void __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
381{
382 struct sit_info *sit_i = SIT_I(sbi);
383 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap))
384 sit_i->dirty_sentries++;
385}
386
387static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
388 unsigned int segno, int modified)
389{
390 struct seg_entry *se = get_seg_entry(sbi, segno);
391 se->type = type;
392 if (modified)
393 __mark_sit_entry_dirty(sbi, segno);
394}
395
396static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
397{
398 struct seg_entry *se;
399 unsigned int segno, offset;
400 long int new_vblocks;
401
402 segno = GET_SEGNO(sbi, blkaddr);
403
404 se = get_seg_entry(sbi, segno);
405 new_vblocks = se->valid_blocks + del;
406 offset = GET_SEGOFF_FROM_SEG0(sbi, blkaddr) & (sbi->blocks_per_seg - 1);
407
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900408 f2fs_bug_on((new_vblocks >> (sizeof(unsigned short) << 3) ||
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900409 (new_vblocks > sbi->blocks_per_seg)));
410
411 se->valid_blocks = new_vblocks;
412 se->mtime = get_mtime(sbi);
413 SIT_I(sbi)->max_mtime = se->mtime;
414
415 /* Update valid block bitmap */
416 if (del > 0) {
417 if (f2fs_set_bit(offset, se->cur_valid_map))
418 BUG();
419 } else {
420 if (!f2fs_clear_bit(offset, se->cur_valid_map))
421 BUG();
422 }
423 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
424 se->ckpt_valid_blocks += del;
425
426 __mark_sit_entry_dirty(sbi, segno);
427
428 /* update total number of valid blocks to be written in ckpt area */
429 SIT_I(sbi)->written_valid_blocks += del;
430
431 if (sbi->segs_per_sec > 1)
432 get_sec_entry(sbi, segno)->valid_blocks += del;
433}
434
435static void refresh_sit_entry(struct f2fs_sb_info *sbi,
436 block_t old_blkaddr, block_t new_blkaddr)
437{
438 update_sit_entry(sbi, new_blkaddr, 1);
439 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
440 update_sit_entry(sbi, old_blkaddr, -1);
441}
442
443void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
444{
445 unsigned int segno = GET_SEGNO(sbi, addr);
446 struct sit_info *sit_i = SIT_I(sbi);
447
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900448 f2fs_bug_on(addr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900449 if (addr == NEW_ADDR)
450 return;
451
452 /* add it into sit main buffer */
453 mutex_lock(&sit_i->sentry_lock);
454
455 update_sit_entry(sbi, addr, -1);
456
457 /* add it into dirty seglist */
458 locate_dirty_segment(sbi, segno);
459
460 mutex_unlock(&sit_i->sentry_lock);
461}
462
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900463/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900464 * This function should be resided under the curseg_mutex lock
465 */
466static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
Haicheng Lie79efe32013-06-13 16:59:27 +0800467 struct f2fs_summary *sum)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900468{
469 struct curseg_info *curseg = CURSEG_I(sbi, type);
470 void *addr = curseg->sum_blk;
Haicheng Lie79efe32013-06-13 16:59:27 +0800471 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900472 memcpy(addr, sum, sizeof(struct f2fs_summary));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900473}
474
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900475/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900476 * Calculate the number of current summary pages for writing
477 */
478int npages_for_summary_flush(struct f2fs_sb_info *sbi)
479{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900480 int valid_sum_count = 0;
Fan Li9a479382013-10-29 16:21:47 +0800481 int i, sum_in_page;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900482
483 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
484 if (sbi->ckpt->alloc_type[i] == SSR)
485 valid_sum_count += sbi->blocks_per_seg;
486 else
487 valid_sum_count += curseg_blkoff(sbi, i);
488 }
489
Fan Li9a479382013-10-29 16:21:47 +0800490 sum_in_page = (PAGE_CACHE_SIZE - 2 * SUM_JOURNAL_SIZE -
491 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
492 if (valid_sum_count <= sum_in_page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900493 return 1;
Fan Li9a479382013-10-29 16:21:47 +0800494 else if ((valid_sum_count - sum_in_page) <=
495 (PAGE_CACHE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900496 return 2;
497 return 3;
498}
499
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900500/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900501 * Caller should put this summary page
502 */
503struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
504{
505 return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
506}
507
508static void write_sum_page(struct f2fs_sb_info *sbi,
509 struct f2fs_summary_block *sum_blk, block_t blk_addr)
510{
511 struct page *page = grab_meta_page(sbi, blk_addr);
512 void *kaddr = page_address(page);
513 memcpy(kaddr, sum_blk, PAGE_CACHE_SIZE);
514 set_page_dirty(page);
515 f2fs_put_page(page, 1);
516}
517
Jaegeuk Kim60374682013-03-31 13:58:51 +0900518static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
519{
520 struct curseg_info *curseg = CURSEG_I(sbi, type);
Haicheng Li81fb5e82013-05-14 18:20:28 +0800521 unsigned int segno = curseg->segno + 1;
Jaegeuk Kim60374682013-03-31 13:58:51 +0900522 struct free_segmap_info *free_i = FREE_I(sbi);
523
Haicheng Li81fb5e82013-05-14 18:20:28 +0800524 if (segno < TOTAL_SEGS(sbi) && segno % sbi->segs_per_sec)
525 return !test_bit(segno, free_i->free_segmap);
Jaegeuk Kim60374682013-03-31 13:58:51 +0900526 return 0;
527}
528
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900529/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900530 * Find a new segment from the free segments bitmap to right order
531 * This function should be returned with success, otherwise BUG
532 */
533static void get_new_segment(struct f2fs_sb_info *sbi,
534 unsigned int *newseg, bool new_sec, int dir)
535{
536 struct free_segmap_info *free_i = FREE_I(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900537 unsigned int segno, secno, zoneno;
Jaegeuk Kim53cf9522013-03-31 12:39:49 +0900538 unsigned int total_zones = TOTAL_SECS(sbi) / sbi->secs_per_zone;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900539 unsigned int hint = *newseg / sbi->segs_per_sec;
540 unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
541 unsigned int left_start = hint;
542 bool init = true;
543 int go_left = 0;
544 int i;
545
546 write_lock(&free_i->segmap_lock);
547
548 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
549 segno = find_next_zero_bit(free_i->free_segmap,
550 TOTAL_SEGS(sbi), *newseg + 1);
Jaegeuk Kim33afa7f2013-03-31 12:59:53 +0900551 if (segno - *newseg < sbi->segs_per_sec -
552 (*newseg % sbi->segs_per_sec))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900553 goto got_it;
554 }
555find_other_zone:
Jaegeuk Kim53cf9522013-03-31 12:39:49 +0900556 secno = find_next_zero_bit(free_i->free_secmap, TOTAL_SECS(sbi), hint);
557 if (secno >= TOTAL_SECS(sbi)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900558 if (dir == ALLOC_RIGHT) {
559 secno = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim53cf9522013-03-31 12:39:49 +0900560 TOTAL_SECS(sbi), 0);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900561 f2fs_bug_on(secno >= TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900562 } else {
563 go_left = 1;
564 left_start = hint - 1;
565 }
566 }
567 if (go_left == 0)
568 goto skip_left;
569
570 while (test_bit(left_start, free_i->free_secmap)) {
571 if (left_start > 0) {
572 left_start--;
573 continue;
574 }
575 left_start = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim53cf9522013-03-31 12:39:49 +0900576 TOTAL_SECS(sbi), 0);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900577 f2fs_bug_on(left_start >= TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900578 break;
579 }
580 secno = left_start;
581skip_left:
582 hint = secno;
583 segno = secno * sbi->segs_per_sec;
584 zoneno = secno / sbi->secs_per_zone;
585
586 /* give up on finding another zone */
587 if (!init)
588 goto got_it;
589 if (sbi->secs_per_zone == 1)
590 goto got_it;
591 if (zoneno == old_zoneno)
592 goto got_it;
593 if (dir == ALLOC_LEFT) {
594 if (!go_left && zoneno + 1 >= total_zones)
595 goto got_it;
596 if (go_left && zoneno == 0)
597 goto got_it;
598 }
599 for (i = 0; i < NR_CURSEG_TYPE; i++)
600 if (CURSEG_I(sbi, i)->zone == zoneno)
601 break;
602
603 if (i < NR_CURSEG_TYPE) {
604 /* zone is in user, try another */
605 if (go_left)
606 hint = zoneno * sbi->secs_per_zone - 1;
607 else if (zoneno + 1 >= total_zones)
608 hint = 0;
609 else
610 hint = (zoneno + 1) * sbi->secs_per_zone;
611 init = false;
612 goto find_other_zone;
613 }
614got_it:
615 /* set it as dirty segment in free segmap */
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900616 f2fs_bug_on(test_bit(segno, free_i->free_segmap));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900617 __set_inuse(sbi, segno);
618 *newseg = segno;
619 write_unlock(&free_i->segmap_lock);
620}
621
622static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
623{
624 struct curseg_info *curseg = CURSEG_I(sbi, type);
625 struct summary_footer *sum_footer;
626
627 curseg->segno = curseg->next_segno;
628 curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
629 curseg->next_blkoff = 0;
630 curseg->next_segno = NULL_SEGNO;
631
632 sum_footer = &(curseg->sum_blk->footer);
633 memset(sum_footer, 0, sizeof(struct summary_footer));
634 if (IS_DATASEG(type))
635 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
636 if (IS_NODESEG(type))
637 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
638 __set_sit_entry_type(sbi, type, curseg->segno, modified);
639}
640
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900641/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900642 * Allocate a current working segment.
643 * This function always allocates a free segment in LFS manner.
644 */
645static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
646{
647 struct curseg_info *curseg = CURSEG_I(sbi, type);
648 unsigned int segno = curseg->segno;
649 int dir = ALLOC_LEFT;
650
651 write_sum_page(sbi, curseg->sum_blk,
Haicheng Li81fb5e82013-05-14 18:20:28 +0800652 GET_SUM_BLOCK(sbi, segno));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900653 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
654 dir = ALLOC_RIGHT;
655
656 if (test_opt(sbi, NOHEAP))
657 dir = ALLOC_RIGHT;
658
659 get_new_segment(sbi, &segno, new_sec, dir);
660 curseg->next_segno = segno;
661 reset_curseg(sbi, type, 1);
662 curseg->alloc_type = LFS;
663}
664
665static void __next_free_blkoff(struct f2fs_sb_info *sbi,
666 struct curseg_info *seg, block_t start)
667{
668 struct seg_entry *se = get_seg_entry(sbi, seg->segno);
Changman Leee81c93c2013-11-15 13:21:16 +0900669 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
670 unsigned long target_map[entries];
671 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
672 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
673 int i, pos;
674
675 for (i = 0; i < entries; i++)
676 target_map[i] = ckpt_map[i] | cur_map[i];
677
678 pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
679
680 seg->next_blkoff = pos;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900681}
682
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900683/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900684 * If a segment is written by LFS manner, next block offset is just obtained
685 * by increasing the current block offset. However, if a segment is written by
686 * SSR manner, next block offset obtained by calling __next_free_blkoff
687 */
688static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
689 struct curseg_info *seg)
690{
691 if (seg->alloc_type == SSR)
692 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
693 else
694 seg->next_blkoff++;
695}
696
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900697/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900698 * This function always allocates a used segment (from dirty seglist) by SSR
699 * manner, so it should recover the existing segment information of valid blocks
700 */
701static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
702{
703 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
704 struct curseg_info *curseg = CURSEG_I(sbi, type);
705 unsigned int new_segno = curseg->next_segno;
706 struct f2fs_summary_block *sum_node;
707 struct page *sum_page;
708
709 write_sum_page(sbi, curseg->sum_blk,
710 GET_SUM_BLOCK(sbi, curseg->segno));
711 __set_test_and_inuse(sbi, new_segno);
712
713 mutex_lock(&dirty_i->seglist_lock);
714 __remove_dirty_segment(sbi, new_segno, PRE);
715 __remove_dirty_segment(sbi, new_segno, DIRTY);
716 mutex_unlock(&dirty_i->seglist_lock);
717
718 reset_curseg(sbi, type, 1);
719 curseg->alloc_type = SSR;
720 __next_free_blkoff(sbi, curseg, 0);
721
722 if (reuse) {
723 sum_page = get_sum_page(sbi, new_segno);
724 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
725 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
726 f2fs_put_page(sum_page, 1);
727 }
728}
729
Jaegeuk Kim43727522013-02-04 15:11:17 +0900730static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
731{
732 struct curseg_info *curseg = CURSEG_I(sbi, type);
733 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
734
735 if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0))
736 return v_ops->get_victim(sbi,
737 &(curseg)->next_segno, BG_GC, type, SSR);
738
739 /* For data segments, let's do SSR more intensively */
740 for (; type >= CURSEG_HOT_DATA; type--)
741 if (v_ops->get_victim(sbi, &(curseg)->next_segno,
742 BG_GC, type, SSR))
743 return 1;
744 return 0;
745}
746
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900747/*
748 * flush out current segment and replace it with new segment
749 * This function should be returned with success, otherwise BUG
750 */
751static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
752 int type, bool force)
753{
754 struct curseg_info *curseg = CURSEG_I(sbi, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900755
Gu Zheng7b405272013-08-19 09:41:15 +0800756 if (force)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900757 new_curseg(sbi, type, true);
Gu Zheng7b405272013-08-19 09:41:15 +0800758 else if (type == CURSEG_WARM_NODE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900759 new_curseg(sbi, type, false);
Jaegeuk Kim60374682013-03-31 13:58:51 +0900760 else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
761 new_curseg(sbi, type, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900762 else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
763 change_curseg(sbi, type, true);
764 else
765 new_curseg(sbi, type, false);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +0900766
767 stat_inc_seg_type(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900768}
769
770void allocate_new_segments(struct f2fs_sb_info *sbi)
771{
772 struct curseg_info *curseg;
773 unsigned int old_curseg;
774 int i;
775
776 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
777 curseg = CURSEG_I(sbi, i);
778 old_curseg = curseg->segno;
779 SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
780 locate_dirty_segment(sbi, old_curseg);
781 }
782}
783
784static const struct segment_allocation default_salloc_ops = {
785 .allocate_segment = allocate_segment_by_default,
786};
787
788static void f2fs_end_io_write(struct bio *bio, int err)
789{
790 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
791 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
792 struct bio_private *p = bio->bi_private;
793
794 do {
795 struct page *page = bvec->bv_page;
796
797 if (--bvec >= bio->bi_io_vec)
798 prefetchw(&bvec->bv_page->flags);
799 if (!uptodate) {
800 SetPageError(page);
801 if (page->mapping)
802 set_bit(AS_EIO, &page->mapping->flags);
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900803 set_ckpt_flags(p->sbi->ckpt, CP_ERROR_FLAG);
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900804 p->sbi->sb->s_flags |= MS_RDONLY;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900805 }
806 end_page_writeback(page);
807 dec_page_count(p->sbi, F2FS_WRITEBACK);
808 } while (bvec >= bio->bi_io_vec);
809
810 if (p->is_sync)
811 complete(p->wait);
Gu Zhenge2340882013-10-14 18:45:56 +0800812
Changman Leefb51b5e2013-11-07 12:48:25 +0900813 if (!get_pages(p->sbi, F2FS_WRITEBACK) &&
814 !list_empty(&p->sbi->cp_wait.task_list))
815 wake_up(&p->sbi->cp_wait);
Gu Zhenge2340882013-10-14 18:45:56 +0800816
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900817 kfree(p);
818 bio_put(bio);
819}
820
Jaegeuk Kim3cd8a232012-12-10 09:26:05 +0900821struct bio *f2fs_bio_alloc(struct block_device *bdev, int npages)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900822{
823 struct bio *bio;
Jaegeuk Kim3cd8a232012-12-10 09:26:05 +0900824
825 /* No failure on bio allocation */
826 bio = bio_alloc(GFP_NOIO, npages);
827 bio->bi_bdev = bdev;
Gu Zhengd8207f62013-07-25 11:30:01 +0800828 bio->bi_private = NULL;
829
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900830 return bio;
831}
832
833static void do_submit_bio(struct f2fs_sb_info *sbi,
834 enum page_type type, bool sync)
835{
836 int rw = sync ? WRITE_SYNC : WRITE;
837 enum page_type btype = type > META ? META : type;
838
839 if (type >= META_FLUSH)
840 rw = WRITE_FLUSH_FUA;
841
Namjae Jeon86804412013-04-25 11:45:21 +0900842 if (btype == META)
843 rw |= REQ_META;
844
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900845 if (sbi->bio[btype]) {
846 struct bio_private *p = sbi->bio[btype]->bi_private;
847 p->sbi = sbi;
848 sbi->bio[btype]->bi_end_io = f2fs_end_io_write;
Namjae Jeon6ec178d2013-04-23 17:51:43 +0900849
850 trace_f2fs_do_submit_bio(sbi->sb, btype, sync, sbi->bio[btype]);
851
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900852 if (type == META_FLUSH) {
853 DECLARE_COMPLETION_ONSTACK(wait);
854 p->is_sync = true;
855 p->wait = &wait;
856 submit_bio(rw, sbi->bio[btype]);
857 wait_for_completion(&wait);
858 } else {
859 p->is_sync = false;
860 submit_bio(rw, sbi->bio[btype]);
861 }
862 sbi->bio[btype] = NULL;
863 }
864}
865
866void f2fs_submit_bio(struct f2fs_sb_info *sbi, enum page_type type, bool sync)
867{
868 down_write(&sbi->bio_sem);
869 do_submit_bio(sbi, type, sync);
870 up_write(&sbi->bio_sem);
871}
872
873static void submit_write_page(struct f2fs_sb_info *sbi, struct page *page,
874 block_t blk_addr, enum page_type type)
875{
876 struct block_device *bdev = sbi->sb->s_bdev;
Chao Yucc7b1bb2013-09-22 15:50:50 +0800877 int bio_blocks;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900878
879 verify_block_addr(sbi, blk_addr);
880
881 down_write(&sbi->bio_sem);
882
883 inc_page_count(sbi, F2FS_WRITEBACK);
884
885 if (sbi->bio[type] && sbi->last_block_in_bio[type] != blk_addr - 1)
886 do_submit_bio(sbi, type, false);
887alloc_new:
Jaegeuk Kim3cd8a232012-12-10 09:26:05 +0900888 if (sbi->bio[type] == NULL) {
Gu Zhengd8207f62013-07-25 11:30:01 +0800889 struct bio_private *priv;
890retry:
891 priv = kmalloc(sizeof(struct bio_private), GFP_NOFS);
892 if (!priv) {
893 cond_resched();
894 goto retry;
895 }
896
Chao Yucc7b1bb2013-09-22 15:50:50 +0800897 bio_blocks = MAX_BIO_BLOCKS(max_hw_blocks(sbi));
898 sbi->bio[type] = f2fs_bio_alloc(bdev, bio_blocks);
Jaegeuk Kim3cd8a232012-12-10 09:26:05 +0900899 sbi->bio[type]->bi_sector = SECTOR_FROM_BLOCK(sbi, blk_addr);
Gu Zhengd8207f62013-07-25 11:30:01 +0800900 sbi->bio[type]->bi_private = priv;
Jaegeuk Kim3cd8a232012-12-10 09:26:05 +0900901 /*
902 * The end_io will be assigned at the sumbission phase.
903 * Until then, let bio_add_page() merge consecutive IOs as much
904 * as possible.
905 */
906 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900907
908 if (bio_add_page(sbi->bio[type], page, PAGE_CACHE_SIZE, 0) <
909 PAGE_CACHE_SIZE) {
910 do_submit_bio(sbi, type, false);
911 goto alloc_new;
912 }
913
914 sbi->last_block_in_bio[type] = blk_addr;
915
916 up_write(&sbi->bio_sem);
Namjae Jeon6ec178d2013-04-23 17:51:43 +0900917 trace_f2fs_submit_write_page(page, blk_addr, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900918}
919
Jin Xua5694692013-08-05 20:02:04 +0800920void f2fs_wait_on_page_writeback(struct page *page,
921 enum page_type type, bool sync)
922{
923 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
924 if (PageWriteback(page)) {
925 f2fs_submit_bio(sbi, type, sync);
926 wait_on_page_writeback(page);
927 }
928}
929
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900930static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
931{
932 struct curseg_info *curseg = CURSEG_I(sbi, type);
933 if (curseg->next_blkoff < sbi->blocks_per_seg)
934 return true;
935 return false;
936}
937
938static int __get_segment_type_2(struct page *page, enum page_type p_type)
939{
940 if (p_type == DATA)
941 return CURSEG_HOT_DATA;
942 else
943 return CURSEG_HOT_NODE;
944}
945
946static int __get_segment_type_4(struct page *page, enum page_type p_type)
947{
948 if (p_type == DATA) {
949 struct inode *inode = page->mapping->host;
950
951 if (S_ISDIR(inode->i_mode))
952 return CURSEG_HOT_DATA;
953 else
954 return CURSEG_COLD_DATA;
955 } else {
956 if (IS_DNODE(page) && !is_cold_node(page))
957 return CURSEG_HOT_NODE;
958 else
959 return CURSEG_COLD_NODE;
960 }
961}
962
963static int __get_segment_type_6(struct page *page, enum page_type p_type)
964{
965 if (p_type == DATA) {
966 struct inode *inode = page->mapping->host;
967
968 if (S_ISDIR(inode->i_mode))
969 return CURSEG_HOT_DATA;
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900970 else if (is_cold_data(page) || file_is_cold(inode))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900971 return CURSEG_COLD_DATA;
972 else
973 return CURSEG_WARM_DATA;
974 } else {
975 if (IS_DNODE(page))
976 return is_cold_node(page) ? CURSEG_WARM_NODE :
977 CURSEG_HOT_NODE;
978 else
979 return CURSEG_COLD_NODE;
980 }
981}
982
983static int __get_segment_type(struct page *page, enum page_type p_type)
984{
985 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
986 switch (sbi->active_logs) {
987 case 2:
988 return __get_segment_type_2(page, p_type);
989 case 4:
990 return __get_segment_type_4(page, p_type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900991 }
Jaegeuk Kim12a67142012-12-21 11:47:05 +0900992 /* NR_CURSEG_TYPE(6) logs by default */
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900993 f2fs_bug_on(sbi->active_logs != NR_CURSEG_TYPE);
Jaegeuk Kim12a67142012-12-21 11:47:05 +0900994 return __get_segment_type_6(page, p_type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900995}
996
997static void do_write_page(struct f2fs_sb_info *sbi, struct page *page,
998 block_t old_blkaddr, block_t *new_blkaddr,
999 struct f2fs_summary *sum, enum page_type p_type)
1000{
1001 struct sit_info *sit_i = SIT_I(sbi);
1002 struct curseg_info *curseg;
1003 unsigned int old_cursegno;
1004 int type;
1005
1006 type = __get_segment_type(page, p_type);
1007 curseg = CURSEG_I(sbi, type);
1008
1009 mutex_lock(&curseg->curseg_mutex);
1010
1011 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
1012 old_cursegno = curseg->segno;
1013
1014 /*
1015 * __add_sum_entry should be resided under the curseg_mutex
1016 * because, this function updates a summary entry in the
1017 * current summary block.
1018 */
Haicheng Lie79efe32013-06-13 16:59:27 +08001019 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001020
1021 mutex_lock(&sit_i->sentry_lock);
1022 __refresh_next_blkoff(sbi, curseg);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +09001023
1024 stat_inc_block_count(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001025
1026 /*
1027 * SIT information should be updated before segment allocation,
1028 * since SSR needs latest valid block information.
1029 */
1030 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
1031
1032 if (!__has_curseg_space(sbi, type))
1033 sit_i->s_ops->allocate_segment(sbi, type, false);
1034
1035 locate_dirty_segment(sbi, old_cursegno);
1036 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
1037 mutex_unlock(&sit_i->sentry_lock);
1038
1039 if (p_type == NODE)
1040 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
1041
1042 /* writeout dirty page into bdev */
1043 submit_write_page(sbi, page, *new_blkaddr, p_type);
1044
1045 mutex_unlock(&curseg->curseg_mutex);
1046}
1047
Jaegeuk Kim577e3492013-01-24 19:56:11 +09001048void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001049{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001050 set_page_writeback(page);
1051 submit_write_page(sbi, page, page->index, META);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001052}
1053
1054void write_node_page(struct f2fs_sb_info *sbi, struct page *page,
1055 unsigned int nid, block_t old_blkaddr, block_t *new_blkaddr)
1056{
1057 struct f2fs_summary sum;
1058 set_summary(&sum, nid, 0, 0);
1059 do_write_page(sbi, page, old_blkaddr, new_blkaddr, &sum, NODE);
1060}
1061
1062void write_data_page(struct inode *inode, struct page *page,
1063 struct dnode_of_data *dn, block_t old_blkaddr,
1064 block_t *new_blkaddr)
1065{
1066 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1067 struct f2fs_summary sum;
1068 struct node_info ni;
1069
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001070 f2fs_bug_on(old_blkaddr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001071 get_node_info(sbi, dn->nid, &ni);
1072 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1073
1074 do_write_page(sbi, page, old_blkaddr,
1075 new_blkaddr, &sum, DATA);
1076}
1077
1078void rewrite_data_page(struct f2fs_sb_info *sbi, struct page *page,
1079 block_t old_blk_addr)
1080{
1081 submit_write_page(sbi, page, old_blk_addr, DATA);
1082}
1083
1084void recover_data_page(struct f2fs_sb_info *sbi,
1085 struct page *page, struct f2fs_summary *sum,
1086 block_t old_blkaddr, block_t new_blkaddr)
1087{
1088 struct sit_info *sit_i = SIT_I(sbi);
1089 struct curseg_info *curseg;
1090 unsigned int segno, old_cursegno;
1091 struct seg_entry *se;
1092 int type;
1093
1094 segno = GET_SEGNO(sbi, new_blkaddr);
1095 se = get_seg_entry(sbi, segno);
1096 type = se->type;
1097
1098 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
1099 if (old_blkaddr == NULL_ADDR)
1100 type = CURSEG_COLD_DATA;
1101 else
1102 type = CURSEG_WARM_DATA;
1103 }
1104 curseg = CURSEG_I(sbi, type);
1105
1106 mutex_lock(&curseg->curseg_mutex);
1107 mutex_lock(&sit_i->sentry_lock);
1108
1109 old_cursegno = curseg->segno;
1110
1111 /* change the current segment */
1112 if (segno != curseg->segno) {
1113 curseg->next_segno = segno;
1114 change_curseg(sbi, type, true);
1115 }
1116
1117 curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, new_blkaddr) &
1118 (sbi->blocks_per_seg - 1);
Haicheng Lie79efe32013-06-13 16:59:27 +08001119 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001120
1121 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
1122
1123 locate_dirty_segment(sbi, old_cursegno);
1124 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
1125
1126 mutex_unlock(&sit_i->sentry_lock);
1127 mutex_unlock(&curseg->curseg_mutex);
1128}
1129
1130void rewrite_node_page(struct f2fs_sb_info *sbi,
1131 struct page *page, struct f2fs_summary *sum,
1132 block_t old_blkaddr, block_t new_blkaddr)
1133{
1134 struct sit_info *sit_i = SIT_I(sbi);
1135 int type = CURSEG_WARM_NODE;
1136 struct curseg_info *curseg;
1137 unsigned int segno, old_cursegno;
1138 block_t next_blkaddr = next_blkaddr_of_node(page);
1139 unsigned int next_segno = GET_SEGNO(sbi, next_blkaddr);
1140
1141 curseg = CURSEG_I(sbi, type);
1142
1143 mutex_lock(&curseg->curseg_mutex);
1144 mutex_lock(&sit_i->sentry_lock);
1145
1146 segno = GET_SEGNO(sbi, new_blkaddr);
1147 old_cursegno = curseg->segno;
1148
1149 /* change the current segment */
1150 if (segno != curseg->segno) {
1151 curseg->next_segno = segno;
1152 change_curseg(sbi, type, true);
1153 }
1154 curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, new_blkaddr) &
1155 (sbi->blocks_per_seg - 1);
Haicheng Lie79efe32013-06-13 16:59:27 +08001156 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001157
1158 /* change the current log to the next block addr in advance */
1159 if (next_segno != segno) {
1160 curseg->next_segno = next_segno;
1161 change_curseg(sbi, type, true);
1162 }
1163 curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, next_blkaddr) &
1164 (sbi->blocks_per_seg - 1);
1165
1166 /* rewrite node page */
1167 set_page_writeback(page);
1168 submit_write_page(sbi, page, new_blkaddr, NODE);
1169 f2fs_submit_bio(sbi, NODE, true);
1170 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
1171
1172 locate_dirty_segment(sbi, old_cursegno);
1173 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
1174
1175 mutex_unlock(&sit_i->sentry_lock);
1176 mutex_unlock(&curseg->curseg_mutex);
1177}
1178
1179static int read_compacted_summaries(struct f2fs_sb_info *sbi)
1180{
1181 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1182 struct curseg_info *seg_i;
1183 unsigned char *kaddr;
1184 struct page *page;
1185 block_t start;
1186 int i, j, offset;
1187
1188 start = start_sum_block(sbi);
1189
1190 page = get_meta_page(sbi, start++);
1191 kaddr = (unsigned char *)page_address(page);
1192
1193 /* Step 1: restore nat cache */
1194 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1195 memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
1196
1197 /* Step 2: restore sit cache */
1198 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1199 memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
1200 SUM_JOURNAL_SIZE);
1201 offset = 2 * SUM_JOURNAL_SIZE;
1202
1203 /* Step 3: restore summary entries */
1204 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1205 unsigned short blk_off;
1206 unsigned int segno;
1207
1208 seg_i = CURSEG_I(sbi, i);
1209 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
1210 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
1211 seg_i->next_segno = segno;
1212 reset_curseg(sbi, i, 0);
1213 seg_i->alloc_type = ckpt->alloc_type[i];
1214 seg_i->next_blkoff = blk_off;
1215
1216 if (seg_i->alloc_type == SSR)
1217 blk_off = sbi->blocks_per_seg;
1218
1219 for (j = 0; j < blk_off; j++) {
1220 struct f2fs_summary *s;
1221 s = (struct f2fs_summary *)(kaddr + offset);
1222 seg_i->sum_blk->entries[j] = *s;
1223 offset += SUMMARY_SIZE;
1224 if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1225 SUM_FOOTER_SIZE)
1226 continue;
1227
1228 f2fs_put_page(page, 1);
1229 page = NULL;
1230
1231 page = get_meta_page(sbi, start++);
1232 kaddr = (unsigned char *)page_address(page);
1233 offset = 0;
1234 }
1235 }
1236 f2fs_put_page(page, 1);
1237 return 0;
1238}
1239
1240static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
1241{
1242 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1243 struct f2fs_summary_block *sum;
1244 struct curseg_info *curseg;
1245 struct page *new;
1246 unsigned short blk_off;
1247 unsigned int segno = 0;
1248 block_t blk_addr = 0;
1249
1250 /* get segment number and block addr */
1251 if (IS_DATASEG(type)) {
1252 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
1253 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
1254 CURSEG_HOT_DATA]);
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001255 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001256 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
1257 else
1258 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
1259 } else {
1260 segno = le32_to_cpu(ckpt->cur_node_segno[type -
1261 CURSEG_HOT_NODE]);
1262 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
1263 CURSEG_HOT_NODE]);
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001264 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001265 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
1266 type - CURSEG_HOT_NODE);
1267 else
1268 blk_addr = GET_SUM_BLOCK(sbi, segno);
1269 }
1270
1271 new = get_meta_page(sbi, blk_addr);
1272 sum = (struct f2fs_summary_block *)page_address(new);
1273
1274 if (IS_NODESEG(type)) {
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001275 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001276 struct f2fs_summary *ns = &sum->entries[0];
1277 int i;
1278 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
1279 ns->version = 0;
1280 ns->ofs_in_node = 0;
1281 }
1282 } else {
1283 if (restore_node_summary(sbi, segno, sum)) {
1284 f2fs_put_page(new, 1);
1285 return -EINVAL;
1286 }
1287 }
1288 }
1289
1290 /* set uncompleted segment to curseg */
1291 curseg = CURSEG_I(sbi, type);
1292 mutex_lock(&curseg->curseg_mutex);
1293 memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
1294 curseg->next_segno = segno;
1295 reset_curseg(sbi, type, 0);
1296 curseg->alloc_type = ckpt->alloc_type[type];
1297 curseg->next_blkoff = blk_off;
1298 mutex_unlock(&curseg->curseg_mutex);
1299 f2fs_put_page(new, 1);
1300 return 0;
1301}
1302
1303static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
1304{
1305 int type = CURSEG_HOT_DATA;
1306
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001307 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001308 /* restore for compacted data summary */
1309 if (read_compacted_summaries(sbi))
1310 return -EINVAL;
1311 type = CURSEG_HOT_NODE;
1312 }
1313
1314 for (; type <= CURSEG_COLD_NODE; type++)
1315 if (read_normal_summaries(sbi, type))
1316 return -EINVAL;
1317 return 0;
1318}
1319
1320static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
1321{
1322 struct page *page;
1323 unsigned char *kaddr;
1324 struct f2fs_summary *summary;
1325 struct curseg_info *seg_i;
1326 int written_size = 0;
1327 int i, j;
1328
1329 page = grab_meta_page(sbi, blkaddr++);
1330 kaddr = (unsigned char *)page_address(page);
1331
1332 /* Step 1: write nat cache */
1333 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1334 memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
1335 written_size += SUM_JOURNAL_SIZE;
1336
1337 /* Step 2: write sit cache */
1338 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1339 memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
1340 SUM_JOURNAL_SIZE);
1341 written_size += SUM_JOURNAL_SIZE;
1342
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001343 /* Step 3: write summary entries */
1344 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1345 unsigned short blkoff;
1346 seg_i = CURSEG_I(sbi, i);
1347 if (sbi->ckpt->alloc_type[i] == SSR)
1348 blkoff = sbi->blocks_per_seg;
1349 else
1350 blkoff = curseg_blkoff(sbi, i);
1351
1352 for (j = 0; j < blkoff; j++) {
1353 if (!page) {
1354 page = grab_meta_page(sbi, blkaddr++);
1355 kaddr = (unsigned char *)page_address(page);
1356 written_size = 0;
1357 }
1358 summary = (struct f2fs_summary *)(kaddr + written_size);
1359 *summary = seg_i->sum_blk->entries[j];
1360 written_size += SUMMARY_SIZE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001361
1362 if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1363 SUM_FOOTER_SIZE)
1364 continue;
1365
Chao Yue8d61a72013-10-24 15:08:28 +08001366 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001367 f2fs_put_page(page, 1);
1368 page = NULL;
1369 }
1370 }
Chao Yue8d61a72013-10-24 15:08:28 +08001371 if (page) {
1372 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001373 f2fs_put_page(page, 1);
Chao Yue8d61a72013-10-24 15:08:28 +08001374 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001375}
1376
1377static void write_normal_summaries(struct f2fs_sb_info *sbi,
1378 block_t blkaddr, int type)
1379{
1380 int i, end;
1381 if (IS_DATASEG(type))
1382 end = type + NR_CURSEG_DATA_TYPE;
1383 else
1384 end = type + NR_CURSEG_NODE_TYPE;
1385
1386 for (i = type; i < end; i++) {
1387 struct curseg_info *sum = CURSEG_I(sbi, i);
1388 mutex_lock(&sum->curseg_mutex);
1389 write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
1390 mutex_unlock(&sum->curseg_mutex);
1391 }
1392}
1393
1394void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1395{
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001396 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001397 write_compacted_summaries(sbi, start_blk);
1398 else
1399 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
1400}
1401
1402void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1403{
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001404 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001405 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001406}
1407
1408int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
1409 unsigned int val, int alloc)
1410{
1411 int i;
1412
1413 if (type == NAT_JOURNAL) {
1414 for (i = 0; i < nats_in_cursum(sum); i++) {
1415 if (le32_to_cpu(nid_in_journal(sum, i)) == val)
1416 return i;
1417 }
1418 if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
1419 return update_nats_in_cursum(sum, 1);
1420 } else if (type == SIT_JOURNAL) {
1421 for (i = 0; i < sits_in_cursum(sum); i++)
1422 if (le32_to_cpu(segno_in_journal(sum, i)) == val)
1423 return i;
1424 if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
1425 return update_sits_in_cursum(sum, 1);
1426 }
1427 return -1;
1428}
1429
1430static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
1431 unsigned int segno)
1432{
1433 struct sit_info *sit_i = SIT_I(sbi);
1434 unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno);
1435 block_t blk_addr = sit_i->sit_base_addr + offset;
1436
1437 check_seg_range(sbi, segno);
1438
1439 /* calculate sit block address */
1440 if (f2fs_test_bit(offset, sit_i->sit_bitmap))
1441 blk_addr += sit_i->sit_blocks;
1442
1443 return get_meta_page(sbi, blk_addr);
1444}
1445
1446static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
1447 unsigned int start)
1448{
1449 struct sit_info *sit_i = SIT_I(sbi);
1450 struct page *src_page, *dst_page;
1451 pgoff_t src_off, dst_off;
1452 void *src_addr, *dst_addr;
1453
1454 src_off = current_sit_addr(sbi, start);
1455 dst_off = next_sit_addr(sbi, src_off);
1456
1457 /* get current sit block page without lock */
1458 src_page = get_meta_page(sbi, src_off);
1459 dst_page = grab_meta_page(sbi, dst_off);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001460 f2fs_bug_on(PageDirty(src_page));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001461
1462 src_addr = page_address(src_page);
1463 dst_addr = page_address(dst_page);
1464 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
1465
1466 set_page_dirty(dst_page);
1467 f2fs_put_page(src_page, 1);
1468
1469 set_to_next_sit(sit_i, start);
1470
1471 return dst_page;
1472}
1473
1474static bool flush_sits_in_journal(struct f2fs_sb_info *sbi)
1475{
1476 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1477 struct f2fs_summary_block *sum = curseg->sum_blk;
1478 int i;
1479
1480 /*
1481 * If the journal area in the current summary is full of sit entries,
1482 * all the sit entries will be flushed. Otherwise the sit entries
1483 * are not able to replace with newly hot sit entries.
1484 */
1485 if (sits_in_cursum(sum) >= SIT_JOURNAL_ENTRIES) {
1486 for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
1487 unsigned int segno;
1488 segno = le32_to_cpu(segno_in_journal(sum, i));
1489 __mark_sit_entry_dirty(sbi, segno);
1490 }
1491 update_sits_in_cursum(sum, -sits_in_cursum(sum));
Haicheng Licffbfa62013-10-18 17:24:07 +08001492 return true;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001493 }
Haicheng Licffbfa62013-10-18 17:24:07 +08001494 return false;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001495}
1496
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001497/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001498 * CP calls this function, which flushes SIT entries including sit_journal,
1499 * and moves prefree segs to free segs.
1500 */
1501void flush_sit_entries(struct f2fs_sb_info *sbi)
1502{
1503 struct sit_info *sit_i = SIT_I(sbi);
1504 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
1505 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1506 struct f2fs_summary_block *sum = curseg->sum_blk;
1507 unsigned long nsegs = TOTAL_SEGS(sbi);
1508 struct page *page = NULL;
1509 struct f2fs_sit_block *raw_sit = NULL;
1510 unsigned int start = 0, end = 0;
1511 unsigned int segno = -1;
1512 bool flushed;
1513
1514 mutex_lock(&curseg->curseg_mutex);
1515 mutex_lock(&sit_i->sentry_lock);
1516
1517 /*
1518 * "flushed" indicates whether sit entries in journal are flushed
1519 * to the SIT area or not.
1520 */
1521 flushed = flush_sits_in_journal(sbi);
1522
1523 while ((segno = find_next_bit(bitmap, nsegs, segno + 1)) < nsegs) {
1524 struct seg_entry *se = get_seg_entry(sbi, segno);
1525 int sit_offset, offset;
1526
1527 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
1528
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001529 /* add discard candidates */
1530 if (SM_I(sbi)->nr_discards < SM_I(sbi)->max_discards)
1531 add_discard_addrs(sbi, segno, se);
1532
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001533 if (flushed)
1534 goto to_sit_page;
1535
1536 offset = lookup_journal_in_cursum(sum, SIT_JOURNAL, segno, 1);
1537 if (offset >= 0) {
1538 segno_in_journal(sum, offset) = cpu_to_le32(segno);
1539 seg_info_to_raw_sit(se, &sit_in_journal(sum, offset));
1540 goto flush_done;
1541 }
1542to_sit_page:
1543 if (!page || (start > segno) || (segno > end)) {
1544 if (page) {
1545 f2fs_put_page(page, 1);
1546 page = NULL;
1547 }
1548
1549 start = START_SEGNO(sit_i, segno);
1550 end = start + SIT_ENTRY_PER_BLOCK - 1;
1551
1552 /* read sit block that will be updated */
1553 page = get_next_sit_page(sbi, start);
1554 raw_sit = page_address(page);
1555 }
1556
1557 /* udpate entry in SIT block */
1558 seg_info_to_raw_sit(se, &raw_sit->entries[sit_offset]);
1559flush_done:
1560 __clear_bit(segno, bitmap);
1561 sit_i->dirty_sentries--;
1562 }
1563 mutex_unlock(&sit_i->sentry_lock);
1564 mutex_unlock(&curseg->curseg_mutex);
1565
1566 /* writeout last modified SIT block */
1567 f2fs_put_page(page, 1);
1568
1569 set_prefree_as_free_segments(sbi);
1570}
1571
1572static int build_sit_info(struct f2fs_sb_info *sbi)
1573{
1574 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1575 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1576 struct sit_info *sit_i;
1577 unsigned int sit_segs, start;
1578 char *src_bitmap, *dst_bitmap;
1579 unsigned int bitmap_size;
1580
1581 /* allocate memory for SIT information */
1582 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
1583 if (!sit_i)
1584 return -ENOMEM;
1585
1586 SM_I(sbi)->sit_info = sit_i;
1587
1588 sit_i->sentries = vzalloc(TOTAL_SEGS(sbi) * sizeof(struct seg_entry));
1589 if (!sit_i->sentries)
1590 return -ENOMEM;
1591
1592 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1593 sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1594 if (!sit_i->dirty_sentries_bitmap)
1595 return -ENOMEM;
1596
1597 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1598 sit_i->sentries[start].cur_valid_map
1599 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1600 sit_i->sentries[start].ckpt_valid_map
1601 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1602 if (!sit_i->sentries[start].cur_valid_map
1603 || !sit_i->sentries[start].ckpt_valid_map)
1604 return -ENOMEM;
1605 }
1606
1607 if (sbi->segs_per_sec > 1) {
Jaegeuk Kim53cf9522013-03-31 12:39:49 +09001608 sit_i->sec_entries = vzalloc(TOTAL_SECS(sbi) *
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001609 sizeof(struct sec_entry));
1610 if (!sit_i->sec_entries)
1611 return -ENOMEM;
1612 }
1613
1614 /* get information related with SIT */
1615 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
1616
1617 /* setup SIT bitmap from ckeckpoint pack */
1618 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
1619 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
1620
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001621 dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001622 if (!dst_bitmap)
1623 return -ENOMEM;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001624
1625 /* init SIT information */
1626 sit_i->s_ops = &default_salloc_ops;
1627
1628 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
1629 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
1630 sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
1631 sit_i->sit_bitmap = dst_bitmap;
1632 sit_i->bitmap_size = bitmap_size;
1633 sit_i->dirty_sentries = 0;
1634 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
1635 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
1636 sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
1637 mutex_init(&sit_i->sentry_lock);
1638 return 0;
1639}
1640
1641static int build_free_segmap(struct f2fs_sb_info *sbi)
1642{
1643 struct f2fs_sm_info *sm_info = SM_I(sbi);
1644 struct free_segmap_info *free_i;
1645 unsigned int bitmap_size, sec_bitmap_size;
1646
1647 /* allocate memory for free segmap information */
1648 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
1649 if (!free_i)
1650 return -ENOMEM;
1651
1652 SM_I(sbi)->free_info = free_i;
1653
1654 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1655 free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL);
1656 if (!free_i->free_segmap)
1657 return -ENOMEM;
1658
Jaegeuk Kim53cf9522013-03-31 12:39:49 +09001659 sec_bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001660 free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL);
1661 if (!free_i->free_secmap)
1662 return -ENOMEM;
1663
1664 /* set all segments as dirty temporarily */
1665 memset(free_i->free_segmap, 0xff, bitmap_size);
1666 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
1667
1668 /* init free segmap information */
1669 free_i->start_segno =
1670 (unsigned int) GET_SEGNO_FROM_SEG0(sbi, sm_info->main_blkaddr);
1671 free_i->free_segments = 0;
1672 free_i->free_sections = 0;
1673 rwlock_init(&free_i->segmap_lock);
1674 return 0;
1675}
1676
1677static int build_curseg(struct f2fs_sb_info *sbi)
1678{
Namjae Jeon1042d602012-12-01 10:56:13 +09001679 struct curseg_info *array;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001680 int i;
1681
1682 array = kzalloc(sizeof(*array) * NR_CURSEG_TYPE, GFP_KERNEL);
1683 if (!array)
1684 return -ENOMEM;
1685
1686 SM_I(sbi)->curseg_array = array;
1687
1688 for (i = 0; i < NR_CURSEG_TYPE; i++) {
1689 mutex_init(&array[i].curseg_mutex);
1690 array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
1691 if (!array[i].sum_blk)
1692 return -ENOMEM;
1693 array[i].segno = NULL_SEGNO;
1694 array[i].next_blkoff = 0;
1695 }
1696 return restore_curseg_summaries(sbi);
1697}
1698
1699static void build_sit_entries(struct f2fs_sb_info *sbi)
1700{
1701 struct sit_info *sit_i = SIT_I(sbi);
1702 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1703 struct f2fs_summary_block *sum = curseg->sum_blk;
1704 unsigned int start;
1705
1706 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1707 struct seg_entry *se = &sit_i->sentries[start];
1708 struct f2fs_sit_block *sit_blk;
1709 struct f2fs_sit_entry sit;
1710 struct page *page;
1711 int i;
1712
1713 mutex_lock(&curseg->curseg_mutex);
1714 for (i = 0; i < sits_in_cursum(sum); i++) {
1715 if (le32_to_cpu(segno_in_journal(sum, i)) == start) {
1716 sit = sit_in_journal(sum, i);
1717 mutex_unlock(&curseg->curseg_mutex);
1718 goto got_it;
1719 }
1720 }
1721 mutex_unlock(&curseg->curseg_mutex);
1722 page = get_current_sit_page(sbi, start);
1723 sit_blk = (struct f2fs_sit_block *)page_address(page);
1724 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
1725 f2fs_put_page(page, 1);
1726got_it:
1727 check_block_count(sbi, start, &sit);
1728 seg_info_from_raw_sit(se, &sit);
1729 if (sbi->segs_per_sec > 1) {
1730 struct sec_entry *e = get_sec_entry(sbi, start);
1731 e->valid_blocks += se->valid_blocks;
1732 }
1733 }
1734}
1735
1736static void init_free_segmap(struct f2fs_sb_info *sbi)
1737{
1738 unsigned int start;
1739 int type;
1740
1741 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1742 struct seg_entry *sentry = get_seg_entry(sbi, start);
1743 if (!sentry->valid_blocks)
1744 __set_free(sbi, start);
1745 }
1746
1747 /* set use the current segments */
1748 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
1749 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
1750 __set_test_and_inuse(sbi, curseg_t->segno);
1751 }
1752}
1753
1754static void init_dirty_segmap(struct f2fs_sb_info *sbi)
1755{
1756 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1757 struct free_segmap_info *free_i = FREE_I(sbi);
Namjae Jeon8736fbf2013-06-16 09:49:11 +09001758 unsigned int segno = 0, offset = 0, total_segs = TOTAL_SEGS(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001759 unsigned short valid_blocks;
1760
Namjae Jeon8736fbf2013-06-16 09:49:11 +09001761 while (1) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001762 /* find dirty segment based on free segmap */
Namjae Jeon8736fbf2013-06-16 09:49:11 +09001763 segno = find_next_inuse(free_i, total_segs, offset);
1764 if (segno >= total_segs)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001765 break;
1766 offset = segno + 1;
1767 valid_blocks = get_valid_blocks(sbi, segno, 0);
1768 if (valid_blocks >= sbi->blocks_per_seg || !valid_blocks)
1769 continue;
1770 mutex_lock(&dirty_i->seglist_lock);
1771 __locate_dirty_segment(sbi, segno, DIRTY);
1772 mutex_unlock(&dirty_i->seglist_lock);
1773 }
1774}
1775
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001776static int init_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001777{
1778 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001779 unsigned int bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001780
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001781 dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL);
1782 if (!dirty_i->victim_secmap)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001783 return -ENOMEM;
1784 return 0;
1785}
1786
1787static int build_dirty_segmap(struct f2fs_sb_info *sbi)
1788{
1789 struct dirty_seglist_info *dirty_i;
1790 unsigned int bitmap_size, i;
1791
1792 /* allocate memory for dirty segments list information */
1793 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
1794 if (!dirty_i)
1795 return -ENOMEM;
1796
1797 SM_I(sbi)->dirty_info = dirty_i;
1798 mutex_init(&dirty_i->seglist_lock);
1799
1800 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1801
1802 for (i = 0; i < NR_DIRTY_TYPE; i++) {
1803 dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001804 if (!dirty_i->dirty_segmap[i])
1805 return -ENOMEM;
1806 }
1807
1808 init_dirty_segmap(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001809 return init_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001810}
1811
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001812/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001813 * Update min, max modified time for cost-benefit GC algorithm
1814 */
1815static void init_min_max_mtime(struct f2fs_sb_info *sbi)
1816{
1817 struct sit_info *sit_i = SIT_I(sbi);
1818 unsigned int segno;
1819
1820 mutex_lock(&sit_i->sentry_lock);
1821
1822 sit_i->min_mtime = LLONG_MAX;
1823
1824 for (segno = 0; segno < TOTAL_SEGS(sbi); segno += sbi->segs_per_sec) {
1825 unsigned int i;
1826 unsigned long long mtime = 0;
1827
1828 for (i = 0; i < sbi->segs_per_sec; i++)
1829 mtime += get_seg_entry(sbi, segno + i)->mtime;
1830
1831 mtime = div_u64(mtime, sbi->segs_per_sec);
1832
1833 if (sit_i->min_mtime > mtime)
1834 sit_i->min_mtime = mtime;
1835 }
1836 sit_i->max_mtime = get_mtime(sbi);
1837 mutex_unlock(&sit_i->sentry_lock);
1838}
1839
1840int build_segment_manager(struct f2fs_sb_info *sbi)
1841{
1842 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1843 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Namjae Jeon1042d602012-12-01 10:56:13 +09001844 struct f2fs_sm_info *sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001845 int err;
1846
1847 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
1848 if (!sm_info)
1849 return -ENOMEM;
1850
1851 /* init sm info */
1852 sbi->sm_info = sm_info;
1853 INIT_LIST_HEAD(&sm_info->wblist_head);
1854 spin_lock_init(&sm_info->wblist_lock);
1855 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
1856 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
1857 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
1858 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
1859 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
1860 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
1861 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
Jaegeuk Kim81eb8d62013-10-24 13:31:34 +09001862 sm_info->rec_prefree_segments = DEF_RECLAIM_PREFREE_SEGMENTS;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001863
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09001864 INIT_LIST_HEAD(&sm_info->discard_list);
1865 sm_info->nr_discards = 0;
1866 sm_info->max_discards = 0;
1867
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001868 err = build_sit_info(sbi);
1869 if (err)
1870 return err;
1871 err = build_free_segmap(sbi);
1872 if (err)
1873 return err;
1874 err = build_curseg(sbi);
1875 if (err)
1876 return err;
1877
1878 /* reinit free segmap based on SIT */
1879 build_sit_entries(sbi);
1880
1881 init_free_segmap(sbi);
1882 err = build_dirty_segmap(sbi);
1883 if (err)
1884 return err;
1885
1886 init_min_max_mtime(sbi);
1887 return 0;
1888}
1889
1890static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
1891 enum dirty_type dirty_type)
1892{
1893 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1894
1895 mutex_lock(&dirty_i->seglist_lock);
1896 kfree(dirty_i->dirty_segmap[dirty_type]);
1897 dirty_i->nr_dirty[dirty_type] = 0;
1898 mutex_unlock(&dirty_i->seglist_lock);
1899}
1900
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001901static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001902{
1903 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001904 kfree(dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001905}
1906
1907static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
1908{
1909 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1910 int i;
1911
1912 if (!dirty_i)
1913 return;
1914
1915 /* discard pre-free/dirty segments list */
1916 for (i = 0; i < NR_DIRTY_TYPE; i++)
1917 discard_dirty_segmap(sbi, i);
1918
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001919 destroy_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001920 SM_I(sbi)->dirty_info = NULL;
1921 kfree(dirty_i);
1922}
1923
1924static void destroy_curseg(struct f2fs_sb_info *sbi)
1925{
1926 struct curseg_info *array = SM_I(sbi)->curseg_array;
1927 int i;
1928
1929 if (!array)
1930 return;
1931 SM_I(sbi)->curseg_array = NULL;
1932 for (i = 0; i < NR_CURSEG_TYPE; i++)
1933 kfree(array[i].sum_blk);
1934 kfree(array);
1935}
1936
1937static void destroy_free_segmap(struct f2fs_sb_info *sbi)
1938{
1939 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
1940 if (!free_i)
1941 return;
1942 SM_I(sbi)->free_info = NULL;
1943 kfree(free_i->free_segmap);
1944 kfree(free_i->free_secmap);
1945 kfree(free_i);
1946}
1947
1948static void destroy_sit_info(struct f2fs_sb_info *sbi)
1949{
1950 struct sit_info *sit_i = SIT_I(sbi);
1951 unsigned int start;
1952
1953 if (!sit_i)
1954 return;
1955
1956 if (sit_i->sentries) {
1957 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1958 kfree(sit_i->sentries[start].cur_valid_map);
1959 kfree(sit_i->sentries[start].ckpt_valid_map);
1960 }
1961 }
1962 vfree(sit_i->sentries);
1963 vfree(sit_i->sec_entries);
1964 kfree(sit_i->dirty_sentries_bitmap);
1965
1966 SM_I(sbi)->sit_info = NULL;
1967 kfree(sit_i->sit_bitmap);
1968 kfree(sit_i);
1969}
1970
1971void destroy_segment_manager(struct f2fs_sb_info *sbi)
1972{
1973 struct f2fs_sm_info *sm_info = SM_I(sbi);
Chao Yu3b03f722013-11-06 09:12:04 +08001974 if (!sm_info)
1975 return;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001976 destroy_dirty_segmap(sbi);
1977 destroy_curseg(sbi);
1978 destroy_free_segmap(sbi);
1979 destroy_sit_info(sbi);
1980 sbi->sm_info = NULL;
1981 kfree(sm_info);
1982}
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09001983
1984int __init create_segment_manager_caches(void)
1985{
1986 discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
1987 sizeof(struct discard_entry), NULL);
1988 if (!discard_entry_slab)
1989 return -ENOMEM;
1990 return 0;
1991}
1992
1993void destroy_segment_manager_caches(void)
1994{
1995 kmem_cache_destroy(discard_entry_slab);
1996}