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