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 | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 20 | |
| 21 | #include "f2fs.h" |
| 22 | #include "segment.h" |
| 23 | #include "node.h" |
Jaegeuk Kim | 9e4ded3 | 2014-12-17 19:58:58 -0800 | [diff] [blame] | 24 | #include "trace.h" |
Namjae Jeon | 6ec178d | 2013-04-23 17:51:43 +0900 | [diff] [blame] | 25 | #include <trace/events/f2fs.h> |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 26 | |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 27 | #define __reverse_ffz(x) __reverse_ffs(~(x)) |
| 28 | |
Jaegeuk Kim | 7fd9e54 | 2013-11-15 13:55:58 +0900 | [diff] [blame] | 29 | static struct kmem_cache *discard_entry_slab; |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 30 | static struct kmem_cache *discard_cmd_slab; |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 31 | static struct kmem_cache *sit_entry_set_slab; |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 32 | static struct kmem_cache *inmem_entry_slab; |
Jaegeuk Kim | 7fd9e54 | 2013-11-15 13:55:58 +0900 | [diff] [blame] | 33 | |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 34 | static unsigned long __reverse_ulong(unsigned char *str) |
| 35 | { |
| 36 | unsigned long tmp = 0; |
| 37 | int shift = 24, idx = 0; |
| 38 | |
| 39 | #if BITS_PER_LONG == 64 |
| 40 | shift = 56; |
| 41 | #endif |
| 42 | while (shift >= 0) { |
| 43 | tmp |= (unsigned long)str[idx++] << shift; |
| 44 | shift -= BITS_PER_BYTE; |
| 45 | } |
| 46 | return tmp; |
| 47 | } |
| 48 | |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 49 | /* |
| 50 | * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since |
| 51 | * MSB and LSB are reversed in a byte by f2fs_set_bit. |
| 52 | */ |
| 53 | static inline unsigned long __reverse_ffs(unsigned long word) |
| 54 | { |
| 55 | int num = 0; |
| 56 | |
| 57 | #if BITS_PER_LONG == 64 |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 58 | if ((word & 0xffffffff00000000UL) == 0) |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 59 | num += 32; |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 60 | else |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 61 | word >>= 32; |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 62 | #endif |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 63 | if ((word & 0xffff0000) == 0) |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 64 | num += 16; |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 65 | else |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 66 | word >>= 16; |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 67 | |
| 68 | if ((word & 0xff00) == 0) |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 69 | num += 8; |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 70 | else |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 71 | word >>= 8; |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 72 | |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 73 | if ((word & 0xf0) == 0) |
| 74 | num += 4; |
| 75 | else |
| 76 | word >>= 4; |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 77 | |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 78 | if ((word & 0xc) == 0) |
| 79 | num += 2; |
| 80 | else |
| 81 | word >>= 2; |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 82 | |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 83 | if ((word & 0x2) == 0) |
| 84 | num += 1; |
| 85 | return num; |
| 86 | } |
| 87 | |
| 88 | /* |
arter97 | e1c4204 | 2014-08-06 23:22:50 +0900 | [diff] [blame] | 89 | * __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] | 90 | * f2fs_set_bit makes MSB and LSB reversed in a byte. |
Fan Li | 692223d | 2015-11-12 08:43:04 +0800 | [diff] [blame] | 91 | * @size must be integral times of unsigned long. |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 92 | * Example: |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 93 | * MSB <--> LSB |
| 94 | * f2fs_set_bit(0, bitmap) => 1000 0000 |
| 95 | * f2fs_set_bit(7, bitmap) => 0000 0001 |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 96 | */ |
| 97 | static unsigned long __find_rev_next_bit(const unsigned long *addr, |
| 98 | unsigned long size, unsigned long offset) |
| 99 | { |
| 100 | const unsigned long *p = addr + BIT_WORD(offset); |
Fan Li | 692223d | 2015-11-12 08:43:04 +0800 | [diff] [blame] | 101 | unsigned long result = size; |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 102 | unsigned long tmp; |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 103 | |
| 104 | if (offset >= size) |
| 105 | return size; |
| 106 | |
Fan Li | 692223d | 2015-11-12 08:43:04 +0800 | [diff] [blame] | 107 | size -= (offset & ~(BITS_PER_LONG - 1)); |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 108 | offset %= BITS_PER_LONG; |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 109 | |
Fan Li | 692223d | 2015-11-12 08:43:04 +0800 | [diff] [blame] | 110 | while (1) { |
| 111 | if (*p == 0) |
| 112 | goto pass; |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 113 | |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 114 | tmp = __reverse_ulong((unsigned char *)p); |
Fan Li | 692223d | 2015-11-12 08:43:04 +0800 | [diff] [blame] | 115 | |
| 116 | tmp &= ~0UL >> offset; |
| 117 | if (size < BITS_PER_LONG) |
| 118 | tmp &= (~0UL << (BITS_PER_LONG - size)); |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 119 | if (tmp) |
Fan Li | 692223d | 2015-11-12 08:43:04 +0800 | [diff] [blame] | 120 | goto found; |
| 121 | pass: |
| 122 | if (size <= BITS_PER_LONG) |
| 123 | break; |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 124 | size -= BITS_PER_LONG; |
Fan Li | 692223d | 2015-11-12 08:43:04 +0800 | [diff] [blame] | 125 | offset = 0; |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 126 | p++; |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 127 | } |
Fan Li | 692223d | 2015-11-12 08:43:04 +0800 | [diff] [blame] | 128 | return result; |
| 129 | found: |
| 130 | return result - size + __reverse_ffs(tmp); |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | static unsigned long __find_rev_next_zero_bit(const unsigned long *addr, |
| 134 | unsigned long size, unsigned long offset) |
| 135 | { |
| 136 | const unsigned long *p = addr + BIT_WORD(offset); |
Jaegeuk Kim | 8060944 | 2015-12-04 16:51:13 -0800 | [diff] [blame] | 137 | unsigned long result = size; |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 138 | unsigned long tmp; |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 139 | |
| 140 | if (offset >= size) |
| 141 | return size; |
| 142 | |
Jaegeuk Kim | 8060944 | 2015-12-04 16:51:13 -0800 | [diff] [blame] | 143 | size -= (offset & ~(BITS_PER_LONG - 1)); |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 144 | offset %= BITS_PER_LONG; |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 145 | |
Jaegeuk Kim | 8060944 | 2015-12-04 16:51:13 -0800 | [diff] [blame] | 146 | while (1) { |
| 147 | if (*p == ~0UL) |
| 148 | goto pass; |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 149 | |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 150 | tmp = __reverse_ulong((unsigned char *)p); |
Jaegeuk Kim | 8060944 | 2015-12-04 16:51:13 -0800 | [diff] [blame] | 151 | |
| 152 | if (offset) |
| 153 | tmp |= ~0UL << (BITS_PER_LONG - offset); |
| 154 | if (size < BITS_PER_LONG) |
| 155 | tmp |= ~0UL >> size; |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 156 | if (tmp != ~0UL) |
Jaegeuk Kim | 8060944 | 2015-12-04 16:51:13 -0800 | [diff] [blame] | 157 | goto found; |
| 158 | pass: |
| 159 | if (size <= BITS_PER_LONG) |
| 160 | break; |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 161 | size -= BITS_PER_LONG; |
Jaegeuk Kim | 8060944 | 2015-12-04 16:51:13 -0800 | [diff] [blame] | 162 | offset = 0; |
Jaegeuk Kim | f96999c | 2015-10-20 15:17:19 -0700 | [diff] [blame] | 163 | p++; |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 164 | } |
Jaegeuk Kim | 8060944 | 2015-12-04 16:51:13 -0800 | [diff] [blame] | 165 | return result; |
| 166 | found: |
| 167 | return result - size + __reverse_ffz(tmp); |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 168 | } |
| 169 | |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 170 | void register_inmem_page(struct inode *inode, struct page *page) |
| 171 | { |
| 172 | struct f2fs_inode_info *fi = F2FS_I(inode); |
| 173 | struct inmem_pages *new; |
Jaegeuk Kim | 9be32d7 | 2014-12-05 10:39:49 -0800 | [diff] [blame] | 174 | |
Jaegeuk Kim | 9e4ded3 | 2014-12-17 19:58:58 -0800 | [diff] [blame] | 175 | f2fs_trace_pid(page); |
Jaegeuk Kim | 0722b10 | 2014-12-05 11:58:02 -0800 | [diff] [blame] | 176 | |
Chao Yu | decd36b | 2015-08-07 18:42:09 +0800 | [diff] [blame] | 177 | set_page_private(page, (unsigned long)ATOMIC_WRITTEN_PAGE); |
| 178 | SetPagePrivate(page); |
| 179 | |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 180 | new = f2fs_kmem_cache_alloc(inmem_entry_slab, GFP_NOFS); |
| 181 | |
| 182 | /* add atomic page indices to the list */ |
| 183 | new->page = page; |
| 184 | INIT_LIST_HEAD(&new->list); |
Chao Yu | decd36b | 2015-08-07 18:42:09 +0800 | [diff] [blame] | 185 | |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 186 | /* increase reference count with clean state */ |
| 187 | mutex_lock(&fi->inmem_lock); |
| 188 | get_page(page); |
| 189 | list_add_tail(&new->list, &fi->inmem_pages); |
Jaegeuk Kim | 8dcf2ff7 | 2014-12-05 17:18:15 -0800 | [diff] [blame] | 190 | inc_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES); |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 191 | mutex_unlock(&fi->inmem_lock); |
Jaegeuk Kim | 8ce67cb | 2015-03-17 17:58:08 -0700 | [diff] [blame] | 192 | |
| 193 | trace_f2fs_register_inmem_page(page, INMEM); |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 196 | static int __revoke_inmem_pages(struct inode *inode, |
| 197 | struct list_head *head, bool drop, bool recover) |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 198 | { |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 199 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 200 | struct inmem_pages *cur, *tmp; |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 201 | int err = 0; |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 202 | |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 203 | list_for_each_entry_safe(cur, tmp, head, list) { |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 204 | struct page *page = cur->page; |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 205 | |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 206 | if (drop) |
| 207 | trace_f2fs_commit_inmem_page(page, INMEM_DROP); |
| 208 | |
| 209 | lock_page(page); |
| 210 | |
| 211 | if (recover) { |
| 212 | struct dnode_of_data dn; |
| 213 | struct node_info ni; |
| 214 | |
| 215 | trace_f2fs_commit_inmem_page(page, INMEM_REVOKE); |
| 216 | |
| 217 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
| 218 | if (get_dnode_of_data(&dn, page->index, LOOKUP_NODE)) { |
| 219 | err = -EAGAIN; |
| 220 | goto next; |
| 221 | } |
| 222 | get_node_info(sbi, dn.nid, &ni); |
| 223 | f2fs_replace_block(sbi, &dn, dn.data_blkaddr, |
| 224 | cur->old_addr, ni.version, true, true); |
| 225 | f2fs_put_dnode(&dn); |
| 226 | } |
| 227 | next: |
Jaegeuk Kim | 63c52d7 | 2016-04-12 14:11:03 -0700 | [diff] [blame] | 228 | /* we don't need to invalidate this in the sccessful status */ |
| 229 | if (drop || recover) |
| 230 | ClearPageUptodate(page); |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 231 | set_page_private(page, 0); |
Chao Yu | c81ced0 | 2016-04-29 20:13:36 +0800 | [diff] [blame] | 232 | ClearPagePrivate(page); |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 233 | f2fs_put_page(page, 1); |
Chao Yu | decd36b | 2015-08-07 18:42:09 +0800 | [diff] [blame] | 234 | |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 235 | list_del(&cur->list); |
| 236 | kmem_cache_free(inmem_entry_slab, cur); |
Jaegeuk Kim | 8dcf2ff7 | 2014-12-05 17:18:15 -0800 | [diff] [blame] | 237 | dec_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES); |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 238 | } |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 239 | return err; |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | void drop_inmem_pages(struct inode *inode) |
| 243 | { |
| 244 | struct f2fs_inode_info *fi = F2FS_I(inode); |
| 245 | |
| 246 | mutex_lock(&fi->inmem_lock); |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 247 | __revoke_inmem_pages(inode, &fi->inmem_pages, true, false); |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 248 | mutex_unlock(&fi->inmem_lock); |
Chao Yu | 5fe4574 | 2017-01-07 18:50:26 +0800 | [diff] [blame] | 249 | |
| 250 | clear_inode_flag(inode, FI_ATOMIC_FILE); |
| 251 | stat_dec_atomic_write(inode); |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 252 | } |
| 253 | |
Jaegeuk Kim | 8c242db | 2017-03-17 09:55:52 +0800 | [diff] [blame] | 254 | void drop_inmem_page(struct inode *inode, struct page *page) |
| 255 | { |
| 256 | struct f2fs_inode_info *fi = F2FS_I(inode); |
| 257 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 258 | struct list_head *head = &fi->inmem_pages; |
| 259 | struct inmem_pages *cur = NULL; |
| 260 | |
| 261 | f2fs_bug_on(sbi, !IS_ATOMIC_WRITTEN_PAGE(page)); |
| 262 | |
| 263 | mutex_lock(&fi->inmem_lock); |
| 264 | list_for_each_entry(cur, head, list) { |
| 265 | if (cur->page == page) |
| 266 | break; |
| 267 | } |
| 268 | |
| 269 | f2fs_bug_on(sbi, !cur || cur->page != page); |
| 270 | list_del(&cur->list); |
| 271 | mutex_unlock(&fi->inmem_lock); |
| 272 | |
| 273 | dec_page_count(sbi, F2FS_INMEM_PAGES); |
| 274 | kmem_cache_free(inmem_entry_slab, cur); |
| 275 | |
| 276 | ClearPageUptodate(page); |
| 277 | set_page_private(page, 0); |
| 278 | ClearPagePrivate(page); |
| 279 | f2fs_put_page(page, 0); |
| 280 | |
| 281 | trace_f2fs_commit_inmem_page(page, INMEM_INVALIDATE); |
| 282 | } |
| 283 | |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 284 | static int __commit_inmem_pages(struct inode *inode, |
| 285 | struct list_head *revoke_list) |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 286 | { |
| 287 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 288 | struct f2fs_inode_info *fi = F2FS_I(inode); |
| 289 | struct inmem_pages *cur, *tmp; |
| 290 | struct f2fs_io_info fio = { |
| 291 | .sbi = sbi, |
| 292 | .type = DATA, |
Mike Christie | 04d328d | 2016-06-05 14:31:55 -0500 | [diff] [blame] | 293 | .op = REQ_OP_WRITE, |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 294 | .op_flags = REQ_SYNC | REQ_PRIO, |
Chao Yu | b0af6d4 | 2017-08-02 23:21:48 +0800 | [diff] [blame^] | 295 | .io_type = FS_DATA_IO, |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 296 | }; |
Jaegeuk Kim | 942fd31 | 2017-02-01 16:51:22 -0800 | [diff] [blame] | 297 | pgoff_t last_idx = ULONG_MAX; |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 298 | int err = 0; |
| 299 | |
| 300 | list_for_each_entry_safe(cur, tmp, &fi->inmem_pages, list) { |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 301 | struct page *page = cur->page; |
| 302 | |
| 303 | lock_page(page); |
| 304 | if (page->mapping == inode->i_mapping) { |
| 305 | trace_f2fs_commit_inmem_page(page, INMEM); |
| 306 | |
| 307 | set_page_dirty(page); |
| 308 | f2fs_wait_on_page_writeback(page, DATA, true); |
Chao Yu | 933439c | 2016-10-11 22:57:01 +0800 | [diff] [blame] | 309 | if (clear_page_dirty_for_io(page)) { |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 310 | inode_dec_dirty_pages(inode); |
Chao Yu | 933439c | 2016-10-11 22:57:01 +0800 | [diff] [blame] | 311 | remove_dirty_inode(inode); |
| 312 | } |
Jaegeuk Kim | 640cc18 | 2017-07-19 10:59:55 -0700 | [diff] [blame] | 313 | retry: |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 314 | fio.page = page; |
Hou Pengyang | e959c8f | 2017-04-25 12:45:13 +0000 | [diff] [blame] | 315 | fio.old_blkaddr = NULL_ADDR; |
Jaegeuk Kim | 4d97807 | 2017-04-26 11:11:12 -0700 | [diff] [blame] | 316 | fio.encrypted_page = NULL; |
Jaegeuk Kim | cc15620 | 2017-05-12 13:51:34 -0700 | [diff] [blame] | 317 | fio.need_lock = LOCK_DONE; |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 318 | err = do_write_data_page(&fio); |
| 319 | if (err) { |
Jaegeuk Kim | 640cc18 | 2017-07-19 10:59:55 -0700 | [diff] [blame] | 320 | if (err == -ENOMEM) { |
| 321 | congestion_wait(BLK_RW_ASYNC, HZ/50); |
| 322 | cond_resched(); |
| 323 | goto retry; |
| 324 | } |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 325 | unlock_page(page); |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 326 | break; |
| 327 | } |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 328 | /* record old blkaddr for revoking */ |
| 329 | cur->old_addr = fio.old_blkaddr; |
Jaegeuk Kim | 942fd31 | 2017-02-01 16:51:22 -0800 | [diff] [blame] | 330 | last_idx = page->index; |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 331 | } |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 332 | unlock_page(page); |
| 333 | list_move_tail(&cur->list, revoke_list); |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 334 | } |
| 335 | |
Jaegeuk Kim | 942fd31 | 2017-02-01 16:51:22 -0800 | [diff] [blame] | 336 | if (last_idx != ULONG_MAX) |
Jaegeuk Kim | b9109b0 | 2017-05-10 11:28:38 -0700 | [diff] [blame] | 337 | f2fs_submit_merged_write_cond(sbi, inode, 0, last_idx, DATA); |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 338 | |
| 339 | if (!err) |
| 340 | __revoke_inmem_pages(inode, revoke_list, false, false); |
| 341 | |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 342 | return err; |
| 343 | } |
| 344 | |
| 345 | int commit_inmem_pages(struct inode *inode) |
| 346 | { |
| 347 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 348 | struct f2fs_inode_info *fi = F2FS_I(inode); |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 349 | struct list_head revoke_list; |
| 350 | int err; |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 351 | |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 352 | INIT_LIST_HEAD(&revoke_list); |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 353 | f2fs_balance_fs(sbi, true); |
| 354 | f2fs_lock_op(sbi); |
| 355 | |
Chao Yu | 5fe4574 | 2017-01-07 18:50:26 +0800 | [diff] [blame] | 356 | set_inode_flag(inode, FI_ATOMIC_COMMIT); |
| 357 | |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 358 | mutex_lock(&fi->inmem_lock); |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 359 | err = __commit_inmem_pages(inode, &revoke_list); |
| 360 | if (err) { |
| 361 | int ret; |
| 362 | /* |
| 363 | * try to revoke all committed pages, but still we could fail |
| 364 | * due to no memory or other reason, if that happened, EAGAIN |
| 365 | * will be returned, which means in such case, transaction is |
| 366 | * already not integrity, caller should use journal to do the |
| 367 | * recovery or rewrite & commit last transaction. For other |
| 368 | * error number, revoking was done by filesystem itself. |
| 369 | */ |
| 370 | ret = __revoke_inmem_pages(inode, &revoke_list, false, true); |
| 371 | if (ret) |
| 372 | err = ret; |
| 373 | |
| 374 | /* drop all uncommitted pages */ |
| 375 | __revoke_inmem_pages(inode, &fi->inmem_pages, true, false); |
| 376 | } |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 377 | mutex_unlock(&fi->inmem_lock); |
| 378 | |
Chao Yu | 5fe4574 | 2017-01-07 18:50:26 +0800 | [diff] [blame] | 379 | clear_inode_flag(inode, FI_ATOMIC_COMMIT); |
| 380 | |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 381 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | edb27de | 2015-07-25 00:52:52 -0700 | [diff] [blame] | 382 | return err; |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 385 | /* |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 386 | * This function balances dirty node and dentry pages. |
| 387 | * In addition, it controls garbage collection. |
| 388 | */ |
Jaegeuk Kim | 2c4db1a | 2016-01-07 14:15:04 -0800 | [diff] [blame] | 389 | void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 390 | { |
Chao Yu | 0f34802 | 2016-09-26 19:45:55 +0800 | [diff] [blame] | 391 | #ifdef CONFIG_F2FS_FAULT_INJECTION |
Chao Yu | 5552351 | 2017-02-25 11:08:28 +0800 | [diff] [blame] | 392 | if (time_to_inject(sbi, FAULT_CHECKPOINT)) { |
| 393 | f2fs_show_injection_info(FAULT_CHECKPOINT); |
Chao Yu | 0f34802 | 2016-09-26 19:45:55 +0800 | [diff] [blame] | 394 | f2fs_stop_checkpoint(sbi, false); |
Chao Yu | 5552351 | 2017-02-25 11:08:28 +0800 | [diff] [blame] | 395 | } |
Chao Yu | 0f34802 | 2016-09-26 19:45:55 +0800 | [diff] [blame] | 396 | #endif |
| 397 | |
Jaegeuk Kim | e589c2c | 2016-06-02 15:24:24 -0700 | [diff] [blame] | 398 | /* balance_fs_bg is able to be pending */ |
Jaegeuk Kim | a788189 | 2017-04-20 13:51:57 -0700 | [diff] [blame] | 399 | if (need && excess_cached_nats(sbi)) |
Jaegeuk Kim | e589c2c | 2016-06-02 15:24:24 -0700 | [diff] [blame] | 400 | f2fs_balance_fs_bg(sbi); |
| 401 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 402 | /* |
Jaegeuk Kim | 029cd28 | 2012-12-21 17:20:21 +0900 | [diff] [blame] | 403 | * We should do GC or end up with checkpoint, if there are so many dirty |
| 404 | * dir/node pages without enough free segments. |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 405 | */ |
Jaegeuk Kim | 7f3037a | 2016-09-01 12:02:51 -0700 | [diff] [blame] | 406 | if (has_not_enough_free_secs(sbi, 0, 0)) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 407 | mutex_lock(&sbi->gc_mutex); |
Jaegeuk Kim | e066b83 | 2017-04-13 15:17:00 -0700 | [diff] [blame] | 408 | f2fs_gc(sbi, false, false, NULL_SEGNO); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | |
Jaegeuk Kim | 4660f9c | 2013-10-24 14:19:18 +0900 | [diff] [blame] | 412 | void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi) |
| 413 | { |
Chao Yu | 1dcc336 | 2015-02-05 17:57:31 +0800 | [diff] [blame] | 414 | /* try to shrink extent cache when there is no enough memory */ |
Jaegeuk Kim | 554df79 | 2015-06-19 13:41:23 -0700 | [diff] [blame] | 415 | if (!available_free_memory(sbi, EXTENT_CACHE)) |
| 416 | f2fs_shrink_extent_tree(sbi, EXTENT_CACHE_SHRINK_NUMBER); |
Chao Yu | 1dcc336 | 2015-02-05 17:57:31 +0800 | [diff] [blame] | 417 | |
Jaegeuk Kim | 1b38dc8 | 2015-06-19 15:36:07 -0700 | [diff] [blame] | 418 | /* check the # of cached NAT entries */ |
| 419 | if (!available_free_memory(sbi, NAT_ENTRIES)) |
| 420 | try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK); |
| 421 | |
Chao Yu | 3169658 | 2015-07-28 18:33:46 +0800 | [diff] [blame] | 422 | if (!available_free_memory(sbi, FREE_NIDS)) |
Jaegeuk Kim | ad4edb8 | 2016-06-16 16:41:49 -0700 | [diff] [blame] | 423 | try_to_free_nids(sbi, MAX_FREE_NIDS); |
| 424 | else |
Jaegeuk Kim | 22ad0b6 | 2017-02-09 10:38:09 -0800 | [diff] [blame] | 425 | build_free_nids(sbi, false, false); |
Chao Yu | 3169658 | 2015-07-28 18:33:46 +0800 | [diff] [blame] | 426 | |
Jaegeuk Kim | 1c0f4bf | 2017-05-01 18:09:44 -0700 | [diff] [blame] | 427 | if (!is_idle(sbi) && !excess_dirty_nats(sbi)) |
Jaegeuk Kim | f455c8a | 2016-12-05 11:37:14 -0800 | [diff] [blame] | 428 | return; |
Jaegeuk Kim | e5e7ea3 | 2014-11-06 15:24:46 -0800 | [diff] [blame] | 429 | |
Jaegeuk Kim | 88a70a6 | 2014-12-10 15:20:48 -0800 | [diff] [blame] | 430 | /* checkpoint is the only way to shrink partial cached entries */ |
Jaegeuk Kim | 4660f9c | 2013-10-24 14:19:18 +0900 | [diff] [blame] | 431 | if (!available_free_memory(sbi, NAT_ENTRIES) || |
Jaegeuk Kim | 60b99b4 | 2015-10-05 14:49:57 -0700 | [diff] [blame] | 432 | !available_free_memory(sbi, INO_ENTRIES) || |
Chao Yu | 7d768d2 | 2016-01-18 18:31:18 +0800 | [diff] [blame] | 433 | excess_prefree_segs(sbi) || |
| 434 | excess_dirty_nats(sbi) || |
Jaegeuk Kim | f455c8a | 2016-12-05 11:37:14 -0800 | [diff] [blame] | 435 | f2fs_time_over(sbi, CP_TIME)) { |
Chao Yu | e9f5b8b | 2016-02-14 18:54:33 +0800 | [diff] [blame] | 436 | if (test_opt(sbi, DATA_FLUSH)) { |
| 437 | struct blk_plug plug; |
| 438 | |
| 439 | blk_start_plug(&plug); |
Chao Yu | 36b35a0 | 2015-12-17 17:13:28 +0800 | [diff] [blame] | 440 | sync_dirty_inodes(sbi, FILE_INODE); |
Chao Yu | e9f5b8b | 2016-02-14 18:54:33 +0800 | [diff] [blame] | 441 | blk_finish_plug(&plug); |
| 442 | } |
Jaegeuk Kim | 4660f9c | 2013-10-24 14:19:18 +0900 | [diff] [blame] | 443 | f2fs_sync_fs(sbi->sb, true); |
Jaegeuk Kim | 42190d2 | 2016-01-09 13:45:17 -0800 | [diff] [blame] | 444 | stat_inc_bg_cp_count(sbi->stat_info); |
Chao Yu | 36b35a0 | 2015-12-17 17:13:28 +0800 | [diff] [blame] | 445 | } |
Jaegeuk Kim | 4660f9c | 2013-10-24 14:19:18 +0900 | [diff] [blame] | 446 | } |
| 447 | |
Kinglong Mee | 20fda56b | 2017-03-04 22:13:10 +0800 | [diff] [blame] | 448 | static int __submit_flush_wait(struct f2fs_sb_info *sbi, |
| 449 | struct block_device *bdev) |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 450 | { |
| 451 | struct bio *bio = f2fs_bio_alloc(0); |
| 452 | int ret; |
| 453 | |
Jan Kara | 3adc5fcb | 2017-05-02 17:03:47 +0200 | [diff] [blame] | 454 | bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH; |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 455 | bio->bi_bdev = bdev; |
| 456 | ret = submit_bio_wait(bio); |
| 457 | bio_put(bio); |
Kinglong Mee | 20fda56b | 2017-03-04 22:13:10 +0800 | [diff] [blame] | 458 | |
| 459 | trace_f2fs_issue_flush(bdev, test_opt(sbi, NOBARRIER), |
| 460 | test_opt(sbi, FLUSH_MERGE), ret); |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 461 | return ret; |
| 462 | } |
| 463 | |
| 464 | static int submit_flush_wait(struct f2fs_sb_info *sbi) |
| 465 | { |
Kinglong Mee | 20fda56b | 2017-03-04 22:13:10 +0800 | [diff] [blame] | 466 | int ret = __submit_flush_wait(sbi, sbi->sb->s_bdev); |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 467 | int i; |
| 468 | |
Kinglong Mee | 20fda56b | 2017-03-04 22:13:10 +0800 | [diff] [blame] | 469 | if (!sbi->s_ndevs || ret) |
| 470 | return ret; |
| 471 | |
| 472 | for (i = 1; i < sbi->s_ndevs; i++) { |
| 473 | ret = __submit_flush_wait(sbi, FDEV(i).bdev); |
| 474 | if (ret) |
| 475 | break; |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 476 | } |
| 477 | return ret; |
| 478 | } |
| 479 | |
Gu Zheng | 2163d19 | 2014-04-27 14:21:33 +0800 | [diff] [blame] | 480 | static int issue_flush_thread(void *data) |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 481 | { |
| 482 | struct f2fs_sb_info *sbi = data; |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 483 | struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info; |
Gu Zheng | a688b9d9e | 2014-04-27 14:21:21 +0800 | [diff] [blame] | 484 | wait_queue_head_t *q = &fcc->flush_wait_queue; |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 485 | repeat: |
| 486 | if (kthread_should_stop()) |
| 487 | return 0; |
| 488 | |
Chao Yu | dc6febb | 2017-07-22 08:52:23 +0800 | [diff] [blame] | 489 | sb_start_intwrite(sbi->sb); |
| 490 | |
Gu Zheng | 721bd4d | 2014-09-05 18:31:00 +0800 | [diff] [blame] | 491 | if (!llist_empty(&fcc->issue_list)) { |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 492 | struct flush_cmd *cmd, *next; |
| 493 | int ret; |
| 494 | |
Gu Zheng | 721bd4d | 2014-09-05 18:31:00 +0800 | [diff] [blame] | 495 | fcc->dispatch_list = llist_del_all(&fcc->issue_list); |
| 496 | fcc->dispatch_list = llist_reverse_order(fcc->dispatch_list); |
| 497 | |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 498 | ret = submit_flush_wait(sbi); |
Chao Yu | 8b8dd65 | 2017-03-25 17:19:58 +0800 | [diff] [blame] | 499 | atomic_inc(&fcc->issued_flush); |
| 500 | |
Gu Zheng | 721bd4d | 2014-09-05 18:31:00 +0800 | [diff] [blame] | 501 | llist_for_each_entry_safe(cmd, next, |
| 502 | fcc->dispatch_list, llnode) { |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 503 | cmd->ret = ret; |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 504 | complete(&cmd->wait); |
| 505 | } |
Gu Zheng | a688b9d9e | 2014-04-27 14:21:21 +0800 | [diff] [blame] | 506 | fcc->dispatch_list = NULL; |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 507 | } |
| 508 | |
Chao Yu | dc6febb | 2017-07-22 08:52:23 +0800 | [diff] [blame] | 509 | sb_end_intwrite(sbi->sb); |
| 510 | |
Gu Zheng | a688b9d9e | 2014-04-27 14:21:21 +0800 | [diff] [blame] | 511 | wait_event_interruptible(*q, |
Gu Zheng | 721bd4d | 2014-09-05 18:31:00 +0800 | [diff] [blame] | 512 | kthread_should_stop() || !llist_empty(&fcc->issue_list)); |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 513 | goto repeat; |
| 514 | } |
| 515 | |
| 516 | int f2fs_issue_flush(struct f2fs_sb_info *sbi) |
| 517 | { |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 518 | struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info; |
Chao Yu | adf8d90 | 2014-05-08 17:00:35 +0800 | [diff] [blame] | 519 | struct flush_cmd cmd; |
Chao Yu | 8b8dd65 | 2017-03-25 17:19:58 +0800 | [diff] [blame] | 520 | int ret; |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 521 | |
Jaegeuk Kim | 0f7b2ab | 2014-07-23 09:57:31 -0700 | [diff] [blame] | 522 | if (test_opt(sbi, NOBARRIER)) |
| 523 | return 0; |
| 524 | |
Chao Yu | 8b8dd65 | 2017-03-25 17:19:58 +0800 | [diff] [blame] | 525 | if (!test_opt(sbi, FLUSH_MERGE)) { |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 526 | ret = submit_flush_wait(sbi); |
Chao Yu | 8b8dd65 | 2017-03-25 17:19:58 +0800 | [diff] [blame] | 527 | atomic_inc(&fcc->issued_flush); |
| 528 | return ret; |
| 529 | } |
| 530 | |
| 531 | if (!atomic_read(&fcc->issing_flush)) { |
| 532 | atomic_inc(&fcc->issing_flush); |
| 533 | ret = submit_flush_wait(sbi); |
| 534 | atomic_dec(&fcc->issing_flush); |
| 535 | |
| 536 | atomic_inc(&fcc->issued_flush); |
Jaegeuk Kim | 740432f | 2015-08-14 11:43:56 -0700 | [diff] [blame] | 537 | return ret; |
| 538 | } |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 539 | |
Chao Yu | adf8d90 | 2014-05-08 17:00:35 +0800 | [diff] [blame] | 540 | init_completion(&cmd.wait); |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 541 | |
Chao Yu | 8b8dd65 | 2017-03-25 17:19:58 +0800 | [diff] [blame] | 542 | atomic_inc(&fcc->issing_flush); |
Gu Zheng | 721bd4d | 2014-09-05 18:31:00 +0800 | [diff] [blame] | 543 | llist_add(&cmd.llnode, &fcc->issue_list); |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 544 | |
Gu Zheng | a688b9d9e | 2014-04-27 14:21:21 +0800 | [diff] [blame] | 545 | if (!fcc->dispatch_list) |
| 546 | wake_up(&fcc->flush_wait_queue); |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 547 | |
Jaegeuk Kim | 5eba8c5 | 2016-12-07 16:23:32 -0800 | [diff] [blame] | 548 | if (fcc->f2fs_issue_flush) { |
| 549 | wait_for_completion(&cmd.wait); |
Chao Yu | 8b8dd65 | 2017-03-25 17:19:58 +0800 | [diff] [blame] | 550 | atomic_dec(&fcc->issing_flush); |
Jaegeuk Kim | 5eba8c5 | 2016-12-07 16:23:32 -0800 | [diff] [blame] | 551 | } else { |
| 552 | llist_del_all(&fcc->issue_list); |
Chao Yu | 8b8dd65 | 2017-03-25 17:19:58 +0800 | [diff] [blame] | 553 | atomic_set(&fcc->issing_flush, 0); |
Jaegeuk Kim | 5eba8c5 | 2016-12-07 16:23:32 -0800 | [diff] [blame] | 554 | } |
Chao Yu | adf8d90 | 2014-05-08 17:00:35 +0800 | [diff] [blame] | 555 | |
| 556 | return cmd.ret; |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 557 | } |
| 558 | |
Gu Zheng | 2163d19 | 2014-04-27 14:21:33 +0800 | [diff] [blame] | 559 | int create_flush_cmd_control(struct f2fs_sb_info *sbi) |
| 560 | { |
| 561 | dev_t dev = sbi->sb->s_bdev->bd_dev; |
| 562 | struct flush_cmd_control *fcc; |
| 563 | int err = 0; |
| 564 | |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 565 | if (SM_I(sbi)->fcc_info) { |
| 566 | fcc = SM_I(sbi)->fcc_info; |
Yunlong Song | d871cd0 | 2017-06-24 15:57:19 +0800 | [diff] [blame] | 567 | if (fcc->f2fs_issue_flush) |
| 568 | return err; |
Jaegeuk Kim | 5eba8c5 | 2016-12-07 16:23:32 -0800 | [diff] [blame] | 569 | goto init_thread; |
| 570 | } |
| 571 | |
Gu Zheng | 2163d19 | 2014-04-27 14:21:33 +0800 | [diff] [blame] | 572 | fcc = kzalloc(sizeof(struct flush_cmd_control), GFP_KERNEL); |
| 573 | if (!fcc) |
| 574 | return -ENOMEM; |
Chao Yu | 8b8dd65 | 2017-03-25 17:19:58 +0800 | [diff] [blame] | 575 | atomic_set(&fcc->issued_flush, 0); |
| 576 | atomic_set(&fcc->issing_flush, 0); |
Gu Zheng | 2163d19 | 2014-04-27 14:21:33 +0800 | [diff] [blame] | 577 | init_waitqueue_head(&fcc->flush_wait_queue); |
Gu Zheng | 721bd4d | 2014-09-05 18:31:00 +0800 | [diff] [blame] | 578 | init_llist_head(&fcc->issue_list); |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 579 | SM_I(sbi)->fcc_info = fcc; |
Yunlei He | d4fdf8b | 2017-06-01 16:43:51 +0800 | [diff] [blame] | 580 | if (!test_opt(sbi, FLUSH_MERGE)) |
| 581 | return err; |
| 582 | |
Jaegeuk Kim | 5eba8c5 | 2016-12-07 16:23:32 -0800 | [diff] [blame] | 583 | init_thread: |
Gu Zheng | 2163d19 | 2014-04-27 14:21:33 +0800 | [diff] [blame] | 584 | fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi, |
| 585 | "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev)); |
| 586 | if (IS_ERR(fcc->f2fs_issue_flush)) { |
| 587 | err = PTR_ERR(fcc->f2fs_issue_flush); |
| 588 | kfree(fcc); |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 589 | SM_I(sbi)->fcc_info = NULL; |
Gu Zheng | 2163d19 | 2014-04-27 14:21:33 +0800 | [diff] [blame] | 590 | return err; |
| 591 | } |
Gu Zheng | 2163d19 | 2014-04-27 14:21:33 +0800 | [diff] [blame] | 592 | |
| 593 | return err; |
| 594 | } |
| 595 | |
Jaegeuk Kim | 5eba8c5 | 2016-12-07 16:23:32 -0800 | [diff] [blame] | 596 | void destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free) |
Gu Zheng | 2163d19 | 2014-04-27 14:21:33 +0800 | [diff] [blame] | 597 | { |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 598 | struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info; |
Gu Zheng | 2163d19 | 2014-04-27 14:21:33 +0800 | [diff] [blame] | 599 | |
Jaegeuk Kim | 5eba8c5 | 2016-12-07 16:23:32 -0800 | [diff] [blame] | 600 | if (fcc && fcc->f2fs_issue_flush) { |
| 601 | struct task_struct *flush_thread = fcc->f2fs_issue_flush; |
| 602 | |
| 603 | fcc->f2fs_issue_flush = NULL; |
| 604 | kthread_stop(flush_thread); |
| 605 | } |
| 606 | if (free) { |
| 607 | kfree(fcc); |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 608 | SM_I(sbi)->fcc_info = NULL; |
Jaegeuk Kim | 5eba8c5 | 2016-12-07 16:23:32 -0800 | [diff] [blame] | 609 | } |
Gu Zheng | 2163d19 | 2014-04-27 14:21:33 +0800 | [diff] [blame] | 610 | } |
| 611 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 612 | static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno, |
| 613 | enum dirty_type dirty_type) |
| 614 | { |
| 615 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
| 616 | |
| 617 | /* need not be added */ |
| 618 | if (IS_CURSEG(sbi, segno)) |
| 619 | return; |
| 620 | |
| 621 | if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type])) |
| 622 | dirty_i->nr_dirty[dirty_type]++; |
| 623 | |
| 624 | if (dirty_type == DIRTY) { |
| 625 | struct seg_entry *sentry = get_seg_entry(sbi, segno); |
Changman Lee | 4625d6a | 2013-10-25 17:31:57 +0900 | [diff] [blame] | 626 | enum dirty_type t = sentry->type; |
Jaegeuk Kim | b2f2c39 | 2013-04-01 13:52:09 +0900 | [diff] [blame] | 627 | |
Jaegeuk Kim | ec325b5 | 2014-09-02 16:24:11 -0700 | [diff] [blame] | 628 | if (unlikely(t >= DIRTY)) { |
| 629 | f2fs_bug_on(sbi, 1); |
| 630 | return; |
| 631 | } |
Changman Lee | 4625d6a | 2013-10-25 17:31:57 +0900 | [diff] [blame] | 632 | if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t])) |
| 633 | dirty_i->nr_dirty[t]++; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 634 | } |
| 635 | } |
| 636 | |
| 637 | static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno, |
| 638 | enum dirty_type dirty_type) |
| 639 | { |
| 640 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
| 641 | |
| 642 | if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type])) |
| 643 | dirty_i->nr_dirty[dirty_type]--; |
| 644 | |
| 645 | if (dirty_type == DIRTY) { |
Changman Lee | 4625d6a | 2013-10-25 17:31:57 +0900 | [diff] [blame] | 646 | struct seg_entry *sentry = get_seg_entry(sbi, segno); |
| 647 | enum dirty_type t = sentry->type; |
Jaegeuk Kim | b2f2c39 | 2013-04-01 13:52:09 +0900 | [diff] [blame] | 648 | |
Changman Lee | 4625d6a | 2013-10-25 17:31:57 +0900 | [diff] [blame] | 649 | if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t])) |
| 650 | dirty_i->nr_dirty[t]--; |
Jaegeuk Kim | b2f2c39 | 2013-04-01 13:52:09 +0900 | [diff] [blame] | 651 | |
Jaegeuk Kim | 302bd34 | 2017-04-07 14:33:22 -0700 | [diff] [blame] | 652 | if (get_valid_blocks(sbi, segno, true) == 0) |
Jaegeuk Kim | 4ddb1a4 | 2017-04-07 15:08:17 -0700 | [diff] [blame] | 653 | clear_bit(GET_SEC_FROM_SEG(sbi, segno), |
Jaegeuk Kim | 5ec4e49 | 2013-03-31 13:26:03 +0900 | [diff] [blame] | 654 | dirty_i->victim_secmap); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 655 | } |
| 656 | } |
| 657 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 658 | /* |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 659 | * Should not occur error such as -ENOMEM. |
| 660 | * Adding dirty entry into seglist is not critical operation. |
| 661 | * If a given segment is one of current working segments, it won't be added. |
| 662 | */ |
Haicheng Li | 8d8451a | 2013-06-13 16:59:28 +0800 | [diff] [blame] | 663 | 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] | 664 | { |
| 665 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
| 666 | unsigned short valid_blocks; |
| 667 | |
| 668 | if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno)) |
| 669 | return; |
| 670 | |
| 671 | mutex_lock(&dirty_i->seglist_lock); |
| 672 | |
Jaegeuk Kim | 302bd34 | 2017-04-07 14:33:22 -0700 | [diff] [blame] | 673 | valid_blocks = get_valid_blocks(sbi, segno, false); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 674 | |
| 675 | if (valid_blocks == 0) { |
| 676 | __locate_dirty_segment(sbi, segno, PRE); |
| 677 | __remove_dirty_segment(sbi, segno, DIRTY); |
| 678 | } else if (valid_blocks < sbi->blocks_per_seg) { |
| 679 | __locate_dirty_segment(sbi, segno, DIRTY); |
| 680 | } else { |
| 681 | /* Recovery routine with SSR needs this */ |
| 682 | __remove_dirty_segment(sbi, segno, DIRTY); |
| 683 | } |
| 684 | |
| 685 | mutex_unlock(&dirty_i->seglist_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 686 | } |
| 687 | |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 688 | static struct discard_cmd *__create_discard_cmd(struct f2fs_sb_info *sbi, |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 689 | struct block_device *bdev, block_t lstart, |
| 690 | block_t start, block_t len) |
Chao Yu | 275b66b | 2016-08-29 23:58:34 +0800 | [diff] [blame] | 691 | { |
Jaegeuk Kim | 0b54fb8 | 2017-01-11 14:40:24 -0800 | [diff] [blame] | 692 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
Chao Yu | ba48a33 | 2017-04-15 14:09:37 +0800 | [diff] [blame] | 693 | struct list_head *pend_list; |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 694 | struct discard_cmd *dc; |
Chao Yu | 275b66b | 2016-08-29 23:58:34 +0800 | [diff] [blame] | 695 | |
Chao Yu | ba48a33 | 2017-04-15 14:09:37 +0800 | [diff] [blame] | 696 | f2fs_bug_on(sbi, !len); |
| 697 | |
| 698 | pend_list = &dcc->pend_list[plist_idx(len)]; |
| 699 | |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 700 | dc = f2fs_kmem_cache_alloc(discard_cmd_slab, GFP_NOFS); |
| 701 | INIT_LIST_HEAD(&dc->list); |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 702 | dc->bdev = bdev; |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 703 | dc->lstart = lstart; |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 704 | dc->start = start; |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 705 | dc->len = len; |
Chao Yu | ec9895a | 2017-04-26 17:39:54 +0800 | [diff] [blame] | 706 | dc->ref = 0; |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 707 | dc->state = D_PREP; |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 708 | dc->error = 0; |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 709 | init_completion(&dc->wait); |
Chao Yu | 22d375d | 2017-04-05 18:19:48 +0800 | [diff] [blame] | 710 | list_add_tail(&dc->list, pend_list); |
Chao Yu | 5f32366 | 2017-03-25 17:19:59 +0800 | [diff] [blame] | 711 | atomic_inc(&dcc->discard_cmd_cnt); |
Chao Yu | d84d1cb | 2017-04-18 19:27:39 +0800 | [diff] [blame] | 712 | dcc->undiscard_blks += len; |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 713 | |
| 714 | return dc; |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 715 | } |
| 716 | |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 717 | static struct discard_cmd *__attach_discard_cmd(struct f2fs_sb_info *sbi, |
| 718 | struct block_device *bdev, block_t lstart, |
| 719 | block_t start, block_t len, |
| 720 | struct rb_node *parent, struct rb_node **p) |
| 721 | { |
| 722 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
| 723 | struct discard_cmd *dc; |
| 724 | |
| 725 | dc = __create_discard_cmd(sbi, bdev, lstart, start, len); |
| 726 | |
| 727 | rb_link_node(&dc->rb_node, parent, p); |
| 728 | rb_insert_color(&dc->rb_node, &dcc->root); |
| 729 | |
| 730 | return dc; |
| 731 | } |
| 732 | |
| 733 | static void __detach_discard_cmd(struct discard_cmd_control *dcc, |
| 734 | struct discard_cmd *dc) |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 735 | { |
Jaegeuk Kim | dcc9165 | 2017-01-11 10:20:04 -0800 | [diff] [blame] | 736 | if (dc->state == D_DONE) |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 737 | atomic_dec(&dcc->issing_discard); |
| 738 | |
| 739 | list_del(&dc->list); |
| 740 | rb_erase(&dc->rb_node, &dcc->root); |
Chao Yu | d84d1cb | 2017-04-18 19:27:39 +0800 | [diff] [blame] | 741 | dcc->undiscard_blks -= dc->len; |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 742 | |
| 743 | kmem_cache_free(discard_cmd_slab, dc); |
| 744 | |
| 745 | atomic_dec(&dcc->discard_cmd_cnt); |
| 746 | } |
| 747 | |
| 748 | static void __remove_discard_cmd(struct f2fs_sb_info *sbi, |
| 749 | struct discard_cmd *dc) |
| 750 | { |
| 751 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
Jaegeuk Kim | dcc9165 | 2017-01-11 10:20:04 -0800 | [diff] [blame] | 752 | |
Chao Yu | d9703d9 | 2017-06-05 18:29:07 +0800 | [diff] [blame] | 753 | f2fs_bug_on(sbi, dc->ref); |
| 754 | |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 755 | if (dc->error == -EOPNOTSUPP) |
| 756 | dc->error = 0; |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 757 | |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 758 | if (dc->error) |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 759 | f2fs_msg(sbi->sb, KERN_INFO, |
Chao Yu | 04dfc23 | 2017-05-19 23:46:43 +0800 | [diff] [blame] | 760 | "Issue discard(%u, %u, %u) failed, ret: %d", |
| 761 | dc->lstart, dc->start, dc->len, dc->error); |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 762 | __detach_discard_cmd(dcc, dc); |
Chao Yu | 275b66b | 2016-08-29 23:58:34 +0800 | [diff] [blame] | 763 | } |
| 764 | |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 765 | static void f2fs_submit_discard_endio(struct bio *bio) |
| 766 | { |
| 767 | struct discard_cmd *dc = (struct discard_cmd *)bio->bi_private; |
| 768 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 769 | dc->error = blk_status_to_errno(bio->bi_status); |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 770 | dc->state = D_DONE; |
Chao Yu | e31b982 | 2017-05-19 23:46:44 +0800 | [diff] [blame] | 771 | complete_all(&dc->wait); |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 772 | bio_put(bio); |
| 773 | } |
| 774 | |
Chao Yu | 6915ea9 | 2017-06-30 17:19:02 +0800 | [diff] [blame] | 775 | void __check_sit_bitmap(struct f2fs_sb_info *sbi, |
| 776 | block_t start, block_t end) |
| 777 | { |
| 778 | #ifdef CONFIG_F2FS_CHECK_FS |
| 779 | struct seg_entry *sentry; |
| 780 | unsigned int segno; |
| 781 | block_t blk = start; |
| 782 | unsigned long offset, size, max_blocks = sbi->blocks_per_seg; |
| 783 | unsigned long *map; |
| 784 | |
| 785 | while (blk < end) { |
| 786 | segno = GET_SEGNO(sbi, blk); |
| 787 | sentry = get_seg_entry(sbi, segno); |
| 788 | offset = GET_BLKOFF_FROM_SEG0(sbi, blk); |
| 789 | |
| 790 | size = min((unsigned long)(end - blk), max_blocks); |
| 791 | map = (unsigned long *)(sentry->cur_valid_map); |
| 792 | offset = __find_rev_next_bit(map, size, offset); |
| 793 | f2fs_bug_on(sbi, offset != size); |
| 794 | blk += size; |
| 795 | } |
| 796 | #endif |
| 797 | } |
| 798 | |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 799 | /* this function is copied from blkdev_issue_discard from block/blk-lib.c */ |
| 800 | static void __submit_discard_cmd(struct f2fs_sb_info *sbi, |
| 801 | struct discard_cmd *dc) |
| 802 | { |
| 803 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
| 804 | struct bio *bio = NULL; |
| 805 | |
| 806 | if (dc->state != D_PREP) |
| 807 | return; |
| 808 | |
Chao Yu | 0243a5f | 2017-04-15 14:09:38 +0800 | [diff] [blame] | 809 | trace_f2fs_issue_discard(dc->bdev, dc->start, dc->len); |
| 810 | |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 811 | dc->error = __blkdev_issue_discard(dc->bdev, |
| 812 | SECTOR_FROM_BLOCK(dc->start), |
| 813 | SECTOR_FROM_BLOCK(dc->len), |
| 814 | GFP_NOFS, 0, &bio); |
| 815 | if (!dc->error) { |
| 816 | /* should keep before submission to avoid D_DONE right away */ |
| 817 | dc->state = D_SUBMIT; |
Chao Yu | 8b8dd65 | 2017-03-25 17:19:58 +0800 | [diff] [blame] | 818 | atomic_inc(&dcc->issued_discard); |
| 819 | atomic_inc(&dcc->issing_discard); |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 820 | if (bio) { |
| 821 | bio->bi_private = dc; |
| 822 | bio->bi_end_io = f2fs_submit_discard_endio; |
| 823 | bio->bi_opf |= REQ_SYNC; |
| 824 | submit_bio(bio); |
Chao Yu | 46f84c2 | 2017-04-15 14:09:36 +0800 | [diff] [blame] | 825 | list_move_tail(&dc->list, &dcc->wait_list); |
Chao Yu | 6915ea9 | 2017-06-30 17:19:02 +0800 | [diff] [blame] | 826 | __check_sit_bitmap(sbi, dc->start, dc->start + dc->len); |
Chao Yu | b0af6d4 | 2017-08-02 23:21:48 +0800 | [diff] [blame^] | 827 | |
| 828 | f2fs_update_iostat(sbi, FS_DISCARD, 1); |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 829 | } |
| 830 | } else { |
| 831 | __remove_discard_cmd(sbi, dc); |
| 832 | } |
| 833 | } |
| 834 | |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 835 | static struct discard_cmd *__insert_discard_tree(struct f2fs_sb_info *sbi, |
| 836 | struct block_device *bdev, block_t lstart, |
| 837 | block_t start, block_t len, |
| 838 | struct rb_node **insert_p, |
| 839 | struct rb_node *insert_parent) |
| 840 | { |
| 841 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
| 842 | struct rb_node **p = &dcc->root.rb_node; |
| 843 | struct rb_node *parent = NULL; |
| 844 | struct discard_cmd *dc = NULL; |
| 845 | |
| 846 | if (insert_p && insert_parent) { |
| 847 | parent = insert_parent; |
| 848 | p = insert_p; |
| 849 | goto do_insert; |
| 850 | } |
| 851 | |
| 852 | p = __lookup_rb_tree_for_insert(sbi, &dcc->root, &parent, lstart); |
| 853 | do_insert: |
| 854 | dc = __attach_discard_cmd(sbi, bdev, lstart, start, len, parent, p); |
| 855 | if (!dc) |
| 856 | return NULL; |
| 857 | |
| 858 | return dc; |
| 859 | } |
| 860 | |
Chao Yu | ba48a33 | 2017-04-15 14:09:37 +0800 | [diff] [blame] | 861 | static void __relocate_discard_cmd(struct discard_cmd_control *dcc, |
| 862 | struct discard_cmd *dc) |
| 863 | { |
| 864 | list_move_tail(&dc->list, &dcc->pend_list[plist_idx(dc->len)]); |
| 865 | } |
| 866 | |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 867 | static void __punch_discard_cmd(struct f2fs_sb_info *sbi, |
| 868 | struct discard_cmd *dc, block_t blkaddr) |
| 869 | { |
Chao Yu | ba48a33 | 2017-04-15 14:09:37 +0800 | [diff] [blame] | 870 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 871 | struct discard_info di = dc->di; |
| 872 | bool modified = false; |
| 873 | |
| 874 | if (dc->state == D_DONE || dc->len == 1) { |
| 875 | __remove_discard_cmd(sbi, dc); |
| 876 | return; |
| 877 | } |
| 878 | |
Chao Yu | d84d1cb | 2017-04-18 19:27:39 +0800 | [diff] [blame] | 879 | dcc->undiscard_blks -= di.len; |
| 880 | |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 881 | if (blkaddr > di.lstart) { |
| 882 | dc->len = blkaddr - dc->lstart; |
Chao Yu | d84d1cb | 2017-04-18 19:27:39 +0800 | [diff] [blame] | 883 | dcc->undiscard_blks += dc->len; |
Chao Yu | ba48a33 | 2017-04-15 14:09:37 +0800 | [diff] [blame] | 884 | __relocate_discard_cmd(dcc, dc); |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 885 | modified = true; |
| 886 | } |
| 887 | |
| 888 | if (blkaddr < di.lstart + di.len - 1) { |
| 889 | if (modified) { |
| 890 | __insert_discard_tree(sbi, dc->bdev, blkaddr + 1, |
| 891 | di.start + blkaddr + 1 - di.lstart, |
| 892 | di.lstart + di.len - 1 - blkaddr, |
| 893 | NULL, NULL); |
| 894 | } else { |
| 895 | dc->lstart++; |
| 896 | dc->len--; |
| 897 | dc->start++; |
Chao Yu | d84d1cb | 2017-04-18 19:27:39 +0800 | [diff] [blame] | 898 | dcc->undiscard_blks += dc->len; |
Chao Yu | ba48a33 | 2017-04-15 14:09:37 +0800 | [diff] [blame] | 899 | __relocate_discard_cmd(dcc, dc); |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 900 | } |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | static void __update_discard_tree_range(struct f2fs_sb_info *sbi, |
| 905 | struct block_device *bdev, block_t lstart, |
| 906 | block_t start, block_t len) |
| 907 | { |
| 908 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
| 909 | struct discard_cmd *prev_dc = NULL, *next_dc = NULL; |
| 910 | struct discard_cmd *dc; |
| 911 | struct discard_info di = {0}; |
| 912 | struct rb_node **insert_p = NULL, *insert_parent = NULL; |
| 913 | block_t end = lstart + len; |
| 914 | |
| 915 | mutex_lock(&dcc->cmd_lock); |
| 916 | |
| 917 | dc = (struct discard_cmd *)__lookup_rb_tree_ret(&dcc->root, |
| 918 | NULL, lstart, |
| 919 | (struct rb_entry **)&prev_dc, |
| 920 | (struct rb_entry **)&next_dc, |
| 921 | &insert_p, &insert_parent, true); |
| 922 | if (dc) |
| 923 | prev_dc = dc; |
| 924 | |
| 925 | if (!prev_dc) { |
| 926 | di.lstart = lstart; |
| 927 | di.len = next_dc ? next_dc->lstart - lstart : len; |
| 928 | di.len = min(di.len, len); |
| 929 | di.start = start; |
| 930 | } |
| 931 | |
| 932 | while (1) { |
| 933 | struct rb_node *node; |
| 934 | bool merged = false; |
| 935 | struct discard_cmd *tdc = NULL; |
| 936 | |
| 937 | if (prev_dc) { |
| 938 | di.lstart = prev_dc->lstart + prev_dc->len; |
| 939 | if (di.lstart < lstart) |
| 940 | di.lstart = lstart; |
| 941 | if (di.lstart >= end) |
| 942 | break; |
| 943 | |
| 944 | if (!next_dc || next_dc->lstart > end) |
| 945 | di.len = end - di.lstart; |
| 946 | else |
| 947 | di.len = next_dc->lstart - di.lstart; |
| 948 | di.start = start + di.lstart - lstart; |
| 949 | } |
| 950 | |
| 951 | if (!di.len) |
| 952 | goto next; |
| 953 | |
| 954 | if (prev_dc && prev_dc->state == D_PREP && |
| 955 | prev_dc->bdev == bdev && |
| 956 | __is_discard_back_mergeable(&di, &prev_dc->di)) { |
| 957 | prev_dc->di.len += di.len; |
Chao Yu | d84d1cb | 2017-04-18 19:27:39 +0800 | [diff] [blame] | 958 | dcc->undiscard_blks += di.len; |
Chao Yu | ba48a33 | 2017-04-15 14:09:37 +0800 | [diff] [blame] | 959 | __relocate_discard_cmd(dcc, prev_dc); |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 960 | di = prev_dc->di; |
| 961 | tdc = prev_dc; |
| 962 | merged = true; |
| 963 | } |
| 964 | |
| 965 | if (next_dc && next_dc->state == D_PREP && |
| 966 | next_dc->bdev == bdev && |
| 967 | __is_discard_front_mergeable(&di, &next_dc->di)) { |
| 968 | next_dc->di.lstart = di.lstart; |
| 969 | next_dc->di.len += di.len; |
| 970 | next_dc->di.start = di.start; |
Chao Yu | d84d1cb | 2017-04-18 19:27:39 +0800 | [diff] [blame] | 971 | dcc->undiscard_blks += di.len; |
Chao Yu | ba48a33 | 2017-04-15 14:09:37 +0800 | [diff] [blame] | 972 | __relocate_discard_cmd(dcc, next_dc); |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 973 | if (tdc) |
| 974 | __remove_discard_cmd(sbi, tdc); |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 975 | merged = true; |
| 976 | } |
| 977 | |
Chao Yu | df0f6b4 | 2017-04-17 18:21:43 +0800 | [diff] [blame] | 978 | if (!merged) { |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 979 | __insert_discard_tree(sbi, bdev, di.lstart, di.start, |
| 980 | di.len, NULL, NULL); |
Chao Yu | df0f6b4 | 2017-04-17 18:21:43 +0800 | [diff] [blame] | 981 | } |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 982 | next: |
| 983 | prev_dc = next_dc; |
| 984 | if (!prev_dc) |
| 985 | break; |
| 986 | |
| 987 | node = rb_next(&prev_dc->rb_node); |
| 988 | next_dc = rb_entry_safe(node, struct discard_cmd, rb_node); |
| 989 | } |
| 990 | |
| 991 | mutex_unlock(&dcc->cmd_lock); |
| 992 | } |
| 993 | |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 994 | static int __queue_discard_cmd(struct f2fs_sb_info *sbi, |
| 995 | struct block_device *bdev, block_t blkstart, block_t blklen) |
| 996 | { |
| 997 | block_t lblkstart = blkstart; |
| 998 | |
Chao Yu | 0243a5f | 2017-04-15 14:09:38 +0800 | [diff] [blame] | 999 | trace_f2fs_queue_discard(bdev, blkstart, blklen); |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 1000 | |
| 1001 | if (sbi->s_ndevs) { |
| 1002 | int devi = f2fs_target_device_index(sbi, blkstart); |
| 1003 | |
| 1004 | blkstart -= FDEV(devi).start_blk; |
| 1005 | } |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1006 | __update_discard_tree_range(sbi, bdev, lblkstart, blkstart, blklen); |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 1007 | return 0; |
| 1008 | } |
| 1009 | |
Chao Yu | bd5b073 | 2017-04-25 20:21:37 +0800 | [diff] [blame] | 1010 | static void __issue_discard_cmd(struct f2fs_sb_info *sbi, bool issue_cond) |
| 1011 | { |
| 1012 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
| 1013 | struct list_head *pend_list; |
| 1014 | struct discard_cmd *dc, *tmp; |
| 1015 | struct blk_plug plug; |
| 1016 | int i, iter = 0; |
| 1017 | |
| 1018 | mutex_lock(&dcc->cmd_lock); |
Yunlei He | 963932a | 2017-05-19 14:42:12 +0800 | [diff] [blame] | 1019 | f2fs_bug_on(sbi, |
| 1020 | !__check_rb_tree_consistence(sbi, &dcc->root)); |
Chao Yu | bd5b073 | 2017-04-25 20:21:37 +0800 | [diff] [blame] | 1021 | blk_start_plug(&plug); |
| 1022 | for (i = MAX_PLIST_NUM - 1; i >= 0; i--) { |
| 1023 | pend_list = &dcc->pend_list[i]; |
| 1024 | list_for_each_entry_safe(dc, tmp, pend_list, list) { |
| 1025 | f2fs_bug_on(sbi, dc->state != D_PREP); |
| 1026 | |
| 1027 | if (!issue_cond || is_idle(sbi)) |
| 1028 | __submit_discard_cmd(sbi, dc); |
| 1029 | if (issue_cond && iter++ > DISCARD_ISSUE_RATE) |
| 1030 | goto out; |
| 1031 | } |
| 1032 | } |
| 1033 | out: |
| 1034 | blk_finish_plug(&plug); |
| 1035 | mutex_unlock(&dcc->cmd_lock); |
| 1036 | } |
| 1037 | |
Chao Yu | 2a510c00 | 2017-06-05 18:29:06 +0800 | [diff] [blame] | 1038 | static void __wait_one_discard_bio(struct f2fs_sb_info *sbi, |
| 1039 | struct discard_cmd *dc) |
| 1040 | { |
| 1041 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
| 1042 | |
| 1043 | wait_for_completion_io(&dc->wait); |
| 1044 | mutex_lock(&dcc->cmd_lock); |
| 1045 | f2fs_bug_on(sbi, dc->state != D_DONE); |
| 1046 | dc->ref--; |
| 1047 | if (!dc->ref) |
| 1048 | __remove_discard_cmd(sbi, dc); |
| 1049 | mutex_unlock(&dcc->cmd_lock); |
| 1050 | } |
| 1051 | |
Chao Yu | 63a94fa | 2017-04-25 20:21:38 +0800 | [diff] [blame] | 1052 | static void __wait_discard_cmd(struct f2fs_sb_info *sbi, bool wait_cond) |
| 1053 | { |
| 1054 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
| 1055 | struct list_head *wait_list = &(dcc->wait_list); |
| 1056 | struct discard_cmd *dc, *tmp; |
Chao Yu | 6afae63 | 2017-05-19 23:46:45 +0800 | [diff] [blame] | 1057 | bool need_wait; |
| 1058 | |
| 1059 | next: |
| 1060 | need_wait = false; |
Chao Yu | 63a94fa | 2017-04-25 20:21:38 +0800 | [diff] [blame] | 1061 | |
| 1062 | mutex_lock(&dcc->cmd_lock); |
| 1063 | list_for_each_entry_safe(dc, tmp, wait_list, list) { |
Chao Yu | 6afae63 | 2017-05-19 23:46:45 +0800 | [diff] [blame] | 1064 | if (!wait_cond || (dc->state == D_DONE && !dc->ref)) { |
Chao Yu | 63a94fa | 2017-04-25 20:21:38 +0800 | [diff] [blame] | 1065 | wait_for_completion_io(&dc->wait); |
| 1066 | __remove_discard_cmd(sbi, dc); |
Chao Yu | 6afae63 | 2017-05-19 23:46:45 +0800 | [diff] [blame] | 1067 | } else { |
| 1068 | dc->ref++; |
| 1069 | need_wait = true; |
| 1070 | break; |
Chao Yu | 63a94fa | 2017-04-25 20:21:38 +0800 | [diff] [blame] | 1071 | } |
| 1072 | } |
| 1073 | mutex_unlock(&dcc->cmd_lock); |
Chao Yu | 6afae63 | 2017-05-19 23:46:45 +0800 | [diff] [blame] | 1074 | |
| 1075 | if (need_wait) { |
Chao Yu | 2a510c00 | 2017-06-05 18:29:06 +0800 | [diff] [blame] | 1076 | __wait_one_discard_bio(sbi, dc); |
Chao Yu | 6afae63 | 2017-05-19 23:46:45 +0800 | [diff] [blame] | 1077 | goto next; |
| 1078 | } |
Chao Yu | 63a94fa | 2017-04-25 20:21:38 +0800 | [diff] [blame] | 1079 | } |
| 1080 | |
Jaegeuk Kim | 4e6a8d9 | 2016-12-29 14:07:53 -0800 | [diff] [blame] | 1081 | /* This should be covered by global mutex, &sit_i->sentry_lock */ |
| 1082 | 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] | 1083 | { |
Jaegeuk Kim | 0b54fb8 | 2017-01-11 14:40:24 -0800 | [diff] [blame] | 1084 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1085 | struct discard_cmd *dc; |
Chao Yu | ec9895a | 2017-04-26 17:39:54 +0800 | [diff] [blame] | 1086 | bool need_wait = false; |
Chao Yu | 275b66b | 2016-08-29 23:58:34 +0800 | [diff] [blame] | 1087 | |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 1088 | mutex_lock(&dcc->cmd_lock); |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1089 | dc = (struct discard_cmd *)__lookup_rb_tree(&dcc->root, NULL, blkaddr); |
| 1090 | if (dc) { |
Chao Yu | ec9895a | 2017-04-26 17:39:54 +0800 | [diff] [blame] | 1091 | if (dc->state == D_PREP) { |
| 1092 | __punch_discard_cmd(sbi, dc, blkaddr); |
| 1093 | } else { |
| 1094 | dc->ref++; |
| 1095 | need_wait = true; |
| 1096 | } |
Chao Yu | 275b66b | 2016-08-29 23:58:34 +0800 | [diff] [blame] | 1097 | } |
Chao Yu | d431413 | 2017-04-05 18:19:49 +0800 | [diff] [blame] | 1098 | mutex_unlock(&dcc->cmd_lock); |
Chao Yu | ec9895a | 2017-04-26 17:39:54 +0800 | [diff] [blame] | 1099 | |
Chao Yu | 2a510c00 | 2017-06-05 18:29:06 +0800 | [diff] [blame] | 1100 | if (need_wait) |
| 1101 | __wait_one_discard_bio(sbi, dc); |
Chao Yu | d431413 | 2017-04-05 18:19:49 +0800 | [diff] [blame] | 1102 | } |
Chao Yu | 22d375d | 2017-04-05 18:19:48 +0800 | [diff] [blame] | 1103 | |
Chao Yu | cce1325 | 2017-06-29 23:17:45 +0800 | [diff] [blame] | 1104 | void stop_discard_thread(struct f2fs_sb_info *sbi) |
| 1105 | { |
| 1106 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
| 1107 | |
| 1108 | if (dcc && dcc->f2fs_issue_discard) { |
| 1109 | struct task_struct *discard_thread = dcc->f2fs_issue_discard; |
| 1110 | |
| 1111 | dcc->f2fs_issue_discard = NULL; |
| 1112 | kthread_stop(discard_thread); |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 1113 | } |
| 1114 | } |
| 1115 | |
| 1116 | /* This comes from f2fs_put_super */ |
Chao Yu | bd5b073 | 2017-04-25 20:21:37 +0800 | [diff] [blame] | 1117 | void f2fs_wait_discard_bios(struct f2fs_sb_info *sbi) |
Chao Yu | 63a94fa | 2017-04-25 20:21:38 +0800 | [diff] [blame] | 1118 | { |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 1119 | __issue_discard_cmd(sbi, false); |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 1120 | __wait_discard_cmd(sbi, false); |
| 1121 | } |
Chao Yu | 22d375d | 2017-04-05 18:19:48 +0800 | [diff] [blame] | 1122 | |
Chao Yu | ba48a33 | 2017-04-15 14:09:37 +0800 | [diff] [blame] | 1123 | static int issue_discard_thread(void *data) |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 1124 | { |
| 1125 | struct f2fs_sb_info *sbi = data; |
| 1126 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
Damien Le Moal | f46e8809 | 2016-10-28 17:45:06 +0900 | [diff] [blame] | 1127 | wait_queue_head_t *q = &dcc->discard_wait_queue; |
Chao Yu | 275b66b | 2016-08-29 23:58:34 +0800 | [diff] [blame] | 1128 | |
Jaegeuk Kim | 1d7be27 | 2017-05-17 10:36:58 -0700 | [diff] [blame] | 1129 | set_freezable(); |
Chao Yu | 275b66b | 2016-08-29 23:58:34 +0800 | [diff] [blame] | 1130 | |
Jaegeuk Kim | 1d7be27 | 2017-05-17 10:36:58 -0700 | [diff] [blame] | 1131 | do { |
| 1132 | wait_event_interruptible(*q, kthread_should_stop() || |
| 1133 | freezing(current) || |
| 1134 | atomic_read(&dcc->discard_cmd_cnt)); |
| 1135 | if (try_to_freeze()) |
| 1136 | continue; |
| 1137 | if (kthread_should_stop()) |
| 1138 | return 0; |
Chao Yu | 275b66b | 2016-08-29 23:58:34 +0800 | [diff] [blame] | 1139 | |
Chao Yu | dc6febb | 2017-07-22 08:52:23 +0800 | [diff] [blame] | 1140 | sb_start_intwrite(sbi->sb); |
| 1141 | |
Jaegeuk Kim | 1d7be27 | 2017-05-17 10:36:58 -0700 | [diff] [blame] | 1142 | __issue_discard_cmd(sbi, true); |
| 1143 | __wait_discard_cmd(sbi, true); |
| 1144 | |
Chao Yu | dc6febb | 2017-07-22 08:52:23 +0800 | [diff] [blame] | 1145 | sb_end_intwrite(sbi->sb); |
| 1146 | |
Jaegeuk Kim | 1d7be27 | 2017-05-17 10:36:58 -0700 | [diff] [blame] | 1147 | congestion_wait(BLK_RW_SYNC, HZ/50); |
| 1148 | } while (!kthread_should_stop()); |
| 1149 | return 0; |
Chao Yu | 275b66b | 2016-08-29 23:58:34 +0800 | [diff] [blame] | 1150 | } |
| 1151 | |
Damien Le Moal | f46e8809 | 2016-10-28 17:45:06 +0900 | [diff] [blame] | 1152 | #ifdef CONFIG_BLK_DEV_ZONED |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 1153 | static int __f2fs_issue_discard_zone(struct f2fs_sb_info *sbi, |
| 1154 | struct block_device *bdev, block_t blkstart, block_t blklen) |
Damien Le Moal | f46e8809 | 2016-10-28 17:45:06 +0900 | [diff] [blame] | 1155 | { |
Jaegeuk Kim | 92592285 | 2017-02-22 20:18:35 -0800 | [diff] [blame] | 1156 | sector_t sector, nr_sects; |
Kinglong Mee | 10a875f | 2017-03-08 09:49:53 +0800 | [diff] [blame] | 1157 | block_t lblkstart = blkstart; |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 1158 | int devi = 0; |
Damien Le Moal | f46e8809 | 2016-10-28 17:45:06 +0900 | [diff] [blame] | 1159 | |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 1160 | if (sbi->s_ndevs) { |
| 1161 | devi = f2fs_target_device_index(sbi, blkstart); |
| 1162 | blkstart -= FDEV(devi).start_blk; |
| 1163 | } |
Damien Le Moal | f46e8809 | 2016-10-28 17:45:06 +0900 | [diff] [blame] | 1164 | |
| 1165 | /* |
| 1166 | * We need to know the type of the zone: for conventional zones, |
| 1167 | * use regular discard if the drive supports it. For sequential |
| 1168 | * zones, reset the zone write pointer. |
| 1169 | */ |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 1170 | switch (get_blkz_type(sbi, bdev, blkstart)) { |
Damien Le Moal | f46e8809 | 2016-10-28 17:45:06 +0900 | [diff] [blame] | 1171 | |
| 1172 | case BLK_ZONE_TYPE_CONVENTIONAL: |
| 1173 | if (!blk_queue_discard(bdev_get_queue(bdev))) |
| 1174 | return 0; |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 1175 | return __queue_discard_cmd(sbi, bdev, lblkstart, blklen); |
Damien Le Moal | f46e8809 | 2016-10-28 17:45:06 +0900 | [diff] [blame] | 1176 | case BLK_ZONE_TYPE_SEQWRITE_REQ: |
| 1177 | case BLK_ZONE_TYPE_SEQWRITE_PREF: |
Jaegeuk Kim | 92592285 | 2017-02-22 20:18:35 -0800 | [diff] [blame] | 1178 | sector = SECTOR_FROM_BLOCK(blkstart); |
| 1179 | nr_sects = SECTOR_FROM_BLOCK(blklen); |
| 1180 | |
| 1181 | if (sector & (bdev_zone_sectors(bdev) - 1) || |
| 1182 | nr_sects != bdev_zone_sectors(bdev)) { |
| 1183 | f2fs_msg(sbi->sb, KERN_INFO, |
| 1184 | "(%d) %s: Unaligned discard attempted (block %x + %x)", |
| 1185 | devi, sbi->s_ndevs ? FDEV(devi).path: "", |
| 1186 | blkstart, blklen); |
| 1187 | return -EIO; |
| 1188 | } |
Jaegeuk Kim | d50aaee | 2017-02-15 11:14:06 -0800 | [diff] [blame] | 1189 | trace_f2fs_issue_reset_zone(bdev, blkstart); |
Damien Le Moal | f46e8809 | 2016-10-28 17:45:06 +0900 | [diff] [blame] | 1190 | return blkdev_reset_zones(bdev, sector, |
| 1191 | nr_sects, GFP_NOFS); |
| 1192 | default: |
| 1193 | /* Unknown zone type: broken device ? */ |
| 1194 | return -EIO; |
| 1195 | } |
| 1196 | } |
| 1197 | #endif |
| 1198 | |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 1199 | static int __issue_discard_async(struct f2fs_sb_info *sbi, |
| 1200 | struct block_device *bdev, block_t blkstart, block_t blklen) |
| 1201 | { |
| 1202 | #ifdef CONFIG_BLK_DEV_ZONED |
| 1203 | if (f2fs_sb_mounted_blkzoned(sbi->sb) && |
| 1204 | bdev_zoned_model(bdev) != BLK_ZONED_NONE) |
| 1205 | return __f2fs_issue_discard_zone(sbi, bdev, blkstart, blklen); |
| 1206 | #endif |
Jaegeuk Kim | c81abe3 | 2017-03-07 18:02:02 -0800 | [diff] [blame] | 1207 | return __queue_discard_cmd(sbi, bdev, blkstart, blklen); |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 1208 | } |
| 1209 | |
Jaegeuk Kim | 1e87a78 | 2014-04-15 13:57:55 +0900 | [diff] [blame] | 1210 | static int f2fs_issue_discard(struct f2fs_sb_info *sbi, |
Jaegeuk Kim | 3720887 | 2013-11-12 16:55:17 +0900 | [diff] [blame] | 1211 | block_t blkstart, block_t blklen) |
| 1212 | { |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 1213 | sector_t start = blkstart, len = 0; |
| 1214 | struct block_device *bdev; |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 1215 | struct seg_entry *se; |
| 1216 | unsigned int offset; |
| 1217 | block_t i; |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 1218 | int err = 0; |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 1219 | |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 1220 | bdev = f2fs_target_device(sbi, blkstart, NULL); |
| 1221 | |
| 1222 | for (i = blkstart; i < blkstart + blklen; i++, len++) { |
| 1223 | if (i != start) { |
| 1224 | struct block_device *bdev2 = |
| 1225 | f2fs_target_device(sbi, i, NULL); |
| 1226 | |
| 1227 | if (bdev2 != bdev) { |
| 1228 | err = __issue_discard_async(sbi, bdev, |
| 1229 | start, len); |
| 1230 | if (err) |
| 1231 | return err; |
| 1232 | bdev = bdev2; |
| 1233 | start = i; |
| 1234 | len = 0; |
| 1235 | } |
| 1236 | } |
| 1237 | |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 1238 | se = get_seg_entry(sbi, GET_SEGNO(sbi, i)); |
| 1239 | offset = GET_BLKOFF_FROM_SEG0(sbi, i); |
| 1240 | |
| 1241 | if (!f2fs_test_and_set_bit(offset, se->discard_map)) |
| 1242 | sbi->discard_blks--; |
| 1243 | } |
Damien Le Moal | f46e8809 | 2016-10-28 17:45:06 +0900 | [diff] [blame] | 1244 | |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 1245 | if (len) |
| 1246 | err = __issue_discard_async(sbi, bdev, start, len); |
| 1247 | return err; |
Jaegeuk Kim | 1e87a78 | 2014-04-15 13:57:55 +0900 | [diff] [blame] | 1248 | } |
| 1249 | |
Jaegeuk Kim | 25290fa | 2016-12-29 22:06:15 -0800 | [diff] [blame] | 1250 | static bool add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc, |
| 1251 | bool check_only) |
Jaegeuk Kim | adf4983 | 2014-10-28 22:27:59 -0700 | [diff] [blame] | 1252 | { |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 1253 | int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long); |
| 1254 | int max_blocks = sbi->blocks_per_seg; |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 1255 | struct seg_entry *se = get_seg_entry(sbi, cpc->trim_start); |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 1256 | unsigned long *cur_map = (unsigned long *)se->cur_valid_map; |
| 1257 | unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map; |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 1258 | unsigned long *discard_map = (unsigned long *)se->discard_map; |
Jaegeuk Kim | 60a3b78 | 2015-02-10 16:44:29 -0800 | [diff] [blame] | 1259 | unsigned long *dmap = SIT_I(sbi)->tmp_map; |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 1260 | unsigned int start = 0, end = -1; |
Chao Yu | c473f1a | 2017-04-27 20:40:39 +0800 | [diff] [blame] | 1261 | bool force = (cpc->reason & CP_DISCARD); |
Chao Yu | a7eeb823 | 2017-03-28 18:18:50 +0800 | [diff] [blame] | 1262 | struct discard_entry *de = NULL; |
Chao Yu | 46f84c2 | 2017-04-15 14:09:36 +0800 | [diff] [blame] | 1263 | struct list_head *head = &SM_I(sbi)->dcc_info->entry_list; |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 1264 | int i; |
| 1265 | |
Jaegeuk Kim | 3e02574 | 2016-08-02 10:56:40 -0700 | [diff] [blame] | 1266 | if (se->valid_blocks == max_blocks || !f2fs_discard_en(sbi)) |
Jaegeuk Kim | 25290fa | 2016-12-29 22:06:15 -0800 | [diff] [blame] | 1267 | return false; |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 1268 | |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 1269 | if (!force) { |
| 1270 | if (!test_opt(sbi, DISCARD) || !se->valid_blocks || |
Jaegeuk Kim | 0b54fb8 | 2017-01-11 14:40:24 -0800 | [diff] [blame] | 1271 | SM_I(sbi)->dcc_info->nr_discards >= |
| 1272 | SM_I(sbi)->dcc_info->max_discards) |
Jaegeuk Kim | 25290fa | 2016-12-29 22:06:15 -0800 | [diff] [blame] | 1273 | return false; |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 1274 | } |
| 1275 | |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 1276 | /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */ |
| 1277 | for (i = 0; i < entries; i++) |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 1278 | dmap[i] = force ? ~ckpt_map[i] & ~discard_map[i] : |
Jaegeuk Kim | d7bc248 | 2014-12-12 13:53:41 -0800 | [diff] [blame] | 1279 | (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i]; |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 1280 | |
Jaegeuk Kim | 0b54fb8 | 2017-01-11 14:40:24 -0800 | [diff] [blame] | 1281 | while (force || SM_I(sbi)->dcc_info->nr_discards <= |
| 1282 | SM_I(sbi)->dcc_info->max_discards) { |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 1283 | start = __find_rev_next_bit(dmap, max_blocks, end + 1); |
| 1284 | if (start >= max_blocks) |
| 1285 | break; |
| 1286 | |
| 1287 | end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1); |
Yunlei He | c7b41e1 | 2016-07-07 12:13:33 +0800 | [diff] [blame] | 1288 | if (force && start && end != max_blocks |
| 1289 | && (end - start) < cpc->trim_minlen) |
| 1290 | continue; |
| 1291 | |
Jaegeuk Kim | 25290fa | 2016-12-29 22:06:15 -0800 | [diff] [blame] | 1292 | if (check_only) |
| 1293 | return true; |
| 1294 | |
Chao Yu | a7eeb823 | 2017-03-28 18:18:50 +0800 | [diff] [blame] | 1295 | if (!de) { |
| 1296 | de = f2fs_kmem_cache_alloc(discard_entry_slab, |
| 1297 | GFP_F2FS_ZERO); |
| 1298 | de->start_blkaddr = START_BLOCK(sbi, cpc->trim_start); |
| 1299 | list_add_tail(&de->list, head); |
| 1300 | } |
| 1301 | |
| 1302 | for (i = start; i < end; i++) |
| 1303 | __set_bit_le(i, (void *)de->discard_map); |
| 1304 | |
| 1305 | SM_I(sbi)->dcc_info->nr_discards += end - start; |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 1306 | } |
Jaegeuk Kim | 25290fa | 2016-12-29 22:06:15 -0800 | [diff] [blame] | 1307 | return false; |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 1308 | } |
| 1309 | |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 1310 | void release_discard_addrs(struct f2fs_sb_info *sbi) |
| 1311 | { |
Chao Yu | 46f84c2 | 2017-04-15 14:09:36 +0800 | [diff] [blame] | 1312 | struct list_head *head = &(SM_I(sbi)->dcc_info->entry_list); |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 1313 | struct discard_entry *entry, *this; |
| 1314 | |
| 1315 | /* drop caches */ |
| 1316 | list_for_each_entry_safe(entry, this, head, list) { |
| 1317 | list_del(&entry->list); |
| 1318 | kmem_cache_free(discard_entry_slab, entry); |
| 1319 | } |
| 1320 | } |
| 1321 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 1322 | /* |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1323 | * Should call clear_prefree_segments after checkpoint is done. |
| 1324 | */ |
| 1325 | static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi) |
| 1326 | { |
| 1327 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
Chao Yu | b65ee14 | 2014-08-04 10:10:07 +0800 | [diff] [blame] | 1328 | unsigned int segno; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1329 | |
| 1330 | mutex_lock(&dirty_i->seglist_lock); |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 1331 | 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] | 1332 | __set_test_and_free(sbi, segno); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1333 | mutex_unlock(&dirty_i->seglist_lock); |
| 1334 | } |
| 1335 | |
Jaegeuk Kim | 836b5a6 | 2015-04-30 22:50:06 -0700 | [diff] [blame] | 1336 | void clear_prefree_segments(struct f2fs_sb_info *sbi, struct cp_control *cpc) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1337 | { |
Chao Yu | 46f84c2 | 2017-04-15 14:09:36 +0800 | [diff] [blame] | 1338 | struct list_head *head = &(SM_I(sbi)->dcc_info->entry_list); |
Chao Yu | 2d7b822 | 2014-03-29 11:33:17 +0800 | [diff] [blame] | 1339 | struct discard_entry *entry, *this; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1340 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
Changman Lee | 29e59c1 | 2013-11-11 09:24:37 +0900 | [diff] [blame] | 1341 | unsigned long *prefree_map = dirty_i->dirty_segmap[PRE]; |
Changman Lee | 29e59c1 | 2013-11-11 09:24:37 +0900 | [diff] [blame] | 1342 | unsigned int start = 0, end = -1; |
Jaegeuk Kim | 36abef4 | 2016-06-03 19:29:38 -0700 | [diff] [blame] | 1343 | unsigned int secno, start_segno; |
Chao Yu | c473f1a | 2017-04-27 20:40:39 +0800 | [diff] [blame] | 1344 | bool force = (cpc->reason & CP_DISCARD); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1345 | |
| 1346 | mutex_lock(&dirty_i->seglist_lock); |
Changman Lee | 29e59c1 | 2013-11-11 09:24:37 +0900 | [diff] [blame] | 1347 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1348 | while (1) { |
Changman Lee | 29e59c1 | 2013-11-11 09:24:37 +0900 | [diff] [blame] | 1349 | int i; |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 1350 | start = find_next_bit(prefree_map, MAIN_SEGS(sbi), end + 1); |
| 1351 | if (start >= MAIN_SEGS(sbi)) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1352 | break; |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 1353 | end = find_next_zero_bit(prefree_map, MAIN_SEGS(sbi), |
| 1354 | start + 1); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1355 | |
Changman Lee | 29e59c1 | 2013-11-11 09:24:37 +0900 | [diff] [blame] | 1356 | for (i = start; i < end; i++) |
| 1357 | clear_bit(i, prefree_map); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1358 | |
Changman Lee | 29e59c1 | 2013-11-11 09:24:37 +0900 | [diff] [blame] | 1359 | dirty_i->nr_dirty[PRE] -= end - start; |
| 1360 | |
Yunlei He | 650d3c4 | 2016-12-22 11:46:24 +0800 | [diff] [blame] | 1361 | if (!test_opt(sbi, DISCARD)) |
Changman Lee | 29e59c1 | 2013-11-11 09:24:37 +0900 | [diff] [blame] | 1362 | continue; |
| 1363 | |
Yunlei He | 650d3c4 | 2016-12-22 11:46:24 +0800 | [diff] [blame] | 1364 | if (force && start >= cpc->trim_start && |
| 1365 | (end - 1) <= cpc->trim_end) |
| 1366 | continue; |
| 1367 | |
Jaegeuk Kim | 36abef4 | 2016-06-03 19:29:38 -0700 | [diff] [blame] | 1368 | if (!test_opt(sbi, LFS) || sbi->segs_per_sec == 1) { |
| 1369 | f2fs_issue_discard(sbi, START_BLOCK(sbi, start), |
Jaegeuk Kim | 3720887 | 2013-11-12 16:55:17 +0900 | [diff] [blame] | 1370 | (end - start) << sbi->log_blocks_per_seg); |
Jaegeuk Kim | 36abef4 | 2016-06-03 19:29:38 -0700 | [diff] [blame] | 1371 | continue; |
| 1372 | } |
| 1373 | next: |
Jaegeuk Kim | 4ddb1a4 | 2017-04-07 15:08:17 -0700 | [diff] [blame] | 1374 | secno = GET_SEC_FROM_SEG(sbi, start); |
| 1375 | start_segno = GET_SEG_FROM_SEC(sbi, secno); |
Jaegeuk Kim | 36abef4 | 2016-06-03 19:29:38 -0700 | [diff] [blame] | 1376 | if (!IS_CURSEC(sbi, secno) && |
Jaegeuk Kim | 302bd34 | 2017-04-07 14:33:22 -0700 | [diff] [blame] | 1377 | !get_valid_blocks(sbi, start, true)) |
Jaegeuk Kim | 36abef4 | 2016-06-03 19:29:38 -0700 | [diff] [blame] | 1378 | f2fs_issue_discard(sbi, START_BLOCK(sbi, start_segno), |
| 1379 | sbi->segs_per_sec << sbi->log_blocks_per_seg); |
| 1380 | |
| 1381 | start = start_segno + sbi->segs_per_sec; |
| 1382 | if (start < end) |
| 1383 | goto next; |
Jaegeuk Kim | 8b107f5 | 2017-02-27 11:57:11 -0800 | [diff] [blame] | 1384 | else |
| 1385 | end = start - 1; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1386 | } |
| 1387 | mutex_unlock(&dirty_i->seglist_lock); |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 1388 | |
| 1389 | /* send small discards */ |
Chao Yu | 2d7b822 | 2014-03-29 11:33:17 +0800 | [diff] [blame] | 1390 | list_for_each_entry_safe(entry, this, head, list) { |
Chao Yu | a7eeb823 | 2017-03-28 18:18:50 +0800 | [diff] [blame] | 1391 | unsigned int cur_pos = 0, next_pos, len, total_len = 0; |
| 1392 | bool is_valid = test_bit_le(0, entry->discard_map); |
| 1393 | |
| 1394 | find_next: |
| 1395 | if (is_valid) { |
| 1396 | next_pos = find_next_zero_bit_le(entry->discard_map, |
| 1397 | sbi->blocks_per_seg, cur_pos); |
| 1398 | len = next_pos - cur_pos; |
| 1399 | |
Damien Le Moal | acfd2810 | 2017-05-26 17:04:40 +0900 | [diff] [blame] | 1400 | if (f2fs_sb_mounted_blkzoned(sbi->sb) || |
| 1401 | (force && len < cpc->trim_minlen)) |
Chao Yu | a7eeb823 | 2017-03-28 18:18:50 +0800 | [diff] [blame] | 1402 | goto skip; |
| 1403 | |
| 1404 | f2fs_issue_discard(sbi, entry->start_blkaddr + cur_pos, |
| 1405 | len); |
| 1406 | cpc->trimmed += len; |
| 1407 | total_len += len; |
| 1408 | } else { |
| 1409 | next_pos = find_next_bit_le(entry->discard_map, |
| 1410 | sbi->blocks_per_seg, cur_pos); |
| 1411 | } |
Jaegeuk Kim | 836b5a6 | 2015-04-30 22:50:06 -0700 | [diff] [blame] | 1412 | skip: |
Chao Yu | a7eeb823 | 2017-03-28 18:18:50 +0800 | [diff] [blame] | 1413 | cur_pos = next_pos; |
| 1414 | is_valid = !is_valid; |
| 1415 | |
| 1416 | if (cur_pos < sbi->blocks_per_seg) |
| 1417 | goto find_next; |
| 1418 | |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 1419 | list_del(&entry->list); |
Chao Yu | a7eeb823 | 2017-03-28 18:18:50 +0800 | [diff] [blame] | 1420 | SM_I(sbi)->dcc_info->nr_discards -= total_len; |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 1421 | kmem_cache_free(discard_entry_slab, entry); |
| 1422 | } |
Chao Yu | 34e159d | 2017-04-25 00:21:34 +0800 | [diff] [blame] | 1423 | |
| 1424 | wake_up(&SM_I(sbi)->dcc_info->discard_wait_queue); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1425 | } |
| 1426 | |
Jaegeuk Kim | 8ed5974 | 2017-01-29 14:27:02 +0900 | [diff] [blame] | 1427 | static int create_discard_cmd_control(struct f2fs_sb_info *sbi) |
Jaegeuk Kim | 0b54fb8 | 2017-01-11 14:40:24 -0800 | [diff] [blame] | 1428 | { |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 1429 | dev_t dev = sbi->sb->s_bdev->bd_dev; |
Jaegeuk Kim | 0b54fb8 | 2017-01-11 14:40:24 -0800 | [diff] [blame] | 1430 | struct discard_cmd_control *dcc; |
Chao Yu | ba48a33 | 2017-04-15 14:09:37 +0800 | [diff] [blame] | 1431 | int err = 0, i; |
Jaegeuk Kim | 0b54fb8 | 2017-01-11 14:40:24 -0800 | [diff] [blame] | 1432 | |
| 1433 | if (SM_I(sbi)->dcc_info) { |
| 1434 | dcc = SM_I(sbi)->dcc_info; |
| 1435 | goto init_thread; |
| 1436 | } |
| 1437 | |
| 1438 | dcc = kzalloc(sizeof(struct discard_cmd_control), GFP_KERNEL); |
| 1439 | if (!dcc) |
| 1440 | return -ENOMEM; |
| 1441 | |
Chao Yu | 46f84c2 | 2017-04-15 14:09:36 +0800 | [diff] [blame] | 1442 | INIT_LIST_HEAD(&dcc->entry_list); |
Chao Yu | ba48a33 | 2017-04-15 14:09:37 +0800 | [diff] [blame] | 1443 | for (i = 0; i < MAX_PLIST_NUM; i++) |
| 1444 | INIT_LIST_HEAD(&dcc->pend_list[i]); |
Chao Yu | 46f84c2 | 2017-04-15 14:09:36 +0800 | [diff] [blame] | 1445 | INIT_LIST_HEAD(&dcc->wait_list); |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 1446 | mutex_init(&dcc->cmd_lock); |
Chao Yu | 8b8dd65 | 2017-03-25 17:19:58 +0800 | [diff] [blame] | 1447 | atomic_set(&dcc->issued_discard, 0); |
| 1448 | atomic_set(&dcc->issing_discard, 0); |
Chao Yu | 5f32366 | 2017-03-25 17:19:59 +0800 | [diff] [blame] | 1449 | atomic_set(&dcc->discard_cmd_cnt, 0); |
Jaegeuk Kim | 0b54fb8 | 2017-01-11 14:40:24 -0800 | [diff] [blame] | 1450 | dcc->nr_discards = 0; |
Chao Yu | d618eba | 2017-04-25 00:21:35 +0800 | [diff] [blame] | 1451 | dcc->max_discards = MAIN_SEGS(sbi) << sbi->log_blocks_per_seg; |
Chao Yu | d84d1cb | 2017-04-18 19:27:39 +0800 | [diff] [blame] | 1452 | dcc->undiscard_blks = 0; |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 1453 | dcc->root = RB_ROOT; |
Jaegeuk Kim | 0b54fb8 | 2017-01-11 14:40:24 -0800 | [diff] [blame] | 1454 | |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 1455 | init_waitqueue_head(&dcc->discard_wait_queue); |
Jaegeuk Kim | 0b54fb8 | 2017-01-11 14:40:24 -0800 | [diff] [blame] | 1456 | SM_I(sbi)->dcc_info = dcc; |
| 1457 | init_thread: |
Jaegeuk Kim | 1546996 | 2017-01-09 20:32:07 -0800 | [diff] [blame] | 1458 | dcc->f2fs_issue_discard = kthread_run(issue_discard_thread, sbi, |
| 1459 | "f2fs_discard-%u:%u", MAJOR(dev), MINOR(dev)); |
| 1460 | if (IS_ERR(dcc->f2fs_issue_discard)) { |
| 1461 | err = PTR_ERR(dcc->f2fs_issue_discard); |
| 1462 | kfree(dcc); |
| 1463 | SM_I(sbi)->dcc_info = NULL; |
| 1464 | return err; |
| 1465 | } |
| 1466 | |
Jaegeuk Kim | 0b54fb8 | 2017-01-11 14:40:24 -0800 | [diff] [blame] | 1467 | return err; |
| 1468 | } |
| 1469 | |
Chao Yu | f099405 | 2017-03-27 18:14:04 +0800 | [diff] [blame] | 1470 | static void destroy_discard_cmd_control(struct f2fs_sb_info *sbi) |
Jaegeuk Kim | 0b54fb8 | 2017-01-11 14:40:24 -0800 | [diff] [blame] | 1471 | { |
| 1472 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
| 1473 | |
Chao Yu | f099405 | 2017-03-27 18:14:04 +0800 | [diff] [blame] | 1474 | if (!dcc) |
| 1475 | return; |
| 1476 | |
Chao Yu | cce1325 | 2017-06-29 23:17:45 +0800 | [diff] [blame] | 1477 | stop_discard_thread(sbi); |
Chao Yu | f099405 | 2017-03-27 18:14:04 +0800 | [diff] [blame] | 1478 | |
| 1479 | kfree(dcc); |
| 1480 | SM_I(sbi)->dcc_info = NULL; |
Jaegeuk Kim | 0b54fb8 | 2017-01-11 14:40:24 -0800 | [diff] [blame] | 1481 | } |
| 1482 | |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 1483 | 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] | 1484 | { |
| 1485 | struct sit_info *sit_i = SIT_I(sbi); |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 1486 | |
| 1487 | if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap)) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1488 | sit_i->dirty_sentries++; |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 1489 | return false; |
| 1490 | } |
| 1491 | |
| 1492 | return true; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type, |
| 1496 | unsigned int segno, int modified) |
| 1497 | { |
| 1498 | struct seg_entry *se = get_seg_entry(sbi, segno); |
| 1499 | se->type = type; |
| 1500 | if (modified) |
| 1501 | __mark_sit_entry_dirty(sbi, segno); |
| 1502 | } |
| 1503 | |
| 1504 | static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del) |
| 1505 | { |
| 1506 | struct seg_entry *se; |
| 1507 | unsigned int segno, offset; |
| 1508 | long int new_vblocks; |
Yunlong Song | 6415fed | 2017-08-02 21:20:13 +0800 | [diff] [blame] | 1509 | bool exist; |
| 1510 | #ifdef CONFIG_F2FS_CHECK_FS |
| 1511 | bool mir_exist; |
| 1512 | #endif |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1513 | |
| 1514 | segno = GET_SEGNO(sbi, blkaddr); |
| 1515 | |
| 1516 | se = get_seg_entry(sbi, segno); |
| 1517 | new_vblocks = se->valid_blocks + del; |
Jaegeuk Kim | 491c085 | 2014-02-04 13:01:10 +0900 | [diff] [blame] | 1518 | offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1519 | |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 1520 | f2fs_bug_on(sbi, (new_vblocks >> (sizeof(unsigned short) << 3) || |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1521 | (new_vblocks > sbi->blocks_per_seg))); |
| 1522 | |
| 1523 | se->valid_blocks = new_vblocks; |
| 1524 | se->mtime = get_mtime(sbi); |
| 1525 | SIT_I(sbi)->max_mtime = se->mtime; |
| 1526 | |
| 1527 | /* Update valid block bitmap */ |
| 1528 | if (del > 0) { |
Yunlong Song | 6415fed | 2017-08-02 21:20:13 +0800 | [diff] [blame] | 1529 | exist = f2fs_test_and_set_bit(offset, se->cur_valid_map); |
Chao Yu | 355e789 | 2017-01-07 18:51:01 +0800 | [diff] [blame] | 1530 | #ifdef CONFIG_F2FS_CHECK_FS |
Yunlong Song | 6415fed | 2017-08-02 21:20:13 +0800 | [diff] [blame] | 1531 | mir_exist = f2fs_test_and_set_bit(offset, |
| 1532 | se->cur_valid_map_mir); |
| 1533 | if (unlikely(exist != mir_exist)) { |
| 1534 | f2fs_msg(sbi->sb, KERN_ERR, "Inconsistent error " |
| 1535 | "when setting bitmap, blk:%u, old bit:%d", |
| 1536 | blkaddr, exist); |
Jaegeuk Kim | 0579676 | 2014-09-02 16:05:00 -0700 | [diff] [blame] | 1537 | f2fs_bug_on(sbi, 1); |
Chao Yu | 355e789 | 2017-01-07 18:51:01 +0800 | [diff] [blame] | 1538 | } |
Yunlong Song | 6415fed | 2017-08-02 21:20:13 +0800 | [diff] [blame] | 1539 | #endif |
| 1540 | if (unlikely(exist)) { |
| 1541 | f2fs_msg(sbi->sb, KERN_ERR, |
| 1542 | "Bitmap was wrongly set, blk:%u", blkaddr); |
| 1543 | f2fs_bug_on(sbi, 1); |
Yunlong Song | 35ee82c | 2017-08-02 22:16:54 +0800 | [diff] [blame] | 1544 | se->valid_blocks--; |
| 1545 | del = 0; |
Yunlong Song | 6415fed | 2017-08-02 21:20:13 +0800 | [diff] [blame] | 1546 | } |
| 1547 | |
Jaegeuk Kim | 3e02574 | 2016-08-02 10:56:40 -0700 | [diff] [blame] | 1548 | if (f2fs_discard_en(sbi) && |
| 1549 | !f2fs_test_and_set_bit(offset, se->discard_map)) |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 1550 | sbi->discard_blks--; |
Jaegeuk Kim | 720037f | 2017-03-06 11:59:56 -0800 | [diff] [blame] | 1551 | |
| 1552 | /* don't overwrite by SSR to keep node chain */ |
| 1553 | if (se->type == CURSEG_WARM_NODE) { |
| 1554 | if (!f2fs_test_and_set_bit(offset, se->ckpt_valid_map)) |
| 1555 | se->ckpt_valid_blocks++; |
| 1556 | } |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1557 | } else { |
Yunlong Song | 6415fed | 2017-08-02 21:20:13 +0800 | [diff] [blame] | 1558 | exist = f2fs_test_and_clear_bit(offset, se->cur_valid_map); |
Chao Yu | 355e789 | 2017-01-07 18:51:01 +0800 | [diff] [blame] | 1559 | #ifdef CONFIG_F2FS_CHECK_FS |
Yunlong Song | 6415fed | 2017-08-02 21:20:13 +0800 | [diff] [blame] | 1560 | mir_exist = f2fs_test_and_clear_bit(offset, |
| 1561 | se->cur_valid_map_mir); |
| 1562 | if (unlikely(exist != mir_exist)) { |
| 1563 | f2fs_msg(sbi->sb, KERN_ERR, "Inconsistent error " |
| 1564 | "when clearing bitmap, blk:%u, old bit:%d", |
| 1565 | blkaddr, exist); |
Jaegeuk Kim | 0579676 | 2014-09-02 16:05:00 -0700 | [diff] [blame] | 1566 | f2fs_bug_on(sbi, 1); |
Chao Yu | 355e789 | 2017-01-07 18:51:01 +0800 | [diff] [blame] | 1567 | } |
Yunlong Song | 6415fed | 2017-08-02 21:20:13 +0800 | [diff] [blame] | 1568 | #endif |
| 1569 | if (unlikely(!exist)) { |
| 1570 | f2fs_msg(sbi->sb, KERN_ERR, |
| 1571 | "Bitmap was wrongly cleared, blk:%u", blkaddr); |
| 1572 | f2fs_bug_on(sbi, 1); |
Yunlong Song | 35ee82c | 2017-08-02 22:16:54 +0800 | [diff] [blame] | 1573 | se->valid_blocks++; |
| 1574 | del = 0; |
Yunlong Song | 6415fed | 2017-08-02 21:20:13 +0800 | [diff] [blame] | 1575 | } |
| 1576 | |
Jaegeuk Kim | 3e02574 | 2016-08-02 10:56:40 -0700 | [diff] [blame] | 1577 | if (f2fs_discard_en(sbi) && |
| 1578 | f2fs_test_and_clear_bit(offset, se->discard_map)) |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 1579 | sbi->discard_blks++; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1580 | } |
| 1581 | if (!f2fs_test_bit(offset, se->ckpt_valid_map)) |
| 1582 | se->ckpt_valid_blocks += del; |
| 1583 | |
| 1584 | __mark_sit_entry_dirty(sbi, segno); |
| 1585 | |
| 1586 | /* update total number of valid blocks to be written in ckpt area */ |
| 1587 | SIT_I(sbi)->written_valid_blocks += del; |
| 1588 | |
| 1589 | if (sbi->segs_per_sec > 1) |
| 1590 | get_sec_entry(sbi, segno)->valid_blocks += del; |
| 1591 | } |
| 1592 | |
Jaegeuk Kim | 5e44381 | 2014-01-28 12:22:14 +0900 | [diff] [blame] | 1593 | void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1594 | { |
Jaegeuk Kim | 5e44381 | 2014-01-28 12:22:14 +0900 | [diff] [blame] | 1595 | update_sit_entry(sbi, new, 1); |
| 1596 | if (GET_SEGNO(sbi, old) != NULL_SEGNO) |
| 1597 | update_sit_entry(sbi, old, -1); |
| 1598 | |
| 1599 | locate_dirty_segment(sbi, GET_SEGNO(sbi, old)); |
| 1600 | locate_dirty_segment(sbi, GET_SEGNO(sbi, new)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1601 | } |
| 1602 | |
| 1603 | void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr) |
| 1604 | { |
| 1605 | unsigned int segno = GET_SEGNO(sbi, addr); |
| 1606 | struct sit_info *sit_i = SIT_I(sbi); |
| 1607 | |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 1608 | f2fs_bug_on(sbi, addr == NULL_ADDR); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1609 | if (addr == NEW_ADDR) |
| 1610 | return; |
| 1611 | |
| 1612 | /* add it into sit main buffer */ |
| 1613 | mutex_lock(&sit_i->sentry_lock); |
| 1614 | |
| 1615 | update_sit_entry(sbi, addr, -1); |
| 1616 | |
| 1617 | /* add it into dirty seglist */ |
| 1618 | locate_dirty_segment(sbi, segno); |
| 1619 | |
| 1620 | mutex_unlock(&sit_i->sentry_lock); |
| 1621 | } |
| 1622 | |
Jaegeuk Kim | 6e2c64a | 2015-10-07 12:28:41 -0700 | [diff] [blame] | 1623 | bool is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr) |
| 1624 | { |
| 1625 | struct sit_info *sit_i = SIT_I(sbi); |
| 1626 | unsigned int segno, offset; |
| 1627 | struct seg_entry *se; |
| 1628 | bool is_cp = false; |
| 1629 | |
| 1630 | if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR) |
| 1631 | return true; |
| 1632 | |
| 1633 | mutex_lock(&sit_i->sentry_lock); |
| 1634 | |
| 1635 | segno = GET_SEGNO(sbi, blkaddr); |
| 1636 | se = get_seg_entry(sbi, segno); |
| 1637 | offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr); |
| 1638 | |
| 1639 | if (f2fs_test_bit(offset, se->ckpt_valid_map)) |
| 1640 | is_cp = true; |
| 1641 | |
| 1642 | mutex_unlock(&sit_i->sentry_lock); |
| 1643 | |
| 1644 | return is_cp; |
| 1645 | } |
| 1646 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 1647 | /* |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1648 | * This function should be resided under the curseg_mutex lock |
| 1649 | */ |
| 1650 | static void __add_sum_entry(struct f2fs_sb_info *sbi, int type, |
Haicheng Li | e79efe3 | 2013-06-13 16:59:27 +0800 | [diff] [blame] | 1651 | struct f2fs_summary *sum) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1652 | { |
| 1653 | struct curseg_info *curseg = CURSEG_I(sbi, type); |
| 1654 | void *addr = curseg->sum_blk; |
Haicheng Li | e79efe3 | 2013-06-13 16:59:27 +0800 | [diff] [blame] | 1655 | addr += curseg->next_blkoff * sizeof(struct f2fs_summary); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1656 | memcpy(addr, sum, sizeof(struct f2fs_summary)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1657 | } |
| 1658 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 1659 | /* |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1660 | * Calculate the number of current summary pages for writing |
| 1661 | */ |
Chao Yu | 3fa06d7 | 2014-12-09 14:21:46 +0800 | [diff] [blame] | 1662 | int npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1663 | { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1664 | int valid_sum_count = 0; |
Fan Li | 9a47938 | 2013-10-29 16:21:47 +0800 | [diff] [blame] | 1665 | int i, sum_in_page; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1666 | |
| 1667 | for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) { |
| 1668 | if (sbi->ckpt->alloc_type[i] == SSR) |
| 1669 | valid_sum_count += sbi->blocks_per_seg; |
Chao Yu | 3fa06d7 | 2014-12-09 14:21:46 +0800 | [diff] [blame] | 1670 | else { |
| 1671 | if (for_ra) |
| 1672 | valid_sum_count += le16_to_cpu( |
| 1673 | F2FS_CKPT(sbi)->cur_data_blkoff[i]); |
| 1674 | else |
| 1675 | valid_sum_count += curseg_blkoff(sbi, i); |
| 1676 | } |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1677 | } |
| 1678 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1679 | sum_in_page = (PAGE_SIZE - 2 * SUM_JOURNAL_SIZE - |
Fan Li | 9a47938 | 2013-10-29 16:21:47 +0800 | [diff] [blame] | 1680 | SUM_FOOTER_SIZE) / SUMMARY_SIZE; |
| 1681 | if (valid_sum_count <= sum_in_page) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1682 | return 1; |
Fan Li | 9a47938 | 2013-10-29 16:21:47 +0800 | [diff] [blame] | 1683 | else if ((valid_sum_count - sum_in_page) <= |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1684 | (PAGE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1685 | return 2; |
| 1686 | return 3; |
| 1687 | } |
| 1688 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 1689 | /* |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1690 | * Caller should put this summary page |
| 1691 | */ |
| 1692 | struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno) |
| 1693 | { |
| 1694 | return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno)); |
| 1695 | } |
| 1696 | |
Chao Yu | 381722d | 2015-05-19 17:40:04 +0800 | [diff] [blame] | 1697 | void update_meta_page(struct f2fs_sb_info *sbi, void *src, block_t blk_addr) |
| 1698 | { |
| 1699 | struct page *page = grab_meta_page(sbi, blk_addr); |
| 1700 | void *dst = page_address(page); |
| 1701 | |
| 1702 | if (src) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1703 | memcpy(dst, src, PAGE_SIZE); |
Chao Yu | 381722d | 2015-05-19 17:40:04 +0800 | [diff] [blame] | 1704 | else |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1705 | memset(dst, 0, PAGE_SIZE); |
Chao Yu | 381722d | 2015-05-19 17:40:04 +0800 | [diff] [blame] | 1706 | set_page_dirty(page); |
| 1707 | f2fs_put_page(page, 1); |
| 1708 | } |
| 1709 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1710 | static void write_sum_page(struct f2fs_sb_info *sbi, |
| 1711 | struct f2fs_summary_block *sum_blk, block_t blk_addr) |
| 1712 | { |
Chao Yu | 381722d | 2015-05-19 17:40:04 +0800 | [diff] [blame] | 1713 | update_meta_page(sbi, (void *)sum_blk, blk_addr); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1714 | } |
| 1715 | |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 1716 | static void write_current_sum_page(struct f2fs_sb_info *sbi, |
| 1717 | int type, block_t blk_addr) |
| 1718 | { |
| 1719 | struct curseg_info *curseg = CURSEG_I(sbi, type); |
| 1720 | struct page *page = grab_meta_page(sbi, blk_addr); |
| 1721 | struct f2fs_summary_block *src = curseg->sum_blk; |
| 1722 | struct f2fs_summary_block *dst; |
| 1723 | |
| 1724 | dst = (struct f2fs_summary_block *)page_address(page); |
| 1725 | |
| 1726 | mutex_lock(&curseg->curseg_mutex); |
| 1727 | |
| 1728 | down_read(&curseg->journal_rwsem); |
| 1729 | memcpy(&dst->journal, curseg->journal, SUM_JOURNAL_SIZE); |
| 1730 | up_read(&curseg->journal_rwsem); |
| 1731 | |
| 1732 | memcpy(dst->entries, src->entries, SUM_ENTRY_SIZE); |
| 1733 | memcpy(&dst->footer, &src->footer, SUM_FOOTER_SIZE); |
| 1734 | |
| 1735 | mutex_unlock(&curseg->curseg_mutex); |
| 1736 | |
| 1737 | set_page_dirty(page); |
| 1738 | f2fs_put_page(page, 1); |
| 1739 | } |
| 1740 | |
Jaegeuk Kim | a788189 | 2017-04-20 13:51:57 -0700 | [diff] [blame] | 1741 | static int is_next_segment_free(struct f2fs_sb_info *sbi, int type) |
| 1742 | { |
| 1743 | struct curseg_info *curseg = CURSEG_I(sbi, type); |
| 1744 | unsigned int segno = curseg->segno + 1; |
| 1745 | struct free_segmap_info *free_i = FREE_I(sbi); |
| 1746 | |
| 1747 | if (segno < MAIN_SEGS(sbi) && segno % sbi->segs_per_sec) |
| 1748 | return !test_bit(segno, free_i->free_segmap); |
| 1749 | return 0; |
| 1750 | } |
| 1751 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 1752 | /* |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1753 | * Find a new segment from the free segments bitmap to right order |
| 1754 | * This function should be returned with success, otherwise BUG |
| 1755 | */ |
| 1756 | static void get_new_segment(struct f2fs_sb_info *sbi, |
| 1757 | unsigned int *newseg, bool new_sec, int dir) |
| 1758 | { |
| 1759 | struct free_segmap_info *free_i = FREE_I(sbi); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1760 | unsigned int segno, secno, zoneno; |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 1761 | unsigned int total_zones = MAIN_SECS(sbi) / sbi->secs_per_zone; |
Jaegeuk Kim | 4ddb1a4 | 2017-04-07 15:08:17 -0700 | [diff] [blame] | 1762 | unsigned int hint = GET_SEC_FROM_SEG(sbi, *newseg); |
| 1763 | unsigned int old_zoneno = GET_ZONE_FROM_SEG(sbi, *newseg); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1764 | unsigned int left_start = hint; |
| 1765 | bool init = true; |
| 1766 | int go_left = 0; |
| 1767 | int i; |
| 1768 | |
Chao Yu | 1a118cc | 2015-02-11 18:20:38 +0800 | [diff] [blame] | 1769 | spin_lock(&free_i->segmap_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1770 | |
| 1771 | if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) { |
| 1772 | segno = find_next_zero_bit(free_i->free_segmap, |
Jaegeuk Kim | 4ddb1a4 | 2017-04-07 15:08:17 -0700 | [diff] [blame] | 1773 | GET_SEG_FROM_SEC(sbi, hint + 1), *newseg + 1); |
| 1774 | if (segno < GET_SEG_FROM_SEC(sbi, hint + 1)) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1775 | goto got_it; |
| 1776 | } |
| 1777 | find_other_zone: |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 1778 | secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint); |
| 1779 | if (secno >= MAIN_SECS(sbi)) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1780 | if (dir == ALLOC_RIGHT) { |
| 1781 | secno = find_next_zero_bit(free_i->free_secmap, |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 1782 | MAIN_SECS(sbi), 0); |
| 1783 | f2fs_bug_on(sbi, secno >= MAIN_SECS(sbi)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1784 | } else { |
| 1785 | go_left = 1; |
| 1786 | left_start = hint - 1; |
| 1787 | } |
| 1788 | } |
| 1789 | if (go_left == 0) |
| 1790 | goto skip_left; |
| 1791 | |
| 1792 | while (test_bit(left_start, free_i->free_secmap)) { |
| 1793 | if (left_start > 0) { |
| 1794 | left_start--; |
| 1795 | continue; |
| 1796 | } |
| 1797 | left_start = find_next_zero_bit(free_i->free_secmap, |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 1798 | MAIN_SECS(sbi), 0); |
| 1799 | f2fs_bug_on(sbi, left_start >= MAIN_SECS(sbi)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1800 | break; |
| 1801 | } |
| 1802 | secno = left_start; |
| 1803 | skip_left: |
| 1804 | hint = secno; |
Jaegeuk Kim | 4ddb1a4 | 2017-04-07 15:08:17 -0700 | [diff] [blame] | 1805 | segno = GET_SEG_FROM_SEC(sbi, secno); |
| 1806 | zoneno = GET_ZONE_FROM_SEC(sbi, secno); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1807 | |
| 1808 | /* give up on finding another zone */ |
| 1809 | if (!init) |
| 1810 | goto got_it; |
| 1811 | if (sbi->secs_per_zone == 1) |
| 1812 | goto got_it; |
| 1813 | if (zoneno == old_zoneno) |
| 1814 | goto got_it; |
| 1815 | if (dir == ALLOC_LEFT) { |
| 1816 | if (!go_left && zoneno + 1 >= total_zones) |
| 1817 | goto got_it; |
| 1818 | if (go_left && zoneno == 0) |
| 1819 | goto got_it; |
| 1820 | } |
| 1821 | for (i = 0; i < NR_CURSEG_TYPE; i++) |
| 1822 | if (CURSEG_I(sbi, i)->zone == zoneno) |
| 1823 | break; |
| 1824 | |
| 1825 | if (i < NR_CURSEG_TYPE) { |
| 1826 | /* zone is in user, try another */ |
| 1827 | if (go_left) |
| 1828 | hint = zoneno * sbi->secs_per_zone - 1; |
| 1829 | else if (zoneno + 1 >= total_zones) |
| 1830 | hint = 0; |
| 1831 | else |
| 1832 | hint = (zoneno + 1) * sbi->secs_per_zone; |
| 1833 | init = false; |
| 1834 | goto find_other_zone; |
| 1835 | } |
| 1836 | got_it: |
| 1837 | /* set it as dirty segment in free segmap */ |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 1838 | f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1839 | __set_inuse(sbi, segno); |
| 1840 | *newseg = segno; |
Chao Yu | 1a118cc | 2015-02-11 18:20:38 +0800 | [diff] [blame] | 1841 | spin_unlock(&free_i->segmap_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1842 | } |
| 1843 | |
| 1844 | static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified) |
| 1845 | { |
| 1846 | struct curseg_info *curseg = CURSEG_I(sbi, type); |
| 1847 | struct summary_footer *sum_footer; |
| 1848 | |
| 1849 | curseg->segno = curseg->next_segno; |
Jaegeuk Kim | 4ddb1a4 | 2017-04-07 15:08:17 -0700 | [diff] [blame] | 1850 | curseg->zone = GET_ZONE_FROM_SEG(sbi, curseg->segno); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1851 | curseg->next_blkoff = 0; |
| 1852 | curseg->next_segno = NULL_SEGNO; |
| 1853 | |
| 1854 | sum_footer = &(curseg->sum_blk->footer); |
| 1855 | memset(sum_footer, 0, sizeof(struct summary_footer)); |
| 1856 | if (IS_DATASEG(type)) |
| 1857 | SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA); |
| 1858 | if (IS_NODESEG(type)) |
| 1859 | SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE); |
| 1860 | __set_sit_entry_type(sbi, type, curseg->segno, modified); |
| 1861 | } |
| 1862 | |
Jaegeuk Kim | 7a20b8a | 2017-03-24 20:41:45 -0400 | [diff] [blame] | 1863 | static unsigned int __get_next_segno(struct f2fs_sb_info *sbi, int type) |
| 1864 | { |
Jaegeuk Kim | a788189 | 2017-04-20 13:51:57 -0700 | [diff] [blame] | 1865 | /* if segs_per_sec is large than 1, we need to keep original policy. */ |
| 1866 | if (sbi->segs_per_sec != 1) |
| 1867 | return CURSEG_I(sbi, type)->segno; |
| 1868 | |
Jaegeuk Kim | 7a20b8a | 2017-03-24 20:41:45 -0400 | [diff] [blame] | 1869 | if (type == CURSEG_HOT_DATA || IS_NODESEG(type)) |
| 1870 | return 0; |
| 1871 | |
Jaegeuk Kim | e066b83 | 2017-04-13 15:17:00 -0700 | [diff] [blame] | 1872 | if (SIT_I(sbi)->last_victim[ALLOC_NEXT]) |
| 1873 | return SIT_I(sbi)->last_victim[ALLOC_NEXT]; |
Jaegeuk Kim | 7a20b8a | 2017-03-24 20:41:45 -0400 | [diff] [blame] | 1874 | return CURSEG_I(sbi, type)->segno; |
| 1875 | } |
| 1876 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 1877 | /* |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1878 | * Allocate a current working segment. |
| 1879 | * This function always allocates a free segment in LFS manner. |
| 1880 | */ |
| 1881 | static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec) |
| 1882 | { |
| 1883 | struct curseg_info *curseg = CURSEG_I(sbi, type); |
| 1884 | unsigned int segno = curseg->segno; |
| 1885 | int dir = ALLOC_LEFT; |
| 1886 | |
| 1887 | write_sum_page(sbi, curseg->sum_blk, |
Haicheng Li | 81fb5e8 | 2013-05-14 18:20:28 +0800 | [diff] [blame] | 1888 | GET_SUM_BLOCK(sbi, segno)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1889 | if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA) |
| 1890 | dir = ALLOC_RIGHT; |
| 1891 | |
| 1892 | if (test_opt(sbi, NOHEAP)) |
| 1893 | dir = ALLOC_RIGHT; |
| 1894 | |
Jaegeuk Kim | 7a20b8a | 2017-03-24 20:41:45 -0400 | [diff] [blame] | 1895 | segno = __get_next_segno(sbi, type); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1896 | get_new_segment(sbi, &segno, new_sec, dir); |
| 1897 | curseg->next_segno = segno; |
| 1898 | reset_curseg(sbi, type, 1); |
| 1899 | curseg->alloc_type = LFS; |
| 1900 | } |
| 1901 | |
| 1902 | static void __next_free_blkoff(struct f2fs_sb_info *sbi, |
| 1903 | struct curseg_info *seg, block_t start) |
| 1904 | { |
| 1905 | struct seg_entry *se = get_seg_entry(sbi, seg->segno); |
Changman Lee | e81c93c | 2013-11-15 13:21:16 +0900 | [diff] [blame] | 1906 | int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long); |
Jaegeuk Kim | 60a3b78 | 2015-02-10 16:44:29 -0800 | [diff] [blame] | 1907 | unsigned long *target_map = SIT_I(sbi)->tmp_map; |
Changman Lee | e81c93c | 2013-11-15 13:21:16 +0900 | [diff] [blame] | 1908 | unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map; |
| 1909 | unsigned long *cur_map = (unsigned long *)se->cur_valid_map; |
| 1910 | int i, pos; |
| 1911 | |
| 1912 | for (i = 0; i < entries; i++) |
| 1913 | target_map[i] = ckpt_map[i] | cur_map[i]; |
| 1914 | |
| 1915 | pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start); |
| 1916 | |
| 1917 | seg->next_blkoff = pos; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1918 | } |
| 1919 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 1920 | /* |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1921 | * If a segment is written by LFS manner, next block offset is just obtained |
| 1922 | * by increasing the current block offset. However, if a segment is written by |
| 1923 | * SSR manner, next block offset obtained by calling __next_free_blkoff |
| 1924 | */ |
| 1925 | static void __refresh_next_blkoff(struct f2fs_sb_info *sbi, |
| 1926 | struct curseg_info *seg) |
| 1927 | { |
| 1928 | if (seg->alloc_type == SSR) |
| 1929 | __next_free_blkoff(sbi, seg, seg->next_blkoff + 1); |
| 1930 | else |
| 1931 | seg->next_blkoff++; |
| 1932 | } |
| 1933 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 1934 | /* |
arter97 | e1c4204 | 2014-08-06 23:22:50 +0900 | [diff] [blame] | 1935 | * This function always allocates a used segment(from dirty seglist) by SSR |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1936 | * manner, so it should recover the existing segment information of valid blocks |
| 1937 | */ |
| 1938 | static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse) |
| 1939 | { |
| 1940 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
| 1941 | struct curseg_info *curseg = CURSEG_I(sbi, type); |
| 1942 | unsigned int new_segno = curseg->next_segno; |
| 1943 | struct f2fs_summary_block *sum_node; |
| 1944 | struct page *sum_page; |
| 1945 | |
| 1946 | write_sum_page(sbi, curseg->sum_blk, |
| 1947 | GET_SUM_BLOCK(sbi, curseg->segno)); |
| 1948 | __set_test_and_inuse(sbi, new_segno); |
| 1949 | |
| 1950 | mutex_lock(&dirty_i->seglist_lock); |
| 1951 | __remove_dirty_segment(sbi, new_segno, PRE); |
| 1952 | __remove_dirty_segment(sbi, new_segno, DIRTY); |
| 1953 | mutex_unlock(&dirty_i->seglist_lock); |
| 1954 | |
| 1955 | reset_curseg(sbi, type, 1); |
| 1956 | curseg->alloc_type = SSR; |
| 1957 | __next_free_blkoff(sbi, curseg, 0); |
| 1958 | |
| 1959 | if (reuse) { |
| 1960 | sum_page = get_sum_page(sbi, new_segno); |
| 1961 | sum_node = (struct f2fs_summary_block *)page_address(sum_page); |
| 1962 | memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE); |
| 1963 | f2fs_put_page(sum_page, 1); |
| 1964 | } |
| 1965 | } |
| 1966 | |
Jaegeuk Kim | 4372752 | 2013-02-04 15:11:17 +0900 | [diff] [blame] | 1967 | static int get_ssr_segment(struct f2fs_sb_info *sbi, int type) |
| 1968 | { |
| 1969 | struct curseg_info *curseg = CURSEG_I(sbi, type); |
| 1970 | const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops; |
Jaegeuk Kim | e066b83 | 2017-04-13 15:17:00 -0700 | [diff] [blame] | 1971 | unsigned segno = NULL_SEGNO; |
Chao Yu | d27c3d8 | 2017-02-24 18:46:00 +0800 | [diff] [blame] | 1972 | int i, cnt; |
| 1973 | bool reversed = false; |
Jaegeuk Kim | c192f7a | 2017-02-22 17:10:18 -0800 | [diff] [blame] | 1974 | |
| 1975 | /* need_SSR() already forces to do this */ |
Jaegeuk Kim | e066b83 | 2017-04-13 15:17:00 -0700 | [diff] [blame] | 1976 | if (v_ops->get_victim(sbi, &segno, BG_GC, type, SSR)) { |
| 1977 | curseg->next_segno = segno; |
Jaegeuk Kim | c192f7a | 2017-02-22 17:10:18 -0800 | [diff] [blame] | 1978 | return 1; |
Jaegeuk Kim | e066b83 | 2017-04-13 15:17:00 -0700 | [diff] [blame] | 1979 | } |
Jaegeuk Kim | 4372752 | 2013-02-04 15:11:17 +0900 | [diff] [blame] | 1980 | |
Jaegeuk Kim | 70d625c | 2017-02-22 17:02:32 -0800 | [diff] [blame] | 1981 | /* For node segments, let's do SSR more intensively */ |
| 1982 | if (IS_NODESEG(type)) { |
Chao Yu | d27c3d8 | 2017-02-24 18:46:00 +0800 | [diff] [blame] | 1983 | if (type >= CURSEG_WARM_NODE) { |
| 1984 | reversed = true; |
| 1985 | i = CURSEG_COLD_NODE; |
| 1986 | } else { |
| 1987 | i = CURSEG_HOT_NODE; |
| 1988 | } |
| 1989 | cnt = NR_CURSEG_NODE_TYPE; |
Jaegeuk Kim | 70d625c | 2017-02-22 17:02:32 -0800 | [diff] [blame] | 1990 | } else { |
Chao Yu | d27c3d8 | 2017-02-24 18:46:00 +0800 | [diff] [blame] | 1991 | if (type >= CURSEG_WARM_DATA) { |
| 1992 | reversed = true; |
| 1993 | i = CURSEG_COLD_DATA; |
| 1994 | } else { |
| 1995 | i = CURSEG_HOT_DATA; |
| 1996 | } |
| 1997 | cnt = NR_CURSEG_DATA_TYPE; |
Jaegeuk Kim | 70d625c | 2017-02-22 17:02:32 -0800 | [diff] [blame] | 1998 | } |
Jaegeuk Kim | 4372752 | 2013-02-04 15:11:17 +0900 | [diff] [blame] | 1999 | |
Chao Yu | d27c3d8 | 2017-02-24 18:46:00 +0800 | [diff] [blame] | 2000 | for (; cnt-- > 0; reversed ? i-- : i++) { |
Jaegeuk Kim | c192f7a | 2017-02-22 17:10:18 -0800 | [diff] [blame] | 2001 | if (i == type) |
| 2002 | continue; |
Jaegeuk Kim | e066b83 | 2017-04-13 15:17:00 -0700 | [diff] [blame] | 2003 | if (v_ops->get_victim(sbi, &segno, BG_GC, i, SSR)) { |
| 2004 | curseg->next_segno = segno; |
Jaegeuk Kim | 4372752 | 2013-02-04 15:11:17 +0900 | [diff] [blame] | 2005 | return 1; |
Jaegeuk Kim | e066b83 | 2017-04-13 15:17:00 -0700 | [diff] [blame] | 2006 | } |
Jaegeuk Kim | c192f7a | 2017-02-22 17:10:18 -0800 | [diff] [blame] | 2007 | } |
Jaegeuk Kim | 4372752 | 2013-02-04 15:11:17 +0900 | [diff] [blame] | 2008 | return 0; |
| 2009 | } |
| 2010 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2011 | /* |
| 2012 | * flush out current segment and replace it with new segment |
| 2013 | * This function should be returned with success, otherwise BUG |
| 2014 | */ |
| 2015 | static void allocate_segment_by_default(struct f2fs_sb_info *sbi, |
| 2016 | int type, bool force) |
| 2017 | { |
Jaegeuk Kim | a788189 | 2017-04-20 13:51:57 -0700 | [diff] [blame] | 2018 | struct curseg_info *curseg = CURSEG_I(sbi, type); |
| 2019 | |
Gu Zheng | 7b40527 | 2013-08-19 09:41:15 +0800 | [diff] [blame] | 2020 | if (force) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2021 | new_curseg(sbi, type, true); |
Jaegeuk Kim | 5b6c6be | 2017-02-14 19:32:51 -0800 | [diff] [blame] | 2022 | else if (!is_set_ckpt_flags(sbi, CP_CRC_RECOVERY_FLAG) && |
| 2023 | type == CURSEG_WARM_NODE) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2024 | new_curseg(sbi, type, false); |
Jaegeuk Kim | a788189 | 2017-04-20 13:51:57 -0700 | [diff] [blame] | 2025 | else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type)) |
| 2026 | new_curseg(sbi, type, false); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2027 | else if (need_SSR(sbi) && get_ssr_segment(sbi, type)) |
| 2028 | change_curseg(sbi, type, true); |
| 2029 | else |
| 2030 | new_curseg(sbi, type, false); |
Jaegeuk Kim | dcdfff6 | 2013-10-22 20:56:10 +0900 | [diff] [blame] | 2031 | |
Jaegeuk Kim | a788189 | 2017-04-20 13:51:57 -0700 | [diff] [blame] | 2032 | stat_inc_seg_type(sbi, curseg); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2033 | } |
| 2034 | |
| 2035 | void allocate_new_segments(struct f2fs_sb_info *sbi) |
| 2036 | { |
Jaegeuk Kim | 6ae1be1 | 2016-11-11 12:31:40 -0800 | [diff] [blame] | 2037 | struct curseg_info *curseg; |
| 2038 | unsigned int old_segno; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2039 | int i; |
| 2040 | |
Jaegeuk Kim | 6ae1be1 | 2016-11-11 12:31:40 -0800 | [diff] [blame] | 2041 | for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) { |
| 2042 | curseg = CURSEG_I(sbi, i); |
| 2043 | old_segno = curseg->segno; |
| 2044 | SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true); |
| 2045 | locate_dirty_segment(sbi, old_segno); |
| 2046 | } |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2047 | } |
| 2048 | |
| 2049 | static const struct segment_allocation default_salloc_ops = { |
| 2050 | .allocate_segment = allocate_segment_by_default, |
| 2051 | }; |
| 2052 | |
Jaegeuk Kim | 25290fa | 2016-12-29 22:06:15 -0800 | [diff] [blame] | 2053 | bool exist_trim_candidates(struct f2fs_sb_info *sbi, struct cp_control *cpc) |
| 2054 | { |
| 2055 | __u64 trim_start = cpc->trim_start; |
| 2056 | bool has_candidate = false; |
| 2057 | |
| 2058 | mutex_lock(&SIT_I(sbi)->sentry_lock); |
| 2059 | for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++) { |
| 2060 | if (add_discard_addrs(sbi, cpc, true)) { |
| 2061 | has_candidate = true; |
| 2062 | break; |
| 2063 | } |
| 2064 | } |
| 2065 | mutex_unlock(&SIT_I(sbi)->sentry_lock); |
| 2066 | |
| 2067 | cpc->trim_start = trim_start; |
| 2068 | return has_candidate; |
| 2069 | } |
| 2070 | |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 2071 | int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range) |
| 2072 | { |
Jaegeuk Kim | f7ef9b8 | 2015-02-09 12:02:44 -0800 | [diff] [blame] | 2073 | __u64 start = F2FS_BYTES_TO_BLK(range->start); |
| 2074 | __u64 end = start + F2FS_BYTES_TO_BLK(range->len) - 1; |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 2075 | unsigned int start_segno, end_segno; |
| 2076 | struct cp_control cpc; |
Chao Yu | c34f42e | 2015-12-23 17:50:30 +0800 | [diff] [blame] | 2077 | int err = 0; |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 2078 | |
Jaegeuk Kim | 836b5a6 | 2015-04-30 22:50:06 -0700 | [diff] [blame] | 2079 | if (start >= MAX_BLKADDR(sbi) || range->len < sbi->blocksize) |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 2080 | return -EINVAL; |
| 2081 | |
Jan Kara | 9bd27ae | 2014-10-21 14:07:33 +0200 | [diff] [blame] | 2082 | cpc.trimmed = 0; |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 2083 | if (end <= MAIN_BLKADDR(sbi)) |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 2084 | goto out; |
| 2085 | |
Yunlei He | ed214a1 | 2016-09-01 10:14:39 +0800 | [diff] [blame] | 2086 | if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) { |
| 2087 | f2fs_msg(sbi->sb, KERN_WARNING, |
| 2088 | "Found FS corruption, run fsck to fix."); |
| 2089 | goto out; |
| 2090 | } |
| 2091 | |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 2092 | /* start/end segment number in main_area */ |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 2093 | start_segno = (start <= MAIN_BLKADDR(sbi)) ? 0 : GET_SEGNO(sbi, start); |
| 2094 | end_segno = (end >= MAX_BLKADDR(sbi)) ? MAIN_SEGS(sbi) - 1 : |
| 2095 | GET_SEGNO(sbi, end); |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 2096 | cpc.reason = CP_DISCARD; |
Jaegeuk Kim | 836b5a6 | 2015-04-30 22:50:06 -0700 | [diff] [blame] | 2097 | cpc.trim_minlen = max_t(__u64, 1, F2FS_BYTES_TO_BLK(range->minlen)); |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 2098 | |
| 2099 | /* do checkpoint to issue discard commands safely */ |
Jaegeuk Kim | bba681c | 2015-01-26 17:41:23 -0800 | [diff] [blame] | 2100 | for (; start_segno <= end_segno; start_segno = cpc.trim_end + 1) { |
| 2101 | cpc.trim_start = start_segno; |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 2102 | |
| 2103 | if (sbi->discard_blks == 0) |
| 2104 | break; |
| 2105 | else if (sbi->discard_blks < BATCHED_TRIM_BLOCKS(sbi)) |
| 2106 | cpc.trim_end = end_segno; |
| 2107 | else |
| 2108 | cpc.trim_end = min_t(unsigned int, |
| 2109 | rounddown(start_segno + |
Jaegeuk Kim | bba681c | 2015-01-26 17:41:23 -0800 | [diff] [blame] | 2110 | BATCHED_TRIM_SEGMENTS(sbi), |
| 2111 | sbi->segs_per_sec) - 1, end_segno); |
| 2112 | |
| 2113 | mutex_lock(&sbi->gc_mutex); |
Chao Yu | c34f42e | 2015-12-23 17:50:30 +0800 | [diff] [blame] | 2114 | err = write_checkpoint(sbi, &cpc); |
Jaegeuk Kim | bba681c | 2015-01-26 17:41:23 -0800 | [diff] [blame] | 2115 | mutex_unlock(&sbi->gc_mutex); |
Chao Yu | e932835 | 2016-08-21 23:21:29 +0800 | [diff] [blame] | 2116 | if (err) |
| 2117 | break; |
Chao Yu | 74fa5f3 | 2016-08-21 23:21:30 +0800 | [diff] [blame] | 2118 | |
| 2119 | schedule(); |
Jaegeuk Kim | bba681c | 2015-01-26 17:41:23 -0800 | [diff] [blame] | 2120 | } |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 2121 | out: |
Jaegeuk Kim | f7ef9b8 | 2015-02-09 12:02:44 -0800 | [diff] [blame] | 2122 | range->len = F2FS_BLK_TO_BYTES(cpc.trimmed); |
Chao Yu | c34f42e | 2015-12-23 17:50:30 +0800 | [diff] [blame] | 2123 | return err; |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 2124 | } |
| 2125 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2126 | static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type) |
| 2127 | { |
| 2128 | struct curseg_info *curseg = CURSEG_I(sbi, type); |
| 2129 | if (curseg->next_blkoff < sbi->blocks_per_seg) |
| 2130 | return true; |
| 2131 | return false; |
| 2132 | } |
| 2133 | |
Jaegeuk Kim | 81377bd | 2017-05-10 14:19:54 -0700 | [diff] [blame] | 2134 | static int __get_segment_type_2(struct f2fs_io_info *fio) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2135 | { |
Jaegeuk Kim | 81377bd | 2017-05-10 14:19:54 -0700 | [diff] [blame] | 2136 | if (fio->type == DATA) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2137 | return CURSEG_HOT_DATA; |
| 2138 | else |
| 2139 | return CURSEG_HOT_NODE; |
| 2140 | } |
| 2141 | |
Jaegeuk Kim | 81377bd | 2017-05-10 14:19:54 -0700 | [diff] [blame] | 2142 | static int __get_segment_type_4(struct f2fs_io_info *fio) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2143 | { |
Jaegeuk Kim | 81377bd | 2017-05-10 14:19:54 -0700 | [diff] [blame] | 2144 | if (fio->type == DATA) { |
| 2145 | struct inode *inode = fio->page->mapping->host; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2146 | |
| 2147 | if (S_ISDIR(inode->i_mode)) |
| 2148 | return CURSEG_HOT_DATA; |
| 2149 | else |
| 2150 | return CURSEG_COLD_DATA; |
| 2151 | } else { |
Jaegeuk Kim | 81377bd | 2017-05-10 14:19:54 -0700 | [diff] [blame] | 2152 | if (IS_DNODE(fio->page) && is_cold_node(fio->page)) |
Jaegeuk Kim | a344b9f | 2014-11-05 20:05:53 -0800 | [diff] [blame] | 2153 | return CURSEG_WARM_NODE; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2154 | else |
| 2155 | return CURSEG_COLD_NODE; |
| 2156 | } |
| 2157 | } |
| 2158 | |
Jaegeuk Kim | 81377bd | 2017-05-10 14:19:54 -0700 | [diff] [blame] | 2159 | static int __get_segment_type_6(struct f2fs_io_info *fio) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2160 | { |
Jaegeuk Kim | 81377bd | 2017-05-10 14:19:54 -0700 | [diff] [blame] | 2161 | if (fio->type == DATA) { |
| 2162 | struct inode *inode = fio->page->mapping->host; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2163 | |
Jaegeuk Kim | 81377bd | 2017-05-10 14:19:54 -0700 | [diff] [blame] | 2164 | if (is_cold_data(fio->page) || file_is_cold(inode)) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2165 | return CURSEG_COLD_DATA; |
Jaegeuk Kim | ef095d1 | 2017-03-24 20:05:13 -0400 | [diff] [blame] | 2166 | if (is_inode_flag_set(inode, FI_HOT_DATA)) |
| 2167 | return CURSEG_HOT_DATA; |
| 2168 | return CURSEG_WARM_DATA; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2169 | } else { |
Jaegeuk Kim | 81377bd | 2017-05-10 14:19:54 -0700 | [diff] [blame] | 2170 | if (IS_DNODE(fio->page)) |
| 2171 | return is_cold_node(fio->page) ? CURSEG_WARM_NODE : |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2172 | CURSEG_HOT_NODE; |
Jaegeuk Kim | ef095d1 | 2017-03-24 20:05:13 -0400 | [diff] [blame] | 2173 | return CURSEG_COLD_NODE; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2174 | } |
| 2175 | } |
| 2176 | |
Jaegeuk Kim | 81377bd | 2017-05-10 14:19:54 -0700 | [diff] [blame] | 2177 | static int __get_segment_type(struct f2fs_io_info *fio) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2178 | { |
Jaegeuk Kim | a912b54 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 2179 | int type = 0; |
| 2180 | |
Jaegeuk Kim | 81377bd | 2017-05-10 14:19:54 -0700 | [diff] [blame] | 2181 | switch (fio->sbi->active_logs) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2182 | case 2: |
Jaegeuk Kim | a912b54 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 2183 | type = __get_segment_type_2(fio); |
| 2184 | break; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2185 | case 4: |
Jaegeuk Kim | a912b54 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 2186 | type = __get_segment_type_4(fio); |
| 2187 | break; |
| 2188 | case 6: |
| 2189 | type = __get_segment_type_6(fio); |
| 2190 | break; |
| 2191 | default: |
| 2192 | f2fs_bug_on(fio->sbi, true); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2193 | } |
Jaegeuk Kim | 81377bd | 2017-05-10 14:19:54 -0700 | [diff] [blame] | 2194 | |
Jaegeuk Kim | a912b54 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 2195 | if (IS_HOT(type)) |
| 2196 | fio->temp = HOT; |
| 2197 | else if (IS_WARM(type)) |
| 2198 | fio->temp = WARM; |
| 2199 | else |
| 2200 | fio->temp = COLD; |
| 2201 | return type; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2202 | } |
| 2203 | |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 2204 | void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page, |
| 2205 | block_t old_blkaddr, block_t *new_blkaddr, |
Chao Yu | fb830fc | 2017-05-19 23:37:01 +0800 | [diff] [blame] | 2206 | struct f2fs_summary *sum, int type, |
| 2207 | struct f2fs_io_info *fio, bool add_list) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2208 | { |
| 2209 | struct sit_info *sit_i = SIT_I(sbi); |
Jaegeuk Kim | 6ae1be1 | 2016-11-11 12:31:40 -0800 | [diff] [blame] | 2210 | struct curseg_info *curseg = CURSEG_I(sbi, type); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2211 | |
| 2212 | mutex_lock(&curseg->curseg_mutex); |
Jaegeuk Kim | 21cb1d9 | 2015-03-11 13:42:48 -0400 | [diff] [blame] | 2213 | mutex_lock(&sit_i->sentry_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2214 | |
| 2215 | *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2216 | |
Jaegeuk Kim | 4e6a8d9 | 2016-12-29 14:07:53 -0800 | [diff] [blame] | 2217 | f2fs_wait_discard_bio(sbi, *new_blkaddr); |
| 2218 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2219 | /* |
| 2220 | * __add_sum_entry should be resided under the curseg_mutex |
| 2221 | * because, this function updates a summary entry in the |
| 2222 | * current summary block. |
| 2223 | */ |
Haicheng Li | e79efe3 | 2013-06-13 16:59:27 +0800 | [diff] [blame] | 2224 | __add_sum_entry(sbi, type, sum); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2225 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2226 | __refresh_next_blkoff(sbi, curseg); |
Jaegeuk Kim | dcdfff6 | 2013-10-22 20:56:10 +0900 | [diff] [blame] | 2227 | |
| 2228 | stat_inc_block_count(sbi, curseg); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2229 | |
Yunlong Song | 3436c4b | 2017-02-21 16:59:26 +0800 | [diff] [blame] | 2230 | if (!__has_curseg_space(sbi, type)) |
| 2231 | sit_i->s_ops->allocate_segment(sbi, type, false); |
Jaegeuk Kim | c6f82fe9 | 2017-04-04 16:45:30 -0700 | [diff] [blame] | 2232 | /* |
| 2233 | * SIT information should be updated after segment allocation, |
| 2234 | * since we need to keep dirty segments precisely under SSR. |
| 2235 | */ |
| 2236 | refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr); |
Yunlong Song | 3436c4b | 2017-02-21 16:59:26 +0800 | [diff] [blame] | 2237 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2238 | mutex_unlock(&sit_i->sentry_lock); |
| 2239 | |
Chao Yu | 704956e | 2017-07-31 20:19:09 +0800 | [diff] [blame] | 2240 | if (page && IS_NODESEG(type)) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2241 | fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg)); |
| 2242 | |
Chao Yu | 704956e | 2017-07-31 20:19:09 +0800 | [diff] [blame] | 2243 | f2fs_inode_chksum_set(sbi, page); |
| 2244 | } |
| 2245 | |
Chao Yu | fb830fc | 2017-05-19 23:37:01 +0800 | [diff] [blame] | 2246 | if (add_list) { |
| 2247 | struct f2fs_bio_info *io; |
| 2248 | |
| 2249 | INIT_LIST_HEAD(&fio->list); |
| 2250 | fio->in_list = true; |
| 2251 | io = sbi->write_io[fio->type] + fio->temp; |
| 2252 | spin_lock(&io->io_lock); |
| 2253 | list_add_tail(&fio->list, &io->io_list); |
| 2254 | spin_unlock(&io->io_lock); |
| 2255 | } |
| 2256 | |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 2257 | mutex_unlock(&curseg->curseg_mutex); |
| 2258 | } |
| 2259 | |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 2260 | 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] | 2261 | { |
Jaegeuk Kim | 81377bd | 2017-05-10 14:19:54 -0700 | [diff] [blame] | 2262 | int type = __get_segment_type(fio); |
Jaegeuk Kim | 0a595eb | 2016-12-14 10:12:56 -0800 | [diff] [blame] | 2263 | int err; |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 2264 | |
Jaegeuk Kim | 0a595eb | 2016-12-14 10:12:56 -0800 | [diff] [blame] | 2265 | reallocate: |
Chao Yu | 7a9d754 | 2016-02-22 18:36:38 +0800 | [diff] [blame] | 2266 | allocate_data_block(fio->sbi, fio->page, fio->old_blkaddr, |
Chao Yu | fb830fc | 2017-05-19 23:37:01 +0800 | [diff] [blame] | 2267 | &fio->new_blkaddr, sum, type, fio, true); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 2268 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2269 | /* writeout dirty page into bdev */ |
Jaegeuk Kim | b9109b0 | 2017-05-10 11:28:38 -0700 | [diff] [blame] | 2270 | err = f2fs_submit_page_write(fio); |
Jaegeuk Kim | 0a595eb | 2016-12-14 10:12:56 -0800 | [diff] [blame] | 2271 | if (err == -EAGAIN) { |
| 2272 | fio->old_blkaddr = fio->new_blkaddr; |
| 2273 | goto reallocate; |
| 2274 | } |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2275 | } |
| 2276 | |
Chao Yu | b0af6d4 | 2017-08-02 23:21:48 +0800 | [diff] [blame^] | 2277 | void write_meta_page(struct f2fs_sb_info *sbi, struct page *page, |
| 2278 | enum iostat_type io_type) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2279 | { |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 2280 | struct f2fs_io_info fio = { |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 2281 | .sbi = sbi, |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 2282 | .type = META, |
Mike Christie | 04d328d | 2016-06-05 14:31:55 -0500 | [diff] [blame] | 2283 | .op = REQ_OP_WRITE, |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 2284 | .op_flags = REQ_SYNC | REQ_META | REQ_PRIO, |
Chao Yu | 7a9d754 | 2016-02-22 18:36:38 +0800 | [diff] [blame] | 2285 | .old_blkaddr = page->index, |
| 2286 | .new_blkaddr = page->index, |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 2287 | .page = page, |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 2288 | .encrypted_page = NULL, |
Chao Yu | fb830fc | 2017-05-19 23:37:01 +0800 | [diff] [blame] | 2289 | .in_list = false, |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 2290 | }; |
| 2291 | |
Chao Yu | 2b94700 | 2015-10-12 17:04:21 +0800 | [diff] [blame] | 2292 | if (unlikely(page->index >= MAIN_BLKADDR(sbi))) |
Mike Christie | 04d328d | 2016-06-05 14:31:55 -0500 | [diff] [blame] | 2293 | fio.op_flags &= ~REQ_META; |
Chao Yu | 2b94700 | 2015-10-12 17:04:21 +0800 | [diff] [blame] | 2294 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2295 | set_page_writeback(page); |
Jaegeuk Kim | b9109b0 | 2017-05-10 11:28:38 -0700 | [diff] [blame] | 2296 | f2fs_submit_page_write(&fio); |
Chao Yu | b0af6d4 | 2017-08-02 23:21:48 +0800 | [diff] [blame^] | 2297 | |
| 2298 | f2fs_update_iostat(sbi, io_type, F2FS_BLKSIZE); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2299 | } |
| 2300 | |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 2301 | void write_node_page(unsigned int nid, struct f2fs_io_info *fio) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2302 | { |
| 2303 | struct f2fs_summary sum; |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 2304 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2305 | set_summary(&sum, nid, 0, 0); |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 2306 | do_write_page(&sum, fio); |
Chao Yu | b0af6d4 | 2017-08-02 23:21:48 +0800 | [diff] [blame^] | 2307 | |
| 2308 | f2fs_update_iostat(fio->sbi, fio->io_type, F2FS_BLKSIZE); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2309 | } |
| 2310 | |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 2311 | void write_data_page(struct dnode_of_data *dn, struct f2fs_io_info *fio) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2312 | { |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 2313 | struct f2fs_sb_info *sbi = fio->sbi; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2314 | struct f2fs_summary sum; |
| 2315 | struct node_info ni; |
| 2316 | |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 2317 | f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2318 | get_node_info(sbi, dn->nid, &ni); |
| 2319 | set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version); |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 2320 | do_write_page(&sum, fio); |
Chao Yu | f28b343 | 2016-02-24 17:16:47 +0800 | [diff] [blame] | 2321 | f2fs_update_data_blkaddr(dn, fio->new_blkaddr); |
Chao Yu | b0af6d4 | 2017-08-02 23:21:48 +0800 | [diff] [blame^] | 2322 | |
| 2323 | f2fs_update_iostat(sbi, fio->io_type, F2FS_BLKSIZE); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2324 | } |
| 2325 | |
Jaegeuk Kim | d1b3e72 | 2017-03-30 21:02:46 -0700 | [diff] [blame] | 2326 | int rewrite_data_page(struct f2fs_io_info *fio) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2327 | { |
Chao Yu | b0af6d4 | 2017-08-02 23:21:48 +0800 | [diff] [blame^] | 2328 | int err; |
| 2329 | |
Chao Yu | 7a9d754 | 2016-02-22 18:36:38 +0800 | [diff] [blame] | 2330 | fio->new_blkaddr = fio->old_blkaddr; |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 2331 | stat_inc_inplace_blocks(fio->sbi); |
Chao Yu | b0af6d4 | 2017-08-02 23:21:48 +0800 | [diff] [blame^] | 2332 | |
| 2333 | err = f2fs_submit_page_bio(fio); |
| 2334 | |
| 2335 | f2fs_update_iostat(fio->sbi, fio->io_type, F2FS_BLKSIZE); |
| 2336 | |
| 2337 | return err; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2338 | } |
| 2339 | |
Chao Yu | 4356e48 | 2016-02-23 17:52:43 +0800 | [diff] [blame] | 2340 | void __f2fs_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum, |
Chao Yu | 19f106b | 2015-05-06 13:08:06 +0800 | [diff] [blame] | 2341 | block_t old_blkaddr, block_t new_blkaddr, |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 2342 | bool recover_curseg, bool recover_newaddr) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2343 | { |
| 2344 | struct sit_info *sit_i = SIT_I(sbi); |
| 2345 | struct curseg_info *curseg; |
| 2346 | unsigned int segno, old_cursegno; |
| 2347 | struct seg_entry *se; |
| 2348 | int type; |
Chao Yu | 19f106b | 2015-05-06 13:08:06 +0800 | [diff] [blame] | 2349 | unsigned short old_blkoff; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2350 | |
| 2351 | segno = GET_SEGNO(sbi, new_blkaddr); |
| 2352 | se = get_seg_entry(sbi, segno); |
| 2353 | type = se->type; |
| 2354 | |
Chao Yu | 19f106b | 2015-05-06 13:08:06 +0800 | [diff] [blame] | 2355 | if (!recover_curseg) { |
| 2356 | /* for recovery flow */ |
| 2357 | if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) { |
| 2358 | if (old_blkaddr == NULL_ADDR) |
| 2359 | type = CURSEG_COLD_DATA; |
| 2360 | else |
| 2361 | type = CURSEG_WARM_DATA; |
| 2362 | } |
| 2363 | } else { |
| 2364 | if (!IS_CURSEG(sbi, segno)) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2365 | type = CURSEG_WARM_DATA; |
| 2366 | } |
Chao Yu | 19f106b | 2015-05-06 13:08:06 +0800 | [diff] [blame] | 2367 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2368 | curseg = CURSEG_I(sbi, type); |
| 2369 | |
| 2370 | mutex_lock(&curseg->curseg_mutex); |
| 2371 | mutex_lock(&sit_i->sentry_lock); |
| 2372 | |
| 2373 | old_cursegno = curseg->segno; |
Chao Yu | 19f106b | 2015-05-06 13:08:06 +0800 | [diff] [blame] | 2374 | old_blkoff = curseg->next_blkoff; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2375 | |
| 2376 | /* change the current segment */ |
| 2377 | if (segno != curseg->segno) { |
| 2378 | curseg->next_segno = segno; |
| 2379 | change_curseg(sbi, type, true); |
| 2380 | } |
| 2381 | |
Jaegeuk Kim | 491c085 | 2014-02-04 13:01:10 +0900 | [diff] [blame] | 2382 | curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr); |
Haicheng Li | e79efe3 | 2013-06-13 16:59:27 +0800 | [diff] [blame] | 2383 | __add_sum_entry(sbi, type, sum); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2384 | |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 2385 | if (!recover_curseg || recover_newaddr) |
Jaegeuk Kim | 6e2c64a | 2015-10-07 12:28:41 -0700 | [diff] [blame] | 2386 | update_sit_entry(sbi, new_blkaddr, 1); |
| 2387 | if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO) |
| 2388 | update_sit_entry(sbi, old_blkaddr, -1); |
| 2389 | |
| 2390 | locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr)); |
| 2391 | locate_dirty_segment(sbi, GET_SEGNO(sbi, new_blkaddr)); |
| 2392 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2393 | locate_dirty_segment(sbi, old_cursegno); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2394 | |
Chao Yu | 19f106b | 2015-05-06 13:08:06 +0800 | [diff] [blame] | 2395 | if (recover_curseg) { |
| 2396 | if (old_cursegno != curseg->segno) { |
| 2397 | curseg->next_segno = old_cursegno; |
| 2398 | change_curseg(sbi, type, true); |
| 2399 | } |
| 2400 | curseg->next_blkoff = old_blkoff; |
| 2401 | } |
| 2402 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2403 | mutex_unlock(&sit_i->sentry_lock); |
| 2404 | mutex_unlock(&curseg->curseg_mutex); |
| 2405 | } |
| 2406 | |
Chao Yu | 528e345 | 2015-05-28 19:15:35 +0800 | [diff] [blame] | 2407 | void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn, |
| 2408 | block_t old_addr, block_t new_addr, |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 2409 | unsigned char version, bool recover_curseg, |
| 2410 | bool recover_newaddr) |
Chao Yu | 528e345 | 2015-05-28 19:15:35 +0800 | [diff] [blame] | 2411 | { |
| 2412 | struct f2fs_summary sum; |
| 2413 | |
| 2414 | set_summary(&sum, dn->nid, dn->ofs_in_node, version); |
| 2415 | |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 2416 | __f2fs_replace_block(sbi, &sum, old_addr, new_addr, |
| 2417 | recover_curseg, recover_newaddr); |
Chao Yu | 528e345 | 2015-05-28 19:15:35 +0800 | [diff] [blame] | 2418 | |
Chao Yu | f28b343 | 2016-02-24 17:16:47 +0800 | [diff] [blame] | 2419 | f2fs_update_data_blkaddr(dn, new_addr); |
Chao Yu | 528e345 | 2015-05-28 19:15:35 +0800 | [diff] [blame] | 2420 | } |
| 2421 | |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 2422 | void f2fs_wait_on_page_writeback(struct page *page, |
Jaegeuk Kim | fec1d65 | 2016-01-20 23:43:51 +0800 | [diff] [blame] | 2423 | enum page_type type, bool ordered) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 2424 | { |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 2425 | if (PageWriteback(page)) { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 2426 | struct f2fs_sb_info *sbi = F2FS_P_SB(page); |
| 2427 | |
Jaegeuk Kim | b9109b0 | 2017-05-10 11:28:38 -0700 | [diff] [blame] | 2428 | f2fs_submit_merged_write_cond(sbi, page->mapping->host, |
| 2429 | 0, page->index, type); |
Jaegeuk Kim | fec1d65 | 2016-01-20 23:43:51 +0800 | [diff] [blame] | 2430 | if (ordered) |
| 2431 | wait_on_page_writeback(page); |
| 2432 | else |
| 2433 | wait_for_stable_page(page); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 2434 | } |
| 2435 | } |
| 2436 | |
Chao Yu | 08b39fb | 2015-10-08 13:27:34 +0800 | [diff] [blame] | 2437 | void f2fs_wait_on_encrypted_page_writeback(struct f2fs_sb_info *sbi, |
| 2438 | block_t blkaddr) |
| 2439 | { |
| 2440 | struct page *cpage; |
| 2441 | |
Yunlei He | 5d4c0af | 2016-09-18 08:16:56 +0800 | [diff] [blame] | 2442 | if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR) |
Chao Yu | 08b39fb | 2015-10-08 13:27:34 +0800 | [diff] [blame] | 2443 | return; |
| 2444 | |
Chao Yu | 08b39fb | 2015-10-08 13:27:34 +0800 | [diff] [blame] | 2445 | cpage = find_lock_page(META_MAPPING(sbi), blkaddr); |
| 2446 | if (cpage) { |
Jaegeuk Kim | fec1d65 | 2016-01-20 23:43:51 +0800 | [diff] [blame] | 2447 | f2fs_wait_on_page_writeback(cpage, DATA, true); |
Chao Yu | 08b39fb | 2015-10-08 13:27:34 +0800 | [diff] [blame] | 2448 | f2fs_put_page(cpage, 1); |
| 2449 | } |
| 2450 | } |
| 2451 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2452 | static int read_compacted_summaries(struct f2fs_sb_info *sbi) |
| 2453 | { |
| 2454 | struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); |
| 2455 | struct curseg_info *seg_i; |
| 2456 | unsigned char *kaddr; |
| 2457 | struct page *page; |
| 2458 | block_t start; |
| 2459 | int i, j, offset; |
| 2460 | |
| 2461 | start = start_sum_block(sbi); |
| 2462 | |
| 2463 | page = get_meta_page(sbi, start++); |
| 2464 | kaddr = (unsigned char *)page_address(page); |
| 2465 | |
| 2466 | /* Step 1: restore nat cache */ |
| 2467 | seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA); |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 2468 | memcpy(seg_i->journal, kaddr, SUM_JOURNAL_SIZE); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2469 | |
| 2470 | /* Step 2: restore sit cache */ |
| 2471 | seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA); |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 2472 | memcpy(seg_i->journal, kaddr + SUM_JOURNAL_SIZE, SUM_JOURNAL_SIZE); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2473 | offset = 2 * SUM_JOURNAL_SIZE; |
| 2474 | |
| 2475 | /* Step 3: restore summary entries */ |
| 2476 | for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) { |
| 2477 | unsigned short blk_off; |
| 2478 | unsigned int segno; |
| 2479 | |
| 2480 | seg_i = CURSEG_I(sbi, i); |
| 2481 | segno = le32_to_cpu(ckpt->cur_data_segno[i]); |
| 2482 | blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]); |
| 2483 | seg_i->next_segno = segno; |
| 2484 | reset_curseg(sbi, i, 0); |
| 2485 | seg_i->alloc_type = ckpt->alloc_type[i]; |
| 2486 | seg_i->next_blkoff = blk_off; |
| 2487 | |
| 2488 | if (seg_i->alloc_type == SSR) |
| 2489 | blk_off = sbi->blocks_per_seg; |
| 2490 | |
| 2491 | for (j = 0; j < blk_off; j++) { |
| 2492 | struct f2fs_summary *s; |
| 2493 | s = (struct f2fs_summary *)(kaddr + offset); |
| 2494 | seg_i->sum_blk->entries[j] = *s; |
| 2495 | offset += SUMMARY_SIZE; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2496 | if (offset + SUMMARY_SIZE <= PAGE_SIZE - |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2497 | SUM_FOOTER_SIZE) |
| 2498 | continue; |
| 2499 | |
| 2500 | f2fs_put_page(page, 1); |
| 2501 | page = NULL; |
| 2502 | |
| 2503 | page = get_meta_page(sbi, start++); |
| 2504 | kaddr = (unsigned char *)page_address(page); |
| 2505 | offset = 0; |
| 2506 | } |
| 2507 | } |
| 2508 | f2fs_put_page(page, 1); |
| 2509 | return 0; |
| 2510 | } |
| 2511 | |
| 2512 | static int read_normal_summaries(struct f2fs_sb_info *sbi, int type) |
| 2513 | { |
| 2514 | struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); |
| 2515 | struct f2fs_summary_block *sum; |
| 2516 | struct curseg_info *curseg; |
| 2517 | struct page *new; |
| 2518 | unsigned short blk_off; |
| 2519 | unsigned int segno = 0; |
| 2520 | block_t blk_addr = 0; |
| 2521 | |
| 2522 | /* get segment number and block addr */ |
| 2523 | if (IS_DATASEG(type)) { |
| 2524 | segno = le32_to_cpu(ckpt->cur_data_segno[type]); |
| 2525 | blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type - |
| 2526 | CURSEG_HOT_DATA]); |
Jaegeuk Kim | 119ee91 | 2015-01-29 11:45:33 -0800 | [diff] [blame] | 2527 | if (__exist_node_summaries(sbi)) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2528 | blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type); |
| 2529 | else |
| 2530 | blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type); |
| 2531 | } else { |
| 2532 | segno = le32_to_cpu(ckpt->cur_node_segno[type - |
| 2533 | CURSEG_HOT_NODE]); |
| 2534 | blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type - |
| 2535 | CURSEG_HOT_NODE]); |
Jaegeuk Kim | 119ee91 | 2015-01-29 11:45:33 -0800 | [diff] [blame] | 2536 | if (__exist_node_summaries(sbi)) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2537 | blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE, |
| 2538 | type - CURSEG_HOT_NODE); |
| 2539 | else |
| 2540 | blk_addr = GET_SUM_BLOCK(sbi, segno); |
| 2541 | } |
| 2542 | |
| 2543 | new = get_meta_page(sbi, blk_addr); |
| 2544 | sum = (struct f2fs_summary_block *)page_address(new); |
| 2545 | |
| 2546 | if (IS_NODESEG(type)) { |
Jaegeuk Kim | 119ee91 | 2015-01-29 11:45:33 -0800 | [diff] [blame] | 2547 | if (__exist_node_summaries(sbi)) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2548 | struct f2fs_summary *ns = &sum->entries[0]; |
| 2549 | int i; |
| 2550 | for (i = 0; i < sbi->blocks_per_seg; i++, ns++) { |
| 2551 | ns->version = 0; |
| 2552 | ns->ofs_in_node = 0; |
| 2553 | } |
| 2554 | } else { |
Gu Zheng | d653788 | 2014-03-07 18:43:36 +0800 | [diff] [blame] | 2555 | int err; |
| 2556 | |
| 2557 | err = restore_node_summary(sbi, segno, sum); |
| 2558 | if (err) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2559 | f2fs_put_page(new, 1); |
Gu Zheng | d653788 | 2014-03-07 18:43:36 +0800 | [diff] [blame] | 2560 | return err; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2561 | } |
| 2562 | } |
| 2563 | } |
| 2564 | |
| 2565 | /* set uncompleted segment to curseg */ |
| 2566 | curseg = CURSEG_I(sbi, type); |
| 2567 | mutex_lock(&curseg->curseg_mutex); |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 2568 | |
| 2569 | /* update journal info */ |
| 2570 | down_write(&curseg->journal_rwsem); |
| 2571 | memcpy(curseg->journal, &sum->journal, SUM_JOURNAL_SIZE); |
| 2572 | up_write(&curseg->journal_rwsem); |
| 2573 | |
| 2574 | memcpy(curseg->sum_blk->entries, sum->entries, SUM_ENTRY_SIZE); |
| 2575 | memcpy(&curseg->sum_blk->footer, &sum->footer, SUM_FOOTER_SIZE); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2576 | curseg->next_segno = segno; |
| 2577 | reset_curseg(sbi, type, 0); |
| 2578 | curseg->alloc_type = ckpt->alloc_type[type]; |
| 2579 | curseg->next_blkoff = blk_off; |
| 2580 | mutex_unlock(&curseg->curseg_mutex); |
| 2581 | f2fs_put_page(new, 1); |
| 2582 | return 0; |
| 2583 | } |
| 2584 | |
| 2585 | static int restore_curseg_summaries(struct f2fs_sb_info *sbi) |
| 2586 | { |
Jin Qian | 21d3f8e | 2017-06-01 11:18:30 -0700 | [diff] [blame] | 2587 | struct f2fs_journal *sit_j = CURSEG_I(sbi, CURSEG_COLD_DATA)->journal; |
| 2588 | struct f2fs_journal *nat_j = CURSEG_I(sbi, CURSEG_HOT_DATA)->journal; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2589 | int type = CURSEG_HOT_DATA; |
Chao Yu | e4fc5fb | 2014-03-17 16:36:24 +0800 | [diff] [blame] | 2590 | int err; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2591 | |
Chao Yu | aaec2b1 | 2016-09-20 11:04:18 +0800 | [diff] [blame] | 2592 | if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG)) { |
Chao Yu | 3fa06d7 | 2014-12-09 14:21:46 +0800 | [diff] [blame] | 2593 | int npages = npages_for_summary_flush(sbi, true); |
| 2594 | |
| 2595 | if (npages >= 2) |
| 2596 | ra_meta_pages(sbi, start_sum_block(sbi), npages, |
Chao Yu | 26879fb | 2015-10-12 17:05:59 +0800 | [diff] [blame] | 2597 | META_CP, true); |
Chao Yu | 3fa06d7 | 2014-12-09 14:21:46 +0800 | [diff] [blame] | 2598 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2599 | /* restore for compacted data summary */ |
| 2600 | if (read_compacted_summaries(sbi)) |
| 2601 | return -EINVAL; |
| 2602 | type = CURSEG_HOT_NODE; |
| 2603 | } |
| 2604 | |
Jaegeuk Kim | 119ee91 | 2015-01-29 11:45:33 -0800 | [diff] [blame] | 2605 | if (__exist_node_summaries(sbi)) |
Chao Yu | 3fa06d7 | 2014-12-09 14:21:46 +0800 | [diff] [blame] | 2606 | ra_meta_pages(sbi, sum_blk_addr(sbi, NR_CURSEG_TYPE, type), |
Chao Yu | 26879fb | 2015-10-12 17:05:59 +0800 | [diff] [blame] | 2607 | NR_CURSEG_TYPE - type, META_CP, true); |
Chao Yu | 3fa06d7 | 2014-12-09 14:21:46 +0800 | [diff] [blame] | 2608 | |
Chao Yu | e4fc5fb | 2014-03-17 16:36:24 +0800 | [diff] [blame] | 2609 | for (; type <= CURSEG_COLD_NODE; type++) { |
| 2610 | err = read_normal_summaries(sbi, type); |
| 2611 | if (err) |
| 2612 | return err; |
| 2613 | } |
| 2614 | |
Jin Qian | 21d3f8e | 2017-06-01 11:18:30 -0700 | [diff] [blame] | 2615 | /* sanity check for summary blocks */ |
| 2616 | if (nats_in_cursum(nat_j) > NAT_JOURNAL_ENTRIES || |
| 2617 | sits_in_cursum(sit_j) > SIT_JOURNAL_ENTRIES) |
| 2618 | return -EINVAL; |
| 2619 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2620 | return 0; |
| 2621 | } |
| 2622 | |
| 2623 | static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr) |
| 2624 | { |
| 2625 | struct page *page; |
| 2626 | unsigned char *kaddr; |
| 2627 | struct f2fs_summary *summary; |
| 2628 | struct curseg_info *seg_i; |
| 2629 | int written_size = 0; |
| 2630 | int i, j; |
| 2631 | |
| 2632 | page = grab_meta_page(sbi, blkaddr++); |
| 2633 | kaddr = (unsigned char *)page_address(page); |
| 2634 | |
| 2635 | /* Step 1: write nat cache */ |
| 2636 | seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA); |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 2637 | memcpy(kaddr, seg_i->journal, SUM_JOURNAL_SIZE); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2638 | written_size += SUM_JOURNAL_SIZE; |
| 2639 | |
| 2640 | /* Step 2: write sit cache */ |
| 2641 | seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA); |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 2642 | memcpy(kaddr + written_size, seg_i->journal, SUM_JOURNAL_SIZE); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2643 | written_size += SUM_JOURNAL_SIZE; |
| 2644 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2645 | /* Step 3: write summary entries */ |
| 2646 | for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) { |
| 2647 | unsigned short blkoff; |
| 2648 | seg_i = CURSEG_I(sbi, i); |
| 2649 | if (sbi->ckpt->alloc_type[i] == SSR) |
| 2650 | blkoff = sbi->blocks_per_seg; |
| 2651 | else |
| 2652 | blkoff = curseg_blkoff(sbi, i); |
| 2653 | |
| 2654 | for (j = 0; j < blkoff; j++) { |
| 2655 | if (!page) { |
| 2656 | page = grab_meta_page(sbi, blkaddr++); |
| 2657 | kaddr = (unsigned char *)page_address(page); |
| 2658 | written_size = 0; |
| 2659 | } |
| 2660 | summary = (struct f2fs_summary *)(kaddr + written_size); |
| 2661 | *summary = seg_i->sum_blk->entries[j]; |
| 2662 | written_size += SUMMARY_SIZE; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2663 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2664 | if (written_size + SUMMARY_SIZE <= PAGE_SIZE - |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2665 | SUM_FOOTER_SIZE) |
| 2666 | continue; |
| 2667 | |
Chao Yu | e8d61a7 | 2013-10-24 15:08:28 +0800 | [diff] [blame] | 2668 | set_page_dirty(page); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2669 | f2fs_put_page(page, 1); |
| 2670 | page = NULL; |
| 2671 | } |
| 2672 | } |
Chao Yu | e8d61a7 | 2013-10-24 15:08:28 +0800 | [diff] [blame] | 2673 | if (page) { |
| 2674 | set_page_dirty(page); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2675 | f2fs_put_page(page, 1); |
Chao Yu | e8d61a7 | 2013-10-24 15:08:28 +0800 | [diff] [blame] | 2676 | } |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2677 | } |
| 2678 | |
| 2679 | static void write_normal_summaries(struct f2fs_sb_info *sbi, |
| 2680 | block_t blkaddr, int type) |
| 2681 | { |
| 2682 | int i, end; |
| 2683 | if (IS_DATASEG(type)) |
| 2684 | end = type + NR_CURSEG_DATA_TYPE; |
| 2685 | else |
| 2686 | end = type + NR_CURSEG_NODE_TYPE; |
| 2687 | |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 2688 | for (i = type; i < end; i++) |
| 2689 | write_current_sum_page(sbi, i, blkaddr + (i - type)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2690 | } |
| 2691 | |
| 2692 | void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk) |
| 2693 | { |
Chao Yu | aaec2b1 | 2016-09-20 11:04:18 +0800 | [diff] [blame] | 2694 | if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG)) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2695 | write_compacted_summaries(sbi, start_blk); |
| 2696 | else |
| 2697 | write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA); |
| 2698 | } |
| 2699 | |
| 2700 | void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk) |
| 2701 | { |
Jaegeuk Kim | 119ee91 | 2015-01-29 11:45:33 -0800 | [diff] [blame] | 2702 | write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2703 | } |
| 2704 | |
Chao Yu | dfc08a1 | 2016-02-14 18:50:40 +0800 | [diff] [blame] | 2705 | int lookup_journal_in_cursum(struct f2fs_journal *journal, int type, |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2706 | unsigned int val, int alloc) |
| 2707 | { |
| 2708 | int i; |
| 2709 | |
| 2710 | if (type == NAT_JOURNAL) { |
Chao Yu | dfc08a1 | 2016-02-14 18:50:40 +0800 | [diff] [blame] | 2711 | for (i = 0; i < nats_in_cursum(journal); i++) { |
| 2712 | if (le32_to_cpu(nid_in_journal(journal, i)) == val) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2713 | return i; |
| 2714 | } |
Chao Yu | dfc08a1 | 2016-02-14 18:50:40 +0800 | [diff] [blame] | 2715 | if (alloc && __has_cursum_space(journal, 1, NAT_JOURNAL)) |
| 2716 | return update_nats_in_cursum(journal, 1); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2717 | } else if (type == SIT_JOURNAL) { |
Chao Yu | dfc08a1 | 2016-02-14 18:50:40 +0800 | [diff] [blame] | 2718 | for (i = 0; i < sits_in_cursum(journal); i++) |
| 2719 | if (le32_to_cpu(segno_in_journal(journal, i)) == val) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2720 | return i; |
Chao Yu | dfc08a1 | 2016-02-14 18:50:40 +0800 | [diff] [blame] | 2721 | if (alloc && __has_cursum_space(journal, 1, SIT_JOURNAL)) |
| 2722 | return update_sits_in_cursum(journal, 1); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2723 | } |
| 2724 | return -1; |
| 2725 | } |
| 2726 | |
| 2727 | static struct page *get_current_sit_page(struct f2fs_sb_info *sbi, |
| 2728 | unsigned int segno) |
| 2729 | { |
Gu Zheng | 2cc2218 | 2014-10-20 17:45:49 +0800 | [diff] [blame] | 2730 | return get_meta_page(sbi, current_sit_addr(sbi, segno)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2731 | } |
| 2732 | |
| 2733 | static struct page *get_next_sit_page(struct f2fs_sb_info *sbi, |
| 2734 | unsigned int start) |
| 2735 | { |
| 2736 | struct sit_info *sit_i = SIT_I(sbi); |
| 2737 | struct page *src_page, *dst_page; |
| 2738 | pgoff_t src_off, dst_off; |
| 2739 | void *src_addr, *dst_addr; |
| 2740 | |
| 2741 | src_off = current_sit_addr(sbi, start); |
| 2742 | dst_off = next_sit_addr(sbi, src_off); |
| 2743 | |
| 2744 | /* get current sit block page without lock */ |
| 2745 | src_page = get_meta_page(sbi, src_off); |
| 2746 | dst_page = grab_meta_page(sbi, dst_off); |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 2747 | f2fs_bug_on(sbi, PageDirty(src_page)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2748 | |
| 2749 | src_addr = page_address(src_page); |
| 2750 | dst_addr = page_address(dst_page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2751 | memcpy(dst_addr, src_addr, PAGE_SIZE); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2752 | |
| 2753 | set_page_dirty(dst_page); |
| 2754 | f2fs_put_page(src_page, 1); |
| 2755 | |
| 2756 | set_to_next_sit(sit_i, start); |
| 2757 | |
| 2758 | return dst_page; |
| 2759 | } |
| 2760 | |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 2761 | static struct sit_entry_set *grab_sit_entry_set(void) |
| 2762 | { |
| 2763 | struct sit_entry_set *ses = |
Jaegeuk Kim | 80c5450 | 2015-08-20 08:51:56 -0700 | [diff] [blame] | 2764 | f2fs_kmem_cache_alloc(sit_entry_set_slab, GFP_NOFS); |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 2765 | |
| 2766 | ses->entry_cnt = 0; |
| 2767 | INIT_LIST_HEAD(&ses->set_list); |
| 2768 | return ses; |
| 2769 | } |
| 2770 | |
| 2771 | static void release_sit_entry_set(struct sit_entry_set *ses) |
| 2772 | { |
| 2773 | list_del(&ses->set_list); |
| 2774 | kmem_cache_free(sit_entry_set_slab, ses); |
| 2775 | } |
| 2776 | |
| 2777 | static void adjust_sit_entry_set(struct sit_entry_set *ses, |
| 2778 | struct list_head *head) |
| 2779 | { |
| 2780 | struct sit_entry_set *next = ses; |
| 2781 | |
| 2782 | if (list_is_last(&ses->set_list, head)) |
| 2783 | return; |
| 2784 | |
| 2785 | list_for_each_entry_continue(next, head, set_list) |
| 2786 | if (ses->entry_cnt <= next->entry_cnt) |
| 2787 | break; |
| 2788 | |
| 2789 | list_move_tail(&ses->set_list, &next->set_list); |
| 2790 | } |
| 2791 | |
| 2792 | static void add_sit_entry(unsigned int segno, struct list_head *head) |
| 2793 | { |
| 2794 | struct sit_entry_set *ses; |
| 2795 | unsigned int start_segno = START_SEGNO(segno); |
| 2796 | |
| 2797 | list_for_each_entry(ses, head, set_list) { |
| 2798 | if (ses->start_segno == start_segno) { |
| 2799 | ses->entry_cnt++; |
| 2800 | adjust_sit_entry_set(ses, head); |
| 2801 | return; |
| 2802 | } |
| 2803 | } |
| 2804 | |
| 2805 | ses = grab_sit_entry_set(); |
| 2806 | |
| 2807 | ses->start_segno = start_segno; |
| 2808 | ses->entry_cnt++; |
| 2809 | list_add(&ses->set_list, head); |
| 2810 | } |
| 2811 | |
| 2812 | static void add_sits_in_set(struct f2fs_sb_info *sbi) |
| 2813 | { |
| 2814 | struct f2fs_sm_info *sm_info = SM_I(sbi); |
| 2815 | struct list_head *set_list = &sm_info->sit_entry_set; |
| 2816 | unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap; |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 2817 | unsigned int segno; |
| 2818 | |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 2819 | for_each_set_bit(segno, bitmap, MAIN_SEGS(sbi)) |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 2820 | add_sit_entry(segno, set_list); |
| 2821 | } |
| 2822 | |
| 2823 | static void remove_sits_in_journal(struct f2fs_sb_info *sbi) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2824 | { |
| 2825 | struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA); |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 2826 | struct f2fs_journal *journal = curseg->journal; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2827 | int i; |
| 2828 | |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 2829 | down_write(&curseg->journal_rwsem); |
Chao Yu | dfc08a1 | 2016-02-14 18:50:40 +0800 | [diff] [blame] | 2830 | for (i = 0; i < sits_in_cursum(journal); i++) { |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 2831 | unsigned int segno; |
| 2832 | bool dirtied; |
| 2833 | |
Chao Yu | dfc08a1 | 2016-02-14 18:50:40 +0800 | [diff] [blame] | 2834 | segno = le32_to_cpu(segno_in_journal(journal, i)); |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 2835 | dirtied = __mark_sit_entry_dirty(sbi, segno); |
| 2836 | |
| 2837 | if (!dirtied) |
| 2838 | add_sit_entry(segno, &SM_I(sbi)->sit_entry_set); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2839 | } |
Chao Yu | dfc08a1 | 2016-02-14 18:50:40 +0800 | [diff] [blame] | 2840 | update_sits_in_cursum(journal, -i); |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 2841 | up_write(&curseg->journal_rwsem); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2842 | } |
| 2843 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 2844 | /* |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2845 | * CP calls this function, which flushes SIT entries including sit_journal, |
| 2846 | * and moves prefree segs to free segs. |
| 2847 | */ |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 2848 | void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2849 | { |
| 2850 | struct sit_info *sit_i = SIT_I(sbi); |
| 2851 | unsigned long *bitmap = sit_i->dirty_sentries_bitmap; |
| 2852 | struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA); |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 2853 | struct f2fs_journal *journal = curseg->journal; |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 2854 | struct sit_entry_set *ses, *tmp; |
| 2855 | struct list_head *head = &SM_I(sbi)->sit_entry_set; |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 2856 | bool to_journal = true; |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 2857 | struct seg_entry *se; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2858 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2859 | mutex_lock(&sit_i->sentry_lock); |
| 2860 | |
Wanpeng Li | 2b11a74 | 2015-02-27 16:52:50 +0800 | [diff] [blame] | 2861 | if (!sit_i->dirty_sentries) |
| 2862 | goto out; |
| 2863 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2864 | /* |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 2865 | * add and account sit entries of dirty bitmap in sit entry |
| 2866 | * set temporarily |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2867 | */ |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 2868 | add_sits_in_set(sbi); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2869 | |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 2870 | /* |
| 2871 | * if there are no enough space in journal to store dirty sit |
| 2872 | * entries, remove all entries from journal and add and account |
| 2873 | * them in sit entry set. |
| 2874 | */ |
Chao Yu | dfc08a1 | 2016-02-14 18:50:40 +0800 | [diff] [blame] | 2875 | if (!__has_cursum_space(journal, sit_i->dirty_sentries, SIT_JOURNAL)) |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 2876 | remove_sits_in_journal(sbi); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2877 | |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 2878 | /* |
| 2879 | * there are two steps to flush sit entries: |
| 2880 | * #1, flush sit entries to journal in current cold data summary block. |
| 2881 | * #2, flush sit entries to sit page. |
| 2882 | */ |
| 2883 | list_for_each_entry_safe(ses, tmp, head, set_list) { |
Jaegeuk Kim | 4a257ed | 2014-10-16 11:43:30 -0700 | [diff] [blame] | 2884 | struct page *page = NULL; |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 2885 | struct f2fs_sit_block *raw_sit = NULL; |
| 2886 | unsigned int start_segno = ses->start_segno; |
| 2887 | unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK, |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 2888 | (unsigned long)MAIN_SEGS(sbi)); |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 2889 | unsigned int segno = start_segno; |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 2890 | |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 2891 | if (to_journal && |
Chao Yu | dfc08a1 | 2016-02-14 18:50:40 +0800 | [diff] [blame] | 2892 | !__has_cursum_space(journal, ses->entry_cnt, SIT_JOURNAL)) |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 2893 | to_journal = false; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2894 | |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 2895 | if (to_journal) { |
| 2896 | down_write(&curseg->journal_rwsem); |
| 2897 | } else { |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 2898 | page = get_next_sit_page(sbi, start_segno); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2899 | raw_sit = page_address(page); |
| 2900 | } |
| 2901 | |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 2902 | /* flush dirty sit entries in region of current sit set */ |
| 2903 | for_each_set_bit_from(segno, bitmap, end) { |
| 2904 | int offset, sit_offset; |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 2905 | |
| 2906 | se = get_seg_entry(sbi, segno); |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 2907 | |
| 2908 | /* add discard candidates */ |
Chao Yu | c473f1a | 2017-04-27 20:40:39 +0800 | [diff] [blame] | 2909 | if (!(cpc->reason & CP_DISCARD)) { |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 2910 | cpc->trim_start = segno; |
Jaegeuk Kim | 25290fa | 2016-12-29 22:06:15 -0800 | [diff] [blame] | 2911 | add_discard_addrs(sbi, cpc, false); |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 2912 | } |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 2913 | |
| 2914 | if (to_journal) { |
Chao Yu | dfc08a1 | 2016-02-14 18:50:40 +0800 | [diff] [blame] | 2915 | offset = lookup_journal_in_cursum(journal, |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 2916 | SIT_JOURNAL, segno, 1); |
| 2917 | f2fs_bug_on(sbi, offset < 0); |
Chao Yu | dfc08a1 | 2016-02-14 18:50:40 +0800 | [diff] [blame] | 2918 | segno_in_journal(journal, offset) = |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 2919 | cpu_to_le32(segno); |
| 2920 | seg_info_to_raw_sit(se, |
Chao Yu | dfc08a1 | 2016-02-14 18:50:40 +0800 | [diff] [blame] | 2921 | &sit_in_journal(journal, offset)); |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 2922 | } else { |
| 2923 | sit_offset = SIT_ENTRY_OFFSET(sit_i, segno); |
| 2924 | seg_info_to_raw_sit(se, |
| 2925 | &raw_sit->entries[sit_offset]); |
| 2926 | } |
| 2927 | |
| 2928 | __clear_bit(segno, bitmap); |
| 2929 | sit_i->dirty_sentries--; |
| 2930 | ses->entry_cnt--; |
| 2931 | } |
| 2932 | |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 2933 | if (to_journal) |
| 2934 | up_write(&curseg->journal_rwsem); |
| 2935 | else |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 2936 | f2fs_put_page(page, 1); |
| 2937 | |
| 2938 | f2fs_bug_on(sbi, ses->entry_cnt); |
| 2939 | release_sit_entry_set(ses); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2940 | } |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 2941 | |
| 2942 | f2fs_bug_on(sbi, !list_empty(head)); |
| 2943 | f2fs_bug_on(sbi, sit_i->dirty_sentries); |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 2944 | out: |
Chao Yu | c473f1a | 2017-04-27 20:40:39 +0800 | [diff] [blame] | 2945 | if (cpc->reason & CP_DISCARD) { |
Yunlei He | 650d3c4 | 2016-12-22 11:46:24 +0800 | [diff] [blame] | 2946 | __u64 trim_start = cpc->trim_start; |
| 2947 | |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 2948 | for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++) |
Jaegeuk Kim | 25290fa | 2016-12-29 22:06:15 -0800 | [diff] [blame] | 2949 | add_discard_addrs(sbi, cpc, false); |
Yunlei He | 650d3c4 | 2016-12-22 11:46:24 +0800 | [diff] [blame] | 2950 | |
| 2951 | cpc->trim_start = trim_start; |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 2952 | } |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2953 | mutex_unlock(&sit_i->sentry_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2954 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2955 | set_prefree_as_free_segments(sbi); |
| 2956 | } |
| 2957 | |
| 2958 | static int build_sit_info(struct f2fs_sb_info *sbi) |
| 2959 | { |
| 2960 | struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2961 | struct sit_info *sit_i; |
| 2962 | unsigned int sit_segs, start; |
Chao Yu | ae27d62 | 2017-01-07 18:52:34 +0800 | [diff] [blame] | 2963 | char *src_bitmap; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2964 | unsigned int bitmap_size; |
| 2965 | |
| 2966 | /* allocate memory for SIT information */ |
| 2967 | sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL); |
| 2968 | if (!sit_i) |
| 2969 | return -ENOMEM; |
| 2970 | |
| 2971 | SM_I(sbi)->sit_info = sit_i; |
| 2972 | |
Michal Hocko | a7c3e90 | 2017-05-08 15:57:09 -0700 | [diff] [blame] | 2973 | sit_i->sentries = kvzalloc(MAIN_SEGS(sbi) * |
Jaegeuk Kim | 39307a8 | 2015-09-22 13:50:47 -0700 | [diff] [blame] | 2974 | sizeof(struct seg_entry), GFP_KERNEL); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2975 | if (!sit_i->sentries) |
| 2976 | return -ENOMEM; |
| 2977 | |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 2978 | bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi)); |
Michal Hocko | a7c3e90 | 2017-05-08 15:57:09 -0700 | [diff] [blame] | 2979 | sit_i->dirty_sentries_bitmap = kvzalloc(bitmap_size, GFP_KERNEL); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2980 | if (!sit_i->dirty_sentries_bitmap) |
| 2981 | return -ENOMEM; |
| 2982 | |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 2983 | for (start = 0; start < MAIN_SEGS(sbi); start++) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2984 | sit_i->sentries[start].cur_valid_map |
| 2985 | = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL); |
| 2986 | sit_i->sentries[start].ckpt_valid_map |
| 2987 | = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL); |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 2988 | if (!sit_i->sentries[start].cur_valid_map || |
Jaegeuk Kim | 3e02574 | 2016-08-02 10:56:40 -0700 | [diff] [blame] | 2989 | !sit_i->sentries[start].ckpt_valid_map) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2990 | return -ENOMEM; |
Jaegeuk Kim | 3e02574 | 2016-08-02 10:56:40 -0700 | [diff] [blame] | 2991 | |
Chao Yu | 355e789 | 2017-01-07 18:51:01 +0800 | [diff] [blame] | 2992 | #ifdef CONFIG_F2FS_CHECK_FS |
| 2993 | sit_i->sentries[start].cur_valid_map_mir |
| 2994 | = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL); |
| 2995 | if (!sit_i->sentries[start].cur_valid_map_mir) |
| 2996 | return -ENOMEM; |
| 2997 | #endif |
| 2998 | |
Jaegeuk Kim | 3e02574 | 2016-08-02 10:56:40 -0700 | [diff] [blame] | 2999 | if (f2fs_discard_en(sbi)) { |
| 3000 | sit_i->sentries[start].discard_map |
| 3001 | = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL); |
| 3002 | if (!sit_i->sentries[start].discard_map) |
| 3003 | return -ENOMEM; |
| 3004 | } |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3005 | } |
| 3006 | |
Jaegeuk Kim | 60a3b78 | 2015-02-10 16:44:29 -0800 | [diff] [blame] | 3007 | sit_i->tmp_map = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL); |
| 3008 | if (!sit_i->tmp_map) |
| 3009 | return -ENOMEM; |
| 3010 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3011 | if (sbi->segs_per_sec > 1) { |
Michal Hocko | a7c3e90 | 2017-05-08 15:57:09 -0700 | [diff] [blame] | 3012 | sit_i->sec_entries = kvzalloc(MAIN_SECS(sbi) * |
Jaegeuk Kim | 39307a8 | 2015-09-22 13:50:47 -0700 | [diff] [blame] | 3013 | sizeof(struct sec_entry), GFP_KERNEL); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3014 | if (!sit_i->sec_entries) |
| 3015 | return -ENOMEM; |
| 3016 | } |
| 3017 | |
| 3018 | /* get information related with SIT */ |
| 3019 | sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1; |
| 3020 | |
| 3021 | /* setup SIT bitmap from ckeckpoint pack */ |
| 3022 | bitmap_size = __bitmap_size(sbi, SIT_BITMAP); |
| 3023 | src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP); |
| 3024 | |
Chao Yu | ae27d62 | 2017-01-07 18:52:34 +0800 | [diff] [blame] | 3025 | sit_i->sit_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL); |
| 3026 | if (!sit_i->sit_bitmap) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3027 | return -ENOMEM; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3028 | |
Chao Yu | ae27d62 | 2017-01-07 18:52:34 +0800 | [diff] [blame] | 3029 | #ifdef CONFIG_F2FS_CHECK_FS |
| 3030 | sit_i->sit_bitmap_mir = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL); |
| 3031 | if (!sit_i->sit_bitmap_mir) |
| 3032 | return -ENOMEM; |
| 3033 | #endif |
| 3034 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3035 | /* init SIT information */ |
| 3036 | sit_i->s_ops = &default_salloc_ops; |
| 3037 | |
| 3038 | sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr); |
| 3039 | sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg; |
Jaegeuk Kim | c79b7ff | 2016-11-14 18:20:10 -0800 | [diff] [blame] | 3040 | sit_i->written_valid_blocks = 0; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3041 | sit_i->bitmap_size = bitmap_size; |
| 3042 | sit_i->dirty_sentries = 0; |
| 3043 | sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK; |
| 3044 | sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time); |
Deepa Dinamani | 48fbfe5 | 2017-05-08 15:59:10 -0700 | [diff] [blame] | 3045 | sit_i->mounted_time = ktime_get_real_seconds(); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3046 | mutex_init(&sit_i->sentry_lock); |
| 3047 | return 0; |
| 3048 | } |
| 3049 | |
| 3050 | static int build_free_segmap(struct f2fs_sb_info *sbi) |
| 3051 | { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3052 | struct free_segmap_info *free_i; |
| 3053 | unsigned int bitmap_size, sec_bitmap_size; |
| 3054 | |
| 3055 | /* allocate memory for free segmap information */ |
| 3056 | free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL); |
| 3057 | if (!free_i) |
| 3058 | return -ENOMEM; |
| 3059 | |
| 3060 | SM_I(sbi)->free_info = free_i; |
| 3061 | |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 3062 | bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi)); |
Michal Hocko | a7c3e90 | 2017-05-08 15:57:09 -0700 | [diff] [blame] | 3063 | free_i->free_segmap = kvmalloc(bitmap_size, GFP_KERNEL); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3064 | if (!free_i->free_segmap) |
| 3065 | return -ENOMEM; |
| 3066 | |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 3067 | sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi)); |
Michal Hocko | a7c3e90 | 2017-05-08 15:57:09 -0700 | [diff] [blame] | 3068 | free_i->free_secmap = kvmalloc(sec_bitmap_size, GFP_KERNEL); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3069 | if (!free_i->free_secmap) |
| 3070 | return -ENOMEM; |
| 3071 | |
| 3072 | /* set all segments as dirty temporarily */ |
| 3073 | memset(free_i->free_segmap, 0xff, bitmap_size); |
| 3074 | memset(free_i->free_secmap, 0xff, sec_bitmap_size); |
| 3075 | |
| 3076 | /* init free segmap information */ |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 3077 | free_i->start_segno = GET_SEGNO_FROM_SEG0(sbi, MAIN_BLKADDR(sbi)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3078 | free_i->free_segments = 0; |
| 3079 | free_i->free_sections = 0; |
Chao Yu | 1a118cc | 2015-02-11 18:20:38 +0800 | [diff] [blame] | 3080 | spin_lock_init(&free_i->segmap_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3081 | return 0; |
| 3082 | } |
| 3083 | |
| 3084 | static int build_curseg(struct f2fs_sb_info *sbi) |
| 3085 | { |
Namjae Jeon | 1042d60 | 2012-12-01 10:56:13 +0900 | [diff] [blame] | 3086 | struct curseg_info *array; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3087 | int i; |
| 3088 | |
Fabian Frederick | b434bab | 2014-06-23 18:39:15 +0200 | [diff] [blame] | 3089 | array = kcalloc(NR_CURSEG_TYPE, sizeof(*array), GFP_KERNEL); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3090 | if (!array) |
| 3091 | return -ENOMEM; |
| 3092 | |
| 3093 | SM_I(sbi)->curseg_array = array; |
| 3094 | |
| 3095 | for (i = 0; i < NR_CURSEG_TYPE; i++) { |
| 3096 | mutex_init(&array[i].curseg_mutex); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3097 | array[i].sum_blk = kzalloc(PAGE_SIZE, GFP_KERNEL); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3098 | if (!array[i].sum_blk) |
| 3099 | return -ENOMEM; |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 3100 | init_rwsem(&array[i].journal_rwsem); |
| 3101 | array[i].journal = kzalloc(sizeof(struct f2fs_journal), |
| 3102 | GFP_KERNEL); |
| 3103 | if (!array[i].journal) |
| 3104 | return -ENOMEM; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3105 | array[i].segno = NULL_SEGNO; |
| 3106 | array[i].next_blkoff = 0; |
| 3107 | } |
| 3108 | return restore_curseg_summaries(sbi); |
| 3109 | } |
| 3110 | |
| 3111 | static void build_sit_entries(struct f2fs_sb_info *sbi) |
| 3112 | { |
| 3113 | struct sit_info *sit_i = SIT_I(sbi); |
| 3114 | struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA); |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 3115 | struct f2fs_journal *journal = curseg->journal; |
Yunlei He | 9c09404 | 2016-09-24 12:29:18 +0800 | [diff] [blame] | 3116 | struct seg_entry *se; |
| 3117 | struct f2fs_sit_entry sit; |
Chao Yu | 74de593 | 2013-11-22 09:09:59 +0800 | [diff] [blame] | 3118 | int sit_blk_cnt = SIT_BLK_CNT(sbi); |
| 3119 | unsigned int i, start, end; |
| 3120 | unsigned int readed, start_blk = 0; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3121 | |
Chao Yu | 74de593 | 2013-11-22 09:09:59 +0800 | [diff] [blame] | 3122 | do { |
Jaegeuk Kim | 664ba97 | 2016-10-18 11:07:45 -0700 | [diff] [blame] | 3123 | readed = ra_meta_pages(sbi, start_blk, BIO_MAX_PAGES, |
| 3124 | META_SIT, true); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3125 | |
Chao Yu | 74de593 | 2013-11-22 09:09:59 +0800 | [diff] [blame] | 3126 | start = start_blk * sit_i->sents_per_block; |
| 3127 | end = (start_blk + readed) * sit_i->sents_per_block; |
| 3128 | |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 3129 | for (; start < end && start < MAIN_SEGS(sbi); start++) { |
Chao Yu | 74de593 | 2013-11-22 09:09:59 +0800 | [diff] [blame] | 3130 | struct f2fs_sit_block *sit_blk; |
Chao Yu | 74de593 | 2013-11-22 09:09:59 +0800 | [diff] [blame] | 3131 | struct page *page; |
| 3132 | |
Yunlei He | 9c09404 | 2016-09-24 12:29:18 +0800 | [diff] [blame] | 3133 | se = &sit_i->sentries[start]; |
Chao Yu | 74de593 | 2013-11-22 09:09:59 +0800 | [diff] [blame] | 3134 | page = get_current_sit_page(sbi, start); |
| 3135 | sit_blk = (struct f2fs_sit_block *)page_address(page); |
| 3136 | sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)]; |
| 3137 | f2fs_put_page(page, 1); |
Chao Yu | d600af23 | 2016-08-19 23:13:47 +0800 | [diff] [blame] | 3138 | |
Chao Yu | 74de593 | 2013-11-22 09:09:59 +0800 | [diff] [blame] | 3139 | check_block_count(sbi, start, &sit); |
| 3140 | seg_info_from_raw_sit(se, &sit); |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 3141 | |
| 3142 | /* build discard map only one time */ |
Jaegeuk Kim | 3e02574 | 2016-08-02 10:56:40 -0700 | [diff] [blame] | 3143 | if (f2fs_discard_en(sbi)) { |
Chao Yu | 1f43e2a | 2017-04-28 13:56:08 +0800 | [diff] [blame] | 3144 | if (is_set_ckpt_flags(sbi, CP_TRIMMED_FLAG)) { |
| 3145 | memset(se->discard_map, 0xff, |
| 3146 | SIT_VBLOCK_MAP_SIZE); |
| 3147 | } else { |
| 3148 | memcpy(se->discard_map, |
| 3149 | se->cur_valid_map, |
| 3150 | SIT_VBLOCK_MAP_SIZE); |
| 3151 | sbi->discard_blks += |
| 3152 | sbi->blocks_per_seg - |
| 3153 | se->valid_blocks; |
| 3154 | } |
Jaegeuk Kim | 3e02574 | 2016-08-02 10:56:40 -0700 | [diff] [blame] | 3155 | } |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 3156 | |
Chao Yu | d600af23 | 2016-08-19 23:13:47 +0800 | [diff] [blame] | 3157 | if (sbi->segs_per_sec > 1) |
| 3158 | get_sec_entry(sbi, start)->valid_blocks += |
| 3159 | se->valid_blocks; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3160 | } |
Chao Yu | 74de593 | 2013-11-22 09:09:59 +0800 | [diff] [blame] | 3161 | start_blk += readed; |
| 3162 | } while (start_blk < sit_blk_cnt); |
Chao Yu | d600af23 | 2016-08-19 23:13:47 +0800 | [diff] [blame] | 3163 | |
| 3164 | down_read(&curseg->journal_rwsem); |
| 3165 | for (i = 0; i < sits_in_cursum(journal); i++) { |
Chao Yu | d600af23 | 2016-08-19 23:13:47 +0800 | [diff] [blame] | 3166 | unsigned int old_valid_blocks; |
| 3167 | |
| 3168 | start = le32_to_cpu(segno_in_journal(journal, i)); |
| 3169 | se = &sit_i->sentries[start]; |
| 3170 | sit = sit_in_journal(journal, i); |
| 3171 | |
| 3172 | old_valid_blocks = se->valid_blocks; |
| 3173 | |
| 3174 | check_block_count(sbi, start, &sit); |
| 3175 | seg_info_from_raw_sit(se, &sit); |
| 3176 | |
| 3177 | if (f2fs_discard_en(sbi)) { |
Chao Yu | 1f43e2a | 2017-04-28 13:56:08 +0800 | [diff] [blame] | 3178 | if (is_set_ckpt_flags(sbi, CP_TRIMMED_FLAG)) { |
| 3179 | memset(se->discard_map, 0xff, |
| 3180 | SIT_VBLOCK_MAP_SIZE); |
| 3181 | } else { |
| 3182 | memcpy(se->discard_map, se->cur_valid_map, |
| 3183 | SIT_VBLOCK_MAP_SIZE); |
| 3184 | sbi->discard_blks += old_valid_blocks - |
| 3185 | se->valid_blocks; |
| 3186 | } |
Chao Yu | d600af23 | 2016-08-19 23:13:47 +0800 | [diff] [blame] | 3187 | } |
| 3188 | |
| 3189 | if (sbi->segs_per_sec > 1) |
| 3190 | get_sec_entry(sbi, start)->valid_blocks += |
| 3191 | se->valid_blocks - old_valid_blocks; |
| 3192 | } |
| 3193 | up_read(&curseg->journal_rwsem); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3194 | } |
| 3195 | |
| 3196 | static void init_free_segmap(struct f2fs_sb_info *sbi) |
| 3197 | { |
| 3198 | unsigned int start; |
| 3199 | int type; |
| 3200 | |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 3201 | for (start = 0; start < MAIN_SEGS(sbi); start++) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3202 | struct seg_entry *sentry = get_seg_entry(sbi, start); |
| 3203 | if (!sentry->valid_blocks) |
| 3204 | __set_free(sbi, start); |
Jaegeuk Kim | c79b7ff | 2016-11-14 18:20:10 -0800 | [diff] [blame] | 3205 | else |
| 3206 | SIT_I(sbi)->written_valid_blocks += |
| 3207 | sentry->valid_blocks; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3208 | } |
| 3209 | |
| 3210 | /* set use the current segments */ |
| 3211 | for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) { |
| 3212 | struct curseg_info *curseg_t = CURSEG_I(sbi, type); |
| 3213 | __set_test_and_inuse(sbi, curseg_t->segno); |
| 3214 | } |
| 3215 | } |
| 3216 | |
| 3217 | static void init_dirty_segmap(struct f2fs_sb_info *sbi) |
| 3218 | { |
| 3219 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
| 3220 | struct free_segmap_info *free_i = FREE_I(sbi); |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 3221 | unsigned int segno = 0, offset = 0; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3222 | unsigned short valid_blocks; |
| 3223 | |
Namjae Jeon | 8736fbf | 2013-06-16 09:49:11 +0900 | [diff] [blame] | 3224 | while (1) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3225 | /* find dirty segment based on free segmap */ |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 3226 | segno = find_next_inuse(free_i, MAIN_SEGS(sbi), offset); |
| 3227 | if (segno >= MAIN_SEGS(sbi)) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3228 | break; |
| 3229 | offset = segno + 1; |
Jaegeuk Kim | 302bd34 | 2017-04-07 14:33:22 -0700 | [diff] [blame] | 3230 | valid_blocks = get_valid_blocks(sbi, segno, false); |
Jaegeuk Kim | ec325b5 | 2014-09-02 16:24:11 -0700 | [diff] [blame] | 3231 | if (valid_blocks == sbi->blocks_per_seg || !valid_blocks) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3232 | continue; |
Jaegeuk Kim | ec325b5 | 2014-09-02 16:24:11 -0700 | [diff] [blame] | 3233 | if (valid_blocks > sbi->blocks_per_seg) { |
| 3234 | f2fs_bug_on(sbi, 1); |
| 3235 | continue; |
| 3236 | } |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3237 | mutex_lock(&dirty_i->seglist_lock); |
| 3238 | __locate_dirty_segment(sbi, segno, DIRTY); |
| 3239 | mutex_unlock(&dirty_i->seglist_lock); |
| 3240 | } |
| 3241 | } |
| 3242 | |
Jaegeuk Kim | 5ec4e49 | 2013-03-31 13:26:03 +0900 | [diff] [blame] | 3243 | static int init_victim_secmap(struct f2fs_sb_info *sbi) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3244 | { |
| 3245 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 3246 | unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3247 | |
Michal Hocko | a7c3e90 | 2017-05-08 15:57:09 -0700 | [diff] [blame] | 3248 | dirty_i->victim_secmap = kvzalloc(bitmap_size, GFP_KERNEL); |
Jaegeuk Kim | 5ec4e49 | 2013-03-31 13:26:03 +0900 | [diff] [blame] | 3249 | if (!dirty_i->victim_secmap) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3250 | return -ENOMEM; |
| 3251 | return 0; |
| 3252 | } |
| 3253 | |
| 3254 | static int build_dirty_segmap(struct f2fs_sb_info *sbi) |
| 3255 | { |
| 3256 | struct dirty_seglist_info *dirty_i; |
| 3257 | unsigned int bitmap_size, i; |
| 3258 | |
| 3259 | /* allocate memory for dirty segments list information */ |
| 3260 | dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL); |
| 3261 | if (!dirty_i) |
| 3262 | return -ENOMEM; |
| 3263 | |
| 3264 | SM_I(sbi)->dirty_info = dirty_i; |
| 3265 | mutex_init(&dirty_i->seglist_lock); |
| 3266 | |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 3267 | bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3268 | |
| 3269 | for (i = 0; i < NR_DIRTY_TYPE; i++) { |
Michal Hocko | a7c3e90 | 2017-05-08 15:57:09 -0700 | [diff] [blame] | 3270 | dirty_i->dirty_segmap[i] = kvzalloc(bitmap_size, GFP_KERNEL); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3271 | if (!dirty_i->dirty_segmap[i]) |
| 3272 | return -ENOMEM; |
| 3273 | } |
| 3274 | |
| 3275 | init_dirty_segmap(sbi); |
Jaegeuk Kim | 5ec4e49 | 2013-03-31 13:26:03 +0900 | [diff] [blame] | 3276 | return init_victim_secmap(sbi); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3277 | } |
| 3278 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 3279 | /* |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3280 | * Update min, max modified time for cost-benefit GC algorithm |
| 3281 | */ |
| 3282 | static void init_min_max_mtime(struct f2fs_sb_info *sbi) |
| 3283 | { |
| 3284 | struct sit_info *sit_i = SIT_I(sbi); |
| 3285 | unsigned int segno; |
| 3286 | |
| 3287 | mutex_lock(&sit_i->sentry_lock); |
| 3288 | |
| 3289 | sit_i->min_mtime = LLONG_MAX; |
| 3290 | |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 3291 | for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3292 | unsigned int i; |
| 3293 | unsigned long long mtime = 0; |
| 3294 | |
| 3295 | for (i = 0; i < sbi->segs_per_sec; i++) |
| 3296 | mtime += get_seg_entry(sbi, segno + i)->mtime; |
| 3297 | |
| 3298 | mtime = div_u64(mtime, sbi->segs_per_sec); |
| 3299 | |
| 3300 | if (sit_i->min_mtime > mtime) |
| 3301 | sit_i->min_mtime = mtime; |
| 3302 | } |
| 3303 | sit_i->max_mtime = get_mtime(sbi); |
| 3304 | mutex_unlock(&sit_i->sentry_lock); |
| 3305 | } |
| 3306 | |
| 3307 | int build_segment_manager(struct f2fs_sb_info *sbi) |
| 3308 | { |
| 3309 | struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi); |
| 3310 | struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); |
Namjae Jeon | 1042d60 | 2012-12-01 10:56:13 +0900 | [diff] [blame] | 3311 | struct f2fs_sm_info *sm_info; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3312 | int err; |
| 3313 | |
| 3314 | sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL); |
| 3315 | if (!sm_info) |
| 3316 | return -ENOMEM; |
| 3317 | |
| 3318 | /* init sm info */ |
| 3319 | sbi->sm_info = sm_info; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3320 | sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr); |
| 3321 | sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr); |
| 3322 | sm_info->segment_count = le32_to_cpu(raw_super->segment_count); |
| 3323 | sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count); |
| 3324 | sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count); |
| 3325 | sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main); |
| 3326 | sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr); |
Jaegeuk Kim | 58c4103 | 2014-03-19 14:17:21 +0900 | [diff] [blame] | 3327 | sm_info->rec_prefree_segments = sm_info->main_segments * |
| 3328 | DEF_RECLAIM_PREFREE_SEGMENTS / 100; |
Jaegeuk Kim | 44a8349 | 2016-07-13 18:23:35 -0700 | [diff] [blame] | 3329 | if (sm_info->rec_prefree_segments > DEF_MAX_RECLAIM_PREFREE_SEGMENTS) |
| 3330 | sm_info->rec_prefree_segments = DEF_MAX_RECLAIM_PREFREE_SEGMENTS; |
| 3331 | |
Jaegeuk Kim | 52763a4 | 2016-06-13 09:47:48 -0700 | [diff] [blame] | 3332 | if (!test_opt(sbi, LFS)) |
| 3333 | sm_info->ipu_policy = 1 << F2FS_IPU_FSYNC; |
Jaegeuk Kim | 216fbd6 | 2013-11-07 13:13:42 +0900 | [diff] [blame] | 3334 | sm_info->min_ipu_util = DEF_MIN_IPU_UTIL; |
Jaegeuk Kim | c1ce1b0 | 2014-09-10 16:53:02 -0700 | [diff] [blame] | 3335 | sm_info->min_fsync_blocks = DEF_MIN_FSYNC_BLOCKS; |
Jaegeuk Kim | ef095d1 | 2017-03-24 20:05:13 -0400 | [diff] [blame] | 3336 | sm_info->min_hot_blocks = DEF_MIN_HOT_BLOCKS; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3337 | |
Jaegeuk Kim | bba681c | 2015-01-26 17:41:23 -0800 | [diff] [blame] | 3338 | sm_info->trim_sections = DEF_BATCHED_TRIM_SECTIONS; |
| 3339 | |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 3340 | INIT_LIST_HEAD(&sm_info->sit_entry_set); |
| 3341 | |
Yunlei He | d4fdf8b | 2017-06-01 16:43:51 +0800 | [diff] [blame] | 3342 | if (!f2fs_readonly(sbi->sb)) { |
Gu Zheng | 2163d19 | 2014-04-27 14:21:33 +0800 | [diff] [blame] | 3343 | err = create_flush_cmd_control(sbi); |
| 3344 | if (err) |
Gu Zheng | a688b9d9e | 2014-04-27 14:21:21 +0800 | [diff] [blame] | 3345 | return err; |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 3346 | } |
| 3347 | |
Jaegeuk Kim | 0b54fb8 | 2017-01-11 14:40:24 -0800 | [diff] [blame] | 3348 | err = create_discard_cmd_control(sbi); |
| 3349 | if (err) |
| 3350 | return err; |
| 3351 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3352 | err = build_sit_info(sbi); |
| 3353 | if (err) |
| 3354 | return err; |
| 3355 | err = build_free_segmap(sbi); |
| 3356 | if (err) |
| 3357 | return err; |
| 3358 | err = build_curseg(sbi); |
| 3359 | if (err) |
| 3360 | return err; |
| 3361 | |
| 3362 | /* reinit free segmap based on SIT */ |
| 3363 | build_sit_entries(sbi); |
| 3364 | |
| 3365 | init_free_segmap(sbi); |
| 3366 | err = build_dirty_segmap(sbi); |
| 3367 | if (err) |
| 3368 | return err; |
| 3369 | |
| 3370 | init_min_max_mtime(sbi); |
| 3371 | return 0; |
| 3372 | } |
| 3373 | |
| 3374 | static void discard_dirty_segmap(struct f2fs_sb_info *sbi, |
| 3375 | enum dirty_type dirty_type) |
| 3376 | { |
| 3377 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
| 3378 | |
| 3379 | mutex_lock(&dirty_i->seglist_lock); |
Jaegeuk Kim | 39307a8 | 2015-09-22 13:50:47 -0700 | [diff] [blame] | 3380 | kvfree(dirty_i->dirty_segmap[dirty_type]); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3381 | dirty_i->nr_dirty[dirty_type] = 0; |
| 3382 | mutex_unlock(&dirty_i->seglist_lock); |
| 3383 | } |
| 3384 | |
Jaegeuk Kim | 5ec4e49 | 2013-03-31 13:26:03 +0900 | [diff] [blame] | 3385 | static void destroy_victim_secmap(struct f2fs_sb_info *sbi) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3386 | { |
| 3387 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
Jaegeuk Kim | 39307a8 | 2015-09-22 13:50:47 -0700 | [diff] [blame] | 3388 | kvfree(dirty_i->victim_secmap); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3389 | } |
| 3390 | |
| 3391 | static void destroy_dirty_segmap(struct f2fs_sb_info *sbi) |
| 3392 | { |
| 3393 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
| 3394 | int i; |
| 3395 | |
| 3396 | if (!dirty_i) |
| 3397 | return; |
| 3398 | |
| 3399 | /* discard pre-free/dirty segments list */ |
| 3400 | for (i = 0; i < NR_DIRTY_TYPE; i++) |
| 3401 | discard_dirty_segmap(sbi, i); |
| 3402 | |
Jaegeuk Kim | 5ec4e49 | 2013-03-31 13:26:03 +0900 | [diff] [blame] | 3403 | destroy_victim_secmap(sbi); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3404 | SM_I(sbi)->dirty_info = NULL; |
| 3405 | kfree(dirty_i); |
| 3406 | } |
| 3407 | |
| 3408 | static void destroy_curseg(struct f2fs_sb_info *sbi) |
| 3409 | { |
| 3410 | struct curseg_info *array = SM_I(sbi)->curseg_array; |
| 3411 | int i; |
| 3412 | |
| 3413 | if (!array) |
| 3414 | return; |
| 3415 | SM_I(sbi)->curseg_array = NULL; |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 3416 | for (i = 0; i < NR_CURSEG_TYPE; i++) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3417 | kfree(array[i].sum_blk); |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 3418 | kfree(array[i].journal); |
| 3419 | } |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3420 | kfree(array); |
| 3421 | } |
| 3422 | |
| 3423 | static void destroy_free_segmap(struct f2fs_sb_info *sbi) |
| 3424 | { |
| 3425 | struct free_segmap_info *free_i = SM_I(sbi)->free_info; |
| 3426 | if (!free_i) |
| 3427 | return; |
| 3428 | SM_I(sbi)->free_info = NULL; |
Jaegeuk Kim | 39307a8 | 2015-09-22 13:50:47 -0700 | [diff] [blame] | 3429 | kvfree(free_i->free_segmap); |
| 3430 | kvfree(free_i->free_secmap); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3431 | kfree(free_i); |
| 3432 | } |
| 3433 | |
| 3434 | static void destroy_sit_info(struct f2fs_sb_info *sbi) |
| 3435 | { |
| 3436 | struct sit_info *sit_i = SIT_I(sbi); |
| 3437 | unsigned int start; |
| 3438 | |
| 3439 | if (!sit_i) |
| 3440 | return; |
| 3441 | |
| 3442 | if (sit_i->sentries) { |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 3443 | for (start = 0; start < MAIN_SEGS(sbi); start++) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3444 | kfree(sit_i->sentries[start].cur_valid_map); |
Chao Yu | 355e789 | 2017-01-07 18:51:01 +0800 | [diff] [blame] | 3445 | #ifdef CONFIG_F2FS_CHECK_FS |
| 3446 | kfree(sit_i->sentries[start].cur_valid_map_mir); |
| 3447 | #endif |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3448 | kfree(sit_i->sentries[start].ckpt_valid_map); |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 3449 | kfree(sit_i->sentries[start].discard_map); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3450 | } |
| 3451 | } |
Jaegeuk Kim | 60a3b78 | 2015-02-10 16:44:29 -0800 | [diff] [blame] | 3452 | kfree(sit_i->tmp_map); |
| 3453 | |
Jaegeuk Kim | 39307a8 | 2015-09-22 13:50:47 -0700 | [diff] [blame] | 3454 | kvfree(sit_i->sentries); |
| 3455 | kvfree(sit_i->sec_entries); |
| 3456 | kvfree(sit_i->dirty_sentries_bitmap); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3457 | |
| 3458 | SM_I(sbi)->sit_info = NULL; |
| 3459 | kfree(sit_i->sit_bitmap); |
Chao Yu | ae27d62 | 2017-01-07 18:52:34 +0800 | [diff] [blame] | 3460 | #ifdef CONFIG_F2FS_CHECK_FS |
| 3461 | kfree(sit_i->sit_bitmap_mir); |
| 3462 | #endif |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3463 | kfree(sit_i); |
| 3464 | } |
| 3465 | |
| 3466 | void destroy_segment_manager(struct f2fs_sb_info *sbi) |
| 3467 | { |
| 3468 | struct f2fs_sm_info *sm_info = SM_I(sbi); |
Gu Zheng | a688b9d9e | 2014-04-27 14:21:21 +0800 | [diff] [blame] | 3469 | |
Chao Yu | 3b03f72 | 2013-11-06 09:12:04 +0800 | [diff] [blame] | 3470 | if (!sm_info) |
| 3471 | return; |
Jaegeuk Kim | 5eba8c5 | 2016-12-07 16:23:32 -0800 | [diff] [blame] | 3472 | destroy_flush_cmd_control(sbi, true); |
Chao Yu | f099405 | 2017-03-27 18:14:04 +0800 | [diff] [blame] | 3473 | destroy_discard_cmd_control(sbi); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 3474 | destroy_dirty_segmap(sbi); |
| 3475 | destroy_curseg(sbi); |
| 3476 | destroy_free_segmap(sbi); |
| 3477 | destroy_sit_info(sbi); |
| 3478 | sbi->sm_info = NULL; |
| 3479 | kfree(sm_info); |
| 3480 | } |
Jaegeuk Kim | 7fd9e54 | 2013-11-15 13:55:58 +0900 | [diff] [blame] | 3481 | |
| 3482 | int __init create_segment_manager_caches(void) |
| 3483 | { |
| 3484 | discard_entry_slab = f2fs_kmem_cache_create("discard_entry", |
Gu Zheng | e8512d2 | 2014-03-07 18:43:28 +0800 | [diff] [blame] | 3485 | sizeof(struct discard_entry)); |
Jaegeuk Kim | 7fd9e54 | 2013-11-15 13:55:58 +0900 | [diff] [blame] | 3486 | if (!discard_entry_slab) |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 3487 | goto fail; |
| 3488 | |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 3489 | discard_cmd_slab = f2fs_kmem_cache_create("discard_cmd", |
| 3490 | sizeof(struct discard_cmd)); |
| 3491 | if (!discard_cmd_slab) |
Chao Yu | 6ab2a30 | 2016-09-05 12:28:26 +0800 | [diff] [blame] | 3492 | goto destroy_discard_entry; |
Chao Yu | 275b66b | 2016-08-29 23:58:34 +0800 | [diff] [blame] | 3493 | |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 3494 | sit_entry_set_slab = f2fs_kmem_cache_create("sit_entry_set", |
Changman Lee | c9ee008 | 2014-11-21 15:42:07 +0900 | [diff] [blame] | 3495 | sizeof(struct sit_entry_set)); |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 3496 | if (!sit_entry_set_slab) |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 3497 | goto destroy_discard_cmd; |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 3498 | |
| 3499 | inmem_entry_slab = f2fs_kmem_cache_create("inmem_page_entry", |
| 3500 | sizeof(struct inmem_pages)); |
| 3501 | if (!inmem_entry_slab) |
| 3502 | goto destroy_sit_entry_set; |
Jaegeuk Kim | 7fd9e54 | 2013-11-15 13:55:58 +0900 | [diff] [blame] | 3503 | return 0; |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 3504 | |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 3505 | destroy_sit_entry_set: |
| 3506 | kmem_cache_destroy(sit_entry_set_slab); |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 3507 | destroy_discard_cmd: |
| 3508 | kmem_cache_destroy(discard_cmd_slab); |
Chao Yu | 6ab2a30 | 2016-09-05 12:28:26 +0800 | [diff] [blame] | 3509 | destroy_discard_entry: |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 3510 | kmem_cache_destroy(discard_entry_slab); |
| 3511 | fail: |
| 3512 | return -ENOMEM; |
Jaegeuk Kim | 7fd9e54 | 2013-11-15 13:55:58 +0900 | [diff] [blame] | 3513 | } |
| 3514 | |
| 3515 | void destroy_segment_manager_caches(void) |
| 3516 | { |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 3517 | kmem_cache_destroy(sit_entry_set_slab); |
Jaegeuk Kim | b01a920 | 2017-01-09 14:13:03 -0800 | [diff] [blame] | 3518 | kmem_cache_destroy(discard_cmd_slab); |
Jaegeuk Kim | 7fd9e54 | 2013-11-15 13:55:58 +0900 | [diff] [blame] | 3519 | kmem_cache_destroy(discard_entry_slab); |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 3520 | kmem_cache_destroy(inmem_entry_slab); |
Jaegeuk Kim | 7fd9e54 | 2013-11-15 13:55:58 +0900 | [diff] [blame] | 3521 | } |