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