blob: 3189537a19dc59926b9c30a2c9d301f93bd08415 [file] [log] [blame]
Chao Yu7c1a0002018-09-12 09:16:07 +08001// SPDX-License-Identifier: GPL-2.0
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003 * fs/f2fs/segment.c
4 *
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09007 */
8#include <linux/fs.h>
9#include <linux/f2fs_fs.h>
10#include <linux/bio.h>
11#include <linux/blkdev.h>
Geert Uytterhoeven690e4a32012-12-19 22:19:30 +010012#include <linux/prefetch.h>
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +090013#include <linux/kthread.h>
Chao Yu74de5932013-11-22 09:09:59 +080014#include <linux/swap.h>
Jaegeuk Kim60b99b42015-10-05 14:49:57 -070015#include <linux/timer.h>
Jaegeuk Kim1d7be272017-05-17 10:36:58 -070016#include <linux/freezer.h>
Jaegeuk Kim1eb1ef42017-09-09 12:03:23 -070017#include <linux/sched/signal.h>
Jaegeuk Kim351df4b2012-11-02 17:09:16 +090018
19#include "f2fs.h"
20#include "segment.h"
21#include "node.h"
Jaegeuk Kim5f656542017-08-15 21:27:19 -070022#include "gc.h"
Daeho Jeong52118742021-08-19 20:52:28 -070023#include "iostat.h"
Namjae Jeon6ec178d2013-04-23 17:51:43 +090024#include <trace/events/f2fs.h>
Jaegeuk Kim351df4b2012-11-02 17:09:16 +090025
Changman Lee9a7f1432013-11-15 10:42:51 +090026#define __reverse_ffz(x) __reverse_ffs(~(x))
27
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +090028static struct kmem_cache *discard_entry_slab;
Jaegeuk Kimb01a9202017-01-09 14:13:03 -080029static struct kmem_cache *discard_cmd_slab;
Chao Yu184a5cd2014-09-04 18:13:01 +080030static struct kmem_cache *sit_entry_set_slab;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -070031static struct kmem_cache *inmem_entry_slab;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +090032
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070033static unsigned long __reverse_ulong(unsigned char *str)
34{
35 unsigned long tmp = 0;
36 int shift = 24, idx = 0;
37
38#if BITS_PER_LONG == 64
39 shift = 56;
40#endif
41 while (shift >= 0) {
42 tmp |= (unsigned long)str[idx++] << shift;
43 shift -= BITS_PER_BYTE;
44 }
45 return tmp;
46}
47
Changman Lee9a7f1432013-11-15 10:42:51 +090048/*
49 * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since
50 * MSB and LSB are reversed in a byte by f2fs_set_bit.
51 */
52static inline unsigned long __reverse_ffs(unsigned long word)
53{
54 int num = 0;
55
56#if BITS_PER_LONG == 64
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070057 if ((word & 0xffffffff00000000UL) == 0)
Changman Lee9a7f1432013-11-15 10:42:51 +090058 num += 32;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070059 else
Changman Lee9a7f1432013-11-15 10:42:51 +090060 word >>= 32;
Changman Lee9a7f1432013-11-15 10:42:51 +090061#endif
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070062 if ((word & 0xffff0000) == 0)
Changman Lee9a7f1432013-11-15 10:42:51 +090063 num += 16;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070064 else
Changman Lee9a7f1432013-11-15 10:42:51 +090065 word >>= 16;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070066
67 if ((word & 0xff00) == 0)
Changman Lee9a7f1432013-11-15 10:42:51 +090068 num += 8;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070069 else
Changman Lee9a7f1432013-11-15 10:42:51 +090070 word >>= 8;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070071
Changman Lee9a7f1432013-11-15 10:42:51 +090072 if ((word & 0xf0) == 0)
73 num += 4;
74 else
75 word >>= 4;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070076
Changman Lee9a7f1432013-11-15 10:42:51 +090077 if ((word & 0xc) == 0)
78 num += 2;
79 else
80 word >>= 2;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070081
Changman Lee9a7f1432013-11-15 10:42:51 +090082 if ((word & 0x2) == 0)
83 num += 1;
84 return num;
85}
86
87/*
arter97e1c42042014-08-06 23:22:50 +090088 * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c because
Changman Lee9a7f1432013-11-15 10:42:51 +090089 * f2fs_set_bit makes MSB and LSB reversed in a byte.
Fan Li692223d2015-11-12 08:43:04 +080090 * @size must be integral times of unsigned long.
Changman Lee9a7f1432013-11-15 10:42:51 +090091 * Example:
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070092 * MSB <--> LSB
93 * f2fs_set_bit(0, bitmap) => 1000 0000
94 * f2fs_set_bit(7, bitmap) => 0000 0001
Changman Lee9a7f1432013-11-15 10:42:51 +090095 */
96static unsigned long __find_rev_next_bit(const unsigned long *addr,
97 unsigned long size, unsigned long offset)
98{
99 const unsigned long *p = addr + BIT_WORD(offset);
Fan Li692223d2015-11-12 08:43:04 +0800100 unsigned long result = size;
Changman Lee9a7f1432013-11-15 10:42:51 +0900101 unsigned long tmp;
Changman Lee9a7f1432013-11-15 10:42:51 +0900102
103 if (offset >= size)
104 return size;
105
Fan Li692223d2015-11-12 08:43:04 +0800106 size -= (offset & ~(BITS_PER_LONG - 1));
Changman Lee9a7f1432013-11-15 10:42:51 +0900107 offset %= BITS_PER_LONG;
Changman Lee9a7f1432013-11-15 10:42:51 +0900108
Fan Li692223d2015-11-12 08:43:04 +0800109 while (1) {
110 if (*p == 0)
111 goto pass;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700112
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700113 tmp = __reverse_ulong((unsigned char *)p);
Fan Li692223d2015-11-12 08:43:04 +0800114
115 tmp &= ~0UL >> offset;
116 if (size < BITS_PER_LONG)
117 tmp &= (~0UL << (BITS_PER_LONG - size));
Changman Lee9a7f1432013-11-15 10:42:51 +0900118 if (tmp)
Fan Li692223d2015-11-12 08:43:04 +0800119 goto found;
120pass:
121 if (size <= BITS_PER_LONG)
122 break;
Changman Lee9a7f1432013-11-15 10:42:51 +0900123 size -= BITS_PER_LONG;
Fan Li692223d2015-11-12 08:43:04 +0800124 offset = 0;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700125 p++;
Changman Lee9a7f1432013-11-15 10:42:51 +0900126 }
Fan Li692223d2015-11-12 08:43:04 +0800127 return result;
128found:
129 return result - size + __reverse_ffs(tmp);
Changman Lee9a7f1432013-11-15 10:42:51 +0900130}
131
132static unsigned long __find_rev_next_zero_bit(const unsigned long *addr,
133 unsigned long size, unsigned long offset)
134{
135 const unsigned long *p = addr + BIT_WORD(offset);
Jaegeuk Kim80609442015-12-04 16:51:13 -0800136 unsigned long result = size;
Changman Lee9a7f1432013-11-15 10:42:51 +0900137 unsigned long tmp;
Changman Lee9a7f1432013-11-15 10:42:51 +0900138
139 if (offset >= size)
140 return size;
141
Jaegeuk Kim80609442015-12-04 16:51:13 -0800142 size -= (offset & ~(BITS_PER_LONG - 1));
Changman Lee9a7f1432013-11-15 10:42:51 +0900143 offset %= BITS_PER_LONG;
Changman Lee9a7f1432013-11-15 10:42:51 +0900144
Jaegeuk Kim80609442015-12-04 16:51:13 -0800145 while (1) {
146 if (*p == ~0UL)
147 goto pass;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700148
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700149 tmp = __reverse_ulong((unsigned char *)p);
Jaegeuk Kim80609442015-12-04 16:51:13 -0800150
151 if (offset)
152 tmp |= ~0UL << (BITS_PER_LONG - offset);
153 if (size < BITS_PER_LONG)
154 tmp |= ~0UL >> size;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700155 if (tmp != ~0UL)
Jaegeuk Kim80609442015-12-04 16:51:13 -0800156 goto found;
157pass:
158 if (size <= BITS_PER_LONG)
159 break;
Changman Lee9a7f1432013-11-15 10:42:51 +0900160 size -= BITS_PER_LONG;
Jaegeuk Kim80609442015-12-04 16:51:13 -0800161 offset = 0;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700162 p++;
Changman Lee9a7f1432013-11-15 10:42:51 +0900163 }
Jaegeuk Kim80609442015-12-04 16:51:13 -0800164 return result;
165found:
166 return result - size + __reverse_ffz(tmp);
Changman Lee9a7f1432013-11-15 10:42:51 +0900167}
168
Chao Yu4d57b862018-05-30 00:20:41 +0800169bool f2fs_need_SSR(struct f2fs_sb_info *sbi)
Jaegeuk Kimb3a97a22017-09-09 11:11:04 -0700170{
171 int node_secs = get_blocktype_secs(sbi, F2FS_DIRTY_NODES);
172 int dent_secs = get_blocktype_secs(sbi, F2FS_DIRTY_DENTS);
173 int imeta_secs = get_blocktype_secs(sbi, F2FS_DIRTY_IMETA);
174
Chao Yub0332a02020-02-14 17:44:12 +0800175 if (f2fs_lfs_mode(sbi))
Jaegeuk Kimb3a97a22017-09-09 11:11:04 -0700176 return false;
Daeho Jeong0e5e8112020-07-02 13:14:14 +0900177 if (sbi->gc_mode == GC_URGENT_HIGH)
Jaegeuk Kimb3a97a22017-09-09 11:11:04 -0700178 return true;
Daniel Rosenberg43549942018-08-20 19:21:43 -0700179 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
180 return true;
Jaegeuk Kimb3a97a22017-09-09 11:11:04 -0700181
182 return free_sections(sbi) <= (node_secs + 2 * dent_secs + imeta_secs +
Chao Yua2a12b62017-10-28 16:52:33 +0800183 SM_I(sbi)->min_ssr_sections + reserved_sections(sbi));
Jaegeuk Kimb3a97a22017-09-09 11:11:04 -0700184}
185
Chao Yu4d57b862018-05-30 00:20:41 +0800186void f2fs_register_inmem_page(struct inode *inode, struct page *page)
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700187{
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700188 struct inmem_pages *new;
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800189
Chao Yub763f3b2021-04-28 17:20:31 +0800190 set_page_private_atomic(page);
Chao Yudecd36b2015-08-07 18:42:09 +0800191
Chao Yu32410572021-08-09 08:24:48 +0800192 new = f2fs_kmem_cache_alloc(inmem_entry_slab,
193 GFP_NOFS, true, NULL);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700194
195 /* add atomic page indices to the list */
196 new->page = page;
197 INIT_LIST_HEAD(&new->list);
Chao Yudecd36b2015-08-07 18:42:09 +0800198
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700199 /* increase reference count with clean state */
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700200 get_page(page);
Jaegeuk Kim743b6202019-09-09 13:10:59 +0100201 mutex_lock(&F2FS_I(inode)->inmem_lock);
202 list_add_tail(&new->list, &F2FS_I(inode)->inmem_pages);
Jaegeuk Kim8dcf2ff72014-12-05 17:18:15 -0800203 inc_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
Jaegeuk Kim743b6202019-09-09 13:10:59 +0100204 mutex_unlock(&F2FS_I(inode)->inmem_lock);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -0700205
206 trace_f2fs_register_inmem_page(page, INMEM);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700207}
208
Chao Yu28bc1062016-02-06 14:40:34 +0800209static int __revoke_inmem_pages(struct inode *inode,
Chao Yu48432982019-02-25 17:11:03 +0800210 struct list_head *head, bool drop, bool recover,
211 bool trylock)
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700212{
Chao Yu28bc1062016-02-06 14:40:34 +0800213 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700214 struct inmem_pages *cur, *tmp;
Chao Yu28bc1062016-02-06 14:40:34 +0800215 int err = 0;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700216
Chao Yu29b96b52016-02-06 14:38:29 +0800217 list_for_each_entry_safe(cur, tmp, head, list) {
Chao Yu28bc1062016-02-06 14:40:34 +0800218 struct page *page = cur->page;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700219
Chao Yu28bc1062016-02-06 14:40:34 +0800220 if (drop)
221 trace_f2fs_commit_inmem_page(page, INMEM_DROP);
222
Chao Yu48432982019-02-25 17:11:03 +0800223 if (trylock) {
224 /*
225 * to avoid deadlock in between page lock and
226 * inmem_lock.
227 */
228 if (!trylock_page(page))
229 continue;
230 } else {
231 lock_page(page);
232 }
Chao Yu28bc1062016-02-06 14:40:34 +0800233
Chao Yubae0ee72018-12-25 17:43:42 +0800234 f2fs_wait_on_page_writeback(page, DATA, true, true);
Chao Yue5e57322018-04-23 10:36:13 +0800235
Chao Yu28bc1062016-02-06 14:40:34 +0800236 if (recover) {
237 struct dnode_of_data dn;
238 struct node_info ni;
239
240 trace_f2fs_commit_inmem_page(page, INMEM_REVOKE);
Chao Yu7f2b4e82017-08-08 19:09:08 +0800241retry:
Chao Yu28bc1062016-02-06 14:40:34 +0800242 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +0800243 err = f2fs_get_dnode_of_data(&dn, page->index,
244 LOOKUP_NODE);
Chao Yu7f2b4e82017-08-08 19:09:08 +0800245 if (err) {
246 if (err == -ENOMEM) {
Chao Yu5df7731f2020-02-17 17:45:44 +0800247 congestion_wait(BLK_RW_ASYNC,
248 DEFAULT_IO_TIMEOUT);
Chao Yu7f2b4e82017-08-08 19:09:08 +0800249 cond_resched();
250 goto retry;
251 }
Chao Yu28bc1062016-02-06 14:40:34 +0800252 err = -EAGAIN;
253 goto next;
254 }
Chao Yu77357302018-07-17 00:02:17 +0800255
256 err = f2fs_get_node_info(sbi, dn.nid, &ni);
257 if (err) {
258 f2fs_put_dnode(&dn);
259 return err;
260 }
261
Daeho Jeongf1d25642018-01-10 16:49:10 +0900262 if (cur->old_addr == NEW_ADDR) {
Chao Yu4d57b862018-05-30 00:20:41 +0800263 f2fs_invalidate_blocks(sbi, dn.data_blkaddr);
Daeho Jeongf1d25642018-01-10 16:49:10 +0900264 f2fs_update_data_blkaddr(&dn, NEW_ADDR);
265 } else
266 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
Chao Yu28bc1062016-02-06 14:40:34 +0800267 cur->old_addr, ni.version, true, true);
268 f2fs_put_dnode(&dn);
269 }
270next:
Jaegeuk Kim63c52d72016-04-12 14:11:03 -0700271 /* we don't need to invalidate this in the sccessful status */
Chao Yu2baf0782018-07-27 18:15:16 +0800272 if (drop || recover) {
Jaegeuk Kim63c52d72016-04-12 14:11:03 -0700273 ClearPageUptodate(page);
Chao Yub763f3b2021-04-28 17:20:31 +0800274 clear_page_private_gcing(page);
Chao Yu2baf0782018-07-27 18:15:16 +0800275 }
Chao Yub763f3b2021-04-28 17:20:31 +0800276 detach_page_private(page);
277 set_page_private(page, 0);
Chao Yu28bc1062016-02-06 14:40:34 +0800278 f2fs_put_page(page, 1);
Chao Yudecd36b2015-08-07 18:42:09 +0800279
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700280 list_del(&cur->list);
281 kmem_cache_free(inmem_entry_slab, cur);
Jaegeuk Kim8dcf2ff72014-12-05 17:18:15 -0800282 dec_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700283 }
Chao Yu28bc1062016-02-06 14:40:34 +0800284 return err;
Chao Yu29b96b52016-02-06 14:38:29 +0800285}
286
Chao Yu4d57b862018-05-30 00:20:41 +0800287void f2fs_drop_inmem_pages_all(struct f2fs_sb_info *sbi, bool gc_failure)
Jaegeuk Kim57864ae2017-10-18 19:05:57 -0700288{
289 struct list_head *head = &sbi->inode_list[ATOMIC_FILE];
290 struct inode *inode;
291 struct f2fs_inode_info *fi;
Sahitya Tummala677017d2019-11-13 16:01:03 +0530292 unsigned int count = sbi->atomic_files;
293 unsigned int looped = 0;
Jaegeuk Kim57864ae2017-10-18 19:05:57 -0700294next:
295 spin_lock(&sbi->inode_lock[ATOMIC_FILE]);
296 if (list_empty(head)) {
297 spin_unlock(&sbi->inode_lock[ATOMIC_FILE]);
298 return;
299 }
300 fi = list_first_entry(head, struct f2fs_inode_info, inmem_ilist);
301 inode = igrab(&fi->vfs_inode);
Sahitya Tummala677017d2019-11-13 16:01:03 +0530302 if (inode)
303 list_move_tail(&fi->inmem_ilist, head);
Jaegeuk Kim57864ae2017-10-18 19:05:57 -0700304 spin_unlock(&sbi->inode_lock[ATOMIC_FILE]);
305
306 if (inode) {
Chao Yu2ef79ec2018-05-07 20:28:54 +0800307 if (gc_failure) {
Sahitya Tummala677017d2019-11-13 16:01:03 +0530308 if (!fi->i_gc_failures[GC_FAILURE_ATOMIC])
309 goto skip;
Chao Yu2ef79ec2018-05-07 20:28:54 +0800310 }
Chao Yu2ef79ec2018-05-07 20:28:54 +0800311 set_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
Chao Yu4d57b862018-05-30 00:20:41 +0800312 f2fs_drop_inmem_pages(inode);
Sahitya Tummala677017d2019-11-13 16:01:03 +0530313skip:
Jaegeuk Kim57864ae2017-10-18 19:05:57 -0700314 iput(inode);
315 }
Chao Yu5df7731f2020-02-17 17:45:44 +0800316 congestion_wait(BLK_RW_ASYNC, DEFAULT_IO_TIMEOUT);
Jaegeuk Kim57864ae2017-10-18 19:05:57 -0700317 cond_resched();
Sahitya Tummala677017d2019-11-13 16:01:03 +0530318 if (gc_failure) {
319 if (++looped >= count)
320 return;
321 }
Jaegeuk Kim57864ae2017-10-18 19:05:57 -0700322 goto next;
323}
324
Chao Yu4d57b862018-05-30 00:20:41 +0800325void f2fs_drop_inmem_pages(struct inode *inode)
Chao Yu29b96b52016-02-06 14:38:29 +0800326{
Jaegeuk Kim57864ae2017-10-18 19:05:57 -0700327 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu29b96b52016-02-06 14:38:29 +0800328 struct f2fs_inode_info *fi = F2FS_I(inode);
329
Yi Zhuangbe1ee452021-03-31 17:34:14 +0800330 do {
Chao Yu48432982019-02-25 17:11:03 +0800331 mutex_lock(&fi->inmem_lock);
Yi Zhuangbe1ee452021-03-31 17:34:14 +0800332 if (list_empty(&fi->inmem_pages)) {
333 fi->i_gc_failures[GC_FAILURE_ATOMIC] = 0;
334
335 spin_lock(&sbi->inode_lock[ATOMIC_FILE]);
336 if (!list_empty(&fi->inmem_ilist))
337 list_del_init(&fi->inmem_ilist);
338 if (f2fs_is_atomic_file(inode)) {
339 clear_inode_flag(inode, FI_ATOMIC_FILE);
340 sbi->atomic_files--;
341 }
342 spin_unlock(&sbi->inode_lock[ATOMIC_FILE]);
343
344 mutex_unlock(&fi->inmem_lock);
345 break;
346 }
Chao Yu48432982019-02-25 17:11:03 +0800347 __revoke_inmem_pages(inode, &fi->inmem_pages,
348 true, false, true);
Chao Yu48432982019-02-25 17:11:03 +0800349 mutex_unlock(&fi->inmem_lock);
Yi Zhuangbe1ee452021-03-31 17:34:14 +0800350 } while (1);
Chao Yu29b96b52016-02-06 14:38:29 +0800351}
352
Chao Yu4d57b862018-05-30 00:20:41 +0800353void f2fs_drop_inmem_page(struct inode *inode, struct page *page)
Jaegeuk Kim8c242db2017-03-17 09:55:52 +0800354{
355 struct f2fs_inode_info *fi = F2FS_I(inode);
356 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
357 struct list_head *head = &fi->inmem_pages;
358 struct inmem_pages *cur = NULL;
359
Chao Yub763f3b2021-04-28 17:20:31 +0800360 f2fs_bug_on(sbi, !page_private_atomic(page));
Jaegeuk Kim8c242db2017-03-17 09:55:52 +0800361
362 mutex_lock(&fi->inmem_lock);
363 list_for_each_entry(cur, head, list) {
364 if (cur->page == page)
365 break;
366 }
367
Sheng Yongd0891e82018-04-17 17:12:27 +0800368 f2fs_bug_on(sbi, list_empty(head) || cur->page != page);
Jaegeuk Kim8c242db2017-03-17 09:55:52 +0800369 list_del(&cur->list);
370 mutex_unlock(&fi->inmem_lock);
371
372 dec_page_count(sbi, F2FS_INMEM_PAGES);
373 kmem_cache_free(inmem_entry_slab, cur);
374
375 ClearPageUptodate(page);
Chao Yub763f3b2021-04-28 17:20:31 +0800376 clear_page_private_atomic(page);
Jaegeuk Kim8c242db2017-03-17 09:55:52 +0800377 f2fs_put_page(page, 0);
378
Chao Yub763f3b2021-04-28 17:20:31 +0800379 detach_page_private(page);
380 set_page_private(page, 0);
381
Jaegeuk Kim8c242db2017-03-17 09:55:52 +0800382 trace_f2fs_commit_inmem_page(page, INMEM_INVALIDATE);
383}
384
Chao Yu4d57b862018-05-30 00:20:41 +0800385static int __f2fs_commit_inmem_pages(struct inode *inode)
Chao Yu29b96b52016-02-06 14:38:29 +0800386{
387 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
388 struct f2fs_inode_info *fi = F2FS_I(inode);
389 struct inmem_pages *cur, *tmp;
390 struct f2fs_io_info fio = {
391 .sbi = sbi,
Chao Yu39d787b2017-09-29 13:59:38 +0800392 .ino = inode->i_ino,
Chao Yu29b96b52016-02-06 14:38:29 +0800393 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -0500394 .op = REQ_OP_WRITE,
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600395 .op_flags = REQ_SYNC | REQ_PRIO,
Chao Yub0af6d42017-08-02 23:21:48 +0800396 .io_type = FS_DATA_IO,
Chao Yu29b96b52016-02-06 14:38:29 +0800397 };
Chao Yucf52b272018-04-23 10:36:14 +0800398 struct list_head revoke_list;
Chao Yubab475c2018-09-27 23:41:16 +0800399 bool submit_bio = false;
Chao Yu29b96b52016-02-06 14:38:29 +0800400 int err = 0;
401
Chao Yucf52b272018-04-23 10:36:14 +0800402 INIT_LIST_HEAD(&revoke_list);
403
Chao Yu29b96b52016-02-06 14:38:29 +0800404 list_for_each_entry_safe(cur, tmp, &fi->inmem_pages, list) {
Chao Yu28bc1062016-02-06 14:40:34 +0800405 struct page *page = cur->page;
406
407 lock_page(page);
408 if (page->mapping == inode->i_mapping) {
409 trace_f2fs_commit_inmem_page(page, INMEM);
410
Chao Yubae0ee72018-12-25 17:43:42 +0800411 f2fs_wait_on_page_writeback(page, DATA, true, true);
Chao Yu8d64d362018-12-12 18:12:30 +0800412
413 set_page_dirty(page);
Chao Yu933439c2016-10-11 22:57:01 +0800414 if (clear_page_dirty_for_io(page)) {
Chao Yu29b96b52016-02-06 14:38:29 +0800415 inode_dec_dirty_pages(inode);
Chao Yu4d57b862018-05-30 00:20:41 +0800416 f2fs_remove_dirty_inode(inode);
Chao Yu933439c2016-10-11 22:57:01 +0800417 }
Jaegeuk Kim640cc182017-07-19 10:59:55 -0700418retry:
Chao Yu28bc1062016-02-06 14:40:34 +0800419 fio.page = page;
Hou Pengyange959c8f2017-04-25 12:45:13 +0000420 fio.old_blkaddr = NULL_ADDR;
Jaegeuk Kim4d978072017-04-26 11:11:12 -0700421 fio.encrypted_page = NULL;
Jaegeuk Kimcc156202017-05-12 13:51:34 -0700422 fio.need_lock = LOCK_DONE;
Chao Yu4d57b862018-05-30 00:20:41 +0800423 err = f2fs_do_write_data_page(&fio);
Chao Yu29b96b52016-02-06 14:38:29 +0800424 if (err) {
Jaegeuk Kim640cc182017-07-19 10:59:55 -0700425 if (err == -ENOMEM) {
Chao Yu5df7731f2020-02-17 17:45:44 +0800426 congestion_wait(BLK_RW_ASYNC,
427 DEFAULT_IO_TIMEOUT);
Jaegeuk Kim640cc182017-07-19 10:59:55 -0700428 cond_resched();
429 goto retry;
430 }
Chao Yu28bc1062016-02-06 14:40:34 +0800431 unlock_page(page);
Chao Yu29b96b52016-02-06 14:38:29 +0800432 break;
433 }
Chao Yu28bc1062016-02-06 14:40:34 +0800434 /* record old blkaddr for revoking */
435 cur->old_addr = fio.old_blkaddr;
Chao Yubab475c2018-09-27 23:41:16 +0800436 submit_bio = true;
Chao Yu29b96b52016-02-06 14:38:29 +0800437 }
Chao Yu28bc1062016-02-06 14:40:34 +0800438 unlock_page(page);
Chao Yucf52b272018-04-23 10:36:14 +0800439 list_move_tail(&cur->list, &revoke_list);
Chao Yu29b96b52016-02-06 14:38:29 +0800440 }
441
Chao Yubab475c2018-09-27 23:41:16 +0800442 if (submit_bio)
443 f2fs_submit_merged_write_cond(sbi, inode, NULL, 0, DATA);
Chao Yu28bc1062016-02-06 14:40:34 +0800444
Chao Yu28bc1062016-02-06 14:40:34 +0800445 if (err) {
Chao Yu28bc1062016-02-06 14:40:34 +0800446 /*
447 * try to revoke all committed pages, but still we could fail
448 * due to no memory or other reason, if that happened, EAGAIN
449 * will be returned, which means in such case, transaction is
450 * already not integrity, caller should use journal to do the
451 * recovery or rewrite & commit last transaction. For other
452 * error number, revoking was done by filesystem itself.
453 */
Chao Yu48432982019-02-25 17:11:03 +0800454 err = __revoke_inmem_pages(inode, &revoke_list,
455 false, true, false);
Chao Yu28bc1062016-02-06 14:40:34 +0800456
457 /* drop all uncommitted pages */
Chao Yu48432982019-02-25 17:11:03 +0800458 __revoke_inmem_pages(inode, &fi->inmem_pages,
459 true, false, false);
Chao Yucf52b272018-04-23 10:36:14 +0800460 } else {
Chao Yu48432982019-02-25 17:11:03 +0800461 __revoke_inmem_pages(inode, &revoke_list,
462 false, false, false);
Chao Yu28bc1062016-02-06 14:40:34 +0800463 }
Chao Yucf52b272018-04-23 10:36:14 +0800464
465 return err;
466}
467
Chao Yu4d57b862018-05-30 00:20:41 +0800468int f2fs_commit_inmem_pages(struct inode *inode)
Chao Yucf52b272018-04-23 10:36:14 +0800469{
470 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
471 struct f2fs_inode_info *fi = F2FS_I(inode);
472 int err;
473
474 f2fs_balance_fs(sbi, true);
Chao Yucf52b272018-04-23 10:36:14 +0800475
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +0900476 down_write(&fi->i_gc_rwsem[WRITE]);
477
478 f2fs_lock_op(sbi);
Chao Yucf52b272018-04-23 10:36:14 +0800479 set_inode_flag(inode, FI_ATOMIC_COMMIT);
480
481 mutex_lock(&fi->inmem_lock);
Chao Yu4d57b862018-05-30 00:20:41 +0800482 err = __f2fs_commit_inmem_pages(inode);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700483 mutex_unlock(&fi->inmem_lock);
484
Chao Yu5fe45742017-01-07 18:50:26 +0800485 clear_inode_flag(inode, FI_ATOMIC_COMMIT);
486
Chao Yu29b96b52016-02-06 14:38:29 +0800487 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +0900488 up_write(&fi->i_gc_rwsem[WRITE]);
489
Jaegeuk Kimedb27de2015-07-25 00:52:52 -0700490 return err;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700491}
492
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900493/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900494 * This function balances dirty node and dentry pages.
495 * In addition, it controls garbage collection.
496 */
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800497void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900498{
Chao Yu55523512017-02-25 11:08:28 +0800499 if (time_to_inject(sbi, FAULT_CHECKPOINT)) {
Chao Yuc45d6002019-11-01 17:53:23 +0800500 f2fs_show_injection_info(sbi, FAULT_CHECKPOINT);
Chao Yu0f348022016-09-26 19:45:55 +0800501 f2fs_stop_checkpoint(sbi, false);
Chao Yu55523512017-02-25 11:08:28 +0800502 }
Chao Yu0f348022016-09-26 19:45:55 +0800503
Jaegeuk Kime589c2c2016-06-02 15:24:24 -0700504 /* balance_fs_bg is able to be pending */
Jaegeuk Kima7881892017-04-20 13:51:57 -0700505 if (need && excess_cached_nats(sbi))
Chao Yu7bcd0cf2020-03-19 19:57:58 +0800506 f2fs_balance_fs_bg(sbi, false);
Jaegeuk Kime589c2c2016-06-02 15:24:24 -0700507
Chao Yu00e09c02019-08-23 17:58:36 +0800508 if (!f2fs_is_checkpoint_ready(sbi))
Daniel Rosenberg43549942018-08-20 19:21:43 -0700509 return;
510
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900511 /*
Jaegeuk Kim029cd282012-12-21 17:20:21 +0900512 * We should do GC or end up with checkpoint, if there are so many dirty
513 * dir/node pages without enough free segments.
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900514 */
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -0700515 if (has_not_enough_free_secs(sbi, 0, 0)) {
Chao Yu5911d2d2021-03-27 17:57:06 +0800516 if (test_opt(sbi, GC_MERGE) && sbi->gc_thread &&
517 sbi->gc_thread->f2fs_gc_task) {
518 DEFINE_WAIT(wait);
519
520 prepare_to_wait(&sbi->gc_thread->fggc_wq, &wait,
521 TASK_UNINTERRUPTIBLE);
522 wake_up(&sbi->gc_thread->gc_wait_queue_head);
523 io_schedule();
524 finish_wait(&sbi->gc_thread->fggc_wq, &wait);
525 } else {
526 down_write(&sbi->gc_lock);
527 f2fs_gc(sbi, false, false, false, NULL_SEGNO);
528 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900529 }
530}
531
Chao Yu287b1402021-09-16 17:09:03 +0800532static inline bool excess_dirty_threshold(struct f2fs_sb_info *sbi)
533{
534 int factor = rwsem_is_locked(&sbi->cp_rwsem) ? 3 : 2;
535 unsigned int dents = get_pages(sbi, F2FS_DIRTY_DENTS);
536 unsigned int qdata = get_pages(sbi, F2FS_DIRTY_QDATA);
537 unsigned int nodes = get_pages(sbi, F2FS_DIRTY_NODES);
538 unsigned int meta = get_pages(sbi, F2FS_DIRTY_META);
539 unsigned int imeta = get_pages(sbi, F2FS_DIRTY_IMETA);
540 unsigned int threshold = sbi->blocks_per_seg * factor *
541 DEFAULT_DIRTY_THRESHOLD;
542 unsigned int global_threshold = threshold * 3 / 2;
543
544 if (dents >= threshold || qdata >= threshold ||
545 nodes >= threshold || meta >= threshold ||
546 imeta >= threshold)
547 return true;
548 return dents + qdata + nodes + meta + imeta > global_threshold;
549}
550
Chao Yu7bcd0cf2020-03-19 19:57:58 +0800551void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi, bool from_bg)
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900552{
Chao Yu64c74a72018-05-26 18:03:34 +0800553 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
554 return;
555
Chao Yu1dcc3362015-02-05 17:57:31 +0800556 /* try to shrink extent cache when there is no enough memory */
Chao Yu4d57b862018-05-30 00:20:41 +0800557 if (!f2fs_available_free_memory(sbi, EXTENT_CACHE))
Jaegeuk Kim554df792015-06-19 13:41:23 -0700558 f2fs_shrink_extent_tree(sbi, EXTENT_CACHE_SHRINK_NUMBER);
Chao Yu1dcc3362015-02-05 17:57:31 +0800559
Jaegeuk Kim1b38dc82015-06-19 15:36:07 -0700560 /* check the # of cached NAT entries */
Chao Yu4d57b862018-05-30 00:20:41 +0800561 if (!f2fs_available_free_memory(sbi, NAT_ENTRIES))
562 f2fs_try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK);
Jaegeuk Kim1b38dc82015-06-19 15:36:07 -0700563
Chao Yu4d57b862018-05-30 00:20:41 +0800564 if (!f2fs_available_free_memory(sbi, FREE_NIDS))
565 f2fs_try_to_free_nids(sbi, MAX_FREE_NIDS);
Jaegeuk Kimad4edb82016-06-16 16:41:49 -0700566 else
Chao Yu4d57b862018-05-30 00:20:41 +0800567 f2fs_build_free_nids(sbi, false, false);
Chao Yu31696582015-07-28 18:33:46 +0800568
Chao Yu287b1402021-09-16 17:09:03 +0800569 if (excess_dirty_nats(sbi) || excess_dirty_threshold(sbi) ||
570 excess_prefree_segs(sbi) || !f2fs_space_for_roll_forward(sbi))
Chao Yu493720a2020-11-25 10:57:36 +0800571 goto do_sync;
572
573 /* there is background inflight IO or foreground operation recently */
574 if (is_inflight_io(sbi, REQ_TIME) ||
575 (!f2fs_time_over(sbi, REQ_TIME) && rwsem_is_locked(&sbi->cp_rwsem)))
Jaegeuk Kimf455c8a2016-12-05 11:37:14 -0800576 return;
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -0800577
Chao Yu493720a2020-11-25 10:57:36 +0800578 /* exceed periodical checkpoint timeout threshold */
579 if (f2fs_time_over(sbi, CP_TIME))
580 goto do_sync;
581
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900582 /* checkpoint is the only way to shrink partial cached entries */
Chao Yucd6d697a62021-09-29 03:19:14 +0800583 if (f2fs_available_free_memory(sbi, NAT_ENTRIES) &&
Chao Yu493720a2020-11-25 10:57:36 +0800584 f2fs_available_free_memory(sbi, INO_ENTRIES))
585 return;
Chao Yue9f5b8b2016-02-14 18:54:33 +0800586
Chao Yu493720a2020-11-25 10:57:36 +0800587do_sync:
588 if (test_opt(sbi, DATA_FLUSH) && from_bg) {
589 struct blk_plug plug;
Chao Yu040d2bb2019-05-20 17:36:59 +0800590
Chao Yu493720a2020-11-25 10:57:36 +0800591 mutex_lock(&sbi->flush_lock);
Chao Yu040d2bb2019-05-20 17:36:59 +0800592
Chao Yu493720a2020-11-25 10:57:36 +0800593 blk_start_plug(&plug);
594 f2fs_sync_dirty_inodes(sbi, FILE_INODE);
595 blk_finish_plug(&plug);
596
597 mutex_unlock(&sbi->flush_lock);
Chao Yu36b35a02015-12-17 17:13:28 +0800598 }
Chao Yu493720a2020-11-25 10:57:36 +0800599 f2fs_sync_fs(sbi->sb, true);
600 stat_inc_bg_cp_count(sbi->stat_info);
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900601}
602
Kinglong Mee20fda56b2017-03-04 22:13:10 +0800603static int __submit_flush_wait(struct f2fs_sb_info *sbi,
604 struct block_device *bdev)
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700605{
Christoph Hellwig25ac8422021-01-26 15:52:37 +0100606 int ret = blkdev_issue_flush(bdev);
Kinglong Mee20fda56b2017-03-04 22:13:10 +0800607
608 trace_f2fs_issue_flush(bdev, test_opt(sbi, NOBARRIER),
609 test_opt(sbi, FLUSH_MERGE), ret);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700610 return ret;
611}
612
Chao Yu39d787b2017-09-29 13:59:38 +0800613static int submit_flush_wait(struct f2fs_sb_info *sbi, nid_t ino)
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700614{
Chao Yu39d787b2017-09-29 13:59:38 +0800615 int ret = 0;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700616 int i;
617
Damien Le Moal09168782019-03-16 09:13:06 +0900618 if (!f2fs_is_multi_device(sbi))
Chao Yu39d787b2017-09-29 13:59:38 +0800619 return __submit_flush_wait(sbi, sbi->sb->s_bdev);
Kinglong Mee20fda56b2017-03-04 22:13:10 +0800620
Chao Yu39d787b2017-09-29 13:59:38 +0800621 for (i = 0; i < sbi->s_ndevs; i++) {
Chao Yu4d57b862018-05-30 00:20:41 +0800622 if (!f2fs_is_dirty_device(sbi, ino, i, FLUSH_INO))
Chao Yu39d787b2017-09-29 13:59:38 +0800623 continue;
Kinglong Mee20fda56b2017-03-04 22:13:10 +0800624 ret = __submit_flush_wait(sbi, FDEV(i).bdev);
625 if (ret)
626 break;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700627 }
628 return ret;
629}
630
Gu Zheng2163d192014-04-27 14:21:33 +0800631static int issue_flush_thread(void *data)
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900632{
633 struct f2fs_sb_info *sbi = data;
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800634 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800635 wait_queue_head_t *q = &fcc->flush_wait_queue;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900636repeat:
637 if (kthread_should_stop())
638 return 0;
639
Gu Zheng721bd4d2014-09-05 18:31:00 +0800640 if (!llist_empty(&fcc->issue_list)) {
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900641 struct flush_cmd *cmd, *next;
642 int ret;
643
Gu Zheng721bd4d2014-09-05 18:31:00 +0800644 fcc->dispatch_list = llist_del_all(&fcc->issue_list);
645 fcc->dispatch_list = llist_reverse_order(fcc->dispatch_list);
646
Chao Yu39d787b2017-09-29 13:59:38 +0800647 cmd = llist_entry(fcc->dispatch_list, struct flush_cmd, llnode);
648
649 ret = submit_flush_wait(sbi, cmd->ino);
Chao Yu8b8dd652017-03-25 17:19:58 +0800650 atomic_inc(&fcc->issued_flush);
651
Gu Zheng721bd4d2014-09-05 18:31:00 +0800652 llist_for_each_entry_safe(cmd, next,
653 fcc->dispatch_list, llnode) {
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900654 cmd->ret = ret;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900655 complete(&cmd->wait);
656 }
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800657 fcc->dispatch_list = NULL;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900658 }
659
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800660 wait_event_interruptible(*q,
Gu Zheng721bd4d2014-09-05 18:31:00 +0800661 kthread_should_stop() || !llist_empty(&fcc->issue_list));
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900662 goto repeat;
663}
664
Chao Yu39d787b2017-09-29 13:59:38 +0800665int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino)
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900666{
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800667 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
Chao Yuadf8d902014-05-08 17:00:35 +0800668 struct flush_cmd cmd;
Chao Yu8b8dd652017-03-25 17:19:58 +0800669 int ret;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900670
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700671 if (test_opt(sbi, NOBARRIER))
672 return 0;
673
Chao Yu8b8dd652017-03-25 17:19:58 +0800674 if (!test_opt(sbi, FLUSH_MERGE)) {
Jaegeuk Kim72691af2018-12-13 16:53:57 -0800675 atomic_inc(&fcc->queued_flush);
Chao Yu39d787b2017-09-29 13:59:38 +0800676 ret = submit_flush_wait(sbi, ino);
Jaegeuk Kim72691af2018-12-13 16:53:57 -0800677 atomic_dec(&fcc->queued_flush);
Chao Yu8b8dd652017-03-25 17:19:58 +0800678 atomic_inc(&fcc->issued_flush);
679 return ret;
680 }
681
Damien Le Moal09168782019-03-16 09:13:06 +0900682 if (atomic_inc_return(&fcc->queued_flush) == 1 ||
683 f2fs_is_multi_device(sbi)) {
Chao Yu39d787b2017-09-29 13:59:38 +0800684 ret = submit_flush_wait(sbi, ino);
Jaegeuk Kim72691af2018-12-13 16:53:57 -0800685 atomic_dec(&fcc->queued_flush);
Chao Yu8b8dd652017-03-25 17:19:58 +0800686
687 atomic_inc(&fcc->issued_flush);
Jaegeuk Kim740432f2015-08-14 11:43:56 -0700688 return ret;
689 }
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900690
Chao Yu39d787b2017-09-29 13:59:38 +0800691 cmd.ino = ino;
Chao Yuadf8d902014-05-08 17:00:35 +0800692 init_completion(&cmd.wait);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900693
Gu Zheng721bd4d2014-09-05 18:31:00 +0800694 llist_add(&cmd.llnode, &fcc->issue_list);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900695
Chao Yu3b42c742021-02-20 17:38:43 +0800696 /*
697 * update issue_list before we wake up issue_flush thread, this
698 * smp_mb() pairs with another barrier in ___wait_event(), see
699 * more details in comments of waitqueue_active().
700 */
Chao Yu6f890df2017-08-21 22:53:45 +0800701 smp_mb();
702
703 if (waitqueue_active(&fcc->flush_wait_queue))
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800704 wake_up(&fcc->flush_wait_queue);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900705
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800706 if (fcc->f2fs_issue_flush) {
707 wait_for_completion(&cmd.wait);
Jaegeuk Kim72691af2018-12-13 16:53:57 -0800708 atomic_dec(&fcc->queued_flush);
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800709 } else {
Chao Yud3238692017-08-31 18:56:06 +0800710 struct llist_node *list;
711
712 list = llist_del_all(&fcc->issue_list);
713 if (!list) {
714 wait_for_completion(&cmd.wait);
Jaegeuk Kim72691af2018-12-13 16:53:57 -0800715 atomic_dec(&fcc->queued_flush);
Chao Yud3238692017-08-31 18:56:06 +0800716 } else {
717 struct flush_cmd *tmp, *next;
718
Chao Yu39d787b2017-09-29 13:59:38 +0800719 ret = submit_flush_wait(sbi, ino);
Chao Yud3238692017-08-31 18:56:06 +0800720
721 llist_for_each_entry_safe(tmp, next, list, llnode) {
722 if (tmp == &cmd) {
723 cmd.ret = ret;
Jaegeuk Kim72691af2018-12-13 16:53:57 -0800724 atomic_dec(&fcc->queued_flush);
Chao Yud3238692017-08-31 18:56:06 +0800725 continue;
726 }
727 tmp->ret = ret;
728 complete(&tmp->wait);
729 }
730 }
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800731 }
Chao Yuadf8d902014-05-08 17:00:35 +0800732
733 return cmd.ret;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900734}
735
Chao Yu4d57b862018-05-30 00:20:41 +0800736int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi)
Gu Zheng2163d192014-04-27 14:21:33 +0800737{
738 dev_t dev = sbi->sb->s_bdev->bd_dev;
739 struct flush_cmd_control *fcc;
740 int err = 0;
741
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800742 if (SM_I(sbi)->fcc_info) {
743 fcc = SM_I(sbi)->fcc_info;
Yunlong Songd871cd02017-06-24 15:57:19 +0800744 if (fcc->f2fs_issue_flush)
745 return err;
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800746 goto init_thread;
747 }
748
Chao Yuacbf0542017-11-30 19:28:17 +0800749 fcc = f2fs_kzalloc(sbi, sizeof(struct flush_cmd_control), GFP_KERNEL);
Gu Zheng2163d192014-04-27 14:21:33 +0800750 if (!fcc)
751 return -ENOMEM;
Chao Yu8b8dd652017-03-25 17:19:58 +0800752 atomic_set(&fcc->issued_flush, 0);
Jaegeuk Kim72691af2018-12-13 16:53:57 -0800753 atomic_set(&fcc->queued_flush, 0);
Gu Zheng2163d192014-04-27 14:21:33 +0800754 init_waitqueue_head(&fcc->flush_wait_queue);
Gu Zheng721bd4d2014-09-05 18:31:00 +0800755 init_llist_head(&fcc->issue_list);
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800756 SM_I(sbi)->fcc_info = fcc;
Yunlei Hed4fdf8b2017-06-01 16:43:51 +0800757 if (!test_opt(sbi, FLUSH_MERGE))
758 return err;
759
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800760init_thread:
Gu Zheng2163d192014-04-27 14:21:33 +0800761 fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi,
762 "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev));
763 if (IS_ERR(fcc->f2fs_issue_flush)) {
764 err = PTR_ERR(fcc->f2fs_issue_flush);
Chao Yuc8eb7022020-09-14 16:47:00 +0800765 kfree(fcc);
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800766 SM_I(sbi)->fcc_info = NULL;
Gu Zheng2163d192014-04-27 14:21:33 +0800767 return err;
768 }
Gu Zheng2163d192014-04-27 14:21:33 +0800769
770 return err;
771}
772
Chao Yu4d57b862018-05-30 00:20:41 +0800773void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free)
Gu Zheng2163d192014-04-27 14:21:33 +0800774{
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800775 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
Gu Zheng2163d192014-04-27 14:21:33 +0800776
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800777 if (fcc && fcc->f2fs_issue_flush) {
778 struct task_struct *flush_thread = fcc->f2fs_issue_flush;
779
780 fcc->f2fs_issue_flush = NULL;
781 kthread_stop(flush_thread);
782 }
783 if (free) {
Chao Yuc8eb7022020-09-14 16:47:00 +0800784 kfree(fcc);
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800785 SM_I(sbi)->fcc_info = NULL;
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800786 }
Gu Zheng2163d192014-04-27 14:21:33 +0800787}
788
Chao Yu1228b482017-09-29 13:59:39 +0800789int f2fs_flush_device_cache(struct f2fs_sb_info *sbi)
790{
791 int ret = 0, i;
792
Damien Le Moal09168782019-03-16 09:13:06 +0900793 if (!f2fs_is_multi_device(sbi))
Chao Yu1228b482017-09-29 13:59:39 +0800794 return 0;
795
Chao Yu6ed29fe2020-10-12 10:28:14 +0800796 if (test_opt(sbi, NOBARRIER))
797 return 0;
798
Chao Yu1228b482017-09-29 13:59:39 +0800799 for (i = 1; i < sbi->s_ndevs; i++) {
Chao Yu91803392021-08-04 08:38:38 +0800800 int count = DEFAULT_RETRY_IO_COUNT;
801
Chao Yu1228b482017-09-29 13:59:39 +0800802 if (!f2fs_test_bit(i, (char *)&sbi->dirty_device))
803 continue;
Chao Yu91803392021-08-04 08:38:38 +0800804
805 do {
806 ret = __submit_flush_wait(sbi, FDEV(i).bdev);
807 if (ret)
808 congestion_wait(BLK_RW_ASYNC,
809 DEFAULT_IO_TIMEOUT);
810 } while (ret && --count);
811
812 if (ret) {
813 f2fs_stop_checkpoint(sbi, false);
Chao Yu1228b482017-09-29 13:59:39 +0800814 break;
Chao Yu91803392021-08-04 08:38:38 +0800815 }
Chao Yu1228b482017-09-29 13:59:39 +0800816
817 spin_lock(&sbi->dev_lock);
818 f2fs_clear_bit(i, (char *)&sbi->dirty_device);
819 spin_unlock(&sbi->dev_lock);
820 }
821
822 return ret;
823}
824
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900825static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
826 enum dirty_type dirty_type)
827{
828 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
829
830 /* need not be added */
831 if (IS_CURSEG(sbi, segno))
832 return;
833
834 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
835 dirty_i->nr_dirty[dirty_type]++;
836
837 if (dirty_type == DIRTY) {
838 struct seg_entry *sentry = get_seg_entry(sbi, segno);
Changman Lee4625d6a2013-10-25 17:31:57 +0900839 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900840
Jaegeuk Kimec325b52014-09-02 16:24:11 -0700841 if (unlikely(t >= DIRTY)) {
842 f2fs_bug_on(sbi, 1);
843 return;
844 }
Changman Lee4625d6a2013-10-25 17:31:57 +0900845 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
846 dirty_i->nr_dirty[t]++;
Jack Qiuda52f8a2020-06-18 12:37:10 +0800847
848 if (__is_large_section(sbi)) {
849 unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
Shin'ichiro Kawasaki123aaf72020-08-19 10:34:48 +0900850 block_t valid_blocks =
Jack Qiuda52f8a2020-06-18 12:37:10 +0800851 get_valid_blocks(sbi, segno, true);
852
853 f2fs_bug_on(sbi, unlikely(!valid_blocks ||
854 valid_blocks == BLKS_PER_SEC(sbi)));
855
856 if (!IS_CURSEC(sbi, secno))
857 set_bit(secno, dirty_i->dirty_secmap);
858 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900859 }
860}
861
862static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
863 enum dirty_type dirty_type)
864{
865 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Shin'ichiro Kawasaki123aaf72020-08-19 10:34:48 +0900866 block_t valid_blocks;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900867
868 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
869 dirty_i->nr_dirty[dirty_type]--;
870
871 if (dirty_type == DIRTY) {
Changman Lee4625d6a2013-10-25 17:31:57 +0900872 struct seg_entry *sentry = get_seg_entry(sbi, segno);
873 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900874
Changman Lee4625d6a2013-10-25 17:31:57 +0900875 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
876 dirty_i->nr_dirty[t]--;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900877
Jack Qiuda52f8a2020-06-18 12:37:10 +0800878 valid_blocks = get_valid_blocks(sbi, segno, true);
879 if (valid_blocks == 0) {
Jaegeuk Kim4ddb1a42017-04-07 15:08:17 -0700880 clear_bit(GET_SEC_FROM_SEG(sbi, segno),
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +0900881 dirty_i->victim_secmap);
Sahitya Tummalabbf9f7d2019-08-07 19:10:32 +0530882#ifdef CONFIG_F2FS_CHECK_FS
883 clear_bit(segno, SIT_I(sbi)->invalid_segmap);
884#endif
885 }
Jack Qiuda52f8a2020-06-18 12:37:10 +0800886 if (__is_large_section(sbi)) {
887 unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
888
889 if (!valid_blocks ||
890 valid_blocks == BLKS_PER_SEC(sbi)) {
891 clear_bit(secno, dirty_i->dirty_secmap);
892 return;
893 }
894
895 if (!IS_CURSEC(sbi, secno))
896 set_bit(secno, dirty_i->dirty_secmap);
897 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900898 }
899}
900
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900901/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900902 * Should not occur error such as -ENOMEM.
903 * Adding dirty entry into seglist is not critical operation.
904 * If a given segment is one of current working segments, it won't be added.
905 */
Haicheng Li8d8451a2013-06-13 16:59:28 +0800906static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900907{
908 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Daniel Rosenberg43549942018-08-20 19:21:43 -0700909 unsigned short valid_blocks, ckpt_valid_blocks;
Aravind Rameshde881df2020-07-16 18:26:56 +0530910 unsigned int usable_blocks;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900911
912 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
913 return;
914
Aravind Rameshde881df2020-07-16 18:26:56 +0530915 usable_blocks = f2fs_usable_blks_in_seg(sbi, segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900916 mutex_lock(&dirty_i->seglist_lock);
917
Jaegeuk Kim302bd342017-04-07 14:33:22 -0700918 valid_blocks = get_valid_blocks(sbi, segno, false);
Chao Yu61461fc2021-03-24 11:18:28 +0800919 ckpt_valid_blocks = get_ckpt_valid_blocks(sbi, segno, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900920
Daniel Rosenberg43549942018-08-20 19:21:43 -0700921 if (valid_blocks == 0 && (!is_sbi_flag_set(sbi, SBI_CP_DISABLED) ||
Aravind Rameshde881df2020-07-16 18:26:56 +0530922 ckpt_valid_blocks == usable_blocks)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900923 __locate_dirty_segment(sbi, segno, PRE);
924 __remove_dirty_segment(sbi, segno, DIRTY);
Aravind Rameshde881df2020-07-16 18:26:56 +0530925 } else if (valid_blocks < usable_blocks) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900926 __locate_dirty_segment(sbi, segno, DIRTY);
927 } else {
928 /* Recovery routine with SSR needs this */
929 __remove_dirty_segment(sbi, segno, DIRTY);
930 }
931
932 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900933}
934
Daniel Rosenberg43549942018-08-20 19:21:43 -0700935/* This moves currently empty dirty blocks to prefree. Must hold seglist_lock */
936void f2fs_dirty_to_prefree(struct f2fs_sb_info *sbi)
937{
938 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
939 unsigned int segno;
940
941 mutex_lock(&dirty_i->seglist_lock);
942 for_each_set_bit(segno, dirty_i->dirty_segmap[DIRTY], MAIN_SEGS(sbi)) {
943 if (get_valid_blocks(sbi, segno, false))
944 continue;
945 if (IS_CURSEG(sbi, segno))
946 continue;
947 __locate_dirty_segment(sbi, segno, PRE);
948 __remove_dirty_segment(sbi, segno, DIRTY);
949 }
950 mutex_unlock(&dirty_i->seglist_lock);
951}
952
Daniel Rosenberg4d3aed72019-05-29 17:49:06 -0700953block_t f2fs_get_unusable_blocks(struct f2fs_sb_info *sbi)
Daniel Rosenberg43549942018-08-20 19:21:43 -0700954{
Daniel Rosenbergae4ad7e2019-05-29 17:49:03 -0700955 int ovp_hole_segs =
956 (overprovision_segments(sbi) - reserved_segments(sbi));
957 block_t ovp_holes = ovp_hole_segs << sbi->log_blocks_per_seg;
Daniel Rosenberg4d3aed72019-05-29 17:49:06 -0700958 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Daniel Rosenberg43549942018-08-20 19:21:43 -0700959 block_t holes[2] = {0, 0}; /* DATA and NODE */
Daniel Rosenberg4d3aed72019-05-29 17:49:06 -0700960 block_t unusable;
Daniel Rosenberg43549942018-08-20 19:21:43 -0700961 struct seg_entry *se;
962 unsigned int segno;
963
964 mutex_lock(&dirty_i->seglist_lock);
965 for_each_set_bit(segno, dirty_i->dirty_segmap[DIRTY], MAIN_SEGS(sbi)) {
966 se = get_seg_entry(sbi, segno);
967 if (IS_NODESEG(se->type))
Aravind Rameshde881df2020-07-16 18:26:56 +0530968 holes[NODE] += f2fs_usable_blks_in_seg(sbi, segno) -
969 se->valid_blocks;
Daniel Rosenberg43549942018-08-20 19:21:43 -0700970 else
Aravind Rameshde881df2020-07-16 18:26:56 +0530971 holes[DATA] += f2fs_usable_blks_in_seg(sbi, segno) -
972 se->valid_blocks;
Daniel Rosenberg43549942018-08-20 19:21:43 -0700973 }
974 mutex_unlock(&dirty_i->seglist_lock);
975
Daniel Rosenberg4d3aed72019-05-29 17:49:06 -0700976 unusable = holes[DATA] > holes[NODE] ? holes[DATA] : holes[NODE];
977 if (unusable > ovp_holes)
978 return unusable - ovp_holes;
979 return 0;
980}
981
982int f2fs_disable_cp_again(struct f2fs_sb_info *sbi, block_t unusable)
983{
984 int ovp_hole_segs =
985 (overprovision_segments(sbi) - reserved_segments(sbi));
986 if (unusable > F2FS_OPTION(sbi).unusable_cap)
Daniel Rosenberg43549942018-08-20 19:21:43 -0700987 return -EAGAIN;
Jaegeuk Kimdb610a62019-01-24 17:48:38 -0800988 if (is_sbi_flag_set(sbi, SBI_CP_DISABLED_QUICK) &&
Daniel Rosenbergae4ad7e2019-05-29 17:49:03 -0700989 dirty_segments(sbi) > ovp_hole_segs)
Jaegeuk Kimdb610a62019-01-24 17:48:38 -0800990 return -EAGAIN;
Daniel Rosenberg43549942018-08-20 19:21:43 -0700991 return 0;
992}
993
994/* This is only used by SBI_CP_DISABLED */
995static unsigned int get_free_segment(struct f2fs_sb_info *sbi)
996{
997 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
998 unsigned int segno = 0;
999
1000 mutex_lock(&dirty_i->seglist_lock);
1001 for_each_set_bit(segno, dirty_i->dirty_segmap[DIRTY], MAIN_SEGS(sbi)) {
1002 if (get_valid_blocks(sbi, segno, false))
1003 continue;
Chao Yu61461fc2021-03-24 11:18:28 +08001004 if (get_ckpt_valid_blocks(sbi, segno, false))
Daniel Rosenberg43549942018-08-20 19:21:43 -07001005 continue;
1006 mutex_unlock(&dirty_i->seglist_lock);
1007 return segno;
1008 }
1009 mutex_unlock(&dirty_i->seglist_lock);
1010 return NULL_SEGNO;
1011}
1012
Chao Yu004b6862017-04-14 23:24:55 +08001013static struct discard_cmd *__create_discard_cmd(struct f2fs_sb_info *sbi,
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001014 struct block_device *bdev, block_t lstart,
1015 block_t start, block_t len)
Chao Yu275b66b2016-08-29 23:58:34 +08001016{
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001017 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Chao Yuba48a332017-04-15 14:09:37 +08001018 struct list_head *pend_list;
Jaegeuk Kimb01a9202017-01-09 14:13:03 -08001019 struct discard_cmd *dc;
Chao Yu275b66b2016-08-29 23:58:34 +08001020
Chao Yuba48a332017-04-15 14:09:37 +08001021 f2fs_bug_on(sbi, !len);
1022
1023 pend_list = &dcc->pend_list[plist_idx(len)];
1024
Chao Yu32410572021-08-09 08:24:48 +08001025 dc = f2fs_kmem_cache_alloc(discard_cmd_slab, GFP_NOFS, true, NULL);
Jaegeuk Kimb01a9202017-01-09 14:13:03 -08001026 INIT_LIST_HEAD(&dc->list);
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001027 dc->bdev = bdev;
Jaegeuk Kimb01a9202017-01-09 14:13:03 -08001028 dc->lstart = lstart;
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001029 dc->start = start;
Jaegeuk Kimb01a9202017-01-09 14:13:03 -08001030 dc->len = len;
Chao Yuec9895a2017-04-26 17:39:54 +08001031 dc->ref = 0;
Jaegeuk Kim15469962017-01-09 20:32:07 -08001032 dc->state = D_PREP;
Jaegeuk Kim72691af2018-12-13 16:53:57 -08001033 dc->queued = 0;
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001034 dc->error = 0;
Jaegeuk Kimb01a9202017-01-09 14:13:03 -08001035 init_completion(&dc->wait);
Chao Yu22d375d2017-04-05 18:19:48 +08001036 list_add_tail(&dc->list, pend_list);
Chao Yu35ec7d52018-08-06 22:43:50 +08001037 spin_lock_init(&dc->lock);
1038 dc->bio_ref = 0;
Chao Yu5f323662017-03-25 17:19:59 +08001039 atomic_inc(&dcc->discard_cmd_cnt);
Chao Yud84d1cb2017-04-18 19:27:39 +08001040 dcc->undiscard_blks += len;
Chao Yu004b6862017-04-14 23:24:55 +08001041
1042 return dc;
Jaegeuk Kim15469962017-01-09 20:32:07 -08001043}
1044
Chao Yu004b6862017-04-14 23:24:55 +08001045static struct discard_cmd *__attach_discard_cmd(struct f2fs_sb_info *sbi,
1046 struct block_device *bdev, block_t lstart,
1047 block_t start, block_t len,
Chao Yu4dada3f2018-10-04 11:18:30 +08001048 struct rb_node *parent, struct rb_node **p,
1049 bool leftmost)
Chao Yu004b6862017-04-14 23:24:55 +08001050{
1051 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1052 struct discard_cmd *dc;
1053
1054 dc = __create_discard_cmd(sbi, bdev, lstart, start, len);
1055
1056 rb_link_node(&dc->rb_node, parent, p);
Chao Yu4dada3f2018-10-04 11:18:30 +08001057 rb_insert_color_cached(&dc->rb_node, &dcc->root, leftmost);
Chao Yu004b6862017-04-14 23:24:55 +08001058
1059 return dc;
1060}
1061
1062static void __detach_discard_cmd(struct discard_cmd_control *dcc,
1063 struct discard_cmd *dc)
Jaegeuk Kim15469962017-01-09 20:32:07 -08001064{
Jaegeuk Kimdcc91652017-01-11 10:20:04 -08001065 if (dc->state == D_DONE)
Jaegeuk Kim72691af2018-12-13 16:53:57 -08001066 atomic_sub(dc->queued, &dcc->queued_discard);
Chao Yu004b6862017-04-14 23:24:55 +08001067
1068 list_del(&dc->list);
Chao Yu4dada3f2018-10-04 11:18:30 +08001069 rb_erase_cached(&dc->rb_node, &dcc->root);
Chao Yud84d1cb2017-04-18 19:27:39 +08001070 dcc->undiscard_blks -= dc->len;
Chao Yu004b6862017-04-14 23:24:55 +08001071
1072 kmem_cache_free(discard_cmd_slab, dc);
1073
1074 atomic_dec(&dcc->discard_cmd_cnt);
1075}
1076
1077static void __remove_discard_cmd(struct f2fs_sb_info *sbi,
1078 struct discard_cmd *dc)
1079{
1080 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Chao Yu35ec7d52018-08-06 22:43:50 +08001081 unsigned long flags;
Jaegeuk Kimdcc91652017-01-11 10:20:04 -08001082
Chao Yu2ec6f2e2017-10-04 09:08:36 +08001083 trace_f2fs_remove_discard(dc->bdev, dc->start, dc->len);
1084
Chao Yu35ec7d52018-08-06 22:43:50 +08001085 spin_lock_irqsave(&dc->lock, flags);
1086 if (dc->bio_ref) {
1087 spin_unlock_irqrestore(&dc->lock, flags);
1088 return;
1089 }
1090 spin_unlock_irqrestore(&dc->lock, flags);
1091
Chao Yud9703d92017-06-05 18:29:07 +08001092 f2fs_bug_on(sbi, dc->ref);
1093
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001094 if (dc->error == -EOPNOTSUPP)
1095 dc->error = 0;
Jaegeuk Kim15469962017-01-09 20:32:07 -08001096
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001097 if (dc->error)
Chao Yu22d7ea12018-08-22 17:17:47 +08001098 printk_ratelimited(
Chao Yuc45d6002019-11-01 17:53:23 +08001099 "%sF2FS-fs (%s): Issue discard(%u, %u, %u) failed, ret: %d",
1100 KERN_INFO, sbi->sb->s_id,
1101 dc->lstart, dc->start, dc->len, dc->error);
Chao Yu004b6862017-04-14 23:24:55 +08001102 __detach_discard_cmd(dcc, dc);
Chao Yu275b66b2016-08-29 23:58:34 +08001103}
1104
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001105static void f2fs_submit_discard_endio(struct bio *bio)
1106{
1107 struct discard_cmd *dc = (struct discard_cmd *)bio->bi_private;
Chao Yu35ec7d52018-08-06 22:43:50 +08001108 unsigned long flags;
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001109
Chao Yu35ec7d52018-08-06 22:43:50 +08001110 spin_lock_irqsave(&dc->lock, flags);
Sahitya Tummala3fa6a8c2020-04-15 09:35:54 +05301111 if (!dc->error)
1112 dc->error = blk_status_to_errno(bio->bi_status);
Chao Yu35ec7d52018-08-06 22:43:50 +08001113 dc->bio_ref--;
1114 if (!dc->bio_ref && dc->state == D_SUBMIT) {
1115 dc->state = D_DONE;
1116 complete_all(&dc->wait);
1117 }
1118 spin_unlock_irqrestore(&dc->lock, flags);
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001119 bio_put(bio);
1120}
1121
Wei Yongjun94b1e102018-01-05 09:41:20 +00001122static void __check_sit_bitmap(struct f2fs_sb_info *sbi,
Chao Yu6915ea92017-06-30 17:19:02 +08001123 block_t start, block_t end)
1124{
1125#ifdef CONFIG_F2FS_CHECK_FS
1126 struct seg_entry *sentry;
1127 unsigned int segno;
1128 block_t blk = start;
1129 unsigned long offset, size, max_blocks = sbi->blocks_per_seg;
1130 unsigned long *map;
1131
1132 while (blk < end) {
1133 segno = GET_SEGNO(sbi, blk);
1134 sentry = get_seg_entry(sbi, segno);
1135 offset = GET_BLKOFF_FROM_SEG0(sbi, blk);
1136
Yunlong Song008396e2017-08-04 17:07:15 +08001137 if (end < START_BLOCK(sbi, segno + 1))
1138 size = GET_BLKOFF_FROM_SEG0(sbi, end);
1139 else
1140 size = max_blocks;
Chao Yu6915ea92017-06-30 17:19:02 +08001141 map = (unsigned long *)(sentry->cur_valid_map);
1142 offset = __find_rev_next_bit(map, size, offset);
1143 f2fs_bug_on(sbi, offset != size);
Yunlong Song008396e2017-08-04 17:07:15 +08001144 blk = START_BLOCK(sbi, segno + 1);
Chao Yu6915ea92017-06-30 17:19:02 +08001145 }
1146#endif
1147}
1148
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -07001149static void __init_discard_policy(struct f2fs_sb_info *sbi,
1150 struct discard_policy *dpolicy,
1151 int discard_type, unsigned int granularity)
1152{
Sahitya Tummalac35b8d52021-04-06 14:39:16 +05301153 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1154
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -07001155 /* common policy */
1156 dpolicy->type = discard_type;
1157 dpolicy->sync = true;
Chao Yu20ee4382018-07-08 22:11:01 +08001158 dpolicy->ordered = false;
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -07001159 dpolicy->granularity = granularity;
1160
1161 dpolicy->max_requests = DEF_MAX_DISCARD_REQUEST;
1162 dpolicy->io_aware_gran = MAX_PLIST_NUM;
Chao Yu6ce48b02020-03-26 17:43:56 +08001163 dpolicy->timeout = false;
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -07001164
1165 if (discard_type == DPOLICY_BG) {
1166 dpolicy->min_interval = DEF_MIN_DISCARD_ISSUE_TIME;
Yunlei Hef9d1dce2018-04-08 15:11:11 +08001167 dpolicy->mid_interval = DEF_MID_DISCARD_ISSUE_TIME;
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -07001168 dpolicy->max_interval = DEF_MAX_DISCARD_ISSUE_TIME;
1169 dpolicy->io_aware = true;
Chao Yucba60842018-04-10 15:43:09 +08001170 dpolicy->sync = false;
Chao Yu20ee4382018-07-08 22:11:01 +08001171 dpolicy->ordered = true;
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -07001172 if (utilization(sbi) > DEF_DISCARD_URGENT_UTIL) {
1173 dpolicy->granularity = 1;
Sahitya Tummalac35b8d52021-04-06 14:39:16 +05301174 if (atomic_read(&dcc->discard_cmd_cnt))
1175 dpolicy->max_interval =
1176 DEF_MIN_DISCARD_ISSUE_TIME;
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -07001177 }
1178 } else if (discard_type == DPOLICY_FORCE) {
1179 dpolicy->min_interval = DEF_MIN_DISCARD_ISSUE_TIME;
Yunlei Hef9d1dce2018-04-08 15:11:11 +08001180 dpolicy->mid_interval = DEF_MID_DISCARD_ISSUE_TIME;
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -07001181 dpolicy->max_interval = DEF_MAX_DISCARD_ISSUE_TIME;
1182 dpolicy->io_aware = false;
1183 } else if (discard_type == DPOLICY_FSTRIM) {
1184 dpolicy->io_aware = false;
1185 } else if (discard_type == DPOLICY_UMOUNT) {
1186 dpolicy->io_aware = false;
Jaegeuk Kimb8623252019-01-25 09:12:13 -08001187 /* we need to issue all to keep CP_TRIMMED_FLAG */
1188 dpolicy->granularity = 1;
Chao Yu6ce48b02020-03-26 17:43:56 +08001189 dpolicy->timeout = true;
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -07001190 }
1191}
1192
Chao Yu35ec7d52018-08-06 22:43:50 +08001193static void __update_discard_tree_range(struct f2fs_sb_info *sbi,
1194 struct block_device *bdev, block_t lstart,
1195 block_t start, block_t len);
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001196/* this function is copied from blkdev_issue_discard from block/blk-lib.c */
Chao Yu6b9cb122018-08-08 10:14:55 +08001197static int __submit_discard_cmd(struct f2fs_sb_info *sbi,
Chao Yu78997b52017-10-04 09:08:34 +08001198 struct discard_policy *dpolicy,
Chao Yu35ec7d52018-08-06 22:43:50 +08001199 struct discard_cmd *dc,
1200 unsigned int *issued)
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001201{
Chao Yu35ec7d52018-08-06 22:43:50 +08001202 struct block_device *bdev = dc->bdev;
1203 struct request_queue *q = bdev_get_queue(bdev);
1204 unsigned int max_discard_blocks =
1205 SECTOR_TO_BLOCK(q->limits.max_discard_sectors);
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001206 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Chao Yu78997b52017-10-04 09:08:34 +08001207 struct list_head *wait_list = (dpolicy->type == DPOLICY_FSTRIM) ?
1208 &(dcc->fstrim_list) : &(dcc->wait_list);
Chao Yu78997b52017-10-04 09:08:34 +08001209 int flag = dpolicy->sync ? REQ_SYNC : 0;
Chao Yu35ec7d52018-08-06 22:43:50 +08001210 block_t lstart, start, len, total_len;
1211 int err = 0;
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001212
1213 if (dc->state != D_PREP)
Chao Yu6b9cb122018-08-08 10:14:55 +08001214 return 0;
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001215
Yunlei Hed6184772018-04-13 11:08:05 +08001216 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK))
Chao Yu6b9cb122018-08-08 10:14:55 +08001217 return 0;
Yunlei Hed6184772018-04-13 11:08:05 +08001218
Chao Yu35ec7d52018-08-06 22:43:50 +08001219 trace_f2fs_issue_discard(bdev, dc->start, dc->len);
Chao Yu0243a5f2017-04-15 14:09:38 +08001220
Chao Yu35ec7d52018-08-06 22:43:50 +08001221 lstart = dc->lstart;
1222 start = dc->start;
1223 len = dc->len;
1224 total_len = len;
1225
1226 dc->len = 0;
1227
1228 while (total_len && *issued < dpolicy->max_requests && !err) {
1229 struct bio *bio = NULL;
1230 unsigned long flags;
1231 bool last = true;
1232
1233 if (len > max_discard_blocks) {
1234 len = max_discard_blocks;
1235 last = false;
1236 }
1237
1238 (*issued)++;
1239 if (*issued == dpolicy->max_requests)
1240 last = true;
1241
1242 dc->len += len;
1243
Chao Yub83dcfe2018-08-06 20:30:18 +08001244 if (time_to_inject(sbi, FAULT_DISCARD)) {
Chao Yuc45d6002019-11-01 17:53:23 +08001245 f2fs_show_injection_info(sbi, FAULT_DISCARD);
Chao Yub83dcfe2018-08-06 20:30:18 +08001246 err = -EIO;
1247 goto submit;
1248 }
Chao Yu35ec7d52018-08-06 22:43:50 +08001249 err = __blkdev_issue_discard(bdev,
1250 SECTOR_FROM_BLOCK(start),
1251 SECTOR_FROM_BLOCK(len),
1252 GFP_NOFS, 0, &bio);
Chao Yub83dcfe2018-08-06 20:30:18 +08001253submit:
Chao Yu6b9cb122018-08-08 10:14:55 +08001254 if (err) {
Chao Yu35ec7d52018-08-06 22:43:50 +08001255 spin_lock_irqsave(&dc->lock, flags);
1256 if (dc->state == D_PARTIAL)
1257 dc->state = D_SUBMIT;
1258 spin_unlock_irqrestore(&dc->lock, flags);
1259
Chao Yu6b9cb122018-08-08 10:14:55 +08001260 break;
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001261 }
Chao Yu35ec7d52018-08-06 22:43:50 +08001262
Chao Yu6b9cb122018-08-08 10:14:55 +08001263 f2fs_bug_on(sbi, !bio);
1264
1265 /*
1266 * should keep before submission to avoid D_DONE
1267 * right away
1268 */
1269 spin_lock_irqsave(&dc->lock, flags);
1270 if (last)
1271 dc->state = D_SUBMIT;
1272 else
1273 dc->state = D_PARTIAL;
1274 dc->bio_ref++;
1275 spin_unlock_irqrestore(&dc->lock, flags);
1276
Jaegeuk Kim72691af2018-12-13 16:53:57 -08001277 atomic_inc(&dcc->queued_discard);
1278 dc->queued++;
Chao Yu6b9cb122018-08-08 10:14:55 +08001279 list_move_tail(&dc->list, wait_list);
1280
1281 /* sanity check on discard range */
Qiuyang Sun9249dde2018-12-18 17:32:23 +08001282 __check_sit_bitmap(sbi, lstart, lstart + len);
Chao Yu6b9cb122018-08-08 10:14:55 +08001283
1284 bio->bi_private = dc;
1285 bio->bi_end_io = f2fs_submit_discard_endio;
1286 bio->bi_opf |= flag;
1287 submit_bio(bio);
1288
1289 atomic_inc(&dcc->issued_discard);
1290
1291 f2fs_update_iostat(sbi, FS_DISCARD, 1);
1292
Chao Yu35ec7d52018-08-06 22:43:50 +08001293 lstart += len;
1294 start += len;
1295 total_len -= len;
1296 len = total_len;
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001297 }
Chao Yu35ec7d52018-08-06 22:43:50 +08001298
Sahitya Tummaladf423392020-04-16 11:47:41 +05301299 if (!err && len) {
1300 dcc->undiscard_blks -= len;
Chao Yu35ec7d52018-08-06 22:43:50 +08001301 __update_discard_tree_range(sbi, bdev, lstart, start, len);
Sahitya Tummaladf423392020-04-16 11:47:41 +05301302 }
Chao Yu6b9cb122018-08-08 10:14:55 +08001303 return err;
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001304}
1305
Chao Yu47d0d7d72020-05-27 18:27:52 +08001306static void __insert_discard_tree(struct f2fs_sb_info *sbi,
Chao Yu004b6862017-04-14 23:24:55 +08001307 struct block_device *bdev, block_t lstart,
1308 block_t start, block_t len,
1309 struct rb_node **insert_p,
1310 struct rb_node *insert_parent)
1311{
1312 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Colin Ian Kingdca69512017-10-19 12:58:21 +02001313 struct rb_node **p;
Chao Yu004b6862017-04-14 23:24:55 +08001314 struct rb_node *parent = NULL;
Chao Yu4dada3f2018-10-04 11:18:30 +08001315 bool leftmost = true;
Chao Yu004b6862017-04-14 23:24:55 +08001316
1317 if (insert_p && insert_parent) {
1318 parent = insert_parent;
1319 p = insert_p;
1320 goto do_insert;
1321 }
1322
Chao Yu4dada3f2018-10-04 11:18:30 +08001323 p = f2fs_lookup_rb_tree_for_insert(sbi, &dcc->root, &parent,
1324 lstart, &leftmost);
Chao Yu004b6862017-04-14 23:24:55 +08001325do_insert:
Chao Yu47d0d7d72020-05-27 18:27:52 +08001326 __attach_discard_cmd(sbi, bdev, lstart, start, len, parent,
Chao Yu4dada3f2018-10-04 11:18:30 +08001327 p, leftmost);
Chao Yu004b6862017-04-14 23:24:55 +08001328}
1329
Chao Yuba48a332017-04-15 14:09:37 +08001330static void __relocate_discard_cmd(struct discard_cmd_control *dcc,
1331 struct discard_cmd *dc)
1332{
1333 list_move_tail(&dc->list, &dcc->pend_list[plist_idx(dc->len)]);
1334}
1335
Chao Yu004b6862017-04-14 23:24:55 +08001336static void __punch_discard_cmd(struct f2fs_sb_info *sbi,
1337 struct discard_cmd *dc, block_t blkaddr)
1338{
Chao Yuba48a332017-04-15 14:09:37 +08001339 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Chao Yu004b6862017-04-14 23:24:55 +08001340 struct discard_info di = dc->di;
1341 bool modified = false;
1342
1343 if (dc->state == D_DONE || dc->len == 1) {
1344 __remove_discard_cmd(sbi, dc);
1345 return;
1346 }
1347
Chao Yud84d1cb2017-04-18 19:27:39 +08001348 dcc->undiscard_blks -= di.len;
1349
Chao Yu004b6862017-04-14 23:24:55 +08001350 if (blkaddr > di.lstart) {
1351 dc->len = blkaddr - dc->lstart;
Chao Yud84d1cb2017-04-18 19:27:39 +08001352 dcc->undiscard_blks += dc->len;
Chao Yuba48a332017-04-15 14:09:37 +08001353 __relocate_discard_cmd(dcc, dc);
Chao Yu004b6862017-04-14 23:24:55 +08001354 modified = true;
1355 }
1356
1357 if (blkaddr < di.lstart + di.len - 1) {
1358 if (modified) {
1359 __insert_discard_tree(sbi, dc->bdev, blkaddr + 1,
1360 di.start + blkaddr + 1 - di.lstart,
1361 di.lstart + di.len - 1 - blkaddr,
1362 NULL, NULL);
1363 } else {
1364 dc->lstart++;
1365 dc->len--;
1366 dc->start++;
Chao Yud84d1cb2017-04-18 19:27:39 +08001367 dcc->undiscard_blks += dc->len;
Chao Yuba48a332017-04-15 14:09:37 +08001368 __relocate_discard_cmd(dcc, dc);
Chao Yu004b6862017-04-14 23:24:55 +08001369 }
1370 }
1371}
1372
1373static void __update_discard_tree_range(struct f2fs_sb_info *sbi,
1374 struct block_device *bdev, block_t lstart,
1375 block_t start, block_t len)
1376{
1377 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1378 struct discard_cmd *prev_dc = NULL, *next_dc = NULL;
1379 struct discard_cmd *dc;
1380 struct discard_info di = {0};
1381 struct rb_node **insert_p = NULL, *insert_parent = NULL;
Chao Yu35ec7d52018-08-06 22:43:50 +08001382 struct request_queue *q = bdev_get_queue(bdev);
1383 unsigned int max_discard_blocks =
1384 SECTOR_TO_BLOCK(q->limits.max_discard_sectors);
Chao Yu004b6862017-04-14 23:24:55 +08001385 block_t end = lstart + len;
1386
Chao Yu4d57b862018-05-30 00:20:41 +08001387 dc = (struct discard_cmd *)f2fs_lookup_rb_tree_ret(&dcc->root,
Chao Yu004b6862017-04-14 23:24:55 +08001388 NULL, lstart,
1389 (struct rb_entry **)&prev_dc,
1390 (struct rb_entry **)&next_dc,
Chao Yu4dada3f2018-10-04 11:18:30 +08001391 &insert_p, &insert_parent, true, NULL);
Chao Yu004b6862017-04-14 23:24:55 +08001392 if (dc)
1393 prev_dc = dc;
1394
1395 if (!prev_dc) {
1396 di.lstart = lstart;
1397 di.len = next_dc ? next_dc->lstart - lstart : len;
1398 di.len = min(di.len, len);
1399 di.start = start;
1400 }
1401
1402 while (1) {
1403 struct rb_node *node;
1404 bool merged = false;
1405 struct discard_cmd *tdc = NULL;
1406
1407 if (prev_dc) {
1408 di.lstart = prev_dc->lstart + prev_dc->len;
1409 if (di.lstart < lstart)
1410 di.lstart = lstart;
1411 if (di.lstart >= end)
1412 break;
1413
1414 if (!next_dc || next_dc->lstart > end)
1415 di.len = end - di.lstart;
1416 else
1417 di.len = next_dc->lstart - di.lstart;
1418 di.start = start + di.lstart - lstart;
1419 }
1420
1421 if (!di.len)
1422 goto next;
1423
1424 if (prev_dc && prev_dc->state == D_PREP &&
1425 prev_dc->bdev == bdev &&
Chao Yu35ec7d52018-08-06 22:43:50 +08001426 __is_discard_back_mergeable(&di, &prev_dc->di,
1427 max_discard_blocks)) {
Chao Yu004b6862017-04-14 23:24:55 +08001428 prev_dc->di.len += di.len;
Chao Yud84d1cb2017-04-18 19:27:39 +08001429 dcc->undiscard_blks += di.len;
Chao Yuba48a332017-04-15 14:09:37 +08001430 __relocate_discard_cmd(dcc, prev_dc);
Chao Yu004b6862017-04-14 23:24:55 +08001431 di = prev_dc->di;
1432 tdc = prev_dc;
1433 merged = true;
1434 }
1435
1436 if (next_dc && next_dc->state == D_PREP &&
1437 next_dc->bdev == bdev &&
Chao Yu35ec7d52018-08-06 22:43:50 +08001438 __is_discard_front_mergeable(&di, &next_dc->di,
1439 max_discard_blocks)) {
Chao Yu004b6862017-04-14 23:24:55 +08001440 next_dc->di.lstart = di.lstart;
1441 next_dc->di.len += di.len;
1442 next_dc->di.start = di.start;
Chao Yud84d1cb2017-04-18 19:27:39 +08001443 dcc->undiscard_blks += di.len;
Chao Yuba48a332017-04-15 14:09:37 +08001444 __relocate_discard_cmd(dcc, next_dc);
Chao Yu004b6862017-04-14 23:24:55 +08001445 if (tdc)
1446 __remove_discard_cmd(sbi, tdc);
Chao Yu004b6862017-04-14 23:24:55 +08001447 merged = true;
1448 }
1449
Chao Yudf0f6b42017-04-17 18:21:43 +08001450 if (!merged) {
Chao Yu004b6862017-04-14 23:24:55 +08001451 __insert_discard_tree(sbi, bdev, di.lstart, di.start,
1452 di.len, NULL, NULL);
Chao Yudf0f6b42017-04-17 18:21:43 +08001453 }
Chao Yu004b6862017-04-14 23:24:55 +08001454 next:
1455 prev_dc = next_dc;
1456 if (!prev_dc)
1457 break;
1458
1459 node = rb_next(&prev_dc->rb_node);
1460 next_dc = rb_entry_safe(node, struct discard_cmd, rb_node);
1461 }
Chao Yu004b6862017-04-14 23:24:55 +08001462}
1463
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001464static int __queue_discard_cmd(struct f2fs_sb_info *sbi,
1465 struct block_device *bdev, block_t blkstart, block_t blklen)
1466{
1467 block_t lblkstart = blkstart;
1468
Damien Le Moal7f3d7712019-03-16 09:13:08 +09001469 if (!f2fs_bdev_support_discard(bdev))
1470 return 0;
1471
Chao Yu0243a5f2017-04-15 14:09:38 +08001472 trace_f2fs_queue_discard(bdev, blkstart, blklen);
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001473
Damien Le Moal09168782019-03-16 09:13:06 +09001474 if (f2fs_is_multi_device(sbi)) {
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001475 int devi = f2fs_target_device_index(sbi, blkstart);
1476
1477 blkstart -= FDEV(devi).start_blk;
1478 }
Chao Yu35ec7d52018-08-06 22:43:50 +08001479 mutex_lock(&SM_I(sbi)->dcc_info->cmd_lock);
Chao Yu004b6862017-04-14 23:24:55 +08001480 __update_discard_tree_range(sbi, bdev, lblkstart, blkstart, blklen);
Chao Yu35ec7d52018-08-06 22:43:50 +08001481 mutex_unlock(&SM_I(sbi)->dcc_info->cmd_lock);
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001482 return 0;
1483}
1484
Chao Yu20ee4382018-07-08 22:11:01 +08001485static unsigned int __issue_discard_cmd_orderly(struct f2fs_sb_info *sbi,
1486 struct discard_policy *dpolicy)
1487{
1488 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1489 struct discard_cmd *prev_dc = NULL, *next_dc = NULL;
1490 struct rb_node **insert_p = NULL, *insert_parent = NULL;
1491 struct discard_cmd *dc;
1492 struct blk_plug plug;
1493 unsigned int pos = dcc->next_pos;
1494 unsigned int issued = 0;
1495 bool io_interrupted = false;
1496
1497 mutex_lock(&dcc->cmd_lock);
1498 dc = (struct discard_cmd *)f2fs_lookup_rb_tree_ret(&dcc->root,
1499 NULL, pos,
1500 (struct rb_entry **)&prev_dc,
1501 (struct rb_entry **)&next_dc,
Chao Yu4dada3f2018-10-04 11:18:30 +08001502 &insert_p, &insert_parent, true, NULL);
Chao Yu20ee4382018-07-08 22:11:01 +08001503 if (!dc)
1504 dc = next_dc;
1505
1506 blk_start_plug(&plug);
1507
1508 while (dc) {
1509 struct rb_node *node;
Chao Yu6b9cb122018-08-08 10:14:55 +08001510 int err = 0;
Chao Yu20ee4382018-07-08 22:11:01 +08001511
1512 if (dc->state != D_PREP)
1513 goto next;
1514
Sahitya Tummalaa7d10cf2018-09-19 14:18:47 +05301515 if (dpolicy->io_aware && !is_idle(sbi, DISCARD_TIME)) {
Chao Yu20ee4382018-07-08 22:11:01 +08001516 io_interrupted = true;
1517 break;
1518 }
1519
1520 dcc->next_pos = dc->lstart + dc->len;
Chao Yu6b9cb122018-08-08 10:14:55 +08001521 err = __submit_discard_cmd(sbi, dpolicy, dc, &issued);
Chao Yu20ee4382018-07-08 22:11:01 +08001522
Chao Yu35ec7d52018-08-06 22:43:50 +08001523 if (issued >= dpolicy->max_requests)
Chao Yu20ee4382018-07-08 22:11:01 +08001524 break;
1525next:
1526 node = rb_next(&dc->rb_node);
Chao Yu6b9cb122018-08-08 10:14:55 +08001527 if (err)
1528 __remove_discard_cmd(sbi, dc);
Chao Yu20ee4382018-07-08 22:11:01 +08001529 dc = rb_entry_safe(node, struct discard_cmd, rb_node);
1530 }
1531
1532 blk_finish_plug(&plug);
1533
1534 if (!dc)
1535 dcc->next_pos = 0;
1536
1537 mutex_unlock(&dcc->cmd_lock);
1538
1539 if (!issued && io_interrupted)
1540 issued = -1;
1541
1542 return issued;
1543}
Sahitya Tummala141af6b2020-04-15 14:37:53 +05301544static unsigned int __wait_all_discard_cmd(struct f2fs_sb_info *sbi,
1545 struct discard_policy *dpolicy);
Chao Yu20ee4382018-07-08 22:11:01 +08001546
Chao Yu78997b52017-10-04 09:08:34 +08001547static int __issue_discard_cmd(struct f2fs_sb_info *sbi,
1548 struct discard_policy *dpolicy)
Chao Yubd5b0732017-04-25 20:21:37 +08001549{
1550 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1551 struct list_head *pend_list;
1552 struct discard_cmd *dc, *tmp;
1553 struct blk_plug plug;
Sahitya Tummala141af6b2020-04-15 14:37:53 +05301554 int i, issued;
Chao Yue6c6de12017-09-12 21:35:12 +08001555 bool io_interrupted = false;
Chao Yubd5b0732017-04-25 20:21:37 +08001556
Chao Yu6ce48b02020-03-26 17:43:56 +08001557 if (dpolicy->timeout)
1558 f2fs_update_time(sbi, UMOUNT_DISCARD_TIMEOUT);
Jaegeuk Kim03f2c022019-01-14 10:42:11 -08001559
Sahitya Tummala141af6b2020-04-15 14:37:53 +05301560retry:
1561 issued = 0;
Chao Yu78997b52017-10-04 09:08:34 +08001562 for (i = MAX_PLIST_NUM - 1; i >= 0; i--) {
Chao Yu6ce48b02020-03-26 17:43:56 +08001563 if (dpolicy->timeout &&
1564 f2fs_time_over(sbi, UMOUNT_DISCARD_TIMEOUT))
Jaegeuk Kim03f2c022019-01-14 10:42:11 -08001565 break;
1566
Chao Yu78997b52017-10-04 09:08:34 +08001567 if (i + 1 < dpolicy->granularity)
1568 break;
Chao Yu20ee4382018-07-08 22:11:01 +08001569
1570 if (i < DEFAULT_DISCARD_GRANULARITY && dpolicy->ordered)
1571 return __issue_discard_cmd_orderly(sbi, dpolicy);
1572
Chao Yubd5b0732017-04-25 20:21:37 +08001573 pend_list = &dcc->pend_list[i];
Chao Yu33da62c2017-10-04 09:08:35 +08001574
1575 mutex_lock(&dcc->cmd_lock);
Chao Yu49c60c62018-01-08 18:48:33 +08001576 if (list_empty(pend_list))
1577 goto next;
Chao Yu67fce702018-06-22 16:06:59 +08001578 if (unlikely(dcc->rbtree_check))
1579 f2fs_bug_on(sbi, !f2fs_check_rb_tree_consistence(sbi,
Chao Yu2e9b2bb2020-08-04 21:14:48 +08001580 &dcc->root, false));
Chao Yu33da62c2017-10-04 09:08:35 +08001581 blk_start_plug(&plug);
Chao Yubd5b0732017-04-25 20:21:37 +08001582 list_for_each_entry_safe(dc, tmp, pend_list, list) {
1583 f2fs_bug_on(sbi, dc->state != D_PREP);
1584
Chao Yu6ce48b02020-03-26 17:43:56 +08001585 if (dpolicy->timeout &&
1586 f2fs_time_over(sbi, UMOUNT_DISCARD_TIMEOUT))
Heng Xiao6e0cd4a2019-07-03 10:29:57 +08001587 break;
1588
Chao Yuecc9aa02017-10-04 09:08:33 +08001589 if (dpolicy->io_aware && i < dpolicy->io_aware_gran &&
Sahitya Tummalaa7d10cf2018-09-19 14:18:47 +05301590 !is_idle(sbi, DISCARD_TIME)) {
Chao Yue6c6de12017-09-12 21:35:12 +08001591 io_interrupted = true;
Chao Yu522d1712018-07-08 22:08:09 +08001592 break;
Chao Yue6c6de12017-09-12 21:35:12 +08001593 }
1594
Chao Yu35ec7d52018-08-06 22:43:50 +08001595 __submit_discard_cmd(sbi, dpolicy, dc, &issued);
Chao Yu522d1712018-07-08 22:08:09 +08001596
Chao Yu35ec7d52018-08-06 22:43:50 +08001597 if (issued >= dpolicy->max_requests)
Chao Yu33da62c2017-10-04 09:08:35 +08001598 break;
Chao Yubd5b0732017-04-25 20:21:37 +08001599 }
Chao Yu33da62c2017-10-04 09:08:35 +08001600 blk_finish_plug(&plug);
Chao Yu49c60c62018-01-08 18:48:33 +08001601next:
Chao Yu33da62c2017-10-04 09:08:35 +08001602 mutex_unlock(&dcc->cmd_lock);
1603
Chao Yu522d1712018-07-08 22:08:09 +08001604 if (issued >= dpolicy->max_requests || io_interrupted)
Chao Yu33da62c2017-10-04 09:08:35 +08001605 break;
Chao Yubd5b0732017-04-25 20:21:37 +08001606 }
Chao Yu969d1b12017-08-07 23:09:56 +08001607
Sahitya Tummala141af6b2020-04-15 14:37:53 +05301608 if (dpolicy->type == DPOLICY_UMOUNT && issued) {
1609 __wait_all_discard_cmd(sbi, dpolicy);
1610 goto retry;
1611 }
1612
Chao Yue6c6de12017-09-12 21:35:12 +08001613 if (!issued && io_interrupted)
1614 issued = -1;
1615
Chao Yu969d1b12017-08-07 23:09:56 +08001616 return issued;
1617}
1618
Chao Yucf5c7592017-10-04 09:08:37 +08001619static bool __drop_discard_cmd(struct f2fs_sb_info *sbi)
Chao Yu969d1b12017-08-07 23:09:56 +08001620{
1621 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1622 struct list_head *pend_list;
1623 struct discard_cmd *dc, *tmp;
1624 int i;
Chao Yucf5c7592017-10-04 09:08:37 +08001625 bool dropped = false;
Chao Yu969d1b12017-08-07 23:09:56 +08001626
1627 mutex_lock(&dcc->cmd_lock);
1628 for (i = MAX_PLIST_NUM - 1; i >= 0; i--) {
1629 pend_list = &dcc->pend_list[i];
1630 list_for_each_entry_safe(dc, tmp, pend_list, list) {
1631 f2fs_bug_on(sbi, dc->state != D_PREP);
1632 __remove_discard_cmd(sbi, dc);
Chao Yucf5c7592017-10-04 09:08:37 +08001633 dropped = true;
Chao Yu969d1b12017-08-07 23:09:56 +08001634 }
1635 }
1636 mutex_unlock(&dcc->cmd_lock);
Chao Yucf5c7592017-10-04 09:08:37 +08001637
1638 return dropped;
Chao Yubd5b0732017-04-25 20:21:37 +08001639}
1640
Chao Yu4d57b862018-05-30 00:20:41 +08001641void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi)
Chao Yu7950e9a2018-01-18 17:23:29 +08001642{
1643 __drop_discard_cmd(sbi);
1644}
1645
Chao Yu0ea80512017-10-28 16:52:32 +08001646static unsigned int __wait_one_discard_bio(struct f2fs_sb_info *sbi,
Chao Yu2a510c002017-06-05 18:29:06 +08001647 struct discard_cmd *dc)
1648{
1649 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Chao Yu0ea80512017-10-28 16:52:32 +08001650 unsigned int len = 0;
Chao Yu2a510c002017-06-05 18:29:06 +08001651
1652 wait_for_completion_io(&dc->wait);
1653 mutex_lock(&dcc->cmd_lock);
1654 f2fs_bug_on(sbi, dc->state != D_DONE);
1655 dc->ref--;
Chao Yu0ea80512017-10-28 16:52:32 +08001656 if (!dc->ref) {
1657 if (!dc->error)
1658 len = dc->len;
Chao Yu2a510c002017-06-05 18:29:06 +08001659 __remove_discard_cmd(sbi, dc);
Chao Yu0ea80512017-10-28 16:52:32 +08001660 }
Chao Yu2a510c002017-06-05 18:29:06 +08001661 mutex_unlock(&dcc->cmd_lock);
Chao Yu0ea80512017-10-28 16:52:32 +08001662
1663 return len;
Chao Yu2a510c002017-06-05 18:29:06 +08001664}
1665
Chao Yu0ea80512017-10-28 16:52:32 +08001666static unsigned int __wait_discard_cmd_range(struct f2fs_sb_info *sbi,
Chao Yu78997b52017-10-04 09:08:34 +08001667 struct discard_policy *dpolicy,
1668 block_t start, block_t end)
Chao Yu63a94fa2017-04-25 20:21:38 +08001669{
1670 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Chao Yu78997b52017-10-04 09:08:34 +08001671 struct list_head *wait_list = (dpolicy->type == DPOLICY_FSTRIM) ?
1672 &(dcc->fstrim_list) : &(dcc->wait_list);
Chao Yu63a94fa2017-04-25 20:21:38 +08001673 struct discard_cmd *dc, *tmp;
Chao Yu6afae632017-05-19 23:46:45 +08001674 bool need_wait;
Chao Yu0ea80512017-10-28 16:52:32 +08001675 unsigned int trimmed = 0;
Chao Yu6afae632017-05-19 23:46:45 +08001676
1677next:
1678 need_wait = false;
Chao Yu63a94fa2017-04-25 20:21:38 +08001679
1680 mutex_lock(&dcc->cmd_lock);
1681 list_for_each_entry_safe(dc, tmp, wait_list, list) {
Chao Yu84126632017-10-04 09:08:32 +08001682 if (dc->lstart + dc->len <= start || end <= dc->lstart)
1683 continue;
Chao Yu78997b52017-10-04 09:08:34 +08001684 if (dc->len < dpolicy->granularity)
Chao Yu84126632017-10-04 09:08:32 +08001685 continue;
Chao Yu78997b52017-10-04 09:08:34 +08001686 if (dc->state == D_DONE && !dc->ref) {
Chao Yu63a94fa2017-04-25 20:21:38 +08001687 wait_for_completion_io(&dc->wait);
Chao Yu0ea80512017-10-28 16:52:32 +08001688 if (!dc->error)
1689 trimmed += dc->len;
Chao Yu63a94fa2017-04-25 20:21:38 +08001690 __remove_discard_cmd(sbi, dc);
Chao Yu6afae632017-05-19 23:46:45 +08001691 } else {
1692 dc->ref++;
1693 need_wait = true;
1694 break;
Chao Yu63a94fa2017-04-25 20:21:38 +08001695 }
1696 }
1697 mutex_unlock(&dcc->cmd_lock);
Chao Yu6afae632017-05-19 23:46:45 +08001698
1699 if (need_wait) {
Chao Yu0ea80512017-10-28 16:52:32 +08001700 trimmed += __wait_one_discard_bio(sbi, dc);
Chao Yu6afae632017-05-19 23:46:45 +08001701 goto next;
1702 }
Chao Yu0ea80512017-10-28 16:52:32 +08001703
1704 return trimmed;
Chao Yu63a94fa2017-04-25 20:21:38 +08001705}
1706
Chao Yu01f9cf62018-06-25 20:33:24 +08001707static unsigned int __wait_all_discard_cmd(struct f2fs_sb_info *sbi,
Chao Yu78997b52017-10-04 09:08:34 +08001708 struct discard_policy *dpolicy)
Chao Yu84126632017-10-04 09:08:32 +08001709{
Jaegeuk Kim9a997182018-05-24 13:57:26 -07001710 struct discard_policy dp;
Chao Yu01f9cf62018-06-25 20:33:24 +08001711 unsigned int discard_blks;
Jaegeuk Kim9a997182018-05-24 13:57:26 -07001712
Chao Yu01f9cf62018-06-25 20:33:24 +08001713 if (dpolicy)
1714 return __wait_discard_cmd_range(sbi, dpolicy, 0, UINT_MAX);
Jaegeuk Kim9a997182018-05-24 13:57:26 -07001715
1716 /* wait all */
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -07001717 __init_discard_policy(sbi, &dp, DPOLICY_FSTRIM, 1);
Chao Yu01f9cf62018-06-25 20:33:24 +08001718 discard_blks = __wait_discard_cmd_range(sbi, &dp, 0, UINT_MAX);
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -07001719 __init_discard_policy(sbi, &dp, DPOLICY_UMOUNT, 1);
Chao Yu01f9cf62018-06-25 20:33:24 +08001720 discard_blks += __wait_discard_cmd_range(sbi, &dp, 0, UINT_MAX);
1721
1722 return discard_blks;
Chao Yu84126632017-10-04 09:08:32 +08001723}
1724
Jaegeuk Kim4e6a8d92016-12-29 14:07:53 -08001725/* This should be covered by global mutex, &sit_i->sentry_lock */
Wei Yongjun94b1e102018-01-05 09:41:20 +00001726static void f2fs_wait_discard_bio(struct f2fs_sb_info *sbi, block_t blkaddr)
Chao Yu275b66b2016-08-29 23:58:34 +08001727{
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001728 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Chao Yu004b6862017-04-14 23:24:55 +08001729 struct discard_cmd *dc;
Chao Yuec9895a2017-04-26 17:39:54 +08001730 bool need_wait = false;
Chao Yu275b66b2016-08-29 23:58:34 +08001731
Jaegeuk Kim15469962017-01-09 20:32:07 -08001732 mutex_lock(&dcc->cmd_lock);
Chao Yu4d57b862018-05-30 00:20:41 +08001733 dc = (struct discard_cmd *)f2fs_lookup_rb_tree(&dcc->root,
1734 NULL, blkaddr);
Chao Yu004b6862017-04-14 23:24:55 +08001735 if (dc) {
Chao Yuec9895a2017-04-26 17:39:54 +08001736 if (dc->state == D_PREP) {
1737 __punch_discard_cmd(sbi, dc, blkaddr);
1738 } else {
1739 dc->ref++;
1740 need_wait = true;
1741 }
Chao Yu275b66b2016-08-29 23:58:34 +08001742 }
Chao Yud4314132017-04-05 18:19:49 +08001743 mutex_unlock(&dcc->cmd_lock);
Chao Yuec9895a2017-04-26 17:39:54 +08001744
Chao Yu2a510c002017-06-05 18:29:06 +08001745 if (need_wait)
1746 __wait_one_discard_bio(sbi, dc);
Chao Yud4314132017-04-05 18:19:49 +08001747}
Chao Yu22d375d2017-04-05 18:19:48 +08001748
Chao Yu4d57b862018-05-30 00:20:41 +08001749void f2fs_stop_discard_thread(struct f2fs_sb_info *sbi)
Chao Yucce13252017-06-29 23:17:45 +08001750{
1751 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1752
1753 if (dcc && dcc->f2fs_issue_discard) {
1754 struct task_struct *discard_thread = dcc->f2fs_issue_discard;
1755
1756 dcc->f2fs_issue_discard = NULL;
1757 kthread_stop(discard_thread);
Jaegeuk Kim15469962017-01-09 20:32:07 -08001758 }
1759}
1760
Chao Yu84126632017-10-04 09:08:32 +08001761/* This comes from f2fs_put_super */
Jaegeuk Kim03f2c022019-01-14 10:42:11 -08001762bool f2fs_issue_discard_timeout(struct f2fs_sb_info *sbi)
Chao Yu275b66b2016-08-29 23:58:34 +08001763{
Chao Yu969d1b12017-08-07 23:09:56 +08001764 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Chao Yu78997b52017-10-04 09:08:34 +08001765 struct discard_policy dpolicy;
Chao Yucf5c7592017-10-04 09:08:37 +08001766 bool dropped;
Chao Yu969d1b12017-08-07 23:09:56 +08001767
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -07001768 __init_discard_policy(sbi, &dpolicy, DPOLICY_UMOUNT,
1769 dcc->discard_granularity);
Chao Yu78997b52017-10-04 09:08:34 +08001770 __issue_discard_cmd(sbi, &dpolicy);
Chao Yucf5c7592017-10-04 09:08:37 +08001771 dropped = __drop_discard_cmd(sbi);
Chao Yucf5c7592017-10-04 09:08:37 +08001772
Jaegeuk Kim9a997182018-05-24 13:57:26 -07001773 /* just to make sure there is no pending discard commands */
1774 __wait_all_discard_cmd(sbi, NULL);
Chao Yu2482c432018-07-08 22:16:53 +08001775
1776 f2fs_bug_on(sbi, atomic_read(&dcc->discard_cmd_cnt));
Chao Yucf5c7592017-10-04 09:08:37 +08001777 return dropped;
Chao Yu969d1b12017-08-07 23:09:56 +08001778}
1779
Jaegeuk Kim15469962017-01-09 20:32:07 -08001780static int issue_discard_thread(void *data)
1781{
1782 struct f2fs_sb_info *sbi = data;
1783 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1784 wait_queue_head_t *q = &dcc->discard_wait_queue;
Chao Yu78997b52017-10-04 09:08:34 +08001785 struct discard_policy dpolicy;
Chao Yu969d1b12017-08-07 23:09:56 +08001786 unsigned int wait_ms = DEF_MIN_DISCARD_ISSUE_TIME;
1787 int issued;
Jaegeuk Kim15469962017-01-09 20:32:07 -08001788
Jaegeuk Kim1d7be272017-05-17 10:36:58 -07001789 set_freezable();
Jaegeuk Kim15469962017-01-09 20:32:07 -08001790
Jaegeuk Kim1d7be272017-05-17 10:36:58 -07001791 do {
Sahitya Tummalac35b8d52021-04-06 14:39:16 +05301792 if (sbi->gc_mode == GC_URGENT_HIGH ||
1793 !f2fs_available_free_memory(sbi, DISCARD_CACHE))
1794 __init_discard_policy(sbi, &dpolicy, DPOLICY_FORCE, 1);
1795 else
1796 __init_discard_policy(sbi, &dpolicy, DPOLICY_BG,
1797 dcc->discard_granularity);
1798
1799 if (!atomic_read(&dcc->discard_cmd_cnt))
1800 wait_ms = dpolicy.max_interval;
Chao Yu78997b52017-10-04 09:08:34 +08001801
Chao Yu969d1b12017-08-07 23:09:56 +08001802 wait_event_interruptible_timeout(*q,
1803 kthread_should_stop() || freezing(current) ||
1804 dcc->discard_wake,
1805 msecs_to_jiffies(wait_ms));
Sheng Yong35a9a762018-05-08 17:51:34 +08001806
1807 if (dcc->discard_wake)
1808 dcc->discard_wake = 0;
1809
Jaegeuk Kim76c7bfb2018-12-13 20:50:51 -08001810 /* clean up pending candidates before going to sleep */
1811 if (atomic_read(&dcc->queued_discard))
1812 __wait_all_discard_cmd(sbi, NULL);
1813
Jaegeuk Kim1d7be272017-05-17 10:36:58 -07001814 if (try_to_freeze())
1815 continue;
Chao Yu3b60d802018-01-25 18:57:27 +08001816 if (f2fs_readonly(sbi->sb))
1817 continue;
Jaegeuk Kim1d7be272017-05-17 10:36:58 -07001818 if (kthread_should_stop())
1819 return 0;
Yunlei Hed6184772018-04-13 11:08:05 +08001820 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) {
1821 wait_ms = dpolicy.max_interval;
1822 continue;
1823 }
Chao Yu43f8c47e2021-02-22 18:07:33 +08001824 if (!atomic_read(&dcc->discard_cmd_cnt))
1825 continue;
Jaegeuk Kim1d7be272017-05-17 10:36:58 -07001826
Chao Yudc6febb2017-07-22 08:52:23 +08001827 sb_start_intwrite(sbi->sb);
1828
Chao Yu78997b52017-10-04 09:08:34 +08001829 issued = __issue_discard_cmd(sbi, &dpolicy);
Yunlei Hef9d1dce2018-04-08 15:11:11 +08001830 if (issued > 0) {
Chao Yu78997b52017-10-04 09:08:34 +08001831 __wait_all_discard_cmd(sbi, &dpolicy);
1832 wait_ms = dpolicy.min_interval;
Yi Zhuang5f029c02021-04-06 09:47:35 +08001833 } else if (issued == -1) {
Sahitya Tummalaa7d10cf2018-09-19 14:18:47 +05301834 wait_ms = f2fs_time_to_wait(sbi, DISCARD_TIME);
1835 if (!wait_ms)
Sahitya Tummalaabde73c2018-08-31 15:09:26 +05301836 wait_ms = dpolicy.mid_interval;
Chao Yu969d1b12017-08-07 23:09:56 +08001837 } else {
Chao Yu78997b52017-10-04 09:08:34 +08001838 wait_ms = dpolicy.max_interval;
Chao Yu969d1b12017-08-07 23:09:56 +08001839 }
Jaegeuk Kim1d7be272017-05-17 10:36:58 -07001840
Chao Yudc6febb2017-07-22 08:52:23 +08001841 sb_end_intwrite(sbi->sb);
1842
Jaegeuk Kim1d7be272017-05-17 10:36:58 -07001843 } while (!kthread_should_stop());
1844 return 0;
Jaegeuk Kim15469962017-01-09 20:32:07 -08001845}
1846
Damien Le Moalf46e88092016-10-28 17:45:06 +09001847#ifdef CONFIG_BLK_DEV_ZONED
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001848static int __f2fs_issue_discard_zone(struct f2fs_sb_info *sbi,
1849 struct block_device *bdev, block_t blkstart, block_t blklen)
Damien Le Moalf46e88092016-10-28 17:45:06 +09001850{
Jaegeuk Kim925922852017-02-22 20:18:35 -08001851 sector_t sector, nr_sects;
Kinglong Mee10a875f2017-03-08 09:49:53 +08001852 block_t lblkstart = blkstart;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001853 int devi = 0;
Damien Le Moalf46e88092016-10-28 17:45:06 +09001854
Damien Le Moal09168782019-03-16 09:13:06 +09001855 if (f2fs_is_multi_device(sbi)) {
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001856 devi = f2fs_target_device_index(sbi, blkstart);
Damien Le Moal95175da2019-03-16 09:13:07 +09001857 if (blkstart < FDEV(devi).start_blk ||
1858 blkstart > FDEV(devi).end_blk) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08001859 f2fs_err(sbi, "Invalid block %x", blkstart);
Damien Le Moal95175da2019-03-16 09:13:07 +09001860 return -EIO;
1861 }
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001862 blkstart -= FDEV(devi).start_blk;
1863 }
Damien Le Moalf46e88092016-10-28 17:45:06 +09001864
Damien Le Moal95175da2019-03-16 09:13:07 +09001865 /* For sequential zones, reset the zone write pointer */
1866 if (f2fs_blkz_is_seq(sbi, devi, blkstart)) {
Jaegeuk Kim925922852017-02-22 20:18:35 -08001867 sector = SECTOR_FROM_BLOCK(blkstart);
1868 nr_sects = SECTOR_FROM_BLOCK(blklen);
1869
1870 if (sector & (bdev_zone_sectors(bdev) - 1) ||
1871 nr_sects != bdev_zone_sectors(bdev)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08001872 f2fs_err(sbi, "(%d) %s: Unaligned zone reset attempted (block %x + %x)",
1873 devi, sbi->s_ndevs ? FDEV(devi).path : "",
1874 blkstart, blklen);
Jaegeuk Kim925922852017-02-22 20:18:35 -08001875 return -EIO;
1876 }
Jaegeuk Kimd50aaee2017-02-15 11:14:06 -08001877 trace_f2fs_issue_reset_zone(bdev, blkstart);
Ajay Joshi6c1b1da2019-10-27 23:05:45 +09001878 return blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET,
1879 sector, nr_sects, GFP_NOFS);
Damien Le Moalf46e88092016-10-28 17:45:06 +09001880 }
Damien Le Moal95175da2019-03-16 09:13:07 +09001881
1882 /* For conventional zones, use regular discard if supported */
Damien Le Moal95175da2019-03-16 09:13:07 +09001883 return __queue_discard_cmd(sbi, bdev, lblkstart, blklen);
Damien Le Moalf46e88092016-10-28 17:45:06 +09001884}
1885#endif
1886
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001887static int __issue_discard_async(struct f2fs_sb_info *sbi,
1888 struct block_device *bdev, block_t blkstart, block_t blklen)
1889{
1890#ifdef CONFIG_BLK_DEV_ZONED
Damien Le Moal7f3d7712019-03-16 09:13:08 +09001891 if (f2fs_sb_has_blkzoned(sbi) && bdev_is_zoned(bdev))
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001892 return __f2fs_issue_discard_zone(sbi, bdev, blkstart, blklen);
1893#endif
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001894 return __queue_discard_cmd(sbi, bdev, blkstart, blklen);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001895}
1896
Jaegeuk Kim1e87a782014-04-15 13:57:55 +09001897static int f2fs_issue_discard(struct f2fs_sb_info *sbi,
Jaegeuk Kim37208872013-11-12 16:55:17 +09001898 block_t blkstart, block_t blklen)
1899{
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001900 sector_t start = blkstart, len = 0;
1901 struct block_device *bdev;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001902 struct seg_entry *se;
1903 unsigned int offset;
1904 block_t i;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001905 int err = 0;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001906
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001907 bdev = f2fs_target_device(sbi, blkstart, NULL);
1908
1909 for (i = blkstart; i < blkstart + blklen; i++, len++) {
1910 if (i != start) {
1911 struct block_device *bdev2 =
1912 f2fs_target_device(sbi, i, NULL);
1913
1914 if (bdev2 != bdev) {
1915 err = __issue_discard_async(sbi, bdev,
1916 start, len);
1917 if (err)
1918 return err;
1919 bdev = bdev2;
1920 start = i;
1921 len = 0;
1922 }
1923 }
1924
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001925 se = get_seg_entry(sbi, GET_SEGNO(sbi, i));
1926 offset = GET_BLKOFF_FROM_SEG0(sbi, i);
1927
Chao Yu4f993262021-08-03 08:15:43 +08001928 if (f2fs_block_unit_discard(sbi) &&
1929 !f2fs_test_and_set_bit(offset, se->discard_map))
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001930 sbi->discard_blks--;
1931 }
Damien Le Moalf46e88092016-10-28 17:45:06 +09001932
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001933 if (len)
1934 err = __issue_discard_async(sbi, bdev, start, len);
1935 return err;
Jaegeuk Kim1e87a782014-04-15 13:57:55 +09001936}
1937
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08001938static bool add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc,
1939 bool check_only)
Jaegeuk Kimadf49832014-10-28 22:27:59 -07001940{
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001941 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
1942 int max_blocks = sbi->blocks_per_seg;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001943 struct seg_entry *se = get_seg_entry(sbi, cpc->trim_start);
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001944 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
1945 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001946 unsigned long *discard_map = (unsigned long *)se->discard_map;
Jaegeuk Kim60a3b782015-02-10 16:44:29 -08001947 unsigned long *dmap = SIT_I(sbi)->tmp_map;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001948 unsigned int start = 0, end = -1;
Chao Yuc473f1a2017-04-27 20:40:39 +08001949 bool force = (cpc->reason & CP_DISCARD);
Chao Yua7eeb8232017-03-28 18:18:50 +08001950 struct discard_entry *de = NULL;
Chao Yu46f84c22017-04-15 14:09:36 +08001951 struct list_head *head = &SM_I(sbi)->dcc_info->entry_list;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001952 int i;
1953
Chao Yu4f993262021-08-03 08:15:43 +08001954 if (se->valid_blocks == max_blocks || !f2fs_hw_support_discard(sbi) ||
1955 !f2fs_block_unit_discard(sbi))
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08001956 return false;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001957
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001958 if (!force) {
Chao Yu7d20c8a2018-09-04 03:52:17 +08001959 if (!f2fs_realtime_discard_enable(sbi) || !se->valid_blocks ||
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001960 SM_I(sbi)->dcc_info->nr_discards >=
1961 SM_I(sbi)->dcc_info->max_discards)
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08001962 return false;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001963 }
1964
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001965 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
1966 for (i = 0; i < entries; i++)
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001967 dmap[i] = force ? ~ckpt_map[i] & ~discard_map[i] :
Jaegeuk Kimd7bc2482014-12-12 13:53:41 -08001968 (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001969
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001970 while (force || SM_I(sbi)->dcc_info->nr_discards <=
1971 SM_I(sbi)->dcc_info->max_discards) {
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001972 start = __find_rev_next_bit(dmap, max_blocks, end + 1);
1973 if (start >= max_blocks)
1974 break;
1975
1976 end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
Yunlei Hec7b41e12016-07-07 12:13:33 +08001977 if (force && start && end != max_blocks
1978 && (end - start) < cpc->trim_minlen)
1979 continue;
1980
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08001981 if (check_only)
1982 return true;
1983
Chao Yua7eeb8232017-03-28 18:18:50 +08001984 if (!de) {
1985 de = f2fs_kmem_cache_alloc(discard_entry_slab,
Chao Yu32410572021-08-09 08:24:48 +08001986 GFP_F2FS_ZERO, true, NULL);
Chao Yua7eeb8232017-03-28 18:18:50 +08001987 de->start_blkaddr = START_BLOCK(sbi, cpc->trim_start);
1988 list_add_tail(&de->list, head);
1989 }
1990
1991 for (i = start; i < end; i++)
1992 __set_bit_le(i, (void *)de->discard_map);
1993
1994 SM_I(sbi)->dcc_info->nr_discards += end - start;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001995 }
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08001996 return false;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001997}
1998
Chao Yuaf8ff652018-04-25 17:38:29 +08001999static void release_discard_addr(struct discard_entry *entry)
2000{
2001 list_del(&entry->list);
2002 kmem_cache_free(discard_entry_slab, entry);
2003}
2004
Chao Yu4d57b862018-05-30 00:20:41 +08002005void f2fs_release_discard_addrs(struct f2fs_sb_info *sbi)
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002006{
Chao Yu46f84c22017-04-15 14:09:36 +08002007 struct list_head *head = &(SM_I(sbi)->dcc_info->entry_list);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002008 struct discard_entry *entry, *this;
2009
2010 /* drop caches */
Chao Yuaf8ff652018-04-25 17:38:29 +08002011 list_for_each_entry_safe(entry, this, head, list)
2012 release_discard_addr(entry);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002013}
2014
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002015/*
Chao Yu4d57b862018-05-30 00:20:41 +08002016 * Should call f2fs_clear_prefree_segments after checkpoint is done.
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002017 */
2018static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
2019{
2020 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Chao Yub65ee142014-08-04 10:10:07 +08002021 unsigned int segno;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002022
2023 mutex_lock(&dirty_i->seglist_lock);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002024 for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], MAIN_SEGS(sbi))
Chao Yud0b9e422020-08-04 21:14:45 +08002025 __set_test_and_free(sbi, segno, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002026 mutex_unlock(&dirty_i->seglist_lock);
2027}
2028
Chao Yu4d57b862018-05-30 00:20:41 +08002029void f2fs_clear_prefree_segments(struct f2fs_sb_info *sbi,
2030 struct cp_control *cpc)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002031{
Chao Yu969d1b12017-08-07 23:09:56 +08002032 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
2033 struct list_head *head = &dcc->entry_list;
Chao Yu2d7b8222014-03-29 11:33:17 +08002034 struct discard_entry *entry, *this;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002035 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Changman Lee29e59c12013-11-11 09:24:37 +09002036 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
Changman Lee29e59c12013-11-11 09:24:37 +09002037 unsigned int start = 0, end = -1;
Jaegeuk Kim36abef42016-06-03 19:29:38 -07002038 unsigned int secno, start_segno;
Chao Yuc473f1a2017-04-27 20:40:39 +08002039 bool force = (cpc->reason & CP_DISCARD);
Chao Yu4f993262021-08-03 08:15:43 +08002040 bool section_alignment = F2FS_OPTION(sbi).discard_unit ==
2041 DISCARD_UNIT_SECTION;
2042
2043 if (f2fs_lfs_mode(sbi) && __is_large_section(sbi))
2044 section_alignment = true;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002045
2046 mutex_lock(&dirty_i->seglist_lock);
Changman Lee29e59c12013-11-11 09:24:37 +09002047
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002048 while (1) {
Changman Lee29e59c12013-11-11 09:24:37 +09002049 int i;
Yunlong Songad6672b2018-07-19 20:58:15 +08002050
Chao Yu4f993262021-08-03 08:15:43 +08002051 if (section_alignment && end != -1)
Yunlong Songad6672b2018-07-19 20:58:15 +08002052 end--;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002053 start = find_next_bit(prefree_map, MAIN_SEGS(sbi), end + 1);
2054 if (start >= MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002055 break;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002056 end = find_next_zero_bit(prefree_map, MAIN_SEGS(sbi),
2057 start + 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002058
Chao Yu4f993262021-08-03 08:15:43 +08002059 if (section_alignment) {
Yunlong Songad6672b2018-07-19 20:58:15 +08002060 start = rounddown(start, sbi->segs_per_sec);
2061 end = roundup(end, sbi->segs_per_sec);
2062 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002063
Yunlong Songad6672b2018-07-19 20:58:15 +08002064 for (i = start; i < end; i++) {
2065 if (test_and_clear_bit(i, prefree_map))
2066 dirty_i->nr_dirty[PRE]--;
2067 }
Changman Lee29e59c12013-11-11 09:24:37 +09002068
Chao Yu7d20c8a2018-09-04 03:52:17 +08002069 if (!f2fs_realtime_discard_enable(sbi))
Changman Lee29e59c12013-11-11 09:24:37 +09002070 continue;
2071
Yunlei He650d3c42016-12-22 11:46:24 +08002072 if (force && start >= cpc->trim_start &&
2073 (end - 1) <= cpc->trim_end)
2074 continue;
2075
Chao Yub0332a02020-02-14 17:44:12 +08002076 if (!f2fs_lfs_mode(sbi) || !__is_large_section(sbi)) {
Jaegeuk Kim36abef42016-06-03 19:29:38 -07002077 f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
Jaegeuk Kim37208872013-11-12 16:55:17 +09002078 (end - start) << sbi->log_blocks_per_seg);
Jaegeuk Kim36abef42016-06-03 19:29:38 -07002079 continue;
2080 }
2081next:
Jaegeuk Kim4ddb1a42017-04-07 15:08:17 -07002082 secno = GET_SEC_FROM_SEG(sbi, start);
2083 start_segno = GET_SEG_FROM_SEC(sbi, secno);
Jaegeuk Kim36abef42016-06-03 19:29:38 -07002084 if (!IS_CURSEC(sbi, secno) &&
Jaegeuk Kim302bd342017-04-07 14:33:22 -07002085 !get_valid_blocks(sbi, start, true))
Jaegeuk Kim36abef42016-06-03 19:29:38 -07002086 f2fs_issue_discard(sbi, START_BLOCK(sbi, start_segno),
2087 sbi->segs_per_sec << sbi->log_blocks_per_seg);
2088
2089 start = start_segno + sbi->segs_per_sec;
2090 if (start < end)
2091 goto next;
Jaegeuk Kim8b107f52017-02-27 11:57:11 -08002092 else
2093 end = start - 1;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002094 }
2095 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kimb2955552013-11-12 14:49:56 +09002096
Chao Yu4f993262021-08-03 08:15:43 +08002097 if (!f2fs_block_unit_discard(sbi))
2098 goto wakeup;
2099
Jaegeuk Kimb2955552013-11-12 14:49:56 +09002100 /* send small discards */
Chao Yu2d7b8222014-03-29 11:33:17 +08002101 list_for_each_entry_safe(entry, this, head, list) {
Chao Yua7eeb8232017-03-28 18:18:50 +08002102 unsigned int cur_pos = 0, next_pos, len, total_len = 0;
2103 bool is_valid = test_bit_le(0, entry->discard_map);
2104
2105find_next:
2106 if (is_valid) {
2107 next_pos = find_next_zero_bit_le(entry->discard_map,
2108 sbi->blocks_per_seg, cur_pos);
2109 len = next_pos - cur_pos;
2110
Chao Yu7beb01f2018-10-24 18:34:26 +08002111 if (f2fs_sb_has_blkzoned(sbi) ||
Damien Le Moalacfd28102017-05-26 17:04:40 +09002112 (force && len < cpc->trim_minlen))
Chao Yua7eeb8232017-03-28 18:18:50 +08002113 goto skip;
2114
2115 f2fs_issue_discard(sbi, entry->start_blkaddr + cur_pos,
2116 len);
Chao Yua7eeb8232017-03-28 18:18:50 +08002117 total_len += len;
2118 } else {
2119 next_pos = find_next_bit_le(entry->discard_map,
2120 sbi->blocks_per_seg, cur_pos);
2121 }
Jaegeuk Kim836b5a62015-04-30 22:50:06 -07002122skip:
Chao Yua7eeb8232017-03-28 18:18:50 +08002123 cur_pos = next_pos;
2124 is_valid = !is_valid;
2125
2126 if (cur_pos < sbi->blocks_per_seg)
2127 goto find_next;
2128
Chao Yuaf8ff652018-04-25 17:38:29 +08002129 release_discard_addr(entry);
Chao Yu969d1b12017-08-07 23:09:56 +08002130 dcc->nr_discards -= total_len;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09002131 }
Chao Yu34e159d2017-04-25 00:21:34 +08002132
Chao Yu4f993262021-08-03 08:15:43 +08002133wakeup:
Jaegeuk Kim01983c72017-08-22 21:15:43 -07002134 wake_up_discard_thread(sbi, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002135}
2136
Fengnan Chang4d674902021-08-19 16:02:37 +08002137int f2fs_start_discard_thread(struct f2fs_sb_info *sbi)
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08002138{
Jaegeuk Kim15469962017-01-09 20:32:07 -08002139 dev_t dev = sbi->sb->s_bdev->bd_dev;
Fengnan Chang4d674902021-08-19 16:02:37 +08002140 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
2141 int err = 0;
2142
2143 if (!f2fs_realtime_discard_enable(sbi))
2144 return 0;
2145
2146 dcc->f2fs_issue_discard = kthread_run(issue_discard_thread, sbi,
2147 "f2fs_discard-%u:%u", MAJOR(dev), MINOR(dev));
2148 if (IS_ERR(dcc->f2fs_issue_discard))
2149 err = PTR_ERR(dcc->f2fs_issue_discard);
2150
2151 return err;
2152}
2153
2154static int create_discard_cmd_control(struct f2fs_sb_info *sbi)
2155{
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08002156 struct discard_cmd_control *dcc;
Chao Yuba48a332017-04-15 14:09:37 +08002157 int err = 0, i;
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08002158
2159 if (SM_I(sbi)->dcc_info) {
2160 dcc = SM_I(sbi)->dcc_info;
2161 goto init_thread;
2162 }
2163
Chao Yuacbf0542017-11-30 19:28:17 +08002164 dcc = f2fs_kzalloc(sbi, sizeof(struct discard_cmd_control), GFP_KERNEL);
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08002165 if (!dcc)
2166 return -ENOMEM;
2167
Chao Yu969d1b12017-08-07 23:09:56 +08002168 dcc->discard_granularity = DEFAULT_DISCARD_GRANULARITY;
Chao Yu4f993262021-08-03 08:15:43 +08002169 if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_SEGMENT)
2170 dcc->discard_granularity = sbi->blocks_per_seg;
2171 else if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_SECTION)
2172 dcc->discard_granularity = BLKS_PER_SEC(sbi);
2173
Chao Yu46f84c22017-04-15 14:09:36 +08002174 INIT_LIST_HEAD(&dcc->entry_list);
Chao Yu78997b52017-10-04 09:08:34 +08002175 for (i = 0; i < MAX_PLIST_NUM; i++)
Chao Yuba48a332017-04-15 14:09:37 +08002176 INIT_LIST_HEAD(&dcc->pend_list[i]);
Chao Yu46f84c22017-04-15 14:09:36 +08002177 INIT_LIST_HEAD(&dcc->wait_list);
Chao Yu84126632017-10-04 09:08:32 +08002178 INIT_LIST_HEAD(&dcc->fstrim_list);
Jaegeuk Kim15469962017-01-09 20:32:07 -08002179 mutex_init(&dcc->cmd_lock);
Chao Yu8b8dd652017-03-25 17:19:58 +08002180 atomic_set(&dcc->issued_discard, 0);
Jaegeuk Kim72691af2018-12-13 16:53:57 -08002181 atomic_set(&dcc->queued_discard, 0);
Chao Yu5f323662017-03-25 17:19:59 +08002182 atomic_set(&dcc->discard_cmd_cnt, 0);
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08002183 dcc->nr_discards = 0;
Chao Yud618eba2017-04-25 00:21:35 +08002184 dcc->max_discards = MAIN_SEGS(sbi) << sbi->log_blocks_per_seg;
Chao Yud84d1cb2017-04-18 19:27:39 +08002185 dcc->undiscard_blks = 0;
Chao Yu20ee4382018-07-08 22:11:01 +08002186 dcc->next_pos = 0;
Chao Yu4dada3f2018-10-04 11:18:30 +08002187 dcc->root = RB_ROOT_CACHED;
Chao Yu67fce702018-06-22 16:06:59 +08002188 dcc->rbtree_check = false;
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08002189
Jaegeuk Kim15469962017-01-09 20:32:07 -08002190 init_waitqueue_head(&dcc->discard_wait_queue);
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08002191 SM_I(sbi)->dcc_info = dcc;
2192init_thread:
Fengnan Chang4d674902021-08-19 16:02:37 +08002193 err = f2fs_start_discard_thread(sbi);
2194 if (err) {
Chao Yuc8eb7022020-09-14 16:47:00 +08002195 kfree(dcc);
Jaegeuk Kim15469962017-01-09 20:32:07 -08002196 SM_I(sbi)->dcc_info = NULL;
Jaegeuk Kim15469962017-01-09 20:32:07 -08002197 }
2198
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08002199 return err;
2200}
2201
Chao Yuf0994052017-03-27 18:14:04 +08002202static void destroy_discard_cmd_control(struct f2fs_sb_info *sbi)
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08002203{
2204 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
2205
Chao Yuf0994052017-03-27 18:14:04 +08002206 if (!dcc)
2207 return;
2208
Chao Yu4d57b862018-05-30 00:20:41 +08002209 f2fs_stop_discard_thread(sbi);
Chao Yuf0994052017-03-27 18:14:04 +08002210
Chao Yu04f92872019-07-19 15:18:44 +08002211 /*
2212 * Recovery can cache discard commands, so in error path of
2213 * fill_super(), it needs to give a chance to handle them.
2214 */
2215 if (unlikely(atomic_read(&dcc->discard_cmd_cnt)))
2216 f2fs_issue_discard_timeout(sbi);
2217
Chao Yuc8eb7022020-09-14 16:47:00 +08002218 kfree(dcc);
Chao Yuf0994052017-03-27 18:14:04 +08002219 SM_I(sbi)->dcc_info = NULL;
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08002220}
2221
Chao Yu184a5cd2014-09-04 18:13:01 +08002222static bool __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002223{
2224 struct sit_info *sit_i = SIT_I(sbi);
Chao Yu184a5cd2014-09-04 18:13:01 +08002225
2226 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002227 sit_i->dirty_sentries++;
Chao Yu184a5cd2014-09-04 18:13:01 +08002228 return false;
2229 }
2230
2231 return true;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002232}
2233
2234static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
2235 unsigned int segno, int modified)
2236{
2237 struct seg_entry *se = get_seg_entry(sbi, segno);
Yi Zhuang5f029c02021-04-06 09:47:35 +08002238
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002239 se->type = type;
2240 if (modified)
2241 __mark_sit_entry_dirty(sbi, segno);
2242}
2243
Chao Yuc5d02782020-08-04 21:14:47 +08002244static inline unsigned long long get_segment_mtime(struct f2fs_sb_info *sbi,
2245 block_t blkaddr)
Chao Yu6f3a01a2020-08-04 21:14:46 +08002246{
2247 unsigned int segno = GET_SEGNO(sbi, blkaddr);
Chao Yuc5d02782020-08-04 21:14:47 +08002248
2249 if (segno == NULL_SEGNO)
2250 return 0;
2251 return get_seg_entry(sbi, segno)->mtime;
2252}
2253
2254static void update_segment_mtime(struct f2fs_sb_info *sbi, block_t blkaddr,
2255 unsigned long long old_mtime)
2256{
2257 struct seg_entry *se;
2258 unsigned int segno = GET_SEGNO(sbi, blkaddr);
2259 unsigned long long ctime = get_mtime(sbi, false);
2260 unsigned long long mtime = old_mtime ? old_mtime : ctime;
2261
2262 if (segno == NULL_SEGNO)
2263 return;
2264
2265 se = get_seg_entry(sbi, segno);
Chao Yu6f3a01a2020-08-04 21:14:46 +08002266
2267 if (!se->mtime)
2268 se->mtime = mtime;
2269 else
2270 se->mtime = div_u64(se->mtime * se->valid_blocks + mtime,
2271 se->valid_blocks + 1);
2272
Chao Yuc5d02782020-08-04 21:14:47 +08002273 if (ctime > SIT_I(sbi)->max_mtime)
2274 SIT_I(sbi)->max_mtime = ctime;
Chao Yu6f3a01a2020-08-04 21:14:46 +08002275}
2276
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002277static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
2278{
2279 struct seg_entry *se;
2280 unsigned int segno, offset;
2281 long int new_vblocks;
Yunlong Song6415fed2017-08-02 21:20:13 +08002282 bool exist;
2283#ifdef CONFIG_F2FS_CHECK_FS
2284 bool mir_exist;
2285#endif
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002286
2287 segno = GET_SEGNO(sbi, blkaddr);
2288
2289 se = get_seg_entry(sbi, segno);
2290 new_vblocks = se->valid_blocks + del;
Jaegeuk Kim491c0852014-02-04 13:01:10 +09002291 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002292
Zhihao Cheng9feffe142020-08-01 11:24:50 +08002293 f2fs_bug_on(sbi, (new_vblocks < 0 ||
Aravind Rameshde881df2020-07-16 18:26:56 +05302294 (new_vblocks > f2fs_usable_blks_in_seg(sbi, segno))));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002295
2296 se->valid_blocks = new_vblocks;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002297
2298 /* Update valid block bitmap */
2299 if (del > 0) {
Yunlong Song6415fed2017-08-02 21:20:13 +08002300 exist = f2fs_test_and_set_bit(offset, se->cur_valid_map);
Chao Yu355e7892017-01-07 18:51:01 +08002301#ifdef CONFIG_F2FS_CHECK_FS
Yunlong Song6415fed2017-08-02 21:20:13 +08002302 mir_exist = f2fs_test_and_set_bit(offset,
2303 se->cur_valid_map_mir);
2304 if (unlikely(exist != mir_exist)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002305 f2fs_err(sbi, "Inconsistent error when setting bitmap, blk:%u, old bit:%d",
2306 blkaddr, exist);
Jaegeuk Kim05796762014-09-02 16:05:00 -07002307 f2fs_bug_on(sbi, 1);
Chao Yu355e7892017-01-07 18:51:01 +08002308 }
Yunlong Song6415fed2017-08-02 21:20:13 +08002309#endif
2310 if (unlikely(exist)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002311 f2fs_err(sbi, "Bitmap was wrongly set, blk:%u",
2312 blkaddr);
Yunlong Song6415fed2017-08-02 21:20:13 +08002313 f2fs_bug_on(sbi, 1);
Yunlong Song35ee82c2017-08-02 22:16:54 +08002314 se->valid_blocks--;
2315 del = 0;
Yunlong Song6415fed2017-08-02 21:20:13 +08002316 }
2317
Chao Yu4f993262021-08-03 08:15:43 +08002318 if (f2fs_block_unit_discard(sbi) &&
2319 !f2fs_test_and_set_bit(offset, se->discard_map))
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07002320 sbi->discard_blks--;
Jaegeuk Kim720037f2017-03-06 11:59:56 -08002321
Chao Yu899fee32019-08-16 11:03:34 +08002322 /*
2323 * SSR should never reuse block which is checkpointed
2324 * or newly invalidated.
2325 */
2326 if (!is_sbi_flag_set(sbi, SBI_CP_DISABLED)) {
Jaegeuk Kim720037f2017-03-06 11:59:56 -08002327 if (!f2fs_test_and_set_bit(offset, se->ckpt_valid_map))
2328 se->ckpt_valid_blocks++;
2329 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002330 } else {
Yunlong Song6415fed2017-08-02 21:20:13 +08002331 exist = f2fs_test_and_clear_bit(offset, se->cur_valid_map);
Chao Yu355e7892017-01-07 18:51:01 +08002332#ifdef CONFIG_F2FS_CHECK_FS
Yunlong Song6415fed2017-08-02 21:20:13 +08002333 mir_exist = f2fs_test_and_clear_bit(offset,
2334 se->cur_valid_map_mir);
2335 if (unlikely(exist != mir_exist)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002336 f2fs_err(sbi, "Inconsistent error when clearing bitmap, blk:%u, old bit:%d",
2337 blkaddr, exist);
Jaegeuk Kim05796762014-09-02 16:05:00 -07002338 f2fs_bug_on(sbi, 1);
Chao Yu355e7892017-01-07 18:51:01 +08002339 }
Yunlong Song6415fed2017-08-02 21:20:13 +08002340#endif
2341 if (unlikely(!exist)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002342 f2fs_err(sbi, "Bitmap was wrongly cleared, blk:%u",
2343 blkaddr);
Yunlong Song6415fed2017-08-02 21:20:13 +08002344 f2fs_bug_on(sbi, 1);
Yunlong Song35ee82c2017-08-02 22:16:54 +08002345 se->valid_blocks++;
2346 del = 0;
Daniel Rosenberg43549942018-08-20 19:21:43 -07002347 } else if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
2348 /*
2349 * If checkpoints are off, we must not reuse data that
2350 * was used in the previous checkpoint. If it was used
2351 * before, we must track that to know how much space we
2352 * really have.
2353 */
Chao Yuc9c8ed52019-05-05 11:40:46 +08002354 if (f2fs_test_bit(offset, se->ckpt_valid_map)) {
2355 spin_lock(&sbi->stat_lock);
Daniel Rosenberg43549942018-08-20 19:21:43 -07002356 sbi->unusable_block_count++;
Chao Yuc9c8ed52019-05-05 11:40:46 +08002357 spin_unlock(&sbi->stat_lock);
2358 }
Yunlong Song6415fed2017-08-02 21:20:13 +08002359 }
2360
Chao Yu4f993262021-08-03 08:15:43 +08002361 if (f2fs_block_unit_discard(sbi) &&
2362 f2fs_test_and_clear_bit(offset, se->discard_map))
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07002363 sbi->discard_blks++;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002364 }
2365 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
2366 se->ckpt_valid_blocks += del;
2367
2368 __mark_sit_entry_dirty(sbi, segno);
2369
2370 /* update total number of valid blocks to be written in ckpt area */
2371 SIT_I(sbi)->written_valid_blocks += del;
2372
Chao Yu2c70c5e2018-10-24 18:37:26 +08002373 if (__is_large_section(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002374 get_sec_entry(sbi, segno)->valid_blocks += del;
2375}
2376
Chao Yu4d57b862018-05-30 00:20:41 +08002377void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002378{
2379 unsigned int segno = GET_SEGNO(sbi, addr);
2380 struct sit_info *sit_i = SIT_I(sbi);
2381
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002382 f2fs_bug_on(sbi, addr == NULL_ADDR);
Chao Yu4c8ff702019-11-01 18:07:14 +08002383 if (addr == NEW_ADDR || addr == COMPRESS_ADDR)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002384 return;
2385
Chao Yu6aa58d82018-08-14 22:37:25 +08002386 invalidate_mapping_pages(META_MAPPING(sbi), addr, addr);
Chao Yu6ce19af2021-05-20 19:51:50 +08002387 f2fs_invalidate_compress_page(sbi, addr);
Chao Yu6aa58d82018-08-14 22:37:25 +08002388
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002389 /* add it into sit main buffer */
Chao Yu3d26fa62017-10-30 17:49:53 +08002390 down_write(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002391
Chao Yuc5d02782020-08-04 21:14:47 +08002392 update_segment_mtime(sbi, addr, 0);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002393 update_sit_entry(sbi, addr, -1);
2394
2395 /* add it into dirty seglist */
2396 locate_dirty_segment(sbi, segno);
2397
Chao Yu3d26fa62017-10-30 17:49:53 +08002398 up_write(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002399}
2400
Chao Yu4d57b862018-05-30 00:20:41 +08002401bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr)
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07002402{
2403 struct sit_info *sit_i = SIT_I(sbi);
2404 unsigned int segno, offset;
2405 struct seg_entry *se;
2406 bool is_cp = false;
2407
Chao Yu93770ab2019-04-15 15:26:32 +08002408 if (!__is_valid_data_blkaddr(blkaddr))
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07002409 return true;
2410
Chao Yu3d26fa62017-10-30 17:49:53 +08002411 down_read(&sit_i->sentry_lock);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07002412
2413 segno = GET_SEGNO(sbi, blkaddr);
2414 se = get_seg_entry(sbi, segno);
2415 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
2416
2417 if (f2fs_test_bit(offset, se->ckpt_valid_map))
2418 is_cp = true;
2419
Chao Yu3d26fa62017-10-30 17:49:53 +08002420 up_read(&sit_i->sentry_lock);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07002421
2422 return is_cp;
2423}
2424
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002425/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002426 * This function should be resided under the curseg_mutex lock
2427 */
2428static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
Haicheng Lie79efe32013-06-13 16:59:27 +08002429 struct f2fs_summary *sum)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002430{
2431 struct curseg_info *curseg = CURSEG_I(sbi, type);
2432 void *addr = curseg->sum_blk;
Yi Zhuang5f029c02021-04-06 09:47:35 +08002433
Haicheng Lie79efe32013-06-13 16:59:27 +08002434 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002435 memcpy(addr, sum, sizeof(struct f2fs_summary));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002436}
2437
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002438/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002439 * Calculate the number of current summary pages for writing
2440 */
Chao Yu4d57b862018-05-30 00:20:41 +08002441int f2fs_npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002442{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002443 int valid_sum_count = 0;
Fan Li9a479382013-10-29 16:21:47 +08002444 int i, sum_in_page;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002445
2446 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
2447 if (sbi->ckpt->alloc_type[i] == SSR)
2448 valid_sum_count += sbi->blocks_per_seg;
Chao Yu3fa06d72014-12-09 14:21:46 +08002449 else {
2450 if (for_ra)
2451 valid_sum_count += le16_to_cpu(
2452 F2FS_CKPT(sbi)->cur_data_blkoff[i]);
2453 else
2454 valid_sum_count += curseg_blkoff(sbi, i);
2455 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002456 }
2457
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002458 sum_in_page = (PAGE_SIZE - 2 * SUM_JOURNAL_SIZE -
Fan Li9a479382013-10-29 16:21:47 +08002459 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
2460 if (valid_sum_count <= sum_in_page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002461 return 1;
Fan Li9a479382013-10-29 16:21:47 +08002462 else if ((valid_sum_count - sum_in_page) <=
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002463 (PAGE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002464 return 2;
2465 return 3;
2466}
2467
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002468/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002469 * Caller should put this summary page
2470 */
Chao Yu4d57b862018-05-30 00:20:41 +08002471struct page *f2fs_get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002472{
Jaegeuk Kim86f33602020-10-02 14:17:35 -07002473 if (unlikely(f2fs_cp_error(sbi)))
2474 return ERR_PTR(-EIO);
2475 return f2fs_get_meta_page_retry(sbi, GET_SUM_BLOCK(sbi, segno));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002476}
2477
Chao Yu4d57b862018-05-30 00:20:41 +08002478void f2fs_update_meta_page(struct f2fs_sb_info *sbi,
2479 void *src, block_t blk_addr)
Chao Yu381722d2015-05-19 17:40:04 +08002480{
Chao Yu4d57b862018-05-30 00:20:41 +08002481 struct page *page = f2fs_grab_meta_page(sbi, blk_addr);
Chao Yu381722d2015-05-19 17:40:04 +08002482
Chao Yu0537b812017-11-02 20:41:02 +08002483 memcpy(page_address(page), src, PAGE_SIZE);
Chao Yu381722d2015-05-19 17:40:04 +08002484 set_page_dirty(page);
2485 f2fs_put_page(page, 1);
2486}
2487
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002488static void write_sum_page(struct f2fs_sb_info *sbi,
2489 struct f2fs_summary_block *sum_blk, block_t blk_addr)
2490{
Chao Yu4d57b862018-05-30 00:20:41 +08002491 f2fs_update_meta_page(sbi, (void *)sum_blk, blk_addr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002492}
2493
Chao Yub7ad7512016-02-19 18:08:46 +08002494static void write_current_sum_page(struct f2fs_sb_info *sbi,
2495 int type, block_t blk_addr)
2496{
2497 struct curseg_info *curseg = CURSEG_I(sbi, type);
Chao Yu4d57b862018-05-30 00:20:41 +08002498 struct page *page = f2fs_grab_meta_page(sbi, blk_addr);
Chao Yub7ad7512016-02-19 18:08:46 +08002499 struct f2fs_summary_block *src = curseg->sum_blk;
2500 struct f2fs_summary_block *dst;
2501
2502 dst = (struct f2fs_summary_block *)page_address(page);
Chao Yu81114ba2018-04-09 20:25:06 +08002503 memset(dst, 0, PAGE_SIZE);
Chao Yub7ad7512016-02-19 18:08:46 +08002504
2505 mutex_lock(&curseg->curseg_mutex);
2506
2507 down_read(&curseg->journal_rwsem);
2508 memcpy(&dst->journal, curseg->journal, SUM_JOURNAL_SIZE);
2509 up_read(&curseg->journal_rwsem);
2510
2511 memcpy(dst->entries, src->entries, SUM_ENTRY_SIZE);
2512 memcpy(&dst->footer, &src->footer, SUM_FOOTER_SIZE);
2513
2514 mutex_unlock(&curseg->curseg_mutex);
2515
2516 set_page_dirty(page);
2517 f2fs_put_page(page, 1);
2518}
2519
Chao Yu093749e2020-08-04 21:14:49 +08002520static int is_next_segment_free(struct f2fs_sb_info *sbi,
2521 struct curseg_info *curseg, int type)
Jaegeuk Kima7881892017-04-20 13:51:57 -07002522{
Jaegeuk Kima7881892017-04-20 13:51:57 -07002523 unsigned int segno = curseg->segno + 1;
2524 struct free_segmap_info *free_i = FREE_I(sbi);
2525
2526 if (segno < MAIN_SEGS(sbi) && segno % sbi->segs_per_sec)
2527 return !test_bit(segno, free_i->free_segmap);
2528 return 0;
2529}
2530
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002531/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002532 * Find a new segment from the free segments bitmap to right order
2533 * This function should be returned with success, otherwise BUG
2534 */
2535static void get_new_segment(struct f2fs_sb_info *sbi,
2536 unsigned int *newseg, bool new_sec, int dir)
2537{
2538 struct free_segmap_info *free_i = FREE_I(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002539 unsigned int segno, secno, zoneno;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002540 unsigned int total_zones = MAIN_SECS(sbi) / sbi->secs_per_zone;
Jaegeuk Kim4ddb1a42017-04-07 15:08:17 -07002541 unsigned int hint = GET_SEC_FROM_SEG(sbi, *newseg);
2542 unsigned int old_zoneno = GET_ZONE_FROM_SEG(sbi, *newseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002543 unsigned int left_start = hint;
2544 bool init = true;
2545 int go_left = 0;
2546 int i;
2547
Chao Yu1a118cc2015-02-11 18:20:38 +08002548 spin_lock(&free_i->segmap_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002549
2550 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
2551 segno = find_next_zero_bit(free_i->free_segmap,
Jaegeuk Kim4ddb1a42017-04-07 15:08:17 -07002552 GET_SEG_FROM_SEC(sbi, hint + 1), *newseg + 1);
2553 if (segno < GET_SEG_FROM_SEC(sbi, hint + 1))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002554 goto got_it;
2555 }
2556find_other_zone:
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002557 secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint);
2558 if (secno >= MAIN_SECS(sbi)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002559 if (dir == ALLOC_RIGHT) {
2560 secno = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002561 MAIN_SECS(sbi), 0);
2562 f2fs_bug_on(sbi, secno >= MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002563 } else {
2564 go_left = 1;
2565 left_start = hint - 1;
2566 }
2567 }
2568 if (go_left == 0)
2569 goto skip_left;
2570
2571 while (test_bit(left_start, free_i->free_secmap)) {
2572 if (left_start > 0) {
2573 left_start--;
2574 continue;
2575 }
2576 left_start = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002577 MAIN_SECS(sbi), 0);
2578 f2fs_bug_on(sbi, left_start >= MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002579 break;
2580 }
2581 secno = left_start;
2582skip_left:
Jaegeuk Kim4ddb1a42017-04-07 15:08:17 -07002583 segno = GET_SEG_FROM_SEC(sbi, secno);
2584 zoneno = GET_ZONE_FROM_SEC(sbi, secno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002585
2586 /* give up on finding another zone */
2587 if (!init)
2588 goto got_it;
2589 if (sbi->secs_per_zone == 1)
2590 goto got_it;
2591 if (zoneno == old_zoneno)
2592 goto got_it;
2593 if (dir == ALLOC_LEFT) {
2594 if (!go_left && zoneno + 1 >= total_zones)
2595 goto got_it;
2596 if (go_left && zoneno == 0)
2597 goto got_it;
2598 }
2599 for (i = 0; i < NR_CURSEG_TYPE; i++)
2600 if (CURSEG_I(sbi, i)->zone == zoneno)
2601 break;
2602
2603 if (i < NR_CURSEG_TYPE) {
2604 /* zone is in user, try another */
2605 if (go_left)
2606 hint = zoneno * sbi->secs_per_zone - 1;
2607 else if (zoneno + 1 >= total_zones)
2608 hint = 0;
2609 else
2610 hint = (zoneno + 1) * sbi->secs_per_zone;
2611 init = false;
2612 goto find_other_zone;
2613 }
2614got_it:
2615 /* set it as dirty segment in free segmap */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002616 f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002617 __set_inuse(sbi, segno);
2618 *newseg = segno;
Chao Yu1a118cc2015-02-11 18:20:38 +08002619 spin_unlock(&free_i->segmap_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002620}
2621
2622static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
2623{
2624 struct curseg_info *curseg = CURSEG_I(sbi, type);
2625 struct summary_footer *sum_footer;
Chao Yu093749e2020-08-04 21:14:49 +08002626 unsigned short seg_type = curseg->seg_type;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002627
Chao Yud0b9e422020-08-04 21:14:45 +08002628 curseg->inited = true;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002629 curseg->segno = curseg->next_segno;
Jaegeuk Kim4ddb1a42017-04-07 15:08:17 -07002630 curseg->zone = GET_ZONE_FROM_SEG(sbi, curseg->segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002631 curseg->next_blkoff = 0;
2632 curseg->next_segno = NULL_SEGNO;
2633
2634 sum_footer = &(curseg->sum_blk->footer);
2635 memset(sum_footer, 0, sizeof(struct summary_footer));
Chao Yu093749e2020-08-04 21:14:49 +08002636
2637 sanity_check_seg_type(sbi, seg_type);
2638
2639 if (IS_DATASEG(seg_type))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002640 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
Chao Yu093749e2020-08-04 21:14:49 +08002641 if (IS_NODESEG(seg_type))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002642 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
Chao Yu093749e2020-08-04 21:14:49 +08002643 __set_sit_entry_type(sbi, seg_type, curseg->segno, modified);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002644}
2645
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -04002646static unsigned int __get_next_segno(struct f2fs_sb_info *sbi, int type)
2647{
Chao Yud0b9e422020-08-04 21:14:45 +08002648 struct curseg_info *curseg = CURSEG_I(sbi, type);
Chao Yu093749e2020-08-04 21:14:49 +08002649 unsigned short seg_type = curseg->seg_type;
2650
2651 sanity_check_seg_type(sbi, seg_type);
Chao Yud0b9e422020-08-04 21:14:45 +08002652
Jaegeuk Kima7881892017-04-20 13:51:57 -07002653 /* if segs_per_sec is large than 1, we need to keep original policy. */
Chao Yu2c70c5e2018-10-24 18:37:26 +08002654 if (__is_large_section(sbi))
Chao Yud0b9e422020-08-04 21:14:45 +08002655 return curseg->segno;
2656
2657 /* inmem log may not locate on any segment after mount */
2658 if (!curseg->inited)
2659 return 0;
Jaegeuk Kima7881892017-04-20 13:51:57 -07002660
Daniel Rosenberg43549942018-08-20 19:21:43 -07002661 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2662 return 0;
2663
Yunlong Songb94929d2018-01-29 11:37:45 +08002664 if (test_opt(sbi, NOHEAP) &&
Chao Yu093749e2020-08-04 21:14:49 +08002665 (seg_type == CURSEG_HOT_DATA || IS_NODESEG(seg_type)))
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -04002666 return 0;
2667
Jaegeuk Kime066b832017-04-13 15:17:00 -07002668 if (SIT_I(sbi)->last_victim[ALLOC_NEXT])
2669 return SIT_I(sbi)->last_victim[ALLOC_NEXT];
Jaegeuk Kim07939622018-02-18 08:50:49 -08002670
2671 /* find segments from 0 to reuse freed segments */
Chao Yu63189b72018-03-08 14:22:56 +08002672 if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_REUSE)
Jaegeuk Kim07939622018-02-18 08:50:49 -08002673 return 0;
2674
Chao Yud0b9e422020-08-04 21:14:45 +08002675 return curseg->segno;
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -04002676}
2677
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002678/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002679 * Allocate a current working segment.
2680 * This function always allocates a free segment in LFS manner.
2681 */
2682static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
2683{
2684 struct curseg_info *curseg = CURSEG_I(sbi, type);
Chao Yud0b9e422020-08-04 21:14:45 +08002685 unsigned short seg_type = curseg->seg_type;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002686 unsigned int segno = curseg->segno;
2687 int dir = ALLOC_LEFT;
2688
Chao Yud0b9e422020-08-04 21:14:45 +08002689 if (curseg->inited)
2690 write_sum_page(sbi, curseg->sum_blk,
Haicheng Li81fb5e82013-05-14 18:20:28 +08002691 GET_SUM_BLOCK(sbi, segno));
Chao Yud0b9e422020-08-04 21:14:45 +08002692 if (seg_type == CURSEG_WARM_DATA || seg_type == CURSEG_COLD_DATA)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002693 dir = ALLOC_RIGHT;
2694
2695 if (test_opt(sbi, NOHEAP))
2696 dir = ALLOC_RIGHT;
2697
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -04002698 segno = __get_next_segno(sbi, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002699 get_new_segment(sbi, &segno, new_sec, dir);
2700 curseg->next_segno = segno;
2701 reset_curseg(sbi, type, 1);
2702 curseg->alloc_type = LFS;
2703}
2704
Chao Yu453e2ff2021-04-13 17:56:18 +08002705static int __next_free_blkoff(struct f2fs_sb_info *sbi,
2706 int segno, block_t start)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002707{
Chao Yu453e2ff2021-04-13 17:56:18 +08002708 struct seg_entry *se = get_seg_entry(sbi, segno);
Changman Leee81c93c2013-11-15 13:21:16 +09002709 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
Jaegeuk Kim60a3b782015-02-10 16:44:29 -08002710 unsigned long *target_map = SIT_I(sbi)->tmp_map;
Changman Leee81c93c2013-11-15 13:21:16 +09002711 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
2712 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
Chao Yu453e2ff2021-04-13 17:56:18 +08002713 int i;
Changman Leee81c93c2013-11-15 13:21:16 +09002714
2715 for (i = 0; i < entries; i++)
2716 target_map[i] = ckpt_map[i] | cur_map[i];
2717
Chao Yu453e2ff2021-04-13 17:56:18 +08002718 return __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002719}
2720
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002721/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002722 * If a segment is written by LFS manner, next block offset is just obtained
2723 * by increasing the current block offset. However, if a segment is written by
2724 * SSR manner, next block offset obtained by calling __next_free_blkoff
2725 */
2726static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
2727 struct curseg_info *seg)
2728{
2729 if (seg->alloc_type == SSR)
Chao Yu453e2ff2021-04-13 17:56:18 +08002730 seg->next_blkoff =
2731 __next_free_blkoff(sbi, seg->segno,
2732 seg->next_blkoff + 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002733 else
2734 seg->next_blkoff++;
2735}
2736
Chao Yu61461fc2021-03-24 11:18:28 +08002737bool f2fs_segment_has_free_slot(struct f2fs_sb_info *sbi, int segno)
2738{
Chao Yu453e2ff2021-04-13 17:56:18 +08002739 return __next_free_blkoff(sbi, segno, 0) < sbi->blocks_per_seg;
Chao Yu61461fc2021-03-24 11:18:28 +08002740}
2741
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002742/*
arter97e1c42042014-08-06 23:22:50 +09002743 * This function always allocates a used segment(from dirty seglist) by SSR
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002744 * manner, so it should recover the existing segment information of valid blocks
2745 */
Chao Yu093749e2020-08-04 21:14:49 +08002746static void change_curseg(struct f2fs_sb_info *sbi, int type, bool flush)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002747{
2748 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2749 struct curseg_info *curseg = CURSEG_I(sbi, type);
2750 unsigned int new_segno = curseg->next_segno;
2751 struct f2fs_summary_block *sum_node;
2752 struct page *sum_page;
2753
Chao Yu093749e2020-08-04 21:14:49 +08002754 if (flush)
2755 write_sum_page(sbi, curseg->sum_blk,
2756 GET_SUM_BLOCK(sbi, curseg->segno));
2757
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002758 __set_test_and_inuse(sbi, new_segno);
2759
2760 mutex_lock(&dirty_i->seglist_lock);
2761 __remove_dirty_segment(sbi, new_segno, PRE);
2762 __remove_dirty_segment(sbi, new_segno, DIRTY);
2763 mutex_unlock(&dirty_i->seglist_lock);
2764
2765 reset_curseg(sbi, type, 1);
2766 curseg->alloc_type = SSR;
Chao Yu453e2ff2021-04-13 17:56:18 +08002767 curseg->next_blkoff = __next_free_blkoff(sbi, curseg->segno, 0);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002768
Chao Yu4d57b862018-05-30 00:20:41 +08002769 sum_page = f2fs_get_sum_page(sbi, new_segno);
Jaegeuk Kim86f33602020-10-02 14:17:35 -07002770 if (IS_ERR(sum_page)) {
2771 /* GC won't be able to use stale summary pages by cp_error */
2772 memset(curseg->sum_blk, 0, SUM_ENTRY_SIZE);
2773 return;
2774 }
Chao Yu025d63a2017-08-30 18:04:48 +08002775 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
2776 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
2777 f2fs_put_page(sum_page, 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002778}
2779
Chao Yu093749e2020-08-04 21:14:49 +08002780static int get_ssr_segment(struct f2fs_sb_info *sbi, int type,
2781 int alloc_mode, unsigned long long age);
2782
2783static void get_atssr_segment(struct f2fs_sb_info *sbi, int type,
2784 int target_type, int alloc_mode,
2785 unsigned long long age)
2786{
2787 struct curseg_info *curseg = CURSEG_I(sbi, type);
2788
2789 curseg->seg_type = target_type;
2790
2791 if (get_ssr_segment(sbi, type, alloc_mode, age)) {
2792 struct seg_entry *se = get_seg_entry(sbi, curseg->next_segno);
2793
2794 curseg->seg_type = se->type;
2795 change_curseg(sbi, type, true);
2796 } else {
2797 /* allocate cold segment by default */
2798 curseg->seg_type = CURSEG_COLD_DATA;
2799 new_curseg(sbi, type, true);
2800 }
2801 stat_inc_seg_type(sbi, curseg);
2802}
2803
2804static void __f2fs_init_atgc_curseg(struct f2fs_sb_info *sbi)
2805{
2806 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_ALL_DATA_ATGC);
2807
2808 if (!sbi->am.atgc_enabled)
2809 return;
2810
2811 down_read(&SM_I(sbi)->curseg_lock);
2812
2813 mutex_lock(&curseg->curseg_mutex);
2814 down_write(&SIT_I(sbi)->sentry_lock);
2815
2816 get_atssr_segment(sbi, CURSEG_ALL_DATA_ATGC, CURSEG_COLD_DATA, SSR, 0);
2817
2818 up_write(&SIT_I(sbi)->sentry_lock);
2819 mutex_unlock(&curseg->curseg_mutex);
2820
2821 up_read(&SM_I(sbi)->curseg_lock);
2822
2823}
2824void f2fs_init_inmem_curseg(struct f2fs_sb_info *sbi)
2825{
2826 __f2fs_init_atgc_curseg(sbi);
2827}
2828
2829static void __f2fs_save_inmem_curseg(struct f2fs_sb_info *sbi, int type)
Chao Yud0b9e422020-08-04 21:14:45 +08002830{
2831 struct curseg_info *curseg = CURSEG_I(sbi, type);
2832
2833 mutex_lock(&curseg->curseg_mutex);
2834 if (!curseg->inited)
2835 goto out;
2836
2837 if (get_valid_blocks(sbi, curseg->segno, false)) {
2838 write_sum_page(sbi, curseg->sum_blk,
2839 GET_SUM_BLOCK(sbi, curseg->segno));
2840 } else {
2841 mutex_lock(&DIRTY_I(sbi)->seglist_lock);
2842 __set_test_and_free(sbi, curseg->segno, true);
2843 mutex_unlock(&DIRTY_I(sbi)->seglist_lock);
2844 }
2845out:
2846 mutex_unlock(&curseg->curseg_mutex);
2847}
2848
Chao Yu093749e2020-08-04 21:14:49 +08002849void f2fs_save_inmem_curseg(struct f2fs_sb_info *sbi)
2850{
2851 __f2fs_save_inmem_curseg(sbi, CURSEG_COLD_DATA_PINNED);
2852
2853 if (sbi->am.atgc_enabled)
2854 __f2fs_save_inmem_curseg(sbi, CURSEG_ALL_DATA_ATGC);
2855}
2856
2857static void __f2fs_restore_inmem_curseg(struct f2fs_sb_info *sbi, int type)
Chao Yud0b9e422020-08-04 21:14:45 +08002858{
2859 struct curseg_info *curseg = CURSEG_I(sbi, type);
2860
2861 mutex_lock(&curseg->curseg_mutex);
2862 if (!curseg->inited)
2863 goto out;
2864 if (get_valid_blocks(sbi, curseg->segno, false))
2865 goto out;
2866
2867 mutex_lock(&DIRTY_I(sbi)->seglist_lock);
2868 __set_test_and_inuse(sbi, curseg->segno);
2869 mutex_unlock(&DIRTY_I(sbi)->seglist_lock);
2870out:
2871 mutex_unlock(&curseg->curseg_mutex);
2872}
2873
Chao Yu093749e2020-08-04 21:14:49 +08002874void f2fs_restore_inmem_curseg(struct f2fs_sb_info *sbi)
2875{
2876 __f2fs_restore_inmem_curseg(sbi, CURSEG_COLD_DATA_PINNED);
2877
2878 if (sbi->am.atgc_enabled)
2879 __f2fs_restore_inmem_curseg(sbi, CURSEG_ALL_DATA_ATGC);
2880}
2881
2882static int get_ssr_segment(struct f2fs_sb_info *sbi, int type,
2883 int alloc_mode, unsigned long long age)
Jaegeuk Kim43727522013-02-04 15:11:17 +09002884{
2885 struct curseg_info *curseg = CURSEG_I(sbi, type);
2886 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
Jaegeuk Kime066b832017-04-13 15:17:00 -07002887 unsigned segno = NULL_SEGNO;
Chao Yu093749e2020-08-04 21:14:49 +08002888 unsigned short seg_type = curseg->seg_type;
Chao Yud27c3d82017-02-24 18:46:00 +08002889 int i, cnt;
2890 bool reversed = false;
Jaegeuk Kimc192f7a2017-02-22 17:10:18 -08002891
Chao Yu093749e2020-08-04 21:14:49 +08002892 sanity_check_seg_type(sbi, seg_type);
2893
Chao Yu4d57b862018-05-30 00:20:41 +08002894 /* f2fs_need_SSR() already forces to do this */
Chao Yu093749e2020-08-04 21:14:49 +08002895 if (!v_ops->get_victim(sbi, &segno, BG_GC, seg_type, alloc_mode, age)) {
Jaegeuk Kime066b832017-04-13 15:17:00 -07002896 curseg->next_segno = segno;
Jaegeuk Kimc192f7a2017-02-22 17:10:18 -08002897 return 1;
Jaegeuk Kime066b832017-04-13 15:17:00 -07002898 }
Jaegeuk Kim43727522013-02-04 15:11:17 +09002899
Jaegeuk Kim70d625c2017-02-22 17:02:32 -08002900 /* For node segments, let's do SSR more intensively */
Chao Yu093749e2020-08-04 21:14:49 +08002901 if (IS_NODESEG(seg_type)) {
2902 if (seg_type >= CURSEG_WARM_NODE) {
Chao Yud27c3d82017-02-24 18:46:00 +08002903 reversed = true;
2904 i = CURSEG_COLD_NODE;
2905 } else {
2906 i = CURSEG_HOT_NODE;
2907 }
2908 cnt = NR_CURSEG_NODE_TYPE;
Jaegeuk Kim70d625c2017-02-22 17:02:32 -08002909 } else {
Chao Yu093749e2020-08-04 21:14:49 +08002910 if (seg_type >= CURSEG_WARM_DATA) {
Chao Yud27c3d82017-02-24 18:46:00 +08002911 reversed = true;
2912 i = CURSEG_COLD_DATA;
2913 } else {
2914 i = CURSEG_HOT_DATA;
2915 }
2916 cnt = NR_CURSEG_DATA_TYPE;
Jaegeuk Kim70d625c2017-02-22 17:02:32 -08002917 }
Jaegeuk Kim43727522013-02-04 15:11:17 +09002918
Chao Yud27c3d82017-02-24 18:46:00 +08002919 for (; cnt-- > 0; reversed ? i-- : i++) {
Chao Yu093749e2020-08-04 21:14:49 +08002920 if (i == seg_type)
Jaegeuk Kimc192f7a2017-02-22 17:10:18 -08002921 continue;
Chao Yu093749e2020-08-04 21:14:49 +08002922 if (!v_ops->get_victim(sbi, &segno, BG_GC, i, alloc_mode, age)) {
Jaegeuk Kime066b832017-04-13 15:17:00 -07002923 curseg->next_segno = segno;
Jaegeuk Kim43727522013-02-04 15:11:17 +09002924 return 1;
Jaegeuk Kime066b832017-04-13 15:17:00 -07002925 }
Jaegeuk Kimc192f7a2017-02-22 17:10:18 -08002926 }
Daniel Rosenberg43549942018-08-20 19:21:43 -07002927
2928 /* find valid_blocks=0 in dirty list */
2929 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
2930 segno = get_free_segment(sbi);
2931 if (segno != NULL_SEGNO) {
2932 curseg->next_segno = segno;
2933 return 1;
2934 }
2935 }
Jaegeuk Kim43727522013-02-04 15:11:17 +09002936 return 0;
2937}
2938
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002939/*
2940 * flush out current segment and replace it with new segment
2941 * This function should be returned with success, otherwise BUG
2942 */
2943static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
2944 int type, bool force)
2945{
Jaegeuk Kima7881892017-04-20 13:51:57 -07002946 struct curseg_info *curseg = CURSEG_I(sbi, type);
2947
Gu Zheng7b405272013-08-19 09:41:15 +08002948 if (force)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002949 new_curseg(sbi, type, true);
Jaegeuk Kim5b6c6be2017-02-14 19:32:51 -08002950 else if (!is_set_ckpt_flags(sbi, CP_CRC_RECOVERY_FLAG) &&
Chao Yu093749e2020-08-04 21:14:49 +08002951 curseg->seg_type == CURSEG_WARM_NODE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002952 new_curseg(sbi, type, false);
Chao Yu093749e2020-08-04 21:14:49 +08002953 else if (curseg->alloc_type == LFS &&
2954 is_next_segment_free(sbi, curseg, type) &&
Daniel Rosenberg43549942018-08-20 19:21:43 -07002955 likely(!is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
Jaegeuk Kima7881892017-04-20 13:51:57 -07002956 new_curseg(sbi, type, false);
Chao Yu093749e2020-08-04 21:14:49 +08002957 else if (f2fs_need_SSR(sbi) &&
2958 get_ssr_segment(sbi, type, SSR, 0))
2959 change_curseg(sbi, type, true);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002960 else
2961 new_curseg(sbi, type, false);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +09002962
Jaegeuk Kima7881892017-04-20 13:51:57 -07002963 stat_inc_seg_type(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002964}
2965
Chao Yu0ef81832020-06-18 14:36:22 +08002966void f2fs_allocate_segment_for_resize(struct f2fs_sb_info *sbi, int type,
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08002967 unsigned int start, unsigned int end)
2968{
2969 struct curseg_info *curseg = CURSEG_I(sbi, type);
2970 unsigned int segno;
2971
2972 down_read(&SM_I(sbi)->curseg_lock);
2973 mutex_lock(&curseg->curseg_mutex);
2974 down_write(&SIT_I(sbi)->sentry_lock);
2975
2976 segno = CURSEG_I(sbi, type)->segno;
2977 if (segno < start || segno > end)
2978 goto unlock;
2979
Chao Yu093749e2020-08-04 21:14:49 +08002980 if (f2fs_need_SSR(sbi) && get_ssr_segment(sbi, type, SSR, 0))
2981 change_curseg(sbi, type, true);
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08002982 else
2983 new_curseg(sbi, type, true);
2984
2985 stat_inc_seg_type(sbi, curseg);
2986
2987 locate_dirty_segment(sbi, segno);
2988unlock:
2989 up_write(&SIT_I(sbi)->sentry_lock);
2990
2991 if (segno != curseg->segno)
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002992 f2fs_notice(sbi, "For resize: curseg of type %d: %u ==> %u",
2993 type, segno, curseg->segno);
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08002994
2995 mutex_unlock(&curseg->curseg_mutex);
2996 up_read(&SM_I(sbi)->curseg_lock);
2997}
2998
Chao Yue1175f02021-03-05 17:56:01 +08002999static void __allocate_new_segment(struct f2fs_sb_info *sbi, int type,
Chao Yu509f1012021-04-21 09:54:55 +08003000 bool new_sec, bool force)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003001{
Chao Yu901d745f2020-06-22 17:38:48 +08003002 struct curseg_info *curseg = CURSEG_I(sbi, type);
Jaegeuk Kim6ae1be12016-11-11 12:31:40 -08003003 unsigned int old_segno;
Chao Yu901d745f2020-06-22 17:38:48 +08003004
Chao Yud0b9e422020-08-04 21:14:45 +08003005 if (!curseg->inited)
3006 goto alloc;
3007
Chao Yu509f1012021-04-21 09:54:55 +08003008 if (force || curseg->next_blkoff ||
Chao Yue1175f02021-03-05 17:56:01 +08003009 get_valid_blocks(sbi, curseg->segno, new_sec))
3010 goto alloc;
Chao Yu901d745f2020-06-22 17:38:48 +08003011
Chao Yu61461fc2021-03-24 11:18:28 +08003012 if (!get_ckpt_valid_blocks(sbi, curseg->segno, new_sec))
3013 return;
Chao Yud0b9e422020-08-04 21:14:45 +08003014alloc:
Chao Yu901d745f2020-06-22 17:38:48 +08003015 old_segno = curseg->segno;
3016 SIT_I(sbi)->s_ops->allocate_segment(sbi, type, true);
3017 locate_dirty_segment(sbi, old_segno);
3018}
3019
Chao Yu509f1012021-04-21 09:54:55 +08003020static void __allocate_new_section(struct f2fs_sb_info *sbi,
3021 int type, bool force)
Chao Yu901d745f2020-06-22 17:38:48 +08003022{
Chao Yu509f1012021-04-21 09:54:55 +08003023 __allocate_new_segment(sbi, type, true, force);
Chao Yue1175f02021-03-05 17:56:01 +08003024}
3025
Chao Yu509f1012021-04-21 09:54:55 +08003026void f2fs_allocate_new_section(struct f2fs_sb_info *sbi, int type, bool force)
Chao Yu901d745f2020-06-22 17:38:48 +08003027{
Chao Yu823d13e2021-03-26 09:46:22 +08003028 down_read(&SM_I(sbi)->curseg_lock);
Chao Yu901d745f2020-06-22 17:38:48 +08003029 down_write(&SIT_I(sbi)->sentry_lock);
Chao Yu509f1012021-04-21 09:54:55 +08003030 __allocate_new_section(sbi, type, force);
Chao Yu901d745f2020-06-22 17:38:48 +08003031 up_write(&SIT_I(sbi)->sentry_lock);
Chao Yu823d13e2021-03-26 09:46:22 +08003032 up_read(&SM_I(sbi)->curseg_lock);
Chao Yu901d745f2020-06-22 17:38:48 +08003033}
3034
3035void f2fs_allocate_new_segments(struct f2fs_sb_info *sbi)
3036{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003037 int i;
3038
Chao Yu823d13e2021-03-26 09:46:22 +08003039 down_read(&SM_I(sbi)->curseg_lock);
Chao Yu3d26fa62017-10-30 17:49:53 +08003040 down_write(&SIT_I(sbi)->sentry_lock);
Chao Yu901d745f2020-06-22 17:38:48 +08003041 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++)
Chao Yu509f1012021-04-21 09:54:55 +08003042 __allocate_new_segment(sbi, i, false, false);
Chao Yu3d26fa62017-10-30 17:49:53 +08003043 up_write(&SIT_I(sbi)->sentry_lock);
Chao Yu823d13e2021-03-26 09:46:22 +08003044 up_read(&SM_I(sbi)->curseg_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003045}
3046
3047static const struct segment_allocation default_salloc_ops = {
3048 .allocate_segment = allocate_segment_by_default,
3049};
3050
Chao Yu4d57b862018-05-30 00:20:41 +08003051bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi,
3052 struct cp_control *cpc)
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08003053{
3054 __u64 trim_start = cpc->trim_start;
3055 bool has_candidate = false;
3056
Chao Yu3d26fa62017-10-30 17:49:53 +08003057 down_write(&SIT_I(sbi)->sentry_lock);
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08003058 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++) {
3059 if (add_discard_addrs(sbi, cpc, true)) {
3060 has_candidate = true;
3061 break;
3062 }
3063 }
Chao Yu3d26fa62017-10-30 17:49:53 +08003064 up_write(&SIT_I(sbi)->sentry_lock);
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08003065
3066 cpc->trim_start = trim_start;
3067 return has_candidate;
3068}
3069
Chao Yu01f9cf62018-06-25 20:33:24 +08003070static unsigned int __issue_discard_cmd_range(struct f2fs_sb_info *sbi,
Jaegeuk Kim9a997182018-05-24 13:57:26 -07003071 struct discard_policy *dpolicy,
3072 unsigned int start, unsigned int end)
3073{
3074 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
3075 struct discard_cmd *prev_dc = NULL, *next_dc = NULL;
3076 struct rb_node **insert_p = NULL, *insert_parent = NULL;
3077 struct discard_cmd *dc;
3078 struct blk_plug plug;
3079 int issued;
Chao Yu01f9cf62018-06-25 20:33:24 +08003080 unsigned int trimmed = 0;
Jaegeuk Kim9a997182018-05-24 13:57:26 -07003081
3082next:
3083 issued = 0;
3084
3085 mutex_lock(&dcc->cmd_lock);
Chao Yu67fce702018-06-22 16:06:59 +08003086 if (unlikely(dcc->rbtree_check))
3087 f2fs_bug_on(sbi, !f2fs_check_rb_tree_consistence(sbi,
Chao Yu2e9b2bb2020-08-04 21:14:48 +08003088 &dcc->root, false));
Jaegeuk Kim9a997182018-05-24 13:57:26 -07003089
Chao Yu4d57b862018-05-30 00:20:41 +08003090 dc = (struct discard_cmd *)f2fs_lookup_rb_tree_ret(&dcc->root,
Jaegeuk Kim9a997182018-05-24 13:57:26 -07003091 NULL, start,
3092 (struct rb_entry **)&prev_dc,
3093 (struct rb_entry **)&next_dc,
Chao Yu4dada3f2018-10-04 11:18:30 +08003094 &insert_p, &insert_parent, true, NULL);
Jaegeuk Kim9a997182018-05-24 13:57:26 -07003095 if (!dc)
3096 dc = next_dc;
3097
3098 blk_start_plug(&plug);
3099
3100 while (dc && dc->lstart <= end) {
3101 struct rb_node *node;
Chao Yu6b9cb122018-08-08 10:14:55 +08003102 int err = 0;
Jaegeuk Kim9a997182018-05-24 13:57:26 -07003103
3104 if (dc->len < dpolicy->granularity)
3105 goto skip;
3106
3107 if (dc->state != D_PREP) {
3108 list_move_tail(&dc->list, &dcc->fstrim_list);
3109 goto skip;
3110 }
3111
Chao Yu6b9cb122018-08-08 10:14:55 +08003112 err = __submit_discard_cmd(sbi, dpolicy, dc, &issued);
Jaegeuk Kim9a997182018-05-24 13:57:26 -07003113
Chao Yu35ec7d52018-08-06 22:43:50 +08003114 if (issued >= dpolicy->max_requests) {
Jaegeuk Kim9a997182018-05-24 13:57:26 -07003115 start = dc->lstart + dc->len;
3116
Chao Yu6b9cb122018-08-08 10:14:55 +08003117 if (err)
3118 __remove_discard_cmd(sbi, dc);
3119
Jaegeuk Kim9a997182018-05-24 13:57:26 -07003120 blk_finish_plug(&plug);
3121 mutex_unlock(&dcc->cmd_lock);
Chao Yu01f9cf62018-06-25 20:33:24 +08003122 trimmed += __wait_all_discard_cmd(sbi, NULL);
Chao Yu5df7731f2020-02-17 17:45:44 +08003123 congestion_wait(BLK_RW_ASYNC, DEFAULT_IO_TIMEOUT);
Jaegeuk Kim9a997182018-05-24 13:57:26 -07003124 goto next;
3125 }
3126skip:
3127 node = rb_next(&dc->rb_node);
Chao Yu6b9cb122018-08-08 10:14:55 +08003128 if (err)
3129 __remove_discard_cmd(sbi, dc);
Jaegeuk Kim9a997182018-05-24 13:57:26 -07003130 dc = rb_entry_safe(node, struct discard_cmd, rb_node);
3131
3132 if (fatal_signal_pending(current))
3133 break;
3134 }
3135
3136 blk_finish_plug(&plug);
3137 mutex_unlock(&dcc->cmd_lock);
Chao Yu01f9cf62018-06-25 20:33:24 +08003138
3139 return trimmed;
Jaegeuk Kim9a997182018-05-24 13:57:26 -07003140}
3141
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003142int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range)
3143{
Jaegeuk Kimf7ef9b82015-02-09 12:02:44 -08003144 __u64 start = F2FS_BYTES_TO_BLK(range->start);
3145 __u64 end = start + F2FS_BYTES_TO_BLK(range->len) - 1;
Chao Yu377224c2018-04-09 10:25:23 +08003146 unsigned int start_segno, end_segno;
Chao Yu84126632017-10-04 09:08:32 +08003147 block_t start_block, end_block;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003148 struct cp_control cpc;
Chao Yu78997b52017-10-04 09:08:34 +08003149 struct discard_policy dpolicy;
Chao Yu0ea80512017-10-28 16:52:32 +08003150 unsigned long long trimmed = 0;
Chao Yuc34f42e2015-12-23 17:50:30 +08003151 int err = 0;
Chao Yub0332a02020-02-14 17:44:12 +08003152 bool need_align = f2fs_lfs_mode(sbi) && __is_large_section(sbi);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003153
Jaegeuk Kim836b5a62015-04-30 22:50:06 -07003154 if (start >= MAX_BLKADDR(sbi) || range->len < sbi->blocksize)
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003155 return -EINVAL;
3156
Chao Yu3f16ecd92018-08-08 17:36:29 +08003157 if (end < MAIN_BLKADDR(sbi))
3158 goto out;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003159
Yunlei Heed214a12016-09-01 10:14:39 +08003160 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003161 f2fs_warn(sbi, "Found FS corruption, run fsck to fix.");
Chao Yu10f966b2019-06-20 11:36:14 +08003162 return -EFSCORRUPTED;
Yunlei Heed214a12016-09-01 10:14:39 +08003163 }
3164
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003165 /* start/end segment number in main_area */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003166 start_segno = (start <= MAIN_BLKADDR(sbi)) ? 0 : GET_SEGNO(sbi, start);
3167 end_segno = (end >= MAX_BLKADDR(sbi)) ? MAIN_SEGS(sbi) - 1 :
3168 GET_SEGNO(sbi, end);
Yunlong Songad6672b2018-07-19 20:58:15 +08003169 if (need_align) {
3170 start_segno = rounddown(start_segno, sbi->segs_per_sec);
3171 end_segno = roundup(end_segno + 1, sbi->segs_per_sec) - 1;
3172 }
Chao Yu84126632017-10-04 09:08:32 +08003173
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003174 cpc.reason = CP_DISCARD;
Jaegeuk Kim836b5a62015-04-30 22:50:06 -07003175 cpc.trim_minlen = max_t(__u64, 1, F2FS_BYTES_TO_BLK(range->minlen));
Chao Yu377224c2018-04-09 10:25:23 +08003176 cpc.trim_start = start_segno;
3177 cpc.trim_end = end_segno;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003178
Chao Yu377224c2018-04-09 10:25:23 +08003179 if (sbi->discard_blks == 0)
3180 goto out;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07003181
Chao Yufb24fea2020-01-14 19:36:50 +08003182 down_write(&sbi->gc_lock);
Chao Yu4d57b862018-05-30 00:20:41 +08003183 err = f2fs_write_checkpoint(sbi, &cpc);
Chao Yufb24fea2020-01-14 19:36:50 +08003184 up_write(&sbi->gc_lock);
Chao Yu377224c2018-04-09 10:25:23 +08003185 if (err)
3186 goto out;
Chao Yu84126632017-10-04 09:08:32 +08003187
Jaegeuk Kime555da92018-05-31 10:20:48 -07003188 /*
3189 * We filed discard candidates, but actually we don't need to wait for
3190 * all of them, since they'll be issued in idle time along with runtime
3191 * discard option. User configuration looks like using runtime discard
3192 * or periodic fstrim instead of it.
3193 */
Chao Yu7d20c8a2018-09-04 03:52:17 +08003194 if (f2fs_realtime_discard_enable(sbi))
Jaegeuk Kim5a615492018-06-20 21:27:21 -07003195 goto out;
3196
3197 start_block = START_BLOCK(sbi, start_segno);
3198 end_block = START_BLOCK(sbi, end_segno + 1);
3199
3200 __init_discard_policy(sbi, &dpolicy, DPOLICY_FSTRIM, cpc.trim_minlen);
Chao Yu01f9cf62018-06-25 20:33:24 +08003201 trimmed = __issue_discard_cmd_range(sbi, &dpolicy,
3202 start_block, end_block);
Jaegeuk Kim5a615492018-06-20 21:27:21 -07003203
Chao Yu01f9cf62018-06-25 20:33:24 +08003204 trimmed += __wait_discard_cmd_range(sbi, &dpolicy,
Chao Yu0ea80512017-10-28 16:52:32 +08003205 start_block, end_block);
Chao Yu377224c2018-04-09 10:25:23 +08003206out:
Chao Yu6eae2692018-08-05 23:09:00 +08003207 if (!err)
3208 range->len = F2FS_BLK_TO_BYTES(trimmed);
Chao Yuc34f42e2015-12-23 17:50:30 +08003209 return err;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003210}
3211
Chao Yu093749e2020-08-04 21:14:49 +08003212static bool __has_curseg_space(struct f2fs_sb_info *sbi,
3213 struct curseg_info *curseg)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003214{
Aravind Rameshde881df2020-07-16 18:26:56 +05303215 return curseg->next_blkoff < f2fs_usable_blks_in_seg(sbi,
3216 curseg->segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003217}
3218
Chao Yu4d57b862018-05-30 00:20:41 +08003219int f2fs_rw_hint_to_seg_type(enum rw_hint hint)
Hyunchul Lee4f0a03d32017-11-09 14:51:27 +09003220{
3221 switch (hint) {
3222 case WRITE_LIFE_SHORT:
3223 return CURSEG_HOT_DATA;
3224 case WRITE_LIFE_EXTREME:
3225 return CURSEG_COLD_DATA;
3226 default:
3227 return CURSEG_WARM_DATA;
3228 }
3229}
3230
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09003231/* This returns write hints for each segment type. This hints will be
3232 * passed down to block layer. There are mapping tables which depend on
3233 * the mount option 'whint_mode'.
3234 *
3235 * 1) whint_mode=off. F2FS only passes down WRITE_LIFE_NOT_SET.
3236 *
3237 * 2) whint_mode=user-based. F2FS tries to pass down hints given by users.
3238 *
3239 * User F2FS Block
3240 * ---- ---- -----
3241 * META WRITE_LIFE_NOT_SET
3242 * HOT_NODE "
3243 * WARM_NODE "
3244 * COLD_NODE "
3245 * ioctl(COLD) COLD_DATA WRITE_LIFE_EXTREME
3246 * extension list " "
3247 *
3248 * -- buffered io
3249 * WRITE_LIFE_EXTREME COLD_DATA WRITE_LIFE_EXTREME
3250 * WRITE_LIFE_SHORT HOT_DATA WRITE_LIFE_SHORT
3251 * WRITE_LIFE_NOT_SET WARM_DATA WRITE_LIFE_NOT_SET
3252 * WRITE_LIFE_NONE " "
3253 * WRITE_LIFE_MEDIUM " "
3254 * WRITE_LIFE_LONG " "
3255 *
3256 * -- direct io
3257 * WRITE_LIFE_EXTREME COLD_DATA WRITE_LIFE_EXTREME
3258 * WRITE_LIFE_SHORT HOT_DATA WRITE_LIFE_SHORT
3259 * WRITE_LIFE_NOT_SET WARM_DATA WRITE_LIFE_NOT_SET
3260 * WRITE_LIFE_NONE " WRITE_LIFE_NONE
3261 * WRITE_LIFE_MEDIUM " WRITE_LIFE_MEDIUM
3262 * WRITE_LIFE_LONG " WRITE_LIFE_LONG
3263 *
Hyunchul Leef2e703f2018-01-31 11:36:58 +09003264 * 3) whint_mode=fs-based. F2FS passes down hints with its policy.
3265 *
3266 * User F2FS Block
3267 * ---- ---- -----
3268 * META WRITE_LIFE_MEDIUM;
3269 * HOT_NODE WRITE_LIFE_NOT_SET
3270 * WARM_NODE "
3271 * COLD_NODE WRITE_LIFE_NONE
3272 * ioctl(COLD) COLD_DATA WRITE_LIFE_EXTREME
3273 * extension list " "
3274 *
3275 * -- buffered io
3276 * WRITE_LIFE_EXTREME COLD_DATA WRITE_LIFE_EXTREME
3277 * WRITE_LIFE_SHORT HOT_DATA WRITE_LIFE_SHORT
3278 * WRITE_LIFE_NOT_SET WARM_DATA WRITE_LIFE_LONG
3279 * WRITE_LIFE_NONE " "
3280 * WRITE_LIFE_MEDIUM " "
3281 * WRITE_LIFE_LONG " "
3282 *
3283 * -- direct io
3284 * WRITE_LIFE_EXTREME COLD_DATA WRITE_LIFE_EXTREME
3285 * WRITE_LIFE_SHORT HOT_DATA WRITE_LIFE_SHORT
3286 * WRITE_LIFE_NOT_SET WARM_DATA WRITE_LIFE_NOT_SET
3287 * WRITE_LIFE_NONE " WRITE_LIFE_NONE
3288 * WRITE_LIFE_MEDIUM " WRITE_LIFE_MEDIUM
3289 * WRITE_LIFE_LONG " WRITE_LIFE_LONG
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09003290 */
3291
Chao Yu4d57b862018-05-30 00:20:41 +08003292enum rw_hint f2fs_io_type_to_rw_hint(struct f2fs_sb_info *sbi,
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09003293 enum page_type type, enum temp_type temp)
3294{
Chao Yu63189b72018-03-08 14:22:56 +08003295 if (F2FS_OPTION(sbi).whint_mode == WHINT_MODE_USER) {
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09003296 if (type == DATA) {
Hyunchul Leef2e703f2018-01-31 11:36:58 +09003297 if (temp == WARM)
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09003298 return WRITE_LIFE_NOT_SET;
Hyunchul Leef2e703f2018-01-31 11:36:58 +09003299 else if (temp == HOT)
3300 return WRITE_LIFE_SHORT;
3301 else if (temp == COLD)
3302 return WRITE_LIFE_EXTREME;
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09003303 } else {
3304 return WRITE_LIFE_NOT_SET;
3305 }
Chao Yu63189b72018-03-08 14:22:56 +08003306 } else if (F2FS_OPTION(sbi).whint_mode == WHINT_MODE_FS) {
Hyunchul Leef2e703f2018-01-31 11:36:58 +09003307 if (type == DATA) {
3308 if (temp == WARM)
3309 return WRITE_LIFE_LONG;
3310 else if (temp == HOT)
3311 return WRITE_LIFE_SHORT;
3312 else if (temp == COLD)
3313 return WRITE_LIFE_EXTREME;
3314 } else if (type == NODE) {
3315 if (temp == WARM || temp == HOT)
3316 return WRITE_LIFE_NOT_SET;
3317 else if (temp == COLD)
3318 return WRITE_LIFE_NONE;
3319 } else if (type == META) {
3320 return WRITE_LIFE_MEDIUM;
3321 }
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09003322 }
Hyunchul Leef2e703f2018-01-31 11:36:58 +09003323 return WRITE_LIFE_NOT_SET;
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09003324}
3325
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07003326static int __get_segment_type_2(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003327{
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07003328 if (fio->type == DATA)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003329 return CURSEG_HOT_DATA;
3330 else
3331 return CURSEG_HOT_NODE;
3332}
3333
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07003334static int __get_segment_type_4(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003335{
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07003336 if (fio->type == DATA) {
3337 struct inode *inode = fio->page->mapping->host;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003338
3339 if (S_ISDIR(inode->i_mode))
3340 return CURSEG_HOT_DATA;
3341 else
3342 return CURSEG_COLD_DATA;
3343 } else {
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07003344 if (IS_DNODE(fio->page) && is_cold_node(fio->page))
Jaegeuk Kima344b9f2014-11-05 20:05:53 -08003345 return CURSEG_WARM_NODE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003346 else
3347 return CURSEG_COLD_NODE;
3348 }
3349}
3350
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07003351static int __get_segment_type_6(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003352{
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07003353 if (fio->type == DATA) {
3354 struct inode *inode = fio->page->mapping->host;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003355
Chao Yu859fca62021-05-26 14:29:27 +08003356 if (is_inode_flag_set(inode, FI_ALIGNED_WRITE))
3357 return CURSEG_COLD_DATA_PINNED;
3358
Chao Yub763f3b2021-04-28 17:20:31 +08003359 if (page_private_gcing(fio->page)) {
Weichao Guoac2d7502021-03-17 17:27:23 +08003360 if (fio->sbi->am.atgc_enabled &&
3361 (fio->io_type == FS_DATA_IO) &&
3362 (fio->sbi->gc_mode != GC_URGENT_HIGH))
Chao Yu093749e2020-08-04 21:14:49 +08003363 return CURSEG_ALL_DATA_ATGC;
3364 else
3365 return CURSEG_COLD_DATA;
3366 }
Daeho Jeong602a16d2020-12-01 13:08:02 +09003367 if (file_is_cold(inode) || f2fs_need_compress_data(inode))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003368 return CURSEG_COLD_DATA;
Chao Yub6a06cb2018-02-28 17:07:27 +08003369 if (file_is_hot(inode) ||
Chao Yub4c3ca82018-04-26 17:05:50 +08003370 is_inode_flag_set(inode, FI_HOT_DATA) ||
Chao Yu2079f112018-07-17 20:41:48 +08003371 f2fs_is_atomic_file(inode) ||
3372 f2fs_is_volatile_file(inode))
Jaegeuk Kimef095d12017-03-24 20:05:13 -04003373 return CURSEG_HOT_DATA;
Chao Yu4d57b862018-05-30 00:20:41 +08003374 return f2fs_rw_hint_to_seg_type(inode->i_write_hint);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003375 } else {
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07003376 if (IS_DNODE(fio->page))
3377 return is_cold_node(fio->page) ? CURSEG_WARM_NODE :
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003378 CURSEG_HOT_NODE;
Jaegeuk Kimef095d12017-03-24 20:05:13 -04003379 return CURSEG_COLD_NODE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003380 }
3381}
3382
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07003383static int __get_segment_type(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003384{
Jaegeuk Kima912b542017-05-10 11:18:25 -07003385 int type = 0;
3386
Chao Yu63189b72018-03-08 14:22:56 +08003387 switch (F2FS_OPTION(fio->sbi).active_logs) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003388 case 2:
Jaegeuk Kima912b542017-05-10 11:18:25 -07003389 type = __get_segment_type_2(fio);
3390 break;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003391 case 4:
Jaegeuk Kima912b542017-05-10 11:18:25 -07003392 type = __get_segment_type_4(fio);
3393 break;
3394 case 6:
3395 type = __get_segment_type_6(fio);
3396 break;
3397 default:
3398 f2fs_bug_on(fio->sbi, true);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003399 }
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07003400
Jaegeuk Kima912b542017-05-10 11:18:25 -07003401 if (IS_HOT(type))
3402 fio->temp = HOT;
3403 else if (IS_WARM(type))
3404 fio->temp = WARM;
3405 else
3406 fio->temp = COLD;
3407 return type;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003408}
3409
Chao Yu4d57b862018-05-30 00:20:41 +08003410void f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09003411 block_t old_blkaddr, block_t *new_blkaddr,
Chao Yufb830fc2017-05-19 23:37:01 +08003412 struct f2fs_summary *sum, int type,
Chao Yu093749e2020-08-04 21:14:49 +08003413 struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003414{
3415 struct sit_info *sit_i = SIT_I(sbi);
Jaegeuk Kim6ae1be12016-11-11 12:31:40 -08003416 struct curseg_info *curseg = CURSEG_I(sbi, type);
Chao Yuc5d02782020-08-04 21:14:47 +08003417 unsigned long long old_mtime;
Chao Yu093749e2020-08-04 21:14:49 +08003418 bool from_gc = (type == CURSEG_ALL_DATA_ATGC);
3419 struct seg_entry *se = NULL;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003420
Chao Yu2b603112017-11-02 20:41:03 +08003421 down_read(&SM_I(sbi)->curseg_lock);
3422
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003423 mutex_lock(&curseg->curseg_mutex);
Chao Yu3d26fa62017-10-30 17:49:53 +08003424 down_write(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003425
Chao Yu093749e2020-08-04 21:14:49 +08003426 if (from_gc) {
3427 f2fs_bug_on(sbi, GET_SEGNO(sbi, old_blkaddr) == NULL_SEGNO);
3428 se = get_seg_entry(sbi, GET_SEGNO(sbi, old_blkaddr));
3429 sanity_check_seg_type(sbi, se->type);
3430 f2fs_bug_on(sbi, IS_NODESEG(se->type));
3431 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003432 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003433
Chao Yu093749e2020-08-04 21:14:49 +08003434 f2fs_bug_on(sbi, curseg->next_blkoff >= sbi->blocks_per_seg);
3435
Jaegeuk Kim4e6a8d92016-12-29 14:07:53 -08003436 f2fs_wait_discard_bio(sbi, *new_blkaddr);
3437
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003438 /*
3439 * __add_sum_entry should be resided under the curseg_mutex
3440 * because, this function updates a summary entry in the
3441 * current summary block.
3442 */
Haicheng Lie79efe32013-06-13 16:59:27 +08003443 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003444
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003445 __refresh_next_blkoff(sbi, curseg);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +09003446
3447 stat_inc_block_count(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003448
Chao Yuc5d02782020-08-04 21:14:47 +08003449 if (from_gc) {
3450 old_mtime = get_segment_mtime(sbi, old_blkaddr);
3451 } else {
3452 update_segment_mtime(sbi, old_blkaddr, 0);
3453 old_mtime = 0;
3454 }
3455 update_segment_mtime(sbi, *new_blkaddr, old_mtime);
3456
Yunlong Song65f1b802017-10-30 09:33:41 +08003457 /*
3458 * SIT information should be updated before segment allocation,
3459 * since SSR needs latest valid block information.
3460 */
3461 update_sit_entry(sbi, *new_blkaddr, 1);
3462 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
3463 update_sit_entry(sbi, old_blkaddr, -1);
3464
Chao Yu093749e2020-08-04 21:14:49 +08003465 if (!__has_curseg_space(sbi, curseg)) {
3466 if (from_gc)
3467 get_atssr_segment(sbi, type, se->type,
3468 AT_SSR, se->mtime);
3469 else
3470 sit_i->s_ops->allocate_segment(sbi, type, false);
3471 }
Jaegeuk Kimc6f82fe92017-04-04 16:45:30 -07003472 /*
Yunlong Song65f1b802017-10-30 09:33:41 +08003473 * segment dirty status should be updated after segment allocation,
3474 * so we just need to update status only one time after previous
3475 * segment being closed.
Jaegeuk Kimc6f82fe92017-04-04 16:45:30 -07003476 */
Yunlong Song65f1b802017-10-30 09:33:41 +08003477 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
3478 locate_dirty_segment(sbi, GET_SEGNO(sbi, *new_blkaddr));
Yunlong Song3436c4b2017-02-21 16:59:26 +08003479
Chao Yu3d26fa62017-10-30 17:49:53 +08003480 up_write(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003481
Chao Yu704956e2017-07-31 20:19:09 +08003482 if (page && IS_NODESEG(type)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003483 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
3484
Chao Yu704956e2017-07-31 20:19:09 +08003485 f2fs_inode_chksum_set(sbi, page);
3486 }
3487
Chao Yuf608c382020-06-18 14:36:24 +08003488 if (fio) {
Chao Yufb830fc2017-05-19 23:37:01 +08003489 struct f2fs_bio_info *io;
3490
Chao Yu25ae8372021-04-02 17:22:23 +08003491 if (F2FS_IO_ALIGNED(sbi))
3492 fio->retry = false;
3493
Chao Yufb830fc2017-05-19 23:37:01 +08003494 INIT_LIST_HEAD(&fio->list);
3495 fio->in_list = true;
3496 io = sbi->write_io[fio->type] + fio->temp;
3497 spin_lock(&io->io_lock);
3498 list_add_tail(&fio->list, &io->io_list);
3499 spin_unlock(&io->io_lock);
3500 }
3501
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09003502 mutex_unlock(&curseg->curseg_mutex);
Chao Yu2b603112017-11-02 20:41:03 +08003503
3504 up_read(&SM_I(sbi)->curseg_lock);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09003505}
3506
Chao Yu39d787b2017-09-29 13:59:38 +08003507static void update_device_state(struct f2fs_io_info *fio)
3508{
3509 struct f2fs_sb_info *sbi = fio->sbi;
3510 unsigned int devidx;
3511
Damien Le Moal09168782019-03-16 09:13:06 +09003512 if (!f2fs_is_multi_device(sbi))
Chao Yu39d787b2017-09-29 13:59:38 +08003513 return;
3514
3515 devidx = f2fs_target_device_index(sbi, fio->new_blkaddr);
3516
3517 /* update device state for fsync */
Chao Yu4d57b862018-05-30 00:20:41 +08003518 f2fs_set_dirty_device(sbi, fio->ino, devidx, FLUSH_INO);
Chao Yu1228b482017-09-29 13:59:39 +08003519
3520 /* update device state for checkpoint */
3521 if (!f2fs_test_bit(devidx, (char *)&sbi->dirty_device)) {
3522 spin_lock(&sbi->dev_lock);
3523 f2fs_set_bit(devidx, (char *)&sbi->dirty_device);
3524 spin_unlock(&sbi->dev_lock);
3525 }
Chao Yu39d787b2017-09-29 13:59:38 +08003526}
3527
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07003528static void do_write_page(struct f2fs_summary *sum, struct f2fs_io_info *fio)
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09003529{
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07003530 int type = __get_segment_type(fio);
Chao Yub0332a02020-02-14 17:44:12 +08003531 bool keep_order = (f2fs_lfs_mode(fio->sbi) && type == CURSEG_COLD_DATA);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09003532
Chao Yu107a8052018-05-26 09:00:13 +08003533 if (keep_order)
3534 down_read(&fio->sbi->io_order_lock);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08003535reallocate:
Chao Yu4d57b862018-05-30 00:20:41 +08003536 f2fs_allocate_data_block(fio->sbi, fio->page, fio->old_blkaddr,
Chao Yu093749e2020-08-04 21:14:49 +08003537 &fio->new_blkaddr, sum, type, fio);
Chao Yu6ce19af2021-05-20 19:51:50 +08003538 if (GET_SEGNO(fio->sbi, fio->old_blkaddr) != NULL_SEGNO) {
Chao Yu6aa58d82018-08-14 22:37:25 +08003539 invalidate_mapping_pages(META_MAPPING(fio->sbi),
3540 fio->old_blkaddr, fio->old_blkaddr);
Chao Yu6ce19af2021-05-20 19:51:50 +08003541 f2fs_invalidate_compress_page(fio->sbi, fio->old_blkaddr);
3542 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09003543
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003544 /* writeout dirty page into bdev */
Chao Yufe16efe2018-05-28 23:47:18 +08003545 f2fs_submit_page_write(fio);
3546 if (fio->retry) {
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08003547 fio->old_blkaddr = fio->new_blkaddr;
3548 goto reallocate;
3549 }
Chao Yufe16efe2018-05-28 23:47:18 +08003550
3551 update_device_state(fio);
3552
Chao Yu107a8052018-05-26 09:00:13 +08003553 if (keep_order)
3554 up_read(&fio->sbi->io_order_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003555}
3556
Chao Yu4d57b862018-05-30 00:20:41 +08003557void f2fs_do_write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
Chao Yub0af6d42017-08-02 23:21:48 +08003558 enum iostat_type io_type)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003559{
Jaegeuk Kim458e6192013-12-11 13:54:01 +09003560 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07003561 .sbi = sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09003562 .type = META,
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09003563 .temp = HOT,
Mike Christie04d328d2016-06-05 14:31:55 -05003564 .op = REQ_OP_WRITE,
Christoph Hellwig70fd7612016-11-01 07:40:10 -06003565 .op_flags = REQ_SYNC | REQ_META | REQ_PRIO,
Chao Yu7a9d7542016-02-22 18:36:38 +08003566 .old_blkaddr = page->index,
3567 .new_blkaddr = page->index,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07003568 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07003569 .encrypted_page = NULL,
Chao Yufb830fc2017-05-19 23:37:01 +08003570 .in_list = false,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09003571 };
3572
Chao Yu2b947002015-10-12 17:04:21 +08003573 if (unlikely(page->index >= MAIN_BLKADDR(sbi)))
Mike Christie04d328d2016-06-05 14:31:55 -05003574 fio.op_flags &= ~REQ_META;
Chao Yu2b947002015-10-12 17:04:21 +08003575
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003576 set_page_writeback(page);
Jaegeuk Kim17c50032018-04-11 23:09:04 -07003577 ClearPageError(page);
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07003578 f2fs_submit_page_write(&fio);
Chao Yub0af6d42017-08-02 23:21:48 +08003579
Chao Yub63e7be2018-09-29 18:31:27 +08003580 stat_inc_meta_count(sbi, page->index);
Chao Yub0af6d42017-08-02 23:21:48 +08003581 f2fs_update_iostat(sbi, io_type, F2FS_BLKSIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003582}
3583
Chao Yu4d57b862018-05-30 00:20:41 +08003584void f2fs_do_write_node_page(unsigned int nid, struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003585{
3586 struct f2fs_summary sum;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07003587
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003588 set_summary(&sum, nid, 0, 0);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07003589 do_write_page(&sum, fio);
Chao Yub0af6d42017-08-02 23:21:48 +08003590
3591 f2fs_update_iostat(fio->sbi, fio->io_type, F2FS_BLKSIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003592}
3593
Chao Yu4d57b862018-05-30 00:20:41 +08003594void f2fs_outplace_write_data(struct dnode_of_data *dn,
3595 struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003596{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07003597 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003598 struct f2fs_summary sum;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003599
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07003600 f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR);
Chao Yu77357302018-07-17 00:02:17 +08003601 set_summary(&sum, dn->nid, dn->ofs_in_node, fio->version);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07003602 do_write_page(&sum, fio);
Chao Yuf28b3432016-02-24 17:16:47 +08003603 f2fs_update_data_blkaddr(dn, fio->new_blkaddr);
Chao Yub0af6d42017-08-02 23:21:48 +08003604
3605 f2fs_update_iostat(sbi, fio->io_type, F2FS_BLKSIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003606}
3607
Chao Yu4d57b862018-05-30 00:20:41 +08003608int f2fs_inplace_write_data(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003609{
Chao Yub0af6d42017-08-02 23:21:48 +08003610 int err;
Yunlei Hed21b0f22018-03-26 17:32:23 +08003611 struct f2fs_sb_info *sbi = fio->sbi;
Chao Yu05573d62019-04-15 15:30:52 +08003612 unsigned int segno;
Chao Yub0af6d42017-08-02 23:21:48 +08003613
Chao Yu7a9d7542016-02-22 18:36:38 +08003614 fio->new_blkaddr = fio->old_blkaddr;
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09003615 /* i/o temperature is needed for passing down write hints */
3616 __get_segment_type(fio);
Yunlei Hed21b0f22018-03-26 17:32:23 +08003617
Chao Yu05573d62019-04-15 15:30:52 +08003618 segno = GET_SEGNO(sbi, fio->new_blkaddr);
3619
3620 if (!IS_DATASEG(get_seg_entry(sbi, segno)->type)) {
3621 set_sbi_flag(sbi, SBI_NEED_FSCK);
Chao Yu2d821c12019-06-18 17:59:03 +08003622 f2fs_warn(sbi, "%s: incorrect segment(%u) type, run fsck to fix.",
3623 __func__, segno);
Chao Yu95577272021-04-22 18:19:25 +08003624 err = -EFSCORRUPTED;
3625 goto drop_bio;
3626 }
3627
Jaegeuk Kim1ffc8f52021-07-14 16:14:02 -07003628 if (f2fs_cp_error(sbi)) {
Chao Yu95577272021-04-22 18:19:25 +08003629 err = -EIO;
3630 goto drop_bio;
Chao Yu05573d62019-04-15 15:30:52 +08003631 }
Yunlei Hed21b0f22018-03-26 17:32:23 +08003632
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07003633 stat_inc_inplace_blocks(fio->sbi);
Chao Yub0af6d42017-08-02 23:21:48 +08003634
Jaegeuk Kim0e7f41972020-01-06 16:43:09 -08003635 if (fio->bio && !(SM_I(sbi)->ipu_policy & (1 << F2FS_IPU_NOCACHE)))
Chao Yu8648de22019-02-19 16:15:29 +08003636 err = f2fs_merge_page_bio(fio);
3637 else
3638 err = f2fs_submit_page_bio(fio);
Chao Yue46f6bd2019-02-21 20:40:13 +08003639 if (!err) {
Chao Yu39d787b2017-09-29 13:59:38 +08003640 update_device_state(fio);
Chao Yue46f6bd2019-02-21 20:40:13 +08003641 f2fs_update_iostat(fio->sbi, fio->io_type, F2FS_BLKSIZE);
3642 }
Chao Yub0af6d42017-08-02 23:21:48 +08003643
3644 return err;
Chao Yu95577272021-04-22 18:19:25 +08003645drop_bio:
Jaegeuk Kim349c4d62021-05-09 21:53:03 -07003646 if (fio->bio && *(fio->bio)) {
Chao Yu95577272021-04-22 18:19:25 +08003647 struct bio *bio = *(fio->bio);
3648
3649 bio->bi_status = BLK_STS_IOERR;
3650 bio_endio(bio);
Jaegeuk Kim349c4d62021-05-09 21:53:03 -07003651 *(fio->bio) = NULL;
Chao Yu95577272021-04-22 18:19:25 +08003652 }
3653 return err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003654}
3655
Chao Yu2b603112017-11-02 20:41:03 +08003656static inline int __f2fs_get_curseg(struct f2fs_sb_info *sbi,
3657 unsigned int segno)
3658{
3659 int i;
3660
3661 for (i = CURSEG_HOT_DATA; i < NO_CHECK_TYPE; i++) {
3662 if (CURSEG_I(sbi, i)->segno == segno)
3663 break;
3664 }
3665 return i;
3666}
3667
Chao Yu4d57b862018-05-30 00:20:41 +08003668void f2fs_do_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
Chao Yu19f106b2015-05-06 13:08:06 +08003669 block_t old_blkaddr, block_t new_blkaddr,
Chao Yuc5d02782020-08-04 21:14:47 +08003670 bool recover_curseg, bool recover_newaddr,
3671 bool from_gc)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003672{
3673 struct sit_info *sit_i = SIT_I(sbi);
3674 struct curseg_info *curseg;
3675 unsigned int segno, old_cursegno;
3676 struct seg_entry *se;
3677 int type;
Chao Yu19f106b2015-05-06 13:08:06 +08003678 unsigned short old_blkoff;
Wang Xiaojun753a8ed2021-03-25 10:19:20 -04003679 unsigned char old_alloc_type;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003680
3681 segno = GET_SEGNO(sbi, new_blkaddr);
3682 se = get_seg_entry(sbi, segno);
3683 type = se->type;
3684
Chao Yu2b603112017-11-02 20:41:03 +08003685 down_write(&SM_I(sbi)->curseg_lock);
3686
Chao Yu19f106b2015-05-06 13:08:06 +08003687 if (!recover_curseg) {
3688 /* for recovery flow */
3689 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
3690 if (old_blkaddr == NULL_ADDR)
3691 type = CURSEG_COLD_DATA;
3692 else
3693 type = CURSEG_WARM_DATA;
3694 }
3695 } else {
Chao Yu2b603112017-11-02 20:41:03 +08003696 if (IS_CURSEG(sbi, segno)) {
3697 /* se->type is volatile as SSR allocation */
3698 type = __f2fs_get_curseg(sbi, segno);
3699 f2fs_bug_on(sbi, type == NO_CHECK_TYPE);
3700 } else {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003701 type = CURSEG_WARM_DATA;
Chao Yu2b603112017-11-02 20:41:03 +08003702 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003703 }
Chao Yu19f106b2015-05-06 13:08:06 +08003704
Yunlong Song2c190502018-01-04 15:02:02 +08003705 f2fs_bug_on(sbi, !IS_DATASEG(type));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003706 curseg = CURSEG_I(sbi, type);
3707
3708 mutex_lock(&curseg->curseg_mutex);
Chao Yu3d26fa62017-10-30 17:49:53 +08003709 down_write(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003710
3711 old_cursegno = curseg->segno;
Chao Yu19f106b2015-05-06 13:08:06 +08003712 old_blkoff = curseg->next_blkoff;
Wang Xiaojun753a8ed2021-03-25 10:19:20 -04003713 old_alloc_type = curseg->alloc_type;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003714
3715 /* change the current segment */
3716 if (segno != curseg->segno) {
3717 curseg->next_segno = segno;
Chao Yu093749e2020-08-04 21:14:49 +08003718 change_curseg(sbi, type, true);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003719 }
3720
Jaegeuk Kim491c0852014-02-04 13:01:10 +09003721 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
Haicheng Lie79efe32013-06-13 16:59:27 +08003722 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003723
Chao Yuc5d02782020-08-04 21:14:47 +08003724 if (!recover_curseg || recover_newaddr) {
3725 if (!from_gc)
3726 update_segment_mtime(sbi, new_blkaddr, 0);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07003727 update_sit_entry(sbi, new_blkaddr, 1);
Chao Yuc5d02782020-08-04 21:14:47 +08003728 }
Chao Yu6aa58d82018-08-14 22:37:25 +08003729 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO) {
3730 invalidate_mapping_pages(META_MAPPING(sbi),
3731 old_blkaddr, old_blkaddr);
Chao Yu6ce19af2021-05-20 19:51:50 +08003732 f2fs_invalidate_compress_page(sbi, old_blkaddr);
Chao Yuc5d02782020-08-04 21:14:47 +08003733 if (!from_gc)
3734 update_segment_mtime(sbi, old_blkaddr, 0);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07003735 update_sit_entry(sbi, old_blkaddr, -1);
Chao Yu6aa58d82018-08-14 22:37:25 +08003736 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07003737
3738 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
3739 locate_dirty_segment(sbi, GET_SEGNO(sbi, new_blkaddr));
3740
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003741 locate_dirty_segment(sbi, old_cursegno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003742
Chao Yu19f106b2015-05-06 13:08:06 +08003743 if (recover_curseg) {
3744 if (old_cursegno != curseg->segno) {
3745 curseg->next_segno = old_cursegno;
Chao Yu093749e2020-08-04 21:14:49 +08003746 change_curseg(sbi, type, true);
Chao Yu19f106b2015-05-06 13:08:06 +08003747 }
3748 curseg->next_blkoff = old_blkoff;
Wang Xiaojun753a8ed2021-03-25 10:19:20 -04003749 curseg->alloc_type = old_alloc_type;
Chao Yu19f106b2015-05-06 13:08:06 +08003750 }
3751
Chao Yu3d26fa62017-10-30 17:49:53 +08003752 up_write(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003753 mutex_unlock(&curseg->curseg_mutex);
Chao Yu2b603112017-11-02 20:41:03 +08003754 up_write(&SM_I(sbi)->curseg_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003755}
3756
Chao Yu528e3452015-05-28 19:15:35 +08003757void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
3758 block_t old_addr, block_t new_addr,
Chao Yu28bc1062016-02-06 14:40:34 +08003759 unsigned char version, bool recover_curseg,
3760 bool recover_newaddr)
Chao Yu528e3452015-05-28 19:15:35 +08003761{
3762 struct f2fs_summary sum;
3763
3764 set_summary(&sum, dn->nid, dn->ofs_in_node, version);
3765
Chao Yu4d57b862018-05-30 00:20:41 +08003766 f2fs_do_replace_block(sbi, &sum, old_addr, new_addr,
Chao Yuc5d02782020-08-04 21:14:47 +08003767 recover_curseg, recover_newaddr, false);
Chao Yu528e3452015-05-28 19:15:35 +08003768
Chao Yuf28b3432016-02-24 17:16:47 +08003769 f2fs_update_data_blkaddr(dn, new_addr);
Chao Yu528e3452015-05-28 19:15:35 +08003770}
3771
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09003772void f2fs_wait_on_page_writeback(struct page *page,
Chao Yubae0ee72018-12-25 17:43:42 +08003773 enum page_type type, bool ordered, bool locked)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09003774{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09003775 if (PageWriteback(page)) {
Jaegeuk Kim40813632014-09-02 15:31:18 -07003776 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
3777
Chao Yu0b20fce2019-09-30 18:53:25 +08003778 /* submit cached LFS IO */
Chao Yubab475c2018-09-27 23:41:16 +08003779 f2fs_submit_merged_write_cond(sbi, NULL, page, 0, type);
Chao Yu0b20fce2019-09-30 18:53:25 +08003780 /* sbumit cached IPU IO */
3781 f2fs_submit_merged_ipu_write(sbi, NULL, page);
Chao Yubae0ee72018-12-25 17:43:42 +08003782 if (ordered) {
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08003783 wait_on_page_writeback(page);
Chao Yubae0ee72018-12-25 17:43:42 +08003784 f2fs_bug_on(sbi, locked && PageWriteback(page));
3785 } else {
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08003786 wait_for_stable_page(page);
Chao Yubae0ee72018-12-25 17:43:42 +08003787 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09003788 }
3789}
3790
Jaegeuk Kim0ded69f2018-08-22 21:18:00 -07003791void f2fs_wait_on_block_writeback(struct inode *inode, block_t blkaddr)
Chao Yu08b39fb2015-10-08 13:27:34 +08003792{
Jaegeuk Kim0ded69f2018-08-22 21:18:00 -07003793 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu08b39fb2015-10-08 13:27:34 +08003794 struct page *cpage;
3795
Jaegeuk Kim0ded69f2018-08-22 21:18:00 -07003796 if (!f2fs_post_read_required(inode))
3797 return;
3798
Chao Yu93770ab2019-04-15 15:26:32 +08003799 if (!__is_valid_data_blkaddr(blkaddr))
Chao Yu08b39fb2015-10-08 13:27:34 +08003800 return;
3801
Chao Yu08b39fb2015-10-08 13:27:34 +08003802 cpage = find_lock_page(META_MAPPING(sbi), blkaddr);
3803 if (cpage) {
Chao Yubae0ee72018-12-25 17:43:42 +08003804 f2fs_wait_on_page_writeback(cpage, DATA, true, true);
Chao Yu08b39fb2015-10-08 13:27:34 +08003805 f2fs_put_page(cpage, 1);
3806 }
3807}
3808
Sahitya Tummala1e78e8b2018-10-10 10:56:22 +05303809void f2fs_wait_on_block_writeback_range(struct inode *inode, block_t blkaddr,
3810 block_t len)
3811{
3812 block_t i;
3813
3814 for (i = 0; i < len; i++)
3815 f2fs_wait_on_block_writeback(inode, blkaddr + i);
3816}
3817
Chao Yu77357302018-07-17 00:02:17 +08003818static int read_compacted_summaries(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003819{
3820 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
3821 struct curseg_info *seg_i;
3822 unsigned char *kaddr;
3823 struct page *page;
3824 block_t start;
3825 int i, j, offset;
3826
3827 start = start_sum_block(sbi);
3828
Chao Yu4d57b862018-05-30 00:20:41 +08003829 page = f2fs_get_meta_page(sbi, start++);
Chao Yu77357302018-07-17 00:02:17 +08003830 if (IS_ERR(page))
3831 return PTR_ERR(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003832 kaddr = (unsigned char *)page_address(page);
3833
3834 /* Step 1: restore nat cache */
3835 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08003836 memcpy(seg_i->journal, kaddr, SUM_JOURNAL_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003837
3838 /* Step 2: restore sit cache */
3839 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08003840 memcpy(seg_i->journal, kaddr + SUM_JOURNAL_SIZE, SUM_JOURNAL_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003841 offset = 2 * SUM_JOURNAL_SIZE;
3842
3843 /* Step 3: restore summary entries */
3844 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
3845 unsigned short blk_off;
3846 unsigned int segno;
3847
3848 seg_i = CURSEG_I(sbi, i);
3849 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
3850 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
3851 seg_i->next_segno = segno;
3852 reset_curseg(sbi, i, 0);
3853 seg_i->alloc_type = ckpt->alloc_type[i];
3854 seg_i->next_blkoff = blk_off;
3855
3856 if (seg_i->alloc_type == SSR)
3857 blk_off = sbi->blocks_per_seg;
3858
3859 for (j = 0; j < blk_off; j++) {
3860 struct f2fs_summary *s;
Yi Zhuang5f029c02021-04-06 09:47:35 +08003861
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003862 s = (struct f2fs_summary *)(kaddr + offset);
3863 seg_i->sum_blk->entries[j] = *s;
3864 offset += SUMMARY_SIZE;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003865 if (offset + SUMMARY_SIZE <= PAGE_SIZE -
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003866 SUM_FOOTER_SIZE)
3867 continue;
3868
3869 f2fs_put_page(page, 1);
3870 page = NULL;
3871
Chao Yu4d57b862018-05-30 00:20:41 +08003872 page = f2fs_get_meta_page(sbi, start++);
Chao Yu77357302018-07-17 00:02:17 +08003873 if (IS_ERR(page))
3874 return PTR_ERR(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003875 kaddr = (unsigned char *)page_address(page);
3876 offset = 0;
3877 }
3878 }
3879 f2fs_put_page(page, 1);
Chao Yu77357302018-07-17 00:02:17 +08003880 return 0;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003881}
3882
3883static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
3884{
3885 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
3886 struct f2fs_summary_block *sum;
3887 struct curseg_info *curseg;
3888 struct page *new;
3889 unsigned short blk_off;
3890 unsigned int segno = 0;
3891 block_t blk_addr = 0;
Chao Yu77357302018-07-17 00:02:17 +08003892 int err = 0;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003893
3894 /* get segment number and block addr */
3895 if (IS_DATASEG(type)) {
3896 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
3897 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
3898 CURSEG_HOT_DATA]);
Jaegeuk Kim119ee912015-01-29 11:45:33 -08003899 if (__exist_node_summaries(sbi))
Chao Yud0b9e422020-08-04 21:14:45 +08003900 blk_addr = sum_blk_addr(sbi, NR_CURSEG_PERSIST_TYPE, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003901 else
3902 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
3903 } else {
3904 segno = le32_to_cpu(ckpt->cur_node_segno[type -
3905 CURSEG_HOT_NODE]);
3906 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
3907 CURSEG_HOT_NODE]);
Jaegeuk Kim119ee912015-01-29 11:45:33 -08003908 if (__exist_node_summaries(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003909 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
3910 type - CURSEG_HOT_NODE);
3911 else
3912 blk_addr = GET_SUM_BLOCK(sbi, segno);
3913 }
3914
Chao Yu4d57b862018-05-30 00:20:41 +08003915 new = f2fs_get_meta_page(sbi, blk_addr);
Chao Yu77357302018-07-17 00:02:17 +08003916 if (IS_ERR(new))
3917 return PTR_ERR(new);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003918 sum = (struct f2fs_summary_block *)page_address(new);
3919
3920 if (IS_NODESEG(type)) {
Jaegeuk Kim119ee912015-01-29 11:45:33 -08003921 if (__exist_node_summaries(sbi)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003922 struct f2fs_summary *ns = &sum->entries[0];
3923 int i;
Yi Zhuang5f029c02021-04-06 09:47:35 +08003924
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003925 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
3926 ns->version = 0;
3927 ns->ofs_in_node = 0;
3928 }
3929 } else {
Chao Yu77357302018-07-17 00:02:17 +08003930 err = f2fs_restore_node_summary(sbi, segno, sum);
3931 if (err)
3932 goto out;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003933 }
3934 }
3935
3936 /* set uncompleted segment to curseg */
3937 curseg = CURSEG_I(sbi, type);
3938 mutex_lock(&curseg->curseg_mutex);
Chao Yub7ad7512016-02-19 18:08:46 +08003939
3940 /* update journal info */
3941 down_write(&curseg->journal_rwsem);
3942 memcpy(curseg->journal, &sum->journal, SUM_JOURNAL_SIZE);
3943 up_write(&curseg->journal_rwsem);
3944
3945 memcpy(curseg->sum_blk->entries, sum->entries, SUM_ENTRY_SIZE);
3946 memcpy(&curseg->sum_blk->footer, &sum->footer, SUM_FOOTER_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003947 curseg->next_segno = segno;
3948 reset_curseg(sbi, type, 0);
3949 curseg->alloc_type = ckpt->alloc_type[type];
3950 curseg->next_blkoff = blk_off;
3951 mutex_unlock(&curseg->curseg_mutex);
Chao Yu77357302018-07-17 00:02:17 +08003952out:
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003953 f2fs_put_page(new, 1);
Chao Yu77357302018-07-17 00:02:17 +08003954 return err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003955}
3956
3957static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
3958{
Jin Qian21d3f8e2017-06-01 11:18:30 -07003959 struct f2fs_journal *sit_j = CURSEG_I(sbi, CURSEG_COLD_DATA)->journal;
3960 struct f2fs_journal *nat_j = CURSEG_I(sbi, CURSEG_HOT_DATA)->journal;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003961 int type = CURSEG_HOT_DATA;
Chao Yue4fc5fb2014-03-17 16:36:24 +08003962 int err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003963
Chao Yuaaec2b12016-09-20 11:04:18 +08003964 if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG)) {
Chao Yu4d57b862018-05-30 00:20:41 +08003965 int npages = f2fs_npages_for_summary_flush(sbi, true);
Chao Yu3fa06d72014-12-09 14:21:46 +08003966
3967 if (npages >= 2)
Chao Yu4d57b862018-05-30 00:20:41 +08003968 f2fs_ra_meta_pages(sbi, start_sum_block(sbi), npages,
Chao Yu26879fb2015-10-12 17:05:59 +08003969 META_CP, true);
Chao Yu3fa06d72014-12-09 14:21:46 +08003970
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003971 /* restore for compacted data summary */
Chao Yu77357302018-07-17 00:02:17 +08003972 err = read_compacted_summaries(sbi);
3973 if (err)
3974 return err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003975 type = CURSEG_HOT_NODE;
3976 }
3977
Jaegeuk Kim119ee912015-01-29 11:45:33 -08003978 if (__exist_node_summaries(sbi))
Chao Yud0b9e422020-08-04 21:14:45 +08003979 f2fs_ra_meta_pages(sbi,
3980 sum_blk_addr(sbi, NR_CURSEG_PERSIST_TYPE, type),
3981 NR_CURSEG_PERSIST_TYPE - type, META_CP, true);
Chao Yu3fa06d72014-12-09 14:21:46 +08003982
Chao Yue4fc5fb2014-03-17 16:36:24 +08003983 for (; type <= CURSEG_COLD_NODE; type++) {
3984 err = read_normal_summaries(sbi, type);
3985 if (err)
3986 return err;
3987 }
3988
Jin Qian21d3f8e2017-06-01 11:18:30 -07003989 /* sanity check for summary blocks */
3990 if (nats_in_cursum(nat_j) > NAT_JOURNAL_ENTRIES ||
Sahitya Tummala9227d522019-05-23 09:49:17 +05303991 sits_in_cursum(sit_j) > SIT_JOURNAL_ENTRIES) {
Joe Perches833dcd32021-05-26 13:05:36 -07003992 f2fs_err(sbi, "invalid journal entries nats %u sits %u",
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003993 nats_in_cursum(nat_j), sits_in_cursum(sit_j));
Jin Qian21d3f8e2017-06-01 11:18:30 -07003994 return -EINVAL;
Sahitya Tummala9227d522019-05-23 09:49:17 +05303995 }
Jin Qian21d3f8e2017-06-01 11:18:30 -07003996
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003997 return 0;
3998}
3999
4000static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
4001{
4002 struct page *page;
4003 unsigned char *kaddr;
4004 struct f2fs_summary *summary;
4005 struct curseg_info *seg_i;
4006 int written_size = 0;
4007 int i, j;
4008
Chao Yu4d57b862018-05-30 00:20:41 +08004009 page = f2fs_grab_meta_page(sbi, blkaddr++);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004010 kaddr = (unsigned char *)page_address(page);
Chao Yu81114ba2018-04-09 20:25:06 +08004011 memset(kaddr, 0, PAGE_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004012
4013 /* Step 1: write nat cache */
4014 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08004015 memcpy(kaddr, seg_i->journal, SUM_JOURNAL_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004016 written_size += SUM_JOURNAL_SIZE;
4017
4018 /* Step 2: write sit cache */
4019 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08004020 memcpy(kaddr + written_size, seg_i->journal, SUM_JOURNAL_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004021 written_size += SUM_JOURNAL_SIZE;
4022
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004023 /* Step 3: write summary entries */
4024 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
4025 unsigned short blkoff;
Yi Zhuang5f029c02021-04-06 09:47:35 +08004026
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004027 seg_i = CURSEG_I(sbi, i);
4028 if (sbi->ckpt->alloc_type[i] == SSR)
4029 blkoff = sbi->blocks_per_seg;
4030 else
4031 blkoff = curseg_blkoff(sbi, i);
4032
4033 for (j = 0; j < blkoff; j++) {
4034 if (!page) {
Chao Yu4d57b862018-05-30 00:20:41 +08004035 page = f2fs_grab_meta_page(sbi, blkaddr++);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004036 kaddr = (unsigned char *)page_address(page);
Chao Yu81114ba2018-04-09 20:25:06 +08004037 memset(kaddr, 0, PAGE_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004038 written_size = 0;
4039 }
4040 summary = (struct f2fs_summary *)(kaddr + written_size);
4041 *summary = seg_i->sum_blk->entries[j];
4042 written_size += SUMMARY_SIZE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004043
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004044 if (written_size + SUMMARY_SIZE <= PAGE_SIZE -
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004045 SUM_FOOTER_SIZE)
4046 continue;
4047
Chao Yue8d61a72013-10-24 15:08:28 +08004048 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004049 f2fs_put_page(page, 1);
4050 page = NULL;
4051 }
4052 }
Chao Yue8d61a72013-10-24 15:08:28 +08004053 if (page) {
4054 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004055 f2fs_put_page(page, 1);
Chao Yue8d61a72013-10-24 15:08:28 +08004056 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004057}
4058
4059static void write_normal_summaries(struct f2fs_sb_info *sbi,
4060 block_t blkaddr, int type)
4061{
4062 int i, end;
Yi Zhuang5f029c02021-04-06 09:47:35 +08004063
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004064 if (IS_DATASEG(type))
4065 end = type + NR_CURSEG_DATA_TYPE;
4066 else
4067 end = type + NR_CURSEG_NODE_TYPE;
4068
Chao Yub7ad7512016-02-19 18:08:46 +08004069 for (i = type; i < end; i++)
4070 write_current_sum_page(sbi, i, blkaddr + (i - type));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004071}
4072
Chao Yu4d57b862018-05-30 00:20:41 +08004073void f2fs_write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004074{
Chao Yuaaec2b12016-09-20 11:04:18 +08004075 if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004076 write_compacted_summaries(sbi, start_blk);
4077 else
4078 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
4079}
4080
Chao Yu4d57b862018-05-30 00:20:41 +08004081void f2fs_write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004082{
Jaegeuk Kim119ee912015-01-29 11:45:33 -08004083 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004084}
4085
Chao Yu4d57b862018-05-30 00:20:41 +08004086int f2fs_lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004087 unsigned int val, int alloc)
4088{
4089 int i;
4090
4091 if (type == NAT_JOURNAL) {
Chao Yudfc08a12016-02-14 18:50:40 +08004092 for (i = 0; i < nats_in_cursum(journal); i++) {
4093 if (le32_to_cpu(nid_in_journal(journal, i)) == val)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004094 return i;
4095 }
Chao Yudfc08a12016-02-14 18:50:40 +08004096 if (alloc && __has_cursum_space(journal, 1, NAT_JOURNAL))
4097 return update_nats_in_cursum(journal, 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004098 } else if (type == SIT_JOURNAL) {
Chao Yudfc08a12016-02-14 18:50:40 +08004099 for (i = 0; i < sits_in_cursum(journal); i++)
4100 if (le32_to_cpu(segno_in_journal(journal, i)) == val)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004101 return i;
Chao Yudfc08a12016-02-14 18:50:40 +08004102 if (alloc && __has_cursum_space(journal, 1, SIT_JOURNAL))
4103 return update_sits_in_cursum(journal, 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004104 }
4105 return -1;
4106}
4107
4108static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
4109 unsigned int segno)
4110{
Jaegeuk Kim86f33602020-10-02 14:17:35 -07004111 return f2fs_get_meta_page(sbi, current_sit_addr(sbi, segno));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004112}
4113
4114static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
4115 unsigned int start)
4116{
4117 struct sit_info *sit_i = SIT_I(sbi);
Yunlei He068c3cd2018-01-25 17:27:11 +08004118 struct page *page;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004119 pgoff_t src_off, dst_off;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004120
4121 src_off = current_sit_addr(sbi, start);
4122 dst_off = next_sit_addr(sbi, src_off);
4123
Chao Yu4d57b862018-05-30 00:20:41 +08004124 page = f2fs_grab_meta_page(sbi, dst_off);
Yunlei He068c3cd2018-01-25 17:27:11 +08004125 seg_info_to_sit_page(sbi, page, start);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004126
Yunlei He068c3cd2018-01-25 17:27:11 +08004127 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004128 set_to_next_sit(sit_i, start);
4129
Yunlei He068c3cd2018-01-25 17:27:11 +08004130 return page;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004131}
4132
Chao Yu184a5cd2014-09-04 18:13:01 +08004133static struct sit_entry_set *grab_sit_entry_set(void)
4134{
4135 struct sit_entry_set *ses =
Chao Yu32410572021-08-09 08:24:48 +08004136 f2fs_kmem_cache_alloc(sit_entry_set_slab,
4137 GFP_NOFS, true, NULL);
Chao Yu184a5cd2014-09-04 18:13:01 +08004138
4139 ses->entry_cnt = 0;
4140 INIT_LIST_HEAD(&ses->set_list);
4141 return ses;
4142}
4143
4144static void release_sit_entry_set(struct sit_entry_set *ses)
4145{
4146 list_del(&ses->set_list);
4147 kmem_cache_free(sit_entry_set_slab, ses);
4148}
4149
4150static void adjust_sit_entry_set(struct sit_entry_set *ses,
4151 struct list_head *head)
4152{
4153 struct sit_entry_set *next = ses;
4154
4155 if (list_is_last(&ses->set_list, head))
4156 return;
4157
4158 list_for_each_entry_continue(next, head, set_list)
4159 if (ses->entry_cnt <= next->entry_cnt)
4160 break;
4161
4162 list_move_tail(&ses->set_list, &next->set_list);
4163}
4164
4165static void add_sit_entry(unsigned int segno, struct list_head *head)
4166{
4167 struct sit_entry_set *ses;
4168 unsigned int start_segno = START_SEGNO(segno);
4169
4170 list_for_each_entry(ses, head, set_list) {
4171 if (ses->start_segno == start_segno) {
4172 ses->entry_cnt++;
4173 adjust_sit_entry_set(ses, head);
4174 return;
4175 }
4176 }
4177
4178 ses = grab_sit_entry_set();
4179
4180 ses->start_segno = start_segno;
4181 ses->entry_cnt++;
4182 list_add(&ses->set_list, head);
4183}
4184
4185static void add_sits_in_set(struct f2fs_sb_info *sbi)
4186{
4187 struct f2fs_sm_info *sm_info = SM_I(sbi);
4188 struct list_head *set_list = &sm_info->sit_entry_set;
4189 unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap;
Chao Yu184a5cd2014-09-04 18:13:01 +08004190 unsigned int segno;
4191
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07004192 for_each_set_bit(segno, bitmap, MAIN_SEGS(sbi))
Chao Yu184a5cd2014-09-04 18:13:01 +08004193 add_sit_entry(segno, set_list);
4194}
4195
4196static void remove_sits_in_journal(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004197{
4198 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08004199 struct f2fs_journal *journal = curseg->journal;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004200 int i;
4201
Chao Yub7ad7512016-02-19 18:08:46 +08004202 down_write(&curseg->journal_rwsem);
Chao Yudfc08a12016-02-14 18:50:40 +08004203 for (i = 0; i < sits_in_cursum(journal); i++) {
Chao Yu184a5cd2014-09-04 18:13:01 +08004204 unsigned int segno;
4205 bool dirtied;
4206
Chao Yudfc08a12016-02-14 18:50:40 +08004207 segno = le32_to_cpu(segno_in_journal(journal, i));
Chao Yu184a5cd2014-09-04 18:13:01 +08004208 dirtied = __mark_sit_entry_dirty(sbi, segno);
4209
4210 if (!dirtied)
4211 add_sit_entry(segno, &SM_I(sbi)->sit_entry_set);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004212 }
Chao Yudfc08a12016-02-14 18:50:40 +08004213 update_sits_in_cursum(journal, -i);
Chao Yub7ad7512016-02-19 18:08:46 +08004214 up_write(&curseg->journal_rwsem);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004215}
4216
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09004217/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004218 * CP calls this function, which flushes SIT entries including sit_journal,
4219 * and moves prefree segs to free segs.
4220 */
Chao Yu4d57b862018-05-30 00:20:41 +08004221void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004222{
4223 struct sit_info *sit_i = SIT_I(sbi);
4224 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
4225 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08004226 struct f2fs_journal *journal = curseg->journal;
Chao Yu184a5cd2014-09-04 18:13:01 +08004227 struct sit_entry_set *ses, *tmp;
4228 struct list_head *head = &SM_I(sbi)->sit_entry_set;
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08004229 bool to_journal = !is_sbi_flag_set(sbi, SBI_IS_RESIZEFS);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07004230 struct seg_entry *se;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004231
Chao Yu3d26fa62017-10-30 17:49:53 +08004232 down_write(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004233
Wanpeng Li2b11a742015-02-27 16:52:50 +08004234 if (!sit_i->dirty_sentries)
4235 goto out;
4236
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004237 /*
Chao Yu184a5cd2014-09-04 18:13:01 +08004238 * add and account sit entries of dirty bitmap in sit entry
4239 * set temporarily
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004240 */
Chao Yu184a5cd2014-09-04 18:13:01 +08004241 add_sits_in_set(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004242
Chao Yu184a5cd2014-09-04 18:13:01 +08004243 /*
4244 * if there are no enough space in journal to store dirty sit
4245 * entries, remove all entries from journal and add and account
4246 * them in sit entry set.
4247 */
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08004248 if (!__has_cursum_space(journal, sit_i->dirty_sentries, SIT_JOURNAL) ||
4249 !to_journal)
Chao Yu184a5cd2014-09-04 18:13:01 +08004250 remove_sits_in_journal(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004251
Chao Yu184a5cd2014-09-04 18:13:01 +08004252 /*
4253 * there are two steps to flush sit entries:
4254 * #1, flush sit entries to journal in current cold data summary block.
4255 * #2, flush sit entries to sit page.
4256 */
4257 list_for_each_entry_safe(ses, tmp, head, set_list) {
Jaegeuk Kim4a257ed2014-10-16 11:43:30 -07004258 struct page *page = NULL;
Chao Yu184a5cd2014-09-04 18:13:01 +08004259 struct f2fs_sit_block *raw_sit = NULL;
4260 unsigned int start_segno = ses->start_segno;
4261 unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07004262 (unsigned long)MAIN_SEGS(sbi));
Chao Yu184a5cd2014-09-04 18:13:01 +08004263 unsigned int segno = start_segno;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09004264
Chao Yu184a5cd2014-09-04 18:13:01 +08004265 if (to_journal &&
Chao Yudfc08a12016-02-14 18:50:40 +08004266 !__has_cursum_space(journal, ses->entry_cnt, SIT_JOURNAL))
Chao Yu184a5cd2014-09-04 18:13:01 +08004267 to_journal = false;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004268
Chao Yub7ad7512016-02-19 18:08:46 +08004269 if (to_journal) {
4270 down_write(&curseg->journal_rwsem);
4271 } else {
Chao Yu184a5cd2014-09-04 18:13:01 +08004272 page = get_next_sit_page(sbi, start_segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004273 raw_sit = page_address(page);
4274 }
4275
Chao Yu184a5cd2014-09-04 18:13:01 +08004276 /* flush dirty sit entries in region of current sit set */
4277 for_each_set_bit_from(segno, bitmap, end) {
4278 int offset, sit_offset;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07004279
4280 se = get_seg_entry(sbi, segno);
Zhikang Zhang56b07e72018-04-09 04:28:41 +08004281#ifdef CONFIG_F2FS_CHECK_FS
4282 if (memcmp(se->cur_valid_map, se->cur_valid_map_mir,
4283 SIT_VBLOCK_MAP_SIZE))
4284 f2fs_bug_on(sbi, 1);
4285#endif
Chao Yu184a5cd2014-09-04 18:13:01 +08004286
4287 /* add discard candidates */
Chao Yuc473f1a2017-04-27 20:40:39 +08004288 if (!(cpc->reason & CP_DISCARD)) {
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07004289 cpc->trim_start = segno;
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08004290 add_discard_addrs(sbi, cpc, false);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07004291 }
Chao Yu184a5cd2014-09-04 18:13:01 +08004292
4293 if (to_journal) {
Chao Yu4d57b862018-05-30 00:20:41 +08004294 offset = f2fs_lookup_journal_in_cursum(journal,
Chao Yu184a5cd2014-09-04 18:13:01 +08004295 SIT_JOURNAL, segno, 1);
4296 f2fs_bug_on(sbi, offset < 0);
Chao Yudfc08a12016-02-14 18:50:40 +08004297 segno_in_journal(journal, offset) =
Chao Yu184a5cd2014-09-04 18:13:01 +08004298 cpu_to_le32(segno);
4299 seg_info_to_raw_sit(se,
Chao Yudfc08a12016-02-14 18:50:40 +08004300 &sit_in_journal(journal, offset));
Zhikang Zhang56b07e72018-04-09 04:28:41 +08004301 check_block_count(sbi, segno,
4302 &sit_in_journal(journal, offset));
Chao Yu184a5cd2014-09-04 18:13:01 +08004303 } else {
4304 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
4305 seg_info_to_raw_sit(se,
4306 &raw_sit->entries[sit_offset]);
Zhikang Zhang56b07e72018-04-09 04:28:41 +08004307 check_block_count(sbi, segno,
4308 &raw_sit->entries[sit_offset]);
Chao Yu184a5cd2014-09-04 18:13:01 +08004309 }
4310
4311 __clear_bit(segno, bitmap);
4312 sit_i->dirty_sentries--;
4313 ses->entry_cnt--;
4314 }
4315
Chao Yub7ad7512016-02-19 18:08:46 +08004316 if (to_journal)
4317 up_write(&curseg->journal_rwsem);
4318 else
Chao Yu184a5cd2014-09-04 18:13:01 +08004319 f2fs_put_page(page, 1);
4320
4321 f2fs_bug_on(sbi, ses->entry_cnt);
4322 release_sit_entry_set(ses);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004323 }
Chao Yu184a5cd2014-09-04 18:13:01 +08004324
4325 f2fs_bug_on(sbi, !list_empty(head));
4326 f2fs_bug_on(sbi, sit_i->dirty_sentries);
Chao Yu184a5cd2014-09-04 18:13:01 +08004327out:
Chao Yuc473f1a2017-04-27 20:40:39 +08004328 if (cpc->reason & CP_DISCARD) {
Yunlei He650d3c42016-12-22 11:46:24 +08004329 __u64 trim_start = cpc->trim_start;
4330
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07004331 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++)
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08004332 add_discard_addrs(sbi, cpc, false);
Yunlei He650d3c42016-12-22 11:46:24 +08004333
4334 cpc->trim_start = trim_start;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07004335 }
Chao Yu3d26fa62017-10-30 17:49:53 +08004336 up_write(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004337
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004338 set_prefree_as_free_segments(sbi);
4339}
4340
4341static int build_sit_info(struct f2fs_sb_info *sbi)
4342{
4343 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004344 struct sit_info *sit_i;
4345 unsigned int sit_segs, start;
Chao Yu2fde3dd2019-07-26 15:41:20 +08004346 char *src_bitmap, *bitmap;
Sahitya Tummalabbf9f7d2019-08-07 19:10:32 +05304347 unsigned int bitmap_size, main_bitmap_size, sit_bitmap_size;
Chao Yu4f993262021-08-03 08:15:43 +08004348 unsigned int discard_map = f2fs_block_unit_discard(sbi) ? 1 : 0;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004349
4350 /* allocate memory for SIT information */
Chao Yuacbf0542017-11-30 19:28:17 +08004351 sit_i = f2fs_kzalloc(sbi, sizeof(struct sit_info), GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004352 if (!sit_i)
4353 return -ENOMEM;
4354
4355 SM_I(sbi)->sit_info = sit_i;
4356
Kees Cook9d2a7892018-06-12 14:28:35 -07004357 sit_i->sentries =
4358 f2fs_kvzalloc(sbi, array_size(sizeof(struct seg_entry),
4359 MAIN_SEGS(sbi)),
4360 GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004361 if (!sit_i->sentries)
4362 return -ENOMEM;
4363
Sahitya Tummalabbf9f7d2019-08-07 19:10:32 +05304364 main_bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
4365 sit_i->dirty_sentries_bitmap = f2fs_kvzalloc(sbi, main_bitmap_size,
Chao Yu628b3d12017-11-30 19:28:18 +08004366 GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004367 if (!sit_i->dirty_sentries_bitmap)
4368 return -ENOMEM;
4369
Chao Yu2fde3dd2019-07-26 15:41:20 +08004370#ifdef CONFIG_F2FS_CHECK_FS
Chao Yu4f993262021-08-03 08:15:43 +08004371 bitmap_size = MAIN_SEGS(sbi) * SIT_VBLOCK_MAP_SIZE * (3 + discard_map);
Chao Yu2fde3dd2019-07-26 15:41:20 +08004372#else
Chao Yu4f993262021-08-03 08:15:43 +08004373 bitmap_size = MAIN_SEGS(sbi) * SIT_VBLOCK_MAP_SIZE * (2 + discard_map);
Chao Yu2fde3dd2019-07-26 15:41:20 +08004374#endif
4375 sit_i->bitmap = f2fs_kvzalloc(sbi, bitmap_size, GFP_KERNEL);
4376 if (!sit_i->bitmap)
4377 return -ENOMEM;
4378
4379 bitmap = sit_i->bitmap;
4380
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07004381 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Chao Yu2fde3dd2019-07-26 15:41:20 +08004382 sit_i->sentries[start].cur_valid_map = bitmap;
4383 bitmap += SIT_VBLOCK_MAP_SIZE;
4384
4385 sit_i->sentries[start].ckpt_valid_map = bitmap;
4386 bitmap += SIT_VBLOCK_MAP_SIZE;
Jaegeuk Kim3e025742016-08-02 10:56:40 -07004387
Chao Yu355e7892017-01-07 18:51:01 +08004388#ifdef CONFIG_F2FS_CHECK_FS
Chao Yu2fde3dd2019-07-26 15:41:20 +08004389 sit_i->sentries[start].cur_valid_map_mir = bitmap;
4390 bitmap += SIT_VBLOCK_MAP_SIZE;
Chao Yu355e7892017-01-07 18:51:01 +08004391#endif
4392
Chao Yu4f993262021-08-03 08:15:43 +08004393 if (discard_map) {
4394 sit_i->sentries[start].discard_map = bitmap;
4395 bitmap += SIT_VBLOCK_MAP_SIZE;
4396 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004397 }
4398
Chao Yuacbf0542017-11-30 19:28:17 +08004399 sit_i->tmp_map = f2fs_kzalloc(sbi, SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
Jaegeuk Kim60a3b782015-02-10 16:44:29 -08004400 if (!sit_i->tmp_map)
4401 return -ENOMEM;
4402
Chao Yu2c70c5e2018-10-24 18:37:26 +08004403 if (__is_large_section(sbi)) {
Kees Cook9d2a7892018-06-12 14:28:35 -07004404 sit_i->sec_entries =
4405 f2fs_kvzalloc(sbi, array_size(sizeof(struct sec_entry),
4406 MAIN_SECS(sbi)),
4407 GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004408 if (!sit_i->sec_entries)
4409 return -ENOMEM;
4410 }
4411
4412 /* get information related with SIT */
4413 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
4414
4415 /* setup SIT bitmap from ckeckpoint pack */
Sahitya Tummalabbf9f7d2019-08-07 19:10:32 +05304416 sit_bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004417 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
4418
Sahitya Tummalabbf9f7d2019-08-07 19:10:32 +05304419 sit_i->sit_bitmap = kmemdup(src_bitmap, sit_bitmap_size, GFP_KERNEL);
Chao Yuae27d622017-01-07 18:52:34 +08004420 if (!sit_i->sit_bitmap)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004421 return -ENOMEM;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004422
Chao Yuae27d622017-01-07 18:52:34 +08004423#ifdef CONFIG_F2FS_CHECK_FS
Sahitya Tummalabbf9f7d2019-08-07 19:10:32 +05304424 sit_i->sit_bitmap_mir = kmemdup(src_bitmap,
4425 sit_bitmap_size, GFP_KERNEL);
Chao Yuae27d622017-01-07 18:52:34 +08004426 if (!sit_i->sit_bitmap_mir)
4427 return -ENOMEM;
Sahitya Tummalabbf9f7d2019-08-07 19:10:32 +05304428
4429 sit_i->invalid_segmap = f2fs_kvzalloc(sbi,
4430 main_bitmap_size, GFP_KERNEL);
4431 if (!sit_i->invalid_segmap)
4432 return -ENOMEM;
Chao Yuae27d622017-01-07 18:52:34 +08004433#endif
4434
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004435 /* init SIT information */
4436 sit_i->s_ops = &default_salloc_ops;
4437
4438 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
4439 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
Jaegeuk Kimc79b7ff2016-11-14 18:20:10 -08004440 sit_i->written_valid_blocks = 0;
Sahitya Tummalabbf9f7d2019-08-07 19:10:32 +05304441 sit_i->bitmap_size = sit_bitmap_size;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004442 sit_i->dirty_sentries = 0;
4443 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
4444 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
Jaegeuk Kima7e679b2020-02-25 19:08:16 -08004445 sit_i->mounted_time = ktime_get_boottime_seconds();
Chao Yu3d26fa62017-10-30 17:49:53 +08004446 init_rwsem(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004447 return 0;
4448}
4449
4450static int build_free_segmap(struct f2fs_sb_info *sbi)
4451{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004452 struct free_segmap_info *free_i;
4453 unsigned int bitmap_size, sec_bitmap_size;
4454
4455 /* allocate memory for free segmap information */
Chao Yuacbf0542017-11-30 19:28:17 +08004456 free_i = f2fs_kzalloc(sbi, sizeof(struct free_segmap_info), GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004457 if (!free_i)
4458 return -ENOMEM;
4459
4460 SM_I(sbi)->free_info = free_i;
4461
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07004462 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Chao Yu628b3d12017-11-30 19:28:18 +08004463 free_i->free_segmap = f2fs_kvmalloc(sbi, bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004464 if (!free_i->free_segmap)
4465 return -ENOMEM;
4466
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07004467 sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
Chao Yu628b3d12017-11-30 19:28:18 +08004468 free_i->free_secmap = f2fs_kvmalloc(sbi, sec_bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004469 if (!free_i->free_secmap)
4470 return -ENOMEM;
4471
4472 /* set all segments as dirty temporarily */
4473 memset(free_i->free_segmap, 0xff, bitmap_size);
4474 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
4475
4476 /* init free segmap information */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07004477 free_i->start_segno = GET_SEGNO_FROM_SEG0(sbi, MAIN_BLKADDR(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004478 free_i->free_segments = 0;
4479 free_i->free_sections = 0;
Chao Yu1a118cc2015-02-11 18:20:38 +08004480 spin_lock_init(&free_i->segmap_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004481 return 0;
4482}
4483
4484static int build_curseg(struct f2fs_sb_info *sbi)
4485{
Namjae Jeon1042d602012-12-01 10:56:13 +09004486 struct curseg_info *array;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004487 int i;
4488
Chao Yud0b9e422020-08-04 21:14:45 +08004489 array = f2fs_kzalloc(sbi, array_size(NR_CURSEG_TYPE,
4490 sizeof(*array)), GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004491 if (!array)
4492 return -ENOMEM;
4493
4494 SM_I(sbi)->curseg_array = array;
4495
Chao Yud0b9e422020-08-04 21:14:45 +08004496 for (i = 0; i < NO_CHECK_TYPE; i++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004497 mutex_init(&array[i].curseg_mutex);
Chao Yuacbf0542017-11-30 19:28:17 +08004498 array[i].sum_blk = f2fs_kzalloc(sbi, PAGE_SIZE, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004499 if (!array[i].sum_blk)
4500 return -ENOMEM;
Chao Yub7ad7512016-02-19 18:08:46 +08004501 init_rwsem(&array[i].journal_rwsem);
Chao Yuacbf0542017-11-30 19:28:17 +08004502 array[i].journal = f2fs_kzalloc(sbi,
4503 sizeof(struct f2fs_journal), GFP_KERNEL);
Chao Yub7ad7512016-02-19 18:08:46 +08004504 if (!array[i].journal)
4505 return -ENOMEM;
Chao Yud0b9e422020-08-04 21:14:45 +08004506 if (i < NR_PERSISTENT_LOG)
4507 array[i].seg_type = CURSEG_HOT_DATA + i;
4508 else if (i == CURSEG_COLD_DATA_PINNED)
4509 array[i].seg_type = CURSEG_COLD_DATA;
Chao Yu093749e2020-08-04 21:14:49 +08004510 else if (i == CURSEG_ALL_DATA_ATGC)
4511 array[i].seg_type = CURSEG_COLD_DATA;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004512 array[i].segno = NULL_SEGNO;
4513 array[i].next_blkoff = 0;
Chao Yud0b9e422020-08-04 21:14:45 +08004514 array[i].inited = false;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004515 }
4516 return restore_curseg_summaries(sbi);
4517}
4518
Jaegeuk Kimc39a1b32017-12-19 19:16:34 -08004519static int build_sit_entries(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004520{
4521 struct sit_info *sit_i = SIT_I(sbi);
4522 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08004523 struct f2fs_journal *journal = curseg->journal;
Yunlei He9c094042016-09-24 12:29:18 +08004524 struct seg_entry *se;
4525 struct f2fs_sit_entry sit;
Chao Yu74de5932013-11-22 09:09:59 +08004526 int sit_blk_cnt = SIT_BLK_CNT(sbi);
4527 unsigned int i, start, end;
4528 unsigned int readed, start_blk = 0;
Jaegeuk Kimc39a1b32017-12-19 19:16:34 -08004529 int err = 0;
Jaegeuk Kim8a29c122018-04-24 21:34:05 -06004530 block_t total_node_blocks = 0;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004531
Chao Yu74de5932013-11-22 09:09:59 +08004532 do {
Christoph Hellwiga8affc02021-03-11 12:01:37 +01004533 readed = f2fs_ra_meta_pages(sbi, start_blk, BIO_MAX_VECS,
Jaegeuk Kim664ba972016-10-18 11:07:45 -07004534 META_SIT, true);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004535
Chao Yu74de5932013-11-22 09:09:59 +08004536 start = start_blk * sit_i->sents_per_block;
4537 end = (start_blk + readed) * sit_i->sents_per_block;
4538
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07004539 for (; start < end && start < MAIN_SEGS(sbi); start++) {
Chao Yu74de5932013-11-22 09:09:59 +08004540 struct f2fs_sit_block *sit_blk;
Chao Yu74de5932013-11-22 09:09:59 +08004541 struct page *page;
4542
Yunlei He9c094042016-09-24 12:29:18 +08004543 se = &sit_i->sentries[start];
Chao Yu74de5932013-11-22 09:09:59 +08004544 page = get_current_sit_page(sbi, start);
Jaegeuk Kimedc55aa2018-09-17 17:36:06 -07004545 if (IS_ERR(page))
4546 return PTR_ERR(page);
Chao Yu74de5932013-11-22 09:09:59 +08004547 sit_blk = (struct f2fs_sit_block *)page_address(page);
4548 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
4549 f2fs_put_page(page, 1);
Chao Yud600af232016-08-19 23:13:47 +08004550
Jaegeuk Kimc39a1b32017-12-19 19:16:34 -08004551 err = check_block_count(sbi, start, &sit);
4552 if (err)
4553 return err;
Chao Yu74de5932013-11-22 09:09:59 +08004554 seg_info_from_raw_sit(se, &sit);
Jaegeuk Kim8a29c122018-04-24 21:34:05 -06004555 if (IS_NODESEG(se->type))
4556 total_node_blocks += se->valid_blocks;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07004557
Chao Yu4f993262021-08-03 08:15:43 +08004558 if (f2fs_block_unit_discard(sbi)) {
4559 /* build discard map only one time */
4560 if (is_set_ckpt_flags(sbi, CP_TRIMMED_FLAG)) {
4561 memset(se->discard_map, 0xff,
4562 SIT_VBLOCK_MAP_SIZE);
4563 } else {
4564 memcpy(se->discard_map,
4565 se->cur_valid_map,
4566 SIT_VBLOCK_MAP_SIZE);
4567 sbi->discard_blks +=
4568 sbi->blocks_per_seg -
4569 se->valid_blocks;
4570 }
Jaegeuk Kim3e025742016-08-02 10:56:40 -07004571 }
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07004572
Chao Yu2c70c5e2018-10-24 18:37:26 +08004573 if (__is_large_section(sbi))
Chao Yud600af232016-08-19 23:13:47 +08004574 get_sec_entry(sbi, start)->valid_blocks +=
4575 se->valid_blocks;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004576 }
Chao Yu74de5932013-11-22 09:09:59 +08004577 start_blk += readed;
4578 } while (start_blk < sit_blk_cnt);
Chao Yud600af232016-08-19 23:13:47 +08004579
4580 down_read(&curseg->journal_rwsem);
4581 for (i = 0; i < sits_in_cursum(journal); i++) {
Chao Yud600af232016-08-19 23:13:47 +08004582 unsigned int old_valid_blocks;
4583
4584 start = le32_to_cpu(segno_in_journal(journal, i));
Jaegeuk Kimb2ca3742018-04-24 15:44:16 -06004585 if (start >= MAIN_SEGS(sbi)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08004586 f2fs_err(sbi, "Wrong journal entry on segno %u",
4587 start);
Chao Yu10f966b2019-06-20 11:36:14 +08004588 err = -EFSCORRUPTED;
Jaegeuk Kimb2ca3742018-04-24 15:44:16 -06004589 break;
4590 }
4591
Chao Yud600af232016-08-19 23:13:47 +08004592 se = &sit_i->sentries[start];
4593 sit = sit_in_journal(journal, i);
4594
4595 old_valid_blocks = se->valid_blocks;
Jaegeuk Kim8a29c122018-04-24 21:34:05 -06004596 if (IS_NODESEG(se->type))
4597 total_node_blocks -= old_valid_blocks;
Chao Yud600af232016-08-19 23:13:47 +08004598
Jaegeuk Kimc39a1b32017-12-19 19:16:34 -08004599 err = check_block_count(sbi, start, &sit);
4600 if (err)
4601 break;
Chao Yud600af232016-08-19 23:13:47 +08004602 seg_info_from_raw_sit(se, &sit);
Jaegeuk Kim8a29c122018-04-24 21:34:05 -06004603 if (IS_NODESEG(se->type))
4604 total_node_blocks += se->valid_blocks;
Chao Yud600af232016-08-19 23:13:47 +08004605
Chao Yu4f993262021-08-03 08:15:43 +08004606 if (f2fs_block_unit_discard(sbi)) {
4607 if (is_set_ckpt_flags(sbi, CP_TRIMMED_FLAG)) {
4608 memset(se->discard_map, 0xff, SIT_VBLOCK_MAP_SIZE);
4609 } else {
4610 memcpy(se->discard_map, se->cur_valid_map,
4611 SIT_VBLOCK_MAP_SIZE);
4612 sbi->discard_blks += old_valid_blocks;
4613 sbi->discard_blks -= se->valid_blocks;
4614 }
Chao Yud600af232016-08-19 23:13:47 +08004615 }
4616
Chao Yu2c70c5e2018-10-24 18:37:26 +08004617 if (__is_large_section(sbi)) {
Chao Yud600af232016-08-19 23:13:47 +08004618 get_sec_entry(sbi, start)->valid_blocks +=
Chao Yua9af3fd2018-04-25 19:38:17 +08004619 se->valid_blocks;
4620 get_sec_entry(sbi, start)->valid_blocks -=
4621 old_valid_blocks;
4622 }
Chao Yud600af232016-08-19 23:13:47 +08004623 }
4624 up_read(&curseg->journal_rwsem);
Jaegeuk Kim8a29c122018-04-24 21:34:05 -06004625
4626 if (!err && total_node_blocks != valid_node_count(sbi)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08004627 f2fs_err(sbi, "SIT is corrupted node# %u vs %u",
4628 total_node_blocks, valid_node_count(sbi));
Chao Yu10f966b2019-06-20 11:36:14 +08004629 err = -EFSCORRUPTED;
Jaegeuk Kim8a29c122018-04-24 21:34:05 -06004630 }
4631
Jaegeuk Kimc39a1b32017-12-19 19:16:34 -08004632 return err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004633}
4634
4635static void init_free_segmap(struct f2fs_sb_info *sbi)
4636{
4637 unsigned int start;
4638 int type;
Aravind Rameshde881df2020-07-16 18:26:56 +05304639 struct seg_entry *sentry;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004640
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07004641 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Aravind Rameshde881df2020-07-16 18:26:56 +05304642 if (f2fs_usable_blks_in_seg(sbi, start) == 0)
4643 continue;
4644 sentry = get_seg_entry(sbi, start);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004645 if (!sentry->valid_blocks)
4646 __set_free(sbi, start);
Jaegeuk Kimc79b7ff2016-11-14 18:20:10 -08004647 else
4648 SIT_I(sbi)->written_valid_blocks +=
4649 sentry->valid_blocks;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004650 }
4651
4652 /* set use the current segments */
4653 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
4654 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
Yi Zhuang5f029c02021-04-06 09:47:35 +08004655
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004656 __set_test_and_inuse(sbi, curseg_t->segno);
4657 }
4658}
4659
4660static void init_dirty_segmap(struct f2fs_sb_info *sbi)
4661{
4662 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
4663 struct free_segmap_info *free_i = FREE_I(sbi);
Jack Qiuda52f8a2020-06-18 12:37:10 +08004664 unsigned int segno = 0, offset = 0, secno;
Aravind Rameshde881df2020-07-16 18:26:56 +05304665 block_t valid_blocks, usable_blks_in_seg;
Shin'ichiro Kawasaki123aaf72020-08-19 10:34:48 +09004666 block_t blks_per_sec = BLKS_PER_SEC(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004667
Namjae Jeon8736fbf2013-06-16 09:49:11 +09004668 while (1) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004669 /* find dirty segment based on free segmap */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07004670 segno = find_next_inuse(free_i, MAIN_SEGS(sbi), offset);
4671 if (segno >= MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004672 break;
4673 offset = segno + 1;
Jaegeuk Kim302bd342017-04-07 14:33:22 -07004674 valid_blocks = get_valid_blocks(sbi, segno, false);
Aravind Rameshde881df2020-07-16 18:26:56 +05304675 usable_blks_in_seg = f2fs_usable_blks_in_seg(sbi, segno);
4676 if (valid_blocks == usable_blks_in_seg || !valid_blocks)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004677 continue;
Aravind Rameshde881df2020-07-16 18:26:56 +05304678 if (valid_blocks > usable_blks_in_seg) {
Jaegeuk Kimec325b52014-09-02 16:24:11 -07004679 f2fs_bug_on(sbi, 1);
4680 continue;
4681 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004682 mutex_lock(&dirty_i->seglist_lock);
4683 __locate_dirty_segment(sbi, segno, DIRTY);
4684 mutex_unlock(&dirty_i->seglist_lock);
4685 }
Jack Qiuda52f8a2020-06-18 12:37:10 +08004686
4687 if (!__is_large_section(sbi))
4688 return;
4689
4690 mutex_lock(&dirty_i->seglist_lock);
Jack Qiu5335bfc2020-12-01 15:45:47 +08004691 for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) {
Jack Qiuda52f8a2020-06-18 12:37:10 +08004692 valid_blocks = get_valid_blocks(sbi, segno, true);
4693 secno = GET_SEC_FROM_SEG(sbi, segno);
4694
4695 if (!valid_blocks || valid_blocks == blks_per_sec)
4696 continue;
4697 if (IS_CURSEC(sbi, secno))
4698 continue;
4699 set_bit(secno, dirty_i->dirty_secmap);
4700 }
4701 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004702}
4703
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09004704static int init_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004705{
4706 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07004707 unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004708
Chao Yu628b3d12017-11-30 19:28:18 +08004709 dirty_i->victim_secmap = f2fs_kvzalloc(sbi, bitmap_size, GFP_KERNEL);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09004710 if (!dirty_i->victim_secmap)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004711 return -ENOMEM;
4712 return 0;
4713}
4714
4715static int build_dirty_segmap(struct f2fs_sb_info *sbi)
4716{
4717 struct dirty_seglist_info *dirty_i;
4718 unsigned int bitmap_size, i;
4719
4720 /* allocate memory for dirty segments list information */
Chao Yuacbf0542017-11-30 19:28:17 +08004721 dirty_i = f2fs_kzalloc(sbi, sizeof(struct dirty_seglist_info),
4722 GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004723 if (!dirty_i)
4724 return -ENOMEM;
4725
4726 SM_I(sbi)->dirty_info = dirty_i;
4727 mutex_init(&dirty_i->seglist_lock);
4728
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07004729 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004730
4731 for (i = 0; i < NR_DIRTY_TYPE; i++) {
Chao Yu628b3d12017-11-30 19:28:18 +08004732 dirty_i->dirty_segmap[i] = f2fs_kvzalloc(sbi, bitmap_size,
4733 GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004734 if (!dirty_i->dirty_segmap[i])
4735 return -ENOMEM;
4736 }
4737
Jack Qiuda52f8a2020-06-18 12:37:10 +08004738 if (__is_large_section(sbi)) {
4739 bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
4740 dirty_i->dirty_secmap = f2fs_kvzalloc(sbi,
4741 bitmap_size, GFP_KERNEL);
4742 if (!dirty_i->dirty_secmap)
4743 return -ENOMEM;
4744 }
4745
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004746 init_dirty_segmap(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09004747 return init_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004748}
4749
Chao Yuc854f4d2019-05-25 23:07:25 +08004750static int sanity_check_curseg(struct f2fs_sb_info *sbi)
4751{
4752 int i;
4753
4754 /*
4755 * In LFS/SSR curseg, .next_blkoff should point to an unused blkaddr;
4756 * In LFS curseg, all blkaddr after .next_blkoff should be unused.
4757 */
Chao Yud0b9e422020-08-04 21:14:45 +08004758 for (i = 0; i < NR_PERSISTENT_LOG; i++) {
Chao Yuc854f4d2019-05-25 23:07:25 +08004759 struct curseg_info *curseg = CURSEG_I(sbi, i);
4760 struct seg_entry *se = get_seg_entry(sbi, curseg->segno);
4761 unsigned int blkofs = curseg->next_blkoff;
4762
Jaegeuk Kima7d9fe32021-05-21 01:32:53 -07004763 if (f2fs_sb_has_readonly(sbi) &&
4764 i != CURSEG_HOT_DATA && i != CURSEG_HOT_NODE)
4765 continue;
4766
Chao Yu093749e2020-08-04 21:14:49 +08004767 sanity_check_seg_type(sbi, curseg->seg_type);
4768
Chao Yuc854f4d2019-05-25 23:07:25 +08004769 if (f2fs_test_bit(blkofs, se->cur_valid_map))
4770 goto out;
4771
4772 if (curseg->alloc_type == SSR)
4773 continue;
4774
4775 for (blkofs += 1; blkofs < sbi->blocks_per_seg; blkofs++) {
4776 if (!f2fs_test_bit(blkofs, se->cur_valid_map))
4777 continue;
4778out:
Joe Perchesdcbb4c12019-06-18 17:48:42 +08004779 f2fs_err(sbi,
4780 "Current segment's next free block offset is inconsistent with bitmap, logtype:%u, segno:%u, type:%u, next_blkoff:%u, blkofs:%u",
4781 i, curseg->segno, curseg->alloc_type,
4782 curseg->next_blkoff, blkofs);
Chao Yu10f966b2019-06-20 11:36:14 +08004783 return -EFSCORRUPTED;
Chao Yuc854f4d2019-05-25 23:07:25 +08004784 }
4785 }
4786 return 0;
4787}
4788
Shin'ichiro Kawasakic426d992019-12-09 19:44:44 +09004789#ifdef CONFIG_BLK_DEV_ZONED
4790
Shin'ichiro Kawasakid508c942019-12-09 19:44:45 +09004791static int check_zone_write_pointer(struct f2fs_sb_info *sbi,
4792 struct f2fs_dev_info *fdev,
4793 struct blk_zone *zone)
4794{
4795 unsigned int wp_segno, wp_blkoff, zone_secno, zone_segno, segno;
4796 block_t zone_block, wp_block, last_valid_block;
4797 unsigned int log_sectors_per_block = sbi->log_blocksize - SECTOR_SHIFT;
4798 int i, s, b, ret;
4799 struct seg_entry *se;
4800
4801 if (zone->type != BLK_ZONE_TYPE_SEQWRITE_REQ)
4802 return 0;
4803
4804 wp_block = fdev->start_blk + (zone->wp >> log_sectors_per_block);
4805 wp_segno = GET_SEGNO(sbi, wp_block);
4806 wp_blkoff = wp_block - START_BLOCK(sbi, wp_segno);
4807 zone_block = fdev->start_blk + (zone->start >> log_sectors_per_block);
4808 zone_segno = GET_SEGNO(sbi, zone_block);
4809 zone_secno = GET_SEC_FROM_SEG(sbi, zone_segno);
4810
4811 if (zone_segno >= MAIN_SEGS(sbi))
4812 return 0;
4813
4814 /*
4815 * Skip check of zones cursegs point to, since
4816 * fix_curseg_write_pointer() checks them.
4817 */
4818 for (i = 0; i < NO_CHECK_TYPE; i++)
4819 if (zone_secno == GET_SEC_FROM_SEG(sbi,
4820 CURSEG_I(sbi, i)->segno))
4821 return 0;
4822
4823 /*
4824 * Get last valid block of the zone.
4825 */
4826 last_valid_block = zone_block - 1;
4827 for (s = sbi->segs_per_sec - 1; s >= 0; s--) {
4828 segno = zone_segno + s;
4829 se = get_seg_entry(sbi, segno);
4830 for (b = sbi->blocks_per_seg - 1; b >= 0; b--)
4831 if (f2fs_test_bit(b, se->cur_valid_map)) {
4832 last_valid_block = START_BLOCK(sbi, segno) + b;
4833 break;
4834 }
4835 if (last_valid_block >= zone_block)
4836 break;
4837 }
4838
4839 /*
4840 * If last valid block is beyond the write pointer, report the
4841 * inconsistency. This inconsistency does not cause write error
4842 * because the zone will not be selected for write operation until
4843 * it get discarded. Just report it.
4844 */
4845 if (last_valid_block >= wp_block) {
4846 f2fs_notice(sbi, "Valid block beyond write pointer: "
4847 "valid block[0x%x,0x%x] wp[0x%x,0x%x]",
4848 GET_SEGNO(sbi, last_valid_block),
4849 GET_BLKOFF_FROM_SEG0(sbi, last_valid_block),
4850 wp_segno, wp_blkoff);
4851 return 0;
4852 }
4853
4854 /*
4855 * If there is no valid block in the zone and if write pointer is
4856 * not at zone start, reset the write pointer.
4857 */
4858 if (last_valid_block + 1 == zone_block && zone->wp != zone->start) {
4859 f2fs_notice(sbi,
4860 "Zone without valid block has non-zero write "
4861 "pointer. Reset the write pointer: wp[0x%x,0x%x]",
4862 wp_segno, wp_blkoff);
4863 ret = __f2fs_issue_discard_zone(sbi, fdev->bdev, zone_block,
4864 zone->len >> log_sectors_per_block);
4865 if (ret) {
4866 f2fs_err(sbi, "Discard zone failed: %s (errno=%d)",
4867 fdev->path, ret);
4868 return ret;
4869 }
4870 }
4871
4872 return 0;
4873}
4874
Shin'ichiro Kawasakic426d992019-12-09 19:44:44 +09004875static struct f2fs_dev_info *get_target_zoned_dev(struct f2fs_sb_info *sbi,
4876 block_t zone_blkaddr)
4877{
4878 int i;
4879
4880 for (i = 0; i < sbi->s_ndevs; i++) {
4881 if (!bdev_is_zoned(FDEV(i).bdev))
4882 continue;
4883 if (sbi->s_ndevs == 1 || (FDEV(i).start_blk <= zone_blkaddr &&
4884 zone_blkaddr <= FDEV(i).end_blk))
4885 return &FDEV(i);
4886 }
4887
4888 return NULL;
4889}
4890
4891static int report_one_zone_cb(struct blk_zone *zone, unsigned int idx,
Yi Zhuang5f029c02021-04-06 09:47:35 +08004892 void *data)
4893{
Shin'ichiro Kawasakic426d992019-12-09 19:44:44 +09004894 memcpy(data, zone, sizeof(struct blk_zone));
4895 return 0;
4896}
4897
4898static int fix_curseg_write_pointer(struct f2fs_sb_info *sbi, int type)
4899{
4900 struct curseg_info *cs = CURSEG_I(sbi, type);
4901 struct f2fs_dev_info *zbd;
4902 struct blk_zone zone;
4903 unsigned int cs_section, wp_segno, wp_blkoff, wp_sector_off;
4904 block_t cs_zone_block, wp_block;
4905 unsigned int log_sectors_per_block = sbi->log_blocksize - SECTOR_SHIFT;
4906 sector_t zone_sector;
4907 int err;
4908
4909 cs_section = GET_SEC_FROM_SEG(sbi, cs->segno);
4910 cs_zone_block = START_BLOCK(sbi, GET_SEG_FROM_SEC(sbi, cs_section));
4911
4912 zbd = get_target_zoned_dev(sbi, cs_zone_block);
4913 if (!zbd)
4914 return 0;
4915
4916 /* report zone for the sector the curseg points to */
4917 zone_sector = (sector_t)(cs_zone_block - zbd->start_blk)
4918 << log_sectors_per_block;
4919 err = blkdev_report_zones(zbd->bdev, zone_sector, 1,
4920 report_one_zone_cb, &zone);
4921 if (err != 1) {
4922 f2fs_err(sbi, "Report zone failed: %s errno=(%d)",
4923 zbd->path, err);
4924 return err;
4925 }
4926
4927 if (zone.type != BLK_ZONE_TYPE_SEQWRITE_REQ)
4928 return 0;
4929
4930 wp_block = zbd->start_blk + (zone.wp >> log_sectors_per_block);
4931 wp_segno = GET_SEGNO(sbi, wp_block);
4932 wp_blkoff = wp_block - START_BLOCK(sbi, wp_segno);
4933 wp_sector_off = zone.wp & GENMASK(log_sectors_per_block - 1, 0);
4934
4935 if (cs->segno == wp_segno && cs->next_blkoff == wp_blkoff &&
4936 wp_sector_off == 0)
4937 return 0;
4938
4939 f2fs_notice(sbi, "Unaligned curseg[%d] with write pointer: "
4940 "curseg[0x%x,0x%x] wp[0x%x,0x%x]",
4941 type, cs->segno, cs->next_blkoff, wp_segno, wp_blkoff);
4942
4943 f2fs_notice(sbi, "Assign new section to curseg[%d]: "
4944 "curseg[0x%x,0x%x]", type, cs->segno, cs->next_blkoff);
Chao Yu509f1012021-04-21 09:54:55 +08004945
4946 f2fs_allocate_new_section(sbi, type, true);
Shin'ichiro Kawasakic426d992019-12-09 19:44:44 +09004947
Shin'ichiro Kawasakid508c942019-12-09 19:44:45 +09004948 /* check consistency of the zone curseg pointed to */
4949 if (check_zone_write_pointer(sbi, zbd, &zone))
4950 return -EIO;
4951
Shin'ichiro Kawasakic426d992019-12-09 19:44:44 +09004952 /* check newly assigned zone */
4953 cs_section = GET_SEC_FROM_SEG(sbi, cs->segno);
4954 cs_zone_block = START_BLOCK(sbi, GET_SEG_FROM_SEC(sbi, cs_section));
4955
4956 zbd = get_target_zoned_dev(sbi, cs_zone_block);
4957 if (!zbd)
4958 return 0;
4959
4960 zone_sector = (sector_t)(cs_zone_block - zbd->start_blk)
4961 << log_sectors_per_block;
4962 err = blkdev_report_zones(zbd->bdev, zone_sector, 1,
4963 report_one_zone_cb, &zone);
4964 if (err != 1) {
4965 f2fs_err(sbi, "Report zone failed: %s errno=(%d)",
4966 zbd->path, err);
4967 return err;
4968 }
4969
4970 if (zone.type != BLK_ZONE_TYPE_SEQWRITE_REQ)
4971 return 0;
4972
4973 if (zone.wp != zone.start) {
4974 f2fs_notice(sbi,
4975 "New zone for curseg[%d] is not yet discarded. "
4976 "Reset the zone: curseg[0x%x,0x%x]",
4977 type, cs->segno, cs->next_blkoff);
4978 err = __f2fs_issue_discard_zone(sbi, zbd->bdev,
4979 zone_sector >> log_sectors_per_block,
4980 zone.len >> log_sectors_per_block);
4981 if (err) {
4982 f2fs_err(sbi, "Discard zone failed: %s (errno=%d)",
4983 zbd->path, err);
4984 return err;
4985 }
4986 }
4987
4988 return 0;
4989}
4990
4991int f2fs_fix_curseg_write_pointer(struct f2fs_sb_info *sbi)
4992{
4993 int i, ret;
4994
Chao Yud0b9e422020-08-04 21:14:45 +08004995 for (i = 0; i < NR_PERSISTENT_LOG; i++) {
Shin'ichiro Kawasakic426d992019-12-09 19:44:44 +09004996 ret = fix_curseg_write_pointer(sbi, i);
4997 if (ret)
4998 return ret;
4999 }
5000
5001 return 0;
5002}
Shin'ichiro Kawasakid508c942019-12-09 19:44:45 +09005003
5004struct check_zone_write_pointer_args {
5005 struct f2fs_sb_info *sbi;
5006 struct f2fs_dev_info *fdev;
5007};
5008
5009static int check_zone_write_pointer_cb(struct blk_zone *zone, unsigned int idx,
Yi Zhuang5f029c02021-04-06 09:47:35 +08005010 void *data)
5011{
Shin'ichiro Kawasakid508c942019-12-09 19:44:45 +09005012 struct check_zone_write_pointer_args *args;
Yi Zhuang5f029c02021-04-06 09:47:35 +08005013
Shin'ichiro Kawasakid508c942019-12-09 19:44:45 +09005014 args = (struct check_zone_write_pointer_args *)data;
5015
5016 return check_zone_write_pointer(args->sbi, args->fdev, zone);
5017}
5018
5019int f2fs_check_write_pointer(struct f2fs_sb_info *sbi)
5020{
5021 int i, ret;
5022 struct check_zone_write_pointer_args args;
5023
5024 for (i = 0; i < sbi->s_ndevs; i++) {
5025 if (!bdev_is_zoned(FDEV(i).bdev))
5026 continue;
5027
5028 args.sbi = sbi;
5029 args.fdev = &FDEV(i);
5030 ret = blkdev_report_zones(FDEV(i).bdev, 0, BLK_ALL_ZONES,
5031 check_zone_write_pointer_cb, &args);
5032 if (ret < 0)
5033 return ret;
5034 }
5035
5036 return 0;
5037}
Aravind Rameshde881df2020-07-16 18:26:56 +05305038
5039static bool is_conv_zone(struct f2fs_sb_info *sbi, unsigned int zone_idx,
5040 unsigned int dev_idx)
5041{
5042 if (!bdev_is_zoned(FDEV(dev_idx).bdev))
5043 return true;
5044 return !test_bit(zone_idx, FDEV(dev_idx).blkz_seq);
5045}
5046
5047/* Return the zone index in the given device */
5048static unsigned int get_zone_idx(struct f2fs_sb_info *sbi, unsigned int secno,
5049 int dev_idx)
5050{
5051 block_t sec_start_blkaddr = START_BLOCK(sbi, GET_SEG_FROM_SEC(sbi, secno));
5052
5053 return (sec_start_blkaddr - FDEV(dev_idx).start_blk) >>
5054 sbi->log_blocks_per_blkz;
5055}
5056
5057/*
5058 * Return the usable segments in a section based on the zone's
5059 * corresponding zone capacity. Zone is equal to a section.
5060 */
5061static inline unsigned int f2fs_usable_zone_segs_in_sec(
5062 struct f2fs_sb_info *sbi, unsigned int segno)
5063{
5064 unsigned int dev_idx, zone_idx, unusable_segs_in_sec;
5065
5066 dev_idx = f2fs_target_device_index(sbi, START_BLOCK(sbi, segno));
5067 zone_idx = get_zone_idx(sbi, GET_SEC_FROM_SEG(sbi, segno), dev_idx);
5068
5069 /* Conventional zone's capacity is always equal to zone size */
5070 if (is_conv_zone(sbi, zone_idx, dev_idx))
5071 return sbi->segs_per_sec;
5072
5073 /*
5074 * If the zone_capacity_blocks array is NULL, then zone capacity
5075 * is equal to the zone size for all zones
5076 */
5077 if (!FDEV(dev_idx).zone_capacity_blocks)
5078 return sbi->segs_per_sec;
5079
5080 /* Get the segment count beyond zone capacity block */
5081 unusable_segs_in_sec = (sbi->blocks_per_blkz -
5082 FDEV(dev_idx).zone_capacity_blocks[zone_idx]) >>
5083 sbi->log_blocks_per_seg;
5084 return sbi->segs_per_sec - unusable_segs_in_sec;
5085}
5086
5087/*
5088 * Return the number of usable blocks in a segment. The number of blocks
5089 * returned is always equal to the number of blocks in a segment for
5090 * segments fully contained within a sequential zone capacity or a
5091 * conventional zone. For segments partially contained in a sequential
5092 * zone capacity, the number of usable blocks up to the zone capacity
5093 * is returned. 0 is returned in all other cases.
5094 */
5095static inline unsigned int f2fs_usable_zone_blks_in_seg(
5096 struct f2fs_sb_info *sbi, unsigned int segno)
5097{
5098 block_t seg_start, sec_start_blkaddr, sec_cap_blkaddr;
5099 unsigned int zone_idx, dev_idx, secno;
5100
5101 secno = GET_SEC_FROM_SEG(sbi, segno);
5102 seg_start = START_BLOCK(sbi, segno);
5103 dev_idx = f2fs_target_device_index(sbi, seg_start);
5104 zone_idx = get_zone_idx(sbi, secno, dev_idx);
5105
5106 /*
5107 * Conventional zone's capacity is always equal to zone size,
5108 * so, blocks per segment is unchanged.
5109 */
5110 if (is_conv_zone(sbi, zone_idx, dev_idx))
5111 return sbi->blocks_per_seg;
5112
5113 if (!FDEV(dev_idx).zone_capacity_blocks)
5114 return sbi->blocks_per_seg;
5115
5116 sec_start_blkaddr = START_BLOCK(sbi, GET_SEG_FROM_SEC(sbi, secno));
5117 sec_cap_blkaddr = sec_start_blkaddr +
5118 FDEV(dev_idx).zone_capacity_blocks[zone_idx];
5119
5120 /*
5121 * If segment starts before zone capacity and spans beyond
5122 * zone capacity, then usable blocks are from seg start to
5123 * zone capacity. If the segment starts after the zone capacity,
5124 * then there are no usable blocks.
5125 */
5126 if (seg_start >= sec_cap_blkaddr)
5127 return 0;
5128 if (seg_start + sbi->blocks_per_seg > sec_cap_blkaddr)
5129 return sec_cap_blkaddr - seg_start;
5130
5131 return sbi->blocks_per_seg;
5132}
Shin'ichiro Kawasakic426d992019-12-09 19:44:44 +09005133#else
5134int f2fs_fix_curseg_write_pointer(struct f2fs_sb_info *sbi)
5135{
5136 return 0;
5137}
Shin'ichiro Kawasakid508c942019-12-09 19:44:45 +09005138
5139int f2fs_check_write_pointer(struct f2fs_sb_info *sbi)
5140{
5141 return 0;
5142}
Aravind Rameshde881df2020-07-16 18:26:56 +05305143
5144static inline unsigned int f2fs_usable_zone_blks_in_seg(struct f2fs_sb_info *sbi,
5145 unsigned int segno)
5146{
5147 return 0;
5148}
5149
5150static inline unsigned int f2fs_usable_zone_segs_in_sec(struct f2fs_sb_info *sbi,
5151 unsigned int segno)
5152{
5153 return 0;
5154}
Shin'ichiro Kawasakic426d992019-12-09 19:44:44 +09005155#endif
Aravind Rameshde881df2020-07-16 18:26:56 +05305156unsigned int f2fs_usable_blks_in_seg(struct f2fs_sb_info *sbi,
5157 unsigned int segno)
5158{
5159 if (f2fs_sb_has_blkzoned(sbi))
5160 return f2fs_usable_zone_blks_in_seg(sbi, segno);
5161
5162 return sbi->blocks_per_seg;
5163}
5164
5165unsigned int f2fs_usable_segs_in_sec(struct f2fs_sb_info *sbi,
5166 unsigned int segno)
5167{
5168 if (f2fs_sb_has_blkzoned(sbi))
5169 return f2fs_usable_zone_segs_in_sec(sbi, segno);
5170
5171 return sbi->segs_per_sec;
5172}
Shin'ichiro Kawasakic426d992019-12-09 19:44:44 +09005173
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09005174/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005175 * Update min, max modified time for cost-benefit GC algorithm
5176 */
5177static void init_min_max_mtime(struct f2fs_sb_info *sbi)
5178{
5179 struct sit_info *sit_i = SIT_I(sbi);
5180 unsigned int segno;
5181
Chao Yu3d26fa62017-10-30 17:49:53 +08005182 down_write(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005183
Chao Yu5ad25442018-05-15 18:59:55 +08005184 sit_i->min_mtime = ULLONG_MAX;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005185
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07005186 for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005187 unsigned int i;
5188 unsigned long long mtime = 0;
5189
5190 for (i = 0; i < sbi->segs_per_sec; i++)
5191 mtime += get_seg_entry(sbi, segno + i)->mtime;
5192
5193 mtime = div_u64(mtime, sbi->segs_per_sec);
5194
5195 if (sit_i->min_mtime > mtime)
5196 sit_i->min_mtime = mtime;
5197 }
Chao Yua1f72ac22018-06-04 23:20:17 +08005198 sit_i->max_mtime = get_mtime(sbi, false);
Chao Yu093749e2020-08-04 21:14:49 +08005199 sit_i->dirty_max_mtime = 0;
Chao Yu3d26fa62017-10-30 17:49:53 +08005200 up_write(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005201}
5202
Chao Yu4d57b862018-05-30 00:20:41 +08005203int f2fs_build_segment_manager(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005204{
5205 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
5206 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Namjae Jeon1042d602012-12-01 10:56:13 +09005207 struct f2fs_sm_info *sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005208 int err;
5209
Chao Yuacbf0542017-11-30 19:28:17 +08005210 sm_info = f2fs_kzalloc(sbi, sizeof(struct f2fs_sm_info), GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005211 if (!sm_info)
5212 return -ENOMEM;
5213
5214 /* init sm info */
5215 sbi->sm_info = sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005216 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
5217 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
5218 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
5219 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
5220 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
5221 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
5222 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
Jaegeuk Kim58c41032014-03-19 14:17:21 +09005223 sm_info->rec_prefree_segments = sm_info->main_segments *
5224 DEF_RECLAIM_PREFREE_SEGMENTS / 100;
Jaegeuk Kim44a83492016-07-13 18:23:35 -07005225 if (sm_info->rec_prefree_segments > DEF_MAX_RECLAIM_PREFREE_SEGMENTS)
5226 sm_info->rec_prefree_segments = DEF_MAX_RECLAIM_PREFREE_SEGMENTS;
5227
Chao Yub0332a02020-02-14 17:44:12 +08005228 if (!f2fs_lfs_mode(sbi))
Jaegeuk Kim52763a42016-06-13 09:47:48 -07005229 sm_info->ipu_policy = 1 << F2FS_IPU_FSYNC;
Jaegeuk Kim216fbd62013-11-07 13:13:42 +09005230 sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -07005231 sm_info->min_fsync_blocks = DEF_MIN_FSYNC_BLOCKS;
Laibin Qiudc675a92021-07-31 11:26:46 +08005232 sm_info->min_seq_blocks = sbi->blocks_per_seg;
Jaegeuk Kimef095d12017-03-24 20:05:13 -04005233 sm_info->min_hot_blocks = DEF_MIN_HOT_BLOCKS;
Chao Yua2a12b62017-10-28 16:52:33 +08005234 sm_info->min_ssr_sections = reserved_sections(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005235
Chao Yu184a5cd2014-09-04 18:13:01 +08005236 INIT_LIST_HEAD(&sm_info->sit_entry_set);
5237
Chao Yu2b603112017-11-02 20:41:03 +08005238 init_rwsem(&sm_info->curseg_lock);
5239
Yunlei Hed4fdf8b2017-06-01 16:43:51 +08005240 if (!f2fs_readonly(sbi->sb)) {
Chao Yu4d57b862018-05-30 00:20:41 +08005241 err = f2fs_create_flush_cmd_control(sbi);
Gu Zheng2163d192014-04-27 14:21:33 +08005242 if (err)
Gu Zhenga688b9d9e2014-04-27 14:21:21 +08005243 return err;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +09005244 }
5245
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08005246 err = create_discard_cmd_control(sbi);
5247 if (err)
5248 return err;
5249
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005250 err = build_sit_info(sbi);
5251 if (err)
5252 return err;
5253 err = build_free_segmap(sbi);
5254 if (err)
5255 return err;
5256 err = build_curseg(sbi);
5257 if (err)
5258 return err;
5259
5260 /* reinit free segmap based on SIT */
Jaegeuk Kimc39a1b32017-12-19 19:16:34 -08005261 err = build_sit_entries(sbi);
5262 if (err)
5263 return err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005264
5265 init_free_segmap(sbi);
5266 err = build_dirty_segmap(sbi);
5267 if (err)
5268 return err;
5269
Chao Yuc854f4d2019-05-25 23:07:25 +08005270 err = sanity_check_curseg(sbi);
5271 if (err)
5272 return err;
5273
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005274 init_min_max_mtime(sbi);
5275 return 0;
5276}
5277
5278static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
5279 enum dirty_type dirty_type)
5280{
5281 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
5282
5283 mutex_lock(&dirty_i->seglist_lock);
Jaegeuk Kim39307a82015-09-22 13:50:47 -07005284 kvfree(dirty_i->dirty_segmap[dirty_type]);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005285 dirty_i->nr_dirty[dirty_type] = 0;
5286 mutex_unlock(&dirty_i->seglist_lock);
5287}
5288
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09005289static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005290{
5291 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Yi Zhuang5f029c02021-04-06 09:47:35 +08005292
Jaegeuk Kim39307a82015-09-22 13:50:47 -07005293 kvfree(dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005294}
5295
5296static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
5297{
5298 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
5299 int i;
5300
5301 if (!dirty_i)
5302 return;
5303
5304 /* discard pre-free/dirty segments list */
5305 for (i = 0; i < NR_DIRTY_TYPE; i++)
5306 discard_dirty_segmap(sbi, i);
5307
Jack Qiuda52f8a2020-06-18 12:37:10 +08005308 if (__is_large_section(sbi)) {
5309 mutex_lock(&dirty_i->seglist_lock);
5310 kvfree(dirty_i->dirty_secmap);
5311 mutex_unlock(&dirty_i->seglist_lock);
5312 }
5313
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09005314 destroy_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005315 SM_I(sbi)->dirty_info = NULL;
Chao Yuc8eb7022020-09-14 16:47:00 +08005316 kfree(dirty_i);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005317}
5318
5319static void destroy_curseg(struct f2fs_sb_info *sbi)
5320{
5321 struct curseg_info *array = SM_I(sbi)->curseg_array;
5322 int i;
5323
5324 if (!array)
5325 return;
5326 SM_I(sbi)->curseg_array = NULL;
Chao Yub7ad7512016-02-19 18:08:46 +08005327 for (i = 0; i < NR_CURSEG_TYPE; i++) {
Chao Yuc8eb7022020-09-14 16:47:00 +08005328 kfree(array[i].sum_blk);
5329 kfree(array[i].journal);
Chao Yub7ad7512016-02-19 18:08:46 +08005330 }
Chao Yuc8eb7022020-09-14 16:47:00 +08005331 kfree(array);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005332}
5333
5334static void destroy_free_segmap(struct f2fs_sb_info *sbi)
5335{
5336 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
Yi Zhuang5f029c02021-04-06 09:47:35 +08005337
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005338 if (!free_i)
5339 return;
5340 SM_I(sbi)->free_info = NULL;
Jaegeuk Kim39307a82015-09-22 13:50:47 -07005341 kvfree(free_i->free_segmap);
5342 kvfree(free_i->free_secmap);
Chao Yuc8eb7022020-09-14 16:47:00 +08005343 kfree(free_i);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005344}
5345
5346static void destroy_sit_info(struct f2fs_sb_info *sbi)
5347{
5348 struct sit_info *sit_i = SIT_I(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005349
5350 if (!sit_i)
5351 return;
5352
Chao Yu2fde3dd2019-07-26 15:41:20 +08005353 if (sit_i->sentries)
5354 kvfree(sit_i->bitmap);
Chao Yuc8eb7022020-09-14 16:47:00 +08005355 kfree(sit_i->tmp_map);
Jaegeuk Kim60a3b782015-02-10 16:44:29 -08005356
Jaegeuk Kim39307a82015-09-22 13:50:47 -07005357 kvfree(sit_i->sentries);
5358 kvfree(sit_i->sec_entries);
5359 kvfree(sit_i->dirty_sentries_bitmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005360
5361 SM_I(sbi)->sit_info = NULL;
Jaegeuk Kim52225952018-12-13 18:38:33 -08005362 kvfree(sit_i->sit_bitmap);
Chao Yuae27d622017-01-07 18:52:34 +08005363#ifdef CONFIG_F2FS_CHECK_FS
Jaegeuk Kim52225952018-12-13 18:38:33 -08005364 kvfree(sit_i->sit_bitmap_mir);
Sahitya Tummalabbf9f7d2019-08-07 19:10:32 +05305365 kvfree(sit_i->invalid_segmap);
Chao Yuae27d622017-01-07 18:52:34 +08005366#endif
Chao Yuc8eb7022020-09-14 16:47:00 +08005367 kfree(sit_i);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005368}
5369
Chao Yu4d57b862018-05-30 00:20:41 +08005370void f2fs_destroy_segment_manager(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005371{
5372 struct f2fs_sm_info *sm_info = SM_I(sbi);
Gu Zhenga688b9d9e2014-04-27 14:21:21 +08005373
Chao Yu3b03f722013-11-06 09:12:04 +08005374 if (!sm_info)
5375 return;
Chao Yu4d57b862018-05-30 00:20:41 +08005376 f2fs_destroy_flush_cmd_control(sbi, true);
Chao Yuf0994052017-03-27 18:14:04 +08005377 destroy_discard_cmd_control(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005378 destroy_dirty_segmap(sbi);
5379 destroy_curseg(sbi);
5380 destroy_free_segmap(sbi);
5381 destroy_sit_info(sbi);
5382 sbi->sm_info = NULL;
Chao Yuc8eb7022020-09-14 16:47:00 +08005383 kfree(sm_info);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005384}
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09005385
Chao Yu4d57b862018-05-30 00:20:41 +08005386int __init f2fs_create_segment_manager_caches(void)
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09005387{
Chao Yu98510002020-02-17 17:46:20 +08005388 discard_entry_slab = f2fs_kmem_cache_create("f2fs_discard_entry",
Gu Zhenge8512d22014-03-07 18:43:28 +08005389 sizeof(struct discard_entry));
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09005390 if (!discard_entry_slab)
Chao Yu184a5cd2014-09-04 18:13:01 +08005391 goto fail;
5392
Chao Yu98510002020-02-17 17:46:20 +08005393 discard_cmd_slab = f2fs_kmem_cache_create("f2fs_discard_cmd",
Jaegeuk Kimb01a9202017-01-09 14:13:03 -08005394 sizeof(struct discard_cmd));
5395 if (!discard_cmd_slab)
Chao Yu6ab2a302016-09-05 12:28:26 +08005396 goto destroy_discard_entry;
Chao Yu275b66b2016-08-29 23:58:34 +08005397
Chao Yu98510002020-02-17 17:46:20 +08005398 sit_entry_set_slab = f2fs_kmem_cache_create("f2fs_sit_entry_set",
Changman Leec9ee0082014-11-21 15:42:07 +09005399 sizeof(struct sit_entry_set));
Chao Yu184a5cd2014-09-04 18:13:01 +08005400 if (!sit_entry_set_slab)
Jaegeuk Kimb01a9202017-01-09 14:13:03 -08005401 goto destroy_discard_cmd;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07005402
Chao Yu98510002020-02-17 17:46:20 +08005403 inmem_entry_slab = f2fs_kmem_cache_create("f2fs_inmem_page_entry",
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07005404 sizeof(struct inmem_pages));
5405 if (!inmem_entry_slab)
5406 goto destroy_sit_entry_set;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09005407 return 0;
Chao Yu184a5cd2014-09-04 18:13:01 +08005408
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07005409destroy_sit_entry_set:
5410 kmem_cache_destroy(sit_entry_set_slab);
Jaegeuk Kimb01a9202017-01-09 14:13:03 -08005411destroy_discard_cmd:
5412 kmem_cache_destroy(discard_cmd_slab);
Chao Yu6ab2a302016-09-05 12:28:26 +08005413destroy_discard_entry:
Chao Yu184a5cd2014-09-04 18:13:01 +08005414 kmem_cache_destroy(discard_entry_slab);
5415fail:
5416 return -ENOMEM;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09005417}
5418
Chao Yu4d57b862018-05-30 00:20:41 +08005419void f2fs_destroy_segment_manager_caches(void)
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09005420{
Chao Yu184a5cd2014-09-04 18:13:01 +08005421 kmem_cache_destroy(sit_entry_set_slab);
Jaegeuk Kimb01a9202017-01-09 14:13:03 -08005422 kmem_cache_destroy(discard_cmd_slab);
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09005423 kmem_cache_destroy(discard_entry_slab);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07005424 kmem_cache_destroy(inmem_entry_slab);
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09005425}