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