blob: 9d4a7ab33b06ba203d19f89091f21312ccbdc092 [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 Kim88b88a62014-10-06 17:39:50 -070029static struct kmem_cache *inmem_entry_slab;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +090030
Changman Lee9a7f1432013-11-15 10:42:51 +090031/*
32 * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since
33 * MSB and LSB are reversed in a byte by f2fs_set_bit.
34 */
35static inline unsigned long __reverse_ffs(unsigned long word)
36{
37 int num = 0;
38
39#if BITS_PER_LONG == 64
40 if ((word & 0xffffffff) == 0) {
41 num += 32;
42 word >>= 32;
43 }
44#endif
45 if ((word & 0xffff) == 0) {
46 num += 16;
47 word >>= 16;
48 }
49 if ((word & 0xff) == 0) {
50 num += 8;
51 word >>= 8;
52 }
53 if ((word & 0xf0) == 0)
54 num += 4;
55 else
56 word >>= 4;
57 if ((word & 0xc) == 0)
58 num += 2;
59 else
60 word >>= 2;
61 if ((word & 0x2) == 0)
62 num += 1;
63 return num;
64}
65
66/*
arter97e1c42042014-08-06 23:22:50 +090067 * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c because
Changman Lee9a7f1432013-11-15 10:42:51 +090068 * f2fs_set_bit makes MSB and LSB reversed in a byte.
69 * Example:
70 * LSB <--> MSB
71 * f2fs_set_bit(0, bitmap) => 0000 0001
72 * f2fs_set_bit(7, bitmap) => 1000 0000
73 */
74static unsigned long __find_rev_next_bit(const unsigned long *addr,
75 unsigned long size, unsigned long offset)
76{
77 const unsigned long *p = addr + BIT_WORD(offset);
78 unsigned long result = offset & ~(BITS_PER_LONG - 1);
79 unsigned long tmp;
80 unsigned long mask, submask;
81 unsigned long quot, rest;
82
83 if (offset >= size)
84 return size;
85
86 size -= result;
87 offset %= BITS_PER_LONG;
88 if (!offset)
89 goto aligned;
90
91 tmp = *(p++);
92 quot = (offset >> 3) << 3;
93 rest = offset & 0x7;
94 mask = ~0UL << quot;
95 submask = (unsigned char)(0xff << rest) >> rest;
96 submask <<= quot;
97 mask &= submask;
98 tmp &= mask;
99 if (size < BITS_PER_LONG)
100 goto found_first;
101 if (tmp)
102 goto found_middle;
103
104 size -= BITS_PER_LONG;
105 result += BITS_PER_LONG;
106aligned:
107 while (size & ~(BITS_PER_LONG-1)) {
108 tmp = *(p++);
109 if (tmp)
110 goto found_middle;
111 result += BITS_PER_LONG;
112 size -= BITS_PER_LONG;
113 }
114 if (!size)
115 return result;
116 tmp = *p;
117found_first:
118 tmp &= (~0UL >> (BITS_PER_LONG - size));
119 if (tmp == 0UL) /* Are any bits set? */
120 return result + size; /* Nope. */
121found_middle:
122 return result + __reverse_ffs(tmp);
123}
124
125static unsigned long __find_rev_next_zero_bit(const unsigned long *addr,
126 unsigned long size, unsigned long offset)
127{
128 const unsigned long *p = addr + BIT_WORD(offset);
129 unsigned long result = offset & ~(BITS_PER_LONG - 1);
130 unsigned long tmp;
131 unsigned long mask, submask;
132 unsigned long quot, rest;
133
134 if (offset >= size)
135 return size;
136
137 size -= result;
138 offset %= BITS_PER_LONG;
139 if (!offset)
140 goto aligned;
141
142 tmp = *(p++);
143 quot = (offset >> 3) << 3;
144 rest = offset & 0x7;
145 mask = ~(~0UL << quot);
146 submask = (unsigned char)~((unsigned char)(0xff << rest) >> rest);
147 submask <<= quot;
148 mask += submask;
149 tmp |= mask;
150 if (size < BITS_PER_LONG)
151 goto found_first;
152 if (~tmp)
153 goto found_middle;
154
155 size -= BITS_PER_LONG;
156 result += BITS_PER_LONG;
157aligned:
158 while (size & ~(BITS_PER_LONG - 1)) {
159 tmp = *(p++);
160 if (~tmp)
161 goto found_middle;
162 result += BITS_PER_LONG;
163 size -= BITS_PER_LONG;
164 }
165 if (!size)
166 return result;
167 tmp = *p;
168
169found_first:
170 tmp |= ~0UL << size;
171 if (tmp == ~0UL) /* Are any bits zero? */
172 return result + size; /* Nope. */
173found_middle:
174 return result + __reverse_ffz(tmp);
175}
176
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700177void register_inmem_page(struct inode *inode, struct page *page)
178{
179 struct f2fs_inode_info *fi = F2FS_I(inode);
180 struct inmem_pages *new;
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -0700181 int err;
182retry:
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700183 new = f2fs_kmem_cache_alloc(inmem_entry_slab, GFP_NOFS);
184
185 /* add atomic page indices to the list */
186 new->page = page;
187 INIT_LIST_HEAD(&new->list);
188
189 /* increase reference count with clean state */
190 mutex_lock(&fi->inmem_lock);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -0700191 err = radix_tree_insert(&fi->inmem_root, page->index, new);
192 if (err == -EEXIST) {
193 mutex_unlock(&fi->inmem_lock);
194 kmem_cache_free(inmem_entry_slab, new);
195 return;
196 } else if (err) {
197 mutex_unlock(&fi->inmem_lock);
198 kmem_cache_free(inmem_entry_slab, new);
199 goto retry;
200 }
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700201 get_page(page);
202 list_add_tail(&new->list, &fi->inmem_pages);
203 mutex_unlock(&fi->inmem_lock);
204}
205
206void commit_inmem_pages(struct inode *inode, bool abort)
207{
208 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
209 struct f2fs_inode_info *fi = F2FS_I(inode);
210 struct inmem_pages *cur, *tmp;
211 bool submit_bio = false;
212 struct f2fs_io_info fio = {
213 .type = DATA,
214 .rw = WRITE_SYNC,
215 };
216
217 f2fs_balance_fs(sbi);
218 f2fs_lock_op(sbi);
219
220 mutex_lock(&fi->inmem_lock);
221 list_for_each_entry_safe(cur, tmp, &fi->inmem_pages, list) {
222 lock_page(cur->page);
223 if (!abort && cur->page->mapping == inode->i_mapping) {
224 f2fs_wait_on_page_writeback(cur->page, DATA);
225 if (clear_page_dirty_for_io(cur->page))
226 inode_dec_dirty_pages(inode);
227 do_write_data_page(cur->page, &fio);
228 submit_bio = true;
229 }
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -0700230 radix_tree_delete(&fi->inmem_root, cur->page->index);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700231 f2fs_put_page(cur->page, 1);
232 list_del(&cur->list);
233 kmem_cache_free(inmem_entry_slab, cur);
234 }
235 if (submit_bio)
236 f2fs_submit_merged_bio(sbi, DATA, WRITE);
237 mutex_unlock(&fi->inmem_lock);
238
239 filemap_fdatawait_range(inode->i_mapping, 0, LLONG_MAX);
240 f2fs_unlock_op(sbi);
241}
242
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900243/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900244 * This function balances dirty node and dentry pages.
245 * In addition, it controls garbage collection.
246 */
247void f2fs_balance_fs(struct f2fs_sb_info *sbi)
248{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900249 /*
Jaegeuk Kim029cd282012-12-21 17:20:21 +0900250 * We should do GC or end up with checkpoint, if there are so many dirty
251 * dir/node pages without enough free segments.
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900252 */
Jaegeuk Kim43727522013-02-04 15:11:17 +0900253 if (has_not_enough_free_secs(sbi, 0)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900254 mutex_lock(&sbi->gc_mutex);
Jaegeuk Kim408e9372013-01-03 17:55:52 +0900255 f2fs_gc(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900256 }
257}
258
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900259void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi)
260{
261 /* check the # of cached NAT entries and prefree segments */
262 if (try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK) ||
263 excess_prefree_segs(sbi))
264 f2fs_sync_fs(sbi->sb, true);
265}
266
Gu Zheng2163d192014-04-27 14:21:33 +0800267static int issue_flush_thread(void *data)
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900268{
269 struct f2fs_sb_info *sbi = data;
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800270 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
271 wait_queue_head_t *q = &fcc->flush_wait_queue;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900272repeat:
273 if (kthread_should_stop())
274 return 0;
275
Gu Zheng721bd4d2014-09-05 18:31:00 +0800276 if (!llist_empty(&fcc->issue_list)) {
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900277 struct bio *bio = bio_alloc(GFP_NOIO, 0);
278 struct flush_cmd *cmd, *next;
279 int ret;
280
Gu Zheng721bd4d2014-09-05 18:31:00 +0800281 fcc->dispatch_list = llist_del_all(&fcc->issue_list);
282 fcc->dispatch_list = llist_reverse_order(fcc->dispatch_list);
283
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900284 bio->bi_bdev = sbi->sb->s_bdev;
285 ret = submit_bio_wait(WRITE_FLUSH, bio);
286
Gu Zheng721bd4d2014-09-05 18:31:00 +0800287 llist_for_each_entry_safe(cmd, next,
288 fcc->dispatch_list, llnode) {
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900289 cmd->ret = ret;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900290 complete(&cmd->wait);
291 }
Gu Zhenga4ed23f2014-04-11 17:49:35 +0800292 bio_put(bio);
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800293 fcc->dispatch_list = NULL;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900294 }
295
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800296 wait_event_interruptible(*q,
Gu Zheng721bd4d2014-09-05 18:31:00 +0800297 kthread_should_stop() || !llist_empty(&fcc->issue_list));
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900298 goto repeat;
299}
300
301int f2fs_issue_flush(struct f2fs_sb_info *sbi)
302{
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800303 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
Chao Yuadf8d902014-05-08 17:00:35 +0800304 struct flush_cmd cmd;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900305
Jaegeuk Kim24a9ee02014-07-25 17:46:10 -0700306 trace_f2fs_issue_flush(sbi->sb, test_opt(sbi, NOBARRIER),
307 test_opt(sbi, FLUSH_MERGE));
308
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700309 if (test_opt(sbi, NOBARRIER))
310 return 0;
311
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900312 if (!test_opt(sbi, FLUSH_MERGE))
313 return blkdev_issue_flush(sbi->sb->s_bdev, GFP_KERNEL, NULL);
314
Chao Yuadf8d902014-05-08 17:00:35 +0800315 init_completion(&cmd.wait);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900316
Gu Zheng721bd4d2014-09-05 18:31:00 +0800317 llist_add(&cmd.llnode, &fcc->issue_list);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900318
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800319 if (!fcc->dispatch_list)
320 wake_up(&fcc->flush_wait_queue);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900321
Chao Yuadf8d902014-05-08 17:00:35 +0800322 wait_for_completion(&cmd.wait);
323
324 return cmd.ret;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900325}
326
Gu Zheng2163d192014-04-27 14:21:33 +0800327int create_flush_cmd_control(struct f2fs_sb_info *sbi)
328{
329 dev_t dev = sbi->sb->s_bdev->bd_dev;
330 struct flush_cmd_control *fcc;
331 int err = 0;
332
333 fcc = kzalloc(sizeof(struct flush_cmd_control), GFP_KERNEL);
334 if (!fcc)
335 return -ENOMEM;
Gu Zheng2163d192014-04-27 14:21:33 +0800336 init_waitqueue_head(&fcc->flush_wait_queue);
Gu Zheng721bd4d2014-09-05 18:31:00 +0800337 init_llist_head(&fcc->issue_list);
Chao Yu6b2920a2014-07-07 11:21:59 +0800338 SM_I(sbi)->cmd_control_info = fcc;
Gu Zheng2163d192014-04-27 14:21:33 +0800339 fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi,
340 "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev));
341 if (IS_ERR(fcc->f2fs_issue_flush)) {
342 err = PTR_ERR(fcc->f2fs_issue_flush);
343 kfree(fcc);
Chao Yu6b2920a2014-07-07 11:21:59 +0800344 SM_I(sbi)->cmd_control_info = NULL;
Gu Zheng2163d192014-04-27 14:21:33 +0800345 return err;
346 }
Gu Zheng2163d192014-04-27 14:21:33 +0800347
348 return err;
349}
350
351void destroy_flush_cmd_control(struct f2fs_sb_info *sbi)
352{
Chao Yu6b2920a2014-07-07 11:21:59 +0800353 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
Gu Zheng2163d192014-04-27 14:21:33 +0800354
355 if (fcc && fcc->f2fs_issue_flush)
356 kthread_stop(fcc->f2fs_issue_flush);
357 kfree(fcc);
Chao Yu6b2920a2014-07-07 11:21:59 +0800358 SM_I(sbi)->cmd_control_info = NULL;
Gu Zheng2163d192014-04-27 14:21:33 +0800359}
360
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900361static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
362 enum dirty_type dirty_type)
363{
364 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
365
366 /* need not be added */
367 if (IS_CURSEG(sbi, segno))
368 return;
369
370 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
371 dirty_i->nr_dirty[dirty_type]++;
372
373 if (dirty_type == DIRTY) {
374 struct seg_entry *sentry = get_seg_entry(sbi, segno);
Changman Lee4625d6a2013-10-25 17:31:57 +0900375 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900376
Jaegeuk Kimec325b52014-09-02 16:24:11 -0700377 if (unlikely(t >= DIRTY)) {
378 f2fs_bug_on(sbi, 1);
379 return;
380 }
Changman Lee4625d6a2013-10-25 17:31:57 +0900381 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
382 dirty_i->nr_dirty[t]++;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900383 }
384}
385
386static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
387 enum dirty_type dirty_type)
388{
389 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
390
391 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
392 dirty_i->nr_dirty[dirty_type]--;
393
394 if (dirty_type == DIRTY) {
Changman Lee4625d6a2013-10-25 17:31:57 +0900395 struct seg_entry *sentry = get_seg_entry(sbi, segno);
396 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900397
Changman Lee4625d6a2013-10-25 17:31:57 +0900398 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
399 dirty_i->nr_dirty[t]--;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900400
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +0900401 if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0)
402 clear_bit(GET_SECNO(sbi, segno),
403 dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900404 }
405}
406
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900407/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900408 * Should not occur error such as -ENOMEM.
409 * Adding dirty entry into seglist is not critical operation.
410 * If a given segment is one of current working segments, it won't be added.
411 */
Haicheng Li8d8451a2013-06-13 16:59:28 +0800412static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900413{
414 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
415 unsigned short valid_blocks;
416
417 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
418 return;
419
420 mutex_lock(&dirty_i->seglist_lock);
421
422 valid_blocks = get_valid_blocks(sbi, segno, 0);
423
424 if (valid_blocks == 0) {
425 __locate_dirty_segment(sbi, segno, PRE);
426 __remove_dirty_segment(sbi, segno, DIRTY);
427 } else if (valid_blocks < sbi->blocks_per_seg) {
428 __locate_dirty_segment(sbi, segno, DIRTY);
429 } else {
430 /* Recovery routine with SSR needs this */
431 __remove_dirty_segment(sbi, segno, DIRTY);
432 }
433
434 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900435}
436
Jaegeuk Kim1e87a782014-04-15 13:57:55 +0900437static int f2fs_issue_discard(struct f2fs_sb_info *sbi,
Jaegeuk Kim37208872013-11-12 16:55:17 +0900438 block_t blkstart, block_t blklen)
439{
Chao Yu55cf9cb2014-09-15 18:01:10 +0800440 sector_t start = SECTOR_FROM_BLOCK(blkstart);
441 sector_t len = SECTOR_FROM_BLOCK(blklen);
Jaegeuk Kim1661d072013-11-12 17:01:00 +0900442 trace_f2fs_issue_discard(sbi->sb, blkstart, blklen);
Jaegeuk Kim1e87a782014-04-15 13:57:55 +0900443 return blkdev_issue_discard(sbi->sb->s_bdev, start, len, GFP_NOFS, 0);
444}
445
Jaegeuk Kimcf2271e2014-07-25 15:47:25 -0700446void discard_next_dnode(struct f2fs_sb_info *sbi, block_t blkaddr)
Jaegeuk Kim1e87a782014-04-15 13:57:55 +0900447{
Jaegeuk Kim1e87a782014-04-15 13:57:55 +0900448 if (f2fs_issue_discard(sbi, blkaddr, 1)) {
449 struct page *page = grab_meta_page(sbi, blkaddr);
450 /* zero-filled page */
451 set_page_dirty(page);
452 f2fs_put_page(page, 1);
453 }
Jaegeuk Kim37208872013-11-12 16:55:17 +0900454}
455
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700456static void add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc)
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900457{
458 struct list_head *head = &SM_I(sbi)->discard_list;
459 struct discard_entry *new;
460 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
461 int max_blocks = sbi->blocks_per_seg;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700462 struct seg_entry *se = get_seg_entry(sbi, cpc->trim_start);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900463 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
464 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
465 unsigned long dmap[entries];
466 unsigned int start = 0, end = -1;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700467 bool force = (cpc->reason == CP_DISCARD);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900468 int i;
469
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700470 if (!force && !test_opt(sbi, DISCARD))
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900471 return;
472
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700473 if (force && !se->valid_blocks) {
474 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
475 /*
476 * if this segment is registered in the prefree list, then
477 * we should skip adding a discard candidate, and let the
478 * checkpoint do that later.
479 */
480 mutex_lock(&dirty_i->seglist_lock);
481 if (test_bit(cpc->trim_start, dirty_i->dirty_segmap[PRE])) {
482 mutex_unlock(&dirty_i->seglist_lock);
483 cpc->trimmed += sbi->blocks_per_seg;
484 return;
485 }
486 mutex_unlock(&dirty_i->seglist_lock);
487
488 new = f2fs_kmem_cache_alloc(discard_entry_slab, GFP_NOFS);
489 INIT_LIST_HEAD(&new->list);
490 new->blkaddr = START_BLOCK(sbi, cpc->trim_start);
491 new->len = sbi->blocks_per_seg;
492 list_add_tail(&new->list, head);
493 SM_I(sbi)->nr_discards += sbi->blocks_per_seg;
494 cpc->trimmed += sbi->blocks_per_seg;
495 return;
496 }
497
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900498 /* zero block will be discarded through the prefree list */
499 if (!se->valid_blocks || se->valid_blocks == max_blocks)
500 return;
501
502 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
503 for (i = 0; i < entries; i++)
504 dmap[i] = (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
505
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700506 while (force || SM_I(sbi)->nr_discards <= SM_I(sbi)->max_discards) {
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900507 start = __find_rev_next_bit(dmap, max_blocks, end + 1);
508 if (start >= max_blocks)
509 break;
510
511 end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
512
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700513 if (end - start < cpc->trim_minlen)
514 continue;
515
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900516 new = f2fs_kmem_cache_alloc(discard_entry_slab, GFP_NOFS);
517 INIT_LIST_HEAD(&new->list);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700518 new->blkaddr = START_BLOCK(sbi, cpc->trim_start) + start;
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900519 new->len = end - start;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700520 cpc->trimmed += end - start;
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900521
522 list_add_tail(&new->list, head);
523 SM_I(sbi)->nr_discards += end - start;
524 }
525}
526
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700527void release_discard_addrs(struct f2fs_sb_info *sbi)
528{
529 struct list_head *head = &(SM_I(sbi)->discard_list);
530 struct discard_entry *entry, *this;
531
532 /* drop caches */
533 list_for_each_entry_safe(entry, this, head, list) {
534 list_del(&entry->list);
535 kmem_cache_free(discard_entry_slab, entry);
536 }
537}
538
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900539/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900540 * Should call clear_prefree_segments after checkpoint is done.
541 */
542static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
543{
544 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Chao Yub65ee142014-08-04 10:10:07 +0800545 unsigned int segno;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900546
547 mutex_lock(&dirty_i->seglist_lock);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700548 for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900549 __set_test_and_free(sbi, segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900550 mutex_unlock(&dirty_i->seglist_lock);
551}
552
553void clear_prefree_segments(struct f2fs_sb_info *sbi)
554{
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900555 struct list_head *head = &(SM_I(sbi)->discard_list);
Chao Yu2d7b8222014-03-29 11:33:17 +0800556 struct discard_entry *entry, *this;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900557 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Changman Lee29e59c12013-11-11 09:24:37 +0900558 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
Changman Lee29e59c12013-11-11 09:24:37 +0900559 unsigned int start = 0, end = -1;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900560
561 mutex_lock(&dirty_i->seglist_lock);
Changman Lee29e59c12013-11-11 09:24:37 +0900562
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900563 while (1) {
Changman Lee29e59c12013-11-11 09:24:37 +0900564 int i;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700565 start = find_next_bit(prefree_map, MAIN_SEGS(sbi), end + 1);
566 if (start >= MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900567 break;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700568 end = find_next_zero_bit(prefree_map, MAIN_SEGS(sbi),
569 start + 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900570
Changman Lee29e59c12013-11-11 09:24:37 +0900571 for (i = start; i < end; i++)
572 clear_bit(i, prefree_map);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900573
Changman Lee29e59c12013-11-11 09:24:37 +0900574 dirty_i->nr_dirty[PRE] -= end - start;
575
576 if (!test_opt(sbi, DISCARD))
577 continue;
578
Jaegeuk Kim37208872013-11-12 16:55:17 +0900579 f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
580 (end - start) << sbi->log_blocks_per_seg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900581 }
582 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900583
584 /* send small discards */
Chao Yu2d7b8222014-03-29 11:33:17 +0800585 list_for_each_entry_safe(entry, this, head, list) {
Jaegeuk Kim37208872013-11-12 16:55:17 +0900586 f2fs_issue_discard(sbi, entry->blkaddr, entry->len);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900587 list_del(&entry->list);
588 SM_I(sbi)->nr_discards -= entry->len;
589 kmem_cache_free(discard_entry_slab, entry);
590 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900591}
592
Chao Yu184a5cd2014-09-04 18:13:01 +0800593static bool __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900594{
595 struct sit_info *sit_i = SIT_I(sbi);
Chao Yu184a5cd2014-09-04 18:13:01 +0800596
597 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900598 sit_i->dirty_sentries++;
Chao Yu184a5cd2014-09-04 18:13:01 +0800599 return false;
600 }
601
602 return true;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900603}
604
605static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
606 unsigned int segno, int modified)
607{
608 struct seg_entry *se = get_seg_entry(sbi, segno);
609 se->type = type;
610 if (modified)
611 __mark_sit_entry_dirty(sbi, segno);
612}
613
614static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
615{
616 struct seg_entry *se;
617 unsigned int segno, offset;
618 long int new_vblocks;
619
620 segno = GET_SEGNO(sbi, blkaddr);
621
622 se = get_seg_entry(sbi, segno);
623 new_vblocks = se->valid_blocks + del;
Jaegeuk Kim491c0852014-02-04 13:01:10 +0900624 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900625
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700626 f2fs_bug_on(sbi, (new_vblocks >> (sizeof(unsigned short) << 3) ||
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900627 (new_vblocks > sbi->blocks_per_seg)));
628
629 se->valid_blocks = new_vblocks;
630 se->mtime = get_mtime(sbi);
631 SIT_I(sbi)->max_mtime = se->mtime;
632
633 /* Update valid block bitmap */
634 if (del > 0) {
635 if (f2fs_set_bit(offset, se->cur_valid_map))
Jaegeuk Kim05796762014-09-02 16:05:00 -0700636 f2fs_bug_on(sbi, 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900637 } else {
638 if (!f2fs_clear_bit(offset, se->cur_valid_map))
Jaegeuk Kim05796762014-09-02 16:05:00 -0700639 f2fs_bug_on(sbi, 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900640 }
641 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
642 se->ckpt_valid_blocks += del;
643
644 __mark_sit_entry_dirty(sbi, segno);
645
646 /* update total number of valid blocks to be written in ckpt area */
647 SIT_I(sbi)->written_valid_blocks += del;
648
649 if (sbi->segs_per_sec > 1)
650 get_sec_entry(sbi, segno)->valid_blocks += del;
651}
652
Jaegeuk Kim5e443812014-01-28 12:22:14 +0900653void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900654{
Jaegeuk Kim5e443812014-01-28 12:22:14 +0900655 update_sit_entry(sbi, new, 1);
656 if (GET_SEGNO(sbi, old) != NULL_SEGNO)
657 update_sit_entry(sbi, old, -1);
658
659 locate_dirty_segment(sbi, GET_SEGNO(sbi, old));
660 locate_dirty_segment(sbi, GET_SEGNO(sbi, new));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900661}
662
663void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
664{
665 unsigned int segno = GET_SEGNO(sbi, addr);
666 struct sit_info *sit_i = SIT_I(sbi);
667
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700668 f2fs_bug_on(sbi, addr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900669 if (addr == NEW_ADDR)
670 return;
671
672 /* add it into sit main buffer */
673 mutex_lock(&sit_i->sentry_lock);
674
675 update_sit_entry(sbi, addr, -1);
676
677 /* add it into dirty seglist */
678 locate_dirty_segment(sbi, segno);
679
680 mutex_unlock(&sit_i->sentry_lock);
681}
682
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900683/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900684 * This function should be resided under the curseg_mutex lock
685 */
686static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
Haicheng Lie79efe32013-06-13 16:59:27 +0800687 struct f2fs_summary *sum)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900688{
689 struct curseg_info *curseg = CURSEG_I(sbi, type);
690 void *addr = curseg->sum_blk;
Haicheng Lie79efe32013-06-13 16:59:27 +0800691 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900692 memcpy(addr, sum, sizeof(struct f2fs_summary));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900693}
694
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900695/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900696 * Calculate the number of current summary pages for writing
697 */
698int npages_for_summary_flush(struct f2fs_sb_info *sbi)
699{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900700 int valid_sum_count = 0;
Fan Li9a479382013-10-29 16:21:47 +0800701 int i, sum_in_page;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900702
703 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
704 if (sbi->ckpt->alloc_type[i] == SSR)
705 valid_sum_count += sbi->blocks_per_seg;
706 else
707 valid_sum_count += curseg_blkoff(sbi, i);
708 }
709
Fan Li9a479382013-10-29 16:21:47 +0800710 sum_in_page = (PAGE_CACHE_SIZE - 2 * SUM_JOURNAL_SIZE -
711 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
712 if (valid_sum_count <= sum_in_page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900713 return 1;
Fan Li9a479382013-10-29 16:21:47 +0800714 else if ((valid_sum_count - sum_in_page) <=
715 (PAGE_CACHE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900716 return 2;
717 return 3;
718}
719
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900720/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900721 * Caller should put this summary page
722 */
723struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
724{
725 return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
726}
727
728static void write_sum_page(struct f2fs_sb_info *sbi,
729 struct f2fs_summary_block *sum_blk, block_t blk_addr)
730{
731 struct page *page = grab_meta_page(sbi, blk_addr);
732 void *kaddr = page_address(page);
733 memcpy(kaddr, sum_blk, PAGE_CACHE_SIZE);
734 set_page_dirty(page);
735 f2fs_put_page(page, 1);
736}
737
Jaegeuk Kim60374682013-03-31 13:58:51 +0900738static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
739{
740 struct curseg_info *curseg = CURSEG_I(sbi, type);
Haicheng Li81fb5e82013-05-14 18:20:28 +0800741 unsigned int segno = curseg->segno + 1;
Jaegeuk Kim60374682013-03-31 13:58:51 +0900742 struct free_segmap_info *free_i = FREE_I(sbi);
743
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700744 if (segno < MAIN_SEGS(sbi) && segno % sbi->segs_per_sec)
Haicheng Li81fb5e82013-05-14 18:20:28 +0800745 return !test_bit(segno, free_i->free_segmap);
Jaegeuk Kim60374682013-03-31 13:58:51 +0900746 return 0;
747}
748
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900749/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900750 * Find a new segment from the free segments bitmap to right order
751 * This function should be returned with success, otherwise BUG
752 */
753static void get_new_segment(struct f2fs_sb_info *sbi,
754 unsigned int *newseg, bool new_sec, int dir)
755{
756 struct free_segmap_info *free_i = FREE_I(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900757 unsigned int segno, secno, zoneno;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700758 unsigned int total_zones = MAIN_SECS(sbi) / sbi->secs_per_zone;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900759 unsigned int hint = *newseg / sbi->segs_per_sec;
760 unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
761 unsigned int left_start = hint;
762 bool init = true;
763 int go_left = 0;
764 int i;
765
766 write_lock(&free_i->segmap_lock);
767
768 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
769 segno = find_next_zero_bit(free_i->free_segmap,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700770 MAIN_SEGS(sbi), *newseg + 1);
Jaegeuk Kim33afa7f2013-03-31 12:59:53 +0900771 if (segno - *newseg < sbi->segs_per_sec -
772 (*newseg % sbi->segs_per_sec))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900773 goto got_it;
774 }
775find_other_zone:
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700776 secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint);
777 if (secno >= MAIN_SECS(sbi)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900778 if (dir == ALLOC_RIGHT) {
779 secno = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700780 MAIN_SECS(sbi), 0);
781 f2fs_bug_on(sbi, secno >= MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900782 } else {
783 go_left = 1;
784 left_start = hint - 1;
785 }
786 }
787 if (go_left == 0)
788 goto skip_left;
789
790 while (test_bit(left_start, free_i->free_secmap)) {
791 if (left_start > 0) {
792 left_start--;
793 continue;
794 }
795 left_start = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700796 MAIN_SECS(sbi), 0);
797 f2fs_bug_on(sbi, left_start >= MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900798 break;
799 }
800 secno = left_start;
801skip_left:
802 hint = secno;
803 segno = secno * sbi->segs_per_sec;
804 zoneno = secno / sbi->secs_per_zone;
805
806 /* give up on finding another zone */
807 if (!init)
808 goto got_it;
809 if (sbi->secs_per_zone == 1)
810 goto got_it;
811 if (zoneno == old_zoneno)
812 goto got_it;
813 if (dir == ALLOC_LEFT) {
814 if (!go_left && zoneno + 1 >= total_zones)
815 goto got_it;
816 if (go_left && zoneno == 0)
817 goto got_it;
818 }
819 for (i = 0; i < NR_CURSEG_TYPE; i++)
820 if (CURSEG_I(sbi, i)->zone == zoneno)
821 break;
822
823 if (i < NR_CURSEG_TYPE) {
824 /* zone is in user, try another */
825 if (go_left)
826 hint = zoneno * sbi->secs_per_zone - 1;
827 else if (zoneno + 1 >= total_zones)
828 hint = 0;
829 else
830 hint = (zoneno + 1) * sbi->secs_per_zone;
831 init = false;
832 goto find_other_zone;
833 }
834got_it:
835 /* set it as dirty segment in free segmap */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700836 f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900837 __set_inuse(sbi, segno);
838 *newseg = segno;
839 write_unlock(&free_i->segmap_lock);
840}
841
842static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
843{
844 struct curseg_info *curseg = CURSEG_I(sbi, type);
845 struct summary_footer *sum_footer;
846
847 curseg->segno = curseg->next_segno;
848 curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
849 curseg->next_blkoff = 0;
850 curseg->next_segno = NULL_SEGNO;
851
852 sum_footer = &(curseg->sum_blk->footer);
853 memset(sum_footer, 0, sizeof(struct summary_footer));
854 if (IS_DATASEG(type))
855 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
856 if (IS_NODESEG(type))
857 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
858 __set_sit_entry_type(sbi, type, curseg->segno, modified);
859}
860
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900861/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900862 * Allocate a current working segment.
863 * This function always allocates a free segment in LFS manner.
864 */
865static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
866{
867 struct curseg_info *curseg = CURSEG_I(sbi, type);
868 unsigned int segno = curseg->segno;
869 int dir = ALLOC_LEFT;
870
871 write_sum_page(sbi, curseg->sum_blk,
Haicheng Li81fb5e82013-05-14 18:20:28 +0800872 GET_SUM_BLOCK(sbi, segno));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900873 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
874 dir = ALLOC_RIGHT;
875
876 if (test_opt(sbi, NOHEAP))
877 dir = ALLOC_RIGHT;
878
879 get_new_segment(sbi, &segno, new_sec, dir);
880 curseg->next_segno = segno;
881 reset_curseg(sbi, type, 1);
882 curseg->alloc_type = LFS;
883}
884
885static void __next_free_blkoff(struct f2fs_sb_info *sbi,
886 struct curseg_info *seg, block_t start)
887{
888 struct seg_entry *se = get_seg_entry(sbi, seg->segno);
Changman Leee81c93c2013-11-15 13:21:16 +0900889 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
890 unsigned long target_map[entries];
891 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
892 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
893 int i, pos;
894
895 for (i = 0; i < entries; i++)
896 target_map[i] = ckpt_map[i] | cur_map[i];
897
898 pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
899
900 seg->next_blkoff = pos;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900901}
902
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900903/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900904 * If a segment is written by LFS manner, next block offset is just obtained
905 * by increasing the current block offset. However, if a segment is written by
906 * SSR manner, next block offset obtained by calling __next_free_blkoff
907 */
908static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
909 struct curseg_info *seg)
910{
911 if (seg->alloc_type == SSR)
912 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
913 else
914 seg->next_blkoff++;
915}
916
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900917/*
arter97e1c42042014-08-06 23:22:50 +0900918 * This function always allocates a used segment(from dirty seglist) by SSR
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900919 * manner, so it should recover the existing segment information of valid blocks
920 */
921static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
922{
923 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
924 struct curseg_info *curseg = CURSEG_I(sbi, type);
925 unsigned int new_segno = curseg->next_segno;
926 struct f2fs_summary_block *sum_node;
927 struct page *sum_page;
928
929 write_sum_page(sbi, curseg->sum_blk,
930 GET_SUM_BLOCK(sbi, curseg->segno));
931 __set_test_and_inuse(sbi, new_segno);
932
933 mutex_lock(&dirty_i->seglist_lock);
934 __remove_dirty_segment(sbi, new_segno, PRE);
935 __remove_dirty_segment(sbi, new_segno, DIRTY);
936 mutex_unlock(&dirty_i->seglist_lock);
937
938 reset_curseg(sbi, type, 1);
939 curseg->alloc_type = SSR;
940 __next_free_blkoff(sbi, curseg, 0);
941
942 if (reuse) {
943 sum_page = get_sum_page(sbi, new_segno);
944 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
945 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
946 f2fs_put_page(sum_page, 1);
947 }
948}
949
Jaegeuk Kim43727522013-02-04 15:11:17 +0900950static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
951{
952 struct curseg_info *curseg = CURSEG_I(sbi, type);
953 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
954
955 if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0))
956 return v_ops->get_victim(sbi,
957 &(curseg)->next_segno, BG_GC, type, SSR);
958
959 /* For data segments, let's do SSR more intensively */
960 for (; type >= CURSEG_HOT_DATA; type--)
961 if (v_ops->get_victim(sbi, &(curseg)->next_segno,
962 BG_GC, type, SSR))
963 return 1;
964 return 0;
965}
966
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900967/*
968 * flush out current segment and replace it with new segment
969 * This function should be returned with success, otherwise BUG
970 */
971static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
972 int type, bool force)
973{
974 struct curseg_info *curseg = CURSEG_I(sbi, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900975
Gu Zheng7b405272013-08-19 09:41:15 +0800976 if (force)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900977 new_curseg(sbi, type, true);
Gu Zheng7b405272013-08-19 09:41:15 +0800978 else if (type == CURSEG_WARM_NODE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900979 new_curseg(sbi, type, false);
Jaegeuk Kim60374682013-03-31 13:58:51 +0900980 else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
981 new_curseg(sbi, type, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900982 else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
983 change_curseg(sbi, type, true);
984 else
985 new_curseg(sbi, type, false);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +0900986
987 stat_inc_seg_type(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900988}
989
990void allocate_new_segments(struct f2fs_sb_info *sbi)
991{
992 struct curseg_info *curseg;
993 unsigned int old_curseg;
994 int i;
995
996 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
997 curseg = CURSEG_I(sbi, i);
998 old_curseg = curseg->segno;
999 SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
1000 locate_dirty_segment(sbi, old_curseg);
1001 }
1002}
1003
1004static const struct segment_allocation default_salloc_ops = {
1005 .allocate_segment = allocate_segment_by_default,
1006};
1007
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001008int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range)
1009{
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001010 __u64 start = range->start >> sbi->log_blocksize;
1011 __u64 end = start + (range->len >> sbi->log_blocksize) - 1;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001012 unsigned int start_segno, end_segno;
1013 struct cp_control cpc;
1014
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001015 if (range->minlen > SEGMENT_SIZE(sbi) || start >= MAX_BLKADDR(sbi) ||
1016 range->len < sbi->blocksize)
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001017 return -EINVAL;
1018
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001019 if (end <= MAIN_BLKADDR(sbi))
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001020 goto out;
1021
1022 /* start/end segment number in main_area */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001023 start_segno = (start <= MAIN_BLKADDR(sbi)) ? 0 : GET_SEGNO(sbi, start);
1024 end_segno = (end >= MAX_BLKADDR(sbi)) ? MAIN_SEGS(sbi) - 1 :
1025 GET_SEGNO(sbi, end);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001026 cpc.reason = CP_DISCARD;
1027 cpc.trim_start = start_segno;
1028 cpc.trim_end = end_segno;
1029 cpc.trim_minlen = range->minlen >> sbi->log_blocksize;
1030 cpc.trimmed = 0;
1031
1032 /* do checkpoint to issue discard commands safely */
1033 write_checkpoint(sbi, &cpc);
1034out:
1035 range->len = cpc.trimmed << sbi->log_blocksize;
1036 return 0;
1037}
1038
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001039static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
1040{
1041 struct curseg_info *curseg = CURSEG_I(sbi, type);
1042 if (curseg->next_blkoff < sbi->blocks_per_seg)
1043 return true;
1044 return false;
1045}
1046
1047static int __get_segment_type_2(struct page *page, enum page_type p_type)
1048{
1049 if (p_type == DATA)
1050 return CURSEG_HOT_DATA;
1051 else
1052 return CURSEG_HOT_NODE;
1053}
1054
1055static int __get_segment_type_4(struct page *page, enum page_type p_type)
1056{
1057 if (p_type == DATA) {
1058 struct inode *inode = page->mapping->host;
1059
1060 if (S_ISDIR(inode->i_mode))
1061 return CURSEG_HOT_DATA;
1062 else
1063 return CURSEG_COLD_DATA;
1064 } else {
1065 if (IS_DNODE(page) && !is_cold_node(page))
1066 return CURSEG_HOT_NODE;
1067 else
1068 return CURSEG_COLD_NODE;
1069 }
1070}
1071
1072static int __get_segment_type_6(struct page *page, enum page_type p_type)
1073{
1074 if (p_type == DATA) {
1075 struct inode *inode = page->mapping->host;
1076
1077 if (S_ISDIR(inode->i_mode))
1078 return CURSEG_HOT_DATA;
Jaegeuk Kim354a3392013-06-14 08:52:35 +09001079 else if (is_cold_data(page) || file_is_cold(inode))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001080 return CURSEG_COLD_DATA;
1081 else
1082 return CURSEG_WARM_DATA;
1083 } else {
1084 if (IS_DNODE(page))
1085 return is_cold_node(page) ? CURSEG_WARM_NODE :
1086 CURSEG_HOT_NODE;
1087 else
1088 return CURSEG_COLD_NODE;
1089 }
1090}
1091
1092static int __get_segment_type(struct page *page, enum page_type p_type)
1093{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001094 switch (F2FS_P_SB(page)->active_logs) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001095 case 2:
1096 return __get_segment_type_2(page, p_type);
1097 case 4:
1098 return __get_segment_type_4(page, p_type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001099 }
Jaegeuk Kim12a67142012-12-21 11:47:05 +09001100 /* NR_CURSEG_TYPE(6) logs by default */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001101 f2fs_bug_on(F2FS_P_SB(page),
1102 F2FS_P_SB(page)->active_logs != NR_CURSEG_TYPE);
Jaegeuk Kim12a67142012-12-21 11:47:05 +09001103 return __get_segment_type_6(page, p_type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001104}
1105
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001106void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
1107 block_t old_blkaddr, block_t *new_blkaddr,
1108 struct f2fs_summary *sum, int type)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001109{
1110 struct sit_info *sit_i = SIT_I(sbi);
1111 struct curseg_info *curseg;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001112
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001113 curseg = CURSEG_I(sbi, type);
1114
1115 mutex_lock(&curseg->curseg_mutex);
1116
1117 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001118
1119 /*
1120 * __add_sum_entry should be resided under the curseg_mutex
1121 * because, this function updates a summary entry in the
1122 * current summary block.
1123 */
Haicheng Lie79efe32013-06-13 16:59:27 +08001124 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001125
1126 mutex_lock(&sit_i->sentry_lock);
1127 __refresh_next_blkoff(sbi, curseg);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +09001128
1129 stat_inc_block_count(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001130
Jaegeuk Kim5e443812014-01-28 12:22:14 +09001131 if (!__has_curseg_space(sbi, type))
1132 sit_i->s_ops->allocate_segment(sbi, type, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001133 /*
1134 * SIT information should be updated before segment allocation,
1135 * since SSR needs latest valid block information.
1136 */
1137 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
Jaegeuk Kim5e443812014-01-28 12:22:14 +09001138
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001139 mutex_unlock(&sit_i->sentry_lock);
1140
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001141 if (page && IS_NODESEG(type))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001142 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
1143
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001144 mutex_unlock(&curseg->curseg_mutex);
1145}
1146
1147static void do_write_page(struct f2fs_sb_info *sbi, struct page *page,
1148 block_t old_blkaddr, block_t *new_blkaddr,
1149 struct f2fs_summary *sum, struct f2fs_io_info *fio)
1150{
1151 int type = __get_segment_type(page, fio->type);
1152
1153 allocate_data_block(sbi, page, old_blkaddr, new_blkaddr, sum, type);
1154
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001155 /* writeout dirty page into bdev */
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001156 f2fs_submit_page_mbio(sbi, page, *new_blkaddr, fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001157}
1158
Jaegeuk Kim577e3492013-01-24 19:56:11 +09001159void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001160{
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001161 struct f2fs_io_info fio = {
1162 .type = META,
Gu Zheng7e8f2302013-12-20 18:17:49 +08001163 .rw = WRITE_SYNC | REQ_META | REQ_PRIO
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001164 };
1165
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001166 set_page_writeback(page);
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001167 f2fs_submit_page_mbio(sbi, page, page->index, &fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001168}
1169
1170void write_node_page(struct f2fs_sb_info *sbi, struct page *page,
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001171 struct f2fs_io_info *fio,
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001172 unsigned int nid, block_t old_blkaddr, block_t *new_blkaddr)
1173{
1174 struct f2fs_summary sum;
1175 set_summary(&sum, nid, 0, 0);
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001176 do_write_page(sbi, page, old_blkaddr, new_blkaddr, &sum, fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001177}
1178
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001179void write_data_page(struct page *page, struct dnode_of_data *dn,
1180 block_t *new_blkaddr, struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001181{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001182 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001183 struct f2fs_summary sum;
1184 struct node_info ni;
1185
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001186 f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001187 get_node_info(sbi, dn->nid, &ni);
1188 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1189
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001190 do_write_page(sbi, page, dn->data_blkaddr, new_blkaddr, &sum, fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001191}
1192
Chris Fries6c311ec2014-01-17 14:44:39 -06001193void rewrite_data_page(struct page *page, block_t old_blkaddr,
1194 struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001195{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001196 f2fs_submit_page_mbio(F2FS_P_SB(page), page, old_blkaddr, fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001197}
1198
1199void recover_data_page(struct f2fs_sb_info *sbi,
1200 struct page *page, struct f2fs_summary *sum,
1201 block_t old_blkaddr, block_t new_blkaddr)
1202{
1203 struct sit_info *sit_i = SIT_I(sbi);
1204 struct curseg_info *curseg;
1205 unsigned int segno, old_cursegno;
1206 struct seg_entry *se;
1207 int type;
1208
1209 segno = GET_SEGNO(sbi, new_blkaddr);
1210 se = get_seg_entry(sbi, segno);
1211 type = se->type;
1212
1213 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
1214 if (old_blkaddr == NULL_ADDR)
1215 type = CURSEG_COLD_DATA;
1216 else
1217 type = CURSEG_WARM_DATA;
1218 }
1219 curseg = CURSEG_I(sbi, type);
1220
1221 mutex_lock(&curseg->curseg_mutex);
1222 mutex_lock(&sit_i->sentry_lock);
1223
1224 old_cursegno = curseg->segno;
1225
1226 /* change the current segment */
1227 if (segno != curseg->segno) {
1228 curseg->next_segno = segno;
1229 change_curseg(sbi, type, true);
1230 }
1231
Jaegeuk Kim491c0852014-02-04 13:01:10 +09001232 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
Haicheng Lie79efe32013-06-13 16:59:27 +08001233 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001234
1235 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001236 locate_dirty_segment(sbi, old_cursegno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001237
1238 mutex_unlock(&sit_i->sentry_lock);
1239 mutex_unlock(&curseg->curseg_mutex);
1240}
1241
Chao Yudf0f8dc2014-03-22 14:57:23 +08001242static inline bool is_merged_page(struct f2fs_sb_info *sbi,
1243 struct page *page, enum page_type type)
1244{
1245 enum page_type btype = PAGE_TYPE_OF_BIO(type);
1246 struct f2fs_bio_info *io = &sbi->write_io[btype];
Chao Yudf0f8dc2014-03-22 14:57:23 +08001247 struct bio_vec *bvec;
1248 int i;
1249
1250 down_read(&io->io_rwsem);
Jaegeuk Kimce234472014-04-02 09:04:42 +09001251 if (!io->bio)
Chao Yudf0f8dc2014-03-22 14:57:23 +08001252 goto out;
1253
Jaegeuk Kimce234472014-04-02 09:04:42 +09001254 bio_for_each_segment_all(bvec, io->bio, i) {
Chao Yudf0f8dc2014-03-22 14:57:23 +08001255 if (page == bvec->bv_page) {
1256 up_read(&io->io_rwsem);
1257 return true;
1258 }
1259 }
1260
1261out:
1262 up_read(&io->io_rwsem);
1263 return false;
1264}
1265
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001266void f2fs_wait_on_page_writeback(struct page *page,
Yuan Zhong5514f0a2014-01-10 07:26:14 +00001267 enum page_type type)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001268{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001269 if (PageWriteback(page)) {
Jaegeuk Kim40813632014-09-02 15:31:18 -07001270 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
1271
Chao Yudf0f8dc2014-03-22 14:57:23 +08001272 if (is_merged_page(sbi, page, type))
1273 f2fs_submit_merged_bio(sbi, type, WRITE);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001274 wait_on_page_writeback(page);
1275 }
1276}
1277
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001278static int read_compacted_summaries(struct f2fs_sb_info *sbi)
1279{
1280 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1281 struct curseg_info *seg_i;
1282 unsigned char *kaddr;
1283 struct page *page;
1284 block_t start;
1285 int i, j, offset;
1286
1287 start = start_sum_block(sbi);
1288
1289 page = get_meta_page(sbi, start++);
1290 kaddr = (unsigned char *)page_address(page);
1291
1292 /* Step 1: restore nat cache */
1293 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1294 memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
1295
1296 /* Step 2: restore sit cache */
1297 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1298 memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
1299 SUM_JOURNAL_SIZE);
1300 offset = 2 * SUM_JOURNAL_SIZE;
1301
1302 /* Step 3: restore summary entries */
1303 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1304 unsigned short blk_off;
1305 unsigned int segno;
1306
1307 seg_i = CURSEG_I(sbi, i);
1308 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
1309 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
1310 seg_i->next_segno = segno;
1311 reset_curseg(sbi, i, 0);
1312 seg_i->alloc_type = ckpt->alloc_type[i];
1313 seg_i->next_blkoff = blk_off;
1314
1315 if (seg_i->alloc_type == SSR)
1316 blk_off = sbi->blocks_per_seg;
1317
1318 for (j = 0; j < blk_off; j++) {
1319 struct f2fs_summary *s;
1320 s = (struct f2fs_summary *)(kaddr + offset);
1321 seg_i->sum_blk->entries[j] = *s;
1322 offset += SUMMARY_SIZE;
1323 if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1324 SUM_FOOTER_SIZE)
1325 continue;
1326
1327 f2fs_put_page(page, 1);
1328 page = NULL;
1329
1330 page = get_meta_page(sbi, start++);
1331 kaddr = (unsigned char *)page_address(page);
1332 offset = 0;
1333 }
1334 }
1335 f2fs_put_page(page, 1);
1336 return 0;
1337}
1338
1339static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
1340{
1341 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1342 struct f2fs_summary_block *sum;
1343 struct curseg_info *curseg;
1344 struct page *new;
1345 unsigned short blk_off;
1346 unsigned int segno = 0;
1347 block_t blk_addr = 0;
1348
1349 /* get segment number and block addr */
1350 if (IS_DATASEG(type)) {
1351 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
1352 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
1353 CURSEG_HOT_DATA]);
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001354 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001355 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
1356 else
1357 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
1358 } else {
1359 segno = le32_to_cpu(ckpt->cur_node_segno[type -
1360 CURSEG_HOT_NODE]);
1361 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
1362 CURSEG_HOT_NODE]);
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001363 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001364 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
1365 type - CURSEG_HOT_NODE);
1366 else
1367 blk_addr = GET_SUM_BLOCK(sbi, segno);
1368 }
1369
1370 new = get_meta_page(sbi, blk_addr);
1371 sum = (struct f2fs_summary_block *)page_address(new);
1372
1373 if (IS_NODESEG(type)) {
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001374 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001375 struct f2fs_summary *ns = &sum->entries[0];
1376 int i;
1377 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
1378 ns->version = 0;
1379 ns->ofs_in_node = 0;
1380 }
1381 } else {
Gu Zhengd6537882014-03-07 18:43:36 +08001382 int err;
1383
1384 err = restore_node_summary(sbi, segno, sum);
1385 if (err) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001386 f2fs_put_page(new, 1);
Gu Zhengd6537882014-03-07 18:43:36 +08001387 return err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001388 }
1389 }
1390 }
1391
1392 /* set uncompleted segment to curseg */
1393 curseg = CURSEG_I(sbi, type);
1394 mutex_lock(&curseg->curseg_mutex);
1395 memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
1396 curseg->next_segno = segno;
1397 reset_curseg(sbi, type, 0);
1398 curseg->alloc_type = ckpt->alloc_type[type];
1399 curseg->next_blkoff = blk_off;
1400 mutex_unlock(&curseg->curseg_mutex);
1401 f2fs_put_page(new, 1);
1402 return 0;
1403}
1404
1405static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
1406{
1407 int type = CURSEG_HOT_DATA;
Chao Yue4fc5fb2014-03-17 16:36:24 +08001408 int err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001409
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001410 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001411 /* restore for compacted data summary */
1412 if (read_compacted_summaries(sbi))
1413 return -EINVAL;
1414 type = CURSEG_HOT_NODE;
1415 }
1416
Chao Yue4fc5fb2014-03-17 16:36:24 +08001417 for (; type <= CURSEG_COLD_NODE; type++) {
1418 err = read_normal_summaries(sbi, type);
1419 if (err)
1420 return err;
1421 }
1422
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001423 return 0;
1424}
1425
1426static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
1427{
1428 struct page *page;
1429 unsigned char *kaddr;
1430 struct f2fs_summary *summary;
1431 struct curseg_info *seg_i;
1432 int written_size = 0;
1433 int i, j;
1434
1435 page = grab_meta_page(sbi, blkaddr++);
1436 kaddr = (unsigned char *)page_address(page);
1437
1438 /* Step 1: write nat cache */
1439 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1440 memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
1441 written_size += SUM_JOURNAL_SIZE;
1442
1443 /* Step 2: write sit cache */
1444 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1445 memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
1446 SUM_JOURNAL_SIZE);
1447 written_size += SUM_JOURNAL_SIZE;
1448
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001449 /* Step 3: write summary entries */
1450 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1451 unsigned short blkoff;
1452 seg_i = CURSEG_I(sbi, i);
1453 if (sbi->ckpt->alloc_type[i] == SSR)
1454 blkoff = sbi->blocks_per_seg;
1455 else
1456 blkoff = curseg_blkoff(sbi, i);
1457
1458 for (j = 0; j < blkoff; j++) {
1459 if (!page) {
1460 page = grab_meta_page(sbi, blkaddr++);
1461 kaddr = (unsigned char *)page_address(page);
1462 written_size = 0;
1463 }
1464 summary = (struct f2fs_summary *)(kaddr + written_size);
1465 *summary = seg_i->sum_blk->entries[j];
1466 written_size += SUMMARY_SIZE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001467
1468 if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1469 SUM_FOOTER_SIZE)
1470 continue;
1471
Chao Yue8d61a72013-10-24 15:08:28 +08001472 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001473 f2fs_put_page(page, 1);
1474 page = NULL;
1475 }
1476 }
Chao Yue8d61a72013-10-24 15:08:28 +08001477 if (page) {
1478 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001479 f2fs_put_page(page, 1);
Chao Yue8d61a72013-10-24 15:08:28 +08001480 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001481}
1482
1483static void write_normal_summaries(struct f2fs_sb_info *sbi,
1484 block_t blkaddr, int type)
1485{
1486 int i, end;
1487 if (IS_DATASEG(type))
1488 end = type + NR_CURSEG_DATA_TYPE;
1489 else
1490 end = type + NR_CURSEG_NODE_TYPE;
1491
1492 for (i = type; i < end; i++) {
1493 struct curseg_info *sum = CURSEG_I(sbi, i);
1494 mutex_lock(&sum->curseg_mutex);
1495 write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
1496 mutex_unlock(&sum->curseg_mutex);
1497 }
1498}
1499
1500void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1501{
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001502 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001503 write_compacted_summaries(sbi, start_blk);
1504 else
1505 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
1506}
1507
1508void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1509{
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001510 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001511 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001512}
1513
1514int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
1515 unsigned int val, int alloc)
1516{
1517 int i;
1518
1519 if (type == NAT_JOURNAL) {
1520 for (i = 0; i < nats_in_cursum(sum); i++) {
1521 if (le32_to_cpu(nid_in_journal(sum, i)) == val)
1522 return i;
1523 }
1524 if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
1525 return update_nats_in_cursum(sum, 1);
1526 } else if (type == SIT_JOURNAL) {
1527 for (i = 0; i < sits_in_cursum(sum); i++)
1528 if (le32_to_cpu(segno_in_journal(sum, i)) == val)
1529 return i;
1530 if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
1531 return update_sits_in_cursum(sum, 1);
1532 }
1533 return -1;
1534}
1535
1536static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
1537 unsigned int segno)
1538{
1539 struct sit_info *sit_i = SIT_I(sbi);
Chao Yud3a14af2014-09-04 18:11:47 +08001540 unsigned int offset = SIT_BLOCK_OFFSET(segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001541 block_t blk_addr = sit_i->sit_base_addr + offset;
1542
1543 check_seg_range(sbi, segno);
1544
1545 /* calculate sit block address */
1546 if (f2fs_test_bit(offset, sit_i->sit_bitmap))
1547 blk_addr += sit_i->sit_blocks;
1548
1549 return get_meta_page(sbi, blk_addr);
1550}
1551
1552static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
1553 unsigned int start)
1554{
1555 struct sit_info *sit_i = SIT_I(sbi);
1556 struct page *src_page, *dst_page;
1557 pgoff_t src_off, dst_off;
1558 void *src_addr, *dst_addr;
1559
1560 src_off = current_sit_addr(sbi, start);
1561 dst_off = next_sit_addr(sbi, src_off);
1562
1563 /* get current sit block page without lock */
1564 src_page = get_meta_page(sbi, src_off);
1565 dst_page = grab_meta_page(sbi, dst_off);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001566 f2fs_bug_on(sbi, PageDirty(src_page));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001567
1568 src_addr = page_address(src_page);
1569 dst_addr = page_address(dst_page);
1570 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
1571
1572 set_page_dirty(dst_page);
1573 f2fs_put_page(src_page, 1);
1574
1575 set_to_next_sit(sit_i, start);
1576
1577 return dst_page;
1578}
1579
Chao Yu184a5cd2014-09-04 18:13:01 +08001580static struct sit_entry_set *grab_sit_entry_set(void)
1581{
1582 struct sit_entry_set *ses =
1583 f2fs_kmem_cache_alloc(sit_entry_set_slab, GFP_ATOMIC);
1584
1585 ses->entry_cnt = 0;
1586 INIT_LIST_HEAD(&ses->set_list);
1587 return ses;
1588}
1589
1590static void release_sit_entry_set(struct sit_entry_set *ses)
1591{
1592 list_del(&ses->set_list);
1593 kmem_cache_free(sit_entry_set_slab, ses);
1594}
1595
1596static void adjust_sit_entry_set(struct sit_entry_set *ses,
1597 struct list_head *head)
1598{
1599 struct sit_entry_set *next = ses;
1600
1601 if (list_is_last(&ses->set_list, head))
1602 return;
1603
1604 list_for_each_entry_continue(next, head, set_list)
1605 if (ses->entry_cnt <= next->entry_cnt)
1606 break;
1607
1608 list_move_tail(&ses->set_list, &next->set_list);
1609}
1610
1611static void add_sit_entry(unsigned int segno, struct list_head *head)
1612{
1613 struct sit_entry_set *ses;
1614 unsigned int start_segno = START_SEGNO(segno);
1615
1616 list_for_each_entry(ses, head, set_list) {
1617 if (ses->start_segno == start_segno) {
1618 ses->entry_cnt++;
1619 adjust_sit_entry_set(ses, head);
1620 return;
1621 }
1622 }
1623
1624 ses = grab_sit_entry_set();
1625
1626 ses->start_segno = start_segno;
1627 ses->entry_cnt++;
1628 list_add(&ses->set_list, head);
1629}
1630
1631static void add_sits_in_set(struct f2fs_sb_info *sbi)
1632{
1633 struct f2fs_sm_info *sm_info = SM_I(sbi);
1634 struct list_head *set_list = &sm_info->sit_entry_set;
1635 unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap;
Chao Yu184a5cd2014-09-04 18:13:01 +08001636 unsigned int segno;
1637
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001638 for_each_set_bit(segno, bitmap, MAIN_SEGS(sbi))
Chao Yu184a5cd2014-09-04 18:13:01 +08001639 add_sit_entry(segno, set_list);
1640}
1641
1642static void remove_sits_in_journal(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001643{
1644 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1645 struct f2fs_summary_block *sum = curseg->sum_blk;
1646 int i;
1647
Chao Yu184a5cd2014-09-04 18:13:01 +08001648 for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
1649 unsigned int segno;
1650 bool dirtied;
1651
1652 segno = le32_to_cpu(segno_in_journal(sum, i));
1653 dirtied = __mark_sit_entry_dirty(sbi, segno);
1654
1655 if (!dirtied)
1656 add_sit_entry(segno, &SM_I(sbi)->sit_entry_set);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001657 }
Chao Yu184a5cd2014-09-04 18:13:01 +08001658 update_sits_in_cursum(sum, -sits_in_cursum(sum));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001659}
1660
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001661/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001662 * CP calls this function, which flushes SIT entries including sit_journal,
1663 * and moves prefree segs to free segs.
1664 */
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001665void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001666{
1667 struct sit_info *sit_i = SIT_I(sbi);
1668 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
1669 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1670 struct f2fs_summary_block *sum = curseg->sum_blk;
Chao Yu184a5cd2014-09-04 18:13:01 +08001671 struct sit_entry_set *ses, *tmp;
1672 struct list_head *head = &SM_I(sbi)->sit_entry_set;
Chao Yu184a5cd2014-09-04 18:13:01 +08001673 bool to_journal = true;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001674 struct seg_entry *se;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001675
1676 mutex_lock(&curseg->curseg_mutex);
1677 mutex_lock(&sit_i->sentry_lock);
1678
1679 /*
Chao Yu184a5cd2014-09-04 18:13:01 +08001680 * add and account sit entries of dirty bitmap in sit entry
1681 * set temporarily
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001682 */
Chao Yu184a5cd2014-09-04 18:13:01 +08001683 add_sits_in_set(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001684
Chao Yu184a5cd2014-09-04 18:13:01 +08001685 /*
1686 * if there are no enough space in journal to store dirty sit
1687 * entries, remove all entries from journal and add and account
1688 * them in sit entry set.
1689 */
1690 if (!__has_cursum_space(sum, sit_i->dirty_sentries, SIT_JOURNAL))
1691 remove_sits_in_journal(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001692
Chao Yu184a5cd2014-09-04 18:13:01 +08001693 if (!sit_i->dirty_sentries)
1694 goto out;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001695
Chao Yu184a5cd2014-09-04 18:13:01 +08001696 /*
1697 * there are two steps to flush sit entries:
1698 * #1, flush sit entries to journal in current cold data summary block.
1699 * #2, flush sit entries to sit page.
1700 */
1701 list_for_each_entry_safe(ses, tmp, head, set_list) {
1702 struct page *page;
1703 struct f2fs_sit_block *raw_sit = NULL;
1704 unsigned int start_segno = ses->start_segno;
1705 unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001706 (unsigned long)MAIN_SEGS(sbi));
Chao Yu184a5cd2014-09-04 18:13:01 +08001707 unsigned int segno = start_segno;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001708
Chao Yu184a5cd2014-09-04 18:13:01 +08001709 if (to_journal &&
1710 !__has_cursum_space(sum, ses->entry_cnt, SIT_JOURNAL))
1711 to_journal = false;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001712
Chao Yu184a5cd2014-09-04 18:13:01 +08001713 if (!to_journal) {
1714 page = get_next_sit_page(sbi, start_segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001715 raw_sit = page_address(page);
1716 }
1717
Chao Yu184a5cd2014-09-04 18:13:01 +08001718 /* flush dirty sit entries in region of current sit set */
1719 for_each_set_bit_from(segno, bitmap, end) {
1720 int offset, sit_offset;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001721
1722 se = get_seg_entry(sbi, segno);
Chao Yu184a5cd2014-09-04 18:13:01 +08001723
1724 /* add discard candidates */
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001725 if (SM_I(sbi)->nr_discards < SM_I(sbi)->max_discards) {
1726 cpc->trim_start = segno;
1727 add_discard_addrs(sbi, cpc);
1728 }
Chao Yu184a5cd2014-09-04 18:13:01 +08001729
1730 if (to_journal) {
1731 offset = lookup_journal_in_cursum(sum,
1732 SIT_JOURNAL, segno, 1);
1733 f2fs_bug_on(sbi, offset < 0);
1734 segno_in_journal(sum, offset) =
1735 cpu_to_le32(segno);
1736 seg_info_to_raw_sit(se,
1737 &sit_in_journal(sum, offset));
1738 } else {
1739 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
1740 seg_info_to_raw_sit(se,
1741 &raw_sit->entries[sit_offset]);
1742 }
1743
1744 __clear_bit(segno, bitmap);
1745 sit_i->dirty_sentries--;
1746 ses->entry_cnt--;
1747 }
1748
1749 if (!to_journal)
1750 f2fs_put_page(page, 1);
1751
1752 f2fs_bug_on(sbi, ses->entry_cnt);
1753 release_sit_entry_set(ses);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001754 }
Chao Yu184a5cd2014-09-04 18:13:01 +08001755
1756 f2fs_bug_on(sbi, !list_empty(head));
1757 f2fs_bug_on(sbi, sit_i->dirty_sentries);
Chao Yu184a5cd2014-09-04 18:13:01 +08001758out:
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001759 if (cpc->reason == CP_DISCARD) {
1760 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++)
1761 add_discard_addrs(sbi, cpc);
1762 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001763 mutex_unlock(&sit_i->sentry_lock);
1764 mutex_unlock(&curseg->curseg_mutex);
1765
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001766 set_prefree_as_free_segments(sbi);
1767}
1768
1769static int build_sit_info(struct f2fs_sb_info *sbi)
1770{
1771 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1772 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1773 struct sit_info *sit_i;
1774 unsigned int sit_segs, start;
1775 char *src_bitmap, *dst_bitmap;
1776 unsigned int bitmap_size;
1777
1778 /* allocate memory for SIT information */
1779 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
1780 if (!sit_i)
1781 return -ENOMEM;
1782
1783 SM_I(sbi)->sit_info = sit_i;
1784
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001785 sit_i->sentries = vzalloc(MAIN_SEGS(sbi) * sizeof(struct seg_entry));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001786 if (!sit_i->sentries)
1787 return -ENOMEM;
1788
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001789 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001790 sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1791 if (!sit_i->dirty_sentries_bitmap)
1792 return -ENOMEM;
1793
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001794 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001795 sit_i->sentries[start].cur_valid_map
1796 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1797 sit_i->sentries[start].ckpt_valid_map
1798 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1799 if (!sit_i->sentries[start].cur_valid_map
1800 || !sit_i->sentries[start].ckpt_valid_map)
1801 return -ENOMEM;
1802 }
1803
1804 if (sbi->segs_per_sec > 1) {
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001805 sit_i->sec_entries = vzalloc(MAIN_SECS(sbi) *
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001806 sizeof(struct sec_entry));
1807 if (!sit_i->sec_entries)
1808 return -ENOMEM;
1809 }
1810
1811 /* get information related with SIT */
1812 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
1813
1814 /* setup SIT bitmap from ckeckpoint pack */
1815 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
1816 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
1817
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001818 dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001819 if (!dst_bitmap)
1820 return -ENOMEM;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001821
1822 /* init SIT information */
1823 sit_i->s_ops = &default_salloc_ops;
1824
1825 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
1826 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
1827 sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
1828 sit_i->sit_bitmap = dst_bitmap;
1829 sit_i->bitmap_size = bitmap_size;
1830 sit_i->dirty_sentries = 0;
1831 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
1832 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
1833 sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
1834 mutex_init(&sit_i->sentry_lock);
1835 return 0;
1836}
1837
1838static int build_free_segmap(struct f2fs_sb_info *sbi)
1839{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001840 struct free_segmap_info *free_i;
1841 unsigned int bitmap_size, sec_bitmap_size;
1842
1843 /* allocate memory for free segmap information */
1844 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
1845 if (!free_i)
1846 return -ENOMEM;
1847
1848 SM_I(sbi)->free_info = free_i;
1849
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001850 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001851 free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL);
1852 if (!free_i->free_segmap)
1853 return -ENOMEM;
1854
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001855 sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001856 free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL);
1857 if (!free_i->free_secmap)
1858 return -ENOMEM;
1859
1860 /* set all segments as dirty temporarily */
1861 memset(free_i->free_segmap, 0xff, bitmap_size);
1862 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
1863
1864 /* init free segmap information */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001865 free_i->start_segno = GET_SEGNO_FROM_SEG0(sbi, MAIN_BLKADDR(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001866 free_i->free_segments = 0;
1867 free_i->free_sections = 0;
1868 rwlock_init(&free_i->segmap_lock);
1869 return 0;
1870}
1871
1872static int build_curseg(struct f2fs_sb_info *sbi)
1873{
Namjae Jeon1042d602012-12-01 10:56:13 +09001874 struct curseg_info *array;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001875 int i;
1876
Fabian Frederickb434bab2014-06-23 18:39:15 +02001877 array = kcalloc(NR_CURSEG_TYPE, sizeof(*array), GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001878 if (!array)
1879 return -ENOMEM;
1880
1881 SM_I(sbi)->curseg_array = array;
1882
1883 for (i = 0; i < NR_CURSEG_TYPE; i++) {
1884 mutex_init(&array[i].curseg_mutex);
1885 array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
1886 if (!array[i].sum_blk)
1887 return -ENOMEM;
1888 array[i].segno = NULL_SEGNO;
1889 array[i].next_blkoff = 0;
1890 }
1891 return restore_curseg_summaries(sbi);
1892}
1893
1894static void build_sit_entries(struct f2fs_sb_info *sbi)
1895{
1896 struct sit_info *sit_i = SIT_I(sbi);
1897 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1898 struct f2fs_summary_block *sum = curseg->sum_blk;
Chao Yu74de5932013-11-22 09:09:59 +08001899 int sit_blk_cnt = SIT_BLK_CNT(sbi);
1900 unsigned int i, start, end;
1901 unsigned int readed, start_blk = 0;
Jaegeuk Kim90a893c2014-09-22 16:21:07 -07001902 int nrpages = MAX_BIO_BLOCKS(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001903
Chao Yu74de5932013-11-22 09:09:59 +08001904 do {
Chao Yu662befd2014-02-07 16:11:53 +08001905 readed = ra_meta_pages(sbi, start_blk, nrpages, META_SIT);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001906
Chao Yu74de5932013-11-22 09:09:59 +08001907 start = start_blk * sit_i->sents_per_block;
1908 end = (start_blk + readed) * sit_i->sents_per_block;
1909
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001910 for (; start < end && start < MAIN_SEGS(sbi); start++) {
Chao Yu74de5932013-11-22 09:09:59 +08001911 struct seg_entry *se = &sit_i->sentries[start];
1912 struct f2fs_sit_block *sit_blk;
1913 struct f2fs_sit_entry sit;
1914 struct page *page;
1915
1916 mutex_lock(&curseg->curseg_mutex);
1917 for (i = 0; i < sits_in_cursum(sum); i++) {
Chris Fries6c311ec2014-01-17 14:44:39 -06001918 if (le32_to_cpu(segno_in_journal(sum, i))
1919 == start) {
Chao Yu74de5932013-11-22 09:09:59 +08001920 sit = sit_in_journal(sum, i);
1921 mutex_unlock(&curseg->curseg_mutex);
1922 goto got_it;
1923 }
1924 }
1925 mutex_unlock(&curseg->curseg_mutex);
1926
1927 page = get_current_sit_page(sbi, start);
1928 sit_blk = (struct f2fs_sit_block *)page_address(page);
1929 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
1930 f2fs_put_page(page, 1);
1931got_it:
1932 check_block_count(sbi, start, &sit);
1933 seg_info_from_raw_sit(se, &sit);
1934 if (sbi->segs_per_sec > 1) {
1935 struct sec_entry *e = get_sec_entry(sbi, start);
1936 e->valid_blocks += se->valid_blocks;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001937 }
1938 }
Chao Yu74de5932013-11-22 09:09:59 +08001939 start_blk += readed;
1940 } while (start_blk < sit_blk_cnt);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001941}
1942
1943static void init_free_segmap(struct f2fs_sb_info *sbi)
1944{
1945 unsigned int start;
1946 int type;
1947
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001948 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001949 struct seg_entry *sentry = get_seg_entry(sbi, start);
1950 if (!sentry->valid_blocks)
1951 __set_free(sbi, start);
1952 }
1953
1954 /* set use the current segments */
1955 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
1956 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
1957 __set_test_and_inuse(sbi, curseg_t->segno);
1958 }
1959}
1960
1961static void init_dirty_segmap(struct f2fs_sb_info *sbi)
1962{
1963 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1964 struct free_segmap_info *free_i = FREE_I(sbi);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001965 unsigned int segno = 0, offset = 0;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001966 unsigned short valid_blocks;
1967
Namjae Jeon8736fbf2013-06-16 09:49:11 +09001968 while (1) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001969 /* find dirty segment based on free segmap */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001970 segno = find_next_inuse(free_i, MAIN_SEGS(sbi), offset);
1971 if (segno >= MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001972 break;
1973 offset = segno + 1;
1974 valid_blocks = get_valid_blocks(sbi, segno, 0);
Jaegeuk Kimec325b52014-09-02 16:24:11 -07001975 if (valid_blocks == sbi->blocks_per_seg || !valid_blocks)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001976 continue;
Jaegeuk Kimec325b52014-09-02 16:24:11 -07001977 if (valid_blocks > sbi->blocks_per_seg) {
1978 f2fs_bug_on(sbi, 1);
1979 continue;
1980 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001981 mutex_lock(&dirty_i->seglist_lock);
1982 __locate_dirty_segment(sbi, segno, DIRTY);
1983 mutex_unlock(&dirty_i->seglist_lock);
1984 }
1985}
1986
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001987static int init_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001988{
1989 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001990 unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001991
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001992 dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL);
1993 if (!dirty_i->victim_secmap)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001994 return -ENOMEM;
1995 return 0;
1996}
1997
1998static int build_dirty_segmap(struct f2fs_sb_info *sbi)
1999{
2000 struct dirty_seglist_info *dirty_i;
2001 unsigned int bitmap_size, i;
2002
2003 /* allocate memory for dirty segments list information */
2004 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
2005 if (!dirty_i)
2006 return -ENOMEM;
2007
2008 SM_I(sbi)->dirty_info = dirty_i;
2009 mutex_init(&dirty_i->seglist_lock);
2010
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002011 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002012
2013 for (i = 0; i < NR_DIRTY_TYPE; i++) {
2014 dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002015 if (!dirty_i->dirty_segmap[i])
2016 return -ENOMEM;
2017 }
2018
2019 init_dirty_segmap(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002020 return init_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002021}
2022
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002023/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002024 * Update min, max modified time for cost-benefit GC algorithm
2025 */
2026static void init_min_max_mtime(struct f2fs_sb_info *sbi)
2027{
2028 struct sit_info *sit_i = SIT_I(sbi);
2029 unsigned int segno;
2030
2031 mutex_lock(&sit_i->sentry_lock);
2032
2033 sit_i->min_mtime = LLONG_MAX;
2034
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002035 for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002036 unsigned int i;
2037 unsigned long long mtime = 0;
2038
2039 for (i = 0; i < sbi->segs_per_sec; i++)
2040 mtime += get_seg_entry(sbi, segno + i)->mtime;
2041
2042 mtime = div_u64(mtime, sbi->segs_per_sec);
2043
2044 if (sit_i->min_mtime > mtime)
2045 sit_i->min_mtime = mtime;
2046 }
2047 sit_i->max_mtime = get_mtime(sbi);
2048 mutex_unlock(&sit_i->sentry_lock);
2049}
2050
2051int build_segment_manager(struct f2fs_sb_info *sbi)
2052{
2053 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
2054 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Namjae Jeon1042d602012-12-01 10:56:13 +09002055 struct f2fs_sm_info *sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002056 int err;
2057
2058 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
2059 if (!sm_info)
2060 return -ENOMEM;
2061
2062 /* init sm info */
2063 sbi->sm_info = sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002064 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
2065 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
2066 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
2067 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
2068 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
2069 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
2070 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
Jaegeuk Kim58c41032014-03-19 14:17:21 +09002071 sm_info->rec_prefree_segments = sm_info->main_segments *
2072 DEF_RECLAIM_PREFREE_SEGMENTS / 100;
Jaegeuk Kim9b5f1362014-09-16 18:30:54 -07002073 sm_info->ipu_policy = 1 << F2FS_IPU_FSYNC;
Jaegeuk Kim216fbd62013-11-07 13:13:42 +09002074 sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -07002075 sm_info->min_fsync_blocks = DEF_MIN_FSYNC_BLOCKS;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002076
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002077 INIT_LIST_HEAD(&sm_info->discard_list);
2078 sm_info->nr_discards = 0;
2079 sm_info->max_discards = 0;
2080
Chao Yu184a5cd2014-09-04 18:13:01 +08002081 INIT_LIST_HEAD(&sm_info->sit_entry_set);
2082
Gu Zhengb270ad62014-04-11 17:49:55 +08002083 if (test_opt(sbi, FLUSH_MERGE) && !f2fs_readonly(sbi->sb)) {
Gu Zheng2163d192014-04-27 14:21:33 +08002084 err = create_flush_cmd_control(sbi);
2085 if (err)
Gu Zhenga688b9d9e2014-04-27 14:21:21 +08002086 return err;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +09002087 }
2088
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002089 err = build_sit_info(sbi);
2090 if (err)
2091 return err;
2092 err = build_free_segmap(sbi);
2093 if (err)
2094 return err;
2095 err = build_curseg(sbi);
2096 if (err)
2097 return err;
2098
2099 /* reinit free segmap based on SIT */
2100 build_sit_entries(sbi);
2101
2102 init_free_segmap(sbi);
2103 err = build_dirty_segmap(sbi);
2104 if (err)
2105 return err;
2106
2107 init_min_max_mtime(sbi);
2108 return 0;
2109}
2110
2111static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
2112 enum dirty_type dirty_type)
2113{
2114 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2115
2116 mutex_lock(&dirty_i->seglist_lock);
2117 kfree(dirty_i->dirty_segmap[dirty_type]);
2118 dirty_i->nr_dirty[dirty_type] = 0;
2119 mutex_unlock(&dirty_i->seglist_lock);
2120}
2121
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002122static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002123{
2124 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002125 kfree(dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002126}
2127
2128static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
2129{
2130 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2131 int i;
2132
2133 if (!dirty_i)
2134 return;
2135
2136 /* discard pre-free/dirty segments list */
2137 for (i = 0; i < NR_DIRTY_TYPE; i++)
2138 discard_dirty_segmap(sbi, i);
2139
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002140 destroy_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002141 SM_I(sbi)->dirty_info = NULL;
2142 kfree(dirty_i);
2143}
2144
2145static void destroy_curseg(struct f2fs_sb_info *sbi)
2146{
2147 struct curseg_info *array = SM_I(sbi)->curseg_array;
2148 int i;
2149
2150 if (!array)
2151 return;
2152 SM_I(sbi)->curseg_array = NULL;
2153 for (i = 0; i < NR_CURSEG_TYPE; i++)
2154 kfree(array[i].sum_blk);
2155 kfree(array);
2156}
2157
2158static void destroy_free_segmap(struct f2fs_sb_info *sbi)
2159{
2160 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
2161 if (!free_i)
2162 return;
2163 SM_I(sbi)->free_info = NULL;
2164 kfree(free_i->free_segmap);
2165 kfree(free_i->free_secmap);
2166 kfree(free_i);
2167}
2168
2169static void destroy_sit_info(struct f2fs_sb_info *sbi)
2170{
2171 struct sit_info *sit_i = SIT_I(sbi);
2172 unsigned int start;
2173
2174 if (!sit_i)
2175 return;
2176
2177 if (sit_i->sentries) {
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002178 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002179 kfree(sit_i->sentries[start].cur_valid_map);
2180 kfree(sit_i->sentries[start].ckpt_valid_map);
2181 }
2182 }
2183 vfree(sit_i->sentries);
2184 vfree(sit_i->sec_entries);
2185 kfree(sit_i->dirty_sentries_bitmap);
2186
2187 SM_I(sbi)->sit_info = NULL;
2188 kfree(sit_i->sit_bitmap);
2189 kfree(sit_i);
2190}
2191
2192void destroy_segment_manager(struct f2fs_sb_info *sbi)
2193{
2194 struct f2fs_sm_info *sm_info = SM_I(sbi);
Gu Zhenga688b9d9e2014-04-27 14:21:21 +08002195
Chao Yu3b03f722013-11-06 09:12:04 +08002196 if (!sm_info)
2197 return;
Gu Zheng2163d192014-04-27 14:21:33 +08002198 destroy_flush_cmd_control(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002199 destroy_dirty_segmap(sbi);
2200 destroy_curseg(sbi);
2201 destroy_free_segmap(sbi);
2202 destroy_sit_info(sbi);
2203 sbi->sm_info = NULL;
2204 kfree(sm_info);
2205}
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002206
2207int __init create_segment_manager_caches(void)
2208{
2209 discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
Gu Zhenge8512d22014-03-07 18:43:28 +08002210 sizeof(struct discard_entry));
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002211 if (!discard_entry_slab)
Chao Yu184a5cd2014-09-04 18:13:01 +08002212 goto fail;
2213
2214 sit_entry_set_slab = f2fs_kmem_cache_create("sit_entry_set",
2215 sizeof(struct nat_entry_set));
2216 if (!sit_entry_set_slab)
2217 goto destory_discard_entry;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002218
2219 inmem_entry_slab = f2fs_kmem_cache_create("inmem_page_entry",
2220 sizeof(struct inmem_pages));
2221 if (!inmem_entry_slab)
2222 goto destroy_sit_entry_set;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002223 return 0;
Chao Yu184a5cd2014-09-04 18:13:01 +08002224
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002225destroy_sit_entry_set:
2226 kmem_cache_destroy(sit_entry_set_slab);
Chao Yu184a5cd2014-09-04 18:13:01 +08002227destory_discard_entry:
2228 kmem_cache_destroy(discard_entry_slab);
2229fail:
2230 return -ENOMEM;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002231}
2232
2233void destroy_segment_manager_caches(void)
2234{
Chao Yu184a5cd2014-09-04 18:13:01 +08002235 kmem_cache_destroy(sit_entry_set_slab);
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002236 kmem_cache_destroy(discard_entry_slab);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002237 kmem_cache_destroy(inmem_entry_slab);
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002238}