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