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