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