blob: f7bfc3b7d934283d9d2f83e30ef5937633cf925e [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{
Jaegeuk Kime19ef5272015-05-18 11:45:15 -070078 while (!f2fs_test_bit(offset, (unsigned char *)addr))
79 offset++;
80
81 if (offset > size)
82 offset = size;
83
84 return offset;
85#if 0
Changman Lee9a7f1432013-11-15 10:42:51 +090086 const unsigned long *p = addr + BIT_WORD(offset);
87 unsigned long result = offset & ~(BITS_PER_LONG - 1);
88 unsigned long tmp;
89 unsigned long mask, submask;
90 unsigned long quot, rest;
91
92 if (offset >= size)
93 return size;
94
95 size -= result;
96 offset %= BITS_PER_LONG;
97 if (!offset)
98 goto aligned;
99
100 tmp = *(p++);
101 quot = (offset >> 3) << 3;
102 rest = offset & 0x7;
103 mask = ~0UL << quot;
104 submask = (unsigned char)(0xff << rest) >> rest;
105 submask <<= quot;
106 mask &= submask;
107 tmp &= mask;
108 if (size < BITS_PER_LONG)
109 goto found_first;
110 if (tmp)
111 goto found_middle;
112
113 size -= BITS_PER_LONG;
114 result += BITS_PER_LONG;
115aligned:
116 while (size & ~(BITS_PER_LONG-1)) {
117 tmp = *(p++);
118 if (tmp)
119 goto found_middle;
120 result += BITS_PER_LONG;
121 size -= BITS_PER_LONG;
122 }
123 if (!size)
124 return result;
125 tmp = *p;
126found_first:
127 tmp &= (~0UL >> (BITS_PER_LONG - size));
128 if (tmp == 0UL) /* Are any bits set? */
129 return result + size; /* Nope. */
130found_middle:
131 return result + __reverse_ffs(tmp);
Jaegeuk Kime19ef5272015-05-18 11:45:15 -0700132#endif
Changman Lee9a7f1432013-11-15 10:42:51 +0900133}
134
135static unsigned long __find_rev_next_zero_bit(const unsigned long *addr,
136 unsigned long size, unsigned long offset)
137{
Jaegeuk Kime19ef5272015-05-18 11:45:15 -0700138 while (f2fs_test_bit(offset, (unsigned char *)addr))
139 offset++;
140
141 if (offset > size)
142 offset = size;
143
144 return offset;
145#if 0
Changman Lee9a7f1432013-11-15 10:42:51 +0900146 const unsigned long *p = addr + BIT_WORD(offset);
147 unsigned long result = offset & ~(BITS_PER_LONG - 1);
148 unsigned long tmp;
149 unsigned long mask, submask;
150 unsigned long quot, rest;
151
152 if (offset >= size)
153 return size;
154
155 size -= result;
156 offset %= BITS_PER_LONG;
157 if (!offset)
158 goto aligned;
159
160 tmp = *(p++);
161 quot = (offset >> 3) << 3;
162 rest = offset & 0x7;
163 mask = ~(~0UL << quot);
164 submask = (unsigned char)~((unsigned char)(0xff << rest) >> rest);
165 submask <<= quot;
166 mask += submask;
167 tmp |= mask;
168 if (size < BITS_PER_LONG)
169 goto found_first;
170 if (~tmp)
171 goto found_middle;
172
173 size -= BITS_PER_LONG;
174 result += BITS_PER_LONG;
175aligned:
176 while (size & ~(BITS_PER_LONG - 1)) {
177 tmp = *(p++);
178 if (~tmp)
179 goto found_middle;
180 result += BITS_PER_LONG;
181 size -= BITS_PER_LONG;
182 }
183 if (!size)
184 return result;
185 tmp = *p;
186
187found_first:
188 tmp |= ~0UL << size;
189 if (tmp == ~0UL) /* Are any bits zero? */
190 return result + size; /* Nope. */
191found_middle:
192 return result + __reverse_ffz(tmp);
Jaegeuk Kime19ef5272015-05-18 11:45:15 -0700193#endif
Changman Lee9a7f1432013-11-15 10:42:51 +0900194}
195
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700196void register_inmem_page(struct inode *inode, struct page *page)
197{
198 struct f2fs_inode_info *fi = F2FS_I(inode);
199 struct inmem_pages *new;
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -0700200 int err;
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800201
Jaegeuk Kim0722b102014-12-05 11:58:02 -0800202 SetPagePrivate(page);
Jaegeuk Kim9e4ded32014-12-17 19:58:58 -0800203 f2fs_trace_pid(page);
Jaegeuk Kim0722b102014-12-05 11:58:02 -0800204
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700205 new = f2fs_kmem_cache_alloc(inmem_entry_slab, GFP_NOFS);
206
207 /* add atomic page indices to the list */
208 new->page = page;
209 INIT_LIST_HEAD(&new->list);
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800210retry:
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700211 /* increase reference count with clean state */
212 mutex_lock(&fi->inmem_lock);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -0700213 err = radix_tree_insert(&fi->inmem_root, page->index, new);
214 if (err == -EEXIST) {
215 mutex_unlock(&fi->inmem_lock);
216 kmem_cache_free(inmem_entry_slab, new);
217 return;
218 } else if (err) {
219 mutex_unlock(&fi->inmem_lock);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -0700220 goto retry;
221 }
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700222 get_page(page);
223 list_add_tail(&new->list, &fi->inmem_pages);
Jaegeuk Kim8dcf2ff72014-12-05 17:18:15 -0800224 inc_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700225 mutex_unlock(&fi->inmem_lock);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -0700226
227 trace_f2fs_register_inmem_page(page, INMEM);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700228}
229
230void commit_inmem_pages(struct inode *inode, bool abort)
231{
232 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
233 struct f2fs_inode_info *fi = F2FS_I(inode);
234 struct inmem_pages *cur, *tmp;
235 bool submit_bio = false;
236 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700237 .sbi = sbi,
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700238 .type = DATA,
Jaegeuk Kim1e843712014-12-09 06:08:59 -0800239 .rw = WRITE_SYNC | REQ_PRIO,
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700240 .encrypted_page = NULL,
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700241 };
242
Jaegeuk Kim03418452014-11-21 16:37:40 -0800243 /*
244 * The abort is true only when f2fs_evict_inode is called.
245 * Basically, the f2fs_evict_inode doesn't produce any data writes, so
246 * that we don't need to call f2fs_balance_fs.
247 * Otherwise, f2fs_gc in f2fs_balance_fs can wait forever until this
248 * inode becomes free by iget_locked in f2fs_iget.
249 */
Jaegeuk Kim70c640b2014-12-10 13:59:33 -0800250 if (!abort) {
Jaegeuk Kim03418452014-11-21 16:37:40 -0800251 f2fs_balance_fs(sbi);
Jaegeuk Kim70c640b2014-12-10 13:59:33 -0800252 f2fs_lock_op(sbi);
253 }
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700254
255 mutex_lock(&fi->inmem_lock);
256 list_for_each_entry_safe(cur, tmp, &fi->inmem_pages, list) {
Jaegeuk Kim70c640b2014-12-10 13:59:33 -0800257 if (!abort) {
258 lock_page(cur->page);
259 if (cur->page->mapping == inode->i_mapping) {
Jaegeuk Kim6282adb2015-07-25 00:29:17 -0700260 set_page_dirty(cur->page);
Jaegeuk Kim70c640b2014-12-10 13:59:33 -0800261 f2fs_wait_on_page_writeback(cur->page, DATA);
262 if (clear_page_dirty_for_io(cur->page))
263 inode_dec_dirty_pages(inode);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -0700264 trace_f2fs_commit_inmem_page(cur->page, INMEM);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700265 fio.page = cur->page;
266 do_write_data_page(&fio);
Jaegeuk Kim70c640b2014-12-10 13:59:33 -0800267 submit_bio = true;
268 }
269 f2fs_put_page(cur->page, 1);
270 } else {
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -0700271 trace_f2fs_commit_inmem_page(cur->page, INMEM_DROP);
Jaegeuk Kim70c640b2014-12-10 13:59:33 -0800272 put_page(cur->page);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700273 }
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -0700274 radix_tree_delete(&fi->inmem_root, cur->page->index);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700275 list_del(&cur->list);
276 kmem_cache_free(inmem_entry_slab, cur);
Jaegeuk Kim8dcf2ff72014-12-05 17:18:15 -0800277 dec_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700278 }
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700279 mutex_unlock(&fi->inmem_lock);
280
Jaegeuk Kim70c640b2014-12-10 13:59:33 -0800281 if (!abort) {
282 f2fs_unlock_op(sbi);
283 if (submit_bio)
284 f2fs_submit_merged_bio(sbi, DATA, WRITE);
285 }
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700286}
287
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900288/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900289 * This function balances dirty node and dentry pages.
290 * In addition, it controls garbage collection.
291 */
292void f2fs_balance_fs(struct f2fs_sb_info *sbi)
293{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900294 /*
Jaegeuk Kim029cd282012-12-21 17:20:21 +0900295 * We should do GC or end up with checkpoint, if there are so many dirty
296 * dir/node pages without enough free segments.
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900297 */
Jaegeuk Kim43727522013-02-04 15:11:17 +0900298 if (has_not_enough_free_secs(sbi, 0)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900299 mutex_lock(&sbi->gc_mutex);
Jaegeuk Kim408e9372013-01-03 17:55:52 +0900300 f2fs_gc(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900301 }
302}
303
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900304void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi)
305{
Chao Yu1dcc3362015-02-05 17:57:31 +0800306 /* try to shrink extent cache when there is no enough memory */
Jaegeuk Kim554df792015-06-19 13:41:23 -0700307 if (!available_free_memory(sbi, EXTENT_CACHE))
308 f2fs_shrink_extent_tree(sbi, EXTENT_CACHE_SHRINK_NUMBER);
Chao Yu1dcc3362015-02-05 17:57:31 +0800309
Jaegeuk Kim1b38dc82015-06-19 15:36:07 -0700310 /* check the # of cached NAT entries */
311 if (!available_free_memory(sbi, NAT_ENTRIES))
312 try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK);
313
314 /* checkpoint is the only way to shrink partial cached entries */
315 if (!available_free_memory(sbi, NAT_ENTRIES) ||
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -0800316 excess_prefree_segs(sbi) ||
Jaegeuk Kim88a70a62014-12-10 15:20:48 -0800317 !available_free_memory(sbi, INO_ENTRIES))
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900318 f2fs_sync_fs(sbi->sb, true);
319}
320
Gu Zheng2163d192014-04-27 14:21:33 +0800321static int issue_flush_thread(void *data)
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900322{
323 struct f2fs_sb_info *sbi = data;
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800324 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
325 wait_queue_head_t *q = &fcc->flush_wait_queue;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900326repeat:
327 if (kthread_should_stop())
328 return 0;
329
Gu Zheng721bd4d2014-09-05 18:31:00 +0800330 if (!llist_empty(&fcc->issue_list)) {
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900331 struct bio *bio = bio_alloc(GFP_NOIO, 0);
332 struct flush_cmd *cmd, *next;
333 int ret;
334
Gu Zheng721bd4d2014-09-05 18:31:00 +0800335 fcc->dispatch_list = llist_del_all(&fcc->issue_list);
336 fcc->dispatch_list = llist_reverse_order(fcc->dispatch_list);
337
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900338 bio->bi_bdev = sbi->sb->s_bdev;
339 ret = submit_bio_wait(WRITE_FLUSH, bio);
340
Gu Zheng721bd4d2014-09-05 18:31:00 +0800341 llist_for_each_entry_safe(cmd, next,
342 fcc->dispatch_list, llnode) {
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900343 cmd->ret = ret;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900344 complete(&cmd->wait);
345 }
Gu Zhenga4ed23f2014-04-11 17:49:35 +0800346 bio_put(bio);
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800347 fcc->dispatch_list = NULL;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900348 }
349
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800350 wait_event_interruptible(*q,
Gu Zheng721bd4d2014-09-05 18:31:00 +0800351 kthread_should_stop() || !llist_empty(&fcc->issue_list));
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900352 goto repeat;
353}
354
355int f2fs_issue_flush(struct f2fs_sb_info *sbi)
356{
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800357 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
Chao Yuadf8d902014-05-08 17:00:35 +0800358 struct flush_cmd cmd;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900359
Jaegeuk Kim24a9ee02014-07-25 17:46:10 -0700360 trace_f2fs_issue_flush(sbi->sb, test_opt(sbi, NOBARRIER),
361 test_opt(sbi, FLUSH_MERGE));
362
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700363 if (test_opt(sbi, NOBARRIER))
364 return 0;
365
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900366 if (!test_opt(sbi, FLUSH_MERGE))
367 return blkdev_issue_flush(sbi->sb->s_bdev, GFP_KERNEL, NULL);
368
Chao Yuadf8d902014-05-08 17:00:35 +0800369 init_completion(&cmd.wait);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900370
Gu Zheng721bd4d2014-09-05 18:31:00 +0800371 llist_add(&cmd.llnode, &fcc->issue_list);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900372
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800373 if (!fcc->dispatch_list)
374 wake_up(&fcc->flush_wait_queue);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900375
Chao Yuadf8d902014-05-08 17:00:35 +0800376 wait_for_completion(&cmd.wait);
377
378 return cmd.ret;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900379}
380
Gu Zheng2163d192014-04-27 14:21:33 +0800381int create_flush_cmd_control(struct f2fs_sb_info *sbi)
382{
383 dev_t dev = sbi->sb->s_bdev->bd_dev;
384 struct flush_cmd_control *fcc;
385 int err = 0;
386
387 fcc = kzalloc(sizeof(struct flush_cmd_control), GFP_KERNEL);
388 if (!fcc)
389 return -ENOMEM;
Gu Zheng2163d192014-04-27 14:21:33 +0800390 init_waitqueue_head(&fcc->flush_wait_queue);
Gu Zheng721bd4d2014-09-05 18:31:00 +0800391 init_llist_head(&fcc->issue_list);
Chao Yu6b2920a2014-07-07 11:21:59 +0800392 SM_I(sbi)->cmd_control_info = fcc;
Gu Zheng2163d192014-04-27 14:21:33 +0800393 fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi,
394 "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev));
395 if (IS_ERR(fcc->f2fs_issue_flush)) {
396 err = PTR_ERR(fcc->f2fs_issue_flush);
397 kfree(fcc);
Chao Yu6b2920a2014-07-07 11:21:59 +0800398 SM_I(sbi)->cmd_control_info = NULL;
Gu Zheng2163d192014-04-27 14:21:33 +0800399 return err;
400 }
Gu Zheng2163d192014-04-27 14:21:33 +0800401
402 return err;
403}
404
405void destroy_flush_cmd_control(struct f2fs_sb_info *sbi)
406{
Chao Yu6b2920a2014-07-07 11:21:59 +0800407 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
Gu Zheng2163d192014-04-27 14:21:33 +0800408
409 if (fcc && fcc->f2fs_issue_flush)
410 kthread_stop(fcc->f2fs_issue_flush);
411 kfree(fcc);
Chao Yu6b2920a2014-07-07 11:21:59 +0800412 SM_I(sbi)->cmd_control_info = NULL;
Gu Zheng2163d192014-04-27 14:21:33 +0800413}
414
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900415static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
416 enum dirty_type dirty_type)
417{
418 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
419
420 /* need not be added */
421 if (IS_CURSEG(sbi, segno))
422 return;
423
424 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
425 dirty_i->nr_dirty[dirty_type]++;
426
427 if (dirty_type == DIRTY) {
428 struct seg_entry *sentry = get_seg_entry(sbi, segno);
Changman Lee4625d6a2013-10-25 17:31:57 +0900429 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900430
Jaegeuk Kimec325b52014-09-02 16:24:11 -0700431 if (unlikely(t >= DIRTY)) {
432 f2fs_bug_on(sbi, 1);
433 return;
434 }
Changman Lee4625d6a2013-10-25 17:31:57 +0900435 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
436 dirty_i->nr_dirty[t]++;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900437 }
438}
439
440static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
441 enum dirty_type dirty_type)
442{
443 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
444
445 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
446 dirty_i->nr_dirty[dirty_type]--;
447
448 if (dirty_type == DIRTY) {
Changman Lee4625d6a2013-10-25 17:31:57 +0900449 struct seg_entry *sentry = get_seg_entry(sbi, segno);
450 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900451
Changman Lee4625d6a2013-10-25 17:31:57 +0900452 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
453 dirty_i->nr_dirty[t]--;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900454
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +0900455 if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0)
456 clear_bit(GET_SECNO(sbi, segno),
457 dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900458 }
459}
460
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900461/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900462 * Should not occur error such as -ENOMEM.
463 * Adding dirty entry into seglist is not critical operation.
464 * If a given segment is one of current working segments, it won't be added.
465 */
Haicheng Li8d8451a2013-06-13 16:59:28 +0800466static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900467{
468 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
469 unsigned short valid_blocks;
470
471 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
472 return;
473
474 mutex_lock(&dirty_i->seglist_lock);
475
476 valid_blocks = get_valid_blocks(sbi, segno, 0);
477
478 if (valid_blocks == 0) {
479 __locate_dirty_segment(sbi, segno, PRE);
480 __remove_dirty_segment(sbi, segno, DIRTY);
481 } else if (valid_blocks < sbi->blocks_per_seg) {
482 __locate_dirty_segment(sbi, segno, DIRTY);
483 } else {
484 /* Recovery routine with SSR needs this */
485 __remove_dirty_segment(sbi, segno, DIRTY);
486 }
487
488 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900489}
490
Jaegeuk Kim1e87a782014-04-15 13:57:55 +0900491static int f2fs_issue_discard(struct f2fs_sb_info *sbi,
Jaegeuk Kim37208872013-11-12 16:55:17 +0900492 block_t blkstart, block_t blklen)
493{
Chao Yu55cf9cb2014-09-15 18:01:10 +0800494 sector_t start = SECTOR_FROM_BLOCK(blkstart);
495 sector_t len = SECTOR_FROM_BLOCK(blklen);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700496 struct seg_entry *se;
497 unsigned int offset;
498 block_t i;
499
500 for (i = blkstart; i < blkstart + blklen; i++) {
501 se = get_seg_entry(sbi, GET_SEGNO(sbi, i));
502 offset = GET_BLKOFF_FROM_SEG0(sbi, i);
503
504 if (!f2fs_test_and_set_bit(offset, se->discard_map))
505 sbi->discard_blks--;
506 }
Jaegeuk Kim1661d072013-11-12 17:01:00 +0900507 trace_f2fs_issue_discard(sbi->sb, blkstart, blklen);
Jaegeuk Kim1e87a782014-04-15 13:57:55 +0900508 return blkdev_issue_discard(sbi->sb->s_bdev, start, len, GFP_NOFS, 0);
509}
510
Jaegeuk Kimcf2271e2014-07-25 15:47:25 -0700511void discard_next_dnode(struct f2fs_sb_info *sbi, block_t blkaddr)
Jaegeuk Kim1e87a782014-04-15 13:57:55 +0900512{
Jaegeuk Kim40a02be2015-05-11 20:03:49 -0700513 int err = -ENOTSUPP;
514
515 if (test_opt(sbi, DISCARD)) {
516 struct seg_entry *se = get_seg_entry(sbi,
517 GET_SEGNO(sbi, blkaddr));
518 unsigned int offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
519
520 if (f2fs_test_bit(offset, se->discard_map))
521 return;
522
523 err = f2fs_issue_discard(sbi, blkaddr, 1);
524 }
525
Chao Yu381722d2015-05-19 17:40:04 +0800526 if (err)
527 update_meta_page(sbi, NULL, blkaddr);
Jaegeuk Kim37208872013-11-12 16:55:17 +0900528}
529
Jaegeuk Kimadf49832014-10-28 22:27:59 -0700530static void __add_discard_entry(struct f2fs_sb_info *sbi,
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700531 struct cp_control *cpc, struct seg_entry *se,
532 unsigned int start, unsigned int end)
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900533{
534 struct list_head *head = &SM_I(sbi)->discard_list;
Jaegeuk Kimadf49832014-10-28 22:27:59 -0700535 struct discard_entry *new, *last;
536
537 if (!list_empty(head)) {
538 last = list_last_entry(head, struct discard_entry, list);
539 if (START_BLOCK(sbi, cpc->trim_start) + start ==
540 last->blkaddr + last->len) {
541 last->len += end - start;
542 goto done;
543 }
544 }
545
546 new = f2fs_kmem_cache_alloc(discard_entry_slab, GFP_NOFS);
547 INIT_LIST_HEAD(&new->list);
548 new->blkaddr = START_BLOCK(sbi, cpc->trim_start) + start;
549 new->len = end - start;
550 list_add_tail(&new->list, head);
551done:
552 SM_I(sbi)->nr_discards += end - start;
Jaegeuk Kimadf49832014-10-28 22:27:59 -0700553}
554
555static void add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc)
556{
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900557 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
558 int max_blocks = sbi->blocks_per_seg;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700559 struct seg_entry *se = get_seg_entry(sbi, cpc->trim_start);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900560 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
561 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700562 unsigned long *discard_map = (unsigned long *)se->discard_map;
Jaegeuk Kim60a3b782015-02-10 16:44:29 -0800563 unsigned long *dmap = SIT_I(sbi)->tmp_map;
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900564 unsigned int start = 0, end = -1;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700565 bool force = (cpc->reason == CP_DISCARD);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900566 int i;
567
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700568 if (se->valid_blocks == max_blocks)
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900569 return;
570
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700571 if (!force) {
572 if (!test_opt(sbi, DISCARD) || !se->valid_blocks ||
Dan Carpenter912a83b2015-05-14 11:52:28 +0300573 SM_I(sbi)->nr_discards >= SM_I(sbi)->max_discards)
574 return;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700575 }
576
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900577 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
578 for (i = 0; i < entries; i++)
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700579 dmap[i] = force ? ~ckpt_map[i] & ~discard_map[i] :
Jaegeuk Kimd7bc2482014-12-12 13:53:41 -0800580 (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900581
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700582 while (force || SM_I(sbi)->nr_discards <= SM_I(sbi)->max_discards) {
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900583 start = __find_rev_next_bit(dmap, max_blocks, end + 1);
584 if (start >= max_blocks)
585 break;
586
587 end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700588 __add_discard_entry(sbi, cpc, se, start, end);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900589 }
590}
591
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700592void release_discard_addrs(struct f2fs_sb_info *sbi)
593{
594 struct list_head *head = &(SM_I(sbi)->discard_list);
595 struct discard_entry *entry, *this;
596
597 /* drop caches */
598 list_for_each_entry_safe(entry, this, head, list) {
599 list_del(&entry->list);
600 kmem_cache_free(discard_entry_slab, entry);
601 }
602}
603
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900604/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900605 * Should call clear_prefree_segments after checkpoint is done.
606 */
607static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
608{
609 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Chao Yub65ee142014-08-04 10:10:07 +0800610 unsigned int segno;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900611
612 mutex_lock(&dirty_i->seglist_lock);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700613 for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900614 __set_test_and_free(sbi, segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900615 mutex_unlock(&dirty_i->seglist_lock);
616}
617
Jaegeuk Kim836b5a62015-04-30 22:50:06 -0700618void clear_prefree_segments(struct f2fs_sb_info *sbi, struct cp_control *cpc)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900619{
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900620 struct list_head *head = &(SM_I(sbi)->discard_list);
Chao Yu2d7b8222014-03-29 11:33:17 +0800621 struct discard_entry *entry, *this;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900622 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Changman Lee29e59c12013-11-11 09:24:37 +0900623 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
Changman Lee29e59c12013-11-11 09:24:37 +0900624 unsigned int start = 0, end = -1;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900625
626 mutex_lock(&dirty_i->seglist_lock);
Changman Lee29e59c12013-11-11 09:24:37 +0900627
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900628 while (1) {
Changman Lee29e59c12013-11-11 09:24:37 +0900629 int i;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700630 start = find_next_bit(prefree_map, MAIN_SEGS(sbi), end + 1);
631 if (start >= MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900632 break;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700633 end = find_next_zero_bit(prefree_map, MAIN_SEGS(sbi),
634 start + 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900635
Changman Lee29e59c12013-11-11 09:24:37 +0900636 for (i = start; i < end; i++)
637 clear_bit(i, prefree_map);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900638
Changman Lee29e59c12013-11-11 09:24:37 +0900639 dirty_i->nr_dirty[PRE] -= end - start;
640
641 if (!test_opt(sbi, DISCARD))
642 continue;
643
Jaegeuk Kim37208872013-11-12 16:55:17 +0900644 f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
645 (end - start) << sbi->log_blocks_per_seg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900646 }
647 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900648
649 /* send small discards */
Chao Yu2d7b8222014-03-29 11:33:17 +0800650 list_for_each_entry_safe(entry, this, head, list) {
Jaegeuk Kim836b5a62015-04-30 22:50:06 -0700651 if (cpc->reason == CP_DISCARD && entry->len < cpc->trim_minlen)
652 goto skip;
Jaegeuk Kim37208872013-11-12 16:55:17 +0900653 f2fs_issue_discard(sbi, entry->blkaddr, entry->len);
Jaegeuk Kimf56aa1c2015-06-02 15:48:20 -0700654 cpc->trimmed += entry->len;
Jaegeuk Kim836b5a62015-04-30 22:50:06 -0700655skip:
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900656 list_del(&entry->list);
657 SM_I(sbi)->nr_discards -= entry->len;
658 kmem_cache_free(discard_entry_slab, entry);
659 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900660}
661
Chao Yu184a5cd2014-09-04 18:13:01 +0800662static bool __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900663{
664 struct sit_info *sit_i = SIT_I(sbi);
Chao Yu184a5cd2014-09-04 18:13:01 +0800665
666 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900667 sit_i->dirty_sentries++;
Chao Yu184a5cd2014-09-04 18:13:01 +0800668 return false;
669 }
670
671 return true;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900672}
673
674static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
675 unsigned int segno, int modified)
676{
677 struct seg_entry *se = get_seg_entry(sbi, segno);
678 se->type = type;
679 if (modified)
680 __mark_sit_entry_dirty(sbi, segno);
681}
682
683static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
684{
685 struct seg_entry *se;
686 unsigned int segno, offset;
687 long int new_vblocks;
688
689 segno = GET_SEGNO(sbi, blkaddr);
690
691 se = get_seg_entry(sbi, segno);
692 new_vblocks = se->valid_blocks + del;
Jaegeuk Kim491c0852014-02-04 13:01:10 +0900693 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900694
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700695 f2fs_bug_on(sbi, (new_vblocks >> (sizeof(unsigned short) << 3) ||
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900696 (new_vblocks > sbi->blocks_per_seg)));
697
698 se->valid_blocks = new_vblocks;
699 se->mtime = get_mtime(sbi);
700 SIT_I(sbi)->max_mtime = se->mtime;
701
702 /* Update valid block bitmap */
703 if (del > 0) {
Gu Zheng52aca072014-10-20 17:45:51 +0800704 if (f2fs_test_and_set_bit(offset, se->cur_valid_map))
Jaegeuk Kim05796762014-09-02 16:05:00 -0700705 f2fs_bug_on(sbi, 1);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700706 if (!f2fs_test_and_set_bit(offset, se->discard_map))
707 sbi->discard_blks--;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900708 } else {
Gu Zheng52aca072014-10-20 17:45:51 +0800709 if (!f2fs_test_and_clear_bit(offset, se->cur_valid_map))
Jaegeuk Kim05796762014-09-02 16:05:00 -0700710 f2fs_bug_on(sbi, 1);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700711 if (f2fs_test_and_clear_bit(offset, se->discard_map))
712 sbi->discard_blks++;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900713 }
714 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
715 se->ckpt_valid_blocks += del;
716
717 __mark_sit_entry_dirty(sbi, segno);
718
719 /* update total number of valid blocks to be written in ckpt area */
720 SIT_I(sbi)->written_valid_blocks += del;
721
722 if (sbi->segs_per_sec > 1)
723 get_sec_entry(sbi, segno)->valid_blocks += del;
724}
725
Jaegeuk Kim5e443812014-01-28 12:22:14 +0900726void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900727{
Jaegeuk Kim5e443812014-01-28 12:22:14 +0900728 update_sit_entry(sbi, new, 1);
729 if (GET_SEGNO(sbi, old) != NULL_SEGNO)
730 update_sit_entry(sbi, old, -1);
731
732 locate_dirty_segment(sbi, GET_SEGNO(sbi, old));
733 locate_dirty_segment(sbi, GET_SEGNO(sbi, new));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900734}
735
736void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
737{
738 unsigned int segno = GET_SEGNO(sbi, addr);
739 struct sit_info *sit_i = SIT_I(sbi);
740
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700741 f2fs_bug_on(sbi, addr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900742 if (addr == NEW_ADDR)
743 return;
744
745 /* add it into sit main buffer */
746 mutex_lock(&sit_i->sentry_lock);
747
748 update_sit_entry(sbi, addr, -1);
749
750 /* add it into dirty seglist */
751 locate_dirty_segment(sbi, segno);
752
753 mutex_unlock(&sit_i->sentry_lock);
754}
755
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900756/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900757 * This function should be resided under the curseg_mutex lock
758 */
759static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
Haicheng Lie79efe32013-06-13 16:59:27 +0800760 struct f2fs_summary *sum)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900761{
762 struct curseg_info *curseg = CURSEG_I(sbi, type);
763 void *addr = curseg->sum_blk;
Haicheng Lie79efe32013-06-13 16:59:27 +0800764 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900765 memcpy(addr, sum, sizeof(struct f2fs_summary));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900766}
767
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900768/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900769 * Calculate the number of current summary pages for writing
770 */
Chao Yu3fa06d72014-12-09 14:21:46 +0800771int npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900772{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900773 int valid_sum_count = 0;
Fan Li9a479382013-10-29 16:21:47 +0800774 int i, sum_in_page;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900775
776 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
777 if (sbi->ckpt->alloc_type[i] == SSR)
778 valid_sum_count += sbi->blocks_per_seg;
Chao Yu3fa06d72014-12-09 14:21:46 +0800779 else {
780 if (for_ra)
781 valid_sum_count += le16_to_cpu(
782 F2FS_CKPT(sbi)->cur_data_blkoff[i]);
783 else
784 valid_sum_count += curseg_blkoff(sbi, i);
785 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900786 }
787
Fan Li9a479382013-10-29 16:21:47 +0800788 sum_in_page = (PAGE_CACHE_SIZE - 2 * SUM_JOURNAL_SIZE -
789 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
790 if (valid_sum_count <= sum_in_page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900791 return 1;
Fan Li9a479382013-10-29 16:21:47 +0800792 else if ((valid_sum_count - sum_in_page) <=
793 (PAGE_CACHE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900794 return 2;
795 return 3;
796}
797
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900798/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900799 * Caller should put this summary page
800 */
801struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
802{
803 return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
804}
805
Chao Yu381722d2015-05-19 17:40:04 +0800806void update_meta_page(struct f2fs_sb_info *sbi, void *src, block_t blk_addr)
807{
808 struct page *page = grab_meta_page(sbi, blk_addr);
809 void *dst = page_address(page);
810
811 if (src)
812 memcpy(dst, src, PAGE_CACHE_SIZE);
813 else
814 memset(dst, 0, PAGE_CACHE_SIZE);
815 set_page_dirty(page);
816 f2fs_put_page(page, 1);
817}
818
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900819static void write_sum_page(struct f2fs_sb_info *sbi,
820 struct f2fs_summary_block *sum_blk, block_t blk_addr)
821{
Chao Yu381722d2015-05-19 17:40:04 +0800822 update_meta_page(sbi, (void *)sum_blk, blk_addr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900823}
824
Jaegeuk Kim60374682013-03-31 13:58:51 +0900825static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
826{
827 struct curseg_info *curseg = CURSEG_I(sbi, type);
Haicheng Li81fb5e82013-05-14 18:20:28 +0800828 unsigned int segno = curseg->segno + 1;
Jaegeuk Kim60374682013-03-31 13:58:51 +0900829 struct free_segmap_info *free_i = FREE_I(sbi);
830
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700831 if (segno < MAIN_SEGS(sbi) && segno % sbi->segs_per_sec)
Haicheng Li81fb5e82013-05-14 18:20:28 +0800832 return !test_bit(segno, free_i->free_segmap);
Jaegeuk Kim60374682013-03-31 13:58:51 +0900833 return 0;
834}
835
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900836/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900837 * Find a new segment from the free segments bitmap to right order
838 * This function should be returned with success, otherwise BUG
839 */
840static void get_new_segment(struct f2fs_sb_info *sbi,
841 unsigned int *newseg, bool new_sec, int dir)
842{
843 struct free_segmap_info *free_i = FREE_I(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900844 unsigned int segno, secno, zoneno;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700845 unsigned int total_zones = MAIN_SECS(sbi) / sbi->secs_per_zone;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900846 unsigned int hint = *newseg / sbi->segs_per_sec;
847 unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
848 unsigned int left_start = hint;
849 bool init = true;
850 int go_left = 0;
851 int i;
852
Chao Yu1a118cc2015-02-11 18:20:38 +0800853 spin_lock(&free_i->segmap_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900854
855 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
856 segno = find_next_zero_bit(free_i->free_segmap,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700857 MAIN_SEGS(sbi), *newseg + 1);
Jaegeuk Kim33afa7f2013-03-31 12:59:53 +0900858 if (segno - *newseg < sbi->segs_per_sec -
859 (*newseg % sbi->segs_per_sec))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900860 goto got_it;
861 }
862find_other_zone:
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700863 secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint);
864 if (secno >= MAIN_SECS(sbi)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900865 if (dir == ALLOC_RIGHT) {
866 secno = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700867 MAIN_SECS(sbi), 0);
868 f2fs_bug_on(sbi, secno >= MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900869 } else {
870 go_left = 1;
871 left_start = hint - 1;
872 }
873 }
874 if (go_left == 0)
875 goto skip_left;
876
877 while (test_bit(left_start, free_i->free_secmap)) {
878 if (left_start > 0) {
879 left_start--;
880 continue;
881 }
882 left_start = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700883 MAIN_SECS(sbi), 0);
884 f2fs_bug_on(sbi, left_start >= MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900885 break;
886 }
887 secno = left_start;
888skip_left:
889 hint = secno;
890 segno = secno * sbi->segs_per_sec;
891 zoneno = secno / sbi->secs_per_zone;
892
893 /* give up on finding another zone */
894 if (!init)
895 goto got_it;
896 if (sbi->secs_per_zone == 1)
897 goto got_it;
898 if (zoneno == old_zoneno)
899 goto got_it;
900 if (dir == ALLOC_LEFT) {
901 if (!go_left && zoneno + 1 >= total_zones)
902 goto got_it;
903 if (go_left && zoneno == 0)
904 goto got_it;
905 }
906 for (i = 0; i < NR_CURSEG_TYPE; i++)
907 if (CURSEG_I(sbi, i)->zone == zoneno)
908 break;
909
910 if (i < NR_CURSEG_TYPE) {
911 /* zone is in user, try another */
912 if (go_left)
913 hint = zoneno * sbi->secs_per_zone - 1;
914 else if (zoneno + 1 >= total_zones)
915 hint = 0;
916 else
917 hint = (zoneno + 1) * sbi->secs_per_zone;
918 init = false;
919 goto find_other_zone;
920 }
921got_it:
922 /* set it as dirty segment in free segmap */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700923 f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900924 __set_inuse(sbi, segno);
925 *newseg = segno;
Chao Yu1a118cc2015-02-11 18:20:38 +0800926 spin_unlock(&free_i->segmap_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900927}
928
929static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
930{
931 struct curseg_info *curseg = CURSEG_I(sbi, type);
932 struct summary_footer *sum_footer;
933
934 curseg->segno = curseg->next_segno;
935 curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
936 curseg->next_blkoff = 0;
937 curseg->next_segno = NULL_SEGNO;
938
939 sum_footer = &(curseg->sum_blk->footer);
940 memset(sum_footer, 0, sizeof(struct summary_footer));
941 if (IS_DATASEG(type))
942 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
943 if (IS_NODESEG(type))
944 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
945 __set_sit_entry_type(sbi, type, curseg->segno, modified);
946}
947
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900948/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900949 * Allocate a current working segment.
950 * This function always allocates a free segment in LFS manner.
951 */
952static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
953{
954 struct curseg_info *curseg = CURSEG_I(sbi, type);
955 unsigned int segno = curseg->segno;
956 int dir = ALLOC_LEFT;
957
958 write_sum_page(sbi, curseg->sum_blk,
Haicheng Li81fb5e82013-05-14 18:20:28 +0800959 GET_SUM_BLOCK(sbi, segno));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900960 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
961 dir = ALLOC_RIGHT;
962
963 if (test_opt(sbi, NOHEAP))
964 dir = ALLOC_RIGHT;
965
966 get_new_segment(sbi, &segno, new_sec, dir);
967 curseg->next_segno = segno;
968 reset_curseg(sbi, type, 1);
969 curseg->alloc_type = LFS;
970}
971
972static void __next_free_blkoff(struct f2fs_sb_info *sbi,
973 struct curseg_info *seg, block_t start)
974{
975 struct seg_entry *se = get_seg_entry(sbi, seg->segno);
Changman Leee81c93c2013-11-15 13:21:16 +0900976 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
Jaegeuk Kim60a3b782015-02-10 16:44:29 -0800977 unsigned long *target_map = SIT_I(sbi)->tmp_map;
Changman Leee81c93c2013-11-15 13:21:16 +0900978 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
979 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
980 int i, pos;
981
982 for (i = 0; i < entries; i++)
983 target_map[i] = ckpt_map[i] | cur_map[i];
984
985 pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
986
987 seg->next_blkoff = pos;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900988}
989
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900990/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900991 * If a segment is written by LFS manner, next block offset is just obtained
992 * by increasing the current block offset. However, if a segment is written by
993 * SSR manner, next block offset obtained by calling __next_free_blkoff
994 */
995static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
996 struct curseg_info *seg)
997{
998 if (seg->alloc_type == SSR)
999 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
1000 else
1001 seg->next_blkoff++;
1002}
1003
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001004/*
arter97e1c42042014-08-06 23:22:50 +09001005 * This function always allocates a used segment(from dirty seglist) by SSR
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001006 * manner, so it should recover the existing segment information of valid blocks
1007 */
1008static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
1009{
1010 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1011 struct curseg_info *curseg = CURSEG_I(sbi, type);
1012 unsigned int new_segno = curseg->next_segno;
1013 struct f2fs_summary_block *sum_node;
1014 struct page *sum_page;
1015
1016 write_sum_page(sbi, curseg->sum_blk,
1017 GET_SUM_BLOCK(sbi, curseg->segno));
1018 __set_test_and_inuse(sbi, new_segno);
1019
1020 mutex_lock(&dirty_i->seglist_lock);
1021 __remove_dirty_segment(sbi, new_segno, PRE);
1022 __remove_dirty_segment(sbi, new_segno, DIRTY);
1023 mutex_unlock(&dirty_i->seglist_lock);
1024
1025 reset_curseg(sbi, type, 1);
1026 curseg->alloc_type = SSR;
1027 __next_free_blkoff(sbi, curseg, 0);
1028
1029 if (reuse) {
1030 sum_page = get_sum_page(sbi, new_segno);
1031 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
1032 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
1033 f2fs_put_page(sum_page, 1);
1034 }
1035}
1036
Jaegeuk Kim43727522013-02-04 15:11:17 +09001037static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
1038{
1039 struct curseg_info *curseg = CURSEG_I(sbi, type);
1040 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
1041
1042 if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0))
1043 return v_ops->get_victim(sbi,
1044 &(curseg)->next_segno, BG_GC, type, SSR);
1045
1046 /* For data segments, let's do SSR more intensively */
1047 for (; type >= CURSEG_HOT_DATA; type--)
1048 if (v_ops->get_victim(sbi, &(curseg)->next_segno,
1049 BG_GC, type, SSR))
1050 return 1;
1051 return 0;
1052}
1053
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001054/*
1055 * flush out current segment and replace it with new segment
1056 * This function should be returned with success, otherwise BUG
1057 */
1058static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
1059 int type, bool force)
1060{
1061 struct curseg_info *curseg = CURSEG_I(sbi, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001062
Gu Zheng7b405272013-08-19 09:41:15 +08001063 if (force)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001064 new_curseg(sbi, type, true);
Gu Zheng7b405272013-08-19 09:41:15 +08001065 else if (type == CURSEG_WARM_NODE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001066 new_curseg(sbi, type, false);
Jaegeuk Kim60374682013-03-31 13:58:51 +09001067 else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
1068 new_curseg(sbi, type, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001069 else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
1070 change_curseg(sbi, type, true);
1071 else
1072 new_curseg(sbi, type, false);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +09001073
1074 stat_inc_seg_type(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001075}
1076
Jaegeuk Kim38aa0882015-01-05 16:02:20 -08001077static void __allocate_new_segments(struct f2fs_sb_info *sbi, int type)
1078{
1079 struct curseg_info *curseg = CURSEG_I(sbi, type);
1080 unsigned int old_segno;
1081
1082 old_segno = curseg->segno;
1083 SIT_I(sbi)->s_ops->allocate_segment(sbi, type, true);
1084 locate_dirty_segment(sbi, old_segno);
1085}
1086
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001087void allocate_new_segments(struct f2fs_sb_info *sbi)
1088{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001089 int i;
1090
Jaegeuk Kim38aa0882015-01-05 16:02:20 -08001091 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++)
1092 __allocate_new_segments(sbi, i);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001093}
1094
1095static const struct segment_allocation default_salloc_ops = {
1096 .allocate_segment = allocate_segment_by_default,
1097};
1098
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001099int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range)
1100{
Jaegeuk Kimf7ef9b82015-02-09 12:02:44 -08001101 __u64 start = F2FS_BYTES_TO_BLK(range->start);
1102 __u64 end = start + F2FS_BYTES_TO_BLK(range->len) - 1;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001103 unsigned int start_segno, end_segno;
1104 struct cp_control cpc;
1105
Jaegeuk Kim836b5a62015-04-30 22:50:06 -07001106 if (start >= MAX_BLKADDR(sbi) || range->len < sbi->blocksize)
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001107 return -EINVAL;
1108
Jan Kara9bd27ae2014-10-21 14:07:33 +02001109 cpc.trimmed = 0;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001110 if (end <= MAIN_BLKADDR(sbi))
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001111 goto out;
1112
1113 /* start/end segment number in main_area */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001114 start_segno = (start <= MAIN_BLKADDR(sbi)) ? 0 : GET_SEGNO(sbi, start);
1115 end_segno = (end >= MAX_BLKADDR(sbi)) ? MAIN_SEGS(sbi) - 1 :
1116 GET_SEGNO(sbi, end);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001117 cpc.reason = CP_DISCARD;
Jaegeuk Kim836b5a62015-04-30 22:50:06 -07001118 cpc.trim_minlen = max_t(__u64, 1, F2FS_BYTES_TO_BLK(range->minlen));
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001119
1120 /* do checkpoint to issue discard commands safely */
Jaegeuk Kimbba681c2015-01-26 17:41:23 -08001121 for (; start_segno <= end_segno; start_segno = cpc.trim_end + 1) {
1122 cpc.trim_start = start_segno;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001123
1124 if (sbi->discard_blks == 0)
1125 break;
1126 else if (sbi->discard_blks < BATCHED_TRIM_BLOCKS(sbi))
1127 cpc.trim_end = end_segno;
1128 else
1129 cpc.trim_end = min_t(unsigned int,
1130 rounddown(start_segno +
Jaegeuk Kimbba681c2015-01-26 17:41:23 -08001131 BATCHED_TRIM_SEGMENTS(sbi),
1132 sbi->segs_per_sec) - 1, end_segno);
1133
1134 mutex_lock(&sbi->gc_mutex);
1135 write_checkpoint(sbi, &cpc);
1136 mutex_unlock(&sbi->gc_mutex);
1137 }
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001138out:
Jaegeuk Kimf7ef9b82015-02-09 12:02:44 -08001139 range->len = F2FS_BLK_TO_BYTES(cpc.trimmed);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001140 return 0;
1141}
1142
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001143static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
1144{
1145 struct curseg_info *curseg = CURSEG_I(sbi, type);
1146 if (curseg->next_blkoff < sbi->blocks_per_seg)
1147 return true;
1148 return false;
1149}
1150
1151static int __get_segment_type_2(struct page *page, enum page_type p_type)
1152{
1153 if (p_type == DATA)
1154 return CURSEG_HOT_DATA;
1155 else
1156 return CURSEG_HOT_NODE;
1157}
1158
1159static int __get_segment_type_4(struct page *page, enum page_type p_type)
1160{
1161 if (p_type == DATA) {
1162 struct inode *inode = page->mapping->host;
1163
1164 if (S_ISDIR(inode->i_mode))
1165 return CURSEG_HOT_DATA;
1166 else
1167 return CURSEG_COLD_DATA;
1168 } else {
Jaegeuk Kima344b9f2014-11-05 20:05:53 -08001169 if (IS_DNODE(page) && is_cold_node(page))
1170 return CURSEG_WARM_NODE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001171 else
1172 return CURSEG_COLD_NODE;
1173 }
1174}
1175
1176static int __get_segment_type_6(struct page *page, enum page_type p_type)
1177{
1178 if (p_type == DATA) {
1179 struct inode *inode = page->mapping->host;
1180
1181 if (S_ISDIR(inode->i_mode))
1182 return CURSEG_HOT_DATA;
Jaegeuk Kim354a3392013-06-14 08:52:35 +09001183 else if (is_cold_data(page) || file_is_cold(inode))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001184 return CURSEG_COLD_DATA;
1185 else
1186 return CURSEG_WARM_DATA;
1187 } else {
1188 if (IS_DNODE(page))
1189 return is_cold_node(page) ? CURSEG_WARM_NODE :
1190 CURSEG_HOT_NODE;
1191 else
1192 return CURSEG_COLD_NODE;
1193 }
1194}
1195
1196static int __get_segment_type(struct page *page, enum page_type p_type)
1197{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001198 switch (F2FS_P_SB(page)->active_logs) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001199 case 2:
1200 return __get_segment_type_2(page, p_type);
1201 case 4:
1202 return __get_segment_type_4(page, p_type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001203 }
Jaegeuk Kim12a67142012-12-21 11:47:05 +09001204 /* NR_CURSEG_TYPE(6) logs by default */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001205 f2fs_bug_on(F2FS_P_SB(page),
1206 F2FS_P_SB(page)->active_logs != NR_CURSEG_TYPE);
Jaegeuk Kim12a67142012-12-21 11:47:05 +09001207 return __get_segment_type_6(page, p_type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001208}
1209
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001210void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
1211 block_t old_blkaddr, block_t *new_blkaddr,
1212 struct f2fs_summary *sum, int type)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001213{
1214 struct sit_info *sit_i = SIT_I(sbi);
1215 struct curseg_info *curseg;
Jaegeuk Kim38aa0882015-01-05 16:02:20 -08001216 bool direct_io = (type == CURSEG_DIRECT_IO);
1217
1218 type = direct_io ? CURSEG_WARM_DATA : type;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001219
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001220 curseg = CURSEG_I(sbi, type);
1221
1222 mutex_lock(&curseg->curseg_mutex);
Jaegeuk Kim21cb1d92015-03-11 13:42:48 -04001223 mutex_lock(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001224
Jaegeuk Kim38aa0882015-01-05 16:02:20 -08001225 /* direct_io'ed data is aligned to the segment for better performance */
1226 if (direct_io && curseg->next_blkoff)
1227 __allocate_new_segments(sbi, type);
1228
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001229 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001230
1231 /*
1232 * __add_sum_entry should be resided under the curseg_mutex
1233 * because, this function updates a summary entry in the
1234 * current summary block.
1235 */
Haicheng Lie79efe32013-06-13 16:59:27 +08001236 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001237
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001238 __refresh_next_blkoff(sbi, curseg);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +09001239
1240 stat_inc_block_count(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001241
Jaegeuk Kim5e443812014-01-28 12:22:14 +09001242 if (!__has_curseg_space(sbi, type))
1243 sit_i->s_ops->allocate_segment(sbi, type, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001244 /*
1245 * SIT information should be updated before segment allocation,
1246 * since SSR needs latest valid block information.
1247 */
1248 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
Jaegeuk Kim5e443812014-01-28 12:22:14 +09001249
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001250 mutex_unlock(&sit_i->sentry_lock);
1251
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001252 if (page && IS_NODESEG(type))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001253 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
1254
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001255 mutex_unlock(&curseg->curseg_mutex);
1256}
1257
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001258static void do_write_page(struct f2fs_summary *sum, struct f2fs_io_info *fio)
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001259{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001260 int type = __get_segment_type(fio->page, fio->type);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001261
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001262 allocate_data_block(fio->sbi, fio->page, fio->blk_addr,
1263 &fio->blk_addr, sum, type);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001264
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001265 /* writeout dirty page into bdev */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001266 f2fs_submit_page_mbio(fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001267}
1268
Jaegeuk Kim577e3492013-01-24 19:56:11 +09001269void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001270{
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001271 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001272 .sbi = sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001273 .type = META,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001274 .rw = WRITE_SYNC | REQ_META | REQ_PRIO,
1275 .blk_addr = page->index,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001276 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001277 .encrypted_page = NULL,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001278 };
1279
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001280 set_page_writeback(page);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001281 f2fs_submit_page_mbio(&fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001282}
1283
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001284void write_node_page(unsigned int nid, struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001285{
1286 struct f2fs_summary sum;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001287
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001288 set_summary(&sum, nid, 0, 0);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001289 do_write_page(&sum, fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001290}
1291
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001292void write_data_page(struct dnode_of_data *dn, struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001293{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001294 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001295 struct f2fs_summary sum;
1296 struct node_info ni;
1297
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001298 f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001299 get_node_info(sbi, dn->nid, &ni);
1300 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001301 do_write_page(&sum, fio);
Jaegeuk Kime1509cf2014-12-30 22:57:55 -08001302 dn->data_blkaddr = fio->blk_addr;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001303}
1304
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001305void rewrite_data_page(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001306{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001307 stat_inc_inplace_blocks(fio->sbi);
1308 f2fs_submit_page_mbio(fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001309}
1310
Chao Yu528e3452015-05-28 19:15:35 +08001311static void __f2fs_replace_block(struct f2fs_sb_info *sbi,
1312 struct f2fs_summary *sum,
Chao Yu19f106b2015-05-06 13:08:06 +08001313 block_t old_blkaddr, block_t new_blkaddr,
1314 bool recover_curseg)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001315{
1316 struct sit_info *sit_i = SIT_I(sbi);
1317 struct curseg_info *curseg;
1318 unsigned int segno, old_cursegno;
1319 struct seg_entry *se;
1320 int type;
Chao Yu19f106b2015-05-06 13:08:06 +08001321 unsigned short old_blkoff;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001322
1323 segno = GET_SEGNO(sbi, new_blkaddr);
1324 se = get_seg_entry(sbi, segno);
1325 type = se->type;
1326
Chao Yu19f106b2015-05-06 13:08:06 +08001327 if (!recover_curseg) {
1328 /* for recovery flow */
1329 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
1330 if (old_blkaddr == NULL_ADDR)
1331 type = CURSEG_COLD_DATA;
1332 else
1333 type = CURSEG_WARM_DATA;
1334 }
1335 } else {
1336 if (!IS_CURSEG(sbi, segno))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001337 type = CURSEG_WARM_DATA;
1338 }
Chao Yu19f106b2015-05-06 13:08:06 +08001339
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001340 curseg = CURSEG_I(sbi, type);
1341
1342 mutex_lock(&curseg->curseg_mutex);
1343 mutex_lock(&sit_i->sentry_lock);
1344
1345 old_cursegno = curseg->segno;
Chao Yu19f106b2015-05-06 13:08:06 +08001346 old_blkoff = curseg->next_blkoff;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001347
1348 /* change the current segment */
1349 if (segno != curseg->segno) {
1350 curseg->next_segno = segno;
1351 change_curseg(sbi, type, true);
1352 }
1353
Jaegeuk Kim491c0852014-02-04 13:01:10 +09001354 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
Haicheng Lie79efe32013-06-13 16:59:27 +08001355 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001356
1357 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001358 locate_dirty_segment(sbi, old_cursegno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001359
Chao Yu19f106b2015-05-06 13:08:06 +08001360 if (recover_curseg) {
1361 if (old_cursegno != curseg->segno) {
1362 curseg->next_segno = old_cursegno;
1363 change_curseg(sbi, type, true);
1364 }
1365 curseg->next_blkoff = old_blkoff;
1366 }
1367
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001368 mutex_unlock(&sit_i->sentry_lock);
1369 mutex_unlock(&curseg->curseg_mutex);
1370}
1371
Chao Yu528e3452015-05-28 19:15:35 +08001372void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
1373 block_t old_addr, block_t new_addr,
1374 unsigned char version, bool recover_curseg)
1375{
1376 struct f2fs_summary sum;
1377
1378 set_summary(&sum, dn->nid, dn->ofs_in_node, version);
1379
1380 __f2fs_replace_block(sbi, &sum, old_addr, new_addr, recover_curseg);
1381
1382 dn->data_blkaddr = new_addr;
1383 set_data_blkaddr(dn);
1384 f2fs_update_extent_cache(dn);
1385}
1386
Chao Yudf0f8dc2014-03-22 14:57:23 +08001387static inline bool is_merged_page(struct f2fs_sb_info *sbi,
1388 struct page *page, enum page_type type)
1389{
1390 enum page_type btype = PAGE_TYPE_OF_BIO(type);
1391 struct f2fs_bio_info *io = &sbi->write_io[btype];
Chao Yudf0f8dc2014-03-22 14:57:23 +08001392 struct bio_vec *bvec;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001393 struct page *target;
Chao Yudf0f8dc2014-03-22 14:57:23 +08001394 int i;
1395
1396 down_read(&io->io_rwsem);
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001397 if (!io->bio) {
1398 up_read(&io->io_rwsem);
1399 return false;
1400 }
Chao Yudf0f8dc2014-03-22 14:57:23 +08001401
Jaegeuk Kimce234472014-04-02 09:04:42 +09001402 bio_for_each_segment_all(bvec, io->bio, i) {
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001403
1404 if (bvec->bv_page->mapping) {
1405 target = bvec->bv_page;
1406 } else {
1407 struct f2fs_crypto_ctx *ctx;
1408
1409 /* encrypted page */
1410 ctx = (struct f2fs_crypto_ctx *)page_private(
1411 bvec->bv_page);
Jaegeuk Kimca40b032015-05-12 13:40:20 -07001412 target = ctx->w.control_page;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001413 }
1414
1415 if (page == target) {
Chao Yudf0f8dc2014-03-22 14:57:23 +08001416 up_read(&io->io_rwsem);
1417 return true;
1418 }
1419 }
1420
Chao Yudf0f8dc2014-03-22 14:57:23 +08001421 up_read(&io->io_rwsem);
1422 return false;
1423}
1424
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001425void f2fs_wait_on_page_writeback(struct page *page,
Yuan Zhong5514f0a2014-01-10 07:26:14 +00001426 enum page_type type)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001427{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001428 if (PageWriteback(page)) {
Jaegeuk Kim40813632014-09-02 15:31:18 -07001429 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
1430
Chao Yudf0f8dc2014-03-22 14:57:23 +08001431 if (is_merged_page(sbi, page, type))
1432 f2fs_submit_merged_bio(sbi, type, WRITE);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001433 wait_on_page_writeback(page);
1434 }
1435}
1436
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001437static int read_compacted_summaries(struct f2fs_sb_info *sbi)
1438{
1439 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1440 struct curseg_info *seg_i;
1441 unsigned char *kaddr;
1442 struct page *page;
1443 block_t start;
1444 int i, j, offset;
1445
1446 start = start_sum_block(sbi);
1447
1448 page = get_meta_page(sbi, start++);
1449 kaddr = (unsigned char *)page_address(page);
1450
1451 /* Step 1: restore nat cache */
1452 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1453 memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
1454
1455 /* Step 2: restore sit cache */
1456 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1457 memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
1458 SUM_JOURNAL_SIZE);
1459 offset = 2 * SUM_JOURNAL_SIZE;
1460
1461 /* Step 3: restore summary entries */
1462 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1463 unsigned short blk_off;
1464 unsigned int segno;
1465
1466 seg_i = CURSEG_I(sbi, i);
1467 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
1468 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
1469 seg_i->next_segno = segno;
1470 reset_curseg(sbi, i, 0);
1471 seg_i->alloc_type = ckpt->alloc_type[i];
1472 seg_i->next_blkoff = blk_off;
1473
1474 if (seg_i->alloc_type == SSR)
1475 blk_off = sbi->blocks_per_seg;
1476
1477 for (j = 0; j < blk_off; j++) {
1478 struct f2fs_summary *s;
1479 s = (struct f2fs_summary *)(kaddr + offset);
1480 seg_i->sum_blk->entries[j] = *s;
1481 offset += SUMMARY_SIZE;
1482 if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1483 SUM_FOOTER_SIZE)
1484 continue;
1485
1486 f2fs_put_page(page, 1);
1487 page = NULL;
1488
1489 page = get_meta_page(sbi, start++);
1490 kaddr = (unsigned char *)page_address(page);
1491 offset = 0;
1492 }
1493 }
1494 f2fs_put_page(page, 1);
1495 return 0;
1496}
1497
1498static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
1499{
1500 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1501 struct f2fs_summary_block *sum;
1502 struct curseg_info *curseg;
1503 struct page *new;
1504 unsigned short blk_off;
1505 unsigned int segno = 0;
1506 block_t blk_addr = 0;
1507
1508 /* get segment number and block addr */
1509 if (IS_DATASEG(type)) {
1510 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
1511 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
1512 CURSEG_HOT_DATA]);
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001513 if (__exist_node_summaries(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001514 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
1515 else
1516 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
1517 } else {
1518 segno = le32_to_cpu(ckpt->cur_node_segno[type -
1519 CURSEG_HOT_NODE]);
1520 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
1521 CURSEG_HOT_NODE]);
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001522 if (__exist_node_summaries(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001523 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
1524 type - CURSEG_HOT_NODE);
1525 else
1526 blk_addr = GET_SUM_BLOCK(sbi, segno);
1527 }
1528
1529 new = get_meta_page(sbi, blk_addr);
1530 sum = (struct f2fs_summary_block *)page_address(new);
1531
1532 if (IS_NODESEG(type)) {
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001533 if (__exist_node_summaries(sbi)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001534 struct f2fs_summary *ns = &sum->entries[0];
1535 int i;
1536 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
1537 ns->version = 0;
1538 ns->ofs_in_node = 0;
1539 }
1540 } else {
Gu Zhengd6537882014-03-07 18:43:36 +08001541 int err;
1542
1543 err = restore_node_summary(sbi, segno, sum);
1544 if (err) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001545 f2fs_put_page(new, 1);
Gu Zhengd6537882014-03-07 18:43:36 +08001546 return err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001547 }
1548 }
1549 }
1550
1551 /* set uncompleted segment to curseg */
1552 curseg = CURSEG_I(sbi, type);
1553 mutex_lock(&curseg->curseg_mutex);
1554 memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
1555 curseg->next_segno = segno;
1556 reset_curseg(sbi, type, 0);
1557 curseg->alloc_type = ckpt->alloc_type[type];
1558 curseg->next_blkoff = blk_off;
1559 mutex_unlock(&curseg->curseg_mutex);
1560 f2fs_put_page(new, 1);
1561 return 0;
1562}
1563
1564static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
1565{
1566 int type = CURSEG_HOT_DATA;
Chao Yue4fc5fb2014-03-17 16:36:24 +08001567 int err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001568
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001569 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
Chao Yu3fa06d72014-12-09 14:21:46 +08001570 int npages = npages_for_summary_flush(sbi, true);
1571
1572 if (npages >= 2)
1573 ra_meta_pages(sbi, start_sum_block(sbi), npages,
1574 META_CP);
1575
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001576 /* restore for compacted data summary */
1577 if (read_compacted_summaries(sbi))
1578 return -EINVAL;
1579 type = CURSEG_HOT_NODE;
1580 }
1581
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001582 if (__exist_node_summaries(sbi))
Chao Yu3fa06d72014-12-09 14:21:46 +08001583 ra_meta_pages(sbi, sum_blk_addr(sbi, NR_CURSEG_TYPE, type),
1584 NR_CURSEG_TYPE - type, META_CP);
1585
Chao Yue4fc5fb2014-03-17 16:36:24 +08001586 for (; type <= CURSEG_COLD_NODE; type++) {
1587 err = read_normal_summaries(sbi, type);
1588 if (err)
1589 return err;
1590 }
1591
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001592 return 0;
1593}
1594
1595static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
1596{
1597 struct page *page;
1598 unsigned char *kaddr;
1599 struct f2fs_summary *summary;
1600 struct curseg_info *seg_i;
1601 int written_size = 0;
1602 int i, j;
1603
1604 page = grab_meta_page(sbi, blkaddr++);
1605 kaddr = (unsigned char *)page_address(page);
1606
1607 /* Step 1: write nat cache */
1608 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1609 memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
1610 written_size += SUM_JOURNAL_SIZE;
1611
1612 /* Step 2: write sit cache */
1613 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1614 memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
1615 SUM_JOURNAL_SIZE);
1616 written_size += SUM_JOURNAL_SIZE;
1617
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001618 /* Step 3: write summary entries */
1619 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1620 unsigned short blkoff;
1621 seg_i = CURSEG_I(sbi, i);
1622 if (sbi->ckpt->alloc_type[i] == SSR)
1623 blkoff = sbi->blocks_per_seg;
1624 else
1625 blkoff = curseg_blkoff(sbi, i);
1626
1627 for (j = 0; j < blkoff; j++) {
1628 if (!page) {
1629 page = grab_meta_page(sbi, blkaddr++);
1630 kaddr = (unsigned char *)page_address(page);
1631 written_size = 0;
1632 }
1633 summary = (struct f2fs_summary *)(kaddr + written_size);
1634 *summary = seg_i->sum_blk->entries[j];
1635 written_size += SUMMARY_SIZE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001636
1637 if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1638 SUM_FOOTER_SIZE)
1639 continue;
1640
Chao Yue8d61a72013-10-24 15:08:28 +08001641 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001642 f2fs_put_page(page, 1);
1643 page = NULL;
1644 }
1645 }
Chao Yue8d61a72013-10-24 15:08:28 +08001646 if (page) {
1647 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001648 f2fs_put_page(page, 1);
Chao Yue8d61a72013-10-24 15:08:28 +08001649 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001650}
1651
1652static void write_normal_summaries(struct f2fs_sb_info *sbi,
1653 block_t blkaddr, int type)
1654{
1655 int i, end;
1656 if (IS_DATASEG(type))
1657 end = type + NR_CURSEG_DATA_TYPE;
1658 else
1659 end = type + NR_CURSEG_NODE_TYPE;
1660
1661 for (i = type; i < end; i++) {
1662 struct curseg_info *sum = CURSEG_I(sbi, i);
1663 mutex_lock(&sum->curseg_mutex);
1664 write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
1665 mutex_unlock(&sum->curseg_mutex);
1666 }
1667}
1668
1669void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1670{
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001671 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001672 write_compacted_summaries(sbi, start_blk);
1673 else
1674 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
1675}
1676
1677void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1678{
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001679 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001680}
1681
1682int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
1683 unsigned int val, int alloc)
1684{
1685 int i;
1686
1687 if (type == NAT_JOURNAL) {
1688 for (i = 0; i < nats_in_cursum(sum); i++) {
1689 if (le32_to_cpu(nid_in_journal(sum, i)) == val)
1690 return i;
1691 }
1692 if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
1693 return update_nats_in_cursum(sum, 1);
1694 } else if (type == SIT_JOURNAL) {
1695 for (i = 0; i < sits_in_cursum(sum); i++)
1696 if (le32_to_cpu(segno_in_journal(sum, i)) == val)
1697 return i;
1698 if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
1699 return update_sits_in_cursum(sum, 1);
1700 }
1701 return -1;
1702}
1703
1704static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
1705 unsigned int segno)
1706{
Gu Zheng2cc22182014-10-20 17:45:49 +08001707 return get_meta_page(sbi, current_sit_addr(sbi, segno));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001708}
1709
1710static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
1711 unsigned int start)
1712{
1713 struct sit_info *sit_i = SIT_I(sbi);
1714 struct page *src_page, *dst_page;
1715 pgoff_t src_off, dst_off;
1716 void *src_addr, *dst_addr;
1717
1718 src_off = current_sit_addr(sbi, start);
1719 dst_off = next_sit_addr(sbi, src_off);
1720
1721 /* get current sit block page without lock */
1722 src_page = get_meta_page(sbi, src_off);
1723 dst_page = grab_meta_page(sbi, dst_off);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001724 f2fs_bug_on(sbi, PageDirty(src_page));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001725
1726 src_addr = page_address(src_page);
1727 dst_addr = page_address(dst_page);
1728 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
1729
1730 set_page_dirty(dst_page);
1731 f2fs_put_page(src_page, 1);
1732
1733 set_to_next_sit(sit_i, start);
1734
1735 return dst_page;
1736}
1737
Chao Yu184a5cd2014-09-04 18:13:01 +08001738static struct sit_entry_set *grab_sit_entry_set(void)
1739{
1740 struct sit_entry_set *ses =
1741 f2fs_kmem_cache_alloc(sit_entry_set_slab, GFP_ATOMIC);
1742
1743 ses->entry_cnt = 0;
1744 INIT_LIST_HEAD(&ses->set_list);
1745 return ses;
1746}
1747
1748static void release_sit_entry_set(struct sit_entry_set *ses)
1749{
1750 list_del(&ses->set_list);
1751 kmem_cache_free(sit_entry_set_slab, ses);
1752}
1753
1754static void adjust_sit_entry_set(struct sit_entry_set *ses,
1755 struct list_head *head)
1756{
1757 struct sit_entry_set *next = ses;
1758
1759 if (list_is_last(&ses->set_list, head))
1760 return;
1761
1762 list_for_each_entry_continue(next, head, set_list)
1763 if (ses->entry_cnt <= next->entry_cnt)
1764 break;
1765
1766 list_move_tail(&ses->set_list, &next->set_list);
1767}
1768
1769static void add_sit_entry(unsigned int segno, struct list_head *head)
1770{
1771 struct sit_entry_set *ses;
1772 unsigned int start_segno = START_SEGNO(segno);
1773
1774 list_for_each_entry(ses, head, set_list) {
1775 if (ses->start_segno == start_segno) {
1776 ses->entry_cnt++;
1777 adjust_sit_entry_set(ses, head);
1778 return;
1779 }
1780 }
1781
1782 ses = grab_sit_entry_set();
1783
1784 ses->start_segno = start_segno;
1785 ses->entry_cnt++;
1786 list_add(&ses->set_list, head);
1787}
1788
1789static void add_sits_in_set(struct f2fs_sb_info *sbi)
1790{
1791 struct f2fs_sm_info *sm_info = SM_I(sbi);
1792 struct list_head *set_list = &sm_info->sit_entry_set;
1793 unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap;
Chao Yu184a5cd2014-09-04 18:13:01 +08001794 unsigned int segno;
1795
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001796 for_each_set_bit(segno, bitmap, MAIN_SEGS(sbi))
Chao Yu184a5cd2014-09-04 18:13:01 +08001797 add_sit_entry(segno, set_list);
1798}
1799
1800static void remove_sits_in_journal(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001801{
1802 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1803 struct f2fs_summary_block *sum = curseg->sum_blk;
1804 int i;
1805
Chao Yu184a5cd2014-09-04 18:13:01 +08001806 for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
1807 unsigned int segno;
1808 bool dirtied;
1809
1810 segno = le32_to_cpu(segno_in_journal(sum, i));
1811 dirtied = __mark_sit_entry_dirty(sbi, segno);
1812
1813 if (!dirtied)
1814 add_sit_entry(segno, &SM_I(sbi)->sit_entry_set);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001815 }
Chao Yu184a5cd2014-09-04 18:13:01 +08001816 update_sits_in_cursum(sum, -sits_in_cursum(sum));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001817}
1818
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001819/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001820 * CP calls this function, which flushes SIT entries including sit_journal,
1821 * and moves prefree segs to free segs.
1822 */
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001823void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001824{
1825 struct sit_info *sit_i = SIT_I(sbi);
1826 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
1827 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1828 struct f2fs_summary_block *sum = curseg->sum_blk;
Chao Yu184a5cd2014-09-04 18:13:01 +08001829 struct sit_entry_set *ses, *tmp;
1830 struct list_head *head = &SM_I(sbi)->sit_entry_set;
Chao Yu184a5cd2014-09-04 18:13:01 +08001831 bool to_journal = true;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001832 struct seg_entry *se;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001833
1834 mutex_lock(&curseg->curseg_mutex);
1835 mutex_lock(&sit_i->sentry_lock);
1836
Wanpeng Li2b11a742015-02-27 16:52:50 +08001837 if (!sit_i->dirty_sentries)
1838 goto out;
1839
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001840 /*
Chao Yu184a5cd2014-09-04 18:13:01 +08001841 * add and account sit entries of dirty bitmap in sit entry
1842 * set temporarily
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001843 */
Chao Yu184a5cd2014-09-04 18:13:01 +08001844 add_sits_in_set(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001845
Chao Yu184a5cd2014-09-04 18:13:01 +08001846 /*
1847 * if there are no enough space in journal to store dirty sit
1848 * entries, remove all entries from journal and add and account
1849 * them in sit entry set.
1850 */
1851 if (!__has_cursum_space(sum, sit_i->dirty_sentries, SIT_JOURNAL))
1852 remove_sits_in_journal(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001853
Chao Yu184a5cd2014-09-04 18:13:01 +08001854 /*
1855 * there are two steps to flush sit entries:
1856 * #1, flush sit entries to journal in current cold data summary block.
1857 * #2, flush sit entries to sit page.
1858 */
1859 list_for_each_entry_safe(ses, tmp, head, set_list) {
Jaegeuk Kim4a257ed2014-10-16 11:43:30 -07001860 struct page *page = NULL;
Chao Yu184a5cd2014-09-04 18:13:01 +08001861 struct f2fs_sit_block *raw_sit = NULL;
1862 unsigned int start_segno = ses->start_segno;
1863 unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001864 (unsigned long)MAIN_SEGS(sbi));
Chao Yu184a5cd2014-09-04 18:13:01 +08001865 unsigned int segno = start_segno;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001866
Chao Yu184a5cd2014-09-04 18:13:01 +08001867 if (to_journal &&
1868 !__has_cursum_space(sum, ses->entry_cnt, SIT_JOURNAL))
1869 to_journal = false;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001870
Chao Yu184a5cd2014-09-04 18:13:01 +08001871 if (!to_journal) {
1872 page = get_next_sit_page(sbi, start_segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001873 raw_sit = page_address(page);
1874 }
1875
Chao Yu184a5cd2014-09-04 18:13:01 +08001876 /* flush dirty sit entries in region of current sit set */
1877 for_each_set_bit_from(segno, bitmap, end) {
1878 int offset, sit_offset;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001879
1880 se = get_seg_entry(sbi, segno);
Chao Yu184a5cd2014-09-04 18:13:01 +08001881
1882 /* add discard candidates */
Jaegeuk Kimd7bc2482014-12-12 13:53:41 -08001883 if (cpc->reason != CP_DISCARD) {
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001884 cpc->trim_start = segno;
1885 add_discard_addrs(sbi, cpc);
1886 }
Chao Yu184a5cd2014-09-04 18:13:01 +08001887
1888 if (to_journal) {
1889 offset = lookup_journal_in_cursum(sum,
1890 SIT_JOURNAL, segno, 1);
1891 f2fs_bug_on(sbi, offset < 0);
1892 segno_in_journal(sum, offset) =
1893 cpu_to_le32(segno);
1894 seg_info_to_raw_sit(se,
1895 &sit_in_journal(sum, offset));
1896 } else {
1897 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
1898 seg_info_to_raw_sit(se,
1899 &raw_sit->entries[sit_offset]);
1900 }
1901
1902 __clear_bit(segno, bitmap);
1903 sit_i->dirty_sentries--;
1904 ses->entry_cnt--;
1905 }
1906
1907 if (!to_journal)
1908 f2fs_put_page(page, 1);
1909
1910 f2fs_bug_on(sbi, ses->entry_cnt);
1911 release_sit_entry_set(ses);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001912 }
Chao Yu184a5cd2014-09-04 18:13:01 +08001913
1914 f2fs_bug_on(sbi, !list_empty(head));
1915 f2fs_bug_on(sbi, sit_i->dirty_sentries);
Chao Yu184a5cd2014-09-04 18:13:01 +08001916out:
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001917 if (cpc->reason == CP_DISCARD) {
1918 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++)
1919 add_discard_addrs(sbi, cpc);
1920 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001921 mutex_unlock(&sit_i->sentry_lock);
1922 mutex_unlock(&curseg->curseg_mutex);
1923
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001924 set_prefree_as_free_segments(sbi);
1925}
1926
1927static int build_sit_info(struct f2fs_sb_info *sbi)
1928{
1929 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1930 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1931 struct sit_info *sit_i;
1932 unsigned int sit_segs, start;
1933 char *src_bitmap, *dst_bitmap;
1934 unsigned int bitmap_size;
1935
1936 /* allocate memory for SIT information */
1937 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
1938 if (!sit_i)
1939 return -ENOMEM;
1940
1941 SM_I(sbi)->sit_info = sit_i;
1942
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001943 sit_i->sentries = vzalloc(MAIN_SEGS(sbi) * sizeof(struct seg_entry));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001944 if (!sit_i->sentries)
1945 return -ENOMEM;
1946
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001947 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001948 sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1949 if (!sit_i->dirty_sentries_bitmap)
1950 return -ENOMEM;
1951
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001952 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001953 sit_i->sentries[start].cur_valid_map
1954 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1955 sit_i->sentries[start].ckpt_valid_map
1956 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001957 sit_i->sentries[start].discard_map
1958 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1959 if (!sit_i->sentries[start].cur_valid_map ||
1960 !sit_i->sentries[start].ckpt_valid_map ||
1961 !sit_i->sentries[start].discard_map)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001962 return -ENOMEM;
1963 }
1964
Jaegeuk Kim60a3b782015-02-10 16:44:29 -08001965 sit_i->tmp_map = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1966 if (!sit_i->tmp_map)
1967 return -ENOMEM;
1968
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001969 if (sbi->segs_per_sec > 1) {
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001970 sit_i->sec_entries = vzalloc(MAIN_SECS(sbi) *
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001971 sizeof(struct sec_entry));
1972 if (!sit_i->sec_entries)
1973 return -ENOMEM;
1974 }
1975
1976 /* get information related with SIT */
1977 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
1978
1979 /* setup SIT bitmap from ckeckpoint pack */
1980 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
1981 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
1982
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001983 dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001984 if (!dst_bitmap)
1985 return -ENOMEM;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001986
1987 /* init SIT information */
1988 sit_i->s_ops = &default_salloc_ops;
1989
1990 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
1991 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
1992 sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
1993 sit_i->sit_bitmap = dst_bitmap;
1994 sit_i->bitmap_size = bitmap_size;
1995 sit_i->dirty_sentries = 0;
1996 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
1997 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
1998 sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
1999 mutex_init(&sit_i->sentry_lock);
2000 return 0;
2001}
2002
2003static int build_free_segmap(struct f2fs_sb_info *sbi)
2004{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002005 struct free_segmap_info *free_i;
2006 unsigned int bitmap_size, sec_bitmap_size;
2007
2008 /* allocate memory for free segmap information */
2009 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
2010 if (!free_i)
2011 return -ENOMEM;
2012
2013 SM_I(sbi)->free_info = free_i;
2014
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002015 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002016 free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL);
2017 if (!free_i->free_segmap)
2018 return -ENOMEM;
2019
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002020 sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002021 free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL);
2022 if (!free_i->free_secmap)
2023 return -ENOMEM;
2024
2025 /* set all segments as dirty temporarily */
2026 memset(free_i->free_segmap, 0xff, bitmap_size);
2027 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
2028
2029 /* init free segmap information */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002030 free_i->start_segno = GET_SEGNO_FROM_SEG0(sbi, MAIN_BLKADDR(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002031 free_i->free_segments = 0;
2032 free_i->free_sections = 0;
Chao Yu1a118cc2015-02-11 18:20:38 +08002033 spin_lock_init(&free_i->segmap_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002034 return 0;
2035}
2036
2037static int build_curseg(struct f2fs_sb_info *sbi)
2038{
Namjae Jeon1042d602012-12-01 10:56:13 +09002039 struct curseg_info *array;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002040 int i;
2041
Fabian Frederickb434bab2014-06-23 18:39:15 +02002042 array = kcalloc(NR_CURSEG_TYPE, sizeof(*array), GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002043 if (!array)
2044 return -ENOMEM;
2045
2046 SM_I(sbi)->curseg_array = array;
2047
2048 for (i = 0; i < NR_CURSEG_TYPE; i++) {
2049 mutex_init(&array[i].curseg_mutex);
2050 array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
2051 if (!array[i].sum_blk)
2052 return -ENOMEM;
2053 array[i].segno = NULL_SEGNO;
2054 array[i].next_blkoff = 0;
2055 }
2056 return restore_curseg_summaries(sbi);
2057}
2058
2059static void build_sit_entries(struct f2fs_sb_info *sbi)
2060{
2061 struct sit_info *sit_i = SIT_I(sbi);
2062 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
2063 struct f2fs_summary_block *sum = curseg->sum_blk;
Chao Yu74de5932013-11-22 09:09:59 +08002064 int sit_blk_cnt = SIT_BLK_CNT(sbi);
2065 unsigned int i, start, end;
2066 unsigned int readed, start_blk = 0;
Jaegeuk Kim90a893c2014-09-22 16:21:07 -07002067 int nrpages = MAX_BIO_BLOCKS(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002068
Chao Yu74de5932013-11-22 09:09:59 +08002069 do {
Chao Yu662befd2014-02-07 16:11:53 +08002070 readed = ra_meta_pages(sbi, start_blk, nrpages, META_SIT);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002071
Chao Yu74de5932013-11-22 09:09:59 +08002072 start = start_blk * sit_i->sents_per_block;
2073 end = (start_blk + readed) * sit_i->sents_per_block;
2074
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002075 for (; start < end && start < MAIN_SEGS(sbi); start++) {
Chao Yu74de5932013-11-22 09:09:59 +08002076 struct seg_entry *se = &sit_i->sentries[start];
2077 struct f2fs_sit_block *sit_blk;
2078 struct f2fs_sit_entry sit;
2079 struct page *page;
2080
2081 mutex_lock(&curseg->curseg_mutex);
2082 for (i = 0; i < sits_in_cursum(sum); i++) {
Chris Fries6c311ec2014-01-17 14:44:39 -06002083 if (le32_to_cpu(segno_in_journal(sum, i))
2084 == start) {
Chao Yu74de5932013-11-22 09:09:59 +08002085 sit = sit_in_journal(sum, i);
2086 mutex_unlock(&curseg->curseg_mutex);
2087 goto got_it;
2088 }
2089 }
2090 mutex_unlock(&curseg->curseg_mutex);
2091
2092 page = get_current_sit_page(sbi, start);
2093 sit_blk = (struct f2fs_sit_block *)page_address(page);
2094 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
2095 f2fs_put_page(page, 1);
2096got_it:
2097 check_block_count(sbi, start, &sit);
2098 seg_info_from_raw_sit(se, &sit);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07002099
2100 /* build discard map only one time */
2101 memcpy(se->discard_map, se->cur_valid_map, SIT_VBLOCK_MAP_SIZE);
2102 sbi->discard_blks += sbi->blocks_per_seg - se->valid_blocks;
2103
Chao Yu74de5932013-11-22 09:09:59 +08002104 if (sbi->segs_per_sec > 1) {
2105 struct sec_entry *e = get_sec_entry(sbi, start);
2106 e->valid_blocks += se->valid_blocks;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002107 }
2108 }
Chao Yu74de5932013-11-22 09:09:59 +08002109 start_blk += readed;
2110 } while (start_blk < sit_blk_cnt);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002111}
2112
2113static void init_free_segmap(struct f2fs_sb_info *sbi)
2114{
2115 unsigned int start;
2116 int type;
2117
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002118 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002119 struct seg_entry *sentry = get_seg_entry(sbi, start);
2120 if (!sentry->valid_blocks)
2121 __set_free(sbi, start);
2122 }
2123
2124 /* set use the current segments */
2125 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
2126 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
2127 __set_test_and_inuse(sbi, curseg_t->segno);
2128 }
2129}
2130
2131static void init_dirty_segmap(struct f2fs_sb_info *sbi)
2132{
2133 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2134 struct free_segmap_info *free_i = FREE_I(sbi);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002135 unsigned int segno = 0, offset = 0;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002136 unsigned short valid_blocks;
2137
Namjae Jeon8736fbf2013-06-16 09:49:11 +09002138 while (1) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002139 /* find dirty segment based on free segmap */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002140 segno = find_next_inuse(free_i, MAIN_SEGS(sbi), offset);
2141 if (segno >= MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002142 break;
2143 offset = segno + 1;
2144 valid_blocks = get_valid_blocks(sbi, segno, 0);
Jaegeuk Kimec325b52014-09-02 16:24:11 -07002145 if (valid_blocks == sbi->blocks_per_seg || !valid_blocks)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002146 continue;
Jaegeuk Kimec325b52014-09-02 16:24:11 -07002147 if (valid_blocks > sbi->blocks_per_seg) {
2148 f2fs_bug_on(sbi, 1);
2149 continue;
2150 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002151 mutex_lock(&dirty_i->seglist_lock);
2152 __locate_dirty_segment(sbi, segno, DIRTY);
2153 mutex_unlock(&dirty_i->seglist_lock);
2154 }
2155}
2156
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002157static int init_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002158{
2159 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002160 unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002161
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002162 dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL);
2163 if (!dirty_i->victim_secmap)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002164 return -ENOMEM;
2165 return 0;
2166}
2167
2168static int build_dirty_segmap(struct f2fs_sb_info *sbi)
2169{
2170 struct dirty_seglist_info *dirty_i;
2171 unsigned int bitmap_size, i;
2172
2173 /* allocate memory for dirty segments list information */
2174 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
2175 if (!dirty_i)
2176 return -ENOMEM;
2177
2178 SM_I(sbi)->dirty_info = dirty_i;
2179 mutex_init(&dirty_i->seglist_lock);
2180
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002181 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002182
2183 for (i = 0; i < NR_DIRTY_TYPE; i++) {
2184 dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002185 if (!dirty_i->dirty_segmap[i])
2186 return -ENOMEM;
2187 }
2188
2189 init_dirty_segmap(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002190 return init_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002191}
2192
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002193/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002194 * Update min, max modified time for cost-benefit GC algorithm
2195 */
2196static void init_min_max_mtime(struct f2fs_sb_info *sbi)
2197{
2198 struct sit_info *sit_i = SIT_I(sbi);
2199 unsigned int segno;
2200
2201 mutex_lock(&sit_i->sentry_lock);
2202
2203 sit_i->min_mtime = LLONG_MAX;
2204
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002205 for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002206 unsigned int i;
2207 unsigned long long mtime = 0;
2208
2209 for (i = 0; i < sbi->segs_per_sec; i++)
2210 mtime += get_seg_entry(sbi, segno + i)->mtime;
2211
2212 mtime = div_u64(mtime, sbi->segs_per_sec);
2213
2214 if (sit_i->min_mtime > mtime)
2215 sit_i->min_mtime = mtime;
2216 }
2217 sit_i->max_mtime = get_mtime(sbi);
2218 mutex_unlock(&sit_i->sentry_lock);
2219}
2220
2221int build_segment_manager(struct f2fs_sb_info *sbi)
2222{
2223 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
2224 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Namjae Jeon1042d602012-12-01 10:56:13 +09002225 struct f2fs_sm_info *sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002226 int err;
2227
2228 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
2229 if (!sm_info)
2230 return -ENOMEM;
2231
2232 /* init sm info */
2233 sbi->sm_info = sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002234 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
2235 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
2236 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
2237 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
2238 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
2239 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
2240 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
Jaegeuk Kim58c41032014-03-19 14:17:21 +09002241 sm_info->rec_prefree_segments = sm_info->main_segments *
2242 DEF_RECLAIM_PREFREE_SEGMENTS / 100;
Jaegeuk Kim9b5f1362014-09-16 18:30:54 -07002243 sm_info->ipu_policy = 1 << F2FS_IPU_FSYNC;
Jaegeuk Kim216fbd62013-11-07 13:13:42 +09002244 sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -07002245 sm_info->min_fsync_blocks = DEF_MIN_FSYNC_BLOCKS;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002246
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002247 INIT_LIST_HEAD(&sm_info->discard_list);
2248 sm_info->nr_discards = 0;
2249 sm_info->max_discards = 0;
2250
Jaegeuk Kimbba681c2015-01-26 17:41:23 -08002251 sm_info->trim_sections = DEF_BATCHED_TRIM_SECTIONS;
2252
Chao Yu184a5cd2014-09-04 18:13:01 +08002253 INIT_LIST_HEAD(&sm_info->sit_entry_set);
2254
Gu Zhengb270ad62014-04-11 17:49:55 +08002255 if (test_opt(sbi, FLUSH_MERGE) && !f2fs_readonly(sbi->sb)) {
Gu Zheng2163d192014-04-27 14:21:33 +08002256 err = create_flush_cmd_control(sbi);
2257 if (err)
Gu Zhenga688b9d9e2014-04-27 14:21:21 +08002258 return err;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +09002259 }
2260
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002261 err = build_sit_info(sbi);
2262 if (err)
2263 return err;
2264 err = build_free_segmap(sbi);
2265 if (err)
2266 return err;
2267 err = build_curseg(sbi);
2268 if (err)
2269 return err;
2270
2271 /* reinit free segmap based on SIT */
2272 build_sit_entries(sbi);
2273
2274 init_free_segmap(sbi);
2275 err = build_dirty_segmap(sbi);
2276 if (err)
2277 return err;
2278
2279 init_min_max_mtime(sbi);
2280 return 0;
2281}
2282
2283static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
2284 enum dirty_type dirty_type)
2285{
2286 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2287
2288 mutex_lock(&dirty_i->seglist_lock);
2289 kfree(dirty_i->dirty_segmap[dirty_type]);
2290 dirty_i->nr_dirty[dirty_type] = 0;
2291 mutex_unlock(&dirty_i->seglist_lock);
2292}
2293
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002294static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002295{
2296 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002297 kfree(dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002298}
2299
2300static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
2301{
2302 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2303 int i;
2304
2305 if (!dirty_i)
2306 return;
2307
2308 /* discard pre-free/dirty segments list */
2309 for (i = 0; i < NR_DIRTY_TYPE; i++)
2310 discard_dirty_segmap(sbi, i);
2311
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002312 destroy_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002313 SM_I(sbi)->dirty_info = NULL;
2314 kfree(dirty_i);
2315}
2316
2317static void destroy_curseg(struct f2fs_sb_info *sbi)
2318{
2319 struct curseg_info *array = SM_I(sbi)->curseg_array;
2320 int i;
2321
2322 if (!array)
2323 return;
2324 SM_I(sbi)->curseg_array = NULL;
2325 for (i = 0; i < NR_CURSEG_TYPE; i++)
2326 kfree(array[i].sum_blk);
2327 kfree(array);
2328}
2329
2330static void destroy_free_segmap(struct f2fs_sb_info *sbi)
2331{
2332 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
2333 if (!free_i)
2334 return;
2335 SM_I(sbi)->free_info = NULL;
2336 kfree(free_i->free_segmap);
2337 kfree(free_i->free_secmap);
2338 kfree(free_i);
2339}
2340
2341static void destroy_sit_info(struct f2fs_sb_info *sbi)
2342{
2343 struct sit_info *sit_i = SIT_I(sbi);
2344 unsigned int start;
2345
2346 if (!sit_i)
2347 return;
2348
2349 if (sit_i->sentries) {
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002350 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002351 kfree(sit_i->sentries[start].cur_valid_map);
2352 kfree(sit_i->sentries[start].ckpt_valid_map);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07002353 kfree(sit_i->sentries[start].discard_map);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002354 }
2355 }
Jaegeuk Kim60a3b782015-02-10 16:44:29 -08002356 kfree(sit_i->tmp_map);
2357
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002358 vfree(sit_i->sentries);
2359 vfree(sit_i->sec_entries);
2360 kfree(sit_i->dirty_sentries_bitmap);
2361
2362 SM_I(sbi)->sit_info = NULL;
2363 kfree(sit_i->sit_bitmap);
2364 kfree(sit_i);
2365}
2366
2367void destroy_segment_manager(struct f2fs_sb_info *sbi)
2368{
2369 struct f2fs_sm_info *sm_info = SM_I(sbi);
Gu Zhenga688b9d9e2014-04-27 14:21:21 +08002370
Chao Yu3b03f722013-11-06 09:12:04 +08002371 if (!sm_info)
2372 return;
Gu Zheng2163d192014-04-27 14:21:33 +08002373 destroy_flush_cmd_control(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002374 destroy_dirty_segmap(sbi);
2375 destroy_curseg(sbi);
2376 destroy_free_segmap(sbi);
2377 destroy_sit_info(sbi);
2378 sbi->sm_info = NULL;
2379 kfree(sm_info);
2380}
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002381
2382int __init create_segment_manager_caches(void)
2383{
2384 discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
Gu Zhenge8512d22014-03-07 18:43:28 +08002385 sizeof(struct discard_entry));
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002386 if (!discard_entry_slab)
Chao Yu184a5cd2014-09-04 18:13:01 +08002387 goto fail;
2388
2389 sit_entry_set_slab = f2fs_kmem_cache_create("sit_entry_set",
Changman Leec9ee0082014-11-21 15:42:07 +09002390 sizeof(struct sit_entry_set));
Chao Yu184a5cd2014-09-04 18:13:01 +08002391 if (!sit_entry_set_slab)
2392 goto destory_discard_entry;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002393
2394 inmem_entry_slab = f2fs_kmem_cache_create("inmem_page_entry",
2395 sizeof(struct inmem_pages));
2396 if (!inmem_entry_slab)
2397 goto destroy_sit_entry_set;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002398 return 0;
Chao Yu184a5cd2014-09-04 18:13:01 +08002399
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002400destroy_sit_entry_set:
2401 kmem_cache_destroy(sit_entry_set_slab);
Chao Yu184a5cd2014-09-04 18:13:01 +08002402destory_discard_entry:
2403 kmem_cache_destroy(discard_entry_slab);
2404fail:
2405 return -ENOMEM;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002406}
2407
2408void destroy_segment_manager_caches(void)
2409{
Chao Yu184a5cd2014-09-04 18:13:01 +08002410 kmem_cache_destroy(sit_entry_set_slab);
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002411 kmem_cache_destroy(discard_entry_slab);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002412 kmem_cache_destroy(inmem_entry_slab);
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002413}