blob: 5337fd6fdc32f2459aa33e28e191fac57cf89734 [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>
Chao Yu74de5932013-11-22 09:09:59 +080017#include <linux/swap.h>
Jaegeuk Kim60b99b42015-10-05 14:49:57 -070018#include <linux/timer.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 Kim9be32d72014-12-05 10:39:49 -0800200
Jaegeuk Kim9e4ded32014-12-17 19:58:58 -0800201 f2fs_trace_pid(page);
Jaegeuk Kim0722b102014-12-05 11:58:02 -0800202
Chao Yudecd36b2015-08-07 18:42:09 +0800203 set_page_private(page, (unsigned long)ATOMIC_WRITTEN_PAGE);
204 SetPagePrivate(page);
205
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700206 new = f2fs_kmem_cache_alloc(inmem_entry_slab, GFP_NOFS);
207
208 /* add atomic page indices to the list */
209 new->page = page;
210 INIT_LIST_HEAD(&new->list);
Chao Yudecd36b2015-08-07 18:42:09 +0800211
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700212 /* increase reference count with clean state */
213 mutex_lock(&fi->inmem_lock);
214 get_page(page);
215 list_add_tail(&new->list, &fi->inmem_pages);
Jaegeuk Kim8dcf2ff72014-12-05 17:18:15 -0800216 inc_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700217 mutex_unlock(&fi->inmem_lock);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -0700218
219 trace_f2fs_register_inmem_page(page, INMEM);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700220}
221
Jaegeuk Kimedb27de2015-07-25 00:52:52 -0700222int commit_inmem_pages(struct inode *inode, bool abort)
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700223{
224 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
225 struct f2fs_inode_info *fi = F2FS_I(inode);
226 struct inmem_pages *cur, *tmp;
227 bool submit_bio = false;
228 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700229 .sbi = sbi,
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700230 .type = DATA,
Jaegeuk Kim1e843712014-12-09 06:08:59 -0800231 .rw = WRITE_SYNC | REQ_PRIO,
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700232 .encrypted_page = NULL,
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700233 };
Jaegeuk Kimedb27de2015-07-25 00:52:52 -0700234 int err = 0;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700235
Jaegeuk Kim03418452014-11-21 16:37:40 -0800236 /*
237 * The abort is true only when f2fs_evict_inode is called.
238 * Basically, the f2fs_evict_inode doesn't produce any data writes, so
239 * that we don't need to call f2fs_balance_fs.
240 * Otherwise, f2fs_gc in f2fs_balance_fs can wait forever until this
241 * inode becomes free by iget_locked in f2fs_iget.
242 */
Jaegeuk Kim70c640b2014-12-10 13:59:33 -0800243 if (!abort) {
Jaegeuk Kim03418452014-11-21 16:37:40 -0800244 f2fs_balance_fs(sbi);
Jaegeuk Kim70c640b2014-12-10 13:59:33 -0800245 f2fs_lock_op(sbi);
246 }
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700247
248 mutex_lock(&fi->inmem_lock);
249 list_for_each_entry_safe(cur, tmp, &fi->inmem_pages, list) {
Chao Yudecd36b2015-08-07 18:42:09 +0800250 lock_page(cur->page);
Jaegeuk Kim70c640b2014-12-10 13:59:33 -0800251 if (!abort) {
Jaegeuk Kim70c640b2014-12-10 13:59:33 -0800252 if (cur->page->mapping == inode->i_mapping) {
Jaegeuk Kim6282adb2015-07-25 00:29:17 -0700253 set_page_dirty(cur->page);
Jaegeuk Kim70c640b2014-12-10 13:59:33 -0800254 f2fs_wait_on_page_writeback(cur->page, DATA);
255 if (clear_page_dirty_for_io(cur->page))
256 inode_dec_dirty_pages(inode);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -0700257 trace_f2fs_commit_inmem_page(cur->page, INMEM);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700258 fio.page = cur->page;
Jaegeuk Kimedb27de2015-07-25 00:52:52 -0700259 err = do_write_data_page(&fio);
Jaegeuk Kim70c640b2014-12-10 13:59:33 -0800260 submit_bio = true;
Jaegeuk Kimedb27de2015-07-25 00:52:52 -0700261 if (err) {
262 unlock_page(cur->page);
263 break;
264 }
Jaegeuk Kim70c640b2014-12-10 13:59:33 -0800265 }
Jaegeuk Kim70c640b2014-12-10 13:59:33 -0800266 } else {
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -0700267 trace_f2fs_commit_inmem_page(cur->page, INMEM_DROP);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700268 }
Chao Yudecd36b2015-08-07 18:42:09 +0800269 set_page_private(cur->page, 0);
270 ClearPagePrivate(cur->page);
271 f2fs_put_page(cur->page, 1);
272
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700273 list_del(&cur->list);
274 kmem_cache_free(inmem_entry_slab, cur);
Jaegeuk Kim8dcf2ff72014-12-05 17:18:15 -0800275 dec_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700276 }
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700277 mutex_unlock(&fi->inmem_lock);
278
Jaegeuk Kim70c640b2014-12-10 13:59:33 -0800279 if (!abort) {
280 f2fs_unlock_op(sbi);
281 if (submit_bio)
282 f2fs_submit_merged_bio(sbi, DATA, WRITE);
283 }
Jaegeuk Kimedb27de2015-07-25 00:52:52 -0700284 return err;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700285}
286
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900287/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900288 * This function balances dirty node and dentry pages.
289 * In addition, it controls garbage collection.
290 */
291void f2fs_balance_fs(struct f2fs_sb_info *sbi)
292{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900293 /*
Jaegeuk Kim029cd282012-12-21 17:20:21 +0900294 * We should do GC or end up with checkpoint, if there are so many dirty
295 * dir/node pages without enough free segments.
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900296 */
Jaegeuk Kim43727522013-02-04 15:11:17 +0900297 if (has_not_enough_free_secs(sbi, 0)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900298 mutex_lock(&sbi->gc_mutex);
Chao Yud530d4d2015-10-05 22:22:44 +0800299 f2fs_gc(sbi, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900300 }
301}
302
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900303void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi)
304{
Chao Yu1dcc3362015-02-05 17:57:31 +0800305 /* try to shrink extent cache when there is no enough memory */
Jaegeuk Kim554df792015-06-19 13:41:23 -0700306 if (!available_free_memory(sbi, EXTENT_CACHE))
307 f2fs_shrink_extent_tree(sbi, EXTENT_CACHE_SHRINK_NUMBER);
Chao Yu1dcc3362015-02-05 17:57:31 +0800308
Jaegeuk Kim1b38dc82015-06-19 15:36:07 -0700309 /* check the # of cached NAT entries */
310 if (!available_free_memory(sbi, NAT_ENTRIES))
311 try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK);
312
Chao Yu31696582015-07-28 18:33:46 +0800313 if (!available_free_memory(sbi, FREE_NIDS))
314 try_to_free_nids(sbi, NAT_ENTRY_PER_BLOCK * FREE_NID_PAGES);
315
Jaegeuk Kim1b38dc82015-06-19 15:36:07 -0700316 /* checkpoint is the only way to shrink partial cached entries */
317 if (!available_free_memory(sbi, NAT_ENTRIES) ||
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -0800318 excess_prefree_segs(sbi) ||
Jaegeuk Kim60b99b42015-10-05 14:49:57 -0700319 !available_free_memory(sbi, INO_ENTRIES) ||
320 jiffies > sbi->cp_expires)
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900321 f2fs_sync_fs(sbi->sb, true);
322}
323
Gu Zheng2163d192014-04-27 14:21:33 +0800324static int issue_flush_thread(void *data)
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900325{
326 struct f2fs_sb_info *sbi = data;
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800327 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
328 wait_queue_head_t *q = &fcc->flush_wait_queue;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900329repeat:
330 if (kthread_should_stop())
331 return 0;
332
Gu Zheng721bd4d2014-09-05 18:31:00 +0800333 if (!llist_empty(&fcc->issue_list)) {
Jaegeuk Kim740432f2015-08-14 11:43:56 -0700334 struct bio *bio;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900335 struct flush_cmd *cmd, *next;
336 int ret;
337
Jaegeuk Kim740432f2015-08-14 11:43:56 -0700338 bio = f2fs_bio_alloc(0);
339
Gu Zheng721bd4d2014-09-05 18:31:00 +0800340 fcc->dispatch_list = llist_del_all(&fcc->issue_list);
341 fcc->dispatch_list = llist_reverse_order(fcc->dispatch_list);
342
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900343 bio->bi_bdev = sbi->sb->s_bdev;
344 ret = submit_bio_wait(WRITE_FLUSH, bio);
345
Gu Zheng721bd4d2014-09-05 18:31:00 +0800346 llist_for_each_entry_safe(cmd, next,
347 fcc->dispatch_list, llnode) {
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900348 cmd->ret = ret;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900349 complete(&cmd->wait);
350 }
Gu Zhenga4ed23f2014-04-11 17:49:35 +0800351 bio_put(bio);
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800352 fcc->dispatch_list = NULL;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900353 }
354
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800355 wait_event_interruptible(*q,
Gu Zheng721bd4d2014-09-05 18:31:00 +0800356 kthread_should_stop() || !llist_empty(&fcc->issue_list));
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900357 goto repeat;
358}
359
360int f2fs_issue_flush(struct f2fs_sb_info *sbi)
361{
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800362 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
Chao Yuadf8d902014-05-08 17:00:35 +0800363 struct flush_cmd cmd;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900364
Jaegeuk Kim24a9ee02014-07-25 17:46:10 -0700365 trace_f2fs_issue_flush(sbi->sb, test_opt(sbi, NOBARRIER),
366 test_opt(sbi, FLUSH_MERGE));
367
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700368 if (test_opt(sbi, NOBARRIER))
369 return 0;
370
Jaegeuk Kim740432f2015-08-14 11:43:56 -0700371 if (!test_opt(sbi, FLUSH_MERGE)) {
372 struct bio *bio = f2fs_bio_alloc(0);
373 int ret;
374
375 bio->bi_bdev = sbi->sb->s_bdev;
376 ret = submit_bio_wait(WRITE_FLUSH, bio);
377 bio_put(bio);
378 return ret;
379 }
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900380
Chao Yuadf8d902014-05-08 17:00:35 +0800381 init_completion(&cmd.wait);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900382
Gu Zheng721bd4d2014-09-05 18:31:00 +0800383 llist_add(&cmd.llnode, &fcc->issue_list);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900384
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800385 if (!fcc->dispatch_list)
386 wake_up(&fcc->flush_wait_queue);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900387
Chao Yuadf8d902014-05-08 17:00:35 +0800388 wait_for_completion(&cmd.wait);
389
390 return cmd.ret;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900391}
392
Gu Zheng2163d192014-04-27 14:21:33 +0800393int create_flush_cmd_control(struct f2fs_sb_info *sbi)
394{
395 dev_t dev = sbi->sb->s_bdev->bd_dev;
396 struct flush_cmd_control *fcc;
397 int err = 0;
398
399 fcc = kzalloc(sizeof(struct flush_cmd_control), GFP_KERNEL);
400 if (!fcc)
401 return -ENOMEM;
Gu Zheng2163d192014-04-27 14:21:33 +0800402 init_waitqueue_head(&fcc->flush_wait_queue);
Gu Zheng721bd4d2014-09-05 18:31:00 +0800403 init_llist_head(&fcc->issue_list);
Chao Yu6b2920a2014-07-07 11:21:59 +0800404 SM_I(sbi)->cmd_control_info = fcc;
Gu Zheng2163d192014-04-27 14:21:33 +0800405 fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi,
406 "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev));
407 if (IS_ERR(fcc->f2fs_issue_flush)) {
408 err = PTR_ERR(fcc->f2fs_issue_flush);
409 kfree(fcc);
Chao Yu6b2920a2014-07-07 11:21:59 +0800410 SM_I(sbi)->cmd_control_info = NULL;
Gu Zheng2163d192014-04-27 14:21:33 +0800411 return err;
412 }
Gu Zheng2163d192014-04-27 14:21:33 +0800413
414 return err;
415}
416
417void destroy_flush_cmd_control(struct f2fs_sb_info *sbi)
418{
Chao Yu6b2920a2014-07-07 11:21:59 +0800419 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
Gu Zheng2163d192014-04-27 14:21:33 +0800420
421 if (fcc && fcc->f2fs_issue_flush)
422 kthread_stop(fcc->f2fs_issue_flush);
423 kfree(fcc);
Chao Yu6b2920a2014-07-07 11:21:59 +0800424 SM_I(sbi)->cmd_control_info = NULL;
Gu Zheng2163d192014-04-27 14:21:33 +0800425}
426
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900427static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
428 enum dirty_type dirty_type)
429{
430 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
431
432 /* need not be added */
433 if (IS_CURSEG(sbi, segno))
434 return;
435
436 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
437 dirty_i->nr_dirty[dirty_type]++;
438
439 if (dirty_type == DIRTY) {
440 struct seg_entry *sentry = get_seg_entry(sbi, segno);
Changman Lee4625d6a2013-10-25 17:31:57 +0900441 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900442
Jaegeuk Kimec325b52014-09-02 16:24:11 -0700443 if (unlikely(t >= DIRTY)) {
444 f2fs_bug_on(sbi, 1);
445 return;
446 }
Changman Lee4625d6a2013-10-25 17:31:57 +0900447 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
448 dirty_i->nr_dirty[t]++;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900449 }
450}
451
452static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
453 enum dirty_type dirty_type)
454{
455 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
456
457 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
458 dirty_i->nr_dirty[dirty_type]--;
459
460 if (dirty_type == DIRTY) {
Changman Lee4625d6a2013-10-25 17:31:57 +0900461 struct seg_entry *sentry = get_seg_entry(sbi, segno);
462 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900463
Changman Lee4625d6a2013-10-25 17:31:57 +0900464 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
465 dirty_i->nr_dirty[t]--;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900466
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +0900467 if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0)
468 clear_bit(GET_SECNO(sbi, segno),
469 dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900470 }
471}
472
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900473/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900474 * Should not occur error such as -ENOMEM.
475 * Adding dirty entry into seglist is not critical operation.
476 * If a given segment is one of current working segments, it won't be added.
477 */
Haicheng Li8d8451a2013-06-13 16:59:28 +0800478static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900479{
480 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
481 unsigned short valid_blocks;
482
483 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
484 return;
485
486 mutex_lock(&dirty_i->seglist_lock);
487
488 valid_blocks = get_valid_blocks(sbi, segno, 0);
489
490 if (valid_blocks == 0) {
491 __locate_dirty_segment(sbi, segno, PRE);
492 __remove_dirty_segment(sbi, segno, DIRTY);
493 } else if (valid_blocks < sbi->blocks_per_seg) {
494 __locate_dirty_segment(sbi, segno, DIRTY);
495 } else {
496 /* Recovery routine with SSR needs this */
497 __remove_dirty_segment(sbi, segno, DIRTY);
498 }
499
500 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900501}
502
Jaegeuk Kim1e87a782014-04-15 13:57:55 +0900503static int f2fs_issue_discard(struct f2fs_sb_info *sbi,
Jaegeuk Kim37208872013-11-12 16:55:17 +0900504 block_t blkstart, block_t blklen)
505{
Chao Yu55cf9cb2014-09-15 18:01:10 +0800506 sector_t start = SECTOR_FROM_BLOCK(blkstart);
507 sector_t len = SECTOR_FROM_BLOCK(blklen);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700508 struct seg_entry *se;
509 unsigned int offset;
510 block_t i;
511
512 for (i = blkstart; i < blkstart + blklen; i++) {
513 se = get_seg_entry(sbi, GET_SEGNO(sbi, i));
514 offset = GET_BLKOFF_FROM_SEG0(sbi, i);
515
516 if (!f2fs_test_and_set_bit(offset, se->discard_map))
517 sbi->discard_blks--;
518 }
Jaegeuk Kim1661d072013-11-12 17:01:00 +0900519 trace_f2fs_issue_discard(sbi->sb, blkstart, blklen);
Jaegeuk Kim1e87a782014-04-15 13:57:55 +0900520 return blkdev_issue_discard(sbi->sb->s_bdev, start, len, GFP_NOFS, 0);
521}
522
Chao Yue90c2d22015-07-28 18:36:47 +0800523bool discard_next_dnode(struct f2fs_sb_info *sbi, block_t blkaddr)
Jaegeuk Kim1e87a782014-04-15 13:57:55 +0900524{
Jaegeuk Kim40a02be2015-05-11 20:03:49 -0700525 int err = -ENOTSUPP;
526
527 if (test_opt(sbi, DISCARD)) {
528 struct seg_entry *se = get_seg_entry(sbi,
529 GET_SEGNO(sbi, blkaddr));
530 unsigned int offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
531
532 if (f2fs_test_bit(offset, se->discard_map))
Chao Yue90c2d22015-07-28 18:36:47 +0800533 return false;
Jaegeuk Kim40a02be2015-05-11 20:03:49 -0700534
535 err = f2fs_issue_discard(sbi, blkaddr, 1);
536 }
537
Chao Yue90c2d22015-07-28 18:36:47 +0800538 if (err) {
Chao Yu381722d2015-05-19 17:40:04 +0800539 update_meta_page(sbi, NULL, blkaddr);
Chao Yue90c2d22015-07-28 18:36:47 +0800540 return true;
541 }
542 return false;
Jaegeuk Kim37208872013-11-12 16:55:17 +0900543}
544
Jaegeuk Kimadf49832014-10-28 22:27:59 -0700545static void __add_discard_entry(struct f2fs_sb_info *sbi,
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700546 struct cp_control *cpc, struct seg_entry *se,
547 unsigned int start, unsigned int end)
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900548{
549 struct list_head *head = &SM_I(sbi)->discard_list;
Jaegeuk Kimadf49832014-10-28 22:27:59 -0700550 struct discard_entry *new, *last;
551
552 if (!list_empty(head)) {
553 last = list_last_entry(head, struct discard_entry, list);
554 if (START_BLOCK(sbi, cpc->trim_start) + start ==
555 last->blkaddr + last->len) {
556 last->len += end - start;
557 goto done;
558 }
559 }
560
561 new = f2fs_kmem_cache_alloc(discard_entry_slab, GFP_NOFS);
562 INIT_LIST_HEAD(&new->list);
563 new->blkaddr = START_BLOCK(sbi, cpc->trim_start) + start;
564 new->len = end - start;
565 list_add_tail(&new->list, head);
566done:
567 SM_I(sbi)->nr_discards += end - start;
Jaegeuk Kimadf49832014-10-28 22:27:59 -0700568}
569
570static void add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc)
571{
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900572 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
573 int max_blocks = sbi->blocks_per_seg;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700574 struct seg_entry *se = get_seg_entry(sbi, cpc->trim_start);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900575 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
576 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700577 unsigned long *discard_map = (unsigned long *)se->discard_map;
Jaegeuk Kim60a3b782015-02-10 16:44:29 -0800578 unsigned long *dmap = SIT_I(sbi)->tmp_map;
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900579 unsigned int start = 0, end = -1;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700580 bool force = (cpc->reason == CP_DISCARD);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900581 int i;
582
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700583 if (se->valid_blocks == max_blocks)
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900584 return;
585
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700586 if (!force) {
587 if (!test_opt(sbi, DISCARD) || !se->valid_blocks ||
Dan Carpenter912a83b2015-05-14 11:52:28 +0300588 SM_I(sbi)->nr_discards >= SM_I(sbi)->max_discards)
589 return;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700590 }
591
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900592 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
593 for (i = 0; i < entries; i++)
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700594 dmap[i] = force ? ~ckpt_map[i] & ~discard_map[i] :
Jaegeuk Kimd7bc2482014-12-12 13:53:41 -0800595 (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900596
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700597 while (force || SM_I(sbi)->nr_discards <= SM_I(sbi)->max_discards) {
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900598 start = __find_rev_next_bit(dmap, max_blocks, end + 1);
599 if (start >= max_blocks)
600 break;
601
602 end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700603 __add_discard_entry(sbi, cpc, se, start, end);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900604 }
605}
606
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700607void release_discard_addrs(struct f2fs_sb_info *sbi)
608{
609 struct list_head *head = &(SM_I(sbi)->discard_list);
610 struct discard_entry *entry, *this;
611
612 /* drop caches */
613 list_for_each_entry_safe(entry, this, head, list) {
614 list_del(&entry->list);
615 kmem_cache_free(discard_entry_slab, entry);
616 }
617}
618
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900619/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900620 * Should call clear_prefree_segments after checkpoint is done.
621 */
622static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
623{
624 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Chao Yub65ee142014-08-04 10:10:07 +0800625 unsigned int segno;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900626
627 mutex_lock(&dirty_i->seglist_lock);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700628 for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900629 __set_test_and_free(sbi, segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900630 mutex_unlock(&dirty_i->seglist_lock);
631}
632
Jaegeuk Kim836b5a62015-04-30 22:50:06 -0700633void clear_prefree_segments(struct f2fs_sb_info *sbi, struct cp_control *cpc)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900634{
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900635 struct list_head *head = &(SM_I(sbi)->discard_list);
Chao Yu2d7b8222014-03-29 11:33:17 +0800636 struct discard_entry *entry, *this;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900637 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Changman Lee29e59c12013-11-11 09:24:37 +0900638 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
Changman Lee29e59c12013-11-11 09:24:37 +0900639 unsigned int start = 0, end = -1;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900640
641 mutex_lock(&dirty_i->seglist_lock);
Changman Lee29e59c12013-11-11 09:24:37 +0900642
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900643 while (1) {
Changman Lee29e59c12013-11-11 09:24:37 +0900644 int i;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700645 start = find_next_bit(prefree_map, MAIN_SEGS(sbi), end + 1);
646 if (start >= MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900647 break;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700648 end = find_next_zero_bit(prefree_map, MAIN_SEGS(sbi),
649 start + 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900650
Changman Lee29e59c12013-11-11 09:24:37 +0900651 for (i = start; i < end; i++)
652 clear_bit(i, prefree_map);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900653
Changman Lee29e59c12013-11-11 09:24:37 +0900654 dirty_i->nr_dirty[PRE] -= end - start;
655
656 if (!test_opt(sbi, DISCARD))
657 continue;
658
Jaegeuk Kim37208872013-11-12 16:55:17 +0900659 f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
660 (end - start) << sbi->log_blocks_per_seg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900661 }
662 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900663
664 /* send small discards */
Chao Yu2d7b8222014-03-29 11:33:17 +0800665 list_for_each_entry_safe(entry, this, head, list) {
Jaegeuk Kim836b5a62015-04-30 22:50:06 -0700666 if (cpc->reason == CP_DISCARD && entry->len < cpc->trim_minlen)
667 goto skip;
Jaegeuk Kim37208872013-11-12 16:55:17 +0900668 f2fs_issue_discard(sbi, entry->blkaddr, entry->len);
Jaegeuk Kimf56aa1c2015-06-02 15:48:20 -0700669 cpc->trimmed += entry->len;
Jaegeuk Kim836b5a62015-04-30 22:50:06 -0700670skip:
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900671 list_del(&entry->list);
672 SM_I(sbi)->nr_discards -= entry->len;
673 kmem_cache_free(discard_entry_slab, entry);
674 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900675}
676
Chao Yu184a5cd2014-09-04 18:13:01 +0800677static bool __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900678{
679 struct sit_info *sit_i = SIT_I(sbi);
Chao Yu184a5cd2014-09-04 18:13:01 +0800680
681 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900682 sit_i->dirty_sentries++;
Chao Yu184a5cd2014-09-04 18:13:01 +0800683 return false;
684 }
685
686 return true;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900687}
688
689static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
690 unsigned int segno, int modified)
691{
692 struct seg_entry *se = get_seg_entry(sbi, segno);
693 se->type = type;
694 if (modified)
695 __mark_sit_entry_dirty(sbi, segno);
696}
697
698static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
699{
700 struct seg_entry *se;
701 unsigned int segno, offset;
702 long int new_vblocks;
703
704 segno = GET_SEGNO(sbi, blkaddr);
705
706 se = get_seg_entry(sbi, segno);
707 new_vblocks = se->valid_blocks + del;
Jaegeuk Kim491c0852014-02-04 13:01:10 +0900708 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900709
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700710 f2fs_bug_on(sbi, (new_vblocks >> (sizeof(unsigned short) << 3) ||
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900711 (new_vblocks > sbi->blocks_per_seg)));
712
713 se->valid_blocks = new_vblocks;
714 se->mtime = get_mtime(sbi);
715 SIT_I(sbi)->max_mtime = se->mtime;
716
717 /* Update valid block bitmap */
718 if (del > 0) {
Gu Zheng52aca072014-10-20 17:45:51 +0800719 if (f2fs_test_and_set_bit(offset, se->cur_valid_map))
Jaegeuk Kim05796762014-09-02 16:05:00 -0700720 f2fs_bug_on(sbi, 1);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700721 if (!f2fs_test_and_set_bit(offset, se->discard_map))
722 sbi->discard_blks--;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900723 } else {
Gu Zheng52aca072014-10-20 17:45:51 +0800724 if (!f2fs_test_and_clear_bit(offset, se->cur_valid_map))
Jaegeuk Kim05796762014-09-02 16:05:00 -0700725 f2fs_bug_on(sbi, 1);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700726 if (f2fs_test_and_clear_bit(offset, se->discard_map))
727 sbi->discard_blks++;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900728 }
729 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
730 se->ckpt_valid_blocks += del;
731
732 __mark_sit_entry_dirty(sbi, segno);
733
734 /* update total number of valid blocks to be written in ckpt area */
735 SIT_I(sbi)->written_valid_blocks += del;
736
737 if (sbi->segs_per_sec > 1)
738 get_sec_entry(sbi, segno)->valid_blocks += del;
739}
740
Jaegeuk Kim5e443812014-01-28 12:22:14 +0900741void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900742{
Jaegeuk Kim5e443812014-01-28 12:22:14 +0900743 update_sit_entry(sbi, new, 1);
744 if (GET_SEGNO(sbi, old) != NULL_SEGNO)
745 update_sit_entry(sbi, old, -1);
746
747 locate_dirty_segment(sbi, GET_SEGNO(sbi, old));
748 locate_dirty_segment(sbi, GET_SEGNO(sbi, new));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900749}
750
751void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
752{
753 unsigned int segno = GET_SEGNO(sbi, addr);
754 struct sit_info *sit_i = SIT_I(sbi);
755
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700756 f2fs_bug_on(sbi, addr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900757 if (addr == NEW_ADDR)
758 return;
759
760 /* add it into sit main buffer */
761 mutex_lock(&sit_i->sentry_lock);
762
763 update_sit_entry(sbi, addr, -1);
764
765 /* add it into dirty seglist */
766 locate_dirty_segment(sbi, segno);
767
768 mutex_unlock(&sit_i->sentry_lock);
769}
770
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700771bool is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr)
772{
773 struct sit_info *sit_i = SIT_I(sbi);
774 unsigned int segno, offset;
775 struct seg_entry *se;
776 bool is_cp = false;
777
778 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR)
779 return true;
780
781 mutex_lock(&sit_i->sentry_lock);
782
783 segno = GET_SEGNO(sbi, blkaddr);
784 se = get_seg_entry(sbi, segno);
785 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
786
787 if (f2fs_test_bit(offset, se->ckpt_valid_map))
788 is_cp = true;
789
790 mutex_unlock(&sit_i->sentry_lock);
791
792 return is_cp;
793}
794
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900795/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900796 * This function should be resided under the curseg_mutex lock
797 */
798static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
Haicheng Lie79efe32013-06-13 16:59:27 +0800799 struct f2fs_summary *sum)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900800{
801 struct curseg_info *curseg = CURSEG_I(sbi, type);
802 void *addr = curseg->sum_blk;
Haicheng Lie79efe32013-06-13 16:59:27 +0800803 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900804 memcpy(addr, sum, sizeof(struct f2fs_summary));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900805}
806
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900807/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900808 * Calculate the number of current summary pages for writing
809 */
Chao Yu3fa06d72014-12-09 14:21:46 +0800810int npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900811{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900812 int valid_sum_count = 0;
Fan Li9a479382013-10-29 16:21:47 +0800813 int i, sum_in_page;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900814
815 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
816 if (sbi->ckpt->alloc_type[i] == SSR)
817 valid_sum_count += sbi->blocks_per_seg;
Chao Yu3fa06d72014-12-09 14:21:46 +0800818 else {
819 if (for_ra)
820 valid_sum_count += le16_to_cpu(
821 F2FS_CKPT(sbi)->cur_data_blkoff[i]);
822 else
823 valid_sum_count += curseg_blkoff(sbi, i);
824 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900825 }
826
Fan Li9a479382013-10-29 16:21:47 +0800827 sum_in_page = (PAGE_CACHE_SIZE - 2 * SUM_JOURNAL_SIZE -
828 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
829 if (valid_sum_count <= sum_in_page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900830 return 1;
Fan Li9a479382013-10-29 16:21:47 +0800831 else if ((valid_sum_count - sum_in_page) <=
832 (PAGE_CACHE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900833 return 2;
834 return 3;
835}
836
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900837/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900838 * Caller should put this summary page
839 */
840struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
841{
842 return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
843}
844
Chao Yu381722d2015-05-19 17:40:04 +0800845void update_meta_page(struct f2fs_sb_info *sbi, void *src, block_t blk_addr)
846{
847 struct page *page = grab_meta_page(sbi, blk_addr);
848 void *dst = page_address(page);
849
850 if (src)
851 memcpy(dst, src, PAGE_CACHE_SIZE);
852 else
853 memset(dst, 0, PAGE_CACHE_SIZE);
854 set_page_dirty(page);
855 f2fs_put_page(page, 1);
856}
857
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900858static void write_sum_page(struct f2fs_sb_info *sbi,
859 struct f2fs_summary_block *sum_blk, block_t blk_addr)
860{
Chao Yu381722d2015-05-19 17:40:04 +0800861 update_meta_page(sbi, (void *)sum_blk, blk_addr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900862}
863
Jaegeuk Kim60374682013-03-31 13:58:51 +0900864static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
865{
866 struct curseg_info *curseg = CURSEG_I(sbi, type);
Haicheng Li81fb5e82013-05-14 18:20:28 +0800867 unsigned int segno = curseg->segno + 1;
Jaegeuk Kim60374682013-03-31 13:58:51 +0900868 struct free_segmap_info *free_i = FREE_I(sbi);
869
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700870 if (segno < MAIN_SEGS(sbi) && segno % sbi->segs_per_sec)
Haicheng Li81fb5e82013-05-14 18:20:28 +0800871 return !test_bit(segno, free_i->free_segmap);
Jaegeuk Kim60374682013-03-31 13:58:51 +0900872 return 0;
873}
874
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900875/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900876 * Find a new segment from the free segments bitmap to right order
877 * This function should be returned with success, otherwise BUG
878 */
879static void get_new_segment(struct f2fs_sb_info *sbi,
880 unsigned int *newseg, bool new_sec, int dir)
881{
882 struct free_segmap_info *free_i = FREE_I(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900883 unsigned int segno, secno, zoneno;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700884 unsigned int total_zones = MAIN_SECS(sbi) / sbi->secs_per_zone;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900885 unsigned int hint = *newseg / sbi->segs_per_sec;
886 unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
887 unsigned int left_start = hint;
888 bool init = true;
889 int go_left = 0;
890 int i;
891
Chao Yu1a118cc2015-02-11 18:20:38 +0800892 spin_lock(&free_i->segmap_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900893
894 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
895 segno = find_next_zero_bit(free_i->free_segmap,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700896 MAIN_SEGS(sbi), *newseg + 1);
Jaegeuk Kim33afa7f2013-03-31 12:59:53 +0900897 if (segno - *newseg < sbi->segs_per_sec -
898 (*newseg % sbi->segs_per_sec))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900899 goto got_it;
900 }
901find_other_zone:
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700902 secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint);
903 if (secno >= MAIN_SECS(sbi)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900904 if (dir == ALLOC_RIGHT) {
905 secno = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700906 MAIN_SECS(sbi), 0);
907 f2fs_bug_on(sbi, secno >= MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900908 } else {
909 go_left = 1;
910 left_start = hint - 1;
911 }
912 }
913 if (go_left == 0)
914 goto skip_left;
915
916 while (test_bit(left_start, free_i->free_secmap)) {
917 if (left_start > 0) {
918 left_start--;
919 continue;
920 }
921 left_start = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700922 MAIN_SECS(sbi), 0);
923 f2fs_bug_on(sbi, left_start >= MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900924 break;
925 }
926 secno = left_start;
927skip_left:
928 hint = secno;
929 segno = secno * sbi->segs_per_sec;
930 zoneno = secno / sbi->secs_per_zone;
931
932 /* give up on finding another zone */
933 if (!init)
934 goto got_it;
935 if (sbi->secs_per_zone == 1)
936 goto got_it;
937 if (zoneno == old_zoneno)
938 goto got_it;
939 if (dir == ALLOC_LEFT) {
940 if (!go_left && zoneno + 1 >= total_zones)
941 goto got_it;
942 if (go_left && zoneno == 0)
943 goto got_it;
944 }
945 for (i = 0; i < NR_CURSEG_TYPE; i++)
946 if (CURSEG_I(sbi, i)->zone == zoneno)
947 break;
948
949 if (i < NR_CURSEG_TYPE) {
950 /* zone is in user, try another */
951 if (go_left)
952 hint = zoneno * sbi->secs_per_zone - 1;
953 else if (zoneno + 1 >= total_zones)
954 hint = 0;
955 else
956 hint = (zoneno + 1) * sbi->secs_per_zone;
957 init = false;
958 goto find_other_zone;
959 }
960got_it:
961 /* set it as dirty segment in free segmap */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700962 f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900963 __set_inuse(sbi, segno);
964 *newseg = segno;
Chao Yu1a118cc2015-02-11 18:20:38 +0800965 spin_unlock(&free_i->segmap_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900966}
967
968static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
969{
970 struct curseg_info *curseg = CURSEG_I(sbi, type);
971 struct summary_footer *sum_footer;
972
973 curseg->segno = curseg->next_segno;
974 curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
975 curseg->next_blkoff = 0;
976 curseg->next_segno = NULL_SEGNO;
977
978 sum_footer = &(curseg->sum_blk->footer);
979 memset(sum_footer, 0, sizeof(struct summary_footer));
980 if (IS_DATASEG(type))
981 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
982 if (IS_NODESEG(type))
983 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
984 __set_sit_entry_type(sbi, type, curseg->segno, modified);
985}
986
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900987/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900988 * Allocate a current working segment.
989 * This function always allocates a free segment in LFS manner.
990 */
991static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
992{
993 struct curseg_info *curseg = CURSEG_I(sbi, type);
994 unsigned int segno = curseg->segno;
995 int dir = ALLOC_LEFT;
996
997 write_sum_page(sbi, curseg->sum_blk,
Haicheng Li81fb5e82013-05-14 18:20:28 +0800998 GET_SUM_BLOCK(sbi, segno));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900999 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
1000 dir = ALLOC_RIGHT;
1001
1002 if (test_opt(sbi, NOHEAP))
1003 dir = ALLOC_RIGHT;
1004
1005 get_new_segment(sbi, &segno, new_sec, dir);
1006 curseg->next_segno = segno;
1007 reset_curseg(sbi, type, 1);
1008 curseg->alloc_type = LFS;
1009}
1010
1011static void __next_free_blkoff(struct f2fs_sb_info *sbi,
1012 struct curseg_info *seg, block_t start)
1013{
1014 struct seg_entry *se = get_seg_entry(sbi, seg->segno);
Changman Leee81c93c2013-11-15 13:21:16 +09001015 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
Jaegeuk Kim60a3b782015-02-10 16:44:29 -08001016 unsigned long *target_map = SIT_I(sbi)->tmp_map;
Changman Leee81c93c2013-11-15 13:21:16 +09001017 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
1018 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
1019 int i, pos;
1020
1021 for (i = 0; i < entries; i++)
1022 target_map[i] = ckpt_map[i] | cur_map[i];
1023
1024 pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
1025
1026 seg->next_blkoff = pos;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001027}
1028
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001029/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001030 * If a segment is written by LFS manner, next block offset is just obtained
1031 * by increasing the current block offset. However, if a segment is written by
1032 * SSR manner, next block offset obtained by calling __next_free_blkoff
1033 */
1034static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
1035 struct curseg_info *seg)
1036{
1037 if (seg->alloc_type == SSR)
1038 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
1039 else
1040 seg->next_blkoff++;
1041}
1042
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001043/*
arter97e1c42042014-08-06 23:22:50 +09001044 * This function always allocates a used segment(from dirty seglist) by SSR
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001045 * manner, so it should recover the existing segment information of valid blocks
1046 */
1047static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
1048{
1049 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1050 struct curseg_info *curseg = CURSEG_I(sbi, type);
1051 unsigned int new_segno = curseg->next_segno;
1052 struct f2fs_summary_block *sum_node;
1053 struct page *sum_page;
1054
1055 write_sum_page(sbi, curseg->sum_blk,
1056 GET_SUM_BLOCK(sbi, curseg->segno));
1057 __set_test_and_inuse(sbi, new_segno);
1058
1059 mutex_lock(&dirty_i->seglist_lock);
1060 __remove_dirty_segment(sbi, new_segno, PRE);
1061 __remove_dirty_segment(sbi, new_segno, DIRTY);
1062 mutex_unlock(&dirty_i->seglist_lock);
1063
1064 reset_curseg(sbi, type, 1);
1065 curseg->alloc_type = SSR;
1066 __next_free_blkoff(sbi, curseg, 0);
1067
1068 if (reuse) {
1069 sum_page = get_sum_page(sbi, new_segno);
1070 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
1071 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
1072 f2fs_put_page(sum_page, 1);
1073 }
1074}
1075
Jaegeuk Kim43727522013-02-04 15:11:17 +09001076static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
1077{
1078 struct curseg_info *curseg = CURSEG_I(sbi, type);
1079 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
1080
1081 if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0))
1082 return v_ops->get_victim(sbi,
1083 &(curseg)->next_segno, BG_GC, type, SSR);
1084
1085 /* For data segments, let's do SSR more intensively */
1086 for (; type >= CURSEG_HOT_DATA; type--)
1087 if (v_ops->get_victim(sbi, &(curseg)->next_segno,
1088 BG_GC, type, SSR))
1089 return 1;
1090 return 0;
1091}
1092
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001093/*
1094 * flush out current segment and replace it with new segment
1095 * This function should be returned with success, otherwise BUG
1096 */
1097static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
1098 int type, bool force)
1099{
1100 struct curseg_info *curseg = CURSEG_I(sbi, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001101
Gu Zheng7b405272013-08-19 09:41:15 +08001102 if (force)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001103 new_curseg(sbi, type, true);
Gu Zheng7b405272013-08-19 09:41:15 +08001104 else if (type == CURSEG_WARM_NODE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001105 new_curseg(sbi, type, false);
Jaegeuk Kim60374682013-03-31 13:58:51 +09001106 else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
1107 new_curseg(sbi, type, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001108 else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
1109 change_curseg(sbi, type, true);
1110 else
1111 new_curseg(sbi, type, false);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +09001112
1113 stat_inc_seg_type(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001114}
1115
Jaegeuk Kim38aa0882015-01-05 16:02:20 -08001116static void __allocate_new_segments(struct f2fs_sb_info *sbi, int type)
1117{
1118 struct curseg_info *curseg = CURSEG_I(sbi, type);
1119 unsigned int old_segno;
1120
1121 old_segno = curseg->segno;
1122 SIT_I(sbi)->s_ops->allocate_segment(sbi, type, true);
1123 locate_dirty_segment(sbi, old_segno);
1124}
1125
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001126void allocate_new_segments(struct f2fs_sb_info *sbi)
1127{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001128 int i;
1129
Jaegeuk Kim38aa0882015-01-05 16:02:20 -08001130 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++)
1131 __allocate_new_segments(sbi, i);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001132}
1133
1134static const struct segment_allocation default_salloc_ops = {
1135 .allocate_segment = allocate_segment_by_default,
1136};
1137
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001138int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range)
1139{
Jaegeuk Kimf7ef9b82015-02-09 12:02:44 -08001140 __u64 start = F2FS_BYTES_TO_BLK(range->start);
1141 __u64 end = start + F2FS_BYTES_TO_BLK(range->len) - 1;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001142 unsigned int start_segno, end_segno;
1143 struct cp_control cpc;
1144
Jaegeuk Kim836b5a62015-04-30 22:50:06 -07001145 if (start >= MAX_BLKADDR(sbi) || range->len < sbi->blocksize)
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001146 return -EINVAL;
1147
Jan Kara9bd27ae2014-10-21 14:07:33 +02001148 cpc.trimmed = 0;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001149 if (end <= MAIN_BLKADDR(sbi))
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001150 goto out;
1151
1152 /* start/end segment number in main_area */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001153 start_segno = (start <= MAIN_BLKADDR(sbi)) ? 0 : GET_SEGNO(sbi, start);
1154 end_segno = (end >= MAX_BLKADDR(sbi)) ? MAIN_SEGS(sbi) - 1 :
1155 GET_SEGNO(sbi, end);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001156 cpc.reason = CP_DISCARD;
Jaegeuk Kim836b5a62015-04-30 22:50:06 -07001157 cpc.trim_minlen = max_t(__u64, 1, F2FS_BYTES_TO_BLK(range->minlen));
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001158
1159 /* do checkpoint to issue discard commands safely */
Jaegeuk Kimbba681c2015-01-26 17:41:23 -08001160 for (; start_segno <= end_segno; start_segno = cpc.trim_end + 1) {
1161 cpc.trim_start = start_segno;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001162
1163 if (sbi->discard_blks == 0)
1164 break;
1165 else if (sbi->discard_blks < BATCHED_TRIM_BLOCKS(sbi))
1166 cpc.trim_end = end_segno;
1167 else
1168 cpc.trim_end = min_t(unsigned int,
1169 rounddown(start_segno +
Jaegeuk Kimbba681c2015-01-26 17:41:23 -08001170 BATCHED_TRIM_SEGMENTS(sbi),
1171 sbi->segs_per_sec) - 1, end_segno);
1172
1173 mutex_lock(&sbi->gc_mutex);
1174 write_checkpoint(sbi, &cpc);
1175 mutex_unlock(&sbi->gc_mutex);
1176 }
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001177out:
Jaegeuk Kimf7ef9b82015-02-09 12:02:44 -08001178 range->len = F2FS_BLK_TO_BYTES(cpc.trimmed);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001179 return 0;
1180}
1181
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001182static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
1183{
1184 struct curseg_info *curseg = CURSEG_I(sbi, type);
1185 if (curseg->next_blkoff < sbi->blocks_per_seg)
1186 return true;
1187 return false;
1188}
1189
1190static int __get_segment_type_2(struct page *page, enum page_type p_type)
1191{
1192 if (p_type == DATA)
1193 return CURSEG_HOT_DATA;
1194 else
1195 return CURSEG_HOT_NODE;
1196}
1197
1198static int __get_segment_type_4(struct page *page, enum page_type p_type)
1199{
1200 if (p_type == DATA) {
1201 struct inode *inode = page->mapping->host;
1202
1203 if (S_ISDIR(inode->i_mode))
1204 return CURSEG_HOT_DATA;
1205 else
1206 return CURSEG_COLD_DATA;
1207 } else {
Jaegeuk Kima344b9f2014-11-05 20:05:53 -08001208 if (IS_DNODE(page) && is_cold_node(page))
1209 return CURSEG_WARM_NODE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001210 else
1211 return CURSEG_COLD_NODE;
1212 }
1213}
1214
1215static int __get_segment_type_6(struct page *page, enum page_type p_type)
1216{
1217 if (p_type == DATA) {
1218 struct inode *inode = page->mapping->host;
1219
1220 if (S_ISDIR(inode->i_mode))
1221 return CURSEG_HOT_DATA;
Jaegeuk Kim354a3392013-06-14 08:52:35 +09001222 else if (is_cold_data(page) || file_is_cold(inode))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001223 return CURSEG_COLD_DATA;
1224 else
1225 return CURSEG_WARM_DATA;
1226 } else {
1227 if (IS_DNODE(page))
1228 return is_cold_node(page) ? CURSEG_WARM_NODE :
1229 CURSEG_HOT_NODE;
1230 else
1231 return CURSEG_COLD_NODE;
1232 }
1233}
1234
1235static int __get_segment_type(struct page *page, enum page_type p_type)
1236{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001237 switch (F2FS_P_SB(page)->active_logs) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001238 case 2:
1239 return __get_segment_type_2(page, p_type);
1240 case 4:
1241 return __get_segment_type_4(page, p_type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001242 }
Jaegeuk Kim12a67142012-12-21 11:47:05 +09001243 /* NR_CURSEG_TYPE(6) logs by default */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001244 f2fs_bug_on(F2FS_P_SB(page),
1245 F2FS_P_SB(page)->active_logs != NR_CURSEG_TYPE);
Jaegeuk Kim12a67142012-12-21 11:47:05 +09001246 return __get_segment_type_6(page, p_type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001247}
1248
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001249void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
1250 block_t old_blkaddr, block_t *new_blkaddr,
1251 struct f2fs_summary *sum, int type)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001252{
1253 struct sit_info *sit_i = SIT_I(sbi);
1254 struct curseg_info *curseg;
Jaegeuk Kim38aa0882015-01-05 16:02:20 -08001255 bool direct_io = (type == CURSEG_DIRECT_IO);
1256
1257 type = direct_io ? CURSEG_WARM_DATA : type;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001258
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001259 curseg = CURSEG_I(sbi, type);
1260
1261 mutex_lock(&curseg->curseg_mutex);
Jaegeuk Kim21cb1d92015-03-11 13:42:48 -04001262 mutex_lock(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001263
Jaegeuk Kim38aa0882015-01-05 16:02:20 -08001264 /* direct_io'ed data is aligned to the segment for better performance */
Jaegeuk Kim47e70ca2015-08-11 10:17:27 -07001265 if (direct_io && curseg->next_blkoff &&
1266 !has_not_enough_free_secs(sbi, 0))
Jaegeuk Kim38aa0882015-01-05 16:02:20 -08001267 __allocate_new_segments(sbi, type);
1268
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001269 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001270
1271 /*
1272 * __add_sum_entry should be resided under the curseg_mutex
1273 * because, this function updates a summary entry in the
1274 * current summary block.
1275 */
Haicheng Lie79efe32013-06-13 16:59:27 +08001276 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001277
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001278 __refresh_next_blkoff(sbi, curseg);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +09001279
1280 stat_inc_block_count(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001281
Jaegeuk Kim5e443812014-01-28 12:22:14 +09001282 if (!__has_curseg_space(sbi, type))
1283 sit_i->s_ops->allocate_segment(sbi, type, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001284 /*
1285 * SIT information should be updated before segment allocation,
1286 * since SSR needs latest valid block information.
1287 */
1288 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
Jaegeuk Kim5e443812014-01-28 12:22:14 +09001289
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001290 mutex_unlock(&sit_i->sentry_lock);
1291
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001292 if (page && IS_NODESEG(type))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001293 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
1294
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001295 mutex_unlock(&curseg->curseg_mutex);
1296}
1297
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001298static void do_write_page(struct f2fs_summary *sum, struct f2fs_io_info *fio)
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001299{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001300 int type = __get_segment_type(fio->page, fio->type);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001301
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001302 allocate_data_block(fio->sbi, fio->page, fio->blk_addr,
1303 &fio->blk_addr, sum, type);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001304
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001305 /* writeout dirty page into bdev */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001306 f2fs_submit_page_mbio(fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001307}
1308
Jaegeuk Kim577e3492013-01-24 19:56:11 +09001309void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001310{
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001311 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001312 .sbi = sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001313 .type = META,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001314 .rw = WRITE_SYNC | REQ_META | REQ_PRIO,
1315 .blk_addr = page->index,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001316 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001317 .encrypted_page = NULL,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001318 };
1319
Chao Yu2b947002015-10-12 17:04:21 +08001320 if (unlikely(page->index >= MAIN_BLKADDR(sbi)))
1321 fio.rw &= ~REQ_META;
1322
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001323 set_page_writeback(page);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001324 f2fs_submit_page_mbio(&fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001325}
1326
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001327void write_node_page(unsigned int nid, struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001328{
1329 struct f2fs_summary sum;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001330
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001331 set_summary(&sum, nid, 0, 0);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001332 do_write_page(&sum, fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001333}
1334
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001335void write_data_page(struct dnode_of_data *dn, struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001336{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001337 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001338 struct f2fs_summary sum;
1339 struct node_info ni;
1340
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001341 f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001342 get_node_info(sbi, dn->nid, &ni);
1343 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001344 do_write_page(&sum, fio);
Jaegeuk Kime1509cf2014-12-30 22:57:55 -08001345 dn->data_blkaddr = fio->blk_addr;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001346}
1347
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001348void rewrite_data_page(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001349{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001350 stat_inc_inplace_blocks(fio->sbi);
1351 f2fs_submit_page_mbio(fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001352}
1353
Chao Yu528e3452015-05-28 19:15:35 +08001354static void __f2fs_replace_block(struct f2fs_sb_info *sbi,
1355 struct f2fs_summary *sum,
Chao Yu19f106b2015-05-06 13:08:06 +08001356 block_t old_blkaddr, block_t new_blkaddr,
1357 bool recover_curseg)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001358{
1359 struct sit_info *sit_i = SIT_I(sbi);
1360 struct curseg_info *curseg;
1361 unsigned int segno, old_cursegno;
1362 struct seg_entry *se;
1363 int type;
Chao Yu19f106b2015-05-06 13:08:06 +08001364 unsigned short old_blkoff;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001365
1366 segno = GET_SEGNO(sbi, new_blkaddr);
1367 se = get_seg_entry(sbi, segno);
1368 type = se->type;
1369
Chao Yu19f106b2015-05-06 13:08:06 +08001370 if (!recover_curseg) {
1371 /* for recovery flow */
1372 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
1373 if (old_blkaddr == NULL_ADDR)
1374 type = CURSEG_COLD_DATA;
1375 else
1376 type = CURSEG_WARM_DATA;
1377 }
1378 } else {
1379 if (!IS_CURSEG(sbi, segno))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001380 type = CURSEG_WARM_DATA;
1381 }
Chao Yu19f106b2015-05-06 13:08:06 +08001382
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001383 curseg = CURSEG_I(sbi, type);
1384
1385 mutex_lock(&curseg->curseg_mutex);
1386 mutex_lock(&sit_i->sentry_lock);
1387
1388 old_cursegno = curseg->segno;
Chao Yu19f106b2015-05-06 13:08:06 +08001389 old_blkoff = curseg->next_blkoff;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001390
1391 /* change the current segment */
1392 if (segno != curseg->segno) {
1393 curseg->next_segno = segno;
1394 change_curseg(sbi, type, true);
1395 }
1396
Jaegeuk Kim491c0852014-02-04 13:01:10 +09001397 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
Haicheng Lie79efe32013-06-13 16:59:27 +08001398 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001399
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001400 if (!recover_curseg)
1401 update_sit_entry(sbi, new_blkaddr, 1);
1402 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
1403 update_sit_entry(sbi, old_blkaddr, -1);
1404
1405 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
1406 locate_dirty_segment(sbi, GET_SEGNO(sbi, new_blkaddr));
1407
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001408 locate_dirty_segment(sbi, old_cursegno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001409
Chao Yu19f106b2015-05-06 13:08:06 +08001410 if (recover_curseg) {
1411 if (old_cursegno != curseg->segno) {
1412 curseg->next_segno = old_cursegno;
1413 change_curseg(sbi, type, true);
1414 }
1415 curseg->next_blkoff = old_blkoff;
1416 }
1417
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001418 mutex_unlock(&sit_i->sentry_lock);
1419 mutex_unlock(&curseg->curseg_mutex);
1420}
1421
Chao Yu528e3452015-05-28 19:15:35 +08001422void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
1423 block_t old_addr, block_t new_addr,
1424 unsigned char version, bool recover_curseg)
1425{
1426 struct f2fs_summary sum;
1427
1428 set_summary(&sum, dn->nid, dn->ofs_in_node, version);
1429
1430 __f2fs_replace_block(sbi, &sum, old_addr, new_addr, recover_curseg);
1431
1432 dn->data_blkaddr = new_addr;
1433 set_data_blkaddr(dn);
1434 f2fs_update_extent_cache(dn);
1435}
1436
Chao Yudf0f8dc2014-03-22 14:57:23 +08001437static inline bool is_merged_page(struct f2fs_sb_info *sbi,
1438 struct page *page, enum page_type type)
1439{
1440 enum page_type btype = PAGE_TYPE_OF_BIO(type);
1441 struct f2fs_bio_info *io = &sbi->write_io[btype];
Chao Yudf0f8dc2014-03-22 14:57:23 +08001442 struct bio_vec *bvec;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001443 struct page *target;
Chao Yudf0f8dc2014-03-22 14:57:23 +08001444 int i;
1445
1446 down_read(&io->io_rwsem);
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001447 if (!io->bio) {
1448 up_read(&io->io_rwsem);
1449 return false;
1450 }
Chao Yudf0f8dc2014-03-22 14:57:23 +08001451
Jaegeuk Kimce234472014-04-02 09:04:42 +09001452 bio_for_each_segment_all(bvec, io->bio, i) {
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001453
1454 if (bvec->bv_page->mapping) {
1455 target = bvec->bv_page;
1456 } else {
1457 struct f2fs_crypto_ctx *ctx;
1458
1459 /* encrypted page */
1460 ctx = (struct f2fs_crypto_ctx *)page_private(
1461 bvec->bv_page);
Jaegeuk Kimca40b032015-05-12 13:40:20 -07001462 target = ctx->w.control_page;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001463 }
1464
1465 if (page == target) {
Chao Yudf0f8dc2014-03-22 14:57:23 +08001466 up_read(&io->io_rwsem);
1467 return true;
1468 }
1469 }
1470
Chao Yudf0f8dc2014-03-22 14:57:23 +08001471 up_read(&io->io_rwsem);
1472 return false;
1473}
1474
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001475void f2fs_wait_on_page_writeback(struct page *page,
Yuan Zhong5514f0a2014-01-10 07:26:14 +00001476 enum page_type type)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001477{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001478 if (PageWriteback(page)) {
Jaegeuk Kim40813632014-09-02 15:31:18 -07001479 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
1480
Chao Yudf0f8dc2014-03-22 14:57:23 +08001481 if (is_merged_page(sbi, page, type))
1482 f2fs_submit_merged_bio(sbi, type, WRITE);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001483 wait_on_page_writeback(page);
1484 }
1485}
1486
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001487static int read_compacted_summaries(struct f2fs_sb_info *sbi)
1488{
1489 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1490 struct curseg_info *seg_i;
1491 unsigned char *kaddr;
1492 struct page *page;
1493 block_t start;
1494 int i, j, offset;
1495
1496 start = start_sum_block(sbi);
1497
1498 page = get_meta_page(sbi, start++);
1499 kaddr = (unsigned char *)page_address(page);
1500
1501 /* Step 1: restore nat cache */
1502 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1503 memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
1504
1505 /* Step 2: restore sit cache */
1506 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1507 memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
1508 SUM_JOURNAL_SIZE);
1509 offset = 2 * SUM_JOURNAL_SIZE;
1510
1511 /* Step 3: restore summary entries */
1512 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1513 unsigned short blk_off;
1514 unsigned int segno;
1515
1516 seg_i = CURSEG_I(sbi, i);
1517 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
1518 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
1519 seg_i->next_segno = segno;
1520 reset_curseg(sbi, i, 0);
1521 seg_i->alloc_type = ckpt->alloc_type[i];
1522 seg_i->next_blkoff = blk_off;
1523
1524 if (seg_i->alloc_type == SSR)
1525 blk_off = sbi->blocks_per_seg;
1526
1527 for (j = 0; j < blk_off; j++) {
1528 struct f2fs_summary *s;
1529 s = (struct f2fs_summary *)(kaddr + offset);
1530 seg_i->sum_blk->entries[j] = *s;
1531 offset += SUMMARY_SIZE;
1532 if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1533 SUM_FOOTER_SIZE)
1534 continue;
1535
1536 f2fs_put_page(page, 1);
1537 page = NULL;
1538
1539 page = get_meta_page(sbi, start++);
1540 kaddr = (unsigned char *)page_address(page);
1541 offset = 0;
1542 }
1543 }
1544 f2fs_put_page(page, 1);
1545 return 0;
1546}
1547
1548static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
1549{
1550 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1551 struct f2fs_summary_block *sum;
1552 struct curseg_info *curseg;
1553 struct page *new;
1554 unsigned short blk_off;
1555 unsigned int segno = 0;
1556 block_t blk_addr = 0;
1557
1558 /* get segment number and block addr */
1559 if (IS_DATASEG(type)) {
1560 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
1561 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
1562 CURSEG_HOT_DATA]);
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001563 if (__exist_node_summaries(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001564 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
1565 else
1566 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
1567 } else {
1568 segno = le32_to_cpu(ckpt->cur_node_segno[type -
1569 CURSEG_HOT_NODE]);
1570 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
1571 CURSEG_HOT_NODE]);
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001572 if (__exist_node_summaries(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001573 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
1574 type - CURSEG_HOT_NODE);
1575 else
1576 blk_addr = GET_SUM_BLOCK(sbi, segno);
1577 }
1578
1579 new = get_meta_page(sbi, blk_addr);
1580 sum = (struct f2fs_summary_block *)page_address(new);
1581
1582 if (IS_NODESEG(type)) {
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001583 if (__exist_node_summaries(sbi)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001584 struct f2fs_summary *ns = &sum->entries[0];
1585 int i;
1586 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
1587 ns->version = 0;
1588 ns->ofs_in_node = 0;
1589 }
1590 } else {
Gu Zhengd6537882014-03-07 18:43:36 +08001591 int err;
1592
1593 err = restore_node_summary(sbi, segno, sum);
1594 if (err) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001595 f2fs_put_page(new, 1);
Gu Zhengd6537882014-03-07 18:43:36 +08001596 return err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001597 }
1598 }
1599 }
1600
1601 /* set uncompleted segment to curseg */
1602 curseg = CURSEG_I(sbi, type);
1603 mutex_lock(&curseg->curseg_mutex);
1604 memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
1605 curseg->next_segno = segno;
1606 reset_curseg(sbi, type, 0);
1607 curseg->alloc_type = ckpt->alloc_type[type];
1608 curseg->next_blkoff = blk_off;
1609 mutex_unlock(&curseg->curseg_mutex);
1610 f2fs_put_page(new, 1);
1611 return 0;
1612}
1613
1614static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
1615{
1616 int type = CURSEG_HOT_DATA;
Chao Yue4fc5fb2014-03-17 16:36:24 +08001617 int err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001618
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001619 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
Chao Yu3fa06d72014-12-09 14:21:46 +08001620 int npages = npages_for_summary_flush(sbi, true);
1621
1622 if (npages >= 2)
1623 ra_meta_pages(sbi, start_sum_block(sbi), npages,
Chao Yu26879fb2015-10-12 17:05:59 +08001624 META_CP, true);
Chao Yu3fa06d72014-12-09 14:21:46 +08001625
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001626 /* restore for compacted data summary */
1627 if (read_compacted_summaries(sbi))
1628 return -EINVAL;
1629 type = CURSEG_HOT_NODE;
1630 }
1631
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001632 if (__exist_node_summaries(sbi))
Chao Yu3fa06d72014-12-09 14:21:46 +08001633 ra_meta_pages(sbi, sum_blk_addr(sbi, NR_CURSEG_TYPE, type),
Chao Yu26879fb2015-10-12 17:05:59 +08001634 NR_CURSEG_TYPE - type, META_CP, true);
Chao Yu3fa06d72014-12-09 14:21:46 +08001635
Chao Yue4fc5fb2014-03-17 16:36:24 +08001636 for (; type <= CURSEG_COLD_NODE; type++) {
1637 err = read_normal_summaries(sbi, type);
1638 if (err)
1639 return err;
1640 }
1641
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001642 return 0;
1643}
1644
1645static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
1646{
1647 struct page *page;
1648 unsigned char *kaddr;
1649 struct f2fs_summary *summary;
1650 struct curseg_info *seg_i;
1651 int written_size = 0;
1652 int i, j;
1653
1654 page = grab_meta_page(sbi, blkaddr++);
1655 kaddr = (unsigned char *)page_address(page);
1656
1657 /* Step 1: write nat cache */
1658 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1659 memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
1660 written_size += SUM_JOURNAL_SIZE;
1661
1662 /* Step 2: write sit cache */
1663 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1664 memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
1665 SUM_JOURNAL_SIZE);
1666 written_size += SUM_JOURNAL_SIZE;
1667
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001668 /* Step 3: write summary entries */
1669 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1670 unsigned short blkoff;
1671 seg_i = CURSEG_I(sbi, i);
1672 if (sbi->ckpt->alloc_type[i] == SSR)
1673 blkoff = sbi->blocks_per_seg;
1674 else
1675 blkoff = curseg_blkoff(sbi, i);
1676
1677 for (j = 0; j < blkoff; j++) {
1678 if (!page) {
1679 page = grab_meta_page(sbi, blkaddr++);
1680 kaddr = (unsigned char *)page_address(page);
1681 written_size = 0;
1682 }
1683 summary = (struct f2fs_summary *)(kaddr + written_size);
1684 *summary = seg_i->sum_blk->entries[j];
1685 written_size += SUMMARY_SIZE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001686
1687 if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1688 SUM_FOOTER_SIZE)
1689 continue;
1690
Chao Yue8d61a72013-10-24 15:08:28 +08001691 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001692 f2fs_put_page(page, 1);
1693 page = NULL;
1694 }
1695 }
Chao Yue8d61a72013-10-24 15:08:28 +08001696 if (page) {
1697 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001698 f2fs_put_page(page, 1);
Chao Yue8d61a72013-10-24 15:08:28 +08001699 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001700}
1701
1702static void write_normal_summaries(struct f2fs_sb_info *sbi,
1703 block_t blkaddr, int type)
1704{
1705 int i, end;
1706 if (IS_DATASEG(type))
1707 end = type + NR_CURSEG_DATA_TYPE;
1708 else
1709 end = type + NR_CURSEG_NODE_TYPE;
1710
1711 for (i = type; i < end; i++) {
1712 struct curseg_info *sum = CURSEG_I(sbi, i);
1713 mutex_lock(&sum->curseg_mutex);
1714 write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
1715 mutex_unlock(&sum->curseg_mutex);
1716 }
1717}
1718
1719void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1720{
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001721 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001722 write_compacted_summaries(sbi, start_blk);
1723 else
1724 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
1725}
1726
1727void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1728{
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001729 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001730}
1731
1732int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
1733 unsigned int val, int alloc)
1734{
1735 int i;
1736
1737 if (type == NAT_JOURNAL) {
1738 for (i = 0; i < nats_in_cursum(sum); i++) {
1739 if (le32_to_cpu(nid_in_journal(sum, i)) == val)
1740 return i;
1741 }
1742 if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
1743 return update_nats_in_cursum(sum, 1);
1744 } else if (type == SIT_JOURNAL) {
1745 for (i = 0; i < sits_in_cursum(sum); i++)
1746 if (le32_to_cpu(segno_in_journal(sum, i)) == val)
1747 return i;
1748 if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
1749 return update_sits_in_cursum(sum, 1);
1750 }
1751 return -1;
1752}
1753
1754static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
1755 unsigned int segno)
1756{
Gu Zheng2cc22182014-10-20 17:45:49 +08001757 return get_meta_page(sbi, current_sit_addr(sbi, segno));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001758}
1759
1760static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
1761 unsigned int start)
1762{
1763 struct sit_info *sit_i = SIT_I(sbi);
1764 struct page *src_page, *dst_page;
1765 pgoff_t src_off, dst_off;
1766 void *src_addr, *dst_addr;
1767
1768 src_off = current_sit_addr(sbi, start);
1769 dst_off = next_sit_addr(sbi, src_off);
1770
1771 /* get current sit block page without lock */
1772 src_page = get_meta_page(sbi, src_off);
1773 dst_page = grab_meta_page(sbi, dst_off);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001774 f2fs_bug_on(sbi, PageDirty(src_page));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001775
1776 src_addr = page_address(src_page);
1777 dst_addr = page_address(dst_page);
1778 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
1779
1780 set_page_dirty(dst_page);
1781 f2fs_put_page(src_page, 1);
1782
1783 set_to_next_sit(sit_i, start);
1784
1785 return dst_page;
1786}
1787
Chao Yu184a5cd2014-09-04 18:13:01 +08001788static struct sit_entry_set *grab_sit_entry_set(void)
1789{
1790 struct sit_entry_set *ses =
Jaegeuk Kim80c54502015-08-20 08:51:56 -07001791 f2fs_kmem_cache_alloc(sit_entry_set_slab, GFP_NOFS);
Chao Yu184a5cd2014-09-04 18:13:01 +08001792
1793 ses->entry_cnt = 0;
1794 INIT_LIST_HEAD(&ses->set_list);
1795 return ses;
1796}
1797
1798static void release_sit_entry_set(struct sit_entry_set *ses)
1799{
1800 list_del(&ses->set_list);
1801 kmem_cache_free(sit_entry_set_slab, ses);
1802}
1803
1804static void adjust_sit_entry_set(struct sit_entry_set *ses,
1805 struct list_head *head)
1806{
1807 struct sit_entry_set *next = ses;
1808
1809 if (list_is_last(&ses->set_list, head))
1810 return;
1811
1812 list_for_each_entry_continue(next, head, set_list)
1813 if (ses->entry_cnt <= next->entry_cnt)
1814 break;
1815
1816 list_move_tail(&ses->set_list, &next->set_list);
1817}
1818
1819static void add_sit_entry(unsigned int segno, struct list_head *head)
1820{
1821 struct sit_entry_set *ses;
1822 unsigned int start_segno = START_SEGNO(segno);
1823
1824 list_for_each_entry(ses, head, set_list) {
1825 if (ses->start_segno == start_segno) {
1826 ses->entry_cnt++;
1827 adjust_sit_entry_set(ses, head);
1828 return;
1829 }
1830 }
1831
1832 ses = grab_sit_entry_set();
1833
1834 ses->start_segno = start_segno;
1835 ses->entry_cnt++;
1836 list_add(&ses->set_list, head);
1837}
1838
1839static void add_sits_in_set(struct f2fs_sb_info *sbi)
1840{
1841 struct f2fs_sm_info *sm_info = SM_I(sbi);
1842 struct list_head *set_list = &sm_info->sit_entry_set;
1843 unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap;
Chao Yu184a5cd2014-09-04 18:13:01 +08001844 unsigned int segno;
1845
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001846 for_each_set_bit(segno, bitmap, MAIN_SEGS(sbi))
Chao Yu184a5cd2014-09-04 18:13:01 +08001847 add_sit_entry(segno, set_list);
1848}
1849
1850static void remove_sits_in_journal(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001851{
1852 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1853 struct f2fs_summary_block *sum = curseg->sum_blk;
1854 int i;
1855
Chao Yu184a5cd2014-09-04 18:13:01 +08001856 for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
1857 unsigned int segno;
1858 bool dirtied;
1859
1860 segno = le32_to_cpu(segno_in_journal(sum, i));
1861 dirtied = __mark_sit_entry_dirty(sbi, segno);
1862
1863 if (!dirtied)
1864 add_sit_entry(segno, &SM_I(sbi)->sit_entry_set);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001865 }
Chao Yu184a5cd2014-09-04 18:13:01 +08001866 update_sits_in_cursum(sum, -sits_in_cursum(sum));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001867}
1868
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001869/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001870 * CP calls this function, which flushes SIT entries including sit_journal,
1871 * and moves prefree segs to free segs.
1872 */
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001873void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001874{
1875 struct sit_info *sit_i = SIT_I(sbi);
1876 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
1877 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1878 struct f2fs_summary_block *sum = curseg->sum_blk;
Chao Yu184a5cd2014-09-04 18:13:01 +08001879 struct sit_entry_set *ses, *tmp;
1880 struct list_head *head = &SM_I(sbi)->sit_entry_set;
Chao Yu184a5cd2014-09-04 18:13:01 +08001881 bool to_journal = true;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001882 struct seg_entry *se;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001883
1884 mutex_lock(&curseg->curseg_mutex);
1885 mutex_lock(&sit_i->sentry_lock);
1886
Wanpeng Li2b11a742015-02-27 16:52:50 +08001887 if (!sit_i->dirty_sentries)
1888 goto out;
1889
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001890 /*
Chao Yu184a5cd2014-09-04 18:13:01 +08001891 * add and account sit entries of dirty bitmap in sit entry
1892 * set temporarily
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001893 */
Chao Yu184a5cd2014-09-04 18:13:01 +08001894 add_sits_in_set(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001895
Chao Yu184a5cd2014-09-04 18:13:01 +08001896 /*
1897 * if there are no enough space in journal to store dirty sit
1898 * entries, remove all entries from journal and add and account
1899 * them in sit entry set.
1900 */
1901 if (!__has_cursum_space(sum, sit_i->dirty_sentries, SIT_JOURNAL))
1902 remove_sits_in_journal(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001903
Chao Yu184a5cd2014-09-04 18:13:01 +08001904 /*
1905 * there are two steps to flush sit entries:
1906 * #1, flush sit entries to journal in current cold data summary block.
1907 * #2, flush sit entries to sit page.
1908 */
1909 list_for_each_entry_safe(ses, tmp, head, set_list) {
Jaegeuk Kim4a257ed2014-10-16 11:43:30 -07001910 struct page *page = NULL;
Chao Yu184a5cd2014-09-04 18:13:01 +08001911 struct f2fs_sit_block *raw_sit = NULL;
1912 unsigned int start_segno = ses->start_segno;
1913 unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001914 (unsigned long)MAIN_SEGS(sbi));
Chao Yu184a5cd2014-09-04 18:13:01 +08001915 unsigned int segno = start_segno;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001916
Chao Yu184a5cd2014-09-04 18:13:01 +08001917 if (to_journal &&
1918 !__has_cursum_space(sum, ses->entry_cnt, SIT_JOURNAL))
1919 to_journal = false;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001920
Chao Yu184a5cd2014-09-04 18:13:01 +08001921 if (!to_journal) {
1922 page = get_next_sit_page(sbi, start_segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001923 raw_sit = page_address(page);
1924 }
1925
Chao Yu184a5cd2014-09-04 18:13:01 +08001926 /* flush dirty sit entries in region of current sit set */
1927 for_each_set_bit_from(segno, bitmap, end) {
1928 int offset, sit_offset;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001929
1930 se = get_seg_entry(sbi, segno);
Chao Yu184a5cd2014-09-04 18:13:01 +08001931
1932 /* add discard candidates */
Jaegeuk Kimd7bc2482014-12-12 13:53:41 -08001933 if (cpc->reason != CP_DISCARD) {
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001934 cpc->trim_start = segno;
1935 add_discard_addrs(sbi, cpc);
1936 }
Chao Yu184a5cd2014-09-04 18:13:01 +08001937
1938 if (to_journal) {
1939 offset = lookup_journal_in_cursum(sum,
1940 SIT_JOURNAL, segno, 1);
1941 f2fs_bug_on(sbi, offset < 0);
1942 segno_in_journal(sum, offset) =
1943 cpu_to_le32(segno);
1944 seg_info_to_raw_sit(se,
1945 &sit_in_journal(sum, offset));
1946 } else {
1947 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
1948 seg_info_to_raw_sit(se,
1949 &raw_sit->entries[sit_offset]);
1950 }
1951
1952 __clear_bit(segno, bitmap);
1953 sit_i->dirty_sentries--;
1954 ses->entry_cnt--;
1955 }
1956
1957 if (!to_journal)
1958 f2fs_put_page(page, 1);
1959
1960 f2fs_bug_on(sbi, ses->entry_cnt);
1961 release_sit_entry_set(ses);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001962 }
Chao Yu184a5cd2014-09-04 18:13:01 +08001963
1964 f2fs_bug_on(sbi, !list_empty(head));
1965 f2fs_bug_on(sbi, sit_i->dirty_sentries);
Chao Yu184a5cd2014-09-04 18:13:01 +08001966out:
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001967 if (cpc->reason == CP_DISCARD) {
1968 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++)
1969 add_discard_addrs(sbi, cpc);
1970 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001971 mutex_unlock(&sit_i->sentry_lock);
1972 mutex_unlock(&curseg->curseg_mutex);
1973
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001974 set_prefree_as_free_segments(sbi);
1975}
1976
1977static int build_sit_info(struct f2fs_sb_info *sbi)
1978{
1979 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1980 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1981 struct sit_info *sit_i;
1982 unsigned int sit_segs, start;
1983 char *src_bitmap, *dst_bitmap;
1984 unsigned int bitmap_size;
1985
1986 /* allocate memory for SIT information */
1987 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
1988 if (!sit_i)
1989 return -ENOMEM;
1990
1991 SM_I(sbi)->sit_info = sit_i;
1992
Jaegeuk Kim39307a82015-09-22 13:50:47 -07001993 sit_i->sentries = f2fs_kvzalloc(MAIN_SEGS(sbi) *
1994 sizeof(struct seg_entry), GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001995 if (!sit_i->sentries)
1996 return -ENOMEM;
1997
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001998 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Jaegeuk Kim39307a82015-09-22 13:50:47 -07001999 sit_i->dirty_sentries_bitmap = f2fs_kvzalloc(bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002000 if (!sit_i->dirty_sentries_bitmap)
2001 return -ENOMEM;
2002
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002003 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002004 sit_i->sentries[start].cur_valid_map
2005 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
2006 sit_i->sentries[start].ckpt_valid_map
2007 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07002008 sit_i->sentries[start].discard_map
2009 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
2010 if (!sit_i->sentries[start].cur_valid_map ||
2011 !sit_i->sentries[start].ckpt_valid_map ||
2012 !sit_i->sentries[start].discard_map)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002013 return -ENOMEM;
2014 }
2015
Jaegeuk Kim60a3b782015-02-10 16:44:29 -08002016 sit_i->tmp_map = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
2017 if (!sit_i->tmp_map)
2018 return -ENOMEM;
2019
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002020 if (sbi->segs_per_sec > 1) {
Jaegeuk Kim39307a82015-09-22 13:50:47 -07002021 sit_i->sec_entries = f2fs_kvzalloc(MAIN_SECS(sbi) *
2022 sizeof(struct sec_entry), GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002023 if (!sit_i->sec_entries)
2024 return -ENOMEM;
2025 }
2026
2027 /* get information related with SIT */
2028 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
2029
2030 /* setup SIT bitmap from ckeckpoint pack */
2031 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
2032 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
2033
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02002034 dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002035 if (!dst_bitmap)
2036 return -ENOMEM;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002037
2038 /* init SIT information */
2039 sit_i->s_ops = &default_salloc_ops;
2040
2041 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
2042 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
2043 sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
2044 sit_i->sit_bitmap = dst_bitmap;
2045 sit_i->bitmap_size = bitmap_size;
2046 sit_i->dirty_sentries = 0;
2047 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
2048 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
2049 sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
2050 mutex_init(&sit_i->sentry_lock);
2051 return 0;
2052}
2053
2054static int build_free_segmap(struct f2fs_sb_info *sbi)
2055{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002056 struct free_segmap_info *free_i;
2057 unsigned int bitmap_size, sec_bitmap_size;
2058
2059 /* allocate memory for free segmap information */
2060 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
2061 if (!free_i)
2062 return -ENOMEM;
2063
2064 SM_I(sbi)->free_info = free_i;
2065
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002066 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Jaegeuk Kim39307a82015-09-22 13:50:47 -07002067 free_i->free_segmap = f2fs_kvmalloc(bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002068 if (!free_i->free_segmap)
2069 return -ENOMEM;
2070
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002071 sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
Jaegeuk Kim39307a82015-09-22 13:50:47 -07002072 free_i->free_secmap = f2fs_kvmalloc(sec_bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002073 if (!free_i->free_secmap)
2074 return -ENOMEM;
2075
2076 /* set all segments as dirty temporarily */
2077 memset(free_i->free_segmap, 0xff, bitmap_size);
2078 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
2079
2080 /* init free segmap information */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002081 free_i->start_segno = GET_SEGNO_FROM_SEG0(sbi, MAIN_BLKADDR(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002082 free_i->free_segments = 0;
2083 free_i->free_sections = 0;
Chao Yu1a118cc2015-02-11 18:20:38 +08002084 spin_lock_init(&free_i->segmap_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002085 return 0;
2086}
2087
2088static int build_curseg(struct f2fs_sb_info *sbi)
2089{
Namjae Jeon1042d602012-12-01 10:56:13 +09002090 struct curseg_info *array;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002091 int i;
2092
Fabian Frederickb434bab2014-06-23 18:39:15 +02002093 array = kcalloc(NR_CURSEG_TYPE, sizeof(*array), GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002094 if (!array)
2095 return -ENOMEM;
2096
2097 SM_I(sbi)->curseg_array = array;
2098
2099 for (i = 0; i < NR_CURSEG_TYPE; i++) {
2100 mutex_init(&array[i].curseg_mutex);
2101 array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
2102 if (!array[i].sum_blk)
2103 return -ENOMEM;
2104 array[i].segno = NULL_SEGNO;
2105 array[i].next_blkoff = 0;
2106 }
2107 return restore_curseg_summaries(sbi);
2108}
2109
2110static void build_sit_entries(struct f2fs_sb_info *sbi)
2111{
2112 struct sit_info *sit_i = SIT_I(sbi);
2113 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
2114 struct f2fs_summary_block *sum = curseg->sum_blk;
Chao Yu74de5932013-11-22 09:09:59 +08002115 int sit_blk_cnt = SIT_BLK_CNT(sbi);
2116 unsigned int i, start, end;
2117 unsigned int readed, start_blk = 0;
Jaegeuk Kim90a893c2014-09-22 16:21:07 -07002118 int nrpages = MAX_BIO_BLOCKS(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002119
Chao Yu74de5932013-11-22 09:09:59 +08002120 do {
Chao Yu26879fb2015-10-12 17:05:59 +08002121 readed = ra_meta_pages(sbi, start_blk, nrpages, META_SIT, true);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002122
Chao Yu74de5932013-11-22 09:09:59 +08002123 start = start_blk * sit_i->sents_per_block;
2124 end = (start_blk + readed) * sit_i->sents_per_block;
2125
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002126 for (; start < end && start < MAIN_SEGS(sbi); start++) {
Chao Yu74de5932013-11-22 09:09:59 +08002127 struct seg_entry *se = &sit_i->sentries[start];
2128 struct f2fs_sit_block *sit_blk;
2129 struct f2fs_sit_entry sit;
2130 struct page *page;
2131
2132 mutex_lock(&curseg->curseg_mutex);
2133 for (i = 0; i < sits_in_cursum(sum); i++) {
Chris Fries6c311ec2014-01-17 14:44:39 -06002134 if (le32_to_cpu(segno_in_journal(sum, i))
2135 == start) {
Chao Yu74de5932013-11-22 09:09:59 +08002136 sit = sit_in_journal(sum, i);
2137 mutex_unlock(&curseg->curseg_mutex);
2138 goto got_it;
2139 }
2140 }
2141 mutex_unlock(&curseg->curseg_mutex);
2142
2143 page = get_current_sit_page(sbi, start);
2144 sit_blk = (struct f2fs_sit_block *)page_address(page);
2145 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
2146 f2fs_put_page(page, 1);
2147got_it:
2148 check_block_count(sbi, start, &sit);
2149 seg_info_from_raw_sit(se, &sit);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07002150
2151 /* build discard map only one time */
2152 memcpy(se->discard_map, se->cur_valid_map, SIT_VBLOCK_MAP_SIZE);
2153 sbi->discard_blks += sbi->blocks_per_seg - se->valid_blocks;
2154
Chao Yu74de5932013-11-22 09:09:59 +08002155 if (sbi->segs_per_sec > 1) {
2156 struct sec_entry *e = get_sec_entry(sbi, start);
2157 e->valid_blocks += se->valid_blocks;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002158 }
2159 }
Chao Yu74de5932013-11-22 09:09:59 +08002160 start_blk += readed;
2161 } while (start_blk < sit_blk_cnt);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002162}
2163
2164static void init_free_segmap(struct f2fs_sb_info *sbi)
2165{
2166 unsigned int start;
2167 int type;
2168
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002169 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002170 struct seg_entry *sentry = get_seg_entry(sbi, start);
2171 if (!sentry->valid_blocks)
2172 __set_free(sbi, start);
2173 }
2174
2175 /* set use the current segments */
2176 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
2177 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
2178 __set_test_and_inuse(sbi, curseg_t->segno);
2179 }
2180}
2181
2182static void init_dirty_segmap(struct f2fs_sb_info *sbi)
2183{
2184 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2185 struct free_segmap_info *free_i = FREE_I(sbi);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002186 unsigned int segno = 0, offset = 0;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002187 unsigned short valid_blocks;
2188
Namjae Jeon8736fbf2013-06-16 09:49:11 +09002189 while (1) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002190 /* find dirty segment based on free segmap */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002191 segno = find_next_inuse(free_i, MAIN_SEGS(sbi), offset);
2192 if (segno >= MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002193 break;
2194 offset = segno + 1;
2195 valid_blocks = get_valid_blocks(sbi, segno, 0);
Jaegeuk Kimec325b52014-09-02 16:24:11 -07002196 if (valid_blocks == sbi->blocks_per_seg || !valid_blocks)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002197 continue;
Jaegeuk Kimec325b52014-09-02 16:24:11 -07002198 if (valid_blocks > sbi->blocks_per_seg) {
2199 f2fs_bug_on(sbi, 1);
2200 continue;
2201 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002202 mutex_lock(&dirty_i->seglist_lock);
2203 __locate_dirty_segment(sbi, segno, DIRTY);
2204 mutex_unlock(&dirty_i->seglist_lock);
2205 }
2206}
2207
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002208static int init_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002209{
2210 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002211 unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002212
Jaegeuk Kim39307a82015-09-22 13:50:47 -07002213 dirty_i->victim_secmap = f2fs_kvzalloc(bitmap_size, GFP_KERNEL);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002214 if (!dirty_i->victim_secmap)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002215 return -ENOMEM;
2216 return 0;
2217}
2218
2219static int build_dirty_segmap(struct f2fs_sb_info *sbi)
2220{
2221 struct dirty_seglist_info *dirty_i;
2222 unsigned int bitmap_size, i;
2223
2224 /* allocate memory for dirty segments list information */
2225 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
2226 if (!dirty_i)
2227 return -ENOMEM;
2228
2229 SM_I(sbi)->dirty_info = dirty_i;
2230 mutex_init(&dirty_i->seglist_lock);
2231
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002232 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002233
2234 for (i = 0; i < NR_DIRTY_TYPE; i++) {
Jaegeuk Kim39307a82015-09-22 13:50:47 -07002235 dirty_i->dirty_segmap[i] = f2fs_kvzalloc(bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002236 if (!dirty_i->dirty_segmap[i])
2237 return -ENOMEM;
2238 }
2239
2240 init_dirty_segmap(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002241 return init_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002242}
2243
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002244/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002245 * Update min, max modified time for cost-benefit GC algorithm
2246 */
2247static void init_min_max_mtime(struct f2fs_sb_info *sbi)
2248{
2249 struct sit_info *sit_i = SIT_I(sbi);
2250 unsigned int segno;
2251
2252 mutex_lock(&sit_i->sentry_lock);
2253
2254 sit_i->min_mtime = LLONG_MAX;
2255
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002256 for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002257 unsigned int i;
2258 unsigned long long mtime = 0;
2259
2260 for (i = 0; i < sbi->segs_per_sec; i++)
2261 mtime += get_seg_entry(sbi, segno + i)->mtime;
2262
2263 mtime = div_u64(mtime, sbi->segs_per_sec);
2264
2265 if (sit_i->min_mtime > mtime)
2266 sit_i->min_mtime = mtime;
2267 }
2268 sit_i->max_mtime = get_mtime(sbi);
2269 mutex_unlock(&sit_i->sentry_lock);
2270}
2271
2272int build_segment_manager(struct f2fs_sb_info *sbi)
2273{
2274 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
2275 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Namjae Jeon1042d602012-12-01 10:56:13 +09002276 struct f2fs_sm_info *sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002277 int err;
2278
2279 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
2280 if (!sm_info)
2281 return -ENOMEM;
2282
2283 /* init sm info */
2284 sbi->sm_info = sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002285 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
2286 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
2287 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
2288 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
2289 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
2290 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
2291 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
Jaegeuk Kim58c41032014-03-19 14:17:21 +09002292 sm_info->rec_prefree_segments = sm_info->main_segments *
2293 DEF_RECLAIM_PREFREE_SEGMENTS / 100;
Jaegeuk Kim9b5f1362014-09-16 18:30:54 -07002294 sm_info->ipu_policy = 1 << F2FS_IPU_FSYNC;
Jaegeuk Kim216fbd62013-11-07 13:13:42 +09002295 sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -07002296 sm_info->min_fsync_blocks = DEF_MIN_FSYNC_BLOCKS;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002297
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002298 INIT_LIST_HEAD(&sm_info->discard_list);
2299 sm_info->nr_discards = 0;
2300 sm_info->max_discards = 0;
2301
Jaegeuk Kimbba681c2015-01-26 17:41:23 -08002302 sm_info->trim_sections = DEF_BATCHED_TRIM_SECTIONS;
2303
Chao Yu184a5cd2014-09-04 18:13:01 +08002304 INIT_LIST_HEAD(&sm_info->sit_entry_set);
2305
Gu Zhengb270ad62014-04-11 17:49:55 +08002306 if (test_opt(sbi, FLUSH_MERGE) && !f2fs_readonly(sbi->sb)) {
Gu Zheng2163d192014-04-27 14:21:33 +08002307 err = create_flush_cmd_control(sbi);
2308 if (err)
Gu Zhenga688b9d9e2014-04-27 14:21:21 +08002309 return err;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +09002310 }
2311
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002312 err = build_sit_info(sbi);
2313 if (err)
2314 return err;
2315 err = build_free_segmap(sbi);
2316 if (err)
2317 return err;
2318 err = build_curseg(sbi);
2319 if (err)
2320 return err;
2321
2322 /* reinit free segmap based on SIT */
2323 build_sit_entries(sbi);
2324
2325 init_free_segmap(sbi);
2326 err = build_dirty_segmap(sbi);
2327 if (err)
2328 return err;
2329
2330 init_min_max_mtime(sbi);
2331 return 0;
2332}
2333
2334static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
2335 enum dirty_type dirty_type)
2336{
2337 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2338
2339 mutex_lock(&dirty_i->seglist_lock);
Jaegeuk Kim39307a82015-09-22 13:50:47 -07002340 kvfree(dirty_i->dirty_segmap[dirty_type]);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002341 dirty_i->nr_dirty[dirty_type] = 0;
2342 mutex_unlock(&dirty_i->seglist_lock);
2343}
2344
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002345static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002346{
2347 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim39307a82015-09-22 13:50:47 -07002348 kvfree(dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002349}
2350
2351static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
2352{
2353 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2354 int i;
2355
2356 if (!dirty_i)
2357 return;
2358
2359 /* discard pre-free/dirty segments list */
2360 for (i = 0; i < NR_DIRTY_TYPE; i++)
2361 discard_dirty_segmap(sbi, i);
2362
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002363 destroy_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002364 SM_I(sbi)->dirty_info = NULL;
2365 kfree(dirty_i);
2366}
2367
2368static void destroy_curseg(struct f2fs_sb_info *sbi)
2369{
2370 struct curseg_info *array = SM_I(sbi)->curseg_array;
2371 int i;
2372
2373 if (!array)
2374 return;
2375 SM_I(sbi)->curseg_array = NULL;
2376 for (i = 0; i < NR_CURSEG_TYPE; i++)
2377 kfree(array[i].sum_blk);
2378 kfree(array);
2379}
2380
2381static void destroy_free_segmap(struct f2fs_sb_info *sbi)
2382{
2383 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
2384 if (!free_i)
2385 return;
2386 SM_I(sbi)->free_info = NULL;
Jaegeuk Kim39307a82015-09-22 13:50:47 -07002387 kvfree(free_i->free_segmap);
2388 kvfree(free_i->free_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002389 kfree(free_i);
2390}
2391
2392static void destroy_sit_info(struct f2fs_sb_info *sbi)
2393{
2394 struct sit_info *sit_i = SIT_I(sbi);
2395 unsigned int start;
2396
2397 if (!sit_i)
2398 return;
2399
2400 if (sit_i->sentries) {
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002401 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002402 kfree(sit_i->sentries[start].cur_valid_map);
2403 kfree(sit_i->sentries[start].ckpt_valid_map);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07002404 kfree(sit_i->sentries[start].discard_map);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002405 }
2406 }
Jaegeuk Kim60a3b782015-02-10 16:44:29 -08002407 kfree(sit_i->tmp_map);
2408
Jaegeuk Kim39307a82015-09-22 13:50:47 -07002409 kvfree(sit_i->sentries);
2410 kvfree(sit_i->sec_entries);
2411 kvfree(sit_i->dirty_sentries_bitmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002412
2413 SM_I(sbi)->sit_info = NULL;
2414 kfree(sit_i->sit_bitmap);
2415 kfree(sit_i);
2416}
2417
2418void destroy_segment_manager(struct f2fs_sb_info *sbi)
2419{
2420 struct f2fs_sm_info *sm_info = SM_I(sbi);
Gu Zhenga688b9d9e2014-04-27 14:21:21 +08002421
Chao Yu3b03f722013-11-06 09:12:04 +08002422 if (!sm_info)
2423 return;
Gu Zheng2163d192014-04-27 14:21:33 +08002424 destroy_flush_cmd_control(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002425 destroy_dirty_segmap(sbi);
2426 destroy_curseg(sbi);
2427 destroy_free_segmap(sbi);
2428 destroy_sit_info(sbi);
2429 sbi->sm_info = NULL;
2430 kfree(sm_info);
2431}
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002432
2433int __init create_segment_manager_caches(void)
2434{
2435 discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
Gu Zhenge8512d22014-03-07 18:43:28 +08002436 sizeof(struct discard_entry));
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002437 if (!discard_entry_slab)
Chao Yu184a5cd2014-09-04 18:13:01 +08002438 goto fail;
2439
2440 sit_entry_set_slab = f2fs_kmem_cache_create("sit_entry_set",
Changman Leec9ee0082014-11-21 15:42:07 +09002441 sizeof(struct sit_entry_set));
Chao Yu184a5cd2014-09-04 18:13:01 +08002442 if (!sit_entry_set_slab)
2443 goto destory_discard_entry;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002444
2445 inmem_entry_slab = f2fs_kmem_cache_create("inmem_page_entry",
2446 sizeof(struct inmem_pages));
2447 if (!inmem_entry_slab)
2448 goto destroy_sit_entry_set;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002449 return 0;
Chao Yu184a5cd2014-09-04 18:13:01 +08002450
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002451destroy_sit_entry_set:
2452 kmem_cache_destroy(sit_entry_set_slab);
Chao Yu184a5cd2014-09-04 18:13:01 +08002453destory_discard_entry:
2454 kmem_cache_destroy(discard_entry_slab);
2455fail:
2456 return -ENOMEM;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002457}
2458
2459void destroy_segment_manager_caches(void)
2460{
Chao Yu184a5cd2014-09-04 18:13:01 +08002461 kmem_cache_destroy(sit_entry_set_slab);
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002462 kmem_cache_destroy(discard_entry_slab);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002463 kmem_cache_destroy(inmem_entry_slab);
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002464}