blob: 7254c11344bee01185ae72278c4e18fbd9274572 [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"
Jaegeuk Kim9e4ded32014-12-17 19:58:58 -080023#include "trace.h"
Namjae Jeon6ec178d2013-04-23 17:51:43 +090024#include <trace/events/f2fs.h>
Jaegeuk Kim351df4b2012-11-02 17:09:16 +090025
Changman Lee9a7f1432013-11-15 10:42:51 +090026#define __reverse_ffz(x) __reverse_ffs(~(x))
27
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +090028static struct kmem_cache *discard_entry_slab;
Chao Yu184a5cd2014-09-04 18:13:01 +080029static struct kmem_cache *sit_entry_set_slab;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -070030static struct kmem_cache *inmem_entry_slab;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +090031
Changman Lee9a7f1432013-11-15 10:42:51 +090032/*
33 * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since
34 * MSB and LSB are reversed in a byte by f2fs_set_bit.
35 */
36static inline unsigned long __reverse_ffs(unsigned long word)
37{
38 int num = 0;
39
40#if BITS_PER_LONG == 64
41 if ((word & 0xffffffff) == 0) {
42 num += 32;
43 word >>= 32;
44 }
45#endif
46 if ((word & 0xffff) == 0) {
47 num += 16;
48 word >>= 16;
49 }
50 if ((word & 0xff) == 0) {
51 num += 8;
52 word >>= 8;
53 }
54 if ((word & 0xf0) == 0)
55 num += 4;
56 else
57 word >>= 4;
58 if ((word & 0xc) == 0)
59 num += 2;
60 else
61 word >>= 2;
62 if ((word & 0x2) == 0)
63 num += 1;
64 return num;
65}
66
67/*
arter97e1c42042014-08-06 23:22:50 +090068 * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c because
Changman Lee9a7f1432013-11-15 10:42:51 +090069 * f2fs_set_bit makes MSB and LSB reversed in a byte.
70 * Example:
71 * LSB <--> MSB
72 * f2fs_set_bit(0, bitmap) => 0000 0001
73 * f2fs_set_bit(7, bitmap) => 1000 0000
74 */
75static unsigned long __find_rev_next_bit(const unsigned long *addr,
76 unsigned long size, unsigned long offset)
77{
Jaegeuk Kime19ef5272015-05-18 11:45:15 -070078 while (!f2fs_test_bit(offset, (unsigned char *)addr))
79 offset++;
80
81 if (offset > size)
82 offset = size;
83
84 return offset;
85#if 0
Changman Lee9a7f1432013-11-15 10:42:51 +090086 const unsigned long *p = addr + BIT_WORD(offset);
87 unsigned long result = offset & ~(BITS_PER_LONG - 1);
88 unsigned long tmp;
89 unsigned long mask, submask;
90 unsigned long quot, rest;
91
92 if (offset >= size)
93 return size;
94
95 size -= result;
96 offset %= BITS_PER_LONG;
97 if (!offset)
98 goto aligned;
99
100 tmp = *(p++);
101 quot = (offset >> 3) << 3;
102 rest = offset & 0x7;
103 mask = ~0UL << quot;
104 submask = (unsigned char)(0xff << rest) >> rest;
105 submask <<= quot;
106 mask &= submask;
107 tmp &= mask;
108 if (size < BITS_PER_LONG)
109 goto found_first;
110 if (tmp)
111 goto found_middle;
112
113 size -= BITS_PER_LONG;
114 result += BITS_PER_LONG;
115aligned:
116 while (size & ~(BITS_PER_LONG-1)) {
117 tmp = *(p++);
118 if (tmp)
119 goto found_middle;
120 result += BITS_PER_LONG;
121 size -= BITS_PER_LONG;
122 }
123 if (!size)
124 return result;
125 tmp = *p;
126found_first:
127 tmp &= (~0UL >> (BITS_PER_LONG - size));
128 if (tmp == 0UL) /* Are any bits set? */
129 return result + size; /* Nope. */
130found_middle:
131 return result + __reverse_ffs(tmp);
Jaegeuk Kime19ef5272015-05-18 11:45:15 -0700132#endif
Changman Lee9a7f1432013-11-15 10:42:51 +0900133}
134
135static unsigned long __find_rev_next_zero_bit(const unsigned long *addr,
136 unsigned long size, unsigned long offset)
137{
Jaegeuk Kime19ef5272015-05-18 11:45:15 -0700138 while (f2fs_test_bit(offset, (unsigned char *)addr))
139 offset++;
140
141 if (offset > size)
142 offset = size;
143
144 return offset;
145#if 0
Changman Lee9a7f1432013-11-15 10:42:51 +0900146 const unsigned long *p = addr + BIT_WORD(offset);
147 unsigned long result = offset & ~(BITS_PER_LONG - 1);
148 unsigned long tmp;
149 unsigned long mask, submask;
150 unsigned long quot, rest;
151
152 if (offset >= size)
153 return size;
154
155 size -= result;
156 offset %= BITS_PER_LONG;
157 if (!offset)
158 goto aligned;
159
160 tmp = *(p++);
161 quot = (offset >> 3) << 3;
162 rest = offset & 0x7;
163 mask = ~(~0UL << quot);
164 submask = (unsigned char)~((unsigned char)(0xff << rest) >> rest);
165 submask <<= quot;
166 mask += submask;
167 tmp |= mask;
168 if (size < BITS_PER_LONG)
169 goto found_first;
170 if (~tmp)
171 goto found_middle;
172
173 size -= BITS_PER_LONG;
174 result += BITS_PER_LONG;
175aligned:
176 while (size & ~(BITS_PER_LONG - 1)) {
177 tmp = *(p++);
178 if (~tmp)
179 goto found_middle;
180 result += BITS_PER_LONG;
181 size -= BITS_PER_LONG;
182 }
183 if (!size)
184 return result;
185 tmp = *p;
186
187found_first:
188 tmp |= ~0UL << size;
189 if (tmp == ~0UL) /* Are any bits zero? */
190 return result + size; /* Nope. */
191found_middle:
192 return result + __reverse_ffz(tmp);
Jaegeuk Kime19ef5272015-05-18 11:45:15 -0700193#endif
Changman Lee9a7f1432013-11-15 10:42:51 +0900194}
195
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700196void register_inmem_page(struct inode *inode, struct page *page)
197{
198 struct f2fs_inode_info *fi = F2FS_I(inode);
199 struct inmem_pages *new;
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -0700200 int err;
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800201
Jaegeuk Kim0722b102014-12-05 11:58:02 -0800202 SetPagePrivate(page);
Jaegeuk Kim9e4ded32014-12-17 19:58:58 -0800203 f2fs_trace_pid(page);
Jaegeuk Kim0722b102014-12-05 11:58:02 -0800204
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700205 new = f2fs_kmem_cache_alloc(inmem_entry_slab, GFP_NOFS);
206
207 /* add atomic page indices to the list */
208 new->page = page;
209 INIT_LIST_HEAD(&new->list);
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800210retry:
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700211 /* increase reference count with clean state */
212 mutex_lock(&fi->inmem_lock);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -0700213 err = radix_tree_insert(&fi->inmem_root, page->index, new);
214 if (err == -EEXIST) {
215 mutex_unlock(&fi->inmem_lock);
216 kmem_cache_free(inmem_entry_slab, new);
217 return;
218 } else if (err) {
219 mutex_unlock(&fi->inmem_lock);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -0700220 goto retry;
221 }
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700222 get_page(page);
223 list_add_tail(&new->list, &fi->inmem_pages);
Jaegeuk Kim8dcf2ff72014-12-05 17:18:15 -0800224 inc_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700225 mutex_unlock(&fi->inmem_lock);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -0700226
227 trace_f2fs_register_inmem_page(page, INMEM);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700228}
229
230void commit_inmem_pages(struct inode *inode, bool abort)
231{
232 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
233 struct f2fs_inode_info *fi = F2FS_I(inode);
234 struct inmem_pages *cur, *tmp;
235 bool submit_bio = false;
236 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700237 .sbi = sbi,
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700238 .type = DATA,
Jaegeuk Kim1e843712014-12-09 06:08:59 -0800239 .rw = WRITE_SYNC | REQ_PRIO,
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700240 .encrypted_page = NULL,
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700241 };
242
Jaegeuk Kim03418452014-11-21 16:37:40 -0800243 /*
244 * The abort is true only when f2fs_evict_inode is called.
245 * Basically, the f2fs_evict_inode doesn't produce any data writes, so
246 * that we don't need to call f2fs_balance_fs.
247 * Otherwise, f2fs_gc in f2fs_balance_fs can wait forever until this
248 * inode becomes free by iget_locked in f2fs_iget.
249 */
Jaegeuk Kim70c640b2014-12-10 13:59:33 -0800250 if (!abort) {
Jaegeuk Kim03418452014-11-21 16:37:40 -0800251 f2fs_balance_fs(sbi);
Jaegeuk Kim70c640b2014-12-10 13:59:33 -0800252 f2fs_lock_op(sbi);
253 }
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700254
255 mutex_lock(&fi->inmem_lock);
256 list_for_each_entry_safe(cur, tmp, &fi->inmem_pages, list) {
Jaegeuk Kim70c640b2014-12-10 13:59:33 -0800257 if (!abort) {
258 lock_page(cur->page);
259 if (cur->page->mapping == inode->i_mapping) {
260 f2fs_wait_on_page_writeback(cur->page, DATA);
261 if (clear_page_dirty_for_io(cur->page))
262 inode_dec_dirty_pages(inode);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -0700263 trace_f2fs_commit_inmem_page(cur->page, INMEM);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700264 fio.page = cur->page;
265 do_write_data_page(&fio);
Jaegeuk Kim70c640b2014-12-10 13:59:33 -0800266 submit_bio = true;
267 }
268 f2fs_put_page(cur->page, 1);
269 } else {
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -0700270 trace_f2fs_commit_inmem_page(cur->page, INMEM_DROP);
Jaegeuk Kim70c640b2014-12-10 13:59:33 -0800271 put_page(cur->page);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700272 }
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -0700273 radix_tree_delete(&fi->inmem_root, cur->page->index);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700274 list_del(&cur->list);
275 kmem_cache_free(inmem_entry_slab, cur);
Jaegeuk Kim8dcf2ff72014-12-05 17:18:15 -0800276 dec_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700277 }
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700278 mutex_unlock(&fi->inmem_lock);
279
Jaegeuk Kim70c640b2014-12-10 13:59:33 -0800280 if (!abort) {
281 f2fs_unlock_op(sbi);
282 if (submit_bio)
283 f2fs_submit_merged_bio(sbi, DATA, WRITE);
284 }
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700285}
286
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900287/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900288 * This function balances dirty node and dentry pages.
289 * In addition, it controls garbage collection.
290 */
291void f2fs_balance_fs(struct f2fs_sb_info *sbi)
292{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900293 /*
Jaegeuk Kim029cd282012-12-21 17:20:21 +0900294 * We should do GC or end up with checkpoint, if there are so many dirty
295 * dir/node pages without enough free segments.
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900296 */
Jaegeuk Kim43727522013-02-04 15:11:17 +0900297 if (has_not_enough_free_secs(sbi, 0)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900298 mutex_lock(&sbi->gc_mutex);
Jaegeuk Kim408e9372013-01-03 17:55:52 +0900299 f2fs_gc(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900300 }
301}
302
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900303void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi)
304{
Chao Yu1dcc3362015-02-05 17:57:31 +0800305 /* try to shrink extent cache when there is no enough memory */
306 f2fs_shrink_extent_tree(sbi, EXTENT_CACHE_SHRINK_NUMBER);
307
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900308 /* check the # of cached NAT entries and prefree segments */
309 if (try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK) ||
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -0800310 excess_prefree_segs(sbi) ||
Jaegeuk Kim88a70a62014-12-10 15:20:48 -0800311 !available_free_memory(sbi, INO_ENTRIES))
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900312 f2fs_sync_fs(sbi->sb, true);
313}
314
Gu Zheng2163d192014-04-27 14:21:33 +0800315static int issue_flush_thread(void *data)
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900316{
317 struct f2fs_sb_info *sbi = data;
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800318 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
319 wait_queue_head_t *q = &fcc->flush_wait_queue;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900320repeat:
321 if (kthread_should_stop())
322 return 0;
323
Gu Zheng721bd4d2014-09-05 18:31:00 +0800324 if (!llist_empty(&fcc->issue_list)) {
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900325 struct bio *bio = bio_alloc(GFP_NOIO, 0);
326 struct flush_cmd *cmd, *next;
327 int ret;
328
Gu Zheng721bd4d2014-09-05 18:31:00 +0800329 fcc->dispatch_list = llist_del_all(&fcc->issue_list);
330 fcc->dispatch_list = llist_reverse_order(fcc->dispatch_list);
331
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900332 bio->bi_bdev = sbi->sb->s_bdev;
333 ret = submit_bio_wait(WRITE_FLUSH, bio);
334
Gu Zheng721bd4d2014-09-05 18:31:00 +0800335 llist_for_each_entry_safe(cmd, next,
336 fcc->dispatch_list, llnode) {
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900337 cmd->ret = ret;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900338 complete(&cmd->wait);
339 }
Gu Zhenga4ed23f2014-04-11 17:49:35 +0800340 bio_put(bio);
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800341 fcc->dispatch_list = NULL;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900342 }
343
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800344 wait_event_interruptible(*q,
Gu Zheng721bd4d2014-09-05 18:31:00 +0800345 kthread_should_stop() || !llist_empty(&fcc->issue_list));
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900346 goto repeat;
347}
348
349int f2fs_issue_flush(struct f2fs_sb_info *sbi)
350{
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800351 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
Chao Yuadf8d902014-05-08 17:00:35 +0800352 struct flush_cmd cmd;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900353
Jaegeuk Kim24a9ee02014-07-25 17:46:10 -0700354 trace_f2fs_issue_flush(sbi->sb, test_opt(sbi, NOBARRIER),
355 test_opt(sbi, FLUSH_MERGE));
356
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700357 if (test_opt(sbi, NOBARRIER))
358 return 0;
359
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900360 if (!test_opt(sbi, FLUSH_MERGE))
361 return blkdev_issue_flush(sbi->sb->s_bdev, GFP_KERNEL, NULL);
362
Chao Yuadf8d902014-05-08 17:00:35 +0800363 init_completion(&cmd.wait);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900364
Gu Zheng721bd4d2014-09-05 18:31:00 +0800365 llist_add(&cmd.llnode, &fcc->issue_list);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900366
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800367 if (!fcc->dispatch_list)
368 wake_up(&fcc->flush_wait_queue);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900369
Chao Yuadf8d902014-05-08 17:00:35 +0800370 wait_for_completion(&cmd.wait);
371
372 return cmd.ret;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900373}
374
Gu Zheng2163d192014-04-27 14:21:33 +0800375int create_flush_cmd_control(struct f2fs_sb_info *sbi)
376{
377 dev_t dev = sbi->sb->s_bdev->bd_dev;
378 struct flush_cmd_control *fcc;
379 int err = 0;
380
381 fcc = kzalloc(sizeof(struct flush_cmd_control), GFP_KERNEL);
382 if (!fcc)
383 return -ENOMEM;
Gu Zheng2163d192014-04-27 14:21:33 +0800384 init_waitqueue_head(&fcc->flush_wait_queue);
Gu Zheng721bd4d2014-09-05 18:31:00 +0800385 init_llist_head(&fcc->issue_list);
Chao Yu6b2920a2014-07-07 11:21:59 +0800386 SM_I(sbi)->cmd_control_info = fcc;
Gu Zheng2163d192014-04-27 14:21:33 +0800387 fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi,
388 "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev));
389 if (IS_ERR(fcc->f2fs_issue_flush)) {
390 err = PTR_ERR(fcc->f2fs_issue_flush);
391 kfree(fcc);
Chao Yu6b2920a2014-07-07 11:21:59 +0800392 SM_I(sbi)->cmd_control_info = NULL;
Gu Zheng2163d192014-04-27 14:21:33 +0800393 return err;
394 }
Gu Zheng2163d192014-04-27 14:21:33 +0800395
396 return err;
397}
398
399void destroy_flush_cmd_control(struct f2fs_sb_info *sbi)
400{
Chao Yu6b2920a2014-07-07 11:21:59 +0800401 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
Gu Zheng2163d192014-04-27 14:21:33 +0800402
403 if (fcc && fcc->f2fs_issue_flush)
404 kthread_stop(fcc->f2fs_issue_flush);
405 kfree(fcc);
Chao Yu6b2920a2014-07-07 11:21:59 +0800406 SM_I(sbi)->cmd_control_info = NULL;
Gu Zheng2163d192014-04-27 14:21:33 +0800407}
408
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900409static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
410 enum dirty_type dirty_type)
411{
412 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
413
414 /* need not be added */
415 if (IS_CURSEG(sbi, segno))
416 return;
417
418 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
419 dirty_i->nr_dirty[dirty_type]++;
420
421 if (dirty_type == DIRTY) {
422 struct seg_entry *sentry = get_seg_entry(sbi, segno);
Changman Lee4625d6a2013-10-25 17:31:57 +0900423 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900424
Jaegeuk Kimec325b52014-09-02 16:24:11 -0700425 if (unlikely(t >= DIRTY)) {
426 f2fs_bug_on(sbi, 1);
427 return;
428 }
Changman Lee4625d6a2013-10-25 17:31:57 +0900429 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
430 dirty_i->nr_dirty[t]++;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900431 }
432}
433
434static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
435 enum dirty_type dirty_type)
436{
437 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
438
439 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
440 dirty_i->nr_dirty[dirty_type]--;
441
442 if (dirty_type == DIRTY) {
Changman Lee4625d6a2013-10-25 17:31:57 +0900443 struct seg_entry *sentry = get_seg_entry(sbi, segno);
444 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900445
Changman Lee4625d6a2013-10-25 17:31:57 +0900446 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
447 dirty_i->nr_dirty[t]--;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900448
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +0900449 if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0)
450 clear_bit(GET_SECNO(sbi, segno),
451 dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900452 }
453}
454
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900455/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900456 * Should not occur error such as -ENOMEM.
457 * Adding dirty entry into seglist is not critical operation.
458 * If a given segment is one of current working segments, it won't be added.
459 */
Haicheng Li8d8451a2013-06-13 16:59:28 +0800460static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900461{
462 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
463 unsigned short valid_blocks;
464
465 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
466 return;
467
468 mutex_lock(&dirty_i->seglist_lock);
469
470 valid_blocks = get_valid_blocks(sbi, segno, 0);
471
472 if (valid_blocks == 0) {
473 __locate_dirty_segment(sbi, segno, PRE);
474 __remove_dirty_segment(sbi, segno, DIRTY);
475 } else if (valid_blocks < sbi->blocks_per_seg) {
476 __locate_dirty_segment(sbi, segno, DIRTY);
477 } else {
478 /* Recovery routine with SSR needs this */
479 __remove_dirty_segment(sbi, segno, DIRTY);
480 }
481
482 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900483}
484
Jaegeuk Kim1e87a782014-04-15 13:57:55 +0900485static int f2fs_issue_discard(struct f2fs_sb_info *sbi,
Jaegeuk Kim37208872013-11-12 16:55:17 +0900486 block_t blkstart, block_t blklen)
487{
Chao Yu55cf9cb2014-09-15 18:01:10 +0800488 sector_t start = SECTOR_FROM_BLOCK(blkstart);
489 sector_t len = SECTOR_FROM_BLOCK(blklen);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700490 struct seg_entry *se;
491 unsigned int offset;
492 block_t i;
493
494 for (i = blkstart; i < blkstart + blklen; i++) {
495 se = get_seg_entry(sbi, GET_SEGNO(sbi, i));
496 offset = GET_BLKOFF_FROM_SEG0(sbi, i);
497
498 if (!f2fs_test_and_set_bit(offset, se->discard_map))
499 sbi->discard_blks--;
500 }
Jaegeuk Kim1661d072013-11-12 17:01:00 +0900501 trace_f2fs_issue_discard(sbi->sb, blkstart, blklen);
Jaegeuk Kim1e87a782014-04-15 13:57:55 +0900502 return blkdev_issue_discard(sbi->sb->s_bdev, start, len, GFP_NOFS, 0);
503}
504
Jaegeuk Kimcf2271e2014-07-25 15:47:25 -0700505void discard_next_dnode(struct f2fs_sb_info *sbi, block_t blkaddr)
Jaegeuk Kim1e87a782014-04-15 13:57:55 +0900506{
Jaegeuk Kim40a02be2015-05-11 20:03:49 -0700507 int err = -ENOTSUPP;
508
509 if (test_opt(sbi, DISCARD)) {
510 struct seg_entry *se = get_seg_entry(sbi,
511 GET_SEGNO(sbi, blkaddr));
512 unsigned int offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
513
514 if (f2fs_test_bit(offset, se->discard_map))
515 return;
516
517 err = f2fs_issue_discard(sbi, blkaddr, 1);
518 }
519
520 if (err) {
Jaegeuk Kim1e87a782014-04-15 13:57:55 +0900521 struct page *page = grab_meta_page(sbi, blkaddr);
Chao Yucb5c94c2015-05-18 18:00:06 +0800522 memset(page_address(page), 0, F2FS_BLKSIZE);
Jaegeuk Kim1e87a782014-04-15 13:57:55 +0900523 set_page_dirty(page);
524 f2fs_put_page(page, 1);
525 }
Jaegeuk Kim37208872013-11-12 16:55:17 +0900526}
527
Jaegeuk Kimadf49832014-10-28 22:27:59 -0700528static void __add_discard_entry(struct f2fs_sb_info *sbi,
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700529 struct cp_control *cpc, struct seg_entry *se,
530 unsigned int start, unsigned int end)
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900531{
532 struct list_head *head = &SM_I(sbi)->discard_list;
Jaegeuk Kimadf49832014-10-28 22:27:59 -0700533 struct discard_entry *new, *last;
534
535 if (!list_empty(head)) {
536 last = list_last_entry(head, struct discard_entry, list);
537 if (START_BLOCK(sbi, cpc->trim_start) + start ==
538 last->blkaddr + last->len) {
539 last->len += end - start;
540 goto done;
541 }
542 }
543
544 new = f2fs_kmem_cache_alloc(discard_entry_slab, GFP_NOFS);
545 INIT_LIST_HEAD(&new->list);
546 new->blkaddr = START_BLOCK(sbi, cpc->trim_start) + start;
547 new->len = end - start;
548 list_add_tail(&new->list, head);
549done:
550 SM_I(sbi)->nr_discards += end - start;
551 cpc->trimmed += end - start;
552}
553
554static void add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc)
555{
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900556 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
557 int max_blocks = sbi->blocks_per_seg;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700558 struct seg_entry *se = get_seg_entry(sbi, cpc->trim_start);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900559 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
560 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700561 unsigned long *discard_map = (unsigned long *)se->discard_map;
Jaegeuk Kim60a3b782015-02-10 16:44:29 -0800562 unsigned long *dmap = SIT_I(sbi)->tmp_map;
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900563 unsigned int start = 0, end = -1;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700564 bool force = (cpc->reason == CP_DISCARD);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900565 int i;
566
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700567 if (se->valid_blocks == max_blocks)
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900568 return;
569
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700570 if (!force) {
571 if (!test_opt(sbi, DISCARD) || !se->valid_blocks ||
Dan Carpenter912a83b2015-05-14 11:52:28 +0300572 SM_I(sbi)->nr_discards >= SM_I(sbi)->max_discards)
573 return;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700574 }
575
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900576 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
577 for (i = 0; i < entries; i++)
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700578 dmap[i] = force ? ~ckpt_map[i] & ~discard_map[i] :
Jaegeuk Kimd7bc2482014-12-12 13:53:41 -0800579 (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900580
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700581 while (force || SM_I(sbi)->nr_discards <= SM_I(sbi)->max_discards) {
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900582 start = __find_rev_next_bit(dmap, max_blocks, end + 1);
583 if (start >= max_blocks)
584 break;
585
586 end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700587 __add_discard_entry(sbi, cpc, se, start, end);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900588 }
589}
590
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700591void release_discard_addrs(struct f2fs_sb_info *sbi)
592{
593 struct list_head *head = &(SM_I(sbi)->discard_list);
594 struct discard_entry *entry, *this;
595
596 /* drop caches */
597 list_for_each_entry_safe(entry, this, head, list) {
598 list_del(&entry->list);
599 kmem_cache_free(discard_entry_slab, entry);
600 }
601}
602
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900603/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900604 * Should call clear_prefree_segments after checkpoint is done.
605 */
606static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
607{
608 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Chao Yub65ee142014-08-04 10:10:07 +0800609 unsigned int segno;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900610
611 mutex_lock(&dirty_i->seglist_lock);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700612 for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900613 __set_test_and_free(sbi, segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900614 mutex_unlock(&dirty_i->seglist_lock);
615}
616
Jaegeuk Kim836b5a62015-04-30 22:50:06 -0700617void clear_prefree_segments(struct f2fs_sb_info *sbi, struct cp_control *cpc)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900618{
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900619 struct list_head *head = &(SM_I(sbi)->discard_list);
Chao Yu2d7b8222014-03-29 11:33:17 +0800620 struct discard_entry *entry, *this;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900621 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Changman Lee29e59c12013-11-11 09:24:37 +0900622 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
Changman Lee29e59c12013-11-11 09:24:37 +0900623 unsigned int start = 0, end = -1;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900624
625 mutex_lock(&dirty_i->seglist_lock);
Changman Lee29e59c12013-11-11 09:24:37 +0900626
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900627 while (1) {
Changman Lee29e59c12013-11-11 09:24:37 +0900628 int i;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700629 start = find_next_bit(prefree_map, MAIN_SEGS(sbi), end + 1);
630 if (start >= MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900631 break;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700632 end = find_next_zero_bit(prefree_map, MAIN_SEGS(sbi),
633 start + 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900634
Changman Lee29e59c12013-11-11 09:24:37 +0900635 for (i = start; i < end; i++)
636 clear_bit(i, prefree_map);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900637
Changman Lee29e59c12013-11-11 09:24:37 +0900638 dirty_i->nr_dirty[PRE] -= end - start;
639
640 if (!test_opt(sbi, DISCARD))
641 continue;
642
Jaegeuk Kim37208872013-11-12 16:55:17 +0900643 f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
644 (end - start) << sbi->log_blocks_per_seg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900645 }
646 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900647
648 /* send small discards */
Chao Yu2d7b8222014-03-29 11:33:17 +0800649 list_for_each_entry_safe(entry, this, head, list) {
Jaegeuk Kim836b5a62015-04-30 22:50:06 -0700650 if (cpc->reason == CP_DISCARD && entry->len < cpc->trim_minlen)
651 goto skip;
Jaegeuk Kim37208872013-11-12 16:55:17 +0900652 f2fs_issue_discard(sbi, entry->blkaddr, entry->len);
Jaegeuk Kim836b5a62015-04-30 22:50:06 -0700653skip:
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900654 list_del(&entry->list);
655 SM_I(sbi)->nr_discards -= entry->len;
656 kmem_cache_free(discard_entry_slab, entry);
657 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900658}
659
Chao Yu184a5cd2014-09-04 18:13:01 +0800660static bool __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900661{
662 struct sit_info *sit_i = SIT_I(sbi);
Chao Yu184a5cd2014-09-04 18:13:01 +0800663
664 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900665 sit_i->dirty_sentries++;
Chao Yu184a5cd2014-09-04 18:13:01 +0800666 return false;
667 }
668
669 return true;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900670}
671
672static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
673 unsigned int segno, int modified)
674{
675 struct seg_entry *se = get_seg_entry(sbi, segno);
676 se->type = type;
677 if (modified)
678 __mark_sit_entry_dirty(sbi, segno);
679}
680
681static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
682{
683 struct seg_entry *se;
684 unsigned int segno, offset;
685 long int new_vblocks;
686
687 segno = GET_SEGNO(sbi, blkaddr);
688
689 se = get_seg_entry(sbi, segno);
690 new_vblocks = se->valid_blocks + del;
Jaegeuk Kim491c0852014-02-04 13:01:10 +0900691 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900692
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700693 f2fs_bug_on(sbi, (new_vblocks >> (sizeof(unsigned short) << 3) ||
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900694 (new_vblocks > sbi->blocks_per_seg)));
695
696 se->valid_blocks = new_vblocks;
697 se->mtime = get_mtime(sbi);
698 SIT_I(sbi)->max_mtime = se->mtime;
699
700 /* Update valid block bitmap */
701 if (del > 0) {
Gu Zheng52aca072014-10-20 17:45:51 +0800702 if (f2fs_test_and_set_bit(offset, se->cur_valid_map))
Jaegeuk Kim05796762014-09-02 16:05:00 -0700703 f2fs_bug_on(sbi, 1);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700704 if (!f2fs_test_and_set_bit(offset, se->discard_map))
705 sbi->discard_blks--;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900706 } else {
Gu Zheng52aca072014-10-20 17:45:51 +0800707 if (!f2fs_test_and_clear_bit(offset, se->cur_valid_map))
Jaegeuk Kim05796762014-09-02 16:05:00 -0700708 f2fs_bug_on(sbi, 1);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700709 if (f2fs_test_and_clear_bit(offset, se->discard_map))
710 sbi->discard_blks++;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900711 }
712 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
713 se->ckpt_valid_blocks += del;
714
715 __mark_sit_entry_dirty(sbi, segno);
716
717 /* update total number of valid blocks to be written in ckpt area */
718 SIT_I(sbi)->written_valid_blocks += del;
719
720 if (sbi->segs_per_sec > 1)
721 get_sec_entry(sbi, segno)->valid_blocks += del;
722}
723
Jaegeuk Kim5e443812014-01-28 12:22:14 +0900724void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900725{
Jaegeuk Kim5e443812014-01-28 12:22:14 +0900726 update_sit_entry(sbi, new, 1);
727 if (GET_SEGNO(sbi, old) != NULL_SEGNO)
728 update_sit_entry(sbi, old, -1);
729
730 locate_dirty_segment(sbi, GET_SEGNO(sbi, old));
731 locate_dirty_segment(sbi, GET_SEGNO(sbi, new));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900732}
733
734void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
735{
736 unsigned int segno = GET_SEGNO(sbi, addr);
737 struct sit_info *sit_i = SIT_I(sbi);
738
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700739 f2fs_bug_on(sbi, addr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900740 if (addr == NEW_ADDR)
741 return;
742
743 /* add it into sit main buffer */
744 mutex_lock(&sit_i->sentry_lock);
745
746 update_sit_entry(sbi, addr, -1);
747
748 /* add it into dirty seglist */
749 locate_dirty_segment(sbi, segno);
750
751 mutex_unlock(&sit_i->sentry_lock);
752}
753
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900754/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900755 * This function should be resided under the curseg_mutex lock
756 */
757static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
Haicheng Lie79efe32013-06-13 16:59:27 +0800758 struct f2fs_summary *sum)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900759{
760 struct curseg_info *curseg = CURSEG_I(sbi, type);
761 void *addr = curseg->sum_blk;
Haicheng Lie79efe32013-06-13 16:59:27 +0800762 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900763 memcpy(addr, sum, sizeof(struct f2fs_summary));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900764}
765
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900766/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900767 * Calculate the number of current summary pages for writing
768 */
Chao Yu3fa06d72014-12-09 14:21:46 +0800769int npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900770{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900771 int valid_sum_count = 0;
Fan Li9a479382013-10-29 16:21:47 +0800772 int i, sum_in_page;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900773
774 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
775 if (sbi->ckpt->alloc_type[i] == SSR)
776 valid_sum_count += sbi->blocks_per_seg;
Chao Yu3fa06d72014-12-09 14:21:46 +0800777 else {
778 if (for_ra)
779 valid_sum_count += le16_to_cpu(
780 F2FS_CKPT(sbi)->cur_data_blkoff[i]);
781 else
782 valid_sum_count += curseg_blkoff(sbi, i);
783 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900784 }
785
Fan Li9a479382013-10-29 16:21:47 +0800786 sum_in_page = (PAGE_CACHE_SIZE - 2 * SUM_JOURNAL_SIZE -
787 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
788 if (valid_sum_count <= sum_in_page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900789 return 1;
Fan Li9a479382013-10-29 16:21:47 +0800790 else if ((valid_sum_count - sum_in_page) <=
791 (PAGE_CACHE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900792 return 2;
793 return 3;
794}
795
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900796/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900797 * Caller should put this summary page
798 */
799struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
800{
801 return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
802}
803
804static void write_sum_page(struct f2fs_sb_info *sbi,
805 struct f2fs_summary_block *sum_blk, block_t blk_addr)
806{
807 struct page *page = grab_meta_page(sbi, blk_addr);
808 void *kaddr = page_address(page);
809 memcpy(kaddr, sum_blk, PAGE_CACHE_SIZE);
810 set_page_dirty(page);
811 f2fs_put_page(page, 1);
812}
813
Jaegeuk Kim60374682013-03-31 13:58:51 +0900814static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
815{
816 struct curseg_info *curseg = CURSEG_I(sbi, type);
Haicheng Li81fb5e82013-05-14 18:20:28 +0800817 unsigned int segno = curseg->segno + 1;
Jaegeuk Kim60374682013-03-31 13:58:51 +0900818 struct free_segmap_info *free_i = FREE_I(sbi);
819
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700820 if (segno < MAIN_SEGS(sbi) && segno % sbi->segs_per_sec)
Haicheng Li81fb5e82013-05-14 18:20:28 +0800821 return !test_bit(segno, free_i->free_segmap);
Jaegeuk Kim60374682013-03-31 13:58:51 +0900822 return 0;
823}
824
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900825/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900826 * Find a new segment from the free segments bitmap to right order
827 * This function should be returned with success, otherwise BUG
828 */
829static void get_new_segment(struct f2fs_sb_info *sbi,
830 unsigned int *newseg, bool new_sec, int dir)
831{
832 struct free_segmap_info *free_i = FREE_I(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900833 unsigned int segno, secno, zoneno;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700834 unsigned int total_zones = MAIN_SECS(sbi) / sbi->secs_per_zone;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900835 unsigned int hint = *newseg / sbi->segs_per_sec;
836 unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
837 unsigned int left_start = hint;
838 bool init = true;
839 int go_left = 0;
840 int i;
841
Chao Yu1a118cc2015-02-11 18:20:38 +0800842 spin_lock(&free_i->segmap_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900843
844 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
845 segno = find_next_zero_bit(free_i->free_segmap,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700846 MAIN_SEGS(sbi), *newseg + 1);
Jaegeuk Kim33afa7f2013-03-31 12:59:53 +0900847 if (segno - *newseg < sbi->segs_per_sec -
848 (*newseg % sbi->segs_per_sec))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900849 goto got_it;
850 }
851find_other_zone:
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700852 secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint);
853 if (secno >= MAIN_SECS(sbi)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900854 if (dir == ALLOC_RIGHT) {
855 secno = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700856 MAIN_SECS(sbi), 0);
857 f2fs_bug_on(sbi, secno >= MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900858 } else {
859 go_left = 1;
860 left_start = hint - 1;
861 }
862 }
863 if (go_left == 0)
864 goto skip_left;
865
866 while (test_bit(left_start, free_i->free_secmap)) {
867 if (left_start > 0) {
868 left_start--;
869 continue;
870 }
871 left_start = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700872 MAIN_SECS(sbi), 0);
873 f2fs_bug_on(sbi, left_start >= MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900874 break;
875 }
876 secno = left_start;
877skip_left:
878 hint = secno;
879 segno = secno * sbi->segs_per_sec;
880 zoneno = secno / sbi->secs_per_zone;
881
882 /* give up on finding another zone */
883 if (!init)
884 goto got_it;
885 if (sbi->secs_per_zone == 1)
886 goto got_it;
887 if (zoneno == old_zoneno)
888 goto got_it;
889 if (dir == ALLOC_LEFT) {
890 if (!go_left && zoneno + 1 >= total_zones)
891 goto got_it;
892 if (go_left && zoneno == 0)
893 goto got_it;
894 }
895 for (i = 0; i < NR_CURSEG_TYPE; i++)
896 if (CURSEG_I(sbi, i)->zone == zoneno)
897 break;
898
899 if (i < NR_CURSEG_TYPE) {
900 /* zone is in user, try another */
901 if (go_left)
902 hint = zoneno * sbi->secs_per_zone - 1;
903 else if (zoneno + 1 >= total_zones)
904 hint = 0;
905 else
906 hint = (zoneno + 1) * sbi->secs_per_zone;
907 init = false;
908 goto find_other_zone;
909 }
910got_it:
911 /* set it as dirty segment in free segmap */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700912 f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900913 __set_inuse(sbi, segno);
914 *newseg = segno;
Chao Yu1a118cc2015-02-11 18:20:38 +0800915 spin_unlock(&free_i->segmap_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900916}
917
918static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
919{
920 struct curseg_info *curseg = CURSEG_I(sbi, type);
921 struct summary_footer *sum_footer;
922
923 curseg->segno = curseg->next_segno;
924 curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
925 curseg->next_blkoff = 0;
926 curseg->next_segno = NULL_SEGNO;
927
928 sum_footer = &(curseg->sum_blk->footer);
929 memset(sum_footer, 0, sizeof(struct summary_footer));
930 if (IS_DATASEG(type))
931 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
932 if (IS_NODESEG(type))
933 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
934 __set_sit_entry_type(sbi, type, curseg->segno, modified);
935}
936
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900937/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900938 * Allocate a current working segment.
939 * This function always allocates a free segment in LFS manner.
940 */
941static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
942{
943 struct curseg_info *curseg = CURSEG_I(sbi, type);
944 unsigned int segno = curseg->segno;
945 int dir = ALLOC_LEFT;
946
947 write_sum_page(sbi, curseg->sum_blk,
Haicheng Li81fb5e82013-05-14 18:20:28 +0800948 GET_SUM_BLOCK(sbi, segno));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900949 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
950 dir = ALLOC_RIGHT;
951
952 if (test_opt(sbi, NOHEAP))
953 dir = ALLOC_RIGHT;
954
955 get_new_segment(sbi, &segno, new_sec, dir);
956 curseg->next_segno = segno;
957 reset_curseg(sbi, type, 1);
958 curseg->alloc_type = LFS;
959}
960
961static void __next_free_blkoff(struct f2fs_sb_info *sbi,
962 struct curseg_info *seg, block_t start)
963{
964 struct seg_entry *se = get_seg_entry(sbi, seg->segno);
Changman Leee81c93c2013-11-15 13:21:16 +0900965 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
Jaegeuk Kim60a3b782015-02-10 16:44:29 -0800966 unsigned long *target_map = SIT_I(sbi)->tmp_map;
Changman Leee81c93c2013-11-15 13:21:16 +0900967 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
968 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
969 int i, pos;
970
971 for (i = 0; i < entries; i++)
972 target_map[i] = ckpt_map[i] | cur_map[i];
973
974 pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
975
976 seg->next_blkoff = pos;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900977}
978
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900979/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900980 * If a segment is written by LFS manner, next block offset is just obtained
981 * by increasing the current block offset. However, if a segment is written by
982 * SSR manner, next block offset obtained by calling __next_free_blkoff
983 */
984static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
985 struct curseg_info *seg)
986{
987 if (seg->alloc_type == SSR)
988 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
989 else
990 seg->next_blkoff++;
991}
992
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900993/*
arter97e1c42042014-08-06 23:22:50 +0900994 * This function always allocates a used segment(from dirty seglist) by SSR
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900995 * manner, so it should recover the existing segment information of valid blocks
996 */
997static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
998{
999 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1000 struct curseg_info *curseg = CURSEG_I(sbi, type);
1001 unsigned int new_segno = curseg->next_segno;
1002 struct f2fs_summary_block *sum_node;
1003 struct page *sum_page;
1004
1005 write_sum_page(sbi, curseg->sum_blk,
1006 GET_SUM_BLOCK(sbi, curseg->segno));
1007 __set_test_and_inuse(sbi, new_segno);
1008
1009 mutex_lock(&dirty_i->seglist_lock);
1010 __remove_dirty_segment(sbi, new_segno, PRE);
1011 __remove_dirty_segment(sbi, new_segno, DIRTY);
1012 mutex_unlock(&dirty_i->seglist_lock);
1013
1014 reset_curseg(sbi, type, 1);
1015 curseg->alloc_type = SSR;
1016 __next_free_blkoff(sbi, curseg, 0);
1017
1018 if (reuse) {
1019 sum_page = get_sum_page(sbi, new_segno);
1020 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
1021 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
1022 f2fs_put_page(sum_page, 1);
1023 }
1024}
1025
Jaegeuk Kim43727522013-02-04 15:11:17 +09001026static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
1027{
1028 struct curseg_info *curseg = CURSEG_I(sbi, type);
1029 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
1030
1031 if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0))
1032 return v_ops->get_victim(sbi,
1033 &(curseg)->next_segno, BG_GC, type, SSR);
1034
1035 /* For data segments, let's do SSR more intensively */
1036 for (; type >= CURSEG_HOT_DATA; type--)
1037 if (v_ops->get_victim(sbi, &(curseg)->next_segno,
1038 BG_GC, type, SSR))
1039 return 1;
1040 return 0;
1041}
1042
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001043/*
1044 * flush out current segment and replace it with new segment
1045 * This function should be returned with success, otherwise BUG
1046 */
1047static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
1048 int type, bool force)
1049{
1050 struct curseg_info *curseg = CURSEG_I(sbi, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001051
Gu Zheng7b405272013-08-19 09:41:15 +08001052 if (force)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001053 new_curseg(sbi, type, true);
Gu Zheng7b405272013-08-19 09:41:15 +08001054 else if (type == CURSEG_WARM_NODE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001055 new_curseg(sbi, type, false);
Jaegeuk Kim60374682013-03-31 13:58:51 +09001056 else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
1057 new_curseg(sbi, type, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001058 else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
1059 change_curseg(sbi, type, true);
1060 else
1061 new_curseg(sbi, type, false);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +09001062
1063 stat_inc_seg_type(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001064}
1065
Jaegeuk Kim38aa0882015-01-05 16:02:20 -08001066static void __allocate_new_segments(struct f2fs_sb_info *sbi, int type)
1067{
1068 struct curseg_info *curseg = CURSEG_I(sbi, type);
1069 unsigned int old_segno;
1070
1071 old_segno = curseg->segno;
1072 SIT_I(sbi)->s_ops->allocate_segment(sbi, type, true);
1073 locate_dirty_segment(sbi, old_segno);
1074}
1075
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001076void allocate_new_segments(struct f2fs_sb_info *sbi)
1077{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001078 int i;
1079
Jaegeuk Kim38aa0882015-01-05 16:02:20 -08001080 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++)
1081 __allocate_new_segments(sbi, i);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001082}
1083
1084static const struct segment_allocation default_salloc_ops = {
1085 .allocate_segment = allocate_segment_by_default,
1086};
1087
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001088int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range)
1089{
Jaegeuk Kimf7ef9b82015-02-09 12:02:44 -08001090 __u64 start = F2FS_BYTES_TO_BLK(range->start);
1091 __u64 end = start + F2FS_BYTES_TO_BLK(range->len) - 1;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001092 unsigned int start_segno, end_segno;
1093 struct cp_control cpc;
1094
Jaegeuk Kim836b5a62015-04-30 22:50:06 -07001095 if (start >= MAX_BLKADDR(sbi) || range->len < sbi->blocksize)
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001096 return -EINVAL;
1097
Jan Kara9bd27ae2014-10-21 14:07:33 +02001098 cpc.trimmed = 0;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001099 if (end <= MAIN_BLKADDR(sbi))
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001100 goto out;
1101
1102 /* start/end segment number in main_area */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001103 start_segno = (start <= MAIN_BLKADDR(sbi)) ? 0 : GET_SEGNO(sbi, start);
1104 end_segno = (end >= MAX_BLKADDR(sbi)) ? MAIN_SEGS(sbi) - 1 :
1105 GET_SEGNO(sbi, end);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001106 cpc.reason = CP_DISCARD;
Jaegeuk Kim836b5a62015-04-30 22:50:06 -07001107 cpc.trim_minlen = max_t(__u64, 1, F2FS_BYTES_TO_BLK(range->minlen));
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001108
1109 /* do checkpoint to issue discard commands safely */
Jaegeuk Kimbba681c2015-01-26 17:41:23 -08001110 for (; start_segno <= end_segno; start_segno = cpc.trim_end + 1) {
1111 cpc.trim_start = start_segno;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001112
1113 if (sbi->discard_blks == 0)
1114 break;
1115 else if (sbi->discard_blks < BATCHED_TRIM_BLOCKS(sbi))
1116 cpc.trim_end = end_segno;
1117 else
1118 cpc.trim_end = min_t(unsigned int,
1119 rounddown(start_segno +
Jaegeuk Kimbba681c2015-01-26 17:41:23 -08001120 BATCHED_TRIM_SEGMENTS(sbi),
1121 sbi->segs_per_sec) - 1, end_segno);
1122
1123 mutex_lock(&sbi->gc_mutex);
1124 write_checkpoint(sbi, &cpc);
1125 mutex_unlock(&sbi->gc_mutex);
1126 }
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001127out:
Jaegeuk Kimf7ef9b82015-02-09 12:02:44 -08001128 range->len = F2FS_BLK_TO_BYTES(cpc.trimmed);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001129 return 0;
1130}
1131
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001132static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
1133{
1134 struct curseg_info *curseg = CURSEG_I(sbi, type);
1135 if (curseg->next_blkoff < sbi->blocks_per_seg)
1136 return true;
1137 return false;
1138}
1139
1140static int __get_segment_type_2(struct page *page, enum page_type p_type)
1141{
1142 if (p_type == DATA)
1143 return CURSEG_HOT_DATA;
1144 else
1145 return CURSEG_HOT_NODE;
1146}
1147
1148static int __get_segment_type_4(struct page *page, enum page_type p_type)
1149{
1150 if (p_type == DATA) {
1151 struct inode *inode = page->mapping->host;
1152
1153 if (S_ISDIR(inode->i_mode))
1154 return CURSEG_HOT_DATA;
1155 else
1156 return CURSEG_COLD_DATA;
1157 } else {
Jaegeuk Kima344b9f2014-11-05 20:05:53 -08001158 if (IS_DNODE(page) && is_cold_node(page))
1159 return CURSEG_WARM_NODE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001160 else
1161 return CURSEG_COLD_NODE;
1162 }
1163}
1164
1165static int __get_segment_type_6(struct page *page, enum page_type p_type)
1166{
1167 if (p_type == DATA) {
1168 struct inode *inode = page->mapping->host;
1169
1170 if (S_ISDIR(inode->i_mode))
1171 return CURSEG_HOT_DATA;
Jaegeuk Kim354a3392013-06-14 08:52:35 +09001172 else if (is_cold_data(page) || file_is_cold(inode))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001173 return CURSEG_COLD_DATA;
1174 else
1175 return CURSEG_WARM_DATA;
1176 } else {
1177 if (IS_DNODE(page))
1178 return is_cold_node(page) ? CURSEG_WARM_NODE :
1179 CURSEG_HOT_NODE;
1180 else
1181 return CURSEG_COLD_NODE;
1182 }
1183}
1184
1185static int __get_segment_type(struct page *page, enum page_type p_type)
1186{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001187 switch (F2FS_P_SB(page)->active_logs) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001188 case 2:
1189 return __get_segment_type_2(page, p_type);
1190 case 4:
1191 return __get_segment_type_4(page, p_type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001192 }
Jaegeuk Kim12a67142012-12-21 11:47:05 +09001193 /* NR_CURSEG_TYPE(6) logs by default */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001194 f2fs_bug_on(F2FS_P_SB(page),
1195 F2FS_P_SB(page)->active_logs != NR_CURSEG_TYPE);
Jaegeuk Kim12a67142012-12-21 11:47:05 +09001196 return __get_segment_type_6(page, p_type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001197}
1198
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001199void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
1200 block_t old_blkaddr, block_t *new_blkaddr,
1201 struct f2fs_summary *sum, int type)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001202{
1203 struct sit_info *sit_i = SIT_I(sbi);
1204 struct curseg_info *curseg;
Jaegeuk Kim38aa0882015-01-05 16:02:20 -08001205 bool direct_io = (type == CURSEG_DIRECT_IO);
1206
1207 type = direct_io ? CURSEG_WARM_DATA : type;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001208
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001209 curseg = CURSEG_I(sbi, type);
1210
1211 mutex_lock(&curseg->curseg_mutex);
Jaegeuk Kim21cb1d92015-03-11 13:42:48 -04001212 mutex_lock(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001213
Jaegeuk Kim38aa0882015-01-05 16:02:20 -08001214 /* direct_io'ed data is aligned to the segment for better performance */
1215 if (direct_io && curseg->next_blkoff)
1216 __allocate_new_segments(sbi, type);
1217
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001218 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001219
1220 /*
1221 * __add_sum_entry should be resided under the curseg_mutex
1222 * because, this function updates a summary entry in the
1223 * current summary block.
1224 */
Haicheng Lie79efe32013-06-13 16:59:27 +08001225 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001226
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001227 __refresh_next_blkoff(sbi, curseg);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +09001228
1229 stat_inc_block_count(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001230
Jaegeuk Kim5e443812014-01-28 12:22:14 +09001231 if (!__has_curseg_space(sbi, type))
1232 sit_i->s_ops->allocate_segment(sbi, type, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001233 /*
1234 * SIT information should be updated before segment allocation,
1235 * since SSR needs latest valid block information.
1236 */
1237 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
Jaegeuk Kim5e443812014-01-28 12:22:14 +09001238
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001239 mutex_unlock(&sit_i->sentry_lock);
1240
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001241 if (page && IS_NODESEG(type))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001242 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
1243
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001244 mutex_unlock(&curseg->curseg_mutex);
1245}
1246
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001247static void do_write_page(struct f2fs_summary *sum, struct f2fs_io_info *fio)
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001248{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001249 int type = __get_segment_type(fio->page, fio->type);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001250
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001251 allocate_data_block(fio->sbi, fio->page, fio->blk_addr,
1252 &fio->blk_addr, sum, type);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001253
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001254 /* writeout dirty page into bdev */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001255 f2fs_submit_page_mbio(fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001256}
1257
Jaegeuk Kim577e3492013-01-24 19:56:11 +09001258void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001259{
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001260 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001261 .sbi = sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001262 .type = META,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001263 .rw = WRITE_SYNC | REQ_META | REQ_PRIO,
1264 .blk_addr = page->index,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001265 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001266 .encrypted_page = NULL,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001267 };
1268
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001269 set_page_writeback(page);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001270 f2fs_submit_page_mbio(&fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001271}
1272
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001273void write_node_page(unsigned int nid, struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001274{
1275 struct f2fs_summary sum;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001276
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001277 set_summary(&sum, nid, 0, 0);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001278 do_write_page(&sum, fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001279}
1280
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001281void write_data_page(struct dnode_of_data *dn, struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001282{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001283 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001284 struct f2fs_summary sum;
1285 struct node_info ni;
1286
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001287 f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001288 get_node_info(sbi, dn->nid, &ni);
1289 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001290 do_write_page(&sum, fio);
Jaegeuk Kime1509cf2014-12-30 22:57:55 -08001291 dn->data_blkaddr = fio->blk_addr;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001292}
1293
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001294void rewrite_data_page(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001295{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001296 stat_inc_inplace_blocks(fio->sbi);
1297 f2fs_submit_page_mbio(fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001298}
1299
Chao Yu19f106b2015-05-06 13:08:06 +08001300void f2fs_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
1301 block_t old_blkaddr, block_t new_blkaddr,
1302 bool recover_curseg)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001303{
1304 struct sit_info *sit_i = SIT_I(sbi);
1305 struct curseg_info *curseg;
1306 unsigned int segno, old_cursegno;
1307 struct seg_entry *se;
1308 int type;
Chao Yu19f106b2015-05-06 13:08:06 +08001309 unsigned short old_blkoff;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001310
1311 segno = GET_SEGNO(sbi, new_blkaddr);
1312 se = get_seg_entry(sbi, segno);
1313 type = se->type;
1314
Chao Yu19f106b2015-05-06 13:08:06 +08001315 if (!recover_curseg) {
1316 /* for recovery flow */
1317 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
1318 if (old_blkaddr == NULL_ADDR)
1319 type = CURSEG_COLD_DATA;
1320 else
1321 type = CURSEG_WARM_DATA;
1322 }
1323 } else {
1324 if (!IS_CURSEG(sbi, segno))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001325 type = CURSEG_WARM_DATA;
1326 }
Chao Yu19f106b2015-05-06 13:08:06 +08001327
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001328 curseg = CURSEG_I(sbi, type);
1329
1330 mutex_lock(&curseg->curseg_mutex);
1331 mutex_lock(&sit_i->sentry_lock);
1332
1333 old_cursegno = curseg->segno;
Chao Yu19f106b2015-05-06 13:08:06 +08001334 old_blkoff = curseg->next_blkoff;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001335
1336 /* change the current segment */
1337 if (segno != curseg->segno) {
1338 curseg->next_segno = segno;
1339 change_curseg(sbi, type, true);
1340 }
1341
Jaegeuk Kim491c0852014-02-04 13:01:10 +09001342 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
Haicheng Lie79efe32013-06-13 16:59:27 +08001343 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001344
1345 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001346 locate_dirty_segment(sbi, old_cursegno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001347
Chao Yu19f106b2015-05-06 13:08:06 +08001348 if (recover_curseg) {
1349 if (old_cursegno != curseg->segno) {
1350 curseg->next_segno = old_cursegno;
1351 change_curseg(sbi, type, true);
1352 }
1353 curseg->next_blkoff = old_blkoff;
1354 }
1355
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001356 mutex_unlock(&sit_i->sentry_lock);
1357 mutex_unlock(&curseg->curseg_mutex);
1358}
1359
Chao Yudf0f8dc2014-03-22 14:57:23 +08001360static inline bool is_merged_page(struct f2fs_sb_info *sbi,
1361 struct page *page, enum page_type type)
1362{
1363 enum page_type btype = PAGE_TYPE_OF_BIO(type);
1364 struct f2fs_bio_info *io = &sbi->write_io[btype];
Chao Yudf0f8dc2014-03-22 14:57:23 +08001365 struct bio_vec *bvec;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001366 struct page *target;
Chao Yudf0f8dc2014-03-22 14:57:23 +08001367 int i;
1368
1369 down_read(&io->io_rwsem);
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001370 if (!io->bio) {
1371 up_read(&io->io_rwsem);
1372 return false;
1373 }
Chao Yudf0f8dc2014-03-22 14:57:23 +08001374
Jaegeuk Kimce234472014-04-02 09:04:42 +09001375 bio_for_each_segment_all(bvec, io->bio, i) {
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001376
1377 if (bvec->bv_page->mapping) {
1378 target = bvec->bv_page;
1379 } else {
1380 struct f2fs_crypto_ctx *ctx;
1381
1382 /* encrypted page */
1383 ctx = (struct f2fs_crypto_ctx *)page_private(
1384 bvec->bv_page);
Jaegeuk Kimca40b032015-05-12 13:40:20 -07001385 target = ctx->w.control_page;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001386 }
1387
1388 if (page == target) {
Chao Yudf0f8dc2014-03-22 14:57:23 +08001389 up_read(&io->io_rwsem);
1390 return true;
1391 }
1392 }
1393
Chao Yudf0f8dc2014-03-22 14:57:23 +08001394 up_read(&io->io_rwsem);
1395 return false;
1396}
1397
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001398void f2fs_wait_on_page_writeback(struct page *page,
Yuan Zhong5514f0a2014-01-10 07:26:14 +00001399 enum page_type type)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001400{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001401 if (PageWriteback(page)) {
Jaegeuk Kim40813632014-09-02 15:31:18 -07001402 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
1403
Chao Yudf0f8dc2014-03-22 14:57:23 +08001404 if (is_merged_page(sbi, page, type))
1405 f2fs_submit_merged_bio(sbi, type, WRITE);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001406 wait_on_page_writeback(page);
1407 }
1408}
1409
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001410static int read_compacted_summaries(struct f2fs_sb_info *sbi)
1411{
1412 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1413 struct curseg_info *seg_i;
1414 unsigned char *kaddr;
1415 struct page *page;
1416 block_t start;
1417 int i, j, offset;
1418
1419 start = start_sum_block(sbi);
1420
1421 page = get_meta_page(sbi, start++);
1422 kaddr = (unsigned char *)page_address(page);
1423
1424 /* Step 1: restore nat cache */
1425 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1426 memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
1427
1428 /* Step 2: restore sit cache */
1429 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1430 memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
1431 SUM_JOURNAL_SIZE);
1432 offset = 2 * SUM_JOURNAL_SIZE;
1433
1434 /* Step 3: restore summary entries */
1435 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1436 unsigned short blk_off;
1437 unsigned int segno;
1438
1439 seg_i = CURSEG_I(sbi, i);
1440 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
1441 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
1442 seg_i->next_segno = segno;
1443 reset_curseg(sbi, i, 0);
1444 seg_i->alloc_type = ckpt->alloc_type[i];
1445 seg_i->next_blkoff = blk_off;
1446
1447 if (seg_i->alloc_type == SSR)
1448 blk_off = sbi->blocks_per_seg;
1449
1450 for (j = 0; j < blk_off; j++) {
1451 struct f2fs_summary *s;
1452 s = (struct f2fs_summary *)(kaddr + offset);
1453 seg_i->sum_blk->entries[j] = *s;
1454 offset += SUMMARY_SIZE;
1455 if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1456 SUM_FOOTER_SIZE)
1457 continue;
1458
1459 f2fs_put_page(page, 1);
1460 page = NULL;
1461
1462 page = get_meta_page(sbi, start++);
1463 kaddr = (unsigned char *)page_address(page);
1464 offset = 0;
1465 }
1466 }
1467 f2fs_put_page(page, 1);
1468 return 0;
1469}
1470
1471static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
1472{
1473 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1474 struct f2fs_summary_block *sum;
1475 struct curseg_info *curseg;
1476 struct page *new;
1477 unsigned short blk_off;
1478 unsigned int segno = 0;
1479 block_t blk_addr = 0;
1480
1481 /* get segment number and block addr */
1482 if (IS_DATASEG(type)) {
1483 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
1484 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
1485 CURSEG_HOT_DATA]);
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001486 if (__exist_node_summaries(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001487 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
1488 else
1489 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
1490 } else {
1491 segno = le32_to_cpu(ckpt->cur_node_segno[type -
1492 CURSEG_HOT_NODE]);
1493 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
1494 CURSEG_HOT_NODE]);
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001495 if (__exist_node_summaries(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001496 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
1497 type - CURSEG_HOT_NODE);
1498 else
1499 blk_addr = GET_SUM_BLOCK(sbi, segno);
1500 }
1501
1502 new = get_meta_page(sbi, blk_addr);
1503 sum = (struct f2fs_summary_block *)page_address(new);
1504
1505 if (IS_NODESEG(type)) {
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001506 if (__exist_node_summaries(sbi)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001507 struct f2fs_summary *ns = &sum->entries[0];
1508 int i;
1509 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
1510 ns->version = 0;
1511 ns->ofs_in_node = 0;
1512 }
1513 } else {
Gu Zhengd6537882014-03-07 18:43:36 +08001514 int err;
1515
1516 err = restore_node_summary(sbi, segno, sum);
1517 if (err) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001518 f2fs_put_page(new, 1);
Gu Zhengd6537882014-03-07 18:43:36 +08001519 return err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001520 }
1521 }
1522 }
1523
1524 /* set uncompleted segment to curseg */
1525 curseg = CURSEG_I(sbi, type);
1526 mutex_lock(&curseg->curseg_mutex);
1527 memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
1528 curseg->next_segno = segno;
1529 reset_curseg(sbi, type, 0);
1530 curseg->alloc_type = ckpt->alloc_type[type];
1531 curseg->next_blkoff = blk_off;
1532 mutex_unlock(&curseg->curseg_mutex);
1533 f2fs_put_page(new, 1);
1534 return 0;
1535}
1536
1537static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
1538{
1539 int type = CURSEG_HOT_DATA;
Chao Yue4fc5fb2014-03-17 16:36:24 +08001540 int err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001541
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001542 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
Chao Yu3fa06d72014-12-09 14:21:46 +08001543 int npages = npages_for_summary_flush(sbi, true);
1544
1545 if (npages >= 2)
1546 ra_meta_pages(sbi, start_sum_block(sbi), npages,
1547 META_CP);
1548
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001549 /* restore for compacted data summary */
1550 if (read_compacted_summaries(sbi))
1551 return -EINVAL;
1552 type = CURSEG_HOT_NODE;
1553 }
1554
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001555 if (__exist_node_summaries(sbi))
Chao Yu3fa06d72014-12-09 14:21:46 +08001556 ra_meta_pages(sbi, sum_blk_addr(sbi, NR_CURSEG_TYPE, type),
1557 NR_CURSEG_TYPE - type, META_CP);
1558
Chao Yue4fc5fb2014-03-17 16:36:24 +08001559 for (; type <= CURSEG_COLD_NODE; type++) {
1560 err = read_normal_summaries(sbi, type);
1561 if (err)
1562 return err;
1563 }
1564
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001565 return 0;
1566}
1567
1568static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
1569{
1570 struct page *page;
1571 unsigned char *kaddr;
1572 struct f2fs_summary *summary;
1573 struct curseg_info *seg_i;
1574 int written_size = 0;
1575 int i, j;
1576
1577 page = grab_meta_page(sbi, blkaddr++);
1578 kaddr = (unsigned char *)page_address(page);
1579
1580 /* Step 1: write nat cache */
1581 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1582 memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
1583 written_size += SUM_JOURNAL_SIZE;
1584
1585 /* Step 2: write sit cache */
1586 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1587 memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
1588 SUM_JOURNAL_SIZE);
1589 written_size += SUM_JOURNAL_SIZE;
1590
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001591 /* Step 3: write summary entries */
1592 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1593 unsigned short blkoff;
1594 seg_i = CURSEG_I(sbi, i);
1595 if (sbi->ckpt->alloc_type[i] == SSR)
1596 blkoff = sbi->blocks_per_seg;
1597 else
1598 blkoff = curseg_blkoff(sbi, i);
1599
1600 for (j = 0; j < blkoff; j++) {
1601 if (!page) {
1602 page = grab_meta_page(sbi, blkaddr++);
1603 kaddr = (unsigned char *)page_address(page);
1604 written_size = 0;
1605 }
1606 summary = (struct f2fs_summary *)(kaddr + written_size);
1607 *summary = seg_i->sum_blk->entries[j];
1608 written_size += SUMMARY_SIZE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001609
1610 if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1611 SUM_FOOTER_SIZE)
1612 continue;
1613
Chao Yue8d61a72013-10-24 15:08:28 +08001614 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001615 f2fs_put_page(page, 1);
1616 page = NULL;
1617 }
1618 }
Chao Yue8d61a72013-10-24 15:08:28 +08001619 if (page) {
1620 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001621 f2fs_put_page(page, 1);
Chao Yue8d61a72013-10-24 15:08:28 +08001622 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001623}
1624
1625static void write_normal_summaries(struct f2fs_sb_info *sbi,
1626 block_t blkaddr, int type)
1627{
1628 int i, end;
1629 if (IS_DATASEG(type))
1630 end = type + NR_CURSEG_DATA_TYPE;
1631 else
1632 end = type + NR_CURSEG_NODE_TYPE;
1633
1634 for (i = type; i < end; i++) {
1635 struct curseg_info *sum = CURSEG_I(sbi, i);
1636 mutex_lock(&sum->curseg_mutex);
1637 write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
1638 mutex_unlock(&sum->curseg_mutex);
1639 }
1640}
1641
1642void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1643{
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001644 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001645 write_compacted_summaries(sbi, start_blk);
1646 else
1647 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
1648}
1649
1650void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1651{
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001652 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001653}
1654
1655int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
1656 unsigned int val, int alloc)
1657{
1658 int i;
1659
1660 if (type == NAT_JOURNAL) {
1661 for (i = 0; i < nats_in_cursum(sum); i++) {
1662 if (le32_to_cpu(nid_in_journal(sum, i)) == val)
1663 return i;
1664 }
1665 if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
1666 return update_nats_in_cursum(sum, 1);
1667 } else if (type == SIT_JOURNAL) {
1668 for (i = 0; i < sits_in_cursum(sum); i++)
1669 if (le32_to_cpu(segno_in_journal(sum, i)) == val)
1670 return i;
1671 if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
1672 return update_sits_in_cursum(sum, 1);
1673 }
1674 return -1;
1675}
1676
1677static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
1678 unsigned int segno)
1679{
Gu Zheng2cc22182014-10-20 17:45:49 +08001680 return get_meta_page(sbi, current_sit_addr(sbi, segno));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001681}
1682
1683static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
1684 unsigned int start)
1685{
1686 struct sit_info *sit_i = SIT_I(sbi);
1687 struct page *src_page, *dst_page;
1688 pgoff_t src_off, dst_off;
1689 void *src_addr, *dst_addr;
1690
1691 src_off = current_sit_addr(sbi, start);
1692 dst_off = next_sit_addr(sbi, src_off);
1693
1694 /* get current sit block page without lock */
1695 src_page = get_meta_page(sbi, src_off);
1696 dst_page = grab_meta_page(sbi, dst_off);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001697 f2fs_bug_on(sbi, PageDirty(src_page));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001698
1699 src_addr = page_address(src_page);
1700 dst_addr = page_address(dst_page);
1701 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
1702
1703 set_page_dirty(dst_page);
1704 f2fs_put_page(src_page, 1);
1705
1706 set_to_next_sit(sit_i, start);
1707
1708 return dst_page;
1709}
1710
Chao Yu184a5cd2014-09-04 18:13:01 +08001711static struct sit_entry_set *grab_sit_entry_set(void)
1712{
1713 struct sit_entry_set *ses =
1714 f2fs_kmem_cache_alloc(sit_entry_set_slab, GFP_ATOMIC);
1715
1716 ses->entry_cnt = 0;
1717 INIT_LIST_HEAD(&ses->set_list);
1718 return ses;
1719}
1720
1721static void release_sit_entry_set(struct sit_entry_set *ses)
1722{
1723 list_del(&ses->set_list);
1724 kmem_cache_free(sit_entry_set_slab, ses);
1725}
1726
1727static void adjust_sit_entry_set(struct sit_entry_set *ses,
1728 struct list_head *head)
1729{
1730 struct sit_entry_set *next = ses;
1731
1732 if (list_is_last(&ses->set_list, head))
1733 return;
1734
1735 list_for_each_entry_continue(next, head, set_list)
1736 if (ses->entry_cnt <= next->entry_cnt)
1737 break;
1738
1739 list_move_tail(&ses->set_list, &next->set_list);
1740}
1741
1742static void add_sit_entry(unsigned int segno, struct list_head *head)
1743{
1744 struct sit_entry_set *ses;
1745 unsigned int start_segno = START_SEGNO(segno);
1746
1747 list_for_each_entry(ses, head, set_list) {
1748 if (ses->start_segno == start_segno) {
1749 ses->entry_cnt++;
1750 adjust_sit_entry_set(ses, head);
1751 return;
1752 }
1753 }
1754
1755 ses = grab_sit_entry_set();
1756
1757 ses->start_segno = start_segno;
1758 ses->entry_cnt++;
1759 list_add(&ses->set_list, head);
1760}
1761
1762static void add_sits_in_set(struct f2fs_sb_info *sbi)
1763{
1764 struct f2fs_sm_info *sm_info = SM_I(sbi);
1765 struct list_head *set_list = &sm_info->sit_entry_set;
1766 unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap;
Chao Yu184a5cd2014-09-04 18:13:01 +08001767 unsigned int segno;
1768
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001769 for_each_set_bit(segno, bitmap, MAIN_SEGS(sbi))
Chao Yu184a5cd2014-09-04 18:13:01 +08001770 add_sit_entry(segno, set_list);
1771}
1772
1773static void remove_sits_in_journal(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001774{
1775 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1776 struct f2fs_summary_block *sum = curseg->sum_blk;
1777 int i;
1778
Chao Yu184a5cd2014-09-04 18:13:01 +08001779 for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
1780 unsigned int segno;
1781 bool dirtied;
1782
1783 segno = le32_to_cpu(segno_in_journal(sum, i));
1784 dirtied = __mark_sit_entry_dirty(sbi, segno);
1785
1786 if (!dirtied)
1787 add_sit_entry(segno, &SM_I(sbi)->sit_entry_set);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001788 }
Chao Yu184a5cd2014-09-04 18:13:01 +08001789 update_sits_in_cursum(sum, -sits_in_cursum(sum));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001790}
1791
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001792/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001793 * CP calls this function, which flushes SIT entries including sit_journal,
1794 * and moves prefree segs to free segs.
1795 */
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001796void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001797{
1798 struct sit_info *sit_i = SIT_I(sbi);
1799 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
1800 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1801 struct f2fs_summary_block *sum = curseg->sum_blk;
Chao Yu184a5cd2014-09-04 18:13:01 +08001802 struct sit_entry_set *ses, *tmp;
1803 struct list_head *head = &SM_I(sbi)->sit_entry_set;
Chao Yu184a5cd2014-09-04 18:13:01 +08001804 bool to_journal = true;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001805 struct seg_entry *se;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001806
1807 mutex_lock(&curseg->curseg_mutex);
1808 mutex_lock(&sit_i->sentry_lock);
1809
Wanpeng Li2b11a742015-02-27 16:52:50 +08001810 if (!sit_i->dirty_sentries)
1811 goto out;
1812
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001813 /*
Chao Yu184a5cd2014-09-04 18:13:01 +08001814 * add and account sit entries of dirty bitmap in sit entry
1815 * set temporarily
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001816 */
Chao Yu184a5cd2014-09-04 18:13:01 +08001817 add_sits_in_set(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001818
Chao Yu184a5cd2014-09-04 18:13:01 +08001819 /*
1820 * if there are no enough space in journal to store dirty sit
1821 * entries, remove all entries from journal and add and account
1822 * them in sit entry set.
1823 */
1824 if (!__has_cursum_space(sum, sit_i->dirty_sentries, SIT_JOURNAL))
1825 remove_sits_in_journal(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001826
Chao Yu184a5cd2014-09-04 18:13:01 +08001827 /*
1828 * there are two steps to flush sit entries:
1829 * #1, flush sit entries to journal in current cold data summary block.
1830 * #2, flush sit entries to sit page.
1831 */
1832 list_for_each_entry_safe(ses, tmp, head, set_list) {
Jaegeuk Kim4a257ed2014-10-16 11:43:30 -07001833 struct page *page = NULL;
Chao Yu184a5cd2014-09-04 18:13:01 +08001834 struct f2fs_sit_block *raw_sit = NULL;
1835 unsigned int start_segno = ses->start_segno;
1836 unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001837 (unsigned long)MAIN_SEGS(sbi));
Chao Yu184a5cd2014-09-04 18:13:01 +08001838 unsigned int segno = start_segno;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001839
Chao Yu184a5cd2014-09-04 18:13:01 +08001840 if (to_journal &&
1841 !__has_cursum_space(sum, ses->entry_cnt, SIT_JOURNAL))
1842 to_journal = false;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001843
Chao Yu184a5cd2014-09-04 18:13:01 +08001844 if (!to_journal) {
1845 page = get_next_sit_page(sbi, start_segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001846 raw_sit = page_address(page);
1847 }
1848
Chao Yu184a5cd2014-09-04 18:13:01 +08001849 /* flush dirty sit entries in region of current sit set */
1850 for_each_set_bit_from(segno, bitmap, end) {
1851 int offset, sit_offset;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001852
1853 se = get_seg_entry(sbi, segno);
Chao Yu184a5cd2014-09-04 18:13:01 +08001854
1855 /* add discard candidates */
Jaegeuk Kimd7bc2482014-12-12 13:53:41 -08001856 if (cpc->reason != CP_DISCARD) {
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001857 cpc->trim_start = segno;
1858 add_discard_addrs(sbi, cpc);
1859 }
Chao Yu184a5cd2014-09-04 18:13:01 +08001860
1861 if (to_journal) {
1862 offset = lookup_journal_in_cursum(sum,
1863 SIT_JOURNAL, segno, 1);
1864 f2fs_bug_on(sbi, offset < 0);
1865 segno_in_journal(sum, offset) =
1866 cpu_to_le32(segno);
1867 seg_info_to_raw_sit(se,
1868 &sit_in_journal(sum, offset));
1869 } else {
1870 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
1871 seg_info_to_raw_sit(se,
1872 &raw_sit->entries[sit_offset]);
1873 }
1874
1875 __clear_bit(segno, bitmap);
1876 sit_i->dirty_sentries--;
1877 ses->entry_cnt--;
1878 }
1879
1880 if (!to_journal)
1881 f2fs_put_page(page, 1);
1882
1883 f2fs_bug_on(sbi, ses->entry_cnt);
1884 release_sit_entry_set(ses);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001885 }
Chao Yu184a5cd2014-09-04 18:13:01 +08001886
1887 f2fs_bug_on(sbi, !list_empty(head));
1888 f2fs_bug_on(sbi, sit_i->dirty_sentries);
Chao Yu184a5cd2014-09-04 18:13:01 +08001889out:
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001890 if (cpc->reason == CP_DISCARD) {
1891 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++)
1892 add_discard_addrs(sbi, cpc);
1893 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001894 mutex_unlock(&sit_i->sentry_lock);
1895 mutex_unlock(&curseg->curseg_mutex);
1896
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001897 set_prefree_as_free_segments(sbi);
1898}
1899
1900static int build_sit_info(struct f2fs_sb_info *sbi)
1901{
1902 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1903 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1904 struct sit_info *sit_i;
1905 unsigned int sit_segs, start;
1906 char *src_bitmap, *dst_bitmap;
1907 unsigned int bitmap_size;
1908
1909 /* allocate memory for SIT information */
1910 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
1911 if (!sit_i)
1912 return -ENOMEM;
1913
1914 SM_I(sbi)->sit_info = sit_i;
1915
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001916 sit_i->sentries = vzalloc(MAIN_SEGS(sbi) * sizeof(struct seg_entry));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001917 if (!sit_i->sentries)
1918 return -ENOMEM;
1919
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001920 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001921 sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1922 if (!sit_i->dirty_sentries_bitmap)
1923 return -ENOMEM;
1924
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001925 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001926 sit_i->sentries[start].cur_valid_map
1927 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1928 sit_i->sentries[start].ckpt_valid_map
1929 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001930 sit_i->sentries[start].discard_map
1931 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1932 if (!sit_i->sentries[start].cur_valid_map ||
1933 !sit_i->sentries[start].ckpt_valid_map ||
1934 !sit_i->sentries[start].discard_map)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001935 return -ENOMEM;
1936 }
1937
Jaegeuk Kim60a3b782015-02-10 16:44:29 -08001938 sit_i->tmp_map = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1939 if (!sit_i->tmp_map)
1940 return -ENOMEM;
1941
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001942 if (sbi->segs_per_sec > 1) {
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001943 sit_i->sec_entries = vzalloc(MAIN_SECS(sbi) *
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001944 sizeof(struct sec_entry));
1945 if (!sit_i->sec_entries)
1946 return -ENOMEM;
1947 }
1948
1949 /* get information related with SIT */
1950 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
1951
1952 /* setup SIT bitmap from ckeckpoint pack */
1953 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
1954 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
1955
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001956 dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001957 if (!dst_bitmap)
1958 return -ENOMEM;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001959
1960 /* init SIT information */
1961 sit_i->s_ops = &default_salloc_ops;
1962
1963 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
1964 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
1965 sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
1966 sit_i->sit_bitmap = dst_bitmap;
1967 sit_i->bitmap_size = bitmap_size;
1968 sit_i->dirty_sentries = 0;
1969 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
1970 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
1971 sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
1972 mutex_init(&sit_i->sentry_lock);
1973 return 0;
1974}
1975
1976static int build_free_segmap(struct f2fs_sb_info *sbi)
1977{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001978 struct free_segmap_info *free_i;
1979 unsigned int bitmap_size, sec_bitmap_size;
1980
1981 /* allocate memory for free segmap information */
1982 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
1983 if (!free_i)
1984 return -ENOMEM;
1985
1986 SM_I(sbi)->free_info = free_i;
1987
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001988 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001989 free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL);
1990 if (!free_i->free_segmap)
1991 return -ENOMEM;
1992
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001993 sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001994 free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL);
1995 if (!free_i->free_secmap)
1996 return -ENOMEM;
1997
1998 /* set all segments as dirty temporarily */
1999 memset(free_i->free_segmap, 0xff, bitmap_size);
2000 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
2001
2002 /* init free segmap information */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002003 free_i->start_segno = GET_SEGNO_FROM_SEG0(sbi, MAIN_BLKADDR(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002004 free_i->free_segments = 0;
2005 free_i->free_sections = 0;
Chao Yu1a118cc2015-02-11 18:20:38 +08002006 spin_lock_init(&free_i->segmap_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002007 return 0;
2008}
2009
2010static int build_curseg(struct f2fs_sb_info *sbi)
2011{
Namjae Jeon1042d602012-12-01 10:56:13 +09002012 struct curseg_info *array;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002013 int i;
2014
Fabian Frederickb434bab2014-06-23 18:39:15 +02002015 array = kcalloc(NR_CURSEG_TYPE, sizeof(*array), GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002016 if (!array)
2017 return -ENOMEM;
2018
2019 SM_I(sbi)->curseg_array = array;
2020
2021 for (i = 0; i < NR_CURSEG_TYPE; i++) {
2022 mutex_init(&array[i].curseg_mutex);
2023 array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
2024 if (!array[i].sum_blk)
2025 return -ENOMEM;
2026 array[i].segno = NULL_SEGNO;
2027 array[i].next_blkoff = 0;
2028 }
2029 return restore_curseg_summaries(sbi);
2030}
2031
2032static void build_sit_entries(struct f2fs_sb_info *sbi)
2033{
2034 struct sit_info *sit_i = SIT_I(sbi);
2035 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
2036 struct f2fs_summary_block *sum = curseg->sum_blk;
Chao Yu74de5932013-11-22 09:09:59 +08002037 int sit_blk_cnt = SIT_BLK_CNT(sbi);
2038 unsigned int i, start, end;
2039 unsigned int readed, start_blk = 0;
Jaegeuk Kim90a893c2014-09-22 16:21:07 -07002040 int nrpages = MAX_BIO_BLOCKS(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002041
Chao Yu74de5932013-11-22 09:09:59 +08002042 do {
Chao Yu662befd2014-02-07 16:11:53 +08002043 readed = ra_meta_pages(sbi, start_blk, nrpages, META_SIT);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002044
Chao Yu74de5932013-11-22 09:09:59 +08002045 start = start_blk * sit_i->sents_per_block;
2046 end = (start_blk + readed) * sit_i->sents_per_block;
2047
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002048 for (; start < end && start < MAIN_SEGS(sbi); start++) {
Chao Yu74de5932013-11-22 09:09:59 +08002049 struct seg_entry *se = &sit_i->sentries[start];
2050 struct f2fs_sit_block *sit_blk;
2051 struct f2fs_sit_entry sit;
2052 struct page *page;
2053
2054 mutex_lock(&curseg->curseg_mutex);
2055 for (i = 0; i < sits_in_cursum(sum); i++) {
Chris Fries6c311ec2014-01-17 14:44:39 -06002056 if (le32_to_cpu(segno_in_journal(sum, i))
2057 == start) {
Chao Yu74de5932013-11-22 09:09:59 +08002058 sit = sit_in_journal(sum, i);
2059 mutex_unlock(&curseg->curseg_mutex);
2060 goto got_it;
2061 }
2062 }
2063 mutex_unlock(&curseg->curseg_mutex);
2064
2065 page = get_current_sit_page(sbi, start);
2066 sit_blk = (struct f2fs_sit_block *)page_address(page);
2067 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
2068 f2fs_put_page(page, 1);
2069got_it:
2070 check_block_count(sbi, start, &sit);
2071 seg_info_from_raw_sit(se, &sit);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07002072
2073 /* build discard map only one time */
2074 memcpy(se->discard_map, se->cur_valid_map, SIT_VBLOCK_MAP_SIZE);
2075 sbi->discard_blks += sbi->blocks_per_seg - se->valid_blocks;
2076
Chao Yu74de5932013-11-22 09:09:59 +08002077 if (sbi->segs_per_sec > 1) {
2078 struct sec_entry *e = get_sec_entry(sbi, start);
2079 e->valid_blocks += se->valid_blocks;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002080 }
2081 }
Chao Yu74de5932013-11-22 09:09:59 +08002082 start_blk += readed;
2083 } while (start_blk < sit_blk_cnt);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002084}
2085
2086static void init_free_segmap(struct f2fs_sb_info *sbi)
2087{
2088 unsigned int start;
2089 int type;
2090
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002091 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002092 struct seg_entry *sentry = get_seg_entry(sbi, start);
2093 if (!sentry->valid_blocks)
2094 __set_free(sbi, start);
2095 }
2096
2097 /* set use the current segments */
2098 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
2099 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
2100 __set_test_and_inuse(sbi, curseg_t->segno);
2101 }
2102}
2103
2104static void init_dirty_segmap(struct f2fs_sb_info *sbi)
2105{
2106 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2107 struct free_segmap_info *free_i = FREE_I(sbi);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002108 unsigned int segno = 0, offset = 0;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002109 unsigned short valid_blocks;
2110
Namjae Jeon8736fbf2013-06-16 09:49:11 +09002111 while (1) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002112 /* find dirty segment based on free segmap */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002113 segno = find_next_inuse(free_i, MAIN_SEGS(sbi), offset);
2114 if (segno >= MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002115 break;
2116 offset = segno + 1;
2117 valid_blocks = get_valid_blocks(sbi, segno, 0);
Jaegeuk Kimec325b52014-09-02 16:24:11 -07002118 if (valid_blocks == sbi->blocks_per_seg || !valid_blocks)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002119 continue;
Jaegeuk Kimec325b52014-09-02 16:24:11 -07002120 if (valid_blocks > sbi->blocks_per_seg) {
2121 f2fs_bug_on(sbi, 1);
2122 continue;
2123 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002124 mutex_lock(&dirty_i->seglist_lock);
2125 __locate_dirty_segment(sbi, segno, DIRTY);
2126 mutex_unlock(&dirty_i->seglist_lock);
2127 }
2128}
2129
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002130static int init_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002131{
2132 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002133 unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002134
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002135 dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL);
2136 if (!dirty_i->victim_secmap)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002137 return -ENOMEM;
2138 return 0;
2139}
2140
2141static int build_dirty_segmap(struct f2fs_sb_info *sbi)
2142{
2143 struct dirty_seglist_info *dirty_i;
2144 unsigned int bitmap_size, i;
2145
2146 /* allocate memory for dirty segments list information */
2147 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
2148 if (!dirty_i)
2149 return -ENOMEM;
2150
2151 SM_I(sbi)->dirty_info = dirty_i;
2152 mutex_init(&dirty_i->seglist_lock);
2153
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002154 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002155
2156 for (i = 0; i < NR_DIRTY_TYPE; i++) {
2157 dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002158 if (!dirty_i->dirty_segmap[i])
2159 return -ENOMEM;
2160 }
2161
2162 init_dirty_segmap(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002163 return init_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002164}
2165
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002166/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002167 * Update min, max modified time for cost-benefit GC algorithm
2168 */
2169static void init_min_max_mtime(struct f2fs_sb_info *sbi)
2170{
2171 struct sit_info *sit_i = SIT_I(sbi);
2172 unsigned int segno;
2173
2174 mutex_lock(&sit_i->sentry_lock);
2175
2176 sit_i->min_mtime = LLONG_MAX;
2177
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002178 for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002179 unsigned int i;
2180 unsigned long long mtime = 0;
2181
2182 for (i = 0; i < sbi->segs_per_sec; i++)
2183 mtime += get_seg_entry(sbi, segno + i)->mtime;
2184
2185 mtime = div_u64(mtime, sbi->segs_per_sec);
2186
2187 if (sit_i->min_mtime > mtime)
2188 sit_i->min_mtime = mtime;
2189 }
2190 sit_i->max_mtime = get_mtime(sbi);
2191 mutex_unlock(&sit_i->sentry_lock);
2192}
2193
2194int build_segment_manager(struct f2fs_sb_info *sbi)
2195{
2196 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
2197 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Namjae Jeon1042d602012-12-01 10:56:13 +09002198 struct f2fs_sm_info *sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002199 int err;
2200
2201 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
2202 if (!sm_info)
2203 return -ENOMEM;
2204
2205 /* init sm info */
2206 sbi->sm_info = sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002207 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
2208 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
2209 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
2210 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
2211 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
2212 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
2213 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
Jaegeuk Kim58c41032014-03-19 14:17:21 +09002214 sm_info->rec_prefree_segments = sm_info->main_segments *
2215 DEF_RECLAIM_PREFREE_SEGMENTS / 100;
Jaegeuk Kim9b5f1362014-09-16 18:30:54 -07002216 sm_info->ipu_policy = 1 << F2FS_IPU_FSYNC;
Jaegeuk Kim216fbd62013-11-07 13:13:42 +09002217 sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -07002218 sm_info->min_fsync_blocks = DEF_MIN_FSYNC_BLOCKS;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002219
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002220 INIT_LIST_HEAD(&sm_info->discard_list);
2221 sm_info->nr_discards = 0;
2222 sm_info->max_discards = 0;
2223
Jaegeuk Kimbba681c2015-01-26 17:41:23 -08002224 sm_info->trim_sections = DEF_BATCHED_TRIM_SECTIONS;
2225
Chao Yu184a5cd2014-09-04 18:13:01 +08002226 INIT_LIST_HEAD(&sm_info->sit_entry_set);
2227
Gu Zhengb270ad62014-04-11 17:49:55 +08002228 if (test_opt(sbi, FLUSH_MERGE) && !f2fs_readonly(sbi->sb)) {
Gu Zheng2163d192014-04-27 14:21:33 +08002229 err = create_flush_cmd_control(sbi);
2230 if (err)
Gu Zhenga688b9d9e2014-04-27 14:21:21 +08002231 return err;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +09002232 }
2233
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002234 err = build_sit_info(sbi);
2235 if (err)
2236 return err;
2237 err = build_free_segmap(sbi);
2238 if (err)
2239 return err;
2240 err = build_curseg(sbi);
2241 if (err)
2242 return err;
2243
2244 /* reinit free segmap based on SIT */
2245 build_sit_entries(sbi);
2246
2247 init_free_segmap(sbi);
2248 err = build_dirty_segmap(sbi);
2249 if (err)
2250 return err;
2251
2252 init_min_max_mtime(sbi);
2253 return 0;
2254}
2255
2256static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
2257 enum dirty_type dirty_type)
2258{
2259 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2260
2261 mutex_lock(&dirty_i->seglist_lock);
2262 kfree(dirty_i->dirty_segmap[dirty_type]);
2263 dirty_i->nr_dirty[dirty_type] = 0;
2264 mutex_unlock(&dirty_i->seglist_lock);
2265}
2266
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002267static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002268{
2269 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002270 kfree(dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002271}
2272
2273static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
2274{
2275 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2276 int i;
2277
2278 if (!dirty_i)
2279 return;
2280
2281 /* discard pre-free/dirty segments list */
2282 for (i = 0; i < NR_DIRTY_TYPE; i++)
2283 discard_dirty_segmap(sbi, i);
2284
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002285 destroy_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002286 SM_I(sbi)->dirty_info = NULL;
2287 kfree(dirty_i);
2288}
2289
2290static void destroy_curseg(struct f2fs_sb_info *sbi)
2291{
2292 struct curseg_info *array = SM_I(sbi)->curseg_array;
2293 int i;
2294
2295 if (!array)
2296 return;
2297 SM_I(sbi)->curseg_array = NULL;
2298 for (i = 0; i < NR_CURSEG_TYPE; i++)
2299 kfree(array[i].sum_blk);
2300 kfree(array);
2301}
2302
2303static void destroy_free_segmap(struct f2fs_sb_info *sbi)
2304{
2305 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
2306 if (!free_i)
2307 return;
2308 SM_I(sbi)->free_info = NULL;
2309 kfree(free_i->free_segmap);
2310 kfree(free_i->free_secmap);
2311 kfree(free_i);
2312}
2313
2314static void destroy_sit_info(struct f2fs_sb_info *sbi)
2315{
2316 struct sit_info *sit_i = SIT_I(sbi);
2317 unsigned int start;
2318
2319 if (!sit_i)
2320 return;
2321
2322 if (sit_i->sentries) {
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002323 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002324 kfree(sit_i->sentries[start].cur_valid_map);
2325 kfree(sit_i->sentries[start].ckpt_valid_map);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07002326 kfree(sit_i->sentries[start].discard_map);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002327 }
2328 }
Jaegeuk Kim60a3b782015-02-10 16:44:29 -08002329 kfree(sit_i->tmp_map);
2330
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002331 vfree(sit_i->sentries);
2332 vfree(sit_i->sec_entries);
2333 kfree(sit_i->dirty_sentries_bitmap);
2334
2335 SM_I(sbi)->sit_info = NULL;
2336 kfree(sit_i->sit_bitmap);
2337 kfree(sit_i);
2338}
2339
2340void destroy_segment_manager(struct f2fs_sb_info *sbi)
2341{
2342 struct f2fs_sm_info *sm_info = SM_I(sbi);
Gu Zhenga688b9d9e2014-04-27 14:21:21 +08002343
Chao Yu3b03f722013-11-06 09:12:04 +08002344 if (!sm_info)
2345 return;
Gu Zheng2163d192014-04-27 14:21:33 +08002346 destroy_flush_cmd_control(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002347 destroy_dirty_segmap(sbi);
2348 destroy_curseg(sbi);
2349 destroy_free_segmap(sbi);
2350 destroy_sit_info(sbi);
2351 sbi->sm_info = NULL;
2352 kfree(sm_info);
2353}
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002354
2355int __init create_segment_manager_caches(void)
2356{
2357 discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
Gu Zhenge8512d22014-03-07 18:43:28 +08002358 sizeof(struct discard_entry));
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002359 if (!discard_entry_slab)
Chao Yu184a5cd2014-09-04 18:13:01 +08002360 goto fail;
2361
2362 sit_entry_set_slab = f2fs_kmem_cache_create("sit_entry_set",
Changman Leec9ee0082014-11-21 15:42:07 +09002363 sizeof(struct sit_entry_set));
Chao Yu184a5cd2014-09-04 18:13:01 +08002364 if (!sit_entry_set_slab)
2365 goto destory_discard_entry;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002366
2367 inmem_entry_slab = f2fs_kmem_cache_create("inmem_page_entry",
2368 sizeof(struct inmem_pages));
2369 if (!inmem_entry_slab)
2370 goto destroy_sit_entry_set;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002371 return 0;
Chao Yu184a5cd2014-09-04 18:13:01 +08002372
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002373destroy_sit_entry_set:
2374 kmem_cache_destroy(sit_entry_set_slab);
Chao Yu184a5cd2014-09-04 18:13:01 +08002375destory_discard_entry:
2376 kmem_cache_destroy(discard_entry_slab);
2377fail:
2378 return -ENOMEM;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002379}
2380
2381void destroy_segment_manager_caches(void)
2382{
Chao Yu184a5cd2014-09-04 18:13:01 +08002383 kmem_cache_destroy(sit_entry_set_slab);
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002384 kmem_cache_destroy(discard_entry_slab);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002385 kmem_cache_destroy(inmem_entry_slab);
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002386}