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