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