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