blob: 1f1200487c4411e2bdeb06bbdbfb40d2d2e6b2e3 [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
Jaegeuk Kimedb27de2015-07-25 00:52:52 -0700230int commit_inmem_pages(struct inode *inode, bool abort)
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700231{
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 };
Jaegeuk Kimedb27de2015-07-25 00:52:52 -0700242 int err = 0;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700243
Jaegeuk Kim03418452014-11-21 16:37:40 -0800244 /*
245 * The abort is true only when f2fs_evict_inode is called.
246 * Basically, the f2fs_evict_inode doesn't produce any data writes, so
247 * that we don't need to call f2fs_balance_fs.
248 * Otherwise, f2fs_gc in f2fs_balance_fs can wait forever until this
249 * inode becomes free by iget_locked in f2fs_iget.
250 */
Jaegeuk Kim70c640b2014-12-10 13:59:33 -0800251 if (!abort) {
Jaegeuk Kim03418452014-11-21 16:37:40 -0800252 f2fs_balance_fs(sbi);
Jaegeuk Kim70c640b2014-12-10 13:59:33 -0800253 f2fs_lock_op(sbi);
254 }
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700255
256 mutex_lock(&fi->inmem_lock);
257 list_for_each_entry_safe(cur, tmp, &fi->inmem_pages, list) {
Jaegeuk Kim70c640b2014-12-10 13:59:33 -0800258 if (!abort) {
259 lock_page(cur->page);
260 if (cur->page->mapping == inode->i_mapping) {
Jaegeuk Kim6282adb2015-07-25 00:29:17 -0700261 set_page_dirty(cur->page);
Jaegeuk Kim70c640b2014-12-10 13:59:33 -0800262 f2fs_wait_on_page_writeback(cur->page, DATA);
263 if (clear_page_dirty_for_io(cur->page))
264 inode_dec_dirty_pages(inode);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -0700265 trace_f2fs_commit_inmem_page(cur->page, INMEM);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700266 fio.page = cur->page;
Jaegeuk Kimedb27de2015-07-25 00:52:52 -0700267 err = do_write_data_page(&fio);
Jaegeuk Kim70c640b2014-12-10 13:59:33 -0800268 submit_bio = true;
Jaegeuk Kimedb27de2015-07-25 00:52:52 -0700269 if (err) {
270 unlock_page(cur->page);
271 break;
272 }
Jaegeuk Kim70c640b2014-12-10 13:59:33 -0800273 }
274 f2fs_put_page(cur->page, 1);
275 } else {
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -0700276 trace_f2fs_commit_inmem_page(cur->page, INMEM_DROP);
Jaegeuk Kim70c640b2014-12-10 13:59:33 -0800277 put_page(cur->page);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700278 }
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -0700279 radix_tree_delete(&fi->inmem_root, cur->page->index);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700280 list_del(&cur->list);
281 kmem_cache_free(inmem_entry_slab, cur);
Jaegeuk Kim8dcf2ff72014-12-05 17:18:15 -0800282 dec_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700283 }
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700284 mutex_unlock(&fi->inmem_lock);
285
Jaegeuk Kim70c640b2014-12-10 13:59:33 -0800286 if (!abort) {
287 f2fs_unlock_op(sbi);
288 if (submit_bio)
289 f2fs_submit_merged_bio(sbi, DATA, WRITE);
290 }
Jaegeuk Kimedb27de2015-07-25 00:52:52 -0700291 return err;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700292}
293
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900294/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900295 * This function balances dirty node and dentry pages.
296 * In addition, it controls garbage collection.
297 */
298void f2fs_balance_fs(struct f2fs_sb_info *sbi)
299{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900300 /*
Jaegeuk Kim029cd282012-12-21 17:20:21 +0900301 * We should do GC or end up with checkpoint, if there are so many dirty
302 * dir/node pages without enough free segments.
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900303 */
Jaegeuk Kim43727522013-02-04 15:11:17 +0900304 if (has_not_enough_free_secs(sbi, 0)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900305 mutex_lock(&sbi->gc_mutex);
Jaegeuk Kim408e9372013-01-03 17:55:52 +0900306 f2fs_gc(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900307 }
308}
309
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900310void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi)
311{
Chao Yu1dcc3362015-02-05 17:57:31 +0800312 /* try to shrink extent cache when there is no enough memory */
Jaegeuk Kim554df792015-06-19 13:41:23 -0700313 if (!available_free_memory(sbi, EXTENT_CACHE))
314 f2fs_shrink_extent_tree(sbi, EXTENT_CACHE_SHRINK_NUMBER);
Chao Yu1dcc3362015-02-05 17:57:31 +0800315
Jaegeuk Kim1b38dc82015-06-19 15:36:07 -0700316 /* check the # of cached NAT entries */
317 if (!available_free_memory(sbi, NAT_ENTRIES))
318 try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK);
319
320 /* checkpoint is the only way to shrink partial cached entries */
321 if (!available_free_memory(sbi, NAT_ENTRIES) ||
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -0800322 excess_prefree_segs(sbi) ||
Jaegeuk Kim88a70a62014-12-10 15:20:48 -0800323 !available_free_memory(sbi, INO_ENTRIES))
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900324 f2fs_sync_fs(sbi->sb, true);
325}
326
Gu Zheng2163d192014-04-27 14:21:33 +0800327static int issue_flush_thread(void *data)
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900328{
329 struct f2fs_sb_info *sbi = data;
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800330 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
331 wait_queue_head_t *q = &fcc->flush_wait_queue;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900332repeat:
333 if (kthread_should_stop())
334 return 0;
335
Gu Zheng721bd4d2014-09-05 18:31:00 +0800336 if (!llist_empty(&fcc->issue_list)) {
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900337 struct bio *bio = bio_alloc(GFP_NOIO, 0);
338 struct flush_cmd *cmd, *next;
339 int ret;
340
Gu Zheng721bd4d2014-09-05 18:31:00 +0800341 fcc->dispatch_list = llist_del_all(&fcc->issue_list);
342 fcc->dispatch_list = llist_reverse_order(fcc->dispatch_list);
343
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900344 bio->bi_bdev = sbi->sb->s_bdev;
345 ret = submit_bio_wait(WRITE_FLUSH, bio);
346
Gu Zheng721bd4d2014-09-05 18:31:00 +0800347 llist_for_each_entry_safe(cmd, next,
348 fcc->dispatch_list, llnode) {
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900349 cmd->ret = ret;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900350 complete(&cmd->wait);
351 }
Gu Zhenga4ed23f2014-04-11 17:49:35 +0800352 bio_put(bio);
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800353 fcc->dispatch_list = NULL;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900354 }
355
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800356 wait_event_interruptible(*q,
Gu Zheng721bd4d2014-09-05 18:31:00 +0800357 kthread_should_stop() || !llist_empty(&fcc->issue_list));
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900358 goto repeat;
359}
360
361int f2fs_issue_flush(struct f2fs_sb_info *sbi)
362{
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800363 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
Chao Yuadf8d902014-05-08 17:00:35 +0800364 struct flush_cmd cmd;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900365
Jaegeuk Kim24a9ee02014-07-25 17:46:10 -0700366 trace_f2fs_issue_flush(sbi->sb, test_opt(sbi, NOBARRIER),
367 test_opt(sbi, FLUSH_MERGE));
368
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700369 if (test_opt(sbi, NOBARRIER))
370 return 0;
371
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900372 if (!test_opt(sbi, FLUSH_MERGE))
373 return blkdev_issue_flush(sbi->sb->s_bdev, GFP_KERNEL, NULL);
374
Chao Yuadf8d902014-05-08 17:00:35 +0800375 init_completion(&cmd.wait);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900376
Gu Zheng721bd4d2014-09-05 18:31:00 +0800377 llist_add(&cmd.llnode, &fcc->issue_list);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900378
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800379 if (!fcc->dispatch_list)
380 wake_up(&fcc->flush_wait_queue);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900381
Chao Yuadf8d902014-05-08 17:00:35 +0800382 wait_for_completion(&cmd.wait);
383
384 return cmd.ret;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900385}
386
Gu Zheng2163d192014-04-27 14:21:33 +0800387int create_flush_cmd_control(struct f2fs_sb_info *sbi)
388{
389 dev_t dev = sbi->sb->s_bdev->bd_dev;
390 struct flush_cmd_control *fcc;
391 int err = 0;
392
393 fcc = kzalloc(sizeof(struct flush_cmd_control), GFP_KERNEL);
394 if (!fcc)
395 return -ENOMEM;
Gu Zheng2163d192014-04-27 14:21:33 +0800396 init_waitqueue_head(&fcc->flush_wait_queue);
Gu Zheng721bd4d2014-09-05 18:31:00 +0800397 init_llist_head(&fcc->issue_list);
Chao Yu6b2920a2014-07-07 11:21:59 +0800398 SM_I(sbi)->cmd_control_info = fcc;
Gu Zheng2163d192014-04-27 14:21:33 +0800399 fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi,
400 "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev));
401 if (IS_ERR(fcc->f2fs_issue_flush)) {
402 err = PTR_ERR(fcc->f2fs_issue_flush);
403 kfree(fcc);
Chao Yu6b2920a2014-07-07 11:21:59 +0800404 SM_I(sbi)->cmd_control_info = NULL;
Gu Zheng2163d192014-04-27 14:21:33 +0800405 return err;
406 }
Gu Zheng2163d192014-04-27 14:21:33 +0800407
408 return err;
409}
410
411void destroy_flush_cmd_control(struct f2fs_sb_info *sbi)
412{
Chao Yu6b2920a2014-07-07 11:21:59 +0800413 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
Gu Zheng2163d192014-04-27 14:21:33 +0800414
415 if (fcc && fcc->f2fs_issue_flush)
416 kthread_stop(fcc->f2fs_issue_flush);
417 kfree(fcc);
Chao Yu6b2920a2014-07-07 11:21:59 +0800418 SM_I(sbi)->cmd_control_info = NULL;
Gu Zheng2163d192014-04-27 14:21:33 +0800419}
420
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900421static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
422 enum dirty_type dirty_type)
423{
424 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
425
426 /* need not be added */
427 if (IS_CURSEG(sbi, segno))
428 return;
429
430 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
431 dirty_i->nr_dirty[dirty_type]++;
432
433 if (dirty_type == DIRTY) {
434 struct seg_entry *sentry = get_seg_entry(sbi, segno);
Changman Lee4625d6a2013-10-25 17:31:57 +0900435 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900436
Jaegeuk Kimec325b52014-09-02 16:24:11 -0700437 if (unlikely(t >= DIRTY)) {
438 f2fs_bug_on(sbi, 1);
439 return;
440 }
Changman Lee4625d6a2013-10-25 17:31:57 +0900441 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
442 dirty_i->nr_dirty[t]++;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900443 }
444}
445
446static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
447 enum dirty_type dirty_type)
448{
449 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
450
451 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
452 dirty_i->nr_dirty[dirty_type]--;
453
454 if (dirty_type == DIRTY) {
Changman Lee4625d6a2013-10-25 17:31:57 +0900455 struct seg_entry *sentry = get_seg_entry(sbi, segno);
456 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900457
Changman Lee4625d6a2013-10-25 17:31:57 +0900458 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
459 dirty_i->nr_dirty[t]--;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900460
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +0900461 if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0)
462 clear_bit(GET_SECNO(sbi, segno),
463 dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900464 }
465}
466
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900467/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900468 * Should not occur error such as -ENOMEM.
469 * Adding dirty entry into seglist is not critical operation.
470 * If a given segment is one of current working segments, it won't be added.
471 */
Haicheng Li8d8451a2013-06-13 16:59:28 +0800472static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900473{
474 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
475 unsigned short valid_blocks;
476
477 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
478 return;
479
480 mutex_lock(&dirty_i->seglist_lock);
481
482 valid_blocks = get_valid_blocks(sbi, segno, 0);
483
484 if (valid_blocks == 0) {
485 __locate_dirty_segment(sbi, segno, PRE);
486 __remove_dirty_segment(sbi, segno, DIRTY);
487 } else if (valid_blocks < sbi->blocks_per_seg) {
488 __locate_dirty_segment(sbi, segno, DIRTY);
489 } else {
490 /* Recovery routine with SSR needs this */
491 __remove_dirty_segment(sbi, segno, DIRTY);
492 }
493
494 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900495}
496
Jaegeuk Kim1e87a782014-04-15 13:57:55 +0900497static int f2fs_issue_discard(struct f2fs_sb_info *sbi,
Jaegeuk Kim37208872013-11-12 16:55:17 +0900498 block_t blkstart, block_t blklen)
499{
Chao Yu55cf9cb2014-09-15 18:01:10 +0800500 sector_t start = SECTOR_FROM_BLOCK(blkstart);
501 sector_t len = SECTOR_FROM_BLOCK(blklen);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700502 struct seg_entry *se;
503 unsigned int offset;
504 block_t i;
505
506 for (i = blkstart; i < blkstart + blklen; i++) {
507 se = get_seg_entry(sbi, GET_SEGNO(sbi, i));
508 offset = GET_BLKOFF_FROM_SEG0(sbi, i);
509
510 if (!f2fs_test_and_set_bit(offset, se->discard_map))
511 sbi->discard_blks--;
512 }
Jaegeuk Kim1661d072013-11-12 17:01:00 +0900513 trace_f2fs_issue_discard(sbi->sb, blkstart, blklen);
Jaegeuk Kim1e87a782014-04-15 13:57:55 +0900514 return blkdev_issue_discard(sbi->sb->s_bdev, start, len, GFP_NOFS, 0);
515}
516
Chao Yue90c2d22015-07-28 18:36:47 +0800517bool discard_next_dnode(struct f2fs_sb_info *sbi, block_t blkaddr)
Jaegeuk Kim1e87a782014-04-15 13:57:55 +0900518{
Jaegeuk Kim40a02be2015-05-11 20:03:49 -0700519 int err = -ENOTSUPP;
520
521 if (test_opt(sbi, DISCARD)) {
522 struct seg_entry *se = get_seg_entry(sbi,
523 GET_SEGNO(sbi, blkaddr));
524 unsigned int offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
525
526 if (f2fs_test_bit(offset, se->discard_map))
Chao Yue90c2d22015-07-28 18:36:47 +0800527 return false;
Jaegeuk Kim40a02be2015-05-11 20:03:49 -0700528
529 err = f2fs_issue_discard(sbi, blkaddr, 1);
530 }
531
Chao Yue90c2d22015-07-28 18:36:47 +0800532 if (err) {
Chao Yu381722d2015-05-19 17:40:04 +0800533 update_meta_page(sbi, NULL, blkaddr);
Chao Yue90c2d22015-07-28 18:36:47 +0800534 return true;
535 }
536 return false;
Jaegeuk Kim37208872013-11-12 16:55:17 +0900537}
538
Jaegeuk Kimadf49832014-10-28 22:27:59 -0700539static void __add_discard_entry(struct f2fs_sb_info *sbi,
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700540 struct cp_control *cpc, struct seg_entry *se,
541 unsigned int start, unsigned int end)
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900542{
543 struct list_head *head = &SM_I(sbi)->discard_list;
Jaegeuk Kimadf49832014-10-28 22:27:59 -0700544 struct discard_entry *new, *last;
545
546 if (!list_empty(head)) {
547 last = list_last_entry(head, struct discard_entry, list);
548 if (START_BLOCK(sbi, cpc->trim_start) + start ==
549 last->blkaddr + last->len) {
550 last->len += end - start;
551 goto done;
552 }
553 }
554
555 new = f2fs_kmem_cache_alloc(discard_entry_slab, GFP_NOFS);
556 INIT_LIST_HEAD(&new->list);
557 new->blkaddr = START_BLOCK(sbi, cpc->trim_start) + start;
558 new->len = end - start;
559 list_add_tail(&new->list, head);
560done:
561 SM_I(sbi)->nr_discards += end - start;
Jaegeuk Kimadf49832014-10-28 22:27:59 -0700562}
563
564static void add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc)
565{
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900566 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
567 int max_blocks = sbi->blocks_per_seg;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700568 struct seg_entry *se = get_seg_entry(sbi, cpc->trim_start);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900569 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
570 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700571 unsigned long *discard_map = (unsigned long *)se->discard_map;
Jaegeuk Kim60a3b782015-02-10 16:44:29 -0800572 unsigned long *dmap = SIT_I(sbi)->tmp_map;
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900573 unsigned int start = 0, end = -1;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700574 bool force = (cpc->reason == CP_DISCARD);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900575 int i;
576
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700577 if (se->valid_blocks == max_blocks)
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900578 return;
579
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700580 if (!force) {
581 if (!test_opt(sbi, DISCARD) || !se->valid_blocks ||
Dan Carpenter912a83b2015-05-14 11:52:28 +0300582 SM_I(sbi)->nr_discards >= SM_I(sbi)->max_discards)
583 return;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700584 }
585
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900586 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
587 for (i = 0; i < entries; i++)
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700588 dmap[i] = force ? ~ckpt_map[i] & ~discard_map[i] :
Jaegeuk Kimd7bc2482014-12-12 13:53:41 -0800589 (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900590
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700591 while (force || SM_I(sbi)->nr_discards <= SM_I(sbi)->max_discards) {
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900592 start = __find_rev_next_bit(dmap, max_blocks, end + 1);
593 if (start >= max_blocks)
594 break;
595
596 end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700597 __add_discard_entry(sbi, cpc, se, start, end);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900598 }
599}
600
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700601void release_discard_addrs(struct f2fs_sb_info *sbi)
602{
603 struct list_head *head = &(SM_I(sbi)->discard_list);
604 struct discard_entry *entry, *this;
605
606 /* drop caches */
607 list_for_each_entry_safe(entry, this, head, list) {
608 list_del(&entry->list);
609 kmem_cache_free(discard_entry_slab, entry);
610 }
611}
612
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900613/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900614 * Should call clear_prefree_segments after checkpoint is done.
615 */
616static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
617{
618 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Chao Yub65ee142014-08-04 10:10:07 +0800619 unsigned int segno;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900620
621 mutex_lock(&dirty_i->seglist_lock);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700622 for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900623 __set_test_and_free(sbi, segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900624 mutex_unlock(&dirty_i->seglist_lock);
625}
626
Jaegeuk Kim836b5a62015-04-30 22:50:06 -0700627void clear_prefree_segments(struct f2fs_sb_info *sbi, struct cp_control *cpc)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900628{
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900629 struct list_head *head = &(SM_I(sbi)->discard_list);
Chao Yu2d7b8222014-03-29 11:33:17 +0800630 struct discard_entry *entry, *this;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900631 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Changman Lee29e59c12013-11-11 09:24:37 +0900632 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
Changman Lee29e59c12013-11-11 09:24:37 +0900633 unsigned int start = 0, end = -1;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900634
635 mutex_lock(&dirty_i->seglist_lock);
Changman Lee29e59c12013-11-11 09:24:37 +0900636
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900637 while (1) {
Changman Lee29e59c12013-11-11 09:24:37 +0900638 int i;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700639 start = find_next_bit(prefree_map, MAIN_SEGS(sbi), end + 1);
640 if (start >= MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900641 break;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700642 end = find_next_zero_bit(prefree_map, MAIN_SEGS(sbi),
643 start + 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900644
Changman Lee29e59c12013-11-11 09:24:37 +0900645 for (i = start; i < end; i++)
646 clear_bit(i, prefree_map);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900647
Changman Lee29e59c12013-11-11 09:24:37 +0900648 dirty_i->nr_dirty[PRE] -= end - start;
649
650 if (!test_opt(sbi, DISCARD))
651 continue;
652
Jaegeuk Kim37208872013-11-12 16:55:17 +0900653 f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
654 (end - start) << sbi->log_blocks_per_seg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900655 }
656 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900657
658 /* send small discards */
Chao Yu2d7b8222014-03-29 11:33:17 +0800659 list_for_each_entry_safe(entry, this, head, list) {
Jaegeuk Kim836b5a62015-04-30 22:50:06 -0700660 if (cpc->reason == CP_DISCARD && entry->len < cpc->trim_minlen)
661 goto skip;
Jaegeuk Kim37208872013-11-12 16:55:17 +0900662 f2fs_issue_discard(sbi, entry->blkaddr, entry->len);
Jaegeuk Kimf56aa1c2015-06-02 15:48:20 -0700663 cpc->trimmed += entry->len;
Jaegeuk Kim836b5a62015-04-30 22:50:06 -0700664skip:
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900665 list_del(&entry->list);
666 SM_I(sbi)->nr_discards -= entry->len;
667 kmem_cache_free(discard_entry_slab, entry);
668 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900669}
670
Chao Yu184a5cd2014-09-04 18:13:01 +0800671static bool __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900672{
673 struct sit_info *sit_i = SIT_I(sbi);
Chao Yu184a5cd2014-09-04 18:13:01 +0800674
675 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900676 sit_i->dirty_sentries++;
Chao Yu184a5cd2014-09-04 18:13:01 +0800677 return false;
678 }
679
680 return true;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900681}
682
683static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
684 unsigned int segno, int modified)
685{
686 struct seg_entry *se = get_seg_entry(sbi, segno);
687 se->type = type;
688 if (modified)
689 __mark_sit_entry_dirty(sbi, segno);
690}
691
692static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
693{
694 struct seg_entry *se;
695 unsigned int segno, offset;
696 long int new_vblocks;
697
698 segno = GET_SEGNO(sbi, blkaddr);
699
700 se = get_seg_entry(sbi, segno);
701 new_vblocks = se->valid_blocks + del;
Jaegeuk Kim491c0852014-02-04 13:01:10 +0900702 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900703
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700704 f2fs_bug_on(sbi, (new_vblocks >> (sizeof(unsigned short) << 3) ||
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900705 (new_vblocks > sbi->blocks_per_seg)));
706
707 se->valid_blocks = new_vblocks;
708 se->mtime = get_mtime(sbi);
709 SIT_I(sbi)->max_mtime = se->mtime;
710
711 /* Update valid block bitmap */
712 if (del > 0) {
Gu Zheng52aca072014-10-20 17:45:51 +0800713 if (f2fs_test_and_set_bit(offset, se->cur_valid_map))
Jaegeuk Kim05796762014-09-02 16:05:00 -0700714 f2fs_bug_on(sbi, 1);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700715 if (!f2fs_test_and_set_bit(offset, se->discard_map))
716 sbi->discard_blks--;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900717 } else {
Gu Zheng52aca072014-10-20 17:45:51 +0800718 if (!f2fs_test_and_clear_bit(offset, se->cur_valid_map))
Jaegeuk Kim05796762014-09-02 16:05:00 -0700719 f2fs_bug_on(sbi, 1);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700720 if (f2fs_test_and_clear_bit(offset, se->discard_map))
721 sbi->discard_blks++;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900722 }
723 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
724 se->ckpt_valid_blocks += del;
725
726 __mark_sit_entry_dirty(sbi, segno);
727
728 /* update total number of valid blocks to be written in ckpt area */
729 SIT_I(sbi)->written_valid_blocks += del;
730
731 if (sbi->segs_per_sec > 1)
732 get_sec_entry(sbi, segno)->valid_blocks += del;
733}
734
Jaegeuk Kim5e443812014-01-28 12:22:14 +0900735void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900736{
Jaegeuk Kim5e443812014-01-28 12:22:14 +0900737 update_sit_entry(sbi, new, 1);
738 if (GET_SEGNO(sbi, old) != NULL_SEGNO)
739 update_sit_entry(sbi, old, -1);
740
741 locate_dirty_segment(sbi, GET_SEGNO(sbi, old));
742 locate_dirty_segment(sbi, GET_SEGNO(sbi, new));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900743}
744
745void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
746{
747 unsigned int segno = GET_SEGNO(sbi, addr);
748 struct sit_info *sit_i = SIT_I(sbi);
749
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700750 f2fs_bug_on(sbi, addr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900751 if (addr == NEW_ADDR)
752 return;
753
754 /* add it into sit main buffer */
755 mutex_lock(&sit_i->sentry_lock);
756
757 update_sit_entry(sbi, addr, -1);
758
759 /* add it into dirty seglist */
760 locate_dirty_segment(sbi, segno);
761
762 mutex_unlock(&sit_i->sentry_lock);
763}
764
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900765/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900766 * This function should be resided under the curseg_mutex lock
767 */
768static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
Haicheng Lie79efe32013-06-13 16:59:27 +0800769 struct f2fs_summary *sum)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900770{
771 struct curseg_info *curseg = CURSEG_I(sbi, type);
772 void *addr = curseg->sum_blk;
Haicheng Lie79efe32013-06-13 16:59:27 +0800773 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900774 memcpy(addr, sum, sizeof(struct f2fs_summary));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900775}
776
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900777/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900778 * Calculate the number of current summary pages for writing
779 */
Chao Yu3fa06d72014-12-09 14:21:46 +0800780int npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900781{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900782 int valid_sum_count = 0;
Fan Li9a479382013-10-29 16:21:47 +0800783 int i, sum_in_page;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900784
785 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
786 if (sbi->ckpt->alloc_type[i] == SSR)
787 valid_sum_count += sbi->blocks_per_seg;
Chao Yu3fa06d72014-12-09 14:21:46 +0800788 else {
789 if (for_ra)
790 valid_sum_count += le16_to_cpu(
791 F2FS_CKPT(sbi)->cur_data_blkoff[i]);
792 else
793 valid_sum_count += curseg_blkoff(sbi, i);
794 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900795 }
796
Fan Li9a479382013-10-29 16:21:47 +0800797 sum_in_page = (PAGE_CACHE_SIZE - 2 * SUM_JOURNAL_SIZE -
798 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
799 if (valid_sum_count <= sum_in_page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900800 return 1;
Fan Li9a479382013-10-29 16:21:47 +0800801 else if ((valid_sum_count - sum_in_page) <=
802 (PAGE_CACHE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900803 return 2;
804 return 3;
805}
806
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900807/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900808 * Caller should put this summary page
809 */
810struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
811{
812 return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
813}
814
Chao Yu381722d2015-05-19 17:40:04 +0800815void update_meta_page(struct f2fs_sb_info *sbi, void *src, block_t blk_addr)
816{
817 struct page *page = grab_meta_page(sbi, blk_addr);
818 void *dst = page_address(page);
819
820 if (src)
821 memcpy(dst, src, PAGE_CACHE_SIZE);
822 else
823 memset(dst, 0, PAGE_CACHE_SIZE);
824 set_page_dirty(page);
825 f2fs_put_page(page, 1);
826}
827
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900828static void write_sum_page(struct f2fs_sb_info *sbi,
829 struct f2fs_summary_block *sum_blk, block_t blk_addr)
830{
Chao Yu381722d2015-05-19 17:40:04 +0800831 update_meta_page(sbi, (void *)sum_blk, blk_addr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900832}
833
Jaegeuk Kim60374682013-03-31 13:58:51 +0900834static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
835{
836 struct curseg_info *curseg = CURSEG_I(sbi, type);
Haicheng Li81fb5e82013-05-14 18:20:28 +0800837 unsigned int segno = curseg->segno + 1;
Jaegeuk Kim60374682013-03-31 13:58:51 +0900838 struct free_segmap_info *free_i = FREE_I(sbi);
839
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700840 if (segno < MAIN_SEGS(sbi) && segno % sbi->segs_per_sec)
Haicheng Li81fb5e82013-05-14 18:20:28 +0800841 return !test_bit(segno, free_i->free_segmap);
Jaegeuk Kim60374682013-03-31 13:58:51 +0900842 return 0;
843}
844
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900845/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900846 * Find a new segment from the free segments bitmap to right order
847 * This function should be returned with success, otherwise BUG
848 */
849static void get_new_segment(struct f2fs_sb_info *sbi,
850 unsigned int *newseg, bool new_sec, int dir)
851{
852 struct free_segmap_info *free_i = FREE_I(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900853 unsigned int segno, secno, zoneno;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700854 unsigned int total_zones = MAIN_SECS(sbi) / sbi->secs_per_zone;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900855 unsigned int hint = *newseg / sbi->segs_per_sec;
856 unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
857 unsigned int left_start = hint;
858 bool init = true;
859 int go_left = 0;
860 int i;
861
Chao Yu1a118cc2015-02-11 18:20:38 +0800862 spin_lock(&free_i->segmap_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900863
864 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
865 segno = find_next_zero_bit(free_i->free_segmap,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700866 MAIN_SEGS(sbi), *newseg + 1);
Jaegeuk Kim33afa7f2013-03-31 12:59:53 +0900867 if (segno - *newseg < sbi->segs_per_sec -
868 (*newseg % sbi->segs_per_sec))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900869 goto got_it;
870 }
871find_other_zone:
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700872 secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint);
873 if (secno >= MAIN_SECS(sbi)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900874 if (dir == ALLOC_RIGHT) {
875 secno = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700876 MAIN_SECS(sbi), 0);
877 f2fs_bug_on(sbi, secno >= MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900878 } else {
879 go_left = 1;
880 left_start = hint - 1;
881 }
882 }
883 if (go_left == 0)
884 goto skip_left;
885
886 while (test_bit(left_start, free_i->free_secmap)) {
887 if (left_start > 0) {
888 left_start--;
889 continue;
890 }
891 left_start = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700892 MAIN_SECS(sbi), 0);
893 f2fs_bug_on(sbi, left_start >= MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900894 break;
895 }
896 secno = left_start;
897skip_left:
898 hint = secno;
899 segno = secno * sbi->segs_per_sec;
900 zoneno = secno / sbi->secs_per_zone;
901
902 /* give up on finding another zone */
903 if (!init)
904 goto got_it;
905 if (sbi->secs_per_zone == 1)
906 goto got_it;
907 if (zoneno == old_zoneno)
908 goto got_it;
909 if (dir == ALLOC_LEFT) {
910 if (!go_left && zoneno + 1 >= total_zones)
911 goto got_it;
912 if (go_left && zoneno == 0)
913 goto got_it;
914 }
915 for (i = 0; i < NR_CURSEG_TYPE; i++)
916 if (CURSEG_I(sbi, i)->zone == zoneno)
917 break;
918
919 if (i < NR_CURSEG_TYPE) {
920 /* zone is in user, try another */
921 if (go_left)
922 hint = zoneno * sbi->secs_per_zone - 1;
923 else if (zoneno + 1 >= total_zones)
924 hint = 0;
925 else
926 hint = (zoneno + 1) * sbi->secs_per_zone;
927 init = false;
928 goto find_other_zone;
929 }
930got_it:
931 /* set it as dirty segment in free segmap */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700932 f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900933 __set_inuse(sbi, segno);
934 *newseg = segno;
Chao Yu1a118cc2015-02-11 18:20:38 +0800935 spin_unlock(&free_i->segmap_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900936}
937
938static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
939{
940 struct curseg_info *curseg = CURSEG_I(sbi, type);
941 struct summary_footer *sum_footer;
942
943 curseg->segno = curseg->next_segno;
944 curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
945 curseg->next_blkoff = 0;
946 curseg->next_segno = NULL_SEGNO;
947
948 sum_footer = &(curseg->sum_blk->footer);
949 memset(sum_footer, 0, sizeof(struct summary_footer));
950 if (IS_DATASEG(type))
951 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
952 if (IS_NODESEG(type))
953 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
954 __set_sit_entry_type(sbi, type, curseg->segno, modified);
955}
956
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900957/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900958 * Allocate a current working segment.
959 * This function always allocates a free segment in LFS manner.
960 */
961static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
962{
963 struct curseg_info *curseg = CURSEG_I(sbi, type);
964 unsigned int segno = curseg->segno;
965 int dir = ALLOC_LEFT;
966
967 write_sum_page(sbi, curseg->sum_blk,
Haicheng Li81fb5e82013-05-14 18:20:28 +0800968 GET_SUM_BLOCK(sbi, segno));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900969 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
970 dir = ALLOC_RIGHT;
971
972 if (test_opt(sbi, NOHEAP))
973 dir = ALLOC_RIGHT;
974
975 get_new_segment(sbi, &segno, new_sec, dir);
976 curseg->next_segno = segno;
977 reset_curseg(sbi, type, 1);
978 curseg->alloc_type = LFS;
979}
980
981static void __next_free_blkoff(struct f2fs_sb_info *sbi,
982 struct curseg_info *seg, block_t start)
983{
984 struct seg_entry *se = get_seg_entry(sbi, seg->segno);
Changman Leee81c93c2013-11-15 13:21:16 +0900985 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
Jaegeuk Kim60a3b782015-02-10 16:44:29 -0800986 unsigned long *target_map = SIT_I(sbi)->tmp_map;
Changman Leee81c93c2013-11-15 13:21:16 +0900987 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
988 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
989 int i, pos;
990
991 for (i = 0; i < entries; i++)
992 target_map[i] = ckpt_map[i] | cur_map[i];
993
994 pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
995
996 seg->next_blkoff = pos;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900997}
998
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900999/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001000 * If a segment is written by LFS manner, next block offset is just obtained
1001 * by increasing the current block offset. However, if a segment is written by
1002 * SSR manner, next block offset obtained by calling __next_free_blkoff
1003 */
1004static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
1005 struct curseg_info *seg)
1006{
1007 if (seg->alloc_type == SSR)
1008 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
1009 else
1010 seg->next_blkoff++;
1011}
1012
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001013/*
arter97e1c42042014-08-06 23:22:50 +09001014 * This function always allocates a used segment(from dirty seglist) by SSR
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001015 * manner, so it should recover the existing segment information of valid blocks
1016 */
1017static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
1018{
1019 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1020 struct curseg_info *curseg = CURSEG_I(sbi, type);
1021 unsigned int new_segno = curseg->next_segno;
1022 struct f2fs_summary_block *sum_node;
1023 struct page *sum_page;
1024
1025 write_sum_page(sbi, curseg->sum_blk,
1026 GET_SUM_BLOCK(sbi, curseg->segno));
1027 __set_test_and_inuse(sbi, new_segno);
1028
1029 mutex_lock(&dirty_i->seglist_lock);
1030 __remove_dirty_segment(sbi, new_segno, PRE);
1031 __remove_dirty_segment(sbi, new_segno, DIRTY);
1032 mutex_unlock(&dirty_i->seglist_lock);
1033
1034 reset_curseg(sbi, type, 1);
1035 curseg->alloc_type = SSR;
1036 __next_free_blkoff(sbi, curseg, 0);
1037
1038 if (reuse) {
1039 sum_page = get_sum_page(sbi, new_segno);
1040 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
1041 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
1042 f2fs_put_page(sum_page, 1);
1043 }
1044}
1045
Jaegeuk Kim43727522013-02-04 15:11:17 +09001046static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
1047{
1048 struct curseg_info *curseg = CURSEG_I(sbi, type);
1049 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
1050
1051 if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0))
1052 return v_ops->get_victim(sbi,
1053 &(curseg)->next_segno, BG_GC, type, SSR);
1054
1055 /* For data segments, let's do SSR more intensively */
1056 for (; type >= CURSEG_HOT_DATA; type--)
1057 if (v_ops->get_victim(sbi, &(curseg)->next_segno,
1058 BG_GC, type, SSR))
1059 return 1;
1060 return 0;
1061}
1062
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001063/*
1064 * flush out current segment and replace it with new segment
1065 * This function should be returned with success, otherwise BUG
1066 */
1067static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
1068 int type, bool force)
1069{
1070 struct curseg_info *curseg = CURSEG_I(sbi, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001071
Gu Zheng7b405272013-08-19 09:41:15 +08001072 if (force)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001073 new_curseg(sbi, type, true);
Gu Zheng7b405272013-08-19 09:41:15 +08001074 else if (type == CURSEG_WARM_NODE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001075 new_curseg(sbi, type, false);
Jaegeuk Kim60374682013-03-31 13:58:51 +09001076 else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
1077 new_curseg(sbi, type, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001078 else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
1079 change_curseg(sbi, type, true);
1080 else
1081 new_curseg(sbi, type, false);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +09001082
1083 stat_inc_seg_type(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001084}
1085
Jaegeuk Kim38aa0882015-01-05 16:02:20 -08001086static void __allocate_new_segments(struct f2fs_sb_info *sbi, int type)
1087{
1088 struct curseg_info *curseg = CURSEG_I(sbi, type);
1089 unsigned int old_segno;
1090
1091 old_segno = curseg->segno;
1092 SIT_I(sbi)->s_ops->allocate_segment(sbi, type, true);
1093 locate_dirty_segment(sbi, old_segno);
1094}
1095
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001096void allocate_new_segments(struct f2fs_sb_info *sbi)
1097{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001098 int i;
1099
Jaegeuk Kim38aa0882015-01-05 16:02:20 -08001100 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++)
1101 __allocate_new_segments(sbi, i);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001102}
1103
1104static const struct segment_allocation default_salloc_ops = {
1105 .allocate_segment = allocate_segment_by_default,
1106};
1107
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001108int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range)
1109{
Jaegeuk Kimf7ef9b82015-02-09 12:02:44 -08001110 __u64 start = F2FS_BYTES_TO_BLK(range->start);
1111 __u64 end = start + F2FS_BYTES_TO_BLK(range->len) - 1;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001112 unsigned int start_segno, end_segno;
1113 struct cp_control cpc;
1114
Jaegeuk Kim836b5a62015-04-30 22:50:06 -07001115 if (start >= MAX_BLKADDR(sbi) || range->len < sbi->blocksize)
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001116 return -EINVAL;
1117
Jan Kara9bd27ae2014-10-21 14:07:33 +02001118 cpc.trimmed = 0;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001119 if (end <= MAIN_BLKADDR(sbi))
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001120 goto out;
1121
1122 /* start/end segment number in main_area */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001123 start_segno = (start <= MAIN_BLKADDR(sbi)) ? 0 : GET_SEGNO(sbi, start);
1124 end_segno = (end >= MAX_BLKADDR(sbi)) ? MAIN_SEGS(sbi) - 1 :
1125 GET_SEGNO(sbi, end);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001126 cpc.reason = CP_DISCARD;
Jaegeuk Kim836b5a62015-04-30 22:50:06 -07001127 cpc.trim_minlen = max_t(__u64, 1, F2FS_BYTES_TO_BLK(range->minlen));
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001128
1129 /* do checkpoint to issue discard commands safely */
Jaegeuk Kimbba681c2015-01-26 17:41:23 -08001130 for (; start_segno <= end_segno; start_segno = cpc.trim_end + 1) {
1131 cpc.trim_start = start_segno;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001132
1133 if (sbi->discard_blks == 0)
1134 break;
1135 else if (sbi->discard_blks < BATCHED_TRIM_BLOCKS(sbi))
1136 cpc.trim_end = end_segno;
1137 else
1138 cpc.trim_end = min_t(unsigned int,
1139 rounddown(start_segno +
Jaegeuk Kimbba681c2015-01-26 17:41:23 -08001140 BATCHED_TRIM_SEGMENTS(sbi),
1141 sbi->segs_per_sec) - 1, end_segno);
1142
1143 mutex_lock(&sbi->gc_mutex);
1144 write_checkpoint(sbi, &cpc);
1145 mutex_unlock(&sbi->gc_mutex);
1146 }
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001147out:
Jaegeuk Kimf7ef9b82015-02-09 12:02:44 -08001148 range->len = F2FS_BLK_TO_BYTES(cpc.trimmed);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001149 return 0;
1150}
1151
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001152static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
1153{
1154 struct curseg_info *curseg = CURSEG_I(sbi, type);
1155 if (curseg->next_blkoff < sbi->blocks_per_seg)
1156 return true;
1157 return false;
1158}
1159
1160static int __get_segment_type_2(struct page *page, enum page_type p_type)
1161{
1162 if (p_type == DATA)
1163 return CURSEG_HOT_DATA;
1164 else
1165 return CURSEG_HOT_NODE;
1166}
1167
1168static int __get_segment_type_4(struct page *page, enum page_type p_type)
1169{
1170 if (p_type == DATA) {
1171 struct inode *inode = page->mapping->host;
1172
1173 if (S_ISDIR(inode->i_mode))
1174 return CURSEG_HOT_DATA;
1175 else
1176 return CURSEG_COLD_DATA;
1177 } else {
Jaegeuk Kima344b9f2014-11-05 20:05:53 -08001178 if (IS_DNODE(page) && is_cold_node(page))
1179 return CURSEG_WARM_NODE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001180 else
1181 return CURSEG_COLD_NODE;
1182 }
1183}
1184
1185static int __get_segment_type_6(struct page *page, enum page_type p_type)
1186{
1187 if (p_type == DATA) {
1188 struct inode *inode = page->mapping->host;
1189
1190 if (S_ISDIR(inode->i_mode))
1191 return CURSEG_HOT_DATA;
Jaegeuk Kim354a3392013-06-14 08:52:35 +09001192 else if (is_cold_data(page) || file_is_cold(inode))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001193 return CURSEG_COLD_DATA;
1194 else
1195 return CURSEG_WARM_DATA;
1196 } else {
1197 if (IS_DNODE(page))
1198 return is_cold_node(page) ? CURSEG_WARM_NODE :
1199 CURSEG_HOT_NODE;
1200 else
1201 return CURSEG_COLD_NODE;
1202 }
1203}
1204
1205static int __get_segment_type(struct page *page, enum page_type p_type)
1206{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001207 switch (F2FS_P_SB(page)->active_logs) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001208 case 2:
1209 return __get_segment_type_2(page, p_type);
1210 case 4:
1211 return __get_segment_type_4(page, p_type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001212 }
Jaegeuk Kim12a67142012-12-21 11:47:05 +09001213 /* NR_CURSEG_TYPE(6) logs by default */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001214 f2fs_bug_on(F2FS_P_SB(page),
1215 F2FS_P_SB(page)->active_logs != NR_CURSEG_TYPE);
Jaegeuk Kim12a67142012-12-21 11:47:05 +09001216 return __get_segment_type_6(page, p_type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001217}
1218
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001219void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
1220 block_t old_blkaddr, block_t *new_blkaddr,
1221 struct f2fs_summary *sum, int type)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001222{
1223 struct sit_info *sit_i = SIT_I(sbi);
1224 struct curseg_info *curseg;
Jaegeuk Kim38aa0882015-01-05 16:02:20 -08001225 bool direct_io = (type == CURSEG_DIRECT_IO);
1226
1227 type = direct_io ? CURSEG_WARM_DATA : type;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001228
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001229 curseg = CURSEG_I(sbi, type);
1230
1231 mutex_lock(&curseg->curseg_mutex);
Jaegeuk Kim21cb1d92015-03-11 13:42:48 -04001232 mutex_lock(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001233
Jaegeuk Kim38aa0882015-01-05 16:02:20 -08001234 /* direct_io'ed data is aligned to the segment for better performance */
1235 if (direct_io && curseg->next_blkoff)
1236 __allocate_new_segments(sbi, type);
1237
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001238 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001239
1240 /*
1241 * __add_sum_entry should be resided under the curseg_mutex
1242 * because, this function updates a summary entry in the
1243 * current summary block.
1244 */
Haicheng Lie79efe32013-06-13 16:59:27 +08001245 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001246
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001247 __refresh_next_blkoff(sbi, curseg);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +09001248
1249 stat_inc_block_count(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001250
Jaegeuk Kim5e443812014-01-28 12:22:14 +09001251 if (!__has_curseg_space(sbi, type))
1252 sit_i->s_ops->allocate_segment(sbi, type, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001253 /*
1254 * SIT information should be updated before segment allocation,
1255 * since SSR needs latest valid block information.
1256 */
1257 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
Jaegeuk Kim5e443812014-01-28 12:22:14 +09001258
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001259 mutex_unlock(&sit_i->sentry_lock);
1260
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001261 if (page && IS_NODESEG(type))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001262 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
1263
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001264 mutex_unlock(&curseg->curseg_mutex);
1265}
1266
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001267static void do_write_page(struct f2fs_summary *sum, struct f2fs_io_info *fio)
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001268{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001269 int type = __get_segment_type(fio->page, fio->type);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001270
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001271 allocate_data_block(fio->sbi, fio->page, fio->blk_addr,
1272 &fio->blk_addr, sum, type);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001273
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001274 /* writeout dirty page into bdev */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001275 f2fs_submit_page_mbio(fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001276}
1277
Jaegeuk Kim577e3492013-01-24 19:56:11 +09001278void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001279{
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001280 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001281 .sbi = sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001282 .type = META,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001283 .rw = WRITE_SYNC | REQ_META | REQ_PRIO,
1284 .blk_addr = page->index,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001285 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001286 .encrypted_page = NULL,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001287 };
1288
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001289 set_page_writeback(page);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001290 f2fs_submit_page_mbio(&fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001291}
1292
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001293void write_node_page(unsigned int nid, struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001294{
1295 struct f2fs_summary sum;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001296
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001297 set_summary(&sum, nid, 0, 0);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001298 do_write_page(&sum, fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001299}
1300
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001301void write_data_page(struct dnode_of_data *dn, struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001302{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001303 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001304 struct f2fs_summary sum;
1305 struct node_info ni;
1306
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001307 f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001308 get_node_info(sbi, dn->nid, &ni);
1309 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001310 do_write_page(&sum, fio);
Jaegeuk Kime1509cf2014-12-30 22:57:55 -08001311 dn->data_blkaddr = fio->blk_addr;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001312}
1313
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001314void rewrite_data_page(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001315{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001316 stat_inc_inplace_blocks(fio->sbi);
1317 f2fs_submit_page_mbio(fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001318}
1319
Chao Yu528e3452015-05-28 19:15:35 +08001320static void __f2fs_replace_block(struct f2fs_sb_info *sbi,
1321 struct f2fs_summary *sum,
Chao Yu19f106b2015-05-06 13:08:06 +08001322 block_t old_blkaddr, block_t new_blkaddr,
1323 bool recover_curseg)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001324{
1325 struct sit_info *sit_i = SIT_I(sbi);
1326 struct curseg_info *curseg;
1327 unsigned int segno, old_cursegno;
1328 struct seg_entry *se;
1329 int type;
Chao Yu19f106b2015-05-06 13:08:06 +08001330 unsigned short old_blkoff;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001331
1332 segno = GET_SEGNO(sbi, new_blkaddr);
1333 se = get_seg_entry(sbi, segno);
1334 type = se->type;
1335
Chao Yu19f106b2015-05-06 13:08:06 +08001336 if (!recover_curseg) {
1337 /* for recovery flow */
1338 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
1339 if (old_blkaddr == NULL_ADDR)
1340 type = CURSEG_COLD_DATA;
1341 else
1342 type = CURSEG_WARM_DATA;
1343 }
1344 } else {
1345 if (!IS_CURSEG(sbi, segno))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001346 type = CURSEG_WARM_DATA;
1347 }
Chao Yu19f106b2015-05-06 13:08:06 +08001348
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001349 curseg = CURSEG_I(sbi, type);
1350
1351 mutex_lock(&curseg->curseg_mutex);
1352 mutex_lock(&sit_i->sentry_lock);
1353
1354 old_cursegno = curseg->segno;
Chao Yu19f106b2015-05-06 13:08:06 +08001355 old_blkoff = curseg->next_blkoff;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001356
1357 /* change the current segment */
1358 if (segno != curseg->segno) {
1359 curseg->next_segno = segno;
1360 change_curseg(sbi, type, true);
1361 }
1362
Jaegeuk Kim491c0852014-02-04 13:01:10 +09001363 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
Haicheng Lie79efe32013-06-13 16:59:27 +08001364 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001365
1366 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001367 locate_dirty_segment(sbi, old_cursegno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001368
Chao Yu19f106b2015-05-06 13:08:06 +08001369 if (recover_curseg) {
1370 if (old_cursegno != curseg->segno) {
1371 curseg->next_segno = old_cursegno;
1372 change_curseg(sbi, type, true);
1373 }
1374 curseg->next_blkoff = old_blkoff;
1375 }
1376
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001377 mutex_unlock(&sit_i->sentry_lock);
1378 mutex_unlock(&curseg->curseg_mutex);
1379}
1380
Chao Yu528e3452015-05-28 19:15:35 +08001381void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
1382 block_t old_addr, block_t new_addr,
1383 unsigned char version, bool recover_curseg)
1384{
1385 struct f2fs_summary sum;
1386
1387 set_summary(&sum, dn->nid, dn->ofs_in_node, version);
1388
1389 __f2fs_replace_block(sbi, &sum, old_addr, new_addr, recover_curseg);
1390
1391 dn->data_blkaddr = new_addr;
1392 set_data_blkaddr(dn);
1393 f2fs_update_extent_cache(dn);
1394}
1395
Chao Yudf0f8dc2014-03-22 14:57:23 +08001396static inline bool is_merged_page(struct f2fs_sb_info *sbi,
1397 struct page *page, enum page_type type)
1398{
1399 enum page_type btype = PAGE_TYPE_OF_BIO(type);
1400 struct f2fs_bio_info *io = &sbi->write_io[btype];
Chao Yudf0f8dc2014-03-22 14:57:23 +08001401 struct bio_vec *bvec;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001402 struct page *target;
Chao Yudf0f8dc2014-03-22 14:57:23 +08001403 int i;
1404
1405 down_read(&io->io_rwsem);
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001406 if (!io->bio) {
1407 up_read(&io->io_rwsem);
1408 return false;
1409 }
Chao Yudf0f8dc2014-03-22 14:57:23 +08001410
Jaegeuk Kimce234472014-04-02 09:04:42 +09001411 bio_for_each_segment_all(bvec, io->bio, i) {
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001412
1413 if (bvec->bv_page->mapping) {
1414 target = bvec->bv_page;
1415 } else {
1416 struct f2fs_crypto_ctx *ctx;
1417
1418 /* encrypted page */
1419 ctx = (struct f2fs_crypto_ctx *)page_private(
1420 bvec->bv_page);
Jaegeuk Kimca40b032015-05-12 13:40:20 -07001421 target = ctx->w.control_page;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001422 }
1423
1424 if (page == target) {
Chao Yudf0f8dc2014-03-22 14:57:23 +08001425 up_read(&io->io_rwsem);
1426 return true;
1427 }
1428 }
1429
Chao Yudf0f8dc2014-03-22 14:57:23 +08001430 up_read(&io->io_rwsem);
1431 return false;
1432}
1433
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001434void f2fs_wait_on_page_writeback(struct page *page,
Yuan Zhong5514f0a2014-01-10 07:26:14 +00001435 enum page_type type)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001436{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001437 if (PageWriteback(page)) {
Jaegeuk Kim40813632014-09-02 15:31:18 -07001438 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
1439
Chao Yudf0f8dc2014-03-22 14:57:23 +08001440 if (is_merged_page(sbi, page, type))
1441 f2fs_submit_merged_bio(sbi, type, WRITE);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001442 wait_on_page_writeback(page);
1443 }
1444}
1445
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001446static int read_compacted_summaries(struct f2fs_sb_info *sbi)
1447{
1448 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1449 struct curseg_info *seg_i;
1450 unsigned char *kaddr;
1451 struct page *page;
1452 block_t start;
1453 int i, j, offset;
1454
1455 start = start_sum_block(sbi);
1456
1457 page = get_meta_page(sbi, start++);
1458 kaddr = (unsigned char *)page_address(page);
1459
1460 /* Step 1: restore nat cache */
1461 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1462 memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
1463
1464 /* Step 2: restore sit cache */
1465 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1466 memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
1467 SUM_JOURNAL_SIZE);
1468 offset = 2 * SUM_JOURNAL_SIZE;
1469
1470 /* Step 3: restore summary entries */
1471 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1472 unsigned short blk_off;
1473 unsigned int segno;
1474
1475 seg_i = CURSEG_I(sbi, i);
1476 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
1477 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
1478 seg_i->next_segno = segno;
1479 reset_curseg(sbi, i, 0);
1480 seg_i->alloc_type = ckpt->alloc_type[i];
1481 seg_i->next_blkoff = blk_off;
1482
1483 if (seg_i->alloc_type == SSR)
1484 blk_off = sbi->blocks_per_seg;
1485
1486 for (j = 0; j < blk_off; j++) {
1487 struct f2fs_summary *s;
1488 s = (struct f2fs_summary *)(kaddr + offset);
1489 seg_i->sum_blk->entries[j] = *s;
1490 offset += SUMMARY_SIZE;
1491 if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1492 SUM_FOOTER_SIZE)
1493 continue;
1494
1495 f2fs_put_page(page, 1);
1496 page = NULL;
1497
1498 page = get_meta_page(sbi, start++);
1499 kaddr = (unsigned char *)page_address(page);
1500 offset = 0;
1501 }
1502 }
1503 f2fs_put_page(page, 1);
1504 return 0;
1505}
1506
1507static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
1508{
1509 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1510 struct f2fs_summary_block *sum;
1511 struct curseg_info *curseg;
1512 struct page *new;
1513 unsigned short blk_off;
1514 unsigned int segno = 0;
1515 block_t blk_addr = 0;
1516
1517 /* get segment number and block addr */
1518 if (IS_DATASEG(type)) {
1519 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
1520 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
1521 CURSEG_HOT_DATA]);
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001522 if (__exist_node_summaries(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001523 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
1524 else
1525 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
1526 } else {
1527 segno = le32_to_cpu(ckpt->cur_node_segno[type -
1528 CURSEG_HOT_NODE]);
1529 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
1530 CURSEG_HOT_NODE]);
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001531 if (__exist_node_summaries(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001532 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
1533 type - CURSEG_HOT_NODE);
1534 else
1535 blk_addr = GET_SUM_BLOCK(sbi, segno);
1536 }
1537
1538 new = get_meta_page(sbi, blk_addr);
1539 sum = (struct f2fs_summary_block *)page_address(new);
1540
1541 if (IS_NODESEG(type)) {
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001542 if (__exist_node_summaries(sbi)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001543 struct f2fs_summary *ns = &sum->entries[0];
1544 int i;
1545 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
1546 ns->version = 0;
1547 ns->ofs_in_node = 0;
1548 }
1549 } else {
Gu Zhengd6537882014-03-07 18:43:36 +08001550 int err;
1551
1552 err = restore_node_summary(sbi, segno, sum);
1553 if (err) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001554 f2fs_put_page(new, 1);
Gu Zhengd6537882014-03-07 18:43:36 +08001555 return err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001556 }
1557 }
1558 }
1559
1560 /* set uncompleted segment to curseg */
1561 curseg = CURSEG_I(sbi, type);
1562 mutex_lock(&curseg->curseg_mutex);
1563 memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
1564 curseg->next_segno = segno;
1565 reset_curseg(sbi, type, 0);
1566 curseg->alloc_type = ckpt->alloc_type[type];
1567 curseg->next_blkoff = blk_off;
1568 mutex_unlock(&curseg->curseg_mutex);
1569 f2fs_put_page(new, 1);
1570 return 0;
1571}
1572
1573static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
1574{
1575 int type = CURSEG_HOT_DATA;
Chao Yue4fc5fb2014-03-17 16:36:24 +08001576 int err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001577
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001578 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
Chao Yu3fa06d72014-12-09 14:21:46 +08001579 int npages = npages_for_summary_flush(sbi, true);
1580
1581 if (npages >= 2)
1582 ra_meta_pages(sbi, start_sum_block(sbi), npages,
1583 META_CP);
1584
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001585 /* restore for compacted data summary */
1586 if (read_compacted_summaries(sbi))
1587 return -EINVAL;
1588 type = CURSEG_HOT_NODE;
1589 }
1590
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001591 if (__exist_node_summaries(sbi))
Chao Yu3fa06d72014-12-09 14:21:46 +08001592 ra_meta_pages(sbi, sum_blk_addr(sbi, NR_CURSEG_TYPE, type),
1593 NR_CURSEG_TYPE - type, META_CP);
1594
Chao Yue4fc5fb2014-03-17 16:36:24 +08001595 for (; type <= CURSEG_COLD_NODE; type++) {
1596 err = read_normal_summaries(sbi, type);
1597 if (err)
1598 return err;
1599 }
1600
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001601 return 0;
1602}
1603
1604static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
1605{
1606 struct page *page;
1607 unsigned char *kaddr;
1608 struct f2fs_summary *summary;
1609 struct curseg_info *seg_i;
1610 int written_size = 0;
1611 int i, j;
1612
1613 page = grab_meta_page(sbi, blkaddr++);
1614 kaddr = (unsigned char *)page_address(page);
1615
1616 /* Step 1: write nat cache */
1617 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1618 memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
1619 written_size += SUM_JOURNAL_SIZE;
1620
1621 /* Step 2: write sit cache */
1622 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1623 memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
1624 SUM_JOURNAL_SIZE);
1625 written_size += SUM_JOURNAL_SIZE;
1626
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001627 /* Step 3: write summary entries */
1628 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1629 unsigned short blkoff;
1630 seg_i = CURSEG_I(sbi, i);
1631 if (sbi->ckpt->alloc_type[i] == SSR)
1632 blkoff = sbi->blocks_per_seg;
1633 else
1634 blkoff = curseg_blkoff(sbi, i);
1635
1636 for (j = 0; j < blkoff; j++) {
1637 if (!page) {
1638 page = grab_meta_page(sbi, blkaddr++);
1639 kaddr = (unsigned char *)page_address(page);
1640 written_size = 0;
1641 }
1642 summary = (struct f2fs_summary *)(kaddr + written_size);
1643 *summary = seg_i->sum_blk->entries[j];
1644 written_size += SUMMARY_SIZE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001645
1646 if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1647 SUM_FOOTER_SIZE)
1648 continue;
1649
Chao Yue8d61a72013-10-24 15:08:28 +08001650 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001651 f2fs_put_page(page, 1);
1652 page = NULL;
1653 }
1654 }
Chao Yue8d61a72013-10-24 15:08:28 +08001655 if (page) {
1656 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001657 f2fs_put_page(page, 1);
Chao Yue8d61a72013-10-24 15:08:28 +08001658 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001659}
1660
1661static void write_normal_summaries(struct f2fs_sb_info *sbi,
1662 block_t blkaddr, int type)
1663{
1664 int i, end;
1665 if (IS_DATASEG(type))
1666 end = type + NR_CURSEG_DATA_TYPE;
1667 else
1668 end = type + NR_CURSEG_NODE_TYPE;
1669
1670 for (i = type; i < end; i++) {
1671 struct curseg_info *sum = CURSEG_I(sbi, i);
1672 mutex_lock(&sum->curseg_mutex);
1673 write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
1674 mutex_unlock(&sum->curseg_mutex);
1675 }
1676}
1677
1678void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1679{
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001680 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001681 write_compacted_summaries(sbi, start_blk);
1682 else
1683 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
1684}
1685
1686void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1687{
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001688 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001689}
1690
1691int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
1692 unsigned int val, int alloc)
1693{
1694 int i;
1695
1696 if (type == NAT_JOURNAL) {
1697 for (i = 0; i < nats_in_cursum(sum); i++) {
1698 if (le32_to_cpu(nid_in_journal(sum, i)) == val)
1699 return i;
1700 }
1701 if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
1702 return update_nats_in_cursum(sum, 1);
1703 } else if (type == SIT_JOURNAL) {
1704 for (i = 0; i < sits_in_cursum(sum); i++)
1705 if (le32_to_cpu(segno_in_journal(sum, i)) == val)
1706 return i;
1707 if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
1708 return update_sits_in_cursum(sum, 1);
1709 }
1710 return -1;
1711}
1712
1713static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
1714 unsigned int segno)
1715{
Gu Zheng2cc22182014-10-20 17:45:49 +08001716 return get_meta_page(sbi, current_sit_addr(sbi, segno));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001717}
1718
1719static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
1720 unsigned int start)
1721{
1722 struct sit_info *sit_i = SIT_I(sbi);
1723 struct page *src_page, *dst_page;
1724 pgoff_t src_off, dst_off;
1725 void *src_addr, *dst_addr;
1726
1727 src_off = current_sit_addr(sbi, start);
1728 dst_off = next_sit_addr(sbi, src_off);
1729
1730 /* get current sit block page without lock */
1731 src_page = get_meta_page(sbi, src_off);
1732 dst_page = grab_meta_page(sbi, dst_off);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001733 f2fs_bug_on(sbi, PageDirty(src_page));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001734
1735 src_addr = page_address(src_page);
1736 dst_addr = page_address(dst_page);
1737 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
1738
1739 set_page_dirty(dst_page);
1740 f2fs_put_page(src_page, 1);
1741
1742 set_to_next_sit(sit_i, start);
1743
1744 return dst_page;
1745}
1746
Chao Yu184a5cd2014-09-04 18:13:01 +08001747static struct sit_entry_set *grab_sit_entry_set(void)
1748{
1749 struct sit_entry_set *ses =
1750 f2fs_kmem_cache_alloc(sit_entry_set_slab, GFP_ATOMIC);
1751
1752 ses->entry_cnt = 0;
1753 INIT_LIST_HEAD(&ses->set_list);
1754 return ses;
1755}
1756
1757static void release_sit_entry_set(struct sit_entry_set *ses)
1758{
1759 list_del(&ses->set_list);
1760 kmem_cache_free(sit_entry_set_slab, ses);
1761}
1762
1763static void adjust_sit_entry_set(struct sit_entry_set *ses,
1764 struct list_head *head)
1765{
1766 struct sit_entry_set *next = ses;
1767
1768 if (list_is_last(&ses->set_list, head))
1769 return;
1770
1771 list_for_each_entry_continue(next, head, set_list)
1772 if (ses->entry_cnt <= next->entry_cnt)
1773 break;
1774
1775 list_move_tail(&ses->set_list, &next->set_list);
1776}
1777
1778static void add_sit_entry(unsigned int segno, struct list_head *head)
1779{
1780 struct sit_entry_set *ses;
1781 unsigned int start_segno = START_SEGNO(segno);
1782
1783 list_for_each_entry(ses, head, set_list) {
1784 if (ses->start_segno == start_segno) {
1785 ses->entry_cnt++;
1786 adjust_sit_entry_set(ses, head);
1787 return;
1788 }
1789 }
1790
1791 ses = grab_sit_entry_set();
1792
1793 ses->start_segno = start_segno;
1794 ses->entry_cnt++;
1795 list_add(&ses->set_list, head);
1796}
1797
1798static void add_sits_in_set(struct f2fs_sb_info *sbi)
1799{
1800 struct f2fs_sm_info *sm_info = SM_I(sbi);
1801 struct list_head *set_list = &sm_info->sit_entry_set;
1802 unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap;
Chao Yu184a5cd2014-09-04 18:13:01 +08001803 unsigned int segno;
1804
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001805 for_each_set_bit(segno, bitmap, MAIN_SEGS(sbi))
Chao Yu184a5cd2014-09-04 18:13:01 +08001806 add_sit_entry(segno, set_list);
1807}
1808
1809static void remove_sits_in_journal(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001810{
1811 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1812 struct f2fs_summary_block *sum = curseg->sum_blk;
1813 int i;
1814
Chao Yu184a5cd2014-09-04 18:13:01 +08001815 for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
1816 unsigned int segno;
1817 bool dirtied;
1818
1819 segno = le32_to_cpu(segno_in_journal(sum, i));
1820 dirtied = __mark_sit_entry_dirty(sbi, segno);
1821
1822 if (!dirtied)
1823 add_sit_entry(segno, &SM_I(sbi)->sit_entry_set);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001824 }
Chao Yu184a5cd2014-09-04 18:13:01 +08001825 update_sits_in_cursum(sum, -sits_in_cursum(sum));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001826}
1827
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001828/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001829 * CP calls this function, which flushes SIT entries including sit_journal,
1830 * and moves prefree segs to free segs.
1831 */
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001832void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001833{
1834 struct sit_info *sit_i = SIT_I(sbi);
1835 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
1836 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1837 struct f2fs_summary_block *sum = curseg->sum_blk;
Chao Yu184a5cd2014-09-04 18:13:01 +08001838 struct sit_entry_set *ses, *tmp;
1839 struct list_head *head = &SM_I(sbi)->sit_entry_set;
Chao Yu184a5cd2014-09-04 18:13:01 +08001840 bool to_journal = true;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001841 struct seg_entry *se;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001842
1843 mutex_lock(&curseg->curseg_mutex);
1844 mutex_lock(&sit_i->sentry_lock);
1845
Wanpeng Li2b11a742015-02-27 16:52:50 +08001846 if (!sit_i->dirty_sentries)
1847 goto out;
1848
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001849 /*
Chao Yu184a5cd2014-09-04 18:13:01 +08001850 * add and account sit entries of dirty bitmap in sit entry
1851 * set temporarily
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001852 */
Chao Yu184a5cd2014-09-04 18:13:01 +08001853 add_sits_in_set(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001854
Chao Yu184a5cd2014-09-04 18:13:01 +08001855 /*
1856 * if there are no enough space in journal to store dirty sit
1857 * entries, remove all entries from journal and add and account
1858 * them in sit entry set.
1859 */
1860 if (!__has_cursum_space(sum, sit_i->dirty_sentries, SIT_JOURNAL))
1861 remove_sits_in_journal(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001862
Chao Yu184a5cd2014-09-04 18:13:01 +08001863 /*
1864 * there are two steps to flush sit entries:
1865 * #1, flush sit entries to journal in current cold data summary block.
1866 * #2, flush sit entries to sit page.
1867 */
1868 list_for_each_entry_safe(ses, tmp, head, set_list) {
Jaegeuk Kim4a257ed2014-10-16 11:43:30 -07001869 struct page *page = NULL;
Chao Yu184a5cd2014-09-04 18:13:01 +08001870 struct f2fs_sit_block *raw_sit = NULL;
1871 unsigned int start_segno = ses->start_segno;
1872 unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001873 (unsigned long)MAIN_SEGS(sbi));
Chao Yu184a5cd2014-09-04 18:13:01 +08001874 unsigned int segno = start_segno;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001875
Chao Yu184a5cd2014-09-04 18:13:01 +08001876 if (to_journal &&
1877 !__has_cursum_space(sum, ses->entry_cnt, SIT_JOURNAL))
1878 to_journal = false;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001879
Chao Yu184a5cd2014-09-04 18:13:01 +08001880 if (!to_journal) {
1881 page = get_next_sit_page(sbi, start_segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001882 raw_sit = page_address(page);
1883 }
1884
Chao Yu184a5cd2014-09-04 18:13:01 +08001885 /* flush dirty sit entries in region of current sit set */
1886 for_each_set_bit_from(segno, bitmap, end) {
1887 int offset, sit_offset;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001888
1889 se = get_seg_entry(sbi, segno);
Chao Yu184a5cd2014-09-04 18:13:01 +08001890
1891 /* add discard candidates */
Jaegeuk Kimd7bc2482014-12-12 13:53:41 -08001892 if (cpc->reason != CP_DISCARD) {
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001893 cpc->trim_start = segno;
1894 add_discard_addrs(sbi, cpc);
1895 }
Chao Yu184a5cd2014-09-04 18:13:01 +08001896
1897 if (to_journal) {
1898 offset = lookup_journal_in_cursum(sum,
1899 SIT_JOURNAL, segno, 1);
1900 f2fs_bug_on(sbi, offset < 0);
1901 segno_in_journal(sum, offset) =
1902 cpu_to_le32(segno);
1903 seg_info_to_raw_sit(se,
1904 &sit_in_journal(sum, offset));
1905 } else {
1906 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
1907 seg_info_to_raw_sit(se,
1908 &raw_sit->entries[sit_offset]);
1909 }
1910
1911 __clear_bit(segno, bitmap);
1912 sit_i->dirty_sentries--;
1913 ses->entry_cnt--;
1914 }
1915
1916 if (!to_journal)
1917 f2fs_put_page(page, 1);
1918
1919 f2fs_bug_on(sbi, ses->entry_cnt);
1920 release_sit_entry_set(ses);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001921 }
Chao Yu184a5cd2014-09-04 18:13:01 +08001922
1923 f2fs_bug_on(sbi, !list_empty(head));
1924 f2fs_bug_on(sbi, sit_i->dirty_sentries);
Chao Yu184a5cd2014-09-04 18:13:01 +08001925out:
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001926 if (cpc->reason == CP_DISCARD) {
1927 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++)
1928 add_discard_addrs(sbi, cpc);
1929 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001930 mutex_unlock(&sit_i->sentry_lock);
1931 mutex_unlock(&curseg->curseg_mutex);
1932
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001933 set_prefree_as_free_segments(sbi);
1934}
1935
1936static int build_sit_info(struct f2fs_sb_info *sbi)
1937{
1938 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1939 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1940 struct sit_info *sit_i;
1941 unsigned int sit_segs, start;
1942 char *src_bitmap, *dst_bitmap;
1943 unsigned int bitmap_size;
1944
1945 /* allocate memory for SIT information */
1946 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
1947 if (!sit_i)
1948 return -ENOMEM;
1949
1950 SM_I(sbi)->sit_info = sit_i;
1951
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001952 sit_i->sentries = vzalloc(MAIN_SEGS(sbi) * sizeof(struct seg_entry));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001953 if (!sit_i->sentries)
1954 return -ENOMEM;
1955
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001956 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001957 sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1958 if (!sit_i->dirty_sentries_bitmap)
1959 return -ENOMEM;
1960
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001961 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001962 sit_i->sentries[start].cur_valid_map
1963 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1964 sit_i->sentries[start].ckpt_valid_map
1965 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001966 sit_i->sentries[start].discard_map
1967 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1968 if (!sit_i->sentries[start].cur_valid_map ||
1969 !sit_i->sentries[start].ckpt_valid_map ||
1970 !sit_i->sentries[start].discard_map)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001971 return -ENOMEM;
1972 }
1973
Jaegeuk Kim60a3b782015-02-10 16:44:29 -08001974 sit_i->tmp_map = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1975 if (!sit_i->tmp_map)
1976 return -ENOMEM;
1977
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001978 if (sbi->segs_per_sec > 1) {
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001979 sit_i->sec_entries = vzalloc(MAIN_SECS(sbi) *
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001980 sizeof(struct sec_entry));
1981 if (!sit_i->sec_entries)
1982 return -ENOMEM;
1983 }
1984
1985 /* get information related with SIT */
1986 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
1987
1988 /* setup SIT bitmap from ckeckpoint pack */
1989 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
1990 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
1991
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001992 dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001993 if (!dst_bitmap)
1994 return -ENOMEM;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001995
1996 /* init SIT information */
1997 sit_i->s_ops = &default_salloc_ops;
1998
1999 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
2000 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
2001 sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
2002 sit_i->sit_bitmap = dst_bitmap;
2003 sit_i->bitmap_size = bitmap_size;
2004 sit_i->dirty_sentries = 0;
2005 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
2006 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
2007 sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
2008 mutex_init(&sit_i->sentry_lock);
2009 return 0;
2010}
2011
2012static int build_free_segmap(struct f2fs_sb_info *sbi)
2013{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002014 struct free_segmap_info *free_i;
2015 unsigned int bitmap_size, sec_bitmap_size;
2016
2017 /* allocate memory for free segmap information */
2018 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
2019 if (!free_i)
2020 return -ENOMEM;
2021
2022 SM_I(sbi)->free_info = free_i;
2023
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002024 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002025 free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL);
2026 if (!free_i->free_segmap)
2027 return -ENOMEM;
2028
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002029 sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002030 free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL);
2031 if (!free_i->free_secmap)
2032 return -ENOMEM;
2033
2034 /* set all segments as dirty temporarily */
2035 memset(free_i->free_segmap, 0xff, bitmap_size);
2036 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
2037
2038 /* init free segmap information */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002039 free_i->start_segno = GET_SEGNO_FROM_SEG0(sbi, MAIN_BLKADDR(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002040 free_i->free_segments = 0;
2041 free_i->free_sections = 0;
Chao Yu1a118cc2015-02-11 18:20:38 +08002042 spin_lock_init(&free_i->segmap_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002043 return 0;
2044}
2045
2046static int build_curseg(struct f2fs_sb_info *sbi)
2047{
Namjae Jeon1042d602012-12-01 10:56:13 +09002048 struct curseg_info *array;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002049 int i;
2050
Fabian Frederickb434bab2014-06-23 18:39:15 +02002051 array = kcalloc(NR_CURSEG_TYPE, sizeof(*array), GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002052 if (!array)
2053 return -ENOMEM;
2054
2055 SM_I(sbi)->curseg_array = array;
2056
2057 for (i = 0; i < NR_CURSEG_TYPE; i++) {
2058 mutex_init(&array[i].curseg_mutex);
2059 array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
2060 if (!array[i].sum_blk)
2061 return -ENOMEM;
2062 array[i].segno = NULL_SEGNO;
2063 array[i].next_blkoff = 0;
2064 }
2065 return restore_curseg_summaries(sbi);
2066}
2067
2068static void build_sit_entries(struct f2fs_sb_info *sbi)
2069{
2070 struct sit_info *sit_i = SIT_I(sbi);
2071 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
2072 struct f2fs_summary_block *sum = curseg->sum_blk;
Chao Yu74de5932013-11-22 09:09:59 +08002073 int sit_blk_cnt = SIT_BLK_CNT(sbi);
2074 unsigned int i, start, end;
2075 unsigned int readed, start_blk = 0;
Jaegeuk Kim90a893c2014-09-22 16:21:07 -07002076 int nrpages = MAX_BIO_BLOCKS(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002077
Chao Yu74de5932013-11-22 09:09:59 +08002078 do {
Chao Yu662befd2014-02-07 16:11:53 +08002079 readed = ra_meta_pages(sbi, start_blk, nrpages, META_SIT);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002080
Chao Yu74de5932013-11-22 09:09:59 +08002081 start = start_blk * sit_i->sents_per_block;
2082 end = (start_blk + readed) * sit_i->sents_per_block;
2083
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002084 for (; start < end && start < MAIN_SEGS(sbi); start++) {
Chao Yu74de5932013-11-22 09:09:59 +08002085 struct seg_entry *se = &sit_i->sentries[start];
2086 struct f2fs_sit_block *sit_blk;
2087 struct f2fs_sit_entry sit;
2088 struct page *page;
2089
2090 mutex_lock(&curseg->curseg_mutex);
2091 for (i = 0; i < sits_in_cursum(sum); i++) {
Chris Fries6c311ec2014-01-17 14:44:39 -06002092 if (le32_to_cpu(segno_in_journal(sum, i))
2093 == start) {
Chao Yu74de5932013-11-22 09:09:59 +08002094 sit = sit_in_journal(sum, i);
2095 mutex_unlock(&curseg->curseg_mutex);
2096 goto got_it;
2097 }
2098 }
2099 mutex_unlock(&curseg->curseg_mutex);
2100
2101 page = get_current_sit_page(sbi, start);
2102 sit_blk = (struct f2fs_sit_block *)page_address(page);
2103 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
2104 f2fs_put_page(page, 1);
2105got_it:
2106 check_block_count(sbi, start, &sit);
2107 seg_info_from_raw_sit(se, &sit);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07002108
2109 /* build discard map only one time */
2110 memcpy(se->discard_map, se->cur_valid_map, SIT_VBLOCK_MAP_SIZE);
2111 sbi->discard_blks += sbi->blocks_per_seg - se->valid_blocks;
2112
Chao Yu74de5932013-11-22 09:09:59 +08002113 if (sbi->segs_per_sec > 1) {
2114 struct sec_entry *e = get_sec_entry(sbi, start);
2115 e->valid_blocks += se->valid_blocks;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002116 }
2117 }
Chao Yu74de5932013-11-22 09:09:59 +08002118 start_blk += readed;
2119 } while (start_blk < sit_blk_cnt);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002120}
2121
2122static void init_free_segmap(struct f2fs_sb_info *sbi)
2123{
2124 unsigned int start;
2125 int type;
2126
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002127 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002128 struct seg_entry *sentry = get_seg_entry(sbi, start);
2129 if (!sentry->valid_blocks)
2130 __set_free(sbi, start);
2131 }
2132
2133 /* set use the current segments */
2134 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
2135 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
2136 __set_test_and_inuse(sbi, curseg_t->segno);
2137 }
2138}
2139
2140static void init_dirty_segmap(struct f2fs_sb_info *sbi)
2141{
2142 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2143 struct free_segmap_info *free_i = FREE_I(sbi);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002144 unsigned int segno = 0, offset = 0;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002145 unsigned short valid_blocks;
2146
Namjae Jeon8736fbf2013-06-16 09:49:11 +09002147 while (1) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002148 /* find dirty segment based on free segmap */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002149 segno = find_next_inuse(free_i, MAIN_SEGS(sbi), offset);
2150 if (segno >= MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002151 break;
2152 offset = segno + 1;
2153 valid_blocks = get_valid_blocks(sbi, segno, 0);
Jaegeuk Kimec325b52014-09-02 16:24:11 -07002154 if (valid_blocks == sbi->blocks_per_seg || !valid_blocks)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002155 continue;
Jaegeuk Kimec325b52014-09-02 16:24:11 -07002156 if (valid_blocks > sbi->blocks_per_seg) {
2157 f2fs_bug_on(sbi, 1);
2158 continue;
2159 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002160 mutex_lock(&dirty_i->seglist_lock);
2161 __locate_dirty_segment(sbi, segno, DIRTY);
2162 mutex_unlock(&dirty_i->seglist_lock);
2163 }
2164}
2165
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002166static int init_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002167{
2168 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002169 unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002170
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002171 dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL);
2172 if (!dirty_i->victim_secmap)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002173 return -ENOMEM;
2174 return 0;
2175}
2176
2177static int build_dirty_segmap(struct f2fs_sb_info *sbi)
2178{
2179 struct dirty_seglist_info *dirty_i;
2180 unsigned int bitmap_size, i;
2181
2182 /* allocate memory for dirty segments list information */
2183 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
2184 if (!dirty_i)
2185 return -ENOMEM;
2186
2187 SM_I(sbi)->dirty_info = dirty_i;
2188 mutex_init(&dirty_i->seglist_lock);
2189
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002190 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002191
2192 for (i = 0; i < NR_DIRTY_TYPE; i++) {
2193 dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002194 if (!dirty_i->dirty_segmap[i])
2195 return -ENOMEM;
2196 }
2197
2198 init_dirty_segmap(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002199 return init_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002200}
2201
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002202/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002203 * Update min, max modified time for cost-benefit GC algorithm
2204 */
2205static void init_min_max_mtime(struct f2fs_sb_info *sbi)
2206{
2207 struct sit_info *sit_i = SIT_I(sbi);
2208 unsigned int segno;
2209
2210 mutex_lock(&sit_i->sentry_lock);
2211
2212 sit_i->min_mtime = LLONG_MAX;
2213
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002214 for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002215 unsigned int i;
2216 unsigned long long mtime = 0;
2217
2218 for (i = 0; i < sbi->segs_per_sec; i++)
2219 mtime += get_seg_entry(sbi, segno + i)->mtime;
2220
2221 mtime = div_u64(mtime, sbi->segs_per_sec);
2222
2223 if (sit_i->min_mtime > mtime)
2224 sit_i->min_mtime = mtime;
2225 }
2226 sit_i->max_mtime = get_mtime(sbi);
2227 mutex_unlock(&sit_i->sentry_lock);
2228}
2229
2230int build_segment_manager(struct f2fs_sb_info *sbi)
2231{
2232 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
2233 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Namjae Jeon1042d602012-12-01 10:56:13 +09002234 struct f2fs_sm_info *sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002235 int err;
2236
2237 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
2238 if (!sm_info)
2239 return -ENOMEM;
2240
2241 /* init sm info */
2242 sbi->sm_info = sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002243 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
2244 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
2245 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
2246 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
2247 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
2248 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
2249 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
Jaegeuk Kim58c41032014-03-19 14:17:21 +09002250 sm_info->rec_prefree_segments = sm_info->main_segments *
2251 DEF_RECLAIM_PREFREE_SEGMENTS / 100;
Jaegeuk Kim9b5f1362014-09-16 18:30:54 -07002252 sm_info->ipu_policy = 1 << F2FS_IPU_FSYNC;
Jaegeuk Kim216fbd62013-11-07 13:13:42 +09002253 sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -07002254 sm_info->min_fsync_blocks = DEF_MIN_FSYNC_BLOCKS;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002255
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002256 INIT_LIST_HEAD(&sm_info->discard_list);
2257 sm_info->nr_discards = 0;
2258 sm_info->max_discards = 0;
2259
Jaegeuk Kimbba681c2015-01-26 17:41:23 -08002260 sm_info->trim_sections = DEF_BATCHED_TRIM_SECTIONS;
2261
Chao Yu184a5cd2014-09-04 18:13:01 +08002262 INIT_LIST_HEAD(&sm_info->sit_entry_set);
2263
Gu Zhengb270ad62014-04-11 17:49:55 +08002264 if (test_opt(sbi, FLUSH_MERGE) && !f2fs_readonly(sbi->sb)) {
Gu Zheng2163d192014-04-27 14:21:33 +08002265 err = create_flush_cmd_control(sbi);
2266 if (err)
Gu Zhenga688b9d9e2014-04-27 14:21:21 +08002267 return err;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +09002268 }
2269
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002270 err = build_sit_info(sbi);
2271 if (err)
2272 return err;
2273 err = build_free_segmap(sbi);
2274 if (err)
2275 return err;
2276 err = build_curseg(sbi);
2277 if (err)
2278 return err;
2279
2280 /* reinit free segmap based on SIT */
2281 build_sit_entries(sbi);
2282
2283 init_free_segmap(sbi);
2284 err = build_dirty_segmap(sbi);
2285 if (err)
2286 return err;
2287
2288 init_min_max_mtime(sbi);
2289 return 0;
2290}
2291
2292static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
2293 enum dirty_type dirty_type)
2294{
2295 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2296
2297 mutex_lock(&dirty_i->seglist_lock);
2298 kfree(dirty_i->dirty_segmap[dirty_type]);
2299 dirty_i->nr_dirty[dirty_type] = 0;
2300 mutex_unlock(&dirty_i->seglist_lock);
2301}
2302
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002303static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002304{
2305 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002306 kfree(dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002307}
2308
2309static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
2310{
2311 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2312 int i;
2313
2314 if (!dirty_i)
2315 return;
2316
2317 /* discard pre-free/dirty segments list */
2318 for (i = 0; i < NR_DIRTY_TYPE; i++)
2319 discard_dirty_segmap(sbi, i);
2320
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002321 destroy_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002322 SM_I(sbi)->dirty_info = NULL;
2323 kfree(dirty_i);
2324}
2325
2326static void destroy_curseg(struct f2fs_sb_info *sbi)
2327{
2328 struct curseg_info *array = SM_I(sbi)->curseg_array;
2329 int i;
2330
2331 if (!array)
2332 return;
2333 SM_I(sbi)->curseg_array = NULL;
2334 for (i = 0; i < NR_CURSEG_TYPE; i++)
2335 kfree(array[i].sum_blk);
2336 kfree(array);
2337}
2338
2339static void destroy_free_segmap(struct f2fs_sb_info *sbi)
2340{
2341 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
2342 if (!free_i)
2343 return;
2344 SM_I(sbi)->free_info = NULL;
2345 kfree(free_i->free_segmap);
2346 kfree(free_i->free_secmap);
2347 kfree(free_i);
2348}
2349
2350static void destroy_sit_info(struct f2fs_sb_info *sbi)
2351{
2352 struct sit_info *sit_i = SIT_I(sbi);
2353 unsigned int start;
2354
2355 if (!sit_i)
2356 return;
2357
2358 if (sit_i->sentries) {
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002359 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002360 kfree(sit_i->sentries[start].cur_valid_map);
2361 kfree(sit_i->sentries[start].ckpt_valid_map);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07002362 kfree(sit_i->sentries[start].discard_map);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002363 }
2364 }
Jaegeuk Kim60a3b782015-02-10 16:44:29 -08002365 kfree(sit_i->tmp_map);
2366
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002367 vfree(sit_i->sentries);
2368 vfree(sit_i->sec_entries);
2369 kfree(sit_i->dirty_sentries_bitmap);
2370
2371 SM_I(sbi)->sit_info = NULL;
2372 kfree(sit_i->sit_bitmap);
2373 kfree(sit_i);
2374}
2375
2376void destroy_segment_manager(struct f2fs_sb_info *sbi)
2377{
2378 struct f2fs_sm_info *sm_info = SM_I(sbi);
Gu Zhenga688b9d9e2014-04-27 14:21:21 +08002379
Chao Yu3b03f722013-11-06 09:12:04 +08002380 if (!sm_info)
2381 return;
Gu Zheng2163d192014-04-27 14:21:33 +08002382 destroy_flush_cmd_control(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002383 destroy_dirty_segmap(sbi);
2384 destroy_curseg(sbi);
2385 destroy_free_segmap(sbi);
2386 destroy_sit_info(sbi);
2387 sbi->sm_info = NULL;
2388 kfree(sm_info);
2389}
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002390
2391int __init create_segment_manager_caches(void)
2392{
2393 discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
Gu Zhenge8512d22014-03-07 18:43:28 +08002394 sizeof(struct discard_entry));
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002395 if (!discard_entry_slab)
Chao Yu184a5cd2014-09-04 18:13:01 +08002396 goto fail;
2397
2398 sit_entry_set_slab = f2fs_kmem_cache_create("sit_entry_set",
Changman Leec9ee0082014-11-21 15:42:07 +09002399 sizeof(struct sit_entry_set));
Chao Yu184a5cd2014-09-04 18:13:01 +08002400 if (!sit_entry_set_slab)
2401 goto destory_discard_entry;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002402
2403 inmem_entry_slab = f2fs_kmem_cache_create("inmem_page_entry",
2404 sizeof(struct inmem_pages));
2405 if (!inmem_entry_slab)
2406 goto destroy_sit_entry_set;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002407 return 0;
Chao Yu184a5cd2014-09-04 18:13:01 +08002408
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002409destroy_sit_entry_set:
2410 kmem_cache_destroy(sit_entry_set_slab);
Chao Yu184a5cd2014-09-04 18:13:01 +08002411destory_discard_entry:
2412 kmem_cache_destroy(discard_entry_slab);
2413fail:
2414 return -ENOMEM;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002415}
2416
2417void destroy_segment_manager_caches(void)
2418{
Chao Yu184a5cd2014-09-04 18:13:01 +08002419 kmem_cache_destroy(sit_entry_set_slab);
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002420 kmem_cache_destroy(discard_entry_slab);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002421 kmem_cache_destroy(inmem_entry_slab);
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002422}