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