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