blob: 42607a679923356c314d4b77399ff3e4b6cf8320 [file] [log] [blame]
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002 * 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>
Geert Uytterhoeven690e4a32012-12-19 22:19:30 +010015#include <linux/prefetch.h>
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +090016#include <linux/kthread.h>
Jaegeuk Kim351df4b2012-11-02 17:09:16 +090017#include <linux/vmalloc.h>
Chao Yu74de5932013-11-22 09:09:59 +080018#include <linux/swap.h>
Jaegeuk Kim351df4b2012-11-02 17:09:16 +090019
20#include "f2fs.h"
21#include "segment.h"
22#include "node.h"
Namjae Jeon6ec178d2013-04-23 17:51:43 +090023#include <trace/events/f2fs.h>
Jaegeuk Kim351df4b2012-11-02 17:09:16 +090024
Changman Lee9a7f1432013-11-15 10:42:51 +090025#define __reverse_ffz(x) __reverse_ffs(~(x))
26
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +090027static struct kmem_cache *discard_entry_slab;
Chao Yu184a5cd2014-09-04 18:13:01 +080028static struct kmem_cache *sit_entry_set_slab;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -070029static struct kmem_cache *inmem_entry_slab;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +090030
Changman Lee9a7f1432013-11-15 10:42:51 +090031/*
32 * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since
33 * MSB and LSB are reversed in a byte by f2fs_set_bit.
34 */
35static inline unsigned long __reverse_ffs(unsigned long word)
36{
37 int num = 0;
38
39#if BITS_PER_LONG == 64
40 if ((word & 0xffffffff) == 0) {
41 num += 32;
42 word >>= 32;
43 }
44#endif
45 if ((word & 0xffff) == 0) {
46 num += 16;
47 word >>= 16;
48 }
49 if ((word & 0xff) == 0) {
50 num += 8;
51 word >>= 8;
52 }
53 if ((word & 0xf0) == 0)
54 num += 4;
55 else
56 word >>= 4;
57 if ((word & 0xc) == 0)
58 num += 2;
59 else
60 word >>= 2;
61 if ((word & 0x2) == 0)
62 num += 1;
63 return num;
64}
65
66/*
arter97e1c42042014-08-06 23:22:50 +090067 * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c because
Changman Lee9a7f1432013-11-15 10:42:51 +090068 * f2fs_set_bit makes MSB and LSB reversed in a byte.
69 * Example:
70 * LSB <--> MSB
71 * f2fs_set_bit(0, bitmap) => 0000 0001
72 * f2fs_set_bit(7, bitmap) => 1000 0000
73 */
74static unsigned long __find_rev_next_bit(const unsigned long *addr,
75 unsigned long size, unsigned long offset)
76{
77 const unsigned long *p = addr + BIT_WORD(offset);
78 unsigned long result = offset & ~(BITS_PER_LONG - 1);
79 unsigned long tmp;
80 unsigned long mask, submask;
81 unsigned long quot, rest;
82
83 if (offset >= size)
84 return size;
85
86 size -= result;
87 offset %= BITS_PER_LONG;
88 if (!offset)
89 goto aligned;
90
91 tmp = *(p++);
92 quot = (offset >> 3) << 3;
93 rest = offset & 0x7;
94 mask = ~0UL << quot;
95 submask = (unsigned char)(0xff << rest) >> rest;
96 submask <<= quot;
97 mask &= submask;
98 tmp &= mask;
99 if (size < BITS_PER_LONG)
100 goto found_first;
101 if (tmp)
102 goto found_middle;
103
104 size -= BITS_PER_LONG;
105 result += BITS_PER_LONG;
106aligned:
107 while (size & ~(BITS_PER_LONG-1)) {
108 tmp = *(p++);
109 if (tmp)
110 goto found_middle;
111 result += BITS_PER_LONG;
112 size -= BITS_PER_LONG;
113 }
114 if (!size)
115 return result;
116 tmp = *p;
117found_first:
118 tmp &= (~0UL >> (BITS_PER_LONG - size));
119 if (tmp == 0UL) /* Are any bits set? */
120 return result + size; /* Nope. */
121found_middle:
122 return result + __reverse_ffs(tmp);
123}
124
125static unsigned long __find_rev_next_zero_bit(const unsigned long *addr,
126 unsigned long size, unsigned long offset)
127{
128 const unsigned long *p = addr + BIT_WORD(offset);
129 unsigned long result = offset & ~(BITS_PER_LONG - 1);
130 unsigned long tmp;
131 unsigned long mask, submask;
132 unsigned long quot, rest;
133
134 if (offset >= size)
135 return size;
136
137 size -= result;
138 offset %= BITS_PER_LONG;
139 if (!offset)
140 goto aligned;
141
142 tmp = *(p++);
143 quot = (offset >> 3) << 3;
144 rest = offset & 0x7;
145 mask = ~(~0UL << quot);
146 submask = (unsigned char)~((unsigned char)(0xff << rest) >> rest);
147 submask <<= quot;
148 mask += submask;
149 tmp |= mask;
150 if (size < BITS_PER_LONG)
151 goto found_first;
152 if (~tmp)
153 goto found_middle;
154
155 size -= BITS_PER_LONG;
156 result += BITS_PER_LONG;
157aligned:
158 while (size & ~(BITS_PER_LONG - 1)) {
159 tmp = *(p++);
160 if (~tmp)
161 goto found_middle;
162 result += BITS_PER_LONG;
163 size -= BITS_PER_LONG;
164 }
165 if (!size)
166 return result;
167 tmp = *p;
168
169found_first:
170 tmp |= ~0UL << size;
171 if (tmp == ~0UL) /* Are any bits zero? */
172 return result + size; /* Nope. */
173found_middle:
174 return result + __reverse_ffz(tmp);
175}
176
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700177void register_inmem_page(struct inode *inode, struct page *page)
178{
179 struct f2fs_inode_info *fi = F2FS_I(inode);
180 struct inmem_pages *new;
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -0700181 int err;
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800182
Jaegeuk Kim0722b102014-12-05 11:58:02 -0800183 SetPagePrivate(page);
184
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700185 new = f2fs_kmem_cache_alloc(inmem_entry_slab, GFP_NOFS);
186
187 /* add atomic page indices to the list */
188 new->page = page;
189 INIT_LIST_HEAD(&new->list);
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800190retry:
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700191 /* increase reference count with clean state */
192 mutex_lock(&fi->inmem_lock);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -0700193 err = radix_tree_insert(&fi->inmem_root, page->index, new);
194 if (err == -EEXIST) {
195 mutex_unlock(&fi->inmem_lock);
196 kmem_cache_free(inmem_entry_slab, new);
197 return;
198 } else if (err) {
199 mutex_unlock(&fi->inmem_lock);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -0700200 goto retry;
201 }
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700202 get_page(page);
203 list_add_tail(&new->list, &fi->inmem_pages);
Jaegeuk Kim8dcf2ff72014-12-05 17:18:15 -0800204 inc_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700205 mutex_unlock(&fi->inmem_lock);
206}
207
Jaegeuk Kimcbcb2872014-10-09 13:39:06 -0700208void invalidate_inmem_page(struct inode *inode, struct page *page)
209{
210 struct f2fs_inode_info *fi = F2FS_I(inode);
211 struct inmem_pages *cur;
212
213 mutex_lock(&fi->inmem_lock);
214 cur = radix_tree_lookup(&fi->inmem_root, page->index);
215 if (cur) {
216 radix_tree_delete(&fi->inmem_root, cur->page->index);
217 f2fs_put_page(cur->page, 0);
218 list_del(&cur->list);
219 kmem_cache_free(inmem_entry_slab, cur);
Jaegeuk Kim8dcf2ff72014-12-05 17:18:15 -0800220 dec_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
Jaegeuk Kimcbcb2872014-10-09 13:39:06 -0700221 }
222 mutex_unlock(&fi->inmem_lock);
223}
224
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700225void commit_inmem_pages(struct inode *inode, bool abort)
226{
227 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
228 struct f2fs_inode_info *fi = F2FS_I(inode);
229 struct inmem_pages *cur, *tmp;
230 bool submit_bio = false;
231 struct f2fs_io_info fio = {
232 .type = DATA,
233 .rw = WRITE_SYNC,
234 };
235
Jaegeuk Kim03418452014-11-21 16:37:40 -0800236 /*
237 * The abort is true only when f2fs_evict_inode is called.
238 * Basically, the f2fs_evict_inode doesn't produce any data writes, so
239 * that we don't need to call f2fs_balance_fs.
240 * Otherwise, f2fs_gc in f2fs_balance_fs can wait forever until this
241 * inode becomes free by iget_locked in f2fs_iget.
242 */
243 if (!abort)
244 f2fs_balance_fs(sbi);
245
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700246 f2fs_lock_op(sbi);
247
248 mutex_lock(&fi->inmem_lock);
249 list_for_each_entry_safe(cur, tmp, &fi->inmem_pages, list) {
250 lock_page(cur->page);
251 if (!abort && cur->page->mapping == inode->i_mapping) {
252 f2fs_wait_on_page_writeback(cur->page, DATA);
253 if (clear_page_dirty_for_io(cur->page))
254 inode_dec_dirty_pages(inode);
255 do_write_data_page(cur->page, &fio);
256 submit_bio = true;
257 }
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -0700258 radix_tree_delete(&fi->inmem_root, cur->page->index);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700259 f2fs_put_page(cur->page, 1);
260 list_del(&cur->list);
261 kmem_cache_free(inmem_entry_slab, cur);
Jaegeuk Kim8dcf2ff72014-12-05 17:18:15 -0800262 dec_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700263 }
264 if (submit_bio)
265 f2fs_submit_merged_bio(sbi, DATA, WRITE);
266 mutex_unlock(&fi->inmem_lock);
267
268 filemap_fdatawait_range(inode->i_mapping, 0, LLONG_MAX);
269 f2fs_unlock_op(sbi);
270}
271
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900272/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900273 * This function balances dirty node and dentry pages.
274 * In addition, it controls garbage collection.
275 */
276void f2fs_balance_fs(struct f2fs_sb_info *sbi)
277{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900278 /*
Jaegeuk Kim029cd282012-12-21 17:20:21 +0900279 * We should do GC or end up with checkpoint, if there are so many dirty
280 * dir/node pages without enough free segments.
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900281 */
Jaegeuk Kim43727522013-02-04 15:11:17 +0900282 if (has_not_enough_free_secs(sbi, 0)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900283 mutex_lock(&sbi->gc_mutex);
Jaegeuk Kim408e9372013-01-03 17:55:52 +0900284 f2fs_gc(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900285 }
286}
287
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900288void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi)
289{
290 /* check the # of cached NAT entries and prefree segments */
291 if (try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK) ||
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -0800292 excess_prefree_segs(sbi) ||
293 available_free_memory(sbi, INO_ENTRIES))
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900294 f2fs_sync_fs(sbi->sb, true);
295}
296
Gu Zheng2163d192014-04-27 14:21:33 +0800297static int issue_flush_thread(void *data)
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900298{
299 struct f2fs_sb_info *sbi = data;
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800300 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
301 wait_queue_head_t *q = &fcc->flush_wait_queue;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900302repeat:
303 if (kthread_should_stop())
304 return 0;
305
Gu Zheng721bd4d2014-09-05 18:31:00 +0800306 if (!llist_empty(&fcc->issue_list)) {
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900307 struct bio *bio = bio_alloc(GFP_NOIO, 0);
308 struct flush_cmd *cmd, *next;
309 int ret;
310
Gu Zheng721bd4d2014-09-05 18:31:00 +0800311 fcc->dispatch_list = llist_del_all(&fcc->issue_list);
312 fcc->dispatch_list = llist_reverse_order(fcc->dispatch_list);
313
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900314 bio->bi_bdev = sbi->sb->s_bdev;
315 ret = submit_bio_wait(WRITE_FLUSH, bio);
316
Gu Zheng721bd4d2014-09-05 18:31:00 +0800317 llist_for_each_entry_safe(cmd, next,
318 fcc->dispatch_list, llnode) {
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900319 cmd->ret = ret;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900320 complete(&cmd->wait);
321 }
Gu Zhenga4ed23f2014-04-11 17:49:35 +0800322 bio_put(bio);
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800323 fcc->dispatch_list = NULL;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900324 }
325
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800326 wait_event_interruptible(*q,
Gu Zheng721bd4d2014-09-05 18:31:00 +0800327 kthread_should_stop() || !llist_empty(&fcc->issue_list));
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900328 goto repeat;
329}
330
331int f2fs_issue_flush(struct f2fs_sb_info *sbi)
332{
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800333 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
Chao Yuadf8d902014-05-08 17:00:35 +0800334 struct flush_cmd cmd;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900335
Jaegeuk Kim24a9ee02014-07-25 17:46:10 -0700336 trace_f2fs_issue_flush(sbi->sb, test_opt(sbi, NOBARRIER),
337 test_opt(sbi, FLUSH_MERGE));
338
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700339 if (test_opt(sbi, NOBARRIER))
340 return 0;
341
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900342 if (!test_opt(sbi, FLUSH_MERGE))
343 return blkdev_issue_flush(sbi->sb->s_bdev, GFP_KERNEL, NULL);
344
Chao Yuadf8d902014-05-08 17:00:35 +0800345 init_completion(&cmd.wait);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900346
Gu Zheng721bd4d2014-09-05 18:31:00 +0800347 llist_add(&cmd.llnode, &fcc->issue_list);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900348
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800349 if (!fcc->dispatch_list)
350 wake_up(&fcc->flush_wait_queue);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900351
Chao Yuadf8d902014-05-08 17:00:35 +0800352 wait_for_completion(&cmd.wait);
353
354 return cmd.ret;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900355}
356
Gu Zheng2163d192014-04-27 14:21:33 +0800357int create_flush_cmd_control(struct f2fs_sb_info *sbi)
358{
359 dev_t dev = sbi->sb->s_bdev->bd_dev;
360 struct flush_cmd_control *fcc;
361 int err = 0;
362
363 fcc = kzalloc(sizeof(struct flush_cmd_control), GFP_KERNEL);
364 if (!fcc)
365 return -ENOMEM;
Gu Zheng2163d192014-04-27 14:21:33 +0800366 init_waitqueue_head(&fcc->flush_wait_queue);
Gu Zheng721bd4d2014-09-05 18:31:00 +0800367 init_llist_head(&fcc->issue_list);
Chao Yu6b2920a2014-07-07 11:21:59 +0800368 SM_I(sbi)->cmd_control_info = fcc;
Gu Zheng2163d192014-04-27 14:21:33 +0800369 fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi,
370 "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev));
371 if (IS_ERR(fcc->f2fs_issue_flush)) {
372 err = PTR_ERR(fcc->f2fs_issue_flush);
373 kfree(fcc);
Chao Yu6b2920a2014-07-07 11:21:59 +0800374 SM_I(sbi)->cmd_control_info = NULL;
Gu Zheng2163d192014-04-27 14:21:33 +0800375 return err;
376 }
Gu Zheng2163d192014-04-27 14:21:33 +0800377
378 return err;
379}
380
381void destroy_flush_cmd_control(struct f2fs_sb_info *sbi)
382{
Chao Yu6b2920a2014-07-07 11:21:59 +0800383 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
Gu Zheng2163d192014-04-27 14:21:33 +0800384
385 if (fcc && fcc->f2fs_issue_flush)
386 kthread_stop(fcc->f2fs_issue_flush);
387 kfree(fcc);
Chao Yu6b2920a2014-07-07 11:21:59 +0800388 SM_I(sbi)->cmd_control_info = NULL;
Gu Zheng2163d192014-04-27 14:21:33 +0800389}
390
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900391static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
392 enum dirty_type dirty_type)
393{
394 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
395
396 /* need not be added */
397 if (IS_CURSEG(sbi, segno))
398 return;
399
400 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
401 dirty_i->nr_dirty[dirty_type]++;
402
403 if (dirty_type == DIRTY) {
404 struct seg_entry *sentry = get_seg_entry(sbi, segno);
Changman Lee4625d6a2013-10-25 17:31:57 +0900405 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900406
Jaegeuk Kimec325b52014-09-02 16:24:11 -0700407 if (unlikely(t >= DIRTY)) {
408 f2fs_bug_on(sbi, 1);
409 return;
410 }
Changman Lee4625d6a2013-10-25 17:31:57 +0900411 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
412 dirty_i->nr_dirty[t]++;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900413 }
414}
415
416static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
417 enum dirty_type dirty_type)
418{
419 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
420
421 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
422 dirty_i->nr_dirty[dirty_type]--;
423
424 if (dirty_type == DIRTY) {
Changman Lee4625d6a2013-10-25 17:31:57 +0900425 struct seg_entry *sentry = get_seg_entry(sbi, segno);
426 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900427
Changman Lee4625d6a2013-10-25 17:31:57 +0900428 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
429 dirty_i->nr_dirty[t]--;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900430
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +0900431 if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0)
432 clear_bit(GET_SECNO(sbi, segno),
433 dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900434 }
435}
436
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900437/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900438 * Should not occur error such as -ENOMEM.
439 * Adding dirty entry into seglist is not critical operation.
440 * If a given segment is one of current working segments, it won't be added.
441 */
Haicheng Li8d8451a2013-06-13 16:59:28 +0800442static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900443{
444 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
445 unsigned short valid_blocks;
446
447 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
448 return;
449
450 mutex_lock(&dirty_i->seglist_lock);
451
452 valid_blocks = get_valid_blocks(sbi, segno, 0);
453
454 if (valid_blocks == 0) {
455 __locate_dirty_segment(sbi, segno, PRE);
456 __remove_dirty_segment(sbi, segno, DIRTY);
457 } else if (valid_blocks < sbi->blocks_per_seg) {
458 __locate_dirty_segment(sbi, segno, DIRTY);
459 } else {
460 /* Recovery routine with SSR needs this */
461 __remove_dirty_segment(sbi, segno, DIRTY);
462 }
463
464 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900465}
466
Jaegeuk Kim1e87a782014-04-15 13:57:55 +0900467static int f2fs_issue_discard(struct f2fs_sb_info *sbi,
Jaegeuk Kim37208872013-11-12 16:55:17 +0900468 block_t blkstart, block_t blklen)
469{
Chao Yu55cf9cb2014-09-15 18:01:10 +0800470 sector_t start = SECTOR_FROM_BLOCK(blkstart);
471 sector_t len = SECTOR_FROM_BLOCK(blklen);
Jaegeuk Kim1661d072013-11-12 17:01:00 +0900472 trace_f2fs_issue_discard(sbi->sb, blkstart, blklen);
Jaegeuk Kim1e87a782014-04-15 13:57:55 +0900473 return blkdev_issue_discard(sbi->sb->s_bdev, start, len, GFP_NOFS, 0);
474}
475
Jaegeuk Kimcf2271e2014-07-25 15:47:25 -0700476void discard_next_dnode(struct f2fs_sb_info *sbi, block_t blkaddr)
Jaegeuk Kim1e87a782014-04-15 13:57:55 +0900477{
Jaegeuk Kim1e87a782014-04-15 13:57:55 +0900478 if (f2fs_issue_discard(sbi, blkaddr, 1)) {
479 struct page *page = grab_meta_page(sbi, blkaddr);
480 /* zero-filled page */
481 set_page_dirty(page);
482 f2fs_put_page(page, 1);
483 }
Jaegeuk Kim37208872013-11-12 16:55:17 +0900484}
485
Jaegeuk Kimadf49832014-10-28 22:27:59 -0700486static void __add_discard_entry(struct f2fs_sb_info *sbi,
487 struct cp_control *cpc, unsigned int start, unsigned int end)
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900488{
489 struct list_head *head = &SM_I(sbi)->discard_list;
Jaegeuk Kimadf49832014-10-28 22:27:59 -0700490 struct discard_entry *new, *last;
491
492 if (!list_empty(head)) {
493 last = list_last_entry(head, struct discard_entry, list);
494 if (START_BLOCK(sbi, cpc->trim_start) + start ==
495 last->blkaddr + last->len) {
496 last->len += end - start;
497 goto done;
498 }
499 }
500
501 new = f2fs_kmem_cache_alloc(discard_entry_slab, GFP_NOFS);
502 INIT_LIST_HEAD(&new->list);
503 new->blkaddr = START_BLOCK(sbi, cpc->trim_start) + start;
504 new->len = end - start;
505 list_add_tail(&new->list, head);
506done:
507 SM_I(sbi)->nr_discards += end - start;
508 cpc->trimmed += end - start;
509}
510
511static void add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc)
512{
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900513 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
514 int max_blocks = sbi->blocks_per_seg;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700515 struct seg_entry *se = get_seg_entry(sbi, cpc->trim_start);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900516 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
517 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
518 unsigned long dmap[entries];
519 unsigned int start = 0, end = -1;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700520 bool force = (cpc->reason == CP_DISCARD);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900521 int i;
522
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700523 if (!force && !test_opt(sbi, DISCARD))
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900524 return;
525
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700526 if (force && !se->valid_blocks) {
527 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
528 /*
529 * if this segment is registered in the prefree list, then
530 * we should skip adding a discard candidate, and let the
531 * checkpoint do that later.
532 */
533 mutex_lock(&dirty_i->seglist_lock);
534 if (test_bit(cpc->trim_start, dirty_i->dirty_segmap[PRE])) {
535 mutex_unlock(&dirty_i->seglist_lock);
536 cpc->trimmed += sbi->blocks_per_seg;
537 return;
538 }
539 mutex_unlock(&dirty_i->seglist_lock);
540
Jaegeuk Kimadf49832014-10-28 22:27:59 -0700541 __add_discard_entry(sbi, cpc, 0, sbi->blocks_per_seg);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700542 return;
543 }
544
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900545 /* zero block will be discarded through the prefree list */
546 if (!se->valid_blocks || se->valid_blocks == max_blocks)
547 return;
548
549 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
550 for (i = 0; i < entries; i++)
Jaegeuk Kime3fb1b72014-10-27 13:54:27 -0700551 dmap[i] = ~(cur_map[i] | ckpt_map[i]);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900552
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700553 while (force || SM_I(sbi)->nr_discards <= SM_I(sbi)->max_discards) {
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900554 start = __find_rev_next_bit(dmap, max_blocks, end + 1);
555 if (start >= max_blocks)
556 break;
557
558 end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
559
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700560 if (end - start < cpc->trim_minlen)
561 continue;
562
Jaegeuk Kimadf49832014-10-28 22:27:59 -0700563 __add_discard_entry(sbi, cpc, start, end);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900564 }
565}
566
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700567void release_discard_addrs(struct f2fs_sb_info *sbi)
568{
569 struct list_head *head = &(SM_I(sbi)->discard_list);
570 struct discard_entry *entry, *this;
571
572 /* drop caches */
573 list_for_each_entry_safe(entry, this, head, list) {
574 list_del(&entry->list);
575 kmem_cache_free(discard_entry_slab, entry);
576 }
577}
578
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900579/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900580 * Should call clear_prefree_segments after checkpoint is done.
581 */
582static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
583{
584 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Chao Yub65ee142014-08-04 10:10:07 +0800585 unsigned int segno;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900586
587 mutex_lock(&dirty_i->seglist_lock);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700588 for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900589 __set_test_and_free(sbi, segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900590 mutex_unlock(&dirty_i->seglist_lock);
591}
592
593void clear_prefree_segments(struct f2fs_sb_info *sbi)
594{
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900595 struct list_head *head = &(SM_I(sbi)->discard_list);
Chao Yu2d7b8222014-03-29 11:33:17 +0800596 struct discard_entry *entry, *this;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900597 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Changman Lee29e59c12013-11-11 09:24:37 +0900598 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
Changman Lee29e59c12013-11-11 09:24:37 +0900599 unsigned int start = 0, end = -1;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900600
601 mutex_lock(&dirty_i->seglist_lock);
Changman Lee29e59c12013-11-11 09:24:37 +0900602
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900603 while (1) {
Changman Lee29e59c12013-11-11 09:24:37 +0900604 int i;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700605 start = find_next_bit(prefree_map, MAIN_SEGS(sbi), end + 1);
606 if (start >= MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900607 break;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700608 end = find_next_zero_bit(prefree_map, MAIN_SEGS(sbi),
609 start + 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900610
Changman Lee29e59c12013-11-11 09:24:37 +0900611 for (i = start; i < end; i++)
612 clear_bit(i, prefree_map);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900613
Changman Lee29e59c12013-11-11 09:24:37 +0900614 dirty_i->nr_dirty[PRE] -= end - start;
615
616 if (!test_opt(sbi, DISCARD))
617 continue;
618
Jaegeuk Kim37208872013-11-12 16:55:17 +0900619 f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
620 (end - start) << sbi->log_blocks_per_seg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900621 }
622 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900623
624 /* send small discards */
Chao Yu2d7b8222014-03-29 11:33:17 +0800625 list_for_each_entry_safe(entry, this, head, list) {
Jaegeuk Kim37208872013-11-12 16:55:17 +0900626 f2fs_issue_discard(sbi, entry->blkaddr, entry->len);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900627 list_del(&entry->list);
628 SM_I(sbi)->nr_discards -= entry->len;
629 kmem_cache_free(discard_entry_slab, entry);
630 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900631}
632
Chao Yu184a5cd2014-09-04 18:13:01 +0800633static bool __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900634{
635 struct sit_info *sit_i = SIT_I(sbi);
Chao Yu184a5cd2014-09-04 18:13:01 +0800636
637 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900638 sit_i->dirty_sentries++;
Chao Yu184a5cd2014-09-04 18:13:01 +0800639 return false;
640 }
641
642 return true;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900643}
644
645static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
646 unsigned int segno, int modified)
647{
648 struct seg_entry *se = get_seg_entry(sbi, segno);
649 se->type = type;
650 if (modified)
651 __mark_sit_entry_dirty(sbi, segno);
652}
653
654static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
655{
656 struct seg_entry *se;
657 unsigned int segno, offset;
658 long int new_vblocks;
659
660 segno = GET_SEGNO(sbi, blkaddr);
661
662 se = get_seg_entry(sbi, segno);
663 new_vblocks = se->valid_blocks + del;
Jaegeuk Kim491c0852014-02-04 13:01:10 +0900664 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900665
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700666 f2fs_bug_on(sbi, (new_vblocks >> (sizeof(unsigned short) << 3) ||
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900667 (new_vblocks > sbi->blocks_per_seg)));
668
669 se->valid_blocks = new_vblocks;
670 se->mtime = get_mtime(sbi);
671 SIT_I(sbi)->max_mtime = se->mtime;
672
673 /* Update valid block bitmap */
674 if (del > 0) {
Gu Zheng52aca072014-10-20 17:45:51 +0800675 if (f2fs_test_and_set_bit(offset, se->cur_valid_map))
Jaegeuk Kim05796762014-09-02 16:05:00 -0700676 f2fs_bug_on(sbi, 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900677 } else {
Gu Zheng52aca072014-10-20 17:45:51 +0800678 if (!f2fs_test_and_clear_bit(offset, se->cur_valid_map))
Jaegeuk Kim05796762014-09-02 16:05:00 -0700679 f2fs_bug_on(sbi, 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900680 }
681 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
682 se->ckpt_valid_blocks += del;
683
684 __mark_sit_entry_dirty(sbi, segno);
685
686 /* update total number of valid blocks to be written in ckpt area */
687 SIT_I(sbi)->written_valid_blocks += del;
688
689 if (sbi->segs_per_sec > 1)
690 get_sec_entry(sbi, segno)->valid_blocks += del;
691}
692
Jaegeuk Kim5e443812014-01-28 12:22:14 +0900693void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900694{
Jaegeuk Kim5e443812014-01-28 12:22:14 +0900695 update_sit_entry(sbi, new, 1);
696 if (GET_SEGNO(sbi, old) != NULL_SEGNO)
697 update_sit_entry(sbi, old, -1);
698
699 locate_dirty_segment(sbi, GET_SEGNO(sbi, old));
700 locate_dirty_segment(sbi, GET_SEGNO(sbi, new));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900701}
702
703void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
704{
705 unsigned int segno = GET_SEGNO(sbi, addr);
706 struct sit_info *sit_i = SIT_I(sbi);
707
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700708 f2fs_bug_on(sbi, addr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900709 if (addr == NEW_ADDR)
710 return;
711
712 /* add it into sit main buffer */
713 mutex_lock(&sit_i->sentry_lock);
714
715 update_sit_entry(sbi, addr, -1);
716
717 /* add it into dirty seglist */
718 locate_dirty_segment(sbi, segno);
719
720 mutex_unlock(&sit_i->sentry_lock);
721}
722
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900723/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900724 * This function should be resided under the curseg_mutex lock
725 */
726static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
Haicheng Lie79efe32013-06-13 16:59:27 +0800727 struct f2fs_summary *sum)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900728{
729 struct curseg_info *curseg = CURSEG_I(sbi, type);
730 void *addr = curseg->sum_blk;
Haicheng Lie79efe32013-06-13 16:59:27 +0800731 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900732 memcpy(addr, sum, sizeof(struct f2fs_summary));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900733}
734
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900735/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900736 * Calculate the number of current summary pages for writing
737 */
738int npages_for_summary_flush(struct f2fs_sb_info *sbi)
739{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900740 int valid_sum_count = 0;
Fan Li9a479382013-10-29 16:21:47 +0800741 int i, sum_in_page;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900742
743 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
744 if (sbi->ckpt->alloc_type[i] == SSR)
745 valid_sum_count += sbi->blocks_per_seg;
746 else
747 valid_sum_count += curseg_blkoff(sbi, i);
748 }
749
Fan Li9a479382013-10-29 16:21:47 +0800750 sum_in_page = (PAGE_CACHE_SIZE - 2 * SUM_JOURNAL_SIZE -
751 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
752 if (valid_sum_count <= sum_in_page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900753 return 1;
Fan Li9a479382013-10-29 16:21:47 +0800754 else if ((valid_sum_count - sum_in_page) <=
755 (PAGE_CACHE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900756 return 2;
757 return 3;
758}
759
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900760/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900761 * Caller should put this summary page
762 */
763struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
764{
765 return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
766}
767
768static void write_sum_page(struct f2fs_sb_info *sbi,
769 struct f2fs_summary_block *sum_blk, block_t blk_addr)
770{
771 struct page *page = grab_meta_page(sbi, blk_addr);
772 void *kaddr = page_address(page);
773 memcpy(kaddr, sum_blk, PAGE_CACHE_SIZE);
774 set_page_dirty(page);
775 f2fs_put_page(page, 1);
776}
777
Jaegeuk Kim60374682013-03-31 13:58:51 +0900778static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
779{
780 struct curseg_info *curseg = CURSEG_I(sbi, type);
Haicheng Li81fb5e82013-05-14 18:20:28 +0800781 unsigned int segno = curseg->segno + 1;
Jaegeuk Kim60374682013-03-31 13:58:51 +0900782 struct free_segmap_info *free_i = FREE_I(sbi);
783
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700784 if (segno < MAIN_SEGS(sbi) && segno % sbi->segs_per_sec)
Haicheng Li81fb5e82013-05-14 18:20:28 +0800785 return !test_bit(segno, free_i->free_segmap);
Jaegeuk Kim60374682013-03-31 13:58:51 +0900786 return 0;
787}
788
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900789/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900790 * Find a new segment from the free segments bitmap to right order
791 * This function should be returned with success, otherwise BUG
792 */
793static void get_new_segment(struct f2fs_sb_info *sbi,
794 unsigned int *newseg, bool new_sec, int dir)
795{
796 struct free_segmap_info *free_i = FREE_I(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900797 unsigned int segno, secno, zoneno;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700798 unsigned int total_zones = MAIN_SECS(sbi) / sbi->secs_per_zone;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900799 unsigned int hint = *newseg / sbi->segs_per_sec;
800 unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
801 unsigned int left_start = hint;
802 bool init = true;
803 int go_left = 0;
804 int i;
805
806 write_lock(&free_i->segmap_lock);
807
808 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
809 segno = find_next_zero_bit(free_i->free_segmap,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700810 MAIN_SEGS(sbi), *newseg + 1);
Jaegeuk Kim33afa7f2013-03-31 12:59:53 +0900811 if (segno - *newseg < sbi->segs_per_sec -
812 (*newseg % sbi->segs_per_sec))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900813 goto got_it;
814 }
815find_other_zone:
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700816 secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint);
817 if (secno >= MAIN_SECS(sbi)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900818 if (dir == ALLOC_RIGHT) {
819 secno = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700820 MAIN_SECS(sbi), 0);
821 f2fs_bug_on(sbi, secno >= MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900822 } else {
823 go_left = 1;
824 left_start = hint - 1;
825 }
826 }
827 if (go_left == 0)
828 goto skip_left;
829
830 while (test_bit(left_start, free_i->free_secmap)) {
831 if (left_start > 0) {
832 left_start--;
833 continue;
834 }
835 left_start = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700836 MAIN_SECS(sbi), 0);
837 f2fs_bug_on(sbi, left_start >= MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900838 break;
839 }
840 secno = left_start;
841skip_left:
842 hint = secno;
843 segno = secno * sbi->segs_per_sec;
844 zoneno = secno / sbi->secs_per_zone;
845
846 /* give up on finding another zone */
847 if (!init)
848 goto got_it;
849 if (sbi->secs_per_zone == 1)
850 goto got_it;
851 if (zoneno == old_zoneno)
852 goto got_it;
853 if (dir == ALLOC_LEFT) {
854 if (!go_left && zoneno + 1 >= total_zones)
855 goto got_it;
856 if (go_left && zoneno == 0)
857 goto got_it;
858 }
859 for (i = 0; i < NR_CURSEG_TYPE; i++)
860 if (CURSEG_I(sbi, i)->zone == zoneno)
861 break;
862
863 if (i < NR_CURSEG_TYPE) {
864 /* zone is in user, try another */
865 if (go_left)
866 hint = zoneno * sbi->secs_per_zone - 1;
867 else if (zoneno + 1 >= total_zones)
868 hint = 0;
869 else
870 hint = (zoneno + 1) * sbi->secs_per_zone;
871 init = false;
872 goto find_other_zone;
873 }
874got_it:
875 /* set it as dirty segment in free segmap */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700876 f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900877 __set_inuse(sbi, segno);
878 *newseg = segno;
879 write_unlock(&free_i->segmap_lock);
880}
881
882static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
883{
884 struct curseg_info *curseg = CURSEG_I(sbi, type);
885 struct summary_footer *sum_footer;
886
887 curseg->segno = curseg->next_segno;
888 curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
889 curseg->next_blkoff = 0;
890 curseg->next_segno = NULL_SEGNO;
891
892 sum_footer = &(curseg->sum_blk->footer);
893 memset(sum_footer, 0, sizeof(struct summary_footer));
894 if (IS_DATASEG(type))
895 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
896 if (IS_NODESEG(type))
897 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
898 __set_sit_entry_type(sbi, type, curseg->segno, modified);
899}
900
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900901/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900902 * Allocate a current working segment.
903 * This function always allocates a free segment in LFS manner.
904 */
905static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
906{
907 struct curseg_info *curseg = CURSEG_I(sbi, type);
908 unsigned int segno = curseg->segno;
909 int dir = ALLOC_LEFT;
910
911 write_sum_page(sbi, curseg->sum_blk,
Haicheng Li81fb5e82013-05-14 18:20:28 +0800912 GET_SUM_BLOCK(sbi, segno));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900913 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
914 dir = ALLOC_RIGHT;
915
916 if (test_opt(sbi, NOHEAP))
917 dir = ALLOC_RIGHT;
918
919 get_new_segment(sbi, &segno, new_sec, dir);
920 curseg->next_segno = segno;
921 reset_curseg(sbi, type, 1);
922 curseg->alloc_type = LFS;
923}
924
925static void __next_free_blkoff(struct f2fs_sb_info *sbi,
926 struct curseg_info *seg, block_t start)
927{
928 struct seg_entry *se = get_seg_entry(sbi, seg->segno);
Changman Leee81c93c2013-11-15 13:21:16 +0900929 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
930 unsigned long target_map[entries];
931 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
932 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
933 int i, pos;
934
935 for (i = 0; i < entries; i++)
936 target_map[i] = ckpt_map[i] | cur_map[i];
937
938 pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
939
940 seg->next_blkoff = pos;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900941}
942
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900943/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900944 * If a segment is written by LFS manner, next block offset is just obtained
945 * by increasing the current block offset. However, if a segment is written by
946 * SSR manner, next block offset obtained by calling __next_free_blkoff
947 */
948static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
949 struct curseg_info *seg)
950{
951 if (seg->alloc_type == SSR)
952 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
953 else
954 seg->next_blkoff++;
955}
956
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900957/*
arter97e1c42042014-08-06 23:22:50 +0900958 * This function always allocates a used segment(from dirty seglist) by SSR
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900959 * manner, so it should recover the existing segment information of valid blocks
960 */
961static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
962{
963 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
964 struct curseg_info *curseg = CURSEG_I(sbi, type);
965 unsigned int new_segno = curseg->next_segno;
966 struct f2fs_summary_block *sum_node;
967 struct page *sum_page;
968
969 write_sum_page(sbi, curseg->sum_blk,
970 GET_SUM_BLOCK(sbi, curseg->segno));
971 __set_test_and_inuse(sbi, new_segno);
972
973 mutex_lock(&dirty_i->seglist_lock);
974 __remove_dirty_segment(sbi, new_segno, PRE);
975 __remove_dirty_segment(sbi, new_segno, DIRTY);
976 mutex_unlock(&dirty_i->seglist_lock);
977
978 reset_curseg(sbi, type, 1);
979 curseg->alloc_type = SSR;
980 __next_free_blkoff(sbi, curseg, 0);
981
982 if (reuse) {
983 sum_page = get_sum_page(sbi, new_segno);
984 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
985 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
986 f2fs_put_page(sum_page, 1);
987 }
988}
989
Jaegeuk Kim43727522013-02-04 15:11:17 +0900990static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
991{
992 struct curseg_info *curseg = CURSEG_I(sbi, type);
993 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
994
995 if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0))
996 return v_ops->get_victim(sbi,
997 &(curseg)->next_segno, BG_GC, type, SSR);
998
999 /* For data segments, let's do SSR more intensively */
1000 for (; type >= CURSEG_HOT_DATA; type--)
1001 if (v_ops->get_victim(sbi, &(curseg)->next_segno,
1002 BG_GC, type, SSR))
1003 return 1;
1004 return 0;
1005}
1006
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001007/*
1008 * flush out current segment and replace it with new segment
1009 * This function should be returned with success, otherwise BUG
1010 */
1011static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
1012 int type, bool force)
1013{
1014 struct curseg_info *curseg = CURSEG_I(sbi, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001015
Gu Zheng7b405272013-08-19 09:41:15 +08001016 if (force)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001017 new_curseg(sbi, type, true);
Gu Zheng7b405272013-08-19 09:41:15 +08001018 else if (type == CURSEG_WARM_NODE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001019 new_curseg(sbi, type, false);
Jaegeuk Kim60374682013-03-31 13:58:51 +09001020 else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
1021 new_curseg(sbi, type, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001022 else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
1023 change_curseg(sbi, type, true);
1024 else
1025 new_curseg(sbi, type, false);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +09001026
1027 stat_inc_seg_type(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001028}
1029
1030void allocate_new_segments(struct f2fs_sb_info *sbi)
1031{
1032 struct curseg_info *curseg;
1033 unsigned int old_curseg;
1034 int i;
1035
1036 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1037 curseg = CURSEG_I(sbi, i);
1038 old_curseg = curseg->segno;
1039 SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
1040 locate_dirty_segment(sbi, old_curseg);
1041 }
1042}
1043
1044static const struct segment_allocation default_salloc_ops = {
1045 .allocate_segment = allocate_segment_by_default,
1046};
1047
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001048int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range)
1049{
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001050 __u64 start = range->start >> sbi->log_blocksize;
1051 __u64 end = start + (range->len >> sbi->log_blocksize) - 1;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001052 unsigned int start_segno, end_segno;
1053 struct cp_control cpc;
1054
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001055 if (range->minlen > SEGMENT_SIZE(sbi) || start >= MAX_BLKADDR(sbi) ||
1056 range->len < sbi->blocksize)
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001057 return -EINVAL;
1058
Jan Kara9bd27ae2014-10-21 14:07:33 +02001059 cpc.trimmed = 0;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001060 if (end <= MAIN_BLKADDR(sbi))
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001061 goto out;
1062
1063 /* start/end segment number in main_area */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001064 start_segno = (start <= MAIN_BLKADDR(sbi)) ? 0 : GET_SEGNO(sbi, start);
1065 end_segno = (end >= MAX_BLKADDR(sbi)) ? MAIN_SEGS(sbi) - 1 :
1066 GET_SEGNO(sbi, end);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001067 cpc.reason = CP_DISCARD;
1068 cpc.trim_start = start_segno;
1069 cpc.trim_end = end_segno;
1070 cpc.trim_minlen = range->minlen >> sbi->log_blocksize;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001071
1072 /* do checkpoint to issue discard commands safely */
Jaegeuk Kimca4b02e2014-10-27 11:04:35 -07001073 mutex_lock(&sbi->gc_mutex);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001074 write_checkpoint(sbi, &cpc);
Jaegeuk Kimca4b02e2014-10-27 11:04:35 -07001075 mutex_unlock(&sbi->gc_mutex);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001076out:
1077 range->len = cpc.trimmed << sbi->log_blocksize;
1078 return 0;
1079}
1080
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001081static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
1082{
1083 struct curseg_info *curseg = CURSEG_I(sbi, type);
1084 if (curseg->next_blkoff < sbi->blocks_per_seg)
1085 return true;
1086 return false;
1087}
1088
1089static int __get_segment_type_2(struct page *page, enum page_type p_type)
1090{
1091 if (p_type == DATA)
1092 return CURSEG_HOT_DATA;
1093 else
1094 return CURSEG_HOT_NODE;
1095}
1096
1097static int __get_segment_type_4(struct page *page, enum page_type p_type)
1098{
1099 if (p_type == DATA) {
1100 struct inode *inode = page->mapping->host;
1101
1102 if (S_ISDIR(inode->i_mode))
1103 return CURSEG_HOT_DATA;
1104 else
1105 return CURSEG_COLD_DATA;
1106 } else {
Jaegeuk Kima344b9f2014-11-05 20:05:53 -08001107 if (IS_DNODE(page) && is_cold_node(page))
1108 return CURSEG_WARM_NODE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001109 else
1110 return CURSEG_COLD_NODE;
1111 }
1112}
1113
1114static int __get_segment_type_6(struct page *page, enum page_type p_type)
1115{
1116 if (p_type == DATA) {
1117 struct inode *inode = page->mapping->host;
1118
1119 if (S_ISDIR(inode->i_mode))
1120 return CURSEG_HOT_DATA;
Jaegeuk Kim354a3392013-06-14 08:52:35 +09001121 else if (is_cold_data(page) || file_is_cold(inode))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001122 return CURSEG_COLD_DATA;
1123 else
1124 return CURSEG_WARM_DATA;
1125 } else {
1126 if (IS_DNODE(page))
1127 return is_cold_node(page) ? CURSEG_WARM_NODE :
1128 CURSEG_HOT_NODE;
1129 else
1130 return CURSEG_COLD_NODE;
1131 }
1132}
1133
1134static int __get_segment_type(struct page *page, enum page_type p_type)
1135{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001136 switch (F2FS_P_SB(page)->active_logs) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001137 case 2:
1138 return __get_segment_type_2(page, p_type);
1139 case 4:
1140 return __get_segment_type_4(page, p_type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001141 }
Jaegeuk Kim12a67142012-12-21 11:47:05 +09001142 /* NR_CURSEG_TYPE(6) logs by default */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001143 f2fs_bug_on(F2FS_P_SB(page),
1144 F2FS_P_SB(page)->active_logs != NR_CURSEG_TYPE);
Jaegeuk Kim12a67142012-12-21 11:47:05 +09001145 return __get_segment_type_6(page, p_type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001146}
1147
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001148void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
1149 block_t old_blkaddr, block_t *new_blkaddr,
1150 struct f2fs_summary *sum, int type)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001151{
1152 struct sit_info *sit_i = SIT_I(sbi);
1153 struct curseg_info *curseg;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001154
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001155 curseg = CURSEG_I(sbi, type);
1156
1157 mutex_lock(&curseg->curseg_mutex);
1158
1159 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001160
1161 /*
1162 * __add_sum_entry should be resided under the curseg_mutex
1163 * because, this function updates a summary entry in the
1164 * current summary block.
1165 */
Haicheng Lie79efe32013-06-13 16:59:27 +08001166 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001167
1168 mutex_lock(&sit_i->sentry_lock);
1169 __refresh_next_blkoff(sbi, curseg);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +09001170
1171 stat_inc_block_count(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001172
Jaegeuk Kim5e443812014-01-28 12:22:14 +09001173 if (!__has_curseg_space(sbi, type))
1174 sit_i->s_ops->allocate_segment(sbi, type, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001175 /*
1176 * SIT information should be updated before segment allocation,
1177 * since SSR needs latest valid block information.
1178 */
1179 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
Jaegeuk Kim5e443812014-01-28 12:22:14 +09001180
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001181 mutex_unlock(&sit_i->sentry_lock);
1182
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001183 if (page && IS_NODESEG(type))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001184 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
1185
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001186 mutex_unlock(&curseg->curseg_mutex);
1187}
1188
1189static void do_write_page(struct f2fs_sb_info *sbi, struct page *page,
1190 block_t old_blkaddr, block_t *new_blkaddr,
1191 struct f2fs_summary *sum, struct f2fs_io_info *fio)
1192{
1193 int type = __get_segment_type(page, fio->type);
1194
1195 allocate_data_block(sbi, page, old_blkaddr, new_blkaddr, sum, type);
1196
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001197 /* writeout dirty page into bdev */
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001198 f2fs_submit_page_mbio(sbi, page, *new_blkaddr, fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001199}
1200
Jaegeuk Kim577e3492013-01-24 19:56:11 +09001201void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001202{
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001203 struct f2fs_io_info fio = {
1204 .type = META,
Gu Zheng7e8f2302013-12-20 18:17:49 +08001205 .rw = WRITE_SYNC | REQ_META | REQ_PRIO
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001206 };
1207
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001208 set_page_writeback(page);
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001209 f2fs_submit_page_mbio(sbi, page, page->index, &fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001210}
1211
1212void write_node_page(struct f2fs_sb_info *sbi, struct page *page,
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001213 struct f2fs_io_info *fio,
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001214 unsigned int nid, block_t old_blkaddr, block_t *new_blkaddr)
1215{
1216 struct f2fs_summary sum;
1217 set_summary(&sum, nid, 0, 0);
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001218 do_write_page(sbi, page, old_blkaddr, new_blkaddr, &sum, fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001219}
1220
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001221void write_data_page(struct page *page, struct dnode_of_data *dn,
1222 block_t *new_blkaddr, struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001223{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001224 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001225 struct f2fs_summary sum;
1226 struct node_info ni;
1227
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001228 f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001229 get_node_info(sbi, dn->nid, &ni);
1230 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1231
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001232 do_write_page(sbi, page, dn->data_blkaddr, new_blkaddr, &sum, fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001233}
1234
Chris Fries6c311ec2014-01-17 14:44:39 -06001235void rewrite_data_page(struct page *page, block_t old_blkaddr,
1236 struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001237{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001238 f2fs_submit_page_mbio(F2FS_P_SB(page), page, old_blkaddr, fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001239}
1240
1241void recover_data_page(struct f2fs_sb_info *sbi,
1242 struct page *page, struct f2fs_summary *sum,
1243 block_t old_blkaddr, block_t new_blkaddr)
1244{
1245 struct sit_info *sit_i = SIT_I(sbi);
1246 struct curseg_info *curseg;
1247 unsigned int segno, old_cursegno;
1248 struct seg_entry *se;
1249 int type;
1250
1251 segno = GET_SEGNO(sbi, new_blkaddr);
1252 se = get_seg_entry(sbi, segno);
1253 type = se->type;
1254
1255 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
1256 if (old_blkaddr == NULL_ADDR)
1257 type = CURSEG_COLD_DATA;
1258 else
1259 type = CURSEG_WARM_DATA;
1260 }
1261 curseg = CURSEG_I(sbi, type);
1262
1263 mutex_lock(&curseg->curseg_mutex);
1264 mutex_lock(&sit_i->sentry_lock);
1265
1266 old_cursegno = curseg->segno;
1267
1268 /* change the current segment */
1269 if (segno != curseg->segno) {
1270 curseg->next_segno = segno;
1271 change_curseg(sbi, type, true);
1272 }
1273
Jaegeuk Kim491c0852014-02-04 13:01:10 +09001274 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
Haicheng Lie79efe32013-06-13 16:59:27 +08001275 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001276
1277 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001278 locate_dirty_segment(sbi, old_cursegno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001279
1280 mutex_unlock(&sit_i->sentry_lock);
1281 mutex_unlock(&curseg->curseg_mutex);
1282}
1283
Chao Yudf0f8dc2014-03-22 14:57:23 +08001284static inline bool is_merged_page(struct f2fs_sb_info *sbi,
1285 struct page *page, enum page_type type)
1286{
1287 enum page_type btype = PAGE_TYPE_OF_BIO(type);
1288 struct f2fs_bio_info *io = &sbi->write_io[btype];
Chao Yudf0f8dc2014-03-22 14:57:23 +08001289 struct bio_vec *bvec;
1290 int i;
1291
1292 down_read(&io->io_rwsem);
Jaegeuk Kimce234472014-04-02 09:04:42 +09001293 if (!io->bio)
Chao Yudf0f8dc2014-03-22 14:57:23 +08001294 goto out;
1295
Jaegeuk Kimce234472014-04-02 09:04:42 +09001296 bio_for_each_segment_all(bvec, io->bio, i) {
Chao Yudf0f8dc2014-03-22 14:57:23 +08001297 if (page == bvec->bv_page) {
1298 up_read(&io->io_rwsem);
1299 return true;
1300 }
1301 }
1302
1303out:
1304 up_read(&io->io_rwsem);
1305 return false;
1306}
1307
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001308void f2fs_wait_on_page_writeback(struct page *page,
Yuan Zhong5514f0a2014-01-10 07:26:14 +00001309 enum page_type type)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001310{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001311 if (PageWriteback(page)) {
Jaegeuk Kim40813632014-09-02 15:31:18 -07001312 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
1313
Chao Yudf0f8dc2014-03-22 14:57:23 +08001314 if (is_merged_page(sbi, page, type))
1315 f2fs_submit_merged_bio(sbi, type, WRITE);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001316 wait_on_page_writeback(page);
1317 }
1318}
1319
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001320static int read_compacted_summaries(struct f2fs_sb_info *sbi)
1321{
1322 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1323 struct curseg_info *seg_i;
1324 unsigned char *kaddr;
1325 struct page *page;
1326 block_t start;
1327 int i, j, offset;
1328
1329 start = start_sum_block(sbi);
1330
1331 page = get_meta_page(sbi, start++);
1332 kaddr = (unsigned char *)page_address(page);
1333
1334 /* Step 1: restore nat cache */
1335 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1336 memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
1337
1338 /* Step 2: restore sit cache */
1339 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1340 memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
1341 SUM_JOURNAL_SIZE);
1342 offset = 2 * SUM_JOURNAL_SIZE;
1343
1344 /* Step 3: restore summary entries */
1345 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1346 unsigned short blk_off;
1347 unsigned int segno;
1348
1349 seg_i = CURSEG_I(sbi, i);
1350 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
1351 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
1352 seg_i->next_segno = segno;
1353 reset_curseg(sbi, i, 0);
1354 seg_i->alloc_type = ckpt->alloc_type[i];
1355 seg_i->next_blkoff = blk_off;
1356
1357 if (seg_i->alloc_type == SSR)
1358 blk_off = sbi->blocks_per_seg;
1359
1360 for (j = 0; j < blk_off; j++) {
1361 struct f2fs_summary *s;
1362 s = (struct f2fs_summary *)(kaddr + offset);
1363 seg_i->sum_blk->entries[j] = *s;
1364 offset += SUMMARY_SIZE;
1365 if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1366 SUM_FOOTER_SIZE)
1367 continue;
1368
1369 f2fs_put_page(page, 1);
1370 page = NULL;
1371
1372 page = get_meta_page(sbi, start++);
1373 kaddr = (unsigned char *)page_address(page);
1374 offset = 0;
1375 }
1376 }
1377 f2fs_put_page(page, 1);
1378 return 0;
1379}
1380
1381static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
1382{
1383 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1384 struct f2fs_summary_block *sum;
1385 struct curseg_info *curseg;
1386 struct page *new;
1387 unsigned short blk_off;
1388 unsigned int segno = 0;
1389 block_t blk_addr = 0;
1390
1391 /* get segment number and block addr */
1392 if (IS_DATASEG(type)) {
1393 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
1394 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
1395 CURSEG_HOT_DATA]);
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001396 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001397 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
1398 else
1399 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
1400 } else {
1401 segno = le32_to_cpu(ckpt->cur_node_segno[type -
1402 CURSEG_HOT_NODE]);
1403 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
1404 CURSEG_HOT_NODE]);
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001405 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001406 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
1407 type - CURSEG_HOT_NODE);
1408 else
1409 blk_addr = GET_SUM_BLOCK(sbi, segno);
1410 }
1411
1412 new = get_meta_page(sbi, blk_addr);
1413 sum = (struct f2fs_summary_block *)page_address(new);
1414
1415 if (IS_NODESEG(type)) {
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001416 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001417 struct f2fs_summary *ns = &sum->entries[0];
1418 int i;
1419 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
1420 ns->version = 0;
1421 ns->ofs_in_node = 0;
1422 }
1423 } else {
Gu Zhengd6537882014-03-07 18:43:36 +08001424 int err;
1425
1426 err = restore_node_summary(sbi, segno, sum);
1427 if (err) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001428 f2fs_put_page(new, 1);
Gu Zhengd6537882014-03-07 18:43:36 +08001429 return err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001430 }
1431 }
1432 }
1433
1434 /* set uncompleted segment to curseg */
1435 curseg = CURSEG_I(sbi, type);
1436 mutex_lock(&curseg->curseg_mutex);
1437 memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
1438 curseg->next_segno = segno;
1439 reset_curseg(sbi, type, 0);
1440 curseg->alloc_type = ckpt->alloc_type[type];
1441 curseg->next_blkoff = blk_off;
1442 mutex_unlock(&curseg->curseg_mutex);
1443 f2fs_put_page(new, 1);
1444 return 0;
1445}
1446
1447static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
1448{
1449 int type = CURSEG_HOT_DATA;
Chao Yue4fc5fb2014-03-17 16:36:24 +08001450 int err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001451
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001452 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001453 /* restore for compacted data summary */
1454 if (read_compacted_summaries(sbi))
1455 return -EINVAL;
1456 type = CURSEG_HOT_NODE;
1457 }
1458
Chao Yue4fc5fb2014-03-17 16:36:24 +08001459 for (; type <= CURSEG_COLD_NODE; type++) {
1460 err = read_normal_summaries(sbi, type);
1461 if (err)
1462 return err;
1463 }
1464
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001465 return 0;
1466}
1467
1468static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
1469{
1470 struct page *page;
1471 unsigned char *kaddr;
1472 struct f2fs_summary *summary;
1473 struct curseg_info *seg_i;
1474 int written_size = 0;
1475 int i, j;
1476
1477 page = grab_meta_page(sbi, blkaddr++);
1478 kaddr = (unsigned char *)page_address(page);
1479
1480 /* Step 1: write nat cache */
1481 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1482 memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
1483 written_size += SUM_JOURNAL_SIZE;
1484
1485 /* Step 2: write sit cache */
1486 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1487 memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
1488 SUM_JOURNAL_SIZE);
1489 written_size += SUM_JOURNAL_SIZE;
1490
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001491 /* Step 3: write summary entries */
1492 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1493 unsigned short blkoff;
1494 seg_i = CURSEG_I(sbi, i);
1495 if (sbi->ckpt->alloc_type[i] == SSR)
1496 blkoff = sbi->blocks_per_seg;
1497 else
1498 blkoff = curseg_blkoff(sbi, i);
1499
1500 for (j = 0; j < blkoff; j++) {
1501 if (!page) {
1502 page = grab_meta_page(sbi, blkaddr++);
1503 kaddr = (unsigned char *)page_address(page);
1504 written_size = 0;
1505 }
1506 summary = (struct f2fs_summary *)(kaddr + written_size);
1507 *summary = seg_i->sum_blk->entries[j];
1508 written_size += SUMMARY_SIZE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001509
1510 if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1511 SUM_FOOTER_SIZE)
1512 continue;
1513
Chao Yue8d61a72013-10-24 15:08:28 +08001514 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001515 f2fs_put_page(page, 1);
1516 page = NULL;
1517 }
1518 }
Chao Yue8d61a72013-10-24 15:08:28 +08001519 if (page) {
1520 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001521 f2fs_put_page(page, 1);
Chao Yue8d61a72013-10-24 15:08:28 +08001522 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001523}
1524
1525static void write_normal_summaries(struct f2fs_sb_info *sbi,
1526 block_t blkaddr, int type)
1527{
1528 int i, end;
1529 if (IS_DATASEG(type))
1530 end = type + NR_CURSEG_DATA_TYPE;
1531 else
1532 end = type + NR_CURSEG_NODE_TYPE;
1533
1534 for (i = type; i < end; i++) {
1535 struct curseg_info *sum = CURSEG_I(sbi, i);
1536 mutex_lock(&sum->curseg_mutex);
1537 write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
1538 mutex_unlock(&sum->curseg_mutex);
1539 }
1540}
1541
1542void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1543{
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001544 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001545 write_compacted_summaries(sbi, start_blk);
1546 else
1547 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
1548}
1549
1550void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1551{
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001552 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001553 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001554}
1555
1556int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
1557 unsigned int val, int alloc)
1558{
1559 int i;
1560
1561 if (type == NAT_JOURNAL) {
1562 for (i = 0; i < nats_in_cursum(sum); i++) {
1563 if (le32_to_cpu(nid_in_journal(sum, i)) == val)
1564 return i;
1565 }
1566 if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
1567 return update_nats_in_cursum(sum, 1);
1568 } else if (type == SIT_JOURNAL) {
1569 for (i = 0; i < sits_in_cursum(sum); i++)
1570 if (le32_to_cpu(segno_in_journal(sum, i)) == val)
1571 return i;
1572 if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
1573 return update_sits_in_cursum(sum, 1);
1574 }
1575 return -1;
1576}
1577
1578static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
1579 unsigned int segno)
1580{
Gu Zheng2cc22182014-10-20 17:45:49 +08001581 return get_meta_page(sbi, current_sit_addr(sbi, segno));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001582}
1583
1584static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
1585 unsigned int start)
1586{
1587 struct sit_info *sit_i = SIT_I(sbi);
1588 struct page *src_page, *dst_page;
1589 pgoff_t src_off, dst_off;
1590 void *src_addr, *dst_addr;
1591
1592 src_off = current_sit_addr(sbi, start);
1593 dst_off = next_sit_addr(sbi, src_off);
1594
1595 /* get current sit block page without lock */
1596 src_page = get_meta_page(sbi, src_off);
1597 dst_page = grab_meta_page(sbi, dst_off);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001598 f2fs_bug_on(sbi, PageDirty(src_page));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001599
1600 src_addr = page_address(src_page);
1601 dst_addr = page_address(dst_page);
1602 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
1603
1604 set_page_dirty(dst_page);
1605 f2fs_put_page(src_page, 1);
1606
1607 set_to_next_sit(sit_i, start);
1608
1609 return dst_page;
1610}
1611
Chao Yu184a5cd2014-09-04 18:13:01 +08001612static struct sit_entry_set *grab_sit_entry_set(void)
1613{
1614 struct sit_entry_set *ses =
1615 f2fs_kmem_cache_alloc(sit_entry_set_slab, GFP_ATOMIC);
1616
1617 ses->entry_cnt = 0;
1618 INIT_LIST_HEAD(&ses->set_list);
1619 return ses;
1620}
1621
1622static void release_sit_entry_set(struct sit_entry_set *ses)
1623{
1624 list_del(&ses->set_list);
1625 kmem_cache_free(sit_entry_set_slab, ses);
1626}
1627
1628static void adjust_sit_entry_set(struct sit_entry_set *ses,
1629 struct list_head *head)
1630{
1631 struct sit_entry_set *next = ses;
1632
1633 if (list_is_last(&ses->set_list, head))
1634 return;
1635
1636 list_for_each_entry_continue(next, head, set_list)
1637 if (ses->entry_cnt <= next->entry_cnt)
1638 break;
1639
1640 list_move_tail(&ses->set_list, &next->set_list);
1641}
1642
1643static void add_sit_entry(unsigned int segno, struct list_head *head)
1644{
1645 struct sit_entry_set *ses;
1646 unsigned int start_segno = START_SEGNO(segno);
1647
1648 list_for_each_entry(ses, head, set_list) {
1649 if (ses->start_segno == start_segno) {
1650 ses->entry_cnt++;
1651 adjust_sit_entry_set(ses, head);
1652 return;
1653 }
1654 }
1655
1656 ses = grab_sit_entry_set();
1657
1658 ses->start_segno = start_segno;
1659 ses->entry_cnt++;
1660 list_add(&ses->set_list, head);
1661}
1662
1663static void add_sits_in_set(struct f2fs_sb_info *sbi)
1664{
1665 struct f2fs_sm_info *sm_info = SM_I(sbi);
1666 struct list_head *set_list = &sm_info->sit_entry_set;
1667 unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap;
Chao Yu184a5cd2014-09-04 18:13:01 +08001668 unsigned int segno;
1669
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001670 for_each_set_bit(segno, bitmap, MAIN_SEGS(sbi))
Chao Yu184a5cd2014-09-04 18:13:01 +08001671 add_sit_entry(segno, set_list);
1672}
1673
1674static void remove_sits_in_journal(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001675{
1676 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1677 struct f2fs_summary_block *sum = curseg->sum_blk;
1678 int i;
1679
Chao Yu184a5cd2014-09-04 18:13:01 +08001680 for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
1681 unsigned int segno;
1682 bool dirtied;
1683
1684 segno = le32_to_cpu(segno_in_journal(sum, i));
1685 dirtied = __mark_sit_entry_dirty(sbi, segno);
1686
1687 if (!dirtied)
1688 add_sit_entry(segno, &SM_I(sbi)->sit_entry_set);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001689 }
Chao Yu184a5cd2014-09-04 18:13:01 +08001690 update_sits_in_cursum(sum, -sits_in_cursum(sum));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001691}
1692
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001693/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001694 * CP calls this function, which flushes SIT entries including sit_journal,
1695 * and moves prefree segs to free segs.
1696 */
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001697void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001698{
1699 struct sit_info *sit_i = SIT_I(sbi);
1700 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
1701 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1702 struct f2fs_summary_block *sum = curseg->sum_blk;
Chao Yu184a5cd2014-09-04 18:13:01 +08001703 struct sit_entry_set *ses, *tmp;
1704 struct list_head *head = &SM_I(sbi)->sit_entry_set;
Chao Yu184a5cd2014-09-04 18:13:01 +08001705 bool to_journal = true;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001706 struct seg_entry *se;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001707
1708 mutex_lock(&curseg->curseg_mutex);
1709 mutex_lock(&sit_i->sentry_lock);
1710
1711 /*
Chao Yu184a5cd2014-09-04 18:13:01 +08001712 * add and account sit entries of dirty bitmap in sit entry
1713 * set temporarily
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001714 */
Chao Yu184a5cd2014-09-04 18:13:01 +08001715 add_sits_in_set(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001716
Chao Yu184a5cd2014-09-04 18:13:01 +08001717 /*
1718 * if there are no enough space in journal to store dirty sit
1719 * entries, remove all entries from journal and add and account
1720 * them in sit entry set.
1721 */
1722 if (!__has_cursum_space(sum, sit_i->dirty_sentries, SIT_JOURNAL))
1723 remove_sits_in_journal(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001724
Chao Yu184a5cd2014-09-04 18:13:01 +08001725 if (!sit_i->dirty_sentries)
1726 goto out;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001727
Chao Yu184a5cd2014-09-04 18:13:01 +08001728 /*
1729 * there are two steps to flush sit entries:
1730 * #1, flush sit entries to journal in current cold data summary block.
1731 * #2, flush sit entries to sit page.
1732 */
1733 list_for_each_entry_safe(ses, tmp, head, set_list) {
Jaegeuk Kim4a257ed2014-10-16 11:43:30 -07001734 struct page *page = NULL;
Chao Yu184a5cd2014-09-04 18:13:01 +08001735 struct f2fs_sit_block *raw_sit = NULL;
1736 unsigned int start_segno = ses->start_segno;
1737 unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001738 (unsigned long)MAIN_SEGS(sbi));
Chao Yu184a5cd2014-09-04 18:13:01 +08001739 unsigned int segno = start_segno;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001740
Chao Yu184a5cd2014-09-04 18:13:01 +08001741 if (to_journal &&
1742 !__has_cursum_space(sum, ses->entry_cnt, SIT_JOURNAL))
1743 to_journal = false;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001744
Chao Yu184a5cd2014-09-04 18:13:01 +08001745 if (!to_journal) {
1746 page = get_next_sit_page(sbi, start_segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001747 raw_sit = page_address(page);
1748 }
1749
Chao Yu184a5cd2014-09-04 18:13:01 +08001750 /* flush dirty sit entries in region of current sit set */
1751 for_each_set_bit_from(segno, bitmap, end) {
1752 int offset, sit_offset;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001753
1754 se = get_seg_entry(sbi, segno);
Chao Yu184a5cd2014-09-04 18:13:01 +08001755
1756 /* add discard candidates */
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001757 if (SM_I(sbi)->nr_discards < SM_I(sbi)->max_discards) {
1758 cpc->trim_start = segno;
1759 add_discard_addrs(sbi, cpc);
1760 }
Chao Yu184a5cd2014-09-04 18:13:01 +08001761
1762 if (to_journal) {
1763 offset = lookup_journal_in_cursum(sum,
1764 SIT_JOURNAL, segno, 1);
1765 f2fs_bug_on(sbi, offset < 0);
1766 segno_in_journal(sum, offset) =
1767 cpu_to_le32(segno);
1768 seg_info_to_raw_sit(se,
1769 &sit_in_journal(sum, offset));
1770 } else {
1771 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
1772 seg_info_to_raw_sit(se,
1773 &raw_sit->entries[sit_offset]);
1774 }
1775
1776 __clear_bit(segno, bitmap);
1777 sit_i->dirty_sentries--;
1778 ses->entry_cnt--;
1779 }
1780
1781 if (!to_journal)
1782 f2fs_put_page(page, 1);
1783
1784 f2fs_bug_on(sbi, ses->entry_cnt);
1785 release_sit_entry_set(ses);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001786 }
Chao Yu184a5cd2014-09-04 18:13:01 +08001787
1788 f2fs_bug_on(sbi, !list_empty(head));
1789 f2fs_bug_on(sbi, sit_i->dirty_sentries);
Chao Yu184a5cd2014-09-04 18:13:01 +08001790out:
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001791 if (cpc->reason == CP_DISCARD) {
1792 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++)
1793 add_discard_addrs(sbi, cpc);
1794 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001795 mutex_unlock(&sit_i->sentry_lock);
1796 mutex_unlock(&curseg->curseg_mutex);
1797
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001798 set_prefree_as_free_segments(sbi);
1799}
1800
1801static int build_sit_info(struct f2fs_sb_info *sbi)
1802{
1803 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1804 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1805 struct sit_info *sit_i;
1806 unsigned int sit_segs, start;
1807 char *src_bitmap, *dst_bitmap;
1808 unsigned int bitmap_size;
1809
1810 /* allocate memory for SIT information */
1811 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
1812 if (!sit_i)
1813 return -ENOMEM;
1814
1815 SM_I(sbi)->sit_info = sit_i;
1816
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001817 sit_i->sentries = vzalloc(MAIN_SEGS(sbi) * sizeof(struct seg_entry));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001818 if (!sit_i->sentries)
1819 return -ENOMEM;
1820
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001821 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001822 sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1823 if (!sit_i->dirty_sentries_bitmap)
1824 return -ENOMEM;
1825
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001826 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001827 sit_i->sentries[start].cur_valid_map
1828 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1829 sit_i->sentries[start].ckpt_valid_map
1830 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1831 if (!sit_i->sentries[start].cur_valid_map
1832 || !sit_i->sentries[start].ckpt_valid_map)
1833 return -ENOMEM;
1834 }
1835
1836 if (sbi->segs_per_sec > 1) {
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001837 sit_i->sec_entries = vzalloc(MAIN_SECS(sbi) *
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001838 sizeof(struct sec_entry));
1839 if (!sit_i->sec_entries)
1840 return -ENOMEM;
1841 }
1842
1843 /* get information related with SIT */
1844 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
1845
1846 /* setup SIT bitmap from ckeckpoint pack */
1847 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
1848 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
1849
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001850 dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001851 if (!dst_bitmap)
1852 return -ENOMEM;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001853
1854 /* init SIT information */
1855 sit_i->s_ops = &default_salloc_ops;
1856
1857 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
1858 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
1859 sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
1860 sit_i->sit_bitmap = dst_bitmap;
1861 sit_i->bitmap_size = bitmap_size;
1862 sit_i->dirty_sentries = 0;
1863 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
1864 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
1865 sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
1866 mutex_init(&sit_i->sentry_lock);
1867 return 0;
1868}
1869
1870static int build_free_segmap(struct f2fs_sb_info *sbi)
1871{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001872 struct free_segmap_info *free_i;
1873 unsigned int bitmap_size, sec_bitmap_size;
1874
1875 /* allocate memory for free segmap information */
1876 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
1877 if (!free_i)
1878 return -ENOMEM;
1879
1880 SM_I(sbi)->free_info = free_i;
1881
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001882 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001883 free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL);
1884 if (!free_i->free_segmap)
1885 return -ENOMEM;
1886
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001887 sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001888 free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL);
1889 if (!free_i->free_secmap)
1890 return -ENOMEM;
1891
1892 /* set all segments as dirty temporarily */
1893 memset(free_i->free_segmap, 0xff, bitmap_size);
1894 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
1895
1896 /* init free segmap information */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001897 free_i->start_segno = GET_SEGNO_FROM_SEG0(sbi, MAIN_BLKADDR(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001898 free_i->free_segments = 0;
1899 free_i->free_sections = 0;
1900 rwlock_init(&free_i->segmap_lock);
1901 return 0;
1902}
1903
1904static int build_curseg(struct f2fs_sb_info *sbi)
1905{
Namjae Jeon1042d602012-12-01 10:56:13 +09001906 struct curseg_info *array;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001907 int i;
1908
Fabian Frederickb434bab2014-06-23 18:39:15 +02001909 array = kcalloc(NR_CURSEG_TYPE, sizeof(*array), GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001910 if (!array)
1911 return -ENOMEM;
1912
1913 SM_I(sbi)->curseg_array = array;
1914
1915 for (i = 0; i < NR_CURSEG_TYPE; i++) {
1916 mutex_init(&array[i].curseg_mutex);
1917 array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
1918 if (!array[i].sum_blk)
1919 return -ENOMEM;
1920 array[i].segno = NULL_SEGNO;
1921 array[i].next_blkoff = 0;
1922 }
1923 return restore_curseg_summaries(sbi);
1924}
1925
1926static void build_sit_entries(struct f2fs_sb_info *sbi)
1927{
1928 struct sit_info *sit_i = SIT_I(sbi);
1929 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1930 struct f2fs_summary_block *sum = curseg->sum_blk;
Chao Yu74de5932013-11-22 09:09:59 +08001931 int sit_blk_cnt = SIT_BLK_CNT(sbi);
1932 unsigned int i, start, end;
1933 unsigned int readed, start_blk = 0;
Jaegeuk Kim90a893c2014-09-22 16:21:07 -07001934 int nrpages = MAX_BIO_BLOCKS(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001935
Chao Yu74de5932013-11-22 09:09:59 +08001936 do {
Chao Yu662befd2014-02-07 16:11:53 +08001937 readed = ra_meta_pages(sbi, start_blk, nrpages, META_SIT);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001938
Chao Yu74de5932013-11-22 09:09:59 +08001939 start = start_blk * sit_i->sents_per_block;
1940 end = (start_blk + readed) * sit_i->sents_per_block;
1941
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001942 for (; start < end && start < MAIN_SEGS(sbi); start++) {
Chao Yu74de5932013-11-22 09:09:59 +08001943 struct seg_entry *se = &sit_i->sentries[start];
1944 struct f2fs_sit_block *sit_blk;
1945 struct f2fs_sit_entry sit;
1946 struct page *page;
1947
1948 mutex_lock(&curseg->curseg_mutex);
1949 for (i = 0; i < sits_in_cursum(sum); i++) {
Chris Fries6c311ec2014-01-17 14:44:39 -06001950 if (le32_to_cpu(segno_in_journal(sum, i))
1951 == start) {
Chao Yu74de5932013-11-22 09:09:59 +08001952 sit = sit_in_journal(sum, i);
1953 mutex_unlock(&curseg->curseg_mutex);
1954 goto got_it;
1955 }
1956 }
1957 mutex_unlock(&curseg->curseg_mutex);
1958
1959 page = get_current_sit_page(sbi, start);
1960 sit_blk = (struct f2fs_sit_block *)page_address(page);
1961 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
1962 f2fs_put_page(page, 1);
1963got_it:
1964 check_block_count(sbi, start, &sit);
1965 seg_info_from_raw_sit(se, &sit);
1966 if (sbi->segs_per_sec > 1) {
1967 struct sec_entry *e = get_sec_entry(sbi, start);
1968 e->valid_blocks += se->valid_blocks;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001969 }
1970 }
Chao Yu74de5932013-11-22 09:09:59 +08001971 start_blk += readed;
1972 } while (start_blk < sit_blk_cnt);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001973}
1974
1975static void init_free_segmap(struct f2fs_sb_info *sbi)
1976{
1977 unsigned int start;
1978 int type;
1979
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001980 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001981 struct seg_entry *sentry = get_seg_entry(sbi, start);
1982 if (!sentry->valid_blocks)
1983 __set_free(sbi, start);
1984 }
1985
1986 /* set use the current segments */
1987 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
1988 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
1989 __set_test_and_inuse(sbi, curseg_t->segno);
1990 }
1991}
1992
1993static void init_dirty_segmap(struct f2fs_sb_info *sbi)
1994{
1995 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1996 struct free_segmap_info *free_i = FREE_I(sbi);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001997 unsigned int segno = 0, offset = 0;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001998 unsigned short valid_blocks;
1999
Namjae Jeon8736fbf2013-06-16 09:49:11 +09002000 while (1) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002001 /* find dirty segment based on free segmap */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002002 segno = find_next_inuse(free_i, MAIN_SEGS(sbi), offset);
2003 if (segno >= MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002004 break;
2005 offset = segno + 1;
2006 valid_blocks = get_valid_blocks(sbi, segno, 0);
Jaegeuk Kimec325b52014-09-02 16:24:11 -07002007 if (valid_blocks == sbi->blocks_per_seg || !valid_blocks)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002008 continue;
Jaegeuk Kimec325b52014-09-02 16:24:11 -07002009 if (valid_blocks > sbi->blocks_per_seg) {
2010 f2fs_bug_on(sbi, 1);
2011 continue;
2012 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002013 mutex_lock(&dirty_i->seglist_lock);
2014 __locate_dirty_segment(sbi, segno, DIRTY);
2015 mutex_unlock(&dirty_i->seglist_lock);
2016 }
2017}
2018
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002019static int init_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002020{
2021 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002022 unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002023
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002024 dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL);
2025 if (!dirty_i->victim_secmap)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002026 return -ENOMEM;
2027 return 0;
2028}
2029
2030static int build_dirty_segmap(struct f2fs_sb_info *sbi)
2031{
2032 struct dirty_seglist_info *dirty_i;
2033 unsigned int bitmap_size, i;
2034
2035 /* allocate memory for dirty segments list information */
2036 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
2037 if (!dirty_i)
2038 return -ENOMEM;
2039
2040 SM_I(sbi)->dirty_info = dirty_i;
2041 mutex_init(&dirty_i->seglist_lock);
2042
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002043 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002044
2045 for (i = 0; i < NR_DIRTY_TYPE; i++) {
2046 dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002047 if (!dirty_i->dirty_segmap[i])
2048 return -ENOMEM;
2049 }
2050
2051 init_dirty_segmap(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002052 return init_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002053}
2054
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002055/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002056 * Update min, max modified time for cost-benefit GC algorithm
2057 */
2058static void init_min_max_mtime(struct f2fs_sb_info *sbi)
2059{
2060 struct sit_info *sit_i = SIT_I(sbi);
2061 unsigned int segno;
2062
2063 mutex_lock(&sit_i->sentry_lock);
2064
2065 sit_i->min_mtime = LLONG_MAX;
2066
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002067 for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002068 unsigned int i;
2069 unsigned long long mtime = 0;
2070
2071 for (i = 0; i < sbi->segs_per_sec; i++)
2072 mtime += get_seg_entry(sbi, segno + i)->mtime;
2073
2074 mtime = div_u64(mtime, sbi->segs_per_sec);
2075
2076 if (sit_i->min_mtime > mtime)
2077 sit_i->min_mtime = mtime;
2078 }
2079 sit_i->max_mtime = get_mtime(sbi);
2080 mutex_unlock(&sit_i->sentry_lock);
2081}
2082
2083int build_segment_manager(struct f2fs_sb_info *sbi)
2084{
2085 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
2086 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Namjae Jeon1042d602012-12-01 10:56:13 +09002087 struct f2fs_sm_info *sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002088 int err;
2089
2090 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
2091 if (!sm_info)
2092 return -ENOMEM;
2093
2094 /* init sm info */
2095 sbi->sm_info = sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002096 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
2097 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
2098 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
2099 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
2100 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
2101 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
2102 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
Jaegeuk Kim58c41032014-03-19 14:17:21 +09002103 sm_info->rec_prefree_segments = sm_info->main_segments *
2104 DEF_RECLAIM_PREFREE_SEGMENTS / 100;
Jaegeuk Kim9b5f1362014-09-16 18:30:54 -07002105 sm_info->ipu_policy = 1 << F2FS_IPU_FSYNC;
Jaegeuk Kim216fbd62013-11-07 13:13:42 +09002106 sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -07002107 sm_info->min_fsync_blocks = DEF_MIN_FSYNC_BLOCKS;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002108
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002109 INIT_LIST_HEAD(&sm_info->discard_list);
2110 sm_info->nr_discards = 0;
2111 sm_info->max_discards = 0;
2112
Chao Yu184a5cd2014-09-04 18:13:01 +08002113 INIT_LIST_HEAD(&sm_info->sit_entry_set);
2114
Gu Zhengb270ad62014-04-11 17:49:55 +08002115 if (test_opt(sbi, FLUSH_MERGE) && !f2fs_readonly(sbi->sb)) {
Gu Zheng2163d192014-04-27 14:21:33 +08002116 err = create_flush_cmd_control(sbi);
2117 if (err)
Gu Zhenga688b9d9e2014-04-27 14:21:21 +08002118 return err;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +09002119 }
2120
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002121 err = build_sit_info(sbi);
2122 if (err)
2123 return err;
2124 err = build_free_segmap(sbi);
2125 if (err)
2126 return err;
2127 err = build_curseg(sbi);
2128 if (err)
2129 return err;
2130
2131 /* reinit free segmap based on SIT */
2132 build_sit_entries(sbi);
2133
2134 init_free_segmap(sbi);
2135 err = build_dirty_segmap(sbi);
2136 if (err)
2137 return err;
2138
2139 init_min_max_mtime(sbi);
2140 return 0;
2141}
2142
2143static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
2144 enum dirty_type dirty_type)
2145{
2146 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2147
2148 mutex_lock(&dirty_i->seglist_lock);
2149 kfree(dirty_i->dirty_segmap[dirty_type]);
2150 dirty_i->nr_dirty[dirty_type] = 0;
2151 mutex_unlock(&dirty_i->seglist_lock);
2152}
2153
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002154static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002155{
2156 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002157 kfree(dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002158}
2159
2160static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
2161{
2162 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2163 int i;
2164
2165 if (!dirty_i)
2166 return;
2167
2168 /* discard pre-free/dirty segments list */
2169 for (i = 0; i < NR_DIRTY_TYPE; i++)
2170 discard_dirty_segmap(sbi, i);
2171
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002172 destroy_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002173 SM_I(sbi)->dirty_info = NULL;
2174 kfree(dirty_i);
2175}
2176
2177static void destroy_curseg(struct f2fs_sb_info *sbi)
2178{
2179 struct curseg_info *array = SM_I(sbi)->curseg_array;
2180 int i;
2181
2182 if (!array)
2183 return;
2184 SM_I(sbi)->curseg_array = NULL;
2185 for (i = 0; i < NR_CURSEG_TYPE; i++)
2186 kfree(array[i].sum_blk);
2187 kfree(array);
2188}
2189
2190static void destroy_free_segmap(struct f2fs_sb_info *sbi)
2191{
2192 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
2193 if (!free_i)
2194 return;
2195 SM_I(sbi)->free_info = NULL;
2196 kfree(free_i->free_segmap);
2197 kfree(free_i->free_secmap);
2198 kfree(free_i);
2199}
2200
2201static void destroy_sit_info(struct f2fs_sb_info *sbi)
2202{
2203 struct sit_info *sit_i = SIT_I(sbi);
2204 unsigned int start;
2205
2206 if (!sit_i)
2207 return;
2208
2209 if (sit_i->sentries) {
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002210 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002211 kfree(sit_i->sentries[start].cur_valid_map);
2212 kfree(sit_i->sentries[start].ckpt_valid_map);
2213 }
2214 }
2215 vfree(sit_i->sentries);
2216 vfree(sit_i->sec_entries);
2217 kfree(sit_i->dirty_sentries_bitmap);
2218
2219 SM_I(sbi)->sit_info = NULL;
2220 kfree(sit_i->sit_bitmap);
2221 kfree(sit_i);
2222}
2223
2224void destroy_segment_manager(struct f2fs_sb_info *sbi)
2225{
2226 struct f2fs_sm_info *sm_info = SM_I(sbi);
Gu Zhenga688b9d9e2014-04-27 14:21:21 +08002227
Chao Yu3b03f722013-11-06 09:12:04 +08002228 if (!sm_info)
2229 return;
Gu Zheng2163d192014-04-27 14:21:33 +08002230 destroy_flush_cmd_control(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002231 destroy_dirty_segmap(sbi);
2232 destroy_curseg(sbi);
2233 destroy_free_segmap(sbi);
2234 destroy_sit_info(sbi);
2235 sbi->sm_info = NULL;
2236 kfree(sm_info);
2237}
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002238
2239int __init create_segment_manager_caches(void)
2240{
2241 discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
Gu Zhenge8512d22014-03-07 18:43:28 +08002242 sizeof(struct discard_entry));
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002243 if (!discard_entry_slab)
Chao Yu184a5cd2014-09-04 18:13:01 +08002244 goto fail;
2245
2246 sit_entry_set_slab = f2fs_kmem_cache_create("sit_entry_set",
Changman Leec9ee0082014-11-21 15:42:07 +09002247 sizeof(struct sit_entry_set));
Chao Yu184a5cd2014-09-04 18:13:01 +08002248 if (!sit_entry_set_slab)
2249 goto destory_discard_entry;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002250
2251 inmem_entry_slab = f2fs_kmem_cache_create("inmem_page_entry",
2252 sizeof(struct inmem_pages));
2253 if (!inmem_entry_slab)
2254 goto destroy_sit_entry_set;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002255 return 0;
Chao Yu184a5cd2014-09-04 18:13:01 +08002256
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002257destroy_sit_entry_set:
2258 kmem_cache_destroy(sit_entry_set_slab);
Chao Yu184a5cd2014-09-04 18:13:01 +08002259destory_discard_entry:
2260 kmem_cache_destroy(discard_entry_slab);
2261fail:
2262 return -ENOMEM;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002263}
2264
2265void destroy_segment_manager_caches(void)
2266{
Chao Yu184a5cd2014-09-04 18:13:01 +08002267 kmem_cache_destroy(sit_entry_set_slab);
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002268 kmem_cache_destroy(discard_entry_slab);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002269 kmem_cache_destroy(inmem_entry_slab);
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002270}