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