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