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