blob: ed2c5dec75267f2780f709a19d51d739c50844d8 [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
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070032static unsigned long __reverse_ulong(unsigned char *str)
33{
34 unsigned long tmp = 0;
35 int shift = 24, idx = 0;
36
37#if BITS_PER_LONG == 64
38 shift = 56;
39#endif
40 while (shift >= 0) {
41 tmp |= (unsigned long)str[idx++] << shift;
42 shift -= BITS_PER_BYTE;
43 }
44 return tmp;
45}
46
Changman Lee9a7f1432013-11-15 10:42:51 +090047/*
48 * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since
49 * MSB and LSB are reversed in a byte by f2fs_set_bit.
50 */
51static inline unsigned long __reverse_ffs(unsigned long word)
52{
53 int num = 0;
54
55#if BITS_PER_LONG == 64
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070056 if ((word & 0xffffffff00000000UL) == 0)
Changman Lee9a7f1432013-11-15 10:42:51 +090057 num += 32;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070058 else
Changman Lee9a7f1432013-11-15 10:42:51 +090059 word >>= 32;
Changman Lee9a7f1432013-11-15 10:42:51 +090060#endif
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070061 if ((word & 0xffff0000) == 0)
Changman Lee9a7f1432013-11-15 10:42:51 +090062 num += 16;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070063 else
Changman Lee9a7f1432013-11-15 10:42:51 +090064 word >>= 16;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070065
66 if ((word & 0xff00) == 0)
Changman Lee9a7f1432013-11-15 10:42:51 +090067 num += 8;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070068 else
Changman Lee9a7f1432013-11-15 10:42:51 +090069 word >>= 8;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070070
Changman Lee9a7f1432013-11-15 10:42:51 +090071 if ((word & 0xf0) == 0)
72 num += 4;
73 else
74 word >>= 4;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070075
Changman Lee9a7f1432013-11-15 10:42:51 +090076 if ((word & 0xc) == 0)
77 num += 2;
78 else
79 word >>= 2;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070080
Changman Lee9a7f1432013-11-15 10:42:51 +090081 if ((word & 0x2) == 0)
82 num += 1;
83 return num;
84}
85
86/*
arter97e1c42042014-08-06 23:22:50 +090087 * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c because
Changman Lee9a7f1432013-11-15 10:42:51 +090088 * f2fs_set_bit makes MSB and LSB reversed in a byte.
Fan Li692223d2015-11-12 08:43:04 +080089 * @size must be integral times of unsigned long.
Changman Lee9a7f1432013-11-15 10:42:51 +090090 * Example:
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070091 * MSB <--> LSB
92 * f2fs_set_bit(0, bitmap) => 1000 0000
93 * f2fs_set_bit(7, bitmap) => 0000 0001
Changman Lee9a7f1432013-11-15 10:42:51 +090094 */
95static unsigned long __find_rev_next_bit(const unsigned long *addr,
96 unsigned long size, unsigned long offset)
97{
98 const unsigned long *p = addr + BIT_WORD(offset);
Fan Li692223d2015-11-12 08:43:04 +080099 unsigned long result = size;
Changman Lee9a7f1432013-11-15 10:42:51 +0900100 unsigned long tmp;
Changman Lee9a7f1432013-11-15 10:42:51 +0900101
102 if (offset >= size)
103 return size;
104
Fan Li692223d2015-11-12 08:43:04 +0800105 size -= (offset & ~(BITS_PER_LONG - 1));
Changman Lee9a7f1432013-11-15 10:42:51 +0900106 offset %= BITS_PER_LONG;
Changman Lee9a7f1432013-11-15 10:42:51 +0900107
Fan Li692223d2015-11-12 08:43:04 +0800108 while (1) {
109 if (*p == 0)
110 goto pass;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700111
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700112 tmp = __reverse_ulong((unsigned char *)p);
Fan Li692223d2015-11-12 08:43:04 +0800113
114 tmp &= ~0UL >> offset;
115 if (size < BITS_PER_LONG)
116 tmp &= (~0UL << (BITS_PER_LONG - size));
Changman Lee9a7f1432013-11-15 10:42:51 +0900117 if (tmp)
Fan Li692223d2015-11-12 08:43:04 +0800118 goto found;
119pass:
120 if (size <= BITS_PER_LONG)
121 break;
Changman Lee9a7f1432013-11-15 10:42:51 +0900122 size -= BITS_PER_LONG;
Fan Li692223d2015-11-12 08:43:04 +0800123 offset = 0;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700124 p++;
Changman Lee9a7f1432013-11-15 10:42:51 +0900125 }
Fan Li692223d2015-11-12 08:43:04 +0800126 return result;
127found:
128 return result - size + __reverse_ffs(tmp);
Changman Lee9a7f1432013-11-15 10:42:51 +0900129}
130
131static unsigned long __find_rev_next_zero_bit(const unsigned long *addr,
132 unsigned long size, unsigned long offset)
133{
134 const unsigned long *p = addr + BIT_WORD(offset);
135 unsigned long result = offset & ~(BITS_PER_LONG - 1);
136 unsigned long tmp;
Changman Lee9a7f1432013-11-15 10:42:51 +0900137
138 if (offset >= size)
139 return size;
140
141 size -= result;
142 offset %= BITS_PER_LONG;
143 if (!offset)
144 goto aligned;
145
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700146 tmp = __reverse_ulong((unsigned char *)p);
147 tmp |= ~((~0UL << offset) >> offset);
148
Changman Lee9a7f1432013-11-15 10:42:51 +0900149 if (size < BITS_PER_LONG)
150 goto found_first;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700151 if (tmp != ~0UL)
Changman Lee9a7f1432013-11-15 10:42:51 +0900152 goto found_middle;
153
154 size -= BITS_PER_LONG;
155 result += BITS_PER_LONG;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700156 p++;
Changman Lee9a7f1432013-11-15 10:42:51 +0900157aligned:
158 while (size & ~(BITS_PER_LONG - 1)) {
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700159 tmp = __reverse_ulong((unsigned char *)p);
160 if (tmp != ~0UL)
Changman Lee9a7f1432013-11-15 10:42:51 +0900161 goto found_middle;
162 result += BITS_PER_LONG;
163 size -= BITS_PER_LONG;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700164 p++;
Changman Lee9a7f1432013-11-15 10:42:51 +0900165 }
166 if (!size)
167 return result;
Changman Lee9a7f1432013-11-15 10:42:51 +0900168
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700169 tmp = __reverse_ulong((unsigned char *)p);
Changman Lee9a7f1432013-11-15 10:42:51 +0900170found_first:
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700171 tmp |= ~(~0UL << (BITS_PER_LONG - size));
172 if (tmp == ~0UL) /* Are any bits zero? */
Changman Lee9a7f1432013-11-15 10:42:51 +0900173 return result + size; /* Nope. */
174found_middle:
175 return result + __reverse_ffz(tmp);
176}
177
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700178void register_inmem_page(struct inode *inode, struct page *page)
179{
180 struct f2fs_inode_info *fi = F2FS_I(inode);
181 struct inmem_pages *new;
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800182
Jaegeuk Kim9e4ded32014-12-17 19:58:58 -0800183 f2fs_trace_pid(page);
Jaegeuk Kim0722b102014-12-05 11:58:02 -0800184
Chao Yudecd36b2015-08-07 18:42:09 +0800185 set_page_private(page, (unsigned long)ATOMIC_WRITTEN_PAGE);
186 SetPagePrivate(page);
187
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700188 new = f2fs_kmem_cache_alloc(inmem_entry_slab, GFP_NOFS);
189
190 /* add atomic page indices to the list */
191 new->page = page;
192 INIT_LIST_HEAD(&new->list);
Chao Yudecd36b2015-08-07 18:42:09 +0800193
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700194 /* increase reference count with clean state */
195 mutex_lock(&fi->inmem_lock);
196 get_page(page);
197 list_add_tail(&new->list, &fi->inmem_pages);
Jaegeuk Kim8dcf2ff72014-12-05 17:18:15 -0800198 inc_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700199 mutex_unlock(&fi->inmem_lock);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -0700200
201 trace_f2fs_register_inmem_page(page, INMEM);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700202}
203
Jaegeuk Kimedb27de2015-07-25 00:52:52 -0700204int commit_inmem_pages(struct inode *inode, bool abort)
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700205{
206 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
207 struct f2fs_inode_info *fi = F2FS_I(inode);
208 struct inmem_pages *cur, *tmp;
209 bool submit_bio = false;
210 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700211 .sbi = sbi,
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700212 .type = DATA,
Jaegeuk Kim1e843712014-12-09 06:08:59 -0800213 .rw = WRITE_SYNC | REQ_PRIO,
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700214 .encrypted_page = NULL,
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700215 };
Jaegeuk Kimedb27de2015-07-25 00:52:52 -0700216 int err = 0;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700217
Jaegeuk Kim03418452014-11-21 16:37:40 -0800218 /*
219 * The abort is true only when f2fs_evict_inode is called.
220 * Basically, the f2fs_evict_inode doesn't produce any data writes, so
221 * that we don't need to call f2fs_balance_fs.
222 * Otherwise, f2fs_gc in f2fs_balance_fs can wait forever until this
223 * inode becomes free by iget_locked in f2fs_iget.
224 */
Jaegeuk Kim70c640b2014-12-10 13:59:33 -0800225 if (!abort) {
Jaegeuk Kim03418452014-11-21 16:37:40 -0800226 f2fs_balance_fs(sbi);
Jaegeuk Kim70c640b2014-12-10 13:59:33 -0800227 f2fs_lock_op(sbi);
228 }
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700229
230 mutex_lock(&fi->inmem_lock);
231 list_for_each_entry_safe(cur, tmp, &fi->inmem_pages, list) {
Chao Yudecd36b2015-08-07 18:42:09 +0800232 lock_page(cur->page);
Jaegeuk Kim70c640b2014-12-10 13:59:33 -0800233 if (!abort) {
Jaegeuk Kim70c640b2014-12-10 13:59:33 -0800234 if (cur->page->mapping == inode->i_mapping) {
Jaegeuk Kim6282adb2015-07-25 00:29:17 -0700235 set_page_dirty(cur->page);
Jaegeuk Kim70c640b2014-12-10 13:59:33 -0800236 f2fs_wait_on_page_writeback(cur->page, DATA);
237 if (clear_page_dirty_for_io(cur->page))
238 inode_dec_dirty_pages(inode);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -0700239 trace_f2fs_commit_inmem_page(cur->page, INMEM);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700240 fio.page = cur->page;
Jaegeuk Kimedb27de2015-07-25 00:52:52 -0700241 err = do_write_data_page(&fio);
Jaegeuk Kimedb27de2015-07-25 00:52:52 -0700242 if (err) {
243 unlock_page(cur->page);
244 break;
245 }
Chao Yu7fee7402015-10-22 18:18:11 +0800246 clear_cold_data(cur->page);
Jaegeuk Kim2b246fb2015-10-21 19:00:31 -0700247 submit_bio = true;
Jaegeuk Kim70c640b2014-12-10 13:59:33 -0800248 }
Jaegeuk Kim70c640b2014-12-10 13:59:33 -0800249 } else {
Chao Yuf478f432015-11-13 18:27:35 +0800250 ClearPageUptodate(cur->page);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -0700251 trace_f2fs_commit_inmem_page(cur->page, INMEM_DROP);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700252 }
Chao Yudecd36b2015-08-07 18:42:09 +0800253 set_page_private(cur->page, 0);
254 ClearPagePrivate(cur->page);
255 f2fs_put_page(cur->page, 1);
256
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700257 list_del(&cur->list);
258 kmem_cache_free(inmem_entry_slab, cur);
Jaegeuk Kim8dcf2ff72014-12-05 17:18:15 -0800259 dec_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700260 }
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700261 mutex_unlock(&fi->inmem_lock);
262
Jaegeuk Kim70c640b2014-12-10 13:59:33 -0800263 if (!abort) {
264 f2fs_unlock_op(sbi);
265 if (submit_bio)
266 f2fs_submit_merged_bio(sbi, DATA, WRITE);
267 }
Jaegeuk Kimedb27de2015-07-25 00:52:52 -0700268 return err;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700269}
270
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900271/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900272 * This function balances dirty node and dentry pages.
273 * In addition, it controls garbage collection.
274 */
275void f2fs_balance_fs(struct f2fs_sb_info *sbi)
276{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900277 /*
Jaegeuk Kim029cd282012-12-21 17:20:21 +0900278 * We should do GC or end up with checkpoint, if there are so many dirty
279 * dir/node pages without enough free segments.
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900280 */
Jaegeuk Kim43727522013-02-04 15:11:17 +0900281 if (has_not_enough_free_secs(sbi, 0)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900282 mutex_lock(&sbi->gc_mutex);
Chao Yud530d4d2015-10-05 22:22:44 +0800283 f2fs_gc(sbi, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900284 }
285}
286
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900287void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi)
288{
Chao Yu1dcc3362015-02-05 17:57:31 +0800289 /* try to shrink extent cache when there is no enough memory */
Jaegeuk Kim554df792015-06-19 13:41:23 -0700290 if (!available_free_memory(sbi, EXTENT_CACHE))
291 f2fs_shrink_extent_tree(sbi, EXTENT_CACHE_SHRINK_NUMBER);
Chao Yu1dcc3362015-02-05 17:57:31 +0800292
Jaegeuk Kim1b38dc82015-06-19 15:36:07 -0700293 /* check the # of cached NAT entries */
294 if (!available_free_memory(sbi, NAT_ENTRIES))
295 try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK);
296
Chao Yu31696582015-07-28 18:33:46 +0800297 if (!available_free_memory(sbi, FREE_NIDS))
298 try_to_free_nids(sbi, NAT_ENTRY_PER_BLOCK * FREE_NID_PAGES);
299
Jaegeuk Kim1b38dc82015-06-19 15:36:07 -0700300 /* checkpoint is the only way to shrink partial cached entries */
301 if (!available_free_memory(sbi, NAT_ENTRIES) ||
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -0800302 excess_prefree_segs(sbi) ||
Jaegeuk Kim60b99b42015-10-05 14:49:57 -0700303 !available_free_memory(sbi, INO_ENTRIES) ||
304 jiffies > sbi->cp_expires)
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900305 f2fs_sync_fs(sbi->sb, true);
306}
307
Gu Zheng2163d192014-04-27 14:21:33 +0800308static int issue_flush_thread(void *data)
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900309{
310 struct f2fs_sb_info *sbi = data;
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800311 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
312 wait_queue_head_t *q = &fcc->flush_wait_queue;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900313repeat:
314 if (kthread_should_stop())
315 return 0;
316
Gu Zheng721bd4d2014-09-05 18:31:00 +0800317 if (!llist_empty(&fcc->issue_list)) {
Jaegeuk Kim740432f2015-08-14 11:43:56 -0700318 struct bio *bio;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900319 struct flush_cmd *cmd, *next;
320 int ret;
321
Jaegeuk Kim740432f2015-08-14 11:43:56 -0700322 bio = f2fs_bio_alloc(0);
323
Gu Zheng721bd4d2014-09-05 18:31:00 +0800324 fcc->dispatch_list = llist_del_all(&fcc->issue_list);
325 fcc->dispatch_list = llist_reverse_order(fcc->dispatch_list);
326
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900327 bio->bi_bdev = sbi->sb->s_bdev;
328 ret = submit_bio_wait(WRITE_FLUSH, bio);
329
Gu Zheng721bd4d2014-09-05 18:31:00 +0800330 llist_for_each_entry_safe(cmd, next,
331 fcc->dispatch_list, llnode) {
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900332 cmd->ret = ret;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900333 complete(&cmd->wait);
334 }
Gu Zhenga4ed23f2014-04-11 17:49:35 +0800335 bio_put(bio);
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800336 fcc->dispatch_list = NULL;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900337 }
338
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800339 wait_event_interruptible(*q,
Gu Zheng721bd4d2014-09-05 18:31:00 +0800340 kthread_should_stop() || !llist_empty(&fcc->issue_list));
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900341 goto repeat;
342}
343
344int f2fs_issue_flush(struct f2fs_sb_info *sbi)
345{
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800346 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
Chao Yuadf8d902014-05-08 17:00:35 +0800347 struct flush_cmd cmd;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900348
Jaegeuk Kim24a9ee02014-07-25 17:46:10 -0700349 trace_f2fs_issue_flush(sbi->sb, test_opt(sbi, NOBARRIER),
350 test_opt(sbi, FLUSH_MERGE));
351
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700352 if (test_opt(sbi, NOBARRIER))
353 return 0;
354
Jaegeuk Kim740432f2015-08-14 11:43:56 -0700355 if (!test_opt(sbi, FLUSH_MERGE)) {
356 struct bio *bio = f2fs_bio_alloc(0);
357 int ret;
358
359 bio->bi_bdev = sbi->sb->s_bdev;
360 ret = submit_bio_wait(WRITE_FLUSH, bio);
361 bio_put(bio);
362 return ret;
363 }
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900364
Chao Yuadf8d902014-05-08 17:00:35 +0800365 init_completion(&cmd.wait);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900366
Gu Zheng721bd4d2014-09-05 18:31:00 +0800367 llist_add(&cmd.llnode, &fcc->issue_list);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900368
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800369 if (!fcc->dispatch_list)
370 wake_up(&fcc->flush_wait_queue);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900371
Chao Yuadf8d902014-05-08 17:00:35 +0800372 wait_for_completion(&cmd.wait);
373
374 return cmd.ret;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900375}
376
Gu Zheng2163d192014-04-27 14:21:33 +0800377int create_flush_cmd_control(struct f2fs_sb_info *sbi)
378{
379 dev_t dev = sbi->sb->s_bdev->bd_dev;
380 struct flush_cmd_control *fcc;
381 int err = 0;
382
383 fcc = kzalloc(sizeof(struct flush_cmd_control), GFP_KERNEL);
384 if (!fcc)
385 return -ENOMEM;
Gu Zheng2163d192014-04-27 14:21:33 +0800386 init_waitqueue_head(&fcc->flush_wait_queue);
Gu Zheng721bd4d2014-09-05 18:31:00 +0800387 init_llist_head(&fcc->issue_list);
Chao Yu6b2920a2014-07-07 11:21:59 +0800388 SM_I(sbi)->cmd_control_info = fcc;
Gu Zheng2163d192014-04-27 14:21:33 +0800389 fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi,
390 "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev));
391 if (IS_ERR(fcc->f2fs_issue_flush)) {
392 err = PTR_ERR(fcc->f2fs_issue_flush);
393 kfree(fcc);
Chao Yu6b2920a2014-07-07 11:21:59 +0800394 SM_I(sbi)->cmd_control_info = NULL;
Gu Zheng2163d192014-04-27 14:21:33 +0800395 return err;
396 }
Gu Zheng2163d192014-04-27 14:21:33 +0800397
398 return err;
399}
400
401void destroy_flush_cmd_control(struct f2fs_sb_info *sbi)
402{
Chao Yu6b2920a2014-07-07 11:21:59 +0800403 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
Gu Zheng2163d192014-04-27 14:21:33 +0800404
405 if (fcc && fcc->f2fs_issue_flush)
406 kthread_stop(fcc->f2fs_issue_flush);
407 kfree(fcc);
Chao Yu6b2920a2014-07-07 11:21:59 +0800408 SM_I(sbi)->cmd_control_info = NULL;
Gu Zheng2163d192014-04-27 14:21:33 +0800409}
410
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900411static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
412 enum dirty_type dirty_type)
413{
414 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
415
416 /* need not be added */
417 if (IS_CURSEG(sbi, segno))
418 return;
419
420 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
421 dirty_i->nr_dirty[dirty_type]++;
422
423 if (dirty_type == DIRTY) {
424 struct seg_entry *sentry = get_seg_entry(sbi, segno);
Changman Lee4625d6a2013-10-25 17:31:57 +0900425 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900426
Jaegeuk Kimec325b52014-09-02 16:24:11 -0700427 if (unlikely(t >= DIRTY)) {
428 f2fs_bug_on(sbi, 1);
429 return;
430 }
Changman Lee4625d6a2013-10-25 17:31:57 +0900431 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
432 dirty_i->nr_dirty[t]++;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900433 }
434}
435
436static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
437 enum dirty_type dirty_type)
438{
439 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
440
441 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
442 dirty_i->nr_dirty[dirty_type]--;
443
444 if (dirty_type == DIRTY) {
Changman Lee4625d6a2013-10-25 17:31:57 +0900445 struct seg_entry *sentry = get_seg_entry(sbi, segno);
446 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900447
Changman Lee4625d6a2013-10-25 17:31:57 +0900448 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
449 dirty_i->nr_dirty[t]--;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900450
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +0900451 if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0)
452 clear_bit(GET_SECNO(sbi, segno),
453 dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900454 }
455}
456
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900457/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900458 * Should not occur error such as -ENOMEM.
459 * Adding dirty entry into seglist is not critical operation.
460 * If a given segment is one of current working segments, it won't be added.
461 */
Haicheng Li8d8451a2013-06-13 16:59:28 +0800462static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900463{
464 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
465 unsigned short valid_blocks;
466
467 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
468 return;
469
470 mutex_lock(&dirty_i->seglist_lock);
471
472 valid_blocks = get_valid_blocks(sbi, segno, 0);
473
474 if (valid_blocks == 0) {
475 __locate_dirty_segment(sbi, segno, PRE);
476 __remove_dirty_segment(sbi, segno, DIRTY);
477 } else if (valid_blocks < sbi->blocks_per_seg) {
478 __locate_dirty_segment(sbi, segno, DIRTY);
479 } else {
480 /* Recovery routine with SSR needs this */
481 __remove_dirty_segment(sbi, segno, DIRTY);
482 }
483
484 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900485}
486
Jaegeuk Kim1e87a782014-04-15 13:57:55 +0900487static int f2fs_issue_discard(struct f2fs_sb_info *sbi,
Jaegeuk Kim37208872013-11-12 16:55:17 +0900488 block_t blkstart, block_t blklen)
489{
Chao Yu55cf9cb2014-09-15 18:01:10 +0800490 sector_t start = SECTOR_FROM_BLOCK(blkstart);
491 sector_t len = SECTOR_FROM_BLOCK(blklen);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700492 struct seg_entry *se;
493 unsigned int offset;
494 block_t i;
495
496 for (i = blkstart; i < blkstart + blklen; i++) {
497 se = get_seg_entry(sbi, GET_SEGNO(sbi, i));
498 offset = GET_BLKOFF_FROM_SEG0(sbi, i);
499
500 if (!f2fs_test_and_set_bit(offset, se->discard_map))
501 sbi->discard_blks--;
502 }
Jaegeuk Kim1661d072013-11-12 17:01:00 +0900503 trace_f2fs_issue_discard(sbi->sb, blkstart, blklen);
Jaegeuk Kim1e87a782014-04-15 13:57:55 +0900504 return blkdev_issue_discard(sbi->sb->s_bdev, start, len, GFP_NOFS, 0);
505}
506
Chao Yue90c2d22015-07-28 18:36:47 +0800507bool discard_next_dnode(struct f2fs_sb_info *sbi, block_t blkaddr)
Jaegeuk Kim1e87a782014-04-15 13:57:55 +0900508{
Jaegeuk Kim40a02be2015-05-11 20:03:49 -0700509 int err = -ENOTSUPP;
510
511 if (test_opt(sbi, DISCARD)) {
512 struct seg_entry *se = get_seg_entry(sbi,
513 GET_SEGNO(sbi, blkaddr));
514 unsigned int offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
515
516 if (f2fs_test_bit(offset, se->discard_map))
Chao Yue90c2d22015-07-28 18:36:47 +0800517 return false;
Jaegeuk Kim40a02be2015-05-11 20:03:49 -0700518
519 err = f2fs_issue_discard(sbi, blkaddr, 1);
520 }
521
Chao Yue90c2d22015-07-28 18:36:47 +0800522 if (err) {
Chao Yu381722d2015-05-19 17:40:04 +0800523 update_meta_page(sbi, NULL, blkaddr);
Chao Yue90c2d22015-07-28 18:36:47 +0800524 return true;
525 }
526 return false;
Jaegeuk Kim37208872013-11-12 16:55:17 +0900527}
528
Jaegeuk Kimadf49832014-10-28 22:27:59 -0700529static void __add_discard_entry(struct f2fs_sb_info *sbi,
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700530 struct cp_control *cpc, struct seg_entry *se,
531 unsigned int start, unsigned int end)
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900532{
533 struct list_head *head = &SM_I(sbi)->discard_list;
Jaegeuk Kimadf49832014-10-28 22:27:59 -0700534 struct discard_entry *new, *last;
535
536 if (!list_empty(head)) {
537 last = list_last_entry(head, struct discard_entry, list);
538 if (START_BLOCK(sbi, cpc->trim_start) + start ==
539 last->blkaddr + last->len) {
540 last->len += end - start;
541 goto done;
542 }
543 }
544
545 new = f2fs_kmem_cache_alloc(discard_entry_slab, GFP_NOFS);
546 INIT_LIST_HEAD(&new->list);
547 new->blkaddr = START_BLOCK(sbi, cpc->trim_start) + start;
548 new->len = end - start;
549 list_add_tail(&new->list, head);
550done:
551 SM_I(sbi)->nr_discards += end - start;
Jaegeuk Kimadf49832014-10-28 22:27:59 -0700552}
553
554static void add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc)
555{
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900556 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
557 int max_blocks = sbi->blocks_per_seg;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700558 struct seg_entry *se = get_seg_entry(sbi, cpc->trim_start);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900559 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
560 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700561 unsigned long *discard_map = (unsigned long *)se->discard_map;
Jaegeuk Kim60a3b782015-02-10 16:44:29 -0800562 unsigned long *dmap = SIT_I(sbi)->tmp_map;
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900563 unsigned int start = 0, end = -1;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700564 bool force = (cpc->reason == CP_DISCARD);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900565 int i;
566
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700567 if (se->valid_blocks == max_blocks)
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900568 return;
569
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700570 if (!force) {
571 if (!test_opt(sbi, DISCARD) || !se->valid_blocks ||
Dan Carpenter912a83b2015-05-14 11:52:28 +0300572 SM_I(sbi)->nr_discards >= SM_I(sbi)->max_discards)
573 return;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700574 }
575
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900576 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
577 for (i = 0; i < entries; i++)
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700578 dmap[i] = force ? ~ckpt_map[i] & ~discard_map[i] :
Jaegeuk Kimd7bc2482014-12-12 13:53:41 -0800579 (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900580
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700581 while (force || SM_I(sbi)->nr_discards <= SM_I(sbi)->max_discards) {
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900582 start = __find_rev_next_bit(dmap, max_blocks, end + 1);
583 if (start >= max_blocks)
584 break;
585
586 end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700587 __add_discard_entry(sbi, cpc, se, start, end);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900588 }
589}
590
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700591void release_discard_addrs(struct f2fs_sb_info *sbi)
592{
593 struct list_head *head = &(SM_I(sbi)->discard_list);
594 struct discard_entry *entry, *this;
595
596 /* drop caches */
597 list_for_each_entry_safe(entry, this, head, list) {
598 list_del(&entry->list);
599 kmem_cache_free(discard_entry_slab, entry);
600 }
601}
602
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900603/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900604 * Should call clear_prefree_segments after checkpoint is done.
605 */
606static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
607{
608 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Chao Yub65ee142014-08-04 10:10:07 +0800609 unsigned int segno;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900610
611 mutex_lock(&dirty_i->seglist_lock);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700612 for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900613 __set_test_and_free(sbi, segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900614 mutex_unlock(&dirty_i->seglist_lock);
615}
616
Jaegeuk Kim836b5a62015-04-30 22:50:06 -0700617void clear_prefree_segments(struct f2fs_sb_info *sbi, struct cp_control *cpc)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900618{
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900619 struct list_head *head = &(SM_I(sbi)->discard_list);
Chao Yu2d7b8222014-03-29 11:33:17 +0800620 struct discard_entry *entry, *this;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900621 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Changman Lee29e59c12013-11-11 09:24:37 +0900622 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
Changman Lee29e59c12013-11-11 09:24:37 +0900623 unsigned int start = 0, end = -1;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900624
625 mutex_lock(&dirty_i->seglist_lock);
Changman Lee29e59c12013-11-11 09:24:37 +0900626
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900627 while (1) {
Changman Lee29e59c12013-11-11 09:24:37 +0900628 int i;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700629 start = find_next_bit(prefree_map, MAIN_SEGS(sbi), end + 1);
630 if (start >= MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900631 break;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700632 end = find_next_zero_bit(prefree_map, MAIN_SEGS(sbi),
633 start + 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900634
Changman Lee29e59c12013-11-11 09:24:37 +0900635 for (i = start; i < end; i++)
636 clear_bit(i, prefree_map);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900637
Changman Lee29e59c12013-11-11 09:24:37 +0900638 dirty_i->nr_dirty[PRE] -= end - start;
639
640 if (!test_opt(sbi, DISCARD))
641 continue;
642
Jaegeuk Kim37208872013-11-12 16:55:17 +0900643 f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
644 (end - start) << sbi->log_blocks_per_seg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900645 }
646 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900647
648 /* send small discards */
Chao Yu2d7b8222014-03-29 11:33:17 +0800649 list_for_each_entry_safe(entry, this, head, list) {
Jaegeuk Kim836b5a62015-04-30 22:50:06 -0700650 if (cpc->reason == CP_DISCARD && entry->len < cpc->trim_minlen)
651 goto skip;
Jaegeuk Kim37208872013-11-12 16:55:17 +0900652 f2fs_issue_discard(sbi, entry->blkaddr, entry->len);
Jaegeuk Kimf56aa1c2015-06-02 15:48:20 -0700653 cpc->trimmed += entry->len;
Jaegeuk Kim836b5a62015-04-30 22:50:06 -0700654skip:
Jaegeuk Kimb2955552013-11-12 14:49:56 +0900655 list_del(&entry->list);
656 SM_I(sbi)->nr_discards -= entry->len;
657 kmem_cache_free(discard_entry_slab, entry);
658 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900659}
660
Chao Yu184a5cd2014-09-04 18:13:01 +0800661static bool __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900662{
663 struct sit_info *sit_i = SIT_I(sbi);
Chao Yu184a5cd2014-09-04 18:13:01 +0800664
665 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900666 sit_i->dirty_sentries++;
Chao Yu184a5cd2014-09-04 18:13:01 +0800667 return false;
668 }
669
670 return true;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900671}
672
673static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
674 unsigned int segno, int modified)
675{
676 struct seg_entry *se = get_seg_entry(sbi, segno);
677 se->type = type;
678 if (modified)
679 __mark_sit_entry_dirty(sbi, segno);
680}
681
682static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
683{
684 struct seg_entry *se;
685 unsigned int segno, offset;
686 long int new_vblocks;
687
688 segno = GET_SEGNO(sbi, blkaddr);
689
690 se = get_seg_entry(sbi, segno);
691 new_vblocks = se->valid_blocks + del;
Jaegeuk Kim491c0852014-02-04 13:01:10 +0900692 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900693
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700694 f2fs_bug_on(sbi, (new_vblocks >> (sizeof(unsigned short) << 3) ||
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900695 (new_vblocks > sbi->blocks_per_seg)));
696
697 se->valid_blocks = new_vblocks;
698 se->mtime = get_mtime(sbi);
699 SIT_I(sbi)->max_mtime = se->mtime;
700
701 /* Update valid block bitmap */
702 if (del > 0) {
Gu Zheng52aca072014-10-20 17:45:51 +0800703 if (f2fs_test_and_set_bit(offset, se->cur_valid_map))
Jaegeuk Kim05796762014-09-02 16:05:00 -0700704 f2fs_bug_on(sbi, 1);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700705 if (!f2fs_test_and_set_bit(offset, se->discard_map))
706 sbi->discard_blks--;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900707 } else {
Gu Zheng52aca072014-10-20 17:45:51 +0800708 if (!f2fs_test_and_clear_bit(offset, se->cur_valid_map))
Jaegeuk Kim05796762014-09-02 16:05:00 -0700709 f2fs_bug_on(sbi, 1);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700710 if (f2fs_test_and_clear_bit(offset, se->discard_map))
711 sbi->discard_blks++;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900712 }
713 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
714 se->ckpt_valid_blocks += del;
715
716 __mark_sit_entry_dirty(sbi, segno);
717
718 /* update total number of valid blocks to be written in ckpt area */
719 SIT_I(sbi)->written_valid_blocks += del;
720
721 if (sbi->segs_per_sec > 1)
722 get_sec_entry(sbi, segno)->valid_blocks += del;
723}
724
Jaegeuk Kim5e443812014-01-28 12:22:14 +0900725void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900726{
Jaegeuk Kim5e443812014-01-28 12:22:14 +0900727 update_sit_entry(sbi, new, 1);
728 if (GET_SEGNO(sbi, old) != NULL_SEGNO)
729 update_sit_entry(sbi, old, -1);
730
731 locate_dirty_segment(sbi, GET_SEGNO(sbi, old));
732 locate_dirty_segment(sbi, GET_SEGNO(sbi, new));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900733}
734
735void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
736{
737 unsigned int segno = GET_SEGNO(sbi, addr);
738 struct sit_info *sit_i = SIT_I(sbi);
739
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700740 f2fs_bug_on(sbi, addr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900741 if (addr == NEW_ADDR)
742 return;
743
744 /* add it into sit main buffer */
745 mutex_lock(&sit_i->sentry_lock);
746
747 update_sit_entry(sbi, addr, -1);
748
749 /* add it into dirty seglist */
750 locate_dirty_segment(sbi, segno);
751
752 mutex_unlock(&sit_i->sentry_lock);
753}
754
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700755bool is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr)
756{
757 struct sit_info *sit_i = SIT_I(sbi);
758 unsigned int segno, offset;
759 struct seg_entry *se;
760 bool is_cp = false;
761
762 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR)
763 return true;
764
765 mutex_lock(&sit_i->sentry_lock);
766
767 segno = GET_SEGNO(sbi, blkaddr);
768 se = get_seg_entry(sbi, segno);
769 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
770
771 if (f2fs_test_bit(offset, se->ckpt_valid_map))
772 is_cp = true;
773
774 mutex_unlock(&sit_i->sentry_lock);
775
776 return is_cp;
777}
778
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900779/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900780 * This function should be resided under the curseg_mutex lock
781 */
782static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
Haicheng Lie79efe32013-06-13 16:59:27 +0800783 struct f2fs_summary *sum)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900784{
785 struct curseg_info *curseg = CURSEG_I(sbi, type);
786 void *addr = curseg->sum_blk;
Haicheng Lie79efe32013-06-13 16:59:27 +0800787 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900788 memcpy(addr, sum, sizeof(struct f2fs_summary));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900789}
790
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900791/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900792 * Calculate the number of current summary pages for writing
793 */
Chao Yu3fa06d72014-12-09 14:21:46 +0800794int npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900795{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900796 int valid_sum_count = 0;
Fan Li9a479382013-10-29 16:21:47 +0800797 int i, sum_in_page;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900798
799 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
800 if (sbi->ckpt->alloc_type[i] == SSR)
801 valid_sum_count += sbi->blocks_per_seg;
Chao Yu3fa06d72014-12-09 14:21:46 +0800802 else {
803 if (for_ra)
804 valid_sum_count += le16_to_cpu(
805 F2FS_CKPT(sbi)->cur_data_blkoff[i]);
806 else
807 valid_sum_count += curseg_blkoff(sbi, i);
808 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900809 }
810
Fan Li9a479382013-10-29 16:21:47 +0800811 sum_in_page = (PAGE_CACHE_SIZE - 2 * SUM_JOURNAL_SIZE -
812 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
813 if (valid_sum_count <= sum_in_page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900814 return 1;
Fan Li9a479382013-10-29 16:21:47 +0800815 else if ((valid_sum_count - sum_in_page) <=
816 (PAGE_CACHE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900817 return 2;
818 return 3;
819}
820
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900821/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900822 * Caller should put this summary page
823 */
824struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
825{
826 return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
827}
828
Chao Yu381722d2015-05-19 17:40:04 +0800829void update_meta_page(struct f2fs_sb_info *sbi, void *src, block_t blk_addr)
830{
831 struct page *page = grab_meta_page(sbi, blk_addr);
832 void *dst = page_address(page);
833
834 if (src)
835 memcpy(dst, src, PAGE_CACHE_SIZE);
836 else
837 memset(dst, 0, PAGE_CACHE_SIZE);
838 set_page_dirty(page);
839 f2fs_put_page(page, 1);
840}
841
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900842static void write_sum_page(struct f2fs_sb_info *sbi,
843 struct f2fs_summary_block *sum_blk, block_t blk_addr)
844{
Chao Yu381722d2015-05-19 17:40:04 +0800845 update_meta_page(sbi, (void *)sum_blk, blk_addr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900846}
847
Jaegeuk Kim60374682013-03-31 13:58:51 +0900848static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
849{
850 struct curseg_info *curseg = CURSEG_I(sbi, type);
Haicheng Li81fb5e82013-05-14 18:20:28 +0800851 unsigned int segno = curseg->segno + 1;
Jaegeuk Kim60374682013-03-31 13:58:51 +0900852 struct free_segmap_info *free_i = FREE_I(sbi);
853
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700854 if (segno < MAIN_SEGS(sbi) && segno % sbi->segs_per_sec)
Haicheng Li81fb5e82013-05-14 18:20:28 +0800855 return !test_bit(segno, free_i->free_segmap);
Jaegeuk Kim60374682013-03-31 13:58:51 +0900856 return 0;
857}
858
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900859/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900860 * Find a new segment from the free segments bitmap to right order
861 * This function should be returned with success, otherwise BUG
862 */
863static void get_new_segment(struct f2fs_sb_info *sbi,
864 unsigned int *newseg, bool new_sec, int dir)
865{
866 struct free_segmap_info *free_i = FREE_I(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900867 unsigned int segno, secno, zoneno;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700868 unsigned int total_zones = MAIN_SECS(sbi) / sbi->secs_per_zone;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900869 unsigned int hint = *newseg / sbi->segs_per_sec;
870 unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
871 unsigned int left_start = hint;
872 bool init = true;
873 int go_left = 0;
874 int i;
875
Chao Yu1a118cc2015-02-11 18:20:38 +0800876 spin_lock(&free_i->segmap_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900877
878 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
879 segno = find_next_zero_bit(free_i->free_segmap,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700880 MAIN_SEGS(sbi), *newseg + 1);
Jaegeuk Kim33afa7f2013-03-31 12:59:53 +0900881 if (segno - *newseg < sbi->segs_per_sec -
882 (*newseg % sbi->segs_per_sec))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900883 goto got_it;
884 }
885find_other_zone:
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700886 secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint);
887 if (secno >= MAIN_SECS(sbi)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900888 if (dir == ALLOC_RIGHT) {
889 secno = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700890 MAIN_SECS(sbi), 0);
891 f2fs_bug_on(sbi, secno >= MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900892 } else {
893 go_left = 1;
894 left_start = hint - 1;
895 }
896 }
897 if (go_left == 0)
898 goto skip_left;
899
900 while (test_bit(left_start, free_i->free_secmap)) {
901 if (left_start > 0) {
902 left_start--;
903 continue;
904 }
905 left_start = 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, left_start >= MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900908 break;
909 }
910 secno = left_start;
911skip_left:
912 hint = secno;
913 segno = secno * sbi->segs_per_sec;
914 zoneno = secno / sbi->secs_per_zone;
915
916 /* give up on finding another zone */
917 if (!init)
918 goto got_it;
919 if (sbi->secs_per_zone == 1)
920 goto got_it;
921 if (zoneno == old_zoneno)
922 goto got_it;
923 if (dir == ALLOC_LEFT) {
924 if (!go_left && zoneno + 1 >= total_zones)
925 goto got_it;
926 if (go_left && zoneno == 0)
927 goto got_it;
928 }
929 for (i = 0; i < NR_CURSEG_TYPE; i++)
930 if (CURSEG_I(sbi, i)->zone == zoneno)
931 break;
932
933 if (i < NR_CURSEG_TYPE) {
934 /* zone is in user, try another */
935 if (go_left)
936 hint = zoneno * sbi->secs_per_zone - 1;
937 else if (zoneno + 1 >= total_zones)
938 hint = 0;
939 else
940 hint = (zoneno + 1) * sbi->secs_per_zone;
941 init = false;
942 goto find_other_zone;
943 }
944got_it:
945 /* set it as dirty segment in free segmap */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700946 f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900947 __set_inuse(sbi, segno);
948 *newseg = segno;
Chao Yu1a118cc2015-02-11 18:20:38 +0800949 spin_unlock(&free_i->segmap_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900950}
951
952static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
953{
954 struct curseg_info *curseg = CURSEG_I(sbi, type);
955 struct summary_footer *sum_footer;
956
957 curseg->segno = curseg->next_segno;
958 curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
959 curseg->next_blkoff = 0;
960 curseg->next_segno = NULL_SEGNO;
961
962 sum_footer = &(curseg->sum_blk->footer);
963 memset(sum_footer, 0, sizeof(struct summary_footer));
964 if (IS_DATASEG(type))
965 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
966 if (IS_NODESEG(type))
967 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
968 __set_sit_entry_type(sbi, type, curseg->segno, modified);
969}
970
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900971/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900972 * Allocate a current working segment.
973 * This function always allocates a free segment in LFS manner.
974 */
975static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
976{
977 struct curseg_info *curseg = CURSEG_I(sbi, type);
978 unsigned int segno = curseg->segno;
979 int dir = ALLOC_LEFT;
980
981 write_sum_page(sbi, curseg->sum_blk,
Haicheng Li81fb5e82013-05-14 18:20:28 +0800982 GET_SUM_BLOCK(sbi, segno));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900983 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
984 dir = ALLOC_RIGHT;
985
986 if (test_opt(sbi, NOHEAP))
987 dir = ALLOC_RIGHT;
988
989 get_new_segment(sbi, &segno, new_sec, dir);
990 curseg->next_segno = segno;
991 reset_curseg(sbi, type, 1);
992 curseg->alloc_type = LFS;
993}
994
995static void __next_free_blkoff(struct f2fs_sb_info *sbi,
996 struct curseg_info *seg, block_t start)
997{
998 struct seg_entry *se = get_seg_entry(sbi, seg->segno);
Changman Leee81c93c2013-11-15 13:21:16 +0900999 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
Jaegeuk Kim60a3b782015-02-10 16:44:29 -08001000 unsigned long *target_map = SIT_I(sbi)->tmp_map;
Changman Leee81c93c2013-11-15 13:21:16 +09001001 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
1002 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
1003 int i, pos;
1004
1005 for (i = 0; i < entries; i++)
1006 target_map[i] = ckpt_map[i] | cur_map[i];
1007
1008 pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
1009
1010 seg->next_blkoff = pos;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001011}
1012
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001013/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001014 * If a segment is written by LFS manner, next block offset is just obtained
1015 * by increasing the current block offset. However, if a segment is written by
1016 * SSR manner, next block offset obtained by calling __next_free_blkoff
1017 */
1018static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
1019 struct curseg_info *seg)
1020{
1021 if (seg->alloc_type == SSR)
1022 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
1023 else
1024 seg->next_blkoff++;
1025}
1026
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001027/*
arter97e1c42042014-08-06 23:22:50 +09001028 * This function always allocates a used segment(from dirty seglist) by SSR
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001029 * manner, so it should recover the existing segment information of valid blocks
1030 */
1031static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
1032{
1033 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1034 struct curseg_info *curseg = CURSEG_I(sbi, type);
1035 unsigned int new_segno = curseg->next_segno;
1036 struct f2fs_summary_block *sum_node;
1037 struct page *sum_page;
1038
1039 write_sum_page(sbi, curseg->sum_blk,
1040 GET_SUM_BLOCK(sbi, curseg->segno));
1041 __set_test_and_inuse(sbi, new_segno);
1042
1043 mutex_lock(&dirty_i->seglist_lock);
1044 __remove_dirty_segment(sbi, new_segno, PRE);
1045 __remove_dirty_segment(sbi, new_segno, DIRTY);
1046 mutex_unlock(&dirty_i->seglist_lock);
1047
1048 reset_curseg(sbi, type, 1);
1049 curseg->alloc_type = SSR;
1050 __next_free_blkoff(sbi, curseg, 0);
1051
1052 if (reuse) {
1053 sum_page = get_sum_page(sbi, new_segno);
1054 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
1055 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
1056 f2fs_put_page(sum_page, 1);
1057 }
1058}
1059
Jaegeuk Kim43727522013-02-04 15:11:17 +09001060static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
1061{
1062 struct curseg_info *curseg = CURSEG_I(sbi, type);
1063 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
1064
1065 if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0))
1066 return v_ops->get_victim(sbi,
1067 &(curseg)->next_segno, BG_GC, type, SSR);
1068
1069 /* For data segments, let's do SSR more intensively */
1070 for (; type >= CURSEG_HOT_DATA; type--)
1071 if (v_ops->get_victim(sbi, &(curseg)->next_segno,
1072 BG_GC, type, SSR))
1073 return 1;
1074 return 0;
1075}
1076
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001077/*
1078 * flush out current segment and replace it with new segment
1079 * This function should be returned with success, otherwise BUG
1080 */
1081static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
1082 int type, bool force)
1083{
1084 struct curseg_info *curseg = CURSEG_I(sbi, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001085
Gu Zheng7b405272013-08-19 09:41:15 +08001086 if (force)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001087 new_curseg(sbi, type, true);
Gu Zheng7b405272013-08-19 09:41:15 +08001088 else if (type == CURSEG_WARM_NODE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001089 new_curseg(sbi, type, false);
Jaegeuk Kim60374682013-03-31 13:58:51 +09001090 else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
1091 new_curseg(sbi, type, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001092 else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
1093 change_curseg(sbi, type, true);
1094 else
1095 new_curseg(sbi, type, false);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +09001096
1097 stat_inc_seg_type(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001098}
1099
Jaegeuk Kim38aa0882015-01-05 16:02:20 -08001100static void __allocate_new_segments(struct f2fs_sb_info *sbi, int type)
1101{
1102 struct curseg_info *curseg = CURSEG_I(sbi, type);
1103 unsigned int old_segno;
1104
1105 old_segno = curseg->segno;
1106 SIT_I(sbi)->s_ops->allocate_segment(sbi, type, true);
1107 locate_dirty_segment(sbi, old_segno);
1108}
1109
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001110void allocate_new_segments(struct f2fs_sb_info *sbi)
1111{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001112 int i;
1113
Jaegeuk Kim38aa0882015-01-05 16:02:20 -08001114 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++)
1115 __allocate_new_segments(sbi, i);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001116}
1117
1118static const struct segment_allocation default_salloc_ops = {
1119 .allocate_segment = allocate_segment_by_default,
1120};
1121
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001122int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range)
1123{
Jaegeuk Kimf7ef9b82015-02-09 12:02:44 -08001124 __u64 start = F2FS_BYTES_TO_BLK(range->start);
1125 __u64 end = start + F2FS_BYTES_TO_BLK(range->len) - 1;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001126 unsigned int start_segno, end_segno;
1127 struct cp_control cpc;
1128
Jaegeuk Kim836b5a62015-04-30 22:50:06 -07001129 if (start >= MAX_BLKADDR(sbi) || range->len < sbi->blocksize)
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001130 return -EINVAL;
1131
Jan Kara9bd27ae2014-10-21 14:07:33 +02001132 cpc.trimmed = 0;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001133 if (end <= MAIN_BLKADDR(sbi))
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001134 goto out;
1135
1136 /* start/end segment number in main_area */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001137 start_segno = (start <= MAIN_BLKADDR(sbi)) ? 0 : GET_SEGNO(sbi, start);
1138 end_segno = (end >= MAX_BLKADDR(sbi)) ? MAIN_SEGS(sbi) - 1 :
1139 GET_SEGNO(sbi, end);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001140 cpc.reason = CP_DISCARD;
Jaegeuk Kim836b5a62015-04-30 22:50:06 -07001141 cpc.trim_minlen = max_t(__u64, 1, F2FS_BYTES_TO_BLK(range->minlen));
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001142
1143 /* do checkpoint to issue discard commands safely */
Jaegeuk Kimbba681c2015-01-26 17:41:23 -08001144 for (; start_segno <= end_segno; start_segno = cpc.trim_end + 1) {
1145 cpc.trim_start = start_segno;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001146
1147 if (sbi->discard_blks == 0)
1148 break;
1149 else if (sbi->discard_blks < BATCHED_TRIM_BLOCKS(sbi))
1150 cpc.trim_end = end_segno;
1151 else
1152 cpc.trim_end = min_t(unsigned int,
1153 rounddown(start_segno +
Jaegeuk Kimbba681c2015-01-26 17:41:23 -08001154 BATCHED_TRIM_SEGMENTS(sbi),
1155 sbi->segs_per_sec) - 1, end_segno);
1156
1157 mutex_lock(&sbi->gc_mutex);
1158 write_checkpoint(sbi, &cpc);
1159 mutex_unlock(&sbi->gc_mutex);
1160 }
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001161out:
Jaegeuk Kimf7ef9b82015-02-09 12:02:44 -08001162 range->len = F2FS_BLK_TO_BYTES(cpc.trimmed);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001163 return 0;
1164}
1165
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001166static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
1167{
1168 struct curseg_info *curseg = CURSEG_I(sbi, type);
1169 if (curseg->next_blkoff < sbi->blocks_per_seg)
1170 return true;
1171 return false;
1172}
1173
1174static int __get_segment_type_2(struct page *page, enum page_type p_type)
1175{
1176 if (p_type == DATA)
1177 return CURSEG_HOT_DATA;
1178 else
1179 return CURSEG_HOT_NODE;
1180}
1181
1182static int __get_segment_type_4(struct page *page, enum page_type p_type)
1183{
1184 if (p_type == DATA) {
1185 struct inode *inode = page->mapping->host;
1186
1187 if (S_ISDIR(inode->i_mode))
1188 return CURSEG_HOT_DATA;
1189 else
1190 return CURSEG_COLD_DATA;
1191 } else {
Jaegeuk Kima344b9f2014-11-05 20:05:53 -08001192 if (IS_DNODE(page) && is_cold_node(page))
1193 return CURSEG_WARM_NODE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001194 else
1195 return CURSEG_COLD_NODE;
1196 }
1197}
1198
1199static int __get_segment_type_6(struct page *page, enum page_type p_type)
1200{
1201 if (p_type == DATA) {
1202 struct inode *inode = page->mapping->host;
1203
1204 if (S_ISDIR(inode->i_mode))
1205 return CURSEG_HOT_DATA;
Jaegeuk Kim354a3392013-06-14 08:52:35 +09001206 else if (is_cold_data(page) || file_is_cold(inode))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001207 return CURSEG_COLD_DATA;
1208 else
1209 return CURSEG_WARM_DATA;
1210 } else {
1211 if (IS_DNODE(page))
1212 return is_cold_node(page) ? CURSEG_WARM_NODE :
1213 CURSEG_HOT_NODE;
1214 else
1215 return CURSEG_COLD_NODE;
1216 }
1217}
1218
1219static int __get_segment_type(struct page *page, enum page_type p_type)
1220{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001221 switch (F2FS_P_SB(page)->active_logs) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001222 case 2:
1223 return __get_segment_type_2(page, p_type);
1224 case 4:
1225 return __get_segment_type_4(page, p_type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001226 }
Jaegeuk Kim12a67142012-12-21 11:47:05 +09001227 /* NR_CURSEG_TYPE(6) logs by default */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001228 f2fs_bug_on(F2FS_P_SB(page),
1229 F2FS_P_SB(page)->active_logs != NR_CURSEG_TYPE);
Jaegeuk Kim12a67142012-12-21 11:47:05 +09001230 return __get_segment_type_6(page, p_type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001231}
1232
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001233void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
1234 block_t old_blkaddr, block_t *new_blkaddr,
1235 struct f2fs_summary *sum, int type)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001236{
1237 struct sit_info *sit_i = SIT_I(sbi);
1238 struct curseg_info *curseg;
Jaegeuk Kim38aa0882015-01-05 16:02:20 -08001239 bool direct_io = (type == CURSEG_DIRECT_IO);
1240
1241 type = direct_io ? CURSEG_WARM_DATA : type;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001242
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001243 curseg = CURSEG_I(sbi, type);
1244
1245 mutex_lock(&curseg->curseg_mutex);
Jaegeuk Kim21cb1d92015-03-11 13:42:48 -04001246 mutex_lock(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001247
Jaegeuk Kim38aa0882015-01-05 16:02:20 -08001248 /* direct_io'ed data is aligned to the segment for better performance */
Jaegeuk Kim47e70ca2015-08-11 10:17:27 -07001249 if (direct_io && curseg->next_blkoff &&
1250 !has_not_enough_free_secs(sbi, 0))
Jaegeuk Kim38aa0882015-01-05 16:02:20 -08001251 __allocate_new_segments(sbi, type);
1252
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001253 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001254
1255 /*
1256 * __add_sum_entry should be resided under the curseg_mutex
1257 * because, this function updates a summary entry in the
1258 * current summary block.
1259 */
Haicheng Lie79efe32013-06-13 16:59:27 +08001260 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001261
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001262 __refresh_next_blkoff(sbi, curseg);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +09001263
1264 stat_inc_block_count(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001265
Jaegeuk Kim5e443812014-01-28 12:22:14 +09001266 if (!__has_curseg_space(sbi, type))
1267 sit_i->s_ops->allocate_segment(sbi, type, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001268 /*
1269 * SIT information should be updated before segment allocation,
1270 * since SSR needs latest valid block information.
1271 */
1272 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
Jaegeuk Kim5e443812014-01-28 12:22:14 +09001273
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001274 mutex_unlock(&sit_i->sentry_lock);
1275
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001276 if (page && IS_NODESEG(type))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001277 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
1278
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001279 mutex_unlock(&curseg->curseg_mutex);
1280}
1281
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001282static void do_write_page(struct f2fs_summary *sum, struct f2fs_io_info *fio)
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001283{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001284 int type = __get_segment_type(fio->page, fio->type);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001285
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001286 allocate_data_block(fio->sbi, fio->page, fio->blk_addr,
1287 &fio->blk_addr, sum, type);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001288
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001289 /* writeout dirty page into bdev */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001290 f2fs_submit_page_mbio(fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001291}
1292
Jaegeuk Kim577e3492013-01-24 19:56:11 +09001293void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001294{
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001295 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001296 .sbi = sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001297 .type = META,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001298 .rw = WRITE_SYNC | REQ_META | REQ_PRIO,
1299 .blk_addr = page->index,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001300 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001301 .encrypted_page = NULL,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001302 };
1303
Chao Yu2b947002015-10-12 17:04:21 +08001304 if (unlikely(page->index >= MAIN_BLKADDR(sbi)))
1305 fio.rw &= ~REQ_META;
1306
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001307 set_page_writeback(page);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001308 f2fs_submit_page_mbio(&fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001309}
1310
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001311void write_node_page(unsigned int nid, struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001312{
1313 struct f2fs_summary sum;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001314
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001315 set_summary(&sum, nid, 0, 0);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001316 do_write_page(&sum, fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001317}
1318
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001319void write_data_page(struct dnode_of_data *dn, struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001320{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001321 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001322 struct f2fs_summary sum;
1323 struct node_info ni;
1324
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001325 f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001326 get_node_info(sbi, dn->nid, &ni);
1327 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001328 do_write_page(&sum, fio);
Jaegeuk Kime1509cf2014-12-30 22:57:55 -08001329 dn->data_blkaddr = fio->blk_addr;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001330}
1331
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001332void rewrite_data_page(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001333{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001334 stat_inc_inplace_blocks(fio->sbi);
1335 f2fs_submit_page_mbio(fio);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001336}
1337
Chao Yu528e3452015-05-28 19:15:35 +08001338static void __f2fs_replace_block(struct f2fs_sb_info *sbi,
1339 struct f2fs_summary *sum,
Chao Yu19f106b2015-05-06 13:08:06 +08001340 block_t old_blkaddr, block_t new_blkaddr,
1341 bool recover_curseg)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001342{
1343 struct sit_info *sit_i = SIT_I(sbi);
1344 struct curseg_info *curseg;
1345 unsigned int segno, old_cursegno;
1346 struct seg_entry *se;
1347 int type;
Chao Yu19f106b2015-05-06 13:08:06 +08001348 unsigned short old_blkoff;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001349
1350 segno = GET_SEGNO(sbi, new_blkaddr);
1351 se = get_seg_entry(sbi, segno);
1352 type = se->type;
1353
Chao Yu19f106b2015-05-06 13:08:06 +08001354 if (!recover_curseg) {
1355 /* for recovery flow */
1356 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
1357 if (old_blkaddr == NULL_ADDR)
1358 type = CURSEG_COLD_DATA;
1359 else
1360 type = CURSEG_WARM_DATA;
1361 }
1362 } else {
1363 if (!IS_CURSEG(sbi, segno))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001364 type = CURSEG_WARM_DATA;
1365 }
Chao Yu19f106b2015-05-06 13:08:06 +08001366
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001367 curseg = CURSEG_I(sbi, type);
1368
1369 mutex_lock(&curseg->curseg_mutex);
1370 mutex_lock(&sit_i->sentry_lock);
1371
1372 old_cursegno = curseg->segno;
Chao Yu19f106b2015-05-06 13:08:06 +08001373 old_blkoff = curseg->next_blkoff;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001374
1375 /* change the current segment */
1376 if (segno != curseg->segno) {
1377 curseg->next_segno = segno;
1378 change_curseg(sbi, type, true);
1379 }
1380
Jaegeuk Kim491c0852014-02-04 13:01:10 +09001381 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
Haicheng Lie79efe32013-06-13 16:59:27 +08001382 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001383
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001384 if (!recover_curseg)
1385 update_sit_entry(sbi, new_blkaddr, 1);
1386 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
1387 update_sit_entry(sbi, old_blkaddr, -1);
1388
1389 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
1390 locate_dirty_segment(sbi, GET_SEGNO(sbi, new_blkaddr));
1391
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001392 locate_dirty_segment(sbi, old_cursegno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001393
Chao Yu19f106b2015-05-06 13:08:06 +08001394 if (recover_curseg) {
1395 if (old_cursegno != curseg->segno) {
1396 curseg->next_segno = old_cursegno;
1397 change_curseg(sbi, type, true);
1398 }
1399 curseg->next_blkoff = old_blkoff;
1400 }
1401
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001402 mutex_unlock(&sit_i->sentry_lock);
1403 mutex_unlock(&curseg->curseg_mutex);
1404}
1405
Chao Yu528e3452015-05-28 19:15:35 +08001406void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
1407 block_t old_addr, block_t new_addr,
1408 unsigned char version, bool recover_curseg)
1409{
1410 struct f2fs_summary sum;
1411
1412 set_summary(&sum, dn->nid, dn->ofs_in_node, version);
1413
1414 __f2fs_replace_block(sbi, &sum, old_addr, new_addr, recover_curseg);
1415
1416 dn->data_blkaddr = new_addr;
1417 set_data_blkaddr(dn);
1418 f2fs_update_extent_cache(dn);
1419}
1420
Chao Yudf0f8dc2014-03-22 14:57:23 +08001421static inline bool is_merged_page(struct f2fs_sb_info *sbi,
1422 struct page *page, enum page_type type)
1423{
1424 enum page_type btype = PAGE_TYPE_OF_BIO(type);
1425 struct f2fs_bio_info *io = &sbi->write_io[btype];
Chao Yudf0f8dc2014-03-22 14:57:23 +08001426 struct bio_vec *bvec;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001427 struct page *target;
Chao Yudf0f8dc2014-03-22 14:57:23 +08001428 int i;
1429
1430 down_read(&io->io_rwsem);
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001431 if (!io->bio) {
1432 up_read(&io->io_rwsem);
1433 return false;
1434 }
Chao Yudf0f8dc2014-03-22 14:57:23 +08001435
Jaegeuk Kimce234472014-04-02 09:04:42 +09001436 bio_for_each_segment_all(bvec, io->bio, i) {
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001437
1438 if (bvec->bv_page->mapping) {
1439 target = bvec->bv_page;
1440 } else {
1441 struct f2fs_crypto_ctx *ctx;
1442
1443 /* encrypted page */
1444 ctx = (struct f2fs_crypto_ctx *)page_private(
1445 bvec->bv_page);
Jaegeuk Kimca40b032015-05-12 13:40:20 -07001446 target = ctx->w.control_page;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001447 }
1448
1449 if (page == target) {
Chao Yudf0f8dc2014-03-22 14:57:23 +08001450 up_read(&io->io_rwsem);
1451 return true;
1452 }
1453 }
1454
Chao Yudf0f8dc2014-03-22 14:57:23 +08001455 up_read(&io->io_rwsem);
1456 return false;
1457}
1458
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001459void f2fs_wait_on_page_writeback(struct page *page,
Yuan Zhong5514f0a2014-01-10 07:26:14 +00001460 enum page_type type)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001461{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001462 if (PageWriteback(page)) {
Jaegeuk Kim40813632014-09-02 15:31:18 -07001463 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
1464
Chao Yudf0f8dc2014-03-22 14:57:23 +08001465 if (is_merged_page(sbi, page, type))
1466 f2fs_submit_merged_bio(sbi, type, WRITE);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001467 wait_on_page_writeback(page);
1468 }
1469}
1470
Chao Yu08b39fb2015-10-08 13:27:34 +08001471void f2fs_wait_on_encrypted_page_writeback(struct f2fs_sb_info *sbi,
1472 block_t blkaddr)
1473{
1474 struct page *cpage;
1475
1476 if (blkaddr == NEW_ADDR)
1477 return;
1478
1479 f2fs_bug_on(sbi, blkaddr == NULL_ADDR);
1480
1481 cpage = find_lock_page(META_MAPPING(sbi), blkaddr);
1482 if (cpage) {
1483 f2fs_wait_on_page_writeback(cpage, DATA);
1484 f2fs_put_page(cpage, 1);
1485 }
1486}
1487
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001488static int read_compacted_summaries(struct f2fs_sb_info *sbi)
1489{
1490 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1491 struct curseg_info *seg_i;
1492 unsigned char *kaddr;
1493 struct page *page;
1494 block_t start;
1495 int i, j, offset;
1496
1497 start = start_sum_block(sbi);
1498
1499 page = get_meta_page(sbi, start++);
1500 kaddr = (unsigned char *)page_address(page);
1501
1502 /* Step 1: restore nat cache */
1503 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1504 memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
1505
1506 /* Step 2: restore sit cache */
1507 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1508 memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
1509 SUM_JOURNAL_SIZE);
1510 offset = 2 * SUM_JOURNAL_SIZE;
1511
1512 /* Step 3: restore summary entries */
1513 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1514 unsigned short blk_off;
1515 unsigned int segno;
1516
1517 seg_i = CURSEG_I(sbi, i);
1518 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
1519 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
1520 seg_i->next_segno = segno;
1521 reset_curseg(sbi, i, 0);
1522 seg_i->alloc_type = ckpt->alloc_type[i];
1523 seg_i->next_blkoff = blk_off;
1524
1525 if (seg_i->alloc_type == SSR)
1526 blk_off = sbi->blocks_per_seg;
1527
1528 for (j = 0; j < blk_off; j++) {
1529 struct f2fs_summary *s;
1530 s = (struct f2fs_summary *)(kaddr + offset);
1531 seg_i->sum_blk->entries[j] = *s;
1532 offset += SUMMARY_SIZE;
1533 if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1534 SUM_FOOTER_SIZE)
1535 continue;
1536
1537 f2fs_put_page(page, 1);
1538 page = NULL;
1539
1540 page = get_meta_page(sbi, start++);
1541 kaddr = (unsigned char *)page_address(page);
1542 offset = 0;
1543 }
1544 }
1545 f2fs_put_page(page, 1);
1546 return 0;
1547}
1548
1549static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
1550{
1551 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1552 struct f2fs_summary_block *sum;
1553 struct curseg_info *curseg;
1554 struct page *new;
1555 unsigned short blk_off;
1556 unsigned int segno = 0;
1557 block_t blk_addr = 0;
1558
1559 /* get segment number and block addr */
1560 if (IS_DATASEG(type)) {
1561 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
1562 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
1563 CURSEG_HOT_DATA]);
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001564 if (__exist_node_summaries(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001565 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
1566 else
1567 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
1568 } else {
1569 segno = le32_to_cpu(ckpt->cur_node_segno[type -
1570 CURSEG_HOT_NODE]);
1571 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
1572 CURSEG_HOT_NODE]);
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001573 if (__exist_node_summaries(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001574 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
1575 type - CURSEG_HOT_NODE);
1576 else
1577 blk_addr = GET_SUM_BLOCK(sbi, segno);
1578 }
1579
1580 new = get_meta_page(sbi, blk_addr);
1581 sum = (struct f2fs_summary_block *)page_address(new);
1582
1583 if (IS_NODESEG(type)) {
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001584 if (__exist_node_summaries(sbi)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001585 struct f2fs_summary *ns = &sum->entries[0];
1586 int i;
1587 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
1588 ns->version = 0;
1589 ns->ofs_in_node = 0;
1590 }
1591 } else {
Gu Zhengd6537882014-03-07 18:43:36 +08001592 int err;
1593
1594 err = restore_node_summary(sbi, segno, sum);
1595 if (err) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001596 f2fs_put_page(new, 1);
Gu Zhengd6537882014-03-07 18:43:36 +08001597 return err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001598 }
1599 }
1600 }
1601
1602 /* set uncompleted segment to curseg */
1603 curseg = CURSEG_I(sbi, type);
1604 mutex_lock(&curseg->curseg_mutex);
1605 memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
1606 curseg->next_segno = segno;
1607 reset_curseg(sbi, type, 0);
1608 curseg->alloc_type = ckpt->alloc_type[type];
1609 curseg->next_blkoff = blk_off;
1610 mutex_unlock(&curseg->curseg_mutex);
1611 f2fs_put_page(new, 1);
1612 return 0;
1613}
1614
1615static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
1616{
1617 int type = CURSEG_HOT_DATA;
Chao Yue4fc5fb2014-03-17 16:36:24 +08001618 int err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001619
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001620 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
Chao Yu3fa06d72014-12-09 14:21:46 +08001621 int npages = npages_for_summary_flush(sbi, true);
1622
1623 if (npages >= 2)
1624 ra_meta_pages(sbi, start_sum_block(sbi), npages,
Chao Yu26879fb2015-10-12 17:05:59 +08001625 META_CP, true);
Chao Yu3fa06d72014-12-09 14:21:46 +08001626
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001627 /* restore for compacted data summary */
1628 if (read_compacted_summaries(sbi))
1629 return -EINVAL;
1630 type = CURSEG_HOT_NODE;
1631 }
1632
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001633 if (__exist_node_summaries(sbi))
Chao Yu3fa06d72014-12-09 14:21:46 +08001634 ra_meta_pages(sbi, sum_blk_addr(sbi, NR_CURSEG_TYPE, type),
Chao Yu26879fb2015-10-12 17:05:59 +08001635 NR_CURSEG_TYPE - type, META_CP, true);
Chao Yu3fa06d72014-12-09 14:21:46 +08001636
Chao Yue4fc5fb2014-03-17 16:36:24 +08001637 for (; type <= CURSEG_COLD_NODE; type++) {
1638 err = read_normal_summaries(sbi, type);
1639 if (err)
1640 return err;
1641 }
1642
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001643 return 0;
1644}
1645
1646static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
1647{
1648 struct page *page;
1649 unsigned char *kaddr;
1650 struct f2fs_summary *summary;
1651 struct curseg_info *seg_i;
1652 int written_size = 0;
1653 int i, j;
1654
1655 page = grab_meta_page(sbi, blkaddr++);
1656 kaddr = (unsigned char *)page_address(page);
1657
1658 /* Step 1: write nat cache */
1659 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1660 memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
1661 written_size += SUM_JOURNAL_SIZE;
1662
1663 /* Step 2: write sit cache */
1664 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1665 memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
1666 SUM_JOURNAL_SIZE);
1667 written_size += SUM_JOURNAL_SIZE;
1668
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001669 /* Step 3: write summary entries */
1670 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1671 unsigned short blkoff;
1672 seg_i = CURSEG_I(sbi, i);
1673 if (sbi->ckpt->alloc_type[i] == SSR)
1674 blkoff = sbi->blocks_per_seg;
1675 else
1676 blkoff = curseg_blkoff(sbi, i);
1677
1678 for (j = 0; j < blkoff; j++) {
1679 if (!page) {
1680 page = grab_meta_page(sbi, blkaddr++);
1681 kaddr = (unsigned char *)page_address(page);
1682 written_size = 0;
1683 }
1684 summary = (struct f2fs_summary *)(kaddr + written_size);
1685 *summary = seg_i->sum_blk->entries[j];
1686 written_size += SUMMARY_SIZE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001687
1688 if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1689 SUM_FOOTER_SIZE)
1690 continue;
1691
Chao Yue8d61a72013-10-24 15:08:28 +08001692 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001693 f2fs_put_page(page, 1);
1694 page = NULL;
1695 }
1696 }
Chao Yue8d61a72013-10-24 15:08:28 +08001697 if (page) {
1698 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001699 f2fs_put_page(page, 1);
Chao Yue8d61a72013-10-24 15:08:28 +08001700 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001701}
1702
1703static void write_normal_summaries(struct f2fs_sb_info *sbi,
1704 block_t blkaddr, int type)
1705{
1706 int i, end;
1707 if (IS_DATASEG(type))
1708 end = type + NR_CURSEG_DATA_TYPE;
1709 else
1710 end = type + NR_CURSEG_NODE_TYPE;
1711
1712 for (i = type; i < end; i++) {
1713 struct curseg_info *sum = CURSEG_I(sbi, i);
1714 mutex_lock(&sum->curseg_mutex);
1715 write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
1716 mutex_unlock(&sum->curseg_mutex);
1717 }
1718}
1719
1720void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1721{
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001722 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001723 write_compacted_summaries(sbi, start_blk);
1724 else
1725 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
1726}
1727
1728void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1729{
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001730 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001731}
1732
1733int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
1734 unsigned int val, int alloc)
1735{
1736 int i;
1737
1738 if (type == NAT_JOURNAL) {
1739 for (i = 0; i < nats_in_cursum(sum); i++) {
1740 if (le32_to_cpu(nid_in_journal(sum, i)) == val)
1741 return i;
1742 }
1743 if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
1744 return update_nats_in_cursum(sum, 1);
1745 } else if (type == SIT_JOURNAL) {
1746 for (i = 0; i < sits_in_cursum(sum); i++)
1747 if (le32_to_cpu(segno_in_journal(sum, i)) == val)
1748 return i;
1749 if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
1750 return update_sits_in_cursum(sum, 1);
1751 }
1752 return -1;
1753}
1754
1755static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
1756 unsigned int segno)
1757{
Gu Zheng2cc22182014-10-20 17:45:49 +08001758 return get_meta_page(sbi, current_sit_addr(sbi, segno));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001759}
1760
1761static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
1762 unsigned int start)
1763{
1764 struct sit_info *sit_i = SIT_I(sbi);
1765 struct page *src_page, *dst_page;
1766 pgoff_t src_off, dst_off;
1767 void *src_addr, *dst_addr;
1768
1769 src_off = current_sit_addr(sbi, start);
1770 dst_off = next_sit_addr(sbi, src_off);
1771
1772 /* get current sit block page without lock */
1773 src_page = get_meta_page(sbi, src_off);
1774 dst_page = grab_meta_page(sbi, dst_off);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001775 f2fs_bug_on(sbi, PageDirty(src_page));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001776
1777 src_addr = page_address(src_page);
1778 dst_addr = page_address(dst_page);
1779 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
1780
1781 set_page_dirty(dst_page);
1782 f2fs_put_page(src_page, 1);
1783
1784 set_to_next_sit(sit_i, start);
1785
1786 return dst_page;
1787}
1788
Chao Yu184a5cd2014-09-04 18:13:01 +08001789static struct sit_entry_set *grab_sit_entry_set(void)
1790{
1791 struct sit_entry_set *ses =
Jaegeuk Kim80c54502015-08-20 08:51:56 -07001792 f2fs_kmem_cache_alloc(sit_entry_set_slab, GFP_NOFS);
Chao Yu184a5cd2014-09-04 18:13:01 +08001793
1794 ses->entry_cnt = 0;
1795 INIT_LIST_HEAD(&ses->set_list);
1796 return ses;
1797}
1798
1799static void release_sit_entry_set(struct sit_entry_set *ses)
1800{
1801 list_del(&ses->set_list);
1802 kmem_cache_free(sit_entry_set_slab, ses);
1803}
1804
1805static void adjust_sit_entry_set(struct sit_entry_set *ses,
1806 struct list_head *head)
1807{
1808 struct sit_entry_set *next = ses;
1809
1810 if (list_is_last(&ses->set_list, head))
1811 return;
1812
1813 list_for_each_entry_continue(next, head, set_list)
1814 if (ses->entry_cnt <= next->entry_cnt)
1815 break;
1816
1817 list_move_tail(&ses->set_list, &next->set_list);
1818}
1819
1820static void add_sit_entry(unsigned int segno, struct list_head *head)
1821{
1822 struct sit_entry_set *ses;
1823 unsigned int start_segno = START_SEGNO(segno);
1824
1825 list_for_each_entry(ses, head, set_list) {
1826 if (ses->start_segno == start_segno) {
1827 ses->entry_cnt++;
1828 adjust_sit_entry_set(ses, head);
1829 return;
1830 }
1831 }
1832
1833 ses = grab_sit_entry_set();
1834
1835 ses->start_segno = start_segno;
1836 ses->entry_cnt++;
1837 list_add(&ses->set_list, head);
1838}
1839
1840static void add_sits_in_set(struct f2fs_sb_info *sbi)
1841{
1842 struct f2fs_sm_info *sm_info = SM_I(sbi);
1843 struct list_head *set_list = &sm_info->sit_entry_set;
1844 unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap;
Chao Yu184a5cd2014-09-04 18:13:01 +08001845 unsigned int segno;
1846
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001847 for_each_set_bit(segno, bitmap, MAIN_SEGS(sbi))
Chao Yu184a5cd2014-09-04 18:13:01 +08001848 add_sit_entry(segno, set_list);
1849}
1850
1851static void remove_sits_in_journal(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001852{
1853 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1854 struct f2fs_summary_block *sum = curseg->sum_blk;
1855 int i;
1856
Chao Yu184a5cd2014-09-04 18:13:01 +08001857 for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
1858 unsigned int segno;
1859 bool dirtied;
1860
1861 segno = le32_to_cpu(segno_in_journal(sum, i));
1862 dirtied = __mark_sit_entry_dirty(sbi, segno);
1863
1864 if (!dirtied)
1865 add_sit_entry(segno, &SM_I(sbi)->sit_entry_set);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001866 }
Chao Yu184a5cd2014-09-04 18:13:01 +08001867 update_sits_in_cursum(sum, -sits_in_cursum(sum));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001868}
1869
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001870/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001871 * CP calls this function, which flushes SIT entries including sit_journal,
1872 * and moves prefree segs to free segs.
1873 */
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001874void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001875{
1876 struct sit_info *sit_i = SIT_I(sbi);
1877 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
1878 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1879 struct f2fs_summary_block *sum = curseg->sum_blk;
Chao Yu184a5cd2014-09-04 18:13:01 +08001880 struct sit_entry_set *ses, *tmp;
1881 struct list_head *head = &SM_I(sbi)->sit_entry_set;
Chao Yu184a5cd2014-09-04 18:13:01 +08001882 bool to_journal = true;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001883 struct seg_entry *se;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001884
1885 mutex_lock(&curseg->curseg_mutex);
1886 mutex_lock(&sit_i->sentry_lock);
1887
Wanpeng Li2b11a742015-02-27 16:52:50 +08001888 if (!sit_i->dirty_sentries)
1889 goto out;
1890
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001891 /*
Chao Yu184a5cd2014-09-04 18:13:01 +08001892 * add and account sit entries of dirty bitmap in sit entry
1893 * set temporarily
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001894 */
Chao Yu184a5cd2014-09-04 18:13:01 +08001895 add_sits_in_set(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001896
Chao Yu184a5cd2014-09-04 18:13:01 +08001897 /*
1898 * if there are no enough space in journal to store dirty sit
1899 * entries, remove all entries from journal and add and account
1900 * them in sit entry set.
1901 */
1902 if (!__has_cursum_space(sum, sit_i->dirty_sentries, SIT_JOURNAL))
1903 remove_sits_in_journal(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001904
Chao Yu184a5cd2014-09-04 18:13:01 +08001905 /*
1906 * there are two steps to flush sit entries:
1907 * #1, flush sit entries to journal in current cold data summary block.
1908 * #2, flush sit entries to sit page.
1909 */
1910 list_for_each_entry_safe(ses, tmp, head, set_list) {
Jaegeuk Kim4a257ed2014-10-16 11:43:30 -07001911 struct page *page = NULL;
Chao Yu184a5cd2014-09-04 18:13:01 +08001912 struct f2fs_sit_block *raw_sit = NULL;
1913 unsigned int start_segno = ses->start_segno;
1914 unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001915 (unsigned long)MAIN_SEGS(sbi));
Chao Yu184a5cd2014-09-04 18:13:01 +08001916 unsigned int segno = start_segno;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001917
Chao Yu184a5cd2014-09-04 18:13:01 +08001918 if (to_journal &&
1919 !__has_cursum_space(sum, ses->entry_cnt, SIT_JOURNAL))
1920 to_journal = false;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001921
Chao Yu184a5cd2014-09-04 18:13:01 +08001922 if (!to_journal) {
1923 page = get_next_sit_page(sbi, start_segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001924 raw_sit = page_address(page);
1925 }
1926
Chao Yu184a5cd2014-09-04 18:13:01 +08001927 /* flush dirty sit entries in region of current sit set */
1928 for_each_set_bit_from(segno, bitmap, end) {
1929 int offset, sit_offset;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001930
1931 se = get_seg_entry(sbi, segno);
Chao Yu184a5cd2014-09-04 18:13:01 +08001932
1933 /* add discard candidates */
Jaegeuk Kimd7bc2482014-12-12 13:53:41 -08001934 if (cpc->reason != CP_DISCARD) {
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001935 cpc->trim_start = segno;
1936 add_discard_addrs(sbi, cpc);
1937 }
Chao Yu184a5cd2014-09-04 18:13:01 +08001938
1939 if (to_journal) {
1940 offset = lookup_journal_in_cursum(sum,
1941 SIT_JOURNAL, segno, 1);
1942 f2fs_bug_on(sbi, offset < 0);
1943 segno_in_journal(sum, offset) =
1944 cpu_to_le32(segno);
1945 seg_info_to_raw_sit(se,
1946 &sit_in_journal(sum, offset));
1947 } else {
1948 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
1949 seg_info_to_raw_sit(se,
1950 &raw_sit->entries[sit_offset]);
1951 }
1952
1953 __clear_bit(segno, bitmap);
1954 sit_i->dirty_sentries--;
1955 ses->entry_cnt--;
1956 }
1957
1958 if (!to_journal)
1959 f2fs_put_page(page, 1);
1960
1961 f2fs_bug_on(sbi, ses->entry_cnt);
1962 release_sit_entry_set(ses);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001963 }
Chao Yu184a5cd2014-09-04 18:13:01 +08001964
1965 f2fs_bug_on(sbi, !list_empty(head));
1966 f2fs_bug_on(sbi, sit_i->dirty_sentries);
Chao Yu184a5cd2014-09-04 18:13:01 +08001967out:
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001968 if (cpc->reason == CP_DISCARD) {
1969 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++)
1970 add_discard_addrs(sbi, cpc);
1971 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001972 mutex_unlock(&sit_i->sentry_lock);
1973 mutex_unlock(&curseg->curseg_mutex);
1974
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001975 set_prefree_as_free_segments(sbi);
1976}
1977
1978static int build_sit_info(struct f2fs_sb_info *sbi)
1979{
1980 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1981 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1982 struct sit_info *sit_i;
1983 unsigned int sit_segs, start;
1984 char *src_bitmap, *dst_bitmap;
1985 unsigned int bitmap_size;
1986
1987 /* allocate memory for SIT information */
1988 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
1989 if (!sit_i)
1990 return -ENOMEM;
1991
1992 SM_I(sbi)->sit_info = sit_i;
1993
Jaegeuk Kim39307a82015-09-22 13:50:47 -07001994 sit_i->sentries = f2fs_kvzalloc(MAIN_SEGS(sbi) *
1995 sizeof(struct seg_entry), GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001996 if (!sit_i->sentries)
1997 return -ENOMEM;
1998
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001999 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Jaegeuk Kim39307a82015-09-22 13:50:47 -07002000 sit_i->dirty_sentries_bitmap = f2fs_kvzalloc(bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002001 if (!sit_i->dirty_sentries_bitmap)
2002 return -ENOMEM;
2003
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002004 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002005 sit_i->sentries[start].cur_valid_map
2006 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
2007 sit_i->sentries[start].ckpt_valid_map
2008 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07002009 sit_i->sentries[start].discard_map
2010 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
2011 if (!sit_i->sentries[start].cur_valid_map ||
2012 !sit_i->sentries[start].ckpt_valid_map ||
2013 !sit_i->sentries[start].discard_map)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002014 return -ENOMEM;
2015 }
2016
Jaegeuk Kim60a3b782015-02-10 16:44:29 -08002017 sit_i->tmp_map = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
2018 if (!sit_i->tmp_map)
2019 return -ENOMEM;
2020
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002021 if (sbi->segs_per_sec > 1) {
Jaegeuk Kim39307a82015-09-22 13:50:47 -07002022 sit_i->sec_entries = f2fs_kvzalloc(MAIN_SECS(sbi) *
2023 sizeof(struct sec_entry), GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002024 if (!sit_i->sec_entries)
2025 return -ENOMEM;
2026 }
2027
2028 /* get information related with SIT */
2029 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
2030
2031 /* setup SIT bitmap from ckeckpoint pack */
2032 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
2033 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
2034
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02002035 dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002036 if (!dst_bitmap)
2037 return -ENOMEM;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002038
2039 /* init SIT information */
2040 sit_i->s_ops = &default_salloc_ops;
2041
2042 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
2043 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
2044 sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
2045 sit_i->sit_bitmap = dst_bitmap;
2046 sit_i->bitmap_size = bitmap_size;
2047 sit_i->dirty_sentries = 0;
2048 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
2049 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
2050 sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
2051 mutex_init(&sit_i->sentry_lock);
2052 return 0;
2053}
2054
2055static int build_free_segmap(struct f2fs_sb_info *sbi)
2056{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002057 struct free_segmap_info *free_i;
2058 unsigned int bitmap_size, sec_bitmap_size;
2059
2060 /* allocate memory for free segmap information */
2061 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
2062 if (!free_i)
2063 return -ENOMEM;
2064
2065 SM_I(sbi)->free_info = free_i;
2066
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002067 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Jaegeuk Kim39307a82015-09-22 13:50:47 -07002068 free_i->free_segmap = f2fs_kvmalloc(bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002069 if (!free_i->free_segmap)
2070 return -ENOMEM;
2071
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002072 sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
Jaegeuk Kim39307a82015-09-22 13:50:47 -07002073 free_i->free_secmap = f2fs_kvmalloc(sec_bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002074 if (!free_i->free_secmap)
2075 return -ENOMEM;
2076
2077 /* set all segments as dirty temporarily */
2078 memset(free_i->free_segmap, 0xff, bitmap_size);
2079 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
2080
2081 /* init free segmap information */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002082 free_i->start_segno = GET_SEGNO_FROM_SEG0(sbi, MAIN_BLKADDR(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002083 free_i->free_segments = 0;
2084 free_i->free_sections = 0;
Chao Yu1a118cc2015-02-11 18:20:38 +08002085 spin_lock_init(&free_i->segmap_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002086 return 0;
2087}
2088
2089static int build_curseg(struct f2fs_sb_info *sbi)
2090{
Namjae Jeon1042d602012-12-01 10:56:13 +09002091 struct curseg_info *array;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002092 int i;
2093
Fabian Frederickb434bab2014-06-23 18:39:15 +02002094 array = kcalloc(NR_CURSEG_TYPE, sizeof(*array), GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002095 if (!array)
2096 return -ENOMEM;
2097
2098 SM_I(sbi)->curseg_array = array;
2099
2100 for (i = 0; i < NR_CURSEG_TYPE; i++) {
2101 mutex_init(&array[i].curseg_mutex);
2102 array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
2103 if (!array[i].sum_blk)
2104 return -ENOMEM;
2105 array[i].segno = NULL_SEGNO;
2106 array[i].next_blkoff = 0;
2107 }
2108 return restore_curseg_summaries(sbi);
2109}
2110
2111static void build_sit_entries(struct f2fs_sb_info *sbi)
2112{
2113 struct sit_info *sit_i = SIT_I(sbi);
2114 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
2115 struct f2fs_summary_block *sum = curseg->sum_blk;
Chao Yu74de5932013-11-22 09:09:59 +08002116 int sit_blk_cnt = SIT_BLK_CNT(sbi);
2117 unsigned int i, start, end;
2118 unsigned int readed, start_blk = 0;
Jaegeuk Kim90a893c2014-09-22 16:21:07 -07002119 int nrpages = MAX_BIO_BLOCKS(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002120
Chao Yu74de5932013-11-22 09:09:59 +08002121 do {
Chao Yu26879fb2015-10-12 17:05:59 +08002122 readed = ra_meta_pages(sbi, start_blk, nrpages, META_SIT, true);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002123
Chao Yu74de5932013-11-22 09:09:59 +08002124 start = start_blk * sit_i->sents_per_block;
2125 end = (start_blk + readed) * sit_i->sents_per_block;
2126
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002127 for (; start < end && start < MAIN_SEGS(sbi); start++) {
Chao Yu74de5932013-11-22 09:09:59 +08002128 struct seg_entry *se = &sit_i->sentries[start];
2129 struct f2fs_sit_block *sit_blk;
2130 struct f2fs_sit_entry sit;
2131 struct page *page;
2132
2133 mutex_lock(&curseg->curseg_mutex);
2134 for (i = 0; i < sits_in_cursum(sum); i++) {
Chris Fries6c311ec2014-01-17 14:44:39 -06002135 if (le32_to_cpu(segno_in_journal(sum, i))
2136 == start) {
Chao Yu74de5932013-11-22 09:09:59 +08002137 sit = sit_in_journal(sum, i);
2138 mutex_unlock(&curseg->curseg_mutex);
2139 goto got_it;
2140 }
2141 }
2142 mutex_unlock(&curseg->curseg_mutex);
2143
2144 page = get_current_sit_page(sbi, start);
2145 sit_blk = (struct f2fs_sit_block *)page_address(page);
2146 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
2147 f2fs_put_page(page, 1);
2148got_it:
2149 check_block_count(sbi, start, &sit);
2150 seg_info_from_raw_sit(se, &sit);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07002151
2152 /* build discard map only one time */
2153 memcpy(se->discard_map, se->cur_valid_map, SIT_VBLOCK_MAP_SIZE);
2154 sbi->discard_blks += sbi->blocks_per_seg - se->valid_blocks;
2155
Chao Yu74de5932013-11-22 09:09:59 +08002156 if (sbi->segs_per_sec > 1) {
2157 struct sec_entry *e = get_sec_entry(sbi, start);
2158 e->valid_blocks += se->valid_blocks;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002159 }
2160 }
Chao Yu74de5932013-11-22 09:09:59 +08002161 start_blk += readed;
2162 } while (start_blk < sit_blk_cnt);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002163}
2164
2165static void init_free_segmap(struct f2fs_sb_info *sbi)
2166{
2167 unsigned int start;
2168 int type;
2169
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002170 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002171 struct seg_entry *sentry = get_seg_entry(sbi, start);
2172 if (!sentry->valid_blocks)
2173 __set_free(sbi, start);
2174 }
2175
2176 /* set use the current segments */
2177 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
2178 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
2179 __set_test_and_inuse(sbi, curseg_t->segno);
2180 }
2181}
2182
2183static void init_dirty_segmap(struct f2fs_sb_info *sbi)
2184{
2185 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2186 struct free_segmap_info *free_i = FREE_I(sbi);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002187 unsigned int segno = 0, offset = 0;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002188 unsigned short valid_blocks;
2189
Namjae Jeon8736fbf2013-06-16 09:49:11 +09002190 while (1) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002191 /* find dirty segment based on free segmap */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002192 segno = find_next_inuse(free_i, MAIN_SEGS(sbi), offset);
2193 if (segno >= MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002194 break;
2195 offset = segno + 1;
2196 valid_blocks = get_valid_blocks(sbi, segno, 0);
Jaegeuk Kimec325b52014-09-02 16:24:11 -07002197 if (valid_blocks == sbi->blocks_per_seg || !valid_blocks)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002198 continue;
Jaegeuk Kimec325b52014-09-02 16:24:11 -07002199 if (valid_blocks > sbi->blocks_per_seg) {
2200 f2fs_bug_on(sbi, 1);
2201 continue;
2202 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002203 mutex_lock(&dirty_i->seglist_lock);
2204 __locate_dirty_segment(sbi, segno, DIRTY);
2205 mutex_unlock(&dirty_i->seglist_lock);
2206 }
2207}
2208
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002209static int init_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002210{
2211 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002212 unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002213
Jaegeuk Kim39307a82015-09-22 13:50:47 -07002214 dirty_i->victim_secmap = f2fs_kvzalloc(bitmap_size, GFP_KERNEL);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002215 if (!dirty_i->victim_secmap)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002216 return -ENOMEM;
2217 return 0;
2218}
2219
2220static int build_dirty_segmap(struct f2fs_sb_info *sbi)
2221{
2222 struct dirty_seglist_info *dirty_i;
2223 unsigned int bitmap_size, i;
2224
2225 /* allocate memory for dirty segments list information */
2226 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
2227 if (!dirty_i)
2228 return -ENOMEM;
2229
2230 SM_I(sbi)->dirty_info = dirty_i;
2231 mutex_init(&dirty_i->seglist_lock);
2232
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002233 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002234
2235 for (i = 0; i < NR_DIRTY_TYPE; i++) {
Jaegeuk Kim39307a82015-09-22 13:50:47 -07002236 dirty_i->dirty_segmap[i] = f2fs_kvzalloc(bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002237 if (!dirty_i->dirty_segmap[i])
2238 return -ENOMEM;
2239 }
2240
2241 init_dirty_segmap(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002242 return init_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002243}
2244
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002245/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002246 * Update min, max modified time for cost-benefit GC algorithm
2247 */
2248static void init_min_max_mtime(struct f2fs_sb_info *sbi)
2249{
2250 struct sit_info *sit_i = SIT_I(sbi);
2251 unsigned int segno;
2252
2253 mutex_lock(&sit_i->sentry_lock);
2254
2255 sit_i->min_mtime = LLONG_MAX;
2256
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002257 for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002258 unsigned int i;
2259 unsigned long long mtime = 0;
2260
2261 for (i = 0; i < sbi->segs_per_sec; i++)
2262 mtime += get_seg_entry(sbi, segno + i)->mtime;
2263
2264 mtime = div_u64(mtime, sbi->segs_per_sec);
2265
2266 if (sit_i->min_mtime > mtime)
2267 sit_i->min_mtime = mtime;
2268 }
2269 sit_i->max_mtime = get_mtime(sbi);
2270 mutex_unlock(&sit_i->sentry_lock);
2271}
2272
2273int build_segment_manager(struct f2fs_sb_info *sbi)
2274{
2275 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
2276 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Namjae Jeon1042d602012-12-01 10:56:13 +09002277 struct f2fs_sm_info *sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002278 int err;
2279
2280 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
2281 if (!sm_info)
2282 return -ENOMEM;
2283
2284 /* init sm info */
2285 sbi->sm_info = sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002286 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
2287 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
2288 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
2289 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
2290 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
2291 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
2292 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
Jaegeuk Kim58c41032014-03-19 14:17:21 +09002293 sm_info->rec_prefree_segments = sm_info->main_segments *
2294 DEF_RECLAIM_PREFREE_SEGMENTS / 100;
Jaegeuk Kim9b5f1362014-09-16 18:30:54 -07002295 sm_info->ipu_policy = 1 << F2FS_IPU_FSYNC;
Jaegeuk Kim216fbd62013-11-07 13:13:42 +09002296 sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -07002297 sm_info->min_fsync_blocks = DEF_MIN_FSYNC_BLOCKS;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002298
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002299 INIT_LIST_HEAD(&sm_info->discard_list);
2300 sm_info->nr_discards = 0;
2301 sm_info->max_discards = 0;
2302
Jaegeuk Kimbba681c2015-01-26 17:41:23 -08002303 sm_info->trim_sections = DEF_BATCHED_TRIM_SECTIONS;
2304
Chao Yu184a5cd2014-09-04 18:13:01 +08002305 INIT_LIST_HEAD(&sm_info->sit_entry_set);
2306
Gu Zhengb270ad62014-04-11 17:49:55 +08002307 if (test_opt(sbi, FLUSH_MERGE) && !f2fs_readonly(sbi->sb)) {
Gu Zheng2163d192014-04-27 14:21:33 +08002308 err = create_flush_cmd_control(sbi);
2309 if (err)
Gu Zhenga688b9d9e2014-04-27 14:21:21 +08002310 return err;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +09002311 }
2312
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002313 err = build_sit_info(sbi);
2314 if (err)
2315 return err;
2316 err = build_free_segmap(sbi);
2317 if (err)
2318 return err;
2319 err = build_curseg(sbi);
2320 if (err)
2321 return err;
2322
2323 /* reinit free segmap based on SIT */
2324 build_sit_entries(sbi);
2325
2326 init_free_segmap(sbi);
2327 err = build_dirty_segmap(sbi);
2328 if (err)
2329 return err;
2330
2331 init_min_max_mtime(sbi);
2332 return 0;
2333}
2334
2335static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
2336 enum dirty_type dirty_type)
2337{
2338 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2339
2340 mutex_lock(&dirty_i->seglist_lock);
Jaegeuk Kim39307a82015-09-22 13:50:47 -07002341 kvfree(dirty_i->dirty_segmap[dirty_type]);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002342 dirty_i->nr_dirty[dirty_type] = 0;
2343 mutex_unlock(&dirty_i->seglist_lock);
2344}
2345
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002346static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002347{
2348 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim39307a82015-09-22 13:50:47 -07002349 kvfree(dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002350}
2351
2352static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
2353{
2354 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2355 int i;
2356
2357 if (!dirty_i)
2358 return;
2359
2360 /* discard pre-free/dirty segments list */
2361 for (i = 0; i < NR_DIRTY_TYPE; i++)
2362 discard_dirty_segmap(sbi, i);
2363
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002364 destroy_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002365 SM_I(sbi)->dirty_info = NULL;
2366 kfree(dirty_i);
2367}
2368
2369static void destroy_curseg(struct f2fs_sb_info *sbi)
2370{
2371 struct curseg_info *array = SM_I(sbi)->curseg_array;
2372 int i;
2373
2374 if (!array)
2375 return;
2376 SM_I(sbi)->curseg_array = NULL;
2377 for (i = 0; i < NR_CURSEG_TYPE; i++)
2378 kfree(array[i].sum_blk);
2379 kfree(array);
2380}
2381
2382static void destroy_free_segmap(struct f2fs_sb_info *sbi)
2383{
2384 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
2385 if (!free_i)
2386 return;
2387 SM_I(sbi)->free_info = NULL;
Jaegeuk Kim39307a82015-09-22 13:50:47 -07002388 kvfree(free_i->free_segmap);
2389 kvfree(free_i->free_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002390 kfree(free_i);
2391}
2392
2393static void destroy_sit_info(struct f2fs_sb_info *sbi)
2394{
2395 struct sit_info *sit_i = SIT_I(sbi);
2396 unsigned int start;
2397
2398 if (!sit_i)
2399 return;
2400
2401 if (sit_i->sentries) {
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002402 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002403 kfree(sit_i->sentries[start].cur_valid_map);
2404 kfree(sit_i->sentries[start].ckpt_valid_map);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07002405 kfree(sit_i->sentries[start].discard_map);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002406 }
2407 }
Jaegeuk Kim60a3b782015-02-10 16:44:29 -08002408 kfree(sit_i->tmp_map);
2409
Jaegeuk Kim39307a82015-09-22 13:50:47 -07002410 kvfree(sit_i->sentries);
2411 kvfree(sit_i->sec_entries);
2412 kvfree(sit_i->dirty_sentries_bitmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002413
2414 SM_I(sbi)->sit_info = NULL;
2415 kfree(sit_i->sit_bitmap);
2416 kfree(sit_i);
2417}
2418
2419void destroy_segment_manager(struct f2fs_sb_info *sbi)
2420{
2421 struct f2fs_sm_info *sm_info = SM_I(sbi);
Gu Zhenga688b9d9e2014-04-27 14:21:21 +08002422
Chao Yu3b03f722013-11-06 09:12:04 +08002423 if (!sm_info)
2424 return;
Gu Zheng2163d192014-04-27 14:21:33 +08002425 destroy_flush_cmd_control(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002426 destroy_dirty_segmap(sbi);
2427 destroy_curseg(sbi);
2428 destroy_free_segmap(sbi);
2429 destroy_sit_info(sbi);
2430 sbi->sm_info = NULL;
2431 kfree(sm_info);
2432}
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002433
2434int __init create_segment_manager_caches(void)
2435{
2436 discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
Gu Zhenge8512d22014-03-07 18:43:28 +08002437 sizeof(struct discard_entry));
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002438 if (!discard_entry_slab)
Chao Yu184a5cd2014-09-04 18:13:01 +08002439 goto fail;
2440
2441 sit_entry_set_slab = f2fs_kmem_cache_create("sit_entry_set",
Changman Leec9ee0082014-11-21 15:42:07 +09002442 sizeof(struct sit_entry_set));
Chao Yu184a5cd2014-09-04 18:13:01 +08002443 if (!sit_entry_set_slab)
2444 goto destory_discard_entry;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002445
2446 inmem_entry_slab = f2fs_kmem_cache_create("inmem_page_entry",
2447 sizeof(struct inmem_pages));
2448 if (!inmem_entry_slab)
2449 goto destroy_sit_entry_set;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002450 return 0;
Chao Yu184a5cd2014-09-04 18:13:01 +08002451
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002452destroy_sit_entry_set:
2453 kmem_cache_destroy(sit_entry_set_slab);
Chao Yu184a5cd2014-09-04 18:13:01 +08002454destory_discard_entry:
2455 kmem_cache_destroy(discard_entry_slab);
2456fail:
2457 return -ENOMEM;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002458}
2459
2460void destroy_segment_manager_caches(void)
2461{
Chao Yu184a5cd2014-09-04 18:13:01 +08002462 kmem_cache_destroy(sit_entry_set_slab);
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002463 kmem_cache_destroy(discard_entry_slab);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002464 kmem_cache_destroy(inmem_entry_slab);
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002465}