blob: 1387925a0d83b142d7d8a6bd35127dc9fc9079e2 [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);
Chao Yu7f2b4e82017-08-08 19:09:08 +0800216retry:
Chao Yu28bc1062016-02-06 14:40:34 +0800217 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu7f2b4e82017-08-08 19:09:08 +0800218 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
219 if (err) {
220 if (err == -ENOMEM) {
221 congestion_wait(BLK_RW_ASYNC, HZ/50);
222 cond_resched();
223 goto retry;
224 }
Chao Yu28bc1062016-02-06 14:40:34 +0800225 err = -EAGAIN;
226 goto next;
227 }
228 get_node_info(sbi, dn.nid, &ni);
229 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
230 cur->old_addr, ni.version, true, true);
231 f2fs_put_dnode(&dn);
232 }
233next:
Jaegeuk Kim63c52d72016-04-12 14:11:03 -0700234 /* we don't need to invalidate this in the sccessful status */
235 if (drop || recover)
236 ClearPageUptodate(page);
Chao Yu28bc1062016-02-06 14:40:34 +0800237 set_page_private(page, 0);
Chao Yuc81ced02016-04-29 20:13:36 +0800238 ClearPagePrivate(page);
Chao Yu28bc1062016-02-06 14:40:34 +0800239 f2fs_put_page(page, 1);
Chao Yudecd36b2015-08-07 18:42:09 +0800240
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700241 list_del(&cur->list);
242 kmem_cache_free(inmem_entry_slab, cur);
Jaegeuk Kim8dcf2ff72014-12-05 17:18:15 -0800243 dec_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700244 }
Chao Yu28bc1062016-02-06 14:40:34 +0800245 return err;
Chao Yu29b96b52016-02-06 14:38:29 +0800246}
247
248void drop_inmem_pages(struct inode *inode)
249{
250 struct f2fs_inode_info *fi = F2FS_I(inode);
251
252 mutex_lock(&fi->inmem_lock);
Chao Yu28bc1062016-02-06 14:40:34 +0800253 __revoke_inmem_pages(inode, &fi->inmem_pages, true, false);
Chao Yu29b96b52016-02-06 14:38:29 +0800254 mutex_unlock(&fi->inmem_lock);
Chao Yu5fe45742017-01-07 18:50:26 +0800255
256 clear_inode_flag(inode, FI_ATOMIC_FILE);
257 stat_dec_atomic_write(inode);
Chao Yu29b96b52016-02-06 14:38:29 +0800258}
259
Jaegeuk Kim8c242db2017-03-17 09:55:52 +0800260void drop_inmem_page(struct inode *inode, struct page *page)
261{
262 struct f2fs_inode_info *fi = F2FS_I(inode);
263 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
264 struct list_head *head = &fi->inmem_pages;
265 struct inmem_pages *cur = NULL;
266
267 f2fs_bug_on(sbi, !IS_ATOMIC_WRITTEN_PAGE(page));
268
269 mutex_lock(&fi->inmem_lock);
270 list_for_each_entry(cur, head, list) {
271 if (cur->page == page)
272 break;
273 }
274
275 f2fs_bug_on(sbi, !cur || cur->page != page);
276 list_del(&cur->list);
277 mutex_unlock(&fi->inmem_lock);
278
279 dec_page_count(sbi, F2FS_INMEM_PAGES);
280 kmem_cache_free(inmem_entry_slab, cur);
281
282 ClearPageUptodate(page);
283 set_page_private(page, 0);
284 ClearPagePrivate(page);
285 f2fs_put_page(page, 0);
286
287 trace_f2fs_commit_inmem_page(page, INMEM_INVALIDATE);
288}
289
Chao Yu28bc1062016-02-06 14:40:34 +0800290static int __commit_inmem_pages(struct inode *inode,
291 struct list_head *revoke_list)
Chao Yu29b96b52016-02-06 14:38:29 +0800292{
293 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
294 struct f2fs_inode_info *fi = F2FS_I(inode);
295 struct inmem_pages *cur, *tmp;
296 struct f2fs_io_info fio = {
297 .sbi = sbi,
298 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -0500299 .op = REQ_OP_WRITE,
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600300 .op_flags = REQ_SYNC | REQ_PRIO,
Chao Yub0af6d42017-08-02 23:21:48 +0800301 .io_type = FS_DATA_IO,
Chao Yu29b96b52016-02-06 14:38:29 +0800302 };
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800303 pgoff_t last_idx = ULONG_MAX;
Chao Yu29b96b52016-02-06 14:38:29 +0800304 int err = 0;
305
306 list_for_each_entry_safe(cur, tmp, &fi->inmem_pages, list) {
Chao Yu28bc1062016-02-06 14:40:34 +0800307 struct page *page = cur->page;
308
309 lock_page(page);
310 if (page->mapping == inode->i_mapping) {
311 trace_f2fs_commit_inmem_page(page, INMEM);
312
313 set_page_dirty(page);
314 f2fs_wait_on_page_writeback(page, DATA, true);
Chao Yu933439c2016-10-11 22:57:01 +0800315 if (clear_page_dirty_for_io(page)) {
Chao Yu29b96b52016-02-06 14:38:29 +0800316 inode_dec_dirty_pages(inode);
Chao Yu933439c2016-10-11 22:57:01 +0800317 remove_dirty_inode(inode);
318 }
Jaegeuk Kim640cc182017-07-19 10:59:55 -0700319retry:
Chao Yu28bc1062016-02-06 14:40:34 +0800320 fio.page = page;
Hou Pengyange959c8f2017-04-25 12:45:13 +0000321 fio.old_blkaddr = NULL_ADDR;
Jaegeuk Kim4d978072017-04-26 11:11:12 -0700322 fio.encrypted_page = NULL;
Jaegeuk Kimcc156202017-05-12 13:51:34 -0700323 fio.need_lock = LOCK_DONE;
Chao Yu29b96b52016-02-06 14:38:29 +0800324 err = do_write_data_page(&fio);
325 if (err) {
Jaegeuk Kim640cc182017-07-19 10:59:55 -0700326 if (err == -ENOMEM) {
327 congestion_wait(BLK_RW_ASYNC, HZ/50);
328 cond_resched();
329 goto retry;
330 }
Chao Yu28bc1062016-02-06 14:40:34 +0800331 unlock_page(page);
Chao Yu29b96b52016-02-06 14:38:29 +0800332 break;
333 }
Chao Yu28bc1062016-02-06 14:40:34 +0800334 /* record old blkaddr for revoking */
335 cur->old_addr = fio.old_blkaddr;
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800336 last_idx = page->index;
Chao Yu29b96b52016-02-06 14:38:29 +0800337 }
Chao Yu28bc1062016-02-06 14:40:34 +0800338 unlock_page(page);
339 list_move_tail(&cur->list, revoke_list);
Chao Yu29b96b52016-02-06 14:38:29 +0800340 }
341
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800342 if (last_idx != ULONG_MAX)
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700343 f2fs_submit_merged_write_cond(sbi, inode, 0, last_idx, DATA);
Chao Yu28bc1062016-02-06 14:40:34 +0800344
345 if (!err)
346 __revoke_inmem_pages(inode, revoke_list, false, false);
347
Chao Yu29b96b52016-02-06 14:38:29 +0800348 return err;
349}
350
351int commit_inmem_pages(struct inode *inode)
352{
353 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
354 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yu28bc1062016-02-06 14:40:34 +0800355 struct list_head revoke_list;
356 int err;
Chao Yu29b96b52016-02-06 14:38:29 +0800357
Chao Yu28bc1062016-02-06 14:40:34 +0800358 INIT_LIST_HEAD(&revoke_list);
Chao Yu29b96b52016-02-06 14:38:29 +0800359 f2fs_balance_fs(sbi, true);
360 f2fs_lock_op(sbi);
361
Chao Yu5fe45742017-01-07 18:50:26 +0800362 set_inode_flag(inode, FI_ATOMIC_COMMIT);
363
Chao Yu29b96b52016-02-06 14:38:29 +0800364 mutex_lock(&fi->inmem_lock);
Chao Yu28bc1062016-02-06 14:40:34 +0800365 err = __commit_inmem_pages(inode, &revoke_list);
366 if (err) {
367 int ret;
368 /*
369 * try to revoke all committed pages, but still we could fail
370 * due to no memory or other reason, if that happened, EAGAIN
371 * will be returned, which means in such case, transaction is
372 * already not integrity, caller should use journal to do the
373 * recovery or rewrite & commit last transaction. For other
374 * error number, revoking was done by filesystem itself.
375 */
376 ret = __revoke_inmem_pages(inode, &revoke_list, false, true);
377 if (ret)
378 err = ret;
379
380 /* drop all uncommitted pages */
381 __revoke_inmem_pages(inode, &fi->inmem_pages, true, false);
382 }
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700383 mutex_unlock(&fi->inmem_lock);
384
Chao Yu5fe45742017-01-07 18:50:26 +0800385 clear_inode_flag(inode, FI_ATOMIC_COMMIT);
386
Chao Yu29b96b52016-02-06 14:38:29 +0800387 f2fs_unlock_op(sbi);
Jaegeuk Kimedb27de2015-07-25 00:52:52 -0700388 return err;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700389}
390
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900391/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900392 * This function balances dirty node and dentry pages.
393 * In addition, it controls garbage collection.
394 */
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800395void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900396{
Chao Yu0f348022016-09-26 19:45:55 +0800397#ifdef CONFIG_F2FS_FAULT_INJECTION
Chao Yu55523512017-02-25 11:08:28 +0800398 if (time_to_inject(sbi, FAULT_CHECKPOINT)) {
399 f2fs_show_injection_info(FAULT_CHECKPOINT);
Chao Yu0f348022016-09-26 19:45:55 +0800400 f2fs_stop_checkpoint(sbi, false);
Chao Yu55523512017-02-25 11:08:28 +0800401 }
Chao Yu0f348022016-09-26 19:45:55 +0800402#endif
403
Jaegeuk Kime589c2c2016-06-02 15:24:24 -0700404 /* balance_fs_bg is able to be pending */
Jaegeuk Kima7881892017-04-20 13:51:57 -0700405 if (need && excess_cached_nats(sbi))
Jaegeuk Kime589c2c2016-06-02 15:24:24 -0700406 f2fs_balance_fs_bg(sbi);
407
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900408 /*
Jaegeuk Kim029cd282012-12-21 17:20:21 +0900409 * We should do GC or end up with checkpoint, if there are so many dirty
410 * dir/node pages without enough free segments.
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900411 */
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -0700412 if (has_not_enough_free_secs(sbi, 0, 0)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900413 mutex_lock(&sbi->gc_mutex);
Jaegeuk Kime066b832017-04-13 15:17:00 -0700414 f2fs_gc(sbi, false, false, NULL_SEGNO);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900415 }
416}
417
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900418void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi)
419{
Chao Yu1dcc3362015-02-05 17:57:31 +0800420 /* try to shrink extent cache when there is no enough memory */
Jaegeuk Kim554df792015-06-19 13:41:23 -0700421 if (!available_free_memory(sbi, EXTENT_CACHE))
422 f2fs_shrink_extent_tree(sbi, EXTENT_CACHE_SHRINK_NUMBER);
Chao Yu1dcc3362015-02-05 17:57:31 +0800423
Jaegeuk Kim1b38dc82015-06-19 15:36:07 -0700424 /* check the # of cached NAT entries */
425 if (!available_free_memory(sbi, NAT_ENTRIES))
426 try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK);
427
Chao Yu31696582015-07-28 18:33:46 +0800428 if (!available_free_memory(sbi, FREE_NIDS))
Jaegeuk Kimad4edb82016-06-16 16:41:49 -0700429 try_to_free_nids(sbi, MAX_FREE_NIDS);
430 else
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -0800431 build_free_nids(sbi, false, false);
Chao Yu31696582015-07-28 18:33:46 +0800432
Jaegeuk Kim1c0f4bf2017-05-01 18:09:44 -0700433 if (!is_idle(sbi) && !excess_dirty_nats(sbi))
Jaegeuk Kimf455c8a2016-12-05 11:37:14 -0800434 return;
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -0800435
Jaegeuk Kim88a70a62014-12-10 15:20:48 -0800436 /* checkpoint is the only way to shrink partial cached entries */
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900437 if (!available_free_memory(sbi, NAT_ENTRIES) ||
Jaegeuk Kim60b99b42015-10-05 14:49:57 -0700438 !available_free_memory(sbi, INO_ENTRIES) ||
Chao Yu7d768d22016-01-18 18:31:18 +0800439 excess_prefree_segs(sbi) ||
440 excess_dirty_nats(sbi) ||
Jaegeuk Kimf455c8a2016-12-05 11:37:14 -0800441 f2fs_time_over(sbi, CP_TIME)) {
Chao Yue9f5b8b2016-02-14 18:54:33 +0800442 if (test_opt(sbi, DATA_FLUSH)) {
443 struct blk_plug plug;
444
445 blk_start_plug(&plug);
Chao Yu36b35a02015-12-17 17:13:28 +0800446 sync_dirty_inodes(sbi, FILE_INODE);
Chao Yue9f5b8b2016-02-14 18:54:33 +0800447 blk_finish_plug(&plug);
448 }
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900449 f2fs_sync_fs(sbi->sb, true);
Jaegeuk Kim42190d22016-01-09 13:45:17 -0800450 stat_inc_bg_cp_count(sbi->stat_info);
Chao Yu36b35a02015-12-17 17:13:28 +0800451 }
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900452}
453
Kinglong Mee20fda56b2017-03-04 22:13:10 +0800454static int __submit_flush_wait(struct f2fs_sb_info *sbi,
455 struct block_device *bdev)
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700456{
457 struct bio *bio = f2fs_bio_alloc(0);
458 int ret;
459
Jan Kara3adc5fcb2017-05-02 17:03:47 +0200460 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700461 bio->bi_bdev = bdev;
462 ret = submit_bio_wait(bio);
463 bio_put(bio);
Kinglong Mee20fda56b2017-03-04 22:13:10 +0800464
465 trace_f2fs_issue_flush(bdev, test_opt(sbi, NOBARRIER),
466 test_opt(sbi, FLUSH_MERGE), ret);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700467 return ret;
468}
469
470static int submit_flush_wait(struct f2fs_sb_info *sbi)
471{
Kinglong Mee20fda56b2017-03-04 22:13:10 +0800472 int ret = __submit_flush_wait(sbi, sbi->sb->s_bdev);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700473 int i;
474
Kinglong Mee20fda56b2017-03-04 22:13:10 +0800475 if (!sbi->s_ndevs || ret)
476 return ret;
477
478 for (i = 1; i < sbi->s_ndevs; i++) {
479 ret = __submit_flush_wait(sbi, FDEV(i).bdev);
480 if (ret)
481 break;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700482 }
483 return ret;
484}
485
Gu Zheng2163d192014-04-27 14:21:33 +0800486static int issue_flush_thread(void *data)
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900487{
488 struct f2fs_sb_info *sbi = data;
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800489 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800490 wait_queue_head_t *q = &fcc->flush_wait_queue;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900491repeat:
492 if (kthread_should_stop())
493 return 0;
494
Chao Yudc6febb2017-07-22 08:52:23 +0800495 sb_start_intwrite(sbi->sb);
496
Gu Zheng721bd4d2014-09-05 18:31:00 +0800497 if (!llist_empty(&fcc->issue_list)) {
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900498 struct flush_cmd *cmd, *next;
499 int ret;
500
Gu Zheng721bd4d2014-09-05 18:31:00 +0800501 fcc->dispatch_list = llist_del_all(&fcc->issue_list);
502 fcc->dispatch_list = llist_reverse_order(fcc->dispatch_list);
503
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700504 ret = submit_flush_wait(sbi);
Chao Yu8b8dd652017-03-25 17:19:58 +0800505 atomic_inc(&fcc->issued_flush);
506
Gu Zheng721bd4d2014-09-05 18:31:00 +0800507 llist_for_each_entry_safe(cmd, next,
508 fcc->dispatch_list, llnode) {
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900509 cmd->ret = ret;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900510 complete(&cmd->wait);
511 }
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800512 fcc->dispatch_list = NULL;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900513 }
514
Chao Yudc6febb2017-07-22 08:52:23 +0800515 sb_end_intwrite(sbi->sb);
516
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800517 wait_event_interruptible(*q,
Gu Zheng721bd4d2014-09-05 18:31:00 +0800518 kthread_should_stop() || !llist_empty(&fcc->issue_list));
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900519 goto repeat;
520}
521
522int f2fs_issue_flush(struct f2fs_sb_info *sbi)
523{
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800524 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
Chao Yuadf8d902014-05-08 17:00:35 +0800525 struct flush_cmd cmd;
Chao Yu8b8dd652017-03-25 17:19:58 +0800526 int ret;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900527
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700528 if (test_opt(sbi, NOBARRIER))
529 return 0;
530
Chao Yu8b8dd652017-03-25 17:19:58 +0800531 if (!test_opt(sbi, FLUSH_MERGE)) {
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700532 ret = submit_flush_wait(sbi);
Chao Yu8b8dd652017-03-25 17:19:58 +0800533 atomic_inc(&fcc->issued_flush);
534 return ret;
535 }
536
537 if (!atomic_read(&fcc->issing_flush)) {
538 atomic_inc(&fcc->issing_flush);
539 ret = submit_flush_wait(sbi);
540 atomic_dec(&fcc->issing_flush);
541
542 atomic_inc(&fcc->issued_flush);
Jaegeuk Kim740432f2015-08-14 11:43:56 -0700543 return ret;
544 }
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900545
Chao Yuadf8d902014-05-08 17:00:35 +0800546 init_completion(&cmd.wait);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900547
Chao Yu8b8dd652017-03-25 17:19:58 +0800548 atomic_inc(&fcc->issing_flush);
Gu Zheng721bd4d2014-09-05 18:31:00 +0800549 llist_add(&cmd.llnode, &fcc->issue_list);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900550
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800551 if (!fcc->dispatch_list)
552 wake_up(&fcc->flush_wait_queue);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900553
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800554 if (fcc->f2fs_issue_flush) {
555 wait_for_completion(&cmd.wait);
Chao Yu8b8dd652017-03-25 17:19:58 +0800556 atomic_dec(&fcc->issing_flush);
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800557 } else {
558 llist_del_all(&fcc->issue_list);
Chao Yu8b8dd652017-03-25 17:19:58 +0800559 atomic_set(&fcc->issing_flush, 0);
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800560 }
Chao Yuadf8d902014-05-08 17:00:35 +0800561
562 return cmd.ret;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900563}
564
Gu Zheng2163d192014-04-27 14:21:33 +0800565int create_flush_cmd_control(struct f2fs_sb_info *sbi)
566{
567 dev_t dev = sbi->sb->s_bdev->bd_dev;
568 struct flush_cmd_control *fcc;
569 int err = 0;
570
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800571 if (SM_I(sbi)->fcc_info) {
572 fcc = SM_I(sbi)->fcc_info;
Yunlong Songd871cd02017-06-24 15:57:19 +0800573 if (fcc->f2fs_issue_flush)
574 return err;
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800575 goto init_thread;
576 }
577
Gu Zheng2163d192014-04-27 14:21:33 +0800578 fcc = kzalloc(sizeof(struct flush_cmd_control), GFP_KERNEL);
579 if (!fcc)
580 return -ENOMEM;
Chao Yu8b8dd652017-03-25 17:19:58 +0800581 atomic_set(&fcc->issued_flush, 0);
582 atomic_set(&fcc->issing_flush, 0);
Gu Zheng2163d192014-04-27 14:21:33 +0800583 init_waitqueue_head(&fcc->flush_wait_queue);
Gu Zheng721bd4d2014-09-05 18:31:00 +0800584 init_llist_head(&fcc->issue_list);
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800585 SM_I(sbi)->fcc_info = fcc;
Yunlei Hed4fdf8b2017-06-01 16:43:51 +0800586 if (!test_opt(sbi, FLUSH_MERGE))
587 return err;
588
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800589init_thread:
Gu Zheng2163d192014-04-27 14:21:33 +0800590 fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi,
591 "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev));
592 if (IS_ERR(fcc->f2fs_issue_flush)) {
593 err = PTR_ERR(fcc->f2fs_issue_flush);
594 kfree(fcc);
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800595 SM_I(sbi)->fcc_info = NULL;
Gu Zheng2163d192014-04-27 14:21:33 +0800596 return err;
597 }
Gu Zheng2163d192014-04-27 14:21:33 +0800598
599 return err;
600}
601
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800602void destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free)
Gu Zheng2163d192014-04-27 14:21:33 +0800603{
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800604 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
Gu Zheng2163d192014-04-27 14:21:33 +0800605
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800606 if (fcc && fcc->f2fs_issue_flush) {
607 struct task_struct *flush_thread = fcc->f2fs_issue_flush;
608
609 fcc->f2fs_issue_flush = NULL;
610 kthread_stop(flush_thread);
611 }
612 if (free) {
613 kfree(fcc);
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800614 SM_I(sbi)->fcc_info = NULL;
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800615 }
Gu Zheng2163d192014-04-27 14:21:33 +0800616}
617
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900618static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
619 enum dirty_type dirty_type)
620{
621 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
622
623 /* need not be added */
624 if (IS_CURSEG(sbi, segno))
625 return;
626
627 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
628 dirty_i->nr_dirty[dirty_type]++;
629
630 if (dirty_type == DIRTY) {
631 struct seg_entry *sentry = get_seg_entry(sbi, segno);
Changman Lee4625d6a2013-10-25 17:31:57 +0900632 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900633
Jaegeuk Kimec325b52014-09-02 16:24:11 -0700634 if (unlikely(t >= DIRTY)) {
635 f2fs_bug_on(sbi, 1);
636 return;
637 }
Changman Lee4625d6a2013-10-25 17:31:57 +0900638 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
639 dirty_i->nr_dirty[t]++;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900640 }
641}
642
643static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
644 enum dirty_type dirty_type)
645{
646 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
647
648 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
649 dirty_i->nr_dirty[dirty_type]--;
650
651 if (dirty_type == DIRTY) {
Changman Lee4625d6a2013-10-25 17:31:57 +0900652 struct seg_entry *sentry = get_seg_entry(sbi, segno);
653 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900654
Changman Lee4625d6a2013-10-25 17:31:57 +0900655 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
656 dirty_i->nr_dirty[t]--;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900657
Jaegeuk Kim302bd342017-04-07 14:33:22 -0700658 if (get_valid_blocks(sbi, segno, true) == 0)
Jaegeuk Kim4ddb1a42017-04-07 15:08:17 -0700659 clear_bit(GET_SEC_FROM_SEG(sbi, segno),
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +0900660 dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900661 }
662}
663
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900664/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900665 * Should not occur error such as -ENOMEM.
666 * Adding dirty entry into seglist is not critical operation.
667 * If a given segment is one of current working segments, it won't be added.
668 */
Haicheng Li8d8451a2013-06-13 16:59:28 +0800669static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900670{
671 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
672 unsigned short valid_blocks;
673
674 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
675 return;
676
677 mutex_lock(&dirty_i->seglist_lock);
678
Jaegeuk Kim302bd342017-04-07 14:33:22 -0700679 valid_blocks = get_valid_blocks(sbi, segno, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900680
681 if (valid_blocks == 0) {
682 __locate_dirty_segment(sbi, segno, PRE);
683 __remove_dirty_segment(sbi, segno, DIRTY);
684 } else if (valid_blocks < sbi->blocks_per_seg) {
685 __locate_dirty_segment(sbi, segno, DIRTY);
686 } else {
687 /* Recovery routine with SSR needs this */
688 __remove_dirty_segment(sbi, segno, DIRTY);
689 }
690
691 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900692}
693
Chao Yu004b6862017-04-14 23:24:55 +0800694static struct discard_cmd *__create_discard_cmd(struct f2fs_sb_info *sbi,
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800695 struct block_device *bdev, block_t lstart,
696 block_t start, block_t len)
Chao Yu275b66b2016-08-29 23:58:34 +0800697{
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -0800698 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Chao Yuba48a332017-04-15 14:09:37 +0800699 struct list_head *pend_list;
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800700 struct discard_cmd *dc;
Chao Yu275b66b2016-08-29 23:58:34 +0800701
Chao Yuba48a332017-04-15 14:09:37 +0800702 f2fs_bug_on(sbi, !len);
703
704 pend_list = &dcc->pend_list[plist_idx(len)];
705
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800706 dc = f2fs_kmem_cache_alloc(discard_cmd_slab, GFP_NOFS);
707 INIT_LIST_HEAD(&dc->list);
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800708 dc->bdev = bdev;
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800709 dc->lstart = lstart;
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800710 dc->start = start;
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800711 dc->len = len;
Chao Yuec9895a2017-04-26 17:39:54 +0800712 dc->ref = 0;
Jaegeuk Kim15469962017-01-09 20:32:07 -0800713 dc->state = D_PREP;
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800714 dc->error = 0;
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800715 init_completion(&dc->wait);
Chao Yu22d375d2017-04-05 18:19:48 +0800716 list_add_tail(&dc->list, pend_list);
Chao Yu5f323662017-03-25 17:19:59 +0800717 atomic_inc(&dcc->discard_cmd_cnt);
Chao Yud84d1cb2017-04-18 19:27:39 +0800718 dcc->undiscard_blks += len;
Chao Yu004b6862017-04-14 23:24:55 +0800719
720 return dc;
Jaegeuk Kim15469962017-01-09 20:32:07 -0800721}
722
Chao Yu004b6862017-04-14 23:24:55 +0800723static struct discard_cmd *__attach_discard_cmd(struct f2fs_sb_info *sbi,
724 struct block_device *bdev, block_t lstart,
725 block_t start, block_t len,
726 struct rb_node *parent, struct rb_node **p)
727{
728 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
729 struct discard_cmd *dc;
730
731 dc = __create_discard_cmd(sbi, bdev, lstart, start, len);
732
733 rb_link_node(&dc->rb_node, parent, p);
734 rb_insert_color(&dc->rb_node, &dcc->root);
735
736 return dc;
737}
738
739static void __detach_discard_cmd(struct discard_cmd_control *dcc,
740 struct discard_cmd *dc)
Jaegeuk Kim15469962017-01-09 20:32:07 -0800741{
Jaegeuk Kimdcc91652017-01-11 10:20:04 -0800742 if (dc->state == D_DONE)
Chao Yu004b6862017-04-14 23:24:55 +0800743 atomic_dec(&dcc->issing_discard);
744
745 list_del(&dc->list);
746 rb_erase(&dc->rb_node, &dcc->root);
Chao Yud84d1cb2017-04-18 19:27:39 +0800747 dcc->undiscard_blks -= dc->len;
Chao Yu004b6862017-04-14 23:24:55 +0800748
749 kmem_cache_free(discard_cmd_slab, dc);
750
751 atomic_dec(&dcc->discard_cmd_cnt);
752}
753
754static void __remove_discard_cmd(struct f2fs_sb_info *sbi,
755 struct discard_cmd *dc)
756{
757 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Jaegeuk Kimdcc91652017-01-11 10:20:04 -0800758
Chao Yud9703d92017-06-05 18:29:07 +0800759 f2fs_bug_on(sbi, dc->ref);
760
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800761 if (dc->error == -EOPNOTSUPP)
762 dc->error = 0;
Jaegeuk Kim15469962017-01-09 20:32:07 -0800763
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800764 if (dc->error)
Jaegeuk Kim15469962017-01-09 20:32:07 -0800765 f2fs_msg(sbi->sb, KERN_INFO,
Chao Yu04dfc232017-05-19 23:46:43 +0800766 "Issue discard(%u, %u, %u) failed, ret: %d",
767 dc->lstart, dc->start, dc->len, dc->error);
Chao Yu004b6862017-04-14 23:24:55 +0800768 __detach_discard_cmd(dcc, dc);
Chao Yu275b66b2016-08-29 23:58:34 +0800769}
770
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800771static void f2fs_submit_discard_endio(struct bio *bio)
772{
773 struct discard_cmd *dc = (struct discard_cmd *)bio->bi_private;
774
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200775 dc->error = blk_status_to_errno(bio->bi_status);
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800776 dc->state = D_DONE;
Chao Yue31b9822017-05-19 23:46:44 +0800777 complete_all(&dc->wait);
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800778 bio_put(bio);
779}
780
Chao Yu6915ea92017-06-30 17:19:02 +0800781void __check_sit_bitmap(struct f2fs_sb_info *sbi,
782 block_t start, block_t end)
783{
784#ifdef CONFIG_F2FS_CHECK_FS
785 struct seg_entry *sentry;
786 unsigned int segno;
787 block_t blk = start;
788 unsigned long offset, size, max_blocks = sbi->blocks_per_seg;
789 unsigned long *map;
790
791 while (blk < end) {
792 segno = GET_SEGNO(sbi, blk);
793 sentry = get_seg_entry(sbi, segno);
794 offset = GET_BLKOFF_FROM_SEG0(sbi, blk);
795
Yunlong Song008396e2017-08-04 17:07:15 +0800796 if (end < START_BLOCK(sbi, segno + 1))
797 size = GET_BLKOFF_FROM_SEG0(sbi, end);
798 else
799 size = max_blocks;
Chao Yu6915ea92017-06-30 17:19:02 +0800800 map = (unsigned long *)(sentry->cur_valid_map);
801 offset = __find_rev_next_bit(map, size, offset);
802 f2fs_bug_on(sbi, offset != size);
Yunlong Song008396e2017-08-04 17:07:15 +0800803 blk = START_BLOCK(sbi, segno + 1);
Chao Yu6915ea92017-06-30 17:19:02 +0800804 }
805#endif
806}
807
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800808/* this function is copied from blkdev_issue_discard from block/blk-lib.c */
809static void __submit_discard_cmd(struct f2fs_sb_info *sbi,
810 struct discard_cmd *dc)
811{
812 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
813 struct bio *bio = NULL;
814
815 if (dc->state != D_PREP)
816 return;
817
Chao Yu0243a5f2017-04-15 14:09:38 +0800818 trace_f2fs_issue_discard(dc->bdev, dc->start, dc->len);
819
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800820 dc->error = __blkdev_issue_discard(dc->bdev,
821 SECTOR_FROM_BLOCK(dc->start),
822 SECTOR_FROM_BLOCK(dc->len),
823 GFP_NOFS, 0, &bio);
824 if (!dc->error) {
825 /* should keep before submission to avoid D_DONE right away */
826 dc->state = D_SUBMIT;
Chao Yu8b8dd652017-03-25 17:19:58 +0800827 atomic_inc(&dcc->issued_discard);
828 atomic_inc(&dcc->issing_discard);
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800829 if (bio) {
830 bio->bi_private = dc;
831 bio->bi_end_io = f2fs_submit_discard_endio;
832 bio->bi_opf |= REQ_SYNC;
833 submit_bio(bio);
Chao Yu46f84c22017-04-15 14:09:36 +0800834 list_move_tail(&dc->list, &dcc->wait_list);
Chao Yu6915ea92017-06-30 17:19:02 +0800835 __check_sit_bitmap(sbi, dc->start, dc->start + dc->len);
Chao Yub0af6d42017-08-02 23:21:48 +0800836
837 f2fs_update_iostat(sbi, FS_DISCARD, 1);
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800838 }
839 } else {
840 __remove_discard_cmd(sbi, dc);
841 }
842}
843
Chao Yu004b6862017-04-14 23:24:55 +0800844static struct discard_cmd *__insert_discard_tree(struct f2fs_sb_info *sbi,
845 struct block_device *bdev, block_t lstart,
846 block_t start, block_t len,
847 struct rb_node **insert_p,
848 struct rb_node *insert_parent)
849{
850 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
851 struct rb_node **p = &dcc->root.rb_node;
852 struct rb_node *parent = NULL;
853 struct discard_cmd *dc = NULL;
854
855 if (insert_p && insert_parent) {
856 parent = insert_parent;
857 p = insert_p;
858 goto do_insert;
859 }
860
861 p = __lookup_rb_tree_for_insert(sbi, &dcc->root, &parent, lstart);
862do_insert:
863 dc = __attach_discard_cmd(sbi, bdev, lstart, start, len, parent, p);
864 if (!dc)
865 return NULL;
866
867 return dc;
868}
869
Chao Yuba48a332017-04-15 14:09:37 +0800870static void __relocate_discard_cmd(struct discard_cmd_control *dcc,
871 struct discard_cmd *dc)
872{
873 list_move_tail(&dc->list, &dcc->pend_list[plist_idx(dc->len)]);
874}
875
Chao Yu004b6862017-04-14 23:24:55 +0800876static void __punch_discard_cmd(struct f2fs_sb_info *sbi,
877 struct discard_cmd *dc, block_t blkaddr)
878{
Chao Yuba48a332017-04-15 14:09:37 +0800879 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Chao Yu004b6862017-04-14 23:24:55 +0800880 struct discard_info di = dc->di;
881 bool modified = false;
882
883 if (dc->state == D_DONE || dc->len == 1) {
884 __remove_discard_cmd(sbi, dc);
885 return;
886 }
887
Chao Yud84d1cb2017-04-18 19:27:39 +0800888 dcc->undiscard_blks -= di.len;
889
Chao Yu004b6862017-04-14 23:24:55 +0800890 if (blkaddr > di.lstart) {
891 dc->len = blkaddr - dc->lstart;
Chao Yud84d1cb2017-04-18 19:27:39 +0800892 dcc->undiscard_blks += dc->len;
Chao Yuba48a332017-04-15 14:09:37 +0800893 __relocate_discard_cmd(dcc, dc);
Chao Yu004b6862017-04-14 23:24:55 +0800894 modified = true;
895 }
896
897 if (blkaddr < di.lstart + di.len - 1) {
898 if (modified) {
899 __insert_discard_tree(sbi, dc->bdev, blkaddr + 1,
900 di.start + blkaddr + 1 - di.lstart,
901 di.lstart + di.len - 1 - blkaddr,
902 NULL, NULL);
903 } else {
904 dc->lstart++;
905 dc->len--;
906 dc->start++;
Chao Yud84d1cb2017-04-18 19:27:39 +0800907 dcc->undiscard_blks += dc->len;
Chao Yuba48a332017-04-15 14:09:37 +0800908 __relocate_discard_cmd(dcc, dc);
Chao Yu004b6862017-04-14 23:24:55 +0800909 }
910 }
911}
912
913static void __update_discard_tree_range(struct f2fs_sb_info *sbi,
914 struct block_device *bdev, block_t lstart,
915 block_t start, block_t len)
916{
917 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
918 struct discard_cmd *prev_dc = NULL, *next_dc = NULL;
919 struct discard_cmd *dc;
920 struct discard_info di = {0};
921 struct rb_node **insert_p = NULL, *insert_parent = NULL;
922 block_t end = lstart + len;
923
924 mutex_lock(&dcc->cmd_lock);
925
926 dc = (struct discard_cmd *)__lookup_rb_tree_ret(&dcc->root,
927 NULL, lstart,
928 (struct rb_entry **)&prev_dc,
929 (struct rb_entry **)&next_dc,
930 &insert_p, &insert_parent, true);
931 if (dc)
932 prev_dc = dc;
933
934 if (!prev_dc) {
935 di.lstart = lstart;
936 di.len = next_dc ? next_dc->lstart - lstart : len;
937 di.len = min(di.len, len);
938 di.start = start;
939 }
940
941 while (1) {
942 struct rb_node *node;
943 bool merged = false;
944 struct discard_cmd *tdc = NULL;
945
946 if (prev_dc) {
947 di.lstart = prev_dc->lstart + prev_dc->len;
948 if (di.lstart < lstart)
949 di.lstart = lstart;
950 if (di.lstart >= end)
951 break;
952
953 if (!next_dc || next_dc->lstart > end)
954 di.len = end - di.lstart;
955 else
956 di.len = next_dc->lstart - di.lstart;
957 di.start = start + di.lstart - lstart;
958 }
959
960 if (!di.len)
961 goto next;
962
963 if (prev_dc && prev_dc->state == D_PREP &&
964 prev_dc->bdev == bdev &&
965 __is_discard_back_mergeable(&di, &prev_dc->di)) {
966 prev_dc->di.len += di.len;
Chao Yud84d1cb2017-04-18 19:27:39 +0800967 dcc->undiscard_blks += di.len;
Chao Yuba48a332017-04-15 14:09:37 +0800968 __relocate_discard_cmd(dcc, prev_dc);
Chao Yu004b6862017-04-14 23:24:55 +0800969 di = prev_dc->di;
970 tdc = prev_dc;
971 merged = true;
972 }
973
974 if (next_dc && next_dc->state == D_PREP &&
975 next_dc->bdev == bdev &&
976 __is_discard_front_mergeable(&di, &next_dc->di)) {
977 next_dc->di.lstart = di.lstart;
978 next_dc->di.len += di.len;
979 next_dc->di.start = di.start;
Chao Yud84d1cb2017-04-18 19:27:39 +0800980 dcc->undiscard_blks += di.len;
Chao Yuba48a332017-04-15 14:09:37 +0800981 __relocate_discard_cmd(dcc, next_dc);
Chao Yu004b6862017-04-14 23:24:55 +0800982 if (tdc)
983 __remove_discard_cmd(sbi, tdc);
Chao Yu004b6862017-04-14 23:24:55 +0800984 merged = true;
985 }
986
Chao Yudf0f6b42017-04-17 18:21:43 +0800987 if (!merged) {
Chao Yu004b6862017-04-14 23:24:55 +0800988 __insert_discard_tree(sbi, bdev, di.lstart, di.start,
989 di.len, NULL, NULL);
Chao Yudf0f6b42017-04-17 18:21:43 +0800990 }
Chao Yu004b6862017-04-14 23:24:55 +0800991 next:
992 prev_dc = next_dc;
993 if (!prev_dc)
994 break;
995
996 node = rb_next(&prev_dc->rb_node);
997 next_dc = rb_entry_safe(node, struct discard_cmd, rb_node);
998 }
999
1000 mutex_unlock(&dcc->cmd_lock);
1001}
1002
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001003static int __queue_discard_cmd(struct f2fs_sb_info *sbi,
1004 struct block_device *bdev, block_t blkstart, block_t blklen)
1005{
1006 block_t lblkstart = blkstart;
1007
Chao Yu0243a5f2017-04-15 14:09:38 +08001008 trace_f2fs_queue_discard(bdev, blkstart, blklen);
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001009
1010 if (sbi->s_ndevs) {
1011 int devi = f2fs_target_device_index(sbi, blkstart);
1012
1013 blkstart -= FDEV(devi).start_blk;
1014 }
Chao Yu004b6862017-04-14 23:24:55 +08001015 __update_discard_tree_range(sbi, bdev, lblkstart, blkstart, blklen);
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001016 return 0;
1017}
1018
Chao Yu969d1b12017-08-07 23:09:56 +08001019static int __issue_discard_cmd(struct f2fs_sb_info *sbi, bool issue_cond)
Chao Yubd5b0732017-04-25 20:21:37 +08001020{
1021 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1022 struct list_head *pend_list;
1023 struct discard_cmd *dc, *tmp;
1024 struct blk_plug plug;
Chao Yu969d1b12017-08-07 23:09:56 +08001025 int iter = 0, issued = 0;
1026 int i;
Chao Yubd5b0732017-04-25 20:21:37 +08001027
1028 mutex_lock(&dcc->cmd_lock);
Yunlei He963932a2017-05-19 14:42:12 +08001029 f2fs_bug_on(sbi,
1030 !__check_rb_tree_consistence(sbi, &dcc->root));
Chao Yubd5b0732017-04-25 20:21:37 +08001031 blk_start_plug(&plug);
Chao Yu969d1b12017-08-07 23:09:56 +08001032 for (i = MAX_PLIST_NUM - 1;
1033 i >= 0 && plist_issue(dcc->pend_list_tag[i]); i--) {
Chao Yubd5b0732017-04-25 20:21:37 +08001034 pend_list = &dcc->pend_list[i];
1035 list_for_each_entry_safe(dc, tmp, pend_list, list) {
1036 f2fs_bug_on(sbi, dc->state != D_PREP);
1037
Chao Yu969d1b12017-08-07 23:09:56 +08001038 /* Hurry up to finish fstrim */
1039 if (dcc->pend_list_tag[i] & P_TRIM) {
Chao Yubd5b0732017-04-25 20:21:37 +08001040 __submit_discard_cmd(sbi, dc);
Chao Yu969d1b12017-08-07 23:09:56 +08001041 issued++;
1042 continue;
1043 }
1044
1045 if (!issue_cond || is_idle(sbi)) {
1046 issued++;
1047 __submit_discard_cmd(sbi, dc);
1048 }
Chao Yubd5b0732017-04-25 20:21:37 +08001049 if (issue_cond && iter++ > DISCARD_ISSUE_RATE)
1050 goto out;
1051 }
Chao Yu969d1b12017-08-07 23:09:56 +08001052 if (list_empty(pend_list) && dcc->pend_list_tag[i] & P_TRIM)
1053 dcc->pend_list_tag[i] &= (~P_TRIM);
Chao Yubd5b0732017-04-25 20:21:37 +08001054 }
1055out:
1056 blk_finish_plug(&plug);
1057 mutex_unlock(&dcc->cmd_lock);
Chao Yu969d1b12017-08-07 23:09:56 +08001058
1059 return issued;
1060}
1061
1062static void __drop_discard_cmd(struct f2fs_sb_info *sbi)
1063{
1064 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1065 struct list_head *pend_list;
1066 struct discard_cmd *dc, *tmp;
1067 int i;
1068
1069 mutex_lock(&dcc->cmd_lock);
1070 for (i = MAX_PLIST_NUM - 1; i >= 0; i--) {
1071 pend_list = &dcc->pend_list[i];
1072 list_for_each_entry_safe(dc, tmp, pend_list, list) {
1073 f2fs_bug_on(sbi, dc->state != D_PREP);
1074 __remove_discard_cmd(sbi, dc);
1075 }
1076 }
1077 mutex_unlock(&dcc->cmd_lock);
Chao Yubd5b0732017-04-25 20:21:37 +08001078}
1079
Chao Yu2a510c002017-06-05 18:29:06 +08001080static void __wait_one_discard_bio(struct f2fs_sb_info *sbi,
1081 struct discard_cmd *dc)
1082{
1083 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1084
1085 wait_for_completion_io(&dc->wait);
1086 mutex_lock(&dcc->cmd_lock);
1087 f2fs_bug_on(sbi, dc->state != D_DONE);
1088 dc->ref--;
1089 if (!dc->ref)
1090 __remove_discard_cmd(sbi, dc);
1091 mutex_unlock(&dcc->cmd_lock);
1092}
1093
Chao Yu63a94fa2017-04-25 20:21:38 +08001094static void __wait_discard_cmd(struct f2fs_sb_info *sbi, bool wait_cond)
1095{
1096 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1097 struct list_head *wait_list = &(dcc->wait_list);
1098 struct discard_cmd *dc, *tmp;
Chao Yu6afae632017-05-19 23:46:45 +08001099 bool need_wait;
1100
1101next:
1102 need_wait = false;
Chao Yu63a94fa2017-04-25 20:21:38 +08001103
1104 mutex_lock(&dcc->cmd_lock);
1105 list_for_each_entry_safe(dc, tmp, wait_list, list) {
Chao Yu6afae632017-05-19 23:46:45 +08001106 if (!wait_cond || (dc->state == D_DONE && !dc->ref)) {
Chao Yu63a94fa2017-04-25 20:21:38 +08001107 wait_for_completion_io(&dc->wait);
1108 __remove_discard_cmd(sbi, dc);
Chao Yu6afae632017-05-19 23:46:45 +08001109 } else {
1110 dc->ref++;
1111 need_wait = true;
1112 break;
Chao Yu63a94fa2017-04-25 20:21:38 +08001113 }
1114 }
1115 mutex_unlock(&dcc->cmd_lock);
Chao Yu6afae632017-05-19 23:46:45 +08001116
1117 if (need_wait) {
Chao Yu2a510c002017-06-05 18:29:06 +08001118 __wait_one_discard_bio(sbi, dc);
Chao Yu6afae632017-05-19 23:46:45 +08001119 goto next;
1120 }
Chao Yu63a94fa2017-04-25 20:21:38 +08001121}
1122
Jaegeuk Kim4e6a8d92016-12-29 14:07:53 -08001123/* This should be covered by global mutex, &sit_i->sentry_lock */
1124void f2fs_wait_discard_bio(struct f2fs_sb_info *sbi, block_t blkaddr)
Chao Yu275b66b2016-08-29 23:58:34 +08001125{
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001126 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Chao Yu004b6862017-04-14 23:24:55 +08001127 struct discard_cmd *dc;
Chao Yuec9895a2017-04-26 17:39:54 +08001128 bool need_wait = false;
Chao Yu275b66b2016-08-29 23:58:34 +08001129
Jaegeuk Kim15469962017-01-09 20:32:07 -08001130 mutex_lock(&dcc->cmd_lock);
Chao Yu004b6862017-04-14 23:24:55 +08001131 dc = (struct discard_cmd *)__lookup_rb_tree(&dcc->root, NULL, blkaddr);
1132 if (dc) {
Chao Yuec9895a2017-04-26 17:39:54 +08001133 if (dc->state == D_PREP) {
1134 __punch_discard_cmd(sbi, dc, blkaddr);
1135 } else {
1136 dc->ref++;
1137 need_wait = true;
1138 }
Chao Yu275b66b2016-08-29 23:58:34 +08001139 }
Chao Yud4314132017-04-05 18:19:49 +08001140 mutex_unlock(&dcc->cmd_lock);
Chao Yuec9895a2017-04-26 17:39:54 +08001141
Chao Yu2a510c002017-06-05 18:29:06 +08001142 if (need_wait)
1143 __wait_one_discard_bio(sbi, dc);
Chao Yud4314132017-04-05 18:19:49 +08001144}
Chao Yu22d375d2017-04-05 18:19:48 +08001145
Chao Yucce13252017-06-29 23:17:45 +08001146void stop_discard_thread(struct f2fs_sb_info *sbi)
1147{
1148 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1149
1150 if (dcc && dcc->f2fs_issue_discard) {
1151 struct task_struct *discard_thread = dcc->f2fs_issue_discard;
1152
1153 dcc->f2fs_issue_discard = NULL;
1154 kthread_stop(discard_thread);
Jaegeuk Kim15469962017-01-09 20:32:07 -08001155 }
1156}
1157
1158/* This comes from f2fs_put_super */
Chao Yubd5b0732017-04-25 20:21:37 +08001159void f2fs_wait_discard_bios(struct f2fs_sb_info *sbi)
Chao Yu63a94fa2017-04-25 20:21:38 +08001160{
Jaegeuk Kim15469962017-01-09 20:32:07 -08001161 __issue_discard_cmd(sbi, false);
Chao Yu969d1b12017-08-07 23:09:56 +08001162 __drop_discard_cmd(sbi);
Jaegeuk Kim15469962017-01-09 20:32:07 -08001163 __wait_discard_cmd(sbi, false);
1164}
1165
Chao Yu969d1b12017-08-07 23:09:56 +08001166static void mark_discard_range_all(struct f2fs_sb_info *sbi)
1167{
1168 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1169 int i;
1170
1171 mutex_lock(&dcc->cmd_lock);
1172 for (i = 0; i < MAX_PLIST_NUM; i++)
1173 dcc->pend_list_tag[i] |= P_TRIM;
1174 mutex_unlock(&dcc->cmd_lock);
1175}
1176
Jaegeuk Kim15469962017-01-09 20:32:07 -08001177static int issue_discard_thread(void *data)
1178{
1179 struct f2fs_sb_info *sbi = data;
1180 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1181 wait_queue_head_t *q = &dcc->discard_wait_queue;
Chao Yu969d1b12017-08-07 23:09:56 +08001182 unsigned int wait_ms = DEF_MIN_DISCARD_ISSUE_TIME;
1183 int issued;
Jaegeuk Kim15469962017-01-09 20:32:07 -08001184
Jaegeuk Kim1d7be272017-05-17 10:36:58 -07001185 set_freezable();
Jaegeuk Kim15469962017-01-09 20:32:07 -08001186
Jaegeuk Kim1d7be272017-05-17 10:36:58 -07001187 do {
Chao Yu969d1b12017-08-07 23:09:56 +08001188 wait_event_interruptible_timeout(*q,
1189 kthread_should_stop() || freezing(current) ||
1190 dcc->discard_wake,
1191 msecs_to_jiffies(wait_ms));
Jaegeuk Kim1d7be272017-05-17 10:36:58 -07001192 if (try_to_freeze())
1193 continue;
1194 if (kthread_should_stop())
1195 return 0;
Jaegeuk Kim15469962017-01-09 20:32:07 -08001196
Chao Yu969d1b12017-08-07 23:09:56 +08001197 if (dcc->discard_wake)
1198 dcc->discard_wake = 0;
1199
Chao Yudc6febb2017-07-22 08:52:23 +08001200 sb_start_intwrite(sbi->sb);
1201
Chao Yu969d1b12017-08-07 23:09:56 +08001202 issued = __issue_discard_cmd(sbi, true);
1203 if (issued) {
1204 __wait_discard_cmd(sbi, true);
1205 wait_ms = DEF_MIN_DISCARD_ISSUE_TIME;
1206 } else {
1207 wait_ms = DEF_MAX_DISCARD_ISSUE_TIME;
1208 }
Jaegeuk Kim1d7be272017-05-17 10:36:58 -07001209
Chao Yudc6febb2017-07-22 08:52:23 +08001210 sb_end_intwrite(sbi->sb);
1211
Jaegeuk Kim1d7be272017-05-17 10:36:58 -07001212 } while (!kthread_should_stop());
1213 return 0;
Jaegeuk Kim15469962017-01-09 20:32:07 -08001214}
1215
Damien Le Moalf46e88092016-10-28 17:45:06 +09001216#ifdef CONFIG_BLK_DEV_ZONED
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001217static int __f2fs_issue_discard_zone(struct f2fs_sb_info *sbi,
1218 struct block_device *bdev, block_t blkstart, block_t blklen)
Damien Le Moalf46e88092016-10-28 17:45:06 +09001219{
Jaegeuk Kim925922852017-02-22 20:18:35 -08001220 sector_t sector, nr_sects;
Kinglong Mee10a875f2017-03-08 09:49:53 +08001221 block_t lblkstart = blkstart;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001222 int devi = 0;
Damien Le Moalf46e88092016-10-28 17:45:06 +09001223
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001224 if (sbi->s_ndevs) {
1225 devi = f2fs_target_device_index(sbi, blkstart);
1226 blkstart -= FDEV(devi).start_blk;
1227 }
Damien Le Moalf46e88092016-10-28 17:45:06 +09001228
1229 /*
1230 * We need to know the type of the zone: for conventional zones,
1231 * use regular discard if the drive supports it. For sequential
1232 * zones, reset the zone write pointer.
1233 */
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001234 switch (get_blkz_type(sbi, bdev, blkstart)) {
Damien Le Moalf46e88092016-10-28 17:45:06 +09001235
1236 case BLK_ZONE_TYPE_CONVENTIONAL:
1237 if (!blk_queue_discard(bdev_get_queue(bdev)))
1238 return 0;
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001239 return __queue_discard_cmd(sbi, bdev, lblkstart, blklen);
Damien Le Moalf46e88092016-10-28 17:45:06 +09001240 case BLK_ZONE_TYPE_SEQWRITE_REQ:
1241 case BLK_ZONE_TYPE_SEQWRITE_PREF:
Jaegeuk Kim925922852017-02-22 20:18:35 -08001242 sector = SECTOR_FROM_BLOCK(blkstart);
1243 nr_sects = SECTOR_FROM_BLOCK(blklen);
1244
1245 if (sector & (bdev_zone_sectors(bdev) - 1) ||
1246 nr_sects != bdev_zone_sectors(bdev)) {
1247 f2fs_msg(sbi->sb, KERN_INFO,
1248 "(%d) %s: Unaligned discard attempted (block %x + %x)",
1249 devi, sbi->s_ndevs ? FDEV(devi).path: "",
1250 blkstart, blklen);
1251 return -EIO;
1252 }
Jaegeuk Kimd50aaee2017-02-15 11:14:06 -08001253 trace_f2fs_issue_reset_zone(bdev, blkstart);
Damien Le Moalf46e88092016-10-28 17:45:06 +09001254 return blkdev_reset_zones(bdev, sector,
1255 nr_sects, GFP_NOFS);
1256 default:
1257 /* Unknown zone type: broken device ? */
1258 return -EIO;
1259 }
1260}
1261#endif
1262
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001263static int __issue_discard_async(struct f2fs_sb_info *sbi,
1264 struct block_device *bdev, block_t blkstart, block_t blklen)
1265{
1266#ifdef CONFIG_BLK_DEV_ZONED
1267 if (f2fs_sb_mounted_blkzoned(sbi->sb) &&
1268 bdev_zoned_model(bdev) != BLK_ZONED_NONE)
1269 return __f2fs_issue_discard_zone(sbi, bdev, blkstart, blklen);
1270#endif
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001271 return __queue_discard_cmd(sbi, bdev, blkstart, blklen);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001272}
1273
Jaegeuk Kim1e87a782014-04-15 13:57:55 +09001274static int f2fs_issue_discard(struct f2fs_sb_info *sbi,
Jaegeuk Kim37208872013-11-12 16:55:17 +09001275 block_t blkstart, block_t blklen)
1276{
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001277 sector_t start = blkstart, len = 0;
1278 struct block_device *bdev;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001279 struct seg_entry *se;
1280 unsigned int offset;
1281 block_t i;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001282 int err = 0;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001283
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001284 bdev = f2fs_target_device(sbi, blkstart, NULL);
1285
1286 for (i = blkstart; i < blkstart + blklen; i++, len++) {
1287 if (i != start) {
1288 struct block_device *bdev2 =
1289 f2fs_target_device(sbi, i, NULL);
1290
1291 if (bdev2 != bdev) {
1292 err = __issue_discard_async(sbi, bdev,
1293 start, len);
1294 if (err)
1295 return err;
1296 bdev = bdev2;
1297 start = i;
1298 len = 0;
1299 }
1300 }
1301
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001302 se = get_seg_entry(sbi, GET_SEGNO(sbi, i));
1303 offset = GET_BLKOFF_FROM_SEG0(sbi, i);
1304
1305 if (!f2fs_test_and_set_bit(offset, se->discard_map))
1306 sbi->discard_blks--;
1307 }
Damien Le Moalf46e88092016-10-28 17:45:06 +09001308
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001309 if (len)
1310 err = __issue_discard_async(sbi, bdev, start, len);
1311 return err;
Jaegeuk Kim1e87a782014-04-15 13:57:55 +09001312}
1313
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08001314static bool add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc,
1315 bool check_only)
Jaegeuk Kimadf49832014-10-28 22:27:59 -07001316{
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001317 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
1318 int max_blocks = sbi->blocks_per_seg;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001319 struct seg_entry *se = get_seg_entry(sbi, cpc->trim_start);
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001320 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
1321 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001322 unsigned long *discard_map = (unsigned long *)se->discard_map;
Jaegeuk Kim60a3b782015-02-10 16:44:29 -08001323 unsigned long *dmap = SIT_I(sbi)->tmp_map;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001324 unsigned int start = 0, end = -1;
Chao Yuc473f1a2017-04-27 20:40:39 +08001325 bool force = (cpc->reason & CP_DISCARD);
Chao Yua7eeb8232017-03-28 18:18:50 +08001326 struct discard_entry *de = NULL;
Chao Yu46f84c22017-04-15 14:09:36 +08001327 struct list_head *head = &SM_I(sbi)->dcc_info->entry_list;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001328 int i;
1329
Jaegeuk Kim3e025742016-08-02 10:56:40 -07001330 if (se->valid_blocks == max_blocks || !f2fs_discard_en(sbi))
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08001331 return false;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001332
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001333 if (!force) {
1334 if (!test_opt(sbi, DISCARD) || !se->valid_blocks ||
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001335 SM_I(sbi)->dcc_info->nr_discards >=
1336 SM_I(sbi)->dcc_info->max_discards)
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08001337 return false;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001338 }
1339
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001340 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
1341 for (i = 0; i < entries; i++)
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001342 dmap[i] = force ? ~ckpt_map[i] & ~discard_map[i] :
Jaegeuk Kimd7bc2482014-12-12 13:53:41 -08001343 (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001344
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001345 while (force || SM_I(sbi)->dcc_info->nr_discards <=
1346 SM_I(sbi)->dcc_info->max_discards) {
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001347 start = __find_rev_next_bit(dmap, max_blocks, end + 1);
1348 if (start >= max_blocks)
1349 break;
1350
1351 end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
Yunlei Hec7b41e12016-07-07 12:13:33 +08001352 if (force && start && end != max_blocks
1353 && (end - start) < cpc->trim_minlen)
1354 continue;
1355
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08001356 if (check_only)
1357 return true;
1358
Chao Yua7eeb8232017-03-28 18:18:50 +08001359 if (!de) {
1360 de = f2fs_kmem_cache_alloc(discard_entry_slab,
1361 GFP_F2FS_ZERO);
1362 de->start_blkaddr = START_BLOCK(sbi, cpc->trim_start);
1363 list_add_tail(&de->list, head);
1364 }
1365
1366 for (i = start; i < end; i++)
1367 __set_bit_le(i, (void *)de->discard_map);
1368
1369 SM_I(sbi)->dcc_info->nr_discards += end - start;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001370 }
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08001371 return false;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001372}
1373
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001374void release_discard_addrs(struct f2fs_sb_info *sbi)
1375{
Chao Yu46f84c22017-04-15 14:09:36 +08001376 struct list_head *head = &(SM_I(sbi)->dcc_info->entry_list);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001377 struct discard_entry *entry, *this;
1378
1379 /* drop caches */
1380 list_for_each_entry_safe(entry, this, head, list) {
1381 list_del(&entry->list);
1382 kmem_cache_free(discard_entry_slab, entry);
1383 }
1384}
1385
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001386/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001387 * Should call clear_prefree_segments after checkpoint is done.
1388 */
1389static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
1390{
1391 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Chao Yub65ee142014-08-04 10:10:07 +08001392 unsigned int segno;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001393
1394 mutex_lock(&dirty_i->seglist_lock);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001395 for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001396 __set_test_and_free(sbi, segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001397 mutex_unlock(&dirty_i->seglist_lock);
1398}
1399
Jaegeuk Kim836b5a62015-04-30 22:50:06 -07001400void clear_prefree_segments(struct f2fs_sb_info *sbi, struct cp_control *cpc)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001401{
Chao Yu969d1b12017-08-07 23:09:56 +08001402 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1403 struct list_head *head = &dcc->entry_list;
Chao Yu2d7b8222014-03-29 11:33:17 +08001404 struct discard_entry *entry, *this;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001405 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Changman Lee29e59c12013-11-11 09:24:37 +09001406 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
Changman Lee29e59c12013-11-11 09:24:37 +09001407 unsigned int start = 0, end = -1;
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001408 unsigned int secno, start_segno;
Chao Yuc473f1a2017-04-27 20:40:39 +08001409 bool force = (cpc->reason & CP_DISCARD);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001410
1411 mutex_lock(&dirty_i->seglist_lock);
Changman Lee29e59c12013-11-11 09:24:37 +09001412
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001413 while (1) {
Changman Lee29e59c12013-11-11 09:24:37 +09001414 int i;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001415 start = find_next_bit(prefree_map, MAIN_SEGS(sbi), end + 1);
1416 if (start >= MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001417 break;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001418 end = find_next_zero_bit(prefree_map, MAIN_SEGS(sbi),
1419 start + 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001420
Changman Lee29e59c12013-11-11 09:24:37 +09001421 for (i = start; i < end; i++)
1422 clear_bit(i, prefree_map);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001423
Changman Lee29e59c12013-11-11 09:24:37 +09001424 dirty_i->nr_dirty[PRE] -= end - start;
1425
Yunlei He650d3c42016-12-22 11:46:24 +08001426 if (!test_opt(sbi, DISCARD))
Changman Lee29e59c12013-11-11 09:24:37 +09001427 continue;
1428
Yunlei He650d3c42016-12-22 11:46:24 +08001429 if (force && start >= cpc->trim_start &&
1430 (end - 1) <= cpc->trim_end)
1431 continue;
1432
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001433 if (!test_opt(sbi, LFS) || sbi->segs_per_sec == 1) {
1434 f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
Jaegeuk Kim37208872013-11-12 16:55:17 +09001435 (end - start) << sbi->log_blocks_per_seg);
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001436 continue;
1437 }
1438next:
Jaegeuk Kim4ddb1a42017-04-07 15:08:17 -07001439 secno = GET_SEC_FROM_SEG(sbi, start);
1440 start_segno = GET_SEG_FROM_SEC(sbi, secno);
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001441 if (!IS_CURSEC(sbi, secno) &&
Jaegeuk Kim302bd342017-04-07 14:33:22 -07001442 !get_valid_blocks(sbi, start, true))
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001443 f2fs_issue_discard(sbi, START_BLOCK(sbi, start_segno),
1444 sbi->segs_per_sec << sbi->log_blocks_per_seg);
1445
1446 start = start_segno + sbi->segs_per_sec;
1447 if (start < end)
1448 goto next;
Jaegeuk Kim8b107f52017-02-27 11:57:11 -08001449 else
1450 end = start - 1;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001451 }
1452 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001453
1454 /* send small discards */
Chao Yu2d7b8222014-03-29 11:33:17 +08001455 list_for_each_entry_safe(entry, this, head, list) {
Chao Yua7eeb8232017-03-28 18:18:50 +08001456 unsigned int cur_pos = 0, next_pos, len, total_len = 0;
1457 bool is_valid = test_bit_le(0, entry->discard_map);
1458
1459find_next:
1460 if (is_valid) {
1461 next_pos = find_next_zero_bit_le(entry->discard_map,
1462 sbi->blocks_per_seg, cur_pos);
1463 len = next_pos - cur_pos;
1464
Damien Le Moalacfd28102017-05-26 17:04:40 +09001465 if (f2fs_sb_mounted_blkzoned(sbi->sb) ||
1466 (force && len < cpc->trim_minlen))
Chao Yua7eeb8232017-03-28 18:18:50 +08001467 goto skip;
1468
1469 f2fs_issue_discard(sbi, entry->start_blkaddr + cur_pos,
1470 len);
1471 cpc->trimmed += len;
1472 total_len += len;
1473 } else {
1474 next_pos = find_next_bit_le(entry->discard_map,
1475 sbi->blocks_per_seg, cur_pos);
1476 }
Jaegeuk Kim836b5a62015-04-30 22:50:06 -07001477skip:
Chao Yua7eeb8232017-03-28 18:18:50 +08001478 cur_pos = next_pos;
1479 is_valid = !is_valid;
1480
1481 if (cur_pos < sbi->blocks_per_seg)
1482 goto find_next;
1483
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001484 list_del(&entry->list);
Chao Yu969d1b12017-08-07 23:09:56 +08001485 dcc->nr_discards -= total_len;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001486 kmem_cache_free(discard_entry_slab, entry);
1487 }
Chao Yu34e159d2017-04-25 00:21:34 +08001488
Chao Yu969d1b12017-08-07 23:09:56 +08001489 dcc->discard_wake = 1;
1490 wake_up_interruptible_all(&dcc->discard_wait_queue);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001491}
1492
Jaegeuk Kim8ed59742017-01-29 14:27:02 +09001493static int create_discard_cmd_control(struct f2fs_sb_info *sbi)
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001494{
Jaegeuk Kim15469962017-01-09 20:32:07 -08001495 dev_t dev = sbi->sb->s_bdev->bd_dev;
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001496 struct discard_cmd_control *dcc;
Chao Yuba48a332017-04-15 14:09:37 +08001497 int err = 0, i;
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001498
1499 if (SM_I(sbi)->dcc_info) {
1500 dcc = SM_I(sbi)->dcc_info;
1501 goto init_thread;
1502 }
1503
1504 dcc = kzalloc(sizeof(struct discard_cmd_control), GFP_KERNEL);
1505 if (!dcc)
1506 return -ENOMEM;
1507
Chao Yu969d1b12017-08-07 23:09:56 +08001508 dcc->discard_granularity = DEFAULT_DISCARD_GRANULARITY;
Chao Yu46f84c22017-04-15 14:09:36 +08001509 INIT_LIST_HEAD(&dcc->entry_list);
Chao Yu969d1b12017-08-07 23:09:56 +08001510 for (i = 0; i < MAX_PLIST_NUM; i++) {
Chao Yuba48a332017-04-15 14:09:37 +08001511 INIT_LIST_HEAD(&dcc->pend_list[i]);
Chao Yu969d1b12017-08-07 23:09:56 +08001512 if (i >= dcc->discard_granularity - 1)
1513 dcc->pend_list_tag[i] |= P_ACTIVE;
1514 }
Chao Yu46f84c22017-04-15 14:09:36 +08001515 INIT_LIST_HEAD(&dcc->wait_list);
Jaegeuk Kim15469962017-01-09 20:32:07 -08001516 mutex_init(&dcc->cmd_lock);
Chao Yu8b8dd652017-03-25 17:19:58 +08001517 atomic_set(&dcc->issued_discard, 0);
1518 atomic_set(&dcc->issing_discard, 0);
Chao Yu5f323662017-03-25 17:19:59 +08001519 atomic_set(&dcc->discard_cmd_cnt, 0);
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001520 dcc->nr_discards = 0;
Chao Yud618eba2017-04-25 00:21:35 +08001521 dcc->max_discards = MAIN_SEGS(sbi) << sbi->log_blocks_per_seg;
Chao Yud84d1cb2017-04-18 19:27:39 +08001522 dcc->undiscard_blks = 0;
Chao Yu004b6862017-04-14 23:24:55 +08001523 dcc->root = RB_ROOT;
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001524
Jaegeuk Kim15469962017-01-09 20:32:07 -08001525 init_waitqueue_head(&dcc->discard_wait_queue);
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001526 SM_I(sbi)->dcc_info = dcc;
1527init_thread:
Jaegeuk Kim15469962017-01-09 20:32:07 -08001528 dcc->f2fs_issue_discard = kthread_run(issue_discard_thread, sbi,
1529 "f2fs_discard-%u:%u", MAJOR(dev), MINOR(dev));
1530 if (IS_ERR(dcc->f2fs_issue_discard)) {
1531 err = PTR_ERR(dcc->f2fs_issue_discard);
1532 kfree(dcc);
1533 SM_I(sbi)->dcc_info = NULL;
1534 return err;
1535 }
1536
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001537 return err;
1538}
1539
Chao Yuf0994052017-03-27 18:14:04 +08001540static void destroy_discard_cmd_control(struct f2fs_sb_info *sbi)
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001541{
1542 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1543
Chao Yuf0994052017-03-27 18:14:04 +08001544 if (!dcc)
1545 return;
1546
Chao Yucce13252017-06-29 23:17:45 +08001547 stop_discard_thread(sbi);
Chao Yuf0994052017-03-27 18:14:04 +08001548
1549 kfree(dcc);
1550 SM_I(sbi)->dcc_info = NULL;
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001551}
1552
Chao Yu184a5cd2014-09-04 18:13:01 +08001553static bool __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001554{
1555 struct sit_info *sit_i = SIT_I(sbi);
Chao Yu184a5cd2014-09-04 18:13:01 +08001556
1557 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001558 sit_i->dirty_sentries++;
Chao Yu184a5cd2014-09-04 18:13:01 +08001559 return false;
1560 }
1561
1562 return true;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001563}
1564
1565static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
1566 unsigned int segno, int modified)
1567{
1568 struct seg_entry *se = get_seg_entry(sbi, segno);
1569 se->type = type;
1570 if (modified)
1571 __mark_sit_entry_dirty(sbi, segno);
1572}
1573
1574static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
1575{
1576 struct seg_entry *se;
1577 unsigned int segno, offset;
1578 long int new_vblocks;
Yunlong Song6415fed2017-08-02 21:20:13 +08001579 bool exist;
1580#ifdef CONFIG_F2FS_CHECK_FS
1581 bool mir_exist;
1582#endif
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001583
1584 segno = GET_SEGNO(sbi, blkaddr);
1585
1586 se = get_seg_entry(sbi, segno);
1587 new_vblocks = se->valid_blocks + del;
Jaegeuk Kim491c0852014-02-04 13:01:10 +09001588 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001589
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001590 f2fs_bug_on(sbi, (new_vblocks >> (sizeof(unsigned short) << 3) ||
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001591 (new_vblocks > sbi->blocks_per_seg)));
1592
1593 se->valid_blocks = new_vblocks;
1594 se->mtime = get_mtime(sbi);
1595 SIT_I(sbi)->max_mtime = se->mtime;
1596
1597 /* Update valid block bitmap */
1598 if (del > 0) {
Yunlong Song6415fed2017-08-02 21:20:13 +08001599 exist = f2fs_test_and_set_bit(offset, se->cur_valid_map);
Chao Yu355e7892017-01-07 18:51:01 +08001600#ifdef CONFIG_F2FS_CHECK_FS
Yunlong Song6415fed2017-08-02 21:20:13 +08001601 mir_exist = f2fs_test_and_set_bit(offset,
1602 se->cur_valid_map_mir);
1603 if (unlikely(exist != mir_exist)) {
1604 f2fs_msg(sbi->sb, KERN_ERR, "Inconsistent error "
1605 "when setting bitmap, blk:%u, old bit:%d",
1606 blkaddr, exist);
Jaegeuk Kim05796762014-09-02 16:05:00 -07001607 f2fs_bug_on(sbi, 1);
Chao Yu355e7892017-01-07 18:51:01 +08001608 }
Yunlong Song6415fed2017-08-02 21:20:13 +08001609#endif
1610 if (unlikely(exist)) {
1611 f2fs_msg(sbi->sb, KERN_ERR,
1612 "Bitmap was wrongly set, blk:%u", blkaddr);
1613 f2fs_bug_on(sbi, 1);
Yunlong Song35ee82c2017-08-02 22:16:54 +08001614 se->valid_blocks--;
1615 del = 0;
Yunlong Song6415fed2017-08-02 21:20:13 +08001616 }
1617
Jaegeuk Kim3e025742016-08-02 10:56:40 -07001618 if (f2fs_discard_en(sbi) &&
1619 !f2fs_test_and_set_bit(offset, se->discard_map))
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001620 sbi->discard_blks--;
Jaegeuk Kim720037f2017-03-06 11:59:56 -08001621
1622 /* don't overwrite by SSR to keep node chain */
1623 if (se->type == CURSEG_WARM_NODE) {
1624 if (!f2fs_test_and_set_bit(offset, se->ckpt_valid_map))
1625 se->ckpt_valid_blocks++;
1626 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001627 } else {
Yunlong Song6415fed2017-08-02 21:20:13 +08001628 exist = f2fs_test_and_clear_bit(offset, se->cur_valid_map);
Chao Yu355e7892017-01-07 18:51:01 +08001629#ifdef CONFIG_F2FS_CHECK_FS
Yunlong Song6415fed2017-08-02 21:20:13 +08001630 mir_exist = f2fs_test_and_clear_bit(offset,
1631 se->cur_valid_map_mir);
1632 if (unlikely(exist != mir_exist)) {
1633 f2fs_msg(sbi->sb, KERN_ERR, "Inconsistent error "
1634 "when clearing bitmap, blk:%u, old bit:%d",
1635 blkaddr, exist);
Jaegeuk Kim05796762014-09-02 16:05:00 -07001636 f2fs_bug_on(sbi, 1);
Chao Yu355e7892017-01-07 18:51:01 +08001637 }
Yunlong Song6415fed2017-08-02 21:20:13 +08001638#endif
1639 if (unlikely(!exist)) {
1640 f2fs_msg(sbi->sb, KERN_ERR,
1641 "Bitmap was wrongly cleared, blk:%u", blkaddr);
1642 f2fs_bug_on(sbi, 1);
Yunlong Song35ee82c2017-08-02 22:16:54 +08001643 se->valid_blocks++;
1644 del = 0;
Yunlong Song6415fed2017-08-02 21:20:13 +08001645 }
1646
Jaegeuk Kim3e025742016-08-02 10:56:40 -07001647 if (f2fs_discard_en(sbi) &&
1648 f2fs_test_and_clear_bit(offset, se->discard_map))
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001649 sbi->discard_blks++;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001650 }
1651 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
1652 se->ckpt_valid_blocks += del;
1653
1654 __mark_sit_entry_dirty(sbi, segno);
1655
1656 /* update total number of valid blocks to be written in ckpt area */
1657 SIT_I(sbi)->written_valid_blocks += del;
1658
1659 if (sbi->segs_per_sec > 1)
1660 get_sec_entry(sbi, segno)->valid_blocks += del;
1661}
1662
Jaegeuk Kim5e443812014-01-28 12:22:14 +09001663void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001664{
Jaegeuk Kim5e443812014-01-28 12:22:14 +09001665 update_sit_entry(sbi, new, 1);
1666 if (GET_SEGNO(sbi, old) != NULL_SEGNO)
1667 update_sit_entry(sbi, old, -1);
1668
1669 locate_dirty_segment(sbi, GET_SEGNO(sbi, old));
1670 locate_dirty_segment(sbi, GET_SEGNO(sbi, new));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001671}
1672
1673void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
1674{
1675 unsigned int segno = GET_SEGNO(sbi, addr);
1676 struct sit_info *sit_i = SIT_I(sbi);
1677
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001678 f2fs_bug_on(sbi, addr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001679 if (addr == NEW_ADDR)
1680 return;
1681
1682 /* add it into sit main buffer */
1683 mutex_lock(&sit_i->sentry_lock);
1684
1685 update_sit_entry(sbi, addr, -1);
1686
1687 /* add it into dirty seglist */
1688 locate_dirty_segment(sbi, segno);
1689
1690 mutex_unlock(&sit_i->sentry_lock);
1691}
1692
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001693bool is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr)
1694{
1695 struct sit_info *sit_i = SIT_I(sbi);
1696 unsigned int segno, offset;
1697 struct seg_entry *se;
1698 bool is_cp = false;
1699
1700 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR)
1701 return true;
1702
1703 mutex_lock(&sit_i->sentry_lock);
1704
1705 segno = GET_SEGNO(sbi, blkaddr);
1706 se = get_seg_entry(sbi, segno);
1707 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
1708
1709 if (f2fs_test_bit(offset, se->ckpt_valid_map))
1710 is_cp = true;
1711
1712 mutex_unlock(&sit_i->sentry_lock);
1713
1714 return is_cp;
1715}
1716
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001717/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001718 * This function should be resided under the curseg_mutex lock
1719 */
1720static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
Haicheng Lie79efe32013-06-13 16:59:27 +08001721 struct f2fs_summary *sum)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001722{
1723 struct curseg_info *curseg = CURSEG_I(sbi, type);
1724 void *addr = curseg->sum_blk;
Haicheng Lie79efe32013-06-13 16:59:27 +08001725 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001726 memcpy(addr, sum, sizeof(struct f2fs_summary));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001727}
1728
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001729/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001730 * Calculate the number of current summary pages for writing
1731 */
Chao Yu3fa06d72014-12-09 14:21:46 +08001732int npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001733{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001734 int valid_sum_count = 0;
Fan Li9a479382013-10-29 16:21:47 +08001735 int i, sum_in_page;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001736
1737 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1738 if (sbi->ckpt->alloc_type[i] == SSR)
1739 valid_sum_count += sbi->blocks_per_seg;
Chao Yu3fa06d72014-12-09 14:21:46 +08001740 else {
1741 if (for_ra)
1742 valid_sum_count += le16_to_cpu(
1743 F2FS_CKPT(sbi)->cur_data_blkoff[i]);
1744 else
1745 valid_sum_count += curseg_blkoff(sbi, i);
1746 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001747 }
1748
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001749 sum_in_page = (PAGE_SIZE - 2 * SUM_JOURNAL_SIZE -
Fan Li9a479382013-10-29 16:21:47 +08001750 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
1751 if (valid_sum_count <= sum_in_page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001752 return 1;
Fan Li9a479382013-10-29 16:21:47 +08001753 else if ((valid_sum_count - sum_in_page) <=
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001754 (PAGE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001755 return 2;
1756 return 3;
1757}
1758
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001759/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001760 * Caller should put this summary page
1761 */
1762struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
1763{
1764 return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
1765}
1766
Chao Yu381722d2015-05-19 17:40:04 +08001767void update_meta_page(struct f2fs_sb_info *sbi, void *src, block_t blk_addr)
1768{
1769 struct page *page = grab_meta_page(sbi, blk_addr);
1770 void *dst = page_address(page);
1771
1772 if (src)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001773 memcpy(dst, src, PAGE_SIZE);
Chao Yu381722d2015-05-19 17:40:04 +08001774 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001775 memset(dst, 0, PAGE_SIZE);
Chao Yu381722d2015-05-19 17:40:04 +08001776 set_page_dirty(page);
1777 f2fs_put_page(page, 1);
1778}
1779
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001780static void write_sum_page(struct f2fs_sb_info *sbi,
1781 struct f2fs_summary_block *sum_blk, block_t blk_addr)
1782{
Chao Yu381722d2015-05-19 17:40:04 +08001783 update_meta_page(sbi, (void *)sum_blk, blk_addr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001784}
1785
Chao Yub7ad7512016-02-19 18:08:46 +08001786static void write_current_sum_page(struct f2fs_sb_info *sbi,
1787 int type, block_t blk_addr)
1788{
1789 struct curseg_info *curseg = CURSEG_I(sbi, type);
1790 struct page *page = grab_meta_page(sbi, blk_addr);
1791 struct f2fs_summary_block *src = curseg->sum_blk;
1792 struct f2fs_summary_block *dst;
1793
1794 dst = (struct f2fs_summary_block *)page_address(page);
1795
1796 mutex_lock(&curseg->curseg_mutex);
1797
1798 down_read(&curseg->journal_rwsem);
1799 memcpy(&dst->journal, curseg->journal, SUM_JOURNAL_SIZE);
1800 up_read(&curseg->journal_rwsem);
1801
1802 memcpy(dst->entries, src->entries, SUM_ENTRY_SIZE);
1803 memcpy(&dst->footer, &src->footer, SUM_FOOTER_SIZE);
1804
1805 mutex_unlock(&curseg->curseg_mutex);
1806
1807 set_page_dirty(page);
1808 f2fs_put_page(page, 1);
1809}
1810
Jaegeuk Kima7881892017-04-20 13:51:57 -07001811static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
1812{
1813 struct curseg_info *curseg = CURSEG_I(sbi, type);
1814 unsigned int segno = curseg->segno + 1;
1815 struct free_segmap_info *free_i = FREE_I(sbi);
1816
1817 if (segno < MAIN_SEGS(sbi) && segno % sbi->segs_per_sec)
1818 return !test_bit(segno, free_i->free_segmap);
1819 return 0;
1820}
1821
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001822/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001823 * Find a new segment from the free segments bitmap to right order
1824 * This function should be returned with success, otherwise BUG
1825 */
1826static void get_new_segment(struct f2fs_sb_info *sbi,
1827 unsigned int *newseg, bool new_sec, int dir)
1828{
1829 struct free_segmap_info *free_i = FREE_I(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001830 unsigned int segno, secno, zoneno;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001831 unsigned int total_zones = MAIN_SECS(sbi) / sbi->secs_per_zone;
Jaegeuk Kim4ddb1a42017-04-07 15:08:17 -07001832 unsigned int hint = GET_SEC_FROM_SEG(sbi, *newseg);
1833 unsigned int old_zoneno = GET_ZONE_FROM_SEG(sbi, *newseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001834 unsigned int left_start = hint;
1835 bool init = true;
1836 int go_left = 0;
1837 int i;
1838
Chao Yu1a118cc2015-02-11 18:20:38 +08001839 spin_lock(&free_i->segmap_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001840
1841 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
1842 segno = find_next_zero_bit(free_i->free_segmap,
Jaegeuk Kim4ddb1a42017-04-07 15:08:17 -07001843 GET_SEG_FROM_SEC(sbi, hint + 1), *newseg + 1);
1844 if (segno < GET_SEG_FROM_SEC(sbi, hint + 1))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001845 goto got_it;
1846 }
1847find_other_zone:
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001848 secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint);
1849 if (secno >= MAIN_SECS(sbi)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001850 if (dir == ALLOC_RIGHT) {
1851 secno = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001852 MAIN_SECS(sbi), 0);
1853 f2fs_bug_on(sbi, secno >= MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001854 } else {
1855 go_left = 1;
1856 left_start = hint - 1;
1857 }
1858 }
1859 if (go_left == 0)
1860 goto skip_left;
1861
1862 while (test_bit(left_start, free_i->free_secmap)) {
1863 if (left_start > 0) {
1864 left_start--;
1865 continue;
1866 }
1867 left_start = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001868 MAIN_SECS(sbi), 0);
1869 f2fs_bug_on(sbi, left_start >= MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001870 break;
1871 }
1872 secno = left_start;
1873skip_left:
1874 hint = secno;
Jaegeuk Kim4ddb1a42017-04-07 15:08:17 -07001875 segno = GET_SEG_FROM_SEC(sbi, secno);
1876 zoneno = GET_ZONE_FROM_SEC(sbi, secno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001877
1878 /* give up on finding another zone */
1879 if (!init)
1880 goto got_it;
1881 if (sbi->secs_per_zone == 1)
1882 goto got_it;
1883 if (zoneno == old_zoneno)
1884 goto got_it;
1885 if (dir == ALLOC_LEFT) {
1886 if (!go_left && zoneno + 1 >= total_zones)
1887 goto got_it;
1888 if (go_left && zoneno == 0)
1889 goto got_it;
1890 }
1891 for (i = 0; i < NR_CURSEG_TYPE; i++)
1892 if (CURSEG_I(sbi, i)->zone == zoneno)
1893 break;
1894
1895 if (i < NR_CURSEG_TYPE) {
1896 /* zone is in user, try another */
1897 if (go_left)
1898 hint = zoneno * sbi->secs_per_zone - 1;
1899 else if (zoneno + 1 >= total_zones)
1900 hint = 0;
1901 else
1902 hint = (zoneno + 1) * sbi->secs_per_zone;
1903 init = false;
1904 goto find_other_zone;
1905 }
1906got_it:
1907 /* set it as dirty segment in free segmap */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001908 f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001909 __set_inuse(sbi, segno);
1910 *newseg = segno;
Chao Yu1a118cc2015-02-11 18:20:38 +08001911 spin_unlock(&free_i->segmap_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001912}
1913
1914static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
1915{
1916 struct curseg_info *curseg = CURSEG_I(sbi, type);
1917 struct summary_footer *sum_footer;
1918
1919 curseg->segno = curseg->next_segno;
Jaegeuk Kim4ddb1a42017-04-07 15:08:17 -07001920 curseg->zone = GET_ZONE_FROM_SEG(sbi, curseg->segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001921 curseg->next_blkoff = 0;
1922 curseg->next_segno = NULL_SEGNO;
1923
1924 sum_footer = &(curseg->sum_blk->footer);
1925 memset(sum_footer, 0, sizeof(struct summary_footer));
1926 if (IS_DATASEG(type))
1927 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
1928 if (IS_NODESEG(type))
1929 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
1930 __set_sit_entry_type(sbi, type, curseg->segno, modified);
1931}
1932
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -04001933static unsigned int __get_next_segno(struct f2fs_sb_info *sbi, int type)
1934{
Jaegeuk Kima7881892017-04-20 13:51:57 -07001935 /* if segs_per_sec is large than 1, we need to keep original policy. */
1936 if (sbi->segs_per_sec != 1)
1937 return CURSEG_I(sbi, type)->segno;
1938
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -04001939 if (type == CURSEG_HOT_DATA || IS_NODESEG(type))
1940 return 0;
1941
Jaegeuk Kime066b832017-04-13 15:17:00 -07001942 if (SIT_I(sbi)->last_victim[ALLOC_NEXT])
1943 return SIT_I(sbi)->last_victim[ALLOC_NEXT];
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -04001944 return CURSEG_I(sbi, type)->segno;
1945}
1946
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001947/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001948 * Allocate a current working segment.
1949 * This function always allocates a free segment in LFS manner.
1950 */
1951static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
1952{
1953 struct curseg_info *curseg = CURSEG_I(sbi, type);
1954 unsigned int segno = curseg->segno;
1955 int dir = ALLOC_LEFT;
1956
1957 write_sum_page(sbi, curseg->sum_blk,
Haicheng Li81fb5e82013-05-14 18:20:28 +08001958 GET_SUM_BLOCK(sbi, segno));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001959 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
1960 dir = ALLOC_RIGHT;
1961
1962 if (test_opt(sbi, NOHEAP))
1963 dir = ALLOC_RIGHT;
1964
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -04001965 segno = __get_next_segno(sbi, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001966 get_new_segment(sbi, &segno, new_sec, dir);
1967 curseg->next_segno = segno;
1968 reset_curseg(sbi, type, 1);
1969 curseg->alloc_type = LFS;
1970}
1971
1972static void __next_free_blkoff(struct f2fs_sb_info *sbi,
1973 struct curseg_info *seg, block_t start)
1974{
1975 struct seg_entry *se = get_seg_entry(sbi, seg->segno);
Changman Leee81c93c2013-11-15 13:21:16 +09001976 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
Jaegeuk Kim60a3b782015-02-10 16:44:29 -08001977 unsigned long *target_map = SIT_I(sbi)->tmp_map;
Changman Leee81c93c2013-11-15 13:21:16 +09001978 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
1979 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
1980 int i, pos;
1981
1982 for (i = 0; i < entries; i++)
1983 target_map[i] = ckpt_map[i] | cur_map[i];
1984
1985 pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
1986
1987 seg->next_blkoff = pos;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001988}
1989
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001990/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001991 * If a segment is written by LFS manner, next block offset is just obtained
1992 * by increasing the current block offset. However, if a segment is written by
1993 * SSR manner, next block offset obtained by calling __next_free_blkoff
1994 */
1995static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
1996 struct curseg_info *seg)
1997{
1998 if (seg->alloc_type == SSR)
1999 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
2000 else
2001 seg->next_blkoff++;
2002}
2003
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002004/*
arter97e1c42042014-08-06 23:22:50 +09002005 * This function always allocates a used segment(from dirty seglist) by SSR
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002006 * manner, so it should recover the existing segment information of valid blocks
2007 */
2008static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
2009{
2010 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2011 struct curseg_info *curseg = CURSEG_I(sbi, type);
2012 unsigned int new_segno = curseg->next_segno;
2013 struct f2fs_summary_block *sum_node;
2014 struct page *sum_page;
2015
2016 write_sum_page(sbi, curseg->sum_blk,
2017 GET_SUM_BLOCK(sbi, curseg->segno));
2018 __set_test_and_inuse(sbi, new_segno);
2019
2020 mutex_lock(&dirty_i->seglist_lock);
2021 __remove_dirty_segment(sbi, new_segno, PRE);
2022 __remove_dirty_segment(sbi, new_segno, DIRTY);
2023 mutex_unlock(&dirty_i->seglist_lock);
2024
2025 reset_curseg(sbi, type, 1);
2026 curseg->alloc_type = SSR;
2027 __next_free_blkoff(sbi, curseg, 0);
2028
2029 if (reuse) {
2030 sum_page = get_sum_page(sbi, new_segno);
2031 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
2032 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
2033 f2fs_put_page(sum_page, 1);
2034 }
2035}
2036
Jaegeuk Kim43727522013-02-04 15:11:17 +09002037static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
2038{
2039 struct curseg_info *curseg = CURSEG_I(sbi, type);
2040 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
Jaegeuk Kime066b832017-04-13 15:17:00 -07002041 unsigned segno = NULL_SEGNO;
Chao Yud27c3d82017-02-24 18:46:00 +08002042 int i, cnt;
2043 bool reversed = false;
Jaegeuk Kimc192f7a2017-02-22 17:10:18 -08002044
2045 /* need_SSR() already forces to do this */
Jaegeuk Kime066b832017-04-13 15:17:00 -07002046 if (v_ops->get_victim(sbi, &segno, BG_GC, type, SSR)) {
2047 curseg->next_segno = segno;
Jaegeuk Kimc192f7a2017-02-22 17:10:18 -08002048 return 1;
Jaegeuk Kime066b832017-04-13 15:17:00 -07002049 }
Jaegeuk Kim43727522013-02-04 15:11:17 +09002050
Jaegeuk Kim70d625c2017-02-22 17:02:32 -08002051 /* For node segments, let's do SSR more intensively */
2052 if (IS_NODESEG(type)) {
Chao Yud27c3d82017-02-24 18:46:00 +08002053 if (type >= CURSEG_WARM_NODE) {
2054 reversed = true;
2055 i = CURSEG_COLD_NODE;
2056 } else {
2057 i = CURSEG_HOT_NODE;
2058 }
2059 cnt = NR_CURSEG_NODE_TYPE;
Jaegeuk Kim70d625c2017-02-22 17:02:32 -08002060 } else {
Chao Yud27c3d82017-02-24 18:46:00 +08002061 if (type >= CURSEG_WARM_DATA) {
2062 reversed = true;
2063 i = CURSEG_COLD_DATA;
2064 } else {
2065 i = CURSEG_HOT_DATA;
2066 }
2067 cnt = NR_CURSEG_DATA_TYPE;
Jaegeuk Kim70d625c2017-02-22 17:02:32 -08002068 }
Jaegeuk Kim43727522013-02-04 15:11:17 +09002069
Chao Yud27c3d82017-02-24 18:46:00 +08002070 for (; cnt-- > 0; reversed ? i-- : i++) {
Jaegeuk Kimc192f7a2017-02-22 17:10:18 -08002071 if (i == type)
2072 continue;
Jaegeuk Kime066b832017-04-13 15:17:00 -07002073 if (v_ops->get_victim(sbi, &segno, BG_GC, i, SSR)) {
2074 curseg->next_segno = segno;
Jaegeuk Kim43727522013-02-04 15:11:17 +09002075 return 1;
Jaegeuk Kime066b832017-04-13 15:17:00 -07002076 }
Jaegeuk Kimc192f7a2017-02-22 17:10:18 -08002077 }
Jaegeuk Kim43727522013-02-04 15:11:17 +09002078 return 0;
2079}
2080
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002081/*
2082 * flush out current segment and replace it with new segment
2083 * This function should be returned with success, otherwise BUG
2084 */
2085static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
2086 int type, bool force)
2087{
Jaegeuk Kima7881892017-04-20 13:51:57 -07002088 struct curseg_info *curseg = CURSEG_I(sbi, type);
2089
Gu Zheng7b405272013-08-19 09:41:15 +08002090 if (force)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002091 new_curseg(sbi, type, true);
Jaegeuk Kim5b6c6be2017-02-14 19:32:51 -08002092 else if (!is_set_ckpt_flags(sbi, CP_CRC_RECOVERY_FLAG) &&
2093 type == CURSEG_WARM_NODE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002094 new_curseg(sbi, type, false);
Jaegeuk Kima7881892017-04-20 13:51:57 -07002095 else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
2096 new_curseg(sbi, type, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002097 else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
2098 change_curseg(sbi, type, true);
2099 else
2100 new_curseg(sbi, type, false);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +09002101
Jaegeuk Kima7881892017-04-20 13:51:57 -07002102 stat_inc_seg_type(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002103}
2104
2105void allocate_new_segments(struct f2fs_sb_info *sbi)
2106{
Jaegeuk Kim6ae1be12016-11-11 12:31:40 -08002107 struct curseg_info *curseg;
2108 unsigned int old_segno;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002109 int i;
2110
Jaegeuk Kim6ae1be12016-11-11 12:31:40 -08002111 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
2112 curseg = CURSEG_I(sbi, i);
2113 old_segno = curseg->segno;
2114 SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
2115 locate_dirty_segment(sbi, old_segno);
2116 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002117}
2118
2119static const struct segment_allocation default_salloc_ops = {
2120 .allocate_segment = allocate_segment_by_default,
2121};
2122
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08002123bool exist_trim_candidates(struct f2fs_sb_info *sbi, struct cp_control *cpc)
2124{
2125 __u64 trim_start = cpc->trim_start;
2126 bool has_candidate = false;
2127
2128 mutex_lock(&SIT_I(sbi)->sentry_lock);
2129 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++) {
2130 if (add_discard_addrs(sbi, cpc, true)) {
2131 has_candidate = true;
2132 break;
2133 }
2134 }
2135 mutex_unlock(&SIT_I(sbi)->sentry_lock);
2136
2137 cpc->trim_start = trim_start;
2138 return has_candidate;
2139}
2140
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002141int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range)
2142{
Jaegeuk Kimf7ef9b82015-02-09 12:02:44 -08002143 __u64 start = F2FS_BYTES_TO_BLK(range->start);
2144 __u64 end = start + F2FS_BYTES_TO_BLK(range->len) - 1;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002145 unsigned int start_segno, end_segno;
2146 struct cp_control cpc;
Chao Yuc34f42e2015-12-23 17:50:30 +08002147 int err = 0;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002148
Jaegeuk Kim836b5a62015-04-30 22:50:06 -07002149 if (start >= MAX_BLKADDR(sbi) || range->len < sbi->blocksize)
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002150 return -EINVAL;
2151
Jan Kara9bd27ae2014-10-21 14:07:33 +02002152 cpc.trimmed = 0;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002153 if (end <= MAIN_BLKADDR(sbi))
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002154 goto out;
2155
Yunlei Heed214a12016-09-01 10:14:39 +08002156 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) {
2157 f2fs_msg(sbi->sb, KERN_WARNING,
2158 "Found FS corruption, run fsck to fix.");
2159 goto out;
2160 }
2161
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002162 /* start/end segment number in main_area */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002163 start_segno = (start <= MAIN_BLKADDR(sbi)) ? 0 : GET_SEGNO(sbi, start);
2164 end_segno = (end >= MAX_BLKADDR(sbi)) ? MAIN_SEGS(sbi) - 1 :
2165 GET_SEGNO(sbi, end);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002166 cpc.reason = CP_DISCARD;
Jaegeuk Kim836b5a62015-04-30 22:50:06 -07002167 cpc.trim_minlen = max_t(__u64, 1, F2FS_BYTES_TO_BLK(range->minlen));
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002168
2169 /* do checkpoint to issue discard commands safely */
Jaegeuk Kimbba681c2015-01-26 17:41:23 -08002170 for (; start_segno <= end_segno; start_segno = cpc.trim_end + 1) {
2171 cpc.trim_start = start_segno;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07002172
2173 if (sbi->discard_blks == 0)
2174 break;
2175 else if (sbi->discard_blks < BATCHED_TRIM_BLOCKS(sbi))
2176 cpc.trim_end = end_segno;
2177 else
2178 cpc.trim_end = min_t(unsigned int,
2179 rounddown(start_segno +
Jaegeuk Kimbba681c2015-01-26 17:41:23 -08002180 BATCHED_TRIM_SEGMENTS(sbi),
2181 sbi->segs_per_sec) - 1, end_segno);
2182
2183 mutex_lock(&sbi->gc_mutex);
Chao Yuc34f42e2015-12-23 17:50:30 +08002184 err = write_checkpoint(sbi, &cpc);
Jaegeuk Kimbba681c2015-01-26 17:41:23 -08002185 mutex_unlock(&sbi->gc_mutex);
Chao Yue9328352016-08-21 23:21:29 +08002186 if (err)
2187 break;
Chao Yu74fa5f32016-08-21 23:21:30 +08002188
2189 schedule();
Jaegeuk Kimbba681c2015-01-26 17:41:23 -08002190 }
Chao Yu969d1b12017-08-07 23:09:56 +08002191 /* It's time to issue all the filed discards */
2192 mark_discard_range_all(sbi);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002193out:
Jaegeuk Kimf7ef9b82015-02-09 12:02:44 -08002194 range->len = F2FS_BLK_TO_BYTES(cpc.trimmed);
Chao Yuc34f42e2015-12-23 17:50:30 +08002195 return err;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002196}
2197
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002198static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
2199{
2200 struct curseg_info *curseg = CURSEG_I(sbi, type);
2201 if (curseg->next_blkoff < sbi->blocks_per_seg)
2202 return true;
2203 return false;
2204}
2205
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002206static int __get_segment_type_2(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002207{
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002208 if (fio->type == DATA)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002209 return CURSEG_HOT_DATA;
2210 else
2211 return CURSEG_HOT_NODE;
2212}
2213
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002214static int __get_segment_type_4(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002215{
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002216 if (fio->type == DATA) {
2217 struct inode *inode = fio->page->mapping->host;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002218
2219 if (S_ISDIR(inode->i_mode))
2220 return CURSEG_HOT_DATA;
2221 else
2222 return CURSEG_COLD_DATA;
2223 } else {
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002224 if (IS_DNODE(fio->page) && is_cold_node(fio->page))
Jaegeuk Kima344b9f2014-11-05 20:05:53 -08002225 return CURSEG_WARM_NODE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002226 else
2227 return CURSEG_COLD_NODE;
2228 }
2229}
2230
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002231static int __get_segment_type_6(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002232{
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002233 if (fio->type == DATA) {
2234 struct inode *inode = fio->page->mapping->host;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002235
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002236 if (is_cold_data(fio->page) || file_is_cold(inode))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002237 return CURSEG_COLD_DATA;
Jaegeuk Kimef095d12017-03-24 20:05:13 -04002238 if (is_inode_flag_set(inode, FI_HOT_DATA))
2239 return CURSEG_HOT_DATA;
2240 return CURSEG_WARM_DATA;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002241 } else {
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002242 if (IS_DNODE(fio->page))
2243 return is_cold_node(fio->page) ? CURSEG_WARM_NODE :
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002244 CURSEG_HOT_NODE;
Jaegeuk Kimef095d12017-03-24 20:05:13 -04002245 return CURSEG_COLD_NODE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002246 }
2247}
2248
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002249static int __get_segment_type(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002250{
Jaegeuk Kima912b542017-05-10 11:18:25 -07002251 int type = 0;
2252
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002253 switch (fio->sbi->active_logs) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002254 case 2:
Jaegeuk Kima912b542017-05-10 11:18:25 -07002255 type = __get_segment_type_2(fio);
2256 break;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002257 case 4:
Jaegeuk Kima912b542017-05-10 11:18:25 -07002258 type = __get_segment_type_4(fio);
2259 break;
2260 case 6:
2261 type = __get_segment_type_6(fio);
2262 break;
2263 default:
2264 f2fs_bug_on(fio->sbi, true);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002265 }
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002266
Jaegeuk Kima912b542017-05-10 11:18:25 -07002267 if (IS_HOT(type))
2268 fio->temp = HOT;
2269 else if (IS_WARM(type))
2270 fio->temp = WARM;
2271 else
2272 fio->temp = COLD;
2273 return type;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002274}
2275
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09002276void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
2277 block_t old_blkaddr, block_t *new_blkaddr,
Chao Yufb830fc2017-05-19 23:37:01 +08002278 struct f2fs_summary *sum, int type,
2279 struct f2fs_io_info *fio, bool add_list)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002280{
2281 struct sit_info *sit_i = SIT_I(sbi);
Jaegeuk Kim6ae1be12016-11-11 12:31:40 -08002282 struct curseg_info *curseg = CURSEG_I(sbi, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002283
2284 mutex_lock(&curseg->curseg_mutex);
Jaegeuk Kim21cb1d92015-03-11 13:42:48 -04002285 mutex_lock(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002286
2287 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002288
Jaegeuk Kim4e6a8d92016-12-29 14:07:53 -08002289 f2fs_wait_discard_bio(sbi, *new_blkaddr);
2290
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002291 /*
2292 * __add_sum_entry should be resided under the curseg_mutex
2293 * because, this function updates a summary entry in the
2294 * current summary block.
2295 */
Haicheng Lie79efe32013-06-13 16:59:27 +08002296 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002297
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002298 __refresh_next_blkoff(sbi, curseg);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +09002299
2300 stat_inc_block_count(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002301
Yunlong Song3436c4b2017-02-21 16:59:26 +08002302 if (!__has_curseg_space(sbi, type))
2303 sit_i->s_ops->allocate_segment(sbi, type, false);
Jaegeuk Kimc6f82fe92017-04-04 16:45:30 -07002304 /*
2305 * SIT information should be updated after segment allocation,
2306 * since we need to keep dirty segments precisely under SSR.
2307 */
2308 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
Yunlong Song3436c4b2017-02-21 16:59:26 +08002309
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002310 mutex_unlock(&sit_i->sentry_lock);
2311
Chao Yu704956e2017-07-31 20:19:09 +08002312 if (page && IS_NODESEG(type)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002313 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
2314
Chao Yu704956e2017-07-31 20:19:09 +08002315 f2fs_inode_chksum_set(sbi, page);
2316 }
2317
Chao Yufb830fc2017-05-19 23:37:01 +08002318 if (add_list) {
2319 struct f2fs_bio_info *io;
2320
2321 INIT_LIST_HEAD(&fio->list);
2322 fio->in_list = true;
2323 io = sbi->write_io[fio->type] + fio->temp;
2324 spin_lock(&io->io_lock);
2325 list_add_tail(&fio->list, &io->io_list);
2326 spin_unlock(&io->io_lock);
2327 }
2328
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09002329 mutex_unlock(&curseg->curseg_mutex);
2330}
2331
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002332static void do_write_page(struct f2fs_summary *sum, struct f2fs_io_info *fio)
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09002333{
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002334 int type = __get_segment_type(fio);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08002335 int err;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09002336
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08002337reallocate:
Chao Yu7a9d7542016-02-22 18:36:38 +08002338 allocate_data_block(fio->sbi, fio->page, fio->old_blkaddr,
Chao Yufb830fc2017-05-19 23:37:01 +08002339 &fio->new_blkaddr, sum, type, fio, true);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09002340
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002341 /* writeout dirty page into bdev */
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07002342 err = f2fs_submit_page_write(fio);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08002343 if (err == -EAGAIN) {
2344 fio->old_blkaddr = fio->new_blkaddr;
2345 goto reallocate;
2346 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002347}
2348
Chao Yub0af6d42017-08-02 23:21:48 +08002349void write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
2350 enum iostat_type io_type)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002351{
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002352 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002353 .sbi = sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002354 .type = META,
Mike Christie04d328d2016-06-05 14:31:55 -05002355 .op = REQ_OP_WRITE,
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002356 .op_flags = REQ_SYNC | REQ_META | REQ_PRIO,
Chao Yu7a9d7542016-02-22 18:36:38 +08002357 .old_blkaddr = page->index,
2358 .new_blkaddr = page->index,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002359 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07002360 .encrypted_page = NULL,
Chao Yufb830fc2017-05-19 23:37:01 +08002361 .in_list = false,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002362 };
2363
Chao Yu2b947002015-10-12 17:04:21 +08002364 if (unlikely(page->index >= MAIN_BLKADDR(sbi)))
Mike Christie04d328d2016-06-05 14:31:55 -05002365 fio.op_flags &= ~REQ_META;
Chao Yu2b947002015-10-12 17:04:21 +08002366
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002367 set_page_writeback(page);
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07002368 f2fs_submit_page_write(&fio);
Chao Yub0af6d42017-08-02 23:21:48 +08002369
2370 f2fs_update_iostat(sbi, io_type, F2FS_BLKSIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002371}
2372
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002373void write_node_page(unsigned int nid, struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002374{
2375 struct f2fs_summary sum;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002376
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002377 set_summary(&sum, nid, 0, 0);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002378 do_write_page(&sum, fio);
Chao Yub0af6d42017-08-02 23:21:48 +08002379
2380 f2fs_update_iostat(fio->sbi, fio->io_type, F2FS_BLKSIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002381}
2382
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002383void write_data_page(struct dnode_of_data *dn, struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002384{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002385 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002386 struct f2fs_summary sum;
2387 struct node_info ni;
2388
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002389 f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002390 get_node_info(sbi, dn->nid, &ni);
2391 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002392 do_write_page(&sum, fio);
Chao Yuf28b3432016-02-24 17:16:47 +08002393 f2fs_update_data_blkaddr(dn, fio->new_blkaddr);
Chao Yub0af6d42017-08-02 23:21:48 +08002394
2395 f2fs_update_iostat(sbi, fio->io_type, F2FS_BLKSIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002396}
2397
Jaegeuk Kimd1b3e722017-03-30 21:02:46 -07002398int rewrite_data_page(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002399{
Chao Yub0af6d42017-08-02 23:21:48 +08002400 int err;
2401
Chao Yu7a9d7542016-02-22 18:36:38 +08002402 fio->new_blkaddr = fio->old_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002403 stat_inc_inplace_blocks(fio->sbi);
Chao Yub0af6d42017-08-02 23:21:48 +08002404
2405 err = f2fs_submit_page_bio(fio);
2406
2407 f2fs_update_iostat(fio->sbi, fio->io_type, F2FS_BLKSIZE);
2408
2409 return err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002410}
2411
Chao Yu4356e482016-02-23 17:52:43 +08002412void __f2fs_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
Chao Yu19f106b2015-05-06 13:08:06 +08002413 block_t old_blkaddr, block_t new_blkaddr,
Chao Yu28bc1062016-02-06 14:40:34 +08002414 bool recover_curseg, bool recover_newaddr)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002415{
2416 struct sit_info *sit_i = SIT_I(sbi);
2417 struct curseg_info *curseg;
2418 unsigned int segno, old_cursegno;
2419 struct seg_entry *se;
2420 int type;
Chao Yu19f106b2015-05-06 13:08:06 +08002421 unsigned short old_blkoff;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002422
2423 segno = GET_SEGNO(sbi, new_blkaddr);
2424 se = get_seg_entry(sbi, segno);
2425 type = se->type;
2426
Chao Yu19f106b2015-05-06 13:08:06 +08002427 if (!recover_curseg) {
2428 /* for recovery flow */
2429 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
2430 if (old_blkaddr == NULL_ADDR)
2431 type = CURSEG_COLD_DATA;
2432 else
2433 type = CURSEG_WARM_DATA;
2434 }
2435 } else {
2436 if (!IS_CURSEG(sbi, segno))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002437 type = CURSEG_WARM_DATA;
2438 }
Chao Yu19f106b2015-05-06 13:08:06 +08002439
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002440 curseg = CURSEG_I(sbi, type);
2441
2442 mutex_lock(&curseg->curseg_mutex);
2443 mutex_lock(&sit_i->sentry_lock);
2444
2445 old_cursegno = curseg->segno;
Chao Yu19f106b2015-05-06 13:08:06 +08002446 old_blkoff = curseg->next_blkoff;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002447
2448 /* change the current segment */
2449 if (segno != curseg->segno) {
2450 curseg->next_segno = segno;
2451 change_curseg(sbi, type, true);
2452 }
2453
Jaegeuk Kim491c0852014-02-04 13:01:10 +09002454 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
Haicheng Lie79efe32013-06-13 16:59:27 +08002455 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002456
Chao Yu28bc1062016-02-06 14:40:34 +08002457 if (!recover_curseg || recover_newaddr)
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07002458 update_sit_entry(sbi, new_blkaddr, 1);
2459 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
2460 update_sit_entry(sbi, old_blkaddr, -1);
2461
2462 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
2463 locate_dirty_segment(sbi, GET_SEGNO(sbi, new_blkaddr));
2464
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002465 locate_dirty_segment(sbi, old_cursegno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002466
Chao Yu19f106b2015-05-06 13:08:06 +08002467 if (recover_curseg) {
2468 if (old_cursegno != curseg->segno) {
2469 curseg->next_segno = old_cursegno;
2470 change_curseg(sbi, type, true);
2471 }
2472 curseg->next_blkoff = old_blkoff;
2473 }
2474
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002475 mutex_unlock(&sit_i->sentry_lock);
2476 mutex_unlock(&curseg->curseg_mutex);
2477}
2478
Chao Yu528e3452015-05-28 19:15:35 +08002479void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
2480 block_t old_addr, block_t new_addr,
Chao Yu28bc1062016-02-06 14:40:34 +08002481 unsigned char version, bool recover_curseg,
2482 bool recover_newaddr)
Chao Yu528e3452015-05-28 19:15:35 +08002483{
2484 struct f2fs_summary sum;
2485
2486 set_summary(&sum, dn->nid, dn->ofs_in_node, version);
2487
Chao Yu28bc1062016-02-06 14:40:34 +08002488 __f2fs_replace_block(sbi, &sum, old_addr, new_addr,
2489 recover_curseg, recover_newaddr);
Chao Yu528e3452015-05-28 19:15:35 +08002490
Chao Yuf28b3432016-02-24 17:16:47 +08002491 f2fs_update_data_blkaddr(dn, new_addr);
Chao Yu528e3452015-05-28 19:15:35 +08002492}
2493
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09002494void f2fs_wait_on_page_writeback(struct page *page,
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08002495 enum page_type type, bool ordered)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09002496{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09002497 if (PageWriteback(page)) {
Jaegeuk Kim40813632014-09-02 15:31:18 -07002498 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
2499
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07002500 f2fs_submit_merged_write_cond(sbi, page->mapping->host,
2501 0, page->index, type);
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08002502 if (ordered)
2503 wait_on_page_writeback(page);
2504 else
2505 wait_for_stable_page(page);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09002506 }
2507}
2508
Chao Yu08b39fb2015-10-08 13:27:34 +08002509void f2fs_wait_on_encrypted_page_writeback(struct f2fs_sb_info *sbi,
2510 block_t blkaddr)
2511{
2512 struct page *cpage;
2513
Yunlei He5d4c0af2016-09-18 08:16:56 +08002514 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR)
Chao Yu08b39fb2015-10-08 13:27:34 +08002515 return;
2516
Chao Yu08b39fb2015-10-08 13:27:34 +08002517 cpage = find_lock_page(META_MAPPING(sbi), blkaddr);
2518 if (cpage) {
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08002519 f2fs_wait_on_page_writeback(cpage, DATA, true);
Chao Yu08b39fb2015-10-08 13:27:34 +08002520 f2fs_put_page(cpage, 1);
2521 }
2522}
2523
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002524static int read_compacted_summaries(struct f2fs_sb_info *sbi)
2525{
2526 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
2527 struct curseg_info *seg_i;
2528 unsigned char *kaddr;
2529 struct page *page;
2530 block_t start;
2531 int i, j, offset;
2532
2533 start = start_sum_block(sbi);
2534
2535 page = get_meta_page(sbi, start++);
2536 kaddr = (unsigned char *)page_address(page);
2537
2538 /* Step 1: restore nat cache */
2539 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08002540 memcpy(seg_i->journal, kaddr, SUM_JOURNAL_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002541
2542 /* Step 2: restore sit cache */
2543 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08002544 memcpy(seg_i->journal, kaddr + SUM_JOURNAL_SIZE, SUM_JOURNAL_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002545 offset = 2 * SUM_JOURNAL_SIZE;
2546
2547 /* Step 3: restore summary entries */
2548 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
2549 unsigned short blk_off;
2550 unsigned int segno;
2551
2552 seg_i = CURSEG_I(sbi, i);
2553 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
2554 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
2555 seg_i->next_segno = segno;
2556 reset_curseg(sbi, i, 0);
2557 seg_i->alloc_type = ckpt->alloc_type[i];
2558 seg_i->next_blkoff = blk_off;
2559
2560 if (seg_i->alloc_type == SSR)
2561 blk_off = sbi->blocks_per_seg;
2562
2563 for (j = 0; j < blk_off; j++) {
2564 struct f2fs_summary *s;
2565 s = (struct f2fs_summary *)(kaddr + offset);
2566 seg_i->sum_blk->entries[j] = *s;
2567 offset += SUMMARY_SIZE;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002568 if (offset + SUMMARY_SIZE <= PAGE_SIZE -
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002569 SUM_FOOTER_SIZE)
2570 continue;
2571
2572 f2fs_put_page(page, 1);
2573 page = NULL;
2574
2575 page = get_meta_page(sbi, start++);
2576 kaddr = (unsigned char *)page_address(page);
2577 offset = 0;
2578 }
2579 }
2580 f2fs_put_page(page, 1);
2581 return 0;
2582}
2583
2584static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
2585{
2586 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
2587 struct f2fs_summary_block *sum;
2588 struct curseg_info *curseg;
2589 struct page *new;
2590 unsigned short blk_off;
2591 unsigned int segno = 0;
2592 block_t blk_addr = 0;
2593
2594 /* get segment number and block addr */
2595 if (IS_DATASEG(type)) {
2596 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
2597 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
2598 CURSEG_HOT_DATA]);
Jaegeuk Kim119ee912015-01-29 11:45:33 -08002599 if (__exist_node_summaries(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002600 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
2601 else
2602 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
2603 } else {
2604 segno = le32_to_cpu(ckpt->cur_node_segno[type -
2605 CURSEG_HOT_NODE]);
2606 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
2607 CURSEG_HOT_NODE]);
Jaegeuk Kim119ee912015-01-29 11:45:33 -08002608 if (__exist_node_summaries(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002609 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
2610 type - CURSEG_HOT_NODE);
2611 else
2612 blk_addr = GET_SUM_BLOCK(sbi, segno);
2613 }
2614
2615 new = get_meta_page(sbi, blk_addr);
2616 sum = (struct f2fs_summary_block *)page_address(new);
2617
2618 if (IS_NODESEG(type)) {
Jaegeuk Kim119ee912015-01-29 11:45:33 -08002619 if (__exist_node_summaries(sbi)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002620 struct f2fs_summary *ns = &sum->entries[0];
2621 int i;
2622 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
2623 ns->version = 0;
2624 ns->ofs_in_node = 0;
2625 }
2626 } else {
Gu Zhengd6537882014-03-07 18:43:36 +08002627 int err;
2628
2629 err = restore_node_summary(sbi, segno, sum);
2630 if (err) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002631 f2fs_put_page(new, 1);
Gu Zhengd6537882014-03-07 18:43:36 +08002632 return err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002633 }
2634 }
2635 }
2636
2637 /* set uncompleted segment to curseg */
2638 curseg = CURSEG_I(sbi, type);
2639 mutex_lock(&curseg->curseg_mutex);
Chao Yub7ad7512016-02-19 18:08:46 +08002640
2641 /* update journal info */
2642 down_write(&curseg->journal_rwsem);
2643 memcpy(curseg->journal, &sum->journal, SUM_JOURNAL_SIZE);
2644 up_write(&curseg->journal_rwsem);
2645
2646 memcpy(curseg->sum_blk->entries, sum->entries, SUM_ENTRY_SIZE);
2647 memcpy(&curseg->sum_blk->footer, &sum->footer, SUM_FOOTER_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002648 curseg->next_segno = segno;
2649 reset_curseg(sbi, type, 0);
2650 curseg->alloc_type = ckpt->alloc_type[type];
2651 curseg->next_blkoff = blk_off;
2652 mutex_unlock(&curseg->curseg_mutex);
2653 f2fs_put_page(new, 1);
2654 return 0;
2655}
2656
2657static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
2658{
Jin Qian21d3f8e2017-06-01 11:18:30 -07002659 struct f2fs_journal *sit_j = CURSEG_I(sbi, CURSEG_COLD_DATA)->journal;
2660 struct f2fs_journal *nat_j = CURSEG_I(sbi, CURSEG_HOT_DATA)->journal;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002661 int type = CURSEG_HOT_DATA;
Chao Yue4fc5fb2014-03-17 16:36:24 +08002662 int err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002663
Chao Yuaaec2b12016-09-20 11:04:18 +08002664 if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG)) {
Chao Yu3fa06d72014-12-09 14:21:46 +08002665 int npages = npages_for_summary_flush(sbi, true);
2666
2667 if (npages >= 2)
2668 ra_meta_pages(sbi, start_sum_block(sbi), npages,
Chao Yu26879fb2015-10-12 17:05:59 +08002669 META_CP, true);
Chao Yu3fa06d72014-12-09 14:21:46 +08002670
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002671 /* restore for compacted data summary */
2672 if (read_compacted_summaries(sbi))
2673 return -EINVAL;
2674 type = CURSEG_HOT_NODE;
2675 }
2676
Jaegeuk Kim119ee912015-01-29 11:45:33 -08002677 if (__exist_node_summaries(sbi))
Chao Yu3fa06d72014-12-09 14:21:46 +08002678 ra_meta_pages(sbi, sum_blk_addr(sbi, NR_CURSEG_TYPE, type),
Chao Yu26879fb2015-10-12 17:05:59 +08002679 NR_CURSEG_TYPE - type, META_CP, true);
Chao Yu3fa06d72014-12-09 14:21:46 +08002680
Chao Yue4fc5fb2014-03-17 16:36:24 +08002681 for (; type <= CURSEG_COLD_NODE; type++) {
2682 err = read_normal_summaries(sbi, type);
2683 if (err)
2684 return err;
2685 }
2686
Jin Qian21d3f8e2017-06-01 11:18:30 -07002687 /* sanity check for summary blocks */
2688 if (nats_in_cursum(nat_j) > NAT_JOURNAL_ENTRIES ||
2689 sits_in_cursum(sit_j) > SIT_JOURNAL_ENTRIES)
2690 return -EINVAL;
2691
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002692 return 0;
2693}
2694
2695static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
2696{
2697 struct page *page;
2698 unsigned char *kaddr;
2699 struct f2fs_summary *summary;
2700 struct curseg_info *seg_i;
2701 int written_size = 0;
2702 int i, j;
2703
2704 page = grab_meta_page(sbi, blkaddr++);
2705 kaddr = (unsigned char *)page_address(page);
2706
2707 /* Step 1: write nat cache */
2708 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08002709 memcpy(kaddr, seg_i->journal, SUM_JOURNAL_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002710 written_size += SUM_JOURNAL_SIZE;
2711
2712 /* Step 2: write sit cache */
2713 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08002714 memcpy(kaddr + written_size, seg_i->journal, SUM_JOURNAL_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002715 written_size += SUM_JOURNAL_SIZE;
2716
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002717 /* Step 3: write summary entries */
2718 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
2719 unsigned short blkoff;
2720 seg_i = CURSEG_I(sbi, i);
2721 if (sbi->ckpt->alloc_type[i] == SSR)
2722 blkoff = sbi->blocks_per_seg;
2723 else
2724 blkoff = curseg_blkoff(sbi, i);
2725
2726 for (j = 0; j < blkoff; j++) {
2727 if (!page) {
2728 page = grab_meta_page(sbi, blkaddr++);
2729 kaddr = (unsigned char *)page_address(page);
2730 written_size = 0;
2731 }
2732 summary = (struct f2fs_summary *)(kaddr + written_size);
2733 *summary = seg_i->sum_blk->entries[j];
2734 written_size += SUMMARY_SIZE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002735
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002736 if (written_size + SUMMARY_SIZE <= PAGE_SIZE -
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002737 SUM_FOOTER_SIZE)
2738 continue;
2739
Chao Yue8d61a72013-10-24 15:08:28 +08002740 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002741 f2fs_put_page(page, 1);
2742 page = NULL;
2743 }
2744 }
Chao Yue8d61a72013-10-24 15:08:28 +08002745 if (page) {
2746 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002747 f2fs_put_page(page, 1);
Chao Yue8d61a72013-10-24 15:08:28 +08002748 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002749}
2750
2751static void write_normal_summaries(struct f2fs_sb_info *sbi,
2752 block_t blkaddr, int type)
2753{
2754 int i, end;
2755 if (IS_DATASEG(type))
2756 end = type + NR_CURSEG_DATA_TYPE;
2757 else
2758 end = type + NR_CURSEG_NODE_TYPE;
2759
Chao Yub7ad7512016-02-19 18:08:46 +08002760 for (i = type; i < end; i++)
2761 write_current_sum_page(sbi, i, blkaddr + (i - type));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002762}
2763
2764void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
2765{
Chao Yuaaec2b12016-09-20 11:04:18 +08002766 if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002767 write_compacted_summaries(sbi, start_blk);
2768 else
2769 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
2770}
2771
2772void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
2773{
Jaegeuk Kim119ee912015-01-29 11:45:33 -08002774 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002775}
2776
Chao Yudfc08a12016-02-14 18:50:40 +08002777int lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002778 unsigned int val, int alloc)
2779{
2780 int i;
2781
2782 if (type == NAT_JOURNAL) {
Chao Yudfc08a12016-02-14 18:50:40 +08002783 for (i = 0; i < nats_in_cursum(journal); i++) {
2784 if (le32_to_cpu(nid_in_journal(journal, i)) == val)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002785 return i;
2786 }
Chao Yudfc08a12016-02-14 18:50:40 +08002787 if (alloc && __has_cursum_space(journal, 1, NAT_JOURNAL))
2788 return update_nats_in_cursum(journal, 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002789 } else if (type == SIT_JOURNAL) {
Chao Yudfc08a12016-02-14 18:50:40 +08002790 for (i = 0; i < sits_in_cursum(journal); i++)
2791 if (le32_to_cpu(segno_in_journal(journal, i)) == val)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002792 return i;
Chao Yudfc08a12016-02-14 18:50:40 +08002793 if (alloc && __has_cursum_space(journal, 1, SIT_JOURNAL))
2794 return update_sits_in_cursum(journal, 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002795 }
2796 return -1;
2797}
2798
2799static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
2800 unsigned int segno)
2801{
Gu Zheng2cc22182014-10-20 17:45:49 +08002802 return get_meta_page(sbi, current_sit_addr(sbi, segno));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002803}
2804
2805static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
2806 unsigned int start)
2807{
2808 struct sit_info *sit_i = SIT_I(sbi);
2809 struct page *src_page, *dst_page;
2810 pgoff_t src_off, dst_off;
2811 void *src_addr, *dst_addr;
2812
2813 src_off = current_sit_addr(sbi, start);
2814 dst_off = next_sit_addr(sbi, src_off);
2815
2816 /* get current sit block page without lock */
2817 src_page = get_meta_page(sbi, src_off);
2818 dst_page = grab_meta_page(sbi, dst_off);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002819 f2fs_bug_on(sbi, PageDirty(src_page));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002820
2821 src_addr = page_address(src_page);
2822 dst_addr = page_address(dst_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002823 memcpy(dst_addr, src_addr, PAGE_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002824
2825 set_page_dirty(dst_page);
2826 f2fs_put_page(src_page, 1);
2827
2828 set_to_next_sit(sit_i, start);
2829
2830 return dst_page;
2831}
2832
Chao Yu184a5cd2014-09-04 18:13:01 +08002833static struct sit_entry_set *grab_sit_entry_set(void)
2834{
2835 struct sit_entry_set *ses =
Jaegeuk Kim80c54502015-08-20 08:51:56 -07002836 f2fs_kmem_cache_alloc(sit_entry_set_slab, GFP_NOFS);
Chao Yu184a5cd2014-09-04 18:13:01 +08002837
2838 ses->entry_cnt = 0;
2839 INIT_LIST_HEAD(&ses->set_list);
2840 return ses;
2841}
2842
2843static void release_sit_entry_set(struct sit_entry_set *ses)
2844{
2845 list_del(&ses->set_list);
2846 kmem_cache_free(sit_entry_set_slab, ses);
2847}
2848
2849static void adjust_sit_entry_set(struct sit_entry_set *ses,
2850 struct list_head *head)
2851{
2852 struct sit_entry_set *next = ses;
2853
2854 if (list_is_last(&ses->set_list, head))
2855 return;
2856
2857 list_for_each_entry_continue(next, head, set_list)
2858 if (ses->entry_cnt <= next->entry_cnt)
2859 break;
2860
2861 list_move_tail(&ses->set_list, &next->set_list);
2862}
2863
2864static void add_sit_entry(unsigned int segno, struct list_head *head)
2865{
2866 struct sit_entry_set *ses;
2867 unsigned int start_segno = START_SEGNO(segno);
2868
2869 list_for_each_entry(ses, head, set_list) {
2870 if (ses->start_segno == start_segno) {
2871 ses->entry_cnt++;
2872 adjust_sit_entry_set(ses, head);
2873 return;
2874 }
2875 }
2876
2877 ses = grab_sit_entry_set();
2878
2879 ses->start_segno = start_segno;
2880 ses->entry_cnt++;
2881 list_add(&ses->set_list, head);
2882}
2883
2884static void add_sits_in_set(struct f2fs_sb_info *sbi)
2885{
2886 struct f2fs_sm_info *sm_info = SM_I(sbi);
2887 struct list_head *set_list = &sm_info->sit_entry_set;
2888 unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap;
Chao Yu184a5cd2014-09-04 18:13:01 +08002889 unsigned int segno;
2890
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002891 for_each_set_bit(segno, bitmap, MAIN_SEGS(sbi))
Chao Yu184a5cd2014-09-04 18:13:01 +08002892 add_sit_entry(segno, set_list);
2893}
2894
2895static void remove_sits_in_journal(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002896{
2897 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08002898 struct f2fs_journal *journal = curseg->journal;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002899 int i;
2900
Chao Yub7ad7512016-02-19 18:08:46 +08002901 down_write(&curseg->journal_rwsem);
Chao Yudfc08a12016-02-14 18:50:40 +08002902 for (i = 0; i < sits_in_cursum(journal); i++) {
Chao Yu184a5cd2014-09-04 18:13:01 +08002903 unsigned int segno;
2904 bool dirtied;
2905
Chao Yudfc08a12016-02-14 18:50:40 +08002906 segno = le32_to_cpu(segno_in_journal(journal, i));
Chao Yu184a5cd2014-09-04 18:13:01 +08002907 dirtied = __mark_sit_entry_dirty(sbi, segno);
2908
2909 if (!dirtied)
2910 add_sit_entry(segno, &SM_I(sbi)->sit_entry_set);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002911 }
Chao Yudfc08a12016-02-14 18:50:40 +08002912 update_sits_in_cursum(journal, -i);
Chao Yub7ad7512016-02-19 18:08:46 +08002913 up_write(&curseg->journal_rwsem);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002914}
2915
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002916/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002917 * CP calls this function, which flushes SIT entries including sit_journal,
2918 * and moves prefree segs to free segs.
2919 */
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002920void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002921{
2922 struct sit_info *sit_i = SIT_I(sbi);
2923 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
2924 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08002925 struct f2fs_journal *journal = curseg->journal;
Chao Yu184a5cd2014-09-04 18:13:01 +08002926 struct sit_entry_set *ses, *tmp;
2927 struct list_head *head = &SM_I(sbi)->sit_entry_set;
Chao Yu184a5cd2014-09-04 18:13:01 +08002928 bool to_journal = true;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002929 struct seg_entry *se;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002930
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002931 mutex_lock(&sit_i->sentry_lock);
2932
Wanpeng Li2b11a742015-02-27 16:52:50 +08002933 if (!sit_i->dirty_sentries)
2934 goto out;
2935
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002936 /*
Chao Yu184a5cd2014-09-04 18:13:01 +08002937 * add and account sit entries of dirty bitmap in sit entry
2938 * set temporarily
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002939 */
Chao Yu184a5cd2014-09-04 18:13:01 +08002940 add_sits_in_set(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002941
Chao Yu184a5cd2014-09-04 18:13:01 +08002942 /*
2943 * if there are no enough space in journal to store dirty sit
2944 * entries, remove all entries from journal and add and account
2945 * them in sit entry set.
2946 */
Chao Yudfc08a12016-02-14 18:50:40 +08002947 if (!__has_cursum_space(journal, sit_i->dirty_sentries, SIT_JOURNAL))
Chao Yu184a5cd2014-09-04 18:13:01 +08002948 remove_sits_in_journal(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002949
Chao Yu184a5cd2014-09-04 18:13:01 +08002950 /*
2951 * there are two steps to flush sit entries:
2952 * #1, flush sit entries to journal in current cold data summary block.
2953 * #2, flush sit entries to sit page.
2954 */
2955 list_for_each_entry_safe(ses, tmp, head, set_list) {
Jaegeuk Kim4a257ed2014-10-16 11:43:30 -07002956 struct page *page = NULL;
Chao Yu184a5cd2014-09-04 18:13:01 +08002957 struct f2fs_sit_block *raw_sit = NULL;
2958 unsigned int start_segno = ses->start_segno;
2959 unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002960 (unsigned long)MAIN_SEGS(sbi));
Chao Yu184a5cd2014-09-04 18:13:01 +08002961 unsigned int segno = start_segno;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09002962
Chao Yu184a5cd2014-09-04 18:13:01 +08002963 if (to_journal &&
Chao Yudfc08a12016-02-14 18:50:40 +08002964 !__has_cursum_space(journal, ses->entry_cnt, SIT_JOURNAL))
Chao Yu184a5cd2014-09-04 18:13:01 +08002965 to_journal = false;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002966
Chao Yub7ad7512016-02-19 18:08:46 +08002967 if (to_journal) {
2968 down_write(&curseg->journal_rwsem);
2969 } else {
Chao Yu184a5cd2014-09-04 18:13:01 +08002970 page = get_next_sit_page(sbi, start_segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002971 raw_sit = page_address(page);
2972 }
2973
Chao Yu184a5cd2014-09-04 18:13:01 +08002974 /* flush dirty sit entries in region of current sit set */
2975 for_each_set_bit_from(segno, bitmap, end) {
2976 int offset, sit_offset;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002977
2978 se = get_seg_entry(sbi, segno);
Chao Yu184a5cd2014-09-04 18:13:01 +08002979
2980 /* add discard candidates */
Chao Yuc473f1a2017-04-27 20:40:39 +08002981 if (!(cpc->reason & CP_DISCARD)) {
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002982 cpc->trim_start = segno;
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08002983 add_discard_addrs(sbi, cpc, false);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002984 }
Chao Yu184a5cd2014-09-04 18:13:01 +08002985
2986 if (to_journal) {
Chao Yudfc08a12016-02-14 18:50:40 +08002987 offset = lookup_journal_in_cursum(journal,
Chao Yu184a5cd2014-09-04 18:13:01 +08002988 SIT_JOURNAL, segno, 1);
2989 f2fs_bug_on(sbi, offset < 0);
Chao Yudfc08a12016-02-14 18:50:40 +08002990 segno_in_journal(journal, offset) =
Chao Yu184a5cd2014-09-04 18:13:01 +08002991 cpu_to_le32(segno);
2992 seg_info_to_raw_sit(se,
Chao Yudfc08a12016-02-14 18:50:40 +08002993 &sit_in_journal(journal, offset));
Chao Yu184a5cd2014-09-04 18:13:01 +08002994 } else {
2995 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
2996 seg_info_to_raw_sit(se,
2997 &raw_sit->entries[sit_offset]);
2998 }
2999
3000 __clear_bit(segno, bitmap);
3001 sit_i->dirty_sentries--;
3002 ses->entry_cnt--;
3003 }
3004
Chao Yub7ad7512016-02-19 18:08:46 +08003005 if (to_journal)
3006 up_write(&curseg->journal_rwsem);
3007 else
Chao Yu184a5cd2014-09-04 18:13:01 +08003008 f2fs_put_page(page, 1);
3009
3010 f2fs_bug_on(sbi, ses->entry_cnt);
3011 release_sit_entry_set(ses);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003012 }
Chao Yu184a5cd2014-09-04 18:13:01 +08003013
3014 f2fs_bug_on(sbi, !list_empty(head));
3015 f2fs_bug_on(sbi, sit_i->dirty_sentries);
Chao Yu184a5cd2014-09-04 18:13:01 +08003016out:
Chao Yuc473f1a2017-04-27 20:40:39 +08003017 if (cpc->reason & CP_DISCARD) {
Yunlei He650d3c42016-12-22 11:46:24 +08003018 __u64 trim_start = cpc->trim_start;
3019
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003020 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++)
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08003021 add_discard_addrs(sbi, cpc, false);
Yunlei He650d3c42016-12-22 11:46:24 +08003022
3023 cpc->trim_start = trim_start;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003024 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003025 mutex_unlock(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003026
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003027 set_prefree_as_free_segments(sbi);
3028}
3029
3030static int build_sit_info(struct f2fs_sb_info *sbi)
3031{
3032 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003033 struct sit_info *sit_i;
3034 unsigned int sit_segs, start;
Chao Yuae27d622017-01-07 18:52:34 +08003035 char *src_bitmap;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003036 unsigned int bitmap_size;
3037
3038 /* allocate memory for SIT information */
3039 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
3040 if (!sit_i)
3041 return -ENOMEM;
3042
3043 SM_I(sbi)->sit_info = sit_i;
3044
Michal Hockoa7c3e902017-05-08 15:57:09 -07003045 sit_i->sentries = kvzalloc(MAIN_SEGS(sbi) *
Jaegeuk Kim39307a82015-09-22 13:50:47 -07003046 sizeof(struct seg_entry), GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003047 if (!sit_i->sentries)
3048 return -ENOMEM;
3049
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003050 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Michal Hockoa7c3e902017-05-08 15:57:09 -07003051 sit_i->dirty_sentries_bitmap = kvzalloc(bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003052 if (!sit_i->dirty_sentries_bitmap)
3053 return -ENOMEM;
3054
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003055 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003056 sit_i->sentries[start].cur_valid_map
3057 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
3058 sit_i->sentries[start].ckpt_valid_map
3059 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07003060 if (!sit_i->sentries[start].cur_valid_map ||
Jaegeuk Kim3e025742016-08-02 10:56:40 -07003061 !sit_i->sentries[start].ckpt_valid_map)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003062 return -ENOMEM;
Jaegeuk Kim3e025742016-08-02 10:56:40 -07003063
Chao Yu355e7892017-01-07 18:51:01 +08003064#ifdef CONFIG_F2FS_CHECK_FS
3065 sit_i->sentries[start].cur_valid_map_mir
3066 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
3067 if (!sit_i->sentries[start].cur_valid_map_mir)
3068 return -ENOMEM;
3069#endif
3070
Jaegeuk Kim3e025742016-08-02 10:56:40 -07003071 if (f2fs_discard_en(sbi)) {
3072 sit_i->sentries[start].discard_map
3073 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
3074 if (!sit_i->sentries[start].discard_map)
3075 return -ENOMEM;
3076 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003077 }
3078
Jaegeuk Kim60a3b782015-02-10 16:44:29 -08003079 sit_i->tmp_map = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
3080 if (!sit_i->tmp_map)
3081 return -ENOMEM;
3082
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003083 if (sbi->segs_per_sec > 1) {
Michal Hockoa7c3e902017-05-08 15:57:09 -07003084 sit_i->sec_entries = kvzalloc(MAIN_SECS(sbi) *
Jaegeuk Kim39307a82015-09-22 13:50:47 -07003085 sizeof(struct sec_entry), GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003086 if (!sit_i->sec_entries)
3087 return -ENOMEM;
3088 }
3089
3090 /* get information related with SIT */
3091 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
3092
3093 /* setup SIT bitmap from ckeckpoint pack */
3094 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
3095 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
3096
Chao Yuae27d622017-01-07 18:52:34 +08003097 sit_i->sit_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
3098 if (!sit_i->sit_bitmap)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003099 return -ENOMEM;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003100
Chao Yuae27d622017-01-07 18:52:34 +08003101#ifdef CONFIG_F2FS_CHECK_FS
3102 sit_i->sit_bitmap_mir = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
3103 if (!sit_i->sit_bitmap_mir)
3104 return -ENOMEM;
3105#endif
3106
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003107 /* init SIT information */
3108 sit_i->s_ops = &default_salloc_ops;
3109
3110 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
3111 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
Jaegeuk Kimc79b7ff2016-11-14 18:20:10 -08003112 sit_i->written_valid_blocks = 0;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003113 sit_i->bitmap_size = bitmap_size;
3114 sit_i->dirty_sentries = 0;
3115 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
3116 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
Deepa Dinamani48fbfe52017-05-08 15:59:10 -07003117 sit_i->mounted_time = ktime_get_real_seconds();
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003118 mutex_init(&sit_i->sentry_lock);
3119 return 0;
3120}
3121
3122static int build_free_segmap(struct f2fs_sb_info *sbi)
3123{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003124 struct free_segmap_info *free_i;
3125 unsigned int bitmap_size, sec_bitmap_size;
3126
3127 /* allocate memory for free segmap information */
3128 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
3129 if (!free_i)
3130 return -ENOMEM;
3131
3132 SM_I(sbi)->free_info = free_i;
3133
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003134 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Michal Hockoa7c3e902017-05-08 15:57:09 -07003135 free_i->free_segmap = kvmalloc(bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003136 if (!free_i->free_segmap)
3137 return -ENOMEM;
3138
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003139 sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
Michal Hockoa7c3e902017-05-08 15:57:09 -07003140 free_i->free_secmap = kvmalloc(sec_bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003141 if (!free_i->free_secmap)
3142 return -ENOMEM;
3143
3144 /* set all segments as dirty temporarily */
3145 memset(free_i->free_segmap, 0xff, bitmap_size);
3146 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
3147
3148 /* init free segmap information */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003149 free_i->start_segno = GET_SEGNO_FROM_SEG0(sbi, MAIN_BLKADDR(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003150 free_i->free_segments = 0;
3151 free_i->free_sections = 0;
Chao Yu1a118cc2015-02-11 18:20:38 +08003152 spin_lock_init(&free_i->segmap_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003153 return 0;
3154}
3155
3156static int build_curseg(struct f2fs_sb_info *sbi)
3157{
Namjae Jeon1042d602012-12-01 10:56:13 +09003158 struct curseg_info *array;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003159 int i;
3160
Fabian Frederickb434bab2014-06-23 18:39:15 +02003161 array = kcalloc(NR_CURSEG_TYPE, sizeof(*array), GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003162 if (!array)
3163 return -ENOMEM;
3164
3165 SM_I(sbi)->curseg_array = array;
3166
3167 for (i = 0; i < NR_CURSEG_TYPE; i++) {
3168 mutex_init(&array[i].curseg_mutex);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003169 array[i].sum_blk = kzalloc(PAGE_SIZE, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003170 if (!array[i].sum_blk)
3171 return -ENOMEM;
Chao Yub7ad7512016-02-19 18:08:46 +08003172 init_rwsem(&array[i].journal_rwsem);
3173 array[i].journal = kzalloc(sizeof(struct f2fs_journal),
3174 GFP_KERNEL);
3175 if (!array[i].journal)
3176 return -ENOMEM;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003177 array[i].segno = NULL_SEGNO;
3178 array[i].next_blkoff = 0;
3179 }
3180 return restore_curseg_summaries(sbi);
3181}
3182
3183static void build_sit_entries(struct f2fs_sb_info *sbi)
3184{
3185 struct sit_info *sit_i = SIT_I(sbi);
3186 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08003187 struct f2fs_journal *journal = curseg->journal;
Yunlei He9c094042016-09-24 12:29:18 +08003188 struct seg_entry *se;
3189 struct f2fs_sit_entry sit;
Chao Yu74de5932013-11-22 09:09:59 +08003190 int sit_blk_cnt = SIT_BLK_CNT(sbi);
3191 unsigned int i, start, end;
3192 unsigned int readed, start_blk = 0;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003193
Chao Yu74de5932013-11-22 09:09:59 +08003194 do {
Jaegeuk Kim664ba972016-10-18 11:07:45 -07003195 readed = ra_meta_pages(sbi, start_blk, BIO_MAX_PAGES,
3196 META_SIT, true);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003197
Chao Yu74de5932013-11-22 09:09:59 +08003198 start = start_blk * sit_i->sents_per_block;
3199 end = (start_blk + readed) * sit_i->sents_per_block;
3200
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003201 for (; start < end && start < MAIN_SEGS(sbi); start++) {
Chao Yu74de5932013-11-22 09:09:59 +08003202 struct f2fs_sit_block *sit_blk;
Chao Yu74de5932013-11-22 09:09:59 +08003203 struct page *page;
3204
Yunlei He9c094042016-09-24 12:29:18 +08003205 se = &sit_i->sentries[start];
Chao Yu74de5932013-11-22 09:09:59 +08003206 page = get_current_sit_page(sbi, start);
3207 sit_blk = (struct f2fs_sit_block *)page_address(page);
3208 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
3209 f2fs_put_page(page, 1);
Chao Yud600af232016-08-19 23:13:47 +08003210
Chao Yu74de5932013-11-22 09:09:59 +08003211 check_block_count(sbi, start, &sit);
3212 seg_info_from_raw_sit(se, &sit);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07003213
3214 /* build discard map only one time */
Jaegeuk Kim3e025742016-08-02 10:56:40 -07003215 if (f2fs_discard_en(sbi)) {
Chao Yu1f43e2a2017-04-28 13:56:08 +08003216 if (is_set_ckpt_flags(sbi, CP_TRIMMED_FLAG)) {
3217 memset(se->discard_map, 0xff,
3218 SIT_VBLOCK_MAP_SIZE);
3219 } else {
3220 memcpy(se->discard_map,
3221 se->cur_valid_map,
3222 SIT_VBLOCK_MAP_SIZE);
3223 sbi->discard_blks +=
3224 sbi->blocks_per_seg -
3225 se->valid_blocks;
3226 }
Jaegeuk Kim3e025742016-08-02 10:56:40 -07003227 }
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07003228
Chao Yud600af232016-08-19 23:13:47 +08003229 if (sbi->segs_per_sec > 1)
3230 get_sec_entry(sbi, start)->valid_blocks +=
3231 se->valid_blocks;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003232 }
Chao Yu74de5932013-11-22 09:09:59 +08003233 start_blk += readed;
3234 } while (start_blk < sit_blk_cnt);
Chao Yud600af232016-08-19 23:13:47 +08003235
3236 down_read(&curseg->journal_rwsem);
3237 for (i = 0; i < sits_in_cursum(journal); i++) {
Chao Yud600af232016-08-19 23:13:47 +08003238 unsigned int old_valid_blocks;
3239
3240 start = le32_to_cpu(segno_in_journal(journal, i));
3241 se = &sit_i->sentries[start];
3242 sit = sit_in_journal(journal, i);
3243
3244 old_valid_blocks = se->valid_blocks;
3245
3246 check_block_count(sbi, start, &sit);
3247 seg_info_from_raw_sit(se, &sit);
3248
3249 if (f2fs_discard_en(sbi)) {
Chao Yu1f43e2a2017-04-28 13:56:08 +08003250 if (is_set_ckpt_flags(sbi, CP_TRIMMED_FLAG)) {
3251 memset(se->discard_map, 0xff,
3252 SIT_VBLOCK_MAP_SIZE);
3253 } else {
3254 memcpy(se->discard_map, se->cur_valid_map,
3255 SIT_VBLOCK_MAP_SIZE);
3256 sbi->discard_blks += old_valid_blocks -
3257 se->valid_blocks;
3258 }
Chao Yud600af232016-08-19 23:13:47 +08003259 }
3260
3261 if (sbi->segs_per_sec > 1)
3262 get_sec_entry(sbi, start)->valid_blocks +=
3263 se->valid_blocks - old_valid_blocks;
3264 }
3265 up_read(&curseg->journal_rwsem);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003266}
3267
3268static void init_free_segmap(struct f2fs_sb_info *sbi)
3269{
3270 unsigned int start;
3271 int type;
3272
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003273 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003274 struct seg_entry *sentry = get_seg_entry(sbi, start);
3275 if (!sentry->valid_blocks)
3276 __set_free(sbi, start);
Jaegeuk Kimc79b7ff2016-11-14 18:20:10 -08003277 else
3278 SIT_I(sbi)->written_valid_blocks +=
3279 sentry->valid_blocks;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003280 }
3281
3282 /* set use the current segments */
3283 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
3284 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
3285 __set_test_and_inuse(sbi, curseg_t->segno);
3286 }
3287}
3288
3289static void init_dirty_segmap(struct f2fs_sb_info *sbi)
3290{
3291 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
3292 struct free_segmap_info *free_i = FREE_I(sbi);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003293 unsigned int segno = 0, offset = 0;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003294 unsigned short valid_blocks;
3295
Namjae Jeon8736fbf2013-06-16 09:49:11 +09003296 while (1) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003297 /* find dirty segment based on free segmap */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003298 segno = find_next_inuse(free_i, MAIN_SEGS(sbi), offset);
3299 if (segno >= MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003300 break;
3301 offset = segno + 1;
Jaegeuk Kim302bd342017-04-07 14:33:22 -07003302 valid_blocks = get_valid_blocks(sbi, segno, false);
Jaegeuk Kimec325b52014-09-02 16:24:11 -07003303 if (valid_blocks == sbi->blocks_per_seg || !valid_blocks)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003304 continue;
Jaegeuk Kimec325b52014-09-02 16:24:11 -07003305 if (valid_blocks > sbi->blocks_per_seg) {
3306 f2fs_bug_on(sbi, 1);
3307 continue;
3308 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003309 mutex_lock(&dirty_i->seglist_lock);
3310 __locate_dirty_segment(sbi, segno, DIRTY);
3311 mutex_unlock(&dirty_i->seglist_lock);
3312 }
3313}
3314
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09003315static int init_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003316{
3317 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003318 unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003319
Michal Hockoa7c3e902017-05-08 15:57:09 -07003320 dirty_i->victim_secmap = kvzalloc(bitmap_size, GFP_KERNEL);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09003321 if (!dirty_i->victim_secmap)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003322 return -ENOMEM;
3323 return 0;
3324}
3325
3326static int build_dirty_segmap(struct f2fs_sb_info *sbi)
3327{
3328 struct dirty_seglist_info *dirty_i;
3329 unsigned int bitmap_size, i;
3330
3331 /* allocate memory for dirty segments list information */
3332 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
3333 if (!dirty_i)
3334 return -ENOMEM;
3335
3336 SM_I(sbi)->dirty_info = dirty_i;
3337 mutex_init(&dirty_i->seglist_lock);
3338
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003339 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003340
3341 for (i = 0; i < NR_DIRTY_TYPE; i++) {
Michal Hockoa7c3e902017-05-08 15:57:09 -07003342 dirty_i->dirty_segmap[i] = kvzalloc(bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003343 if (!dirty_i->dirty_segmap[i])
3344 return -ENOMEM;
3345 }
3346
3347 init_dirty_segmap(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09003348 return init_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003349}
3350
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09003351/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003352 * Update min, max modified time for cost-benefit GC algorithm
3353 */
3354static void init_min_max_mtime(struct f2fs_sb_info *sbi)
3355{
3356 struct sit_info *sit_i = SIT_I(sbi);
3357 unsigned int segno;
3358
3359 mutex_lock(&sit_i->sentry_lock);
3360
3361 sit_i->min_mtime = LLONG_MAX;
3362
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003363 for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003364 unsigned int i;
3365 unsigned long long mtime = 0;
3366
3367 for (i = 0; i < sbi->segs_per_sec; i++)
3368 mtime += get_seg_entry(sbi, segno + i)->mtime;
3369
3370 mtime = div_u64(mtime, sbi->segs_per_sec);
3371
3372 if (sit_i->min_mtime > mtime)
3373 sit_i->min_mtime = mtime;
3374 }
3375 sit_i->max_mtime = get_mtime(sbi);
3376 mutex_unlock(&sit_i->sentry_lock);
3377}
3378
3379int build_segment_manager(struct f2fs_sb_info *sbi)
3380{
3381 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
3382 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Namjae Jeon1042d602012-12-01 10:56:13 +09003383 struct f2fs_sm_info *sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003384 int err;
3385
3386 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
3387 if (!sm_info)
3388 return -ENOMEM;
3389
3390 /* init sm info */
3391 sbi->sm_info = sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003392 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
3393 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
3394 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
3395 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
3396 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
3397 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
3398 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
Jaegeuk Kim58c41032014-03-19 14:17:21 +09003399 sm_info->rec_prefree_segments = sm_info->main_segments *
3400 DEF_RECLAIM_PREFREE_SEGMENTS / 100;
Jaegeuk Kim44a83492016-07-13 18:23:35 -07003401 if (sm_info->rec_prefree_segments > DEF_MAX_RECLAIM_PREFREE_SEGMENTS)
3402 sm_info->rec_prefree_segments = DEF_MAX_RECLAIM_PREFREE_SEGMENTS;
3403
Jaegeuk Kim52763a42016-06-13 09:47:48 -07003404 if (!test_opt(sbi, LFS))
3405 sm_info->ipu_policy = 1 << F2FS_IPU_FSYNC;
Jaegeuk Kim216fbd62013-11-07 13:13:42 +09003406 sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -07003407 sm_info->min_fsync_blocks = DEF_MIN_FSYNC_BLOCKS;
Jaegeuk Kimef095d12017-03-24 20:05:13 -04003408 sm_info->min_hot_blocks = DEF_MIN_HOT_BLOCKS;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003409
Jaegeuk Kimbba681c2015-01-26 17:41:23 -08003410 sm_info->trim_sections = DEF_BATCHED_TRIM_SECTIONS;
3411
Chao Yu184a5cd2014-09-04 18:13:01 +08003412 INIT_LIST_HEAD(&sm_info->sit_entry_set);
3413
Yunlei Hed4fdf8b2017-06-01 16:43:51 +08003414 if (!f2fs_readonly(sbi->sb)) {
Gu Zheng2163d192014-04-27 14:21:33 +08003415 err = create_flush_cmd_control(sbi);
3416 if (err)
Gu Zhenga688b9d9e2014-04-27 14:21:21 +08003417 return err;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +09003418 }
3419
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08003420 err = create_discard_cmd_control(sbi);
3421 if (err)
3422 return err;
3423
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003424 err = build_sit_info(sbi);
3425 if (err)
3426 return err;
3427 err = build_free_segmap(sbi);
3428 if (err)
3429 return err;
3430 err = build_curseg(sbi);
3431 if (err)
3432 return err;
3433
3434 /* reinit free segmap based on SIT */
3435 build_sit_entries(sbi);
3436
3437 init_free_segmap(sbi);
3438 err = build_dirty_segmap(sbi);
3439 if (err)
3440 return err;
3441
3442 init_min_max_mtime(sbi);
3443 return 0;
3444}
3445
3446static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
3447 enum dirty_type dirty_type)
3448{
3449 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
3450
3451 mutex_lock(&dirty_i->seglist_lock);
Jaegeuk Kim39307a82015-09-22 13:50:47 -07003452 kvfree(dirty_i->dirty_segmap[dirty_type]);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003453 dirty_i->nr_dirty[dirty_type] = 0;
3454 mutex_unlock(&dirty_i->seglist_lock);
3455}
3456
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09003457static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003458{
3459 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim39307a82015-09-22 13:50:47 -07003460 kvfree(dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003461}
3462
3463static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
3464{
3465 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
3466 int i;
3467
3468 if (!dirty_i)
3469 return;
3470
3471 /* discard pre-free/dirty segments list */
3472 for (i = 0; i < NR_DIRTY_TYPE; i++)
3473 discard_dirty_segmap(sbi, i);
3474
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09003475 destroy_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003476 SM_I(sbi)->dirty_info = NULL;
3477 kfree(dirty_i);
3478}
3479
3480static void destroy_curseg(struct f2fs_sb_info *sbi)
3481{
3482 struct curseg_info *array = SM_I(sbi)->curseg_array;
3483 int i;
3484
3485 if (!array)
3486 return;
3487 SM_I(sbi)->curseg_array = NULL;
Chao Yub7ad7512016-02-19 18:08:46 +08003488 for (i = 0; i < NR_CURSEG_TYPE; i++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003489 kfree(array[i].sum_blk);
Chao Yub7ad7512016-02-19 18:08:46 +08003490 kfree(array[i].journal);
3491 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003492 kfree(array);
3493}
3494
3495static void destroy_free_segmap(struct f2fs_sb_info *sbi)
3496{
3497 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
3498 if (!free_i)
3499 return;
3500 SM_I(sbi)->free_info = NULL;
Jaegeuk Kim39307a82015-09-22 13:50:47 -07003501 kvfree(free_i->free_segmap);
3502 kvfree(free_i->free_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003503 kfree(free_i);
3504}
3505
3506static void destroy_sit_info(struct f2fs_sb_info *sbi)
3507{
3508 struct sit_info *sit_i = SIT_I(sbi);
3509 unsigned int start;
3510
3511 if (!sit_i)
3512 return;
3513
3514 if (sit_i->sentries) {
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003515 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003516 kfree(sit_i->sentries[start].cur_valid_map);
Chao Yu355e7892017-01-07 18:51:01 +08003517#ifdef CONFIG_F2FS_CHECK_FS
3518 kfree(sit_i->sentries[start].cur_valid_map_mir);
3519#endif
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003520 kfree(sit_i->sentries[start].ckpt_valid_map);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07003521 kfree(sit_i->sentries[start].discard_map);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003522 }
3523 }
Jaegeuk Kim60a3b782015-02-10 16:44:29 -08003524 kfree(sit_i->tmp_map);
3525
Jaegeuk Kim39307a82015-09-22 13:50:47 -07003526 kvfree(sit_i->sentries);
3527 kvfree(sit_i->sec_entries);
3528 kvfree(sit_i->dirty_sentries_bitmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003529
3530 SM_I(sbi)->sit_info = NULL;
3531 kfree(sit_i->sit_bitmap);
Chao Yuae27d622017-01-07 18:52:34 +08003532#ifdef CONFIG_F2FS_CHECK_FS
3533 kfree(sit_i->sit_bitmap_mir);
3534#endif
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003535 kfree(sit_i);
3536}
3537
3538void destroy_segment_manager(struct f2fs_sb_info *sbi)
3539{
3540 struct f2fs_sm_info *sm_info = SM_I(sbi);
Gu Zhenga688b9d9e2014-04-27 14:21:21 +08003541
Chao Yu3b03f722013-11-06 09:12:04 +08003542 if (!sm_info)
3543 return;
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -08003544 destroy_flush_cmd_control(sbi, true);
Chao Yuf0994052017-03-27 18:14:04 +08003545 destroy_discard_cmd_control(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003546 destroy_dirty_segmap(sbi);
3547 destroy_curseg(sbi);
3548 destroy_free_segmap(sbi);
3549 destroy_sit_info(sbi);
3550 sbi->sm_info = NULL;
3551 kfree(sm_info);
3552}
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09003553
3554int __init create_segment_manager_caches(void)
3555{
3556 discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
Gu Zhenge8512d22014-03-07 18:43:28 +08003557 sizeof(struct discard_entry));
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09003558 if (!discard_entry_slab)
Chao Yu184a5cd2014-09-04 18:13:01 +08003559 goto fail;
3560
Jaegeuk Kimb01a9202017-01-09 14:13:03 -08003561 discard_cmd_slab = f2fs_kmem_cache_create("discard_cmd",
3562 sizeof(struct discard_cmd));
3563 if (!discard_cmd_slab)
Chao Yu6ab2a302016-09-05 12:28:26 +08003564 goto destroy_discard_entry;
Chao Yu275b66b2016-08-29 23:58:34 +08003565
Chao Yu184a5cd2014-09-04 18:13:01 +08003566 sit_entry_set_slab = f2fs_kmem_cache_create("sit_entry_set",
Changman Leec9ee0082014-11-21 15:42:07 +09003567 sizeof(struct sit_entry_set));
Chao Yu184a5cd2014-09-04 18:13:01 +08003568 if (!sit_entry_set_slab)
Jaegeuk Kimb01a9202017-01-09 14:13:03 -08003569 goto destroy_discard_cmd;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07003570
3571 inmem_entry_slab = f2fs_kmem_cache_create("inmem_page_entry",
3572 sizeof(struct inmem_pages));
3573 if (!inmem_entry_slab)
3574 goto destroy_sit_entry_set;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09003575 return 0;
Chao Yu184a5cd2014-09-04 18:13:01 +08003576
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07003577destroy_sit_entry_set:
3578 kmem_cache_destroy(sit_entry_set_slab);
Jaegeuk Kimb01a9202017-01-09 14:13:03 -08003579destroy_discard_cmd:
3580 kmem_cache_destroy(discard_cmd_slab);
Chao Yu6ab2a302016-09-05 12:28:26 +08003581destroy_discard_entry:
Chao Yu184a5cd2014-09-04 18:13:01 +08003582 kmem_cache_destroy(discard_entry_slab);
3583fail:
3584 return -ENOMEM;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09003585}
3586
3587void destroy_segment_manager_caches(void)
3588{
Chao Yu184a5cd2014-09-04 18:13:01 +08003589 kmem_cache_destroy(sit_entry_set_slab);
Jaegeuk Kimb01a9202017-01-09 14:13:03 -08003590 kmem_cache_destroy(discard_cmd_slab);
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09003591 kmem_cache_destroy(discard_entry_slab);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07003592 kmem_cache_destroy(inmem_entry_slab);
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09003593}