blob: c6e4a1174f8ad9cd91e4d2514544202a91ff9ec0 [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>
Chao Yu74de5932013-11-22 09:09:59 +080017#include <linux/swap.h>
Jaegeuk Kim60b99b42015-10-05 14:49:57 -070018#include <linux/timer.h>
Jaegeuk Kim1d7be272017-05-17 10:36:58 -070019#include <linux/freezer.h>
Jaegeuk Kim351df4b2012-11-02 17:09:16 +090020
21#include "f2fs.h"
22#include "segment.h"
23#include "node.h"
Jaegeuk Kim9e4ded32014-12-17 19:58:58 -080024#include "trace.h"
Namjae Jeon6ec178d2013-04-23 17:51:43 +090025#include <trace/events/f2fs.h>
Jaegeuk Kim351df4b2012-11-02 17:09:16 +090026
Changman Lee9a7f1432013-11-15 10:42:51 +090027#define __reverse_ffz(x) __reverse_ffs(~(x))
28
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +090029static struct kmem_cache *discard_entry_slab;
Jaegeuk Kimb01a9202017-01-09 14:13:03 -080030static struct kmem_cache *discard_cmd_slab;
Chao Yu184a5cd2014-09-04 18:13:01 +080031static struct kmem_cache *sit_entry_set_slab;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -070032static struct kmem_cache *inmem_entry_slab;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +090033
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070034static unsigned long __reverse_ulong(unsigned char *str)
35{
36 unsigned long tmp = 0;
37 int shift = 24, idx = 0;
38
39#if BITS_PER_LONG == 64
40 shift = 56;
41#endif
42 while (shift >= 0) {
43 tmp |= (unsigned long)str[idx++] << shift;
44 shift -= BITS_PER_BYTE;
45 }
46 return tmp;
47}
48
Changman Lee9a7f1432013-11-15 10:42:51 +090049/*
50 * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since
51 * MSB and LSB are reversed in a byte by f2fs_set_bit.
52 */
53static inline unsigned long __reverse_ffs(unsigned long word)
54{
55 int num = 0;
56
57#if BITS_PER_LONG == 64
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070058 if ((word & 0xffffffff00000000UL) == 0)
Changman Lee9a7f1432013-11-15 10:42:51 +090059 num += 32;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070060 else
Changman Lee9a7f1432013-11-15 10:42:51 +090061 word >>= 32;
Changman Lee9a7f1432013-11-15 10:42:51 +090062#endif
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070063 if ((word & 0xffff0000) == 0)
Changman Lee9a7f1432013-11-15 10:42:51 +090064 num += 16;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070065 else
Changman Lee9a7f1432013-11-15 10:42:51 +090066 word >>= 16;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070067
68 if ((word & 0xff00) == 0)
Changman Lee9a7f1432013-11-15 10:42:51 +090069 num += 8;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070070 else
Changman Lee9a7f1432013-11-15 10:42:51 +090071 word >>= 8;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070072
Changman Lee9a7f1432013-11-15 10:42:51 +090073 if ((word & 0xf0) == 0)
74 num += 4;
75 else
76 word >>= 4;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070077
Changman Lee9a7f1432013-11-15 10:42:51 +090078 if ((word & 0xc) == 0)
79 num += 2;
80 else
81 word >>= 2;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070082
Changman Lee9a7f1432013-11-15 10:42:51 +090083 if ((word & 0x2) == 0)
84 num += 1;
85 return num;
86}
87
88/*
arter97e1c42042014-08-06 23:22:50 +090089 * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c because
Changman Lee9a7f1432013-11-15 10:42:51 +090090 * f2fs_set_bit makes MSB and LSB reversed in a byte.
Fan Li692223d2015-11-12 08:43:04 +080091 * @size must be integral times of unsigned long.
Changman Lee9a7f1432013-11-15 10:42:51 +090092 * Example:
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070093 * MSB <--> LSB
94 * f2fs_set_bit(0, bitmap) => 1000 0000
95 * f2fs_set_bit(7, bitmap) => 0000 0001
Changman Lee9a7f1432013-11-15 10:42:51 +090096 */
97static unsigned long __find_rev_next_bit(const unsigned long *addr,
98 unsigned long size, unsigned long offset)
99{
100 const unsigned long *p = addr + BIT_WORD(offset);
Fan Li692223d2015-11-12 08:43:04 +0800101 unsigned long result = size;
Changman Lee9a7f1432013-11-15 10:42:51 +0900102 unsigned long tmp;
Changman Lee9a7f1432013-11-15 10:42:51 +0900103
104 if (offset >= size)
105 return size;
106
Fan Li692223d2015-11-12 08:43:04 +0800107 size -= (offset & ~(BITS_PER_LONG - 1));
Changman Lee9a7f1432013-11-15 10:42:51 +0900108 offset %= BITS_PER_LONG;
Changman Lee9a7f1432013-11-15 10:42:51 +0900109
Fan Li692223d2015-11-12 08:43:04 +0800110 while (1) {
111 if (*p == 0)
112 goto pass;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700113
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700114 tmp = __reverse_ulong((unsigned char *)p);
Fan Li692223d2015-11-12 08:43:04 +0800115
116 tmp &= ~0UL >> offset;
117 if (size < BITS_PER_LONG)
118 tmp &= (~0UL << (BITS_PER_LONG - size));
Changman Lee9a7f1432013-11-15 10:42:51 +0900119 if (tmp)
Fan Li692223d2015-11-12 08:43:04 +0800120 goto found;
121pass:
122 if (size <= BITS_PER_LONG)
123 break;
Changman Lee9a7f1432013-11-15 10:42:51 +0900124 size -= BITS_PER_LONG;
Fan Li692223d2015-11-12 08:43:04 +0800125 offset = 0;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700126 p++;
Changman Lee9a7f1432013-11-15 10:42:51 +0900127 }
Fan Li692223d2015-11-12 08:43:04 +0800128 return result;
129found:
130 return result - size + __reverse_ffs(tmp);
Changman Lee9a7f1432013-11-15 10:42:51 +0900131}
132
133static unsigned long __find_rev_next_zero_bit(const unsigned long *addr,
134 unsigned long size, unsigned long offset)
135{
136 const unsigned long *p = addr + BIT_WORD(offset);
Jaegeuk Kim80609442015-12-04 16:51:13 -0800137 unsigned long result = size;
Changman Lee9a7f1432013-11-15 10:42:51 +0900138 unsigned long tmp;
Changman Lee9a7f1432013-11-15 10:42:51 +0900139
140 if (offset >= size)
141 return size;
142
Jaegeuk Kim80609442015-12-04 16:51:13 -0800143 size -= (offset & ~(BITS_PER_LONG - 1));
Changman Lee9a7f1432013-11-15 10:42:51 +0900144 offset %= BITS_PER_LONG;
Changman Lee9a7f1432013-11-15 10:42:51 +0900145
Jaegeuk Kim80609442015-12-04 16:51:13 -0800146 while (1) {
147 if (*p == ~0UL)
148 goto pass;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700149
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700150 tmp = __reverse_ulong((unsigned char *)p);
Jaegeuk Kim80609442015-12-04 16:51:13 -0800151
152 if (offset)
153 tmp |= ~0UL << (BITS_PER_LONG - offset);
154 if (size < BITS_PER_LONG)
155 tmp |= ~0UL >> size;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700156 if (tmp != ~0UL)
Jaegeuk Kim80609442015-12-04 16:51:13 -0800157 goto found;
158pass:
159 if (size <= BITS_PER_LONG)
160 break;
Changman Lee9a7f1432013-11-15 10:42:51 +0900161 size -= BITS_PER_LONG;
Jaegeuk Kim80609442015-12-04 16:51:13 -0800162 offset = 0;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700163 p++;
Changman Lee9a7f1432013-11-15 10:42:51 +0900164 }
Jaegeuk Kim80609442015-12-04 16:51:13 -0800165 return result;
166found:
167 return result - size + __reverse_ffz(tmp);
Changman Lee9a7f1432013-11-15 10:42:51 +0900168}
169
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700170void register_inmem_page(struct inode *inode, struct page *page)
171{
172 struct f2fs_inode_info *fi = F2FS_I(inode);
173 struct inmem_pages *new;
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800174
Jaegeuk Kim9e4ded32014-12-17 19:58:58 -0800175 f2fs_trace_pid(page);
Jaegeuk Kim0722b102014-12-05 11:58:02 -0800176
Chao Yudecd36b2015-08-07 18:42:09 +0800177 set_page_private(page, (unsigned long)ATOMIC_WRITTEN_PAGE);
178 SetPagePrivate(page);
179
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700180 new = f2fs_kmem_cache_alloc(inmem_entry_slab, GFP_NOFS);
181
182 /* add atomic page indices to the list */
183 new->page = page;
184 INIT_LIST_HEAD(&new->list);
Chao Yudecd36b2015-08-07 18:42:09 +0800185
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700186 /* increase reference count with clean state */
187 mutex_lock(&fi->inmem_lock);
188 get_page(page);
189 list_add_tail(&new->list, &fi->inmem_pages);
Jaegeuk Kim8dcf2ff72014-12-05 17:18:15 -0800190 inc_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700191 mutex_unlock(&fi->inmem_lock);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -0700192
193 trace_f2fs_register_inmem_page(page, INMEM);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700194}
195
Chao Yu28bc1062016-02-06 14:40:34 +0800196static int __revoke_inmem_pages(struct inode *inode,
197 struct list_head *head, bool drop, bool recover)
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700198{
Chao Yu28bc1062016-02-06 14:40:34 +0800199 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700200 struct inmem_pages *cur, *tmp;
Chao Yu28bc1062016-02-06 14:40:34 +0800201 int err = 0;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700202
Chao Yu29b96b52016-02-06 14:38:29 +0800203 list_for_each_entry_safe(cur, tmp, head, list) {
Chao Yu28bc1062016-02-06 14:40:34 +0800204 struct page *page = cur->page;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700205
Chao Yu28bc1062016-02-06 14:40:34 +0800206 if (drop)
207 trace_f2fs_commit_inmem_page(page, INMEM_DROP);
208
209 lock_page(page);
210
211 if (recover) {
212 struct dnode_of_data dn;
213 struct node_info ni;
214
215 trace_f2fs_commit_inmem_page(page, INMEM_REVOKE);
216
217 set_new_dnode(&dn, inode, NULL, NULL, 0);
218 if (get_dnode_of_data(&dn, page->index, LOOKUP_NODE)) {
219 err = -EAGAIN;
220 goto next;
221 }
222 get_node_info(sbi, dn.nid, &ni);
223 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
224 cur->old_addr, ni.version, true, true);
225 f2fs_put_dnode(&dn);
226 }
227next:
Jaegeuk Kim63c52d72016-04-12 14:11:03 -0700228 /* we don't need to invalidate this in the sccessful status */
229 if (drop || recover)
230 ClearPageUptodate(page);
Chao Yu28bc1062016-02-06 14:40:34 +0800231 set_page_private(page, 0);
Chao Yuc81ced02016-04-29 20:13:36 +0800232 ClearPagePrivate(page);
Chao Yu28bc1062016-02-06 14:40:34 +0800233 f2fs_put_page(page, 1);
Chao Yudecd36b2015-08-07 18:42:09 +0800234
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700235 list_del(&cur->list);
236 kmem_cache_free(inmem_entry_slab, cur);
Jaegeuk Kim8dcf2ff72014-12-05 17:18:15 -0800237 dec_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700238 }
Chao Yu28bc1062016-02-06 14:40:34 +0800239 return err;
Chao Yu29b96b52016-02-06 14:38:29 +0800240}
241
242void drop_inmem_pages(struct inode *inode)
243{
244 struct f2fs_inode_info *fi = F2FS_I(inode);
245
246 mutex_lock(&fi->inmem_lock);
Chao Yu28bc1062016-02-06 14:40:34 +0800247 __revoke_inmem_pages(inode, &fi->inmem_pages, true, false);
Chao Yu29b96b52016-02-06 14:38:29 +0800248 mutex_unlock(&fi->inmem_lock);
Chao Yu5fe45742017-01-07 18:50:26 +0800249
250 clear_inode_flag(inode, FI_ATOMIC_FILE);
251 stat_dec_atomic_write(inode);
Chao Yu29b96b52016-02-06 14:38:29 +0800252}
253
Jaegeuk Kim8c242db2017-03-17 09:55:52 +0800254void drop_inmem_page(struct inode *inode, struct page *page)
255{
256 struct f2fs_inode_info *fi = F2FS_I(inode);
257 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
258 struct list_head *head = &fi->inmem_pages;
259 struct inmem_pages *cur = NULL;
260
261 f2fs_bug_on(sbi, !IS_ATOMIC_WRITTEN_PAGE(page));
262
263 mutex_lock(&fi->inmem_lock);
264 list_for_each_entry(cur, head, list) {
265 if (cur->page == page)
266 break;
267 }
268
269 f2fs_bug_on(sbi, !cur || cur->page != page);
270 list_del(&cur->list);
271 mutex_unlock(&fi->inmem_lock);
272
273 dec_page_count(sbi, F2FS_INMEM_PAGES);
274 kmem_cache_free(inmem_entry_slab, cur);
275
276 ClearPageUptodate(page);
277 set_page_private(page, 0);
278 ClearPagePrivate(page);
279 f2fs_put_page(page, 0);
280
281 trace_f2fs_commit_inmem_page(page, INMEM_INVALIDATE);
282}
283
Chao Yu28bc1062016-02-06 14:40:34 +0800284static int __commit_inmem_pages(struct inode *inode,
285 struct list_head *revoke_list)
Chao Yu29b96b52016-02-06 14:38:29 +0800286{
287 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
288 struct f2fs_inode_info *fi = F2FS_I(inode);
289 struct inmem_pages *cur, *tmp;
290 struct f2fs_io_info fio = {
291 .sbi = sbi,
292 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -0500293 .op = REQ_OP_WRITE,
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600294 .op_flags = REQ_SYNC | REQ_PRIO,
Chao Yub0af6d42017-08-02 23:21:48 +0800295 .io_type = FS_DATA_IO,
Chao Yu29b96b52016-02-06 14:38:29 +0800296 };
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800297 pgoff_t last_idx = ULONG_MAX;
Chao Yu29b96b52016-02-06 14:38:29 +0800298 int err = 0;
299
300 list_for_each_entry_safe(cur, tmp, &fi->inmem_pages, list) {
Chao Yu28bc1062016-02-06 14:40:34 +0800301 struct page *page = cur->page;
302
303 lock_page(page);
304 if (page->mapping == inode->i_mapping) {
305 trace_f2fs_commit_inmem_page(page, INMEM);
306
307 set_page_dirty(page);
308 f2fs_wait_on_page_writeback(page, DATA, true);
Chao Yu933439c2016-10-11 22:57:01 +0800309 if (clear_page_dirty_for_io(page)) {
Chao Yu29b96b52016-02-06 14:38:29 +0800310 inode_dec_dirty_pages(inode);
Chao Yu933439c2016-10-11 22:57:01 +0800311 remove_dirty_inode(inode);
312 }
Jaegeuk Kim640cc182017-07-19 10:59:55 -0700313retry:
Chao Yu28bc1062016-02-06 14:40:34 +0800314 fio.page = page;
Hou Pengyange959c8f2017-04-25 12:45:13 +0000315 fio.old_blkaddr = NULL_ADDR;
Jaegeuk Kim4d978072017-04-26 11:11:12 -0700316 fio.encrypted_page = NULL;
Jaegeuk Kimcc156202017-05-12 13:51:34 -0700317 fio.need_lock = LOCK_DONE;
Chao Yu29b96b52016-02-06 14:38:29 +0800318 err = do_write_data_page(&fio);
319 if (err) {
Jaegeuk Kim640cc182017-07-19 10:59:55 -0700320 if (err == -ENOMEM) {
321 congestion_wait(BLK_RW_ASYNC, HZ/50);
322 cond_resched();
323 goto retry;
324 }
Chao Yu28bc1062016-02-06 14:40:34 +0800325 unlock_page(page);
Chao Yu29b96b52016-02-06 14:38:29 +0800326 break;
327 }
Chao Yu28bc1062016-02-06 14:40:34 +0800328 /* record old blkaddr for revoking */
329 cur->old_addr = fio.old_blkaddr;
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800330 last_idx = page->index;
Chao Yu29b96b52016-02-06 14:38:29 +0800331 }
Chao Yu28bc1062016-02-06 14:40:34 +0800332 unlock_page(page);
333 list_move_tail(&cur->list, revoke_list);
Chao Yu29b96b52016-02-06 14:38:29 +0800334 }
335
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800336 if (last_idx != ULONG_MAX)
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700337 f2fs_submit_merged_write_cond(sbi, inode, 0, last_idx, DATA);
Chao Yu28bc1062016-02-06 14:40:34 +0800338
339 if (!err)
340 __revoke_inmem_pages(inode, revoke_list, false, false);
341
Chao Yu29b96b52016-02-06 14:38:29 +0800342 return err;
343}
344
345int commit_inmem_pages(struct inode *inode)
346{
347 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
348 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yu28bc1062016-02-06 14:40:34 +0800349 struct list_head revoke_list;
350 int err;
Chao Yu29b96b52016-02-06 14:38:29 +0800351
Chao Yu28bc1062016-02-06 14:40:34 +0800352 INIT_LIST_HEAD(&revoke_list);
Chao Yu29b96b52016-02-06 14:38:29 +0800353 f2fs_balance_fs(sbi, true);
354 f2fs_lock_op(sbi);
355
Chao Yu5fe45742017-01-07 18:50:26 +0800356 set_inode_flag(inode, FI_ATOMIC_COMMIT);
357
Chao Yu29b96b52016-02-06 14:38:29 +0800358 mutex_lock(&fi->inmem_lock);
Chao Yu28bc1062016-02-06 14:40:34 +0800359 err = __commit_inmem_pages(inode, &revoke_list);
360 if (err) {
361 int ret;
362 /*
363 * try to revoke all committed pages, but still we could fail
364 * due to no memory or other reason, if that happened, EAGAIN
365 * will be returned, which means in such case, transaction is
366 * already not integrity, caller should use journal to do the
367 * recovery or rewrite & commit last transaction. For other
368 * error number, revoking was done by filesystem itself.
369 */
370 ret = __revoke_inmem_pages(inode, &revoke_list, false, true);
371 if (ret)
372 err = ret;
373
374 /* drop all uncommitted pages */
375 __revoke_inmem_pages(inode, &fi->inmem_pages, true, false);
376 }
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700377 mutex_unlock(&fi->inmem_lock);
378
Chao Yu5fe45742017-01-07 18:50:26 +0800379 clear_inode_flag(inode, FI_ATOMIC_COMMIT);
380
Chao Yu29b96b52016-02-06 14:38:29 +0800381 f2fs_unlock_op(sbi);
Jaegeuk Kimedb27de2015-07-25 00:52:52 -0700382 return err;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700383}
384
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900385/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900386 * This function balances dirty node and dentry pages.
387 * In addition, it controls garbage collection.
388 */
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800389void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900390{
Chao Yu0f348022016-09-26 19:45:55 +0800391#ifdef CONFIG_F2FS_FAULT_INJECTION
Chao Yu55523512017-02-25 11:08:28 +0800392 if (time_to_inject(sbi, FAULT_CHECKPOINT)) {
393 f2fs_show_injection_info(FAULT_CHECKPOINT);
Chao Yu0f348022016-09-26 19:45:55 +0800394 f2fs_stop_checkpoint(sbi, false);
Chao Yu55523512017-02-25 11:08:28 +0800395 }
Chao Yu0f348022016-09-26 19:45:55 +0800396#endif
397
Jaegeuk Kime589c2c2016-06-02 15:24:24 -0700398 /* balance_fs_bg is able to be pending */
Jaegeuk Kima7881892017-04-20 13:51:57 -0700399 if (need && excess_cached_nats(sbi))
Jaegeuk Kime589c2c2016-06-02 15:24:24 -0700400 f2fs_balance_fs_bg(sbi);
401
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900402 /*
Jaegeuk Kim029cd282012-12-21 17:20:21 +0900403 * We should do GC or end up with checkpoint, if there are so many dirty
404 * dir/node pages without enough free segments.
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900405 */
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -0700406 if (has_not_enough_free_secs(sbi, 0, 0)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900407 mutex_lock(&sbi->gc_mutex);
Jaegeuk Kime066b832017-04-13 15:17:00 -0700408 f2fs_gc(sbi, false, false, NULL_SEGNO);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900409 }
410}
411
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900412void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi)
413{
Chao Yu1dcc3362015-02-05 17:57:31 +0800414 /* try to shrink extent cache when there is no enough memory */
Jaegeuk Kim554df792015-06-19 13:41:23 -0700415 if (!available_free_memory(sbi, EXTENT_CACHE))
416 f2fs_shrink_extent_tree(sbi, EXTENT_CACHE_SHRINK_NUMBER);
Chao Yu1dcc3362015-02-05 17:57:31 +0800417
Jaegeuk Kim1b38dc82015-06-19 15:36:07 -0700418 /* check the # of cached NAT entries */
419 if (!available_free_memory(sbi, NAT_ENTRIES))
420 try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK);
421
Chao Yu31696582015-07-28 18:33:46 +0800422 if (!available_free_memory(sbi, FREE_NIDS))
Jaegeuk Kimad4edb82016-06-16 16:41:49 -0700423 try_to_free_nids(sbi, MAX_FREE_NIDS);
424 else
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -0800425 build_free_nids(sbi, false, false);
Chao Yu31696582015-07-28 18:33:46 +0800426
Jaegeuk Kim1c0f4bf2017-05-01 18:09:44 -0700427 if (!is_idle(sbi) && !excess_dirty_nats(sbi))
Jaegeuk Kimf455c8a2016-12-05 11:37:14 -0800428 return;
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -0800429
Jaegeuk Kim88a70a62014-12-10 15:20:48 -0800430 /* checkpoint is the only way to shrink partial cached entries */
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900431 if (!available_free_memory(sbi, NAT_ENTRIES) ||
Jaegeuk Kim60b99b42015-10-05 14:49:57 -0700432 !available_free_memory(sbi, INO_ENTRIES) ||
Chao Yu7d768d22016-01-18 18:31:18 +0800433 excess_prefree_segs(sbi) ||
434 excess_dirty_nats(sbi) ||
Jaegeuk Kimf455c8a2016-12-05 11:37:14 -0800435 f2fs_time_over(sbi, CP_TIME)) {
Chao Yue9f5b8b2016-02-14 18:54:33 +0800436 if (test_opt(sbi, DATA_FLUSH)) {
437 struct blk_plug plug;
438
439 blk_start_plug(&plug);
Chao Yu36b35a02015-12-17 17:13:28 +0800440 sync_dirty_inodes(sbi, FILE_INODE);
Chao Yue9f5b8b2016-02-14 18:54:33 +0800441 blk_finish_plug(&plug);
442 }
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900443 f2fs_sync_fs(sbi->sb, true);
Jaegeuk Kim42190d22016-01-09 13:45:17 -0800444 stat_inc_bg_cp_count(sbi->stat_info);
Chao Yu36b35a02015-12-17 17:13:28 +0800445 }
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900446}
447
Kinglong Mee20fda56b2017-03-04 22:13:10 +0800448static int __submit_flush_wait(struct f2fs_sb_info *sbi,
449 struct block_device *bdev)
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700450{
451 struct bio *bio = f2fs_bio_alloc(0);
452 int ret;
453
Jan Kara3adc5fcb2017-05-02 17:03:47 +0200454 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700455 bio->bi_bdev = bdev;
456 ret = submit_bio_wait(bio);
457 bio_put(bio);
Kinglong Mee20fda56b2017-03-04 22:13:10 +0800458
459 trace_f2fs_issue_flush(bdev, test_opt(sbi, NOBARRIER),
460 test_opt(sbi, FLUSH_MERGE), ret);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700461 return ret;
462}
463
464static int submit_flush_wait(struct f2fs_sb_info *sbi)
465{
Kinglong Mee20fda56b2017-03-04 22:13:10 +0800466 int ret = __submit_flush_wait(sbi, sbi->sb->s_bdev);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700467 int i;
468
Kinglong Mee20fda56b2017-03-04 22:13:10 +0800469 if (!sbi->s_ndevs || ret)
470 return ret;
471
472 for (i = 1; i < sbi->s_ndevs; i++) {
473 ret = __submit_flush_wait(sbi, FDEV(i).bdev);
474 if (ret)
475 break;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700476 }
477 return ret;
478}
479
Gu Zheng2163d192014-04-27 14:21:33 +0800480static int issue_flush_thread(void *data)
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900481{
482 struct f2fs_sb_info *sbi = data;
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800483 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800484 wait_queue_head_t *q = &fcc->flush_wait_queue;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900485repeat:
486 if (kthread_should_stop())
487 return 0;
488
Chao Yudc6febb2017-07-22 08:52:23 +0800489 sb_start_intwrite(sbi->sb);
490
Gu Zheng721bd4d2014-09-05 18:31:00 +0800491 if (!llist_empty(&fcc->issue_list)) {
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900492 struct flush_cmd *cmd, *next;
493 int ret;
494
Gu Zheng721bd4d2014-09-05 18:31:00 +0800495 fcc->dispatch_list = llist_del_all(&fcc->issue_list);
496 fcc->dispatch_list = llist_reverse_order(fcc->dispatch_list);
497
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700498 ret = submit_flush_wait(sbi);
Chao Yu8b8dd652017-03-25 17:19:58 +0800499 atomic_inc(&fcc->issued_flush);
500
Gu Zheng721bd4d2014-09-05 18:31:00 +0800501 llist_for_each_entry_safe(cmd, next,
502 fcc->dispatch_list, llnode) {
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900503 cmd->ret = ret;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900504 complete(&cmd->wait);
505 }
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800506 fcc->dispatch_list = NULL;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900507 }
508
Chao Yudc6febb2017-07-22 08:52:23 +0800509 sb_end_intwrite(sbi->sb);
510
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800511 wait_event_interruptible(*q,
Gu Zheng721bd4d2014-09-05 18:31:00 +0800512 kthread_should_stop() || !llist_empty(&fcc->issue_list));
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900513 goto repeat;
514}
515
516int f2fs_issue_flush(struct f2fs_sb_info *sbi)
517{
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800518 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
Chao Yuadf8d902014-05-08 17:00:35 +0800519 struct flush_cmd cmd;
Chao Yu8b8dd652017-03-25 17:19:58 +0800520 int ret;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900521
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700522 if (test_opt(sbi, NOBARRIER))
523 return 0;
524
Chao Yu8b8dd652017-03-25 17:19:58 +0800525 if (!test_opt(sbi, FLUSH_MERGE)) {
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700526 ret = submit_flush_wait(sbi);
Chao Yu8b8dd652017-03-25 17:19:58 +0800527 atomic_inc(&fcc->issued_flush);
528 return ret;
529 }
530
531 if (!atomic_read(&fcc->issing_flush)) {
532 atomic_inc(&fcc->issing_flush);
533 ret = submit_flush_wait(sbi);
534 atomic_dec(&fcc->issing_flush);
535
536 atomic_inc(&fcc->issued_flush);
Jaegeuk Kim740432f2015-08-14 11:43:56 -0700537 return ret;
538 }
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900539
Chao Yuadf8d902014-05-08 17:00:35 +0800540 init_completion(&cmd.wait);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900541
Chao Yu8b8dd652017-03-25 17:19:58 +0800542 atomic_inc(&fcc->issing_flush);
Gu Zheng721bd4d2014-09-05 18:31:00 +0800543 llist_add(&cmd.llnode, &fcc->issue_list);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900544
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800545 if (!fcc->dispatch_list)
546 wake_up(&fcc->flush_wait_queue);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900547
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800548 if (fcc->f2fs_issue_flush) {
549 wait_for_completion(&cmd.wait);
Chao Yu8b8dd652017-03-25 17:19:58 +0800550 atomic_dec(&fcc->issing_flush);
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800551 } else {
552 llist_del_all(&fcc->issue_list);
Chao Yu8b8dd652017-03-25 17:19:58 +0800553 atomic_set(&fcc->issing_flush, 0);
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800554 }
Chao Yuadf8d902014-05-08 17:00:35 +0800555
556 return cmd.ret;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900557}
558
Gu Zheng2163d192014-04-27 14:21:33 +0800559int create_flush_cmd_control(struct f2fs_sb_info *sbi)
560{
561 dev_t dev = sbi->sb->s_bdev->bd_dev;
562 struct flush_cmd_control *fcc;
563 int err = 0;
564
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800565 if (SM_I(sbi)->fcc_info) {
566 fcc = SM_I(sbi)->fcc_info;
Yunlong Songd871cd02017-06-24 15:57:19 +0800567 if (fcc->f2fs_issue_flush)
568 return err;
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800569 goto init_thread;
570 }
571
Gu Zheng2163d192014-04-27 14:21:33 +0800572 fcc = kzalloc(sizeof(struct flush_cmd_control), GFP_KERNEL);
573 if (!fcc)
574 return -ENOMEM;
Chao Yu8b8dd652017-03-25 17:19:58 +0800575 atomic_set(&fcc->issued_flush, 0);
576 atomic_set(&fcc->issing_flush, 0);
Gu Zheng2163d192014-04-27 14:21:33 +0800577 init_waitqueue_head(&fcc->flush_wait_queue);
Gu Zheng721bd4d2014-09-05 18:31:00 +0800578 init_llist_head(&fcc->issue_list);
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800579 SM_I(sbi)->fcc_info = fcc;
Yunlei Hed4fdf8b2017-06-01 16:43:51 +0800580 if (!test_opt(sbi, FLUSH_MERGE))
581 return err;
582
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800583init_thread:
Gu Zheng2163d192014-04-27 14:21:33 +0800584 fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi,
585 "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev));
586 if (IS_ERR(fcc->f2fs_issue_flush)) {
587 err = PTR_ERR(fcc->f2fs_issue_flush);
588 kfree(fcc);
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800589 SM_I(sbi)->fcc_info = NULL;
Gu Zheng2163d192014-04-27 14:21:33 +0800590 return err;
591 }
Gu Zheng2163d192014-04-27 14:21:33 +0800592
593 return err;
594}
595
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800596void destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free)
Gu Zheng2163d192014-04-27 14:21:33 +0800597{
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800598 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
Gu Zheng2163d192014-04-27 14:21:33 +0800599
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800600 if (fcc && fcc->f2fs_issue_flush) {
601 struct task_struct *flush_thread = fcc->f2fs_issue_flush;
602
603 fcc->f2fs_issue_flush = NULL;
604 kthread_stop(flush_thread);
605 }
606 if (free) {
607 kfree(fcc);
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800608 SM_I(sbi)->fcc_info = NULL;
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800609 }
Gu Zheng2163d192014-04-27 14:21:33 +0800610}
611
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900612static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
613 enum dirty_type dirty_type)
614{
615 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
616
617 /* need not be added */
618 if (IS_CURSEG(sbi, segno))
619 return;
620
621 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
622 dirty_i->nr_dirty[dirty_type]++;
623
624 if (dirty_type == DIRTY) {
625 struct seg_entry *sentry = get_seg_entry(sbi, segno);
Changman Lee4625d6a2013-10-25 17:31:57 +0900626 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900627
Jaegeuk Kimec325b52014-09-02 16:24:11 -0700628 if (unlikely(t >= DIRTY)) {
629 f2fs_bug_on(sbi, 1);
630 return;
631 }
Changman Lee4625d6a2013-10-25 17:31:57 +0900632 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
633 dirty_i->nr_dirty[t]++;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900634 }
635}
636
637static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
638 enum dirty_type dirty_type)
639{
640 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
641
642 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
643 dirty_i->nr_dirty[dirty_type]--;
644
645 if (dirty_type == DIRTY) {
Changman Lee4625d6a2013-10-25 17:31:57 +0900646 struct seg_entry *sentry = get_seg_entry(sbi, segno);
647 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900648
Changman Lee4625d6a2013-10-25 17:31:57 +0900649 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
650 dirty_i->nr_dirty[t]--;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900651
Jaegeuk Kim302bd342017-04-07 14:33:22 -0700652 if (get_valid_blocks(sbi, segno, true) == 0)
Jaegeuk Kim4ddb1a42017-04-07 15:08:17 -0700653 clear_bit(GET_SEC_FROM_SEG(sbi, segno),
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +0900654 dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900655 }
656}
657
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900658/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900659 * Should not occur error such as -ENOMEM.
660 * Adding dirty entry into seglist is not critical operation.
661 * If a given segment is one of current working segments, it won't be added.
662 */
Haicheng Li8d8451a2013-06-13 16:59:28 +0800663static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900664{
665 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
666 unsigned short valid_blocks;
667
668 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
669 return;
670
671 mutex_lock(&dirty_i->seglist_lock);
672
Jaegeuk Kim302bd342017-04-07 14:33:22 -0700673 valid_blocks = get_valid_blocks(sbi, segno, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900674
675 if (valid_blocks == 0) {
676 __locate_dirty_segment(sbi, segno, PRE);
677 __remove_dirty_segment(sbi, segno, DIRTY);
678 } else if (valid_blocks < sbi->blocks_per_seg) {
679 __locate_dirty_segment(sbi, segno, DIRTY);
680 } else {
681 /* Recovery routine with SSR needs this */
682 __remove_dirty_segment(sbi, segno, DIRTY);
683 }
684
685 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900686}
687
Chao Yu004b6862017-04-14 23:24:55 +0800688static struct discard_cmd *__create_discard_cmd(struct f2fs_sb_info *sbi,
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800689 struct block_device *bdev, block_t lstart,
690 block_t start, block_t len)
Chao Yu275b66b2016-08-29 23:58:34 +0800691{
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -0800692 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Chao Yuba48a332017-04-15 14:09:37 +0800693 struct list_head *pend_list;
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800694 struct discard_cmd *dc;
Chao Yu275b66b2016-08-29 23:58:34 +0800695
Chao Yuba48a332017-04-15 14:09:37 +0800696 f2fs_bug_on(sbi, !len);
697
698 pend_list = &dcc->pend_list[plist_idx(len)];
699
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800700 dc = f2fs_kmem_cache_alloc(discard_cmd_slab, GFP_NOFS);
701 INIT_LIST_HEAD(&dc->list);
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800702 dc->bdev = bdev;
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800703 dc->lstart = lstart;
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800704 dc->start = start;
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800705 dc->len = len;
Chao Yuec9895a2017-04-26 17:39:54 +0800706 dc->ref = 0;
Jaegeuk Kim15469962017-01-09 20:32:07 -0800707 dc->state = D_PREP;
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800708 dc->error = 0;
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800709 init_completion(&dc->wait);
Chao Yu22d375d2017-04-05 18:19:48 +0800710 list_add_tail(&dc->list, pend_list);
Chao Yu5f323662017-03-25 17:19:59 +0800711 atomic_inc(&dcc->discard_cmd_cnt);
Chao Yud84d1cb2017-04-18 19:27:39 +0800712 dcc->undiscard_blks += len;
Chao Yu004b6862017-04-14 23:24:55 +0800713
714 return dc;
Jaegeuk Kim15469962017-01-09 20:32:07 -0800715}
716
Chao Yu004b6862017-04-14 23:24:55 +0800717static struct discard_cmd *__attach_discard_cmd(struct f2fs_sb_info *sbi,
718 struct block_device *bdev, block_t lstart,
719 block_t start, block_t len,
720 struct rb_node *parent, struct rb_node **p)
721{
722 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
723 struct discard_cmd *dc;
724
725 dc = __create_discard_cmd(sbi, bdev, lstart, start, len);
726
727 rb_link_node(&dc->rb_node, parent, p);
728 rb_insert_color(&dc->rb_node, &dcc->root);
729
730 return dc;
731}
732
733static void __detach_discard_cmd(struct discard_cmd_control *dcc,
734 struct discard_cmd *dc)
Jaegeuk Kim15469962017-01-09 20:32:07 -0800735{
Jaegeuk Kimdcc91652017-01-11 10:20:04 -0800736 if (dc->state == D_DONE)
Chao Yu004b6862017-04-14 23:24:55 +0800737 atomic_dec(&dcc->issing_discard);
738
739 list_del(&dc->list);
740 rb_erase(&dc->rb_node, &dcc->root);
Chao Yud84d1cb2017-04-18 19:27:39 +0800741 dcc->undiscard_blks -= dc->len;
Chao Yu004b6862017-04-14 23:24:55 +0800742
743 kmem_cache_free(discard_cmd_slab, dc);
744
745 atomic_dec(&dcc->discard_cmd_cnt);
746}
747
748static void __remove_discard_cmd(struct f2fs_sb_info *sbi,
749 struct discard_cmd *dc)
750{
751 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Jaegeuk Kimdcc91652017-01-11 10:20:04 -0800752
Chao Yud9703d92017-06-05 18:29:07 +0800753 f2fs_bug_on(sbi, dc->ref);
754
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800755 if (dc->error == -EOPNOTSUPP)
756 dc->error = 0;
Jaegeuk Kim15469962017-01-09 20:32:07 -0800757
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800758 if (dc->error)
Jaegeuk Kim15469962017-01-09 20:32:07 -0800759 f2fs_msg(sbi->sb, KERN_INFO,
Chao Yu04dfc232017-05-19 23:46:43 +0800760 "Issue discard(%u, %u, %u) failed, ret: %d",
761 dc->lstart, dc->start, dc->len, dc->error);
Chao Yu004b6862017-04-14 23:24:55 +0800762 __detach_discard_cmd(dcc, dc);
Chao Yu275b66b2016-08-29 23:58:34 +0800763}
764
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800765static void f2fs_submit_discard_endio(struct bio *bio)
766{
767 struct discard_cmd *dc = (struct discard_cmd *)bio->bi_private;
768
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200769 dc->error = blk_status_to_errno(bio->bi_status);
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800770 dc->state = D_DONE;
Chao Yue31b9822017-05-19 23:46:44 +0800771 complete_all(&dc->wait);
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800772 bio_put(bio);
773}
774
Chao Yu6915ea92017-06-30 17:19:02 +0800775void __check_sit_bitmap(struct f2fs_sb_info *sbi,
776 block_t start, block_t end)
777{
778#ifdef CONFIG_F2FS_CHECK_FS
779 struct seg_entry *sentry;
780 unsigned int segno;
781 block_t blk = start;
782 unsigned long offset, size, max_blocks = sbi->blocks_per_seg;
783 unsigned long *map;
784
785 while (blk < end) {
786 segno = GET_SEGNO(sbi, blk);
787 sentry = get_seg_entry(sbi, segno);
788 offset = GET_BLKOFF_FROM_SEG0(sbi, blk);
789
790 size = min((unsigned long)(end - blk), max_blocks);
791 map = (unsigned long *)(sentry->cur_valid_map);
792 offset = __find_rev_next_bit(map, size, offset);
793 f2fs_bug_on(sbi, offset != size);
794 blk += size;
795 }
796#endif
797}
798
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800799/* this function is copied from blkdev_issue_discard from block/blk-lib.c */
800static void __submit_discard_cmd(struct f2fs_sb_info *sbi,
801 struct discard_cmd *dc)
802{
803 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
804 struct bio *bio = NULL;
805
806 if (dc->state != D_PREP)
807 return;
808
Chao Yu0243a5f2017-04-15 14:09:38 +0800809 trace_f2fs_issue_discard(dc->bdev, dc->start, dc->len);
810
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800811 dc->error = __blkdev_issue_discard(dc->bdev,
812 SECTOR_FROM_BLOCK(dc->start),
813 SECTOR_FROM_BLOCK(dc->len),
814 GFP_NOFS, 0, &bio);
815 if (!dc->error) {
816 /* should keep before submission to avoid D_DONE right away */
817 dc->state = D_SUBMIT;
Chao Yu8b8dd652017-03-25 17:19:58 +0800818 atomic_inc(&dcc->issued_discard);
819 atomic_inc(&dcc->issing_discard);
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800820 if (bio) {
821 bio->bi_private = dc;
822 bio->bi_end_io = f2fs_submit_discard_endio;
823 bio->bi_opf |= REQ_SYNC;
824 submit_bio(bio);
Chao Yu46f84c22017-04-15 14:09:36 +0800825 list_move_tail(&dc->list, &dcc->wait_list);
Chao Yu6915ea92017-06-30 17:19:02 +0800826 __check_sit_bitmap(sbi, dc->start, dc->start + dc->len);
Chao Yub0af6d42017-08-02 23:21:48 +0800827
828 f2fs_update_iostat(sbi, FS_DISCARD, 1);
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800829 }
830 } else {
831 __remove_discard_cmd(sbi, dc);
832 }
833}
834
Chao Yu004b6862017-04-14 23:24:55 +0800835static struct discard_cmd *__insert_discard_tree(struct f2fs_sb_info *sbi,
836 struct block_device *bdev, block_t lstart,
837 block_t start, block_t len,
838 struct rb_node **insert_p,
839 struct rb_node *insert_parent)
840{
841 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
842 struct rb_node **p = &dcc->root.rb_node;
843 struct rb_node *parent = NULL;
844 struct discard_cmd *dc = NULL;
845
846 if (insert_p && insert_parent) {
847 parent = insert_parent;
848 p = insert_p;
849 goto do_insert;
850 }
851
852 p = __lookup_rb_tree_for_insert(sbi, &dcc->root, &parent, lstart);
853do_insert:
854 dc = __attach_discard_cmd(sbi, bdev, lstart, start, len, parent, p);
855 if (!dc)
856 return NULL;
857
858 return dc;
859}
860
Chao Yuba48a332017-04-15 14:09:37 +0800861static void __relocate_discard_cmd(struct discard_cmd_control *dcc,
862 struct discard_cmd *dc)
863{
864 list_move_tail(&dc->list, &dcc->pend_list[plist_idx(dc->len)]);
865}
866
Chao Yu004b6862017-04-14 23:24:55 +0800867static void __punch_discard_cmd(struct f2fs_sb_info *sbi,
868 struct discard_cmd *dc, block_t blkaddr)
869{
Chao Yuba48a332017-04-15 14:09:37 +0800870 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Chao Yu004b6862017-04-14 23:24:55 +0800871 struct discard_info di = dc->di;
872 bool modified = false;
873
874 if (dc->state == D_DONE || dc->len == 1) {
875 __remove_discard_cmd(sbi, dc);
876 return;
877 }
878
Chao Yud84d1cb2017-04-18 19:27:39 +0800879 dcc->undiscard_blks -= di.len;
880
Chao Yu004b6862017-04-14 23:24:55 +0800881 if (blkaddr > di.lstart) {
882 dc->len = blkaddr - dc->lstart;
Chao Yud84d1cb2017-04-18 19:27:39 +0800883 dcc->undiscard_blks += dc->len;
Chao Yuba48a332017-04-15 14:09:37 +0800884 __relocate_discard_cmd(dcc, dc);
Chao Yu004b6862017-04-14 23:24:55 +0800885 modified = true;
886 }
887
888 if (blkaddr < di.lstart + di.len - 1) {
889 if (modified) {
890 __insert_discard_tree(sbi, dc->bdev, blkaddr + 1,
891 di.start + blkaddr + 1 - di.lstart,
892 di.lstart + di.len - 1 - blkaddr,
893 NULL, NULL);
894 } else {
895 dc->lstart++;
896 dc->len--;
897 dc->start++;
Chao Yud84d1cb2017-04-18 19:27:39 +0800898 dcc->undiscard_blks += dc->len;
Chao Yuba48a332017-04-15 14:09:37 +0800899 __relocate_discard_cmd(dcc, dc);
Chao Yu004b6862017-04-14 23:24:55 +0800900 }
901 }
902}
903
904static void __update_discard_tree_range(struct f2fs_sb_info *sbi,
905 struct block_device *bdev, block_t lstart,
906 block_t start, block_t len)
907{
908 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
909 struct discard_cmd *prev_dc = NULL, *next_dc = NULL;
910 struct discard_cmd *dc;
911 struct discard_info di = {0};
912 struct rb_node **insert_p = NULL, *insert_parent = NULL;
913 block_t end = lstart + len;
914
915 mutex_lock(&dcc->cmd_lock);
916
917 dc = (struct discard_cmd *)__lookup_rb_tree_ret(&dcc->root,
918 NULL, lstart,
919 (struct rb_entry **)&prev_dc,
920 (struct rb_entry **)&next_dc,
921 &insert_p, &insert_parent, true);
922 if (dc)
923 prev_dc = dc;
924
925 if (!prev_dc) {
926 di.lstart = lstart;
927 di.len = next_dc ? next_dc->lstart - lstart : len;
928 di.len = min(di.len, len);
929 di.start = start;
930 }
931
932 while (1) {
933 struct rb_node *node;
934 bool merged = false;
935 struct discard_cmd *tdc = NULL;
936
937 if (prev_dc) {
938 di.lstart = prev_dc->lstart + prev_dc->len;
939 if (di.lstart < lstart)
940 di.lstart = lstart;
941 if (di.lstart >= end)
942 break;
943
944 if (!next_dc || next_dc->lstart > end)
945 di.len = end - di.lstart;
946 else
947 di.len = next_dc->lstart - di.lstart;
948 di.start = start + di.lstart - lstart;
949 }
950
951 if (!di.len)
952 goto next;
953
954 if (prev_dc && prev_dc->state == D_PREP &&
955 prev_dc->bdev == bdev &&
956 __is_discard_back_mergeable(&di, &prev_dc->di)) {
957 prev_dc->di.len += di.len;
Chao Yud84d1cb2017-04-18 19:27:39 +0800958 dcc->undiscard_blks += di.len;
Chao Yuba48a332017-04-15 14:09:37 +0800959 __relocate_discard_cmd(dcc, prev_dc);
Chao Yu004b6862017-04-14 23:24:55 +0800960 di = prev_dc->di;
961 tdc = prev_dc;
962 merged = true;
963 }
964
965 if (next_dc && next_dc->state == D_PREP &&
966 next_dc->bdev == bdev &&
967 __is_discard_front_mergeable(&di, &next_dc->di)) {
968 next_dc->di.lstart = di.lstart;
969 next_dc->di.len += di.len;
970 next_dc->di.start = di.start;
Chao Yud84d1cb2017-04-18 19:27:39 +0800971 dcc->undiscard_blks += di.len;
Chao Yuba48a332017-04-15 14:09:37 +0800972 __relocate_discard_cmd(dcc, next_dc);
Chao Yu004b6862017-04-14 23:24:55 +0800973 if (tdc)
974 __remove_discard_cmd(sbi, tdc);
Chao Yu004b6862017-04-14 23:24:55 +0800975 merged = true;
976 }
977
Chao Yudf0f6b42017-04-17 18:21:43 +0800978 if (!merged) {
Chao Yu004b6862017-04-14 23:24:55 +0800979 __insert_discard_tree(sbi, bdev, di.lstart, di.start,
980 di.len, NULL, NULL);
Chao Yudf0f6b42017-04-17 18:21:43 +0800981 }
Chao Yu004b6862017-04-14 23:24:55 +0800982 next:
983 prev_dc = next_dc;
984 if (!prev_dc)
985 break;
986
987 node = rb_next(&prev_dc->rb_node);
988 next_dc = rb_entry_safe(node, struct discard_cmd, rb_node);
989 }
990
991 mutex_unlock(&dcc->cmd_lock);
992}
993
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800994static int __queue_discard_cmd(struct f2fs_sb_info *sbi,
995 struct block_device *bdev, block_t blkstart, block_t blklen)
996{
997 block_t lblkstart = blkstart;
998
Chao Yu0243a5f2017-04-15 14:09:38 +0800999 trace_f2fs_queue_discard(bdev, blkstart, blklen);
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001000
1001 if (sbi->s_ndevs) {
1002 int devi = f2fs_target_device_index(sbi, blkstart);
1003
1004 blkstart -= FDEV(devi).start_blk;
1005 }
Chao Yu004b6862017-04-14 23:24:55 +08001006 __update_discard_tree_range(sbi, bdev, lblkstart, blkstart, blklen);
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001007 return 0;
1008}
1009
Chao Yubd5b0732017-04-25 20:21:37 +08001010static void __issue_discard_cmd(struct f2fs_sb_info *sbi, bool issue_cond)
1011{
1012 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1013 struct list_head *pend_list;
1014 struct discard_cmd *dc, *tmp;
1015 struct blk_plug plug;
1016 int i, iter = 0;
1017
1018 mutex_lock(&dcc->cmd_lock);
Yunlei He963932a2017-05-19 14:42:12 +08001019 f2fs_bug_on(sbi,
1020 !__check_rb_tree_consistence(sbi, &dcc->root));
Chao Yubd5b0732017-04-25 20:21:37 +08001021 blk_start_plug(&plug);
1022 for (i = MAX_PLIST_NUM - 1; i >= 0; i--) {
1023 pend_list = &dcc->pend_list[i];
1024 list_for_each_entry_safe(dc, tmp, pend_list, list) {
1025 f2fs_bug_on(sbi, dc->state != D_PREP);
1026
1027 if (!issue_cond || is_idle(sbi))
1028 __submit_discard_cmd(sbi, dc);
1029 if (issue_cond && iter++ > DISCARD_ISSUE_RATE)
1030 goto out;
1031 }
1032 }
1033out:
1034 blk_finish_plug(&plug);
1035 mutex_unlock(&dcc->cmd_lock);
1036}
1037
Chao Yu2a510c002017-06-05 18:29:06 +08001038static void __wait_one_discard_bio(struct f2fs_sb_info *sbi,
1039 struct discard_cmd *dc)
1040{
1041 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1042
1043 wait_for_completion_io(&dc->wait);
1044 mutex_lock(&dcc->cmd_lock);
1045 f2fs_bug_on(sbi, dc->state != D_DONE);
1046 dc->ref--;
1047 if (!dc->ref)
1048 __remove_discard_cmd(sbi, dc);
1049 mutex_unlock(&dcc->cmd_lock);
1050}
1051
Chao Yu63a94fa2017-04-25 20:21:38 +08001052static void __wait_discard_cmd(struct f2fs_sb_info *sbi, bool wait_cond)
1053{
1054 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1055 struct list_head *wait_list = &(dcc->wait_list);
1056 struct discard_cmd *dc, *tmp;
Chao Yu6afae632017-05-19 23:46:45 +08001057 bool need_wait;
1058
1059next:
1060 need_wait = false;
Chao Yu63a94fa2017-04-25 20:21:38 +08001061
1062 mutex_lock(&dcc->cmd_lock);
1063 list_for_each_entry_safe(dc, tmp, wait_list, list) {
Chao Yu6afae632017-05-19 23:46:45 +08001064 if (!wait_cond || (dc->state == D_DONE && !dc->ref)) {
Chao Yu63a94fa2017-04-25 20:21:38 +08001065 wait_for_completion_io(&dc->wait);
1066 __remove_discard_cmd(sbi, dc);
Chao Yu6afae632017-05-19 23:46:45 +08001067 } else {
1068 dc->ref++;
1069 need_wait = true;
1070 break;
Chao Yu63a94fa2017-04-25 20:21:38 +08001071 }
1072 }
1073 mutex_unlock(&dcc->cmd_lock);
Chao Yu6afae632017-05-19 23:46:45 +08001074
1075 if (need_wait) {
Chao Yu2a510c002017-06-05 18:29:06 +08001076 __wait_one_discard_bio(sbi, dc);
Chao Yu6afae632017-05-19 23:46:45 +08001077 goto next;
1078 }
Chao Yu63a94fa2017-04-25 20:21:38 +08001079}
1080
Jaegeuk Kim4e6a8d92016-12-29 14:07:53 -08001081/* This should be covered by global mutex, &sit_i->sentry_lock */
1082void f2fs_wait_discard_bio(struct f2fs_sb_info *sbi, block_t blkaddr)
Chao Yu275b66b2016-08-29 23:58:34 +08001083{
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001084 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Chao Yu004b6862017-04-14 23:24:55 +08001085 struct discard_cmd *dc;
Chao Yuec9895a2017-04-26 17:39:54 +08001086 bool need_wait = false;
Chao Yu275b66b2016-08-29 23:58:34 +08001087
Jaegeuk Kim15469962017-01-09 20:32:07 -08001088 mutex_lock(&dcc->cmd_lock);
Chao Yu004b6862017-04-14 23:24:55 +08001089 dc = (struct discard_cmd *)__lookup_rb_tree(&dcc->root, NULL, blkaddr);
1090 if (dc) {
Chao Yuec9895a2017-04-26 17:39:54 +08001091 if (dc->state == D_PREP) {
1092 __punch_discard_cmd(sbi, dc, blkaddr);
1093 } else {
1094 dc->ref++;
1095 need_wait = true;
1096 }
Chao Yu275b66b2016-08-29 23:58:34 +08001097 }
Chao Yud4314132017-04-05 18:19:49 +08001098 mutex_unlock(&dcc->cmd_lock);
Chao Yuec9895a2017-04-26 17:39:54 +08001099
Chao Yu2a510c002017-06-05 18:29:06 +08001100 if (need_wait)
1101 __wait_one_discard_bio(sbi, dc);
Chao Yud4314132017-04-05 18:19:49 +08001102}
Chao Yu22d375d2017-04-05 18:19:48 +08001103
Chao Yucce13252017-06-29 23:17:45 +08001104void stop_discard_thread(struct f2fs_sb_info *sbi)
1105{
1106 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1107
1108 if (dcc && dcc->f2fs_issue_discard) {
1109 struct task_struct *discard_thread = dcc->f2fs_issue_discard;
1110
1111 dcc->f2fs_issue_discard = NULL;
1112 kthread_stop(discard_thread);
Jaegeuk Kim15469962017-01-09 20:32:07 -08001113 }
1114}
1115
1116/* This comes from f2fs_put_super */
Chao Yubd5b0732017-04-25 20:21:37 +08001117void f2fs_wait_discard_bios(struct f2fs_sb_info *sbi)
Chao Yu63a94fa2017-04-25 20:21:38 +08001118{
Jaegeuk Kim15469962017-01-09 20:32:07 -08001119 __issue_discard_cmd(sbi, false);
Jaegeuk Kim15469962017-01-09 20:32:07 -08001120 __wait_discard_cmd(sbi, false);
1121}
Chao Yu22d375d2017-04-05 18:19:48 +08001122
Chao Yuba48a332017-04-15 14:09:37 +08001123static int issue_discard_thread(void *data)
Jaegeuk Kim15469962017-01-09 20:32:07 -08001124{
1125 struct f2fs_sb_info *sbi = data;
1126 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Damien Le Moalf46e88092016-10-28 17:45:06 +09001127 wait_queue_head_t *q = &dcc->discard_wait_queue;
Chao Yu275b66b2016-08-29 23:58:34 +08001128
Jaegeuk Kim1d7be272017-05-17 10:36:58 -07001129 set_freezable();
Chao Yu275b66b2016-08-29 23:58:34 +08001130
Jaegeuk Kim1d7be272017-05-17 10:36:58 -07001131 do {
1132 wait_event_interruptible(*q, kthread_should_stop() ||
1133 freezing(current) ||
1134 atomic_read(&dcc->discard_cmd_cnt));
1135 if (try_to_freeze())
1136 continue;
1137 if (kthread_should_stop())
1138 return 0;
Chao Yu275b66b2016-08-29 23:58:34 +08001139
Chao Yudc6febb2017-07-22 08:52:23 +08001140 sb_start_intwrite(sbi->sb);
1141
Jaegeuk Kim1d7be272017-05-17 10:36:58 -07001142 __issue_discard_cmd(sbi, true);
1143 __wait_discard_cmd(sbi, true);
1144
Chao Yudc6febb2017-07-22 08:52:23 +08001145 sb_end_intwrite(sbi->sb);
1146
Jaegeuk Kim1d7be272017-05-17 10:36:58 -07001147 congestion_wait(BLK_RW_SYNC, HZ/50);
1148 } while (!kthread_should_stop());
1149 return 0;
Chao Yu275b66b2016-08-29 23:58:34 +08001150}
1151
Damien Le Moalf46e88092016-10-28 17:45:06 +09001152#ifdef CONFIG_BLK_DEV_ZONED
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001153static int __f2fs_issue_discard_zone(struct f2fs_sb_info *sbi,
1154 struct block_device *bdev, block_t blkstart, block_t blklen)
Damien Le Moalf46e88092016-10-28 17:45:06 +09001155{
Jaegeuk Kim925922852017-02-22 20:18:35 -08001156 sector_t sector, nr_sects;
Kinglong Mee10a875f2017-03-08 09:49:53 +08001157 block_t lblkstart = blkstart;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001158 int devi = 0;
Damien Le Moalf46e88092016-10-28 17:45:06 +09001159
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001160 if (sbi->s_ndevs) {
1161 devi = f2fs_target_device_index(sbi, blkstart);
1162 blkstart -= FDEV(devi).start_blk;
1163 }
Damien Le Moalf46e88092016-10-28 17:45:06 +09001164
1165 /*
1166 * We need to know the type of the zone: for conventional zones,
1167 * use regular discard if the drive supports it. For sequential
1168 * zones, reset the zone write pointer.
1169 */
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001170 switch (get_blkz_type(sbi, bdev, blkstart)) {
Damien Le Moalf46e88092016-10-28 17:45:06 +09001171
1172 case BLK_ZONE_TYPE_CONVENTIONAL:
1173 if (!blk_queue_discard(bdev_get_queue(bdev)))
1174 return 0;
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001175 return __queue_discard_cmd(sbi, bdev, lblkstart, blklen);
Damien Le Moalf46e88092016-10-28 17:45:06 +09001176 case BLK_ZONE_TYPE_SEQWRITE_REQ:
1177 case BLK_ZONE_TYPE_SEQWRITE_PREF:
Jaegeuk Kim925922852017-02-22 20:18:35 -08001178 sector = SECTOR_FROM_BLOCK(blkstart);
1179 nr_sects = SECTOR_FROM_BLOCK(blklen);
1180
1181 if (sector & (bdev_zone_sectors(bdev) - 1) ||
1182 nr_sects != bdev_zone_sectors(bdev)) {
1183 f2fs_msg(sbi->sb, KERN_INFO,
1184 "(%d) %s: Unaligned discard attempted (block %x + %x)",
1185 devi, sbi->s_ndevs ? FDEV(devi).path: "",
1186 blkstart, blklen);
1187 return -EIO;
1188 }
Jaegeuk Kimd50aaee2017-02-15 11:14:06 -08001189 trace_f2fs_issue_reset_zone(bdev, blkstart);
Damien Le Moalf46e88092016-10-28 17:45:06 +09001190 return blkdev_reset_zones(bdev, sector,
1191 nr_sects, GFP_NOFS);
1192 default:
1193 /* Unknown zone type: broken device ? */
1194 return -EIO;
1195 }
1196}
1197#endif
1198
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001199static int __issue_discard_async(struct f2fs_sb_info *sbi,
1200 struct block_device *bdev, block_t blkstart, block_t blklen)
1201{
1202#ifdef CONFIG_BLK_DEV_ZONED
1203 if (f2fs_sb_mounted_blkzoned(sbi->sb) &&
1204 bdev_zoned_model(bdev) != BLK_ZONED_NONE)
1205 return __f2fs_issue_discard_zone(sbi, bdev, blkstart, blklen);
1206#endif
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001207 return __queue_discard_cmd(sbi, bdev, blkstart, blklen);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001208}
1209
Jaegeuk Kim1e87a782014-04-15 13:57:55 +09001210static int f2fs_issue_discard(struct f2fs_sb_info *sbi,
Jaegeuk Kim37208872013-11-12 16:55:17 +09001211 block_t blkstart, block_t blklen)
1212{
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001213 sector_t start = blkstart, len = 0;
1214 struct block_device *bdev;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001215 struct seg_entry *se;
1216 unsigned int offset;
1217 block_t i;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001218 int err = 0;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001219
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001220 bdev = f2fs_target_device(sbi, blkstart, NULL);
1221
1222 for (i = blkstart; i < blkstart + blklen; i++, len++) {
1223 if (i != start) {
1224 struct block_device *bdev2 =
1225 f2fs_target_device(sbi, i, NULL);
1226
1227 if (bdev2 != bdev) {
1228 err = __issue_discard_async(sbi, bdev,
1229 start, len);
1230 if (err)
1231 return err;
1232 bdev = bdev2;
1233 start = i;
1234 len = 0;
1235 }
1236 }
1237
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001238 se = get_seg_entry(sbi, GET_SEGNO(sbi, i));
1239 offset = GET_BLKOFF_FROM_SEG0(sbi, i);
1240
1241 if (!f2fs_test_and_set_bit(offset, se->discard_map))
1242 sbi->discard_blks--;
1243 }
Damien Le Moalf46e88092016-10-28 17:45:06 +09001244
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001245 if (len)
1246 err = __issue_discard_async(sbi, bdev, start, len);
1247 return err;
Jaegeuk Kim1e87a782014-04-15 13:57:55 +09001248}
1249
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08001250static bool add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc,
1251 bool check_only)
Jaegeuk Kimadf49832014-10-28 22:27:59 -07001252{
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001253 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
1254 int max_blocks = sbi->blocks_per_seg;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001255 struct seg_entry *se = get_seg_entry(sbi, cpc->trim_start);
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001256 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
1257 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001258 unsigned long *discard_map = (unsigned long *)se->discard_map;
Jaegeuk Kim60a3b782015-02-10 16:44:29 -08001259 unsigned long *dmap = SIT_I(sbi)->tmp_map;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001260 unsigned int start = 0, end = -1;
Chao Yuc473f1a2017-04-27 20:40:39 +08001261 bool force = (cpc->reason & CP_DISCARD);
Chao Yua7eeb8232017-03-28 18:18:50 +08001262 struct discard_entry *de = NULL;
Chao Yu46f84c22017-04-15 14:09:36 +08001263 struct list_head *head = &SM_I(sbi)->dcc_info->entry_list;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001264 int i;
1265
Jaegeuk Kim3e025742016-08-02 10:56:40 -07001266 if (se->valid_blocks == max_blocks || !f2fs_discard_en(sbi))
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08001267 return false;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001268
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001269 if (!force) {
1270 if (!test_opt(sbi, DISCARD) || !se->valid_blocks ||
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001271 SM_I(sbi)->dcc_info->nr_discards >=
1272 SM_I(sbi)->dcc_info->max_discards)
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08001273 return false;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001274 }
1275
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001276 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
1277 for (i = 0; i < entries; i++)
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001278 dmap[i] = force ? ~ckpt_map[i] & ~discard_map[i] :
Jaegeuk Kimd7bc2482014-12-12 13:53:41 -08001279 (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001280
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001281 while (force || SM_I(sbi)->dcc_info->nr_discards <=
1282 SM_I(sbi)->dcc_info->max_discards) {
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001283 start = __find_rev_next_bit(dmap, max_blocks, end + 1);
1284 if (start >= max_blocks)
1285 break;
1286
1287 end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
Yunlei Hec7b41e12016-07-07 12:13:33 +08001288 if (force && start && end != max_blocks
1289 && (end - start) < cpc->trim_minlen)
1290 continue;
1291
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08001292 if (check_only)
1293 return true;
1294
Chao Yua7eeb8232017-03-28 18:18:50 +08001295 if (!de) {
1296 de = f2fs_kmem_cache_alloc(discard_entry_slab,
1297 GFP_F2FS_ZERO);
1298 de->start_blkaddr = START_BLOCK(sbi, cpc->trim_start);
1299 list_add_tail(&de->list, head);
1300 }
1301
1302 for (i = start; i < end; i++)
1303 __set_bit_le(i, (void *)de->discard_map);
1304
1305 SM_I(sbi)->dcc_info->nr_discards += end - start;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001306 }
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08001307 return false;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001308}
1309
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001310void release_discard_addrs(struct f2fs_sb_info *sbi)
1311{
Chao Yu46f84c22017-04-15 14:09:36 +08001312 struct list_head *head = &(SM_I(sbi)->dcc_info->entry_list);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001313 struct discard_entry *entry, *this;
1314
1315 /* drop caches */
1316 list_for_each_entry_safe(entry, this, head, list) {
1317 list_del(&entry->list);
1318 kmem_cache_free(discard_entry_slab, entry);
1319 }
1320}
1321
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001322/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001323 * Should call clear_prefree_segments after checkpoint is done.
1324 */
1325static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
1326{
1327 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Chao Yub65ee142014-08-04 10:10:07 +08001328 unsigned int segno;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001329
1330 mutex_lock(&dirty_i->seglist_lock);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001331 for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001332 __set_test_and_free(sbi, segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001333 mutex_unlock(&dirty_i->seglist_lock);
1334}
1335
Jaegeuk Kim836b5a62015-04-30 22:50:06 -07001336void clear_prefree_segments(struct f2fs_sb_info *sbi, struct cp_control *cpc)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001337{
Chao Yu46f84c22017-04-15 14:09:36 +08001338 struct list_head *head = &(SM_I(sbi)->dcc_info->entry_list);
Chao Yu2d7b8222014-03-29 11:33:17 +08001339 struct discard_entry *entry, *this;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001340 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Changman Lee29e59c12013-11-11 09:24:37 +09001341 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
Changman Lee29e59c12013-11-11 09:24:37 +09001342 unsigned int start = 0, end = -1;
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001343 unsigned int secno, start_segno;
Chao Yuc473f1a2017-04-27 20:40:39 +08001344 bool force = (cpc->reason & CP_DISCARD);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001345
1346 mutex_lock(&dirty_i->seglist_lock);
Changman Lee29e59c12013-11-11 09:24:37 +09001347
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001348 while (1) {
Changman Lee29e59c12013-11-11 09:24:37 +09001349 int i;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001350 start = find_next_bit(prefree_map, MAIN_SEGS(sbi), end + 1);
1351 if (start >= MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001352 break;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001353 end = find_next_zero_bit(prefree_map, MAIN_SEGS(sbi),
1354 start + 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001355
Changman Lee29e59c12013-11-11 09:24:37 +09001356 for (i = start; i < end; i++)
1357 clear_bit(i, prefree_map);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001358
Changman Lee29e59c12013-11-11 09:24:37 +09001359 dirty_i->nr_dirty[PRE] -= end - start;
1360
Yunlei He650d3c42016-12-22 11:46:24 +08001361 if (!test_opt(sbi, DISCARD))
Changman Lee29e59c12013-11-11 09:24:37 +09001362 continue;
1363
Yunlei He650d3c42016-12-22 11:46:24 +08001364 if (force && start >= cpc->trim_start &&
1365 (end - 1) <= cpc->trim_end)
1366 continue;
1367
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001368 if (!test_opt(sbi, LFS) || sbi->segs_per_sec == 1) {
1369 f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
Jaegeuk Kim37208872013-11-12 16:55:17 +09001370 (end - start) << sbi->log_blocks_per_seg);
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001371 continue;
1372 }
1373next:
Jaegeuk Kim4ddb1a42017-04-07 15:08:17 -07001374 secno = GET_SEC_FROM_SEG(sbi, start);
1375 start_segno = GET_SEG_FROM_SEC(sbi, secno);
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001376 if (!IS_CURSEC(sbi, secno) &&
Jaegeuk Kim302bd342017-04-07 14:33:22 -07001377 !get_valid_blocks(sbi, start, true))
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001378 f2fs_issue_discard(sbi, START_BLOCK(sbi, start_segno),
1379 sbi->segs_per_sec << sbi->log_blocks_per_seg);
1380
1381 start = start_segno + sbi->segs_per_sec;
1382 if (start < end)
1383 goto next;
Jaegeuk Kim8b107f52017-02-27 11:57:11 -08001384 else
1385 end = start - 1;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001386 }
1387 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001388
1389 /* send small discards */
Chao Yu2d7b8222014-03-29 11:33:17 +08001390 list_for_each_entry_safe(entry, this, head, list) {
Chao Yua7eeb8232017-03-28 18:18:50 +08001391 unsigned int cur_pos = 0, next_pos, len, total_len = 0;
1392 bool is_valid = test_bit_le(0, entry->discard_map);
1393
1394find_next:
1395 if (is_valid) {
1396 next_pos = find_next_zero_bit_le(entry->discard_map,
1397 sbi->blocks_per_seg, cur_pos);
1398 len = next_pos - cur_pos;
1399
Damien Le Moalacfd28102017-05-26 17:04:40 +09001400 if (f2fs_sb_mounted_blkzoned(sbi->sb) ||
1401 (force && len < cpc->trim_minlen))
Chao Yua7eeb8232017-03-28 18:18:50 +08001402 goto skip;
1403
1404 f2fs_issue_discard(sbi, entry->start_blkaddr + cur_pos,
1405 len);
1406 cpc->trimmed += len;
1407 total_len += len;
1408 } else {
1409 next_pos = find_next_bit_le(entry->discard_map,
1410 sbi->blocks_per_seg, cur_pos);
1411 }
Jaegeuk Kim836b5a62015-04-30 22:50:06 -07001412skip:
Chao Yua7eeb8232017-03-28 18:18:50 +08001413 cur_pos = next_pos;
1414 is_valid = !is_valid;
1415
1416 if (cur_pos < sbi->blocks_per_seg)
1417 goto find_next;
1418
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001419 list_del(&entry->list);
Chao Yua7eeb8232017-03-28 18:18:50 +08001420 SM_I(sbi)->dcc_info->nr_discards -= total_len;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001421 kmem_cache_free(discard_entry_slab, entry);
1422 }
Chao Yu34e159d2017-04-25 00:21:34 +08001423
1424 wake_up(&SM_I(sbi)->dcc_info->discard_wait_queue);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001425}
1426
Jaegeuk Kim8ed59742017-01-29 14:27:02 +09001427static int create_discard_cmd_control(struct f2fs_sb_info *sbi)
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001428{
Jaegeuk Kim15469962017-01-09 20:32:07 -08001429 dev_t dev = sbi->sb->s_bdev->bd_dev;
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001430 struct discard_cmd_control *dcc;
Chao Yuba48a332017-04-15 14:09:37 +08001431 int err = 0, i;
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001432
1433 if (SM_I(sbi)->dcc_info) {
1434 dcc = SM_I(sbi)->dcc_info;
1435 goto init_thread;
1436 }
1437
1438 dcc = kzalloc(sizeof(struct discard_cmd_control), GFP_KERNEL);
1439 if (!dcc)
1440 return -ENOMEM;
1441
Chao Yu46f84c22017-04-15 14:09:36 +08001442 INIT_LIST_HEAD(&dcc->entry_list);
Chao Yuba48a332017-04-15 14:09:37 +08001443 for (i = 0; i < MAX_PLIST_NUM; i++)
1444 INIT_LIST_HEAD(&dcc->pend_list[i]);
Chao Yu46f84c22017-04-15 14:09:36 +08001445 INIT_LIST_HEAD(&dcc->wait_list);
Jaegeuk Kim15469962017-01-09 20:32:07 -08001446 mutex_init(&dcc->cmd_lock);
Chao Yu8b8dd652017-03-25 17:19:58 +08001447 atomic_set(&dcc->issued_discard, 0);
1448 atomic_set(&dcc->issing_discard, 0);
Chao Yu5f323662017-03-25 17:19:59 +08001449 atomic_set(&dcc->discard_cmd_cnt, 0);
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001450 dcc->nr_discards = 0;
Chao Yud618eba2017-04-25 00:21:35 +08001451 dcc->max_discards = MAIN_SEGS(sbi) << sbi->log_blocks_per_seg;
Chao Yud84d1cb2017-04-18 19:27:39 +08001452 dcc->undiscard_blks = 0;
Chao Yu004b6862017-04-14 23:24:55 +08001453 dcc->root = RB_ROOT;
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001454
Jaegeuk Kim15469962017-01-09 20:32:07 -08001455 init_waitqueue_head(&dcc->discard_wait_queue);
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001456 SM_I(sbi)->dcc_info = dcc;
1457init_thread:
Jaegeuk Kim15469962017-01-09 20:32:07 -08001458 dcc->f2fs_issue_discard = kthread_run(issue_discard_thread, sbi,
1459 "f2fs_discard-%u:%u", MAJOR(dev), MINOR(dev));
1460 if (IS_ERR(dcc->f2fs_issue_discard)) {
1461 err = PTR_ERR(dcc->f2fs_issue_discard);
1462 kfree(dcc);
1463 SM_I(sbi)->dcc_info = NULL;
1464 return err;
1465 }
1466
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001467 return err;
1468}
1469
Chao Yuf0994052017-03-27 18:14:04 +08001470static void destroy_discard_cmd_control(struct f2fs_sb_info *sbi)
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001471{
1472 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1473
Chao Yuf0994052017-03-27 18:14:04 +08001474 if (!dcc)
1475 return;
1476
Chao Yucce13252017-06-29 23:17:45 +08001477 stop_discard_thread(sbi);
Chao Yuf0994052017-03-27 18:14:04 +08001478
1479 kfree(dcc);
1480 SM_I(sbi)->dcc_info = NULL;
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001481}
1482
Chao Yu184a5cd2014-09-04 18:13:01 +08001483static bool __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001484{
1485 struct sit_info *sit_i = SIT_I(sbi);
Chao Yu184a5cd2014-09-04 18:13:01 +08001486
1487 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001488 sit_i->dirty_sentries++;
Chao Yu184a5cd2014-09-04 18:13:01 +08001489 return false;
1490 }
1491
1492 return true;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001493}
1494
1495static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
1496 unsigned int segno, int modified)
1497{
1498 struct seg_entry *se = get_seg_entry(sbi, segno);
1499 se->type = type;
1500 if (modified)
1501 __mark_sit_entry_dirty(sbi, segno);
1502}
1503
1504static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
1505{
1506 struct seg_entry *se;
1507 unsigned int segno, offset;
1508 long int new_vblocks;
Yunlong Song6415fed2017-08-02 21:20:13 +08001509 bool exist;
1510#ifdef CONFIG_F2FS_CHECK_FS
1511 bool mir_exist;
1512#endif
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001513
1514 segno = GET_SEGNO(sbi, blkaddr);
1515
1516 se = get_seg_entry(sbi, segno);
1517 new_vblocks = se->valid_blocks + del;
Jaegeuk Kim491c0852014-02-04 13:01:10 +09001518 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001519
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001520 f2fs_bug_on(sbi, (new_vblocks >> (sizeof(unsigned short) << 3) ||
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001521 (new_vblocks > sbi->blocks_per_seg)));
1522
1523 se->valid_blocks = new_vblocks;
1524 se->mtime = get_mtime(sbi);
1525 SIT_I(sbi)->max_mtime = se->mtime;
1526
1527 /* Update valid block bitmap */
1528 if (del > 0) {
Yunlong Song6415fed2017-08-02 21:20:13 +08001529 exist = f2fs_test_and_set_bit(offset, se->cur_valid_map);
Chao Yu355e7892017-01-07 18:51:01 +08001530#ifdef CONFIG_F2FS_CHECK_FS
Yunlong Song6415fed2017-08-02 21:20:13 +08001531 mir_exist = f2fs_test_and_set_bit(offset,
1532 se->cur_valid_map_mir);
1533 if (unlikely(exist != mir_exist)) {
1534 f2fs_msg(sbi->sb, KERN_ERR, "Inconsistent error "
1535 "when setting bitmap, blk:%u, old bit:%d",
1536 blkaddr, exist);
Jaegeuk Kim05796762014-09-02 16:05:00 -07001537 f2fs_bug_on(sbi, 1);
Chao Yu355e7892017-01-07 18:51:01 +08001538 }
Yunlong Song6415fed2017-08-02 21:20:13 +08001539#endif
1540 if (unlikely(exist)) {
1541 f2fs_msg(sbi->sb, KERN_ERR,
1542 "Bitmap was wrongly set, blk:%u", blkaddr);
1543 f2fs_bug_on(sbi, 1);
Yunlong Song35ee82c2017-08-02 22:16:54 +08001544 se->valid_blocks--;
1545 del = 0;
Yunlong Song6415fed2017-08-02 21:20:13 +08001546 }
1547
Jaegeuk Kim3e025742016-08-02 10:56:40 -07001548 if (f2fs_discard_en(sbi) &&
1549 !f2fs_test_and_set_bit(offset, se->discard_map))
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001550 sbi->discard_blks--;
Jaegeuk Kim720037f2017-03-06 11:59:56 -08001551
1552 /* don't overwrite by SSR to keep node chain */
1553 if (se->type == CURSEG_WARM_NODE) {
1554 if (!f2fs_test_and_set_bit(offset, se->ckpt_valid_map))
1555 se->ckpt_valid_blocks++;
1556 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001557 } else {
Yunlong Song6415fed2017-08-02 21:20:13 +08001558 exist = f2fs_test_and_clear_bit(offset, se->cur_valid_map);
Chao Yu355e7892017-01-07 18:51:01 +08001559#ifdef CONFIG_F2FS_CHECK_FS
Yunlong Song6415fed2017-08-02 21:20:13 +08001560 mir_exist = f2fs_test_and_clear_bit(offset,
1561 se->cur_valid_map_mir);
1562 if (unlikely(exist != mir_exist)) {
1563 f2fs_msg(sbi->sb, KERN_ERR, "Inconsistent error "
1564 "when clearing bitmap, blk:%u, old bit:%d",
1565 blkaddr, exist);
Jaegeuk Kim05796762014-09-02 16:05:00 -07001566 f2fs_bug_on(sbi, 1);
Chao Yu355e7892017-01-07 18:51:01 +08001567 }
Yunlong Song6415fed2017-08-02 21:20:13 +08001568#endif
1569 if (unlikely(!exist)) {
1570 f2fs_msg(sbi->sb, KERN_ERR,
1571 "Bitmap was wrongly cleared, blk:%u", blkaddr);
1572 f2fs_bug_on(sbi, 1);
Yunlong Song35ee82c2017-08-02 22:16:54 +08001573 se->valid_blocks++;
1574 del = 0;
Yunlong Song6415fed2017-08-02 21:20:13 +08001575 }
1576
Jaegeuk Kim3e025742016-08-02 10:56:40 -07001577 if (f2fs_discard_en(sbi) &&
1578 f2fs_test_and_clear_bit(offset, se->discard_map))
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001579 sbi->discard_blks++;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001580 }
1581 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
1582 se->ckpt_valid_blocks += del;
1583
1584 __mark_sit_entry_dirty(sbi, segno);
1585
1586 /* update total number of valid blocks to be written in ckpt area */
1587 SIT_I(sbi)->written_valid_blocks += del;
1588
1589 if (sbi->segs_per_sec > 1)
1590 get_sec_entry(sbi, segno)->valid_blocks += del;
1591}
1592
Jaegeuk Kim5e443812014-01-28 12:22:14 +09001593void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001594{
Jaegeuk Kim5e443812014-01-28 12:22:14 +09001595 update_sit_entry(sbi, new, 1);
1596 if (GET_SEGNO(sbi, old) != NULL_SEGNO)
1597 update_sit_entry(sbi, old, -1);
1598
1599 locate_dirty_segment(sbi, GET_SEGNO(sbi, old));
1600 locate_dirty_segment(sbi, GET_SEGNO(sbi, new));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001601}
1602
1603void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
1604{
1605 unsigned int segno = GET_SEGNO(sbi, addr);
1606 struct sit_info *sit_i = SIT_I(sbi);
1607
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001608 f2fs_bug_on(sbi, addr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001609 if (addr == NEW_ADDR)
1610 return;
1611
1612 /* add it into sit main buffer */
1613 mutex_lock(&sit_i->sentry_lock);
1614
1615 update_sit_entry(sbi, addr, -1);
1616
1617 /* add it into dirty seglist */
1618 locate_dirty_segment(sbi, segno);
1619
1620 mutex_unlock(&sit_i->sentry_lock);
1621}
1622
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001623bool is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr)
1624{
1625 struct sit_info *sit_i = SIT_I(sbi);
1626 unsigned int segno, offset;
1627 struct seg_entry *se;
1628 bool is_cp = false;
1629
1630 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR)
1631 return true;
1632
1633 mutex_lock(&sit_i->sentry_lock);
1634
1635 segno = GET_SEGNO(sbi, blkaddr);
1636 se = get_seg_entry(sbi, segno);
1637 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
1638
1639 if (f2fs_test_bit(offset, se->ckpt_valid_map))
1640 is_cp = true;
1641
1642 mutex_unlock(&sit_i->sentry_lock);
1643
1644 return is_cp;
1645}
1646
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001647/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001648 * This function should be resided under the curseg_mutex lock
1649 */
1650static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
Haicheng Lie79efe32013-06-13 16:59:27 +08001651 struct f2fs_summary *sum)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001652{
1653 struct curseg_info *curseg = CURSEG_I(sbi, type);
1654 void *addr = curseg->sum_blk;
Haicheng Lie79efe32013-06-13 16:59:27 +08001655 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001656 memcpy(addr, sum, sizeof(struct f2fs_summary));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001657}
1658
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001659/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001660 * Calculate the number of current summary pages for writing
1661 */
Chao Yu3fa06d72014-12-09 14:21:46 +08001662int npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001663{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001664 int valid_sum_count = 0;
Fan Li9a479382013-10-29 16:21:47 +08001665 int i, sum_in_page;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001666
1667 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1668 if (sbi->ckpt->alloc_type[i] == SSR)
1669 valid_sum_count += sbi->blocks_per_seg;
Chao Yu3fa06d72014-12-09 14:21:46 +08001670 else {
1671 if (for_ra)
1672 valid_sum_count += le16_to_cpu(
1673 F2FS_CKPT(sbi)->cur_data_blkoff[i]);
1674 else
1675 valid_sum_count += curseg_blkoff(sbi, i);
1676 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001677 }
1678
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001679 sum_in_page = (PAGE_SIZE - 2 * SUM_JOURNAL_SIZE -
Fan Li9a479382013-10-29 16:21:47 +08001680 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
1681 if (valid_sum_count <= sum_in_page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001682 return 1;
Fan Li9a479382013-10-29 16:21:47 +08001683 else if ((valid_sum_count - sum_in_page) <=
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001684 (PAGE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001685 return 2;
1686 return 3;
1687}
1688
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001689/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001690 * Caller should put this summary page
1691 */
1692struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
1693{
1694 return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
1695}
1696
Chao Yu381722d2015-05-19 17:40:04 +08001697void update_meta_page(struct f2fs_sb_info *sbi, void *src, block_t blk_addr)
1698{
1699 struct page *page = grab_meta_page(sbi, blk_addr);
1700 void *dst = page_address(page);
1701
1702 if (src)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001703 memcpy(dst, src, PAGE_SIZE);
Chao Yu381722d2015-05-19 17:40:04 +08001704 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001705 memset(dst, 0, PAGE_SIZE);
Chao Yu381722d2015-05-19 17:40:04 +08001706 set_page_dirty(page);
1707 f2fs_put_page(page, 1);
1708}
1709
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001710static void write_sum_page(struct f2fs_sb_info *sbi,
1711 struct f2fs_summary_block *sum_blk, block_t blk_addr)
1712{
Chao Yu381722d2015-05-19 17:40:04 +08001713 update_meta_page(sbi, (void *)sum_blk, blk_addr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001714}
1715
Chao Yub7ad7512016-02-19 18:08:46 +08001716static void write_current_sum_page(struct f2fs_sb_info *sbi,
1717 int type, block_t blk_addr)
1718{
1719 struct curseg_info *curseg = CURSEG_I(sbi, type);
1720 struct page *page = grab_meta_page(sbi, blk_addr);
1721 struct f2fs_summary_block *src = curseg->sum_blk;
1722 struct f2fs_summary_block *dst;
1723
1724 dst = (struct f2fs_summary_block *)page_address(page);
1725
1726 mutex_lock(&curseg->curseg_mutex);
1727
1728 down_read(&curseg->journal_rwsem);
1729 memcpy(&dst->journal, curseg->journal, SUM_JOURNAL_SIZE);
1730 up_read(&curseg->journal_rwsem);
1731
1732 memcpy(dst->entries, src->entries, SUM_ENTRY_SIZE);
1733 memcpy(&dst->footer, &src->footer, SUM_FOOTER_SIZE);
1734
1735 mutex_unlock(&curseg->curseg_mutex);
1736
1737 set_page_dirty(page);
1738 f2fs_put_page(page, 1);
1739}
1740
Jaegeuk Kima7881892017-04-20 13:51:57 -07001741static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
1742{
1743 struct curseg_info *curseg = CURSEG_I(sbi, type);
1744 unsigned int segno = curseg->segno + 1;
1745 struct free_segmap_info *free_i = FREE_I(sbi);
1746
1747 if (segno < MAIN_SEGS(sbi) && segno % sbi->segs_per_sec)
1748 return !test_bit(segno, free_i->free_segmap);
1749 return 0;
1750}
1751
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001752/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001753 * Find a new segment from the free segments bitmap to right order
1754 * This function should be returned with success, otherwise BUG
1755 */
1756static void get_new_segment(struct f2fs_sb_info *sbi,
1757 unsigned int *newseg, bool new_sec, int dir)
1758{
1759 struct free_segmap_info *free_i = FREE_I(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001760 unsigned int segno, secno, zoneno;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001761 unsigned int total_zones = MAIN_SECS(sbi) / sbi->secs_per_zone;
Jaegeuk Kim4ddb1a42017-04-07 15:08:17 -07001762 unsigned int hint = GET_SEC_FROM_SEG(sbi, *newseg);
1763 unsigned int old_zoneno = GET_ZONE_FROM_SEG(sbi, *newseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001764 unsigned int left_start = hint;
1765 bool init = true;
1766 int go_left = 0;
1767 int i;
1768
Chao Yu1a118cc2015-02-11 18:20:38 +08001769 spin_lock(&free_i->segmap_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001770
1771 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
1772 segno = find_next_zero_bit(free_i->free_segmap,
Jaegeuk Kim4ddb1a42017-04-07 15:08:17 -07001773 GET_SEG_FROM_SEC(sbi, hint + 1), *newseg + 1);
1774 if (segno < GET_SEG_FROM_SEC(sbi, hint + 1))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001775 goto got_it;
1776 }
1777find_other_zone:
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001778 secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint);
1779 if (secno >= MAIN_SECS(sbi)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001780 if (dir == ALLOC_RIGHT) {
1781 secno = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001782 MAIN_SECS(sbi), 0);
1783 f2fs_bug_on(sbi, secno >= MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001784 } else {
1785 go_left = 1;
1786 left_start = hint - 1;
1787 }
1788 }
1789 if (go_left == 0)
1790 goto skip_left;
1791
1792 while (test_bit(left_start, free_i->free_secmap)) {
1793 if (left_start > 0) {
1794 left_start--;
1795 continue;
1796 }
1797 left_start = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001798 MAIN_SECS(sbi), 0);
1799 f2fs_bug_on(sbi, left_start >= MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001800 break;
1801 }
1802 secno = left_start;
1803skip_left:
1804 hint = secno;
Jaegeuk Kim4ddb1a42017-04-07 15:08:17 -07001805 segno = GET_SEG_FROM_SEC(sbi, secno);
1806 zoneno = GET_ZONE_FROM_SEC(sbi, secno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001807
1808 /* give up on finding another zone */
1809 if (!init)
1810 goto got_it;
1811 if (sbi->secs_per_zone == 1)
1812 goto got_it;
1813 if (zoneno == old_zoneno)
1814 goto got_it;
1815 if (dir == ALLOC_LEFT) {
1816 if (!go_left && zoneno + 1 >= total_zones)
1817 goto got_it;
1818 if (go_left && zoneno == 0)
1819 goto got_it;
1820 }
1821 for (i = 0; i < NR_CURSEG_TYPE; i++)
1822 if (CURSEG_I(sbi, i)->zone == zoneno)
1823 break;
1824
1825 if (i < NR_CURSEG_TYPE) {
1826 /* zone is in user, try another */
1827 if (go_left)
1828 hint = zoneno * sbi->secs_per_zone - 1;
1829 else if (zoneno + 1 >= total_zones)
1830 hint = 0;
1831 else
1832 hint = (zoneno + 1) * sbi->secs_per_zone;
1833 init = false;
1834 goto find_other_zone;
1835 }
1836got_it:
1837 /* set it as dirty segment in free segmap */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001838 f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001839 __set_inuse(sbi, segno);
1840 *newseg = segno;
Chao Yu1a118cc2015-02-11 18:20:38 +08001841 spin_unlock(&free_i->segmap_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001842}
1843
1844static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
1845{
1846 struct curseg_info *curseg = CURSEG_I(sbi, type);
1847 struct summary_footer *sum_footer;
1848
1849 curseg->segno = curseg->next_segno;
Jaegeuk Kim4ddb1a42017-04-07 15:08:17 -07001850 curseg->zone = GET_ZONE_FROM_SEG(sbi, curseg->segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001851 curseg->next_blkoff = 0;
1852 curseg->next_segno = NULL_SEGNO;
1853
1854 sum_footer = &(curseg->sum_blk->footer);
1855 memset(sum_footer, 0, sizeof(struct summary_footer));
1856 if (IS_DATASEG(type))
1857 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
1858 if (IS_NODESEG(type))
1859 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
1860 __set_sit_entry_type(sbi, type, curseg->segno, modified);
1861}
1862
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -04001863static unsigned int __get_next_segno(struct f2fs_sb_info *sbi, int type)
1864{
Jaegeuk Kima7881892017-04-20 13:51:57 -07001865 /* if segs_per_sec is large than 1, we need to keep original policy. */
1866 if (sbi->segs_per_sec != 1)
1867 return CURSEG_I(sbi, type)->segno;
1868
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -04001869 if (type == CURSEG_HOT_DATA || IS_NODESEG(type))
1870 return 0;
1871
Jaegeuk Kime066b832017-04-13 15:17:00 -07001872 if (SIT_I(sbi)->last_victim[ALLOC_NEXT])
1873 return SIT_I(sbi)->last_victim[ALLOC_NEXT];
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -04001874 return CURSEG_I(sbi, type)->segno;
1875}
1876
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001877/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001878 * Allocate a current working segment.
1879 * This function always allocates a free segment in LFS manner.
1880 */
1881static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
1882{
1883 struct curseg_info *curseg = CURSEG_I(sbi, type);
1884 unsigned int segno = curseg->segno;
1885 int dir = ALLOC_LEFT;
1886
1887 write_sum_page(sbi, curseg->sum_blk,
Haicheng Li81fb5e82013-05-14 18:20:28 +08001888 GET_SUM_BLOCK(sbi, segno));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001889 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
1890 dir = ALLOC_RIGHT;
1891
1892 if (test_opt(sbi, NOHEAP))
1893 dir = ALLOC_RIGHT;
1894
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -04001895 segno = __get_next_segno(sbi, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001896 get_new_segment(sbi, &segno, new_sec, dir);
1897 curseg->next_segno = segno;
1898 reset_curseg(sbi, type, 1);
1899 curseg->alloc_type = LFS;
1900}
1901
1902static void __next_free_blkoff(struct f2fs_sb_info *sbi,
1903 struct curseg_info *seg, block_t start)
1904{
1905 struct seg_entry *se = get_seg_entry(sbi, seg->segno);
Changman Leee81c93c2013-11-15 13:21:16 +09001906 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
Jaegeuk Kim60a3b782015-02-10 16:44:29 -08001907 unsigned long *target_map = SIT_I(sbi)->tmp_map;
Changman Leee81c93c2013-11-15 13:21:16 +09001908 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
1909 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
1910 int i, pos;
1911
1912 for (i = 0; i < entries; i++)
1913 target_map[i] = ckpt_map[i] | cur_map[i];
1914
1915 pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
1916
1917 seg->next_blkoff = pos;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001918}
1919
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001920/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001921 * If a segment is written by LFS manner, next block offset is just obtained
1922 * by increasing the current block offset. However, if a segment is written by
1923 * SSR manner, next block offset obtained by calling __next_free_blkoff
1924 */
1925static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
1926 struct curseg_info *seg)
1927{
1928 if (seg->alloc_type == SSR)
1929 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
1930 else
1931 seg->next_blkoff++;
1932}
1933
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001934/*
arter97e1c42042014-08-06 23:22:50 +09001935 * This function always allocates a used segment(from dirty seglist) by SSR
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001936 * manner, so it should recover the existing segment information of valid blocks
1937 */
1938static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
1939{
1940 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1941 struct curseg_info *curseg = CURSEG_I(sbi, type);
1942 unsigned int new_segno = curseg->next_segno;
1943 struct f2fs_summary_block *sum_node;
1944 struct page *sum_page;
1945
1946 write_sum_page(sbi, curseg->sum_blk,
1947 GET_SUM_BLOCK(sbi, curseg->segno));
1948 __set_test_and_inuse(sbi, new_segno);
1949
1950 mutex_lock(&dirty_i->seglist_lock);
1951 __remove_dirty_segment(sbi, new_segno, PRE);
1952 __remove_dirty_segment(sbi, new_segno, DIRTY);
1953 mutex_unlock(&dirty_i->seglist_lock);
1954
1955 reset_curseg(sbi, type, 1);
1956 curseg->alloc_type = SSR;
1957 __next_free_blkoff(sbi, curseg, 0);
1958
1959 if (reuse) {
1960 sum_page = get_sum_page(sbi, new_segno);
1961 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
1962 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
1963 f2fs_put_page(sum_page, 1);
1964 }
1965}
1966
Jaegeuk Kim43727522013-02-04 15:11:17 +09001967static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
1968{
1969 struct curseg_info *curseg = CURSEG_I(sbi, type);
1970 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
Jaegeuk Kime066b832017-04-13 15:17:00 -07001971 unsigned segno = NULL_SEGNO;
Chao Yud27c3d82017-02-24 18:46:00 +08001972 int i, cnt;
1973 bool reversed = false;
Jaegeuk Kimc192f7a2017-02-22 17:10:18 -08001974
1975 /* need_SSR() already forces to do this */
Jaegeuk Kime066b832017-04-13 15:17:00 -07001976 if (v_ops->get_victim(sbi, &segno, BG_GC, type, SSR)) {
1977 curseg->next_segno = segno;
Jaegeuk Kimc192f7a2017-02-22 17:10:18 -08001978 return 1;
Jaegeuk Kime066b832017-04-13 15:17:00 -07001979 }
Jaegeuk Kim43727522013-02-04 15:11:17 +09001980
Jaegeuk Kim70d625c2017-02-22 17:02:32 -08001981 /* For node segments, let's do SSR more intensively */
1982 if (IS_NODESEG(type)) {
Chao Yud27c3d82017-02-24 18:46:00 +08001983 if (type >= CURSEG_WARM_NODE) {
1984 reversed = true;
1985 i = CURSEG_COLD_NODE;
1986 } else {
1987 i = CURSEG_HOT_NODE;
1988 }
1989 cnt = NR_CURSEG_NODE_TYPE;
Jaegeuk Kim70d625c2017-02-22 17:02:32 -08001990 } else {
Chao Yud27c3d82017-02-24 18:46:00 +08001991 if (type >= CURSEG_WARM_DATA) {
1992 reversed = true;
1993 i = CURSEG_COLD_DATA;
1994 } else {
1995 i = CURSEG_HOT_DATA;
1996 }
1997 cnt = NR_CURSEG_DATA_TYPE;
Jaegeuk Kim70d625c2017-02-22 17:02:32 -08001998 }
Jaegeuk Kim43727522013-02-04 15:11:17 +09001999
Chao Yud27c3d82017-02-24 18:46:00 +08002000 for (; cnt-- > 0; reversed ? i-- : i++) {
Jaegeuk Kimc192f7a2017-02-22 17:10:18 -08002001 if (i == type)
2002 continue;
Jaegeuk Kime066b832017-04-13 15:17:00 -07002003 if (v_ops->get_victim(sbi, &segno, BG_GC, i, SSR)) {
2004 curseg->next_segno = segno;
Jaegeuk Kim43727522013-02-04 15:11:17 +09002005 return 1;
Jaegeuk Kime066b832017-04-13 15:17:00 -07002006 }
Jaegeuk Kimc192f7a2017-02-22 17:10:18 -08002007 }
Jaegeuk Kim43727522013-02-04 15:11:17 +09002008 return 0;
2009}
2010
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002011/*
2012 * flush out current segment and replace it with new segment
2013 * This function should be returned with success, otherwise BUG
2014 */
2015static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
2016 int type, bool force)
2017{
Jaegeuk Kima7881892017-04-20 13:51:57 -07002018 struct curseg_info *curseg = CURSEG_I(sbi, type);
2019
Gu Zheng7b405272013-08-19 09:41:15 +08002020 if (force)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002021 new_curseg(sbi, type, true);
Jaegeuk Kim5b6c6be2017-02-14 19:32:51 -08002022 else if (!is_set_ckpt_flags(sbi, CP_CRC_RECOVERY_FLAG) &&
2023 type == CURSEG_WARM_NODE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002024 new_curseg(sbi, type, false);
Jaegeuk Kima7881892017-04-20 13:51:57 -07002025 else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
2026 new_curseg(sbi, type, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002027 else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
2028 change_curseg(sbi, type, true);
2029 else
2030 new_curseg(sbi, type, false);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +09002031
Jaegeuk Kima7881892017-04-20 13:51:57 -07002032 stat_inc_seg_type(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002033}
2034
2035void allocate_new_segments(struct f2fs_sb_info *sbi)
2036{
Jaegeuk Kim6ae1be12016-11-11 12:31:40 -08002037 struct curseg_info *curseg;
2038 unsigned int old_segno;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002039 int i;
2040
Jaegeuk Kim6ae1be12016-11-11 12:31:40 -08002041 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
2042 curseg = CURSEG_I(sbi, i);
2043 old_segno = curseg->segno;
2044 SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
2045 locate_dirty_segment(sbi, old_segno);
2046 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002047}
2048
2049static const struct segment_allocation default_salloc_ops = {
2050 .allocate_segment = allocate_segment_by_default,
2051};
2052
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08002053bool exist_trim_candidates(struct f2fs_sb_info *sbi, struct cp_control *cpc)
2054{
2055 __u64 trim_start = cpc->trim_start;
2056 bool has_candidate = false;
2057
2058 mutex_lock(&SIT_I(sbi)->sentry_lock);
2059 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++) {
2060 if (add_discard_addrs(sbi, cpc, true)) {
2061 has_candidate = true;
2062 break;
2063 }
2064 }
2065 mutex_unlock(&SIT_I(sbi)->sentry_lock);
2066
2067 cpc->trim_start = trim_start;
2068 return has_candidate;
2069}
2070
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002071int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range)
2072{
Jaegeuk Kimf7ef9b82015-02-09 12:02:44 -08002073 __u64 start = F2FS_BYTES_TO_BLK(range->start);
2074 __u64 end = start + F2FS_BYTES_TO_BLK(range->len) - 1;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002075 unsigned int start_segno, end_segno;
2076 struct cp_control cpc;
Chao Yuc34f42e2015-12-23 17:50:30 +08002077 int err = 0;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002078
Jaegeuk Kim836b5a62015-04-30 22:50:06 -07002079 if (start >= MAX_BLKADDR(sbi) || range->len < sbi->blocksize)
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002080 return -EINVAL;
2081
Jan Kara9bd27ae2014-10-21 14:07:33 +02002082 cpc.trimmed = 0;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002083 if (end <= MAIN_BLKADDR(sbi))
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002084 goto out;
2085
Yunlei Heed214a12016-09-01 10:14:39 +08002086 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) {
2087 f2fs_msg(sbi->sb, KERN_WARNING,
2088 "Found FS corruption, run fsck to fix.");
2089 goto out;
2090 }
2091
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002092 /* start/end segment number in main_area */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002093 start_segno = (start <= MAIN_BLKADDR(sbi)) ? 0 : GET_SEGNO(sbi, start);
2094 end_segno = (end >= MAX_BLKADDR(sbi)) ? MAIN_SEGS(sbi) - 1 :
2095 GET_SEGNO(sbi, end);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002096 cpc.reason = CP_DISCARD;
Jaegeuk Kim836b5a62015-04-30 22:50:06 -07002097 cpc.trim_minlen = max_t(__u64, 1, F2FS_BYTES_TO_BLK(range->minlen));
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002098
2099 /* do checkpoint to issue discard commands safely */
Jaegeuk Kimbba681c2015-01-26 17:41:23 -08002100 for (; start_segno <= end_segno; start_segno = cpc.trim_end + 1) {
2101 cpc.trim_start = start_segno;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07002102
2103 if (sbi->discard_blks == 0)
2104 break;
2105 else if (sbi->discard_blks < BATCHED_TRIM_BLOCKS(sbi))
2106 cpc.trim_end = end_segno;
2107 else
2108 cpc.trim_end = min_t(unsigned int,
2109 rounddown(start_segno +
Jaegeuk Kimbba681c2015-01-26 17:41:23 -08002110 BATCHED_TRIM_SEGMENTS(sbi),
2111 sbi->segs_per_sec) - 1, end_segno);
2112
2113 mutex_lock(&sbi->gc_mutex);
Chao Yuc34f42e2015-12-23 17:50:30 +08002114 err = write_checkpoint(sbi, &cpc);
Jaegeuk Kimbba681c2015-01-26 17:41:23 -08002115 mutex_unlock(&sbi->gc_mutex);
Chao Yue9328352016-08-21 23:21:29 +08002116 if (err)
2117 break;
Chao Yu74fa5f32016-08-21 23:21:30 +08002118
2119 schedule();
Jaegeuk Kimbba681c2015-01-26 17:41:23 -08002120 }
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002121out:
Jaegeuk Kimf7ef9b82015-02-09 12:02:44 -08002122 range->len = F2FS_BLK_TO_BYTES(cpc.trimmed);
Chao Yuc34f42e2015-12-23 17:50:30 +08002123 return err;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002124}
2125
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002126static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
2127{
2128 struct curseg_info *curseg = CURSEG_I(sbi, type);
2129 if (curseg->next_blkoff < sbi->blocks_per_seg)
2130 return true;
2131 return false;
2132}
2133
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002134static int __get_segment_type_2(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002135{
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002136 if (fio->type == DATA)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002137 return CURSEG_HOT_DATA;
2138 else
2139 return CURSEG_HOT_NODE;
2140}
2141
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002142static int __get_segment_type_4(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002143{
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002144 if (fio->type == DATA) {
2145 struct inode *inode = fio->page->mapping->host;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002146
2147 if (S_ISDIR(inode->i_mode))
2148 return CURSEG_HOT_DATA;
2149 else
2150 return CURSEG_COLD_DATA;
2151 } else {
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002152 if (IS_DNODE(fio->page) && is_cold_node(fio->page))
Jaegeuk Kima344b9f2014-11-05 20:05:53 -08002153 return CURSEG_WARM_NODE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002154 else
2155 return CURSEG_COLD_NODE;
2156 }
2157}
2158
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002159static int __get_segment_type_6(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002160{
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002161 if (fio->type == DATA) {
2162 struct inode *inode = fio->page->mapping->host;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002163
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002164 if (is_cold_data(fio->page) || file_is_cold(inode))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002165 return CURSEG_COLD_DATA;
Jaegeuk Kimef095d12017-03-24 20:05:13 -04002166 if (is_inode_flag_set(inode, FI_HOT_DATA))
2167 return CURSEG_HOT_DATA;
2168 return CURSEG_WARM_DATA;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002169 } else {
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002170 if (IS_DNODE(fio->page))
2171 return is_cold_node(fio->page) ? CURSEG_WARM_NODE :
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002172 CURSEG_HOT_NODE;
Jaegeuk Kimef095d12017-03-24 20:05:13 -04002173 return CURSEG_COLD_NODE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002174 }
2175}
2176
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002177static int __get_segment_type(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002178{
Jaegeuk Kima912b542017-05-10 11:18:25 -07002179 int type = 0;
2180
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002181 switch (fio->sbi->active_logs) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002182 case 2:
Jaegeuk Kima912b542017-05-10 11:18:25 -07002183 type = __get_segment_type_2(fio);
2184 break;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002185 case 4:
Jaegeuk Kima912b542017-05-10 11:18:25 -07002186 type = __get_segment_type_4(fio);
2187 break;
2188 case 6:
2189 type = __get_segment_type_6(fio);
2190 break;
2191 default:
2192 f2fs_bug_on(fio->sbi, true);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002193 }
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002194
Jaegeuk Kima912b542017-05-10 11:18:25 -07002195 if (IS_HOT(type))
2196 fio->temp = HOT;
2197 else if (IS_WARM(type))
2198 fio->temp = WARM;
2199 else
2200 fio->temp = COLD;
2201 return type;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002202}
2203
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09002204void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
2205 block_t old_blkaddr, block_t *new_blkaddr,
Chao Yufb830fc2017-05-19 23:37:01 +08002206 struct f2fs_summary *sum, int type,
2207 struct f2fs_io_info *fio, bool add_list)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002208{
2209 struct sit_info *sit_i = SIT_I(sbi);
Jaegeuk Kim6ae1be12016-11-11 12:31:40 -08002210 struct curseg_info *curseg = CURSEG_I(sbi, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002211
2212 mutex_lock(&curseg->curseg_mutex);
Jaegeuk Kim21cb1d92015-03-11 13:42:48 -04002213 mutex_lock(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002214
2215 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002216
Jaegeuk Kim4e6a8d92016-12-29 14:07:53 -08002217 f2fs_wait_discard_bio(sbi, *new_blkaddr);
2218
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002219 /*
2220 * __add_sum_entry should be resided under the curseg_mutex
2221 * because, this function updates a summary entry in the
2222 * current summary block.
2223 */
Haicheng Lie79efe32013-06-13 16:59:27 +08002224 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002225
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002226 __refresh_next_blkoff(sbi, curseg);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +09002227
2228 stat_inc_block_count(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002229
Yunlong Song3436c4b2017-02-21 16:59:26 +08002230 if (!__has_curseg_space(sbi, type))
2231 sit_i->s_ops->allocate_segment(sbi, type, false);
Jaegeuk Kimc6f82fe92017-04-04 16:45:30 -07002232 /*
2233 * SIT information should be updated after segment allocation,
2234 * since we need to keep dirty segments precisely under SSR.
2235 */
2236 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
Yunlong Song3436c4b2017-02-21 16:59:26 +08002237
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002238 mutex_unlock(&sit_i->sentry_lock);
2239
Chao Yu704956e2017-07-31 20:19:09 +08002240 if (page && IS_NODESEG(type)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002241 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
2242
Chao Yu704956e2017-07-31 20:19:09 +08002243 f2fs_inode_chksum_set(sbi, page);
2244 }
2245
Chao Yufb830fc2017-05-19 23:37:01 +08002246 if (add_list) {
2247 struct f2fs_bio_info *io;
2248
2249 INIT_LIST_HEAD(&fio->list);
2250 fio->in_list = true;
2251 io = sbi->write_io[fio->type] + fio->temp;
2252 spin_lock(&io->io_lock);
2253 list_add_tail(&fio->list, &io->io_list);
2254 spin_unlock(&io->io_lock);
2255 }
2256
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09002257 mutex_unlock(&curseg->curseg_mutex);
2258}
2259
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002260static void do_write_page(struct f2fs_summary *sum, struct f2fs_io_info *fio)
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09002261{
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002262 int type = __get_segment_type(fio);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08002263 int err;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09002264
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08002265reallocate:
Chao Yu7a9d7542016-02-22 18:36:38 +08002266 allocate_data_block(fio->sbi, fio->page, fio->old_blkaddr,
Chao Yufb830fc2017-05-19 23:37:01 +08002267 &fio->new_blkaddr, sum, type, fio, true);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09002268
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002269 /* writeout dirty page into bdev */
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07002270 err = f2fs_submit_page_write(fio);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08002271 if (err == -EAGAIN) {
2272 fio->old_blkaddr = fio->new_blkaddr;
2273 goto reallocate;
2274 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002275}
2276
Chao Yub0af6d42017-08-02 23:21:48 +08002277void write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
2278 enum iostat_type io_type)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002279{
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002280 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002281 .sbi = sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002282 .type = META,
Mike Christie04d328d2016-06-05 14:31:55 -05002283 .op = REQ_OP_WRITE,
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002284 .op_flags = REQ_SYNC | REQ_META | REQ_PRIO,
Chao Yu7a9d7542016-02-22 18:36:38 +08002285 .old_blkaddr = page->index,
2286 .new_blkaddr = page->index,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002287 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07002288 .encrypted_page = NULL,
Chao Yufb830fc2017-05-19 23:37:01 +08002289 .in_list = false,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002290 };
2291
Chao Yu2b947002015-10-12 17:04:21 +08002292 if (unlikely(page->index >= MAIN_BLKADDR(sbi)))
Mike Christie04d328d2016-06-05 14:31:55 -05002293 fio.op_flags &= ~REQ_META;
Chao Yu2b947002015-10-12 17:04:21 +08002294
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002295 set_page_writeback(page);
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07002296 f2fs_submit_page_write(&fio);
Chao Yub0af6d42017-08-02 23:21:48 +08002297
2298 f2fs_update_iostat(sbi, io_type, F2FS_BLKSIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002299}
2300
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002301void write_node_page(unsigned int nid, struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002302{
2303 struct f2fs_summary sum;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002304
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002305 set_summary(&sum, nid, 0, 0);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002306 do_write_page(&sum, fio);
Chao Yub0af6d42017-08-02 23:21:48 +08002307
2308 f2fs_update_iostat(fio->sbi, fio->io_type, F2FS_BLKSIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002309}
2310
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002311void write_data_page(struct dnode_of_data *dn, struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002312{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002313 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002314 struct f2fs_summary sum;
2315 struct node_info ni;
2316
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002317 f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002318 get_node_info(sbi, dn->nid, &ni);
2319 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002320 do_write_page(&sum, fio);
Chao Yuf28b3432016-02-24 17:16:47 +08002321 f2fs_update_data_blkaddr(dn, fio->new_blkaddr);
Chao Yub0af6d42017-08-02 23:21:48 +08002322
2323 f2fs_update_iostat(sbi, fio->io_type, F2FS_BLKSIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002324}
2325
Jaegeuk Kimd1b3e722017-03-30 21:02:46 -07002326int rewrite_data_page(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002327{
Chao Yub0af6d42017-08-02 23:21:48 +08002328 int err;
2329
Chao Yu7a9d7542016-02-22 18:36:38 +08002330 fio->new_blkaddr = fio->old_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002331 stat_inc_inplace_blocks(fio->sbi);
Chao Yub0af6d42017-08-02 23:21:48 +08002332
2333 err = f2fs_submit_page_bio(fio);
2334
2335 f2fs_update_iostat(fio->sbi, fio->io_type, F2FS_BLKSIZE);
2336
2337 return err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002338}
2339
Chao Yu4356e482016-02-23 17:52:43 +08002340void __f2fs_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
Chao Yu19f106b2015-05-06 13:08:06 +08002341 block_t old_blkaddr, block_t new_blkaddr,
Chao Yu28bc1062016-02-06 14:40:34 +08002342 bool recover_curseg, bool recover_newaddr)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002343{
2344 struct sit_info *sit_i = SIT_I(sbi);
2345 struct curseg_info *curseg;
2346 unsigned int segno, old_cursegno;
2347 struct seg_entry *se;
2348 int type;
Chao Yu19f106b2015-05-06 13:08:06 +08002349 unsigned short old_blkoff;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002350
2351 segno = GET_SEGNO(sbi, new_blkaddr);
2352 se = get_seg_entry(sbi, segno);
2353 type = se->type;
2354
Chao Yu19f106b2015-05-06 13:08:06 +08002355 if (!recover_curseg) {
2356 /* for recovery flow */
2357 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
2358 if (old_blkaddr == NULL_ADDR)
2359 type = CURSEG_COLD_DATA;
2360 else
2361 type = CURSEG_WARM_DATA;
2362 }
2363 } else {
2364 if (!IS_CURSEG(sbi, segno))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002365 type = CURSEG_WARM_DATA;
2366 }
Chao Yu19f106b2015-05-06 13:08:06 +08002367
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002368 curseg = CURSEG_I(sbi, type);
2369
2370 mutex_lock(&curseg->curseg_mutex);
2371 mutex_lock(&sit_i->sentry_lock);
2372
2373 old_cursegno = curseg->segno;
Chao Yu19f106b2015-05-06 13:08:06 +08002374 old_blkoff = curseg->next_blkoff;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002375
2376 /* change the current segment */
2377 if (segno != curseg->segno) {
2378 curseg->next_segno = segno;
2379 change_curseg(sbi, type, true);
2380 }
2381
Jaegeuk Kim491c0852014-02-04 13:01:10 +09002382 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
Haicheng Lie79efe32013-06-13 16:59:27 +08002383 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002384
Chao Yu28bc1062016-02-06 14:40:34 +08002385 if (!recover_curseg || recover_newaddr)
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07002386 update_sit_entry(sbi, new_blkaddr, 1);
2387 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
2388 update_sit_entry(sbi, old_blkaddr, -1);
2389
2390 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
2391 locate_dirty_segment(sbi, GET_SEGNO(sbi, new_blkaddr));
2392
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002393 locate_dirty_segment(sbi, old_cursegno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002394
Chao Yu19f106b2015-05-06 13:08:06 +08002395 if (recover_curseg) {
2396 if (old_cursegno != curseg->segno) {
2397 curseg->next_segno = old_cursegno;
2398 change_curseg(sbi, type, true);
2399 }
2400 curseg->next_blkoff = old_blkoff;
2401 }
2402
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002403 mutex_unlock(&sit_i->sentry_lock);
2404 mutex_unlock(&curseg->curseg_mutex);
2405}
2406
Chao Yu528e3452015-05-28 19:15:35 +08002407void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
2408 block_t old_addr, block_t new_addr,
Chao Yu28bc1062016-02-06 14:40:34 +08002409 unsigned char version, bool recover_curseg,
2410 bool recover_newaddr)
Chao Yu528e3452015-05-28 19:15:35 +08002411{
2412 struct f2fs_summary sum;
2413
2414 set_summary(&sum, dn->nid, dn->ofs_in_node, version);
2415
Chao Yu28bc1062016-02-06 14:40:34 +08002416 __f2fs_replace_block(sbi, &sum, old_addr, new_addr,
2417 recover_curseg, recover_newaddr);
Chao Yu528e3452015-05-28 19:15:35 +08002418
Chao Yuf28b3432016-02-24 17:16:47 +08002419 f2fs_update_data_blkaddr(dn, new_addr);
Chao Yu528e3452015-05-28 19:15:35 +08002420}
2421
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09002422void f2fs_wait_on_page_writeback(struct page *page,
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08002423 enum page_type type, bool ordered)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09002424{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09002425 if (PageWriteback(page)) {
Jaegeuk Kim40813632014-09-02 15:31:18 -07002426 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
2427
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07002428 f2fs_submit_merged_write_cond(sbi, page->mapping->host,
2429 0, page->index, type);
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08002430 if (ordered)
2431 wait_on_page_writeback(page);
2432 else
2433 wait_for_stable_page(page);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09002434 }
2435}
2436
Chao Yu08b39fb2015-10-08 13:27:34 +08002437void f2fs_wait_on_encrypted_page_writeback(struct f2fs_sb_info *sbi,
2438 block_t blkaddr)
2439{
2440 struct page *cpage;
2441
Yunlei He5d4c0af2016-09-18 08:16:56 +08002442 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR)
Chao Yu08b39fb2015-10-08 13:27:34 +08002443 return;
2444
Chao Yu08b39fb2015-10-08 13:27:34 +08002445 cpage = find_lock_page(META_MAPPING(sbi), blkaddr);
2446 if (cpage) {
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08002447 f2fs_wait_on_page_writeback(cpage, DATA, true);
Chao Yu08b39fb2015-10-08 13:27:34 +08002448 f2fs_put_page(cpage, 1);
2449 }
2450}
2451
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002452static int read_compacted_summaries(struct f2fs_sb_info *sbi)
2453{
2454 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
2455 struct curseg_info *seg_i;
2456 unsigned char *kaddr;
2457 struct page *page;
2458 block_t start;
2459 int i, j, offset;
2460
2461 start = start_sum_block(sbi);
2462
2463 page = get_meta_page(sbi, start++);
2464 kaddr = (unsigned char *)page_address(page);
2465
2466 /* Step 1: restore nat cache */
2467 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08002468 memcpy(seg_i->journal, kaddr, SUM_JOURNAL_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002469
2470 /* Step 2: restore sit cache */
2471 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08002472 memcpy(seg_i->journal, kaddr + SUM_JOURNAL_SIZE, SUM_JOURNAL_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002473 offset = 2 * SUM_JOURNAL_SIZE;
2474
2475 /* Step 3: restore summary entries */
2476 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
2477 unsigned short blk_off;
2478 unsigned int segno;
2479
2480 seg_i = CURSEG_I(sbi, i);
2481 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
2482 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
2483 seg_i->next_segno = segno;
2484 reset_curseg(sbi, i, 0);
2485 seg_i->alloc_type = ckpt->alloc_type[i];
2486 seg_i->next_blkoff = blk_off;
2487
2488 if (seg_i->alloc_type == SSR)
2489 blk_off = sbi->blocks_per_seg;
2490
2491 for (j = 0; j < blk_off; j++) {
2492 struct f2fs_summary *s;
2493 s = (struct f2fs_summary *)(kaddr + offset);
2494 seg_i->sum_blk->entries[j] = *s;
2495 offset += SUMMARY_SIZE;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002496 if (offset + SUMMARY_SIZE <= PAGE_SIZE -
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002497 SUM_FOOTER_SIZE)
2498 continue;
2499
2500 f2fs_put_page(page, 1);
2501 page = NULL;
2502
2503 page = get_meta_page(sbi, start++);
2504 kaddr = (unsigned char *)page_address(page);
2505 offset = 0;
2506 }
2507 }
2508 f2fs_put_page(page, 1);
2509 return 0;
2510}
2511
2512static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
2513{
2514 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
2515 struct f2fs_summary_block *sum;
2516 struct curseg_info *curseg;
2517 struct page *new;
2518 unsigned short blk_off;
2519 unsigned int segno = 0;
2520 block_t blk_addr = 0;
2521
2522 /* get segment number and block addr */
2523 if (IS_DATASEG(type)) {
2524 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
2525 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
2526 CURSEG_HOT_DATA]);
Jaegeuk Kim119ee912015-01-29 11:45:33 -08002527 if (__exist_node_summaries(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002528 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
2529 else
2530 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
2531 } else {
2532 segno = le32_to_cpu(ckpt->cur_node_segno[type -
2533 CURSEG_HOT_NODE]);
2534 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
2535 CURSEG_HOT_NODE]);
Jaegeuk Kim119ee912015-01-29 11:45:33 -08002536 if (__exist_node_summaries(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002537 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
2538 type - CURSEG_HOT_NODE);
2539 else
2540 blk_addr = GET_SUM_BLOCK(sbi, segno);
2541 }
2542
2543 new = get_meta_page(sbi, blk_addr);
2544 sum = (struct f2fs_summary_block *)page_address(new);
2545
2546 if (IS_NODESEG(type)) {
Jaegeuk Kim119ee912015-01-29 11:45:33 -08002547 if (__exist_node_summaries(sbi)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002548 struct f2fs_summary *ns = &sum->entries[0];
2549 int i;
2550 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
2551 ns->version = 0;
2552 ns->ofs_in_node = 0;
2553 }
2554 } else {
Gu Zhengd6537882014-03-07 18:43:36 +08002555 int err;
2556
2557 err = restore_node_summary(sbi, segno, sum);
2558 if (err) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002559 f2fs_put_page(new, 1);
Gu Zhengd6537882014-03-07 18:43:36 +08002560 return err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002561 }
2562 }
2563 }
2564
2565 /* set uncompleted segment to curseg */
2566 curseg = CURSEG_I(sbi, type);
2567 mutex_lock(&curseg->curseg_mutex);
Chao Yub7ad7512016-02-19 18:08:46 +08002568
2569 /* update journal info */
2570 down_write(&curseg->journal_rwsem);
2571 memcpy(curseg->journal, &sum->journal, SUM_JOURNAL_SIZE);
2572 up_write(&curseg->journal_rwsem);
2573
2574 memcpy(curseg->sum_blk->entries, sum->entries, SUM_ENTRY_SIZE);
2575 memcpy(&curseg->sum_blk->footer, &sum->footer, SUM_FOOTER_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002576 curseg->next_segno = segno;
2577 reset_curseg(sbi, type, 0);
2578 curseg->alloc_type = ckpt->alloc_type[type];
2579 curseg->next_blkoff = blk_off;
2580 mutex_unlock(&curseg->curseg_mutex);
2581 f2fs_put_page(new, 1);
2582 return 0;
2583}
2584
2585static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
2586{
Jin Qian21d3f8e2017-06-01 11:18:30 -07002587 struct f2fs_journal *sit_j = CURSEG_I(sbi, CURSEG_COLD_DATA)->journal;
2588 struct f2fs_journal *nat_j = CURSEG_I(sbi, CURSEG_HOT_DATA)->journal;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002589 int type = CURSEG_HOT_DATA;
Chao Yue4fc5fb2014-03-17 16:36:24 +08002590 int err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002591
Chao Yuaaec2b12016-09-20 11:04:18 +08002592 if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG)) {
Chao Yu3fa06d72014-12-09 14:21:46 +08002593 int npages = npages_for_summary_flush(sbi, true);
2594
2595 if (npages >= 2)
2596 ra_meta_pages(sbi, start_sum_block(sbi), npages,
Chao Yu26879fb2015-10-12 17:05:59 +08002597 META_CP, true);
Chao Yu3fa06d72014-12-09 14:21:46 +08002598
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002599 /* restore for compacted data summary */
2600 if (read_compacted_summaries(sbi))
2601 return -EINVAL;
2602 type = CURSEG_HOT_NODE;
2603 }
2604
Jaegeuk Kim119ee912015-01-29 11:45:33 -08002605 if (__exist_node_summaries(sbi))
Chao Yu3fa06d72014-12-09 14:21:46 +08002606 ra_meta_pages(sbi, sum_blk_addr(sbi, NR_CURSEG_TYPE, type),
Chao Yu26879fb2015-10-12 17:05:59 +08002607 NR_CURSEG_TYPE - type, META_CP, true);
Chao Yu3fa06d72014-12-09 14:21:46 +08002608
Chao Yue4fc5fb2014-03-17 16:36:24 +08002609 for (; type <= CURSEG_COLD_NODE; type++) {
2610 err = read_normal_summaries(sbi, type);
2611 if (err)
2612 return err;
2613 }
2614
Jin Qian21d3f8e2017-06-01 11:18:30 -07002615 /* sanity check for summary blocks */
2616 if (nats_in_cursum(nat_j) > NAT_JOURNAL_ENTRIES ||
2617 sits_in_cursum(sit_j) > SIT_JOURNAL_ENTRIES)
2618 return -EINVAL;
2619
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002620 return 0;
2621}
2622
2623static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
2624{
2625 struct page *page;
2626 unsigned char *kaddr;
2627 struct f2fs_summary *summary;
2628 struct curseg_info *seg_i;
2629 int written_size = 0;
2630 int i, j;
2631
2632 page = grab_meta_page(sbi, blkaddr++);
2633 kaddr = (unsigned char *)page_address(page);
2634
2635 /* Step 1: write nat cache */
2636 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08002637 memcpy(kaddr, seg_i->journal, SUM_JOURNAL_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002638 written_size += SUM_JOURNAL_SIZE;
2639
2640 /* Step 2: write sit cache */
2641 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08002642 memcpy(kaddr + written_size, seg_i->journal, SUM_JOURNAL_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002643 written_size += SUM_JOURNAL_SIZE;
2644
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002645 /* Step 3: write summary entries */
2646 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
2647 unsigned short blkoff;
2648 seg_i = CURSEG_I(sbi, i);
2649 if (sbi->ckpt->alloc_type[i] == SSR)
2650 blkoff = sbi->blocks_per_seg;
2651 else
2652 blkoff = curseg_blkoff(sbi, i);
2653
2654 for (j = 0; j < blkoff; j++) {
2655 if (!page) {
2656 page = grab_meta_page(sbi, blkaddr++);
2657 kaddr = (unsigned char *)page_address(page);
2658 written_size = 0;
2659 }
2660 summary = (struct f2fs_summary *)(kaddr + written_size);
2661 *summary = seg_i->sum_blk->entries[j];
2662 written_size += SUMMARY_SIZE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002663
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002664 if (written_size + SUMMARY_SIZE <= PAGE_SIZE -
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002665 SUM_FOOTER_SIZE)
2666 continue;
2667
Chao Yue8d61a72013-10-24 15:08:28 +08002668 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002669 f2fs_put_page(page, 1);
2670 page = NULL;
2671 }
2672 }
Chao Yue8d61a72013-10-24 15:08:28 +08002673 if (page) {
2674 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002675 f2fs_put_page(page, 1);
Chao Yue8d61a72013-10-24 15:08:28 +08002676 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002677}
2678
2679static void write_normal_summaries(struct f2fs_sb_info *sbi,
2680 block_t blkaddr, int type)
2681{
2682 int i, end;
2683 if (IS_DATASEG(type))
2684 end = type + NR_CURSEG_DATA_TYPE;
2685 else
2686 end = type + NR_CURSEG_NODE_TYPE;
2687
Chao Yub7ad7512016-02-19 18:08:46 +08002688 for (i = type; i < end; i++)
2689 write_current_sum_page(sbi, i, blkaddr + (i - type));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002690}
2691
2692void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
2693{
Chao Yuaaec2b12016-09-20 11:04:18 +08002694 if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002695 write_compacted_summaries(sbi, start_blk);
2696 else
2697 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
2698}
2699
2700void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
2701{
Jaegeuk Kim119ee912015-01-29 11:45:33 -08002702 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002703}
2704
Chao Yudfc08a12016-02-14 18:50:40 +08002705int lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002706 unsigned int val, int alloc)
2707{
2708 int i;
2709
2710 if (type == NAT_JOURNAL) {
Chao Yudfc08a12016-02-14 18:50:40 +08002711 for (i = 0; i < nats_in_cursum(journal); i++) {
2712 if (le32_to_cpu(nid_in_journal(journal, i)) == val)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002713 return i;
2714 }
Chao Yudfc08a12016-02-14 18:50:40 +08002715 if (alloc && __has_cursum_space(journal, 1, NAT_JOURNAL))
2716 return update_nats_in_cursum(journal, 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002717 } else if (type == SIT_JOURNAL) {
Chao Yudfc08a12016-02-14 18:50:40 +08002718 for (i = 0; i < sits_in_cursum(journal); i++)
2719 if (le32_to_cpu(segno_in_journal(journal, i)) == val)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002720 return i;
Chao Yudfc08a12016-02-14 18:50:40 +08002721 if (alloc && __has_cursum_space(journal, 1, SIT_JOURNAL))
2722 return update_sits_in_cursum(journal, 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002723 }
2724 return -1;
2725}
2726
2727static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
2728 unsigned int segno)
2729{
Gu Zheng2cc22182014-10-20 17:45:49 +08002730 return get_meta_page(sbi, current_sit_addr(sbi, segno));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002731}
2732
2733static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
2734 unsigned int start)
2735{
2736 struct sit_info *sit_i = SIT_I(sbi);
2737 struct page *src_page, *dst_page;
2738 pgoff_t src_off, dst_off;
2739 void *src_addr, *dst_addr;
2740
2741 src_off = current_sit_addr(sbi, start);
2742 dst_off = next_sit_addr(sbi, src_off);
2743
2744 /* get current sit block page without lock */
2745 src_page = get_meta_page(sbi, src_off);
2746 dst_page = grab_meta_page(sbi, dst_off);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002747 f2fs_bug_on(sbi, PageDirty(src_page));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002748
2749 src_addr = page_address(src_page);
2750 dst_addr = page_address(dst_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002751 memcpy(dst_addr, src_addr, PAGE_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002752
2753 set_page_dirty(dst_page);
2754 f2fs_put_page(src_page, 1);
2755
2756 set_to_next_sit(sit_i, start);
2757
2758 return dst_page;
2759}
2760
Chao Yu184a5cd2014-09-04 18:13:01 +08002761static struct sit_entry_set *grab_sit_entry_set(void)
2762{
2763 struct sit_entry_set *ses =
Jaegeuk Kim80c54502015-08-20 08:51:56 -07002764 f2fs_kmem_cache_alloc(sit_entry_set_slab, GFP_NOFS);
Chao Yu184a5cd2014-09-04 18:13:01 +08002765
2766 ses->entry_cnt = 0;
2767 INIT_LIST_HEAD(&ses->set_list);
2768 return ses;
2769}
2770
2771static void release_sit_entry_set(struct sit_entry_set *ses)
2772{
2773 list_del(&ses->set_list);
2774 kmem_cache_free(sit_entry_set_slab, ses);
2775}
2776
2777static void adjust_sit_entry_set(struct sit_entry_set *ses,
2778 struct list_head *head)
2779{
2780 struct sit_entry_set *next = ses;
2781
2782 if (list_is_last(&ses->set_list, head))
2783 return;
2784
2785 list_for_each_entry_continue(next, head, set_list)
2786 if (ses->entry_cnt <= next->entry_cnt)
2787 break;
2788
2789 list_move_tail(&ses->set_list, &next->set_list);
2790}
2791
2792static void add_sit_entry(unsigned int segno, struct list_head *head)
2793{
2794 struct sit_entry_set *ses;
2795 unsigned int start_segno = START_SEGNO(segno);
2796
2797 list_for_each_entry(ses, head, set_list) {
2798 if (ses->start_segno == start_segno) {
2799 ses->entry_cnt++;
2800 adjust_sit_entry_set(ses, head);
2801 return;
2802 }
2803 }
2804
2805 ses = grab_sit_entry_set();
2806
2807 ses->start_segno = start_segno;
2808 ses->entry_cnt++;
2809 list_add(&ses->set_list, head);
2810}
2811
2812static void add_sits_in_set(struct f2fs_sb_info *sbi)
2813{
2814 struct f2fs_sm_info *sm_info = SM_I(sbi);
2815 struct list_head *set_list = &sm_info->sit_entry_set;
2816 unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap;
Chao Yu184a5cd2014-09-04 18:13:01 +08002817 unsigned int segno;
2818
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002819 for_each_set_bit(segno, bitmap, MAIN_SEGS(sbi))
Chao Yu184a5cd2014-09-04 18:13:01 +08002820 add_sit_entry(segno, set_list);
2821}
2822
2823static void remove_sits_in_journal(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002824{
2825 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08002826 struct f2fs_journal *journal = curseg->journal;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002827 int i;
2828
Chao Yub7ad7512016-02-19 18:08:46 +08002829 down_write(&curseg->journal_rwsem);
Chao Yudfc08a12016-02-14 18:50:40 +08002830 for (i = 0; i < sits_in_cursum(journal); i++) {
Chao Yu184a5cd2014-09-04 18:13:01 +08002831 unsigned int segno;
2832 bool dirtied;
2833
Chao Yudfc08a12016-02-14 18:50:40 +08002834 segno = le32_to_cpu(segno_in_journal(journal, i));
Chao Yu184a5cd2014-09-04 18:13:01 +08002835 dirtied = __mark_sit_entry_dirty(sbi, segno);
2836
2837 if (!dirtied)
2838 add_sit_entry(segno, &SM_I(sbi)->sit_entry_set);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002839 }
Chao Yudfc08a12016-02-14 18:50:40 +08002840 update_sits_in_cursum(journal, -i);
Chao Yub7ad7512016-02-19 18:08:46 +08002841 up_write(&curseg->journal_rwsem);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002842}
2843
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002844/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002845 * CP calls this function, which flushes SIT entries including sit_journal,
2846 * and moves prefree segs to free segs.
2847 */
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002848void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002849{
2850 struct sit_info *sit_i = SIT_I(sbi);
2851 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
2852 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08002853 struct f2fs_journal *journal = curseg->journal;
Chao Yu184a5cd2014-09-04 18:13:01 +08002854 struct sit_entry_set *ses, *tmp;
2855 struct list_head *head = &SM_I(sbi)->sit_entry_set;
Chao Yu184a5cd2014-09-04 18:13:01 +08002856 bool to_journal = true;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002857 struct seg_entry *se;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002858
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002859 mutex_lock(&sit_i->sentry_lock);
2860
Wanpeng Li2b11a742015-02-27 16:52:50 +08002861 if (!sit_i->dirty_sentries)
2862 goto out;
2863
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002864 /*
Chao Yu184a5cd2014-09-04 18:13:01 +08002865 * add and account sit entries of dirty bitmap in sit entry
2866 * set temporarily
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002867 */
Chao Yu184a5cd2014-09-04 18:13:01 +08002868 add_sits_in_set(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002869
Chao Yu184a5cd2014-09-04 18:13:01 +08002870 /*
2871 * if there are no enough space in journal to store dirty sit
2872 * entries, remove all entries from journal and add and account
2873 * them in sit entry set.
2874 */
Chao Yudfc08a12016-02-14 18:50:40 +08002875 if (!__has_cursum_space(journal, sit_i->dirty_sentries, SIT_JOURNAL))
Chao Yu184a5cd2014-09-04 18:13:01 +08002876 remove_sits_in_journal(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002877
Chao Yu184a5cd2014-09-04 18:13:01 +08002878 /*
2879 * there are two steps to flush sit entries:
2880 * #1, flush sit entries to journal in current cold data summary block.
2881 * #2, flush sit entries to sit page.
2882 */
2883 list_for_each_entry_safe(ses, tmp, head, set_list) {
Jaegeuk Kim4a257ed2014-10-16 11:43:30 -07002884 struct page *page = NULL;
Chao Yu184a5cd2014-09-04 18:13:01 +08002885 struct f2fs_sit_block *raw_sit = NULL;
2886 unsigned int start_segno = ses->start_segno;
2887 unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002888 (unsigned long)MAIN_SEGS(sbi));
Chao Yu184a5cd2014-09-04 18:13:01 +08002889 unsigned int segno = start_segno;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09002890
Chao Yu184a5cd2014-09-04 18:13:01 +08002891 if (to_journal &&
Chao Yudfc08a12016-02-14 18:50:40 +08002892 !__has_cursum_space(journal, ses->entry_cnt, SIT_JOURNAL))
Chao Yu184a5cd2014-09-04 18:13:01 +08002893 to_journal = false;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002894
Chao Yub7ad7512016-02-19 18:08:46 +08002895 if (to_journal) {
2896 down_write(&curseg->journal_rwsem);
2897 } else {
Chao Yu184a5cd2014-09-04 18:13:01 +08002898 page = get_next_sit_page(sbi, start_segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002899 raw_sit = page_address(page);
2900 }
2901
Chao Yu184a5cd2014-09-04 18:13:01 +08002902 /* flush dirty sit entries in region of current sit set */
2903 for_each_set_bit_from(segno, bitmap, end) {
2904 int offset, sit_offset;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002905
2906 se = get_seg_entry(sbi, segno);
Chao Yu184a5cd2014-09-04 18:13:01 +08002907
2908 /* add discard candidates */
Chao Yuc473f1a2017-04-27 20:40:39 +08002909 if (!(cpc->reason & CP_DISCARD)) {
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002910 cpc->trim_start = segno;
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08002911 add_discard_addrs(sbi, cpc, false);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002912 }
Chao Yu184a5cd2014-09-04 18:13:01 +08002913
2914 if (to_journal) {
Chao Yudfc08a12016-02-14 18:50:40 +08002915 offset = lookup_journal_in_cursum(journal,
Chao Yu184a5cd2014-09-04 18:13:01 +08002916 SIT_JOURNAL, segno, 1);
2917 f2fs_bug_on(sbi, offset < 0);
Chao Yudfc08a12016-02-14 18:50:40 +08002918 segno_in_journal(journal, offset) =
Chao Yu184a5cd2014-09-04 18:13:01 +08002919 cpu_to_le32(segno);
2920 seg_info_to_raw_sit(se,
Chao Yudfc08a12016-02-14 18:50:40 +08002921 &sit_in_journal(journal, offset));
Chao Yu184a5cd2014-09-04 18:13:01 +08002922 } else {
2923 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
2924 seg_info_to_raw_sit(se,
2925 &raw_sit->entries[sit_offset]);
2926 }
2927
2928 __clear_bit(segno, bitmap);
2929 sit_i->dirty_sentries--;
2930 ses->entry_cnt--;
2931 }
2932
Chao Yub7ad7512016-02-19 18:08:46 +08002933 if (to_journal)
2934 up_write(&curseg->journal_rwsem);
2935 else
Chao Yu184a5cd2014-09-04 18:13:01 +08002936 f2fs_put_page(page, 1);
2937
2938 f2fs_bug_on(sbi, ses->entry_cnt);
2939 release_sit_entry_set(ses);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002940 }
Chao Yu184a5cd2014-09-04 18:13:01 +08002941
2942 f2fs_bug_on(sbi, !list_empty(head));
2943 f2fs_bug_on(sbi, sit_i->dirty_sentries);
Chao Yu184a5cd2014-09-04 18:13:01 +08002944out:
Chao Yuc473f1a2017-04-27 20:40:39 +08002945 if (cpc->reason & CP_DISCARD) {
Yunlei He650d3c42016-12-22 11:46:24 +08002946 __u64 trim_start = cpc->trim_start;
2947
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002948 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++)
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08002949 add_discard_addrs(sbi, cpc, false);
Yunlei He650d3c42016-12-22 11:46:24 +08002950
2951 cpc->trim_start = trim_start;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002952 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002953 mutex_unlock(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002954
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002955 set_prefree_as_free_segments(sbi);
2956}
2957
2958static int build_sit_info(struct f2fs_sb_info *sbi)
2959{
2960 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002961 struct sit_info *sit_i;
2962 unsigned int sit_segs, start;
Chao Yuae27d622017-01-07 18:52:34 +08002963 char *src_bitmap;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002964 unsigned int bitmap_size;
2965
2966 /* allocate memory for SIT information */
2967 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
2968 if (!sit_i)
2969 return -ENOMEM;
2970
2971 SM_I(sbi)->sit_info = sit_i;
2972
Michal Hockoa7c3e902017-05-08 15:57:09 -07002973 sit_i->sentries = kvzalloc(MAIN_SEGS(sbi) *
Jaegeuk Kim39307a82015-09-22 13:50:47 -07002974 sizeof(struct seg_entry), GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002975 if (!sit_i->sentries)
2976 return -ENOMEM;
2977
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002978 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Michal Hockoa7c3e902017-05-08 15:57:09 -07002979 sit_i->dirty_sentries_bitmap = kvzalloc(bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002980 if (!sit_i->dirty_sentries_bitmap)
2981 return -ENOMEM;
2982
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002983 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002984 sit_i->sentries[start].cur_valid_map
2985 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
2986 sit_i->sentries[start].ckpt_valid_map
2987 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07002988 if (!sit_i->sentries[start].cur_valid_map ||
Jaegeuk Kim3e025742016-08-02 10:56:40 -07002989 !sit_i->sentries[start].ckpt_valid_map)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002990 return -ENOMEM;
Jaegeuk Kim3e025742016-08-02 10:56:40 -07002991
Chao Yu355e7892017-01-07 18:51:01 +08002992#ifdef CONFIG_F2FS_CHECK_FS
2993 sit_i->sentries[start].cur_valid_map_mir
2994 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
2995 if (!sit_i->sentries[start].cur_valid_map_mir)
2996 return -ENOMEM;
2997#endif
2998
Jaegeuk Kim3e025742016-08-02 10:56:40 -07002999 if (f2fs_discard_en(sbi)) {
3000 sit_i->sentries[start].discard_map
3001 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
3002 if (!sit_i->sentries[start].discard_map)
3003 return -ENOMEM;
3004 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003005 }
3006
Jaegeuk Kim60a3b782015-02-10 16:44:29 -08003007 sit_i->tmp_map = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
3008 if (!sit_i->tmp_map)
3009 return -ENOMEM;
3010
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003011 if (sbi->segs_per_sec > 1) {
Michal Hockoa7c3e902017-05-08 15:57:09 -07003012 sit_i->sec_entries = kvzalloc(MAIN_SECS(sbi) *
Jaegeuk Kim39307a82015-09-22 13:50:47 -07003013 sizeof(struct sec_entry), GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003014 if (!sit_i->sec_entries)
3015 return -ENOMEM;
3016 }
3017
3018 /* get information related with SIT */
3019 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
3020
3021 /* setup SIT bitmap from ckeckpoint pack */
3022 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
3023 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
3024
Chao Yuae27d622017-01-07 18:52:34 +08003025 sit_i->sit_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
3026 if (!sit_i->sit_bitmap)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003027 return -ENOMEM;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003028
Chao Yuae27d622017-01-07 18:52:34 +08003029#ifdef CONFIG_F2FS_CHECK_FS
3030 sit_i->sit_bitmap_mir = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
3031 if (!sit_i->sit_bitmap_mir)
3032 return -ENOMEM;
3033#endif
3034
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003035 /* init SIT information */
3036 sit_i->s_ops = &default_salloc_ops;
3037
3038 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
3039 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
Jaegeuk Kimc79b7ff2016-11-14 18:20:10 -08003040 sit_i->written_valid_blocks = 0;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003041 sit_i->bitmap_size = bitmap_size;
3042 sit_i->dirty_sentries = 0;
3043 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
3044 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
Deepa Dinamani48fbfe52017-05-08 15:59:10 -07003045 sit_i->mounted_time = ktime_get_real_seconds();
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003046 mutex_init(&sit_i->sentry_lock);
3047 return 0;
3048}
3049
3050static int build_free_segmap(struct f2fs_sb_info *sbi)
3051{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003052 struct free_segmap_info *free_i;
3053 unsigned int bitmap_size, sec_bitmap_size;
3054
3055 /* allocate memory for free segmap information */
3056 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
3057 if (!free_i)
3058 return -ENOMEM;
3059
3060 SM_I(sbi)->free_info = free_i;
3061
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003062 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Michal Hockoa7c3e902017-05-08 15:57:09 -07003063 free_i->free_segmap = kvmalloc(bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003064 if (!free_i->free_segmap)
3065 return -ENOMEM;
3066
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003067 sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
Michal Hockoa7c3e902017-05-08 15:57:09 -07003068 free_i->free_secmap = kvmalloc(sec_bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003069 if (!free_i->free_secmap)
3070 return -ENOMEM;
3071
3072 /* set all segments as dirty temporarily */
3073 memset(free_i->free_segmap, 0xff, bitmap_size);
3074 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
3075
3076 /* init free segmap information */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003077 free_i->start_segno = GET_SEGNO_FROM_SEG0(sbi, MAIN_BLKADDR(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003078 free_i->free_segments = 0;
3079 free_i->free_sections = 0;
Chao Yu1a118cc2015-02-11 18:20:38 +08003080 spin_lock_init(&free_i->segmap_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003081 return 0;
3082}
3083
3084static int build_curseg(struct f2fs_sb_info *sbi)
3085{
Namjae Jeon1042d602012-12-01 10:56:13 +09003086 struct curseg_info *array;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003087 int i;
3088
Fabian Frederickb434bab2014-06-23 18:39:15 +02003089 array = kcalloc(NR_CURSEG_TYPE, sizeof(*array), GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003090 if (!array)
3091 return -ENOMEM;
3092
3093 SM_I(sbi)->curseg_array = array;
3094
3095 for (i = 0; i < NR_CURSEG_TYPE; i++) {
3096 mutex_init(&array[i].curseg_mutex);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003097 array[i].sum_blk = kzalloc(PAGE_SIZE, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003098 if (!array[i].sum_blk)
3099 return -ENOMEM;
Chao Yub7ad7512016-02-19 18:08:46 +08003100 init_rwsem(&array[i].journal_rwsem);
3101 array[i].journal = kzalloc(sizeof(struct f2fs_journal),
3102 GFP_KERNEL);
3103 if (!array[i].journal)
3104 return -ENOMEM;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003105 array[i].segno = NULL_SEGNO;
3106 array[i].next_blkoff = 0;
3107 }
3108 return restore_curseg_summaries(sbi);
3109}
3110
3111static void build_sit_entries(struct f2fs_sb_info *sbi)
3112{
3113 struct sit_info *sit_i = SIT_I(sbi);
3114 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08003115 struct f2fs_journal *journal = curseg->journal;
Yunlei He9c094042016-09-24 12:29:18 +08003116 struct seg_entry *se;
3117 struct f2fs_sit_entry sit;
Chao Yu74de5932013-11-22 09:09:59 +08003118 int sit_blk_cnt = SIT_BLK_CNT(sbi);
3119 unsigned int i, start, end;
3120 unsigned int readed, start_blk = 0;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003121
Chao Yu74de5932013-11-22 09:09:59 +08003122 do {
Jaegeuk Kim664ba972016-10-18 11:07:45 -07003123 readed = ra_meta_pages(sbi, start_blk, BIO_MAX_PAGES,
3124 META_SIT, true);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003125
Chao Yu74de5932013-11-22 09:09:59 +08003126 start = start_blk * sit_i->sents_per_block;
3127 end = (start_blk + readed) * sit_i->sents_per_block;
3128
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003129 for (; start < end && start < MAIN_SEGS(sbi); start++) {
Chao Yu74de5932013-11-22 09:09:59 +08003130 struct f2fs_sit_block *sit_blk;
Chao Yu74de5932013-11-22 09:09:59 +08003131 struct page *page;
3132
Yunlei He9c094042016-09-24 12:29:18 +08003133 se = &sit_i->sentries[start];
Chao Yu74de5932013-11-22 09:09:59 +08003134 page = get_current_sit_page(sbi, start);
3135 sit_blk = (struct f2fs_sit_block *)page_address(page);
3136 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
3137 f2fs_put_page(page, 1);
Chao Yud600af232016-08-19 23:13:47 +08003138
Chao Yu74de5932013-11-22 09:09:59 +08003139 check_block_count(sbi, start, &sit);
3140 seg_info_from_raw_sit(se, &sit);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07003141
3142 /* build discard map only one time */
Jaegeuk Kim3e025742016-08-02 10:56:40 -07003143 if (f2fs_discard_en(sbi)) {
Chao Yu1f43e2a2017-04-28 13:56:08 +08003144 if (is_set_ckpt_flags(sbi, CP_TRIMMED_FLAG)) {
3145 memset(se->discard_map, 0xff,
3146 SIT_VBLOCK_MAP_SIZE);
3147 } else {
3148 memcpy(se->discard_map,
3149 se->cur_valid_map,
3150 SIT_VBLOCK_MAP_SIZE);
3151 sbi->discard_blks +=
3152 sbi->blocks_per_seg -
3153 se->valid_blocks;
3154 }
Jaegeuk Kim3e025742016-08-02 10:56:40 -07003155 }
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07003156
Chao Yud600af232016-08-19 23:13:47 +08003157 if (sbi->segs_per_sec > 1)
3158 get_sec_entry(sbi, start)->valid_blocks +=
3159 se->valid_blocks;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003160 }
Chao Yu74de5932013-11-22 09:09:59 +08003161 start_blk += readed;
3162 } while (start_blk < sit_blk_cnt);
Chao Yud600af232016-08-19 23:13:47 +08003163
3164 down_read(&curseg->journal_rwsem);
3165 for (i = 0; i < sits_in_cursum(journal); i++) {
Chao Yud600af232016-08-19 23:13:47 +08003166 unsigned int old_valid_blocks;
3167
3168 start = le32_to_cpu(segno_in_journal(journal, i));
3169 se = &sit_i->sentries[start];
3170 sit = sit_in_journal(journal, i);
3171
3172 old_valid_blocks = se->valid_blocks;
3173
3174 check_block_count(sbi, start, &sit);
3175 seg_info_from_raw_sit(se, &sit);
3176
3177 if (f2fs_discard_en(sbi)) {
Chao Yu1f43e2a2017-04-28 13:56:08 +08003178 if (is_set_ckpt_flags(sbi, CP_TRIMMED_FLAG)) {
3179 memset(se->discard_map, 0xff,
3180 SIT_VBLOCK_MAP_SIZE);
3181 } else {
3182 memcpy(se->discard_map, se->cur_valid_map,
3183 SIT_VBLOCK_MAP_SIZE);
3184 sbi->discard_blks += old_valid_blocks -
3185 se->valid_blocks;
3186 }
Chao Yud600af232016-08-19 23:13:47 +08003187 }
3188
3189 if (sbi->segs_per_sec > 1)
3190 get_sec_entry(sbi, start)->valid_blocks +=
3191 se->valid_blocks - old_valid_blocks;
3192 }
3193 up_read(&curseg->journal_rwsem);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003194}
3195
3196static void init_free_segmap(struct f2fs_sb_info *sbi)
3197{
3198 unsigned int start;
3199 int type;
3200
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003201 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003202 struct seg_entry *sentry = get_seg_entry(sbi, start);
3203 if (!sentry->valid_blocks)
3204 __set_free(sbi, start);
Jaegeuk Kimc79b7ff2016-11-14 18:20:10 -08003205 else
3206 SIT_I(sbi)->written_valid_blocks +=
3207 sentry->valid_blocks;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003208 }
3209
3210 /* set use the current segments */
3211 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
3212 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
3213 __set_test_and_inuse(sbi, curseg_t->segno);
3214 }
3215}
3216
3217static void init_dirty_segmap(struct f2fs_sb_info *sbi)
3218{
3219 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
3220 struct free_segmap_info *free_i = FREE_I(sbi);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003221 unsigned int segno = 0, offset = 0;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003222 unsigned short valid_blocks;
3223
Namjae Jeon8736fbf2013-06-16 09:49:11 +09003224 while (1) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003225 /* find dirty segment based on free segmap */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003226 segno = find_next_inuse(free_i, MAIN_SEGS(sbi), offset);
3227 if (segno >= MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003228 break;
3229 offset = segno + 1;
Jaegeuk Kim302bd342017-04-07 14:33:22 -07003230 valid_blocks = get_valid_blocks(sbi, segno, false);
Jaegeuk Kimec325b52014-09-02 16:24:11 -07003231 if (valid_blocks == sbi->blocks_per_seg || !valid_blocks)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003232 continue;
Jaegeuk Kimec325b52014-09-02 16:24:11 -07003233 if (valid_blocks > sbi->blocks_per_seg) {
3234 f2fs_bug_on(sbi, 1);
3235 continue;
3236 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003237 mutex_lock(&dirty_i->seglist_lock);
3238 __locate_dirty_segment(sbi, segno, DIRTY);
3239 mutex_unlock(&dirty_i->seglist_lock);
3240 }
3241}
3242
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09003243static int init_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003244{
3245 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003246 unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003247
Michal Hockoa7c3e902017-05-08 15:57:09 -07003248 dirty_i->victim_secmap = kvzalloc(bitmap_size, GFP_KERNEL);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09003249 if (!dirty_i->victim_secmap)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003250 return -ENOMEM;
3251 return 0;
3252}
3253
3254static int build_dirty_segmap(struct f2fs_sb_info *sbi)
3255{
3256 struct dirty_seglist_info *dirty_i;
3257 unsigned int bitmap_size, i;
3258
3259 /* allocate memory for dirty segments list information */
3260 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
3261 if (!dirty_i)
3262 return -ENOMEM;
3263
3264 SM_I(sbi)->dirty_info = dirty_i;
3265 mutex_init(&dirty_i->seglist_lock);
3266
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003267 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003268
3269 for (i = 0; i < NR_DIRTY_TYPE; i++) {
Michal Hockoa7c3e902017-05-08 15:57:09 -07003270 dirty_i->dirty_segmap[i] = kvzalloc(bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003271 if (!dirty_i->dirty_segmap[i])
3272 return -ENOMEM;
3273 }
3274
3275 init_dirty_segmap(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09003276 return init_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003277}
3278
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09003279/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003280 * Update min, max modified time for cost-benefit GC algorithm
3281 */
3282static void init_min_max_mtime(struct f2fs_sb_info *sbi)
3283{
3284 struct sit_info *sit_i = SIT_I(sbi);
3285 unsigned int segno;
3286
3287 mutex_lock(&sit_i->sentry_lock);
3288
3289 sit_i->min_mtime = LLONG_MAX;
3290
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003291 for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003292 unsigned int i;
3293 unsigned long long mtime = 0;
3294
3295 for (i = 0; i < sbi->segs_per_sec; i++)
3296 mtime += get_seg_entry(sbi, segno + i)->mtime;
3297
3298 mtime = div_u64(mtime, sbi->segs_per_sec);
3299
3300 if (sit_i->min_mtime > mtime)
3301 sit_i->min_mtime = mtime;
3302 }
3303 sit_i->max_mtime = get_mtime(sbi);
3304 mutex_unlock(&sit_i->sentry_lock);
3305}
3306
3307int build_segment_manager(struct f2fs_sb_info *sbi)
3308{
3309 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
3310 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Namjae Jeon1042d602012-12-01 10:56:13 +09003311 struct f2fs_sm_info *sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003312 int err;
3313
3314 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
3315 if (!sm_info)
3316 return -ENOMEM;
3317
3318 /* init sm info */
3319 sbi->sm_info = sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003320 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
3321 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
3322 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
3323 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
3324 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
3325 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
3326 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
Jaegeuk Kim58c41032014-03-19 14:17:21 +09003327 sm_info->rec_prefree_segments = sm_info->main_segments *
3328 DEF_RECLAIM_PREFREE_SEGMENTS / 100;
Jaegeuk Kim44a83492016-07-13 18:23:35 -07003329 if (sm_info->rec_prefree_segments > DEF_MAX_RECLAIM_PREFREE_SEGMENTS)
3330 sm_info->rec_prefree_segments = DEF_MAX_RECLAIM_PREFREE_SEGMENTS;
3331
Jaegeuk Kim52763a42016-06-13 09:47:48 -07003332 if (!test_opt(sbi, LFS))
3333 sm_info->ipu_policy = 1 << F2FS_IPU_FSYNC;
Jaegeuk Kim216fbd62013-11-07 13:13:42 +09003334 sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -07003335 sm_info->min_fsync_blocks = DEF_MIN_FSYNC_BLOCKS;
Jaegeuk Kimef095d12017-03-24 20:05:13 -04003336 sm_info->min_hot_blocks = DEF_MIN_HOT_BLOCKS;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003337
Jaegeuk Kimbba681c2015-01-26 17:41:23 -08003338 sm_info->trim_sections = DEF_BATCHED_TRIM_SECTIONS;
3339
Chao Yu184a5cd2014-09-04 18:13:01 +08003340 INIT_LIST_HEAD(&sm_info->sit_entry_set);
3341
Yunlei Hed4fdf8b2017-06-01 16:43:51 +08003342 if (!f2fs_readonly(sbi->sb)) {
Gu Zheng2163d192014-04-27 14:21:33 +08003343 err = create_flush_cmd_control(sbi);
3344 if (err)
Gu Zhenga688b9d9e2014-04-27 14:21:21 +08003345 return err;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +09003346 }
3347
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08003348 err = create_discard_cmd_control(sbi);
3349 if (err)
3350 return err;
3351
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003352 err = build_sit_info(sbi);
3353 if (err)
3354 return err;
3355 err = build_free_segmap(sbi);
3356 if (err)
3357 return err;
3358 err = build_curseg(sbi);
3359 if (err)
3360 return err;
3361
3362 /* reinit free segmap based on SIT */
3363 build_sit_entries(sbi);
3364
3365 init_free_segmap(sbi);
3366 err = build_dirty_segmap(sbi);
3367 if (err)
3368 return err;
3369
3370 init_min_max_mtime(sbi);
3371 return 0;
3372}
3373
3374static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
3375 enum dirty_type dirty_type)
3376{
3377 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
3378
3379 mutex_lock(&dirty_i->seglist_lock);
Jaegeuk Kim39307a82015-09-22 13:50:47 -07003380 kvfree(dirty_i->dirty_segmap[dirty_type]);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003381 dirty_i->nr_dirty[dirty_type] = 0;
3382 mutex_unlock(&dirty_i->seglist_lock);
3383}
3384
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09003385static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003386{
3387 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim39307a82015-09-22 13:50:47 -07003388 kvfree(dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003389}
3390
3391static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
3392{
3393 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
3394 int i;
3395
3396 if (!dirty_i)
3397 return;
3398
3399 /* discard pre-free/dirty segments list */
3400 for (i = 0; i < NR_DIRTY_TYPE; i++)
3401 discard_dirty_segmap(sbi, i);
3402
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09003403 destroy_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003404 SM_I(sbi)->dirty_info = NULL;
3405 kfree(dirty_i);
3406}
3407
3408static void destroy_curseg(struct f2fs_sb_info *sbi)
3409{
3410 struct curseg_info *array = SM_I(sbi)->curseg_array;
3411 int i;
3412
3413 if (!array)
3414 return;
3415 SM_I(sbi)->curseg_array = NULL;
Chao Yub7ad7512016-02-19 18:08:46 +08003416 for (i = 0; i < NR_CURSEG_TYPE; i++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003417 kfree(array[i].sum_blk);
Chao Yub7ad7512016-02-19 18:08:46 +08003418 kfree(array[i].journal);
3419 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003420 kfree(array);
3421}
3422
3423static void destroy_free_segmap(struct f2fs_sb_info *sbi)
3424{
3425 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
3426 if (!free_i)
3427 return;
3428 SM_I(sbi)->free_info = NULL;
Jaegeuk Kim39307a82015-09-22 13:50:47 -07003429 kvfree(free_i->free_segmap);
3430 kvfree(free_i->free_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003431 kfree(free_i);
3432}
3433
3434static void destroy_sit_info(struct f2fs_sb_info *sbi)
3435{
3436 struct sit_info *sit_i = SIT_I(sbi);
3437 unsigned int start;
3438
3439 if (!sit_i)
3440 return;
3441
3442 if (sit_i->sentries) {
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003443 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003444 kfree(sit_i->sentries[start].cur_valid_map);
Chao Yu355e7892017-01-07 18:51:01 +08003445#ifdef CONFIG_F2FS_CHECK_FS
3446 kfree(sit_i->sentries[start].cur_valid_map_mir);
3447#endif
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003448 kfree(sit_i->sentries[start].ckpt_valid_map);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07003449 kfree(sit_i->sentries[start].discard_map);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003450 }
3451 }
Jaegeuk Kim60a3b782015-02-10 16:44:29 -08003452 kfree(sit_i->tmp_map);
3453
Jaegeuk Kim39307a82015-09-22 13:50:47 -07003454 kvfree(sit_i->sentries);
3455 kvfree(sit_i->sec_entries);
3456 kvfree(sit_i->dirty_sentries_bitmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003457
3458 SM_I(sbi)->sit_info = NULL;
3459 kfree(sit_i->sit_bitmap);
Chao Yuae27d622017-01-07 18:52:34 +08003460#ifdef CONFIG_F2FS_CHECK_FS
3461 kfree(sit_i->sit_bitmap_mir);
3462#endif
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003463 kfree(sit_i);
3464}
3465
3466void destroy_segment_manager(struct f2fs_sb_info *sbi)
3467{
3468 struct f2fs_sm_info *sm_info = SM_I(sbi);
Gu Zhenga688b9d9e2014-04-27 14:21:21 +08003469
Chao Yu3b03f722013-11-06 09:12:04 +08003470 if (!sm_info)
3471 return;
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -08003472 destroy_flush_cmd_control(sbi, true);
Chao Yuf0994052017-03-27 18:14:04 +08003473 destroy_discard_cmd_control(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003474 destroy_dirty_segmap(sbi);
3475 destroy_curseg(sbi);
3476 destroy_free_segmap(sbi);
3477 destroy_sit_info(sbi);
3478 sbi->sm_info = NULL;
3479 kfree(sm_info);
3480}
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09003481
3482int __init create_segment_manager_caches(void)
3483{
3484 discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
Gu Zhenge8512d22014-03-07 18:43:28 +08003485 sizeof(struct discard_entry));
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09003486 if (!discard_entry_slab)
Chao Yu184a5cd2014-09-04 18:13:01 +08003487 goto fail;
3488
Jaegeuk Kimb01a9202017-01-09 14:13:03 -08003489 discard_cmd_slab = f2fs_kmem_cache_create("discard_cmd",
3490 sizeof(struct discard_cmd));
3491 if (!discard_cmd_slab)
Chao Yu6ab2a302016-09-05 12:28:26 +08003492 goto destroy_discard_entry;
Chao Yu275b66b2016-08-29 23:58:34 +08003493
Chao Yu184a5cd2014-09-04 18:13:01 +08003494 sit_entry_set_slab = f2fs_kmem_cache_create("sit_entry_set",
Changman Leec9ee0082014-11-21 15:42:07 +09003495 sizeof(struct sit_entry_set));
Chao Yu184a5cd2014-09-04 18:13:01 +08003496 if (!sit_entry_set_slab)
Jaegeuk Kimb01a9202017-01-09 14:13:03 -08003497 goto destroy_discard_cmd;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07003498
3499 inmem_entry_slab = f2fs_kmem_cache_create("inmem_page_entry",
3500 sizeof(struct inmem_pages));
3501 if (!inmem_entry_slab)
3502 goto destroy_sit_entry_set;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09003503 return 0;
Chao Yu184a5cd2014-09-04 18:13:01 +08003504
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07003505destroy_sit_entry_set:
3506 kmem_cache_destroy(sit_entry_set_slab);
Jaegeuk Kimb01a9202017-01-09 14:13:03 -08003507destroy_discard_cmd:
3508 kmem_cache_destroy(discard_cmd_slab);
Chao Yu6ab2a302016-09-05 12:28:26 +08003509destroy_discard_entry:
Chao Yu184a5cd2014-09-04 18:13:01 +08003510 kmem_cache_destroy(discard_entry_slab);
3511fail:
3512 return -ENOMEM;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09003513}
3514
3515void destroy_segment_manager_caches(void)
3516{
Chao Yu184a5cd2014-09-04 18:13:01 +08003517 kmem_cache_destroy(sit_entry_set_slab);
Jaegeuk Kimb01a9202017-01-09 14:13:03 -08003518 kmem_cache_destroy(discard_cmd_slab);
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09003519 kmem_cache_destroy(discard_entry_slab);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07003520 kmem_cache_destroy(inmem_entry_slab);
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09003521}