blob: d5043bdaa1af6fe330f171afc243c17b7614eccc [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);
839 struct bio *bio = sbi->bio[btype];
840 struct bio_private *p;
841
842 if (!bio)
843 return;
844
845 sbi->bio[btype] = NULL;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900846
847 if (type >= META_FLUSH)
848 rw = WRITE_FLUSH_FUA;
Namjae Jeon86804412013-04-25 11:45:21 +0900849 if (btype == META)
850 rw |= REQ_META;
851
Jaegeuk Kim7d5e5102013-11-18 17:13:35 +0900852 p = bio->bi_private;
853 p->sbi = sbi;
854 bio->bi_end_io = f2fs_end_io_write;
Namjae Jeon6ec178d2013-04-23 17:51:43 +0900855
Jaegeuk Kim7d5e5102013-11-18 17:13:35 +0900856 trace_f2fs_do_submit_bio(sbi->sb, btype, sync, bio);
Namjae Jeon6ec178d2013-04-23 17:51:43 +0900857
Jaegeuk Kim7d5e5102013-11-18 17:13:35 +0900858 if (type == META_FLUSH) {
859 DECLARE_COMPLETION_ONSTACK(wait);
860 p->is_sync = true;
861 p->wait = &wait;
862 submit_bio(rw, bio);
863 wait_for_completion(&wait);
864 } else {
865 p->is_sync = false;
866 submit_bio(rw, bio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900867 }
868}
869
870void f2fs_submit_bio(struct f2fs_sb_info *sbi, enum page_type type, bool sync)
871{
872 down_write(&sbi->bio_sem);
873 do_submit_bio(sbi, type, sync);
874 up_write(&sbi->bio_sem);
875}
876
877static void submit_write_page(struct f2fs_sb_info *sbi, struct page *page,
878 block_t blk_addr, enum page_type type)
879{
880 struct block_device *bdev = sbi->sb->s_bdev;
Chao Yucc7b1bb2013-09-22 15:50:50 +0800881 int bio_blocks;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900882
883 verify_block_addr(sbi, blk_addr);
884
885 down_write(&sbi->bio_sem);
886
887 inc_page_count(sbi, F2FS_WRITEBACK);
888
889 if (sbi->bio[type] && sbi->last_block_in_bio[type] != blk_addr - 1)
890 do_submit_bio(sbi, type, false);
891alloc_new:
Jaegeuk Kim3cd8a232012-12-10 09:26:05 +0900892 if (sbi->bio[type] == NULL) {
Gu Zhengd8207f62013-07-25 11:30:01 +0800893 struct bio_private *priv;
894retry:
895 priv = kmalloc(sizeof(struct bio_private), GFP_NOFS);
896 if (!priv) {
897 cond_resched();
898 goto retry;
899 }
900
Chao Yucc7b1bb2013-09-22 15:50:50 +0800901 bio_blocks = MAX_BIO_BLOCKS(max_hw_blocks(sbi));
902 sbi->bio[type] = f2fs_bio_alloc(bdev, bio_blocks);
Jaegeuk Kim3cd8a232012-12-10 09:26:05 +0900903 sbi->bio[type]->bi_sector = SECTOR_FROM_BLOCK(sbi, blk_addr);
Gu Zhengd8207f62013-07-25 11:30:01 +0800904 sbi->bio[type]->bi_private = priv;
Jaegeuk Kim3cd8a232012-12-10 09:26:05 +0900905 /*
906 * The end_io will be assigned at the sumbission phase.
907 * Until then, let bio_add_page() merge consecutive IOs as much
908 * as possible.
909 */
910 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900911
912 if (bio_add_page(sbi->bio[type], page, PAGE_CACHE_SIZE, 0) <
913 PAGE_CACHE_SIZE) {
914 do_submit_bio(sbi, type, false);
915 goto alloc_new;
916 }
917
918 sbi->last_block_in_bio[type] = blk_addr;
919
920 up_write(&sbi->bio_sem);
Namjae Jeon6ec178d2013-04-23 17:51:43 +0900921 trace_f2fs_submit_write_page(page, blk_addr, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900922}
923
Jin Xua5694692013-08-05 20:02:04 +0800924void f2fs_wait_on_page_writeback(struct page *page,
925 enum page_type type, bool sync)
926{
927 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
928 if (PageWriteback(page)) {
929 f2fs_submit_bio(sbi, type, sync);
930 wait_on_page_writeback(page);
931 }
932}
933
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900934static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
935{
936 struct curseg_info *curseg = CURSEG_I(sbi, type);
937 if (curseg->next_blkoff < sbi->blocks_per_seg)
938 return true;
939 return false;
940}
941
942static int __get_segment_type_2(struct page *page, enum page_type p_type)
943{
944 if (p_type == DATA)
945 return CURSEG_HOT_DATA;
946 else
947 return CURSEG_HOT_NODE;
948}
949
950static int __get_segment_type_4(struct page *page, enum page_type p_type)
951{
952 if (p_type == DATA) {
953 struct inode *inode = page->mapping->host;
954
955 if (S_ISDIR(inode->i_mode))
956 return CURSEG_HOT_DATA;
957 else
958 return CURSEG_COLD_DATA;
959 } else {
960 if (IS_DNODE(page) && !is_cold_node(page))
961 return CURSEG_HOT_NODE;
962 else
963 return CURSEG_COLD_NODE;
964 }
965}
966
967static int __get_segment_type_6(struct page *page, enum page_type p_type)
968{
969 if (p_type == DATA) {
970 struct inode *inode = page->mapping->host;
971
972 if (S_ISDIR(inode->i_mode))
973 return CURSEG_HOT_DATA;
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900974 else if (is_cold_data(page) || file_is_cold(inode))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900975 return CURSEG_COLD_DATA;
976 else
977 return CURSEG_WARM_DATA;
978 } else {
979 if (IS_DNODE(page))
980 return is_cold_node(page) ? CURSEG_WARM_NODE :
981 CURSEG_HOT_NODE;
982 else
983 return CURSEG_COLD_NODE;
984 }
985}
986
987static int __get_segment_type(struct page *page, enum page_type p_type)
988{
989 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
990 switch (sbi->active_logs) {
991 case 2:
992 return __get_segment_type_2(page, p_type);
993 case 4:
994 return __get_segment_type_4(page, p_type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900995 }
Jaegeuk Kim12a67142012-12-21 11:47:05 +0900996 /* NR_CURSEG_TYPE(6) logs by default */
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900997 f2fs_bug_on(sbi->active_logs != NR_CURSEG_TYPE);
Jaegeuk Kim12a67142012-12-21 11:47:05 +0900998 return __get_segment_type_6(page, p_type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900999}
1000
1001static void do_write_page(struct f2fs_sb_info *sbi, struct page *page,
1002 block_t old_blkaddr, block_t *new_blkaddr,
1003 struct f2fs_summary *sum, enum page_type p_type)
1004{
1005 struct sit_info *sit_i = SIT_I(sbi);
1006 struct curseg_info *curseg;
1007 unsigned int old_cursegno;
1008 int type;
1009
1010 type = __get_segment_type(page, p_type);
1011 curseg = CURSEG_I(sbi, type);
1012
1013 mutex_lock(&curseg->curseg_mutex);
1014
1015 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
1016 old_cursegno = curseg->segno;
1017
1018 /*
1019 * __add_sum_entry should be resided under the curseg_mutex
1020 * because, this function updates a summary entry in the
1021 * current summary block.
1022 */
Haicheng Lie79efe32013-06-13 16:59:27 +08001023 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001024
1025 mutex_lock(&sit_i->sentry_lock);
1026 __refresh_next_blkoff(sbi, curseg);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +09001027
1028 stat_inc_block_count(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001029
1030 /*
1031 * SIT information should be updated before segment allocation,
1032 * since SSR needs latest valid block information.
1033 */
1034 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
1035
1036 if (!__has_curseg_space(sbi, type))
1037 sit_i->s_ops->allocate_segment(sbi, type, false);
1038
1039 locate_dirty_segment(sbi, old_cursegno);
1040 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
1041 mutex_unlock(&sit_i->sentry_lock);
1042
1043 if (p_type == NODE)
1044 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
1045
1046 /* writeout dirty page into bdev */
1047 submit_write_page(sbi, page, *new_blkaddr, p_type);
1048
1049 mutex_unlock(&curseg->curseg_mutex);
1050}
1051
Jaegeuk Kim577e3492013-01-24 19:56:11 +09001052void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001053{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001054 set_page_writeback(page);
1055 submit_write_page(sbi, page, page->index, META);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001056}
1057
1058void write_node_page(struct f2fs_sb_info *sbi, struct page *page,
1059 unsigned int nid, block_t old_blkaddr, block_t *new_blkaddr)
1060{
1061 struct f2fs_summary sum;
1062 set_summary(&sum, nid, 0, 0);
1063 do_write_page(sbi, page, old_blkaddr, new_blkaddr, &sum, NODE);
1064}
1065
1066void write_data_page(struct inode *inode, struct page *page,
1067 struct dnode_of_data *dn, block_t old_blkaddr,
1068 block_t *new_blkaddr)
1069{
1070 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1071 struct f2fs_summary sum;
1072 struct node_info ni;
1073
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001074 f2fs_bug_on(old_blkaddr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001075 get_node_info(sbi, dn->nid, &ni);
1076 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1077
1078 do_write_page(sbi, page, old_blkaddr,
1079 new_blkaddr, &sum, DATA);
1080}
1081
1082void rewrite_data_page(struct f2fs_sb_info *sbi, struct page *page,
1083 block_t old_blk_addr)
1084{
1085 submit_write_page(sbi, page, old_blk_addr, DATA);
1086}
1087
1088void recover_data_page(struct f2fs_sb_info *sbi,
1089 struct page *page, struct f2fs_summary *sum,
1090 block_t old_blkaddr, block_t new_blkaddr)
1091{
1092 struct sit_info *sit_i = SIT_I(sbi);
1093 struct curseg_info *curseg;
1094 unsigned int segno, old_cursegno;
1095 struct seg_entry *se;
1096 int type;
1097
1098 segno = GET_SEGNO(sbi, new_blkaddr);
1099 se = get_seg_entry(sbi, segno);
1100 type = se->type;
1101
1102 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
1103 if (old_blkaddr == NULL_ADDR)
1104 type = CURSEG_COLD_DATA;
1105 else
1106 type = CURSEG_WARM_DATA;
1107 }
1108 curseg = CURSEG_I(sbi, type);
1109
1110 mutex_lock(&curseg->curseg_mutex);
1111 mutex_lock(&sit_i->sentry_lock);
1112
1113 old_cursegno = curseg->segno;
1114
1115 /* change the current segment */
1116 if (segno != curseg->segno) {
1117 curseg->next_segno = segno;
1118 change_curseg(sbi, type, true);
1119 }
1120
1121 curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, new_blkaddr) &
1122 (sbi->blocks_per_seg - 1);
Haicheng Lie79efe32013-06-13 16:59:27 +08001123 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001124
1125 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
1126
1127 locate_dirty_segment(sbi, old_cursegno);
1128 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
1129
1130 mutex_unlock(&sit_i->sentry_lock);
1131 mutex_unlock(&curseg->curseg_mutex);
1132}
1133
1134void rewrite_node_page(struct f2fs_sb_info *sbi,
1135 struct page *page, struct f2fs_summary *sum,
1136 block_t old_blkaddr, block_t new_blkaddr)
1137{
1138 struct sit_info *sit_i = SIT_I(sbi);
1139 int type = CURSEG_WARM_NODE;
1140 struct curseg_info *curseg;
1141 unsigned int segno, old_cursegno;
1142 block_t next_blkaddr = next_blkaddr_of_node(page);
1143 unsigned int next_segno = GET_SEGNO(sbi, next_blkaddr);
1144
1145 curseg = CURSEG_I(sbi, type);
1146
1147 mutex_lock(&curseg->curseg_mutex);
1148 mutex_lock(&sit_i->sentry_lock);
1149
1150 segno = GET_SEGNO(sbi, new_blkaddr);
1151 old_cursegno = curseg->segno;
1152
1153 /* change the current segment */
1154 if (segno != curseg->segno) {
1155 curseg->next_segno = segno;
1156 change_curseg(sbi, type, true);
1157 }
1158 curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, new_blkaddr) &
1159 (sbi->blocks_per_seg - 1);
Haicheng Lie79efe32013-06-13 16:59:27 +08001160 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001161
1162 /* change the current log to the next block addr in advance */
1163 if (next_segno != segno) {
1164 curseg->next_segno = next_segno;
1165 change_curseg(sbi, type, true);
1166 }
1167 curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, next_blkaddr) &
1168 (sbi->blocks_per_seg - 1);
1169
1170 /* rewrite node page */
1171 set_page_writeback(page);
1172 submit_write_page(sbi, page, new_blkaddr, NODE);
1173 f2fs_submit_bio(sbi, NODE, true);
1174 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
1175
1176 locate_dirty_segment(sbi, old_cursegno);
1177 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
1178
1179 mutex_unlock(&sit_i->sentry_lock);
1180 mutex_unlock(&curseg->curseg_mutex);
1181}
1182
1183static int read_compacted_summaries(struct f2fs_sb_info *sbi)
1184{
1185 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1186 struct curseg_info *seg_i;
1187 unsigned char *kaddr;
1188 struct page *page;
1189 block_t start;
1190 int i, j, offset;
1191
1192 start = start_sum_block(sbi);
1193
1194 page = get_meta_page(sbi, start++);
1195 kaddr = (unsigned char *)page_address(page);
1196
1197 /* Step 1: restore nat cache */
1198 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1199 memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
1200
1201 /* Step 2: restore sit cache */
1202 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1203 memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
1204 SUM_JOURNAL_SIZE);
1205 offset = 2 * SUM_JOURNAL_SIZE;
1206
1207 /* Step 3: restore summary entries */
1208 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1209 unsigned short blk_off;
1210 unsigned int segno;
1211
1212 seg_i = CURSEG_I(sbi, i);
1213 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
1214 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
1215 seg_i->next_segno = segno;
1216 reset_curseg(sbi, i, 0);
1217 seg_i->alloc_type = ckpt->alloc_type[i];
1218 seg_i->next_blkoff = blk_off;
1219
1220 if (seg_i->alloc_type == SSR)
1221 blk_off = sbi->blocks_per_seg;
1222
1223 for (j = 0; j < blk_off; j++) {
1224 struct f2fs_summary *s;
1225 s = (struct f2fs_summary *)(kaddr + offset);
1226 seg_i->sum_blk->entries[j] = *s;
1227 offset += SUMMARY_SIZE;
1228 if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1229 SUM_FOOTER_SIZE)
1230 continue;
1231
1232 f2fs_put_page(page, 1);
1233 page = NULL;
1234
1235 page = get_meta_page(sbi, start++);
1236 kaddr = (unsigned char *)page_address(page);
1237 offset = 0;
1238 }
1239 }
1240 f2fs_put_page(page, 1);
1241 return 0;
1242}
1243
1244static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
1245{
1246 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1247 struct f2fs_summary_block *sum;
1248 struct curseg_info *curseg;
1249 struct page *new;
1250 unsigned short blk_off;
1251 unsigned int segno = 0;
1252 block_t blk_addr = 0;
1253
1254 /* get segment number and block addr */
1255 if (IS_DATASEG(type)) {
1256 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
1257 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
1258 CURSEG_HOT_DATA]);
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001259 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001260 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
1261 else
1262 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
1263 } else {
1264 segno = le32_to_cpu(ckpt->cur_node_segno[type -
1265 CURSEG_HOT_NODE]);
1266 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
1267 CURSEG_HOT_NODE]);
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001268 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001269 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
1270 type - CURSEG_HOT_NODE);
1271 else
1272 blk_addr = GET_SUM_BLOCK(sbi, segno);
1273 }
1274
1275 new = get_meta_page(sbi, blk_addr);
1276 sum = (struct f2fs_summary_block *)page_address(new);
1277
1278 if (IS_NODESEG(type)) {
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001279 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001280 struct f2fs_summary *ns = &sum->entries[0];
1281 int i;
1282 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
1283 ns->version = 0;
1284 ns->ofs_in_node = 0;
1285 }
1286 } else {
1287 if (restore_node_summary(sbi, segno, sum)) {
1288 f2fs_put_page(new, 1);
1289 return -EINVAL;
1290 }
1291 }
1292 }
1293
1294 /* set uncompleted segment to curseg */
1295 curseg = CURSEG_I(sbi, type);
1296 mutex_lock(&curseg->curseg_mutex);
1297 memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
1298 curseg->next_segno = segno;
1299 reset_curseg(sbi, type, 0);
1300 curseg->alloc_type = ckpt->alloc_type[type];
1301 curseg->next_blkoff = blk_off;
1302 mutex_unlock(&curseg->curseg_mutex);
1303 f2fs_put_page(new, 1);
1304 return 0;
1305}
1306
1307static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
1308{
1309 int type = CURSEG_HOT_DATA;
1310
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001311 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001312 /* restore for compacted data summary */
1313 if (read_compacted_summaries(sbi))
1314 return -EINVAL;
1315 type = CURSEG_HOT_NODE;
1316 }
1317
1318 for (; type <= CURSEG_COLD_NODE; type++)
1319 if (read_normal_summaries(sbi, type))
1320 return -EINVAL;
1321 return 0;
1322}
1323
1324static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
1325{
1326 struct page *page;
1327 unsigned char *kaddr;
1328 struct f2fs_summary *summary;
1329 struct curseg_info *seg_i;
1330 int written_size = 0;
1331 int i, j;
1332
1333 page = grab_meta_page(sbi, blkaddr++);
1334 kaddr = (unsigned char *)page_address(page);
1335
1336 /* Step 1: write nat cache */
1337 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1338 memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
1339 written_size += SUM_JOURNAL_SIZE;
1340
1341 /* Step 2: write sit cache */
1342 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1343 memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
1344 SUM_JOURNAL_SIZE);
1345 written_size += SUM_JOURNAL_SIZE;
1346
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001347 /* Step 3: write summary entries */
1348 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1349 unsigned short blkoff;
1350 seg_i = CURSEG_I(sbi, i);
1351 if (sbi->ckpt->alloc_type[i] == SSR)
1352 blkoff = sbi->blocks_per_seg;
1353 else
1354 blkoff = curseg_blkoff(sbi, i);
1355
1356 for (j = 0; j < blkoff; j++) {
1357 if (!page) {
1358 page = grab_meta_page(sbi, blkaddr++);
1359 kaddr = (unsigned char *)page_address(page);
1360 written_size = 0;
1361 }
1362 summary = (struct f2fs_summary *)(kaddr + written_size);
1363 *summary = seg_i->sum_blk->entries[j];
1364 written_size += SUMMARY_SIZE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001365
1366 if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1367 SUM_FOOTER_SIZE)
1368 continue;
1369
Chao Yue8d61a72013-10-24 15:08:28 +08001370 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001371 f2fs_put_page(page, 1);
1372 page = NULL;
1373 }
1374 }
Chao Yue8d61a72013-10-24 15:08:28 +08001375 if (page) {
1376 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001377 f2fs_put_page(page, 1);
Chao Yue8d61a72013-10-24 15:08:28 +08001378 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001379}
1380
1381static void write_normal_summaries(struct f2fs_sb_info *sbi,
1382 block_t blkaddr, int type)
1383{
1384 int i, end;
1385 if (IS_DATASEG(type))
1386 end = type + NR_CURSEG_DATA_TYPE;
1387 else
1388 end = type + NR_CURSEG_NODE_TYPE;
1389
1390 for (i = type; i < end; i++) {
1391 struct curseg_info *sum = CURSEG_I(sbi, i);
1392 mutex_lock(&sum->curseg_mutex);
1393 write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
1394 mutex_unlock(&sum->curseg_mutex);
1395 }
1396}
1397
1398void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1399{
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001400 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001401 write_compacted_summaries(sbi, start_blk);
1402 else
1403 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
1404}
1405
1406void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1407{
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001408 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001409 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001410}
1411
1412int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
1413 unsigned int val, int alloc)
1414{
1415 int i;
1416
1417 if (type == NAT_JOURNAL) {
1418 for (i = 0; i < nats_in_cursum(sum); i++) {
1419 if (le32_to_cpu(nid_in_journal(sum, i)) == val)
1420 return i;
1421 }
1422 if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
1423 return update_nats_in_cursum(sum, 1);
1424 } else if (type == SIT_JOURNAL) {
1425 for (i = 0; i < sits_in_cursum(sum); i++)
1426 if (le32_to_cpu(segno_in_journal(sum, i)) == val)
1427 return i;
1428 if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
1429 return update_sits_in_cursum(sum, 1);
1430 }
1431 return -1;
1432}
1433
1434static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
1435 unsigned int segno)
1436{
1437 struct sit_info *sit_i = SIT_I(sbi);
1438 unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno);
1439 block_t blk_addr = sit_i->sit_base_addr + offset;
1440
1441 check_seg_range(sbi, segno);
1442
1443 /* calculate sit block address */
1444 if (f2fs_test_bit(offset, sit_i->sit_bitmap))
1445 blk_addr += sit_i->sit_blocks;
1446
1447 return get_meta_page(sbi, blk_addr);
1448}
1449
1450static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
1451 unsigned int start)
1452{
1453 struct sit_info *sit_i = SIT_I(sbi);
1454 struct page *src_page, *dst_page;
1455 pgoff_t src_off, dst_off;
1456 void *src_addr, *dst_addr;
1457
1458 src_off = current_sit_addr(sbi, start);
1459 dst_off = next_sit_addr(sbi, src_off);
1460
1461 /* get current sit block page without lock */
1462 src_page = get_meta_page(sbi, src_off);
1463 dst_page = grab_meta_page(sbi, dst_off);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001464 f2fs_bug_on(PageDirty(src_page));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001465
1466 src_addr = page_address(src_page);
1467 dst_addr = page_address(dst_page);
1468 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
1469
1470 set_page_dirty(dst_page);
1471 f2fs_put_page(src_page, 1);
1472
1473 set_to_next_sit(sit_i, start);
1474
1475 return dst_page;
1476}
1477
1478static bool flush_sits_in_journal(struct f2fs_sb_info *sbi)
1479{
1480 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1481 struct f2fs_summary_block *sum = curseg->sum_blk;
1482 int i;
1483
1484 /*
1485 * If the journal area in the current summary is full of sit entries,
1486 * all the sit entries will be flushed. Otherwise the sit entries
1487 * are not able to replace with newly hot sit entries.
1488 */
1489 if (sits_in_cursum(sum) >= SIT_JOURNAL_ENTRIES) {
1490 for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
1491 unsigned int segno;
1492 segno = le32_to_cpu(segno_in_journal(sum, i));
1493 __mark_sit_entry_dirty(sbi, segno);
1494 }
1495 update_sits_in_cursum(sum, -sits_in_cursum(sum));
Haicheng Licffbfa62013-10-18 17:24:07 +08001496 return true;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001497 }
Haicheng Licffbfa62013-10-18 17:24:07 +08001498 return false;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001499}
1500
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001501/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001502 * CP calls this function, which flushes SIT entries including sit_journal,
1503 * and moves prefree segs to free segs.
1504 */
1505void flush_sit_entries(struct f2fs_sb_info *sbi)
1506{
1507 struct sit_info *sit_i = SIT_I(sbi);
1508 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
1509 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1510 struct f2fs_summary_block *sum = curseg->sum_blk;
1511 unsigned long nsegs = TOTAL_SEGS(sbi);
1512 struct page *page = NULL;
1513 struct f2fs_sit_block *raw_sit = NULL;
1514 unsigned int start = 0, end = 0;
1515 unsigned int segno = -1;
1516 bool flushed;
1517
1518 mutex_lock(&curseg->curseg_mutex);
1519 mutex_lock(&sit_i->sentry_lock);
1520
1521 /*
1522 * "flushed" indicates whether sit entries in journal are flushed
1523 * to the SIT area or not.
1524 */
1525 flushed = flush_sits_in_journal(sbi);
1526
1527 while ((segno = find_next_bit(bitmap, nsegs, segno + 1)) < nsegs) {
1528 struct seg_entry *se = get_seg_entry(sbi, segno);
1529 int sit_offset, offset;
1530
1531 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
1532
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001533 /* add discard candidates */
1534 if (SM_I(sbi)->nr_discards < SM_I(sbi)->max_discards)
1535 add_discard_addrs(sbi, segno, se);
1536
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001537 if (flushed)
1538 goto to_sit_page;
1539
1540 offset = lookup_journal_in_cursum(sum, SIT_JOURNAL, segno, 1);
1541 if (offset >= 0) {
1542 segno_in_journal(sum, offset) = cpu_to_le32(segno);
1543 seg_info_to_raw_sit(se, &sit_in_journal(sum, offset));
1544 goto flush_done;
1545 }
1546to_sit_page:
1547 if (!page || (start > segno) || (segno > end)) {
1548 if (page) {
1549 f2fs_put_page(page, 1);
1550 page = NULL;
1551 }
1552
1553 start = START_SEGNO(sit_i, segno);
1554 end = start + SIT_ENTRY_PER_BLOCK - 1;
1555
1556 /* read sit block that will be updated */
1557 page = get_next_sit_page(sbi, start);
1558 raw_sit = page_address(page);
1559 }
1560
1561 /* udpate entry in SIT block */
1562 seg_info_to_raw_sit(se, &raw_sit->entries[sit_offset]);
1563flush_done:
1564 __clear_bit(segno, bitmap);
1565 sit_i->dirty_sentries--;
1566 }
1567 mutex_unlock(&sit_i->sentry_lock);
1568 mutex_unlock(&curseg->curseg_mutex);
1569
1570 /* writeout last modified SIT block */
1571 f2fs_put_page(page, 1);
1572
1573 set_prefree_as_free_segments(sbi);
1574}
1575
1576static int build_sit_info(struct f2fs_sb_info *sbi)
1577{
1578 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1579 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1580 struct sit_info *sit_i;
1581 unsigned int sit_segs, start;
1582 char *src_bitmap, *dst_bitmap;
1583 unsigned int bitmap_size;
1584
1585 /* allocate memory for SIT information */
1586 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
1587 if (!sit_i)
1588 return -ENOMEM;
1589
1590 SM_I(sbi)->sit_info = sit_i;
1591
1592 sit_i->sentries = vzalloc(TOTAL_SEGS(sbi) * sizeof(struct seg_entry));
1593 if (!sit_i->sentries)
1594 return -ENOMEM;
1595
1596 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1597 sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1598 if (!sit_i->dirty_sentries_bitmap)
1599 return -ENOMEM;
1600
1601 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1602 sit_i->sentries[start].cur_valid_map
1603 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1604 sit_i->sentries[start].ckpt_valid_map
1605 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1606 if (!sit_i->sentries[start].cur_valid_map
1607 || !sit_i->sentries[start].ckpt_valid_map)
1608 return -ENOMEM;
1609 }
1610
1611 if (sbi->segs_per_sec > 1) {
Jaegeuk Kim53cf9522013-03-31 12:39:49 +09001612 sit_i->sec_entries = vzalloc(TOTAL_SECS(sbi) *
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001613 sizeof(struct sec_entry));
1614 if (!sit_i->sec_entries)
1615 return -ENOMEM;
1616 }
1617
1618 /* get information related with SIT */
1619 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
1620
1621 /* setup SIT bitmap from ckeckpoint pack */
1622 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
1623 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
1624
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001625 dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001626 if (!dst_bitmap)
1627 return -ENOMEM;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001628
1629 /* init SIT information */
1630 sit_i->s_ops = &default_salloc_ops;
1631
1632 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
1633 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
1634 sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
1635 sit_i->sit_bitmap = dst_bitmap;
1636 sit_i->bitmap_size = bitmap_size;
1637 sit_i->dirty_sentries = 0;
1638 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
1639 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
1640 sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
1641 mutex_init(&sit_i->sentry_lock);
1642 return 0;
1643}
1644
1645static int build_free_segmap(struct f2fs_sb_info *sbi)
1646{
1647 struct f2fs_sm_info *sm_info = SM_I(sbi);
1648 struct free_segmap_info *free_i;
1649 unsigned int bitmap_size, sec_bitmap_size;
1650
1651 /* allocate memory for free segmap information */
1652 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
1653 if (!free_i)
1654 return -ENOMEM;
1655
1656 SM_I(sbi)->free_info = free_i;
1657
1658 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1659 free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL);
1660 if (!free_i->free_segmap)
1661 return -ENOMEM;
1662
Jaegeuk Kim53cf9522013-03-31 12:39:49 +09001663 sec_bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001664 free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL);
1665 if (!free_i->free_secmap)
1666 return -ENOMEM;
1667
1668 /* set all segments as dirty temporarily */
1669 memset(free_i->free_segmap, 0xff, bitmap_size);
1670 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
1671
1672 /* init free segmap information */
1673 free_i->start_segno =
1674 (unsigned int) GET_SEGNO_FROM_SEG0(sbi, sm_info->main_blkaddr);
1675 free_i->free_segments = 0;
1676 free_i->free_sections = 0;
1677 rwlock_init(&free_i->segmap_lock);
1678 return 0;
1679}
1680
1681static int build_curseg(struct f2fs_sb_info *sbi)
1682{
Namjae Jeon1042d602012-12-01 10:56:13 +09001683 struct curseg_info *array;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001684 int i;
1685
1686 array = kzalloc(sizeof(*array) * NR_CURSEG_TYPE, GFP_KERNEL);
1687 if (!array)
1688 return -ENOMEM;
1689
1690 SM_I(sbi)->curseg_array = array;
1691
1692 for (i = 0; i < NR_CURSEG_TYPE; i++) {
1693 mutex_init(&array[i].curseg_mutex);
1694 array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
1695 if (!array[i].sum_blk)
1696 return -ENOMEM;
1697 array[i].segno = NULL_SEGNO;
1698 array[i].next_blkoff = 0;
1699 }
1700 return restore_curseg_summaries(sbi);
1701}
1702
1703static void build_sit_entries(struct f2fs_sb_info *sbi)
1704{
1705 struct sit_info *sit_i = SIT_I(sbi);
1706 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1707 struct f2fs_summary_block *sum = curseg->sum_blk;
1708 unsigned int start;
1709
1710 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1711 struct seg_entry *se = &sit_i->sentries[start];
1712 struct f2fs_sit_block *sit_blk;
1713 struct f2fs_sit_entry sit;
1714 struct page *page;
1715 int i;
1716
1717 mutex_lock(&curseg->curseg_mutex);
1718 for (i = 0; i < sits_in_cursum(sum); i++) {
1719 if (le32_to_cpu(segno_in_journal(sum, i)) == start) {
1720 sit = sit_in_journal(sum, i);
1721 mutex_unlock(&curseg->curseg_mutex);
1722 goto got_it;
1723 }
1724 }
1725 mutex_unlock(&curseg->curseg_mutex);
1726 page = get_current_sit_page(sbi, start);
1727 sit_blk = (struct f2fs_sit_block *)page_address(page);
1728 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
1729 f2fs_put_page(page, 1);
1730got_it:
1731 check_block_count(sbi, start, &sit);
1732 seg_info_from_raw_sit(se, &sit);
1733 if (sbi->segs_per_sec > 1) {
1734 struct sec_entry *e = get_sec_entry(sbi, start);
1735 e->valid_blocks += se->valid_blocks;
1736 }
1737 }
1738}
1739
1740static void init_free_segmap(struct f2fs_sb_info *sbi)
1741{
1742 unsigned int start;
1743 int type;
1744
1745 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1746 struct seg_entry *sentry = get_seg_entry(sbi, start);
1747 if (!sentry->valid_blocks)
1748 __set_free(sbi, start);
1749 }
1750
1751 /* set use the current segments */
1752 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
1753 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
1754 __set_test_and_inuse(sbi, curseg_t->segno);
1755 }
1756}
1757
1758static void init_dirty_segmap(struct f2fs_sb_info *sbi)
1759{
1760 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1761 struct free_segmap_info *free_i = FREE_I(sbi);
Namjae Jeon8736fbf2013-06-16 09:49:11 +09001762 unsigned int segno = 0, offset = 0, total_segs = TOTAL_SEGS(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001763 unsigned short valid_blocks;
1764
Namjae Jeon8736fbf2013-06-16 09:49:11 +09001765 while (1) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001766 /* find dirty segment based on free segmap */
Namjae Jeon8736fbf2013-06-16 09:49:11 +09001767 segno = find_next_inuse(free_i, total_segs, offset);
1768 if (segno >= total_segs)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001769 break;
1770 offset = segno + 1;
1771 valid_blocks = get_valid_blocks(sbi, segno, 0);
1772 if (valid_blocks >= sbi->blocks_per_seg || !valid_blocks)
1773 continue;
1774 mutex_lock(&dirty_i->seglist_lock);
1775 __locate_dirty_segment(sbi, segno, DIRTY);
1776 mutex_unlock(&dirty_i->seglist_lock);
1777 }
1778}
1779
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001780static int init_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001781{
1782 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001783 unsigned int bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001784
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001785 dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL);
1786 if (!dirty_i->victim_secmap)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001787 return -ENOMEM;
1788 return 0;
1789}
1790
1791static int build_dirty_segmap(struct f2fs_sb_info *sbi)
1792{
1793 struct dirty_seglist_info *dirty_i;
1794 unsigned int bitmap_size, i;
1795
1796 /* allocate memory for dirty segments list information */
1797 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
1798 if (!dirty_i)
1799 return -ENOMEM;
1800
1801 SM_I(sbi)->dirty_info = dirty_i;
1802 mutex_init(&dirty_i->seglist_lock);
1803
1804 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1805
1806 for (i = 0; i < NR_DIRTY_TYPE; i++) {
1807 dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001808 if (!dirty_i->dirty_segmap[i])
1809 return -ENOMEM;
1810 }
1811
1812 init_dirty_segmap(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001813 return init_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001814}
1815
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001816/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001817 * Update min, max modified time for cost-benefit GC algorithm
1818 */
1819static void init_min_max_mtime(struct f2fs_sb_info *sbi)
1820{
1821 struct sit_info *sit_i = SIT_I(sbi);
1822 unsigned int segno;
1823
1824 mutex_lock(&sit_i->sentry_lock);
1825
1826 sit_i->min_mtime = LLONG_MAX;
1827
1828 for (segno = 0; segno < TOTAL_SEGS(sbi); segno += sbi->segs_per_sec) {
1829 unsigned int i;
1830 unsigned long long mtime = 0;
1831
1832 for (i = 0; i < sbi->segs_per_sec; i++)
1833 mtime += get_seg_entry(sbi, segno + i)->mtime;
1834
1835 mtime = div_u64(mtime, sbi->segs_per_sec);
1836
1837 if (sit_i->min_mtime > mtime)
1838 sit_i->min_mtime = mtime;
1839 }
1840 sit_i->max_mtime = get_mtime(sbi);
1841 mutex_unlock(&sit_i->sentry_lock);
1842}
1843
1844int build_segment_manager(struct f2fs_sb_info *sbi)
1845{
1846 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1847 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Namjae Jeon1042d602012-12-01 10:56:13 +09001848 struct f2fs_sm_info *sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001849 int err;
1850
1851 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
1852 if (!sm_info)
1853 return -ENOMEM;
1854
1855 /* init sm info */
1856 sbi->sm_info = sm_info;
1857 INIT_LIST_HEAD(&sm_info->wblist_head);
1858 spin_lock_init(&sm_info->wblist_lock);
1859 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
1860 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
1861 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
1862 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
1863 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
1864 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
1865 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
Jaegeuk Kim81eb8d62013-10-24 13:31:34 +09001866 sm_info->rec_prefree_segments = DEF_RECLAIM_PREFREE_SEGMENTS;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001867
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09001868 INIT_LIST_HEAD(&sm_info->discard_list);
1869 sm_info->nr_discards = 0;
1870 sm_info->max_discards = 0;
1871
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001872 err = build_sit_info(sbi);
1873 if (err)
1874 return err;
1875 err = build_free_segmap(sbi);
1876 if (err)
1877 return err;
1878 err = build_curseg(sbi);
1879 if (err)
1880 return err;
1881
1882 /* reinit free segmap based on SIT */
1883 build_sit_entries(sbi);
1884
1885 init_free_segmap(sbi);
1886 err = build_dirty_segmap(sbi);
1887 if (err)
1888 return err;
1889
1890 init_min_max_mtime(sbi);
1891 return 0;
1892}
1893
1894static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
1895 enum dirty_type dirty_type)
1896{
1897 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1898
1899 mutex_lock(&dirty_i->seglist_lock);
1900 kfree(dirty_i->dirty_segmap[dirty_type]);
1901 dirty_i->nr_dirty[dirty_type] = 0;
1902 mutex_unlock(&dirty_i->seglist_lock);
1903}
1904
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001905static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001906{
1907 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001908 kfree(dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001909}
1910
1911static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
1912{
1913 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1914 int i;
1915
1916 if (!dirty_i)
1917 return;
1918
1919 /* discard pre-free/dirty segments list */
1920 for (i = 0; i < NR_DIRTY_TYPE; i++)
1921 discard_dirty_segmap(sbi, i);
1922
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001923 destroy_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001924 SM_I(sbi)->dirty_info = NULL;
1925 kfree(dirty_i);
1926}
1927
1928static void destroy_curseg(struct f2fs_sb_info *sbi)
1929{
1930 struct curseg_info *array = SM_I(sbi)->curseg_array;
1931 int i;
1932
1933 if (!array)
1934 return;
1935 SM_I(sbi)->curseg_array = NULL;
1936 for (i = 0; i < NR_CURSEG_TYPE; i++)
1937 kfree(array[i].sum_blk);
1938 kfree(array);
1939}
1940
1941static void destroy_free_segmap(struct f2fs_sb_info *sbi)
1942{
1943 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
1944 if (!free_i)
1945 return;
1946 SM_I(sbi)->free_info = NULL;
1947 kfree(free_i->free_segmap);
1948 kfree(free_i->free_secmap);
1949 kfree(free_i);
1950}
1951
1952static void destroy_sit_info(struct f2fs_sb_info *sbi)
1953{
1954 struct sit_info *sit_i = SIT_I(sbi);
1955 unsigned int start;
1956
1957 if (!sit_i)
1958 return;
1959
1960 if (sit_i->sentries) {
1961 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1962 kfree(sit_i->sentries[start].cur_valid_map);
1963 kfree(sit_i->sentries[start].ckpt_valid_map);
1964 }
1965 }
1966 vfree(sit_i->sentries);
1967 vfree(sit_i->sec_entries);
1968 kfree(sit_i->dirty_sentries_bitmap);
1969
1970 SM_I(sbi)->sit_info = NULL;
1971 kfree(sit_i->sit_bitmap);
1972 kfree(sit_i);
1973}
1974
1975void destroy_segment_manager(struct f2fs_sb_info *sbi)
1976{
1977 struct f2fs_sm_info *sm_info = SM_I(sbi);
Chao Yu3b03f722013-11-06 09:12:04 +08001978 if (!sm_info)
1979 return;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001980 destroy_dirty_segmap(sbi);
1981 destroy_curseg(sbi);
1982 destroy_free_segmap(sbi);
1983 destroy_sit_info(sbi);
1984 sbi->sm_info = NULL;
1985 kfree(sm_info);
1986}
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09001987
1988int __init create_segment_manager_caches(void)
1989{
1990 discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
1991 sizeof(struct discard_entry), NULL);
1992 if (!discard_entry_slab)
1993 return -ENOMEM;
1994 return 0;
1995}
1996
1997void destroy_segment_manager_caches(void)
1998{
1999 kmem_cache_destroy(discard_entry_slab);
2000}