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