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