blob: d42426dc3706bc0a012a20f8c9abdec052450f92 [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);
Jaegeuk Kim1661d072013-11-12 17:01:00 +0900275 trace_f2fs_issue_discard(sbi->sb, blkstart, blklen);
Jaegeuk Kim37208872013-11-12 16:55:17 +0900276}
277
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900278static void add_discard_addrs(struct f2fs_sb_info *sbi,
279 unsigned int segno, struct seg_entry *se)
280{
281 struct list_head *head = &SM_I(sbi)->discard_list;
282 struct discard_entry *new;
283 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
284 int max_blocks = sbi->blocks_per_seg;
285 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
286 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
287 unsigned long dmap[entries];
288 unsigned int start = 0, end = -1;
289 int i;
290
291 if (!test_opt(sbi, DISCARD))
292 return;
293
294 /* zero block will be discarded through the prefree list */
295 if (!se->valid_blocks || se->valid_blocks == max_blocks)
296 return;
297
298 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
299 for (i = 0; i < entries; i++)
300 dmap[i] = (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
301
302 while (SM_I(sbi)->nr_discards <= SM_I(sbi)->max_discards) {
303 start = __find_rev_next_bit(dmap, max_blocks, end + 1);
304 if (start >= max_blocks)
305 break;
306
307 end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
308
309 new = f2fs_kmem_cache_alloc(discard_entry_slab, GFP_NOFS);
310 INIT_LIST_HEAD(&new->list);
311 new->blkaddr = START_BLOCK(sbi, segno) + start;
312 new->len = end - start;
313
314 list_add_tail(&new->list, head);
315 SM_I(sbi)->nr_discards += end - start;
316 }
317}
318
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900319/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900320 * Should call clear_prefree_segments after checkpoint is done.
321 */
322static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
323{
324 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Haicheng Li81fb5e82013-05-14 18:20:28 +0800325 unsigned int segno = -1;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900326 unsigned int total_segs = TOTAL_SEGS(sbi);
327
328 mutex_lock(&dirty_i->seglist_lock);
329 while (1) {
330 segno = find_next_bit(dirty_i->dirty_segmap[PRE], total_segs,
Haicheng Li81fb5e82013-05-14 18:20:28 +0800331 segno + 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900332 if (segno >= total_segs)
333 break;
334 __set_test_and_free(sbi, segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900335 }
336 mutex_unlock(&dirty_i->seglist_lock);
337}
338
339void clear_prefree_segments(struct f2fs_sb_info *sbi)
340{
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900341 struct list_head *head = &(SM_I(sbi)->discard_list);
342 struct list_head *this, *next;
343 struct discard_entry *entry;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900344 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Changman Lee29e59c12013-11-11 09:24:37 +0900345 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900346 unsigned int total_segs = TOTAL_SEGS(sbi);
Changman Lee29e59c12013-11-11 09:24:37 +0900347 unsigned int start = 0, end = -1;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900348
349 mutex_lock(&dirty_i->seglist_lock);
Changman Lee29e59c12013-11-11 09:24:37 +0900350
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900351 while (1) {
Changman Lee29e59c12013-11-11 09:24:37 +0900352 int i;
353 start = find_next_bit(prefree_map, total_segs, end + 1);
354 if (start >= total_segs)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900355 break;
Changman Lee29e59c12013-11-11 09:24:37 +0900356 end = find_next_zero_bit(prefree_map, total_segs, start + 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900357
Changman Lee29e59c12013-11-11 09:24:37 +0900358 for (i = start; i < end; i++)
359 clear_bit(i, prefree_map);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900360
Changman Lee29e59c12013-11-11 09:24:37 +0900361 dirty_i->nr_dirty[PRE] -= end - start;
362
363 if (!test_opt(sbi, DISCARD))
364 continue;
365
Jaegeuk Kim37208872013-11-12 16:55:17 +0900366 f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
367 (end - start) << sbi->log_blocks_per_seg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900368 }
369 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900370
371 /* send small discards */
372 list_for_each_safe(this, next, head) {
373 entry = list_entry(this, struct discard_entry, list);
Jaegeuk Kim37208872013-11-12 16:55:17 +0900374 f2fs_issue_discard(sbi, entry->blkaddr, entry->len);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900375 list_del(&entry->list);
376 SM_I(sbi)->nr_discards -= entry->len;
377 kmem_cache_free(discard_entry_slab, entry);
378 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900379}
380
381static void __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
382{
383 struct sit_info *sit_i = SIT_I(sbi);
384 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap))
385 sit_i->dirty_sentries++;
386}
387
388static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
389 unsigned int segno, int modified)
390{
391 struct seg_entry *se = get_seg_entry(sbi, segno);
392 se->type = type;
393 if (modified)
394 __mark_sit_entry_dirty(sbi, segno);
395}
396
397static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
398{
399 struct seg_entry *se;
400 unsigned int segno, offset;
401 long int new_vblocks;
402
403 segno = GET_SEGNO(sbi, blkaddr);
404
405 se = get_seg_entry(sbi, segno);
406 new_vblocks = se->valid_blocks + del;
407 offset = GET_SEGOFF_FROM_SEG0(sbi, blkaddr) & (sbi->blocks_per_seg - 1);
408
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900409 f2fs_bug_on((new_vblocks >> (sizeof(unsigned short) << 3) ||
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900410 (new_vblocks > sbi->blocks_per_seg)));
411
412 se->valid_blocks = new_vblocks;
413 se->mtime = get_mtime(sbi);
414 SIT_I(sbi)->max_mtime = se->mtime;
415
416 /* Update valid block bitmap */
417 if (del > 0) {
418 if (f2fs_set_bit(offset, se->cur_valid_map))
419 BUG();
420 } else {
421 if (!f2fs_clear_bit(offset, se->cur_valid_map))
422 BUG();
423 }
424 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
425 se->ckpt_valid_blocks += del;
426
427 __mark_sit_entry_dirty(sbi, segno);
428
429 /* update total number of valid blocks to be written in ckpt area */
430 SIT_I(sbi)->written_valid_blocks += del;
431
432 if (sbi->segs_per_sec > 1)
433 get_sec_entry(sbi, segno)->valid_blocks += del;
434}
435
436static void refresh_sit_entry(struct f2fs_sb_info *sbi,
437 block_t old_blkaddr, block_t new_blkaddr)
438{
439 update_sit_entry(sbi, new_blkaddr, 1);
440 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
441 update_sit_entry(sbi, old_blkaddr, -1);
442}
443
444void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
445{
446 unsigned int segno = GET_SEGNO(sbi, addr);
447 struct sit_info *sit_i = SIT_I(sbi);
448
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900449 f2fs_bug_on(addr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900450 if (addr == NEW_ADDR)
451 return;
452
453 /* add it into sit main buffer */
454 mutex_lock(&sit_i->sentry_lock);
455
456 update_sit_entry(sbi, addr, -1);
457
458 /* add it into dirty seglist */
459 locate_dirty_segment(sbi, segno);
460
461 mutex_unlock(&sit_i->sentry_lock);
462}
463
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900464/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900465 * This function should be resided under the curseg_mutex lock
466 */
467static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
Haicheng Lie79efe32013-06-13 16:59:27 +0800468 struct f2fs_summary *sum)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900469{
470 struct curseg_info *curseg = CURSEG_I(sbi, type);
471 void *addr = curseg->sum_blk;
Haicheng Lie79efe32013-06-13 16:59:27 +0800472 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900473 memcpy(addr, sum, sizeof(struct f2fs_summary));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900474}
475
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900476/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900477 * Calculate the number of current summary pages for writing
478 */
479int npages_for_summary_flush(struct f2fs_sb_info *sbi)
480{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900481 int valid_sum_count = 0;
Fan Li9a479382013-10-29 16:21:47 +0800482 int i, sum_in_page;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900483
484 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
485 if (sbi->ckpt->alloc_type[i] == SSR)
486 valid_sum_count += sbi->blocks_per_seg;
487 else
488 valid_sum_count += curseg_blkoff(sbi, i);
489 }
490
Fan Li9a479382013-10-29 16:21:47 +0800491 sum_in_page = (PAGE_CACHE_SIZE - 2 * SUM_JOURNAL_SIZE -
492 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
493 if (valid_sum_count <= sum_in_page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900494 return 1;
Fan Li9a479382013-10-29 16:21:47 +0800495 else if ((valid_sum_count - sum_in_page) <=
496 (PAGE_CACHE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900497 return 2;
498 return 3;
499}
500
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900501/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900502 * Caller should put this summary page
503 */
504struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
505{
506 return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
507}
508
509static void write_sum_page(struct f2fs_sb_info *sbi,
510 struct f2fs_summary_block *sum_blk, block_t blk_addr)
511{
512 struct page *page = grab_meta_page(sbi, blk_addr);
513 void *kaddr = page_address(page);
514 memcpy(kaddr, sum_blk, PAGE_CACHE_SIZE);
515 set_page_dirty(page);
516 f2fs_put_page(page, 1);
517}
518
Jaegeuk Kim60374682013-03-31 13:58:51 +0900519static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
520{
521 struct curseg_info *curseg = CURSEG_I(sbi, type);
Haicheng Li81fb5e82013-05-14 18:20:28 +0800522 unsigned int segno = curseg->segno + 1;
Jaegeuk Kim60374682013-03-31 13:58:51 +0900523 struct free_segmap_info *free_i = FREE_I(sbi);
524
Haicheng Li81fb5e82013-05-14 18:20:28 +0800525 if (segno < TOTAL_SEGS(sbi) && segno % sbi->segs_per_sec)
526 return !test_bit(segno, free_i->free_segmap);
Jaegeuk Kim60374682013-03-31 13:58:51 +0900527 return 0;
528}
529
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900530/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900531 * Find a new segment from the free segments bitmap to right order
532 * This function should be returned with success, otherwise BUG
533 */
534static void get_new_segment(struct f2fs_sb_info *sbi,
535 unsigned int *newseg, bool new_sec, int dir)
536{
537 struct free_segmap_info *free_i = FREE_I(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900538 unsigned int segno, secno, zoneno;
Jaegeuk Kim53cf9522013-03-31 12:39:49 +0900539 unsigned int total_zones = TOTAL_SECS(sbi) / sbi->secs_per_zone;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900540 unsigned int hint = *newseg / sbi->segs_per_sec;
541 unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
542 unsigned int left_start = hint;
543 bool init = true;
544 int go_left = 0;
545 int i;
546
547 write_lock(&free_i->segmap_lock);
548
549 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
550 segno = find_next_zero_bit(free_i->free_segmap,
551 TOTAL_SEGS(sbi), *newseg + 1);
Jaegeuk Kim33afa7f2013-03-31 12:59:53 +0900552 if (segno - *newseg < sbi->segs_per_sec -
553 (*newseg % sbi->segs_per_sec))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900554 goto got_it;
555 }
556find_other_zone:
Jaegeuk Kim53cf9522013-03-31 12:39:49 +0900557 secno = find_next_zero_bit(free_i->free_secmap, TOTAL_SECS(sbi), hint);
558 if (secno >= TOTAL_SECS(sbi)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900559 if (dir == ALLOC_RIGHT) {
560 secno = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim53cf9522013-03-31 12:39:49 +0900561 TOTAL_SECS(sbi), 0);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900562 f2fs_bug_on(secno >= TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900563 } else {
564 go_left = 1;
565 left_start = hint - 1;
566 }
567 }
568 if (go_left == 0)
569 goto skip_left;
570
571 while (test_bit(left_start, free_i->free_secmap)) {
572 if (left_start > 0) {
573 left_start--;
574 continue;
575 }
576 left_start = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim53cf9522013-03-31 12:39:49 +0900577 TOTAL_SECS(sbi), 0);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900578 f2fs_bug_on(left_start >= TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900579 break;
580 }
581 secno = left_start;
582skip_left:
583 hint = secno;
584 segno = secno * sbi->segs_per_sec;
585 zoneno = secno / sbi->secs_per_zone;
586
587 /* give up on finding another zone */
588 if (!init)
589 goto got_it;
590 if (sbi->secs_per_zone == 1)
591 goto got_it;
592 if (zoneno == old_zoneno)
593 goto got_it;
594 if (dir == ALLOC_LEFT) {
595 if (!go_left && zoneno + 1 >= total_zones)
596 goto got_it;
597 if (go_left && zoneno == 0)
598 goto got_it;
599 }
600 for (i = 0; i < NR_CURSEG_TYPE; i++)
601 if (CURSEG_I(sbi, i)->zone == zoneno)
602 break;
603
604 if (i < NR_CURSEG_TYPE) {
605 /* zone is in user, try another */
606 if (go_left)
607 hint = zoneno * sbi->secs_per_zone - 1;
608 else if (zoneno + 1 >= total_zones)
609 hint = 0;
610 else
611 hint = (zoneno + 1) * sbi->secs_per_zone;
612 init = false;
613 goto find_other_zone;
614 }
615got_it:
616 /* set it as dirty segment in free segmap */
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900617 f2fs_bug_on(test_bit(segno, free_i->free_segmap));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900618 __set_inuse(sbi, segno);
619 *newseg = segno;
620 write_unlock(&free_i->segmap_lock);
621}
622
623static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
624{
625 struct curseg_info *curseg = CURSEG_I(sbi, type);
626 struct summary_footer *sum_footer;
627
628 curseg->segno = curseg->next_segno;
629 curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
630 curseg->next_blkoff = 0;
631 curseg->next_segno = NULL_SEGNO;
632
633 sum_footer = &(curseg->sum_blk->footer);
634 memset(sum_footer, 0, sizeof(struct summary_footer));
635 if (IS_DATASEG(type))
636 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
637 if (IS_NODESEG(type))
638 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
639 __set_sit_entry_type(sbi, type, curseg->segno, modified);
640}
641
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900642/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900643 * Allocate a current working segment.
644 * This function always allocates a free segment in LFS manner.
645 */
646static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
647{
648 struct curseg_info *curseg = CURSEG_I(sbi, type);
649 unsigned int segno = curseg->segno;
650 int dir = ALLOC_LEFT;
651
652 write_sum_page(sbi, curseg->sum_blk,
Haicheng Li81fb5e82013-05-14 18:20:28 +0800653 GET_SUM_BLOCK(sbi, segno));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900654 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
655 dir = ALLOC_RIGHT;
656
657 if (test_opt(sbi, NOHEAP))
658 dir = ALLOC_RIGHT;
659
660 get_new_segment(sbi, &segno, new_sec, dir);
661 curseg->next_segno = segno;
662 reset_curseg(sbi, type, 1);
663 curseg->alloc_type = LFS;
664}
665
666static void __next_free_blkoff(struct f2fs_sb_info *sbi,
667 struct curseg_info *seg, block_t start)
668{
669 struct seg_entry *se = get_seg_entry(sbi, seg->segno);
Changman Leee81c93c2013-11-15 13:21:16 +0900670 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
671 unsigned long target_map[entries];
672 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
673 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
674 int i, pos;
675
676 for (i = 0; i < entries; i++)
677 target_map[i] = ckpt_map[i] | cur_map[i];
678
679 pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
680
681 seg->next_blkoff = pos;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900682}
683
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900684/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900685 * If a segment is written by LFS manner, next block offset is just obtained
686 * by increasing the current block offset. However, if a segment is written by
687 * SSR manner, next block offset obtained by calling __next_free_blkoff
688 */
689static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
690 struct curseg_info *seg)
691{
692 if (seg->alloc_type == SSR)
693 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
694 else
695 seg->next_blkoff++;
696}
697
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900698/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900699 * This function always allocates a used segment (from dirty seglist) by SSR
700 * manner, so it should recover the existing segment information of valid blocks
701 */
702static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
703{
704 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
705 struct curseg_info *curseg = CURSEG_I(sbi, type);
706 unsigned int new_segno = curseg->next_segno;
707 struct f2fs_summary_block *sum_node;
708 struct page *sum_page;
709
710 write_sum_page(sbi, curseg->sum_blk,
711 GET_SUM_BLOCK(sbi, curseg->segno));
712 __set_test_and_inuse(sbi, new_segno);
713
714 mutex_lock(&dirty_i->seglist_lock);
715 __remove_dirty_segment(sbi, new_segno, PRE);
716 __remove_dirty_segment(sbi, new_segno, DIRTY);
717 mutex_unlock(&dirty_i->seglist_lock);
718
719 reset_curseg(sbi, type, 1);
720 curseg->alloc_type = SSR;
721 __next_free_blkoff(sbi, curseg, 0);
722
723 if (reuse) {
724 sum_page = get_sum_page(sbi, new_segno);
725 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
726 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
727 f2fs_put_page(sum_page, 1);
728 }
729}
730
Jaegeuk Kim43727522013-02-04 15:11:17 +0900731static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
732{
733 struct curseg_info *curseg = CURSEG_I(sbi, type);
734 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
735
736 if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0))
737 return v_ops->get_victim(sbi,
738 &(curseg)->next_segno, BG_GC, type, SSR);
739
740 /* For data segments, let's do SSR more intensively */
741 for (; type >= CURSEG_HOT_DATA; type--)
742 if (v_ops->get_victim(sbi, &(curseg)->next_segno,
743 BG_GC, type, SSR))
744 return 1;
745 return 0;
746}
747
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900748/*
749 * flush out current segment and replace it with new segment
750 * This function should be returned with success, otherwise BUG
751 */
752static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
753 int type, bool force)
754{
755 struct curseg_info *curseg = CURSEG_I(sbi, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900756
Gu Zheng7b405272013-08-19 09:41:15 +0800757 if (force)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900758 new_curseg(sbi, type, true);
Gu Zheng7b405272013-08-19 09:41:15 +0800759 else if (type == CURSEG_WARM_NODE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900760 new_curseg(sbi, type, false);
Jaegeuk Kim60374682013-03-31 13:58:51 +0900761 else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
762 new_curseg(sbi, type, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900763 else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
764 change_curseg(sbi, type, true);
765 else
766 new_curseg(sbi, type, false);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +0900767
768 stat_inc_seg_type(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900769}
770
771void allocate_new_segments(struct f2fs_sb_info *sbi)
772{
773 struct curseg_info *curseg;
774 unsigned int old_curseg;
775 int i;
776
777 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
778 curseg = CURSEG_I(sbi, i);
779 old_curseg = curseg->segno;
780 SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
781 locate_dirty_segment(sbi, old_curseg);
782 }
783}
784
785static const struct segment_allocation default_salloc_ops = {
786 .allocate_segment = allocate_segment_by_default,
787};
788
789static void f2fs_end_io_write(struct bio *bio, int err)
790{
791 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
792 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
793 struct bio_private *p = bio->bi_private;
794
795 do {
796 struct page *page = bvec->bv_page;
797
798 if (--bvec >= bio->bi_io_vec)
799 prefetchw(&bvec->bv_page->flags);
800 if (!uptodate) {
801 SetPageError(page);
802 if (page->mapping)
803 set_bit(AS_EIO, &page->mapping->flags);
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900804 set_ckpt_flags(p->sbi->ckpt, CP_ERROR_FLAG);
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900805 p->sbi->sb->s_flags |= MS_RDONLY;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900806 }
807 end_page_writeback(page);
808 dec_page_count(p->sbi, F2FS_WRITEBACK);
809 } while (bvec >= bio->bi_io_vec);
810
811 if (p->is_sync)
812 complete(p->wait);
Gu Zhenge2340882013-10-14 18:45:56 +0800813
Changman Leefb51b5e2013-11-07 12:48:25 +0900814 if (!get_pages(p->sbi, F2FS_WRITEBACK) &&
815 !list_empty(&p->sbi->cp_wait.task_list))
816 wake_up(&p->sbi->cp_wait);
Gu Zhenge2340882013-10-14 18:45:56 +0800817
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900818 kfree(p);
819 bio_put(bio);
820}
821
Jaegeuk Kim3cd8a232012-12-10 09:26:05 +0900822struct bio *f2fs_bio_alloc(struct block_device *bdev, int npages)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900823{
824 struct bio *bio;
Jaegeuk Kim3cd8a232012-12-10 09:26:05 +0900825
826 /* No failure on bio allocation */
827 bio = bio_alloc(GFP_NOIO, npages);
828 bio->bi_bdev = bdev;
Gu Zhengd8207f62013-07-25 11:30:01 +0800829 bio->bi_private = NULL;
830
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900831 return bio;
832}
833
834static void do_submit_bio(struct f2fs_sb_info *sbi,
835 enum page_type type, bool sync)
836{
837 int rw = sync ? WRITE_SYNC : WRITE;
Jaegeuk Kim7d5e5102013-11-18 17:13:35 +0900838 enum page_type btype = PAGE_TYPE_OF_BIO(type);
Jaegeuk Kim1ff7bd32013-11-19 12:47:22 +0900839 struct f2fs_bio_info *io = &sbi->write_io[btype];
Jaegeuk Kim7d5e5102013-11-18 17:13:35 +0900840 struct bio_private *p;
841
Jaegeuk Kim1ff7bd32013-11-19 12:47:22 +0900842 if (!io->bio)
Jaegeuk Kim7d5e5102013-11-18 17:13:35 +0900843 return;
844
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900845 if (type >= META_FLUSH)
846 rw = WRITE_FLUSH_FUA;
Namjae Jeon86804412013-04-25 11:45:21 +0900847 if (btype == META)
848 rw |= REQ_META;
849
Jaegeuk Kim1ff7bd32013-11-19 12:47:22 +0900850 p = io->bio->bi_private;
Jaegeuk Kim7d5e5102013-11-18 17:13:35 +0900851 p->sbi = sbi;
Jaegeuk Kim1ff7bd32013-11-19 12:47:22 +0900852 io->bio->bi_end_io = f2fs_end_io_write;
Namjae Jeon6ec178d2013-04-23 17:51:43 +0900853
Jaegeuk Kim1ff7bd32013-11-19 12:47:22 +0900854 trace_f2fs_do_submit_bio(sbi->sb, btype, sync, io->bio);
Namjae Jeon6ec178d2013-04-23 17:51:43 +0900855
Jaegeuk Kim7d5e5102013-11-18 17:13:35 +0900856 if (type == META_FLUSH) {
857 DECLARE_COMPLETION_ONSTACK(wait);
858 p->is_sync = true;
859 p->wait = &wait;
Jaegeuk Kim1ff7bd32013-11-19 12:47:22 +0900860 submit_bio(rw, io->bio);
Jaegeuk Kim7d5e5102013-11-18 17:13:35 +0900861 wait_for_completion(&wait);
862 } else {
863 p->is_sync = false;
Jaegeuk Kim1ff7bd32013-11-19 12:47:22 +0900864 submit_bio(rw, io->bio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900865 }
Jaegeuk Kim1ff7bd32013-11-19 12:47:22 +0900866 io->bio = NULL;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900867}
868
869void f2fs_submit_bio(struct f2fs_sb_info *sbi, enum page_type type, bool sync)
870{
Jaegeuk Kim1ff7bd32013-11-19 12:47:22 +0900871 struct f2fs_bio_info *io = &sbi->write_io[PAGE_TYPE_OF_BIO(type)];
Jaegeuk Kim971767c2013-11-18 17:16:17 +0900872
Jaegeuk Kim1ff7bd32013-11-19 12:47:22 +0900873 if (!io->bio)
Jaegeuk Kim971767c2013-11-18 17:16:17 +0900874 return;
875
Jaegeuk Kim1ff7bd32013-11-19 12:47:22 +0900876 mutex_lock(&io->io_mutex);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900877 do_submit_bio(sbi, type, sync);
Jaegeuk Kim1ff7bd32013-11-19 12:47:22 +0900878 mutex_unlock(&io->io_mutex);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900879}
880
881static void submit_write_page(struct f2fs_sb_info *sbi, struct page *page,
882 block_t blk_addr, enum page_type type)
883{
884 struct block_device *bdev = sbi->sb->s_bdev;
Jaegeuk Kim1ff7bd32013-11-19 12:47:22 +0900885 struct f2fs_bio_info *io = &sbi->write_io[type];
Chao Yucc7b1bb2013-09-22 15:50:50 +0800886 int bio_blocks;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900887
888 verify_block_addr(sbi, blk_addr);
889
Jaegeuk Kim1ff7bd32013-11-19 12:47:22 +0900890 mutex_lock(&io->io_mutex);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900891
892 inc_page_count(sbi, F2FS_WRITEBACK);
893
Jaegeuk Kim1ff7bd32013-11-19 12:47:22 +0900894 if (io->bio && io->last_block_in_bio != blk_addr - 1)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900895 do_submit_bio(sbi, type, false);
896alloc_new:
Jaegeuk Kim1ff7bd32013-11-19 12:47:22 +0900897 if (io->bio == NULL) {
Gu Zhengd8207f62013-07-25 11:30:01 +0800898 struct bio_private *priv;
899retry:
900 priv = kmalloc(sizeof(struct bio_private), GFP_NOFS);
901 if (!priv) {
902 cond_resched();
903 goto retry;
904 }
905
Chao Yucc7b1bb2013-09-22 15:50:50 +0800906 bio_blocks = MAX_BIO_BLOCKS(max_hw_blocks(sbi));
Jaegeuk Kim1ff7bd32013-11-19 12:47:22 +0900907 io->bio = f2fs_bio_alloc(bdev, bio_blocks);
908 io->bio->bi_sector = SECTOR_FROM_BLOCK(sbi, blk_addr);
909 io->bio->bi_private = priv;
Jaegeuk Kim3cd8a232012-12-10 09:26:05 +0900910 /*
911 * The end_io will be assigned at the sumbission phase.
912 * Until then, let bio_add_page() merge consecutive IOs as much
913 * as possible.
914 */
915 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900916
Jaegeuk Kim1ff7bd32013-11-19 12:47:22 +0900917 if (bio_add_page(io->bio, page, PAGE_CACHE_SIZE, 0) <
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900918 PAGE_CACHE_SIZE) {
919 do_submit_bio(sbi, type, false);
920 goto alloc_new;
921 }
922
Jaegeuk Kim1ff7bd32013-11-19 12:47:22 +0900923 io->last_block_in_bio = blk_addr;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900924
Jaegeuk Kim1ff7bd32013-11-19 12:47:22 +0900925 mutex_unlock(&io->io_mutex);
Chao Yu87b88722013-11-20 16:40:10 +0800926 trace_f2fs_submit_write_page(page, WRITE, type, blk_addr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900927}
928
Jin Xua5694692013-08-05 20:02:04 +0800929void f2fs_wait_on_page_writeback(struct page *page,
930 enum page_type type, bool sync)
931{
932 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
933 if (PageWriteback(page)) {
934 f2fs_submit_bio(sbi, type, sync);
935 wait_on_page_writeback(page);
936 }
937}
938
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900939static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
940{
941 struct curseg_info *curseg = CURSEG_I(sbi, type);
942 if (curseg->next_blkoff < sbi->blocks_per_seg)
943 return true;
944 return false;
945}
946
947static int __get_segment_type_2(struct page *page, enum page_type p_type)
948{
949 if (p_type == DATA)
950 return CURSEG_HOT_DATA;
951 else
952 return CURSEG_HOT_NODE;
953}
954
955static int __get_segment_type_4(struct page *page, enum page_type p_type)
956{
957 if (p_type == DATA) {
958 struct inode *inode = page->mapping->host;
959
960 if (S_ISDIR(inode->i_mode))
961 return CURSEG_HOT_DATA;
962 else
963 return CURSEG_COLD_DATA;
964 } else {
965 if (IS_DNODE(page) && !is_cold_node(page))
966 return CURSEG_HOT_NODE;
967 else
968 return CURSEG_COLD_NODE;
969 }
970}
971
972static int __get_segment_type_6(struct page *page, enum page_type p_type)
973{
974 if (p_type == DATA) {
975 struct inode *inode = page->mapping->host;
976
977 if (S_ISDIR(inode->i_mode))
978 return CURSEG_HOT_DATA;
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900979 else if (is_cold_data(page) || file_is_cold(inode))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900980 return CURSEG_COLD_DATA;
981 else
982 return CURSEG_WARM_DATA;
983 } else {
984 if (IS_DNODE(page))
985 return is_cold_node(page) ? CURSEG_WARM_NODE :
986 CURSEG_HOT_NODE;
987 else
988 return CURSEG_COLD_NODE;
989 }
990}
991
992static int __get_segment_type(struct page *page, enum page_type p_type)
993{
994 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
995 switch (sbi->active_logs) {
996 case 2:
997 return __get_segment_type_2(page, p_type);
998 case 4:
999 return __get_segment_type_4(page, p_type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001000 }
Jaegeuk Kim12a67142012-12-21 11:47:05 +09001001 /* NR_CURSEG_TYPE(6) logs by default */
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001002 f2fs_bug_on(sbi->active_logs != NR_CURSEG_TYPE);
Jaegeuk Kim12a67142012-12-21 11:47:05 +09001003 return __get_segment_type_6(page, p_type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001004}
1005
1006static void do_write_page(struct f2fs_sb_info *sbi, struct page *page,
1007 block_t old_blkaddr, block_t *new_blkaddr,
1008 struct f2fs_summary *sum, enum page_type p_type)
1009{
1010 struct sit_info *sit_i = SIT_I(sbi);
1011 struct curseg_info *curseg;
1012 unsigned int old_cursegno;
1013 int type;
1014
1015 type = __get_segment_type(page, p_type);
1016 curseg = CURSEG_I(sbi, type);
1017
1018 mutex_lock(&curseg->curseg_mutex);
1019
1020 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
1021 old_cursegno = curseg->segno;
1022
1023 /*
1024 * __add_sum_entry should be resided under the curseg_mutex
1025 * because, this function updates a summary entry in the
1026 * current summary block.
1027 */
Haicheng Lie79efe32013-06-13 16:59:27 +08001028 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001029
1030 mutex_lock(&sit_i->sentry_lock);
1031 __refresh_next_blkoff(sbi, curseg);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +09001032
1033 stat_inc_block_count(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001034
1035 /*
1036 * SIT information should be updated before segment allocation,
1037 * since SSR needs latest valid block information.
1038 */
1039 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
1040
1041 if (!__has_curseg_space(sbi, type))
1042 sit_i->s_ops->allocate_segment(sbi, type, false);
1043
1044 locate_dirty_segment(sbi, old_cursegno);
1045 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
1046 mutex_unlock(&sit_i->sentry_lock);
1047
1048 if (p_type == NODE)
1049 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
1050
1051 /* writeout dirty page into bdev */
1052 submit_write_page(sbi, page, *new_blkaddr, p_type);
1053
1054 mutex_unlock(&curseg->curseg_mutex);
1055}
1056
Jaegeuk Kim577e3492013-01-24 19:56:11 +09001057void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001058{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001059 set_page_writeback(page);
1060 submit_write_page(sbi, page, page->index, META);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001061}
1062
1063void write_node_page(struct f2fs_sb_info *sbi, struct page *page,
1064 unsigned int nid, block_t old_blkaddr, block_t *new_blkaddr)
1065{
1066 struct f2fs_summary sum;
1067 set_summary(&sum, nid, 0, 0);
1068 do_write_page(sbi, page, old_blkaddr, new_blkaddr, &sum, NODE);
1069}
1070
1071void write_data_page(struct inode *inode, struct page *page,
1072 struct dnode_of_data *dn, block_t old_blkaddr,
1073 block_t *new_blkaddr)
1074{
1075 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1076 struct f2fs_summary sum;
1077 struct node_info ni;
1078
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001079 f2fs_bug_on(old_blkaddr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001080 get_node_info(sbi, dn->nid, &ni);
1081 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1082
1083 do_write_page(sbi, page, old_blkaddr,
1084 new_blkaddr, &sum, DATA);
1085}
1086
1087void rewrite_data_page(struct f2fs_sb_info *sbi, struct page *page,
1088 block_t old_blk_addr)
1089{
1090 submit_write_page(sbi, page, old_blk_addr, DATA);
1091}
1092
1093void recover_data_page(struct f2fs_sb_info *sbi,
1094 struct page *page, struct f2fs_summary *sum,
1095 block_t old_blkaddr, block_t new_blkaddr)
1096{
1097 struct sit_info *sit_i = SIT_I(sbi);
1098 struct curseg_info *curseg;
1099 unsigned int segno, old_cursegno;
1100 struct seg_entry *se;
1101 int type;
1102
1103 segno = GET_SEGNO(sbi, new_blkaddr);
1104 se = get_seg_entry(sbi, segno);
1105 type = se->type;
1106
1107 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
1108 if (old_blkaddr == NULL_ADDR)
1109 type = CURSEG_COLD_DATA;
1110 else
1111 type = CURSEG_WARM_DATA;
1112 }
1113 curseg = CURSEG_I(sbi, type);
1114
1115 mutex_lock(&curseg->curseg_mutex);
1116 mutex_lock(&sit_i->sentry_lock);
1117
1118 old_cursegno = curseg->segno;
1119
1120 /* change the current segment */
1121 if (segno != curseg->segno) {
1122 curseg->next_segno = segno;
1123 change_curseg(sbi, type, true);
1124 }
1125
1126 curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, new_blkaddr) &
1127 (sbi->blocks_per_seg - 1);
Haicheng Lie79efe32013-06-13 16:59:27 +08001128 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001129
1130 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
1131
1132 locate_dirty_segment(sbi, old_cursegno);
1133 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
1134
1135 mutex_unlock(&sit_i->sentry_lock);
1136 mutex_unlock(&curseg->curseg_mutex);
1137}
1138
1139void rewrite_node_page(struct f2fs_sb_info *sbi,
1140 struct page *page, struct f2fs_summary *sum,
1141 block_t old_blkaddr, block_t new_blkaddr)
1142{
1143 struct sit_info *sit_i = SIT_I(sbi);
1144 int type = CURSEG_WARM_NODE;
1145 struct curseg_info *curseg;
1146 unsigned int segno, old_cursegno;
1147 block_t next_blkaddr = next_blkaddr_of_node(page);
1148 unsigned int next_segno = GET_SEGNO(sbi, next_blkaddr);
1149
1150 curseg = CURSEG_I(sbi, type);
1151
1152 mutex_lock(&curseg->curseg_mutex);
1153 mutex_lock(&sit_i->sentry_lock);
1154
1155 segno = GET_SEGNO(sbi, new_blkaddr);
1156 old_cursegno = curseg->segno;
1157
1158 /* change the current segment */
1159 if (segno != curseg->segno) {
1160 curseg->next_segno = segno;
1161 change_curseg(sbi, type, true);
1162 }
1163 curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, new_blkaddr) &
1164 (sbi->blocks_per_seg - 1);
Haicheng Lie79efe32013-06-13 16:59:27 +08001165 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001166
1167 /* change the current log to the next block addr in advance */
1168 if (next_segno != segno) {
1169 curseg->next_segno = next_segno;
1170 change_curseg(sbi, type, true);
1171 }
1172 curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, next_blkaddr) &
1173 (sbi->blocks_per_seg - 1);
1174
1175 /* rewrite node page */
1176 set_page_writeback(page);
1177 submit_write_page(sbi, page, new_blkaddr, NODE);
1178 f2fs_submit_bio(sbi, NODE, true);
1179 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
1180
1181 locate_dirty_segment(sbi, old_cursegno);
1182 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
1183
1184 mutex_unlock(&sit_i->sentry_lock);
1185 mutex_unlock(&curseg->curseg_mutex);
1186}
1187
1188static int read_compacted_summaries(struct f2fs_sb_info *sbi)
1189{
1190 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1191 struct curseg_info *seg_i;
1192 unsigned char *kaddr;
1193 struct page *page;
1194 block_t start;
1195 int i, j, offset;
1196
1197 start = start_sum_block(sbi);
1198
1199 page = get_meta_page(sbi, start++);
1200 kaddr = (unsigned char *)page_address(page);
1201
1202 /* Step 1: restore nat cache */
1203 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1204 memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
1205
1206 /* Step 2: restore sit cache */
1207 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1208 memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
1209 SUM_JOURNAL_SIZE);
1210 offset = 2 * SUM_JOURNAL_SIZE;
1211
1212 /* Step 3: restore summary entries */
1213 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1214 unsigned short blk_off;
1215 unsigned int segno;
1216
1217 seg_i = CURSEG_I(sbi, i);
1218 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
1219 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
1220 seg_i->next_segno = segno;
1221 reset_curseg(sbi, i, 0);
1222 seg_i->alloc_type = ckpt->alloc_type[i];
1223 seg_i->next_blkoff = blk_off;
1224
1225 if (seg_i->alloc_type == SSR)
1226 blk_off = sbi->blocks_per_seg;
1227
1228 for (j = 0; j < blk_off; j++) {
1229 struct f2fs_summary *s;
1230 s = (struct f2fs_summary *)(kaddr + offset);
1231 seg_i->sum_blk->entries[j] = *s;
1232 offset += SUMMARY_SIZE;
1233 if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1234 SUM_FOOTER_SIZE)
1235 continue;
1236
1237 f2fs_put_page(page, 1);
1238 page = NULL;
1239
1240 page = get_meta_page(sbi, start++);
1241 kaddr = (unsigned char *)page_address(page);
1242 offset = 0;
1243 }
1244 }
1245 f2fs_put_page(page, 1);
1246 return 0;
1247}
1248
1249static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
1250{
1251 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1252 struct f2fs_summary_block *sum;
1253 struct curseg_info *curseg;
1254 struct page *new;
1255 unsigned short blk_off;
1256 unsigned int segno = 0;
1257 block_t blk_addr = 0;
1258
1259 /* get segment number and block addr */
1260 if (IS_DATASEG(type)) {
1261 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
1262 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
1263 CURSEG_HOT_DATA]);
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_TYPE, type);
1266 else
1267 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
1268 } else {
1269 segno = le32_to_cpu(ckpt->cur_node_segno[type -
1270 CURSEG_HOT_NODE]);
1271 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
1272 CURSEG_HOT_NODE]);
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001273 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001274 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
1275 type - CURSEG_HOT_NODE);
1276 else
1277 blk_addr = GET_SUM_BLOCK(sbi, segno);
1278 }
1279
1280 new = get_meta_page(sbi, blk_addr);
1281 sum = (struct f2fs_summary_block *)page_address(new);
1282
1283 if (IS_NODESEG(type)) {
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001284 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001285 struct f2fs_summary *ns = &sum->entries[0];
1286 int i;
1287 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
1288 ns->version = 0;
1289 ns->ofs_in_node = 0;
1290 }
1291 } else {
1292 if (restore_node_summary(sbi, segno, sum)) {
1293 f2fs_put_page(new, 1);
1294 return -EINVAL;
1295 }
1296 }
1297 }
1298
1299 /* set uncompleted segment to curseg */
1300 curseg = CURSEG_I(sbi, type);
1301 mutex_lock(&curseg->curseg_mutex);
1302 memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
1303 curseg->next_segno = segno;
1304 reset_curseg(sbi, type, 0);
1305 curseg->alloc_type = ckpt->alloc_type[type];
1306 curseg->next_blkoff = blk_off;
1307 mutex_unlock(&curseg->curseg_mutex);
1308 f2fs_put_page(new, 1);
1309 return 0;
1310}
1311
1312static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
1313{
1314 int type = CURSEG_HOT_DATA;
1315
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001316 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001317 /* restore for compacted data summary */
1318 if (read_compacted_summaries(sbi))
1319 return -EINVAL;
1320 type = CURSEG_HOT_NODE;
1321 }
1322
1323 for (; type <= CURSEG_COLD_NODE; type++)
1324 if (read_normal_summaries(sbi, type))
1325 return -EINVAL;
1326 return 0;
1327}
1328
1329static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
1330{
1331 struct page *page;
1332 unsigned char *kaddr;
1333 struct f2fs_summary *summary;
1334 struct curseg_info *seg_i;
1335 int written_size = 0;
1336 int i, j;
1337
1338 page = grab_meta_page(sbi, blkaddr++);
1339 kaddr = (unsigned char *)page_address(page);
1340
1341 /* Step 1: write nat cache */
1342 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1343 memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
1344 written_size += SUM_JOURNAL_SIZE;
1345
1346 /* Step 2: write sit cache */
1347 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1348 memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
1349 SUM_JOURNAL_SIZE);
1350 written_size += SUM_JOURNAL_SIZE;
1351
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001352 /* Step 3: write summary entries */
1353 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1354 unsigned short blkoff;
1355 seg_i = CURSEG_I(sbi, i);
1356 if (sbi->ckpt->alloc_type[i] == SSR)
1357 blkoff = sbi->blocks_per_seg;
1358 else
1359 blkoff = curseg_blkoff(sbi, i);
1360
1361 for (j = 0; j < blkoff; j++) {
1362 if (!page) {
1363 page = grab_meta_page(sbi, blkaddr++);
1364 kaddr = (unsigned char *)page_address(page);
1365 written_size = 0;
1366 }
1367 summary = (struct f2fs_summary *)(kaddr + written_size);
1368 *summary = seg_i->sum_blk->entries[j];
1369 written_size += SUMMARY_SIZE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001370
1371 if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1372 SUM_FOOTER_SIZE)
1373 continue;
1374
Chao Yue8d61a72013-10-24 15:08:28 +08001375 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001376 f2fs_put_page(page, 1);
1377 page = NULL;
1378 }
1379 }
Chao Yue8d61a72013-10-24 15:08:28 +08001380 if (page) {
1381 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001382 f2fs_put_page(page, 1);
Chao Yue8d61a72013-10-24 15:08:28 +08001383 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001384}
1385
1386static void write_normal_summaries(struct f2fs_sb_info *sbi,
1387 block_t blkaddr, int type)
1388{
1389 int i, end;
1390 if (IS_DATASEG(type))
1391 end = type + NR_CURSEG_DATA_TYPE;
1392 else
1393 end = type + NR_CURSEG_NODE_TYPE;
1394
1395 for (i = type; i < end; i++) {
1396 struct curseg_info *sum = CURSEG_I(sbi, i);
1397 mutex_lock(&sum->curseg_mutex);
1398 write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
1399 mutex_unlock(&sum->curseg_mutex);
1400 }
1401}
1402
1403void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1404{
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001405 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001406 write_compacted_summaries(sbi, start_blk);
1407 else
1408 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
1409}
1410
1411void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1412{
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001413 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001414 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001415}
1416
1417int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
1418 unsigned int val, int alloc)
1419{
1420 int i;
1421
1422 if (type == NAT_JOURNAL) {
1423 for (i = 0; i < nats_in_cursum(sum); i++) {
1424 if (le32_to_cpu(nid_in_journal(sum, i)) == val)
1425 return i;
1426 }
1427 if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
1428 return update_nats_in_cursum(sum, 1);
1429 } else if (type == SIT_JOURNAL) {
1430 for (i = 0; i < sits_in_cursum(sum); i++)
1431 if (le32_to_cpu(segno_in_journal(sum, i)) == val)
1432 return i;
1433 if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
1434 return update_sits_in_cursum(sum, 1);
1435 }
1436 return -1;
1437}
1438
1439static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
1440 unsigned int segno)
1441{
1442 struct sit_info *sit_i = SIT_I(sbi);
1443 unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno);
1444 block_t blk_addr = sit_i->sit_base_addr + offset;
1445
1446 check_seg_range(sbi, segno);
1447
1448 /* calculate sit block address */
1449 if (f2fs_test_bit(offset, sit_i->sit_bitmap))
1450 blk_addr += sit_i->sit_blocks;
1451
1452 return get_meta_page(sbi, blk_addr);
1453}
1454
1455static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
1456 unsigned int start)
1457{
1458 struct sit_info *sit_i = SIT_I(sbi);
1459 struct page *src_page, *dst_page;
1460 pgoff_t src_off, dst_off;
1461 void *src_addr, *dst_addr;
1462
1463 src_off = current_sit_addr(sbi, start);
1464 dst_off = next_sit_addr(sbi, src_off);
1465
1466 /* get current sit block page without lock */
1467 src_page = get_meta_page(sbi, src_off);
1468 dst_page = grab_meta_page(sbi, dst_off);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001469 f2fs_bug_on(PageDirty(src_page));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001470
1471 src_addr = page_address(src_page);
1472 dst_addr = page_address(dst_page);
1473 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
1474
1475 set_page_dirty(dst_page);
1476 f2fs_put_page(src_page, 1);
1477
1478 set_to_next_sit(sit_i, start);
1479
1480 return dst_page;
1481}
1482
1483static bool flush_sits_in_journal(struct f2fs_sb_info *sbi)
1484{
1485 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1486 struct f2fs_summary_block *sum = curseg->sum_blk;
1487 int i;
1488
1489 /*
1490 * If the journal area in the current summary is full of sit entries,
1491 * all the sit entries will be flushed. Otherwise the sit entries
1492 * are not able to replace with newly hot sit entries.
1493 */
1494 if (sits_in_cursum(sum) >= SIT_JOURNAL_ENTRIES) {
1495 for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
1496 unsigned int segno;
1497 segno = le32_to_cpu(segno_in_journal(sum, i));
1498 __mark_sit_entry_dirty(sbi, segno);
1499 }
1500 update_sits_in_cursum(sum, -sits_in_cursum(sum));
Haicheng Licffbfa62013-10-18 17:24:07 +08001501 return true;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001502 }
Haicheng Licffbfa62013-10-18 17:24:07 +08001503 return false;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001504}
1505
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001506/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001507 * CP calls this function, which flushes SIT entries including sit_journal,
1508 * and moves prefree segs to free segs.
1509 */
1510void flush_sit_entries(struct f2fs_sb_info *sbi)
1511{
1512 struct sit_info *sit_i = SIT_I(sbi);
1513 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
1514 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1515 struct f2fs_summary_block *sum = curseg->sum_blk;
1516 unsigned long nsegs = TOTAL_SEGS(sbi);
1517 struct page *page = NULL;
1518 struct f2fs_sit_block *raw_sit = NULL;
1519 unsigned int start = 0, end = 0;
1520 unsigned int segno = -1;
1521 bool flushed;
1522
1523 mutex_lock(&curseg->curseg_mutex);
1524 mutex_lock(&sit_i->sentry_lock);
1525
1526 /*
1527 * "flushed" indicates whether sit entries in journal are flushed
1528 * to the SIT area or not.
1529 */
1530 flushed = flush_sits_in_journal(sbi);
1531
1532 while ((segno = find_next_bit(bitmap, nsegs, segno + 1)) < nsegs) {
1533 struct seg_entry *se = get_seg_entry(sbi, segno);
1534 int sit_offset, offset;
1535
1536 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
1537
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001538 /* add discard candidates */
1539 if (SM_I(sbi)->nr_discards < SM_I(sbi)->max_discards)
1540 add_discard_addrs(sbi, segno, se);
1541
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001542 if (flushed)
1543 goto to_sit_page;
1544
1545 offset = lookup_journal_in_cursum(sum, SIT_JOURNAL, segno, 1);
1546 if (offset >= 0) {
1547 segno_in_journal(sum, offset) = cpu_to_le32(segno);
1548 seg_info_to_raw_sit(se, &sit_in_journal(sum, offset));
1549 goto flush_done;
1550 }
1551to_sit_page:
1552 if (!page || (start > segno) || (segno > end)) {
1553 if (page) {
1554 f2fs_put_page(page, 1);
1555 page = NULL;
1556 }
1557
1558 start = START_SEGNO(sit_i, segno);
1559 end = start + SIT_ENTRY_PER_BLOCK - 1;
1560
1561 /* read sit block that will be updated */
1562 page = get_next_sit_page(sbi, start);
1563 raw_sit = page_address(page);
1564 }
1565
1566 /* udpate entry in SIT block */
1567 seg_info_to_raw_sit(se, &raw_sit->entries[sit_offset]);
1568flush_done:
1569 __clear_bit(segno, bitmap);
1570 sit_i->dirty_sentries--;
1571 }
1572 mutex_unlock(&sit_i->sentry_lock);
1573 mutex_unlock(&curseg->curseg_mutex);
1574
1575 /* writeout last modified SIT block */
1576 f2fs_put_page(page, 1);
1577
1578 set_prefree_as_free_segments(sbi);
1579}
1580
1581static int build_sit_info(struct f2fs_sb_info *sbi)
1582{
1583 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1584 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1585 struct sit_info *sit_i;
1586 unsigned int sit_segs, start;
1587 char *src_bitmap, *dst_bitmap;
1588 unsigned int bitmap_size;
1589
1590 /* allocate memory for SIT information */
1591 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
1592 if (!sit_i)
1593 return -ENOMEM;
1594
1595 SM_I(sbi)->sit_info = sit_i;
1596
1597 sit_i->sentries = vzalloc(TOTAL_SEGS(sbi) * sizeof(struct seg_entry));
1598 if (!sit_i->sentries)
1599 return -ENOMEM;
1600
1601 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1602 sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1603 if (!sit_i->dirty_sentries_bitmap)
1604 return -ENOMEM;
1605
1606 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1607 sit_i->sentries[start].cur_valid_map
1608 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1609 sit_i->sentries[start].ckpt_valid_map
1610 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1611 if (!sit_i->sentries[start].cur_valid_map
1612 || !sit_i->sentries[start].ckpt_valid_map)
1613 return -ENOMEM;
1614 }
1615
1616 if (sbi->segs_per_sec > 1) {
Jaegeuk Kim53cf9522013-03-31 12:39:49 +09001617 sit_i->sec_entries = vzalloc(TOTAL_SECS(sbi) *
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001618 sizeof(struct sec_entry));
1619 if (!sit_i->sec_entries)
1620 return -ENOMEM;
1621 }
1622
1623 /* get information related with SIT */
1624 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
1625
1626 /* setup SIT bitmap from ckeckpoint pack */
1627 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
1628 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
1629
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001630 dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001631 if (!dst_bitmap)
1632 return -ENOMEM;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001633
1634 /* init SIT information */
1635 sit_i->s_ops = &default_salloc_ops;
1636
1637 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
1638 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
1639 sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
1640 sit_i->sit_bitmap = dst_bitmap;
1641 sit_i->bitmap_size = bitmap_size;
1642 sit_i->dirty_sentries = 0;
1643 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
1644 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
1645 sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
1646 mutex_init(&sit_i->sentry_lock);
1647 return 0;
1648}
1649
1650static int build_free_segmap(struct f2fs_sb_info *sbi)
1651{
1652 struct f2fs_sm_info *sm_info = SM_I(sbi);
1653 struct free_segmap_info *free_i;
1654 unsigned int bitmap_size, sec_bitmap_size;
1655
1656 /* allocate memory for free segmap information */
1657 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
1658 if (!free_i)
1659 return -ENOMEM;
1660
1661 SM_I(sbi)->free_info = free_i;
1662
1663 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1664 free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL);
1665 if (!free_i->free_segmap)
1666 return -ENOMEM;
1667
Jaegeuk Kim53cf9522013-03-31 12:39:49 +09001668 sec_bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001669 free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL);
1670 if (!free_i->free_secmap)
1671 return -ENOMEM;
1672
1673 /* set all segments as dirty temporarily */
1674 memset(free_i->free_segmap, 0xff, bitmap_size);
1675 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
1676
1677 /* init free segmap information */
1678 free_i->start_segno =
1679 (unsigned int) GET_SEGNO_FROM_SEG0(sbi, sm_info->main_blkaddr);
1680 free_i->free_segments = 0;
1681 free_i->free_sections = 0;
1682 rwlock_init(&free_i->segmap_lock);
1683 return 0;
1684}
1685
1686static int build_curseg(struct f2fs_sb_info *sbi)
1687{
Namjae Jeon1042d602012-12-01 10:56:13 +09001688 struct curseg_info *array;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001689 int i;
1690
1691 array = kzalloc(sizeof(*array) * NR_CURSEG_TYPE, GFP_KERNEL);
1692 if (!array)
1693 return -ENOMEM;
1694
1695 SM_I(sbi)->curseg_array = array;
1696
1697 for (i = 0; i < NR_CURSEG_TYPE; i++) {
1698 mutex_init(&array[i].curseg_mutex);
1699 array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
1700 if (!array[i].sum_blk)
1701 return -ENOMEM;
1702 array[i].segno = NULL_SEGNO;
1703 array[i].next_blkoff = 0;
1704 }
1705 return restore_curseg_summaries(sbi);
1706}
1707
1708static void build_sit_entries(struct f2fs_sb_info *sbi)
1709{
1710 struct sit_info *sit_i = SIT_I(sbi);
1711 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1712 struct f2fs_summary_block *sum = curseg->sum_blk;
1713 unsigned int start;
1714
1715 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1716 struct seg_entry *se = &sit_i->sentries[start];
1717 struct f2fs_sit_block *sit_blk;
1718 struct f2fs_sit_entry sit;
1719 struct page *page;
1720 int i;
1721
1722 mutex_lock(&curseg->curseg_mutex);
1723 for (i = 0; i < sits_in_cursum(sum); i++) {
1724 if (le32_to_cpu(segno_in_journal(sum, i)) == start) {
1725 sit = sit_in_journal(sum, i);
1726 mutex_unlock(&curseg->curseg_mutex);
1727 goto got_it;
1728 }
1729 }
1730 mutex_unlock(&curseg->curseg_mutex);
1731 page = get_current_sit_page(sbi, start);
1732 sit_blk = (struct f2fs_sit_block *)page_address(page);
1733 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
1734 f2fs_put_page(page, 1);
1735got_it:
1736 check_block_count(sbi, start, &sit);
1737 seg_info_from_raw_sit(se, &sit);
1738 if (sbi->segs_per_sec > 1) {
1739 struct sec_entry *e = get_sec_entry(sbi, start);
1740 e->valid_blocks += se->valid_blocks;
1741 }
1742 }
1743}
1744
1745static void init_free_segmap(struct f2fs_sb_info *sbi)
1746{
1747 unsigned int start;
1748 int type;
1749
1750 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1751 struct seg_entry *sentry = get_seg_entry(sbi, start);
1752 if (!sentry->valid_blocks)
1753 __set_free(sbi, start);
1754 }
1755
1756 /* set use the current segments */
1757 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
1758 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
1759 __set_test_and_inuse(sbi, curseg_t->segno);
1760 }
1761}
1762
1763static void init_dirty_segmap(struct f2fs_sb_info *sbi)
1764{
1765 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1766 struct free_segmap_info *free_i = FREE_I(sbi);
Namjae Jeon8736fbf2013-06-16 09:49:11 +09001767 unsigned int segno = 0, offset = 0, total_segs = TOTAL_SEGS(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001768 unsigned short valid_blocks;
1769
Namjae Jeon8736fbf2013-06-16 09:49:11 +09001770 while (1) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001771 /* find dirty segment based on free segmap */
Namjae Jeon8736fbf2013-06-16 09:49:11 +09001772 segno = find_next_inuse(free_i, total_segs, offset);
1773 if (segno >= total_segs)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001774 break;
1775 offset = segno + 1;
1776 valid_blocks = get_valid_blocks(sbi, segno, 0);
1777 if (valid_blocks >= sbi->blocks_per_seg || !valid_blocks)
1778 continue;
1779 mutex_lock(&dirty_i->seglist_lock);
1780 __locate_dirty_segment(sbi, segno, DIRTY);
1781 mutex_unlock(&dirty_i->seglist_lock);
1782 }
1783}
1784
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001785static int init_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001786{
1787 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001788 unsigned int bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001789
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001790 dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL);
1791 if (!dirty_i->victim_secmap)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001792 return -ENOMEM;
1793 return 0;
1794}
1795
1796static int build_dirty_segmap(struct f2fs_sb_info *sbi)
1797{
1798 struct dirty_seglist_info *dirty_i;
1799 unsigned int bitmap_size, i;
1800
1801 /* allocate memory for dirty segments list information */
1802 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
1803 if (!dirty_i)
1804 return -ENOMEM;
1805
1806 SM_I(sbi)->dirty_info = dirty_i;
1807 mutex_init(&dirty_i->seglist_lock);
1808
1809 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1810
1811 for (i = 0; i < NR_DIRTY_TYPE; i++) {
1812 dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001813 if (!dirty_i->dirty_segmap[i])
1814 return -ENOMEM;
1815 }
1816
1817 init_dirty_segmap(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001818 return init_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001819}
1820
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001821/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001822 * Update min, max modified time for cost-benefit GC algorithm
1823 */
1824static void init_min_max_mtime(struct f2fs_sb_info *sbi)
1825{
1826 struct sit_info *sit_i = SIT_I(sbi);
1827 unsigned int segno;
1828
1829 mutex_lock(&sit_i->sentry_lock);
1830
1831 sit_i->min_mtime = LLONG_MAX;
1832
1833 for (segno = 0; segno < TOTAL_SEGS(sbi); segno += sbi->segs_per_sec) {
1834 unsigned int i;
1835 unsigned long long mtime = 0;
1836
1837 for (i = 0; i < sbi->segs_per_sec; i++)
1838 mtime += get_seg_entry(sbi, segno + i)->mtime;
1839
1840 mtime = div_u64(mtime, sbi->segs_per_sec);
1841
1842 if (sit_i->min_mtime > mtime)
1843 sit_i->min_mtime = mtime;
1844 }
1845 sit_i->max_mtime = get_mtime(sbi);
1846 mutex_unlock(&sit_i->sentry_lock);
1847}
1848
1849int build_segment_manager(struct f2fs_sb_info *sbi)
1850{
1851 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1852 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Namjae Jeon1042d602012-12-01 10:56:13 +09001853 struct f2fs_sm_info *sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001854 int err;
1855
1856 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
1857 if (!sm_info)
1858 return -ENOMEM;
1859
1860 /* init sm info */
1861 sbi->sm_info = sm_info;
1862 INIT_LIST_HEAD(&sm_info->wblist_head);
1863 spin_lock_init(&sm_info->wblist_lock);
1864 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
1865 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
1866 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
1867 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
1868 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
1869 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
1870 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
Jaegeuk Kim81eb8d62013-10-24 13:31:34 +09001871 sm_info->rec_prefree_segments = DEF_RECLAIM_PREFREE_SEGMENTS;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001872
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09001873 INIT_LIST_HEAD(&sm_info->discard_list);
1874 sm_info->nr_discards = 0;
1875 sm_info->max_discards = 0;
1876
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001877 err = build_sit_info(sbi);
1878 if (err)
1879 return err;
1880 err = build_free_segmap(sbi);
1881 if (err)
1882 return err;
1883 err = build_curseg(sbi);
1884 if (err)
1885 return err;
1886
1887 /* reinit free segmap based on SIT */
1888 build_sit_entries(sbi);
1889
1890 init_free_segmap(sbi);
1891 err = build_dirty_segmap(sbi);
1892 if (err)
1893 return err;
1894
1895 init_min_max_mtime(sbi);
1896 return 0;
1897}
1898
1899static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
1900 enum dirty_type dirty_type)
1901{
1902 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1903
1904 mutex_lock(&dirty_i->seglist_lock);
1905 kfree(dirty_i->dirty_segmap[dirty_type]);
1906 dirty_i->nr_dirty[dirty_type] = 0;
1907 mutex_unlock(&dirty_i->seglist_lock);
1908}
1909
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001910static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001911{
1912 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001913 kfree(dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001914}
1915
1916static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
1917{
1918 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1919 int i;
1920
1921 if (!dirty_i)
1922 return;
1923
1924 /* discard pre-free/dirty segments list */
1925 for (i = 0; i < NR_DIRTY_TYPE; i++)
1926 discard_dirty_segmap(sbi, i);
1927
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001928 destroy_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001929 SM_I(sbi)->dirty_info = NULL;
1930 kfree(dirty_i);
1931}
1932
1933static void destroy_curseg(struct f2fs_sb_info *sbi)
1934{
1935 struct curseg_info *array = SM_I(sbi)->curseg_array;
1936 int i;
1937
1938 if (!array)
1939 return;
1940 SM_I(sbi)->curseg_array = NULL;
1941 for (i = 0; i < NR_CURSEG_TYPE; i++)
1942 kfree(array[i].sum_blk);
1943 kfree(array);
1944}
1945
1946static void destroy_free_segmap(struct f2fs_sb_info *sbi)
1947{
1948 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
1949 if (!free_i)
1950 return;
1951 SM_I(sbi)->free_info = NULL;
1952 kfree(free_i->free_segmap);
1953 kfree(free_i->free_secmap);
1954 kfree(free_i);
1955}
1956
1957static void destroy_sit_info(struct f2fs_sb_info *sbi)
1958{
1959 struct sit_info *sit_i = SIT_I(sbi);
1960 unsigned int start;
1961
1962 if (!sit_i)
1963 return;
1964
1965 if (sit_i->sentries) {
1966 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1967 kfree(sit_i->sentries[start].cur_valid_map);
1968 kfree(sit_i->sentries[start].ckpt_valid_map);
1969 }
1970 }
1971 vfree(sit_i->sentries);
1972 vfree(sit_i->sec_entries);
1973 kfree(sit_i->dirty_sentries_bitmap);
1974
1975 SM_I(sbi)->sit_info = NULL;
1976 kfree(sit_i->sit_bitmap);
1977 kfree(sit_i);
1978}
1979
1980void destroy_segment_manager(struct f2fs_sb_info *sbi)
1981{
1982 struct f2fs_sm_info *sm_info = SM_I(sbi);
Chao Yu3b03f722013-11-06 09:12:04 +08001983 if (!sm_info)
1984 return;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001985 destroy_dirty_segmap(sbi);
1986 destroy_curseg(sbi);
1987 destroy_free_segmap(sbi);
1988 destroy_sit_info(sbi);
1989 sbi->sm_info = NULL;
1990 kfree(sm_info);
1991}
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09001992
1993int __init create_segment_manager_caches(void)
1994{
1995 discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
1996 sizeof(struct discard_entry), NULL);
1997 if (!discard_entry_slab)
1998 return -ENOMEM;
1999 return 0;
2000}
2001
2002void destroy_segment_manager_caches(void)
2003{
2004 kmem_cache_destroy(discard_entry_slab);
2005}