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