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