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