blob: fc87da189884f71983a60632e86db3f8450d8c1e [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{
Chao Yu55cf9cb2014-09-15 18:01:10 +0800373 sector_t start = SECTOR_FROM_BLOCK(blkstart);
374 sector_t len = SECTOR_FROM_BLOCK(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 Kim4b2fecc2014-09-20 22:06:39 -0700389static void add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc)
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900390{
391 struct list_head *head = &SM_I(sbi)->discard_list;
392 struct discard_entry *new;
393 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
394 int max_blocks = sbi->blocks_per_seg;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700395 struct seg_entry *se = get_seg_entry(sbi, cpc->trim_start);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900396 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;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700400 bool force = (cpc->reason == CP_DISCARD);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900401 int i;
402
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700403 if (!force && !test_opt(sbi, DISCARD))
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900404 return;
405
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700406 if (force && !se->valid_blocks) {
407 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
408 /*
409 * if this segment is registered in the prefree list, then
410 * we should skip adding a discard candidate, and let the
411 * checkpoint do that later.
412 */
413 mutex_lock(&dirty_i->seglist_lock);
414 if (test_bit(cpc->trim_start, dirty_i->dirty_segmap[PRE])) {
415 mutex_unlock(&dirty_i->seglist_lock);
416 cpc->trimmed += sbi->blocks_per_seg;
417 return;
418 }
419 mutex_unlock(&dirty_i->seglist_lock);
420
421 new = f2fs_kmem_cache_alloc(discard_entry_slab, GFP_NOFS);
422 INIT_LIST_HEAD(&new->list);
423 new->blkaddr = START_BLOCK(sbi, cpc->trim_start);
424 new->len = sbi->blocks_per_seg;
425 list_add_tail(&new->list, head);
426 SM_I(sbi)->nr_discards += sbi->blocks_per_seg;
427 cpc->trimmed += sbi->blocks_per_seg;
428 return;
429 }
430
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900431 /* zero block will be discarded through the prefree list */
432 if (!se->valid_blocks || se->valid_blocks == max_blocks)
433 return;
434
435 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
436 for (i = 0; i < entries; i++)
437 dmap[i] = (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
438
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700439 while (force || SM_I(sbi)->nr_discards <= SM_I(sbi)->max_discards) {
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900440 start = __find_rev_next_bit(dmap, max_blocks, end + 1);
441 if (start >= max_blocks)
442 break;
443
444 end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
445
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700446 if (end - start < cpc->trim_minlen)
447 continue;
448
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900449 new = f2fs_kmem_cache_alloc(discard_entry_slab, GFP_NOFS);
450 INIT_LIST_HEAD(&new->list);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700451 new->blkaddr = START_BLOCK(sbi, cpc->trim_start) + start;
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900452 new->len = end - start;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700453 cpc->trimmed += end - start;
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900454
455 list_add_tail(&new->list, head);
456 SM_I(sbi)->nr_discards += end - start;
457 }
458}
459
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700460void release_discard_addrs(struct f2fs_sb_info *sbi)
461{
462 struct list_head *head = &(SM_I(sbi)->discard_list);
463 struct discard_entry *entry, *this;
464
465 /* drop caches */
466 list_for_each_entry_safe(entry, this, head, list) {
467 list_del(&entry->list);
468 kmem_cache_free(discard_entry_slab, entry);
469 }
470}
471
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900472/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900473 * Should call clear_prefree_segments after checkpoint is done.
474 */
475static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
476{
477 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Chao Yub65ee142014-08-04 10:10:07 +0800478 unsigned int segno;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900479 unsigned int total_segs = TOTAL_SEGS(sbi);
480
481 mutex_lock(&dirty_i->seglist_lock);
Chao Yub65ee142014-08-04 10:10:07 +0800482 for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], total_segs)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900483 __set_test_and_free(sbi, segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900484 mutex_unlock(&dirty_i->seglist_lock);
485}
486
487void clear_prefree_segments(struct f2fs_sb_info *sbi)
488{
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900489 struct list_head *head = &(SM_I(sbi)->discard_list);
Chao Yu2d7b8222014-03-29 11:33:17 +0800490 struct discard_entry *entry, *this;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900491 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Changman Lee29e59c12013-11-11 09:24:37 +0900492 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900493 unsigned int total_segs = TOTAL_SEGS(sbi);
Changman Lee29e59c12013-11-11 09:24:37 +0900494 unsigned int start = 0, end = -1;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900495
496 mutex_lock(&dirty_i->seglist_lock);
Changman Lee29e59c12013-11-11 09:24:37 +0900497
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900498 while (1) {
Changman Lee29e59c12013-11-11 09:24:37 +0900499 int i;
500 start = find_next_bit(prefree_map, total_segs, end + 1);
501 if (start >= total_segs)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900502 break;
Changman Lee29e59c12013-11-11 09:24:37 +0900503 end = find_next_zero_bit(prefree_map, total_segs, start + 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900504
Changman Lee29e59c12013-11-11 09:24:37 +0900505 for (i = start; i < end; i++)
506 clear_bit(i, prefree_map);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900507
Changman Lee29e59c12013-11-11 09:24:37 +0900508 dirty_i->nr_dirty[PRE] -= end - start;
509
510 if (!test_opt(sbi, DISCARD))
511 continue;
512
Jaegeuk Kim37208872013-11-12 16:55:17 +0900513 f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
514 (end - start) << sbi->log_blocks_per_seg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900515 }
516 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900517
518 /* send small discards */
Chao Yu2d7b8222014-03-29 11:33:17 +0800519 list_for_each_entry_safe(entry, this, head, list) {
Jaegeuk Kim37208872013-11-12 16:55:17 +0900520 f2fs_issue_discard(sbi, entry->blkaddr, entry->len);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900521 list_del(&entry->list);
522 SM_I(sbi)->nr_discards -= entry->len;
523 kmem_cache_free(discard_entry_slab, entry);
524 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900525}
526
Chao Yu184a5cd2014-09-04 18:13:01 +0800527static bool __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900528{
529 struct sit_info *sit_i = SIT_I(sbi);
Chao Yu184a5cd2014-09-04 18:13:01 +0800530
531 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900532 sit_i->dirty_sentries++;
Chao Yu184a5cd2014-09-04 18:13:01 +0800533 return false;
534 }
535
536 return true;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900537}
538
539static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
540 unsigned int segno, int modified)
541{
542 struct seg_entry *se = get_seg_entry(sbi, segno);
543 se->type = type;
544 if (modified)
545 __mark_sit_entry_dirty(sbi, segno);
546}
547
548static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
549{
550 struct seg_entry *se;
551 unsigned int segno, offset;
552 long int new_vblocks;
553
554 segno = GET_SEGNO(sbi, blkaddr);
555
556 se = get_seg_entry(sbi, segno);
557 new_vblocks = se->valid_blocks + del;
Jaegeuk Kim491c0852014-02-04 13:01:10 +0900558 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900559
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700560 f2fs_bug_on(sbi, (new_vblocks >> (sizeof(unsigned short) << 3) ||
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900561 (new_vblocks > sbi->blocks_per_seg)));
562
563 se->valid_blocks = new_vblocks;
564 se->mtime = get_mtime(sbi);
565 SIT_I(sbi)->max_mtime = se->mtime;
566
567 /* Update valid block bitmap */
568 if (del > 0) {
569 if (f2fs_set_bit(offset, se->cur_valid_map))
Jaegeuk Kim05796762014-09-02 16:05:00 -0700570 f2fs_bug_on(sbi, 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900571 } else {
572 if (!f2fs_clear_bit(offset, se->cur_valid_map))
Jaegeuk Kim05796762014-09-02 16:05:00 -0700573 f2fs_bug_on(sbi, 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900574 }
575 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
576 se->ckpt_valid_blocks += del;
577
578 __mark_sit_entry_dirty(sbi, segno);
579
580 /* update total number of valid blocks to be written in ckpt area */
581 SIT_I(sbi)->written_valid_blocks += del;
582
583 if (sbi->segs_per_sec > 1)
584 get_sec_entry(sbi, segno)->valid_blocks += del;
585}
586
Jaegeuk Kim5e443812014-01-28 12:22:14 +0900587void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900588{
Jaegeuk Kim5e443812014-01-28 12:22:14 +0900589 update_sit_entry(sbi, new, 1);
590 if (GET_SEGNO(sbi, old) != NULL_SEGNO)
591 update_sit_entry(sbi, old, -1);
592
593 locate_dirty_segment(sbi, GET_SEGNO(sbi, old));
594 locate_dirty_segment(sbi, GET_SEGNO(sbi, new));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900595}
596
597void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
598{
599 unsigned int segno = GET_SEGNO(sbi, addr);
600 struct sit_info *sit_i = SIT_I(sbi);
601
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700602 f2fs_bug_on(sbi, addr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900603 if (addr == NEW_ADDR)
604 return;
605
606 /* add it into sit main buffer */
607 mutex_lock(&sit_i->sentry_lock);
608
609 update_sit_entry(sbi, addr, -1);
610
611 /* add it into dirty seglist */
612 locate_dirty_segment(sbi, segno);
613
614 mutex_unlock(&sit_i->sentry_lock);
615}
616
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900617/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900618 * This function should be resided under the curseg_mutex lock
619 */
620static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
Haicheng Lie79efe32013-06-13 16:59:27 +0800621 struct f2fs_summary *sum)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900622{
623 struct curseg_info *curseg = CURSEG_I(sbi, type);
624 void *addr = curseg->sum_blk;
Haicheng Lie79efe32013-06-13 16:59:27 +0800625 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900626 memcpy(addr, sum, sizeof(struct f2fs_summary));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900627}
628
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900629/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900630 * Calculate the number of current summary pages for writing
631 */
632int npages_for_summary_flush(struct f2fs_sb_info *sbi)
633{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900634 int valid_sum_count = 0;
Fan Li9a479382013-10-29 16:21:47 +0800635 int i, sum_in_page;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900636
637 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
638 if (sbi->ckpt->alloc_type[i] == SSR)
639 valid_sum_count += sbi->blocks_per_seg;
640 else
641 valid_sum_count += curseg_blkoff(sbi, i);
642 }
643
Fan Li9a479382013-10-29 16:21:47 +0800644 sum_in_page = (PAGE_CACHE_SIZE - 2 * SUM_JOURNAL_SIZE -
645 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
646 if (valid_sum_count <= sum_in_page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900647 return 1;
Fan Li9a479382013-10-29 16:21:47 +0800648 else if ((valid_sum_count - sum_in_page) <=
649 (PAGE_CACHE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900650 return 2;
651 return 3;
652}
653
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900654/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900655 * Caller should put this summary page
656 */
657struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
658{
659 return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
660}
661
662static void write_sum_page(struct f2fs_sb_info *sbi,
663 struct f2fs_summary_block *sum_blk, block_t blk_addr)
664{
665 struct page *page = grab_meta_page(sbi, blk_addr);
666 void *kaddr = page_address(page);
667 memcpy(kaddr, sum_blk, PAGE_CACHE_SIZE);
668 set_page_dirty(page);
669 f2fs_put_page(page, 1);
670}
671
Jaegeuk Kim60374682013-03-31 13:58:51 +0900672static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
673{
674 struct curseg_info *curseg = CURSEG_I(sbi, type);
Haicheng Li81fb5e82013-05-14 18:20:28 +0800675 unsigned int segno = curseg->segno + 1;
Jaegeuk Kim60374682013-03-31 13:58:51 +0900676 struct free_segmap_info *free_i = FREE_I(sbi);
677
Haicheng Li81fb5e82013-05-14 18:20:28 +0800678 if (segno < TOTAL_SEGS(sbi) && segno % sbi->segs_per_sec)
679 return !test_bit(segno, free_i->free_segmap);
Jaegeuk Kim60374682013-03-31 13:58:51 +0900680 return 0;
681}
682
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900683/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900684 * Find a new segment from the free segments bitmap to right order
685 * This function should be returned with success, otherwise BUG
686 */
687static void get_new_segment(struct f2fs_sb_info *sbi,
688 unsigned int *newseg, bool new_sec, int dir)
689{
690 struct free_segmap_info *free_i = FREE_I(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900691 unsigned int segno, secno, zoneno;
Jaegeuk Kim53cf9522013-03-31 12:39:49 +0900692 unsigned int total_zones = TOTAL_SECS(sbi) / sbi->secs_per_zone;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900693 unsigned int hint = *newseg / sbi->segs_per_sec;
694 unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
695 unsigned int left_start = hint;
696 bool init = true;
697 int go_left = 0;
698 int i;
699
700 write_lock(&free_i->segmap_lock);
701
702 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
703 segno = find_next_zero_bit(free_i->free_segmap,
704 TOTAL_SEGS(sbi), *newseg + 1);
Jaegeuk Kim33afa7f2013-03-31 12:59:53 +0900705 if (segno - *newseg < sbi->segs_per_sec -
706 (*newseg % sbi->segs_per_sec))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900707 goto got_it;
708 }
709find_other_zone:
Jaegeuk Kim53cf9522013-03-31 12:39:49 +0900710 secno = find_next_zero_bit(free_i->free_secmap, TOTAL_SECS(sbi), hint);
711 if (secno >= TOTAL_SECS(sbi)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900712 if (dir == ALLOC_RIGHT) {
713 secno = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim53cf9522013-03-31 12:39:49 +0900714 TOTAL_SECS(sbi), 0);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700715 f2fs_bug_on(sbi, secno >= TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900716 } else {
717 go_left = 1;
718 left_start = hint - 1;
719 }
720 }
721 if (go_left == 0)
722 goto skip_left;
723
724 while (test_bit(left_start, free_i->free_secmap)) {
725 if (left_start > 0) {
726 left_start--;
727 continue;
728 }
729 left_start = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim53cf9522013-03-31 12:39:49 +0900730 TOTAL_SECS(sbi), 0);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700731 f2fs_bug_on(sbi, left_start >= TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900732 break;
733 }
734 secno = left_start;
735skip_left:
736 hint = secno;
737 segno = secno * sbi->segs_per_sec;
738 zoneno = secno / sbi->secs_per_zone;
739
740 /* give up on finding another zone */
741 if (!init)
742 goto got_it;
743 if (sbi->secs_per_zone == 1)
744 goto got_it;
745 if (zoneno == old_zoneno)
746 goto got_it;
747 if (dir == ALLOC_LEFT) {
748 if (!go_left && zoneno + 1 >= total_zones)
749 goto got_it;
750 if (go_left && zoneno == 0)
751 goto got_it;
752 }
753 for (i = 0; i < NR_CURSEG_TYPE; i++)
754 if (CURSEG_I(sbi, i)->zone == zoneno)
755 break;
756
757 if (i < NR_CURSEG_TYPE) {
758 /* zone is in user, try another */
759 if (go_left)
760 hint = zoneno * sbi->secs_per_zone - 1;
761 else if (zoneno + 1 >= total_zones)
762 hint = 0;
763 else
764 hint = (zoneno + 1) * sbi->secs_per_zone;
765 init = false;
766 goto find_other_zone;
767 }
768got_it:
769 /* set it as dirty segment in free segmap */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700770 f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900771 __set_inuse(sbi, segno);
772 *newseg = segno;
773 write_unlock(&free_i->segmap_lock);
774}
775
776static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
777{
778 struct curseg_info *curseg = CURSEG_I(sbi, type);
779 struct summary_footer *sum_footer;
780
781 curseg->segno = curseg->next_segno;
782 curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
783 curseg->next_blkoff = 0;
784 curseg->next_segno = NULL_SEGNO;
785
786 sum_footer = &(curseg->sum_blk->footer);
787 memset(sum_footer, 0, sizeof(struct summary_footer));
788 if (IS_DATASEG(type))
789 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
790 if (IS_NODESEG(type))
791 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
792 __set_sit_entry_type(sbi, type, curseg->segno, modified);
793}
794
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900795/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900796 * Allocate a current working segment.
797 * This function always allocates a free segment in LFS manner.
798 */
799static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
800{
801 struct curseg_info *curseg = CURSEG_I(sbi, type);
802 unsigned int segno = curseg->segno;
803 int dir = ALLOC_LEFT;
804
805 write_sum_page(sbi, curseg->sum_blk,
Haicheng Li81fb5e82013-05-14 18:20:28 +0800806 GET_SUM_BLOCK(sbi, segno));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900807 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
808 dir = ALLOC_RIGHT;
809
810 if (test_opt(sbi, NOHEAP))
811 dir = ALLOC_RIGHT;
812
813 get_new_segment(sbi, &segno, new_sec, dir);
814 curseg->next_segno = segno;
815 reset_curseg(sbi, type, 1);
816 curseg->alloc_type = LFS;
817}
818
819static void __next_free_blkoff(struct f2fs_sb_info *sbi,
820 struct curseg_info *seg, block_t start)
821{
822 struct seg_entry *se = get_seg_entry(sbi, seg->segno);
Changman Leee81c93c2013-11-15 13:21:16 +0900823 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
824 unsigned long target_map[entries];
825 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
826 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
827 int i, pos;
828
829 for (i = 0; i < entries; i++)
830 target_map[i] = ckpt_map[i] | cur_map[i];
831
832 pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
833
834 seg->next_blkoff = pos;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900835}
836
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900837/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900838 * If a segment is written by LFS manner, next block offset is just obtained
839 * by increasing the current block offset. However, if a segment is written by
840 * SSR manner, next block offset obtained by calling __next_free_blkoff
841 */
842static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
843 struct curseg_info *seg)
844{
845 if (seg->alloc_type == SSR)
846 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
847 else
848 seg->next_blkoff++;
849}
850
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900851/*
arter97e1c42042014-08-06 23:22:50 +0900852 * This function always allocates a used segment(from dirty seglist) by SSR
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900853 * manner, so it should recover the existing segment information of valid blocks
854 */
855static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
856{
857 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
858 struct curseg_info *curseg = CURSEG_I(sbi, type);
859 unsigned int new_segno = curseg->next_segno;
860 struct f2fs_summary_block *sum_node;
861 struct page *sum_page;
862
863 write_sum_page(sbi, curseg->sum_blk,
864 GET_SUM_BLOCK(sbi, curseg->segno));
865 __set_test_and_inuse(sbi, new_segno);
866
867 mutex_lock(&dirty_i->seglist_lock);
868 __remove_dirty_segment(sbi, new_segno, PRE);
869 __remove_dirty_segment(sbi, new_segno, DIRTY);
870 mutex_unlock(&dirty_i->seglist_lock);
871
872 reset_curseg(sbi, type, 1);
873 curseg->alloc_type = SSR;
874 __next_free_blkoff(sbi, curseg, 0);
875
876 if (reuse) {
877 sum_page = get_sum_page(sbi, new_segno);
878 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
879 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
880 f2fs_put_page(sum_page, 1);
881 }
882}
883
Jaegeuk Kim43727522013-02-04 15:11:17 +0900884static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
885{
886 struct curseg_info *curseg = CURSEG_I(sbi, type);
887 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
888
889 if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0))
890 return v_ops->get_victim(sbi,
891 &(curseg)->next_segno, BG_GC, type, SSR);
892
893 /* For data segments, let's do SSR more intensively */
894 for (; type >= CURSEG_HOT_DATA; type--)
895 if (v_ops->get_victim(sbi, &(curseg)->next_segno,
896 BG_GC, type, SSR))
897 return 1;
898 return 0;
899}
900
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900901/*
902 * flush out current segment and replace it with new segment
903 * This function should be returned with success, otherwise BUG
904 */
905static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
906 int type, bool force)
907{
908 struct curseg_info *curseg = CURSEG_I(sbi, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900909
Gu Zheng7b405272013-08-19 09:41:15 +0800910 if (force)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900911 new_curseg(sbi, type, true);
Gu Zheng7b405272013-08-19 09:41:15 +0800912 else if (type == CURSEG_WARM_NODE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900913 new_curseg(sbi, type, false);
Jaegeuk Kim60374682013-03-31 13:58:51 +0900914 else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
915 new_curseg(sbi, type, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900916 else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
917 change_curseg(sbi, type, true);
918 else
919 new_curseg(sbi, type, false);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +0900920
921 stat_inc_seg_type(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900922}
923
924void allocate_new_segments(struct f2fs_sb_info *sbi)
925{
926 struct curseg_info *curseg;
927 unsigned int old_curseg;
928 int i;
929
930 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
931 curseg = CURSEG_I(sbi, i);
932 old_curseg = curseg->segno;
933 SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
934 locate_dirty_segment(sbi, old_curseg);
935 }
936}
937
938static const struct segment_allocation default_salloc_ops = {
939 .allocate_segment = allocate_segment_by_default,
940};
941
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700942int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range)
943{
944 block_t start_addr = SM_I(sbi)->main_blkaddr;
945 __u64 start = range->start >> sbi->log_blocksize;
946 __u64 end = start + (range->len >> sbi->log_blocksize) - 1;
947 __u64 segment = 1 << (sbi->log_blocksize + sbi->log_blocks_per_seg);
948 unsigned int start_segno, end_segno;
949 struct cp_control cpc;
950
951 if (range->minlen > segment ||
952 start >= SM_I(sbi)->seg0_blkaddr + TOTAL_BLKS(sbi) ||
953 range->len < sbi->blocksize)
954 return -EINVAL;
955
956 if (end <= start_addr)
957 goto out;
958
959 /* start/end segment number in main_area */
960 start_segno = (start <= start_addr) ? 0 : GET_SEGNO(sbi, start);
961 end_segno = (end >= SM_I(sbi)->seg0_blkaddr + TOTAL_BLKS(sbi)) ?
962 TOTAL_SEGS(sbi) - 1 : GET_SEGNO(sbi, end);
963
964 cpc.reason = CP_DISCARD;
965 cpc.trim_start = start_segno;
966 cpc.trim_end = end_segno;
967 cpc.trim_minlen = range->minlen >> sbi->log_blocksize;
968 cpc.trimmed = 0;
969
970 /* do checkpoint to issue discard commands safely */
971 write_checkpoint(sbi, &cpc);
972out:
973 range->len = cpc.trimmed << sbi->log_blocksize;
974 return 0;
975}
976
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900977static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
978{
979 struct curseg_info *curseg = CURSEG_I(sbi, type);
980 if (curseg->next_blkoff < sbi->blocks_per_seg)
981 return true;
982 return false;
983}
984
985static int __get_segment_type_2(struct page *page, enum page_type p_type)
986{
987 if (p_type == DATA)
988 return CURSEG_HOT_DATA;
989 else
990 return CURSEG_HOT_NODE;
991}
992
993static int __get_segment_type_4(struct page *page, enum page_type p_type)
994{
995 if (p_type == DATA) {
996 struct inode *inode = page->mapping->host;
997
998 if (S_ISDIR(inode->i_mode))
999 return CURSEG_HOT_DATA;
1000 else
1001 return CURSEG_COLD_DATA;
1002 } else {
1003 if (IS_DNODE(page) && !is_cold_node(page))
1004 return CURSEG_HOT_NODE;
1005 else
1006 return CURSEG_COLD_NODE;
1007 }
1008}
1009
1010static int __get_segment_type_6(struct page *page, enum page_type p_type)
1011{
1012 if (p_type == DATA) {
1013 struct inode *inode = page->mapping->host;
1014
1015 if (S_ISDIR(inode->i_mode))
1016 return CURSEG_HOT_DATA;
Jaegeuk Kim354a3392013-06-14 08:52:35 +09001017 else if (is_cold_data(page) || file_is_cold(inode))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001018 return CURSEG_COLD_DATA;
1019 else
1020 return CURSEG_WARM_DATA;
1021 } else {
1022 if (IS_DNODE(page))
1023 return is_cold_node(page) ? CURSEG_WARM_NODE :
1024 CURSEG_HOT_NODE;
1025 else
1026 return CURSEG_COLD_NODE;
1027 }
1028}
1029
1030static int __get_segment_type(struct page *page, enum page_type p_type)
1031{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001032 switch (F2FS_P_SB(page)->active_logs) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001033 case 2:
1034 return __get_segment_type_2(page, p_type);
1035 case 4:
1036 return __get_segment_type_4(page, p_type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001037 }
Jaegeuk Kim12a67142012-12-21 11:47:05 +09001038 /* NR_CURSEG_TYPE(6) logs by default */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001039 f2fs_bug_on(F2FS_P_SB(page),
1040 F2FS_P_SB(page)->active_logs != NR_CURSEG_TYPE);
Jaegeuk Kim12a67142012-12-21 11:47:05 +09001041 return __get_segment_type_6(page, p_type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001042}
1043
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001044void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
1045 block_t old_blkaddr, block_t *new_blkaddr,
1046 struct f2fs_summary *sum, int type)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001047{
1048 struct sit_info *sit_i = SIT_I(sbi);
1049 struct curseg_info *curseg;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001050
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001051 curseg = CURSEG_I(sbi, type);
1052
1053 mutex_lock(&curseg->curseg_mutex);
1054
1055 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001056
1057 /*
1058 * __add_sum_entry should be resided under the curseg_mutex
1059 * because, this function updates a summary entry in the
1060 * current summary block.
1061 */
Haicheng Lie79efe32013-06-13 16:59:27 +08001062 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001063
1064 mutex_lock(&sit_i->sentry_lock);
1065 __refresh_next_blkoff(sbi, curseg);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +09001066
1067 stat_inc_block_count(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001068
Jaegeuk Kim5e443812014-01-28 12:22:14 +09001069 if (!__has_curseg_space(sbi, type))
1070 sit_i->s_ops->allocate_segment(sbi, type, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001071 /*
1072 * SIT information should be updated before segment allocation,
1073 * since SSR needs latest valid block information.
1074 */
1075 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
Jaegeuk Kim5e443812014-01-28 12:22:14 +09001076
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001077 mutex_unlock(&sit_i->sentry_lock);
1078
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001079 if (page && IS_NODESEG(type))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001080 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
1081
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001082 mutex_unlock(&curseg->curseg_mutex);
1083}
1084
1085static void do_write_page(struct f2fs_sb_info *sbi, struct page *page,
1086 block_t old_blkaddr, block_t *new_blkaddr,
1087 struct f2fs_summary *sum, struct f2fs_io_info *fio)
1088{
1089 int type = __get_segment_type(page, fio->type);
1090
1091 allocate_data_block(sbi, page, old_blkaddr, new_blkaddr, sum, type);
1092
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001093 /* writeout dirty page into bdev */
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001094 f2fs_submit_page_mbio(sbi, page, *new_blkaddr, fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001095}
1096
Jaegeuk Kim577e3492013-01-24 19:56:11 +09001097void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001098{
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001099 struct f2fs_io_info fio = {
1100 .type = META,
Gu Zheng7e8f2302013-12-20 18:17:49 +08001101 .rw = WRITE_SYNC | REQ_META | REQ_PRIO
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001102 };
1103
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001104 set_page_writeback(page);
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001105 f2fs_submit_page_mbio(sbi, page, page->index, &fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001106}
1107
1108void write_node_page(struct f2fs_sb_info *sbi, struct page *page,
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001109 struct f2fs_io_info *fio,
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001110 unsigned int nid, block_t old_blkaddr, block_t *new_blkaddr)
1111{
1112 struct f2fs_summary sum;
1113 set_summary(&sum, nid, 0, 0);
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001114 do_write_page(sbi, page, old_blkaddr, new_blkaddr, &sum, fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001115}
1116
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001117void write_data_page(struct page *page, struct dnode_of_data *dn,
1118 block_t *new_blkaddr, struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001119{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001120 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001121 struct f2fs_summary sum;
1122 struct node_info ni;
1123
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001124 f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001125 get_node_info(sbi, dn->nid, &ni);
1126 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1127
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001128 do_write_page(sbi, page, dn->data_blkaddr, new_blkaddr, &sum, fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001129}
1130
Chris Fries6c311ec2014-01-17 14:44:39 -06001131void rewrite_data_page(struct page *page, block_t old_blkaddr,
1132 struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001133{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001134 f2fs_submit_page_mbio(F2FS_P_SB(page), page, old_blkaddr, fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001135}
1136
1137void recover_data_page(struct f2fs_sb_info *sbi,
1138 struct page *page, struct f2fs_summary *sum,
1139 block_t old_blkaddr, block_t new_blkaddr)
1140{
1141 struct sit_info *sit_i = SIT_I(sbi);
1142 struct curseg_info *curseg;
1143 unsigned int segno, old_cursegno;
1144 struct seg_entry *se;
1145 int type;
1146
1147 segno = GET_SEGNO(sbi, new_blkaddr);
1148 se = get_seg_entry(sbi, segno);
1149 type = se->type;
1150
1151 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
1152 if (old_blkaddr == NULL_ADDR)
1153 type = CURSEG_COLD_DATA;
1154 else
1155 type = CURSEG_WARM_DATA;
1156 }
1157 curseg = CURSEG_I(sbi, type);
1158
1159 mutex_lock(&curseg->curseg_mutex);
1160 mutex_lock(&sit_i->sentry_lock);
1161
1162 old_cursegno = curseg->segno;
1163
1164 /* change the current segment */
1165 if (segno != curseg->segno) {
1166 curseg->next_segno = segno;
1167 change_curseg(sbi, type, true);
1168 }
1169
Jaegeuk Kim491c0852014-02-04 13:01:10 +09001170 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
Haicheng Lie79efe32013-06-13 16:59:27 +08001171 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001172
1173 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001174 locate_dirty_segment(sbi, old_cursegno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001175
1176 mutex_unlock(&sit_i->sentry_lock);
1177 mutex_unlock(&curseg->curseg_mutex);
1178}
1179
Chao Yudf0f8dc2014-03-22 14:57:23 +08001180static inline bool is_merged_page(struct f2fs_sb_info *sbi,
1181 struct page *page, enum page_type type)
1182{
1183 enum page_type btype = PAGE_TYPE_OF_BIO(type);
1184 struct f2fs_bio_info *io = &sbi->write_io[btype];
Chao Yudf0f8dc2014-03-22 14:57:23 +08001185 struct bio_vec *bvec;
1186 int i;
1187
1188 down_read(&io->io_rwsem);
Jaegeuk Kimce234472014-04-02 09:04:42 +09001189 if (!io->bio)
Chao Yudf0f8dc2014-03-22 14:57:23 +08001190 goto out;
1191
Jaegeuk Kimce234472014-04-02 09:04:42 +09001192 bio_for_each_segment_all(bvec, io->bio, i) {
Chao Yudf0f8dc2014-03-22 14:57:23 +08001193 if (page == bvec->bv_page) {
1194 up_read(&io->io_rwsem);
1195 return true;
1196 }
1197 }
1198
1199out:
1200 up_read(&io->io_rwsem);
1201 return false;
1202}
1203
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001204void f2fs_wait_on_page_writeback(struct page *page,
Yuan Zhong5514f0a2014-01-10 07:26:14 +00001205 enum page_type type)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001206{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001207 if (PageWriteback(page)) {
Jaegeuk Kim40813632014-09-02 15:31:18 -07001208 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
1209
Chao Yudf0f8dc2014-03-22 14:57:23 +08001210 if (is_merged_page(sbi, page, type))
1211 f2fs_submit_merged_bio(sbi, type, WRITE);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001212 wait_on_page_writeback(page);
1213 }
1214}
1215
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001216static int read_compacted_summaries(struct f2fs_sb_info *sbi)
1217{
1218 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1219 struct curseg_info *seg_i;
1220 unsigned char *kaddr;
1221 struct page *page;
1222 block_t start;
1223 int i, j, offset;
1224
1225 start = start_sum_block(sbi);
1226
1227 page = get_meta_page(sbi, start++);
1228 kaddr = (unsigned char *)page_address(page);
1229
1230 /* Step 1: restore nat cache */
1231 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1232 memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
1233
1234 /* Step 2: restore sit cache */
1235 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1236 memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
1237 SUM_JOURNAL_SIZE);
1238 offset = 2 * SUM_JOURNAL_SIZE;
1239
1240 /* Step 3: restore summary entries */
1241 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1242 unsigned short blk_off;
1243 unsigned int segno;
1244
1245 seg_i = CURSEG_I(sbi, i);
1246 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
1247 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
1248 seg_i->next_segno = segno;
1249 reset_curseg(sbi, i, 0);
1250 seg_i->alloc_type = ckpt->alloc_type[i];
1251 seg_i->next_blkoff = blk_off;
1252
1253 if (seg_i->alloc_type == SSR)
1254 blk_off = sbi->blocks_per_seg;
1255
1256 for (j = 0; j < blk_off; j++) {
1257 struct f2fs_summary *s;
1258 s = (struct f2fs_summary *)(kaddr + offset);
1259 seg_i->sum_blk->entries[j] = *s;
1260 offset += SUMMARY_SIZE;
1261 if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1262 SUM_FOOTER_SIZE)
1263 continue;
1264
1265 f2fs_put_page(page, 1);
1266 page = NULL;
1267
1268 page = get_meta_page(sbi, start++);
1269 kaddr = (unsigned char *)page_address(page);
1270 offset = 0;
1271 }
1272 }
1273 f2fs_put_page(page, 1);
1274 return 0;
1275}
1276
1277static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
1278{
1279 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1280 struct f2fs_summary_block *sum;
1281 struct curseg_info *curseg;
1282 struct page *new;
1283 unsigned short blk_off;
1284 unsigned int segno = 0;
1285 block_t blk_addr = 0;
1286
1287 /* get segment number and block addr */
1288 if (IS_DATASEG(type)) {
1289 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
1290 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
1291 CURSEG_HOT_DATA]);
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001292 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001293 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
1294 else
1295 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
1296 } else {
1297 segno = le32_to_cpu(ckpt->cur_node_segno[type -
1298 CURSEG_HOT_NODE]);
1299 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
1300 CURSEG_HOT_NODE]);
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001301 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001302 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
1303 type - CURSEG_HOT_NODE);
1304 else
1305 blk_addr = GET_SUM_BLOCK(sbi, segno);
1306 }
1307
1308 new = get_meta_page(sbi, blk_addr);
1309 sum = (struct f2fs_summary_block *)page_address(new);
1310
1311 if (IS_NODESEG(type)) {
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001312 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001313 struct f2fs_summary *ns = &sum->entries[0];
1314 int i;
1315 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
1316 ns->version = 0;
1317 ns->ofs_in_node = 0;
1318 }
1319 } else {
Gu Zhengd6537882014-03-07 18:43:36 +08001320 int err;
1321
1322 err = restore_node_summary(sbi, segno, sum);
1323 if (err) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001324 f2fs_put_page(new, 1);
Gu Zhengd6537882014-03-07 18:43:36 +08001325 return err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001326 }
1327 }
1328 }
1329
1330 /* set uncompleted segment to curseg */
1331 curseg = CURSEG_I(sbi, type);
1332 mutex_lock(&curseg->curseg_mutex);
1333 memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
1334 curseg->next_segno = segno;
1335 reset_curseg(sbi, type, 0);
1336 curseg->alloc_type = ckpt->alloc_type[type];
1337 curseg->next_blkoff = blk_off;
1338 mutex_unlock(&curseg->curseg_mutex);
1339 f2fs_put_page(new, 1);
1340 return 0;
1341}
1342
1343static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
1344{
1345 int type = CURSEG_HOT_DATA;
Chao Yue4fc5fb2014-03-17 16:36:24 +08001346 int err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001347
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001348 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001349 /* restore for compacted data summary */
1350 if (read_compacted_summaries(sbi))
1351 return -EINVAL;
1352 type = CURSEG_HOT_NODE;
1353 }
1354
Chao Yue4fc5fb2014-03-17 16:36:24 +08001355 for (; type <= CURSEG_COLD_NODE; type++) {
1356 err = read_normal_summaries(sbi, type);
1357 if (err)
1358 return err;
1359 }
1360
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001361 return 0;
1362}
1363
1364static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
1365{
1366 struct page *page;
1367 unsigned char *kaddr;
1368 struct f2fs_summary *summary;
1369 struct curseg_info *seg_i;
1370 int written_size = 0;
1371 int i, j;
1372
1373 page = grab_meta_page(sbi, blkaddr++);
1374 kaddr = (unsigned char *)page_address(page);
1375
1376 /* Step 1: write nat cache */
1377 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1378 memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
1379 written_size += SUM_JOURNAL_SIZE;
1380
1381 /* Step 2: write sit cache */
1382 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1383 memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
1384 SUM_JOURNAL_SIZE);
1385 written_size += SUM_JOURNAL_SIZE;
1386
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001387 /* Step 3: write summary entries */
1388 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1389 unsigned short blkoff;
1390 seg_i = CURSEG_I(sbi, i);
1391 if (sbi->ckpt->alloc_type[i] == SSR)
1392 blkoff = sbi->blocks_per_seg;
1393 else
1394 blkoff = curseg_blkoff(sbi, i);
1395
1396 for (j = 0; j < blkoff; j++) {
1397 if (!page) {
1398 page = grab_meta_page(sbi, blkaddr++);
1399 kaddr = (unsigned char *)page_address(page);
1400 written_size = 0;
1401 }
1402 summary = (struct f2fs_summary *)(kaddr + written_size);
1403 *summary = seg_i->sum_blk->entries[j];
1404 written_size += SUMMARY_SIZE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001405
1406 if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1407 SUM_FOOTER_SIZE)
1408 continue;
1409
Chao Yue8d61a72013-10-24 15:08:28 +08001410 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001411 f2fs_put_page(page, 1);
1412 page = NULL;
1413 }
1414 }
Chao Yue8d61a72013-10-24 15:08:28 +08001415 if (page) {
1416 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001417 f2fs_put_page(page, 1);
Chao Yue8d61a72013-10-24 15:08:28 +08001418 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001419}
1420
1421static void write_normal_summaries(struct f2fs_sb_info *sbi,
1422 block_t blkaddr, int type)
1423{
1424 int i, end;
1425 if (IS_DATASEG(type))
1426 end = type + NR_CURSEG_DATA_TYPE;
1427 else
1428 end = type + NR_CURSEG_NODE_TYPE;
1429
1430 for (i = type; i < end; i++) {
1431 struct curseg_info *sum = CURSEG_I(sbi, i);
1432 mutex_lock(&sum->curseg_mutex);
1433 write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
1434 mutex_unlock(&sum->curseg_mutex);
1435 }
1436}
1437
1438void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1439{
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001440 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001441 write_compacted_summaries(sbi, start_blk);
1442 else
1443 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
1444}
1445
1446void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1447{
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001448 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001449 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001450}
1451
1452int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
1453 unsigned int val, int alloc)
1454{
1455 int i;
1456
1457 if (type == NAT_JOURNAL) {
1458 for (i = 0; i < nats_in_cursum(sum); i++) {
1459 if (le32_to_cpu(nid_in_journal(sum, i)) == val)
1460 return i;
1461 }
1462 if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
1463 return update_nats_in_cursum(sum, 1);
1464 } else if (type == SIT_JOURNAL) {
1465 for (i = 0; i < sits_in_cursum(sum); i++)
1466 if (le32_to_cpu(segno_in_journal(sum, i)) == val)
1467 return i;
1468 if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
1469 return update_sits_in_cursum(sum, 1);
1470 }
1471 return -1;
1472}
1473
1474static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
1475 unsigned int segno)
1476{
1477 struct sit_info *sit_i = SIT_I(sbi);
Chao Yud3a14af2014-09-04 18:11:47 +08001478 unsigned int offset = SIT_BLOCK_OFFSET(segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001479 block_t blk_addr = sit_i->sit_base_addr + offset;
1480
1481 check_seg_range(sbi, segno);
1482
1483 /* calculate sit block address */
1484 if (f2fs_test_bit(offset, sit_i->sit_bitmap))
1485 blk_addr += sit_i->sit_blocks;
1486
1487 return get_meta_page(sbi, blk_addr);
1488}
1489
1490static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
1491 unsigned int start)
1492{
1493 struct sit_info *sit_i = SIT_I(sbi);
1494 struct page *src_page, *dst_page;
1495 pgoff_t src_off, dst_off;
1496 void *src_addr, *dst_addr;
1497
1498 src_off = current_sit_addr(sbi, start);
1499 dst_off = next_sit_addr(sbi, src_off);
1500
1501 /* get current sit block page without lock */
1502 src_page = get_meta_page(sbi, src_off);
1503 dst_page = grab_meta_page(sbi, dst_off);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001504 f2fs_bug_on(sbi, PageDirty(src_page));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001505
1506 src_addr = page_address(src_page);
1507 dst_addr = page_address(dst_page);
1508 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
1509
1510 set_page_dirty(dst_page);
1511 f2fs_put_page(src_page, 1);
1512
1513 set_to_next_sit(sit_i, start);
1514
1515 return dst_page;
1516}
1517
Chao Yu184a5cd2014-09-04 18:13:01 +08001518static struct sit_entry_set *grab_sit_entry_set(void)
1519{
1520 struct sit_entry_set *ses =
1521 f2fs_kmem_cache_alloc(sit_entry_set_slab, GFP_ATOMIC);
1522
1523 ses->entry_cnt = 0;
1524 INIT_LIST_HEAD(&ses->set_list);
1525 return ses;
1526}
1527
1528static void release_sit_entry_set(struct sit_entry_set *ses)
1529{
1530 list_del(&ses->set_list);
1531 kmem_cache_free(sit_entry_set_slab, ses);
1532}
1533
1534static void adjust_sit_entry_set(struct sit_entry_set *ses,
1535 struct list_head *head)
1536{
1537 struct sit_entry_set *next = ses;
1538
1539 if (list_is_last(&ses->set_list, head))
1540 return;
1541
1542 list_for_each_entry_continue(next, head, set_list)
1543 if (ses->entry_cnt <= next->entry_cnt)
1544 break;
1545
1546 list_move_tail(&ses->set_list, &next->set_list);
1547}
1548
1549static void add_sit_entry(unsigned int segno, struct list_head *head)
1550{
1551 struct sit_entry_set *ses;
1552 unsigned int start_segno = START_SEGNO(segno);
1553
1554 list_for_each_entry(ses, head, set_list) {
1555 if (ses->start_segno == start_segno) {
1556 ses->entry_cnt++;
1557 adjust_sit_entry_set(ses, head);
1558 return;
1559 }
1560 }
1561
1562 ses = grab_sit_entry_set();
1563
1564 ses->start_segno = start_segno;
1565 ses->entry_cnt++;
1566 list_add(&ses->set_list, head);
1567}
1568
1569static void add_sits_in_set(struct f2fs_sb_info *sbi)
1570{
1571 struct f2fs_sm_info *sm_info = SM_I(sbi);
1572 struct list_head *set_list = &sm_info->sit_entry_set;
1573 unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap;
1574 unsigned long nsegs = TOTAL_SEGS(sbi);
1575 unsigned int segno;
1576
1577 for_each_set_bit(segno, bitmap, nsegs)
1578 add_sit_entry(segno, set_list);
1579}
1580
1581static void remove_sits_in_journal(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001582{
1583 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1584 struct f2fs_summary_block *sum = curseg->sum_blk;
1585 int i;
1586
Chao Yu184a5cd2014-09-04 18:13:01 +08001587 for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
1588 unsigned int segno;
1589 bool dirtied;
1590
1591 segno = le32_to_cpu(segno_in_journal(sum, i));
1592 dirtied = __mark_sit_entry_dirty(sbi, segno);
1593
1594 if (!dirtied)
1595 add_sit_entry(segno, &SM_I(sbi)->sit_entry_set);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001596 }
Chao Yu184a5cd2014-09-04 18:13:01 +08001597 update_sits_in_cursum(sum, -sits_in_cursum(sum));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001598}
1599
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001600/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001601 * CP calls this function, which flushes SIT entries including sit_journal,
1602 * and moves prefree segs to free segs.
1603 */
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001604void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001605{
1606 struct sit_info *sit_i = SIT_I(sbi);
1607 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
1608 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1609 struct f2fs_summary_block *sum = curseg->sum_blk;
Chao Yu184a5cd2014-09-04 18:13:01 +08001610 struct sit_entry_set *ses, *tmp;
1611 struct list_head *head = &SM_I(sbi)->sit_entry_set;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001612 unsigned long nsegs = TOTAL_SEGS(sbi);
Chao Yu184a5cd2014-09-04 18:13:01 +08001613 bool to_journal = true;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001614 struct seg_entry *se;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001615
1616 mutex_lock(&curseg->curseg_mutex);
1617 mutex_lock(&sit_i->sentry_lock);
1618
1619 /*
Chao Yu184a5cd2014-09-04 18:13:01 +08001620 * add and account sit entries of dirty bitmap in sit entry
1621 * set temporarily
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001622 */
Chao Yu184a5cd2014-09-04 18:13:01 +08001623 add_sits_in_set(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001624
Chao Yu184a5cd2014-09-04 18:13:01 +08001625 /*
1626 * if there are no enough space in journal to store dirty sit
1627 * entries, remove all entries from journal and add and account
1628 * them in sit entry set.
1629 */
1630 if (!__has_cursum_space(sum, sit_i->dirty_sentries, SIT_JOURNAL))
1631 remove_sits_in_journal(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001632
Chao Yu184a5cd2014-09-04 18:13:01 +08001633 if (!sit_i->dirty_sentries)
1634 goto out;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001635
Chao Yu184a5cd2014-09-04 18:13:01 +08001636 /*
1637 * there are two steps to flush sit entries:
1638 * #1, flush sit entries to journal in current cold data summary block.
1639 * #2, flush sit entries to sit page.
1640 */
1641 list_for_each_entry_safe(ses, tmp, head, set_list) {
1642 struct page *page;
1643 struct f2fs_sit_block *raw_sit = NULL;
1644 unsigned int start_segno = ses->start_segno;
1645 unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK,
1646 nsegs);
1647 unsigned int segno = start_segno;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001648
Chao Yu184a5cd2014-09-04 18:13:01 +08001649 if (to_journal &&
1650 !__has_cursum_space(sum, ses->entry_cnt, SIT_JOURNAL))
1651 to_journal = false;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001652
Chao Yu184a5cd2014-09-04 18:13:01 +08001653 if (!to_journal) {
1654 page = get_next_sit_page(sbi, start_segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001655 raw_sit = page_address(page);
1656 }
1657
Chao Yu184a5cd2014-09-04 18:13:01 +08001658 /* flush dirty sit entries in region of current sit set */
1659 for_each_set_bit_from(segno, bitmap, end) {
1660 int offset, sit_offset;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001661
1662 se = get_seg_entry(sbi, segno);
Chao Yu184a5cd2014-09-04 18:13:01 +08001663
1664 /* add discard candidates */
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001665 if (SM_I(sbi)->nr_discards < SM_I(sbi)->max_discards) {
1666 cpc->trim_start = segno;
1667 add_discard_addrs(sbi, cpc);
1668 }
Chao Yu184a5cd2014-09-04 18:13:01 +08001669
1670 if (to_journal) {
1671 offset = lookup_journal_in_cursum(sum,
1672 SIT_JOURNAL, segno, 1);
1673 f2fs_bug_on(sbi, offset < 0);
1674 segno_in_journal(sum, offset) =
1675 cpu_to_le32(segno);
1676 seg_info_to_raw_sit(se,
1677 &sit_in_journal(sum, offset));
1678 } else {
1679 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
1680 seg_info_to_raw_sit(se,
1681 &raw_sit->entries[sit_offset]);
1682 }
1683
1684 __clear_bit(segno, bitmap);
1685 sit_i->dirty_sentries--;
1686 ses->entry_cnt--;
1687 }
1688
1689 if (!to_journal)
1690 f2fs_put_page(page, 1);
1691
1692 f2fs_bug_on(sbi, ses->entry_cnt);
1693 release_sit_entry_set(ses);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001694 }
Chao Yu184a5cd2014-09-04 18:13:01 +08001695
1696 f2fs_bug_on(sbi, !list_empty(head));
1697 f2fs_bug_on(sbi, sit_i->dirty_sentries);
Chao Yu184a5cd2014-09-04 18:13:01 +08001698out:
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001699 if (cpc->reason == CP_DISCARD) {
1700 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++)
1701 add_discard_addrs(sbi, cpc);
1702 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001703 mutex_unlock(&sit_i->sentry_lock);
1704 mutex_unlock(&curseg->curseg_mutex);
1705
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001706 set_prefree_as_free_segments(sbi);
1707}
1708
1709static int build_sit_info(struct f2fs_sb_info *sbi)
1710{
1711 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1712 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1713 struct sit_info *sit_i;
1714 unsigned int sit_segs, start;
1715 char *src_bitmap, *dst_bitmap;
1716 unsigned int bitmap_size;
1717
1718 /* allocate memory for SIT information */
1719 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
1720 if (!sit_i)
1721 return -ENOMEM;
1722
1723 SM_I(sbi)->sit_info = sit_i;
1724
1725 sit_i->sentries = vzalloc(TOTAL_SEGS(sbi) * sizeof(struct seg_entry));
1726 if (!sit_i->sentries)
1727 return -ENOMEM;
1728
1729 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1730 sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1731 if (!sit_i->dirty_sentries_bitmap)
1732 return -ENOMEM;
1733
1734 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1735 sit_i->sentries[start].cur_valid_map
1736 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1737 sit_i->sentries[start].ckpt_valid_map
1738 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1739 if (!sit_i->sentries[start].cur_valid_map
1740 || !sit_i->sentries[start].ckpt_valid_map)
1741 return -ENOMEM;
1742 }
1743
1744 if (sbi->segs_per_sec > 1) {
Jaegeuk Kim53cf9522013-03-31 12:39:49 +09001745 sit_i->sec_entries = vzalloc(TOTAL_SECS(sbi) *
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001746 sizeof(struct sec_entry));
1747 if (!sit_i->sec_entries)
1748 return -ENOMEM;
1749 }
1750
1751 /* get information related with SIT */
1752 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
1753
1754 /* setup SIT bitmap from ckeckpoint pack */
1755 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
1756 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
1757
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001758 dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001759 if (!dst_bitmap)
1760 return -ENOMEM;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001761
1762 /* init SIT information */
1763 sit_i->s_ops = &default_salloc_ops;
1764
1765 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
1766 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
1767 sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
1768 sit_i->sit_bitmap = dst_bitmap;
1769 sit_i->bitmap_size = bitmap_size;
1770 sit_i->dirty_sentries = 0;
1771 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
1772 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
1773 sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
1774 mutex_init(&sit_i->sentry_lock);
1775 return 0;
1776}
1777
1778static int build_free_segmap(struct f2fs_sb_info *sbi)
1779{
1780 struct f2fs_sm_info *sm_info = SM_I(sbi);
1781 struct free_segmap_info *free_i;
1782 unsigned int bitmap_size, sec_bitmap_size;
1783
1784 /* allocate memory for free segmap information */
1785 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
1786 if (!free_i)
1787 return -ENOMEM;
1788
1789 SM_I(sbi)->free_info = free_i;
1790
1791 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1792 free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL);
1793 if (!free_i->free_segmap)
1794 return -ENOMEM;
1795
Jaegeuk Kim53cf9522013-03-31 12:39:49 +09001796 sec_bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001797 free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL);
1798 if (!free_i->free_secmap)
1799 return -ENOMEM;
1800
1801 /* set all segments as dirty temporarily */
1802 memset(free_i->free_segmap, 0xff, bitmap_size);
1803 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
1804
1805 /* init free segmap information */
1806 free_i->start_segno =
1807 (unsigned int) GET_SEGNO_FROM_SEG0(sbi, sm_info->main_blkaddr);
1808 free_i->free_segments = 0;
1809 free_i->free_sections = 0;
1810 rwlock_init(&free_i->segmap_lock);
1811 return 0;
1812}
1813
1814static int build_curseg(struct f2fs_sb_info *sbi)
1815{
Namjae Jeon1042d602012-12-01 10:56:13 +09001816 struct curseg_info *array;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001817 int i;
1818
Fabian Frederickb434bab2014-06-23 18:39:15 +02001819 array = kcalloc(NR_CURSEG_TYPE, sizeof(*array), GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001820 if (!array)
1821 return -ENOMEM;
1822
1823 SM_I(sbi)->curseg_array = array;
1824
1825 for (i = 0; i < NR_CURSEG_TYPE; i++) {
1826 mutex_init(&array[i].curseg_mutex);
1827 array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
1828 if (!array[i].sum_blk)
1829 return -ENOMEM;
1830 array[i].segno = NULL_SEGNO;
1831 array[i].next_blkoff = 0;
1832 }
1833 return restore_curseg_summaries(sbi);
1834}
1835
1836static void build_sit_entries(struct f2fs_sb_info *sbi)
1837{
1838 struct sit_info *sit_i = SIT_I(sbi);
1839 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1840 struct f2fs_summary_block *sum = curseg->sum_blk;
Chao Yu74de5932013-11-22 09:09:59 +08001841 int sit_blk_cnt = SIT_BLK_CNT(sbi);
1842 unsigned int i, start, end;
1843 unsigned int readed, start_blk = 0;
Jaegeuk Kim90a893c2014-09-22 16:21:07 -07001844 int nrpages = MAX_BIO_BLOCKS(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001845
Chao Yu74de5932013-11-22 09:09:59 +08001846 do {
Chao Yu662befd2014-02-07 16:11:53 +08001847 readed = ra_meta_pages(sbi, start_blk, nrpages, META_SIT);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001848
Chao Yu74de5932013-11-22 09:09:59 +08001849 start = start_blk * sit_i->sents_per_block;
1850 end = (start_blk + readed) * sit_i->sents_per_block;
1851
1852 for (; start < end && start < TOTAL_SEGS(sbi); start++) {
1853 struct seg_entry *se = &sit_i->sentries[start];
1854 struct f2fs_sit_block *sit_blk;
1855 struct f2fs_sit_entry sit;
1856 struct page *page;
1857
1858 mutex_lock(&curseg->curseg_mutex);
1859 for (i = 0; i < sits_in_cursum(sum); i++) {
Chris Fries6c311ec2014-01-17 14:44:39 -06001860 if (le32_to_cpu(segno_in_journal(sum, i))
1861 == start) {
Chao Yu74de5932013-11-22 09:09:59 +08001862 sit = sit_in_journal(sum, i);
1863 mutex_unlock(&curseg->curseg_mutex);
1864 goto got_it;
1865 }
1866 }
1867 mutex_unlock(&curseg->curseg_mutex);
1868
1869 page = get_current_sit_page(sbi, start);
1870 sit_blk = (struct f2fs_sit_block *)page_address(page);
1871 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
1872 f2fs_put_page(page, 1);
1873got_it:
1874 check_block_count(sbi, start, &sit);
1875 seg_info_from_raw_sit(se, &sit);
1876 if (sbi->segs_per_sec > 1) {
1877 struct sec_entry *e = get_sec_entry(sbi, start);
1878 e->valid_blocks += se->valid_blocks;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001879 }
1880 }
Chao Yu74de5932013-11-22 09:09:59 +08001881 start_blk += readed;
1882 } while (start_blk < sit_blk_cnt);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001883}
1884
1885static void init_free_segmap(struct f2fs_sb_info *sbi)
1886{
1887 unsigned int start;
1888 int type;
1889
1890 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1891 struct seg_entry *sentry = get_seg_entry(sbi, start);
1892 if (!sentry->valid_blocks)
1893 __set_free(sbi, start);
1894 }
1895
1896 /* set use the current segments */
1897 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
1898 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
1899 __set_test_and_inuse(sbi, curseg_t->segno);
1900 }
1901}
1902
1903static void init_dirty_segmap(struct f2fs_sb_info *sbi)
1904{
1905 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1906 struct free_segmap_info *free_i = FREE_I(sbi);
Namjae Jeon8736fbf2013-06-16 09:49:11 +09001907 unsigned int segno = 0, offset = 0, total_segs = TOTAL_SEGS(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001908 unsigned short valid_blocks;
1909
Namjae Jeon8736fbf2013-06-16 09:49:11 +09001910 while (1) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001911 /* find dirty segment based on free segmap */
Namjae Jeon8736fbf2013-06-16 09:49:11 +09001912 segno = find_next_inuse(free_i, total_segs, offset);
1913 if (segno >= total_segs)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001914 break;
1915 offset = segno + 1;
1916 valid_blocks = get_valid_blocks(sbi, segno, 0);
Jaegeuk Kimec325b52014-09-02 16:24:11 -07001917 if (valid_blocks == sbi->blocks_per_seg || !valid_blocks)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001918 continue;
Jaegeuk Kimec325b52014-09-02 16:24:11 -07001919 if (valid_blocks > sbi->blocks_per_seg) {
1920 f2fs_bug_on(sbi, 1);
1921 continue;
1922 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001923 mutex_lock(&dirty_i->seglist_lock);
1924 __locate_dirty_segment(sbi, segno, DIRTY);
1925 mutex_unlock(&dirty_i->seglist_lock);
1926 }
1927}
1928
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001929static int init_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001930{
1931 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001932 unsigned int bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001933
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001934 dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL);
1935 if (!dirty_i->victim_secmap)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001936 return -ENOMEM;
1937 return 0;
1938}
1939
1940static int build_dirty_segmap(struct f2fs_sb_info *sbi)
1941{
1942 struct dirty_seglist_info *dirty_i;
1943 unsigned int bitmap_size, i;
1944
1945 /* allocate memory for dirty segments list information */
1946 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
1947 if (!dirty_i)
1948 return -ENOMEM;
1949
1950 SM_I(sbi)->dirty_info = dirty_i;
1951 mutex_init(&dirty_i->seglist_lock);
1952
1953 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1954
1955 for (i = 0; i < NR_DIRTY_TYPE; i++) {
1956 dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001957 if (!dirty_i->dirty_segmap[i])
1958 return -ENOMEM;
1959 }
1960
1961 init_dirty_segmap(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001962 return init_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001963}
1964
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001965/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001966 * Update min, max modified time for cost-benefit GC algorithm
1967 */
1968static void init_min_max_mtime(struct f2fs_sb_info *sbi)
1969{
1970 struct sit_info *sit_i = SIT_I(sbi);
1971 unsigned int segno;
1972
1973 mutex_lock(&sit_i->sentry_lock);
1974
1975 sit_i->min_mtime = LLONG_MAX;
1976
1977 for (segno = 0; segno < TOTAL_SEGS(sbi); segno += sbi->segs_per_sec) {
1978 unsigned int i;
1979 unsigned long long mtime = 0;
1980
1981 for (i = 0; i < sbi->segs_per_sec; i++)
1982 mtime += get_seg_entry(sbi, segno + i)->mtime;
1983
1984 mtime = div_u64(mtime, sbi->segs_per_sec);
1985
1986 if (sit_i->min_mtime > mtime)
1987 sit_i->min_mtime = mtime;
1988 }
1989 sit_i->max_mtime = get_mtime(sbi);
1990 mutex_unlock(&sit_i->sentry_lock);
1991}
1992
1993int build_segment_manager(struct f2fs_sb_info *sbi)
1994{
1995 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1996 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Namjae Jeon1042d602012-12-01 10:56:13 +09001997 struct f2fs_sm_info *sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001998 int err;
1999
2000 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
2001 if (!sm_info)
2002 return -ENOMEM;
2003
2004 /* init sm info */
2005 sbi->sm_info = sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002006 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
2007 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
2008 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
2009 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
2010 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
2011 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
2012 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
Jaegeuk Kim58c41032014-03-19 14:17:21 +09002013 sm_info->rec_prefree_segments = sm_info->main_segments *
2014 DEF_RECLAIM_PREFREE_SEGMENTS / 100;
Jaegeuk Kim9b5f1362014-09-16 18:30:54 -07002015 sm_info->ipu_policy = 1 << F2FS_IPU_FSYNC;
Jaegeuk Kim216fbd62013-11-07 13:13:42 +09002016 sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -07002017 sm_info->min_fsync_blocks = DEF_MIN_FSYNC_BLOCKS;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002018
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002019 INIT_LIST_HEAD(&sm_info->discard_list);
2020 sm_info->nr_discards = 0;
2021 sm_info->max_discards = 0;
2022
Chao Yu184a5cd2014-09-04 18:13:01 +08002023 INIT_LIST_HEAD(&sm_info->sit_entry_set);
2024
Gu Zhengb270ad62014-04-11 17:49:55 +08002025 if (test_opt(sbi, FLUSH_MERGE) && !f2fs_readonly(sbi->sb)) {
Gu Zheng2163d192014-04-27 14:21:33 +08002026 err = create_flush_cmd_control(sbi);
2027 if (err)
Gu Zhenga688b9d9e2014-04-27 14:21:21 +08002028 return err;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +09002029 }
2030
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002031 err = build_sit_info(sbi);
2032 if (err)
2033 return err;
2034 err = build_free_segmap(sbi);
2035 if (err)
2036 return err;
2037 err = build_curseg(sbi);
2038 if (err)
2039 return err;
2040
2041 /* reinit free segmap based on SIT */
2042 build_sit_entries(sbi);
2043
2044 init_free_segmap(sbi);
2045 err = build_dirty_segmap(sbi);
2046 if (err)
2047 return err;
2048
2049 init_min_max_mtime(sbi);
2050 return 0;
2051}
2052
2053static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
2054 enum dirty_type dirty_type)
2055{
2056 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2057
2058 mutex_lock(&dirty_i->seglist_lock);
2059 kfree(dirty_i->dirty_segmap[dirty_type]);
2060 dirty_i->nr_dirty[dirty_type] = 0;
2061 mutex_unlock(&dirty_i->seglist_lock);
2062}
2063
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002064static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002065{
2066 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002067 kfree(dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002068}
2069
2070static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
2071{
2072 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2073 int i;
2074
2075 if (!dirty_i)
2076 return;
2077
2078 /* discard pre-free/dirty segments list */
2079 for (i = 0; i < NR_DIRTY_TYPE; i++)
2080 discard_dirty_segmap(sbi, i);
2081
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002082 destroy_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002083 SM_I(sbi)->dirty_info = NULL;
2084 kfree(dirty_i);
2085}
2086
2087static void destroy_curseg(struct f2fs_sb_info *sbi)
2088{
2089 struct curseg_info *array = SM_I(sbi)->curseg_array;
2090 int i;
2091
2092 if (!array)
2093 return;
2094 SM_I(sbi)->curseg_array = NULL;
2095 for (i = 0; i < NR_CURSEG_TYPE; i++)
2096 kfree(array[i].sum_blk);
2097 kfree(array);
2098}
2099
2100static void destroy_free_segmap(struct f2fs_sb_info *sbi)
2101{
2102 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
2103 if (!free_i)
2104 return;
2105 SM_I(sbi)->free_info = NULL;
2106 kfree(free_i->free_segmap);
2107 kfree(free_i->free_secmap);
2108 kfree(free_i);
2109}
2110
2111static void destroy_sit_info(struct f2fs_sb_info *sbi)
2112{
2113 struct sit_info *sit_i = SIT_I(sbi);
2114 unsigned int start;
2115
2116 if (!sit_i)
2117 return;
2118
2119 if (sit_i->sentries) {
2120 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
2121 kfree(sit_i->sentries[start].cur_valid_map);
2122 kfree(sit_i->sentries[start].ckpt_valid_map);
2123 }
2124 }
2125 vfree(sit_i->sentries);
2126 vfree(sit_i->sec_entries);
2127 kfree(sit_i->dirty_sentries_bitmap);
2128
2129 SM_I(sbi)->sit_info = NULL;
2130 kfree(sit_i->sit_bitmap);
2131 kfree(sit_i);
2132}
2133
2134void destroy_segment_manager(struct f2fs_sb_info *sbi)
2135{
2136 struct f2fs_sm_info *sm_info = SM_I(sbi);
Gu Zhenga688b9d9e2014-04-27 14:21:21 +08002137
Chao Yu3b03f722013-11-06 09:12:04 +08002138 if (!sm_info)
2139 return;
Gu Zheng2163d192014-04-27 14:21:33 +08002140 destroy_flush_cmd_control(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002141 destroy_dirty_segmap(sbi);
2142 destroy_curseg(sbi);
2143 destroy_free_segmap(sbi);
2144 destroy_sit_info(sbi);
2145 sbi->sm_info = NULL;
2146 kfree(sm_info);
2147}
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002148
2149int __init create_segment_manager_caches(void)
2150{
2151 discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
Gu Zhenge8512d22014-03-07 18:43:28 +08002152 sizeof(struct discard_entry));
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002153 if (!discard_entry_slab)
Chao Yu184a5cd2014-09-04 18:13:01 +08002154 goto fail;
2155
2156 sit_entry_set_slab = f2fs_kmem_cache_create("sit_entry_set",
2157 sizeof(struct nat_entry_set));
2158 if (!sit_entry_set_slab)
2159 goto destory_discard_entry;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002160 return 0;
Chao Yu184a5cd2014-09-04 18:13:01 +08002161
2162destory_discard_entry:
2163 kmem_cache_destroy(discard_entry_slab);
2164fail:
2165 return -ENOMEM;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002166}
2167
2168void destroy_segment_manager_caches(void)
2169{
Chao Yu184a5cd2014-09-04 18:13:01 +08002170 kmem_cache_destroy(sit_entry_set_slab);
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002171 kmem_cache_destroy(discard_entry_slab);
2172}