f2fs: avoid writing inode redundantly when creating a file
[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
JK
542
543 if (force) {
544 new_curseg(sbi, type, true);
545 goto out;
546 }
547
763bfe1b 548 if (type == CURSEG_WARM_NODE)
351df4b2 549 new_curseg(sbi, type, false);
60374688
JK
550 else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
551 new_curseg(sbi, type, false);
351df4b2
JK
552 else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
553 change_curseg(sbi, type, true);
554 else
555 new_curseg(sbi, type, false);
556out:
35b09d82 557#ifdef CONFIG_F2FS_STAT_FS
351df4b2 558 sbi->segment_count[curseg->alloc_type]++;
35b09d82 559#endif
351df4b2
JK
560}
561
562void allocate_new_segments(struct f2fs_sb_info *sbi)
563{
564 struct curseg_info *curseg;
565 unsigned int old_curseg;
566 int i;
567
568 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
569 curseg = CURSEG_I(sbi, i);
570 old_curseg = curseg->segno;
571 SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
572 locate_dirty_segment(sbi, old_curseg);
573 }
574}
575
576static const struct segment_allocation default_salloc_ops = {
577 .allocate_segment = allocate_segment_by_default,
578};
579
580static void f2fs_end_io_write(struct bio *bio, int err)
581{
582 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
583 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
584 struct bio_private *p = bio->bi_private;
585
586 do {
587 struct page *page = bvec->bv_page;
588
589 if (--bvec >= bio->bi_io_vec)
590 prefetchw(&bvec->bv_page->flags);
591 if (!uptodate) {
592 SetPageError(page);
593 if (page->mapping)
594 set_bit(AS_EIO, &page->mapping->flags);
25ca923b 595 set_ckpt_flags(p->sbi->ckpt, CP_ERROR_FLAG);
577e3495 596 p->sbi->sb->s_flags |= MS_RDONLY;
351df4b2
JK
597 }
598 end_page_writeback(page);
599 dec_page_count(p->sbi, F2FS_WRITEBACK);
600 } while (bvec >= bio->bi_io_vec);
601
602 if (p->is_sync)
603 complete(p->wait);
604 kfree(p);
605 bio_put(bio);
606}
607
3cd8a239 608struct bio *f2fs_bio_alloc(struct block_device *bdev, int npages)
351df4b2
JK
609{
610 struct bio *bio;
3cd8a239
JK
611
612 /* No failure on bio allocation */
613 bio = bio_alloc(GFP_NOIO, npages);
614 bio->bi_bdev = bdev;
d8207f69
GZ
615 bio->bi_private = NULL;
616
351df4b2
JK
617 return bio;
618}
619
620static void do_submit_bio(struct f2fs_sb_info *sbi,
621 enum page_type type, bool sync)
622{
623 int rw = sync ? WRITE_SYNC : WRITE;
624 enum page_type btype = type > META ? META : type;
625
626 if (type >= META_FLUSH)
627 rw = WRITE_FLUSH_FUA;
628
8680441c
NJ
629 if (btype == META)
630 rw |= REQ_META;
631
351df4b2
JK
632 if (sbi->bio[btype]) {
633 struct bio_private *p = sbi->bio[btype]->bi_private;
634 p->sbi = sbi;
635 sbi->bio[btype]->bi_end_io = f2fs_end_io_write;
6ec178da
NJ
636
637 trace_f2fs_do_submit_bio(sbi->sb, btype, sync, sbi->bio[btype]);
638
351df4b2
JK
639 if (type == META_FLUSH) {
640 DECLARE_COMPLETION_ONSTACK(wait);
641 p->is_sync = true;
642 p->wait = &wait;
643 submit_bio(rw, sbi->bio[btype]);
644 wait_for_completion(&wait);
645 } else {
646 p->is_sync = false;
647 submit_bio(rw, sbi->bio[btype]);
648 }
649 sbi->bio[btype] = NULL;
650 }
651}
652
653void f2fs_submit_bio(struct f2fs_sb_info *sbi, enum page_type type, bool sync)
654{
655 down_write(&sbi->bio_sem);
656 do_submit_bio(sbi, type, sync);
657 up_write(&sbi->bio_sem);
658}
659
660static void submit_write_page(struct f2fs_sb_info *sbi, struct page *page,
661 block_t blk_addr, enum page_type type)
662{
663 struct block_device *bdev = sbi->sb->s_bdev;
664
665 verify_block_addr(sbi, blk_addr);
666
667 down_write(&sbi->bio_sem);
668
669 inc_page_count(sbi, F2FS_WRITEBACK);
670
671 if (sbi->bio[type] && sbi->last_block_in_bio[type] != blk_addr - 1)
672 do_submit_bio(sbi, type, false);
673alloc_new:
3cd8a239 674 if (sbi->bio[type] == NULL) {
d8207f69
GZ
675 struct bio_private *priv;
676retry:
677 priv = kmalloc(sizeof(struct bio_private), GFP_NOFS);
678 if (!priv) {
679 cond_resched();
680 goto retry;
681 }
682
ac5d156c 683 sbi->bio[type] = f2fs_bio_alloc(bdev, max_hw_blocks(sbi));
3cd8a239 684 sbi->bio[type]->bi_sector = SECTOR_FROM_BLOCK(sbi, blk_addr);
d8207f69 685 sbi->bio[type]->bi_private = priv;
3cd8a239
JK
686 /*
687 * The end_io will be assigned at the sumbission phase.
688 * Until then, let bio_add_page() merge consecutive IOs as much
689 * as possible.
690 */
691 }
351df4b2
JK
692
693 if (bio_add_page(sbi->bio[type], page, PAGE_CACHE_SIZE, 0) <
694 PAGE_CACHE_SIZE) {
695 do_submit_bio(sbi, type, false);
696 goto alloc_new;
697 }
698
699 sbi->last_block_in_bio[type] = blk_addr;
700
701 up_write(&sbi->bio_sem);
6ec178da 702 trace_f2fs_submit_write_page(page, blk_addr, type);
351df4b2
JK
703}
704
a569469e
JX
705void f2fs_wait_on_page_writeback(struct page *page,
706 enum page_type type, bool sync)
707{
708 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
709 if (PageWriteback(page)) {
710 f2fs_submit_bio(sbi, type, sync);
711 wait_on_page_writeback(page);
712 }
713}
714
351df4b2
JK
715static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
716{
717 struct curseg_info *curseg = CURSEG_I(sbi, type);
718 if (curseg->next_blkoff < sbi->blocks_per_seg)
719 return true;
720 return false;
721}
722
723static int __get_segment_type_2(struct page *page, enum page_type p_type)
724{
725 if (p_type == DATA)
726 return CURSEG_HOT_DATA;
727 else
728 return CURSEG_HOT_NODE;
729}
730
731static int __get_segment_type_4(struct page *page, enum page_type p_type)
732{
733 if (p_type == DATA) {
734 struct inode *inode = page->mapping->host;
735
736 if (S_ISDIR(inode->i_mode))
737 return CURSEG_HOT_DATA;
738 else
739 return CURSEG_COLD_DATA;
740 } else {
741 if (IS_DNODE(page) && !is_cold_node(page))
742 return CURSEG_HOT_NODE;
743 else
744 return CURSEG_COLD_NODE;
745 }
746}
747
748static int __get_segment_type_6(struct page *page, enum page_type p_type)
749{
750 if (p_type == DATA) {
751 struct inode *inode = page->mapping->host;
752
753 if (S_ISDIR(inode->i_mode))
754 return CURSEG_HOT_DATA;
354a3399 755 else if (is_cold_data(page) || file_is_cold(inode))
351df4b2
JK
756 return CURSEG_COLD_DATA;
757 else
758 return CURSEG_WARM_DATA;
759 } else {
760 if (IS_DNODE(page))
761 return is_cold_node(page) ? CURSEG_WARM_NODE :
762 CURSEG_HOT_NODE;
763 else
764 return CURSEG_COLD_NODE;
765 }
766}
767
768static int __get_segment_type(struct page *page, enum page_type p_type)
769{
770 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
771 switch (sbi->active_logs) {
772 case 2:
773 return __get_segment_type_2(page, p_type);
774 case 4:
775 return __get_segment_type_4(page, p_type);
351df4b2 776 }
12a67146
JK
777 /* NR_CURSEG_TYPE(6) logs by default */
778 BUG_ON(sbi->active_logs != NR_CURSEG_TYPE);
779 return __get_segment_type_6(page, p_type);
351df4b2
JK
780}
781
782static void do_write_page(struct f2fs_sb_info *sbi, struct page *page,
783 block_t old_blkaddr, block_t *new_blkaddr,
784 struct f2fs_summary *sum, enum page_type p_type)
785{
786 struct sit_info *sit_i = SIT_I(sbi);
787 struct curseg_info *curseg;
788 unsigned int old_cursegno;
789 int type;
790
791 type = __get_segment_type(page, p_type);
792 curseg = CURSEG_I(sbi, type);
793
794 mutex_lock(&curseg->curseg_mutex);
795
796 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
797 old_cursegno = curseg->segno;
798
799 /*
800 * __add_sum_entry should be resided under the curseg_mutex
801 * because, this function updates a summary entry in the
802 * current summary block.
803 */
e79efe3b 804 __add_sum_entry(sbi, type, sum);
351df4b2
JK
805
806 mutex_lock(&sit_i->sentry_lock);
807 __refresh_next_blkoff(sbi, curseg);
35b09d82 808#ifdef CONFIG_F2FS_STAT_FS
351df4b2 809 sbi->block_count[curseg->alloc_type]++;
35b09d82 810#endif
351df4b2
JK
811
812 /*
813 * SIT information should be updated before segment allocation,
814 * since SSR needs latest valid block information.
815 */
816 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
817
818 if (!__has_curseg_space(sbi, type))
819 sit_i->s_ops->allocate_segment(sbi, type, false);
820
821 locate_dirty_segment(sbi, old_cursegno);
822 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
823 mutex_unlock(&sit_i->sentry_lock);
824
825 if (p_type == NODE)
826 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
827
828 /* writeout dirty page into bdev */
829 submit_write_page(sbi, page, *new_blkaddr, p_type);
830
831 mutex_unlock(&curseg->curseg_mutex);
832}
833
577e3495 834void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
351df4b2 835{
351df4b2
JK
836 set_page_writeback(page);
837 submit_write_page(sbi, page, page->index, META);
351df4b2
JK
838}
839
840void write_node_page(struct f2fs_sb_info *sbi, struct page *page,
841 unsigned int nid, block_t old_blkaddr, block_t *new_blkaddr)
842{
843 struct f2fs_summary sum;
844 set_summary(&sum, nid, 0, 0);
845 do_write_page(sbi, page, old_blkaddr, new_blkaddr, &sum, NODE);
846}
847
848void write_data_page(struct inode *inode, struct page *page,
849 struct dnode_of_data *dn, block_t old_blkaddr,
850 block_t *new_blkaddr)
851{
852 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
853 struct f2fs_summary sum;
854 struct node_info ni;
855
856 BUG_ON(old_blkaddr == NULL_ADDR);
857 get_node_info(sbi, dn->nid, &ni);
858 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
859
860 do_write_page(sbi, page, old_blkaddr,
861 new_blkaddr, &sum, DATA);
862}
863
864void rewrite_data_page(struct f2fs_sb_info *sbi, struct page *page,
865 block_t old_blk_addr)
866{
867 submit_write_page(sbi, page, old_blk_addr, DATA);
868}
869
870void recover_data_page(struct f2fs_sb_info *sbi,
871 struct page *page, struct f2fs_summary *sum,
872 block_t old_blkaddr, block_t new_blkaddr)
873{
874 struct sit_info *sit_i = SIT_I(sbi);
875 struct curseg_info *curseg;
876 unsigned int segno, old_cursegno;
877 struct seg_entry *se;
878 int type;
879
880 segno = GET_SEGNO(sbi, new_blkaddr);
881 se = get_seg_entry(sbi, segno);
882 type = se->type;
883
884 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
885 if (old_blkaddr == NULL_ADDR)
886 type = CURSEG_COLD_DATA;
887 else
888 type = CURSEG_WARM_DATA;
889 }
890 curseg = CURSEG_I(sbi, type);
891
892 mutex_lock(&curseg->curseg_mutex);
893 mutex_lock(&sit_i->sentry_lock);
894
895 old_cursegno = curseg->segno;
896
897 /* change the current segment */
898 if (segno != curseg->segno) {
899 curseg->next_segno = segno;
900 change_curseg(sbi, type, true);
901 }
902
903 curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, new_blkaddr) &
904 (sbi->blocks_per_seg - 1);
e79efe3b 905 __add_sum_entry(sbi, type, sum);
351df4b2
JK
906
907 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
908
909 locate_dirty_segment(sbi, old_cursegno);
910 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
911
912 mutex_unlock(&sit_i->sentry_lock);
913 mutex_unlock(&curseg->curseg_mutex);
914}
915
916void rewrite_node_page(struct f2fs_sb_info *sbi,
917 struct page *page, struct f2fs_summary *sum,
918 block_t old_blkaddr, block_t new_blkaddr)
919{
920 struct sit_info *sit_i = SIT_I(sbi);
921 int type = CURSEG_WARM_NODE;
922 struct curseg_info *curseg;
923 unsigned int segno, old_cursegno;
924 block_t next_blkaddr = next_blkaddr_of_node(page);
925 unsigned int next_segno = GET_SEGNO(sbi, next_blkaddr);
926
927 curseg = CURSEG_I(sbi, type);
928
929 mutex_lock(&curseg->curseg_mutex);
930 mutex_lock(&sit_i->sentry_lock);
931
932 segno = GET_SEGNO(sbi, new_blkaddr);
933 old_cursegno = curseg->segno;
934
935 /* change the current segment */
936 if (segno != curseg->segno) {
937 curseg->next_segno = segno;
938 change_curseg(sbi, type, true);
939 }
940 curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, new_blkaddr) &
941 (sbi->blocks_per_seg - 1);
e79efe3b 942 __add_sum_entry(sbi, type, sum);
351df4b2
JK
943
944 /* change the current log to the next block addr in advance */
945 if (next_segno != segno) {
946 curseg->next_segno = next_segno;
947 change_curseg(sbi, type, true);
948 }
949 curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, next_blkaddr) &
950 (sbi->blocks_per_seg - 1);
951
952 /* rewrite node page */
953 set_page_writeback(page);
954 submit_write_page(sbi, page, new_blkaddr, NODE);
955 f2fs_submit_bio(sbi, NODE, true);
956 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
957
958 locate_dirty_segment(sbi, old_cursegno);
959 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
960
961 mutex_unlock(&sit_i->sentry_lock);
962 mutex_unlock(&curseg->curseg_mutex);
963}
964
965static int read_compacted_summaries(struct f2fs_sb_info *sbi)
966{
967 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
968 struct curseg_info *seg_i;
969 unsigned char *kaddr;
970 struct page *page;
971 block_t start;
972 int i, j, offset;
973
974 start = start_sum_block(sbi);
975
976 page = get_meta_page(sbi, start++);
977 kaddr = (unsigned char *)page_address(page);
978
979 /* Step 1: restore nat cache */
980 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
981 memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
982
983 /* Step 2: restore sit cache */
984 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
985 memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
986 SUM_JOURNAL_SIZE);
987 offset = 2 * SUM_JOURNAL_SIZE;
988
989 /* Step 3: restore summary entries */
990 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
991 unsigned short blk_off;
992 unsigned int segno;
993
994 seg_i = CURSEG_I(sbi, i);
995 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
996 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
997 seg_i->next_segno = segno;
998 reset_curseg(sbi, i, 0);
999 seg_i->alloc_type = ckpt->alloc_type[i];
1000 seg_i->next_blkoff = blk_off;
1001
1002 if (seg_i->alloc_type == SSR)
1003 blk_off = sbi->blocks_per_seg;
1004
1005 for (j = 0; j < blk_off; j++) {
1006 struct f2fs_summary *s;
1007 s = (struct f2fs_summary *)(kaddr + offset);
1008 seg_i->sum_blk->entries[j] = *s;
1009 offset += SUMMARY_SIZE;
1010 if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1011 SUM_FOOTER_SIZE)
1012 continue;
1013
1014 f2fs_put_page(page, 1);
1015 page = NULL;
1016
1017 page = get_meta_page(sbi, start++);
1018 kaddr = (unsigned char *)page_address(page);
1019 offset = 0;
1020 }
1021 }
1022 f2fs_put_page(page, 1);
1023 return 0;
1024}
1025
1026static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
1027{
1028 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1029 struct f2fs_summary_block *sum;
1030 struct curseg_info *curseg;
1031 struct page *new;
1032 unsigned short blk_off;
1033 unsigned int segno = 0;
1034 block_t blk_addr = 0;
1035
1036 /* get segment number and block addr */
1037 if (IS_DATASEG(type)) {
1038 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
1039 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
1040 CURSEG_HOT_DATA]);
25ca923b 1041 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
351df4b2
JK
1042 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
1043 else
1044 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
1045 } else {
1046 segno = le32_to_cpu(ckpt->cur_node_segno[type -
1047 CURSEG_HOT_NODE]);
1048 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
1049 CURSEG_HOT_NODE]);
25ca923b 1050 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
351df4b2
JK
1051 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
1052 type - CURSEG_HOT_NODE);
1053 else
1054 blk_addr = GET_SUM_BLOCK(sbi, segno);
1055 }
1056
1057 new = get_meta_page(sbi, blk_addr);
1058 sum = (struct f2fs_summary_block *)page_address(new);
1059
1060 if (IS_NODESEG(type)) {
25ca923b 1061 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG)) {
351df4b2
JK
1062 struct f2fs_summary *ns = &sum->entries[0];
1063 int i;
1064 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
1065 ns->version = 0;
1066 ns->ofs_in_node = 0;
1067 }
1068 } else {
1069 if (restore_node_summary(sbi, segno, sum)) {
1070 f2fs_put_page(new, 1);
1071 return -EINVAL;
1072 }
1073 }
1074 }
1075
1076 /* set uncompleted segment to curseg */
1077 curseg = CURSEG_I(sbi, type);
1078 mutex_lock(&curseg->curseg_mutex);
1079 memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
1080 curseg->next_segno = segno;
1081 reset_curseg(sbi, type, 0);
1082 curseg->alloc_type = ckpt->alloc_type[type];
1083 curseg->next_blkoff = blk_off;
1084 mutex_unlock(&curseg->curseg_mutex);
1085 f2fs_put_page(new, 1);
1086 return 0;
1087}
1088
1089static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
1090{
1091 int type = CURSEG_HOT_DATA;
1092
25ca923b 1093 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
351df4b2
JK
1094 /* restore for compacted data summary */
1095 if (read_compacted_summaries(sbi))
1096 return -EINVAL;
1097 type = CURSEG_HOT_NODE;
1098 }
1099
1100 for (; type <= CURSEG_COLD_NODE; type++)
1101 if (read_normal_summaries(sbi, type))
1102 return -EINVAL;
1103 return 0;
1104}
1105
1106static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
1107{
1108 struct page *page;
1109 unsigned char *kaddr;
1110 struct f2fs_summary *summary;
1111 struct curseg_info *seg_i;
1112 int written_size = 0;
1113 int i, j;
1114
1115 page = grab_meta_page(sbi, blkaddr++);
1116 kaddr = (unsigned char *)page_address(page);
1117
1118 /* Step 1: write nat cache */
1119 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1120 memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
1121 written_size += SUM_JOURNAL_SIZE;
1122
1123 /* Step 2: write sit cache */
1124 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1125 memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
1126 SUM_JOURNAL_SIZE);
1127 written_size += SUM_JOURNAL_SIZE;
1128
1129 set_page_dirty(page);
1130
1131 /* Step 3: write summary entries */
1132 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1133 unsigned short blkoff;
1134 seg_i = CURSEG_I(sbi, i);
1135 if (sbi->ckpt->alloc_type[i] == SSR)
1136 blkoff = sbi->blocks_per_seg;
1137 else
1138 blkoff = curseg_blkoff(sbi, i);
1139
1140 for (j = 0; j < blkoff; j++) {
1141 if (!page) {
1142 page = grab_meta_page(sbi, blkaddr++);
1143 kaddr = (unsigned char *)page_address(page);
1144 written_size = 0;
1145 }
1146 summary = (struct f2fs_summary *)(kaddr + written_size);
1147 *summary = seg_i->sum_blk->entries[j];
1148 written_size += SUMMARY_SIZE;
1149 set_page_dirty(page);
1150
1151 if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1152 SUM_FOOTER_SIZE)
1153 continue;
1154
1155 f2fs_put_page(page, 1);
1156 page = NULL;
1157 }
1158 }
1159 if (page)
1160 f2fs_put_page(page, 1);
1161}
1162
1163static void write_normal_summaries(struct f2fs_sb_info *sbi,
1164 block_t blkaddr, int type)
1165{
1166 int i, end;
1167 if (IS_DATASEG(type))
1168 end = type + NR_CURSEG_DATA_TYPE;
1169 else
1170 end = type + NR_CURSEG_NODE_TYPE;
1171
1172 for (i = type; i < end; i++) {
1173 struct curseg_info *sum = CURSEG_I(sbi, i);
1174 mutex_lock(&sum->curseg_mutex);
1175 write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
1176 mutex_unlock(&sum->curseg_mutex);
1177 }
1178}
1179
1180void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1181{
25ca923b 1182 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
351df4b2
JK
1183 write_compacted_summaries(sbi, start_blk);
1184 else
1185 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
1186}
1187
1188void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1189{
25ca923b 1190 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG))
351df4b2 1191 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
351df4b2
JK
1192}
1193
1194int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
1195 unsigned int val, int alloc)
1196{
1197 int i;
1198
1199 if (type == NAT_JOURNAL) {
1200 for (i = 0; i < nats_in_cursum(sum); i++) {
1201 if (le32_to_cpu(nid_in_journal(sum, i)) == val)
1202 return i;
1203 }
1204 if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
1205 return update_nats_in_cursum(sum, 1);
1206 } else if (type == SIT_JOURNAL) {
1207 for (i = 0; i < sits_in_cursum(sum); i++)
1208 if (le32_to_cpu(segno_in_journal(sum, i)) == val)
1209 return i;
1210 if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
1211 return update_sits_in_cursum(sum, 1);
1212 }
1213 return -1;
1214}
1215
1216static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
1217 unsigned int segno)
1218{
1219 struct sit_info *sit_i = SIT_I(sbi);
1220 unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno);
1221 block_t blk_addr = sit_i->sit_base_addr + offset;
1222
1223 check_seg_range(sbi, segno);
1224
1225 /* calculate sit block address */
1226 if (f2fs_test_bit(offset, sit_i->sit_bitmap))
1227 blk_addr += sit_i->sit_blocks;
1228
1229 return get_meta_page(sbi, blk_addr);
1230}
1231
1232static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
1233 unsigned int start)
1234{
1235 struct sit_info *sit_i = SIT_I(sbi);
1236 struct page *src_page, *dst_page;
1237 pgoff_t src_off, dst_off;
1238 void *src_addr, *dst_addr;
1239
1240 src_off = current_sit_addr(sbi, start);
1241 dst_off = next_sit_addr(sbi, src_off);
1242
1243 /* get current sit block page without lock */
1244 src_page = get_meta_page(sbi, src_off);
1245 dst_page = grab_meta_page(sbi, dst_off);
1246 BUG_ON(PageDirty(src_page));
1247
1248 src_addr = page_address(src_page);
1249 dst_addr = page_address(dst_page);
1250 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
1251
1252 set_page_dirty(dst_page);
1253 f2fs_put_page(src_page, 1);
1254
1255 set_to_next_sit(sit_i, start);
1256
1257 return dst_page;
1258}
1259
1260static bool flush_sits_in_journal(struct f2fs_sb_info *sbi)
1261{
1262 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1263 struct f2fs_summary_block *sum = curseg->sum_blk;
1264 int i;
1265
1266 /*
1267 * If the journal area in the current summary is full of sit entries,
1268 * all the sit entries will be flushed. Otherwise the sit entries
1269 * are not able to replace with newly hot sit entries.
1270 */
1271 if (sits_in_cursum(sum) >= SIT_JOURNAL_ENTRIES) {
1272 for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
1273 unsigned int segno;
1274 segno = le32_to_cpu(segno_in_journal(sum, i));
1275 __mark_sit_entry_dirty(sbi, segno);
1276 }
1277 update_sits_in_cursum(sum, -sits_in_cursum(sum));
1278 return 1;
1279 }
1280 return 0;
1281}
1282
0a8165d7 1283/*
351df4b2
JK
1284 * CP calls this function, which flushes SIT entries including sit_journal,
1285 * and moves prefree segs to free segs.
1286 */
1287void flush_sit_entries(struct f2fs_sb_info *sbi)
1288{
1289 struct sit_info *sit_i = SIT_I(sbi);
1290 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
1291 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1292 struct f2fs_summary_block *sum = curseg->sum_blk;
1293 unsigned long nsegs = TOTAL_SEGS(sbi);
1294 struct page *page = NULL;
1295 struct f2fs_sit_block *raw_sit = NULL;
1296 unsigned int start = 0, end = 0;
1297 unsigned int segno = -1;
1298 bool flushed;
1299
1300 mutex_lock(&curseg->curseg_mutex);
1301 mutex_lock(&sit_i->sentry_lock);
1302
1303 /*
1304 * "flushed" indicates whether sit entries in journal are flushed
1305 * to the SIT area or not.
1306 */
1307 flushed = flush_sits_in_journal(sbi);
1308
1309 while ((segno = find_next_bit(bitmap, nsegs, segno + 1)) < nsegs) {
1310 struct seg_entry *se = get_seg_entry(sbi, segno);
1311 int sit_offset, offset;
1312
1313 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
1314
1315 if (flushed)
1316 goto to_sit_page;
1317
1318 offset = lookup_journal_in_cursum(sum, SIT_JOURNAL, segno, 1);
1319 if (offset >= 0) {
1320 segno_in_journal(sum, offset) = cpu_to_le32(segno);
1321 seg_info_to_raw_sit(se, &sit_in_journal(sum, offset));
1322 goto flush_done;
1323 }
1324to_sit_page:
1325 if (!page || (start > segno) || (segno > end)) {
1326 if (page) {
1327 f2fs_put_page(page, 1);
1328 page = NULL;
1329 }
1330
1331 start = START_SEGNO(sit_i, segno);
1332 end = start + SIT_ENTRY_PER_BLOCK - 1;
1333
1334 /* read sit block that will be updated */
1335 page = get_next_sit_page(sbi, start);
1336 raw_sit = page_address(page);
1337 }
1338
1339 /* udpate entry in SIT block */
1340 seg_info_to_raw_sit(se, &raw_sit->entries[sit_offset]);
1341flush_done:
1342 __clear_bit(segno, bitmap);
1343 sit_i->dirty_sentries--;
1344 }
1345 mutex_unlock(&sit_i->sentry_lock);
1346 mutex_unlock(&curseg->curseg_mutex);
1347
1348 /* writeout last modified SIT block */
1349 f2fs_put_page(page, 1);
1350
1351 set_prefree_as_free_segments(sbi);
1352}
1353
1354static int build_sit_info(struct f2fs_sb_info *sbi)
1355{
1356 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1357 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1358 struct sit_info *sit_i;
1359 unsigned int sit_segs, start;
1360 char *src_bitmap, *dst_bitmap;
1361 unsigned int bitmap_size;
1362
1363 /* allocate memory for SIT information */
1364 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
1365 if (!sit_i)
1366 return -ENOMEM;
1367
1368 SM_I(sbi)->sit_info = sit_i;
1369
1370 sit_i->sentries = vzalloc(TOTAL_SEGS(sbi) * sizeof(struct seg_entry));
1371 if (!sit_i->sentries)
1372 return -ENOMEM;
1373
1374 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1375 sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1376 if (!sit_i->dirty_sentries_bitmap)
1377 return -ENOMEM;
1378
1379 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1380 sit_i->sentries[start].cur_valid_map
1381 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1382 sit_i->sentries[start].ckpt_valid_map
1383 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1384 if (!sit_i->sentries[start].cur_valid_map
1385 || !sit_i->sentries[start].ckpt_valid_map)
1386 return -ENOMEM;
1387 }
1388
1389 if (sbi->segs_per_sec > 1) {
53cf9522 1390 sit_i->sec_entries = vzalloc(TOTAL_SECS(sbi) *
351df4b2
JK
1391 sizeof(struct sec_entry));
1392 if (!sit_i->sec_entries)
1393 return -ENOMEM;
1394 }
1395
1396 /* get information related with SIT */
1397 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
1398
1399 /* setup SIT bitmap from ckeckpoint pack */
1400 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
1401 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
1402
79b5793b 1403 dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
351df4b2
JK
1404 if (!dst_bitmap)
1405 return -ENOMEM;
351df4b2
JK
1406
1407 /* init SIT information */
1408 sit_i->s_ops = &default_salloc_ops;
1409
1410 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
1411 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
1412 sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
1413 sit_i->sit_bitmap = dst_bitmap;
1414 sit_i->bitmap_size = bitmap_size;
1415 sit_i->dirty_sentries = 0;
1416 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
1417 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
1418 sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
1419 mutex_init(&sit_i->sentry_lock);
1420 return 0;
1421}
1422
1423static int build_free_segmap(struct f2fs_sb_info *sbi)
1424{
1425 struct f2fs_sm_info *sm_info = SM_I(sbi);
1426 struct free_segmap_info *free_i;
1427 unsigned int bitmap_size, sec_bitmap_size;
1428
1429 /* allocate memory for free segmap information */
1430 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
1431 if (!free_i)
1432 return -ENOMEM;
1433
1434 SM_I(sbi)->free_info = free_i;
1435
1436 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1437 free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL);
1438 if (!free_i->free_segmap)
1439 return -ENOMEM;
1440
53cf9522 1441 sec_bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
351df4b2
JK
1442 free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL);
1443 if (!free_i->free_secmap)
1444 return -ENOMEM;
1445
1446 /* set all segments as dirty temporarily */
1447 memset(free_i->free_segmap, 0xff, bitmap_size);
1448 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
1449
1450 /* init free segmap information */
1451 free_i->start_segno =
1452 (unsigned int) GET_SEGNO_FROM_SEG0(sbi, sm_info->main_blkaddr);
1453 free_i->free_segments = 0;
1454 free_i->free_sections = 0;
1455 rwlock_init(&free_i->segmap_lock);
1456 return 0;
1457}
1458
1459static int build_curseg(struct f2fs_sb_info *sbi)
1460{
1042d60f 1461 struct curseg_info *array;
351df4b2
JK
1462 int i;
1463
1464 array = kzalloc(sizeof(*array) * NR_CURSEG_TYPE, GFP_KERNEL);
1465 if (!array)
1466 return -ENOMEM;
1467
1468 SM_I(sbi)->curseg_array = array;
1469
1470 for (i = 0; i < NR_CURSEG_TYPE; i++) {
1471 mutex_init(&array[i].curseg_mutex);
1472 array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
1473 if (!array[i].sum_blk)
1474 return -ENOMEM;
1475 array[i].segno = NULL_SEGNO;
1476 array[i].next_blkoff = 0;
1477 }
1478 return restore_curseg_summaries(sbi);
1479}
1480
1481static void build_sit_entries(struct f2fs_sb_info *sbi)
1482{
1483 struct sit_info *sit_i = SIT_I(sbi);
1484 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1485 struct f2fs_summary_block *sum = curseg->sum_blk;
1486 unsigned int start;
1487
1488 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1489 struct seg_entry *se = &sit_i->sentries[start];
1490 struct f2fs_sit_block *sit_blk;
1491 struct f2fs_sit_entry sit;
1492 struct page *page;
1493 int i;
1494
1495 mutex_lock(&curseg->curseg_mutex);
1496 for (i = 0; i < sits_in_cursum(sum); i++) {
1497 if (le32_to_cpu(segno_in_journal(sum, i)) == start) {
1498 sit = sit_in_journal(sum, i);
1499 mutex_unlock(&curseg->curseg_mutex);
1500 goto got_it;
1501 }
1502 }
1503 mutex_unlock(&curseg->curseg_mutex);
1504 page = get_current_sit_page(sbi, start);
1505 sit_blk = (struct f2fs_sit_block *)page_address(page);
1506 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
1507 f2fs_put_page(page, 1);
1508got_it:
1509 check_block_count(sbi, start, &sit);
1510 seg_info_from_raw_sit(se, &sit);
1511 if (sbi->segs_per_sec > 1) {
1512 struct sec_entry *e = get_sec_entry(sbi, start);
1513 e->valid_blocks += se->valid_blocks;
1514 }
1515 }
1516}
1517
1518static void init_free_segmap(struct f2fs_sb_info *sbi)
1519{
1520 unsigned int start;
1521 int type;
1522
1523 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1524 struct seg_entry *sentry = get_seg_entry(sbi, start);
1525 if (!sentry->valid_blocks)
1526 __set_free(sbi, start);
1527 }
1528
1529 /* set use the current segments */
1530 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
1531 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
1532 __set_test_and_inuse(sbi, curseg_t->segno);
1533 }
1534}
1535
1536static void init_dirty_segmap(struct f2fs_sb_info *sbi)
1537{
1538 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1539 struct free_segmap_info *free_i = FREE_I(sbi);
8736fbf0 1540 unsigned int segno = 0, offset = 0, total_segs = TOTAL_SEGS(sbi);
351df4b2
JK
1541 unsigned short valid_blocks;
1542
8736fbf0 1543 while (1) {
351df4b2 1544 /* find dirty segment based on free segmap */
8736fbf0
NJ
1545 segno = find_next_inuse(free_i, total_segs, offset);
1546 if (segno >= total_segs)
351df4b2
JK
1547 break;
1548 offset = segno + 1;
1549 valid_blocks = get_valid_blocks(sbi, segno, 0);
1550 if (valid_blocks >= sbi->blocks_per_seg || !valid_blocks)
1551 continue;
1552 mutex_lock(&dirty_i->seglist_lock);
1553 __locate_dirty_segment(sbi, segno, DIRTY);
1554 mutex_unlock(&dirty_i->seglist_lock);
1555 }
1556}
1557
5ec4e49f 1558static int init_victim_secmap(struct f2fs_sb_info *sbi)
351df4b2
JK
1559{
1560 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
5ec4e49f 1561 unsigned int bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
351df4b2 1562
5ec4e49f
JK
1563 dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL);
1564 if (!dirty_i->victim_secmap)
351df4b2
JK
1565 return -ENOMEM;
1566 return 0;
1567}
1568
1569static int build_dirty_segmap(struct f2fs_sb_info *sbi)
1570{
1571 struct dirty_seglist_info *dirty_i;
1572 unsigned int bitmap_size, i;
1573
1574 /* allocate memory for dirty segments list information */
1575 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
1576 if (!dirty_i)
1577 return -ENOMEM;
1578
1579 SM_I(sbi)->dirty_info = dirty_i;
1580 mutex_init(&dirty_i->seglist_lock);
1581
1582 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1583
1584 for (i = 0; i < NR_DIRTY_TYPE; i++) {
1585 dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
351df4b2
JK
1586 if (!dirty_i->dirty_segmap[i])
1587 return -ENOMEM;
1588 }
1589
1590 init_dirty_segmap(sbi);
5ec4e49f 1591 return init_victim_secmap(sbi);
351df4b2
JK
1592}
1593
0a8165d7 1594/*
351df4b2
JK
1595 * Update min, max modified time for cost-benefit GC algorithm
1596 */
1597static void init_min_max_mtime(struct f2fs_sb_info *sbi)
1598{
1599 struct sit_info *sit_i = SIT_I(sbi);
1600 unsigned int segno;
1601
1602 mutex_lock(&sit_i->sentry_lock);
1603
1604 sit_i->min_mtime = LLONG_MAX;
1605
1606 for (segno = 0; segno < TOTAL_SEGS(sbi); segno += sbi->segs_per_sec) {
1607 unsigned int i;
1608 unsigned long long mtime = 0;
1609
1610 for (i = 0; i < sbi->segs_per_sec; i++)
1611 mtime += get_seg_entry(sbi, segno + i)->mtime;
1612
1613 mtime = div_u64(mtime, sbi->segs_per_sec);
1614
1615 if (sit_i->min_mtime > mtime)
1616 sit_i->min_mtime = mtime;
1617 }
1618 sit_i->max_mtime = get_mtime(sbi);
1619 mutex_unlock(&sit_i->sentry_lock);
1620}
1621
1622int build_segment_manager(struct f2fs_sb_info *sbi)
1623{
1624 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1625 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1042d60f 1626 struct f2fs_sm_info *sm_info;
351df4b2
JK
1627 int err;
1628
1629 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
1630 if (!sm_info)
1631 return -ENOMEM;
1632
1633 /* init sm info */
1634 sbi->sm_info = sm_info;
1635 INIT_LIST_HEAD(&sm_info->wblist_head);
1636 spin_lock_init(&sm_info->wblist_lock);
1637 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
1638 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
1639 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
1640 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
1641 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
1642 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
1643 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
1644
1645 err = build_sit_info(sbi);
1646 if (err)
1647 return err;
1648 err = build_free_segmap(sbi);
1649 if (err)
1650 return err;
1651 err = build_curseg(sbi);
1652 if (err)
1653 return err;
1654
1655 /* reinit free segmap based on SIT */
1656 build_sit_entries(sbi);
1657
1658 init_free_segmap(sbi);
1659 err = build_dirty_segmap(sbi);
1660 if (err)
1661 return err;
1662
1663 init_min_max_mtime(sbi);
1664 return 0;
1665}
1666
1667static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
1668 enum dirty_type dirty_type)
1669{
1670 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1671
1672 mutex_lock(&dirty_i->seglist_lock);
1673 kfree(dirty_i->dirty_segmap[dirty_type]);
1674 dirty_i->nr_dirty[dirty_type] = 0;
1675 mutex_unlock(&dirty_i->seglist_lock);
1676}
1677
5ec4e49f 1678static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
351df4b2
JK
1679{
1680 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
5ec4e49f 1681 kfree(dirty_i->victim_secmap);
351df4b2
JK
1682}
1683
1684static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
1685{
1686 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1687 int i;
1688
1689 if (!dirty_i)
1690 return;
1691
1692 /* discard pre-free/dirty segments list */
1693 for (i = 0; i < NR_DIRTY_TYPE; i++)
1694 discard_dirty_segmap(sbi, i);
1695
5ec4e49f 1696 destroy_victim_secmap(sbi);
351df4b2
JK
1697 SM_I(sbi)->dirty_info = NULL;
1698 kfree(dirty_i);
1699}
1700
1701static void destroy_curseg(struct f2fs_sb_info *sbi)
1702{
1703 struct curseg_info *array = SM_I(sbi)->curseg_array;
1704 int i;
1705
1706 if (!array)
1707 return;
1708 SM_I(sbi)->curseg_array = NULL;
1709 for (i = 0; i < NR_CURSEG_TYPE; i++)
1710 kfree(array[i].sum_blk);
1711 kfree(array);
1712}
1713
1714static void destroy_free_segmap(struct f2fs_sb_info *sbi)
1715{
1716 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
1717 if (!free_i)
1718 return;
1719 SM_I(sbi)->free_info = NULL;
1720 kfree(free_i->free_segmap);
1721 kfree(free_i->free_secmap);
1722 kfree(free_i);
1723}
1724
1725static void destroy_sit_info(struct f2fs_sb_info *sbi)
1726{
1727 struct sit_info *sit_i = SIT_I(sbi);
1728 unsigned int start;
1729
1730 if (!sit_i)
1731 return;
1732
1733 if (sit_i->sentries) {
1734 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1735 kfree(sit_i->sentries[start].cur_valid_map);
1736 kfree(sit_i->sentries[start].ckpt_valid_map);
1737 }
1738 }
1739 vfree(sit_i->sentries);
1740 vfree(sit_i->sec_entries);
1741 kfree(sit_i->dirty_sentries_bitmap);
1742
1743 SM_I(sbi)->sit_info = NULL;
1744 kfree(sit_i->sit_bitmap);
1745 kfree(sit_i);
1746}
1747
1748void destroy_segment_manager(struct f2fs_sb_info *sbi)
1749{
1750 struct f2fs_sm_info *sm_info = SM_I(sbi);
1751 destroy_dirty_segmap(sbi);
1752 destroy_curseg(sbi);
1753 destroy_free_segmap(sbi);
1754 destroy_sit_info(sbi);
1755 sbi->sm_info = NULL;
1756 kfree(sm_info);
1757}
This page took 0.261454 seconds and 5 git commands to generate.