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