Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 1 | /* |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2 | * fs/f2fs/segment.c |
| 3 | * |
| 4 | * Copyright (c) 2012 Samsung Electronics Co., Ltd. |
| 5 | * http://www.samsung.com/ |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | #include <linux/fs.h> |
| 12 | #include <linux/f2fs_fs.h> |
| 13 | #include <linux/bio.h> |
| 14 | #include <linux/blkdev.h> |
Geert Uytterhoeven | 690e4a3 | 2012-12-19 22:19:30 +0100 | [diff] [blame] | 15 | #include <linux/prefetch.h> |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 16 | #include <linux/kthread.h> |
Chao Yu | 74de593 | 2013-11-22 09:09:59 +0800 | [diff] [blame] | 17 | #include <linux/swap.h> |
Jaegeuk Kim | 60b99b4 | 2015-10-05 14:49:57 -0700 | [diff] [blame] | 18 | #include <linux/timer.h> |
Jaegeuk Kim | 1d7be27 | 2017-05-17 10:36:58 -0700 | [diff] [blame] | 19 | #include <linux/freezer.h> |
Jaegeuk Kim | 1eb1ef4 | 2017-09-09 12:03:23 -0700 | [diff] [blame] | 20 | #include <linux/sched/signal.h> |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 21 | |
| 22 | #include "f2fs.h" |
| 23 | #include "segment.h" |
| 24 | #include "node.h" |
Jaegeuk Kim | 5f65654 | 2017-08-15 21:27:19 -0700 | [diff] [blame] | 25 | #include "gc.h" |
Jaegeuk Kim | 9e4ded3 | 2014-12-17 19:58:58 -0800 | [diff] [blame] | 26 | #include "trace.h" |
Namjae Jeon | 6ec178d | 2013-04-23 17:51:43 +0900 | [diff] [blame] | 27 | #include <trace/events/f2fs.h> |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 28 | |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 29 | #define __reverse_ffz(x) __reverse_ffs(~(x)) |
| 30 | |
Jaegeuk Kim | 7fd9e54 | 2013-11-15 13:55:58 +0900 | [diff] [blame] | 31 | static struct kmem_cache *discard_entry_slab; |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 32 | static struct kmem_cache *discard_cmd_slab; |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 33 | static struct kmem_cache *sit_entry_set_slab; |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 34 | static struct kmem_cache *inmem_entry_slab; |
Jaegeuk Kim | 7fd9e54 | 2013-11-15 13:55:58 +0900 | [diff] [blame] | 35 | |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 36 | static unsigned long __reverse_ulong(unsigned char *str) |
| 37 | { |
| 38 | unsigned long tmp = 0; |
| 39 | int shift = 24, idx = 0; |
| 40 | |
| 41 | #if BITS_PER_LONG == 64 |
| 42 | shift = 56; |
| 43 | #endif |
| 44 | while (shift >= 0) { |
| 45 | tmp |= (unsigned long)str[idx++] << shift; |
| 46 | shift -= BITS_PER_BYTE; |
| 47 | } |
| 48 | return tmp; |
| 49 | } |
| 50 | |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 51 | /* |
| 52 | * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since |
| 53 | * MSB and LSB are reversed in a byte by f2fs_set_bit. |
| 54 | */ |
| 55 | static inline unsigned long __reverse_ffs(unsigned long word) |
| 56 | { |
| 57 | int num = 0; |
| 58 | |
| 59 | #if BITS_PER_LONG == 64 |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 60 | if ((word & 0xffffffff00000000UL) == 0) |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 61 | num += 32; |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 62 | else |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 63 | word >>= 32; |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 64 | #endif |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 65 | if ((word & 0xffff0000) == 0) |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 66 | num += 16; |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 67 | else |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 68 | word >>= 16; |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 69 | |
| 70 | if ((word & 0xff00) == 0) |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 71 | num += 8; |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 72 | else |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 73 | word >>= 8; |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 74 | |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 75 | if ((word & 0xf0) == 0) |
| 76 | num += 4; |
| 77 | else |
| 78 | word >>= 4; |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 79 | |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 80 | if ((word & 0xc) == 0) |
| 81 | num += 2; |
| 82 | else |
| 83 | word >>= 2; |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 84 | |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 85 | if ((word & 0x2) == 0) |
| 86 | num += 1; |
| 87 | return num; |
| 88 | } |
| 89 | |
| 90 | /* |
arter97 | e1c4204 | 2014-08-06 23:22:50 +0900 | [diff] [blame] | 91 | * __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] | 92 | * f2fs_set_bit makes MSB and LSB reversed in a byte. |
Fan Li | 692223d | 2015-11-12 08:43:04 +0800 | [diff] [blame] | 93 | * @size must be integral times of unsigned long. |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 94 | * Example: |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 95 | * MSB <--> LSB |
| 96 | * f2fs_set_bit(0, bitmap) => 1000 0000 |
| 97 | * f2fs_set_bit(7, bitmap) => 0000 0001 |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 98 | */ |
| 99 | static unsigned long __find_rev_next_bit(const unsigned long *addr, |
| 100 | unsigned long size, unsigned long offset) |
| 101 | { |
| 102 | const unsigned long *p = addr + BIT_WORD(offset); |
Fan Li | 692223d | 2015-11-12 08:43:04 +0800 | [diff] [blame] | 103 | unsigned long result = size; |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 104 | unsigned long tmp; |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 105 | |
| 106 | if (offset >= size) |
| 107 | return size; |
| 108 | |
Fan Li | 692223d | 2015-11-12 08:43:04 +0800 | [diff] [blame] | 109 | size -= (offset & ~(BITS_PER_LONG - 1)); |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 110 | offset %= BITS_PER_LONG; |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 111 | |
Fan Li | 692223d | 2015-11-12 08:43:04 +0800 | [diff] [blame] | 112 | while (1) { |
| 113 | if (*p == 0) |
| 114 | goto pass; |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 115 | |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 116 | tmp = __reverse_ulong((unsigned char *)p); |
Fan Li | 692223d | 2015-11-12 08:43:04 +0800 | [diff] [blame] | 117 | |
| 118 | tmp &= ~0UL >> offset; |
| 119 | if (size < BITS_PER_LONG) |
| 120 | tmp &= (~0UL << (BITS_PER_LONG - size)); |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 121 | if (tmp) |
Fan Li | 692223d | 2015-11-12 08:43:04 +0800 | [diff] [blame] | 122 | goto found; |
| 123 | pass: |
| 124 | if (size <= BITS_PER_LONG) |
| 125 | break; |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 126 | size -= BITS_PER_LONG; |
Fan Li | 692223d | 2015-11-12 08:43:04 +0800 | [diff] [blame] | 127 | offset = 0; |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 128 | p++; |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 129 | } |
Fan Li | 692223d | 2015-11-12 08:43:04 +0800 | [diff] [blame] | 130 | return result; |
| 131 | found: |
| 132 | return result - size + __reverse_ffs(tmp); |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | static unsigned long __find_rev_next_zero_bit(const unsigned long *addr, |
| 136 | unsigned long size, unsigned long offset) |
| 137 | { |
| 138 | const unsigned long *p = addr + BIT_WORD(offset); |
Jaegeuk Kim | 8060944 | 2015-12-04 16:51:13 -0800 | [diff] [blame] | 139 | unsigned long result = size; |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 140 | unsigned long tmp; |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 141 | |
| 142 | if (offset >= size) |
| 143 | return size; |
| 144 | |
Jaegeuk Kim | 8060944 | 2015-12-04 16:51:13 -0800 | [diff] [blame] | 145 | size -= (offset & ~(BITS_PER_LONG - 1)); |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 146 | offset %= BITS_PER_LONG; |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 147 | |
Jaegeuk Kim | 8060944 | 2015-12-04 16:51:13 -0800 | [diff] [blame] | 148 | while (1) { |
| 149 | if (*p == ~0UL) |
| 150 | goto pass; |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 151 | |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 152 | tmp = __reverse_ulong((unsigned char *)p); |
Jaegeuk Kim | 8060944 | 2015-12-04 16:51:13 -0800 | [diff] [blame] | 153 | |
| 154 | if (offset) |
| 155 | tmp |= ~0UL << (BITS_PER_LONG - offset); |
| 156 | if (size < BITS_PER_LONG) |
| 157 | tmp |= ~0UL >> size; |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 158 | if (tmp != ~0UL) |
Jaegeuk Kim | 8060944 | 2015-12-04 16:51:13 -0800 | [diff] [blame] | 159 | goto found; |
| 160 | pass: |
| 161 | if (size <= BITS_PER_LONG) |
| 162 | break; |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 163 | size -= BITS_PER_LONG; |
Jaegeuk Kim | 8060944 | 2015-12-04 16:51:13 -0800 | [diff] [blame] | 164 | offset = 0; |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 165 | p++; |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 166 | } |
Jaegeuk Kim | 8060944 | 2015-12-04 16:51:13 -0800 | [diff] [blame] | 167 | return result; |
| 168 | found: |
| 169 | return result - size + __reverse_ffz(tmp); |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 170 | } |
| 171 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 172 | bool f2fs_need_SSR(struct f2fs_sb_info *sbi) |
Jaegeuk Kim | b3a97a2 | 2017-09-09 11:11:04 -0700 | [diff] [blame] | 173 | { |
| 174 | int node_secs = get_blocktype_secs(sbi, F2FS_DIRTY_NODES); |
| 175 | int dent_secs = get_blocktype_secs(sbi, F2FS_DIRTY_DENTS); |
| 176 | int imeta_secs = get_blocktype_secs(sbi, F2FS_DIRTY_IMETA); |
| 177 | |
| 178 | if (test_opt(sbi, LFS)) |
| 179 | return false; |
Jaegeuk Kim | 5b0e953 | 2018-05-07 14:22:40 -0700 | [diff] [blame] | 180 | if (sbi->gc_mode == GC_URGENT) |
Jaegeuk Kim | b3a97a2 | 2017-09-09 11:11:04 -0700 | [diff] [blame] | 181 | return true; |
| 182 | |
| 183 | return free_sections(sbi) <= (node_secs + 2 * dent_secs + imeta_secs + |
Chao Yu | a2a12b6 | 2017-10-28 16:52:33 +0800 | [diff] [blame] | 184 | SM_I(sbi)->min_ssr_sections + reserved_sections(sbi)); |
Jaegeuk Kim | b3a97a2 | 2017-09-09 11:11:04 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 187 | void f2fs_register_inmem_page(struct inode *inode, struct page *page) |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 188 | { |
Jaegeuk Kim | 57864ae | 2017-10-18 19:05:57 -0700 | [diff] [blame] | 189 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 190 | struct f2fs_inode_info *fi = F2FS_I(inode); |
| 191 | struct inmem_pages *new; |
Jaegeuk Kim | 9be32d7 | 2014-12-05 10:39:49 -0800 | [diff] [blame] | 192 | |
Jaegeuk Kim | 9e4ded3 | 2014-12-17 19:58:58 -0800 | [diff] [blame] | 193 | f2fs_trace_pid(page); |
Jaegeuk Kim | 0722b10 | 2014-12-05 11:58:02 -0800 | [diff] [blame] | 194 | |
Chao Yu | decd36b | 2015-08-07 18:42:09 +0800 | [diff] [blame] | 195 | set_page_private(page, (unsigned long)ATOMIC_WRITTEN_PAGE); |
| 196 | SetPagePrivate(page); |
| 197 | |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 198 | new = f2fs_kmem_cache_alloc(inmem_entry_slab, GFP_NOFS); |
| 199 | |
| 200 | /* add atomic page indices to the list */ |
| 201 | new->page = page; |
| 202 | INIT_LIST_HEAD(&new->list); |
Chao Yu | decd36b | 2015-08-07 18:42:09 +0800 | [diff] [blame] | 203 | |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 204 | /* increase reference count with clean state */ |
| 205 | mutex_lock(&fi->inmem_lock); |
| 206 | get_page(page); |
| 207 | list_add_tail(&new->list, &fi->inmem_pages); |
Jaegeuk Kim | 57864ae | 2017-10-18 19:05:57 -0700 | [diff] [blame] | 208 | spin_lock(&sbi->inode_lock[ATOMIC_FILE]); |
| 209 | if (list_empty(&fi->inmem_ilist)) |
| 210 | list_add_tail(&fi->inmem_ilist, &sbi->inode_list[ATOMIC_FILE]); |
| 211 | spin_unlock(&sbi->inode_lock[ATOMIC_FILE]); |
Jaegeuk Kim | 8dcf2ff7 | 2014-12-05 17:18:15 -0800 | [diff] [blame] | 212 | inc_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES); |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 213 | mutex_unlock(&fi->inmem_lock); |
Jaegeuk Kim | 8ce67cb | 2015-03-17 17:58:08 -0700 | [diff] [blame] | 214 | |
| 215 | trace_f2fs_register_inmem_page(page, INMEM); |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 218 | static int __revoke_inmem_pages(struct inode *inode, |
| 219 | struct list_head *head, bool drop, bool recover) |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 220 | { |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 221 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 222 | struct inmem_pages *cur, *tmp; |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 223 | int err = 0; |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 224 | |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 225 | list_for_each_entry_safe(cur, tmp, head, list) { |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 226 | struct page *page = cur->page; |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 227 | |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 228 | if (drop) |
| 229 | trace_f2fs_commit_inmem_page(page, INMEM_DROP); |
| 230 | |
| 231 | lock_page(page); |
| 232 | |
Chao Yu | e5e5732 | 2018-04-23 10:36:13 +0800 | [diff] [blame] | 233 | f2fs_wait_on_page_writeback(page, DATA, true); |
| 234 | |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 235 | if (recover) { |
| 236 | struct dnode_of_data dn; |
| 237 | struct node_info ni; |
| 238 | |
| 239 | trace_f2fs_commit_inmem_page(page, INMEM_REVOKE); |
Chao Yu | 7f2b4e8 | 2017-08-08 19:09:08 +0800 | [diff] [blame] | 240 | retry: |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 241 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 242 | err = f2fs_get_dnode_of_data(&dn, page->index, |
| 243 | LOOKUP_NODE); |
Chao Yu | 7f2b4e8 | 2017-08-08 19:09:08 +0800 | [diff] [blame] | 244 | if (err) { |
| 245 | if (err == -ENOMEM) { |
| 246 | congestion_wait(BLK_RW_ASYNC, HZ/50); |
| 247 | cond_resched(); |
| 248 | goto retry; |
| 249 | } |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 250 | err = -EAGAIN; |
| 251 | goto next; |
| 252 | } |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 253 | |
| 254 | err = f2fs_get_node_info(sbi, dn.nid, &ni); |
| 255 | if (err) { |
| 256 | f2fs_put_dnode(&dn); |
| 257 | return err; |
| 258 | } |
| 259 | |
Daeho Jeong | f1d2564 | 2018-01-10 16:49:10 +0900 | [diff] [blame] | 260 | if (cur->old_addr == NEW_ADDR) { |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 261 | f2fs_invalidate_blocks(sbi, dn.data_blkaddr); |
Daeho Jeong | f1d2564 | 2018-01-10 16:49:10 +0900 | [diff] [blame] | 262 | f2fs_update_data_blkaddr(&dn, NEW_ADDR); |
| 263 | } else |
| 264 | f2fs_replace_block(sbi, &dn, dn.data_blkaddr, |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 265 | cur->old_addr, ni.version, true, true); |
| 266 | f2fs_put_dnode(&dn); |
| 267 | } |
| 268 | next: |
Jaegeuk Kim | 63c52d7 | 2016-04-12 14:11:03 -0700 | [diff] [blame] | 269 | /* we don't need to invalidate this in the sccessful status */ |
| 270 | if (drop || recover) |
| 271 | ClearPageUptodate(page); |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 272 | set_page_private(page, 0); |
Chao Yu | c81ced0 | 2016-04-29 20:13:36 +0800 | [diff] [blame] | 273 | ClearPagePrivate(page); |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 274 | f2fs_put_page(page, 1); |
Chao Yu | decd36b | 2015-08-07 18:42:09 +0800 | [diff] [blame] | 275 | |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 276 | list_del(&cur->list); |
| 277 | kmem_cache_free(inmem_entry_slab, cur); |
Jaegeuk Kim | 8dcf2ff7 | 2014-12-05 17:18:15 -0800 | [diff] [blame] | 278 | dec_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES); |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 279 | } |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 280 | return err; |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 281 | } |
| 282 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 283 | 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] | 284 | { |
| 285 | struct list_head *head = &sbi->inode_list[ATOMIC_FILE]; |
| 286 | struct inode *inode; |
| 287 | struct f2fs_inode_info *fi; |
| 288 | next: |
| 289 | spin_lock(&sbi->inode_lock[ATOMIC_FILE]); |
| 290 | if (list_empty(head)) { |
| 291 | spin_unlock(&sbi->inode_lock[ATOMIC_FILE]); |
| 292 | return; |
| 293 | } |
| 294 | fi = list_first_entry(head, struct f2fs_inode_info, inmem_ilist); |
| 295 | inode = igrab(&fi->vfs_inode); |
| 296 | spin_unlock(&sbi->inode_lock[ATOMIC_FILE]); |
| 297 | |
| 298 | if (inode) { |
Chao Yu | 2ef79ec | 2018-05-07 20:28:54 +0800 | [diff] [blame] | 299 | if (gc_failure) { |
| 300 | if (fi->i_gc_failures[GC_FAILURE_ATOMIC]) |
| 301 | goto drop; |
| 302 | goto skip; |
| 303 | } |
| 304 | drop: |
| 305 | set_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST); |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 306 | f2fs_drop_inmem_pages(inode); |
Jaegeuk Kim | 57864ae | 2017-10-18 19:05:57 -0700 | [diff] [blame] | 307 | iput(inode); |
| 308 | } |
Chao Yu | 2ef79ec | 2018-05-07 20:28:54 +0800 | [diff] [blame] | 309 | skip: |
Jaegeuk Kim | 57864ae | 2017-10-18 19:05:57 -0700 | [diff] [blame] | 310 | congestion_wait(BLK_RW_ASYNC, HZ/50); |
| 311 | cond_resched(); |
| 312 | goto next; |
| 313 | } |
| 314 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 315 | void f2fs_drop_inmem_pages(struct inode *inode) |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 316 | { |
Jaegeuk Kim | 57864ae | 2017-10-18 19:05:57 -0700 | [diff] [blame] | 317 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 318 | struct f2fs_inode_info *fi = F2FS_I(inode); |
| 319 | |
| 320 | mutex_lock(&fi->inmem_lock); |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 321 | __revoke_inmem_pages(inode, &fi->inmem_pages, true, false); |
Jaegeuk Kim | 57864ae | 2017-10-18 19:05:57 -0700 | [diff] [blame] | 322 | spin_lock(&sbi->inode_lock[ATOMIC_FILE]); |
| 323 | if (!list_empty(&fi->inmem_ilist)) |
| 324 | list_del_init(&fi->inmem_ilist); |
| 325 | spin_unlock(&sbi->inode_lock[ATOMIC_FILE]); |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 326 | mutex_unlock(&fi->inmem_lock); |
Chao Yu | 5fe4574 | 2017-01-07 18:50:26 +0800 | [diff] [blame] | 327 | |
| 328 | clear_inode_flag(inode, FI_ATOMIC_FILE); |
Chao Yu | 2ef79ec | 2018-05-07 20:28:54 +0800 | [diff] [blame] | 329 | fi->i_gc_failures[GC_FAILURE_ATOMIC] = 0; |
Chao Yu | 5fe4574 | 2017-01-07 18:50:26 +0800 | [diff] [blame] | 330 | stat_dec_atomic_write(inode); |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 331 | } |
| 332 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 333 | void f2fs_drop_inmem_page(struct inode *inode, struct page *page) |
Jaegeuk Kim | 8c242db | 2017-03-17 09:55:52 +0800 | [diff] [blame] | 334 | { |
| 335 | struct f2fs_inode_info *fi = F2FS_I(inode); |
| 336 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 337 | struct list_head *head = &fi->inmem_pages; |
| 338 | struct inmem_pages *cur = NULL; |
| 339 | |
| 340 | f2fs_bug_on(sbi, !IS_ATOMIC_WRITTEN_PAGE(page)); |
| 341 | |
| 342 | mutex_lock(&fi->inmem_lock); |
| 343 | list_for_each_entry(cur, head, list) { |
| 344 | if (cur->page == page) |
| 345 | break; |
| 346 | } |
| 347 | |
Sheng Yong | d0891e8 | 2018-04-17 17:12:27 +0800 | [diff] [blame] | 348 | f2fs_bug_on(sbi, list_empty(head) || cur->page != page); |
Jaegeuk Kim | 8c242db | 2017-03-17 09:55:52 +0800 | [diff] [blame] | 349 | list_del(&cur->list); |
| 350 | mutex_unlock(&fi->inmem_lock); |
| 351 | |
| 352 | dec_page_count(sbi, F2FS_INMEM_PAGES); |
| 353 | kmem_cache_free(inmem_entry_slab, cur); |
| 354 | |
| 355 | ClearPageUptodate(page); |
| 356 | set_page_private(page, 0); |
| 357 | ClearPagePrivate(page); |
| 358 | f2fs_put_page(page, 0); |
| 359 | |
| 360 | trace_f2fs_commit_inmem_page(page, INMEM_INVALIDATE); |
| 361 | } |
| 362 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 363 | static int __f2fs_commit_inmem_pages(struct inode *inode) |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 364 | { |
| 365 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 366 | struct f2fs_inode_info *fi = F2FS_I(inode); |
| 367 | struct inmem_pages *cur, *tmp; |
| 368 | struct f2fs_io_info fio = { |
| 369 | .sbi = sbi, |
Chao Yu | 39d787b | 2017-09-29 13:59:38 +0800 | [diff] [blame] | 370 | .ino = inode->i_ino, |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 371 | .type = DATA, |
Mike Christie | 04d328d | 2016-06-05 14:31:55 -0500 | [diff] [blame] | 372 | .op = REQ_OP_WRITE, |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 373 | .op_flags = REQ_SYNC | REQ_PRIO, |
Chao Yu | b0af6d4 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 374 | .io_type = FS_DATA_IO, |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 375 | }; |
Chao Yu | cf52b27 | 2018-04-23 10:36:14 +0800 | [diff] [blame] | 376 | struct list_head revoke_list; |
Jaegeuk Kim | 942fd31 | 2017-02-01 16:51:22 -0800 | [diff] [blame] | 377 | pgoff_t last_idx = ULONG_MAX; |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 378 | int err = 0; |
| 379 | |
Chao Yu | cf52b27 | 2018-04-23 10:36:14 +0800 | [diff] [blame] | 380 | INIT_LIST_HEAD(&revoke_list); |
| 381 | |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 382 | list_for_each_entry_safe(cur, tmp, &fi->inmem_pages, list) { |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 383 | struct page *page = cur->page; |
| 384 | |
| 385 | lock_page(page); |
| 386 | if (page->mapping == inode->i_mapping) { |
| 387 | trace_f2fs_commit_inmem_page(page, INMEM); |
| 388 | |
| 389 | set_page_dirty(page); |
| 390 | f2fs_wait_on_page_writeback(page, DATA, true); |
Chao Yu | 933439c | 2016-10-11 22:57:01 +0800 | [diff] [blame] | 391 | if (clear_page_dirty_for_io(page)) { |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 392 | inode_dec_dirty_pages(inode); |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 393 | f2fs_remove_dirty_inode(inode); |
Chao Yu | 933439c | 2016-10-11 22:57:01 +0800 | [diff] [blame] | 394 | } |
Jaegeuk Kim | 640cc18 | 2017-07-19 10:59:55 -0700 | [diff] [blame] | 395 | retry: |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 396 | fio.page = page; |
Hou Pengyang | e959c8f | 2017-04-25 12:45:13 +0000 | [diff] [blame] | 397 | fio.old_blkaddr = NULL_ADDR; |
Jaegeuk Kim | 4d97807 | 2017-04-26 11:11:12 -0700 | [diff] [blame] | 398 | fio.encrypted_page = NULL; |
Jaegeuk Kim | cc15620 | 2017-05-12 13:51:34 -0700 | [diff] [blame] | 399 | fio.need_lock = LOCK_DONE; |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 400 | err = f2fs_do_write_data_page(&fio); |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 401 | if (err) { |
Jaegeuk Kim | 640cc18 | 2017-07-19 10:59:55 -0700 | [diff] [blame] | 402 | if (err == -ENOMEM) { |
| 403 | congestion_wait(BLK_RW_ASYNC, HZ/50); |
| 404 | cond_resched(); |
| 405 | goto retry; |
| 406 | } |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 407 | unlock_page(page); |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 408 | break; |
| 409 | } |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 410 | /* record old blkaddr for revoking */ |
| 411 | cur->old_addr = fio.old_blkaddr; |
Jaegeuk Kim | 942fd31 | 2017-02-01 16:51:22 -0800 | [diff] [blame] | 412 | last_idx = page->index; |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 413 | } |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 414 | unlock_page(page); |
Chao Yu | cf52b27 | 2018-04-23 10:36:14 +0800 | [diff] [blame] | 415 | list_move_tail(&cur->list, &revoke_list); |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 416 | } |
| 417 | |
Jaegeuk Kim | 942fd31 | 2017-02-01 16:51:22 -0800 | [diff] [blame] | 418 | if (last_idx != ULONG_MAX) |
Jaegeuk Kim | b9109b0 | 2017-05-10 11:28:38 -0700 | [diff] [blame] | 419 | f2fs_submit_merged_write_cond(sbi, inode, 0, last_idx, DATA); |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 420 | |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 421 | if (err) { |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 422 | /* |
| 423 | * try to revoke all committed pages, but still we could fail |
| 424 | * due to no memory or other reason, if that happened, EAGAIN |
| 425 | * will be returned, which means in such case, transaction is |
| 426 | * already not integrity, caller should use journal to do the |
| 427 | * recovery or rewrite & commit last transaction. For other |
| 428 | * error number, revoking was done by filesystem itself. |
| 429 | */ |
Chao Yu | cf52b27 | 2018-04-23 10:36:14 +0800 | [diff] [blame] | 430 | err = __revoke_inmem_pages(inode, &revoke_list, false, true); |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 431 | |
| 432 | /* drop all uncommitted pages */ |
| 433 | __revoke_inmem_pages(inode, &fi->inmem_pages, true, false); |
Chao Yu | cf52b27 | 2018-04-23 10:36:14 +0800 | [diff] [blame] | 434 | } else { |
| 435 | __revoke_inmem_pages(inode, &revoke_list, false, false); |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 436 | } |
Chao Yu | cf52b27 | 2018-04-23 10:36:14 +0800 | [diff] [blame] | 437 | |
| 438 | return err; |
| 439 | } |
| 440 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 441 | int f2fs_commit_inmem_pages(struct inode *inode) |
Chao Yu | cf52b27 | 2018-04-23 10:36:14 +0800 | [diff] [blame] | 442 | { |
| 443 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 444 | struct f2fs_inode_info *fi = F2FS_I(inode); |
| 445 | int err; |
| 446 | |
| 447 | f2fs_balance_fs(sbi, true); |
| 448 | f2fs_lock_op(sbi); |
| 449 | |
| 450 | set_inode_flag(inode, FI_ATOMIC_COMMIT); |
| 451 | |
| 452 | mutex_lock(&fi->inmem_lock); |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 453 | err = __f2fs_commit_inmem_pages(inode); |
Chao Yu | cf52b27 | 2018-04-23 10:36:14 +0800 | [diff] [blame] | 454 | |
Jaegeuk Kim | 57864ae | 2017-10-18 19:05:57 -0700 | [diff] [blame] | 455 | spin_lock(&sbi->inode_lock[ATOMIC_FILE]); |
| 456 | if (!list_empty(&fi->inmem_ilist)) |
| 457 | list_del_init(&fi->inmem_ilist); |
| 458 | spin_unlock(&sbi->inode_lock[ATOMIC_FILE]); |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 459 | mutex_unlock(&fi->inmem_lock); |
| 460 | |
Chao Yu | 5fe4574 | 2017-01-07 18:50:26 +0800 | [diff] [blame] | 461 | clear_inode_flag(inode, FI_ATOMIC_COMMIT); |
| 462 | |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 463 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | edb27de | 2015-07-25 00:52:52 -0700 | [diff] [blame] | 464 | return err; |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 467 | /* |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 468 | * This function balances dirty node and dentry pages. |
| 469 | * In addition, it controls garbage collection. |
| 470 | */ |
Jaegeuk Kim | 2c4db1a | 2016-01-07 14:15:04 -0800 | [diff] [blame] | 471 | void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 472 | { |
Chao Yu | 5552351 | 2017-02-25 11:08:28 +0800 | [diff] [blame] | 473 | if (time_to_inject(sbi, FAULT_CHECKPOINT)) { |
| 474 | f2fs_show_injection_info(FAULT_CHECKPOINT); |
Chao Yu | 0f34802 | 2016-09-26 19:45:55 +0800 | [diff] [blame] | 475 | f2fs_stop_checkpoint(sbi, false); |
Chao Yu | 5552351 | 2017-02-25 11:08:28 +0800 | [diff] [blame] | 476 | } |
Chao Yu | 0f34802 | 2016-09-26 19:45:55 +0800 | [diff] [blame] | 477 | |
Jaegeuk Kim | e589c2c | 2016-06-02 15:24:24 -0700 | [diff] [blame] | 478 | /* balance_fs_bg is able to be pending */ |
Jaegeuk Kim | a788189 | 2017-04-20 13:51:57 -0700 | [diff] [blame] | 479 | if (need && excess_cached_nats(sbi)) |
Jaegeuk Kim | e589c2c | 2016-06-02 15:24:24 -0700 | [diff] [blame] | 480 | f2fs_balance_fs_bg(sbi); |
| 481 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 482 | /* |
Jaegeuk Kim | 029cd28 | 2012-12-21 17:20:21 +0900 | [diff] [blame] | 483 | * We should do GC or end up with checkpoint, if there are so many dirty |
| 484 | * dir/node pages without enough free segments. |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 485 | */ |
Jaegeuk Kim | 7f3037a | 2016-09-01 12:02:51 -0700 | [diff] [blame] | 486 | if (has_not_enough_free_secs(sbi, 0, 0)) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 487 | mutex_lock(&sbi->gc_mutex); |
Jaegeuk Kim | e066b83 | 2017-04-13 15:17:00 -0700 | [diff] [blame] | 488 | f2fs_gc(sbi, false, false, NULL_SEGNO); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | |
Jaegeuk Kim | 4660f9c | 2013-10-24 14:19:18 +0900 | [diff] [blame] | 492 | void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi) |
| 493 | { |
Chao Yu | 64c74a7 | 2018-05-26 18:03:34 +0800 | [diff] [blame] | 494 | if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) |
| 495 | return; |
| 496 | |
Chao Yu | 1dcc336 | 2015-02-05 17:57:31 +0800 | [diff] [blame] | 497 | /* try to shrink extent cache when there is no enough memory */ |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 498 | if (!f2fs_available_free_memory(sbi, EXTENT_CACHE)) |
Jaegeuk Kim | 554df79 | 2015-06-19 13:41:23 -0700 | [diff] [blame] | 499 | f2fs_shrink_extent_tree(sbi, EXTENT_CACHE_SHRINK_NUMBER); |
Chao Yu | 1dcc336 | 2015-02-05 17:57:31 +0800 | [diff] [blame] | 500 | |
Jaegeuk Kim | 1b38dc8 | 2015-06-19 15:36:07 -0700 | [diff] [blame] | 501 | /* check the # of cached NAT entries */ |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 502 | if (!f2fs_available_free_memory(sbi, NAT_ENTRIES)) |
| 503 | f2fs_try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK); |
Jaegeuk Kim | 1b38dc8 | 2015-06-19 15:36:07 -0700 | [diff] [blame] | 504 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 505 | if (!f2fs_available_free_memory(sbi, FREE_NIDS)) |
| 506 | f2fs_try_to_free_nids(sbi, MAX_FREE_NIDS); |
Jaegeuk Kim | ad4edb8 | 2016-06-16 16:41:49 -0700 | [diff] [blame] | 507 | else |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 508 | f2fs_build_free_nids(sbi, false, false); |
Chao Yu | 3169658 | 2015-07-28 18:33:46 +0800 | [diff] [blame] | 509 | |
Chao Yu | fd8c8ca | 2018-07-25 19:16:21 +0800 | [diff] [blame] | 510 | if (!is_idle(sbi) && |
| 511 | (!excess_dirty_nats(sbi) && !excess_dirty_nodes(sbi))) |
Jaegeuk Kim | f455c8a | 2016-12-05 11:37:14 -0800 | [diff] [blame] | 512 | return; |
Jaegeuk Kim | e5e7ea3 | 2014-11-06 15:24:46 -0800 | [diff] [blame] | 513 | |
Jaegeuk Kim | 88a70a6 | 2014-12-10 15:20:48 -0800 | [diff] [blame] | 514 | /* checkpoint is the only way to shrink partial cached entries */ |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 515 | if (!f2fs_available_free_memory(sbi, NAT_ENTRIES) || |
| 516 | !f2fs_available_free_memory(sbi, INO_ENTRIES) || |
Chao Yu | 7d768d2 | 2016-01-18 18:31:18 +0800 | [diff] [blame] | 517 | excess_prefree_segs(sbi) || |
| 518 | excess_dirty_nats(sbi) || |
Chao Yu | fd8c8ca | 2018-07-25 19:16:21 +0800 | [diff] [blame] | 519 | excess_dirty_nodes(sbi) || |
Jaegeuk Kim | f455c8a | 2016-12-05 11:37:14 -0800 | [diff] [blame] | 520 | f2fs_time_over(sbi, CP_TIME)) { |
Chao Yu | e9f5b8b | 2016-02-14 18:54:33 +0800 | [diff] [blame] | 521 | if (test_opt(sbi, DATA_FLUSH)) { |
| 522 | struct blk_plug plug; |
| 523 | |
| 524 | blk_start_plug(&plug); |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 525 | f2fs_sync_dirty_inodes(sbi, FILE_INODE); |
Chao Yu | e9f5b8b | 2016-02-14 18:54:33 +0800 | [diff] [blame] | 526 | blk_finish_plug(&plug); |
| 527 | } |
Jaegeuk Kim | 4660f9c | 2013-10-24 14:19:18 +0900 | [diff] [blame] | 528 | f2fs_sync_fs(sbi->sb, true); |
Jaegeuk Kim | 42190d2 | 2016-01-09 13:45:17 -0800 | [diff] [blame] | 529 | stat_inc_bg_cp_count(sbi->stat_info); |
Chao Yu | 36b35a0 | 2015-12-17 17:13:28 +0800 | [diff] [blame] | 530 | } |
Jaegeuk Kim | 4660f9c | 2013-10-24 14:19:18 +0900 | [diff] [blame] | 531 | } |
| 532 | |
Kinglong Mee | 20fda56b | 2017-03-04 22:13:10 +0800 | [diff] [blame] | 533 | static int __submit_flush_wait(struct f2fs_sb_info *sbi, |
| 534 | struct block_device *bdev) |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 535 | { |
Chao Yu | d62fe97 | 2017-10-28 16:52:31 +0800 | [diff] [blame] | 536 | struct bio *bio = f2fs_bio_alloc(sbi, 0, true); |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 537 | int ret; |
| 538 | |
Jan Kara | 3adc5fcb | 2017-05-02 17:03:47 +0200 | [diff] [blame] | 539 | bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH; |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 540 | bio_set_dev(bio, bdev); |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 541 | ret = submit_bio_wait(bio); |
| 542 | bio_put(bio); |
Kinglong Mee | 20fda56b | 2017-03-04 22:13:10 +0800 | [diff] [blame] | 543 | |
| 544 | trace_f2fs_issue_flush(bdev, test_opt(sbi, NOBARRIER), |
| 545 | test_opt(sbi, FLUSH_MERGE), ret); |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 546 | return ret; |
| 547 | } |
| 548 | |
Chao Yu | 39d787b | 2017-09-29 13:59:38 +0800 | [diff] [blame] | 549 | 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] | 550 | { |
Chao Yu | 39d787b | 2017-09-29 13:59:38 +0800 | [diff] [blame] | 551 | int ret = 0; |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 552 | int i; |
| 553 | |
Chao Yu | 39d787b | 2017-09-29 13:59:38 +0800 | [diff] [blame] | 554 | if (!sbi->s_ndevs) |
| 555 | return __submit_flush_wait(sbi, sbi->sb->s_bdev); |
Kinglong Mee | 20fda56b | 2017-03-04 22:13:10 +0800 | [diff] [blame] | 556 | |
Chao Yu | 39d787b | 2017-09-29 13:59:38 +0800 | [diff] [blame] | 557 | for (i = 0; i < sbi->s_ndevs; i++) { |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 558 | if (!f2fs_is_dirty_device(sbi, ino, i, FLUSH_INO)) |
Chao Yu | 39d787b | 2017-09-29 13:59:38 +0800 | [diff] [blame] | 559 | continue; |
Kinglong Mee | 20fda56b | 2017-03-04 22:13:10 +0800 | [diff] [blame] | 560 | ret = __submit_flush_wait(sbi, FDEV(i).bdev); |
| 561 | if (ret) |
| 562 | break; |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 563 | } |
| 564 | return ret; |
| 565 | } |
| 566 | |
Gu Zheng | 2163d19 | 2014-04-27 14:21:33 +0800 | [diff] [blame] | 567 | static int issue_flush_thread(void *data) |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 568 | { |
| 569 | struct f2fs_sb_info *sbi = data; |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 570 | struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info; |
Gu Zheng | a688b9d9e | 2014-04-27 14:21:21 +0800 | [diff] [blame] | 571 | wait_queue_head_t *q = &fcc->flush_wait_queue; |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 572 | repeat: |
| 573 | if (kthread_should_stop()) |
| 574 | return 0; |
| 575 | |
Chao Yu | dc6febb | 2017-07-22 08:52:23 +0800 | [diff] [blame] | 576 | sb_start_intwrite(sbi->sb); |
| 577 | |
Gu Zheng | 721bd4d | 2014-09-05 18:31:00 +0800 | [diff] [blame] | 578 | if (!llist_empty(&fcc->issue_list)) { |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 579 | struct flush_cmd *cmd, *next; |
| 580 | int ret; |
| 581 | |
Gu Zheng | 721bd4d | 2014-09-05 18:31:00 +0800 | [diff] [blame] | 582 | fcc->dispatch_list = llist_del_all(&fcc->issue_list); |
| 583 | fcc->dispatch_list = llist_reverse_order(fcc->dispatch_list); |
| 584 | |
Chao Yu | 39d787b | 2017-09-29 13:59:38 +0800 | [diff] [blame] | 585 | cmd = llist_entry(fcc->dispatch_list, struct flush_cmd, llnode); |
| 586 | |
| 587 | ret = submit_flush_wait(sbi, cmd->ino); |
Chao Yu | 8b8dd65 | 2017-03-25 17:19:58 +0800 | [diff] [blame] | 588 | atomic_inc(&fcc->issued_flush); |
| 589 | |
Gu Zheng | 721bd4d | 2014-09-05 18:31:00 +0800 | [diff] [blame] | 590 | llist_for_each_entry_safe(cmd, next, |
| 591 | fcc->dispatch_list, llnode) { |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 592 | cmd->ret = ret; |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 593 | complete(&cmd->wait); |
| 594 | } |
Gu Zheng | a688b9d9e | 2014-04-27 14:21:21 +0800 | [diff] [blame] | 595 | fcc->dispatch_list = NULL; |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 596 | } |
| 597 | |
Chao Yu | dc6febb | 2017-07-22 08:52:23 +0800 | [diff] [blame] | 598 | sb_end_intwrite(sbi->sb); |
| 599 | |
Gu Zheng | a688b9d9e | 2014-04-27 14:21:21 +0800 | [diff] [blame] | 600 | wait_event_interruptible(*q, |
Gu Zheng | 721bd4d | 2014-09-05 18:31:00 +0800 | [diff] [blame] | 601 | kthread_should_stop() || !llist_empty(&fcc->issue_list)); |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 602 | goto repeat; |
| 603 | } |
| 604 | |
Chao Yu | 39d787b | 2017-09-29 13:59:38 +0800 | [diff] [blame] | 605 | int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino) |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 606 | { |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 607 | struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info; |
Chao Yu | adf8d90 | 2014-05-08 17:00:35 +0800 | [diff] [blame] | 608 | struct flush_cmd cmd; |
Chao Yu | 8b8dd65 | 2017-03-25 17:19:58 +0800 | [diff] [blame] | 609 | int ret; |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 610 | |
Jaegeuk Kim | 0f7b2ab | 2014-07-23 09:57:31 -0700 | [diff] [blame] | 611 | if (test_opt(sbi, NOBARRIER)) |
| 612 | return 0; |
| 613 | |
Chao Yu | 8b8dd65 | 2017-03-25 17:19:58 +0800 | [diff] [blame] | 614 | if (!test_opt(sbi, FLUSH_MERGE)) { |
Chao Yu | 39d787b | 2017-09-29 13:59:38 +0800 | [diff] [blame] | 615 | ret = submit_flush_wait(sbi, ino); |
Chao Yu | 8b8dd65 | 2017-03-25 17:19:58 +0800 | [diff] [blame] | 616 | atomic_inc(&fcc->issued_flush); |
| 617 | return ret; |
| 618 | } |
| 619 | |
Chao Yu | 39d787b | 2017-09-29 13:59:38 +0800 | [diff] [blame] | 620 | if (atomic_inc_return(&fcc->issing_flush) == 1 || sbi->s_ndevs > 1) { |
| 621 | ret = submit_flush_wait(sbi, ino); |
Chao Yu | 8b8dd65 | 2017-03-25 17:19:58 +0800 | [diff] [blame] | 622 | atomic_dec(&fcc->issing_flush); |
| 623 | |
| 624 | atomic_inc(&fcc->issued_flush); |
Jaegeuk Kim | 740432f | 2015-08-14 11:43:56 -0700 | [diff] [blame] | 625 | return ret; |
| 626 | } |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 627 | |
Chao Yu | 39d787b | 2017-09-29 13:59:38 +0800 | [diff] [blame] | 628 | cmd.ino = ino; |
Chao Yu | adf8d90 | 2014-05-08 17:00:35 +0800 | [diff] [blame] | 629 | init_completion(&cmd.wait); |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 630 | |
Gu Zheng | 721bd4d | 2014-09-05 18:31:00 +0800 | [diff] [blame] | 631 | llist_add(&cmd.llnode, &fcc->issue_list); |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 632 | |
Chao Yu | 6f890df | 2017-08-21 22:53:45 +0800 | [diff] [blame] | 633 | /* update issue_list before we wake up issue_flush thread */ |
| 634 | smp_mb(); |
| 635 | |
| 636 | if (waitqueue_active(&fcc->flush_wait_queue)) |
Gu Zheng | a688b9d9e | 2014-04-27 14:21:21 +0800 | [diff] [blame] | 637 | wake_up(&fcc->flush_wait_queue); |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 638 | |
Jaegeuk Kim | 5eba8c5 | 2016-12-07 16:23:32 -0800 | [diff] [blame] | 639 | if (fcc->f2fs_issue_flush) { |
| 640 | wait_for_completion(&cmd.wait); |
Chao Yu | 8b8dd65 | 2017-03-25 17:19:58 +0800 | [diff] [blame] | 641 | atomic_dec(&fcc->issing_flush); |
Jaegeuk Kim | 5eba8c5 | 2016-12-07 16:23:32 -0800 | [diff] [blame] | 642 | } else { |
Chao Yu | d323869 | 2017-08-31 18:56:06 +0800 | [diff] [blame] | 643 | struct llist_node *list; |
| 644 | |
| 645 | list = llist_del_all(&fcc->issue_list); |
| 646 | if (!list) { |
| 647 | wait_for_completion(&cmd.wait); |
| 648 | atomic_dec(&fcc->issing_flush); |
| 649 | } else { |
| 650 | struct flush_cmd *tmp, *next; |
| 651 | |
Chao Yu | 39d787b | 2017-09-29 13:59:38 +0800 | [diff] [blame] | 652 | ret = submit_flush_wait(sbi, ino); |
Chao Yu | d323869 | 2017-08-31 18:56:06 +0800 | [diff] [blame] | 653 | |
| 654 | llist_for_each_entry_safe(tmp, next, list, llnode) { |
| 655 | if (tmp == &cmd) { |
| 656 | cmd.ret = ret; |
| 657 | atomic_dec(&fcc->issing_flush); |
| 658 | continue; |
| 659 | } |
| 660 | tmp->ret = ret; |
| 661 | complete(&tmp->wait); |
| 662 | } |
| 663 | } |
Jaegeuk Kim | 5eba8c5 | 2016-12-07 16:23:32 -0800 | [diff] [blame] | 664 | } |
Chao Yu | adf8d90 | 2014-05-08 17:00:35 +0800 | [diff] [blame] | 665 | |
| 666 | return cmd.ret; |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 667 | } |
| 668 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 669 | int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi) |
Gu Zheng | 2163d19 | 2014-04-27 14:21:33 +0800 | [diff] [blame] | 670 | { |
| 671 | dev_t dev = sbi->sb->s_bdev->bd_dev; |
| 672 | struct flush_cmd_control *fcc; |
| 673 | int err = 0; |
| 674 | |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 675 | if (SM_I(sbi)->fcc_info) { |
| 676 | fcc = SM_I(sbi)->fcc_info; |
Yunlong Song | d871cd0 | 2017-06-24 15:57:19 +0800 | [diff] [blame] | 677 | if (fcc->f2fs_issue_flush) |
| 678 | return err; |
Jaegeuk Kim | 5eba8c5 | 2016-12-07 16:23:32 -0800 | [diff] [blame] | 679 | goto init_thread; |
| 680 | } |
| 681 | |
Chao Yu | acbf054 | 2017-11-30 19:28:17 +0800 | [diff] [blame] | 682 | fcc = f2fs_kzalloc(sbi, sizeof(struct flush_cmd_control), GFP_KERNEL); |
Gu Zheng | 2163d19 | 2014-04-27 14:21:33 +0800 | [diff] [blame] | 683 | if (!fcc) |
| 684 | return -ENOMEM; |
Chao Yu | 8b8dd65 | 2017-03-25 17:19:58 +0800 | [diff] [blame] | 685 | atomic_set(&fcc->issued_flush, 0); |
| 686 | atomic_set(&fcc->issing_flush, 0); |
Gu Zheng | 2163d19 | 2014-04-27 14:21:33 +0800 | [diff] [blame] | 687 | init_waitqueue_head(&fcc->flush_wait_queue); |
Gu Zheng | 721bd4d | 2014-09-05 18:31:00 +0800 | [diff] [blame] | 688 | init_llist_head(&fcc->issue_list); |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 689 | SM_I(sbi)->fcc_info = fcc; |
Yunlei He | d4fdf8b | 2017-06-01 16:43:51 +0800 | [diff] [blame] | 690 | if (!test_opt(sbi, FLUSH_MERGE)) |
| 691 | return err; |
| 692 | |
Jaegeuk Kim | 5eba8c5 | 2016-12-07 16:23:32 -0800 | [diff] [blame] | 693 | init_thread: |
Gu Zheng | 2163d19 | 2014-04-27 14:21:33 +0800 | [diff] [blame] | 694 | fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi, |
| 695 | "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev)); |
| 696 | if (IS_ERR(fcc->f2fs_issue_flush)) { |
| 697 | err = PTR_ERR(fcc->f2fs_issue_flush); |
| 698 | kfree(fcc); |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 699 | SM_I(sbi)->fcc_info = NULL; |
Gu Zheng | 2163d19 | 2014-04-27 14:21:33 +0800 | [diff] [blame] | 700 | return err; |
| 701 | } |
Gu Zheng | 2163d19 | 2014-04-27 14:21:33 +0800 | [diff] [blame] | 702 | |
| 703 | return err; |
| 704 | } |
| 705 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 706 | 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] | 707 | { |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 708 | struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info; |
Gu Zheng | 2163d19 | 2014-04-27 14:21:33 +0800 | [diff] [blame] | 709 | |
Jaegeuk Kim | 5eba8c5 | 2016-12-07 16:23:32 -0800 | [diff] [blame] | 710 | if (fcc && fcc->f2fs_issue_flush) { |
| 711 | struct task_struct *flush_thread = fcc->f2fs_issue_flush; |
| 712 | |
| 713 | fcc->f2fs_issue_flush = NULL; |
| 714 | kthread_stop(flush_thread); |
| 715 | } |
| 716 | if (free) { |
| 717 | kfree(fcc); |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 718 | SM_I(sbi)->fcc_info = NULL; |
Jaegeuk Kim | 5eba8c5 | 2016-12-07 16:23:32 -0800 | [diff] [blame] | 719 | } |
Gu Zheng | 2163d19 | 2014-04-27 14:21:33 +0800 | [diff] [blame] | 720 | } |
| 721 | |
Chao Yu | 1228b48 | 2017-09-29 13:59:39 +0800 | [diff] [blame] | 722 | int f2fs_flush_device_cache(struct f2fs_sb_info *sbi) |
| 723 | { |
| 724 | int ret = 0, i; |
| 725 | |
| 726 | if (!sbi->s_ndevs) |
| 727 | return 0; |
| 728 | |
| 729 | for (i = 1; i < sbi->s_ndevs; i++) { |
| 730 | if (!f2fs_test_bit(i, (char *)&sbi->dirty_device)) |
| 731 | continue; |
| 732 | ret = __submit_flush_wait(sbi, FDEV(i).bdev); |
| 733 | if (ret) |
| 734 | break; |
| 735 | |
| 736 | spin_lock(&sbi->dev_lock); |
| 737 | f2fs_clear_bit(i, (char *)&sbi->dirty_device); |
| 738 | spin_unlock(&sbi->dev_lock); |
| 739 | } |
| 740 | |
| 741 | return ret; |
| 742 | } |
| 743 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 744 | static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno, |
| 745 | enum dirty_type dirty_type) |
| 746 | { |
| 747 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
| 748 | |
| 749 | /* need not be added */ |
| 750 | if (IS_CURSEG(sbi, segno)) |
| 751 | return; |
| 752 | |
| 753 | if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type])) |
| 754 | dirty_i->nr_dirty[dirty_type]++; |
| 755 | |
| 756 | if (dirty_type == DIRTY) { |
| 757 | struct seg_entry *sentry = get_seg_entry(sbi, segno); |
Changman Lee | 4625d6a | 2013-10-25 17:31:57 +0900 | [diff] [blame] | 758 | enum dirty_type t = sentry->type; |
Jaegeuk Kim | b2f2c39 | 2013-04-01 13:52:09 +0900 | [diff] [blame] | 759 | |
Jaegeuk Kim | ec325b5 | 2014-09-02 16:24:11 -0700 | [diff] [blame] | 760 | if (unlikely(t >= DIRTY)) { |
| 761 | f2fs_bug_on(sbi, 1); |
| 762 | return; |
| 763 | } |
Changman Lee | 4625d6a | 2013-10-25 17:31:57 +0900 | [diff] [blame] | 764 | if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t])) |
| 765 | dirty_i->nr_dirty[t]++; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 766 | } |
| 767 | } |
| 768 | |
| 769 | static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno, |
| 770 | enum dirty_type dirty_type) |
| 771 | { |
| 772 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
| 773 | |
| 774 | if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type])) |
| 775 | dirty_i->nr_dirty[dirty_type]--; |
| 776 | |
| 777 | if (dirty_type == DIRTY) { |
Changman Lee | 4625d6a | 2013-10-25 17:31:57 +0900 | [diff] [blame] | 778 | struct seg_entry *sentry = get_seg_entry(sbi, segno); |
| 779 | enum dirty_type t = sentry->type; |
Jaegeuk Kim | b2f2c39 | 2013-04-01 13:52:09 +0900 | [diff] [blame] | 780 | |
Changman Lee | 4625d6a | 2013-10-25 17:31:57 +0900 | [diff] [blame] | 781 | if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t])) |
| 782 | dirty_i->nr_dirty[t]--; |
Jaegeuk Kim | b2f2c39 | 2013-04-01 13:52:09 +0900 | [diff] [blame] | 783 | |
Jaegeuk Kim | 302bd34 | 2017-04-07 14:33:22 -0700 | [diff] [blame] | 784 | if (get_valid_blocks(sbi, segno, true) == 0) |
Jaegeuk Kim | 4ddb1a4 | 2017-04-07 15:08:17 -0700 | [diff] [blame] | 785 | clear_bit(GET_SEC_FROM_SEG(sbi, segno), |
Jaegeuk Kim | 5ec4e49 | 2013-03-31 13:26:03 +0900 | [diff] [blame] | 786 | dirty_i->victim_secmap); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 787 | } |
| 788 | } |
| 789 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 790 | /* |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 791 | * Should not occur error such as -ENOMEM. |
| 792 | * Adding dirty entry into seglist is not critical operation. |
| 793 | * If a given segment is one of current working segments, it won't be added. |
| 794 | */ |
Haicheng Li | 8d8451a | 2013-06-13 16:59:28 +0800 | [diff] [blame] | 795 | 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] | 796 | { |
| 797 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
| 798 | unsigned short valid_blocks; |
| 799 | |
| 800 | if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno)) |
| 801 | return; |
| 802 | |
| 803 | mutex_lock(&dirty_i->seglist_lock); |
| 804 | |
Jaegeuk Kim | 302bd34 | 2017-04-07 14:33:22 -0700 | [diff] [blame] | 805 | valid_blocks = get_valid_blocks(sbi, segno, false); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 806 | |
| 807 | if (valid_blocks == 0) { |
| 808 | __locate_dirty_segment(sbi, segno, PRE); |
| 809 | __remove_dirty_segment(sbi, segno, DIRTY); |
| 810 | } else if (valid_blocks < sbi->blocks_per_seg) { |
| 811 | __locate_dirty_segment(sbi, segno, DIRTY); |
| 812 | } else { |
| 813 | /* Recovery routine with SSR needs this */ |
| 814 | __remove_dirty_segment(sbi, segno, DIRTY); |
| 815 | } |
| 816 | |
| 817 | mutex_unlock(&dirty_i->seglist_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 818 | } |
| 819 | |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 820 | static struct discard_cmd *__create_discard_cmd(struct f2fs_sb_info *sbi, |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 821 | struct block_device *bdev, block_t lstart, |
| 822 | block_t start, block_t len) |
Chao Yu | 275b66b | 2016-08-29 23:58:34 +0800 | [diff] [blame] | 823 | { |
Jaegeuk Kim | 0b54fb8 | 2017-01-11 14:40:24 -0800 | [diff] [blame] | 824 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
Chao Yu | ba48a33 | 2017-04-15 14:09:37 +0800 | [diff] [blame] | 825 | struct list_head *pend_list; |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 826 | struct discard_cmd *dc; |
Chao Yu | 275b66b | 2016-08-29 23:58:34 +0800 | [diff] [blame] | 827 | |
Chao Yu | ba48a33 | 2017-04-15 14:09:37 +0800 | [diff] [blame] | 828 | f2fs_bug_on(sbi, !len); |
| 829 | |
| 830 | pend_list = &dcc->pend_list[plist_idx(len)]; |
| 831 | |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 832 | dc = f2fs_kmem_cache_alloc(discard_cmd_slab, GFP_NOFS); |
| 833 | INIT_LIST_HEAD(&dc->list); |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 834 | dc->bdev = bdev; |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 835 | dc->lstart = lstart; |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 836 | dc->start = start; |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 837 | dc->len = len; |
Chao Yu | ec9895a | 2017-04-26 17:39:54 +0800 | [diff] [blame] | 838 | dc->ref = 0; |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 839 | dc->state = D_PREP; |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 840 | dc->issuing = 0; |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 841 | dc->error = 0; |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 842 | init_completion(&dc->wait); |
Chao Yu | 22d375d | 2017-04-05 18:19:48 +0800 | [diff] [blame] | 843 | list_add_tail(&dc->list, pend_list); |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 844 | spin_lock_init(&dc->lock); |
| 845 | dc->bio_ref = 0; |
Chao Yu | 5f32366 | 2017-03-25 17:19:59 +0800 | [diff] [blame] | 846 | atomic_inc(&dcc->discard_cmd_cnt); |
Chao Yu | d84d1cb | 2017-04-18 19:27:39 +0800 | [diff] [blame] | 847 | dcc->undiscard_blks += len; |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 848 | |
| 849 | return dc; |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 850 | } |
| 851 | |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 852 | static struct discard_cmd *__attach_discard_cmd(struct f2fs_sb_info *sbi, |
| 853 | struct block_device *bdev, block_t lstart, |
| 854 | block_t start, block_t len, |
| 855 | struct rb_node *parent, struct rb_node **p) |
| 856 | { |
| 857 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
| 858 | struct discard_cmd *dc; |
| 859 | |
| 860 | dc = __create_discard_cmd(sbi, bdev, lstart, start, len); |
| 861 | |
| 862 | rb_link_node(&dc->rb_node, parent, p); |
| 863 | rb_insert_color(&dc->rb_node, &dcc->root); |
| 864 | |
| 865 | return dc; |
| 866 | } |
| 867 | |
| 868 | static void __detach_discard_cmd(struct discard_cmd_control *dcc, |
| 869 | struct discard_cmd *dc) |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 870 | { |
Jaegeuk Kim | dcc9165 | 2017-01-11 10:20:04 -0800 | [diff] [blame] | 871 | if (dc->state == D_DONE) |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 872 | atomic_sub(dc->issuing, &dcc->issing_discard); |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 873 | |
| 874 | list_del(&dc->list); |
| 875 | rb_erase(&dc->rb_node, &dcc->root); |
Chao Yu | d84d1cb | 2017-04-18 19:27:39 +0800 | [diff] [blame] | 876 | dcc->undiscard_blks -= dc->len; |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 877 | |
| 878 | kmem_cache_free(discard_cmd_slab, dc); |
| 879 | |
| 880 | atomic_dec(&dcc->discard_cmd_cnt); |
| 881 | } |
| 882 | |
| 883 | static void __remove_discard_cmd(struct f2fs_sb_info *sbi, |
| 884 | struct discard_cmd *dc) |
| 885 | { |
| 886 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 887 | unsigned long flags; |
Jaegeuk Kim | dcc9165 | 2017-01-11 10:20:04 -0800 | [diff] [blame] | 888 | |
Chao Yu | 2ec6f2e | 2017-10-04 09:08:36 +0800 | [diff] [blame] | 889 | trace_f2fs_remove_discard(dc->bdev, dc->start, dc->len); |
| 890 | |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 891 | spin_lock_irqsave(&dc->lock, flags); |
| 892 | if (dc->bio_ref) { |
| 893 | spin_unlock_irqrestore(&dc->lock, flags); |
| 894 | return; |
| 895 | } |
| 896 | spin_unlock_irqrestore(&dc->lock, flags); |
| 897 | |
Chao Yu | d9703d9 | 2017-06-05 18:29:07 +0800 | [diff] [blame] | 898 | f2fs_bug_on(sbi, dc->ref); |
| 899 | |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 900 | if (dc->error == -EOPNOTSUPP) |
| 901 | dc->error = 0; |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 902 | |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 903 | if (dc->error) |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 904 | f2fs_msg(sbi->sb, KERN_INFO, |
Chao Yu | 04dfc23 | 2017-05-19 23:46:43 +0800 | [diff] [blame] | 905 | "Issue discard(%u, %u, %u) failed, ret: %d", |
| 906 | dc->lstart, dc->start, dc->len, dc->error); |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 907 | __detach_discard_cmd(dcc, dc); |
Chao Yu | 275b66b | 2016-08-29 23:58:34 +0800 | [diff] [blame] | 908 | } |
| 909 | |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 910 | static void f2fs_submit_discard_endio(struct bio *bio) |
| 911 | { |
| 912 | struct discard_cmd *dc = (struct discard_cmd *)bio->bi_private; |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 913 | unsigned long flags; |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 914 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 915 | dc->error = blk_status_to_errno(bio->bi_status); |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 916 | |
| 917 | spin_lock_irqsave(&dc->lock, flags); |
| 918 | dc->bio_ref--; |
| 919 | if (!dc->bio_ref && dc->state == D_SUBMIT) { |
| 920 | dc->state = D_DONE; |
| 921 | complete_all(&dc->wait); |
| 922 | } |
| 923 | spin_unlock_irqrestore(&dc->lock, flags); |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 924 | bio_put(bio); |
| 925 | } |
| 926 | |
Wei Yongjun | 94b1e10 | 2018-01-05 09:41:20 +0000 | [diff] [blame] | 927 | static void __check_sit_bitmap(struct f2fs_sb_info *sbi, |
Chao Yu | 6915ea9 | 2017-06-30 17:19:02 +0800 | [diff] [blame] | 928 | block_t start, block_t end) |
| 929 | { |
| 930 | #ifdef CONFIG_F2FS_CHECK_FS |
| 931 | struct seg_entry *sentry; |
| 932 | unsigned int segno; |
| 933 | block_t blk = start; |
| 934 | unsigned long offset, size, max_blocks = sbi->blocks_per_seg; |
| 935 | unsigned long *map; |
| 936 | |
| 937 | while (blk < end) { |
| 938 | segno = GET_SEGNO(sbi, blk); |
| 939 | sentry = get_seg_entry(sbi, segno); |
| 940 | offset = GET_BLKOFF_FROM_SEG0(sbi, blk); |
| 941 | |
Yunlong Song | 008396e | 2017-08-04 17:07:15 +0800 | [diff] [blame] | 942 | if (end < START_BLOCK(sbi, segno + 1)) |
| 943 | size = GET_BLKOFF_FROM_SEG0(sbi, end); |
| 944 | else |
| 945 | size = max_blocks; |
Chao Yu | 6915ea9 | 2017-06-30 17:19:02 +0800 | [diff] [blame] | 946 | map = (unsigned long *)(sentry->cur_valid_map); |
| 947 | offset = __find_rev_next_bit(map, size, offset); |
| 948 | f2fs_bug_on(sbi, offset != size); |
Yunlong Song | 008396e | 2017-08-04 17:07:15 +0800 | [diff] [blame] | 949 | blk = START_BLOCK(sbi, segno + 1); |
Chao Yu | 6915ea9 | 2017-06-30 17:19:02 +0800 | [diff] [blame] | 950 | } |
| 951 | #endif |
| 952 | } |
| 953 | |
Jaegeuk Kim | 8bb4f25 | 2018-05-29 09:58:42 -0700 | [diff] [blame] | 954 | static void __init_discard_policy(struct f2fs_sb_info *sbi, |
| 955 | struct discard_policy *dpolicy, |
| 956 | int discard_type, unsigned int granularity) |
| 957 | { |
| 958 | /* common policy */ |
| 959 | dpolicy->type = discard_type; |
| 960 | dpolicy->sync = true; |
Chao Yu | 20ee438 | 2018-07-08 22:11:01 +0800 | [diff] [blame] | 961 | dpolicy->ordered = false; |
Jaegeuk Kim | 8bb4f25 | 2018-05-29 09:58:42 -0700 | [diff] [blame] | 962 | dpolicy->granularity = granularity; |
| 963 | |
| 964 | dpolicy->max_requests = DEF_MAX_DISCARD_REQUEST; |
| 965 | dpolicy->io_aware_gran = MAX_PLIST_NUM; |
| 966 | |
| 967 | if (discard_type == DPOLICY_BG) { |
| 968 | dpolicy->min_interval = DEF_MIN_DISCARD_ISSUE_TIME; |
Yunlei He | f9d1dce | 2018-04-08 15:11:11 +0800 | [diff] [blame] | 969 | dpolicy->mid_interval = DEF_MID_DISCARD_ISSUE_TIME; |
Jaegeuk Kim | 8bb4f25 | 2018-05-29 09:58:42 -0700 | [diff] [blame] | 970 | dpolicy->max_interval = DEF_MAX_DISCARD_ISSUE_TIME; |
| 971 | dpolicy->io_aware = true; |
Chao Yu | cba6084 | 2018-04-10 15:43:09 +0800 | [diff] [blame] | 972 | dpolicy->sync = false; |
Chao Yu | 20ee438 | 2018-07-08 22:11:01 +0800 | [diff] [blame] | 973 | dpolicy->ordered = true; |
Jaegeuk Kim | 8bb4f25 | 2018-05-29 09:58:42 -0700 | [diff] [blame] | 974 | if (utilization(sbi) > DEF_DISCARD_URGENT_UTIL) { |
| 975 | dpolicy->granularity = 1; |
| 976 | dpolicy->max_interval = DEF_MIN_DISCARD_ISSUE_TIME; |
| 977 | } |
| 978 | } else if (discard_type == DPOLICY_FORCE) { |
| 979 | dpolicy->min_interval = DEF_MIN_DISCARD_ISSUE_TIME; |
Yunlei He | f9d1dce | 2018-04-08 15:11:11 +0800 | [diff] [blame] | 980 | dpolicy->mid_interval = DEF_MID_DISCARD_ISSUE_TIME; |
Jaegeuk Kim | 8bb4f25 | 2018-05-29 09:58:42 -0700 | [diff] [blame] | 981 | dpolicy->max_interval = DEF_MAX_DISCARD_ISSUE_TIME; |
| 982 | dpolicy->io_aware = false; |
| 983 | } else if (discard_type == DPOLICY_FSTRIM) { |
| 984 | dpolicy->io_aware = false; |
| 985 | } else if (discard_type == DPOLICY_UMOUNT) { |
Yunlei He | 241b493 | 2018-04-04 17:29:05 +0800 | [diff] [blame] | 986 | dpolicy->max_requests = UINT_MAX; |
Jaegeuk Kim | 8bb4f25 | 2018-05-29 09:58:42 -0700 | [diff] [blame] | 987 | dpolicy->io_aware = false; |
| 988 | } |
| 989 | } |
| 990 | |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 991 | static void __update_discard_tree_range(struct f2fs_sb_info *sbi, |
| 992 | struct block_device *bdev, block_t lstart, |
| 993 | block_t start, block_t len); |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 994 | /* 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] | 995 | static int __submit_discard_cmd(struct f2fs_sb_info *sbi, |
Chao Yu | 78997b5 | 2017-10-04 09:08:34 +0800 | [diff] [blame] | 996 | struct discard_policy *dpolicy, |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 997 | struct discard_cmd *dc, |
| 998 | unsigned int *issued) |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 999 | { |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 1000 | struct block_device *bdev = dc->bdev; |
| 1001 | struct request_queue *q = bdev_get_queue(bdev); |
| 1002 | unsigned int max_discard_blocks = |
| 1003 | SECTOR_TO_BLOCK(q->limits.max_discard_sectors); |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 1004 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
Chao Yu | 78997b5 | 2017-10-04 09:08:34 +0800 | [diff] [blame] | 1005 | struct list_head *wait_list = (dpolicy->type == DPOLICY_FSTRIM) ? |
| 1006 | &(dcc->fstrim_list) : &(dcc->wait_list); |
Chao Yu | 78997b5 | 2017-10-04 09:08:34 +0800 | [diff] [blame] | 1007 | int flag = dpolicy->sync ? REQ_SYNC : 0; |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 1008 | block_t lstart, start, len, total_len; |
| 1009 | int err = 0; |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 1010 | |
| 1011 | if (dc->state != D_PREP) |
Chao Yu | 6b9cb12 | 2018-08-08 10:14:55 +0800 | [diff] [blame] | 1012 | return 0; |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 1013 | |
Yunlei He | d618477 | 2018-04-13 11:08:05 +0800 | [diff] [blame] | 1014 | if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) |
Chao Yu | 6b9cb12 | 2018-08-08 10:14:55 +0800 | [diff] [blame] | 1015 | return 0; |
Yunlei He | d618477 | 2018-04-13 11:08:05 +0800 | [diff] [blame] | 1016 | |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 1017 | trace_f2fs_issue_discard(bdev, dc->start, dc->len); |
Chao Yu | 0243a5f | 2017-04-15 14:09:38 +0800 | [diff] [blame] | 1018 | |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 1019 | lstart = dc->lstart; |
| 1020 | start = dc->start; |
| 1021 | len = dc->len; |
| 1022 | total_len = len; |
| 1023 | |
| 1024 | dc->len = 0; |
| 1025 | |
| 1026 | while (total_len && *issued < dpolicy->max_requests && !err) { |
| 1027 | struct bio *bio = NULL; |
| 1028 | unsigned long flags; |
| 1029 | bool last = true; |
| 1030 | |
| 1031 | if (len > max_discard_blocks) { |
| 1032 | len = max_discard_blocks; |
| 1033 | last = false; |
| 1034 | } |
| 1035 | |
| 1036 | (*issued)++; |
| 1037 | if (*issued == dpolicy->max_requests) |
| 1038 | last = true; |
| 1039 | |
| 1040 | dc->len += len; |
| 1041 | |
Chao Yu | b83dcfe | 2018-08-06 20:30:18 +0800 | [diff] [blame] | 1042 | if (time_to_inject(sbi, FAULT_DISCARD)) { |
| 1043 | f2fs_show_injection_info(FAULT_DISCARD); |
| 1044 | err = -EIO; |
| 1045 | goto submit; |
| 1046 | } |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 1047 | err = __blkdev_issue_discard(bdev, |
| 1048 | SECTOR_FROM_BLOCK(start), |
| 1049 | SECTOR_FROM_BLOCK(len), |
| 1050 | GFP_NOFS, 0, &bio); |
Chao Yu | b83dcfe | 2018-08-06 20:30:18 +0800 | [diff] [blame] | 1051 | submit: |
Chao Yu | 6b9cb12 | 2018-08-08 10:14:55 +0800 | [diff] [blame] | 1052 | if (err) { |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 1053 | spin_lock_irqsave(&dc->lock, flags); |
| 1054 | if (dc->state == D_PARTIAL) |
| 1055 | dc->state = D_SUBMIT; |
| 1056 | spin_unlock_irqrestore(&dc->lock, flags); |
| 1057 | |
Chao Yu | 6b9cb12 | 2018-08-08 10:14:55 +0800 | [diff] [blame] | 1058 | break; |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 1059 | } |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 1060 | |
Chao Yu | 6b9cb12 | 2018-08-08 10:14:55 +0800 | [diff] [blame] | 1061 | f2fs_bug_on(sbi, !bio); |
| 1062 | |
| 1063 | /* |
| 1064 | * should keep before submission to avoid D_DONE |
| 1065 | * right away |
| 1066 | */ |
| 1067 | spin_lock_irqsave(&dc->lock, flags); |
| 1068 | if (last) |
| 1069 | dc->state = D_SUBMIT; |
| 1070 | else |
| 1071 | dc->state = D_PARTIAL; |
| 1072 | dc->bio_ref++; |
| 1073 | spin_unlock_irqrestore(&dc->lock, flags); |
| 1074 | |
| 1075 | atomic_inc(&dcc->issing_discard); |
| 1076 | dc->issuing++; |
| 1077 | list_move_tail(&dc->list, wait_list); |
| 1078 | |
| 1079 | /* sanity check on discard range */ |
| 1080 | __check_sit_bitmap(sbi, start, start + len); |
| 1081 | |
| 1082 | bio->bi_private = dc; |
| 1083 | bio->bi_end_io = f2fs_submit_discard_endio; |
| 1084 | bio->bi_opf |= flag; |
| 1085 | submit_bio(bio); |
| 1086 | |
| 1087 | atomic_inc(&dcc->issued_discard); |
| 1088 | |
| 1089 | f2fs_update_iostat(sbi, FS_DISCARD, 1); |
| 1090 | |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 1091 | lstart += len; |
| 1092 | start += len; |
| 1093 | total_len -= len; |
| 1094 | len = total_len; |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 1095 | } |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 1096 | |
Chao Yu | 6b9cb12 | 2018-08-08 10:14:55 +0800 | [diff] [blame] | 1097 | if (!err && len) |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 1098 | __update_discard_tree_range(sbi, bdev, lstart, start, len); |
Chao Yu | 6b9cb12 | 2018-08-08 10:14:55 +0800 | [diff] [blame] | 1099 | return err; |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 1100 | } |
| 1101 | |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1102 | static struct discard_cmd *__insert_discard_tree(struct f2fs_sb_info *sbi, |
| 1103 | struct block_device *bdev, block_t lstart, |
| 1104 | block_t start, block_t len, |
| 1105 | struct rb_node **insert_p, |
| 1106 | struct rb_node *insert_parent) |
| 1107 | { |
| 1108 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
Colin Ian King | dca6951 | 2017-10-19 12:58:21 +0200 | [diff] [blame] | 1109 | struct rb_node **p; |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1110 | struct rb_node *parent = NULL; |
| 1111 | struct discard_cmd *dc = NULL; |
| 1112 | |
| 1113 | if (insert_p && insert_parent) { |
| 1114 | parent = insert_parent; |
| 1115 | p = insert_p; |
| 1116 | goto do_insert; |
| 1117 | } |
| 1118 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1119 | p = f2fs_lookup_rb_tree_for_insert(sbi, &dcc->root, &parent, lstart); |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1120 | do_insert: |
| 1121 | dc = __attach_discard_cmd(sbi, bdev, lstart, start, len, parent, p); |
| 1122 | if (!dc) |
| 1123 | return NULL; |
| 1124 | |
| 1125 | return dc; |
| 1126 | } |
| 1127 | |
Chao Yu | ba48a33 | 2017-04-15 14:09:37 +0800 | [diff] [blame] | 1128 | static void __relocate_discard_cmd(struct discard_cmd_control *dcc, |
| 1129 | struct discard_cmd *dc) |
| 1130 | { |
| 1131 | list_move_tail(&dc->list, &dcc->pend_list[plist_idx(dc->len)]); |
| 1132 | } |
| 1133 | |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1134 | static void __punch_discard_cmd(struct f2fs_sb_info *sbi, |
| 1135 | struct discard_cmd *dc, block_t blkaddr) |
| 1136 | { |
Chao Yu | ba48a33 | 2017-04-15 14:09:37 +0800 | [diff] [blame] | 1137 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1138 | struct discard_info di = dc->di; |
| 1139 | bool modified = false; |
| 1140 | |
| 1141 | if (dc->state == D_DONE || dc->len == 1) { |
| 1142 | __remove_discard_cmd(sbi, dc); |
| 1143 | return; |
| 1144 | } |
| 1145 | |
Chao Yu | d84d1cb | 2017-04-18 19:27:39 +0800 | [diff] [blame] | 1146 | dcc->undiscard_blks -= di.len; |
| 1147 | |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1148 | if (blkaddr > di.lstart) { |
| 1149 | dc->len = blkaddr - dc->lstart; |
Chao Yu | d84d1cb | 2017-04-18 19:27:39 +0800 | [diff] [blame] | 1150 | dcc->undiscard_blks += dc->len; |
Chao Yu | ba48a33 | 2017-04-15 14:09:37 +0800 | [diff] [blame] | 1151 | __relocate_discard_cmd(dcc, dc); |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1152 | modified = true; |
| 1153 | } |
| 1154 | |
| 1155 | if (blkaddr < di.lstart + di.len - 1) { |
| 1156 | if (modified) { |
| 1157 | __insert_discard_tree(sbi, dc->bdev, blkaddr + 1, |
| 1158 | di.start + blkaddr + 1 - di.lstart, |
| 1159 | di.lstart + di.len - 1 - blkaddr, |
| 1160 | NULL, NULL); |
| 1161 | } else { |
| 1162 | dc->lstart++; |
| 1163 | dc->len--; |
| 1164 | dc->start++; |
Chao Yu | d84d1cb | 2017-04-18 19:27:39 +0800 | [diff] [blame] | 1165 | dcc->undiscard_blks += dc->len; |
Chao Yu | ba48a33 | 2017-04-15 14:09:37 +0800 | [diff] [blame] | 1166 | __relocate_discard_cmd(dcc, dc); |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1167 | } |
| 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | static void __update_discard_tree_range(struct f2fs_sb_info *sbi, |
| 1172 | struct block_device *bdev, block_t lstart, |
| 1173 | block_t start, block_t len) |
| 1174 | { |
| 1175 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
| 1176 | struct discard_cmd *prev_dc = NULL, *next_dc = NULL; |
| 1177 | struct discard_cmd *dc; |
| 1178 | struct discard_info di = {0}; |
| 1179 | struct rb_node **insert_p = NULL, *insert_parent = NULL; |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 1180 | struct request_queue *q = bdev_get_queue(bdev); |
| 1181 | unsigned int max_discard_blocks = |
| 1182 | SECTOR_TO_BLOCK(q->limits.max_discard_sectors); |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1183 | block_t end = lstart + len; |
| 1184 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1185 | dc = (struct discard_cmd *)f2fs_lookup_rb_tree_ret(&dcc->root, |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1186 | NULL, lstart, |
| 1187 | (struct rb_entry **)&prev_dc, |
| 1188 | (struct rb_entry **)&next_dc, |
| 1189 | &insert_p, &insert_parent, true); |
| 1190 | if (dc) |
| 1191 | prev_dc = dc; |
| 1192 | |
| 1193 | if (!prev_dc) { |
| 1194 | di.lstart = lstart; |
| 1195 | di.len = next_dc ? next_dc->lstart - lstart : len; |
| 1196 | di.len = min(di.len, len); |
| 1197 | di.start = start; |
| 1198 | } |
| 1199 | |
| 1200 | while (1) { |
| 1201 | struct rb_node *node; |
| 1202 | bool merged = false; |
| 1203 | struct discard_cmd *tdc = NULL; |
| 1204 | |
| 1205 | if (prev_dc) { |
| 1206 | di.lstart = prev_dc->lstart + prev_dc->len; |
| 1207 | if (di.lstart < lstart) |
| 1208 | di.lstart = lstart; |
| 1209 | if (di.lstart >= end) |
| 1210 | break; |
| 1211 | |
| 1212 | if (!next_dc || next_dc->lstart > end) |
| 1213 | di.len = end - di.lstart; |
| 1214 | else |
| 1215 | di.len = next_dc->lstart - di.lstart; |
| 1216 | di.start = start + di.lstart - lstart; |
| 1217 | } |
| 1218 | |
| 1219 | if (!di.len) |
| 1220 | goto next; |
| 1221 | |
| 1222 | if (prev_dc && prev_dc->state == D_PREP && |
| 1223 | prev_dc->bdev == bdev && |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 1224 | __is_discard_back_mergeable(&di, &prev_dc->di, |
| 1225 | max_discard_blocks)) { |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1226 | prev_dc->di.len += di.len; |
Chao Yu | d84d1cb | 2017-04-18 19:27:39 +0800 | [diff] [blame] | 1227 | dcc->undiscard_blks += di.len; |
Chao Yu | ba48a33 | 2017-04-15 14:09:37 +0800 | [diff] [blame] | 1228 | __relocate_discard_cmd(dcc, prev_dc); |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1229 | di = prev_dc->di; |
| 1230 | tdc = prev_dc; |
| 1231 | merged = true; |
| 1232 | } |
| 1233 | |
| 1234 | if (next_dc && next_dc->state == D_PREP && |
| 1235 | next_dc->bdev == bdev && |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 1236 | __is_discard_front_mergeable(&di, &next_dc->di, |
| 1237 | max_discard_blocks)) { |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1238 | next_dc->di.lstart = di.lstart; |
| 1239 | next_dc->di.len += di.len; |
| 1240 | next_dc->di.start = di.start; |
Chao Yu | d84d1cb | 2017-04-18 19:27:39 +0800 | [diff] [blame] | 1241 | dcc->undiscard_blks += di.len; |
Chao Yu | ba48a33 | 2017-04-15 14:09:37 +0800 | [diff] [blame] | 1242 | __relocate_discard_cmd(dcc, next_dc); |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1243 | if (tdc) |
| 1244 | __remove_discard_cmd(sbi, tdc); |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1245 | merged = true; |
| 1246 | } |
| 1247 | |
Chao Yu | df0f6b4 | 2017-04-17 18:21:43 +0800 | [diff] [blame] | 1248 | if (!merged) { |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1249 | __insert_discard_tree(sbi, bdev, di.lstart, di.start, |
| 1250 | di.len, NULL, NULL); |
Chao Yu | df0f6b4 | 2017-04-17 18:21:43 +0800 | [diff] [blame] | 1251 | } |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1252 | next: |
| 1253 | prev_dc = next_dc; |
| 1254 | if (!prev_dc) |
| 1255 | break; |
| 1256 | |
| 1257 | node = rb_next(&prev_dc->rb_node); |
| 1258 | next_dc = rb_entry_safe(node, struct discard_cmd, rb_node); |
| 1259 | } |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1260 | } |
| 1261 | |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 1262 | static int __queue_discard_cmd(struct f2fs_sb_info *sbi, |
| 1263 | struct block_device *bdev, block_t blkstart, block_t blklen) |
| 1264 | { |
| 1265 | block_t lblkstart = blkstart; |
| 1266 | |
Chao Yu | 0243a5f | 2017-04-15 14:09:38 +0800 | [diff] [blame] | 1267 | trace_f2fs_queue_discard(bdev, blkstart, blklen); |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 1268 | |
| 1269 | if (sbi->s_ndevs) { |
| 1270 | int devi = f2fs_target_device_index(sbi, blkstart); |
| 1271 | |
| 1272 | blkstart -= FDEV(devi).start_blk; |
| 1273 | } |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 1274 | mutex_lock(&SM_I(sbi)->dcc_info->cmd_lock); |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1275 | __update_discard_tree_range(sbi, bdev, lblkstart, blkstart, blklen); |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 1276 | mutex_unlock(&SM_I(sbi)->dcc_info->cmd_lock); |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 1277 | return 0; |
| 1278 | } |
| 1279 | |
Chao Yu | 20ee438 | 2018-07-08 22:11:01 +0800 | [diff] [blame] | 1280 | static unsigned int __issue_discard_cmd_orderly(struct f2fs_sb_info *sbi, |
| 1281 | struct discard_policy *dpolicy) |
| 1282 | { |
| 1283 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
| 1284 | struct discard_cmd *prev_dc = NULL, *next_dc = NULL; |
| 1285 | struct rb_node **insert_p = NULL, *insert_parent = NULL; |
| 1286 | struct discard_cmd *dc; |
| 1287 | struct blk_plug plug; |
| 1288 | unsigned int pos = dcc->next_pos; |
| 1289 | unsigned int issued = 0; |
| 1290 | bool io_interrupted = false; |
| 1291 | |
| 1292 | mutex_lock(&dcc->cmd_lock); |
| 1293 | dc = (struct discard_cmd *)f2fs_lookup_rb_tree_ret(&dcc->root, |
| 1294 | NULL, pos, |
| 1295 | (struct rb_entry **)&prev_dc, |
| 1296 | (struct rb_entry **)&next_dc, |
| 1297 | &insert_p, &insert_parent, true); |
| 1298 | if (!dc) |
| 1299 | dc = next_dc; |
| 1300 | |
| 1301 | blk_start_plug(&plug); |
| 1302 | |
| 1303 | while (dc) { |
| 1304 | struct rb_node *node; |
Chao Yu | 6b9cb12 | 2018-08-08 10:14:55 +0800 | [diff] [blame] | 1305 | int err = 0; |
Chao Yu | 20ee438 | 2018-07-08 22:11:01 +0800 | [diff] [blame] | 1306 | |
| 1307 | if (dc->state != D_PREP) |
| 1308 | goto next; |
| 1309 | |
| 1310 | if (dpolicy->io_aware && !is_idle(sbi)) { |
| 1311 | io_interrupted = true; |
| 1312 | break; |
| 1313 | } |
| 1314 | |
| 1315 | dcc->next_pos = dc->lstart + dc->len; |
Chao Yu | 6b9cb12 | 2018-08-08 10:14:55 +0800 | [diff] [blame] | 1316 | err = __submit_discard_cmd(sbi, dpolicy, dc, &issued); |
Chao Yu | 20ee438 | 2018-07-08 22:11:01 +0800 | [diff] [blame] | 1317 | |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 1318 | if (issued >= dpolicy->max_requests) |
Chao Yu | 20ee438 | 2018-07-08 22:11:01 +0800 | [diff] [blame] | 1319 | break; |
| 1320 | next: |
| 1321 | node = rb_next(&dc->rb_node); |
Chao Yu | 6b9cb12 | 2018-08-08 10:14:55 +0800 | [diff] [blame] | 1322 | if (err) |
| 1323 | __remove_discard_cmd(sbi, dc); |
Chao Yu | 20ee438 | 2018-07-08 22:11:01 +0800 | [diff] [blame] | 1324 | dc = rb_entry_safe(node, struct discard_cmd, rb_node); |
| 1325 | } |
| 1326 | |
| 1327 | blk_finish_plug(&plug); |
| 1328 | |
| 1329 | if (!dc) |
| 1330 | dcc->next_pos = 0; |
| 1331 | |
| 1332 | mutex_unlock(&dcc->cmd_lock); |
| 1333 | |
| 1334 | if (!issued && io_interrupted) |
| 1335 | issued = -1; |
| 1336 | |
| 1337 | return issued; |
| 1338 | } |
| 1339 | |
Chao Yu | 78997b5 | 2017-10-04 09:08:34 +0800 | [diff] [blame] | 1340 | static int __issue_discard_cmd(struct f2fs_sb_info *sbi, |
| 1341 | struct discard_policy *dpolicy) |
Chao Yu | bd5b073 | 2017-04-25 20:21:37 +0800 | [diff] [blame] | 1342 | { |
| 1343 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
| 1344 | struct list_head *pend_list; |
| 1345 | struct discard_cmd *dc, *tmp; |
| 1346 | struct blk_plug plug; |
Chao Yu | 522d171 | 2018-07-08 22:08:09 +0800 | [diff] [blame] | 1347 | int i, issued = 0; |
Chao Yu | e6c6de1 | 2017-09-12 21:35:12 +0800 | [diff] [blame] | 1348 | bool io_interrupted = false; |
Chao Yu | bd5b073 | 2017-04-25 20:21:37 +0800 | [diff] [blame] | 1349 | |
Chao Yu | 78997b5 | 2017-10-04 09:08:34 +0800 | [diff] [blame] | 1350 | for (i = MAX_PLIST_NUM - 1; i >= 0; i--) { |
| 1351 | if (i + 1 < dpolicy->granularity) |
| 1352 | break; |
Chao Yu | 20ee438 | 2018-07-08 22:11:01 +0800 | [diff] [blame] | 1353 | |
| 1354 | if (i < DEFAULT_DISCARD_GRANULARITY && dpolicy->ordered) |
| 1355 | return __issue_discard_cmd_orderly(sbi, dpolicy); |
| 1356 | |
Chao Yu | bd5b073 | 2017-04-25 20:21:37 +0800 | [diff] [blame] | 1357 | pend_list = &dcc->pend_list[i]; |
Chao Yu | 33da62c | 2017-10-04 09:08:35 +0800 | [diff] [blame] | 1358 | |
| 1359 | mutex_lock(&dcc->cmd_lock); |
Chao Yu | 49c60c6 | 2018-01-08 18:48:33 +0800 | [diff] [blame] | 1360 | if (list_empty(pend_list)) |
| 1361 | goto next; |
Chao Yu | 67fce70 | 2018-06-22 16:06:59 +0800 | [diff] [blame] | 1362 | if (unlikely(dcc->rbtree_check)) |
| 1363 | f2fs_bug_on(sbi, !f2fs_check_rb_tree_consistence(sbi, |
| 1364 | &dcc->root)); |
Chao Yu | 33da62c | 2017-10-04 09:08:35 +0800 | [diff] [blame] | 1365 | blk_start_plug(&plug); |
Chao Yu | bd5b073 | 2017-04-25 20:21:37 +0800 | [diff] [blame] | 1366 | list_for_each_entry_safe(dc, tmp, pend_list, list) { |
| 1367 | f2fs_bug_on(sbi, dc->state != D_PREP); |
| 1368 | |
Chao Yu | ecc9aa0 | 2017-10-04 09:08:33 +0800 | [diff] [blame] | 1369 | if (dpolicy->io_aware && i < dpolicy->io_aware_gran && |
| 1370 | !is_idle(sbi)) { |
Chao Yu | e6c6de1 | 2017-09-12 21:35:12 +0800 | [diff] [blame] | 1371 | io_interrupted = true; |
Chao Yu | 522d171 | 2018-07-08 22:08:09 +0800 | [diff] [blame] | 1372 | break; |
Chao Yu | e6c6de1 | 2017-09-12 21:35:12 +0800 | [diff] [blame] | 1373 | } |
| 1374 | |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 1375 | __submit_discard_cmd(sbi, dpolicy, dc, &issued); |
Chao Yu | 522d171 | 2018-07-08 22:08:09 +0800 | [diff] [blame] | 1376 | |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 1377 | if (issued >= dpolicy->max_requests) |
Chao Yu | 33da62c | 2017-10-04 09:08:35 +0800 | [diff] [blame] | 1378 | break; |
Chao Yu | bd5b073 | 2017-04-25 20:21:37 +0800 | [diff] [blame] | 1379 | } |
Chao Yu | 33da62c | 2017-10-04 09:08:35 +0800 | [diff] [blame] | 1380 | blk_finish_plug(&plug); |
Chao Yu | 49c60c6 | 2018-01-08 18:48:33 +0800 | [diff] [blame] | 1381 | next: |
Chao Yu | 33da62c | 2017-10-04 09:08:35 +0800 | [diff] [blame] | 1382 | mutex_unlock(&dcc->cmd_lock); |
| 1383 | |
Chao Yu | 522d171 | 2018-07-08 22:08:09 +0800 | [diff] [blame] | 1384 | if (issued >= dpolicy->max_requests || io_interrupted) |
Chao Yu | 33da62c | 2017-10-04 09:08:35 +0800 | [diff] [blame] | 1385 | break; |
Chao Yu | bd5b073 | 2017-04-25 20:21:37 +0800 | [diff] [blame] | 1386 | } |
Chao Yu | 969d1b1 | 2017-08-07 23:09:56 +0800 | [diff] [blame] | 1387 | |
Chao Yu | e6c6de1 | 2017-09-12 21:35:12 +0800 | [diff] [blame] | 1388 | if (!issued && io_interrupted) |
| 1389 | issued = -1; |
| 1390 | |
Chao Yu | 969d1b1 | 2017-08-07 23:09:56 +0800 | [diff] [blame] | 1391 | return issued; |
| 1392 | } |
| 1393 | |
Chao Yu | cf5c759 | 2017-10-04 09:08:37 +0800 | [diff] [blame] | 1394 | static bool __drop_discard_cmd(struct f2fs_sb_info *sbi) |
Chao Yu | 969d1b1 | 2017-08-07 23:09:56 +0800 | [diff] [blame] | 1395 | { |
| 1396 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
| 1397 | struct list_head *pend_list; |
| 1398 | struct discard_cmd *dc, *tmp; |
| 1399 | int i; |
Chao Yu | cf5c759 | 2017-10-04 09:08:37 +0800 | [diff] [blame] | 1400 | bool dropped = false; |
Chao Yu | 969d1b1 | 2017-08-07 23:09:56 +0800 | [diff] [blame] | 1401 | |
| 1402 | mutex_lock(&dcc->cmd_lock); |
| 1403 | for (i = MAX_PLIST_NUM - 1; i >= 0; i--) { |
| 1404 | pend_list = &dcc->pend_list[i]; |
| 1405 | list_for_each_entry_safe(dc, tmp, pend_list, list) { |
| 1406 | f2fs_bug_on(sbi, dc->state != D_PREP); |
| 1407 | __remove_discard_cmd(sbi, dc); |
Chao Yu | cf5c759 | 2017-10-04 09:08:37 +0800 | [diff] [blame] | 1408 | dropped = true; |
Chao Yu | 969d1b1 | 2017-08-07 23:09:56 +0800 | [diff] [blame] | 1409 | } |
| 1410 | } |
| 1411 | mutex_unlock(&dcc->cmd_lock); |
Chao Yu | cf5c759 | 2017-10-04 09:08:37 +0800 | [diff] [blame] | 1412 | |
| 1413 | return dropped; |
Chao Yu | bd5b073 | 2017-04-25 20:21:37 +0800 | [diff] [blame] | 1414 | } |
| 1415 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1416 | void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi) |
Chao Yu | 7950e9a | 2018-01-18 17:23:29 +0800 | [diff] [blame] | 1417 | { |
| 1418 | __drop_discard_cmd(sbi); |
| 1419 | } |
| 1420 | |
Chao Yu | 0ea8051 | 2017-10-28 16:52:32 +0800 | [diff] [blame] | 1421 | static unsigned int __wait_one_discard_bio(struct f2fs_sb_info *sbi, |
Chao Yu | 2a510c00 | 2017-06-05 18:29:06 +0800 | [diff] [blame] | 1422 | struct discard_cmd *dc) |
| 1423 | { |
| 1424 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
Chao Yu | 0ea8051 | 2017-10-28 16:52:32 +0800 | [diff] [blame] | 1425 | unsigned int len = 0; |
Chao Yu | 2a510c00 | 2017-06-05 18:29:06 +0800 | [diff] [blame] | 1426 | |
| 1427 | wait_for_completion_io(&dc->wait); |
| 1428 | mutex_lock(&dcc->cmd_lock); |
| 1429 | f2fs_bug_on(sbi, dc->state != D_DONE); |
| 1430 | dc->ref--; |
Chao Yu | 0ea8051 | 2017-10-28 16:52:32 +0800 | [diff] [blame] | 1431 | if (!dc->ref) { |
| 1432 | if (!dc->error) |
| 1433 | len = dc->len; |
Chao Yu | 2a510c00 | 2017-06-05 18:29:06 +0800 | [diff] [blame] | 1434 | __remove_discard_cmd(sbi, dc); |
Chao Yu | 0ea8051 | 2017-10-28 16:52:32 +0800 | [diff] [blame] | 1435 | } |
Chao Yu | 2a510c00 | 2017-06-05 18:29:06 +0800 | [diff] [blame] | 1436 | mutex_unlock(&dcc->cmd_lock); |
Chao Yu | 0ea8051 | 2017-10-28 16:52:32 +0800 | [diff] [blame] | 1437 | |
| 1438 | return len; |
Chao Yu | 2a510c00 | 2017-06-05 18:29:06 +0800 | [diff] [blame] | 1439 | } |
| 1440 | |
Chao Yu | 0ea8051 | 2017-10-28 16:52:32 +0800 | [diff] [blame] | 1441 | static unsigned int __wait_discard_cmd_range(struct f2fs_sb_info *sbi, |
Chao Yu | 78997b5 | 2017-10-04 09:08:34 +0800 | [diff] [blame] | 1442 | struct discard_policy *dpolicy, |
| 1443 | block_t start, block_t end) |
Chao Yu | 63a94fa | 2017-04-25 20:21:38 +0800 | [diff] [blame] | 1444 | { |
| 1445 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
Chao Yu | 78997b5 | 2017-10-04 09:08:34 +0800 | [diff] [blame] | 1446 | struct list_head *wait_list = (dpolicy->type == DPOLICY_FSTRIM) ? |
| 1447 | &(dcc->fstrim_list) : &(dcc->wait_list); |
Chao Yu | 63a94fa | 2017-04-25 20:21:38 +0800 | [diff] [blame] | 1448 | struct discard_cmd *dc, *tmp; |
Chao Yu | 6afae63 | 2017-05-19 23:46:45 +0800 | [diff] [blame] | 1449 | bool need_wait; |
Chao Yu | 0ea8051 | 2017-10-28 16:52:32 +0800 | [diff] [blame] | 1450 | unsigned int trimmed = 0; |
Chao Yu | 6afae63 | 2017-05-19 23:46:45 +0800 | [diff] [blame] | 1451 | |
| 1452 | next: |
| 1453 | need_wait = false; |
Chao Yu | 63a94fa | 2017-04-25 20:21:38 +0800 | [diff] [blame] | 1454 | |
| 1455 | mutex_lock(&dcc->cmd_lock); |
| 1456 | list_for_each_entry_safe(dc, tmp, wait_list, list) { |
Chao Yu | 8412663 | 2017-10-04 09:08:32 +0800 | [diff] [blame] | 1457 | if (dc->lstart + dc->len <= start || end <= dc->lstart) |
| 1458 | continue; |
Chao Yu | 78997b5 | 2017-10-04 09:08:34 +0800 | [diff] [blame] | 1459 | if (dc->len < dpolicy->granularity) |
Chao Yu | 8412663 | 2017-10-04 09:08:32 +0800 | [diff] [blame] | 1460 | continue; |
Chao Yu | 78997b5 | 2017-10-04 09:08:34 +0800 | [diff] [blame] | 1461 | if (dc->state == D_DONE && !dc->ref) { |
Chao Yu | 63a94fa | 2017-04-25 20:21:38 +0800 | [diff] [blame] | 1462 | wait_for_completion_io(&dc->wait); |
Chao Yu | 0ea8051 | 2017-10-28 16:52:32 +0800 | [diff] [blame] | 1463 | if (!dc->error) |
| 1464 | trimmed += dc->len; |
Chao Yu | 63a94fa | 2017-04-25 20:21:38 +0800 | [diff] [blame] | 1465 | __remove_discard_cmd(sbi, dc); |
Chao Yu | 6afae63 | 2017-05-19 23:46:45 +0800 | [diff] [blame] | 1466 | } else { |
| 1467 | dc->ref++; |
| 1468 | need_wait = true; |
| 1469 | break; |
Chao Yu | 63a94fa | 2017-04-25 20:21:38 +0800 | [diff] [blame] | 1470 | } |
| 1471 | } |
| 1472 | mutex_unlock(&dcc->cmd_lock); |
Chao Yu | 6afae63 | 2017-05-19 23:46:45 +0800 | [diff] [blame] | 1473 | |
| 1474 | if (need_wait) { |
Chao Yu | 0ea8051 | 2017-10-28 16:52:32 +0800 | [diff] [blame] | 1475 | trimmed += __wait_one_discard_bio(sbi, dc); |
Chao Yu | 6afae63 | 2017-05-19 23:46:45 +0800 | [diff] [blame] | 1476 | goto next; |
| 1477 | } |
Chao Yu | 0ea8051 | 2017-10-28 16:52:32 +0800 | [diff] [blame] | 1478 | |
| 1479 | return trimmed; |
Chao Yu | 63a94fa | 2017-04-25 20:21:38 +0800 | [diff] [blame] | 1480 | } |
| 1481 | |
Chao Yu | 01f9cf6 | 2018-06-25 20:33:24 +0800 | [diff] [blame] | 1482 | static unsigned int __wait_all_discard_cmd(struct f2fs_sb_info *sbi, |
Chao Yu | 78997b5 | 2017-10-04 09:08:34 +0800 | [diff] [blame] | 1483 | struct discard_policy *dpolicy) |
Chao Yu | 8412663 | 2017-10-04 09:08:32 +0800 | [diff] [blame] | 1484 | { |
Jaegeuk Kim | 9a99718 | 2018-05-24 13:57:26 -0700 | [diff] [blame] | 1485 | struct discard_policy dp; |
Chao Yu | 01f9cf6 | 2018-06-25 20:33:24 +0800 | [diff] [blame] | 1486 | unsigned int discard_blks; |
Jaegeuk Kim | 9a99718 | 2018-05-24 13:57:26 -0700 | [diff] [blame] | 1487 | |
Chao Yu | 01f9cf6 | 2018-06-25 20:33:24 +0800 | [diff] [blame] | 1488 | if (dpolicy) |
| 1489 | return __wait_discard_cmd_range(sbi, dpolicy, 0, UINT_MAX); |
Jaegeuk Kim | 9a99718 | 2018-05-24 13:57:26 -0700 | [diff] [blame] | 1490 | |
| 1491 | /* wait all */ |
Jaegeuk Kim | 8bb4f25 | 2018-05-29 09:58:42 -0700 | [diff] [blame] | 1492 | __init_discard_policy(sbi, &dp, DPOLICY_FSTRIM, 1); |
Chao Yu | 01f9cf6 | 2018-06-25 20:33:24 +0800 | [diff] [blame] | 1493 | discard_blks = __wait_discard_cmd_range(sbi, &dp, 0, UINT_MAX); |
Jaegeuk Kim | 8bb4f25 | 2018-05-29 09:58:42 -0700 | [diff] [blame] | 1494 | __init_discard_policy(sbi, &dp, DPOLICY_UMOUNT, 1); |
Chao Yu | 01f9cf6 | 2018-06-25 20:33:24 +0800 | [diff] [blame] | 1495 | discard_blks += __wait_discard_cmd_range(sbi, &dp, 0, UINT_MAX); |
| 1496 | |
| 1497 | return discard_blks; |
Chao Yu | 8412663 | 2017-10-04 09:08:32 +0800 | [diff] [blame] | 1498 | } |
| 1499 | |
Jaegeuk Kim | 4e6a8d9 | 2016-12-29 14:07:53 -0800 | [diff] [blame] | 1500 | /* This should be covered by global mutex, &sit_i->sentry_lock */ |
Wei Yongjun | 94b1e10 | 2018-01-05 09:41:20 +0000 | [diff] [blame] | 1501 | 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] | 1502 | { |
Jaegeuk Kim | 0b54fb8 | 2017-01-11 14:40:24 -0800 | [diff] [blame] | 1503 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1504 | struct discard_cmd *dc; |
Chao Yu | ec9895a | 2017-04-26 17:39:54 +0800 | [diff] [blame] | 1505 | bool need_wait = false; |
Chao Yu | 275b66b | 2016-08-29 23:58:34 +0800 | [diff] [blame] | 1506 | |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 1507 | mutex_lock(&dcc->cmd_lock); |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1508 | dc = (struct discard_cmd *)f2fs_lookup_rb_tree(&dcc->root, |
| 1509 | NULL, blkaddr); |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1510 | if (dc) { |
Chao Yu | ec9895a | 2017-04-26 17:39:54 +0800 | [diff] [blame] | 1511 | if (dc->state == D_PREP) { |
| 1512 | __punch_discard_cmd(sbi, dc, blkaddr); |
| 1513 | } else { |
| 1514 | dc->ref++; |
| 1515 | need_wait = true; |
| 1516 | } |
Chao Yu | 275b66b | 2016-08-29 23:58:34 +0800 | [diff] [blame] | 1517 | } |
Chao Yu | d431413 | 2017-04-05 18:19:49 +0800 | [diff] [blame] | 1518 | mutex_unlock(&dcc->cmd_lock); |
Chao Yu | ec9895a | 2017-04-26 17:39:54 +0800 | [diff] [blame] | 1519 | |
Chao Yu | 2a510c00 | 2017-06-05 18:29:06 +0800 | [diff] [blame] | 1520 | if (need_wait) |
| 1521 | __wait_one_discard_bio(sbi, dc); |
Chao Yu | d431413 | 2017-04-05 18:19:49 +0800 | [diff] [blame] | 1522 | } |
Chao Yu | 22d375d | 2017-04-05 18:19:48 +0800 | [diff] [blame] | 1523 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1524 | void f2fs_stop_discard_thread(struct f2fs_sb_info *sbi) |
Chao Yu | cce1325 | 2017-06-29 23:17:45 +0800 | [diff] [blame] | 1525 | { |
| 1526 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
| 1527 | |
| 1528 | if (dcc && dcc->f2fs_issue_discard) { |
| 1529 | struct task_struct *discard_thread = dcc->f2fs_issue_discard; |
| 1530 | |
| 1531 | dcc->f2fs_issue_discard = NULL; |
| 1532 | kthread_stop(discard_thread); |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 1533 | } |
| 1534 | } |
| 1535 | |
Chao Yu | 8412663 | 2017-10-04 09:08:32 +0800 | [diff] [blame] | 1536 | /* This comes from f2fs_put_super */ |
Chao Yu | cf5c759 | 2017-10-04 09:08:37 +0800 | [diff] [blame] | 1537 | bool f2fs_wait_discard_bios(struct f2fs_sb_info *sbi) |
Chao Yu | 275b66b | 2016-08-29 23:58:34 +0800 | [diff] [blame] | 1538 | { |
Chao Yu | 969d1b1 | 2017-08-07 23:09:56 +0800 | [diff] [blame] | 1539 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
Chao Yu | 78997b5 | 2017-10-04 09:08:34 +0800 | [diff] [blame] | 1540 | struct discard_policy dpolicy; |
Chao Yu | cf5c759 | 2017-10-04 09:08:37 +0800 | [diff] [blame] | 1541 | bool dropped; |
Chao Yu | 969d1b1 | 2017-08-07 23:09:56 +0800 | [diff] [blame] | 1542 | |
Jaegeuk Kim | 8bb4f25 | 2018-05-29 09:58:42 -0700 | [diff] [blame] | 1543 | __init_discard_policy(sbi, &dpolicy, DPOLICY_UMOUNT, |
| 1544 | dcc->discard_granularity); |
Chao Yu | 78997b5 | 2017-10-04 09:08:34 +0800 | [diff] [blame] | 1545 | __issue_discard_cmd(sbi, &dpolicy); |
Chao Yu | cf5c759 | 2017-10-04 09:08:37 +0800 | [diff] [blame] | 1546 | dropped = __drop_discard_cmd(sbi); |
Chao Yu | cf5c759 | 2017-10-04 09:08:37 +0800 | [diff] [blame] | 1547 | |
Jaegeuk Kim | 9a99718 | 2018-05-24 13:57:26 -0700 | [diff] [blame] | 1548 | /* just to make sure there is no pending discard commands */ |
| 1549 | __wait_all_discard_cmd(sbi, NULL); |
Chao Yu | 2482c43 | 2018-07-08 22:16:53 +0800 | [diff] [blame] | 1550 | |
| 1551 | f2fs_bug_on(sbi, atomic_read(&dcc->discard_cmd_cnt)); |
Chao Yu | cf5c759 | 2017-10-04 09:08:37 +0800 | [diff] [blame] | 1552 | return dropped; |
Chao Yu | 969d1b1 | 2017-08-07 23:09:56 +0800 | [diff] [blame] | 1553 | } |
| 1554 | |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 1555 | static int issue_discard_thread(void *data) |
| 1556 | { |
| 1557 | struct f2fs_sb_info *sbi = data; |
| 1558 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
| 1559 | wait_queue_head_t *q = &dcc->discard_wait_queue; |
Chao Yu | 78997b5 | 2017-10-04 09:08:34 +0800 | [diff] [blame] | 1560 | struct discard_policy dpolicy; |
Chao Yu | 969d1b1 | 2017-08-07 23:09:56 +0800 | [diff] [blame] | 1561 | unsigned int wait_ms = DEF_MIN_DISCARD_ISSUE_TIME; |
| 1562 | int issued; |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 1563 | |
Jaegeuk Kim | 1d7be27 | 2017-05-17 10:36:58 -0700 | [diff] [blame] | 1564 | set_freezable(); |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 1565 | |
Jaegeuk Kim | 1d7be27 | 2017-05-17 10:36:58 -0700 | [diff] [blame] | 1566 | do { |
Jaegeuk Kim | 8bb4f25 | 2018-05-29 09:58:42 -0700 | [diff] [blame] | 1567 | __init_discard_policy(sbi, &dpolicy, DPOLICY_BG, |
Chao Yu | 78997b5 | 2017-10-04 09:08:34 +0800 | [diff] [blame] | 1568 | dcc->discard_granularity); |
| 1569 | |
Chao Yu | 969d1b1 | 2017-08-07 23:09:56 +0800 | [diff] [blame] | 1570 | wait_event_interruptible_timeout(*q, |
| 1571 | kthread_should_stop() || freezing(current) || |
| 1572 | dcc->discard_wake, |
| 1573 | msecs_to_jiffies(wait_ms)); |
Sheng Yong | 35a9a76 | 2018-05-08 17:51:34 +0800 | [diff] [blame] | 1574 | |
| 1575 | if (dcc->discard_wake) |
| 1576 | dcc->discard_wake = 0; |
| 1577 | |
Jaegeuk Kim | 1d7be27 | 2017-05-17 10:36:58 -0700 | [diff] [blame] | 1578 | if (try_to_freeze()) |
| 1579 | continue; |
Chao Yu | 3b60d80 | 2018-01-25 18:57:27 +0800 | [diff] [blame] | 1580 | if (f2fs_readonly(sbi->sb)) |
| 1581 | continue; |
Jaegeuk Kim | 1d7be27 | 2017-05-17 10:36:58 -0700 | [diff] [blame] | 1582 | if (kthread_should_stop()) |
| 1583 | return 0; |
Yunlei He | d618477 | 2018-04-13 11:08:05 +0800 | [diff] [blame] | 1584 | if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) { |
| 1585 | wait_ms = dpolicy.max_interval; |
| 1586 | continue; |
| 1587 | } |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 1588 | |
Jaegeuk Kim | 5b0e953 | 2018-05-07 14:22:40 -0700 | [diff] [blame] | 1589 | if (sbi->gc_mode == GC_URGENT) |
Jaegeuk Kim | 8bb4f25 | 2018-05-29 09:58:42 -0700 | [diff] [blame] | 1590 | __init_discard_policy(sbi, &dpolicy, DPOLICY_FORCE, 1); |
Jaegeuk Kim | 1d7be27 | 2017-05-17 10:36:58 -0700 | [diff] [blame] | 1591 | |
Chao Yu | dc6febb | 2017-07-22 08:52:23 +0800 | [diff] [blame] | 1592 | sb_start_intwrite(sbi->sb); |
| 1593 | |
Chao Yu | 78997b5 | 2017-10-04 09:08:34 +0800 | [diff] [blame] | 1594 | issued = __issue_discard_cmd(sbi, &dpolicy); |
Yunlei He | f9d1dce | 2018-04-08 15:11:11 +0800 | [diff] [blame] | 1595 | if (issued > 0) { |
Chao Yu | 78997b5 | 2017-10-04 09:08:34 +0800 | [diff] [blame] | 1596 | __wait_all_discard_cmd(sbi, &dpolicy); |
| 1597 | wait_ms = dpolicy.min_interval; |
Yunlei He | f9d1dce | 2018-04-08 15:11:11 +0800 | [diff] [blame] | 1598 | } else if (issued == -1){ |
| 1599 | wait_ms = dpolicy.mid_interval; |
Chao Yu | 969d1b1 | 2017-08-07 23:09:56 +0800 | [diff] [blame] | 1600 | } else { |
Chao Yu | 78997b5 | 2017-10-04 09:08:34 +0800 | [diff] [blame] | 1601 | wait_ms = dpolicy.max_interval; |
Chao Yu | 969d1b1 | 2017-08-07 23:09:56 +0800 | [diff] [blame] | 1602 | } |
Jaegeuk Kim | 1d7be27 | 2017-05-17 10:36:58 -0700 | [diff] [blame] | 1603 | |
Chao Yu | dc6febb | 2017-07-22 08:52:23 +0800 | [diff] [blame] | 1604 | sb_end_intwrite(sbi->sb); |
| 1605 | |
Jaegeuk Kim | 1d7be27 | 2017-05-17 10:36:58 -0700 | [diff] [blame] | 1606 | } while (!kthread_should_stop()); |
| 1607 | return 0; |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 1608 | } |
| 1609 | |
Damien Le Moal | f46e8809 | 2016-10-28 17:45:06 +0900 | [diff] [blame] | 1610 | #ifdef CONFIG_BLK_DEV_ZONED |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 1611 | static int __f2fs_issue_discard_zone(struct f2fs_sb_info *sbi, |
| 1612 | struct block_device *bdev, block_t blkstart, block_t blklen) |
Damien Le Moal | f46e8809 | 2016-10-28 17:45:06 +0900 | [diff] [blame] | 1613 | { |
Jaegeuk Kim | 92592285 | 2017-02-22 20:18:35 -0800 | [diff] [blame] | 1614 | sector_t sector, nr_sects; |
Kinglong Mee | 10a875f | 2017-03-08 09:49:53 +0800 | [diff] [blame] | 1615 | block_t lblkstart = blkstart; |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 1616 | int devi = 0; |
Damien Le Moal | f46e8809 | 2016-10-28 17:45:06 +0900 | [diff] [blame] | 1617 | |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 1618 | if (sbi->s_ndevs) { |
| 1619 | devi = f2fs_target_device_index(sbi, blkstart); |
| 1620 | blkstart -= FDEV(devi).start_blk; |
| 1621 | } |
Damien Le Moal | f46e8809 | 2016-10-28 17:45:06 +0900 | [diff] [blame] | 1622 | |
| 1623 | /* |
| 1624 | * We need to know the type of the zone: for conventional zones, |
| 1625 | * use regular discard if the drive supports it. For sequential |
| 1626 | * zones, reset the zone write pointer. |
| 1627 | */ |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 1628 | switch (get_blkz_type(sbi, bdev, blkstart)) { |
Damien Le Moal | f46e8809 | 2016-10-28 17:45:06 +0900 | [diff] [blame] | 1629 | |
| 1630 | case BLK_ZONE_TYPE_CONVENTIONAL: |
| 1631 | if (!blk_queue_discard(bdev_get_queue(bdev))) |
| 1632 | return 0; |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 1633 | return __queue_discard_cmd(sbi, bdev, lblkstart, blklen); |
Damien Le Moal | f46e8809 | 2016-10-28 17:45:06 +0900 | [diff] [blame] | 1634 | case BLK_ZONE_TYPE_SEQWRITE_REQ: |
| 1635 | case BLK_ZONE_TYPE_SEQWRITE_PREF: |
Jaegeuk Kim | 92592285 | 2017-02-22 20:18:35 -0800 | [diff] [blame] | 1636 | sector = SECTOR_FROM_BLOCK(blkstart); |
| 1637 | nr_sects = SECTOR_FROM_BLOCK(blklen); |
| 1638 | |
| 1639 | if (sector & (bdev_zone_sectors(bdev) - 1) || |
| 1640 | nr_sects != bdev_zone_sectors(bdev)) { |
| 1641 | f2fs_msg(sbi->sb, KERN_INFO, |
| 1642 | "(%d) %s: Unaligned discard attempted (block %x + %x)", |
| 1643 | devi, sbi->s_ndevs ? FDEV(devi).path: "", |
| 1644 | blkstart, blklen); |
| 1645 | return -EIO; |
| 1646 | } |
Jaegeuk Kim | d50aaee | 2017-02-15 11:14:06 -0800 | [diff] [blame] | 1647 | trace_f2fs_issue_reset_zone(bdev, blkstart); |
Damien Le Moal | f46e8809 | 2016-10-28 17:45:06 +0900 | [diff] [blame] | 1648 | return blkdev_reset_zones(bdev, sector, |
| 1649 | nr_sects, GFP_NOFS); |
| 1650 | default: |
| 1651 | /* Unknown zone type: broken device ? */ |
| 1652 | return -EIO; |
| 1653 | } |
| 1654 | } |
| 1655 | #endif |
| 1656 | |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 1657 | static int __issue_discard_async(struct f2fs_sb_info *sbi, |
| 1658 | struct block_device *bdev, block_t blkstart, block_t blklen) |
| 1659 | { |
| 1660 | #ifdef CONFIG_BLK_DEV_ZONED |
Sheng Yong | ccd31cb | 2018-02-06 12:31:17 +0800 | [diff] [blame] | 1661 | if (f2fs_sb_has_blkzoned(sbi->sb) && |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 1662 | bdev_zoned_model(bdev) != BLK_ZONED_NONE) |
| 1663 | return __f2fs_issue_discard_zone(sbi, bdev, blkstart, blklen); |
| 1664 | #endif |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 1665 | return __queue_discard_cmd(sbi, bdev, blkstart, blklen); |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 1666 | } |
| 1667 | |
Jaegeuk Kim | 1e87a78 | 2014-04-15 13:57:55 +0900 | [diff] [blame] | 1668 | static int f2fs_issue_discard(struct f2fs_sb_info *sbi, |
Jaegeuk Kim | 3720887 | 2013-11-12 16:55:17 +0900 | [diff] [blame] | 1669 | block_t blkstart, block_t blklen) |
| 1670 | { |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 1671 | sector_t start = blkstart, len = 0; |
| 1672 | struct block_device *bdev; |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 1673 | struct seg_entry *se; |
| 1674 | unsigned int offset; |
| 1675 | block_t i; |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 1676 | int err = 0; |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 1677 | |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 1678 | bdev = f2fs_target_device(sbi, blkstart, NULL); |
| 1679 | |
| 1680 | for (i = blkstart; i < blkstart + blklen; i++, len++) { |
| 1681 | if (i != start) { |
| 1682 | struct block_device *bdev2 = |
| 1683 | f2fs_target_device(sbi, i, NULL); |
| 1684 | |
| 1685 | if (bdev2 != bdev) { |
| 1686 | err = __issue_discard_async(sbi, bdev, |
| 1687 | start, len); |
| 1688 | if (err) |
| 1689 | return err; |
| 1690 | bdev = bdev2; |
| 1691 | start = i; |
| 1692 | len = 0; |
| 1693 | } |
| 1694 | } |
| 1695 | |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 1696 | se = get_seg_entry(sbi, GET_SEGNO(sbi, i)); |
| 1697 | offset = GET_BLKOFF_FROM_SEG0(sbi, i); |
| 1698 | |
| 1699 | if (!f2fs_test_and_set_bit(offset, se->discard_map)) |
| 1700 | sbi->discard_blks--; |
| 1701 | } |
Damien Le Moal | f46e8809 | 2016-10-28 17:45:06 +0900 | [diff] [blame] | 1702 | |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 1703 | if (len) |
| 1704 | err = __issue_discard_async(sbi, bdev, start, len); |
| 1705 | return err; |
Jaegeuk Kim | 1e87a78 | 2014-04-15 13:57:55 +0900 | [diff] [blame] | 1706 | } |
| 1707 | |
Jaegeuk Kim | 25290fa | 2016-12-29 22:06:15 -0800 | [diff] [blame] | 1708 | static bool add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc, |
| 1709 | bool check_only) |
Jaegeuk Kim | adf4983 | 2014-10-28 22:27:59 -0700 | [diff] [blame] | 1710 | { |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 1711 | int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long); |
| 1712 | int max_blocks = sbi->blocks_per_seg; |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 1713 | struct seg_entry *se = get_seg_entry(sbi, cpc->trim_start); |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 1714 | unsigned long *cur_map = (unsigned long *)se->cur_valid_map; |
| 1715 | unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map; |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 1716 | unsigned long *discard_map = (unsigned long *)se->discard_map; |
Jaegeuk Kim | 60a3b78 | 2015-02-10 16:44:29 -0800 | [diff] [blame] | 1717 | unsigned long *dmap = SIT_I(sbi)->tmp_map; |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 1718 | unsigned int start = 0, end = -1; |
Chao Yu | c473f1a | 2017-04-27 20:40:39 +0800 | [diff] [blame] | 1719 | bool force = (cpc->reason & CP_DISCARD); |
Chao Yu | a7eeb823 | 2017-03-28 18:18:50 +0800 | [diff] [blame] | 1720 | struct discard_entry *de = NULL; |
Chao Yu | 46f84c2 | 2017-04-15 14:09:36 +0800 | [diff] [blame] | 1721 | struct list_head *head = &SM_I(sbi)->dcc_info->entry_list; |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 1722 | int i; |
| 1723 | |
Jaegeuk Kim | 3e02574 | 2016-08-02 10:56:40 -0700 | [diff] [blame] | 1724 | if (se->valid_blocks == max_blocks || !f2fs_discard_en(sbi)) |
Jaegeuk Kim | 25290fa | 2016-12-29 22:06:15 -0800 | [diff] [blame] | 1725 | return false; |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 1726 | |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 1727 | if (!force) { |
| 1728 | if (!test_opt(sbi, DISCARD) || !se->valid_blocks || |
Jaegeuk Kim | 0b54fb8 | 2017-01-11 14:40:24 -0800 | [diff] [blame] | 1729 | SM_I(sbi)->dcc_info->nr_discards >= |
| 1730 | SM_I(sbi)->dcc_info->max_discards) |
Jaegeuk Kim | 25290fa | 2016-12-29 22:06:15 -0800 | [diff] [blame] | 1731 | return false; |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 1732 | } |
| 1733 | |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 1734 | /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */ |
| 1735 | for (i = 0; i < entries; i++) |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 1736 | dmap[i] = force ? ~ckpt_map[i] & ~discard_map[i] : |
Jaegeuk Kim | d7bc248 | 2014-12-12 13:53:41 -0800 | [diff] [blame] | 1737 | (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i]; |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 1738 | |
Jaegeuk Kim | 0b54fb8 | 2017-01-11 14:40:24 -0800 | [diff] [blame] | 1739 | while (force || SM_I(sbi)->dcc_info->nr_discards <= |
| 1740 | SM_I(sbi)->dcc_info->max_discards) { |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 1741 | start = __find_rev_next_bit(dmap, max_blocks, end + 1); |
| 1742 | if (start >= max_blocks) |
| 1743 | break; |
| 1744 | |
| 1745 | end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1); |
Yunlei He | c7b41e1 | 2016-07-07 12:13:33 +0800 | [diff] [blame] | 1746 | if (force && start && end != max_blocks |
| 1747 | && (end - start) < cpc->trim_minlen) |
| 1748 | continue; |
| 1749 | |
Jaegeuk Kim | 25290fa | 2016-12-29 22:06:15 -0800 | [diff] [blame] | 1750 | if (check_only) |
| 1751 | return true; |
| 1752 | |
Chao Yu | a7eeb823 | 2017-03-28 18:18:50 +0800 | [diff] [blame] | 1753 | if (!de) { |
| 1754 | de = f2fs_kmem_cache_alloc(discard_entry_slab, |
| 1755 | GFP_F2FS_ZERO); |
| 1756 | de->start_blkaddr = START_BLOCK(sbi, cpc->trim_start); |
| 1757 | list_add_tail(&de->list, head); |
| 1758 | } |
| 1759 | |
| 1760 | for (i = start; i < end; i++) |
| 1761 | __set_bit_le(i, (void *)de->discard_map); |
| 1762 | |
| 1763 | SM_I(sbi)->dcc_info->nr_discards += end - start; |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 1764 | } |
Jaegeuk Kim | 25290fa | 2016-12-29 22:06:15 -0800 | [diff] [blame] | 1765 | return false; |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 1766 | } |
| 1767 | |
Chao Yu | af8ff65 | 2018-04-25 17:38:29 +0800 | [diff] [blame] | 1768 | static void release_discard_addr(struct discard_entry *entry) |
| 1769 | { |
| 1770 | list_del(&entry->list); |
| 1771 | kmem_cache_free(discard_entry_slab, entry); |
| 1772 | } |
| 1773 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1774 | void f2fs_release_discard_addrs(struct f2fs_sb_info *sbi) |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 1775 | { |
Chao Yu | 46f84c2 | 2017-04-15 14:09:36 +0800 | [diff] [blame] | 1776 | struct list_head *head = &(SM_I(sbi)->dcc_info->entry_list); |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 1777 | struct discard_entry *entry, *this; |
| 1778 | |
| 1779 | /* drop caches */ |
Chao Yu | af8ff65 | 2018-04-25 17:38:29 +0800 | [diff] [blame] | 1780 | list_for_each_entry_safe(entry, this, head, list) |
| 1781 | release_discard_addr(entry); |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 1782 | } |
| 1783 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 1784 | /* |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1785 | * Should call f2fs_clear_prefree_segments after checkpoint is done. |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1786 | */ |
| 1787 | static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi) |
| 1788 | { |
| 1789 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
Chao Yu | b65ee14 | 2014-08-04 10:10:07 +0800 | [diff] [blame] | 1790 | unsigned int segno; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1791 | |
| 1792 | mutex_lock(&dirty_i->seglist_lock); |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 1793 | for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], MAIN_SEGS(sbi)) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1794 | __set_test_and_free(sbi, segno); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1795 | mutex_unlock(&dirty_i->seglist_lock); |
| 1796 | } |
| 1797 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1798 | void f2fs_clear_prefree_segments(struct f2fs_sb_info *sbi, |
| 1799 | struct cp_control *cpc) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1800 | { |
Chao Yu | 969d1b1 | 2017-08-07 23:09:56 +0800 | [diff] [blame] | 1801 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
| 1802 | struct list_head *head = &dcc->entry_list; |
Chao Yu | 2d7b822 | 2014-03-29 11:33:17 +0800 | [diff] [blame] | 1803 | struct discard_entry *entry, *this; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1804 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
Changman Lee | 29e59c1 | 2013-11-11 09:24:37 +0900 | [diff] [blame] | 1805 | unsigned long *prefree_map = dirty_i->dirty_segmap[PRE]; |
Changman Lee | 29e59c1 | 2013-11-11 09:24:37 +0900 | [diff] [blame] | 1806 | unsigned int start = 0, end = -1; |
Jaegeuk Kim | 36abef4 | 2016-06-03 19:29:38 -0700 | [diff] [blame] | 1807 | unsigned int secno, start_segno; |
Chao Yu | c473f1a | 2017-04-27 20:40:39 +0800 | [diff] [blame] | 1808 | bool force = (cpc->reason & CP_DISCARD); |
Yunlong Song | ad6672b | 2018-07-19 20:58:15 +0800 | [diff] [blame] | 1809 | bool need_align = test_opt(sbi, LFS) && sbi->segs_per_sec > 1; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1810 | |
| 1811 | mutex_lock(&dirty_i->seglist_lock); |
Changman Lee | 29e59c1 | 2013-11-11 09:24:37 +0900 | [diff] [blame] | 1812 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1813 | while (1) { |
Changman Lee | 29e59c1 | 2013-11-11 09:24:37 +0900 | [diff] [blame] | 1814 | int i; |
Yunlong Song | ad6672b | 2018-07-19 20:58:15 +0800 | [diff] [blame] | 1815 | |
| 1816 | if (need_align && end != -1) |
| 1817 | end--; |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 1818 | start = find_next_bit(prefree_map, MAIN_SEGS(sbi), end + 1); |
| 1819 | if (start >= MAIN_SEGS(sbi)) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1820 | break; |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 1821 | end = find_next_zero_bit(prefree_map, MAIN_SEGS(sbi), |
| 1822 | start + 1); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1823 | |
Yunlong Song | ad6672b | 2018-07-19 20:58:15 +0800 | [diff] [blame] | 1824 | if (need_align) { |
| 1825 | start = rounddown(start, sbi->segs_per_sec); |
| 1826 | end = roundup(end, sbi->segs_per_sec); |
| 1827 | } |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1828 | |
Yunlong Song | ad6672b | 2018-07-19 20:58:15 +0800 | [diff] [blame] | 1829 | for (i = start; i < end; i++) { |
| 1830 | if (test_and_clear_bit(i, prefree_map)) |
| 1831 | dirty_i->nr_dirty[PRE]--; |
| 1832 | } |
Changman Lee | 29e59c1 | 2013-11-11 09:24:37 +0900 | [diff] [blame] | 1833 | |
Yunlei He | 650d3c4 | 2016-12-22 11:46:24 +0800 | [diff] [blame] | 1834 | if (!test_opt(sbi, DISCARD)) |
Changman Lee | 29e59c1 | 2013-11-11 09:24:37 +0900 | [diff] [blame] | 1835 | continue; |
| 1836 | |
Yunlei He | 650d3c4 | 2016-12-22 11:46:24 +0800 | [diff] [blame] | 1837 | if (force && start >= cpc->trim_start && |
| 1838 | (end - 1) <= cpc->trim_end) |
| 1839 | continue; |
| 1840 | |
Jaegeuk Kim | 36abef4 | 2016-06-03 19:29:38 -0700 | [diff] [blame] | 1841 | if (!test_opt(sbi, LFS) || sbi->segs_per_sec == 1) { |
| 1842 | f2fs_issue_discard(sbi, START_BLOCK(sbi, start), |
Jaegeuk Kim | 3720887 | 2013-11-12 16:55:17 +0900 | [diff] [blame] | 1843 | (end - start) << sbi->log_blocks_per_seg); |
Jaegeuk Kim | 36abef4 | 2016-06-03 19:29:38 -0700 | [diff] [blame] | 1844 | continue; |
| 1845 | } |
| 1846 | next: |
Jaegeuk Kim | 4ddb1a4 | 2017-04-07 15:08:17 -0700 | [diff] [blame] | 1847 | secno = GET_SEC_FROM_SEG(sbi, start); |
| 1848 | start_segno = GET_SEG_FROM_SEC(sbi, secno); |
Jaegeuk Kim | 36abef4 | 2016-06-03 19:29:38 -0700 | [diff] [blame] | 1849 | if (!IS_CURSEC(sbi, secno) && |
Jaegeuk Kim | 302bd34 | 2017-04-07 14:33:22 -0700 | [diff] [blame] | 1850 | !get_valid_blocks(sbi, start, true)) |
Jaegeuk Kim | 36abef4 | 2016-06-03 19:29:38 -0700 | [diff] [blame] | 1851 | f2fs_issue_discard(sbi, START_BLOCK(sbi, start_segno), |
| 1852 | sbi->segs_per_sec << sbi->log_blocks_per_seg); |
| 1853 | |
| 1854 | start = start_segno + sbi->segs_per_sec; |
| 1855 | if (start < end) |
| 1856 | goto next; |
Jaegeuk Kim | 8b107f5 | 2017-02-27 11:57:11 -0800 | [diff] [blame] | 1857 | else |
| 1858 | end = start - 1; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1859 | } |
| 1860 | mutex_unlock(&dirty_i->seglist_lock); |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 1861 | |
| 1862 | /* send small discards */ |
Chao Yu | 2d7b822 | 2014-03-29 11:33:17 +0800 | [diff] [blame] | 1863 | list_for_each_entry_safe(entry, this, head, list) { |
Chao Yu | a7eeb823 | 2017-03-28 18:18:50 +0800 | [diff] [blame] | 1864 | unsigned int cur_pos = 0, next_pos, len, total_len = 0; |
| 1865 | bool is_valid = test_bit_le(0, entry->discard_map); |
| 1866 | |
| 1867 | find_next: |
| 1868 | if (is_valid) { |
| 1869 | next_pos = find_next_zero_bit_le(entry->discard_map, |
| 1870 | sbi->blocks_per_seg, cur_pos); |
| 1871 | len = next_pos - cur_pos; |
| 1872 | |
Sheng Yong | ccd31cb | 2018-02-06 12:31:17 +0800 | [diff] [blame] | 1873 | if (f2fs_sb_has_blkzoned(sbi->sb) || |
Damien Le Moal | acfd2810 | 2017-05-26 17:04:40 +0900 | [diff] [blame] | 1874 | (force && len < cpc->trim_minlen)) |
Chao Yu | a7eeb823 | 2017-03-28 18:18:50 +0800 | [diff] [blame] | 1875 | goto skip; |
| 1876 | |
| 1877 | f2fs_issue_discard(sbi, entry->start_blkaddr + cur_pos, |
| 1878 | len); |
Chao Yu | a7eeb823 | 2017-03-28 18:18:50 +0800 | [diff] [blame] | 1879 | total_len += len; |
| 1880 | } else { |
| 1881 | next_pos = find_next_bit_le(entry->discard_map, |
| 1882 | sbi->blocks_per_seg, cur_pos); |
| 1883 | } |
Jaegeuk Kim | 836b5a6 | 2015-04-30 22:50:06 -0700 | [diff] [blame] | 1884 | skip: |
Chao Yu | a7eeb823 | 2017-03-28 18:18:50 +0800 | [diff] [blame] | 1885 | cur_pos = next_pos; |
| 1886 | is_valid = !is_valid; |
| 1887 | |
| 1888 | if (cur_pos < sbi->blocks_per_seg) |
| 1889 | goto find_next; |
| 1890 | |
Chao Yu | af8ff65 | 2018-04-25 17:38:29 +0800 | [diff] [blame] | 1891 | release_discard_addr(entry); |
Chao Yu | 969d1b1 | 2017-08-07 23:09:56 +0800 | [diff] [blame] | 1892 | dcc->nr_discards -= total_len; |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 1893 | } |
Chao Yu | 34e159d | 2017-04-25 00:21:34 +0800 | [diff] [blame] | 1894 | |
Jaegeuk Kim | 01983c7 | 2017-08-22 21:15:43 -0700 | [diff] [blame] | 1895 | wake_up_discard_thread(sbi, false); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1896 | } |
| 1897 | |
Jaegeuk Kim | 8ed5974 | 2017-01-29 14:27:02 +0900 | [diff] [blame] | 1898 | static int create_discard_cmd_control(struct f2fs_sb_info *sbi) |
Jaegeuk Kim | 0b54fb8 | 2017-01-11 14:40:24 -0800 | [diff] [blame] | 1899 | { |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 1900 | dev_t dev = sbi->sb->s_bdev->bd_dev; |
Jaegeuk Kim | 0b54fb8 | 2017-01-11 14:40:24 -0800 | [diff] [blame] | 1901 | struct discard_cmd_control *dcc; |
Chao Yu | ba48a33 | 2017-04-15 14:09:37 +0800 | [diff] [blame] | 1902 | int err = 0, i; |
Jaegeuk Kim | 0b54fb8 | 2017-01-11 14:40:24 -0800 | [diff] [blame] | 1903 | |
| 1904 | if (SM_I(sbi)->dcc_info) { |
| 1905 | dcc = SM_I(sbi)->dcc_info; |
| 1906 | goto init_thread; |
| 1907 | } |
| 1908 | |
Chao Yu | acbf054 | 2017-11-30 19:28:17 +0800 | [diff] [blame] | 1909 | dcc = f2fs_kzalloc(sbi, sizeof(struct discard_cmd_control), GFP_KERNEL); |
Jaegeuk Kim | 0b54fb8 | 2017-01-11 14:40:24 -0800 | [diff] [blame] | 1910 | if (!dcc) |
| 1911 | return -ENOMEM; |
| 1912 | |
Chao Yu | 969d1b1 | 2017-08-07 23:09:56 +0800 | [diff] [blame] | 1913 | dcc->discard_granularity = DEFAULT_DISCARD_GRANULARITY; |
Chao Yu | 46f84c2 | 2017-04-15 14:09:36 +0800 | [diff] [blame] | 1914 | INIT_LIST_HEAD(&dcc->entry_list); |
Chao Yu | 78997b5 | 2017-10-04 09:08:34 +0800 | [diff] [blame] | 1915 | for (i = 0; i < MAX_PLIST_NUM; i++) |
Chao Yu | ba48a33 | 2017-04-15 14:09:37 +0800 | [diff] [blame] | 1916 | INIT_LIST_HEAD(&dcc->pend_list[i]); |
Chao Yu | 46f84c2 | 2017-04-15 14:09:36 +0800 | [diff] [blame] | 1917 | INIT_LIST_HEAD(&dcc->wait_list); |
Chao Yu | 8412663 | 2017-10-04 09:08:32 +0800 | [diff] [blame] | 1918 | INIT_LIST_HEAD(&dcc->fstrim_list); |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 1919 | mutex_init(&dcc->cmd_lock); |
Chao Yu | 8b8dd65 | 2017-03-25 17:19:58 +0800 | [diff] [blame] | 1920 | atomic_set(&dcc->issued_discard, 0); |
| 1921 | atomic_set(&dcc->issing_discard, 0); |
Chao Yu | 5f32366 | 2017-03-25 17:19:59 +0800 | [diff] [blame] | 1922 | atomic_set(&dcc->discard_cmd_cnt, 0); |
Jaegeuk Kim | 0b54fb8 | 2017-01-11 14:40:24 -0800 | [diff] [blame] | 1923 | dcc->nr_discards = 0; |
Chao Yu | d618eba | 2017-04-25 00:21:35 +0800 | [diff] [blame] | 1924 | dcc->max_discards = MAIN_SEGS(sbi) << sbi->log_blocks_per_seg; |
Chao Yu | d84d1cb | 2017-04-18 19:27:39 +0800 | [diff] [blame] | 1925 | dcc->undiscard_blks = 0; |
Chao Yu | 20ee438 | 2018-07-08 22:11:01 +0800 | [diff] [blame] | 1926 | dcc->next_pos = 0; |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1927 | dcc->root = RB_ROOT; |
Chao Yu | 67fce70 | 2018-06-22 16:06:59 +0800 | [diff] [blame] | 1928 | dcc->rbtree_check = false; |
Jaegeuk Kim | 0b54fb8 | 2017-01-11 14:40:24 -0800 | [diff] [blame] | 1929 | |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 1930 | init_waitqueue_head(&dcc->discard_wait_queue); |
Jaegeuk Kim | 0b54fb8 | 2017-01-11 14:40:24 -0800 | [diff] [blame] | 1931 | SM_I(sbi)->dcc_info = dcc; |
| 1932 | init_thread: |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 1933 | dcc->f2fs_issue_discard = kthread_run(issue_discard_thread, sbi, |
| 1934 | "f2fs_discard-%u:%u", MAJOR(dev), MINOR(dev)); |
| 1935 | if (IS_ERR(dcc->f2fs_issue_discard)) { |
| 1936 | err = PTR_ERR(dcc->f2fs_issue_discard); |
| 1937 | kfree(dcc); |
| 1938 | SM_I(sbi)->dcc_info = NULL; |
| 1939 | return err; |
| 1940 | } |
| 1941 | |
Jaegeuk Kim | 0b54fb8 | 2017-01-11 14:40:24 -0800 | [diff] [blame] | 1942 | return err; |
| 1943 | } |
| 1944 | |
Chao Yu | f099405 | 2017-03-27 18:14:04 +0800 | [diff] [blame] | 1945 | static void destroy_discard_cmd_control(struct f2fs_sb_info *sbi) |
Jaegeuk Kim | 0b54fb8 | 2017-01-11 14:40:24 -0800 | [diff] [blame] | 1946 | { |
| 1947 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
| 1948 | |
Chao Yu | f099405 | 2017-03-27 18:14:04 +0800 | [diff] [blame] | 1949 | if (!dcc) |
| 1950 | return; |
| 1951 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1952 | f2fs_stop_discard_thread(sbi); |
Chao Yu | f099405 | 2017-03-27 18:14:04 +0800 | [diff] [blame] | 1953 | |
| 1954 | kfree(dcc); |
| 1955 | SM_I(sbi)->dcc_info = NULL; |
Jaegeuk Kim | 0b54fb8 | 2017-01-11 14:40:24 -0800 | [diff] [blame] | 1956 | } |
| 1957 | |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 1958 | 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] | 1959 | { |
| 1960 | struct sit_info *sit_i = SIT_I(sbi); |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 1961 | |
| 1962 | if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap)) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1963 | sit_i->dirty_sentries++; |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 1964 | return false; |
| 1965 | } |
| 1966 | |
| 1967 | return true; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1968 | } |
| 1969 | |
| 1970 | static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type, |
| 1971 | unsigned int segno, int modified) |
| 1972 | { |
| 1973 | struct seg_entry *se = get_seg_entry(sbi, segno); |
| 1974 | se->type = type; |
| 1975 | if (modified) |
| 1976 | __mark_sit_entry_dirty(sbi, segno); |
| 1977 | } |
| 1978 | |
| 1979 | static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del) |
| 1980 | { |
| 1981 | struct seg_entry *se; |
| 1982 | unsigned int segno, offset; |
| 1983 | long int new_vblocks; |
Yunlong Song | 6415fed | 2017-08-02 21:20:13 +0800 | [diff] [blame] | 1984 | bool exist; |
| 1985 | #ifdef CONFIG_F2FS_CHECK_FS |
| 1986 | bool mir_exist; |
| 1987 | #endif |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1988 | |
| 1989 | segno = GET_SEGNO(sbi, blkaddr); |
| 1990 | |
| 1991 | se = get_seg_entry(sbi, segno); |
| 1992 | new_vblocks = se->valid_blocks + del; |
Jaegeuk Kim | 491c085 | 2014-02-04 13:01:10 +0900 | [diff] [blame] | 1993 | offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1994 | |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 1995 | f2fs_bug_on(sbi, (new_vblocks >> (sizeof(unsigned short) << 3) || |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1996 | (new_vblocks > sbi->blocks_per_seg))); |
| 1997 | |
| 1998 | se->valid_blocks = new_vblocks; |
Chao Yu | a1f72ac2 | 2018-06-04 23:20:17 +0800 | [diff] [blame] | 1999 | se->mtime = get_mtime(sbi, false); |
| 2000 | if (se->mtime > SIT_I(sbi)->max_mtime) |
| 2001 | SIT_I(sbi)->max_mtime = se->mtime; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2002 | |
| 2003 | /* Update valid block bitmap */ |
| 2004 | if (del > 0) { |
Yunlong Song | 6415fed | 2017-08-02 21:20:13 +0800 | [diff] [blame] | 2005 | exist = f2fs_test_and_set_bit(offset, se->cur_valid_map); |
Chao Yu | 355e789 | 2017-01-07 18:51:01 +0800 | [diff] [blame] | 2006 | #ifdef CONFIG_F2FS_CHECK_FS |
Yunlong Song | 6415fed | 2017-08-02 21:20:13 +0800 | [diff] [blame] | 2007 | mir_exist = f2fs_test_and_set_bit(offset, |
| 2008 | se->cur_valid_map_mir); |
| 2009 | if (unlikely(exist != mir_exist)) { |
| 2010 | f2fs_msg(sbi->sb, KERN_ERR, "Inconsistent error " |
| 2011 | "when setting bitmap, blk:%u, old bit:%d", |
| 2012 | blkaddr, exist); |
Jaegeuk Kim | 0579676 | 2014-09-02 16:05:00 -0700 | [diff] [blame] | 2013 | f2fs_bug_on(sbi, 1); |
Chao Yu | 355e789 | 2017-01-07 18:51:01 +0800 | [diff] [blame] | 2014 | } |
Yunlong Song | 6415fed | 2017-08-02 21:20:13 +0800 | [diff] [blame] | 2015 | #endif |
| 2016 | if (unlikely(exist)) { |
| 2017 | f2fs_msg(sbi->sb, KERN_ERR, |
| 2018 | "Bitmap was wrongly set, blk:%u", blkaddr); |
| 2019 | f2fs_bug_on(sbi, 1); |
Yunlong Song | 35ee82c | 2017-08-02 22:16:54 +0800 | [diff] [blame] | 2020 | se->valid_blocks--; |
| 2021 | del = 0; |
Yunlong Song | 6415fed | 2017-08-02 21:20:13 +0800 | [diff] [blame] | 2022 | } |
| 2023 | |
Jaegeuk Kim | 3e02574 | 2016-08-02 10:56:40 -0700 | [diff] [blame] | 2024 | if (f2fs_discard_en(sbi) && |
| 2025 | !f2fs_test_and_set_bit(offset, se->discard_map)) |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 2026 | sbi->discard_blks--; |
Jaegeuk Kim | 720037f | 2017-03-06 11:59:56 -0800 | [diff] [blame] | 2027 | |
| 2028 | /* don't overwrite by SSR to keep node chain */ |
Yunlei He | 5d7881c | 2018-03-07 16:22:50 +0800 | [diff] [blame] | 2029 | if (IS_NODESEG(se->type)) { |
Jaegeuk Kim | 720037f | 2017-03-06 11:59:56 -0800 | [diff] [blame] | 2030 | if (!f2fs_test_and_set_bit(offset, se->ckpt_valid_map)) |
| 2031 | se->ckpt_valid_blocks++; |
| 2032 | } |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2033 | } else { |
Yunlong Song | 6415fed | 2017-08-02 21:20:13 +0800 | [diff] [blame] | 2034 | exist = f2fs_test_and_clear_bit(offset, se->cur_valid_map); |
Chao Yu | 355e789 | 2017-01-07 18:51:01 +0800 | [diff] [blame] | 2035 | #ifdef CONFIG_F2FS_CHECK_FS |
Yunlong Song | 6415fed | 2017-08-02 21:20:13 +0800 | [diff] [blame] | 2036 | mir_exist = f2fs_test_and_clear_bit(offset, |
| 2037 | se->cur_valid_map_mir); |
| 2038 | if (unlikely(exist != mir_exist)) { |
| 2039 | f2fs_msg(sbi->sb, KERN_ERR, "Inconsistent error " |
| 2040 | "when clearing bitmap, blk:%u, old bit:%d", |
| 2041 | blkaddr, exist); |
Jaegeuk Kim | 0579676 | 2014-09-02 16:05:00 -0700 | [diff] [blame] | 2042 | f2fs_bug_on(sbi, 1); |
Chao Yu | 355e789 | 2017-01-07 18:51:01 +0800 | [diff] [blame] | 2043 | } |
Yunlong Song | 6415fed | 2017-08-02 21:20:13 +0800 | [diff] [blame] | 2044 | #endif |
| 2045 | if (unlikely(!exist)) { |
| 2046 | f2fs_msg(sbi->sb, KERN_ERR, |
| 2047 | "Bitmap was wrongly cleared, blk:%u", blkaddr); |
| 2048 | f2fs_bug_on(sbi, 1); |
Yunlong Song | 35ee82c | 2017-08-02 22:16:54 +0800 | [diff] [blame] | 2049 | se->valid_blocks++; |
| 2050 | del = 0; |
Yunlong Song | 6415fed | 2017-08-02 21:20:13 +0800 | [diff] [blame] | 2051 | } |
| 2052 | |
Jaegeuk Kim | 3e02574 | 2016-08-02 10:56:40 -0700 | [diff] [blame] | 2053 | if (f2fs_discard_en(sbi) && |
| 2054 | f2fs_test_and_clear_bit(offset, se->discard_map)) |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 2055 | sbi->discard_blks++; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2056 | } |
| 2057 | if (!f2fs_test_bit(offset, se->ckpt_valid_map)) |
| 2058 | se->ckpt_valid_blocks += del; |
| 2059 | |
| 2060 | __mark_sit_entry_dirty(sbi, segno); |
| 2061 | |
| 2062 | /* update total number of valid blocks to be written in ckpt area */ |
| 2063 | SIT_I(sbi)->written_valid_blocks += del; |
| 2064 | |
| 2065 | if (sbi->segs_per_sec > 1) |
| 2066 | get_sec_entry(sbi, segno)->valid_blocks += del; |
| 2067 | } |
| 2068 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2069 | void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2070 | { |
| 2071 | unsigned int segno = GET_SEGNO(sbi, addr); |
| 2072 | struct sit_info *sit_i = SIT_I(sbi); |
| 2073 | |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 2074 | f2fs_bug_on(sbi, addr == NULL_ADDR); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2075 | if (addr == NEW_ADDR) |
| 2076 | return; |
| 2077 | |
| 2078 | /* add it into sit main buffer */ |
Chao Yu | 3d26fa6 | 2017-10-30 17:49:53 +0800 | [diff] [blame] | 2079 | down_write(&sit_i->sentry_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2080 | |
| 2081 | update_sit_entry(sbi, addr, -1); |
| 2082 | |
| 2083 | /* add it into dirty seglist */ |
| 2084 | locate_dirty_segment(sbi, segno); |
| 2085 | |
Chao Yu | 3d26fa6 | 2017-10-30 17:49:53 +0800 | [diff] [blame] | 2086 | up_write(&sit_i->sentry_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2087 | } |
| 2088 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2089 | 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] | 2090 | { |
| 2091 | struct sit_info *sit_i = SIT_I(sbi); |
| 2092 | unsigned int segno, offset; |
| 2093 | struct seg_entry *se; |
| 2094 | bool is_cp = false; |
| 2095 | |
Chao Yu | e1da787 | 2018-06-05 17:44:11 +0800 | [diff] [blame] | 2096 | if (!is_valid_data_blkaddr(sbi, blkaddr)) |
Jaegeuk Kim | 6e2c64a | 2015-10-07 12:28:41 -0700 | [diff] [blame] | 2097 | return true; |
| 2098 | |
Chao Yu | 3d26fa6 | 2017-10-30 17:49:53 +0800 | [diff] [blame] | 2099 | down_read(&sit_i->sentry_lock); |
Jaegeuk Kim | 6e2c64a | 2015-10-07 12:28:41 -0700 | [diff] [blame] | 2100 | |
| 2101 | segno = GET_SEGNO(sbi, blkaddr); |
| 2102 | se = get_seg_entry(sbi, segno); |
| 2103 | offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr); |
| 2104 | |
| 2105 | if (f2fs_test_bit(offset, se->ckpt_valid_map)) |
| 2106 | is_cp = true; |
| 2107 | |
Chao Yu | 3d26fa6 | 2017-10-30 17:49:53 +0800 | [diff] [blame] | 2108 | up_read(&sit_i->sentry_lock); |
Jaegeuk Kim | 6e2c64a | 2015-10-07 12:28:41 -0700 | [diff] [blame] | 2109 | |
| 2110 | return is_cp; |
| 2111 | } |
| 2112 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 2113 | /* |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2114 | * This function should be resided under the curseg_mutex lock |
| 2115 | */ |
| 2116 | static void __add_sum_entry(struct f2fs_sb_info *sbi, int type, |
Haicheng Li | e79efe3 | 2013-06-13 16:59:27 +0800 | [diff] [blame] | 2117 | struct f2fs_summary *sum) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2118 | { |
| 2119 | struct curseg_info *curseg = CURSEG_I(sbi, type); |
| 2120 | void *addr = curseg->sum_blk; |
Haicheng Li | e79efe3 | 2013-06-13 16:59:27 +0800 | [diff] [blame] | 2121 | addr += curseg->next_blkoff * sizeof(struct f2fs_summary); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2122 | memcpy(addr, sum, sizeof(struct f2fs_summary)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2123 | } |
| 2124 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 2125 | /* |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2126 | * Calculate the number of current summary pages for writing |
| 2127 | */ |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2128 | 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] | 2129 | { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2130 | int valid_sum_count = 0; |
Fan Li | 9a47938 | 2013-10-29 16:21:47 +0800 | [diff] [blame] | 2131 | int i, sum_in_page; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2132 | |
| 2133 | for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) { |
| 2134 | if (sbi->ckpt->alloc_type[i] == SSR) |
| 2135 | valid_sum_count += sbi->blocks_per_seg; |
Chao Yu | 3fa06d7 | 2014-12-09 14:21:46 +0800 | [diff] [blame] | 2136 | else { |
| 2137 | if (for_ra) |
| 2138 | valid_sum_count += le16_to_cpu( |
| 2139 | F2FS_CKPT(sbi)->cur_data_blkoff[i]); |
| 2140 | else |
| 2141 | valid_sum_count += curseg_blkoff(sbi, i); |
| 2142 | } |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2143 | } |
| 2144 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2145 | sum_in_page = (PAGE_SIZE - 2 * SUM_JOURNAL_SIZE - |
Fan Li | 9a47938 | 2013-10-29 16:21:47 +0800 | [diff] [blame] | 2146 | SUM_FOOTER_SIZE) / SUMMARY_SIZE; |
| 2147 | if (valid_sum_count <= sum_in_page) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2148 | return 1; |
Fan Li | 9a47938 | 2013-10-29 16:21:47 +0800 | [diff] [blame] | 2149 | else if ((valid_sum_count - sum_in_page) <= |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2150 | (PAGE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2151 | return 2; |
| 2152 | return 3; |
| 2153 | } |
| 2154 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 2155 | /* |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2156 | * Caller should put this summary page |
| 2157 | */ |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2158 | 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] | 2159 | { |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 2160 | return f2fs_get_meta_page_nofail(sbi, GET_SUM_BLOCK(sbi, segno)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2161 | } |
| 2162 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2163 | void f2fs_update_meta_page(struct f2fs_sb_info *sbi, |
| 2164 | void *src, block_t blk_addr) |
Chao Yu | 381722d | 2015-05-19 17:40:04 +0800 | [diff] [blame] | 2165 | { |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2166 | struct page *page = f2fs_grab_meta_page(sbi, blk_addr); |
Chao Yu | 381722d | 2015-05-19 17:40:04 +0800 | [diff] [blame] | 2167 | |
Chao Yu | 0537b81 | 2017-11-02 20:41:02 +0800 | [diff] [blame] | 2168 | memcpy(page_address(page), src, PAGE_SIZE); |
Chao Yu | 381722d | 2015-05-19 17:40:04 +0800 | [diff] [blame] | 2169 | set_page_dirty(page); |
| 2170 | f2fs_put_page(page, 1); |
| 2171 | } |
| 2172 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2173 | static void write_sum_page(struct f2fs_sb_info *sbi, |
| 2174 | struct f2fs_summary_block *sum_blk, block_t blk_addr) |
| 2175 | { |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2176 | f2fs_update_meta_page(sbi, (void *)sum_blk, blk_addr); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2177 | } |
| 2178 | |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 2179 | static void write_current_sum_page(struct f2fs_sb_info *sbi, |
| 2180 | int type, block_t blk_addr) |
| 2181 | { |
| 2182 | struct curseg_info *curseg = CURSEG_I(sbi, type); |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2183 | struct page *page = f2fs_grab_meta_page(sbi, blk_addr); |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 2184 | struct f2fs_summary_block *src = curseg->sum_blk; |
| 2185 | struct f2fs_summary_block *dst; |
| 2186 | |
| 2187 | dst = (struct f2fs_summary_block *)page_address(page); |
Chao Yu | 81114ba | 2018-04-09 20:25:06 +0800 | [diff] [blame] | 2188 | memset(dst, 0, PAGE_SIZE); |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 2189 | |
| 2190 | mutex_lock(&curseg->curseg_mutex); |
| 2191 | |
| 2192 | down_read(&curseg->journal_rwsem); |
| 2193 | memcpy(&dst->journal, curseg->journal, SUM_JOURNAL_SIZE); |
| 2194 | up_read(&curseg->journal_rwsem); |
| 2195 | |
| 2196 | memcpy(dst->entries, src->entries, SUM_ENTRY_SIZE); |
| 2197 | memcpy(&dst->footer, &src->footer, SUM_FOOTER_SIZE); |
| 2198 | |
| 2199 | mutex_unlock(&curseg->curseg_mutex); |
| 2200 | |
| 2201 | set_page_dirty(page); |
| 2202 | f2fs_put_page(page, 1); |
| 2203 | } |
| 2204 | |
Jaegeuk Kim | a788189 | 2017-04-20 13:51:57 -0700 | [diff] [blame] | 2205 | static int is_next_segment_free(struct f2fs_sb_info *sbi, int type) |
| 2206 | { |
| 2207 | struct curseg_info *curseg = CURSEG_I(sbi, type); |
| 2208 | unsigned int segno = curseg->segno + 1; |
| 2209 | struct free_segmap_info *free_i = FREE_I(sbi); |
| 2210 | |
| 2211 | if (segno < MAIN_SEGS(sbi) && segno % sbi->segs_per_sec) |
| 2212 | return !test_bit(segno, free_i->free_segmap); |
| 2213 | return 0; |
| 2214 | } |
| 2215 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 2216 | /* |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2217 | * Find a new segment from the free segments bitmap to right order |
| 2218 | * This function should be returned with success, otherwise BUG |
| 2219 | */ |
| 2220 | static void get_new_segment(struct f2fs_sb_info *sbi, |
| 2221 | unsigned int *newseg, bool new_sec, int dir) |
| 2222 | { |
| 2223 | struct free_segmap_info *free_i = FREE_I(sbi); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2224 | unsigned int segno, secno, zoneno; |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 2225 | unsigned int total_zones = MAIN_SECS(sbi) / sbi->secs_per_zone; |
Jaegeuk Kim | 4ddb1a4 | 2017-04-07 15:08:17 -0700 | [diff] [blame] | 2226 | unsigned int hint = GET_SEC_FROM_SEG(sbi, *newseg); |
| 2227 | unsigned int old_zoneno = GET_ZONE_FROM_SEG(sbi, *newseg); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2228 | unsigned int left_start = hint; |
| 2229 | bool init = true; |
| 2230 | int go_left = 0; |
| 2231 | int i; |
| 2232 | |
Chao Yu | 1a118cc | 2015-02-11 18:20:38 +0800 | [diff] [blame] | 2233 | spin_lock(&free_i->segmap_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2234 | |
| 2235 | if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) { |
| 2236 | segno = find_next_zero_bit(free_i->free_segmap, |
Jaegeuk Kim | 4ddb1a4 | 2017-04-07 15:08:17 -0700 | [diff] [blame] | 2237 | GET_SEG_FROM_SEC(sbi, hint + 1), *newseg + 1); |
| 2238 | if (segno < GET_SEG_FROM_SEC(sbi, hint + 1)) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2239 | goto got_it; |
| 2240 | } |
| 2241 | find_other_zone: |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 2242 | secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint); |
| 2243 | if (secno >= MAIN_SECS(sbi)) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2244 | if (dir == ALLOC_RIGHT) { |
| 2245 | secno = find_next_zero_bit(free_i->free_secmap, |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 2246 | MAIN_SECS(sbi), 0); |
| 2247 | f2fs_bug_on(sbi, secno >= MAIN_SECS(sbi)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2248 | } else { |
| 2249 | go_left = 1; |
| 2250 | left_start = hint - 1; |
| 2251 | } |
| 2252 | } |
| 2253 | if (go_left == 0) |
| 2254 | goto skip_left; |
| 2255 | |
| 2256 | while (test_bit(left_start, free_i->free_secmap)) { |
| 2257 | if (left_start > 0) { |
| 2258 | left_start--; |
| 2259 | continue; |
| 2260 | } |
| 2261 | left_start = find_next_zero_bit(free_i->free_secmap, |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 2262 | MAIN_SECS(sbi), 0); |
| 2263 | f2fs_bug_on(sbi, left_start >= MAIN_SECS(sbi)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2264 | break; |
| 2265 | } |
| 2266 | secno = left_start; |
| 2267 | skip_left: |
Jaegeuk Kim | 4ddb1a4 | 2017-04-07 15:08:17 -0700 | [diff] [blame] | 2268 | segno = GET_SEG_FROM_SEC(sbi, secno); |
| 2269 | zoneno = GET_ZONE_FROM_SEC(sbi, secno); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2270 | |
| 2271 | /* give up on finding another zone */ |
| 2272 | if (!init) |
| 2273 | goto got_it; |
| 2274 | if (sbi->secs_per_zone == 1) |
| 2275 | goto got_it; |
| 2276 | if (zoneno == old_zoneno) |
| 2277 | goto got_it; |
| 2278 | if (dir == ALLOC_LEFT) { |
| 2279 | if (!go_left && zoneno + 1 >= total_zones) |
| 2280 | goto got_it; |
| 2281 | if (go_left && zoneno == 0) |
| 2282 | goto got_it; |
| 2283 | } |
| 2284 | for (i = 0; i < NR_CURSEG_TYPE; i++) |
| 2285 | if (CURSEG_I(sbi, i)->zone == zoneno) |
| 2286 | break; |
| 2287 | |
| 2288 | if (i < NR_CURSEG_TYPE) { |
| 2289 | /* zone is in user, try another */ |
| 2290 | if (go_left) |
| 2291 | hint = zoneno * sbi->secs_per_zone - 1; |
| 2292 | else if (zoneno + 1 >= total_zones) |
| 2293 | hint = 0; |
| 2294 | else |
| 2295 | hint = (zoneno + 1) * sbi->secs_per_zone; |
| 2296 | init = false; |
| 2297 | goto find_other_zone; |
| 2298 | } |
| 2299 | got_it: |
| 2300 | /* set it as dirty segment in free segmap */ |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 2301 | f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2302 | __set_inuse(sbi, segno); |
| 2303 | *newseg = segno; |
Chao Yu | 1a118cc | 2015-02-11 18:20:38 +0800 | [diff] [blame] | 2304 | spin_unlock(&free_i->segmap_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2305 | } |
| 2306 | |
| 2307 | static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified) |
| 2308 | { |
| 2309 | struct curseg_info *curseg = CURSEG_I(sbi, type); |
| 2310 | struct summary_footer *sum_footer; |
| 2311 | |
| 2312 | curseg->segno = curseg->next_segno; |
Jaegeuk Kim | 4ddb1a4 | 2017-04-07 15:08:17 -0700 | [diff] [blame] | 2313 | curseg->zone = GET_ZONE_FROM_SEG(sbi, curseg->segno); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2314 | curseg->next_blkoff = 0; |
| 2315 | curseg->next_segno = NULL_SEGNO; |
| 2316 | |
| 2317 | sum_footer = &(curseg->sum_blk->footer); |
| 2318 | memset(sum_footer, 0, sizeof(struct summary_footer)); |
| 2319 | if (IS_DATASEG(type)) |
| 2320 | SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA); |
| 2321 | if (IS_NODESEG(type)) |
| 2322 | SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE); |
| 2323 | __set_sit_entry_type(sbi, type, curseg->segno, modified); |
| 2324 | } |
| 2325 | |
Jaegeuk Kim | 7a20b8a | 2017-03-24 20:41:45 -0400 | [diff] [blame] | 2326 | static unsigned int __get_next_segno(struct f2fs_sb_info *sbi, int type) |
| 2327 | { |
Jaegeuk Kim | a788189 | 2017-04-20 13:51:57 -0700 | [diff] [blame] | 2328 | /* if segs_per_sec is large than 1, we need to keep original policy. */ |
| 2329 | if (sbi->segs_per_sec != 1) |
| 2330 | return CURSEG_I(sbi, type)->segno; |
| 2331 | |
Yunlong Song | b94929d | 2018-01-29 11:37:45 +0800 | [diff] [blame] | 2332 | if (test_opt(sbi, NOHEAP) && |
| 2333 | (type == CURSEG_HOT_DATA || IS_NODESEG(type))) |
Jaegeuk Kim | 7a20b8a | 2017-03-24 20:41:45 -0400 | [diff] [blame] | 2334 | return 0; |
| 2335 | |
Jaegeuk Kim | e066b83 | 2017-04-13 15:17:00 -0700 | [diff] [blame] | 2336 | if (SIT_I(sbi)->last_victim[ALLOC_NEXT]) |
| 2337 | return SIT_I(sbi)->last_victim[ALLOC_NEXT]; |
Jaegeuk Kim | 0793962 | 2018-02-18 08:50:49 -0800 | [diff] [blame] | 2338 | |
| 2339 | /* find segments from 0 to reuse freed segments */ |
Chao Yu | 63189b7 | 2018-03-08 14:22:56 +0800 | [diff] [blame] | 2340 | if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_REUSE) |
Jaegeuk Kim | 0793962 | 2018-02-18 08:50:49 -0800 | [diff] [blame] | 2341 | return 0; |
| 2342 | |
Jaegeuk Kim | 7a20b8a | 2017-03-24 20:41:45 -0400 | [diff] [blame] | 2343 | return CURSEG_I(sbi, type)->segno; |
| 2344 | } |
| 2345 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 2346 | /* |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2347 | * Allocate a current working segment. |
| 2348 | * This function always allocates a free segment in LFS manner. |
| 2349 | */ |
| 2350 | static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec) |
| 2351 | { |
| 2352 | struct curseg_info *curseg = CURSEG_I(sbi, type); |
| 2353 | unsigned int segno = curseg->segno; |
| 2354 | int dir = ALLOC_LEFT; |
| 2355 | |
| 2356 | write_sum_page(sbi, curseg->sum_blk, |
Haicheng Li | 81fb5e8 | 2013-05-14 18:20:28 +0800 | [diff] [blame] | 2357 | GET_SUM_BLOCK(sbi, segno)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2358 | if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA) |
| 2359 | dir = ALLOC_RIGHT; |
| 2360 | |
| 2361 | if (test_opt(sbi, NOHEAP)) |
| 2362 | dir = ALLOC_RIGHT; |
| 2363 | |
Jaegeuk Kim | 7a20b8a | 2017-03-24 20:41:45 -0400 | [diff] [blame] | 2364 | segno = __get_next_segno(sbi, type); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2365 | get_new_segment(sbi, &segno, new_sec, dir); |
| 2366 | curseg->next_segno = segno; |
| 2367 | reset_curseg(sbi, type, 1); |
| 2368 | curseg->alloc_type = LFS; |
| 2369 | } |
| 2370 | |
| 2371 | static void __next_free_blkoff(struct f2fs_sb_info *sbi, |
| 2372 | struct curseg_info *seg, block_t start) |
| 2373 | { |
| 2374 | struct seg_entry *se = get_seg_entry(sbi, seg->segno); |
Changman Lee | e81c93c | 2013-11-15 13:21:16 +0900 | [diff] [blame] | 2375 | int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long); |
Jaegeuk Kim | 60a3b78 | 2015-02-10 16:44:29 -0800 | [diff] [blame] | 2376 | unsigned long *target_map = SIT_I(sbi)->tmp_map; |
Changman Lee | e81c93c | 2013-11-15 13:21:16 +0900 | [diff] [blame] | 2377 | unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map; |
| 2378 | unsigned long *cur_map = (unsigned long *)se->cur_valid_map; |
| 2379 | int i, pos; |
| 2380 | |
| 2381 | for (i = 0; i < entries; i++) |
| 2382 | target_map[i] = ckpt_map[i] | cur_map[i]; |
| 2383 | |
| 2384 | pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start); |
| 2385 | |
| 2386 | seg->next_blkoff = pos; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2387 | } |
| 2388 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 2389 | /* |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2390 | * If a segment is written by LFS manner, next block offset is just obtained |
| 2391 | * by increasing the current block offset. However, if a segment is written by |
| 2392 | * SSR manner, next block offset obtained by calling __next_free_blkoff |
| 2393 | */ |
| 2394 | static void __refresh_next_blkoff(struct f2fs_sb_info *sbi, |
| 2395 | struct curseg_info *seg) |
| 2396 | { |
| 2397 | if (seg->alloc_type == SSR) |
| 2398 | __next_free_blkoff(sbi, seg, seg->next_blkoff + 1); |
| 2399 | else |
| 2400 | seg->next_blkoff++; |
| 2401 | } |
| 2402 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 2403 | /* |
arter97 | e1c4204 | 2014-08-06 23:22:50 +0900 | [diff] [blame] | 2404 | * This function always allocates a used segment(from dirty seglist) by SSR |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2405 | * manner, so it should recover the existing segment information of valid blocks |
| 2406 | */ |
Chao Yu | 025d63a | 2017-08-30 18:04:48 +0800 | [diff] [blame] | 2407 | static void change_curseg(struct f2fs_sb_info *sbi, int type) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2408 | { |
| 2409 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
| 2410 | struct curseg_info *curseg = CURSEG_I(sbi, type); |
| 2411 | unsigned int new_segno = curseg->next_segno; |
| 2412 | struct f2fs_summary_block *sum_node; |
| 2413 | struct page *sum_page; |
| 2414 | |
| 2415 | write_sum_page(sbi, curseg->sum_blk, |
| 2416 | GET_SUM_BLOCK(sbi, curseg->segno)); |
| 2417 | __set_test_and_inuse(sbi, new_segno); |
| 2418 | |
| 2419 | mutex_lock(&dirty_i->seglist_lock); |
| 2420 | __remove_dirty_segment(sbi, new_segno, PRE); |
| 2421 | __remove_dirty_segment(sbi, new_segno, DIRTY); |
| 2422 | mutex_unlock(&dirty_i->seglist_lock); |
| 2423 | |
| 2424 | reset_curseg(sbi, type, 1); |
| 2425 | curseg->alloc_type = SSR; |
| 2426 | __next_free_blkoff(sbi, curseg, 0); |
| 2427 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2428 | sum_page = f2fs_get_sum_page(sbi, new_segno); |
Chao Yu | 025d63a | 2017-08-30 18:04:48 +0800 | [diff] [blame] | 2429 | sum_node = (struct f2fs_summary_block *)page_address(sum_page); |
| 2430 | memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE); |
| 2431 | f2fs_put_page(sum_page, 1); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2432 | } |
| 2433 | |
Jaegeuk Kim | 4372752 | 2013-02-04 15:11:17 +0900 | [diff] [blame] | 2434 | static int get_ssr_segment(struct f2fs_sb_info *sbi, int type) |
| 2435 | { |
| 2436 | struct curseg_info *curseg = CURSEG_I(sbi, type); |
| 2437 | const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops; |
Jaegeuk Kim | e066b83 | 2017-04-13 15:17:00 -0700 | [diff] [blame] | 2438 | unsigned segno = NULL_SEGNO; |
Chao Yu | d27c3d8 | 2017-02-24 18:46:00 +0800 | [diff] [blame] | 2439 | int i, cnt; |
| 2440 | bool reversed = false; |
Jaegeuk Kim | c192f7a | 2017-02-22 17:10:18 -0800 | [diff] [blame] | 2441 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2442 | /* f2fs_need_SSR() already forces to do this */ |
Jaegeuk Kim | e066b83 | 2017-04-13 15:17:00 -0700 | [diff] [blame] | 2443 | if (v_ops->get_victim(sbi, &segno, BG_GC, type, SSR)) { |
| 2444 | curseg->next_segno = segno; |
Jaegeuk Kim | c192f7a | 2017-02-22 17:10:18 -0800 | [diff] [blame] | 2445 | return 1; |
Jaegeuk Kim | e066b83 | 2017-04-13 15:17:00 -0700 | [diff] [blame] | 2446 | } |
Jaegeuk Kim | 4372752 | 2013-02-04 15:11:17 +0900 | [diff] [blame] | 2447 | |
Jaegeuk Kim | 70d625c | 2017-02-22 17:02:32 -0800 | [diff] [blame] | 2448 | /* For node segments, let's do SSR more intensively */ |
| 2449 | if (IS_NODESEG(type)) { |
Chao Yu | d27c3d8 | 2017-02-24 18:46:00 +0800 | [diff] [blame] | 2450 | if (type >= CURSEG_WARM_NODE) { |
| 2451 | reversed = true; |
| 2452 | i = CURSEG_COLD_NODE; |
| 2453 | } else { |
| 2454 | i = CURSEG_HOT_NODE; |
| 2455 | } |
| 2456 | cnt = NR_CURSEG_NODE_TYPE; |
Jaegeuk Kim | 70d625c | 2017-02-22 17:02:32 -0800 | [diff] [blame] | 2457 | } else { |
Chao Yu | d27c3d8 | 2017-02-24 18:46:00 +0800 | [diff] [blame] | 2458 | if (type >= CURSEG_WARM_DATA) { |
| 2459 | reversed = true; |
| 2460 | i = CURSEG_COLD_DATA; |
| 2461 | } else { |
| 2462 | i = CURSEG_HOT_DATA; |
| 2463 | } |
| 2464 | cnt = NR_CURSEG_DATA_TYPE; |
Jaegeuk Kim | 70d625c | 2017-02-22 17:02:32 -0800 | [diff] [blame] | 2465 | } |
Jaegeuk Kim | 4372752 | 2013-02-04 15:11:17 +0900 | [diff] [blame] | 2466 | |
Chao Yu | d27c3d8 | 2017-02-24 18:46:00 +0800 | [diff] [blame] | 2467 | for (; cnt-- > 0; reversed ? i-- : i++) { |
Jaegeuk Kim | c192f7a | 2017-02-22 17:10:18 -0800 | [diff] [blame] | 2468 | if (i == type) |
| 2469 | continue; |
Jaegeuk Kim | e066b83 | 2017-04-13 15:17:00 -0700 | [diff] [blame] | 2470 | if (v_ops->get_victim(sbi, &segno, BG_GC, i, SSR)) { |
| 2471 | curseg->next_segno = segno; |
Jaegeuk Kim | 4372752 | 2013-02-04 15:11:17 +0900 | [diff] [blame] | 2472 | return 1; |
Jaegeuk Kim | e066b83 | 2017-04-13 15:17:00 -0700 | [diff] [blame] | 2473 | } |
Jaegeuk Kim | c192f7a | 2017-02-22 17:10:18 -0800 | [diff] [blame] | 2474 | } |
Jaegeuk Kim | 4372752 | 2013-02-04 15:11:17 +0900 | [diff] [blame] | 2475 | return 0; |
| 2476 | } |
| 2477 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2478 | /* |
| 2479 | * flush out current segment and replace it with new segment |
| 2480 | * This function should be returned with success, otherwise BUG |
| 2481 | */ |
| 2482 | static void allocate_segment_by_default(struct f2fs_sb_info *sbi, |
| 2483 | int type, bool force) |
| 2484 | { |
Jaegeuk Kim | a788189 | 2017-04-20 13:51:57 -0700 | [diff] [blame] | 2485 | struct curseg_info *curseg = CURSEG_I(sbi, type); |
| 2486 | |
Gu Zheng | 7b40527 | 2013-08-19 09:41:15 +0800 | [diff] [blame] | 2487 | if (force) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2488 | new_curseg(sbi, type, true); |
Jaegeuk Kim | 5b6c6be | 2017-02-14 19:32:51 -0800 | [diff] [blame] | 2489 | else if (!is_set_ckpt_flags(sbi, CP_CRC_RECOVERY_FLAG) && |
| 2490 | type == CURSEG_WARM_NODE) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2491 | new_curseg(sbi, type, false); |
Jaegeuk Kim | a788189 | 2017-04-20 13:51:57 -0700 | [diff] [blame] | 2492 | else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type)) |
| 2493 | new_curseg(sbi, type, false); |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2494 | else if (f2fs_need_SSR(sbi) && get_ssr_segment(sbi, type)) |
Chao Yu | 025d63a | 2017-08-30 18:04:48 +0800 | [diff] [blame] | 2495 | change_curseg(sbi, type); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2496 | else |
| 2497 | new_curseg(sbi, type, false); |
Jaegeuk Kim | dcdfff6 | 2013-10-22 20:56:10 +0900 | [diff] [blame] | 2498 | |
Jaegeuk Kim | a788189 | 2017-04-20 13:51:57 -0700 | [diff] [blame] | 2499 | stat_inc_seg_type(sbi, curseg); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2500 | } |
| 2501 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2502 | void f2fs_allocate_new_segments(struct f2fs_sb_info *sbi) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2503 | { |
Jaegeuk Kim | 6ae1be1 | 2016-11-11 12:31:40 -0800 | [diff] [blame] | 2504 | struct curseg_info *curseg; |
| 2505 | unsigned int old_segno; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2506 | int i; |
| 2507 | |
Chao Yu | 3d26fa6 | 2017-10-30 17:49:53 +0800 | [diff] [blame] | 2508 | down_write(&SIT_I(sbi)->sentry_lock); |
| 2509 | |
Jaegeuk Kim | 6ae1be1 | 2016-11-11 12:31:40 -0800 | [diff] [blame] | 2510 | for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) { |
| 2511 | curseg = CURSEG_I(sbi, i); |
| 2512 | old_segno = curseg->segno; |
| 2513 | SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true); |
| 2514 | locate_dirty_segment(sbi, old_segno); |
| 2515 | } |
Chao Yu | 3d26fa6 | 2017-10-30 17:49:53 +0800 | [diff] [blame] | 2516 | |
| 2517 | up_write(&SIT_I(sbi)->sentry_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2518 | } |
| 2519 | |
| 2520 | static const struct segment_allocation default_salloc_ops = { |
| 2521 | .allocate_segment = allocate_segment_by_default, |
| 2522 | }; |
| 2523 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2524 | bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi, |
| 2525 | struct cp_control *cpc) |
Jaegeuk Kim | 25290fa | 2016-12-29 22:06:15 -0800 | [diff] [blame] | 2526 | { |
| 2527 | __u64 trim_start = cpc->trim_start; |
| 2528 | bool has_candidate = false; |
| 2529 | |
Chao Yu | 3d26fa6 | 2017-10-30 17:49:53 +0800 | [diff] [blame] | 2530 | down_write(&SIT_I(sbi)->sentry_lock); |
Jaegeuk Kim | 25290fa | 2016-12-29 22:06:15 -0800 | [diff] [blame] | 2531 | for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++) { |
| 2532 | if (add_discard_addrs(sbi, cpc, true)) { |
| 2533 | has_candidate = true; |
| 2534 | break; |
| 2535 | } |
| 2536 | } |
Chao Yu | 3d26fa6 | 2017-10-30 17:49:53 +0800 | [diff] [blame] | 2537 | up_write(&SIT_I(sbi)->sentry_lock); |
Jaegeuk Kim | 25290fa | 2016-12-29 22:06:15 -0800 | [diff] [blame] | 2538 | |
| 2539 | cpc->trim_start = trim_start; |
| 2540 | return has_candidate; |
| 2541 | } |
| 2542 | |
Chao Yu | 01f9cf6 | 2018-06-25 20:33:24 +0800 | [diff] [blame] | 2543 | static unsigned int __issue_discard_cmd_range(struct f2fs_sb_info *sbi, |
Jaegeuk Kim | 9a99718 | 2018-05-24 13:57:26 -0700 | [diff] [blame] | 2544 | struct discard_policy *dpolicy, |
| 2545 | unsigned int start, unsigned int end) |
| 2546 | { |
| 2547 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
| 2548 | struct discard_cmd *prev_dc = NULL, *next_dc = NULL; |
| 2549 | struct rb_node **insert_p = NULL, *insert_parent = NULL; |
| 2550 | struct discard_cmd *dc; |
| 2551 | struct blk_plug plug; |
| 2552 | int issued; |
Chao Yu | 01f9cf6 | 2018-06-25 20:33:24 +0800 | [diff] [blame] | 2553 | unsigned int trimmed = 0; |
Jaegeuk Kim | 9a99718 | 2018-05-24 13:57:26 -0700 | [diff] [blame] | 2554 | |
| 2555 | next: |
| 2556 | issued = 0; |
| 2557 | |
| 2558 | mutex_lock(&dcc->cmd_lock); |
Chao Yu | 67fce70 | 2018-06-22 16:06:59 +0800 | [diff] [blame] | 2559 | if (unlikely(dcc->rbtree_check)) |
| 2560 | f2fs_bug_on(sbi, !f2fs_check_rb_tree_consistence(sbi, |
| 2561 | &dcc->root)); |
Jaegeuk Kim | 9a99718 | 2018-05-24 13:57:26 -0700 | [diff] [blame] | 2562 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2563 | dc = (struct discard_cmd *)f2fs_lookup_rb_tree_ret(&dcc->root, |
Jaegeuk Kim | 9a99718 | 2018-05-24 13:57:26 -0700 | [diff] [blame] | 2564 | NULL, start, |
| 2565 | (struct rb_entry **)&prev_dc, |
| 2566 | (struct rb_entry **)&next_dc, |
| 2567 | &insert_p, &insert_parent, true); |
| 2568 | if (!dc) |
| 2569 | dc = next_dc; |
| 2570 | |
| 2571 | blk_start_plug(&plug); |
| 2572 | |
| 2573 | while (dc && dc->lstart <= end) { |
| 2574 | struct rb_node *node; |
Chao Yu | 6b9cb12 | 2018-08-08 10:14:55 +0800 | [diff] [blame] | 2575 | int err = 0; |
Jaegeuk Kim | 9a99718 | 2018-05-24 13:57:26 -0700 | [diff] [blame] | 2576 | |
| 2577 | if (dc->len < dpolicy->granularity) |
| 2578 | goto skip; |
| 2579 | |
| 2580 | if (dc->state != D_PREP) { |
| 2581 | list_move_tail(&dc->list, &dcc->fstrim_list); |
| 2582 | goto skip; |
| 2583 | } |
| 2584 | |
Chao Yu | 6b9cb12 | 2018-08-08 10:14:55 +0800 | [diff] [blame] | 2585 | err = __submit_discard_cmd(sbi, dpolicy, dc, &issued); |
Jaegeuk Kim | 9a99718 | 2018-05-24 13:57:26 -0700 | [diff] [blame] | 2586 | |
Chao Yu | 35ec7d5 | 2018-08-06 22:43:50 +0800 | [diff] [blame] | 2587 | if (issued >= dpolicy->max_requests) { |
Jaegeuk Kim | 9a99718 | 2018-05-24 13:57:26 -0700 | [diff] [blame] | 2588 | start = dc->lstart + dc->len; |
| 2589 | |
Chao Yu | 6b9cb12 | 2018-08-08 10:14:55 +0800 | [diff] [blame] | 2590 | if (err) |
| 2591 | __remove_discard_cmd(sbi, dc); |
| 2592 | |
Jaegeuk Kim | 9a99718 | 2018-05-24 13:57:26 -0700 | [diff] [blame] | 2593 | blk_finish_plug(&plug); |
| 2594 | mutex_unlock(&dcc->cmd_lock); |
Chao Yu | 01f9cf6 | 2018-06-25 20:33:24 +0800 | [diff] [blame] | 2595 | trimmed += __wait_all_discard_cmd(sbi, NULL); |
Jaegeuk Kim | 9a99718 | 2018-05-24 13:57:26 -0700 | [diff] [blame] | 2596 | congestion_wait(BLK_RW_ASYNC, HZ/50); |
| 2597 | goto next; |
| 2598 | } |
| 2599 | skip: |
| 2600 | node = rb_next(&dc->rb_node); |
Chao Yu | 6b9cb12 | 2018-08-08 10:14:55 +0800 | [diff] [blame] | 2601 | if (err) |
| 2602 | __remove_discard_cmd(sbi, dc); |
Jaegeuk Kim | 9a99718 | 2018-05-24 13:57:26 -0700 | [diff] [blame] | 2603 | dc = rb_entry_safe(node, struct discard_cmd, rb_node); |
| 2604 | |
| 2605 | if (fatal_signal_pending(current)) |
| 2606 | break; |
| 2607 | } |
| 2608 | |
| 2609 | blk_finish_plug(&plug); |
| 2610 | mutex_unlock(&dcc->cmd_lock); |
Chao Yu | 01f9cf6 | 2018-06-25 20:33:24 +0800 | [diff] [blame] | 2611 | |
| 2612 | return trimmed; |
Jaegeuk Kim | 9a99718 | 2018-05-24 13:57:26 -0700 | [diff] [blame] | 2613 | } |
| 2614 | |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 2615 | int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range) |
| 2616 | { |
Jaegeuk Kim | f7ef9b8 | 2015-02-09 12:02:44 -0800 | [diff] [blame] | 2617 | __u64 start = F2FS_BYTES_TO_BLK(range->start); |
| 2618 | __u64 end = start + F2FS_BYTES_TO_BLK(range->len) - 1; |
Chao Yu | 377224c | 2018-04-09 10:25:23 +0800 | [diff] [blame] | 2619 | unsigned int start_segno, end_segno; |
Chao Yu | 8412663 | 2017-10-04 09:08:32 +0800 | [diff] [blame] | 2620 | block_t start_block, end_block; |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 2621 | struct cp_control cpc; |
Chao Yu | 78997b5 | 2017-10-04 09:08:34 +0800 | [diff] [blame] | 2622 | struct discard_policy dpolicy; |
Chao Yu | 0ea8051 | 2017-10-28 16:52:32 +0800 | [diff] [blame] | 2623 | unsigned long long trimmed = 0; |
Chao Yu | c34f42e | 2015-12-23 17:50:30 +0800 | [diff] [blame] | 2624 | int err = 0; |
Yunlong Song | ad6672b | 2018-07-19 20:58:15 +0800 | [diff] [blame] | 2625 | bool need_align = test_opt(sbi, LFS) && sbi->segs_per_sec > 1; |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 2626 | |
Jaegeuk Kim | 836b5a6 | 2015-04-30 22:50:06 -0700 | [diff] [blame] | 2627 | if (start >= MAX_BLKADDR(sbi) || range->len < sbi->blocksize) |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 2628 | return -EINVAL; |
| 2629 | |
Chao Yu | 3f16ecd9 | 2018-08-08 17:36:29 +0800 | [diff] [blame] | 2630 | if (end < MAIN_BLKADDR(sbi)) |
| 2631 | goto out; |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 2632 | |
Yunlei He | ed214a1 | 2016-09-01 10:14:39 +0800 | [diff] [blame] | 2633 | if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) { |
| 2634 | f2fs_msg(sbi->sb, KERN_WARNING, |
| 2635 | "Found FS corruption, run fsck to fix."); |
Chao Yu | 3d165dc | 2018-04-08 20:39:03 +0800 | [diff] [blame] | 2636 | return -EIO; |
Yunlei He | ed214a1 | 2016-09-01 10:14:39 +0800 | [diff] [blame] | 2637 | } |
| 2638 | |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 2639 | /* start/end segment number in main_area */ |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 2640 | start_segno = (start <= MAIN_BLKADDR(sbi)) ? 0 : GET_SEGNO(sbi, start); |
| 2641 | end_segno = (end >= MAX_BLKADDR(sbi)) ? MAIN_SEGS(sbi) - 1 : |
| 2642 | GET_SEGNO(sbi, end); |
Yunlong Song | ad6672b | 2018-07-19 20:58:15 +0800 | [diff] [blame] | 2643 | if (need_align) { |
| 2644 | start_segno = rounddown(start_segno, sbi->segs_per_sec); |
| 2645 | end_segno = roundup(end_segno + 1, sbi->segs_per_sec) - 1; |
| 2646 | } |
Chao Yu | 8412663 | 2017-10-04 09:08:32 +0800 | [diff] [blame] | 2647 | |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 2648 | cpc.reason = CP_DISCARD; |
Jaegeuk Kim | 836b5a6 | 2015-04-30 22:50:06 -0700 | [diff] [blame] | 2649 | 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] | 2650 | cpc.trim_start = start_segno; |
| 2651 | cpc.trim_end = end_segno; |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 2652 | |
Chao Yu | 377224c | 2018-04-09 10:25:23 +0800 | [diff] [blame] | 2653 | if (sbi->discard_blks == 0) |
| 2654 | goto out; |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 2655 | |
Chao Yu | 377224c | 2018-04-09 10:25:23 +0800 | [diff] [blame] | 2656 | mutex_lock(&sbi->gc_mutex); |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2657 | err = f2fs_write_checkpoint(sbi, &cpc); |
Chao Yu | 377224c | 2018-04-09 10:25:23 +0800 | [diff] [blame] | 2658 | mutex_unlock(&sbi->gc_mutex); |
| 2659 | if (err) |
| 2660 | goto out; |
Chao Yu | 8412663 | 2017-10-04 09:08:32 +0800 | [diff] [blame] | 2661 | |
Jaegeuk Kim | e555da9 | 2018-05-31 10:20:48 -0700 | [diff] [blame] | 2662 | /* |
| 2663 | * We filed discard candidates, but actually we don't need to wait for |
| 2664 | * all of them, since they'll be issued in idle time along with runtime |
| 2665 | * discard option. User configuration looks like using runtime discard |
| 2666 | * or periodic fstrim instead of it. |
| 2667 | */ |
Jaegeuk Kim | 5a61549 | 2018-06-20 21:27:21 -0700 | [diff] [blame] | 2668 | if (test_opt(sbi, DISCARD)) |
| 2669 | goto out; |
| 2670 | |
| 2671 | start_block = START_BLOCK(sbi, start_segno); |
| 2672 | end_block = START_BLOCK(sbi, end_segno + 1); |
| 2673 | |
| 2674 | __init_discard_policy(sbi, &dpolicy, DPOLICY_FSTRIM, cpc.trim_minlen); |
Chao Yu | 01f9cf6 | 2018-06-25 20:33:24 +0800 | [diff] [blame] | 2675 | trimmed = __issue_discard_cmd_range(sbi, &dpolicy, |
| 2676 | start_block, end_block); |
Jaegeuk Kim | 5a61549 | 2018-06-20 21:27:21 -0700 | [diff] [blame] | 2677 | |
Chao Yu | 01f9cf6 | 2018-06-25 20:33:24 +0800 | [diff] [blame] | 2678 | trimmed += __wait_discard_cmd_range(sbi, &dpolicy, |
Chao Yu | 0ea8051 | 2017-10-28 16:52:32 +0800 | [diff] [blame] | 2679 | start_block, end_block); |
Chao Yu | 377224c | 2018-04-09 10:25:23 +0800 | [diff] [blame] | 2680 | out: |
Chao Yu | 6eae269 | 2018-08-05 23:09:00 +0800 | [diff] [blame] | 2681 | if (!err) |
| 2682 | range->len = F2FS_BLK_TO_BYTES(trimmed); |
Chao Yu | c34f42e | 2015-12-23 17:50:30 +0800 | [diff] [blame] | 2683 | return err; |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 2684 | } |
| 2685 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2686 | static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type) |
| 2687 | { |
| 2688 | struct curseg_info *curseg = CURSEG_I(sbi, type); |
| 2689 | if (curseg->next_blkoff < sbi->blocks_per_seg) |
| 2690 | return true; |
| 2691 | return false; |
| 2692 | } |
| 2693 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2694 | int f2fs_rw_hint_to_seg_type(enum rw_hint hint) |
Hyunchul Lee | 4f0a03d3 | 2017-11-09 14:51:27 +0900 | [diff] [blame] | 2695 | { |
| 2696 | switch (hint) { |
| 2697 | case WRITE_LIFE_SHORT: |
| 2698 | return CURSEG_HOT_DATA; |
| 2699 | case WRITE_LIFE_EXTREME: |
| 2700 | return CURSEG_COLD_DATA; |
| 2701 | default: |
| 2702 | return CURSEG_WARM_DATA; |
| 2703 | } |
| 2704 | } |
| 2705 | |
Hyunchul Lee | 0cdd319 | 2018-01-31 11:36:57 +0900 | [diff] [blame] | 2706 | /* This returns write hints for each segment type. This hints will be |
| 2707 | * passed down to block layer. There are mapping tables which depend on |
| 2708 | * the mount option 'whint_mode'. |
| 2709 | * |
| 2710 | * 1) whint_mode=off. F2FS only passes down WRITE_LIFE_NOT_SET. |
| 2711 | * |
| 2712 | * 2) whint_mode=user-based. F2FS tries to pass down hints given by users. |
| 2713 | * |
| 2714 | * User F2FS Block |
| 2715 | * ---- ---- ----- |
| 2716 | * META WRITE_LIFE_NOT_SET |
| 2717 | * HOT_NODE " |
| 2718 | * WARM_NODE " |
| 2719 | * COLD_NODE " |
| 2720 | * ioctl(COLD) COLD_DATA WRITE_LIFE_EXTREME |
| 2721 | * extension list " " |
| 2722 | * |
| 2723 | * -- buffered io |
| 2724 | * WRITE_LIFE_EXTREME COLD_DATA WRITE_LIFE_EXTREME |
| 2725 | * WRITE_LIFE_SHORT HOT_DATA WRITE_LIFE_SHORT |
| 2726 | * WRITE_LIFE_NOT_SET WARM_DATA WRITE_LIFE_NOT_SET |
| 2727 | * WRITE_LIFE_NONE " " |
| 2728 | * WRITE_LIFE_MEDIUM " " |
| 2729 | * WRITE_LIFE_LONG " " |
| 2730 | * |
| 2731 | * -- direct io |
| 2732 | * WRITE_LIFE_EXTREME COLD_DATA WRITE_LIFE_EXTREME |
| 2733 | * WRITE_LIFE_SHORT HOT_DATA WRITE_LIFE_SHORT |
| 2734 | * WRITE_LIFE_NOT_SET WARM_DATA WRITE_LIFE_NOT_SET |
| 2735 | * WRITE_LIFE_NONE " WRITE_LIFE_NONE |
| 2736 | * WRITE_LIFE_MEDIUM " WRITE_LIFE_MEDIUM |
| 2737 | * WRITE_LIFE_LONG " WRITE_LIFE_LONG |
| 2738 | * |
Hyunchul Lee | f2e703f | 2018-01-31 11:36:58 +0900 | [diff] [blame] | 2739 | * 3) whint_mode=fs-based. F2FS passes down hints with its policy. |
| 2740 | * |
| 2741 | * User F2FS Block |
| 2742 | * ---- ---- ----- |
| 2743 | * META WRITE_LIFE_MEDIUM; |
| 2744 | * HOT_NODE WRITE_LIFE_NOT_SET |
| 2745 | * WARM_NODE " |
| 2746 | * COLD_NODE WRITE_LIFE_NONE |
| 2747 | * ioctl(COLD) COLD_DATA WRITE_LIFE_EXTREME |
| 2748 | * extension list " " |
| 2749 | * |
| 2750 | * -- buffered io |
| 2751 | * WRITE_LIFE_EXTREME COLD_DATA WRITE_LIFE_EXTREME |
| 2752 | * WRITE_LIFE_SHORT HOT_DATA WRITE_LIFE_SHORT |
| 2753 | * WRITE_LIFE_NOT_SET WARM_DATA WRITE_LIFE_LONG |
| 2754 | * WRITE_LIFE_NONE " " |
| 2755 | * WRITE_LIFE_MEDIUM " " |
| 2756 | * WRITE_LIFE_LONG " " |
| 2757 | * |
| 2758 | * -- direct io |
| 2759 | * WRITE_LIFE_EXTREME COLD_DATA WRITE_LIFE_EXTREME |
| 2760 | * WRITE_LIFE_SHORT HOT_DATA WRITE_LIFE_SHORT |
| 2761 | * WRITE_LIFE_NOT_SET WARM_DATA WRITE_LIFE_NOT_SET |
| 2762 | * WRITE_LIFE_NONE " WRITE_LIFE_NONE |
| 2763 | * WRITE_LIFE_MEDIUM " WRITE_LIFE_MEDIUM |
| 2764 | * WRITE_LIFE_LONG " WRITE_LIFE_LONG |
Hyunchul Lee | 0cdd319 | 2018-01-31 11:36:57 +0900 | [diff] [blame] | 2765 | */ |
| 2766 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2767 | 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] | 2768 | enum page_type type, enum temp_type temp) |
| 2769 | { |
Chao Yu | 63189b7 | 2018-03-08 14:22:56 +0800 | [diff] [blame] | 2770 | if (F2FS_OPTION(sbi).whint_mode == WHINT_MODE_USER) { |
Hyunchul Lee | 0cdd319 | 2018-01-31 11:36:57 +0900 | [diff] [blame] | 2771 | if (type == DATA) { |
Hyunchul Lee | f2e703f | 2018-01-31 11:36:58 +0900 | [diff] [blame] | 2772 | if (temp == WARM) |
Hyunchul Lee | 0cdd319 | 2018-01-31 11:36:57 +0900 | [diff] [blame] | 2773 | return WRITE_LIFE_NOT_SET; |
Hyunchul Lee | f2e703f | 2018-01-31 11:36:58 +0900 | [diff] [blame] | 2774 | else if (temp == HOT) |
| 2775 | return WRITE_LIFE_SHORT; |
| 2776 | else if (temp == COLD) |
| 2777 | return WRITE_LIFE_EXTREME; |
Hyunchul Lee | 0cdd319 | 2018-01-31 11:36:57 +0900 | [diff] [blame] | 2778 | } else { |
| 2779 | return WRITE_LIFE_NOT_SET; |
| 2780 | } |
Chao Yu | 63189b7 | 2018-03-08 14:22:56 +0800 | [diff] [blame] | 2781 | } else if (F2FS_OPTION(sbi).whint_mode == WHINT_MODE_FS) { |
Hyunchul Lee | f2e703f | 2018-01-31 11:36:58 +0900 | [diff] [blame] | 2782 | if (type == DATA) { |
| 2783 | if (temp == WARM) |
| 2784 | return WRITE_LIFE_LONG; |
| 2785 | else if (temp == HOT) |
| 2786 | return WRITE_LIFE_SHORT; |
| 2787 | else if (temp == COLD) |
| 2788 | return WRITE_LIFE_EXTREME; |
| 2789 | } else if (type == NODE) { |
| 2790 | if (temp == WARM || temp == HOT) |
| 2791 | return WRITE_LIFE_NOT_SET; |
| 2792 | else if (temp == COLD) |
| 2793 | return WRITE_LIFE_NONE; |
| 2794 | } else if (type == META) { |
| 2795 | return WRITE_LIFE_MEDIUM; |
| 2796 | } |
Hyunchul Lee | 0cdd319 | 2018-01-31 11:36:57 +0900 | [diff] [blame] | 2797 | } |
Hyunchul Lee | f2e703f | 2018-01-31 11:36:58 +0900 | [diff] [blame] | 2798 | return WRITE_LIFE_NOT_SET; |
Hyunchul Lee | 0cdd319 | 2018-01-31 11:36:57 +0900 | [diff] [blame] | 2799 | } |
| 2800 | |
Jaegeuk Kim | 81377bd | 2017-05-10 14:19:54 -0700 | [diff] [blame] | 2801 | static int __get_segment_type_2(struct f2fs_io_info *fio) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2802 | { |
Jaegeuk Kim | 81377bd | 2017-05-10 14:19:54 -0700 | [diff] [blame] | 2803 | if (fio->type == DATA) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2804 | return CURSEG_HOT_DATA; |
| 2805 | else |
| 2806 | return CURSEG_HOT_NODE; |
| 2807 | } |
| 2808 | |
Jaegeuk Kim | 81377bd | 2017-05-10 14:19:54 -0700 | [diff] [blame] | 2809 | static int __get_segment_type_4(struct f2fs_io_info *fio) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2810 | { |
Jaegeuk Kim | 81377bd | 2017-05-10 14:19:54 -0700 | [diff] [blame] | 2811 | if (fio->type == DATA) { |
| 2812 | struct inode *inode = fio->page->mapping->host; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2813 | |
| 2814 | if (S_ISDIR(inode->i_mode)) |
| 2815 | return CURSEG_HOT_DATA; |
| 2816 | else |
| 2817 | return CURSEG_COLD_DATA; |
| 2818 | } else { |
Jaegeuk Kim | 81377bd | 2017-05-10 14:19:54 -0700 | [diff] [blame] | 2819 | if (IS_DNODE(fio->page) && is_cold_node(fio->page)) |
Jaegeuk Kim | a344b9f | 2014-11-05 20:05:53 -0800 | [diff] [blame] | 2820 | return CURSEG_WARM_NODE; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2821 | else |
| 2822 | return CURSEG_COLD_NODE; |
| 2823 | } |
| 2824 | } |
| 2825 | |
Jaegeuk Kim | 81377bd | 2017-05-10 14:19:54 -0700 | [diff] [blame] | 2826 | static int __get_segment_type_6(struct f2fs_io_info *fio) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2827 | { |
Jaegeuk Kim | 81377bd | 2017-05-10 14:19:54 -0700 | [diff] [blame] | 2828 | if (fio->type == DATA) { |
| 2829 | struct inode *inode = fio->page->mapping->host; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2830 | |
Jaegeuk Kim | 81377bd | 2017-05-10 14:19:54 -0700 | [diff] [blame] | 2831 | if (is_cold_data(fio->page) || file_is_cold(inode)) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2832 | return CURSEG_COLD_DATA; |
Chao Yu | b6a06cb | 2018-02-28 17:07:27 +0800 | [diff] [blame] | 2833 | if (file_is_hot(inode) || |
Chao Yu | b4c3ca8 | 2018-04-26 17:05:50 +0800 | [diff] [blame] | 2834 | is_inode_flag_set(inode, FI_HOT_DATA) || |
Chao Yu | 2079f11 | 2018-07-17 20:41:48 +0800 | [diff] [blame] | 2835 | f2fs_is_atomic_file(inode) || |
| 2836 | f2fs_is_volatile_file(inode)) |
Jaegeuk Kim | ef095d1 | 2017-03-24 20:05:13 -0400 | [diff] [blame] | 2837 | return CURSEG_HOT_DATA; |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2838 | return f2fs_rw_hint_to_seg_type(inode->i_write_hint); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2839 | } else { |
Jaegeuk Kim | 81377bd | 2017-05-10 14:19:54 -0700 | [diff] [blame] | 2840 | if (IS_DNODE(fio->page)) |
| 2841 | return is_cold_node(fio->page) ? CURSEG_WARM_NODE : |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2842 | CURSEG_HOT_NODE; |
Jaegeuk Kim | ef095d1 | 2017-03-24 20:05:13 -0400 | [diff] [blame] | 2843 | return CURSEG_COLD_NODE; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2844 | } |
| 2845 | } |
| 2846 | |
Jaegeuk Kim | 81377bd | 2017-05-10 14:19:54 -0700 | [diff] [blame] | 2847 | static int __get_segment_type(struct f2fs_io_info *fio) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2848 | { |
Jaegeuk Kim | a912b54 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 2849 | int type = 0; |
| 2850 | |
Chao Yu | 63189b7 | 2018-03-08 14:22:56 +0800 | [diff] [blame] | 2851 | switch (F2FS_OPTION(fio->sbi).active_logs) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2852 | case 2: |
Jaegeuk Kim | a912b54 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 2853 | type = __get_segment_type_2(fio); |
| 2854 | break; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2855 | case 4: |
Jaegeuk Kim | a912b54 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 2856 | type = __get_segment_type_4(fio); |
| 2857 | break; |
| 2858 | case 6: |
| 2859 | type = __get_segment_type_6(fio); |
| 2860 | break; |
| 2861 | default: |
| 2862 | f2fs_bug_on(fio->sbi, true); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2863 | } |
Jaegeuk Kim | 81377bd | 2017-05-10 14:19:54 -0700 | [diff] [blame] | 2864 | |
Jaegeuk Kim | a912b54 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 2865 | if (IS_HOT(type)) |
| 2866 | fio->temp = HOT; |
| 2867 | else if (IS_WARM(type)) |
| 2868 | fio->temp = WARM; |
| 2869 | else |
| 2870 | fio->temp = COLD; |
| 2871 | return type; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2872 | } |
| 2873 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2874 | 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] | 2875 | block_t old_blkaddr, block_t *new_blkaddr, |
Chao Yu | fb830fc | 2017-05-19 23:37:01 +0800 | [diff] [blame] | 2876 | struct f2fs_summary *sum, int type, |
| 2877 | struct f2fs_io_info *fio, bool add_list) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2878 | { |
| 2879 | struct sit_info *sit_i = SIT_I(sbi); |
Jaegeuk Kim | 6ae1be1 | 2016-11-11 12:31:40 -0800 | [diff] [blame] | 2880 | struct curseg_info *curseg = CURSEG_I(sbi, type); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2881 | |
Chao Yu | 2b60311 | 2017-11-02 20:41:03 +0800 | [diff] [blame] | 2882 | down_read(&SM_I(sbi)->curseg_lock); |
| 2883 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2884 | mutex_lock(&curseg->curseg_mutex); |
Chao Yu | 3d26fa6 | 2017-10-30 17:49:53 +0800 | [diff] [blame] | 2885 | down_write(&sit_i->sentry_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2886 | |
| 2887 | *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2888 | |
Jaegeuk Kim | 4e6a8d9 | 2016-12-29 14:07:53 -0800 | [diff] [blame] | 2889 | f2fs_wait_discard_bio(sbi, *new_blkaddr); |
| 2890 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2891 | /* |
| 2892 | * __add_sum_entry should be resided under the curseg_mutex |
| 2893 | * because, this function updates a summary entry in the |
| 2894 | * current summary block. |
| 2895 | */ |
Haicheng Li | e79efe3 | 2013-06-13 16:59:27 +0800 | [diff] [blame] | 2896 | __add_sum_entry(sbi, type, sum); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2897 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2898 | __refresh_next_blkoff(sbi, curseg); |
Jaegeuk Kim | dcdfff6 | 2013-10-22 20:56:10 +0900 | [diff] [blame] | 2899 | |
| 2900 | stat_inc_block_count(sbi, curseg); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2901 | |
Yunlong Song | 65f1b80 | 2017-10-30 09:33:41 +0800 | [diff] [blame] | 2902 | /* |
| 2903 | * SIT information should be updated before segment allocation, |
| 2904 | * since SSR needs latest valid block information. |
| 2905 | */ |
| 2906 | update_sit_entry(sbi, *new_blkaddr, 1); |
| 2907 | if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO) |
| 2908 | update_sit_entry(sbi, old_blkaddr, -1); |
| 2909 | |
Yunlong Song | 3436c4b | 2017-02-21 16:59:26 +0800 | [diff] [blame] | 2910 | if (!__has_curseg_space(sbi, type)) |
| 2911 | sit_i->s_ops->allocate_segment(sbi, type, false); |
Yunlong Song | 65f1b80 | 2017-10-30 09:33:41 +0800 | [diff] [blame] | 2912 | |
Jaegeuk Kim | c6f82fe9 | 2017-04-04 16:45:30 -0700 | [diff] [blame] | 2913 | /* |
Yunlong Song | 65f1b80 | 2017-10-30 09:33:41 +0800 | [diff] [blame] | 2914 | * segment dirty status should be updated after segment allocation, |
| 2915 | * so we just need to update status only one time after previous |
| 2916 | * segment being closed. |
Jaegeuk Kim | c6f82fe9 | 2017-04-04 16:45:30 -0700 | [diff] [blame] | 2917 | */ |
Yunlong Song | 65f1b80 | 2017-10-30 09:33:41 +0800 | [diff] [blame] | 2918 | locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr)); |
| 2919 | locate_dirty_segment(sbi, GET_SEGNO(sbi, *new_blkaddr)); |
Yunlong Song | 3436c4b | 2017-02-21 16:59:26 +0800 | [diff] [blame] | 2920 | |
Chao Yu | 3d26fa6 | 2017-10-30 17:49:53 +0800 | [diff] [blame] | 2921 | up_write(&sit_i->sentry_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2922 | |
Chao Yu | 704956e | 2017-07-31 20:19:09 +0800 | [diff] [blame] | 2923 | if (page && IS_NODESEG(type)) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2924 | fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg)); |
| 2925 | |
Chao Yu | 704956e | 2017-07-31 20:19:09 +0800 | [diff] [blame] | 2926 | f2fs_inode_chksum_set(sbi, page); |
| 2927 | } |
| 2928 | |
Chao Yu | fb830fc | 2017-05-19 23:37:01 +0800 | [diff] [blame] | 2929 | if (add_list) { |
| 2930 | struct f2fs_bio_info *io; |
| 2931 | |
| 2932 | INIT_LIST_HEAD(&fio->list); |
| 2933 | fio->in_list = true; |
Chao Yu | fe16efe | 2018-05-28 23:47:18 +0800 | [diff] [blame] | 2934 | fio->retry = false; |
Chao Yu | fb830fc | 2017-05-19 23:37:01 +0800 | [diff] [blame] | 2935 | io = sbi->write_io[fio->type] + fio->temp; |
| 2936 | spin_lock(&io->io_lock); |
| 2937 | list_add_tail(&fio->list, &io->io_list); |
| 2938 | spin_unlock(&io->io_lock); |
| 2939 | } |
| 2940 | |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 2941 | mutex_unlock(&curseg->curseg_mutex); |
Chao Yu | 2b60311 | 2017-11-02 20:41:03 +0800 | [diff] [blame] | 2942 | |
| 2943 | up_read(&SM_I(sbi)->curseg_lock); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 2944 | } |
| 2945 | |
Chao Yu | 39d787b | 2017-09-29 13:59:38 +0800 | [diff] [blame] | 2946 | static void update_device_state(struct f2fs_io_info *fio) |
| 2947 | { |
| 2948 | struct f2fs_sb_info *sbi = fio->sbi; |
| 2949 | unsigned int devidx; |
| 2950 | |
| 2951 | if (!sbi->s_ndevs) |
| 2952 | return; |
| 2953 | |
| 2954 | devidx = f2fs_target_device_index(sbi, fio->new_blkaddr); |
| 2955 | |
| 2956 | /* update device state for fsync */ |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2957 | f2fs_set_dirty_device(sbi, fio->ino, devidx, FLUSH_INO); |
Chao Yu | 1228b48 | 2017-09-29 13:59:39 +0800 | [diff] [blame] | 2958 | |
| 2959 | /* update device state for checkpoint */ |
| 2960 | if (!f2fs_test_bit(devidx, (char *)&sbi->dirty_device)) { |
| 2961 | spin_lock(&sbi->dev_lock); |
| 2962 | f2fs_set_bit(devidx, (char *)&sbi->dirty_device); |
| 2963 | spin_unlock(&sbi->dev_lock); |
| 2964 | } |
Chao Yu | 39d787b | 2017-09-29 13:59:38 +0800 | [diff] [blame] | 2965 | } |
| 2966 | |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 2967 | 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] | 2968 | { |
Jaegeuk Kim | 81377bd | 2017-05-10 14:19:54 -0700 | [diff] [blame] | 2969 | int type = __get_segment_type(fio); |
Chao Yu | 107a805 | 2018-05-26 09:00:13 +0800 | [diff] [blame] | 2970 | bool keep_order = (test_opt(fio->sbi, LFS) && type == CURSEG_COLD_DATA); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 2971 | |
Chao Yu | 107a805 | 2018-05-26 09:00:13 +0800 | [diff] [blame] | 2972 | if (keep_order) |
| 2973 | down_read(&fio->sbi->io_order_lock); |
Jaegeuk Kim | 0a595eb | 2016-12-14 10:12:56 -0800 | [diff] [blame] | 2974 | reallocate: |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2975 | f2fs_allocate_data_block(fio->sbi, fio->page, fio->old_blkaddr, |
Chao Yu | fb830fc | 2017-05-19 23:37:01 +0800 | [diff] [blame] | 2976 | &fio->new_blkaddr, sum, type, fio, true); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 2977 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2978 | /* writeout dirty page into bdev */ |
Chao Yu | fe16efe | 2018-05-28 23:47:18 +0800 | [diff] [blame] | 2979 | f2fs_submit_page_write(fio); |
| 2980 | if (fio->retry) { |
Jaegeuk Kim | 0a595eb | 2016-12-14 10:12:56 -0800 | [diff] [blame] | 2981 | fio->old_blkaddr = fio->new_blkaddr; |
| 2982 | goto reallocate; |
| 2983 | } |
Chao Yu | fe16efe | 2018-05-28 23:47:18 +0800 | [diff] [blame] | 2984 | |
| 2985 | update_device_state(fio); |
| 2986 | |
Chao Yu | 107a805 | 2018-05-26 09:00:13 +0800 | [diff] [blame] | 2987 | if (keep_order) |
| 2988 | up_read(&fio->sbi->io_order_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2989 | } |
| 2990 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2991 | 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] | 2992 | enum iostat_type io_type) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2993 | { |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 2994 | struct f2fs_io_info fio = { |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 2995 | .sbi = sbi, |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 2996 | .type = META, |
Hyunchul Lee | 0cdd319 | 2018-01-31 11:36:57 +0900 | [diff] [blame] | 2997 | .temp = HOT, |
Mike Christie | 04d328d | 2016-06-05 14:31:55 -0500 | [diff] [blame] | 2998 | .op = REQ_OP_WRITE, |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 2999 | .op_flags = REQ_SYNC | REQ_META | REQ_PRIO, |
Chao Yu | 7a9d754 | 2016-02-22 18:36:38 +0800 | [diff] [blame] | 3000 | .old_blkaddr = page->index, |
| 3001 | .new_blkaddr = page->index, |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 3002 | .page = page, |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 3003 | .encrypted_page = NULL, |
Chao Yu | fb830fc | 2017-05-19 23:37:01 +0800 | [diff] [blame] | 3004 | .in_list = false, |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 3005 | }; |
| 3006 | |
Chao Yu | 2b94700 | 2015-10-12 17:04:21 +0800 | [diff] [blame] | 3007 | if (unlikely(page->index >= MAIN_BLKADDR(sbi))) |
Mike Christie | 04d328d | 2016-06-05 14:31:55 -0500 | [diff] [blame] | 3008 | fio.op_flags &= ~REQ_META; |
Chao Yu | 2b94700 | 2015-10-12 17:04:21 +0800 | [diff] [blame] | 3009 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3010 | set_page_writeback(page); |
Jaegeuk Kim | 17c5003 | 2018-04-11 23:09:04 -0700 | [diff] [blame] | 3011 | ClearPageError(page); |
Jaegeuk Kim | b9109b0 | 2017-05-10 11:28:38 -0700 | [diff] [blame] | 3012 | f2fs_submit_page_write(&fio); |
Chao Yu | b0af6d4 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 3013 | |
| 3014 | f2fs_update_iostat(sbi, io_type, F2FS_BLKSIZE); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3015 | } |
| 3016 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 3017 | 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] | 3018 | { |
| 3019 | struct f2fs_summary sum; |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 3020 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3021 | set_summary(&sum, nid, 0, 0); |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 3022 | do_write_page(&sum, fio); |
Chao Yu | b0af6d4 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 3023 | |
| 3024 | f2fs_update_iostat(fio->sbi, fio->io_type, F2FS_BLKSIZE); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3025 | } |
| 3026 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 3027 | void f2fs_outplace_write_data(struct dnode_of_data *dn, |
| 3028 | struct f2fs_io_info *fio) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3029 | { |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 3030 | struct f2fs_sb_info *sbi = fio->sbi; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3031 | struct f2fs_summary sum; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3032 | |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 3033 | f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR); |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 3034 | set_summary(&sum, dn->nid, dn->ofs_in_node, fio->version); |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 3035 | do_write_page(&sum, fio); |
Chao Yu | f28b343 | 2016-02-24 17:16:47 +0800 | [diff] [blame] | 3036 | f2fs_update_data_blkaddr(dn, fio->new_blkaddr); |
Chao Yu | b0af6d4 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 3037 | |
| 3038 | f2fs_update_iostat(sbi, fio->io_type, F2FS_BLKSIZE); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3039 | } |
| 3040 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 3041 | int f2fs_inplace_write_data(struct f2fs_io_info *fio) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3042 | { |
Chao Yu | b0af6d4 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 3043 | int err; |
Yunlei He | d21b0f2 | 2018-03-26 17:32:23 +0800 | [diff] [blame] | 3044 | struct f2fs_sb_info *sbi = fio->sbi; |
Chao Yu | b0af6d4 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 3045 | |
Chao Yu | 7a9d754 | 2016-02-22 18:36:38 +0800 | [diff] [blame] | 3046 | fio->new_blkaddr = fio->old_blkaddr; |
Hyunchul Lee | 0cdd319 | 2018-01-31 11:36:57 +0900 | [diff] [blame] | 3047 | /* i/o temperature is needed for passing down write hints */ |
| 3048 | __get_segment_type(fio); |
Yunlei He | d21b0f2 | 2018-03-26 17:32:23 +0800 | [diff] [blame] | 3049 | |
| 3050 | f2fs_bug_on(sbi, !IS_DATASEG(get_seg_entry(sbi, |
| 3051 | GET_SEGNO(sbi, fio->new_blkaddr))->type)); |
| 3052 | |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 3053 | stat_inc_inplace_blocks(fio->sbi); |
Chao Yu | b0af6d4 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 3054 | |
| 3055 | err = f2fs_submit_page_bio(fio); |
Chao Yu | 39d787b | 2017-09-29 13:59:38 +0800 | [diff] [blame] | 3056 | if (!err) |
| 3057 | update_device_state(fio); |
Chao Yu | b0af6d4 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 3058 | |
| 3059 | f2fs_update_iostat(fio->sbi, fio->io_type, F2FS_BLKSIZE); |
| 3060 | |
| 3061 | return err; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3062 | } |
| 3063 | |
Chao Yu | 2b60311 | 2017-11-02 20:41:03 +0800 | [diff] [blame] | 3064 | static inline int __f2fs_get_curseg(struct f2fs_sb_info *sbi, |
| 3065 | unsigned int segno) |
| 3066 | { |
| 3067 | int i; |
| 3068 | |
| 3069 | for (i = CURSEG_HOT_DATA; i < NO_CHECK_TYPE; i++) { |
| 3070 | if (CURSEG_I(sbi, i)->segno == segno) |
| 3071 | break; |
| 3072 | } |
| 3073 | return i; |
| 3074 | } |
| 3075 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 3076 | 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] | 3077 | block_t old_blkaddr, block_t new_blkaddr, |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 3078 | bool recover_curseg, bool recover_newaddr) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3079 | { |
| 3080 | struct sit_info *sit_i = SIT_I(sbi); |
| 3081 | struct curseg_info *curseg; |
| 3082 | unsigned int segno, old_cursegno; |
| 3083 | struct seg_entry *se; |
| 3084 | int type; |
Chao Yu | 19f106b | 2015-05-06 13:08:06 +0800 | [diff] [blame] | 3085 | unsigned short old_blkoff; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3086 | |
| 3087 | segno = GET_SEGNO(sbi, new_blkaddr); |
| 3088 | se = get_seg_entry(sbi, segno); |
| 3089 | type = se->type; |
| 3090 | |
Chao Yu | 2b60311 | 2017-11-02 20:41:03 +0800 | [diff] [blame] | 3091 | down_write(&SM_I(sbi)->curseg_lock); |
| 3092 | |
Chao Yu | 19f106b | 2015-05-06 13:08:06 +0800 | [diff] [blame] | 3093 | if (!recover_curseg) { |
| 3094 | /* for recovery flow */ |
| 3095 | if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) { |
| 3096 | if (old_blkaddr == NULL_ADDR) |
| 3097 | type = CURSEG_COLD_DATA; |
| 3098 | else |
| 3099 | type = CURSEG_WARM_DATA; |
| 3100 | } |
| 3101 | } else { |
Chao Yu | 2b60311 | 2017-11-02 20:41:03 +0800 | [diff] [blame] | 3102 | if (IS_CURSEG(sbi, segno)) { |
| 3103 | /* se->type is volatile as SSR allocation */ |
| 3104 | type = __f2fs_get_curseg(sbi, segno); |
| 3105 | f2fs_bug_on(sbi, type == NO_CHECK_TYPE); |
| 3106 | } else { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3107 | type = CURSEG_WARM_DATA; |
Chao Yu | 2b60311 | 2017-11-02 20:41:03 +0800 | [diff] [blame] | 3108 | } |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3109 | } |
Chao Yu | 19f106b | 2015-05-06 13:08:06 +0800 | [diff] [blame] | 3110 | |
Yunlong Song | 2c19050 | 2018-01-04 15:02:02 +0800 | [diff] [blame] | 3111 | f2fs_bug_on(sbi, !IS_DATASEG(type)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3112 | curseg = CURSEG_I(sbi, type); |
| 3113 | |
| 3114 | mutex_lock(&curseg->curseg_mutex); |
Chao Yu | 3d26fa6 | 2017-10-30 17:49:53 +0800 | [diff] [blame] | 3115 | down_write(&sit_i->sentry_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3116 | |
| 3117 | old_cursegno = curseg->segno; |
Chao Yu | 19f106b | 2015-05-06 13:08:06 +0800 | [diff] [blame] | 3118 | old_blkoff = curseg->next_blkoff; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3119 | |
| 3120 | /* change the current segment */ |
| 3121 | if (segno != curseg->segno) { |
| 3122 | curseg->next_segno = segno; |
Chao Yu | 025d63a | 2017-08-30 18:04:48 +0800 | [diff] [blame] | 3123 | change_curseg(sbi, type); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3124 | } |
| 3125 | |
Jaegeuk Kim | 491c085 | 2014-02-04 13:01:10 +0900 | [diff] [blame] | 3126 | curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr); |
Haicheng Li | e79efe3 | 2013-06-13 16:59:27 +0800 | [diff] [blame] | 3127 | __add_sum_entry(sbi, type, sum); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3128 | |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 3129 | if (!recover_curseg || recover_newaddr) |
Jaegeuk Kim | 6e2c64a | 2015-10-07 12:28:41 -0700 | [diff] [blame] | 3130 | update_sit_entry(sbi, new_blkaddr, 1); |
| 3131 | if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO) |
| 3132 | update_sit_entry(sbi, old_blkaddr, -1); |
| 3133 | |
| 3134 | locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr)); |
| 3135 | locate_dirty_segment(sbi, GET_SEGNO(sbi, new_blkaddr)); |
| 3136 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3137 | locate_dirty_segment(sbi, old_cursegno); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3138 | |
Chao Yu | 19f106b | 2015-05-06 13:08:06 +0800 | [diff] [blame] | 3139 | if (recover_curseg) { |
| 3140 | if (old_cursegno != curseg->segno) { |
| 3141 | curseg->next_segno = old_cursegno; |
Chao Yu | 025d63a | 2017-08-30 18:04:48 +0800 | [diff] [blame] | 3142 | change_curseg(sbi, type); |
Chao Yu | 19f106b | 2015-05-06 13:08:06 +0800 | [diff] [blame] | 3143 | } |
| 3144 | curseg->next_blkoff = old_blkoff; |
| 3145 | } |
| 3146 | |
Chao Yu | 3d26fa6 | 2017-10-30 17:49:53 +0800 | [diff] [blame] | 3147 | up_write(&sit_i->sentry_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3148 | mutex_unlock(&curseg->curseg_mutex); |
Chao Yu | 2b60311 | 2017-11-02 20:41:03 +0800 | [diff] [blame] | 3149 | up_write(&SM_I(sbi)->curseg_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3150 | } |
| 3151 | |
Chao Yu | 528e345 | 2015-05-28 19:15:35 +0800 | [diff] [blame] | 3152 | void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn, |
| 3153 | block_t old_addr, block_t new_addr, |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 3154 | unsigned char version, bool recover_curseg, |
| 3155 | bool recover_newaddr) |
Chao Yu | 528e345 | 2015-05-28 19:15:35 +0800 | [diff] [blame] | 3156 | { |
| 3157 | struct f2fs_summary sum; |
| 3158 | |
| 3159 | set_summary(&sum, dn->nid, dn->ofs_in_node, version); |
| 3160 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 3161 | f2fs_do_replace_block(sbi, &sum, old_addr, new_addr, |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 3162 | recover_curseg, recover_newaddr); |
Chao Yu | 528e345 | 2015-05-28 19:15:35 +0800 | [diff] [blame] | 3163 | |
Chao Yu | f28b343 | 2016-02-24 17:16:47 +0800 | [diff] [blame] | 3164 | f2fs_update_data_blkaddr(dn, new_addr); |
Chao Yu | 528e345 | 2015-05-28 19:15:35 +0800 | [diff] [blame] | 3165 | } |
| 3166 | |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 3167 | void f2fs_wait_on_page_writeback(struct page *page, |
Jaegeuk Kim | fec1d65 | 2016-01-20 23:43:51 +0800 | [diff] [blame] | 3168 | enum page_type type, bool ordered) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 3169 | { |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 3170 | if (PageWriteback(page)) { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 3171 | struct f2fs_sb_info *sbi = F2FS_P_SB(page); |
| 3172 | |
Jaegeuk Kim | b9109b0 | 2017-05-10 11:28:38 -0700 | [diff] [blame] | 3173 | f2fs_submit_merged_write_cond(sbi, page->mapping->host, |
| 3174 | 0, page->index, type); |
Jaegeuk Kim | fec1d65 | 2016-01-20 23:43:51 +0800 | [diff] [blame] | 3175 | if (ordered) |
| 3176 | wait_on_page_writeback(page); |
| 3177 | else |
| 3178 | wait_for_stable_page(page); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 3179 | } |
| 3180 | } |
| 3181 | |
Jaegeuk Kim | d4c759e | 2017-09-05 17:04:35 -0700 | [diff] [blame] | 3182 | void f2fs_wait_on_block_writeback(struct f2fs_sb_info *sbi, block_t blkaddr) |
Chao Yu | 08b39fb | 2015-10-08 13:27:34 +0800 | [diff] [blame] | 3183 | { |
| 3184 | struct page *cpage; |
| 3185 | |
Chao Yu | e1da787 | 2018-06-05 17:44:11 +0800 | [diff] [blame] | 3186 | if (!is_valid_data_blkaddr(sbi, blkaddr)) |
Chao Yu | 08b39fb | 2015-10-08 13:27:34 +0800 | [diff] [blame] | 3187 | return; |
| 3188 | |
Chao Yu | 08b39fb | 2015-10-08 13:27:34 +0800 | [diff] [blame] | 3189 | cpage = find_lock_page(META_MAPPING(sbi), blkaddr); |
| 3190 | if (cpage) { |
Jaegeuk Kim | fec1d65 | 2016-01-20 23:43:51 +0800 | [diff] [blame] | 3191 | f2fs_wait_on_page_writeback(cpage, DATA, true); |
Chao Yu | 08b39fb | 2015-10-08 13:27:34 +0800 | [diff] [blame] | 3192 | f2fs_put_page(cpage, 1); |
| 3193 | } |
| 3194 | } |
| 3195 | |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 3196 | static int read_compacted_summaries(struct f2fs_sb_info *sbi) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3197 | { |
| 3198 | struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); |
| 3199 | struct curseg_info *seg_i; |
| 3200 | unsigned char *kaddr; |
| 3201 | struct page *page; |
| 3202 | block_t start; |
| 3203 | int i, j, offset; |
| 3204 | |
| 3205 | start = start_sum_block(sbi); |
| 3206 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 3207 | page = f2fs_get_meta_page(sbi, start++); |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 3208 | if (IS_ERR(page)) |
| 3209 | return PTR_ERR(page); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3210 | kaddr = (unsigned char *)page_address(page); |
| 3211 | |
| 3212 | /* Step 1: restore nat cache */ |
| 3213 | seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA); |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 3214 | memcpy(seg_i->journal, kaddr, SUM_JOURNAL_SIZE); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3215 | |
| 3216 | /* Step 2: restore sit cache */ |
| 3217 | seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA); |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 3218 | memcpy(seg_i->journal, kaddr + SUM_JOURNAL_SIZE, SUM_JOURNAL_SIZE); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3219 | offset = 2 * SUM_JOURNAL_SIZE; |
| 3220 | |
| 3221 | /* Step 3: restore summary entries */ |
| 3222 | for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) { |
| 3223 | unsigned short blk_off; |
| 3224 | unsigned int segno; |
| 3225 | |
| 3226 | seg_i = CURSEG_I(sbi, i); |
| 3227 | segno = le32_to_cpu(ckpt->cur_data_segno[i]); |
| 3228 | blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]); |
| 3229 | seg_i->next_segno = segno; |
| 3230 | reset_curseg(sbi, i, 0); |
| 3231 | seg_i->alloc_type = ckpt->alloc_type[i]; |
| 3232 | seg_i->next_blkoff = blk_off; |
| 3233 | |
| 3234 | if (seg_i->alloc_type == SSR) |
| 3235 | blk_off = sbi->blocks_per_seg; |
| 3236 | |
| 3237 | for (j = 0; j < blk_off; j++) { |
| 3238 | struct f2fs_summary *s; |
| 3239 | s = (struct f2fs_summary *)(kaddr + offset); |
| 3240 | seg_i->sum_blk->entries[j] = *s; |
| 3241 | offset += SUMMARY_SIZE; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3242 | if (offset + SUMMARY_SIZE <= PAGE_SIZE - |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3243 | SUM_FOOTER_SIZE) |
| 3244 | continue; |
| 3245 | |
| 3246 | f2fs_put_page(page, 1); |
| 3247 | page = NULL; |
| 3248 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 3249 | page = f2fs_get_meta_page(sbi, start++); |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 3250 | if (IS_ERR(page)) |
| 3251 | return PTR_ERR(page); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3252 | kaddr = (unsigned char *)page_address(page); |
| 3253 | offset = 0; |
| 3254 | } |
| 3255 | } |
| 3256 | f2fs_put_page(page, 1); |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 3257 | return 0; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3258 | } |
| 3259 | |
| 3260 | static int read_normal_summaries(struct f2fs_sb_info *sbi, int type) |
| 3261 | { |
| 3262 | struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); |
| 3263 | struct f2fs_summary_block *sum; |
| 3264 | struct curseg_info *curseg; |
| 3265 | struct page *new; |
| 3266 | unsigned short blk_off; |
| 3267 | unsigned int segno = 0; |
| 3268 | block_t blk_addr = 0; |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 3269 | int err = 0; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3270 | |
| 3271 | /* get segment number and block addr */ |
| 3272 | if (IS_DATASEG(type)) { |
| 3273 | segno = le32_to_cpu(ckpt->cur_data_segno[type]); |
| 3274 | blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type - |
| 3275 | CURSEG_HOT_DATA]); |
Jaegeuk Kim | 119ee91 | 2015-01-29 11:45:33 -0800 | [diff] [blame] | 3276 | if (__exist_node_summaries(sbi)) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3277 | blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type); |
| 3278 | else |
| 3279 | blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type); |
| 3280 | } else { |
| 3281 | segno = le32_to_cpu(ckpt->cur_node_segno[type - |
| 3282 | CURSEG_HOT_NODE]); |
| 3283 | blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type - |
| 3284 | CURSEG_HOT_NODE]); |
Jaegeuk Kim | 119ee91 | 2015-01-29 11:45:33 -0800 | [diff] [blame] | 3285 | if (__exist_node_summaries(sbi)) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3286 | blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE, |
| 3287 | type - CURSEG_HOT_NODE); |
| 3288 | else |
| 3289 | blk_addr = GET_SUM_BLOCK(sbi, segno); |
| 3290 | } |
| 3291 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 3292 | new = f2fs_get_meta_page(sbi, blk_addr); |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 3293 | if (IS_ERR(new)) |
| 3294 | return PTR_ERR(new); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3295 | sum = (struct f2fs_summary_block *)page_address(new); |
| 3296 | |
| 3297 | if (IS_NODESEG(type)) { |
Jaegeuk Kim | 119ee91 | 2015-01-29 11:45:33 -0800 | [diff] [blame] | 3298 | if (__exist_node_summaries(sbi)) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3299 | struct f2fs_summary *ns = &sum->entries[0]; |
| 3300 | int i; |
| 3301 | for (i = 0; i < sbi->blocks_per_seg; i++, ns++) { |
| 3302 | ns->version = 0; |
| 3303 | ns->ofs_in_node = 0; |
| 3304 | } |
| 3305 | } else { |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 3306 | err = f2fs_restore_node_summary(sbi, segno, sum); |
| 3307 | if (err) |
| 3308 | goto out; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3309 | } |
| 3310 | } |
| 3311 | |
| 3312 | /* set uncompleted segment to curseg */ |
| 3313 | curseg = CURSEG_I(sbi, type); |
| 3314 | mutex_lock(&curseg->curseg_mutex); |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 3315 | |
| 3316 | /* update journal info */ |
| 3317 | down_write(&curseg->journal_rwsem); |
| 3318 | memcpy(curseg->journal, &sum->journal, SUM_JOURNAL_SIZE); |
| 3319 | up_write(&curseg->journal_rwsem); |
| 3320 | |
| 3321 | memcpy(curseg->sum_blk->entries, sum->entries, SUM_ENTRY_SIZE); |
| 3322 | memcpy(&curseg->sum_blk->footer, &sum->footer, SUM_FOOTER_SIZE); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3323 | curseg->next_segno = segno; |
| 3324 | reset_curseg(sbi, type, 0); |
| 3325 | curseg->alloc_type = ckpt->alloc_type[type]; |
| 3326 | curseg->next_blkoff = blk_off; |
| 3327 | mutex_unlock(&curseg->curseg_mutex); |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 3328 | out: |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3329 | f2fs_put_page(new, 1); |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 3330 | return err; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3331 | } |
| 3332 | |
| 3333 | static int restore_curseg_summaries(struct f2fs_sb_info *sbi) |
| 3334 | { |
Jin Qian | 21d3f8e | 2017-06-01 11:18:30 -0700 | [diff] [blame] | 3335 | struct f2fs_journal *sit_j = CURSEG_I(sbi, CURSEG_COLD_DATA)->journal; |
| 3336 | struct f2fs_journal *nat_j = CURSEG_I(sbi, CURSEG_HOT_DATA)->journal; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3337 | int type = CURSEG_HOT_DATA; |
Chao Yu | e4fc5fb | 2014-03-17 16:36:24 +0800 | [diff] [blame] | 3338 | int err; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3339 | |
Chao Yu | aaec2b1 | 2016-09-20 11:04:18 +0800 | [diff] [blame] | 3340 | if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG)) { |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 3341 | int npages = f2fs_npages_for_summary_flush(sbi, true); |
Chao Yu | 3fa06d7 | 2014-12-09 14:21:46 +0800 | [diff] [blame] | 3342 | |
| 3343 | if (npages >= 2) |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 3344 | f2fs_ra_meta_pages(sbi, start_sum_block(sbi), npages, |
Chao Yu | 26879fb | 2015-10-12 17:05:59 +0800 | [diff] [blame] | 3345 | META_CP, true); |
Chao Yu | 3fa06d7 | 2014-12-09 14:21:46 +0800 | [diff] [blame] | 3346 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3347 | /* restore for compacted data summary */ |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 3348 | err = read_compacted_summaries(sbi); |
| 3349 | if (err) |
| 3350 | return err; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3351 | type = CURSEG_HOT_NODE; |
| 3352 | } |
| 3353 | |
Jaegeuk Kim | 119ee91 | 2015-01-29 11:45:33 -0800 | [diff] [blame] | 3354 | if (__exist_node_summaries(sbi)) |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 3355 | f2fs_ra_meta_pages(sbi, sum_blk_addr(sbi, NR_CURSEG_TYPE, type), |
Chao Yu | 26879fb | 2015-10-12 17:05:59 +0800 | [diff] [blame] | 3356 | NR_CURSEG_TYPE - type, META_CP, true); |
Chao Yu | 3fa06d7 | 2014-12-09 14:21:46 +0800 | [diff] [blame] | 3357 | |
Chao Yu | e4fc5fb | 2014-03-17 16:36:24 +0800 | [diff] [blame] | 3358 | for (; type <= CURSEG_COLD_NODE; type++) { |
| 3359 | err = read_normal_summaries(sbi, type); |
| 3360 | if (err) |
| 3361 | return err; |
| 3362 | } |
| 3363 | |
Jin Qian | 21d3f8e | 2017-06-01 11:18:30 -0700 | [diff] [blame] | 3364 | /* sanity check for summary blocks */ |
| 3365 | if (nats_in_cursum(nat_j) > NAT_JOURNAL_ENTRIES || |
| 3366 | sits_in_cursum(sit_j) > SIT_JOURNAL_ENTRIES) |
| 3367 | return -EINVAL; |
| 3368 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3369 | return 0; |
| 3370 | } |
| 3371 | |
| 3372 | static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr) |
| 3373 | { |
| 3374 | struct page *page; |
| 3375 | unsigned char *kaddr; |
| 3376 | struct f2fs_summary *summary; |
| 3377 | struct curseg_info *seg_i; |
| 3378 | int written_size = 0; |
| 3379 | int i, j; |
| 3380 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 3381 | page = f2fs_grab_meta_page(sbi, blkaddr++); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3382 | kaddr = (unsigned char *)page_address(page); |
Chao Yu | 81114ba | 2018-04-09 20:25:06 +0800 | [diff] [blame] | 3383 | memset(kaddr, 0, PAGE_SIZE); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3384 | |
| 3385 | /* Step 1: write nat cache */ |
| 3386 | seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA); |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 3387 | memcpy(kaddr, seg_i->journal, SUM_JOURNAL_SIZE); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3388 | written_size += SUM_JOURNAL_SIZE; |
| 3389 | |
| 3390 | /* Step 2: write sit cache */ |
| 3391 | seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA); |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 3392 | memcpy(kaddr + written_size, seg_i->journal, SUM_JOURNAL_SIZE); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3393 | written_size += SUM_JOURNAL_SIZE; |
| 3394 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3395 | /* Step 3: write summary entries */ |
| 3396 | for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) { |
| 3397 | unsigned short blkoff; |
| 3398 | seg_i = CURSEG_I(sbi, i); |
| 3399 | if (sbi->ckpt->alloc_type[i] == SSR) |
| 3400 | blkoff = sbi->blocks_per_seg; |
| 3401 | else |
| 3402 | blkoff = curseg_blkoff(sbi, i); |
| 3403 | |
| 3404 | for (j = 0; j < blkoff; j++) { |
| 3405 | if (!page) { |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 3406 | page = f2fs_grab_meta_page(sbi, blkaddr++); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3407 | kaddr = (unsigned char *)page_address(page); |
Chao Yu | 81114ba | 2018-04-09 20:25:06 +0800 | [diff] [blame] | 3408 | memset(kaddr, 0, PAGE_SIZE); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3409 | written_size = 0; |
| 3410 | } |
| 3411 | summary = (struct f2fs_summary *)(kaddr + written_size); |
| 3412 | *summary = seg_i->sum_blk->entries[j]; |
| 3413 | written_size += SUMMARY_SIZE; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3414 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3415 | if (written_size + SUMMARY_SIZE <= PAGE_SIZE - |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3416 | SUM_FOOTER_SIZE) |
| 3417 | continue; |
| 3418 | |
Chao Yu | e8d61a7 | 2013-10-24 15:08:28 +0800 | [diff] [blame] | 3419 | set_page_dirty(page); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3420 | f2fs_put_page(page, 1); |
| 3421 | page = NULL; |
| 3422 | } |
| 3423 | } |
Chao Yu | e8d61a7 | 2013-10-24 15:08:28 +0800 | [diff] [blame] | 3424 | if (page) { |
| 3425 | set_page_dirty(page); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3426 | f2fs_put_page(page, 1); |
Chao Yu | e8d61a7 | 2013-10-24 15:08:28 +0800 | [diff] [blame] | 3427 | } |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3428 | } |
| 3429 | |
| 3430 | static void write_normal_summaries(struct f2fs_sb_info *sbi, |
| 3431 | block_t blkaddr, int type) |
| 3432 | { |
| 3433 | int i, end; |
| 3434 | if (IS_DATASEG(type)) |
| 3435 | end = type + NR_CURSEG_DATA_TYPE; |
| 3436 | else |
| 3437 | end = type + NR_CURSEG_NODE_TYPE; |
| 3438 | |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 3439 | for (i = type; i < end; i++) |
| 3440 | write_current_sum_page(sbi, i, blkaddr + (i - type)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3441 | } |
| 3442 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 3443 | 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] | 3444 | { |
Chao Yu | aaec2b1 | 2016-09-20 11:04:18 +0800 | [diff] [blame] | 3445 | if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG)) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3446 | write_compacted_summaries(sbi, start_blk); |
| 3447 | else |
| 3448 | write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA); |
| 3449 | } |
| 3450 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 3451 | 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] | 3452 | { |
Jaegeuk Kim | 119ee91 | 2015-01-29 11:45:33 -0800 | [diff] [blame] | 3453 | write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3454 | } |
| 3455 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 3456 | int f2fs_lookup_journal_in_cursum(struct f2fs_journal *journal, int type, |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3457 | unsigned int val, int alloc) |
| 3458 | { |
| 3459 | int i; |
| 3460 | |
| 3461 | if (type == NAT_JOURNAL) { |
Chao Yu | dfc08a1 | 2016-02-14 18:50:40 +0800 | [diff] [blame] | 3462 | for (i = 0; i < nats_in_cursum(journal); i++) { |
| 3463 | if (le32_to_cpu(nid_in_journal(journal, i)) == val) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3464 | return i; |
| 3465 | } |
Chao Yu | dfc08a1 | 2016-02-14 18:50:40 +0800 | [diff] [blame] | 3466 | if (alloc && __has_cursum_space(journal, 1, NAT_JOURNAL)) |
| 3467 | return update_nats_in_cursum(journal, 1); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3468 | } else if (type == SIT_JOURNAL) { |
Chao Yu | dfc08a1 | 2016-02-14 18:50:40 +0800 | [diff] [blame] | 3469 | for (i = 0; i < sits_in_cursum(journal); i++) |
| 3470 | if (le32_to_cpu(segno_in_journal(journal, i)) == val) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3471 | return i; |
Chao Yu | dfc08a1 | 2016-02-14 18:50:40 +0800 | [diff] [blame] | 3472 | if (alloc && __has_cursum_space(journal, 1, SIT_JOURNAL)) |
| 3473 | return update_sits_in_cursum(journal, 1); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3474 | } |
| 3475 | return -1; |
| 3476 | } |
| 3477 | |
| 3478 | static struct page *get_current_sit_page(struct f2fs_sb_info *sbi, |
| 3479 | unsigned int segno) |
| 3480 | { |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 3481 | return f2fs_get_meta_page_nofail(sbi, current_sit_addr(sbi, segno)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3482 | } |
| 3483 | |
| 3484 | static struct page *get_next_sit_page(struct f2fs_sb_info *sbi, |
| 3485 | unsigned int start) |
| 3486 | { |
| 3487 | struct sit_info *sit_i = SIT_I(sbi); |
Yunlei He | 068c3cd | 2018-01-25 17:27:11 +0800 | [diff] [blame] | 3488 | struct page *page; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3489 | pgoff_t src_off, dst_off; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3490 | |
| 3491 | src_off = current_sit_addr(sbi, start); |
| 3492 | dst_off = next_sit_addr(sbi, src_off); |
| 3493 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 3494 | page = f2fs_grab_meta_page(sbi, dst_off); |
Yunlei He | 068c3cd | 2018-01-25 17:27:11 +0800 | [diff] [blame] | 3495 | seg_info_to_sit_page(sbi, page, start); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3496 | |
Yunlei He | 068c3cd | 2018-01-25 17:27:11 +0800 | [diff] [blame] | 3497 | set_page_dirty(page); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3498 | set_to_next_sit(sit_i, start); |
| 3499 | |
Yunlei He | 068c3cd | 2018-01-25 17:27:11 +0800 | [diff] [blame] | 3500 | return page; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3501 | } |
| 3502 | |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 3503 | static struct sit_entry_set *grab_sit_entry_set(void) |
| 3504 | { |
| 3505 | struct sit_entry_set *ses = |
Jaegeuk Kim | 80c5450 | 2015-08-20 08:51:56 -0700 | [diff] [blame] | 3506 | f2fs_kmem_cache_alloc(sit_entry_set_slab, GFP_NOFS); |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 3507 | |
| 3508 | ses->entry_cnt = 0; |
| 3509 | INIT_LIST_HEAD(&ses->set_list); |
| 3510 | return ses; |
| 3511 | } |
| 3512 | |
| 3513 | static void release_sit_entry_set(struct sit_entry_set *ses) |
| 3514 | { |
| 3515 | list_del(&ses->set_list); |
| 3516 | kmem_cache_free(sit_entry_set_slab, ses); |
| 3517 | } |
| 3518 | |
| 3519 | static void adjust_sit_entry_set(struct sit_entry_set *ses, |
| 3520 | struct list_head *head) |
| 3521 | { |
| 3522 | struct sit_entry_set *next = ses; |
| 3523 | |
| 3524 | if (list_is_last(&ses->set_list, head)) |
| 3525 | return; |
| 3526 | |
| 3527 | list_for_each_entry_continue(next, head, set_list) |
| 3528 | if (ses->entry_cnt <= next->entry_cnt) |
| 3529 | break; |
| 3530 | |
| 3531 | list_move_tail(&ses->set_list, &next->set_list); |
| 3532 | } |
| 3533 | |
| 3534 | static void add_sit_entry(unsigned int segno, struct list_head *head) |
| 3535 | { |
| 3536 | struct sit_entry_set *ses; |
| 3537 | unsigned int start_segno = START_SEGNO(segno); |
| 3538 | |
| 3539 | list_for_each_entry(ses, head, set_list) { |
| 3540 | if (ses->start_segno == start_segno) { |
| 3541 | ses->entry_cnt++; |
| 3542 | adjust_sit_entry_set(ses, head); |
| 3543 | return; |
| 3544 | } |
| 3545 | } |
| 3546 | |
| 3547 | ses = grab_sit_entry_set(); |
| 3548 | |
| 3549 | ses->start_segno = start_segno; |
| 3550 | ses->entry_cnt++; |
| 3551 | list_add(&ses->set_list, head); |
| 3552 | } |
| 3553 | |
| 3554 | static void add_sits_in_set(struct f2fs_sb_info *sbi) |
| 3555 | { |
| 3556 | struct f2fs_sm_info *sm_info = SM_I(sbi); |
| 3557 | struct list_head *set_list = &sm_info->sit_entry_set; |
| 3558 | unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap; |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 3559 | unsigned int segno; |
| 3560 | |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 3561 | for_each_set_bit(segno, bitmap, MAIN_SEGS(sbi)) |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 3562 | add_sit_entry(segno, set_list); |
| 3563 | } |
| 3564 | |
| 3565 | static void remove_sits_in_journal(struct f2fs_sb_info *sbi) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3566 | { |
| 3567 | struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA); |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 3568 | struct f2fs_journal *journal = curseg->journal; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3569 | int i; |
| 3570 | |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 3571 | down_write(&curseg->journal_rwsem); |
Chao Yu | dfc08a1 | 2016-02-14 18:50:40 +0800 | [diff] [blame] | 3572 | for (i = 0; i < sits_in_cursum(journal); i++) { |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 3573 | unsigned int segno; |
| 3574 | bool dirtied; |
| 3575 | |
Chao Yu | dfc08a1 | 2016-02-14 18:50:40 +0800 | [diff] [blame] | 3576 | segno = le32_to_cpu(segno_in_journal(journal, i)); |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 3577 | dirtied = __mark_sit_entry_dirty(sbi, segno); |
| 3578 | |
| 3579 | if (!dirtied) |
| 3580 | add_sit_entry(segno, &SM_I(sbi)->sit_entry_set); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3581 | } |
Chao Yu | dfc08a1 | 2016-02-14 18:50:40 +0800 | [diff] [blame] | 3582 | update_sits_in_cursum(journal, -i); |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 3583 | up_write(&curseg->journal_rwsem); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3584 | } |
| 3585 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 3586 | /* |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3587 | * CP calls this function, which flushes SIT entries including sit_journal, |
| 3588 | * and moves prefree segs to free segs. |
| 3589 | */ |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 3590 | 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] | 3591 | { |
| 3592 | struct sit_info *sit_i = SIT_I(sbi); |
| 3593 | unsigned long *bitmap = sit_i->dirty_sentries_bitmap; |
| 3594 | struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA); |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 3595 | struct f2fs_journal *journal = curseg->journal; |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 3596 | struct sit_entry_set *ses, *tmp; |
| 3597 | struct list_head *head = &SM_I(sbi)->sit_entry_set; |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 3598 | bool to_journal = true; |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 3599 | struct seg_entry *se; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3600 | |
Chao Yu | 3d26fa6 | 2017-10-30 17:49:53 +0800 | [diff] [blame] | 3601 | down_write(&sit_i->sentry_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3602 | |
Wanpeng Li | 2b11a74 | 2015-02-27 16:52:50 +0800 | [diff] [blame] | 3603 | if (!sit_i->dirty_sentries) |
| 3604 | goto out; |
| 3605 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3606 | /* |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 3607 | * add and account sit entries of dirty bitmap in sit entry |
| 3608 | * set temporarily |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3609 | */ |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 3610 | add_sits_in_set(sbi); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3611 | |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 3612 | /* |
| 3613 | * if there are no enough space in journal to store dirty sit |
| 3614 | * entries, remove all entries from journal and add and account |
| 3615 | * them in sit entry set. |
| 3616 | */ |
Chao Yu | dfc08a1 | 2016-02-14 18:50:40 +0800 | [diff] [blame] | 3617 | if (!__has_cursum_space(journal, sit_i->dirty_sentries, SIT_JOURNAL)) |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 3618 | remove_sits_in_journal(sbi); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3619 | |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 3620 | /* |
| 3621 | * there are two steps to flush sit entries: |
| 3622 | * #1, flush sit entries to journal in current cold data summary block. |
| 3623 | * #2, flush sit entries to sit page. |
| 3624 | */ |
| 3625 | list_for_each_entry_safe(ses, tmp, head, set_list) { |
Jaegeuk Kim | 4a257ed | 2014-10-16 11:43:30 -0700 | [diff] [blame] | 3626 | struct page *page = NULL; |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 3627 | struct f2fs_sit_block *raw_sit = NULL; |
| 3628 | unsigned int start_segno = ses->start_segno; |
| 3629 | unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK, |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 3630 | (unsigned long)MAIN_SEGS(sbi)); |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 3631 | unsigned int segno = start_segno; |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 3632 | |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 3633 | if (to_journal && |
Chao Yu | dfc08a1 | 2016-02-14 18:50:40 +0800 | [diff] [blame] | 3634 | !__has_cursum_space(journal, ses->entry_cnt, SIT_JOURNAL)) |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 3635 | to_journal = false; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3636 | |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 3637 | if (to_journal) { |
| 3638 | down_write(&curseg->journal_rwsem); |
| 3639 | } else { |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 3640 | page = get_next_sit_page(sbi, start_segno); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3641 | raw_sit = page_address(page); |
| 3642 | } |
| 3643 | |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 3644 | /* flush dirty sit entries in region of current sit set */ |
| 3645 | for_each_set_bit_from(segno, bitmap, end) { |
| 3646 | int offset, sit_offset; |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 3647 | |
| 3648 | se = get_seg_entry(sbi, segno); |
Zhikang Zhang | 56b07e7 | 2018-04-09 04:28:41 +0800 | [diff] [blame] | 3649 | #ifdef CONFIG_F2FS_CHECK_FS |
| 3650 | if (memcmp(se->cur_valid_map, se->cur_valid_map_mir, |
| 3651 | SIT_VBLOCK_MAP_SIZE)) |
| 3652 | f2fs_bug_on(sbi, 1); |
| 3653 | #endif |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 3654 | |
| 3655 | /* add discard candidates */ |
Chao Yu | c473f1a | 2017-04-27 20:40:39 +0800 | [diff] [blame] | 3656 | if (!(cpc->reason & CP_DISCARD)) { |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 3657 | cpc->trim_start = segno; |
Jaegeuk Kim | 25290fa | 2016-12-29 22:06:15 -0800 | [diff] [blame] | 3658 | add_discard_addrs(sbi, cpc, false); |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 3659 | } |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 3660 | |
| 3661 | if (to_journal) { |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 3662 | offset = f2fs_lookup_journal_in_cursum(journal, |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 3663 | SIT_JOURNAL, segno, 1); |
| 3664 | f2fs_bug_on(sbi, offset < 0); |
Chao Yu | dfc08a1 | 2016-02-14 18:50:40 +0800 | [diff] [blame] | 3665 | segno_in_journal(journal, offset) = |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 3666 | cpu_to_le32(segno); |
| 3667 | seg_info_to_raw_sit(se, |
Chao Yu | dfc08a1 | 2016-02-14 18:50:40 +0800 | [diff] [blame] | 3668 | &sit_in_journal(journal, offset)); |
Zhikang Zhang | 56b07e7 | 2018-04-09 04:28:41 +0800 | [diff] [blame] | 3669 | check_block_count(sbi, segno, |
| 3670 | &sit_in_journal(journal, offset)); |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 3671 | } else { |
| 3672 | sit_offset = SIT_ENTRY_OFFSET(sit_i, segno); |
| 3673 | seg_info_to_raw_sit(se, |
| 3674 | &raw_sit->entries[sit_offset]); |
Zhikang Zhang | 56b07e7 | 2018-04-09 04:28:41 +0800 | [diff] [blame] | 3675 | check_block_count(sbi, segno, |
| 3676 | &raw_sit->entries[sit_offset]); |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 3677 | } |
| 3678 | |
| 3679 | __clear_bit(segno, bitmap); |
| 3680 | sit_i->dirty_sentries--; |
| 3681 | ses->entry_cnt--; |
| 3682 | } |
| 3683 | |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 3684 | if (to_journal) |
| 3685 | up_write(&curseg->journal_rwsem); |
| 3686 | else |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 3687 | f2fs_put_page(page, 1); |
| 3688 | |
| 3689 | f2fs_bug_on(sbi, ses->entry_cnt); |
| 3690 | release_sit_entry_set(ses); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3691 | } |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 3692 | |
| 3693 | f2fs_bug_on(sbi, !list_empty(head)); |
| 3694 | f2fs_bug_on(sbi, sit_i->dirty_sentries); |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 3695 | out: |
Chao Yu | c473f1a | 2017-04-27 20:40:39 +0800 | [diff] [blame] | 3696 | if (cpc->reason & CP_DISCARD) { |
Yunlei He | 650d3c4 | 2016-12-22 11:46:24 +0800 | [diff] [blame] | 3697 | __u64 trim_start = cpc->trim_start; |
| 3698 | |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 3699 | for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++) |
Jaegeuk Kim | 25290fa | 2016-12-29 22:06:15 -0800 | [diff] [blame] | 3700 | add_discard_addrs(sbi, cpc, false); |
Yunlei He | 650d3c4 | 2016-12-22 11:46:24 +0800 | [diff] [blame] | 3701 | |
| 3702 | cpc->trim_start = trim_start; |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 3703 | } |
Chao Yu | 3d26fa6 | 2017-10-30 17:49:53 +0800 | [diff] [blame] | 3704 | up_write(&sit_i->sentry_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3705 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3706 | set_prefree_as_free_segments(sbi); |
| 3707 | } |
| 3708 | |
| 3709 | static int build_sit_info(struct f2fs_sb_info *sbi) |
| 3710 | { |
| 3711 | struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3712 | struct sit_info *sit_i; |
| 3713 | unsigned int sit_segs, start; |
Chao Yu | ae27d62 | 2017-01-07 18:52:34 +0800 | [diff] [blame] | 3714 | char *src_bitmap; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3715 | unsigned int bitmap_size; |
| 3716 | |
| 3717 | /* allocate memory for SIT information */ |
Chao Yu | acbf054 | 2017-11-30 19:28:17 +0800 | [diff] [blame] | 3718 | sit_i = f2fs_kzalloc(sbi, sizeof(struct sit_info), GFP_KERNEL); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3719 | if (!sit_i) |
| 3720 | return -ENOMEM; |
| 3721 | |
| 3722 | SM_I(sbi)->sit_info = sit_i; |
| 3723 | |
Kees Cook | 9d2a789 | 2018-06-12 14:28:35 -0700 | [diff] [blame] | 3724 | sit_i->sentries = |
| 3725 | f2fs_kvzalloc(sbi, array_size(sizeof(struct seg_entry), |
| 3726 | MAIN_SEGS(sbi)), |
| 3727 | GFP_KERNEL); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3728 | if (!sit_i->sentries) |
| 3729 | return -ENOMEM; |
| 3730 | |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 3731 | bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi)); |
Chao Yu | 628b3d1 | 2017-11-30 19:28:18 +0800 | [diff] [blame] | 3732 | sit_i->dirty_sentries_bitmap = f2fs_kvzalloc(sbi, bitmap_size, |
| 3733 | GFP_KERNEL); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3734 | if (!sit_i->dirty_sentries_bitmap) |
| 3735 | return -ENOMEM; |
| 3736 | |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 3737 | for (start = 0; start < MAIN_SEGS(sbi); start++) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3738 | sit_i->sentries[start].cur_valid_map |
Chao Yu | acbf054 | 2017-11-30 19:28:17 +0800 | [diff] [blame] | 3739 | = f2fs_kzalloc(sbi, SIT_VBLOCK_MAP_SIZE, GFP_KERNEL); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3740 | sit_i->sentries[start].ckpt_valid_map |
Chao Yu | acbf054 | 2017-11-30 19:28:17 +0800 | [diff] [blame] | 3741 | = f2fs_kzalloc(sbi, SIT_VBLOCK_MAP_SIZE, GFP_KERNEL); |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 3742 | if (!sit_i->sentries[start].cur_valid_map || |
Jaegeuk Kim | 3e02574 | 2016-08-02 10:56:40 -0700 | [diff] [blame] | 3743 | !sit_i->sentries[start].ckpt_valid_map) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3744 | return -ENOMEM; |
Jaegeuk Kim | 3e02574 | 2016-08-02 10:56:40 -0700 | [diff] [blame] | 3745 | |
Chao Yu | 355e789 | 2017-01-07 18:51:01 +0800 | [diff] [blame] | 3746 | #ifdef CONFIG_F2FS_CHECK_FS |
| 3747 | sit_i->sentries[start].cur_valid_map_mir |
Chao Yu | acbf054 | 2017-11-30 19:28:17 +0800 | [diff] [blame] | 3748 | = f2fs_kzalloc(sbi, SIT_VBLOCK_MAP_SIZE, GFP_KERNEL); |
Chao Yu | 355e789 | 2017-01-07 18:51:01 +0800 | [diff] [blame] | 3749 | if (!sit_i->sentries[start].cur_valid_map_mir) |
| 3750 | return -ENOMEM; |
| 3751 | #endif |
| 3752 | |
Jaegeuk Kim | 3e02574 | 2016-08-02 10:56:40 -0700 | [diff] [blame] | 3753 | if (f2fs_discard_en(sbi)) { |
| 3754 | sit_i->sentries[start].discard_map |
Chao Yu | acbf054 | 2017-11-30 19:28:17 +0800 | [diff] [blame] | 3755 | = f2fs_kzalloc(sbi, SIT_VBLOCK_MAP_SIZE, |
| 3756 | GFP_KERNEL); |
Jaegeuk Kim | 3e02574 | 2016-08-02 10:56:40 -0700 | [diff] [blame] | 3757 | if (!sit_i->sentries[start].discard_map) |
| 3758 | return -ENOMEM; |
| 3759 | } |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3760 | } |
| 3761 | |
Chao Yu | acbf054 | 2017-11-30 19:28:17 +0800 | [diff] [blame] | 3762 | 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] | 3763 | if (!sit_i->tmp_map) |
| 3764 | return -ENOMEM; |
| 3765 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3766 | if (sbi->segs_per_sec > 1) { |
Kees Cook | 9d2a789 | 2018-06-12 14:28:35 -0700 | [diff] [blame] | 3767 | sit_i->sec_entries = |
| 3768 | f2fs_kvzalloc(sbi, array_size(sizeof(struct sec_entry), |
| 3769 | MAIN_SECS(sbi)), |
| 3770 | GFP_KERNEL); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3771 | if (!sit_i->sec_entries) |
| 3772 | return -ENOMEM; |
| 3773 | } |
| 3774 | |
| 3775 | /* get information related with SIT */ |
| 3776 | sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1; |
| 3777 | |
| 3778 | /* setup SIT bitmap from ckeckpoint pack */ |
| 3779 | bitmap_size = __bitmap_size(sbi, SIT_BITMAP); |
| 3780 | src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP); |
| 3781 | |
Chao Yu | ae27d62 | 2017-01-07 18:52:34 +0800 | [diff] [blame] | 3782 | sit_i->sit_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL); |
| 3783 | if (!sit_i->sit_bitmap) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3784 | return -ENOMEM; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3785 | |
Chao Yu | ae27d62 | 2017-01-07 18:52:34 +0800 | [diff] [blame] | 3786 | #ifdef CONFIG_F2FS_CHECK_FS |
| 3787 | sit_i->sit_bitmap_mir = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL); |
| 3788 | if (!sit_i->sit_bitmap_mir) |
| 3789 | return -ENOMEM; |
| 3790 | #endif |
| 3791 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3792 | /* init SIT information */ |
| 3793 | sit_i->s_ops = &default_salloc_ops; |
| 3794 | |
| 3795 | sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr); |
| 3796 | sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg; |
Jaegeuk Kim | c79b7ff | 2016-11-14 18:20:10 -0800 | [diff] [blame] | 3797 | sit_i->written_valid_blocks = 0; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3798 | sit_i->bitmap_size = bitmap_size; |
| 3799 | sit_i->dirty_sentries = 0; |
| 3800 | sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK; |
| 3801 | sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time); |
Deepa Dinamani | 48fbfe5 | 2017-05-08 15:59:10 -0700 | [diff] [blame] | 3802 | sit_i->mounted_time = ktime_get_real_seconds(); |
Chao Yu | 3d26fa6 | 2017-10-30 17:49:53 +0800 | [diff] [blame] | 3803 | init_rwsem(&sit_i->sentry_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3804 | return 0; |
| 3805 | } |
| 3806 | |
| 3807 | static int build_free_segmap(struct f2fs_sb_info *sbi) |
| 3808 | { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3809 | struct free_segmap_info *free_i; |
| 3810 | unsigned int bitmap_size, sec_bitmap_size; |
| 3811 | |
| 3812 | /* allocate memory for free segmap information */ |
Chao Yu | acbf054 | 2017-11-30 19:28:17 +0800 | [diff] [blame] | 3813 | free_i = f2fs_kzalloc(sbi, sizeof(struct free_segmap_info), GFP_KERNEL); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3814 | if (!free_i) |
| 3815 | return -ENOMEM; |
| 3816 | |
| 3817 | SM_I(sbi)->free_info = free_i; |
| 3818 | |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 3819 | bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi)); |
Chao Yu | 628b3d1 | 2017-11-30 19:28:18 +0800 | [diff] [blame] | 3820 | free_i->free_segmap = f2fs_kvmalloc(sbi, bitmap_size, GFP_KERNEL); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3821 | if (!free_i->free_segmap) |
| 3822 | return -ENOMEM; |
| 3823 | |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 3824 | sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi)); |
Chao Yu | 628b3d1 | 2017-11-30 19:28:18 +0800 | [diff] [blame] | 3825 | free_i->free_secmap = f2fs_kvmalloc(sbi, sec_bitmap_size, GFP_KERNEL); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3826 | if (!free_i->free_secmap) |
| 3827 | return -ENOMEM; |
| 3828 | |
| 3829 | /* set all segments as dirty temporarily */ |
| 3830 | memset(free_i->free_segmap, 0xff, bitmap_size); |
| 3831 | memset(free_i->free_secmap, 0xff, sec_bitmap_size); |
| 3832 | |
| 3833 | /* init free segmap information */ |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 3834 | free_i->start_segno = GET_SEGNO_FROM_SEG0(sbi, MAIN_BLKADDR(sbi)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3835 | free_i->free_segments = 0; |
| 3836 | free_i->free_sections = 0; |
Chao Yu | 1a118cc | 2015-02-11 18:20:38 +0800 | [diff] [blame] | 3837 | spin_lock_init(&free_i->segmap_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3838 | return 0; |
| 3839 | } |
| 3840 | |
| 3841 | static int build_curseg(struct f2fs_sb_info *sbi) |
| 3842 | { |
Namjae Jeon | 1042d60 | 2012-12-01 10:56:13 +0900 | [diff] [blame] | 3843 | struct curseg_info *array; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3844 | int i; |
| 3845 | |
Kees Cook | 026f050 | 2018-06-12 14:28:23 -0700 | [diff] [blame] | 3846 | array = f2fs_kzalloc(sbi, array_size(NR_CURSEG_TYPE, sizeof(*array)), |
| 3847 | GFP_KERNEL); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3848 | if (!array) |
| 3849 | return -ENOMEM; |
| 3850 | |
| 3851 | SM_I(sbi)->curseg_array = array; |
| 3852 | |
| 3853 | for (i = 0; i < NR_CURSEG_TYPE; i++) { |
| 3854 | mutex_init(&array[i].curseg_mutex); |
Chao Yu | acbf054 | 2017-11-30 19:28:17 +0800 | [diff] [blame] | 3855 | array[i].sum_blk = f2fs_kzalloc(sbi, PAGE_SIZE, GFP_KERNEL); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3856 | if (!array[i].sum_blk) |
| 3857 | return -ENOMEM; |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 3858 | init_rwsem(&array[i].journal_rwsem); |
Chao Yu | acbf054 | 2017-11-30 19:28:17 +0800 | [diff] [blame] | 3859 | array[i].journal = f2fs_kzalloc(sbi, |
| 3860 | sizeof(struct f2fs_journal), GFP_KERNEL); |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 3861 | if (!array[i].journal) |
| 3862 | return -ENOMEM; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3863 | array[i].segno = NULL_SEGNO; |
| 3864 | array[i].next_blkoff = 0; |
| 3865 | } |
| 3866 | return restore_curseg_summaries(sbi); |
| 3867 | } |
| 3868 | |
Jaegeuk Kim | c39a1b3 | 2017-12-19 19:16:34 -0800 | [diff] [blame] | 3869 | static int build_sit_entries(struct f2fs_sb_info *sbi) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3870 | { |
| 3871 | struct sit_info *sit_i = SIT_I(sbi); |
| 3872 | struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA); |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 3873 | struct f2fs_journal *journal = curseg->journal; |
Yunlei He | 9c09404 | 2016-09-24 12:29:18 +0800 | [diff] [blame] | 3874 | struct seg_entry *se; |
| 3875 | struct f2fs_sit_entry sit; |
Chao Yu | 74de593 | 2013-11-22 09:09:59 +0800 | [diff] [blame] | 3876 | int sit_blk_cnt = SIT_BLK_CNT(sbi); |
| 3877 | unsigned int i, start, end; |
| 3878 | unsigned int readed, start_blk = 0; |
Jaegeuk Kim | c39a1b3 | 2017-12-19 19:16:34 -0800 | [diff] [blame] | 3879 | int err = 0; |
Jaegeuk Kim | 8a29c12 | 2018-04-24 21:34:05 -0600 | [diff] [blame] | 3880 | block_t total_node_blocks = 0; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3881 | |
Chao Yu | 74de593 | 2013-11-22 09:09:59 +0800 | [diff] [blame] | 3882 | do { |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 3883 | readed = f2fs_ra_meta_pages(sbi, start_blk, BIO_MAX_PAGES, |
Jaegeuk Kim | 664ba97 | 2016-10-18 11:07:45 -0700 | [diff] [blame] | 3884 | META_SIT, true); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3885 | |
Chao Yu | 74de593 | 2013-11-22 09:09:59 +0800 | [diff] [blame] | 3886 | start = start_blk * sit_i->sents_per_block; |
| 3887 | end = (start_blk + readed) * sit_i->sents_per_block; |
| 3888 | |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 3889 | for (; start < end && start < MAIN_SEGS(sbi); start++) { |
Chao Yu | 74de593 | 2013-11-22 09:09:59 +0800 | [diff] [blame] | 3890 | struct f2fs_sit_block *sit_blk; |
Chao Yu | 74de593 | 2013-11-22 09:09:59 +0800 | [diff] [blame] | 3891 | struct page *page; |
| 3892 | |
Yunlei He | 9c09404 | 2016-09-24 12:29:18 +0800 | [diff] [blame] | 3893 | se = &sit_i->sentries[start]; |
Chao Yu | 74de593 | 2013-11-22 09:09:59 +0800 | [diff] [blame] | 3894 | page = get_current_sit_page(sbi, start); |
| 3895 | sit_blk = (struct f2fs_sit_block *)page_address(page); |
| 3896 | sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)]; |
| 3897 | f2fs_put_page(page, 1); |
Chao Yu | d600af23 | 2016-08-19 23:13:47 +0800 | [diff] [blame] | 3898 | |
Jaegeuk Kim | c39a1b3 | 2017-12-19 19:16:34 -0800 | [diff] [blame] | 3899 | err = check_block_count(sbi, start, &sit); |
| 3900 | if (err) |
| 3901 | return err; |
Chao Yu | 74de593 | 2013-11-22 09:09:59 +0800 | [diff] [blame] | 3902 | seg_info_from_raw_sit(se, &sit); |
Jaegeuk Kim | 8a29c12 | 2018-04-24 21:34:05 -0600 | [diff] [blame] | 3903 | if (IS_NODESEG(se->type)) |
| 3904 | total_node_blocks += se->valid_blocks; |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 3905 | |
| 3906 | /* build discard map only one time */ |
Jaegeuk Kim | 3e02574 | 2016-08-02 10:56:40 -0700 | [diff] [blame] | 3907 | if (f2fs_discard_en(sbi)) { |
Chao Yu | 1f43e2a | 2017-04-28 13:56:08 +0800 | [diff] [blame] | 3908 | if (is_set_ckpt_flags(sbi, CP_TRIMMED_FLAG)) { |
| 3909 | memset(se->discard_map, 0xff, |
| 3910 | SIT_VBLOCK_MAP_SIZE); |
| 3911 | } else { |
| 3912 | memcpy(se->discard_map, |
| 3913 | se->cur_valid_map, |
| 3914 | SIT_VBLOCK_MAP_SIZE); |
| 3915 | sbi->discard_blks += |
| 3916 | sbi->blocks_per_seg - |
| 3917 | se->valid_blocks; |
| 3918 | } |
Jaegeuk Kim | 3e02574 | 2016-08-02 10:56:40 -0700 | [diff] [blame] | 3919 | } |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 3920 | |
Chao Yu | d600af23 | 2016-08-19 23:13:47 +0800 | [diff] [blame] | 3921 | if (sbi->segs_per_sec > 1) |
| 3922 | get_sec_entry(sbi, start)->valid_blocks += |
| 3923 | se->valid_blocks; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3924 | } |
Chao Yu | 74de593 | 2013-11-22 09:09:59 +0800 | [diff] [blame] | 3925 | start_blk += readed; |
| 3926 | } while (start_blk < sit_blk_cnt); |
Chao Yu | d600af23 | 2016-08-19 23:13:47 +0800 | [diff] [blame] | 3927 | |
| 3928 | down_read(&curseg->journal_rwsem); |
| 3929 | for (i = 0; i < sits_in_cursum(journal); i++) { |
Chao Yu | d600af23 | 2016-08-19 23:13:47 +0800 | [diff] [blame] | 3930 | unsigned int old_valid_blocks; |
| 3931 | |
| 3932 | start = le32_to_cpu(segno_in_journal(journal, i)); |
Jaegeuk Kim | b2ca374 | 2018-04-24 15:44:16 -0600 | [diff] [blame] | 3933 | if (start >= MAIN_SEGS(sbi)) { |
| 3934 | f2fs_msg(sbi->sb, KERN_ERR, |
| 3935 | "Wrong journal entry on segno %u", |
| 3936 | start); |
| 3937 | set_sbi_flag(sbi, SBI_NEED_FSCK); |
| 3938 | err = -EINVAL; |
| 3939 | break; |
| 3940 | } |
| 3941 | |
Chao Yu | d600af23 | 2016-08-19 23:13:47 +0800 | [diff] [blame] | 3942 | se = &sit_i->sentries[start]; |
| 3943 | sit = sit_in_journal(journal, i); |
| 3944 | |
| 3945 | old_valid_blocks = se->valid_blocks; |
Jaegeuk Kim | 8a29c12 | 2018-04-24 21:34:05 -0600 | [diff] [blame] | 3946 | if (IS_NODESEG(se->type)) |
| 3947 | total_node_blocks -= old_valid_blocks; |
Chao Yu | d600af23 | 2016-08-19 23:13:47 +0800 | [diff] [blame] | 3948 | |
Jaegeuk Kim | c39a1b3 | 2017-12-19 19:16:34 -0800 | [diff] [blame] | 3949 | err = check_block_count(sbi, start, &sit); |
| 3950 | if (err) |
| 3951 | break; |
Chao Yu | d600af23 | 2016-08-19 23:13:47 +0800 | [diff] [blame] | 3952 | seg_info_from_raw_sit(se, &sit); |
Jaegeuk Kim | 8a29c12 | 2018-04-24 21:34:05 -0600 | [diff] [blame] | 3953 | if (IS_NODESEG(se->type)) |
| 3954 | total_node_blocks += se->valid_blocks; |
Chao Yu | d600af23 | 2016-08-19 23:13:47 +0800 | [diff] [blame] | 3955 | |
| 3956 | if (f2fs_discard_en(sbi)) { |
Chao Yu | 1f43e2a | 2017-04-28 13:56:08 +0800 | [diff] [blame] | 3957 | if (is_set_ckpt_flags(sbi, CP_TRIMMED_FLAG)) { |
| 3958 | memset(se->discard_map, 0xff, |
| 3959 | SIT_VBLOCK_MAP_SIZE); |
| 3960 | } else { |
| 3961 | memcpy(se->discard_map, se->cur_valid_map, |
| 3962 | SIT_VBLOCK_MAP_SIZE); |
Chao Yu | a9af3fd | 2018-04-25 19:38:17 +0800 | [diff] [blame] | 3963 | sbi->discard_blks += old_valid_blocks; |
| 3964 | sbi->discard_blks -= se->valid_blocks; |
Chao Yu | 1f43e2a | 2017-04-28 13:56:08 +0800 | [diff] [blame] | 3965 | } |
Chao Yu | d600af23 | 2016-08-19 23:13:47 +0800 | [diff] [blame] | 3966 | } |
| 3967 | |
Chao Yu | a9af3fd | 2018-04-25 19:38:17 +0800 | [diff] [blame] | 3968 | if (sbi->segs_per_sec > 1) { |
Chao Yu | d600af23 | 2016-08-19 23:13:47 +0800 | [diff] [blame] | 3969 | get_sec_entry(sbi, start)->valid_blocks += |
Chao Yu | a9af3fd | 2018-04-25 19:38:17 +0800 | [diff] [blame] | 3970 | se->valid_blocks; |
| 3971 | get_sec_entry(sbi, start)->valid_blocks -= |
| 3972 | old_valid_blocks; |
| 3973 | } |
Chao Yu | d600af23 | 2016-08-19 23:13:47 +0800 | [diff] [blame] | 3974 | } |
| 3975 | up_read(&curseg->journal_rwsem); |
Jaegeuk Kim | 8a29c12 | 2018-04-24 21:34:05 -0600 | [diff] [blame] | 3976 | |
| 3977 | if (!err && total_node_blocks != valid_node_count(sbi)) { |
| 3978 | f2fs_msg(sbi->sb, KERN_ERR, |
| 3979 | "SIT is corrupted node# %u vs %u", |
| 3980 | total_node_blocks, valid_node_count(sbi)); |
| 3981 | set_sbi_flag(sbi, SBI_NEED_FSCK); |
| 3982 | err = -EINVAL; |
| 3983 | } |
| 3984 | |
Jaegeuk Kim | c39a1b3 | 2017-12-19 19:16:34 -0800 | [diff] [blame] | 3985 | return err; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3986 | } |
| 3987 | |
| 3988 | static void init_free_segmap(struct f2fs_sb_info *sbi) |
| 3989 | { |
| 3990 | unsigned int start; |
| 3991 | int type; |
| 3992 | |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 3993 | for (start = 0; start < MAIN_SEGS(sbi); start++) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3994 | struct seg_entry *sentry = get_seg_entry(sbi, start); |
| 3995 | if (!sentry->valid_blocks) |
| 3996 | __set_free(sbi, start); |
Jaegeuk Kim | c79b7ff | 2016-11-14 18:20:10 -0800 | [diff] [blame] | 3997 | else |
| 3998 | SIT_I(sbi)->written_valid_blocks += |
| 3999 | sentry->valid_blocks; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4000 | } |
| 4001 | |
| 4002 | /* set use the current segments */ |
| 4003 | for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) { |
| 4004 | struct curseg_info *curseg_t = CURSEG_I(sbi, type); |
| 4005 | __set_test_and_inuse(sbi, curseg_t->segno); |
| 4006 | } |
| 4007 | } |
| 4008 | |
| 4009 | static void init_dirty_segmap(struct f2fs_sb_info *sbi) |
| 4010 | { |
| 4011 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
| 4012 | struct free_segmap_info *free_i = FREE_I(sbi); |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 4013 | unsigned int segno = 0, offset = 0; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4014 | unsigned short valid_blocks; |
| 4015 | |
Namjae Jeon | 8736fbf | 2013-06-16 09:49:11 +0900 | [diff] [blame] | 4016 | while (1) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4017 | /* find dirty segment based on free segmap */ |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 4018 | segno = find_next_inuse(free_i, MAIN_SEGS(sbi), offset); |
| 4019 | if (segno >= MAIN_SEGS(sbi)) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4020 | break; |
| 4021 | offset = segno + 1; |
Jaegeuk Kim | 302bd34 | 2017-04-07 14:33:22 -0700 | [diff] [blame] | 4022 | valid_blocks = get_valid_blocks(sbi, segno, false); |
Jaegeuk Kim | ec325b5 | 2014-09-02 16:24:11 -0700 | [diff] [blame] | 4023 | if (valid_blocks == sbi->blocks_per_seg || !valid_blocks) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4024 | continue; |
Jaegeuk Kim | ec325b5 | 2014-09-02 16:24:11 -0700 | [diff] [blame] | 4025 | if (valid_blocks > sbi->blocks_per_seg) { |
| 4026 | f2fs_bug_on(sbi, 1); |
| 4027 | continue; |
| 4028 | } |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4029 | mutex_lock(&dirty_i->seglist_lock); |
| 4030 | __locate_dirty_segment(sbi, segno, DIRTY); |
| 4031 | mutex_unlock(&dirty_i->seglist_lock); |
| 4032 | } |
| 4033 | } |
| 4034 | |
Jaegeuk Kim | 5ec4e49 | 2013-03-31 13:26:03 +0900 | [diff] [blame] | 4035 | static int init_victim_secmap(struct f2fs_sb_info *sbi) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4036 | { |
| 4037 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 4038 | unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4039 | |
Chao Yu | 628b3d1 | 2017-11-30 19:28:18 +0800 | [diff] [blame] | 4040 | dirty_i->victim_secmap = f2fs_kvzalloc(sbi, bitmap_size, GFP_KERNEL); |
Jaegeuk Kim | 5ec4e49 | 2013-03-31 13:26:03 +0900 | [diff] [blame] | 4041 | if (!dirty_i->victim_secmap) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4042 | return -ENOMEM; |
| 4043 | return 0; |
| 4044 | } |
| 4045 | |
| 4046 | static int build_dirty_segmap(struct f2fs_sb_info *sbi) |
| 4047 | { |
| 4048 | struct dirty_seglist_info *dirty_i; |
| 4049 | unsigned int bitmap_size, i; |
| 4050 | |
| 4051 | /* allocate memory for dirty segments list information */ |
Chao Yu | acbf054 | 2017-11-30 19:28:17 +0800 | [diff] [blame] | 4052 | dirty_i = f2fs_kzalloc(sbi, sizeof(struct dirty_seglist_info), |
| 4053 | GFP_KERNEL); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4054 | if (!dirty_i) |
| 4055 | return -ENOMEM; |
| 4056 | |
| 4057 | SM_I(sbi)->dirty_info = dirty_i; |
| 4058 | mutex_init(&dirty_i->seglist_lock); |
| 4059 | |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 4060 | bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4061 | |
| 4062 | for (i = 0; i < NR_DIRTY_TYPE; i++) { |
Chao Yu | 628b3d1 | 2017-11-30 19:28:18 +0800 | [diff] [blame] | 4063 | dirty_i->dirty_segmap[i] = f2fs_kvzalloc(sbi, bitmap_size, |
| 4064 | GFP_KERNEL); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4065 | if (!dirty_i->dirty_segmap[i]) |
| 4066 | return -ENOMEM; |
| 4067 | } |
| 4068 | |
| 4069 | init_dirty_segmap(sbi); |
Jaegeuk Kim | 5ec4e49 | 2013-03-31 13:26:03 +0900 | [diff] [blame] | 4070 | return init_victim_secmap(sbi); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4071 | } |
| 4072 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 4073 | /* |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4074 | * Update min, max modified time for cost-benefit GC algorithm |
| 4075 | */ |
| 4076 | static void init_min_max_mtime(struct f2fs_sb_info *sbi) |
| 4077 | { |
| 4078 | struct sit_info *sit_i = SIT_I(sbi); |
| 4079 | unsigned int segno; |
| 4080 | |
Chao Yu | 3d26fa6 | 2017-10-30 17:49:53 +0800 | [diff] [blame] | 4081 | down_write(&sit_i->sentry_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4082 | |
Chao Yu | 5ad2544 | 2018-05-15 18:59:55 +0800 | [diff] [blame] | 4083 | sit_i->min_mtime = ULLONG_MAX; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4084 | |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 4085 | for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4086 | unsigned int i; |
| 4087 | unsigned long long mtime = 0; |
| 4088 | |
| 4089 | for (i = 0; i < sbi->segs_per_sec; i++) |
| 4090 | mtime += get_seg_entry(sbi, segno + i)->mtime; |
| 4091 | |
| 4092 | mtime = div_u64(mtime, sbi->segs_per_sec); |
| 4093 | |
| 4094 | if (sit_i->min_mtime > mtime) |
| 4095 | sit_i->min_mtime = mtime; |
| 4096 | } |
Chao Yu | a1f72ac2 | 2018-06-04 23:20:17 +0800 | [diff] [blame] | 4097 | sit_i->max_mtime = get_mtime(sbi, false); |
Chao Yu | 3d26fa6 | 2017-10-30 17:49:53 +0800 | [diff] [blame] | 4098 | up_write(&sit_i->sentry_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4099 | } |
| 4100 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 4101 | int f2fs_build_segment_manager(struct f2fs_sb_info *sbi) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4102 | { |
| 4103 | struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi); |
| 4104 | struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); |
Namjae Jeon | 1042d60 | 2012-12-01 10:56:13 +0900 | [diff] [blame] | 4105 | struct f2fs_sm_info *sm_info; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4106 | int err; |
| 4107 | |
Chao Yu | acbf054 | 2017-11-30 19:28:17 +0800 | [diff] [blame] | 4108 | sm_info = f2fs_kzalloc(sbi, sizeof(struct f2fs_sm_info), GFP_KERNEL); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4109 | if (!sm_info) |
| 4110 | return -ENOMEM; |
| 4111 | |
| 4112 | /* init sm info */ |
| 4113 | sbi->sm_info = sm_info; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4114 | sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr); |
| 4115 | sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr); |
| 4116 | sm_info->segment_count = le32_to_cpu(raw_super->segment_count); |
| 4117 | sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count); |
| 4118 | sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count); |
| 4119 | sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main); |
| 4120 | sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr); |
Jaegeuk Kim | 58c4103 | 2014-03-19 14:17:21 +0900 | [diff] [blame] | 4121 | sm_info->rec_prefree_segments = sm_info->main_segments * |
| 4122 | DEF_RECLAIM_PREFREE_SEGMENTS / 100; |
Jaegeuk Kim | 44a8349 | 2016-07-13 18:23:35 -0700 | [diff] [blame] | 4123 | if (sm_info->rec_prefree_segments > DEF_MAX_RECLAIM_PREFREE_SEGMENTS) |
| 4124 | sm_info->rec_prefree_segments = DEF_MAX_RECLAIM_PREFREE_SEGMENTS; |
| 4125 | |
Jaegeuk Kim | 52763a4 | 2016-06-13 09:47:48 -0700 | [diff] [blame] | 4126 | if (!test_opt(sbi, LFS)) |
| 4127 | sm_info->ipu_policy = 1 << F2FS_IPU_FSYNC; |
Jaegeuk Kim | 216fbd6 | 2013-11-07 13:13:42 +0900 | [diff] [blame] | 4128 | sm_info->min_ipu_util = DEF_MIN_IPU_UTIL; |
Jaegeuk Kim | c1ce1b0 | 2014-09-10 16:53:02 -0700 | [diff] [blame] | 4129 | sm_info->min_fsync_blocks = DEF_MIN_FSYNC_BLOCKS; |
Jaegeuk Kim | 853137c | 2018-08-09 17:53:34 -0700 | [diff] [blame^] | 4130 | sm_info->min_seq_blocks = sbi->blocks_per_seg * sbi->segs_per_sec; |
Jaegeuk Kim | ef095d1 | 2017-03-24 20:05:13 -0400 | [diff] [blame] | 4131 | sm_info->min_hot_blocks = DEF_MIN_HOT_BLOCKS; |
Chao Yu | a2a12b6 | 2017-10-28 16:52:33 +0800 | [diff] [blame] | 4132 | sm_info->min_ssr_sections = reserved_sections(sbi); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4133 | |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 4134 | INIT_LIST_HEAD(&sm_info->sit_entry_set); |
| 4135 | |
Chao Yu | 2b60311 | 2017-11-02 20:41:03 +0800 | [diff] [blame] | 4136 | init_rwsem(&sm_info->curseg_lock); |
| 4137 | |
Yunlei He | d4fdf8b | 2017-06-01 16:43:51 +0800 | [diff] [blame] | 4138 | if (!f2fs_readonly(sbi->sb)) { |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 4139 | err = f2fs_create_flush_cmd_control(sbi); |
Gu Zheng | 2163d19 | 2014-04-27 14:21:33 +0800 | [diff] [blame] | 4140 | if (err) |
Gu Zheng | a688b9d9e | 2014-04-27 14:21:21 +0800 | [diff] [blame] | 4141 | return err; |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 4142 | } |
| 4143 | |
Jaegeuk Kim | 0b54fb8 | 2017-01-11 14:40:24 -0800 | [diff] [blame] | 4144 | err = create_discard_cmd_control(sbi); |
| 4145 | if (err) |
| 4146 | return err; |
| 4147 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4148 | err = build_sit_info(sbi); |
| 4149 | if (err) |
| 4150 | return err; |
| 4151 | err = build_free_segmap(sbi); |
| 4152 | if (err) |
| 4153 | return err; |
| 4154 | err = build_curseg(sbi); |
| 4155 | if (err) |
| 4156 | return err; |
| 4157 | |
| 4158 | /* reinit free segmap based on SIT */ |
Jaegeuk Kim | c39a1b3 | 2017-12-19 19:16:34 -0800 | [diff] [blame] | 4159 | err = build_sit_entries(sbi); |
| 4160 | if (err) |
| 4161 | return err; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4162 | |
| 4163 | init_free_segmap(sbi); |
| 4164 | err = build_dirty_segmap(sbi); |
| 4165 | if (err) |
| 4166 | return err; |
| 4167 | |
| 4168 | init_min_max_mtime(sbi); |
| 4169 | return 0; |
| 4170 | } |
| 4171 | |
| 4172 | static void discard_dirty_segmap(struct f2fs_sb_info *sbi, |
| 4173 | enum dirty_type dirty_type) |
| 4174 | { |
| 4175 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
| 4176 | |
| 4177 | mutex_lock(&dirty_i->seglist_lock); |
Jaegeuk Kim | 39307a8 | 2015-09-22 13:50:47 -0700 | [diff] [blame] | 4178 | kvfree(dirty_i->dirty_segmap[dirty_type]); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4179 | dirty_i->nr_dirty[dirty_type] = 0; |
| 4180 | mutex_unlock(&dirty_i->seglist_lock); |
| 4181 | } |
| 4182 | |
Jaegeuk Kim | 5ec4e49 | 2013-03-31 13:26:03 +0900 | [diff] [blame] | 4183 | static void destroy_victim_secmap(struct f2fs_sb_info *sbi) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4184 | { |
| 4185 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
Jaegeuk Kim | 39307a8 | 2015-09-22 13:50:47 -0700 | [diff] [blame] | 4186 | kvfree(dirty_i->victim_secmap); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4187 | } |
| 4188 | |
| 4189 | static void destroy_dirty_segmap(struct f2fs_sb_info *sbi) |
| 4190 | { |
| 4191 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
| 4192 | int i; |
| 4193 | |
| 4194 | if (!dirty_i) |
| 4195 | return; |
| 4196 | |
| 4197 | /* discard pre-free/dirty segments list */ |
| 4198 | for (i = 0; i < NR_DIRTY_TYPE; i++) |
| 4199 | discard_dirty_segmap(sbi, i); |
| 4200 | |
Jaegeuk Kim | 5ec4e49 | 2013-03-31 13:26:03 +0900 | [diff] [blame] | 4201 | destroy_victim_secmap(sbi); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4202 | SM_I(sbi)->dirty_info = NULL; |
| 4203 | kfree(dirty_i); |
| 4204 | } |
| 4205 | |
| 4206 | static void destroy_curseg(struct f2fs_sb_info *sbi) |
| 4207 | { |
| 4208 | struct curseg_info *array = SM_I(sbi)->curseg_array; |
| 4209 | int i; |
| 4210 | |
| 4211 | if (!array) |
| 4212 | return; |
| 4213 | SM_I(sbi)->curseg_array = NULL; |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 4214 | for (i = 0; i < NR_CURSEG_TYPE; i++) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4215 | kfree(array[i].sum_blk); |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 4216 | kfree(array[i].journal); |
| 4217 | } |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4218 | kfree(array); |
| 4219 | } |
| 4220 | |
| 4221 | static void destroy_free_segmap(struct f2fs_sb_info *sbi) |
| 4222 | { |
| 4223 | struct free_segmap_info *free_i = SM_I(sbi)->free_info; |
| 4224 | if (!free_i) |
| 4225 | return; |
| 4226 | SM_I(sbi)->free_info = NULL; |
Jaegeuk Kim | 39307a8 | 2015-09-22 13:50:47 -0700 | [diff] [blame] | 4227 | kvfree(free_i->free_segmap); |
| 4228 | kvfree(free_i->free_secmap); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4229 | kfree(free_i); |
| 4230 | } |
| 4231 | |
| 4232 | static void destroy_sit_info(struct f2fs_sb_info *sbi) |
| 4233 | { |
| 4234 | struct sit_info *sit_i = SIT_I(sbi); |
| 4235 | unsigned int start; |
| 4236 | |
| 4237 | if (!sit_i) |
| 4238 | return; |
| 4239 | |
| 4240 | if (sit_i->sentries) { |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 4241 | for (start = 0; start < MAIN_SEGS(sbi); start++) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4242 | kfree(sit_i->sentries[start].cur_valid_map); |
Chao Yu | 355e789 | 2017-01-07 18:51:01 +0800 | [diff] [blame] | 4243 | #ifdef CONFIG_F2FS_CHECK_FS |
| 4244 | kfree(sit_i->sentries[start].cur_valid_map_mir); |
| 4245 | #endif |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4246 | kfree(sit_i->sentries[start].ckpt_valid_map); |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 4247 | kfree(sit_i->sentries[start].discard_map); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4248 | } |
| 4249 | } |
Jaegeuk Kim | 60a3b78 | 2015-02-10 16:44:29 -0800 | [diff] [blame] | 4250 | kfree(sit_i->tmp_map); |
| 4251 | |
Jaegeuk Kim | 39307a8 | 2015-09-22 13:50:47 -0700 | [diff] [blame] | 4252 | kvfree(sit_i->sentries); |
| 4253 | kvfree(sit_i->sec_entries); |
| 4254 | kvfree(sit_i->dirty_sentries_bitmap); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4255 | |
| 4256 | SM_I(sbi)->sit_info = NULL; |
| 4257 | kfree(sit_i->sit_bitmap); |
Chao Yu | ae27d62 | 2017-01-07 18:52:34 +0800 | [diff] [blame] | 4258 | #ifdef CONFIG_F2FS_CHECK_FS |
| 4259 | kfree(sit_i->sit_bitmap_mir); |
| 4260 | #endif |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4261 | kfree(sit_i); |
| 4262 | } |
| 4263 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 4264 | void f2fs_destroy_segment_manager(struct f2fs_sb_info *sbi) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4265 | { |
| 4266 | struct f2fs_sm_info *sm_info = SM_I(sbi); |
Gu Zheng | a688b9d9e | 2014-04-27 14:21:21 +0800 | [diff] [blame] | 4267 | |
Chao Yu | 3b03f72 | 2013-11-06 09:12:04 +0800 | [diff] [blame] | 4268 | if (!sm_info) |
| 4269 | return; |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 4270 | f2fs_destroy_flush_cmd_control(sbi, true); |
Chao Yu | f099405 | 2017-03-27 18:14:04 +0800 | [diff] [blame] | 4271 | destroy_discard_cmd_control(sbi); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 4272 | destroy_dirty_segmap(sbi); |
| 4273 | destroy_curseg(sbi); |
| 4274 | destroy_free_segmap(sbi); |
| 4275 | destroy_sit_info(sbi); |
| 4276 | sbi->sm_info = NULL; |
| 4277 | kfree(sm_info); |
| 4278 | } |
Jaegeuk Kim | 7fd9e54 | 2013-11-15 13:55:58 +0900 | [diff] [blame] | 4279 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 4280 | int __init f2fs_create_segment_manager_caches(void) |
Jaegeuk Kim | 7fd9e54 | 2013-11-15 13:55:58 +0900 | [diff] [blame] | 4281 | { |
| 4282 | discard_entry_slab = f2fs_kmem_cache_create("discard_entry", |
Gu Zheng | e8512d2 | 2014-03-07 18:43:28 +0800 | [diff] [blame] | 4283 | sizeof(struct discard_entry)); |
Jaegeuk Kim | 7fd9e54 | 2013-11-15 13:55:58 +0900 | [diff] [blame] | 4284 | if (!discard_entry_slab) |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 4285 | goto fail; |
| 4286 | |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 4287 | discard_cmd_slab = f2fs_kmem_cache_create("discard_cmd", |
| 4288 | sizeof(struct discard_cmd)); |
| 4289 | if (!discard_cmd_slab) |
Chao Yu | 6ab2a30 | 2016-09-05 12:28:26 +0800 | [diff] [blame] | 4290 | goto destroy_discard_entry; |
Chao Yu | 275b66b | 2016-08-29 23:58:34 +0800 | [diff] [blame] | 4291 | |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 4292 | sit_entry_set_slab = f2fs_kmem_cache_create("sit_entry_set", |
Changman Lee | c9ee008 | 2014-11-21 15:42:07 +0900 | [diff] [blame] | 4293 | sizeof(struct sit_entry_set)); |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 4294 | if (!sit_entry_set_slab) |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 4295 | goto destroy_discard_cmd; |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 4296 | |
| 4297 | inmem_entry_slab = f2fs_kmem_cache_create("inmem_page_entry", |
| 4298 | sizeof(struct inmem_pages)); |
| 4299 | if (!inmem_entry_slab) |
| 4300 | goto destroy_sit_entry_set; |
Jaegeuk Kim | 7fd9e54 | 2013-11-15 13:55:58 +0900 | [diff] [blame] | 4301 | return 0; |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 4302 | |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 4303 | destroy_sit_entry_set: |
| 4304 | kmem_cache_destroy(sit_entry_set_slab); |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 4305 | destroy_discard_cmd: |
| 4306 | kmem_cache_destroy(discard_cmd_slab); |
Chao Yu | 6ab2a30 | 2016-09-05 12:28:26 +0800 | [diff] [blame] | 4307 | destroy_discard_entry: |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 4308 | kmem_cache_destroy(discard_entry_slab); |
| 4309 | fail: |
| 4310 | return -ENOMEM; |
Jaegeuk Kim | 7fd9e54 | 2013-11-15 13:55:58 +0900 | [diff] [blame] | 4311 | } |
| 4312 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 4313 | void f2fs_destroy_segment_manager_caches(void) |
Jaegeuk Kim | 7fd9e54 | 2013-11-15 13:55:58 +0900 | [diff] [blame] | 4314 | { |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 4315 | kmem_cache_destroy(sit_entry_set_slab); |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 4316 | kmem_cache_destroy(discard_cmd_slab); |
Jaegeuk Kim | 7fd9e54 | 2013-11-15 13:55:58 +0900 | [diff] [blame] | 4317 | kmem_cache_destroy(discard_entry_slab); |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 4318 | kmem_cache_destroy(inmem_entry_slab); |
Jaegeuk Kim | 7fd9e54 | 2013-11-15 13:55:58 +0900 | [diff] [blame] | 4319 | } |