blob: 250530f124ed8c042dc046ae8c70599656fed82e [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 Kimd430af22017-05-17 10:36:58 -070019#include <linux/freezer.h>
Jaegeuk Kimcfc72cc2017-09-09 12:03:23 -070020#include <linux/sched.h>
Jaegeuk Kim351df4b2012-11-02 17:09:16 +090021
22#include "f2fs.h"
23#include "segment.h"
24#include "node.h"
Jaegeuk Kim766330d2017-08-15 21:27:19 -070025#include "gc.h"
Jaegeuk Kim9e4ded32014-12-17 19:58:58 -080026#include "trace.h"
Namjae Jeon6ec178d2013-04-23 17:51:43 +090027#include <trace/events/f2fs.h>
Jaegeuk Kim351df4b2012-11-02 17:09:16 +090028
Changman Lee9a7f1432013-11-15 10:42:51 +090029#define __reverse_ffz(x) __reverse_ffs(~(x))
30
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +090031static struct kmem_cache *discard_entry_slab;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070032static struct kmem_cache *discard_cmd_slab;
Chao Yu184a5cd2014-09-04 18:13:01 +080033static struct kmem_cache *sit_entry_set_slab;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -070034static struct kmem_cache *inmem_entry_slab;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +090035
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070036static unsigned long __reverse_ulong(unsigned char *str)
37{
38 unsigned long tmp = 0;
39 int shift = 24, idx = 0;
40
41#if BITS_PER_LONG == 64
42 shift = 56;
43#endif
44 while (shift >= 0) {
45 tmp |= (unsigned long)str[idx++] << shift;
46 shift -= BITS_PER_BYTE;
47 }
48 return tmp;
49}
50
Changman Lee9a7f1432013-11-15 10:42:51 +090051/*
52 * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since
53 * MSB and LSB are reversed in a byte by f2fs_set_bit.
54 */
55static inline unsigned long __reverse_ffs(unsigned long word)
56{
57 int num = 0;
58
59#if BITS_PER_LONG == 64
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070060 if ((word & 0xffffffff00000000UL) == 0)
Changman Lee9a7f1432013-11-15 10:42:51 +090061 num += 32;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070062 else
Changman Lee9a7f1432013-11-15 10:42:51 +090063 word >>= 32;
Changman Lee9a7f1432013-11-15 10:42:51 +090064#endif
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070065 if ((word & 0xffff0000) == 0)
Changman Lee9a7f1432013-11-15 10:42:51 +090066 num += 16;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070067 else
Changman Lee9a7f1432013-11-15 10:42:51 +090068 word >>= 16;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070069
70 if ((word & 0xff00) == 0)
Changman Lee9a7f1432013-11-15 10:42:51 +090071 num += 8;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070072 else
Changman Lee9a7f1432013-11-15 10:42:51 +090073 word >>= 8;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070074
Changman Lee9a7f1432013-11-15 10:42:51 +090075 if ((word & 0xf0) == 0)
76 num += 4;
77 else
78 word >>= 4;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070079
Changman Lee9a7f1432013-11-15 10:42:51 +090080 if ((word & 0xc) == 0)
81 num += 2;
82 else
83 word >>= 2;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070084
Changman Lee9a7f1432013-11-15 10:42:51 +090085 if ((word & 0x2) == 0)
86 num += 1;
87 return num;
88}
89
90/*
arter97e1c42042014-08-06 23:22:50 +090091 * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c because
Changman Lee9a7f1432013-11-15 10:42:51 +090092 * f2fs_set_bit makes MSB and LSB reversed in a byte.
Fan Li692223d2015-11-12 08:43:04 +080093 * @size must be integral times of unsigned long.
Changman Lee9a7f1432013-11-15 10:42:51 +090094 * Example:
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070095 * MSB <--> LSB
96 * f2fs_set_bit(0, bitmap) => 1000 0000
97 * f2fs_set_bit(7, bitmap) => 0000 0001
Changman Lee9a7f1432013-11-15 10:42:51 +090098 */
99static unsigned long __find_rev_next_bit(const unsigned long *addr,
100 unsigned long size, unsigned long offset)
101{
102 const unsigned long *p = addr + BIT_WORD(offset);
Fan Li692223d2015-11-12 08:43:04 +0800103 unsigned long result = size;
Changman Lee9a7f1432013-11-15 10:42:51 +0900104 unsigned long tmp;
Changman Lee9a7f1432013-11-15 10:42:51 +0900105
106 if (offset >= size)
107 return size;
108
Fan Li692223d2015-11-12 08:43:04 +0800109 size -= (offset & ~(BITS_PER_LONG - 1));
Changman Lee9a7f1432013-11-15 10:42:51 +0900110 offset %= BITS_PER_LONG;
Changman Lee9a7f1432013-11-15 10:42:51 +0900111
Fan Li692223d2015-11-12 08:43:04 +0800112 while (1) {
113 if (*p == 0)
114 goto pass;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700115
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700116 tmp = __reverse_ulong((unsigned char *)p);
Fan Li692223d2015-11-12 08:43:04 +0800117
118 tmp &= ~0UL >> offset;
119 if (size < BITS_PER_LONG)
120 tmp &= (~0UL << (BITS_PER_LONG - size));
Changman Lee9a7f1432013-11-15 10:42:51 +0900121 if (tmp)
Fan Li692223d2015-11-12 08:43:04 +0800122 goto found;
123pass:
124 if (size <= BITS_PER_LONG)
125 break;
Changman Lee9a7f1432013-11-15 10:42:51 +0900126 size -= BITS_PER_LONG;
Fan Li692223d2015-11-12 08:43:04 +0800127 offset = 0;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700128 p++;
Changman Lee9a7f1432013-11-15 10:42:51 +0900129 }
Fan Li692223d2015-11-12 08:43:04 +0800130 return result;
131found:
132 return result - size + __reverse_ffs(tmp);
Changman Lee9a7f1432013-11-15 10:42:51 +0900133}
134
135static unsigned long __find_rev_next_zero_bit(const unsigned long *addr,
136 unsigned long size, unsigned long offset)
137{
138 const unsigned long *p = addr + BIT_WORD(offset);
Jaegeuk Kim80609442015-12-04 16:51:13 -0800139 unsigned long result = size;
Changman Lee9a7f1432013-11-15 10:42:51 +0900140 unsigned long tmp;
Changman Lee9a7f1432013-11-15 10:42:51 +0900141
142 if (offset >= size)
143 return size;
144
Jaegeuk Kim80609442015-12-04 16:51:13 -0800145 size -= (offset & ~(BITS_PER_LONG - 1));
Changman Lee9a7f1432013-11-15 10:42:51 +0900146 offset %= BITS_PER_LONG;
Changman Lee9a7f1432013-11-15 10:42:51 +0900147
Jaegeuk Kim80609442015-12-04 16:51:13 -0800148 while (1) {
149 if (*p == ~0UL)
150 goto pass;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700151
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700152 tmp = __reverse_ulong((unsigned char *)p);
Jaegeuk Kim80609442015-12-04 16:51:13 -0800153
154 if (offset)
155 tmp |= ~0UL << (BITS_PER_LONG - offset);
156 if (size < BITS_PER_LONG)
157 tmp |= ~0UL >> size;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700158 if (tmp != ~0UL)
Jaegeuk Kim80609442015-12-04 16:51:13 -0800159 goto found;
160pass:
161 if (size <= BITS_PER_LONG)
162 break;
Changman Lee9a7f1432013-11-15 10:42:51 +0900163 size -= BITS_PER_LONG;
Jaegeuk Kim80609442015-12-04 16:51:13 -0800164 offset = 0;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700165 p++;
Changman Lee9a7f1432013-11-15 10:42:51 +0900166 }
Jaegeuk Kim80609442015-12-04 16:51:13 -0800167 return result;
168found:
169 return result - size + __reverse_ffz(tmp);
Changman Lee9a7f1432013-11-15 10:42:51 +0900170}
171
Jaegeuk Kim2b170fc2017-09-09 11:11:04 -0700172bool need_SSR(struct f2fs_sb_info *sbi)
173{
174 int node_secs = get_blocktype_secs(sbi, F2FS_DIRTY_NODES);
175 int dent_secs = get_blocktype_secs(sbi, F2FS_DIRTY_DENTS);
176 int imeta_secs = get_blocktype_secs(sbi, F2FS_DIRTY_IMETA);
177
178 if (test_opt(sbi, LFS))
179 return false;
180 if (sbi->gc_thread && sbi->gc_thread->gc_urgent)
181 return true;
182
183 return free_sections(sbi) <= (node_secs + 2 * dent_secs + imeta_secs +
184 2 * reserved_sections(sbi));
185}
186
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700187void register_inmem_page(struct inode *inode, struct page *page)
188{
189 struct f2fs_inode_info *fi = F2FS_I(inode);
190 struct inmem_pages *new;
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800191
Jaegeuk Kim9e4ded32014-12-17 19:58:58 -0800192 f2fs_trace_pid(page);
Jaegeuk Kim0722b102014-12-05 11:58:02 -0800193
Chao Yudecd36b2015-08-07 18:42:09 +0800194 set_page_private(page, (unsigned long)ATOMIC_WRITTEN_PAGE);
195 SetPagePrivate(page);
196
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700197 new = f2fs_kmem_cache_alloc(inmem_entry_slab, GFP_NOFS);
198
199 /* add atomic page indices to the list */
200 new->page = page;
201 INIT_LIST_HEAD(&new->list);
Chao Yudecd36b2015-08-07 18:42:09 +0800202
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700203 /* increase reference count with clean state */
204 mutex_lock(&fi->inmem_lock);
205 get_page(page);
206 list_add_tail(&new->list, &fi->inmem_pages);
Jaegeuk Kim8dcf2ff72014-12-05 17:18:15 -0800207 inc_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700208 mutex_unlock(&fi->inmem_lock);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -0700209
210 trace_f2fs_register_inmem_page(page, INMEM);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700211}
212
Chao Yu28bc1062016-02-06 14:40:34 +0800213static int __revoke_inmem_pages(struct inode *inode,
214 struct list_head *head, bool drop, bool recover)
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700215{
Chao Yu28bc1062016-02-06 14:40:34 +0800216 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700217 struct inmem_pages *cur, *tmp;
Chao Yu28bc1062016-02-06 14:40:34 +0800218 int err = 0;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700219
Chao Yu29b96b52016-02-06 14:38:29 +0800220 list_for_each_entry_safe(cur, tmp, head, list) {
Chao Yu28bc1062016-02-06 14:40:34 +0800221 struct page *page = cur->page;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700222
Chao Yu28bc1062016-02-06 14:40:34 +0800223 if (drop)
224 trace_f2fs_commit_inmem_page(page, INMEM_DROP);
225
226 lock_page(page);
227
228 if (recover) {
229 struct dnode_of_data dn;
230 struct node_info ni;
231
232 trace_f2fs_commit_inmem_page(page, INMEM_REVOKE);
Chao Yu4b634f362017-08-08 19:09:08 +0800233retry:
Chao Yu28bc1062016-02-06 14:40:34 +0800234 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4b634f362017-08-08 19:09:08 +0800235 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
236 if (err) {
237 if (err == -ENOMEM) {
238 congestion_wait(BLK_RW_ASYNC, HZ/50);
239 cond_resched();
240 goto retry;
241 }
Chao Yu28bc1062016-02-06 14:40:34 +0800242 err = -EAGAIN;
243 goto next;
244 }
245 get_node_info(sbi, dn.nid, &ni);
246 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
247 cur->old_addr, ni.version, true, true);
248 f2fs_put_dnode(&dn);
249 }
250next:
Jaegeuk Kim63c52d72016-04-12 14:11:03 -0700251 /* we don't need to invalidate this in the sccessful status */
252 if (drop || recover)
253 ClearPageUptodate(page);
Chao Yu28bc1062016-02-06 14:40:34 +0800254 set_page_private(page, 0);
Chao Yuc81ced02016-04-29 20:13:36 +0800255 ClearPagePrivate(page);
Chao Yu28bc1062016-02-06 14:40:34 +0800256 f2fs_put_page(page, 1);
Chao Yudecd36b2015-08-07 18:42:09 +0800257
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700258 list_del(&cur->list);
259 kmem_cache_free(inmem_entry_slab, cur);
Jaegeuk Kim8dcf2ff72014-12-05 17:18:15 -0800260 dec_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700261 }
Chao Yu28bc1062016-02-06 14:40:34 +0800262 return err;
Chao Yu29b96b52016-02-06 14:38:29 +0800263}
264
265void drop_inmem_pages(struct inode *inode)
266{
267 struct f2fs_inode_info *fi = F2FS_I(inode);
268
269 mutex_lock(&fi->inmem_lock);
Chao Yu28bc1062016-02-06 14:40:34 +0800270 __revoke_inmem_pages(inode, &fi->inmem_pages, true, false);
Chao Yu29b96b52016-02-06 14:38:29 +0800271 mutex_unlock(&fi->inmem_lock);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700272
273 clear_inode_flag(inode, FI_ATOMIC_FILE);
Chao Yu4e7540c2017-08-18 23:37:36 +0800274 clear_inode_flag(inode, FI_HOT_DATA);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700275 stat_dec_atomic_write(inode);
276}
277
278void drop_inmem_page(struct inode *inode, struct page *page)
279{
280 struct f2fs_inode_info *fi = F2FS_I(inode);
281 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
282 struct list_head *head = &fi->inmem_pages;
283 struct inmem_pages *cur = NULL;
284
285 f2fs_bug_on(sbi, !IS_ATOMIC_WRITTEN_PAGE(page));
286
287 mutex_lock(&fi->inmem_lock);
288 list_for_each_entry(cur, head, list) {
289 if (cur->page == page)
290 break;
291 }
292
293 f2fs_bug_on(sbi, !cur || cur->page != page);
294 list_del(&cur->list);
295 mutex_unlock(&fi->inmem_lock);
296
297 dec_page_count(sbi, F2FS_INMEM_PAGES);
298 kmem_cache_free(inmem_entry_slab, cur);
299
300 ClearPageUptodate(page);
301 set_page_private(page, 0);
302 ClearPagePrivate(page);
303 f2fs_put_page(page, 0);
304
305 trace_f2fs_commit_inmem_page(page, INMEM_INVALIDATE);
Chao Yu29b96b52016-02-06 14:38:29 +0800306}
307
Chao Yu28bc1062016-02-06 14:40:34 +0800308static int __commit_inmem_pages(struct inode *inode,
309 struct list_head *revoke_list)
Chao Yu29b96b52016-02-06 14:38:29 +0800310{
311 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
312 struct f2fs_inode_info *fi = F2FS_I(inode);
313 struct inmem_pages *cur, *tmp;
314 struct f2fs_io_info fio = {
315 .sbi = sbi,
Chao Yuf014be82017-09-29 13:59:38 +0800316 .ino = inode->i_ino,
Chao Yu29b96b52016-02-06 14:38:29 +0800317 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -0500318 .op = REQ_OP_WRITE,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700319 .op_flags = REQ_SYNC | REQ_PRIO,
Chao Yuc0fe4882017-08-02 23:21:48 +0800320 .io_type = FS_DATA_IO,
Chao Yu29b96b52016-02-06 14:38:29 +0800321 };
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700322 pgoff_t last_idx = ULONG_MAX;
Chao Yu29b96b52016-02-06 14:38:29 +0800323 int err = 0;
324
325 list_for_each_entry_safe(cur, tmp, &fi->inmem_pages, list) {
Chao Yu28bc1062016-02-06 14:40:34 +0800326 struct page *page = cur->page;
327
328 lock_page(page);
329 if (page->mapping == inode->i_mapping) {
330 trace_f2fs_commit_inmem_page(page, INMEM);
331
332 set_page_dirty(page);
333 f2fs_wait_on_page_writeback(page, DATA, true);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700334 if (clear_page_dirty_for_io(page)) {
Chao Yu29b96b52016-02-06 14:38:29 +0800335 inode_dec_dirty_pages(inode);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700336 remove_dirty_inode(inode);
337 }
Jaegeuk Kim847413f2017-07-19 10:59:55 -0700338retry:
Chao Yu28bc1062016-02-06 14:40:34 +0800339 fio.page = page;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700340 fio.old_blkaddr = NULL_ADDR;
341 fio.encrypted_page = NULL;
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -0700342 fio.need_lock = LOCK_DONE;
Chao Yu29b96b52016-02-06 14:38:29 +0800343 err = do_write_data_page(&fio);
344 if (err) {
Jaegeuk Kim847413f2017-07-19 10:59:55 -0700345 if (err == -ENOMEM) {
346 congestion_wait(BLK_RW_ASYNC, HZ/50);
347 cond_resched();
348 goto retry;
349 }
Chao Yu28bc1062016-02-06 14:40:34 +0800350 unlock_page(page);
Chao Yu29b96b52016-02-06 14:38:29 +0800351 break;
352 }
Chao Yu28bc1062016-02-06 14:40:34 +0800353 /* record old blkaddr for revoking */
354 cur->old_addr = fio.old_blkaddr;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700355 last_idx = page->index;
Chao Yu29b96b52016-02-06 14:38:29 +0800356 }
Chao Yu28bc1062016-02-06 14:40:34 +0800357 unlock_page(page);
358 list_move_tail(&cur->list, revoke_list);
Chao Yu29b96b52016-02-06 14:38:29 +0800359 }
360
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700361 if (last_idx != ULONG_MAX)
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -0700362 f2fs_submit_merged_write_cond(sbi, inode, 0, last_idx, DATA);
Chao Yu28bc1062016-02-06 14:40:34 +0800363
364 if (!err)
365 __revoke_inmem_pages(inode, revoke_list, false, false);
366
Chao Yu29b96b52016-02-06 14:38:29 +0800367 return err;
368}
369
370int commit_inmem_pages(struct inode *inode)
371{
372 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
373 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yu28bc1062016-02-06 14:40:34 +0800374 struct list_head revoke_list;
375 int err;
Chao Yu29b96b52016-02-06 14:38:29 +0800376
Chao Yu28bc1062016-02-06 14:40:34 +0800377 INIT_LIST_HEAD(&revoke_list);
Chao Yu29b96b52016-02-06 14:38:29 +0800378 f2fs_balance_fs(sbi, true);
379 f2fs_lock_op(sbi);
380
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700381 set_inode_flag(inode, FI_ATOMIC_COMMIT);
382
Chao Yu29b96b52016-02-06 14:38:29 +0800383 mutex_lock(&fi->inmem_lock);
Chao Yu28bc1062016-02-06 14:40:34 +0800384 err = __commit_inmem_pages(inode, &revoke_list);
385 if (err) {
386 int ret;
387 /*
388 * try to revoke all committed pages, but still we could fail
389 * due to no memory or other reason, if that happened, EAGAIN
390 * will be returned, which means in such case, transaction is
391 * already not integrity, caller should use journal to do the
392 * recovery or rewrite & commit last transaction. For other
393 * error number, revoking was done by filesystem itself.
394 */
395 ret = __revoke_inmem_pages(inode, &revoke_list, false, true);
396 if (ret)
397 err = ret;
398
399 /* drop all uncommitted pages */
400 __revoke_inmem_pages(inode, &fi->inmem_pages, true, false);
401 }
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700402 mutex_unlock(&fi->inmem_lock);
403
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700404 clear_inode_flag(inode, FI_ATOMIC_COMMIT);
405
Chao Yu29b96b52016-02-06 14:38:29 +0800406 f2fs_unlock_op(sbi);
Jaegeuk Kimedb27de2015-07-25 00:52:52 -0700407 return err;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700408}
409
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900410/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900411 * This function balances dirty node and dentry pages.
412 * In addition, it controls garbage collection.
413 */
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800414void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900415{
Chao Yu0f348022016-09-26 19:45:55 +0800416#ifdef CONFIG_F2FS_FAULT_INJECTION
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700417 if (time_to_inject(sbi, FAULT_CHECKPOINT)) {
418 f2fs_show_injection_info(FAULT_CHECKPOINT);
Chao Yu0f348022016-09-26 19:45:55 +0800419 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700420 }
Chao Yu0f348022016-09-26 19:45:55 +0800421#endif
422
Jaegeuk Kime589c2c2016-06-02 15:24:24 -0700423 /* balance_fs_bg is able to be pending */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700424 if (need && excess_cached_nats(sbi))
Jaegeuk Kime589c2c2016-06-02 15:24:24 -0700425 f2fs_balance_fs_bg(sbi);
426
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900427 /*
Jaegeuk Kim029cd282012-12-21 17:20:21 +0900428 * We should do GC or end up with checkpoint, if there are so many dirty
429 * dir/node pages without enough free segments.
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900430 */
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -0700431 if (has_not_enough_free_secs(sbi, 0, 0)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900432 mutex_lock(&sbi->gc_mutex);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700433 f2fs_gc(sbi, false, false, NULL_SEGNO);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900434 }
435}
436
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900437void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi)
438{
Chao Yu1dcc3362015-02-05 17:57:31 +0800439 /* try to shrink extent cache when there is no enough memory */
Jaegeuk Kim554df792015-06-19 13:41:23 -0700440 if (!available_free_memory(sbi, EXTENT_CACHE))
441 f2fs_shrink_extent_tree(sbi, EXTENT_CACHE_SHRINK_NUMBER);
Chao Yu1dcc3362015-02-05 17:57:31 +0800442
Jaegeuk Kim1b38dc82015-06-19 15:36:07 -0700443 /* check the # of cached NAT entries */
444 if (!available_free_memory(sbi, NAT_ENTRIES))
445 try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK);
446
Chao Yu31696582015-07-28 18:33:46 +0800447 if (!available_free_memory(sbi, FREE_NIDS))
Jaegeuk Kimad4edb82016-06-16 16:41:49 -0700448 try_to_free_nids(sbi, MAX_FREE_NIDS);
449 else
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700450 build_free_nids(sbi, false, false);
451
452 if (!is_idle(sbi) && !excess_dirty_nats(sbi))
453 return;
Chao Yu31696582015-07-28 18:33:46 +0800454
Jaegeuk Kim1b38dc82015-06-19 15:36:07 -0700455 /* checkpoint is the only way to shrink partial cached entries */
456 if (!available_free_memory(sbi, NAT_ENTRIES) ||
Jaegeuk Kim60b99b42015-10-05 14:49:57 -0700457 !available_free_memory(sbi, INO_ENTRIES) ||
Chao Yu7d768d22016-01-18 18:31:18 +0800458 excess_prefree_segs(sbi) ||
459 excess_dirty_nats(sbi) ||
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700460 f2fs_time_over(sbi, CP_TIME)) {
Chao Yue9f5b8b2016-02-14 18:54:33 +0800461 if (test_opt(sbi, DATA_FLUSH)) {
462 struct blk_plug plug;
463
464 blk_start_plug(&plug);
Chao Yu36b35a02015-12-17 17:13:28 +0800465 sync_dirty_inodes(sbi, FILE_INODE);
Chao Yue9f5b8b2016-02-14 18:54:33 +0800466 blk_finish_plug(&plug);
467 }
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900468 f2fs_sync_fs(sbi->sb, true);
Jaegeuk Kim42190d22016-01-09 13:45:17 -0800469 stat_inc_bg_cp_count(sbi->stat_info);
Chao Yu36b35a02015-12-17 17:13:28 +0800470 }
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900471}
472
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700473static int __submit_flush_wait(struct f2fs_sb_info *sbi,
474 struct block_device *bdev)
475{
476 struct bio *bio = f2fs_bio_alloc(0);
477 int ret;
478
479 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH;
480 bio->bi_bdev = bdev;
481 ret = submit_bio_wait(bio);
482 bio_put(bio);
483
484 trace_f2fs_issue_flush(bdev, test_opt(sbi, NOBARRIER),
485 test_opt(sbi, FLUSH_MERGE), ret);
486 return ret;
487}
488
Chao Yuf014be82017-09-29 13:59:38 +0800489static int submit_flush_wait(struct f2fs_sb_info *sbi, nid_t ino)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700490{
Chao Yuf014be82017-09-29 13:59:38 +0800491 int ret = 0;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700492 int i;
493
Chao Yuf014be82017-09-29 13:59:38 +0800494 if (!sbi->s_ndevs)
495 return __submit_flush_wait(sbi, sbi->sb->s_bdev);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700496
Chao Yuf014be82017-09-29 13:59:38 +0800497 for (i = 0; i < sbi->s_ndevs; i++) {
498 if (!is_dirty_device(sbi, ino, i, FLUSH_INO))
499 continue;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700500 ret = __submit_flush_wait(sbi, FDEV(i).bdev);
501 if (ret)
502 break;
503 }
504 return ret;
505}
506
Gu Zheng2163d192014-04-27 14:21:33 +0800507static int issue_flush_thread(void *data)
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900508{
509 struct f2fs_sb_info *sbi = data;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700510 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800511 wait_queue_head_t *q = &fcc->flush_wait_queue;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900512repeat:
513 if (kthread_should_stop())
514 return 0;
515
Chao Yuce8bd4b2017-07-22 08:52:23 +0800516 sb_start_intwrite(sbi->sb);
517
Gu Zheng721bd4d2014-09-05 18:31:00 +0800518 if (!llist_empty(&fcc->issue_list)) {
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900519 struct flush_cmd *cmd, *next;
520 int ret;
521
Gu Zheng721bd4d2014-09-05 18:31:00 +0800522 fcc->dispatch_list = llist_del_all(&fcc->issue_list);
523 fcc->dispatch_list = llist_reverse_order(fcc->dispatch_list);
524
Chao Yuf014be82017-09-29 13:59:38 +0800525 cmd = llist_entry(fcc->dispatch_list, struct flush_cmd, llnode);
526
527 ret = submit_flush_wait(sbi, cmd->ino);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700528 atomic_inc(&fcc->issued_flush);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900529
Gu Zheng721bd4d2014-09-05 18:31:00 +0800530 llist_for_each_entry_safe(cmd, next,
531 fcc->dispatch_list, llnode) {
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900532 cmd->ret = ret;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900533 complete(&cmd->wait);
534 }
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800535 fcc->dispatch_list = NULL;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900536 }
537
Chao Yuce8bd4b2017-07-22 08:52:23 +0800538 sb_end_intwrite(sbi->sb);
539
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800540 wait_event_interruptible(*q,
Gu Zheng721bd4d2014-09-05 18:31:00 +0800541 kthread_should_stop() || !llist_empty(&fcc->issue_list));
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900542 goto repeat;
543}
544
Chao Yuf014be82017-09-29 13:59:38 +0800545int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino)
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900546{
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700547 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
Chao Yuadf8d902014-05-08 17:00:35 +0800548 struct flush_cmd cmd;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700549 int ret;
Jaegeuk Kim24a9ee02014-07-25 17:46:10 -0700550
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700551 if (test_opt(sbi, NOBARRIER))
552 return 0;
553
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700554 if (!test_opt(sbi, FLUSH_MERGE)) {
Chao Yuf014be82017-09-29 13:59:38 +0800555 ret = submit_flush_wait(sbi, ino);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700556 atomic_inc(&fcc->issued_flush);
557 return ret;
558 }
Jaegeuk Kim740432f2015-08-14 11:43:56 -0700559
Chao Yuf014be82017-09-29 13:59:38 +0800560 if (atomic_inc_return(&fcc->issing_flush) == 1 || sbi->s_ndevs > 1) {
561 ret = submit_flush_wait(sbi, ino);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700562 atomic_dec(&fcc->issing_flush);
563
564 atomic_inc(&fcc->issued_flush);
Jaegeuk Kim740432f2015-08-14 11:43:56 -0700565 return ret;
566 }
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900567
Chao Yuf014be82017-09-29 13:59:38 +0800568 cmd.ino = ino;
Chao Yuadf8d902014-05-08 17:00:35 +0800569 init_completion(&cmd.wait);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900570
Gu Zheng721bd4d2014-09-05 18:31:00 +0800571 llist_add(&cmd.llnode, &fcc->issue_list);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900572
Chao Yu4aab88e2017-08-21 22:53:45 +0800573 /* update issue_list before we wake up issue_flush thread */
574 smp_mb();
575
576 if (waitqueue_active(&fcc->flush_wait_queue))
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800577 wake_up(&fcc->flush_wait_queue);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900578
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700579 if (fcc->f2fs_issue_flush) {
580 wait_for_completion(&cmd.wait);
581 atomic_dec(&fcc->issing_flush);
582 } else {
Chao Yub0171d92017-08-31 18:56:06 +0800583 struct llist_node *list;
584
585 list = llist_del_all(&fcc->issue_list);
586 if (!list) {
587 wait_for_completion(&cmd.wait);
588 atomic_dec(&fcc->issing_flush);
589 } else {
590 struct flush_cmd *tmp, *next;
591
Chao Yuf014be82017-09-29 13:59:38 +0800592 ret = submit_flush_wait(sbi, ino);
Chao Yub0171d92017-08-31 18:56:06 +0800593
594 llist_for_each_entry_safe(tmp, next, list, llnode) {
595 if (tmp == &cmd) {
596 cmd.ret = ret;
597 atomic_dec(&fcc->issing_flush);
598 continue;
599 }
600 tmp->ret = ret;
601 complete(&tmp->wait);
602 }
603 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700604 }
Chao Yuadf8d902014-05-08 17:00:35 +0800605
606 return cmd.ret;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900607}
608
Gu Zheng2163d192014-04-27 14:21:33 +0800609int create_flush_cmd_control(struct f2fs_sb_info *sbi)
610{
611 dev_t dev = sbi->sb->s_bdev->bd_dev;
612 struct flush_cmd_control *fcc;
613 int err = 0;
614
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700615 if (SM_I(sbi)->fcc_info) {
616 fcc = SM_I(sbi)->fcc_info;
Yunlong Song471cd232017-06-24 15:57:19 +0800617 if (fcc->f2fs_issue_flush)
618 return err;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700619 goto init_thread;
620 }
621
Gu Zheng2163d192014-04-27 14:21:33 +0800622 fcc = kzalloc(sizeof(struct flush_cmd_control), GFP_KERNEL);
623 if (!fcc)
624 return -ENOMEM;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700625 atomic_set(&fcc->issued_flush, 0);
626 atomic_set(&fcc->issing_flush, 0);
Gu Zheng2163d192014-04-27 14:21:33 +0800627 init_waitqueue_head(&fcc->flush_wait_queue);
Gu Zheng721bd4d2014-09-05 18:31:00 +0800628 init_llist_head(&fcc->issue_list);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700629 SM_I(sbi)->fcc_info = fcc;
Yunlei Head9baf42017-06-01 16:43:51 +0800630 if (!test_opt(sbi, FLUSH_MERGE))
631 return err;
632
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700633init_thread:
Gu Zheng2163d192014-04-27 14:21:33 +0800634 fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi,
635 "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev));
636 if (IS_ERR(fcc->f2fs_issue_flush)) {
637 err = PTR_ERR(fcc->f2fs_issue_flush);
638 kfree(fcc);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700639 SM_I(sbi)->fcc_info = NULL;
Gu Zheng2163d192014-04-27 14:21:33 +0800640 return err;
641 }
Gu Zheng2163d192014-04-27 14:21:33 +0800642
643 return err;
644}
645
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700646void destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free)
Gu Zheng2163d192014-04-27 14:21:33 +0800647{
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700648 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
Gu Zheng2163d192014-04-27 14:21:33 +0800649
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700650 if (fcc && fcc->f2fs_issue_flush) {
651 struct task_struct *flush_thread = fcc->f2fs_issue_flush;
652
653 fcc->f2fs_issue_flush = NULL;
654 kthread_stop(flush_thread);
655 }
656 if (free) {
657 kfree(fcc);
658 SM_I(sbi)->fcc_info = NULL;
659 }
Gu Zheng2163d192014-04-27 14:21:33 +0800660}
661
Chao Yufd6422e2017-09-29 13:59:39 +0800662int f2fs_flush_device_cache(struct f2fs_sb_info *sbi)
663{
664 int ret = 0, i;
665
666 if (!sbi->s_ndevs)
667 return 0;
668
669 for (i = 1; i < sbi->s_ndevs; i++) {
670 if (!f2fs_test_bit(i, (char *)&sbi->dirty_device))
671 continue;
672 ret = __submit_flush_wait(sbi, FDEV(i).bdev);
673 if (ret)
674 break;
675
676 spin_lock(&sbi->dev_lock);
677 f2fs_clear_bit(i, (char *)&sbi->dirty_device);
678 spin_unlock(&sbi->dev_lock);
679 }
680
681 return ret;
682}
683
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900684static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
685 enum dirty_type dirty_type)
686{
687 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
688
689 /* need not be added */
690 if (IS_CURSEG(sbi, segno))
691 return;
692
693 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
694 dirty_i->nr_dirty[dirty_type]++;
695
696 if (dirty_type == DIRTY) {
697 struct seg_entry *sentry = get_seg_entry(sbi, segno);
Changman Lee4625d6a2013-10-25 17:31:57 +0900698 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900699
Jaegeuk Kimec325b52014-09-02 16:24:11 -0700700 if (unlikely(t >= DIRTY)) {
701 f2fs_bug_on(sbi, 1);
702 return;
703 }
Changman Lee4625d6a2013-10-25 17:31:57 +0900704 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
705 dirty_i->nr_dirty[t]++;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900706 }
707}
708
709static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
710 enum dirty_type dirty_type)
711{
712 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
713
714 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
715 dirty_i->nr_dirty[dirty_type]--;
716
717 if (dirty_type == DIRTY) {
Changman Lee4625d6a2013-10-25 17:31:57 +0900718 struct seg_entry *sentry = get_seg_entry(sbi, segno);
719 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900720
Changman Lee4625d6a2013-10-25 17:31:57 +0900721 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
722 dirty_i->nr_dirty[t]--;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900723
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700724 if (get_valid_blocks(sbi, segno, true) == 0)
725 clear_bit(GET_SEC_FROM_SEG(sbi, segno),
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +0900726 dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900727 }
728}
729
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900730/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900731 * Should not occur error such as -ENOMEM.
732 * Adding dirty entry into seglist is not critical operation.
733 * If a given segment is one of current working segments, it won't be added.
734 */
Haicheng Li8d8451a2013-06-13 16:59:28 +0800735static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900736{
737 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
738 unsigned short valid_blocks;
739
740 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
741 return;
742
743 mutex_lock(&dirty_i->seglist_lock);
744
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700745 valid_blocks = get_valid_blocks(sbi, segno, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900746
747 if (valid_blocks == 0) {
748 __locate_dirty_segment(sbi, segno, PRE);
749 __remove_dirty_segment(sbi, segno, DIRTY);
750 } else if (valid_blocks < sbi->blocks_per_seg) {
751 __locate_dirty_segment(sbi, segno, DIRTY);
752 } else {
753 /* Recovery routine with SSR needs this */
754 __remove_dirty_segment(sbi, segno, DIRTY);
755 }
756
757 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900758}
759
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700760static struct discard_cmd *__create_discard_cmd(struct f2fs_sb_info *sbi,
761 struct block_device *bdev, block_t lstart,
762 block_t start, block_t len)
Chao Yu275b66b2016-08-29 23:58:34 +0800763{
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700764 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
765 struct list_head *pend_list;
766 struct discard_cmd *dc;
Chao Yu275b66b2016-08-29 23:58:34 +0800767
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700768 f2fs_bug_on(sbi, !len);
Chao Yu275b66b2016-08-29 23:58:34 +0800769
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700770 pend_list = &dcc->pend_list[plist_idx(len)];
771
772 dc = f2fs_kmem_cache_alloc(discard_cmd_slab, GFP_NOFS);
773 INIT_LIST_HEAD(&dc->list);
774 dc->bdev = bdev;
775 dc->lstart = lstart;
776 dc->start = start;
777 dc->len = len;
778 dc->ref = 0;
779 dc->state = D_PREP;
780 dc->error = 0;
781 init_completion(&dc->wait);
782 list_add_tail(&dc->list, pend_list);
783 atomic_inc(&dcc->discard_cmd_cnt);
784 dcc->undiscard_blks += len;
785
786 return dc;
Chao Yu275b66b2016-08-29 23:58:34 +0800787}
788
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700789static struct discard_cmd *__attach_discard_cmd(struct f2fs_sb_info *sbi,
790 struct block_device *bdev, block_t lstart,
791 block_t start, block_t len,
792 struct rb_node *parent, struct rb_node **p)
Chao Yu275b66b2016-08-29 23:58:34 +0800793{
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700794 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
795 struct discard_cmd *dc;
Chao Yu275b66b2016-08-29 23:58:34 +0800796
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700797 dc = __create_discard_cmd(sbi, bdev, lstart, start, len);
Chao Yu275b66b2016-08-29 23:58:34 +0800798
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700799 rb_link_node(&dc->rb_node, parent, p);
800 rb_insert_color(&dc->rb_node, &dcc->root);
Chao Yu275b66b2016-08-29 23:58:34 +0800801
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700802 return dc;
Chao Yu275b66b2016-08-29 23:58:34 +0800803}
804
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700805static void __detach_discard_cmd(struct discard_cmd_control *dcc,
806 struct discard_cmd *dc)
Chao Yu275b66b2016-08-29 23:58:34 +0800807{
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700808 if (dc->state == D_DONE)
809 atomic_dec(&dcc->issing_discard);
Chao Yu275b66b2016-08-29 23:58:34 +0800810
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700811 list_del(&dc->list);
812 rb_erase(&dc->rb_node, &dcc->root);
813 dcc->undiscard_blks -= dc->len;
814
815 kmem_cache_free(discard_cmd_slab, dc);
816
817 atomic_dec(&dcc->discard_cmd_cnt);
818}
819
820static void __remove_discard_cmd(struct f2fs_sb_info *sbi,
821 struct discard_cmd *dc)
822{
823 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
824
Chao Yu8258fd32017-10-04 09:08:36 +0800825 trace_f2fs_remove_discard(dc->bdev, dc->start, dc->len);
826
Chao Yueb6c48e2017-06-05 18:29:07 +0800827 f2fs_bug_on(sbi, dc->ref);
828
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700829 if (dc->error == -EOPNOTSUPP)
830 dc->error = 0;
831
832 if (dc->error)
833 f2fs_msg(sbi->sb, KERN_INFO,
Chao Yufa681212017-05-19 23:46:43 +0800834 "Issue discard(%u, %u, %u) failed, ret: %d",
835 dc->lstart, dc->start, dc->len, dc->error);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700836 __detach_discard_cmd(dcc, dc);
837}
838
839static void f2fs_submit_discard_endio(struct bio *bio)
840{
841 struct discard_cmd *dc = (struct discard_cmd *)bio->bi_private;
842
843 dc->error = bio->bi_error;
844 dc->state = D_DONE;
Chao Yub1f5c762017-05-19 23:46:44 +0800845 complete_all(&dc->wait);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700846 bio_put(bio);
Chao Yu275b66b2016-08-29 23:58:34 +0800847}
848
Chao Yu280fd0f2017-06-30 17:19:02 +0800849void __check_sit_bitmap(struct f2fs_sb_info *sbi,
850 block_t start, block_t end)
851{
852#ifdef CONFIG_F2FS_CHECK_FS
853 struct seg_entry *sentry;
854 unsigned int segno;
855 block_t blk = start;
856 unsigned long offset, size, max_blocks = sbi->blocks_per_seg;
857 unsigned long *map;
858
859 while (blk < end) {
860 segno = GET_SEGNO(sbi, blk);
861 sentry = get_seg_entry(sbi, segno);
862 offset = GET_BLKOFF_FROM_SEG0(sbi, blk);
863
Yunlong Song55824ab2017-08-04 17:07:15 +0800864 if (end < START_BLOCK(sbi, segno + 1))
865 size = GET_BLKOFF_FROM_SEG0(sbi, end);
866 else
867 size = max_blocks;
Chao Yu280fd0f2017-06-30 17:19:02 +0800868 map = (unsigned long *)(sentry->cur_valid_map);
869 offset = __find_rev_next_bit(map, size, offset);
870 f2fs_bug_on(sbi, offset != size);
Yunlong Song55824ab2017-08-04 17:07:15 +0800871 blk = START_BLOCK(sbi, segno + 1);
Chao Yu280fd0f2017-06-30 17:19:02 +0800872 }
873#endif
874}
875
Chao Yu275b66b2016-08-29 23:58:34 +0800876/* this function is copied from blkdev_issue_discard from block/blk-lib.c */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700877static void __submit_discard_cmd(struct f2fs_sb_info *sbi,
Chao Yudaf437d2017-10-04 09:08:34 +0800878 struct discard_policy *dpolicy,
879 struct discard_cmd *dc)
Chao Yu275b66b2016-08-29 23:58:34 +0800880{
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700881 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Chao Yudaf437d2017-10-04 09:08:34 +0800882 struct list_head *wait_list = (dpolicy->type == DPOLICY_FSTRIM) ?
883 &(dcc->fstrim_list) : &(dcc->wait_list);
Chao Yu275b66b2016-08-29 23:58:34 +0800884 struct bio *bio = NULL;
Chao Yudaf437d2017-10-04 09:08:34 +0800885 int flag = dpolicy->sync ? REQ_SYNC : 0;
Chao Yu275b66b2016-08-29 23:58:34 +0800886
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700887 if (dc->state != D_PREP)
888 return;
Chao Yu275b66b2016-08-29 23:58:34 +0800889
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700890 trace_f2fs_issue_discard(dc->bdev, dc->start, dc->len);
891
892 dc->error = __blkdev_issue_discard(dc->bdev,
893 SECTOR_FROM_BLOCK(dc->start),
894 SECTOR_FROM_BLOCK(dc->len),
895 GFP_NOFS, 0, &bio);
896 if (!dc->error) {
897 /* should keep before submission to avoid D_DONE right away */
898 dc->state = D_SUBMIT;
899 atomic_inc(&dcc->issued_discard);
900 atomic_inc(&dcc->issing_discard);
901 if (bio) {
902 bio->bi_private = dc;
903 bio->bi_end_io = f2fs_submit_discard_endio;
Chao Yu69a59672017-10-04 09:08:33 +0800904 bio->bi_opf |= flag;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700905 submit_bio(bio);
Chao Yu28e10232017-10-04 09:08:32 +0800906 list_move_tail(&dc->list, wait_list);
Chao Yu280fd0f2017-06-30 17:19:02 +0800907 __check_sit_bitmap(sbi, dc->start, dc->start + dc->len);
Chao Yuc0fe4882017-08-02 23:21:48 +0800908
909 f2fs_update_iostat(sbi, FS_DISCARD, 1);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700910 }
911 } else {
912 __remove_discard_cmd(sbi, dc);
913 }
914}
915
916static struct discard_cmd *__insert_discard_tree(struct f2fs_sb_info *sbi,
917 struct block_device *bdev, block_t lstart,
918 block_t start, block_t len,
919 struct rb_node **insert_p,
920 struct rb_node *insert_parent)
921{
922 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
923 struct rb_node **p = &dcc->root.rb_node;
924 struct rb_node *parent = NULL;
925 struct discard_cmd *dc = NULL;
926
927 if (insert_p && insert_parent) {
928 parent = insert_parent;
929 p = insert_p;
930 goto do_insert;
Chao Yu275b66b2016-08-29 23:58:34 +0800931 }
932
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700933 p = __lookup_rb_tree_for_insert(sbi, &dcc->root, &parent, lstart);
934do_insert:
935 dc = __attach_discard_cmd(sbi, bdev, lstart, start, len, parent, p);
936 if (!dc)
937 return NULL;
938
939 return dc;
940}
941
942static void __relocate_discard_cmd(struct discard_cmd_control *dcc,
943 struct discard_cmd *dc)
944{
945 list_move_tail(&dc->list, &dcc->pend_list[plist_idx(dc->len)]);
946}
947
948static void __punch_discard_cmd(struct f2fs_sb_info *sbi,
949 struct discard_cmd *dc, block_t blkaddr)
950{
951 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
952 struct discard_info di = dc->di;
953 bool modified = false;
954
955 if (dc->state == D_DONE || dc->len == 1) {
956 __remove_discard_cmd(sbi, dc);
957 return;
958 }
959
960 dcc->undiscard_blks -= di.len;
961
962 if (blkaddr > di.lstart) {
963 dc->len = blkaddr - dc->lstart;
964 dcc->undiscard_blks += dc->len;
965 __relocate_discard_cmd(dcc, dc);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700966 modified = true;
967 }
968
969 if (blkaddr < di.lstart + di.len - 1) {
970 if (modified) {
971 __insert_discard_tree(sbi, dc->bdev, blkaddr + 1,
972 di.start + blkaddr + 1 - di.lstart,
973 di.lstart + di.len - 1 - blkaddr,
974 NULL, NULL);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700975 } else {
976 dc->lstart++;
977 dc->len--;
978 dc->start++;
979 dcc->undiscard_blks += dc->len;
980 __relocate_discard_cmd(dcc, dc);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700981 }
982 }
983}
984
985static void __update_discard_tree_range(struct f2fs_sb_info *sbi,
986 struct block_device *bdev, block_t lstart,
987 block_t start, block_t len)
988{
989 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
990 struct discard_cmd *prev_dc = NULL, *next_dc = NULL;
991 struct discard_cmd *dc;
992 struct discard_info di = {0};
993 struct rb_node **insert_p = NULL, *insert_parent = NULL;
994 block_t end = lstart + len;
995
996 mutex_lock(&dcc->cmd_lock);
997
998 dc = (struct discard_cmd *)__lookup_rb_tree_ret(&dcc->root,
999 NULL, lstart,
1000 (struct rb_entry **)&prev_dc,
1001 (struct rb_entry **)&next_dc,
1002 &insert_p, &insert_parent, true);
1003 if (dc)
1004 prev_dc = dc;
1005
1006 if (!prev_dc) {
1007 di.lstart = lstart;
1008 di.len = next_dc ? next_dc->lstart - lstart : len;
1009 di.len = min(di.len, len);
1010 di.start = start;
1011 }
1012
1013 while (1) {
1014 struct rb_node *node;
1015 bool merged = false;
1016 struct discard_cmd *tdc = NULL;
1017
1018 if (prev_dc) {
1019 di.lstart = prev_dc->lstart + prev_dc->len;
1020 if (di.lstart < lstart)
1021 di.lstart = lstart;
1022 if (di.lstart >= end)
1023 break;
1024
1025 if (!next_dc || next_dc->lstart > end)
1026 di.len = end - di.lstart;
1027 else
1028 di.len = next_dc->lstart - di.lstart;
1029 di.start = start + di.lstart - lstart;
1030 }
1031
1032 if (!di.len)
1033 goto next;
1034
1035 if (prev_dc && prev_dc->state == D_PREP &&
1036 prev_dc->bdev == bdev &&
1037 __is_discard_back_mergeable(&di, &prev_dc->di)) {
1038 prev_dc->di.len += di.len;
1039 dcc->undiscard_blks += di.len;
1040 __relocate_discard_cmd(dcc, prev_dc);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001041 di = prev_dc->di;
1042 tdc = prev_dc;
1043 merged = true;
1044 }
1045
1046 if (next_dc && next_dc->state == D_PREP &&
1047 next_dc->bdev == bdev &&
1048 __is_discard_front_mergeable(&di, &next_dc->di)) {
1049 next_dc->di.lstart = di.lstart;
1050 next_dc->di.len += di.len;
1051 next_dc->di.start = di.start;
1052 dcc->undiscard_blks += di.len;
1053 __relocate_discard_cmd(dcc, next_dc);
1054 if (tdc)
1055 __remove_discard_cmd(sbi, tdc);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001056 merged = true;
1057 }
1058
1059 if (!merged) {
1060 __insert_discard_tree(sbi, bdev, di.lstart, di.start,
1061 di.len, NULL, NULL);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001062 }
1063 next:
1064 prev_dc = next_dc;
1065 if (!prev_dc)
1066 break;
1067
1068 node = rb_next(&prev_dc->rb_node);
1069 next_dc = rb_entry_safe(node, struct discard_cmd, rb_node);
1070 }
1071
1072 mutex_unlock(&dcc->cmd_lock);
1073}
1074
1075static int __queue_discard_cmd(struct f2fs_sb_info *sbi,
1076 struct block_device *bdev, block_t blkstart, block_t blklen)
1077{
1078 block_t lblkstart = blkstart;
1079
1080 trace_f2fs_queue_discard(bdev, blkstart, blklen);
1081
1082 if (sbi->s_ndevs) {
1083 int devi = f2fs_target_device_index(sbi, blkstart);
1084
1085 blkstart -= FDEV(devi).start_blk;
1086 }
1087 __update_discard_tree_range(sbi, bdev, lblkstart, blkstart, blklen);
1088 return 0;
1089}
1090
Chao Yu28e10232017-10-04 09:08:32 +08001091static void __issue_discard_cmd_range(struct f2fs_sb_info *sbi,
Chao Yudaf437d2017-10-04 09:08:34 +08001092 struct discard_policy *dpolicy,
1093 unsigned int start, unsigned int end)
Chao Yu28e10232017-10-04 09:08:32 +08001094{
1095 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1096 struct discard_cmd *prev_dc = NULL, *next_dc = NULL;
1097 struct rb_node **insert_p = NULL, *insert_parent = NULL;
1098 struct discard_cmd *dc;
1099 struct blk_plug plug;
1100 int issued;
1101
1102next:
1103 issued = 0;
1104
1105 mutex_lock(&dcc->cmd_lock);
1106 f2fs_bug_on(sbi, !__check_rb_tree_consistence(sbi, &dcc->root));
1107
1108 dc = (struct discard_cmd *)__lookup_rb_tree_ret(&dcc->root,
1109 NULL, start,
1110 (struct rb_entry **)&prev_dc,
1111 (struct rb_entry **)&next_dc,
1112 &insert_p, &insert_parent, true);
1113 if (!dc)
1114 dc = next_dc;
1115
1116 blk_start_plug(&plug);
1117
1118 while (dc && dc->lstart <= end) {
1119 struct rb_node *node;
1120
Chao Yudaf437d2017-10-04 09:08:34 +08001121 if (dc->len < dpolicy->granularity)
Chao Yu28e10232017-10-04 09:08:32 +08001122 goto skip;
1123
1124 if (dc->state != D_PREP) {
1125 list_move_tail(&dc->list, &dcc->fstrim_list);
1126 goto skip;
1127 }
1128
Chao Yudaf437d2017-10-04 09:08:34 +08001129 __submit_discard_cmd(sbi, dpolicy, dc);
Chao Yu28e10232017-10-04 09:08:32 +08001130
Chao Yu69a59672017-10-04 09:08:33 +08001131 if (++issued >= dpolicy->max_requests) {
Chao Yu28e10232017-10-04 09:08:32 +08001132 start = dc->lstart + dc->len;
1133
1134 blk_finish_plug(&plug);
1135 mutex_unlock(&dcc->cmd_lock);
1136
1137 schedule();
1138
1139 goto next;
1140 }
1141skip:
1142 node = rb_next(&dc->rb_node);
1143 dc = rb_entry_safe(node, struct discard_cmd, rb_node);
1144
1145 if (fatal_signal_pending(current))
1146 break;
1147 }
1148
1149 blk_finish_plug(&plug);
1150 mutex_unlock(&dcc->cmd_lock);
1151}
1152
Chao Yudaf437d2017-10-04 09:08:34 +08001153static int __issue_discard_cmd(struct f2fs_sb_info *sbi,
1154 struct discard_policy *dpolicy)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001155{
1156 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1157 struct list_head *pend_list;
1158 struct discard_cmd *dc, *tmp;
1159 struct blk_plug plug;
Chao Yudaf437d2017-10-04 09:08:34 +08001160 int i, iter = 0, issued = 0;
Chao Yu30da3a42017-09-12 21:35:12 +08001161 bool io_interrupted = false;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001162
Chao Yudaf437d2017-10-04 09:08:34 +08001163 for (i = MAX_PLIST_NUM - 1; i >= 0; i--) {
1164 if (i + 1 < dpolicy->granularity)
1165 break;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001166 pend_list = &dcc->pend_list[i];
Chao Yu6c46b372017-10-04 09:08:35 +08001167
1168 mutex_lock(&dcc->cmd_lock);
1169 f2fs_bug_on(sbi, !__check_rb_tree_consistence(sbi, &dcc->root));
1170 blk_start_plug(&plug);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001171 list_for_each_entry_safe(dc, tmp, pend_list, list) {
1172 f2fs_bug_on(sbi, dc->state != D_PREP);
1173
Chao Yu69a59672017-10-04 09:08:33 +08001174 if (dpolicy->io_aware && i < dpolicy->io_aware_gran &&
1175 !is_idle(sbi)) {
Chao Yu30da3a42017-09-12 21:35:12 +08001176 io_interrupted = true;
Chao Yu69a59672017-10-04 09:08:33 +08001177 goto skip;
Chao Yu30da3a42017-09-12 21:35:12 +08001178 }
1179
Chao Yudaf437d2017-10-04 09:08:34 +08001180 __submit_discard_cmd(sbi, dpolicy, dc);
Chao Yu69a59672017-10-04 09:08:33 +08001181 issued++;
1182skip:
1183 if (++iter >= dpolicy->max_requests)
Chao Yu6c46b372017-10-04 09:08:35 +08001184 break;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001185 }
Chao Yu6c46b372017-10-04 09:08:35 +08001186 blk_finish_plug(&plug);
1187 mutex_unlock(&dcc->cmd_lock);
1188
1189 if (iter >= dpolicy->max_requests)
1190 break;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001191 }
Chao Yuefe24da2017-08-07 23:09:56 +08001192
Chao Yu30da3a42017-09-12 21:35:12 +08001193 if (!issued && io_interrupted)
1194 issued = -1;
1195
Chao Yuefe24da2017-08-07 23:09:56 +08001196 return issued;
1197}
1198
1199static void __drop_discard_cmd(struct f2fs_sb_info *sbi)
1200{
1201 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1202 struct list_head *pend_list;
1203 struct discard_cmd *dc, *tmp;
1204 int i;
1205
1206 mutex_lock(&dcc->cmd_lock);
1207 for (i = MAX_PLIST_NUM - 1; i >= 0; i--) {
1208 pend_list = &dcc->pend_list[i];
1209 list_for_each_entry_safe(dc, tmp, pend_list, list) {
1210 f2fs_bug_on(sbi, dc->state != D_PREP);
1211 __remove_discard_cmd(sbi, dc);
1212 }
1213 }
1214 mutex_unlock(&dcc->cmd_lock);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001215}
1216
Chao Yua7aa97d2017-06-05 18:29:06 +08001217static void __wait_one_discard_bio(struct f2fs_sb_info *sbi,
1218 struct discard_cmd *dc)
1219{
1220 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1221
1222 wait_for_completion_io(&dc->wait);
1223 mutex_lock(&dcc->cmd_lock);
1224 f2fs_bug_on(sbi, dc->state != D_DONE);
1225 dc->ref--;
1226 if (!dc->ref)
1227 __remove_discard_cmd(sbi, dc);
1228 mutex_unlock(&dcc->cmd_lock);
1229}
1230
Chao Yudaf437d2017-10-04 09:08:34 +08001231static void __wait_discard_cmd_range(struct f2fs_sb_info *sbi,
1232 struct discard_policy *dpolicy,
1233 block_t start, block_t end)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001234{
1235 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Chao Yudaf437d2017-10-04 09:08:34 +08001236 struct list_head *wait_list = (dpolicy->type == DPOLICY_FSTRIM) ?
1237 &(dcc->fstrim_list) : &(dcc->wait_list);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001238 struct discard_cmd *dc, *tmp;
Chao Yu3215e782017-05-19 23:46:45 +08001239 bool need_wait;
1240
1241next:
1242 need_wait = false;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001243
1244 mutex_lock(&dcc->cmd_lock);
1245 list_for_each_entry_safe(dc, tmp, wait_list, list) {
Chao Yu28e10232017-10-04 09:08:32 +08001246 if (dc->lstart + dc->len <= start || end <= dc->lstart)
1247 continue;
Chao Yudaf437d2017-10-04 09:08:34 +08001248 if (dc->len < dpolicy->granularity)
Chao Yu28e10232017-10-04 09:08:32 +08001249 continue;
Chao Yudaf437d2017-10-04 09:08:34 +08001250 if (dc->state == D_DONE && !dc->ref) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001251 wait_for_completion_io(&dc->wait);
1252 __remove_discard_cmd(sbi, dc);
Chao Yu3215e782017-05-19 23:46:45 +08001253 } else {
1254 dc->ref++;
1255 need_wait = true;
1256 break;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001257 }
1258 }
1259 mutex_unlock(&dcc->cmd_lock);
Chao Yu3215e782017-05-19 23:46:45 +08001260
1261 if (need_wait) {
Chao Yua7aa97d2017-06-05 18:29:06 +08001262 __wait_one_discard_bio(sbi, dc);
Chao Yu3215e782017-05-19 23:46:45 +08001263 goto next;
1264 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001265}
1266
Chao Yudaf437d2017-10-04 09:08:34 +08001267static void __wait_all_discard_cmd(struct f2fs_sb_info *sbi,
1268 struct discard_policy *dpolicy)
Chao Yu28e10232017-10-04 09:08:32 +08001269{
Chao Yudaf437d2017-10-04 09:08:34 +08001270 __wait_discard_cmd_range(sbi, dpolicy, 0, UINT_MAX);
Chao Yu28e10232017-10-04 09:08:32 +08001271}
1272
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001273/* This should be covered by global mutex, &sit_i->sentry_lock */
1274void f2fs_wait_discard_bio(struct f2fs_sb_info *sbi, block_t blkaddr)
1275{
1276 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1277 struct discard_cmd *dc;
1278 bool need_wait = false;
1279
1280 mutex_lock(&dcc->cmd_lock);
1281 dc = (struct discard_cmd *)__lookup_rb_tree(&dcc->root, NULL, blkaddr);
1282 if (dc) {
1283 if (dc->state == D_PREP) {
1284 __punch_discard_cmd(sbi, dc, blkaddr);
1285 } else {
1286 dc->ref++;
1287 need_wait = true;
1288 }
1289 }
1290 mutex_unlock(&dcc->cmd_lock);
1291
Chao Yua7aa97d2017-06-05 18:29:06 +08001292 if (need_wait)
1293 __wait_one_discard_bio(sbi, dc);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001294}
1295
Chao Yud9d85cc2017-06-29 23:17:45 +08001296void stop_discard_thread(struct f2fs_sb_info *sbi)
1297{
1298 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1299
1300 if (dcc && dcc->f2fs_issue_discard) {
1301 struct task_struct *discard_thread = dcc->f2fs_issue_discard;
1302
1303 dcc->f2fs_issue_discard = NULL;
1304 kthread_stop(discard_thread);
1305 }
1306}
1307
Chao Yu28e10232017-10-04 09:08:32 +08001308/* This comes from f2fs_put_super */
1309void f2fs_wait_discard_bios(struct f2fs_sb_info *sbi)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001310{
Chao Yuefe24da2017-08-07 23:09:56 +08001311 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Chao Yudaf437d2017-10-04 09:08:34 +08001312 struct discard_policy dpolicy;
Chao Yuefe24da2017-08-07 23:09:56 +08001313
Chao Yudaf437d2017-10-04 09:08:34 +08001314 init_discard_policy(&dpolicy, DPOLICY_UMOUNT, dcc->discard_granularity);
1315 __issue_discard_cmd(sbi, &dpolicy);
1316 __drop_discard_cmd(sbi);
1317 __wait_all_discard_cmd(sbi, &dpolicy);
Chao Yuefe24da2017-08-07 23:09:56 +08001318}
1319
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001320static int issue_discard_thread(void *data)
1321{
1322 struct f2fs_sb_info *sbi = data;
1323 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1324 wait_queue_head_t *q = &dcc->discard_wait_queue;
Chao Yudaf437d2017-10-04 09:08:34 +08001325 struct discard_policy dpolicy;
Chao Yuefe24da2017-08-07 23:09:56 +08001326 unsigned int wait_ms = DEF_MIN_DISCARD_ISSUE_TIME;
1327 int issued;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001328
Jaegeuk Kimd430af22017-05-17 10:36:58 -07001329 set_freezable();
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001330
Jaegeuk Kimd430af22017-05-17 10:36:58 -07001331 do {
Chao Yudaf437d2017-10-04 09:08:34 +08001332 init_discard_policy(&dpolicy, DPOLICY_BG,
1333 dcc->discard_granularity);
1334
Chao Yuefe24da2017-08-07 23:09:56 +08001335 wait_event_interruptible_timeout(*q,
1336 kthread_should_stop() || freezing(current) ||
1337 dcc->discard_wake,
1338 msecs_to_jiffies(wait_ms));
Jaegeuk Kimd430af22017-05-17 10:36:58 -07001339 if (try_to_freeze())
1340 continue;
1341 if (kthread_should_stop())
1342 return 0;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001343
Jaegeuk Kim766330d2017-08-15 21:27:19 -07001344 if (dcc->discard_wake) {
Chao Yuefe24da2017-08-07 23:09:56 +08001345 dcc->discard_wake = 0;
Jaegeuk Kim766330d2017-08-15 21:27:19 -07001346 if (sbi->gc_thread && sbi->gc_thread->gc_urgent)
Chao Yudaf437d2017-10-04 09:08:34 +08001347 init_discard_policy(&dpolicy,
1348 DPOLICY_FORCE, 1);
Jaegeuk Kim766330d2017-08-15 21:27:19 -07001349 }
Chao Yuefe24da2017-08-07 23:09:56 +08001350
Chao Yuce8bd4b2017-07-22 08:52:23 +08001351 sb_start_intwrite(sbi->sb);
1352
Chao Yudaf437d2017-10-04 09:08:34 +08001353 issued = __issue_discard_cmd(sbi, &dpolicy);
Chao Yuefe24da2017-08-07 23:09:56 +08001354 if (issued) {
Chao Yudaf437d2017-10-04 09:08:34 +08001355 __wait_all_discard_cmd(sbi, &dpolicy);
1356 wait_ms = dpolicy.min_interval;
Chao Yuefe24da2017-08-07 23:09:56 +08001357 } else {
Chao Yudaf437d2017-10-04 09:08:34 +08001358 wait_ms = dpolicy.max_interval;
Chao Yuefe24da2017-08-07 23:09:56 +08001359 }
Jaegeuk Kimd430af22017-05-17 10:36:58 -07001360
Chao Yuce8bd4b2017-07-22 08:52:23 +08001361 sb_end_intwrite(sbi->sb);
1362
Jaegeuk Kimd430af22017-05-17 10:36:58 -07001363 } while (!kthread_should_stop());
1364 return 0;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001365}
1366
1367#ifdef CONFIG_BLK_DEV_ZONED
1368static int __f2fs_issue_discard_zone(struct f2fs_sb_info *sbi,
1369 struct block_device *bdev, block_t blkstart, block_t blklen)
1370{
1371 sector_t sector, nr_sects;
1372 block_t lblkstart = blkstart;
1373 int devi = 0;
1374
1375 if (sbi->s_ndevs) {
1376 devi = f2fs_target_device_index(sbi, blkstart);
1377 blkstart -= FDEV(devi).start_blk;
1378 }
1379
1380 /*
1381 * We need to know the type of the zone: for conventional zones,
1382 * use regular discard if the drive supports it. For sequential
1383 * zones, reset the zone write pointer.
1384 */
1385 switch (get_blkz_type(sbi, bdev, blkstart)) {
1386
1387 case BLK_ZONE_TYPE_CONVENTIONAL:
1388 if (!blk_queue_discard(bdev_get_queue(bdev)))
1389 return 0;
1390 return __queue_discard_cmd(sbi, bdev, lblkstart, blklen);
1391 case BLK_ZONE_TYPE_SEQWRITE_REQ:
1392 case BLK_ZONE_TYPE_SEQWRITE_PREF:
1393 sector = SECTOR_FROM_BLOCK(blkstart);
1394 nr_sects = SECTOR_FROM_BLOCK(blklen);
1395
1396 if (sector & (bdev_zone_sectors(bdev) - 1) ||
1397 nr_sects != bdev_zone_sectors(bdev)) {
1398 f2fs_msg(sbi->sb, KERN_INFO,
1399 "(%d) %s: Unaligned discard attempted (block %x + %x)",
1400 devi, sbi->s_ndevs ? FDEV(devi).path: "",
1401 blkstart, blklen);
1402 return -EIO;
1403 }
1404 trace_f2fs_issue_reset_zone(bdev, blkstart);
1405 return blkdev_reset_zones(bdev, sector,
1406 nr_sects, GFP_NOFS);
1407 default:
1408 /* Unknown zone type: broken device ? */
1409 return -EIO;
1410 }
1411}
1412#endif
1413
1414static int __issue_discard_async(struct f2fs_sb_info *sbi,
1415 struct block_device *bdev, block_t blkstart, block_t blklen)
1416{
1417#ifdef CONFIG_BLK_DEV_ZONED
1418 if (f2fs_sb_mounted_blkzoned(sbi->sb) &&
1419 bdev_zoned_model(bdev) != BLK_ZONED_NONE)
1420 return __f2fs_issue_discard_zone(sbi, bdev, blkstart, blklen);
1421#endif
1422 return __queue_discard_cmd(sbi, bdev, blkstart, blklen);
Chao Yu275b66b2016-08-29 23:58:34 +08001423}
1424
Jaegeuk Kim1e87a782014-04-15 13:57:55 +09001425static int f2fs_issue_discard(struct f2fs_sb_info *sbi,
Jaegeuk Kim37208872013-11-12 16:55:17 +09001426 block_t blkstart, block_t blklen)
1427{
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001428 sector_t start = blkstart, len = 0;
1429 struct block_device *bdev;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001430 struct seg_entry *se;
1431 unsigned int offset;
1432 block_t i;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001433 int err = 0;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001434
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001435 bdev = f2fs_target_device(sbi, blkstart, NULL);
1436
1437 for (i = blkstart; i < blkstart + blklen; i++, len++) {
1438 if (i != start) {
1439 struct block_device *bdev2 =
1440 f2fs_target_device(sbi, i, NULL);
1441
1442 if (bdev2 != bdev) {
1443 err = __issue_discard_async(sbi, bdev,
1444 start, len);
1445 if (err)
1446 return err;
1447 bdev = bdev2;
1448 start = i;
1449 len = 0;
1450 }
1451 }
1452
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001453 se = get_seg_entry(sbi, GET_SEGNO(sbi, i));
1454 offset = GET_BLKOFF_FROM_SEG0(sbi, i);
1455
1456 if (!f2fs_test_and_set_bit(offset, se->discard_map))
1457 sbi->discard_blks--;
1458 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001459
1460 if (len)
1461 err = __issue_discard_async(sbi, bdev, start, len);
1462 return err;
Jaegeuk Kim1e87a782014-04-15 13:57:55 +09001463}
1464
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001465static bool add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc,
1466 bool check_only)
Jaegeuk Kimadf49832014-10-28 22:27:59 -07001467{
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001468 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
1469 int max_blocks = sbi->blocks_per_seg;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001470 struct seg_entry *se = get_seg_entry(sbi, cpc->trim_start);
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001471 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
1472 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001473 unsigned long *discard_map = (unsigned long *)se->discard_map;
Jaegeuk Kim60a3b782015-02-10 16:44:29 -08001474 unsigned long *dmap = SIT_I(sbi)->tmp_map;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001475 unsigned int start = 0, end = -1;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001476 bool force = (cpc->reason & CP_DISCARD);
1477 struct discard_entry *de = NULL;
1478 struct list_head *head = &SM_I(sbi)->dcc_info->entry_list;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001479 int i;
1480
Jaegeuk Kim3e025742016-08-02 10:56:40 -07001481 if (se->valid_blocks == max_blocks || !f2fs_discard_en(sbi))
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001482 return false;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001483
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001484 if (!force) {
1485 if (!test_opt(sbi, DISCARD) || !se->valid_blocks ||
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001486 SM_I(sbi)->dcc_info->nr_discards >=
1487 SM_I(sbi)->dcc_info->max_discards)
1488 return false;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001489 }
1490
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001491 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
1492 for (i = 0; i < entries; i++)
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001493 dmap[i] = force ? ~ckpt_map[i] & ~discard_map[i] :
Jaegeuk Kimd7bc2482014-12-12 13:53:41 -08001494 (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001495
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001496 while (force || SM_I(sbi)->dcc_info->nr_discards <=
1497 SM_I(sbi)->dcc_info->max_discards) {
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001498 start = __find_rev_next_bit(dmap, max_blocks, end + 1);
1499 if (start >= max_blocks)
1500 break;
1501
1502 end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
Yunlei Hec7b41e12016-07-07 12:13:33 +08001503 if (force && start && end != max_blocks
1504 && (end - start) < cpc->trim_minlen)
1505 continue;
1506
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001507 if (check_only)
1508 return true;
1509
1510 if (!de) {
1511 de = f2fs_kmem_cache_alloc(discard_entry_slab,
1512 GFP_F2FS_ZERO);
1513 de->start_blkaddr = START_BLOCK(sbi, cpc->trim_start);
1514 list_add_tail(&de->list, head);
1515 }
1516
1517 for (i = start; i < end; i++)
1518 __set_bit_le(i, (void *)de->discard_map);
1519
1520 SM_I(sbi)->dcc_info->nr_discards += end - start;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001521 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001522 return false;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001523}
1524
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001525void release_discard_addrs(struct f2fs_sb_info *sbi)
1526{
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001527 struct list_head *head = &(SM_I(sbi)->dcc_info->entry_list);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001528 struct discard_entry *entry, *this;
1529
1530 /* drop caches */
1531 list_for_each_entry_safe(entry, this, head, list) {
1532 list_del(&entry->list);
1533 kmem_cache_free(discard_entry_slab, entry);
1534 }
1535}
1536
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001537/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001538 * Should call clear_prefree_segments after checkpoint is done.
1539 */
1540static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
1541{
1542 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Chao Yub65ee142014-08-04 10:10:07 +08001543 unsigned int segno;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001544
1545 mutex_lock(&dirty_i->seglist_lock);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001546 for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001547 __set_test_and_free(sbi, segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001548 mutex_unlock(&dirty_i->seglist_lock);
1549}
1550
Jaegeuk Kim836b5a62015-04-30 22:50:06 -07001551void clear_prefree_segments(struct f2fs_sb_info *sbi, struct cp_control *cpc)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001552{
Chao Yuefe24da2017-08-07 23:09:56 +08001553 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1554 struct list_head *head = &dcc->entry_list;
Chao Yu2d7b8222014-03-29 11:33:17 +08001555 struct discard_entry *entry, *this;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001556 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Changman Lee29e59c12013-11-11 09:24:37 +09001557 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
Changman Lee29e59c12013-11-11 09:24:37 +09001558 unsigned int start = 0, end = -1;
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001559 unsigned int secno, start_segno;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001560 bool force = (cpc->reason & CP_DISCARD);
Chao Yu275b66b2016-08-29 23:58:34 +08001561
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001562 mutex_lock(&dirty_i->seglist_lock);
Changman Lee29e59c12013-11-11 09:24:37 +09001563
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001564 while (1) {
Changman Lee29e59c12013-11-11 09:24:37 +09001565 int i;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001566 start = find_next_bit(prefree_map, MAIN_SEGS(sbi), end + 1);
1567 if (start >= MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001568 break;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001569 end = find_next_zero_bit(prefree_map, MAIN_SEGS(sbi),
1570 start + 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001571
Changman Lee29e59c12013-11-11 09:24:37 +09001572 for (i = start; i < end; i++)
1573 clear_bit(i, prefree_map);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001574
Changman Lee29e59c12013-11-11 09:24:37 +09001575 dirty_i->nr_dirty[PRE] -= end - start;
1576
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001577 if (!test_opt(sbi, DISCARD))
Changman Lee29e59c12013-11-11 09:24:37 +09001578 continue;
1579
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001580 if (force && start >= cpc->trim_start &&
1581 (end - 1) <= cpc->trim_end)
1582 continue;
1583
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001584 if (!test_opt(sbi, LFS) || sbi->segs_per_sec == 1) {
1585 f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
Jaegeuk Kim37208872013-11-12 16:55:17 +09001586 (end - start) << sbi->log_blocks_per_seg);
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001587 continue;
1588 }
1589next:
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001590 secno = GET_SEC_FROM_SEG(sbi, start);
1591 start_segno = GET_SEG_FROM_SEC(sbi, secno);
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001592 if (!IS_CURSEC(sbi, secno) &&
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001593 !get_valid_blocks(sbi, start, true))
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001594 f2fs_issue_discard(sbi, START_BLOCK(sbi, start_segno),
1595 sbi->segs_per_sec << sbi->log_blocks_per_seg);
1596
1597 start = start_segno + sbi->segs_per_sec;
1598 if (start < end)
1599 goto next;
Jaegeuk Kim8c53efc32017-02-27 11:57:11 -08001600 else
1601 end = start - 1;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001602 }
1603 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001604
1605 /* send small discards */
Chao Yu2d7b8222014-03-29 11:33:17 +08001606 list_for_each_entry_safe(entry, this, head, list) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001607 unsigned int cur_pos = 0, next_pos, len, total_len = 0;
1608 bool is_valid = test_bit_le(0, entry->discard_map);
1609
1610find_next:
1611 if (is_valid) {
1612 next_pos = find_next_zero_bit_le(entry->discard_map,
1613 sbi->blocks_per_seg, cur_pos);
1614 len = next_pos - cur_pos;
1615
Damien Le Moal57a91232017-05-26 17:04:40 +09001616 if (f2fs_sb_mounted_blkzoned(sbi->sb) ||
1617 (force && len < cpc->trim_minlen))
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001618 goto skip;
1619
1620 f2fs_issue_discard(sbi, entry->start_blkaddr + cur_pos,
1621 len);
1622 cpc->trimmed += len;
1623 total_len += len;
1624 } else {
1625 next_pos = find_next_bit_le(entry->discard_map,
1626 sbi->blocks_per_seg, cur_pos);
1627 }
Jaegeuk Kim836b5a62015-04-30 22:50:06 -07001628skip:
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001629 cur_pos = next_pos;
1630 is_valid = !is_valid;
1631
1632 if (cur_pos < sbi->blocks_per_seg)
1633 goto find_next;
1634
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001635 list_del(&entry->list);
Chao Yuefe24da2017-08-07 23:09:56 +08001636 dcc->nr_discards -= total_len;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001637 kmem_cache_free(discard_entry_slab, entry);
1638 }
Chao Yu275b66b2016-08-29 23:58:34 +08001639
Jaegeuk Kim200ad892017-08-22 21:15:43 -07001640 wake_up_discard_thread(sbi, false);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001641}
1642
Chao Yudaf437d2017-10-04 09:08:34 +08001643void init_discard_policy(struct discard_policy *dpolicy,
1644 int discard_type, unsigned int granularity)
Chao Yu69a59672017-10-04 09:08:33 +08001645{
Chao Yudaf437d2017-10-04 09:08:34 +08001646 /* common policy */
1647 dpolicy->type = discard_type;
Chao Yu69a59672017-10-04 09:08:33 +08001648 dpolicy->sync = true;
Chao Yudaf437d2017-10-04 09:08:34 +08001649 dpolicy->granularity = granularity;
1650
1651 if (discard_type == DPOLICY_BG) {
1652 dpolicy->min_interval = DEF_MIN_DISCARD_ISSUE_TIME;
1653 dpolicy->max_interval = DEF_MAX_DISCARD_ISSUE_TIME;
1654 dpolicy->max_requests = DEF_MAX_DISCARD_REQUEST;
1655 dpolicy->io_aware_gran = MAX_PLIST_NUM;
1656 dpolicy->io_aware = true;
1657 } else if (discard_type == DPOLICY_FORCE) {
1658 dpolicy->min_interval = DEF_MIN_DISCARD_ISSUE_TIME;
1659 dpolicy->max_interval = DEF_MAX_DISCARD_ISSUE_TIME;
1660 dpolicy->max_requests = DEF_MAX_DISCARD_REQUEST;
1661 dpolicy->io_aware_gran = MAX_PLIST_NUM;
1662 dpolicy->io_aware = true;
1663 } else if (discard_type == DPOLICY_FSTRIM) {
1664 dpolicy->max_requests = DEF_MAX_DISCARD_REQUEST;
1665 dpolicy->io_aware_gran = MAX_PLIST_NUM;
1666 dpolicy->io_aware = false;
1667 } else if (discard_type == DPOLICY_UMOUNT) {
1668 dpolicy->max_requests = DEF_MAX_DISCARD_REQUEST;
1669 dpolicy->io_aware_gran = MAX_PLIST_NUM;
1670 dpolicy->io_aware = false;
1671 }
Chao Yu69a59672017-10-04 09:08:33 +08001672}
1673
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001674static int create_discard_cmd_control(struct f2fs_sb_info *sbi)
1675{
1676 dev_t dev = sbi->sb->s_bdev->bd_dev;
1677 struct discard_cmd_control *dcc;
1678 int err = 0, i;
1679
1680 if (SM_I(sbi)->dcc_info) {
1681 dcc = SM_I(sbi)->dcc_info;
1682 goto init_thread;
1683 }
1684
1685 dcc = kzalloc(sizeof(struct discard_cmd_control), GFP_KERNEL);
1686 if (!dcc)
1687 return -ENOMEM;
1688
Chao Yuefe24da2017-08-07 23:09:56 +08001689 dcc->discard_granularity = DEFAULT_DISCARD_GRANULARITY;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001690 INIT_LIST_HEAD(&dcc->entry_list);
Chao Yudaf437d2017-10-04 09:08:34 +08001691 for (i = 0; i < MAX_PLIST_NUM; i++)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001692 INIT_LIST_HEAD(&dcc->pend_list[i]);
1693 INIT_LIST_HEAD(&dcc->wait_list);
Chao Yu28e10232017-10-04 09:08:32 +08001694 INIT_LIST_HEAD(&dcc->fstrim_list);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001695 mutex_init(&dcc->cmd_lock);
1696 atomic_set(&dcc->issued_discard, 0);
1697 atomic_set(&dcc->issing_discard, 0);
1698 atomic_set(&dcc->discard_cmd_cnt, 0);
1699 dcc->nr_discards = 0;
1700 dcc->max_discards = MAIN_SEGS(sbi) << sbi->log_blocks_per_seg;
1701 dcc->undiscard_blks = 0;
1702 dcc->root = RB_ROOT;
1703
1704 init_waitqueue_head(&dcc->discard_wait_queue);
1705 SM_I(sbi)->dcc_info = dcc;
1706init_thread:
1707 dcc->f2fs_issue_discard = kthread_run(issue_discard_thread, sbi,
1708 "f2fs_discard-%u:%u", MAJOR(dev), MINOR(dev));
1709 if (IS_ERR(dcc->f2fs_issue_discard)) {
1710 err = PTR_ERR(dcc->f2fs_issue_discard);
1711 kfree(dcc);
1712 SM_I(sbi)->dcc_info = NULL;
1713 return err;
1714 }
1715
1716 return err;
1717}
1718
1719static void destroy_discard_cmd_control(struct f2fs_sb_info *sbi)
1720{
1721 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1722
1723 if (!dcc)
1724 return;
1725
Chao Yud9d85cc2017-06-29 23:17:45 +08001726 stop_discard_thread(sbi);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001727
1728 kfree(dcc);
1729 SM_I(sbi)->dcc_info = NULL;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001730}
1731
Chao Yu184a5cd2014-09-04 18:13:01 +08001732static bool __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001733{
1734 struct sit_info *sit_i = SIT_I(sbi);
Chao Yu184a5cd2014-09-04 18:13:01 +08001735
1736 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001737 sit_i->dirty_sentries++;
Chao Yu184a5cd2014-09-04 18:13:01 +08001738 return false;
1739 }
1740
1741 return true;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001742}
1743
1744static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
1745 unsigned int segno, int modified)
1746{
1747 struct seg_entry *se = get_seg_entry(sbi, segno);
1748 se->type = type;
1749 if (modified)
1750 __mark_sit_entry_dirty(sbi, segno);
1751}
1752
1753static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
1754{
1755 struct seg_entry *se;
1756 unsigned int segno, offset;
1757 long int new_vblocks;
Yunlong Song5ccdd092017-08-02 21:20:13 +08001758 bool exist;
1759#ifdef CONFIG_F2FS_CHECK_FS
1760 bool mir_exist;
1761#endif
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001762
1763 segno = GET_SEGNO(sbi, blkaddr);
1764
1765 se = get_seg_entry(sbi, segno);
1766 new_vblocks = se->valid_blocks + del;
Jaegeuk Kim491c0852014-02-04 13:01:10 +09001767 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001768
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001769 f2fs_bug_on(sbi, (new_vblocks >> (sizeof(unsigned short) << 3) ||
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001770 (new_vblocks > sbi->blocks_per_seg)));
1771
1772 se->valid_blocks = new_vblocks;
1773 se->mtime = get_mtime(sbi);
1774 SIT_I(sbi)->max_mtime = se->mtime;
1775
1776 /* Update valid block bitmap */
1777 if (del > 0) {
Yunlong Song5ccdd092017-08-02 21:20:13 +08001778 exist = f2fs_test_and_set_bit(offset, se->cur_valid_map);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001779#ifdef CONFIG_F2FS_CHECK_FS
Yunlong Song5ccdd092017-08-02 21:20:13 +08001780 mir_exist = f2fs_test_and_set_bit(offset,
1781 se->cur_valid_map_mir);
1782 if (unlikely(exist != mir_exist)) {
1783 f2fs_msg(sbi->sb, KERN_ERR, "Inconsistent error "
1784 "when setting bitmap, blk:%u, old bit:%d",
1785 blkaddr, exist);
Jaegeuk Kim05796762014-09-02 16:05:00 -07001786 f2fs_bug_on(sbi, 1);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001787 }
Yunlong Song5ccdd092017-08-02 21:20:13 +08001788#endif
1789 if (unlikely(exist)) {
1790 f2fs_msg(sbi->sb, KERN_ERR,
1791 "Bitmap was wrongly set, blk:%u", blkaddr);
1792 f2fs_bug_on(sbi, 1);
Yunlong Song5fd827b2017-08-02 22:16:54 +08001793 se->valid_blocks--;
1794 del = 0;
Yunlong Song5ccdd092017-08-02 21:20:13 +08001795 }
1796
Jaegeuk Kim3e025742016-08-02 10:56:40 -07001797 if (f2fs_discard_en(sbi) &&
1798 !f2fs_test_and_set_bit(offset, se->discard_map))
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001799 sbi->discard_blks--;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001800
1801 /* don't overwrite by SSR to keep node chain */
1802 if (se->type == CURSEG_WARM_NODE) {
1803 if (!f2fs_test_and_set_bit(offset, se->ckpt_valid_map))
1804 se->ckpt_valid_blocks++;
1805 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001806 } else {
Yunlong Song5ccdd092017-08-02 21:20:13 +08001807 exist = f2fs_test_and_clear_bit(offset, se->cur_valid_map);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001808#ifdef CONFIG_F2FS_CHECK_FS
Yunlong Song5ccdd092017-08-02 21:20:13 +08001809 mir_exist = f2fs_test_and_clear_bit(offset,
1810 se->cur_valid_map_mir);
1811 if (unlikely(exist != mir_exist)) {
1812 f2fs_msg(sbi->sb, KERN_ERR, "Inconsistent error "
1813 "when clearing bitmap, blk:%u, old bit:%d",
1814 blkaddr, exist);
Jaegeuk Kim05796762014-09-02 16:05:00 -07001815 f2fs_bug_on(sbi, 1);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001816 }
Yunlong Song5ccdd092017-08-02 21:20:13 +08001817#endif
1818 if (unlikely(!exist)) {
1819 f2fs_msg(sbi->sb, KERN_ERR,
1820 "Bitmap was wrongly cleared, blk:%u", blkaddr);
1821 f2fs_bug_on(sbi, 1);
Yunlong Song5fd827b2017-08-02 22:16:54 +08001822 se->valid_blocks++;
1823 del = 0;
Yunlong Song5ccdd092017-08-02 21:20:13 +08001824 }
1825
Jaegeuk Kim3e025742016-08-02 10:56:40 -07001826 if (f2fs_discard_en(sbi) &&
1827 f2fs_test_and_clear_bit(offset, se->discard_map))
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001828 sbi->discard_blks++;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001829 }
1830 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
1831 se->ckpt_valid_blocks += del;
1832
1833 __mark_sit_entry_dirty(sbi, segno);
1834
1835 /* update total number of valid blocks to be written in ckpt area */
1836 SIT_I(sbi)->written_valid_blocks += del;
1837
1838 if (sbi->segs_per_sec > 1)
1839 get_sec_entry(sbi, segno)->valid_blocks += del;
1840}
1841
Jaegeuk Kim5e443812014-01-28 12:22:14 +09001842void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001843{
Jaegeuk Kim5e443812014-01-28 12:22:14 +09001844 update_sit_entry(sbi, new, 1);
1845 if (GET_SEGNO(sbi, old) != NULL_SEGNO)
1846 update_sit_entry(sbi, old, -1);
1847
1848 locate_dirty_segment(sbi, GET_SEGNO(sbi, old));
1849 locate_dirty_segment(sbi, GET_SEGNO(sbi, new));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001850}
1851
1852void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
1853{
1854 unsigned int segno = GET_SEGNO(sbi, addr);
1855 struct sit_info *sit_i = SIT_I(sbi);
1856
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001857 f2fs_bug_on(sbi, addr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001858 if (addr == NEW_ADDR)
1859 return;
1860
1861 /* add it into sit main buffer */
1862 mutex_lock(&sit_i->sentry_lock);
1863
1864 update_sit_entry(sbi, addr, -1);
1865
1866 /* add it into dirty seglist */
1867 locate_dirty_segment(sbi, segno);
1868
1869 mutex_unlock(&sit_i->sentry_lock);
1870}
1871
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001872bool is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr)
1873{
1874 struct sit_info *sit_i = SIT_I(sbi);
1875 unsigned int segno, offset;
1876 struct seg_entry *se;
1877 bool is_cp = false;
1878
1879 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR)
1880 return true;
1881
1882 mutex_lock(&sit_i->sentry_lock);
1883
1884 segno = GET_SEGNO(sbi, blkaddr);
1885 se = get_seg_entry(sbi, segno);
1886 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
1887
1888 if (f2fs_test_bit(offset, se->ckpt_valid_map))
1889 is_cp = true;
1890
1891 mutex_unlock(&sit_i->sentry_lock);
1892
1893 return is_cp;
1894}
1895
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001896/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001897 * This function should be resided under the curseg_mutex lock
1898 */
1899static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
Haicheng Lie79efe32013-06-13 16:59:27 +08001900 struct f2fs_summary *sum)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001901{
1902 struct curseg_info *curseg = CURSEG_I(sbi, type);
1903 void *addr = curseg->sum_blk;
Haicheng Lie79efe32013-06-13 16:59:27 +08001904 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001905 memcpy(addr, sum, sizeof(struct f2fs_summary));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001906}
1907
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001908/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001909 * Calculate the number of current summary pages for writing
1910 */
Chao Yu3fa06d72014-12-09 14:21:46 +08001911int npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001912{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001913 int valid_sum_count = 0;
Fan Li9a479382013-10-29 16:21:47 +08001914 int i, sum_in_page;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001915
1916 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1917 if (sbi->ckpt->alloc_type[i] == SSR)
1918 valid_sum_count += sbi->blocks_per_seg;
Chao Yu3fa06d72014-12-09 14:21:46 +08001919 else {
1920 if (for_ra)
1921 valid_sum_count += le16_to_cpu(
1922 F2FS_CKPT(sbi)->cur_data_blkoff[i]);
1923 else
1924 valid_sum_count += curseg_blkoff(sbi, i);
1925 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001926 }
1927
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001928 sum_in_page = (PAGE_SIZE - 2 * SUM_JOURNAL_SIZE -
Fan Li9a479382013-10-29 16:21:47 +08001929 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
1930 if (valid_sum_count <= sum_in_page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001931 return 1;
Fan Li9a479382013-10-29 16:21:47 +08001932 else if ((valid_sum_count - sum_in_page) <=
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001933 (PAGE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001934 return 2;
1935 return 3;
1936}
1937
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001938/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001939 * Caller should put this summary page
1940 */
1941struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
1942{
1943 return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
1944}
1945
Chao Yu381722d2015-05-19 17:40:04 +08001946void update_meta_page(struct f2fs_sb_info *sbi, void *src, block_t blk_addr)
1947{
1948 struct page *page = grab_meta_page(sbi, blk_addr);
1949 void *dst = page_address(page);
1950
1951 if (src)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001952 memcpy(dst, src, PAGE_SIZE);
Chao Yu381722d2015-05-19 17:40:04 +08001953 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001954 memset(dst, 0, PAGE_SIZE);
Chao Yu381722d2015-05-19 17:40:04 +08001955 set_page_dirty(page);
1956 f2fs_put_page(page, 1);
1957}
1958
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001959static void write_sum_page(struct f2fs_sb_info *sbi,
1960 struct f2fs_summary_block *sum_blk, block_t blk_addr)
1961{
Chao Yu381722d2015-05-19 17:40:04 +08001962 update_meta_page(sbi, (void *)sum_blk, blk_addr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001963}
1964
Chao Yub7ad7512016-02-19 18:08:46 +08001965static void write_current_sum_page(struct f2fs_sb_info *sbi,
1966 int type, block_t blk_addr)
1967{
1968 struct curseg_info *curseg = CURSEG_I(sbi, type);
1969 struct page *page = grab_meta_page(sbi, blk_addr);
1970 struct f2fs_summary_block *src = curseg->sum_blk;
1971 struct f2fs_summary_block *dst;
1972
1973 dst = (struct f2fs_summary_block *)page_address(page);
1974
1975 mutex_lock(&curseg->curseg_mutex);
1976
1977 down_read(&curseg->journal_rwsem);
1978 memcpy(&dst->journal, curseg->journal, SUM_JOURNAL_SIZE);
1979 up_read(&curseg->journal_rwsem);
1980
1981 memcpy(dst->entries, src->entries, SUM_ENTRY_SIZE);
1982 memcpy(&dst->footer, &src->footer, SUM_FOOTER_SIZE);
1983
1984 mutex_unlock(&curseg->curseg_mutex);
1985
1986 set_page_dirty(page);
1987 f2fs_put_page(page, 1);
1988}
1989
Jaegeuk Kim60374682013-03-31 13:58:51 +09001990static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
1991{
1992 struct curseg_info *curseg = CURSEG_I(sbi, type);
Haicheng Li81fb5e82013-05-14 18:20:28 +08001993 unsigned int segno = curseg->segno + 1;
Jaegeuk Kim60374682013-03-31 13:58:51 +09001994 struct free_segmap_info *free_i = FREE_I(sbi);
1995
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001996 if (segno < MAIN_SEGS(sbi) && segno % sbi->segs_per_sec)
Haicheng Li81fb5e82013-05-14 18:20:28 +08001997 return !test_bit(segno, free_i->free_segmap);
Jaegeuk Kim60374682013-03-31 13:58:51 +09001998 return 0;
1999}
2000
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002001/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002002 * Find a new segment from the free segments bitmap to right order
2003 * This function should be returned with success, otherwise BUG
2004 */
2005static void get_new_segment(struct f2fs_sb_info *sbi,
2006 unsigned int *newseg, bool new_sec, int dir)
2007{
2008 struct free_segmap_info *free_i = FREE_I(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002009 unsigned int segno, secno, zoneno;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002010 unsigned int total_zones = MAIN_SECS(sbi) / sbi->secs_per_zone;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002011 unsigned int hint = GET_SEC_FROM_SEG(sbi, *newseg);
2012 unsigned int old_zoneno = GET_ZONE_FROM_SEG(sbi, *newseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002013 unsigned int left_start = hint;
2014 bool init = true;
2015 int go_left = 0;
2016 int i;
2017
Chao Yu1a118cc2015-02-11 18:20:38 +08002018 spin_lock(&free_i->segmap_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002019
2020 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
2021 segno = find_next_zero_bit(free_i->free_segmap,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002022 GET_SEG_FROM_SEC(sbi, hint + 1), *newseg + 1);
2023 if (segno < GET_SEG_FROM_SEC(sbi, hint + 1))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002024 goto got_it;
2025 }
2026find_other_zone:
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002027 secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint);
2028 if (secno >= MAIN_SECS(sbi)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002029 if (dir == ALLOC_RIGHT) {
2030 secno = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002031 MAIN_SECS(sbi), 0);
2032 f2fs_bug_on(sbi, secno >= MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002033 } else {
2034 go_left = 1;
2035 left_start = hint - 1;
2036 }
2037 }
2038 if (go_left == 0)
2039 goto skip_left;
2040
2041 while (test_bit(left_start, free_i->free_secmap)) {
2042 if (left_start > 0) {
2043 left_start--;
2044 continue;
2045 }
2046 left_start = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002047 MAIN_SECS(sbi), 0);
2048 f2fs_bug_on(sbi, left_start >= MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002049 break;
2050 }
2051 secno = left_start;
2052skip_left:
2053 hint = secno;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002054 segno = GET_SEG_FROM_SEC(sbi, secno);
2055 zoneno = GET_ZONE_FROM_SEC(sbi, secno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002056
2057 /* give up on finding another zone */
2058 if (!init)
2059 goto got_it;
2060 if (sbi->secs_per_zone == 1)
2061 goto got_it;
2062 if (zoneno == old_zoneno)
2063 goto got_it;
2064 if (dir == ALLOC_LEFT) {
2065 if (!go_left && zoneno + 1 >= total_zones)
2066 goto got_it;
2067 if (go_left && zoneno == 0)
2068 goto got_it;
2069 }
2070 for (i = 0; i < NR_CURSEG_TYPE; i++)
2071 if (CURSEG_I(sbi, i)->zone == zoneno)
2072 break;
2073
2074 if (i < NR_CURSEG_TYPE) {
2075 /* zone is in user, try another */
2076 if (go_left)
2077 hint = zoneno * sbi->secs_per_zone - 1;
2078 else if (zoneno + 1 >= total_zones)
2079 hint = 0;
2080 else
2081 hint = (zoneno + 1) * sbi->secs_per_zone;
2082 init = false;
2083 goto find_other_zone;
2084 }
2085got_it:
2086 /* set it as dirty segment in free segmap */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002087 f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002088 __set_inuse(sbi, segno);
2089 *newseg = segno;
Chao Yu1a118cc2015-02-11 18:20:38 +08002090 spin_unlock(&free_i->segmap_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002091}
2092
2093static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
2094{
2095 struct curseg_info *curseg = CURSEG_I(sbi, type);
2096 struct summary_footer *sum_footer;
2097
2098 curseg->segno = curseg->next_segno;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002099 curseg->zone = GET_ZONE_FROM_SEG(sbi, curseg->segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002100 curseg->next_blkoff = 0;
2101 curseg->next_segno = NULL_SEGNO;
2102
2103 sum_footer = &(curseg->sum_blk->footer);
2104 memset(sum_footer, 0, sizeof(struct summary_footer));
2105 if (IS_DATASEG(type))
2106 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
2107 if (IS_NODESEG(type))
2108 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
2109 __set_sit_entry_type(sbi, type, curseg->segno, modified);
2110}
2111
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002112static unsigned int __get_next_segno(struct f2fs_sb_info *sbi, int type)
2113{
2114 /* if segs_per_sec is large than 1, we need to keep original policy. */
2115 if (sbi->segs_per_sec != 1)
2116 return CURSEG_I(sbi, type)->segno;
2117
2118 if (type == CURSEG_HOT_DATA || IS_NODESEG(type))
2119 return 0;
2120
2121 if (SIT_I(sbi)->last_victim[ALLOC_NEXT])
2122 return SIT_I(sbi)->last_victim[ALLOC_NEXT];
2123 return CURSEG_I(sbi, type)->segno;
2124}
2125
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002126/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002127 * Allocate a current working segment.
2128 * This function always allocates a free segment in LFS manner.
2129 */
2130static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
2131{
2132 struct curseg_info *curseg = CURSEG_I(sbi, type);
2133 unsigned int segno = curseg->segno;
2134 int dir = ALLOC_LEFT;
2135
2136 write_sum_page(sbi, curseg->sum_blk,
Haicheng Li81fb5e82013-05-14 18:20:28 +08002137 GET_SUM_BLOCK(sbi, segno));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002138 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
2139 dir = ALLOC_RIGHT;
2140
2141 if (test_opt(sbi, NOHEAP))
2142 dir = ALLOC_RIGHT;
2143
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002144 segno = __get_next_segno(sbi, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002145 get_new_segment(sbi, &segno, new_sec, dir);
2146 curseg->next_segno = segno;
2147 reset_curseg(sbi, type, 1);
2148 curseg->alloc_type = LFS;
2149}
2150
2151static void __next_free_blkoff(struct f2fs_sb_info *sbi,
2152 struct curseg_info *seg, block_t start)
2153{
2154 struct seg_entry *se = get_seg_entry(sbi, seg->segno);
Changman Leee81c93c2013-11-15 13:21:16 +09002155 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
Jaegeuk Kim60a3b782015-02-10 16:44:29 -08002156 unsigned long *target_map = SIT_I(sbi)->tmp_map;
Changman Leee81c93c2013-11-15 13:21:16 +09002157 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
2158 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
2159 int i, pos;
2160
2161 for (i = 0; i < entries; i++)
2162 target_map[i] = ckpt_map[i] | cur_map[i];
2163
2164 pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
2165
2166 seg->next_blkoff = pos;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002167}
2168
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002169/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002170 * If a segment is written by LFS manner, next block offset is just obtained
2171 * by increasing the current block offset. However, if a segment is written by
2172 * SSR manner, next block offset obtained by calling __next_free_blkoff
2173 */
2174static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
2175 struct curseg_info *seg)
2176{
2177 if (seg->alloc_type == SSR)
2178 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
2179 else
2180 seg->next_blkoff++;
2181}
2182
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002183/*
arter97e1c42042014-08-06 23:22:50 +09002184 * This function always allocates a used segment(from dirty seglist) by SSR
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002185 * manner, so it should recover the existing segment information of valid blocks
2186 */
Chao Yua69c9742017-08-30 18:04:48 +08002187static void change_curseg(struct f2fs_sb_info *sbi, int type)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002188{
2189 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2190 struct curseg_info *curseg = CURSEG_I(sbi, type);
2191 unsigned int new_segno = curseg->next_segno;
2192 struct f2fs_summary_block *sum_node;
2193 struct page *sum_page;
2194
2195 write_sum_page(sbi, curseg->sum_blk,
2196 GET_SUM_BLOCK(sbi, curseg->segno));
2197 __set_test_and_inuse(sbi, new_segno);
2198
2199 mutex_lock(&dirty_i->seglist_lock);
2200 __remove_dirty_segment(sbi, new_segno, PRE);
2201 __remove_dirty_segment(sbi, new_segno, DIRTY);
2202 mutex_unlock(&dirty_i->seglist_lock);
2203
2204 reset_curseg(sbi, type, 1);
2205 curseg->alloc_type = SSR;
2206 __next_free_blkoff(sbi, curseg, 0);
2207
Chao Yua69c9742017-08-30 18:04:48 +08002208 sum_page = get_sum_page(sbi, new_segno);
2209 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
2210 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
2211 f2fs_put_page(sum_page, 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002212}
2213
Jaegeuk Kim43727522013-02-04 15:11:17 +09002214static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
2215{
2216 struct curseg_info *curseg = CURSEG_I(sbi, type);
2217 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002218 unsigned segno = NULL_SEGNO;
2219 int i, cnt;
2220 bool reversed = false;
Jaegeuk Kim43727522013-02-04 15:11:17 +09002221
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002222 /* need_SSR() already forces to do this */
2223 if (v_ops->get_victim(sbi, &segno, BG_GC, type, SSR)) {
2224 curseg->next_segno = segno;
2225 return 1;
2226 }
Jaegeuk Kim43727522013-02-04 15:11:17 +09002227
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002228 /* For node segments, let's do SSR more intensively */
2229 if (IS_NODESEG(type)) {
2230 if (type >= CURSEG_WARM_NODE) {
2231 reversed = true;
2232 i = CURSEG_COLD_NODE;
2233 } else {
2234 i = CURSEG_HOT_NODE;
2235 }
2236 cnt = NR_CURSEG_NODE_TYPE;
2237 } else {
2238 if (type >= CURSEG_WARM_DATA) {
2239 reversed = true;
2240 i = CURSEG_COLD_DATA;
2241 } else {
2242 i = CURSEG_HOT_DATA;
2243 }
2244 cnt = NR_CURSEG_DATA_TYPE;
2245 }
2246
2247 for (; cnt-- > 0; reversed ? i-- : i++) {
2248 if (i == type)
2249 continue;
2250 if (v_ops->get_victim(sbi, &segno, BG_GC, i, SSR)) {
2251 curseg->next_segno = segno;
Jaegeuk Kim43727522013-02-04 15:11:17 +09002252 return 1;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002253 }
2254 }
Jaegeuk Kim43727522013-02-04 15:11:17 +09002255 return 0;
2256}
2257
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002258/*
2259 * flush out current segment and replace it with new segment
2260 * This function should be returned with success, otherwise BUG
2261 */
2262static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
2263 int type, bool force)
2264{
2265 struct curseg_info *curseg = CURSEG_I(sbi, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002266
Gu Zheng7b405272013-08-19 09:41:15 +08002267 if (force)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002268 new_curseg(sbi, type, true);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002269 else if (!is_set_ckpt_flags(sbi, CP_CRC_RECOVERY_FLAG) &&
2270 type == CURSEG_WARM_NODE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002271 new_curseg(sbi, type, false);
Jaegeuk Kim60374682013-03-31 13:58:51 +09002272 else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
2273 new_curseg(sbi, type, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002274 else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
Chao Yua69c9742017-08-30 18:04:48 +08002275 change_curseg(sbi, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002276 else
2277 new_curseg(sbi, type, false);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +09002278
2279 stat_inc_seg_type(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002280}
2281
2282void allocate_new_segments(struct f2fs_sb_info *sbi)
2283{
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002284 struct curseg_info *curseg;
2285 unsigned int old_segno;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002286 int i;
2287
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002288 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
2289 curseg = CURSEG_I(sbi, i);
2290 old_segno = curseg->segno;
2291 SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
2292 locate_dirty_segment(sbi, old_segno);
2293 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002294}
2295
2296static const struct segment_allocation default_salloc_ops = {
2297 .allocate_segment = allocate_segment_by_default,
2298};
2299
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002300bool exist_trim_candidates(struct f2fs_sb_info *sbi, struct cp_control *cpc)
2301{
2302 __u64 trim_start = cpc->trim_start;
2303 bool has_candidate = false;
2304
2305 mutex_lock(&SIT_I(sbi)->sentry_lock);
2306 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++) {
2307 if (add_discard_addrs(sbi, cpc, true)) {
2308 has_candidate = true;
2309 break;
2310 }
2311 }
2312 mutex_unlock(&SIT_I(sbi)->sentry_lock);
2313
2314 cpc->trim_start = trim_start;
2315 return has_candidate;
2316}
2317
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002318int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range)
2319{
Jaegeuk Kimf7ef9b82015-02-09 12:02:44 -08002320 __u64 start = F2FS_BYTES_TO_BLK(range->start);
2321 __u64 end = start + F2FS_BYTES_TO_BLK(range->len) - 1;
Chao Yu28e10232017-10-04 09:08:32 +08002322 unsigned int start_segno, end_segno, cur_segno;
2323 block_t start_block, end_block;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002324 struct cp_control cpc;
Chao Yudaf437d2017-10-04 09:08:34 +08002325 struct discard_policy dpolicy;
Chao Yuc34f42e2015-12-23 17:50:30 +08002326 int err = 0;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002327
Jaegeuk Kim836b5a62015-04-30 22:50:06 -07002328 if (start >= MAX_BLKADDR(sbi) || range->len < sbi->blocksize)
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002329 return -EINVAL;
2330
Jan Kara9bd27ae2014-10-21 14:07:33 +02002331 cpc.trimmed = 0;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002332 if (end <= MAIN_BLKADDR(sbi))
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002333 goto out;
2334
Yunlei Heed214a12016-09-01 10:14:39 +08002335 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) {
2336 f2fs_msg(sbi->sb, KERN_WARNING,
2337 "Found FS corruption, run fsck to fix.");
2338 goto out;
2339 }
2340
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002341 /* start/end segment number in main_area */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002342 start_segno = (start <= MAIN_BLKADDR(sbi)) ? 0 : GET_SEGNO(sbi, start);
2343 end_segno = (end >= MAX_BLKADDR(sbi)) ? MAIN_SEGS(sbi) - 1 :
2344 GET_SEGNO(sbi, end);
Chao Yu28e10232017-10-04 09:08:32 +08002345
2346 start_block = START_BLOCK(sbi, start_segno);
2347 end_block = START_BLOCK(sbi, end_segno + 1);
2348
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002349 cpc.reason = CP_DISCARD;
Jaegeuk Kim836b5a62015-04-30 22:50:06 -07002350 cpc.trim_minlen = max_t(__u64, 1, F2FS_BYTES_TO_BLK(range->minlen));
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002351
2352 /* do checkpoint to issue discard commands safely */
Chao Yu28e10232017-10-04 09:08:32 +08002353 for (cur_segno = start_segno; cur_segno <= end_segno;
2354 cur_segno = cpc.trim_end + 1) {
2355 cpc.trim_start = cur_segno;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07002356
2357 if (sbi->discard_blks == 0)
2358 break;
2359 else if (sbi->discard_blks < BATCHED_TRIM_BLOCKS(sbi))
2360 cpc.trim_end = end_segno;
2361 else
2362 cpc.trim_end = min_t(unsigned int,
Chao Yu28e10232017-10-04 09:08:32 +08002363 rounddown(cur_segno +
Jaegeuk Kimbba681c2015-01-26 17:41:23 -08002364 BATCHED_TRIM_SEGMENTS(sbi),
2365 sbi->segs_per_sec) - 1, end_segno);
2366
2367 mutex_lock(&sbi->gc_mutex);
Chao Yuc34f42e2015-12-23 17:50:30 +08002368 err = write_checkpoint(sbi, &cpc);
Jaegeuk Kimbba681c2015-01-26 17:41:23 -08002369 mutex_unlock(&sbi->gc_mutex);
Chao Yue9328352016-08-21 23:21:29 +08002370 if (err)
2371 break;
Chao Yu74fa5f32016-08-21 23:21:30 +08002372
2373 schedule();
Jaegeuk Kimbba681c2015-01-26 17:41:23 -08002374 }
Chao Yu28e10232017-10-04 09:08:32 +08002375
2376 start_block = START_BLOCK(sbi, start_segno);
2377 end_block = START_BLOCK(sbi, min(cur_segno, end_segno) + 1);
2378
Chao Yudaf437d2017-10-04 09:08:34 +08002379 init_discard_policy(&dpolicy, DPOLICY_FSTRIM, cpc.trim_minlen);
2380 __issue_discard_cmd_range(sbi, &dpolicy, start_block, end_block);
2381 __wait_discard_cmd_range(sbi, &dpolicy, start_block, end_block);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002382out:
Jaegeuk Kimf7ef9b82015-02-09 12:02:44 -08002383 range->len = F2FS_BLK_TO_BYTES(cpc.trimmed);
Chao Yuc34f42e2015-12-23 17:50:30 +08002384 return err;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002385}
2386
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002387static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
2388{
2389 struct curseg_info *curseg = CURSEG_I(sbi, type);
2390 if (curseg->next_blkoff < sbi->blocks_per_seg)
2391 return true;
2392 return false;
2393}
2394
Jaegeuk Kim72cc4582017-05-10 14:19:54 -07002395static int __get_segment_type_2(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002396{
Jaegeuk Kim72cc4582017-05-10 14:19:54 -07002397 if (fio->type == DATA)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002398 return CURSEG_HOT_DATA;
2399 else
2400 return CURSEG_HOT_NODE;
2401}
2402
Jaegeuk Kim72cc4582017-05-10 14:19:54 -07002403static int __get_segment_type_4(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002404{
Jaegeuk Kim72cc4582017-05-10 14:19:54 -07002405 if (fio->type == DATA) {
2406 struct inode *inode = fio->page->mapping->host;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002407
2408 if (S_ISDIR(inode->i_mode))
2409 return CURSEG_HOT_DATA;
2410 else
2411 return CURSEG_COLD_DATA;
2412 } else {
Jaegeuk Kim72cc4582017-05-10 14:19:54 -07002413 if (IS_DNODE(fio->page) && is_cold_node(fio->page))
Jaegeuk Kima344b9f2014-11-05 20:05:53 -08002414 return CURSEG_WARM_NODE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002415 else
2416 return CURSEG_COLD_NODE;
2417 }
2418}
2419
Jaegeuk Kim72cc4582017-05-10 14:19:54 -07002420static int __get_segment_type_6(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002421{
Jaegeuk Kim72cc4582017-05-10 14:19:54 -07002422 if (fio->type == DATA) {
2423 struct inode *inode = fio->page->mapping->host;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002424
Jaegeuk Kim72cc4582017-05-10 14:19:54 -07002425 if (is_cold_data(fio->page) || file_is_cold(inode))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002426 return CURSEG_COLD_DATA;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002427 if (is_inode_flag_set(inode, FI_HOT_DATA))
2428 return CURSEG_HOT_DATA;
2429 return CURSEG_WARM_DATA;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002430 } else {
Jaegeuk Kim72cc4582017-05-10 14:19:54 -07002431 if (IS_DNODE(fio->page))
2432 return is_cold_node(fio->page) ? CURSEG_WARM_NODE :
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002433 CURSEG_HOT_NODE;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002434 return CURSEG_COLD_NODE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002435 }
2436}
2437
Jaegeuk Kim72cc4582017-05-10 14:19:54 -07002438static int __get_segment_type(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002439{
Jaegeuk Kimc4127262017-05-10 11:18:25 -07002440 int type = 0;
2441
Jaegeuk Kim72cc4582017-05-10 14:19:54 -07002442 switch (fio->sbi->active_logs) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002443 case 2:
Jaegeuk Kimc4127262017-05-10 11:18:25 -07002444 type = __get_segment_type_2(fio);
2445 break;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002446 case 4:
Jaegeuk Kimc4127262017-05-10 11:18:25 -07002447 type = __get_segment_type_4(fio);
2448 break;
2449 case 6:
2450 type = __get_segment_type_6(fio);
2451 break;
2452 default:
2453 f2fs_bug_on(fio->sbi, true);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002454 }
Jaegeuk Kim72cc4582017-05-10 14:19:54 -07002455
Jaegeuk Kimc4127262017-05-10 11:18:25 -07002456 if (IS_HOT(type))
2457 fio->temp = HOT;
2458 else if (IS_WARM(type))
2459 fio->temp = WARM;
2460 else
2461 fio->temp = COLD;
2462 return type;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002463}
2464
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09002465void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
2466 block_t old_blkaddr, block_t *new_blkaddr,
Chao Yuc52dc0f2017-05-19 23:37:01 +08002467 struct f2fs_summary *sum, int type,
2468 struct f2fs_io_info *fio, bool add_list)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002469{
2470 struct sit_info *sit_i = SIT_I(sbi);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002471 struct curseg_info *curseg = CURSEG_I(sbi, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002472
2473 mutex_lock(&curseg->curseg_mutex);
Jaegeuk Kim21cb1d92015-03-11 13:42:48 -04002474 mutex_lock(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002475
2476 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002477
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002478 f2fs_wait_discard_bio(sbi, *new_blkaddr);
2479
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002480 /*
2481 * __add_sum_entry should be resided under the curseg_mutex
2482 * because, this function updates a summary entry in the
2483 * current summary block.
2484 */
Haicheng Lie79efe32013-06-13 16:59:27 +08002485 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002486
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002487 __refresh_next_blkoff(sbi, curseg);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +09002488
2489 stat_inc_block_count(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002490
Jaegeuk Kim5e443812014-01-28 12:22:14 +09002491 if (!__has_curseg_space(sbi, type))
2492 sit_i->s_ops->allocate_segment(sbi, type, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002493 /*
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002494 * SIT information should be updated after segment allocation,
2495 * since we need to keep dirty segments precisely under SSR.
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002496 */
2497 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
Jaegeuk Kim5e443812014-01-28 12:22:14 +09002498
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002499 mutex_unlock(&sit_i->sentry_lock);
2500
Chao Yu43101252017-07-31 20:19:09 +08002501 if (page && IS_NODESEG(type)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002502 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
2503
Chao Yu43101252017-07-31 20:19:09 +08002504 f2fs_inode_chksum_set(sbi, page);
2505 }
2506
Chao Yuc52dc0f2017-05-19 23:37:01 +08002507 if (add_list) {
2508 struct f2fs_bio_info *io;
2509
2510 INIT_LIST_HEAD(&fio->list);
2511 fio->in_list = true;
2512 io = sbi->write_io[fio->type] + fio->temp;
2513 spin_lock(&io->io_lock);
2514 list_add_tail(&fio->list, &io->io_list);
2515 spin_unlock(&io->io_lock);
2516 }
2517
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09002518 mutex_unlock(&curseg->curseg_mutex);
2519}
2520
Chao Yuf014be82017-09-29 13:59:38 +08002521static void update_device_state(struct f2fs_io_info *fio)
2522{
2523 struct f2fs_sb_info *sbi = fio->sbi;
2524 unsigned int devidx;
2525
2526 if (!sbi->s_ndevs)
2527 return;
2528
2529 devidx = f2fs_target_device_index(sbi, fio->new_blkaddr);
2530
2531 /* update device state for fsync */
2532 set_dirty_device(sbi, fio->ino, devidx, FLUSH_INO);
Chao Yufd6422e2017-09-29 13:59:39 +08002533
2534 /* update device state for checkpoint */
2535 if (!f2fs_test_bit(devidx, (char *)&sbi->dirty_device)) {
2536 spin_lock(&sbi->dev_lock);
2537 f2fs_set_bit(devidx, (char *)&sbi->dirty_device);
2538 spin_unlock(&sbi->dev_lock);
2539 }
Chao Yuf014be82017-09-29 13:59:38 +08002540}
2541
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002542static void do_write_page(struct f2fs_summary *sum, struct f2fs_io_info *fio)
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09002543{
Jaegeuk Kim72cc4582017-05-10 14:19:54 -07002544 int type = __get_segment_type(fio);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002545 int err;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09002546
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002547reallocate:
Chao Yu7a9d7542016-02-22 18:36:38 +08002548 allocate_data_block(fio->sbi, fio->page, fio->old_blkaddr,
Chao Yuc52dc0f2017-05-19 23:37:01 +08002549 &fio->new_blkaddr, sum, type, fio, true);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09002550
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002551 /* writeout dirty page into bdev */
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -07002552 err = f2fs_submit_page_write(fio);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002553 if (err == -EAGAIN) {
2554 fio->old_blkaddr = fio->new_blkaddr;
2555 goto reallocate;
Chao Yuf014be82017-09-29 13:59:38 +08002556 } else if (!err) {
2557 update_device_state(fio);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002558 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002559}
2560
Chao Yuc0fe4882017-08-02 23:21:48 +08002561void write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
2562 enum iostat_type io_type)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002563{
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002564 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002565 .sbi = sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002566 .type = META,
Mike Christie04d328d2016-06-05 14:31:55 -05002567 .op = REQ_OP_WRITE,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002568 .op_flags = REQ_SYNC | REQ_META | REQ_PRIO,
Chao Yu7a9d7542016-02-22 18:36:38 +08002569 .old_blkaddr = page->index,
2570 .new_blkaddr = page->index,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002571 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07002572 .encrypted_page = NULL,
Chao Yuc52dc0f2017-05-19 23:37:01 +08002573 .in_list = false,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002574 };
2575
Chao Yu2b947002015-10-12 17:04:21 +08002576 if (unlikely(page->index >= MAIN_BLKADDR(sbi)))
Mike Christie04d328d2016-06-05 14:31:55 -05002577 fio.op_flags &= ~REQ_META;
Chao Yu2b947002015-10-12 17:04:21 +08002578
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002579 set_page_writeback(page);
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -07002580 f2fs_submit_page_write(&fio);
Chao Yuc0fe4882017-08-02 23:21:48 +08002581
2582 f2fs_update_iostat(sbi, io_type, F2FS_BLKSIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002583}
2584
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002585void write_node_page(unsigned int nid, struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002586{
2587 struct f2fs_summary sum;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002588
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002589 set_summary(&sum, nid, 0, 0);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002590 do_write_page(&sum, fio);
Chao Yuc0fe4882017-08-02 23:21:48 +08002591
2592 f2fs_update_iostat(fio->sbi, fio->io_type, F2FS_BLKSIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002593}
2594
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002595void write_data_page(struct dnode_of_data *dn, struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002596{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002597 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002598 struct f2fs_summary sum;
2599 struct node_info ni;
2600
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002601 f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002602 get_node_info(sbi, dn->nid, &ni);
2603 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002604 do_write_page(&sum, fio);
Chao Yuf28b3432016-02-24 17:16:47 +08002605 f2fs_update_data_blkaddr(dn, fio->new_blkaddr);
Chao Yuc0fe4882017-08-02 23:21:48 +08002606
2607 f2fs_update_iostat(sbi, fio->io_type, F2FS_BLKSIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002608}
2609
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002610int rewrite_data_page(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002611{
Chao Yuc0fe4882017-08-02 23:21:48 +08002612 int err;
2613
Chao Yu7a9d7542016-02-22 18:36:38 +08002614 fio->new_blkaddr = fio->old_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002615 stat_inc_inplace_blocks(fio->sbi);
Chao Yuc0fe4882017-08-02 23:21:48 +08002616
2617 err = f2fs_submit_page_bio(fio);
Chao Yuf014be82017-09-29 13:59:38 +08002618 if (!err)
2619 update_device_state(fio);
Chao Yuc0fe4882017-08-02 23:21:48 +08002620
2621 f2fs_update_iostat(fio->sbi, fio->io_type, F2FS_BLKSIZE);
2622
2623 return err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002624}
2625
Chao Yu4356e482016-02-23 17:52:43 +08002626void __f2fs_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
Chao Yu19f106b2015-05-06 13:08:06 +08002627 block_t old_blkaddr, block_t new_blkaddr,
Chao Yu28bc1062016-02-06 14:40:34 +08002628 bool recover_curseg, bool recover_newaddr)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002629{
2630 struct sit_info *sit_i = SIT_I(sbi);
2631 struct curseg_info *curseg;
2632 unsigned int segno, old_cursegno;
2633 struct seg_entry *se;
2634 int type;
Chao Yu19f106b2015-05-06 13:08:06 +08002635 unsigned short old_blkoff;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002636
2637 segno = GET_SEGNO(sbi, new_blkaddr);
2638 se = get_seg_entry(sbi, segno);
2639 type = se->type;
2640
Chao Yu19f106b2015-05-06 13:08:06 +08002641 if (!recover_curseg) {
2642 /* for recovery flow */
2643 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
2644 if (old_blkaddr == NULL_ADDR)
2645 type = CURSEG_COLD_DATA;
2646 else
2647 type = CURSEG_WARM_DATA;
2648 }
2649 } else {
2650 if (!IS_CURSEG(sbi, segno))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002651 type = CURSEG_WARM_DATA;
2652 }
Chao Yu19f106b2015-05-06 13:08:06 +08002653
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002654 curseg = CURSEG_I(sbi, type);
2655
2656 mutex_lock(&curseg->curseg_mutex);
2657 mutex_lock(&sit_i->sentry_lock);
2658
2659 old_cursegno = curseg->segno;
Chao Yu19f106b2015-05-06 13:08:06 +08002660 old_blkoff = curseg->next_blkoff;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002661
2662 /* change the current segment */
2663 if (segno != curseg->segno) {
2664 curseg->next_segno = segno;
Chao Yua69c9742017-08-30 18:04:48 +08002665 change_curseg(sbi, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002666 }
2667
Jaegeuk Kim491c0852014-02-04 13:01:10 +09002668 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
Haicheng Lie79efe32013-06-13 16:59:27 +08002669 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002670
Chao Yu28bc1062016-02-06 14:40:34 +08002671 if (!recover_curseg || recover_newaddr)
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07002672 update_sit_entry(sbi, new_blkaddr, 1);
2673 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
2674 update_sit_entry(sbi, old_blkaddr, -1);
2675
2676 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
2677 locate_dirty_segment(sbi, GET_SEGNO(sbi, new_blkaddr));
2678
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002679 locate_dirty_segment(sbi, old_cursegno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002680
Chao Yu19f106b2015-05-06 13:08:06 +08002681 if (recover_curseg) {
2682 if (old_cursegno != curseg->segno) {
2683 curseg->next_segno = old_cursegno;
Chao Yua69c9742017-08-30 18:04:48 +08002684 change_curseg(sbi, type);
Chao Yu19f106b2015-05-06 13:08:06 +08002685 }
2686 curseg->next_blkoff = old_blkoff;
2687 }
2688
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002689 mutex_unlock(&sit_i->sentry_lock);
2690 mutex_unlock(&curseg->curseg_mutex);
2691}
2692
Chao Yu528e3452015-05-28 19:15:35 +08002693void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
2694 block_t old_addr, block_t new_addr,
Chao Yu28bc1062016-02-06 14:40:34 +08002695 unsigned char version, bool recover_curseg,
2696 bool recover_newaddr)
Chao Yu528e3452015-05-28 19:15:35 +08002697{
2698 struct f2fs_summary sum;
2699
2700 set_summary(&sum, dn->nid, dn->ofs_in_node, version);
2701
Chao Yu28bc1062016-02-06 14:40:34 +08002702 __f2fs_replace_block(sbi, &sum, old_addr, new_addr,
2703 recover_curseg, recover_newaddr);
Chao Yu528e3452015-05-28 19:15:35 +08002704
Chao Yuf28b3432016-02-24 17:16:47 +08002705 f2fs_update_data_blkaddr(dn, new_addr);
Chao Yu528e3452015-05-28 19:15:35 +08002706}
2707
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09002708void f2fs_wait_on_page_writeback(struct page *page,
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08002709 enum page_type type, bool ordered)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09002710{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09002711 if (PageWriteback(page)) {
Jaegeuk Kim40813632014-09-02 15:31:18 -07002712 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
2713
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -07002714 f2fs_submit_merged_write_cond(sbi, page->mapping->host,
2715 0, page->index, type);
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08002716 if (ordered)
2717 wait_on_page_writeback(page);
2718 else
2719 wait_for_stable_page(page);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09002720 }
2721}
2722
Jaegeuk Kimfb605d02017-09-05 17:04:35 -07002723void f2fs_wait_on_block_writeback(struct f2fs_sb_info *sbi, block_t blkaddr)
Chao Yu08b39fb2015-10-08 13:27:34 +08002724{
2725 struct page *cpage;
2726
Yunlei He5d4c0af2016-09-18 08:16:56 +08002727 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR)
Chao Yu08b39fb2015-10-08 13:27:34 +08002728 return;
2729
Chao Yu08b39fb2015-10-08 13:27:34 +08002730 cpage = find_lock_page(META_MAPPING(sbi), blkaddr);
2731 if (cpage) {
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08002732 f2fs_wait_on_page_writeback(cpage, DATA, true);
Chao Yu08b39fb2015-10-08 13:27:34 +08002733 f2fs_put_page(cpage, 1);
2734 }
2735}
2736
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002737static int read_compacted_summaries(struct f2fs_sb_info *sbi)
2738{
2739 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
2740 struct curseg_info *seg_i;
2741 unsigned char *kaddr;
2742 struct page *page;
2743 block_t start;
2744 int i, j, offset;
2745
2746 start = start_sum_block(sbi);
2747
2748 page = get_meta_page(sbi, start++);
2749 kaddr = (unsigned char *)page_address(page);
2750
2751 /* Step 1: restore nat cache */
2752 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08002753 memcpy(seg_i->journal, kaddr, SUM_JOURNAL_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002754
2755 /* Step 2: restore sit cache */
2756 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08002757 memcpy(seg_i->journal, kaddr + SUM_JOURNAL_SIZE, SUM_JOURNAL_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002758 offset = 2 * SUM_JOURNAL_SIZE;
2759
2760 /* Step 3: restore summary entries */
2761 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
2762 unsigned short blk_off;
2763 unsigned int segno;
2764
2765 seg_i = CURSEG_I(sbi, i);
2766 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
2767 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
2768 seg_i->next_segno = segno;
2769 reset_curseg(sbi, i, 0);
2770 seg_i->alloc_type = ckpt->alloc_type[i];
2771 seg_i->next_blkoff = blk_off;
2772
2773 if (seg_i->alloc_type == SSR)
2774 blk_off = sbi->blocks_per_seg;
2775
2776 for (j = 0; j < blk_off; j++) {
2777 struct f2fs_summary *s;
2778 s = (struct f2fs_summary *)(kaddr + offset);
2779 seg_i->sum_blk->entries[j] = *s;
2780 offset += SUMMARY_SIZE;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002781 if (offset + SUMMARY_SIZE <= PAGE_SIZE -
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002782 SUM_FOOTER_SIZE)
2783 continue;
2784
2785 f2fs_put_page(page, 1);
2786 page = NULL;
2787
2788 page = get_meta_page(sbi, start++);
2789 kaddr = (unsigned char *)page_address(page);
2790 offset = 0;
2791 }
2792 }
2793 f2fs_put_page(page, 1);
2794 return 0;
2795}
2796
2797static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
2798{
2799 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
2800 struct f2fs_summary_block *sum;
2801 struct curseg_info *curseg;
2802 struct page *new;
2803 unsigned short blk_off;
2804 unsigned int segno = 0;
2805 block_t blk_addr = 0;
2806
2807 /* get segment number and block addr */
2808 if (IS_DATASEG(type)) {
2809 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
2810 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
2811 CURSEG_HOT_DATA]);
Jaegeuk Kim119ee912015-01-29 11:45:33 -08002812 if (__exist_node_summaries(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002813 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
2814 else
2815 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
2816 } else {
2817 segno = le32_to_cpu(ckpt->cur_node_segno[type -
2818 CURSEG_HOT_NODE]);
2819 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
2820 CURSEG_HOT_NODE]);
Jaegeuk Kim119ee912015-01-29 11:45:33 -08002821 if (__exist_node_summaries(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002822 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
2823 type - CURSEG_HOT_NODE);
2824 else
2825 blk_addr = GET_SUM_BLOCK(sbi, segno);
2826 }
2827
2828 new = get_meta_page(sbi, blk_addr);
2829 sum = (struct f2fs_summary_block *)page_address(new);
2830
2831 if (IS_NODESEG(type)) {
Jaegeuk Kim119ee912015-01-29 11:45:33 -08002832 if (__exist_node_summaries(sbi)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002833 struct f2fs_summary *ns = &sum->entries[0];
2834 int i;
2835 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
2836 ns->version = 0;
2837 ns->ofs_in_node = 0;
2838 }
2839 } else {
Gu Zhengd6537882014-03-07 18:43:36 +08002840 int err;
2841
2842 err = restore_node_summary(sbi, segno, sum);
2843 if (err) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002844 f2fs_put_page(new, 1);
Gu Zhengd6537882014-03-07 18:43:36 +08002845 return err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002846 }
2847 }
2848 }
2849
2850 /* set uncompleted segment to curseg */
2851 curseg = CURSEG_I(sbi, type);
2852 mutex_lock(&curseg->curseg_mutex);
Chao Yub7ad7512016-02-19 18:08:46 +08002853
2854 /* update journal info */
2855 down_write(&curseg->journal_rwsem);
2856 memcpy(curseg->journal, &sum->journal, SUM_JOURNAL_SIZE);
2857 up_write(&curseg->journal_rwsem);
2858
2859 memcpy(curseg->sum_blk->entries, sum->entries, SUM_ENTRY_SIZE);
2860 memcpy(&curseg->sum_blk->footer, &sum->footer, SUM_FOOTER_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002861 curseg->next_segno = segno;
2862 reset_curseg(sbi, type, 0);
2863 curseg->alloc_type = ckpt->alloc_type[type];
2864 curseg->next_blkoff = blk_off;
2865 mutex_unlock(&curseg->curseg_mutex);
2866 f2fs_put_page(new, 1);
2867 return 0;
2868}
2869
2870static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
2871{
Jin Qiana8d61752017-06-01 11:18:30 -07002872 struct f2fs_journal *sit_j = CURSEG_I(sbi, CURSEG_COLD_DATA)->journal;
2873 struct f2fs_journal *nat_j = CURSEG_I(sbi, CURSEG_HOT_DATA)->journal;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002874 int type = CURSEG_HOT_DATA;
Chao Yue4fc5fb2014-03-17 16:36:24 +08002875 int err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002876
Chao Yuaaec2b12016-09-20 11:04:18 +08002877 if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG)) {
Chao Yu3fa06d72014-12-09 14:21:46 +08002878 int npages = npages_for_summary_flush(sbi, true);
2879
2880 if (npages >= 2)
2881 ra_meta_pages(sbi, start_sum_block(sbi), npages,
Chao Yu26879fb2015-10-12 17:05:59 +08002882 META_CP, true);
Chao Yu3fa06d72014-12-09 14:21:46 +08002883
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002884 /* restore for compacted data summary */
2885 if (read_compacted_summaries(sbi))
2886 return -EINVAL;
2887 type = CURSEG_HOT_NODE;
2888 }
2889
Jaegeuk Kim119ee912015-01-29 11:45:33 -08002890 if (__exist_node_summaries(sbi))
Chao Yu3fa06d72014-12-09 14:21:46 +08002891 ra_meta_pages(sbi, sum_blk_addr(sbi, NR_CURSEG_TYPE, type),
Chao Yu26879fb2015-10-12 17:05:59 +08002892 NR_CURSEG_TYPE - type, META_CP, true);
Chao Yu3fa06d72014-12-09 14:21:46 +08002893
Chao Yue4fc5fb2014-03-17 16:36:24 +08002894 for (; type <= CURSEG_COLD_NODE; type++) {
2895 err = read_normal_summaries(sbi, type);
2896 if (err)
2897 return err;
2898 }
2899
Jin Qiana8d61752017-06-01 11:18:30 -07002900 /* sanity check for summary blocks */
2901 if (nats_in_cursum(nat_j) > NAT_JOURNAL_ENTRIES ||
2902 sits_in_cursum(sit_j) > SIT_JOURNAL_ENTRIES)
2903 return -EINVAL;
2904
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002905 return 0;
2906}
2907
2908static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
2909{
2910 struct page *page;
2911 unsigned char *kaddr;
2912 struct f2fs_summary *summary;
2913 struct curseg_info *seg_i;
2914 int written_size = 0;
2915 int i, j;
2916
2917 page = grab_meta_page(sbi, blkaddr++);
2918 kaddr = (unsigned char *)page_address(page);
2919
2920 /* Step 1: write nat cache */
2921 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08002922 memcpy(kaddr, seg_i->journal, SUM_JOURNAL_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002923 written_size += SUM_JOURNAL_SIZE;
2924
2925 /* Step 2: write sit cache */
2926 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08002927 memcpy(kaddr + written_size, seg_i->journal, SUM_JOURNAL_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002928 written_size += SUM_JOURNAL_SIZE;
2929
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002930 /* Step 3: write summary entries */
2931 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
2932 unsigned short blkoff;
2933 seg_i = CURSEG_I(sbi, i);
2934 if (sbi->ckpt->alloc_type[i] == SSR)
2935 blkoff = sbi->blocks_per_seg;
2936 else
2937 blkoff = curseg_blkoff(sbi, i);
2938
2939 for (j = 0; j < blkoff; j++) {
2940 if (!page) {
2941 page = grab_meta_page(sbi, blkaddr++);
2942 kaddr = (unsigned char *)page_address(page);
2943 written_size = 0;
2944 }
2945 summary = (struct f2fs_summary *)(kaddr + written_size);
2946 *summary = seg_i->sum_blk->entries[j];
2947 written_size += SUMMARY_SIZE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002948
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002949 if (written_size + SUMMARY_SIZE <= PAGE_SIZE -
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002950 SUM_FOOTER_SIZE)
2951 continue;
2952
Chao Yue8d61a72013-10-24 15:08:28 +08002953 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002954 f2fs_put_page(page, 1);
2955 page = NULL;
2956 }
2957 }
Chao Yue8d61a72013-10-24 15:08:28 +08002958 if (page) {
2959 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002960 f2fs_put_page(page, 1);
Chao Yue8d61a72013-10-24 15:08:28 +08002961 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002962}
2963
2964static void write_normal_summaries(struct f2fs_sb_info *sbi,
2965 block_t blkaddr, int type)
2966{
2967 int i, end;
2968 if (IS_DATASEG(type))
2969 end = type + NR_CURSEG_DATA_TYPE;
2970 else
2971 end = type + NR_CURSEG_NODE_TYPE;
2972
Chao Yub7ad7512016-02-19 18:08:46 +08002973 for (i = type; i < end; i++)
2974 write_current_sum_page(sbi, i, blkaddr + (i - type));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002975}
2976
2977void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
2978{
Chao Yuaaec2b12016-09-20 11:04:18 +08002979 if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002980 write_compacted_summaries(sbi, start_blk);
2981 else
2982 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
2983}
2984
2985void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
2986{
Jaegeuk Kim119ee912015-01-29 11:45:33 -08002987 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002988}
2989
Chao Yudfc08a12016-02-14 18:50:40 +08002990int lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002991 unsigned int val, int alloc)
2992{
2993 int i;
2994
2995 if (type == NAT_JOURNAL) {
Chao Yudfc08a12016-02-14 18:50:40 +08002996 for (i = 0; i < nats_in_cursum(journal); i++) {
2997 if (le32_to_cpu(nid_in_journal(journal, i)) == val)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002998 return i;
2999 }
Chao Yudfc08a12016-02-14 18:50:40 +08003000 if (alloc && __has_cursum_space(journal, 1, NAT_JOURNAL))
3001 return update_nats_in_cursum(journal, 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003002 } else if (type == SIT_JOURNAL) {
Chao Yudfc08a12016-02-14 18:50:40 +08003003 for (i = 0; i < sits_in_cursum(journal); i++)
3004 if (le32_to_cpu(segno_in_journal(journal, i)) == val)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003005 return i;
Chao Yudfc08a12016-02-14 18:50:40 +08003006 if (alloc && __has_cursum_space(journal, 1, SIT_JOURNAL))
3007 return update_sits_in_cursum(journal, 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003008 }
3009 return -1;
3010}
3011
3012static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
3013 unsigned int segno)
3014{
Gu Zheng2cc22182014-10-20 17:45:49 +08003015 return get_meta_page(sbi, current_sit_addr(sbi, segno));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003016}
3017
3018static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
3019 unsigned int start)
3020{
3021 struct sit_info *sit_i = SIT_I(sbi);
3022 struct page *src_page, *dst_page;
3023 pgoff_t src_off, dst_off;
3024 void *src_addr, *dst_addr;
3025
3026 src_off = current_sit_addr(sbi, start);
3027 dst_off = next_sit_addr(sbi, src_off);
3028
3029 /* get current sit block page without lock */
3030 src_page = get_meta_page(sbi, src_off);
3031 dst_page = grab_meta_page(sbi, dst_off);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07003032 f2fs_bug_on(sbi, PageDirty(src_page));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003033
3034 src_addr = page_address(src_page);
3035 dst_addr = page_address(dst_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003036 memcpy(dst_addr, src_addr, PAGE_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003037
3038 set_page_dirty(dst_page);
3039 f2fs_put_page(src_page, 1);
3040
3041 set_to_next_sit(sit_i, start);
3042
3043 return dst_page;
3044}
3045
Chao Yu184a5cd2014-09-04 18:13:01 +08003046static struct sit_entry_set *grab_sit_entry_set(void)
3047{
3048 struct sit_entry_set *ses =
Jaegeuk Kim80c54502015-08-20 08:51:56 -07003049 f2fs_kmem_cache_alloc(sit_entry_set_slab, GFP_NOFS);
Chao Yu184a5cd2014-09-04 18:13:01 +08003050
3051 ses->entry_cnt = 0;
3052 INIT_LIST_HEAD(&ses->set_list);
3053 return ses;
3054}
3055
3056static void release_sit_entry_set(struct sit_entry_set *ses)
3057{
3058 list_del(&ses->set_list);
3059 kmem_cache_free(sit_entry_set_slab, ses);
3060}
3061
3062static void adjust_sit_entry_set(struct sit_entry_set *ses,
3063 struct list_head *head)
3064{
3065 struct sit_entry_set *next = ses;
3066
3067 if (list_is_last(&ses->set_list, head))
3068 return;
3069
3070 list_for_each_entry_continue(next, head, set_list)
3071 if (ses->entry_cnt <= next->entry_cnt)
3072 break;
3073
3074 list_move_tail(&ses->set_list, &next->set_list);
3075}
3076
3077static void add_sit_entry(unsigned int segno, struct list_head *head)
3078{
3079 struct sit_entry_set *ses;
3080 unsigned int start_segno = START_SEGNO(segno);
3081
3082 list_for_each_entry(ses, head, set_list) {
3083 if (ses->start_segno == start_segno) {
3084 ses->entry_cnt++;
3085 adjust_sit_entry_set(ses, head);
3086 return;
3087 }
3088 }
3089
3090 ses = grab_sit_entry_set();
3091
3092 ses->start_segno = start_segno;
3093 ses->entry_cnt++;
3094 list_add(&ses->set_list, head);
3095}
3096
3097static void add_sits_in_set(struct f2fs_sb_info *sbi)
3098{
3099 struct f2fs_sm_info *sm_info = SM_I(sbi);
3100 struct list_head *set_list = &sm_info->sit_entry_set;
3101 unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap;
Chao Yu184a5cd2014-09-04 18:13:01 +08003102 unsigned int segno;
3103
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003104 for_each_set_bit(segno, bitmap, MAIN_SEGS(sbi))
Chao Yu184a5cd2014-09-04 18:13:01 +08003105 add_sit_entry(segno, set_list);
3106}
3107
3108static void remove_sits_in_journal(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003109{
3110 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08003111 struct f2fs_journal *journal = curseg->journal;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003112 int i;
3113
Chao Yub7ad7512016-02-19 18:08:46 +08003114 down_write(&curseg->journal_rwsem);
Chao Yudfc08a12016-02-14 18:50:40 +08003115 for (i = 0; i < sits_in_cursum(journal); i++) {
Chao Yu184a5cd2014-09-04 18:13:01 +08003116 unsigned int segno;
3117 bool dirtied;
3118
Chao Yudfc08a12016-02-14 18:50:40 +08003119 segno = le32_to_cpu(segno_in_journal(journal, i));
Chao Yu184a5cd2014-09-04 18:13:01 +08003120 dirtied = __mark_sit_entry_dirty(sbi, segno);
3121
3122 if (!dirtied)
3123 add_sit_entry(segno, &SM_I(sbi)->sit_entry_set);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003124 }
Chao Yudfc08a12016-02-14 18:50:40 +08003125 update_sits_in_cursum(journal, -i);
Chao Yub7ad7512016-02-19 18:08:46 +08003126 up_write(&curseg->journal_rwsem);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003127}
3128
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09003129/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003130 * CP calls this function, which flushes SIT entries including sit_journal,
3131 * and moves prefree segs to free segs.
3132 */
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003133void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003134{
3135 struct sit_info *sit_i = SIT_I(sbi);
3136 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
3137 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08003138 struct f2fs_journal *journal = curseg->journal;
Chao Yu184a5cd2014-09-04 18:13:01 +08003139 struct sit_entry_set *ses, *tmp;
3140 struct list_head *head = &SM_I(sbi)->sit_entry_set;
Chao Yu184a5cd2014-09-04 18:13:01 +08003141 bool to_journal = true;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003142 struct seg_entry *se;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003143
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003144 mutex_lock(&sit_i->sentry_lock);
3145
Wanpeng Li2b11a742015-02-27 16:52:50 +08003146 if (!sit_i->dirty_sentries)
3147 goto out;
3148
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003149 /*
Chao Yu184a5cd2014-09-04 18:13:01 +08003150 * add and account sit entries of dirty bitmap in sit entry
3151 * set temporarily
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003152 */
Chao Yu184a5cd2014-09-04 18:13:01 +08003153 add_sits_in_set(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003154
Chao Yu184a5cd2014-09-04 18:13:01 +08003155 /*
3156 * if there are no enough space in journal to store dirty sit
3157 * entries, remove all entries from journal and add and account
3158 * them in sit entry set.
3159 */
Chao Yudfc08a12016-02-14 18:50:40 +08003160 if (!__has_cursum_space(journal, sit_i->dirty_sentries, SIT_JOURNAL))
Chao Yu184a5cd2014-09-04 18:13:01 +08003161 remove_sits_in_journal(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003162
Chao Yu184a5cd2014-09-04 18:13:01 +08003163 /*
3164 * there are two steps to flush sit entries:
3165 * #1, flush sit entries to journal in current cold data summary block.
3166 * #2, flush sit entries to sit page.
3167 */
3168 list_for_each_entry_safe(ses, tmp, head, set_list) {
Jaegeuk Kim4a257ed2014-10-16 11:43:30 -07003169 struct page *page = NULL;
Chao Yu184a5cd2014-09-04 18:13:01 +08003170 struct f2fs_sit_block *raw_sit = NULL;
3171 unsigned int start_segno = ses->start_segno;
3172 unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003173 (unsigned long)MAIN_SEGS(sbi));
Chao Yu184a5cd2014-09-04 18:13:01 +08003174 unsigned int segno = start_segno;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09003175
Chao Yu184a5cd2014-09-04 18:13:01 +08003176 if (to_journal &&
Chao Yudfc08a12016-02-14 18:50:40 +08003177 !__has_cursum_space(journal, ses->entry_cnt, SIT_JOURNAL))
Chao Yu184a5cd2014-09-04 18:13:01 +08003178 to_journal = false;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003179
Chao Yub7ad7512016-02-19 18:08:46 +08003180 if (to_journal) {
3181 down_write(&curseg->journal_rwsem);
3182 } else {
Chao Yu184a5cd2014-09-04 18:13:01 +08003183 page = get_next_sit_page(sbi, start_segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003184 raw_sit = page_address(page);
3185 }
3186
Chao Yu184a5cd2014-09-04 18:13:01 +08003187 /* flush dirty sit entries in region of current sit set */
3188 for_each_set_bit_from(segno, bitmap, end) {
3189 int offset, sit_offset;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003190
3191 se = get_seg_entry(sbi, segno);
Chao Yu184a5cd2014-09-04 18:13:01 +08003192
3193 /* add discard candidates */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003194 if (!(cpc->reason & CP_DISCARD)) {
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003195 cpc->trim_start = segno;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003196 add_discard_addrs(sbi, cpc, false);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003197 }
Chao Yu184a5cd2014-09-04 18:13:01 +08003198
3199 if (to_journal) {
Chao Yudfc08a12016-02-14 18:50:40 +08003200 offset = lookup_journal_in_cursum(journal,
Chao Yu184a5cd2014-09-04 18:13:01 +08003201 SIT_JOURNAL, segno, 1);
3202 f2fs_bug_on(sbi, offset < 0);
Chao Yudfc08a12016-02-14 18:50:40 +08003203 segno_in_journal(journal, offset) =
Chao Yu184a5cd2014-09-04 18:13:01 +08003204 cpu_to_le32(segno);
3205 seg_info_to_raw_sit(se,
Chao Yudfc08a12016-02-14 18:50:40 +08003206 &sit_in_journal(journal, offset));
Chao Yu184a5cd2014-09-04 18:13:01 +08003207 } else {
3208 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
3209 seg_info_to_raw_sit(se,
3210 &raw_sit->entries[sit_offset]);
3211 }
3212
3213 __clear_bit(segno, bitmap);
3214 sit_i->dirty_sentries--;
3215 ses->entry_cnt--;
3216 }
3217
Chao Yub7ad7512016-02-19 18:08:46 +08003218 if (to_journal)
3219 up_write(&curseg->journal_rwsem);
3220 else
Chao Yu184a5cd2014-09-04 18:13:01 +08003221 f2fs_put_page(page, 1);
3222
3223 f2fs_bug_on(sbi, ses->entry_cnt);
3224 release_sit_entry_set(ses);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003225 }
Chao Yu184a5cd2014-09-04 18:13:01 +08003226
3227 f2fs_bug_on(sbi, !list_empty(head));
3228 f2fs_bug_on(sbi, sit_i->dirty_sentries);
Chao Yu184a5cd2014-09-04 18:13:01 +08003229out:
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003230 if (cpc->reason & CP_DISCARD) {
3231 __u64 trim_start = cpc->trim_start;
3232
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003233 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003234 add_discard_addrs(sbi, cpc, false);
3235
3236 cpc->trim_start = trim_start;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003237 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003238 mutex_unlock(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003239
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003240 set_prefree_as_free_segments(sbi);
3241}
3242
3243static int build_sit_info(struct f2fs_sb_info *sbi)
3244{
3245 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003246 struct sit_info *sit_i;
3247 unsigned int sit_segs, start;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003248 char *src_bitmap;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003249 unsigned int bitmap_size;
3250
3251 /* allocate memory for SIT information */
3252 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
3253 if (!sit_i)
3254 return -ENOMEM;
3255
3256 SM_I(sbi)->sit_info = sit_i;
3257
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003258 sit_i->sentries = kvzalloc(MAIN_SEGS(sbi) *
Jaegeuk Kim39307a82015-09-22 13:50:47 -07003259 sizeof(struct seg_entry), GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003260 if (!sit_i->sentries)
3261 return -ENOMEM;
3262
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003263 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003264 sit_i->dirty_sentries_bitmap = kvzalloc(bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003265 if (!sit_i->dirty_sentries_bitmap)
3266 return -ENOMEM;
3267
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003268 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003269 sit_i->sentries[start].cur_valid_map
3270 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
3271 sit_i->sentries[start].ckpt_valid_map
3272 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07003273 if (!sit_i->sentries[start].cur_valid_map ||
Jaegeuk Kim3e025742016-08-02 10:56:40 -07003274 !sit_i->sentries[start].ckpt_valid_map)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003275 return -ENOMEM;
Jaegeuk Kim3e025742016-08-02 10:56:40 -07003276
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003277#ifdef CONFIG_F2FS_CHECK_FS
3278 sit_i->sentries[start].cur_valid_map_mir
3279 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
3280 if (!sit_i->sentries[start].cur_valid_map_mir)
3281 return -ENOMEM;
3282#endif
3283
Jaegeuk Kim3e025742016-08-02 10:56:40 -07003284 if (f2fs_discard_en(sbi)) {
3285 sit_i->sentries[start].discard_map
3286 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
3287 if (!sit_i->sentries[start].discard_map)
3288 return -ENOMEM;
3289 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003290 }
3291
Jaegeuk Kim60a3b782015-02-10 16:44:29 -08003292 sit_i->tmp_map = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
3293 if (!sit_i->tmp_map)
3294 return -ENOMEM;
3295
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003296 if (sbi->segs_per_sec > 1) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003297 sit_i->sec_entries = kvzalloc(MAIN_SECS(sbi) *
Jaegeuk Kim39307a82015-09-22 13:50:47 -07003298 sizeof(struct sec_entry), GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003299 if (!sit_i->sec_entries)
3300 return -ENOMEM;
3301 }
3302
3303 /* get information related with SIT */
3304 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
3305
3306 /* setup SIT bitmap from ckeckpoint pack */
3307 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
3308 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
3309
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003310 sit_i->sit_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
3311 if (!sit_i->sit_bitmap)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003312 return -ENOMEM;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003313
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003314#ifdef CONFIG_F2FS_CHECK_FS
3315 sit_i->sit_bitmap_mir = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
3316 if (!sit_i->sit_bitmap_mir)
3317 return -ENOMEM;
3318#endif
3319
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003320 /* init SIT information */
3321 sit_i->s_ops = &default_salloc_ops;
3322
3323 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
3324 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003325 sit_i->written_valid_blocks = 0;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003326 sit_i->bitmap_size = bitmap_size;
3327 sit_i->dirty_sentries = 0;
3328 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
3329 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003330 sit_i->mounted_time = ktime_get_real_seconds();
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003331 mutex_init(&sit_i->sentry_lock);
3332 return 0;
3333}
3334
3335static int build_free_segmap(struct f2fs_sb_info *sbi)
3336{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003337 struct free_segmap_info *free_i;
3338 unsigned int bitmap_size, sec_bitmap_size;
3339
3340 /* allocate memory for free segmap information */
3341 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
3342 if (!free_i)
3343 return -ENOMEM;
3344
3345 SM_I(sbi)->free_info = free_i;
3346
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003347 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003348 free_i->free_segmap = kvmalloc(bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003349 if (!free_i->free_segmap)
3350 return -ENOMEM;
3351
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003352 sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003353 free_i->free_secmap = kvmalloc(sec_bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003354 if (!free_i->free_secmap)
3355 return -ENOMEM;
3356
3357 /* set all segments as dirty temporarily */
3358 memset(free_i->free_segmap, 0xff, bitmap_size);
3359 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
3360
3361 /* init free segmap information */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003362 free_i->start_segno = GET_SEGNO_FROM_SEG0(sbi, MAIN_BLKADDR(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003363 free_i->free_segments = 0;
3364 free_i->free_sections = 0;
Chao Yu1a118cc2015-02-11 18:20:38 +08003365 spin_lock_init(&free_i->segmap_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003366 return 0;
3367}
3368
3369static int build_curseg(struct f2fs_sb_info *sbi)
3370{
Namjae Jeon1042d602012-12-01 10:56:13 +09003371 struct curseg_info *array;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003372 int i;
3373
Fabian Frederickb434bab2014-06-23 18:39:15 +02003374 array = kcalloc(NR_CURSEG_TYPE, sizeof(*array), GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003375 if (!array)
3376 return -ENOMEM;
3377
3378 SM_I(sbi)->curseg_array = array;
3379
3380 for (i = 0; i < NR_CURSEG_TYPE; i++) {
3381 mutex_init(&array[i].curseg_mutex);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003382 array[i].sum_blk = kzalloc(PAGE_SIZE, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003383 if (!array[i].sum_blk)
3384 return -ENOMEM;
Chao Yub7ad7512016-02-19 18:08:46 +08003385 init_rwsem(&array[i].journal_rwsem);
3386 array[i].journal = kzalloc(sizeof(struct f2fs_journal),
3387 GFP_KERNEL);
3388 if (!array[i].journal)
3389 return -ENOMEM;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003390 array[i].segno = NULL_SEGNO;
3391 array[i].next_blkoff = 0;
3392 }
3393 return restore_curseg_summaries(sbi);
3394}
3395
3396static void build_sit_entries(struct f2fs_sb_info *sbi)
3397{
3398 struct sit_info *sit_i = SIT_I(sbi);
3399 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08003400 struct f2fs_journal *journal = curseg->journal;
Yunlei He9c094042016-09-24 12:29:18 +08003401 struct seg_entry *se;
3402 struct f2fs_sit_entry sit;
Chao Yu74de5932013-11-22 09:09:59 +08003403 int sit_blk_cnt = SIT_BLK_CNT(sbi);
3404 unsigned int i, start, end;
3405 unsigned int readed, start_blk = 0;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003406
Chao Yu74de5932013-11-22 09:09:59 +08003407 do {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003408 readed = ra_meta_pages(sbi, start_blk, BIO_MAX_PAGES,
3409 META_SIT, true);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003410
Chao Yu74de5932013-11-22 09:09:59 +08003411 start = start_blk * sit_i->sents_per_block;
3412 end = (start_blk + readed) * sit_i->sents_per_block;
3413
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003414 for (; start < end && start < MAIN_SEGS(sbi); start++) {
Chao Yu74de5932013-11-22 09:09:59 +08003415 struct f2fs_sit_block *sit_blk;
Chao Yu74de5932013-11-22 09:09:59 +08003416 struct page *page;
3417
Yunlei He9c094042016-09-24 12:29:18 +08003418 se = &sit_i->sentries[start];
Chao Yu74de5932013-11-22 09:09:59 +08003419 page = get_current_sit_page(sbi, start);
3420 sit_blk = (struct f2fs_sit_block *)page_address(page);
3421 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
3422 f2fs_put_page(page, 1);
Chao Yud600af232016-08-19 23:13:47 +08003423
Chao Yu74de5932013-11-22 09:09:59 +08003424 check_block_count(sbi, start, &sit);
3425 seg_info_from_raw_sit(se, &sit);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07003426
3427 /* build discard map only one time */
Jaegeuk Kim3e025742016-08-02 10:56:40 -07003428 if (f2fs_discard_en(sbi)) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003429 if (is_set_ckpt_flags(sbi, CP_TRIMMED_FLAG)) {
3430 memset(se->discard_map, 0xff,
3431 SIT_VBLOCK_MAP_SIZE);
3432 } else {
3433 memcpy(se->discard_map,
3434 se->cur_valid_map,
3435 SIT_VBLOCK_MAP_SIZE);
3436 sbi->discard_blks +=
3437 sbi->blocks_per_seg -
3438 se->valid_blocks;
3439 }
Jaegeuk Kim3e025742016-08-02 10:56:40 -07003440 }
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07003441
Chao Yud600af232016-08-19 23:13:47 +08003442 if (sbi->segs_per_sec > 1)
3443 get_sec_entry(sbi, start)->valid_blocks +=
3444 se->valid_blocks;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003445 }
Chao Yu74de5932013-11-22 09:09:59 +08003446 start_blk += readed;
3447 } while (start_blk < sit_blk_cnt);
Chao Yud600af232016-08-19 23:13:47 +08003448
3449 down_read(&curseg->journal_rwsem);
3450 for (i = 0; i < sits_in_cursum(journal); i++) {
Chao Yud600af232016-08-19 23:13:47 +08003451 unsigned int old_valid_blocks;
3452
3453 start = le32_to_cpu(segno_in_journal(journal, i));
3454 se = &sit_i->sentries[start];
3455 sit = sit_in_journal(journal, i);
3456
3457 old_valid_blocks = se->valid_blocks;
3458
3459 check_block_count(sbi, start, &sit);
3460 seg_info_from_raw_sit(se, &sit);
3461
3462 if (f2fs_discard_en(sbi)) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003463 if (is_set_ckpt_flags(sbi, CP_TRIMMED_FLAG)) {
3464 memset(se->discard_map, 0xff,
3465 SIT_VBLOCK_MAP_SIZE);
3466 } else {
3467 memcpy(se->discard_map, se->cur_valid_map,
3468 SIT_VBLOCK_MAP_SIZE);
3469 sbi->discard_blks += old_valid_blocks -
3470 se->valid_blocks;
3471 }
Chao Yud600af232016-08-19 23:13:47 +08003472 }
3473
3474 if (sbi->segs_per_sec > 1)
3475 get_sec_entry(sbi, start)->valid_blocks +=
3476 se->valid_blocks - old_valid_blocks;
3477 }
3478 up_read(&curseg->journal_rwsem);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003479}
3480
3481static void init_free_segmap(struct f2fs_sb_info *sbi)
3482{
3483 unsigned int start;
3484 int type;
3485
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003486 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003487 struct seg_entry *sentry = get_seg_entry(sbi, start);
3488 if (!sentry->valid_blocks)
3489 __set_free(sbi, start);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003490 else
3491 SIT_I(sbi)->written_valid_blocks +=
3492 sentry->valid_blocks;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003493 }
3494
3495 /* set use the current segments */
3496 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
3497 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
3498 __set_test_and_inuse(sbi, curseg_t->segno);
3499 }
3500}
3501
3502static void init_dirty_segmap(struct f2fs_sb_info *sbi)
3503{
3504 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
3505 struct free_segmap_info *free_i = FREE_I(sbi);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003506 unsigned int segno = 0, offset = 0;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003507 unsigned short valid_blocks;
3508
Namjae Jeon8736fbf2013-06-16 09:49:11 +09003509 while (1) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003510 /* find dirty segment based on free segmap */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003511 segno = find_next_inuse(free_i, MAIN_SEGS(sbi), offset);
3512 if (segno >= MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003513 break;
3514 offset = segno + 1;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003515 valid_blocks = get_valid_blocks(sbi, segno, false);
Jaegeuk Kimec325b52014-09-02 16:24:11 -07003516 if (valid_blocks == sbi->blocks_per_seg || !valid_blocks)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003517 continue;
Jaegeuk Kimec325b52014-09-02 16:24:11 -07003518 if (valid_blocks > sbi->blocks_per_seg) {
3519 f2fs_bug_on(sbi, 1);
3520 continue;
3521 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003522 mutex_lock(&dirty_i->seglist_lock);
3523 __locate_dirty_segment(sbi, segno, DIRTY);
3524 mutex_unlock(&dirty_i->seglist_lock);
3525 }
3526}
3527
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09003528static int init_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003529{
3530 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003531 unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003532
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003533 dirty_i->victim_secmap = kvzalloc(bitmap_size, GFP_KERNEL);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09003534 if (!dirty_i->victim_secmap)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003535 return -ENOMEM;
3536 return 0;
3537}
3538
3539static int build_dirty_segmap(struct f2fs_sb_info *sbi)
3540{
3541 struct dirty_seglist_info *dirty_i;
3542 unsigned int bitmap_size, i;
3543
3544 /* allocate memory for dirty segments list information */
3545 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
3546 if (!dirty_i)
3547 return -ENOMEM;
3548
3549 SM_I(sbi)->dirty_info = dirty_i;
3550 mutex_init(&dirty_i->seglist_lock);
3551
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003552 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003553
3554 for (i = 0; i < NR_DIRTY_TYPE; i++) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003555 dirty_i->dirty_segmap[i] = kvzalloc(bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003556 if (!dirty_i->dirty_segmap[i])
3557 return -ENOMEM;
3558 }
3559
3560 init_dirty_segmap(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09003561 return init_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003562}
3563
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09003564/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003565 * Update min, max modified time for cost-benefit GC algorithm
3566 */
3567static void init_min_max_mtime(struct f2fs_sb_info *sbi)
3568{
3569 struct sit_info *sit_i = SIT_I(sbi);
3570 unsigned int segno;
3571
3572 mutex_lock(&sit_i->sentry_lock);
3573
3574 sit_i->min_mtime = LLONG_MAX;
3575
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003576 for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003577 unsigned int i;
3578 unsigned long long mtime = 0;
3579
3580 for (i = 0; i < sbi->segs_per_sec; i++)
3581 mtime += get_seg_entry(sbi, segno + i)->mtime;
3582
3583 mtime = div_u64(mtime, sbi->segs_per_sec);
3584
3585 if (sit_i->min_mtime > mtime)
3586 sit_i->min_mtime = mtime;
3587 }
3588 sit_i->max_mtime = get_mtime(sbi);
3589 mutex_unlock(&sit_i->sentry_lock);
3590}
3591
3592int build_segment_manager(struct f2fs_sb_info *sbi)
3593{
3594 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
3595 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Namjae Jeon1042d602012-12-01 10:56:13 +09003596 struct f2fs_sm_info *sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003597 int err;
3598
3599 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
3600 if (!sm_info)
3601 return -ENOMEM;
3602
3603 /* init sm info */
3604 sbi->sm_info = sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003605 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
3606 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
3607 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
3608 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
3609 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
3610 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
3611 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
Jaegeuk Kim58c41032014-03-19 14:17:21 +09003612 sm_info->rec_prefree_segments = sm_info->main_segments *
3613 DEF_RECLAIM_PREFREE_SEGMENTS / 100;
Jaegeuk Kim44a83492016-07-13 18:23:35 -07003614 if (sm_info->rec_prefree_segments > DEF_MAX_RECLAIM_PREFREE_SEGMENTS)
3615 sm_info->rec_prefree_segments = DEF_MAX_RECLAIM_PREFREE_SEGMENTS;
3616
Jaegeuk Kim52763a42016-06-13 09:47:48 -07003617 if (!test_opt(sbi, LFS))
3618 sm_info->ipu_policy = 1 << F2FS_IPU_FSYNC;
Jaegeuk Kim216fbd62013-11-07 13:13:42 +09003619 sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -07003620 sm_info->min_fsync_blocks = DEF_MIN_FSYNC_BLOCKS;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003621 sm_info->min_hot_blocks = DEF_MIN_HOT_BLOCKS;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09003622
Jaegeuk Kimbba681c2015-01-26 17:41:23 -08003623 sm_info->trim_sections = DEF_BATCHED_TRIM_SECTIONS;
3624
Chao Yu184a5cd2014-09-04 18:13:01 +08003625 INIT_LIST_HEAD(&sm_info->sit_entry_set);
3626
Yunlei Head9baf42017-06-01 16:43:51 +08003627 if (!f2fs_readonly(sbi->sb)) {
Gu Zheng2163d192014-04-27 14:21:33 +08003628 err = create_flush_cmd_control(sbi);
3629 if (err)
Gu Zhenga688b9d9e2014-04-27 14:21:21 +08003630 return err;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +09003631 }
3632
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003633 err = create_discard_cmd_control(sbi);
3634 if (err)
3635 return err;
3636
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003637 err = build_sit_info(sbi);
3638 if (err)
3639 return err;
3640 err = build_free_segmap(sbi);
3641 if (err)
3642 return err;
3643 err = build_curseg(sbi);
3644 if (err)
3645 return err;
3646
3647 /* reinit free segmap based on SIT */
3648 build_sit_entries(sbi);
3649
3650 init_free_segmap(sbi);
3651 err = build_dirty_segmap(sbi);
3652 if (err)
3653 return err;
3654
3655 init_min_max_mtime(sbi);
3656 return 0;
3657}
3658
3659static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
3660 enum dirty_type dirty_type)
3661{
3662 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
3663
3664 mutex_lock(&dirty_i->seglist_lock);
Jaegeuk Kim39307a82015-09-22 13:50:47 -07003665 kvfree(dirty_i->dirty_segmap[dirty_type]);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003666 dirty_i->nr_dirty[dirty_type] = 0;
3667 mutex_unlock(&dirty_i->seglist_lock);
3668}
3669
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09003670static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003671{
3672 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim39307a82015-09-22 13:50:47 -07003673 kvfree(dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003674}
3675
3676static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
3677{
3678 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
3679 int i;
3680
3681 if (!dirty_i)
3682 return;
3683
3684 /* discard pre-free/dirty segments list */
3685 for (i = 0; i < NR_DIRTY_TYPE; i++)
3686 discard_dirty_segmap(sbi, i);
3687
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09003688 destroy_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003689 SM_I(sbi)->dirty_info = NULL;
3690 kfree(dirty_i);
3691}
3692
3693static void destroy_curseg(struct f2fs_sb_info *sbi)
3694{
3695 struct curseg_info *array = SM_I(sbi)->curseg_array;
3696 int i;
3697
3698 if (!array)
3699 return;
3700 SM_I(sbi)->curseg_array = NULL;
Chao Yub7ad7512016-02-19 18:08:46 +08003701 for (i = 0; i < NR_CURSEG_TYPE; i++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003702 kfree(array[i].sum_blk);
Chao Yub7ad7512016-02-19 18:08:46 +08003703 kfree(array[i].journal);
3704 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003705 kfree(array);
3706}
3707
3708static void destroy_free_segmap(struct f2fs_sb_info *sbi)
3709{
3710 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
3711 if (!free_i)
3712 return;
3713 SM_I(sbi)->free_info = NULL;
Jaegeuk Kim39307a82015-09-22 13:50:47 -07003714 kvfree(free_i->free_segmap);
3715 kvfree(free_i->free_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003716 kfree(free_i);
3717}
3718
3719static void destroy_sit_info(struct f2fs_sb_info *sbi)
3720{
3721 struct sit_info *sit_i = SIT_I(sbi);
3722 unsigned int start;
3723
3724 if (!sit_i)
3725 return;
3726
3727 if (sit_i->sentries) {
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003728 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003729 kfree(sit_i->sentries[start].cur_valid_map);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003730#ifdef CONFIG_F2FS_CHECK_FS
3731 kfree(sit_i->sentries[start].cur_valid_map_mir);
3732#endif
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003733 kfree(sit_i->sentries[start].ckpt_valid_map);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07003734 kfree(sit_i->sentries[start].discard_map);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003735 }
3736 }
Jaegeuk Kim60a3b782015-02-10 16:44:29 -08003737 kfree(sit_i->tmp_map);
3738
Jaegeuk Kim39307a82015-09-22 13:50:47 -07003739 kvfree(sit_i->sentries);
3740 kvfree(sit_i->sec_entries);
3741 kvfree(sit_i->dirty_sentries_bitmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003742
3743 SM_I(sbi)->sit_info = NULL;
3744 kfree(sit_i->sit_bitmap);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003745#ifdef CONFIG_F2FS_CHECK_FS
3746 kfree(sit_i->sit_bitmap_mir);
3747#endif
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003748 kfree(sit_i);
3749}
3750
3751void destroy_segment_manager(struct f2fs_sb_info *sbi)
3752{
3753 struct f2fs_sm_info *sm_info = SM_I(sbi);
Gu Zhenga688b9d9e2014-04-27 14:21:21 +08003754
Chao Yu3b03f722013-11-06 09:12:04 +08003755 if (!sm_info)
3756 return;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003757 destroy_flush_cmd_control(sbi, true);
3758 destroy_discard_cmd_control(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003759 destroy_dirty_segmap(sbi);
3760 destroy_curseg(sbi);
3761 destroy_free_segmap(sbi);
3762 destroy_sit_info(sbi);
3763 sbi->sm_info = NULL;
3764 kfree(sm_info);
3765}
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09003766
3767int __init create_segment_manager_caches(void)
3768{
3769 discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
Gu Zhenge8512d22014-03-07 18:43:28 +08003770 sizeof(struct discard_entry));
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09003771 if (!discard_entry_slab)
Chao Yu184a5cd2014-09-04 18:13:01 +08003772 goto fail;
3773
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003774 discard_cmd_slab = f2fs_kmem_cache_create("discard_cmd",
3775 sizeof(struct discard_cmd));
3776 if (!discard_cmd_slab)
Chao Yu6ab2a302016-09-05 12:28:26 +08003777 goto destroy_discard_entry;
Chao Yu275b66b2016-08-29 23:58:34 +08003778
Chao Yu184a5cd2014-09-04 18:13:01 +08003779 sit_entry_set_slab = f2fs_kmem_cache_create("sit_entry_set",
Changman Leec9ee0082014-11-21 15:42:07 +09003780 sizeof(struct sit_entry_set));
Chao Yu184a5cd2014-09-04 18:13:01 +08003781 if (!sit_entry_set_slab)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003782 goto destroy_discard_cmd;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07003783
3784 inmem_entry_slab = f2fs_kmem_cache_create("inmem_page_entry",
3785 sizeof(struct inmem_pages));
3786 if (!inmem_entry_slab)
3787 goto destroy_sit_entry_set;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09003788 return 0;
Chao Yu184a5cd2014-09-04 18:13:01 +08003789
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07003790destroy_sit_entry_set:
3791 kmem_cache_destroy(sit_entry_set_slab);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003792destroy_discard_cmd:
3793 kmem_cache_destroy(discard_cmd_slab);
Chao Yu6ab2a302016-09-05 12:28:26 +08003794destroy_discard_entry:
Chao Yu184a5cd2014-09-04 18:13:01 +08003795 kmem_cache_destroy(discard_entry_slab);
3796fail:
3797 return -ENOMEM;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09003798}
3799
3800void destroy_segment_manager_caches(void)
3801{
Chao Yu184a5cd2014-09-04 18:13:01 +08003802 kmem_cache_destroy(sit_entry_set_slab);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003803 kmem_cache_destroy(discard_cmd_slab);
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09003804 kmem_cache_destroy(discard_entry_slab);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07003805 kmem_cache_destroy(inmem_entry_slab);
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09003806}