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