blob: 4ea53aab786d984da2a9fd4e8bdd2ea283394c4c [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;
Chao Yu184a5cd2014-09-04 18:13:01 +080028static struct kmem_cache *sit_entry_set_slab;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +090029
Changman Lee9a7f1432013-11-15 10:42:51 +090030/*
31 * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since
32 * MSB and LSB are reversed in a byte by f2fs_set_bit.
33 */
34static inline unsigned long __reverse_ffs(unsigned long word)
35{
36 int num = 0;
37
38#if BITS_PER_LONG == 64
39 if ((word & 0xffffffff) == 0) {
40 num += 32;
41 word >>= 32;
42 }
43#endif
44 if ((word & 0xffff) == 0) {
45 num += 16;
46 word >>= 16;
47 }
48 if ((word & 0xff) == 0) {
49 num += 8;
50 word >>= 8;
51 }
52 if ((word & 0xf0) == 0)
53 num += 4;
54 else
55 word >>= 4;
56 if ((word & 0xc) == 0)
57 num += 2;
58 else
59 word >>= 2;
60 if ((word & 0x2) == 0)
61 num += 1;
62 return num;
63}
64
65/*
arter97e1c42042014-08-06 23:22:50 +090066 * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c because
Changman Lee9a7f1432013-11-15 10:42:51 +090067 * f2fs_set_bit makes MSB and LSB reversed in a byte.
68 * Example:
69 * LSB <--> MSB
70 * f2fs_set_bit(0, bitmap) => 0000 0001
71 * f2fs_set_bit(7, bitmap) => 1000 0000
72 */
73static unsigned long __find_rev_next_bit(const unsigned long *addr,
74 unsigned long size, unsigned long offset)
75{
76 const unsigned long *p = addr + BIT_WORD(offset);
77 unsigned long result = offset & ~(BITS_PER_LONG - 1);
78 unsigned long tmp;
79 unsigned long mask, submask;
80 unsigned long quot, rest;
81
82 if (offset >= size)
83 return size;
84
85 size -= result;
86 offset %= BITS_PER_LONG;
87 if (!offset)
88 goto aligned;
89
90 tmp = *(p++);
91 quot = (offset >> 3) << 3;
92 rest = offset & 0x7;
93 mask = ~0UL << quot;
94 submask = (unsigned char)(0xff << rest) >> rest;
95 submask <<= quot;
96 mask &= submask;
97 tmp &= mask;
98 if (size < BITS_PER_LONG)
99 goto found_first;
100 if (tmp)
101 goto found_middle;
102
103 size -= BITS_PER_LONG;
104 result += BITS_PER_LONG;
105aligned:
106 while (size & ~(BITS_PER_LONG-1)) {
107 tmp = *(p++);
108 if (tmp)
109 goto found_middle;
110 result += BITS_PER_LONG;
111 size -= BITS_PER_LONG;
112 }
113 if (!size)
114 return result;
115 tmp = *p;
116found_first:
117 tmp &= (~0UL >> (BITS_PER_LONG - size));
118 if (tmp == 0UL) /* Are any bits set? */
119 return result + size; /* Nope. */
120found_middle:
121 return result + __reverse_ffs(tmp);
122}
123
124static unsigned long __find_rev_next_zero_bit(const unsigned long *addr,
125 unsigned long size, unsigned long offset)
126{
127 const unsigned long *p = addr + BIT_WORD(offset);
128 unsigned long result = offset & ~(BITS_PER_LONG - 1);
129 unsigned long tmp;
130 unsigned long mask, submask;
131 unsigned long quot, rest;
132
133 if (offset >= size)
134 return size;
135
136 size -= result;
137 offset %= BITS_PER_LONG;
138 if (!offset)
139 goto aligned;
140
141 tmp = *(p++);
142 quot = (offset >> 3) << 3;
143 rest = offset & 0x7;
144 mask = ~(~0UL << quot);
145 submask = (unsigned char)~((unsigned char)(0xff << rest) >> rest);
146 submask <<= quot;
147 mask += submask;
148 tmp |= mask;
149 if (size < BITS_PER_LONG)
150 goto found_first;
151 if (~tmp)
152 goto found_middle;
153
154 size -= BITS_PER_LONG;
155 result += BITS_PER_LONG;
156aligned:
157 while (size & ~(BITS_PER_LONG - 1)) {
158 tmp = *(p++);
159 if (~tmp)
160 goto found_middle;
161 result += BITS_PER_LONG;
162 size -= BITS_PER_LONG;
163 }
164 if (!size)
165 return result;
166 tmp = *p;
167
168found_first:
169 tmp |= ~0UL << size;
170 if (tmp == ~0UL) /* Are any bits zero? */
171 return result + size; /* Nope. */
172found_middle:
173 return result + __reverse_ffz(tmp);
174}
175
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900176/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900177 * This function balances dirty node and dentry pages.
178 * In addition, it controls garbage collection.
179 */
180void f2fs_balance_fs(struct f2fs_sb_info *sbi)
181{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900182 /*
Jaegeuk Kim029cd282012-12-21 17:20:21 +0900183 * We should do GC or end up with checkpoint, if there are so many dirty
184 * dir/node pages without enough free segments.
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900185 */
Jaegeuk Kim43727522013-02-04 15:11:17 +0900186 if (has_not_enough_free_secs(sbi, 0)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900187 mutex_lock(&sbi->gc_mutex);
Jaegeuk Kim408e9372013-01-03 17:55:52 +0900188 f2fs_gc(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900189 }
190}
191
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900192void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi)
193{
194 /* check the # of cached NAT entries and prefree segments */
195 if (try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK) ||
196 excess_prefree_segs(sbi))
197 f2fs_sync_fs(sbi->sb, true);
198}
199
Gu Zheng2163d192014-04-27 14:21:33 +0800200static int issue_flush_thread(void *data)
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900201{
202 struct f2fs_sb_info *sbi = data;
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800203 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
204 wait_queue_head_t *q = &fcc->flush_wait_queue;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900205repeat:
206 if (kthread_should_stop())
207 return 0;
208
Gu Zheng721bd4d2014-09-05 18:31:00 +0800209 if (!llist_empty(&fcc->issue_list)) {
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900210 struct bio *bio = bio_alloc(GFP_NOIO, 0);
211 struct flush_cmd *cmd, *next;
212 int ret;
213
Gu Zheng721bd4d2014-09-05 18:31:00 +0800214 fcc->dispatch_list = llist_del_all(&fcc->issue_list);
215 fcc->dispatch_list = llist_reverse_order(fcc->dispatch_list);
216
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900217 bio->bi_bdev = sbi->sb->s_bdev;
218 ret = submit_bio_wait(WRITE_FLUSH, bio);
219
Gu Zheng721bd4d2014-09-05 18:31:00 +0800220 llist_for_each_entry_safe(cmd, next,
221 fcc->dispatch_list, llnode) {
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900222 cmd->ret = ret;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900223 complete(&cmd->wait);
224 }
Gu Zhenga4ed23f2014-04-11 17:49:35 +0800225 bio_put(bio);
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800226 fcc->dispatch_list = NULL;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900227 }
228
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800229 wait_event_interruptible(*q,
Gu Zheng721bd4d2014-09-05 18:31:00 +0800230 kthread_should_stop() || !llist_empty(&fcc->issue_list));
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900231 goto repeat;
232}
233
234int f2fs_issue_flush(struct f2fs_sb_info *sbi)
235{
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800236 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
Chao Yuadf8d902014-05-08 17:00:35 +0800237 struct flush_cmd cmd;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900238
Jaegeuk Kim24a9ee02014-07-25 17:46:10 -0700239 trace_f2fs_issue_flush(sbi->sb, test_opt(sbi, NOBARRIER),
240 test_opt(sbi, FLUSH_MERGE));
241
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700242 if (test_opt(sbi, NOBARRIER))
243 return 0;
244
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900245 if (!test_opt(sbi, FLUSH_MERGE))
246 return blkdev_issue_flush(sbi->sb->s_bdev, GFP_KERNEL, NULL);
247
Chao Yuadf8d902014-05-08 17:00:35 +0800248 init_completion(&cmd.wait);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900249
Gu Zheng721bd4d2014-09-05 18:31:00 +0800250 llist_add(&cmd.llnode, &fcc->issue_list);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900251
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800252 if (!fcc->dispatch_list)
253 wake_up(&fcc->flush_wait_queue);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900254
Chao Yuadf8d902014-05-08 17:00:35 +0800255 wait_for_completion(&cmd.wait);
256
257 return cmd.ret;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900258}
259
Gu Zheng2163d192014-04-27 14:21:33 +0800260int create_flush_cmd_control(struct f2fs_sb_info *sbi)
261{
262 dev_t dev = sbi->sb->s_bdev->bd_dev;
263 struct flush_cmd_control *fcc;
264 int err = 0;
265
266 fcc = kzalloc(sizeof(struct flush_cmd_control), GFP_KERNEL);
267 if (!fcc)
268 return -ENOMEM;
Gu Zheng2163d192014-04-27 14:21:33 +0800269 init_waitqueue_head(&fcc->flush_wait_queue);
Gu Zheng721bd4d2014-09-05 18:31:00 +0800270 init_llist_head(&fcc->issue_list);
Chao Yu6b2920a2014-07-07 11:21:59 +0800271 SM_I(sbi)->cmd_control_info = fcc;
Gu Zheng2163d192014-04-27 14:21:33 +0800272 fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi,
273 "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev));
274 if (IS_ERR(fcc->f2fs_issue_flush)) {
275 err = PTR_ERR(fcc->f2fs_issue_flush);
276 kfree(fcc);
Chao Yu6b2920a2014-07-07 11:21:59 +0800277 SM_I(sbi)->cmd_control_info = NULL;
Gu Zheng2163d192014-04-27 14:21:33 +0800278 return err;
279 }
Gu Zheng2163d192014-04-27 14:21:33 +0800280
281 return err;
282}
283
284void destroy_flush_cmd_control(struct f2fs_sb_info *sbi)
285{
Chao Yu6b2920a2014-07-07 11:21:59 +0800286 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
Gu Zheng2163d192014-04-27 14:21:33 +0800287
288 if (fcc && fcc->f2fs_issue_flush)
289 kthread_stop(fcc->f2fs_issue_flush);
290 kfree(fcc);
Chao Yu6b2920a2014-07-07 11:21:59 +0800291 SM_I(sbi)->cmd_control_info = NULL;
Gu Zheng2163d192014-04-27 14:21:33 +0800292}
293
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900294static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
295 enum dirty_type dirty_type)
296{
297 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
298
299 /* need not be added */
300 if (IS_CURSEG(sbi, segno))
301 return;
302
303 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
304 dirty_i->nr_dirty[dirty_type]++;
305
306 if (dirty_type == DIRTY) {
307 struct seg_entry *sentry = get_seg_entry(sbi, segno);
Changman Lee4625d6a2013-10-25 17:31:57 +0900308 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900309
Jaegeuk Kimec325b52014-09-02 16:24:11 -0700310 if (unlikely(t >= DIRTY)) {
311 f2fs_bug_on(sbi, 1);
312 return;
313 }
Changman Lee4625d6a2013-10-25 17:31:57 +0900314 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
315 dirty_i->nr_dirty[t]++;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900316 }
317}
318
319static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
320 enum dirty_type dirty_type)
321{
322 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
323
324 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
325 dirty_i->nr_dirty[dirty_type]--;
326
327 if (dirty_type == DIRTY) {
Changman Lee4625d6a2013-10-25 17:31:57 +0900328 struct seg_entry *sentry = get_seg_entry(sbi, segno);
329 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900330
Changman Lee4625d6a2013-10-25 17:31:57 +0900331 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
332 dirty_i->nr_dirty[t]--;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900333
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +0900334 if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0)
335 clear_bit(GET_SECNO(sbi, segno),
336 dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900337 }
338}
339
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900340/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900341 * Should not occur error such as -ENOMEM.
342 * Adding dirty entry into seglist is not critical operation.
343 * If a given segment is one of current working segments, it won't be added.
344 */
Haicheng Li8d8451a2013-06-13 16:59:28 +0800345static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900346{
347 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
348 unsigned short valid_blocks;
349
350 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
351 return;
352
353 mutex_lock(&dirty_i->seglist_lock);
354
355 valid_blocks = get_valid_blocks(sbi, segno, 0);
356
357 if (valid_blocks == 0) {
358 __locate_dirty_segment(sbi, segno, PRE);
359 __remove_dirty_segment(sbi, segno, DIRTY);
360 } else if (valid_blocks < sbi->blocks_per_seg) {
361 __locate_dirty_segment(sbi, segno, DIRTY);
362 } else {
363 /* Recovery routine with SSR needs this */
364 __remove_dirty_segment(sbi, segno, DIRTY);
365 }
366
367 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900368}
369
Jaegeuk Kim1e87a782014-04-15 13:57:55 +0900370static int f2fs_issue_discard(struct f2fs_sb_info *sbi,
Jaegeuk Kim37208872013-11-12 16:55:17 +0900371 block_t blkstart, block_t blklen)
372{
Jaegeuk Kimf9a4e6d2013-11-28 12:44:05 +0900373 sector_t start = SECTOR_FROM_BLOCK(sbi, blkstart);
374 sector_t len = SECTOR_FROM_BLOCK(sbi, blklen);
Jaegeuk Kim1661d072013-11-12 17:01:00 +0900375 trace_f2fs_issue_discard(sbi->sb, blkstart, blklen);
Jaegeuk Kim1e87a782014-04-15 13:57:55 +0900376 return blkdev_issue_discard(sbi->sb->s_bdev, start, len, GFP_NOFS, 0);
377}
378
Jaegeuk Kimcf2271e2014-07-25 15:47:25 -0700379void discard_next_dnode(struct f2fs_sb_info *sbi, block_t blkaddr)
Jaegeuk Kim1e87a782014-04-15 13:57:55 +0900380{
Jaegeuk Kim1e87a782014-04-15 13:57:55 +0900381 if (f2fs_issue_discard(sbi, blkaddr, 1)) {
382 struct page *page = grab_meta_page(sbi, blkaddr);
383 /* zero-filled page */
384 set_page_dirty(page);
385 f2fs_put_page(page, 1);
386 }
Jaegeuk Kim37208872013-11-12 16:55:17 +0900387}
388
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900389static void add_discard_addrs(struct f2fs_sb_info *sbi,
390 unsigned int segno, struct seg_entry *se)
391{
392 struct list_head *head = &SM_I(sbi)->discard_list;
393 struct discard_entry *new;
394 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
395 int max_blocks = sbi->blocks_per_seg;
396 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
397 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
398 unsigned long dmap[entries];
399 unsigned int start = 0, end = -1;
400 int i;
401
402 if (!test_opt(sbi, DISCARD))
403 return;
404
405 /* zero block will be discarded through the prefree list */
406 if (!se->valid_blocks || se->valid_blocks == max_blocks)
407 return;
408
409 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
410 for (i = 0; i < entries; i++)
411 dmap[i] = (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
412
413 while (SM_I(sbi)->nr_discards <= SM_I(sbi)->max_discards) {
414 start = __find_rev_next_bit(dmap, max_blocks, end + 1);
415 if (start >= max_blocks)
416 break;
417
418 end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
419
420 new = f2fs_kmem_cache_alloc(discard_entry_slab, GFP_NOFS);
421 INIT_LIST_HEAD(&new->list);
422 new->blkaddr = START_BLOCK(sbi, segno) + start;
423 new->len = end - start;
424
425 list_add_tail(&new->list, head);
426 SM_I(sbi)->nr_discards += end - start;
427 }
428}
429
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900430/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900431 * Should call clear_prefree_segments after checkpoint is done.
432 */
433static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
434{
435 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Chao Yub65ee142014-08-04 10:10:07 +0800436 unsigned int segno;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900437 unsigned int total_segs = TOTAL_SEGS(sbi);
438
439 mutex_lock(&dirty_i->seglist_lock);
Chao Yub65ee142014-08-04 10:10:07 +0800440 for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], total_segs)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900441 __set_test_and_free(sbi, segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900442 mutex_unlock(&dirty_i->seglist_lock);
443}
444
445void clear_prefree_segments(struct f2fs_sb_info *sbi)
446{
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900447 struct list_head *head = &(SM_I(sbi)->discard_list);
Chao Yu2d7b8222014-03-29 11:33:17 +0800448 struct discard_entry *entry, *this;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900449 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Changman Lee29e59c12013-11-11 09:24:37 +0900450 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900451 unsigned int total_segs = TOTAL_SEGS(sbi);
Changman Lee29e59c12013-11-11 09:24:37 +0900452 unsigned int start = 0, end = -1;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900453
454 mutex_lock(&dirty_i->seglist_lock);
Changman Lee29e59c12013-11-11 09:24:37 +0900455
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900456 while (1) {
Changman Lee29e59c12013-11-11 09:24:37 +0900457 int i;
458 start = find_next_bit(prefree_map, total_segs, end + 1);
459 if (start >= total_segs)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900460 break;
Changman Lee29e59c12013-11-11 09:24:37 +0900461 end = find_next_zero_bit(prefree_map, total_segs, start + 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900462
Changman Lee29e59c12013-11-11 09:24:37 +0900463 for (i = start; i < end; i++)
464 clear_bit(i, prefree_map);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900465
Changman Lee29e59c12013-11-11 09:24:37 +0900466 dirty_i->nr_dirty[PRE] -= end - start;
467
468 if (!test_opt(sbi, DISCARD))
469 continue;
470
Jaegeuk Kim37208872013-11-12 16:55:17 +0900471 f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
472 (end - start) << sbi->log_blocks_per_seg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900473 }
474 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900475
476 /* send small discards */
Chao Yu2d7b8222014-03-29 11:33:17 +0800477 list_for_each_entry_safe(entry, this, head, list) {
Jaegeuk Kim37208872013-11-12 16:55:17 +0900478 f2fs_issue_discard(sbi, entry->blkaddr, entry->len);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900479 list_del(&entry->list);
480 SM_I(sbi)->nr_discards -= entry->len;
481 kmem_cache_free(discard_entry_slab, entry);
482 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900483}
484
Chao Yu184a5cd2014-09-04 18:13:01 +0800485static bool __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900486{
487 struct sit_info *sit_i = SIT_I(sbi);
Chao Yu184a5cd2014-09-04 18:13:01 +0800488
489 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900490 sit_i->dirty_sentries++;
Chao Yu184a5cd2014-09-04 18:13:01 +0800491 return false;
492 }
493
494 return true;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900495}
496
497static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
498 unsigned int segno, int modified)
499{
500 struct seg_entry *se = get_seg_entry(sbi, segno);
501 se->type = type;
502 if (modified)
503 __mark_sit_entry_dirty(sbi, segno);
504}
505
506static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
507{
508 struct seg_entry *se;
509 unsigned int segno, offset;
510 long int new_vblocks;
511
512 segno = GET_SEGNO(sbi, blkaddr);
513
514 se = get_seg_entry(sbi, segno);
515 new_vblocks = se->valid_blocks + del;
Jaegeuk Kim491c0852014-02-04 13:01:10 +0900516 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900517
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700518 f2fs_bug_on(sbi, (new_vblocks >> (sizeof(unsigned short) << 3) ||
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900519 (new_vblocks > sbi->blocks_per_seg)));
520
521 se->valid_blocks = new_vblocks;
522 se->mtime = get_mtime(sbi);
523 SIT_I(sbi)->max_mtime = se->mtime;
524
525 /* Update valid block bitmap */
526 if (del > 0) {
527 if (f2fs_set_bit(offset, se->cur_valid_map))
Jaegeuk Kim05796762014-09-02 16:05:00 -0700528 f2fs_bug_on(sbi, 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900529 } else {
530 if (!f2fs_clear_bit(offset, se->cur_valid_map))
Jaegeuk Kim05796762014-09-02 16:05:00 -0700531 f2fs_bug_on(sbi, 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900532 }
533 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
534 se->ckpt_valid_blocks += del;
535
536 __mark_sit_entry_dirty(sbi, segno);
537
538 /* update total number of valid blocks to be written in ckpt area */
539 SIT_I(sbi)->written_valid_blocks += del;
540
541 if (sbi->segs_per_sec > 1)
542 get_sec_entry(sbi, segno)->valid_blocks += del;
543}
544
Jaegeuk Kim5e443812014-01-28 12:22:14 +0900545void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900546{
Jaegeuk Kim5e443812014-01-28 12:22:14 +0900547 update_sit_entry(sbi, new, 1);
548 if (GET_SEGNO(sbi, old) != NULL_SEGNO)
549 update_sit_entry(sbi, old, -1);
550
551 locate_dirty_segment(sbi, GET_SEGNO(sbi, old));
552 locate_dirty_segment(sbi, GET_SEGNO(sbi, new));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900553}
554
555void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
556{
557 unsigned int segno = GET_SEGNO(sbi, addr);
558 struct sit_info *sit_i = SIT_I(sbi);
559
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700560 f2fs_bug_on(sbi, addr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900561 if (addr == NEW_ADDR)
562 return;
563
564 /* add it into sit main buffer */
565 mutex_lock(&sit_i->sentry_lock);
566
567 update_sit_entry(sbi, addr, -1);
568
569 /* add it into dirty seglist */
570 locate_dirty_segment(sbi, segno);
571
572 mutex_unlock(&sit_i->sentry_lock);
573}
574
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900575/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900576 * This function should be resided under the curseg_mutex lock
577 */
578static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
Haicheng Lie79efe32013-06-13 16:59:27 +0800579 struct f2fs_summary *sum)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900580{
581 struct curseg_info *curseg = CURSEG_I(sbi, type);
582 void *addr = curseg->sum_blk;
Haicheng Lie79efe32013-06-13 16:59:27 +0800583 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900584 memcpy(addr, sum, sizeof(struct f2fs_summary));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900585}
586
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900587/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900588 * Calculate the number of current summary pages for writing
589 */
590int npages_for_summary_flush(struct f2fs_sb_info *sbi)
591{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900592 int valid_sum_count = 0;
Fan Li9a479382013-10-29 16:21:47 +0800593 int i, sum_in_page;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900594
595 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
596 if (sbi->ckpt->alloc_type[i] == SSR)
597 valid_sum_count += sbi->blocks_per_seg;
598 else
599 valid_sum_count += curseg_blkoff(sbi, i);
600 }
601
Fan Li9a479382013-10-29 16:21:47 +0800602 sum_in_page = (PAGE_CACHE_SIZE - 2 * SUM_JOURNAL_SIZE -
603 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
604 if (valid_sum_count <= sum_in_page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900605 return 1;
Fan Li9a479382013-10-29 16:21:47 +0800606 else if ((valid_sum_count - sum_in_page) <=
607 (PAGE_CACHE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900608 return 2;
609 return 3;
610}
611
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900612/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900613 * Caller should put this summary page
614 */
615struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
616{
617 return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
618}
619
620static void write_sum_page(struct f2fs_sb_info *sbi,
621 struct f2fs_summary_block *sum_blk, block_t blk_addr)
622{
623 struct page *page = grab_meta_page(sbi, blk_addr);
624 void *kaddr = page_address(page);
625 memcpy(kaddr, sum_blk, PAGE_CACHE_SIZE);
626 set_page_dirty(page);
627 f2fs_put_page(page, 1);
628}
629
Jaegeuk Kim60374682013-03-31 13:58:51 +0900630static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
631{
632 struct curseg_info *curseg = CURSEG_I(sbi, type);
Haicheng Li81fb5e82013-05-14 18:20:28 +0800633 unsigned int segno = curseg->segno + 1;
Jaegeuk Kim60374682013-03-31 13:58:51 +0900634 struct free_segmap_info *free_i = FREE_I(sbi);
635
Haicheng Li81fb5e82013-05-14 18:20:28 +0800636 if (segno < TOTAL_SEGS(sbi) && segno % sbi->segs_per_sec)
637 return !test_bit(segno, free_i->free_segmap);
Jaegeuk Kim60374682013-03-31 13:58:51 +0900638 return 0;
639}
640
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900641/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900642 * Find a new segment from the free segments bitmap to right order
643 * This function should be returned with success, otherwise BUG
644 */
645static void get_new_segment(struct f2fs_sb_info *sbi,
646 unsigned int *newseg, bool new_sec, int dir)
647{
648 struct free_segmap_info *free_i = FREE_I(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900649 unsigned int segno, secno, zoneno;
Jaegeuk Kim53cf9522013-03-31 12:39:49 +0900650 unsigned int total_zones = TOTAL_SECS(sbi) / sbi->secs_per_zone;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900651 unsigned int hint = *newseg / sbi->segs_per_sec;
652 unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
653 unsigned int left_start = hint;
654 bool init = true;
655 int go_left = 0;
656 int i;
657
658 write_lock(&free_i->segmap_lock);
659
660 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
661 segno = find_next_zero_bit(free_i->free_segmap,
662 TOTAL_SEGS(sbi), *newseg + 1);
Jaegeuk Kim33afa7f2013-03-31 12:59:53 +0900663 if (segno - *newseg < sbi->segs_per_sec -
664 (*newseg % sbi->segs_per_sec))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900665 goto got_it;
666 }
667find_other_zone:
Jaegeuk Kim53cf9522013-03-31 12:39:49 +0900668 secno = find_next_zero_bit(free_i->free_secmap, TOTAL_SECS(sbi), hint);
669 if (secno >= TOTAL_SECS(sbi)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900670 if (dir == ALLOC_RIGHT) {
671 secno = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim53cf9522013-03-31 12:39:49 +0900672 TOTAL_SECS(sbi), 0);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700673 f2fs_bug_on(sbi, secno >= TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900674 } else {
675 go_left = 1;
676 left_start = hint - 1;
677 }
678 }
679 if (go_left == 0)
680 goto skip_left;
681
682 while (test_bit(left_start, free_i->free_secmap)) {
683 if (left_start > 0) {
684 left_start--;
685 continue;
686 }
687 left_start = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim53cf9522013-03-31 12:39:49 +0900688 TOTAL_SECS(sbi), 0);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700689 f2fs_bug_on(sbi, left_start >= TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900690 break;
691 }
692 secno = left_start;
693skip_left:
694 hint = secno;
695 segno = secno * sbi->segs_per_sec;
696 zoneno = secno / sbi->secs_per_zone;
697
698 /* give up on finding another zone */
699 if (!init)
700 goto got_it;
701 if (sbi->secs_per_zone == 1)
702 goto got_it;
703 if (zoneno == old_zoneno)
704 goto got_it;
705 if (dir == ALLOC_LEFT) {
706 if (!go_left && zoneno + 1 >= total_zones)
707 goto got_it;
708 if (go_left && zoneno == 0)
709 goto got_it;
710 }
711 for (i = 0; i < NR_CURSEG_TYPE; i++)
712 if (CURSEG_I(sbi, i)->zone == zoneno)
713 break;
714
715 if (i < NR_CURSEG_TYPE) {
716 /* zone is in user, try another */
717 if (go_left)
718 hint = zoneno * sbi->secs_per_zone - 1;
719 else if (zoneno + 1 >= total_zones)
720 hint = 0;
721 else
722 hint = (zoneno + 1) * sbi->secs_per_zone;
723 init = false;
724 goto find_other_zone;
725 }
726got_it:
727 /* set it as dirty segment in free segmap */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700728 f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900729 __set_inuse(sbi, segno);
730 *newseg = segno;
731 write_unlock(&free_i->segmap_lock);
732}
733
734static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
735{
736 struct curseg_info *curseg = CURSEG_I(sbi, type);
737 struct summary_footer *sum_footer;
738
739 curseg->segno = curseg->next_segno;
740 curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
741 curseg->next_blkoff = 0;
742 curseg->next_segno = NULL_SEGNO;
743
744 sum_footer = &(curseg->sum_blk->footer);
745 memset(sum_footer, 0, sizeof(struct summary_footer));
746 if (IS_DATASEG(type))
747 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
748 if (IS_NODESEG(type))
749 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
750 __set_sit_entry_type(sbi, type, curseg->segno, modified);
751}
752
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900753/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900754 * Allocate a current working segment.
755 * This function always allocates a free segment in LFS manner.
756 */
757static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
758{
759 struct curseg_info *curseg = CURSEG_I(sbi, type);
760 unsigned int segno = curseg->segno;
761 int dir = ALLOC_LEFT;
762
763 write_sum_page(sbi, curseg->sum_blk,
Haicheng Li81fb5e82013-05-14 18:20:28 +0800764 GET_SUM_BLOCK(sbi, segno));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900765 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
766 dir = ALLOC_RIGHT;
767
768 if (test_opt(sbi, NOHEAP))
769 dir = ALLOC_RIGHT;
770
771 get_new_segment(sbi, &segno, new_sec, dir);
772 curseg->next_segno = segno;
773 reset_curseg(sbi, type, 1);
774 curseg->alloc_type = LFS;
775}
776
777static void __next_free_blkoff(struct f2fs_sb_info *sbi,
778 struct curseg_info *seg, block_t start)
779{
780 struct seg_entry *se = get_seg_entry(sbi, seg->segno);
Changman Leee81c93c2013-11-15 13:21:16 +0900781 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
782 unsigned long target_map[entries];
783 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
784 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
785 int i, pos;
786
787 for (i = 0; i < entries; i++)
788 target_map[i] = ckpt_map[i] | cur_map[i];
789
790 pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
791
792 seg->next_blkoff = pos;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900793}
794
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900795/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900796 * If a segment is written by LFS manner, next block offset is just obtained
797 * by increasing the current block offset. However, if a segment is written by
798 * SSR manner, next block offset obtained by calling __next_free_blkoff
799 */
800static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
801 struct curseg_info *seg)
802{
803 if (seg->alloc_type == SSR)
804 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
805 else
806 seg->next_blkoff++;
807}
808
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900809/*
arter97e1c42042014-08-06 23:22:50 +0900810 * This function always allocates a used segment(from dirty seglist) by SSR
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900811 * manner, so it should recover the existing segment information of valid blocks
812 */
813static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
814{
815 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
816 struct curseg_info *curseg = CURSEG_I(sbi, type);
817 unsigned int new_segno = curseg->next_segno;
818 struct f2fs_summary_block *sum_node;
819 struct page *sum_page;
820
821 write_sum_page(sbi, curseg->sum_blk,
822 GET_SUM_BLOCK(sbi, curseg->segno));
823 __set_test_and_inuse(sbi, new_segno);
824
825 mutex_lock(&dirty_i->seglist_lock);
826 __remove_dirty_segment(sbi, new_segno, PRE);
827 __remove_dirty_segment(sbi, new_segno, DIRTY);
828 mutex_unlock(&dirty_i->seglist_lock);
829
830 reset_curseg(sbi, type, 1);
831 curseg->alloc_type = SSR;
832 __next_free_blkoff(sbi, curseg, 0);
833
834 if (reuse) {
835 sum_page = get_sum_page(sbi, new_segno);
836 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
837 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
838 f2fs_put_page(sum_page, 1);
839 }
840}
841
Jaegeuk Kim43727522013-02-04 15:11:17 +0900842static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
843{
844 struct curseg_info *curseg = CURSEG_I(sbi, type);
845 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
846
847 if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0))
848 return v_ops->get_victim(sbi,
849 &(curseg)->next_segno, BG_GC, type, SSR);
850
851 /* For data segments, let's do SSR more intensively */
852 for (; type >= CURSEG_HOT_DATA; type--)
853 if (v_ops->get_victim(sbi, &(curseg)->next_segno,
854 BG_GC, type, SSR))
855 return 1;
856 return 0;
857}
858
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900859/*
860 * flush out current segment and replace it with new segment
861 * This function should be returned with success, otherwise BUG
862 */
863static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
864 int type, bool force)
865{
866 struct curseg_info *curseg = CURSEG_I(sbi, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900867
Gu Zheng7b405272013-08-19 09:41:15 +0800868 if (force)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900869 new_curseg(sbi, type, true);
Gu Zheng7b405272013-08-19 09:41:15 +0800870 else if (type == CURSEG_WARM_NODE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900871 new_curseg(sbi, type, false);
Jaegeuk Kim60374682013-03-31 13:58:51 +0900872 else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
873 new_curseg(sbi, type, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900874 else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
875 change_curseg(sbi, type, true);
876 else
877 new_curseg(sbi, type, false);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +0900878
879 stat_inc_seg_type(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900880}
881
882void allocate_new_segments(struct f2fs_sb_info *sbi)
883{
884 struct curseg_info *curseg;
885 unsigned int old_curseg;
886 int i;
887
888 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
889 curseg = CURSEG_I(sbi, i);
890 old_curseg = curseg->segno;
891 SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
892 locate_dirty_segment(sbi, old_curseg);
893 }
894}
895
896static const struct segment_allocation default_salloc_ops = {
897 .allocate_segment = allocate_segment_by_default,
898};
899
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900900static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
901{
902 struct curseg_info *curseg = CURSEG_I(sbi, type);
903 if (curseg->next_blkoff < sbi->blocks_per_seg)
904 return true;
905 return false;
906}
907
908static int __get_segment_type_2(struct page *page, enum page_type p_type)
909{
910 if (p_type == DATA)
911 return CURSEG_HOT_DATA;
912 else
913 return CURSEG_HOT_NODE;
914}
915
916static int __get_segment_type_4(struct page *page, enum page_type p_type)
917{
918 if (p_type == DATA) {
919 struct inode *inode = page->mapping->host;
920
921 if (S_ISDIR(inode->i_mode))
922 return CURSEG_HOT_DATA;
923 else
924 return CURSEG_COLD_DATA;
925 } else {
926 if (IS_DNODE(page) && !is_cold_node(page))
927 return CURSEG_HOT_NODE;
928 else
929 return CURSEG_COLD_NODE;
930 }
931}
932
933static int __get_segment_type_6(struct page *page, enum page_type p_type)
934{
935 if (p_type == DATA) {
936 struct inode *inode = page->mapping->host;
937
938 if (S_ISDIR(inode->i_mode))
939 return CURSEG_HOT_DATA;
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900940 else if (is_cold_data(page) || file_is_cold(inode))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900941 return CURSEG_COLD_DATA;
942 else
943 return CURSEG_WARM_DATA;
944 } else {
945 if (IS_DNODE(page))
946 return is_cold_node(page) ? CURSEG_WARM_NODE :
947 CURSEG_HOT_NODE;
948 else
949 return CURSEG_COLD_NODE;
950 }
951}
952
953static int __get_segment_type(struct page *page, enum page_type p_type)
954{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700955 switch (F2FS_P_SB(page)->active_logs) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900956 case 2:
957 return __get_segment_type_2(page, p_type);
958 case 4:
959 return __get_segment_type_4(page, p_type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900960 }
Jaegeuk Kim12a67142012-12-21 11:47:05 +0900961 /* NR_CURSEG_TYPE(6) logs by default */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700962 f2fs_bug_on(F2FS_P_SB(page),
963 F2FS_P_SB(page)->active_logs != NR_CURSEG_TYPE);
Jaegeuk Kim12a67142012-12-21 11:47:05 +0900964 return __get_segment_type_6(page, p_type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900965}
966
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900967void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
968 block_t old_blkaddr, block_t *new_blkaddr,
969 struct f2fs_summary *sum, int type)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900970{
971 struct sit_info *sit_i = SIT_I(sbi);
972 struct curseg_info *curseg;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900973
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900974 curseg = CURSEG_I(sbi, type);
975
976 mutex_lock(&curseg->curseg_mutex);
977
978 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900979
980 /*
981 * __add_sum_entry should be resided under the curseg_mutex
982 * because, this function updates a summary entry in the
983 * current summary block.
984 */
Haicheng Lie79efe32013-06-13 16:59:27 +0800985 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900986
987 mutex_lock(&sit_i->sentry_lock);
988 __refresh_next_blkoff(sbi, curseg);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +0900989
990 stat_inc_block_count(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900991
Jaegeuk Kim5e443812014-01-28 12:22:14 +0900992 if (!__has_curseg_space(sbi, type))
993 sit_i->s_ops->allocate_segment(sbi, type, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900994 /*
995 * SIT information should be updated before segment allocation,
996 * since SSR needs latest valid block information.
997 */
998 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
Jaegeuk Kim5e443812014-01-28 12:22:14 +0900999
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001000 mutex_unlock(&sit_i->sentry_lock);
1001
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001002 if (page && IS_NODESEG(type))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001003 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
1004
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001005 mutex_unlock(&curseg->curseg_mutex);
1006}
1007
1008static void do_write_page(struct f2fs_sb_info *sbi, struct page *page,
1009 block_t old_blkaddr, block_t *new_blkaddr,
1010 struct f2fs_summary *sum, struct f2fs_io_info *fio)
1011{
1012 int type = __get_segment_type(page, fio->type);
1013
1014 allocate_data_block(sbi, page, old_blkaddr, new_blkaddr, sum, type);
1015
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001016 /* writeout dirty page into bdev */
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001017 f2fs_submit_page_mbio(sbi, page, *new_blkaddr, fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001018}
1019
Jaegeuk Kim577e3492013-01-24 19:56:11 +09001020void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001021{
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001022 struct f2fs_io_info fio = {
1023 .type = META,
Gu Zheng7e8f2302013-12-20 18:17:49 +08001024 .rw = WRITE_SYNC | REQ_META | REQ_PRIO
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001025 };
1026
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001027 set_page_writeback(page);
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001028 f2fs_submit_page_mbio(sbi, page, page->index, &fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001029}
1030
1031void write_node_page(struct f2fs_sb_info *sbi, struct page *page,
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001032 struct f2fs_io_info *fio,
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001033 unsigned int nid, block_t old_blkaddr, block_t *new_blkaddr)
1034{
1035 struct f2fs_summary sum;
1036 set_summary(&sum, nid, 0, 0);
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001037 do_write_page(sbi, page, old_blkaddr, new_blkaddr, &sum, fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001038}
1039
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001040void write_data_page(struct page *page, struct dnode_of_data *dn,
1041 block_t *new_blkaddr, struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001042{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001043 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001044 struct f2fs_summary sum;
1045 struct node_info ni;
1046
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001047 f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001048 get_node_info(sbi, dn->nid, &ni);
1049 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1050
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001051 do_write_page(sbi, page, dn->data_blkaddr, new_blkaddr, &sum, fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001052}
1053
Chris Fries6c311ec2014-01-17 14:44:39 -06001054void rewrite_data_page(struct page *page, block_t old_blkaddr,
1055 struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001056{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001057 f2fs_submit_page_mbio(F2FS_P_SB(page), page, old_blkaddr, fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001058}
1059
1060void recover_data_page(struct f2fs_sb_info *sbi,
1061 struct page *page, struct f2fs_summary *sum,
1062 block_t old_blkaddr, block_t new_blkaddr)
1063{
1064 struct sit_info *sit_i = SIT_I(sbi);
1065 struct curseg_info *curseg;
1066 unsigned int segno, old_cursegno;
1067 struct seg_entry *se;
1068 int type;
1069
1070 segno = GET_SEGNO(sbi, new_blkaddr);
1071 se = get_seg_entry(sbi, segno);
1072 type = se->type;
1073
1074 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
1075 if (old_blkaddr == NULL_ADDR)
1076 type = CURSEG_COLD_DATA;
1077 else
1078 type = CURSEG_WARM_DATA;
1079 }
1080 curseg = CURSEG_I(sbi, type);
1081
1082 mutex_lock(&curseg->curseg_mutex);
1083 mutex_lock(&sit_i->sentry_lock);
1084
1085 old_cursegno = curseg->segno;
1086
1087 /* change the current segment */
1088 if (segno != curseg->segno) {
1089 curseg->next_segno = segno;
1090 change_curseg(sbi, type, true);
1091 }
1092
Jaegeuk Kim491c0852014-02-04 13:01:10 +09001093 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
Haicheng Lie79efe32013-06-13 16:59:27 +08001094 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001095
1096 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001097 locate_dirty_segment(sbi, old_cursegno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001098
1099 mutex_unlock(&sit_i->sentry_lock);
1100 mutex_unlock(&curseg->curseg_mutex);
1101}
1102
Chao Yudf0f8dc2014-03-22 14:57:23 +08001103static inline bool is_merged_page(struct f2fs_sb_info *sbi,
1104 struct page *page, enum page_type type)
1105{
1106 enum page_type btype = PAGE_TYPE_OF_BIO(type);
1107 struct f2fs_bio_info *io = &sbi->write_io[btype];
Chao Yudf0f8dc2014-03-22 14:57:23 +08001108 struct bio_vec *bvec;
1109 int i;
1110
1111 down_read(&io->io_rwsem);
Jaegeuk Kimce234472014-04-02 09:04:42 +09001112 if (!io->bio)
Chao Yudf0f8dc2014-03-22 14:57:23 +08001113 goto out;
1114
Jaegeuk Kimce234472014-04-02 09:04:42 +09001115 bio_for_each_segment_all(bvec, io->bio, i) {
Chao Yudf0f8dc2014-03-22 14:57:23 +08001116 if (page == bvec->bv_page) {
1117 up_read(&io->io_rwsem);
1118 return true;
1119 }
1120 }
1121
1122out:
1123 up_read(&io->io_rwsem);
1124 return false;
1125}
1126
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001127void f2fs_wait_on_page_writeback(struct page *page,
Yuan Zhong5514f0a2014-01-10 07:26:14 +00001128 enum page_type type)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001129{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001130 if (PageWriteback(page)) {
Jaegeuk Kim40813632014-09-02 15:31:18 -07001131 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
1132
Chao Yudf0f8dc2014-03-22 14:57:23 +08001133 if (is_merged_page(sbi, page, type))
1134 f2fs_submit_merged_bio(sbi, type, WRITE);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001135 wait_on_page_writeback(page);
1136 }
1137}
1138
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001139static int read_compacted_summaries(struct f2fs_sb_info *sbi)
1140{
1141 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1142 struct curseg_info *seg_i;
1143 unsigned char *kaddr;
1144 struct page *page;
1145 block_t start;
1146 int i, j, offset;
1147
1148 start = start_sum_block(sbi);
1149
1150 page = get_meta_page(sbi, start++);
1151 kaddr = (unsigned char *)page_address(page);
1152
1153 /* Step 1: restore nat cache */
1154 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1155 memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
1156
1157 /* Step 2: restore sit cache */
1158 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1159 memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
1160 SUM_JOURNAL_SIZE);
1161 offset = 2 * SUM_JOURNAL_SIZE;
1162
1163 /* Step 3: restore summary entries */
1164 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1165 unsigned short blk_off;
1166 unsigned int segno;
1167
1168 seg_i = CURSEG_I(sbi, i);
1169 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
1170 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
1171 seg_i->next_segno = segno;
1172 reset_curseg(sbi, i, 0);
1173 seg_i->alloc_type = ckpt->alloc_type[i];
1174 seg_i->next_blkoff = blk_off;
1175
1176 if (seg_i->alloc_type == SSR)
1177 blk_off = sbi->blocks_per_seg;
1178
1179 for (j = 0; j < blk_off; j++) {
1180 struct f2fs_summary *s;
1181 s = (struct f2fs_summary *)(kaddr + offset);
1182 seg_i->sum_blk->entries[j] = *s;
1183 offset += SUMMARY_SIZE;
1184 if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1185 SUM_FOOTER_SIZE)
1186 continue;
1187
1188 f2fs_put_page(page, 1);
1189 page = NULL;
1190
1191 page = get_meta_page(sbi, start++);
1192 kaddr = (unsigned char *)page_address(page);
1193 offset = 0;
1194 }
1195 }
1196 f2fs_put_page(page, 1);
1197 return 0;
1198}
1199
1200static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
1201{
1202 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1203 struct f2fs_summary_block *sum;
1204 struct curseg_info *curseg;
1205 struct page *new;
1206 unsigned short blk_off;
1207 unsigned int segno = 0;
1208 block_t blk_addr = 0;
1209
1210 /* get segment number and block addr */
1211 if (IS_DATASEG(type)) {
1212 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
1213 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
1214 CURSEG_HOT_DATA]);
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001215 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001216 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
1217 else
1218 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
1219 } else {
1220 segno = le32_to_cpu(ckpt->cur_node_segno[type -
1221 CURSEG_HOT_NODE]);
1222 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
1223 CURSEG_HOT_NODE]);
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001224 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001225 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
1226 type - CURSEG_HOT_NODE);
1227 else
1228 blk_addr = GET_SUM_BLOCK(sbi, segno);
1229 }
1230
1231 new = get_meta_page(sbi, blk_addr);
1232 sum = (struct f2fs_summary_block *)page_address(new);
1233
1234 if (IS_NODESEG(type)) {
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001235 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001236 struct f2fs_summary *ns = &sum->entries[0];
1237 int i;
1238 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
1239 ns->version = 0;
1240 ns->ofs_in_node = 0;
1241 }
1242 } else {
Gu Zhengd6537882014-03-07 18:43:36 +08001243 int err;
1244
1245 err = restore_node_summary(sbi, segno, sum);
1246 if (err) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001247 f2fs_put_page(new, 1);
Gu Zhengd6537882014-03-07 18:43:36 +08001248 return err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001249 }
1250 }
1251 }
1252
1253 /* set uncompleted segment to curseg */
1254 curseg = CURSEG_I(sbi, type);
1255 mutex_lock(&curseg->curseg_mutex);
1256 memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
1257 curseg->next_segno = segno;
1258 reset_curseg(sbi, type, 0);
1259 curseg->alloc_type = ckpt->alloc_type[type];
1260 curseg->next_blkoff = blk_off;
1261 mutex_unlock(&curseg->curseg_mutex);
1262 f2fs_put_page(new, 1);
1263 return 0;
1264}
1265
1266static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
1267{
1268 int type = CURSEG_HOT_DATA;
Chao Yue4fc5fb2014-03-17 16:36:24 +08001269 int err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001270
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001271 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001272 /* restore for compacted data summary */
1273 if (read_compacted_summaries(sbi))
1274 return -EINVAL;
1275 type = CURSEG_HOT_NODE;
1276 }
1277
Chao Yue4fc5fb2014-03-17 16:36:24 +08001278 for (; type <= CURSEG_COLD_NODE; type++) {
1279 err = read_normal_summaries(sbi, type);
1280 if (err)
1281 return err;
1282 }
1283
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001284 return 0;
1285}
1286
1287static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
1288{
1289 struct page *page;
1290 unsigned char *kaddr;
1291 struct f2fs_summary *summary;
1292 struct curseg_info *seg_i;
1293 int written_size = 0;
1294 int i, j;
1295
1296 page = grab_meta_page(sbi, blkaddr++);
1297 kaddr = (unsigned char *)page_address(page);
1298
1299 /* Step 1: write nat cache */
1300 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1301 memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
1302 written_size += SUM_JOURNAL_SIZE;
1303
1304 /* Step 2: write sit cache */
1305 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1306 memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
1307 SUM_JOURNAL_SIZE);
1308 written_size += SUM_JOURNAL_SIZE;
1309
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001310 /* Step 3: write summary entries */
1311 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1312 unsigned short blkoff;
1313 seg_i = CURSEG_I(sbi, i);
1314 if (sbi->ckpt->alloc_type[i] == SSR)
1315 blkoff = sbi->blocks_per_seg;
1316 else
1317 blkoff = curseg_blkoff(sbi, i);
1318
1319 for (j = 0; j < blkoff; j++) {
1320 if (!page) {
1321 page = grab_meta_page(sbi, blkaddr++);
1322 kaddr = (unsigned char *)page_address(page);
1323 written_size = 0;
1324 }
1325 summary = (struct f2fs_summary *)(kaddr + written_size);
1326 *summary = seg_i->sum_blk->entries[j];
1327 written_size += SUMMARY_SIZE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001328
1329 if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1330 SUM_FOOTER_SIZE)
1331 continue;
1332
Chao Yue8d61a72013-10-24 15:08:28 +08001333 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001334 f2fs_put_page(page, 1);
1335 page = NULL;
1336 }
1337 }
Chao Yue8d61a72013-10-24 15:08:28 +08001338 if (page) {
1339 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001340 f2fs_put_page(page, 1);
Chao Yue8d61a72013-10-24 15:08:28 +08001341 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001342}
1343
1344static void write_normal_summaries(struct f2fs_sb_info *sbi,
1345 block_t blkaddr, int type)
1346{
1347 int i, end;
1348 if (IS_DATASEG(type))
1349 end = type + NR_CURSEG_DATA_TYPE;
1350 else
1351 end = type + NR_CURSEG_NODE_TYPE;
1352
1353 for (i = type; i < end; i++) {
1354 struct curseg_info *sum = CURSEG_I(sbi, i);
1355 mutex_lock(&sum->curseg_mutex);
1356 write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
1357 mutex_unlock(&sum->curseg_mutex);
1358 }
1359}
1360
1361void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1362{
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001363 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001364 write_compacted_summaries(sbi, start_blk);
1365 else
1366 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
1367}
1368
1369void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1370{
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001371 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001372 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001373}
1374
1375int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
1376 unsigned int val, int alloc)
1377{
1378 int i;
1379
1380 if (type == NAT_JOURNAL) {
1381 for (i = 0; i < nats_in_cursum(sum); i++) {
1382 if (le32_to_cpu(nid_in_journal(sum, i)) == val)
1383 return i;
1384 }
1385 if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
1386 return update_nats_in_cursum(sum, 1);
1387 } else if (type == SIT_JOURNAL) {
1388 for (i = 0; i < sits_in_cursum(sum); i++)
1389 if (le32_to_cpu(segno_in_journal(sum, i)) == val)
1390 return i;
1391 if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
1392 return update_sits_in_cursum(sum, 1);
1393 }
1394 return -1;
1395}
1396
1397static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
1398 unsigned int segno)
1399{
1400 struct sit_info *sit_i = SIT_I(sbi);
Chao Yud3a14af2014-09-04 18:11:47 +08001401 unsigned int offset = SIT_BLOCK_OFFSET(segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001402 block_t blk_addr = sit_i->sit_base_addr + offset;
1403
1404 check_seg_range(sbi, segno);
1405
1406 /* calculate sit block address */
1407 if (f2fs_test_bit(offset, sit_i->sit_bitmap))
1408 blk_addr += sit_i->sit_blocks;
1409
1410 return get_meta_page(sbi, blk_addr);
1411}
1412
1413static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
1414 unsigned int start)
1415{
1416 struct sit_info *sit_i = SIT_I(sbi);
1417 struct page *src_page, *dst_page;
1418 pgoff_t src_off, dst_off;
1419 void *src_addr, *dst_addr;
1420
1421 src_off = current_sit_addr(sbi, start);
1422 dst_off = next_sit_addr(sbi, src_off);
1423
1424 /* get current sit block page without lock */
1425 src_page = get_meta_page(sbi, src_off);
1426 dst_page = grab_meta_page(sbi, dst_off);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001427 f2fs_bug_on(sbi, PageDirty(src_page));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001428
1429 src_addr = page_address(src_page);
1430 dst_addr = page_address(dst_page);
1431 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
1432
1433 set_page_dirty(dst_page);
1434 f2fs_put_page(src_page, 1);
1435
1436 set_to_next_sit(sit_i, start);
1437
1438 return dst_page;
1439}
1440
Chao Yu184a5cd2014-09-04 18:13:01 +08001441static struct sit_entry_set *grab_sit_entry_set(void)
1442{
1443 struct sit_entry_set *ses =
1444 f2fs_kmem_cache_alloc(sit_entry_set_slab, GFP_ATOMIC);
1445
1446 ses->entry_cnt = 0;
1447 INIT_LIST_HEAD(&ses->set_list);
1448 return ses;
1449}
1450
1451static void release_sit_entry_set(struct sit_entry_set *ses)
1452{
1453 list_del(&ses->set_list);
1454 kmem_cache_free(sit_entry_set_slab, ses);
1455}
1456
1457static void adjust_sit_entry_set(struct sit_entry_set *ses,
1458 struct list_head *head)
1459{
1460 struct sit_entry_set *next = ses;
1461
1462 if (list_is_last(&ses->set_list, head))
1463 return;
1464
1465 list_for_each_entry_continue(next, head, set_list)
1466 if (ses->entry_cnt <= next->entry_cnt)
1467 break;
1468
1469 list_move_tail(&ses->set_list, &next->set_list);
1470}
1471
1472static void add_sit_entry(unsigned int segno, struct list_head *head)
1473{
1474 struct sit_entry_set *ses;
1475 unsigned int start_segno = START_SEGNO(segno);
1476
1477 list_for_each_entry(ses, head, set_list) {
1478 if (ses->start_segno == start_segno) {
1479 ses->entry_cnt++;
1480 adjust_sit_entry_set(ses, head);
1481 return;
1482 }
1483 }
1484
1485 ses = grab_sit_entry_set();
1486
1487 ses->start_segno = start_segno;
1488 ses->entry_cnt++;
1489 list_add(&ses->set_list, head);
1490}
1491
1492static void add_sits_in_set(struct f2fs_sb_info *sbi)
1493{
1494 struct f2fs_sm_info *sm_info = SM_I(sbi);
1495 struct list_head *set_list = &sm_info->sit_entry_set;
1496 unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap;
1497 unsigned long nsegs = TOTAL_SEGS(sbi);
1498 unsigned int segno;
1499
1500 for_each_set_bit(segno, bitmap, nsegs)
1501 add_sit_entry(segno, set_list);
1502}
1503
1504static void remove_sits_in_journal(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001505{
1506 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1507 struct f2fs_summary_block *sum = curseg->sum_blk;
1508 int i;
1509
Chao Yu184a5cd2014-09-04 18:13:01 +08001510 for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
1511 unsigned int segno;
1512 bool dirtied;
1513
1514 segno = le32_to_cpu(segno_in_journal(sum, i));
1515 dirtied = __mark_sit_entry_dirty(sbi, segno);
1516
1517 if (!dirtied)
1518 add_sit_entry(segno, &SM_I(sbi)->sit_entry_set);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001519 }
Chao Yu184a5cd2014-09-04 18:13:01 +08001520 update_sits_in_cursum(sum, -sits_in_cursum(sum));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001521}
1522
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001523/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001524 * CP calls this function, which flushes SIT entries including sit_journal,
1525 * and moves prefree segs to free segs.
1526 */
1527void flush_sit_entries(struct f2fs_sb_info *sbi)
1528{
1529 struct sit_info *sit_i = SIT_I(sbi);
1530 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
1531 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1532 struct f2fs_summary_block *sum = curseg->sum_blk;
Chao Yu184a5cd2014-09-04 18:13:01 +08001533 struct sit_entry_set *ses, *tmp;
1534 struct list_head *head = &SM_I(sbi)->sit_entry_set;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001535 unsigned long nsegs = TOTAL_SEGS(sbi);
Chao Yu184a5cd2014-09-04 18:13:01 +08001536 bool to_journal = true;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001537
1538 mutex_lock(&curseg->curseg_mutex);
1539 mutex_lock(&sit_i->sentry_lock);
1540
1541 /*
Chao Yu184a5cd2014-09-04 18:13:01 +08001542 * add and account sit entries of dirty bitmap in sit entry
1543 * set temporarily
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001544 */
Chao Yu184a5cd2014-09-04 18:13:01 +08001545 add_sits_in_set(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001546
Chao Yu184a5cd2014-09-04 18:13:01 +08001547 /*
1548 * if there are no enough space in journal to store dirty sit
1549 * entries, remove all entries from journal and add and account
1550 * them in sit entry set.
1551 */
1552 if (!__has_cursum_space(sum, sit_i->dirty_sentries, SIT_JOURNAL))
1553 remove_sits_in_journal(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001554
Chao Yu184a5cd2014-09-04 18:13:01 +08001555 if (!sit_i->dirty_sentries)
1556 goto out;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001557
Chao Yu184a5cd2014-09-04 18:13:01 +08001558 /*
1559 * there are two steps to flush sit entries:
1560 * #1, flush sit entries to journal in current cold data summary block.
1561 * #2, flush sit entries to sit page.
1562 */
1563 list_for_each_entry_safe(ses, tmp, head, set_list) {
1564 struct page *page;
1565 struct f2fs_sit_block *raw_sit = NULL;
1566 unsigned int start_segno = ses->start_segno;
1567 unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK,
1568 nsegs);
1569 unsigned int segno = start_segno;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001570
Chao Yu184a5cd2014-09-04 18:13:01 +08001571 if (to_journal &&
1572 !__has_cursum_space(sum, ses->entry_cnt, SIT_JOURNAL))
1573 to_journal = false;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001574
Chao Yu184a5cd2014-09-04 18:13:01 +08001575 if (!to_journal) {
1576 page = get_next_sit_page(sbi, start_segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001577 raw_sit = page_address(page);
1578 }
1579
Chao Yu184a5cd2014-09-04 18:13:01 +08001580 /* flush dirty sit entries in region of current sit set */
1581 for_each_set_bit_from(segno, bitmap, end) {
1582 int offset, sit_offset;
1583 struct seg_entry *se = get_seg_entry(sbi, segno);
1584
1585 /* add discard candidates */
1586 if (SM_I(sbi)->nr_discards < SM_I(sbi)->max_discards)
1587 add_discard_addrs(sbi, segno, se);
1588
1589 if (to_journal) {
1590 offset = lookup_journal_in_cursum(sum,
1591 SIT_JOURNAL, segno, 1);
1592 f2fs_bug_on(sbi, offset < 0);
1593 segno_in_journal(sum, offset) =
1594 cpu_to_le32(segno);
1595 seg_info_to_raw_sit(se,
1596 &sit_in_journal(sum, offset));
1597 } else {
1598 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
1599 seg_info_to_raw_sit(se,
1600 &raw_sit->entries[sit_offset]);
1601 }
1602
1603 __clear_bit(segno, bitmap);
1604 sit_i->dirty_sentries--;
1605 ses->entry_cnt--;
1606 }
1607
1608 if (!to_journal)
1609 f2fs_put_page(page, 1);
1610
1611 f2fs_bug_on(sbi, ses->entry_cnt);
1612 release_sit_entry_set(ses);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001613 }
Chao Yu184a5cd2014-09-04 18:13:01 +08001614
1615 f2fs_bug_on(sbi, !list_empty(head));
1616 f2fs_bug_on(sbi, sit_i->dirty_sentries);
1617
1618out:
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001619 mutex_unlock(&sit_i->sentry_lock);
1620 mutex_unlock(&curseg->curseg_mutex);
1621
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001622 set_prefree_as_free_segments(sbi);
1623}
1624
1625static int build_sit_info(struct f2fs_sb_info *sbi)
1626{
1627 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1628 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1629 struct sit_info *sit_i;
1630 unsigned int sit_segs, start;
1631 char *src_bitmap, *dst_bitmap;
1632 unsigned int bitmap_size;
1633
1634 /* allocate memory for SIT information */
1635 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
1636 if (!sit_i)
1637 return -ENOMEM;
1638
1639 SM_I(sbi)->sit_info = sit_i;
1640
1641 sit_i->sentries = vzalloc(TOTAL_SEGS(sbi) * sizeof(struct seg_entry));
1642 if (!sit_i->sentries)
1643 return -ENOMEM;
1644
1645 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1646 sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1647 if (!sit_i->dirty_sentries_bitmap)
1648 return -ENOMEM;
1649
1650 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1651 sit_i->sentries[start].cur_valid_map
1652 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1653 sit_i->sentries[start].ckpt_valid_map
1654 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1655 if (!sit_i->sentries[start].cur_valid_map
1656 || !sit_i->sentries[start].ckpt_valid_map)
1657 return -ENOMEM;
1658 }
1659
1660 if (sbi->segs_per_sec > 1) {
Jaegeuk Kim53cf9522013-03-31 12:39:49 +09001661 sit_i->sec_entries = vzalloc(TOTAL_SECS(sbi) *
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001662 sizeof(struct sec_entry));
1663 if (!sit_i->sec_entries)
1664 return -ENOMEM;
1665 }
1666
1667 /* get information related with SIT */
1668 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
1669
1670 /* setup SIT bitmap from ckeckpoint pack */
1671 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
1672 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
1673
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001674 dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001675 if (!dst_bitmap)
1676 return -ENOMEM;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001677
1678 /* init SIT information */
1679 sit_i->s_ops = &default_salloc_ops;
1680
1681 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
1682 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
1683 sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
1684 sit_i->sit_bitmap = dst_bitmap;
1685 sit_i->bitmap_size = bitmap_size;
1686 sit_i->dirty_sentries = 0;
1687 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
1688 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
1689 sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
1690 mutex_init(&sit_i->sentry_lock);
1691 return 0;
1692}
1693
1694static int build_free_segmap(struct f2fs_sb_info *sbi)
1695{
1696 struct f2fs_sm_info *sm_info = SM_I(sbi);
1697 struct free_segmap_info *free_i;
1698 unsigned int bitmap_size, sec_bitmap_size;
1699
1700 /* allocate memory for free segmap information */
1701 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
1702 if (!free_i)
1703 return -ENOMEM;
1704
1705 SM_I(sbi)->free_info = free_i;
1706
1707 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1708 free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL);
1709 if (!free_i->free_segmap)
1710 return -ENOMEM;
1711
Jaegeuk Kim53cf9522013-03-31 12:39:49 +09001712 sec_bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001713 free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL);
1714 if (!free_i->free_secmap)
1715 return -ENOMEM;
1716
1717 /* set all segments as dirty temporarily */
1718 memset(free_i->free_segmap, 0xff, bitmap_size);
1719 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
1720
1721 /* init free segmap information */
1722 free_i->start_segno =
1723 (unsigned int) GET_SEGNO_FROM_SEG0(sbi, sm_info->main_blkaddr);
1724 free_i->free_segments = 0;
1725 free_i->free_sections = 0;
1726 rwlock_init(&free_i->segmap_lock);
1727 return 0;
1728}
1729
1730static int build_curseg(struct f2fs_sb_info *sbi)
1731{
Namjae Jeon1042d602012-12-01 10:56:13 +09001732 struct curseg_info *array;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001733 int i;
1734
Fabian Frederickb434bab2014-06-23 18:39:15 +02001735 array = kcalloc(NR_CURSEG_TYPE, sizeof(*array), GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001736 if (!array)
1737 return -ENOMEM;
1738
1739 SM_I(sbi)->curseg_array = array;
1740
1741 for (i = 0; i < NR_CURSEG_TYPE; i++) {
1742 mutex_init(&array[i].curseg_mutex);
1743 array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
1744 if (!array[i].sum_blk)
1745 return -ENOMEM;
1746 array[i].segno = NULL_SEGNO;
1747 array[i].next_blkoff = 0;
1748 }
1749 return restore_curseg_summaries(sbi);
1750}
1751
1752static void build_sit_entries(struct f2fs_sb_info *sbi)
1753{
1754 struct sit_info *sit_i = SIT_I(sbi);
1755 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1756 struct f2fs_summary_block *sum = curseg->sum_blk;
Chao Yu74de5932013-11-22 09:09:59 +08001757 int sit_blk_cnt = SIT_BLK_CNT(sbi);
1758 unsigned int i, start, end;
1759 unsigned int readed, start_blk = 0;
Jaegeuk Kim90a893c2014-09-22 16:21:07 -07001760 int nrpages = MAX_BIO_BLOCKS(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001761
Chao Yu74de5932013-11-22 09:09:59 +08001762 do {
Chao Yu662befd2014-02-07 16:11:53 +08001763 readed = ra_meta_pages(sbi, start_blk, nrpages, META_SIT);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001764
Chao Yu74de5932013-11-22 09:09:59 +08001765 start = start_blk * sit_i->sents_per_block;
1766 end = (start_blk + readed) * sit_i->sents_per_block;
1767
1768 for (; start < end && start < TOTAL_SEGS(sbi); start++) {
1769 struct seg_entry *se = &sit_i->sentries[start];
1770 struct f2fs_sit_block *sit_blk;
1771 struct f2fs_sit_entry sit;
1772 struct page *page;
1773
1774 mutex_lock(&curseg->curseg_mutex);
1775 for (i = 0; i < sits_in_cursum(sum); i++) {
Chris Fries6c311ec2014-01-17 14:44:39 -06001776 if (le32_to_cpu(segno_in_journal(sum, i))
1777 == start) {
Chao Yu74de5932013-11-22 09:09:59 +08001778 sit = sit_in_journal(sum, i);
1779 mutex_unlock(&curseg->curseg_mutex);
1780 goto got_it;
1781 }
1782 }
1783 mutex_unlock(&curseg->curseg_mutex);
1784
1785 page = get_current_sit_page(sbi, start);
1786 sit_blk = (struct f2fs_sit_block *)page_address(page);
1787 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
1788 f2fs_put_page(page, 1);
1789got_it:
1790 check_block_count(sbi, start, &sit);
1791 seg_info_from_raw_sit(se, &sit);
1792 if (sbi->segs_per_sec > 1) {
1793 struct sec_entry *e = get_sec_entry(sbi, start);
1794 e->valid_blocks += se->valid_blocks;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001795 }
1796 }
Chao Yu74de5932013-11-22 09:09:59 +08001797 start_blk += readed;
1798 } while (start_blk < sit_blk_cnt);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001799}
1800
1801static void init_free_segmap(struct f2fs_sb_info *sbi)
1802{
1803 unsigned int start;
1804 int type;
1805
1806 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1807 struct seg_entry *sentry = get_seg_entry(sbi, start);
1808 if (!sentry->valid_blocks)
1809 __set_free(sbi, start);
1810 }
1811
1812 /* set use the current segments */
1813 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
1814 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
1815 __set_test_and_inuse(sbi, curseg_t->segno);
1816 }
1817}
1818
1819static void init_dirty_segmap(struct f2fs_sb_info *sbi)
1820{
1821 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1822 struct free_segmap_info *free_i = FREE_I(sbi);
Namjae Jeon8736fbf2013-06-16 09:49:11 +09001823 unsigned int segno = 0, offset = 0, total_segs = TOTAL_SEGS(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001824 unsigned short valid_blocks;
1825
Namjae Jeon8736fbf2013-06-16 09:49:11 +09001826 while (1) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001827 /* find dirty segment based on free segmap */
Namjae Jeon8736fbf2013-06-16 09:49:11 +09001828 segno = find_next_inuse(free_i, total_segs, offset);
1829 if (segno >= total_segs)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001830 break;
1831 offset = segno + 1;
1832 valid_blocks = get_valid_blocks(sbi, segno, 0);
Jaegeuk Kimec325b52014-09-02 16:24:11 -07001833 if (valid_blocks == sbi->blocks_per_seg || !valid_blocks)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001834 continue;
Jaegeuk Kimec325b52014-09-02 16:24:11 -07001835 if (valid_blocks > sbi->blocks_per_seg) {
1836 f2fs_bug_on(sbi, 1);
1837 continue;
1838 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001839 mutex_lock(&dirty_i->seglist_lock);
1840 __locate_dirty_segment(sbi, segno, DIRTY);
1841 mutex_unlock(&dirty_i->seglist_lock);
1842 }
1843}
1844
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001845static int init_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001846{
1847 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001848 unsigned int bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001849
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001850 dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL);
1851 if (!dirty_i->victim_secmap)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001852 return -ENOMEM;
1853 return 0;
1854}
1855
1856static int build_dirty_segmap(struct f2fs_sb_info *sbi)
1857{
1858 struct dirty_seglist_info *dirty_i;
1859 unsigned int bitmap_size, i;
1860
1861 /* allocate memory for dirty segments list information */
1862 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
1863 if (!dirty_i)
1864 return -ENOMEM;
1865
1866 SM_I(sbi)->dirty_info = dirty_i;
1867 mutex_init(&dirty_i->seglist_lock);
1868
1869 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1870
1871 for (i = 0; i < NR_DIRTY_TYPE; i++) {
1872 dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001873 if (!dirty_i->dirty_segmap[i])
1874 return -ENOMEM;
1875 }
1876
1877 init_dirty_segmap(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001878 return init_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001879}
1880
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001881/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001882 * Update min, max modified time for cost-benefit GC algorithm
1883 */
1884static void init_min_max_mtime(struct f2fs_sb_info *sbi)
1885{
1886 struct sit_info *sit_i = SIT_I(sbi);
1887 unsigned int segno;
1888
1889 mutex_lock(&sit_i->sentry_lock);
1890
1891 sit_i->min_mtime = LLONG_MAX;
1892
1893 for (segno = 0; segno < TOTAL_SEGS(sbi); segno += sbi->segs_per_sec) {
1894 unsigned int i;
1895 unsigned long long mtime = 0;
1896
1897 for (i = 0; i < sbi->segs_per_sec; i++)
1898 mtime += get_seg_entry(sbi, segno + i)->mtime;
1899
1900 mtime = div_u64(mtime, sbi->segs_per_sec);
1901
1902 if (sit_i->min_mtime > mtime)
1903 sit_i->min_mtime = mtime;
1904 }
1905 sit_i->max_mtime = get_mtime(sbi);
1906 mutex_unlock(&sit_i->sentry_lock);
1907}
1908
1909int build_segment_manager(struct f2fs_sb_info *sbi)
1910{
1911 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1912 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Namjae Jeon1042d602012-12-01 10:56:13 +09001913 struct f2fs_sm_info *sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001914 int err;
1915
1916 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
1917 if (!sm_info)
1918 return -ENOMEM;
1919
1920 /* init sm info */
1921 sbi->sm_info = sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001922 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
1923 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
1924 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
1925 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
1926 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
1927 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
1928 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
Jaegeuk Kim58c41032014-03-19 14:17:21 +09001929 sm_info->rec_prefree_segments = sm_info->main_segments *
1930 DEF_RECLAIM_PREFREE_SEGMENTS / 100;
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -07001931 sm_info->ipu_policy = F2FS_IPU_FSYNC;
Jaegeuk Kim216fbd62013-11-07 13:13:42 +09001932 sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -07001933 sm_info->min_fsync_blocks = DEF_MIN_FSYNC_BLOCKS;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001934
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09001935 INIT_LIST_HEAD(&sm_info->discard_list);
1936 sm_info->nr_discards = 0;
1937 sm_info->max_discards = 0;
1938
Chao Yu184a5cd2014-09-04 18:13:01 +08001939 INIT_LIST_HEAD(&sm_info->sit_entry_set);
1940
Gu Zhengb270ad62014-04-11 17:49:55 +08001941 if (test_opt(sbi, FLUSH_MERGE) && !f2fs_readonly(sbi->sb)) {
Gu Zheng2163d192014-04-27 14:21:33 +08001942 err = create_flush_cmd_control(sbi);
1943 if (err)
Gu Zhenga688b9d9e2014-04-27 14:21:21 +08001944 return err;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +09001945 }
1946
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001947 err = build_sit_info(sbi);
1948 if (err)
1949 return err;
1950 err = build_free_segmap(sbi);
1951 if (err)
1952 return err;
1953 err = build_curseg(sbi);
1954 if (err)
1955 return err;
1956
1957 /* reinit free segmap based on SIT */
1958 build_sit_entries(sbi);
1959
1960 init_free_segmap(sbi);
1961 err = build_dirty_segmap(sbi);
1962 if (err)
1963 return err;
1964
1965 init_min_max_mtime(sbi);
1966 return 0;
1967}
1968
1969static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
1970 enum dirty_type dirty_type)
1971{
1972 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1973
1974 mutex_lock(&dirty_i->seglist_lock);
1975 kfree(dirty_i->dirty_segmap[dirty_type]);
1976 dirty_i->nr_dirty[dirty_type] = 0;
1977 mutex_unlock(&dirty_i->seglist_lock);
1978}
1979
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001980static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001981{
1982 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001983 kfree(dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001984}
1985
1986static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
1987{
1988 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1989 int i;
1990
1991 if (!dirty_i)
1992 return;
1993
1994 /* discard pre-free/dirty segments list */
1995 for (i = 0; i < NR_DIRTY_TYPE; i++)
1996 discard_dirty_segmap(sbi, i);
1997
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001998 destroy_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001999 SM_I(sbi)->dirty_info = NULL;
2000 kfree(dirty_i);
2001}
2002
2003static void destroy_curseg(struct f2fs_sb_info *sbi)
2004{
2005 struct curseg_info *array = SM_I(sbi)->curseg_array;
2006 int i;
2007
2008 if (!array)
2009 return;
2010 SM_I(sbi)->curseg_array = NULL;
2011 for (i = 0; i < NR_CURSEG_TYPE; i++)
2012 kfree(array[i].sum_blk);
2013 kfree(array);
2014}
2015
2016static void destroy_free_segmap(struct f2fs_sb_info *sbi)
2017{
2018 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
2019 if (!free_i)
2020 return;
2021 SM_I(sbi)->free_info = NULL;
2022 kfree(free_i->free_segmap);
2023 kfree(free_i->free_secmap);
2024 kfree(free_i);
2025}
2026
2027static void destroy_sit_info(struct f2fs_sb_info *sbi)
2028{
2029 struct sit_info *sit_i = SIT_I(sbi);
2030 unsigned int start;
2031
2032 if (!sit_i)
2033 return;
2034
2035 if (sit_i->sentries) {
2036 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
2037 kfree(sit_i->sentries[start].cur_valid_map);
2038 kfree(sit_i->sentries[start].ckpt_valid_map);
2039 }
2040 }
2041 vfree(sit_i->sentries);
2042 vfree(sit_i->sec_entries);
2043 kfree(sit_i->dirty_sentries_bitmap);
2044
2045 SM_I(sbi)->sit_info = NULL;
2046 kfree(sit_i->sit_bitmap);
2047 kfree(sit_i);
2048}
2049
2050void destroy_segment_manager(struct f2fs_sb_info *sbi)
2051{
2052 struct f2fs_sm_info *sm_info = SM_I(sbi);
Gu Zhenga688b9d9e2014-04-27 14:21:21 +08002053
Chao Yu3b03f722013-11-06 09:12:04 +08002054 if (!sm_info)
2055 return;
Gu Zheng2163d192014-04-27 14:21:33 +08002056 destroy_flush_cmd_control(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002057 destroy_dirty_segmap(sbi);
2058 destroy_curseg(sbi);
2059 destroy_free_segmap(sbi);
2060 destroy_sit_info(sbi);
2061 sbi->sm_info = NULL;
2062 kfree(sm_info);
2063}
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002064
2065int __init create_segment_manager_caches(void)
2066{
2067 discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
Gu Zhenge8512d22014-03-07 18:43:28 +08002068 sizeof(struct discard_entry));
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002069 if (!discard_entry_slab)
Chao Yu184a5cd2014-09-04 18:13:01 +08002070 goto fail;
2071
2072 sit_entry_set_slab = f2fs_kmem_cache_create("sit_entry_set",
2073 sizeof(struct nat_entry_set));
2074 if (!sit_entry_set_slab)
2075 goto destory_discard_entry;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002076 return 0;
Chao Yu184a5cd2014-09-04 18:13:01 +08002077
2078destory_discard_entry:
2079 kmem_cache_destroy(discard_entry_slab);
2080fail:
2081 return -ENOMEM;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002082}
2083
2084void destroy_segment_manager_caches(void)
2085{
Chao Yu184a5cd2014-09-04 18:13:01 +08002086 kmem_cache_destroy(sit_entry_set_slab);
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002087 kmem_cache_destroy(discard_entry_slab);
2088}