blob: 46586d3d39042e1cc65ac17e2682f32540aef752 [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
Changman Lee4625d6a2013-10-25 17:31:57 +0900320 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
321 dirty_i->nr_dirty[t]++;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900322 }
323}
324
325static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
326 enum dirty_type dirty_type)
327{
328 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
329
330 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
331 dirty_i->nr_dirty[dirty_type]--;
332
333 if (dirty_type == DIRTY) {
Changman Lee4625d6a2013-10-25 17:31:57 +0900334 struct seg_entry *sentry = get_seg_entry(sbi, segno);
335 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900336
Changman Lee4625d6a2013-10-25 17:31:57 +0900337 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
338 dirty_i->nr_dirty[t]--;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900339
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +0900340 if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0)
341 clear_bit(GET_SECNO(sbi, segno),
342 dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900343 }
344}
345
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900346/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900347 * Should not occur error such as -ENOMEM.
348 * Adding dirty entry into seglist is not critical operation.
349 * If a given segment is one of current working segments, it won't be added.
350 */
Haicheng Li8d8451a2013-06-13 16:59:28 +0800351static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900352{
353 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
354 unsigned short valid_blocks;
355
356 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
357 return;
358
359 mutex_lock(&dirty_i->seglist_lock);
360
361 valid_blocks = get_valid_blocks(sbi, segno, 0);
362
363 if (valid_blocks == 0) {
364 __locate_dirty_segment(sbi, segno, PRE);
365 __remove_dirty_segment(sbi, segno, DIRTY);
366 } else if (valid_blocks < sbi->blocks_per_seg) {
367 __locate_dirty_segment(sbi, segno, DIRTY);
368 } else {
369 /* Recovery routine with SSR needs this */
370 __remove_dirty_segment(sbi, segno, DIRTY);
371 }
372
373 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900374}
375
Jaegeuk Kim1e87a782014-04-15 13:57:55 +0900376static int f2fs_issue_discard(struct f2fs_sb_info *sbi,
Jaegeuk Kim37208872013-11-12 16:55:17 +0900377 block_t blkstart, block_t blklen)
378{
Jaegeuk Kimf9a4e6d2013-11-28 12:44:05 +0900379 sector_t start = SECTOR_FROM_BLOCK(sbi, blkstart);
380 sector_t len = SECTOR_FROM_BLOCK(sbi, blklen);
Jaegeuk Kim1661d072013-11-12 17:01:00 +0900381 trace_f2fs_issue_discard(sbi->sb, blkstart, blklen);
Jaegeuk Kim1e87a782014-04-15 13:57:55 +0900382 return blkdev_issue_discard(sbi->sb->s_bdev, start, len, GFP_NOFS, 0);
383}
384
Jaegeuk Kimcf2271e2014-07-25 15:47:25 -0700385void discard_next_dnode(struct f2fs_sb_info *sbi, block_t blkaddr)
Jaegeuk Kim1e87a782014-04-15 13:57:55 +0900386{
Jaegeuk Kim1e87a782014-04-15 13:57:55 +0900387 if (f2fs_issue_discard(sbi, blkaddr, 1)) {
388 struct page *page = grab_meta_page(sbi, blkaddr);
389 /* zero-filled page */
390 set_page_dirty(page);
391 f2fs_put_page(page, 1);
392 }
Jaegeuk Kim37208872013-11-12 16:55:17 +0900393}
394
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900395static void add_discard_addrs(struct f2fs_sb_info *sbi,
396 unsigned int segno, struct seg_entry *se)
397{
398 struct list_head *head = &SM_I(sbi)->discard_list;
399 struct discard_entry *new;
400 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
401 int max_blocks = sbi->blocks_per_seg;
402 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
403 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
404 unsigned long dmap[entries];
405 unsigned int start = 0, end = -1;
406 int i;
407
408 if (!test_opt(sbi, DISCARD))
409 return;
410
411 /* zero block will be discarded through the prefree list */
412 if (!se->valid_blocks || se->valid_blocks == max_blocks)
413 return;
414
415 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
416 for (i = 0; i < entries; i++)
417 dmap[i] = (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
418
419 while (SM_I(sbi)->nr_discards <= SM_I(sbi)->max_discards) {
420 start = __find_rev_next_bit(dmap, max_blocks, end + 1);
421 if (start >= max_blocks)
422 break;
423
424 end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
425
426 new = f2fs_kmem_cache_alloc(discard_entry_slab, GFP_NOFS);
427 INIT_LIST_HEAD(&new->list);
428 new->blkaddr = START_BLOCK(sbi, segno) + start;
429 new->len = end - start;
430
431 list_add_tail(&new->list, head);
432 SM_I(sbi)->nr_discards += end - start;
433 }
434}
435
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900436/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900437 * Should call clear_prefree_segments after checkpoint is done.
438 */
439static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
440{
441 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Chao Yub65ee142014-08-04 10:10:07 +0800442 unsigned int segno;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900443 unsigned int total_segs = TOTAL_SEGS(sbi);
444
445 mutex_lock(&dirty_i->seglist_lock);
Chao Yub65ee142014-08-04 10:10:07 +0800446 for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], total_segs)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900447 __set_test_and_free(sbi, segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900448 mutex_unlock(&dirty_i->seglist_lock);
449}
450
451void clear_prefree_segments(struct f2fs_sb_info *sbi)
452{
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900453 struct list_head *head = &(SM_I(sbi)->discard_list);
Chao Yu2d7b8222014-03-29 11:33:17 +0800454 struct discard_entry *entry, *this;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900455 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Changman Lee29e59c12013-11-11 09:24:37 +0900456 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900457 unsigned int total_segs = TOTAL_SEGS(sbi);
Changman Lee29e59c12013-11-11 09:24:37 +0900458 unsigned int start = 0, end = -1;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900459
460 mutex_lock(&dirty_i->seglist_lock);
Changman Lee29e59c12013-11-11 09:24:37 +0900461
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900462 while (1) {
Changman Lee29e59c12013-11-11 09:24:37 +0900463 int i;
464 start = find_next_bit(prefree_map, total_segs, end + 1);
465 if (start >= total_segs)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900466 break;
Changman Lee29e59c12013-11-11 09:24:37 +0900467 end = find_next_zero_bit(prefree_map, total_segs, start + 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900468
Changman Lee29e59c12013-11-11 09:24:37 +0900469 for (i = start; i < end; i++)
470 clear_bit(i, prefree_map);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900471
Changman Lee29e59c12013-11-11 09:24:37 +0900472 dirty_i->nr_dirty[PRE] -= end - start;
473
474 if (!test_opt(sbi, DISCARD))
475 continue;
476
Jaegeuk Kim37208872013-11-12 16:55:17 +0900477 f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
478 (end - start) << sbi->log_blocks_per_seg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900479 }
480 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900481
482 /* send small discards */
Chao Yu2d7b8222014-03-29 11:33:17 +0800483 list_for_each_entry_safe(entry, this, head, list) {
Jaegeuk Kim37208872013-11-12 16:55:17 +0900484 f2fs_issue_discard(sbi, entry->blkaddr, entry->len);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900485 list_del(&entry->list);
486 SM_I(sbi)->nr_discards -= entry->len;
487 kmem_cache_free(discard_entry_slab, entry);
488 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900489}
490
491static void __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
492{
493 struct sit_info *sit_i = SIT_I(sbi);
494 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap))
495 sit_i->dirty_sentries++;
496}
497
498static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
499 unsigned int segno, int modified)
500{
501 struct seg_entry *se = get_seg_entry(sbi, segno);
502 se->type = type;
503 if (modified)
504 __mark_sit_entry_dirty(sbi, segno);
505}
506
507static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
508{
509 struct seg_entry *se;
510 unsigned int segno, offset;
511 long int new_vblocks;
512
513 segno = GET_SEGNO(sbi, blkaddr);
514
515 se = get_seg_entry(sbi, segno);
516 new_vblocks = se->valid_blocks + del;
Jaegeuk Kim491c0852014-02-04 13:01:10 +0900517 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900518
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700519 f2fs_bug_on(sbi, (new_vblocks >> (sizeof(unsigned short) << 3) ||
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900520 (new_vblocks > sbi->blocks_per_seg)));
521
522 se->valid_blocks = new_vblocks;
523 se->mtime = get_mtime(sbi);
524 SIT_I(sbi)->max_mtime = se->mtime;
525
526 /* Update valid block bitmap */
527 if (del > 0) {
528 if (f2fs_set_bit(offset, se->cur_valid_map))
Jaegeuk Kim05796762014-09-02 16:05:00 -0700529 f2fs_bug_on(sbi, 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900530 } else {
531 if (!f2fs_clear_bit(offset, se->cur_valid_map))
Jaegeuk Kim05796762014-09-02 16:05:00 -0700532 f2fs_bug_on(sbi, 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900533 }
534 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
535 se->ckpt_valid_blocks += del;
536
537 __mark_sit_entry_dirty(sbi, segno);
538
539 /* update total number of valid blocks to be written in ckpt area */
540 SIT_I(sbi)->written_valid_blocks += del;
541
542 if (sbi->segs_per_sec > 1)
543 get_sec_entry(sbi, segno)->valid_blocks += del;
544}
545
Jaegeuk Kim5e443812014-01-28 12:22:14 +0900546void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900547{
Jaegeuk Kim5e443812014-01-28 12:22:14 +0900548 update_sit_entry(sbi, new, 1);
549 if (GET_SEGNO(sbi, old) != NULL_SEGNO)
550 update_sit_entry(sbi, old, -1);
551
552 locate_dirty_segment(sbi, GET_SEGNO(sbi, old));
553 locate_dirty_segment(sbi, GET_SEGNO(sbi, new));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900554}
555
556void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
557{
558 unsigned int segno = GET_SEGNO(sbi, addr);
559 struct sit_info *sit_i = SIT_I(sbi);
560
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700561 f2fs_bug_on(sbi, addr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900562 if (addr == NEW_ADDR)
563 return;
564
565 /* add it into sit main buffer */
566 mutex_lock(&sit_i->sentry_lock);
567
568 update_sit_entry(sbi, addr, -1);
569
570 /* add it into dirty seglist */
571 locate_dirty_segment(sbi, segno);
572
573 mutex_unlock(&sit_i->sentry_lock);
574}
575
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900576/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900577 * This function should be resided under the curseg_mutex lock
578 */
579static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
Haicheng Lie79efe32013-06-13 16:59:27 +0800580 struct f2fs_summary *sum)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900581{
582 struct curseg_info *curseg = CURSEG_I(sbi, type);
583 void *addr = curseg->sum_blk;
Haicheng Lie79efe32013-06-13 16:59:27 +0800584 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900585 memcpy(addr, sum, sizeof(struct f2fs_summary));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900586}
587
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900588/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900589 * Calculate the number of current summary pages for writing
590 */
591int npages_for_summary_flush(struct f2fs_sb_info *sbi)
592{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900593 int valid_sum_count = 0;
Fan Li9a479382013-10-29 16:21:47 +0800594 int i, sum_in_page;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900595
596 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
597 if (sbi->ckpt->alloc_type[i] == SSR)
598 valid_sum_count += sbi->blocks_per_seg;
599 else
600 valid_sum_count += curseg_blkoff(sbi, i);
601 }
602
Fan Li9a479382013-10-29 16:21:47 +0800603 sum_in_page = (PAGE_CACHE_SIZE - 2 * SUM_JOURNAL_SIZE -
604 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
605 if (valid_sum_count <= sum_in_page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900606 return 1;
Fan Li9a479382013-10-29 16:21:47 +0800607 else if ((valid_sum_count - sum_in_page) <=
608 (PAGE_CACHE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900609 return 2;
610 return 3;
611}
612
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900613/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900614 * Caller should put this summary page
615 */
616struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
617{
618 return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
619}
620
621static void write_sum_page(struct f2fs_sb_info *sbi,
622 struct f2fs_summary_block *sum_blk, block_t blk_addr)
623{
624 struct page *page = grab_meta_page(sbi, blk_addr);
625 void *kaddr = page_address(page);
626 memcpy(kaddr, sum_blk, PAGE_CACHE_SIZE);
627 set_page_dirty(page);
628 f2fs_put_page(page, 1);
629}
630
Jaegeuk Kim60374682013-03-31 13:58:51 +0900631static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
632{
633 struct curseg_info *curseg = CURSEG_I(sbi, type);
Haicheng Li81fb5e82013-05-14 18:20:28 +0800634 unsigned int segno = curseg->segno + 1;
Jaegeuk Kim60374682013-03-31 13:58:51 +0900635 struct free_segmap_info *free_i = FREE_I(sbi);
636
Haicheng Li81fb5e82013-05-14 18:20:28 +0800637 if (segno < TOTAL_SEGS(sbi) && segno % sbi->segs_per_sec)
638 return !test_bit(segno, free_i->free_segmap);
Jaegeuk Kim60374682013-03-31 13:58:51 +0900639 return 0;
640}
641
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900642/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900643 * Find a new segment from the free segments bitmap to right order
644 * This function should be returned with success, otherwise BUG
645 */
646static void get_new_segment(struct f2fs_sb_info *sbi,
647 unsigned int *newseg, bool new_sec, int dir)
648{
649 struct free_segmap_info *free_i = FREE_I(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900650 unsigned int segno, secno, zoneno;
Jaegeuk Kim53cf9522013-03-31 12:39:49 +0900651 unsigned int total_zones = TOTAL_SECS(sbi) / sbi->secs_per_zone;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900652 unsigned int hint = *newseg / sbi->segs_per_sec;
653 unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
654 unsigned int left_start = hint;
655 bool init = true;
656 int go_left = 0;
657 int i;
658
659 write_lock(&free_i->segmap_lock);
660
661 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
662 segno = find_next_zero_bit(free_i->free_segmap,
663 TOTAL_SEGS(sbi), *newseg + 1);
Jaegeuk Kim33afa7f2013-03-31 12:59:53 +0900664 if (segno - *newseg < sbi->segs_per_sec -
665 (*newseg % sbi->segs_per_sec))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900666 goto got_it;
667 }
668find_other_zone:
Jaegeuk Kim53cf9522013-03-31 12:39:49 +0900669 secno = find_next_zero_bit(free_i->free_secmap, TOTAL_SECS(sbi), hint);
670 if (secno >= TOTAL_SECS(sbi)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900671 if (dir == ALLOC_RIGHT) {
672 secno = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim53cf9522013-03-31 12:39:49 +0900673 TOTAL_SECS(sbi), 0);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700674 f2fs_bug_on(sbi, secno >= TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900675 } else {
676 go_left = 1;
677 left_start = hint - 1;
678 }
679 }
680 if (go_left == 0)
681 goto skip_left;
682
683 while (test_bit(left_start, free_i->free_secmap)) {
684 if (left_start > 0) {
685 left_start--;
686 continue;
687 }
688 left_start = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim53cf9522013-03-31 12:39:49 +0900689 TOTAL_SECS(sbi), 0);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700690 f2fs_bug_on(sbi, left_start >= TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900691 break;
692 }
693 secno = left_start;
694skip_left:
695 hint = secno;
696 segno = secno * sbi->segs_per_sec;
697 zoneno = secno / sbi->secs_per_zone;
698
699 /* give up on finding another zone */
700 if (!init)
701 goto got_it;
702 if (sbi->secs_per_zone == 1)
703 goto got_it;
704 if (zoneno == old_zoneno)
705 goto got_it;
706 if (dir == ALLOC_LEFT) {
707 if (!go_left && zoneno + 1 >= total_zones)
708 goto got_it;
709 if (go_left && zoneno == 0)
710 goto got_it;
711 }
712 for (i = 0; i < NR_CURSEG_TYPE; i++)
713 if (CURSEG_I(sbi, i)->zone == zoneno)
714 break;
715
716 if (i < NR_CURSEG_TYPE) {
717 /* zone is in user, try another */
718 if (go_left)
719 hint = zoneno * sbi->secs_per_zone - 1;
720 else if (zoneno + 1 >= total_zones)
721 hint = 0;
722 else
723 hint = (zoneno + 1) * sbi->secs_per_zone;
724 init = false;
725 goto find_other_zone;
726 }
727got_it:
728 /* set it as dirty segment in free segmap */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700729 f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900730 __set_inuse(sbi, segno);
731 *newseg = segno;
732 write_unlock(&free_i->segmap_lock);
733}
734
735static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
736{
737 struct curseg_info *curseg = CURSEG_I(sbi, type);
738 struct summary_footer *sum_footer;
739
740 curseg->segno = curseg->next_segno;
741 curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
742 curseg->next_blkoff = 0;
743 curseg->next_segno = NULL_SEGNO;
744
745 sum_footer = &(curseg->sum_blk->footer);
746 memset(sum_footer, 0, sizeof(struct summary_footer));
747 if (IS_DATASEG(type))
748 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
749 if (IS_NODESEG(type))
750 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
751 __set_sit_entry_type(sbi, type, curseg->segno, modified);
752}
753
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900754/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900755 * Allocate a current working segment.
756 * This function always allocates a free segment in LFS manner.
757 */
758static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
759{
760 struct curseg_info *curseg = CURSEG_I(sbi, type);
761 unsigned int segno = curseg->segno;
762 int dir = ALLOC_LEFT;
763
764 write_sum_page(sbi, curseg->sum_blk,
Haicheng Li81fb5e82013-05-14 18:20:28 +0800765 GET_SUM_BLOCK(sbi, segno));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900766 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
767 dir = ALLOC_RIGHT;
768
769 if (test_opt(sbi, NOHEAP))
770 dir = ALLOC_RIGHT;
771
772 get_new_segment(sbi, &segno, new_sec, dir);
773 curseg->next_segno = segno;
774 reset_curseg(sbi, type, 1);
775 curseg->alloc_type = LFS;
776}
777
778static void __next_free_blkoff(struct f2fs_sb_info *sbi,
779 struct curseg_info *seg, block_t start)
780{
781 struct seg_entry *se = get_seg_entry(sbi, seg->segno);
Changman Leee81c93c2013-11-15 13:21:16 +0900782 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
783 unsigned long target_map[entries];
784 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
785 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
786 int i, pos;
787
788 for (i = 0; i < entries; i++)
789 target_map[i] = ckpt_map[i] | cur_map[i];
790
791 pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
792
793 seg->next_blkoff = pos;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900794}
795
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900796/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900797 * If a segment is written by LFS manner, next block offset is just obtained
798 * by increasing the current block offset. However, if a segment is written by
799 * SSR manner, next block offset obtained by calling __next_free_blkoff
800 */
801static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
802 struct curseg_info *seg)
803{
804 if (seg->alloc_type == SSR)
805 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
806 else
807 seg->next_blkoff++;
808}
809
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900810/*
arter97e1c42042014-08-06 23:22:50 +0900811 * This function always allocates a used segment(from dirty seglist) by SSR
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900812 * manner, so it should recover the existing segment information of valid blocks
813 */
814static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
815{
816 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
817 struct curseg_info *curseg = CURSEG_I(sbi, type);
818 unsigned int new_segno = curseg->next_segno;
819 struct f2fs_summary_block *sum_node;
820 struct page *sum_page;
821
822 write_sum_page(sbi, curseg->sum_blk,
823 GET_SUM_BLOCK(sbi, curseg->segno));
824 __set_test_and_inuse(sbi, new_segno);
825
826 mutex_lock(&dirty_i->seglist_lock);
827 __remove_dirty_segment(sbi, new_segno, PRE);
828 __remove_dirty_segment(sbi, new_segno, DIRTY);
829 mutex_unlock(&dirty_i->seglist_lock);
830
831 reset_curseg(sbi, type, 1);
832 curseg->alloc_type = SSR;
833 __next_free_blkoff(sbi, curseg, 0);
834
835 if (reuse) {
836 sum_page = get_sum_page(sbi, new_segno);
837 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
838 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
839 f2fs_put_page(sum_page, 1);
840 }
841}
842
Jaegeuk Kim43727522013-02-04 15:11:17 +0900843static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
844{
845 struct curseg_info *curseg = CURSEG_I(sbi, type);
846 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
847
848 if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0))
849 return v_ops->get_victim(sbi,
850 &(curseg)->next_segno, BG_GC, type, SSR);
851
852 /* For data segments, let's do SSR more intensively */
853 for (; type >= CURSEG_HOT_DATA; type--)
854 if (v_ops->get_victim(sbi, &(curseg)->next_segno,
855 BG_GC, type, SSR))
856 return 1;
857 return 0;
858}
859
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900860/*
861 * flush out current segment and replace it with new segment
862 * This function should be returned with success, otherwise BUG
863 */
864static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
865 int type, bool force)
866{
867 struct curseg_info *curseg = CURSEG_I(sbi, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900868
Gu Zheng7b405272013-08-19 09:41:15 +0800869 if (force)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900870 new_curseg(sbi, type, true);
Gu Zheng7b405272013-08-19 09:41:15 +0800871 else if (type == CURSEG_WARM_NODE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900872 new_curseg(sbi, type, false);
Jaegeuk Kim60374682013-03-31 13:58:51 +0900873 else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
874 new_curseg(sbi, type, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900875 else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
876 change_curseg(sbi, type, true);
877 else
878 new_curseg(sbi, type, false);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +0900879
880 stat_inc_seg_type(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900881}
882
883void allocate_new_segments(struct f2fs_sb_info *sbi)
884{
885 struct curseg_info *curseg;
886 unsigned int old_curseg;
887 int i;
888
889 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
890 curseg = CURSEG_I(sbi, i);
891 old_curseg = curseg->segno;
892 SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
893 locate_dirty_segment(sbi, old_curseg);
894 }
895}
896
897static const struct segment_allocation default_salloc_ops = {
898 .allocate_segment = allocate_segment_by_default,
899};
900
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900901static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
902{
903 struct curseg_info *curseg = CURSEG_I(sbi, type);
904 if (curseg->next_blkoff < sbi->blocks_per_seg)
905 return true;
906 return false;
907}
908
909static int __get_segment_type_2(struct page *page, enum page_type p_type)
910{
911 if (p_type == DATA)
912 return CURSEG_HOT_DATA;
913 else
914 return CURSEG_HOT_NODE;
915}
916
917static int __get_segment_type_4(struct page *page, enum page_type p_type)
918{
919 if (p_type == DATA) {
920 struct inode *inode = page->mapping->host;
921
922 if (S_ISDIR(inode->i_mode))
923 return CURSEG_HOT_DATA;
924 else
925 return CURSEG_COLD_DATA;
926 } else {
927 if (IS_DNODE(page) && !is_cold_node(page))
928 return CURSEG_HOT_NODE;
929 else
930 return CURSEG_COLD_NODE;
931 }
932}
933
934static int __get_segment_type_6(struct page *page, enum page_type p_type)
935{
936 if (p_type == DATA) {
937 struct inode *inode = page->mapping->host;
938
939 if (S_ISDIR(inode->i_mode))
940 return CURSEG_HOT_DATA;
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900941 else if (is_cold_data(page) || file_is_cold(inode))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900942 return CURSEG_COLD_DATA;
943 else
944 return CURSEG_WARM_DATA;
945 } else {
946 if (IS_DNODE(page))
947 return is_cold_node(page) ? CURSEG_WARM_NODE :
948 CURSEG_HOT_NODE;
949 else
950 return CURSEG_COLD_NODE;
951 }
952}
953
954static int __get_segment_type(struct page *page, enum page_type p_type)
955{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700956 switch (F2FS_P_SB(page)->active_logs) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900957 case 2:
958 return __get_segment_type_2(page, p_type);
959 case 4:
960 return __get_segment_type_4(page, p_type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900961 }
Jaegeuk Kim12a67142012-12-21 11:47:05 +0900962 /* NR_CURSEG_TYPE(6) logs by default */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700963 f2fs_bug_on(F2FS_P_SB(page),
964 F2FS_P_SB(page)->active_logs != NR_CURSEG_TYPE);
Jaegeuk Kim12a67142012-12-21 11:47:05 +0900965 return __get_segment_type_6(page, p_type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900966}
967
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900968void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
969 block_t old_blkaddr, block_t *new_blkaddr,
970 struct f2fs_summary *sum, int type)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900971{
972 struct sit_info *sit_i = SIT_I(sbi);
973 struct curseg_info *curseg;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900974
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900975 curseg = CURSEG_I(sbi, type);
976
977 mutex_lock(&curseg->curseg_mutex);
978
979 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900980
981 /*
982 * __add_sum_entry should be resided under the curseg_mutex
983 * because, this function updates a summary entry in the
984 * current summary block.
985 */
Haicheng Lie79efe32013-06-13 16:59:27 +0800986 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900987
988 mutex_lock(&sit_i->sentry_lock);
989 __refresh_next_blkoff(sbi, curseg);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +0900990
991 stat_inc_block_count(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900992
Jaegeuk Kim5e443812014-01-28 12:22:14 +0900993 if (!__has_curseg_space(sbi, type))
994 sit_i->s_ops->allocate_segment(sbi, type, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900995 /*
996 * SIT information should be updated before segment allocation,
997 * since SSR needs latest valid block information.
998 */
999 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
Jaegeuk Kim5e443812014-01-28 12:22:14 +09001000
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001001 mutex_unlock(&sit_i->sentry_lock);
1002
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001003 if (page && IS_NODESEG(type))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001004 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
1005
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001006 mutex_unlock(&curseg->curseg_mutex);
1007}
1008
1009static void do_write_page(struct f2fs_sb_info *sbi, struct page *page,
1010 block_t old_blkaddr, block_t *new_blkaddr,
1011 struct f2fs_summary *sum, struct f2fs_io_info *fio)
1012{
1013 int type = __get_segment_type(page, fio->type);
1014
1015 allocate_data_block(sbi, page, old_blkaddr, new_blkaddr, sum, type);
1016
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001017 /* writeout dirty page into bdev */
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001018 f2fs_submit_page_mbio(sbi, page, *new_blkaddr, fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001019}
1020
Jaegeuk Kim577e3492013-01-24 19:56:11 +09001021void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001022{
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001023 struct f2fs_io_info fio = {
1024 .type = META,
Gu Zheng7e8f2302013-12-20 18:17:49 +08001025 .rw = WRITE_SYNC | REQ_META | REQ_PRIO
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001026 };
1027
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001028 set_page_writeback(page);
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001029 f2fs_submit_page_mbio(sbi, page, page->index, &fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001030}
1031
1032void write_node_page(struct f2fs_sb_info *sbi, struct page *page,
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001033 struct f2fs_io_info *fio,
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001034 unsigned int nid, block_t old_blkaddr, block_t *new_blkaddr)
1035{
1036 struct f2fs_summary sum;
1037 set_summary(&sum, nid, 0, 0);
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001038 do_write_page(sbi, page, old_blkaddr, new_blkaddr, &sum, fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001039}
1040
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001041void write_data_page(struct page *page, struct dnode_of_data *dn,
1042 block_t *new_blkaddr, struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001043{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001044 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001045 struct f2fs_summary sum;
1046 struct node_info ni;
1047
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001048 f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001049 get_node_info(sbi, dn->nid, &ni);
1050 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1051
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001052 do_write_page(sbi, page, dn->data_blkaddr, new_blkaddr, &sum, fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001053}
1054
Chris Fries6c311ec2014-01-17 14:44:39 -06001055void rewrite_data_page(struct page *page, block_t old_blkaddr,
1056 struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001057{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001058 f2fs_submit_page_mbio(F2FS_P_SB(page), page, old_blkaddr, fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001059}
1060
1061void recover_data_page(struct f2fs_sb_info *sbi,
1062 struct page *page, struct f2fs_summary *sum,
1063 block_t old_blkaddr, block_t new_blkaddr)
1064{
1065 struct sit_info *sit_i = SIT_I(sbi);
1066 struct curseg_info *curseg;
1067 unsigned int segno, old_cursegno;
1068 struct seg_entry *se;
1069 int type;
1070
1071 segno = GET_SEGNO(sbi, new_blkaddr);
1072 se = get_seg_entry(sbi, segno);
1073 type = se->type;
1074
1075 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
1076 if (old_blkaddr == NULL_ADDR)
1077 type = CURSEG_COLD_DATA;
1078 else
1079 type = CURSEG_WARM_DATA;
1080 }
1081 curseg = CURSEG_I(sbi, type);
1082
1083 mutex_lock(&curseg->curseg_mutex);
1084 mutex_lock(&sit_i->sentry_lock);
1085
1086 old_cursegno = curseg->segno;
1087
1088 /* change the current segment */
1089 if (segno != curseg->segno) {
1090 curseg->next_segno = segno;
1091 change_curseg(sbi, type, true);
1092 }
1093
Jaegeuk Kim491c0852014-02-04 13:01:10 +09001094 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
Haicheng Lie79efe32013-06-13 16:59:27 +08001095 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001096
1097 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001098 locate_dirty_segment(sbi, old_cursegno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001099
1100 mutex_unlock(&sit_i->sentry_lock);
1101 mutex_unlock(&curseg->curseg_mutex);
1102}
1103
Chao Yudf0f8dc2014-03-22 14:57:23 +08001104static inline bool is_merged_page(struct f2fs_sb_info *sbi,
1105 struct page *page, enum page_type type)
1106{
1107 enum page_type btype = PAGE_TYPE_OF_BIO(type);
1108 struct f2fs_bio_info *io = &sbi->write_io[btype];
Chao Yudf0f8dc2014-03-22 14:57:23 +08001109 struct bio_vec *bvec;
1110 int i;
1111
1112 down_read(&io->io_rwsem);
Jaegeuk Kimce234472014-04-02 09:04:42 +09001113 if (!io->bio)
Chao Yudf0f8dc2014-03-22 14:57:23 +08001114 goto out;
1115
Jaegeuk Kimce234472014-04-02 09:04:42 +09001116 bio_for_each_segment_all(bvec, io->bio, i) {
Chao Yudf0f8dc2014-03-22 14:57:23 +08001117 if (page == bvec->bv_page) {
1118 up_read(&io->io_rwsem);
1119 return true;
1120 }
1121 }
1122
1123out:
1124 up_read(&io->io_rwsem);
1125 return false;
1126}
1127
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001128void f2fs_wait_on_page_writeback(struct page *page,
Yuan Zhong5514f0a2014-01-10 07:26:14 +00001129 enum page_type type)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001130{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001131 if (PageWriteback(page)) {
Jaegeuk Kim40813632014-09-02 15:31:18 -07001132 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
1133
Chao Yudf0f8dc2014-03-22 14:57:23 +08001134 if (is_merged_page(sbi, page, type))
1135 f2fs_submit_merged_bio(sbi, type, WRITE);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001136 wait_on_page_writeback(page);
1137 }
1138}
1139
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001140static int read_compacted_summaries(struct f2fs_sb_info *sbi)
1141{
1142 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1143 struct curseg_info *seg_i;
1144 unsigned char *kaddr;
1145 struct page *page;
1146 block_t start;
1147 int i, j, offset;
1148
1149 start = start_sum_block(sbi);
1150
1151 page = get_meta_page(sbi, start++);
1152 kaddr = (unsigned char *)page_address(page);
1153
1154 /* Step 1: restore nat cache */
1155 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1156 memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
1157
1158 /* Step 2: restore sit cache */
1159 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1160 memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
1161 SUM_JOURNAL_SIZE);
1162 offset = 2 * SUM_JOURNAL_SIZE;
1163
1164 /* Step 3: restore summary entries */
1165 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1166 unsigned short blk_off;
1167 unsigned int segno;
1168
1169 seg_i = CURSEG_I(sbi, i);
1170 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
1171 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
1172 seg_i->next_segno = segno;
1173 reset_curseg(sbi, i, 0);
1174 seg_i->alloc_type = ckpt->alloc_type[i];
1175 seg_i->next_blkoff = blk_off;
1176
1177 if (seg_i->alloc_type == SSR)
1178 blk_off = sbi->blocks_per_seg;
1179
1180 for (j = 0; j < blk_off; j++) {
1181 struct f2fs_summary *s;
1182 s = (struct f2fs_summary *)(kaddr + offset);
1183 seg_i->sum_blk->entries[j] = *s;
1184 offset += SUMMARY_SIZE;
1185 if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1186 SUM_FOOTER_SIZE)
1187 continue;
1188
1189 f2fs_put_page(page, 1);
1190 page = NULL;
1191
1192 page = get_meta_page(sbi, start++);
1193 kaddr = (unsigned char *)page_address(page);
1194 offset = 0;
1195 }
1196 }
1197 f2fs_put_page(page, 1);
1198 return 0;
1199}
1200
1201static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
1202{
1203 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1204 struct f2fs_summary_block *sum;
1205 struct curseg_info *curseg;
1206 struct page *new;
1207 unsigned short blk_off;
1208 unsigned int segno = 0;
1209 block_t blk_addr = 0;
1210
1211 /* get segment number and block addr */
1212 if (IS_DATASEG(type)) {
1213 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
1214 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
1215 CURSEG_HOT_DATA]);
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001216 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001217 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
1218 else
1219 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
1220 } else {
1221 segno = le32_to_cpu(ckpt->cur_node_segno[type -
1222 CURSEG_HOT_NODE]);
1223 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
1224 CURSEG_HOT_NODE]);
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001225 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001226 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
1227 type - CURSEG_HOT_NODE);
1228 else
1229 blk_addr = GET_SUM_BLOCK(sbi, segno);
1230 }
1231
1232 new = get_meta_page(sbi, blk_addr);
1233 sum = (struct f2fs_summary_block *)page_address(new);
1234
1235 if (IS_NODESEG(type)) {
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001236 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001237 struct f2fs_summary *ns = &sum->entries[0];
1238 int i;
1239 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
1240 ns->version = 0;
1241 ns->ofs_in_node = 0;
1242 }
1243 } else {
Gu Zhengd6537882014-03-07 18:43:36 +08001244 int err;
1245
1246 err = restore_node_summary(sbi, segno, sum);
1247 if (err) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001248 f2fs_put_page(new, 1);
Gu Zhengd6537882014-03-07 18:43:36 +08001249 return err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001250 }
1251 }
1252 }
1253
1254 /* set uncompleted segment to curseg */
1255 curseg = CURSEG_I(sbi, type);
1256 mutex_lock(&curseg->curseg_mutex);
1257 memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
1258 curseg->next_segno = segno;
1259 reset_curseg(sbi, type, 0);
1260 curseg->alloc_type = ckpt->alloc_type[type];
1261 curseg->next_blkoff = blk_off;
1262 mutex_unlock(&curseg->curseg_mutex);
1263 f2fs_put_page(new, 1);
1264 return 0;
1265}
1266
1267static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
1268{
1269 int type = CURSEG_HOT_DATA;
Chao Yue4fc5fb2014-03-17 16:36:24 +08001270 int err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001271
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001272 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001273 /* restore for compacted data summary */
1274 if (read_compacted_summaries(sbi))
1275 return -EINVAL;
1276 type = CURSEG_HOT_NODE;
1277 }
1278
Chao Yue4fc5fb2014-03-17 16:36:24 +08001279 for (; type <= CURSEG_COLD_NODE; type++) {
1280 err = read_normal_summaries(sbi, type);
1281 if (err)
1282 return err;
1283 }
1284
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001285 return 0;
1286}
1287
1288static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
1289{
1290 struct page *page;
1291 unsigned char *kaddr;
1292 struct f2fs_summary *summary;
1293 struct curseg_info *seg_i;
1294 int written_size = 0;
1295 int i, j;
1296
1297 page = grab_meta_page(sbi, blkaddr++);
1298 kaddr = (unsigned char *)page_address(page);
1299
1300 /* Step 1: write nat cache */
1301 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1302 memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
1303 written_size += SUM_JOURNAL_SIZE;
1304
1305 /* Step 2: write sit cache */
1306 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1307 memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
1308 SUM_JOURNAL_SIZE);
1309 written_size += SUM_JOURNAL_SIZE;
1310
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001311 /* Step 3: write summary entries */
1312 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1313 unsigned short blkoff;
1314 seg_i = CURSEG_I(sbi, i);
1315 if (sbi->ckpt->alloc_type[i] == SSR)
1316 blkoff = sbi->blocks_per_seg;
1317 else
1318 blkoff = curseg_blkoff(sbi, i);
1319
1320 for (j = 0; j < blkoff; j++) {
1321 if (!page) {
1322 page = grab_meta_page(sbi, blkaddr++);
1323 kaddr = (unsigned char *)page_address(page);
1324 written_size = 0;
1325 }
1326 summary = (struct f2fs_summary *)(kaddr + written_size);
1327 *summary = seg_i->sum_blk->entries[j];
1328 written_size += SUMMARY_SIZE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001329
1330 if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1331 SUM_FOOTER_SIZE)
1332 continue;
1333
Chao Yue8d61a72013-10-24 15:08:28 +08001334 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001335 f2fs_put_page(page, 1);
1336 page = NULL;
1337 }
1338 }
Chao Yue8d61a72013-10-24 15:08:28 +08001339 if (page) {
1340 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001341 f2fs_put_page(page, 1);
Chao Yue8d61a72013-10-24 15:08:28 +08001342 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001343}
1344
1345static void write_normal_summaries(struct f2fs_sb_info *sbi,
1346 block_t blkaddr, int type)
1347{
1348 int i, end;
1349 if (IS_DATASEG(type))
1350 end = type + NR_CURSEG_DATA_TYPE;
1351 else
1352 end = type + NR_CURSEG_NODE_TYPE;
1353
1354 for (i = type; i < end; i++) {
1355 struct curseg_info *sum = CURSEG_I(sbi, i);
1356 mutex_lock(&sum->curseg_mutex);
1357 write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
1358 mutex_unlock(&sum->curseg_mutex);
1359 }
1360}
1361
1362void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1363{
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001364 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001365 write_compacted_summaries(sbi, start_blk);
1366 else
1367 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
1368}
1369
1370void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1371{
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001372 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001373 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001374}
1375
1376int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
1377 unsigned int val, int alloc)
1378{
1379 int i;
1380
1381 if (type == NAT_JOURNAL) {
1382 for (i = 0; i < nats_in_cursum(sum); i++) {
1383 if (le32_to_cpu(nid_in_journal(sum, i)) == val)
1384 return i;
1385 }
1386 if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
1387 return update_nats_in_cursum(sum, 1);
1388 } else if (type == SIT_JOURNAL) {
1389 for (i = 0; i < sits_in_cursum(sum); i++)
1390 if (le32_to_cpu(segno_in_journal(sum, i)) == val)
1391 return i;
1392 if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
1393 return update_sits_in_cursum(sum, 1);
1394 }
1395 return -1;
1396}
1397
1398static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
1399 unsigned int segno)
1400{
1401 struct sit_info *sit_i = SIT_I(sbi);
1402 unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno);
1403 block_t blk_addr = sit_i->sit_base_addr + offset;
1404
1405 check_seg_range(sbi, segno);
1406
1407 /* calculate sit block address */
1408 if (f2fs_test_bit(offset, sit_i->sit_bitmap))
1409 blk_addr += sit_i->sit_blocks;
1410
1411 return get_meta_page(sbi, blk_addr);
1412}
1413
1414static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
1415 unsigned int start)
1416{
1417 struct sit_info *sit_i = SIT_I(sbi);
1418 struct page *src_page, *dst_page;
1419 pgoff_t src_off, dst_off;
1420 void *src_addr, *dst_addr;
1421
1422 src_off = current_sit_addr(sbi, start);
1423 dst_off = next_sit_addr(sbi, src_off);
1424
1425 /* get current sit block page without lock */
1426 src_page = get_meta_page(sbi, src_off);
1427 dst_page = grab_meta_page(sbi, dst_off);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001428 f2fs_bug_on(sbi, PageDirty(src_page));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001429
1430 src_addr = page_address(src_page);
1431 dst_addr = page_address(dst_page);
1432 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
1433
1434 set_page_dirty(dst_page);
1435 f2fs_put_page(src_page, 1);
1436
1437 set_to_next_sit(sit_i, start);
1438
1439 return dst_page;
1440}
1441
1442static bool flush_sits_in_journal(struct f2fs_sb_info *sbi)
1443{
1444 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1445 struct f2fs_summary_block *sum = curseg->sum_blk;
1446 int i;
1447
1448 /*
1449 * If the journal area in the current summary is full of sit entries,
1450 * all the sit entries will be flushed. Otherwise the sit entries
1451 * are not able to replace with newly hot sit entries.
1452 */
1453 if (sits_in_cursum(sum) >= SIT_JOURNAL_ENTRIES) {
1454 for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
1455 unsigned int segno;
1456 segno = le32_to_cpu(segno_in_journal(sum, i));
1457 __mark_sit_entry_dirty(sbi, segno);
1458 }
1459 update_sits_in_cursum(sum, -sits_in_cursum(sum));
Haicheng Licffbfa62013-10-18 17:24:07 +08001460 return true;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001461 }
Haicheng Licffbfa62013-10-18 17:24:07 +08001462 return false;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001463}
1464
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001465/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001466 * CP calls this function, which flushes SIT entries including sit_journal,
1467 * and moves prefree segs to free segs.
1468 */
1469void flush_sit_entries(struct f2fs_sb_info *sbi)
1470{
1471 struct sit_info *sit_i = SIT_I(sbi);
1472 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
1473 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1474 struct f2fs_summary_block *sum = curseg->sum_blk;
1475 unsigned long nsegs = TOTAL_SEGS(sbi);
1476 struct page *page = NULL;
1477 struct f2fs_sit_block *raw_sit = NULL;
1478 unsigned int start = 0, end = 0;
Chao Yub65ee142014-08-04 10:10:07 +08001479 unsigned int segno;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001480 bool flushed;
1481
1482 mutex_lock(&curseg->curseg_mutex);
1483 mutex_lock(&sit_i->sentry_lock);
1484
1485 /*
1486 * "flushed" indicates whether sit entries in journal are flushed
1487 * to the SIT area or not.
1488 */
1489 flushed = flush_sits_in_journal(sbi);
1490
Chao Yub65ee142014-08-04 10:10:07 +08001491 for_each_set_bit(segno, bitmap, nsegs) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001492 struct seg_entry *se = get_seg_entry(sbi, segno);
1493 int sit_offset, offset;
1494
1495 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
1496
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001497 /* add discard candidates */
1498 if (SM_I(sbi)->nr_discards < SM_I(sbi)->max_discards)
1499 add_discard_addrs(sbi, segno, se);
1500
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001501 if (flushed)
1502 goto to_sit_page;
1503
1504 offset = lookup_journal_in_cursum(sum, SIT_JOURNAL, segno, 1);
1505 if (offset >= 0) {
1506 segno_in_journal(sum, offset) = cpu_to_le32(segno);
1507 seg_info_to_raw_sit(se, &sit_in_journal(sum, offset));
1508 goto flush_done;
1509 }
1510to_sit_page:
1511 if (!page || (start > segno) || (segno > end)) {
1512 if (page) {
1513 f2fs_put_page(page, 1);
1514 page = NULL;
1515 }
1516
1517 start = START_SEGNO(sit_i, segno);
1518 end = start + SIT_ENTRY_PER_BLOCK - 1;
1519
1520 /* read sit block that will be updated */
1521 page = get_next_sit_page(sbi, start);
1522 raw_sit = page_address(page);
1523 }
1524
1525 /* udpate entry in SIT block */
1526 seg_info_to_raw_sit(se, &raw_sit->entries[sit_offset]);
1527flush_done:
1528 __clear_bit(segno, bitmap);
1529 sit_i->dirty_sentries--;
1530 }
1531 mutex_unlock(&sit_i->sentry_lock);
1532 mutex_unlock(&curseg->curseg_mutex);
1533
1534 /* writeout last modified SIT block */
1535 f2fs_put_page(page, 1);
1536
1537 set_prefree_as_free_segments(sbi);
1538}
1539
1540static int build_sit_info(struct f2fs_sb_info *sbi)
1541{
1542 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1543 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1544 struct sit_info *sit_i;
1545 unsigned int sit_segs, start;
1546 char *src_bitmap, *dst_bitmap;
1547 unsigned int bitmap_size;
1548
1549 /* allocate memory for SIT information */
1550 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
1551 if (!sit_i)
1552 return -ENOMEM;
1553
1554 SM_I(sbi)->sit_info = sit_i;
1555
1556 sit_i->sentries = vzalloc(TOTAL_SEGS(sbi) * sizeof(struct seg_entry));
1557 if (!sit_i->sentries)
1558 return -ENOMEM;
1559
1560 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1561 sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1562 if (!sit_i->dirty_sentries_bitmap)
1563 return -ENOMEM;
1564
1565 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1566 sit_i->sentries[start].cur_valid_map
1567 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1568 sit_i->sentries[start].ckpt_valid_map
1569 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1570 if (!sit_i->sentries[start].cur_valid_map
1571 || !sit_i->sentries[start].ckpt_valid_map)
1572 return -ENOMEM;
1573 }
1574
1575 if (sbi->segs_per_sec > 1) {
Jaegeuk Kim53cf9522013-03-31 12:39:49 +09001576 sit_i->sec_entries = vzalloc(TOTAL_SECS(sbi) *
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001577 sizeof(struct sec_entry));
1578 if (!sit_i->sec_entries)
1579 return -ENOMEM;
1580 }
1581
1582 /* get information related with SIT */
1583 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
1584
1585 /* setup SIT bitmap from ckeckpoint pack */
1586 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
1587 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
1588
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001589 dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001590 if (!dst_bitmap)
1591 return -ENOMEM;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001592
1593 /* init SIT information */
1594 sit_i->s_ops = &default_salloc_ops;
1595
1596 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
1597 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
1598 sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
1599 sit_i->sit_bitmap = dst_bitmap;
1600 sit_i->bitmap_size = bitmap_size;
1601 sit_i->dirty_sentries = 0;
1602 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
1603 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
1604 sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
1605 mutex_init(&sit_i->sentry_lock);
1606 return 0;
1607}
1608
1609static int build_free_segmap(struct f2fs_sb_info *sbi)
1610{
1611 struct f2fs_sm_info *sm_info = SM_I(sbi);
1612 struct free_segmap_info *free_i;
1613 unsigned int bitmap_size, sec_bitmap_size;
1614
1615 /* allocate memory for free segmap information */
1616 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
1617 if (!free_i)
1618 return -ENOMEM;
1619
1620 SM_I(sbi)->free_info = free_i;
1621
1622 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1623 free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL);
1624 if (!free_i->free_segmap)
1625 return -ENOMEM;
1626
Jaegeuk Kim53cf9522013-03-31 12:39:49 +09001627 sec_bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001628 free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL);
1629 if (!free_i->free_secmap)
1630 return -ENOMEM;
1631
1632 /* set all segments as dirty temporarily */
1633 memset(free_i->free_segmap, 0xff, bitmap_size);
1634 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
1635
1636 /* init free segmap information */
1637 free_i->start_segno =
1638 (unsigned int) GET_SEGNO_FROM_SEG0(sbi, sm_info->main_blkaddr);
1639 free_i->free_segments = 0;
1640 free_i->free_sections = 0;
1641 rwlock_init(&free_i->segmap_lock);
1642 return 0;
1643}
1644
1645static int build_curseg(struct f2fs_sb_info *sbi)
1646{
Namjae Jeon1042d602012-12-01 10:56:13 +09001647 struct curseg_info *array;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001648 int i;
1649
Fabian Frederickb434bab2014-06-23 18:39:15 +02001650 array = kcalloc(NR_CURSEG_TYPE, sizeof(*array), GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001651 if (!array)
1652 return -ENOMEM;
1653
1654 SM_I(sbi)->curseg_array = array;
1655
1656 for (i = 0; i < NR_CURSEG_TYPE; i++) {
1657 mutex_init(&array[i].curseg_mutex);
1658 array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
1659 if (!array[i].sum_blk)
1660 return -ENOMEM;
1661 array[i].segno = NULL_SEGNO;
1662 array[i].next_blkoff = 0;
1663 }
1664 return restore_curseg_summaries(sbi);
1665}
1666
1667static void build_sit_entries(struct f2fs_sb_info *sbi)
1668{
1669 struct sit_info *sit_i = SIT_I(sbi);
1670 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1671 struct f2fs_summary_block *sum = curseg->sum_blk;
Chao Yu74de5932013-11-22 09:09:59 +08001672 int sit_blk_cnt = SIT_BLK_CNT(sbi);
1673 unsigned int i, start, end;
1674 unsigned int readed, start_blk = 0;
1675 int nrpages = MAX_BIO_BLOCKS(max_hw_blocks(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001676
Chao Yu74de5932013-11-22 09:09:59 +08001677 do {
Chao Yu662befd2014-02-07 16:11:53 +08001678 readed = ra_meta_pages(sbi, start_blk, nrpages, META_SIT);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001679
Chao Yu74de5932013-11-22 09:09:59 +08001680 start = start_blk * sit_i->sents_per_block;
1681 end = (start_blk + readed) * sit_i->sents_per_block;
1682
1683 for (; start < end && start < TOTAL_SEGS(sbi); start++) {
1684 struct seg_entry *se = &sit_i->sentries[start];
1685 struct f2fs_sit_block *sit_blk;
1686 struct f2fs_sit_entry sit;
1687 struct page *page;
1688
1689 mutex_lock(&curseg->curseg_mutex);
1690 for (i = 0; i < sits_in_cursum(sum); i++) {
Chris Fries6c311ec2014-01-17 14:44:39 -06001691 if (le32_to_cpu(segno_in_journal(sum, i))
1692 == start) {
Chao Yu74de5932013-11-22 09:09:59 +08001693 sit = sit_in_journal(sum, i);
1694 mutex_unlock(&curseg->curseg_mutex);
1695 goto got_it;
1696 }
1697 }
1698 mutex_unlock(&curseg->curseg_mutex);
1699
1700 page = get_current_sit_page(sbi, start);
1701 sit_blk = (struct f2fs_sit_block *)page_address(page);
1702 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
1703 f2fs_put_page(page, 1);
1704got_it:
1705 check_block_count(sbi, start, &sit);
1706 seg_info_from_raw_sit(se, &sit);
1707 if (sbi->segs_per_sec > 1) {
1708 struct sec_entry *e = get_sec_entry(sbi, start);
1709 e->valid_blocks += se->valid_blocks;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001710 }
1711 }
Chao Yu74de5932013-11-22 09:09:59 +08001712 start_blk += readed;
1713 } while (start_blk < sit_blk_cnt);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001714}
1715
1716static void init_free_segmap(struct f2fs_sb_info *sbi)
1717{
1718 unsigned int start;
1719 int type;
1720
1721 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1722 struct seg_entry *sentry = get_seg_entry(sbi, start);
1723 if (!sentry->valid_blocks)
1724 __set_free(sbi, start);
1725 }
1726
1727 /* set use the current segments */
1728 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
1729 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
1730 __set_test_and_inuse(sbi, curseg_t->segno);
1731 }
1732}
1733
1734static void init_dirty_segmap(struct f2fs_sb_info *sbi)
1735{
1736 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1737 struct free_segmap_info *free_i = FREE_I(sbi);
Namjae Jeon8736fbf2013-06-16 09:49:11 +09001738 unsigned int segno = 0, offset = 0, total_segs = TOTAL_SEGS(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001739 unsigned short valid_blocks;
1740
Namjae Jeon8736fbf2013-06-16 09:49:11 +09001741 while (1) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001742 /* find dirty segment based on free segmap */
Namjae Jeon8736fbf2013-06-16 09:49:11 +09001743 segno = find_next_inuse(free_i, total_segs, offset);
1744 if (segno >= total_segs)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001745 break;
1746 offset = segno + 1;
1747 valid_blocks = get_valid_blocks(sbi, segno, 0);
1748 if (valid_blocks >= sbi->blocks_per_seg || !valid_blocks)
1749 continue;
1750 mutex_lock(&dirty_i->seglist_lock);
1751 __locate_dirty_segment(sbi, segno, DIRTY);
1752 mutex_unlock(&dirty_i->seglist_lock);
1753 }
1754}
1755
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001756static int init_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001757{
1758 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001759 unsigned int bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001760
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001761 dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL);
1762 if (!dirty_i->victim_secmap)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001763 return -ENOMEM;
1764 return 0;
1765}
1766
1767static int build_dirty_segmap(struct f2fs_sb_info *sbi)
1768{
1769 struct dirty_seglist_info *dirty_i;
1770 unsigned int bitmap_size, i;
1771
1772 /* allocate memory for dirty segments list information */
1773 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
1774 if (!dirty_i)
1775 return -ENOMEM;
1776
1777 SM_I(sbi)->dirty_info = dirty_i;
1778 mutex_init(&dirty_i->seglist_lock);
1779
1780 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1781
1782 for (i = 0; i < NR_DIRTY_TYPE; i++) {
1783 dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001784 if (!dirty_i->dirty_segmap[i])
1785 return -ENOMEM;
1786 }
1787
1788 init_dirty_segmap(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001789 return init_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001790}
1791
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001792/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001793 * Update min, max modified time for cost-benefit GC algorithm
1794 */
1795static void init_min_max_mtime(struct f2fs_sb_info *sbi)
1796{
1797 struct sit_info *sit_i = SIT_I(sbi);
1798 unsigned int segno;
1799
1800 mutex_lock(&sit_i->sentry_lock);
1801
1802 sit_i->min_mtime = LLONG_MAX;
1803
1804 for (segno = 0; segno < TOTAL_SEGS(sbi); segno += sbi->segs_per_sec) {
1805 unsigned int i;
1806 unsigned long long mtime = 0;
1807
1808 for (i = 0; i < sbi->segs_per_sec; i++)
1809 mtime += get_seg_entry(sbi, segno + i)->mtime;
1810
1811 mtime = div_u64(mtime, sbi->segs_per_sec);
1812
1813 if (sit_i->min_mtime > mtime)
1814 sit_i->min_mtime = mtime;
1815 }
1816 sit_i->max_mtime = get_mtime(sbi);
1817 mutex_unlock(&sit_i->sentry_lock);
1818}
1819
1820int build_segment_manager(struct f2fs_sb_info *sbi)
1821{
1822 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1823 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Namjae Jeon1042d602012-12-01 10:56:13 +09001824 struct f2fs_sm_info *sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001825 int err;
1826
1827 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
1828 if (!sm_info)
1829 return -ENOMEM;
1830
1831 /* init sm info */
1832 sbi->sm_info = sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001833 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
1834 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
1835 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
1836 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
1837 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
1838 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
1839 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
Jaegeuk Kim58c41032014-03-19 14:17:21 +09001840 sm_info->rec_prefree_segments = sm_info->main_segments *
1841 DEF_RECLAIM_PREFREE_SEGMENTS / 100;
Jaegeuk Kim216fbd62013-11-07 13:13:42 +09001842 sm_info->ipu_policy = F2FS_IPU_DISABLE;
1843 sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001844
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09001845 INIT_LIST_HEAD(&sm_info->discard_list);
1846 sm_info->nr_discards = 0;
1847 sm_info->max_discards = 0;
1848
Gu Zhengb270ad62014-04-11 17:49:55 +08001849 if (test_opt(sbi, FLUSH_MERGE) && !f2fs_readonly(sbi->sb)) {
Gu Zheng2163d192014-04-27 14:21:33 +08001850 err = create_flush_cmd_control(sbi);
1851 if (err)
Gu Zhenga688b9d9e2014-04-27 14:21:21 +08001852 return err;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +09001853 }
1854
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001855 err = build_sit_info(sbi);
1856 if (err)
1857 return err;
1858 err = build_free_segmap(sbi);
1859 if (err)
1860 return err;
1861 err = build_curseg(sbi);
1862 if (err)
1863 return err;
1864
1865 /* reinit free segmap based on SIT */
1866 build_sit_entries(sbi);
1867
1868 init_free_segmap(sbi);
1869 err = build_dirty_segmap(sbi);
1870 if (err)
1871 return err;
1872
1873 init_min_max_mtime(sbi);
1874 return 0;
1875}
1876
1877static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
1878 enum dirty_type dirty_type)
1879{
1880 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1881
1882 mutex_lock(&dirty_i->seglist_lock);
1883 kfree(dirty_i->dirty_segmap[dirty_type]);
1884 dirty_i->nr_dirty[dirty_type] = 0;
1885 mutex_unlock(&dirty_i->seglist_lock);
1886}
1887
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001888static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001889{
1890 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001891 kfree(dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001892}
1893
1894static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
1895{
1896 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1897 int i;
1898
1899 if (!dirty_i)
1900 return;
1901
1902 /* discard pre-free/dirty segments list */
1903 for (i = 0; i < NR_DIRTY_TYPE; i++)
1904 discard_dirty_segmap(sbi, i);
1905
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001906 destroy_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001907 SM_I(sbi)->dirty_info = NULL;
1908 kfree(dirty_i);
1909}
1910
1911static void destroy_curseg(struct f2fs_sb_info *sbi)
1912{
1913 struct curseg_info *array = SM_I(sbi)->curseg_array;
1914 int i;
1915
1916 if (!array)
1917 return;
1918 SM_I(sbi)->curseg_array = NULL;
1919 for (i = 0; i < NR_CURSEG_TYPE; i++)
1920 kfree(array[i].sum_blk);
1921 kfree(array);
1922}
1923
1924static void destroy_free_segmap(struct f2fs_sb_info *sbi)
1925{
1926 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
1927 if (!free_i)
1928 return;
1929 SM_I(sbi)->free_info = NULL;
1930 kfree(free_i->free_segmap);
1931 kfree(free_i->free_secmap);
1932 kfree(free_i);
1933}
1934
1935static void destroy_sit_info(struct f2fs_sb_info *sbi)
1936{
1937 struct sit_info *sit_i = SIT_I(sbi);
1938 unsigned int start;
1939
1940 if (!sit_i)
1941 return;
1942
1943 if (sit_i->sentries) {
1944 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1945 kfree(sit_i->sentries[start].cur_valid_map);
1946 kfree(sit_i->sentries[start].ckpt_valid_map);
1947 }
1948 }
1949 vfree(sit_i->sentries);
1950 vfree(sit_i->sec_entries);
1951 kfree(sit_i->dirty_sentries_bitmap);
1952
1953 SM_I(sbi)->sit_info = NULL;
1954 kfree(sit_i->sit_bitmap);
1955 kfree(sit_i);
1956}
1957
1958void destroy_segment_manager(struct f2fs_sb_info *sbi)
1959{
1960 struct f2fs_sm_info *sm_info = SM_I(sbi);
Gu Zhenga688b9d9e2014-04-27 14:21:21 +08001961
Chao Yu3b03f722013-11-06 09:12:04 +08001962 if (!sm_info)
1963 return;
Gu Zheng2163d192014-04-27 14:21:33 +08001964 destroy_flush_cmd_control(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001965 destroy_dirty_segmap(sbi);
1966 destroy_curseg(sbi);
1967 destroy_free_segmap(sbi);
1968 destroy_sit_info(sbi);
1969 sbi->sm_info = NULL;
1970 kfree(sm_info);
1971}
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09001972
1973int __init create_segment_manager_caches(void)
1974{
1975 discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
Gu Zhenge8512d22014-03-07 18:43:28 +08001976 sizeof(struct discard_entry));
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09001977 if (!discard_entry_slab)
1978 return -ENOMEM;
1979 return 0;
1980}
1981
1982void destroy_segment_manager_caches(void)
1983{
1984 kmem_cache_destroy(discard_entry_slab);
1985}