blob: bdaf5fbdfb1835a231f2fc459ac3b6b3b38a26b0 [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>
Daeho Jeong6691d942021-09-29 11:12:03 -070018#include <linux/random.h>
Jaegeuk Kim351df4b2012-11-02 17:09:16 +090019
20#include "f2fs.h"
21#include "segment.h"
22#include "node.h"
Jaegeuk Kim5f656542017-08-15 21:27:19 -070023#include "gc.h"
Daeho Jeong52118742021-08-19 20:52:28 -070024#include "iostat.h"
Namjae Jeon6ec178d2013-04-23 17:51:43 +090025#include <trace/events/f2fs.h>
Jaegeuk Kim351df4b2012-11-02 17:09:16 +090026
Changman Lee9a7f1432013-11-15 10:42:51 +090027#define __reverse_ffz(x) __reverse_ffs(~(x))
28
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +090029static struct kmem_cache *discard_entry_slab;
Jaegeuk Kimb01a9202017-01-09 14:13:03 -080030static struct kmem_cache *discard_cmd_slab;
Chao Yu184a5cd2014-09-04 18:13:01 +080031static struct kmem_cache *sit_entry_set_slab;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -070032static struct kmem_cache *inmem_entry_slab;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +090033
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070034static unsigned long __reverse_ulong(unsigned char *str)
35{
36 unsigned long tmp = 0;
37 int shift = 24, idx = 0;
38
39#if BITS_PER_LONG == 64
40 shift = 56;
41#endif
42 while (shift >= 0) {
43 tmp |= (unsigned long)str[idx++] << shift;
44 shift -= BITS_PER_BYTE;
45 }
46 return tmp;
47}
48
Changman Lee9a7f1432013-11-15 10:42:51 +090049/*
50 * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since
51 * MSB and LSB are reversed in a byte by f2fs_set_bit.
52 */
53static inline unsigned long __reverse_ffs(unsigned long word)
54{
55 int num = 0;
56
57#if BITS_PER_LONG == 64
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070058 if ((word & 0xffffffff00000000UL) == 0)
Changman Lee9a7f1432013-11-15 10:42:51 +090059 num += 32;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070060 else
Changman Lee9a7f1432013-11-15 10:42:51 +090061 word >>= 32;
Changman Lee9a7f1432013-11-15 10:42:51 +090062#endif
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070063 if ((word & 0xffff0000) == 0)
Changman Lee9a7f1432013-11-15 10:42:51 +090064 num += 16;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070065 else
Changman Lee9a7f1432013-11-15 10:42:51 +090066 word >>= 16;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070067
68 if ((word & 0xff00) == 0)
Changman Lee9a7f1432013-11-15 10:42:51 +090069 num += 8;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070070 else
Changman Lee9a7f1432013-11-15 10:42:51 +090071 word >>= 8;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070072
Changman Lee9a7f1432013-11-15 10:42:51 +090073 if ((word & 0xf0) == 0)
74 num += 4;
75 else
76 word >>= 4;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070077
Changman Lee9a7f1432013-11-15 10:42:51 +090078 if ((word & 0xc) == 0)
79 num += 2;
80 else
81 word >>= 2;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070082
Changman Lee9a7f1432013-11-15 10:42:51 +090083 if ((word & 0x2) == 0)
84 num += 1;
85 return num;
86}
87
88/*
arter97e1c42042014-08-06 23:22:50 +090089 * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c because
Changman Lee9a7f1432013-11-15 10:42:51 +090090 * f2fs_set_bit makes MSB and LSB reversed in a byte.
Fan Li692223d2015-11-12 08:43:04 +080091 * @size must be integral times of unsigned long.
Changman Lee9a7f1432013-11-15 10:42:51 +090092 * Example:
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070093 * MSB <--> LSB
94 * f2fs_set_bit(0, bitmap) => 1000 0000
95 * f2fs_set_bit(7, bitmap) => 0000 0001
Changman Lee9a7f1432013-11-15 10:42:51 +090096 */
97static unsigned long __find_rev_next_bit(const unsigned long *addr,
98 unsigned long size, unsigned long offset)
99{
100 const unsigned long *p = addr + BIT_WORD(offset);
Fan Li692223d2015-11-12 08:43:04 +0800101 unsigned long result = size;
Changman Lee9a7f1432013-11-15 10:42:51 +0900102 unsigned long tmp;
Changman Lee9a7f1432013-11-15 10:42:51 +0900103
104 if (offset >= size)
105 return size;
106
Fan Li692223d2015-11-12 08:43:04 +0800107 size -= (offset & ~(BITS_PER_LONG - 1));
Changman Lee9a7f1432013-11-15 10:42:51 +0900108 offset %= BITS_PER_LONG;
Changman Lee9a7f1432013-11-15 10:42:51 +0900109
Fan Li692223d2015-11-12 08:43:04 +0800110 while (1) {
111 if (*p == 0)
112 goto pass;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700113
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700114 tmp = __reverse_ulong((unsigned char *)p);
Fan Li692223d2015-11-12 08:43:04 +0800115
116 tmp &= ~0UL >> offset;
117 if (size < BITS_PER_LONG)
118 tmp &= (~0UL << (BITS_PER_LONG - size));
Changman Lee9a7f1432013-11-15 10:42:51 +0900119 if (tmp)
Fan Li692223d2015-11-12 08:43:04 +0800120 goto found;
121pass:
122 if (size <= BITS_PER_LONG)
123 break;
Changman Lee9a7f1432013-11-15 10:42:51 +0900124 size -= BITS_PER_LONG;
Fan Li692223d2015-11-12 08:43:04 +0800125 offset = 0;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700126 p++;
Changman Lee9a7f1432013-11-15 10:42:51 +0900127 }
Fan Li692223d2015-11-12 08:43:04 +0800128 return result;
129found:
130 return result - size + __reverse_ffs(tmp);
Changman Lee9a7f1432013-11-15 10:42:51 +0900131}
132
133static unsigned long __find_rev_next_zero_bit(const unsigned long *addr,
134 unsigned long size, unsigned long offset)
135{
136 const unsigned long *p = addr + BIT_WORD(offset);
Jaegeuk Kim80609442015-12-04 16:51:13 -0800137 unsigned long result = size;
Changman Lee9a7f1432013-11-15 10:42:51 +0900138 unsigned long tmp;
Changman Lee9a7f1432013-11-15 10:42:51 +0900139
140 if (offset >= size)
141 return size;
142
Jaegeuk Kim80609442015-12-04 16:51:13 -0800143 size -= (offset & ~(BITS_PER_LONG - 1));
Changman Lee9a7f1432013-11-15 10:42:51 +0900144 offset %= BITS_PER_LONG;
Changman Lee9a7f1432013-11-15 10:42:51 +0900145
Jaegeuk Kim80609442015-12-04 16:51:13 -0800146 while (1) {
147 if (*p == ~0UL)
148 goto pass;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700149
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700150 tmp = __reverse_ulong((unsigned char *)p);
Jaegeuk Kim80609442015-12-04 16:51:13 -0800151
152 if (offset)
153 tmp |= ~0UL << (BITS_PER_LONG - offset);
154 if (size < BITS_PER_LONG)
155 tmp |= ~0UL >> size;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700156 if (tmp != ~0UL)
Jaegeuk Kim80609442015-12-04 16:51:13 -0800157 goto found;
158pass:
159 if (size <= BITS_PER_LONG)
160 break;
Changman Lee9a7f1432013-11-15 10:42:51 +0900161 size -= BITS_PER_LONG;
Jaegeuk Kim80609442015-12-04 16:51:13 -0800162 offset = 0;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700163 p++;
Changman Lee9a7f1432013-11-15 10:42:51 +0900164 }
Jaegeuk Kim80609442015-12-04 16:51:13 -0800165 return result;
166found:
167 return result - size + __reverse_ffz(tmp);
Changman Lee9a7f1432013-11-15 10:42:51 +0900168}
169
Chao Yu4d57b862018-05-30 00:20:41 +0800170bool f2fs_need_SSR(struct f2fs_sb_info *sbi)
Jaegeuk Kimb3a97a22017-09-09 11:11:04 -0700171{
172 int node_secs = get_blocktype_secs(sbi, F2FS_DIRTY_NODES);
173 int dent_secs = get_blocktype_secs(sbi, F2FS_DIRTY_DENTS);
174 int imeta_secs = get_blocktype_secs(sbi, F2FS_DIRTY_IMETA);
175
Chao Yub0332a02020-02-14 17:44:12 +0800176 if (f2fs_lfs_mode(sbi))
Jaegeuk Kimb3a97a22017-09-09 11:11:04 -0700177 return false;
Daeho Jeong0e5e8112020-07-02 13:14:14 +0900178 if (sbi->gc_mode == GC_URGENT_HIGH)
Jaegeuk Kimb3a97a22017-09-09 11:11:04 -0700179 return true;
Daniel Rosenberg43549942018-08-20 19:21:43 -0700180 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
181 return true;
Jaegeuk Kimb3a97a22017-09-09 11:11:04 -0700182
183 return free_sections(sbi) <= (node_secs + 2 * dent_secs + imeta_secs +
Chao Yua2a12b62017-10-28 16:52:33 +0800184 SM_I(sbi)->min_ssr_sections + reserved_sections(sbi));
Jaegeuk Kimb3a97a22017-09-09 11:11:04 -0700185}
186
Chao Yu4d57b862018-05-30 00:20:41 +0800187void f2fs_register_inmem_page(struct inode *inode, struct page *page)
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700188{
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700189 struct inmem_pages *new;
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800190
Chao Yub763f3b2021-04-28 17:20:31 +0800191 set_page_private_atomic(page);
Chao Yudecd36b2015-08-07 18:42:09 +0800192
Chao Yu32410572021-08-09 08:24:48 +0800193 new = f2fs_kmem_cache_alloc(inmem_entry_slab,
194 GFP_NOFS, true, NULL);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700195
196 /* add atomic page indices to the list */
197 new->page = page;
198 INIT_LIST_HEAD(&new->list);
Chao Yudecd36b2015-08-07 18:42:09 +0800199
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700200 /* increase reference count with clean state */
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700201 get_page(page);
Jaegeuk Kim743b6202019-09-09 13:10:59 +0100202 mutex_lock(&F2FS_I(inode)->inmem_lock);
203 list_add_tail(&new->list, &F2FS_I(inode)->inmem_pages);
Jaegeuk Kim8dcf2ff72014-12-05 17:18:15 -0800204 inc_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
Jaegeuk Kim743b6202019-09-09 13:10:59 +0100205 mutex_unlock(&F2FS_I(inode)->inmem_lock);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -0700206
207 trace_f2fs_register_inmem_page(page, INMEM);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700208}
209
Chao Yu28bc1062016-02-06 14:40:34 +0800210static int __revoke_inmem_pages(struct inode *inode,
Chao Yu48432982019-02-25 17:11:03 +0800211 struct list_head *head, bool drop, bool recover,
212 bool trylock)
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700213{
Chao Yu28bc1062016-02-06 14:40:34 +0800214 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700215 struct inmem_pages *cur, *tmp;
Chao Yu28bc1062016-02-06 14:40:34 +0800216 int err = 0;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700217
Chao Yu29b96b52016-02-06 14:38:29 +0800218 list_for_each_entry_safe(cur, tmp, head, list) {
Chao Yu28bc1062016-02-06 14:40:34 +0800219 struct page *page = cur->page;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700220
Chao Yu28bc1062016-02-06 14:40:34 +0800221 if (drop)
222 trace_f2fs_commit_inmem_page(page, INMEM_DROP);
223
Chao Yu48432982019-02-25 17:11:03 +0800224 if (trylock) {
225 /*
226 * to avoid deadlock in between page lock and
227 * inmem_lock.
228 */
229 if (!trylock_page(page))
230 continue;
231 } else {
232 lock_page(page);
233 }
Chao Yu28bc1062016-02-06 14:40:34 +0800234
Chao Yubae0ee72018-12-25 17:43:42 +0800235 f2fs_wait_on_page_writeback(page, DATA, true, true);
Chao Yue5e57322018-04-23 10:36:13 +0800236
Chao Yu28bc1062016-02-06 14:40:34 +0800237 if (recover) {
238 struct dnode_of_data dn;
239 struct node_info ni;
240
241 trace_f2fs_commit_inmem_page(page, INMEM_REVOKE);
Chao Yu7f2b4e82017-08-08 19:09:08 +0800242retry:
Chao Yu28bc1062016-02-06 14:40:34 +0800243 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +0800244 err = f2fs_get_dnode_of_data(&dn, page->index,
245 LOOKUP_NODE);
Chao Yu7f2b4e82017-08-08 19:09:08 +0800246 if (err) {
247 if (err == -ENOMEM) {
Chao Yu5df7731f2020-02-17 17:45:44 +0800248 congestion_wait(BLK_RW_ASYNC,
249 DEFAULT_IO_TIMEOUT);
Chao Yu7f2b4e82017-08-08 19:09:08 +0800250 cond_resched();
251 goto retry;
252 }
Chao Yu28bc1062016-02-06 14:40:34 +0800253 err = -EAGAIN;
254 goto next;
255 }
Chao Yu77357302018-07-17 00:02:17 +0800256
257 err = f2fs_get_node_info(sbi, dn.nid, &ni);
258 if (err) {
259 f2fs_put_dnode(&dn);
260 return err;
261 }
262
Daeho Jeongf1d25642018-01-10 16:49:10 +0900263 if (cur->old_addr == NEW_ADDR) {
Chao Yu4d57b862018-05-30 00:20:41 +0800264 f2fs_invalidate_blocks(sbi, dn.data_blkaddr);
Daeho Jeongf1d25642018-01-10 16:49:10 +0900265 f2fs_update_data_blkaddr(&dn, NEW_ADDR);
266 } else
267 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
Chao Yu28bc1062016-02-06 14:40:34 +0800268 cur->old_addr, ni.version, true, true);
269 f2fs_put_dnode(&dn);
270 }
271next:
Jaegeuk Kim63c52d72016-04-12 14:11:03 -0700272 /* we don't need to invalidate this in the sccessful status */
Chao Yu2baf0782018-07-27 18:15:16 +0800273 if (drop || recover) {
Jaegeuk Kim63c52d72016-04-12 14:11:03 -0700274 ClearPageUptodate(page);
Chao Yub763f3b2021-04-28 17:20:31 +0800275 clear_page_private_gcing(page);
Chao Yu2baf0782018-07-27 18:15:16 +0800276 }
Chao Yub763f3b2021-04-28 17:20:31 +0800277 detach_page_private(page);
278 set_page_private(page, 0);
Chao Yu28bc1062016-02-06 14:40:34 +0800279 f2fs_put_page(page, 1);
Chao Yudecd36b2015-08-07 18:42:09 +0800280
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700281 list_del(&cur->list);
282 kmem_cache_free(inmem_entry_slab, cur);
Jaegeuk Kim8dcf2ff72014-12-05 17:18:15 -0800283 dec_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700284 }
Chao Yu28bc1062016-02-06 14:40:34 +0800285 return err;
Chao Yu29b96b52016-02-06 14:38:29 +0800286}
287
Chao Yu4d57b862018-05-30 00:20:41 +0800288void f2fs_drop_inmem_pages_all(struct f2fs_sb_info *sbi, bool gc_failure)
Jaegeuk Kim57864ae2017-10-18 19:05:57 -0700289{
290 struct list_head *head = &sbi->inode_list[ATOMIC_FILE];
291 struct inode *inode;
292 struct f2fs_inode_info *fi;
Sahitya Tummala677017d2019-11-13 16:01:03 +0530293 unsigned int count = sbi->atomic_files;
294 unsigned int looped = 0;
Jaegeuk Kim57864ae2017-10-18 19:05:57 -0700295next:
296 spin_lock(&sbi->inode_lock[ATOMIC_FILE]);
297 if (list_empty(head)) {
298 spin_unlock(&sbi->inode_lock[ATOMIC_FILE]);
299 return;
300 }
301 fi = list_first_entry(head, struct f2fs_inode_info, inmem_ilist);
302 inode = igrab(&fi->vfs_inode);
Sahitya Tummala677017d2019-11-13 16:01:03 +0530303 if (inode)
304 list_move_tail(&fi->inmem_ilist, head);
Jaegeuk Kim57864ae2017-10-18 19:05:57 -0700305 spin_unlock(&sbi->inode_lock[ATOMIC_FILE]);
306
307 if (inode) {
Chao Yu2ef79ec2018-05-07 20:28:54 +0800308 if (gc_failure) {
Sahitya Tummala677017d2019-11-13 16:01:03 +0530309 if (!fi->i_gc_failures[GC_FAILURE_ATOMIC])
310 goto skip;
Chao Yu2ef79ec2018-05-07 20:28:54 +0800311 }
Chao Yu2ef79ec2018-05-07 20:28:54 +0800312 set_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
Chao Yu4d57b862018-05-30 00:20:41 +0800313 f2fs_drop_inmem_pages(inode);
Sahitya Tummala677017d2019-11-13 16:01:03 +0530314skip:
Jaegeuk Kim57864ae2017-10-18 19:05:57 -0700315 iput(inode);
316 }
Chao Yu5df7731f2020-02-17 17:45:44 +0800317 congestion_wait(BLK_RW_ASYNC, DEFAULT_IO_TIMEOUT);
Jaegeuk Kim57864ae2017-10-18 19:05:57 -0700318 cond_resched();
Sahitya Tummala677017d2019-11-13 16:01:03 +0530319 if (gc_failure) {
320 if (++looped >= count)
321 return;
322 }
Jaegeuk Kim57864ae2017-10-18 19:05:57 -0700323 goto next;
324}
325
Chao Yu4d57b862018-05-30 00:20:41 +0800326void f2fs_drop_inmem_pages(struct inode *inode)
Chao Yu29b96b52016-02-06 14:38:29 +0800327{
Jaegeuk Kim57864ae2017-10-18 19:05:57 -0700328 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu29b96b52016-02-06 14:38:29 +0800329 struct f2fs_inode_info *fi = F2FS_I(inode);
330
Yi Zhuangbe1ee452021-03-31 17:34:14 +0800331 do {
Chao Yu48432982019-02-25 17:11:03 +0800332 mutex_lock(&fi->inmem_lock);
Yi Zhuangbe1ee452021-03-31 17:34:14 +0800333 if (list_empty(&fi->inmem_pages)) {
334 fi->i_gc_failures[GC_FAILURE_ATOMIC] = 0;
335
336 spin_lock(&sbi->inode_lock[ATOMIC_FILE]);
337 if (!list_empty(&fi->inmem_ilist))
338 list_del_init(&fi->inmem_ilist);
339 if (f2fs_is_atomic_file(inode)) {
340 clear_inode_flag(inode, FI_ATOMIC_FILE);
341 sbi->atomic_files--;
342 }
343 spin_unlock(&sbi->inode_lock[ATOMIC_FILE]);
344
345 mutex_unlock(&fi->inmem_lock);
346 break;
347 }
Chao Yu48432982019-02-25 17:11:03 +0800348 __revoke_inmem_pages(inode, &fi->inmem_pages,
349 true, false, true);
Chao Yu48432982019-02-25 17:11:03 +0800350 mutex_unlock(&fi->inmem_lock);
Yi Zhuangbe1ee452021-03-31 17:34:14 +0800351 } while (1);
Chao Yu29b96b52016-02-06 14:38:29 +0800352}
353
Chao Yu4d57b862018-05-30 00:20:41 +0800354void f2fs_drop_inmem_page(struct inode *inode, struct page *page)
Jaegeuk Kim8c242db2017-03-17 09:55:52 +0800355{
356 struct f2fs_inode_info *fi = F2FS_I(inode);
357 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
358 struct list_head *head = &fi->inmem_pages;
359 struct inmem_pages *cur = NULL;
360
Chao Yub763f3b2021-04-28 17:20:31 +0800361 f2fs_bug_on(sbi, !page_private_atomic(page));
Jaegeuk Kim8c242db2017-03-17 09:55:52 +0800362
363 mutex_lock(&fi->inmem_lock);
364 list_for_each_entry(cur, head, list) {
365 if (cur->page == page)
366 break;
367 }
368
Sheng Yongd0891e82018-04-17 17:12:27 +0800369 f2fs_bug_on(sbi, list_empty(head) || cur->page != page);
Jaegeuk Kim8c242db2017-03-17 09:55:52 +0800370 list_del(&cur->list);
371 mutex_unlock(&fi->inmem_lock);
372
373 dec_page_count(sbi, F2FS_INMEM_PAGES);
374 kmem_cache_free(inmem_entry_slab, cur);
375
376 ClearPageUptodate(page);
Chao Yub763f3b2021-04-28 17:20:31 +0800377 clear_page_private_atomic(page);
Jaegeuk Kim8c242db2017-03-17 09:55:52 +0800378 f2fs_put_page(page, 0);
379
Chao Yub763f3b2021-04-28 17:20:31 +0800380 detach_page_private(page);
381 set_page_private(page, 0);
382
Jaegeuk Kim8c242db2017-03-17 09:55:52 +0800383 trace_f2fs_commit_inmem_page(page, INMEM_INVALIDATE);
384}
385
Chao Yu4d57b862018-05-30 00:20:41 +0800386static int __f2fs_commit_inmem_pages(struct inode *inode)
Chao Yu29b96b52016-02-06 14:38:29 +0800387{
388 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
389 struct f2fs_inode_info *fi = F2FS_I(inode);
390 struct inmem_pages *cur, *tmp;
391 struct f2fs_io_info fio = {
392 .sbi = sbi,
Chao Yu39d787b2017-09-29 13:59:38 +0800393 .ino = inode->i_ino,
Chao Yu29b96b52016-02-06 14:38:29 +0800394 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -0500395 .op = REQ_OP_WRITE,
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600396 .op_flags = REQ_SYNC | REQ_PRIO,
Chao Yub0af6d42017-08-02 23:21:48 +0800397 .io_type = FS_DATA_IO,
Chao Yu29b96b52016-02-06 14:38:29 +0800398 };
Chao Yucf52b272018-04-23 10:36:14 +0800399 struct list_head revoke_list;
Chao Yubab475c2018-09-27 23:41:16 +0800400 bool submit_bio = false;
Chao Yu29b96b52016-02-06 14:38:29 +0800401 int err = 0;
402
Chao Yucf52b272018-04-23 10:36:14 +0800403 INIT_LIST_HEAD(&revoke_list);
404
Chao Yu29b96b52016-02-06 14:38:29 +0800405 list_for_each_entry_safe(cur, tmp, &fi->inmem_pages, list) {
Chao Yu28bc1062016-02-06 14:40:34 +0800406 struct page *page = cur->page;
407
408 lock_page(page);
409 if (page->mapping == inode->i_mapping) {
410 trace_f2fs_commit_inmem_page(page, INMEM);
411
Chao Yubae0ee72018-12-25 17:43:42 +0800412 f2fs_wait_on_page_writeback(page, DATA, true, true);
Chao Yu8d64d362018-12-12 18:12:30 +0800413
414 set_page_dirty(page);
Chao Yu933439c2016-10-11 22:57:01 +0800415 if (clear_page_dirty_for_io(page)) {
Chao Yu29b96b52016-02-06 14:38:29 +0800416 inode_dec_dirty_pages(inode);
Chao Yu4d57b862018-05-30 00:20:41 +0800417 f2fs_remove_dirty_inode(inode);
Chao Yu933439c2016-10-11 22:57:01 +0800418 }
Jaegeuk Kim640cc182017-07-19 10:59:55 -0700419retry:
Chao Yu28bc1062016-02-06 14:40:34 +0800420 fio.page = page;
Hou Pengyange959c8f2017-04-25 12:45:13 +0000421 fio.old_blkaddr = NULL_ADDR;
Jaegeuk Kim4d978072017-04-26 11:11:12 -0700422 fio.encrypted_page = NULL;
Jaegeuk Kimcc156202017-05-12 13:51:34 -0700423 fio.need_lock = LOCK_DONE;
Chao Yu4d57b862018-05-30 00:20:41 +0800424 err = f2fs_do_write_data_page(&fio);
Chao Yu29b96b52016-02-06 14:38:29 +0800425 if (err) {
Jaegeuk Kim640cc182017-07-19 10:59:55 -0700426 if (err == -ENOMEM) {
Chao Yu5df7731f2020-02-17 17:45:44 +0800427 congestion_wait(BLK_RW_ASYNC,
428 DEFAULT_IO_TIMEOUT);
Jaegeuk Kim640cc182017-07-19 10:59:55 -0700429 cond_resched();
430 goto retry;
431 }
Chao Yu28bc1062016-02-06 14:40:34 +0800432 unlock_page(page);
Chao Yu29b96b52016-02-06 14:38:29 +0800433 break;
434 }
Chao Yu28bc1062016-02-06 14:40:34 +0800435 /* record old blkaddr for revoking */
436 cur->old_addr = fio.old_blkaddr;
Chao Yubab475c2018-09-27 23:41:16 +0800437 submit_bio = true;
Chao Yu29b96b52016-02-06 14:38:29 +0800438 }
Chao Yu28bc1062016-02-06 14:40:34 +0800439 unlock_page(page);
Chao Yucf52b272018-04-23 10:36:14 +0800440 list_move_tail(&cur->list, &revoke_list);
Chao Yu29b96b52016-02-06 14:38:29 +0800441 }
442
Chao Yubab475c2018-09-27 23:41:16 +0800443 if (submit_bio)
444 f2fs_submit_merged_write_cond(sbi, inode, NULL, 0, DATA);
Chao Yu28bc1062016-02-06 14:40:34 +0800445
Chao Yu28bc1062016-02-06 14:40:34 +0800446 if (err) {
Chao Yu28bc1062016-02-06 14:40:34 +0800447 /*
448 * try to revoke all committed pages, but still we could fail
449 * due to no memory or other reason, if that happened, EAGAIN
450 * will be returned, which means in such case, transaction is
451 * already not integrity, caller should use journal to do the
452 * recovery or rewrite & commit last transaction. For other
453 * error number, revoking was done by filesystem itself.
454 */
Chao Yu48432982019-02-25 17:11:03 +0800455 err = __revoke_inmem_pages(inode, &revoke_list,
456 false, true, false);
Chao Yu28bc1062016-02-06 14:40:34 +0800457
458 /* drop all uncommitted pages */
Chao Yu48432982019-02-25 17:11:03 +0800459 __revoke_inmem_pages(inode, &fi->inmem_pages,
460 true, false, false);
Chao Yucf52b272018-04-23 10:36:14 +0800461 } else {
Chao Yu48432982019-02-25 17:11:03 +0800462 __revoke_inmem_pages(inode, &revoke_list,
463 false, false, false);
Chao Yu28bc1062016-02-06 14:40:34 +0800464 }
Chao Yucf52b272018-04-23 10:36:14 +0800465
466 return err;
467}
468
Chao Yu4d57b862018-05-30 00:20:41 +0800469int f2fs_commit_inmem_pages(struct inode *inode)
Chao Yucf52b272018-04-23 10:36:14 +0800470{
471 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
472 struct f2fs_inode_info *fi = F2FS_I(inode);
473 int err;
474
475 f2fs_balance_fs(sbi, true);
Chao Yucf52b272018-04-23 10:36:14 +0800476
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +0900477 down_write(&fi->i_gc_rwsem[WRITE]);
478
479 f2fs_lock_op(sbi);
Chao Yucf52b272018-04-23 10:36:14 +0800480 set_inode_flag(inode, FI_ATOMIC_COMMIT);
481
482 mutex_lock(&fi->inmem_lock);
Chao Yu4d57b862018-05-30 00:20:41 +0800483 err = __f2fs_commit_inmem_pages(inode);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700484 mutex_unlock(&fi->inmem_lock);
485
Chao Yu5fe45742017-01-07 18:50:26 +0800486 clear_inode_flag(inode, FI_ATOMIC_COMMIT);
487
Chao Yu29b96b52016-02-06 14:38:29 +0800488 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +0900489 up_write(&fi->i_gc_rwsem[WRITE]);
490
Jaegeuk Kimedb27de2015-07-25 00:52:52 -0700491 return err;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700492}
493
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900494/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900495 * This function balances dirty node and dentry pages.
496 * In addition, it controls garbage collection.
497 */
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800498void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900499{
Chao Yu55523512017-02-25 11:08:28 +0800500 if (time_to_inject(sbi, FAULT_CHECKPOINT)) {
Chao Yuc45d6002019-11-01 17:53:23 +0800501 f2fs_show_injection_info(sbi, FAULT_CHECKPOINT);
Chao Yu0f348022016-09-26 19:45:55 +0800502 f2fs_stop_checkpoint(sbi, false);
Chao Yu55523512017-02-25 11:08:28 +0800503 }
Chao Yu0f348022016-09-26 19:45:55 +0800504
Jaegeuk Kime589c2c2016-06-02 15:24:24 -0700505 /* balance_fs_bg is able to be pending */
Jaegeuk Kima7881892017-04-20 13:51:57 -0700506 if (need && excess_cached_nats(sbi))
Chao Yu7bcd0cf2020-03-19 19:57:58 +0800507 f2fs_balance_fs_bg(sbi, false);
Jaegeuk Kime589c2c2016-06-02 15:24:24 -0700508
Chao Yu00e09c02019-08-23 17:58:36 +0800509 if (!f2fs_is_checkpoint_ready(sbi))
Daniel Rosenberg43549942018-08-20 19:21:43 -0700510 return;
511
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900512 /*
Jaegeuk Kim029cd282012-12-21 17:20:21 +0900513 * We should do GC or end up with checkpoint, if there are so many dirty
514 * dir/node pages without enough free segments.
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900515 */
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -0700516 if (has_not_enough_free_secs(sbi, 0, 0)) {
Chao Yu5911d2d2021-03-27 17:57:06 +0800517 if (test_opt(sbi, GC_MERGE) && sbi->gc_thread &&
518 sbi->gc_thread->f2fs_gc_task) {
519 DEFINE_WAIT(wait);
520
521 prepare_to_wait(&sbi->gc_thread->fggc_wq, &wait,
522 TASK_UNINTERRUPTIBLE);
523 wake_up(&sbi->gc_thread->gc_wait_queue_head);
524 io_schedule();
525 finish_wait(&sbi->gc_thread->fggc_wq, &wait);
526 } else {
527 down_write(&sbi->gc_lock);
528 f2fs_gc(sbi, false, false, false, NULL_SEGNO);
529 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900530 }
531}
532
Chao Yu287b1402021-09-16 17:09:03 +0800533static inline bool excess_dirty_threshold(struct f2fs_sb_info *sbi)
534{
535 int factor = rwsem_is_locked(&sbi->cp_rwsem) ? 3 : 2;
536 unsigned int dents = get_pages(sbi, F2FS_DIRTY_DENTS);
537 unsigned int qdata = get_pages(sbi, F2FS_DIRTY_QDATA);
538 unsigned int nodes = get_pages(sbi, F2FS_DIRTY_NODES);
539 unsigned int meta = get_pages(sbi, F2FS_DIRTY_META);
540 unsigned int imeta = get_pages(sbi, F2FS_DIRTY_IMETA);
541 unsigned int threshold = sbi->blocks_per_seg * factor *
542 DEFAULT_DIRTY_THRESHOLD;
543 unsigned int global_threshold = threshold * 3 / 2;
544
545 if (dents >= threshold || qdata >= threshold ||
546 nodes >= threshold || meta >= threshold ||
547 imeta >= threshold)
548 return true;
549 return dents + qdata + nodes + meta + imeta > global_threshold;
550}
551
Chao Yu7bcd0cf2020-03-19 19:57:58 +0800552void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi, bool from_bg)
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900553{
Chao Yu64c74a72018-05-26 18:03:34 +0800554 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
555 return;
556
Chao Yu1dcc3362015-02-05 17:57:31 +0800557 /* try to shrink extent cache when there is no enough memory */
Chao Yu4d57b862018-05-30 00:20:41 +0800558 if (!f2fs_available_free_memory(sbi, EXTENT_CACHE))
Jaegeuk Kim554df792015-06-19 13:41:23 -0700559 f2fs_shrink_extent_tree(sbi, EXTENT_CACHE_SHRINK_NUMBER);
Chao Yu1dcc3362015-02-05 17:57:31 +0800560
Jaegeuk Kim1b38dc82015-06-19 15:36:07 -0700561 /* check the # of cached NAT entries */
Chao Yu4d57b862018-05-30 00:20:41 +0800562 if (!f2fs_available_free_memory(sbi, NAT_ENTRIES))
563 f2fs_try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK);
Jaegeuk Kim1b38dc82015-06-19 15:36:07 -0700564
Chao Yu4d57b862018-05-30 00:20:41 +0800565 if (!f2fs_available_free_memory(sbi, FREE_NIDS))
566 f2fs_try_to_free_nids(sbi, MAX_FREE_NIDS);
Jaegeuk Kimad4edb82016-06-16 16:41:49 -0700567 else
Chao Yu4d57b862018-05-30 00:20:41 +0800568 f2fs_build_free_nids(sbi, false, false);
Chao Yu31696582015-07-28 18:33:46 +0800569
Chao Yu287b1402021-09-16 17:09:03 +0800570 if (excess_dirty_nats(sbi) || excess_dirty_threshold(sbi) ||
571 excess_prefree_segs(sbi) || !f2fs_space_for_roll_forward(sbi))
Chao Yu493720a2020-11-25 10:57:36 +0800572 goto do_sync;
573
574 /* there is background inflight IO or foreground operation recently */
575 if (is_inflight_io(sbi, REQ_TIME) ||
576 (!f2fs_time_over(sbi, REQ_TIME) && rwsem_is_locked(&sbi->cp_rwsem)))
Jaegeuk Kimf455c8a2016-12-05 11:37:14 -0800577 return;
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -0800578
Chao Yu493720a2020-11-25 10:57:36 +0800579 /* exceed periodical checkpoint timeout threshold */
580 if (f2fs_time_over(sbi, CP_TIME))
581 goto do_sync;
582
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900583 /* checkpoint is the only way to shrink partial cached entries */
Chao Yucd6d697a62021-09-29 03:19:14 +0800584 if (f2fs_available_free_memory(sbi, NAT_ENTRIES) &&
Chao Yu493720a2020-11-25 10:57:36 +0800585 f2fs_available_free_memory(sbi, INO_ENTRIES))
586 return;
Chao Yue9f5b8b2016-02-14 18:54:33 +0800587
Chao Yu493720a2020-11-25 10:57:36 +0800588do_sync:
589 if (test_opt(sbi, DATA_FLUSH) && from_bg) {
590 struct blk_plug plug;
Chao Yu040d2bb2019-05-20 17:36:59 +0800591
Chao Yu493720a2020-11-25 10:57:36 +0800592 mutex_lock(&sbi->flush_lock);
Chao Yu040d2bb2019-05-20 17:36:59 +0800593
Chao Yu493720a2020-11-25 10:57:36 +0800594 blk_start_plug(&plug);
595 f2fs_sync_dirty_inodes(sbi, FILE_INODE);
596 blk_finish_plug(&plug);
597
598 mutex_unlock(&sbi->flush_lock);
Chao Yu36b35a02015-12-17 17:13:28 +0800599 }
Chao Yu493720a2020-11-25 10:57:36 +0800600 f2fs_sync_fs(sbi->sb, true);
601 stat_inc_bg_cp_count(sbi->stat_info);
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900602}
603
Kinglong Mee20fda56b2017-03-04 22:13:10 +0800604static int __submit_flush_wait(struct f2fs_sb_info *sbi,
605 struct block_device *bdev)
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700606{
Christoph Hellwig25ac8422021-01-26 15:52:37 +0100607 int ret = blkdev_issue_flush(bdev);
Kinglong Mee20fda56b2017-03-04 22:13:10 +0800608
609 trace_f2fs_issue_flush(bdev, test_opt(sbi, NOBARRIER),
610 test_opt(sbi, FLUSH_MERGE), ret);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700611 return ret;
612}
613
Chao Yu39d787b2017-09-29 13:59:38 +0800614static int submit_flush_wait(struct f2fs_sb_info *sbi, nid_t ino)
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700615{
Chao Yu39d787b2017-09-29 13:59:38 +0800616 int ret = 0;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700617 int i;
618
Damien Le Moal09168782019-03-16 09:13:06 +0900619 if (!f2fs_is_multi_device(sbi))
Chao Yu39d787b2017-09-29 13:59:38 +0800620 return __submit_flush_wait(sbi, sbi->sb->s_bdev);
Kinglong Mee20fda56b2017-03-04 22:13:10 +0800621
Chao Yu39d787b2017-09-29 13:59:38 +0800622 for (i = 0; i < sbi->s_ndevs; i++) {
Chao Yu4d57b862018-05-30 00:20:41 +0800623 if (!f2fs_is_dirty_device(sbi, ino, i, FLUSH_INO))
Chao Yu39d787b2017-09-29 13:59:38 +0800624 continue;
Kinglong Mee20fda56b2017-03-04 22:13:10 +0800625 ret = __submit_flush_wait(sbi, FDEV(i).bdev);
626 if (ret)
627 break;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700628 }
629 return ret;
630}
631
Gu Zheng2163d192014-04-27 14:21:33 +0800632static int issue_flush_thread(void *data)
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900633{
634 struct f2fs_sb_info *sbi = data;
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800635 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800636 wait_queue_head_t *q = &fcc->flush_wait_queue;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900637repeat:
638 if (kthread_should_stop())
639 return 0;
640
Gu Zheng721bd4d2014-09-05 18:31:00 +0800641 if (!llist_empty(&fcc->issue_list)) {
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900642 struct flush_cmd *cmd, *next;
643 int ret;
644
Gu Zheng721bd4d2014-09-05 18:31:00 +0800645 fcc->dispatch_list = llist_del_all(&fcc->issue_list);
646 fcc->dispatch_list = llist_reverse_order(fcc->dispatch_list);
647
Chao Yu39d787b2017-09-29 13:59:38 +0800648 cmd = llist_entry(fcc->dispatch_list, struct flush_cmd, llnode);
649
650 ret = submit_flush_wait(sbi, cmd->ino);
Chao Yu8b8dd652017-03-25 17:19:58 +0800651 atomic_inc(&fcc->issued_flush);
652
Gu Zheng721bd4d2014-09-05 18:31:00 +0800653 llist_for_each_entry_safe(cmd, next,
654 fcc->dispatch_list, llnode) {
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900655 cmd->ret = ret;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900656 complete(&cmd->wait);
657 }
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800658 fcc->dispatch_list = NULL;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900659 }
660
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800661 wait_event_interruptible(*q,
Gu Zheng721bd4d2014-09-05 18:31:00 +0800662 kthread_should_stop() || !llist_empty(&fcc->issue_list));
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900663 goto repeat;
664}
665
Chao Yu39d787b2017-09-29 13:59:38 +0800666int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino)
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900667{
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800668 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
Chao Yuadf8d902014-05-08 17:00:35 +0800669 struct flush_cmd cmd;
Chao Yu8b8dd652017-03-25 17:19:58 +0800670 int ret;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900671
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700672 if (test_opt(sbi, NOBARRIER))
673 return 0;
674
Chao Yu8b8dd652017-03-25 17:19:58 +0800675 if (!test_opt(sbi, FLUSH_MERGE)) {
Jaegeuk Kim72691af2018-12-13 16:53:57 -0800676 atomic_inc(&fcc->queued_flush);
Chao Yu39d787b2017-09-29 13:59:38 +0800677 ret = submit_flush_wait(sbi, ino);
Jaegeuk Kim72691af2018-12-13 16:53:57 -0800678 atomic_dec(&fcc->queued_flush);
Chao Yu8b8dd652017-03-25 17:19:58 +0800679 atomic_inc(&fcc->issued_flush);
680 return ret;
681 }
682
Damien Le Moal09168782019-03-16 09:13:06 +0900683 if (atomic_inc_return(&fcc->queued_flush) == 1 ||
684 f2fs_is_multi_device(sbi)) {
Chao Yu39d787b2017-09-29 13:59:38 +0800685 ret = submit_flush_wait(sbi, ino);
Jaegeuk Kim72691af2018-12-13 16:53:57 -0800686 atomic_dec(&fcc->queued_flush);
Chao Yu8b8dd652017-03-25 17:19:58 +0800687
688 atomic_inc(&fcc->issued_flush);
Jaegeuk Kim740432f2015-08-14 11:43:56 -0700689 return ret;
690 }
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900691
Chao Yu39d787b2017-09-29 13:59:38 +0800692 cmd.ino = ino;
Chao Yuadf8d902014-05-08 17:00:35 +0800693 init_completion(&cmd.wait);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900694
Gu Zheng721bd4d2014-09-05 18:31:00 +0800695 llist_add(&cmd.llnode, &fcc->issue_list);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900696
Chao Yu3b42c742021-02-20 17:38:43 +0800697 /*
698 * update issue_list before we wake up issue_flush thread, this
699 * smp_mb() pairs with another barrier in ___wait_event(), see
700 * more details in comments of waitqueue_active().
701 */
Chao Yu6f890df2017-08-21 22:53:45 +0800702 smp_mb();
703
704 if (waitqueue_active(&fcc->flush_wait_queue))
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800705 wake_up(&fcc->flush_wait_queue);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900706
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800707 if (fcc->f2fs_issue_flush) {
708 wait_for_completion(&cmd.wait);
Jaegeuk Kim72691af2018-12-13 16:53:57 -0800709 atomic_dec(&fcc->queued_flush);
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800710 } else {
Chao Yud3238692017-08-31 18:56:06 +0800711 struct llist_node *list;
712
713 list = llist_del_all(&fcc->issue_list);
714 if (!list) {
715 wait_for_completion(&cmd.wait);
Jaegeuk Kim72691af2018-12-13 16:53:57 -0800716 atomic_dec(&fcc->queued_flush);
Chao Yud3238692017-08-31 18:56:06 +0800717 } else {
718 struct flush_cmd *tmp, *next;
719
Chao Yu39d787b2017-09-29 13:59:38 +0800720 ret = submit_flush_wait(sbi, ino);
Chao Yud3238692017-08-31 18:56:06 +0800721
722 llist_for_each_entry_safe(tmp, next, list, llnode) {
723 if (tmp == &cmd) {
724 cmd.ret = ret;
Jaegeuk Kim72691af2018-12-13 16:53:57 -0800725 atomic_dec(&fcc->queued_flush);
Chao Yud3238692017-08-31 18:56:06 +0800726 continue;
727 }
728 tmp->ret = ret;
729 complete(&tmp->wait);
730 }
731 }
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800732 }
Chao Yuadf8d902014-05-08 17:00:35 +0800733
734 return cmd.ret;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900735}
736
Chao Yu4d57b862018-05-30 00:20:41 +0800737int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi)
Gu Zheng2163d192014-04-27 14:21:33 +0800738{
739 dev_t dev = sbi->sb->s_bdev->bd_dev;
740 struct flush_cmd_control *fcc;
741 int err = 0;
742
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800743 if (SM_I(sbi)->fcc_info) {
744 fcc = SM_I(sbi)->fcc_info;
Yunlong Songd871cd02017-06-24 15:57:19 +0800745 if (fcc->f2fs_issue_flush)
746 return err;
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800747 goto init_thread;
748 }
749
Chao Yuacbf0542017-11-30 19:28:17 +0800750 fcc = f2fs_kzalloc(sbi, sizeof(struct flush_cmd_control), GFP_KERNEL);
Gu Zheng2163d192014-04-27 14:21:33 +0800751 if (!fcc)
752 return -ENOMEM;
Chao Yu8b8dd652017-03-25 17:19:58 +0800753 atomic_set(&fcc->issued_flush, 0);
Jaegeuk Kim72691af2018-12-13 16:53:57 -0800754 atomic_set(&fcc->queued_flush, 0);
Gu Zheng2163d192014-04-27 14:21:33 +0800755 init_waitqueue_head(&fcc->flush_wait_queue);
Gu Zheng721bd4d2014-09-05 18:31:00 +0800756 init_llist_head(&fcc->issue_list);
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800757 SM_I(sbi)->fcc_info = fcc;
Yunlei Hed4fdf8b2017-06-01 16:43:51 +0800758 if (!test_opt(sbi, FLUSH_MERGE))
759 return err;
760
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800761init_thread:
Gu Zheng2163d192014-04-27 14:21:33 +0800762 fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi,
763 "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev));
764 if (IS_ERR(fcc->f2fs_issue_flush)) {
765 err = PTR_ERR(fcc->f2fs_issue_flush);
Chao Yuc8eb7022020-09-14 16:47:00 +0800766 kfree(fcc);
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800767 SM_I(sbi)->fcc_info = NULL;
Gu Zheng2163d192014-04-27 14:21:33 +0800768 return err;
769 }
Gu Zheng2163d192014-04-27 14:21:33 +0800770
771 return err;
772}
773
Chao Yu4d57b862018-05-30 00:20:41 +0800774void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free)
Gu Zheng2163d192014-04-27 14:21:33 +0800775{
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800776 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
Gu Zheng2163d192014-04-27 14:21:33 +0800777
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800778 if (fcc && fcc->f2fs_issue_flush) {
779 struct task_struct *flush_thread = fcc->f2fs_issue_flush;
780
781 fcc->f2fs_issue_flush = NULL;
782 kthread_stop(flush_thread);
783 }
784 if (free) {
Chao Yuc8eb7022020-09-14 16:47:00 +0800785 kfree(fcc);
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800786 SM_I(sbi)->fcc_info = NULL;
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800787 }
Gu Zheng2163d192014-04-27 14:21:33 +0800788}
789
Chao Yu1228b482017-09-29 13:59:39 +0800790int f2fs_flush_device_cache(struct f2fs_sb_info *sbi)
791{
792 int ret = 0, i;
793
Damien Le Moal09168782019-03-16 09:13:06 +0900794 if (!f2fs_is_multi_device(sbi))
Chao Yu1228b482017-09-29 13:59:39 +0800795 return 0;
796
Chao Yu6ed29fe2020-10-12 10:28:14 +0800797 if (test_opt(sbi, NOBARRIER))
798 return 0;
799
Chao Yu1228b482017-09-29 13:59:39 +0800800 for (i = 1; i < sbi->s_ndevs; i++) {
Chao Yu91803392021-08-04 08:38:38 +0800801 int count = DEFAULT_RETRY_IO_COUNT;
802
Chao Yu1228b482017-09-29 13:59:39 +0800803 if (!f2fs_test_bit(i, (char *)&sbi->dirty_device))
804 continue;
Chao Yu91803392021-08-04 08:38:38 +0800805
806 do {
807 ret = __submit_flush_wait(sbi, FDEV(i).bdev);
808 if (ret)
809 congestion_wait(BLK_RW_ASYNC,
810 DEFAULT_IO_TIMEOUT);
811 } while (ret && --count);
812
813 if (ret) {
814 f2fs_stop_checkpoint(sbi, false);
Chao Yu1228b482017-09-29 13:59:39 +0800815 break;
Chao Yu91803392021-08-04 08:38:38 +0800816 }
Chao Yu1228b482017-09-29 13:59:39 +0800817
818 spin_lock(&sbi->dev_lock);
819 f2fs_clear_bit(i, (char *)&sbi->dirty_device);
820 spin_unlock(&sbi->dev_lock);
821 }
822
823 return ret;
824}
825
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900826static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
827 enum dirty_type dirty_type)
828{
829 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
830
831 /* need not be added */
832 if (IS_CURSEG(sbi, segno))
833 return;
834
835 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
836 dirty_i->nr_dirty[dirty_type]++;
837
838 if (dirty_type == DIRTY) {
839 struct seg_entry *sentry = get_seg_entry(sbi, segno);
Changman Lee4625d6a2013-10-25 17:31:57 +0900840 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900841
Jaegeuk Kimec325b52014-09-02 16:24:11 -0700842 if (unlikely(t >= DIRTY)) {
843 f2fs_bug_on(sbi, 1);
844 return;
845 }
Changman Lee4625d6a2013-10-25 17:31:57 +0900846 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
847 dirty_i->nr_dirty[t]++;
Jack Qiuda52f8a2020-06-18 12:37:10 +0800848
849 if (__is_large_section(sbi)) {
850 unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
Shin'ichiro Kawasaki123aaf72020-08-19 10:34:48 +0900851 block_t valid_blocks =
Jack Qiuda52f8a2020-06-18 12:37:10 +0800852 get_valid_blocks(sbi, segno, true);
853
854 f2fs_bug_on(sbi, unlikely(!valid_blocks ||
855 valid_blocks == BLKS_PER_SEC(sbi)));
856
857 if (!IS_CURSEC(sbi, secno))
858 set_bit(secno, dirty_i->dirty_secmap);
859 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900860 }
861}
862
863static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
864 enum dirty_type dirty_type)
865{
866 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Shin'ichiro Kawasaki123aaf72020-08-19 10:34:48 +0900867 block_t valid_blocks;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900868
869 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
870 dirty_i->nr_dirty[dirty_type]--;
871
872 if (dirty_type == DIRTY) {
Changman Lee4625d6a2013-10-25 17:31:57 +0900873 struct seg_entry *sentry = get_seg_entry(sbi, segno);
874 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900875
Changman Lee4625d6a2013-10-25 17:31:57 +0900876 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
877 dirty_i->nr_dirty[t]--;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900878
Jack Qiuda52f8a2020-06-18 12:37:10 +0800879 valid_blocks = get_valid_blocks(sbi, segno, true);
880 if (valid_blocks == 0) {
Jaegeuk Kim4ddb1a42017-04-07 15:08:17 -0700881 clear_bit(GET_SEC_FROM_SEG(sbi, segno),
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +0900882 dirty_i->victim_secmap);
Sahitya Tummalabbf9f7d2019-08-07 19:10:32 +0530883#ifdef CONFIG_F2FS_CHECK_FS
884 clear_bit(segno, SIT_I(sbi)->invalid_segmap);
885#endif
886 }
Jack Qiuda52f8a2020-06-18 12:37:10 +0800887 if (__is_large_section(sbi)) {
888 unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
889
890 if (!valid_blocks ||
891 valid_blocks == BLKS_PER_SEC(sbi)) {
892 clear_bit(secno, dirty_i->dirty_secmap);
893 return;
894 }
895
896 if (!IS_CURSEC(sbi, secno))
897 set_bit(secno, dirty_i->dirty_secmap);
898 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900899 }
900}
901
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900902/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900903 * Should not occur error such as -ENOMEM.
904 * Adding dirty entry into seglist is not critical operation.
905 * If a given segment is one of current working segments, it won't be added.
906 */
Haicheng Li8d8451a2013-06-13 16:59:28 +0800907static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900908{
909 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Daniel Rosenberg43549942018-08-20 19:21:43 -0700910 unsigned short valid_blocks, ckpt_valid_blocks;
Aravind Rameshde881df2020-07-16 18:26:56 +0530911 unsigned int usable_blocks;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900912
913 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
914 return;
915
Aravind Rameshde881df2020-07-16 18:26:56 +0530916 usable_blocks = f2fs_usable_blks_in_seg(sbi, segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900917 mutex_lock(&dirty_i->seglist_lock);
918
Jaegeuk Kim302bd342017-04-07 14:33:22 -0700919 valid_blocks = get_valid_blocks(sbi, segno, false);
Chao Yu61461fc2021-03-24 11:18:28 +0800920 ckpt_valid_blocks = get_ckpt_valid_blocks(sbi, segno, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900921
Daniel Rosenberg43549942018-08-20 19:21:43 -0700922 if (valid_blocks == 0 && (!is_sbi_flag_set(sbi, SBI_CP_DISABLED) ||
Aravind Rameshde881df2020-07-16 18:26:56 +0530923 ckpt_valid_blocks == usable_blocks)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900924 __locate_dirty_segment(sbi, segno, PRE);
925 __remove_dirty_segment(sbi, segno, DIRTY);
Aravind Rameshde881df2020-07-16 18:26:56 +0530926 } else if (valid_blocks < usable_blocks) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900927 __locate_dirty_segment(sbi, segno, DIRTY);
928 } else {
929 /* Recovery routine with SSR needs this */
930 __remove_dirty_segment(sbi, segno, DIRTY);
931 }
932
933 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900934}
935
Daniel Rosenberg43549942018-08-20 19:21:43 -0700936/* This moves currently empty dirty blocks to prefree. Must hold seglist_lock */
937void f2fs_dirty_to_prefree(struct f2fs_sb_info *sbi)
938{
939 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
940 unsigned int segno;
941
942 mutex_lock(&dirty_i->seglist_lock);
943 for_each_set_bit(segno, dirty_i->dirty_segmap[DIRTY], MAIN_SEGS(sbi)) {
944 if (get_valid_blocks(sbi, segno, false))
945 continue;
946 if (IS_CURSEG(sbi, segno))
947 continue;
948 __locate_dirty_segment(sbi, segno, PRE);
949 __remove_dirty_segment(sbi, segno, DIRTY);
950 }
951 mutex_unlock(&dirty_i->seglist_lock);
952}
953
Daniel Rosenberg4d3aed72019-05-29 17:49:06 -0700954block_t f2fs_get_unusable_blocks(struct f2fs_sb_info *sbi)
Daniel Rosenberg43549942018-08-20 19:21:43 -0700955{
Daniel Rosenbergae4ad7e2019-05-29 17:49:03 -0700956 int ovp_hole_segs =
957 (overprovision_segments(sbi) - reserved_segments(sbi));
958 block_t ovp_holes = ovp_hole_segs << sbi->log_blocks_per_seg;
Daniel Rosenberg4d3aed72019-05-29 17:49:06 -0700959 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Daniel Rosenberg43549942018-08-20 19:21:43 -0700960 block_t holes[2] = {0, 0}; /* DATA and NODE */
Daniel Rosenberg4d3aed72019-05-29 17:49:06 -0700961 block_t unusable;
Daniel Rosenberg43549942018-08-20 19:21:43 -0700962 struct seg_entry *se;
963 unsigned int segno;
964
965 mutex_lock(&dirty_i->seglist_lock);
966 for_each_set_bit(segno, dirty_i->dirty_segmap[DIRTY], MAIN_SEGS(sbi)) {
967 se = get_seg_entry(sbi, segno);
968 if (IS_NODESEG(se->type))
Aravind Rameshde881df2020-07-16 18:26:56 +0530969 holes[NODE] += f2fs_usable_blks_in_seg(sbi, segno) -
970 se->valid_blocks;
Daniel Rosenberg43549942018-08-20 19:21:43 -0700971 else
Aravind Rameshde881df2020-07-16 18:26:56 +0530972 holes[DATA] += f2fs_usable_blks_in_seg(sbi, segno) -
973 se->valid_blocks;
Daniel Rosenberg43549942018-08-20 19:21:43 -0700974 }
975 mutex_unlock(&dirty_i->seglist_lock);
976
Daniel Rosenberg4d3aed72019-05-29 17:49:06 -0700977 unusable = holes[DATA] > holes[NODE] ? holes[DATA] : holes[NODE];
978 if (unusable > ovp_holes)
979 return unusable - ovp_holes;
980 return 0;
981}
982
983int f2fs_disable_cp_again(struct f2fs_sb_info *sbi, block_t unusable)
984{
985 int ovp_hole_segs =
986 (overprovision_segments(sbi) - reserved_segments(sbi));
987 if (unusable > F2FS_OPTION(sbi).unusable_cap)
Daniel Rosenberg43549942018-08-20 19:21:43 -0700988 return -EAGAIN;
Jaegeuk Kimdb610a62019-01-24 17:48:38 -0800989 if (is_sbi_flag_set(sbi, SBI_CP_DISABLED_QUICK) &&
Daniel Rosenbergae4ad7e2019-05-29 17:49:03 -0700990 dirty_segments(sbi) > ovp_hole_segs)
Jaegeuk Kimdb610a62019-01-24 17:48:38 -0800991 return -EAGAIN;
Daniel Rosenberg43549942018-08-20 19:21:43 -0700992 return 0;
993}
994
995/* This is only used by SBI_CP_DISABLED */
996static unsigned int get_free_segment(struct f2fs_sb_info *sbi)
997{
998 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
999 unsigned int segno = 0;
1000
1001 mutex_lock(&dirty_i->seglist_lock);
1002 for_each_set_bit(segno, dirty_i->dirty_segmap[DIRTY], MAIN_SEGS(sbi)) {
1003 if (get_valid_blocks(sbi, segno, false))
1004 continue;
Chao Yu61461fc2021-03-24 11:18:28 +08001005 if (get_ckpt_valid_blocks(sbi, segno, false))
Daniel Rosenberg43549942018-08-20 19:21:43 -07001006 continue;
1007 mutex_unlock(&dirty_i->seglist_lock);
1008 return segno;
1009 }
1010 mutex_unlock(&dirty_i->seglist_lock);
1011 return NULL_SEGNO;
1012}
1013
Chao Yu004b6862017-04-14 23:24:55 +08001014static struct discard_cmd *__create_discard_cmd(struct f2fs_sb_info *sbi,
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001015 struct block_device *bdev, block_t lstart,
1016 block_t start, block_t len)
Chao Yu275b66b2016-08-29 23:58:34 +08001017{
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001018 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Chao Yuba48a332017-04-15 14:09:37 +08001019 struct list_head *pend_list;
Jaegeuk Kimb01a9202017-01-09 14:13:03 -08001020 struct discard_cmd *dc;
Chao Yu275b66b2016-08-29 23:58:34 +08001021
Chao Yuba48a332017-04-15 14:09:37 +08001022 f2fs_bug_on(sbi, !len);
1023
1024 pend_list = &dcc->pend_list[plist_idx(len)];
1025
Chao Yu32410572021-08-09 08:24:48 +08001026 dc = f2fs_kmem_cache_alloc(discard_cmd_slab, GFP_NOFS, true, NULL);
Jaegeuk Kimb01a9202017-01-09 14:13:03 -08001027 INIT_LIST_HEAD(&dc->list);
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001028 dc->bdev = bdev;
Jaegeuk Kimb01a9202017-01-09 14:13:03 -08001029 dc->lstart = lstart;
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001030 dc->start = start;
Jaegeuk Kimb01a9202017-01-09 14:13:03 -08001031 dc->len = len;
Chao Yuec9895a2017-04-26 17:39:54 +08001032 dc->ref = 0;
Jaegeuk Kim15469962017-01-09 20:32:07 -08001033 dc->state = D_PREP;
Jaegeuk Kim72691af2018-12-13 16:53:57 -08001034 dc->queued = 0;
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001035 dc->error = 0;
Jaegeuk Kimb01a9202017-01-09 14:13:03 -08001036 init_completion(&dc->wait);
Chao Yu22d375d2017-04-05 18:19:48 +08001037 list_add_tail(&dc->list, pend_list);
Chao Yu35ec7d52018-08-06 22:43:50 +08001038 spin_lock_init(&dc->lock);
1039 dc->bio_ref = 0;
Chao Yu5f323662017-03-25 17:19:59 +08001040 atomic_inc(&dcc->discard_cmd_cnt);
Chao Yud84d1cb2017-04-18 19:27:39 +08001041 dcc->undiscard_blks += len;
Chao Yu004b6862017-04-14 23:24:55 +08001042
1043 return dc;
Jaegeuk Kim15469962017-01-09 20:32:07 -08001044}
1045
Chao Yu004b6862017-04-14 23:24:55 +08001046static struct discard_cmd *__attach_discard_cmd(struct f2fs_sb_info *sbi,
1047 struct block_device *bdev, block_t lstart,
1048 block_t start, block_t len,
Chao Yu4dada3f2018-10-04 11:18:30 +08001049 struct rb_node *parent, struct rb_node **p,
1050 bool leftmost)
Chao Yu004b6862017-04-14 23:24:55 +08001051{
1052 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1053 struct discard_cmd *dc;
1054
1055 dc = __create_discard_cmd(sbi, bdev, lstart, start, len);
1056
1057 rb_link_node(&dc->rb_node, parent, p);
Chao Yu4dada3f2018-10-04 11:18:30 +08001058 rb_insert_color_cached(&dc->rb_node, &dcc->root, leftmost);
Chao Yu004b6862017-04-14 23:24:55 +08001059
1060 return dc;
1061}
1062
1063static void __detach_discard_cmd(struct discard_cmd_control *dcc,
1064 struct discard_cmd *dc)
Jaegeuk Kim15469962017-01-09 20:32:07 -08001065{
Jaegeuk Kimdcc91652017-01-11 10:20:04 -08001066 if (dc->state == D_DONE)
Jaegeuk Kim72691af2018-12-13 16:53:57 -08001067 atomic_sub(dc->queued, &dcc->queued_discard);
Chao Yu004b6862017-04-14 23:24:55 +08001068
1069 list_del(&dc->list);
Chao Yu4dada3f2018-10-04 11:18:30 +08001070 rb_erase_cached(&dc->rb_node, &dcc->root);
Chao Yud84d1cb2017-04-18 19:27:39 +08001071 dcc->undiscard_blks -= dc->len;
Chao Yu004b6862017-04-14 23:24:55 +08001072
1073 kmem_cache_free(discard_cmd_slab, dc);
1074
1075 atomic_dec(&dcc->discard_cmd_cnt);
1076}
1077
1078static void __remove_discard_cmd(struct f2fs_sb_info *sbi,
1079 struct discard_cmd *dc)
1080{
1081 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Chao Yu35ec7d52018-08-06 22:43:50 +08001082 unsigned long flags;
Jaegeuk Kimdcc91652017-01-11 10:20:04 -08001083
Chao Yu2ec6f2e2017-10-04 09:08:36 +08001084 trace_f2fs_remove_discard(dc->bdev, dc->start, dc->len);
1085
Chao Yu35ec7d52018-08-06 22:43:50 +08001086 spin_lock_irqsave(&dc->lock, flags);
1087 if (dc->bio_ref) {
1088 spin_unlock_irqrestore(&dc->lock, flags);
1089 return;
1090 }
1091 spin_unlock_irqrestore(&dc->lock, flags);
1092
Chao Yud9703d92017-06-05 18:29:07 +08001093 f2fs_bug_on(sbi, dc->ref);
1094
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001095 if (dc->error == -EOPNOTSUPP)
1096 dc->error = 0;
Jaegeuk Kim15469962017-01-09 20:32:07 -08001097
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001098 if (dc->error)
Chao Yu22d7ea12018-08-22 17:17:47 +08001099 printk_ratelimited(
Chao Yuc45d6002019-11-01 17:53:23 +08001100 "%sF2FS-fs (%s): Issue discard(%u, %u, %u) failed, ret: %d",
1101 KERN_INFO, sbi->sb->s_id,
1102 dc->lstart, dc->start, dc->len, dc->error);
Chao Yu004b6862017-04-14 23:24:55 +08001103 __detach_discard_cmd(dcc, dc);
Chao Yu275b66b2016-08-29 23:58:34 +08001104}
1105
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001106static void f2fs_submit_discard_endio(struct bio *bio)
1107{
1108 struct discard_cmd *dc = (struct discard_cmd *)bio->bi_private;
Chao Yu35ec7d52018-08-06 22:43:50 +08001109 unsigned long flags;
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001110
Chao Yu35ec7d52018-08-06 22:43:50 +08001111 spin_lock_irqsave(&dc->lock, flags);
Sahitya Tummala3fa6a8c2020-04-15 09:35:54 +05301112 if (!dc->error)
1113 dc->error = blk_status_to_errno(bio->bi_status);
Chao Yu35ec7d52018-08-06 22:43:50 +08001114 dc->bio_ref--;
1115 if (!dc->bio_ref && dc->state == D_SUBMIT) {
1116 dc->state = D_DONE;
1117 complete_all(&dc->wait);
1118 }
1119 spin_unlock_irqrestore(&dc->lock, flags);
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001120 bio_put(bio);
1121}
1122
Wei Yongjun94b1e102018-01-05 09:41:20 +00001123static void __check_sit_bitmap(struct f2fs_sb_info *sbi,
Chao Yu6915ea92017-06-30 17:19:02 +08001124 block_t start, block_t end)
1125{
1126#ifdef CONFIG_F2FS_CHECK_FS
1127 struct seg_entry *sentry;
1128 unsigned int segno;
1129 block_t blk = start;
1130 unsigned long offset, size, max_blocks = sbi->blocks_per_seg;
1131 unsigned long *map;
1132
1133 while (blk < end) {
1134 segno = GET_SEGNO(sbi, blk);
1135 sentry = get_seg_entry(sbi, segno);
1136 offset = GET_BLKOFF_FROM_SEG0(sbi, blk);
1137
Yunlong Song008396e2017-08-04 17:07:15 +08001138 if (end < START_BLOCK(sbi, segno + 1))
1139 size = GET_BLKOFF_FROM_SEG0(sbi, end);
1140 else
1141 size = max_blocks;
Chao Yu6915ea92017-06-30 17:19:02 +08001142 map = (unsigned long *)(sentry->cur_valid_map);
1143 offset = __find_rev_next_bit(map, size, offset);
1144 f2fs_bug_on(sbi, offset != size);
Yunlong Song008396e2017-08-04 17:07:15 +08001145 blk = START_BLOCK(sbi, segno + 1);
Chao Yu6915ea92017-06-30 17:19:02 +08001146 }
1147#endif
1148}
1149
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -07001150static void __init_discard_policy(struct f2fs_sb_info *sbi,
1151 struct discard_policy *dpolicy,
1152 int discard_type, unsigned int granularity)
1153{
Sahitya Tummalac35b8d52021-04-06 14:39:16 +05301154 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1155
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -07001156 /* common policy */
1157 dpolicy->type = discard_type;
1158 dpolicy->sync = true;
Chao Yu20ee4382018-07-08 22:11:01 +08001159 dpolicy->ordered = false;
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -07001160 dpolicy->granularity = granularity;
1161
1162 dpolicy->max_requests = DEF_MAX_DISCARD_REQUEST;
1163 dpolicy->io_aware_gran = MAX_PLIST_NUM;
Chao Yu6ce48b02020-03-26 17:43:56 +08001164 dpolicy->timeout = false;
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -07001165
1166 if (discard_type == DPOLICY_BG) {
1167 dpolicy->min_interval = DEF_MIN_DISCARD_ISSUE_TIME;
Yunlei Hef9d1dce2018-04-08 15:11:11 +08001168 dpolicy->mid_interval = DEF_MID_DISCARD_ISSUE_TIME;
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -07001169 dpolicy->max_interval = DEF_MAX_DISCARD_ISSUE_TIME;
1170 dpolicy->io_aware = true;
Chao Yucba60842018-04-10 15:43:09 +08001171 dpolicy->sync = false;
Chao Yu20ee4382018-07-08 22:11:01 +08001172 dpolicy->ordered = true;
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -07001173 if (utilization(sbi) > DEF_DISCARD_URGENT_UTIL) {
1174 dpolicy->granularity = 1;
Sahitya Tummalac35b8d52021-04-06 14:39:16 +05301175 if (atomic_read(&dcc->discard_cmd_cnt))
1176 dpolicy->max_interval =
1177 DEF_MIN_DISCARD_ISSUE_TIME;
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -07001178 }
1179 } else if (discard_type == DPOLICY_FORCE) {
1180 dpolicy->min_interval = DEF_MIN_DISCARD_ISSUE_TIME;
Yunlei Hef9d1dce2018-04-08 15:11:11 +08001181 dpolicy->mid_interval = DEF_MID_DISCARD_ISSUE_TIME;
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -07001182 dpolicy->max_interval = DEF_MAX_DISCARD_ISSUE_TIME;
1183 dpolicy->io_aware = false;
1184 } else if (discard_type == DPOLICY_FSTRIM) {
1185 dpolicy->io_aware = false;
1186 } else if (discard_type == DPOLICY_UMOUNT) {
1187 dpolicy->io_aware = false;
Jaegeuk Kimb8623252019-01-25 09:12:13 -08001188 /* we need to issue all to keep CP_TRIMMED_FLAG */
1189 dpolicy->granularity = 1;
Chao Yu6ce48b02020-03-26 17:43:56 +08001190 dpolicy->timeout = true;
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -07001191 }
1192}
1193
Chao Yu35ec7d52018-08-06 22:43:50 +08001194static void __update_discard_tree_range(struct f2fs_sb_info *sbi,
1195 struct block_device *bdev, block_t lstart,
1196 block_t start, block_t len);
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001197/* this function is copied from blkdev_issue_discard from block/blk-lib.c */
Chao Yu6b9cb122018-08-08 10:14:55 +08001198static int __submit_discard_cmd(struct f2fs_sb_info *sbi,
Chao Yu78997b52017-10-04 09:08:34 +08001199 struct discard_policy *dpolicy,
Chao Yu35ec7d52018-08-06 22:43:50 +08001200 struct discard_cmd *dc,
1201 unsigned int *issued)
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001202{
Chao Yu35ec7d52018-08-06 22:43:50 +08001203 struct block_device *bdev = dc->bdev;
1204 struct request_queue *q = bdev_get_queue(bdev);
1205 unsigned int max_discard_blocks =
1206 SECTOR_TO_BLOCK(q->limits.max_discard_sectors);
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001207 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Chao Yu78997b52017-10-04 09:08:34 +08001208 struct list_head *wait_list = (dpolicy->type == DPOLICY_FSTRIM) ?
1209 &(dcc->fstrim_list) : &(dcc->wait_list);
Chao Yu78997b52017-10-04 09:08:34 +08001210 int flag = dpolicy->sync ? REQ_SYNC : 0;
Chao Yu35ec7d52018-08-06 22:43:50 +08001211 block_t lstart, start, len, total_len;
1212 int err = 0;
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001213
1214 if (dc->state != D_PREP)
Chao Yu6b9cb122018-08-08 10:14:55 +08001215 return 0;
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001216
Yunlei Hed6184772018-04-13 11:08:05 +08001217 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK))
Chao Yu6b9cb122018-08-08 10:14:55 +08001218 return 0;
Yunlei Hed6184772018-04-13 11:08:05 +08001219
Chao Yu35ec7d52018-08-06 22:43:50 +08001220 trace_f2fs_issue_discard(bdev, dc->start, dc->len);
Chao Yu0243a5f2017-04-15 14:09:38 +08001221
Chao Yu35ec7d52018-08-06 22:43:50 +08001222 lstart = dc->lstart;
1223 start = dc->start;
1224 len = dc->len;
1225 total_len = len;
1226
1227 dc->len = 0;
1228
1229 while (total_len && *issued < dpolicy->max_requests && !err) {
1230 struct bio *bio = NULL;
1231 unsigned long flags;
1232 bool last = true;
1233
1234 if (len > max_discard_blocks) {
1235 len = max_discard_blocks;
1236 last = false;
1237 }
1238
1239 (*issued)++;
1240 if (*issued == dpolicy->max_requests)
1241 last = true;
1242
1243 dc->len += len;
1244
Chao Yub83dcfe2018-08-06 20:30:18 +08001245 if (time_to_inject(sbi, FAULT_DISCARD)) {
Chao Yuc45d6002019-11-01 17:53:23 +08001246 f2fs_show_injection_info(sbi, FAULT_DISCARD);
Chao Yub83dcfe2018-08-06 20:30:18 +08001247 err = -EIO;
1248 goto submit;
1249 }
Chao Yu35ec7d52018-08-06 22:43:50 +08001250 err = __blkdev_issue_discard(bdev,
1251 SECTOR_FROM_BLOCK(start),
1252 SECTOR_FROM_BLOCK(len),
1253 GFP_NOFS, 0, &bio);
Chao Yub83dcfe2018-08-06 20:30:18 +08001254submit:
Chao Yu6b9cb122018-08-08 10:14:55 +08001255 if (err) {
Chao Yu35ec7d52018-08-06 22:43:50 +08001256 spin_lock_irqsave(&dc->lock, flags);
1257 if (dc->state == D_PARTIAL)
1258 dc->state = D_SUBMIT;
1259 spin_unlock_irqrestore(&dc->lock, flags);
1260
Chao Yu6b9cb122018-08-08 10:14:55 +08001261 break;
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001262 }
Chao Yu35ec7d52018-08-06 22:43:50 +08001263
Chao Yu6b9cb122018-08-08 10:14:55 +08001264 f2fs_bug_on(sbi, !bio);
1265
1266 /*
1267 * should keep before submission to avoid D_DONE
1268 * right away
1269 */
1270 spin_lock_irqsave(&dc->lock, flags);
1271 if (last)
1272 dc->state = D_SUBMIT;
1273 else
1274 dc->state = D_PARTIAL;
1275 dc->bio_ref++;
1276 spin_unlock_irqrestore(&dc->lock, flags);
1277
Jaegeuk Kim72691af2018-12-13 16:53:57 -08001278 atomic_inc(&dcc->queued_discard);
1279 dc->queued++;
Chao Yu6b9cb122018-08-08 10:14:55 +08001280 list_move_tail(&dc->list, wait_list);
1281
1282 /* sanity check on discard range */
Qiuyang Sun9249dde2018-12-18 17:32:23 +08001283 __check_sit_bitmap(sbi, lstart, lstart + len);
Chao Yu6b9cb122018-08-08 10:14:55 +08001284
1285 bio->bi_private = dc;
1286 bio->bi_end_io = f2fs_submit_discard_endio;
1287 bio->bi_opf |= flag;
1288 submit_bio(bio);
1289
1290 atomic_inc(&dcc->issued_discard);
1291
1292 f2fs_update_iostat(sbi, FS_DISCARD, 1);
1293
Chao Yu35ec7d52018-08-06 22:43:50 +08001294 lstart += len;
1295 start += len;
1296 total_len -= len;
1297 len = total_len;
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001298 }
Chao Yu35ec7d52018-08-06 22:43:50 +08001299
Sahitya Tummaladf423392020-04-16 11:47:41 +05301300 if (!err && len) {
1301 dcc->undiscard_blks -= len;
Chao Yu35ec7d52018-08-06 22:43:50 +08001302 __update_discard_tree_range(sbi, bdev, lstart, start, len);
Sahitya Tummaladf423392020-04-16 11:47:41 +05301303 }
Chao Yu6b9cb122018-08-08 10:14:55 +08001304 return err;
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001305}
1306
Chao Yu47d0d7d72020-05-27 18:27:52 +08001307static void __insert_discard_tree(struct f2fs_sb_info *sbi,
Chao Yu004b6862017-04-14 23:24:55 +08001308 struct block_device *bdev, block_t lstart,
1309 block_t start, block_t len,
1310 struct rb_node **insert_p,
1311 struct rb_node *insert_parent)
1312{
1313 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Colin Ian Kingdca69512017-10-19 12:58:21 +02001314 struct rb_node **p;
Chao Yu004b6862017-04-14 23:24:55 +08001315 struct rb_node *parent = NULL;
Chao Yu4dada3f2018-10-04 11:18:30 +08001316 bool leftmost = true;
Chao Yu004b6862017-04-14 23:24:55 +08001317
1318 if (insert_p && insert_parent) {
1319 parent = insert_parent;
1320 p = insert_p;
1321 goto do_insert;
1322 }
1323
Chao Yu4dada3f2018-10-04 11:18:30 +08001324 p = f2fs_lookup_rb_tree_for_insert(sbi, &dcc->root, &parent,
1325 lstart, &leftmost);
Chao Yu004b6862017-04-14 23:24:55 +08001326do_insert:
Chao Yu47d0d7d72020-05-27 18:27:52 +08001327 __attach_discard_cmd(sbi, bdev, lstart, start, len, parent,
Chao Yu4dada3f2018-10-04 11:18:30 +08001328 p, leftmost);
Chao Yu004b6862017-04-14 23:24:55 +08001329}
1330
Chao Yuba48a332017-04-15 14:09:37 +08001331static void __relocate_discard_cmd(struct discard_cmd_control *dcc,
1332 struct discard_cmd *dc)
1333{
1334 list_move_tail(&dc->list, &dcc->pend_list[plist_idx(dc->len)]);
1335}
1336
Chao Yu004b6862017-04-14 23:24:55 +08001337static void __punch_discard_cmd(struct f2fs_sb_info *sbi,
1338 struct discard_cmd *dc, block_t blkaddr)
1339{
Chao Yuba48a332017-04-15 14:09:37 +08001340 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Chao Yu004b6862017-04-14 23:24:55 +08001341 struct discard_info di = dc->di;
1342 bool modified = false;
1343
1344 if (dc->state == D_DONE || dc->len == 1) {
1345 __remove_discard_cmd(sbi, dc);
1346 return;
1347 }
1348
Chao Yud84d1cb2017-04-18 19:27:39 +08001349 dcc->undiscard_blks -= di.len;
1350
Chao Yu004b6862017-04-14 23:24:55 +08001351 if (blkaddr > di.lstart) {
1352 dc->len = blkaddr - dc->lstart;
Chao Yud84d1cb2017-04-18 19:27:39 +08001353 dcc->undiscard_blks += dc->len;
Chao Yuba48a332017-04-15 14:09:37 +08001354 __relocate_discard_cmd(dcc, dc);
Chao Yu004b6862017-04-14 23:24:55 +08001355 modified = true;
1356 }
1357
1358 if (blkaddr < di.lstart + di.len - 1) {
1359 if (modified) {
1360 __insert_discard_tree(sbi, dc->bdev, blkaddr + 1,
1361 di.start + blkaddr + 1 - di.lstart,
1362 di.lstart + di.len - 1 - blkaddr,
1363 NULL, NULL);
1364 } else {
1365 dc->lstart++;
1366 dc->len--;
1367 dc->start++;
Chao Yud84d1cb2017-04-18 19:27:39 +08001368 dcc->undiscard_blks += dc->len;
Chao Yuba48a332017-04-15 14:09:37 +08001369 __relocate_discard_cmd(dcc, dc);
Chao Yu004b6862017-04-14 23:24:55 +08001370 }
1371 }
1372}
1373
1374static void __update_discard_tree_range(struct f2fs_sb_info *sbi,
1375 struct block_device *bdev, block_t lstart,
1376 block_t start, block_t len)
1377{
1378 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1379 struct discard_cmd *prev_dc = NULL, *next_dc = NULL;
1380 struct discard_cmd *dc;
1381 struct discard_info di = {0};
1382 struct rb_node **insert_p = NULL, *insert_parent = NULL;
Chao Yu35ec7d52018-08-06 22:43:50 +08001383 struct request_queue *q = bdev_get_queue(bdev);
1384 unsigned int max_discard_blocks =
1385 SECTOR_TO_BLOCK(q->limits.max_discard_sectors);
Chao Yu004b6862017-04-14 23:24:55 +08001386 block_t end = lstart + len;
1387
Chao Yu4d57b862018-05-30 00:20:41 +08001388 dc = (struct discard_cmd *)f2fs_lookup_rb_tree_ret(&dcc->root,
Chao Yu004b6862017-04-14 23:24:55 +08001389 NULL, lstart,
1390 (struct rb_entry **)&prev_dc,
1391 (struct rb_entry **)&next_dc,
Chao Yu4dada3f2018-10-04 11:18:30 +08001392 &insert_p, &insert_parent, true, NULL);
Chao Yu004b6862017-04-14 23:24:55 +08001393 if (dc)
1394 prev_dc = dc;
1395
1396 if (!prev_dc) {
1397 di.lstart = lstart;
1398 di.len = next_dc ? next_dc->lstart - lstart : len;
1399 di.len = min(di.len, len);
1400 di.start = start;
1401 }
1402
1403 while (1) {
1404 struct rb_node *node;
1405 bool merged = false;
1406 struct discard_cmd *tdc = NULL;
1407
1408 if (prev_dc) {
1409 di.lstart = prev_dc->lstart + prev_dc->len;
1410 if (di.lstart < lstart)
1411 di.lstart = lstart;
1412 if (di.lstart >= end)
1413 break;
1414
1415 if (!next_dc || next_dc->lstart > end)
1416 di.len = end - di.lstart;
1417 else
1418 di.len = next_dc->lstart - di.lstart;
1419 di.start = start + di.lstart - lstart;
1420 }
1421
1422 if (!di.len)
1423 goto next;
1424
1425 if (prev_dc && prev_dc->state == D_PREP &&
1426 prev_dc->bdev == bdev &&
Chao Yu35ec7d52018-08-06 22:43:50 +08001427 __is_discard_back_mergeable(&di, &prev_dc->di,
1428 max_discard_blocks)) {
Chao Yu004b6862017-04-14 23:24:55 +08001429 prev_dc->di.len += di.len;
Chao Yud84d1cb2017-04-18 19:27:39 +08001430 dcc->undiscard_blks += di.len;
Chao Yuba48a332017-04-15 14:09:37 +08001431 __relocate_discard_cmd(dcc, prev_dc);
Chao Yu004b6862017-04-14 23:24:55 +08001432 di = prev_dc->di;
1433 tdc = prev_dc;
1434 merged = true;
1435 }
1436
1437 if (next_dc && next_dc->state == D_PREP &&
1438 next_dc->bdev == bdev &&
Chao Yu35ec7d52018-08-06 22:43:50 +08001439 __is_discard_front_mergeable(&di, &next_dc->di,
1440 max_discard_blocks)) {
Chao Yu004b6862017-04-14 23:24:55 +08001441 next_dc->di.lstart = di.lstart;
1442 next_dc->di.len += di.len;
1443 next_dc->di.start = di.start;
Chao Yud84d1cb2017-04-18 19:27:39 +08001444 dcc->undiscard_blks += di.len;
Chao Yuba48a332017-04-15 14:09:37 +08001445 __relocate_discard_cmd(dcc, next_dc);
Chao Yu004b6862017-04-14 23:24:55 +08001446 if (tdc)
1447 __remove_discard_cmd(sbi, tdc);
Chao Yu004b6862017-04-14 23:24:55 +08001448 merged = true;
1449 }
1450
Chao Yudf0f6b42017-04-17 18:21:43 +08001451 if (!merged) {
Chao Yu004b6862017-04-14 23:24:55 +08001452 __insert_discard_tree(sbi, bdev, di.lstart, di.start,
1453 di.len, NULL, NULL);
Chao Yudf0f6b42017-04-17 18:21:43 +08001454 }
Chao Yu004b6862017-04-14 23:24:55 +08001455 next:
1456 prev_dc = next_dc;
1457 if (!prev_dc)
1458 break;
1459
1460 node = rb_next(&prev_dc->rb_node);
1461 next_dc = rb_entry_safe(node, struct discard_cmd, rb_node);
1462 }
Chao Yu004b6862017-04-14 23:24:55 +08001463}
1464
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001465static int __queue_discard_cmd(struct f2fs_sb_info *sbi,
1466 struct block_device *bdev, block_t blkstart, block_t blklen)
1467{
1468 block_t lblkstart = blkstart;
1469
Damien Le Moal7f3d7712019-03-16 09:13:08 +09001470 if (!f2fs_bdev_support_discard(bdev))
1471 return 0;
1472
Chao Yu0243a5f2017-04-15 14:09:38 +08001473 trace_f2fs_queue_discard(bdev, blkstart, blklen);
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001474
Damien Le Moal09168782019-03-16 09:13:06 +09001475 if (f2fs_is_multi_device(sbi)) {
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001476 int devi = f2fs_target_device_index(sbi, blkstart);
1477
1478 blkstart -= FDEV(devi).start_blk;
1479 }
Chao Yu35ec7d52018-08-06 22:43:50 +08001480 mutex_lock(&SM_I(sbi)->dcc_info->cmd_lock);
Chao Yu004b6862017-04-14 23:24:55 +08001481 __update_discard_tree_range(sbi, bdev, lblkstart, blkstart, blklen);
Chao Yu35ec7d52018-08-06 22:43:50 +08001482 mutex_unlock(&SM_I(sbi)->dcc_info->cmd_lock);
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001483 return 0;
1484}
1485
Chao Yu20ee4382018-07-08 22:11:01 +08001486static unsigned int __issue_discard_cmd_orderly(struct f2fs_sb_info *sbi,
1487 struct discard_policy *dpolicy)
1488{
1489 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1490 struct discard_cmd *prev_dc = NULL, *next_dc = NULL;
1491 struct rb_node **insert_p = NULL, *insert_parent = NULL;
1492 struct discard_cmd *dc;
1493 struct blk_plug plug;
1494 unsigned int pos = dcc->next_pos;
1495 unsigned int issued = 0;
1496 bool io_interrupted = false;
1497
1498 mutex_lock(&dcc->cmd_lock);
1499 dc = (struct discard_cmd *)f2fs_lookup_rb_tree_ret(&dcc->root,
1500 NULL, pos,
1501 (struct rb_entry **)&prev_dc,
1502 (struct rb_entry **)&next_dc,
Chao Yu4dada3f2018-10-04 11:18:30 +08001503 &insert_p, &insert_parent, true, NULL);
Chao Yu20ee4382018-07-08 22:11:01 +08001504 if (!dc)
1505 dc = next_dc;
1506
1507 blk_start_plug(&plug);
1508
1509 while (dc) {
1510 struct rb_node *node;
Chao Yu6b9cb122018-08-08 10:14:55 +08001511 int err = 0;
Chao Yu20ee4382018-07-08 22:11:01 +08001512
1513 if (dc->state != D_PREP)
1514 goto next;
1515
Sahitya Tummalaa7d10cf2018-09-19 14:18:47 +05301516 if (dpolicy->io_aware && !is_idle(sbi, DISCARD_TIME)) {
Chao Yu20ee4382018-07-08 22:11:01 +08001517 io_interrupted = true;
1518 break;
1519 }
1520
1521 dcc->next_pos = dc->lstart + dc->len;
Chao Yu6b9cb122018-08-08 10:14:55 +08001522 err = __submit_discard_cmd(sbi, dpolicy, dc, &issued);
Chao Yu20ee4382018-07-08 22:11:01 +08001523
Chao Yu35ec7d52018-08-06 22:43:50 +08001524 if (issued >= dpolicy->max_requests)
Chao Yu20ee4382018-07-08 22:11:01 +08001525 break;
1526next:
1527 node = rb_next(&dc->rb_node);
Chao Yu6b9cb122018-08-08 10:14:55 +08001528 if (err)
1529 __remove_discard_cmd(sbi, dc);
Chao Yu20ee4382018-07-08 22:11:01 +08001530 dc = rb_entry_safe(node, struct discard_cmd, rb_node);
1531 }
1532
1533 blk_finish_plug(&plug);
1534
1535 if (!dc)
1536 dcc->next_pos = 0;
1537
1538 mutex_unlock(&dcc->cmd_lock);
1539
1540 if (!issued && io_interrupted)
1541 issued = -1;
1542
1543 return issued;
1544}
Sahitya Tummala141af6b2020-04-15 14:37:53 +05301545static unsigned int __wait_all_discard_cmd(struct f2fs_sb_info *sbi,
1546 struct discard_policy *dpolicy);
Chao Yu20ee4382018-07-08 22:11:01 +08001547
Chao Yu78997b52017-10-04 09:08:34 +08001548static int __issue_discard_cmd(struct f2fs_sb_info *sbi,
1549 struct discard_policy *dpolicy)
Chao Yubd5b0732017-04-25 20:21:37 +08001550{
1551 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1552 struct list_head *pend_list;
1553 struct discard_cmd *dc, *tmp;
1554 struct blk_plug plug;
Sahitya Tummala141af6b2020-04-15 14:37:53 +05301555 int i, issued;
Chao Yue6c6de12017-09-12 21:35:12 +08001556 bool io_interrupted = false;
Chao Yubd5b0732017-04-25 20:21:37 +08001557
Chao Yu6ce48b02020-03-26 17:43:56 +08001558 if (dpolicy->timeout)
1559 f2fs_update_time(sbi, UMOUNT_DISCARD_TIMEOUT);
Jaegeuk Kim03f2c022019-01-14 10:42:11 -08001560
Sahitya Tummala141af6b2020-04-15 14:37:53 +05301561retry:
1562 issued = 0;
Chao Yu78997b52017-10-04 09:08:34 +08001563 for (i = MAX_PLIST_NUM - 1; i >= 0; i--) {
Chao Yu6ce48b02020-03-26 17:43:56 +08001564 if (dpolicy->timeout &&
1565 f2fs_time_over(sbi, UMOUNT_DISCARD_TIMEOUT))
Jaegeuk Kim03f2c022019-01-14 10:42:11 -08001566 break;
1567
Chao Yu78997b52017-10-04 09:08:34 +08001568 if (i + 1 < dpolicy->granularity)
1569 break;
Chao Yu20ee4382018-07-08 22:11:01 +08001570
1571 if (i < DEFAULT_DISCARD_GRANULARITY && dpolicy->ordered)
1572 return __issue_discard_cmd_orderly(sbi, dpolicy);
1573
Chao Yubd5b0732017-04-25 20:21:37 +08001574 pend_list = &dcc->pend_list[i];
Chao Yu33da62c2017-10-04 09:08:35 +08001575
1576 mutex_lock(&dcc->cmd_lock);
Chao Yu49c60c62018-01-08 18:48:33 +08001577 if (list_empty(pend_list))
1578 goto next;
Chao Yu67fce702018-06-22 16:06:59 +08001579 if (unlikely(dcc->rbtree_check))
1580 f2fs_bug_on(sbi, !f2fs_check_rb_tree_consistence(sbi,
Chao Yu2e9b2bb2020-08-04 21:14:48 +08001581 &dcc->root, false));
Chao Yu33da62c2017-10-04 09:08:35 +08001582 blk_start_plug(&plug);
Chao Yubd5b0732017-04-25 20:21:37 +08001583 list_for_each_entry_safe(dc, tmp, pend_list, list) {
1584 f2fs_bug_on(sbi, dc->state != D_PREP);
1585
Chao Yu6ce48b02020-03-26 17:43:56 +08001586 if (dpolicy->timeout &&
1587 f2fs_time_over(sbi, UMOUNT_DISCARD_TIMEOUT))
Heng Xiao6e0cd4a2019-07-03 10:29:57 +08001588 break;
1589
Chao Yuecc9aa02017-10-04 09:08:33 +08001590 if (dpolicy->io_aware && i < dpolicy->io_aware_gran &&
Sahitya Tummalaa7d10cf2018-09-19 14:18:47 +05301591 !is_idle(sbi, DISCARD_TIME)) {
Chao Yue6c6de12017-09-12 21:35:12 +08001592 io_interrupted = true;
Chao Yu522d1712018-07-08 22:08:09 +08001593 break;
Chao Yue6c6de12017-09-12 21:35:12 +08001594 }
1595
Chao Yu35ec7d52018-08-06 22:43:50 +08001596 __submit_discard_cmd(sbi, dpolicy, dc, &issued);
Chao Yu522d1712018-07-08 22:08:09 +08001597
Chao Yu35ec7d52018-08-06 22:43:50 +08001598 if (issued >= dpolicy->max_requests)
Chao Yu33da62c2017-10-04 09:08:35 +08001599 break;
Chao Yubd5b0732017-04-25 20:21:37 +08001600 }
Chao Yu33da62c2017-10-04 09:08:35 +08001601 blk_finish_plug(&plug);
Chao Yu49c60c62018-01-08 18:48:33 +08001602next:
Chao Yu33da62c2017-10-04 09:08:35 +08001603 mutex_unlock(&dcc->cmd_lock);
1604
Chao Yu522d1712018-07-08 22:08:09 +08001605 if (issued >= dpolicy->max_requests || io_interrupted)
Chao Yu33da62c2017-10-04 09:08:35 +08001606 break;
Chao Yubd5b0732017-04-25 20:21:37 +08001607 }
Chao Yu969d1b12017-08-07 23:09:56 +08001608
Sahitya Tummala141af6b2020-04-15 14:37:53 +05301609 if (dpolicy->type == DPOLICY_UMOUNT && issued) {
1610 __wait_all_discard_cmd(sbi, dpolicy);
1611 goto retry;
1612 }
1613
Chao Yue6c6de12017-09-12 21:35:12 +08001614 if (!issued && io_interrupted)
1615 issued = -1;
1616
Chao Yu969d1b12017-08-07 23:09:56 +08001617 return issued;
1618}
1619
Chao Yucf5c7592017-10-04 09:08:37 +08001620static bool __drop_discard_cmd(struct f2fs_sb_info *sbi)
Chao Yu969d1b12017-08-07 23:09:56 +08001621{
1622 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1623 struct list_head *pend_list;
1624 struct discard_cmd *dc, *tmp;
1625 int i;
Chao Yucf5c7592017-10-04 09:08:37 +08001626 bool dropped = false;
Chao Yu969d1b12017-08-07 23:09:56 +08001627
1628 mutex_lock(&dcc->cmd_lock);
1629 for (i = MAX_PLIST_NUM - 1; i >= 0; i--) {
1630 pend_list = &dcc->pend_list[i];
1631 list_for_each_entry_safe(dc, tmp, pend_list, list) {
1632 f2fs_bug_on(sbi, dc->state != D_PREP);
1633 __remove_discard_cmd(sbi, dc);
Chao Yucf5c7592017-10-04 09:08:37 +08001634 dropped = true;
Chao Yu969d1b12017-08-07 23:09:56 +08001635 }
1636 }
1637 mutex_unlock(&dcc->cmd_lock);
Chao Yucf5c7592017-10-04 09:08:37 +08001638
1639 return dropped;
Chao Yubd5b0732017-04-25 20:21:37 +08001640}
1641
Chao Yu4d57b862018-05-30 00:20:41 +08001642void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi)
Chao Yu7950e9a2018-01-18 17:23:29 +08001643{
1644 __drop_discard_cmd(sbi);
1645}
1646
Chao Yu0ea80512017-10-28 16:52:32 +08001647static unsigned int __wait_one_discard_bio(struct f2fs_sb_info *sbi,
Chao Yu2a510c002017-06-05 18:29:06 +08001648 struct discard_cmd *dc)
1649{
1650 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Chao Yu0ea80512017-10-28 16:52:32 +08001651 unsigned int len = 0;
Chao Yu2a510c002017-06-05 18:29:06 +08001652
1653 wait_for_completion_io(&dc->wait);
1654 mutex_lock(&dcc->cmd_lock);
1655 f2fs_bug_on(sbi, dc->state != D_DONE);
1656 dc->ref--;
Chao Yu0ea80512017-10-28 16:52:32 +08001657 if (!dc->ref) {
1658 if (!dc->error)
1659 len = dc->len;
Chao Yu2a510c002017-06-05 18:29:06 +08001660 __remove_discard_cmd(sbi, dc);
Chao Yu0ea80512017-10-28 16:52:32 +08001661 }
Chao Yu2a510c002017-06-05 18:29:06 +08001662 mutex_unlock(&dcc->cmd_lock);
Chao Yu0ea80512017-10-28 16:52:32 +08001663
1664 return len;
Chao Yu2a510c002017-06-05 18:29:06 +08001665}
1666
Chao Yu0ea80512017-10-28 16:52:32 +08001667static unsigned int __wait_discard_cmd_range(struct f2fs_sb_info *sbi,
Chao Yu78997b52017-10-04 09:08:34 +08001668 struct discard_policy *dpolicy,
1669 block_t start, block_t end)
Chao Yu63a94fa2017-04-25 20:21:38 +08001670{
1671 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Chao Yu78997b52017-10-04 09:08:34 +08001672 struct list_head *wait_list = (dpolicy->type == DPOLICY_FSTRIM) ?
1673 &(dcc->fstrim_list) : &(dcc->wait_list);
Chao Yu63a94fa2017-04-25 20:21:38 +08001674 struct discard_cmd *dc, *tmp;
Chao Yu6afae632017-05-19 23:46:45 +08001675 bool need_wait;
Chao Yu0ea80512017-10-28 16:52:32 +08001676 unsigned int trimmed = 0;
Chao Yu6afae632017-05-19 23:46:45 +08001677
1678next:
1679 need_wait = false;
Chao Yu63a94fa2017-04-25 20:21:38 +08001680
1681 mutex_lock(&dcc->cmd_lock);
1682 list_for_each_entry_safe(dc, tmp, wait_list, list) {
Chao Yu84126632017-10-04 09:08:32 +08001683 if (dc->lstart + dc->len <= start || end <= dc->lstart)
1684 continue;
Chao Yu78997b52017-10-04 09:08:34 +08001685 if (dc->len < dpolicy->granularity)
Chao Yu84126632017-10-04 09:08:32 +08001686 continue;
Chao Yu78997b52017-10-04 09:08:34 +08001687 if (dc->state == D_DONE && !dc->ref) {
Chao Yu63a94fa2017-04-25 20:21:38 +08001688 wait_for_completion_io(&dc->wait);
Chao Yu0ea80512017-10-28 16:52:32 +08001689 if (!dc->error)
1690 trimmed += dc->len;
Chao Yu63a94fa2017-04-25 20:21:38 +08001691 __remove_discard_cmd(sbi, dc);
Chao Yu6afae632017-05-19 23:46:45 +08001692 } else {
1693 dc->ref++;
1694 need_wait = true;
1695 break;
Chao Yu63a94fa2017-04-25 20:21:38 +08001696 }
1697 }
1698 mutex_unlock(&dcc->cmd_lock);
Chao Yu6afae632017-05-19 23:46:45 +08001699
1700 if (need_wait) {
Chao Yu0ea80512017-10-28 16:52:32 +08001701 trimmed += __wait_one_discard_bio(sbi, dc);
Chao Yu6afae632017-05-19 23:46:45 +08001702 goto next;
1703 }
Chao Yu0ea80512017-10-28 16:52:32 +08001704
1705 return trimmed;
Chao Yu63a94fa2017-04-25 20:21:38 +08001706}
1707
Chao Yu01f9cf62018-06-25 20:33:24 +08001708static unsigned int __wait_all_discard_cmd(struct f2fs_sb_info *sbi,
Chao Yu78997b52017-10-04 09:08:34 +08001709 struct discard_policy *dpolicy)
Chao Yu84126632017-10-04 09:08:32 +08001710{
Jaegeuk Kim9a997182018-05-24 13:57:26 -07001711 struct discard_policy dp;
Chao Yu01f9cf62018-06-25 20:33:24 +08001712 unsigned int discard_blks;
Jaegeuk Kim9a997182018-05-24 13:57:26 -07001713
Chao Yu01f9cf62018-06-25 20:33:24 +08001714 if (dpolicy)
1715 return __wait_discard_cmd_range(sbi, dpolicy, 0, UINT_MAX);
Jaegeuk Kim9a997182018-05-24 13:57:26 -07001716
1717 /* wait all */
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -07001718 __init_discard_policy(sbi, &dp, DPOLICY_FSTRIM, 1);
Chao Yu01f9cf62018-06-25 20:33:24 +08001719 discard_blks = __wait_discard_cmd_range(sbi, &dp, 0, UINT_MAX);
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -07001720 __init_discard_policy(sbi, &dp, DPOLICY_UMOUNT, 1);
Chao Yu01f9cf62018-06-25 20:33:24 +08001721 discard_blks += __wait_discard_cmd_range(sbi, &dp, 0, UINT_MAX);
1722
1723 return discard_blks;
Chao Yu84126632017-10-04 09:08:32 +08001724}
1725
Jaegeuk Kim4e6a8d92016-12-29 14:07:53 -08001726/* This should be covered by global mutex, &sit_i->sentry_lock */
Wei Yongjun94b1e102018-01-05 09:41:20 +00001727static void f2fs_wait_discard_bio(struct f2fs_sb_info *sbi, block_t blkaddr)
Chao Yu275b66b2016-08-29 23:58:34 +08001728{
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001729 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Chao Yu004b6862017-04-14 23:24:55 +08001730 struct discard_cmd *dc;
Chao Yuec9895a2017-04-26 17:39:54 +08001731 bool need_wait = false;
Chao Yu275b66b2016-08-29 23:58:34 +08001732
Jaegeuk Kim15469962017-01-09 20:32:07 -08001733 mutex_lock(&dcc->cmd_lock);
Chao Yu4d57b862018-05-30 00:20:41 +08001734 dc = (struct discard_cmd *)f2fs_lookup_rb_tree(&dcc->root,
1735 NULL, blkaddr);
Chao Yu004b6862017-04-14 23:24:55 +08001736 if (dc) {
Chao Yuec9895a2017-04-26 17:39:54 +08001737 if (dc->state == D_PREP) {
1738 __punch_discard_cmd(sbi, dc, blkaddr);
1739 } else {
1740 dc->ref++;
1741 need_wait = true;
1742 }
Chao Yu275b66b2016-08-29 23:58:34 +08001743 }
Chao Yud4314132017-04-05 18:19:49 +08001744 mutex_unlock(&dcc->cmd_lock);
Chao Yuec9895a2017-04-26 17:39:54 +08001745
Chao Yu2a510c002017-06-05 18:29:06 +08001746 if (need_wait)
1747 __wait_one_discard_bio(sbi, dc);
Chao Yud4314132017-04-05 18:19:49 +08001748}
Chao Yu22d375d2017-04-05 18:19:48 +08001749
Chao Yu4d57b862018-05-30 00:20:41 +08001750void f2fs_stop_discard_thread(struct f2fs_sb_info *sbi)
Chao Yucce13252017-06-29 23:17:45 +08001751{
1752 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1753
1754 if (dcc && dcc->f2fs_issue_discard) {
1755 struct task_struct *discard_thread = dcc->f2fs_issue_discard;
1756
1757 dcc->f2fs_issue_discard = NULL;
1758 kthread_stop(discard_thread);
Jaegeuk Kim15469962017-01-09 20:32:07 -08001759 }
1760}
1761
Chao Yu84126632017-10-04 09:08:32 +08001762/* This comes from f2fs_put_super */
Jaegeuk Kim03f2c022019-01-14 10:42:11 -08001763bool f2fs_issue_discard_timeout(struct f2fs_sb_info *sbi)
Chao Yu275b66b2016-08-29 23:58:34 +08001764{
Chao Yu969d1b12017-08-07 23:09:56 +08001765 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Chao Yu78997b52017-10-04 09:08:34 +08001766 struct discard_policy dpolicy;
Chao Yucf5c7592017-10-04 09:08:37 +08001767 bool dropped;
Chao Yu969d1b12017-08-07 23:09:56 +08001768
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -07001769 __init_discard_policy(sbi, &dpolicy, DPOLICY_UMOUNT,
1770 dcc->discard_granularity);
Chao Yu78997b52017-10-04 09:08:34 +08001771 __issue_discard_cmd(sbi, &dpolicy);
Chao Yucf5c7592017-10-04 09:08:37 +08001772 dropped = __drop_discard_cmd(sbi);
Chao Yucf5c7592017-10-04 09:08:37 +08001773
Jaegeuk Kim9a997182018-05-24 13:57:26 -07001774 /* just to make sure there is no pending discard commands */
1775 __wait_all_discard_cmd(sbi, NULL);
Chao Yu2482c432018-07-08 22:16:53 +08001776
1777 f2fs_bug_on(sbi, atomic_read(&dcc->discard_cmd_cnt));
Chao Yucf5c7592017-10-04 09:08:37 +08001778 return dropped;
Chao Yu969d1b12017-08-07 23:09:56 +08001779}
1780
Jaegeuk Kim15469962017-01-09 20:32:07 -08001781static int issue_discard_thread(void *data)
1782{
1783 struct f2fs_sb_info *sbi = data;
1784 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1785 wait_queue_head_t *q = &dcc->discard_wait_queue;
Chao Yu78997b52017-10-04 09:08:34 +08001786 struct discard_policy dpolicy;
Chao Yu969d1b12017-08-07 23:09:56 +08001787 unsigned int wait_ms = DEF_MIN_DISCARD_ISSUE_TIME;
1788 int issued;
Jaegeuk Kim15469962017-01-09 20:32:07 -08001789
Jaegeuk Kim1d7be272017-05-17 10:36:58 -07001790 set_freezable();
Jaegeuk Kim15469962017-01-09 20:32:07 -08001791
Jaegeuk Kim1d7be272017-05-17 10:36:58 -07001792 do {
Sahitya Tummalac35b8d52021-04-06 14:39:16 +05301793 if (sbi->gc_mode == GC_URGENT_HIGH ||
1794 !f2fs_available_free_memory(sbi, DISCARD_CACHE))
1795 __init_discard_policy(sbi, &dpolicy, DPOLICY_FORCE, 1);
1796 else
1797 __init_discard_policy(sbi, &dpolicy, DPOLICY_BG,
1798 dcc->discard_granularity);
1799
1800 if (!atomic_read(&dcc->discard_cmd_cnt))
1801 wait_ms = dpolicy.max_interval;
Chao Yu78997b52017-10-04 09:08:34 +08001802
Chao Yu969d1b12017-08-07 23:09:56 +08001803 wait_event_interruptible_timeout(*q,
1804 kthread_should_stop() || freezing(current) ||
1805 dcc->discard_wake,
1806 msecs_to_jiffies(wait_ms));
Sheng Yong35a9a762018-05-08 17:51:34 +08001807
1808 if (dcc->discard_wake)
1809 dcc->discard_wake = 0;
1810
Jaegeuk Kim76c7bfb2018-12-13 20:50:51 -08001811 /* clean up pending candidates before going to sleep */
1812 if (atomic_read(&dcc->queued_discard))
1813 __wait_all_discard_cmd(sbi, NULL);
1814
Jaegeuk Kim1d7be272017-05-17 10:36:58 -07001815 if (try_to_freeze())
1816 continue;
Chao Yu3b60d802018-01-25 18:57:27 +08001817 if (f2fs_readonly(sbi->sb))
1818 continue;
Jaegeuk Kim1d7be272017-05-17 10:36:58 -07001819 if (kthread_should_stop())
1820 return 0;
Yunlei Hed6184772018-04-13 11:08:05 +08001821 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) {
1822 wait_ms = dpolicy.max_interval;
1823 continue;
1824 }
Chao Yu43f8c47e2021-02-22 18:07:33 +08001825 if (!atomic_read(&dcc->discard_cmd_cnt))
1826 continue;
Jaegeuk Kim1d7be272017-05-17 10:36:58 -07001827
Chao Yudc6febb2017-07-22 08:52:23 +08001828 sb_start_intwrite(sbi->sb);
1829
Chao Yu78997b52017-10-04 09:08:34 +08001830 issued = __issue_discard_cmd(sbi, &dpolicy);
Yunlei Hef9d1dce2018-04-08 15:11:11 +08001831 if (issued > 0) {
Chao Yu78997b52017-10-04 09:08:34 +08001832 __wait_all_discard_cmd(sbi, &dpolicy);
1833 wait_ms = dpolicy.min_interval;
Yi Zhuang5f029c02021-04-06 09:47:35 +08001834 } else if (issued == -1) {
Sahitya Tummalaa7d10cf2018-09-19 14:18:47 +05301835 wait_ms = f2fs_time_to_wait(sbi, DISCARD_TIME);
1836 if (!wait_ms)
Sahitya Tummalaabde73c2018-08-31 15:09:26 +05301837 wait_ms = dpolicy.mid_interval;
Chao Yu969d1b12017-08-07 23:09:56 +08001838 } else {
Chao Yu78997b52017-10-04 09:08:34 +08001839 wait_ms = dpolicy.max_interval;
Chao Yu969d1b12017-08-07 23:09:56 +08001840 }
Jaegeuk Kim1d7be272017-05-17 10:36:58 -07001841
Chao Yudc6febb2017-07-22 08:52:23 +08001842 sb_end_intwrite(sbi->sb);
1843
Jaegeuk Kim1d7be272017-05-17 10:36:58 -07001844 } while (!kthread_should_stop());
1845 return 0;
Jaegeuk Kim15469962017-01-09 20:32:07 -08001846}
1847
Damien Le Moalf46e88092016-10-28 17:45:06 +09001848#ifdef CONFIG_BLK_DEV_ZONED
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001849static int __f2fs_issue_discard_zone(struct f2fs_sb_info *sbi,
1850 struct block_device *bdev, block_t blkstart, block_t blklen)
Damien Le Moalf46e88092016-10-28 17:45:06 +09001851{
Jaegeuk Kim925922852017-02-22 20:18:35 -08001852 sector_t sector, nr_sects;
Kinglong Mee10a875f2017-03-08 09:49:53 +08001853 block_t lblkstart = blkstart;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001854 int devi = 0;
Damien Le Moalf46e88092016-10-28 17:45:06 +09001855
Damien Le Moal09168782019-03-16 09:13:06 +09001856 if (f2fs_is_multi_device(sbi)) {
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001857 devi = f2fs_target_device_index(sbi, blkstart);
Damien Le Moal95175da2019-03-16 09:13:07 +09001858 if (blkstart < FDEV(devi).start_blk ||
1859 blkstart > FDEV(devi).end_blk) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08001860 f2fs_err(sbi, "Invalid block %x", blkstart);
Damien Le Moal95175da2019-03-16 09:13:07 +09001861 return -EIO;
1862 }
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001863 blkstart -= FDEV(devi).start_blk;
1864 }
Damien Le Moalf46e88092016-10-28 17:45:06 +09001865
Damien Le Moal95175da2019-03-16 09:13:07 +09001866 /* For sequential zones, reset the zone write pointer */
1867 if (f2fs_blkz_is_seq(sbi, devi, blkstart)) {
Jaegeuk Kim925922852017-02-22 20:18:35 -08001868 sector = SECTOR_FROM_BLOCK(blkstart);
1869 nr_sects = SECTOR_FROM_BLOCK(blklen);
1870
1871 if (sector & (bdev_zone_sectors(bdev) - 1) ||
1872 nr_sects != bdev_zone_sectors(bdev)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08001873 f2fs_err(sbi, "(%d) %s: Unaligned zone reset attempted (block %x + %x)",
1874 devi, sbi->s_ndevs ? FDEV(devi).path : "",
1875 blkstart, blklen);
Jaegeuk Kim925922852017-02-22 20:18:35 -08001876 return -EIO;
1877 }
Jaegeuk Kimd50aaee2017-02-15 11:14:06 -08001878 trace_f2fs_issue_reset_zone(bdev, blkstart);
Ajay Joshi6c1b1da2019-10-27 23:05:45 +09001879 return blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET,
1880 sector, nr_sects, GFP_NOFS);
Damien Le Moalf46e88092016-10-28 17:45:06 +09001881 }
Damien Le Moal95175da2019-03-16 09:13:07 +09001882
1883 /* For conventional zones, use regular discard if supported */
Damien Le Moal95175da2019-03-16 09:13:07 +09001884 return __queue_discard_cmd(sbi, bdev, lblkstart, blklen);
Damien Le Moalf46e88092016-10-28 17:45:06 +09001885}
1886#endif
1887
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001888static int __issue_discard_async(struct f2fs_sb_info *sbi,
1889 struct block_device *bdev, block_t blkstart, block_t blklen)
1890{
1891#ifdef CONFIG_BLK_DEV_ZONED
Damien Le Moal7f3d7712019-03-16 09:13:08 +09001892 if (f2fs_sb_has_blkzoned(sbi) && bdev_is_zoned(bdev))
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001893 return __f2fs_issue_discard_zone(sbi, bdev, blkstart, blklen);
1894#endif
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001895 return __queue_discard_cmd(sbi, bdev, blkstart, blklen);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001896}
1897
Jaegeuk Kim1e87a782014-04-15 13:57:55 +09001898static int f2fs_issue_discard(struct f2fs_sb_info *sbi,
Jaegeuk Kim37208872013-11-12 16:55:17 +09001899 block_t blkstart, block_t blklen)
1900{
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001901 sector_t start = blkstart, len = 0;
1902 struct block_device *bdev;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001903 struct seg_entry *se;
1904 unsigned int offset;
1905 block_t i;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001906 int err = 0;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001907
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001908 bdev = f2fs_target_device(sbi, blkstart, NULL);
1909
1910 for (i = blkstart; i < blkstart + blklen; i++, len++) {
1911 if (i != start) {
1912 struct block_device *bdev2 =
1913 f2fs_target_device(sbi, i, NULL);
1914
1915 if (bdev2 != bdev) {
1916 err = __issue_discard_async(sbi, bdev,
1917 start, len);
1918 if (err)
1919 return err;
1920 bdev = bdev2;
1921 start = i;
1922 len = 0;
1923 }
1924 }
1925
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001926 se = get_seg_entry(sbi, GET_SEGNO(sbi, i));
1927 offset = GET_BLKOFF_FROM_SEG0(sbi, i);
1928
Chao Yu4f993262021-08-03 08:15:43 +08001929 if (f2fs_block_unit_discard(sbi) &&
1930 !f2fs_test_and_set_bit(offset, se->discard_map))
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001931 sbi->discard_blks--;
1932 }
Damien Le Moalf46e88092016-10-28 17:45:06 +09001933
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001934 if (len)
1935 err = __issue_discard_async(sbi, bdev, start, len);
1936 return err;
Jaegeuk Kim1e87a782014-04-15 13:57:55 +09001937}
1938
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08001939static bool add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc,
1940 bool check_only)
Jaegeuk Kimadf49832014-10-28 22:27:59 -07001941{
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001942 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
1943 int max_blocks = sbi->blocks_per_seg;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001944 struct seg_entry *se = get_seg_entry(sbi, cpc->trim_start);
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001945 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
1946 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001947 unsigned long *discard_map = (unsigned long *)se->discard_map;
Jaegeuk Kim60a3b782015-02-10 16:44:29 -08001948 unsigned long *dmap = SIT_I(sbi)->tmp_map;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001949 unsigned int start = 0, end = -1;
Chao Yuc473f1a2017-04-27 20:40:39 +08001950 bool force = (cpc->reason & CP_DISCARD);
Chao Yua7eeb8232017-03-28 18:18:50 +08001951 struct discard_entry *de = NULL;
Chao Yu46f84c22017-04-15 14:09:36 +08001952 struct list_head *head = &SM_I(sbi)->dcc_info->entry_list;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001953 int i;
1954
Chao Yu4f993262021-08-03 08:15:43 +08001955 if (se->valid_blocks == max_blocks || !f2fs_hw_support_discard(sbi) ||
1956 !f2fs_block_unit_discard(sbi))
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08001957 return false;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001958
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001959 if (!force) {
Chao Yu7d20c8a2018-09-04 03:52:17 +08001960 if (!f2fs_realtime_discard_enable(sbi) || !se->valid_blocks ||
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001961 SM_I(sbi)->dcc_info->nr_discards >=
1962 SM_I(sbi)->dcc_info->max_discards)
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08001963 return false;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001964 }
1965
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001966 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
1967 for (i = 0; i < entries; i++)
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001968 dmap[i] = force ? ~ckpt_map[i] & ~discard_map[i] :
Jaegeuk Kimd7bc2482014-12-12 13:53:41 -08001969 (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001970
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001971 while (force || SM_I(sbi)->dcc_info->nr_discards <=
1972 SM_I(sbi)->dcc_info->max_discards) {
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001973 start = __find_rev_next_bit(dmap, max_blocks, end + 1);
1974 if (start >= max_blocks)
1975 break;
1976
1977 end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
Yunlei Hec7b41e12016-07-07 12:13:33 +08001978 if (force && start && end != max_blocks
1979 && (end - start) < cpc->trim_minlen)
1980 continue;
1981
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08001982 if (check_only)
1983 return true;
1984
Chao Yua7eeb8232017-03-28 18:18:50 +08001985 if (!de) {
1986 de = f2fs_kmem_cache_alloc(discard_entry_slab,
Chao Yu32410572021-08-09 08:24:48 +08001987 GFP_F2FS_ZERO, true, NULL);
Chao Yua7eeb8232017-03-28 18:18:50 +08001988 de->start_blkaddr = START_BLOCK(sbi, cpc->trim_start);
1989 list_add_tail(&de->list, head);
1990 }
1991
1992 for (i = start; i < end; i++)
1993 __set_bit_le(i, (void *)de->discard_map);
1994
1995 SM_I(sbi)->dcc_info->nr_discards += end - start;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001996 }
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08001997 return false;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001998}
1999
Chao Yuaf8ff652018-04-25 17:38:29 +08002000static void release_discard_addr(struct discard_entry *entry)
2001{
2002 list_del(&entry->list);
2003 kmem_cache_free(discard_entry_slab, entry);
2004}
2005
Chao Yu4d57b862018-05-30 00:20:41 +08002006void f2fs_release_discard_addrs(struct f2fs_sb_info *sbi)
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002007{
Chao Yu46f84c22017-04-15 14:09:36 +08002008 struct list_head *head = &(SM_I(sbi)->dcc_info->entry_list);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002009 struct discard_entry *entry, *this;
2010
2011 /* drop caches */
Chao Yuaf8ff652018-04-25 17:38:29 +08002012 list_for_each_entry_safe(entry, this, head, list)
2013 release_discard_addr(entry);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002014}
2015
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002016/*
Chao Yu4d57b862018-05-30 00:20:41 +08002017 * Should call f2fs_clear_prefree_segments after checkpoint is done.
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002018 */
2019static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
2020{
2021 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Chao Yub65ee142014-08-04 10:10:07 +08002022 unsigned int segno;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002023
2024 mutex_lock(&dirty_i->seglist_lock);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002025 for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], MAIN_SEGS(sbi))
Chao Yud0b9e422020-08-04 21:14:45 +08002026 __set_test_and_free(sbi, segno, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002027 mutex_unlock(&dirty_i->seglist_lock);
2028}
2029
Chao Yu4d57b862018-05-30 00:20:41 +08002030void f2fs_clear_prefree_segments(struct f2fs_sb_info *sbi,
2031 struct cp_control *cpc)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002032{
Chao Yu969d1b12017-08-07 23:09:56 +08002033 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
2034 struct list_head *head = &dcc->entry_list;
Chao Yu2d7b8222014-03-29 11:33:17 +08002035 struct discard_entry *entry, *this;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002036 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Changman Lee29e59c12013-11-11 09:24:37 +09002037 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
Changman Lee29e59c12013-11-11 09:24:37 +09002038 unsigned int start = 0, end = -1;
Jaegeuk Kim36abef42016-06-03 19:29:38 -07002039 unsigned int secno, start_segno;
Chao Yuc473f1a2017-04-27 20:40:39 +08002040 bool force = (cpc->reason & CP_DISCARD);
Chao Yu4f993262021-08-03 08:15:43 +08002041 bool section_alignment = F2FS_OPTION(sbi).discard_unit ==
2042 DISCARD_UNIT_SECTION;
2043
2044 if (f2fs_lfs_mode(sbi) && __is_large_section(sbi))
2045 section_alignment = true;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002046
2047 mutex_lock(&dirty_i->seglist_lock);
Changman Lee29e59c12013-11-11 09:24:37 +09002048
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002049 while (1) {
Changman Lee29e59c12013-11-11 09:24:37 +09002050 int i;
Yunlong Songad6672b2018-07-19 20:58:15 +08002051
Chao Yu4f993262021-08-03 08:15:43 +08002052 if (section_alignment && end != -1)
Yunlong Songad6672b2018-07-19 20:58:15 +08002053 end--;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002054 start = find_next_bit(prefree_map, MAIN_SEGS(sbi), end + 1);
2055 if (start >= MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002056 break;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002057 end = find_next_zero_bit(prefree_map, MAIN_SEGS(sbi),
2058 start + 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002059
Chao Yu4f993262021-08-03 08:15:43 +08002060 if (section_alignment) {
Yunlong Songad6672b2018-07-19 20:58:15 +08002061 start = rounddown(start, sbi->segs_per_sec);
2062 end = roundup(end, sbi->segs_per_sec);
2063 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002064
Yunlong Songad6672b2018-07-19 20:58:15 +08002065 for (i = start; i < end; i++) {
2066 if (test_and_clear_bit(i, prefree_map))
2067 dirty_i->nr_dirty[PRE]--;
2068 }
Changman Lee29e59c12013-11-11 09:24:37 +09002069
Chao Yu7d20c8a2018-09-04 03:52:17 +08002070 if (!f2fs_realtime_discard_enable(sbi))
Changman Lee29e59c12013-11-11 09:24:37 +09002071 continue;
2072
Yunlei He650d3c42016-12-22 11:46:24 +08002073 if (force && start >= cpc->trim_start &&
2074 (end - 1) <= cpc->trim_end)
2075 continue;
2076
Chao Yub0332a02020-02-14 17:44:12 +08002077 if (!f2fs_lfs_mode(sbi) || !__is_large_section(sbi)) {
Jaegeuk Kim36abef42016-06-03 19:29:38 -07002078 f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
Jaegeuk Kim37208872013-11-12 16:55:17 +09002079 (end - start) << sbi->log_blocks_per_seg);
Jaegeuk Kim36abef42016-06-03 19:29:38 -07002080 continue;
2081 }
2082next:
Jaegeuk Kim4ddb1a42017-04-07 15:08:17 -07002083 secno = GET_SEC_FROM_SEG(sbi, start);
2084 start_segno = GET_SEG_FROM_SEC(sbi, secno);
Jaegeuk Kim36abef42016-06-03 19:29:38 -07002085 if (!IS_CURSEC(sbi, secno) &&
Jaegeuk Kim302bd342017-04-07 14:33:22 -07002086 !get_valid_blocks(sbi, start, true))
Jaegeuk Kim36abef42016-06-03 19:29:38 -07002087 f2fs_issue_discard(sbi, START_BLOCK(sbi, start_segno),
2088 sbi->segs_per_sec << sbi->log_blocks_per_seg);
2089
2090 start = start_segno + sbi->segs_per_sec;
2091 if (start < end)
2092 goto next;
Jaegeuk Kim8b107f52017-02-27 11:57:11 -08002093 else
2094 end = start - 1;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002095 }
2096 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kimb2955552013-11-12 14:49:56 +09002097
Chao Yu4f993262021-08-03 08:15:43 +08002098 if (!f2fs_block_unit_discard(sbi))
2099 goto wakeup;
2100
Jaegeuk Kimb2955552013-11-12 14:49:56 +09002101 /* send small discards */
Chao Yu2d7b8222014-03-29 11:33:17 +08002102 list_for_each_entry_safe(entry, this, head, list) {
Chao Yua7eeb8232017-03-28 18:18:50 +08002103 unsigned int cur_pos = 0, next_pos, len, total_len = 0;
2104 bool is_valid = test_bit_le(0, entry->discard_map);
2105
2106find_next:
2107 if (is_valid) {
2108 next_pos = find_next_zero_bit_le(entry->discard_map,
2109 sbi->blocks_per_seg, cur_pos);
2110 len = next_pos - cur_pos;
2111
Chao Yu7beb01f2018-10-24 18:34:26 +08002112 if (f2fs_sb_has_blkzoned(sbi) ||
Damien Le Moalacfd28102017-05-26 17:04:40 +09002113 (force && len < cpc->trim_minlen))
Chao Yua7eeb8232017-03-28 18:18:50 +08002114 goto skip;
2115
2116 f2fs_issue_discard(sbi, entry->start_blkaddr + cur_pos,
2117 len);
Chao Yua7eeb8232017-03-28 18:18:50 +08002118 total_len += len;
2119 } else {
2120 next_pos = find_next_bit_le(entry->discard_map,
2121 sbi->blocks_per_seg, cur_pos);
2122 }
Jaegeuk Kim836b5a62015-04-30 22:50:06 -07002123skip:
Chao Yua7eeb8232017-03-28 18:18:50 +08002124 cur_pos = next_pos;
2125 is_valid = !is_valid;
2126
2127 if (cur_pos < sbi->blocks_per_seg)
2128 goto find_next;
2129
Chao Yuaf8ff652018-04-25 17:38:29 +08002130 release_discard_addr(entry);
Chao Yu969d1b12017-08-07 23:09:56 +08002131 dcc->nr_discards -= total_len;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09002132 }
Chao Yu34e159d2017-04-25 00:21:34 +08002133
Chao Yu4f993262021-08-03 08:15:43 +08002134wakeup:
Jaegeuk Kim01983c72017-08-22 21:15:43 -07002135 wake_up_discard_thread(sbi, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002136}
2137
Fengnan Chang4d674902021-08-19 16:02:37 +08002138int f2fs_start_discard_thread(struct f2fs_sb_info *sbi)
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08002139{
Jaegeuk Kim15469962017-01-09 20:32:07 -08002140 dev_t dev = sbi->sb->s_bdev->bd_dev;
Fengnan Chang4d674902021-08-19 16:02:37 +08002141 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
2142 int err = 0;
2143
2144 if (!f2fs_realtime_discard_enable(sbi))
2145 return 0;
2146
2147 dcc->f2fs_issue_discard = kthread_run(issue_discard_thread, sbi,
2148 "f2fs_discard-%u:%u", MAJOR(dev), MINOR(dev));
2149 if (IS_ERR(dcc->f2fs_issue_discard))
2150 err = PTR_ERR(dcc->f2fs_issue_discard);
2151
2152 return err;
2153}
2154
2155static int create_discard_cmd_control(struct f2fs_sb_info *sbi)
2156{
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08002157 struct discard_cmd_control *dcc;
Chao Yuba48a332017-04-15 14:09:37 +08002158 int err = 0, i;
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08002159
2160 if (SM_I(sbi)->dcc_info) {
2161 dcc = SM_I(sbi)->dcc_info;
2162 goto init_thread;
2163 }
2164
Chao Yuacbf0542017-11-30 19:28:17 +08002165 dcc = f2fs_kzalloc(sbi, sizeof(struct discard_cmd_control), GFP_KERNEL);
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08002166 if (!dcc)
2167 return -ENOMEM;
2168
Chao Yu969d1b12017-08-07 23:09:56 +08002169 dcc->discard_granularity = DEFAULT_DISCARD_GRANULARITY;
Chao Yu4f993262021-08-03 08:15:43 +08002170 if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_SEGMENT)
2171 dcc->discard_granularity = sbi->blocks_per_seg;
2172 else if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_SECTION)
2173 dcc->discard_granularity = BLKS_PER_SEC(sbi);
2174
Chao Yu46f84c22017-04-15 14:09:36 +08002175 INIT_LIST_HEAD(&dcc->entry_list);
Chao Yu78997b52017-10-04 09:08:34 +08002176 for (i = 0; i < MAX_PLIST_NUM; i++)
Chao Yuba48a332017-04-15 14:09:37 +08002177 INIT_LIST_HEAD(&dcc->pend_list[i]);
Chao Yu46f84c22017-04-15 14:09:36 +08002178 INIT_LIST_HEAD(&dcc->wait_list);
Chao Yu84126632017-10-04 09:08:32 +08002179 INIT_LIST_HEAD(&dcc->fstrim_list);
Jaegeuk Kim15469962017-01-09 20:32:07 -08002180 mutex_init(&dcc->cmd_lock);
Chao Yu8b8dd652017-03-25 17:19:58 +08002181 atomic_set(&dcc->issued_discard, 0);
Jaegeuk Kim72691af2018-12-13 16:53:57 -08002182 atomic_set(&dcc->queued_discard, 0);
Chao Yu5f323662017-03-25 17:19:59 +08002183 atomic_set(&dcc->discard_cmd_cnt, 0);
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08002184 dcc->nr_discards = 0;
Chao Yud618eba2017-04-25 00:21:35 +08002185 dcc->max_discards = MAIN_SEGS(sbi) << sbi->log_blocks_per_seg;
Chao Yud84d1cb2017-04-18 19:27:39 +08002186 dcc->undiscard_blks = 0;
Chao Yu20ee4382018-07-08 22:11:01 +08002187 dcc->next_pos = 0;
Chao Yu4dada3f2018-10-04 11:18:30 +08002188 dcc->root = RB_ROOT_CACHED;
Chao Yu67fce702018-06-22 16:06:59 +08002189 dcc->rbtree_check = false;
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08002190
Jaegeuk Kim15469962017-01-09 20:32:07 -08002191 init_waitqueue_head(&dcc->discard_wait_queue);
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08002192 SM_I(sbi)->dcc_info = dcc;
2193init_thread:
Fengnan Chang4d674902021-08-19 16:02:37 +08002194 err = f2fs_start_discard_thread(sbi);
2195 if (err) {
Chao Yuc8eb7022020-09-14 16:47:00 +08002196 kfree(dcc);
Jaegeuk Kim15469962017-01-09 20:32:07 -08002197 SM_I(sbi)->dcc_info = NULL;
Jaegeuk Kim15469962017-01-09 20:32:07 -08002198 }
2199
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08002200 return err;
2201}
2202
Chao Yuf0994052017-03-27 18:14:04 +08002203static void destroy_discard_cmd_control(struct f2fs_sb_info *sbi)
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08002204{
2205 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
2206
Chao Yuf0994052017-03-27 18:14:04 +08002207 if (!dcc)
2208 return;
2209
Chao Yu4d57b862018-05-30 00:20:41 +08002210 f2fs_stop_discard_thread(sbi);
Chao Yuf0994052017-03-27 18:14:04 +08002211
Chao Yu04f92872019-07-19 15:18:44 +08002212 /*
2213 * Recovery can cache discard commands, so in error path of
2214 * fill_super(), it needs to give a chance to handle them.
2215 */
2216 if (unlikely(atomic_read(&dcc->discard_cmd_cnt)))
2217 f2fs_issue_discard_timeout(sbi);
2218
Chao Yuc8eb7022020-09-14 16:47:00 +08002219 kfree(dcc);
Chao Yuf0994052017-03-27 18:14:04 +08002220 SM_I(sbi)->dcc_info = NULL;
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08002221}
2222
Chao Yu184a5cd2014-09-04 18:13:01 +08002223static bool __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002224{
2225 struct sit_info *sit_i = SIT_I(sbi);
Chao Yu184a5cd2014-09-04 18:13:01 +08002226
2227 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002228 sit_i->dirty_sentries++;
Chao Yu184a5cd2014-09-04 18:13:01 +08002229 return false;
2230 }
2231
2232 return true;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002233}
2234
2235static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
2236 unsigned int segno, int modified)
2237{
2238 struct seg_entry *se = get_seg_entry(sbi, segno);
Yi Zhuang5f029c02021-04-06 09:47:35 +08002239
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002240 se->type = type;
2241 if (modified)
2242 __mark_sit_entry_dirty(sbi, segno);
2243}
2244
Chao Yuc5d02782020-08-04 21:14:47 +08002245static inline unsigned long long get_segment_mtime(struct f2fs_sb_info *sbi,
2246 block_t blkaddr)
Chao Yu6f3a01a2020-08-04 21:14:46 +08002247{
2248 unsigned int segno = GET_SEGNO(sbi, blkaddr);
Chao Yuc5d02782020-08-04 21:14:47 +08002249
2250 if (segno == NULL_SEGNO)
2251 return 0;
2252 return get_seg_entry(sbi, segno)->mtime;
2253}
2254
2255static void update_segment_mtime(struct f2fs_sb_info *sbi, block_t blkaddr,
2256 unsigned long long old_mtime)
2257{
2258 struct seg_entry *se;
2259 unsigned int segno = GET_SEGNO(sbi, blkaddr);
2260 unsigned long long ctime = get_mtime(sbi, false);
2261 unsigned long long mtime = old_mtime ? old_mtime : ctime;
2262
2263 if (segno == NULL_SEGNO)
2264 return;
2265
2266 se = get_seg_entry(sbi, segno);
Chao Yu6f3a01a2020-08-04 21:14:46 +08002267
2268 if (!se->mtime)
2269 se->mtime = mtime;
2270 else
2271 se->mtime = div_u64(se->mtime * se->valid_blocks + mtime,
2272 se->valid_blocks + 1);
2273
Chao Yuc5d02782020-08-04 21:14:47 +08002274 if (ctime > SIT_I(sbi)->max_mtime)
2275 SIT_I(sbi)->max_mtime = ctime;
Chao Yu6f3a01a2020-08-04 21:14:46 +08002276}
2277
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002278static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
2279{
2280 struct seg_entry *se;
2281 unsigned int segno, offset;
2282 long int new_vblocks;
Yunlong Song6415fed2017-08-02 21:20:13 +08002283 bool exist;
2284#ifdef CONFIG_F2FS_CHECK_FS
2285 bool mir_exist;
2286#endif
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002287
2288 segno = GET_SEGNO(sbi, blkaddr);
2289
2290 se = get_seg_entry(sbi, segno);
2291 new_vblocks = se->valid_blocks + del;
Jaegeuk Kim491c0852014-02-04 13:01:10 +09002292 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002293
Zhihao Cheng9feffe142020-08-01 11:24:50 +08002294 f2fs_bug_on(sbi, (new_vblocks < 0 ||
Aravind Rameshde881df2020-07-16 18:26:56 +05302295 (new_vblocks > f2fs_usable_blks_in_seg(sbi, segno))));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002296
2297 se->valid_blocks = new_vblocks;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002298
2299 /* Update valid block bitmap */
2300 if (del > 0) {
Yunlong Song6415fed2017-08-02 21:20:13 +08002301 exist = f2fs_test_and_set_bit(offset, se->cur_valid_map);
Chao Yu355e7892017-01-07 18:51:01 +08002302#ifdef CONFIG_F2FS_CHECK_FS
Yunlong Song6415fed2017-08-02 21:20:13 +08002303 mir_exist = f2fs_test_and_set_bit(offset,
2304 se->cur_valid_map_mir);
2305 if (unlikely(exist != mir_exist)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002306 f2fs_err(sbi, "Inconsistent error when setting bitmap, blk:%u, old bit:%d",
2307 blkaddr, exist);
Jaegeuk Kim05796762014-09-02 16:05:00 -07002308 f2fs_bug_on(sbi, 1);
Chao Yu355e7892017-01-07 18:51:01 +08002309 }
Yunlong Song6415fed2017-08-02 21:20:13 +08002310#endif
2311 if (unlikely(exist)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002312 f2fs_err(sbi, "Bitmap was wrongly set, blk:%u",
2313 blkaddr);
Yunlong Song6415fed2017-08-02 21:20:13 +08002314 f2fs_bug_on(sbi, 1);
Yunlong Song35ee82c2017-08-02 22:16:54 +08002315 se->valid_blocks--;
2316 del = 0;
Yunlong Song6415fed2017-08-02 21:20:13 +08002317 }
2318
Chao Yu4f993262021-08-03 08:15:43 +08002319 if (f2fs_block_unit_discard(sbi) &&
2320 !f2fs_test_and_set_bit(offset, se->discard_map))
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07002321 sbi->discard_blks--;
Jaegeuk Kim720037f2017-03-06 11:59:56 -08002322
Chao Yu899fee32019-08-16 11:03:34 +08002323 /*
2324 * SSR should never reuse block which is checkpointed
2325 * or newly invalidated.
2326 */
2327 if (!is_sbi_flag_set(sbi, SBI_CP_DISABLED)) {
Jaegeuk Kim720037f2017-03-06 11:59:56 -08002328 if (!f2fs_test_and_set_bit(offset, se->ckpt_valid_map))
2329 se->ckpt_valid_blocks++;
2330 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002331 } else {
Yunlong Song6415fed2017-08-02 21:20:13 +08002332 exist = f2fs_test_and_clear_bit(offset, se->cur_valid_map);
Chao Yu355e7892017-01-07 18:51:01 +08002333#ifdef CONFIG_F2FS_CHECK_FS
Yunlong Song6415fed2017-08-02 21:20:13 +08002334 mir_exist = f2fs_test_and_clear_bit(offset,
2335 se->cur_valid_map_mir);
2336 if (unlikely(exist != mir_exist)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002337 f2fs_err(sbi, "Inconsistent error when clearing bitmap, blk:%u, old bit:%d",
2338 blkaddr, exist);
Jaegeuk Kim05796762014-09-02 16:05:00 -07002339 f2fs_bug_on(sbi, 1);
Chao Yu355e7892017-01-07 18:51:01 +08002340 }
Yunlong Song6415fed2017-08-02 21:20:13 +08002341#endif
2342 if (unlikely(!exist)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002343 f2fs_err(sbi, "Bitmap was wrongly cleared, blk:%u",
2344 blkaddr);
Yunlong Song6415fed2017-08-02 21:20:13 +08002345 f2fs_bug_on(sbi, 1);
Yunlong Song35ee82c2017-08-02 22:16:54 +08002346 se->valid_blocks++;
2347 del = 0;
Daniel Rosenberg43549942018-08-20 19:21:43 -07002348 } else if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
2349 /*
2350 * If checkpoints are off, we must not reuse data that
2351 * was used in the previous checkpoint. If it was used
2352 * before, we must track that to know how much space we
2353 * really have.
2354 */
Chao Yuc9c8ed52019-05-05 11:40:46 +08002355 if (f2fs_test_bit(offset, se->ckpt_valid_map)) {
2356 spin_lock(&sbi->stat_lock);
Daniel Rosenberg43549942018-08-20 19:21:43 -07002357 sbi->unusable_block_count++;
Chao Yuc9c8ed52019-05-05 11:40:46 +08002358 spin_unlock(&sbi->stat_lock);
2359 }
Yunlong Song6415fed2017-08-02 21:20:13 +08002360 }
2361
Chao Yu4f993262021-08-03 08:15:43 +08002362 if (f2fs_block_unit_discard(sbi) &&
2363 f2fs_test_and_clear_bit(offset, se->discard_map))
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07002364 sbi->discard_blks++;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002365 }
2366 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
2367 se->ckpt_valid_blocks += del;
2368
2369 __mark_sit_entry_dirty(sbi, segno);
2370
2371 /* update total number of valid blocks to be written in ckpt area */
2372 SIT_I(sbi)->written_valid_blocks += del;
2373
Chao Yu2c70c5e2018-10-24 18:37:26 +08002374 if (__is_large_section(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002375 get_sec_entry(sbi, segno)->valid_blocks += del;
2376}
2377
Chao Yu4d57b862018-05-30 00:20:41 +08002378void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002379{
2380 unsigned int segno = GET_SEGNO(sbi, addr);
2381 struct sit_info *sit_i = SIT_I(sbi);
2382
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002383 f2fs_bug_on(sbi, addr == NULL_ADDR);
Chao Yu4c8ff702019-11-01 18:07:14 +08002384 if (addr == NEW_ADDR || addr == COMPRESS_ADDR)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002385 return;
2386
Chao Yu6aa58d82018-08-14 22:37:25 +08002387 invalidate_mapping_pages(META_MAPPING(sbi), addr, addr);
Chao Yu6ce19af2021-05-20 19:51:50 +08002388 f2fs_invalidate_compress_page(sbi, addr);
Chao Yu6aa58d82018-08-14 22:37:25 +08002389
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002390 /* add it into sit main buffer */
Chao Yu3d26fa62017-10-30 17:49:53 +08002391 down_write(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002392
Chao Yuc5d02782020-08-04 21:14:47 +08002393 update_segment_mtime(sbi, addr, 0);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002394 update_sit_entry(sbi, addr, -1);
2395
2396 /* add it into dirty seglist */
2397 locate_dirty_segment(sbi, segno);
2398
Chao Yu3d26fa62017-10-30 17:49:53 +08002399 up_write(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002400}
2401
Chao Yu4d57b862018-05-30 00:20:41 +08002402bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr)
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07002403{
2404 struct sit_info *sit_i = SIT_I(sbi);
2405 unsigned int segno, offset;
2406 struct seg_entry *se;
2407 bool is_cp = false;
2408
Chao Yu93770ab2019-04-15 15:26:32 +08002409 if (!__is_valid_data_blkaddr(blkaddr))
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07002410 return true;
2411
Chao Yu3d26fa62017-10-30 17:49:53 +08002412 down_read(&sit_i->sentry_lock);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07002413
2414 segno = GET_SEGNO(sbi, blkaddr);
2415 se = get_seg_entry(sbi, segno);
2416 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
2417
2418 if (f2fs_test_bit(offset, se->ckpt_valid_map))
2419 is_cp = true;
2420
Chao Yu3d26fa62017-10-30 17:49:53 +08002421 up_read(&sit_i->sentry_lock);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07002422
2423 return is_cp;
2424}
2425
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002426/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002427 * This function should be resided under the curseg_mutex lock
2428 */
2429static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
Haicheng Lie79efe32013-06-13 16:59:27 +08002430 struct f2fs_summary *sum)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002431{
2432 struct curseg_info *curseg = CURSEG_I(sbi, type);
2433 void *addr = curseg->sum_blk;
Yi Zhuang5f029c02021-04-06 09:47:35 +08002434
Haicheng Lie79efe32013-06-13 16:59:27 +08002435 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002436 memcpy(addr, sum, sizeof(struct f2fs_summary));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002437}
2438
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002439/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002440 * Calculate the number of current summary pages for writing
2441 */
Chao Yu4d57b862018-05-30 00:20:41 +08002442int f2fs_npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002443{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002444 int valid_sum_count = 0;
Fan Li9a479382013-10-29 16:21:47 +08002445 int i, sum_in_page;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002446
2447 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
2448 if (sbi->ckpt->alloc_type[i] == SSR)
2449 valid_sum_count += sbi->blocks_per_seg;
Chao Yu3fa06d72014-12-09 14:21:46 +08002450 else {
2451 if (for_ra)
2452 valid_sum_count += le16_to_cpu(
2453 F2FS_CKPT(sbi)->cur_data_blkoff[i]);
2454 else
2455 valid_sum_count += curseg_blkoff(sbi, i);
2456 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002457 }
2458
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002459 sum_in_page = (PAGE_SIZE - 2 * SUM_JOURNAL_SIZE -
Fan Li9a479382013-10-29 16:21:47 +08002460 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
2461 if (valid_sum_count <= sum_in_page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002462 return 1;
Fan Li9a479382013-10-29 16:21:47 +08002463 else if ((valid_sum_count - sum_in_page) <=
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002464 (PAGE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002465 return 2;
2466 return 3;
2467}
2468
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002469/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002470 * Caller should put this summary page
2471 */
Chao Yu4d57b862018-05-30 00:20:41 +08002472struct page *f2fs_get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002473{
Jaegeuk Kim86f33602020-10-02 14:17:35 -07002474 if (unlikely(f2fs_cp_error(sbi)))
2475 return ERR_PTR(-EIO);
2476 return f2fs_get_meta_page_retry(sbi, GET_SUM_BLOCK(sbi, segno));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002477}
2478
Chao Yu4d57b862018-05-30 00:20:41 +08002479void f2fs_update_meta_page(struct f2fs_sb_info *sbi,
2480 void *src, block_t blk_addr)
Chao Yu381722d2015-05-19 17:40:04 +08002481{
Chao Yu4d57b862018-05-30 00:20:41 +08002482 struct page *page = f2fs_grab_meta_page(sbi, blk_addr);
Chao Yu381722d2015-05-19 17:40:04 +08002483
Chao Yu0537b812017-11-02 20:41:02 +08002484 memcpy(page_address(page), src, PAGE_SIZE);
Chao Yu381722d2015-05-19 17:40:04 +08002485 set_page_dirty(page);
2486 f2fs_put_page(page, 1);
2487}
2488
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002489static void write_sum_page(struct f2fs_sb_info *sbi,
2490 struct f2fs_summary_block *sum_blk, block_t blk_addr)
2491{
Chao Yu4d57b862018-05-30 00:20:41 +08002492 f2fs_update_meta_page(sbi, (void *)sum_blk, blk_addr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002493}
2494
Chao Yub7ad7512016-02-19 18:08:46 +08002495static void write_current_sum_page(struct f2fs_sb_info *sbi,
2496 int type, block_t blk_addr)
2497{
2498 struct curseg_info *curseg = CURSEG_I(sbi, type);
Chao Yu4d57b862018-05-30 00:20:41 +08002499 struct page *page = f2fs_grab_meta_page(sbi, blk_addr);
Chao Yub7ad7512016-02-19 18:08:46 +08002500 struct f2fs_summary_block *src = curseg->sum_blk;
2501 struct f2fs_summary_block *dst;
2502
2503 dst = (struct f2fs_summary_block *)page_address(page);
Chao Yu81114ba2018-04-09 20:25:06 +08002504 memset(dst, 0, PAGE_SIZE);
Chao Yub7ad7512016-02-19 18:08:46 +08002505
2506 mutex_lock(&curseg->curseg_mutex);
2507
2508 down_read(&curseg->journal_rwsem);
2509 memcpy(&dst->journal, curseg->journal, SUM_JOURNAL_SIZE);
2510 up_read(&curseg->journal_rwsem);
2511
2512 memcpy(dst->entries, src->entries, SUM_ENTRY_SIZE);
2513 memcpy(&dst->footer, &src->footer, SUM_FOOTER_SIZE);
2514
2515 mutex_unlock(&curseg->curseg_mutex);
2516
2517 set_page_dirty(page);
2518 f2fs_put_page(page, 1);
2519}
2520
Chao Yu093749e2020-08-04 21:14:49 +08002521static int is_next_segment_free(struct f2fs_sb_info *sbi,
2522 struct curseg_info *curseg, int type)
Jaegeuk Kima7881892017-04-20 13:51:57 -07002523{
Jaegeuk Kima7881892017-04-20 13:51:57 -07002524 unsigned int segno = curseg->segno + 1;
2525 struct free_segmap_info *free_i = FREE_I(sbi);
2526
2527 if (segno < MAIN_SEGS(sbi) && segno % sbi->segs_per_sec)
2528 return !test_bit(segno, free_i->free_segmap);
2529 return 0;
2530}
2531
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002532/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002533 * Find a new segment from the free segments bitmap to right order
2534 * This function should be returned with success, otherwise BUG
2535 */
2536static void get_new_segment(struct f2fs_sb_info *sbi,
2537 unsigned int *newseg, bool new_sec, int dir)
2538{
2539 struct free_segmap_info *free_i = FREE_I(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002540 unsigned int segno, secno, zoneno;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002541 unsigned int total_zones = MAIN_SECS(sbi) / sbi->secs_per_zone;
Jaegeuk Kim4ddb1a42017-04-07 15:08:17 -07002542 unsigned int hint = GET_SEC_FROM_SEG(sbi, *newseg);
2543 unsigned int old_zoneno = GET_ZONE_FROM_SEG(sbi, *newseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002544 unsigned int left_start = hint;
2545 bool init = true;
2546 int go_left = 0;
2547 int i;
2548
Chao Yu1a118cc2015-02-11 18:20:38 +08002549 spin_lock(&free_i->segmap_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002550
2551 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
2552 segno = find_next_zero_bit(free_i->free_segmap,
Jaegeuk Kim4ddb1a42017-04-07 15:08:17 -07002553 GET_SEG_FROM_SEC(sbi, hint + 1), *newseg + 1);
2554 if (segno < GET_SEG_FROM_SEC(sbi, hint + 1))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002555 goto got_it;
2556 }
2557find_other_zone:
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002558 secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint);
2559 if (secno >= MAIN_SECS(sbi)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002560 if (dir == ALLOC_RIGHT) {
2561 secno = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002562 MAIN_SECS(sbi), 0);
2563 f2fs_bug_on(sbi, secno >= MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002564 } else {
2565 go_left = 1;
2566 left_start = hint - 1;
2567 }
2568 }
2569 if (go_left == 0)
2570 goto skip_left;
2571
2572 while (test_bit(left_start, free_i->free_secmap)) {
2573 if (left_start > 0) {
2574 left_start--;
2575 continue;
2576 }
2577 left_start = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002578 MAIN_SECS(sbi), 0);
2579 f2fs_bug_on(sbi, left_start >= MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002580 break;
2581 }
2582 secno = left_start;
2583skip_left:
Jaegeuk Kim4ddb1a42017-04-07 15:08:17 -07002584 segno = GET_SEG_FROM_SEC(sbi, secno);
2585 zoneno = GET_ZONE_FROM_SEC(sbi, secno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002586
2587 /* give up on finding another zone */
2588 if (!init)
2589 goto got_it;
2590 if (sbi->secs_per_zone == 1)
2591 goto got_it;
2592 if (zoneno == old_zoneno)
2593 goto got_it;
2594 if (dir == ALLOC_LEFT) {
2595 if (!go_left && zoneno + 1 >= total_zones)
2596 goto got_it;
2597 if (go_left && zoneno == 0)
2598 goto got_it;
2599 }
2600 for (i = 0; i < NR_CURSEG_TYPE; i++)
2601 if (CURSEG_I(sbi, i)->zone == zoneno)
2602 break;
2603
2604 if (i < NR_CURSEG_TYPE) {
2605 /* zone is in user, try another */
2606 if (go_left)
2607 hint = zoneno * sbi->secs_per_zone - 1;
2608 else if (zoneno + 1 >= total_zones)
2609 hint = 0;
2610 else
2611 hint = (zoneno + 1) * sbi->secs_per_zone;
2612 init = false;
2613 goto find_other_zone;
2614 }
2615got_it:
2616 /* set it as dirty segment in free segmap */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002617 f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002618 __set_inuse(sbi, segno);
2619 *newseg = segno;
Chao Yu1a118cc2015-02-11 18:20:38 +08002620 spin_unlock(&free_i->segmap_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002621}
2622
2623static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
2624{
2625 struct curseg_info *curseg = CURSEG_I(sbi, type);
2626 struct summary_footer *sum_footer;
Chao Yu093749e2020-08-04 21:14:49 +08002627 unsigned short seg_type = curseg->seg_type;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002628
Chao Yud0b9e422020-08-04 21:14:45 +08002629 curseg->inited = true;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002630 curseg->segno = curseg->next_segno;
Jaegeuk Kim4ddb1a42017-04-07 15:08:17 -07002631 curseg->zone = GET_ZONE_FROM_SEG(sbi, curseg->segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002632 curseg->next_blkoff = 0;
2633 curseg->next_segno = NULL_SEGNO;
2634
2635 sum_footer = &(curseg->sum_blk->footer);
2636 memset(sum_footer, 0, sizeof(struct summary_footer));
Chao Yu093749e2020-08-04 21:14:49 +08002637
2638 sanity_check_seg_type(sbi, seg_type);
2639
2640 if (IS_DATASEG(seg_type))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002641 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
Chao Yu093749e2020-08-04 21:14:49 +08002642 if (IS_NODESEG(seg_type))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002643 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
Chao Yu093749e2020-08-04 21:14:49 +08002644 __set_sit_entry_type(sbi, seg_type, curseg->segno, modified);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002645}
2646
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -04002647static unsigned int __get_next_segno(struct f2fs_sb_info *sbi, int type)
2648{
Chao Yud0b9e422020-08-04 21:14:45 +08002649 struct curseg_info *curseg = CURSEG_I(sbi, type);
Chao Yu093749e2020-08-04 21:14:49 +08002650 unsigned short seg_type = curseg->seg_type;
2651
2652 sanity_check_seg_type(sbi, seg_type);
Daeho Jeong6691d942021-09-29 11:12:03 -07002653 if (f2fs_need_rand_seg(sbi))
2654 return prandom_u32() % (MAIN_SECS(sbi) * sbi->segs_per_sec);
Chao Yud0b9e422020-08-04 21:14:45 +08002655
Jaegeuk Kima7881892017-04-20 13:51:57 -07002656 /* if segs_per_sec is large than 1, we need to keep original policy. */
Chao Yu2c70c5e2018-10-24 18:37:26 +08002657 if (__is_large_section(sbi))
Chao Yud0b9e422020-08-04 21:14:45 +08002658 return curseg->segno;
2659
2660 /* inmem log may not locate on any segment after mount */
2661 if (!curseg->inited)
2662 return 0;
Jaegeuk Kima7881892017-04-20 13:51:57 -07002663
Daniel Rosenberg43549942018-08-20 19:21:43 -07002664 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2665 return 0;
2666
Yunlong Songb94929d2018-01-29 11:37:45 +08002667 if (test_opt(sbi, NOHEAP) &&
Chao Yu093749e2020-08-04 21:14:49 +08002668 (seg_type == CURSEG_HOT_DATA || IS_NODESEG(seg_type)))
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -04002669 return 0;
2670
Jaegeuk Kime066b832017-04-13 15:17:00 -07002671 if (SIT_I(sbi)->last_victim[ALLOC_NEXT])
2672 return SIT_I(sbi)->last_victim[ALLOC_NEXT];
Jaegeuk Kim07939622018-02-18 08:50:49 -08002673
2674 /* find segments from 0 to reuse freed segments */
Chao Yu63189b72018-03-08 14:22:56 +08002675 if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_REUSE)
Jaegeuk Kim07939622018-02-18 08:50:49 -08002676 return 0;
2677
Chao Yud0b9e422020-08-04 21:14:45 +08002678 return curseg->segno;
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -04002679}
2680
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002681/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002682 * Allocate a current working segment.
2683 * This function always allocates a free segment in LFS manner.
2684 */
2685static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
2686{
2687 struct curseg_info *curseg = CURSEG_I(sbi, type);
Chao Yud0b9e422020-08-04 21:14:45 +08002688 unsigned short seg_type = curseg->seg_type;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002689 unsigned int segno = curseg->segno;
2690 int dir = ALLOC_LEFT;
2691
Chao Yud0b9e422020-08-04 21:14:45 +08002692 if (curseg->inited)
2693 write_sum_page(sbi, curseg->sum_blk,
Haicheng Li81fb5e82013-05-14 18:20:28 +08002694 GET_SUM_BLOCK(sbi, segno));
Chao Yud0b9e422020-08-04 21:14:45 +08002695 if (seg_type == CURSEG_WARM_DATA || seg_type == CURSEG_COLD_DATA)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002696 dir = ALLOC_RIGHT;
2697
2698 if (test_opt(sbi, NOHEAP))
2699 dir = ALLOC_RIGHT;
2700
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -04002701 segno = __get_next_segno(sbi, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002702 get_new_segment(sbi, &segno, new_sec, dir);
2703 curseg->next_segno = segno;
2704 reset_curseg(sbi, type, 1);
2705 curseg->alloc_type = LFS;
Daeho Jeong6691d942021-09-29 11:12:03 -07002706 if (F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_BLK)
2707 curseg->fragment_remained_chunk =
2708 prandom_u32() % sbi->max_fragment_chunk + 1;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002709}
2710
Chao Yu453e2ff2021-04-13 17:56:18 +08002711static int __next_free_blkoff(struct f2fs_sb_info *sbi,
2712 int segno, block_t start)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002713{
Chao Yu453e2ff2021-04-13 17:56:18 +08002714 struct seg_entry *se = get_seg_entry(sbi, segno);
Changman Leee81c93c2013-11-15 13:21:16 +09002715 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
Jaegeuk Kim60a3b782015-02-10 16:44:29 -08002716 unsigned long *target_map = SIT_I(sbi)->tmp_map;
Changman Leee81c93c2013-11-15 13:21:16 +09002717 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
2718 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
Chao Yu453e2ff2021-04-13 17:56:18 +08002719 int i;
Changman Leee81c93c2013-11-15 13:21:16 +09002720
2721 for (i = 0; i < entries; i++)
2722 target_map[i] = ckpt_map[i] | cur_map[i];
2723
Chao Yu453e2ff2021-04-13 17:56:18 +08002724 return __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002725}
2726
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002727/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002728 * If a segment is written by LFS manner, next block offset is just obtained
2729 * by increasing the current block offset. However, if a segment is written by
2730 * SSR manner, next block offset obtained by calling __next_free_blkoff
2731 */
2732static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
2733 struct curseg_info *seg)
2734{
Daeho Jeong6691d942021-09-29 11:12:03 -07002735 if (seg->alloc_type == SSR) {
Chao Yu453e2ff2021-04-13 17:56:18 +08002736 seg->next_blkoff =
2737 __next_free_blkoff(sbi, seg->segno,
2738 seg->next_blkoff + 1);
Daeho Jeong6691d942021-09-29 11:12:03 -07002739 } else {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002740 seg->next_blkoff++;
Daeho Jeong6691d942021-09-29 11:12:03 -07002741 if (F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_BLK) {
2742 /* To allocate block chunks in different sizes, use random number */
2743 if (--seg->fragment_remained_chunk <= 0) {
2744 seg->fragment_remained_chunk =
2745 prandom_u32() % sbi->max_fragment_chunk + 1;
2746 seg->next_blkoff +=
2747 prandom_u32() % sbi->max_fragment_hole + 1;
2748 }
2749 }
2750 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002751}
2752
Chao Yu61461fc2021-03-24 11:18:28 +08002753bool f2fs_segment_has_free_slot(struct f2fs_sb_info *sbi, int segno)
2754{
Chao Yu453e2ff2021-04-13 17:56:18 +08002755 return __next_free_blkoff(sbi, segno, 0) < sbi->blocks_per_seg;
Chao Yu61461fc2021-03-24 11:18:28 +08002756}
2757
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002758/*
arter97e1c42042014-08-06 23:22:50 +09002759 * This function always allocates a used segment(from dirty seglist) by SSR
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002760 * manner, so it should recover the existing segment information of valid blocks
2761 */
Chao Yu093749e2020-08-04 21:14:49 +08002762static void change_curseg(struct f2fs_sb_info *sbi, int type, bool flush)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002763{
2764 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2765 struct curseg_info *curseg = CURSEG_I(sbi, type);
2766 unsigned int new_segno = curseg->next_segno;
2767 struct f2fs_summary_block *sum_node;
2768 struct page *sum_page;
2769
Chao Yu093749e2020-08-04 21:14:49 +08002770 if (flush)
2771 write_sum_page(sbi, curseg->sum_blk,
2772 GET_SUM_BLOCK(sbi, curseg->segno));
2773
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002774 __set_test_and_inuse(sbi, new_segno);
2775
2776 mutex_lock(&dirty_i->seglist_lock);
2777 __remove_dirty_segment(sbi, new_segno, PRE);
2778 __remove_dirty_segment(sbi, new_segno, DIRTY);
2779 mutex_unlock(&dirty_i->seglist_lock);
2780
2781 reset_curseg(sbi, type, 1);
2782 curseg->alloc_type = SSR;
Chao Yu453e2ff2021-04-13 17:56:18 +08002783 curseg->next_blkoff = __next_free_blkoff(sbi, curseg->segno, 0);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002784
Chao Yu4d57b862018-05-30 00:20:41 +08002785 sum_page = f2fs_get_sum_page(sbi, new_segno);
Jaegeuk Kim86f33602020-10-02 14:17:35 -07002786 if (IS_ERR(sum_page)) {
2787 /* GC won't be able to use stale summary pages by cp_error */
2788 memset(curseg->sum_blk, 0, SUM_ENTRY_SIZE);
2789 return;
2790 }
Chao Yu025d63a2017-08-30 18:04:48 +08002791 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
2792 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
2793 f2fs_put_page(sum_page, 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002794}
2795
Chao Yu093749e2020-08-04 21:14:49 +08002796static int get_ssr_segment(struct f2fs_sb_info *sbi, int type,
2797 int alloc_mode, unsigned long long age);
2798
2799static void get_atssr_segment(struct f2fs_sb_info *sbi, int type,
2800 int target_type, int alloc_mode,
2801 unsigned long long age)
2802{
2803 struct curseg_info *curseg = CURSEG_I(sbi, type);
2804
2805 curseg->seg_type = target_type;
2806
2807 if (get_ssr_segment(sbi, type, alloc_mode, age)) {
2808 struct seg_entry *se = get_seg_entry(sbi, curseg->next_segno);
2809
2810 curseg->seg_type = se->type;
2811 change_curseg(sbi, type, true);
2812 } else {
2813 /* allocate cold segment by default */
2814 curseg->seg_type = CURSEG_COLD_DATA;
2815 new_curseg(sbi, type, true);
2816 }
2817 stat_inc_seg_type(sbi, curseg);
2818}
2819
2820static void __f2fs_init_atgc_curseg(struct f2fs_sb_info *sbi)
2821{
2822 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_ALL_DATA_ATGC);
2823
2824 if (!sbi->am.atgc_enabled)
2825 return;
2826
2827 down_read(&SM_I(sbi)->curseg_lock);
2828
2829 mutex_lock(&curseg->curseg_mutex);
2830 down_write(&SIT_I(sbi)->sentry_lock);
2831
2832 get_atssr_segment(sbi, CURSEG_ALL_DATA_ATGC, CURSEG_COLD_DATA, SSR, 0);
2833
2834 up_write(&SIT_I(sbi)->sentry_lock);
2835 mutex_unlock(&curseg->curseg_mutex);
2836
2837 up_read(&SM_I(sbi)->curseg_lock);
2838
2839}
2840void f2fs_init_inmem_curseg(struct f2fs_sb_info *sbi)
2841{
2842 __f2fs_init_atgc_curseg(sbi);
2843}
2844
2845static void __f2fs_save_inmem_curseg(struct f2fs_sb_info *sbi, int type)
Chao Yud0b9e422020-08-04 21:14:45 +08002846{
2847 struct curseg_info *curseg = CURSEG_I(sbi, type);
2848
2849 mutex_lock(&curseg->curseg_mutex);
2850 if (!curseg->inited)
2851 goto out;
2852
2853 if (get_valid_blocks(sbi, curseg->segno, false)) {
2854 write_sum_page(sbi, curseg->sum_blk,
2855 GET_SUM_BLOCK(sbi, curseg->segno));
2856 } else {
2857 mutex_lock(&DIRTY_I(sbi)->seglist_lock);
2858 __set_test_and_free(sbi, curseg->segno, true);
2859 mutex_unlock(&DIRTY_I(sbi)->seglist_lock);
2860 }
2861out:
2862 mutex_unlock(&curseg->curseg_mutex);
2863}
2864
Chao Yu093749e2020-08-04 21:14:49 +08002865void f2fs_save_inmem_curseg(struct f2fs_sb_info *sbi)
2866{
2867 __f2fs_save_inmem_curseg(sbi, CURSEG_COLD_DATA_PINNED);
2868
2869 if (sbi->am.atgc_enabled)
2870 __f2fs_save_inmem_curseg(sbi, CURSEG_ALL_DATA_ATGC);
2871}
2872
2873static void __f2fs_restore_inmem_curseg(struct f2fs_sb_info *sbi, int type)
Chao Yud0b9e422020-08-04 21:14:45 +08002874{
2875 struct curseg_info *curseg = CURSEG_I(sbi, type);
2876
2877 mutex_lock(&curseg->curseg_mutex);
2878 if (!curseg->inited)
2879 goto out;
2880 if (get_valid_blocks(sbi, curseg->segno, false))
2881 goto out;
2882
2883 mutex_lock(&DIRTY_I(sbi)->seglist_lock);
2884 __set_test_and_inuse(sbi, curseg->segno);
2885 mutex_unlock(&DIRTY_I(sbi)->seglist_lock);
2886out:
2887 mutex_unlock(&curseg->curseg_mutex);
2888}
2889
Chao Yu093749e2020-08-04 21:14:49 +08002890void f2fs_restore_inmem_curseg(struct f2fs_sb_info *sbi)
2891{
2892 __f2fs_restore_inmem_curseg(sbi, CURSEG_COLD_DATA_PINNED);
2893
2894 if (sbi->am.atgc_enabled)
2895 __f2fs_restore_inmem_curseg(sbi, CURSEG_ALL_DATA_ATGC);
2896}
2897
2898static int get_ssr_segment(struct f2fs_sb_info *sbi, int type,
2899 int alloc_mode, unsigned long long age)
Jaegeuk Kim43727522013-02-04 15:11:17 +09002900{
2901 struct curseg_info *curseg = CURSEG_I(sbi, type);
2902 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
Jaegeuk Kime066b832017-04-13 15:17:00 -07002903 unsigned segno = NULL_SEGNO;
Chao Yu093749e2020-08-04 21:14:49 +08002904 unsigned short seg_type = curseg->seg_type;
Chao Yud27c3d82017-02-24 18:46:00 +08002905 int i, cnt;
2906 bool reversed = false;
Jaegeuk Kimc192f7a2017-02-22 17:10:18 -08002907
Chao Yu093749e2020-08-04 21:14:49 +08002908 sanity_check_seg_type(sbi, seg_type);
2909
Chao Yu4d57b862018-05-30 00:20:41 +08002910 /* f2fs_need_SSR() already forces to do this */
Chao Yu093749e2020-08-04 21:14:49 +08002911 if (!v_ops->get_victim(sbi, &segno, BG_GC, seg_type, alloc_mode, age)) {
Jaegeuk Kime066b832017-04-13 15:17:00 -07002912 curseg->next_segno = segno;
Jaegeuk Kimc192f7a2017-02-22 17:10:18 -08002913 return 1;
Jaegeuk Kime066b832017-04-13 15:17:00 -07002914 }
Jaegeuk Kim43727522013-02-04 15:11:17 +09002915
Jaegeuk Kim70d625c2017-02-22 17:02:32 -08002916 /* For node segments, let's do SSR more intensively */
Chao Yu093749e2020-08-04 21:14:49 +08002917 if (IS_NODESEG(seg_type)) {
2918 if (seg_type >= CURSEG_WARM_NODE) {
Chao Yud27c3d82017-02-24 18:46:00 +08002919 reversed = true;
2920 i = CURSEG_COLD_NODE;
2921 } else {
2922 i = CURSEG_HOT_NODE;
2923 }
2924 cnt = NR_CURSEG_NODE_TYPE;
Jaegeuk Kim70d625c2017-02-22 17:02:32 -08002925 } else {
Chao Yu093749e2020-08-04 21:14:49 +08002926 if (seg_type >= CURSEG_WARM_DATA) {
Chao Yud27c3d82017-02-24 18:46:00 +08002927 reversed = true;
2928 i = CURSEG_COLD_DATA;
2929 } else {
2930 i = CURSEG_HOT_DATA;
2931 }
2932 cnt = NR_CURSEG_DATA_TYPE;
Jaegeuk Kim70d625c2017-02-22 17:02:32 -08002933 }
Jaegeuk Kim43727522013-02-04 15:11:17 +09002934
Chao Yud27c3d82017-02-24 18:46:00 +08002935 for (; cnt-- > 0; reversed ? i-- : i++) {
Chao Yu093749e2020-08-04 21:14:49 +08002936 if (i == seg_type)
Jaegeuk Kimc192f7a2017-02-22 17:10:18 -08002937 continue;
Chao Yu093749e2020-08-04 21:14:49 +08002938 if (!v_ops->get_victim(sbi, &segno, BG_GC, i, alloc_mode, age)) {
Jaegeuk Kime066b832017-04-13 15:17:00 -07002939 curseg->next_segno = segno;
Jaegeuk Kim43727522013-02-04 15:11:17 +09002940 return 1;
Jaegeuk Kime066b832017-04-13 15:17:00 -07002941 }
Jaegeuk Kimc192f7a2017-02-22 17:10:18 -08002942 }
Daniel Rosenberg43549942018-08-20 19:21:43 -07002943
2944 /* find valid_blocks=0 in dirty list */
2945 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
2946 segno = get_free_segment(sbi);
2947 if (segno != NULL_SEGNO) {
2948 curseg->next_segno = segno;
2949 return 1;
2950 }
2951 }
Jaegeuk Kim43727522013-02-04 15:11:17 +09002952 return 0;
2953}
2954
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002955/*
2956 * flush out current segment and replace it with new segment
2957 * This function should be returned with success, otherwise BUG
2958 */
2959static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
2960 int type, bool force)
2961{
Jaegeuk Kima7881892017-04-20 13:51:57 -07002962 struct curseg_info *curseg = CURSEG_I(sbi, type);
2963
Gu Zheng7b405272013-08-19 09:41:15 +08002964 if (force)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002965 new_curseg(sbi, type, true);
Jaegeuk Kim5b6c6be2017-02-14 19:32:51 -08002966 else if (!is_set_ckpt_flags(sbi, CP_CRC_RECOVERY_FLAG) &&
Chao Yu093749e2020-08-04 21:14:49 +08002967 curseg->seg_type == CURSEG_WARM_NODE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002968 new_curseg(sbi, type, false);
Chao Yu093749e2020-08-04 21:14:49 +08002969 else if (curseg->alloc_type == LFS &&
2970 is_next_segment_free(sbi, curseg, type) &&
Daniel Rosenberg43549942018-08-20 19:21:43 -07002971 likely(!is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
Jaegeuk Kima7881892017-04-20 13:51:57 -07002972 new_curseg(sbi, type, false);
Chao Yu093749e2020-08-04 21:14:49 +08002973 else if (f2fs_need_SSR(sbi) &&
2974 get_ssr_segment(sbi, type, SSR, 0))
2975 change_curseg(sbi, type, true);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002976 else
2977 new_curseg(sbi, type, false);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +09002978
Jaegeuk Kima7881892017-04-20 13:51:57 -07002979 stat_inc_seg_type(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002980}
2981
Chao Yu0ef81832020-06-18 14:36:22 +08002982void f2fs_allocate_segment_for_resize(struct f2fs_sb_info *sbi, int type,
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08002983 unsigned int start, unsigned int end)
2984{
2985 struct curseg_info *curseg = CURSEG_I(sbi, type);
2986 unsigned int segno;
2987
2988 down_read(&SM_I(sbi)->curseg_lock);
2989 mutex_lock(&curseg->curseg_mutex);
2990 down_write(&SIT_I(sbi)->sentry_lock);
2991
2992 segno = CURSEG_I(sbi, type)->segno;
2993 if (segno < start || segno > end)
2994 goto unlock;
2995
Chao Yu093749e2020-08-04 21:14:49 +08002996 if (f2fs_need_SSR(sbi) && get_ssr_segment(sbi, type, SSR, 0))
2997 change_curseg(sbi, type, true);
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08002998 else
2999 new_curseg(sbi, type, true);
3000
3001 stat_inc_seg_type(sbi, curseg);
3002
3003 locate_dirty_segment(sbi, segno);
3004unlock:
3005 up_write(&SIT_I(sbi)->sentry_lock);
3006
3007 if (segno != curseg->segno)
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003008 f2fs_notice(sbi, "For resize: curseg of type %d: %u ==> %u",
3009 type, segno, curseg->segno);
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08003010
3011 mutex_unlock(&curseg->curseg_mutex);
3012 up_read(&SM_I(sbi)->curseg_lock);
3013}
3014
Chao Yue1175f02021-03-05 17:56:01 +08003015static void __allocate_new_segment(struct f2fs_sb_info *sbi, int type,
Chao Yu509f1012021-04-21 09:54:55 +08003016 bool new_sec, bool force)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003017{
Chao Yu901d745f2020-06-22 17:38:48 +08003018 struct curseg_info *curseg = CURSEG_I(sbi, type);
Jaegeuk Kim6ae1be12016-11-11 12:31:40 -08003019 unsigned int old_segno;
Chao Yu901d745f2020-06-22 17:38:48 +08003020
Chao Yud0b9e422020-08-04 21:14:45 +08003021 if (!curseg->inited)
3022 goto alloc;
3023
Chao Yu509f1012021-04-21 09:54:55 +08003024 if (force || curseg->next_blkoff ||
Chao Yue1175f02021-03-05 17:56:01 +08003025 get_valid_blocks(sbi, curseg->segno, new_sec))
3026 goto alloc;
Chao Yu901d745f2020-06-22 17:38:48 +08003027
Chao Yu61461fc2021-03-24 11:18:28 +08003028 if (!get_ckpt_valid_blocks(sbi, curseg->segno, new_sec))
3029 return;
Chao Yud0b9e422020-08-04 21:14:45 +08003030alloc:
Chao Yu901d745f2020-06-22 17:38:48 +08003031 old_segno = curseg->segno;
3032 SIT_I(sbi)->s_ops->allocate_segment(sbi, type, true);
3033 locate_dirty_segment(sbi, old_segno);
3034}
3035
Chao Yu509f1012021-04-21 09:54:55 +08003036static void __allocate_new_section(struct f2fs_sb_info *sbi,
3037 int type, bool force)
Chao Yu901d745f2020-06-22 17:38:48 +08003038{
Chao Yu509f1012021-04-21 09:54:55 +08003039 __allocate_new_segment(sbi, type, true, force);
Chao Yue1175f02021-03-05 17:56:01 +08003040}
3041
Chao Yu509f1012021-04-21 09:54:55 +08003042void f2fs_allocate_new_section(struct f2fs_sb_info *sbi, int type, bool force)
Chao Yu901d745f2020-06-22 17:38:48 +08003043{
Chao Yu823d13e2021-03-26 09:46:22 +08003044 down_read(&SM_I(sbi)->curseg_lock);
Chao Yu901d745f2020-06-22 17:38:48 +08003045 down_write(&SIT_I(sbi)->sentry_lock);
Chao Yu509f1012021-04-21 09:54:55 +08003046 __allocate_new_section(sbi, type, force);
Chao Yu901d745f2020-06-22 17:38:48 +08003047 up_write(&SIT_I(sbi)->sentry_lock);
Chao Yu823d13e2021-03-26 09:46:22 +08003048 up_read(&SM_I(sbi)->curseg_lock);
Chao Yu901d745f2020-06-22 17:38:48 +08003049}
3050
3051void f2fs_allocate_new_segments(struct f2fs_sb_info *sbi)
3052{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003053 int i;
3054
Chao Yu823d13e2021-03-26 09:46:22 +08003055 down_read(&SM_I(sbi)->curseg_lock);
Chao Yu3d26fa62017-10-30 17:49:53 +08003056 down_write(&SIT_I(sbi)->sentry_lock);
Chao Yu901d745f2020-06-22 17:38:48 +08003057 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++)
Chao Yu509f1012021-04-21 09:54:55 +08003058 __allocate_new_segment(sbi, i, false, false);
Chao Yu3d26fa62017-10-30 17:49:53 +08003059 up_write(&SIT_I(sbi)->sentry_lock);
Chao Yu823d13e2021-03-26 09:46:22 +08003060 up_read(&SM_I(sbi)->curseg_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003061}
3062
3063static const struct segment_allocation default_salloc_ops = {
3064 .allocate_segment = allocate_segment_by_default,
3065};
3066
Chao Yu4d57b862018-05-30 00:20:41 +08003067bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi,
3068 struct cp_control *cpc)
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08003069{
3070 __u64 trim_start = cpc->trim_start;
3071 bool has_candidate = false;
3072
Chao Yu3d26fa62017-10-30 17:49:53 +08003073 down_write(&SIT_I(sbi)->sentry_lock);
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08003074 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++) {
3075 if (add_discard_addrs(sbi, cpc, true)) {
3076 has_candidate = true;
3077 break;
3078 }
3079 }
Chao Yu3d26fa62017-10-30 17:49:53 +08003080 up_write(&SIT_I(sbi)->sentry_lock);
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08003081
3082 cpc->trim_start = trim_start;
3083 return has_candidate;
3084}
3085
Chao Yu01f9cf62018-06-25 20:33:24 +08003086static unsigned int __issue_discard_cmd_range(struct f2fs_sb_info *sbi,
Jaegeuk Kim9a997182018-05-24 13:57:26 -07003087 struct discard_policy *dpolicy,
3088 unsigned int start, unsigned int end)
3089{
3090 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
3091 struct discard_cmd *prev_dc = NULL, *next_dc = NULL;
3092 struct rb_node **insert_p = NULL, *insert_parent = NULL;
3093 struct discard_cmd *dc;
3094 struct blk_plug plug;
3095 int issued;
Chao Yu01f9cf62018-06-25 20:33:24 +08003096 unsigned int trimmed = 0;
Jaegeuk Kim9a997182018-05-24 13:57:26 -07003097
3098next:
3099 issued = 0;
3100
3101 mutex_lock(&dcc->cmd_lock);
Chao Yu67fce702018-06-22 16:06:59 +08003102 if (unlikely(dcc->rbtree_check))
3103 f2fs_bug_on(sbi, !f2fs_check_rb_tree_consistence(sbi,
Chao Yu2e9b2bb2020-08-04 21:14:48 +08003104 &dcc->root, false));
Jaegeuk Kim9a997182018-05-24 13:57:26 -07003105
Chao Yu4d57b862018-05-30 00:20:41 +08003106 dc = (struct discard_cmd *)f2fs_lookup_rb_tree_ret(&dcc->root,
Jaegeuk Kim9a997182018-05-24 13:57:26 -07003107 NULL, start,
3108 (struct rb_entry **)&prev_dc,
3109 (struct rb_entry **)&next_dc,
Chao Yu4dada3f2018-10-04 11:18:30 +08003110 &insert_p, &insert_parent, true, NULL);
Jaegeuk Kim9a997182018-05-24 13:57:26 -07003111 if (!dc)
3112 dc = next_dc;
3113
3114 blk_start_plug(&plug);
3115
3116 while (dc && dc->lstart <= end) {
3117 struct rb_node *node;
Chao Yu6b9cb122018-08-08 10:14:55 +08003118 int err = 0;
Jaegeuk Kim9a997182018-05-24 13:57:26 -07003119
3120 if (dc->len < dpolicy->granularity)
3121 goto skip;
3122
3123 if (dc->state != D_PREP) {
3124 list_move_tail(&dc->list, &dcc->fstrim_list);
3125 goto skip;
3126 }
3127
Chao Yu6b9cb122018-08-08 10:14:55 +08003128 err = __submit_discard_cmd(sbi, dpolicy, dc, &issued);
Jaegeuk Kim9a997182018-05-24 13:57:26 -07003129
Chao Yu35ec7d52018-08-06 22:43:50 +08003130 if (issued >= dpolicy->max_requests) {
Jaegeuk Kim9a997182018-05-24 13:57:26 -07003131 start = dc->lstart + dc->len;
3132
Chao Yu6b9cb122018-08-08 10:14:55 +08003133 if (err)
3134 __remove_discard_cmd(sbi, dc);
3135
Jaegeuk Kim9a997182018-05-24 13:57:26 -07003136 blk_finish_plug(&plug);
3137 mutex_unlock(&dcc->cmd_lock);
Chao Yu01f9cf62018-06-25 20:33:24 +08003138 trimmed += __wait_all_discard_cmd(sbi, NULL);
Chao Yu5df7731f2020-02-17 17:45:44 +08003139 congestion_wait(BLK_RW_ASYNC, DEFAULT_IO_TIMEOUT);
Jaegeuk Kim9a997182018-05-24 13:57:26 -07003140 goto next;
3141 }
3142skip:
3143 node = rb_next(&dc->rb_node);
Chao Yu6b9cb122018-08-08 10:14:55 +08003144 if (err)
3145 __remove_discard_cmd(sbi, dc);
Jaegeuk Kim9a997182018-05-24 13:57:26 -07003146 dc = rb_entry_safe(node, struct discard_cmd, rb_node);
3147
3148 if (fatal_signal_pending(current))
3149 break;
3150 }
3151
3152 blk_finish_plug(&plug);
3153 mutex_unlock(&dcc->cmd_lock);
Chao Yu01f9cf62018-06-25 20:33:24 +08003154
3155 return trimmed;
Jaegeuk Kim9a997182018-05-24 13:57:26 -07003156}
3157
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003158int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range)
3159{
Jaegeuk Kimf7ef9b82015-02-09 12:02:44 -08003160 __u64 start = F2FS_BYTES_TO_BLK(range->start);
3161 __u64 end = start + F2FS_BYTES_TO_BLK(range->len) - 1;
Chao Yu377224c2018-04-09 10:25:23 +08003162 unsigned int start_segno, end_segno;
Chao Yu84126632017-10-04 09:08:32 +08003163 block_t start_block, end_block;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003164 struct cp_control cpc;
Chao Yu78997b52017-10-04 09:08:34 +08003165 struct discard_policy dpolicy;
Chao Yu0ea80512017-10-28 16:52:32 +08003166 unsigned long long trimmed = 0;
Chao Yuc34f42e2015-12-23 17:50:30 +08003167 int err = 0;
Chao Yub0332a02020-02-14 17:44:12 +08003168 bool need_align = f2fs_lfs_mode(sbi) && __is_large_section(sbi);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003169
Jaegeuk Kim836b5a62015-04-30 22:50:06 -07003170 if (start >= MAX_BLKADDR(sbi) || range->len < sbi->blocksize)
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003171 return -EINVAL;
3172
Chao Yu3f16ecd92018-08-08 17:36:29 +08003173 if (end < MAIN_BLKADDR(sbi))
3174 goto out;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003175
Yunlei Heed214a12016-09-01 10:14:39 +08003176 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003177 f2fs_warn(sbi, "Found FS corruption, run fsck to fix.");
Chao Yu10f966b2019-06-20 11:36:14 +08003178 return -EFSCORRUPTED;
Yunlei Heed214a12016-09-01 10:14:39 +08003179 }
3180
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003181 /* start/end segment number in main_area */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003182 start_segno = (start <= MAIN_BLKADDR(sbi)) ? 0 : GET_SEGNO(sbi, start);
3183 end_segno = (end >= MAX_BLKADDR(sbi)) ? MAIN_SEGS(sbi) - 1 :
3184 GET_SEGNO(sbi, end);
Yunlong Songad6672b2018-07-19 20:58:15 +08003185 if (need_align) {
3186 start_segno = rounddown(start_segno, sbi->segs_per_sec);
3187 end_segno = roundup(end_segno + 1, sbi->segs_per_sec) - 1;
3188 }
Chao Yu84126632017-10-04 09:08:32 +08003189
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003190 cpc.reason = CP_DISCARD;
Jaegeuk Kim836b5a62015-04-30 22:50:06 -07003191 cpc.trim_minlen = max_t(__u64, 1, F2FS_BYTES_TO_BLK(range->minlen));
Chao Yu377224c2018-04-09 10:25:23 +08003192 cpc.trim_start = start_segno;
3193 cpc.trim_end = end_segno;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003194
Chao Yu377224c2018-04-09 10:25:23 +08003195 if (sbi->discard_blks == 0)
3196 goto out;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07003197
Chao Yufb24fea2020-01-14 19:36:50 +08003198 down_write(&sbi->gc_lock);
Chao Yu4d57b862018-05-30 00:20:41 +08003199 err = f2fs_write_checkpoint(sbi, &cpc);
Chao Yufb24fea2020-01-14 19:36:50 +08003200 up_write(&sbi->gc_lock);
Chao Yu377224c2018-04-09 10:25:23 +08003201 if (err)
3202 goto out;
Chao Yu84126632017-10-04 09:08:32 +08003203
Jaegeuk Kime555da92018-05-31 10:20:48 -07003204 /*
3205 * We filed discard candidates, but actually we don't need to wait for
3206 * all of them, since they'll be issued in idle time along with runtime
3207 * discard option. User configuration looks like using runtime discard
3208 * or periodic fstrim instead of it.
3209 */
Chao Yu7d20c8a2018-09-04 03:52:17 +08003210 if (f2fs_realtime_discard_enable(sbi))
Jaegeuk Kim5a615492018-06-20 21:27:21 -07003211 goto out;
3212
3213 start_block = START_BLOCK(sbi, start_segno);
3214 end_block = START_BLOCK(sbi, end_segno + 1);
3215
3216 __init_discard_policy(sbi, &dpolicy, DPOLICY_FSTRIM, cpc.trim_minlen);
Chao Yu01f9cf62018-06-25 20:33:24 +08003217 trimmed = __issue_discard_cmd_range(sbi, &dpolicy,
3218 start_block, end_block);
Jaegeuk Kim5a615492018-06-20 21:27:21 -07003219
Chao Yu01f9cf62018-06-25 20:33:24 +08003220 trimmed += __wait_discard_cmd_range(sbi, &dpolicy,
Chao Yu0ea80512017-10-28 16:52:32 +08003221 start_block, end_block);
Chao Yu377224c2018-04-09 10:25:23 +08003222out:
Chao Yu6eae2692018-08-05 23:09:00 +08003223 if (!err)
3224 range->len = F2FS_BLK_TO_BYTES(trimmed);
Chao Yuc34f42e2015-12-23 17:50:30 +08003225 return err;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003226}
3227
Chao Yu093749e2020-08-04 21:14:49 +08003228static bool __has_curseg_space(struct f2fs_sb_info *sbi,
3229 struct curseg_info *curseg)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003230{
Aravind Rameshde881df2020-07-16 18:26:56 +05303231 return curseg->next_blkoff < f2fs_usable_blks_in_seg(sbi,
3232 curseg->segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003233}
3234
Chao Yu4d57b862018-05-30 00:20:41 +08003235int f2fs_rw_hint_to_seg_type(enum rw_hint hint)
Hyunchul Lee4f0a03d32017-11-09 14:51:27 +09003236{
3237 switch (hint) {
3238 case WRITE_LIFE_SHORT:
3239 return CURSEG_HOT_DATA;
3240 case WRITE_LIFE_EXTREME:
3241 return CURSEG_COLD_DATA;
3242 default:
3243 return CURSEG_WARM_DATA;
3244 }
3245}
3246
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09003247/* This returns write hints for each segment type. This hints will be
3248 * passed down to block layer. There are mapping tables which depend on
3249 * the mount option 'whint_mode'.
3250 *
3251 * 1) whint_mode=off. F2FS only passes down WRITE_LIFE_NOT_SET.
3252 *
3253 * 2) whint_mode=user-based. F2FS tries to pass down hints given by users.
3254 *
3255 * User F2FS Block
3256 * ---- ---- -----
3257 * META WRITE_LIFE_NOT_SET
3258 * HOT_NODE "
3259 * WARM_NODE "
3260 * COLD_NODE "
3261 * ioctl(COLD) COLD_DATA WRITE_LIFE_EXTREME
3262 * extension list " "
3263 *
3264 * -- buffered io
3265 * WRITE_LIFE_EXTREME COLD_DATA WRITE_LIFE_EXTREME
3266 * WRITE_LIFE_SHORT HOT_DATA WRITE_LIFE_SHORT
3267 * WRITE_LIFE_NOT_SET WARM_DATA WRITE_LIFE_NOT_SET
3268 * WRITE_LIFE_NONE " "
3269 * WRITE_LIFE_MEDIUM " "
3270 * WRITE_LIFE_LONG " "
3271 *
3272 * -- direct io
3273 * WRITE_LIFE_EXTREME COLD_DATA WRITE_LIFE_EXTREME
3274 * WRITE_LIFE_SHORT HOT_DATA WRITE_LIFE_SHORT
3275 * WRITE_LIFE_NOT_SET WARM_DATA WRITE_LIFE_NOT_SET
3276 * WRITE_LIFE_NONE " WRITE_LIFE_NONE
3277 * WRITE_LIFE_MEDIUM " WRITE_LIFE_MEDIUM
3278 * WRITE_LIFE_LONG " WRITE_LIFE_LONG
3279 *
Hyunchul Leef2e703f2018-01-31 11:36:58 +09003280 * 3) whint_mode=fs-based. F2FS passes down hints with its policy.
3281 *
3282 * User F2FS Block
3283 * ---- ---- -----
3284 * META WRITE_LIFE_MEDIUM;
3285 * HOT_NODE WRITE_LIFE_NOT_SET
3286 * WARM_NODE "
3287 * COLD_NODE WRITE_LIFE_NONE
3288 * ioctl(COLD) COLD_DATA WRITE_LIFE_EXTREME
3289 * extension list " "
3290 *
3291 * -- buffered io
3292 * WRITE_LIFE_EXTREME COLD_DATA WRITE_LIFE_EXTREME
3293 * WRITE_LIFE_SHORT HOT_DATA WRITE_LIFE_SHORT
3294 * WRITE_LIFE_NOT_SET WARM_DATA WRITE_LIFE_LONG
3295 * WRITE_LIFE_NONE " "
3296 * WRITE_LIFE_MEDIUM " "
3297 * WRITE_LIFE_LONG " "
3298 *
3299 * -- direct io
3300 * WRITE_LIFE_EXTREME COLD_DATA WRITE_LIFE_EXTREME
3301 * WRITE_LIFE_SHORT HOT_DATA WRITE_LIFE_SHORT
3302 * WRITE_LIFE_NOT_SET WARM_DATA WRITE_LIFE_NOT_SET
3303 * WRITE_LIFE_NONE " WRITE_LIFE_NONE
3304 * WRITE_LIFE_MEDIUM " WRITE_LIFE_MEDIUM
3305 * WRITE_LIFE_LONG " WRITE_LIFE_LONG
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09003306 */
3307
Chao Yu4d57b862018-05-30 00:20:41 +08003308enum rw_hint f2fs_io_type_to_rw_hint(struct f2fs_sb_info *sbi,
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09003309 enum page_type type, enum temp_type temp)
3310{
Chao Yu63189b72018-03-08 14:22:56 +08003311 if (F2FS_OPTION(sbi).whint_mode == WHINT_MODE_USER) {
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09003312 if (type == DATA) {
Hyunchul Leef2e703f2018-01-31 11:36:58 +09003313 if (temp == WARM)
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09003314 return WRITE_LIFE_NOT_SET;
Hyunchul Leef2e703f2018-01-31 11:36:58 +09003315 else if (temp == HOT)
3316 return WRITE_LIFE_SHORT;
3317 else if (temp == COLD)
3318 return WRITE_LIFE_EXTREME;
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09003319 } else {
3320 return WRITE_LIFE_NOT_SET;
3321 }
Chao Yu63189b72018-03-08 14:22:56 +08003322 } else if (F2FS_OPTION(sbi).whint_mode == WHINT_MODE_FS) {
Hyunchul Leef2e703f2018-01-31 11:36:58 +09003323 if (type == DATA) {
3324 if (temp == WARM)
3325 return WRITE_LIFE_LONG;
3326 else if (temp == HOT)
3327 return WRITE_LIFE_SHORT;
3328 else if (temp == COLD)
3329 return WRITE_LIFE_EXTREME;
3330 } else if (type == NODE) {
3331 if (temp == WARM || temp == HOT)
3332 return WRITE_LIFE_NOT_SET;
3333 else if (temp == COLD)
3334 return WRITE_LIFE_NONE;
3335 } else if (type == META) {
3336 return WRITE_LIFE_MEDIUM;
3337 }
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09003338 }
Hyunchul Leef2e703f2018-01-31 11:36:58 +09003339 return WRITE_LIFE_NOT_SET;
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09003340}
3341
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07003342static int __get_segment_type_2(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003343{
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07003344 if (fio->type == DATA)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003345 return CURSEG_HOT_DATA;
3346 else
3347 return CURSEG_HOT_NODE;
3348}
3349
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07003350static int __get_segment_type_4(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003351{
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07003352 if (fio->type == DATA) {
3353 struct inode *inode = fio->page->mapping->host;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003354
3355 if (S_ISDIR(inode->i_mode))
3356 return CURSEG_HOT_DATA;
3357 else
3358 return CURSEG_COLD_DATA;
3359 } else {
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07003360 if (IS_DNODE(fio->page) && is_cold_node(fio->page))
Jaegeuk Kima344b9f2014-11-05 20:05:53 -08003361 return CURSEG_WARM_NODE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003362 else
3363 return CURSEG_COLD_NODE;
3364 }
3365}
3366
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07003367static int __get_segment_type_6(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003368{
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07003369 if (fio->type == DATA) {
3370 struct inode *inode = fio->page->mapping->host;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003371
Chao Yu859fca62021-05-26 14:29:27 +08003372 if (is_inode_flag_set(inode, FI_ALIGNED_WRITE))
3373 return CURSEG_COLD_DATA_PINNED;
3374
Chao Yub763f3b2021-04-28 17:20:31 +08003375 if (page_private_gcing(fio->page)) {
Weichao Guoac2d7502021-03-17 17:27:23 +08003376 if (fio->sbi->am.atgc_enabled &&
3377 (fio->io_type == FS_DATA_IO) &&
3378 (fio->sbi->gc_mode != GC_URGENT_HIGH))
Chao Yu093749e2020-08-04 21:14:49 +08003379 return CURSEG_ALL_DATA_ATGC;
3380 else
3381 return CURSEG_COLD_DATA;
3382 }
Daeho Jeong602a16d2020-12-01 13:08:02 +09003383 if (file_is_cold(inode) || f2fs_need_compress_data(inode))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003384 return CURSEG_COLD_DATA;
Chao Yub6a06cb2018-02-28 17:07:27 +08003385 if (file_is_hot(inode) ||
Chao Yub4c3ca82018-04-26 17:05:50 +08003386 is_inode_flag_set(inode, FI_HOT_DATA) ||
Chao Yu2079f112018-07-17 20:41:48 +08003387 f2fs_is_atomic_file(inode) ||
3388 f2fs_is_volatile_file(inode))
Jaegeuk Kimef095d12017-03-24 20:05:13 -04003389 return CURSEG_HOT_DATA;
Chao Yu4d57b862018-05-30 00:20:41 +08003390 return f2fs_rw_hint_to_seg_type(inode->i_write_hint);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003391 } else {
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07003392 if (IS_DNODE(fio->page))
3393 return is_cold_node(fio->page) ? CURSEG_WARM_NODE :
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003394 CURSEG_HOT_NODE;
Jaegeuk Kimef095d12017-03-24 20:05:13 -04003395 return CURSEG_COLD_NODE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003396 }
3397}
3398
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07003399static int __get_segment_type(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003400{
Jaegeuk Kima912b542017-05-10 11:18:25 -07003401 int type = 0;
3402
Chao Yu63189b72018-03-08 14:22:56 +08003403 switch (F2FS_OPTION(fio->sbi).active_logs) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003404 case 2:
Jaegeuk Kima912b542017-05-10 11:18:25 -07003405 type = __get_segment_type_2(fio);
3406 break;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003407 case 4:
Jaegeuk Kima912b542017-05-10 11:18:25 -07003408 type = __get_segment_type_4(fio);
3409 break;
3410 case 6:
3411 type = __get_segment_type_6(fio);
3412 break;
3413 default:
3414 f2fs_bug_on(fio->sbi, true);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003415 }
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07003416
Jaegeuk Kima912b542017-05-10 11:18:25 -07003417 if (IS_HOT(type))
3418 fio->temp = HOT;
3419 else if (IS_WARM(type))
3420 fio->temp = WARM;
3421 else
3422 fio->temp = COLD;
3423 return type;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003424}
3425
Chao Yu4d57b862018-05-30 00:20:41 +08003426void f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09003427 block_t old_blkaddr, block_t *new_blkaddr,
Chao Yufb830fc2017-05-19 23:37:01 +08003428 struct f2fs_summary *sum, int type,
Chao Yu093749e2020-08-04 21:14:49 +08003429 struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003430{
3431 struct sit_info *sit_i = SIT_I(sbi);
Jaegeuk Kim6ae1be12016-11-11 12:31:40 -08003432 struct curseg_info *curseg = CURSEG_I(sbi, type);
Chao Yuc5d02782020-08-04 21:14:47 +08003433 unsigned long long old_mtime;
Chao Yu093749e2020-08-04 21:14:49 +08003434 bool from_gc = (type == CURSEG_ALL_DATA_ATGC);
3435 struct seg_entry *se = NULL;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003436
Chao Yu2b603112017-11-02 20:41:03 +08003437 down_read(&SM_I(sbi)->curseg_lock);
3438
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003439 mutex_lock(&curseg->curseg_mutex);
Chao Yu3d26fa62017-10-30 17:49:53 +08003440 down_write(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003441
Chao Yu093749e2020-08-04 21:14:49 +08003442 if (from_gc) {
3443 f2fs_bug_on(sbi, GET_SEGNO(sbi, old_blkaddr) == NULL_SEGNO);
3444 se = get_seg_entry(sbi, GET_SEGNO(sbi, old_blkaddr));
3445 sanity_check_seg_type(sbi, se->type);
3446 f2fs_bug_on(sbi, IS_NODESEG(se->type));
3447 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003448 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003449
Chao Yu093749e2020-08-04 21:14:49 +08003450 f2fs_bug_on(sbi, curseg->next_blkoff >= sbi->blocks_per_seg);
3451
Jaegeuk Kim4e6a8d92016-12-29 14:07:53 -08003452 f2fs_wait_discard_bio(sbi, *new_blkaddr);
3453
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003454 /*
3455 * __add_sum_entry should be resided under the curseg_mutex
3456 * because, this function updates a summary entry in the
3457 * current summary block.
3458 */
Haicheng Lie79efe32013-06-13 16:59:27 +08003459 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003460
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003461 __refresh_next_blkoff(sbi, curseg);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +09003462
3463 stat_inc_block_count(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003464
Chao Yuc5d02782020-08-04 21:14:47 +08003465 if (from_gc) {
3466 old_mtime = get_segment_mtime(sbi, old_blkaddr);
3467 } else {
3468 update_segment_mtime(sbi, old_blkaddr, 0);
3469 old_mtime = 0;
3470 }
3471 update_segment_mtime(sbi, *new_blkaddr, old_mtime);
3472
Yunlong Song65f1b802017-10-30 09:33:41 +08003473 /*
3474 * SIT information should be updated before segment allocation,
3475 * since SSR needs latest valid block information.
3476 */
3477 update_sit_entry(sbi, *new_blkaddr, 1);
3478 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
3479 update_sit_entry(sbi, old_blkaddr, -1);
3480
Chao Yu093749e2020-08-04 21:14:49 +08003481 if (!__has_curseg_space(sbi, curseg)) {
3482 if (from_gc)
3483 get_atssr_segment(sbi, type, se->type,
3484 AT_SSR, se->mtime);
3485 else
3486 sit_i->s_ops->allocate_segment(sbi, type, false);
3487 }
Jaegeuk Kimc6f82fe92017-04-04 16:45:30 -07003488 /*
Yunlong Song65f1b802017-10-30 09:33:41 +08003489 * segment dirty status should be updated after segment allocation,
3490 * so we just need to update status only one time after previous
3491 * segment being closed.
Jaegeuk Kimc6f82fe92017-04-04 16:45:30 -07003492 */
Yunlong Song65f1b802017-10-30 09:33:41 +08003493 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
3494 locate_dirty_segment(sbi, GET_SEGNO(sbi, *new_blkaddr));
Yunlong Song3436c4b2017-02-21 16:59:26 +08003495
Chao Yu3d26fa62017-10-30 17:49:53 +08003496 up_write(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003497
Chao Yu704956e2017-07-31 20:19:09 +08003498 if (page && IS_NODESEG(type)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003499 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
3500
Chao Yu704956e2017-07-31 20:19:09 +08003501 f2fs_inode_chksum_set(sbi, page);
3502 }
3503
Chao Yuf608c382020-06-18 14:36:24 +08003504 if (fio) {
Chao Yufb830fc2017-05-19 23:37:01 +08003505 struct f2fs_bio_info *io;
3506
Chao Yu25ae8372021-04-02 17:22:23 +08003507 if (F2FS_IO_ALIGNED(sbi))
3508 fio->retry = false;
3509
Chao Yufb830fc2017-05-19 23:37:01 +08003510 INIT_LIST_HEAD(&fio->list);
3511 fio->in_list = true;
3512 io = sbi->write_io[fio->type] + fio->temp;
3513 spin_lock(&io->io_lock);
3514 list_add_tail(&fio->list, &io->io_list);
3515 spin_unlock(&io->io_lock);
3516 }
3517
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09003518 mutex_unlock(&curseg->curseg_mutex);
Chao Yu2b603112017-11-02 20:41:03 +08003519
3520 up_read(&SM_I(sbi)->curseg_lock);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09003521}
3522
Chao Yu39d787b2017-09-29 13:59:38 +08003523static void update_device_state(struct f2fs_io_info *fio)
3524{
3525 struct f2fs_sb_info *sbi = fio->sbi;
3526 unsigned int devidx;
3527
Damien Le Moal09168782019-03-16 09:13:06 +09003528 if (!f2fs_is_multi_device(sbi))
Chao Yu39d787b2017-09-29 13:59:38 +08003529 return;
3530
3531 devidx = f2fs_target_device_index(sbi, fio->new_blkaddr);
3532
3533 /* update device state for fsync */
Chao Yu4d57b862018-05-30 00:20:41 +08003534 f2fs_set_dirty_device(sbi, fio->ino, devidx, FLUSH_INO);
Chao Yu1228b482017-09-29 13:59:39 +08003535
3536 /* update device state for checkpoint */
3537 if (!f2fs_test_bit(devidx, (char *)&sbi->dirty_device)) {
3538 spin_lock(&sbi->dev_lock);
3539 f2fs_set_bit(devidx, (char *)&sbi->dirty_device);
3540 spin_unlock(&sbi->dev_lock);
3541 }
Chao Yu39d787b2017-09-29 13:59:38 +08003542}
3543
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07003544static void do_write_page(struct f2fs_summary *sum, struct f2fs_io_info *fio)
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09003545{
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07003546 int type = __get_segment_type(fio);
Chao Yub0332a02020-02-14 17:44:12 +08003547 bool keep_order = (f2fs_lfs_mode(fio->sbi) && type == CURSEG_COLD_DATA);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09003548
Chao Yu107a8052018-05-26 09:00:13 +08003549 if (keep_order)
3550 down_read(&fio->sbi->io_order_lock);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08003551reallocate:
Chao Yu4d57b862018-05-30 00:20:41 +08003552 f2fs_allocate_data_block(fio->sbi, fio->page, fio->old_blkaddr,
Chao Yu093749e2020-08-04 21:14:49 +08003553 &fio->new_blkaddr, sum, type, fio);
Chao Yu6ce19af2021-05-20 19:51:50 +08003554 if (GET_SEGNO(fio->sbi, fio->old_blkaddr) != NULL_SEGNO) {
Chao Yu6aa58d82018-08-14 22:37:25 +08003555 invalidate_mapping_pages(META_MAPPING(fio->sbi),
3556 fio->old_blkaddr, fio->old_blkaddr);
Chao Yu6ce19af2021-05-20 19:51:50 +08003557 f2fs_invalidate_compress_page(fio->sbi, fio->old_blkaddr);
3558 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09003559
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003560 /* writeout dirty page into bdev */
Chao Yufe16efe2018-05-28 23:47:18 +08003561 f2fs_submit_page_write(fio);
3562 if (fio->retry) {
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08003563 fio->old_blkaddr = fio->new_blkaddr;
3564 goto reallocate;
3565 }
Chao Yufe16efe2018-05-28 23:47:18 +08003566
3567 update_device_state(fio);
3568
Chao Yu107a8052018-05-26 09:00:13 +08003569 if (keep_order)
3570 up_read(&fio->sbi->io_order_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003571}
3572
Chao Yu4d57b862018-05-30 00:20:41 +08003573void f2fs_do_write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
Chao Yub0af6d42017-08-02 23:21:48 +08003574 enum iostat_type io_type)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003575{
Jaegeuk Kim458e6192013-12-11 13:54:01 +09003576 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07003577 .sbi = sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09003578 .type = META,
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09003579 .temp = HOT,
Mike Christie04d328d2016-06-05 14:31:55 -05003580 .op = REQ_OP_WRITE,
Christoph Hellwig70fd7612016-11-01 07:40:10 -06003581 .op_flags = REQ_SYNC | REQ_META | REQ_PRIO,
Chao Yu7a9d7542016-02-22 18:36:38 +08003582 .old_blkaddr = page->index,
3583 .new_blkaddr = page->index,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07003584 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07003585 .encrypted_page = NULL,
Chao Yufb830fc2017-05-19 23:37:01 +08003586 .in_list = false,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09003587 };
3588
Chao Yu2b947002015-10-12 17:04:21 +08003589 if (unlikely(page->index >= MAIN_BLKADDR(sbi)))
Mike Christie04d328d2016-06-05 14:31:55 -05003590 fio.op_flags &= ~REQ_META;
Chao Yu2b947002015-10-12 17:04:21 +08003591
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003592 set_page_writeback(page);
Jaegeuk Kim17c50032018-04-11 23:09:04 -07003593 ClearPageError(page);
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07003594 f2fs_submit_page_write(&fio);
Chao Yub0af6d42017-08-02 23:21:48 +08003595
Chao Yub63e7be2018-09-29 18:31:27 +08003596 stat_inc_meta_count(sbi, page->index);
Chao Yub0af6d42017-08-02 23:21:48 +08003597 f2fs_update_iostat(sbi, io_type, F2FS_BLKSIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003598}
3599
Chao Yu4d57b862018-05-30 00:20:41 +08003600void f2fs_do_write_node_page(unsigned int nid, struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003601{
3602 struct f2fs_summary sum;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07003603
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003604 set_summary(&sum, nid, 0, 0);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07003605 do_write_page(&sum, fio);
Chao Yub0af6d42017-08-02 23:21:48 +08003606
3607 f2fs_update_iostat(fio->sbi, fio->io_type, F2FS_BLKSIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003608}
3609
Chao Yu4d57b862018-05-30 00:20:41 +08003610void f2fs_outplace_write_data(struct dnode_of_data *dn,
3611 struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003612{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07003613 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003614 struct f2fs_summary sum;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003615
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07003616 f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR);
Chao Yu77357302018-07-17 00:02:17 +08003617 set_summary(&sum, dn->nid, dn->ofs_in_node, fio->version);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07003618 do_write_page(&sum, fio);
Chao Yuf28b3432016-02-24 17:16:47 +08003619 f2fs_update_data_blkaddr(dn, fio->new_blkaddr);
Chao Yub0af6d42017-08-02 23:21:48 +08003620
3621 f2fs_update_iostat(sbi, fio->io_type, F2FS_BLKSIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003622}
3623
Chao Yu4d57b862018-05-30 00:20:41 +08003624int f2fs_inplace_write_data(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003625{
Chao Yub0af6d42017-08-02 23:21:48 +08003626 int err;
Yunlei Hed21b0f22018-03-26 17:32:23 +08003627 struct f2fs_sb_info *sbi = fio->sbi;
Chao Yu05573d62019-04-15 15:30:52 +08003628 unsigned int segno;
Chao Yub0af6d42017-08-02 23:21:48 +08003629
Chao Yu7a9d7542016-02-22 18:36:38 +08003630 fio->new_blkaddr = fio->old_blkaddr;
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09003631 /* i/o temperature is needed for passing down write hints */
3632 __get_segment_type(fio);
Yunlei Hed21b0f22018-03-26 17:32:23 +08003633
Chao Yu05573d62019-04-15 15:30:52 +08003634 segno = GET_SEGNO(sbi, fio->new_blkaddr);
3635
3636 if (!IS_DATASEG(get_seg_entry(sbi, segno)->type)) {
3637 set_sbi_flag(sbi, SBI_NEED_FSCK);
Chao Yu2d821c12019-06-18 17:59:03 +08003638 f2fs_warn(sbi, "%s: incorrect segment(%u) type, run fsck to fix.",
3639 __func__, segno);
Chao Yu95577272021-04-22 18:19:25 +08003640 err = -EFSCORRUPTED;
3641 goto drop_bio;
3642 }
3643
Jaegeuk Kim1ffc8f52021-07-14 16:14:02 -07003644 if (f2fs_cp_error(sbi)) {
Chao Yu95577272021-04-22 18:19:25 +08003645 err = -EIO;
3646 goto drop_bio;
Chao Yu05573d62019-04-15 15:30:52 +08003647 }
Yunlei Hed21b0f22018-03-26 17:32:23 +08003648
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07003649 stat_inc_inplace_blocks(fio->sbi);
Chao Yub0af6d42017-08-02 23:21:48 +08003650
Jaegeuk Kim0e7f41972020-01-06 16:43:09 -08003651 if (fio->bio && !(SM_I(sbi)->ipu_policy & (1 << F2FS_IPU_NOCACHE)))
Chao Yu8648de22019-02-19 16:15:29 +08003652 err = f2fs_merge_page_bio(fio);
3653 else
3654 err = f2fs_submit_page_bio(fio);
Chao Yue46f6bd2019-02-21 20:40:13 +08003655 if (!err) {
Chao Yu39d787b2017-09-29 13:59:38 +08003656 update_device_state(fio);
Chao Yue46f6bd2019-02-21 20:40:13 +08003657 f2fs_update_iostat(fio->sbi, fio->io_type, F2FS_BLKSIZE);
3658 }
Chao Yub0af6d42017-08-02 23:21:48 +08003659
3660 return err;
Chao Yu95577272021-04-22 18:19:25 +08003661drop_bio:
Jaegeuk Kim349c4d62021-05-09 21:53:03 -07003662 if (fio->bio && *(fio->bio)) {
Chao Yu95577272021-04-22 18:19:25 +08003663 struct bio *bio = *(fio->bio);
3664
3665 bio->bi_status = BLK_STS_IOERR;
3666 bio_endio(bio);
Jaegeuk Kim349c4d62021-05-09 21:53:03 -07003667 *(fio->bio) = NULL;
Chao Yu95577272021-04-22 18:19:25 +08003668 }
3669 return err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003670}
3671
Chao Yu2b603112017-11-02 20:41:03 +08003672static inline int __f2fs_get_curseg(struct f2fs_sb_info *sbi,
3673 unsigned int segno)
3674{
3675 int i;
3676
3677 for (i = CURSEG_HOT_DATA; i < NO_CHECK_TYPE; i++) {
3678 if (CURSEG_I(sbi, i)->segno == segno)
3679 break;
3680 }
3681 return i;
3682}
3683
Chao Yu4d57b862018-05-30 00:20:41 +08003684void f2fs_do_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
Chao Yu19f106b2015-05-06 13:08:06 +08003685 block_t old_blkaddr, block_t new_blkaddr,
Chao Yuc5d02782020-08-04 21:14:47 +08003686 bool recover_curseg, bool recover_newaddr,
3687 bool from_gc)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003688{
3689 struct sit_info *sit_i = SIT_I(sbi);
3690 struct curseg_info *curseg;
3691 unsigned int segno, old_cursegno;
3692 struct seg_entry *se;
3693 int type;
Chao Yu19f106b2015-05-06 13:08:06 +08003694 unsigned short old_blkoff;
Wang Xiaojun753a8ed2021-03-25 10:19:20 -04003695 unsigned char old_alloc_type;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003696
3697 segno = GET_SEGNO(sbi, new_blkaddr);
3698 se = get_seg_entry(sbi, segno);
3699 type = se->type;
3700
Chao Yu2b603112017-11-02 20:41:03 +08003701 down_write(&SM_I(sbi)->curseg_lock);
3702
Chao Yu19f106b2015-05-06 13:08:06 +08003703 if (!recover_curseg) {
3704 /* for recovery flow */
3705 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
3706 if (old_blkaddr == NULL_ADDR)
3707 type = CURSEG_COLD_DATA;
3708 else
3709 type = CURSEG_WARM_DATA;
3710 }
3711 } else {
Chao Yu2b603112017-11-02 20:41:03 +08003712 if (IS_CURSEG(sbi, segno)) {
3713 /* se->type is volatile as SSR allocation */
3714 type = __f2fs_get_curseg(sbi, segno);
3715 f2fs_bug_on(sbi, type == NO_CHECK_TYPE);
3716 } else {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003717 type = CURSEG_WARM_DATA;
Chao Yu2b603112017-11-02 20:41:03 +08003718 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003719 }
Chao Yu19f106b2015-05-06 13:08:06 +08003720
Yunlong Song2c190502018-01-04 15:02:02 +08003721 f2fs_bug_on(sbi, !IS_DATASEG(type));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003722 curseg = CURSEG_I(sbi, type);
3723
3724 mutex_lock(&curseg->curseg_mutex);
Chao Yu3d26fa62017-10-30 17:49:53 +08003725 down_write(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003726
3727 old_cursegno = curseg->segno;
Chao Yu19f106b2015-05-06 13:08:06 +08003728 old_blkoff = curseg->next_blkoff;
Wang Xiaojun753a8ed2021-03-25 10:19:20 -04003729 old_alloc_type = curseg->alloc_type;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003730
3731 /* change the current segment */
3732 if (segno != curseg->segno) {
3733 curseg->next_segno = segno;
Chao Yu093749e2020-08-04 21:14:49 +08003734 change_curseg(sbi, type, true);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003735 }
3736
Jaegeuk Kim491c0852014-02-04 13:01:10 +09003737 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
Haicheng Lie79efe32013-06-13 16:59:27 +08003738 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003739
Chao Yuc5d02782020-08-04 21:14:47 +08003740 if (!recover_curseg || recover_newaddr) {
3741 if (!from_gc)
3742 update_segment_mtime(sbi, new_blkaddr, 0);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07003743 update_sit_entry(sbi, new_blkaddr, 1);
Chao Yuc5d02782020-08-04 21:14:47 +08003744 }
Chao Yu6aa58d82018-08-14 22:37:25 +08003745 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO) {
3746 invalidate_mapping_pages(META_MAPPING(sbi),
3747 old_blkaddr, old_blkaddr);
Chao Yu6ce19af2021-05-20 19:51:50 +08003748 f2fs_invalidate_compress_page(sbi, old_blkaddr);
Chao Yuc5d02782020-08-04 21:14:47 +08003749 if (!from_gc)
3750 update_segment_mtime(sbi, old_blkaddr, 0);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07003751 update_sit_entry(sbi, old_blkaddr, -1);
Chao Yu6aa58d82018-08-14 22:37:25 +08003752 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07003753
3754 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
3755 locate_dirty_segment(sbi, GET_SEGNO(sbi, new_blkaddr));
3756
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003757 locate_dirty_segment(sbi, old_cursegno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003758
Chao Yu19f106b2015-05-06 13:08:06 +08003759 if (recover_curseg) {
3760 if (old_cursegno != curseg->segno) {
3761 curseg->next_segno = old_cursegno;
Chao Yu093749e2020-08-04 21:14:49 +08003762 change_curseg(sbi, type, true);
Chao Yu19f106b2015-05-06 13:08:06 +08003763 }
3764 curseg->next_blkoff = old_blkoff;
Wang Xiaojun753a8ed2021-03-25 10:19:20 -04003765 curseg->alloc_type = old_alloc_type;
Chao Yu19f106b2015-05-06 13:08:06 +08003766 }
3767
Chao Yu3d26fa62017-10-30 17:49:53 +08003768 up_write(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003769 mutex_unlock(&curseg->curseg_mutex);
Chao Yu2b603112017-11-02 20:41:03 +08003770 up_write(&SM_I(sbi)->curseg_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003771}
3772
Chao Yu528e3452015-05-28 19:15:35 +08003773void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
3774 block_t old_addr, block_t new_addr,
Chao Yu28bc1062016-02-06 14:40:34 +08003775 unsigned char version, bool recover_curseg,
3776 bool recover_newaddr)
Chao Yu528e3452015-05-28 19:15:35 +08003777{
3778 struct f2fs_summary sum;
3779
3780 set_summary(&sum, dn->nid, dn->ofs_in_node, version);
3781
Chao Yu4d57b862018-05-30 00:20:41 +08003782 f2fs_do_replace_block(sbi, &sum, old_addr, new_addr,
Chao Yuc5d02782020-08-04 21:14:47 +08003783 recover_curseg, recover_newaddr, false);
Chao Yu528e3452015-05-28 19:15:35 +08003784
Chao Yuf28b3432016-02-24 17:16:47 +08003785 f2fs_update_data_blkaddr(dn, new_addr);
Chao Yu528e3452015-05-28 19:15:35 +08003786}
3787
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09003788void f2fs_wait_on_page_writeback(struct page *page,
Chao Yubae0ee72018-12-25 17:43:42 +08003789 enum page_type type, bool ordered, bool locked)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09003790{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09003791 if (PageWriteback(page)) {
Jaegeuk Kim40813632014-09-02 15:31:18 -07003792 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
3793
Chao Yu0b20fce2019-09-30 18:53:25 +08003794 /* submit cached LFS IO */
Chao Yubab475c2018-09-27 23:41:16 +08003795 f2fs_submit_merged_write_cond(sbi, NULL, page, 0, type);
Chao Yu0b20fce2019-09-30 18:53:25 +08003796 /* sbumit cached IPU IO */
3797 f2fs_submit_merged_ipu_write(sbi, NULL, page);
Chao Yubae0ee72018-12-25 17:43:42 +08003798 if (ordered) {
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08003799 wait_on_page_writeback(page);
Chao Yubae0ee72018-12-25 17:43:42 +08003800 f2fs_bug_on(sbi, locked && PageWriteback(page));
3801 } else {
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08003802 wait_for_stable_page(page);
Chao Yubae0ee72018-12-25 17:43:42 +08003803 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09003804 }
3805}
3806
Jaegeuk Kim0ded69f2018-08-22 21:18:00 -07003807void f2fs_wait_on_block_writeback(struct inode *inode, block_t blkaddr)
Chao Yu08b39fb2015-10-08 13:27:34 +08003808{
Jaegeuk Kim0ded69f2018-08-22 21:18:00 -07003809 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu08b39fb2015-10-08 13:27:34 +08003810 struct page *cpage;
3811
Jaegeuk Kim0ded69f2018-08-22 21:18:00 -07003812 if (!f2fs_post_read_required(inode))
3813 return;
3814
Chao Yu93770ab2019-04-15 15:26:32 +08003815 if (!__is_valid_data_blkaddr(blkaddr))
Chao Yu08b39fb2015-10-08 13:27:34 +08003816 return;
3817
Chao Yu08b39fb2015-10-08 13:27:34 +08003818 cpage = find_lock_page(META_MAPPING(sbi), blkaddr);
3819 if (cpage) {
Chao Yubae0ee72018-12-25 17:43:42 +08003820 f2fs_wait_on_page_writeback(cpage, DATA, true, true);
Chao Yu08b39fb2015-10-08 13:27:34 +08003821 f2fs_put_page(cpage, 1);
3822 }
3823}
3824
Sahitya Tummala1e78e8b2018-10-10 10:56:22 +05303825void f2fs_wait_on_block_writeback_range(struct inode *inode, block_t blkaddr,
3826 block_t len)
3827{
3828 block_t i;
3829
3830 for (i = 0; i < len; i++)
3831 f2fs_wait_on_block_writeback(inode, blkaddr + i);
3832}
3833
Chao Yu77357302018-07-17 00:02:17 +08003834static int read_compacted_summaries(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003835{
3836 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
3837 struct curseg_info *seg_i;
3838 unsigned char *kaddr;
3839 struct page *page;
3840 block_t start;
3841 int i, j, offset;
3842
3843 start = start_sum_block(sbi);
3844
Chao Yu4d57b862018-05-30 00:20:41 +08003845 page = f2fs_get_meta_page(sbi, start++);
Chao Yu77357302018-07-17 00:02:17 +08003846 if (IS_ERR(page))
3847 return PTR_ERR(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003848 kaddr = (unsigned char *)page_address(page);
3849
3850 /* Step 1: restore nat cache */
3851 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08003852 memcpy(seg_i->journal, kaddr, SUM_JOURNAL_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003853
3854 /* Step 2: restore sit cache */
3855 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08003856 memcpy(seg_i->journal, kaddr + SUM_JOURNAL_SIZE, SUM_JOURNAL_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003857 offset = 2 * SUM_JOURNAL_SIZE;
3858
3859 /* Step 3: restore summary entries */
3860 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
3861 unsigned short blk_off;
3862 unsigned int segno;
3863
3864 seg_i = CURSEG_I(sbi, i);
3865 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
3866 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
3867 seg_i->next_segno = segno;
3868 reset_curseg(sbi, i, 0);
3869 seg_i->alloc_type = ckpt->alloc_type[i];
3870 seg_i->next_blkoff = blk_off;
3871
3872 if (seg_i->alloc_type == SSR)
3873 blk_off = sbi->blocks_per_seg;
3874
3875 for (j = 0; j < blk_off; j++) {
3876 struct f2fs_summary *s;
Yi Zhuang5f029c02021-04-06 09:47:35 +08003877
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003878 s = (struct f2fs_summary *)(kaddr + offset);
3879 seg_i->sum_blk->entries[j] = *s;
3880 offset += SUMMARY_SIZE;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003881 if (offset + SUMMARY_SIZE <= PAGE_SIZE -
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003882 SUM_FOOTER_SIZE)
3883 continue;
3884
3885 f2fs_put_page(page, 1);
3886 page = NULL;
3887
Chao Yu4d57b862018-05-30 00:20:41 +08003888 page = f2fs_get_meta_page(sbi, start++);
Chao Yu77357302018-07-17 00:02:17 +08003889 if (IS_ERR(page))
3890 return PTR_ERR(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003891 kaddr = (unsigned char *)page_address(page);
3892 offset = 0;
3893 }
3894 }
3895 f2fs_put_page(page, 1);
Chao Yu77357302018-07-17 00:02:17 +08003896 return 0;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003897}
3898
3899static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
3900{
3901 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
3902 struct f2fs_summary_block *sum;
3903 struct curseg_info *curseg;
3904 struct page *new;
3905 unsigned short blk_off;
3906 unsigned int segno = 0;
3907 block_t blk_addr = 0;
Chao Yu77357302018-07-17 00:02:17 +08003908 int err = 0;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003909
3910 /* get segment number and block addr */
3911 if (IS_DATASEG(type)) {
3912 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
3913 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
3914 CURSEG_HOT_DATA]);
Jaegeuk Kim119ee912015-01-29 11:45:33 -08003915 if (__exist_node_summaries(sbi))
Chao Yud0b9e422020-08-04 21:14:45 +08003916 blk_addr = sum_blk_addr(sbi, NR_CURSEG_PERSIST_TYPE, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003917 else
3918 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
3919 } else {
3920 segno = le32_to_cpu(ckpt->cur_node_segno[type -
3921 CURSEG_HOT_NODE]);
3922 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
3923 CURSEG_HOT_NODE]);
Jaegeuk Kim119ee912015-01-29 11:45:33 -08003924 if (__exist_node_summaries(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003925 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
3926 type - CURSEG_HOT_NODE);
3927 else
3928 blk_addr = GET_SUM_BLOCK(sbi, segno);
3929 }
3930
Chao Yu4d57b862018-05-30 00:20:41 +08003931 new = f2fs_get_meta_page(sbi, blk_addr);
Chao Yu77357302018-07-17 00:02:17 +08003932 if (IS_ERR(new))
3933 return PTR_ERR(new);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003934 sum = (struct f2fs_summary_block *)page_address(new);
3935
3936 if (IS_NODESEG(type)) {
Jaegeuk Kim119ee912015-01-29 11:45:33 -08003937 if (__exist_node_summaries(sbi)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003938 struct f2fs_summary *ns = &sum->entries[0];
3939 int i;
Yi Zhuang5f029c02021-04-06 09:47:35 +08003940
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003941 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
3942 ns->version = 0;
3943 ns->ofs_in_node = 0;
3944 }
3945 } else {
Chao Yu77357302018-07-17 00:02:17 +08003946 err = f2fs_restore_node_summary(sbi, segno, sum);
3947 if (err)
3948 goto out;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003949 }
3950 }
3951
3952 /* set uncompleted segment to curseg */
3953 curseg = CURSEG_I(sbi, type);
3954 mutex_lock(&curseg->curseg_mutex);
Chao Yub7ad7512016-02-19 18:08:46 +08003955
3956 /* update journal info */
3957 down_write(&curseg->journal_rwsem);
3958 memcpy(curseg->journal, &sum->journal, SUM_JOURNAL_SIZE);
3959 up_write(&curseg->journal_rwsem);
3960
3961 memcpy(curseg->sum_blk->entries, sum->entries, SUM_ENTRY_SIZE);
3962 memcpy(&curseg->sum_blk->footer, &sum->footer, SUM_FOOTER_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003963 curseg->next_segno = segno;
3964 reset_curseg(sbi, type, 0);
3965 curseg->alloc_type = ckpt->alloc_type[type];
3966 curseg->next_blkoff = blk_off;
3967 mutex_unlock(&curseg->curseg_mutex);
Chao Yu77357302018-07-17 00:02:17 +08003968out:
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003969 f2fs_put_page(new, 1);
Chao Yu77357302018-07-17 00:02:17 +08003970 return err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003971}
3972
3973static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
3974{
Jin Qian21d3f8e2017-06-01 11:18:30 -07003975 struct f2fs_journal *sit_j = CURSEG_I(sbi, CURSEG_COLD_DATA)->journal;
3976 struct f2fs_journal *nat_j = CURSEG_I(sbi, CURSEG_HOT_DATA)->journal;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003977 int type = CURSEG_HOT_DATA;
Chao Yue4fc5fb2014-03-17 16:36:24 +08003978 int err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003979
Chao Yuaaec2b12016-09-20 11:04:18 +08003980 if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG)) {
Chao Yu4d57b862018-05-30 00:20:41 +08003981 int npages = f2fs_npages_for_summary_flush(sbi, true);
Chao Yu3fa06d72014-12-09 14:21:46 +08003982
3983 if (npages >= 2)
Chao Yu4d57b862018-05-30 00:20:41 +08003984 f2fs_ra_meta_pages(sbi, start_sum_block(sbi), npages,
Chao Yu26879fb2015-10-12 17:05:59 +08003985 META_CP, true);
Chao Yu3fa06d72014-12-09 14:21:46 +08003986
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003987 /* restore for compacted data summary */
Chao Yu77357302018-07-17 00:02:17 +08003988 err = read_compacted_summaries(sbi);
3989 if (err)
3990 return err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003991 type = CURSEG_HOT_NODE;
3992 }
3993
Jaegeuk Kim119ee912015-01-29 11:45:33 -08003994 if (__exist_node_summaries(sbi))
Chao Yud0b9e422020-08-04 21:14:45 +08003995 f2fs_ra_meta_pages(sbi,
3996 sum_blk_addr(sbi, NR_CURSEG_PERSIST_TYPE, type),
3997 NR_CURSEG_PERSIST_TYPE - type, META_CP, true);
Chao Yu3fa06d72014-12-09 14:21:46 +08003998
Chao Yue4fc5fb2014-03-17 16:36:24 +08003999 for (; type <= CURSEG_COLD_NODE; type++) {
4000 err = read_normal_summaries(sbi, type);
4001 if (err)
4002 return err;
4003 }
4004
Jin Qian21d3f8e2017-06-01 11:18:30 -07004005 /* sanity check for summary blocks */
4006 if (nats_in_cursum(nat_j) > NAT_JOURNAL_ENTRIES ||
Sahitya Tummala9227d522019-05-23 09:49:17 +05304007 sits_in_cursum(sit_j) > SIT_JOURNAL_ENTRIES) {
Joe Perches833dcd32021-05-26 13:05:36 -07004008 f2fs_err(sbi, "invalid journal entries nats %u sits %u",
Joe Perchesdcbb4c12019-06-18 17:48:42 +08004009 nats_in_cursum(nat_j), sits_in_cursum(sit_j));
Jin Qian21d3f8e2017-06-01 11:18:30 -07004010 return -EINVAL;
Sahitya Tummala9227d522019-05-23 09:49:17 +05304011 }
Jin Qian21d3f8e2017-06-01 11:18:30 -07004012
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004013 return 0;
4014}
4015
4016static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
4017{
4018 struct page *page;
4019 unsigned char *kaddr;
4020 struct f2fs_summary *summary;
4021 struct curseg_info *seg_i;
4022 int written_size = 0;
4023 int i, j;
4024
Chao Yu4d57b862018-05-30 00:20:41 +08004025 page = f2fs_grab_meta_page(sbi, blkaddr++);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004026 kaddr = (unsigned char *)page_address(page);
Chao Yu81114ba2018-04-09 20:25:06 +08004027 memset(kaddr, 0, PAGE_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004028
4029 /* Step 1: write nat cache */
4030 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08004031 memcpy(kaddr, seg_i->journal, SUM_JOURNAL_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004032 written_size += SUM_JOURNAL_SIZE;
4033
4034 /* Step 2: write sit cache */
4035 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08004036 memcpy(kaddr + written_size, seg_i->journal, SUM_JOURNAL_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004037 written_size += SUM_JOURNAL_SIZE;
4038
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004039 /* Step 3: write summary entries */
4040 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
4041 unsigned short blkoff;
Yi Zhuang5f029c02021-04-06 09:47:35 +08004042
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004043 seg_i = CURSEG_I(sbi, i);
4044 if (sbi->ckpt->alloc_type[i] == SSR)
4045 blkoff = sbi->blocks_per_seg;
4046 else
4047 blkoff = curseg_blkoff(sbi, i);
4048
4049 for (j = 0; j < blkoff; j++) {
4050 if (!page) {
Chao Yu4d57b862018-05-30 00:20:41 +08004051 page = f2fs_grab_meta_page(sbi, blkaddr++);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004052 kaddr = (unsigned char *)page_address(page);
Chao Yu81114ba2018-04-09 20:25:06 +08004053 memset(kaddr, 0, PAGE_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004054 written_size = 0;
4055 }
4056 summary = (struct f2fs_summary *)(kaddr + written_size);
4057 *summary = seg_i->sum_blk->entries[j];
4058 written_size += SUMMARY_SIZE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004059
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004060 if (written_size + SUMMARY_SIZE <= PAGE_SIZE -
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004061 SUM_FOOTER_SIZE)
4062 continue;
4063
Chao Yue8d61a72013-10-24 15:08:28 +08004064 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004065 f2fs_put_page(page, 1);
4066 page = NULL;
4067 }
4068 }
Chao Yue8d61a72013-10-24 15:08:28 +08004069 if (page) {
4070 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004071 f2fs_put_page(page, 1);
Chao Yue8d61a72013-10-24 15:08:28 +08004072 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004073}
4074
4075static void write_normal_summaries(struct f2fs_sb_info *sbi,
4076 block_t blkaddr, int type)
4077{
4078 int i, end;
Yi Zhuang5f029c02021-04-06 09:47:35 +08004079
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004080 if (IS_DATASEG(type))
4081 end = type + NR_CURSEG_DATA_TYPE;
4082 else
4083 end = type + NR_CURSEG_NODE_TYPE;
4084
Chao Yub7ad7512016-02-19 18:08:46 +08004085 for (i = type; i < end; i++)
4086 write_current_sum_page(sbi, i, blkaddr + (i - type));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004087}
4088
Chao Yu4d57b862018-05-30 00:20:41 +08004089void f2fs_write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004090{
Chao Yuaaec2b12016-09-20 11:04:18 +08004091 if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004092 write_compacted_summaries(sbi, start_blk);
4093 else
4094 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
4095}
4096
Chao Yu4d57b862018-05-30 00:20:41 +08004097void f2fs_write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004098{
Jaegeuk Kim119ee912015-01-29 11:45:33 -08004099 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004100}
4101
Chao Yu4d57b862018-05-30 00:20:41 +08004102int f2fs_lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004103 unsigned int val, int alloc)
4104{
4105 int i;
4106
4107 if (type == NAT_JOURNAL) {
Chao Yudfc08a12016-02-14 18:50:40 +08004108 for (i = 0; i < nats_in_cursum(journal); i++) {
4109 if (le32_to_cpu(nid_in_journal(journal, i)) == val)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004110 return i;
4111 }
Chao Yudfc08a12016-02-14 18:50:40 +08004112 if (alloc && __has_cursum_space(journal, 1, NAT_JOURNAL))
4113 return update_nats_in_cursum(journal, 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004114 } else if (type == SIT_JOURNAL) {
Chao Yudfc08a12016-02-14 18:50:40 +08004115 for (i = 0; i < sits_in_cursum(journal); i++)
4116 if (le32_to_cpu(segno_in_journal(journal, i)) == val)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004117 return i;
Chao Yudfc08a12016-02-14 18:50:40 +08004118 if (alloc && __has_cursum_space(journal, 1, SIT_JOURNAL))
4119 return update_sits_in_cursum(journal, 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004120 }
4121 return -1;
4122}
4123
4124static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
4125 unsigned int segno)
4126{
Jaegeuk Kim86f33602020-10-02 14:17:35 -07004127 return f2fs_get_meta_page(sbi, current_sit_addr(sbi, segno));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004128}
4129
4130static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
4131 unsigned int start)
4132{
4133 struct sit_info *sit_i = SIT_I(sbi);
Yunlei He068c3cd2018-01-25 17:27:11 +08004134 struct page *page;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004135 pgoff_t src_off, dst_off;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004136
4137 src_off = current_sit_addr(sbi, start);
4138 dst_off = next_sit_addr(sbi, src_off);
4139
Chao Yu4d57b862018-05-30 00:20:41 +08004140 page = f2fs_grab_meta_page(sbi, dst_off);
Yunlei He068c3cd2018-01-25 17:27:11 +08004141 seg_info_to_sit_page(sbi, page, start);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004142
Yunlei He068c3cd2018-01-25 17:27:11 +08004143 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004144 set_to_next_sit(sit_i, start);
4145
Yunlei He068c3cd2018-01-25 17:27:11 +08004146 return page;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004147}
4148
Chao Yu184a5cd2014-09-04 18:13:01 +08004149static struct sit_entry_set *grab_sit_entry_set(void)
4150{
4151 struct sit_entry_set *ses =
Chao Yu32410572021-08-09 08:24:48 +08004152 f2fs_kmem_cache_alloc(sit_entry_set_slab,
4153 GFP_NOFS, true, NULL);
Chao Yu184a5cd2014-09-04 18:13:01 +08004154
4155 ses->entry_cnt = 0;
4156 INIT_LIST_HEAD(&ses->set_list);
4157 return ses;
4158}
4159
4160static void release_sit_entry_set(struct sit_entry_set *ses)
4161{
4162 list_del(&ses->set_list);
4163 kmem_cache_free(sit_entry_set_slab, ses);
4164}
4165
4166static void adjust_sit_entry_set(struct sit_entry_set *ses,
4167 struct list_head *head)
4168{
4169 struct sit_entry_set *next = ses;
4170
4171 if (list_is_last(&ses->set_list, head))
4172 return;
4173
4174 list_for_each_entry_continue(next, head, set_list)
4175 if (ses->entry_cnt <= next->entry_cnt)
4176 break;
4177
4178 list_move_tail(&ses->set_list, &next->set_list);
4179}
4180
4181static void add_sit_entry(unsigned int segno, struct list_head *head)
4182{
4183 struct sit_entry_set *ses;
4184 unsigned int start_segno = START_SEGNO(segno);
4185
4186 list_for_each_entry(ses, head, set_list) {
4187 if (ses->start_segno == start_segno) {
4188 ses->entry_cnt++;
4189 adjust_sit_entry_set(ses, head);
4190 return;
4191 }
4192 }
4193
4194 ses = grab_sit_entry_set();
4195
4196 ses->start_segno = start_segno;
4197 ses->entry_cnt++;
4198 list_add(&ses->set_list, head);
4199}
4200
4201static void add_sits_in_set(struct f2fs_sb_info *sbi)
4202{
4203 struct f2fs_sm_info *sm_info = SM_I(sbi);
4204 struct list_head *set_list = &sm_info->sit_entry_set;
4205 unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap;
Chao Yu184a5cd2014-09-04 18:13:01 +08004206 unsigned int segno;
4207
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07004208 for_each_set_bit(segno, bitmap, MAIN_SEGS(sbi))
Chao Yu184a5cd2014-09-04 18:13:01 +08004209 add_sit_entry(segno, set_list);
4210}
4211
4212static void remove_sits_in_journal(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004213{
4214 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08004215 struct f2fs_journal *journal = curseg->journal;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004216 int i;
4217
Chao Yub7ad7512016-02-19 18:08:46 +08004218 down_write(&curseg->journal_rwsem);
Chao Yudfc08a12016-02-14 18:50:40 +08004219 for (i = 0; i < sits_in_cursum(journal); i++) {
Chao Yu184a5cd2014-09-04 18:13:01 +08004220 unsigned int segno;
4221 bool dirtied;
4222
Chao Yudfc08a12016-02-14 18:50:40 +08004223 segno = le32_to_cpu(segno_in_journal(journal, i));
Chao Yu184a5cd2014-09-04 18:13:01 +08004224 dirtied = __mark_sit_entry_dirty(sbi, segno);
4225
4226 if (!dirtied)
4227 add_sit_entry(segno, &SM_I(sbi)->sit_entry_set);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004228 }
Chao Yudfc08a12016-02-14 18:50:40 +08004229 update_sits_in_cursum(journal, -i);
Chao Yub7ad7512016-02-19 18:08:46 +08004230 up_write(&curseg->journal_rwsem);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004231}
4232
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09004233/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004234 * CP calls this function, which flushes SIT entries including sit_journal,
4235 * and moves prefree segs to free segs.
4236 */
Chao Yu4d57b862018-05-30 00:20:41 +08004237void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004238{
4239 struct sit_info *sit_i = SIT_I(sbi);
4240 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
4241 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08004242 struct f2fs_journal *journal = curseg->journal;
Chao Yu184a5cd2014-09-04 18:13:01 +08004243 struct sit_entry_set *ses, *tmp;
4244 struct list_head *head = &SM_I(sbi)->sit_entry_set;
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08004245 bool to_journal = !is_sbi_flag_set(sbi, SBI_IS_RESIZEFS);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07004246 struct seg_entry *se;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004247
Chao Yu3d26fa62017-10-30 17:49:53 +08004248 down_write(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004249
Wanpeng Li2b11a742015-02-27 16:52:50 +08004250 if (!sit_i->dirty_sentries)
4251 goto out;
4252
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004253 /*
Chao Yu184a5cd2014-09-04 18:13:01 +08004254 * add and account sit entries of dirty bitmap in sit entry
4255 * set temporarily
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004256 */
Chao Yu184a5cd2014-09-04 18:13:01 +08004257 add_sits_in_set(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004258
Chao Yu184a5cd2014-09-04 18:13:01 +08004259 /*
4260 * if there are no enough space in journal to store dirty sit
4261 * entries, remove all entries from journal and add and account
4262 * them in sit entry set.
4263 */
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08004264 if (!__has_cursum_space(journal, sit_i->dirty_sentries, SIT_JOURNAL) ||
4265 !to_journal)
Chao Yu184a5cd2014-09-04 18:13:01 +08004266 remove_sits_in_journal(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004267
Chao Yu184a5cd2014-09-04 18:13:01 +08004268 /*
4269 * there are two steps to flush sit entries:
4270 * #1, flush sit entries to journal in current cold data summary block.
4271 * #2, flush sit entries to sit page.
4272 */
4273 list_for_each_entry_safe(ses, tmp, head, set_list) {
Jaegeuk Kim4a257ed2014-10-16 11:43:30 -07004274 struct page *page = NULL;
Chao Yu184a5cd2014-09-04 18:13:01 +08004275 struct f2fs_sit_block *raw_sit = NULL;
4276 unsigned int start_segno = ses->start_segno;
4277 unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07004278 (unsigned long)MAIN_SEGS(sbi));
Chao Yu184a5cd2014-09-04 18:13:01 +08004279 unsigned int segno = start_segno;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09004280
Chao Yu184a5cd2014-09-04 18:13:01 +08004281 if (to_journal &&
Chao Yudfc08a12016-02-14 18:50:40 +08004282 !__has_cursum_space(journal, ses->entry_cnt, SIT_JOURNAL))
Chao Yu184a5cd2014-09-04 18:13:01 +08004283 to_journal = false;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004284
Chao Yub7ad7512016-02-19 18:08:46 +08004285 if (to_journal) {
4286 down_write(&curseg->journal_rwsem);
4287 } else {
Chao Yu184a5cd2014-09-04 18:13:01 +08004288 page = get_next_sit_page(sbi, start_segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004289 raw_sit = page_address(page);
4290 }
4291
Chao Yu184a5cd2014-09-04 18:13:01 +08004292 /* flush dirty sit entries in region of current sit set */
4293 for_each_set_bit_from(segno, bitmap, end) {
4294 int offset, sit_offset;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07004295
4296 se = get_seg_entry(sbi, segno);
Zhikang Zhang56b07e72018-04-09 04:28:41 +08004297#ifdef CONFIG_F2FS_CHECK_FS
4298 if (memcmp(se->cur_valid_map, se->cur_valid_map_mir,
4299 SIT_VBLOCK_MAP_SIZE))
4300 f2fs_bug_on(sbi, 1);
4301#endif
Chao Yu184a5cd2014-09-04 18:13:01 +08004302
4303 /* add discard candidates */
Chao Yuc473f1a2017-04-27 20:40:39 +08004304 if (!(cpc->reason & CP_DISCARD)) {
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07004305 cpc->trim_start = segno;
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08004306 add_discard_addrs(sbi, cpc, false);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07004307 }
Chao Yu184a5cd2014-09-04 18:13:01 +08004308
4309 if (to_journal) {
Chao Yu4d57b862018-05-30 00:20:41 +08004310 offset = f2fs_lookup_journal_in_cursum(journal,
Chao Yu184a5cd2014-09-04 18:13:01 +08004311 SIT_JOURNAL, segno, 1);
4312 f2fs_bug_on(sbi, offset < 0);
Chao Yudfc08a12016-02-14 18:50:40 +08004313 segno_in_journal(journal, offset) =
Chao Yu184a5cd2014-09-04 18:13:01 +08004314 cpu_to_le32(segno);
4315 seg_info_to_raw_sit(se,
Chao Yudfc08a12016-02-14 18:50:40 +08004316 &sit_in_journal(journal, offset));
Zhikang Zhang56b07e72018-04-09 04:28:41 +08004317 check_block_count(sbi, segno,
4318 &sit_in_journal(journal, offset));
Chao Yu184a5cd2014-09-04 18:13:01 +08004319 } else {
4320 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
4321 seg_info_to_raw_sit(se,
4322 &raw_sit->entries[sit_offset]);
Zhikang Zhang56b07e72018-04-09 04:28:41 +08004323 check_block_count(sbi, segno,
4324 &raw_sit->entries[sit_offset]);
Chao Yu184a5cd2014-09-04 18:13:01 +08004325 }
4326
4327 __clear_bit(segno, bitmap);
4328 sit_i->dirty_sentries--;
4329 ses->entry_cnt--;
4330 }
4331
Chao Yub7ad7512016-02-19 18:08:46 +08004332 if (to_journal)
4333 up_write(&curseg->journal_rwsem);
4334 else
Chao Yu184a5cd2014-09-04 18:13:01 +08004335 f2fs_put_page(page, 1);
4336
4337 f2fs_bug_on(sbi, ses->entry_cnt);
4338 release_sit_entry_set(ses);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004339 }
Chao Yu184a5cd2014-09-04 18:13:01 +08004340
4341 f2fs_bug_on(sbi, !list_empty(head));
4342 f2fs_bug_on(sbi, sit_i->dirty_sentries);
Chao Yu184a5cd2014-09-04 18:13:01 +08004343out:
Chao Yuc473f1a2017-04-27 20:40:39 +08004344 if (cpc->reason & CP_DISCARD) {
Yunlei He650d3c42016-12-22 11:46:24 +08004345 __u64 trim_start = cpc->trim_start;
4346
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07004347 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++)
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08004348 add_discard_addrs(sbi, cpc, false);
Yunlei He650d3c42016-12-22 11:46:24 +08004349
4350 cpc->trim_start = trim_start;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07004351 }
Chao Yu3d26fa62017-10-30 17:49:53 +08004352 up_write(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004353
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004354 set_prefree_as_free_segments(sbi);
4355}
4356
4357static int build_sit_info(struct f2fs_sb_info *sbi)
4358{
4359 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004360 struct sit_info *sit_i;
4361 unsigned int sit_segs, start;
Chao Yu2fde3dd2019-07-26 15:41:20 +08004362 char *src_bitmap, *bitmap;
Sahitya Tummalabbf9f7d2019-08-07 19:10:32 +05304363 unsigned int bitmap_size, main_bitmap_size, sit_bitmap_size;
Chao Yu4f993262021-08-03 08:15:43 +08004364 unsigned int discard_map = f2fs_block_unit_discard(sbi) ? 1 : 0;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004365
4366 /* allocate memory for SIT information */
Chao Yuacbf0542017-11-30 19:28:17 +08004367 sit_i = f2fs_kzalloc(sbi, sizeof(struct sit_info), GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004368 if (!sit_i)
4369 return -ENOMEM;
4370
4371 SM_I(sbi)->sit_info = sit_i;
4372
Kees Cook9d2a7892018-06-12 14:28:35 -07004373 sit_i->sentries =
4374 f2fs_kvzalloc(sbi, array_size(sizeof(struct seg_entry),
4375 MAIN_SEGS(sbi)),
4376 GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004377 if (!sit_i->sentries)
4378 return -ENOMEM;
4379
Sahitya Tummalabbf9f7d2019-08-07 19:10:32 +05304380 main_bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
4381 sit_i->dirty_sentries_bitmap = f2fs_kvzalloc(sbi, main_bitmap_size,
Chao Yu628b3d12017-11-30 19:28:18 +08004382 GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004383 if (!sit_i->dirty_sentries_bitmap)
4384 return -ENOMEM;
4385
Chao Yu2fde3dd2019-07-26 15:41:20 +08004386#ifdef CONFIG_F2FS_CHECK_FS
Chao Yu4f993262021-08-03 08:15:43 +08004387 bitmap_size = MAIN_SEGS(sbi) * SIT_VBLOCK_MAP_SIZE * (3 + discard_map);
Chao Yu2fde3dd2019-07-26 15:41:20 +08004388#else
Chao Yu4f993262021-08-03 08:15:43 +08004389 bitmap_size = MAIN_SEGS(sbi) * SIT_VBLOCK_MAP_SIZE * (2 + discard_map);
Chao Yu2fde3dd2019-07-26 15:41:20 +08004390#endif
4391 sit_i->bitmap = f2fs_kvzalloc(sbi, bitmap_size, GFP_KERNEL);
4392 if (!sit_i->bitmap)
4393 return -ENOMEM;
4394
4395 bitmap = sit_i->bitmap;
4396
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07004397 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Chao Yu2fde3dd2019-07-26 15:41:20 +08004398 sit_i->sentries[start].cur_valid_map = bitmap;
4399 bitmap += SIT_VBLOCK_MAP_SIZE;
4400
4401 sit_i->sentries[start].ckpt_valid_map = bitmap;
4402 bitmap += SIT_VBLOCK_MAP_SIZE;
Jaegeuk Kim3e025742016-08-02 10:56:40 -07004403
Chao Yu355e7892017-01-07 18:51:01 +08004404#ifdef CONFIG_F2FS_CHECK_FS
Chao Yu2fde3dd2019-07-26 15:41:20 +08004405 sit_i->sentries[start].cur_valid_map_mir = bitmap;
4406 bitmap += SIT_VBLOCK_MAP_SIZE;
Chao Yu355e7892017-01-07 18:51:01 +08004407#endif
4408
Chao Yu4f993262021-08-03 08:15:43 +08004409 if (discard_map) {
4410 sit_i->sentries[start].discard_map = bitmap;
4411 bitmap += SIT_VBLOCK_MAP_SIZE;
4412 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004413 }
4414
Chao Yuacbf0542017-11-30 19:28:17 +08004415 sit_i->tmp_map = f2fs_kzalloc(sbi, SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
Jaegeuk Kim60a3b782015-02-10 16:44:29 -08004416 if (!sit_i->tmp_map)
4417 return -ENOMEM;
4418
Chao Yu2c70c5e2018-10-24 18:37:26 +08004419 if (__is_large_section(sbi)) {
Kees Cook9d2a7892018-06-12 14:28:35 -07004420 sit_i->sec_entries =
4421 f2fs_kvzalloc(sbi, array_size(sizeof(struct sec_entry),
4422 MAIN_SECS(sbi)),
4423 GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004424 if (!sit_i->sec_entries)
4425 return -ENOMEM;
4426 }
4427
4428 /* get information related with SIT */
4429 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
4430
4431 /* setup SIT bitmap from ckeckpoint pack */
Sahitya Tummalabbf9f7d2019-08-07 19:10:32 +05304432 sit_bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004433 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
4434
Sahitya Tummalabbf9f7d2019-08-07 19:10:32 +05304435 sit_i->sit_bitmap = kmemdup(src_bitmap, sit_bitmap_size, GFP_KERNEL);
Chao Yuae27d622017-01-07 18:52:34 +08004436 if (!sit_i->sit_bitmap)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004437 return -ENOMEM;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004438
Chao Yuae27d622017-01-07 18:52:34 +08004439#ifdef CONFIG_F2FS_CHECK_FS
Sahitya Tummalabbf9f7d2019-08-07 19:10:32 +05304440 sit_i->sit_bitmap_mir = kmemdup(src_bitmap,
4441 sit_bitmap_size, GFP_KERNEL);
Chao Yuae27d622017-01-07 18:52:34 +08004442 if (!sit_i->sit_bitmap_mir)
4443 return -ENOMEM;
Sahitya Tummalabbf9f7d2019-08-07 19:10:32 +05304444
4445 sit_i->invalid_segmap = f2fs_kvzalloc(sbi,
4446 main_bitmap_size, GFP_KERNEL);
4447 if (!sit_i->invalid_segmap)
4448 return -ENOMEM;
Chao Yuae27d622017-01-07 18:52:34 +08004449#endif
4450
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004451 /* init SIT information */
4452 sit_i->s_ops = &default_salloc_ops;
4453
4454 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
4455 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
Jaegeuk Kimc79b7ff2016-11-14 18:20:10 -08004456 sit_i->written_valid_blocks = 0;
Sahitya Tummalabbf9f7d2019-08-07 19:10:32 +05304457 sit_i->bitmap_size = sit_bitmap_size;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004458 sit_i->dirty_sentries = 0;
4459 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
4460 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
Jaegeuk Kima7e679b2020-02-25 19:08:16 -08004461 sit_i->mounted_time = ktime_get_boottime_seconds();
Chao Yu3d26fa62017-10-30 17:49:53 +08004462 init_rwsem(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004463 return 0;
4464}
4465
4466static int build_free_segmap(struct f2fs_sb_info *sbi)
4467{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004468 struct free_segmap_info *free_i;
4469 unsigned int bitmap_size, sec_bitmap_size;
4470
4471 /* allocate memory for free segmap information */
Chao Yuacbf0542017-11-30 19:28:17 +08004472 free_i = f2fs_kzalloc(sbi, sizeof(struct free_segmap_info), GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004473 if (!free_i)
4474 return -ENOMEM;
4475
4476 SM_I(sbi)->free_info = free_i;
4477
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07004478 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Chao Yu628b3d12017-11-30 19:28:18 +08004479 free_i->free_segmap = f2fs_kvmalloc(sbi, bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004480 if (!free_i->free_segmap)
4481 return -ENOMEM;
4482
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07004483 sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
Chao Yu628b3d12017-11-30 19:28:18 +08004484 free_i->free_secmap = f2fs_kvmalloc(sbi, sec_bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004485 if (!free_i->free_secmap)
4486 return -ENOMEM;
4487
4488 /* set all segments as dirty temporarily */
4489 memset(free_i->free_segmap, 0xff, bitmap_size);
4490 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
4491
4492 /* init free segmap information */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07004493 free_i->start_segno = GET_SEGNO_FROM_SEG0(sbi, MAIN_BLKADDR(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004494 free_i->free_segments = 0;
4495 free_i->free_sections = 0;
Chao Yu1a118cc2015-02-11 18:20:38 +08004496 spin_lock_init(&free_i->segmap_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004497 return 0;
4498}
4499
4500static int build_curseg(struct f2fs_sb_info *sbi)
4501{
Namjae Jeon1042d602012-12-01 10:56:13 +09004502 struct curseg_info *array;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004503 int i;
4504
Chao Yud0b9e422020-08-04 21:14:45 +08004505 array = f2fs_kzalloc(sbi, array_size(NR_CURSEG_TYPE,
4506 sizeof(*array)), GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004507 if (!array)
4508 return -ENOMEM;
4509
4510 SM_I(sbi)->curseg_array = array;
4511
Chao Yud0b9e422020-08-04 21:14:45 +08004512 for (i = 0; i < NO_CHECK_TYPE; i++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004513 mutex_init(&array[i].curseg_mutex);
Chao Yuacbf0542017-11-30 19:28:17 +08004514 array[i].sum_blk = f2fs_kzalloc(sbi, PAGE_SIZE, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004515 if (!array[i].sum_blk)
4516 return -ENOMEM;
Chao Yub7ad7512016-02-19 18:08:46 +08004517 init_rwsem(&array[i].journal_rwsem);
Chao Yuacbf0542017-11-30 19:28:17 +08004518 array[i].journal = f2fs_kzalloc(sbi,
4519 sizeof(struct f2fs_journal), GFP_KERNEL);
Chao Yub7ad7512016-02-19 18:08:46 +08004520 if (!array[i].journal)
4521 return -ENOMEM;
Chao Yud0b9e422020-08-04 21:14:45 +08004522 if (i < NR_PERSISTENT_LOG)
4523 array[i].seg_type = CURSEG_HOT_DATA + i;
4524 else if (i == CURSEG_COLD_DATA_PINNED)
4525 array[i].seg_type = CURSEG_COLD_DATA;
Chao Yu093749e2020-08-04 21:14:49 +08004526 else if (i == CURSEG_ALL_DATA_ATGC)
4527 array[i].seg_type = CURSEG_COLD_DATA;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004528 array[i].segno = NULL_SEGNO;
4529 array[i].next_blkoff = 0;
Chao Yud0b9e422020-08-04 21:14:45 +08004530 array[i].inited = false;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004531 }
4532 return restore_curseg_summaries(sbi);
4533}
4534
Jaegeuk Kimc39a1b32017-12-19 19:16:34 -08004535static int build_sit_entries(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004536{
4537 struct sit_info *sit_i = SIT_I(sbi);
4538 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08004539 struct f2fs_journal *journal = curseg->journal;
Yunlei He9c094042016-09-24 12:29:18 +08004540 struct seg_entry *se;
4541 struct f2fs_sit_entry sit;
Chao Yu74de5932013-11-22 09:09:59 +08004542 int sit_blk_cnt = SIT_BLK_CNT(sbi);
4543 unsigned int i, start, end;
4544 unsigned int readed, start_blk = 0;
Jaegeuk Kimc39a1b32017-12-19 19:16:34 -08004545 int err = 0;
Jaegeuk Kim8a29c122018-04-24 21:34:05 -06004546 block_t total_node_blocks = 0;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004547
Chao Yu74de5932013-11-22 09:09:59 +08004548 do {
Christoph Hellwiga8affc02021-03-11 12:01:37 +01004549 readed = f2fs_ra_meta_pages(sbi, start_blk, BIO_MAX_VECS,
Jaegeuk Kim664ba972016-10-18 11:07:45 -07004550 META_SIT, true);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004551
Chao Yu74de5932013-11-22 09:09:59 +08004552 start = start_blk * sit_i->sents_per_block;
4553 end = (start_blk + readed) * sit_i->sents_per_block;
4554
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07004555 for (; start < end && start < MAIN_SEGS(sbi); start++) {
Chao Yu74de5932013-11-22 09:09:59 +08004556 struct f2fs_sit_block *sit_blk;
Chao Yu74de5932013-11-22 09:09:59 +08004557 struct page *page;
4558
Yunlei He9c094042016-09-24 12:29:18 +08004559 se = &sit_i->sentries[start];
Chao Yu74de5932013-11-22 09:09:59 +08004560 page = get_current_sit_page(sbi, start);
Jaegeuk Kimedc55aa2018-09-17 17:36:06 -07004561 if (IS_ERR(page))
4562 return PTR_ERR(page);
Chao Yu74de5932013-11-22 09:09:59 +08004563 sit_blk = (struct f2fs_sit_block *)page_address(page);
4564 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
4565 f2fs_put_page(page, 1);
Chao Yud600af232016-08-19 23:13:47 +08004566
Jaegeuk Kimc39a1b32017-12-19 19:16:34 -08004567 err = check_block_count(sbi, start, &sit);
4568 if (err)
4569 return err;
Chao Yu74de5932013-11-22 09:09:59 +08004570 seg_info_from_raw_sit(se, &sit);
Jaegeuk Kim8a29c122018-04-24 21:34:05 -06004571 if (IS_NODESEG(se->type))
4572 total_node_blocks += se->valid_blocks;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07004573
Chao Yu4f993262021-08-03 08:15:43 +08004574 if (f2fs_block_unit_discard(sbi)) {
4575 /* build discard map only one time */
4576 if (is_set_ckpt_flags(sbi, CP_TRIMMED_FLAG)) {
4577 memset(se->discard_map, 0xff,
4578 SIT_VBLOCK_MAP_SIZE);
4579 } else {
4580 memcpy(se->discard_map,
4581 se->cur_valid_map,
4582 SIT_VBLOCK_MAP_SIZE);
4583 sbi->discard_blks +=
4584 sbi->blocks_per_seg -
4585 se->valid_blocks;
4586 }
Jaegeuk Kim3e025742016-08-02 10:56:40 -07004587 }
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07004588
Chao Yu2c70c5e2018-10-24 18:37:26 +08004589 if (__is_large_section(sbi))
Chao Yud600af232016-08-19 23:13:47 +08004590 get_sec_entry(sbi, start)->valid_blocks +=
4591 se->valid_blocks;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004592 }
Chao Yu74de5932013-11-22 09:09:59 +08004593 start_blk += readed;
4594 } while (start_blk < sit_blk_cnt);
Chao Yud600af232016-08-19 23:13:47 +08004595
4596 down_read(&curseg->journal_rwsem);
4597 for (i = 0; i < sits_in_cursum(journal); i++) {
Chao Yud600af232016-08-19 23:13:47 +08004598 unsigned int old_valid_blocks;
4599
4600 start = le32_to_cpu(segno_in_journal(journal, i));
Jaegeuk Kimb2ca3742018-04-24 15:44:16 -06004601 if (start >= MAIN_SEGS(sbi)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08004602 f2fs_err(sbi, "Wrong journal entry on segno %u",
4603 start);
Chao Yu10f966b2019-06-20 11:36:14 +08004604 err = -EFSCORRUPTED;
Jaegeuk Kimb2ca3742018-04-24 15:44:16 -06004605 break;
4606 }
4607
Chao Yud600af232016-08-19 23:13:47 +08004608 se = &sit_i->sentries[start];
4609 sit = sit_in_journal(journal, i);
4610
4611 old_valid_blocks = se->valid_blocks;
Jaegeuk Kim8a29c122018-04-24 21:34:05 -06004612 if (IS_NODESEG(se->type))
4613 total_node_blocks -= old_valid_blocks;
Chao Yud600af232016-08-19 23:13:47 +08004614
Jaegeuk Kimc39a1b32017-12-19 19:16:34 -08004615 err = check_block_count(sbi, start, &sit);
4616 if (err)
4617 break;
Chao Yud600af232016-08-19 23:13:47 +08004618 seg_info_from_raw_sit(se, &sit);
Jaegeuk Kim8a29c122018-04-24 21:34:05 -06004619 if (IS_NODESEG(se->type))
4620 total_node_blocks += se->valid_blocks;
Chao Yud600af232016-08-19 23:13:47 +08004621
Chao Yu4f993262021-08-03 08:15:43 +08004622 if (f2fs_block_unit_discard(sbi)) {
4623 if (is_set_ckpt_flags(sbi, CP_TRIMMED_FLAG)) {
4624 memset(se->discard_map, 0xff, SIT_VBLOCK_MAP_SIZE);
4625 } else {
4626 memcpy(se->discard_map, se->cur_valid_map,
4627 SIT_VBLOCK_MAP_SIZE);
4628 sbi->discard_blks += old_valid_blocks;
4629 sbi->discard_blks -= se->valid_blocks;
4630 }
Chao Yud600af232016-08-19 23:13:47 +08004631 }
4632
Chao Yu2c70c5e2018-10-24 18:37:26 +08004633 if (__is_large_section(sbi)) {
Chao Yud600af232016-08-19 23:13:47 +08004634 get_sec_entry(sbi, start)->valid_blocks +=
Chao Yua9af3fd2018-04-25 19:38:17 +08004635 se->valid_blocks;
4636 get_sec_entry(sbi, start)->valid_blocks -=
4637 old_valid_blocks;
4638 }
Chao Yud600af232016-08-19 23:13:47 +08004639 }
4640 up_read(&curseg->journal_rwsem);
Jaegeuk Kim8a29c122018-04-24 21:34:05 -06004641
4642 if (!err && total_node_blocks != valid_node_count(sbi)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08004643 f2fs_err(sbi, "SIT is corrupted node# %u vs %u",
4644 total_node_blocks, valid_node_count(sbi));
Chao Yu10f966b2019-06-20 11:36:14 +08004645 err = -EFSCORRUPTED;
Jaegeuk Kim8a29c122018-04-24 21:34:05 -06004646 }
4647
Jaegeuk Kimc39a1b32017-12-19 19:16:34 -08004648 return err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004649}
4650
4651static void init_free_segmap(struct f2fs_sb_info *sbi)
4652{
4653 unsigned int start;
4654 int type;
Aravind Rameshde881df2020-07-16 18:26:56 +05304655 struct seg_entry *sentry;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004656
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07004657 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Aravind Rameshde881df2020-07-16 18:26:56 +05304658 if (f2fs_usable_blks_in_seg(sbi, start) == 0)
4659 continue;
4660 sentry = get_seg_entry(sbi, start);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004661 if (!sentry->valid_blocks)
4662 __set_free(sbi, start);
Jaegeuk Kimc79b7ff2016-11-14 18:20:10 -08004663 else
4664 SIT_I(sbi)->written_valid_blocks +=
4665 sentry->valid_blocks;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004666 }
4667
4668 /* set use the current segments */
4669 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
4670 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
Yi Zhuang5f029c02021-04-06 09:47:35 +08004671
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004672 __set_test_and_inuse(sbi, curseg_t->segno);
4673 }
4674}
4675
4676static void init_dirty_segmap(struct f2fs_sb_info *sbi)
4677{
4678 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
4679 struct free_segmap_info *free_i = FREE_I(sbi);
Jack Qiuda52f8a2020-06-18 12:37:10 +08004680 unsigned int segno = 0, offset = 0, secno;
Aravind Rameshde881df2020-07-16 18:26:56 +05304681 block_t valid_blocks, usable_blks_in_seg;
Shin'ichiro Kawasaki123aaf72020-08-19 10:34:48 +09004682 block_t blks_per_sec = BLKS_PER_SEC(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004683
Namjae Jeon8736fbf2013-06-16 09:49:11 +09004684 while (1) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004685 /* find dirty segment based on free segmap */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07004686 segno = find_next_inuse(free_i, MAIN_SEGS(sbi), offset);
4687 if (segno >= MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004688 break;
4689 offset = segno + 1;
Jaegeuk Kim302bd342017-04-07 14:33:22 -07004690 valid_blocks = get_valid_blocks(sbi, segno, false);
Aravind Rameshde881df2020-07-16 18:26:56 +05304691 usable_blks_in_seg = f2fs_usable_blks_in_seg(sbi, segno);
4692 if (valid_blocks == usable_blks_in_seg || !valid_blocks)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004693 continue;
Aravind Rameshde881df2020-07-16 18:26:56 +05304694 if (valid_blocks > usable_blks_in_seg) {
Jaegeuk Kimec325b52014-09-02 16:24:11 -07004695 f2fs_bug_on(sbi, 1);
4696 continue;
4697 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004698 mutex_lock(&dirty_i->seglist_lock);
4699 __locate_dirty_segment(sbi, segno, DIRTY);
4700 mutex_unlock(&dirty_i->seglist_lock);
4701 }
Jack Qiuda52f8a2020-06-18 12:37:10 +08004702
4703 if (!__is_large_section(sbi))
4704 return;
4705
4706 mutex_lock(&dirty_i->seglist_lock);
Jack Qiu5335bfc2020-12-01 15:45:47 +08004707 for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) {
Jack Qiuda52f8a2020-06-18 12:37:10 +08004708 valid_blocks = get_valid_blocks(sbi, segno, true);
4709 secno = GET_SEC_FROM_SEG(sbi, segno);
4710
4711 if (!valid_blocks || valid_blocks == blks_per_sec)
4712 continue;
4713 if (IS_CURSEC(sbi, secno))
4714 continue;
4715 set_bit(secno, dirty_i->dirty_secmap);
4716 }
4717 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004718}
4719
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09004720static int init_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004721{
4722 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07004723 unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004724
Chao Yu628b3d12017-11-30 19:28:18 +08004725 dirty_i->victim_secmap = f2fs_kvzalloc(sbi, bitmap_size, GFP_KERNEL);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09004726 if (!dirty_i->victim_secmap)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004727 return -ENOMEM;
4728 return 0;
4729}
4730
4731static int build_dirty_segmap(struct f2fs_sb_info *sbi)
4732{
4733 struct dirty_seglist_info *dirty_i;
4734 unsigned int bitmap_size, i;
4735
4736 /* allocate memory for dirty segments list information */
Chao Yuacbf0542017-11-30 19:28:17 +08004737 dirty_i = f2fs_kzalloc(sbi, sizeof(struct dirty_seglist_info),
4738 GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004739 if (!dirty_i)
4740 return -ENOMEM;
4741
4742 SM_I(sbi)->dirty_info = dirty_i;
4743 mutex_init(&dirty_i->seglist_lock);
4744
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07004745 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004746
4747 for (i = 0; i < NR_DIRTY_TYPE; i++) {
Chao Yu628b3d12017-11-30 19:28:18 +08004748 dirty_i->dirty_segmap[i] = f2fs_kvzalloc(sbi, bitmap_size,
4749 GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004750 if (!dirty_i->dirty_segmap[i])
4751 return -ENOMEM;
4752 }
4753
Jack Qiuda52f8a2020-06-18 12:37:10 +08004754 if (__is_large_section(sbi)) {
4755 bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
4756 dirty_i->dirty_secmap = f2fs_kvzalloc(sbi,
4757 bitmap_size, GFP_KERNEL);
4758 if (!dirty_i->dirty_secmap)
4759 return -ENOMEM;
4760 }
4761
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004762 init_dirty_segmap(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09004763 return init_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004764}
4765
Chao Yuc854f4d2019-05-25 23:07:25 +08004766static int sanity_check_curseg(struct f2fs_sb_info *sbi)
4767{
4768 int i;
4769
4770 /*
4771 * In LFS/SSR curseg, .next_blkoff should point to an unused blkaddr;
4772 * In LFS curseg, all blkaddr after .next_blkoff should be unused.
4773 */
Chao Yud0b9e422020-08-04 21:14:45 +08004774 for (i = 0; i < NR_PERSISTENT_LOG; i++) {
Chao Yuc854f4d2019-05-25 23:07:25 +08004775 struct curseg_info *curseg = CURSEG_I(sbi, i);
4776 struct seg_entry *se = get_seg_entry(sbi, curseg->segno);
4777 unsigned int blkofs = curseg->next_blkoff;
4778
Jaegeuk Kima7d9fe32021-05-21 01:32:53 -07004779 if (f2fs_sb_has_readonly(sbi) &&
4780 i != CURSEG_HOT_DATA && i != CURSEG_HOT_NODE)
4781 continue;
4782
Chao Yu093749e2020-08-04 21:14:49 +08004783 sanity_check_seg_type(sbi, curseg->seg_type);
4784
Chao Yuc854f4d2019-05-25 23:07:25 +08004785 if (f2fs_test_bit(blkofs, se->cur_valid_map))
4786 goto out;
4787
4788 if (curseg->alloc_type == SSR)
4789 continue;
4790
4791 for (blkofs += 1; blkofs < sbi->blocks_per_seg; blkofs++) {
4792 if (!f2fs_test_bit(blkofs, se->cur_valid_map))
4793 continue;
4794out:
Joe Perchesdcbb4c12019-06-18 17:48:42 +08004795 f2fs_err(sbi,
4796 "Current segment's next free block offset is inconsistent with bitmap, logtype:%u, segno:%u, type:%u, next_blkoff:%u, blkofs:%u",
4797 i, curseg->segno, curseg->alloc_type,
4798 curseg->next_blkoff, blkofs);
Chao Yu10f966b2019-06-20 11:36:14 +08004799 return -EFSCORRUPTED;
Chao Yuc854f4d2019-05-25 23:07:25 +08004800 }
4801 }
4802 return 0;
4803}
4804
Shin'ichiro Kawasakic426d992019-12-09 19:44:44 +09004805#ifdef CONFIG_BLK_DEV_ZONED
4806
Shin'ichiro Kawasakid508c942019-12-09 19:44:45 +09004807static int check_zone_write_pointer(struct f2fs_sb_info *sbi,
4808 struct f2fs_dev_info *fdev,
4809 struct blk_zone *zone)
4810{
4811 unsigned int wp_segno, wp_blkoff, zone_secno, zone_segno, segno;
4812 block_t zone_block, wp_block, last_valid_block;
4813 unsigned int log_sectors_per_block = sbi->log_blocksize - SECTOR_SHIFT;
4814 int i, s, b, ret;
4815 struct seg_entry *se;
4816
4817 if (zone->type != BLK_ZONE_TYPE_SEQWRITE_REQ)
4818 return 0;
4819
4820 wp_block = fdev->start_blk + (zone->wp >> log_sectors_per_block);
4821 wp_segno = GET_SEGNO(sbi, wp_block);
4822 wp_blkoff = wp_block - START_BLOCK(sbi, wp_segno);
4823 zone_block = fdev->start_blk + (zone->start >> log_sectors_per_block);
4824 zone_segno = GET_SEGNO(sbi, zone_block);
4825 zone_secno = GET_SEC_FROM_SEG(sbi, zone_segno);
4826
4827 if (zone_segno >= MAIN_SEGS(sbi))
4828 return 0;
4829
4830 /*
4831 * Skip check of zones cursegs point to, since
4832 * fix_curseg_write_pointer() checks them.
4833 */
4834 for (i = 0; i < NO_CHECK_TYPE; i++)
4835 if (zone_secno == GET_SEC_FROM_SEG(sbi,
4836 CURSEG_I(sbi, i)->segno))
4837 return 0;
4838
4839 /*
4840 * Get last valid block of the zone.
4841 */
4842 last_valid_block = zone_block - 1;
4843 for (s = sbi->segs_per_sec - 1; s >= 0; s--) {
4844 segno = zone_segno + s;
4845 se = get_seg_entry(sbi, segno);
4846 for (b = sbi->blocks_per_seg - 1; b >= 0; b--)
4847 if (f2fs_test_bit(b, se->cur_valid_map)) {
4848 last_valid_block = START_BLOCK(sbi, segno) + b;
4849 break;
4850 }
4851 if (last_valid_block >= zone_block)
4852 break;
4853 }
4854
4855 /*
4856 * If last valid block is beyond the write pointer, report the
4857 * inconsistency. This inconsistency does not cause write error
4858 * because the zone will not be selected for write operation until
4859 * it get discarded. Just report it.
4860 */
4861 if (last_valid_block >= wp_block) {
4862 f2fs_notice(sbi, "Valid block beyond write pointer: "
4863 "valid block[0x%x,0x%x] wp[0x%x,0x%x]",
4864 GET_SEGNO(sbi, last_valid_block),
4865 GET_BLKOFF_FROM_SEG0(sbi, last_valid_block),
4866 wp_segno, wp_blkoff);
4867 return 0;
4868 }
4869
4870 /*
4871 * If there is no valid block in the zone and if write pointer is
4872 * not at zone start, reset the write pointer.
4873 */
4874 if (last_valid_block + 1 == zone_block && zone->wp != zone->start) {
4875 f2fs_notice(sbi,
4876 "Zone without valid block has non-zero write "
4877 "pointer. Reset the write pointer: wp[0x%x,0x%x]",
4878 wp_segno, wp_blkoff);
4879 ret = __f2fs_issue_discard_zone(sbi, fdev->bdev, zone_block,
4880 zone->len >> log_sectors_per_block);
4881 if (ret) {
4882 f2fs_err(sbi, "Discard zone failed: %s (errno=%d)",
4883 fdev->path, ret);
4884 return ret;
4885 }
4886 }
4887
4888 return 0;
4889}
4890
Shin'ichiro Kawasakic426d992019-12-09 19:44:44 +09004891static struct f2fs_dev_info *get_target_zoned_dev(struct f2fs_sb_info *sbi,
4892 block_t zone_blkaddr)
4893{
4894 int i;
4895
4896 for (i = 0; i < sbi->s_ndevs; i++) {
4897 if (!bdev_is_zoned(FDEV(i).bdev))
4898 continue;
4899 if (sbi->s_ndevs == 1 || (FDEV(i).start_blk <= zone_blkaddr &&
4900 zone_blkaddr <= FDEV(i).end_blk))
4901 return &FDEV(i);
4902 }
4903
4904 return NULL;
4905}
4906
4907static int report_one_zone_cb(struct blk_zone *zone, unsigned int idx,
Yi Zhuang5f029c02021-04-06 09:47:35 +08004908 void *data)
4909{
Shin'ichiro Kawasakic426d992019-12-09 19:44:44 +09004910 memcpy(data, zone, sizeof(struct blk_zone));
4911 return 0;
4912}
4913
4914static int fix_curseg_write_pointer(struct f2fs_sb_info *sbi, int type)
4915{
4916 struct curseg_info *cs = CURSEG_I(sbi, type);
4917 struct f2fs_dev_info *zbd;
4918 struct blk_zone zone;
4919 unsigned int cs_section, wp_segno, wp_blkoff, wp_sector_off;
4920 block_t cs_zone_block, wp_block;
4921 unsigned int log_sectors_per_block = sbi->log_blocksize - SECTOR_SHIFT;
4922 sector_t zone_sector;
4923 int err;
4924
4925 cs_section = GET_SEC_FROM_SEG(sbi, cs->segno);
4926 cs_zone_block = START_BLOCK(sbi, GET_SEG_FROM_SEC(sbi, cs_section));
4927
4928 zbd = get_target_zoned_dev(sbi, cs_zone_block);
4929 if (!zbd)
4930 return 0;
4931
4932 /* report zone for the sector the curseg points to */
4933 zone_sector = (sector_t)(cs_zone_block - zbd->start_blk)
4934 << log_sectors_per_block;
4935 err = blkdev_report_zones(zbd->bdev, zone_sector, 1,
4936 report_one_zone_cb, &zone);
4937 if (err != 1) {
4938 f2fs_err(sbi, "Report zone failed: %s errno=(%d)",
4939 zbd->path, err);
4940 return err;
4941 }
4942
4943 if (zone.type != BLK_ZONE_TYPE_SEQWRITE_REQ)
4944 return 0;
4945
4946 wp_block = zbd->start_blk + (zone.wp >> log_sectors_per_block);
4947 wp_segno = GET_SEGNO(sbi, wp_block);
4948 wp_blkoff = wp_block - START_BLOCK(sbi, wp_segno);
4949 wp_sector_off = zone.wp & GENMASK(log_sectors_per_block - 1, 0);
4950
4951 if (cs->segno == wp_segno && cs->next_blkoff == wp_blkoff &&
4952 wp_sector_off == 0)
4953 return 0;
4954
4955 f2fs_notice(sbi, "Unaligned curseg[%d] with write pointer: "
4956 "curseg[0x%x,0x%x] wp[0x%x,0x%x]",
4957 type, cs->segno, cs->next_blkoff, wp_segno, wp_blkoff);
4958
4959 f2fs_notice(sbi, "Assign new section to curseg[%d]: "
4960 "curseg[0x%x,0x%x]", type, cs->segno, cs->next_blkoff);
Chao Yu509f1012021-04-21 09:54:55 +08004961
4962 f2fs_allocate_new_section(sbi, type, true);
Shin'ichiro Kawasakic426d992019-12-09 19:44:44 +09004963
Shin'ichiro Kawasakid508c942019-12-09 19:44:45 +09004964 /* check consistency of the zone curseg pointed to */
4965 if (check_zone_write_pointer(sbi, zbd, &zone))
4966 return -EIO;
4967
Shin'ichiro Kawasakic426d992019-12-09 19:44:44 +09004968 /* check newly assigned zone */
4969 cs_section = GET_SEC_FROM_SEG(sbi, cs->segno);
4970 cs_zone_block = START_BLOCK(sbi, GET_SEG_FROM_SEC(sbi, cs_section));
4971
4972 zbd = get_target_zoned_dev(sbi, cs_zone_block);
4973 if (!zbd)
4974 return 0;
4975
4976 zone_sector = (sector_t)(cs_zone_block - zbd->start_blk)
4977 << log_sectors_per_block;
4978 err = blkdev_report_zones(zbd->bdev, zone_sector, 1,
4979 report_one_zone_cb, &zone);
4980 if (err != 1) {
4981 f2fs_err(sbi, "Report zone failed: %s errno=(%d)",
4982 zbd->path, err);
4983 return err;
4984 }
4985
4986 if (zone.type != BLK_ZONE_TYPE_SEQWRITE_REQ)
4987 return 0;
4988
4989 if (zone.wp != zone.start) {
4990 f2fs_notice(sbi,
4991 "New zone for curseg[%d] is not yet discarded. "
4992 "Reset the zone: curseg[0x%x,0x%x]",
4993 type, cs->segno, cs->next_blkoff);
4994 err = __f2fs_issue_discard_zone(sbi, zbd->bdev,
4995 zone_sector >> log_sectors_per_block,
4996 zone.len >> log_sectors_per_block);
4997 if (err) {
4998 f2fs_err(sbi, "Discard zone failed: %s (errno=%d)",
4999 zbd->path, err);
5000 return err;
5001 }
5002 }
5003
5004 return 0;
5005}
5006
5007int f2fs_fix_curseg_write_pointer(struct f2fs_sb_info *sbi)
5008{
5009 int i, ret;
5010
Chao Yud0b9e422020-08-04 21:14:45 +08005011 for (i = 0; i < NR_PERSISTENT_LOG; i++) {
Shin'ichiro Kawasakic426d992019-12-09 19:44:44 +09005012 ret = fix_curseg_write_pointer(sbi, i);
5013 if (ret)
5014 return ret;
5015 }
5016
5017 return 0;
5018}
Shin'ichiro Kawasakid508c942019-12-09 19:44:45 +09005019
5020struct check_zone_write_pointer_args {
5021 struct f2fs_sb_info *sbi;
5022 struct f2fs_dev_info *fdev;
5023};
5024
5025static int check_zone_write_pointer_cb(struct blk_zone *zone, unsigned int idx,
Yi Zhuang5f029c02021-04-06 09:47:35 +08005026 void *data)
5027{
Shin'ichiro Kawasakid508c942019-12-09 19:44:45 +09005028 struct check_zone_write_pointer_args *args;
Yi Zhuang5f029c02021-04-06 09:47:35 +08005029
Shin'ichiro Kawasakid508c942019-12-09 19:44:45 +09005030 args = (struct check_zone_write_pointer_args *)data;
5031
5032 return check_zone_write_pointer(args->sbi, args->fdev, zone);
5033}
5034
5035int f2fs_check_write_pointer(struct f2fs_sb_info *sbi)
5036{
5037 int i, ret;
5038 struct check_zone_write_pointer_args args;
5039
5040 for (i = 0; i < sbi->s_ndevs; i++) {
5041 if (!bdev_is_zoned(FDEV(i).bdev))
5042 continue;
5043
5044 args.sbi = sbi;
5045 args.fdev = &FDEV(i);
5046 ret = blkdev_report_zones(FDEV(i).bdev, 0, BLK_ALL_ZONES,
5047 check_zone_write_pointer_cb, &args);
5048 if (ret < 0)
5049 return ret;
5050 }
5051
5052 return 0;
5053}
Aravind Rameshde881df2020-07-16 18:26:56 +05305054
5055static bool is_conv_zone(struct f2fs_sb_info *sbi, unsigned int zone_idx,
5056 unsigned int dev_idx)
5057{
5058 if (!bdev_is_zoned(FDEV(dev_idx).bdev))
5059 return true;
5060 return !test_bit(zone_idx, FDEV(dev_idx).blkz_seq);
5061}
5062
5063/* Return the zone index in the given device */
5064static unsigned int get_zone_idx(struct f2fs_sb_info *sbi, unsigned int secno,
5065 int dev_idx)
5066{
5067 block_t sec_start_blkaddr = START_BLOCK(sbi, GET_SEG_FROM_SEC(sbi, secno));
5068
5069 return (sec_start_blkaddr - FDEV(dev_idx).start_blk) >>
5070 sbi->log_blocks_per_blkz;
5071}
5072
5073/*
5074 * Return the usable segments in a section based on the zone's
5075 * corresponding zone capacity. Zone is equal to a section.
5076 */
5077static inline unsigned int f2fs_usable_zone_segs_in_sec(
5078 struct f2fs_sb_info *sbi, unsigned int segno)
5079{
5080 unsigned int dev_idx, zone_idx, unusable_segs_in_sec;
5081
5082 dev_idx = f2fs_target_device_index(sbi, START_BLOCK(sbi, segno));
5083 zone_idx = get_zone_idx(sbi, GET_SEC_FROM_SEG(sbi, segno), dev_idx);
5084
5085 /* Conventional zone's capacity is always equal to zone size */
5086 if (is_conv_zone(sbi, zone_idx, dev_idx))
5087 return sbi->segs_per_sec;
5088
5089 /*
5090 * If the zone_capacity_blocks array is NULL, then zone capacity
5091 * is equal to the zone size for all zones
5092 */
5093 if (!FDEV(dev_idx).zone_capacity_blocks)
5094 return sbi->segs_per_sec;
5095
5096 /* Get the segment count beyond zone capacity block */
5097 unusable_segs_in_sec = (sbi->blocks_per_blkz -
5098 FDEV(dev_idx).zone_capacity_blocks[zone_idx]) >>
5099 sbi->log_blocks_per_seg;
5100 return sbi->segs_per_sec - unusable_segs_in_sec;
5101}
5102
5103/*
5104 * Return the number of usable blocks in a segment. The number of blocks
5105 * returned is always equal to the number of blocks in a segment for
5106 * segments fully contained within a sequential zone capacity or a
5107 * conventional zone. For segments partially contained in a sequential
5108 * zone capacity, the number of usable blocks up to the zone capacity
5109 * is returned. 0 is returned in all other cases.
5110 */
5111static inline unsigned int f2fs_usable_zone_blks_in_seg(
5112 struct f2fs_sb_info *sbi, unsigned int segno)
5113{
5114 block_t seg_start, sec_start_blkaddr, sec_cap_blkaddr;
5115 unsigned int zone_idx, dev_idx, secno;
5116
5117 secno = GET_SEC_FROM_SEG(sbi, segno);
5118 seg_start = START_BLOCK(sbi, segno);
5119 dev_idx = f2fs_target_device_index(sbi, seg_start);
5120 zone_idx = get_zone_idx(sbi, secno, dev_idx);
5121
5122 /*
5123 * Conventional zone's capacity is always equal to zone size,
5124 * so, blocks per segment is unchanged.
5125 */
5126 if (is_conv_zone(sbi, zone_idx, dev_idx))
5127 return sbi->blocks_per_seg;
5128
5129 if (!FDEV(dev_idx).zone_capacity_blocks)
5130 return sbi->blocks_per_seg;
5131
5132 sec_start_blkaddr = START_BLOCK(sbi, GET_SEG_FROM_SEC(sbi, secno));
5133 sec_cap_blkaddr = sec_start_blkaddr +
5134 FDEV(dev_idx).zone_capacity_blocks[zone_idx];
5135
5136 /*
5137 * If segment starts before zone capacity and spans beyond
5138 * zone capacity, then usable blocks are from seg start to
5139 * zone capacity. If the segment starts after the zone capacity,
5140 * then there are no usable blocks.
5141 */
5142 if (seg_start >= sec_cap_blkaddr)
5143 return 0;
5144 if (seg_start + sbi->blocks_per_seg > sec_cap_blkaddr)
5145 return sec_cap_blkaddr - seg_start;
5146
5147 return sbi->blocks_per_seg;
5148}
Shin'ichiro Kawasakic426d992019-12-09 19:44:44 +09005149#else
5150int f2fs_fix_curseg_write_pointer(struct f2fs_sb_info *sbi)
5151{
5152 return 0;
5153}
Shin'ichiro Kawasakid508c942019-12-09 19:44:45 +09005154
5155int f2fs_check_write_pointer(struct f2fs_sb_info *sbi)
5156{
5157 return 0;
5158}
Aravind Rameshde881df2020-07-16 18:26:56 +05305159
5160static inline unsigned int f2fs_usable_zone_blks_in_seg(struct f2fs_sb_info *sbi,
5161 unsigned int segno)
5162{
5163 return 0;
5164}
5165
5166static inline unsigned int f2fs_usable_zone_segs_in_sec(struct f2fs_sb_info *sbi,
5167 unsigned int segno)
5168{
5169 return 0;
5170}
Shin'ichiro Kawasakic426d992019-12-09 19:44:44 +09005171#endif
Aravind Rameshde881df2020-07-16 18:26:56 +05305172unsigned int f2fs_usable_blks_in_seg(struct f2fs_sb_info *sbi,
5173 unsigned int segno)
5174{
5175 if (f2fs_sb_has_blkzoned(sbi))
5176 return f2fs_usable_zone_blks_in_seg(sbi, segno);
5177
5178 return sbi->blocks_per_seg;
5179}
5180
5181unsigned int f2fs_usable_segs_in_sec(struct f2fs_sb_info *sbi,
5182 unsigned int segno)
5183{
5184 if (f2fs_sb_has_blkzoned(sbi))
5185 return f2fs_usable_zone_segs_in_sec(sbi, segno);
5186
5187 return sbi->segs_per_sec;
5188}
Shin'ichiro Kawasakic426d992019-12-09 19:44:44 +09005189
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09005190/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005191 * Update min, max modified time for cost-benefit GC algorithm
5192 */
5193static void init_min_max_mtime(struct f2fs_sb_info *sbi)
5194{
5195 struct sit_info *sit_i = SIT_I(sbi);
5196 unsigned int segno;
5197
Chao Yu3d26fa62017-10-30 17:49:53 +08005198 down_write(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005199
Chao Yu5ad25442018-05-15 18:59:55 +08005200 sit_i->min_mtime = ULLONG_MAX;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005201
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07005202 for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005203 unsigned int i;
5204 unsigned long long mtime = 0;
5205
5206 for (i = 0; i < sbi->segs_per_sec; i++)
5207 mtime += get_seg_entry(sbi, segno + i)->mtime;
5208
5209 mtime = div_u64(mtime, sbi->segs_per_sec);
5210
5211 if (sit_i->min_mtime > mtime)
5212 sit_i->min_mtime = mtime;
5213 }
Chao Yua1f72ac22018-06-04 23:20:17 +08005214 sit_i->max_mtime = get_mtime(sbi, false);
Chao Yu093749e2020-08-04 21:14:49 +08005215 sit_i->dirty_max_mtime = 0;
Chao Yu3d26fa62017-10-30 17:49:53 +08005216 up_write(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005217}
5218
Chao Yu4d57b862018-05-30 00:20:41 +08005219int f2fs_build_segment_manager(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005220{
5221 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
5222 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Namjae Jeon1042d602012-12-01 10:56:13 +09005223 struct f2fs_sm_info *sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005224 int err;
5225
Chao Yuacbf0542017-11-30 19:28:17 +08005226 sm_info = f2fs_kzalloc(sbi, sizeof(struct f2fs_sm_info), GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005227 if (!sm_info)
5228 return -ENOMEM;
5229
5230 /* init sm info */
5231 sbi->sm_info = sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005232 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
5233 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
5234 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
5235 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
5236 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
5237 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
5238 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
Jaegeuk Kim58c41032014-03-19 14:17:21 +09005239 sm_info->rec_prefree_segments = sm_info->main_segments *
5240 DEF_RECLAIM_PREFREE_SEGMENTS / 100;
Jaegeuk Kim44a83492016-07-13 18:23:35 -07005241 if (sm_info->rec_prefree_segments > DEF_MAX_RECLAIM_PREFREE_SEGMENTS)
5242 sm_info->rec_prefree_segments = DEF_MAX_RECLAIM_PREFREE_SEGMENTS;
5243
Chao Yub0332a02020-02-14 17:44:12 +08005244 if (!f2fs_lfs_mode(sbi))
Jaegeuk Kim52763a42016-06-13 09:47:48 -07005245 sm_info->ipu_policy = 1 << F2FS_IPU_FSYNC;
Jaegeuk Kim216fbd62013-11-07 13:13:42 +09005246 sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -07005247 sm_info->min_fsync_blocks = DEF_MIN_FSYNC_BLOCKS;
Laibin Qiudc675a92021-07-31 11:26:46 +08005248 sm_info->min_seq_blocks = sbi->blocks_per_seg;
Jaegeuk Kimef095d12017-03-24 20:05:13 -04005249 sm_info->min_hot_blocks = DEF_MIN_HOT_BLOCKS;
Chao Yua2a12b62017-10-28 16:52:33 +08005250 sm_info->min_ssr_sections = reserved_sections(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005251
Chao Yu184a5cd2014-09-04 18:13:01 +08005252 INIT_LIST_HEAD(&sm_info->sit_entry_set);
5253
Chao Yu2b603112017-11-02 20:41:03 +08005254 init_rwsem(&sm_info->curseg_lock);
5255
Yunlei Hed4fdf8b2017-06-01 16:43:51 +08005256 if (!f2fs_readonly(sbi->sb)) {
Chao Yu4d57b862018-05-30 00:20:41 +08005257 err = f2fs_create_flush_cmd_control(sbi);
Gu Zheng2163d192014-04-27 14:21:33 +08005258 if (err)
Gu Zhenga688b9d9e2014-04-27 14:21:21 +08005259 return err;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +09005260 }
5261
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08005262 err = create_discard_cmd_control(sbi);
5263 if (err)
5264 return err;
5265
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005266 err = build_sit_info(sbi);
5267 if (err)
5268 return err;
5269 err = build_free_segmap(sbi);
5270 if (err)
5271 return err;
5272 err = build_curseg(sbi);
5273 if (err)
5274 return err;
5275
5276 /* reinit free segmap based on SIT */
Jaegeuk Kimc39a1b32017-12-19 19:16:34 -08005277 err = build_sit_entries(sbi);
5278 if (err)
5279 return err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005280
5281 init_free_segmap(sbi);
5282 err = build_dirty_segmap(sbi);
5283 if (err)
5284 return err;
5285
Chao Yuc854f4d2019-05-25 23:07:25 +08005286 err = sanity_check_curseg(sbi);
5287 if (err)
5288 return err;
5289
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005290 init_min_max_mtime(sbi);
5291 return 0;
5292}
5293
5294static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
5295 enum dirty_type dirty_type)
5296{
5297 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
5298
5299 mutex_lock(&dirty_i->seglist_lock);
Jaegeuk Kim39307a82015-09-22 13:50:47 -07005300 kvfree(dirty_i->dirty_segmap[dirty_type]);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005301 dirty_i->nr_dirty[dirty_type] = 0;
5302 mutex_unlock(&dirty_i->seglist_lock);
5303}
5304
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09005305static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005306{
5307 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Yi Zhuang5f029c02021-04-06 09:47:35 +08005308
Jaegeuk Kim39307a82015-09-22 13:50:47 -07005309 kvfree(dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005310}
5311
5312static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
5313{
5314 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
5315 int i;
5316
5317 if (!dirty_i)
5318 return;
5319
5320 /* discard pre-free/dirty segments list */
5321 for (i = 0; i < NR_DIRTY_TYPE; i++)
5322 discard_dirty_segmap(sbi, i);
5323
Jack Qiuda52f8a2020-06-18 12:37:10 +08005324 if (__is_large_section(sbi)) {
5325 mutex_lock(&dirty_i->seglist_lock);
5326 kvfree(dirty_i->dirty_secmap);
5327 mutex_unlock(&dirty_i->seglist_lock);
5328 }
5329
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09005330 destroy_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005331 SM_I(sbi)->dirty_info = NULL;
Chao Yuc8eb7022020-09-14 16:47:00 +08005332 kfree(dirty_i);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005333}
5334
5335static void destroy_curseg(struct f2fs_sb_info *sbi)
5336{
5337 struct curseg_info *array = SM_I(sbi)->curseg_array;
5338 int i;
5339
5340 if (!array)
5341 return;
5342 SM_I(sbi)->curseg_array = NULL;
Chao Yub7ad7512016-02-19 18:08:46 +08005343 for (i = 0; i < NR_CURSEG_TYPE; i++) {
Chao Yuc8eb7022020-09-14 16:47:00 +08005344 kfree(array[i].sum_blk);
5345 kfree(array[i].journal);
Chao Yub7ad7512016-02-19 18:08:46 +08005346 }
Chao Yuc8eb7022020-09-14 16:47:00 +08005347 kfree(array);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005348}
5349
5350static void destroy_free_segmap(struct f2fs_sb_info *sbi)
5351{
5352 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
Yi Zhuang5f029c02021-04-06 09:47:35 +08005353
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005354 if (!free_i)
5355 return;
5356 SM_I(sbi)->free_info = NULL;
Jaegeuk Kim39307a82015-09-22 13:50:47 -07005357 kvfree(free_i->free_segmap);
5358 kvfree(free_i->free_secmap);
Chao Yuc8eb7022020-09-14 16:47:00 +08005359 kfree(free_i);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005360}
5361
5362static void destroy_sit_info(struct f2fs_sb_info *sbi)
5363{
5364 struct sit_info *sit_i = SIT_I(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005365
5366 if (!sit_i)
5367 return;
5368
Chao Yu2fde3dd2019-07-26 15:41:20 +08005369 if (sit_i->sentries)
5370 kvfree(sit_i->bitmap);
Chao Yuc8eb7022020-09-14 16:47:00 +08005371 kfree(sit_i->tmp_map);
Jaegeuk Kim60a3b782015-02-10 16:44:29 -08005372
Jaegeuk Kim39307a82015-09-22 13:50:47 -07005373 kvfree(sit_i->sentries);
5374 kvfree(sit_i->sec_entries);
5375 kvfree(sit_i->dirty_sentries_bitmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005376
5377 SM_I(sbi)->sit_info = NULL;
Jaegeuk Kim52225952018-12-13 18:38:33 -08005378 kvfree(sit_i->sit_bitmap);
Chao Yuae27d622017-01-07 18:52:34 +08005379#ifdef CONFIG_F2FS_CHECK_FS
Jaegeuk Kim52225952018-12-13 18:38:33 -08005380 kvfree(sit_i->sit_bitmap_mir);
Sahitya Tummalabbf9f7d2019-08-07 19:10:32 +05305381 kvfree(sit_i->invalid_segmap);
Chao Yuae27d622017-01-07 18:52:34 +08005382#endif
Chao Yuc8eb7022020-09-14 16:47:00 +08005383 kfree(sit_i);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005384}
5385
Chao Yu4d57b862018-05-30 00:20:41 +08005386void f2fs_destroy_segment_manager(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005387{
5388 struct f2fs_sm_info *sm_info = SM_I(sbi);
Gu Zhenga688b9d9e2014-04-27 14:21:21 +08005389
Chao Yu3b03f722013-11-06 09:12:04 +08005390 if (!sm_info)
5391 return;
Chao Yu4d57b862018-05-30 00:20:41 +08005392 f2fs_destroy_flush_cmd_control(sbi, true);
Chao Yuf0994052017-03-27 18:14:04 +08005393 destroy_discard_cmd_control(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005394 destroy_dirty_segmap(sbi);
5395 destroy_curseg(sbi);
5396 destroy_free_segmap(sbi);
5397 destroy_sit_info(sbi);
5398 sbi->sm_info = NULL;
Chao Yuc8eb7022020-09-14 16:47:00 +08005399 kfree(sm_info);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09005400}
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09005401
Chao Yu4d57b862018-05-30 00:20:41 +08005402int __init f2fs_create_segment_manager_caches(void)
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09005403{
Chao Yu98510002020-02-17 17:46:20 +08005404 discard_entry_slab = f2fs_kmem_cache_create("f2fs_discard_entry",
Gu Zhenge8512d22014-03-07 18:43:28 +08005405 sizeof(struct discard_entry));
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09005406 if (!discard_entry_slab)
Chao Yu184a5cd2014-09-04 18:13:01 +08005407 goto fail;
5408
Chao Yu98510002020-02-17 17:46:20 +08005409 discard_cmd_slab = f2fs_kmem_cache_create("f2fs_discard_cmd",
Jaegeuk Kimb01a9202017-01-09 14:13:03 -08005410 sizeof(struct discard_cmd));
5411 if (!discard_cmd_slab)
Chao Yu6ab2a302016-09-05 12:28:26 +08005412 goto destroy_discard_entry;
Chao Yu275b66b2016-08-29 23:58:34 +08005413
Chao Yu98510002020-02-17 17:46:20 +08005414 sit_entry_set_slab = f2fs_kmem_cache_create("f2fs_sit_entry_set",
Changman Leec9ee0082014-11-21 15:42:07 +09005415 sizeof(struct sit_entry_set));
Chao Yu184a5cd2014-09-04 18:13:01 +08005416 if (!sit_entry_set_slab)
Jaegeuk Kimb01a9202017-01-09 14:13:03 -08005417 goto destroy_discard_cmd;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07005418
Chao Yu98510002020-02-17 17:46:20 +08005419 inmem_entry_slab = f2fs_kmem_cache_create("f2fs_inmem_page_entry",
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07005420 sizeof(struct inmem_pages));
5421 if (!inmem_entry_slab)
5422 goto destroy_sit_entry_set;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09005423 return 0;
Chao Yu184a5cd2014-09-04 18:13:01 +08005424
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07005425destroy_sit_entry_set:
5426 kmem_cache_destroy(sit_entry_set_slab);
Jaegeuk Kimb01a9202017-01-09 14:13:03 -08005427destroy_discard_cmd:
5428 kmem_cache_destroy(discard_cmd_slab);
Chao Yu6ab2a302016-09-05 12:28:26 +08005429destroy_discard_entry:
Chao Yu184a5cd2014-09-04 18:13:01 +08005430 kmem_cache_destroy(discard_entry_slab);
5431fail:
5432 return -ENOMEM;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09005433}
5434
Chao Yu4d57b862018-05-30 00:20:41 +08005435void f2fs_destroy_segment_manager_caches(void)
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09005436{
Chao Yu184a5cd2014-09-04 18:13:01 +08005437 kmem_cache_destroy(sit_entry_set_slab);
Jaegeuk Kimb01a9202017-01-09 14:13:03 -08005438 kmem_cache_destroy(discard_cmd_slab);
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09005439 kmem_cache_destroy(discard_entry_slab);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07005440 kmem_cache_destroy(inmem_entry_slab);
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09005441}