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