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