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