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