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