blob: 524b7ed36e0acc8a24cd4d558f440cf9e9d39826 [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 Kim6b4afdd2014-04-02 15:34:36 +090016#include <linux/kthread.h>
Jaegeuk Kim351df4b2012-11-02 17:09:16 +090017#include <linux/vmalloc.h>
Chao Yu74de5932013-11-22 09:09:59 +080018#include <linux/swap.h>
Jaegeuk Kim351df4b2012-11-02 17:09:16 +090019
20#include "f2fs.h"
21#include "segment.h"
22#include "node.h"
Namjae Jeon6ec178d2013-04-23 17:51:43 +090023#include <trace/events/f2fs.h>
Jaegeuk Kim351df4b2012-11-02 17:09:16 +090024
Changman Lee9a7f1432013-11-15 10:42:51 +090025#define __reverse_ffz(x) __reverse_ffs(~(x))
26
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +090027static struct kmem_cache *discard_entry_slab;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +090028static struct kmem_cache *flush_cmd_slab;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +090029
Changman Lee9a7f1432013-11-15 10:42:51 +090030/*
31 * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since
32 * MSB and LSB are reversed in a byte by f2fs_set_bit.
33 */
34static inline unsigned long __reverse_ffs(unsigned long word)
35{
36 int num = 0;
37
38#if BITS_PER_LONG == 64
39 if ((word & 0xffffffff) == 0) {
40 num += 32;
41 word >>= 32;
42 }
43#endif
44 if ((word & 0xffff) == 0) {
45 num += 16;
46 word >>= 16;
47 }
48 if ((word & 0xff) == 0) {
49 num += 8;
50 word >>= 8;
51 }
52 if ((word & 0xf0) == 0)
53 num += 4;
54 else
55 word >>= 4;
56 if ((word & 0xc) == 0)
57 num += 2;
58 else
59 word >>= 2;
60 if ((word & 0x2) == 0)
61 num += 1;
62 return num;
63}
64
65/*
66 * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c becasue
67 * f2fs_set_bit makes MSB and LSB reversed in a byte.
68 * Example:
69 * LSB <--> MSB
70 * f2fs_set_bit(0, bitmap) => 0000 0001
71 * f2fs_set_bit(7, bitmap) => 1000 0000
72 */
73static unsigned long __find_rev_next_bit(const unsigned long *addr,
74 unsigned long size, unsigned long offset)
75{
76 const unsigned long *p = addr + BIT_WORD(offset);
77 unsigned long result = offset & ~(BITS_PER_LONG - 1);
78 unsigned long tmp;
79 unsigned long mask, submask;
80 unsigned long quot, rest;
81
82 if (offset >= size)
83 return size;
84
85 size -= result;
86 offset %= BITS_PER_LONG;
87 if (!offset)
88 goto aligned;
89
90 tmp = *(p++);
91 quot = (offset >> 3) << 3;
92 rest = offset & 0x7;
93 mask = ~0UL << quot;
94 submask = (unsigned char)(0xff << rest) >> rest;
95 submask <<= quot;
96 mask &= submask;
97 tmp &= mask;
98 if (size < BITS_PER_LONG)
99 goto found_first;
100 if (tmp)
101 goto found_middle;
102
103 size -= BITS_PER_LONG;
104 result += BITS_PER_LONG;
105aligned:
106 while (size & ~(BITS_PER_LONG-1)) {
107 tmp = *(p++);
108 if (tmp)
109 goto found_middle;
110 result += BITS_PER_LONG;
111 size -= BITS_PER_LONG;
112 }
113 if (!size)
114 return result;
115 tmp = *p;
116found_first:
117 tmp &= (~0UL >> (BITS_PER_LONG - size));
118 if (tmp == 0UL) /* Are any bits set? */
119 return result + size; /* Nope. */
120found_middle:
121 return result + __reverse_ffs(tmp);
122}
123
124static unsigned long __find_rev_next_zero_bit(const unsigned long *addr,
125 unsigned long size, unsigned long offset)
126{
127 const unsigned long *p = addr + BIT_WORD(offset);
128 unsigned long result = offset & ~(BITS_PER_LONG - 1);
129 unsigned long tmp;
130 unsigned long mask, submask;
131 unsigned long quot, rest;
132
133 if (offset >= size)
134 return size;
135
136 size -= result;
137 offset %= BITS_PER_LONG;
138 if (!offset)
139 goto aligned;
140
141 tmp = *(p++);
142 quot = (offset >> 3) << 3;
143 rest = offset & 0x7;
144 mask = ~(~0UL << quot);
145 submask = (unsigned char)~((unsigned char)(0xff << rest) >> rest);
146 submask <<= quot;
147 mask += submask;
148 tmp |= mask;
149 if (size < BITS_PER_LONG)
150 goto found_first;
151 if (~tmp)
152 goto found_middle;
153
154 size -= BITS_PER_LONG;
155 result += BITS_PER_LONG;
156aligned:
157 while (size & ~(BITS_PER_LONG - 1)) {
158 tmp = *(p++);
159 if (~tmp)
160 goto found_middle;
161 result += BITS_PER_LONG;
162 size -= BITS_PER_LONG;
163 }
164 if (!size)
165 return result;
166 tmp = *p;
167
168found_first:
169 tmp |= ~0UL << size;
170 if (tmp == ~0UL) /* Are any bits zero? */
171 return result + size; /* Nope. */
172found_middle:
173 return result + __reverse_ffz(tmp);
174}
175
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900176/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900177 * This function balances dirty node and dentry pages.
178 * In addition, it controls garbage collection.
179 */
180void f2fs_balance_fs(struct f2fs_sb_info *sbi)
181{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900182 /*
Jaegeuk Kim029cd282012-12-21 17:20:21 +0900183 * We should do GC or end up with checkpoint, if there are so many dirty
184 * dir/node pages without enough free segments.
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900185 */
Jaegeuk Kim43727522013-02-04 15:11:17 +0900186 if (has_not_enough_free_secs(sbi, 0)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900187 mutex_lock(&sbi->gc_mutex);
Jaegeuk Kim408e9372013-01-03 17:55:52 +0900188 f2fs_gc(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900189 }
190}
191
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900192void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi)
193{
194 /* check the # of cached NAT entries and prefree segments */
195 if (try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK) ||
196 excess_prefree_segs(sbi))
197 f2fs_sync_fs(sbi->sb, true);
198}
199
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900200static int issue_flush_thread(void *data)
201{
202 struct f2fs_sb_info *sbi = data;
203 struct f2fs_sm_info *sm_i = SM_I(sbi);
204 wait_queue_head_t *q = &sm_i->flush_wait_queue;
205repeat:
206 if (kthread_should_stop())
207 return 0;
208
209 spin_lock(&sm_i->issue_lock);
210 if (sm_i->issue_list) {
211 sm_i->dispatch_list = sm_i->issue_list;
212 sm_i->issue_list = sm_i->issue_tail = NULL;
213 }
214 spin_unlock(&sm_i->issue_lock);
215
216 if (sm_i->dispatch_list) {
217 struct bio *bio = bio_alloc(GFP_NOIO, 0);
218 struct flush_cmd *cmd, *next;
219 int ret;
220
221 bio->bi_bdev = sbi->sb->s_bdev;
222 ret = submit_bio_wait(WRITE_FLUSH, bio);
223
224 for (cmd = sm_i->dispatch_list; cmd; cmd = next) {
225 cmd->ret = ret;
226 next = cmd->next;
227 complete(&cmd->wait);
228 }
Gu Zhenga4ed23f2014-04-11 17:49:35 +0800229 bio_put(bio);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900230 sm_i->dispatch_list = NULL;
231 }
232
233 wait_event_interruptible(*q, kthread_should_stop() || sm_i->issue_list);
234 goto repeat;
235}
236
237int f2fs_issue_flush(struct f2fs_sb_info *sbi)
238{
239 struct f2fs_sm_info *sm_i = SM_I(sbi);
240 struct flush_cmd *cmd;
241 int ret;
242
243 if (!test_opt(sbi, FLUSH_MERGE))
244 return blkdev_issue_flush(sbi->sb->s_bdev, GFP_KERNEL, NULL);
245
246 cmd = f2fs_kmem_cache_alloc(flush_cmd_slab, GFP_ATOMIC);
247 cmd->next = NULL;
248 cmd->ret = 0;
249 init_completion(&cmd->wait);
250
251 spin_lock(&sm_i->issue_lock);
252 if (sm_i->issue_list)
253 sm_i->issue_tail->next = cmd;
254 else
255 sm_i->issue_list = cmd;
256 sm_i->issue_tail = cmd;
257 spin_unlock(&sm_i->issue_lock);
258
259 if (!sm_i->dispatch_list)
260 wake_up(&sm_i->flush_wait_queue);
261
262 wait_for_completion(&cmd->wait);
263 ret = cmd->ret;
264 kmem_cache_free(flush_cmd_slab, cmd);
265 return ret;
266}
267
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900268static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
269 enum dirty_type dirty_type)
270{
271 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
272
273 /* need not be added */
274 if (IS_CURSEG(sbi, segno))
275 return;
276
277 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
278 dirty_i->nr_dirty[dirty_type]++;
279
280 if (dirty_type == DIRTY) {
281 struct seg_entry *sentry = get_seg_entry(sbi, segno);
Changman Lee4625d6a2013-10-25 17:31:57 +0900282 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900283
Changman Lee4625d6a2013-10-25 17:31:57 +0900284 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
285 dirty_i->nr_dirty[t]++;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900286 }
287}
288
289static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
290 enum dirty_type dirty_type)
291{
292 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
293
294 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
295 dirty_i->nr_dirty[dirty_type]--;
296
297 if (dirty_type == DIRTY) {
Changman Lee4625d6a2013-10-25 17:31:57 +0900298 struct seg_entry *sentry = get_seg_entry(sbi, segno);
299 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900300
Changman Lee4625d6a2013-10-25 17:31:57 +0900301 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
302 dirty_i->nr_dirty[t]--;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900303
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +0900304 if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0)
305 clear_bit(GET_SECNO(sbi, segno),
306 dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900307 }
308}
309
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900310/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900311 * Should not occur error such as -ENOMEM.
312 * Adding dirty entry into seglist is not critical operation.
313 * If a given segment is one of current working segments, it won't be added.
314 */
Haicheng Li8d8451a2013-06-13 16:59:28 +0800315static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900316{
317 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
318 unsigned short valid_blocks;
319
320 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
321 return;
322
323 mutex_lock(&dirty_i->seglist_lock);
324
325 valid_blocks = get_valid_blocks(sbi, segno, 0);
326
327 if (valid_blocks == 0) {
328 __locate_dirty_segment(sbi, segno, PRE);
329 __remove_dirty_segment(sbi, segno, DIRTY);
330 } else if (valid_blocks < sbi->blocks_per_seg) {
331 __locate_dirty_segment(sbi, segno, DIRTY);
332 } else {
333 /* Recovery routine with SSR needs this */
334 __remove_dirty_segment(sbi, segno, DIRTY);
335 }
336
337 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900338}
339
Jaegeuk Kim37208872013-11-12 16:55:17 +0900340static void f2fs_issue_discard(struct f2fs_sb_info *sbi,
341 block_t blkstart, block_t blklen)
342{
Jaegeuk Kimf9a4e6d2013-11-28 12:44:05 +0900343 sector_t start = SECTOR_FROM_BLOCK(sbi, blkstart);
344 sector_t len = SECTOR_FROM_BLOCK(sbi, blklen);
Jaegeuk Kim37208872013-11-12 16:55:17 +0900345 blkdev_issue_discard(sbi->sb->s_bdev, start, len, GFP_NOFS, 0);
Jaegeuk Kim1661d072013-11-12 17:01:00 +0900346 trace_f2fs_issue_discard(sbi->sb, blkstart, blklen);
Jaegeuk Kim37208872013-11-12 16:55:17 +0900347}
348
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900349static void add_discard_addrs(struct f2fs_sb_info *sbi,
350 unsigned int segno, struct seg_entry *se)
351{
352 struct list_head *head = &SM_I(sbi)->discard_list;
353 struct discard_entry *new;
354 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
355 int max_blocks = sbi->blocks_per_seg;
356 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
357 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
358 unsigned long dmap[entries];
359 unsigned int start = 0, end = -1;
360 int i;
361
362 if (!test_opt(sbi, DISCARD))
363 return;
364
365 /* zero block will be discarded through the prefree list */
366 if (!se->valid_blocks || se->valid_blocks == max_blocks)
367 return;
368
369 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
370 for (i = 0; i < entries; i++)
371 dmap[i] = (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
372
373 while (SM_I(sbi)->nr_discards <= SM_I(sbi)->max_discards) {
374 start = __find_rev_next_bit(dmap, max_blocks, end + 1);
375 if (start >= max_blocks)
376 break;
377
378 end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
379
380 new = f2fs_kmem_cache_alloc(discard_entry_slab, GFP_NOFS);
381 INIT_LIST_HEAD(&new->list);
382 new->blkaddr = START_BLOCK(sbi, segno) + start;
383 new->len = end - start;
384
385 list_add_tail(&new->list, head);
386 SM_I(sbi)->nr_discards += end - start;
387 }
388}
389
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900390/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900391 * Should call clear_prefree_segments after checkpoint is done.
392 */
393static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
394{
395 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Haicheng Li81fb5e82013-05-14 18:20:28 +0800396 unsigned int segno = -1;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900397 unsigned int total_segs = TOTAL_SEGS(sbi);
398
399 mutex_lock(&dirty_i->seglist_lock);
400 while (1) {
401 segno = find_next_bit(dirty_i->dirty_segmap[PRE], total_segs,
Haicheng Li81fb5e82013-05-14 18:20:28 +0800402 segno + 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900403 if (segno >= total_segs)
404 break;
405 __set_test_and_free(sbi, segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900406 }
407 mutex_unlock(&dirty_i->seglist_lock);
408}
409
410void clear_prefree_segments(struct f2fs_sb_info *sbi)
411{
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900412 struct list_head *head = &(SM_I(sbi)->discard_list);
Chao Yu2d7b8222014-03-29 11:33:17 +0800413 struct discard_entry *entry, *this;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900414 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Changman Lee29e59c12013-11-11 09:24:37 +0900415 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900416 unsigned int total_segs = TOTAL_SEGS(sbi);
Changman Lee29e59c12013-11-11 09:24:37 +0900417 unsigned int start = 0, end = -1;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900418
419 mutex_lock(&dirty_i->seglist_lock);
Changman Lee29e59c12013-11-11 09:24:37 +0900420
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900421 while (1) {
Changman Lee29e59c12013-11-11 09:24:37 +0900422 int i;
423 start = find_next_bit(prefree_map, total_segs, end + 1);
424 if (start >= total_segs)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900425 break;
Changman Lee29e59c12013-11-11 09:24:37 +0900426 end = find_next_zero_bit(prefree_map, total_segs, start + 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900427
Changman Lee29e59c12013-11-11 09:24:37 +0900428 for (i = start; i < end; i++)
429 clear_bit(i, prefree_map);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900430
Changman Lee29e59c12013-11-11 09:24:37 +0900431 dirty_i->nr_dirty[PRE] -= end - start;
432
433 if (!test_opt(sbi, DISCARD))
434 continue;
435
Jaegeuk Kim37208872013-11-12 16:55:17 +0900436 f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
437 (end - start) << sbi->log_blocks_per_seg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900438 }
439 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900440
441 /* send small discards */
Chao Yu2d7b8222014-03-29 11:33:17 +0800442 list_for_each_entry_safe(entry, this, head, list) {
Jaegeuk Kim37208872013-11-12 16:55:17 +0900443 f2fs_issue_discard(sbi, entry->blkaddr, entry->len);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900444 list_del(&entry->list);
445 SM_I(sbi)->nr_discards -= entry->len;
446 kmem_cache_free(discard_entry_slab, entry);
447 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900448}
449
450static void __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
451{
452 struct sit_info *sit_i = SIT_I(sbi);
453 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap))
454 sit_i->dirty_sentries++;
455}
456
457static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
458 unsigned int segno, int modified)
459{
460 struct seg_entry *se = get_seg_entry(sbi, segno);
461 se->type = type;
462 if (modified)
463 __mark_sit_entry_dirty(sbi, segno);
464}
465
466static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
467{
468 struct seg_entry *se;
469 unsigned int segno, offset;
470 long int new_vblocks;
471
472 segno = GET_SEGNO(sbi, blkaddr);
473
474 se = get_seg_entry(sbi, segno);
475 new_vblocks = se->valid_blocks + del;
Jaegeuk Kim491c0852014-02-04 13:01:10 +0900476 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900477
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900478 f2fs_bug_on((new_vblocks >> (sizeof(unsigned short) << 3) ||
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900479 (new_vblocks > sbi->blocks_per_seg)));
480
481 se->valid_blocks = new_vblocks;
482 se->mtime = get_mtime(sbi);
483 SIT_I(sbi)->max_mtime = se->mtime;
484
485 /* Update valid block bitmap */
486 if (del > 0) {
487 if (f2fs_set_bit(offset, se->cur_valid_map))
488 BUG();
489 } else {
490 if (!f2fs_clear_bit(offset, se->cur_valid_map))
491 BUG();
492 }
493 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
494 se->ckpt_valid_blocks += del;
495
496 __mark_sit_entry_dirty(sbi, segno);
497
498 /* update total number of valid blocks to be written in ckpt area */
499 SIT_I(sbi)->written_valid_blocks += del;
500
501 if (sbi->segs_per_sec > 1)
502 get_sec_entry(sbi, segno)->valid_blocks += del;
503}
504
Jaegeuk Kim5e443812014-01-28 12:22:14 +0900505void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900506{
Jaegeuk Kim5e443812014-01-28 12:22:14 +0900507 update_sit_entry(sbi, new, 1);
508 if (GET_SEGNO(sbi, old) != NULL_SEGNO)
509 update_sit_entry(sbi, old, -1);
510
511 locate_dirty_segment(sbi, GET_SEGNO(sbi, old));
512 locate_dirty_segment(sbi, GET_SEGNO(sbi, new));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900513}
514
515void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
516{
517 unsigned int segno = GET_SEGNO(sbi, addr);
518 struct sit_info *sit_i = SIT_I(sbi);
519
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900520 f2fs_bug_on(addr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900521 if (addr == NEW_ADDR)
522 return;
523
524 /* add it into sit main buffer */
525 mutex_lock(&sit_i->sentry_lock);
526
527 update_sit_entry(sbi, addr, -1);
528
529 /* add it into dirty seglist */
530 locate_dirty_segment(sbi, segno);
531
532 mutex_unlock(&sit_i->sentry_lock);
533}
534
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900535/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900536 * This function should be resided under the curseg_mutex lock
537 */
538static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
Haicheng Lie79efe32013-06-13 16:59:27 +0800539 struct f2fs_summary *sum)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900540{
541 struct curseg_info *curseg = CURSEG_I(sbi, type);
542 void *addr = curseg->sum_blk;
Haicheng Lie79efe32013-06-13 16:59:27 +0800543 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900544 memcpy(addr, sum, sizeof(struct f2fs_summary));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900545}
546
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900547/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900548 * Calculate the number of current summary pages for writing
549 */
550int npages_for_summary_flush(struct f2fs_sb_info *sbi)
551{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900552 int valid_sum_count = 0;
Fan Li9a479382013-10-29 16:21:47 +0800553 int i, sum_in_page;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900554
555 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
556 if (sbi->ckpt->alloc_type[i] == SSR)
557 valid_sum_count += sbi->blocks_per_seg;
558 else
559 valid_sum_count += curseg_blkoff(sbi, i);
560 }
561
Fan Li9a479382013-10-29 16:21:47 +0800562 sum_in_page = (PAGE_CACHE_SIZE - 2 * SUM_JOURNAL_SIZE -
563 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
564 if (valid_sum_count <= sum_in_page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900565 return 1;
Fan Li9a479382013-10-29 16:21:47 +0800566 else if ((valid_sum_count - sum_in_page) <=
567 (PAGE_CACHE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900568 return 2;
569 return 3;
570}
571
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900572/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900573 * Caller should put this summary page
574 */
575struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
576{
577 return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
578}
579
580static void write_sum_page(struct f2fs_sb_info *sbi,
581 struct f2fs_summary_block *sum_blk, block_t blk_addr)
582{
583 struct page *page = grab_meta_page(sbi, blk_addr);
584 void *kaddr = page_address(page);
585 memcpy(kaddr, sum_blk, PAGE_CACHE_SIZE);
586 set_page_dirty(page);
587 f2fs_put_page(page, 1);
588}
589
Jaegeuk Kim60374682013-03-31 13:58:51 +0900590static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
591{
592 struct curseg_info *curseg = CURSEG_I(sbi, type);
Haicheng Li81fb5e82013-05-14 18:20:28 +0800593 unsigned int segno = curseg->segno + 1;
Jaegeuk Kim60374682013-03-31 13:58:51 +0900594 struct free_segmap_info *free_i = FREE_I(sbi);
595
Haicheng Li81fb5e82013-05-14 18:20:28 +0800596 if (segno < TOTAL_SEGS(sbi) && segno % sbi->segs_per_sec)
597 return !test_bit(segno, free_i->free_segmap);
Jaegeuk Kim60374682013-03-31 13:58:51 +0900598 return 0;
599}
600
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900601/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900602 * Find a new segment from the free segments bitmap to right order
603 * This function should be returned with success, otherwise BUG
604 */
605static void get_new_segment(struct f2fs_sb_info *sbi,
606 unsigned int *newseg, bool new_sec, int dir)
607{
608 struct free_segmap_info *free_i = FREE_I(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900609 unsigned int segno, secno, zoneno;
Jaegeuk Kim53cf9522013-03-31 12:39:49 +0900610 unsigned int total_zones = TOTAL_SECS(sbi) / sbi->secs_per_zone;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900611 unsigned int hint = *newseg / sbi->segs_per_sec;
612 unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
613 unsigned int left_start = hint;
614 bool init = true;
615 int go_left = 0;
616 int i;
617
618 write_lock(&free_i->segmap_lock);
619
620 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
621 segno = find_next_zero_bit(free_i->free_segmap,
622 TOTAL_SEGS(sbi), *newseg + 1);
Jaegeuk Kim33afa7f2013-03-31 12:59:53 +0900623 if (segno - *newseg < sbi->segs_per_sec -
624 (*newseg % sbi->segs_per_sec))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900625 goto got_it;
626 }
627find_other_zone:
Jaegeuk Kim53cf9522013-03-31 12:39:49 +0900628 secno = find_next_zero_bit(free_i->free_secmap, TOTAL_SECS(sbi), hint);
629 if (secno >= TOTAL_SECS(sbi)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900630 if (dir == ALLOC_RIGHT) {
631 secno = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim53cf9522013-03-31 12:39:49 +0900632 TOTAL_SECS(sbi), 0);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900633 f2fs_bug_on(secno >= TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900634 } else {
635 go_left = 1;
636 left_start = hint - 1;
637 }
638 }
639 if (go_left == 0)
640 goto skip_left;
641
642 while (test_bit(left_start, free_i->free_secmap)) {
643 if (left_start > 0) {
644 left_start--;
645 continue;
646 }
647 left_start = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim53cf9522013-03-31 12:39:49 +0900648 TOTAL_SECS(sbi), 0);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900649 f2fs_bug_on(left_start >= TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900650 break;
651 }
652 secno = left_start;
653skip_left:
654 hint = secno;
655 segno = secno * sbi->segs_per_sec;
656 zoneno = secno / sbi->secs_per_zone;
657
658 /* give up on finding another zone */
659 if (!init)
660 goto got_it;
661 if (sbi->secs_per_zone == 1)
662 goto got_it;
663 if (zoneno == old_zoneno)
664 goto got_it;
665 if (dir == ALLOC_LEFT) {
666 if (!go_left && zoneno + 1 >= total_zones)
667 goto got_it;
668 if (go_left && zoneno == 0)
669 goto got_it;
670 }
671 for (i = 0; i < NR_CURSEG_TYPE; i++)
672 if (CURSEG_I(sbi, i)->zone == zoneno)
673 break;
674
675 if (i < NR_CURSEG_TYPE) {
676 /* zone is in user, try another */
677 if (go_left)
678 hint = zoneno * sbi->secs_per_zone - 1;
679 else if (zoneno + 1 >= total_zones)
680 hint = 0;
681 else
682 hint = (zoneno + 1) * sbi->secs_per_zone;
683 init = false;
684 goto find_other_zone;
685 }
686got_it:
687 /* set it as dirty segment in free segmap */
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900688 f2fs_bug_on(test_bit(segno, free_i->free_segmap));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900689 __set_inuse(sbi, segno);
690 *newseg = segno;
691 write_unlock(&free_i->segmap_lock);
692}
693
694static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
695{
696 struct curseg_info *curseg = CURSEG_I(sbi, type);
697 struct summary_footer *sum_footer;
698
699 curseg->segno = curseg->next_segno;
700 curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
701 curseg->next_blkoff = 0;
702 curseg->next_segno = NULL_SEGNO;
703
704 sum_footer = &(curseg->sum_blk->footer);
705 memset(sum_footer, 0, sizeof(struct summary_footer));
706 if (IS_DATASEG(type))
707 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
708 if (IS_NODESEG(type))
709 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
710 __set_sit_entry_type(sbi, type, curseg->segno, modified);
711}
712
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900713/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900714 * Allocate a current working segment.
715 * This function always allocates a free segment in LFS manner.
716 */
717static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
718{
719 struct curseg_info *curseg = CURSEG_I(sbi, type);
720 unsigned int segno = curseg->segno;
721 int dir = ALLOC_LEFT;
722
723 write_sum_page(sbi, curseg->sum_blk,
Haicheng Li81fb5e82013-05-14 18:20:28 +0800724 GET_SUM_BLOCK(sbi, segno));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900725 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
726 dir = ALLOC_RIGHT;
727
728 if (test_opt(sbi, NOHEAP))
729 dir = ALLOC_RIGHT;
730
731 get_new_segment(sbi, &segno, new_sec, dir);
732 curseg->next_segno = segno;
733 reset_curseg(sbi, type, 1);
734 curseg->alloc_type = LFS;
735}
736
737static void __next_free_blkoff(struct f2fs_sb_info *sbi,
738 struct curseg_info *seg, block_t start)
739{
740 struct seg_entry *se = get_seg_entry(sbi, seg->segno);
Changman Leee81c93c2013-11-15 13:21:16 +0900741 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
742 unsigned long target_map[entries];
743 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
744 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
745 int i, pos;
746
747 for (i = 0; i < entries; i++)
748 target_map[i] = ckpt_map[i] | cur_map[i];
749
750 pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
751
752 seg->next_blkoff = pos;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900753}
754
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900755/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900756 * If a segment is written by LFS manner, next block offset is just obtained
757 * by increasing the current block offset. However, if a segment is written by
758 * SSR manner, next block offset obtained by calling __next_free_blkoff
759 */
760static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
761 struct curseg_info *seg)
762{
763 if (seg->alloc_type == SSR)
764 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
765 else
766 seg->next_blkoff++;
767}
768
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900769/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900770 * This function always allocates a used segment (from dirty seglist) by SSR
771 * manner, so it should recover the existing segment information of valid blocks
772 */
773static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
774{
775 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
776 struct curseg_info *curseg = CURSEG_I(sbi, type);
777 unsigned int new_segno = curseg->next_segno;
778 struct f2fs_summary_block *sum_node;
779 struct page *sum_page;
780
781 write_sum_page(sbi, curseg->sum_blk,
782 GET_SUM_BLOCK(sbi, curseg->segno));
783 __set_test_and_inuse(sbi, new_segno);
784
785 mutex_lock(&dirty_i->seglist_lock);
786 __remove_dirty_segment(sbi, new_segno, PRE);
787 __remove_dirty_segment(sbi, new_segno, DIRTY);
788 mutex_unlock(&dirty_i->seglist_lock);
789
790 reset_curseg(sbi, type, 1);
791 curseg->alloc_type = SSR;
792 __next_free_blkoff(sbi, curseg, 0);
793
794 if (reuse) {
795 sum_page = get_sum_page(sbi, new_segno);
796 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
797 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
798 f2fs_put_page(sum_page, 1);
799 }
800}
801
Jaegeuk Kim43727522013-02-04 15:11:17 +0900802static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
803{
804 struct curseg_info *curseg = CURSEG_I(sbi, type);
805 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
806
807 if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0))
808 return v_ops->get_victim(sbi,
809 &(curseg)->next_segno, BG_GC, type, SSR);
810
811 /* For data segments, let's do SSR more intensively */
812 for (; type >= CURSEG_HOT_DATA; type--)
813 if (v_ops->get_victim(sbi, &(curseg)->next_segno,
814 BG_GC, type, SSR))
815 return 1;
816 return 0;
817}
818
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900819/*
820 * flush out current segment and replace it with new segment
821 * This function should be returned with success, otherwise BUG
822 */
823static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
824 int type, bool force)
825{
826 struct curseg_info *curseg = CURSEG_I(sbi, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900827
Gu Zheng7b405272013-08-19 09:41:15 +0800828 if (force)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900829 new_curseg(sbi, type, true);
Gu Zheng7b405272013-08-19 09:41:15 +0800830 else if (type == CURSEG_WARM_NODE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900831 new_curseg(sbi, type, false);
Jaegeuk Kim60374682013-03-31 13:58:51 +0900832 else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
833 new_curseg(sbi, type, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900834 else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
835 change_curseg(sbi, type, true);
836 else
837 new_curseg(sbi, type, false);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +0900838
839 stat_inc_seg_type(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900840}
841
842void allocate_new_segments(struct f2fs_sb_info *sbi)
843{
844 struct curseg_info *curseg;
845 unsigned int old_curseg;
846 int i;
847
848 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
849 curseg = CURSEG_I(sbi, i);
850 old_curseg = curseg->segno;
851 SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
852 locate_dirty_segment(sbi, old_curseg);
853 }
854}
855
856static const struct segment_allocation default_salloc_ops = {
857 .allocate_segment = allocate_segment_by_default,
858};
859
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900860static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
861{
862 struct curseg_info *curseg = CURSEG_I(sbi, type);
863 if (curseg->next_blkoff < sbi->blocks_per_seg)
864 return true;
865 return false;
866}
867
868static int __get_segment_type_2(struct page *page, enum page_type p_type)
869{
870 if (p_type == DATA)
871 return CURSEG_HOT_DATA;
872 else
873 return CURSEG_HOT_NODE;
874}
875
876static int __get_segment_type_4(struct page *page, enum page_type p_type)
877{
878 if (p_type == DATA) {
879 struct inode *inode = page->mapping->host;
880
881 if (S_ISDIR(inode->i_mode))
882 return CURSEG_HOT_DATA;
883 else
884 return CURSEG_COLD_DATA;
885 } else {
886 if (IS_DNODE(page) && !is_cold_node(page))
887 return CURSEG_HOT_NODE;
888 else
889 return CURSEG_COLD_NODE;
890 }
891}
892
893static int __get_segment_type_6(struct page *page, enum page_type p_type)
894{
895 if (p_type == DATA) {
896 struct inode *inode = page->mapping->host;
897
898 if (S_ISDIR(inode->i_mode))
899 return CURSEG_HOT_DATA;
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900900 else if (is_cold_data(page) || file_is_cold(inode))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900901 return CURSEG_COLD_DATA;
902 else
903 return CURSEG_WARM_DATA;
904 } else {
905 if (IS_DNODE(page))
906 return is_cold_node(page) ? CURSEG_WARM_NODE :
907 CURSEG_HOT_NODE;
908 else
909 return CURSEG_COLD_NODE;
910 }
911}
912
913static int __get_segment_type(struct page *page, enum page_type p_type)
914{
915 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
916 switch (sbi->active_logs) {
917 case 2:
918 return __get_segment_type_2(page, p_type);
919 case 4:
920 return __get_segment_type_4(page, p_type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900921 }
Jaegeuk Kim12a67142012-12-21 11:47:05 +0900922 /* NR_CURSEG_TYPE(6) logs by default */
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900923 f2fs_bug_on(sbi->active_logs != NR_CURSEG_TYPE);
Jaegeuk Kim12a67142012-12-21 11:47:05 +0900924 return __get_segment_type_6(page, p_type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900925}
926
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900927void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
928 block_t old_blkaddr, block_t *new_blkaddr,
929 struct f2fs_summary *sum, int type)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900930{
931 struct sit_info *sit_i = SIT_I(sbi);
932 struct curseg_info *curseg;
933 unsigned int old_cursegno;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900934
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900935 curseg = CURSEG_I(sbi, type);
936
937 mutex_lock(&curseg->curseg_mutex);
938
939 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
940 old_cursegno = curseg->segno;
941
942 /*
943 * __add_sum_entry should be resided under the curseg_mutex
944 * because, this function updates a summary entry in the
945 * current summary block.
946 */
Haicheng Lie79efe32013-06-13 16:59:27 +0800947 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900948
949 mutex_lock(&sit_i->sentry_lock);
950 __refresh_next_blkoff(sbi, curseg);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +0900951
952 stat_inc_block_count(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900953
Jaegeuk Kim5e443812014-01-28 12:22:14 +0900954 if (!__has_curseg_space(sbi, type))
955 sit_i->s_ops->allocate_segment(sbi, type, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900956 /*
957 * SIT information should be updated before segment allocation,
958 * since SSR needs latest valid block information.
959 */
960 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900961 locate_dirty_segment(sbi, old_cursegno);
Jaegeuk Kim5e443812014-01-28 12:22:14 +0900962
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900963 mutex_unlock(&sit_i->sentry_lock);
964
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900965 if (page && IS_NODESEG(type))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900966 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
967
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900968 mutex_unlock(&curseg->curseg_mutex);
969}
970
971static void do_write_page(struct f2fs_sb_info *sbi, struct page *page,
972 block_t old_blkaddr, block_t *new_blkaddr,
973 struct f2fs_summary *sum, struct f2fs_io_info *fio)
974{
975 int type = __get_segment_type(page, fio->type);
976
977 allocate_data_block(sbi, page, old_blkaddr, new_blkaddr, sum, type);
978
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900979 /* writeout dirty page into bdev */
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900980 f2fs_submit_page_mbio(sbi, page, *new_blkaddr, fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900981}
982
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900983void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900984{
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900985 struct f2fs_io_info fio = {
986 .type = META,
Gu Zheng7e8f2302013-12-20 18:17:49 +0800987 .rw = WRITE_SYNC | REQ_META | REQ_PRIO
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900988 };
989
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900990 set_page_writeback(page);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900991 f2fs_submit_page_mbio(sbi, page, page->index, &fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900992}
993
994void write_node_page(struct f2fs_sb_info *sbi, struct page *page,
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +0900995 struct f2fs_io_info *fio,
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900996 unsigned int nid, block_t old_blkaddr, block_t *new_blkaddr)
997{
998 struct f2fs_summary sum;
999 set_summary(&sum, nid, 0, 0);
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001000 do_write_page(sbi, page, old_blkaddr, new_blkaddr, &sum, fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001001}
1002
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001003void write_data_page(struct page *page, struct dnode_of_data *dn,
1004 block_t *new_blkaddr, struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001005{
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001006 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001007 struct f2fs_summary sum;
1008 struct node_info ni;
1009
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001010 f2fs_bug_on(dn->data_blkaddr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001011 get_node_info(sbi, dn->nid, &ni);
1012 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1013
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001014 do_write_page(sbi, page, dn->data_blkaddr, new_blkaddr, &sum, fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001015}
1016
Chris Fries6c311ec2014-01-17 14:44:39 -06001017void rewrite_data_page(struct page *page, block_t old_blkaddr,
1018 struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001019{
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001020 struct inode *inode = page->mapping->host;
1021 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1022 f2fs_submit_page_mbio(sbi, page, old_blkaddr, fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001023}
1024
1025void recover_data_page(struct f2fs_sb_info *sbi,
1026 struct page *page, struct f2fs_summary *sum,
1027 block_t old_blkaddr, block_t new_blkaddr)
1028{
1029 struct sit_info *sit_i = SIT_I(sbi);
1030 struct curseg_info *curseg;
1031 unsigned int segno, old_cursegno;
1032 struct seg_entry *se;
1033 int type;
1034
1035 segno = GET_SEGNO(sbi, new_blkaddr);
1036 se = get_seg_entry(sbi, segno);
1037 type = se->type;
1038
1039 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
1040 if (old_blkaddr == NULL_ADDR)
1041 type = CURSEG_COLD_DATA;
1042 else
1043 type = CURSEG_WARM_DATA;
1044 }
1045 curseg = CURSEG_I(sbi, type);
1046
1047 mutex_lock(&curseg->curseg_mutex);
1048 mutex_lock(&sit_i->sentry_lock);
1049
1050 old_cursegno = curseg->segno;
1051
1052 /* change the current segment */
1053 if (segno != curseg->segno) {
1054 curseg->next_segno = segno;
1055 change_curseg(sbi, type, true);
1056 }
1057
Jaegeuk Kim491c0852014-02-04 13:01:10 +09001058 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
Haicheng Lie79efe32013-06-13 16:59:27 +08001059 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001060
1061 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001062 locate_dirty_segment(sbi, old_cursegno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001063
1064 mutex_unlock(&sit_i->sentry_lock);
1065 mutex_unlock(&curseg->curseg_mutex);
1066}
1067
1068void rewrite_node_page(struct f2fs_sb_info *sbi,
1069 struct page *page, struct f2fs_summary *sum,
1070 block_t old_blkaddr, block_t new_blkaddr)
1071{
1072 struct sit_info *sit_i = SIT_I(sbi);
1073 int type = CURSEG_WARM_NODE;
1074 struct curseg_info *curseg;
1075 unsigned int segno, old_cursegno;
1076 block_t next_blkaddr = next_blkaddr_of_node(page);
1077 unsigned int next_segno = GET_SEGNO(sbi, next_blkaddr);
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001078 struct f2fs_io_info fio = {
1079 .type = NODE,
1080 .rw = WRITE_SYNC,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001081 };
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001082
1083 curseg = CURSEG_I(sbi, type);
1084
1085 mutex_lock(&curseg->curseg_mutex);
1086 mutex_lock(&sit_i->sentry_lock);
1087
1088 segno = GET_SEGNO(sbi, new_blkaddr);
1089 old_cursegno = curseg->segno;
1090
1091 /* change the current segment */
1092 if (segno != curseg->segno) {
1093 curseg->next_segno = segno;
1094 change_curseg(sbi, type, true);
1095 }
Jaegeuk Kim491c0852014-02-04 13:01:10 +09001096 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
Haicheng Lie79efe32013-06-13 16:59:27 +08001097 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001098
1099 /* change the current log to the next block addr in advance */
1100 if (next_segno != segno) {
1101 curseg->next_segno = next_segno;
1102 change_curseg(sbi, type, true);
1103 }
Jaegeuk Kim491c0852014-02-04 13:01:10 +09001104 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, next_blkaddr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001105
1106 /* rewrite node page */
1107 set_page_writeback(page);
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001108 f2fs_submit_page_mbio(sbi, page, new_blkaddr, &fio);
1109 f2fs_submit_merged_bio(sbi, NODE, WRITE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001110 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001111 locate_dirty_segment(sbi, old_cursegno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001112
1113 mutex_unlock(&sit_i->sentry_lock);
1114 mutex_unlock(&curseg->curseg_mutex);
1115}
1116
Chao Yudf0f8dc2014-03-22 14:57:23 +08001117static inline bool is_merged_page(struct f2fs_sb_info *sbi,
1118 struct page *page, enum page_type type)
1119{
1120 enum page_type btype = PAGE_TYPE_OF_BIO(type);
1121 struct f2fs_bio_info *io = &sbi->write_io[btype];
Chao Yudf0f8dc2014-03-22 14:57:23 +08001122 struct bio_vec *bvec;
1123 int i;
1124
1125 down_read(&io->io_rwsem);
Jaegeuk Kimce234472014-04-02 09:04:42 +09001126 if (!io->bio)
Chao Yudf0f8dc2014-03-22 14:57:23 +08001127 goto out;
1128
Jaegeuk Kimce234472014-04-02 09:04:42 +09001129 bio_for_each_segment_all(bvec, io->bio, i) {
Chao Yudf0f8dc2014-03-22 14:57:23 +08001130 if (page == bvec->bv_page) {
1131 up_read(&io->io_rwsem);
1132 return true;
1133 }
1134 }
1135
1136out:
1137 up_read(&io->io_rwsem);
1138 return false;
1139}
1140
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001141void f2fs_wait_on_page_writeback(struct page *page,
Yuan Zhong5514f0a2014-01-10 07:26:14 +00001142 enum page_type type)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001143{
1144 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
1145 if (PageWriteback(page)) {
Chao Yudf0f8dc2014-03-22 14:57:23 +08001146 if (is_merged_page(sbi, page, type))
1147 f2fs_submit_merged_bio(sbi, type, WRITE);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001148 wait_on_page_writeback(page);
1149 }
1150}
1151
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001152static int read_compacted_summaries(struct f2fs_sb_info *sbi)
1153{
1154 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1155 struct curseg_info *seg_i;
1156 unsigned char *kaddr;
1157 struct page *page;
1158 block_t start;
1159 int i, j, offset;
1160
1161 start = start_sum_block(sbi);
1162
1163 page = get_meta_page(sbi, start++);
1164 kaddr = (unsigned char *)page_address(page);
1165
1166 /* Step 1: restore nat cache */
1167 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1168 memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
1169
1170 /* Step 2: restore sit cache */
1171 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1172 memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
1173 SUM_JOURNAL_SIZE);
1174 offset = 2 * SUM_JOURNAL_SIZE;
1175
1176 /* Step 3: restore summary entries */
1177 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1178 unsigned short blk_off;
1179 unsigned int segno;
1180
1181 seg_i = CURSEG_I(sbi, i);
1182 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
1183 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
1184 seg_i->next_segno = segno;
1185 reset_curseg(sbi, i, 0);
1186 seg_i->alloc_type = ckpt->alloc_type[i];
1187 seg_i->next_blkoff = blk_off;
1188
1189 if (seg_i->alloc_type == SSR)
1190 blk_off = sbi->blocks_per_seg;
1191
1192 for (j = 0; j < blk_off; j++) {
1193 struct f2fs_summary *s;
1194 s = (struct f2fs_summary *)(kaddr + offset);
1195 seg_i->sum_blk->entries[j] = *s;
1196 offset += SUMMARY_SIZE;
1197 if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1198 SUM_FOOTER_SIZE)
1199 continue;
1200
1201 f2fs_put_page(page, 1);
1202 page = NULL;
1203
1204 page = get_meta_page(sbi, start++);
1205 kaddr = (unsigned char *)page_address(page);
1206 offset = 0;
1207 }
1208 }
1209 f2fs_put_page(page, 1);
1210 return 0;
1211}
1212
1213static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
1214{
1215 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1216 struct f2fs_summary_block *sum;
1217 struct curseg_info *curseg;
1218 struct page *new;
1219 unsigned short blk_off;
1220 unsigned int segno = 0;
1221 block_t blk_addr = 0;
1222
1223 /* get segment number and block addr */
1224 if (IS_DATASEG(type)) {
1225 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
1226 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
1227 CURSEG_HOT_DATA]);
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001228 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001229 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
1230 else
1231 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
1232 } else {
1233 segno = le32_to_cpu(ckpt->cur_node_segno[type -
1234 CURSEG_HOT_NODE]);
1235 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
1236 CURSEG_HOT_NODE]);
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001237 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001238 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
1239 type - CURSEG_HOT_NODE);
1240 else
1241 blk_addr = GET_SUM_BLOCK(sbi, segno);
1242 }
1243
1244 new = get_meta_page(sbi, blk_addr);
1245 sum = (struct f2fs_summary_block *)page_address(new);
1246
1247 if (IS_NODESEG(type)) {
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001248 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001249 struct f2fs_summary *ns = &sum->entries[0];
1250 int i;
1251 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
1252 ns->version = 0;
1253 ns->ofs_in_node = 0;
1254 }
1255 } else {
Gu Zhengd6537882014-03-07 18:43:36 +08001256 int err;
1257
1258 err = restore_node_summary(sbi, segno, sum);
1259 if (err) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001260 f2fs_put_page(new, 1);
Gu Zhengd6537882014-03-07 18:43:36 +08001261 return err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001262 }
1263 }
1264 }
1265
1266 /* set uncompleted segment to curseg */
1267 curseg = CURSEG_I(sbi, type);
1268 mutex_lock(&curseg->curseg_mutex);
1269 memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
1270 curseg->next_segno = segno;
1271 reset_curseg(sbi, type, 0);
1272 curseg->alloc_type = ckpt->alloc_type[type];
1273 curseg->next_blkoff = blk_off;
1274 mutex_unlock(&curseg->curseg_mutex);
1275 f2fs_put_page(new, 1);
1276 return 0;
1277}
1278
1279static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
1280{
1281 int type = CURSEG_HOT_DATA;
Chao Yue4fc5fb2014-03-17 16:36:24 +08001282 int err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001283
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001284 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001285 /* restore for compacted data summary */
1286 if (read_compacted_summaries(sbi))
1287 return -EINVAL;
1288 type = CURSEG_HOT_NODE;
1289 }
1290
Chao Yue4fc5fb2014-03-17 16:36:24 +08001291 for (; type <= CURSEG_COLD_NODE; type++) {
1292 err = read_normal_summaries(sbi, type);
1293 if (err)
1294 return err;
1295 }
1296
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001297 return 0;
1298}
1299
1300static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
1301{
1302 struct page *page;
1303 unsigned char *kaddr;
1304 struct f2fs_summary *summary;
1305 struct curseg_info *seg_i;
1306 int written_size = 0;
1307 int i, j;
1308
1309 page = grab_meta_page(sbi, blkaddr++);
1310 kaddr = (unsigned char *)page_address(page);
1311
1312 /* Step 1: write nat cache */
1313 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1314 memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
1315 written_size += SUM_JOURNAL_SIZE;
1316
1317 /* Step 2: write sit cache */
1318 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1319 memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
1320 SUM_JOURNAL_SIZE);
1321 written_size += SUM_JOURNAL_SIZE;
1322
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001323 /* Step 3: write summary entries */
1324 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1325 unsigned short blkoff;
1326 seg_i = CURSEG_I(sbi, i);
1327 if (sbi->ckpt->alloc_type[i] == SSR)
1328 blkoff = sbi->blocks_per_seg;
1329 else
1330 blkoff = curseg_blkoff(sbi, i);
1331
1332 for (j = 0; j < blkoff; j++) {
1333 if (!page) {
1334 page = grab_meta_page(sbi, blkaddr++);
1335 kaddr = (unsigned char *)page_address(page);
1336 written_size = 0;
1337 }
1338 summary = (struct f2fs_summary *)(kaddr + written_size);
1339 *summary = seg_i->sum_blk->entries[j];
1340 written_size += SUMMARY_SIZE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001341
1342 if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1343 SUM_FOOTER_SIZE)
1344 continue;
1345
Chao Yue8d61a72013-10-24 15:08:28 +08001346 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001347 f2fs_put_page(page, 1);
1348 page = NULL;
1349 }
1350 }
Chao Yue8d61a72013-10-24 15:08:28 +08001351 if (page) {
1352 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001353 f2fs_put_page(page, 1);
Chao Yue8d61a72013-10-24 15:08:28 +08001354 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001355}
1356
1357static void write_normal_summaries(struct f2fs_sb_info *sbi,
1358 block_t blkaddr, int type)
1359{
1360 int i, end;
1361 if (IS_DATASEG(type))
1362 end = type + NR_CURSEG_DATA_TYPE;
1363 else
1364 end = type + NR_CURSEG_NODE_TYPE;
1365
1366 for (i = type; i < end; i++) {
1367 struct curseg_info *sum = CURSEG_I(sbi, i);
1368 mutex_lock(&sum->curseg_mutex);
1369 write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
1370 mutex_unlock(&sum->curseg_mutex);
1371 }
1372}
1373
1374void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1375{
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001376 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001377 write_compacted_summaries(sbi, start_blk);
1378 else
1379 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
1380}
1381
1382void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1383{
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001384 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001385 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001386}
1387
1388int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
1389 unsigned int val, int alloc)
1390{
1391 int i;
1392
1393 if (type == NAT_JOURNAL) {
1394 for (i = 0; i < nats_in_cursum(sum); i++) {
1395 if (le32_to_cpu(nid_in_journal(sum, i)) == val)
1396 return i;
1397 }
1398 if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
1399 return update_nats_in_cursum(sum, 1);
1400 } else if (type == SIT_JOURNAL) {
1401 for (i = 0; i < sits_in_cursum(sum); i++)
1402 if (le32_to_cpu(segno_in_journal(sum, i)) == val)
1403 return i;
1404 if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
1405 return update_sits_in_cursum(sum, 1);
1406 }
1407 return -1;
1408}
1409
1410static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
1411 unsigned int segno)
1412{
1413 struct sit_info *sit_i = SIT_I(sbi);
1414 unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno);
1415 block_t blk_addr = sit_i->sit_base_addr + offset;
1416
1417 check_seg_range(sbi, segno);
1418
1419 /* calculate sit block address */
1420 if (f2fs_test_bit(offset, sit_i->sit_bitmap))
1421 blk_addr += sit_i->sit_blocks;
1422
1423 return get_meta_page(sbi, blk_addr);
1424}
1425
1426static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
1427 unsigned int start)
1428{
1429 struct sit_info *sit_i = SIT_I(sbi);
1430 struct page *src_page, *dst_page;
1431 pgoff_t src_off, dst_off;
1432 void *src_addr, *dst_addr;
1433
1434 src_off = current_sit_addr(sbi, start);
1435 dst_off = next_sit_addr(sbi, src_off);
1436
1437 /* get current sit block page without lock */
1438 src_page = get_meta_page(sbi, src_off);
1439 dst_page = grab_meta_page(sbi, dst_off);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001440 f2fs_bug_on(PageDirty(src_page));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001441
1442 src_addr = page_address(src_page);
1443 dst_addr = page_address(dst_page);
1444 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
1445
1446 set_page_dirty(dst_page);
1447 f2fs_put_page(src_page, 1);
1448
1449 set_to_next_sit(sit_i, start);
1450
1451 return dst_page;
1452}
1453
1454static bool flush_sits_in_journal(struct f2fs_sb_info *sbi)
1455{
1456 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1457 struct f2fs_summary_block *sum = curseg->sum_blk;
1458 int i;
1459
1460 /*
1461 * If the journal area in the current summary is full of sit entries,
1462 * all the sit entries will be flushed. Otherwise the sit entries
1463 * are not able to replace with newly hot sit entries.
1464 */
1465 if (sits_in_cursum(sum) >= SIT_JOURNAL_ENTRIES) {
1466 for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
1467 unsigned int segno;
1468 segno = le32_to_cpu(segno_in_journal(sum, i));
1469 __mark_sit_entry_dirty(sbi, segno);
1470 }
1471 update_sits_in_cursum(sum, -sits_in_cursum(sum));
Haicheng Licffbfa62013-10-18 17:24:07 +08001472 return true;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001473 }
Haicheng Licffbfa62013-10-18 17:24:07 +08001474 return false;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001475}
1476
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001477/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001478 * CP calls this function, which flushes SIT entries including sit_journal,
1479 * and moves prefree segs to free segs.
1480 */
1481void flush_sit_entries(struct f2fs_sb_info *sbi)
1482{
1483 struct sit_info *sit_i = SIT_I(sbi);
1484 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
1485 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1486 struct f2fs_summary_block *sum = curseg->sum_blk;
1487 unsigned long nsegs = TOTAL_SEGS(sbi);
1488 struct page *page = NULL;
1489 struct f2fs_sit_block *raw_sit = NULL;
1490 unsigned int start = 0, end = 0;
1491 unsigned int segno = -1;
1492 bool flushed;
1493
1494 mutex_lock(&curseg->curseg_mutex);
1495 mutex_lock(&sit_i->sentry_lock);
1496
1497 /*
1498 * "flushed" indicates whether sit entries in journal are flushed
1499 * to the SIT area or not.
1500 */
1501 flushed = flush_sits_in_journal(sbi);
1502
1503 while ((segno = find_next_bit(bitmap, nsegs, segno + 1)) < nsegs) {
1504 struct seg_entry *se = get_seg_entry(sbi, segno);
1505 int sit_offset, offset;
1506
1507 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
1508
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001509 /* add discard candidates */
1510 if (SM_I(sbi)->nr_discards < SM_I(sbi)->max_discards)
1511 add_discard_addrs(sbi, segno, se);
1512
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001513 if (flushed)
1514 goto to_sit_page;
1515
1516 offset = lookup_journal_in_cursum(sum, SIT_JOURNAL, segno, 1);
1517 if (offset >= 0) {
1518 segno_in_journal(sum, offset) = cpu_to_le32(segno);
1519 seg_info_to_raw_sit(se, &sit_in_journal(sum, offset));
1520 goto flush_done;
1521 }
1522to_sit_page:
1523 if (!page || (start > segno) || (segno > end)) {
1524 if (page) {
1525 f2fs_put_page(page, 1);
1526 page = NULL;
1527 }
1528
1529 start = START_SEGNO(sit_i, segno);
1530 end = start + SIT_ENTRY_PER_BLOCK - 1;
1531
1532 /* read sit block that will be updated */
1533 page = get_next_sit_page(sbi, start);
1534 raw_sit = page_address(page);
1535 }
1536
1537 /* udpate entry in SIT block */
1538 seg_info_to_raw_sit(se, &raw_sit->entries[sit_offset]);
1539flush_done:
1540 __clear_bit(segno, bitmap);
1541 sit_i->dirty_sentries--;
1542 }
1543 mutex_unlock(&sit_i->sentry_lock);
1544 mutex_unlock(&curseg->curseg_mutex);
1545
1546 /* writeout last modified SIT block */
1547 f2fs_put_page(page, 1);
1548
1549 set_prefree_as_free_segments(sbi);
1550}
1551
1552static int build_sit_info(struct f2fs_sb_info *sbi)
1553{
1554 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1555 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1556 struct sit_info *sit_i;
1557 unsigned int sit_segs, start;
1558 char *src_bitmap, *dst_bitmap;
1559 unsigned int bitmap_size;
1560
1561 /* allocate memory for SIT information */
1562 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
1563 if (!sit_i)
1564 return -ENOMEM;
1565
1566 SM_I(sbi)->sit_info = sit_i;
1567
1568 sit_i->sentries = vzalloc(TOTAL_SEGS(sbi) * sizeof(struct seg_entry));
1569 if (!sit_i->sentries)
1570 return -ENOMEM;
1571
1572 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1573 sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1574 if (!sit_i->dirty_sentries_bitmap)
1575 return -ENOMEM;
1576
1577 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1578 sit_i->sentries[start].cur_valid_map
1579 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1580 sit_i->sentries[start].ckpt_valid_map
1581 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1582 if (!sit_i->sentries[start].cur_valid_map
1583 || !sit_i->sentries[start].ckpt_valid_map)
1584 return -ENOMEM;
1585 }
1586
1587 if (sbi->segs_per_sec > 1) {
Jaegeuk Kim53cf9522013-03-31 12:39:49 +09001588 sit_i->sec_entries = vzalloc(TOTAL_SECS(sbi) *
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001589 sizeof(struct sec_entry));
1590 if (!sit_i->sec_entries)
1591 return -ENOMEM;
1592 }
1593
1594 /* get information related with SIT */
1595 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
1596
1597 /* setup SIT bitmap from ckeckpoint pack */
1598 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
1599 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
1600
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001601 dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001602 if (!dst_bitmap)
1603 return -ENOMEM;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001604
1605 /* init SIT information */
1606 sit_i->s_ops = &default_salloc_ops;
1607
1608 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
1609 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
1610 sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
1611 sit_i->sit_bitmap = dst_bitmap;
1612 sit_i->bitmap_size = bitmap_size;
1613 sit_i->dirty_sentries = 0;
1614 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
1615 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
1616 sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
1617 mutex_init(&sit_i->sentry_lock);
1618 return 0;
1619}
1620
1621static int build_free_segmap(struct f2fs_sb_info *sbi)
1622{
1623 struct f2fs_sm_info *sm_info = SM_I(sbi);
1624 struct free_segmap_info *free_i;
1625 unsigned int bitmap_size, sec_bitmap_size;
1626
1627 /* allocate memory for free segmap information */
1628 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
1629 if (!free_i)
1630 return -ENOMEM;
1631
1632 SM_I(sbi)->free_info = free_i;
1633
1634 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1635 free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL);
1636 if (!free_i->free_segmap)
1637 return -ENOMEM;
1638
Jaegeuk Kim53cf9522013-03-31 12:39:49 +09001639 sec_bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001640 free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL);
1641 if (!free_i->free_secmap)
1642 return -ENOMEM;
1643
1644 /* set all segments as dirty temporarily */
1645 memset(free_i->free_segmap, 0xff, bitmap_size);
1646 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
1647
1648 /* init free segmap information */
1649 free_i->start_segno =
1650 (unsigned int) GET_SEGNO_FROM_SEG0(sbi, sm_info->main_blkaddr);
1651 free_i->free_segments = 0;
1652 free_i->free_sections = 0;
1653 rwlock_init(&free_i->segmap_lock);
1654 return 0;
1655}
1656
1657static int build_curseg(struct f2fs_sb_info *sbi)
1658{
Namjae Jeon1042d602012-12-01 10:56:13 +09001659 struct curseg_info *array;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001660 int i;
1661
1662 array = kzalloc(sizeof(*array) * NR_CURSEG_TYPE, GFP_KERNEL);
1663 if (!array)
1664 return -ENOMEM;
1665
1666 SM_I(sbi)->curseg_array = array;
1667
1668 for (i = 0; i < NR_CURSEG_TYPE; i++) {
1669 mutex_init(&array[i].curseg_mutex);
1670 array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
1671 if (!array[i].sum_blk)
1672 return -ENOMEM;
1673 array[i].segno = NULL_SEGNO;
1674 array[i].next_blkoff = 0;
1675 }
1676 return restore_curseg_summaries(sbi);
1677}
1678
1679static void build_sit_entries(struct f2fs_sb_info *sbi)
1680{
1681 struct sit_info *sit_i = SIT_I(sbi);
1682 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1683 struct f2fs_summary_block *sum = curseg->sum_blk;
Chao Yu74de5932013-11-22 09:09:59 +08001684 int sit_blk_cnt = SIT_BLK_CNT(sbi);
1685 unsigned int i, start, end;
1686 unsigned int readed, start_blk = 0;
1687 int nrpages = MAX_BIO_BLOCKS(max_hw_blocks(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001688
Chao Yu74de5932013-11-22 09:09:59 +08001689 do {
Chao Yu662befd2014-02-07 16:11:53 +08001690 readed = ra_meta_pages(sbi, start_blk, nrpages, META_SIT);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001691
Chao Yu74de5932013-11-22 09:09:59 +08001692 start = start_blk * sit_i->sents_per_block;
1693 end = (start_blk + readed) * sit_i->sents_per_block;
1694
1695 for (; start < end && start < TOTAL_SEGS(sbi); start++) {
1696 struct seg_entry *se = &sit_i->sentries[start];
1697 struct f2fs_sit_block *sit_blk;
1698 struct f2fs_sit_entry sit;
1699 struct page *page;
1700
1701 mutex_lock(&curseg->curseg_mutex);
1702 for (i = 0; i < sits_in_cursum(sum); i++) {
Chris Fries6c311ec2014-01-17 14:44:39 -06001703 if (le32_to_cpu(segno_in_journal(sum, i))
1704 == start) {
Chao Yu74de5932013-11-22 09:09:59 +08001705 sit = sit_in_journal(sum, i);
1706 mutex_unlock(&curseg->curseg_mutex);
1707 goto got_it;
1708 }
1709 }
1710 mutex_unlock(&curseg->curseg_mutex);
1711
1712 page = get_current_sit_page(sbi, start);
1713 sit_blk = (struct f2fs_sit_block *)page_address(page);
1714 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
1715 f2fs_put_page(page, 1);
1716got_it:
1717 check_block_count(sbi, start, &sit);
1718 seg_info_from_raw_sit(se, &sit);
1719 if (sbi->segs_per_sec > 1) {
1720 struct sec_entry *e = get_sec_entry(sbi, start);
1721 e->valid_blocks += se->valid_blocks;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001722 }
1723 }
Chao Yu74de5932013-11-22 09:09:59 +08001724 start_blk += readed;
1725 } while (start_blk < sit_blk_cnt);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001726}
1727
1728static void init_free_segmap(struct f2fs_sb_info *sbi)
1729{
1730 unsigned int start;
1731 int type;
1732
1733 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1734 struct seg_entry *sentry = get_seg_entry(sbi, start);
1735 if (!sentry->valid_blocks)
1736 __set_free(sbi, start);
1737 }
1738
1739 /* set use the current segments */
1740 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
1741 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
1742 __set_test_and_inuse(sbi, curseg_t->segno);
1743 }
1744}
1745
1746static void init_dirty_segmap(struct f2fs_sb_info *sbi)
1747{
1748 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1749 struct free_segmap_info *free_i = FREE_I(sbi);
Namjae Jeon8736fbf2013-06-16 09:49:11 +09001750 unsigned int segno = 0, offset = 0, total_segs = TOTAL_SEGS(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001751 unsigned short valid_blocks;
1752
Namjae Jeon8736fbf2013-06-16 09:49:11 +09001753 while (1) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001754 /* find dirty segment based on free segmap */
Namjae Jeon8736fbf2013-06-16 09:49:11 +09001755 segno = find_next_inuse(free_i, total_segs, offset);
1756 if (segno >= total_segs)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001757 break;
1758 offset = segno + 1;
1759 valid_blocks = get_valid_blocks(sbi, segno, 0);
1760 if (valid_blocks >= sbi->blocks_per_seg || !valid_blocks)
1761 continue;
1762 mutex_lock(&dirty_i->seglist_lock);
1763 __locate_dirty_segment(sbi, segno, DIRTY);
1764 mutex_unlock(&dirty_i->seglist_lock);
1765 }
1766}
1767
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001768static int init_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001769{
1770 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001771 unsigned int bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001772
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001773 dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL);
1774 if (!dirty_i->victim_secmap)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001775 return -ENOMEM;
1776 return 0;
1777}
1778
1779static int build_dirty_segmap(struct f2fs_sb_info *sbi)
1780{
1781 struct dirty_seglist_info *dirty_i;
1782 unsigned int bitmap_size, i;
1783
1784 /* allocate memory for dirty segments list information */
1785 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
1786 if (!dirty_i)
1787 return -ENOMEM;
1788
1789 SM_I(sbi)->dirty_info = dirty_i;
1790 mutex_init(&dirty_i->seglist_lock);
1791
1792 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1793
1794 for (i = 0; i < NR_DIRTY_TYPE; i++) {
1795 dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001796 if (!dirty_i->dirty_segmap[i])
1797 return -ENOMEM;
1798 }
1799
1800 init_dirty_segmap(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001801 return init_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001802}
1803
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001804/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001805 * Update min, max modified time for cost-benefit GC algorithm
1806 */
1807static void init_min_max_mtime(struct f2fs_sb_info *sbi)
1808{
1809 struct sit_info *sit_i = SIT_I(sbi);
1810 unsigned int segno;
1811
1812 mutex_lock(&sit_i->sentry_lock);
1813
1814 sit_i->min_mtime = LLONG_MAX;
1815
1816 for (segno = 0; segno < TOTAL_SEGS(sbi); segno += sbi->segs_per_sec) {
1817 unsigned int i;
1818 unsigned long long mtime = 0;
1819
1820 for (i = 0; i < sbi->segs_per_sec; i++)
1821 mtime += get_seg_entry(sbi, segno + i)->mtime;
1822
1823 mtime = div_u64(mtime, sbi->segs_per_sec);
1824
1825 if (sit_i->min_mtime > mtime)
1826 sit_i->min_mtime = mtime;
1827 }
1828 sit_i->max_mtime = get_mtime(sbi);
1829 mutex_unlock(&sit_i->sentry_lock);
1830}
1831
1832int build_segment_manager(struct f2fs_sb_info *sbi)
1833{
1834 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1835 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +09001836 dev_t dev = sbi->sb->s_bdev->bd_dev;
Namjae Jeon1042d602012-12-01 10:56:13 +09001837 struct f2fs_sm_info *sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001838 int err;
1839
1840 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
1841 if (!sm_info)
1842 return -ENOMEM;
1843
1844 /* init sm info */
1845 sbi->sm_info = sm_info;
1846 INIT_LIST_HEAD(&sm_info->wblist_head);
1847 spin_lock_init(&sm_info->wblist_lock);
1848 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
1849 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
1850 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
1851 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
1852 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
1853 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
1854 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
Jaegeuk Kim58c41032014-03-19 14:17:21 +09001855 sm_info->rec_prefree_segments = sm_info->main_segments *
1856 DEF_RECLAIM_PREFREE_SEGMENTS / 100;
Jaegeuk Kim216fbd62013-11-07 13:13:42 +09001857 sm_info->ipu_policy = F2FS_IPU_DISABLE;
1858 sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001859
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09001860 INIT_LIST_HEAD(&sm_info->discard_list);
1861 sm_info->nr_discards = 0;
1862 sm_info->max_discards = 0;
1863
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +09001864 if (test_opt(sbi, FLUSH_MERGE)) {
1865 spin_lock_init(&sm_info->issue_lock);
1866 init_waitqueue_head(&sm_info->flush_wait_queue);
1867
1868 sm_info->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi,
1869 "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev));
1870 if (IS_ERR(sm_info->f2fs_issue_flush))
1871 return PTR_ERR(sm_info->f2fs_issue_flush);
1872 }
1873
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001874 err = build_sit_info(sbi);
1875 if (err)
1876 return err;
1877 err = build_free_segmap(sbi);
1878 if (err)
1879 return err;
1880 err = build_curseg(sbi);
1881 if (err)
1882 return err;
1883
1884 /* reinit free segmap based on SIT */
1885 build_sit_entries(sbi);
1886
1887 init_free_segmap(sbi);
1888 err = build_dirty_segmap(sbi);
1889 if (err)
1890 return err;
1891
1892 init_min_max_mtime(sbi);
1893 return 0;
1894}
1895
1896static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
1897 enum dirty_type dirty_type)
1898{
1899 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1900
1901 mutex_lock(&dirty_i->seglist_lock);
1902 kfree(dirty_i->dirty_segmap[dirty_type]);
1903 dirty_i->nr_dirty[dirty_type] = 0;
1904 mutex_unlock(&dirty_i->seglist_lock);
1905}
1906
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001907static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001908{
1909 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001910 kfree(dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001911}
1912
1913static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
1914{
1915 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1916 int i;
1917
1918 if (!dirty_i)
1919 return;
1920
1921 /* discard pre-free/dirty segments list */
1922 for (i = 0; i < NR_DIRTY_TYPE; i++)
1923 discard_dirty_segmap(sbi, i);
1924
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001925 destroy_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001926 SM_I(sbi)->dirty_info = NULL;
1927 kfree(dirty_i);
1928}
1929
1930static void destroy_curseg(struct f2fs_sb_info *sbi)
1931{
1932 struct curseg_info *array = SM_I(sbi)->curseg_array;
1933 int i;
1934
1935 if (!array)
1936 return;
1937 SM_I(sbi)->curseg_array = NULL;
1938 for (i = 0; i < NR_CURSEG_TYPE; i++)
1939 kfree(array[i].sum_blk);
1940 kfree(array);
1941}
1942
1943static void destroy_free_segmap(struct f2fs_sb_info *sbi)
1944{
1945 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
1946 if (!free_i)
1947 return;
1948 SM_I(sbi)->free_info = NULL;
1949 kfree(free_i->free_segmap);
1950 kfree(free_i->free_secmap);
1951 kfree(free_i);
1952}
1953
1954static void destroy_sit_info(struct f2fs_sb_info *sbi)
1955{
1956 struct sit_info *sit_i = SIT_I(sbi);
1957 unsigned int start;
1958
1959 if (!sit_i)
1960 return;
1961
1962 if (sit_i->sentries) {
1963 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1964 kfree(sit_i->sentries[start].cur_valid_map);
1965 kfree(sit_i->sentries[start].ckpt_valid_map);
1966 }
1967 }
1968 vfree(sit_i->sentries);
1969 vfree(sit_i->sec_entries);
1970 kfree(sit_i->dirty_sentries_bitmap);
1971
1972 SM_I(sbi)->sit_info = NULL;
1973 kfree(sit_i->sit_bitmap);
1974 kfree(sit_i);
1975}
1976
1977void destroy_segment_manager(struct f2fs_sb_info *sbi)
1978{
1979 struct f2fs_sm_info *sm_info = SM_I(sbi);
Chao Yu3b03f722013-11-06 09:12:04 +08001980 if (!sm_info)
1981 return;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +09001982 if (sm_info->f2fs_issue_flush)
1983 kthread_stop(sm_info->f2fs_issue_flush);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001984 destroy_dirty_segmap(sbi);
1985 destroy_curseg(sbi);
1986 destroy_free_segmap(sbi);
1987 destroy_sit_info(sbi);
1988 sbi->sm_info = NULL;
1989 kfree(sm_info);
1990}
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09001991
1992int __init create_segment_manager_caches(void)
1993{
1994 discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
Gu Zhenge8512d22014-03-07 18:43:28 +08001995 sizeof(struct discard_entry));
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09001996 if (!discard_entry_slab)
1997 return -ENOMEM;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +09001998 flush_cmd_slab = f2fs_kmem_cache_create("flush_command",
1999 sizeof(struct flush_cmd));
2000 if (!flush_cmd_slab) {
2001 kmem_cache_destroy(discard_entry_slab);
2002 return -ENOMEM;
2003 }
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002004 return 0;
2005}
2006
2007void destroy_segment_manager_caches(void)
2008{
2009 kmem_cache_destroy(discard_entry_slab);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +09002010 kmem_cache_destroy(flush_cmd_slab);
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002011}