blob: 9f278d156d880f3153b9de18c008091a8cbc2b61 [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"
Jaegeuk Kim9e4ded32014-12-17 19:58:58 -080023#include "trace.h"
Namjae Jeon6ec178d2013-04-23 17:51:43 +090024#include <trace/events/f2fs.h>
Jaegeuk Kim351df4b2012-11-02 17:09:16 +090025
Changman Lee9a7f1432013-11-15 10:42:51 +090026#define __reverse_ffz(x) __reverse_ffs(~(x))
27
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +090028static struct kmem_cache *discard_entry_slab;
Chao Yu184a5cd2014-09-04 18:13:01 +080029static struct kmem_cache *sit_entry_set_slab;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -070030static struct kmem_cache *inmem_entry_slab;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +090031
Changman Lee9a7f1432013-11-15 10:42:51 +090032/*
33 * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since
34 * MSB and LSB are reversed in a byte by f2fs_set_bit.
35 */
36static inline unsigned long __reverse_ffs(unsigned long word)
37{
38 int num = 0;
39
40#if BITS_PER_LONG == 64
41 if ((word & 0xffffffff) == 0) {
42 num += 32;
43 word >>= 32;
44 }
45#endif
46 if ((word & 0xffff) == 0) {
47 num += 16;
48 word >>= 16;
49 }
50 if ((word & 0xff) == 0) {
51 num += 8;
52 word >>= 8;
53 }
54 if ((word & 0xf0) == 0)
55 num += 4;
56 else
57 word >>= 4;
58 if ((word & 0xc) == 0)
59 num += 2;
60 else
61 word >>= 2;
62 if ((word & 0x2) == 0)
63 num += 1;
64 return num;
65}
66
67/*
arter97e1c42042014-08-06 23:22:50 +090068 * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c because
Changman Lee9a7f1432013-11-15 10:42:51 +090069 * f2fs_set_bit makes MSB and LSB reversed in a byte.
70 * Example:
71 * LSB <--> MSB
72 * f2fs_set_bit(0, bitmap) => 0000 0001
73 * f2fs_set_bit(7, bitmap) => 1000 0000
74 */
75static unsigned long __find_rev_next_bit(const unsigned long *addr,
76 unsigned long size, unsigned long offset)
77{
78 const unsigned long *p = addr + BIT_WORD(offset);
79 unsigned long result = offset & ~(BITS_PER_LONG - 1);
80 unsigned long tmp;
81 unsigned long mask, submask;
82 unsigned long quot, rest;
83
84 if (offset >= size)
85 return size;
86
87 size -= result;
88 offset %= BITS_PER_LONG;
89 if (!offset)
90 goto aligned;
91
92 tmp = *(p++);
93 quot = (offset >> 3) << 3;
94 rest = offset & 0x7;
95 mask = ~0UL << quot;
96 submask = (unsigned char)(0xff << rest) >> rest;
97 submask <<= quot;
98 mask &= submask;
99 tmp &= mask;
100 if (size < BITS_PER_LONG)
101 goto found_first;
102 if (tmp)
103 goto found_middle;
104
105 size -= BITS_PER_LONG;
106 result += BITS_PER_LONG;
107aligned:
108 while (size & ~(BITS_PER_LONG-1)) {
109 tmp = *(p++);
110 if (tmp)
111 goto found_middle;
112 result += BITS_PER_LONG;
113 size -= BITS_PER_LONG;
114 }
115 if (!size)
116 return result;
117 tmp = *p;
118found_first:
119 tmp &= (~0UL >> (BITS_PER_LONG - size));
120 if (tmp == 0UL) /* Are any bits set? */
121 return result + size; /* Nope. */
122found_middle:
123 return result + __reverse_ffs(tmp);
124}
125
126static unsigned long __find_rev_next_zero_bit(const unsigned long *addr,
127 unsigned long size, unsigned long offset)
128{
129 const unsigned long *p = addr + BIT_WORD(offset);
130 unsigned long result = offset & ~(BITS_PER_LONG - 1);
131 unsigned long tmp;
132 unsigned long mask, submask;
133 unsigned long quot, rest;
134
135 if (offset >= size)
136 return size;
137
138 size -= result;
139 offset %= BITS_PER_LONG;
140 if (!offset)
141 goto aligned;
142
143 tmp = *(p++);
144 quot = (offset >> 3) << 3;
145 rest = offset & 0x7;
146 mask = ~(~0UL << quot);
147 submask = (unsigned char)~((unsigned char)(0xff << rest) >> rest);
148 submask <<= quot;
149 mask += submask;
150 tmp |= mask;
151 if (size < BITS_PER_LONG)
152 goto found_first;
153 if (~tmp)
154 goto found_middle;
155
156 size -= BITS_PER_LONG;
157 result += BITS_PER_LONG;
158aligned:
159 while (size & ~(BITS_PER_LONG - 1)) {
160 tmp = *(p++);
161 if (~tmp)
162 goto found_middle;
163 result += BITS_PER_LONG;
164 size -= BITS_PER_LONG;
165 }
166 if (!size)
167 return result;
168 tmp = *p;
169
170found_first:
171 tmp |= ~0UL << size;
172 if (tmp == ~0UL) /* Are any bits zero? */
173 return result + size; /* Nope. */
174found_middle:
175 return result + __reverse_ffz(tmp);
176}
177
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700178void register_inmem_page(struct inode *inode, struct page *page)
179{
180 struct f2fs_inode_info *fi = F2FS_I(inode);
181 struct inmem_pages *new;
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -0700182 int err;
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800183
Jaegeuk Kim0722b102014-12-05 11:58:02 -0800184 SetPagePrivate(page);
Jaegeuk Kim9e4ded32014-12-17 19:58:58 -0800185 f2fs_trace_pid(page);
Jaegeuk Kim0722b102014-12-05 11:58:02 -0800186
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700187 new = f2fs_kmem_cache_alloc(inmem_entry_slab, GFP_NOFS);
188
189 /* add atomic page indices to the list */
190 new->page = page;
191 INIT_LIST_HEAD(&new->list);
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800192retry:
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700193 /* increase reference count with clean state */
194 mutex_lock(&fi->inmem_lock);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -0700195 err = radix_tree_insert(&fi->inmem_root, page->index, new);
196 if (err == -EEXIST) {
197 mutex_unlock(&fi->inmem_lock);
198 kmem_cache_free(inmem_entry_slab, new);
199 return;
200 } else if (err) {
201 mutex_unlock(&fi->inmem_lock);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -0700202 goto retry;
203 }
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700204 get_page(page);
205 list_add_tail(&new->list, &fi->inmem_pages);
Jaegeuk Kim8dcf2ff72014-12-05 17:18:15 -0800206 inc_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700207 mutex_unlock(&fi->inmem_lock);
208}
209
210void commit_inmem_pages(struct inode *inode, bool abort)
211{
212 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
213 struct f2fs_inode_info *fi = F2FS_I(inode);
214 struct inmem_pages *cur, *tmp;
215 bool submit_bio = false;
216 struct f2fs_io_info fio = {
217 .type = DATA,
Jaegeuk Kim1e843712014-12-09 06:08:59 -0800218 .rw = WRITE_SYNC | REQ_PRIO,
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700219 };
220
Jaegeuk Kim03418452014-11-21 16:37:40 -0800221 /*
222 * The abort is true only when f2fs_evict_inode is called.
223 * Basically, the f2fs_evict_inode doesn't produce any data writes, so
224 * that we don't need to call f2fs_balance_fs.
225 * Otherwise, f2fs_gc in f2fs_balance_fs can wait forever until this
226 * inode becomes free by iget_locked in f2fs_iget.
227 */
Jaegeuk Kim70c640b2014-12-10 13:59:33 -0800228 if (!abort) {
Jaegeuk Kim03418452014-11-21 16:37:40 -0800229 f2fs_balance_fs(sbi);
Jaegeuk Kim70c640b2014-12-10 13:59:33 -0800230 f2fs_lock_op(sbi);
231 }
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700232
233 mutex_lock(&fi->inmem_lock);
234 list_for_each_entry_safe(cur, tmp, &fi->inmem_pages, list) {
Jaegeuk Kim70c640b2014-12-10 13:59:33 -0800235 if (!abort) {
236 lock_page(cur->page);
237 if (cur->page->mapping == inode->i_mapping) {
238 f2fs_wait_on_page_writeback(cur->page, DATA);
239 if (clear_page_dirty_for_io(cur->page))
240 inode_dec_dirty_pages(inode);
241 do_write_data_page(cur->page, &fio);
242 submit_bio = true;
243 }
244 f2fs_put_page(cur->page, 1);
245 } else {
246 put_page(cur->page);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700247 }
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -0700248 radix_tree_delete(&fi->inmem_root, cur->page->index);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700249 list_del(&cur->list);
250 kmem_cache_free(inmem_entry_slab, cur);
Jaegeuk Kim8dcf2ff72014-12-05 17:18:15 -0800251 dec_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700252 }
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700253 mutex_unlock(&fi->inmem_lock);
254
Jaegeuk Kim70c640b2014-12-10 13:59:33 -0800255 if (!abort) {
256 f2fs_unlock_op(sbi);
257 if (submit_bio)
258 f2fs_submit_merged_bio(sbi, DATA, WRITE);
259 }
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700260}
261
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900262/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900263 * This function balances dirty node and dentry pages.
264 * In addition, it controls garbage collection.
265 */
266void f2fs_balance_fs(struct f2fs_sb_info *sbi)
267{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900268 /*
Jaegeuk Kim029cd282012-12-21 17:20:21 +0900269 * We should do GC or end up with checkpoint, if there are so many dirty
270 * dir/node pages without enough free segments.
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900271 */
Jaegeuk Kim43727522013-02-04 15:11:17 +0900272 if (has_not_enough_free_secs(sbi, 0)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900273 mutex_lock(&sbi->gc_mutex);
Jaegeuk Kim408e9372013-01-03 17:55:52 +0900274 f2fs_gc(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900275 }
276}
277
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900278void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi)
279{
280 /* check the # of cached NAT entries and prefree segments */
281 if (try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK) ||
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -0800282 excess_prefree_segs(sbi) ||
Jaegeuk Kim88a70a62014-12-10 15:20:48 -0800283 !available_free_memory(sbi, INO_ENTRIES))
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900284 f2fs_sync_fs(sbi->sb, true);
285}
286
Gu Zheng2163d192014-04-27 14:21:33 +0800287static int issue_flush_thread(void *data)
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900288{
289 struct f2fs_sb_info *sbi = data;
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800290 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
291 wait_queue_head_t *q = &fcc->flush_wait_queue;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900292repeat:
293 if (kthread_should_stop())
294 return 0;
295
Gu Zheng721bd4d2014-09-05 18:31:00 +0800296 if (!llist_empty(&fcc->issue_list)) {
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900297 struct bio *bio = bio_alloc(GFP_NOIO, 0);
298 struct flush_cmd *cmd, *next;
299 int ret;
300
Gu Zheng721bd4d2014-09-05 18:31:00 +0800301 fcc->dispatch_list = llist_del_all(&fcc->issue_list);
302 fcc->dispatch_list = llist_reverse_order(fcc->dispatch_list);
303
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900304 bio->bi_bdev = sbi->sb->s_bdev;
305 ret = submit_bio_wait(WRITE_FLUSH, bio);
306
Gu Zheng721bd4d2014-09-05 18:31:00 +0800307 llist_for_each_entry_safe(cmd, next,
308 fcc->dispatch_list, llnode) {
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900309 cmd->ret = ret;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900310 complete(&cmd->wait);
311 }
Gu Zhenga4ed23f2014-04-11 17:49:35 +0800312 bio_put(bio);
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800313 fcc->dispatch_list = NULL;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900314 }
315
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800316 wait_event_interruptible(*q,
Gu Zheng721bd4d2014-09-05 18:31:00 +0800317 kthread_should_stop() || !llist_empty(&fcc->issue_list));
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900318 goto repeat;
319}
320
321int f2fs_issue_flush(struct f2fs_sb_info *sbi)
322{
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800323 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
Chao Yuadf8d902014-05-08 17:00:35 +0800324 struct flush_cmd cmd;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900325
Jaegeuk Kim24a9ee02014-07-25 17:46:10 -0700326 trace_f2fs_issue_flush(sbi->sb, test_opt(sbi, NOBARRIER),
327 test_opt(sbi, FLUSH_MERGE));
328
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700329 if (test_opt(sbi, NOBARRIER))
330 return 0;
331
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900332 if (!test_opt(sbi, FLUSH_MERGE))
333 return blkdev_issue_flush(sbi->sb->s_bdev, GFP_KERNEL, NULL);
334
Chao Yuadf8d902014-05-08 17:00:35 +0800335 init_completion(&cmd.wait);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900336
Gu Zheng721bd4d2014-09-05 18:31:00 +0800337 llist_add(&cmd.llnode, &fcc->issue_list);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900338
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800339 if (!fcc->dispatch_list)
340 wake_up(&fcc->flush_wait_queue);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900341
Chao Yuadf8d902014-05-08 17:00:35 +0800342 wait_for_completion(&cmd.wait);
343
344 return cmd.ret;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900345}
346
Gu Zheng2163d192014-04-27 14:21:33 +0800347int create_flush_cmd_control(struct f2fs_sb_info *sbi)
348{
349 dev_t dev = sbi->sb->s_bdev->bd_dev;
350 struct flush_cmd_control *fcc;
351 int err = 0;
352
353 fcc = kzalloc(sizeof(struct flush_cmd_control), GFP_KERNEL);
354 if (!fcc)
355 return -ENOMEM;
Gu Zheng2163d192014-04-27 14:21:33 +0800356 init_waitqueue_head(&fcc->flush_wait_queue);
Gu Zheng721bd4d2014-09-05 18:31:00 +0800357 init_llist_head(&fcc->issue_list);
Chao Yu6b2920a2014-07-07 11:21:59 +0800358 SM_I(sbi)->cmd_control_info = fcc;
Gu Zheng2163d192014-04-27 14:21:33 +0800359 fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi,
360 "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev));
361 if (IS_ERR(fcc->f2fs_issue_flush)) {
362 err = PTR_ERR(fcc->f2fs_issue_flush);
363 kfree(fcc);
Chao Yu6b2920a2014-07-07 11:21:59 +0800364 SM_I(sbi)->cmd_control_info = NULL;
Gu Zheng2163d192014-04-27 14:21:33 +0800365 return err;
366 }
Gu Zheng2163d192014-04-27 14:21:33 +0800367
368 return err;
369}
370
371void destroy_flush_cmd_control(struct f2fs_sb_info *sbi)
372{
Chao Yu6b2920a2014-07-07 11:21:59 +0800373 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
Gu Zheng2163d192014-04-27 14:21:33 +0800374
375 if (fcc && fcc->f2fs_issue_flush)
376 kthread_stop(fcc->f2fs_issue_flush);
377 kfree(fcc);
Chao Yu6b2920a2014-07-07 11:21:59 +0800378 SM_I(sbi)->cmd_control_info = NULL;
Gu Zheng2163d192014-04-27 14:21:33 +0800379}
380
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900381static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
382 enum dirty_type dirty_type)
383{
384 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
385
386 /* need not be added */
387 if (IS_CURSEG(sbi, segno))
388 return;
389
390 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
391 dirty_i->nr_dirty[dirty_type]++;
392
393 if (dirty_type == DIRTY) {
394 struct seg_entry *sentry = get_seg_entry(sbi, segno);
Changman Lee4625d6a2013-10-25 17:31:57 +0900395 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900396
Jaegeuk Kimec325b52014-09-02 16:24:11 -0700397 if (unlikely(t >= DIRTY)) {
398 f2fs_bug_on(sbi, 1);
399 return;
400 }
Changman Lee4625d6a2013-10-25 17:31:57 +0900401 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
402 dirty_i->nr_dirty[t]++;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900403 }
404}
405
406static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
407 enum dirty_type dirty_type)
408{
409 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
410
411 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
412 dirty_i->nr_dirty[dirty_type]--;
413
414 if (dirty_type == DIRTY) {
Changman Lee4625d6a2013-10-25 17:31:57 +0900415 struct seg_entry *sentry = get_seg_entry(sbi, segno);
416 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900417
Changman Lee4625d6a2013-10-25 17:31:57 +0900418 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
419 dirty_i->nr_dirty[t]--;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900420
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +0900421 if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0)
422 clear_bit(GET_SECNO(sbi, segno),
423 dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900424 }
425}
426
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900427/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900428 * Should not occur error such as -ENOMEM.
429 * Adding dirty entry into seglist is not critical operation.
430 * If a given segment is one of current working segments, it won't be added.
431 */
Haicheng Li8d8451a2013-06-13 16:59:28 +0800432static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900433{
434 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
435 unsigned short valid_blocks;
436
437 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
438 return;
439
440 mutex_lock(&dirty_i->seglist_lock);
441
442 valid_blocks = get_valid_blocks(sbi, segno, 0);
443
444 if (valid_blocks == 0) {
445 __locate_dirty_segment(sbi, segno, PRE);
446 __remove_dirty_segment(sbi, segno, DIRTY);
447 } else if (valid_blocks < sbi->blocks_per_seg) {
448 __locate_dirty_segment(sbi, segno, DIRTY);
449 } else {
450 /* Recovery routine with SSR needs this */
451 __remove_dirty_segment(sbi, segno, DIRTY);
452 }
453
454 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900455}
456
Jaegeuk Kim1e87a782014-04-15 13:57:55 +0900457static int f2fs_issue_discard(struct f2fs_sb_info *sbi,
Jaegeuk Kim37208872013-11-12 16:55:17 +0900458 block_t blkstart, block_t blklen)
459{
Chao Yu55cf9cb2014-09-15 18:01:10 +0800460 sector_t start = SECTOR_FROM_BLOCK(blkstart);
461 sector_t len = SECTOR_FROM_BLOCK(blklen);
Jaegeuk Kim1661d072013-11-12 17:01:00 +0900462 trace_f2fs_issue_discard(sbi->sb, blkstart, blklen);
Jaegeuk Kim1e87a782014-04-15 13:57:55 +0900463 return blkdev_issue_discard(sbi->sb->s_bdev, start, len, GFP_NOFS, 0);
464}
465
Jaegeuk Kimcf2271e2014-07-25 15:47:25 -0700466void discard_next_dnode(struct f2fs_sb_info *sbi, block_t blkaddr)
Jaegeuk Kim1e87a782014-04-15 13:57:55 +0900467{
Jaegeuk Kim1e87a782014-04-15 13:57:55 +0900468 if (f2fs_issue_discard(sbi, blkaddr, 1)) {
469 struct page *page = grab_meta_page(sbi, blkaddr);
470 /* zero-filled page */
471 set_page_dirty(page);
472 f2fs_put_page(page, 1);
473 }
Jaegeuk Kim37208872013-11-12 16:55:17 +0900474}
475
Jaegeuk Kimadf49832014-10-28 22:27:59 -0700476static void __add_discard_entry(struct f2fs_sb_info *sbi,
477 struct cp_control *cpc, unsigned int start, unsigned int end)
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900478{
479 struct list_head *head = &SM_I(sbi)->discard_list;
Jaegeuk Kimadf49832014-10-28 22:27:59 -0700480 struct discard_entry *new, *last;
481
482 if (!list_empty(head)) {
483 last = list_last_entry(head, struct discard_entry, list);
484 if (START_BLOCK(sbi, cpc->trim_start) + start ==
485 last->blkaddr + last->len) {
486 last->len += end - start;
487 goto done;
488 }
489 }
490
491 new = f2fs_kmem_cache_alloc(discard_entry_slab, GFP_NOFS);
492 INIT_LIST_HEAD(&new->list);
493 new->blkaddr = START_BLOCK(sbi, cpc->trim_start) + start;
494 new->len = end - start;
495 list_add_tail(&new->list, head);
496done:
497 SM_I(sbi)->nr_discards += end - start;
498 cpc->trimmed += end - start;
499}
500
501static void add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc)
502{
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900503 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
504 int max_blocks = sbi->blocks_per_seg;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700505 struct seg_entry *se = get_seg_entry(sbi, cpc->trim_start);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900506 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
507 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
508 unsigned long dmap[entries];
509 unsigned int start = 0, end = -1;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700510 bool force = (cpc->reason == CP_DISCARD);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900511 int i;
512
Jaegeuk Kimd7bc2482014-12-12 13:53:41 -0800513 if (!force && (!test_opt(sbi, DISCARD) ||
514 SM_I(sbi)->nr_discards >= SM_I(sbi)->max_discards))
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900515 return;
516
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700517 if (force && !se->valid_blocks) {
518 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
519 /*
520 * if this segment is registered in the prefree list, then
521 * we should skip adding a discard candidate, and let the
522 * checkpoint do that later.
523 */
524 mutex_lock(&dirty_i->seglist_lock);
525 if (test_bit(cpc->trim_start, dirty_i->dirty_segmap[PRE])) {
526 mutex_unlock(&dirty_i->seglist_lock);
527 cpc->trimmed += sbi->blocks_per_seg;
528 return;
529 }
530 mutex_unlock(&dirty_i->seglist_lock);
531
Jaegeuk Kimadf49832014-10-28 22:27:59 -0700532 __add_discard_entry(sbi, cpc, 0, sbi->blocks_per_seg);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700533 return;
534 }
535
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900536 /* zero block will be discarded through the prefree list */
537 if (!se->valid_blocks || se->valid_blocks == max_blocks)
538 return;
539
540 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
541 for (i = 0; i < entries; i++)
Jaegeuk Kimd7bc2482014-12-12 13:53:41 -0800542 dmap[i] = force ? ~ckpt_map[i] :
543 (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900544
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700545 while (force || SM_I(sbi)->nr_discards <= SM_I(sbi)->max_discards) {
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900546 start = __find_rev_next_bit(dmap, max_blocks, end + 1);
547 if (start >= max_blocks)
548 break;
549
550 end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
551
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700552 if (end - start < cpc->trim_minlen)
553 continue;
554
Jaegeuk Kimadf49832014-10-28 22:27:59 -0700555 __add_discard_entry(sbi, cpc, start, end);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900556 }
557}
558
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700559void release_discard_addrs(struct f2fs_sb_info *sbi)
560{
561 struct list_head *head = &(SM_I(sbi)->discard_list);
562 struct discard_entry *entry, *this;
563
564 /* drop caches */
565 list_for_each_entry_safe(entry, this, head, list) {
566 list_del(&entry->list);
567 kmem_cache_free(discard_entry_slab, entry);
568 }
569}
570
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900571/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900572 * Should call clear_prefree_segments after checkpoint is done.
573 */
574static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
575{
576 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Chao Yub65ee142014-08-04 10:10:07 +0800577 unsigned int segno;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900578
579 mutex_lock(&dirty_i->seglist_lock);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700580 for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900581 __set_test_and_free(sbi, segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900582 mutex_unlock(&dirty_i->seglist_lock);
583}
584
585void clear_prefree_segments(struct f2fs_sb_info *sbi)
586{
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900587 struct list_head *head = &(SM_I(sbi)->discard_list);
Chao Yu2d7b8222014-03-29 11:33:17 +0800588 struct discard_entry *entry, *this;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900589 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Changman Lee29e59c12013-11-11 09:24:37 +0900590 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
Changman Lee29e59c12013-11-11 09:24:37 +0900591 unsigned int start = 0, end = -1;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900592
593 mutex_lock(&dirty_i->seglist_lock);
Changman Lee29e59c12013-11-11 09:24:37 +0900594
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900595 while (1) {
Changman Lee29e59c12013-11-11 09:24:37 +0900596 int i;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700597 start = find_next_bit(prefree_map, MAIN_SEGS(sbi), end + 1);
598 if (start >= MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900599 break;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700600 end = find_next_zero_bit(prefree_map, MAIN_SEGS(sbi),
601 start + 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900602
Changman Lee29e59c12013-11-11 09:24:37 +0900603 for (i = start; i < end; i++)
604 clear_bit(i, prefree_map);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900605
Changman Lee29e59c12013-11-11 09:24:37 +0900606 dirty_i->nr_dirty[PRE] -= end - start;
607
608 if (!test_opt(sbi, DISCARD))
609 continue;
610
Jaegeuk Kim37208872013-11-12 16:55:17 +0900611 f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
612 (end - start) << sbi->log_blocks_per_seg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900613 }
614 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900615
616 /* send small discards */
Chao Yu2d7b8222014-03-29 11:33:17 +0800617 list_for_each_entry_safe(entry, this, head, list) {
Jaegeuk Kim37208872013-11-12 16:55:17 +0900618 f2fs_issue_discard(sbi, entry->blkaddr, entry->len);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900619 list_del(&entry->list);
620 SM_I(sbi)->nr_discards -= entry->len;
621 kmem_cache_free(discard_entry_slab, entry);
622 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900623}
624
Chao Yu184a5cd2014-09-04 18:13:01 +0800625static bool __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900626{
627 struct sit_info *sit_i = SIT_I(sbi);
Chao Yu184a5cd2014-09-04 18:13:01 +0800628
629 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900630 sit_i->dirty_sentries++;
Chao Yu184a5cd2014-09-04 18:13:01 +0800631 return false;
632 }
633
634 return true;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900635}
636
637static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
638 unsigned int segno, int modified)
639{
640 struct seg_entry *se = get_seg_entry(sbi, segno);
641 se->type = type;
642 if (modified)
643 __mark_sit_entry_dirty(sbi, segno);
644}
645
646static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
647{
648 struct seg_entry *se;
649 unsigned int segno, offset;
650 long int new_vblocks;
651
652 segno = GET_SEGNO(sbi, blkaddr);
653
654 se = get_seg_entry(sbi, segno);
655 new_vblocks = se->valid_blocks + del;
Jaegeuk Kim491c0852014-02-04 13:01:10 +0900656 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900657
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700658 f2fs_bug_on(sbi, (new_vblocks >> (sizeof(unsigned short) << 3) ||
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900659 (new_vblocks > sbi->blocks_per_seg)));
660
661 se->valid_blocks = new_vblocks;
662 se->mtime = get_mtime(sbi);
663 SIT_I(sbi)->max_mtime = se->mtime;
664
665 /* Update valid block bitmap */
666 if (del > 0) {
Gu Zheng52aca072014-10-20 17:45:51 +0800667 if (f2fs_test_and_set_bit(offset, se->cur_valid_map))
Jaegeuk Kim05796762014-09-02 16:05:00 -0700668 f2fs_bug_on(sbi, 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900669 } else {
Gu Zheng52aca072014-10-20 17:45:51 +0800670 if (!f2fs_test_and_clear_bit(offset, se->cur_valid_map))
Jaegeuk Kim05796762014-09-02 16:05:00 -0700671 f2fs_bug_on(sbi, 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900672 }
673 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
674 se->ckpt_valid_blocks += del;
675
676 __mark_sit_entry_dirty(sbi, segno);
677
678 /* update total number of valid blocks to be written in ckpt area */
679 SIT_I(sbi)->written_valid_blocks += del;
680
681 if (sbi->segs_per_sec > 1)
682 get_sec_entry(sbi, segno)->valid_blocks += del;
683}
684
Jaegeuk Kim5e443812014-01-28 12:22:14 +0900685void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900686{
Jaegeuk Kim5e443812014-01-28 12:22:14 +0900687 update_sit_entry(sbi, new, 1);
688 if (GET_SEGNO(sbi, old) != NULL_SEGNO)
689 update_sit_entry(sbi, old, -1);
690
691 locate_dirty_segment(sbi, GET_SEGNO(sbi, old));
692 locate_dirty_segment(sbi, GET_SEGNO(sbi, new));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900693}
694
695void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
696{
697 unsigned int segno = GET_SEGNO(sbi, addr);
698 struct sit_info *sit_i = SIT_I(sbi);
699
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700700 f2fs_bug_on(sbi, addr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900701 if (addr == NEW_ADDR)
702 return;
703
704 /* add it into sit main buffer */
705 mutex_lock(&sit_i->sentry_lock);
706
707 update_sit_entry(sbi, addr, -1);
708
709 /* add it into dirty seglist */
710 locate_dirty_segment(sbi, segno);
711
712 mutex_unlock(&sit_i->sentry_lock);
713}
714
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900715/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900716 * This function should be resided under the curseg_mutex lock
717 */
718static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
Haicheng Lie79efe32013-06-13 16:59:27 +0800719 struct f2fs_summary *sum)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900720{
721 struct curseg_info *curseg = CURSEG_I(sbi, type);
722 void *addr = curseg->sum_blk;
Haicheng Lie79efe32013-06-13 16:59:27 +0800723 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900724 memcpy(addr, sum, sizeof(struct f2fs_summary));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900725}
726
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900727/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900728 * Calculate the number of current summary pages for writing
729 */
Chao Yu3fa06d72014-12-09 14:21:46 +0800730int npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900731{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900732 int valid_sum_count = 0;
Fan Li9a479382013-10-29 16:21:47 +0800733 int i, sum_in_page;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900734
735 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
736 if (sbi->ckpt->alloc_type[i] == SSR)
737 valid_sum_count += sbi->blocks_per_seg;
Chao Yu3fa06d72014-12-09 14:21:46 +0800738 else {
739 if (for_ra)
740 valid_sum_count += le16_to_cpu(
741 F2FS_CKPT(sbi)->cur_data_blkoff[i]);
742 else
743 valid_sum_count += curseg_blkoff(sbi, i);
744 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900745 }
746
Fan Li9a479382013-10-29 16:21:47 +0800747 sum_in_page = (PAGE_CACHE_SIZE - 2 * SUM_JOURNAL_SIZE -
748 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
749 if (valid_sum_count <= sum_in_page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900750 return 1;
Fan Li9a479382013-10-29 16:21:47 +0800751 else if ((valid_sum_count - sum_in_page) <=
752 (PAGE_CACHE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900753 return 2;
754 return 3;
755}
756
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900757/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900758 * Caller should put this summary page
759 */
760struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
761{
762 return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
763}
764
765static void write_sum_page(struct f2fs_sb_info *sbi,
766 struct f2fs_summary_block *sum_blk, block_t blk_addr)
767{
768 struct page *page = grab_meta_page(sbi, blk_addr);
769 void *kaddr = page_address(page);
770 memcpy(kaddr, sum_blk, PAGE_CACHE_SIZE);
771 set_page_dirty(page);
772 f2fs_put_page(page, 1);
773}
774
Jaegeuk Kim60374682013-03-31 13:58:51 +0900775static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
776{
777 struct curseg_info *curseg = CURSEG_I(sbi, type);
Haicheng Li81fb5e82013-05-14 18:20:28 +0800778 unsigned int segno = curseg->segno + 1;
Jaegeuk Kim60374682013-03-31 13:58:51 +0900779 struct free_segmap_info *free_i = FREE_I(sbi);
780
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700781 if (segno < MAIN_SEGS(sbi) && segno % sbi->segs_per_sec)
Haicheng Li81fb5e82013-05-14 18:20:28 +0800782 return !test_bit(segno, free_i->free_segmap);
Jaegeuk Kim60374682013-03-31 13:58:51 +0900783 return 0;
784}
785
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900786/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900787 * Find a new segment from the free segments bitmap to right order
788 * This function should be returned with success, otherwise BUG
789 */
790static void get_new_segment(struct f2fs_sb_info *sbi,
791 unsigned int *newseg, bool new_sec, int dir)
792{
793 struct free_segmap_info *free_i = FREE_I(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900794 unsigned int segno, secno, zoneno;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700795 unsigned int total_zones = MAIN_SECS(sbi) / sbi->secs_per_zone;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900796 unsigned int hint = *newseg / sbi->segs_per_sec;
797 unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
798 unsigned int left_start = hint;
799 bool init = true;
800 int go_left = 0;
801 int i;
802
803 write_lock(&free_i->segmap_lock);
804
805 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
806 segno = find_next_zero_bit(free_i->free_segmap,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700807 MAIN_SEGS(sbi), *newseg + 1);
Jaegeuk Kim33afa7f2013-03-31 12:59:53 +0900808 if (segno - *newseg < sbi->segs_per_sec -
809 (*newseg % sbi->segs_per_sec))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900810 goto got_it;
811 }
812find_other_zone:
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700813 secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint);
814 if (secno >= MAIN_SECS(sbi)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900815 if (dir == ALLOC_RIGHT) {
816 secno = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700817 MAIN_SECS(sbi), 0);
818 f2fs_bug_on(sbi, secno >= MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900819 } else {
820 go_left = 1;
821 left_start = hint - 1;
822 }
823 }
824 if (go_left == 0)
825 goto skip_left;
826
827 while (test_bit(left_start, free_i->free_secmap)) {
828 if (left_start > 0) {
829 left_start--;
830 continue;
831 }
832 left_start = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700833 MAIN_SECS(sbi), 0);
834 f2fs_bug_on(sbi, left_start >= MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900835 break;
836 }
837 secno = left_start;
838skip_left:
839 hint = secno;
840 segno = secno * sbi->segs_per_sec;
841 zoneno = secno / sbi->secs_per_zone;
842
843 /* give up on finding another zone */
844 if (!init)
845 goto got_it;
846 if (sbi->secs_per_zone == 1)
847 goto got_it;
848 if (zoneno == old_zoneno)
849 goto got_it;
850 if (dir == ALLOC_LEFT) {
851 if (!go_left && zoneno + 1 >= total_zones)
852 goto got_it;
853 if (go_left && zoneno == 0)
854 goto got_it;
855 }
856 for (i = 0; i < NR_CURSEG_TYPE; i++)
857 if (CURSEG_I(sbi, i)->zone == zoneno)
858 break;
859
860 if (i < NR_CURSEG_TYPE) {
861 /* zone is in user, try another */
862 if (go_left)
863 hint = zoneno * sbi->secs_per_zone - 1;
864 else if (zoneno + 1 >= total_zones)
865 hint = 0;
866 else
867 hint = (zoneno + 1) * sbi->secs_per_zone;
868 init = false;
869 goto find_other_zone;
870 }
871got_it:
872 /* set it as dirty segment in free segmap */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700873 f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900874 __set_inuse(sbi, segno);
875 *newseg = segno;
876 write_unlock(&free_i->segmap_lock);
877}
878
879static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
880{
881 struct curseg_info *curseg = CURSEG_I(sbi, type);
882 struct summary_footer *sum_footer;
883
884 curseg->segno = curseg->next_segno;
885 curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
886 curseg->next_blkoff = 0;
887 curseg->next_segno = NULL_SEGNO;
888
889 sum_footer = &(curseg->sum_blk->footer);
890 memset(sum_footer, 0, sizeof(struct summary_footer));
891 if (IS_DATASEG(type))
892 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
893 if (IS_NODESEG(type))
894 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
895 __set_sit_entry_type(sbi, type, curseg->segno, modified);
896}
897
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900898/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900899 * Allocate a current working segment.
900 * This function always allocates a free segment in LFS manner.
901 */
902static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
903{
904 struct curseg_info *curseg = CURSEG_I(sbi, type);
905 unsigned int segno = curseg->segno;
906 int dir = ALLOC_LEFT;
907
908 write_sum_page(sbi, curseg->sum_blk,
Haicheng Li81fb5e82013-05-14 18:20:28 +0800909 GET_SUM_BLOCK(sbi, segno));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900910 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
911 dir = ALLOC_RIGHT;
912
913 if (test_opt(sbi, NOHEAP))
914 dir = ALLOC_RIGHT;
915
916 get_new_segment(sbi, &segno, new_sec, dir);
917 curseg->next_segno = segno;
918 reset_curseg(sbi, type, 1);
919 curseg->alloc_type = LFS;
920}
921
922static void __next_free_blkoff(struct f2fs_sb_info *sbi,
923 struct curseg_info *seg, block_t start)
924{
925 struct seg_entry *se = get_seg_entry(sbi, seg->segno);
Changman Leee81c93c2013-11-15 13:21:16 +0900926 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
927 unsigned long target_map[entries];
928 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
929 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
930 int i, pos;
931
932 for (i = 0; i < entries; i++)
933 target_map[i] = ckpt_map[i] | cur_map[i];
934
935 pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
936
937 seg->next_blkoff = pos;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900938}
939
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900940/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900941 * If a segment is written by LFS manner, next block offset is just obtained
942 * by increasing the current block offset. However, if a segment is written by
943 * SSR manner, next block offset obtained by calling __next_free_blkoff
944 */
945static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
946 struct curseg_info *seg)
947{
948 if (seg->alloc_type == SSR)
949 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
950 else
951 seg->next_blkoff++;
952}
953
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900954/*
arter97e1c42042014-08-06 23:22:50 +0900955 * This function always allocates a used segment(from dirty seglist) by SSR
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900956 * manner, so it should recover the existing segment information of valid blocks
957 */
958static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
959{
960 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
961 struct curseg_info *curseg = CURSEG_I(sbi, type);
962 unsigned int new_segno = curseg->next_segno;
963 struct f2fs_summary_block *sum_node;
964 struct page *sum_page;
965
966 write_sum_page(sbi, curseg->sum_blk,
967 GET_SUM_BLOCK(sbi, curseg->segno));
968 __set_test_and_inuse(sbi, new_segno);
969
970 mutex_lock(&dirty_i->seglist_lock);
971 __remove_dirty_segment(sbi, new_segno, PRE);
972 __remove_dirty_segment(sbi, new_segno, DIRTY);
973 mutex_unlock(&dirty_i->seglist_lock);
974
975 reset_curseg(sbi, type, 1);
976 curseg->alloc_type = SSR;
977 __next_free_blkoff(sbi, curseg, 0);
978
979 if (reuse) {
980 sum_page = get_sum_page(sbi, new_segno);
981 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
982 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
983 f2fs_put_page(sum_page, 1);
984 }
985}
986
Jaegeuk Kim43727522013-02-04 15:11:17 +0900987static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
988{
989 struct curseg_info *curseg = CURSEG_I(sbi, type);
990 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
991
992 if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0))
993 return v_ops->get_victim(sbi,
994 &(curseg)->next_segno, BG_GC, type, SSR);
995
996 /* For data segments, let's do SSR more intensively */
997 for (; type >= CURSEG_HOT_DATA; type--)
998 if (v_ops->get_victim(sbi, &(curseg)->next_segno,
999 BG_GC, type, SSR))
1000 return 1;
1001 return 0;
1002}
1003
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001004/*
1005 * flush out current segment and replace it with new segment
1006 * This function should be returned with success, otherwise BUG
1007 */
1008static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
1009 int type, bool force)
1010{
1011 struct curseg_info *curseg = CURSEG_I(sbi, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001012
Gu Zheng7b405272013-08-19 09:41:15 +08001013 if (force)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001014 new_curseg(sbi, type, true);
Gu Zheng7b405272013-08-19 09:41:15 +08001015 else if (type == CURSEG_WARM_NODE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001016 new_curseg(sbi, type, false);
Jaegeuk Kim60374682013-03-31 13:58:51 +09001017 else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
1018 new_curseg(sbi, type, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001019 else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
1020 change_curseg(sbi, type, true);
1021 else
1022 new_curseg(sbi, type, false);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +09001023
1024 stat_inc_seg_type(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001025}
1026
Jaegeuk Kim38aa0882015-01-05 16:02:20 -08001027static void __allocate_new_segments(struct f2fs_sb_info *sbi, int type)
1028{
1029 struct curseg_info *curseg = CURSEG_I(sbi, type);
1030 unsigned int old_segno;
1031
1032 old_segno = curseg->segno;
1033 SIT_I(sbi)->s_ops->allocate_segment(sbi, type, true);
1034 locate_dirty_segment(sbi, old_segno);
1035}
1036
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001037void allocate_new_segments(struct f2fs_sb_info *sbi)
1038{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001039 int i;
1040
Jaegeuk Kim38aa0882015-01-05 16:02:20 -08001041 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++)
1042 __allocate_new_segments(sbi, i);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001043}
1044
1045static const struct segment_allocation default_salloc_ops = {
1046 .allocate_segment = allocate_segment_by_default,
1047};
1048
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001049int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range)
1050{
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001051 __u64 start = range->start >> sbi->log_blocksize;
1052 __u64 end = start + (range->len >> sbi->log_blocksize) - 1;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001053 unsigned int start_segno, end_segno;
1054 struct cp_control cpc;
1055
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001056 if (range->minlen > SEGMENT_SIZE(sbi) || start >= MAX_BLKADDR(sbi) ||
1057 range->len < sbi->blocksize)
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001058 return -EINVAL;
1059
Jan Kara9bd27ae2014-10-21 14:07:33 +02001060 cpc.trimmed = 0;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001061 if (end <= MAIN_BLKADDR(sbi))
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001062 goto out;
1063
1064 /* start/end segment number in main_area */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001065 start_segno = (start <= MAIN_BLKADDR(sbi)) ? 0 : GET_SEGNO(sbi, start);
1066 end_segno = (end >= MAX_BLKADDR(sbi)) ? MAIN_SEGS(sbi) - 1 :
1067 GET_SEGNO(sbi, end);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001068 cpc.reason = CP_DISCARD;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001069 cpc.trim_minlen = range->minlen >> sbi->log_blocksize;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001070
1071 /* do checkpoint to issue discard commands safely */
Jaegeuk Kimbba681c2015-01-26 17:41:23 -08001072 for (; start_segno <= end_segno; start_segno = cpc.trim_end + 1) {
1073 cpc.trim_start = start_segno;
1074 cpc.trim_end = min_t(unsigned int, rounddown(start_segno +
1075 BATCHED_TRIM_SEGMENTS(sbi),
1076 sbi->segs_per_sec) - 1, end_segno);
1077
1078 mutex_lock(&sbi->gc_mutex);
1079 write_checkpoint(sbi, &cpc);
1080 mutex_unlock(&sbi->gc_mutex);
1081 }
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001082out:
1083 range->len = cpc.trimmed << sbi->log_blocksize;
1084 return 0;
1085}
1086
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001087static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
1088{
1089 struct curseg_info *curseg = CURSEG_I(sbi, type);
1090 if (curseg->next_blkoff < sbi->blocks_per_seg)
1091 return true;
1092 return false;
1093}
1094
1095static int __get_segment_type_2(struct page *page, enum page_type p_type)
1096{
1097 if (p_type == DATA)
1098 return CURSEG_HOT_DATA;
1099 else
1100 return CURSEG_HOT_NODE;
1101}
1102
1103static int __get_segment_type_4(struct page *page, enum page_type p_type)
1104{
1105 if (p_type == DATA) {
1106 struct inode *inode = page->mapping->host;
1107
1108 if (S_ISDIR(inode->i_mode))
1109 return CURSEG_HOT_DATA;
1110 else
1111 return CURSEG_COLD_DATA;
1112 } else {
Jaegeuk Kima344b9f2014-11-05 20:05:53 -08001113 if (IS_DNODE(page) && is_cold_node(page))
1114 return CURSEG_WARM_NODE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001115 else
1116 return CURSEG_COLD_NODE;
1117 }
1118}
1119
1120static int __get_segment_type_6(struct page *page, enum page_type p_type)
1121{
1122 if (p_type == DATA) {
1123 struct inode *inode = page->mapping->host;
1124
1125 if (S_ISDIR(inode->i_mode))
1126 return CURSEG_HOT_DATA;
Jaegeuk Kim354a3392013-06-14 08:52:35 +09001127 else if (is_cold_data(page) || file_is_cold(inode))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001128 return CURSEG_COLD_DATA;
1129 else
1130 return CURSEG_WARM_DATA;
1131 } else {
1132 if (IS_DNODE(page))
1133 return is_cold_node(page) ? CURSEG_WARM_NODE :
1134 CURSEG_HOT_NODE;
1135 else
1136 return CURSEG_COLD_NODE;
1137 }
1138}
1139
1140static int __get_segment_type(struct page *page, enum page_type p_type)
1141{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001142 switch (F2FS_P_SB(page)->active_logs) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001143 case 2:
1144 return __get_segment_type_2(page, p_type);
1145 case 4:
1146 return __get_segment_type_4(page, p_type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001147 }
Jaegeuk Kim12a67142012-12-21 11:47:05 +09001148 /* NR_CURSEG_TYPE(6) logs by default */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001149 f2fs_bug_on(F2FS_P_SB(page),
1150 F2FS_P_SB(page)->active_logs != NR_CURSEG_TYPE);
Jaegeuk Kim12a67142012-12-21 11:47:05 +09001151 return __get_segment_type_6(page, p_type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001152}
1153
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001154void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
1155 block_t old_blkaddr, block_t *new_blkaddr,
1156 struct f2fs_summary *sum, int type)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001157{
1158 struct sit_info *sit_i = SIT_I(sbi);
1159 struct curseg_info *curseg;
Jaegeuk Kim38aa0882015-01-05 16:02:20 -08001160 bool direct_io = (type == CURSEG_DIRECT_IO);
1161
1162 type = direct_io ? CURSEG_WARM_DATA : type;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001163
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001164 curseg = CURSEG_I(sbi, type);
1165
1166 mutex_lock(&curseg->curseg_mutex);
1167
Jaegeuk Kim38aa0882015-01-05 16:02:20 -08001168 /* direct_io'ed data is aligned to the segment for better performance */
1169 if (direct_io && curseg->next_blkoff)
1170 __allocate_new_segments(sbi, type);
1171
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001172 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001173
1174 /*
1175 * __add_sum_entry should be resided under the curseg_mutex
1176 * because, this function updates a summary entry in the
1177 * current summary block.
1178 */
Haicheng Lie79efe32013-06-13 16:59:27 +08001179 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001180
1181 mutex_lock(&sit_i->sentry_lock);
1182 __refresh_next_blkoff(sbi, curseg);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +09001183
1184 stat_inc_block_count(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001185
Jaegeuk Kim5e443812014-01-28 12:22:14 +09001186 if (!__has_curseg_space(sbi, type))
1187 sit_i->s_ops->allocate_segment(sbi, type, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001188 /*
1189 * SIT information should be updated before segment allocation,
1190 * since SSR needs latest valid block information.
1191 */
1192 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
Jaegeuk Kim5e443812014-01-28 12:22:14 +09001193
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001194 mutex_unlock(&sit_i->sentry_lock);
1195
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001196 if (page && IS_NODESEG(type))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001197 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
1198
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001199 mutex_unlock(&curseg->curseg_mutex);
1200}
1201
1202static void do_write_page(struct f2fs_sb_info *sbi, struct page *page,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001203 struct f2fs_summary *sum,
1204 struct f2fs_io_info *fio)
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001205{
1206 int type = __get_segment_type(page, fio->type);
1207
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001208 allocate_data_block(sbi, page, fio->blk_addr, &fio->blk_addr, sum, type);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001209
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001210 /* writeout dirty page into bdev */
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001211 f2fs_submit_page_mbio(sbi, page, fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001212}
1213
Jaegeuk Kim577e3492013-01-24 19:56:11 +09001214void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001215{
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001216 struct f2fs_io_info fio = {
1217 .type = META,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001218 .rw = WRITE_SYNC | REQ_META | REQ_PRIO,
1219 .blk_addr = page->index,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001220 };
1221
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001222 set_page_writeback(page);
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001223 f2fs_submit_page_mbio(sbi, page, &fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001224}
1225
1226void write_node_page(struct f2fs_sb_info *sbi, struct page *page,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001227 unsigned int nid, struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001228{
1229 struct f2fs_summary sum;
1230 set_summary(&sum, nid, 0, 0);
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001231 do_write_page(sbi, page, &sum, fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001232}
1233
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001234void write_data_page(struct page *page, struct dnode_of_data *dn,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001235 struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001236{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001237 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001238 struct f2fs_summary sum;
1239 struct node_info ni;
1240
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001241 f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001242 get_node_info(sbi, dn->nid, &ni);
1243 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001244 do_write_page(sbi, page, &sum, fio);
Jaegeuk Kime1509cf2014-12-30 22:57:55 -08001245 dn->data_blkaddr = fio->blk_addr;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001246}
1247
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001248void rewrite_data_page(struct page *page, struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001249{
Changman Leeb9a2c252014-12-24 02:16:54 +09001250 stat_inc_inplace_blocks(F2FS_P_SB(page));
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001251 f2fs_submit_page_mbio(F2FS_P_SB(page), page, fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001252}
1253
1254void recover_data_page(struct f2fs_sb_info *sbi,
1255 struct page *page, struct f2fs_summary *sum,
1256 block_t old_blkaddr, block_t new_blkaddr)
1257{
1258 struct sit_info *sit_i = SIT_I(sbi);
1259 struct curseg_info *curseg;
1260 unsigned int segno, old_cursegno;
1261 struct seg_entry *se;
1262 int type;
1263
1264 segno = GET_SEGNO(sbi, new_blkaddr);
1265 se = get_seg_entry(sbi, segno);
1266 type = se->type;
1267
1268 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
1269 if (old_blkaddr == NULL_ADDR)
1270 type = CURSEG_COLD_DATA;
1271 else
1272 type = CURSEG_WARM_DATA;
1273 }
1274 curseg = CURSEG_I(sbi, type);
1275
1276 mutex_lock(&curseg->curseg_mutex);
1277 mutex_lock(&sit_i->sentry_lock);
1278
1279 old_cursegno = curseg->segno;
1280
1281 /* change the current segment */
1282 if (segno != curseg->segno) {
1283 curseg->next_segno = segno;
1284 change_curseg(sbi, type, true);
1285 }
1286
Jaegeuk Kim491c0852014-02-04 13:01:10 +09001287 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
Haicheng Lie79efe32013-06-13 16:59:27 +08001288 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001289
1290 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001291 locate_dirty_segment(sbi, old_cursegno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001292
1293 mutex_unlock(&sit_i->sentry_lock);
1294 mutex_unlock(&curseg->curseg_mutex);
1295}
1296
Chao Yudf0f8dc2014-03-22 14:57:23 +08001297static inline bool is_merged_page(struct f2fs_sb_info *sbi,
1298 struct page *page, enum page_type type)
1299{
1300 enum page_type btype = PAGE_TYPE_OF_BIO(type);
1301 struct f2fs_bio_info *io = &sbi->write_io[btype];
Chao Yudf0f8dc2014-03-22 14:57:23 +08001302 struct bio_vec *bvec;
1303 int i;
1304
1305 down_read(&io->io_rwsem);
Jaegeuk Kimce234472014-04-02 09:04:42 +09001306 if (!io->bio)
Chao Yudf0f8dc2014-03-22 14:57:23 +08001307 goto out;
1308
Jaegeuk Kimce234472014-04-02 09:04:42 +09001309 bio_for_each_segment_all(bvec, io->bio, i) {
Chao Yudf0f8dc2014-03-22 14:57:23 +08001310 if (page == bvec->bv_page) {
1311 up_read(&io->io_rwsem);
1312 return true;
1313 }
1314 }
1315
1316out:
1317 up_read(&io->io_rwsem);
1318 return false;
1319}
1320
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001321void f2fs_wait_on_page_writeback(struct page *page,
Yuan Zhong5514f0a2014-01-10 07:26:14 +00001322 enum page_type type)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001323{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001324 if (PageWriteback(page)) {
Jaegeuk Kim40813632014-09-02 15:31:18 -07001325 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
1326
Chao Yudf0f8dc2014-03-22 14:57:23 +08001327 if (is_merged_page(sbi, page, type))
1328 f2fs_submit_merged_bio(sbi, type, WRITE);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001329 wait_on_page_writeback(page);
1330 }
1331}
1332
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001333static int read_compacted_summaries(struct f2fs_sb_info *sbi)
1334{
1335 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1336 struct curseg_info *seg_i;
1337 unsigned char *kaddr;
1338 struct page *page;
1339 block_t start;
1340 int i, j, offset;
1341
1342 start = start_sum_block(sbi);
1343
1344 page = get_meta_page(sbi, start++);
1345 kaddr = (unsigned char *)page_address(page);
1346
1347 /* Step 1: restore nat cache */
1348 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1349 memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
1350
1351 /* Step 2: restore sit cache */
1352 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1353 memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
1354 SUM_JOURNAL_SIZE);
1355 offset = 2 * SUM_JOURNAL_SIZE;
1356
1357 /* Step 3: restore summary entries */
1358 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1359 unsigned short blk_off;
1360 unsigned int segno;
1361
1362 seg_i = CURSEG_I(sbi, i);
1363 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
1364 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
1365 seg_i->next_segno = segno;
1366 reset_curseg(sbi, i, 0);
1367 seg_i->alloc_type = ckpt->alloc_type[i];
1368 seg_i->next_blkoff = blk_off;
1369
1370 if (seg_i->alloc_type == SSR)
1371 blk_off = sbi->blocks_per_seg;
1372
1373 for (j = 0; j < blk_off; j++) {
1374 struct f2fs_summary *s;
1375 s = (struct f2fs_summary *)(kaddr + offset);
1376 seg_i->sum_blk->entries[j] = *s;
1377 offset += SUMMARY_SIZE;
1378 if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1379 SUM_FOOTER_SIZE)
1380 continue;
1381
1382 f2fs_put_page(page, 1);
1383 page = NULL;
1384
1385 page = get_meta_page(sbi, start++);
1386 kaddr = (unsigned char *)page_address(page);
1387 offset = 0;
1388 }
1389 }
1390 f2fs_put_page(page, 1);
1391 return 0;
1392}
1393
1394static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
1395{
1396 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1397 struct f2fs_summary_block *sum;
1398 struct curseg_info *curseg;
1399 struct page *new;
1400 unsigned short blk_off;
1401 unsigned int segno = 0;
1402 block_t blk_addr = 0;
1403
1404 /* get segment number and block addr */
1405 if (IS_DATASEG(type)) {
1406 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
1407 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
1408 CURSEG_HOT_DATA]);
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001409 if (__exist_node_summaries(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001410 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
1411 else
1412 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
1413 } else {
1414 segno = le32_to_cpu(ckpt->cur_node_segno[type -
1415 CURSEG_HOT_NODE]);
1416 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
1417 CURSEG_HOT_NODE]);
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001418 if (__exist_node_summaries(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001419 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
1420 type - CURSEG_HOT_NODE);
1421 else
1422 blk_addr = GET_SUM_BLOCK(sbi, segno);
1423 }
1424
1425 new = get_meta_page(sbi, blk_addr);
1426 sum = (struct f2fs_summary_block *)page_address(new);
1427
1428 if (IS_NODESEG(type)) {
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001429 if (__exist_node_summaries(sbi)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001430 struct f2fs_summary *ns = &sum->entries[0];
1431 int i;
1432 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
1433 ns->version = 0;
1434 ns->ofs_in_node = 0;
1435 }
1436 } else {
Gu Zhengd6537882014-03-07 18:43:36 +08001437 int err;
1438
1439 err = restore_node_summary(sbi, segno, sum);
1440 if (err) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001441 f2fs_put_page(new, 1);
Gu Zhengd6537882014-03-07 18:43:36 +08001442 return err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001443 }
1444 }
1445 }
1446
1447 /* set uncompleted segment to curseg */
1448 curseg = CURSEG_I(sbi, type);
1449 mutex_lock(&curseg->curseg_mutex);
1450 memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
1451 curseg->next_segno = segno;
1452 reset_curseg(sbi, type, 0);
1453 curseg->alloc_type = ckpt->alloc_type[type];
1454 curseg->next_blkoff = blk_off;
1455 mutex_unlock(&curseg->curseg_mutex);
1456 f2fs_put_page(new, 1);
1457 return 0;
1458}
1459
1460static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
1461{
1462 int type = CURSEG_HOT_DATA;
Chao Yue4fc5fb2014-03-17 16:36:24 +08001463 int err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001464
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001465 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
Chao Yu3fa06d72014-12-09 14:21:46 +08001466 int npages = npages_for_summary_flush(sbi, true);
1467
1468 if (npages >= 2)
1469 ra_meta_pages(sbi, start_sum_block(sbi), npages,
1470 META_CP);
1471
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001472 /* restore for compacted data summary */
1473 if (read_compacted_summaries(sbi))
1474 return -EINVAL;
1475 type = CURSEG_HOT_NODE;
1476 }
1477
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001478 if (__exist_node_summaries(sbi))
Chao Yu3fa06d72014-12-09 14:21:46 +08001479 ra_meta_pages(sbi, sum_blk_addr(sbi, NR_CURSEG_TYPE, type),
1480 NR_CURSEG_TYPE - type, META_CP);
1481
Chao Yue4fc5fb2014-03-17 16:36:24 +08001482 for (; type <= CURSEG_COLD_NODE; type++) {
1483 err = read_normal_summaries(sbi, type);
1484 if (err)
1485 return err;
1486 }
1487
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001488 return 0;
1489}
1490
1491static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
1492{
1493 struct page *page;
1494 unsigned char *kaddr;
1495 struct f2fs_summary *summary;
1496 struct curseg_info *seg_i;
1497 int written_size = 0;
1498 int i, j;
1499
1500 page = grab_meta_page(sbi, blkaddr++);
1501 kaddr = (unsigned char *)page_address(page);
1502
1503 /* Step 1: write nat cache */
1504 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1505 memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
1506 written_size += SUM_JOURNAL_SIZE;
1507
1508 /* Step 2: write sit cache */
1509 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1510 memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
1511 SUM_JOURNAL_SIZE);
1512 written_size += SUM_JOURNAL_SIZE;
1513
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001514 /* Step 3: write summary entries */
1515 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1516 unsigned short blkoff;
1517 seg_i = CURSEG_I(sbi, i);
1518 if (sbi->ckpt->alloc_type[i] == SSR)
1519 blkoff = sbi->blocks_per_seg;
1520 else
1521 blkoff = curseg_blkoff(sbi, i);
1522
1523 for (j = 0; j < blkoff; j++) {
1524 if (!page) {
1525 page = grab_meta_page(sbi, blkaddr++);
1526 kaddr = (unsigned char *)page_address(page);
1527 written_size = 0;
1528 }
1529 summary = (struct f2fs_summary *)(kaddr + written_size);
1530 *summary = seg_i->sum_blk->entries[j];
1531 written_size += SUMMARY_SIZE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001532
1533 if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1534 SUM_FOOTER_SIZE)
1535 continue;
1536
Chao Yue8d61a72013-10-24 15:08:28 +08001537 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001538 f2fs_put_page(page, 1);
1539 page = NULL;
1540 }
1541 }
Chao Yue8d61a72013-10-24 15:08:28 +08001542 if (page) {
1543 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001544 f2fs_put_page(page, 1);
Chao Yue8d61a72013-10-24 15:08:28 +08001545 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001546}
1547
1548static void write_normal_summaries(struct f2fs_sb_info *sbi,
1549 block_t blkaddr, int type)
1550{
1551 int i, end;
1552 if (IS_DATASEG(type))
1553 end = type + NR_CURSEG_DATA_TYPE;
1554 else
1555 end = type + NR_CURSEG_NODE_TYPE;
1556
1557 for (i = type; i < end; i++) {
1558 struct curseg_info *sum = CURSEG_I(sbi, i);
1559 mutex_lock(&sum->curseg_mutex);
1560 write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
1561 mutex_unlock(&sum->curseg_mutex);
1562 }
1563}
1564
1565void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1566{
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001567 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001568 write_compacted_summaries(sbi, start_blk);
1569 else
1570 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
1571}
1572
1573void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1574{
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001575 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001576}
1577
1578int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
1579 unsigned int val, int alloc)
1580{
1581 int i;
1582
1583 if (type == NAT_JOURNAL) {
1584 for (i = 0; i < nats_in_cursum(sum); i++) {
1585 if (le32_to_cpu(nid_in_journal(sum, i)) == val)
1586 return i;
1587 }
1588 if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
1589 return update_nats_in_cursum(sum, 1);
1590 } else if (type == SIT_JOURNAL) {
1591 for (i = 0; i < sits_in_cursum(sum); i++)
1592 if (le32_to_cpu(segno_in_journal(sum, i)) == val)
1593 return i;
1594 if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
1595 return update_sits_in_cursum(sum, 1);
1596 }
1597 return -1;
1598}
1599
1600static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
1601 unsigned int segno)
1602{
Gu Zheng2cc22182014-10-20 17:45:49 +08001603 return get_meta_page(sbi, current_sit_addr(sbi, segno));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001604}
1605
1606static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
1607 unsigned int start)
1608{
1609 struct sit_info *sit_i = SIT_I(sbi);
1610 struct page *src_page, *dst_page;
1611 pgoff_t src_off, dst_off;
1612 void *src_addr, *dst_addr;
1613
1614 src_off = current_sit_addr(sbi, start);
1615 dst_off = next_sit_addr(sbi, src_off);
1616
1617 /* get current sit block page without lock */
1618 src_page = get_meta_page(sbi, src_off);
1619 dst_page = grab_meta_page(sbi, dst_off);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001620 f2fs_bug_on(sbi, PageDirty(src_page));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001621
1622 src_addr = page_address(src_page);
1623 dst_addr = page_address(dst_page);
1624 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
1625
1626 set_page_dirty(dst_page);
1627 f2fs_put_page(src_page, 1);
1628
1629 set_to_next_sit(sit_i, start);
1630
1631 return dst_page;
1632}
1633
Chao Yu184a5cd2014-09-04 18:13:01 +08001634static struct sit_entry_set *grab_sit_entry_set(void)
1635{
1636 struct sit_entry_set *ses =
1637 f2fs_kmem_cache_alloc(sit_entry_set_slab, GFP_ATOMIC);
1638
1639 ses->entry_cnt = 0;
1640 INIT_LIST_HEAD(&ses->set_list);
1641 return ses;
1642}
1643
1644static void release_sit_entry_set(struct sit_entry_set *ses)
1645{
1646 list_del(&ses->set_list);
1647 kmem_cache_free(sit_entry_set_slab, ses);
1648}
1649
1650static void adjust_sit_entry_set(struct sit_entry_set *ses,
1651 struct list_head *head)
1652{
1653 struct sit_entry_set *next = ses;
1654
1655 if (list_is_last(&ses->set_list, head))
1656 return;
1657
1658 list_for_each_entry_continue(next, head, set_list)
1659 if (ses->entry_cnt <= next->entry_cnt)
1660 break;
1661
1662 list_move_tail(&ses->set_list, &next->set_list);
1663}
1664
1665static void add_sit_entry(unsigned int segno, struct list_head *head)
1666{
1667 struct sit_entry_set *ses;
1668 unsigned int start_segno = START_SEGNO(segno);
1669
1670 list_for_each_entry(ses, head, set_list) {
1671 if (ses->start_segno == start_segno) {
1672 ses->entry_cnt++;
1673 adjust_sit_entry_set(ses, head);
1674 return;
1675 }
1676 }
1677
1678 ses = grab_sit_entry_set();
1679
1680 ses->start_segno = start_segno;
1681 ses->entry_cnt++;
1682 list_add(&ses->set_list, head);
1683}
1684
1685static void add_sits_in_set(struct f2fs_sb_info *sbi)
1686{
1687 struct f2fs_sm_info *sm_info = SM_I(sbi);
1688 struct list_head *set_list = &sm_info->sit_entry_set;
1689 unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap;
Chao Yu184a5cd2014-09-04 18:13:01 +08001690 unsigned int segno;
1691
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001692 for_each_set_bit(segno, bitmap, MAIN_SEGS(sbi))
Chao Yu184a5cd2014-09-04 18:13:01 +08001693 add_sit_entry(segno, set_list);
1694}
1695
1696static void remove_sits_in_journal(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001697{
1698 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1699 struct f2fs_summary_block *sum = curseg->sum_blk;
1700 int i;
1701
Chao Yu184a5cd2014-09-04 18:13:01 +08001702 for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
1703 unsigned int segno;
1704 bool dirtied;
1705
1706 segno = le32_to_cpu(segno_in_journal(sum, i));
1707 dirtied = __mark_sit_entry_dirty(sbi, segno);
1708
1709 if (!dirtied)
1710 add_sit_entry(segno, &SM_I(sbi)->sit_entry_set);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001711 }
Chao Yu184a5cd2014-09-04 18:13:01 +08001712 update_sits_in_cursum(sum, -sits_in_cursum(sum));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001713}
1714
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001715/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001716 * CP calls this function, which flushes SIT entries including sit_journal,
1717 * and moves prefree segs to free segs.
1718 */
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001719void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001720{
1721 struct sit_info *sit_i = SIT_I(sbi);
1722 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
1723 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1724 struct f2fs_summary_block *sum = curseg->sum_blk;
Chao Yu184a5cd2014-09-04 18:13:01 +08001725 struct sit_entry_set *ses, *tmp;
1726 struct list_head *head = &SM_I(sbi)->sit_entry_set;
Chao Yu184a5cd2014-09-04 18:13:01 +08001727 bool to_journal = true;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001728 struct seg_entry *se;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001729
1730 mutex_lock(&curseg->curseg_mutex);
1731 mutex_lock(&sit_i->sentry_lock);
1732
1733 /*
Chao Yu184a5cd2014-09-04 18:13:01 +08001734 * add and account sit entries of dirty bitmap in sit entry
1735 * set temporarily
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001736 */
Chao Yu184a5cd2014-09-04 18:13:01 +08001737 add_sits_in_set(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001738
Chao Yu184a5cd2014-09-04 18:13:01 +08001739 /*
1740 * if there are no enough space in journal to store dirty sit
1741 * entries, remove all entries from journal and add and account
1742 * them in sit entry set.
1743 */
1744 if (!__has_cursum_space(sum, sit_i->dirty_sentries, SIT_JOURNAL))
1745 remove_sits_in_journal(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001746
Chao Yu184a5cd2014-09-04 18:13:01 +08001747 if (!sit_i->dirty_sentries)
1748 goto out;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001749
Chao Yu184a5cd2014-09-04 18:13:01 +08001750 /*
1751 * there are two steps to flush sit entries:
1752 * #1, flush sit entries to journal in current cold data summary block.
1753 * #2, flush sit entries to sit page.
1754 */
1755 list_for_each_entry_safe(ses, tmp, head, set_list) {
Jaegeuk Kim4a257ed2014-10-16 11:43:30 -07001756 struct page *page = NULL;
Chao Yu184a5cd2014-09-04 18:13:01 +08001757 struct f2fs_sit_block *raw_sit = NULL;
1758 unsigned int start_segno = ses->start_segno;
1759 unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001760 (unsigned long)MAIN_SEGS(sbi));
Chao Yu184a5cd2014-09-04 18:13:01 +08001761 unsigned int segno = start_segno;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001762
Chao Yu184a5cd2014-09-04 18:13:01 +08001763 if (to_journal &&
1764 !__has_cursum_space(sum, ses->entry_cnt, SIT_JOURNAL))
1765 to_journal = false;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001766
Chao Yu184a5cd2014-09-04 18:13:01 +08001767 if (!to_journal) {
1768 page = get_next_sit_page(sbi, start_segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001769 raw_sit = page_address(page);
1770 }
1771
Chao Yu184a5cd2014-09-04 18:13:01 +08001772 /* flush dirty sit entries in region of current sit set */
1773 for_each_set_bit_from(segno, bitmap, end) {
1774 int offset, sit_offset;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001775
1776 se = get_seg_entry(sbi, segno);
Chao Yu184a5cd2014-09-04 18:13:01 +08001777
1778 /* add discard candidates */
Jaegeuk Kimd7bc2482014-12-12 13:53:41 -08001779 if (cpc->reason != CP_DISCARD) {
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001780 cpc->trim_start = segno;
1781 add_discard_addrs(sbi, cpc);
1782 }
Chao Yu184a5cd2014-09-04 18:13:01 +08001783
1784 if (to_journal) {
1785 offset = lookup_journal_in_cursum(sum,
1786 SIT_JOURNAL, segno, 1);
1787 f2fs_bug_on(sbi, offset < 0);
1788 segno_in_journal(sum, offset) =
1789 cpu_to_le32(segno);
1790 seg_info_to_raw_sit(se,
1791 &sit_in_journal(sum, offset));
1792 } else {
1793 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
1794 seg_info_to_raw_sit(se,
1795 &raw_sit->entries[sit_offset]);
1796 }
1797
1798 __clear_bit(segno, bitmap);
1799 sit_i->dirty_sentries--;
1800 ses->entry_cnt--;
1801 }
1802
1803 if (!to_journal)
1804 f2fs_put_page(page, 1);
1805
1806 f2fs_bug_on(sbi, ses->entry_cnt);
1807 release_sit_entry_set(ses);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001808 }
Chao Yu184a5cd2014-09-04 18:13:01 +08001809
1810 f2fs_bug_on(sbi, !list_empty(head));
1811 f2fs_bug_on(sbi, sit_i->dirty_sentries);
Chao Yu184a5cd2014-09-04 18:13:01 +08001812out:
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001813 if (cpc->reason == CP_DISCARD) {
1814 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++)
1815 add_discard_addrs(sbi, cpc);
1816 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001817 mutex_unlock(&sit_i->sentry_lock);
1818 mutex_unlock(&curseg->curseg_mutex);
1819
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001820 set_prefree_as_free_segments(sbi);
1821}
1822
1823static int build_sit_info(struct f2fs_sb_info *sbi)
1824{
1825 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1826 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1827 struct sit_info *sit_i;
1828 unsigned int sit_segs, start;
1829 char *src_bitmap, *dst_bitmap;
1830 unsigned int bitmap_size;
1831
1832 /* allocate memory for SIT information */
1833 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
1834 if (!sit_i)
1835 return -ENOMEM;
1836
1837 SM_I(sbi)->sit_info = sit_i;
1838
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001839 sit_i->sentries = vzalloc(MAIN_SEGS(sbi) * sizeof(struct seg_entry));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001840 if (!sit_i->sentries)
1841 return -ENOMEM;
1842
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001843 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001844 sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1845 if (!sit_i->dirty_sentries_bitmap)
1846 return -ENOMEM;
1847
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001848 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001849 sit_i->sentries[start].cur_valid_map
1850 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1851 sit_i->sentries[start].ckpt_valid_map
1852 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1853 if (!sit_i->sentries[start].cur_valid_map
1854 || !sit_i->sentries[start].ckpt_valid_map)
1855 return -ENOMEM;
1856 }
1857
1858 if (sbi->segs_per_sec > 1) {
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001859 sit_i->sec_entries = vzalloc(MAIN_SECS(sbi) *
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001860 sizeof(struct sec_entry));
1861 if (!sit_i->sec_entries)
1862 return -ENOMEM;
1863 }
1864
1865 /* get information related with SIT */
1866 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
1867
1868 /* setup SIT bitmap from ckeckpoint pack */
1869 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
1870 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
1871
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001872 dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001873 if (!dst_bitmap)
1874 return -ENOMEM;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001875
1876 /* init SIT information */
1877 sit_i->s_ops = &default_salloc_ops;
1878
1879 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
1880 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
1881 sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
1882 sit_i->sit_bitmap = dst_bitmap;
1883 sit_i->bitmap_size = bitmap_size;
1884 sit_i->dirty_sentries = 0;
1885 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
1886 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
1887 sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
1888 mutex_init(&sit_i->sentry_lock);
1889 return 0;
1890}
1891
1892static int build_free_segmap(struct f2fs_sb_info *sbi)
1893{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001894 struct free_segmap_info *free_i;
1895 unsigned int bitmap_size, sec_bitmap_size;
1896
1897 /* allocate memory for free segmap information */
1898 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
1899 if (!free_i)
1900 return -ENOMEM;
1901
1902 SM_I(sbi)->free_info = free_i;
1903
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001904 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001905 free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL);
1906 if (!free_i->free_segmap)
1907 return -ENOMEM;
1908
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001909 sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001910 free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL);
1911 if (!free_i->free_secmap)
1912 return -ENOMEM;
1913
1914 /* set all segments as dirty temporarily */
1915 memset(free_i->free_segmap, 0xff, bitmap_size);
1916 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
1917
1918 /* init free segmap information */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001919 free_i->start_segno = GET_SEGNO_FROM_SEG0(sbi, MAIN_BLKADDR(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001920 free_i->free_segments = 0;
1921 free_i->free_sections = 0;
1922 rwlock_init(&free_i->segmap_lock);
1923 return 0;
1924}
1925
1926static int build_curseg(struct f2fs_sb_info *sbi)
1927{
Namjae Jeon1042d602012-12-01 10:56:13 +09001928 struct curseg_info *array;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001929 int i;
1930
Fabian Frederickb434bab2014-06-23 18:39:15 +02001931 array = kcalloc(NR_CURSEG_TYPE, sizeof(*array), GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001932 if (!array)
1933 return -ENOMEM;
1934
1935 SM_I(sbi)->curseg_array = array;
1936
1937 for (i = 0; i < NR_CURSEG_TYPE; i++) {
1938 mutex_init(&array[i].curseg_mutex);
1939 array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
1940 if (!array[i].sum_blk)
1941 return -ENOMEM;
1942 array[i].segno = NULL_SEGNO;
1943 array[i].next_blkoff = 0;
1944 }
1945 return restore_curseg_summaries(sbi);
1946}
1947
1948static void build_sit_entries(struct f2fs_sb_info *sbi)
1949{
1950 struct sit_info *sit_i = SIT_I(sbi);
1951 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1952 struct f2fs_summary_block *sum = curseg->sum_blk;
Chao Yu74de5932013-11-22 09:09:59 +08001953 int sit_blk_cnt = SIT_BLK_CNT(sbi);
1954 unsigned int i, start, end;
1955 unsigned int readed, start_blk = 0;
Jaegeuk Kim90a893c2014-09-22 16:21:07 -07001956 int nrpages = MAX_BIO_BLOCKS(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001957
Chao Yu74de5932013-11-22 09:09:59 +08001958 do {
Chao Yu662befd2014-02-07 16:11:53 +08001959 readed = ra_meta_pages(sbi, start_blk, nrpages, META_SIT);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001960
Chao Yu74de5932013-11-22 09:09:59 +08001961 start = start_blk * sit_i->sents_per_block;
1962 end = (start_blk + readed) * sit_i->sents_per_block;
1963
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001964 for (; start < end && start < MAIN_SEGS(sbi); start++) {
Chao Yu74de5932013-11-22 09:09:59 +08001965 struct seg_entry *se = &sit_i->sentries[start];
1966 struct f2fs_sit_block *sit_blk;
1967 struct f2fs_sit_entry sit;
1968 struct page *page;
1969
1970 mutex_lock(&curseg->curseg_mutex);
1971 for (i = 0; i < sits_in_cursum(sum); i++) {
Chris Fries6c311ec2014-01-17 14:44:39 -06001972 if (le32_to_cpu(segno_in_journal(sum, i))
1973 == start) {
Chao Yu74de5932013-11-22 09:09:59 +08001974 sit = sit_in_journal(sum, i);
1975 mutex_unlock(&curseg->curseg_mutex);
1976 goto got_it;
1977 }
1978 }
1979 mutex_unlock(&curseg->curseg_mutex);
1980
1981 page = get_current_sit_page(sbi, start);
1982 sit_blk = (struct f2fs_sit_block *)page_address(page);
1983 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
1984 f2fs_put_page(page, 1);
1985got_it:
1986 check_block_count(sbi, start, &sit);
1987 seg_info_from_raw_sit(se, &sit);
1988 if (sbi->segs_per_sec > 1) {
1989 struct sec_entry *e = get_sec_entry(sbi, start);
1990 e->valid_blocks += se->valid_blocks;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001991 }
1992 }
Chao Yu74de5932013-11-22 09:09:59 +08001993 start_blk += readed;
1994 } while (start_blk < sit_blk_cnt);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001995}
1996
1997static void init_free_segmap(struct f2fs_sb_info *sbi)
1998{
1999 unsigned int start;
2000 int type;
2001
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002002 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002003 struct seg_entry *sentry = get_seg_entry(sbi, start);
2004 if (!sentry->valid_blocks)
2005 __set_free(sbi, start);
2006 }
2007
2008 /* set use the current segments */
2009 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
2010 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
2011 __set_test_and_inuse(sbi, curseg_t->segno);
2012 }
2013}
2014
2015static void init_dirty_segmap(struct f2fs_sb_info *sbi)
2016{
2017 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2018 struct free_segmap_info *free_i = FREE_I(sbi);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002019 unsigned int segno = 0, offset = 0;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002020 unsigned short valid_blocks;
2021
Namjae Jeon8736fbf2013-06-16 09:49:11 +09002022 while (1) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002023 /* find dirty segment based on free segmap */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002024 segno = find_next_inuse(free_i, MAIN_SEGS(sbi), offset);
2025 if (segno >= MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002026 break;
2027 offset = segno + 1;
2028 valid_blocks = get_valid_blocks(sbi, segno, 0);
Jaegeuk Kimec325b52014-09-02 16:24:11 -07002029 if (valid_blocks == sbi->blocks_per_seg || !valid_blocks)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002030 continue;
Jaegeuk Kimec325b52014-09-02 16:24:11 -07002031 if (valid_blocks > sbi->blocks_per_seg) {
2032 f2fs_bug_on(sbi, 1);
2033 continue;
2034 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002035 mutex_lock(&dirty_i->seglist_lock);
2036 __locate_dirty_segment(sbi, segno, DIRTY);
2037 mutex_unlock(&dirty_i->seglist_lock);
2038 }
2039}
2040
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002041static int init_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002042{
2043 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002044 unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002045
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002046 dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL);
2047 if (!dirty_i->victim_secmap)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002048 return -ENOMEM;
2049 return 0;
2050}
2051
2052static int build_dirty_segmap(struct f2fs_sb_info *sbi)
2053{
2054 struct dirty_seglist_info *dirty_i;
2055 unsigned int bitmap_size, i;
2056
2057 /* allocate memory for dirty segments list information */
2058 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
2059 if (!dirty_i)
2060 return -ENOMEM;
2061
2062 SM_I(sbi)->dirty_info = dirty_i;
2063 mutex_init(&dirty_i->seglist_lock);
2064
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002065 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002066
2067 for (i = 0; i < NR_DIRTY_TYPE; i++) {
2068 dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002069 if (!dirty_i->dirty_segmap[i])
2070 return -ENOMEM;
2071 }
2072
2073 init_dirty_segmap(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002074 return init_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002075}
2076
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002077/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002078 * Update min, max modified time for cost-benefit GC algorithm
2079 */
2080static void init_min_max_mtime(struct f2fs_sb_info *sbi)
2081{
2082 struct sit_info *sit_i = SIT_I(sbi);
2083 unsigned int segno;
2084
2085 mutex_lock(&sit_i->sentry_lock);
2086
2087 sit_i->min_mtime = LLONG_MAX;
2088
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002089 for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002090 unsigned int i;
2091 unsigned long long mtime = 0;
2092
2093 for (i = 0; i < sbi->segs_per_sec; i++)
2094 mtime += get_seg_entry(sbi, segno + i)->mtime;
2095
2096 mtime = div_u64(mtime, sbi->segs_per_sec);
2097
2098 if (sit_i->min_mtime > mtime)
2099 sit_i->min_mtime = mtime;
2100 }
2101 sit_i->max_mtime = get_mtime(sbi);
2102 mutex_unlock(&sit_i->sentry_lock);
2103}
2104
2105int build_segment_manager(struct f2fs_sb_info *sbi)
2106{
2107 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
2108 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Namjae Jeon1042d602012-12-01 10:56:13 +09002109 struct f2fs_sm_info *sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002110 int err;
2111
2112 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
2113 if (!sm_info)
2114 return -ENOMEM;
2115
2116 /* init sm info */
2117 sbi->sm_info = sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002118 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
2119 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
2120 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
2121 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
2122 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
2123 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
2124 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
Jaegeuk Kim58c41032014-03-19 14:17:21 +09002125 sm_info->rec_prefree_segments = sm_info->main_segments *
2126 DEF_RECLAIM_PREFREE_SEGMENTS / 100;
Jaegeuk Kim9b5f1362014-09-16 18:30:54 -07002127 sm_info->ipu_policy = 1 << F2FS_IPU_FSYNC;
Jaegeuk Kim216fbd62013-11-07 13:13:42 +09002128 sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -07002129 sm_info->min_fsync_blocks = DEF_MIN_FSYNC_BLOCKS;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002130
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002131 INIT_LIST_HEAD(&sm_info->discard_list);
2132 sm_info->nr_discards = 0;
2133 sm_info->max_discards = 0;
2134
Jaegeuk Kimbba681c2015-01-26 17:41:23 -08002135 sm_info->trim_sections = DEF_BATCHED_TRIM_SECTIONS;
2136
Chao Yu184a5cd2014-09-04 18:13:01 +08002137 INIT_LIST_HEAD(&sm_info->sit_entry_set);
2138
Gu Zhengb270ad62014-04-11 17:49:55 +08002139 if (test_opt(sbi, FLUSH_MERGE) && !f2fs_readonly(sbi->sb)) {
Gu Zheng2163d192014-04-27 14:21:33 +08002140 err = create_flush_cmd_control(sbi);
2141 if (err)
Gu Zhenga688b9d9e2014-04-27 14:21:21 +08002142 return err;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +09002143 }
2144
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002145 err = build_sit_info(sbi);
2146 if (err)
2147 return err;
2148 err = build_free_segmap(sbi);
2149 if (err)
2150 return err;
2151 err = build_curseg(sbi);
2152 if (err)
2153 return err;
2154
2155 /* reinit free segmap based on SIT */
2156 build_sit_entries(sbi);
2157
2158 init_free_segmap(sbi);
2159 err = build_dirty_segmap(sbi);
2160 if (err)
2161 return err;
2162
2163 init_min_max_mtime(sbi);
2164 return 0;
2165}
2166
2167static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
2168 enum dirty_type dirty_type)
2169{
2170 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2171
2172 mutex_lock(&dirty_i->seglist_lock);
2173 kfree(dirty_i->dirty_segmap[dirty_type]);
2174 dirty_i->nr_dirty[dirty_type] = 0;
2175 mutex_unlock(&dirty_i->seglist_lock);
2176}
2177
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002178static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002179{
2180 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002181 kfree(dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002182}
2183
2184static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
2185{
2186 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2187 int i;
2188
2189 if (!dirty_i)
2190 return;
2191
2192 /* discard pre-free/dirty segments list */
2193 for (i = 0; i < NR_DIRTY_TYPE; i++)
2194 discard_dirty_segmap(sbi, i);
2195
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002196 destroy_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002197 SM_I(sbi)->dirty_info = NULL;
2198 kfree(dirty_i);
2199}
2200
2201static void destroy_curseg(struct f2fs_sb_info *sbi)
2202{
2203 struct curseg_info *array = SM_I(sbi)->curseg_array;
2204 int i;
2205
2206 if (!array)
2207 return;
2208 SM_I(sbi)->curseg_array = NULL;
2209 for (i = 0; i < NR_CURSEG_TYPE; i++)
2210 kfree(array[i].sum_blk);
2211 kfree(array);
2212}
2213
2214static void destroy_free_segmap(struct f2fs_sb_info *sbi)
2215{
2216 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
2217 if (!free_i)
2218 return;
2219 SM_I(sbi)->free_info = NULL;
2220 kfree(free_i->free_segmap);
2221 kfree(free_i->free_secmap);
2222 kfree(free_i);
2223}
2224
2225static void destroy_sit_info(struct f2fs_sb_info *sbi)
2226{
2227 struct sit_info *sit_i = SIT_I(sbi);
2228 unsigned int start;
2229
2230 if (!sit_i)
2231 return;
2232
2233 if (sit_i->sentries) {
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002234 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002235 kfree(sit_i->sentries[start].cur_valid_map);
2236 kfree(sit_i->sentries[start].ckpt_valid_map);
2237 }
2238 }
2239 vfree(sit_i->sentries);
2240 vfree(sit_i->sec_entries);
2241 kfree(sit_i->dirty_sentries_bitmap);
2242
2243 SM_I(sbi)->sit_info = NULL;
2244 kfree(sit_i->sit_bitmap);
2245 kfree(sit_i);
2246}
2247
2248void destroy_segment_manager(struct f2fs_sb_info *sbi)
2249{
2250 struct f2fs_sm_info *sm_info = SM_I(sbi);
Gu Zhenga688b9d9e2014-04-27 14:21:21 +08002251
Chao Yu3b03f722013-11-06 09:12:04 +08002252 if (!sm_info)
2253 return;
Gu Zheng2163d192014-04-27 14:21:33 +08002254 destroy_flush_cmd_control(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002255 destroy_dirty_segmap(sbi);
2256 destroy_curseg(sbi);
2257 destroy_free_segmap(sbi);
2258 destroy_sit_info(sbi);
2259 sbi->sm_info = NULL;
2260 kfree(sm_info);
2261}
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002262
2263int __init create_segment_manager_caches(void)
2264{
2265 discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
Gu Zhenge8512d22014-03-07 18:43:28 +08002266 sizeof(struct discard_entry));
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002267 if (!discard_entry_slab)
Chao Yu184a5cd2014-09-04 18:13:01 +08002268 goto fail;
2269
2270 sit_entry_set_slab = f2fs_kmem_cache_create("sit_entry_set",
Changman Leec9ee0082014-11-21 15:42:07 +09002271 sizeof(struct sit_entry_set));
Chao Yu184a5cd2014-09-04 18:13:01 +08002272 if (!sit_entry_set_slab)
2273 goto destory_discard_entry;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002274
2275 inmem_entry_slab = f2fs_kmem_cache_create("inmem_page_entry",
2276 sizeof(struct inmem_pages));
2277 if (!inmem_entry_slab)
2278 goto destroy_sit_entry_set;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002279 return 0;
Chao Yu184a5cd2014-09-04 18:13:01 +08002280
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002281destroy_sit_entry_set:
2282 kmem_cache_destroy(sit_entry_set_slab);
Chao Yu184a5cd2014-09-04 18:13:01 +08002283destory_discard_entry:
2284 kmem_cache_destroy(discard_entry_slab);
2285fail:
2286 return -ENOMEM;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002287}
2288
2289void destroy_segment_manager_caches(void)
2290{
Chao Yu184a5cd2014-09-04 18:13:01 +08002291 kmem_cache_destroy(sit_entry_set_slab);
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002292 kmem_cache_destroy(discard_entry_slab);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002293 kmem_cache_destroy(inmem_entry_slab);
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002294}