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 | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 19 | |
| 20 | #include "f2fs.h" |
| 21 | #include "segment.h" |
| 22 | #include "node.h" |
Jaegeuk Kim | 9e4ded3 | 2014-12-17 19:58:58 -0800 | [diff] [blame] | 23 | #include "trace.h" |
Namjae Jeon | 6ec178d | 2013-04-23 17:51:43 +0900 | [diff] [blame] | 24 | #include <trace/events/f2fs.h> |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 25 | |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 26 | #define __reverse_ffz(x) __reverse_ffs(~(x)) |
| 27 | |
Jaegeuk Kim | 7fd9e54 | 2013-11-15 13:55:58 +0900 | [diff] [blame] | 28 | static struct kmem_cache *discard_entry_slab; |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 29 | static struct kmem_cache *sit_entry_set_slab; |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 30 | static struct kmem_cache *inmem_entry_slab; |
Jaegeuk Kim | 7fd9e54 | 2013-11-15 13:55:58 +0900 | [diff] [blame] | 31 | |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 32 | /* |
| 33 | * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since |
| 34 | * MSB and LSB are reversed in a byte by f2fs_set_bit. |
| 35 | */ |
| 36 | static inline unsigned long __reverse_ffs(unsigned long word) |
| 37 | { |
| 38 | int num = 0; |
| 39 | |
| 40 | #if BITS_PER_LONG == 64 |
| 41 | if ((word & 0xffffffff) == 0) { |
| 42 | num += 32; |
| 43 | word >>= 32; |
| 44 | } |
| 45 | #endif |
| 46 | if ((word & 0xffff) == 0) { |
| 47 | num += 16; |
| 48 | word >>= 16; |
| 49 | } |
| 50 | if ((word & 0xff) == 0) { |
| 51 | num += 8; |
| 52 | word >>= 8; |
| 53 | } |
| 54 | if ((word & 0xf0) == 0) |
| 55 | num += 4; |
| 56 | else |
| 57 | word >>= 4; |
| 58 | if ((word & 0xc) == 0) |
| 59 | num += 2; |
| 60 | else |
| 61 | word >>= 2; |
| 62 | if ((word & 0x2) == 0) |
| 63 | num += 1; |
| 64 | return num; |
| 65 | } |
| 66 | |
| 67 | /* |
arter97 | e1c4204 | 2014-08-06 23:22:50 +0900 | [diff] [blame] | 68 | * __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] | 69 | * f2fs_set_bit makes MSB and LSB reversed in a byte. |
| 70 | * Example: |
| 71 | * LSB <--> MSB |
| 72 | * f2fs_set_bit(0, bitmap) => 0000 0001 |
| 73 | * f2fs_set_bit(7, bitmap) => 1000 0000 |
| 74 | */ |
| 75 | static unsigned long __find_rev_next_bit(const unsigned long *addr, |
| 76 | unsigned long size, unsigned long offset) |
| 77 | { |
Jaegeuk Kim | e19ef527 | 2015-05-18 11:45:15 -0700 | [diff] [blame] | 78 | while (!f2fs_test_bit(offset, (unsigned char *)addr)) |
| 79 | offset++; |
| 80 | |
| 81 | if (offset > size) |
| 82 | offset = size; |
| 83 | |
| 84 | return offset; |
| 85 | #if 0 |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 86 | const unsigned long *p = addr + BIT_WORD(offset); |
| 87 | unsigned long result = offset & ~(BITS_PER_LONG - 1); |
| 88 | unsigned long tmp; |
| 89 | unsigned long mask, submask; |
| 90 | unsigned long quot, rest; |
| 91 | |
| 92 | if (offset >= size) |
| 93 | return size; |
| 94 | |
| 95 | size -= result; |
| 96 | offset %= BITS_PER_LONG; |
| 97 | if (!offset) |
| 98 | goto aligned; |
| 99 | |
| 100 | tmp = *(p++); |
| 101 | quot = (offset >> 3) << 3; |
| 102 | rest = offset & 0x7; |
| 103 | mask = ~0UL << quot; |
| 104 | submask = (unsigned char)(0xff << rest) >> rest; |
| 105 | submask <<= quot; |
| 106 | mask &= submask; |
| 107 | tmp &= mask; |
| 108 | if (size < BITS_PER_LONG) |
| 109 | goto found_first; |
| 110 | if (tmp) |
| 111 | goto found_middle; |
| 112 | |
| 113 | size -= BITS_PER_LONG; |
| 114 | result += BITS_PER_LONG; |
| 115 | aligned: |
| 116 | while (size & ~(BITS_PER_LONG-1)) { |
| 117 | tmp = *(p++); |
| 118 | if (tmp) |
| 119 | goto found_middle; |
| 120 | result += BITS_PER_LONG; |
| 121 | size -= BITS_PER_LONG; |
| 122 | } |
| 123 | if (!size) |
| 124 | return result; |
| 125 | tmp = *p; |
| 126 | found_first: |
| 127 | tmp &= (~0UL >> (BITS_PER_LONG - size)); |
| 128 | if (tmp == 0UL) /* Are any bits set? */ |
| 129 | return result + size; /* Nope. */ |
| 130 | found_middle: |
| 131 | return result + __reverse_ffs(tmp); |
Jaegeuk Kim | e19ef527 | 2015-05-18 11:45:15 -0700 | [diff] [blame] | 132 | #endif |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | static unsigned long __find_rev_next_zero_bit(const unsigned long *addr, |
| 136 | unsigned long size, unsigned long offset) |
| 137 | { |
Jaegeuk Kim | e19ef527 | 2015-05-18 11:45:15 -0700 | [diff] [blame] | 138 | while (f2fs_test_bit(offset, (unsigned char *)addr)) |
| 139 | offset++; |
| 140 | |
| 141 | if (offset > size) |
| 142 | offset = size; |
| 143 | |
| 144 | return offset; |
| 145 | #if 0 |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 146 | const unsigned long *p = addr + BIT_WORD(offset); |
| 147 | unsigned long result = offset & ~(BITS_PER_LONG - 1); |
| 148 | unsigned long tmp; |
| 149 | unsigned long mask, submask; |
| 150 | unsigned long quot, rest; |
| 151 | |
| 152 | if (offset >= size) |
| 153 | return size; |
| 154 | |
| 155 | size -= result; |
| 156 | offset %= BITS_PER_LONG; |
| 157 | if (!offset) |
| 158 | goto aligned; |
| 159 | |
| 160 | tmp = *(p++); |
| 161 | quot = (offset >> 3) << 3; |
| 162 | rest = offset & 0x7; |
| 163 | mask = ~(~0UL << quot); |
| 164 | submask = (unsigned char)~((unsigned char)(0xff << rest) >> rest); |
| 165 | submask <<= quot; |
| 166 | mask += submask; |
| 167 | tmp |= mask; |
| 168 | if (size < BITS_PER_LONG) |
| 169 | goto found_first; |
| 170 | if (~tmp) |
| 171 | goto found_middle; |
| 172 | |
| 173 | size -= BITS_PER_LONG; |
| 174 | result += BITS_PER_LONG; |
| 175 | aligned: |
| 176 | while (size & ~(BITS_PER_LONG - 1)) { |
| 177 | tmp = *(p++); |
| 178 | if (~tmp) |
| 179 | goto found_middle; |
| 180 | result += BITS_PER_LONG; |
| 181 | size -= BITS_PER_LONG; |
| 182 | } |
| 183 | if (!size) |
| 184 | return result; |
| 185 | tmp = *p; |
| 186 | |
| 187 | found_first: |
| 188 | tmp |= ~0UL << size; |
| 189 | if (tmp == ~0UL) /* Are any bits zero? */ |
| 190 | return result + size; /* Nope. */ |
| 191 | found_middle: |
| 192 | return result + __reverse_ffz(tmp); |
Jaegeuk Kim | e19ef527 | 2015-05-18 11:45:15 -0700 | [diff] [blame] | 193 | #endif |
Changman Lee | 9a7f143 | 2013-11-15 10:42:51 +0900 | [diff] [blame] | 194 | } |
| 195 | |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 196 | void register_inmem_page(struct inode *inode, struct page *page) |
| 197 | { |
| 198 | struct f2fs_inode_info *fi = F2FS_I(inode); |
| 199 | struct inmem_pages *new; |
Jaegeuk Kim | 9be32d7 | 2014-12-05 10:39:49 -0800 | [diff] [blame] | 200 | |
Jaegeuk Kim | 9e4ded3 | 2014-12-17 19:58:58 -0800 | [diff] [blame] | 201 | f2fs_trace_pid(page); |
Jaegeuk Kim | 0722b10 | 2014-12-05 11:58:02 -0800 | [diff] [blame] | 202 | |
Chao Yu | decd36b | 2015-08-07 18:42:09 +0800 | [diff] [blame] | 203 | set_page_private(page, (unsigned long)ATOMIC_WRITTEN_PAGE); |
| 204 | SetPagePrivate(page); |
| 205 | |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 206 | new = f2fs_kmem_cache_alloc(inmem_entry_slab, GFP_NOFS); |
| 207 | |
| 208 | /* add atomic page indices to the list */ |
| 209 | new->page = page; |
| 210 | INIT_LIST_HEAD(&new->list); |
Chao Yu | decd36b | 2015-08-07 18:42:09 +0800 | [diff] [blame] | 211 | |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 212 | /* increase reference count with clean state */ |
| 213 | mutex_lock(&fi->inmem_lock); |
| 214 | get_page(page); |
| 215 | list_add_tail(&new->list, &fi->inmem_pages); |
Jaegeuk Kim | 8dcf2ff7 | 2014-12-05 17:18:15 -0800 | [diff] [blame] | 216 | inc_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES); |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 217 | mutex_unlock(&fi->inmem_lock); |
Jaegeuk Kim | 8ce67cb | 2015-03-17 17:58:08 -0700 | [diff] [blame] | 218 | |
| 219 | trace_f2fs_register_inmem_page(page, INMEM); |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Jaegeuk Kim | edb27de | 2015-07-25 00:52:52 -0700 | [diff] [blame] | 222 | int commit_inmem_pages(struct inode *inode, bool abort) |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 223 | { |
| 224 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 225 | struct f2fs_inode_info *fi = F2FS_I(inode); |
| 226 | struct inmem_pages *cur, *tmp; |
| 227 | bool submit_bio = false; |
| 228 | struct f2fs_io_info fio = { |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 229 | .sbi = sbi, |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 230 | .type = DATA, |
Jaegeuk Kim | 1e84371 | 2014-12-09 06:08:59 -0800 | [diff] [blame] | 231 | .rw = WRITE_SYNC | REQ_PRIO, |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 232 | .encrypted_page = NULL, |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 233 | }; |
Jaegeuk Kim | edb27de | 2015-07-25 00:52:52 -0700 | [diff] [blame] | 234 | int err = 0; |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 235 | |
Jaegeuk Kim | 0341845 | 2014-11-21 16:37:40 -0800 | [diff] [blame] | 236 | /* |
| 237 | * The abort is true only when f2fs_evict_inode is called. |
| 238 | * Basically, the f2fs_evict_inode doesn't produce any data writes, so |
| 239 | * that we don't need to call f2fs_balance_fs. |
| 240 | * Otherwise, f2fs_gc in f2fs_balance_fs can wait forever until this |
| 241 | * inode becomes free by iget_locked in f2fs_iget. |
| 242 | */ |
Jaegeuk Kim | 70c640b | 2014-12-10 13:59:33 -0800 | [diff] [blame] | 243 | if (!abort) { |
Jaegeuk Kim | 0341845 | 2014-11-21 16:37:40 -0800 | [diff] [blame] | 244 | f2fs_balance_fs(sbi); |
Jaegeuk Kim | 70c640b | 2014-12-10 13:59:33 -0800 | [diff] [blame] | 245 | f2fs_lock_op(sbi); |
| 246 | } |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 247 | |
| 248 | mutex_lock(&fi->inmem_lock); |
| 249 | list_for_each_entry_safe(cur, tmp, &fi->inmem_pages, list) { |
Chao Yu | decd36b | 2015-08-07 18:42:09 +0800 | [diff] [blame] | 250 | lock_page(cur->page); |
Jaegeuk Kim | 70c640b | 2014-12-10 13:59:33 -0800 | [diff] [blame] | 251 | if (!abort) { |
Jaegeuk Kim | 70c640b | 2014-12-10 13:59:33 -0800 | [diff] [blame] | 252 | if (cur->page->mapping == inode->i_mapping) { |
Jaegeuk Kim | 6282adb | 2015-07-25 00:29:17 -0700 | [diff] [blame] | 253 | set_page_dirty(cur->page); |
Jaegeuk Kim | 70c640b | 2014-12-10 13:59:33 -0800 | [diff] [blame] | 254 | f2fs_wait_on_page_writeback(cur->page, DATA); |
| 255 | if (clear_page_dirty_for_io(cur->page)) |
| 256 | inode_dec_dirty_pages(inode); |
Jaegeuk Kim | 8ce67cb | 2015-03-17 17:58:08 -0700 | [diff] [blame] | 257 | trace_f2fs_commit_inmem_page(cur->page, INMEM); |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 258 | fio.page = cur->page; |
Jaegeuk Kim | edb27de | 2015-07-25 00:52:52 -0700 | [diff] [blame] | 259 | err = do_write_data_page(&fio); |
Jaegeuk Kim | 70c640b | 2014-12-10 13:59:33 -0800 | [diff] [blame] | 260 | submit_bio = true; |
Jaegeuk Kim | edb27de | 2015-07-25 00:52:52 -0700 | [diff] [blame] | 261 | if (err) { |
| 262 | unlock_page(cur->page); |
| 263 | break; |
| 264 | } |
Jaegeuk Kim | 70c640b | 2014-12-10 13:59:33 -0800 | [diff] [blame] | 265 | } |
Jaegeuk Kim | 70c640b | 2014-12-10 13:59:33 -0800 | [diff] [blame] | 266 | } else { |
Jaegeuk Kim | 8ce67cb | 2015-03-17 17:58:08 -0700 | [diff] [blame] | 267 | trace_f2fs_commit_inmem_page(cur->page, INMEM_DROP); |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 268 | } |
Chao Yu | decd36b | 2015-08-07 18:42:09 +0800 | [diff] [blame] | 269 | set_page_private(cur->page, 0); |
| 270 | ClearPagePrivate(cur->page); |
| 271 | f2fs_put_page(cur->page, 1); |
| 272 | |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 273 | list_del(&cur->list); |
| 274 | kmem_cache_free(inmem_entry_slab, cur); |
Jaegeuk Kim | 8dcf2ff7 | 2014-12-05 17:18:15 -0800 | [diff] [blame] | 275 | dec_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES); |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 276 | } |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 277 | mutex_unlock(&fi->inmem_lock); |
| 278 | |
Jaegeuk Kim | 70c640b | 2014-12-10 13:59:33 -0800 | [diff] [blame] | 279 | if (!abort) { |
| 280 | f2fs_unlock_op(sbi); |
| 281 | if (submit_bio) |
| 282 | f2fs_submit_merged_bio(sbi, DATA, WRITE); |
| 283 | } |
Jaegeuk Kim | edb27de | 2015-07-25 00:52:52 -0700 | [diff] [blame] | 284 | return err; |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 287 | /* |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 288 | * This function balances dirty node and dentry pages. |
| 289 | * In addition, it controls garbage collection. |
| 290 | */ |
| 291 | void f2fs_balance_fs(struct f2fs_sb_info *sbi) |
| 292 | { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 293 | /* |
Jaegeuk Kim | 029cd28 | 2012-12-21 17:20:21 +0900 | [diff] [blame] | 294 | * We should do GC or end up with checkpoint, if there are so many dirty |
| 295 | * dir/node pages without enough free segments. |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 296 | */ |
Jaegeuk Kim | 4372752 | 2013-02-04 15:11:17 +0900 | [diff] [blame] | 297 | if (has_not_enough_free_secs(sbi, 0)) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 298 | mutex_lock(&sbi->gc_mutex); |
Chao Yu | d530d4d | 2015-10-05 22:22:44 +0800 | [diff] [blame] | 299 | f2fs_gc(sbi, false); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | |
Jaegeuk Kim | 4660f9c | 2013-10-24 14:19:18 +0900 | [diff] [blame] | 303 | void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi) |
| 304 | { |
Chao Yu | 1dcc336 | 2015-02-05 17:57:31 +0800 | [diff] [blame] | 305 | /* try to shrink extent cache when there is no enough memory */ |
Jaegeuk Kim | 554df79 | 2015-06-19 13:41:23 -0700 | [diff] [blame] | 306 | if (!available_free_memory(sbi, EXTENT_CACHE)) |
| 307 | f2fs_shrink_extent_tree(sbi, EXTENT_CACHE_SHRINK_NUMBER); |
Chao Yu | 1dcc336 | 2015-02-05 17:57:31 +0800 | [diff] [blame] | 308 | |
Jaegeuk Kim | 1b38dc8 | 2015-06-19 15:36:07 -0700 | [diff] [blame] | 309 | /* check the # of cached NAT entries */ |
| 310 | if (!available_free_memory(sbi, NAT_ENTRIES)) |
| 311 | try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK); |
| 312 | |
Chao Yu | 3169658 | 2015-07-28 18:33:46 +0800 | [diff] [blame] | 313 | if (!available_free_memory(sbi, FREE_NIDS)) |
| 314 | try_to_free_nids(sbi, NAT_ENTRY_PER_BLOCK * FREE_NID_PAGES); |
| 315 | |
Jaegeuk Kim | 1b38dc8 | 2015-06-19 15:36:07 -0700 | [diff] [blame] | 316 | /* checkpoint is the only way to shrink partial cached entries */ |
| 317 | if (!available_free_memory(sbi, NAT_ENTRIES) || |
Jaegeuk Kim | e5e7ea3 | 2014-11-06 15:24:46 -0800 | [diff] [blame] | 318 | excess_prefree_segs(sbi) || |
Jaegeuk Kim | 60b99b4 | 2015-10-05 14:49:57 -0700 | [diff] [blame^] | 319 | !available_free_memory(sbi, INO_ENTRIES) || |
| 320 | jiffies > sbi->cp_expires) |
Jaegeuk Kim | 4660f9c | 2013-10-24 14:19:18 +0900 | [diff] [blame] | 321 | f2fs_sync_fs(sbi->sb, true); |
| 322 | } |
| 323 | |
Gu Zheng | 2163d19 | 2014-04-27 14:21:33 +0800 | [diff] [blame] | 324 | static int issue_flush_thread(void *data) |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 325 | { |
| 326 | struct f2fs_sb_info *sbi = data; |
Gu Zheng | a688b9d9e | 2014-04-27 14:21:21 +0800 | [diff] [blame] | 327 | struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info; |
| 328 | wait_queue_head_t *q = &fcc->flush_wait_queue; |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 329 | repeat: |
| 330 | if (kthread_should_stop()) |
| 331 | return 0; |
| 332 | |
Gu Zheng | 721bd4d | 2014-09-05 18:31:00 +0800 | [diff] [blame] | 333 | if (!llist_empty(&fcc->issue_list)) { |
Jaegeuk Kim | 740432f | 2015-08-14 11:43:56 -0700 | [diff] [blame] | 334 | struct bio *bio; |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 335 | struct flush_cmd *cmd, *next; |
| 336 | int ret; |
| 337 | |
Jaegeuk Kim | 740432f | 2015-08-14 11:43:56 -0700 | [diff] [blame] | 338 | bio = f2fs_bio_alloc(0); |
| 339 | |
Gu Zheng | 721bd4d | 2014-09-05 18:31:00 +0800 | [diff] [blame] | 340 | fcc->dispatch_list = llist_del_all(&fcc->issue_list); |
| 341 | fcc->dispatch_list = llist_reverse_order(fcc->dispatch_list); |
| 342 | |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 343 | bio->bi_bdev = sbi->sb->s_bdev; |
| 344 | ret = submit_bio_wait(WRITE_FLUSH, bio); |
| 345 | |
Gu Zheng | 721bd4d | 2014-09-05 18:31:00 +0800 | [diff] [blame] | 346 | llist_for_each_entry_safe(cmd, next, |
| 347 | fcc->dispatch_list, llnode) { |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 348 | cmd->ret = ret; |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 349 | complete(&cmd->wait); |
| 350 | } |
Gu Zheng | a4ed23f | 2014-04-11 17:49:35 +0800 | [diff] [blame] | 351 | bio_put(bio); |
Gu Zheng | a688b9d9e | 2014-04-27 14:21:21 +0800 | [diff] [blame] | 352 | fcc->dispatch_list = NULL; |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 353 | } |
| 354 | |
Gu Zheng | a688b9d9e | 2014-04-27 14:21:21 +0800 | [diff] [blame] | 355 | wait_event_interruptible(*q, |
Gu Zheng | 721bd4d | 2014-09-05 18:31:00 +0800 | [diff] [blame] | 356 | kthread_should_stop() || !llist_empty(&fcc->issue_list)); |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 357 | goto repeat; |
| 358 | } |
| 359 | |
| 360 | int f2fs_issue_flush(struct f2fs_sb_info *sbi) |
| 361 | { |
Gu Zheng | a688b9d9e | 2014-04-27 14:21:21 +0800 | [diff] [blame] | 362 | struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info; |
Chao Yu | adf8d90 | 2014-05-08 17:00:35 +0800 | [diff] [blame] | 363 | struct flush_cmd cmd; |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 364 | |
Jaegeuk Kim | 24a9ee0 | 2014-07-25 17:46:10 -0700 | [diff] [blame] | 365 | trace_f2fs_issue_flush(sbi->sb, test_opt(sbi, NOBARRIER), |
| 366 | test_opt(sbi, FLUSH_MERGE)); |
| 367 | |
Jaegeuk Kim | 0f7b2ab | 2014-07-23 09:57:31 -0700 | [diff] [blame] | 368 | if (test_opt(sbi, NOBARRIER)) |
| 369 | return 0; |
| 370 | |
Jaegeuk Kim | 740432f | 2015-08-14 11:43:56 -0700 | [diff] [blame] | 371 | if (!test_opt(sbi, FLUSH_MERGE)) { |
| 372 | struct bio *bio = f2fs_bio_alloc(0); |
| 373 | int ret; |
| 374 | |
| 375 | bio->bi_bdev = sbi->sb->s_bdev; |
| 376 | ret = submit_bio_wait(WRITE_FLUSH, bio); |
| 377 | bio_put(bio); |
| 378 | return ret; |
| 379 | } |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 380 | |
Chao Yu | adf8d90 | 2014-05-08 17:00:35 +0800 | [diff] [blame] | 381 | init_completion(&cmd.wait); |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 382 | |
Gu Zheng | 721bd4d | 2014-09-05 18:31:00 +0800 | [diff] [blame] | 383 | llist_add(&cmd.llnode, &fcc->issue_list); |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 384 | |
Gu Zheng | a688b9d9e | 2014-04-27 14:21:21 +0800 | [diff] [blame] | 385 | if (!fcc->dispatch_list) |
| 386 | wake_up(&fcc->flush_wait_queue); |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 387 | |
Chao Yu | adf8d90 | 2014-05-08 17:00:35 +0800 | [diff] [blame] | 388 | wait_for_completion(&cmd.wait); |
| 389 | |
| 390 | return cmd.ret; |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 391 | } |
| 392 | |
Gu Zheng | 2163d19 | 2014-04-27 14:21:33 +0800 | [diff] [blame] | 393 | int create_flush_cmd_control(struct f2fs_sb_info *sbi) |
| 394 | { |
| 395 | dev_t dev = sbi->sb->s_bdev->bd_dev; |
| 396 | struct flush_cmd_control *fcc; |
| 397 | int err = 0; |
| 398 | |
| 399 | fcc = kzalloc(sizeof(struct flush_cmd_control), GFP_KERNEL); |
| 400 | if (!fcc) |
| 401 | return -ENOMEM; |
Gu Zheng | 2163d19 | 2014-04-27 14:21:33 +0800 | [diff] [blame] | 402 | init_waitqueue_head(&fcc->flush_wait_queue); |
Gu Zheng | 721bd4d | 2014-09-05 18:31:00 +0800 | [diff] [blame] | 403 | init_llist_head(&fcc->issue_list); |
Chao Yu | 6b2920a | 2014-07-07 11:21:59 +0800 | [diff] [blame] | 404 | SM_I(sbi)->cmd_control_info = fcc; |
Gu Zheng | 2163d19 | 2014-04-27 14:21:33 +0800 | [diff] [blame] | 405 | fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi, |
| 406 | "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev)); |
| 407 | if (IS_ERR(fcc->f2fs_issue_flush)) { |
| 408 | err = PTR_ERR(fcc->f2fs_issue_flush); |
| 409 | kfree(fcc); |
Chao Yu | 6b2920a | 2014-07-07 11:21:59 +0800 | [diff] [blame] | 410 | SM_I(sbi)->cmd_control_info = NULL; |
Gu Zheng | 2163d19 | 2014-04-27 14:21:33 +0800 | [diff] [blame] | 411 | return err; |
| 412 | } |
Gu Zheng | 2163d19 | 2014-04-27 14:21:33 +0800 | [diff] [blame] | 413 | |
| 414 | return err; |
| 415 | } |
| 416 | |
| 417 | void destroy_flush_cmd_control(struct f2fs_sb_info *sbi) |
| 418 | { |
Chao Yu | 6b2920a | 2014-07-07 11:21:59 +0800 | [diff] [blame] | 419 | struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info; |
Gu Zheng | 2163d19 | 2014-04-27 14:21:33 +0800 | [diff] [blame] | 420 | |
| 421 | if (fcc && fcc->f2fs_issue_flush) |
| 422 | kthread_stop(fcc->f2fs_issue_flush); |
| 423 | kfree(fcc); |
Chao Yu | 6b2920a | 2014-07-07 11:21:59 +0800 | [diff] [blame] | 424 | SM_I(sbi)->cmd_control_info = NULL; |
Gu Zheng | 2163d19 | 2014-04-27 14:21:33 +0800 | [diff] [blame] | 425 | } |
| 426 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 427 | static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno, |
| 428 | enum dirty_type dirty_type) |
| 429 | { |
| 430 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
| 431 | |
| 432 | /* need not be added */ |
| 433 | if (IS_CURSEG(sbi, segno)) |
| 434 | return; |
| 435 | |
| 436 | if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type])) |
| 437 | dirty_i->nr_dirty[dirty_type]++; |
| 438 | |
| 439 | if (dirty_type == DIRTY) { |
| 440 | struct seg_entry *sentry = get_seg_entry(sbi, segno); |
Changman Lee | 4625d6a | 2013-10-25 17:31:57 +0900 | [diff] [blame] | 441 | enum dirty_type t = sentry->type; |
Jaegeuk Kim | b2f2c39 | 2013-04-01 13:52:09 +0900 | [diff] [blame] | 442 | |
Jaegeuk Kim | ec325b5 | 2014-09-02 16:24:11 -0700 | [diff] [blame] | 443 | if (unlikely(t >= DIRTY)) { |
| 444 | f2fs_bug_on(sbi, 1); |
| 445 | return; |
| 446 | } |
Changman Lee | 4625d6a | 2013-10-25 17:31:57 +0900 | [diff] [blame] | 447 | if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t])) |
| 448 | dirty_i->nr_dirty[t]++; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | |
| 452 | static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno, |
| 453 | enum dirty_type dirty_type) |
| 454 | { |
| 455 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
| 456 | |
| 457 | if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type])) |
| 458 | dirty_i->nr_dirty[dirty_type]--; |
| 459 | |
| 460 | if (dirty_type == DIRTY) { |
Changman Lee | 4625d6a | 2013-10-25 17:31:57 +0900 | [diff] [blame] | 461 | struct seg_entry *sentry = get_seg_entry(sbi, segno); |
| 462 | enum dirty_type t = sentry->type; |
Jaegeuk Kim | b2f2c39 | 2013-04-01 13:52:09 +0900 | [diff] [blame] | 463 | |
Changman Lee | 4625d6a | 2013-10-25 17:31:57 +0900 | [diff] [blame] | 464 | if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t])) |
| 465 | dirty_i->nr_dirty[t]--; |
Jaegeuk Kim | b2f2c39 | 2013-04-01 13:52:09 +0900 | [diff] [blame] | 466 | |
Jaegeuk Kim | 5ec4e49 | 2013-03-31 13:26:03 +0900 | [diff] [blame] | 467 | if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0) |
| 468 | clear_bit(GET_SECNO(sbi, segno), |
| 469 | dirty_i->victim_secmap); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 470 | } |
| 471 | } |
| 472 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 473 | /* |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 474 | * Should not occur error such as -ENOMEM. |
| 475 | * Adding dirty entry into seglist is not critical operation. |
| 476 | * If a given segment is one of current working segments, it won't be added. |
| 477 | */ |
Haicheng Li | 8d8451a | 2013-06-13 16:59:28 +0800 | [diff] [blame] | 478 | 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] | 479 | { |
| 480 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
| 481 | unsigned short valid_blocks; |
| 482 | |
| 483 | if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno)) |
| 484 | return; |
| 485 | |
| 486 | mutex_lock(&dirty_i->seglist_lock); |
| 487 | |
| 488 | valid_blocks = get_valid_blocks(sbi, segno, 0); |
| 489 | |
| 490 | if (valid_blocks == 0) { |
| 491 | __locate_dirty_segment(sbi, segno, PRE); |
| 492 | __remove_dirty_segment(sbi, segno, DIRTY); |
| 493 | } else if (valid_blocks < sbi->blocks_per_seg) { |
| 494 | __locate_dirty_segment(sbi, segno, DIRTY); |
| 495 | } else { |
| 496 | /* Recovery routine with SSR needs this */ |
| 497 | __remove_dirty_segment(sbi, segno, DIRTY); |
| 498 | } |
| 499 | |
| 500 | mutex_unlock(&dirty_i->seglist_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 501 | } |
| 502 | |
Jaegeuk Kim | 1e87a78 | 2014-04-15 13:57:55 +0900 | [diff] [blame] | 503 | static int f2fs_issue_discard(struct f2fs_sb_info *sbi, |
Jaegeuk Kim | 3720887 | 2013-11-12 16:55:17 +0900 | [diff] [blame] | 504 | block_t blkstart, block_t blklen) |
| 505 | { |
Chao Yu | 55cf9cb | 2014-09-15 18:01:10 +0800 | [diff] [blame] | 506 | sector_t start = SECTOR_FROM_BLOCK(blkstart); |
| 507 | sector_t len = SECTOR_FROM_BLOCK(blklen); |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 508 | struct seg_entry *se; |
| 509 | unsigned int offset; |
| 510 | block_t i; |
| 511 | |
| 512 | for (i = blkstart; i < blkstart + blklen; i++) { |
| 513 | se = get_seg_entry(sbi, GET_SEGNO(sbi, i)); |
| 514 | offset = GET_BLKOFF_FROM_SEG0(sbi, i); |
| 515 | |
| 516 | if (!f2fs_test_and_set_bit(offset, se->discard_map)) |
| 517 | sbi->discard_blks--; |
| 518 | } |
Jaegeuk Kim | 1661d07 | 2013-11-12 17:01:00 +0900 | [diff] [blame] | 519 | trace_f2fs_issue_discard(sbi->sb, blkstart, blklen); |
Jaegeuk Kim | 1e87a78 | 2014-04-15 13:57:55 +0900 | [diff] [blame] | 520 | return blkdev_issue_discard(sbi->sb->s_bdev, start, len, GFP_NOFS, 0); |
| 521 | } |
| 522 | |
Chao Yu | e90c2d2 | 2015-07-28 18:36:47 +0800 | [diff] [blame] | 523 | bool discard_next_dnode(struct f2fs_sb_info *sbi, block_t blkaddr) |
Jaegeuk Kim | 1e87a78 | 2014-04-15 13:57:55 +0900 | [diff] [blame] | 524 | { |
Jaegeuk Kim | 40a02be | 2015-05-11 20:03:49 -0700 | [diff] [blame] | 525 | int err = -ENOTSUPP; |
| 526 | |
| 527 | if (test_opt(sbi, DISCARD)) { |
| 528 | struct seg_entry *se = get_seg_entry(sbi, |
| 529 | GET_SEGNO(sbi, blkaddr)); |
| 530 | unsigned int offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr); |
| 531 | |
| 532 | if (f2fs_test_bit(offset, se->discard_map)) |
Chao Yu | e90c2d2 | 2015-07-28 18:36:47 +0800 | [diff] [blame] | 533 | return false; |
Jaegeuk Kim | 40a02be | 2015-05-11 20:03:49 -0700 | [diff] [blame] | 534 | |
| 535 | err = f2fs_issue_discard(sbi, blkaddr, 1); |
| 536 | } |
| 537 | |
Chao Yu | e90c2d2 | 2015-07-28 18:36:47 +0800 | [diff] [blame] | 538 | if (err) { |
Chao Yu | 381722d | 2015-05-19 17:40:04 +0800 | [diff] [blame] | 539 | update_meta_page(sbi, NULL, blkaddr); |
Chao Yu | e90c2d2 | 2015-07-28 18:36:47 +0800 | [diff] [blame] | 540 | return true; |
| 541 | } |
| 542 | return false; |
Jaegeuk Kim | 3720887 | 2013-11-12 16:55:17 +0900 | [diff] [blame] | 543 | } |
| 544 | |
Jaegeuk Kim | adf4983 | 2014-10-28 22:27:59 -0700 | [diff] [blame] | 545 | static void __add_discard_entry(struct f2fs_sb_info *sbi, |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 546 | struct cp_control *cpc, struct seg_entry *se, |
| 547 | unsigned int start, unsigned int end) |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 548 | { |
| 549 | struct list_head *head = &SM_I(sbi)->discard_list; |
Jaegeuk Kim | adf4983 | 2014-10-28 22:27:59 -0700 | [diff] [blame] | 550 | struct discard_entry *new, *last; |
| 551 | |
| 552 | if (!list_empty(head)) { |
| 553 | last = list_last_entry(head, struct discard_entry, list); |
| 554 | if (START_BLOCK(sbi, cpc->trim_start) + start == |
| 555 | last->blkaddr + last->len) { |
| 556 | last->len += end - start; |
| 557 | goto done; |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | new = f2fs_kmem_cache_alloc(discard_entry_slab, GFP_NOFS); |
| 562 | INIT_LIST_HEAD(&new->list); |
| 563 | new->blkaddr = START_BLOCK(sbi, cpc->trim_start) + start; |
| 564 | new->len = end - start; |
| 565 | list_add_tail(&new->list, head); |
| 566 | done: |
| 567 | SM_I(sbi)->nr_discards += end - start; |
Jaegeuk Kim | adf4983 | 2014-10-28 22:27:59 -0700 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | static void add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc) |
| 571 | { |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 572 | int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long); |
| 573 | int max_blocks = sbi->blocks_per_seg; |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 574 | struct seg_entry *se = get_seg_entry(sbi, cpc->trim_start); |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 575 | unsigned long *cur_map = (unsigned long *)se->cur_valid_map; |
| 576 | unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map; |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 577 | unsigned long *discard_map = (unsigned long *)se->discard_map; |
Jaegeuk Kim | 60a3b78 | 2015-02-10 16:44:29 -0800 | [diff] [blame] | 578 | unsigned long *dmap = SIT_I(sbi)->tmp_map; |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 579 | unsigned int start = 0, end = -1; |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 580 | bool force = (cpc->reason == CP_DISCARD); |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 581 | int i; |
| 582 | |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 583 | if (se->valid_blocks == max_blocks) |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 584 | return; |
| 585 | |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 586 | if (!force) { |
| 587 | if (!test_opt(sbi, DISCARD) || !se->valid_blocks || |
Dan Carpenter | 912a83b | 2015-05-14 11:52:28 +0300 | [diff] [blame] | 588 | SM_I(sbi)->nr_discards >= SM_I(sbi)->max_discards) |
| 589 | return; |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 590 | } |
| 591 | |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 592 | /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */ |
| 593 | for (i = 0; i < entries; i++) |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 594 | dmap[i] = force ? ~ckpt_map[i] & ~discard_map[i] : |
Jaegeuk Kim | d7bc248 | 2014-12-12 13:53:41 -0800 | [diff] [blame] | 595 | (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i]; |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 596 | |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 597 | while (force || SM_I(sbi)->nr_discards <= SM_I(sbi)->max_discards) { |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 598 | start = __find_rev_next_bit(dmap, max_blocks, end + 1); |
| 599 | if (start >= max_blocks) |
| 600 | break; |
| 601 | |
| 602 | end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1); |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 603 | __add_discard_entry(sbi, cpc, se, start, end); |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 604 | } |
| 605 | } |
| 606 | |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 607 | void release_discard_addrs(struct f2fs_sb_info *sbi) |
| 608 | { |
| 609 | struct list_head *head = &(SM_I(sbi)->discard_list); |
| 610 | struct discard_entry *entry, *this; |
| 611 | |
| 612 | /* drop caches */ |
| 613 | list_for_each_entry_safe(entry, this, head, list) { |
| 614 | list_del(&entry->list); |
| 615 | kmem_cache_free(discard_entry_slab, entry); |
| 616 | } |
| 617 | } |
| 618 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 619 | /* |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 620 | * Should call clear_prefree_segments after checkpoint is done. |
| 621 | */ |
| 622 | static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi) |
| 623 | { |
| 624 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
Chao Yu | b65ee14 | 2014-08-04 10:10:07 +0800 | [diff] [blame] | 625 | unsigned int segno; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 626 | |
| 627 | mutex_lock(&dirty_i->seglist_lock); |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 628 | 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] | 629 | __set_test_and_free(sbi, segno); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 630 | mutex_unlock(&dirty_i->seglist_lock); |
| 631 | } |
| 632 | |
Jaegeuk Kim | 836b5a6 | 2015-04-30 22:50:06 -0700 | [diff] [blame] | 633 | 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] | 634 | { |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 635 | struct list_head *head = &(SM_I(sbi)->discard_list); |
Chao Yu | 2d7b822 | 2014-03-29 11:33:17 +0800 | [diff] [blame] | 636 | struct discard_entry *entry, *this; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 637 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
Changman Lee | 29e59c1 | 2013-11-11 09:24:37 +0900 | [diff] [blame] | 638 | unsigned long *prefree_map = dirty_i->dirty_segmap[PRE]; |
Changman Lee | 29e59c1 | 2013-11-11 09:24:37 +0900 | [diff] [blame] | 639 | unsigned int start = 0, end = -1; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 640 | |
| 641 | mutex_lock(&dirty_i->seglist_lock); |
Changman Lee | 29e59c1 | 2013-11-11 09:24:37 +0900 | [diff] [blame] | 642 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 643 | while (1) { |
Changman Lee | 29e59c1 | 2013-11-11 09:24:37 +0900 | [diff] [blame] | 644 | int i; |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 645 | start = find_next_bit(prefree_map, MAIN_SEGS(sbi), end + 1); |
| 646 | if (start >= MAIN_SEGS(sbi)) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 647 | break; |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 648 | end = find_next_zero_bit(prefree_map, MAIN_SEGS(sbi), |
| 649 | start + 1); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 650 | |
Changman Lee | 29e59c1 | 2013-11-11 09:24:37 +0900 | [diff] [blame] | 651 | for (i = start; i < end; i++) |
| 652 | clear_bit(i, prefree_map); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 653 | |
Changman Lee | 29e59c1 | 2013-11-11 09:24:37 +0900 | [diff] [blame] | 654 | dirty_i->nr_dirty[PRE] -= end - start; |
| 655 | |
| 656 | if (!test_opt(sbi, DISCARD)) |
| 657 | continue; |
| 658 | |
Jaegeuk Kim | 3720887 | 2013-11-12 16:55:17 +0900 | [diff] [blame] | 659 | f2fs_issue_discard(sbi, START_BLOCK(sbi, start), |
| 660 | (end - start) << sbi->log_blocks_per_seg); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 661 | } |
| 662 | mutex_unlock(&dirty_i->seglist_lock); |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 663 | |
| 664 | /* send small discards */ |
Chao Yu | 2d7b822 | 2014-03-29 11:33:17 +0800 | [diff] [blame] | 665 | list_for_each_entry_safe(entry, this, head, list) { |
Jaegeuk Kim | 836b5a6 | 2015-04-30 22:50:06 -0700 | [diff] [blame] | 666 | if (cpc->reason == CP_DISCARD && entry->len < cpc->trim_minlen) |
| 667 | goto skip; |
Jaegeuk Kim | 3720887 | 2013-11-12 16:55:17 +0900 | [diff] [blame] | 668 | f2fs_issue_discard(sbi, entry->blkaddr, entry->len); |
Jaegeuk Kim | f56aa1c | 2015-06-02 15:48:20 -0700 | [diff] [blame] | 669 | cpc->trimmed += entry->len; |
Jaegeuk Kim | 836b5a6 | 2015-04-30 22:50:06 -0700 | [diff] [blame] | 670 | skip: |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 671 | list_del(&entry->list); |
| 672 | SM_I(sbi)->nr_discards -= entry->len; |
| 673 | kmem_cache_free(discard_entry_slab, entry); |
| 674 | } |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 675 | } |
| 676 | |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 677 | 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] | 678 | { |
| 679 | struct sit_info *sit_i = SIT_I(sbi); |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 680 | |
| 681 | if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap)) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 682 | sit_i->dirty_sentries++; |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 683 | return false; |
| 684 | } |
| 685 | |
| 686 | return true; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type, |
| 690 | unsigned int segno, int modified) |
| 691 | { |
| 692 | struct seg_entry *se = get_seg_entry(sbi, segno); |
| 693 | se->type = type; |
| 694 | if (modified) |
| 695 | __mark_sit_entry_dirty(sbi, segno); |
| 696 | } |
| 697 | |
| 698 | static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del) |
| 699 | { |
| 700 | struct seg_entry *se; |
| 701 | unsigned int segno, offset; |
| 702 | long int new_vblocks; |
| 703 | |
| 704 | segno = GET_SEGNO(sbi, blkaddr); |
| 705 | |
| 706 | se = get_seg_entry(sbi, segno); |
| 707 | new_vblocks = se->valid_blocks + del; |
Jaegeuk Kim | 491c085 | 2014-02-04 13:01:10 +0900 | [diff] [blame] | 708 | offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 709 | |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 710 | f2fs_bug_on(sbi, (new_vblocks >> (sizeof(unsigned short) << 3) || |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 711 | (new_vblocks > sbi->blocks_per_seg))); |
| 712 | |
| 713 | se->valid_blocks = new_vblocks; |
| 714 | se->mtime = get_mtime(sbi); |
| 715 | SIT_I(sbi)->max_mtime = se->mtime; |
| 716 | |
| 717 | /* Update valid block bitmap */ |
| 718 | if (del > 0) { |
Gu Zheng | 52aca07 | 2014-10-20 17:45:51 +0800 | [diff] [blame] | 719 | if (f2fs_test_and_set_bit(offset, se->cur_valid_map)) |
Jaegeuk Kim | 0579676 | 2014-09-02 16:05:00 -0700 | [diff] [blame] | 720 | f2fs_bug_on(sbi, 1); |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 721 | if (!f2fs_test_and_set_bit(offset, se->discard_map)) |
| 722 | sbi->discard_blks--; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 723 | } else { |
Gu Zheng | 52aca07 | 2014-10-20 17:45:51 +0800 | [diff] [blame] | 724 | if (!f2fs_test_and_clear_bit(offset, se->cur_valid_map)) |
Jaegeuk Kim | 0579676 | 2014-09-02 16:05:00 -0700 | [diff] [blame] | 725 | f2fs_bug_on(sbi, 1); |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 726 | if (f2fs_test_and_clear_bit(offset, se->discard_map)) |
| 727 | sbi->discard_blks++; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 728 | } |
| 729 | if (!f2fs_test_bit(offset, se->ckpt_valid_map)) |
| 730 | se->ckpt_valid_blocks += del; |
| 731 | |
| 732 | __mark_sit_entry_dirty(sbi, segno); |
| 733 | |
| 734 | /* update total number of valid blocks to be written in ckpt area */ |
| 735 | SIT_I(sbi)->written_valid_blocks += del; |
| 736 | |
| 737 | if (sbi->segs_per_sec > 1) |
| 738 | get_sec_entry(sbi, segno)->valid_blocks += del; |
| 739 | } |
| 740 | |
Jaegeuk Kim | 5e44381 | 2014-01-28 12:22:14 +0900 | [diff] [blame] | 741 | 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] | 742 | { |
Jaegeuk Kim | 5e44381 | 2014-01-28 12:22:14 +0900 | [diff] [blame] | 743 | update_sit_entry(sbi, new, 1); |
| 744 | if (GET_SEGNO(sbi, old) != NULL_SEGNO) |
| 745 | update_sit_entry(sbi, old, -1); |
| 746 | |
| 747 | locate_dirty_segment(sbi, GET_SEGNO(sbi, old)); |
| 748 | locate_dirty_segment(sbi, GET_SEGNO(sbi, new)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr) |
| 752 | { |
| 753 | unsigned int segno = GET_SEGNO(sbi, addr); |
| 754 | struct sit_info *sit_i = SIT_I(sbi); |
| 755 | |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 756 | f2fs_bug_on(sbi, addr == NULL_ADDR); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 757 | if (addr == NEW_ADDR) |
| 758 | return; |
| 759 | |
| 760 | /* add it into sit main buffer */ |
| 761 | mutex_lock(&sit_i->sentry_lock); |
| 762 | |
| 763 | update_sit_entry(sbi, addr, -1); |
| 764 | |
| 765 | /* add it into dirty seglist */ |
| 766 | locate_dirty_segment(sbi, segno); |
| 767 | |
| 768 | mutex_unlock(&sit_i->sentry_lock); |
| 769 | } |
| 770 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 771 | /* |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 772 | * This function should be resided under the curseg_mutex lock |
| 773 | */ |
| 774 | static void __add_sum_entry(struct f2fs_sb_info *sbi, int type, |
Haicheng Li | e79efe3 | 2013-06-13 16:59:27 +0800 | [diff] [blame] | 775 | struct f2fs_summary *sum) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 776 | { |
| 777 | struct curseg_info *curseg = CURSEG_I(sbi, type); |
| 778 | void *addr = curseg->sum_blk; |
Haicheng Li | e79efe3 | 2013-06-13 16:59:27 +0800 | [diff] [blame] | 779 | addr += curseg->next_blkoff * sizeof(struct f2fs_summary); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 780 | memcpy(addr, sum, sizeof(struct f2fs_summary)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 781 | } |
| 782 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 783 | /* |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 784 | * Calculate the number of current summary pages for writing |
| 785 | */ |
Chao Yu | 3fa06d7 | 2014-12-09 14:21:46 +0800 | [diff] [blame] | 786 | 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] | 787 | { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 788 | int valid_sum_count = 0; |
Fan Li | 9a47938 | 2013-10-29 16:21:47 +0800 | [diff] [blame] | 789 | int i, sum_in_page; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 790 | |
| 791 | for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) { |
| 792 | if (sbi->ckpt->alloc_type[i] == SSR) |
| 793 | valid_sum_count += sbi->blocks_per_seg; |
Chao Yu | 3fa06d7 | 2014-12-09 14:21:46 +0800 | [diff] [blame] | 794 | else { |
| 795 | if (for_ra) |
| 796 | valid_sum_count += le16_to_cpu( |
| 797 | F2FS_CKPT(sbi)->cur_data_blkoff[i]); |
| 798 | else |
| 799 | valid_sum_count += curseg_blkoff(sbi, i); |
| 800 | } |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 801 | } |
| 802 | |
Fan Li | 9a47938 | 2013-10-29 16:21:47 +0800 | [diff] [blame] | 803 | sum_in_page = (PAGE_CACHE_SIZE - 2 * SUM_JOURNAL_SIZE - |
| 804 | SUM_FOOTER_SIZE) / SUMMARY_SIZE; |
| 805 | if (valid_sum_count <= sum_in_page) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 806 | return 1; |
Fan Li | 9a47938 | 2013-10-29 16:21:47 +0800 | [diff] [blame] | 807 | else if ((valid_sum_count - sum_in_page) <= |
| 808 | (PAGE_CACHE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 809 | return 2; |
| 810 | return 3; |
| 811 | } |
| 812 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 813 | /* |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 814 | * Caller should put this summary page |
| 815 | */ |
| 816 | struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno) |
| 817 | { |
| 818 | return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno)); |
| 819 | } |
| 820 | |
Chao Yu | 381722d | 2015-05-19 17:40:04 +0800 | [diff] [blame] | 821 | void update_meta_page(struct f2fs_sb_info *sbi, void *src, block_t blk_addr) |
| 822 | { |
| 823 | struct page *page = grab_meta_page(sbi, blk_addr); |
| 824 | void *dst = page_address(page); |
| 825 | |
| 826 | if (src) |
| 827 | memcpy(dst, src, PAGE_CACHE_SIZE); |
| 828 | else |
| 829 | memset(dst, 0, PAGE_CACHE_SIZE); |
| 830 | set_page_dirty(page); |
| 831 | f2fs_put_page(page, 1); |
| 832 | } |
| 833 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 834 | static void write_sum_page(struct f2fs_sb_info *sbi, |
| 835 | struct f2fs_summary_block *sum_blk, block_t blk_addr) |
| 836 | { |
Chao Yu | 381722d | 2015-05-19 17:40:04 +0800 | [diff] [blame] | 837 | update_meta_page(sbi, (void *)sum_blk, blk_addr); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 838 | } |
| 839 | |
Jaegeuk Kim | 6037468 | 2013-03-31 13:58:51 +0900 | [diff] [blame] | 840 | static int is_next_segment_free(struct f2fs_sb_info *sbi, int type) |
| 841 | { |
| 842 | struct curseg_info *curseg = CURSEG_I(sbi, type); |
Haicheng Li | 81fb5e8 | 2013-05-14 18:20:28 +0800 | [diff] [blame] | 843 | unsigned int segno = curseg->segno + 1; |
Jaegeuk Kim | 6037468 | 2013-03-31 13:58:51 +0900 | [diff] [blame] | 844 | struct free_segmap_info *free_i = FREE_I(sbi); |
| 845 | |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 846 | if (segno < MAIN_SEGS(sbi) && segno % sbi->segs_per_sec) |
Haicheng Li | 81fb5e8 | 2013-05-14 18:20:28 +0800 | [diff] [blame] | 847 | return !test_bit(segno, free_i->free_segmap); |
Jaegeuk Kim | 6037468 | 2013-03-31 13:58:51 +0900 | [diff] [blame] | 848 | return 0; |
| 849 | } |
| 850 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 851 | /* |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 852 | * Find a new segment from the free segments bitmap to right order |
| 853 | * This function should be returned with success, otherwise BUG |
| 854 | */ |
| 855 | static void get_new_segment(struct f2fs_sb_info *sbi, |
| 856 | unsigned int *newseg, bool new_sec, int dir) |
| 857 | { |
| 858 | struct free_segmap_info *free_i = FREE_I(sbi); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 859 | unsigned int segno, secno, zoneno; |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 860 | unsigned int total_zones = MAIN_SECS(sbi) / sbi->secs_per_zone; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 861 | unsigned int hint = *newseg / sbi->segs_per_sec; |
| 862 | unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg); |
| 863 | unsigned int left_start = hint; |
| 864 | bool init = true; |
| 865 | int go_left = 0; |
| 866 | int i; |
| 867 | |
Chao Yu | 1a118cc | 2015-02-11 18:20:38 +0800 | [diff] [blame] | 868 | spin_lock(&free_i->segmap_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 869 | |
| 870 | if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) { |
| 871 | segno = find_next_zero_bit(free_i->free_segmap, |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 872 | MAIN_SEGS(sbi), *newseg + 1); |
Jaegeuk Kim | 33afa7f | 2013-03-31 12:59:53 +0900 | [diff] [blame] | 873 | if (segno - *newseg < sbi->segs_per_sec - |
| 874 | (*newseg % sbi->segs_per_sec)) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 875 | goto got_it; |
| 876 | } |
| 877 | find_other_zone: |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 878 | secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint); |
| 879 | if (secno >= MAIN_SECS(sbi)) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 880 | if (dir == ALLOC_RIGHT) { |
| 881 | secno = find_next_zero_bit(free_i->free_secmap, |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 882 | MAIN_SECS(sbi), 0); |
| 883 | f2fs_bug_on(sbi, secno >= MAIN_SECS(sbi)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 884 | } else { |
| 885 | go_left = 1; |
| 886 | left_start = hint - 1; |
| 887 | } |
| 888 | } |
| 889 | if (go_left == 0) |
| 890 | goto skip_left; |
| 891 | |
| 892 | while (test_bit(left_start, free_i->free_secmap)) { |
| 893 | if (left_start > 0) { |
| 894 | left_start--; |
| 895 | continue; |
| 896 | } |
| 897 | left_start = find_next_zero_bit(free_i->free_secmap, |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 898 | MAIN_SECS(sbi), 0); |
| 899 | f2fs_bug_on(sbi, left_start >= MAIN_SECS(sbi)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 900 | break; |
| 901 | } |
| 902 | secno = left_start; |
| 903 | skip_left: |
| 904 | hint = secno; |
| 905 | segno = secno * sbi->segs_per_sec; |
| 906 | zoneno = secno / sbi->secs_per_zone; |
| 907 | |
| 908 | /* give up on finding another zone */ |
| 909 | if (!init) |
| 910 | goto got_it; |
| 911 | if (sbi->secs_per_zone == 1) |
| 912 | goto got_it; |
| 913 | if (zoneno == old_zoneno) |
| 914 | goto got_it; |
| 915 | if (dir == ALLOC_LEFT) { |
| 916 | if (!go_left && zoneno + 1 >= total_zones) |
| 917 | goto got_it; |
| 918 | if (go_left && zoneno == 0) |
| 919 | goto got_it; |
| 920 | } |
| 921 | for (i = 0; i < NR_CURSEG_TYPE; i++) |
| 922 | if (CURSEG_I(sbi, i)->zone == zoneno) |
| 923 | break; |
| 924 | |
| 925 | if (i < NR_CURSEG_TYPE) { |
| 926 | /* zone is in user, try another */ |
| 927 | if (go_left) |
| 928 | hint = zoneno * sbi->secs_per_zone - 1; |
| 929 | else if (zoneno + 1 >= total_zones) |
| 930 | hint = 0; |
| 931 | else |
| 932 | hint = (zoneno + 1) * sbi->secs_per_zone; |
| 933 | init = false; |
| 934 | goto find_other_zone; |
| 935 | } |
| 936 | got_it: |
| 937 | /* set it as dirty segment in free segmap */ |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 938 | f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 939 | __set_inuse(sbi, segno); |
| 940 | *newseg = segno; |
Chao Yu | 1a118cc | 2015-02-11 18:20:38 +0800 | [diff] [blame] | 941 | spin_unlock(&free_i->segmap_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified) |
| 945 | { |
| 946 | struct curseg_info *curseg = CURSEG_I(sbi, type); |
| 947 | struct summary_footer *sum_footer; |
| 948 | |
| 949 | curseg->segno = curseg->next_segno; |
| 950 | curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno); |
| 951 | curseg->next_blkoff = 0; |
| 952 | curseg->next_segno = NULL_SEGNO; |
| 953 | |
| 954 | sum_footer = &(curseg->sum_blk->footer); |
| 955 | memset(sum_footer, 0, sizeof(struct summary_footer)); |
| 956 | if (IS_DATASEG(type)) |
| 957 | SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA); |
| 958 | if (IS_NODESEG(type)) |
| 959 | SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE); |
| 960 | __set_sit_entry_type(sbi, type, curseg->segno, modified); |
| 961 | } |
| 962 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 963 | /* |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 964 | * Allocate a current working segment. |
| 965 | * This function always allocates a free segment in LFS manner. |
| 966 | */ |
| 967 | static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec) |
| 968 | { |
| 969 | struct curseg_info *curseg = CURSEG_I(sbi, type); |
| 970 | unsigned int segno = curseg->segno; |
| 971 | int dir = ALLOC_LEFT; |
| 972 | |
| 973 | write_sum_page(sbi, curseg->sum_blk, |
Haicheng Li | 81fb5e8 | 2013-05-14 18:20:28 +0800 | [diff] [blame] | 974 | GET_SUM_BLOCK(sbi, segno)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 975 | if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA) |
| 976 | dir = ALLOC_RIGHT; |
| 977 | |
| 978 | if (test_opt(sbi, NOHEAP)) |
| 979 | dir = ALLOC_RIGHT; |
| 980 | |
| 981 | get_new_segment(sbi, &segno, new_sec, dir); |
| 982 | curseg->next_segno = segno; |
| 983 | reset_curseg(sbi, type, 1); |
| 984 | curseg->alloc_type = LFS; |
| 985 | } |
| 986 | |
| 987 | static void __next_free_blkoff(struct f2fs_sb_info *sbi, |
| 988 | struct curseg_info *seg, block_t start) |
| 989 | { |
| 990 | struct seg_entry *se = get_seg_entry(sbi, seg->segno); |
Changman Lee | e81c93c | 2013-11-15 13:21:16 +0900 | [diff] [blame] | 991 | int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long); |
Jaegeuk Kim | 60a3b78 | 2015-02-10 16:44:29 -0800 | [diff] [blame] | 992 | unsigned long *target_map = SIT_I(sbi)->tmp_map; |
Changman Lee | e81c93c | 2013-11-15 13:21:16 +0900 | [diff] [blame] | 993 | unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map; |
| 994 | unsigned long *cur_map = (unsigned long *)se->cur_valid_map; |
| 995 | int i, pos; |
| 996 | |
| 997 | for (i = 0; i < entries; i++) |
| 998 | target_map[i] = ckpt_map[i] | cur_map[i]; |
| 999 | |
| 1000 | pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start); |
| 1001 | |
| 1002 | seg->next_blkoff = pos; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1003 | } |
| 1004 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 1005 | /* |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1006 | * If a segment is written by LFS manner, next block offset is just obtained |
| 1007 | * by increasing the current block offset. However, if a segment is written by |
| 1008 | * SSR manner, next block offset obtained by calling __next_free_blkoff |
| 1009 | */ |
| 1010 | static void __refresh_next_blkoff(struct f2fs_sb_info *sbi, |
| 1011 | struct curseg_info *seg) |
| 1012 | { |
| 1013 | if (seg->alloc_type == SSR) |
| 1014 | __next_free_blkoff(sbi, seg, seg->next_blkoff + 1); |
| 1015 | else |
| 1016 | seg->next_blkoff++; |
| 1017 | } |
| 1018 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 1019 | /* |
arter97 | e1c4204 | 2014-08-06 23:22:50 +0900 | [diff] [blame] | 1020 | * This function always allocates a used segment(from dirty seglist) by SSR |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1021 | * manner, so it should recover the existing segment information of valid blocks |
| 1022 | */ |
| 1023 | static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse) |
| 1024 | { |
| 1025 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
| 1026 | struct curseg_info *curseg = CURSEG_I(sbi, type); |
| 1027 | unsigned int new_segno = curseg->next_segno; |
| 1028 | struct f2fs_summary_block *sum_node; |
| 1029 | struct page *sum_page; |
| 1030 | |
| 1031 | write_sum_page(sbi, curseg->sum_blk, |
| 1032 | GET_SUM_BLOCK(sbi, curseg->segno)); |
| 1033 | __set_test_and_inuse(sbi, new_segno); |
| 1034 | |
| 1035 | mutex_lock(&dirty_i->seglist_lock); |
| 1036 | __remove_dirty_segment(sbi, new_segno, PRE); |
| 1037 | __remove_dirty_segment(sbi, new_segno, DIRTY); |
| 1038 | mutex_unlock(&dirty_i->seglist_lock); |
| 1039 | |
| 1040 | reset_curseg(sbi, type, 1); |
| 1041 | curseg->alloc_type = SSR; |
| 1042 | __next_free_blkoff(sbi, curseg, 0); |
| 1043 | |
| 1044 | if (reuse) { |
| 1045 | sum_page = get_sum_page(sbi, new_segno); |
| 1046 | sum_node = (struct f2fs_summary_block *)page_address(sum_page); |
| 1047 | memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE); |
| 1048 | f2fs_put_page(sum_page, 1); |
| 1049 | } |
| 1050 | } |
| 1051 | |
Jaegeuk Kim | 4372752 | 2013-02-04 15:11:17 +0900 | [diff] [blame] | 1052 | static int get_ssr_segment(struct f2fs_sb_info *sbi, int type) |
| 1053 | { |
| 1054 | struct curseg_info *curseg = CURSEG_I(sbi, type); |
| 1055 | const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops; |
| 1056 | |
| 1057 | if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0)) |
| 1058 | return v_ops->get_victim(sbi, |
| 1059 | &(curseg)->next_segno, BG_GC, type, SSR); |
| 1060 | |
| 1061 | /* For data segments, let's do SSR more intensively */ |
| 1062 | for (; type >= CURSEG_HOT_DATA; type--) |
| 1063 | if (v_ops->get_victim(sbi, &(curseg)->next_segno, |
| 1064 | BG_GC, type, SSR)) |
| 1065 | return 1; |
| 1066 | return 0; |
| 1067 | } |
| 1068 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1069 | /* |
| 1070 | * flush out current segment and replace it with new segment |
| 1071 | * This function should be returned with success, otherwise BUG |
| 1072 | */ |
| 1073 | static void allocate_segment_by_default(struct f2fs_sb_info *sbi, |
| 1074 | int type, bool force) |
| 1075 | { |
| 1076 | struct curseg_info *curseg = CURSEG_I(sbi, type); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1077 | |
Gu Zheng | 7b40527 | 2013-08-19 09:41:15 +0800 | [diff] [blame] | 1078 | if (force) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1079 | new_curseg(sbi, type, true); |
Gu Zheng | 7b40527 | 2013-08-19 09:41:15 +0800 | [diff] [blame] | 1080 | else if (type == CURSEG_WARM_NODE) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1081 | new_curseg(sbi, type, false); |
Jaegeuk Kim | 6037468 | 2013-03-31 13:58:51 +0900 | [diff] [blame] | 1082 | else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type)) |
| 1083 | new_curseg(sbi, type, false); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1084 | else if (need_SSR(sbi) && get_ssr_segment(sbi, type)) |
| 1085 | change_curseg(sbi, type, true); |
| 1086 | else |
| 1087 | new_curseg(sbi, type, false); |
Jaegeuk Kim | dcdfff6 | 2013-10-22 20:56:10 +0900 | [diff] [blame] | 1088 | |
| 1089 | stat_inc_seg_type(sbi, curseg); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1090 | } |
| 1091 | |
Jaegeuk Kim | 38aa088 | 2015-01-05 16:02:20 -0800 | [diff] [blame] | 1092 | static void __allocate_new_segments(struct f2fs_sb_info *sbi, int type) |
| 1093 | { |
| 1094 | struct curseg_info *curseg = CURSEG_I(sbi, type); |
| 1095 | unsigned int old_segno; |
| 1096 | |
| 1097 | old_segno = curseg->segno; |
| 1098 | SIT_I(sbi)->s_ops->allocate_segment(sbi, type, true); |
| 1099 | locate_dirty_segment(sbi, old_segno); |
| 1100 | } |
| 1101 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1102 | void allocate_new_segments(struct f2fs_sb_info *sbi) |
| 1103 | { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1104 | int i; |
| 1105 | |
Jaegeuk Kim | 38aa088 | 2015-01-05 16:02:20 -0800 | [diff] [blame] | 1106 | for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) |
| 1107 | __allocate_new_segments(sbi, i); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | static const struct segment_allocation default_salloc_ops = { |
| 1111 | .allocate_segment = allocate_segment_by_default, |
| 1112 | }; |
| 1113 | |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 1114 | int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range) |
| 1115 | { |
Jaegeuk Kim | f7ef9b8 | 2015-02-09 12:02:44 -0800 | [diff] [blame] | 1116 | __u64 start = F2FS_BYTES_TO_BLK(range->start); |
| 1117 | __u64 end = start + F2FS_BYTES_TO_BLK(range->len) - 1; |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 1118 | unsigned int start_segno, end_segno; |
| 1119 | struct cp_control cpc; |
| 1120 | |
Jaegeuk Kim | 836b5a6 | 2015-04-30 22:50:06 -0700 | [diff] [blame] | 1121 | if (start >= MAX_BLKADDR(sbi) || range->len < sbi->blocksize) |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 1122 | return -EINVAL; |
| 1123 | |
Jan Kara | 9bd27ae | 2014-10-21 14:07:33 +0200 | [diff] [blame] | 1124 | cpc.trimmed = 0; |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 1125 | if (end <= MAIN_BLKADDR(sbi)) |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 1126 | goto out; |
| 1127 | |
| 1128 | /* start/end segment number in main_area */ |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 1129 | start_segno = (start <= MAIN_BLKADDR(sbi)) ? 0 : GET_SEGNO(sbi, start); |
| 1130 | end_segno = (end >= MAX_BLKADDR(sbi)) ? MAIN_SEGS(sbi) - 1 : |
| 1131 | GET_SEGNO(sbi, end); |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 1132 | cpc.reason = CP_DISCARD; |
Jaegeuk Kim | 836b5a6 | 2015-04-30 22:50:06 -0700 | [diff] [blame] | 1133 | 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] | 1134 | |
| 1135 | /* do checkpoint to issue discard commands safely */ |
Jaegeuk Kim | bba681c | 2015-01-26 17:41:23 -0800 | [diff] [blame] | 1136 | for (; start_segno <= end_segno; start_segno = cpc.trim_end + 1) { |
| 1137 | cpc.trim_start = start_segno; |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 1138 | |
| 1139 | if (sbi->discard_blks == 0) |
| 1140 | break; |
| 1141 | else if (sbi->discard_blks < BATCHED_TRIM_BLOCKS(sbi)) |
| 1142 | cpc.trim_end = end_segno; |
| 1143 | else |
| 1144 | cpc.trim_end = min_t(unsigned int, |
| 1145 | rounddown(start_segno + |
Jaegeuk Kim | bba681c | 2015-01-26 17:41:23 -0800 | [diff] [blame] | 1146 | BATCHED_TRIM_SEGMENTS(sbi), |
| 1147 | sbi->segs_per_sec) - 1, end_segno); |
| 1148 | |
| 1149 | mutex_lock(&sbi->gc_mutex); |
| 1150 | write_checkpoint(sbi, &cpc); |
| 1151 | mutex_unlock(&sbi->gc_mutex); |
| 1152 | } |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 1153 | out: |
Jaegeuk Kim | f7ef9b8 | 2015-02-09 12:02:44 -0800 | [diff] [blame] | 1154 | range->len = F2FS_BLK_TO_BYTES(cpc.trimmed); |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 1155 | return 0; |
| 1156 | } |
| 1157 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1158 | static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type) |
| 1159 | { |
| 1160 | struct curseg_info *curseg = CURSEG_I(sbi, type); |
| 1161 | if (curseg->next_blkoff < sbi->blocks_per_seg) |
| 1162 | return true; |
| 1163 | return false; |
| 1164 | } |
| 1165 | |
| 1166 | static int __get_segment_type_2(struct page *page, enum page_type p_type) |
| 1167 | { |
| 1168 | if (p_type == DATA) |
| 1169 | return CURSEG_HOT_DATA; |
| 1170 | else |
| 1171 | return CURSEG_HOT_NODE; |
| 1172 | } |
| 1173 | |
| 1174 | static int __get_segment_type_4(struct page *page, enum page_type p_type) |
| 1175 | { |
| 1176 | if (p_type == DATA) { |
| 1177 | struct inode *inode = page->mapping->host; |
| 1178 | |
| 1179 | if (S_ISDIR(inode->i_mode)) |
| 1180 | return CURSEG_HOT_DATA; |
| 1181 | else |
| 1182 | return CURSEG_COLD_DATA; |
| 1183 | } else { |
Jaegeuk Kim | a344b9f | 2014-11-05 20:05:53 -0800 | [diff] [blame] | 1184 | if (IS_DNODE(page) && is_cold_node(page)) |
| 1185 | return CURSEG_WARM_NODE; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1186 | else |
| 1187 | return CURSEG_COLD_NODE; |
| 1188 | } |
| 1189 | } |
| 1190 | |
| 1191 | static int __get_segment_type_6(struct page *page, enum page_type p_type) |
| 1192 | { |
| 1193 | if (p_type == DATA) { |
| 1194 | struct inode *inode = page->mapping->host; |
| 1195 | |
| 1196 | if (S_ISDIR(inode->i_mode)) |
| 1197 | return CURSEG_HOT_DATA; |
Jaegeuk Kim | 354a339 | 2013-06-14 08:52:35 +0900 | [diff] [blame] | 1198 | else if (is_cold_data(page) || file_is_cold(inode)) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1199 | return CURSEG_COLD_DATA; |
| 1200 | else |
| 1201 | return CURSEG_WARM_DATA; |
| 1202 | } else { |
| 1203 | if (IS_DNODE(page)) |
| 1204 | return is_cold_node(page) ? CURSEG_WARM_NODE : |
| 1205 | CURSEG_HOT_NODE; |
| 1206 | else |
| 1207 | return CURSEG_COLD_NODE; |
| 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | static int __get_segment_type(struct page *page, enum page_type p_type) |
| 1212 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 1213 | switch (F2FS_P_SB(page)->active_logs) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1214 | case 2: |
| 1215 | return __get_segment_type_2(page, p_type); |
| 1216 | case 4: |
| 1217 | return __get_segment_type_4(page, p_type); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1218 | } |
Jaegeuk Kim | 12a6714 | 2012-12-21 11:47:05 +0900 | [diff] [blame] | 1219 | /* NR_CURSEG_TYPE(6) logs by default */ |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 1220 | f2fs_bug_on(F2FS_P_SB(page), |
| 1221 | F2FS_P_SB(page)->active_logs != NR_CURSEG_TYPE); |
Jaegeuk Kim | 12a6714 | 2012-12-21 11:47:05 +0900 | [diff] [blame] | 1222 | return __get_segment_type_6(page, p_type); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1223 | } |
| 1224 | |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1225 | void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page, |
| 1226 | block_t old_blkaddr, block_t *new_blkaddr, |
| 1227 | struct f2fs_summary *sum, int type) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1228 | { |
| 1229 | struct sit_info *sit_i = SIT_I(sbi); |
| 1230 | struct curseg_info *curseg; |
Jaegeuk Kim | 38aa088 | 2015-01-05 16:02:20 -0800 | [diff] [blame] | 1231 | bool direct_io = (type == CURSEG_DIRECT_IO); |
| 1232 | |
| 1233 | type = direct_io ? CURSEG_WARM_DATA : type; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1234 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1235 | curseg = CURSEG_I(sbi, type); |
| 1236 | |
| 1237 | mutex_lock(&curseg->curseg_mutex); |
Jaegeuk Kim | 21cb1d9 | 2015-03-11 13:42:48 -0400 | [diff] [blame] | 1238 | mutex_lock(&sit_i->sentry_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1239 | |
Jaegeuk Kim | 38aa088 | 2015-01-05 16:02:20 -0800 | [diff] [blame] | 1240 | /* direct_io'ed data is aligned to the segment for better performance */ |
Jaegeuk Kim | 47e70ca | 2015-08-11 10:17:27 -0700 | [diff] [blame] | 1241 | if (direct_io && curseg->next_blkoff && |
| 1242 | !has_not_enough_free_secs(sbi, 0)) |
Jaegeuk Kim | 38aa088 | 2015-01-05 16:02:20 -0800 | [diff] [blame] | 1243 | __allocate_new_segments(sbi, type); |
| 1244 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1245 | *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1246 | |
| 1247 | /* |
| 1248 | * __add_sum_entry should be resided under the curseg_mutex |
| 1249 | * because, this function updates a summary entry in the |
| 1250 | * current summary block. |
| 1251 | */ |
Haicheng Li | e79efe3 | 2013-06-13 16:59:27 +0800 | [diff] [blame] | 1252 | __add_sum_entry(sbi, type, sum); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1253 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1254 | __refresh_next_blkoff(sbi, curseg); |
Jaegeuk Kim | dcdfff6 | 2013-10-22 20:56:10 +0900 | [diff] [blame] | 1255 | |
| 1256 | stat_inc_block_count(sbi, curseg); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1257 | |
Jaegeuk Kim | 5e44381 | 2014-01-28 12:22:14 +0900 | [diff] [blame] | 1258 | if (!__has_curseg_space(sbi, type)) |
| 1259 | sit_i->s_ops->allocate_segment(sbi, type, false); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1260 | /* |
| 1261 | * SIT information should be updated before segment allocation, |
| 1262 | * since SSR needs latest valid block information. |
| 1263 | */ |
| 1264 | refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr); |
Jaegeuk Kim | 5e44381 | 2014-01-28 12:22:14 +0900 | [diff] [blame] | 1265 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1266 | mutex_unlock(&sit_i->sentry_lock); |
| 1267 | |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1268 | if (page && IS_NODESEG(type)) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1269 | fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg)); |
| 1270 | |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1271 | mutex_unlock(&curseg->curseg_mutex); |
| 1272 | } |
| 1273 | |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 1274 | 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] | 1275 | { |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 1276 | int type = __get_segment_type(fio->page, fio->type); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1277 | |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 1278 | allocate_data_block(fio->sbi, fio->page, fio->blk_addr, |
| 1279 | &fio->blk_addr, sum, type); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1280 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1281 | /* writeout dirty page into bdev */ |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 1282 | f2fs_submit_page_mbio(fio); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1283 | } |
| 1284 | |
Jaegeuk Kim | 577e349 | 2013-01-24 19:56:11 +0900 | [diff] [blame] | 1285 | void write_meta_page(struct f2fs_sb_info *sbi, struct page *page) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1286 | { |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 1287 | struct f2fs_io_info fio = { |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 1288 | .sbi = sbi, |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 1289 | .type = META, |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 1290 | .rw = WRITE_SYNC | REQ_META | REQ_PRIO, |
| 1291 | .blk_addr = page->index, |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 1292 | .page = page, |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 1293 | .encrypted_page = NULL, |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 1294 | }; |
| 1295 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1296 | set_page_writeback(page); |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 1297 | f2fs_submit_page_mbio(&fio); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1298 | } |
| 1299 | |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 1300 | void write_node_page(unsigned int nid, struct f2fs_io_info *fio) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1301 | { |
| 1302 | struct f2fs_summary sum; |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 1303 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1304 | set_summary(&sum, nid, 0, 0); |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 1305 | do_write_page(&sum, fio); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1306 | } |
| 1307 | |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 1308 | 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] | 1309 | { |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 1310 | struct f2fs_sb_info *sbi = fio->sbi; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1311 | struct f2fs_summary sum; |
| 1312 | struct node_info ni; |
| 1313 | |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 1314 | f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1315 | get_node_info(sbi, dn->nid, &ni); |
| 1316 | set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version); |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 1317 | do_write_page(&sum, fio); |
Jaegeuk Kim | e1509cf | 2014-12-30 22:57:55 -0800 | [diff] [blame] | 1318 | dn->data_blkaddr = fio->blk_addr; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1319 | } |
| 1320 | |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 1321 | void rewrite_data_page(struct f2fs_io_info *fio) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1322 | { |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 1323 | stat_inc_inplace_blocks(fio->sbi); |
| 1324 | f2fs_submit_page_mbio(fio); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1325 | } |
| 1326 | |
Chao Yu | 528e345 | 2015-05-28 19:15:35 +0800 | [diff] [blame] | 1327 | static void __f2fs_replace_block(struct f2fs_sb_info *sbi, |
| 1328 | struct f2fs_summary *sum, |
Chao Yu | 19f106b | 2015-05-06 13:08:06 +0800 | [diff] [blame] | 1329 | block_t old_blkaddr, block_t new_blkaddr, |
| 1330 | bool recover_curseg) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1331 | { |
| 1332 | struct sit_info *sit_i = SIT_I(sbi); |
| 1333 | struct curseg_info *curseg; |
| 1334 | unsigned int segno, old_cursegno; |
| 1335 | struct seg_entry *se; |
| 1336 | int type; |
Chao Yu | 19f106b | 2015-05-06 13:08:06 +0800 | [diff] [blame] | 1337 | unsigned short old_blkoff; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1338 | |
| 1339 | segno = GET_SEGNO(sbi, new_blkaddr); |
| 1340 | se = get_seg_entry(sbi, segno); |
| 1341 | type = se->type; |
| 1342 | |
Chao Yu | 19f106b | 2015-05-06 13:08:06 +0800 | [diff] [blame] | 1343 | if (!recover_curseg) { |
| 1344 | /* for recovery flow */ |
| 1345 | if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) { |
| 1346 | if (old_blkaddr == NULL_ADDR) |
| 1347 | type = CURSEG_COLD_DATA; |
| 1348 | else |
| 1349 | type = CURSEG_WARM_DATA; |
| 1350 | } |
| 1351 | } else { |
| 1352 | if (!IS_CURSEG(sbi, segno)) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1353 | type = CURSEG_WARM_DATA; |
| 1354 | } |
Chao Yu | 19f106b | 2015-05-06 13:08:06 +0800 | [diff] [blame] | 1355 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1356 | curseg = CURSEG_I(sbi, type); |
| 1357 | |
| 1358 | mutex_lock(&curseg->curseg_mutex); |
| 1359 | mutex_lock(&sit_i->sentry_lock); |
| 1360 | |
| 1361 | old_cursegno = curseg->segno; |
Chao Yu | 19f106b | 2015-05-06 13:08:06 +0800 | [diff] [blame] | 1362 | old_blkoff = curseg->next_blkoff; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1363 | |
| 1364 | /* change the current segment */ |
| 1365 | if (segno != curseg->segno) { |
| 1366 | curseg->next_segno = segno; |
| 1367 | change_curseg(sbi, type, true); |
| 1368 | } |
| 1369 | |
Jaegeuk Kim | 491c085 | 2014-02-04 13:01:10 +0900 | [diff] [blame] | 1370 | curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr); |
Haicheng Li | e79efe3 | 2013-06-13 16:59:27 +0800 | [diff] [blame] | 1371 | __add_sum_entry(sbi, type, sum); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1372 | |
| 1373 | refresh_sit_entry(sbi, old_blkaddr, new_blkaddr); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1374 | locate_dirty_segment(sbi, old_cursegno); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1375 | |
Chao Yu | 19f106b | 2015-05-06 13:08:06 +0800 | [diff] [blame] | 1376 | if (recover_curseg) { |
| 1377 | if (old_cursegno != curseg->segno) { |
| 1378 | curseg->next_segno = old_cursegno; |
| 1379 | change_curseg(sbi, type, true); |
| 1380 | } |
| 1381 | curseg->next_blkoff = old_blkoff; |
| 1382 | } |
| 1383 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1384 | mutex_unlock(&sit_i->sentry_lock); |
| 1385 | mutex_unlock(&curseg->curseg_mutex); |
| 1386 | } |
| 1387 | |
Chao Yu | 528e345 | 2015-05-28 19:15:35 +0800 | [diff] [blame] | 1388 | void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn, |
| 1389 | block_t old_addr, block_t new_addr, |
| 1390 | unsigned char version, bool recover_curseg) |
| 1391 | { |
| 1392 | struct f2fs_summary sum; |
| 1393 | |
| 1394 | set_summary(&sum, dn->nid, dn->ofs_in_node, version); |
| 1395 | |
| 1396 | __f2fs_replace_block(sbi, &sum, old_addr, new_addr, recover_curseg); |
| 1397 | |
| 1398 | dn->data_blkaddr = new_addr; |
| 1399 | set_data_blkaddr(dn); |
| 1400 | f2fs_update_extent_cache(dn); |
| 1401 | } |
| 1402 | |
Chao Yu | df0f8dc | 2014-03-22 14:57:23 +0800 | [diff] [blame] | 1403 | static inline bool is_merged_page(struct f2fs_sb_info *sbi, |
| 1404 | struct page *page, enum page_type type) |
| 1405 | { |
| 1406 | enum page_type btype = PAGE_TYPE_OF_BIO(type); |
| 1407 | struct f2fs_bio_info *io = &sbi->write_io[btype]; |
Chao Yu | df0f8dc | 2014-03-22 14:57:23 +0800 | [diff] [blame] | 1408 | struct bio_vec *bvec; |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 1409 | struct page *target; |
Chao Yu | df0f8dc | 2014-03-22 14:57:23 +0800 | [diff] [blame] | 1410 | int i; |
| 1411 | |
| 1412 | down_read(&io->io_rwsem); |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 1413 | if (!io->bio) { |
| 1414 | up_read(&io->io_rwsem); |
| 1415 | return false; |
| 1416 | } |
Chao Yu | df0f8dc | 2014-03-22 14:57:23 +0800 | [diff] [blame] | 1417 | |
Jaegeuk Kim | ce23447 | 2014-04-02 09:04:42 +0900 | [diff] [blame] | 1418 | bio_for_each_segment_all(bvec, io->bio, i) { |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 1419 | |
| 1420 | if (bvec->bv_page->mapping) { |
| 1421 | target = bvec->bv_page; |
| 1422 | } else { |
| 1423 | struct f2fs_crypto_ctx *ctx; |
| 1424 | |
| 1425 | /* encrypted page */ |
| 1426 | ctx = (struct f2fs_crypto_ctx *)page_private( |
| 1427 | bvec->bv_page); |
Jaegeuk Kim | ca40b03 | 2015-05-12 13:40:20 -0700 | [diff] [blame] | 1428 | target = ctx->w.control_page; |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 1429 | } |
| 1430 | |
| 1431 | if (page == target) { |
Chao Yu | df0f8dc | 2014-03-22 14:57:23 +0800 | [diff] [blame] | 1432 | up_read(&io->io_rwsem); |
| 1433 | return true; |
| 1434 | } |
| 1435 | } |
| 1436 | |
Chao Yu | df0f8dc | 2014-03-22 14:57:23 +0800 | [diff] [blame] | 1437 | up_read(&io->io_rwsem); |
| 1438 | return false; |
| 1439 | } |
| 1440 | |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 1441 | void f2fs_wait_on_page_writeback(struct page *page, |
Yuan Zhong | 5514f0a | 2014-01-10 07:26:14 +0000 | [diff] [blame] | 1442 | enum page_type type) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 1443 | { |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 1444 | if (PageWriteback(page)) { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 1445 | struct f2fs_sb_info *sbi = F2FS_P_SB(page); |
| 1446 | |
Chao Yu | df0f8dc | 2014-03-22 14:57:23 +0800 | [diff] [blame] | 1447 | if (is_merged_page(sbi, page, type)) |
| 1448 | f2fs_submit_merged_bio(sbi, type, WRITE); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 1449 | wait_on_page_writeback(page); |
| 1450 | } |
| 1451 | } |
| 1452 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1453 | static int read_compacted_summaries(struct f2fs_sb_info *sbi) |
| 1454 | { |
| 1455 | struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); |
| 1456 | struct curseg_info *seg_i; |
| 1457 | unsigned char *kaddr; |
| 1458 | struct page *page; |
| 1459 | block_t start; |
| 1460 | int i, j, offset; |
| 1461 | |
| 1462 | start = start_sum_block(sbi); |
| 1463 | |
| 1464 | page = get_meta_page(sbi, start++); |
| 1465 | kaddr = (unsigned char *)page_address(page); |
| 1466 | |
| 1467 | /* Step 1: restore nat cache */ |
| 1468 | seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA); |
| 1469 | memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE); |
| 1470 | |
| 1471 | /* Step 2: restore sit cache */ |
| 1472 | seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA); |
| 1473 | memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE, |
| 1474 | SUM_JOURNAL_SIZE); |
| 1475 | offset = 2 * SUM_JOURNAL_SIZE; |
| 1476 | |
| 1477 | /* Step 3: restore summary entries */ |
| 1478 | for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) { |
| 1479 | unsigned short blk_off; |
| 1480 | unsigned int segno; |
| 1481 | |
| 1482 | seg_i = CURSEG_I(sbi, i); |
| 1483 | segno = le32_to_cpu(ckpt->cur_data_segno[i]); |
| 1484 | blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]); |
| 1485 | seg_i->next_segno = segno; |
| 1486 | reset_curseg(sbi, i, 0); |
| 1487 | seg_i->alloc_type = ckpt->alloc_type[i]; |
| 1488 | seg_i->next_blkoff = blk_off; |
| 1489 | |
| 1490 | if (seg_i->alloc_type == SSR) |
| 1491 | blk_off = sbi->blocks_per_seg; |
| 1492 | |
| 1493 | for (j = 0; j < blk_off; j++) { |
| 1494 | struct f2fs_summary *s; |
| 1495 | s = (struct f2fs_summary *)(kaddr + offset); |
| 1496 | seg_i->sum_blk->entries[j] = *s; |
| 1497 | offset += SUMMARY_SIZE; |
| 1498 | if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE - |
| 1499 | SUM_FOOTER_SIZE) |
| 1500 | continue; |
| 1501 | |
| 1502 | f2fs_put_page(page, 1); |
| 1503 | page = NULL; |
| 1504 | |
| 1505 | page = get_meta_page(sbi, start++); |
| 1506 | kaddr = (unsigned char *)page_address(page); |
| 1507 | offset = 0; |
| 1508 | } |
| 1509 | } |
| 1510 | f2fs_put_page(page, 1); |
| 1511 | return 0; |
| 1512 | } |
| 1513 | |
| 1514 | static int read_normal_summaries(struct f2fs_sb_info *sbi, int type) |
| 1515 | { |
| 1516 | struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); |
| 1517 | struct f2fs_summary_block *sum; |
| 1518 | struct curseg_info *curseg; |
| 1519 | struct page *new; |
| 1520 | unsigned short blk_off; |
| 1521 | unsigned int segno = 0; |
| 1522 | block_t blk_addr = 0; |
| 1523 | |
| 1524 | /* get segment number and block addr */ |
| 1525 | if (IS_DATASEG(type)) { |
| 1526 | segno = le32_to_cpu(ckpt->cur_data_segno[type]); |
| 1527 | blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type - |
| 1528 | CURSEG_HOT_DATA]); |
Jaegeuk Kim | 119ee91 | 2015-01-29 11:45:33 -0800 | [diff] [blame] | 1529 | if (__exist_node_summaries(sbi)) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1530 | blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type); |
| 1531 | else |
| 1532 | blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type); |
| 1533 | } else { |
| 1534 | segno = le32_to_cpu(ckpt->cur_node_segno[type - |
| 1535 | CURSEG_HOT_NODE]); |
| 1536 | blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type - |
| 1537 | CURSEG_HOT_NODE]); |
Jaegeuk Kim | 119ee91 | 2015-01-29 11:45:33 -0800 | [diff] [blame] | 1538 | if (__exist_node_summaries(sbi)) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1539 | blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE, |
| 1540 | type - CURSEG_HOT_NODE); |
| 1541 | else |
| 1542 | blk_addr = GET_SUM_BLOCK(sbi, segno); |
| 1543 | } |
| 1544 | |
| 1545 | new = get_meta_page(sbi, blk_addr); |
| 1546 | sum = (struct f2fs_summary_block *)page_address(new); |
| 1547 | |
| 1548 | if (IS_NODESEG(type)) { |
Jaegeuk Kim | 119ee91 | 2015-01-29 11:45:33 -0800 | [diff] [blame] | 1549 | if (__exist_node_summaries(sbi)) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1550 | struct f2fs_summary *ns = &sum->entries[0]; |
| 1551 | int i; |
| 1552 | for (i = 0; i < sbi->blocks_per_seg; i++, ns++) { |
| 1553 | ns->version = 0; |
| 1554 | ns->ofs_in_node = 0; |
| 1555 | } |
| 1556 | } else { |
Gu Zheng | d653788 | 2014-03-07 18:43:36 +0800 | [diff] [blame] | 1557 | int err; |
| 1558 | |
| 1559 | err = restore_node_summary(sbi, segno, sum); |
| 1560 | if (err) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1561 | f2fs_put_page(new, 1); |
Gu Zheng | d653788 | 2014-03-07 18:43:36 +0800 | [diff] [blame] | 1562 | return err; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1563 | } |
| 1564 | } |
| 1565 | } |
| 1566 | |
| 1567 | /* set uncompleted segment to curseg */ |
| 1568 | curseg = CURSEG_I(sbi, type); |
| 1569 | mutex_lock(&curseg->curseg_mutex); |
| 1570 | memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE); |
| 1571 | curseg->next_segno = segno; |
| 1572 | reset_curseg(sbi, type, 0); |
| 1573 | curseg->alloc_type = ckpt->alloc_type[type]; |
| 1574 | curseg->next_blkoff = blk_off; |
| 1575 | mutex_unlock(&curseg->curseg_mutex); |
| 1576 | f2fs_put_page(new, 1); |
| 1577 | return 0; |
| 1578 | } |
| 1579 | |
| 1580 | static int restore_curseg_summaries(struct f2fs_sb_info *sbi) |
| 1581 | { |
| 1582 | int type = CURSEG_HOT_DATA; |
Chao Yu | e4fc5fb | 2014-03-17 16:36:24 +0800 | [diff] [blame] | 1583 | int err; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1584 | |
Jaegeuk Kim | 25ca923 | 2012-11-28 16:12:41 +0900 | [diff] [blame] | 1585 | if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) { |
Chao Yu | 3fa06d7 | 2014-12-09 14:21:46 +0800 | [diff] [blame] | 1586 | int npages = npages_for_summary_flush(sbi, true); |
| 1587 | |
| 1588 | if (npages >= 2) |
| 1589 | ra_meta_pages(sbi, start_sum_block(sbi), npages, |
| 1590 | META_CP); |
| 1591 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1592 | /* restore for compacted data summary */ |
| 1593 | if (read_compacted_summaries(sbi)) |
| 1594 | return -EINVAL; |
| 1595 | type = CURSEG_HOT_NODE; |
| 1596 | } |
| 1597 | |
Jaegeuk Kim | 119ee91 | 2015-01-29 11:45:33 -0800 | [diff] [blame] | 1598 | if (__exist_node_summaries(sbi)) |
Chao Yu | 3fa06d7 | 2014-12-09 14:21:46 +0800 | [diff] [blame] | 1599 | ra_meta_pages(sbi, sum_blk_addr(sbi, NR_CURSEG_TYPE, type), |
| 1600 | NR_CURSEG_TYPE - type, META_CP); |
| 1601 | |
Chao Yu | e4fc5fb | 2014-03-17 16:36:24 +0800 | [diff] [blame] | 1602 | for (; type <= CURSEG_COLD_NODE; type++) { |
| 1603 | err = read_normal_summaries(sbi, type); |
| 1604 | if (err) |
| 1605 | return err; |
| 1606 | } |
| 1607 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1608 | return 0; |
| 1609 | } |
| 1610 | |
| 1611 | static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr) |
| 1612 | { |
| 1613 | struct page *page; |
| 1614 | unsigned char *kaddr; |
| 1615 | struct f2fs_summary *summary; |
| 1616 | struct curseg_info *seg_i; |
| 1617 | int written_size = 0; |
| 1618 | int i, j; |
| 1619 | |
| 1620 | page = grab_meta_page(sbi, blkaddr++); |
| 1621 | kaddr = (unsigned char *)page_address(page); |
| 1622 | |
| 1623 | /* Step 1: write nat cache */ |
| 1624 | seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA); |
| 1625 | memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE); |
| 1626 | written_size += SUM_JOURNAL_SIZE; |
| 1627 | |
| 1628 | /* Step 2: write sit cache */ |
| 1629 | seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA); |
| 1630 | memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits, |
| 1631 | SUM_JOURNAL_SIZE); |
| 1632 | written_size += SUM_JOURNAL_SIZE; |
| 1633 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1634 | /* Step 3: write summary entries */ |
| 1635 | for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) { |
| 1636 | unsigned short blkoff; |
| 1637 | seg_i = CURSEG_I(sbi, i); |
| 1638 | if (sbi->ckpt->alloc_type[i] == SSR) |
| 1639 | blkoff = sbi->blocks_per_seg; |
| 1640 | else |
| 1641 | blkoff = curseg_blkoff(sbi, i); |
| 1642 | |
| 1643 | for (j = 0; j < blkoff; j++) { |
| 1644 | if (!page) { |
| 1645 | page = grab_meta_page(sbi, blkaddr++); |
| 1646 | kaddr = (unsigned char *)page_address(page); |
| 1647 | written_size = 0; |
| 1648 | } |
| 1649 | summary = (struct f2fs_summary *)(kaddr + written_size); |
| 1650 | *summary = seg_i->sum_blk->entries[j]; |
| 1651 | written_size += SUMMARY_SIZE; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1652 | |
| 1653 | if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE - |
| 1654 | SUM_FOOTER_SIZE) |
| 1655 | continue; |
| 1656 | |
Chao Yu | e8d61a7 | 2013-10-24 15:08:28 +0800 | [diff] [blame] | 1657 | set_page_dirty(page); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1658 | f2fs_put_page(page, 1); |
| 1659 | page = NULL; |
| 1660 | } |
| 1661 | } |
Chao Yu | e8d61a7 | 2013-10-24 15:08:28 +0800 | [diff] [blame] | 1662 | if (page) { |
| 1663 | set_page_dirty(page); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1664 | f2fs_put_page(page, 1); |
Chao Yu | e8d61a7 | 2013-10-24 15:08:28 +0800 | [diff] [blame] | 1665 | } |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1666 | } |
| 1667 | |
| 1668 | static void write_normal_summaries(struct f2fs_sb_info *sbi, |
| 1669 | block_t blkaddr, int type) |
| 1670 | { |
| 1671 | int i, end; |
| 1672 | if (IS_DATASEG(type)) |
| 1673 | end = type + NR_CURSEG_DATA_TYPE; |
| 1674 | else |
| 1675 | end = type + NR_CURSEG_NODE_TYPE; |
| 1676 | |
| 1677 | for (i = type; i < end; i++) { |
| 1678 | struct curseg_info *sum = CURSEG_I(sbi, i); |
| 1679 | mutex_lock(&sum->curseg_mutex); |
| 1680 | write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type)); |
| 1681 | mutex_unlock(&sum->curseg_mutex); |
| 1682 | } |
| 1683 | } |
| 1684 | |
| 1685 | void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk) |
| 1686 | { |
Jaegeuk Kim | 25ca923 | 2012-11-28 16:12:41 +0900 | [diff] [blame] | 1687 | if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1688 | write_compacted_summaries(sbi, start_blk); |
| 1689 | else |
| 1690 | write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA); |
| 1691 | } |
| 1692 | |
| 1693 | void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk) |
| 1694 | { |
Jaegeuk Kim | 119ee91 | 2015-01-29 11:45:33 -0800 | [diff] [blame] | 1695 | write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1696 | } |
| 1697 | |
| 1698 | int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type, |
| 1699 | unsigned int val, int alloc) |
| 1700 | { |
| 1701 | int i; |
| 1702 | |
| 1703 | if (type == NAT_JOURNAL) { |
| 1704 | for (i = 0; i < nats_in_cursum(sum); i++) { |
| 1705 | if (le32_to_cpu(nid_in_journal(sum, i)) == val) |
| 1706 | return i; |
| 1707 | } |
| 1708 | if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES) |
| 1709 | return update_nats_in_cursum(sum, 1); |
| 1710 | } else if (type == SIT_JOURNAL) { |
| 1711 | for (i = 0; i < sits_in_cursum(sum); i++) |
| 1712 | if (le32_to_cpu(segno_in_journal(sum, i)) == val) |
| 1713 | return i; |
| 1714 | if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES) |
| 1715 | return update_sits_in_cursum(sum, 1); |
| 1716 | } |
| 1717 | return -1; |
| 1718 | } |
| 1719 | |
| 1720 | static struct page *get_current_sit_page(struct f2fs_sb_info *sbi, |
| 1721 | unsigned int segno) |
| 1722 | { |
Gu Zheng | 2cc2218 | 2014-10-20 17:45:49 +0800 | [diff] [blame] | 1723 | return get_meta_page(sbi, current_sit_addr(sbi, segno)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1724 | } |
| 1725 | |
| 1726 | static struct page *get_next_sit_page(struct f2fs_sb_info *sbi, |
| 1727 | unsigned int start) |
| 1728 | { |
| 1729 | struct sit_info *sit_i = SIT_I(sbi); |
| 1730 | struct page *src_page, *dst_page; |
| 1731 | pgoff_t src_off, dst_off; |
| 1732 | void *src_addr, *dst_addr; |
| 1733 | |
| 1734 | src_off = current_sit_addr(sbi, start); |
| 1735 | dst_off = next_sit_addr(sbi, src_off); |
| 1736 | |
| 1737 | /* get current sit block page without lock */ |
| 1738 | src_page = get_meta_page(sbi, src_off); |
| 1739 | dst_page = grab_meta_page(sbi, dst_off); |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 1740 | f2fs_bug_on(sbi, PageDirty(src_page)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1741 | |
| 1742 | src_addr = page_address(src_page); |
| 1743 | dst_addr = page_address(dst_page); |
| 1744 | memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE); |
| 1745 | |
| 1746 | set_page_dirty(dst_page); |
| 1747 | f2fs_put_page(src_page, 1); |
| 1748 | |
| 1749 | set_to_next_sit(sit_i, start); |
| 1750 | |
| 1751 | return dst_page; |
| 1752 | } |
| 1753 | |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 1754 | static struct sit_entry_set *grab_sit_entry_set(void) |
| 1755 | { |
| 1756 | struct sit_entry_set *ses = |
Jaegeuk Kim | 80c5450 | 2015-08-20 08:51:56 -0700 | [diff] [blame] | 1757 | f2fs_kmem_cache_alloc(sit_entry_set_slab, GFP_NOFS); |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 1758 | |
| 1759 | ses->entry_cnt = 0; |
| 1760 | INIT_LIST_HEAD(&ses->set_list); |
| 1761 | return ses; |
| 1762 | } |
| 1763 | |
| 1764 | static void release_sit_entry_set(struct sit_entry_set *ses) |
| 1765 | { |
| 1766 | list_del(&ses->set_list); |
| 1767 | kmem_cache_free(sit_entry_set_slab, ses); |
| 1768 | } |
| 1769 | |
| 1770 | static void adjust_sit_entry_set(struct sit_entry_set *ses, |
| 1771 | struct list_head *head) |
| 1772 | { |
| 1773 | struct sit_entry_set *next = ses; |
| 1774 | |
| 1775 | if (list_is_last(&ses->set_list, head)) |
| 1776 | return; |
| 1777 | |
| 1778 | list_for_each_entry_continue(next, head, set_list) |
| 1779 | if (ses->entry_cnt <= next->entry_cnt) |
| 1780 | break; |
| 1781 | |
| 1782 | list_move_tail(&ses->set_list, &next->set_list); |
| 1783 | } |
| 1784 | |
| 1785 | static void add_sit_entry(unsigned int segno, struct list_head *head) |
| 1786 | { |
| 1787 | struct sit_entry_set *ses; |
| 1788 | unsigned int start_segno = START_SEGNO(segno); |
| 1789 | |
| 1790 | list_for_each_entry(ses, head, set_list) { |
| 1791 | if (ses->start_segno == start_segno) { |
| 1792 | ses->entry_cnt++; |
| 1793 | adjust_sit_entry_set(ses, head); |
| 1794 | return; |
| 1795 | } |
| 1796 | } |
| 1797 | |
| 1798 | ses = grab_sit_entry_set(); |
| 1799 | |
| 1800 | ses->start_segno = start_segno; |
| 1801 | ses->entry_cnt++; |
| 1802 | list_add(&ses->set_list, head); |
| 1803 | } |
| 1804 | |
| 1805 | static void add_sits_in_set(struct f2fs_sb_info *sbi) |
| 1806 | { |
| 1807 | struct f2fs_sm_info *sm_info = SM_I(sbi); |
| 1808 | struct list_head *set_list = &sm_info->sit_entry_set; |
| 1809 | unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap; |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 1810 | unsigned int segno; |
| 1811 | |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 1812 | for_each_set_bit(segno, bitmap, MAIN_SEGS(sbi)) |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 1813 | add_sit_entry(segno, set_list); |
| 1814 | } |
| 1815 | |
| 1816 | static void remove_sits_in_journal(struct f2fs_sb_info *sbi) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1817 | { |
| 1818 | struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA); |
| 1819 | struct f2fs_summary_block *sum = curseg->sum_blk; |
| 1820 | int i; |
| 1821 | |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 1822 | for (i = sits_in_cursum(sum) - 1; i >= 0; i--) { |
| 1823 | unsigned int segno; |
| 1824 | bool dirtied; |
| 1825 | |
| 1826 | segno = le32_to_cpu(segno_in_journal(sum, i)); |
| 1827 | dirtied = __mark_sit_entry_dirty(sbi, segno); |
| 1828 | |
| 1829 | if (!dirtied) |
| 1830 | add_sit_entry(segno, &SM_I(sbi)->sit_entry_set); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1831 | } |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 1832 | update_sits_in_cursum(sum, -sits_in_cursum(sum)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1833 | } |
| 1834 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 1835 | /* |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1836 | * CP calls this function, which flushes SIT entries including sit_journal, |
| 1837 | * and moves prefree segs to free segs. |
| 1838 | */ |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 1839 | 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] | 1840 | { |
| 1841 | struct sit_info *sit_i = SIT_I(sbi); |
| 1842 | unsigned long *bitmap = sit_i->dirty_sentries_bitmap; |
| 1843 | struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA); |
| 1844 | struct f2fs_summary_block *sum = curseg->sum_blk; |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 1845 | struct sit_entry_set *ses, *tmp; |
| 1846 | struct list_head *head = &SM_I(sbi)->sit_entry_set; |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 1847 | bool to_journal = true; |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 1848 | struct seg_entry *se; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1849 | |
| 1850 | mutex_lock(&curseg->curseg_mutex); |
| 1851 | mutex_lock(&sit_i->sentry_lock); |
| 1852 | |
Wanpeng Li | 2b11a74 | 2015-02-27 16:52:50 +0800 | [diff] [blame] | 1853 | if (!sit_i->dirty_sentries) |
| 1854 | goto out; |
| 1855 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1856 | /* |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 1857 | * add and account sit entries of dirty bitmap in sit entry |
| 1858 | * set temporarily |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1859 | */ |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 1860 | add_sits_in_set(sbi); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1861 | |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 1862 | /* |
| 1863 | * if there are no enough space in journal to store dirty sit |
| 1864 | * entries, remove all entries from journal and add and account |
| 1865 | * them in sit entry set. |
| 1866 | */ |
| 1867 | if (!__has_cursum_space(sum, sit_i->dirty_sentries, SIT_JOURNAL)) |
| 1868 | remove_sits_in_journal(sbi); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1869 | |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 1870 | /* |
| 1871 | * there are two steps to flush sit entries: |
| 1872 | * #1, flush sit entries to journal in current cold data summary block. |
| 1873 | * #2, flush sit entries to sit page. |
| 1874 | */ |
| 1875 | list_for_each_entry_safe(ses, tmp, head, set_list) { |
Jaegeuk Kim | 4a257ed | 2014-10-16 11:43:30 -0700 | [diff] [blame] | 1876 | struct page *page = NULL; |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 1877 | struct f2fs_sit_block *raw_sit = NULL; |
| 1878 | unsigned int start_segno = ses->start_segno; |
| 1879 | unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK, |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 1880 | (unsigned long)MAIN_SEGS(sbi)); |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 1881 | unsigned int segno = start_segno; |
Jaegeuk Kim | b295555 | 2013-11-12 14:49:56 +0900 | [diff] [blame] | 1882 | |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 1883 | if (to_journal && |
| 1884 | !__has_cursum_space(sum, ses->entry_cnt, SIT_JOURNAL)) |
| 1885 | to_journal = false; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1886 | |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 1887 | if (!to_journal) { |
| 1888 | page = get_next_sit_page(sbi, start_segno); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1889 | raw_sit = page_address(page); |
| 1890 | } |
| 1891 | |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 1892 | /* flush dirty sit entries in region of current sit set */ |
| 1893 | for_each_set_bit_from(segno, bitmap, end) { |
| 1894 | int offset, sit_offset; |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 1895 | |
| 1896 | se = get_seg_entry(sbi, segno); |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 1897 | |
| 1898 | /* add discard candidates */ |
Jaegeuk Kim | d7bc248 | 2014-12-12 13:53:41 -0800 | [diff] [blame] | 1899 | if (cpc->reason != CP_DISCARD) { |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 1900 | cpc->trim_start = segno; |
| 1901 | add_discard_addrs(sbi, cpc); |
| 1902 | } |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 1903 | |
| 1904 | if (to_journal) { |
| 1905 | offset = lookup_journal_in_cursum(sum, |
| 1906 | SIT_JOURNAL, segno, 1); |
| 1907 | f2fs_bug_on(sbi, offset < 0); |
| 1908 | segno_in_journal(sum, offset) = |
| 1909 | cpu_to_le32(segno); |
| 1910 | seg_info_to_raw_sit(se, |
| 1911 | &sit_in_journal(sum, offset)); |
| 1912 | } else { |
| 1913 | sit_offset = SIT_ENTRY_OFFSET(sit_i, segno); |
| 1914 | seg_info_to_raw_sit(se, |
| 1915 | &raw_sit->entries[sit_offset]); |
| 1916 | } |
| 1917 | |
| 1918 | __clear_bit(segno, bitmap); |
| 1919 | sit_i->dirty_sentries--; |
| 1920 | ses->entry_cnt--; |
| 1921 | } |
| 1922 | |
| 1923 | if (!to_journal) |
| 1924 | f2fs_put_page(page, 1); |
| 1925 | |
| 1926 | f2fs_bug_on(sbi, ses->entry_cnt); |
| 1927 | release_sit_entry_set(ses); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1928 | } |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 1929 | |
| 1930 | f2fs_bug_on(sbi, !list_empty(head)); |
| 1931 | f2fs_bug_on(sbi, sit_i->dirty_sentries); |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 1932 | out: |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 1933 | if (cpc->reason == CP_DISCARD) { |
| 1934 | for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++) |
| 1935 | add_discard_addrs(sbi, cpc); |
| 1936 | } |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1937 | mutex_unlock(&sit_i->sentry_lock); |
| 1938 | mutex_unlock(&curseg->curseg_mutex); |
| 1939 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1940 | set_prefree_as_free_segments(sbi); |
| 1941 | } |
| 1942 | |
| 1943 | static int build_sit_info(struct f2fs_sb_info *sbi) |
| 1944 | { |
| 1945 | struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi); |
| 1946 | struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); |
| 1947 | struct sit_info *sit_i; |
| 1948 | unsigned int sit_segs, start; |
| 1949 | char *src_bitmap, *dst_bitmap; |
| 1950 | unsigned int bitmap_size; |
| 1951 | |
| 1952 | /* allocate memory for SIT information */ |
| 1953 | sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL); |
| 1954 | if (!sit_i) |
| 1955 | return -ENOMEM; |
| 1956 | |
| 1957 | SM_I(sbi)->sit_info = sit_i; |
| 1958 | |
Jaegeuk Kim | 39307a8 | 2015-09-22 13:50:47 -0700 | [diff] [blame] | 1959 | sit_i->sentries = f2fs_kvzalloc(MAIN_SEGS(sbi) * |
| 1960 | sizeof(struct seg_entry), GFP_KERNEL); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1961 | if (!sit_i->sentries) |
| 1962 | return -ENOMEM; |
| 1963 | |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 1964 | bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi)); |
Jaegeuk Kim | 39307a8 | 2015-09-22 13:50:47 -0700 | [diff] [blame] | 1965 | sit_i->dirty_sentries_bitmap = f2fs_kvzalloc(bitmap_size, GFP_KERNEL); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1966 | if (!sit_i->dirty_sentries_bitmap) |
| 1967 | return -ENOMEM; |
| 1968 | |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 1969 | for (start = 0; start < MAIN_SEGS(sbi); start++) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1970 | sit_i->sentries[start].cur_valid_map |
| 1971 | = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL); |
| 1972 | sit_i->sentries[start].ckpt_valid_map |
| 1973 | = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL); |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 1974 | sit_i->sentries[start].discard_map |
| 1975 | = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL); |
| 1976 | if (!sit_i->sentries[start].cur_valid_map || |
| 1977 | !sit_i->sentries[start].ckpt_valid_map || |
| 1978 | !sit_i->sentries[start].discard_map) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1979 | return -ENOMEM; |
| 1980 | } |
| 1981 | |
Jaegeuk Kim | 60a3b78 | 2015-02-10 16:44:29 -0800 | [diff] [blame] | 1982 | sit_i->tmp_map = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL); |
| 1983 | if (!sit_i->tmp_map) |
| 1984 | return -ENOMEM; |
| 1985 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1986 | if (sbi->segs_per_sec > 1) { |
Jaegeuk Kim | 39307a8 | 2015-09-22 13:50:47 -0700 | [diff] [blame] | 1987 | sit_i->sec_entries = f2fs_kvzalloc(MAIN_SECS(sbi) * |
| 1988 | sizeof(struct sec_entry), GFP_KERNEL); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 1989 | if (!sit_i->sec_entries) |
| 1990 | return -ENOMEM; |
| 1991 | } |
| 1992 | |
| 1993 | /* get information related with SIT */ |
| 1994 | sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1; |
| 1995 | |
| 1996 | /* setup SIT bitmap from ckeckpoint pack */ |
| 1997 | bitmap_size = __bitmap_size(sbi, SIT_BITMAP); |
| 1998 | src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP); |
| 1999 | |
Alexandru Gheorghiu | 79b5793 | 2013-03-28 02:24:53 +0200 | [diff] [blame] | 2000 | dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2001 | if (!dst_bitmap) |
| 2002 | return -ENOMEM; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2003 | |
| 2004 | /* init SIT information */ |
| 2005 | sit_i->s_ops = &default_salloc_ops; |
| 2006 | |
| 2007 | sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr); |
| 2008 | sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg; |
| 2009 | sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count); |
| 2010 | sit_i->sit_bitmap = dst_bitmap; |
| 2011 | sit_i->bitmap_size = bitmap_size; |
| 2012 | sit_i->dirty_sentries = 0; |
| 2013 | sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK; |
| 2014 | sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time); |
| 2015 | sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec; |
| 2016 | mutex_init(&sit_i->sentry_lock); |
| 2017 | return 0; |
| 2018 | } |
| 2019 | |
| 2020 | static int build_free_segmap(struct f2fs_sb_info *sbi) |
| 2021 | { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2022 | struct free_segmap_info *free_i; |
| 2023 | unsigned int bitmap_size, sec_bitmap_size; |
| 2024 | |
| 2025 | /* allocate memory for free segmap information */ |
| 2026 | free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL); |
| 2027 | if (!free_i) |
| 2028 | return -ENOMEM; |
| 2029 | |
| 2030 | SM_I(sbi)->free_info = free_i; |
| 2031 | |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 2032 | bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi)); |
Jaegeuk Kim | 39307a8 | 2015-09-22 13:50:47 -0700 | [diff] [blame] | 2033 | free_i->free_segmap = f2fs_kvmalloc(bitmap_size, GFP_KERNEL); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2034 | if (!free_i->free_segmap) |
| 2035 | return -ENOMEM; |
| 2036 | |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 2037 | sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi)); |
Jaegeuk Kim | 39307a8 | 2015-09-22 13:50:47 -0700 | [diff] [blame] | 2038 | free_i->free_secmap = f2fs_kvmalloc(sec_bitmap_size, GFP_KERNEL); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2039 | if (!free_i->free_secmap) |
| 2040 | return -ENOMEM; |
| 2041 | |
| 2042 | /* set all segments as dirty temporarily */ |
| 2043 | memset(free_i->free_segmap, 0xff, bitmap_size); |
| 2044 | memset(free_i->free_secmap, 0xff, sec_bitmap_size); |
| 2045 | |
| 2046 | /* init free segmap information */ |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 2047 | free_i->start_segno = GET_SEGNO_FROM_SEG0(sbi, MAIN_BLKADDR(sbi)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2048 | free_i->free_segments = 0; |
| 2049 | free_i->free_sections = 0; |
Chao Yu | 1a118cc | 2015-02-11 18:20:38 +0800 | [diff] [blame] | 2050 | spin_lock_init(&free_i->segmap_lock); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2051 | return 0; |
| 2052 | } |
| 2053 | |
| 2054 | static int build_curseg(struct f2fs_sb_info *sbi) |
| 2055 | { |
Namjae Jeon | 1042d60 | 2012-12-01 10:56:13 +0900 | [diff] [blame] | 2056 | struct curseg_info *array; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2057 | int i; |
| 2058 | |
Fabian Frederick | b434bab | 2014-06-23 18:39:15 +0200 | [diff] [blame] | 2059 | array = kcalloc(NR_CURSEG_TYPE, sizeof(*array), GFP_KERNEL); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2060 | if (!array) |
| 2061 | return -ENOMEM; |
| 2062 | |
| 2063 | SM_I(sbi)->curseg_array = array; |
| 2064 | |
| 2065 | for (i = 0; i < NR_CURSEG_TYPE; i++) { |
| 2066 | mutex_init(&array[i].curseg_mutex); |
| 2067 | array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL); |
| 2068 | if (!array[i].sum_blk) |
| 2069 | return -ENOMEM; |
| 2070 | array[i].segno = NULL_SEGNO; |
| 2071 | array[i].next_blkoff = 0; |
| 2072 | } |
| 2073 | return restore_curseg_summaries(sbi); |
| 2074 | } |
| 2075 | |
| 2076 | static void build_sit_entries(struct f2fs_sb_info *sbi) |
| 2077 | { |
| 2078 | struct sit_info *sit_i = SIT_I(sbi); |
| 2079 | struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA); |
| 2080 | struct f2fs_summary_block *sum = curseg->sum_blk; |
Chao Yu | 74de593 | 2013-11-22 09:09:59 +0800 | [diff] [blame] | 2081 | int sit_blk_cnt = SIT_BLK_CNT(sbi); |
| 2082 | unsigned int i, start, end; |
| 2083 | unsigned int readed, start_blk = 0; |
Jaegeuk Kim | 90a893c | 2014-09-22 16:21:07 -0700 | [diff] [blame] | 2084 | int nrpages = MAX_BIO_BLOCKS(sbi); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2085 | |
Chao Yu | 74de593 | 2013-11-22 09:09:59 +0800 | [diff] [blame] | 2086 | do { |
Chao Yu | 662befd | 2014-02-07 16:11:53 +0800 | [diff] [blame] | 2087 | readed = ra_meta_pages(sbi, start_blk, nrpages, META_SIT); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2088 | |
Chao Yu | 74de593 | 2013-11-22 09:09:59 +0800 | [diff] [blame] | 2089 | start = start_blk * sit_i->sents_per_block; |
| 2090 | end = (start_blk + readed) * sit_i->sents_per_block; |
| 2091 | |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 2092 | for (; start < end && start < MAIN_SEGS(sbi); start++) { |
Chao Yu | 74de593 | 2013-11-22 09:09:59 +0800 | [diff] [blame] | 2093 | struct seg_entry *se = &sit_i->sentries[start]; |
| 2094 | struct f2fs_sit_block *sit_blk; |
| 2095 | struct f2fs_sit_entry sit; |
| 2096 | struct page *page; |
| 2097 | |
| 2098 | mutex_lock(&curseg->curseg_mutex); |
| 2099 | for (i = 0; i < sits_in_cursum(sum); i++) { |
Chris Fries | 6c311ec | 2014-01-17 14:44:39 -0600 | [diff] [blame] | 2100 | if (le32_to_cpu(segno_in_journal(sum, i)) |
| 2101 | == start) { |
Chao Yu | 74de593 | 2013-11-22 09:09:59 +0800 | [diff] [blame] | 2102 | sit = sit_in_journal(sum, i); |
| 2103 | mutex_unlock(&curseg->curseg_mutex); |
| 2104 | goto got_it; |
| 2105 | } |
| 2106 | } |
| 2107 | mutex_unlock(&curseg->curseg_mutex); |
| 2108 | |
| 2109 | page = get_current_sit_page(sbi, start); |
| 2110 | sit_blk = (struct f2fs_sit_block *)page_address(page); |
| 2111 | sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)]; |
| 2112 | f2fs_put_page(page, 1); |
| 2113 | got_it: |
| 2114 | check_block_count(sbi, start, &sit); |
| 2115 | seg_info_from_raw_sit(se, &sit); |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 2116 | |
| 2117 | /* build discard map only one time */ |
| 2118 | memcpy(se->discard_map, se->cur_valid_map, SIT_VBLOCK_MAP_SIZE); |
| 2119 | sbi->discard_blks += sbi->blocks_per_seg - se->valid_blocks; |
| 2120 | |
Chao Yu | 74de593 | 2013-11-22 09:09:59 +0800 | [diff] [blame] | 2121 | if (sbi->segs_per_sec > 1) { |
| 2122 | struct sec_entry *e = get_sec_entry(sbi, start); |
| 2123 | e->valid_blocks += se->valid_blocks; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2124 | } |
| 2125 | } |
Chao Yu | 74de593 | 2013-11-22 09:09:59 +0800 | [diff] [blame] | 2126 | start_blk += readed; |
| 2127 | } while (start_blk < sit_blk_cnt); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2128 | } |
| 2129 | |
| 2130 | static void init_free_segmap(struct f2fs_sb_info *sbi) |
| 2131 | { |
| 2132 | unsigned int start; |
| 2133 | int type; |
| 2134 | |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 2135 | for (start = 0; start < MAIN_SEGS(sbi); start++) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2136 | struct seg_entry *sentry = get_seg_entry(sbi, start); |
| 2137 | if (!sentry->valid_blocks) |
| 2138 | __set_free(sbi, start); |
| 2139 | } |
| 2140 | |
| 2141 | /* set use the current segments */ |
| 2142 | for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) { |
| 2143 | struct curseg_info *curseg_t = CURSEG_I(sbi, type); |
| 2144 | __set_test_and_inuse(sbi, curseg_t->segno); |
| 2145 | } |
| 2146 | } |
| 2147 | |
| 2148 | static void init_dirty_segmap(struct f2fs_sb_info *sbi) |
| 2149 | { |
| 2150 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
| 2151 | struct free_segmap_info *free_i = FREE_I(sbi); |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 2152 | unsigned int segno = 0, offset = 0; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2153 | unsigned short valid_blocks; |
| 2154 | |
Namjae Jeon | 8736fbf | 2013-06-16 09:49:11 +0900 | [diff] [blame] | 2155 | while (1) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2156 | /* find dirty segment based on free segmap */ |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 2157 | segno = find_next_inuse(free_i, MAIN_SEGS(sbi), offset); |
| 2158 | if (segno >= MAIN_SEGS(sbi)) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2159 | break; |
| 2160 | offset = segno + 1; |
| 2161 | valid_blocks = get_valid_blocks(sbi, segno, 0); |
Jaegeuk Kim | ec325b5 | 2014-09-02 16:24:11 -0700 | [diff] [blame] | 2162 | if (valid_blocks == sbi->blocks_per_seg || !valid_blocks) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2163 | continue; |
Jaegeuk Kim | ec325b5 | 2014-09-02 16:24:11 -0700 | [diff] [blame] | 2164 | if (valid_blocks > sbi->blocks_per_seg) { |
| 2165 | f2fs_bug_on(sbi, 1); |
| 2166 | continue; |
| 2167 | } |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2168 | mutex_lock(&dirty_i->seglist_lock); |
| 2169 | __locate_dirty_segment(sbi, segno, DIRTY); |
| 2170 | mutex_unlock(&dirty_i->seglist_lock); |
| 2171 | } |
| 2172 | } |
| 2173 | |
Jaegeuk Kim | 5ec4e49 | 2013-03-31 13:26:03 +0900 | [diff] [blame] | 2174 | static int init_victim_secmap(struct f2fs_sb_info *sbi) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2175 | { |
| 2176 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 2177 | unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2178 | |
Jaegeuk Kim | 39307a8 | 2015-09-22 13:50:47 -0700 | [diff] [blame] | 2179 | dirty_i->victim_secmap = f2fs_kvzalloc(bitmap_size, GFP_KERNEL); |
Jaegeuk Kim | 5ec4e49 | 2013-03-31 13:26:03 +0900 | [diff] [blame] | 2180 | if (!dirty_i->victim_secmap) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2181 | return -ENOMEM; |
| 2182 | return 0; |
| 2183 | } |
| 2184 | |
| 2185 | static int build_dirty_segmap(struct f2fs_sb_info *sbi) |
| 2186 | { |
| 2187 | struct dirty_seglist_info *dirty_i; |
| 2188 | unsigned int bitmap_size, i; |
| 2189 | |
| 2190 | /* allocate memory for dirty segments list information */ |
| 2191 | dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL); |
| 2192 | if (!dirty_i) |
| 2193 | return -ENOMEM; |
| 2194 | |
| 2195 | SM_I(sbi)->dirty_info = dirty_i; |
| 2196 | mutex_init(&dirty_i->seglist_lock); |
| 2197 | |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 2198 | bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi)); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2199 | |
| 2200 | for (i = 0; i < NR_DIRTY_TYPE; i++) { |
Jaegeuk Kim | 39307a8 | 2015-09-22 13:50:47 -0700 | [diff] [blame] | 2201 | dirty_i->dirty_segmap[i] = f2fs_kvzalloc(bitmap_size, GFP_KERNEL); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2202 | if (!dirty_i->dirty_segmap[i]) |
| 2203 | return -ENOMEM; |
| 2204 | } |
| 2205 | |
| 2206 | init_dirty_segmap(sbi); |
Jaegeuk Kim | 5ec4e49 | 2013-03-31 13:26:03 +0900 | [diff] [blame] | 2207 | return init_victim_secmap(sbi); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2208 | } |
| 2209 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 2210 | /* |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2211 | * Update min, max modified time for cost-benefit GC algorithm |
| 2212 | */ |
| 2213 | static void init_min_max_mtime(struct f2fs_sb_info *sbi) |
| 2214 | { |
| 2215 | struct sit_info *sit_i = SIT_I(sbi); |
| 2216 | unsigned int segno; |
| 2217 | |
| 2218 | mutex_lock(&sit_i->sentry_lock); |
| 2219 | |
| 2220 | sit_i->min_mtime = LLONG_MAX; |
| 2221 | |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 2222 | for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2223 | unsigned int i; |
| 2224 | unsigned long long mtime = 0; |
| 2225 | |
| 2226 | for (i = 0; i < sbi->segs_per_sec; i++) |
| 2227 | mtime += get_seg_entry(sbi, segno + i)->mtime; |
| 2228 | |
| 2229 | mtime = div_u64(mtime, sbi->segs_per_sec); |
| 2230 | |
| 2231 | if (sit_i->min_mtime > mtime) |
| 2232 | sit_i->min_mtime = mtime; |
| 2233 | } |
| 2234 | sit_i->max_mtime = get_mtime(sbi); |
| 2235 | mutex_unlock(&sit_i->sentry_lock); |
| 2236 | } |
| 2237 | |
| 2238 | int build_segment_manager(struct f2fs_sb_info *sbi) |
| 2239 | { |
| 2240 | struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi); |
| 2241 | struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); |
Namjae Jeon | 1042d60 | 2012-12-01 10:56:13 +0900 | [diff] [blame] | 2242 | struct f2fs_sm_info *sm_info; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2243 | int err; |
| 2244 | |
| 2245 | sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL); |
| 2246 | if (!sm_info) |
| 2247 | return -ENOMEM; |
| 2248 | |
| 2249 | /* init sm info */ |
| 2250 | sbi->sm_info = sm_info; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2251 | sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr); |
| 2252 | sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr); |
| 2253 | sm_info->segment_count = le32_to_cpu(raw_super->segment_count); |
| 2254 | sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count); |
| 2255 | sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count); |
| 2256 | sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main); |
| 2257 | sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr); |
Jaegeuk Kim | 58c4103 | 2014-03-19 14:17:21 +0900 | [diff] [blame] | 2258 | sm_info->rec_prefree_segments = sm_info->main_segments * |
| 2259 | DEF_RECLAIM_PREFREE_SEGMENTS / 100; |
Jaegeuk Kim | 9b5f136 | 2014-09-16 18:30:54 -0700 | [diff] [blame] | 2260 | sm_info->ipu_policy = 1 << F2FS_IPU_FSYNC; |
Jaegeuk Kim | 216fbd6 | 2013-11-07 13:13:42 +0900 | [diff] [blame] | 2261 | sm_info->min_ipu_util = DEF_MIN_IPU_UTIL; |
Jaegeuk Kim | c1ce1b0 | 2014-09-10 16:53:02 -0700 | [diff] [blame] | 2262 | sm_info->min_fsync_blocks = DEF_MIN_FSYNC_BLOCKS; |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2263 | |
Jaegeuk Kim | 7fd9e54 | 2013-11-15 13:55:58 +0900 | [diff] [blame] | 2264 | INIT_LIST_HEAD(&sm_info->discard_list); |
| 2265 | sm_info->nr_discards = 0; |
| 2266 | sm_info->max_discards = 0; |
| 2267 | |
Jaegeuk Kim | bba681c | 2015-01-26 17:41:23 -0800 | [diff] [blame] | 2268 | sm_info->trim_sections = DEF_BATCHED_TRIM_SECTIONS; |
| 2269 | |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 2270 | INIT_LIST_HEAD(&sm_info->sit_entry_set); |
| 2271 | |
Gu Zheng | b270ad6 | 2014-04-11 17:49:55 +0800 | [diff] [blame] | 2272 | if (test_opt(sbi, FLUSH_MERGE) && !f2fs_readonly(sbi->sb)) { |
Gu Zheng | 2163d19 | 2014-04-27 14:21:33 +0800 | [diff] [blame] | 2273 | err = create_flush_cmd_control(sbi); |
| 2274 | if (err) |
Gu Zheng | a688b9d9e | 2014-04-27 14:21:21 +0800 | [diff] [blame] | 2275 | return err; |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 2276 | } |
| 2277 | |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2278 | err = build_sit_info(sbi); |
| 2279 | if (err) |
| 2280 | return err; |
| 2281 | err = build_free_segmap(sbi); |
| 2282 | if (err) |
| 2283 | return err; |
| 2284 | err = build_curseg(sbi); |
| 2285 | if (err) |
| 2286 | return err; |
| 2287 | |
| 2288 | /* reinit free segmap based on SIT */ |
| 2289 | build_sit_entries(sbi); |
| 2290 | |
| 2291 | init_free_segmap(sbi); |
| 2292 | err = build_dirty_segmap(sbi); |
| 2293 | if (err) |
| 2294 | return err; |
| 2295 | |
| 2296 | init_min_max_mtime(sbi); |
| 2297 | return 0; |
| 2298 | } |
| 2299 | |
| 2300 | static void discard_dirty_segmap(struct f2fs_sb_info *sbi, |
| 2301 | enum dirty_type dirty_type) |
| 2302 | { |
| 2303 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
| 2304 | |
| 2305 | mutex_lock(&dirty_i->seglist_lock); |
Jaegeuk Kim | 39307a8 | 2015-09-22 13:50:47 -0700 | [diff] [blame] | 2306 | kvfree(dirty_i->dirty_segmap[dirty_type]); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2307 | dirty_i->nr_dirty[dirty_type] = 0; |
| 2308 | mutex_unlock(&dirty_i->seglist_lock); |
| 2309 | } |
| 2310 | |
Jaegeuk Kim | 5ec4e49 | 2013-03-31 13:26:03 +0900 | [diff] [blame] | 2311 | static void destroy_victim_secmap(struct f2fs_sb_info *sbi) |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2312 | { |
| 2313 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
Jaegeuk Kim | 39307a8 | 2015-09-22 13:50:47 -0700 | [diff] [blame] | 2314 | kvfree(dirty_i->victim_secmap); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2315 | } |
| 2316 | |
| 2317 | static void destroy_dirty_segmap(struct f2fs_sb_info *sbi) |
| 2318 | { |
| 2319 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
| 2320 | int i; |
| 2321 | |
| 2322 | if (!dirty_i) |
| 2323 | return; |
| 2324 | |
| 2325 | /* discard pre-free/dirty segments list */ |
| 2326 | for (i = 0; i < NR_DIRTY_TYPE; i++) |
| 2327 | discard_dirty_segmap(sbi, i); |
| 2328 | |
Jaegeuk Kim | 5ec4e49 | 2013-03-31 13:26:03 +0900 | [diff] [blame] | 2329 | destroy_victim_secmap(sbi); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2330 | SM_I(sbi)->dirty_info = NULL; |
| 2331 | kfree(dirty_i); |
| 2332 | } |
| 2333 | |
| 2334 | static void destroy_curseg(struct f2fs_sb_info *sbi) |
| 2335 | { |
| 2336 | struct curseg_info *array = SM_I(sbi)->curseg_array; |
| 2337 | int i; |
| 2338 | |
| 2339 | if (!array) |
| 2340 | return; |
| 2341 | SM_I(sbi)->curseg_array = NULL; |
| 2342 | for (i = 0; i < NR_CURSEG_TYPE; i++) |
| 2343 | kfree(array[i].sum_blk); |
| 2344 | kfree(array); |
| 2345 | } |
| 2346 | |
| 2347 | static void destroy_free_segmap(struct f2fs_sb_info *sbi) |
| 2348 | { |
| 2349 | struct free_segmap_info *free_i = SM_I(sbi)->free_info; |
| 2350 | if (!free_i) |
| 2351 | return; |
| 2352 | SM_I(sbi)->free_info = NULL; |
Jaegeuk Kim | 39307a8 | 2015-09-22 13:50:47 -0700 | [diff] [blame] | 2353 | kvfree(free_i->free_segmap); |
| 2354 | kvfree(free_i->free_secmap); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2355 | kfree(free_i); |
| 2356 | } |
| 2357 | |
| 2358 | static void destroy_sit_info(struct f2fs_sb_info *sbi) |
| 2359 | { |
| 2360 | struct sit_info *sit_i = SIT_I(sbi); |
| 2361 | unsigned int start; |
| 2362 | |
| 2363 | if (!sit_i) |
| 2364 | return; |
| 2365 | |
| 2366 | if (sit_i->sentries) { |
Jaegeuk Kim | 7cd8558 | 2014-09-23 11:23:01 -0700 | [diff] [blame] | 2367 | for (start = 0; start < MAIN_SEGS(sbi); start++) { |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2368 | kfree(sit_i->sentries[start].cur_valid_map); |
| 2369 | kfree(sit_i->sentries[start].ckpt_valid_map); |
Jaegeuk Kim | a66cdd9 | 2015-04-30 22:37:50 -0700 | [diff] [blame] | 2370 | kfree(sit_i->sentries[start].discard_map); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2371 | } |
| 2372 | } |
Jaegeuk Kim | 60a3b78 | 2015-02-10 16:44:29 -0800 | [diff] [blame] | 2373 | kfree(sit_i->tmp_map); |
| 2374 | |
Jaegeuk Kim | 39307a8 | 2015-09-22 13:50:47 -0700 | [diff] [blame] | 2375 | kvfree(sit_i->sentries); |
| 2376 | kvfree(sit_i->sec_entries); |
| 2377 | kvfree(sit_i->dirty_sentries_bitmap); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2378 | |
| 2379 | SM_I(sbi)->sit_info = NULL; |
| 2380 | kfree(sit_i->sit_bitmap); |
| 2381 | kfree(sit_i); |
| 2382 | } |
| 2383 | |
| 2384 | void destroy_segment_manager(struct f2fs_sb_info *sbi) |
| 2385 | { |
| 2386 | struct f2fs_sm_info *sm_info = SM_I(sbi); |
Gu Zheng | a688b9d9e | 2014-04-27 14:21:21 +0800 | [diff] [blame] | 2387 | |
Chao Yu | 3b03f72 | 2013-11-06 09:12:04 +0800 | [diff] [blame] | 2388 | if (!sm_info) |
| 2389 | return; |
Gu Zheng | 2163d19 | 2014-04-27 14:21:33 +0800 | [diff] [blame] | 2390 | destroy_flush_cmd_control(sbi); |
Jaegeuk Kim | 351df4b | 2012-11-02 17:09:16 +0900 | [diff] [blame] | 2391 | destroy_dirty_segmap(sbi); |
| 2392 | destroy_curseg(sbi); |
| 2393 | destroy_free_segmap(sbi); |
| 2394 | destroy_sit_info(sbi); |
| 2395 | sbi->sm_info = NULL; |
| 2396 | kfree(sm_info); |
| 2397 | } |
Jaegeuk Kim | 7fd9e54 | 2013-11-15 13:55:58 +0900 | [diff] [blame] | 2398 | |
| 2399 | int __init create_segment_manager_caches(void) |
| 2400 | { |
| 2401 | discard_entry_slab = f2fs_kmem_cache_create("discard_entry", |
Gu Zheng | e8512d2 | 2014-03-07 18:43:28 +0800 | [diff] [blame] | 2402 | sizeof(struct discard_entry)); |
Jaegeuk Kim | 7fd9e54 | 2013-11-15 13:55:58 +0900 | [diff] [blame] | 2403 | if (!discard_entry_slab) |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 2404 | goto fail; |
| 2405 | |
| 2406 | sit_entry_set_slab = f2fs_kmem_cache_create("sit_entry_set", |
Changman Lee | c9ee008 | 2014-11-21 15:42:07 +0900 | [diff] [blame] | 2407 | sizeof(struct sit_entry_set)); |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 2408 | if (!sit_entry_set_slab) |
| 2409 | goto destory_discard_entry; |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 2410 | |
| 2411 | inmem_entry_slab = f2fs_kmem_cache_create("inmem_page_entry", |
| 2412 | sizeof(struct inmem_pages)); |
| 2413 | if (!inmem_entry_slab) |
| 2414 | goto destroy_sit_entry_set; |
Jaegeuk Kim | 7fd9e54 | 2013-11-15 13:55:58 +0900 | [diff] [blame] | 2415 | return 0; |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 2416 | |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 2417 | destroy_sit_entry_set: |
| 2418 | kmem_cache_destroy(sit_entry_set_slab); |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 2419 | destory_discard_entry: |
| 2420 | kmem_cache_destroy(discard_entry_slab); |
| 2421 | fail: |
| 2422 | return -ENOMEM; |
Jaegeuk Kim | 7fd9e54 | 2013-11-15 13:55:58 +0900 | [diff] [blame] | 2423 | } |
| 2424 | |
| 2425 | void destroy_segment_manager_caches(void) |
| 2426 | { |
Chao Yu | 184a5cd | 2014-09-04 18:13:01 +0800 | [diff] [blame] | 2427 | kmem_cache_destroy(sit_entry_set_slab); |
Jaegeuk Kim | 7fd9e54 | 2013-11-15 13:55:58 +0900 | [diff] [blame] | 2428 | kmem_cache_destroy(discard_entry_slab); |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 2429 | kmem_cache_destroy(inmem_entry_slab); |
Jaegeuk Kim | 7fd9e54 | 2013-11-15 13:55:58 +0900 | [diff] [blame] | 2430 | } |