Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 1 | /* |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 2 | * fs/f2fs/gc.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/module.h> |
| 13 | #include <linux/backing-dev.h> |
| 14 | #include <linux/proc_fs.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/f2fs_fs.h> |
| 17 | #include <linux/kthread.h> |
| 18 | #include <linux/delay.h> |
| 19 | #include <linux/freezer.h> |
| 20 | #include <linux/blkdev.h> |
| 21 | |
| 22 | #include "f2fs.h" |
| 23 | #include "node.h" |
| 24 | #include "segment.h" |
| 25 | #include "gc.h" |
| 26 | |
| 27 | static struct kmem_cache *winode_slab; |
| 28 | |
| 29 | static int gc_thread_func(void *data) |
| 30 | { |
| 31 | struct f2fs_sb_info *sbi = data; |
| 32 | wait_queue_head_t *wq = &sbi->gc_thread->gc_wait_queue_head; |
| 33 | long wait_ms; |
| 34 | |
| 35 | wait_ms = GC_THREAD_MIN_SLEEP_TIME; |
| 36 | |
| 37 | do { |
| 38 | if (try_to_freeze()) |
| 39 | continue; |
| 40 | else |
| 41 | wait_event_interruptible_timeout(*wq, |
| 42 | kthread_should_stop(), |
| 43 | msecs_to_jiffies(wait_ms)); |
| 44 | if (kthread_should_stop()) |
| 45 | break; |
| 46 | |
Changman Lee | d6212a5 | 2013-01-29 18:30:07 +0900 | [diff] [blame] | 47 | if (sbi->sb->s_writers.frozen >= SB_FREEZE_WRITE) { |
| 48 | wait_ms = GC_THREAD_MAX_SLEEP_TIME; |
| 49 | continue; |
| 50 | } |
| 51 | |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 52 | /* |
| 53 | * [GC triggering condition] |
| 54 | * 0. GC is not conducted currently. |
| 55 | * 1. There are enough dirty segments. |
| 56 | * 2. IO subsystem is idle by checking the # of writeback pages. |
| 57 | * 3. IO subsystem is idle by checking the # of requests in |
| 58 | * bdev's request list. |
| 59 | * |
| 60 | * Note) We have to avoid triggering GCs too much frequently. |
| 61 | * Because it is possible that some segments can be |
| 62 | * invalidated soon after by user update or deletion. |
| 63 | * So, I'd like to wait some time to collect dirty segments. |
| 64 | */ |
| 65 | if (!mutex_trylock(&sbi->gc_mutex)) |
| 66 | continue; |
| 67 | |
| 68 | if (!is_idle(sbi)) { |
| 69 | wait_ms = increase_sleep_time(wait_ms); |
| 70 | mutex_unlock(&sbi->gc_mutex); |
| 71 | continue; |
| 72 | } |
| 73 | |
| 74 | if (has_enough_invalid_blocks(sbi)) |
| 75 | wait_ms = decrease_sleep_time(wait_ms); |
| 76 | else |
| 77 | wait_ms = increase_sleep_time(wait_ms); |
| 78 | |
| 79 | sbi->bg_gc++; |
| 80 | |
Jaegeuk Kim | 408e937 | 2013-01-03 17:55:52 +0900 | [diff] [blame] | 81 | if (f2fs_gc(sbi) == GC_NONE) |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 82 | wait_ms = GC_THREAD_NOGC_SLEEP_TIME; |
| 83 | else if (wait_ms == GC_THREAD_NOGC_SLEEP_TIME) |
| 84 | wait_ms = GC_THREAD_MAX_SLEEP_TIME; |
| 85 | |
| 86 | } while (!kthread_should_stop()); |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | int start_gc_thread(struct f2fs_sb_info *sbi) |
| 91 | { |
Namjae Jeon | 1042d60 | 2012-12-01 10:56:13 +0900 | [diff] [blame] | 92 | struct f2fs_gc_kthread *gc_th; |
Namjae Jeon | ec7b1f2 | 2013-02-02 23:52:28 +0900 | [diff] [blame^] | 93 | dev_t dev = sbi->sb->s_bdev->bd_dev; |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 94 | |
Changman Lee | 48600e4 | 2013-02-04 10:05:09 +0900 | [diff] [blame] | 95 | if (!test_opt(sbi, BG_GC)) |
| 96 | return 0; |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 97 | gc_th = kmalloc(sizeof(struct f2fs_gc_kthread), GFP_KERNEL); |
| 98 | if (!gc_th) |
| 99 | return -ENOMEM; |
| 100 | |
| 101 | sbi->gc_thread = gc_th; |
| 102 | init_waitqueue_head(&sbi->gc_thread->gc_wait_queue_head); |
| 103 | sbi->gc_thread->f2fs_gc_task = kthread_run(gc_thread_func, sbi, |
Namjae Jeon | ec7b1f2 | 2013-02-02 23:52:28 +0900 | [diff] [blame^] | 104 | "f2fs_gc-%u:%u", MAJOR(dev), MINOR(dev)); |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 105 | if (IS_ERR(gc_th->f2fs_gc_task)) { |
| 106 | kfree(gc_th); |
| 107 | return -ENOMEM; |
| 108 | } |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | void stop_gc_thread(struct f2fs_sb_info *sbi) |
| 113 | { |
| 114 | struct f2fs_gc_kthread *gc_th = sbi->gc_thread; |
| 115 | if (!gc_th) |
| 116 | return; |
| 117 | kthread_stop(gc_th->f2fs_gc_task); |
| 118 | kfree(gc_th); |
| 119 | sbi->gc_thread = NULL; |
| 120 | } |
| 121 | |
| 122 | static int select_gc_type(int gc_type) |
| 123 | { |
| 124 | return (gc_type == BG_GC) ? GC_CB : GC_GREEDY; |
| 125 | } |
| 126 | |
| 127 | static void select_policy(struct f2fs_sb_info *sbi, int gc_type, |
| 128 | int type, struct victim_sel_policy *p) |
| 129 | { |
| 130 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
| 131 | |
| 132 | if (p->alloc_mode) { |
| 133 | p->gc_mode = GC_GREEDY; |
| 134 | p->dirty_segmap = dirty_i->dirty_segmap[type]; |
| 135 | p->ofs_unit = 1; |
| 136 | } else { |
| 137 | p->gc_mode = select_gc_type(gc_type); |
| 138 | p->dirty_segmap = dirty_i->dirty_segmap[DIRTY]; |
| 139 | p->ofs_unit = sbi->segs_per_sec; |
| 140 | } |
| 141 | p->offset = sbi->last_victim[p->gc_mode]; |
| 142 | } |
| 143 | |
| 144 | static unsigned int get_max_cost(struct f2fs_sb_info *sbi, |
| 145 | struct victim_sel_policy *p) |
| 146 | { |
| 147 | if (p->gc_mode == GC_GREEDY) |
| 148 | return (1 << sbi->log_blocks_per_seg) * p->ofs_unit; |
| 149 | else if (p->gc_mode == GC_CB) |
| 150 | return UINT_MAX; |
| 151 | else /* No other gc_mode */ |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | static unsigned int check_bg_victims(struct f2fs_sb_info *sbi) |
| 156 | { |
| 157 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
| 158 | unsigned int segno; |
| 159 | |
| 160 | /* |
| 161 | * If the gc_type is FG_GC, we can select victim segments |
| 162 | * selected by background GC before. |
| 163 | * Those segments guarantee they have small valid blocks. |
| 164 | */ |
| 165 | segno = find_next_bit(dirty_i->victim_segmap[BG_GC], |
| 166 | TOTAL_SEGS(sbi), 0); |
| 167 | if (segno < TOTAL_SEGS(sbi)) { |
| 168 | clear_bit(segno, dirty_i->victim_segmap[BG_GC]); |
| 169 | return segno; |
| 170 | } |
| 171 | return NULL_SEGNO; |
| 172 | } |
| 173 | |
| 174 | static unsigned int get_cb_cost(struct f2fs_sb_info *sbi, unsigned int segno) |
| 175 | { |
| 176 | struct sit_info *sit_i = SIT_I(sbi); |
| 177 | unsigned int secno = GET_SECNO(sbi, segno); |
| 178 | unsigned int start = secno * sbi->segs_per_sec; |
| 179 | unsigned long long mtime = 0; |
| 180 | unsigned int vblocks; |
| 181 | unsigned char age = 0; |
| 182 | unsigned char u; |
| 183 | unsigned int i; |
| 184 | |
| 185 | for (i = 0; i < sbi->segs_per_sec; i++) |
| 186 | mtime += get_seg_entry(sbi, start + i)->mtime; |
| 187 | vblocks = get_valid_blocks(sbi, segno, sbi->segs_per_sec); |
| 188 | |
| 189 | mtime = div_u64(mtime, sbi->segs_per_sec); |
| 190 | vblocks = div_u64(vblocks, sbi->segs_per_sec); |
| 191 | |
| 192 | u = (vblocks * 100) >> sbi->log_blocks_per_seg; |
| 193 | |
| 194 | /* Handle if the system time is changed by user */ |
| 195 | if (mtime < sit_i->min_mtime) |
| 196 | sit_i->min_mtime = mtime; |
| 197 | if (mtime > sit_i->max_mtime) |
| 198 | sit_i->max_mtime = mtime; |
| 199 | if (sit_i->max_mtime != sit_i->min_mtime) |
| 200 | age = 100 - div64_u64(100 * (mtime - sit_i->min_mtime), |
| 201 | sit_i->max_mtime - sit_i->min_mtime); |
| 202 | |
| 203 | return UINT_MAX - ((100 * (100 - u) * age) / (100 + u)); |
| 204 | } |
| 205 | |
| 206 | static unsigned int get_gc_cost(struct f2fs_sb_info *sbi, unsigned int segno, |
| 207 | struct victim_sel_policy *p) |
| 208 | { |
| 209 | if (p->alloc_mode == SSR) |
| 210 | return get_seg_entry(sbi, segno)->ckpt_valid_blocks; |
| 211 | |
| 212 | /* alloc_mode == LFS */ |
| 213 | if (p->gc_mode == GC_GREEDY) |
| 214 | return get_valid_blocks(sbi, segno, sbi->segs_per_sec); |
| 215 | else |
| 216 | return get_cb_cost(sbi, segno); |
| 217 | } |
| 218 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 219 | /* |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 220 | * This function is called from two pathes. |
| 221 | * One is garbage collection and the other is SSR segment selection. |
| 222 | * When it is called during GC, it just gets a victim segment |
| 223 | * and it does not remove it from dirty seglist. |
| 224 | * When it is called from SSR segment selection, it finds a segment |
| 225 | * which has minimum valid blocks and removes it from dirty seglist. |
| 226 | */ |
| 227 | static int get_victim_by_default(struct f2fs_sb_info *sbi, |
| 228 | unsigned int *result, int gc_type, int type, char alloc_mode) |
| 229 | { |
| 230 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
| 231 | struct victim_sel_policy p; |
| 232 | unsigned int segno; |
| 233 | int nsearched = 0; |
| 234 | |
| 235 | p.alloc_mode = alloc_mode; |
| 236 | select_policy(sbi, gc_type, type, &p); |
| 237 | |
| 238 | p.min_segno = NULL_SEGNO; |
| 239 | p.min_cost = get_max_cost(sbi, &p); |
| 240 | |
| 241 | mutex_lock(&dirty_i->seglist_lock); |
| 242 | |
| 243 | if (p.alloc_mode == LFS && gc_type == FG_GC) { |
| 244 | p.min_segno = check_bg_victims(sbi); |
| 245 | if (p.min_segno != NULL_SEGNO) |
| 246 | goto got_it; |
| 247 | } |
| 248 | |
| 249 | while (1) { |
| 250 | unsigned long cost; |
| 251 | |
| 252 | segno = find_next_bit(p.dirty_segmap, |
| 253 | TOTAL_SEGS(sbi), p.offset); |
| 254 | if (segno >= TOTAL_SEGS(sbi)) { |
| 255 | if (sbi->last_victim[p.gc_mode]) { |
| 256 | sbi->last_victim[p.gc_mode] = 0; |
| 257 | p.offset = 0; |
| 258 | continue; |
| 259 | } |
| 260 | break; |
| 261 | } |
| 262 | p.offset = ((segno / p.ofs_unit) * p.ofs_unit) + p.ofs_unit; |
| 263 | |
| 264 | if (test_bit(segno, dirty_i->victim_segmap[FG_GC])) |
| 265 | continue; |
| 266 | if (gc_type == BG_GC && |
| 267 | test_bit(segno, dirty_i->victim_segmap[BG_GC])) |
| 268 | continue; |
| 269 | if (IS_CURSEC(sbi, GET_SECNO(sbi, segno))) |
| 270 | continue; |
| 271 | |
| 272 | cost = get_gc_cost(sbi, segno, &p); |
| 273 | |
| 274 | if (p.min_cost > cost) { |
| 275 | p.min_segno = segno; |
| 276 | p.min_cost = cost; |
| 277 | } |
| 278 | |
| 279 | if (cost == get_max_cost(sbi, &p)) |
| 280 | continue; |
| 281 | |
| 282 | if (nsearched++ >= MAX_VICTIM_SEARCH) { |
| 283 | sbi->last_victim[p.gc_mode] = segno; |
| 284 | break; |
| 285 | } |
| 286 | } |
| 287 | got_it: |
| 288 | if (p.min_segno != NULL_SEGNO) { |
| 289 | *result = (p.min_segno / p.ofs_unit) * p.ofs_unit; |
| 290 | if (p.alloc_mode == LFS) { |
| 291 | int i; |
| 292 | for (i = 0; i < p.ofs_unit; i++) |
| 293 | set_bit(*result + i, |
| 294 | dirty_i->victim_segmap[gc_type]); |
| 295 | } |
| 296 | } |
| 297 | mutex_unlock(&dirty_i->seglist_lock); |
| 298 | |
| 299 | return (p.min_segno == NULL_SEGNO) ? 0 : 1; |
| 300 | } |
| 301 | |
| 302 | static const struct victim_selection default_v_ops = { |
| 303 | .get_victim = get_victim_by_default, |
| 304 | }; |
| 305 | |
| 306 | static struct inode *find_gc_inode(nid_t ino, struct list_head *ilist) |
| 307 | { |
| 308 | struct list_head *this; |
| 309 | struct inode_entry *ie; |
| 310 | |
| 311 | list_for_each(this, ilist) { |
| 312 | ie = list_entry(this, struct inode_entry, list); |
| 313 | if (ie->inode->i_ino == ino) |
| 314 | return ie->inode; |
| 315 | } |
| 316 | return NULL; |
| 317 | } |
| 318 | |
| 319 | static void add_gc_inode(struct inode *inode, struct list_head *ilist) |
| 320 | { |
| 321 | struct list_head *this; |
| 322 | struct inode_entry *new_ie, *ie; |
| 323 | |
| 324 | list_for_each(this, ilist) { |
| 325 | ie = list_entry(this, struct inode_entry, list); |
| 326 | if (ie->inode == inode) { |
| 327 | iput(inode); |
| 328 | return; |
| 329 | } |
| 330 | } |
| 331 | repeat: |
| 332 | new_ie = kmem_cache_alloc(winode_slab, GFP_NOFS); |
| 333 | if (!new_ie) { |
| 334 | cond_resched(); |
| 335 | goto repeat; |
| 336 | } |
| 337 | new_ie->inode = inode; |
| 338 | list_add_tail(&new_ie->list, ilist); |
| 339 | } |
| 340 | |
| 341 | static void put_gc_inode(struct list_head *ilist) |
| 342 | { |
| 343 | struct inode_entry *ie, *next_ie; |
| 344 | list_for_each_entry_safe(ie, next_ie, ilist, list) { |
| 345 | iput(ie->inode); |
| 346 | list_del(&ie->list); |
| 347 | kmem_cache_free(winode_slab, ie); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | static int check_valid_map(struct f2fs_sb_info *sbi, |
| 352 | unsigned int segno, int offset) |
| 353 | { |
| 354 | struct sit_info *sit_i = SIT_I(sbi); |
| 355 | struct seg_entry *sentry; |
| 356 | int ret; |
| 357 | |
| 358 | mutex_lock(&sit_i->sentry_lock); |
| 359 | sentry = get_seg_entry(sbi, segno); |
| 360 | ret = f2fs_test_bit(offset, sentry->cur_valid_map); |
| 361 | mutex_unlock(&sit_i->sentry_lock); |
| 362 | return ret ? GC_OK : GC_NEXT; |
| 363 | } |
| 364 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 365 | /* |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 366 | * This function compares node address got in summary with that in NAT. |
| 367 | * On validity, copy that node with cold status, otherwise (invalid node) |
| 368 | * ignore that. |
| 369 | */ |
| 370 | static int gc_node_segment(struct f2fs_sb_info *sbi, |
| 371 | struct f2fs_summary *sum, unsigned int segno, int gc_type) |
| 372 | { |
| 373 | bool initial = true; |
| 374 | struct f2fs_summary *entry; |
| 375 | int off; |
| 376 | |
| 377 | next_step: |
| 378 | entry = sum; |
| 379 | for (off = 0; off < sbi->blocks_per_seg; off++, entry++) { |
| 380 | nid_t nid = le32_to_cpu(entry->nid); |
| 381 | struct page *node_page; |
| 382 | int err; |
| 383 | |
| 384 | /* |
| 385 | * It makes sure that free segments are able to write |
| 386 | * all the dirty node pages before CP after this CP. |
| 387 | * So let's check the space of dirty node pages. |
| 388 | */ |
| 389 | if (should_do_checkpoint(sbi)) { |
| 390 | mutex_lock(&sbi->cp_mutex); |
| 391 | block_operations(sbi); |
| 392 | return GC_BLOCKED; |
| 393 | } |
| 394 | |
| 395 | err = check_valid_map(sbi, segno, off); |
Jaegeuk Kim | 2b50638 | 2012-12-26 14:39:50 +0900 | [diff] [blame] | 396 | if (err == GC_NEXT) |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 397 | continue; |
| 398 | |
| 399 | if (initial) { |
| 400 | ra_node_page(sbi, nid); |
| 401 | continue; |
| 402 | } |
| 403 | node_page = get_node_page(sbi, nid); |
| 404 | if (IS_ERR(node_page)) |
| 405 | continue; |
| 406 | |
| 407 | /* set page dirty and write it */ |
| 408 | if (!PageWriteback(node_page)) |
| 409 | set_page_dirty(node_page); |
| 410 | f2fs_put_page(node_page, 1); |
| 411 | stat_inc_node_blk_count(sbi, 1); |
| 412 | } |
| 413 | if (initial) { |
| 414 | initial = false; |
| 415 | goto next_step; |
| 416 | } |
| 417 | |
| 418 | if (gc_type == FG_GC) { |
| 419 | struct writeback_control wbc = { |
| 420 | .sync_mode = WB_SYNC_ALL, |
| 421 | .nr_to_write = LONG_MAX, |
| 422 | .for_reclaim = 0, |
| 423 | }; |
| 424 | sync_node_pages(sbi, 0, &wbc); |
| 425 | } |
| 426 | return GC_DONE; |
| 427 | } |
| 428 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 429 | /* |
Jaegeuk Kim | 9af45ef | 2013-01-21 17:34:21 +0900 | [diff] [blame] | 430 | * Calculate start block index indicating the given node offset. |
| 431 | * Be careful, caller should give this node offset only indicating direct node |
| 432 | * blocks. If any node offsets, which point the other types of node blocks such |
| 433 | * as indirect or double indirect node blocks, are given, it must be a caller's |
| 434 | * bug. |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 435 | */ |
| 436 | block_t start_bidx_of_node(unsigned int node_ofs) |
| 437 | { |
Jaegeuk Kim | ce19a5d | 2012-12-26 12:03:22 +0900 | [diff] [blame] | 438 | unsigned int indirect_blks = 2 * NIDS_PER_BLOCK + 4; |
| 439 | unsigned int bidx; |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 440 | |
Jaegeuk Kim | ce19a5d | 2012-12-26 12:03:22 +0900 | [diff] [blame] | 441 | if (node_ofs == 0) |
| 442 | return 0; |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 443 | |
Jaegeuk Kim | ce19a5d | 2012-12-26 12:03:22 +0900 | [diff] [blame] | 444 | if (node_ofs <= 2) { |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 445 | bidx = node_ofs - 1; |
| 446 | } else if (node_ofs <= indirect_blks) { |
Jaegeuk Kim | ce19a5d | 2012-12-26 12:03:22 +0900 | [diff] [blame] | 447 | int dec = (node_ofs - 4) / (NIDS_PER_BLOCK + 1); |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 448 | bidx = node_ofs - 2 - dec; |
| 449 | } else { |
Jaegeuk Kim | ce19a5d | 2012-12-26 12:03:22 +0900 | [diff] [blame] | 450 | int dec = (node_ofs - indirect_blks - 3) / (NIDS_PER_BLOCK + 1); |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 451 | bidx = node_ofs - 5 - dec; |
| 452 | } |
Jaegeuk Kim | ce19a5d | 2012-12-26 12:03:22 +0900 | [diff] [blame] | 453 | return bidx * ADDRS_PER_BLOCK + ADDRS_PER_INODE; |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | static int check_dnode(struct f2fs_sb_info *sbi, struct f2fs_summary *sum, |
| 457 | struct node_info *dni, block_t blkaddr, unsigned int *nofs) |
| 458 | { |
| 459 | struct page *node_page; |
| 460 | nid_t nid; |
| 461 | unsigned int ofs_in_node; |
| 462 | block_t source_blkaddr; |
| 463 | |
| 464 | nid = le32_to_cpu(sum->nid); |
| 465 | ofs_in_node = le16_to_cpu(sum->ofs_in_node); |
| 466 | |
| 467 | node_page = get_node_page(sbi, nid); |
| 468 | if (IS_ERR(node_page)) |
| 469 | return GC_NEXT; |
| 470 | |
| 471 | get_node_info(sbi, nid, dni); |
| 472 | |
| 473 | if (sum->version != dni->version) { |
| 474 | f2fs_put_page(node_page, 1); |
| 475 | return GC_NEXT; |
| 476 | } |
| 477 | |
| 478 | *nofs = ofs_of_node(node_page); |
| 479 | source_blkaddr = datablock_addr(node_page, ofs_in_node); |
| 480 | f2fs_put_page(node_page, 1); |
| 481 | |
| 482 | if (source_blkaddr != blkaddr) |
| 483 | return GC_NEXT; |
| 484 | return GC_OK; |
| 485 | } |
| 486 | |
| 487 | static void move_data_page(struct inode *inode, struct page *page, int gc_type) |
| 488 | { |
| 489 | if (page->mapping != inode->i_mapping) |
| 490 | goto out; |
| 491 | |
| 492 | if (inode != page->mapping->host) |
| 493 | goto out; |
| 494 | |
| 495 | if (PageWriteback(page)) |
| 496 | goto out; |
| 497 | |
| 498 | if (gc_type == BG_GC) { |
| 499 | set_page_dirty(page); |
| 500 | set_cold_data(page); |
| 501 | } else { |
| 502 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
| 503 | mutex_lock_op(sbi, DATA_WRITE); |
| 504 | if (clear_page_dirty_for_io(page) && |
| 505 | S_ISDIR(inode->i_mode)) { |
| 506 | dec_page_count(sbi, F2FS_DIRTY_DENTS); |
| 507 | inode_dec_dirty_dents(inode); |
| 508 | } |
| 509 | set_cold_data(page); |
| 510 | do_write_data_page(page); |
| 511 | mutex_unlock_op(sbi, DATA_WRITE); |
| 512 | clear_cold_data(page); |
| 513 | } |
| 514 | out: |
| 515 | f2fs_put_page(page, 1); |
| 516 | } |
| 517 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 518 | /* |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 519 | * This function tries to get parent node of victim data block, and identifies |
| 520 | * data block validity. If the block is valid, copy that with cold status and |
| 521 | * modify parent node. |
| 522 | * If the parent node is not valid or the data block address is different, |
| 523 | * the victim data block is ignored. |
| 524 | */ |
| 525 | static int gc_data_segment(struct f2fs_sb_info *sbi, struct f2fs_summary *sum, |
| 526 | struct list_head *ilist, unsigned int segno, int gc_type) |
| 527 | { |
| 528 | struct super_block *sb = sbi->sb; |
| 529 | struct f2fs_summary *entry; |
| 530 | block_t start_addr; |
| 531 | int err, off; |
| 532 | int phase = 0; |
| 533 | |
| 534 | start_addr = START_BLOCK(sbi, segno); |
| 535 | |
| 536 | next_step: |
| 537 | entry = sum; |
| 538 | for (off = 0; off < sbi->blocks_per_seg; off++, entry++) { |
| 539 | struct page *data_page; |
| 540 | struct inode *inode; |
| 541 | struct node_info dni; /* dnode info for the data */ |
| 542 | unsigned int ofs_in_node, nofs; |
| 543 | block_t start_bidx; |
| 544 | |
| 545 | /* |
| 546 | * It makes sure that free segments are able to write |
| 547 | * all the dirty node pages before CP after this CP. |
| 548 | * So let's check the space of dirty node pages. |
| 549 | */ |
| 550 | if (should_do_checkpoint(sbi)) { |
| 551 | mutex_lock(&sbi->cp_mutex); |
| 552 | block_operations(sbi); |
| 553 | err = GC_BLOCKED; |
| 554 | goto stop; |
| 555 | } |
| 556 | |
| 557 | err = check_valid_map(sbi, segno, off); |
Jaegeuk Kim | 2b50638 | 2012-12-26 14:39:50 +0900 | [diff] [blame] | 558 | if (err == GC_NEXT) |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 559 | continue; |
| 560 | |
| 561 | if (phase == 0) { |
| 562 | ra_node_page(sbi, le32_to_cpu(entry->nid)); |
| 563 | continue; |
| 564 | } |
| 565 | |
| 566 | /* Get an inode by ino with checking validity */ |
| 567 | err = check_dnode(sbi, entry, &dni, start_addr + off, &nofs); |
Jaegeuk Kim | 2b50638 | 2012-12-26 14:39:50 +0900 | [diff] [blame] | 568 | if (err == GC_NEXT) |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 569 | continue; |
| 570 | |
| 571 | if (phase == 1) { |
| 572 | ra_node_page(sbi, dni.ino); |
| 573 | continue; |
| 574 | } |
| 575 | |
| 576 | start_bidx = start_bidx_of_node(nofs); |
| 577 | ofs_in_node = le16_to_cpu(entry->ofs_in_node); |
| 578 | |
| 579 | if (phase == 2) { |
Jaegeuk Kim | d4686d56 | 2013-01-31 15:36:04 +0900 | [diff] [blame] | 580 | inode = f2fs_iget(sb, dni.ino); |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 581 | if (IS_ERR(inode)) |
| 582 | continue; |
| 583 | |
| 584 | data_page = find_data_page(inode, |
| 585 | start_bidx + ofs_in_node); |
| 586 | if (IS_ERR(data_page)) |
| 587 | goto next_iput; |
| 588 | |
| 589 | f2fs_put_page(data_page, 0); |
| 590 | add_gc_inode(inode, ilist); |
| 591 | } else { |
| 592 | inode = find_gc_inode(dni.ino, ilist); |
| 593 | if (inode) { |
| 594 | data_page = get_lock_data_page(inode, |
| 595 | start_bidx + ofs_in_node); |
| 596 | if (IS_ERR(data_page)) |
| 597 | continue; |
| 598 | move_data_page(inode, data_page, gc_type); |
| 599 | stat_inc_data_blk_count(sbi, 1); |
| 600 | } |
| 601 | } |
| 602 | continue; |
| 603 | next_iput: |
| 604 | iput(inode); |
| 605 | } |
| 606 | if (++phase < 4) |
| 607 | goto next_step; |
| 608 | err = GC_DONE; |
| 609 | stop: |
| 610 | if (gc_type == FG_GC) |
| 611 | f2fs_submit_bio(sbi, DATA, true); |
| 612 | return err; |
| 613 | } |
| 614 | |
| 615 | static int __get_victim(struct f2fs_sb_info *sbi, unsigned int *victim, |
| 616 | int gc_type, int type) |
| 617 | { |
| 618 | struct sit_info *sit_i = SIT_I(sbi); |
| 619 | int ret; |
| 620 | mutex_lock(&sit_i->sentry_lock); |
| 621 | ret = DIRTY_I(sbi)->v_ops->get_victim(sbi, victim, gc_type, type, LFS); |
| 622 | mutex_unlock(&sit_i->sentry_lock); |
| 623 | return ret; |
| 624 | } |
| 625 | |
| 626 | static int do_garbage_collect(struct f2fs_sb_info *sbi, unsigned int segno, |
| 627 | struct list_head *ilist, int gc_type) |
| 628 | { |
| 629 | struct page *sum_page; |
| 630 | struct f2fs_summary_block *sum; |
| 631 | int ret = GC_DONE; |
| 632 | |
| 633 | /* read segment summary of victim */ |
| 634 | sum_page = get_sum_page(sbi, segno); |
| 635 | if (IS_ERR(sum_page)) |
| 636 | return GC_ERROR; |
| 637 | |
| 638 | /* |
| 639 | * CP needs to lock sum_page. In this time, we don't need |
| 640 | * to lock this page, because this summary page is not gone anywhere. |
| 641 | * Also, this page is not gonna be updated before GC is done. |
| 642 | */ |
| 643 | unlock_page(sum_page); |
| 644 | sum = page_address(sum_page); |
| 645 | |
| 646 | switch (GET_SUM_TYPE((&sum->footer))) { |
| 647 | case SUM_TYPE_NODE: |
| 648 | ret = gc_node_segment(sbi, sum->entries, segno, gc_type); |
| 649 | break; |
| 650 | case SUM_TYPE_DATA: |
| 651 | ret = gc_data_segment(sbi, sum->entries, ilist, segno, gc_type); |
| 652 | break; |
| 653 | } |
| 654 | stat_inc_seg_count(sbi, GET_SUM_TYPE((&sum->footer))); |
| 655 | stat_inc_call_count(sbi->stat_info); |
| 656 | |
| 657 | f2fs_put_page(sum_page, 0); |
| 658 | return ret; |
| 659 | } |
| 660 | |
Jaegeuk Kim | 408e937 | 2013-01-03 17:55:52 +0900 | [diff] [blame] | 661 | int f2fs_gc(struct f2fs_sb_info *sbi) |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 662 | { |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 663 | struct list_head ilist; |
Jaegeuk Kim | 408e937 | 2013-01-03 17:55:52 +0900 | [diff] [blame] | 664 | unsigned int segno, i; |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 665 | int gc_type = BG_GC; |
Jaegeuk Kim | 408e937 | 2013-01-03 17:55:52 +0900 | [diff] [blame] | 666 | int gc_status = GC_NONE; |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 667 | |
| 668 | INIT_LIST_HEAD(&ilist); |
| 669 | gc_more: |
Jaegeuk Kim | 408e937 | 2013-01-03 17:55:52 +0900 | [diff] [blame] | 670 | if (!(sbi->sb->s_flags & MS_ACTIVE)) |
| 671 | goto stop; |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 672 | |
Namjae Jeon | 3786dfd | 2013-01-30 22:47:02 +0900 | [diff] [blame] | 673 | if (gc_type == BG_GC && has_not_enough_free_secs(sbi)) |
Jaegeuk Kim | 408e937 | 2013-01-03 17:55:52 +0900 | [diff] [blame] | 674 | gc_type = FG_GC; |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 675 | |
Jaegeuk Kim | 408e937 | 2013-01-03 17:55:52 +0900 | [diff] [blame] | 676 | if (!__get_victim(sbi, &segno, gc_type, NO_CHECK_TYPE)) |
| 677 | goto stop; |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 678 | |
Jaegeuk Kim | 408e937 | 2013-01-03 17:55:52 +0900 | [diff] [blame] | 679 | for (i = 0; i < sbi->segs_per_sec; i++) { |
| 680 | /* |
| 681 | * do_garbage_collect will give us three gc_status: |
| 682 | * GC_ERROR, GC_DONE, and GC_BLOCKED. |
| 683 | * If GC is finished uncleanly, we have to return |
| 684 | * the victim to dirty segment list. |
| 685 | */ |
| 686 | gc_status = do_garbage_collect(sbi, segno + i, &ilist, gc_type); |
| 687 | if (gc_status != GC_DONE) |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 688 | break; |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 689 | } |
Jaegeuk Kim | 408e937 | 2013-01-03 17:55:52 +0900 | [diff] [blame] | 690 | if (has_not_enough_free_secs(sbi)) { |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 691 | write_checkpoint(sbi, (gc_status == GC_BLOCKED), false); |
Jaegeuk Kim | 408e937 | 2013-01-03 17:55:52 +0900 | [diff] [blame] | 692 | if (has_not_enough_free_secs(sbi)) |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 693 | goto gc_more; |
| 694 | } |
Jaegeuk Kim | 408e937 | 2013-01-03 17:55:52 +0900 | [diff] [blame] | 695 | stop: |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 696 | mutex_unlock(&sbi->gc_mutex); |
| 697 | |
| 698 | put_gc_inode(&ilist); |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 699 | return gc_status; |
| 700 | } |
| 701 | |
| 702 | void build_gc_manager(struct f2fs_sb_info *sbi) |
| 703 | { |
| 704 | DIRTY_I(sbi)->v_ops = &default_v_ops; |
| 705 | } |
| 706 | |
Namjae Jeon | 6e6093a | 2013-01-17 00:08:30 +0900 | [diff] [blame] | 707 | int __init create_gc_caches(void) |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 708 | { |
| 709 | winode_slab = f2fs_kmem_cache_create("f2fs_gc_inodes", |
| 710 | sizeof(struct inode_entry), NULL); |
| 711 | if (!winode_slab) |
| 712 | return -ENOMEM; |
| 713 | return 0; |
| 714 | } |
| 715 | |
| 716 | void destroy_gc_caches(void) |
| 717 | { |
| 718 | kmem_cache_destroy(winode_slab); |
| 719 | } |