Chao Yu | 7c1a000 | 2018-09-12 09:16:07 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 2 | /* |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3 | * fs/f2fs/segment.c |
| 4 | * |
| 5 | * Copyright (c) 2012 Samsung Electronics Co., Ltd. |
| 6 | * http://www.samsung.com/ |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 7 | */ |
| 8 | #include <linux/fs.h> |
| 9 | #include <linux/f2fs_fs.h> |
| 10 | #include <linux/bio.h> |
| 11 | #include <linux/blkdev.h> |
NeilBrown | 4034247 | 2022-01-14 14:07:14 -0800 | [diff] [blame] | 12 | #include <linux/sched/mm.h> |
Geert Uytterhoeven | 690e4a3 | 2012-12-19 22:19:30 +0100 | [diff] [blame] | 13 | #include <linux/prefetch.h> |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 14 | #include <linux/kthread.h> |
Chao Yu | 74de593 | 2013-11-22 09:09:59 +0800 | [diff] [blame] | 15 | #include <linux/swap.h> |
Jaegeuk Kim | 60b99b4 | 2015-10-05 14:49:57 -0700 | [diff] [blame] | 16 | #include <linux/timer.h> |
Jaegeuk Kim | 1d7be27 | 2017-05-17 10:36:58 -0700 | [diff] [blame] | 17 | #include <linux/freezer.h> |
Jaegeuk Kim | 1eb1ef4 | 2017-09-09 12:03:23 -0700 | [diff] [blame] | 18 | #include <linux/sched/signal.h> |
Daeho Jeong | 6691d94 | 2021-09-29 11:12:03 -0700 | [diff] [blame] | 19 | #include <linux/random.h> |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 20 | |
| 21 | #include "f2fs.h" |
| 22 | #include "segment.h" |
| 23 | #include "node.h" |
Jaegeuk Kim | 5f65654 | 2017-08-15 21:27:19 -0700 | [diff] [blame] | 24 | #include "gc.h" |
Daeho Jeong | 5211874 | 2021-08-19 20:52:28 -0700 | [diff] [blame] | 25 | #include "iostat.h" |
Namjae Jeon | 6ec178d | 2013-04-23 17:51:43 +0900 | [diff] [blame] | 26 | #include <trace/events/f2fs.h> |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 27 | |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 28 | #define __reverse_ffz(x) __reverse_ffs(~(x)) |
| 29 | |
Jaegeuk Kim | 7fd9e54 | 2013-11-15 13:55:58 +0900 | [diff] [blame] | 30 | static struct kmem_cache *discard_entry_slab; |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 31 | static struct kmem_cache *discard_cmd_slab; |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 32 | static struct kmem_cache *sit_entry_set_slab; |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 33 | static struct kmem_cache *inmem_entry_slab; |
Jaegeuk Kim | 7fd9e54 | 2013-11-15 13:55:58 +0900 | [diff] [blame] | 34 | |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 35 | static unsigned long __reverse_ulong(unsigned char *str) |
| 36 | { |
| 37 | unsigned long tmp = 0; |
| 38 | int shift = 24, idx = 0; |
| 39 | |
| 40 | #if BITS_PER_LONG == 64 |
| 41 | shift = 56; |
| 42 | #endif |
| 43 | while (shift >= 0) { |
| 44 | tmp |= (unsigned long)str[idx++] << shift; |
| 45 | shift -= BITS_PER_BYTE; |
| 46 | } |
| 47 | return tmp; |
| 48 | } |
| 49 | |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 50 | /* |
| 51 | * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since |
| 52 | * MSB and LSB are reversed in a byte by f2fs_set_bit. |
| 53 | */ |
| 54 | static inline unsigned long __reverse_ffs(unsigned long word) |
| 55 | { |
| 56 | int num = 0; |
| 57 | |
| 58 | #if BITS_PER_LONG == 64 |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 59 | if ((word & 0xffffffff00000000UL) == 0) |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 60 | num += 32; |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 61 | else |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 62 | word >>= 32; |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 63 | #endif |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 64 | if ((word & 0xffff0000) == 0) |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 65 | num += 16; |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 66 | else |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 67 | word >>= 16; |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 68 | |
| 69 | if ((word & 0xff00) == 0) |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 70 | num += 8; |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 71 | else |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 72 | word >>= 8; |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 73 | |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 74 | if ((word & 0xf0) == 0) |
| 75 | num += 4; |
| 76 | else |
| 77 | word >>= 4; |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 78 | |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 79 | if ((word & 0xc) == 0) |
| 80 | num += 2; |
| 81 | else |
| 82 | word >>= 2; |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 83 | |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 84 | if ((word & 0x2) == 0) |
| 85 | num += 1; |
| 86 | return num; |
| 87 | } |
| 88 | |
| 89 | /* |
arter97 | e1c4204 | 2014-08-06 23:22:50 +0900 | [diff] [blame] | 90 | * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c because |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 91 | * f2fs_set_bit makes MSB and LSB reversed in a byte. |
Fan Li | 692223d | 2015-11-12 08:43:04 +0800 | [diff] [blame] | 92 | * @size must be integral times of unsigned long. |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 93 | * Example: |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 94 | * MSB <--> LSB |
| 95 | * f2fs_set_bit(0, bitmap) => 1000 0000 |
| 96 | * f2fs_set_bit(7, bitmap) => 0000 0001 |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 97 | */ |
| 98 | static unsigned long __find_rev_next_bit(const unsigned long *addr, |
| 99 | unsigned long size, unsigned long offset) |
| 100 | { |
| 101 | const unsigned long *p = addr + BIT_WORD(offset); |
Fan Li | 692223d | 2015-11-12 08:43:04 +0800 | [diff] [blame] | 102 | unsigned long result = size; |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 103 | unsigned long tmp; |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 104 | |
| 105 | if (offset >= size) |
| 106 | return size; |
| 107 | |
Fan Li | 692223d | 2015-11-12 08:43:04 +0800 | [diff] [blame] | 108 | size -= (offset & ~(BITS_PER_LONG - 1)); |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 109 | offset %= BITS_PER_LONG; |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 110 | |
Fan Li | 692223d | 2015-11-12 08:43:04 +0800 | [diff] [blame] | 111 | while (1) { |
| 112 | if (*p == 0) |
| 113 | goto pass; |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 114 | |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 115 | tmp = __reverse_ulong((unsigned char *)p); |
Fan Li | 692223d | 2015-11-12 08:43:04 +0800 | [diff] [blame] | 116 | |
| 117 | tmp &= ~0UL >> offset; |
| 118 | if (size < BITS_PER_LONG) |
| 119 | tmp &= (~0UL << (BITS_PER_LONG - size)); |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 120 | if (tmp) |
Fan Li | 692223d | 2015-11-12 08:43:04 +0800 | [diff] [blame] | 121 | goto found; |
| 122 | pass: |
| 123 | if (size <= BITS_PER_LONG) |
| 124 | break; |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 125 | size -= BITS_PER_LONG; |
Fan Li | 692223d | 2015-11-12 08:43:04 +0800 | [diff] [blame] | 126 | offset = 0; |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 127 | p++; |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 128 | } |
Fan Li | 692223d | 2015-11-12 08:43:04 +0800 | [diff] [blame] | 129 | return result; |
| 130 | found: |
| 131 | return result - size + __reverse_ffs(tmp); |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | static unsigned long __find_rev_next_zero_bit(const unsigned long *addr, |
| 135 | unsigned long size, unsigned long offset) |
| 136 | { |
| 137 | const unsigned long *p = addr + BIT_WORD(offset); |
Jaegeuk Kim | 8060944 | 2015-12-04 16:51:13 -0800 | [diff] [blame] | 138 | unsigned long result = size; |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 139 | unsigned long tmp; |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 140 | |
| 141 | if (offset >= size) |
| 142 | return size; |
| 143 | |
Jaegeuk Kim | 8060944 | 2015-12-04 16:51:13 -0800 | [diff] [blame] | 144 | size -= (offset & ~(BITS_PER_LONG - 1)); |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 145 | offset %= BITS_PER_LONG; |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 146 | |
Jaegeuk Kim | 8060944 | 2015-12-04 16:51:13 -0800 | [diff] [blame] | 147 | while (1) { |
| 148 | if (*p == ~0UL) |
| 149 | goto pass; |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 150 | |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 151 | tmp = __reverse_ulong((unsigned char *)p); |
Jaegeuk Kim | 8060944 | 2015-12-04 16:51:13 -0800 | [diff] [blame] | 152 | |
| 153 | if (offset) |
| 154 | tmp |= ~0UL << (BITS_PER_LONG - offset); |
| 155 | if (size < BITS_PER_LONG) |
| 156 | tmp |= ~0UL >> size; |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 157 | if (tmp != ~0UL) |
Jaegeuk Kim | 8060944 | 2015-12-04 16:51:13 -0800 | [diff] [blame] | 158 | goto found; |
| 159 | pass: |
| 160 | if (size <= BITS_PER_LONG) |
| 161 | break; |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 162 | size -= BITS_PER_LONG; |
Jaegeuk Kim | 8060944 | 2015-12-04 16:51:13 -0800 | [diff] [blame] | 163 | offset = 0; |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 164 | p++; |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 165 | } |
Jaegeuk Kim | 8060944 | 2015-12-04 16:51:13 -0800 | [diff] [blame] | 166 | return result; |
| 167 | found: |
| 168 | return result - size + __reverse_ffz(tmp); |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 169 | } |
| 170 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 171 | bool f2fs_need_SSR(struct f2fs_sb_info *sbi) |
Jaegeuk Kim | b3a97a2 | 2017-09-09 11:11:04 -0700 | [diff] [blame] | 172 | { |
| 173 | int node_secs = get_blocktype_secs(sbi, F2FS_DIRTY_NODES); |
| 174 | int dent_secs = get_blocktype_secs(sbi, F2FS_DIRTY_DENTS); |
| 175 | int imeta_secs = get_blocktype_secs(sbi, F2FS_DIRTY_IMETA); |
| 176 | |
Chao Yu | b0332a0 | 2020-02-14 17:44:12 +0800 | [diff] [blame] | 177 | if (f2fs_lfs_mode(sbi)) |
Jaegeuk Kim | b3a97a2 | 2017-09-09 11:11:04 -0700 | [diff] [blame] | 178 | return false; |
Daeho Jeong | 0e5e811 | 2020-07-02 13:14:14 +0900 | [diff] [blame] | 179 | if (sbi->gc_mode == GC_URGENT_HIGH) |
Jaegeuk Kim | b3a97a2 | 2017-09-09 11:11:04 -0700 | [diff] [blame] | 180 | return true; |
Daniel Rosenberg | 4354994 | 2018-08-20 19:21:43 -0700 | [diff] [blame] | 181 | if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) |
| 182 | return true; |
Jaegeuk Kim | b3a97a2 | 2017-09-09 11:11:04 -0700 | [diff] [blame] | 183 | |
| 184 | return free_sections(sbi) <= (node_secs + 2 * dent_secs + imeta_secs + |
Chao Yu | a2a12b6 | 2017-10-28 16:52:33 +0800 | [diff] [blame] | 185 | SM_I(sbi)->min_ssr_sections + reserved_sections(sbi)); |
Jaegeuk Kim | b3a97a2 | 2017-09-09 11:11:04 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 188 | void f2fs_register_inmem_page(struct inode *inode, struct page *page) |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 189 | { |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 190 | struct inmem_pages *new; |
Jaegeuk Kim | 9be32d7 | 2014-12-05 10:39:49 -0800 | [diff] [blame] | 191 | |
Chao Yu | b763f3b | 2021-04-28 17:20:31 +0800 | [diff] [blame] | 192 | set_page_private_atomic(page); |
Chao Yu | decd36b | 2015-08-07 18:42:09 +0800 | [diff] [blame] | 193 | |
Chao Yu | 3241057 | 2021-08-09 08:24:48 +0800 | [diff] [blame] | 194 | new = f2fs_kmem_cache_alloc(inmem_entry_slab, |
| 195 | GFP_NOFS, true, NULL); |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 196 | |
| 197 | /* add atomic page indices to the list */ |
| 198 | new->page = page; |
| 199 | INIT_LIST_HEAD(&new->list); |
Chao Yu | decd36b | 2015-08-07 18:42:09 +0800 | [diff] [blame] | 200 | |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 201 | /* increase reference count with clean state */ |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 202 | get_page(page); |
Jaegeuk Kim | 743b620 | 2019-09-09 13:10:59 +0100 | [diff] [blame] | 203 | mutex_lock(&F2FS_I(inode)->inmem_lock); |
| 204 | list_add_tail(&new->list, &F2FS_I(inode)->inmem_pages); |
Jaegeuk Kim | 8dcf2ff7 | 2014-12-05 17:18:15 -0800 | [diff] [blame] | 205 | inc_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES); |
Jaegeuk Kim | 743b620 | 2019-09-09 13:10:59 +0100 | [diff] [blame] | 206 | mutex_unlock(&F2FS_I(inode)->inmem_lock); |
Jaegeuk Kim | 8ce67cb | 2015-03-17 17:58:08 -0700 | [diff] [blame] | 207 | |
| 208 | trace_f2fs_register_inmem_page(page, INMEM); |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 211 | static int __revoke_inmem_pages(struct inode *inode, |
Chao Yu | 4843298 | 2019-02-25 17:11:03 +0800 | [diff] [blame] | 212 | struct list_head *head, bool drop, bool recover, |
| 213 | bool trylock) |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 214 | { |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 215 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 216 | struct inmem_pages *cur, *tmp; |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 217 | int err = 0; |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 218 | |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 219 | list_for_each_entry_safe(cur, tmp, head, list) { |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 220 | struct page *page = cur->page; |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 221 | |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 222 | if (drop) |
| 223 | trace_f2fs_commit_inmem_page(page, INMEM_DROP); |
| 224 | |
Chao Yu | 4843298 | 2019-02-25 17:11:03 +0800 | [diff] [blame] | 225 | if (trylock) { |
| 226 | /* |
| 227 | * to avoid deadlock in between page lock and |
| 228 | * inmem_lock. |
| 229 | */ |
| 230 | if (!trylock_page(page)) |
| 231 | continue; |
| 232 | } else { |
| 233 | lock_page(page); |
| 234 | } |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 235 | |
Chao Yu | bae0ee7 | 2018-12-25 17:43:42 +0800 | [diff] [blame] | 236 | f2fs_wait_on_page_writeback(page, DATA, true, true); |
Chao Yu | e5e5732 | 2018-04-23 10:36:13 +0800 | [diff] [blame] | 237 | |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 238 | if (recover) { |
| 239 | struct dnode_of_data dn; |
| 240 | struct node_info ni; |
| 241 | |
| 242 | trace_f2fs_commit_inmem_page(page, INMEM_REVOKE); |
Chao Yu | 7f2b4e8 | 2017-08-08 19:09:08 +0800 | [diff] [blame] | 243 | retry: |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 244 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 245 | err = f2fs_get_dnode_of_data(&dn, page->index, |
| 246 | LOOKUP_NODE); |
Chao Yu | 7f2b4e8 | 2017-08-08 19:09:08 +0800 | [diff] [blame] | 247 | if (err) { |
| 248 | if (err == -ENOMEM) { |
NeilBrown | 4034247 | 2022-01-14 14:07:14 -0800 | [diff] [blame] | 249 | memalloc_retry_wait(GFP_NOFS); |
Chao Yu | 7f2b4e8 | 2017-08-08 19:09:08 +0800 | [diff] [blame] | 250 | goto retry; |
| 251 | } |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 252 | err = -EAGAIN; |
| 253 | goto next; |
| 254 | } |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 255 | |
Jaegeuk Kim | a9419b6 | 2021-12-13 14:16:32 -0800 | [diff] [blame] | 256 | err = f2fs_get_node_info(sbi, dn.nid, &ni, false); |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 257 | if (err) { |
| 258 | f2fs_put_dnode(&dn); |
| 259 | return err; |
| 260 | } |
| 261 | |
Daeho Jeong | f1d2564 | 2018-01-10 16:49:10 +0900 | [diff] [blame] | 262 | if (cur->old_addr == NEW_ADDR) { |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 263 | f2fs_invalidate_blocks(sbi, dn.data_blkaddr); |
Daeho Jeong | f1d2564 | 2018-01-10 16:49:10 +0900 | [diff] [blame] | 264 | f2fs_update_data_blkaddr(&dn, NEW_ADDR); |
| 265 | } else |
| 266 | f2fs_replace_block(sbi, &dn, dn.data_blkaddr, |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 267 | cur->old_addr, ni.version, true, true); |
| 268 | f2fs_put_dnode(&dn); |
| 269 | } |
| 270 | next: |
Jaegeuk Kim | 63c52d7 | 2016-04-12 14:11:03 -0700 | [diff] [blame] | 271 | /* we don't need to invalidate this in the sccessful status */ |
Chao Yu | 2baf078 | 2018-07-27 18:15:16 +0800 | [diff] [blame] | 272 | if (drop || recover) { |
Jaegeuk Kim | 63c52d7 | 2016-04-12 14:11:03 -0700 | [diff] [blame] | 273 | ClearPageUptodate(page); |
Chao Yu | b763f3b | 2021-04-28 17:20:31 +0800 | [diff] [blame] | 274 | clear_page_private_gcing(page); |
Chao Yu | 2baf078 | 2018-07-27 18:15:16 +0800 | [diff] [blame] | 275 | } |
Chao Yu | b763f3b | 2021-04-28 17:20:31 +0800 | [diff] [blame] | 276 | detach_page_private(page); |
| 277 | set_page_private(page, 0); |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 278 | f2fs_put_page(page, 1); |
Chao Yu | decd36b | 2015-08-07 18:42:09 +0800 | [diff] [blame] | 279 | |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 280 | list_del(&cur->list); |
| 281 | kmem_cache_free(inmem_entry_slab, cur); |
Jaegeuk Kim | 8dcf2ff7 | 2014-12-05 17:18:15 -0800 | [diff] [blame] | 282 | dec_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES); |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 283 | } |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 284 | return err; |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 285 | } |
| 286 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 287 | void f2fs_drop_inmem_pages_all(struct f2fs_sb_info *sbi, bool gc_failure) |
Jaegeuk Kim | 57864ae | 2017-10-18 19:05:57 -0700 | [diff] [blame] | 288 | { |
| 289 | struct list_head *head = &sbi->inode_list[ATOMIC_FILE]; |
| 290 | struct inode *inode; |
| 291 | struct f2fs_inode_info *fi; |
Sahitya Tummala | 677017d | 2019-11-13 16:01:03 +0530 | [diff] [blame] | 292 | unsigned int count = sbi->atomic_files; |
| 293 | unsigned int looped = 0; |
Jaegeuk Kim | 57864ae | 2017-10-18 19:05:57 -0700 | [diff] [blame] | 294 | next: |
| 295 | spin_lock(&sbi->inode_lock[ATOMIC_FILE]); |
| 296 | if (list_empty(head)) { |
| 297 | spin_unlock(&sbi->inode_lock[ATOMIC_FILE]); |
| 298 | return; |
| 299 | } |
| 300 | fi = list_first_entry(head, struct f2fs_inode_info, inmem_ilist); |
| 301 | inode = igrab(&fi->vfs_inode); |
Sahitya Tummala | 677017d | 2019-11-13 16:01:03 +0530 | [diff] [blame] | 302 | if (inode) |
| 303 | list_move_tail(&fi->inmem_ilist, head); |
Jaegeuk Kim | 57864ae | 2017-10-18 19:05:57 -0700 | [diff] [blame] | 304 | spin_unlock(&sbi->inode_lock[ATOMIC_FILE]); |
| 305 | |
| 306 | if (inode) { |
Chao Yu | 2ef79ec | 2018-05-07 20:28:54 +0800 | [diff] [blame] | 307 | if (gc_failure) { |
Sahitya Tummala | 677017d | 2019-11-13 16:01:03 +0530 | [diff] [blame] | 308 | if (!fi->i_gc_failures[GC_FAILURE_ATOMIC]) |
| 309 | goto skip; |
Chao Yu | 2ef79ec | 2018-05-07 20:28:54 +0800 | [diff] [blame] | 310 | } |
Chao Yu | 2ef79ec | 2018-05-07 20:28:54 +0800 | [diff] [blame] | 311 | set_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST); |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 312 | f2fs_drop_inmem_pages(inode); |
Sahitya Tummala | 677017d | 2019-11-13 16:01:03 +0530 | [diff] [blame] | 313 | skip: |
Jaegeuk Kim | 57864ae | 2017-10-18 19:05:57 -0700 | [diff] [blame] | 314 | iput(inode); |
| 315 | } |
Chao Yu | 5df7731f | 2020-02-17 17:45:44 +0800 | [diff] [blame] | 316 | congestion_wait(BLK_RW_ASYNC, DEFAULT_IO_TIMEOUT); |
Jaegeuk Kim | 57864ae | 2017-10-18 19:05:57 -0700 | [diff] [blame] | 317 | cond_resched(); |
Sahitya Tummala | 677017d | 2019-11-13 16:01:03 +0530 | [diff] [blame] | 318 | if (gc_failure) { |
| 319 | if (++looped >= count) |
| 320 | return; |
| 321 | } |
Jaegeuk Kim | 57864ae | 2017-10-18 19:05:57 -0700 | [diff] [blame] | 322 | goto next; |
| 323 | } |
| 324 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 325 | void f2fs_drop_inmem_pages(struct inode *inode) |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 326 | { |
Jaegeuk Kim | 57864ae | 2017-10-18 19:05:57 -0700 | [diff] [blame] | 327 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 328 | struct f2fs_inode_info *fi = F2FS_I(inode); |
| 329 | |
Yi Zhuang | be1ee45 | 2021-03-31 17:34:14 +0800 | [diff] [blame] | 330 | do { |
Chao Yu | 4843298 | 2019-02-25 17:11:03 +0800 | [diff] [blame] | 331 | mutex_lock(&fi->inmem_lock); |
Yi Zhuang | be1ee45 | 2021-03-31 17:34:14 +0800 | [diff] [blame] | 332 | if (list_empty(&fi->inmem_pages)) { |
| 333 | fi->i_gc_failures[GC_FAILURE_ATOMIC] = 0; |
| 334 | |
| 335 | spin_lock(&sbi->inode_lock[ATOMIC_FILE]); |
| 336 | if (!list_empty(&fi->inmem_ilist)) |
| 337 | list_del_init(&fi->inmem_ilist); |
| 338 | if (f2fs_is_atomic_file(inode)) { |
| 339 | clear_inode_flag(inode, FI_ATOMIC_FILE); |
| 340 | sbi->atomic_files--; |
| 341 | } |
| 342 | spin_unlock(&sbi->inode_lock[ATOMIC_FILE]); |
| 343 | |
| 344 | mutex_unlock(&fi->inmem_lock); |
| 345 | break; |
| 346 | } |
Chao Yu | 4843298 | 2019-02-25 17:11:03 +0800 | [diff] [blame] | 347 | __revoke_inmem_pages(inode, &fi->inmem_pages, |
| 348 | true, false, true); |
Chao Yu | 4843298 | 2019-02-25 17:11:03 +0800 | [diff] [blame] | 349 | mutex_unlock(&fi->inmem_lock); |
Yi Zhuang | be1ee45 | 2021-03-31 17:34:14 +0800 | [diff] [blame] | 350 | } while (1); |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 351 | } |
| 352 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 353 | void f2fs_drop_inmem_page(struct inode *inode, struct page *page) |
Jaegeuk Kim | 8c242db | 2017-03-17 09:55:52 +0800 | [diff] [blame] | 354 | { |
| 355 | struct f2fs_inode_info *fi = F2FS_I(inode); |
| 356 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 357 | struct list_head *head = &fi->inmem_pages; |
| 358 | struct inmem_pages *cur = NULL; |
| 359 | |
Chao Yu | b763f3b | 2021-04-28 17:20:31 +0800 | [diff] [blame] | 360 | f2fs_bug_on(sbi, !page_private_atomic(page)); |
Jaegeuk Kim | 8c242db | 2017-03-17 09:55:52 +0800 | [diff] [blame] | 361 | |
| 362 | mutex_lock(&fi->inmem_lock); |
| 363 | list_for_each_entry(cur, head, list) { |
| 364 | if (cur->page == page) |
| 365 | break; |
| 366 | } |
| 367 | |
Sheng Yong | d0891e8 | 2018-04-17 17:12:27 +0800 | [diff] [blame] | 368 | f2fs_bug_on(sbi, list_empty(head) || cur->page != page); |
Jaegeuk Kim | 8c242db | 2017-03-17 09:55:52 +0800 | [diff] [blame] | 369 | list_del(&cur->list); |
| 370 | mutex_unlock(&fi->inmem_lock); |
| 371 | |
| 372 | dec_page_count(sbi, F2FS_INMEM_PAGES); |
| 373 | kmem_cache_free(inmem_entry_slab, cur); |
| 374 | |
| 375 | ClearPageUptodate(page); |
Chao Yu | b763f3b | 2021-04-28 17:20:31 +0800 | [diff] [blame] | 376 | clear_page_private_atomic(page); |
Jaegeuk Kim | 8c242db | 2017-03-17 09:55:52 +0800 | [diff] [blame] | 377 | f2fs_put_page(page, 0); |
| 378 | |
Chao Yu | b763f3b | 2021-04-28 17:20:31 +0800 | [diff] [blame] | 379 | detach_page_private(page); |
| 380 | set_page_private(page, 0); |
| 381 | |
Jaegeuk Kim | 8c242db | 2017-03-17 09:55:52 +0800 | [diff] [blame] | 382 | trace_f2fs_commit_inmem_page(page, INMEM_INVALIDATE); |
| 383 | } |
| 384 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 385 | static int __f2fs_commit_inmem_pages(struct inode *inode) |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 386 | { |
| 387 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 388 | struct f2fs_inode_info *fi = F2FS_I(inode); |
| 389 | struct inmem_pages *cur, *tmp; |
| 390 | struct f2fs_io_info fio = { |
| 391 | .sbi = sbi, |
Chao Yu | 39d787b | 2017-09-29 13:59:38 +0800 | [diff] [blame] | 392 | .ino = inode->i_ino, |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 393 | .type = DATA, |
Mike Christie | 04d328d | 2016-06-05 14:31:55 -0500 | [diff] [blame] | 394 | .op = REQ_OP_WRITE, |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 395 | .op_flags = REQ_SYNC | REQ_PRIO, |
Chao Yu | b0af6d4 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 396 | .io_type = FS_DATA_IO, |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 397 | }; |
Chao Yu | cf52b27 | 2018-04-23 10:36:14 +0800 | [diff] [blame] | 398 | struct list_head revoke_list; |
Chao Yu | bab475c | 2018-09-27 23:41:16 +0800 | [diff] [blame] | 399 | bool submit_bio = false; |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 400 | int err = 0; |
| 401 | |
Chao Yu | cf52b27 | 2018-04-23 10:36:14 +0800 | [diff] [blame] | 402 | INIT_LIST_HEAD(&revoke_list); |
| 403 | |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 404 | list_for_each_entry_safe(cur, tmp, &fi->inmem_pages, list) { |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 405 | struct page *page = cur->page; |
| 406 | |
| 407 | lock_page(page); |
| 408 | if (page->mapping == inode->i_mapping) { |
| 409 | trace_f2fs_commit_inmem_page(page, INMEM); |
| 410 | |
Chao Yu | bae0ee7 | 2018-12-25 17:43:42 +0800 | [diff] [blame] | 411 | f2fs_wait_on_page_writeback(page, DATA, true, true); |
Chao Yu | 8d64d36 | 2018-12-12 18:12:30 +0800 | [diff] [blame] | 412 | |
| 413 | set_page_dirty(page); |
Chao Yu | 933439c | 2016-10-11 22:57:01 +0800 | [diff] [blame] | 414 | if (clear_page_dirty_for_io(page)) { |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 415 | inode_dec_dirty_pages(inode); |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 416 | f2fs_remove_dirty_inode(inode); |
Chao Yu | 933439c | 2016-10-11 22:57:01 +0800 | [diff] [blame] | 417 | } |
Jaegeuk Kim | 640cc18 | 2017-07-19 10:59:55 -0700 | [diff] [blame] | 418 | retry: |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 419 | fio.page = page; |
Hou Pengyang | e959c8f | 2017-04-25 12:45:13 +0000 | [diff] [blame] | 420 | fio.old_blkaddr = NULL_ADDR; |
Jaegeuk Kim | 4d97807 | 2017-04-26 11:11:12 -0700 | [diff] [blame] | 421 | fio.encrypted_page = NULL; |
Jaegeuk Kim | cc15620 | 2017-05-12 13:51:34 -0700 | [diff] [blame] | 422 | fio.need_lock = LOCK_DONE; |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 423 | err = f2fs_do_write_data_page(&fio); |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 424 | if (err) { |
Jaegeuk Kim | 640cc18 | 2017-07-19 10:59:55 -0700 | [diff] [blame] | 425 | if (err == -ENOMEM) { |
NeilBrown | 4034247 | 2022-01-14 14:07:14 -0800 | [diff] [blame] | 426 | memalloc_retry_wait(GFP_NOFS); |
Jaegeuk Kim | 640cc18 | 2017-07-19 10:59:55 -0700 | [diff] [blame] | 427 | goto retry; |
| 428 | } |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 429 | unlock_page(page); |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 430 | break; |
| 431 | } |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 432 | /* record old blkaddr for revoking */ |
| 433 | cur->old_addr = fio.old_blkaddr; |
Chao Yu | bab475c | 2018-09-27 23:41:16 +0800 | [diff] [blame] | 434 | submit_bio = true; |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 435 | } |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 436 | unlock_page(page); |
Chao Yu | cf52b27 | 2018-04-23 10:36:14 +0800 | [diff] [blame] | 437 | list_move_tail(&cur->list, &revoke_list); |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 438 | } |
| 439 | |
Chao Yu | bab475c | 2018-09-27 23:41:16 +0800 | [diff] [blame] | 440 | if (submit_bio) |
| 441 | f2fs_submit_merged_write_cond(sbi, inode, NULL, 0, DATA); |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 442 | |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 443 | if (err) { |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 444 | /* |
| 445 | * try to revoke all committed pages, but still we could fail |
| 446 | * due to no memory or other reason, if that happened, EAGAIN |
| 447 | * will be returned, which means in such case, transaction is |
| 448 | * already not integrity, caller should use journal to do the |
| 449 | * recovery or rewrite & commit last transaction. For other |
| 450 | * error number, revoking was done by filesystem itself. |
| 451 | */ |
Chao Yu | 4843298 | 2019-02-25 17:11:03 +0800 | [diff] [blame] | 452 | err = __revoke_inmem_pages(inode, &revoke_list, |
| 453 | false, true, false); |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 454 | |
| 455 | /* drop all uncommitted pages */ |
Chao Yu | 4843298 | 2019-02-25 17:11:03 +0800 | [diff] [blame] | 456 | __revoke_inmem_pages(inode, &fi->inmem_pages, |
| 457 | true, false, false); |
Chao Yu | cf52b27 | 2018-04-23 10:36:14 +0800 | [diff] [blame] | 458 | } else { |
Chao Yu | 4843298 | 2019-02-25 17:11:03 +0800 | [diff] [blame] | 459 | __revoke_inmem_pages(inode, &revoke_list, |
| 460 | false, false, false); |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 461 | } |
Chao Yu | cf52b27 | 2018-04-23 10:36:14 +0800 | [diff] [blame] | 462 | |
| 463 | return err; |
| 464 | } |
| 465 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 466 | int f2fs_commit_inmem_pages(struct inode *inode) |
Chao Yu | cf52b27 | 2018-04-23 10:36:14 +0800 | [diff] [blame] | 467 | { |
| 468 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 469 | struct f2fs_inode_info *fi = F2FS_I(inode); |
| 470 | int err; |
| 471 | |
| 472 | f2fs_balance_fs(sbi, true); |
Chao Yu | cf52b27 | 2018-04-23 10:36:14 +0800 | [diff] [blame] | 473 | |
Jaegeuk Kim | 6f8d445 | 2018-07-25 12:11:56 +0900 | [diff] [blame] | 474 | down_write(&fi->i_gc_rwsem[WRITE]); |
| 475 | |
| 476 | f2fs_lock_op(sbi); |
Chao Yu | cf52b27 | 2018-04-23 10:36:14 +0800 | [diff] [blame] | 477 | set_inode_flag(inode, FI_ATOMIC_COMMIT); |
| 478 | |
| 479 | mutex_lock(&fi->inmem_lock); |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 480 | err = __f2fs_commit_inmem_pages(inode); |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 481 | mutex_unlock(&fi->inmem_lock); |
| 482 | |
Chao Yu | 5fe4574 | 2017-01-07 18:50:26 +0800 | [diff] [blame] | 483 | clear_inode_flag(inode, FI_ATOMIC_COMMIT); |
| 484 | |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 485 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 6f8d445 | 2018-07-25 12:11:56 +0900 | [diff] [blame] | 486 | up_write(&fi->i_gc_rwsem[WRITE]); |
| 487 | |
Jaegeuk Kim | edb27de | 2015-07-25 00:52:52 -0700 | [diff] [blame] | 488 | return err; |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 489 | } |
| 490 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 491 | /* |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 492 | * This function balances dirty node and dentry pages. |
| 493 | * In addition, it controls garbage collection. |
| 494 | */ |
Jaegeuk Kim | 2c4db1a | 2016-01-07 14:15:04 -0800 | [diff] [blame] | 495 | void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 496 | { |
Chao Yu | 5552351 | 2017-02-25 11:08:28 +0800 | [diff] [blame] | 497 | if (time_to_inject(sbi, FAULT_CHECKPOINT)) { |
Chao Yu | c45d600 | 2019-11-01 17:53:23 +0800 | [diff] [blame] | 498 | f2fs_show_injection_info(sbi, FAULT_CHECKPOINT); |
Chao Yu | 0f34802 | 2016-09-26 19:45:55 +0800 | [diff] [blame] | 499 | f2fs_stop_checkpoint(sbi, false); |
Chao Yu | 5552351 | 2017-02-25 11:08:28 +0800 | [diff] [blame] | 500 | } |
Chao Yu | 0f34802 | 2016-09-26 19:45:55 +0800 | [diff] [blame] | 501 | |
Jaegeuk Kim | e589c2c | 2016-06-02 15:24:24 -0700 | [diff] [blame] | 502 | /* balance_fs_bg is able to be pending */ |
Jaegeuk Kim | a788189 | 2017-04-20 13:51:57 -0700 | [diff] [blame] | 503 | if (need && excess_cached_nats(sbi)) |
Chao Yu | 7bcd0cf | 2020-03-19 19:57:58 +0800 | [diff] [blame] | 504 | f2fs_balance_fs_bg(sbi, false); |
Jaegeuk Kim | e589c2c | 2016-06-02 15:24:24 -0700 | [diff] [blame] | 505 | |
Chao Yu | 00e09c0 | 2019-08-23 17:58:36 +0800 | [diff] [blame] | 506 | if (!f2fs_is_checkpoint_ready(sbi)) |
Daniel Rosenberg | 4354994 | 2018-08-20 19:21:43 -0700 | [diff] [blame] | 507 | return; |
| 508 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 509 | /* |
Jaegeuk Kim | 029cd28 | 2012-12-21 17:20:21 +0900 | [diff] [blame] | 510 | * We should do GC or end up with checkpoint, if there are so many dirty |
| 511 | * dir/node pages without enough free segments. |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 512 | */ |
Jaegeuk Kim | 7f3037a | 2016-09-01 12:02:51 -0700 | [diff] [blame] | 513 | if (has_not_enough_free_secs(sbi, 0, 0)) { |
Chao Yu | 5911d2d | 2021-03-27 17:57:06 +0800 | [diff] [blame] | 514 | if (test_opt(sbi, GC_MERGE) && sbi->gc_thread && |
| 515 | sbi->gc_thread->f2fs_gc_task) { |
| 516 | DEFINE_WAIT(wait); |
| 517 | |
| 518 | prepare_to_wait(&sbi->gc_thread->fggc_wq, &wait, |
| 519 | TASK_UNINTERRUPTIBLE); |
| 520 | wake_up(&sbi->gc_thread->gc_wait_queue_head); |
| 521 | io_schedule(); |
| 522 | finish_wait(&sbi->gc_thread->fggc_wq, &wait); |
| 523 | } else { |
| 524 | down_write(&sbi->gc_lock); |
| 525 | f2fs_gc(sbi, false, false, false, NULL_SEGNO); |
| 526 | } |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | |
Chao Yu | 287b140 | 2021-09-16 17:09:03 +0800 | [diff] [blame] | 530 | static inline bool excess_dirty_threshold(struct f2fs_sb_info *sbi) |
| 531 | { |
| 532 | int factor = rwsem_is_locked(&sbi->cp_rwsem) ? 3 : 2; |
| 533 | unsigned int dents = get_pages(sbi, F2FS_DIRTY_DENTS); |
| 534 | unsigned int qdata = get_pages(sbi, F2FS_DIRTY_QDATA); |
| 535 | unsigned int nodes = get_pages(sbi, F2FS_DIRTY_NODES); |
| 536 | unsigned int meta = get_pages(sbi, F2FS_DIRTY_META); |
| 537 | unsigned int imeta = get_pages(sbi, F2FS_DIRTY_IMETA); |
| 538 | unsigned int threshold = sbi->blocks_per_seg * factor * |
| 539 | DEFAULT_DIRTY_THRESHOLD; |
| 540 | unsigned int global_threshold = threshold * 3 / 2; |
| 541 | |
| 542 | if (dents >= threshold || qdata >= threshold || |
| 543 | nodes >= threshold || meta >= threshold || |
| 544 | imeta >= threshold) |
| 545 | return true; |
| 546 | return dents + qdata + nodes + meta + imeta > global_threshold; |
| 547 | } |
| 548 | |
Chao Yu | 7bcd0cf | 2020-03-19 19:57:58 +0800 | [diff] [blame] | 549 | void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi, bool from_bg) |
Jaegeuk Kim | 4660f9c | 2013-10-24 14:19:18 +0900 | [diff] [blame] | 550 | { |
Chao Yu | 64c74a7 | 2018-05-26 18:03:34 +0800 | [diff] [blame] | 551 | if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) |
| 552 | return; |
| 553 | |
Chao Yu | 1dcc336 | 2015-02-05 17:57:31 +0800 | [diff] [blame] | 554 | /* try to shrink extent cache when there is no enough memory */ |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 555 | if (!f2fs_available_free_memory(sbi, EXTENT_CACHE)) |
Jaegeuk Kim | 554df79 | 2015-06-19 13:41:23 -0700 | [diff] [blame] | 556 | f2fs_shrink_extent_tree(sbi, EXTENT_CACHE_SHRINK_NUMBER); |
Chao Yu | 1dcc336 | 2015-02-05 17:57:31 +0800 | [diff] [blame] | 557 | |
Jaegeuk Kim | 1b38dc8 | 2015-06-19 15:36:07 -0700 | [diff] [blame] | 558 | /* check the # of cached NAT entries */ |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 559 | if (!f2fs_available_free_memory(sbi, NAT_ENTRIES)) |
| 560 | f2fs_try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK); |
Jaegeuk Kim | 1b38dc8 | 2015-06-19 15:36:07 -0700 | [diff] [blame] | 561 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 562 | if (!f2fs_available_free_memory(sbi, FREE_NIDS)) |
| 563 | f2fs_try_to_free_nids(sbi, MAX_FREE_NIDS); |
Jaegeuk Kim | ad4edb8 | 2016-06-16 16:41:49 -0700 | [diff] [blame] | 564 | else |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 565 | f2fs_build_free_nids(sbi, false, false); |
Chao Yu | 3169658 | 2015-07-28 18:33:46 +0800 | [diff] [blame] | 566 | |
Chao Yu | 287b140 | 2021-09-16 17:09:03 +0800 | [diff] [blame] | 567 | if (excess_dirty_nats(sbi) || excess_dirty_threshold(sbi) || |
| 568 | excess_prefree_segs(sbi) || !f2fs_space_for_roll_forward(sbi)) |
Chao Yu | 493720a | 2020-11-25 10:57:36 +0800 | [diff] [blame] | 569 | goto do_sync; |
| 570 | |
| 571 | /* there is background inflight IO or foreground operation recently */ |
| 572 | if (is_inflight_io(sbi, REQ_TIME) || |
| 573 | (!f2fs_time_over(sbi, REQ_TIME) && rwsem_is_locked(&sbi->cp_rwsem))) |
Jaegeuk Kim | f455c8a | 2016-12-05 11:37:14 -0800 | [diff] [blame] | 574 | return; |
Jaegeuk Kim | e5e7ea3 | 2014-11-06 15:24:46 -0800 | [diff] [blame] | 575 | |
Chao Yu | 493720a | 2020-11-25 10:57:36 +0800 | [diff] [blame] | 576 | /* exceed periodical checkpoint timeout threshold */ |
| 577 | if (f2fs_time_over(sbi, CP_TIME)) |
| 578 | goto do_sync; |
| 579 | |
Jaegeuk Kim | 4660f9c | 2013-10-24 14:19:18 +0900 | [diff] [blame] | 580 | /* checkpoint is the only way to shrink partial cached entries */ |
Chao Yu | cd6d697a6 | 2021-09-29 03:19:14 +0800 | [diff] [blame] | 581 | if (f2fs_available_free_memory(sbi, NAT_ENTRIES) && |
Chao Yu | 493720a | 2020-11-25 10:57:36 +0800 | [diff] [blame] | 582 | f2fs_available_free_memory(sbi, INO_ENTRIES)) |
| 583 | return; |
Chao Yu | e9f5b8b | 2016-02-14 18:54:33 +0800 | [diff] [blame] | 584 | |
Chao Yu | 493720a | 2020-11-25 10:57:36 +0800 | [diff] [blame] | 585 | do_sync: |
| 586 | if (test_opt(sbi, DATA_FLUSH) && from_bg) { |
| 587 | struct blk_plug plug; |
Chao Yu | 040d2bb | 2019-05-20 17:36:59 +0800 | [diff] [blame] | 588 | |
Chao Yu | 493720a | 2020-11-25 10:57:36 +0800 | [diff] [blame] | 589 | mutex_lock(&sbi->flush_lock); |
Chao Yu | 040d2bb | 2019-05-20 17:36:59 +0800 | [diff] [blame] | 590 | |
Chao Yu | 493720a | 2020-11-25 10:57:36 +0800 | [diff] [blame] | 591 | blk_start_plug(&plug); |
| 592 | f2fs_sync_dirty_inodes(sbi, FILE_INODE); |
| 593 | blk_finish_plug(&plug); |
| 594 | |
| 595 | mutex_unlock(&sbi->flush_lock); |
Chao Yu | 36b35a0 | 2015-12-17 17:13:28 +0800 | [diff] [blame] | 596 | } |
Chao Yu | 493720a | 2020-11-25 10:57:36 +0800 | [diff] [blame] | 597 | f2fs_sync_fs(sbi->sb, true); |
| 598 | stat_inc_bg_cp_count(sbi->stat_info); |
Jaegeuk Kim | 4660f9c | 2013-10-24 14:19:18 +0900 | [diff] [blame] | 599 | } |
| 600 | |
Kinglong Mee | 20fda56b | 2017-03-04 22:13:10 +0800 | [diff] [blame] | 601 | static int __submit_flush_wait(struct f2fs_sb_info *sbi, |
| 602 | struct block_device *bdev) |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 603 | { |
Christoph Hellwig | 25ac842 | 2021-01-26 15:52:37 +0100 | [diff] [blame] | 604 | int ret = blkdev_issue_flush(bdev); |
Kinglong Mee | 20fda56b | 2017-03-04 22:13:10 +0800 | [diff] [blame] | 605 | |
| 606 | trace_f2fs_issue_flush(bdev, test_opt(sbi, NOBARRIER), |
| 607 | test_opt(sbi, FLUSH_MERGE), ret); |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 608 | return ret; |
| 609 | } |
| 610 | |
Chao Yu | 39d787b | 2017-09-29 13:59:38 +0800 | [diff] [blame] | 611 | static int submit_flush_wait(struct f2fs_sb_info *sbi, nid_t ino) |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 612 | { |
Chao Yu | 39d787b | 2017-09-29 13:59:38 +0800 | [diff] [blame] | 613 | int ret = 0; |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 614 | int i; |
| 615 | |
Damien Le Moal | 0916878 | 2019-03-16 09:13:06 +0900 | [diff] [blame] | 616 | if (!f2fs_is_multi_device(sbi)) |
Chao Yu | 39d787b | 2017-09-29 13:59:38 +0800 | [diff] [blame] | 617 | return __submit_flush_wait(sbi, sbi->sb->s_bdev); |
Kinglong Mee | 20fda56b | 2017-03-04 22:13:10 +0800 | [diff] [blame] | 618 | |
Chao Yu | 39d787b | 2017-09-29 13:59:38 +0800 | [diff] [blame] | 619 | for (i = 0; i < sbi->s_ndevs; i++) { |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 620 | if (!f2fs_is_dirty_device(sbi, ino, i, FLUSH_INO)) |
Chao Yu | 39d787b | 2017-09-29 13:59:38 +0800 | [diff] [blame] | 621 | continue; |
Kinglong Mee | 20fda56b | 2017-03-04 22:13:10 +0800 | [diff] [blame] | 622 | ret = __submit_flush_wait(sbi, FDEV(i).bdev); |
| 623 | if (ret) |
| 624 | break; |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 625 | } |
| 626 | return ret; |
| 627 | } |
| 628 | |
Gu Zheng | 2163d19 | 2014-04-27 14:21:33 +0800 | [diff] [blame] | 629 | static int issue_flush_thread(void *data) |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 630 | { |
| 631 | struct f2fs_sb_info *sbi = data; |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 632 | struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info; |
Gu Zheng | a688b9d9e | 2014-04-27 14:21:21 +0800 | [diff] [blame] | 633 | wait_queue_head_t *q = &fcc->flush_wait_queue; |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 634 | repeat: |
| 635 | if (kthread_should_stop()) |
| 636 | return 0; |
| 637 | |
Gu Zheng | 721bd4d | 2014-09-05 18:31:00 +0800 | [diff] [blame] | 638 | if (!llist_empty(&fcc->issue_list)) { |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 639 | struct flush_cmd *cmd, *next; |
| 640 | int ret; |
| 641 | |
Gu Zheng | 721bd4d | 2014-09-05 18:31:00 +0800 | [diff] [blame] | 642 | fcc->dispatch_list = llist_del_all(&fcc->issue_list); |
| 643 | fcc->dispatch_list = llist_reverse_order(fcc->dispatch_list); |
| 644 | |
Chao Yu | 39d787b | 2017-09-29 13:59:38 +0800 | [diff] [blame] | 645 | cmd = llist_entry(fcc->dispatch_list, struct flush_cmd, llnode); |
| 646 | |
| 647 | ret = submit_flush_wait(sbi, cmd->ino); |
Chao Yu | 8b8dd65 | 2017-03-25 17:19:58 +0800 | [diff] [blame] | 648 | atomic_inc(&fcc->issued_flush); |
| 649 | |
Gu Zheng | 721bd4d | 2014-09-05 18:31:00 +0800 | [diff] [blame] | 650 | llist_for_each_entry_safe(cmd, next, |
| 651 | fcc->dispatch_list, llnode) { |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 652 | cmd->ret = ret; |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 653 | complete(&cmd->wait); |
| 654 | } |
Gu Zheng | a688b9d9e | 2014-04-27 14:21:21 +0800 | [diff] [blame] | 655 | fcc->dispatch_list = NULL; |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 656 | } |
| 657 | |
Gu Zheng | a688b9d9e | 2014-04-27 14:21:21 +0800 | [diff] [blame] | 658 | wait_event_interruptible(*q, |
Gu Zheng | 721bd4d | 2014-09-05 18:31:00 +0800 | [diff] [blame] | 659 | kthread_should_stop() || !llist_empty(&fcc->issue_list)); |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 660 | goto repeat; |
| 661 | } |
| 662 | |
Chao Yu | 39d787b | 2017-09-29 13:59:38 +0800 | [diff] [blame] | 663 | int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino) |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 664 | { |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 665 | struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info; |
Chao Yu | adf8d90 | 2014-05-08 17:00:35 +0800 | [diff] [blame] | 666 | struct flush_cmd cmd; |
Chao Yu | 8b8dd65 | 2017-03-25 17:19:58 +0800 | [diff] [blame] | 667 | int ret; |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 668 | |
Jaegeuk Kim | 0f7b2ab | 2014-07-23 09:57:31 -0700 | [diff] [blame] | 669 | if (test_opt(sbi, NOBARRIER)) |
| 670 | return 0; |
| 671 | |
Chao Yu | 8b8dd65 | 2017-03-25 17:19:58 +0800 | [diff] [blame] | 672 | if (!test_opt(sbi, FLUSH_MERGE)) { |
Jaegeuk Kim | 72691af | 2018-12-13 16:53:57 -0800 | [diff] [blame] | 673 | atomic_inc(&fcc->queued_flush); |
Chao Yu | 39d787b | 2017-09-29 13:59:38 +0800 | [diff] [blame] | 674 | ret = submit_flush_wait(sbi, ino); |
Jaegeuk Kim | 72691af | 2018-12-13 16:53:57 -0800 | [diff] [blame] | 675 | atomic_dec(&fcc->queued_flush); |
Chao Yu | 8b8dd65 | 2017-03-25 17:19:58 +0800 | [diff] [blame] | 676 | atomic_inc(&fcc->issued_flush); |
| 677 | return ret; |
| 678 | } |
| 679 | |
Damien Le Moal | 0916878 | 2019-03-16 09:13:06 +0900 | [diff] [blame] | 680 | if (atomic_inc_return(&fcc->queued_flush) == 1 || |
| 681 | f2fs_is_multi_device(sbi)) { |
Chao Yu | 39d787b | 2017-09-29 13:59:38 +0800 | [diff] [blame] | 682 | ret = submit_flush_wait(sbi, ino); |
Jaegeuk Kim | 72691af | 2018-12-13 16:53:57 -0800 | [diff] [blame] | 683 | atomic_dec(&fcc->queued_flush); |
Chao Yu | 8b8dd65 | 2017-03-25 17:19:58 +0800 | [diff] [blame] | 684 | |
| 685 | atomic_inc(&fcc->issued_flush); |
Jaegeuk Kim | 740432f | 2015-08-14 11:43:56 -0700 | [diff] [blame] | 686 | return ret; |
| 687 | } |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 688 | |
Chao Yu | 39d787b | 2017-09-29 13:59:38 +0800 | [diff] [blame] | 689 | cmd.ino = ino; |
Chao Yu | adf8d90 | 2014-05-08 17:00:35 +0800 | [diff] [blame] | 690 | init_completion(&cmd.wait); |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 691 | |
Gu Zheng | 721bd4d | 2014-09-05 18:31:00 +0800 | [diff] [blame] | 692 | llist_add(&cmd.llnode, &fcc->issue_list); |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 693 | |
Chao Yu | 3b42c74 | 2021-02-20 17:38:43 +0800 | [diff] [blame] | 694 | /* |
| 695 | * update issue_list before we wake up issue_flush thread, this |
| 696 | * smp_mb() pairs with another barrier in ___wait_event(), see |
| 697 | * more details in comments of waitqueue_active(). |
| 698 | */ |
Chao Yu | 6f890df | 2017-08-21 22:53:45 +0800 | [diff] [blame] | 699 | smp_mb(); |
| 700 | |
| 701 | if (waitqueue_active(&fcc->flush_wait_queue)) |
Gu Zheng | a688b9d9e | 2014-04-27 14:21:21 +0800 | [diff] [blame] | 702 | wake_up(&fcc->flush_wait_queue); |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 703 | |
Jaegeuk Kim | 5eba8c5 | 2016-12-07 16:23:32 -0800 | [diff] [blame] | 704 | if (fcc->f2fs_issue_flush) { |
| 705 | wait_for_completion(&cmd.wait); |
Jaegeuk Kim | 72691af | 2018-12-13 16:53:57 -0800 | [diff] [blame] | 706 | atomic_dec(&fcc->queued_flush); |
Jaegeuk Kim | 5eba8c5 | 2016-12-07 16:23:32 -0800 | [diff] [blame] | 707 | } else { |
Chao Yu | d323869 | 2017-08-31 18:56:06 +0800 | [diff] [blame] | 708 | struct llist_node *list; |
| 709 | |
| 710 | list = llist_del_all(&fcc->issue_list); |
| 711 | if (!list) { |
| 712 | wait_for_completion(&cmd.wait); |
Jaegeuk Kim | 72691af | 2018-12-13 16:53:57 -0800 | [diff] [blame] | 713 | atomic_dec(&fcc->queued_flush); |
Chao Yu | d323869 | 2017-08-31 18:56:06 +0800 | [diff] [blame] | 714 | } else { |
| 715 | struct flush_cmd *tmp, *next; |
| 716 | |
Chao Yu | 39d787b | 2017-09-29 13:59:38 +0800 | [diff] [blame] | 717 | ret = submit_flush_wait(sbi, ino); |
Chao Yu | d323869 | 2017-08-31 18:56:06 +0800 | [diff] [blame] | 718 | |
| 719 | llist_for_each_entry_safe(tmp, next, list, llnode) { |
| 720 | if (tmp == &cmd) { |
| 721 | cmd.ret = ret; |
Jaegeuk Kim | 72691af | 2018-12-13 16:53:57 -0800 | [diff] [blame] | 722 | atomic_dec(&fcc->queued_flush); |
Chao Yu | d323869 | 2017-08-31 18:56:06 +0800 | [diff] [blame] | 723 | continue; |
| 724 | } |
| 725 | tmp->ret = ret; |
| 726 | complete(&tmp->wait); |
| 727 | } |
| 728 | } |
Jaegeuk Kim | 5eba8c5 | 2016-12-07 16:23:32 -0800 | [diff] [blame] | 729 | } |
Chao Yu | adf8d90 | 2014-05-08 17:00:35 +0800 | [diff] [blame] | 730 | |
| 731 | return cmd.ret; |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 732 | } |
| 733 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 734 | int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi) |
Gu Zheng | 2163d19 | 2014-04-27 14:21:33 +0800 | [diff] [blame] | 735 | { |
| 736 | dev_t dev = sbi->sb->s_bdev->bd_dev; |
| 737 | struct flush_cmd_control *fcc; |
| 738 | int err = 0; |
| 739 | |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 740 | if (SM_I(sbi)->fcc_info) { |
| 741 | fcc = SM_I(sbi)->fcc_info; |
Yunlong Song | d871cd0 | 2017-06-24 15:57:19 +0800 | [diff] [blame] | 742 | if (fcc->f2fs_issue_flush) |
| 743 | return err; |
Jaegeuk Kim | 5eba8c5 | 2016-12-07 16:23:32 -0800 | [diff] [blame] | 744 | goto init_thread; |
| 745 | } |
| 746 | |
Chao Yu | acbf054 | 2017-11-30 19:28:17 +0800 | [diff] [blame] | 747 | fcc = f2fs_kzalloc(sbi, sizeof(struct flush_cmd_control), GFP_KERNEL); |
Gu Zheng | 2163d19 | 2014-04-27 14:21:33 +0800 | [diff] [blame] | 748 | if (!fcc) |
| 749 | return -ENOMEM; |
Chao Yu | 8b8dd65 | 2017-03-25 17:19:58 +0800 | [diff] [blame] | 750 | atomic_set(&fcc->issued_flush, 0); |
Jaegeuk Kim | 72691af | 2018-12-13 16:53:57 -0800 | [diff] [blame] | 751 | atomic_set(&fcc->queued_flush, 0); |
Gu Zheng | 2163d19 | 2014-04-27 14:21:33 +0800 | [diff] [blame] | 752 | init_waitqueue_head(&fcc->flush_wait_queue); |
Gu Zheng | 721bd4d | 2014-09-05 18:31:00 +0800 | [diff] [blame] | 753 | init_llist_head(&fcc->issue_list); |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 754 | SM_I(sbi)->fcc_info = fcc; |
Yunlei He | d4fdf8b | 2017-06-01 16:43:51 +0800 | [diff] [blame] | 755 | if (!test_opt(sbi, FLUSH_MERGE)) |
| 756 | return err; |
| 757 | |
Jaegeuk Kim | 5eba8c5 | 2016-12-07 16:23:32 -0800 | [diff] [blame] | 758 | init_thread: |
Gu Zheng | 2163d19 | 2014-04-27 14:21:33 +0800 | [diff] [blame] | 759 | fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi, |
| 760 | "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev)); |
| 761 | if (IS_ERR(fcc->f2fs_issue_flush)) { |
| 762 | err = PTR_ERR(fcc->f2fs_issue_flush); |
Chao Yu | c8eb702 | 2020-09-14 16:47:00 +0800 | [diff] [blame] | 763 | kfree(fcc); |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 764 | SM_I(sbi)->fcc_info = NULL; |
Gu Zheng | 2163d19 | 2014-04-27 14:21:33 +0800 | [diff] [blame] | 765 | return err; |
| 766 | } |
Gu Zheng | 2163d19 | 2014-04-27 14:21:33 +0800 | [diff] [blame] | 767 | |
| 768 | return err; |
| 769 | } |
| 770 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 771 | void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free) |
Gu Zheng | 2163d19 | 2014-04-27 14:21:33 +0800 | [diff] [blame] | 772 | { |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 773 | struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info; |
Gu Zheng | 2163d19 | 2014-04-27 14:21:33 +0800 | [diff] [blame] | 774 | |
Jaegeuk Kim | 5eba8c5 | 2016-12-07 16:23:32 -0800 | [diff] [blame] | 775 | if (fcc && fcc->f2fs_issue_flush) { |
| 776 | struct task_struct *flush_thread = fcc->f2fs_issue_flush; |
| 777 | |
| 778 | fcc->f2fs_issue_flush = NULL; |
| 779 | kthread_stop(flush_thread); |
| 780 | } |
| 781 | if (free) { |
Chao Yu | c8eb702 | 2020-09-14 16:47:00 +0800 | [diff] [blame] | 782 | kfree(fcc); |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 783 | SM_I(sbi)->fcc_info = NULL; |
Jaegeuk Kim | 5eba8c5 | 2016-12-07 16:23:32 -0800 | [diff] [blame] | 784 | } |
Gu Zheng | 2163d19 | 2014-04-27 14:21:33 +0800 | [diff] [blame] | 785 | } |
| 786 | |
Chao Yu | 1228b48 | 2017-09-29 13:59:39 +0800 | [diff] [blame] | 787 | int f2fs_flush_device_cache(struct f2fs_sb_info *sbi) |
| 788 | { |
| 789 | int ret = 0, i; |
| 790 | |
Damien Le Moal | 0916878 | 2019-03-16 09:13:06 +0900 | [diff] [blame] | 791 | if (!f2fs_is_multi_device(sbi)) |
Chao Yu | 1228b48 | 2017-09-29 13:59:39 +0800 | [diff] [blame] | 792 | return 0; |
| 793 | |
Chao Yu | 6ed29fe | 2020-10-12 10:28:14 +0800 | [diff] [blame] | 794 | if (test_opt(sbi, NOBARRIER)) |
| 795 | return 0; |
| 796 | |
Chao Yu | 1228b48 | 2017-09-29 13:59:39 +0800 | [diff] [blame] | 797 | for (i = 1; i < sbi->s_ndevs; i++) { |
Chao Yu | 9180339 | 2021-08-04 08:38:38 +0800 | [diff] [blame] | 798 | int count = DEFAULT_RETRY_IO_COUNT; |
| 799 | |
Chao Yu | 1228b48 | 2017-09-29 13:59:39 +0800 | [diff] [blame] | 800 | if (!f2fs_test_bit(i, (char *)&sbi->dirty_device)) |
| 801 | continue; |
Chao Yu | 9180339 | 2021-08-04 08:38:38 +0800 | [diff] [blame] | 802 | |
| 803 | do { |
| 804 | ret = __submit_flush_wait(sbi, FDEV(i).bdev); |
| 805 | if (ret) |
| 806 | congestion_wait(BLK_RW_ASYNC, |
| 807 | DEFAULT_IO_TIMEOUT); |
| 808 | } while (ret && --count); |
| 809 | |
| 810 | if (ret) { |
| 811 | f2fs_stop_checkpoint(sbi, false); |
Chao Yu | 1228b48 | 2017-09-29 13:59:39 +0800 | [diff] [blame] | 812 | break; |
Chao Yu | 9180339 | 2021-08-04 08:38:38 +0800 | [diff] [blame] | 813 | } |
Chao Yu | 1228b48 | 2017-09-29 13:59:39 +0800 | [diff] [blame] | 814 | |
| 815 | spin_lock(&sbi->dev_lock); |
| 816 | f2fs_clear_bit(i, (char *)&sbi->dirty_device); |
| 817 | spin_unlock(&sbi->dev_lock); |
| 818 | } |
| 819 | |
| 820 | return ret; |
| 821 | } |
| 822 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 823 | static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno, |
| 824 | enum dirty_type dirty_type) |
| 825 | { |
| 826 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
| 827 | |
| 828 | /* need not be added */ |
| 829 | if (IS_CURSEG(sbi, segno)) |
| 830 | return; |
| 831 | |
| 832 | if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type])) |
| 833 | dirty_i->nr_dirty[dirty_type]++; |
| 834 | |
| 835 | if (dirty_type == DIRTY) { |
| 836 | struct seg_entry *sentry = get_seg_entry(sbi, segno); |
Changman Lee | 4625d6a | 2013-10-25 17:31:57 +0900 | [diff] [blame] | 837 | enum dirty_type t = sentry->type; |
Jaegeuk Kim | b2f2c39 | 2013-04-01 13:52:09 +0900 | [diff] [blame] | 838 | |
Jaegeuk Kim | ec325b5 | 2014-09-02 16:24:11 -0700 | [diff] [blame] | 839 | if (unlikely(t >= DIRTY)) { |
| 840 | f2fs_bug_on(sbi, 1); |
| 841 | return; |
| 842 | } |
Changman Lee | 4625d6a | 2013-10-25 17:31:57 +0900 | [diff] [blame] | 843 | if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t])) |
| 844 | dirty_i->nr_dirty[t]++; |
Jack Qiu | da52f8a | 2020-06-18 12:37:10 +0800 | [diff] [blame] | 845 | |
| 846 | if (__is_large_section(sbi)) { |
| 847 | unsigned int secno = GET_SEC_FROM_SEG(sbi, segno); |
Shin'ichiro Kawasaki | 123aaf7 | 2020-08-19 10:34:48 +0900 | [diff] [blame] | 848 | block_t valid_blocks = |
Jack Qiu | da52f8a | 2020-06-18 12:37:10 +0800 | [diff] [blame] | 849 | get_valid_blocks(sbi, segno, true); |
| 850 | |
| 851 | f2fs_bug_on(sbi, unlikely(!valid_blocks || |
| 852 | valid_blocks == BLKS_PER_SEC(sbi))); |
| 853 | |
| 854 | if (!IS_CURSEC(sbi, secno)) |
| 855 | set_bit(secno, dirty_i->dirty_secmap); |
| 856 | } |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 857 | } |
| 858 | } |
| 859 | |
| 860 | static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno, |
| 861 | enum dirty_type dirty_type) |
| 862 | { |
| 863 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
Shin'ichiro Kawasaki | 123aaf7 | 2020-08-19 10:34:48 +0900 | [diff] [blame] | 864 | block_t valid_blocks; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 865 | |
| 866 | if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type])) |
| 867 | dirty_i->nr_dirty[dirty_type]--; |
| 868 | |
| 869 | if (dirty_type == DIRTY) { |
Changman Lee | 4625d6a | 2013-10-25 17:31:57 +0900 | [diff] [blame] | 870 | struct seg_entry *sentry = get_seg_entry(sbi, segno); |
| 871 | enum dirty_type t = sentry->type; |
Jaegeuk Kim | b2f2c39 | 2013-04-01 13:52:09 +0900 | [diff] [blame] | 872 | |
Changman Lee | 4625d6a | 2013-10-25 17:31:57 +0900 | [diff] [blame] | 873 | if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t])) |
| 874 | dirty_i->nr_dirty[t]--; |
Jaegeuk Kim | b2f2c39 | 2013-04-01 13:52:09 +0900 | [diff] [blame] | 875 | |
Jack Qiu | da52f8a | 2020-06-18 12:37:10 +0800 | [diff] [blame] | 876 | valid_blocks = get_valid_blocks(sbi, segno, true); |
| 877 | if (valid_blocks == 0) { |
Jaegeuk Kim | 4ddb1a4 | 2017-04-07 15:08:17 -0700 | [diff] [blame] | 878 | clear_bit(GET_SEC_FROM_SEG(sbi, segno), |
Jaegeuk Kim | 5ec4e49 | 2013-03-31 13:26:03 +0900 | [diff] [blame] | 879 | dirty_i->victim_secmap); |
Sahitya Tummala | bbf9f7d | 2019-08-07 19:10:32 +0530 | [diff] [blame] | 880 | #ifdef CONFIG_F2FS_CHECK_FS |
| 881 | clear_bit(segno, SIT_I(sbi)->invalid_segmap); |
| 882 | #endif |
| 883 | } |
Jack Qiu | da52f8a | 2020-06-18 12:37:10 +0800 | [diff] [blame] | 884 | if (__is_large_section(sbi)) { |
| 885 | unsigned int secno = GET_SEC_FROM_SEG(sbi, segno); |
| 886 | |
| 887 | if (!valid_blocks || |
| 888 | valid_blocks == BLKS_PER_SEC(sbi)) { |
| 889 | clear_bit(secno, dirty_i->dirty_secmap); |
| 890 | return; |
| 891 | } |
| 892 | |
| 893 | if (!IS_CURSEC(sbi, secno)) |
| 894 | set_bit(secno, dirty_i->dirty_secmap); |
| 895 | } |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 896 | } |
| 897 | } |
| 898 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 899 | /* |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 900 | * Should not occur error such as -ENOMEM. |
| 901 | * Adding dirty entry into seglist is not critical operation. |
| 902 | * If a given segment is one of current working segments, it won't be added. |
| 903 | */ |
Haicheng Li | 8d8451a | 2013-06-13 16:59:28 +0800 | [diff] [blame] | 904 | static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 905 | { |
| 906 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
Daniel Rosenberg | 4354994 | 2018-08-20 19:21:43 -0700 | [diff] [blame] | 907 | unsigned short valid_blocks, ckpt_valid_blocks; |
Aravind Ramesh | de881df | 2020-07-16 18:26:56 +0530 | [diff] [blame] | 908 | unsigned int usable_blocks; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 909 | |
| 910 | if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno)) |
| 911 | return; |
| 912 | |
Aravind Ramesh | de881df | 2020-07-16 18:26:56 +0530 | [diff] [blame] | 913 | usable_blocks = f2fs_usable_blks_in_seg(sbi, segno); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 914 | mutex_lock(&dirty_i->seglist_lock); |
| 915 | |
Jaegeuk Kim | 302bd34 | 2017-04-07 14:33:22 -0700 | [diff] [blame] | 916 | valid_blocks = get_valid_blocks(sbi, segno, false); |
Chao Yu | 61461fc | 2021-03-24 11:18:28 +0800 | [diff] [blame] | 917 | ckpt_valid_blocks = get_ckpt_valid_blocks(sbi, segno, false); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 918 | |
Daniel Rosenberg | 4354994 | 2018-08-20 19:21:43 -0700 | [diff] [blame] | 919 | if (valid_blocks == 0 && (!is_sbi_flag_set(sbi, SBI_CP_DISABLED) || |
Aravind Ramesh | de881df | 2020-07-16 18:26:56 +0530 | [diff] [blame] | 920 | ckpt_valid_blocks == usable_blocks)) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 921 | __locate_dirty_segment(sbi, segno, PRE); |
| 922 | __remove_dirty_segment(sbi, segno, DIRTY); |
Aravind Ramesh | de881df | 2020-07-16 18:26:56 +0530 | [diff] [blame] | 923 | } else if (valid_blocks < usable_blocks) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 924 | __locate_dirty_segment(sbi, segno, DIRTY); |
| 925 | } else { |
| 926 | /* Recovery routine with SSR needs this */ |
| 927 | __remove_dirty_segment(sbi, segno, DIRTY); |
| 928 | } |
| 929 | |
| 930 | mutex_unlock(&dirty_i->seglist_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 931 | } |
| 932 | |
Daniel Rosenberg | 4354994 | 2018-08-20 19:21:43 -0700 | [diff] [blame] | 933 | /* This moves currently empty dirty blocks to prefree. Must hold seglist_lock */ |
| 934 | void f2fs_dirty_to_prefree(struct f2fs_sb_info *sbi) |
| 935 | { |
| 936 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
| 937 | unsigned int segno; |
| 938 | |
| 939 | mutex_lock(&dirty_i->seglist_lock); |
| 940 | for_each_set_bit(segno, dirty_i->dirty_segmap[DIRTY], MAIN_SEGS(sbi)) { |
| 941 | if (get_valid_blocks(sbi, segno, false)) |
| 942 | continue; |
| 943 | if (IS_CURSEG(sbi, segno)) |
| 944 | continue; |
| 945 | __locate_dirty_segment(sbi, segno, PRE); |
| 946 | __remove_dirty_segment(sbi, segno, DIRTY); |
| 947 | } |
| 948 | mutex_unlock(&dirty_i->seglist_lock); |
| 949 | } |
| 950 | |
Daniel Rosenberg | 4d3aed7 | 2019-05-29 17:49:06 -0700 | [diff] [blame] | 951 | block_t f2fs_get_unusable_blocks(struct f2fs_sb_info *sbi) |
Daniel Rosenberg | 4354994 | 2018-08-20 19:21:43 -0700 | [diff] [blame] | 952 | { |
Daniel Rosenberg | ae4ad7e | 2019-05-29 17:49:03 -0700 | [diff] [blame] | 953 | int ovp_hole_segs = |
| 954 | (overprovision_segments(sbi) - reserved_segments(sbi)); |
| 955 | block_t ovp_holes = ovp_hole_segs << sbi->log_blocks_per_seg; |
Daniel Rosenberg | 4d3aed7 | 2019-05-29 17:49:06 -0700 | [diff] [blame] | 956 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
Daniel Rosenberg | 4354994 | 2018-08-20 19:21:43 -0700 | [diff] [blame] | 957 | block_t holes[2] = {0, 0}; /* DATA and NODE */ |
Daniel Rosenberg | 4d3aed7 | 2019-05-29 17:49:06 -0700 | [diff] [blame] | 958 | block_t unusable; |
Daniel Rosenberg | 4354994 | 2018-08-20 19:21:43 -0700 | [diff] [blame] | 959 | struct seg_entry *se; |
| 960 | unsigned int segno; |
| 961 | |
| 962 | mutex_lock(&dirty_i->seglist_lock); |
| 963 | for_each_set_bit(segno, dirty_i->dirty_segmap[DIRTY], MAIN_SEGS(sbi)) { |
| 964 | se = get_seg_entry(sbi, segno); |
| 965 | if (IS_NODESEG(se->type)) |
Aravind Ramesh | de881df | 2020-07-16 18:26:56 +0530 | [diff] [blame] | 966 | holes[NODE] += f2fs_usable_blks_in_seg(sbi, segno) - |
| 967 | se->valid_blocks; |
Daniel Rosenberg | 4354994 | 2018-08-20 19:21:43 -0700 | [diff] [blame] | 968 | else |
Aravind Ramesh | de881df | 2020-07-16 18:26:56 +0530 | [diff] [blame] | 969 | holes[DATA] += f2fs_usable_blks_in_seg(sbi, segno) - |
| 970 | se->valid_blocks; |
Daniel Rosenberg | 4354994 | 2018-08-20 19:21:43 -0700 | [diff] [blame] | 971 | } |
| 972 | mutex_unlock(&dirty_i->seglist_lock); |
| 973 | |
Daniel Rosenberg | 4d3aed7 | 2019-05-29 17:49:06 -0700 | [diff] [blame] | 974 | unusable = holes[DATA] > holes[NODE] ? holes[DATA] : holes[NODE]; |
| 975 | if (unusable > ovp_holes) |
| 976 | return unusable - ovp_holes; |
| 977 | return 0; |
| 978 | } |
| 979 | |
| 980 | int f2fs_disable_cp_again(struct f2fs_sb_info *sbi, block_t unusable) |
| 981 | { |
| 982 | int ovp_hole_segs = |
| 983 | (overprovision_segments(sbi) - reserved_segments(sbi)); |
| 984 | if (unusable > F2FS_OPTION(sbi).unusable_cap) |
Daniel Rosenberg | 4354994 | 2018-08-20 19:21:43 -0700 | [diff] [blame] | 985 | return -EAGAIN; |
Jaegeuk Kim | db610a6 | 2019-01-24 17:48:38 -0800 | [diff] [blame] | 986 | if (is_sbi_flag_set(sbi, SBI_CP_DISABLED_QUICK) && |
Daniel Rosenberg | ae4ad7e | 2019-05-29 17:49:03 -0700 | [diff] [blame] | 987 | dirty_segments(sbi) > ovp_hole_segs) |
Jaegeuk Kim | db610a6 | 2019-01-24 17:48:38 -0800 | [diff] [blame] | 988 | return -EAGAIN; |
Daniel Rosenberg | 4354994 | 2018-08-20 19:21:43 -0700 | [diff] [blame] | 989 | return 0; |
| 990 | } |
| 991 | |
| 992 | /* This is only used by SBI_CP_DISABLED */ |
| 993 | static unsigned int get_free_segment(struct f2fs_sb_info *sbi) |
| 994 | { |
| 995 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
| 996 | unsigned int segno = 0; |
| 997 | |
| 998 | mutex_lock(&dirty_i->seglist_lock); |
| 999 | for_each_set_bit(segno, dirty_i->dirty_segmap[DIRTY], MAIN_SEGS(sbi)) { |
| 1000 | if (get_valid_blocks(sbi, segno, false)) |
| 1001 | continue; |
Chao Yu | 61461fc | 2021-03-24 11:18:28 +0800 | [diff] [blame] | 1002 | if (get_ckpt_valid_blocks(sbi, segno, false)) |
Daniel Rosenberg | 4354994 | 2018-08-20 19:21:43 -0700 | [diff] [blame] | 1003 | continue; |
| 1004 | mutex_unlock(&dirty_i->seglist_lock); |
| 1005 | return segno; |
| 1006 | } |
| 1007 | mutex_unlock(&dirty_i->seglist_lock); |
| 1008 | return NULL_SEGNO; |
| 1009 | } |
| 1010 | |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1011 | static struct discard_cmd *__create_discard_cmd(struct f2fs_sb_info *sbi, |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 1012 | struct block_device *bdev, block_t lstart, |
| 1013 | block_t start, block_t len) |
Chao Yu | 275b66b | 2016-08-29 23:58:34 +0800 | [diff] [blame] | 1014 | { |
Jaegeuk Kim | 0b54fb8 | 2017-01-11 14:40:24 -0800 | [diff] [blame] | 1015 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
Chao Yu | ba48a33 | 2017-04-15 14:09:37 +0800 | [diff] [blame] | 1016 | struct list_head *pend_list; |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 1017 | struct discard_cmd *dc; |
Chao Yu | 275b66b | 2016-08-29 23:58:34 +0800 | [diff] [blame] | 1018 | |
Chao Yu | ba48a33 | 2017-04-15 14:09:37 +0800 | [diff] [blame] | 1019 | f2fs_bug_on(sbi, !len); |
| 1020 | |
| 1021 | pend_list = &dcc->pend_list[plist_idx(len)]; |
| 1022 | |
Chao Yu | 3241057 | 2021-08-09 08:24:48 +0800 | [diff] [blame] | 1023 | dc = f2fs_kmem_cache_alloc(discard_cmd_slab, GFP_NOFS, true, NULL); |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 1024 | INIT_LIST_HEAD(&dc->list); |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 1025 | dc->bdev = bdev; |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 1026 | dc->lstart = lstart; |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 1027 | dc->start = start; |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 1028 | dc->len = len; |
Chao Yu | ec9895a | 2017-04-26 17:39:54 +0800 | [diff] [blame] | 1029 | dc->ref = 0; |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 1030 | dc->state = D_PREP; |
Jaegeuk Kim | 72691af | 2018-12-13 16:53:57 -0800 | [diff] [blame] | 1031 | dc->queued = 0; |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 1032 | dc->error = 0; |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 1033 | init_completion(&dc->wait); |
Chao Yu | 22d375d | 2017-04-05 18:19:48 +0800 | [diff] [blame] | 1034 | list_add_tail(&dc->list, pend_list); |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 1035 | spin_lock_init(&dc->lock); |
| 1036 | dc->bio_ref = 0; |
Chao Yu | 5f32366 | 2017-03-25 17:19:59 +0800 | [diff] [blame] | 1037 | atomic_inc(&dcc->discard_cmd_cnt); |
Chao Yu | d84d1cb | 2017-04-18 19:27:39 +0800 | [diff] [blame] | 1038 | dcc->undiscard_blks += len; |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1039 | |
| 1040 | return dc; |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 1041 | } |
| 1042 | |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1043 | static struct discard_cmd *__attach_discard_cmd(struct f2fs_sb_info *sbi, |
| 1044 | struct block_device *bdev, block_t lstart, |
| 1045 | block_t start, block_t len, |
Chao Yu | 4dada3f | 2018-10-04 11:18:30 +0800 | [diff] [blame] | 1046 | struct rb_node *parent, struct rb_node **p, |
| 1047 | bool leftmost) |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1048 | { |
| 1049 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
| 1050 | struct discard_cmd *dc; |
| 1051 | |
| 1052 | dc = __create_discard_cmd(sbi, bdev, lstart, start, len); |
| 1053 | |
| 1054 | rb_link_node(&dc->rb_node, parent, p); |
Chao Yu | 4dada3f | 2018-10-04 11:18:30 +0800 | [diff] [blame] | 1055 | rb_insert_color_cached(&dc->rb_node, &dcc->root, leftmost); |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1056 | |
| 1057 | return dc; |
| 1058 | } |
| 1059 | |
| 1060 | static void __detach_discard_cmd(struct discard_cmd_control *dcc, |
| 1061 | struct discard_cmd *dc) |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 1062 | { |
Jaegeuk Kim | dcc9165 | 2017-01-11 10:20:04 -0800 | [diff] [blame] | 1063 | if (dc->state == D_DONE) |
Jaegeuk Kim | 72691af | 2018-12-13 16:53:57 -0800 | [diff] [blame] | 1064 | atomic_sub(dc->queued, &dcc->queued_discard); |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1065 | |
| 1066 | list_del(&dc->list); |
Chao Yu | 4dada3f | 2018-10-04 11:18:30 +0800 | [diff] [blame] | 1067 | rb_erase_cached(&dc->rb_node, &dcc->root); |
Chao Yu | d84d1cb | 2017-04-18 19:27:39 +0800 | [diff] [blame] | 1068 | dcc->undiscard_blks -= dc->len; |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1069 | |
| 1070 | kmem_cache_free(discard_cmd_slab, dc); |
| 1071 | |
| 1072 | atomic_dec(&dcc->discard_cmd_cnt); |
| 1073 | } |
| 1074 | |
| 1075 | static void __remove_discard_cmd(struct f2fs_sb_info *sbi, |
| 1076 | struct discard_cmd *dc) |
| 1077 | { |
| 1078 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 1079 | unsigned long flags; |
Jaegeuk Kim | dcc9165 | 2017-01-11 10:20:04 -0800 | [diff] [blame] | 1080 | |
Chao Yu | 2ec6f2e | 2017-10-04 09:08:36 +0800 | [diff] [blame] | 1081 | trace_f2fs_remove_discard(dc->bdev, dc->start, dc->len); |
| 1082 | |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 1083 | spin_lock_irqsave(&dc->lock, flags); |
| 1084 | if (dc->bio_ref) { |
| 1085 | spin_unlock_irqrestore(&dc->lock, flags); |
| 1086 | return; |
| 1087 | } |
| 1088 | spin_unlock_irqrestore(&dc->lock, flags); |
| 1089 | |
Chao Yu | d9703d9 | 2017-06-05 18:29:07 +0800 | [diff] [blame] | 1090 | f2fs_bug_on(sbi, dc->ref); |
| 1091 | |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 1092 | if (dc->error == -EOPNOTSUPP) |
| 1093 | dc->error = 0; |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 1094 | |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 1095 | if (dc->error) |
Chao Yu | 22d7ea1 | 2018-08-22 17:17:47 +0800 | [diff] [blame] | 1096 | printk_ratelimited( |
Chao Yu | c45d600 | 2019-11-01 17:53:23 +0800 | [diff] [blame] | 1097 | "%sF2FS-fs (%s): Issue discard(%u, %u, %u) failed, ret: %d", |
| 1098 | KERN_INFO, sbi->sb->s_id, |
| 1099 | dc->lstart, dc->start, dc->len, dc->error); |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1100 | __detach_discard_cmd(dcc, dc); |
Chao Yu | 275b66b | 2016-08-29 23:58:34 +0800 | [diff] [blame] | 1101 | } |
| 1102 | |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 1103 | static void f2fs_submit_discard_endio(struct bio *bio) |
| 1104 | { |
| 1105 | struct discard_cmd *dc = (struct discard_cmd *)bio->bi_private; |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 1106 | unsigned long flags; |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 1107 | |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 1108 | spin_lock_irqsave(&dc->lock, flags); |
Sahitya Tummala | 3fa6a8c | 2020-04-15 09:35:54 +0530 | [diff] [blame] | 1109 | if (!dc->error) |
| 1110 | dc->error = blk_status_to_errno(bio->bi_status); |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 1111 | dc->bio_ref--; |
| 1112 | if (!dc->bio_ref && dc->state == D_SUBMIT) { |
| 1113 | dc->state = D_DONE; |
| 1114 | complete_all(&dc->wait); |
| 1115 | } |
| 1116 | spin_unlock_irqrestore(&dc->lock, flags); |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 1117 | bio_put(bio); |
| 1118 | } |
| 1119 | |
Wei Yongjun | 94b1e10 | 2018-01-05 09:41:20 +0000 | [diff] [blame] | 1120 | static void __check_sit_bitmap(struct f2fs_sb_info *sbi, |
Chao Yu | 6915ea9 | 2017-06-30 17:19:02 +0800 | [diff] [blame] | 1121 | block_t start, block_t end) |
| 1122 | { |
| 1123 | #ifdef CONFIG_F2FS_CHECK_FS |
| 1124 | struct seg_entry *sentry; |
| 1125 | unsigned int segno; |
| 1126 | block_t blk = start; |
| 1127 | unsigned long offset, size, max_blocks = sbi->blocks_per_seg; |
| 1128 | unsigned long *map; |
| 1129 | |
| 1130 | while (blk < end) { |
| 1131 | segno = GET_SEGNO(sbi, blk); |
| 1132 | sentry = get_seg_entry(sbi, segno); |
| 1133 | offset = GET_BLKOFF_FROM_SEG0(sbi, blk); |
| 1134 | |
Yunlong Song | 008396e | 2017-08-04 17:07:15 +0800 | [diff] [blame] | 1135 | if (end < START_BLOCK(sbi, segno + 1)) |
| 1136 | size = GET_BLKOFF_FROM_SEG0(sbi, end); |
| 1137 | else |
| 1138 | size = max_blocks; |
Chao Yu | 6915ea9 | 2017-06-30 17:19:02 +0800 | [diff] [blame] | 1139 | map = (unsigned long *)(sentry->cur_valid_map); |
| 1140 | offset = __find_rev_next_bit(map, size, offset); |
| 1141 | f2fs_bug_on(sbi, offset != size); |
Yunlong Song | 008396e | 2017-08-04 17:07:15 +0800 | [diff] [blame] | 1142 | blk = START_BLOCK(sbi, segno + 1); |
Chao Yu | 6915ea9 | 2017-06-30 17:19:02 +0800 | [diff] [blame] | 1143 | } |
| 1144 | #endif |
| 1145 | } |
| 1146 | |
Jaegeuk Kim | 8bb4f25 | 2018-05-29 09:58:42 -0700 | [diff] [blame] | 1147 | static void __init_discard_policy(struct f2fs_sb_info *sbi, |
| 1148 | struct discard_policy *dpolicy, |
| 1149 | int discard_type, unsigned int granularity) |
| 1150 | { |
Sahitya Tummala | c35b8d5 | 2021-04-06 14:39:16 +0530 | [diff] [blame] | 1151 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
| 1152 | |
Jaegeuk Kim | 8bb4f25 | 2018-05-29 09:58:42 -0700 | [diff] [blame] | 1153 | /* common policy */ |
| 1154 | dpolicy->type = discard_type; |
| 1155 | dpolicy->sync = true; |
Chao Yu | 20ee438 | 2018-07-08 22:11:01 +0800 | [diff] [blame] | 1156 | dpolicy->ordered = false; |
Jaegeuk Kim | 8bb4f25 | 2018-05-29 09:58:42 -0700 | [diff] [blame] | 1157 | dpolicy->granularity = granularity; |
| 1158 | |
| 1159 | dpolicy->max_requests = DEF_MAX_DISCARD_REQUEST; |
| 1160 | dpolicy->io_aware_gran = MAX_PLIST_NUM; |
Chao Yu | 6ce48b0 | 2020-03-26 17:43:56 +0800 | [diff] [blame] | 1161 | dpolicy->timeout = false; |
Jaegeuk Kim | 8bb4f25 | 2018-05-29 09:58:42 -0700 | [diff] [blame] | 1162 | |
| 1163 | if (discard_type == DPOLICY_BG) { |
| 1164 | dpolicy->min_interval = DEF_MIN_DISCARD_ISSUE_TIME; |
Yunlei He | f9d1dce | 2018-04-08 15:11:11 +0800 | [diff] [blame] | 1165 | dpolicy->mid_interval = DEF_MID_DISCARD_ISSUE_TIME; |
Jaegeuk Kim | 8bb4f25 | 2018-05-29 09:58:42 -0700 | [diff] [blame] | 1166 | dpolicy->max_interval = DEF_MAX_DISCARD_ISSUE_TIME; |
| 1167 | dpolicy->io_aware = true; |
Chao Yu | cba6084 | 2018-04-10 15:43:09 +0800 | [diff] [blame] | 1168 | dpolicy->sync = false; |
Chao Yu | 20ee438 | 2018-07-08 22:11:01 +0800 | [diff] [blame] | 1169 | dpolicy->ordered = true; |
Jaegeuk Kim | 8bb4f25 | 2018-05-29 09:58:42 -0700 | [diff] [blame] | 1170 | if (utilization(sbi) > DEF_DISCARD_URGENT_UTIL) { |
| 1171 | dpolicy->granularity = 1; |
Sahitya Tummala | c35b8d5 | 2021-04-06 14:39:16 +0530 | [diff] [blame] | 1172 | if (atomic_read(&dcc->discard_cmd_cnt)) |
| 1173 | dpolicy->max_interval = |
| 1174 | DEF_MIN_DISCARD_ISSUE_TIME; |
Jaegeuk Kim | 8bb4f25 | 2018-05-29 09:58:42 -0700 | [diff] [blame] | 1175 | } |
| 1176 | } else if (discard_type == DPOLICY_FORCE) { |
| 1177 | dpolicy->min_interval = DEF_MIN_DISCARD_ISSUE_TIME; |
Yunlei He | f9d1dce | 2018-04-08 15:11:11 +0800 | [diff] [blame] | 1178 | dpolicy->mid_interval = DEF_MID_DISCARD_ISSUE_TIME; |
Jaegeuk Kim | 8bb4f25 | 2018-05-29 09:58:42 -0700 | [diff] [blame] | 1179 | dpolicy->max_interval = DEF_MAX_DISCARD_ISSUE_TIME; |
| 1180 | dpolicy->io_aware = false; |
| 1181 | } else if (discard_type == DPOLICY_FSTRIM) { |
| 1182 | dpolicy->io_aware = false; |
| 1183 | } else if (discard_type == DPOLICY_UMOUNT) { |
| 1184 | dpolicy->io_aware = false; |
Jaegeuk Kim | b862325 | 2019-01-25 09:12:13 -0800 | [diff] [blame] | 1185 | /* we need to issue all to keep CP_TRIMMED_FLAG */ |
| 1186 | dpolicy->granularity = 1; |
Chao Yu | 6ce48b0 | 2020-03-26 17:43:56 +0800 | [diff] [blame] | 1187 | dpolicy->timeout = true; |
Jaegeuk Kim | 8bb4f25 | 2018-05-29 09:58:42 -0700 | [diff] [blame] | 1188 | } |
| 1189 | } |
| 1190 | |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 1191 | static void __update_discard_tree_range(struct f2fs_sb_info *sbi, |
| 1192 | struct block_device *bdev, block_t lstart, |
| 1193 | block_t start, block_t len); |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 1194 | /* this function is copied from blkdev_issue_discard from block/blk-lib.c */ |
Chao Yu | 6b9cb12 | 2018-08-08 10:14:55 +0800 | [diff] [blame] | 1195 | static int __submit_discard_cmd(struct f2fs_sb_info *sbi, |
Chao Yu | 78997b5 | 2017-10-04 09:08:34 +0800 | [diff] [blame] | 1196 | struct discard_policy *dpolicy, |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 1197 | struct discard_cmd *dc, |
| 1198 | unsigned int *issued) |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 1199 | { |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 1200 | struct block_device *bdev = dc->bdev; |
| 1201 | struct request_queue *q = bdev_get_queue(bdev); |
| 1202 | unsigned int max_discard_blocks = |
| 1203 | SECTOR_TO_BLOCK(q->limits.max_discard_sectors); |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 1204 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
Chao Yu | 78997b5 | 2017-10-04 09:08:34 +0800 | [diff] [blame] | 1205 | struct list_head *wait_list = (dpolicy->type == DPOLICY_FSTRIM) ? |
| 1206 | &(dcc->fstrim_list) : &(dcc->wait_list); |
Chao Yu | 78997b5 | 2017-10-04 09:08:34 +0800 | [diff] [blame] | 1207 | int flag = dpolicy->sync ? REQ_SYNC : 0; |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 1208 | block_t lstart, start, len, total_len; |
| 1209 | int err = 0; |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 1210 | |
| 1211 | if (dc->state != D_PREP) |
Chao Yu | 6b9cb12 | 2018-08-08 10:14:55 +0800 | [diff] [blame] | 1212 | return 0; |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 1213 | |
Yunlei He | d618477 | 2018-04-13 11:08:05 +0800 | [diff] [blame] | 1214 | if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) |
Chao Yu | 6b9cb12 | 2018-08-08 10:14:55 +0800 | [diff] [blame] | 1215 | return 0; |
Yunlei He | d618477 | 2018-04-13 11:08:05 +0800 | [diff] [blame] | 1216 | |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 1217 | trace_f2fs_issue_discard(bdev, dc->start, dc->len); |
Chao Yu | 0243a5f | 2017-04-15 14:09:38 +0800 | [diff] [blame] | 1218 | |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 1219 | lstart = dc->lstart; |
| 1220 | start = dc->start; |
| 1221 | len = dc->len; |
| 1222 | total_len = len; |
| 1223 | |
| 1224 | dc->len = 0; |
| 1225 | |
| 1226 | while (total_len && *issued < dpolicy->max_requests && !err) { |
| 1227 | struct bio *bio = NULL; |
| 1228 | unsigned long flags; |
| 1229 | bool last = true; |
| 1230 | |
| 1231 | if (len > max_discard_blocks) { |
| 1232 | len = max_discard_blocks; |
| 1233 | last = false; |
| 1234 | } |
| 1235 | |
| 1236 | (*issued)++; |
| 1237 | if (*issued == dpolicy->max_requests) |
| 1238 | last = true; |
| 1239 | |
| 1240 | dc->len += len; |
| 1241 | |
Chao Yu | b83dcfe | 2018-08-06 20:30:18 +0800 | [diff] [blame] | 1242 | if (time_to_inject(sbi, FAULT_DISCARD)) { |
Chao Yu | c45d600 | 2019-11-01 17:53:23 +0800 | [diff] [blame] | 1243 | f2fs_show_injection_info(sbi, FAULT_DISCARD); |
Chao Yu | b83dcfe | 2018-08-06 20:30:18 +0800 | [diff] [blame] | 1244 | err = -EIO; |
| 1245 | goto submit; |
| 1246 | } |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 1247 | err = __blkdev_issue_discard(bdev, |
| 1248 | SECTOR_FROM_BLOCK(start), |
| 1249 | SECTOR_FROM_BLOCK(len), |
| 1250 | GFP_NOFS, 0, &bio); |
Chao Yu | b83dcfe | 2018-08-06 20:30:18 +0800 | [diff] [blame] | 1251 | submit: |
Chao Yu | 6b9cb12 | 2018-08-08 10:14:55 +0800 | [diff] [blame] | 1252 | if (err) { |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 1253 | spin_lock_irqsave(&dc->lock, flags); |
| 1254 | if (dc->state == D_PARTIAL) |
| 1255 | dc->state = D_SUBMIT; |
| 1256 | spin_unlock_irqrestore(&dc->lock, flags); |
| 1257 | |
Chao Yu | 6b9cb12 | 2018-08-08 10:14:55 +0800 | [diff] [blame] | 1258 | break; |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 1259 | } |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 1260 | |
Chao Yu | 6b9cb12 | 2018-08-08 10:14:55 +0800 | [diff] [blame] | 1261 | f2fs_bug_on(sbi, !bio); |
| 1262 | |
| 1263 | /* |
| 1264 | * should keep before submission to avoid D_DONE |
| 1265 | * right away |
| 1266 | */ |
| 1267 | spin_lock_irqsave(&dc->lock, flags); |
| 1268 | if (last) |
| 1269 | dc->state = D_SUBMIT; |
| 1270 | else |
| 1271 | dc->state = D_PARTIAL; |
| 1272 | dc->bio_ref++; |
| 1273 | spin_unlock_irqrestore(&dc->lock, flags); |
| 1274 | |
Jaegeuk Kim | 72691af | 2018-12-13 16:53:57 -0800 | [diff] [blame] | 1275 | atomic_inc(&dcc->queued_discard); |
| 1276 | dc->queued++; |
Chao Yu | 6b9cb12 | 2018-08-08 10:14:55 +0800 | [diff] [blame] | 1277 | list_move_tail(&dc->list, wait_list); |
| 1278 | |
| 1279 | /* sanity check on discard range */ |
Qiuyang Sun | 9249dde | 2018-12-18 17:32:23 +0800 | [diff] [blame] | 1280 | __check_sit_bitmap(sbi, lstart, lstart + len); |
Chao Yu | 6b9cb12 | 2018-08-08 10:14:55 +0800 | [diff] [blame] | 1281 | |
| 1282 | bio->bi_private = dc; |
| 1283 | bio->bi_end_io = f2fs_submit_discard_endio; |
| 1284 | bio->bi_opf |= flag; |
| 1285 | submit_bio(bio); |
| 1286 | |
| 1287 | atomic_inc(&dcc->issued_discard); |
| 1288 | |
| 1289 | f2fs_update_iostat(sbi, FS_DISCARD, 1); |
| 1290 | |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 1291 | lstart += len; |
| 1292 | start += len; |
| 1293 | total_len -= len; |
| 1294 | len = total_len; |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 1295 | } |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 1296 | |
Sahitya Tummala | df42339 | 2020-04-16 11:47:41 +0530 | [diff] [blame] | 1297 | if (!err && len) { |
| 1298 | dcc->undiscard_blks -= len; |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 1299 | __update_discard_tree_range(sbi, bdev, lstart, start, len); |
Sahitya Tummala | df42339 | 2020-04-16 11:47:41 +0530 | [diff] [blame] | 1300 | } |
Chao Yu | 6b9cb12 | 2018-08-08 10:14:55 +0800 | [diff] [blame] | 1301 | return err; |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 1302 | } |
| 1303 | |
Chao Yu | 47d0d7d7 | 2020-05-27 18:27:52 +0800 | [diff] [blame] | 1304 | static void __insert_discard_tree(struct f2fs_sb_info *sbi, |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1305 | struct block_device *bdev, block_t lstart, |
| 1306 | block_t start, block_t len, |
| 1307 | struct rb_node **insert_p, |
| 1308 | struct rb_node *insert_parent) |
| 1309 | { |
| 1310 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
Colin Ian King | dca6951 | 2017-10-19 12:58:21 +0200 | [diff] [blame] | 1311 | struct rb_node **p; |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1312 | struct rb_node *parent = NULL; |
Chao Yu | 4dada3f | 2018-10-04 11:18:30 +0800 | [diff] [blame] | 1313 | bool leftmost = true; |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1314 | |
| 1315 | if (insert_p && insert_parent) { |
| 1316 | parent = insert_parent; |
| 1317 | p = insert_p; |
| 1318 | goto do_insert; |
| 1319 | } |
| 1320 | |
Chao Yu | 4dada3f | 2018-10-04 11:18:30 +0800 | [diff] [blame] | 1321 | p = f2fs_lookup_rb_tree_for_insert(sbi, &dcc->root, &parent, |
| 1322 | lstart, &leftmost); |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1323 | do_insert: |
Chao Yu | 47d0d7d7 | 2020-05-27 18:27:52 +0800 | [diff] [blame] | 1324 | __attach_discard_cmd(sbi, bdev, lstart, start, len, parent, |
Chao Yu | 4dada3f | 2018-10-04 11:18:30 +0800 | [diff] [blame] | 1325 | p, leftmost); |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1326 | } |
| 1327 | |
Chao Yu | ba48a33 | 2017-04-15 14:09:37 +0800 | [diff] [blame] | 1328 | static void __relocate_discard_cmd(struct discard_cmd_control *dcc, |
| 1329 | struct discard_cmd *dc) |
| 1330 | { |
| 1331 | list_move_tail(&dc->list, &dcc->pend_list[plist_idx(dc->len)]); |
| 1332 | } |
| 1333 | |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1334 | static void __punch_discard_cmd(struct f2fs_sb_info *sbi, |
| 1335 | struct discard_cmd *dc, block_t blkaddr) |
| 1336 | { |
Chao Yu | ba48a33 | 2017-04-15 14:09:37 +0800 | [diff] [blame] | 1337 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1338 | struct discard_info di = dc->di; |
| 1339 | bool modified = false; |
| 1340 | |
| 1341 | if (dc->state == D_DONE || dc->len == 1) { |
| 1342 | __remove_discard_cmd(sbi, dc); |
| 1343 | return; |
| 1344 | } |
| 1345 | |
Chao Yu | d84d1cb | 2017-04-18 19:27:39 +0800 | [diff] [blame] | 1346 | dcc->undiscard_blks -= di.len; |
| 1347 | |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1348 | if (blkaddr > di.lstart) { |
| 1349 | dc->len = blkaddr - dc->lstart; |
Chao Yu | d84d1cb | 2017-04-18 19:27:39 +0800 | [diff] [blame] | 1350 | dcc->undiscard_blks += dc->len; |
Chao Yu | ba48a33 | 2017-04-15 14:09:37 +0800 | [diff] [blame] | 1351 | __relocate_discard_cmd(dcc, dc); |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1352 | modified = true; |
| 1353 | } |
| 1354 | |
| 1355 | if (blkaddr < di.lstart + di.len - 1) { |
| 1356 | if (modified) { |
| 1357 | __insert_discard_tree(sbi, dc->bdev, blkaddr + 1, |
| 1358 | di.start + blkaddr + 1 - di.lstart, |
| 1359 | di.lstart + di.len - 1 - blkaddr, |
| 1360 | NULL, NULL); |
| 1361 | } else { |
| 1362 | dc->lstart++; |
| 1363 | dc->len--; |
| 1364 | dc->start++; |
Chao Yu | d84d1cb | 2017-04-18 19:27:39 +0800 | [diff] [blame] | 1365 | dcc->undiscard_blks += dc->len; |
Chao Yu | ba48a33 | 2017-04-15 14:09:37 +0800 | [diff] [blame] | 1366 | __relocate_discard_cmd(dcc, dc); |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1367 | } |
| 1368 | } |
| 1369 | } |
| 1370 | |
| 1371 | static void __update_discard_tree_range(struct f2fs_sb_info *sbi, |
| 1372 | struct block_device *bdev, block_t lstart, |
| 1373 | block_t start, block_t len) |
| 1374 | { |
| 1375 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
| 1376 | struct discard_cmd *prev_dc = NULL, *next_dc = NULL; |
| 1377 | struct discard_cmd *dc; |
| 1378 | struct discard_info di = {0}; |
| 1379 | struct rb_node **insert_p = NULL, *insert_parent = NULL; |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 1380 | struct request_queue *q = bdev_get_queue(bdev); |
| 1381 | unsigned int max_discard_blocks = |
| 1382 | SECTOR_TO_BLOCK(q->limits.max_discard_sectors); |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1383 | block_t end = lstart + len; |
| 1384 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1385 | dc = (struct discard_cmd *)f2fs_lookup_rb_tree_ret(&dcc->root, |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1386 | NULL, lstart, |
| 1387 | (struct rb_entry **)&prev_dc, |
| 1388 | (struct rb_entry **)&next_dc, |
Chao Yu | 4dada3f | 2018-10-04 11:18:30 +0800 | [diff] [blame] | 1389 | &insert_p, &insert_parent, true, NULL); |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1390 | if (dc) |
| 1391 | prev_dc = dc; |
| 1392 | |
| 1393 | if (!prev_dc) { |
| 1394 | di.lstart = lstart; |
| 1395 | di.len = next_dc ? next_dc->lstart - lstart : len; |
| 1396 | di.len = min(di.len, len); |
| 1397 | di.start = start; |
| 1398 | } |
| 1399 | |
| 1400 | while (1) { |
| 1401 | struct rb_node *node; |
| 1402 | bool merged = false; |
| 1403 | struct discard_cmd *tdc = NULL; |
| 1404 | |
| 1405 | if (prev_dc) { |
| 1406 | di.lstart = prev_dc->lstart + prev_dc->len; |
| 1407 | if (di.lstart < lstart) |
| 1408 | di.lstart = lstart; |
| 1409 | if (di.lstart >= end) |
| 1410 | break; |
| 1411 | |
| 1412 | if (!next_dc || next_dc->lstart > end) |
| 1413 | di.len = end - di.lstart; |
| 1414 | else |
| 1415 | di.len = next_dc->lstart - di.lstart; |
| 1416 | di.start = start + di.lstart - lstart; |
| 1417 | } |
| 1418 | |
| 1419 | if (!di.len) |
| 1420 | goto next; |
| 1421 | |
| 1422 | if (prev_dc && prev_dc->state == D_PREP && |
| 1423 | prev_dc->bdev == bdev && |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 1424 | __is_discard_back_mergeable(&di, &prev_dc->di, |
| 1425 | max_discard_blocks)) { |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1426 | prev_dc->di.len += di.len; |
Chao Yu | d84d1cb | 2017-04-18 19:27:39 +0800 | [diff] [blame] | 1427 | dcc->undiscard_blks += di.len; |
Chao Yu | ba48a33 | 2017-04-15 14:09:37 +0800 | [diff] [blame] | 1428 | __relocate_discard_cmd(dcc, prev_dc); |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1429 | di = prev_dc->di; |
| 1430 | tdc = prev_dc; |
| 1431 | merged = true; |
| 1432 | } |
| 1433 | |
| 1434 | if (next_dc && next_dc->state == D_PREP && |
| 1435 | next_dc->bdev == bdev && |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 1436 | __is_discard_front_mergeable(&di, &next_dc->di, |
| 1437 | max_discard_blocks)) { |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1438 | next_dc->di.lstart = di.lstart; |
| 1439 | next_dc->di.len += di.len; |
| 1440 | next_dc->di.start = di.start; |
Chao Yu | d84d1cb | 2017-04-18 19:27:39 +0800 | [diff] [blame] | 1441 | dcc->undiscard_blks += di.len; |
Chao Yu | ba48a33 | 2017-04-15 14:09:37 +0800 | [diff] [blame] | 1442 | __relocate_discard_cmd(dcc, next_dc); |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1443 | if (tdc) |
| 1444 | __remove_discard_cmd(sbi, tdc); |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1445 | merged = true; |
| 1446 | } |
| 1447 | |
Chao Yu | df0f6b4 | 2017-04-17 18:21:43 +0800 | [diff] [blame] | 1448 | if (!merged) { |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1449 | __insert_discard_tree(sbi, bdev, di.lstart, di.start, |
| 1450 | di.len, NULL, NULL); |
Chao Yu | df0f6b4 | 2017-04-17 18:21:43 +0800 | [diff] [blame] | 1451 | } |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1452 | next: |
| 1453 | prev_dc = next_dc; |
| 1454 | if (!prev_dc) |
| 1455 | break; |
| 1456 | |
| 1457 | node = rb_next(&prev_dc->rb_node); |
| 1458 | next_dc = rb_entry_safe(node, struct discard_cmd, rb_node); |
| 1459 | } |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1460 | } |
| 1461 | |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 1462 | static int __queue_discard_cmd(struct f2fs_sb_info *sbi, |
| 1463 | struct block_device *bdev, block_t blkstart, block_t blklen) |
| 1464 | { |
| 1465 | block_t lblkstart = blkstart; |
| 1466 | |
Damien Le Moal | 7f3d771 | 2019-03-16 09:13:08 +0900 | [diff] [blame] | 1467 | if (!f2fs_bdev_support_discard(bdev)) |
| 1468 | return 0; |
| 1469 | |
Chao Yu | 0243a5f | 2017-04-15 14:09:38 +0800 | [diff] [blame] | 1470 | trace_f2fs_queue_discard(bdev, blkstart, blklen); |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 1471 | |
Damien Le Moal | 0916878 | 2019-03-16 09:13:06 +0900 | [diff] [blame] | 1472 | if (f2fs_is_multi_device(sbi)) { |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 1473 | int devi = f2fs_target_device_index(sbi, blkstart); |
| 1474 | |
| 1475 | blkstart -= FDEV(devi).start_blk; |
| 1476 | } |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 1477 | mutex_lock(&SM_I(sbi)->dcc_info->cmd_lock); |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1478 | __update_discard_tree_range(sbi, bdev, lblkstart, blkstart, blklen); |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 1479 | mutex_unlock(&SM_I(sbi)->dcc_info->cmd_lock); |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 1480 | return 0; |
| 1481 | } |
| 1482 | |
Chao Yu | 20ee438 | 2018-07-08 22:11:01 +0800 | [diff] [blame] | 1483 | static unsigned int __issue_discard_cmd_orderly(struct f2fs_sb_info *sbi, |
| 1484 | struct discard_policy *dpolicy) |
| 1485 | { |
| 1486 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
| 1487 | struct discard_cmd *prev_dc = NULL, *next_dc = NULL; |
| 1488 | struct rb_node **insert_p = NULL, *insert_parent = NULL; |
| 1489 | struct discard_cmd *dc; |
| 1490 | struct blk_plug plug; |
| 1491 | unsigned int pos = dcc->next_pos; |
| 1492 | unsigned int issued = 0; |
| 1493 | bool io_interrupted = false; |
| 1494 | |
| 1495 | mutex_lock(&dcc->cmd_lock); |
| 1496 | dc = (struct discard_cmd *)f2fs_lookup_rb_tree_ret(&dcc->root, |
| 1497 | NULL, pos, |
| 1498 | (struct rb_entry **)&prev_dc, |
| 1499 | (struct rb_entry **)&next_dc, |
Chao Yu | 4dada3f | 2018-10-04 11:18:30 +0800 | [diff] [blame] | 1500 | &insert_p, &insert_parent, true, NULL); |
Chao Yu | 20ee438 | 2018-07-08 22:11:01 +0800 | [diff] [blame] | 1501 | if (!dc) |
| 1502 | dc = next_dc; |
| 1503 | |
| 1504 | blk_start_plug(&plug); |
| 1505 | |
| 1506 | while (dc) { |
| 1507 | struct rb_node *node; |
Chao Yu | 6b9cb12 | 2018-08-08 10:14:55 +0800 | [diff] [blame] | 1508 | int err = 0; |
Chao Yu | 20ee438 | 2018-07-08 22:11:01 +0800 | [diff] [blame] | 1509 | |
| 1510 | if (dc->state != D_PREP) |
| 1511 | goto next; |
| 1512 | |
Sahitya Tummala | a7d10cf | 2018-09-19 14:18:47 +0530 | [diff] [blame] | 1513 | if (dpolicy->io_aware && !is_idle(sbi, DISCARD_TIME)) { |
Chao Yu | 20ee438 | 2018-07-08 22:11:01 +0800 | [diff] [blame] | 1514 | io_interrupted = true; |
| 1515 | break; |
| 1516 | } |
| 1517 | |
| 1518 | dcc->next_pos = dc->lstart + dc->len; |
Chao Yu | 6b9cb12 | 2018-08-08 10:14:55 +0800 | [diff] [blame] | 1519 | err = __submit_discard_cmd(sbi, dpolicy, dc, &issued); |
Chao Yu | 20ee438 | 2018-07-08 22:11:01 +0800 | [diff] [blame] | 1520 | |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 1521 | if (issued >= dpolicy->max_requests) |
Chao Yu | 20ee438 | 2018-07-08 22:11:01 +0800 | [diff] [blame] | 1522 | break; |
| 1523 | next: |
| 1524 | node = rb_next(&dc->rb_node); |
Chao Yu | 6b9cb12 | 2018-08-08 10:14:55 +0800 | [diff] [blame] | 1525 | if (err) |
| 1526 | __remove_discard_cmd(sbi, dc); |
Chao Yu | 20ee438 | 2018-07-08 22:11:01 +0800 | [diff] [blame] | 1527 | dc = rb_entry_safe(node, struct discard_cmd, rb_node); |
| 1528 | } |
| 1529 | |
| 1530 | blk_finish_plug(&plug); |
| 1531 | |
| 1532 | if (!dc) |
| 1533 | dcc->next_pos = 0; |
| 1534 | |
| 1535 | mutex_unlock(&dcc->cmd_lock); |
| 1536 | |
| 1537 | if (!issued && io_interrupted) |
| 1538 | issued = -1; |
| 1539 | |
| 1540 | return issued; |
| 1541 | } |
Sahitya Tummala | 141af6b | 2020-04-15 14:37:53 +0530 | [diff] [blame] | 1542 | static unsigned int __wait_all_discard_cmd(struct f2fs_sb_info *sbi, |
| 1543 | struct discard_policy *dpolicy); |
Chao Yu | 20ee438 | 2018-07-08 22:11:01 +0800 | [diff] [blame] | 1544 | |
Chao Yu | 78997b5 | 2017-10-04 09:08:34 +0800 | [diff] [blame] | 1545 | static int __issue_discard_cmd(struct f2fs_sb_info *sbi, |
| 1546 | struct discard_policy *dpolicy) |
Chao Yu | bd5b073 | 2017-04-25 20:21:37 +0800 | [diff] [blame] | 1547 | { |
| 1548 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
| 1549 | struct list_head *pend_list; |
| 1550 | struct discard_cmd *dc, *tmp; |
| 1551 | struct blk_plug plug; |
Sahitya Tummala | 141af6b | 2020-04-15 14:37:53 +0530 | [diff] [blame] | 1552 | int i, issued; |
Chao Yu | e6c6de1 | 2017-09-12 21:35:12 +0800 | [diff] [blame] | 1553 | bool io_interrupted = false; |
Chao Yu | bd5b073 | 2017-04-25 20:21:37 +0800 | [diff] [blame] | 1554 | |
Chao Yu | 6ce48b0 | 2020-03-26 17:43:56 +0800 | [diff] [blame] | 1555 | if (dpolicy->timeout) |
| 1556 | f2fs_update_time(sbi, UMOUNT_DISCARD_TIMEOUT); |
Jaegeuk Kim | 03f2c02 | 2019-01-14 10:42:11 -0800 | [diff] [blame] | 1557 | |
Sahitya Tummala | 141af6b | 2020-04-15 14:37:53 +0530 | [diff] [blame] | 1558 | retry: |
| 1559 | issued = 0; |
Chao Yu | 78997b5 | 2017-10-04 09:08:34 +0800 | [diff] [blame] | 1560 | for (i = MAX_PLIST_NUM - 1; i >= 0; i--) { |
Chao Yu | 6ce48b0 | 2020-03-26 17:43:56 +0800 | [diff] [blame] | 1561 | if (dpolicy->timeout && |
| 1562 | f2fs_time_over(sbi, UMOUNT_DISCARD_TIMEOUT)) |
Jaegeuk Kim | 03f2c02 | 2019-01-14 10:42:11 -0800 | [diff] [blame] | 1563 | break; |
| 1564 | |
Chao Yu | 78997b5 | 2017-10-04 09:08:34 +0800 | [diff] [blame] | 1565 | if (i + 1 < dpolicy->granularity) |
| 1566 | break; |
Chao Yu | 20ee438 | 2018-07-08 22:11:01 +0800 | [diff] [blame] | 1567 | |
| 1568 | if (i < DEFAULT_DISCARD_GRANULARITY && dpolicy->ordered) |
| 1569 | return __issue_discard_cmd_orderly(sbi, dpolicy); |
| 1570 | |
Chao Yu | bd5b073 | 2017-04-25 20:21:37 +0800 | [diff] [blame] | 1571 | pend_list = &dcc->pend_list[i]; |
Chao Yu | 33da62c | 2017-10-04 09:08:35 +0800 | [diff] [blame] | 1572 | |
| 1573 | mutex_lock(&dcc->cmd_lock); |
Chao Yu | 49c60c6 | 2018-01-08 18:48:33 +0800 | [diff] [blame] | 1574 | if (list_empty(pend_list)) |
| 1575 | goto next; |
Chao Yu | 67fce70 | 2018-06-22 16:06:59 +0800 | [diff] [blame] | 1576 | if (unlikely(dcc->rbtree_check)) |
| 1577 | f2fs_bug_on(sbi, !f2fs_check_rb_tree_consistence(sbi, |
Chao Yu | 2e9b2bb | 2020-08-04 21:14:48 +0800 | [diff] [blame] | 1578 | &dcc->root, false)); |
Chao Yu | 33da62c | 2017-10-04 09:08:35 +0800 | [diff] [blame] | 1579 | blk_start_plug(&plug); |
Chao Yu | bd5b073 | 2017-04-25 20:21:37 +0800 | [diff] [blame] | 1580 | list_for_each_entry_safe(dc, tmp, pend_list, list) { |
| 1581 | f2fs_bug_on(sbi, dc->state != D_PREP); |
| 1582 | |
Chao Yu | 6ce48b0 | 2020-03-26 17:43:56 +0800 | [diff] [blame] | 1583 | if (dpolicy->timeout && |
| 1584 | f2fs_time_over(sbi, UMOUNT_DISCARD_TIMEOUT)) |
Heng Xiao | 6e0cd4a | 2019-07-03 10:29:57 +0800 | [diff] [blame] | 1585 | break; |
| 1586 | |
Chao Yu | ecc9aa0 | 2017-10-04 09:08:33 +0800 | [diff] [blame] | 1587 | if (dpolicy->io_aware && i < dpolicy->io_aware_gran && |
Sahitya Tummala | a7d10cf | 2018-09-19 14:18:47 +0530 | [diff] [blame] | 1588 | !is_idle(sbi, DISCARD_TIME)) { |
Chao Yu | e6c6de1 | 2017-09-12 21:35:12 +0800 | [diff] [blame] | 1589 | io_interrupted = true; |
Chao Yu | 522d171 | 2018-07-08 22:08:09 +0800 | [diff] [blame] | 1590 | break; |
Chao Yu | e6c6de1 | 2017-09-12 21:35:12 +0800 | [diff] [blame] | 1591 | } |
| 1592 | |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 1593 | __submit_discard_cmd(sbi, dpolicy, dc, &issued); |
Chao Yu | 522d171 | 2018-07-08 22:08:09 +0800 | [diff] [blame] | 1594 | |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 1595 | if (issued >= dpolicy->max_requests) |
Chao Yu | 33da62c | 2017-10-04 09:08:35 +0800 | [diff] [blame] | 1596 | break; |
Chao Yu | bd5b073 | 2017-04-25 20:21:37 +0800 | [diff] [blame] | 1597 | } |
Chao Yu | 33da62c | 2017-10-04 09:08:35 +0800 | [diff] [blame] | 1598 | blk_finish_plug(&plug); |
Chao Yu | 49c60c6 | 2018-01-08 18:48:33 +0800 | [diff] [blame] | 1599 | next: |
Chao Yu | 33da62c | 2017-10-04 09:08:35 +0800 | [diff] [blame] | 1600 | mutex_unlock(&dcc->cmd_lock); |
| 1601 | |
Chao Yu | 522d171 | 2018-07-08 22:08:09 +0800 | [diff] [blame] | 1602 | if (issued >= dpolicy->max_requests || io_interrupted) |
Chao Yu | 33da62c | 2017-10-04 09:08:35 +0800 | [diff] [blame] | 1603 | break; |
Chao Yu | bd5b073 | 2017-04-25 20:21:37 +0800 | [diff] [blame] | 1604 | } |
Chao Yu | 969d1b1 | 2017-08-07 23:09:56 +0800 | [diff] [blame] | 1605 | |
Sahitya Tummala | 141af6b | 2020-04-15 14:37:53 +0530 | [diff] [blame] | 1606 | if (dpolicy->type == DPOLICY_UMOUNT && issued) { |
| 1607 | __wait_all_discard_cmd(sbi, dpolicy); |
| 1608 | goto retry; |
| 1609 | } |
| 1610 | |
Chao Yu | e6c6de1 | 2017-09-12 21:35:12 +0800 | [diff] [blame] | 1611 | if (!issued && io_interrupted) |
| 1612 | issued = -1; |
| 1613 | |
Chao Yu | 969d1b1 | 2017-08-07 23:09:56 +0800 | [diff] [blame] | 1614 | return issued; |
| 1615 | } |
| 1616 | |
Chao Yu | cf5c759 | 2017-10-04 09:08:37 +0800 | [diff] [blame] | 1617 | static bool __drop_discard_cmd(struct f2fs_sb_info *sbi) |
Chao Yu | 969d1b1 | 2017-08-07 23:09:56 +0800 | [diff] [blame] | 1618 | { |
| 1619 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
| 1620 | struct list_head *pend_list; |
| 1621 | struct discard_cmd *dc, *tmp; |
| 1622 | int i; |
Chao Yu | cf5c759 | 2017-10-04 09:08:37 +0800 | [diff] [blame] | 1623 | bool dropped = false; |
Chao Yu | 969d1b1 | 2017-08-07 23:09:56 +0800 | [diff] [blame] | 1624 | |
| 1625 | mutex_lock(&dcc->cmd_lock); |
| 1626 | for (i = MAX_PLIST_NUM - 1; i >= 0; i--) { |
| 1627 | pend_list = &dcc->pend_list[i]; |
| 1628 | list_for_each_entry_safe(dc, tmp, pend_list, list) { |
| 1629 | f2fs_bug_on(sbi, dc->state != D_PREP); |
| 1630 | __remove_discard_cmd(sbi, dc); |
Chao Yu | cf5c759 | 2017-10-04 09:08:37 +0800 | [diff] [blame] | 1631 | dropped = true; |
Chao Yu | 969d1b1 | 2017-08-07 23:09:56 +0800 | [diff] [blame] | 1632 | } |
| 1633 | } |
| 1634 | mutex_unlock(&dcc->cmd_lock); |
Chao Yu | cf5c759 | 2017-10-04 09:08:37 +0800 | [diff] [blame] | 1635 | |
| 1636 | return dropped; |
Chao Yu | bd5b073 | 2017-04-25 20:21:37 +0800 | [diff] [blame] | 1637 | } |
| 1638 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1639 | void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi) |
Chao Yu | 7950e9a | 2018-01-18 17:23:29 +0800 | [diff] [blame] | 1640 | { |
| 1641 | __drop_discard_cmd(sbi); |
| 1642 | } |
| 1643 | |
Chao Yu | 0ea8051 | 2017-10-28 16:52:32 +0800 | [diff] [blame] | 1644 | static unsigned int __wait_one_discard_bio(struct f2fs_sb_info *sbi, |
Chao Yu | 2a510c00 | 2017-06-05 18:29:06 +0800 | [diff] [blame] | 1645 | struct discard_cmd *dc) |
| 1646 | { |
| 1647 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
Chao Yu | 0ea8051 | 2017-10-28 16:52:32 +0800 | [diff] [blame] | 1648 | unsigned int len = 0; |
Chao Yu | 2a510c00 | 2017-06-05 18:29:06 +0800 | [diff] [blame] | 1649 | |
| 1650 | wait_for_completion_io(&dc->wait); |
| 1651 | mutex_lock(&dcc->cmd_lock); |
| 1652 | f2fs_bug_on(sbi, dc->state != D_DONE); |
| 1653 | dc->ref--; |
Chao Yu | 0ea8051 | 2017-10-28 16:52:32 +0800 | [diff] [blame] | 1654 | if (!dc->ref) { |
| 1655 | if (!dc->error) |
| 1656 | len = dc->len; |
Chao Yu | 2a510c00 | 2017-06-05 18:29:06 +0800 | [diff] [blame] | 1657 | __remove_discard_cmd(sbi, dc); |
Chao Yu | 0ea8051 | 2017-10-28 16:52:32 +0800 | [diff] [blame] | 1658 | } |
Chao Yu | 2a510c00 | 2017-06-05 18:29:06 +0800 | [diff] [blame] | 1659 | mutex_unlock(&dcc->cmd_lock); |
Chao Yu | 0ea8051 | 2017-10-28 16:52:32 +0800 | [diff] [blame] | 1660 | |
| 1661 | return len; |
Chao Yu | 2a510c00 | 2017-06-05 18:29:06 +0800 | [diff] [blame] | 1662 | } |
| 1663 | |
Chao Yu | 0ea8051 | 2017-10-28 16:52:32 +0800 | [diff] [blame] | 1664 | static unsigned int __wait_discard_cmd_range(struct f2fs_sb_info *sbi, |
Chao Yu | 78997b5 | 2017-10-04 09:08:34 +0800 | [diff] [blame] | 1665 | struct discard_policy *dpolicy, |
| 1666 | block_t start, block_t end) |
Chao Yu | 63a94fa | 2017-04-25 20:21:38 +0800 | [diff] [blame] | 1667 | { |
| 1668 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
Chao Yu | 78997b5 | 2017-10-04 09:08:34 +0800 | [diff] [blame] | 1669 | struct list_head *wait_list = (dpolicy->type == DPOLICY_FSTRIM) ? |
| 1670 | &(dcc->fstrim_list) : &(dcc->wait_list); |
Chao Yu | 63a94fa | 2017-04-25 20:21:38 +0800 | [diff] [blame] | 1671 | struct discard_cmd *dc, *tmp; |
Chao Yu | 6afae63 | 2017-05-19 23:46:45 +0800 | [diff] [blame] | 1672 | bool need_wait; |
Chao Yu | 0ea8051 | 2017-10-28 16:52:32 +0800 | [diff] [blame] | 1673 | unsigned int trimmed = 0; |
Chao Yu | 6afae63 | 2017-05-19 23:46:45 +0800 | [diff] [blame] | 1674 | |
| 1675 | next: |
| 1676 | need_wait = false; |
Chao Yu | 63a94fa | 2017-04-25 20:21:38 +0800 | [diff] [blame] | 1677 | |
| 1678 | mutex_lock(&dcc->cmd_lock); |
| 1679 | list_for_each_entry_safe(dc, tmp, wait_list, list) { |
Chao Yu | 8412663 | 2017-10-04 09:08:32 +0800 | [diff] [blame] | 1680 | if (dc->lstart + dc->len <= start || end <= dc->lstart) |
| 1681 | continue; |
Chao Yu | 78997b5 | 2017-10-04 09:08:34 +0800 | [diff] [blame] | 1682 | if (dc->len < dpolicy->granularity) |
Chao Yu | 8412663 | 2017-10-04 09:08:32 +0800 | [diff] [blame] | 1683 | continue; |
Chao Yu | 78997b5 | 2017-10-04 09:08:34 +0800 | [diff] [blame] | 1684 | if (dc->state == D_DONE && !dc->ref) { |
Chao Yu | 63a94fa | 2017-04-25 20:21:38 +0800 | [diff] [blame] | 1685 | wait_for_completion_io(&dc->wait); |
Chao Yu | 0ea8051 | 2017-10-28 16:52:32 +0800 | [diff] [blame] | 1686 | if (!dc->error) |
| 1687 | trimmed += dc->len; |
Chao Yu | 63a94fa | 2017-04-25 20:21:38 +0800 | [diff] [blame] | 1688 | __remove_discard_cmd(sbi, dc); |
Chao Yu | 6afae63 | 2017-05-19 23:46:45 +0800 | [diff] [blame] | 1689 | } else { |
| 1690 | dc->ref++; |
| 1691 | need_wait = true; |
| 1692 | break; |
Chao Yu | 63a94fa | 2017-04-25 20:21:38 +0800 | [diff] [blame] | 1693 | } |
| 1694 | } |
| 1695 | mutex_unlock(&dcc->cmd_lock); |
Chao Yu | 6afae63 | 2017-05-19 23:46:45 +0800 | [diff] [blame] | 1696 | |
| 1697 | if (need_wait) { |
Chao Yu | 0ea8051 | 2017-10-28 16:52:32 +0800 | [diff] [blame] | 1698 | trimmed += __wait_one_discard_bio(sbi, dc); |
Chao Yu | 6afae63 | 2017-05-19 23:46:45 +0800 | [diff] [blame] | 1699 | goto next; |
| 1700 | } |
Chao Yu | 0ea8051 | 2017-10-28 16:52:32 +0800 | [diff] [blame] | 1701 | |
| 1702 | return trimmed; |
Chao Yu | 63a94fa | 2017-04-25 20:21:38 +0800 | [diff] [blame] | 1703 | } |
| 1704 | |
Chao Yu | 01f9cf6 | 2018-06-25 20:33:24 +0800 | [diff] [blame] | 1705 | static unsigned int __wait_all_discard_cmd(struct f2fs_sb_info *sbi, |
Chao Yu | 78997b5 | 2017-10-04 09:08:34 +0800 | [diff] [blame] | 1706 | struct discard_policy *dpolicy) |
Chao Yu | 8412663 | 2017-10-04 09:08:32 +0800 | [diff] [blame] | 1707 | { |
Jaegeuk Kim | 9a99718 | 2018-05-24 13:57:26 -0700 | [diff] [blame] | 1708 | struct discard_policy dp; |
Chao Yu | 01f9cf6 | 2018-06-25 20:33:24 +0800 | [diff] [blame] | 1709 | unsigned int discard_blks; |
Jaegeuk Kim | 9a99718 | 2018-05-24 13:57:26 -0700 | [diff] [blame] | 1710 | |
Chao Yu | 01f9cf6 | 2018-06-25 20:33:24 +0800 | [diff] [blame] | 1711 | if (dpolicy) |
| 1712 | return __wait_discard_cmd_range(sbi, dpolicy, 0, UINT_MAX); |
Jaegeuk Kim | 9a99718 | 2018-05-24 13:57:26 -0700 | [diff] [blame] | 1713 | |
| 1714 | /* wait all */ |
Jaegeuk Kim | 8bb4f25 | 2018-05-29 09:58:42 -0700 | [diff] [blame] | 1715 | __init_discard_policy(sbi, &dp, DPOLICY_FSTRIM, 1); |
Chao Yu | 01f9cf6 | 2018-06-25 20:33:24 +0800 | [diff] [blame] | 1716 | discard_blks = __wait_discard_cmd_range(sbi, &dp, 0, UINT_MAX); |
Jaegeuk Kim | 8bb4f25 | 2018-05-29 09:58:42 -0700 | [diff] [blame] | 1717 | __init_discard_policy(sbi, &dp, DPOLICY_UMOUNT, 1); |
Chao Yu | 01f9cf6 | 2018-06-25 20:33:24 +0800 | [diff] [blame] | 1718 | discard_blks += __wait_discard_cmd_range(sbi, &dp, 0, UINT_MAX); |
| 1719 | |
| 1720 | return discard_blks; |
Chao Yu | 8412663 | 2017-10-04 09:08:32 +0800 | [diff] [blame] | 1721 | } |
| 1722 | |
Jaegeuk Kim | 4e6a8d9 | 2016-12-29 14:07:53 -0800 | [diff] [blame] | 1723 | /* This should be covered by global mutex, &sit_i->sentry_lock */ |
Wei Yongjun | 94b1e10 | 2018-01-05 09:41:20 +0000 | [diff] [blame] | 1724 | static void f2fs_wait_discard_bio(struct f2fs_sb_info *sbi, block_t blkaddr) |
Chao Yu | 275b66b | 2016-08-29 23:58:34 +0800 | [diff] [blame] | 1725 | { |
Jaegeuk Kim | 0b54fb8 | 2017-01-11 14:40:24 -0800 | [diff] [blame] | 1726 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1727 | struct discard_cmd *dc; |
Chao Yu | ec9895a | 2017-04-26 17:39:54 +0800 | [diff] [blame] | 1728 | bool need_wait = false; |
Chao Yu | 275b66b | 2016-08-29 23:58:34 +0800 | [diff] [blame] | 1729 | |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 1730 | mutex_lock(&dcc->cmd_lock); |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1731 | dc = (struct discard_cmd *)f2fs_lookup_rb_tree(&dcc->root, |
| 1732 | NULL, blkaddr); |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1733 | if (dc) { |
Chao Yu | ec9895a | 2017-04-26 17:39:54 +0800 | [diff] [blame] | 1734 | if (dc->state == D_PREP) { |
| 1735 | __punch_discard_cmd(sbi, dc, blkaddr); |
| 1736 | } else { |
| 1737 | dc->ref++; |
| 1738 | need_wait = true; |
| 1739 | } |
Chao Yu | 275b66b | 2016-08-29 23:58:34 +0800 | [diff] [blame] | 1740 | } |
Chao Yu | d431413 | 2017-04-05 18:19:49 +0800 | [diff] [blame] | 1741 | mutex_unlock(&dcc->cmd_lock); |
Chao Yu | ec9895a | 2017-04-26 17:39:54 +0800 | [diff] [blame] | 1742 | |
Chao Yu | 2a510c00 | 2017-06-05 18:29:06 +0800 | [diff] [blame] | 1743 | if (need_wait) |
| 1744 | __wait_one_discard_bio(sbi, dc); |
Chao Yu | d431413 | 2017-04-05 18:19:49 +0800 | [diff] [blame] | 1745 | } |
Chao Yu | 22d375d | 2017-04-05 18:19:48 +0800 | [diff] [blame] | 1746 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1747 | void f2fs_stop_discard_thread(struct f2fs_sb_info *sbi) |
Chao Yu | cce1325 | 2017-06-29 23:17:45 +0800 | [diff] [blame] | 1748 | { |
| 1749 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
| 1750 | |
| 1751 | if (dcc && dcc->f2fs_issue_discard) { |
| 1752 | struct task_struct *discard_thread = dcc->f2fs_issue_discard; |
| 1753 | |
| 1754 | dcc->f2fs_issue_discard = NULL; |
| 1755 | kthread_stop(discard_thread); |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 1756 | } |
| 1757 | } |
| 1758 | |
Chao Yu | 8412663 | 2017-10-04 09:08:32 +0800 | [diff] [blame] | 1759 | /* This comes from f2fs_put_super */ |
Jaegeuk Kim | 03f2c02 | 2019-01-14 10:42:11 -0800 | [diff] [blame] | 1760 | bool f2fs_issue_discard_timeout(struct f2fs_sb_info *sbi) |
Chao Yu | 275b66b | 2016-08-29 23:58:34 +0800 | [diff] [blame] | 1761 | { |
Chao Yu | 969d1b1 | 2017-08-07 23:09:56 +0800 | [diff] [blame] | 1762 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
Chao Yu | 78997b5 | 2017-10-04 09:08:34 +0800 | [diff] [blame] | 1763 | struct discard_policy dpolicy; |
Chao Yu | cf5c759 | 2017-10-04 09:08:37 +0800 | [diff] [blame] | 1764 | bool dropped; |
Chao Yu | 969d1b1 | 2017-08-07 23:09:56 +0800 | [diff] [blame] | 1765 | |
Jaegeuk Kim | 8bb4f25 | 2018-05-29 09:58:42 -0700 | [diff] [blame] | 1766 | __init_discard_policy(sbi, &dpolicy, DPOLICY_UMOUNT, |
| 1767 | dcc->discard_granularity); |
Chao Yu | 78997b5 | 2017-10-04 09:08:34 +0800 | [diff] [blame] | 1768 | __issue_discard_cmd(sbi, &dpolicy); |
Chao Yu | cf5c759 | 2017-10-04 09:08:37 +0800 | [diff] [blame] | 1769 | dropped = __drop_discard_cmd(sbi); |
Chao Yu | cf5c759 | 2017-10-04 09:08:37 +0800 | [diff] [blame] | 1770 | |
Jaegeuk Kim | 9a99718 | 2018-05-24 13:57:26 -0700 | [diff] [blame] | 1771 | /* just to make sure there is no pending discard commands */ |
| 1772 | __wait_all_discard_cmd(sbi, NULL); |
Chao Yu | 2482c43 | 2018-07-08 22:16:53 +0800 | [diff] [blame] | 1773 | |
| 1774 | f2fs_bug_on(sbi, atomic_read(&dcc->discard_cmd_cnt)); |
Chao Yu | cf5c759 | 2017-10-04 09:08:37 +0800 | [diff] [blame] | 1775 | return dropped; |
Chao Yu | 969d1b1 | 2017-08-07 23:09:56 +0800 | [diff] [blame] | 1776 | } |
| 1777 | |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 1778 | static int issue_discard_thread(void *data) |
| 1779 | { |
| 1780 | struct f2fs_sb_info *sbi = data; |
| 1781 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
| 1782 | wait_queue_head_t *q = &dcc->discard_wait_queue; |
Chao Yu | 78997b5 | 2017-10-04 09:08:34 +0800 | [diff] [blame] | 1783 | struct discard_policy dpolicy; |
Chao Yu | 969d1b1 | 2017-08-07 23:09:56 +0800 | [diff] [blame] | 1784 | unsigned int wait_ms = DEF_MIN_DISCARD_ISSUE_TIME; |
| 1785 | int issued; |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 1786 | |
Jaegeuk Kim | 1d7be27 | 2017-05-17 10:36:58 -0700 | [diff] [blame] | 1787 | set_freezable(); |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 1788 | |
Jaegeuk Kim | 1d7be27 | 2017-05-17 10:36:58 -0700 | [diff] [blame] | 1789 | do { |
Sahitya Tummala | c35b8d5 | 2021-04-06 14:39:16 +0530 | [diff] [blame] | 1790 | if (sbi->gc_mode == GC_URGENT_HIGH || |
| 1791 | !f2fs_available_free_memory(sbi, DISCARD_CACHE)) |
| 1792 | __init_discard_policy(sbi, &dpolicy, DPOLICY_FORCE, 1); |
| 1793 | else |
| 1794 | __init_discard_policy(sbi, &dpolicy, DPOLICY_BG, |
| 1795 | dcc->discard_granularity); |
| 1796 | |
| 1797 | if (!atomic_read(&dcc->discard_cmd_cnt)) |
| 1798 | wait_ms = dpolicy.max_interval; |
Chao Yu | 78997b5 | 2017-10-04 09:08:34 +0800 | [diff] [blame] | 1799 | |
Chao Yu | 969d1b1 | 2017-08-07 23:09:56 +0800 | [diff] [blame] | 1800 | wait_event_interruptible_timeout(*q, |
| 1801 | kthread_should_stop() || freezing(current) || |
| 1802 | dcc->discard_wake, |
| 1803 | msecs_to_jiffies(wait_ms)); |
Sheng Yong | 35a9a76 | 2018-05-08 17:51:34 +0800 | [diff] [blame] | 1804 | |
| 1805 | if (dcc->discard_wake) |
| 1806 | dcc->discard_wake = 0; |
| 1807 | |
Jaegeuk Kim | 76c7bfb | 2018-12-13 20:50:51 -0800 | [diff] [blame] | 1808 | /* clean up pending candidates before going to sleep */ |
| 1809 | if (atomic_read(&dcc->queued_discard)) |
| 1810 | __wait_all_discard_cmd(sbi, NULL); |
| 1811 | |
Jaegeuk Kim | 1d7be27 | 2017-05-17 10:36:58 -0700 | [diff] [blame] | 1812 | if (try_to_freeze()) |
| 1813 | continue; |
Chao Yu | 3b60d80 | 2018-01-25 18:57:27 +0800 | [diff] [blame] | 1814 | if (f2fs_readonly(sbi->sb)) |
| 1815 | continue; |
Jaegeuk Kim | 1d7be27 | 2017-05-17 10:36:58 -0700 | [diff] [blame] | 1816 | if (kthread_should_stop()) |
| 1817 | return 0; |
Yunlei He | d618477 | 2018-04-13 11:08:05 +0800 | [diff] [blame] | 1818 | if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) { |
| 1819 | wait_ms = dpolicy.max_interval; |
| 1820 | continue; |
| 1821 | } |
Chao Yu | 43f8c47e | 2021-02-22 18:07:33 +0800 | [diff] [blame] | 1822 | if (!atomic_read(&dcc->discard_cmd_cnt)) |
| 1823 | continue; |
Jaegeuk Kim | 1d7be27 | 2017-05-17 10:36:58 -0700 | [diff] [blame] | 1824 | |
Chao Yu | dc6febb | 2017-07-22 08:52:23 +0800 | [diff] [blame] | 1825 | sb_start_intwrite(sbi->sb); |
| 1826 | |
Chao Yu | 78997b5 | 2017-10-04 09:08:34 +0800 | [diff] [blame] | 1827 | issued = __issue_discard_cmd(sbi, &dpolicy); |
Yunlei He | f9d1dce | 2018-04-08 15:11:11 +0800 | [diff] [blame] | 1828 | if (issued > 0) { |
Chao Yu | 78997b5 | 2017-10-04 09:08:34 +0800 | [diff] [blame] | 1829 | __wait_all_discard_cmd(sbi, &dpolicy); |
| 1830 | wait_ms = dpolicy.min_interval; |
Yi Zhuang | 5f029c0 | 2021-04-06 09:47:35 +0800 | [diff] [blame] | 1831 | } else if (issued == -1) { |
Sahitya Tummala | a7d10cf | 2018-09-19 14:18:47 +0530 | [diff] [blame] | 1832 | wait_ms = f2fs_time_to_wait(sbi, DISCARD_TIME); |
| 1833 | if (!wait_ms) |
Sahitya Tummala | abde73c | 2018-08-31 15:09:26 +0530 | [diff] [blame] | 1834 | wait_ms = dpolicy.mid_interval; |
Chao Yu | 969d1b1 | 2017-08-07 23:09:56 +0800 | [diff] [blame] | 1835 | } else { |
Chao Yu | 78997b5 | 2017-10-04 09:08:34 +0800 | [diff] [blame] | 1836 | wait_ms = dpolicy.max_interval; |
Chao Yu | 969d1b1 | 2017-08-07 23:09:56 +0800 | [diff] [blame] | 1837 | } |
Jaegeuk Kim | 1d7be27 | 2017-05-17 10:36:58 -0700 | [diff] [blame] | 1838 | |
Chao Yu | dc6febb | 2017-07-22 08:52:23 +0800 | [diff] [blame] | 1839 | sb_end_intwrite(sbi->sb); |
| 1840 | |
Jaegeuk Kim | 1d7be27 | 2017-05-17 10:36:58 -0700 | [diff] [blame] | 1841 | } while (!kthread_should_stop()); |
| 1842 | return 0; |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 1843 | } |
| 1844 | |
Damien Le Moal | f46e8809 | 2016-10-28 17:45:06 +0900 | [diff] [blame] | 1845 | #ifdef CONFIG_BLK_DEV_ZONED |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 1846 | static int __f2fs_issue_discard_zone(struct f2fs_sb_info *sbi, |
| 1847 | struct block_device *bdev, block_t blkstart, block_t blklen) |
Damien Le Moal | f46e8809 | 2016-10-28 17:45:06 +0900 | [diff] [blame] | 1848 | { |
Jaegeuk Kim | 92592285 | 2017-02-22 20:18:35 -0800 | [diff] [blame] | 1849 | sector_t sector, nr_sects; |
Kinglong Mee | 10a875f | 2017-03-08 09:49:53 +0800 | [diff] [blame] | 1850 | block_t lblkstart = blkstart; |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 1851 | int devi = 0; |
Damien Le Moal | f46e8809 | 2016-10-28 17:45:06 +0900 | [diff] [blame] | 1852 | |
Damien Le Moal | 0916878 | 2019-03-16 09:13:06 +0900 | [diff] [blame] | 1853 | if (f2fs_is_multi_device(sbi)) { |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 1854 | devi = f2fs_target_device_index(sbi, blkstart); |
Damien Le Moal | 95175da | 2019-03-16 09:13:07 +0900 | [diff] [blame] | 1855 | if (blkstart < FDEV(devi).start_blk || |
| 1856 | blkstart > FDEV(devi).end_blk) { |
Joe Perches | dcbb4c1 | 2019-06-18 17:48:42 +0800 | [diff] [blame] | 1857 | f2fs_err(sbi, "Invalid block %x", blkstart); |
Damien Le Moal | 95175da | 2019-03-16 09:13:07 +0900 | [diff] [blame] | 1858 | return -EIO; |
| 1859 | } |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 1860 | blkstart -= FDEV(devi).start_blk; |
| 1861 | } |
Damien Le Moal | f46e8809 | 2016-10-28 17:45:06 +0900 | [diff] [blame] | 1862 | |
Damien Le Moal | 95175da | 2019-03-16 09:13:07 +0900 | [diff] [blame] | 1863 | /* For sequential zones, reset the zone write pointer */ |
| 1864 | if (f2fs_blkz_is_seq(sbi, devi, blkstart)) { |
Jaegeuk Kim | 92592285 | 2017-02-22 20:18:35 -0800 | [diff] [blame] | 1865 | sector = SECTOR_FROM_BLOCK(blkstart); |
| 1866 | nr_sects = SECTOR_FROM_BLOCK(blklen); |
| 1867 | |
| 1868 | if (sector & (bdev_zone_sectors(bdev) - 1) || |
| 1869 | nr_sects != bdev_zone_sectors(bdev)) { |
Joe Perches | dcbb4c1 | 2019-06-18 17:48:42 +0800 | [diff] [blame] | 1870 | f2fs_err(sbi, "(%d) %s: Unaligned zone reset attempted (block %x + %x)", |
| 1871 | devi, sbi->s_ndevs ? FDEV(devi).path : "", |
| 1872 | blkstart, blklen); |
Jaegeuk Kim | 92592285 | 2017-02-22 20:18:35 -0800 | [diff] [blame] | 1873 | return -EIO; |
| 1874 | } |
Jaegeuk Kim | d50aaee | 2017-02-15 11:14:06 -0800 | [diff] [blame] | 1875 | trace_f2fs_issue_reset_zone(bdev, blkstart); |
Ajay Joshi | 6c1b1da | 2019-10-27 23:05:45 +0900 | [diff] [blame] | 1876 | return blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET, |
| 1877 | sector, nr_sects, GFP_NOFS); |
Damien Le Moal | f46e8809 | 2016-10-28 17:45:06 +0900 | [diff] [blame] | 1878 | } |
Damien Le Moal | 95175da | 2019-03-16 09:13:07 +0900 | [diff] [blame] | 1879 | |
| 1880 | /* For conventional zones, use regular discard if supported */ |
Damien Le Moal | 95175da | 2019-03-16 09:13:07 +0900 | [diff] [blame] | 1881 | return __queue_discard_cmd(sbi, bdev, lblkstart, blklen); |
Damien Le Moal | f46e8809 | 2016-10-28 17:45:06 +0900 | [diff] [blame] | 1882 | } |
| 1883 | #endif |
| 1884 | |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 1885 | static int __issue_discard_async(struct f2fs_sb_info *sbi, |
| 1886 | struct block_device *bdev, block_t blkstart, block_t blklen) |
| 1887 | { |
| 1888 | #ifdef CONFIG_BLK_DEV_ZONED |
Damien Le Moal | 7f3d771 | 2019-03-16 09:13:08 +0900 | [diff] [blame] | 1889 | if (f2fs_sb_has_blkzoned(sbi) && bdev_is_zoned(bdev)) |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 1890 | return __f2fs_issue_discard_zone(sbi, bdev, blkstart, blklen); |
| 1891 | #endif |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 1892 | return __queue_discard_cmd(sbi, bdev, blkstart, blklen); |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 1893 | } |
| 1894 | |
Jaegeuk Kim | 1e87a78 | 2014-04-15 13:57:55 +0900 | [diff] [blame] | 1895 | static int f2fs_issue_discard(struct f2fs_sb_info *sbi, |
Jaegeuk Kim | 3720887 | 2013-11-12 16:55:17 +0900 | [diff] [blame] | 1896 | block_t blkstart, block_t blklen) |
| 1897 | { |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 1898 | sector_t start = blkstart, len = 0; |
| 1899 | struct block_device *bdev; |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 1900 | struct seg_entry *se; |
| 1901 | unsigned int offset; |
| 1902 | block_t i; |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 1903 | int err = 0; |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 1904 | |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 1905 | bdev = f2fs_target_device(sbi, blkstart, NULL); |
| 1906 | |
| 1907 | for (i = blkstart; i < blkstart + blklen; i++, len++) { |
| 1908 | if (i != start) { |
| 1909 | struct block_device *bdev2 = |
| 1910 | f2fs_target_device(sbi, i, NULL); |
| 1911 | |
| 1912 | if (bdev2 != bdev) { |
| 1913 | err = __issue_discard_async(sbi, bdev, |
| 1914 | start, len); |
| 1915 | if (err) |
| 1916 | return err; |
| 1917 | bdev = bdev2; |
| 1918 | start = i; |
| 1919 | len = 0; |
| 1920 | } |
| 1921 | } |
| 1922 | |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 1923 | se = get_seg_entry(sbi, GET_SEGNO(sbi, i)); |
| 1924 | offset = GET_BLKOFF_FROM_SEG0(sbi, i); |
| 1925 | |
Chao Yu | 4f99326 | 2021-08-03 08:15:43 +0800 | [diff] [blame] | 1926 | if (f2fs_block_unit_discard(sbi) && |
| 1927 | !f2fs_test_and_set_bit(offset, se->discard_map)) |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 1928 | sbi->discard_blks--; |
| 1929 | } |
Damien Le Moal | f46e8809 | 2016-10-28 17:45:06 +0900 | [diff] [blame] | 1930 | |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 1931 | if (len) |
| 1932 | err = __issue_discard_async(sbi, bdev, start, len); |
| 1933 | return err; |
Jaegeuk Kim | 1e87a78 | 2014-04-15 13:57:55 +0900 | [diff] [blame] | 1934 | } |
| 1935 | |
Jaegeuk Kim | 25290fa | 2016-12-29 22:06:15 -0800 | [diff] [blame] | 1936 | static bool add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc, |
| 1937 | bool check_only) |
Jaegeuk Kim | adf4983 | 2014-10-28 22:27:59 -0700 | [diff] [blame] | 1938 | { |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 1939 | int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long); |
| 1940 | int max_blocks = sbi->blocks_per_seg; |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 1941 | struct seg_entry *se = get_seg_entry(sbi, cpc->trim_start); |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 1942 | unsigned long *cur_map = (unsigned long *)se->cur_valid_map; |
| 1943 | unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map; |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 1944 | unsigned long *discard_map = (unsigned long *)se->discard_map; |
Jaegeuk Kim | 60a3b78 | 2015-02-10 16:44:29 -0800 | [diff] [blame] | 1945 | unsigned long *dmap = SIT_I(sbi)->tmp_map; |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 1946 | unsigned int start = 0, end = -1; |
Chao Yu | c473f1a | 2017-04-27 20:40:39 +0800 | [diff] [blame] | 1947 | bool force = (cpc->reason & CP_DISCARD); |
Chao Yu | a7eeb823 | 2017-03-28 18:18:50 +0800 | [diff] [blame] | 1948 | struct discard_entry *de = NULL; |
Chao Yu | 46f84c2 | 2017-04-15 14:09:36 +0800 | [diff] [blame] | 1949 | struct list_head *head = &SM_I(sbi)->dcc_info->entry_list; |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 1950 | int i; |
| 1951 | |
Chao Yu | 4f99326 | 2021-08-03 08:15:43 +0800 | [diff] [blame] | 1952 | if (se->valid_blocks == max_blocks || !f2fs_hw_support_discard(sbi) || |
| 1953 | !f2fs_block_unit_discard(sbi)) |
Jaegeuk Kim | 25290fa | 2016-12-29 22:06:15 -0800 | [diff] [blame] | 1954 | return false; |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 1955 | |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 1956 | if (!force) { |
Chao Yu | 7d20c8a | 2018-09-04 03:52:17 +0800 | [diff] [blame] | 1957 | if (!f2fs_realtime_discard_enable(sbi) || !se->valid_blocks || |
Jaegeuk Kim | 0b54fb8 | 2017-01-11 14:40:24 -0800 | [diff] [blame] | 1958 | SM_I(sbi)->dcc_info->nr_discards >= |
| 1959 | SM_I(sbi)->dcc_info->max_discards) |
Jaegeuk Kim | 25290fa | 2016-12-29 22:06:15 -0800 | [diff] [blame] | 1960 | return false; |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 1961 | } |
| 1962 | |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 1963 | /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */ |
| 1964 | for (i = 0; i < entries; i++) |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 1965 | dmap[i] = force ? ~ckpt_map[i] & ~discard_map[i] : |
Jaegeuk Kim | d7bc248 | 2014-12-12 13:53:41 -0800 | [diff] [blame] | 1966 | (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i]; |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 1967 | |
Jaegeuk Kim | 0b54fb8 | 2017-01-11 14:40:24 -0800 | [diff] [blame] | 1968 | while (force || SM_I(sbi)->dcc_info->nr_discards <= |
| 1969 | SM_I(sbi)->dcc_info->max_discards) { |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 1970 | start = __find_rev_next_bit(dmap, max_blocks, end + 1); |
| 1971 | if (start >= max_blocks) |
| 1972 | break; |
| 1973 | |
| 1974 | end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1); |
Yunlei He | c7b41e1 | 2016-07-07 12:13:33 +0800 | [diff] [blame] | 1975 | if (force && start && end != max_blocks |
| 1976 | && (end - start) < cpc->trim_minlen) |
| 1977 | continue; |
| 1978 | |
Jaegeuk Kim | 25290fa | 2016-12-29 22:06:15 -0800 | [diff] [blame] | 1979 | if (check_only) |
| 1980 | return true; |
| 1981 | |
Chao Yu | a7eeb823 | 2017-03-28 18:18:50 +0800 | [diff] [blame] | 1982 | if (!de) { |
| 1983 | de = f2fs_kmem_cache_alloc(discard_entry_slab, |
Chao Yu | 3241057 | 2021-08-09 08:24:48 +0800 | [diff] [blame] | 1984 | GFP_F2FS_ZERO, true, NULL); |
Chao Yu | a7eeb823 | 2017-03-28 18:18:50 +0800 | [diff] [blame] | 1985 | de->start_blkaddr = START_BLOCK(sbi, cpc->trim_start); |
| 1986 | list_add_tail(&de->list, head); |
| 1987 | } |
| 1988 | |
| 1989 | for (i = start; i < end; i++) |
| 1990 | __set_bit_le(i, (void *)de->discard_map); |
| 1991 | |
| 1992 | SM_I(sbi)->dcc_info->nr_discards += end - start; |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 1993 | } |
Jaegeuk Kim | 25290fa | 2016-12-29 22:06:15 -0800 | [diff] [blame] | 1994 | return false; |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 1995 | } |
| 1996 | |
Chao Yu | af8ff65 | 2018-04-25 17:38:29 +0800 | [diff] [blame] | 1997 | static void release_discard_addr(struct discard_entry *entry) |
| 1998 | { |
| 1999 | list_del(&entry->list); |
| 2000 | kmem_cache_free(discard_entry_slab, entry); |
| 2001 | } |
| 2002 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2003 | void f2fs_release_discard_addrs(struct f2fs_sb_info *sbi) |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 2004 | { |
Chao Yu | 46f84c2 | 2017-04-15 14:09:36 +0800 | [diff] [blame] | 2005 | struct list_head *head = &(SM_I(sbi)->dcc_info->entry_list); |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 2006 | struct discard_entry *entry, *this; |
| 2007 | |
| 2008 | /* drop caches */ |
Chao Yu | af8ff65 | 2018-04-25 17:38:29 +0800 | [diff] [blame] | 2009 | list_for_each_entry_safe(entry, this, head, list) |
| 2010 | release_discard_addr(entry); |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 2011 | } |
| 2012 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 2013 | /* |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2014 | * Should call f2fs_clear_prefree_segments after checkpoint is done. |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2015 | */ |
| 2016 | static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi) |
| 2017 | { |
| 2018 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
Chao Yu | b65ee14 | 2014-08-04 10:10:07 +0800 | [diff] [blame] | 2019 | unsigned int segno; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2020 | |
| 2021 | mutex_lock(&dirty_i->seglist_lock); |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 2022 | for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], MAIN_SEGS(sbi)) |
Chao Yu | d0b9e42 | 2020-08-04 21:14:45 +0800 | [diff] [blame] | 2023 | __set_test_and_free(sbi, segno, false); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2024 | mutex_unlock(&dirty_i->seglist_lock); |
| 2025 | } |
| 2026 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2027 | void f2fs_clear_prefree_segments(struct f2fs_sb_info *sbi, |
| 2028 | struct cp_control *cpc) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2029 | { |
Chao Yu | 969d1b1 | 2017-08-07 23:09:56 +0800 | [diff] [blame] | 2030 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
| 2031 | struct list_head *head = &dcc->entry_list; |
Chao Yu | 2d7b822 | 2014-03-29 11:33:17 +0800 | [diff] [blame] | 2032 | struct discard_entry *entry, *this; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2033 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
Changman Lee | 29e59c1 | 2013-11-11 09:24:37 +0900 | [diff] [blame] | 2034 | unsigned long *prefree_map = dirty_i->dirty_segmap[PRE]; |
Changman Lee | 29e59c1 | 2013-11-11 09:24:37 +0900 | [diff] [blame] | 2035 | unsigned int start = 0, end = -1; |
Jaegeuk Kim | 36abef4 | 2016-06-03 19:29:38 -0700 | [diff] [blame] | 2036 | unsigned int secno, start_segno; |
Chao Yu | c473f1a | 2017-04-27 20:40:39 +0800 | [diff] [blame] | 2037 | bool force = (cpc->reason & CP_DISCARD); |
Chao Yu | 4f99326 | 2021-08-03 08:15:43 +0800 | [diff] [blame] | 2038 | bool section_alignment = F2FS_OPTION(sbi).discard_unit == |
| 2039 | DISCARD_UNIT_SECTION; |
| 2040 | |
| 2041 | if (f2fs_lfs_mode(sbi) && __is_large_section(sbi)) |
| 2042 | section_alignment = true; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2043 | |
| 2044 | mutex_lock(&dirty_i->seglist_lock); |
Changman Lee | 29e59c1 | 2013-11-11 09:24:37 +0900 | [diff] [blame] | 2045 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2046 | while (1) { |
Changman Lee | 29e59c1 | 2013-11-11 09:24:37 +0900 | [diff] [blame] | 2047 | int i; |
Yunlong Song | ad6672b | 2018-07-19 20:58:15 +0800 | [diff] [blame] | 2048 | |
Chao Yu | 4f99326 | 2021-08-03 08:15:43 +0800 | [diff] [blame] | 2049 | if (section_alignment && end != -1) |
Yunlong Song | ad6672b | 2018-07-19 20:58:15 +0800 | [diff] [blame] | 2050 | end--; |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 2051 | start = find_next_bit(prefree_map, MAIN_SEGS(sbi), end + 1); |
| 2052 | if (start >= MAIN_SEGS(sbi)) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2053 | break; |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 2054 | end = find_next_zero_bit(prefree_map, MAIN_SEGS(sbi), |
| 2055 | start + 1); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2056 | |
Chao Yu | 4f99326 | 2021-08-03 08:15:43 +0800 | [diff] [blame] | 2057 | if (section_alignment) { |
Yunlong Song | ad6672b | 2018-07-19 20:58:15 +0800 | [diff] [blame] | 2058 | start = rounddown(start, sbi->segs_per_sec); |
| 2059 | end = roundup(end, sbi->segs_per_sec); |
| 2060 | } |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2061 | |
Yunlong Song | ad6672b | 2018-07-19 20:58:15 +0800 | [diff] [blame] | 2062 | for (i = start; i < end; i++) { |
| 2063 | if (test_and_clear_bit(i, prefree_map)) |
| 2064 | dirty_i->nr_dirty[PRE]--; |
| 2065 | } |
Changman Lee | 29e59c1 | 2013-11-11 09:24:37 +0900 | [diff] [blame] | 2066 | |
Chao Yu | 7d20c8a | 2018-09-04 03:52:17 +0800 | [diff] [blame] | 2067 | if (!f2fs_realtime_discard_enable(sbi)) |
Changman Lee | 29e59c1 | 2013-11-11 09:24:37 +0900 | [diff] [blame] | 2068 | continue; |
| 2069 | |
Yunlei He | 650d3c4 | 2016-12-22 11:46:24 +0800 | [diff] [blame] | 2070 | if (force && start >= cpc->trim_start && |
| 2071 | (end - 1) <= cpc->trim_end) |
| 2072 | continue; |
| 2073 | |
Chao Yu | b0332a0 | 2020-02-14 17:44:12 +0800 | [diff] [blame] | 2074 | if (!f2fs_lfs_mode(sbi) || !__is_large_section(sbi)) { |
Jaegeuk Kim | 36abef4 | 2016-06-03 19:29:38 -0700 | [diff] [blame] | 2075 | f2fs_issue_discard(sbi, START_BLOCK(sbi, start), |
Jaegeuk Kim | 3720887 | 2013-11-12 16:55:17 +0900 | [diff] [blame] | 2076 | (end - start) << sbi->log_blocks_per_seg); |
Jaegeuk Kim | 36abef4 | 2016-06-03 19:29:38 -0700 | [diff] [blame] | 2077 | continue; |
| 2078 | } |
| 2079 | next: |
Jaegeuk Kim | 4ddb1a4 | 2017-04-07 15:08:17 -0700 | [diff] [blame] | 2080 | secno = GET_SEC_FROM_SEG(sbi, start); |
| 2081 | start_segno = GET_SEG_FROM_SEC(sbi, secno); |
Jaegeuk Kim | 36abef4 | 2016-06-03 19:29:38 -0700 | [diff] [blame] | 2082 | if (!IS_CURSEC(sbi, secno) && |
Jaegeuk Kim | 302bd34 | 2017-04-07 14:33:22 -0700 | [diff] [blame] | 2083 | !get_valid_blocks(sbi, start, true)) |
Jaegeuk Kim | 36abef4 | 2016-06-03 19:29:38 -0700 | [diff] [blame] | 2084 | f2fs_issue_discard(sbi, START_BLOCK(sbi, start_segno), |
| 2085 | sbi->segs_per_sec << sbi->log_blocks_per_seg); |
| 2086 | |
| 2087 | start = start_segno + sbi->segs_per_sec; |
| 2088 | if (start < end) |
| 2089 | goto next; |
Jaegeuk Kim | 8b107f5 | 2017-02-27 11:57:11 -0800 | [diff] [blame] | 2090 | else |
| 2091 | end = start - 1; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2092 | } |
| 2093 | mutex_unlock(&dirty_i->seglist_lock); |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 2094 | |
Chao Yu | 4f99326 | 2021-08-03 08:15:43 +0800 | [diff] [blame] | 2095 | if (!f2fs_block_unit_discard(sbi)) |
| 2096 | goto wakeup; |
| 2097 | |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 2098 | /* send small discards */ |
Chao Yu | 2d7b822 | 2014-03-29 11:33:17 +0800 | [diff] [blame] | 2099 | list_for_each_entry_safe(entry, this, head, list) { |
Chao Yu | a7eeb823 | 2017-03-28 18:18:50 +0800 | [diff] [blame] | 2100 | unsigned int cur_pos = 0, next_pos, len, total_len = 0; |
| 2101 | bool is_valid = test_bit_le(0, entry->discard_map); |
| 2102 | |
| 2103 | find_next: |
| 2104 | if (is_valid) { |
| 2105 | next_pos = find_next_zero_bit_le(entry->discard_map, |
| 2106 | sbi->blocks_per_seg, cur_pos); |
| 2107 | len = next_pos - cur_pos; |
| 2108 | |
Chao Yu | 7beb01f | 2018-10-24 18:34:26 +0800 | [diff] [blame] | 2109 | if (f2fs_sb_has_blkzoned(sbi) || |
Damien Le Moal | acfd2810 | 2017-05-26 17:04:40 +0900 | [diff] [blame] | 2110 | (force && len < cpc->trim_minlen)) |
Chao Yu | a7eeb823 | 2017-03-28 18:18:50 +0800 | [diff] [blame] | 2111 | goto skip; |
| 2112 | |
| 2113 | f2fs_issue_discard(sbi, entry->start_blkaddr + cur_pos, |
| 2114 | len); |
Chao Yu | a7eeb823 | 2017-03-28 18:18:50 +0800 | [diff] [blame] | 2115 | total_len += len; |
| 2116 | } else { |
| 2117 | next_pos = find_next_bit_le(entry->discard_map, |
| 2118 | sbi->blocks_per_seg, cur_pos); |
| 2119 | } |
Jaegeuk Kim | 836b5a6 | 2015-04-30 22:50:06 -0700 | [diff] [blame] | 2120 | skip: |
Chao Yu | a7eeb823 | 2017-03-28 18:18:50 +0800 | [diff] [blame] | 2121 | cur_pos = next_pos; |
| 2122 | is_valid = !is_valid; |
| 2123 | |
| 2124 | if (cur_pos < sbi->blocks_per_seg) |
| 2125 | goto find_next; |
| 2126 | |
Chao Yu | af8ff65 | 2018-04-25 17:38:29 +0800 | [diff] [blame] | 2127 | release_discard_addr(entry); |
Chao Yu | 969d1b1 | 2017-08-07 23:09:56 +0800 | [diff] [blame] | 2128 | dcc->nr_discards -= total_len; |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 2129 | } |
Chao Yu | 34e159d | 2017-04-25 00:21:34 +0800 | [diff] [blame] | 2130 | |
Chao Yu | 4f99326 | 2021-08-03 08:15:43 +0800 | [diff] [blame] | 2131 | wakeup: |
Jaegeuk Kim | 01983c7 | 2017-08-22 21:15:43 -0700 | [diff] [blame] | 2132 | wake_up_discard_thread(sbi, false); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2133 | } |
| 2134 | |
Fengnan Chang | 4d67490 | 2021-08-19 16:02:37 +0800 | [diff] [blame] | 2135 | int f2fs_start_discard_thread(struct f2fs_sb_info *sbi) |
Jaegeuk Kim | 0b54fb8 | 2017-01-11 14:40:24 -0800 | [diff] [blame] | 2136 | { |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 2137 | dev_t dev = sbi->sb->s_bdev->bd_dev; |
Fengnan Chang | 4d67490 | 2021-08-19 16:02:37 +0800 | [diff] [blame] | 2138 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
| 2139 | int err = 0; |
| 2140 | |
| 2141 | if (!f2fs_realtime_discard_enable(sbi)) |
| 2142 | return 0; |
| 2143 | |
| 2144 | dcc->f2fs_issue_discard = kthread_run(issue_discard_thread, sbi, |
| 2145 | "f2fs_discard-%u:%u", MAJOR(dev), MINOR(dev)); |
| 2146 | if (IS_ERR(dcc->f2fs_issue_discard)) |
| 2147 | err = PTR_ERR(dcc->f2fs_issue_discard); |
| 2148 | |
| 2149 | return err; |
| 2150 | } |
| 2151 | |
| 2152 | static int create_discard_cmd_control(struct f2fs_sb_info *sbi) |
| 2153 | { |
Jaegeuk Kim | 0b54fb8 | 2017-01-11 14:40:24 -0800 | [diff] [blame] | 2154 | struct discard_cmd_control *dcc; |
Chao Yu | ba48a33 | 2017-04-15 14:09:37 +0800 | [diff] [blame] | 2155 | int err = 0, i; |
Jaegeuk Kim | 0b54fb8 | 2017-01-11 14:40:24 -0800 | [diff] [blame] | 2156 | |
| 2157 | if (SM_I(sbi)->dcc_info) { |
| 2158 | dcc = SM_I(sbi)->dcc_info; |
| 2159 | goto init_thread; |
| 2160 | } |
| 2161 | |
Chao Yu | acbf054 | 2017-11-30 19:28:17 +0800 | [diff] [blame] | 2162 | dcc = f2fs_kzalloc(sbi, sizeof(struct discard_cmd_control), GFP_KERNEL); |
Jaegeuk Kim | 0b54fb8 | 2017-01-11 14:40:24 -0800 | [diff] [blame] | 2163 | if (!dcc) |
| 2164 | return -ENOMEM; |
| 2165 | |
Chao Yu | 969d1b1 | 2017-08-07 23:09:56 +0800 | [diff] [blame] | 2166 | dcc->discard_granularity = DEFAULT_DISCARD_GRANULARITY; |
Chao Yu | 4f99326 | 2021-08-03 08:15:43 +0800 | [diff] [blame] | 2167 | if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_SEGMENT) |
| 2168 | dcc->discard_granularity = sbi->blocks_per_seg; |
| 2169 | else if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_SECTION) |
| 2170 | dcc->discard_granularity = BLKS_PER_SEC(sbi); |
| 2171 | |
Chao Yu | 46f84c2 | 2017-04-15 14:09:36 +0800 | [diff] [blame] | 2172 | INIT_LIST_HEAD(&dcc->entry_list); |
Chao Yu | 78997b5 | 2017-10-04 09:08:34 +0800 | [diff] [blame] | 2173 | for (i = 0; i < MAX_PLIST_NUM; i++) |
Chao Yu | ba48a33 | 2017-04-15 14:09:37 +0800 | [diff] [blame] | 2174 | INIT_LIST_HEAD(&dcc->pend_list[i]); |
Chao Yu | 46f84c2 | 2017-04-15 14:09:36 +0800 | [diff] [blame] | 2175 | INIT_LIST_HEAD(&dcc->wait_list); |
Chao Yu | 8412663 | 2017-10-04 09:08:32 +0800 | [diff] [blame] | 2176 | INIT_LIST_HEAD(&dcc->fstrim_list); |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 2177 | mutex_init(&dcc->cmd_lock); |
Chao Yu | 8b8dd65 | 2017-03-25 17:19:58 +0800 | [diff] [blame] | 2178 | atomic_set(&dcc->issued_discard, 0); |
Jaegeuk Kim | 72691af | 2018-12-13 16:53:57 -0800 | [diff] [blame] | 2179 | atomic_set(&dcc->queued_discard, 0); |
Chao Yu | 5f32366 | 2017-03-25 17:19:59 +0800 | [diff] [blame] | 2180 | atomic_set(&dcc->discard_cmd_cnt, 0); |
Jaegeuk Kim | 0b54fb8 | 2017-01-11 14:40:24 -0800 | [diff] [blame] | 2181 | dcc->nr_discards = 0; |
Chao Yu | d618eba | 2017-04-25 00:21:35 +0800 | [diff] [blame] | 2182 | dcc->max_discards = MAIN_SEGS(sbi) << sbi->log_blocks_per_seg; |
Chao Yu | d84d1cb | 2017-04-18 19:27:39 +0800 | [diff] [blame] | 2183 | dcc->undiscard_blks = 0; |
Chao Yu | 20ee438 | 2018-07-08 22:11:01 +0800 | [diff] [blame] | 2184 | dcc->next_pos = 0; |
Chao Yu | 4dada3f | 2018-10-04 11:18:30 +0800 | [diff] [blame] | 2185 | dcc->root = RB_ROOT_CACHED; |
Chao Yu | 67fce70 | 2018-06-22 16:06:59 +0800 | [diff] [blame] | 2186 | dcc->rbtree_check = false; |
Jaegeuk Kim | 0b54fb8 | 2017-01-11 14:40:24 -0800 | [diff] [blame] | 2187 | |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 2188 | init_waitqueue_head(&dcc->discard_wait_queue); |
Jaegeuk Kim | 0b54fb8 | 2017-01-11 14:40:24 -0800 | [diff] [blame] | 2189 | SM_I(sbi)->dcc_info = dcc; |
| 2190 | init_thread: |
Fengnan Chang | 4d67490 | 2021-08-19 16:02:37 +0800 | [diff] [blame] | 2191 | err = f2fs_start_discard_thread(sbi); |
| 2192 | if (err) { |
Chao Yu | c8eb702 | 2020-09-14 16:47:00 +0800 | [diff] [blame] | 2193 | kfree(dcc); |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 2194 | SM_I(sbi)->dcc_info = NULL; |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 2195 | } |
| 2196 | |
Jaegeuk Kim | 0b54fb8 | 2017-01-11 14:40:24 -0800 | [diff] [blame] | 2197 | return err; |
| 2198 | } |
| 2199 | |
Chao Yu | f099405 | 2017-03-27 18:14:04 +0800 | [diff] [blame] | 2200 | static void destroy_discard_cmd_control(struct f2fs_sb_info *sbi) |
Jaegeuk Kim | 0b54fb8 | 2017-01-11 14:40:24 -0800 | [diff] [blame] | 2201 | { |
| 2202 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
| 2203 | |
Chao Yu | f099405 | 2017-03-27 18:14:04 +0800 | [diff] [blame] | 2204 | if (!dcc) |
| 2205 | return; |
| 2206 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2207 | f2fs_stop_discard_thread(sbi); |
Chao Yu | f099405 | 2017-03-27 18:14:04 +0800 | [diff] [blame] | 2208 | |
Chao Yu | 04f9287 | 2019-07-19 15:18:44 +0800 | [diff] [blame] | 2209 | /* |
| 2210 | * Recovery can cache discard commands, so in error path of |
| 2211 | * fill_super(), it needs to give a chance to handle them. |
| 2212 | */ |
| 2213 | if (unlikely(atomic_read(&dcc->discard_cmd_cnt))) |
| 2214 | f2fs_issue_discard_timeout(sbi); |
| 2215 | |
Chao Yu | c8eb702 | 2020-09-14 16:47:00 +0800 | [diff] [blame] | 2216 | kfree(dcc); |
Chao Yu | f099405 | 2017-03-27 18:14:04 +0800 | [diff] [blame] | 2217 | SM_I(sbi)->dcc_info = NULL; |
Jaegeuk Kim | 0b54fb8 | 2017-01-11 14:40:24 -0800 | [diff] [blame] | 2218 | } |
| 2219 | |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 2220 | static bool __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2221 | { |
| 2222 | struct sit_info *sit_i = SIT_I(sbi); |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 2223 | |
| 2224 | if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap)) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2225 | sit_i->dirty_sentries++; |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 2226 | return false; |
| 2227 | } |
| 2228 | |
| 2229 | return true; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2230 | } |
| 2231 | |
| 2232 | static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type, |
| 2233 | unsigned int segno, int modified) |
| 2234 | { |
| 2235 | struct seg_entry *se = get_seg_entry(sbi, segno); |
Yi Zhuang | 5f029c0 | 2021-04-06 09:47:35 +0800 | [diff] [blame] | 2236 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2237 | se->type = type; |
| 2238 | if (modified) |
| 2239 | __mark_sit_entry_dirty(sbi, segno); |
| 2240 | } |
| 2241 | |
Chao Yu | c5d0278 | 2020-08-04 21:14:47 +0800 | [diff] [blame] | 2242 | static inline unsigned long long get_segment_mtime(struct f2fs_sb_info *sbi, |
| 2243 | block_t blkaddr) |
Chao Yu | 6f3a01a | 2020-08-04 21:14:46 +0800 | [diff] [blame] | 2244 | { |
| 2245 | unsigned int segno = GET_SEGNO(sbi, blkaddr); |
Chao Yu | c5d0278 | 2020-08-04 21:14:47 +0800 | [diff] [blame] | 2246 | |
| 2247 | if (segno == NULL_SEGNO) |
| 2248 | return 0; |
| 2249 | return get_seg_entry(sbi, segno)->mtime; |
| 2250 | } |
| 2251 | |
| 2252 | static void update_segment_mtime(struct f2fs_sb_info *sbi, block_t blkaddr, |
| 2253 | unsigned long long old_mtime) |
| 2254 | { |
| 2255 | struct seg_entry *se; |
| 2256 | unsigned int segno = GET_SEGNO(sbi, blkaddr); |
| 2257 | unsigned long long ctime = get_mtime(sbi, false); |
| 2258 | unsigned long long mtime = old_mtime ? old_mtime : ctime; |
| 2259 | |
| 2260 | if (segno == NULL_SEGNO) |
| 2261 | return; |
| 2262 | |
| 2263 | se = get_seg_entry(sbi, segno); |
Chao Yu | 6f3a01a | 2020-08-04 21:14:46 +0800 | [diff] [blame] | 2264 | |
| 2265 | if (!se->mtime) |
| 2266 | se->mtime = mtime; |
| 2267 | else |
| 2268 | se->mtime = div_u64(se->mtime * se->valid_blocks + mtime, |
| 2269 | se->valid_blocks + 1); |
| 2270 | |
Chao Yu | c5d0278 | 2020-08-04 21:14:47 +0800 | [diff] [blame] | 2271 | if (ctime > SIT_I(sbi)->max_mtime) |
| 2272 | SIT_I(sbi)->max_mtime = ctime; |
Chao Yu | 6f3a01a | 2020-08-04 21:14:46 +0800 | [diff] [blame] | 2273 | } |
| 2274 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2275 | static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del) |
| 2276 | { |
| 2277 | struct seg_entry *se; |
| 2278 | unsigned int segno, offset; |
| 2279 | long int new_vblocks; |
Yunlong Song | 6415fed | 2017-08-02 21:20:13 +0800 | [diff] [blame] | 2280 | bool exist; |
| 2281 | #ifdef CONFIG_F2FS_CHECK_FS |
| 2282 | bool mir_exist; |
| 2283 | #endif |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2284 | |
| 2285 | segno = GET_SEGNO(sbi, blkaddr); |
| 2286 | |
| 2287 | se = get_seg_entry(sbi, segno); |
| 2288 | new_vblocks = se->valid_blocks + del; |
Jaegeuk Kim | 491c085 | 2014-02-04 13:01:10 +0900 | [diff] [blame] | 2289 | offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2290 | |
Zhihao Cheng | 9feffe14 | 2020-08-01 11:24:50 +0800 | [diff] [blame] | 2291 | f2fs_bug_on(sbi, (new_vblocks < 0 || |
Aravind Ramesh | de881df | 2020-07-16 18:26:56 +0530 | [diff] [blame] | 2292 | (new_vblocks > f2fs_usable_blks_in_seg(sbi, segno)))); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2293 | |
| 2294 | se->valid_blocks = new_vblocks; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2295 | |
| 2296 | /* Update valid block bitmap */ |
| 2297 | if (del > 0) { |
Yunlong Song | 6415fed | 2017-08-02 21:20:13 +0800 | [diff] [blame] | 2298 | exist = f2fs_test_and_set_bit(offset, se->cur_valid_map); |
Chao Yu | 355e789 | 2017-01-07 18:51:01 +0800 | [diff] [blame] | 2299 | #ifdef CONFIG_F2FS_CHECK_FS |
Yunlong Song | 6415fed | 2017-08-02 21:20:13 +0800 | [diff] [blame] | 2300 | mir_exist = f2fs_test_and_set_bit(offset, |
| 2301 | se->cur_valid_map_mir); |
| 2302 | if (unlikely(exist != mir_exist)) { |
Joe Perches | dcbb4c1 | 2019-06-18 17:48:42 +0800 | [diff] [blame] | 2303 | f2fs_err(sbi, "Inconsistent error when setting bitmap, blk:%u, old bit:%d", |
| 2304 | blkaddr, exist); |
Jaegeuk Kim | 0579676 | 2014-09-02 16:05:00 -0700 | [diff] [blame] | 2305 | f2fs_bug_on(sbi, 1); |
Chao Yu | 355e789 | 2017-01-07 18:51:01 +0800 | [diff] [blame] | 2306 | } |
Yunlong Song | 6415fed | 2017-08-02 21:20:13 +0800 | [diff] [blame] | 2307 | #endif |
| 2308 | if (unlikely(exist)) { |
Joe Perches | dcbb4c1 | 2019-06-18 17:48:42 +0800 | [diff] [blame] | 2309 | f2fs_err(sbi, "Bitmap was wrongly set, blk:%u", |
| 2310 | blkaddr); |
Yunlong Song | 6415fed | 2017-08-02 21:20:13 +0800 | [diff] [blame] | 2311 | f2fs_bug_on(sbi, 1); |
Yunlong Song | 35ee82c | 2017-08-02 22:16:54 +0800 | [diff] [blame] | 2312 | se->valid_blocks--; |
| 2313 | del = 0; |
Yunlong Song | 6415fed | 2017-08-02 21:20:13 +0800 | [diff] [blame] | 2314 | } |
| 2315 | |
Chao Yu | 4f99326 | 2021-08-03 08:15:43 +0800 | [diff] [blame] | 2316 | if (f2fs_block_unit_discard(sbi) && |
| 2317 | !f2fs_test_and_set_bit(offset, se->discard_map)) |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 2318 | sbi->discard_blks--; |
Jaegeuk Kim | 720037f | 2017-03-06 11:59:56 -0800 | [diff] [blame] | 2319 | |
Chao Yu | 899fee3 | 2019-08-16 11:03:34 +0800 | [diff] [blame] | 2320 | /* |
| 2321 | * SSR should never reuse block which is checkpointed |
| 2322 | * or newly invalidated. |
| 2323 | */ |
| 2324 | if (!is_sbi_flag_set(sbi, SBI_CP_DISABLED)) { |
Jaegeuk Kim | 720037f | 2017-03-06 11:59:56 -0800 | [diff] [blame] | 2325 | if (!f2fs_test_and_set_bit(offset, se->ckpt_valid_map)) |
| 2326 | se->ckpt_valid_blocks++; |
| 2327 | } |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2328 | } else { |
Yunlong Song | 6415fed | 2017-08-02 21:20:13 +0800 | [diff] [blame] | 2329 | exist = f2fs_test_and_clear_bit(offset, se->cur_valid_map); |
Chao Yu | 355e789 | 2017-01-07 18:51:01 +0800 | [diff] [blame] | 2330 | #ifdef CONFIG_F2FS_CHECK_FS |
Yunlong Song | 6415fed | 2017-08-02 21:20:13 +0800 | [diff] [blame] | 2331 | mir_exist = f2fs_test_and_clear_bit(offset, |
| 2332 | se->cur_valid_map_mir); |
| 2333 | if (unlikely(exist != mir_exist)) { |
Joe Perches | dcbb4c1 | 2019-06-18 17:48:42 +0800 | [diff] [blame] | 2334 | f2fs_err(sbi, "Inconsistent error when clearing bitmap, blk:%u, old bit:%d", |
| 2335 | blkaddr, exist); |
Jaegeuk Kim | 0579676 | 2014-09-02 16:05:00 -0700 | [diff] [blame] | 2336 | f2fs_bug_on(sbi, 1); |
Chao Yu | 355e789 | 2017-01-07 18:51:01 +0800 | [diff] [blame] | 2337 | } |
Yunlong Song | 6415fed | 2017-08-02 21:20:13 +0800 | [diff] [blame] | 2338 | #endif |
| 2339 | if (unlikely(!exist)) { |
Joe Perches | dcbb4c1 | 2019-06-18 17:48:42 +0800 | [diff] [blame] | 2340 | f2fs_err(sbi, "Bitmap was wrongly cleared, blk:%u", |
| 2341 | blkaddr); |
Yunlong Song | 6415fed | 2017-08-02 21:20:13 +0800 | [diff] [blame] | 2342 | f2fs_bug_on(sbi, 1); |
Yunlong Song | 35ee82c | 2017-08-02 22:16:54 +0800 | [diff] [blame] | 2343 | se->valid_blocks++; |
| 2344 | del = 0; |
Daniel Rosenberg | 4354994 | 2018-08-20 19:21:43 -0700 | [diff] [blame] | 2345 | } else if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) { |
| 2346 | /* |
| 2347 | * If checkpoints are off, we must not reuse data that |
| 2348 | * was used in the previous checkpoint. If it was used |
| 2349 | * before, we must track that to know how much space we |
| 2350 | * really have. |
| 2351 | */ |
Chao Yu | c9c8ed5 | 2019-05-05 11:40:46 +0800 | [diff] [blame] | 2352 | if (f2fs_test_bit(offset, se->ckpt_valid_map)) { |
| 2353 | spin_lock(&sbi->stat_lock); |
Daniel Rosenberg | 4354994 | 2018-08-20 19:21:43 -0700 | [diff] [blame] | 2354 | sbi->unusable_block_count++; |
Chao Yu | c9c8ed5 | 2019-05-05 11:40:46 +0800 | [diff] [blame] | 2355 | spin_unlock(&sbi->stat_lock); |
| 2356 | } |
Yunlong Song | 6415fed | 2017-08-02 21:20:13 +0800 | [diff] [blame] | 2357 | } |
| 2358 | |
Chao Yu | 4f99326 | 2021-08-03 08:15:43 +0800 | [diff] [blame] | 2359 | if (f2fs_block_unit_discard(sbi) && |
| 2360 | f2fs_test_and_clear_bit(offset, se->discard_map)) |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 2361 | sbi->discard_blks++; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2362 | } |
| 2363 | if (!f2fs_test_bit(offset, se->ckpt_valid_map)) |
| 2364 | se->ckpt_valid_blocks += del; |
| 2365 | |
| 2366 | __mark_sit_entry_dirty(sbi, segno); |
| 2367 | |
| 2368 | /* update total number of valid blocks to be written in ckpt area */ |
| 2369 | SIT_I(sbi)->written_valid_blocks += del; |
| 2370 | |
Chao Yu | 2c70c5e | 2018-10-24 18:37:26 +0800 | [diff] [blame] | 2371 | if (__is_large_section(sbi)) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2372 | get_sec_entry(sbi, segno)->valid_blocks += del; |
| 2373 | } |
| 2374 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2375 | void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2376 | { |
| 2377 | unsigned int segno = GET_SEGNO(sbi, addr); |
| 2378 | struct sit_info *sit_i = SIT_I(sbi); |
| 2379 | |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 2380 | f2fs_bug_on(sbi, addr == NULL_ADDR); |
Chao Yu | 4c8ff70 | 2019-11-01 18:07:14 +0800 | [diff] [blame] | 2381 | if (addr == NEW_ADDR || addr == COMPRESS_ADDR) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2382 | return; |
| 2383 | |
Chao Yu | 6aa58d8 | 2018-08-14 22:37:25 +0800 | [diff] [blame] | 2384 | invalidate_mapping_pages(META_MAPPING(sbi), addr, addr); |
Chao Yu | 6ce19af | 2021-05-20 19:51:50 +0800 | [diff] [blame] | 2385 | f2fs_invalidate_compress_page(sbi, addr); |
Chao Yu | 6aa58d8 | 2018-08-14 22:37:25 +0800 | [diff] [blame] | 2386 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2387 | /* add it into sit main buffer */ |
Chao Yu | 3d26fa6 | 2017-10-30 17:49:53 +0800 | [diff] [blame] | 2388 | down_write(&sit_i->sentry_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2389 | |
Chao Yu | c5d0278 | 2020-08-04 21:14:47 +0800 | [diff] [blame] | 2390 | update_segment_mtime(sbi, addr, 0); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2391 | update_sit_entry(sbi, addr, -1); |
| 2392 | |
| 2393 | /* add it into dirty seglist */ |
| 2394 | locate_dirty_segment(sbi, segno); |
| 2395 | |
Chao Yu | 3d26fa6 | 2017-10-30 17:49:53 +0800 | [diff] [blame] | 2396 | up_write(&sit_i->sentry_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2397 | } |
| 2398 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2399 | bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr) |
Jaegeuk Kim | 6e2c64a | 2015-10-07 12:28:41 -0700 | [diff] [blame] | 2400 | { |
| 2401 | struct sit_info *sit_i = SIT_I(sbi); |
| 2402 | unsigned int segno, offset; |
| 2403 | struct seg_entry *se; |
| 2404 | bool is_cp = false; |
| 2405 | |
Chao Yu | 93770ab | 2019-04-15 15:26:32 +0800 | [diff] [blame] | 2406 | if (!__is_valid_data_blkaddr(blkaddr)) |
Jaegeuk Kim | 6e2c64a | 2015-10-07 12:28:41 -0700 | [diff] [blame] | 2407 | return true; |
| 2408 | |
Chao Yu | 3d26fa6 | 2017-10-30 17:49:53 +0800 | [diff] [blame] | 2409 | down_read(&sit_i->sentry_lock); |
Jaegeuk Kim | 6e2c64a | 2015-10-07 12:28:41 -0700 | [diff] [blame] | 2410 | |
| 2411 | segno = GET_SEGNO(sbi, blkaddr); |
| 2412 | se = get_seg_entry(sbi, segno); |
| 2413 | offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr); |
| 2414 | |
| 2415 | if (f2fs_test_bit(offset, se->ckpt_valid_map)) |
| 2416 | is_cp = true; |
| 2417 | |
Chao Yu | 3d26fa6 | 2017-10-30 17:49:53 +0800 | [diff] [blame] | 2418 | up_read(&sit_i->sentry_lock); |
Jaegeuk Kim | 6e2c64a | 2015-10-07 12:28:41 -0700 | [diff] [blame] | 2419 | |
| 2420 | return is_cp; |
| 2421 | } |
| 2422 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 2423 | /* |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2424 | * This function should be resided under the curseg_mutex lock |
| 2425 | */ |
| 2426 | static void __add_sum_entry(struct f2fs_sb_info *sbi, int type, |
Haicheng Li | e79efe3 | 2013-06-13 16:59:27 +0800 | [diff] [blame] | 2427 | struct f2fs_summary *sum) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2428 | { |
| 2429 | struct curseg_info *curseg = CURSEG_I(sbi, type); |
| 2430 | void *addr = curseg->sum_blk; |
Yi Zhuang | 5f029c0 | 2021-04-06 09:47:35 +0800 | [diff] [blame] | 2431 | |
Haicheng Li | e79efe3 | 2013-06-13 16:59:27 +0800 | [diff] [blame] | 2432 | addr += curseg->next_blkoff * sizeof(struct f2fs_summary); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2433 | memcpy(addr, sum, sizeof(struct f2fs_summary)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2434 | } |
| 2435 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 2436 | /* |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2437 | * Calculate the number of current summary pages for writing |
| 2438 | */ |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2439 | int f2fs_npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2440 | { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2441 | int valid_sum_count = 0; |
Fan Li | 9a47938 | 2013-10-29 16:21:47 +0800 | [diff] [blame] | 2442 | int i, sum_in_page; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2443 | |
| 2444 | for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) { |
| 2445 | if (sbi->ckpt->alloc_type[i] == SSR) |
| 2446 | valid_sum_count += sbi->blocks_per_seg; |
Chao Yu | 3fa06d7 | 2014-12-09 14:21:46 +0800 | [diff] [blame] | 2447 | else { |
| 2448 | if (for_ra) |
| 2449 | valid_sum_count += le16_to_cpu( |
| 2450 | F2FS_CKPT(sbi)->cur_data_blkoff[i]); |
| 2451 | else |
| 2452 | valid_sum_count += curseg_blkoff(sbi, i); |
| 2453 | } |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2454 | } |
| 2455 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2456 | sum_in_page = (PAGE_SIZE - 2 * SUM_JOURNAL_SIZE - |
Fan Li | 9a47938 | 2013-10-29 16:21:47 +0800 | [diff] [blame] | 2457 | SUM_FOOTER_SIZE) / SUMMARY_SIZE; |
| 2458 | if (valid_sum_count <= sum_in_page) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2459 | return 1; |
Fan Li | 9a47938 | 2013-10-29 16:21:47 +0800 | [diff] [blame] | 2460 | else if ((valid_sum_count - sum_in_page) <= |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2461 | (PAGE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2462 | return 2; |
| 2463 | return 3; |
| 2464 | } |
| 2465 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 2466 | /* |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2467 | * Caller should put this summary page |
| 2468 | */ |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2469 | struct page *f2fs_get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2470 | { |
Jaegeuk Kim | 86f3360 | 2020-10-02 14:17:35 -0700 | [diff] [blame] | 2471 | if (unlikely(f2fs_cp_error(sbi))) |
| 2472 | return ERR_PTR(-EIO); |
| 2473 | return f2fs_get_meta_page_retry(sbi, GET_SUM_BLOCK(sbi, segno)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2474 | } |
| 2475 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2476 | void f2fs_update_meta_page(struct f2fs_sb_info *sbi, |
| 2477 | void *src, block_t blk_addr) |
Chao Yu | 381722d | 2015-05-19 17:40:04 +0800 | [diff] [blame] | 2478 | { |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2479 | struct page *page = f2fs_grab_meta_page(sbi, blk_addr); |
Chao Yu | 381722d | 2015-05-19 17:40:04 +0800 | [diff] [blame] | 2480 | |
Chao Yu | 0537b81 | 2017-11-02 20:41:02 +0800 | [diff] [blame] | 2481 | memcpy(page_address(page), src, PAGE_SIZE); |
Chao Yu | 381722d | 2015-05-19 17:40:04 +0800 | [diff] [blame] | 2482 | set_page_dirty(page); |
| 2483 | f2fs_put_page(page, 1); |
| 2484 | } |
| 2485 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2486 | static void write_sum_page(struct f2fs_sb_info *sbi, |
| 2487 | struct f2fs_summary_block *sum_blk, block_t blk_addr) |
| 2488 | { |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2489 | f2fs_update_meta_page(sbi, (void *)sum_blk, blk_addr); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2490 | } |
| 2491 | |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 2492 | static void write_current_sum_page(struct f2fs_sb_info *sbi, |
| 2493 | int type, block_t blk_addr) |
| 2494 | { |
| 2495 | struct curseg_info *curseg = CURSEG_I(sbi, type); |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2496 | struct page *page = f2fs_grab_meta_page(sbi, blk_addr); |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 2497 | struct f2fs_summary_block *src = curseg->sum_blk; |
| 2498 | struct f2fs_summary_block *dst; |
| 2499 | |
| 2500 | dst = (struct f2fs_summary_block *)page_address(page); |
Chao Yu | 81114ba | 2018-04-09 20:25:06 +0800 | [diff] [blame] | 2501 | memset(dst, 0, PAGE_SIZE); |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 2502 | |
| 2503 | mutex_lock(&curseg->curseg_mutex); |
| 2504 | |
| 2505 | down_read(&curseg->journal_rwsem); |
| 2506 | memcpy(&dst->journal, curseg->journal, SUM_JOURNAL_SIZE); |
| 2507 | up_read(&curseg->journal_rwsem); |
| 2508 | |
| 2509 | memcpy(dst->entries, src->entries, SUM_ENTRY_SIZE); |
| 2510 | memcpy(&dst->footer, &src->footer, SUM_FOOTER_SIZE); |
| 2511 | |
| 2512 | mutex_unlock(&curseg->curseg_mutex); |
| 2513 | |
| 2514 | set_page_dirty(page); |
| 2515 | f2fs_put_page(page, 1); |
| 2516 | } |
| 2517 | |
Chao Yu | 093749e | 2020-08-04 21:14:49 +0800 | [diff] [blame] | 2518 | static int is_next_segment_free(struct f2fs_sb_info *sbi, |
| 2519 | struct curseg_info *curseg, int type) |
Jaegeuk Kim | a788189 | 2017-04-20 13:51:57 -0700 | [diff] [blame] | 2520 | { |
Jaegeuk Kim | a788189 | 2017-04-20 13:51:57 -0700 | [diff] [blame] | 2521 | unsigned int segno = curseg->segno + 1; |
| 2522 | struct free_segmap_info *free_i = FREE_I(sbi); |
| 2523 | |
| 2524 | if (segno < MAIN_SEGS(sbi) && segno % sbi->segs_per_sec) |
| 2525 | return !test_bit(segno, free_i->free_segmap); |
| 2526 | return 0; |
| 2527 | } |
| 2528 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 2529 | /* |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2530 | * Find a new segment from the free segments bitmap to right order |
| 2531 | * This function should be returned with success, otherwise BUG |
| 2532 | */ |
| 2533 | static void get_new_segment(struct f2fs_sb_info *sbi, |
| 2534 | unsigned int *newseg, bool new_sec, int dir) |
| 2535 | { |
| 2536 | struct free_segmap_info *free_i = FREE_I(sbi); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2537 | unsigned int segno, secno, zoneno; |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 2538 | unsigned int total_zones = MAIN_SECS(sbi) / sbi->secs_per_zone; |
Jaegeuk Kim | 4ddb1a4 | 2017-04-07 15:08:17 -0700 | [diff] [blame] | 2539 | unsigned int hint = GET_SEC_FROM_SEG(sbi, *newseg); |
| 2540 | unsigned int old_zoneno = GET_ZONE_FROM_SEG(sbi, *newseg); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2541 | unsigned int left_start = hint; |
| 2542 | bool init = true; |
| 2543 | int go_left = 0; |
| 2544 | int i; |
| 2545 | |
Chao Yu | 1a118cc | 2015-02-11 18:20:38 +0800 | [diff] [blame] | 2546 | spin_lock(&free_i->segmap_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2547 | |
| 2548 | if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) { |
| 2549 | segno = find_next_zero_bit(free_i->free_segmap, |
Jaegeuk Kim | 4ddb1a4 | 2017-04-07 15:08:17 -0700 | [diff] [blame] | 2550 | GET_SEG_FROM_SEC(sbi, hint + 1), *newseg + 1); |
| 2551 | if (segno < GET_SEG_FROM_SEC(sbi, hint + 1)) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2552 | goto got_it; |
| 2553 | } |
| 2554 | find_other_zone: |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 2555 | secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint); |
| 2556 | if (secno >= MAIN_SECS(sbi)) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2557 | if (dir == ALLOC_RIGHT) { |
Yury Norov | b5c7e7e | 2021-08-14 14:17:03 -0700 | [diff] [blame] | 2558 | secno = find_first_zero_bit(free_i->free_secmap, |
| 2559 | MAIN_SECS(sbi)); |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 2560 | f2fs_bug_on(sbi, secno >= MAIN_SECS(sbi)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2561 | } else { |
| 2562 | go_left = 1; |
| 2563 | left_start = hint - 1; |
| 2564 | } |
| 2565 | } |
| 2566 | if (go_left == 0) |
| 2567 | goto skip_left; |
| 2568 | |
| 2569 | while (test_bit(left_start, free_i->free_secmap)) { |
| 2570 | if (left_start > 0) { |
| 2571 | left_start--; |
| 2572 | continue; |
| 2573 | } |
Yury Norov | b5c7e7e | 2021-08-14 14:17:03 -0700 | [diff] [blame] | 2574 | left_start = find_first_zero_bit(free_i->free_secmap, |
| 2575 | MAIN_SECS(sbi)); |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 2576 | f2fs_bug_on(sbi, left_start >= MAIN_SECS(sbi)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2577 | break; |
| 2578 | } |
| 2579 | secno = left_start; |
| 2580 | skip_left: |
Jaegeuk Kim | 4ddb1a4 | 2017-04-07 15:08:17 -0700 | [diff] [blame] | 2581 | segno = GET_SEG_FROM_SEC(sbi, secno); |
| 2582 | zoneno = GET_ZONE_FROM_SEC(sbi, secno); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2583 | |
| 2584 | /* give up on finding another zone */ |
| 2585 | if (!init) |
| 2586 | goto got_it; |
| 2587 | if (sbi->secs_per_zone == 1) |
| 2588 | goto got_it; |
| 2589 | if (zoneno == old_zoneno) |
| 2590 | goto got_it; |
| 2591 | if (dir == ALLOC_LEFT) { |
| 2592 | if (!go_left && zoneno + 1 >= total_zones) |
| 2593 | goto got_it; |
| 2594 | if (go_left && zoneno == 0) |
| 2595 | goto got_it; |
| 2596 | } |
| 2597 | for (i = 0; i < NR_CURSEG_TYPE; i++) |
| 2598 | if (CURSEG_I(sbi, i)->zone == zoneno) |
| 2599 | break; |
| 2600 | |
| 2601 | if (i < NR_CURSEG_TYPE) { |
| 2602 | /* zone is in user, try another */ |
| 2603 | if (go_left) |
| 2604 | hint = zoneno * sbi->secs_per_zone - 1; |
| 2605 | else if (zoneno + 1 >= total_zones) |
| 2606 | hint = 0; |
| 2607 | else |
| 2608 | hint = (zoneno + 1) * sbi->secs_per_zone; |
| 2609 | init = false; |
| 2610 | goto find_other_zone; |
| 2611 | } |
| 2612 | got_it: |
| 2613 | /* set it as dirty segment in free segmap */ |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 2614 | f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2615 | __set_inuse(sbi, segno); |
| 2616 | *newseg = segno; |
Chao Yu | 1a118cc | 2015-02-11 18:20:38 +0800 | [diff] [blame] | 2617 | spin_unlock(&free_i->segmap_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2618 | } |
| 2619 | |
| 2620 | static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified) |
| 2621 | { |
| 2622 | struct curseg_info *curseg = CURSEG_I(sbi, type); |
| 2623 | struct summary_footer *sum_footer; |
Chao Yu | 093749e | 2020-08-04 21:14:49 +0800 | [diff] [blame] | 2624 | unsigned short seg_type = curseg->seg_type; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2625 | |
Chao Yu | d0b9e42 | 2020-08-04 21:14:45 +0800 | [diff] [blame] | 2626 | curseg->inited = true; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2627 | curseg->segno = curseg->next_segno; |
Jaegeuk Kim | 4ddb1a4 | 2017-04-07 15:08:17 -0700 | [diff] [blame] | 2628 | curseg->zone = GET_ZONE_FROM_SEG(sbi, curseg->segno); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2629 | curseg->next_blkoff = 0; |
| 2630 | curseg->next_segno = NULL_SEGNO; |
| 2631 | |
| 2632 | sum_footer = &(curseg->sum_blk->footer); |
| 2633 | memset(sum_footer, 0, sizeof(struct summary_footer)); |
Chao Yu | 093749e | 2020-08-04 21:14:49 +0800 | [diff] [blame] | 2634 | |
| 2635 | sanity_check_seg_type(sbi, seg_type); |
| 2636 | |
| 2637 | if (IS_DATASEG(seg_type)) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2638 | SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA); |
Chao Yu | 093749e | 2020-08-04 21:14:49 +0800 | [diff] [blame] | 2639 | if (IS_NODESEG(seg_type)) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2640 | SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE); |
Chao Yu | 093749e | 2020-08-04 21:14:49 +0800 | [diff] [blame] | 2641 | __set_sit_entry_type(sbi, seg_type, curseg->segno, modified); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2642 | } |
| 2643 | |
Jaegeuk Kim | 7a20b8a | 2017-03-24 20:41:45 -0400 | [diff] [blame] | 2644 | static unsigned int __get_next_segno(struct f2fs_sb_info *sbi, int type) |
| 2645 | { |
Chao Yu | d0b9e42 | 2020-08-04 21:14:45 +0800 | [diff] [blame] | 2646 | struct curseg_info *curseg = CURSEG_I(sbi, type); |
Chao Yu | 093749e | 2020-08-04 21:14:49 +0800 | [diff] [blame] | 2647 | unsigned short seg_type = curseg->seg_type; |
| 2648 | |
| 2649 | sanity_check_seg_type(sbi, seg_type); |
Daeho Jeong | 6691d94 | 2021-09-29 11:12:03 -0700 | [diff] [blame] | 2650 | if (f2fs_need_rand_seg(sbi)) |
| 2651 | return prandom_u32() % (MAIN_SECS(sbi) * sbi->segs_per_sec); |
Chao Yu | d0b9e42 | 2020-08-04 21:14:45 +0800 | [diff] [blame] | 2652 | |
Jaegeuk Kim | a788189 | 2017-04-20 13:51:57 -0700 | [diff] [blame] | 2653 | /* if segs_per_sec is large than 1, we need to keep original policy. */ |
Chao Yu | 2c70c5e | 2018-10-24 18:37:26 +0800 | [diff] [blame] | 2654 | if (__is_large_section(sbi)) |
Chao Yu | d0b9e42 | 2020-08-04 21:14:45 +0800 | [diff] [blame] | 2655 | return curseg->segno; |
| 2656 | |
| 2657 | /* inmem log may not locate on any segment after mount */ |
| 2658 | if (!curseg->inited) |
| 2659 | return 0; |
Jaegeuk Kim | a788189 | 2017-04-20 13:51:57 -0700 | [diff] [blame] | 2660 | |
Daniel Rosenberg | 4354994 | 2018-08-20 19:21:43 -0700 | [diff] [blame] | 2661 | if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) |
| 2662 | return 0; |
| 2663 | |
Yunlong Song | b94929d | 2018-01-29 11:37:45 +0800 | [diff] [blame] | 2664 | if (test_opt(sbi, NOHEAP) && |
Chao Yu | 093749e | 2020-08-04 21:14:49 +0800 | [diff] [blame] | 2665 | (seg_type == CURSEG_HOT_DATA || IS_NODESEG(seg_type))) |
Jaegeuk Kim | 7a20b8a | 2017-03-24 20:41:45 -0400 | [diff] [blame] | 2666 | return 0; |
| 2667 | |
Jaegeuk Kim | e066b83 | 2017-04-13 15:17:00 -0700 | [diff] [blame] | 2668 | if (SIT_I(sbi)->last_victim[ALLOC_NEXT]) |
| 2669 | return SIT_I(sbi)->last_victim[ALLOC_NEXT]; |
Jaegeuk Kim | 0793962 | 2018-02-18 08:50:49 -0800 | [diff] [blame] | 2670 | |
| 2671 | /* find segments from 0 to reuse freed segments */ |
Chao Yu | 63189b7 | 2018-03-08 14:22:56 +0800 | [diff] [blame] | 2672 | if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_REUSE) |
Jaegeuk Kim | 0793962 | 2018-02-18 08:50:49 -0800 | [diff] [blame] | 2673 | return 0; |
| 2674 | |
Chao Yu | d0b9e42 | 2020-08-04 21:14:45 +0800 | [diff] [blame] | 2675 | return curseg->segno; |
Jaegeuk Kim | 7a20b8a | 2017-03-24 20:41:45 -0400 | [diff] [blame] | 2676 | } |
| 2677 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 2678 | /* |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2679 | * Allocate a current working segment. |
| 2680 | * This function always allocates a free segment in LFS manner. |
| 2681 | */ |
| 2682 | static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec) |
| 2683 | { |
| 2684 | struct curseg_info *curseg = CURSEG_I(sbi, type); |
Chao Yu | d0b9e42 | 2020-08-04 21:14:45 +0800 | [diff] [blame] | 2685 | unsigned short seg_type = curseg->seg_type; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2686 | unsigned int segno = curseg->segno; |
| 2687 | int dir = ALLOC_LEFT; |
| 2688 | |
Chao Yu | d0b9e42 | 2020-08-04 21:14:45 +0800 | [diff] [blame] | 2689 | if (curseg->inited) |
| 2690 | write_sum_page(sbi, curseg->sum_blk, |
Haicheng Li | 81fb5e8 | 2013-05-14 18:20:28 +0800 | [diff] [blame] | 2691 | GET_SUM_BLOCK(sbi, segno)); |
Chao Yu | d0b9e42 | 2020-08-04 21:14:45 +0800 | [diff] [blame] | 2692 | if (seg_type == CURSEG_WARM_DATA || seg_type == CURSEG_COLD_DATA) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2693 | dir = ALLOC_RIGHT; |
| 2694 | |
| 2695 | if (test_opt(sbi, NOHEAP)) |
| 2696 | dir = ALLOC_RIGHT; |
| 2697 | |
Jaegeuk Kim | 7a20b8a | 2017-03-24 20:41:45 -0400 | [diff] [blame] | 2698 | segno = __get_next_segno(sbi, type); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2699 | get_new_segment(sbi, &segno, new_sec, dir); |
| 2700 | curseg->next_segno = segno; |
| 2701 | reset_curseg(sbi, type, 1); |
| 2702 | curseg->alloc_type = LFS; |
Daeho Jeong | 6691d94 | 2021-09-29 11:12:03 -0700 | [diff] [blame] | 2703 | if (F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_BLK) |
| 2704 | curseg->fragment_remained_chunk = |
| 2705 | prandom_u32() % sbi->max_fragment_chunk + 1; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2706 | } |
| 2707 | |
Chao Yu | 453e2ff | 2021-04-13 17:56:18 +0800 | [diff] [blame] | 2708 | static int __next_free_blkoff(struct f2fs_sb_info *sbi, |
| 2709 | int segno, block_t start) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2710 | { |
Chao Yu | 453e2ff | 2021-04-13 17:56:18 +0800 | [diff] [blame] | 2711 | struct seg_entry *se = get_seg_entry(sbi, segno); |
Changman Lee | e81c93c | 2013-11-15 13:21:16 +0900 | [diff] [blame] | 2712 | int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long); |
Jaegeuk Kim | 60a3b78 | 2015-02-10 16:44:29 -0800 | [diff] [blame] | 2713 | unsigned long *target_map = SIT_I(sbi)->tmp_map; |
Changman Lee | e81c93c | 2013-11-15 13:21:16 +0900 | [diff] [blame] | 2714 | unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map; |
| 2715 | unsigned long *cur_map = (unsigned long *)se->cur_valid_map; |
Chao Yu | 453e2ff | 2021-04-13 17:56:18 +0800 | [diff] [blame] | 2716 | int i; |
Changman Lee | e81c93c | 2013-11-15 13:21:16 +0900 | [diff] [blame] | 2717 | |
| 2718 | for (i = 0; i < entries; i++) |
| 2719 | target_map[i] = ckpt_map[i] | cur_map[i]; |
| 2720 | |
Chao Yu | 453e2ff | 2021-04-13 17:56:18 +0800 | [diff] [blame] | 2721 | return __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2722 | } |
| 2723 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 2724 | /* |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2725 | * If a segment is written by LFS manner, next block offset is just obtained |
| 2726 | * by increasing the current block offset. However, if a segment is written by |
| 2727 | * SSR manner, next block offset obtained by calling __next_free_blkoff |
| 2728 | */ |
| 2729 | static void __refresh_next_blkoff(struct f2fs_sb_info *sbi, |
| 2730 | struct curseg_info *seg) |
| 2731 | { |
Daeho Jeong | 6691d94 | 2021-09-29 11:12:03 -0700 | [diff] [blame] | 2732 | if (seg->alloc_type == SSR) { |
Chao Yu | 453e2ff | 2021-04-13 17:56:18 +0800 | [diff] [blame] | 2733 | seg->next_blkoff = |
| 2734 | __next_free_blkoff(sbi, seg->segno, |
| 2735 | seg->next_blkoff + 1); |
Daeho Jeong | 6691d94 | 2021-09-29 11:12:03 -0700 | [diff] [blame] | 2736 | } else { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2737 | seg->next_blkoff++; |
Daeho Jeong | 6691d94 | 2021-09-29 11:12:03 -0700 | [diff] [blame] | 2738 | if (F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_BLK) { |
| 2739 | /* To allocate block chunks in different sizes, use random number */ |
| 2740 | if (--seg->fragment_remained_chunk <= 0) { |
| 2741 | seg->fragment_remained_chunk = |
| 2742 | prandom_u32() % sbi->max_fragment_chunk + 1; |
| 2743 | seg->next_blkoff += |
| 2744 | prandom_u32() % sbi->max_fragment_hole + 1; |
| 2745 | } |
| 2746 | } |
| 2747 | } |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2748 | } |
| 2749 | |
Chao Yu | 61461fc | 2021-03-24 11:18:28 +0800 | [diff] [blame] | 2750 | bool f2fs_segment_has_free_slot(struct f2fs_sb_info *sbi, int segno) |
| 2751 | { |
Chao Yu | 453e2ff | 2021-04-13 17:56:18 +0800 | [diff] [blame] | 2752 | return __next_free_blkoff(sbi, segno, 0) < sbi->blocks_per_seg; |
Chao Yu | 61461fc | 2021-03-24 11:18:28 +0800 | [diff] [blame] | 2753 | } |
| 2754 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 2755 | /* |
arter97 | e1c4204 | 2014-08-06 23:22:50 +0900 | [diff] [blame] | 2756 | * This function always allocates a used segment(from dirty seglist) by SSR |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2757 | * manner, so it should recover the existing segment information of valid blocks |
| 2758 | */ |
Chao Yu | 093749e | 2020-08-04 21:14:49 +0800 | [diff] [blame] | 2759 | static void change_curseg(struct f2fs_sb_info *sbi, int type, bool flush) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2760 | { |
| 2761 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
| 2762 | struct curseg_info *curseg = CURSEG_I(sbi, type); |
| 2763 | unsigned int new_segno = curseg->next_segno; |
| 2764 | struct f2fs_summary_block *sum_node; |
| 2765 | struct page *sum_page; |
| 2766 | |
Chao Yu | 093749e | 2020-08-04 21:14:49 +0800 | [diff] [blame] | 2767 | if (flush) |
| 2768 | write_sum_page(sbi, curseg->sum_blk, |
| 2769 | GET_SUM_BLOCK(sbi, curseg->segno)); |
| 2770 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2771 | __set_test_and_inuse(sbi, new_segno); |
| 2772 | |
| 2773 | mutex_lock(&dirty_i->seglist_lock); |
| 2774 | __remove_dirty_segment(sbi, new_segno, PRE); |
| 2775 | __remove_dirty_segment(sbi, new_segno, DIRTY); |
| 2776 | mutex_unlock(&dirty_i->seglist_lock); |
| 2777 | |
| 2778 | reset_curseg(sbi, type, 1); |
| 2779 | curseg->alloc_type = SSR; |
Chao Yu | 453e2ff | 2021-04-13 17:56:18 +0800 | [diff] [blame] | 2780 | curseg->next_blkoff = __next_free_blkoff(sbi, curseg->segno, 0); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2781 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2782 | sum_page = f2fs_get_sum_page(sbi, new_segno); |
Jaegeuk Kim | 86f3360 | 2020-10-02 14:17:35 -0700 | [diff] [blame] | 2783 | if (IS_ERR(sum_page)) { |
| 2784 | /* GC won't be able to use stale summary pages by cp_error */ |
| 2785 | memset(curseg->sum_blk, 0, SUM_ENTRY_SIZE); |
| 2786 | return; |
| 2787 | } |
Chao Yu | 025d63a | 2017-08-30 18:04:48 +0800 | [diff] [blame] | 2788 | sum_node = (struct f2fs_summary_block *)page_address(sum_page); |
| 2789 | memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE); |
| 2790 | f2fs_put_page(sum_page, 1); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2791 | } |
| 2792 | |
Chao Yu | 093749e | 2020-08-04 21:14:49 +0800 | [diff] [blame] | 2793 | static int get_ssr_segment(struct f2fs_sb_info *sbi, int type, |
| 2794 | int alloc_mode, unsigned long long age); |
| 2795 | |
| 2796 | static void get_atssr_segment(struct f2fs_sb_info *sbi, int type, |
| 2797 | int target_type, int alloc_mode, |
| 2798 | unsigned long long age) |
| 2799 | { |
| 2800 | struct curseg_info *curseg = CURSEG_I(sbi, type); |
| 2801 | |
| 2802 | curseg->seg_type = target_type; |
| 2803 | |
| 2804 | if (get_ssr_segment(sbi, type, alloc_mode, age)) { |
| 2805 | struct seg_entry *se = get_seg_entry(sbi, curseg->next_segno); |
| 2806 | |
| 2807 | curseg->seg_type = se->type; |
| 2808 | change_curseg(sbi, type, true); |
| 2809 | } else { |
| 2810 | /* allocate cold segment by default */ |
| 2811 | curseg->seg_type = CURSEG_COLD_DATA; |
| 2812 | new_curseg(sbi, type, true); |
| 2813 | } |
| 2814 | stat_inc_seg_type(sbi, curseg); |
| 2815 | } |
| 2816 | |
| 2817 | static void __f2fs_init_atgc_curseg(struct f2fs_sb_info *sbi) |
| 2818 | { |
| 2819 | struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_ALL_DATA_ATGC); |
| 2820 | |
| 2821 | if (!sbi->am.atgc_enabled) |
| 2822 | return; |
| 2823 | |
| 2824 | down_read(&SM_I(sbi)->curseg_lock); |
| 2825 | |
| 2826 | mutex_lock(&curseg->curseg_mutex); |
| 2827 | down_write(&SIT_I(sbi)->sentry_lock); |
| 2828 | |
| 2829 | get_atssr_segment(sbi, CURSEG_ALL_DATA_ATGC, CURSEG_COLD_DATA, SSR, 0); |
| 2830 | |
| 2831 | up_write(&SIT_I(sbi)->sentry_lock); |
| 2832 | mutex_unlock(&curseg->curseg_mutex); |
| 2833 | |
| 2834 | up_read(&SM_I(sbi)->curseg_lock); |
| 2835 | |
| 2836 | } |
| 2837 | void f2fs_init_inmem_curseg(struct f2fs_sb_info *sbi) |
| 2838 | { |
| 2839 | __f2fs_init_atgc_curseg(sbi); |
| 2840 | } |
| 2841 | |
| 2842 | static void __f2fs_save_inmem_curseg(struct f2fs_sb_info *sbi, int type) |
Chao Yu | d0b9e42 | 2020-08-04 21:14:45 +0800 | [diff] [blame] | 2843 | { |
| 2844 | struct curseg_info *curseg = CURSEG_I(sbi, type); |
| 2845 | |
| 2846 | mutex_lock(&curseg->curseg_mutex); |
| 2847 | if (!curseg->inited) |
| 2848 | goto out; |
| 2849 | |
| 2850 | if (get_valid_blocks(sbi, curseg->segno, false)) { |
| 2851 | write_sum_page(sbi, curseg->sum_blk, |
| 2852 | GET_SUM_BLOCK(sbi, curseg->segno)); |
| 2853 | } else { |
| 2854 | mutex_lock(&DIRTY_I(sbi)->seglist_lock); |
| 2855 | __set_test_and_free(sbi, curseg->segno, true); |
| 2856 | mutex_unlock(&DIRTY_I(sbi)->seglist_lock); |
| 2857 | } |
| 2858 | out: |
| 2859 | mutex_unlock(&curseg->curseg_mutex); |
| 2860 | } |
| 2861 | |
Chao Yu | 093749e | 2020-08-04 21:14:49 +0800 | [diff] [blame] | 2862 | void f2fs_save_inmem_curseg(struct f2fs_sb_info *sbi) |
| 2863 | { |
| 2864 | __f2fs_save_inmem_curseg(sbi, CURSEG_COLD_DATA_PINNED); |
| 2865 | |
| 2866 | if (sbi->am.atgc_enabled) |
| 2867 | __f2fs_save_inmem_curseg(sbi, CURSEG_ALL_DATA_ATGC); |
| 2868 | } |
| 2869 | |
| 2870 | static void __f2fs_restore_inmem_curseg(struct f2fs_sb_info *sbi, int type) |
Chao Yu | d0b9e42 | 2020-08-04 21:14:45 +0800 | [diff] [blame] | 2871 | { |
| 2872 | struct curseg_info *curseg = CURSEG_I(sbi, type); |
| 2873 | |
| 2874 | mutex_lock(&curseg->curseg_mutex); |
| 2875 | if (!curseg->inited) |
| 2876 | goto out; |
| 2877 | if (get_valid_blocks(sbi, curseg->segno, false)) |
| 2878 | goto out; |
| 2879 | |
| 2880 | mutex_lock(&DIRTY_I(sbi)->seglist_lock); |
| 2881 | __set_test_and_inuse(sbi, curseg->segno); |
| 2882 | mutex_unlock(&DIRTY_I(sbi)->seglist_lock); |
| 2883 | out: |
| 2884 | mutex_unlock(&curseg->curseg_mutex); |
| 2885 | } |
| 2886 | |
Chao Yu | 093749e | 2020-08-04 21:14:49 +0800 | [diff] [blame] | 2887 | void f2fs_restore_inmem_curseg(struct f2fs_sb_info *sbi) |
| 2888 | { |
| 2889 | __f2fs_restore_inmem_curseg(sbi, CURSEG_COLD_DATA_PINNED); |
| 2890 | |
| 2891 | if (sbi->am.atgc_enabled) |
| 2892 | __f2fs_restore_inmem_curseg(sbi, CURSEG_ALL_DATA_ATGC); |
| 2893 | } |
| 2894 | |
| 2895 | static int get_ssr_segment(struct f2fs_sb_info *sbi, int type, |
| 2896 | int alloc_mode, unsigned long long age) |
Jaegeuk Kim | 4372752 | 2013-02-04 15:11:17 +0900 | [diff] [blame] | 2897 | { |
| 2898 | struct curseg_info *curseg = CURSEG_I(sbi, type); |
| 2899 | const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops; |
Jaegeuk Kim | e066b83 | 2017-04-13 15:17:00 -0700 | [diff] [blame] | 2900 | unsigned segno = NULL_SEGNO; |
Chao Yu | 093749e | 2020-08-04 21:14:49 +0800 | [diff] [blame] | 2901 | unsigned short seg_type = curseg->seg_type; |
Chao Yu | d27c3d8 | 2017-02-24 18:46:00 +0800 | [diff] [blame] | 2902 | int i, cnt; |
| 2903 | bool reversed = false; |
Jaegeuk Kim | c192f7a | 2017-02-22 17:10:18 -0800 | [diff] [blame] | 2904 | |
Chao Yu | 093749e | 2020-08-04 21:14:49 +0800 | [diff] [blame] | 2905 | sanity_check_seg_type(sbi, seg_type); |
| 2906 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2907 | /* f2fs_need_SSR() already forces to do this */ |
Chao Yu | 093749e | 2020-08-04 21:14:49 +0800 | [diff] [blame] | 2908 | if (!v_ops->get_victim(sbi, &segno, BG_GC, seg_type, alloc_mode, age)) { |
Jaegeuk Kim | e066b83 | 2017-04-13 15:17:00 -0700 | [diff] [blame] | 2909 | curseg->next_segno = segno; |
Jaegeuk Kim | c192f7a | 2017-02-22 17:10:18 -0800 | [diff] [blame] | 2910 | return 1; |
Jaegeuk Kim | e066b83 | 2017-04-13 15:17:00 -0700 | [diff] [blame] | 2911 | } |
Jaegeuk Kim | 4372752 | 2013-02-04 15:11:17 +0900 | [diff] [blame] | 2912 | |
Jaegeuk Kim | 70d625c | 2017-02-22 17:02:32 -0800 | [diff] [blame] | 2913 | /* For node segments, let's do SSR more intensively */ |
Chao Yu | 093749e | 2020-08-04 21:14:49 +0800 | [diff] [blame] | 2914 | if (IS_NODESEG(seg_type)) { |
| 2915 | if (seg_type >= CURSEG_WARM_NODE) { |
Chao Yu | d27c3d8 | 2017-02-24 18:46:00 +0800 | [diff] [blame] | 2916 | reversed = true; |
| 2917 | i = CURSEG_COLD_NODE; |
| 2918 | } else { |
| 2919 | i = CURSEG_HOT_NODE; |
| 2920 | } |
| 2921 | cnt = NR_CURSEG_NODE_TYPE; |
Jaegeuk Kim | 70d625c | 2017-02-22 17:02:32 -0800 | [diff] [blame] | 2922 | } else { |
Chao Yu | 093749e | 2020-08-04 21:14:49 +0800 | [diff] [blame] | 2923 | if (seg_type >= CURSEG_WARM_DATA) { |
Chao Yu | d27c3d8 | 2017-02-24 18:46:00 +0800 | [diff] [blame] | 2924 | reversed = true; |
| 2925 | i = CURSEG_COLD_DATA; |
| 2926 | } else { |
| 2927 | i = CURSEG_HOT_DATA; |
| 2928 | } |
| 2929 | cnt = NR_CURSEG_DATA_TYPE; |
Jaegeuk Kim | 70d625c | 2017-02-22 17:02:32 -0800 | [diff] [blame] | 2930 | } |
Jaegeuk Kim | 4372752 | 2013-02-04 15:11:17 +0900 | [diff] [blame] | 2931 | |
Chao Yu | d27c3d8 | 2017-02-24 18:46:00 +0800 | [diff] [blame] | 2932 | for (; cnt-- > 0; reversed ? i-- : i++) { |
Chao Yu | 093749e | 2020-08-04 21:14:49 +0800 | [diff] [blame] | 2933 | if (i == seg_type) |
Jaegeuk Kim | c192f7a | 2017-02-22 17:10:18 -0800 | [diff] [blame] | 2934 | continue; |
Chao Yu | 093749e | 2020-08-04 21:14:49 +0800 | [diff] [blame] | 2935 | if (!v_ops->get_victim(sbi, &segno, BG_GC, i, alloc_mode, age)) { |
Jaegeuk Kim | e066b83 | 2017-04-13 15:17:00 -0700 | [diff] [blame] | 2936 | curseg->next_segno = segno; |
Jaegeuk Kim | 4372752 | 2013-02-04 15:11:17 +0900 | [diff] [blame] | 2937 | return 1; |
Jaegeuk Kim | e066b83 | 2017-04-13 15:17:00 -0700 | [diff] [blame] | 2938 | } |
Jaegeuk Kim | c192f7a | 2017-02-22 17:10:18 -0800 | [diff] [blame] | 2939 | } |
Daniel Rosenberg | 4354994 | 2018-08-20 19:21:43 -0700 | [diff] [blame] | 2940 | |
| 2941 | /* find valid_blocks=0 in dirty list */ |
| 2942 | if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) { |
| 2943 | segno = get_free_segment(sbi); |
| 2944 | if (segno != NULL_SEGNO) { |
| 2945 | curseg->next_segno = segno; |
| 2946 | return 1; |
| 2947 | } |
| 2948 | } |
Jaegeuk Kim | 4372752 | 2013-02-04 15:11:17 +0900 | [diff] [blame] | 2949 | return 0; |
| 2950 | } |
| 2951 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2952 | /* |
| 2953 | * flush out current segment and replace it with new segment |
| 2954 | * This function should be returned with success, otherwise BUG |
| 2955 | */ |
| 2956 | static void allocate_segment_by_default(struct f2fs_sb_info *sbi, |
| 2957 | int type, bool force) |
| 2958 | { |
Jaegeuk Kim | a788189 | 2017-04-20 13:51:57 -0700 | [diff] [blame] | 2959 | struct curseg_info *curseg = CURSEG_I(sbi, type); |
| 2960 | |
Gu Zheng | 7b40527 | 2013-08-19 09:41:15 +0800 | [diff] [blame] | 2961 | if (force) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2962 | new_curseg(sbi, type, true); |
Jaegeuk Kim | 5b6c6be | 2017-02-14 19:32:51 -0800 | [diff] [blame] | 2963 | else if (!is_set_ckpt_flags(sbi, CP_CRC_RECOVERY_FLAG) && |
Chao Yu | 093749e | 2020-08-04 21:14:49 +0800 | [diff] [blame] | 2964 | curseg->seg_type == CURSEG_WARM_NODE) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2965 | new_curseg(sbi, type, false); |
Chao Yu | 093749e | 2020-08-04 21:14:49 +0800 | [diff] [blame] | 2966 | else if (curseg->alloc_type == LFS && |
| 2967 | is_next_segment_free(sbi, curseg, type) && |
Daniel Rosenberg | 4354994 | 2018-08-20 19:21:43 -0700 | [diff] [blame] | 2968 | likely(!is_sbi_flag_set(sbi, SBI_CP_DISABLED))) |
Jaegeuk Kim | a788189 | 2017-04-20 13:51:57 -0700 | [diff] [blame] | 2969 | new_curseg(sbi, type, false); |
Chao Yu | 093749e | 2020-08-04 21:14:49 +0800 | [diff] [blame] | 2970 | else if (f2fs_need_SSR(sbi) && |
| 2971 | get_ssr_segment(sbi, type, SSR, 0)) |
| 2972 | change_curseg(sbi, type, true); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2973 | else |
| 2974 | new_curseg(sbi, type, false); |
Jaegeuk Kim | dcdfff6 | 2013-10-22 20:56:10 +0900 | [diff] [blame] | 2975 | |
Jaegeuk Kim | a788189 | 2017-04-20 13:51:57 -0700 | [diff] [blame] | 2976 | stat_inc_seg_type(sbi, curseg); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2977 | } |
| 2978 | |
Chao Yu | 0ef8183 | 2020-06-18 14:36:22 +0800 | [diff] [blame] | 2979 | void f2fs_allocate_segment_for_resize(struct f2fs_sb_info *sbi, int type, |
Qiuyang Sun | 04f0b2e | 2019-06-05 11:33:25 +0800 | [diff] [blame] | 2980 | unsigned int start, unsigned int end) |
| 2981 | { |
| 2982 | struct curseg_info *curseg = CURSEG_I(sbi, type); |
| 2983 | unsigned int segno; |
| 2984 | |
| 2985 | down_read(&SM_I(sbi)->curseg_lock); |
| 2986 | mutex_lock(&curseg->curseg_mutex); |
| 2987 | down_write(&SIT_I(sbi)->sentry_lock); |
| 2988 | |
| 2989 | segno = CURSEG_I(sbi, type)->segno; |
| 2990 | if (segno < start || segno > end) |
| 2991 | goto unlock; |
| 2992 | |
Chao Yu | 093749e | 2020-08-04 21:14:49 +0800 | [diff] [blame] | 2993 | if (f2fs_need_SSR(sbi) && get_ssr_segment(sbi, type, SSR, 0)) |
| 2994 | change_curseg(sbi, type, true); |
Qiuyang Sun | 04f0b2e | 2019-06-05 11:33:25 +0800 | [diff] [blame] | 2995 | else |
| 2996 | new_curseg(sbi, type, true); |
| 2997 | |
| 2998 | stat_inc_seg_type(sbi, curseg); |
| 2999 | |
| 3000 | locate_dirty_segment(sbi, segno); |
| 3001 | unlock: |
| 3002 | up_write(&SIT_I(sbi)->sentry_lock); |
| 3003 | |
| 3004 | if (segno != curseg->segno) |
Joe Perches | dcbb4c1 | 2019-06-18 17:48:42 +0800 | [diff] [blame] | 3005 | f2fs_notice(sbi, "For resize: curseg of type %d: %u ==> %u", |
| 3006 | type, segno, curseg->segno); |
Qiuyang Sun | 04f0b2e | 2019-06-05 11:33:25 +0800 | [diff] [blame] | 3007 | |
| 3008 | mutex_unlock(&curseg->curseg_mutex); |
| 3009 | up_read(&SM_I(sbi)->curseg_lock); |
| 3010 | } |
| 3011 | |
Chao Yu | e1175f0 | 2021-03-05 17:56:01 +0800 | [diff] [blame] | 3012 | static void __allocate_new_segment(struct f2fs_sb_info *sbi, int type, |
Chao Yu | 509f101 | 2021-04-21 09:54:55 +0800 | [diff] [blame] | 3013 | bool new_sec, bool force) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3014 | { |
Chao Yu | 901d745f | 2020-06-22 17:38:48 +0800 | [diff] [blame] | 3015 | struct curseg_info *curseg = CURSEG_I(sbi, type); |
Jaegeuk Kim | 6ae1be1 | 2016-11-11 12:31:40 -0800 | [diff] [blame] | 3016 | unsigned int old_segno; |
Chao Yu | 901d745f | 2020-06-22 17:38:48 +0800 | [diff] [blame] | 3017 | |
Chao Yu | d0b9e42 | 2020-08-04 21:14:45 +0800 | [diff] [blame] | 3018 | if (!curseg->inited) |
| 3019 | goto alloc; |
| 3020 | |
Chao Yu | 509f101 | 2021-04-21 09:54:55 +0800 | [diff] [blame] | 3021 | if (force || curseg->next_blkoff || |
Chao Yu | e1175f0 | 2021-03-05 17:56:01 +0800 | [diff] [blame] | 3022 | get_valid_blocks(sbi, curseg->segno, new_sec)) |
| 3023 | goto alloc; |
Chao Yu | 901d745f | 2020-06-22 17:38:48 +0800 | [diff] [blame] | 3024 | |
Chao Yu | 61461fc | 2021-03-24 11:18:28 +0800 | [diff] [blame] | 3025 | if (!get_ckpt_valid_blocks(sbi, curseg->segno, new_sec)) |
| 3026 | return; |
Chao Yu | d0b9e42 | 2020-08-04 21:14:45 +0800 | [diff] [blame] | 3027 | alloc: |
Chao Yu | 901d745f | 2020-06-22 17:38:48 +0800 | [diff] [blame] | 3028 | old_segno = curseg->segno; |
| 3029 | SIT_I(sbi)->s_ops->allocate_segment(sbi, type, true); |
| 3030 | locate_dirty_segment(sbi, old_segno); |
| 3031 | } |
| 3032 | |
Chao Yu | 509f101 | 2021-04-21 09:54:55 +0800 | [diff] [blame] | 3033 | static void __allocate_new_section(struct f2fs_sb_info *sbi, |
| 3034 | int type, bool force) |
Chao Yu | 901d745f | 2020-06-22 17:38:48 +0800 | [diff] [blame] | 3035 | { |
Chao Yu | 509f101 | 2021-04-21 09:54:55 +0800 | [diff] [blame] | 3036 | __allocate_new_segment(sbi, type, true, force); |
Chao Yu | e1175f0 | 2021-03-05 17:56:01 +0800 | [diff] [blame] | 3037 | } |
| 3038 | |
Chao Yu | 509f101 | 2021-04-21 09:54:55 +0800 | [diff] [blame] | 3039 | void f2fs_allocate_new_section(struct f2fs_sb_info *sbi, int type, bool force) |
Chao Yu | 901d745f | 2020-06-22 17:38:48 +0800 | [diff] [blame] | 3040 | { |
Chao Yu | 823d13e | 2021-03-26 09:46:22 +0800 | [diff] [blame] | 3041 | down_read(&SM_I(sbi)->curseg_lock); |
Chao Yu | 901d745f | 2020-06-22 17:38:48 +0800 | [diff] [blame] | 3042 | down_write(&SIT_I(sbi)->sentry_lock); |
Chao Yu | 509f101 | 2021-04-21 09:54:55 +0800 | [diff] [blame] | 3043 | __allocate_new_section(sbi, type, force); |
Chao Yu | 901d745f | 2020-06-22 17:38:48 +0800 | [diff] [blame] | 3044 | up_write(&SIT_I(sbi)->sentry_lock); |
Chao Yu | 823d13e | 2021-03-26 09:46:22 +0800 | [diff] [blame] | 3045 | up_read(&SM_I(sbi)->curseg_lock); |
Chao Yu | 901d745f | 2020-06-22 17:38:48 +0800 | [diff] [blame] | 3046 | } |
| 3047 | |
| 3048 | void f2fs_allocate_new_segments(struct f2fs_sb_info *sbi) |
| 3049 | { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3050 | int i; |
| 3051 | |
Chao Yu | 823d13e | 2021-03-26 09:46:22 +0800 | [diff] [blame] | 3052 | down_read(&SM_I(sbi)->curseg_lock); |
Chao Yu | 3d26fa6 | 2017-10-30 17:49:53 +0800 | [diff] [blame] | 3053 | down_write(&SIT_I(sbi)->sentry_lock); |
Chao Yu | 901d745f | 2020-06-22 17:38:48 +0800 | [diff] [blame] | 3054 | for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) |
Chao Yu | 509f101 | 2021-04-21 09:54:55 +0800 | [diff] [blame] | 3055 | __allocate_new_segment(sbi, i, false, false); |
Chao Yu | 3d26fa6 | 2017-10-30 17:49:53 +0800 | [diff] [blame] | 3056 | up_write(&SIT_I(sbi)->sentry_lock); |
Chao Yu | 823d13e | 2021-03-26 09:46:22 +0800 | [diff] [blame] | 3057 | up_read(&SM_I(sbi)->curseg_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3058 | } |
| 3059 | |
| 3060 | static const struct segment_allocation default_salloc_ops = { |
| 3061 | .allocate_segment = allocate_segment_by_default, |
| 3062 | }; |
| 3063 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 3064 | bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi, |
| 3065 | struct cp_control *cpc) |
Jaegeuk Kim | 25290fa | 2016-12-29 22:06:15 -0800 | [diff] [blame] | 3066 | { |
| 3067 | __u64 trim_start = cpc->trim_start; |
| 3068 | bool has_candidate = false; |
| 3069 | |
Chao Yu | 3d26fa6 | 2017-10-30 17:49:53 +0800 | [diff] [blame] | 3070 | down_write(&SIT_I(sbi)->sentry_lock); |
Jaegeuk Kim | 25290fa | 2016-12-29 22:06:15 -0800 | [diff] [blame] | 3071 | for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++) { |
| 3072 | if (add_discard_addrs(sbi, cpc, true)) { |
| 3073 | has_candidate = true; |
| 3074 | break; |
| 3075 | } |
| 3076 | } |
Chao Yu | 3d26fa6 | 2017-10-30 17:49:53 +0800 | [diff] [blame] | 3077 | up_write(&SIT_I(sbi)->sentry_lock); |
Jaegeuk Kim | 25290fa | 2016-12-29 22:06:15 -0800 | [diff] [blame] | 3078 | |
| 3079 | cpc->trim_start = trim_start; |
| 3080 | return has_candidate; |
| 3081 | } |
| 3082 | |
Chao Yu | 01f9cf6 | 2018-06-25 20:33:24 +0800 | [diff] [blame] | 3083 | static unsigned int __issue_discard_cmd_range(struct f2fs_sb_info *sbi, |
Jaegeuk Kim | 9a99718 | 2018-05-24 13:57:26 -0700 | [diff] [blame] | 3084 | struct discard_policy *dpolicy, |
| 3085 | unsigned int start, unsigned int end) |
| 3086 | { |
| 3087 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
| 3088 | struct discard_cmd *prev_dc = NULL, *next_dc = NULL; |
| 3089 | struct rb_node **insert_p = NULL, *insert_parent = NULL; |
| 3090 | struct discard_cmd *dc; |
| 3091 | struct blk_plug plug; |
| 3092 | int issued; |
Chao Yu | 01f9cf6 | 2018-06-25 20:33:24 +0800 | [diff] [blame] | 3093 | unsigned int trimmed = 0; |
Jaegeuk Kim | 9a99718 | 2018-05-24 13:57:26 -0700 | [diff] [blame] | 3094 | |
| 3095 | next: |
| 3096 | issued = 0; |
| 3097 | |
| 3098 | mutex_lock(&dcc->cmd_lock); |
Chao Yu | 67fce70 | 2018-06-22 16:06:59 +0800 | [diff] [blame] | 3099 | if (unlikely(dcc->rbtree_check)) |
| 3100 | f2fs_bug_on(sbi, !f2fs_check_rb_tree_consistence(sbi, |
Chao Yu | 2e9b2bb | 2020-08-04 21:14:48 +0800 | [diff] [blame] | 3101 | &dcc->root, false)); |
Jaegeuk Kim | 9a99718 | 2018-05-24 13:57:26 -0700 | [diff] [blame] | 3102 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 3103 | dc = (struct discard_cmd *)f2fs_lookup_rb_tree_ret(&dcc->root, |
Jaegeuk Kim | 9a99718 | 2018-05-24 13:57:26 -0700 | [diff] [blame] | 3104 | NULL, start, |
| 3105 | (struct rb_entry **)&prev_dc, |
| 3106 | (struct rb_entry **)&next_dc, |
Chao Yu | 4dada3f | 2018-10-04 11:18:30 +0800 | [diff] [blame] | 3107 | &insert_p, &insert_parent, true, NULL); |
Jaegeuk Kim | 9a99718 | 2018-05-24 13:57:26 -0700 | [diff] [blame] | 3108 | if (!dc) |
| 3109 | dc = next_dc; |
| 3110 | |
| 3111 | blk_start_plug(&plug); |
| 3112 | |
| 3113 | while (dc && dc->lstart <= end) { |
| 3114 | struct rb_node *node; |
Chao Yu | 6b9cb12 | 2018-08-08 10:14:55 +0800 | [diff] [blame] | 3115 | int err = 0; |
Jaegeuk Kim | 9a99718 | 2018-05-24 13:57:26 -0700 | [diff] [blame] | 3116 | |
| 3117 | if (dc->len < dpolicy->granularity) |
| 3118 | goto skip; |
| 3119 | |
| 3120 | if (dc->state != D_PREP) { |
| 3121 | list_move_tail(&dc->list, &dcc->fstrim_list); |
| 3122 | goto skip; |
| 3123 | } |
| 3124 | |
Chao Yu | 6b9cb12 | 2018-08-08 10:14:55 +0800 | [diff] [blame] | 3125 | err = __submit_discard_cmd(sbi, dpolicy, dc, &issued); |
Jaegeuk Kim | 9a99718 | 2018-05-24 13:57:26 -0700 | [diff] [blame] | 3126 | |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 3127 | if (issued >= dpolicy->max_requests) { |
Jaegeuk Kim | 9a99718 | 2018-05-24 13:57:26 -0700 | [diff] [blame] | 3128 | start = dc->lstart + dc->len; |
| 3129 | |
Chao Yu | 6b9cb12 | 2018-08-08 10:14:55 +0800 | [diff] [blame] | 3130 | if (err) |
| 3131 | __remove_discard_cmd(sbi, dc); |
| 3132 | |
Jaegeuk Kim | 9a99718 | 2018-05-24 13:57:26 -0700 | [diff] [blame] | 3133 | blk_finish_plug(&plug); |
| 3134 | mutex_unlock(&dcc->cmd_lock); |
Chao Yu | 01f9cf6 | 2018-06-25 20:33:24 +0800 | [diff] [blame] | 3135 | trimmed += __wait_all_discard_cmd(sbi, NULL); |
Chao Yu | 5df7731f | 2020-02-17 17:45:44 +0800 | [diff] [blame] | 3136 | congestion_wait(BLK_RW_ASYNC, DEFAULT_IO_TIMEOUT); |
Jaegeuk Kim | 9a99718 | 2018-05-24 13:57:26 -0700 | [diff] [blame] | 3137 | goto next; |
| 3138 | } |
| 3139 | skip: |
| 3140 | node = rb_next(&dc->rb_node); |
Chao Yu | 6b9cb12 | 2018-08-08 10:14:55 +0800 | [diff] [blame] | 3141 | if (err) |
| 3142 | __remove_discard_cmd(sbi, dc); |
Jaegeuk Kim | 9a99718 | 2018-05-24 13:57:26 -0700 | [diff] [blame] | 3143 | dc = rb_entry_safe(node, struct discard_cmd, rb_node); |
| 3144 | |
| 3145 | if (fatal_signal_pending(current)) |
| 3146 | break; |
| 3147 | } |
| 3148 | |
| 3149 | blk_finish_plug(&plug); |
| 3150 | mutex_unlock(&dcc->cmd_lock); |
Chao Yu | 01f9cf6 | 2018-06-25 20:33:24 +0800 | [diff] [blame] | 3151 | |
| 3152 | return trimmed; |
Jaegeuk Kim | 9a99718 | 2018-05-24 13:57:26 -0700 | [diff] [blame] | 3153 | } |
| 3154 | |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 3155 | int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range) |
| 3156 | { |
Jaegeuk Kim | f7ef9b8 | 2015-02-09 12:02:44 -0800 | [diff] [blame] | 3157 | __u64 start = F2FS_BYTES_TO_BLK(range->start); |
| 3158 | __u64 end = start + F2FS_BYTES_TO_BLK(range->len) - 1; |
Chao Yu | 377224c | 2018-04-09 10:25:23 +0800 | [diff] [blame] | 3159 | unsigned int start_segno, end_segno; |
Chao Yu | 8412663 | 2017-10-04 09:08:32 +0800 | [diff] [blame] | 3160 | block_t start_block, end_block; |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 3161 | struct cp_control cpc; |
Chao Yu | 78997b5 | 2017-10-04 09:08:34 +0800 | [diff] [blame] | 3162 | struct discard_policy dpolicy; |
Chao Yu | 0ea8051 | 2017-10-28 16:52:32 +0800 | [diff] [blame] | 3163 | unsigned long long trimmed = 0; |
Chao Yu | c34f42e | 2015-12-23 17:50:30 +0800 | [diff] [blame] | 3164 | int err = 0; |
Chao Yu | b0332a0 | 2020-02-14 17:44:12 +0800 | [diff] [blame] | 3165 | bool need_align = f2fs_lfs_mode(sbi) && __is_large_section(sbi); |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 3166 | |
Jaegeuk Kim | 836b5a6 | 2015-04-30 22:50:06 -0700 | [diff] [blame] | 3167 | if (start >= MAX_BLKADDR(sbi) || range->len < sbi->blocksize) |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 3168 | return -EINVAL; |
| 3169 | |
Chao Yu | 3f16ecd9 | 2018-08-08 17:36:29 +0800 | [diff] [blame] | 3170 | if (end < MAIN_BLKADDR(sbi)) |
| 3171 | goto out; |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 3172 | |
Yunlei He | ed214a1 | 2016-09-01 10:14:39 +0800 | [diff] [blame] | 3173 | if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) { |
Joe Perches | dcbb4c1 | 2019-06-18 17:48:42 +0800 | [diff] [blame] | 3174 | f2fs_warn(sbi, "Found FS corruption, run fsck to fix."); |
Chao Yu | 10f966b | 2019-06-20 11:36:14 +0800 | [diff] [blame] | 3175 | return -EFSCORRUPTED; |
Yunlei He | ed214a1 | 2016-09-01 10:14:39 +0800 | [diff] [blame] | 3176 | } |
| 3177 | |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 3178 | /* start/end segment number in main_area */ |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 3179 | start_segno = (start <= MAIN_BLKADDR(sbi)) ? 0 : GET_SEGNO(sbi, start); |
| 3180 | end_segno = (end >= MAX_BLKADDR(sbi)) ? MAIN_SEGS(sbi) - 1 : |
| 3181 | GET_SEGNO(sbi, end); |
Yunlong Song | ad6672b | 2018-07-19 20:58:15 +0800 | [diff] [blame] | 3182 | if (need_align) { |
| 3183 | start_segno = rounddown(start_segno, sbi->segs_per_sec); |
| 3184 | end_segno = roundup(end_segno + 1, sbi->segs_per_sec) - 1; |
| 3185 | } |
Chao Yu | 8412663 | 2017-10-04 09:08:32 +0800 | [diff] [blame] | 3186 | |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 3187 | cpc.reason = CP_DISCARD; |
Jaegeuk Kim | 836b5a6 | 2015-04-30 22:50:06 -0700 | [diff] [blame] | 3188 | cpc.trim_minlen = max_t(__u64, 1, F2FS_BYTES_TO_BLK(range->minlen)); |
Chao Yu | 377224c | 2018-04-09 10:25:23 +0800 | [diff] [blame] | 3189 | cpc.trim_start = start_segno; |
| 3190 | cpc.trim_end = end_segno; |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 3191 | |
Chao Yu | 377224c | 2018-04-09 10:25:23 +0800 | [diff] [blame] | 3192 | if (sbi->discard_blks == 0) |
| 3193 | goto out; |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 3194 | |
Chao Yu | fb24fea | 2020-01-14 19:36:50 +0800 | [diff] [blame] | 3195 | down_write(&sbi->gc_lock); |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 3196 | err = f2fs_write_checkpoint(sbi, &cpc); |
Chao Yu | fb24fea | 2020-01-14 19:36:50 +0800 | [diff] [blame] | 3197 | up_write(&sbi->gc_lock); |
Chao Yu | 377224c | 2018-04-09 10:25:23 +0800 | [diff] [blame] | 3198 | if (err) |
| 3199 | goto out; |
Chao Yu | 8412663 | 2017-10-04 09:08:32 +0800 | [diff] [blame] | 3200 | |
Jaegeuk Kim | e555da9 | 2018-05-31 10:20:48 -0700 | [diff] [blame] | 3201 | /* |
| 3202 | * We filed discard candidates, but actually we don't need to wait for |
| 3203 | * all of them, since they'll be issued in idle time along with runtime |
| 3204 | * discard option. User configuration looks like using runtime discard |
| 3205 | * or periodic fstrim instead of it. |
| 3206 | */ |
Chao Yu | 7d20c8a | 2018-09-04 03:52:17 +0800 | [diff] [blame] | 3207 | if (f2fs_realtime_discard_enable(sbi)) |
Jaegeuk Kim | 5a61549 | 2018-06-20 21:27:21 -0700 | [diff] [blame] | 3208 | goto out; |
| 3209 | |
| 3210 | start_block = START_BLOCK(sbi, start_segno); |
| 3211 | end_block = START_BLOCK(sbi, end_segno + 1); |
| 3212 | |
| 3213 | __init_discard_policy(sbi, &dpolicy, DPOLICY_FSTRIM, cpc.trim_minlen); |
Chao Yu | 01f9cf6 | 2018-06-25 20:33:24 +0800 | [diff] [blame] | 3214 | trimmed = __issue_discard_cmd_range(sbi, &dpolicy, |
| 3215 | start_block, end_block); |
Jaegeuk Kim | 5a61549 | 2018-06-20 21:27:21 -0700 | [diff] [blame] | 3216 | |
Chao Yu | 01f9cf6 | 2018-06-25 20:33:24 +0800 | [diff] [blame] | 3217 | trimmed += __wait_discard_cmd_range(sbi, &dpolicy, |
Chao Yu | 0ea8051 | 2017-10-28 16:52:32 +0800 | [diff] [blame] | 3218 | start_block, end_block); |
Chao Yu | 377224c | 2018-04-09 10:25:23 +0800 | [diff] [blame] | 3219 | out: |
Chao Yu | 6eae269 | 2018-08-05 23:09:00 +0800 | [diff] [blame] | 3220 | if (!err) |
| 3221 | range->len = F2FS_BLK_TO_BYTES(trimmed); |
Chao Yu | c34f42e | 2015-12-23 17:50:30 +0800 | [diff] [blame] | 3222 | return err; |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 3223 | } |
| 3224 | |
Chao Yu | 093749e | 2020-08-04 21:14:49 +0800 | [diff] [blame] | 3225 | static bool __has_curseg_space(struct f2fs_sb_info *sbi, |
| 3226 | struct curseg_info *curseg) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3227 | { |
Aravind Ramesh | de881df | 2020-07-16 18:26:56 +0530 | [diff] [blame] | 3228 | return curseg->next_blkoff < f2fs_usable_blks_in_seg(sbi, |
| 3229 | curseg->segno); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3230 | } |
| 3231 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 3232 | int f2fs_rw_hint_to_seg_type(enum rw_hint hint) |
Hyunchul Lee | 4f0a03d3 | 2017-11-09 14:51:27 +0900 | [diff] [blame] | 3233 | { |
| 3234 | switch (hint) { |
| 3235 | case WRITE_LIFE_SHORT: |
| 3236 | return CURSEG_HOT_DATA; |
| 3237 | case WRITE_LIFE_EXTREME: |
| 3238 | return CURSEG_COLD_DATA; |
| 3239 | default: |
| 3240 | return CURSEG_WARM_DATA; |
| 3241 | } |
| 3242 | } |
| 3243 | |
Hyunchul Lee | 0cdd319 | 2018-01-31 11:36:57 +0900 | [diff] [blame] | 3244 | /* This returns write hints for each segment type. This hints will be |
| 3245 | * passed down to block layer. There are mapping tables which depend on |
| 3246 | * the mount option 'whint_mode'. |
| 3247 | * |
| 3248 | * 1) whint_mode=off. F2FS only passes down WRITE_LIFE_NOT_SET. |
| 3249 | * |
| 3250 | * 2) whint_mode=user-based. F2FS tries to pass down hints given by users. |
| 3251 | * |
| 3252 | * User F2FS Block |
| 3253 | * ---- ---- ----- |
| 3254 | * META WRITE_LIFE_NOT_SET |
| 3255 | * HOT_NODE " |
| 3256 | * WARM_NODE " |
| 3257 | * COLD_NODE " |
| 3258 | * ioctl(COLD) COLD_DATA WRITE_LIFE_EXTREME |
| 3259 | * extension list " " |
| 3260 | * |
| 3261 | * -- buffered io |
| 3262 | * WRITE_LIFE_EXTREME COLD_DATA WRITE_LIFE_EXTREME |
| 3263 | * WRITE_LIFE_SHORT HOT_DATA WRITE_LIFE_SHORT |
| 3264 | * WRITE_LIFE_NOT_SET WARM_DATA WRITE_LIFE_NOT_SET |
| 3265 | * WRITE_LIFE_NONE " " |
| 3266 | * WRITE_LIFE_MEDIUM " " |
| 3267 | * WRITE_LIFE_LONG " " |
| 3268 | * |
| 3269 | * -- direct io |
| 3270 | * WRITE_LIFE_EXTREME COLD_DATA WRITE_LIFE_EXTREME |
| 3271 | * WRITE_LIFE_SHORT HOT_DATA WRITE_LIFE_SHORT |
| 3272 | * WRITE_LIFE_NOT_SET WARM_DATA WRITE_LIFE_NOT_SET |
| 3273 | * WRITE_LIFE_NONE " WRITE_LIFE_NONE |
| 3274 | * WRITE_LIFE_MEDIUM " WRITE_LIFE_MEDIUM |
| 3275 | * WRITE_LIFE_LONG " WRITE_LIFE_LONG |
| 3276 | * |
Hyunchul Lee | f2e703f | 2018-01-31 11:36:58 +0900 | [diff] [blame] | 3277 | * 3) whint_mode=fs-based. F2FS passes down hints with its policy. |
| 3278 | * |
| 3279 | * User F2FS Block |
| 3280 | * ---- ---- ----- |
| 3281 | * META WRITE_LIFE_MEDIUM; |
| 3282 | * HOT_NODE WRITE_LIFE_NOT_SET |
| 3283 | * WARM_NODE " |
| 3284 | * COLD_NODE WRITE_LIFE_NONE |
| 3285 | * ioctl(COLD) COLD_DATA WRITE_LIFE_EXTREME |
| 3286 | * extension list " " |
| 3287 | * |
| 3288 | * -- buffered io |
| 3289 | * WRITE_LIFE_EXTREME COLD_DATA WRITE_LIFE_EXTREME |
| 3290 | * WRITE_LIFE_SHORT HOT_DATA WRITE_LIFE_SHORT |
| 3291 | * WRITE_LIFE_NOT_SET WARM_DATA WRITE_LIFE_LONG |
| 3292 | * WRITE_LIFE_NONE " " |
| 3293 | * WRITE_LIFE_MEDIUM " " |
| 3294 | * WRITE_LIFE_LONG " " |
| 3295 | * |
| 3296 | * -- direct io |
| 3297 | * WRITE_LIFE_EXTREME COLD_DATA WRITE_LIFE_EXTREME |
| 3298 | * WRITE_LIFE_SHORT HOT_DATA WRITE_LIFE_SHORT |
| 3299 | * WRITE_LIFE_NOT_SET WARM_DATA WRITE_LIFE_NOT_SET |
| 3300 | * WRITE_LIFE_NONE " WRITE_LIFE_NONE |
| 3301 | * WRITE_LIFE_MEDIUM " WRITE_LIFE_MEDIUM |
| 3302 | * WRITE_LIFE_LONG " WRITE_LIFE_LONG |
Hyunchul Lee | 0cdd319 | 2018-01-31 11:36:57 +0900 | [diff] [blame] | 3303 | */ |
| 3304 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 3305 | enum rw_hint f2fs_io_type_to_rw_hint(struct f2fs_sb_info *sbi, |
Hyunchul Lee | 0cdd319 | 2018-01-31 11:36:57 +0900 | [diff] [blame] | 3306 | enum page_type type, enum temp_type temp) |
| 3307 | { |
Chao Yu | 63189b7 | 2018-03-08 14:22:56 +0800 | [diff] [blame] | 3308 | if (F2FS_OPTION(sbi).whint_mode == WHINT_MODE_USER) { |
Hyunchul Lee | 0cdd319 | 2018-01-31 11:36:57 +0900 | [diff] [blame] | 3309 | if (type == DATA) { |
Hyunchul Lee | f2e703f | 2018-01-31 11:36:58 +0900 | [diff] [blame] | 3310 | if (temp == WARM) |
Hyunchul Lee | 0cdd319 | 2018-01-31 11:36:57 +0900 | [diff] [blame] | 3311 | return WRITE_LIFE_NOT_SET; |
Hyunchul Lee | f2e703f | 2018-01-31 11:36:58 +0900 | [diff] [blame] | 3312 | else if (temp == HOT) |
| 3313 | return WRITE_LIFE_SHORT; |
| 3314 | else if (temp == COLD) |
| 3315 | return WRITE_LIFE_EXTREME; |
Hyunchul Lee | 0cdd319 | 2018-01-31 11:36:57 +0900 | [diff] [blame] | 3316 | } else { |
| 3317 | return WRITE_LIFE_NOT_SET; |
| 3318 | } |
Chao Yu | 63189b7 | 2018-03-08 14:22:56 +0800 | [diff] [blame] | 3319 | } else if (F2FS_OPTION(sbi).whint_mode == WHINT_MODE_FS) { |
Hyunchul Lee | f2e703f | 2018-01-31 11:36:58 +0900 | [diff] [blame] | 3320 | if (type == DATA) { |
| 3321 | if (temp == WARM) |
| 3322 | return WRITE_LIFE_LONG; |
| 3323 | else if (temp == HOT) |
| 3324 | return WRITE_LIFE_SHORT; |
| 3325 | else if (temp == COLD) |
| 3326 | return WRITE_LIFE_EXTREME; |
| 3327 | } else if (type == NODE) { |
| 3328 | if (temp == WARM || temp == HOT) |
| 3329 | return WRITE_LIFE_NOT_SET; |
| 3330 | else if (temp == COLD) |
| 3331 | return WRITE_LIFE_NONE; |
| 3332 | } else if (type == META) { |
| 3333 | return WRITE_LIFE_MEDIUM; |
| 3334 | } |
Hyunchul Lee | 0cdd319 | 2018-01-31 11:36:57 +0900 | [diff] [blame] | 3335 | } |
Hyunchul Lee | f2e703f | 2018-01-31 11:36:58 +0900 | [diff] [blame] | 3336 | return WRITE_LIFE_NOT_SET; |
Hyunchul Lee | 0cdd319 | 2018-01-31 11:36:57 +0900 | [diff] [blame] | 3337 | } |
| 3338 | |
Jaegeuk Kim | 81377bd | 2017-05-10 14:19:54 -0700 | [diff] [blame] | 3339 | static int __get_segment_type_2(struct f2fs_io_info *fio) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3340 | { |
Jaegeuk Kim | 81377bd | 2017-05-10 14:19:54 -0700 | [diff] [blame] | 3341 | if (fio->type == DATA) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3342 | return CURSEG_HOT_DATA; |
| 3343 | else |
| 3344 | return CURSEG_HOT_NODE; |
| 3345 | } |
| 3346 | |
Jaegeuk Kim | 81377bd | 2017-05-10 14:19:54 -0700 | [diff] [blame] | 3347 | static int __get_segment_type_4(struct f2fs_io_info *fio) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3348 | { |
Jaegeuk Kim | 81377bd | 2017-05-10 14:19:54 -0700 | [diff] [blame] | 3349 | if (fio->type == DATA) { |
| 3350 | struct inode *inode = fio->page->mapping->host; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3351 | |
| 3352 | if (S_ISDIR(inode->i_mode)) |
| 3353 | return CURSEG_HOT_DATA; |
| 3354 | else |
| 3355 | return CURSEG_COLD_DATA; |
| 3356 | } else { |
Jaegeuk Kim | 81377bd | 2017-05-10 14:19:54 -0700 | [diff] [blame] | 3357 | if (IS_DNODE(fio->page) && is_cold_node(fio->page)) |
Jaegeuk Kim | a344b9f | 2014-11-05 20:05:53 -0800 | [diff] [blame] | 3358 | return CURSEG_WARM_NODE; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3359 | else |
| 3360 | return CURSEG_COLD_NODE; |
| 3361 | } |
| 3362 | } |
| 3363 | |
Jaegeuk Kim | 81377bd | 2017-05-10 14:19:54 -0700 | [diff] [blame] | 3364 | static int __get_segment_type_6(struct f2fs_io_info *fio) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3365 | { |
Jaegeuk Kim | 81377bd | 2017-05-10 14:19:54 -0700 | [diff] [blame] | 3366 | if (fio->type == DATA) { |
| 3367 | struct inode *inode = fio->page->mapping->host; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3368 | |
Chao Yu | 859fca6 | 2021-05-26 14:29:27 +0800 | [diff] [blame] | 3369 | if (is_inode_flag_set(inode, FI_ALIGNED_WRITE)) |
| 3370 | return CURSEG_COLD_DATA_PINNED; |
| 3371 | |
Chao Yu | b763f3b | 2021-04-28 17:20:31 +0800 | [diff] [blame] | 3372 | if (page_private_gcing(fio->page)) { |
Weichao Guo | ac2d750 | 2021-03-17 17:27:23 +0800 | [diff] [blame] | 3373 | if (fio->sbi->am.atgc_enabled && |
| 3374 | (fio->io_type == FS_DATA_IO) && |
| 3375 | (fio->sbi->gc_mode != GC_URGENT_HIGH)) |
Chao Yu | 093749e | 2020-08-04 21:14:49 +0800 | [diff] [blame] | 3376 | return CURSEG_ALL_DATA_ATGC; |
| 3377 | else |
| 3378 | return CURSEG_COLD_DATA; |
| 3379 | } |
Daeho Jeong | 602a16d | 2020-12-01 13:08:02 +0900 | [diff] [blame] | 3380 | if (file_is_cold(inode) || f2fs_need_compress_data(inode)) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3381 | return CURSEG_COLD_DATA; |
Chao Yu | b6a06cb | 2018-02-28 17:07:27 +0800 | [diff] [blame] | 3382 | if (file_is_hot(inode) || |
Chao Yu | b4c3ca8 | 2018-04-26 17:05:50 +0800 | [diff] [blame] | 3383 | is_inode_flag_set(inode, FI_HOT_DATA) || |
Chao Yu | 2079f11 | 2018-07-17 20:41:48 +0800 | [diff] [blame] | 3384 | f2fs_is_atomic_file(inode) || |
| 3385 | f2fs_is_volatile_file(inode)) |
Jaegeuk Kim | ef095d1 | 2017-03-24 20:05:13 -0400 | [diff] [blame] | 3386 | return CURSEG_HOT_DATA; |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 3387 | return f2fs_rw_hint_to_seg_type(inode->i_write_hint); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3388 | } else { |
Jaegeuk Kim | 81377bd | 2017-05-10 14:19:54 -0700 | [diff] [blame] | 3389 | if (IS_DNODE(fio->page)) |
| 3390 | return is_cold_node(fio->page) ? CURSEG_WARM_NODE : |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3391 | CURSEG_HOT_NODE; |
Jaegeuk Kim | ef095d1 | 2017-03-24 20:05:13 -0400 | [diff] [blame] | 3392 | return CURSEG_COLD_NODE; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3393 | } |
| 3394 | } |
| 3395 | |
Jaegeuk Kim | 81377bd | 2017-05-10 14:19:54 -0700 | [diff] [blame] | 3396 | static int __get_segment_type(struct f2fs_io_info *fio) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3397 | { |
Jaegeuk Kim | a912b54 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 3398 | int type = 0; |
| 3399 | |
Chao Yu | 63189b7 | 2018-03-08 14:22:56 +0800 | [diff] [blame] | 3400 | switch (F2FS_OPTION(fio->sbi).active_logs) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3401 | case 2: |
Jaegeuk Kim | a912b54 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 3402 | type = __get_segment_type_2(fio); |
| 3403 | break; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3404 | case 4: |
Jaegeuk Kim | a912b54 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 3405 | type = __get_segment_type_4(fio); |
| 3406 | break; |
| 3407 | case 6: |
| 3408 | type = __get_segment_type_6(fio); |
| 3409 | break; |
| 3410 | default: |
| 3411 | f2fs_bug_on(fio->sbi, true); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3412 | } |
Jaegeuk Kim | 81377bd | 2017-05-10 14:19:54 -0700 | [diff] [blame] | 3413 | |
Jaegeuk Kim | a912b54 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 3414 | if (IS_HOT(type)) |
| 3415 | fio->temp = HOT; |
| 3416 | else if (IS_WARM(type)) |
| 3417 | fio->temp = WARM; |
| 3418 | else |
| 3419 | fio->temp = COLD; |
| 3420 | return type; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3421 | } |
| 3422 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 3423 | void f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct page *page, |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 3424 | block_t old_blkaddr, block_t *new_blkaddr, |
Chao Yu | fb830fc | 2017-05-19 23:37:01 +0800 | [diff] [blame] | 3425 | struct f2fs_summary *sum, int type, |
Chao Yu | 093749e | 2020-08-04 21:14:49 +0800 | [diff] [blame] | 3426 | struct f2fs_io_info *fio) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3427 | { |
| 3428 | struct sit_info *sit_i = SIT_I(sbi); |
Jaegeuk Kim | 6ae1be1 | 2016-11-11 12:31:40 -0800 | [diff] [blame] | 3429 | struct curseg_info *curseg = CURSEG_I(sbi, type); |
Chao Yu | c5d0278 | 2020-08-04 21:14:47 +0800 | [diff] [blame] | 3430 | unsigned long long old_mtime; |
Chao Yu | 093749e | 2020-08-04 21:14:49 +0800 | [diff] [blame] | 3431 | bool from_gc = (type == CURSEG_ALL_DATA_ATGC); |
| 3432 | struct seg_entry *se = NULL; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3433 | |
Chao Yu | 2b60311 | 2017-11-02 20:41:03 +0800 | [diff] [blame] | 3434 | down_read(&SM_I(sbi)->curseg_lock); |
| 3435 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3436 | mutex_lock(&curseg->curseg_mutex); |
Chao Yu | 3d26fa6 | 2017-10-30 17:49:53 +0800 | [diff] [blame] | 3437 | down_write(&sit_i->sentry_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3438 | |
Chao Yu | 093749e | 2020-08-04 21:14:49 +0800 | [diff] [blame] | 3439 | if (from_gc) { |
| 3440 | f2fs_bug_on(sbi, GET_SEGNO(sbi, old_blkaddr) == NULL_SEGNO); |
| 3441 | se = get_seg_entry(sbi, GET_SEGNO(sbi, old_blkaddr)); |
| 3442 | sanity_check_seg_type(sbi, se->type); |
| 3443 | f2fs_bug_on(sbi, IS_NODESEG(se->type)); |
| 3444 | } |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3445 | *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3446 | |
Chao Yu | 093749e | 2020-08-04 21:14:49 +0800 | [diff] [blame] | 3447 | f2fs_bug_on(sbi, curseg->next_blkoff >= sbi->blocks_per_seg); |
| 3448 | |
Jaegeuk Kim | 4e6a8d9 | 2016-12-29 14:07:53 -0800 | [diff] [blame] | 3449 | f2fs_wait_discard_bio(sbi, *new_blkaddr); |
| 3450 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3451 | /* |
| 3452 | * __add_sum_entry should be resided under the curseg_mutex |
| 3453 | * because, this function updates a summary entry in the |
| 3454 | * current summary block. |
| 3455 | */ |
Haicheng Li | e79efe3 | 2013-06-13 16:59:27 +0800 | [diff] [blame] | 3456 | __add_sum_entry(sbi, type, sum); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3457 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3458 | __refresh_next_blkoff(sbi, curseg); |
Jaegeuk Kim | dcdfff6 | 2013-10-22 20:56:10 +0900 | [diff] [blame] | 3459 | |
| 3460 | stat_inc_block_count(sbi, curseg); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3461 | |
Chao Yu | c5d0278 | 2020-08-04 21:14:47 +0800 | [diff] [blame] | 3462 | if (from_gc) { |
| 3463 | old_mtime = get_segment_mtime(sbi, old_blkaddr); |
| 3464 | } else { |
| 3465 | update_segment_mtime(sbi, old_blkaddr, 0); |
| 3466 | old_mtime = 0; |
| 3467 | } |
| 3468 | update_segment_mtime(sbi, *new_blkaddr, old_mtime); |
| 3469 | |
Yunlong Song | 65f1b80 | 2017-10-30 09:33:41 +0800 | [diff] [blame] | 3470 | /* |
| 3471 | * SIT information should be updated before segment allocation, |
| 3472 | * since SSR needs latest valid block information. |
| 3473 | */ |
| 3474 | update_sit_entry(sbi, *new_blkaddr, 1); |
| 3475 | if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO) |
| 3476 | update_sit_entry(sbi, old_blkaddr, -1); |
| 3477 | |
Chao Yu | 093749e | 2020-08-04 21:14:49 +0800 | [diff] [blame] | 3478 | if (!__has_curseg_space(sbi, curseg)) { |
| 3479 | if (from_gc) |
| 3480 | get_atssr_segment(sbi, type, se->type, |
| 3481 | AT_SSR, se->mtime); |
| 3482 | else |
| 3483 | sit_i->s_ops->allocate_segment(sbi, type, false); |
| 3484 | } |
Jaegeuk Kim | c6f82fe9 | 2017-04-04 16:45:30 -0700 | [diff] [blame] | 3485 | /* |
Yunlong Song | 65f1b80 | 2017-10-30 09:33:41 +0800 | [diff] [blame] | 3486 | * segment dirty status should be updated after segment allocation, |
| 3487 | * so we just need to update status only one time after previous |
| 3488 | * segment being closed. |
Jaegeuk Kim | c6f82fe9 | 2017-04-04 16:45:30 -0700 | [diff] [blame] | 3489 | */ |
Yunlong Song | 65f1b80 | 2017-10-30 09:33:41 +0800 | [diff] [blame] | 3490 | locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr)); |
| 3491 | locate_dirty_segment(sbi, GET_SEGNO(sbi, *new_blkaddr)); |
Yunlong Song | 3436c4b | 2017-02-21 16:59:26 +0800 | [diff] [blame] | 3492 | |
Chao Yu | 3d26fa6 | 2017-10-30 17:49:53 +0800 | [diff] [blame] | 3493 | up_write(&sit_i->sentry_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3494 | |
Chao Yu | 704956e | 2017-07-31 20:19:09 +0800 | [diff] [blame] | 3495 | if (page && IS_NODESEG(type)) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3496 | fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg)); |
| 3497 | |
Chao Yu | 704956e | 2017-07-31 20:19:09 +0800 | [diff] [blame] | 3498 | f2fs_inode_chksum_set(sbi, page); |
| 3499 | } |
| 3500 | |
Chao Yu | f608c38 | 2020-06-18 14:36:24 +0800 | [diff] [blame] | 3501 | if (fio) { |
Chao Yu | fb830fc | 2017-05-19 23:37:01 +0800 | [diff] [blame] | 3502 | struct f2fs_bio_info *io; |
| 3503 | |
Chao Yu | 25ae837 | 2021-04-02 17:22:23 +0800 | [diff] [blame] | 3504 | if (F2FS_IO_ALIGNED(sbi)) |
| 3505 | fio->retry = false; |
| 3506 | |
Chao Yu | fb830fc | 2017-05-19 23:37:01 +0800 | [diff] [blame] | 3507 | INIT_LIST_HEAD(&fio->list); |
| 3508 | fio->in_list = true; |
| 3509 | io = sbi->write_io[fio->type] + fio->temp; |
| 3510 | spin_lock(&io->io_lock); |
| 3511 | list_add_tail(&fio->list, &io->io_list); |
| 3512 | spin_unlock(&io->io_lock); |
| 3513 | } |
| 3514 | |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 3515 | mutex_unlock(&curseg->curseg_mutex); |
Chao Yu | 2b60311 | 2017-11-02 20:41:03 +0800 | [diff] [blame] | 3516 | |
| 3517 | up_read(&SM_I(sbi)->curseg_lock); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 3518 | } |
| 3519 | |
Chao Yu | 71f2c82 | 2021-09-01 14:39:20 +0800 | [diff] [blame] | 3520 | void f2fs_update_device_state(struct f2fs_sb_info *sbi, nid_t ino, |
| 3521 | block_t blkaddr, unsigned int blkcnt) |
Chao Yu | 39d787b | 2017-09-29 13:59:38 +0800 | [diff] [blame] | 3522 | { |
Damien Le Moal | 0916878 | 2019-03-16 09:13:06 +0900 | [diff] [blame] | 3523 | if (!f2fs_is_multi_device(sbi)) |
Chao Yu | 39d787b | 2017-09-29 13:59:38 +0800 | [diff] [blame] | 3524 | return; |
| 3525 | |
Chao Yu | 71f2c82 | 2021-09-01 14:39:20 +0800 | [diff] [blame] | 3526 | while (1) { |
| 3527 | unsigned int devidx = f2fs_target_device_index(sbi, blkaddr); |
| 3528 | unsigned int blks = FDEV(devidx).end_blk - blkaddr + 1; |
Chao Yu | 39d787b | 2017-09-29 13:59:38 +0800 | [diff] [blame] | 3529 | |
Chao Yu | 71f2c82 | 2021-09-01 14:39:20 +0800 | [diff] [blame] | 3530 | /* update device state for fsync */ |
| 3531 | f2fs_set_dirty_device(sbi, ino, devidx, FLUSH_INO); |
Chao Yu | 1228b48 | 2017-09-29 13:59:39 +0800 | [diff] [blame] | 3532 | |
Chao Yu | 71f2c82 | 2021-09-01 14:39:20 +0800 | [diff] [blame] | 3533 | /* update device state for checkpoint */ |
| 3534 | if (!f2fs_test_bit(devidx, (char *)&sbi->dirty_device)) { |
| 3535 | spin_lock(&sbi->dev_lock); |
| 3536 | f2fs_set_bit(devidx, (char *)&sbi->dirty_device); |
| 3537 | spin_unlock(&sbi->dev_lock); |
| 3538 | } |
| 3539 | |
| 3540 | if (blkcnt <= blks) |
| 3541 | break; |
| 3542 | blkcnt -= blks; |
| 3543 | blkaddr += blks; |
Chao Yu | 1228b48 | 2017-09-29 13:59:39 +0800 | [diff] [blame] | 3544 | } |
Chao Yu | 39d787b | 2017-09-29 13:59:38 +0800 | [diff] [blame] | 3545 | } |
| 3546 | |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 3547 | static void do_write_page(struct f2fs_summary *sum, struct f2fs_io_info *fio) |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 3548 | { |
Jaegeuk Kim | 81377bd | 2017-05-10 14:19:54 -0700 | [diff] [blame] | 3549 | int type = __get_segment_type(fio); |
Chao Yu | b0332a0 | 2020-02-14 17:44:12 +0800 | [diff] [blame] | 3550 | bool keep_order = (f2fs_lfs_mode(fio->sbi) && type == CURSEG_COLD_DATA); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 3551 | |
Chao Yu | 107a805 | 2018-05-26 09:00:13 +0800 | [diff] [blame] | 3552 | if (keep_order) |
| 3553 | down_read(&fio->sbi->io_order_lock); |
Jaegeuk Kim | 0a595eb | 2016-12-14 10:12:56 -0800 | [diff] [blame] | 3554 | reallocate: |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 3555 | f2fs_allocate_data_block(fio->sbi, fio->page, fio->old_blkaddr, |
Chao Yu | 093749e | 2020-08-04 21:14:49 +0800 | [diff] [blame] | 3556 | &fio->new_blkaddr, sum, type, fio); |
Chao Yu | 6ce19af | 2021-05-20 19:51:50 +0800 | [diff] [blame] | 3557 | if (GET_SEGNO(fio->sbi, fio->old_blkaddr) != NULL_SEGNO) { |
Chao Yu | 6aa58d8 | 2018-08-14 22:37:25 +0800 | [diff] [blame] | 3558 | invalidate_mapping_pages(META_MAPPING(fio->sbi), |
| 3559 | fio->old_blkaddr, fio->old_blkaddr); |
Chao Yu | 6ce19af | 2021-05-20 19:51:50 +0800 | [diff] [blame] | 3560 | f2fs_invalidate_compress_page(fio->sbi, fio->old_blkaddr); |
| 3561 | } |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 3562 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3563 | /* writeout dirty page into bdev */ |
Chao Yu | fe16efe | 2018-05-28 23:47:18 +0800 | [diff] [blame] | 3564 | f2fs_submit_page_write(fio); |
| 3565 | if (fio->retry) { |
Jaegeuk Kim | 0a595eb | 2016-12-14 10:12:56 -0800 | [diff] [blame] | 3566 | fio->old_blkaddr = fio->new_blkaddr; |
| 3567 | goto reallocate; |
| 3568 | } |
Chao Yu | fe16efe | 2018-05-28 23:47:18 +0800 | [diff] [blame] | 3569 | |
Chao Yu | 71f2c82 | 2021-09-01 14:39:20 +0800 | [diff] [blame] | 3570 | f2fs_update_device_state(fio->sbi, fio->ino, fio->new_blkaddr, 1); |
Chao Yu | fe16efe | 2018-05-28 23:47:18 +0800 | [diff] [blame] | 3571 | |
Chao Yu | 107a805 | 2018-05-26 09:00:13 +0800 | [diff] [blame] | 3572 | if (keep_order) |
| 3573 | up_read(&fio->sbi->io_order_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3574 | } |
| 3575 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 3576 | void f2fs_do_write_meta_page(struct f2fs_sb_info *sbi, struct page *page, |
Chao Yu | b0af6d4 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 3577 | enum iostat_type io_type) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3578 | { |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 3579 | struct f2fs_io_info fio = { |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 3580 | .sbi = sbi, |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 3581 | .type = META, |
Hyunchul Lee | 0cdd319 | 2018-01-31 11:36:57 +0900 | [diff] [blame] | 3582 | .temp = HOT, |
Mike Christie | 04d328d | 2016-06-05 14:31:55 -0500 | [diff] [blame] | 3583 | .op = REQ_OP_WRITE, |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 3584 | .op_flags = REQ_SYNC | REQ_META | REQ_PRIO, |
Chao Yu | 7a9d754 | 2016-02-22 18:36:38 +0800 | [diff] [blame] | 3585 | .old_blkaddr = page->index, |
| 3586 | .new_blkaddr = page->index, |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 3587 | .page = page, |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 3588 | .encrypted_page = NULL, |
Chao Yu | fb830fc | 2017-05-19 23:37:01 +0800 | [diff] [blame] | 3589 | .in_list = false, |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 3590 | }; |
| 3591 | |
Chao Yu | 2b94700 | 2015-10-12 17:04:21 +0800 | [diff] [blame] | 3592 | if (unlikely(page->index >= MAIN_BLKADDR(sbi))) |
Mike Christie | 04d328d | 2016-06-05 14:31:55 -0500 | [diff] [blame] | 3593 | fio.op_flags &= ~REQ_META; |
Chao Yu | 2b94700 | 2015-10-12 17:04:21 +0800 | [diff] [blame] | 3594 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3595 | set_page_writeback(page); |
Jaegeuk Kim | 17c5003 | 2018-04-11 23:09:04 -0700 | [diff] [blame] | 3596 | ClearPageError(page); |
Jaegeuk Kim | b9109b0 | 2017-05-10 11:28:38 -0700 | [diff] [blame] | 3597 | f2fs_submit_page_write(&fio); |
Chao Yu | b0af6d4 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 3598 | |
Chao Yu | b63e7be | 2018-09-29 18:31:27 +0800 | [diff] [blame] | 3599 | stat_inc_meta_count(sbi, page->index); |
Chao Yu | b0af6d4 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 3600 | f2fs_update_iostat(sbi, io_type, F2FS_BLKSIZE); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3601 | } |
| 3602 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 3603 | void f2fs_do_write_node_page(unsigned int nid, struct f2fs_io_info *fio) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3604 | { |
| 3605 | struct f2fs_summary sum; |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 3606 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3607 | set_summary(&sum, nid, 0, 0); |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 3608 | do_write_page(&sum, fio); |
Chao Yu | b0af6d4 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 3609 | |
| 3610 | f2fs_update_iostat(fio->sbi, fio->io_type, F2FS_BLKSIZE); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3611 | } |
| 3612 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 3613 | void f2fs_outplace_write_data(struct dnode_of_data *dn, |
| 3614 | struct f2fs_io_info *fio) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3615 | { |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 3616 | struct f2fs_sb_info *sbi = fio->sbi; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3617 | struct f2fs_summary sum; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3618 | |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 3619 | f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR); |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 3620 | set_summary(&sum, dn->nid, dn->ofs_in_node, fio->version); |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 3621 | do_write_page(&sum, fio); |
Chao Yu | f28b343 | 2016-02-24 17:16:47 +0800 | [diff] [blame] | 3622 | f2fs_update_data_blkaddr(dn, fio->new_blkaddr); |
Chao Yu | b0af6d4 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 3623 | |
| 3624 | f2fs_update_iostat(sbi, fio->io_type, F2FS_BLKSIZE); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3625 | } |
| 3626 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 3627 | int f2fs_inplace_write_data(struct f2fs_io_info *fio) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3628 | { |
Chao Yu | b0af6d4 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 3629 | int err; |
Yunlei He | d21b0f2 | 2018-03-26 17:32:23 +0800 | [diff] [blame] | 3630 | struct f2fs_sb_info *sbi = fio->sbi; |
Chao Yu | 05573d6 | 2019-04-15 15:30:52 +0800 | [diff] [blame] | 3631 | unsigned int segno; |
Chao Yu | b0af6d4 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 3632 | |
Chao Yu | 7a9d754 | 2016-02-22 18:36:38 +0800 | [diff] [blame] | 3633 | fio->new_blkaddr = fio->old_blkaddr; |
Hyunchul Lee | 0cdd319 | 2018-01-31 11:36:57 +0900 | [diff] [blame] | 3634 | /* i/o temperature is needed for passing down write hints */ |
| 3635 | __get_segment_type(fio); |
Yunlei He | d21b0f2 | 2018-03-26 17:32:23 +0800 | [diff] [blame] | 3636 | |
Chao Yu | 05573d6 | 2019-04-15 15:30:52 +0800 | [diff] [blame] | 3637 | segno = GET_SEGNO(sbi, fio->new_blkaddr); |
| 3638 | |
| 3639 | if (!IS_DATASEG(get_seg_entry(sbi, segno)->type)) { |
| 3640 | set_sbi_flag(sbi, SBI_NEED_FSCK); |
Chao Yu | 2d821c1 | 2019-06-18 17:59:03 +0800 | [diff] [blame] | 3641 | f2fs_warn(sbi, "%s: incorrect segment(%u) type, run fsck to fix.", |
| 3642 | __func__, segno); |
Chao Yu | 9557727 | 2021-04-22 18:19:25 +0800 | [diff] [blame] | 3643 | err = -EFSCORRUPTED; |
| 3644 | goto drop_bio; |
| 3645 | } |
| 3646 | |
Jaegeuk Kim | 1ffc8f5 | 2021-07-14 16:14:02 -0700 | [diff] [blame] | 3647 | if (f2fs_cp_error(sbi)) { |
Chao Yu | 9557727 | 2021-04-22 18:19:25 +0800 | [diff] [blame] | 3648 | err = -EIO; |
| 3649 | goto drop_bio; |
Chao Yu | 05573d6 | 2019-04-15 15:30:52 +0800 | [diff] [blame] | 3650 | } |
Yunlei He | d21b0f2 | 2018-03-26 17:32:23 +0800 | [diff] [blame] | 3651 | |
Hyeong-Jun Kim | e3b49ea | 2021-11-02 16:10:02 +0900 | [diff] [blame] | 3652 | invalidate_mapping_pages(META_MAPPING(sbi), |
| 3653 | fio->new_blkaddr, fio->new_blkaddr); |
| 3654 | |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 3655 | stat_inc_inplace_blocks(fio->sbi); |
Chao Yu | b0af6d4 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 3656 | |
Jaegeuk Kim | 0e7f4197 | 2020-01-06 16:43:09 -0800 | [diff] [blame] | 3657 | if (fio->bio && !(SM_I(sbi)->ipu_policy & (1 << F2FS_IPU_NOCACHE))) |
Chao Yu | 8648de2 | 2019-02-19 16:15:29 +0800 | [diff] [blame] | 3658 | err = f2fs_merge_page_bio(fio); |
| 3659 | else |
| 3660 | err = f2fs_submit_page_bio(fio); |
Chao Yu | e46f6bd | 2019-02-21 20:40:13 +0800 | [diff] [blame] | 3661 | if (!err) { |
Chao Yu | 71f2c82 | 2021-09-01 14:39:20 +0800 | [diff] [blame] | 3662 | f2fs_update_device_state(fio->sbi, fio->ino, |
| 3663 | fio->new_blkaddr, 1); |
Chao Yu | e46f6bd | 2019-02-21 20:40:13 +0800 | [diff] [blame] | 3664 | f2fs_update_iostat(fio->sbi, fio->io_type, F2FS_BLKSIZE); |
| 3665 | } |
Chao Yu | b0af6d4 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 3666 | |
| 3667 | return err; |
Chao Yu | 9557727 | 2021-04-22 18:19:25 +0800 | [diff] [blame] | 3668 | drop_bio: |
Jaegeuk Kim | 349c4d6 | 2021-05-09 21:53:03 -0700 | [diff] [blame] | 3669 | if (fio->bio && *(fio->bio)) { |
Chao Yu | 9557727 | 2021-04-22 18:19:25 +0800 | [diff] [blame] | 3670 | struct bio *bio = *(fio->bio); |
| 3671 | |
| 3672 | bio->bi_status = BLK_STS_IOERR; |
| 3673 | bio_endio(bio); |
Jaegeuk Kim | 349c4d6 | 2021-05-09 21:53:03 -0700 | [diff] [blame] | 3674 | *(fio->bio) = NULL; |
Chao Yu | 9557727 | 2021-04-22 18:19:25 +0800 | [diff] [blame] | 3675 | } |
| 3676 | return err; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3677 | } |
| 3678 | |
Chao Yu | 2b60311 | 2017-11-02 20:41:03 +0800 | [diff] [blame] | 3679 | static inline int __f2fs_get_curseg(struct f2fs_sb_info *sbi, |
| 3680 | unsigned int segno) |
| 3681 | { |
| 3682 | int i; |
| 3683 | |
| 3684 | for (i = CURSEG_HOT_DATA; i < NO_CHECK_TYPE; i++) { |
| 3685 | if (CURSEG_I(sbi, i)->segno == segno) |
| 3686 | break; |
| 3687 | } |
| 3688 | return i; |
| 3689 | } |
| 3690 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 3691 | void f2fs_do_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum, |
Chao Yu | 19f106b | 2015-05-06 13:08:06 +0800 | [diff] [blame] | 3692 | block_t old_blkaddr, block_t new_blkaddr, |
Chao Yu | c5d0278 | 2020-08-04 21:14:47 +0800 | [diff] [blame] | 3693 | bool recover_curseg, bool recover_newaddr, |
| 3694 | bool from_gc) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3695 | { |
| 3696 | struct sit_info *sit_i = SIT_I(sbi); |
| 3697 | struct curseg_info *curseg; |
| 3698 | unsigned int segno, old_cursegno; |
| 3699 | struct seg_entry *se; |
| 3700 | int type; |
Chao Yu | 19f106b | 2015-05-06 13:08:06 +0800 | [diff] [blame] | 3701 | unsigned short old_blkoff; |
Wang Xiaojun | 753a8ed | 2021-03-25 10:19:20 -0400 | [diff] [blame] | 3702 | unsigned char old_alloc_type; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3703 | |
| 3704 | segno = GET_SEGNO(sbi, new_blkaddr); |
| 3705 | se = get_seg_entry(sbi, segno); |
| 3706 | type = se->type; |
| 3707 | |
Chao Yu | 2b60311 | 2017-11-02 20:41:03 +0800 | [diff] [blame] | 3708 | down_write(&SM_I(sbi)->curseg_lock); |
| 3709 | |
Chao Yu | 19f106b | 2015-05-06 13:08:06 +0800 | [diff] [blame] | 3710 | if (!recover_curseg) { |
| 3711 | /* for recovery flow */ |
| 3712 | if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) { |
| 3713 | if (old_blkaddr == NULL_ADDR) |
| 3714 | type = CURSEG_COLD_DATA; |
| 3715 | else |
| 3716 | type = CURSEG_WARM_DATA; |
| 3717 | } |
| 3718 | } else { |
Chao Yu | 2b60311 | 2017-11-02 20:41:03 +0800 | [diff] [blame] | 3719 | if (IS_CURSEG(sbi, segno)) { |
| 3720 | /* se->type is volatile as SSR allocation */ |
| 3721 | type = __f2fs_get_curseg(sbi, segno); |
| 3722 | f2fs_bug_on(sbi, type == NO_CHECK_TYPE); |
| 3723 | } else { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3724 | type = CURSEG_WARM_DATA; |
Chao Yu | 2b60311 | 2017-11-02 20:41:03 +0800 | [diff] [blame] | 3725 | } |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3726 | } |
Chao Yu | 19f106b | 2015-05-06 13:08:06 +0800 | [diff] [blame] | 3727 | |
Yunlong Song | 2c19050 | 2018-01-04 15:02:02 +0800 | [diff] [blame] | 3728 | f2fs_bug_on(sbi, !IS_DATASEG(type)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3729 | curseg = CURSEG_I(sbi, type); |
| 3730 | |
| 3731 | mutex_lock(&curseg->curseg_mutex); |
Chao Yu | 3d26fa6 | 2017-10-30 17:49:53 +0800 | [diff] [blame] | 3732 | down_write(&sit_i->sentry_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3733 | |
| 3734 | old_cursegno = curseg->segno; |
Chao Yu | 19f106b | 2015-05-06 13:08:06 +0800 | [diff] [blame] | 3735 | old_blkoff = curseg->next_blkoff; |
Wang Xiaojun | 753a8ed | 2021-03-25 10:19:20 -0400 | [diff] [blame] | 3736 | old_alloc_type = curseg->alloc_type; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3737 | |
| 3738 | /* change the current segment */ |
| 3739 | if (segno != curseg->segno) { |
| 3740 | curseg->next_segno = segno; |
Chao Yu | 093749e | 2020-08-04 21:14:49 +0800 | [diff] [blame] | 3741 | change_curseg(sbi, type, true); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3742 | } |
| 3743 | |
Jaegeuk Kim | 491c085 | 2014-02-04 13:01:10 +0900 | [diff] [blame] | 3744 | curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr); |
Haicheng Li | e79efe3 | 2013-06-13 16:59:27 +0800 | [diff] [blame] | 3745 | __add_sum_entry(sbi, type, sum); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3746 | |
Chao Yu | c5d0278 | 2020-08-04 21:14:47 +0800 | [diff] [blame] | 3747 | if (!recover_curseg || recover_newaddr) { |
| 3748 | if (!from_gc) |
| 3749 | update_segment_mtime(sbi, new_blkaddr, 0); |
Jaegeuk Kim | 6e2c64a | 2015-10-07 12:28:41 -0700 | [diff] [blame] | 3750 | update_sit_entry(sbi, new_blkaddr, 1); |
Chao Yu | c5d0278 | 2020-08-04 21:14:47 +0800 | [diff] [blame] | 3751 | } |
Chao Yu | 6aa58d8 | 2018-08-14 22:37:25 +0800 | [diff] [blame] | 3752 | if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO) { |
| 3753 | invalidate_mapping_pages(META_MAPPING(sbi), |
| 3754 | old_blkaddr, old_blkaddr); |
Chao Yu | 6ce19af | 2021-05-20 19:51:50 +0800 | [diff] [blame] | 3755 | f2fs_invalidate_compress_page(sbi, old_blkaddr); |
Chao Yu | c5d0278 | 2020-08-04 21:14:47 +0800 | [diff] [blame] | 3756 | if (!from_gc) |
| 3757 | update_segment_mtime(sbi, old_blkaddr, 0); |
Jaegeuk Kim | 6e2c64a | 2015-10-07 12:28:41 -0700 | [diff] [blame] | 3758 | update_sit_entry(sbi, old_blkaddr, -1); |
Chao Yu | 6aa58d8 | 2018-08-14 22:37:25 +0800 | [diff] [blame] | 3759 | } |
Jaegeuk Kim | 6e2c64a | 2015-10-07 12:28:41 -0700 | [diff] [blame] | 3760 | |
| 3761 | locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr)); |
| 3762 | locate_dirty_segment(sbi, GET_SEGNO(sbi, new_blkaddr)); |
| 3763 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3764 | locate_dirty_segment(sbi, old_cursegno); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3765 | |
Chao Yu | 19f106b | 2015-05-06 13:08:06 +0800 | [diff] [blame] | 3766 | if (recover_curseg) { |
| 3767 | if (old_cursegno != curseg->segno) { |
| 3768 | curseg->next_segno = old_cursegno; |
Chao Yu | 093749e | 2020-08-04 21:14:49 +0800 | [diff] [blame] | 3769 | change_curseg(sbi, type, true); |
Chao Yu | 19f106b | 2015-05-06 13:08:06 +0800 | [diff] [blame] | 3770 | } |
| 3771 | curseg->next_blkoff = old_blkoff; |
Wang Xiaojun | 753a8ed | 2021-03-25 10:19:20 -0400 | [diff] [blame] | 3772 | curseg->alloc_type = old_alloc_type; |
Chao Yu | 19f106b | 2015-05-06 13:08:06 +0800 | [diff] [blame] | 3773 | } |
| 3774 | |
Chao Yu | 3d26fa6 | 2017-10-30 17:49:53 +0800 | [diff] [blame] | 3775 | up_write(&sit_i->sentry_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3776 | mutex_unlock(&curseg->curseg_mutex); |
Chao Yu | 2b60311 | 2017-11-02 20:41:03 +0800 | [diff] [blame] | 3777 | up_write(&SM_I(sbi)->curseg_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3778 | } |
| 3779 | |
Chao Yu | 528e345 | 2015-05-28 19:15:35 +0800 | [diff] [blame] | 3780 | void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn, |
| 3781 | block_t old_addr, block_t new_addr, |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 3782 | unsigned char version, bool recover_curseg, |
| 3783 | bool recover_newaddr) |
Chao Yu | 528e345 | 2015-05-28 19:15:35 +0800 | [diff] [blame] | 3784 | { |
| 3785 | struct f2fs_summary sum; |
| 3786 | |
| 3787 | set_summary(&sum, dn->nid, dn->ofs_in_node, version); |
| 3788 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 3789 | f2fs_do_replace_block(sbi, &sum, old_addr, new_addr, |
Chao Yu | c5d0278 | 2020-08-04 21:14:47 +0800 | [diff] [blame] | 3790 | recover_curseg, recover_newaddr, false); |
Chao Yu | 528e345 | 2015-05-28 19:15:35 +0800 | [diff] [blame] | 3791 | |
Chao Yu | f28b343 | 2016-02-24 17:16:47 +0800 | [diff] [blame] | 3792 | f2fs_update_data_blkaddr(dn, new_addr); |
Chao Yu | 528e345 | 2015-05-28 19:15:35 +0800 | [diff] [blame] | 3793 | } |
| 3794 | |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 3795 | void f2fs_wait_on_page_writeback(struct page *page, |
Chao Yu | bae0ee7 | 2018-12-25 17:43:42 +0800 | [diff] [blame] | 3796 | enum page_type type, bool ordered, bool locked) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 3797 | { |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 3798 | if (PageWriteback(page)) { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 3799 | struct f2fs_sb_info *sbi = F2FS_P_SB(page); |
| 3800 | |
Chao Yu | 0b20fce | 2019-09-30 18:53:25 +0800 | [diff] [blame] | 3801 | /* submit cached LFS IO */ |
Chao Yu | bab475c | 2018-09-27 23:41:16 +0800 | [diff] [blame] | 3802 | f2fs_submit_merged_write_cond(sbi, NULL, page, 0, type); |
Chao Yu | 0b20fce | 2019-09-30 18:53:25 +0800 | [diff] [blame] | 3803 | /* sbumit cached IPU IO */ |
| 3804 | f2fs_submit_merged_ipu_write(sbi, NULL, page); |
Chao Yu | bae0ee7 | 2018-12-25 17:43:42 +0800 | [diff] [blame] | 3805 | if (ordered) { |
Jaegeuk Kim | fec1d65 | 2016-01-20 23:43:51 +0800 | [diff] [blame] | 3806 | wait_on_page_writeback(page); |
Chao Yu | bae0ee7 | 2018-12-25 17:43:42 +0800 | [diff] [blame] | 3807 | f2fs_bug_on(sbi, locked && PageWriteback(page)); |
| 3808 | } else { |
Jaegeuk Kim | fec1d65 | 2016-01-20 23:43:51 +0800 | [diff] [blame] | 3809 | wait_for_stable_page(page); |
Chao Yu | bae0ee7 | 2018-12-25 17:43:42 +0800 | [diff] [blame] | 3810 | } |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 3811 | } |
| 3812 | } |
| 3813 | |
Jaegeuk Kim | 0ded69f | 2018-08-22 21:18:00 -0700 | [diff] [blame] | 3814 | void f2fs_wait_on_block_writeback(struct inode *inode, block_t blkaddr) |
Chao Yu | 08b39fb | 2015-10-08 13:27:34 +0800 | [diff] [blame] | 3815 | { |
Jaegeuk Kim | 0ded69f | 2018-08-22 21:18:00 -0700 | [diff] [blame] | 3816 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Chao Yu | 08b39fb | 2015-10-08 13:27:34 +0800 | [diff] [blame] | 3817 | struct page *cpage; |
| 3818 | |
Jaegeuk Kim | 0ded69f | 2018-08-22 21:18:00 -0700 | [diff] [blame] | 3819 | if (!f2fs_post_read_required(inode)) |
| 3820 | return; |
| 3821 | |
Chao Yu | 93770ab | 2019-04-15 15:26:32 +0800 | [diff] [blame] | 3822 | if (!__is_valid_data_blkaddr(blkaddr)) |
Chao Yu | 08b39fb | 2015-10-08 13:27:34 +0800 | [diff] [blame] | 3823 | return; |
| 3824 | |
Chao Yu | 08b39fb | 2015-10-08 13:27:34 +0800 | [diff] [blame] | 3825 | cpage = find_lock_page(META_MAPPING(sbi), blkaddr); |
| 3826 | if (cpage) { |
Chao Yu | bae0ee7 | 2018-12-25 17:43:42 +0800 | [diff] [blame] | 3827 | f2fs_wait_on_page_writeback(cpage, DATA, true, true); |
Chao Yu | 08b39fb | 2015-10-08 13:27:34 +0800 | [diff] [blame] | 3828 | f2fs_put_page(cpage, 1); |
| 3829 | } |
| 3830 | } |
| 3831 | |
Sahitya Tummala | 1e78e8b | 2018-10-10 10:56:22 +0530 | [diff] [blame] | 3832 | void f2fs_wait_on_block_writeback_range(struct inode *inode, block_t blkaddr, |
| 3833 | block_t len) |
| 3834 | { |
| 3835 | block_t i; |
| 3836 | |
| 3837 | for (i = 0; i < len; i++) |
| 3838 | f2fs_wait_on_block_writeback(inode, blkaddr + i); |
| 3839 | } |
| 3840 | |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 3841 | static int read_compacted_summaries(struct f2fs_sb_info *sbi) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3842 | { |
| 3843 | struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); |
| 3844 | struct curseg_info *seg_i; |
| 3845 | unsigned char *kaddr; |
| 3846 | struct page *page; |
| 3847 | block_t start; |
| 3848 | int i, j, offset; |
| 3849 | |
| 3850 | start = start_sum_block(sbi); |
| 3851 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 3852 | page = f2fs_get_meta_page(sbi, start++); |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 3853 | if (IS_ERR(page)) |
| 3854 | return PTR_ERR(page); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3855 | kaddr = (unsigned char *)page_address(page); |
| 3856 | |
| 3857 | /* Step 1: restore nat cache */ |
| 3858 | seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA); |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 3859 | memcpy(seg_i->journal, kaddr, SUM_JOURNAL_SIZE); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3860 | |
| 3861 | /* Step 2: restore sit cache */ |
| 3862 | seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA); |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 3863 | memcpy(seg_i->journal, kaddr + SUM_JOURNAL_SIZE, SUM_JOURNAL_SIZE); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3864 | offset = 2 * SUM_JOURNAL_SIZE; |
| 3865 | |
| 3866 | /* Step 3: restore summary entries */ |
| 3867 | for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) { |
| 3868 | unsigned short blk_off; |
| 3869 | unsigned int segno; |
| 3870 | |
| 3871 | seg_i = CURSEG_I(sbi, i); |
| 3872 | segno = le32_to_cpu(ckpt->cur_data_segno[i]); |
| 3873 | blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]); |
| 3874 | seg_i->next_segno = segno; |
| 3875 | reset_curseg(sbi, i, 0); |
| 3876 | seg_i->alloc_type = ckpt->alloc_type[i]; |
| 3877 | seg_i->next_blkoff = blk_off; |
| 3878 | |
| 3879 | if (seg_i->alloc_type == SSR) |
| 3880 | blk_off = sbi->blocks_per_seg; |
| 3881 | |
| 3882 | for (j = 0; j < blk_off; j++) { |
| 3883 | struct f2fs_summary *s; |
Yi Zhuang | 5f029c0 | 2021-04-06 09:47:35 +0800 | [diff] [blame] | 3884 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3885 | s = (struct f2fs_summary *)(kaddr + offset); |
| 3886 | seg_i->sum_blk->entries[j] = *s; |
| 3887 | offset += SUMMARY_SIZE; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3888 | if (offset + SUMMARY_SIZE <= PAGE_SIZE - |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3889 | SUM_FOOTER_SIZE) |
| 3890 | continue; |
| 3891 | |
| 3892 | f2fs_put_page(page, 1); |
| 3893 | page = NULL; |
| 3894 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 3895 | page = f2fs_get_meta_page(sbi, start++); |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 3896 | if (IS_ERR(page)) |
| 3897 | return PTR_ERR(page); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3898 | kaddr = (unsigned char *)page_address(page); |
| 3899 | offset = 0; |
| 3900 | } |
| 3901 | } |
| 3902 | f2fs_put_page(page, 1); |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 3903 | return 0; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3904 | } |
| 3905 | |
| 3906 | static int read_normal_summaries(struct f2fs_sb_info *sbi, int type) |
| 3907 | { |
| 3908 | struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); |
| 3909 | struct f2fs_summary_block *sum; |
| 3910 | struct curseg_info *curseg; |
| 3911 | struct page *new; |
| 3912 | unsigned short blk_off; |
| 3913 | unsigned int segno = 0; |
| 3914 | block_t blk_addr = 0; |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 3915 | int err = 0; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3916 | |
| 3917 | /* get segment number and block addr */ |
| 3918 | if (IS_DATASEG(type)) { |
| 3919 | segno = le32_to_cpu(ckpt->cur_data_segno[type]); |
| 3920 | blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type - |
| 3921 | CURSEG_HOT_DATA]); |
Jaegeuk Kim | 119ee91 | 2015-01-29 11:45:33 -0800 | [diff] [blame] | 3922 | if (__exist_node_summaries(sbi)) |
Chao Yu | d0b9e42 | 2020-08-04 21:14:45 +0800 | [diff] [blame] | 3923 | blk_addr = sum_blk_addr(sbi, NR_CURSEG_PERSIST_TYPE, type); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3924 | else |
| 3925 | blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type); |
| 3926 | } else { |
| 3927 | segno = le32_to_cpu(ckpt->cur_node_segno[type - |
| 3928 | CURSEG_HOT_NODE]); |
| 3929 | blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type - |
| 3930 | CURSEG_HOT_NODE]); |
Jaegeuk Kim | 119ee91 | 2015-01-29 11:45:33 -0800 | [diff] [blame] | 3931 | if (__exist_node_summaries(sbi)) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3932 | blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE, |
| 3933 | type - CURSEG_HOT_NODE); |
| 3934 | else |
| 3935 | blk_addr = GET_SUM_BLOCK(sbi, segno); |
| 3936 | } |
| 3937 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 3938 | new = f2fs_get_meta_page(sbi, blk_addr); |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 3939 | if (IS_ERR(new)) |
| 3940 | return PTR_ERR(new); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3941 | sum = (struct f2fs_summary_block *)page_address(new); |
| 3942 | |
| 3943 | if (IS_NODESEG(type)) { |
Jaegeuk Kim | 119ee91 | 2015-01-29 11:45:33 -0800 | [diff] [blame] | 3944 | if (__exist_node_summaries(sbi)) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3945 | struct f2fs_summary *ns = &sum->entries[0]; |
| 3946 | int i; |
Yi Zhuang | 5f029c0 | 2021-04-06 09:47:35 +0800 | [diff] [blame] | 3947 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3948 | for (i = 0; i < sbi->blocks_per_seg; i++, ns++) { |
| 3949 | ns->version = 0; |
| 3950 | ns->ofs_in_node = 0; |
| 3951 | } |
| 3952 | } else { |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 3953 | err = f2fs_restore_node_summary(sbi, segno, sum); |
| 3954 | if (err) |
| 3955 | goto out; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3956 | } |
| 3957 | } |
| 3958 | |
| 3959 | /* set uncompleted segment to curseg */ |
| 3960 | curseg = CURSEG_I(sbi, type); |
| 3961 | mutex_lock(&curseg->curseg_mutex); |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 3962 | |
| 3963 | /* update journal info */ |
| 3964 | down_write(&curseg->journal_rwsem); |
| 3965 | memcpy(curseg->journal, &sum->journal, SUM_JOURNAL_SIZE); |
| 3966 | up_write(&curseg->journal_rwsem); |
| 3967 | |
| 3968 | memcpy(curseg->sum_blk->entries, sum->entries, SUM_ENTRY_SIZE); |
| 3969 | memcpy(&curseg->sum_blk->footer, &sum->footer, SUM_FOOTER_SIZE); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3970 | curseg->next_segno = segno; |
| 3971 | reset_curseg(sbi, type, 0); |
| 3972 | curseg->alloc_type = ckpt->alloc_type[type]; |
| 3973 | curseg->next_blkoff = blk_off; |
| 3974 | mutex_unlock(&curseg->curseg_mutex); |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 3975 | out: |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3976 | f2fs_put_page(new, 1); |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 3977 | return err; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3978 | } |
| 3979 | |
| 3980 | static int restore_curseg_summaries(struct f2fs_sb_info *sbi) |
| 3981 | { |
Jin Qian | 21d3f8e | 2017-06-01 11:18:30 -0700 | [diff] [blame] | 3982 | struct f2fs_journal *sit_j = CURSEG_I(sbi, CURSEG_COLD_DATA)->journal; |
| 3983 | struct f2fs_journal *nat_j = CURSEG_I(sbi, CURSEG_HOT_DATA)->journal; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3984 | int type = CURSEG_HOT_DATA; |
Chao Yu | e4fc5fb | 2014-03-17 16:36:24 +0800 | [diff] [blame] | 3985 | int err; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3986 | |
Chao Yu | aaec2b1 | 2016-09-20 11:04:18 +0800 | [diff] [blame] | 3987 | if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG)) { |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 3988 | int npages = f2fs_npages_for_summary_flush(sbi, true); |
Chao Yu | 3fa06d7 | 2014-12-09 14:21:46 +0800 | [diff] [blame] | 3989 | |
| 3990 | if (npages >= 2) |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 3991 | f2fs_ra_meta_pages(sbi, start_sum_block(sbi), npages, |
Chao Yu | 26879fb | 2015-10-12 17:05:59 +0800 | [diff] [blame] | 3992 | META_CP, true); |
Chao Yu | 3fa06d7 | 2014-12-09 14:21:46 +0800 | [diff] [blame] | 3993 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3994 | /* restore for compacted data summary */ |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 3995 | err = read_compacted_summaries(sbi); |
| 3996 | if (err) |
| 3997 | return err; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3998 | type = CURSEG_HOT_NODE; |
| 3999 | } |
| 4000 | |
Jaegeuk Kim | 119ee91 | 2015-01-29 11:45:33 -0800 | [diff] [blame] | 4001 | if (__exist_node_summaries(sbi)) |
Chao Yu | d0b9e42 | 2020-08-04 21:14:45 +0800 | [diff] [blame] | 4002 | f2fs_ra_meta_pages(sbi, |
| 4003 | sum_blk_addr(sbi, NR_CURSEG_PERSIST_TYPE, type), |
| 4004 | NR_CURSEG_PERSIST_TYPE - type, META_CP, true); |
Chao Yu | 3fa06d7 | 2014-12-09 14:21:46 +0800 | [diff] [blame] | 4005 | |
Chao Yu | e4fc5fb | 2014-03-17 16:36:24 +0800 | [diff] [blame] | 4006 | for (; type <= CURSEG_COLD_NODE; type++) { |
| 4007 | err = read_normal_summaries(sbi, type); |
| 4008 | if (err) |
| 4009 | return err; |
| 4010 | } |
| 4011 | |
Jin Qian | 21d3f8e | 2017-06-01 11:18:30 -0700 | [diff] [blame] | 4012 | /* sanity check for summary blocks */ |
| 4013 | if (nats_in_cursum(nat_j) > NAT_JOURNAL_ENTRIES || |
Sahitya Tummala | 9227d52 | 2019-05-23 09:49:17 +0530 | [diff] [blame] | 4014 | sits_in_cursum(sit_j) > SIT_JOURNAL_ENTRIES) { |
Joe Perches | 833dcd3 | 2021-05-26 13:05:36 -0700 | [diff] [blame] | 4015 | f2fs_err(sbi, "invalid journal entries nats %u sits %u", |
Joe Perches | dcbb4c1 | 2019-06-18 17:48:42 +0800 | [diff] [blame] | 4016 | nats_in_cursum(nat_j), sits_in_cursum(sit_j)); |
Jin Qian | 21d3f8e | 2017-06-01 11:18:30 -0700 | [diff] [blame] | 4017 | return -EINVAL; |
Sahitya Tummala | 9227d52 | 2019-05-23 09:49:17 +0530 | [diff] [blame] | 4018 | } |
Jin Qian | 21d3f8e | 2017-06-01 11:18:30 -0700 | [diff] [blame] | 4019 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4020 | return 0; |
| 4021 | } |
| 4022 | |
| 4023 | static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr) |
| 4024 | { |
| 4025 | struct page *page; |
| 4026 | unsigned char *kaddr; |
| 4027 | struct f2fs_summary *summary; |
| 4028 | struct curseg_info *seg_i; |
| 4029 | int written_size = 0; |
| 4030 | int i, j; |
| 4031 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 4032 | page = f2fs_grab_meta_page(sbi, blkaddr++); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4033 | kaddr = (unsigned char *)page_address(page); |
Chao Yu | 81114ba | 2018-04-09 20:25:06 +0800 | [diff] [blame] | 4034 | memset(kaddr, 0, PAGE_SIZE); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4035 | |
| 4036 | /* Step 1: write nat cache */ |
| 4037 | seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA); |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 4038 | memcpy(kaddr, seg_i->journal, SUM_JOURNAL_SIZE); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4039 | written_size += SUM_JOURNAL_SIZE; |
| 4040 | |
| 4041 | /* Step 2: write sit cache */ |
| 4042 | seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA); |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 4043 | memcpy(kaddr + written_size, seg_i->journal, SUM_JOURNAL_SIZE); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4044 | written_size += SUM_JOURNAL_SIZE; |
| 4045 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4046 | /* Step 3: write summary entries */ |
| 4047 | for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) { |
| 4048 | unsigned short blkoff; |
Yi Zhuang | 5f029c0 | 2021-04-06 09:47:35 +0800 | [diff] [blame] | 4049 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4050 | seg_i = CURSEG_I(sbi, i); |
| 4051 | if (sbi->ckpt->alloc_type[i] == SSR) |
| 4052 | blkoff = sbi->blocks_per_seg; |
| 4053 | else |
| 4054 | blkoff = curseg_blkoff(sbi, i); |
| 4055 | |
| 4056 | for (j = 0; j < blkoff; j++) { |
| 4057 | if (!page) { |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 4058 | page = f2fs_grab_meta_page(sbi, blkaddr++); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4059 | kaddr = (unsigned char *)page_address(page); |
Chao Yu | 81114ba | 2018-04-09 20:25:06 +0800 | [diff] [blame] | 4060 | memset(kaddr, 0, PAGE_SIZE); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4061 | written_size = 0; |
| 4062 | } |
| 4063 | summary = (struct f2fs_summary *)(kaddr + written_size); |
| 4064 | *summary = seg_i->sum_blk->entries[j]; |
| 4065 | written_size += SUMMARY_SIZE; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4066 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4067 | if (written_size + SUMMARY_SIZE <= PAGE_SIZE - |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4068 | SUM_FOOTER_SIZE) |
| 4069 | continue; |
| 4070 | |
Chao Yu | e8d61a7 | 2013-10-24 15:08:28 +0800 | [diff] [blame] | 4071 | set_page_dirty(page); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4072 | f2fs_put_page(page, 1); |
| 4073 | page = NULL; |
| 4074 | } |
| 4075 | } |
Chao Yu | e8d61a7 | 2013-10-24 15:08:28 +0800 | [diff] [blame] | 4076 | if (page) { |
| 4077 | set_page_dirty(page); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4078 | f2fs_put_page(page, 1); |
Chao Yu | e8d61a7 | 2013-10-24 15:08:28 +0800 | [diff] [blame] | 4079 | } |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4080 | } |
| 4081 | |
| 4082 | static void write_normal_summaries(struct f2fs_sb_info *sbi, |
| 4083 | block_t blkaddr, int type) |
| 4084 | { |
| 4085 | int i, end; |
Yi Zhuang | 5f029c0 | 2021-04-06 09:47:35 +0800 | [diff] [blame] | 4086 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4087 | if (IS_DATASEG(type)) |
| 4088 | end = type + NR_CURSEG_DATA_TYPE; |
| 4089 | else |
| 4090 | end = type + NR_CURSEG_NODE_TYPE; |
| 4091 | |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 4092 | for (i = type; i < end; i++) |
| 4093 | write_current_sum_page(sbi, i, blkaddr + (i - type)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4094 | } |
| 4095 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 4096 | void f2fs_write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4097 | { |
Chao Yu | aaec2b1 | 2016-09-20 11:04:18 +0800 | [diff] [blame] | 4098 | if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG)) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4099 | write_compacted_summaries(sbi, start_blk); |
| 4100 | else |
| 4101 | write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA); |
| 4102 | } |
| 4103 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 4104 | void f2fs_write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4105 | { |
Jaegeuk Kim | 119ee91 | 2015-01-29 11:45:33 -0800 | [diff] [blame] | 4106 | write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4107 | } |
| 4108 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 4109 | int f2fs_lookup_journal_in_cursum(struct f2fs_journal *journal, int type, |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4110 | unsigned int val, int alloc) |
| 4111 | { |
| 4112 | int i; |
| 4113 | |
| 4114 | if (type == NAT_JOURNAL) { |
Chao Yu | dfc08a1 | 2016-02-14 18:50:40 +0800 | [diff] [blame] | 4115 | for (i = 0; i < nats_in_cursum(journal); i++) { |
| 4116 | if (le32_to_cpu(nid_in_journal(journal, i)) == val) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4117 | return i; |
| 4118 | } |
Chao Yu | dfc08a1 | 2016-02-14 18:50:40 +0800 | [diff] [blame] | 4119 | if (alloc && __has_cursum_space(journal, 1, NAT_JOURNAL)) |
| 4120 | return update_nats_in_cursum(journal, 1); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4121 | } else if (type == SIT_JOURNAL) { |
Chao Yu | dfc08a1 | 2016-02-14 18:50:40 +0800 | [diff] [blame] | 4122 | for (i = 0; i < sits_in_cursum(journal); i++) |
| 4123 | if (le32_to_cpu(segno_in_journal(journal, i)) == val) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4124 | return i; |
Chao Yu | dfc08a1 | 2016-02-14 18:50:40 +0800 | [diff] [blame] | 4125 | if (alloc && __has_cursum_space(journal, 1, SIT_JOURNAL)) |
| 4126 | return update_sits_in_cursum(journal, 1); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4127 | } |
| 4128 | return -1; |
| 4129 | } |
| 4130 | |
| 4131 | static struct page *get_current_sit_page(struct f2fs_sb_info *sbi, |
| 4132 | unsigned int segno) |
| 4133 | { |
Jaegeuk Kim | 86f3360 | 2020-10-02 14:17:35 -0700 | [diff] [blame] | 4134 | return f2fs_get_meta_page(sbi, current_sit_addr(sbi, segno)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4135 | } |
| 4136 | |
| 4137 | static struct page *get_next_sit_page(struct f2fs_sb_info *sbi, |
| 4138 | unsigned int start) |
| 4139 | { |
| 4140 | struct sit_info *sit_i = SIT_I(sbi); |
Yunlei He | 068c3cd | 2018-01-25 17:27:11 +0800 | [diff] [blame] | 4141 | struct page *page; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4142 | pgoff_t src_off, dst_off; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4143 | |
| 4144 | src_off = current_sit_addr(sbi, start); |
| 4145 | dst_off = next_sit_addr(sbi, src_off); |
| 4146 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 4147 | page = f2fs_grab_meta_page(sbi, dst_off); |
Yunlei He | 068c3cd | 2018-01-25 17:27:11 +0800 | [diff] [blame] | 4148 | seg_info_to_sit_page(sbi, page, start); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4149 | |
Yunlei He | 068c3cd | 2018-01-25 17:27:11 +0800 | [diff] [blame] | 4150 | set_page_dirty(page); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4151 | set_to_next_sit(sit_i, start); |
| 4152 | |
Yunlei He | 068c3cd | 2018-01-25 17:27:11 +0800 | [diff] [blame] | 4153 | return page; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4154 | } |
| 4155 | |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 4156 | static struct sit_entry_set *grab_sit_entry_set(void) |
| 4157 | { |
| 4158 | struct sit_entry_set *ses = |
Chao Yu | 3241057 | 2021-08-09 08:24:48 +0800 | [diff] [blame] | 4159 | f2fs_kmem_cache_alloc(sit_entry_set_slab, |
| 4160 | GFP_NOFS, true, NULL); |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 4161 | |
| 4162 | ses->entry_cnt = 0; |
| 4163 | INIT_LIST_HEAD(&ses->set_list); |
| 4164 | return ses; |
| 4165 | } |
| 4166 | |
| 4167 | static void release_sit_entry_set(struct sit_entry_set *ses) |
| 4168 | { |
| 4169 | list_del(&ses->set_list); |
| 4170 | kmem_cache_free(sit_entry_set_slab, ses); |
| 4171 | } |
| 4172 | |
| 4173 | static void adjust_sit_entry_set(struct sit_entry_set *ses, |
| 4174 | struct list_head *head) |
| 4175 | { |
| 4176 | struct sit_entry_set *next = ses; |
| 4177 | |
| 4178 | if (list_is_last(&ses->set_list, head)) |
| 4179 | return; |
| 4180 | |
| 4181 | list_for_each_entry_continue(next, head, set_list) |
| 4182 | if (ses->entry_cnt <= next->entry_cnt) |
| 4183 | break; |
| 4184 | |
| 4185 | list_move_tail(&ses->set_list, &next->set_list); |
| 4186 | } |
| 4187 | |
| 4188 | static void add_sit_entry(unsigned int segno, struct list_head *head) |
| 4189 | { |
| 4190 | struct sit_entry_set *ses; |
| 4191 | unsigned int start_segno = START_SEGNO(segno); |
| 4192 | |
| 4193 | list_for_each_entry(ses, head, set_list) { |
| 4194 | if (ses->start_segno == start_segno) { |
| 4195 | ses->entry_cnt++; |
| 4196 | adjust_sit_entry_set(ses, head); |
| 4197 | return; |
| 4198 | } |
| 4199 | } |
| 4200 | |
| 4201 | ses = grab_sit_entry_set(); |
| 4202 | |
| 4203 | ses->start_segno = start_segno; |
| 4204 | ses->entry_cnt++; |
| 4205 | list_add(&ses->set_list, head); |
| 4206 | } |
| 4207 | |
| 4208 | static void add_sits_in_set(struct f2fs_sb_info *sbi) |
| 4209 | { |
| 4210 | struct f2fs_sm_info *sm_info = SM_I(sbi); |
| 4211 | struct list_head *set_list = &sm_info->sit_entry_set; |
| 4212 | unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap; |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 4213 | unsigned int segno; |
| 4214 | |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 4215 | for_each_set_bit(segno, bitmap, MAIN_SEGS(sbi)) |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 4216 | add_sit_entry(segno, set_list); |
| 4217 | } |
| 4218 | |
| 4219 | static void remove_sits_in_journal(struct f2fs_sb_info *sbi) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4220 | { |
| 4221 | struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA); |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 4222 | struct f2fs_journal *journal = curseg->journal; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4223 | int i; |
| 4224 | |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 4225 | down_write(&curseg->journal_rwsem); |
Chao Yu | dfc08a1 | 2016-02-14 18:50:40 +0800 | [diff] [blame] | 4226 | for (i = 0; i < sits_in_cursum(journal); i++) { |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 4227 | unsigned int segno; |
| 4228 | bool dirtied; |
| 4229 | |
Chao Yu | dfc08a1 | 2016-02-14 18:50:40 +0800 | [diff] [blame] | 4230 | segno = le32_to_cpu(segno_in_journal(journal, i)); |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 4231 | dirtied = __mark_sit_entry_dirty(sbi, segno); |
| 4232 | |
| 4233 | if (!dirtied) |
| 4234 | add_sit_entry(segno, &SM_I(sbi)->sit_entry_set); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4235 | } |
Chao Yu | dfc08a1 | 2016-02-14 18:50:40 +0800 | [diff] [blame] | 4236 | update_sits_in_cursum(journal, -i); |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 4237 | up_write(&curseg->journal_rwsem); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4238 | } |
| 4239 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 4240 | /* |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4241 | * CP calls this function, which flushes SIT entries including sit_journal, |
| 4242 | * and moves prefree segs to free segs. |
| 4243 | */ |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 4244 | void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4245 | { |
| 4246 | struct sit_info *sit_i = SIT_I(sbi); |
| 4247 | unsigned long *bitmap = sit_i->dirty_sentries_bitmap; |
| 4248 | struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA); |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 4249 | struct f2fs_journal *journal = curseg->journal; |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 4250 | struct sit_entry_set *ses, *tmp; |
| 4251 | struct list_head *head = &SM_I(sbi)->sit_entry_set; |
Qiuyang Sun | 04f0b2e | 2019-06-05 11:33:25 +0800 | [diff] [blame] | 4252 | bool to_journal = !is_sbi_flag_set(sbi, SBI_IS_RESIZEFS); |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 4253 | struct seg_entry *se; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4254 | |
Chao Yu | 3d26fa6 | 2017-10-30 17:49:53 +0800 | [diff] [blame] | 4255 | down_write(&sit_i->sentry_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4256 | |
Wanpeng Li | 2b11a74 | 2015-02-27 16:52:50 +0800 | [diff] [blame] | 4257 | if (!sit_i->dirty_sentries) |
| 4258 | goto out; |
| 4259 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4260 | /* |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 4261 | * add and account sit entries of dirty bitmap in sit entry |
| 4262 | * set temporarily |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4263 | */ |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 4264 | add_sits_in_set(sbi); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4265 | |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 4266 | /* |
| 4267 | * if there are no enough space in journal to store dirty sit |
| 4268 | * entries, remove all entries from journal and add and account |
| 4269 | * them in sit entry set. |
| 4270 | */ |
Qiuyang Sun | 04f0b2e | 2019-06-05 11:33:25 +0800 | [diff] [blame] | 4271 | if (!__has_cursum_space(journal, sit_i->dirty_sentries, SIT_JOURNAL) || |
| 4272 | !to_journal) |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 4273 | remove_sits_in_journal(sbi); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4274 | |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 4275 | /* |
| 4276 | * there are two steps to flush sit entries: |
| 4277 | * #1, flush sit entries to journal in current cold data summary block. |
| 4278 | * #2, flush sit entries to sit page. |
| 4279 | */ |
| 4280 | list_for_each_entry_safe(ses, tmp, head, set_list) { |
Jaegeuk Kim | 4a257ed | 2014-10-16 11:43:30 -0700 | [diff] [blame] | 4281 | struct page *page = NULL; |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 4282 | struct f2fs_sit_block *raw_sit = NULL; |
| 4283 | unsigned int start_segno = ses->start_segno; |
| 4284 | unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK, |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 4285 | (unsigned long)MAIN_SEGS(sbi)); |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 4286 | unsigned int segno = start_segno; |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 4287 | |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 4288 | if (to_journal && |
Chao Yu | dfc08a1 | 2016-02-14 18:50:40 +0800 | [diff] [blame] | 4289 | !__has_cursum_space(journal, ses->entry_cnt, SIT_JOURNAL)) |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 4290 | to_journal = false; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4291 | |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 4292 | if (to_journal) { |
| 4293 | down_write(&curseg->journal_rwsem); |
| 4294 | } else { |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 4295 | page = get_next_sit_page(sbi, start_segno); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4296 | raw_sit = page_address(page); |
| 4297 | } |
| 4298 | |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 4299 | /* flush dirty sit entries in region of current sit set */ |
| 4300 | for_each_set_bit_from(segno, bitmap, end) { |
| 4301 | int offset, sit_offset; |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 4302 | |
| 4303 | se = get_seg_entry(sbi, segno); |
Zhikang Zhang | 56b07e7 | 2018-04-09 04:28:41 +0800 | [diff] [blame] | 4304 | #ifdef CONFIG_F2FS_CHECK_FS |
| 4305 | if (memcmp(se->cur_valid_map, se->cur_valid_map_mir, |
| 4306 | SIT_VBLOCK_MAP_SIZE)) |
| 4307 | f2fs_bug_on(sbi, 1); |
| 4308 | #endif |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 4309 | |
| 4310 | /* add discard candidates */ |
Chao Yu | c473f1a | 2017-04-27 20:40:39 +0800 | [diff] [blame] | 4311 | if (!(cpc->reason & CP_DISCARD)) { |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 4312 | cpc->trim_start = segno; |
Jaegeuk Kim | 25290fa | 2016-12-29 22:06:15 -0800 | [diff] [blame] | 4313 | add_discard_addrs(sbi, cpc, false); |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 4314 | } |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 4315 | |
| 4316 | if (to_journal) { |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 4317 | offset = f2fs_lookup_journal_in_cursum(journal, |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 4318 | SIT_JOURNAL, segno, 1); |
| 4319 | f2fs_bug_on(sbi, offset < 0); |
Chao Yu | dfc08a1 | 2016-02-14 18:50:40 +0800 | [diff] [blame] | 4320 | segno_in_journal(journal, offset) = |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 4321 | cpu_to_le32(segno); |
| 4322 | seg_info_to_raw_sit(se, |
Chao Yu | dfc08a1 | 2016-02-14 18:50:40 +0800 | [diff] [blame] | 4323 | &sit_in_journal(journal, offset)); |
Zhikang Zhang | 56b07e7 | 2018-04-09 04:28:41 +0800 | [diff] [blame] | 4324 | check_block_count(sbi, segno, |
| 4325 | &sit_in_journal(journal, offset)); |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 4326 | } else { |
| 4327 | sit_offset = SIT_ENTRY_OFFSET(sit_i, segno); |
| 4328 | seg_info_to_raw_sit(se, |
| 4329 | &raw_sit->entries[sit_offset]); |
Zhikang Zhang | 56b07e7 | 2018-04-09 04:28:41 +0800 | [diff] [blame] | 4330 | check_block_count(sbi, segno, |
| 4331 | &raw_sit->entries[sit_offset]); |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 4332 | } |
| 4333 | |
| 4334 | __clear_bit(segno, bitmap); |
| 4335 | sit_i->dirty_sentries--; |
| 4336 | ses->entry_cnt--; |
| 4337 | } |
| 4338 | |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 4339 | if (to_journal) |
| 4340 | up_write(&curseg->journal_rwsem); |
| 4341 | else |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 4342 | f2fs_put_page(page, 1); |
| 4343 | |
| 4344 | f2fs_bug_on(sbi, ses->entry_cnt); |
| 4345 | release_sit_entry_set(ses); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4346 | } |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 4347 | |
| 4348 | f2fs_bug_on(sbi, !list_empty(head)); |
| 4349 | f2fs_bug_on(sbi, sit_i->dirty_sentries); |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 4350 | out: |
Chao Yu | c473f1a | 2017-04-27 20:40:39 +0800 | [diff] [blame] | 4351 | if (cpc->reason & CP_DISCARD) { |
Yunlei He | 650d3c4 | 2016-12-22 11:46:24 +0800 | [diff] [blame] | 4352 | __u64 trim_start = cpc->trim_start; |
| 4353 | |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 4354 | for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++) |
Jaegeuk Kim | 25290fa | 2016-12-29 22:06:15 -0800 | [diff] [blame] | 4355 | add_discard_addrs(sbi, cpc, false); |
Yunlei He | 650d3c4 | 2016-12-22 11:46:24 +0800 | [diff] [blame] | 4356 | |
| 4357 | cpc->trim_start = trim_start; |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 4358 | } |
Chao Yu | 3d26fa6 | 2017-10-30 17:49:53 +0800 | [diff] [blame] | 4359 | up_write(&sit_i->sentry_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4360 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4361 | set_prefree_as_free_segments(sbi); |
| 4362 | } |
| 4363 | |
| 4364 | static int build_sit_info(struct f2fs_sb_info *sbi) |
| 4365 | { |
| 4366 | struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4367 | struct sit_info *sit_i; |
| 4368 | unsigned int sit_segs, start; |
Chao Yu | 2fde3dd | 2019-07-26 15:41:20 +0800 | [diff] [blame] | 4369 | char *src_bitmap, *bitmap; |
Sahitya Tummala | bbf9f7d | 2019-08-07 19:10:32 +0530 | [diff] [blame] | 4370 | unsigned int bitmap_size, main_bitmap_size, sit_bitmap_size; |
Chao Yu | 4f99326 | 2021-08-03 08:15:43 +0800 | [diff] [blame] | 4371 | unsigned int discard_map = f2fs_block_unit_discard(sbi) ? 1 : 0; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4372 | |
| 4373 | /* allocate memory for SIT information */ |
Chao Yu | acbf054 | 2017-11-30 19:28:17 +0800 | [diff] [blame] | 4374 | sit_i = f2fs_kzalloc(sbi, sizeof(struct sit_info), GFP_KERNEL); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4375 | if (!sit_i) |
| 4376 | return -ENOMEM; |
| 4377 | |
| 4378 | SM_I(sbi)->sit_info = sit_i; |
| 4379 | |
Kees Cook | 9d2a789 | 2018-06-12 14:28:35 -0700 | [diff] [blame] | 4380 | sit_i->sentries = |
| 4381 | f2fs_kvzalloc(sbi, array_size(sizeof(struct seg_entry), |
| 4382 | MAIN_SEGS(sbi)), |
| 4383 | GFP_KERNEL); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4384 | if (!sit_i->sentries) |
| 4385 | return -ENOMEM; |
| 4386 | |
Sahitya Tummala | bbf9f7d | 2019-08-07 19:10:32 +0530 | [diff] [blame] | 4387 | main_bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi)); |
| 4388 | sit_i->dirty_sentries_bitmap = f2fs_kvzalloc(sbi, main_bitmap_size, |
Chao Yu | 628b3d1 | 2017-11-30 19:28:18 +0800 | [diff] [blame] | 4389 | GFP_KERNEL); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4390 | if (!sit_i->dirty_sentries_bitmap) |
| 4391 | return -ENOMEM; |
| 4392 | |
Chao Yu | 2fde3dd | 2019-07-26 15:41:20 +0800 | [diff] [blame] | 4393 | #ifdef CONFIG_F2FS_CHECK_FS |
Chao Yu | 4f99326 | 2021-08-03 08:15:43 +0800 | [diff] [blame] | 4394 | bitmap_size = MAIN_SEGS(sbi) * SIT_VBLOCK_MAP_SIZE * (3 + discard_map); |
Chao Yu | 2fde3dd | 2019-07-26 15:41:20 +0800 | [diff] [blame] | 4395 | #else |
Chao Yu | 4f99326 | 2021-08-03 08:15:43 +0800 | [diff] [blame] | 4396 | bitmap_size = MAIN_SEGS(sbi) * SIT_VBLOCK_MAP_SIZE * (2 + discard_map); |
Chao Yu | 2fde3dd | 2019-07-26 15:41:20 +0800 | [diff] [blame] | 4397 | #endif |
| 4398 | sit_i->bitmap = f2fs_kvzalloc(sbi, bitmap_size, GFP_KERNEL); |
| 4399 | if (!sit_i->bitmap) |
| 4400 | return -ENOMEM; |
| 4401 | |
| 4402 | bitmap = sit_i->bitmap; |
| 4403 | |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 4404 | for (start = 0; start < MAIN_SEGS(sbi); start++) { |
Chao Yu | 2fde3dd | 2019-07-26 15:41:20 +0800 | [diff] [blame] | 4405 | sit_i->sentries[start].cur_valid_map = bitmap; |
| 4406 | bitmap += SIT_VBLOCK_MAP_SIZE; |
| 4407 | |
| 4408 | sit_i->sentries[start].ckpt_valid_map = bitmap; |
| 4409 | bitmap += SIT_VBLOCK_MAP_SIZE; |
Jaegeuk Kim | 3e02574 | 2016-08-02 10:56:40 -0700 | [diff] [blame] | 4410 | |
Chao Yu | 355e789 | 2017-01-07 18:51:01 +0800 | [diff] [blame] | 4411 | #ifdef CONFIG_F2FS_CHECK_FS |
Chao Yu | 2fde3dd | 2019-07-26 15:41:20 +0800 | [diff] [blame] | 4412 | sit_i->sentries[start].cur_valid_map_mir = bitmap; |
| 4413 | bitmap += SIT_VBLOCK_MAP_SIZE; |
Chao Yu | 355e789 | 2017-01-07 18:51:01 +0800 | [diff] [blame] | 4414 | #endif |
| 4415 | |
Chao Yu | 4f99326 | 2021-08-03 08:15:43 +0800 | [diff] [blame] | 4416 | if (discard_map) { |
| 4417 | sit_i->sentries[start].discard_map = bitmap; |
| 4418 | bitmap += SIT_VBLOCK_MAP_SIZE; |
| 4419 | } |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4420 | } |
| 4421 | |
Chao Yu | acbf054 | 2017-11-30 19:28:17 +0800 | [diff] [blame] | 4422 | sit_i->tmp_map = f2fs_kzalloc(sbi, SIT_VBLOCK_MAP_SIZE, GFP_KERNEL); |
Jaegeuk Kim | 60a3b78 | 2015-02-10 16:44:29 -0800 | [diff] [blame] | 4423 | if (!sit_i->tmp_map) |
| 4424 | return -ENOMEM; |
| 4425 | |
Chao Yu | 2c70c5e | 2018-10-24 18:37:26 +0800 | [diff] [blame] | 4426 | if (__is_large_section(sbi)) { |
Kees Cook | 9d2a789 | 2018-06-12 14:28:35 -0700 | [diff] [blame] | 4427 | sit_i->sec_entries = |
| 4428 | f2fs_kvzalloc(sbi, array_size(sizeof(struct sec_entry), |
| 4429 | MAIN_SECS(sbi)), |
| 4430 | GFP_KERNEL); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4431 | if (!sit_i->sec_entries) |
| 4432 | return -ENOMEM; |
| 4433 | } |
| 4434 | |
| 4435 | /* get information related with SIT */ |
| 4436 | sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1; |
| 4437 | |
| 4438 | /* setup SIT bitmap from ckeckpoint pack */ |
Sahitya Tummala | bbf9f7d | 2019-08-07 19:10:32 +0530 | [diff] [blame] | 4439 | sit_bitmap_size = __bitmap_size(sbi, SIT_BITMAP); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4440 | src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP); |
| 4441 | |
Sahitya Tummala | bbf9f7d | 2019-08-07 19:10:32 +0530 | [diff] [blame] | 4442 | sit_i->sit_bitmap = kmemdup(src_bitmap, sit_bitmap_size, GFP_KERNEL); |
Chao Yu | ae27d62 | 2017-01-07 18:52:34 +0800 | [diff] [blame] | 4443 | if (!sit_i->sit_bitmap) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4444 | return -ENOMEM; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4445 | |
Chao Yu | ae27d62 | 2017-01-07 18:52:34 +0800 | [diff] [blame] | 4446 | #ifdef CONFIG_F2FS_CHECK_FS |
Sahitya Tummala | bbf9f7d | 2019-08-07 19:10:32 +0530 | [diff] [blame] | 4447 | sit_i->sit_bitmap_mir = kmemdup(src_bitmap, |
| 4448 | sit_bitmap_size, GFP_KERNEL); |
Chao Yu | ae27d62 | 2017-01-07 18:52:34 +0800 | [diff] [blame] | 4449 | if (!sit_i->sit_bitmap_mir) |
| 4450 | return -ENOMEM; |
Sahitya Tummala | bbf9f7d | 2019-08-07 19:10:32 +0530 | [diff] [blame] | 4451 | |
| 4452 | sit_i->invalid_segmap = f2fs_kvzalloc(sbi, |
| 4453 | main_bitmap_size, GFP_KERNEL); |
| 4454 | if (!sit_i->invalid_segmap) |
| 4455 | return -ENOMEM; |
Chao Yu | ae27d62 | 2017-01-07 18:52:34 +0800 | [diff] [blame] | 4456 | #endif |
| 4457 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4458 | /* init SIT information */ |
| 4459 | sit_i->s_ops = &default_salloc_ops; |
| 4460 | |
| 4461 | sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr); |
| 4462 | sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg; |
Jaegeuk Kim | c79b7ff | 2016-11-14 18:20:10 -0800 | [diff] [blame] | 4463 | sit_i->written_valid_blocks = 0; |
Sahitya Tummala | bbf9f7d | 2019-08-07 19:10:32 +0530 | [diff] [blame] | 4464 | sit_i->bitmap_size = sit_bitmap_size; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4465 | sit_i->dirty_sentries = 0; |
| 4466 | sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK; |
| 4467 | sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time); |
Jaegeuk Kim | a7e679b | 2020-02-25 19:08:16 -0800 | [diff] [blame] | 4468 | sit_i->mounted_time = ktime_get_boottime_seconds(); |
Chao Yu | 3d26fa6 | 2017-10-30 17:49:53 +0800 | [diff] [blame] | 4469 | init_rwsem(&sit_i->sentry_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4470 | return 0; |
| 4471 | } |
| 4472 | |
| 4473 | static int build_free_segmap(struct f2fs_sb_info *sbi) |
| 4474 | { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4475 | struct free_segmap_info *free_i; |
| 4476 | unsigned int bitmap_size, sec_bitmap_size; |
| 4477 | |
| 4478 | /* allocate memory for free segmap information */ |
Chao Yu | acbf054 | 2017-11-30 19:28:17 +0800 | [diff] [blame] | 4479 | free_i = f2fs_kzalloc(sbi, sizeof(struct free_segmap_info), GFP_KERNEL); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4480 | if (!free_i) |
| 4481 | return -ENOMEM; |
| 4482 | |
| 4483 | SM_I(sbi)->free_info = free_i; |
| 4484 | |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 4485 | bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi)); |
Chao Yu | 628b3d1 | 2017-11-30 19:28:18 +0800 | [diff] [blame] | 4486 | free_i->free_segmap = f2fs_kvmalloc(sbi, bitmap_size, GFP_KERNEL); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4487 | if (!free_i->free_segmap) |
| 4488 | return -ENOMEM; |
| 4489 | |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 4490 | sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi)); |
Chao Yu | 628b3d1 | 2017-11-30 19:28:18 +0800 | [diff] [blame] | 4491 | free_i->free_secmap = f2fs_kvmalloc(sbi, sec_bitmap_size, GFP_KERNEL); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4492 | if (!free_i->free_secmap) |
| 4493 | return -ENOMEM; |
| 4494 | |
| 4495 | /* set all segments as dirty temporarily */ |
| 4496 | memset(free_i->free_segmap, 0xff, bitmap_size); |
| 4497 | memset(free_i->free_secmap, 0xff, sec_bitmap_size); |
| 4498 | |
| 4499 | /* init free segmap information */ |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 4500 | free_i->start_segno = GET_SEGNO_FROM_SEG0(sbi, MAIN_BLKADDR(sbi)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4501 | free_i->free_segments = 0; |
| 4502 | free_i->free_sections = 0; |
Chao Yu | 1a118cc | 2015-02-11 18:20:38 +0800 | [diff] [blame] | 4503 | spin_lock_init(&free_i->segmap_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4504 | return 0; |
| 4505 | } |
| 4506 | |
| 4507 | static int build_curseg(struct f2fs_sb_info *sbi) |
| 4508 | { |
Namjae Jeon | 1042d60 | 2012-12-01 10:56:13 +0900 | [diff] [blame] | 4509 | struct curseg_info *array; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4510 | int i; |
| 4511 | |
Chao Yu | d0b9e42 | 2020-08-04 21:14:45 +0800 | [diff] [blame] | 4512 | array = f2fs_kzalloc(sbi, array_size(NR_CURSEG_TYPE, |
| 4513 | sizeof(*array)), GFP_KERNEL); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4514 | if (!array) |
| 4515 | return -ENOMEM; |
| 4516 | |
| 4517 | SM_I(sbi)->curseg_array = array; |
| 4518 | |
Chao Yu | d0b9e42 | 2020-08-04 21:14:45 +0800 | [diff] [blame] | 4519 | for (i = 0; i < NO_CHECK_TYPE; i++) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4520 | mutex_init(&array[i].curseg_mutex); |
Chao Yu | acbf054 | 2017-11-30 19:28:17 +0800 | [diff] [blame] | 4521 | array[i].sum_blk = f2fs_kzalloc(sbi, PAGE_SIZE, GFP_KERNEL); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4522 | if (!array[i].sum_blk) |
| 4523 | return -ENOMEM; |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 4524 | init_rwsem(&array[i].journal_rwsem); |
Chao Yu | acbf054 | 2017-11-30 19:28:17 +0800 | [diff] [blame] | 4525 | array[i].journal = f2fs_kzalloc(sbi, |
| 4526 | sizeof(struct f2fs_journal), GFP_KERNEL); |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 4527 | if (!array[i].journal) |
| 4528 | return -ENOMEM; |
Chao Yu | d0b9e42 | 2020-08-04 21:14:45 +0800 | [diff] [blame] | 4529 | if (i < NR_PERSISTENT_LOG) |
| 4530 | array[i].seg_type = CURSEG_HOT_DATA + i; |
| 4531 | else if (i == CURSEG_COLD_DATA_PINNED) |
| 4532 | array[i].seg_type = CURSEG_COLD_DATA; |
Chao Yu | 093749e | 2020-08-04 21:14:49 +0800 | [diff] [blame] | 4533 | else if (i == CURSEG_ALL_DATA_ATGC) |
| 4534 | array[i].seg_type = CURSEG_COLD_DATA; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4535 | array[i].segno = NULL_SEGNO; |
| 4536 | array[i].next_blkoff = 0; |
Chao Yu | d0b9e42 | 2020-08-04 21:14:45 +0800 | [diff] [blame] | 4537 | array[i].inited = false; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4538 | } |
| 4539 | return restore_curseg_summaries(sbi); |
| 4540 | } |
| 4541 | |
Jaegeuk Kim | c39a1b3 | 2017-12-19 19:16:34 -0800 | [diff] [blame] | 4542 | static int build_sit_entries(struct f2fs_sb_info *sbi) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4543 | { |
| 4544 | struct sit_info *sit_i = SIT_I(sbi); |
| 4545 | struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA); |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 4546 | struct f2fs_journal *journal = curseg->journal; |
Yunlei He | 9c09404 | 2016-09-24 12:29:18 +0800 | [diff] [blame] | 4547 | struct seg_entry *se; |
| 4548 | struct f2fs_sit_entry sit; |
Chao Yu | 74de593 | 2013-11-22 09:09:59 +0800 | [diff] [blame] | 4549 | int sit_blk_cnt = SIT_BLK_CNT(sbi); |
| 4550 | unsigned int i, start, end; |
| 4551 | unsigned int readed, start_blk = 0; |
Jaegeuk Kim | c39a1b3 | 2017-12-19 19:16:34 -0800 | [diff] [blame] | 4552 | int err = 0; |
Jaegeuk Kim | 8a29c12 | 2018-04-24 21:34:05 -0600 | [diff] [blame] | 4553 | block_t total_node_blocks = 0; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4554 | |
Chao Yu | 74de593 | 2013-11-22 09:09:59 +0800 | [diff] [blame] | 4555 | do { |
Christoph Hellwig | a8affc0 | 2021-03-11 12:01:37 +0100 | [diff] [blame] | 4556 | readed = f2fs_ra_meta_pages(sbi, start_blk, BIO_MAX_VECS, |
Jaegeuk Kim | 664ba97 | 2016-10-18 11:07:45 -0700 | [diff] [blame] | 4557 | META_SIT, true); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4558 | |
Chao Yu | 74de593 | 2013-11-22 09:09:59 +0800 | [diff] [blame] | 4559 | start = start_blk * sit_i->sents_per_block; |
| 4560 | end = (start_blk + readed) * sit_i->sents_per_block; |
| 4561 | |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 4562 | for (; start < end && start < MAIN_SEGS(sbi); start++) { |
Chao Yu | 74de593 | 2013-11-22 09:09:59 +0800 | [diff] [blame] | 4563 | struct f2fs_sit_block *sit_blk; |
Chao Yu | 74de593 | 2013-11-22 09:09:59 +0800 | [diff] [blame] | 4564 | struct page *page; |
| 4565 | |
Yunlei He | 9c09404 | 2016-09-24 12:29:18 +0800 | [diff] [blame] | 4566 | se = &sit_i->sentries[start]; |
Chao Yu | 74de593 | 2013-11-22 09:09:59 +0800 | [diff] [blame] | 4567 | page = get_current_sit_page(sbi, start); |
Jaegeuk Kim | edc55aa | 2018-09-17 17:36:06 -0700 | [diff] [blame] | 4568 | if (IS_ERR(page)) |
| 4569 | return PTR_ERR(page); |
Chao Yu | 74de593 | 2013-11-22 09:09:59 +0800 | [diff] [blame] | 4570 | sit_blk = (struct f2fs_sit_block *)page_address(page); |
| 4571 | sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)]; |
| 4572 | f2fs_put_page(page, 1); |
Chao Yu | d600af23 | 2016-08-19 23:13:47 +0800 | [diff] [blame] | 4573 | |
Jaegeuk Kim | c39a1b3 | 2017-12-19 19:16:34 -0800 | [diff] [blame] | 4574 | err = check_block_count(sbi, start, &sit); |
| 4575 | if (err) |
| 4576 | return err; |
Chao Yu | 74de593 | 2013-11-22 09:09:59 +0800 | [diff] [blame] | 4577 | seg_info_from_raw_sit(se, &sit); |
Jaegeuk Kim | 8a29c12 | 2018-04-24 21:34:05 -0600 | [diff] [blame] | 4578 | if (IS_NODESEG(se->type)) |
| 4579 | total_node_blocks += se->valid_blocks; |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 4580 | |
Chao Yu | 4f99326 | 2021-08-03 08:15:43 +0800 | [diff] [blame] | 4581 | if (f2fs_block_unit_discard(sbi)) { |
| 4582 | /* build discard map only one time */ |
| 4583 | if (is_set_ckpt_flags(sbi, CP_TRIMMED_FLAG)) { |
| 4584 | memset(se->discard_map, 0xff, |
| 4585 | SIT_VBLOCK_MAP_SIZE); |
| 4586 | } else { |
| 4587 | memcpy(se->discard_map, |
| 4588 | se->cur_valid_map, |
| 4589 | SIT_VBLOCK_MAP_SIZE); |
| 4590 | sbi->discard_blks += |
| 4591 | sbi->blocks_per_seg - |
| 4592 | se->valid_blocks; |
| 4593 | } |
Jaegeuk Kim | 3e02574 | 2016-08-02 10:56:40 -0700 | [diff] [blame] | 4594 | } |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 4595 | |
Chao Yu | 2c70c5e | 2018-10-24 18:37:26 +0800 | [diff] [blame] | 4596 | if (__is_large_section(sbi)) |
Chao Yu | d600af23 | 2016-08-19 23:13:47 +0800 | [diff] [blame] | 4597 | get_sec_entry(sbi, start)->valid_blocks += |
| 4598 | se->valid_blocks; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4599 | } |
Chao Yu | 74de593 | 2013-11-22 09:09:59 +0800 | [diff] [blame] | 4600 | start_blk += readed; |
| 4601 | } while (start_blk < sit_blk_cnt); |
Chao Yu | d600af23 | 2016-08-19 23:13:47 +0800 | [diff] [blame] | 4602 | |
| 4603 | down_read(&curseg->journal_rwsem); |
| 4604 | for (i = 0; i < sits_in_cursum(journal); i++) { |
Chao Yu | d600af23 | 2016-08-19 23:13:47 +0800 | [diff] [blame] | 4605 | unsigned int old_valid_blocks; |
| 4606 | |
| 4607 | start = le32_to_cpu(segno_in_journal(journal, i)); |
Jaegeuk Kim | b2ca374 | 2018-04-24 15:44:16 -0600 | [diff] [blame] | 4608 | if (start >= MAIN_SEGS(sbi)) { |
Joe Perches | dcbb4c1 | 2019-06-18 17:48:42 +0800 | [diff] [blame] | 4609 | f2fs_err(sbi, "Wrong journal entry on segno %u", |
| 4610 | start); |
Chao Yu | 10f966b | 2019-06-20 11:36:14 +0800 | [diff] [blame] | 4611 | err = -EFSCORRUPTED; |
Jaegeuk Kim | b2ca374 | 2018-04-24 15:44:16 -0600 | [diff] [blame] | 4612 | break; |
| 4613 | } |
| 4614 | |
Chao Yu | d600af23 | 2016-08-19 23:13:47 +0800 | [diff] [blame] | 4615 | se = &sit_i->sentries[start]; |
| 4616 | sit = sit_in_journal(journal, i); |
| 4617 | |
| 4618 | old_valid_blocks = se->valid_blocks; |
Jaegeuk Kim | 8a29c12 | 2018-04-24 21:34:05 -0600 | [diff] [blame] | 4619 | if (IS_NODESEG(se->type)) |
| 4620 | total_node_blocks -= old_valid_blocks; |
Chao Yu | d600af23 | 2016-08-19 23:13:47 +0800 | [diff] [blame] | 4621 | |
Jaegeuk Kim | c39a1b3 | 2017-12-19 19:16:34 -0800 | [diff] [blame] | 4622 | err = check_block_count(sbi, start, &sit); |
| 4623 | if (err) |
| 4624 | break; |
Chao Yu | d600af23 | 2016-08-19 23:13:47 +0800 | [diff] [blame] | 4625 | seg_info_from_raw_sit(se, &sit); |
Jaegeuk Kim | 8a29c12 | 2018-04-24 21:34:05 -0600 | [diff] [blame] | 4626 | if (IS_NODESEG(se->type)) |
| 4627 | total_node_blocks += se->valid_blocks; |
Chao Yu | d600af23 | 2016-08-19 23:13:47 +0800 | [diff] [blame] | 4628 | |
Chao Yu | 4f99326 | 2021-08-03 08:15:43 +0800 | [diff] [blame] | 4629 | if (f2fs_block_unit_discard(sbi)) { |
| 4630 | if (is_set_ckpt_flags(sbi, CP_TRIMMED_FLAG)) { |
| 4631 | memset(se->discard_map, 0xff, SIT_VBLOCK_MAP_SIZE); |
| 4632 | } else { |
| 4633 | memcpy(se->discard_map, se->cur_valid_map, |
| 4634 | SIT_VBLOCK_MAP_SIZE); |
| 4635 | sbi->discard_blks += old_valid_blocks; |
| 4636 | sbi->discard_blks -= se->valid_blocks; |
| 4637 | } |
Chao Yu | d600af23 | 2016-08-19 23:13:47 +0800 | [diff] [blame] | 4638 | } |
| 4639 | |
Chao Yu | 2c70c5e | 2018-10-24 18:37:26 +0800 | [diff] [blame] | 4640 | if (__is_large_section(sbi)) { |
Chao Yu | d600af23 | 2016-08-19 23:13:47 +0800 | [diff] [blame] | 4641 | get_sec_entry(sbi, start)->valid_blocks += |
Chao Yu | a9af3fd | 2018-04-25 19:38:17 +0800 | [diff] [blame] | 4642 | se->valid_blocks; |
| 4643 | get_sec_entry(sbi, start)->valid_blocks -= |
| 4644 | old_valid_blocks; |
| 4645 | } |
Chao Yu | d600af23 | 2016-08-19 23:13:47 +0800 | [diff] [blame] | 4646 | } |
| 4647 | up_read(&curseg->journal_rwsem); |
Jaegeuk Kim | 8a29c12 | 2018-04-24 21:34:05 -0600 | [diff] [blame] | 4648 | |
| 4649 | if (!err && total_node_blocks != valid_node_count(sbi)) { |
Joe Perches | dcbb4c1 | 2019-06-18 17:48:42 +0800 | [diff] [blame] | 4650 | f2fs_err(sbi, "SIT is corrupted node# %u vs %u", |
| 4651 | total_node_blocks, valid_node_count(sbi)); |
Chao Yu | 10f966b | 2019-06-20 11:36:14 +0800 | [diff] [blame] | 4652 | err = -EFSCORRUPTED; |
Jaegeuk Kim | 8a29c12 | 2018-04-24 21:34:05 -0600 | [diff] [blame] | 4653 | } |
| 4654 | |
Jaegeuk Kim | c39a1b3 | 2017-12-19 19:16:34 -0800 | [diff] [blame] | 4655 | return err; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4656 | } |
| 4657 | |
| 4658 | static void init_free_segmap(struct f2fs_sb_info *sbi) |
| 4659 | { |
| 4660 | unsigned int start; |
| 4661 | int type; |
Aravind Ramesh | de881df | 2020-07-16 18:26:56 +0530 | [diff] [blame] | 4662 | struct seg_entry *sentry; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4663 | |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 4664 | for (start = 0; start < MAIN_SEGS(sbi); start++) { |
Aravind Ramesh | de881df | 2020-07-16 18:26:56 +0530 | [diff] [blame] | 4665 | if (f2fs_usable_blks_in_seg(sbi, start) == 0) |
| 4666 | continue; |
| 4667 | sentry = get_seg_entry(sbi, start); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4668 | if (!sentry->valid_blocks) |
| 4669 | __set_free(sbi, start); |
Jaegeuk Kim | c79b7ff | 2016-11-14 18:20:10 -0800 | [diff] [blame] | 4670 | else |
| 4671 | SIT_I(sbi)->written_valid_blocks += |
| 4672 | sentry->valid_blocks; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4673 | } |
| 4674 | |
| 4675 | /* set use the current segments */ |
| 4676 | for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) { |
| 4677 | struct curseg_info *curseg_t = CURSEG_I(sbi, type); |
Yi Zhuang | 5f029c0 | 2021-04-06 09:47:35 +0800 | [diff] [blame] | 4678 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4679 | __set_test_and_inuse(sbi, curseg_t->segno); |
| 4680 | } |
| 4681 | } |
| 4682 | |
| 4683 | static void init_dirty_segmap(struct f2fs_sb_info *sbi) |
| 4684 | { |
| 4685 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
| 4686 | struct free_segmap_info *free_i = FREE_I(sbi); |
Jack Qiu | da52f8a | 2020-06-18 12:37:10 +0800 | [diff] [blame] | 4687 | unsigned int segno = 0, offset = 0, secno; |
Aravind Ramesh | de881df | 2020-07-16 18:26:56 +0530 | [diff] [blame] | 4688 | block_t valid_blocks, usable_blks_in_seg; |
Shin'ichiro Kawasaki | 123aaf7 | 2020-08-19 10:34:48 +0900 | [diff] [blame] | 4689 | block_t blks_per_sec = BLKS_PER_SEC(sbi); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4690 | |
Namjae Jeon | 8736fbf | 2013-06-16 09:49:11 +0900 | [diff] [blame] | 4691 | while (1) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4692 | /* find dirty segment based on free segmap */ |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 4693 | segno = find_next_inuse(free_i, MAIN_SEGS(sbi), offset); |
| 4694 | if (segno >= MAIN_SEGS(sbi)) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4695 | break; |
| 4696 | offset = segno + 1; |
Jaegeuk Kim | 302bd34 | 2017-04-07 14:33:22 -0700 | [diff] [blame] | 4697 | valid_blocks = get_valid_blocks(sbi, segno, false); |
Aravind Ramesh | de881df | 2020-07-16 18:26:56 +0530 | [diff] [blame] | 4698 | usable_blks_in_seg = f2fs_usable_blks_in_seg(sbi, segno); |
| 4699 | if (valid_blocks == usable_blks_in_seg || !valid_blocks) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4700 | continue; |
Aravind Ramesh | de881df | 2020-07-16 18:26:56 +0530 | [diff] [blame] | 4701 | if (valid_blocks > usable_blks_in_seg) { |
Jaegeuk Kim | ec325b5 | 2014-09-02 16:24:11 -0700 | [diff] [blame] | 4702 | f2fs_bug_on(sbi, 1); |
| 4703 | continue; |
| 4704 | } |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4705 | mutex_lock(&dirty_i->seglist_lock); |
| 4706 | __locate_dirty_segment(sbi, segno, DIRTY); |
| 4707 | mutex_unlock(&dirty_i->seglist_lock); |
| 4708 | } |
Jack Qiu | da52f8a | 2020-06-18 12:37:10 +0800 | [diff] [blame] | 4709 | |
| 4710 | if (!__is_large_section(sbi)) |
| 4711 | return; |
| 4712 | |
| 4713 | mutex_lock(&dirty_i->seglist_lock); |
Jack Qiu | 5335bfc | 2020-12-01 15:45:47 +0800 | [diff] [blame] | 4714 | for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) { |
Jack Qiu | da52f8a | 2020-06-18 12:37:10 +0800 | [diff] [blame] | 4715 | valid_blocks = get_valid_blocks(sbi, segno, true); |
| 4716 | secno = GET_SEC_FROM_SEG(sbi, segno); |
| 4717 | |
| 4718 | if (!valid_blocks || valid_blocks == blks_per_sec) |
| 4719 | continue; |
| 4720 | if (IS_CURSEC(sbi, secno)) |
| 4721 | continue; |
| 4722 | set_bit(secno, dirty_i->dirty_secmap); |
| 4723 | } |
| 4724 | mutex_unlock(&dirty_i->seglist_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4725 | } |
| 4726 | |
Jaegeuk Kim | 5ec4e49 | 2013-03-31 13:26:03 +0900 | [diff] [blame] | 4727 | static int init_victim_secmap(struct f2fs_sb_info *sbi) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4728 | { |
| 4729 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 4730 | unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4731 | |
Chao Yu | 628b3d1 | 2017-11-30 19:28:18 +0800 | [diff] [blame] | 4732 | dirty_i->victim_secmap = f2fs_kvzalloc(sbi, bitmap_size, GFP_KERNEL); |
Jaegeuk Kim | 5ec4e49 | 2013-03-31 13:26:03 +0900 | [diff] [blame] | 4733 | if (!dirty_i->victim_secmap) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4734 | return -ENOMEM; |
| 4735 | return 0; |
| 4736 | } |
| 4737 | |
| 4738 | static int build_dirty_segmap(struct f2fs_sb_info *sbi) |
| 4739 | { |
| 4740 | struct dirty_seglist_info *dirty_i; |
| 4741 | unsigned int bitmap_size, i; |
| 4742 | |
| 4743 | /* allocate memory for dirty segments list information */ |
Chao Yu | acbf054 | 2017-11-30 19:28:17 +0800 | [diff] [blame] | 4744 | dirty_i = f2fs_kzalloc(sbi, sizeof(struct dirty_seglist_info), |
| 4745 | GFP_KERNEL); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4746 | if (!dirty_i) |
| 4747 | return -ENOMEM; |
| 4748 | |
| 4749 | SM_I(sbi)->dirty_info = dirty_i; |
| 4750 | mutex_init(&dirty_i->seglist_lock); |
| 4751 | |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 4752 | bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4753 | |
| 4754 | for (i = 0; i < NR_DIRTY_TYPE; i++) { |
Chao Yu | 628b3d1 | 2017-11-30 19:28:18 +0800 | [diff] [blame] | 4755 | dirty_i->dirty_segmap[i] = f2fs_kvzalloc(sbi, bitmap_size, |
| 4756 | GFP_KERNEL); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4757 | if (!dirty_i->dirty_segmap[i]) |
| 4758 | return -ENOMEM; |
| 4759 | } |
| 4760 | |
Jack Qiu | da52f8a | 2020-06-18 12:37:10 +0800 | [diff] [blame] | 4761 | if (__is_large_section(sbi)) { |
| 4762 | bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi)); |
| 4763 | dirty_i->dirty_secmap = f2fs_kvzalloc(sbi, |
| 4764 | bitmap_size, GFP_KERNEL); |
| 4765 | if (!dirty_i->dirty_secmap) |
| 4766 | return -ENOMEM; |
| 4767 | } |
| 4768 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4769 | init_dirty_segmap(sbi); |
Jaegeuk Kim | 5ec4e49 | 2013-03-31 13:26:03 +0900 | [diff] [blame] | 4770 | return init_victim_secmap(sbi); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4771 | } |
| 4772 | |
Chao Yu | c854f4d | 2019-05-25 23:07:25 +0800 | [diff] [blame] | 4773 | static int sanity_check_curseg(struct f2fs_sb_info *sbi) |
| 4774 | { |
| 4775 | int i; |
| 4776 | |
| 4777 | /* |
| 4778 | * In LFS/SSR curseg, .next_blkoff should point to an unused blkaddr; |
| 4779 | * In LFS curseg, all blkaddr after .next_blkoff should be unused. |
| 4780 | */ |
Chao Yu | d0b9e42 | 2020-08-04 21:14:45 +0800 | [diff] [blame] | 4781 | for (i = 0; i < NR_PERSISTENT_LOG; i++) { |
Chao Yu | c854f4d | 2019-05-25 23:07:25 +0800 | [diff] [blame] | 4782 | struct curseg_info *curseg = CURSEG_I(sbi, i); |
| 4783 | struct seg_entry *se = get_seg_entry(sbi, curseg->segno); |
| 4784 | unsigned int blkofs = curseg->next_blkoff; |
| 4785 | |
Jaegeuk Kim | a7d9fe3 | 2021-05-21 01:32:53 -0700 | [diff] [blame] | 4786 | if (f2fs_sb_has_readonly(sbi) && |
| 4787 | i != CURSEG_HOT_DATA && i != CURSEG_HOT_NODE) |
| 4788 | continue; |
| 4789 | |
Chao Yu | 093749e | 2020-08-04 21:14:49 +0800 | [diff] [blame] | 4790 | sanity_check_seg_type(sbi, curseg->seg_type); |
| 4791 | |
Chao Yu | c854f4d | 2019-05-25 23:07:25 +0800 | [diff] [blame] | 4792 | if (f2fs_test_bit(blkofs, se->cur_valid_map)) |
| 4793 | goto out; |
| 4794 | |
| 4795 | if (curseg->alloc_type == SSR) |
| 4796 | continue; |
| 4797 | |
| 4798 | for (blkofs += 1; blkofs < sbi->blocks_per_seg; blkofs++) { |
| 4799 | if (!f2fs_test_bit(blkofs, se->cur_valid_map)) |
| 4800 | continue; |
| 4801 | out: |
Joe Perches | dcbb4c1 | 2019-06-18 17:48:42 +0800 | [diff] [blame] | 4802 | f2fs_err(sbi, |
| 4803 | "Current segment's next free block offset is inconsistent with bitmap, logtype:%u, segno:%u, type:%u, next_blkoff:%u, blkofs:%u", |
| 4804 | i, curseg->segno, curseg->alloc_type, |
| 4805 | curseg->next_blkoff, blkofs); |
Chao Yu | 10f966b | 2019-06-20 11:36:14 +0800 | [diff] [blame] | 4806 | return -EFSCORRUPTED; |
Chao Yu | c854f4d | 2019-05-25 23:07:25 +0800 | [diff] [blame] | 4807 | } |
| 4808 | } |
| 4809 | return 0; |
| 4810 | } |
| 4811 | |
Shin'ichiro Kawasaki | c426d99 | 2019-12-09 19:44:44 +0900 | [diff] [blame] | 4812 | #ifdef CONFIG_BLK_DEV_ZONED |
| 4813 | |
Shin'ichiro Kawasaki | d508c94 | 2019-12-09 19:44:45 +0900 | [diff] [blame] | 4814 | static int check_zone_write_pointer(struct f2fs_sb_info *sbi, |
| 4815 | struct f2fs_dev_info *fdev, |
| 4816 | struct blk_zone *zone) |
| 4817 | { |
| 4818 | unsigned int wp_segno, wp_blkoff, zone_secno, zone_segno, segno; |
| 4819 | block_t zone_block, wp_block, last_valid_block; |
| 4820 | unsigned int log_sectors_per_block = sbi->log_blocksize - SECTOR_SHIFT; |
| 4821 | int i, s, b, ret; |
| 4822 | struct seg_entry *se; |
| 4823 | |
| 4824 | if (zone->type != BLK_ZONE_TYPE_SEQWRITE_REQ) |
| 4825 | return 0; |
| 4826 | |
| 4827 | wp_block = fdev->start_blk + (zone->wp >> log_sectors_per_block); |
| 4828 | wp_segno = GET_SEGNO(sbi, wp_block); |
| 4829 | wp_blkoff = wp_block - START_BLOCK(sbi, wp_segno); |
| 4830 | zone_block = fdev->start_blk + (zone->start >> log_sectors_per_block); |
| 4831 | zone_segno = GET_SEGNO(sbi, zone_block); |
| 4832 | zone_secno = GET_SEC_FROM_SEG(sbi, zone_segno); |
| 4833 | |
| 4834 | if (zone_segno >= MAIN_SEGS(sbi)) |
| 4835 | return 0; |
| 4836 | |
| 4837 | /* |
| 4838 | * Skip check of zones cursegs point to, since |
| 4839 | * fix_curseg_write_pointer() checks them. |
| 4840 | */ |
| 4841 | for (i = 0; i < NO_CHECK_TYPE; i++) |
| 4842 | if (zone_secno == GET_SEC_FROM_SEG(sbi, |
| 4843 | CURSEG_I(sbi, i)->segno)) |
| 4844 | return 0; |
| 4845 | |
| 4846 | /* |
| 4847 | * Get last valid block of the zone. |
| 4848 | */ |
| 4849 | last_valid_block = zone_block - 1; |
| 4850 | for (s = sbi->segs_per_sec - 1; s >= 0; s--) { |
| 4851 | segno = zone_segno + s; |
| 4852 | se = get_seg_entry(sbi, segno); |
| 4853 | for (b = sbi->blocks_per_seg - 1; b >= 0; b--) |
| 4854 | if (f2fs_test_bit(b, se->cur_valid_map)) { |
| 4855 | last_valid_block = START_BLOCK(sbi, segno) + b; |
| 4856 | break; |
| 4857 | } |
| 4858 | if (last_valid_block >= zone_block) |
| 4859 | break; |
| 4860 | } |
| 4861 | |
| 4862 | /* |
| 4863 | * If last valid block is beyond the write pointer, report the |
| 4864 | * inconsistency. This inconsistency does not cause write error |
| 4865 | * because the zone will not be selected for write operation until |
| 4866 | * it get discarded. Just report it. |
| 4867 | */ |
| 4868 | if (last_valid_block >= wp_block) { |
| 4869 | f2fs_notice(sbi, "Valid block beyond write pointer: " |
| 4870 | "valid block[0x%x,0x%x] wp[0x%x,0x%x]", |
| 4871 | GET_SEGNO(sbi, last_valid_block), |
| 4872 | GET_BLKOFF_FROM_SEG0(sbi, last_valid_block), |
| 4873 | wp_segno, wp_blkoff); |
| 4874 | return 0; |
| 4875 | } |
| 4876 | |
| 4877 | /* |
| 4878 | * If there is no valid block in the zone and if write pointer is |
| 4879 | * not at zone start, reset the write pointer. |
| 4880 | */ |
| 4881 | if (last_valid_block + 1 == zone_block && zone->wp != zone->start) { |
| 4882 | f2fs_notice(sbi, |
| 4883 | "Zone without valid block has non-zero write " |
| 4884 | "pointer. Reset the write pointer: wp[0x%x,0x%x]", |
| 4885 | wp_segno, wp_blkoff); |
| 4886 | ret = __f2fs_issue_discard_zone(sbi, fdev->bdev, zone_block, |
| 4887 | zone->len >> log_sectors_per_block); |
| 4888 | if (ret) { |
| 4889 | f2fs_err(sbi, "Discard zone failed: %s (errno=%d)", |
| 4890 | fdev->path, ret); |
| 4891 | return ret; |
| 4892 | } |
| 4893 | } |
| 4894 | |
| 4895 | return 0; |
| 4896 | } |
| 4897 | |
Shin'ichiro Kawasaki | c426d99 | 2019-12-09 19:44:44 +0900 | [diff] [blame] | 4898 | static struct f2fs_dev_info *get_target_zoned_dev(struct f2fs_sb_info *sbi, |
| 4899 | block_t zone_blkaddr) |
| 4900 | { |
| 4901 | int i; |
| 4902 | |
| 4903 | for (i = 0; i < sbi->s_ndevs; i++) { |
| 4904 | if (!bdev_is_zoned(FDEV(i).bdev)) |
| 4905 | continue; |
| 4906 | if (sbi->s_ndevs == 1 || (FDEV(i).start_blk <= zone_blkaddr && |
| 4907 | zone_blkaddr <= FDEV(i).end_blk)) |
| 4908 | return &FDEV(i); |
| 4909 | } |
| 4910 | |
| 4911 | return NULL; |
| 4912 | } |
| 4913 | |
| 4914 | static int report_one_zone_cb(struct blk_zone *zone, unsigned int idx, |
Yi Zhuang | 5f029c0 | 2021-04-06 09:47:35 +0800 | [diff] [blame] | 4915 | void *data) |
| 4916 | { |
Shin'ichiro Kawasaki | c426d99 | 2019-12-09 19:44:44 +0900 | [diff] [blame] | 4917 | memcpy(data, zone, sizeof(struct blk_zone)); |
| 4918 | return 0; |
| 4919 | } |
| 4920 | |
| 4921 | static int fix_curseg_write_pointer(struct f2fs_sb_info *sbi, int type) |
| 4922 | { |
| 4923 | struct curseg_info *cs = CURSEG_I(sbi, type); |
| 4924 | struct f2fs_dev_info *zbd; |
| 4925 | struct blk_zone zone; |
| 4926 | unsigned int cs_section, wp_segno, wp_blkoff, wp_sector_off; |
| 4927 | block_t cs_zone_block, wp_block; |
| 4928 | unsigned int log_sectors_per_block = sbi->log_blocksize - SECTOR_SHIFT; |
| 4929 | sector_t zone_sector; |
| 4930 | int err; |
| 4931 | |
| 4932 | cs_section = GET_SEC_FROM_SEG(sbi, cs->segno); |
| 4933 | cs_zone_block = START_BLOCK(sbi, GET_SEG_FROM_SEC(sbi, cs_section)); |
| 4934 | |
| 4935 | zbd = get_target_zoned_dev(sbi, cs_zone_block); |
| 4936 | if (!zbd) |
| 4937 | return 0; |
| 4938 | |
| 4939 | /* report zone for the sector the curseg points to */ |
| 4940 | zone_sector = (sector_t)(cs_zone_block - zbd->start_blk) |
| 4941 | << log_sectors_per_block; |
| 4942 | err = blkdev_report_zones(zbd->bdev, zone_sector, 1, |
| 4943 | report_one_zone_cb, &zone); |
| 4944 | if (err != 1) { |
| 4945 | f2fs_err(sbi, "Report zone failed: %s errno=(%d)", |
| 4946 | zbd->path, err); |
| 4947 | return err; |
| 4948 | } |
| 4949 | |
| 4950 | if (zone.type != BLK_ZONE_TYPE_SEQWRITE_REQ) |
| 4951 | return 0; |
| 4952 | |
| 4953 | wp_block = zbd->start_blk + (zone.wp >> log_sectors_per_block); |
| 4954 | wp_segno = GET_SEGNO(sbi, wp_block); |
| 4955 | wp_blkoff = wp_block - START_BLOCK(sbi, wp_segno); |
| 4956 | wp_sector_off = zone.wp & GENMASK(log_sectors_per_block - 1, 0); |
| 4957 | |
| 4958 | if (cs->segno == wp_segno && cs->next_blkoff == wp_blkoff && |
| 4959 | wp_sector_off == 0) |
| 4960 | return 0; |
| 4961 | |
| 4962 | f2fs_notice(sbi, "Unaligned curseg[%d] with write pointer: " |
| 4963 | "curseg[0x%x,0x%x] wp[0x%x,0x%x]", |
| 4964 | type, cs->segno, cs->next_blkoff, wp_segno, wp_blkoff); |
| 4965 | |
| 4966 | f2fs_notice(sbi, "Assign new section to curseg[%d]: " |
| 4967 | "curseg[0x%x,0x%x]", type, cs->segno, cs->next_blkoff); |
Chao Yu | 509f101 | 2021-04-21 09:54:55 +0800 | [diff] [blame] | 4968 | |
| 4969 | f2fs_allocate_new_section(sbi, type, true); |
Shin'ichiro Kawasaki | c426d99 | 2019-12-09 19:44:44 +0900 | [diff] [blame] | 4970 | |
Shin'ichiro Kawasaki | d508c94 | 2019-12-09 19:44:45 +0900 | [diff] [blame] | 4971 | /* check consistency of the zone curseg pointed to */ |
| 4972 | if (check_zone_write_pointer(sbi, zbd, &zone)) |
| 4973 | return -EIO; |
| 4974 | |
Shin'ichiro Kawasaki | c426d99 | 2019-12-09 19:44:44 +0900 | [diff] [blame] | 4975 | /* check newly assigned zone */ |
| 4976 | cs_section = GET_SEC_FROM_SEG(sbi, cs->segno); |
| 4977 | cs_zone_block = START_BLOCK(sbi, GET_SEG_FROM_SEC(sbi, cs_section)); |
| 4978 | |
| 4979 | zbd = get_target_zoned_dev(sbi, cs_zone_block); |
| 4980 | if (!zbd) |
| 4981 | return 0; |
| 4982 | |
| 4983 | zone_sector = (sector_t)(cs_zone_block - zbd->start_blk) |
| 4984 | << log_sectors_per_block; |
| 4985 | err = blkdev_report_zones(zbd->bdev, zone_sector, 1, |
| 4986 | report_one_zone_cb, &zone); |
| 4987 | if (err != 1) { |
| 4988 | f2fs_err(sbi, "Report zone failed: %s errno=(%d)", |
| 4989 | zbd->path, err); |
| 4990 | return err; |
| 4991 | } |
| 4992 | |
| 4993 | if (zone.type != BLK_ZONE_TYPE_SEQWRITE_REQ) |
| 4994 | return 0; |
| 4995 | |
| 4996 | if (zone.wp != zone.start) { |
| 4997 | f2fs_notice(sbi, |
| 4998 | "New zone for curseg[%d] is not yet discarded. " |
| 4999 | "Reset the zone: curseg[0x%x,0x%x]", |
| 5000 | type, cs->segno, cs->next_blkoff); |
| 5001 | err = __f2fs_issue_discard_zone(sbi, zbd->bdev, |
| 5002 | zone_sector >> log_sectors_per_block, |
| 5003 | zone.len >> log_sectors_per_block); |
| 5004 | if (err) { |
| 5005 | f2fs_err(sbi, "Discard zone failed: %s (errno=%d)", |
| 5006 | zbd->path, err); |
| 5007 | return err; |
| 5008 | } |
| 5009 | } |
| 5010 | |
| 5011 | return 0; |
| 5012 | } |
| 5013 | |
| 5014 | int f2fs_fix_curseg_write_pointer(struct f2fs_sb_info *sbi) |
| 5015 | { |
| 5016 | int i, ret; |
| 5017 | |
Chao Yu | d0b9e42 | 2020-08-04 21:14:45 +0800 | [diff] [blame] | 5018 | for (i = 0; i < NR_PERSISTENT_LOG; i++) { |
Shin'ichiro Kawasaki | c426d99 | 2019-12-09 19:44:44 +0900 | [diff] [blame] | 5019 | ret = fix_curseg_write_pointer(sbi, i); |
| 5020 | if (ret) |
| 5021 | return ret; |
| 5022 | } |
| 5023 | |
| 5024 | return 0; |
| 5025 | } |
Shin'ichiro Kawasaki | d508c94 | 2019-12-09 19:44:45 +0900 | [diff] [blame] | 5026 | |
| 5027 | struct check_zone_write_pointer_args { |
| 5028 | struct f2fs_sb_info *sbi; |
| 5029 | struct f2fs_dev_info *fdev; |
| 5030 | }; |
| 5031 | |
| 5032 | static int check_zone_write_pointer_cb(struct blk_zone *zone, unsigned int idx, |
Yi Zhuang | 5f029c0 | 2021-04-06 09:47:35 +0800 | [diff] [blame] | 5033 | void *data) |
| 5034 | { |
Shin'ichiro Kawasaki | d508c94 | 2019-12-09 19:44:45 +0900 | [diff] [blame] | 5035 | struct check_zone_write_pointer_args *args; |
Yi Zhuang | 5f029c0 | 2021-04-06 09:47:35 +0800 | [diff] [blame] | 5036 | |
Shin'ichiro Kawasaki | d508c94 | 2019-12-09 19:44:45 +0900 | [diff] [blame] | 5037 | args = (struct check_zone_write_pointer_args *)data; |
| 5038 | |
| 5039 | return check_zone_write_pointer(args->sbi, args->fdev, zone); |
| 5040 | } |
| 5041 | |
| 5042 | int f2fs_check_write_pointer(struct f2fs_sb_info *sbi) |
| 5043 | { |
| 5044 | int i, ret; |
| 5045 | struct check_zone_write_pointer_args args; |
| 5046 | |
| 5047 | for (i = 0; i < sbi->s_ndevs; i++) { |
| 5048 | if (!bdev_is_zoned(FDEV(i).bdev)) |
| 5049 | continue; |
| 5050 | |
| 5051 | args.sbi = sbi; |
| 5052 | args.fdev = &FDEV(i); |
| 5053 | ret = blkdev_report_zones(FDEV(i).bdev, 0, BLK_ALL_ZONES, |
| 5054 | check_zone_write_pointer_cb, &args); |
| 5055 | if (ret < 0) |
| 5056 | return ret; |
| 5057 | } |
| 5058 | |
| 5059 | return 0; |
| 5060 | } |
Aravind Ramesh | de881df | 2020-07-16 18:26:56 +0530 | [diff] [blame] | 5061 | |
| 5062 | static bool is_conv_zone(struct f2fs_sb_info *sbi, unsigned int zone_idx, |
| 5063 | unsigned int dev_idx) |
| 5064 | { |
| 5065 | if (!bdev_is_zoned(FDEV(dev_idx).bdev)) |
| 5066 | return true; |
| 5067 | return !test_bit(zone_idx, FDEV(dev_idx).blkz_seq); |
| 5068 | } |
| 5069 | |
| 5070 | /* Return the zone index in the given device */ |
| 5071 | static unsigned int get_zone_idx(struct f2fs_sb_info *sbi, unsigned int secno, |
| 5072 | int dev_idx) |
| 5073 | { |
| 5074 | block_t sec_start_blkaddr = START_BLOCK(sbi, GET_SEG_FROM_SEC(sbi, secno)); |
| 5075 | |
| 5076 | return (sec_start_blkaddr - FDEV(dev_idx).start_blk) >> |
| 5077 | sbi->log_blocks_per_blkz; |
| 5078 | } |
| 5079 | |
| 5080 | /* |
| 5081 | * Return the usable segments in a section based on the zone's |
| 5082 | * corresponding zone capacity. Zone is equal to a section. |
| 5083 | */ |
| 5084 | static inline unsigned int f2fs_usable_zone_segs_in_sec( |
| 5085 | struct f2fs_sb_info *sbi, unsigned int segno) |
| 5086 | { |
| 5087 | unsigned int dev_idx, zone_idx, unusable_segs_in_sec; |
| 5088 | |
| 5089 | dev_idx = f2fs_target_device_index(sbi, START_BLOCK(sbi, segno)); |
| 5090 | zone_idx = get_zone_idx(sbi, GET_SEC_FROM_SEG(sbi, segno), dev_idx); |
| 5091 | |
| 5092 | /* Conventional zone's capacity is always equal to zone size */ |
| 5093 | if (is_conv_zone(sbi, zone_idx, dev_idx)) |
| 5094 | return sbi->segs_per_sec; |
| 5095 | |
| 5096 | /* |
| 5097 | * If the zone_capacity_blocks array is NULL, then zone capacity |
| 5098 | * is equal to the zone size for all zones |
| 5099 | */ |
| 5100 | if (!FDEV(dev_idx).zone_capacity_blocks) |
| 5101 | return sbi->segs_per_sec; |
| 5102 | |
| 5103 | /* Get the segment count beyond zone capacity block */ |
| 5104 | unusable_segs_in_sec = (sbi->blocks_per_blkz - |
| 5105 | FDEV(dev_idx).zone_capacity_blocks[zone_idx]) >> |
| 5106 | sbi->log_blocks_per_seg; |
| 5107 | return sbi->segs_per_sec - unusable_segs_in_sec; |
| 5108 | } |
| 5109 | |
| 5110 | /* |
| 5111 | * Return the number of usable blocks in a segment. The number of blocks |
| 5112 | * returned is always equal to the number of blocks in a segment for |
| 5113 | * segments fully contained within a sequential zone capacity or a |
| 5114 | * conventional zone. For segments partially contained in a sequential |
| 5115 | * zone capacity, the number of usable blocks up to the zone capacity |
| 5116 | * is returned. 0 is returned in all other cases. |
| 5117 | */ |
| 5118 | static inline unsigned int f2fs_usable_zone_blks_in_seg( |
| 5119 | struct f2fs_sb_info *sbi, unsigned int segno) |
| 5120 | { |
| 5121 | block_t seg_start, sec_start_blkaddr, sec_cap_blkaddr; |
| 5122 | unsigned int zone_idx, dev_idx, secno; |
| 5123 | |
| 5124 | secno = GET_SEC_FROM_SEG(sbi, segno); |
| 5125 | seg_start = START_BLOCK(sbi, segno); |
| 5126 | dev_idx = f2fs_target_device_index(sbi, seg_start); |
| 5127 | zone_idx = get_zone_idx(sbi, secno, dev_idx); |
| 5128 | |
| 5129 | /* |
| 5130 | * Conventional zone's capacity is always equal to zone size, |
| 5131 | * so, blocks per segment is unchanged. |
| 5132 | */ |
| 5133 | if (is_conv_zone(sbi, zone_idx, dev_idx)) |
| 5134 | return sbi->blocks_per_seg; |
| 5135 | |
| 5136 | if (!FDEV(dev_idx).zone_capacity_blocks) |
| 5137 | return sbi->blocks_per_seg; |
| 5138 | |
| 5139 | sec_start_blkaddr = START_BLOCK(sbi, GET_SEG_FROM_SEC(sbi, secno)); |
| 5140 | sec_cap_blkaddr = sec_start_blkaddr + |
| 5141 | FDEV(dev_idx).zone_capacity_blocks[zone_idx]; |
| 5142 | |
| 5143 | /* |
| 5144 | * If segment starts before zone capacity and spans beyond |
| 5145 | * zone capacity, then usable blocks are from seg start to |
| 5146 | * zone capacity. If the segment starts after the zone capacity, |
| 5147 | * then there are no usable blocks. |
| 5148 | */ |
| 5149 | if (seg_start >= sec_cap_blkaddr) |
| 5150 | return 0; |
| 5151 | if (seg_start + sbi->blocks_per_seg > sec_cap_blkaddr) |
| 5152 | return sec_cap_blkaddr - seg_start; |
| 5153 | |
| 5154 | return sbi->blocks_per_seg; |
| 5155 | } |
Shin'ichiro Kawasaki | c426d99 | 2019-12-09 19:44:44 +0900 | [diff] [blame] | 5156 | #else |
| 5157 | int f2fs_fix_curseg_write_pointer(struct f2fs_sb_info *sbi) |
| 5158 | { |
| 5159 | return 0; |
| 5160 | } |
Shin'ichiro Kawasaki | d508c94 | 2019-12-09 19:44:45 +0900 | [diff] [blame] | 5161 | |
| 5162 | int f2fs_check_write_pointer(struct f2fs_sb_info *sbi) |
| 5163 | { |
| 5164 | return 0; |
| 5165 | } |
Aravind Ramesh | de881df | 2020-07-16 18:26:56 +0530 | [diff] [blame] | 5166 | |
| 5167 | static inline unsigned int f2fs_usable_zone_blks_in_seg(struct f2fs_sb_info *sbi, |
| 5168 | unsigned int segno) |
| 5169 | { |
| 5170 | return 0; |
| 5171 | } |
| 5172 | |
| 5173 | static inline unsigned int f2fs_usable_zone_segs_in_sec(struct f2fs_sb_info *sbi, |
| 5174 | unsigned int segno) |
| 5175 | { |
| 5176 | return 0; |
| 5177 | } |
Shin'ichiro Kawasaki | c426d99 | 2019-12-09 19:44:44 +0900 | [diff] [blame] | 5178 | #endif |
Aravind Ramesh | de881df | 2020-07-16 18:26:56 +0530 | [diff] [blame] | 5179 | unsigned int f2fs_usable_blks_in_seg(struct f2fs_sb_info *sbi, |
| 5180 | unsigned int segno) |
| 5181 | { |
| 5182 | if (f2fs_sb_has_blkzoned(sbi)) |
| 5183 | return f2fs_usable_zone_blks_in_seg(sbi, segno); |
| 5184 | |
| 5185 | return sbi->blocks_per_seg; |
| 5186 | } |
| 5187 | |
| 5188 | unsigned int f2fs_usable_segs_in_sec(struct f2fs_sb_info *sbi, |
| 5189 | unsigned int segno) |
| 5190 | { |
| 5191 | if (f2fs_sb_has_blkzoned(sbi)) |
| 5192 | return f2fs_usable_zone_segs_in_sec(sbi, segno); |
| 5193 | |
| 5194 | return sbi->segs_per_sec; |
| 5195 | } |
Shin'ichiro Kawasaki | c426d99 | 2019-12-09 19:44:44 +0900 | [diff] [blame] | 5196 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 5197 | /* |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 5198 | * Update min, max modified time for cost-benefit GC algorithm |
| 5199 | */ |
| 5200 | static void init_min_max_mtime(struct f2fs_sb_info *sbi) |
| 5201 | { |
| 5202 | struct sit_info *sit_i = SIT_I(sbi); |
| 5203 | unsigned int segno; |
| 5204 | |
Chao Yu | 3d26fa6 | 2017-10-30 17:49:53 +0800 | [diff] [blame] | 5205 | down_write(&sit_i->sentry_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 5206 | |
Chao Yu | 5ad2544 | 2018-05-15 18:59:55 +0800 | [diff] [blame] | 5207 | sit_i->min_mtime = ULLONG_MAX; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 5208 | |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 5209 | for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 5210 | unsigned int i; |
| 5211 | unsigned long long mtime = 0; |
| 5212 | |
| 5213 | for (i = 0; i < sbi->segs_per_sec; i++) |
| 5214 | mtime += get_seg_entry(sbi, segno + i)->mtime; |
| 5215 | |
| 5216 | mtime = div_u64(mtime, sbi->segs_per_sec); |
| 5217 | |
| 5218 | if (sit_i->min_mtime > mtime) |
| 5219 | sit_i->min_mtime = mtime; |
| 5220 | } |
Chao Yu | a1f72ac2 | 2018-06-04 23:20:17 +0800 | [diff] [blame] | 5221 | sit_i->max_mtime = get_mtime(sbi, false); |
Chao Yu | 093749e | 2020-08-04 21:14:49 +0800 | [diff] [blame] | 5222 | sit_i->dirty_max_mtime = 0; |
Chao Yu | 3d26fa6 | 2017-10-30 17:49:53 +0800 | [diff] [blame] | 5223 | up_write(&sit_i->sentry_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 5224 | } |
| 5225 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 5226 | int f2fs_build_segment_manager(struct f2fs_sb_info *sbi) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 5227 | { |
| 5228 | struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi); |
| 5229 | struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); |
Namjae Jeon | 1042d60 | 2012-12-01 10:56:13 +0900 | [diff] [blame] | 5230 | struct f2fs_sm_info *sm_info; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 5231 | int err; |
| 5232 | |
Chao Yu | acbf054 | 2017-11-30 19:28:17 +0800 | [diff] [blame] | 5233 | sm_info = f2fs_kzalloc(sbi, sizeof(struct f2fs_sm_info), GFP_KERNEL); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 5234 | if (!sm_info) |
| 5235 | return -ENOMEM; |
| 5236 | |
| 5237 | /* init sm info */ |
| 5238 | sbi->sm_info = sm_info; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 5239 | sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr); |
| 5240 | sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr); |
| 5241 | sm_info->segment_count = le32_to_cpu(raw_super->segment_count); |
| 5242 | sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count); |
| 5243 | sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count); |
| 5244 | sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main); |
| 5245 | sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr); |
Jaegeuk Kim | 58c4103 | 2014-03-19 14:17:21 +0900 | [diff] [blame] | 5246 | sm_info->rec_prefree_segments = sm_info->main_segments * |
| 5247 | DEF_RECLAIM_PREFREE_SEGMENTS / 100; |
Jaegeuk Kim | 44a8349 | 2016-07-13 18:23:35 -0700 | [diff] [blame] | 5248 | if (sm_info->rec_prefree_segments > DEF_MAX_RECLAIM_PREFREE_SEGMENTS) |
| 5249 | sm_info->rec_prefree_segments = DEF_MAX_RECLAIM_PREFREE_SEGMENTS; |
| 5250 | |
Chao Yu | b0332a0 | 2020-02-14 17:44:12 +0800 | [diff] [blame] | 5251 | if (!f2fs_lfs_mode(sbi)) |
Jaegeuk Kim | 52763a4 | 2016-06-13 09:47:48 -0700 | [diff] [blame] | 5252 | sm_info->ipu_policy = 1 << F2FS_IPU_FSYNC; |
Jaegeuk Kim | 216fbd6 | 2013-11-07 13:13:42 +0900 | [diff] [blame] | 5253 | sm_info->min_ipu_util = DEF_MIN_IPU_UTIL; |
Jaegeuk Kim | c1ce1b0 | 2014-09-10 16:53:02 -0700 | [diff] [blame] | 5254 | sm_info->min_fsync_blocks = DEF_MIN_FSYNC_BLOCKS; |
Laibin Qiu | dc675a9 | 2021-07-31 11:26:46 +0800 | [diff] [blame] | 5255 | sm_info->min_seq_blocks = sbi->blocks_per_seg; |
Jaegeuk Kim | ef095d1 | 2017-03-24 20:05:13 -0400 | [diff] [blame] | 5256 | sm_info->min_hot_blocks = DEF_MIN_HOT_BLOCKS; |
Chao Yu | a2a12b6 | 2017-10-28 16:52:33 +0800 | [diff] [blame] | 5257 | sm_info->min_ssr_sections = reserved_sections(sbi); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 5258 | |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 5259 | INIT_LIST_HEAD(&sm_info->sit_entry_set); |
| 5260 | |
Chao Yu | 2b60311 | 2017-11-02 20:41:03 +0800 | [diff] [blame] | 5261 | init_rwsem(&sm_info->curseg_lock); |
| 5262 | |
Yunlei He | d4fdf8b | 2017-06-01 16:43:51 +0800 | [diff] [blame] | 5263 | if (!f2fs_readonly(sbi->sb)) { |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 5264 | err = f2fs_create_flush_cmd_control(sbi); |
Gu Zheng | 2163d19 | 2014-04-27 14:21:33 +0800 | [diff] [blame] | 5265 | if (err) |
Gu Zheng | a688b9d9e | 2014-04-27 14:21:21 +0800 | [diff] [blame] | 5266 | return err; |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 5267 | } |
| 5268 | |
Jaegeuk Kim | 0b54fb8 | 2017-01-11 14:40:24 -0800 | [diff] [blame] | 5269 | err = create_discard_cmd_control(sbi); |
| 5270 | if (err) |
| 5271 | return err; |
| 5272 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 5273 | err = build_sit_info(sbi); |
| 5274 | if (err) |
| 5275 | return err; |
| 5276 | err = build_free_segmap(sbi); |
| 5277 | if (err) |
| 5278 | return err; |
| 5279 | err = build_curseg(sbi); |
| 5280 | if (err) |
| 5281 | return err; |
| 5282 | |
| 5283 | /* reinit free segmap based on SIT */ |
Jaegeuk Kim | c39a1b3 | 2017-12-19 19:16:34 -0800 | [diff] [blame] | 5284 | err = build_sit_entries(sbi); |
| 5285 | if (err) |
| 5286 | return err; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 5287 | |
| 5288 | init_free_segmap(sbi); |
| 5289 | err = build_dirty_segmap(sbi); |
| 5290 | if (err) |
| 5291 | return err; |
| 5292 | |
Chao Yu | c854f4d | 2019-05-25 23:07:25 +0800 | [diff] [blame] | 5293 | err = sanity_check_curseg(sbi); |
| 5294 | if (err) |
| 5295 | return err; |
| 5296 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 5297 | init_min_max_mtime(sbi); |
| 5298 | return 0; |
| 5299 | } |
| 5300 | |
| 5301 | static void discard_dirty_segmap(struct f2fs_sb_info *sbi, |
| 5302 | enum dirty_type dirty_type) |
| 5303 | { |
| 5304 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
| 5305 | |
| 5306 | mutex_lock(&dirty_i->seglist_lock); |
Jaegeuk Kim | 39307a8 | 2015-09-22 13:50:47 -0700 | [diff] [blame] | 5307 | kvfree(dirty_i->dirty_segmap[dirty_type]); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 5308 | dirty_i->nr_dirty[dirty_type] = 0; |
| 5309 | mutex_unlock(&dirty_i->seglist_lock); |
| 5310 | } |
| 5311 | |
Jaegeuk Kim | 5ec4e49 | 2013-03-31 13:26:03 +0900 | [diff] [blame] | 5312 | static void destroy_victim_secmap(struct f2fs_sb_info *sbi) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 5313 | { |
| 5314 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
Yi Zhuang | 5f029c0 | 2021-04-06 09:47:35 +0800 | [diff] [blame] | 5315 | |
Jaegeuk Kim | 39307a8 | 2015-09-22 13:50:47 -0700 | [diff] [blame] | 5316 | kvfree(dirty_i->victim_secmap); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 5317 | } |
| 5318 | |
| 5319 | static void destroy_dirty_segmap(struct f2fs_sb_info *sbi) |
| 5320 | { |
| 5321 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
| 5322 | int i; |
| 5323 | |
| 5324 | if (!dirty_i) |
| 5325 | return; |
| 5326 | |
| 5327 | /* discard pre-free/dirty segments list */ |
| 5328 | for (i = 0; i < NR_DIRTY_TYPE; i++) |
| 5329 | discard_dirty_segmap(sbi, i); |
| 5330 | |
Jack Qiu | da52f8a | 2020-06-18 12:37:10 +0800 | [diff] [blame] | 5331 | if (__is_large_section(sbi)) { |
| 5332 | mutex_lock(&dirty_i->seglist_lock); |
| 5333 | kvfree(dirty_i->dirty_secmap); |
| 5334 | mutex_unlock(&dirty_i->seglist_lock); |
| 5335 | } |
| 5336 | |
Jaegeuk Kim | 5ec4e49 | 2013-03-31 13:26:03 +0900 | [diff] [blame] | 5337 | destroy_victim_secmap(sbi); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 5338 | SM_I(sbi)->dirty_info = NULL; |
Chao Yu | c8eb702 | 2020-09-14 16:47:00 +0800 | [diff] [blame] | 5339 | kfree(dirty_i); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 5340 | } |
| 5341 | |
| 5342 | static void destroy_curseg(struct f2fs_sb_info *sbi) |
| 5343 | { |
| 5344 | struct curseg_info *array = SM_I(sbi)->curseg_array; |
| 5345 | int i; |
| 5346 | |
| 5347 | if (!array) |
| 5348 | return; |
| 5349 | SM_I(sbi)->curseg_array = NULL; |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 5350 | for (i = 0; i < NR_CURSEG_TYPE; i++) { |
Chao Yu | c8eb702 | 2020-09-14 16:47:00 +0800 | [diff] [blame] | 5351 | kfree(array[i].sum_blk); |
| 5352 | kfree(array[i].journal); |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 5353 | } |
Chao Yu | c8eb702 | 2020-09-14 16:47:00 +0800 | [diff] [blame] | 5354 | kfree(array); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 5355 | } |
| 5356 | |
| 5357 | static void destroy_free_segmap(struct f2fs_sb_info *sbi) |
| 5358 | { |
| 5359 | struct free_segmap_info *free_i = SM_I(sbi)->free_info; |
Yi Zhuang | 5f029c0 | 2021-04-06 09:47:35 +0800 | [diff] [blame] | 5360 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 5361 | if (!free_i) |
| 5362 | return; |
| 5363 | SM_I(sbi)->free_info = NULL; |
Jaegeuk Kim | 39307a8 | 2015-09-22 13:50:47 -0700 | [diff] [blame] | 5364 | kvfree(free_i->free_segmap); |
| 5365 | kvfree(free_i->free_secmap); |
Chao Yu | c8eb702 | 2020-09-14 16:47:00 +0800 | [diff] [blame] | 5366 | kfree(free_i); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 5367 | } |
| 5368 | |
| 5369 | static void destroy_sit_info(struct f2fs_sb_info *sbi) |
| 5370 | { |
| 5371 | struct sit_info *sit_i = SIT_I(sbi); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 5372 | |
| 5373 | if (!sit_i) |
| 5374 | return; |
| 5375 | |
Chao Yu | 2fde3dd | 2019-07-26 15:41:20 +0800 | [diff] [blame] | 5376 | if (sit_i->sentries) |
| 5377 | kvfree(sit_i->bitmap); |
Chao Yu | c8eb702 | 2020-09-14 16:47:00 +0800 | [diff] [blame] | 5378 | kfree(sit_i->tmp_map); |
Jaegeuk Kim | 60a3b78 | 2015-02-10 16:44:29 -0800 | [diff] [blame] | 5379 | |
Jaegeuk Kim | 39307a8 | 2015-09-22 13:50:47 -0700 | [diff] [blame] | 5380 | kvfree(sit_i->sentries); |
| 5381 | kvfree(sit_i->sec_entries); |
| 5382 | kvfree(sit_i->dirty_sentries_bitmap); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 5383 | |
| 5384 | SM_I(sbi)->sit_info = NULL; |
Jaegeuk Kim | 5222595 | 2018-12-13 18:38:33 -0800 | [diff] [blame] | 5385 | kvfree(sit_i->sit_bitmap); |
Chao Yu | ae27d62 | 2017-01-07 18:52:34 +0800 | [diff] [blame] | 5386 | #ifdef CONFIG_F2FS_CHECK_FS |
Jaegeuk Kim | 5222595 | 2018-12-13 18:38:33 -0800 | [diff] [blame] | 5387 | kvfree(sit_i->sit_bitmap_mir); |
Sahitya Tummala | bbf9f7d | 2019-08-07 19:10:32 +0530 | [diff] [blame] | 5388 | kvfree(sit_i->invalid_segmap); |
Chao Yu | ae27d62 | 2017-01-07 18:52:34 +0800 | [diff] [blame] | 5389 | #endif |
Chao Yu | c8eb702 | 2020-09-14 16:47:00 +0800 | [diff] [blame] | 5390 | kfree(sit_i); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 5391 | } |
| 5392 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 5393 | void f2fs_destroy_segment_manager(struct f2fs_sb_info *sbi) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 5394 | { |
| 5395 | struct f2fs_sm_info *sm_info = SM_I(sbi); |
Gu Zheng | a688b9d9e | 2014-04-27 14:21:21 +0800 | [diff] [blame] | 5396 | |
Chao Yu | 3b03f72 | 2013-11-06 09:12:04 +0800 | [diff] [blame] | 5397 | if (!sm_info) |
| 5398 | return; |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 5399 | f2fs_destroy_flush_cmd_control(sbi, true); |
Chao Yu | f099405 | 2017-03-27 18:14:04 +0800 | [diff] [blame] | 5400 | destroy_discard_cmd_control(sbi); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 5401 | destroy_dirty_segmap(sbi); |
| 5402 | destroy_curseg(sbi); |
| 5403 | destroy_free_segmap(sbi); |
| 5404 | destroy_sit_info(sbi); |
| 5405 | sbi->sm_info = NULL; |
Chao Yu | c8eb702 | 2020-09-14 16:47:00 +0800 | [diff] [blame] | 5406 | kfree(sm_info); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 5407 | } |
Jaegeuk Kim | 7fd9e54 | 2013-11-15 13:55:58 +0900 | [diff] [blame] | 5408 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 5409 | int __init f2fs_create_segment_manager_caches(void) |
Jaegeuk Kim | 7fd9e54 | 2013-11-15 13:55:58 +0900 | [diff] [blame] | 5410 | { |
Chao Yu | 9851000 | 2020-02-17 17:46:20 +0800 | [diff] [blame] | 5411 | discard_entry_slab = f2fs_kmem_cache_create("f2fs_discard_entry", |
Gu Zheng | e8512d2 | 2014-03-07 18:43:28 +0800 | [diff] [blame] | 5412 | sizeof(struct discard_entry)); |
Jaegeuk Kim | 7fd9e54 | 2013-11-15 13:55:58 +0900 | [diff] [blame] | 5413 | if (!discard_entry_slab) |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 5414 | goto fail; |
| 5415 | |
Chao Yu | 9851000 | 2020-02-17 17:46:20 +0800 | [diff] [blame] | 5416 | discard_cmd_slab = f2fs_kmem_cache_create("f2fs_discard_cmd", |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 5417 | sizeof(struct discard_cmd)); |
| 5418 | if (!discard_cmd_slab) |
Chao Yu | 6ab2a30 | 2016-09-05 12:28:26 +0800 | [diff] [blame] | 5419 | goto destroy_discard_entry; |
Chao Yu | 275b66b | 2016-08-29 23:58:34 +0800 | [diff] [blame] | 5420 | |
Chao Yu | 9851000 | 2020-02-17 17:46:20 +0800 | [diff] [blame] | 5421 | sit_entry_set_slab = f2fs_kmem_cache_create("f2fs_sit_entry_set", |
Changman Lee | c9ee008 | 2014-11-21 15:42:07 +0900 | [diff] [blame] | 5422 | sizeof(struct sit_entry_set)); |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 5423 | if (!sit_entry_set_slab) |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 5424 | goto destroy_discard_cmd; |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 5425 | |
Chao Yu | 9851000 | 2020-02-17 17:46:20 +0800 | [diff] [blame] | 5426 | inmem_entry_slab = f2fs_kmem_cache_create("f2fs_inmem_page_entry", |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 5427 | sizeof(struct inmem_pages)); |
| 5428 | if (!inmem_entry_slab) |
| 5429 | goto destroy_sit_entry_set; |
Jaegeuk Kim | 7fd9e54 | 2013-11-15 13:55:58 +0900 | [diff] [blame] | 5430 | return 0; |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 5431 | |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 5432 | destroy_sit_entry_set: |
| 5433 | kmem_cache_destroy(sit_entry_set_slab); |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 5434 | destroy_discard_cmd: |
| 5435 | kmem_cache_destroy(discard_cmd_slab); |
Chao Yu | 6ab2a30 | 2016-09-05 12:28:26 +0800 | [diff] [blame] | 5436 | destroy_discard_entry: |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 5437 | kmem_cache_destroy(discard_entry_slab); |
| 5438 | fail: |
| 5439 | return -ENOMEM; |
Jaegeuk Kim | 7fd9e54 | 2013-11-15 13:55:58 +0900 | [diff] [blame] | 5440 | } |
| 5441 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 5442 | void f2fs_destroy_segment_manager_caches(void) |
Jaegeuk Kim | 7fd9e54 | 2013-11-15 13:55:58 +0900 | [diff] [blame] | 5443 | { |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 5444 | kmem_cache_destroy(sit_entry_set_slab); |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 5445 | kmem_cache_destroy(discard_cmd_slab); |
Jaegeuk Kim | 7fd9e54 | 2013-11-15 13:55:58 +0900 | [diff] [blame] | 5446 | kmem_cache_destroy(discard_entry_slab); |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 5447 | kmem_cache_destroy(inmem_entry_slab); |
Jaegeuk Kim | 7fd9e54 | 2013-11-15 13:55:58 +0900 | [diff] [blame] | 5448 | } |