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