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