blob: a6b90a520894a0791fd15035024c0344fa039c37 [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;
28
Changman Lee9a7f1432013-11-15 10:42:51 +090029/*
30 * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since
31 * MSB and LSB are reversed in a byte by f2fs_set_bit.
32 */
33static inline unsigned long __reverse_ffs(unsigned long word)
34{
35 int num = 0;
36
37#if BITS_PER_LONG == 64
38 if ((word & 0xffffffff) == 0) {
39 num += 32;
40 word >>= 32;
41 }
42#endif
43 if ((word & 0xffff) == 0) {
44 num += 16;
45 word >>= 16;
46 }
47 if ((word & 0xff) == 0) {
48 num += 8;
49 word >>= 8;
50 }
51 if ((word & 0xf0) == 0)
52 num += 4;
53 else
54 word >>= 4;
55 if ((word & 0xc) == 0)
56 num += 2;
57 else
58 word >>= 2;
59 if ((word & 0x2) == 0)
60 num += 1;
61 return num;
62}
63
64/*
arter97e1c42042014-08-06 23:22:50 +090065 * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c because
Changman Lee9a7f1432013-11-15 10:42:51 +090066 * f2fs_set_bit makes MSB and LSB reversed in a byte.
67 * Example:
68 * LSB <--> MSB
69 * f2fs_set_bit(0, bitmap) => 0000 0001
70 * f2fs_set_bit(7, bitmap) => 1000 0000
71 */
72static unsigned long __find_rev_next_bit(const unsigned long *addr,
73 unsigned long size, unsigned long offset)
74{
75 const unsigned long *p = addr + BIT_WORD(offset);
76 unsigned long result = offset & ~(BITS_PER_LONG - 1);
77 unsigned long tmp;
78 unsigned long mask, submask;
79 unsigned long quot, rest;
80
81 if (offset >= size)
82 return size;
83
84 size -= result;
85 offset %= BITS_PER_LONG;
86 if (!offset)
87 goto aligned;
88
89 tmp = *(p++);
90 quot = (offset >> 3) << 3;
91 rest = offset & 0x7;
92 mask = ~0UL << quot;
93 submask = (unsigned char)(0xff << rest) >> rest;
94 submask <<= quot;
95 mask &= submask;
96 tmp &= mask;
97 if (size < BITS_PER_LONG)
98 goto found_first;
99 if (tmp)
100 goto found_middle;
101
102 size -= BITS_PER_LONG;
103 result += BITS_PER_LONG;
104aligned:
105 while (size & ~(BITS_PER_LONG-1)) {
106 tmp = *(p++);
107 if (tmp)
108 goto found_middle;
109 result += BITS_PER_LONG;
110 size -= BITS_PER_LONG;
111 }
112 if (!size)
113 return result;
114 tmp = *p;
115found_first:
116 tmp &= (~0UL >> (BITS_PER_LONG - size));
117 if (tmp == 0UL) /* Are any bits set? */
118 return result + size; /* Nope. */
119found_middle:
120 return result + __reverse_ffs(tmp);
121}
122
123static unsigned long __find_rev_next_zero_bit(const unsigned long *addr,
124 unsigned long size, unsigned long offset)
125{
126 const unsigned long *p = addr + BIT_WORD(offset);
127 unsigned long result = offset & ~(BITS_PER_LONG - 1);
128 unsigned long tmp;
129 unsigned long mask, submask;
130 unsigned long quot, rest;
131
132 if (offset >= size)
133 return size;
134
135 size -= result;
136 offset %= BITS_PER_LONG;
137 if (!offset)
138 goto aligned;
139
140 tmp = *(p++);
141 quot = (offset >> 3) << 3;
142 rest = offset & 0x7;
143 mask = ~(~0UL << quot);
144 submask = (unsigned char)~((unsigned char)(0xff << rest) >> rest);
145 submask <<= quot;
146 mask += submask;
147 tmp |= mask;
148 if (size < BITS_PER_LONG)
149 goto found_first;
150 if (~tmp)
151 goto found_middle;
152
153 size -= BITS_PER_LONG;
154 result += BITS_PER_LONG;
155aligned:
156 while (size & ~(BITS_PER_LONG - 1)) {
157 tmp = *(p++);
158 if (~tmp)
159 goto found_middle;
160 result += BITS_PER_LONG;
161 size -= BITS_PER_LONG;
162 }
163 if (!size)
164 return result;
165 tmp = *p;
166
167found_first:
168 tmp |= ~0UL << size;
169 if (tmp == ~0UL) /* Are any bits zero? */
170 return result + size; /* Nope. */
171found_middle:
172 return result + __reverse_ffz(tmp);
173}
174
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900175/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900176 * This function balances dirty node and dentry pages.
177 * In addition, it controls garbage collection.
178 */
179void f2fs_balance_fs(struct f2fs_sb_info *sbi)
180{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900181 /*
Jaegeuk Kim029cd282012-12-21 17:20:21 +0900182 * We should do GC or end up with checkpoint, if there are so many dirty
183 * dir/node pages without enough free segments.
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900184 */
Jaegeuk Kim43727522013-02-04 15:11:17 +0900185 if (has_not_enough_free_secs(sbi, 0)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900186 mutex_lock(&sbi->gc_mutex);
Jaegeuk Kim408e9372013-01-03 17:55:52 +0900187 f2fs_gc(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900188 }
189}
190
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900191void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi)
192{
193 /* check the # of cached NAT entries and prefree segments */
194 if (try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK) ||
195 excess_prefree_segs(sbi))
196 f2fs_sync_fs(sbi->sb, true);
197}
198
Gu Zheng2163d192014-04-27 14:21:33 +0800199static int issue_flush_thread(void *data)
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900200{
201 struct f2fs_sb_info *sbi = data;
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800202 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
203 wait_queue_head_t *q = &fcc->flush_wait_queue;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900204repeat:
205 if (kthread_should_stop())
206 return 0;
207
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800208 spin_lock(&fcc->issue_lock);
209 if (fcc->issue_list) {
210 fcc->dispatch_list = fcc->issue_list;
211 fcc->issue_list = fcc->issue_tail = NULL;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900212 }
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800213 spin_unlock(&fcc->issue_lock);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900214
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800215 if (fcc->dispatch_list) {
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900216 struct bio *bio = bio_alloc(GFP_NOIO, 0);
217 struct flush_cmd *cmd, *next;
218 int ret;
219
220 bio->bi_bdev = sbi->sb->s_bdev;
221 ret = submit_bio_wait(WRITE_FLUSH, bio);
222
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800223 for (cmd = fcc->dispatch_list; cmd; cmd = next) {
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900224 cmd->ret = ret;
225 next = cmd->next;
226 complete(&cmd->wait);
227 }
Gu Zhenga4ed23f2014-04-11 17:49:35 +0800228 bio_put(bio);
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800229 fcc->dispatch_list = NULL;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900230 }
231
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800232 wait_event_interruptible(*q,
233 kthread_should_stop() || fcc->issue_list);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900234 goto repeat;
235}
236
237int f2fs_issue_flush(struct f2fs_sb_info *sbi)
238{
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800239 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
Chao Yuadf8d902014-05-08 17:00:35 +0800240 struct flush_cmd cmd;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900241
Jaegeuk Kim24a9ee02014-07-25 17:46:10 -0700242 trace_f2fs_issue_flush(sbi->sb, test_opt(sbi, NOBARRIER),
243 test_opt(sbi, FLUSH_MERGE));
244
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700245 if (test_opt(sbi, NOBARRIER))
246 return 0;
247
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900248 if (!test_opt(sbi, FLUSH_MERGE))
249 return blkdev_issue_flush(sbi->sb->s_bdev, GFP_KERNEL, NULL);
250
Chao Yuadf8d902014-05-08 17:00:35 +0800251 init_completion(&cmd.wait);
252 cmd.next = NULL;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900253
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800254 spin_lock(&fcc->issue_lock);
255 if (fcc->issue_list)
Chao Yuadf8d902014-05-08 17:00:35 +0800256 fcc->issue_tail->next = &cmd;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900257 else
Chao Yuadf8d902014-05-08 17:00:35 +0800258 fcc->issue_list = &cmd;
259 fcc->issue_tail = &cmd;
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800260 spin_unlock(&fcc->issue_lock);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900261
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800262 if (!fcc->dispatch_list)
263 wake_up(&fcc->flush_wait_queue);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900264
Chao Yuadf8d902014-05-08 17:00:35 +0800265 wait_for_completion(&cmd.wait);
266
267 return cmd.ret;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900268}
269
Gu Zheng2163d192014-04-27 14:21:33 +0800270int create_flush_cmd_control(struct f2fs_sb_info *sbi)
271{
272 dev_t dev = sbi->sb->s_bdev->bd_dev;
273 struct flush_cmd_control *fcc;
274 int err = 0;
275
276 fcc = kzalloc(sizeof(struct flush_cmd_control), GFP_KERNEL);
277 if (!fcc)
278 return -ENOMEM;
279 spin_lock_init(&fcc->issue_lock);
280 init_waitqueue_head(&fcc->flush_wait_queue);
Chao Yu6b2920a2014-07-07 11:21:59 +0800281 SM_I(sbi)->cmd_control_info = fcc;
Gu Zheng2163d192014-04-27 14:21:33 +0800282 fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi,
283 "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev));
284 if (IS_ERR(fcc->f2fs_issue_flush)) {
285 err = PTR_ERR(fcc->f2fs_issue_flush);
286 kfree(fcc);
Chao Yu6b2920a2014-07-07 11:21:59 +0800287 SM_I(sbi)->cmd_control_info = NULL;
Gu Zheng2163d192014-04-27 14:21:33 +0800288 return err;
289 }
Gu Zheng2163d192014-04-27 14:21:33 +0800290
291 return err;
292}
293
294void destroy_flush_cmd_control(struct f2fs_sb_info *sbi)
295{
Chao Yu6b2920a2014-07-07 11:21:59 +0800296 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
Gu Zheng2163d192014-04-27 14:21:33 +0800297
298 if (fcc && fcc->f2fs_issue_flush)
299 kthread_stop(fcc->f2fs_issue_flush);
300 kfree(fcc);
Chao Yu6b2920a2014-07-07 11:21:59 +0800301 SM_I(sbi)->cmd_control_info = NULL;
Gu Zheng2163d192014-04-27 14:21:33 +0800302}
303
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900304static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
305 enum dirty_type dirty_type)
306{
307 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
308
309 /* need not be added */
310 if (IS_CURSEG(sbi, segno))
311 return;
312
313 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
314 dirty_i->nr_dirty[dirty_type]++;
315
316 if (dirty_type == DIRTY) {
317 struct seg_entry *sentry = get_seg_entry(sbi, segno);
Changman Lee4625d6a2013-10-25 17:31:57 +0900318 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900319
Jaegeuk Kimec325b52014-09-02 16:24:11 -0700320 if (unlikely(t >= DIRTY)) {
321 f2fs_bug_on(sbi, 1);
322 return;
323 }
Changman Lee4625d6a2013-10-25 17:31:57 +0900324 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
325 dirty_i->nr_dirty[t]++;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900326 }
327}
328
329static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
330 enum dirty_type dirty_type)
331{
332 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
333
334 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
335 dirty_i->nr_dirty[dirty_type]--;
336
337 if (dirty_type == DIRTY) {
Changman Lee4625d6a2013-10-25 17:31:57 +0900338 struct seg_entry *sentry = get_seg_entry(sbi, segno);
339 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900340
Changman Lee4625d6a2013-10-25 17:31:57 +0900341 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
342 dirty_i->nr_dirty[t]--;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900343
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +0900344 if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0)
345 clear_bit(GET_SECNO(sbi, segno),
346 dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900347 }
348}
349
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900350/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900351 * Should not occur error such as -ENOMEM.
352 * Adding dirty entry into seglist is not critical operation.
353 * If a given segment is one of current working segments, it won't be added.
354 */
Haicheng Li8d8451a2013-06-13 16:59:28 +0800355static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900356{
357 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
358 unsigned short valid_blocks;
359
360 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
361 return;
362
363 mutex_lock(&dirty_i->seglist_lock);
364
365 valid_blocks = get_valid_blocks(sbi, segno, 0);
366
367 if (valid_blocks == 0) {
368 __locate_dirty_segment(sbi, segno, PRE);
369 __remove_dirty_segment(sbi, segno, DIRTY);
370 } else if (valid_blocks < sbi->blocks_per_seg) {
371 __locate_dirty_segment(sbi, segno, DIRTY);
372 } else {
373 /* Recovery routine with SSR needs this */
374 __remove_dirty_segment(sbi, segno, DIRTY);
375 }
376
377 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900378}
379
Jaegeuk Kim1e87a782014-04-15 13:57:55 +0900380static int f2fs_issue_discard(struct f2fs_sb_info *sbi,
Jaegeuk Kim37208872013-11-12 16:55:17 +0900381 block_t blkstart, block_t blklen)
382{
Jaegeuk Kimf9a4e6d2013-11-28 12:44:05 +0900383 sector_t start = SECTOR_FROM_BLOCK(sbi, blkstart);
384 sector_t len = SECTOR_FROM_BLOCK(sbi, blklen);
Jaegeuk Kim1661d072013-11-12 17:01:00 +0900385 trace_f2fs_issue_discard(sbi->sb, blkstart, blklen);
Jaegeuk Kim1e87a782014-04-15 13:57:55 +0900386 return blkdev_issue_discard(sbi->sb->s_bdev, start, len, GFP_NOFS, 0);
387}
388
Jaegeuk Kimcf2271e2014-07-25 15:47:25 -0700389void discard_next_dnode(struct f2fs_sb_info *sbi, block_t blkaddr)
Jaegeuk Kim1e87a782014-04-15 13:57:55 +0900390{
Jaegeuk Kim1e87a782014-04-15 13:57:55 +0900391 if (f2fs_issue_discard(sbi, blkaddr, 1)) {
392 struct page *page = grab_meta_page(sbi, blkaddr);
393 /* zero-filled page */
394 set_page_dirty(page);
395 f2fs_put_page(page, 1);
396 }
Jaegeuk Kim37208872013-11-12 16:55:17 +0900397}
398
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900399static void add_discard_addrs(struct f2fs_sb_info *sbi,
400 unsigned int segno, struct seg_entry *se)
401{
402 struct list_head *head = &SM_I(sbi)->discard_list;
403 struct discard_entry *new;
404 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
405 int max_blocks = sbi->blocks_per_seg;
406 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
407 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
408 unsigned long dmap[entries];
409 unsigned int start = 0, end = -1;
410 int i;
411
412 if (!test_opt(sbi, DISCARD))
413 return;
414
415 /* zero block will be discarded through the prefree list */
416 if (!se->valid_blocks || se->valid_blocks == max_blocks)
417 return;
418
419 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
420 for (i = 0; i < entries; i++)
421 dmap[i] = (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
422
423 while (SM_I(sbi)->nr_discards <= SM_I(sbi)->max_discards) {
424 start = __find_rev_next_bit(dmap, max_blocks, end + 1);
425 if (start >= max_blocks)
426 break;
427
428 end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
429
430 new = f2fs_kmem_cache_alloc(discard_entry_slab, GFP_NOFS);
431 INIT_LIST_HEAD(&new->list);
432 new->blkaddr = START_BLOCK(sbi, segno) + start;
433 new->len = end - start;
434
435 list_add_tail(&new->list, head);
436 SM_I(sbi)->nr_discards += end - start;
437 }
438}
439
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900440/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900441 * Should call clear_prefree_segments after checkpoint is done.
442 */
443static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
444{
445 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Chao Yub65ee142014-08-04 10:10:07 +0800446 unsigned int segno;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900447 unsigned int total_segs = TOTAL_SEGS(sbi);
448
449 mutex_lock(&dirty_i->seglist_lock);
Chao Yub65ee142014-08-04 10:10:07 +0800450 for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], total_segs)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900451 __set_test_and_free(sbi, segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900452 mutex_unlock(&dirty_i->seglist_lock);
453}
454
455void clear_prefree_segments(struct f2fs_sb_info *sbi)
456{
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900457 struct list_head *head = &(SM_I(sbi)->discard_list);
Chao Yu2d7b8222014-03-29 11:33:17 +0800458 struct discard_entry *entry, *this;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900459 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Changman Lee29e59c12013-11-11 09:24:37 +0900460 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900461 unsigned int total_segs = TOTAL_SEGS(sbi);
Changman Lee29e59c12013-11-11 09:24:37 +0900462 unsigned int start = 0, end = -1;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900463
464 mutex_lock(&dirty_i->seglist_lock);
Changman Lee29e59c12013-11-11 09:24:37 +0900465
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900466 while (1) {
Changman Lee29e59c12013-11-11 09:24:37 +0900467 int i;
468 start = find_next_bit(prefree_map, total_segs, end + 1);
469 if (start >= total_segs)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900470 break;
Changman Lee29e59c12013-11-11 09:24:37 +0900471 end = find_next_zero_bit(prefree_map, total_segs, start + 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900472
Changman Lee29e59c12013-11-11 09:24:37 +0900473 for (i = start; i < end; i++)
474 clear_bit(i, prefree_map);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900475
Changman Lee29e59c12013-11-11 09:24:37 +0900476 dirty_i->nr_dirty[PRE] -= end - start;
477
478 if (!test_opt(sbi, DISCARD))
479 continue;
480
Jaegeuk Kim37208872013-11-12 16:55:17 +0900481 f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
482 (end - start) << sbi->log_blocks_per_seg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900483 }
484 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900485
486 /* send small discards */
Chao Yu2d7b8222014-03-29 11:33:17 +0800487 list_for_each_entry_safe(entry, this, head, list) {
Jaegeuk Kim37208872013-11-12 16:55:17 +0900488 f2fs_issue_discard(sbi, entry->blkaddr, entry->len);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900489 list_del(&entry->list);
490 SM_I(sbi)->nr_discards -= entry->len;
491 kmem_cache_free(discard_entry_slab, entry);
492 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900493}
494
495static void __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
496{
497 struct sit_info *sit_i = SIT_I(sbi);
498 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap))
499 sit_i->dirty_sentries++;
500}
501
502static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
503 unsigned int segno, int modified)
504{
505 struct seg_entry *se = get_seg_entry(sbi, segno);
506 se->type = type;
507 if (modified)
508 __mark_sit_entry_dirty(sbi, segno);
509}
510
511static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
512{
513 struct seg_entry *se;
514 unsigned int segno, offset;
515 long int new_vblocks;
516
517 segno = GET_SEGNO(sbi, blkaddr);
518
519 se = get_seg_entry(sbi, segno);
520 new_vblocks = se->valid_blocks + del;
Jaegeuk Kim491c0852014-02-04 13:01:10 +0900521 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900522
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700523 f2fs_bug_on(sbi, (new_vblocks >> (sizeof(unsigned short) << 3) ||
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900524 (new_vblocks > sbi->blocks_per_seg)));
525
526 se->valid_blocks = new_vblocks;
527 se->mtime = get_mtime(sbi);
528 SIT_I(sbi)->max_mtime = se->mtime;
529
530 /* Update valid block bitmap */
531 if (del > 0) {
532 if (f2fs_set_bit(offset, se->cur_valid_map))
Jaegeuk Kim05796762014-09-02 16:05:00 -0700533 f2fs_bug_on(sbi, 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900534 } else {
535 if (!f2fs_clear_bit(offset, se->cur_valid_map))
Jaegeuk Kim05796762014-09-02 16:05:00 -0700536 f2fs_bug_on(sbi, 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900537 }
538 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
539 se->ckpt_valid_blocks += del;
540
541 __mark_sit_entry_dirty(sbi, segno);
542
543 /* update total number of valid blocks to be written in ckpt area */
544 SIT_I(sbi)->written_valid_blocks += del;
545
546 if (sbi->segs_per_sec > 1)
547 get_sec_entry(sbi, segno)->valid_blocks += del;
548}
549
Jaegeuk Kim5e443812014-01-28 12:22:14 +0900550void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900551{
Jaegeuk Kim5e443812014-01-28 12:22:14 +0900552 update_sit_entry(sbi, new, 1);
553 if (GET_SEGNO(sbi, old) != NULL_SEGNO)
554 update_sit_entry(sbi, old, -1);
555
556 locate_dirty_segment(sbi, GET_SEGNO(sbi, old));
557 locate_dirty_segment(sbi, GET_SEGNO(sbi, new));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900558}
559
560void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
561{
562 unsigned int segno = GET_SEGNO(sbi, addr);
563 struct sit_info *sit_i = SIT_I(sbi);
564
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700565 f2fs_bug_on(sbi, addr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900566 if (addr == NEW_ADDR)
567 return;
568
569 /* add it into sit main buffer */
570 mutex_lock(&sit_i->sentry_lock);
571
572 update_sit_entry(sbi, addr, -1);
573
574 /* add it into dirty seglist */
575 locate_dirty_segment(sbi, segno);
576
577 mutex_unlock(&sit_i->sentry_lock);
578}
579
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900580/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900581 * This function should be resided under the curseg_mutex lock
582 */
583static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
Haicheng Lie79efe32013-06-13 16:59:27 +0800584 struct f2fs_summary *sum)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900585{
586 struct curseg_info *curseg = CURSEG_I(sbi, type);
587 void *addr = curseg->sum_blk;
Haicheng Lie79efe32013-06-13 16:59:27 +0800588 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900589 memcpy(addr, sum, sizeof(struct f2fs_summary));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900590}
591
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900592/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900593 * Calculate the number of current summary pages for writing
594 */
595int npages_for_summary_flush(struct f2fs_sb_info *sbi)
596{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900597 int valid_sum_count = 0;
Fan Li9a479382013-10-29 16:21:47 +0800598 int i, sum_in_page;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900599
600 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
601 if (sbi->ckpt->alloc_type[i] == SSR)
602 valid_sum_count += sbi->blocks_per_seg;
603 else
604 valid_sum_count += curseg_blkoff(sbi, i);
605 }
606
Fan Li9a479382013-10-29 16:21:47 +0800607 sum_in_page = (PAGE_CACHE_SIZE - 2 * SUM_JOURNAL_SIZE -
608 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
609 if (valid_sum_count <= sum_in_page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900610 return 1;
Fan Li9a479382013-10-29 16:21:47 +0800611 else if ((valid_sum_count - sum_in_page) <=
612 (PAGE_CACHE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900613 return 2;
614 return 3;
615}
616
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900617/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900618 * Caller should put this summary page
619 */
620struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
621{
622 return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
623}
624
625static void write_sum_page(struct f2fs_sb_info *sbi,
626 struct f2fs_summary_block *sum_blk, block_t blk_addr)
627{
628 struct page *page = grab_meta_page(sbi, blk_addr);
629 void *kaddr = page_address(page);
630 memcpy(kaddr, sum_blk, PAGE_CACHE_SIZE);
631 set_page_dirty(page);
632 f2fs_put_page(page, 1);
633}
634
Jaegeuk Kim60374682013-03-31 13:58:51 +0900635static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
636{
637 struct curseg_info *curseg = CURSEG_I(sbi, type);
Haicheng Li81fb5e82013-05-14 18:20:28 +0800638 unsigned int segno = curseg->segno + 1;
Jaegeuk Kim60374682013-03-31 13:58:51 +0900639 struct free_segmap_info *free_i = FREE_I(sbi);
640
Haicheng Li81fb5e82013-05-14 18:20:28 +0800641 if (segno < TOTAL_SEGS(sbi) && segno % sbi->segs_per_sec)
642 return !test_bit(segno, free_i->free_segmap);
Jaegeuk Kim60374682013-03-31 13:58:51 +0900643 return 0;
644}
645
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900646/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900647 * Find a new segment from the free segments bitmap to right order
648 * This function should be returned with success, otherwise BUG
649 */
650static void get_new_segment(struct f2fs_sb_info *sbi,
651 unsigned int *newseg, bool new_sec, int dir)
652{
653 struct free_segmap_info *free_i = FREE_I(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900654 unsigned int segno, secno, zoneno;
Jaegeuk Kim53cf9522013-03-31 12:39:49 +0900655 unsigned int total_zones = TOTAL_SECS(sbi) / sbi->secs_per_zone;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900656 unsigned int hint = *newseg / sbi->segs_per_sec;
657 unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
658 unsigned int left_start = hint;
659 bool init = true;
660 int go_left = 0;
661 int i;
662
663 write_lock(&free_i->segmap_lock);
664
665 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
666 segno = find_next_zero_bit(free_i->free_segmap,
667 TOTAL_SEGS(sbi), *newseg + 1);
Jaegeuk Kim33afa7f2013-03-31 12:59:53 +0900668 if (segno - *newseg < sbi->segs_per_sec -
669 (*newseg % sbi->segs_per_sec))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900670 goto got_it;
671 }
672find_other_zone:
Jaegeuk Kim53cf9522013-03-31 12:39:49 +0900673 secno = find_next_zero_bit(free_i->free_secmap, TOTAL_SECS(sbi), hint);
674 if (secno >= TOTAL_SECS(sbi)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900675 if (dir == ALLOC_RIGHT) {
676 secno = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim53cf9522013-03-31 12:39:49 +0900677 TOTAL_SECS(sbi), 0);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700678 f2fs_bug_on(sbi, secno >= TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900679 } else {
680 go_left = 1;
681 left_start = hint - 1;
682 }
683 }
684 if (go_left == 0)
685 goto skip_left;
686
687 while (test_bit(left_start, free_i->free_secmap)) {
688 if (left_start > 0) {
689 left_start--;
690 continue;
691 }
692 left_start = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim53cf9522013-03-31 12:39:49 +0900693 TOTAL_SECS(sbi), 0);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700694 f2fs_bug_on(sbi, left_start >= TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900695 break;
696 }
697 secno = left_start;
698skip_left:
699 hint = secno;
700 segno = secno * sbi->segs_per_sec;
701 zoneno = secno / sbi->secs_per_zone;
702
703 /* give up on finding another zone */
704 if (!init)
705 goto got_it;
706 if (sbi->secs_per_zone == 1)
707 goto got_it;
708 if (zoneno == old_zoneno)
709 goto got_it;
710 if (dir == ALLOC_LEFT) {
711 if (!go_left && zoneno + 1 >= total_zones)
712 goto got_it;
713 if (go_left && zoneno == 0)
714 goto got_it;
715 }
716 for (i = 0; i < NR_CURSEG_TYPE; i++)
717 if (CURSEG_I(sbi, i)->zone == zoneno)
718 break;
719
720 if (i < NR_CURSEG_TYPE) {
721 /* zone is in user, try another */
722 if (go_left)
723 hint = zoneno * sbi->secs_per_zone - 1;
724 else if (zoneno + 1 >= total_zones)
725 hint = 0;
726 else
727 hint = (zoneno + 1) * sbi->secs_per_zone;
728 init = false;
729 goto find_other_zone;
730 }
731got_it:
732 /* set it as dirty segment in free segmap */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700733 f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900734 __set_inuse(sbi, segno);
735 *newseg = segno;
736 write_unlock(&free_i->segmap_lock);
737}
738
739static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
740{
741 struct curseg_info *curseg = CURSEG_I(sbi, type);
742 struct summary_footer *sum_footer;
743
744 curseg->segno = curseg->next_segno;
745 curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
746 curseg->next_blkoff = 0;
747 curseg->next_segno = NULL_SEGNO;
748
749 sum_footer = &(curseg->sum_blk->footer);
750 memset(sum_footer, 0, sizeof(struct summary_footer));
751 if (IS_DATASEG(type))
752 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
753 if (IS_NODESEG(type))
754 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
755 __set_sit_entry_type(sbi, type, curseg->segno, modified);
756}
757
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900758/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900759 * Allocate a current working segment.
760 * This function always allocates a free segment in LFS manner.
761 */
762static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
763{
764 struct curseg_info *curseg = CURSEG_I(sbi, type);
765 unsigned int segno = curseg->segno;
766 int dir = ALLOC_LEFT;
767
768 write_sum_page(sbi, curseg->sum_blk,
Haicheng Li81fb5e82013-05-14 18:20:28 +0800769 GET_SUM_BLOCK(sbi, segno));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900770 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
771 dir = ALLOC_RIGHT;
772
773 if (test_opt(sbi, NOHEAP))
774 dir = ALLOC_RIGHT;
775
776 get_new_segment(sbi, &segno, new_sec, dir);
777 curseg->next_segno = segno;
778 reset_curseg(sbi, type, 1);
779 curseg->alloc_type = LFS;
780}
781
782static void __next_free_blkoff(struct f2fs_sb_info *sbi,
783 struct curseg_info *seg, block_t start)
784{
785 struct seg_entry *se = get_seg_entry(sbi, seg->segno);
Changman Leee81c93c2013-11-15 13:21:16 +0900786 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
787 unsigned long target_map[entries];
788 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
789 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
790 int i, pos;
791
792 for (i = 0; i < entries; i++)
793 target_map[i] = ckpt_map[i] | cur_map[i];
794
795 pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
796
797 seg->next_blkoff = pos;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900798}
799
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900800/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900801 * If a segment is written by LFS manner, next block offset is just obtained
802 * by increasing the current block offset. However, if a segment is written by
803 * SSR manner, next block offset obtained by calling __next_free_blkoff
804 */
805static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
806 struct curseg_info *seg)
807{
808 if (seg->alloc_type == SSR)
809 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
810 else
811 seg->next_blkoff++;
812}
813
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900814/*
arter97e1c42042014-08-06 23:22:50 +0900815 * This function always allocates a used segment(from dirty seglist) by SSR
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900816 * manner, so it should recover the existing segment information of valid blocks
817 */
818static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
819{
820 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
821 struct curseg_info *curseg = CURSEG_I(sbi, type);
822 unsigned int new_segno = curseg->next_segno;
823 struct f2fs_summary_block *sum_node;
824 struct page *sum_page;
825
826 write_sum_page(sbi, curseg->sum_blk,
827 GET_SUM_BLOCK(sbi, curseg->segno));
828 __set_test_and_inuse(sbi, new_segno);
829
830 mutex_lock(&dirty_i->seglist_lock);
831 __remove_dirty_segment(sbi, new_segno, PRE);
832 __remove_dirty_segment(sbi, new_segno, DIRTY);
833 mutex_unlock(&dirty_i->seglist_lock);
834
835 reset_curseg(sbi, type, 1);
836 curseg->alloc_type = SSR;
837 __next_free_blkoff(sbi, curseg, 0);
838
839 if (reuse) {
840 sum_page = get_sum_page(sbi, new_segno);
841 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
842 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
843 f2fs_put_page(sum_page, 1);
844 }
845}
846
Jaegeuk Kim43727522013-02-04 15:11:17 +0900847static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
848{
849 struct curseg_info *curseg = CURSEG_I(sbi, type);
850 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
851
852 if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0))
853 return v_ops->get_victim(sbi,
854 &(curseg)->next_segno, BG_GC, type, SSR);
855
856 /* For data segments, let's do SSR more intensively */
857 for (; type >= CURSEG_HOT_DATA; type--)
858 if (v_ops->get_victim(sbi, &(curseg)->next_segno,
859 BG_GC, type, SSR))
860 return 1;
861 return 0;
862}
863
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900864/*
865 * flush out current segment and replace it with new segment
866 * This function should be returned with success, otherwise BUG
867 */
868static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
869 int type, bool force)
870{
871 struct curseg_info *curseg = CURSEG_I(sbi, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900872
Gu Zheng7b405272013-08-19 09:41:15 +0800873 if (force)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900874 new_curseg(sbi, type, true);
Gu Zheng7b405272013-08-19 09:41:15 +0800875 else if (type == CURSEG_WARM_NODE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900876 new_curseg(sbi, type, false);
Jaegeuk Kim60374682013-03-31 13:58:51 +0900877 else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
878 new_curseg(sbi, type, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900879 else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
880 change_curseg(sbi, type, true);
881 else
882 new_curseg(sbi, type, false);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +0900883
884 stat_inc_seg_type(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900885}
886
887void allocate_new_segments(struct f2fs_sb_info *sbi)
888{
889 struct curseg_info *curseg;
890 unsigned int old_curseg;
891 int i;
892
893 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
894 curseg = CURSEG_I(sbi, i);
895 old_curseg = curseg->segno;
896 SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
897 locate_dirty_segment(sbi, old_curseg);
898 }
899}
900
901static const struct segment_allocation default_salloc_ops = {
902 .allocate_segment = allocate_segment_by_default,
903};
904
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900905static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
906{
907 struct curseg_info *curseg = CURSEG_I(sbi, type);
908 if (curseg->next_blkoff < sbi->blocks_per_seg)
909 return true;
910 return false;
911}
912
913static int __get_segment_type_2(struct page *page, enum page_type p_type)
914{
915 if (p_type == DATA)
916 return CURSEG_HOT_DATA;
917 else
918 return CURSEG_HOT_NODE;
919}
920
921static int __get_segment_type_4(struct page *page, enum page_type p_type)
922{
923 if (p_type == DATA) {
924 struct inode *inode = page->mapping->host;
925
926 if (S_ISDIR(inode->i_mode))
927 return CURSEG_HOT_DATA;
928 else
929 return CURSEG_COLD_DATA;
930 } else {
931 if (IS_DNODE(page) && !is_cold_node(page))
932 return CURSEG_HOT_NODE;
933 else
934 return CURSEG_COLD_NODE;
935 }
936}
937
938static int __get_segment_type_6(struct page *page, enum page_type p_type)
939{
940 if (p_type == DATA) {
941 struct inode *inode = page->mapping->host;
942
943 if (S_ISDIR(inode->i_mode))
944 return CURSEG_HOT_DATA;
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900945 else if (is_cold_data(page) || file_is_cold(inode))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900946 return CURSEG_COLD_DATA;
947 else
948 return CURSEG_WARM_DATA;
949 } else {
950 if (IS_DNODE(page))
951 return is_cold_node(page) ? CURSEG_WARM_NODE :
952 CURSEG_HOT_NODE;
953 else
954 return CURSEG_COLD_NODE;
955 }
956}
957
958static int __get_segment_type(struct page *page, enum page_type p_type)
959{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700960 switch (F2FS_P_SB(page)->active_logs) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900961 case 2:
962 return __get_segment_type_2(page, p_type);
963 case 4:
964 return __get_segment_type_4(page, p_type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900965 }
Jaegeuk Kim12a67142012-12-21 11:47:05 +0900966 /* NR_CURSEG_TYPE(6) logs by default */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700967 f2fs_bug_on(F2FS_P_SB(page),
968 F2FS_P_SB(page)->active_logs != NR_CURSEG_TYPE);
Jaegeuk Kim12a67142012-12-21 11:47:05 +0900969 return __get_segment_type_6(page, p_type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900970}
971
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900972void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
973 block_t old_blkaddr, block_t *new_blkaddr,
974 struct f2fs_summary *sum, int type)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900975{
976 struct sit_info *sit_i = SIT_I(sbi);
977 struct curseg_info *curseg;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900978
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900979 curseg = CURSEG_I(sbi, type);
980
981 mutex_lock(&curseg->curseg_mutex);
982
983 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900984
985 /*
986 * __add_sum_entry should be resided under the curseg_mutex
987 * because, this function updates a summary entry in the
988 * current summary block.
989 */
Haicheng Lie79efe32013-06-13 16:59:27 +0800990 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900991
992 mutex_lock(&sit_i->sentry_lock);
993 __refresh_next_blkoff(sbi, curseg);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +0900994
995 stat_inc_block_count(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900996
Jaegeuk Kim5e443812014-01-28 12:22:14 +0900997 if (!__has_curseg_space(sbi, type))
998 sit_i->s_ops->allocate_segment(sbi, type, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900999 /*
1000 * SIT information should be updated before segment allocation,
1001 * since SSR needs latest valid block information.
1002 */
1003 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
Jaegeuk Kim5e443812014-01-28 12:22:14 +09001004
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001005 mutex_unlock(&sit_i->sentry_lock);
1006
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001007 if (page && IS_NODESEG(type))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001008 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
1009
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001010 mutex_unlock(&curseg->curseg_mutex);
1011}
1012
1013static void do_write_page(struct f2fs_sb_info *sbi, struct page *page,
1014 block_t old_blkaddr, block_t *new_blkaddr,
1015 struct f2fs_summary *sum, struct f2fs_io_info *fio)
1016{
1017 int type = __get_segment_type(page, fio->type);
1018
1019 allocate_data_block(sbi, page, old_blkaddr, new_blkaddr, sum, type);
1020
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001021 /* writeout dirty page into bdev */
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001022 f2fs_submit_page_mbio(sbi, page, *new_blkaddr, fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001023}
1024
Jaegeuk Kim577e3492013-01-24 19:56:11 +09001025void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001026{
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001027 struct f2fs_io_info fio = {
1028 .type = META,
Gu Zheng7e8f2302013-12-20 18:17:49 +08001029 .rw = WRITE_SYNC | REQ_META | REQ_PRIO
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001030 };
1031
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001032 set_page_writeback(page);
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001033 f2fs_submit_page_mbio(sbi, page, page->index, &fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001034}
1035
1036void write_node_page(struct f2fs_sb_info *sbi, struct page *page,
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001037 struct f2fs_io_info *fio,
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001038 unsigned int nid, block_t old_blkaddr, block_t *new_blkaddr)
1039{
1040 struct f2fs_summary sum;
1041 set_summary(&sum, nid, 0, 0);
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001042 do_write_page(sbi, page, old_blkaddr, new_blkaddr, &sum, fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001043}
1044
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001045void write_data_page(struct page *page, struct dnode_of_data *dn,
1046 block_t *new_blkaddr, struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001047{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001048 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001049 struct f2fs_summary sum;
1050 struct node_info ni;
1051
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001052 f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001053 get_node_info(sbi, dn->nid, &ni);
1054 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1055
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001056 do_write_page(sbi, page, dn->data_blkaddr, new_blkaddr, &sum, fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001057}
1058
Chris Fries6c311ec2014-01-17 14:44:39 -06001059void rewrite_data_page(struct page *page, block_t old_blkaddr,
1060 struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001061{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001062 f2fs_submit_page_mbio(F2FS_P_SB(page), page, old_blkaddr, fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001063}
1064
1065void recover_data_page(struct f2fs_sb_info *sbi,
1066 struct page *page, struct f2fs_summary *sum,
1067 block_t old_blkaddr, block_t new_blkaddr)
1068{
1069 struct sit_info *sit_i = SIT_I(sbi);
1070 struct curseg_info *curseg;
1071 unsigned int segno, old_cursegno;
1072 struct seg_entry *se;
1073 int type;
1074
1075 segno = GET_SEGNO(sbi, new_blkaddr);
1076 se = get_seg_entry(sbi, segno);
1077 type = se->type;
1078
1079 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
1080 if (old_blkaddr == NULL_ADDR)
1081 type = CURSEG_COLD_DATA;
1082 else
1083 type = CURSEG_WARM_DATA;
1084 }
1085 curseg = CURSEG_I(sbi, type);
1086
1087 mutex_lock(&curseg->curseg_mutex);
1088 mutex_lock(&sit_i->sentry_lock);
1089
1090 old_cursegno = curseg->segno;
1091
1092 /* change the current segment */
1093 if (segno != curseg->segno) {
1094 curseg->next_segno = segno;
1095 change_curseg(sbi, type, true);
1096 }
1097
Jaegeuk Kim491c0852014-02-04 13:01:10 +09001098 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
Haicheng Lie79efe32013-06-13 16:59:27 +08001099 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001100
1101 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001102 locate_dirty_segment(sbi, old_cursegno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001103
1104 mutex_unlock(&sit_i->sentry_lock);
1105 mutex_unlock(&curseg->curseg_mutex);
1106}
1107
Chao Yudf0f8dc2014-03-22 14:57:23 +08001108static inline bool is_merged_page(struct f2fs_sb_info *sbi,
1109 struct page *page, enum page_type type)
1110{
1111 enum page_type btype = PAGE_TYPE_OF_BIO(type);
1112 struct f2fs_bio_info *io = &sbi->write_io[btype];
Chao Yudf0f8dc2014-03-22 14:57:23 +08001113 struct bio_vec *bvec;
1114 int i;
1115
1116 down_read(&io->io_rwsem);
Jaegeuk Kimce234472014-04-02 09:04:42 +09001117 if (!io->bio)
Chao Yudf0f8dc2014-03-22 14:57:23 +08001118 goto out;
1119
Jaegeuk Kimce234472014-04-02 09:04:42 +09001120 bio_for_each_segment_all(bvec, io->bio, i) {
Chao Yudf0f8dc2014-03-22 14:57:23 +08001121 if (page == bvec->bv_page) {
1122 up_read(&io->io_rwsem);
1123 return true;
1124 }
1125 }
1126
1127out:
1128 up_read(&io->io_rwsem);
1129 return false;
1130}
1131
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001132void f2fs_wait_on_page_writeback(struct page *page,
Yuan Zhong5514f0a2014-01-10 07:26:14 +00001133 enum page_type type)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001134{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001135 if (PageWriteback(page)) {
Jaegeuk Kim40813632014-09-02 15:31:18 -07001136 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
1137
Chao Yudf0f8dc2014-03-22 14:57:23 +08001138 if (is_merged_page(sbi, page, type))
1139 f2fs_submit_merged_bio(sbi, type, WRITE);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001140 wait_on_page_writeback(page);
1141 }
1142}
1143
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001144static int read_compacted_summaries(struct f2fs_sb_info *sbi)
1145{
1146 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1147 struct curseg_info *seg_i;
1148 unsigned char *kaddr;
1149 struct page *page;
1150 block_t start;
1151 int i, j, offset;
1152
1153 start = start_sum_block(sbi);
1154
1155 page = get_meta_page(sbi, start++);
1156 kaddr = (unsigned char *)page_address(page);
1157
1158 /* Step 1: restore nat cache */
1159 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1160 memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
1161
1162 /* Step 2: restore sit cache */
1163 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1164 memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
1165 SUM_JOURNAL_SIZE);
1166 offset = 2 * SUM_JOURNAL_SIZE;
1167
1168 /* Step 3: restore summary entries */
1169 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1170 unsigned short blk_off;
1171 unsigned int segno;
1172
1173 seg_i = CURSEG_I(sbi, i);
1174 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
1175 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
1176 seg_i->next_segno = segno;
1177 reset_curseg(sbi, i, 0);
1178 seg_i->alloc_type = ckpt->alloc_type[i];
1179 seg_i->next_blkoff = blk_off;
1180
1181 if (seg_i->alloc_type == SSR)
1182 blk_off = sbi->blocks_per_seg;
1183
1184 for (j = 0; j < blk_off; j++) {
1185 struct f2fs_summary *s;
1186 s = (struct f2fs_summary *)(kaddr + offset);
1187 seg_i->sum_blk->entries[j] = *s;
1188 offset += SUMMARY_SIZE;
1189 if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1190 SUM_FOOTER_SIZE)
1191 continue;
1192
1193 f2fs_put_page(page, 1);
1194 page = NULL;
1195
1196 page = get_meta_page(sbi, start++);
1197 kaddr = (unsigned char *)page_address(page);
1198 offset = 0;
1199 }
1200 }
1201 f2fs_put_page(page, 1);
1202 return 0;
1203}
1204
1205static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
1206{
1207 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1208 struct f2fs_summary_block *sum;
1209 struct curseg_info *curseg;
1210 struct page *new;
1211 unsigned short blk_off;
1212 unsigned int segno = 0;
1213 block_t blk_addr = 0;
1214
1215 /* get segment number and block addr */
1216 if (IS_DATASEG(type)) {
1217 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
1218 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
1219 CURSEG_HOT_DATA]);
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001220 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001221 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
1222 else
1223 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
1224 } else {
1225 segno = le32_to_cpu(ckpt->cur_node_segno[type -
1226 CURSEG_HOT_NODE]);
1227 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
1228 CURSEG_HOT_NODE]);
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001229 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001230 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
1231 type - CURSEG_HOT_NODE);
1232 else
1233 blk_addr = GET_SUM_BLOCK(sbi, segno);
1234 }
1235
1236 new = get_meta_page(sbi, blk_addr);
1237 sum = (struct f2fs_summary_block *)page_address(new);
1238
1239 if (IS_NODESEG(type)) {
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001240 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001241 struct f2fs_summary *ns = &sum->entries[0];
1242 int i;
1243 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
1244 ns->version = 0;
1245 ns->ofs_in_node = 0;
1246 }
1247 } else {
Gu Zhengd6537882014-03-07 18:43:36 +08001248 int err;
1249
1250 err = restore_node_summary(sbi, segno, sum);
1251 if (err) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001252 f2fs_put_page(new, 1);
Gu Zhengd6537882014-03-07 18:43:36 +08001253 return err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001254 }
1255 }
1256 }
1257
1258 /* set uncompleted segment to curseg */
1259 curseg = CURSEG_I(sbi, type);
1260 mutex_lock(&curseg->curseg_mutex);
1261 memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
1262 curseg->next_segno = segno;
1263 reset_curseg(sbi, type, 0);
1264 curseg->alloc_type = ckpt->alloc_type[type];
1265 curseg->next_blkoff = blk_off;
1266 mutex_unlock(&curseg->curseg_mutex);
1267 f2fs_put_page(new, 1);
1268 return 0;
1269}
1270
1271static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
1272{
1273 int type = CURSEG_HOT_DATA;
Chao Yue4fc5fb2014-03-17 16:36:24 +08001274 int err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001275
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001276 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001277 /* restore for compacted data summary */
1278 if (read_compacted_summaries(sbi))
1279 return -EINVAL;
1280 type = CURSEG_HOT_NODE;
1281 }
1282
Chao Yue4fc5fb2014-03-17 16:36:24 +08001283 for (; type <= CURSEG_COLD_NODE; type++) {
1284 err = read_normal_summaries(sbi, type);
1285 if (err)
1286 return err;
1287 }
1288
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001289 return 0;
1290}
1291
1292static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
1293{
1294 struct page *page;
1295 unsigned char *kaddr;
1296 struct f2fs_summary *summary;
1297 struct curseg_info *seg_i;
1298 int written_size = 0;
1299 int i, j;
1300
1301 page = grab_meta_page(sbi, blkaddr++);
1302 kaddr = (unsigned char *)page_address(page);
1303
1304 /* Step 1: write nat cache */
1305 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1306 memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
1307 written_size += SUM_JOURNAL_SIZE;
1308
1309 /* Step 2: write sit cache */
1310 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1311 memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
1312 SUM_JOURNAL_SIZE);
1313 written_size += SUM_JOURNAL_SIZE;
1314
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001315 /* Step 3: write summary entries */
1316 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1317 unsigned short blkoff;
1318 seg_i = CURSEG_I(sbi, i);
1319 if (sbi->ckpt->alloc_type[i] == SSR)
1320 blkoff = sbi->blocks_per_seg;
1321 else
1322 blkoff = curseg_blkoff(sbi, i);
1323
1324 for (j = 0; j < blkoff; j++) {
1325 if (!page) {
1326 page = grab_meta_page(sbi, blkaddr++);
1327 kaddr = (unsigned char *)page_address(page);
1328 written_size = 0;
1329 }
1330 summary = (struct f2fs_summary *)(kaddr + written_size);
1331 *summary = seg_i->sum_blk->entries[j];
1332 written_size += SUMMARY_SIZE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001333
1334 if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1335 SUM_FOOTER_SIZE)
1336 continue;
1337
Chao Yue8d61a72013-10-24 15:08:28 +08001338 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001339 f2fs_put_page(page, 1);
1340 page = NULL;
1341 }
1342 }
Chao Yue8d61a72013-10-24 15:08:28 +08001343 if (page) {
1344 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001345 f2fs_put_page(page, 1);
Chao Yue8d61a72013-10-24 15:08:28 +08001346 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001347}
1348
1349static void write_normal_summaries(struct f2fs_sb_info *sbi,
1350 block_t blkaddr, int type)
1351{
1352 int i, end;
1353 if (IS_DATASEG(type))
1354 end = type + NR_CURSEG_DATA_TYPE;
1355 else
1356 end = type + NR_CURSEG_NODE_TYPE;
1357
1358 for (i = type; i < end; i++) {
1359 struct curseg_info *sum = CURSEG_I(sbi, i);
1360 mutex_lock(&sum->curseg_mutex);
1361 write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
1362 mutex_unlock(&sum->curseg_mutex);
1363 }
1364}
1365
1366void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1367{
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001368 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001369 write_compacted_summaries(sbi, start_blk);
1370 else
1371 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
1372}
1373
1374void write_node_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_UMOUNT_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001377 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001378}
1379
1380int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
1381 unsigned int val, int alloc)
1382{
1383 int i;
1384
1385 if (type == NAT_JOURNAL) {
1386 for (i = 0; i < nats_in_cursum(sum); i++) {
1387 if (le32_to_cpu(nid_in_journal(sum, i)) == val)
1388 return i;
1389 }
1390 if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
1391 return update_nats_in_cursum(sum, 1);
1392 } else if (type == SIT_JOURNAL) {
1393 for (i = 0; i < sits_in_cursum(sum); i++)
1394 if (le32_to_cpu(segno_in_journal(sum, i)) == val)
1395 return i;
1396 if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
1397 return update_sits_in_cursum(sum, 1);
1398 }
1399 return -1;
1400}
1401
1402static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
1403 unsigned int segno)
1404{
1405 struct sit_info *sit_i = SIT_I(sbi);
Chao Yud3a14af2014-09-04 18:11:47 +08001406 unsigned int offset = SIT_BLOCK_OFFSET(segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001407 block_t blk_addr = sit_i->sit_base_addr + offset;
1408
1409 check_seg_range(sbi, segno);
1410
1411 /* calculate sit block address */
1412 if (f2fs_test_bit(offset, sit_i->sit_bitmap))
1413 blk_addr += sit_i->sit_blocks;
1414
1415 return get_meta_page(sbi, blk_addr);
1416}
1417
1418static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
1419 unsigned int start)
1420{
1421 struct sit_info *sit_i = SIT_I(sbi);
1422 struct page *src_page, *dst_page;
1423 pgoff_t src_off, dst_off;
1424 void *src_addr, *dst_addr;
1425
1426 src_off = current_sit_addr(sbi, start);
1427 dst_off = next_sit_addr(sbi, src_off);
1428
1429 /* get current sit block page without lock */
1430 src_page = get_meta_page(sbi, src_off);
1431 dst_page = grab_meta_page(sbi, dst_off);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001432 f2fs_bug_on(sbi, PageDirty(src_page));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001433
1434 src_addr = page_address(src_page);
1435 dst_addr = page_address(dst_page);
1436 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
1437
1438 set_page_dirty(dst_page);
1439 f2fs_put_page(src_page, 1);
1440
1441 set_to_next_sit(sit_i, start);
1442
1443 return dst_page;
1444}
1445
1446static bool flush_sits_in_journal(struct f2fs_sb_info *sbi)
1447{
1448 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1449 struct f2fs_summary_block *sum = curseg->sum_blk;
1450 int i;
1451
1452 /*
1453 * If the journal area in the current summary is full of sit entries,
1454 * all the sit entries will be flushed. Otherwise the sit entries
1455 * are not able to replace with newly hot sit entries.
1456 */
1457 if (sits_in_cursum(sum) >= SIT_JOURNAL_ENTRIES) {
1458 for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
1459 unsigned int segno;
1460 segno = le32_to_cpu(segno_in_journal(sum, i));
1461 __mark_sit_entry_dirty(sbi, segno);
1462 }
1463 update_sits_in_cursum(sum, -sits_in_cursum(sum));
Haicheng Licffbfa62013-10-18 17:24:07 +08001464 return true;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001465 }
Haicheng Licffbfa62013-10-18 17:24:07 +08001466 return false;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001467}
1468
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001469/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001470 * CP calls this function, which flushes SIT entries including sit_journal,
1471 * and moves prefree segs to free segs.
1472 */
1473void flush_sit_entries(struct f2fs_sb_info *sbi)
1474{
1475 struct sit_info *sit_i = SIT_I(sbi);
1476 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
1477 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1478 struct f2fs_summary_block *sum = curseg->sum_blk;
1479 unsigned long nsegs = TOTAL_SEGS(sbi);
1480 struct page *page = NULL;
1481 struct f2fs_sit_block *raw_sit = NULL;
1482 unsigned int start = 0, end = 0;
Chao Yub65ee142014-08-04 10:10:07 +08001483 unsigned int segno;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001484 bool flushed;
1485
1486 mutex_lock(&curseg->curseg_mutex);
1487 mutex_lock(&sit_i->sentry_lock);
1488
1489 /*
1490 * "flushed" indicates whether sit entries in journal are flushed
1491 * to the SIT area or not.
1492 */
1493 flushed = flush_sits_in_journal(sbi);
1494
Chao Yub65ee142014-08-04 10:10:07 +08001495 for_each_set_bit(segno, bitmap, nsegs) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001496 struct seg_entry *se = get_seg_entry(sbi, segno);
1497 int sit_offset, offset;
1498
1499 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
1500
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001501 /* add discard candidates */
1502 if (SM_I(sbi)->nr_discards < SM_I(sbi)->max_discards)
1503 add_discard_addrs(sbi, segno, se);
1504
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001505 if (flushed)
1506 goto to_sit_page;
1507
1508 offset = lookup_journal_in_cursum(sum, SIT_JOURNAL, segno, 1);
1509 if (offset >= 0) {
1510 segno_in_journal(sum, offset) = cpu_to_le32(segno);
1511 seg_info_to_raw_sit(se, &sit_in_journal(sum, offset));
1512 goto flush_done;
1513 }
1514to_sit_page:
1515 if (!page || (start > segno) || (segno > end)) {
1516 if (page) {
1517 f2fs_put_page(page, 1);
1518 page = NULL;
1519 }
1520
Chao Yud3a14af2014-09-04 18:11:47 +08001521 start = START_SEGNO(segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001522 end = start + SIT_ENTRY_PER_BLOCK - 1;
1523
1524 /* read sit block that will be updated */
1525 page = get_next_sit_page(sbi, start);
1526 raw_sit = page_address(page);
1527 }
1528
1529 /* udpate entry in SIT block */
1530 seg_info_to_raw_sit(se, &raw_sit->entries[sit_offset]);
1531flush_done:
1532 __clear_bit(segno, bitmap);
1533 sit_i->dirty_sentries--;
1534 }
1535 mutex_unlock(&sit_i->sentry_lock);
1536 mutex_unlock(&curseg->curseg_mutex);
1537
1538 /* writeout last modified SIT block */
1539 f2fs_put_page(page, 1);
1540
1541 set_prefree_as_free_segments(sbi);
1542}
1543
1544static int build_sit_info(struct f2fs_sb_info *sbi)
1545{
1546 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1547 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1548 struct sit_info *sit_i;
1549 unsigned int sit_segs, start;
1550 char *src_bitmap, *dst_bitmap;
1551 unsigned int bitmap_size;
1552
1553 /* allocate memory for SIT information */
1554 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
1555 if (!sit_i)
1556 return -ENOMEM;
1557
1558 SM_I(sbi)->sit_info = sit_i;
1559
1560 sit_i->sentries = vzalloc(TOTAL_SEGS(sbi) * sizeof(struct seg_entry));
1561 if (!sit_i->sentries)
1562 return -ENOMEM;
1563
1564 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1565 sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1566 if (!sit_i->dirty_sentries_bitmap)
1567 return -ENOMEM;
1568
1569 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1570 sit_i->sentries[start].cur_valid_map
1571 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1572 sit_i->sentries[start].ckpt_valid_map
1573 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1574 if (!sit_i->sentries[start].cur_valid_map
1575 || !sit_i->sentries[start].ckpt_valid_map)
1576 return -ENOMEM;
1577 }
1578
1579 if (sbi->segs_per_sec > 1) {
Jaegeuk Kim53cf9522013-03-31 12:39:49 +09001580 sit_i->sec_entries = vzalloc(TOTAL_SECS(sbi) *
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001581 sizeof(struct sec_entry));
1582 if (!sit_i->sec_entries)
1583 return -ENOMEM;
1584 }
1585
1586 /* get information related with SIT */
1587 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
1588
1589 /* setup SIT bitmap from ckeckpoint pack */
1590 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
1591 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
1592
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001593 dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001594 if (!dst_bitmap)
1595 return -ENOMEM;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001596
1597 /* init SIT information */
1598 sit_i->s_ops = &default_salloc_ops;
1599
1600 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
1601 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
1602 sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
1603 sit_i->sit_bitmap = dst_bitmap;
1604 sit_i->bitmap_size = bitmap_size;
1605 sit_i->dirty_sentries = 0;
1606 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
1607 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
1608 sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
1609 mutex_init(&sit_i->sentry_lock);
1610 return 0;
1611}
1612
1613static int build_free_segmap(struct f2fs_sb_info *sbi)
1614{
1615 struct f2fs_sm_info *sm_info = SM_I(sbi);
1616 struct free_segmap_info *free_i;
1617 unsigned int bitmap_size, sec_bitmap_size;
1618
1619 /* allocate memory for free segmap information */
1620 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
1621 if (!free_i)
1622 return -ENOMEM;
1623
1624 SM_I(sbi)->free_info = free_i;
1625
1626 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1627 free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL);
1628 if (!free_i->free_segmap)
1629 return -ENOMEM;
1630
Jaegeuk Kim53cf9522013-03-31 12:39:49 +09001631 sec_bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001632 free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL);
1633 if (!free_i->free_secmap)
1634 return -ENOMEM;
1635
1636 /* set all segments as dirty temporarily */
1637 memset(free_i->free_segmap, 0xff, bitmap_size);
1638 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
1639
1640 /* init free segmap information */
1641 free_i->start_segno =
1642 (unsigned int) GET_SEGNO_FROM_SEG0(sbi, sm_info->main_blkaddr);
1643 free_i->free_segments = 0;
1644 free_i->free_sections = 0;
1645 rwlock_init(&free_i->segmap_lock);
1646 return 0;
1647}
1648
1649static int build_curseg(struct f2fs_sb_info *sbi)
1650{
Namjae Jeon1042d602012-12-01 10:56:13 +09001651 struct curseg_info *array;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001652 int i;
1653
Fabian Frederickb434bab2014-06-23 18:39:15 +02001654 array = kcalloc(NR_CURSEG_TYPE, sizeof(*array), GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001655 if (!array)
1656 return -ENOMEM;
1657
1658 SM_I(sbi)->curseg_array = array;
1659
1660 for (i = 0; i < NR_CURSEG_TYPE; i++) {
1661 mutex_init(&array[i].curseg_mutex);
1662 array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
1663 if (!array[i].sum_blk)
1664 return -ENOMEM;
1665 array[i].segno = NULL_SEGNO;
1666 array[i].next_blkoff = 0;
1667 }
1668 return restore_curseg_summaries(sbi);
1669}
1670
1671static void build_sit_entries(struct f2fs_sb_info *sbi)
1672{
1673 struct sit_info *sit_i = SIT_I(sbi);
1674 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1675 struct f2fs_summary_block *sum = curseg->sum_blk;
Chao Yu74de5932013-11-22 09:09:59 +08001676 int sit_blk_cnt = SIT_BLK_CNT(sbi);
1677 unsigned int i, start, end;
1678 unsigned int readed, start_blk = 0;
1679 int nrpages = MAX_BIO_BLOCKS(max_hw_blocks(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001680
Chao Yu74de5932013-11-22 09:09:59 +08001681 do {
Chao Yu662befd2014-02-07 16:11:53 +08001682 readed = ra_meta_pages(sbi, start_blk, nrpages, META_SIT);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001683
Chao Yu74de5932013-11-22 09:09:59 +08001684 start = start_blk * sit_i->sents_per_block;
1685 end = (start_blk + readed) * sit_i->sents_per_block;
1686
1687 for (; start < end && start < TOTAL_SEGS(sbi); start++) {
1688 struct seg_entry *se = &sit_i->sentries[start];
1689 struct f2fs_sit_block *sit_blk;
1690 struct f2fs_sit_entry sit;
1691 struct page *page;
1692
1693 mutex_lock(&curseg->curseg_mutex);
1694 for (i = 0; i < sits_in_cursum(sum); i++) {
Chris Fries6c311ec2014-01-17 14:44:39 -06001695 if (le32_to_cpu(segno_in_journal(sum, i))
1696 == start) {
Chao Yu74de5932013-11-22 09:09:59 +08001697 sit = sit_in_journal(sum, i);
1698 mutex_unlock(&curseg->curseg_mutex);
1699 goto got_it;
1700 }
1701 }
1702 mutex_unlock(&curseg->curseg_mutex);
1703
1704 page = get_current_sit_page(sbi, start);
1705 sit_blk = (struct f2fs_sit_block *)page_address(page);
1706 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
1707 f2fs_put_page(page, 1);
1708got_it:
1709 check_block_count(sbi, start, &sit);
1710 seg_info_from_raw_sit(se, &sit);
1711 if (sbi->segs_per_sec > 1) {
1712 struct sec_entry *e = get_sec_entry(sbi, start);
1713 e->valid_blocks += se->valid_blocks;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001714 }
1715 }
Chao Yu74de5932013-11-22 09:09:59 +08001716 start_blk += readed;
1717 } while (start_blk < sit_blk_cnt);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001718}
1719
1720static void init_free_segmap(struct f2fs_sb_info *sbi)
1721{
1722 unsigned int start;
1723 int type;
1724
1725 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1726 struct seg_entry *sentry = get_seg_entry(sbi, start);
1727 if (!sentry->valid_blocks)
1728 __set_free(sbi, start);
1729 }
1730
1731 /* set use the current segments */
1732 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
1733 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
1734 __set_test_and_inuse(sbi, curseg_t->segno);
1735 }
1736}
1737
1738static void init_dirty_segmap(struct f2fs_sb_info *sbi)
1739{
1740 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1741 struct free_segmap_info *free_i = FREE_I(sbi);
Namjae Jeon8736fbf2013-06-16 09:49:11 +09001742 unsigned int segno = 0, offset = 0, total_segs = TOTAL_SEGS(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001743 unsigned short valid_blocks;
1744
Namjae Jeon8736fbf2013-06-16 09:49:11 +09001745 while (1) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001746 /* find dirty segment based on free segmap */
Namjae Jeon8736fbf2013-06-16 09:49:11 +09001747 segno = find_next_inuse(free_i, total_segs, offset);
1748 if (segno >= total_segs)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001749 break;
1750 offset = segno + 1;
1751 valid_blocks = get_valid_blocks(sbi, segno, 0);
Jaegeuk Kimec325b52014-09-02 16:24:11 -07001752 if (valid_blocks == sbi->blocks_per_seg || !valid_blocks)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001753 continue;
Jaegeuk Kimec325b52014-09-02 16:24:11 -07001754 if (valid_blocks > sbi->blocks_per_seg) {
1755 f2fs_bug_on(sbi, 1);
1756 continue;
1757 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001758 mutex_lock(&dirty_i->seglist_lock);
1759 __locate_dirty_segment(sbi, segno, DIRTY);
1760 mutex_unlock(&dirty_i->seglist_lock);
1761 }
1762}
1763
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001764static int init_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001765{
1766 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001767 unsigned int bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001768
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001769 dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL);
1770 if (!dirty_i->victim_secmap)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001771 return -ENOMEM;
1772 return 0;
1773}
1774
1775static int build_dirty_segmap(struct f2fs_sb_info *sbi)
1776{
1777 struct dirty_seglist_info *dirty_i;
1778 unsigned int bitmap_size, i;
1779
1780 /* allocate memory for dirty segments list information */
1781 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
1782 if (!dirty_i)
1783 return -ENOMEM;
1784
1785 SM_I(sbi)->dirty_info = dirty_i;
1786 mutex_init(&dirty_i->seglist_lock);
1787
1788 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1789
1790 for (i = 0; i < NR_DIRTY_TYPE; i++) {
1791 dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001792 if (!dirty_i->dirty_segmap[i])
1793 return -ENOMEM;
1794 }
1795
1796 init_dirty_segmap(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001797 return init_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001798}
1799
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001800/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001801 * Update min, max modified time for cost-benefit GC algorithm
1802 */
1803static void init_min_max_mtime(struct f2fs_sb_info *sbi)
1804{
1805 struct sit_info *sit_i = SIT_I(sbi);
1806 unsigned int segno;
1807
1808 mutex_lock(&sit_i->sentry_lock);
1809
1810 sit_i->min_mtime = LLONG_MAX;
1811
1812 for (segno = 0; segno < TOTAL_SEGS(sbi); segno += sbi->segs_per_sec) {
1813 unsigned int i;
1814 unsigned long long mtime = 0;
1815
1816 for (i = 0; i < sbi->segs_per_sec; i++)
1817 mtime += get_seg_entry(sbi, segno + i)->mtime;
1818
1819 mtime = div_u64(mtime, sbi->segs_per_sec);
1820
1821 if (sit_i->min_mtime > mtime)
1822 sit_i->min_mtime = mtime;
1823 }
1824 sit_i->max_mtime = get_mtime(sbi);
1825 mutex_unlock(&sit_i->sentry_lock);
1826}
1827
1828int build_segment_manager(struct f2fs_sb_info *sbi)
1829{
1830 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1831 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Namjae Jeon1042d602012-12-01 10:56:13 +09001832 struct f2fs_sm_info *sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001833 int err;
1834
1835 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
1836 if (!sm_info)
1837 return -ENOMEM;
1838
1839 /* init sm info */
1840 sbi->sm_info = sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001841 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
1842 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
1843 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
1844 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
1845 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
1846 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
1847 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
Jaegeuk Kim58c41032014-03-19 14:17:21 +09001848 sm_info->rec_prefree_segments = sm_info->main_segments *
1849 DEF_RECLAIM_PREFREE_SEGMENTS / 100;
Jaegeuk Kim216fbd62013-11-07 13:13:42 +09001850 sm_info->ipu_policy = F2FS_IPU_DISABLE;
1851 sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001852
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09001853 INIT_LIST_HEAD(&sm_info->discard_list);
1854 sm_info->nr_discards = 0;
1855 sm_info->max_discards = 0;
1856
Gu Zhengb270ad62014-04-11 17:49:55 +08001857 if (test_opt(sbi, FLUSH_MERGE) && !f2fs_readonly(sbi->sb)) {
Gu Zheng2163d192014-04-27 14:21:33 +08001858 err = create_flush_cmd_control(sbi);
1859 if (err)
Gu Zhenga688b9d9e2014-04-27 14:21:21 +08001860 return err;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +09001861 }
1862
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001863 err = build_sit_info(sbi);
1864 if (err)
1865 return err;
1866 err = build_free_segmap(sbi);
1867 if (err)
1868 return err;
1869 err = build_curseg(sbi);
1870 if (err)
1871 return err;
1872
1873 /* reinit free segmap based on SIT */
1874 build_sit_entries(sbi);
1875
1876 init_free_segmap(sbi);
1877 err = build_dirty_segmap(sbi);
1878 if (err)
1879 return err;
1880
1881 init_min_max_mtime(sbi);
1882 return 0;
1883}
1884
1885static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
1886 enum dirty_type dirty_type)
1887{
1888 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1889
1890 mutex_lock(&dirty_i->seglist_lock);
1891 kfree(dirty_i->dirty_segmap[dirty_type]);
1892 dirty_i->nr_dirty[dirty_type] = 0;
1893 mutex_unlock(&dirty_i->seglist_lock);
1894}
1895
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001896static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001897{
1898 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001899 kfree(dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001900}
1901
1902static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
1903{
1904 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1905 int i;
1906
1907 if (!dirty_i)
1908 return;
1909
1910 /* discard pre-free/dirty segments list */
1911 for (i = 0; i < NR_DIRTY_TYPE; i++)
1912 discard_dirty_segmap(sbi, i);
1913
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001914 destroy_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001915 SM_I(sbi)->dirty_info = NULL;
1916 kfree(dirty_i);
1917}
1918
1919static void destroy_curseg(struct f2fs_sb_info *sbi)
1920{
1921 struct curseg_info *array = SM_I(sbi)->curseg_array;
1922 int i;
1923
1924 if (!array)
1925 return;
1926 SM_I(sbi)->curseg_array = NULL;
1927 for (i = 0; i < NR_CURSEG_TYPE; i++)
1928 kfree(array[i].sum_blk);
1929 kfree(array);
1930}
1931
1932static void destroy_free_segmap(struct f2fs_sb_info *sbi)
1933{
1934 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
1935 if (!free_i)
1936 return;
1937 SM_I(sbi)->free_info = NULL;
1938 kfree(free_i->free_segmap);
1939 kfree(free_i->free_secmap);
1940 kfree(free_i);
1941}
1942
1943static void destroy_sit_info(struct f2fs_sb_info *sbi)
1944{
1945 struct sit_info *sit_i = SIT_I(sbi);
1946 unsigned int start;
1947
1948 if (!sit_i)
1949 return;
1950
1951 if (sit_i->sentries) {
1952 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1953 kfree(sit_i->sentries[start].cur_valid_map);
1954 kfree(sit_i->sentries[start].ckpt_valid_map);
1955 }
1956 }
1957 vfree(sit_i->sentries);
1958 vfree(sit_i->sec_entries);
1959 kfree(sit_i->dirty_sentries_bitmap);
1960
1961 SM_I(sbi)->sit_info = NULL;
1962 kfree(sit_i->sit_bitmap);
1963 kfree(sit_i);
1964}
1965
1966void destroy_segment_manager(struct f2fs_sb_info *sbi)
1967{
1968 struct f2fs_sm_info *sm_info = SM_I(sbi);
Gu Zhenga688b9d9e2014-04-27 14:21:21 +08001969
Chao Yu3b03f722013-11-06 09:12:04 +08001970 if (!sm_info)
1971 return;
Gu Zheng2163d192014-04-27 14:21:33 +08001972 destroy_flush_cmd_control(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001973 destroy_dirty_segmap(sbi);
1974 destroy_curseg(sbi);
1975 destroy_free_segmap(sbi);
1976 destroy_sit_info(sbi);
1977 sbi->sm_info = NULL;
1978 kfree(sm_info);
1979}
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09001980
1981int __init create_segment_manager_caches(void)
1982{
1983 discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
Gu Zhenge8512d22014-03-07 18:43:28 +08001984 sizeof(struct discard_entry));
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09001985 if (!discard_entry_slab)
1986 return -ENOMEM;
1987 return 0;
1988}
1989
1990void destroy_segment_manager_caches(void)
1991{
1992 kmem_cache_destroy(discard_entry_slab);
1993}