blob: 20650e25117b296b01b3aaa541a0552b8654e070 [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 Kim1d7be272017-05-17 10:36:58 -070019#include <linux/freezer.h>
Jaegeuk Kim1eb1ef42017-09-09 12:03:23 -070020#include <linux/sched/signal.h>
Jaegeuk Kim351df4b2012-11-02 17:09:16 +090021
22#include "f2fs.h"
23#include "segment.h"
24#include "node.h"
Jaegeuk Kim5f656542017-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 Kimb01a9202017-01-09 14:13:03 -080032static 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
Chao Yu4d57b862018-05-30 00:20:41 +0800172bool f2fs_need_SSR(struct f2fs_sb_info *sbi)
Jaegeuk Kimb3a97a22017-09-09 11:11:04 -0700173{
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;
Jaegeuk Kim5b0e9532018-05-07 14:22:40 -0700180 if (sbi->gc_mode == GC_URGENT)
Jaegeuk Kimb3a97a22017-09-09 11:11:04 -0700181 return true;
182
183 return free_sections(sbi) <= (node_secs + 2 * dent_secs + imeta_secs +
Chao Yua2a12b62017-10-28 16:52:33 +0800184 SM_I(sbi)->min_ssr_sections + reserved_sections(sbi));
Jaegeuk Kimb3a97a22017-09-09 11:11:04 -0700185}
186
Chao Yu4d57b862018-05-30 00:20:41 +0800187void f2fs_register_inmem_page(struct inode *inode, struct page *page)
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700188{
Jaegeuk Kim57864ae2017-10-18 19:05:57 -0700189 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700190 struct f2fs_inode_info *fi = F2FS_I(inode);
191 struct inmem_pages *new;
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800192
Jaegeuk Kim9e4ded32014-12-17 19:58:58 -0800193 f2fs_trace_pid(page);
Jaegeuk Kim0722b102014-12-05 11:58:02 -0800194
Chao Yudecd36b2015-08-07 18:42:09 +0800195 set_page_private(page, (unsigned long)ATOMIC_WRITTEN_PAGE);
196 SetPagePrivate(page);
197
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700198 new = f2fs_kmem_cache_alloc(inmem_entry_slab, GFP_NOFS);
199
200 /* add atomic page indices to the list */
201 new->page = page;
202 INIT_LIST_HEAD(&new->list);
Chao Yudecd36b2015-08-07 18:42:09 +0800203
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700204 /* increase reference count with clean state */
205 mutex_lock(&fi->inmem_lock);
206 get_page(page);
207 list_add_tail(&new->list, &fi->inmem_pages);
Jaegeuk Kim57864ae2017-10-18 19:05:57 -0700208 spin_lock(&sbi->inode_lock[ATOMIC_FILE]);
209 if (list_empty(&fi->inmem_ilist))
210 list_add_tail(&fi->inmem_ilist, &sbi->inode_list[ATOMIC_FILE]);
211 spin_unlock(&sbi->inode_lock[ATOMIC_FILE]);
Jaegeuk Kim8dcf2ff72014-12-05 17:18:15 -0800212 inc_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700213 mutex_unlock(&fi->inmem_lock);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -0700214
215 trace_f2fs_register_inmem_page(page, INMEM);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700216}
217
Chao Yu28bc1062016-02-06 14:40:34 +0800218static int __revoke_inmem_pages(struct inode *inode,
219 struct list_head *head, bool drop, bool recover)
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700220{
Chao Yu28bc1062016-02-06 14:40:34 +0800221 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700222 struct inmem_pages *cur, *tmp;
Chao Yu28bc1062016-02-06 14:40:34 +0800223 int err = 0;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700224
Chao Yu29b96b52016-02-06 14:38:29 +0800225 list_for_each_entry_safe(cur, tmp, head, list) {
Chao Yu28bc1062016-02-06 14:40:34 +0800226 struct page *page = cur->page;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700227
Chao Yu28bc1062016-02-06 14:40:34 +0800228 if (drop)
229 trace_f2fs_commit_inmem_page(page, INMEM_DROP);
230
231 lock_page(page);
232
Chao Yue5e57322018-04-23 10:36:13 +0800233 f2fs_wait_on_page_writeback(page, DATA, true);
234
Chao Yu28bc1062016-02-06 14:40:34 +0800235 if (recover) {
236 struct dnode_of_data dn;
237 struct node_info ni;
238
239 trace_f2fs_commit_inmem_page(page, INMEM_REVOKE);
Chao Yu7f2b4e82017-08-08 19:09:08 +0800240retry:
Chao Yu28bc1062016-02-06 14:40:34 +0800241 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +0800242 err = f2fs_get_dnode_of_data(&dn, page->index,
243 LOOKUP_NODE);
Chao Yu7f2b4e82017-08-08 19:09:08 +0800244 if (err) {
245 if (err == -ENOMEM) {
246 congestion_wait(BLK_RW_ASYNC, HZ/50);
247 cond_resched();
248 goto retry;
249 }
Chao Yu28bc1062016-02-06 14:40:34 +0800250 err = -EAGAIN;
251 goto next;
252 }
Chao Yu77357302018-07-17 00:02:17 +0800253
254 err = f2fs_get_node_info(sbi, dn.nid, &ni);
255 if (err) {
256 f2fs_put_dnode(&dn);
257 return err;
258 }
259
Daeho Jeongf1d25642018-01-10 16:49:10 +0900260 if (cur->old_addr == NEW_ADDR) {
Chao Yu4d57b862018-05-30 00:20:41 +0800261 f2fs_invalidate_blocks(sbi, dn.data_blkaddr);
Daeho Jeongf1d25642018-01-10 16:49:10 +0900262 f2fs_update_data_blkaddr(&dn, NEW_ADDR);
263 } else
264 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
Chao Yu28bc1062016-02-06 14:40:34 +0800265 cur->old_addr, ni.version, true, true);
266 f2fs_put_dnode(&dn);
267 }
268next:
Jaegeuk Kim63c52d72016-04-12 14:11:03 -0700269 /* we don't need to invalidate this in the sccessful status */
270 if (drop || recover)
271 ClearPageUptodate(page);
Chao Yu28bc1062016-02-06 14:40:34 +0800272 set_page_private(page, 0);
Chao Yuc81ced02016-04-29 20:13:36 +0800273 ClearPagePrivate(page);
Chao Yu28bc1062016-02-06 14:40:34 +0800274 f2fs_put_page(page, 1);
Chao Yudecd36b2015-08-07 18:42:09 +0800275
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700276 list_del(&cur->list);
277 kmem_cache_free(inmem_entry_slab, cur);
Jaegeuk Kim8dcf2ff72014-12-05 17:18:15 -0800278 dec_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700279 }
Chao Yu28bc1062016-02-06 14:40:34 +0800280 return err;
Chao Yu29b96b52016-02-06 14:38:29 +0800281}
282
Chao Yu4d57b862018-05-30 00:20:41 +0800283void f2fs_drop_inmem_pages_all(struct f2fs_sb_info *sbi, bool gc_failure)
Jaegeuk Kim57864ae2017-10-18 19:05:57 -0700284{
285 struct list_head *head = &sbi->inode_list[ATOMIC_FILE];
286 struct inode *inode;
287 struct f2fs_inode_info *fi;
288next:
289 spin_lock(&sbi->inode_lock[ATOMIC_FILE]);
290 if (list_empty(head)) {
291 spin_unlock(&sbi->inode_lock[ATOMIC_FILE]);
292 return;
293 }
294 fi = list_first_entry(head, struct f2fs_inode_info, inmem_ilist);
295 inode = igrab(&fi->vfs_inode);
296 spin_unlock(&sbi->inode_lock[ATOMIC_FILE]);
297
298 if (inode) {
Chao Yu2ef79ec2018-05-07 20:28:54 +0800299 if (gc_failure) {
300 if (fi->i_gc_failures[GC_FAILURE_ATOMIC])
301 goto drop;
302 goto skip;
303 }
304drop:
305 set_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
Chao Yu4d57b862018-05-30 00:20:41 +0800306 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim57864ae2017-10-18 19:05:57 -0700307 iput(inode);
308 }
Chao Yu2ef79ec2018-05-07 20:28:54 +0800309skip:
Jaegeuk Kim57864ae2017-10-18 19:05:57 -0700310 congestion_wait(BLK_RW_ASYNC, HZ/50);
311 cond_resched();
312 goto next;
313}
314
Chao Yu4d57b862018-05-30 00:20:41 +0800315void f2fs_drop_inmem_pages(struct inode *inode)
Chao Yu29b96b52016-02-06 14:38:29 +0800316{
Jaegeuk Kim57864ae2017-10-18 19:05:57 -0700317 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu29b96b52016-02-06 14:38:29 +0800318 struct f2fs_inode_info *fi = F2FS_I(inode);
319
320 mutex_lock(&fi->inmem_lock);
Chao Yu28bc1062016-02-06 14:40:34 +0800321 __revoke_inmem_pages(inode, &fi->inmem_pages, true, false);
Jaegeuk Kim57864ae2017-10-18 19:05:57 -0700322 spin_lock(&sbi->inode_lock[ATOMIC_FILE]);
323 if (!list_empty(&fi->inmem_ilist))
324 list_del_init(&fi->inmem_ilist);
325 spin_unlock(&sbi->inode_lock[ATOMIC_FILE]);
Chao Yu29b96b52016-02-06 14:38:29 +0800326 mutex_unlock(&fi->inmem_lock);
Chao Yu5fe45742017-01-07 18:50:26 +0800327
328 clear_inode_flag(inode, FI_ATOMIC_FILE);
Chao Yu2ef79ec2018-05-07 20:28:54 +0800329 fi->i_gc_failures[GC_FAILURE_ATOMIC] = 0;
Chao Yu5fe45742017-01-07 18:50:26 +0800330 stat_dec_atomic_write(inode);
Chao Yu29b96b52016-02-06 14:38:29 +0800331}
332
Chao Yu4d57b862018-05-30 00:20:41 +0800333void f2fs_drop_inmem_page(struct inode *inode, struct page *page)
Jaegeuk Kim8c242db2017-03-17 09:55:52 +0800334{
335 struct f2fs_inode_info *fi = F2FS_I(inode);
336 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
337 struct list_head *head = &fi->inmem_pages;
338 struct inmem_pages *cur = NULL;
339
340 f2fs_bug_on(sbi, !IS_ATOMIC_WRITTEN_PAGE(page));
341
342 mutex_lock(&fi->inmem_lock);
343 list_for_each_entry(cur, head, list) {
344 if (cur->page == page)
345 break;
346 }
347
Sheng Yongd0891e82018-04-17 17:12:27 +0800348 f2fs_bug_on(sbi, list_empty(head) || cur->page != page);
Jaegeuk Kim8c242db2017-03-17 09:55:52 +0800349 list_del(&cur->list);
350 mutex_unlock(&fi->inmem_lock);
351
352 dec_page_count(sbi, F2FS_INMEM_PAGES);
353 kmem_cache_free(inmem_entry_slab, cur);
354
355 ClearPageUptodate(page);
356 set_page_private(page, 0);
357 ClearPagePrivate(page);
358 f2fs_put_page(page, 0);
359
360 trace_f2fs_commit_inmem_page(page, INMEM_INVALIDATE);
361}
362
Chao Yu4d57b862018-05-30 00:20:41 +0800363static int __f2fs_commit_inmem_pages(struct inode *inode)
Chao Yu29b96b52016-02-06 14:38:29 +0800364{
365 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
366 struct f2fs_inode_info *fi = F2FS_I(inode);
367 struct inmem_pages *cur, *tmp;
368 struct f2fs_io_info fio = {
369 .sbi = sbi,
Chao Yu39d787b2017-09-29 13:59:38 +0800370 .ino = inode->i_ino,
Chao Yu29b96b52016-02-06 14:38:29 +0800371 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -0500372 .op = REQ_OP_WRITE,
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600373 .op_flags = REQ_SYNC | REQ_PRIO,
Chao Yub0af6d42017-08-02 23:21:48 +0800374 .io_type = FS_DATA_IO,
Chao Yu29b96b52016-02-06 14:38:29 +0800375 };
Chao Yucf52b272018-04-23 10:36:14 +0800376 struct list_head revoke_list;
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800377 pgoff_t last_idx = ULONG_MAX;
Chao Yu29b96b52016-02-06 14:38:29 +0800378 int err = 0;
379
Chao Yucf52b272018-04-23 10:36:14 +0800380 INIT_LIST_HEAD(&revoke_list);
381
Chao Yu29b96b52016-02-06 14:38:29 +0800382 list_for_each_entry_safe(cur, tmp, &fi->inmem_pages, list) {
Chao Yu28bc1062016-02-06 14:40:34 +0800383 struct page *page = cur->page;
384
385 lock_page(page);
386 if (page->mapping == inode->i_mapping) {
387 trace_f2fs_commit_inmem_page(page, INMEM);
388
389 set_page_dirty(page);
390 f2fs_wait_on_page_writeback(page, DATA, true);
Chao Yu933439c2016-10-11 22:57:01 +0800391 if (clear_page_dirty_for_io(page)) {
Chao Yu29b96b52016-02-06 14:38:29 +0800392 inode_dec_dirty_pages(inode);
Chao Yu4d57b862018-05-30 00:20:41 +0800393 f2fs_remove_dirty_inode(inode);
Chao Yu933439c2016-10-11 22:57:01 +0800394 }
Jaegeuk Kim640cc182017-07-19 10:59:55 -0700395retry:
Chao Yu28bc1062016-02-06 14:40:34 +0800396 fio.page = page;
Hou Pengyange959c8f2017-04-25 12:45:13 +0000397 fio.old_blkaddr = NULL_ADDR;
Jaegeuk Kim4d978072017-04-26 11:11:12 -0700398 fio.encrypted_page = NULL;
Jaegeuk Kimcc156202017-05-12 13:51:34 -0700399 fio.need_lock = LOCK_DONE;
Chao Yu4d57b862018-05-30 00:20:41 +0800400 err = f2fs_do_write_data_page(&fio);
Chao Yu29b96b52016-02-06 14:38:29 +0800401 if (err) {
Jaegeuk Kim640cc182017-07-19 10:59:55 -0700402 if (err == -ENOMEM) {
403 congestion_wait(BLK_RW_ASYNC, HZ/50);
404 cond_resched();
405 goto retry;
406 }
Chao Yu28bc1062016-02-06 14:40:34 +0800407 unlock_page(page);
Chao Yu29b96b52016-02-06 14:38:29 +0800408 break;
409 }
Chao Yu28bc1062016-02-06 14:40:34 +0800410 /* record old blkaddr for revoking */
411 cur->old_addr = fio.old_blkaddr;
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800412 last_idx = page->index;
Chao Yu29b96b52016-02-06 14:38:29 +0800413 }
Chao Yu28bc1062016-02-06 14:40:34 +0800414 unlock_page(page);
Chao Yucf52b272018-04-23 10:36:14 +0800415 list_move_tail(&cur->list, &revoke_list);
Chao Yu29b96b52016-02-06 14:38:29 +0800416 }
417
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800418 if (last_idx != ULONG_MAX)
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700419 f2fs_submit_merged_write_cond(sbi, inode, 0, last_idx, DATA);
Chao Yu28bc1062016-02-06 14:40:34 +0800420
Chao Yu28bc1062016-02-06 14:40:34 +0800421 if (err) {
Chao Yu28bc1062016-02-06 14:40:34 +0800422 /*
423 * try to revoke all committed pages, but still we could fail
424 * due to no memory or other reason, if that happened, EAGAIN
425 * will be returned, which means in such case, transaction is
426 * already not integrity, caller should use journal to do the
427 * recovery or rewrite & commit last transaction. For other
428 * error number, revoking was done by filesystem itself.
429 */
Chao Yucf52b272018-04-23 10:36:14 +0800430 err = __revoke_inmem_pages(inode, &revoke_list, false, true);
Chao Yu28bc1062016-02-06 14:40:34 +0800431
432 /* drop all uncommitted pages */
433 __revoke_inmem_pages(inode, &fi->inmem_pages, true, false);
Chao Yucf52b272018-04-23 10:36:14 +0800434 } else {
435 __revoke_inmem_pages(inode, &revoke_list, false, false);
Chao Yu28bc1062016-02-06 14:40:34 +0800436 }
Chao Yucf52b272018-04-23 10:36:14 +0800437
438 return err;
439}
440
Chao Yu4d57b862018-05-30 00:20:41 +0800441int f2fs_commit_inmem_pages(struct inode *inode)
Chao Yucf52b272018-04-23 10:36:14 +0800442{
443 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
444 struct f2fs_inode_info *fi = F2FS_I(inode);
445 int err;
446
447 f2fs_balance_fs(sbi, true);
448 f2fs_lock_op(sbi);
449
450 set_inode_flag(inode, FI_ATOMIC_COMMIT);
451
452 mutex_lock(&fi->inmem_lock);
Chao Yu4d57b862018-05-30 00:20:41 +0800453 err = __f2fs_commit_inmem_pages(inode);
Chao Yucf52b272018-04-23 10:36:14 +0800454
Jaegeuk Kim57864ae2017-10-18 19:05:57 -0700455 spin_lock(&sbi->inode_lock[ATOMIC_FILE]);
456 if (!list_empty(&fi->inmem_ilist))
457 list_del_init(&fi->inmem_ilist);
458 spin_unlock(&sbi->inode_lock[ATOMIC_FILE]);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700459 mutex_unlock(&fi->inmem_lock);
460
Chao Yu5fe45742017-01-07 18:50:26 +0800461 clear_inode_flag(inode, FI_ATOMIC_COMMIT);
462
Chao Yu29b96b52016-02-06 14:38:29 +0800463 f2fs_unlock_op(sbi);
Jaegeuk Kimedb27de2015-07-25 00:52:52 -0700464 return err;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700465}
466
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900467/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900468 * This function balances dirty node and dentry pages.
469 * In addition, it controls garbage collection.
470 */
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800471void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900472{
Chao Yu55523512017-02-25 11:08:28 +0800473 if (time_to_inject(sbi, FAULT_CHECKPOINT)) {
474 f2fs_show_injection_info(FAULT_CHECKPOINT);
Chao Yu0f348022016-09-26 19:45:55 +0800475 f2fs_stop_checkpoint(sbi, false);
Chao Yu55523512017-02-25 11:08:28 +0800476 }
Chao Yu0f348022016-09-26 19:45:55 +0800477
Jaegeuk Kime589c2c2016-06-02 15:24:24 -0700478 /* balance_fs_bg is able to be pending */
Jaegeuk Kima7881892017-04-20 13:51:57 -0700479 if (need && excess_cached_nats(sbi))
Jaegeuk Kime589c2c2016-06-02 15:24:24 -0700480 f2fs_balance_fs_bg(sbi);
481
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900482 /*
Jaegeuk Kim029cd282012-12-21 17:20:21 +0900483 * We should do GC or end up with checkpoint, if there are so many dirty
484 * dir/node pages without enough free segments.
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900485 */
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -0700486 if (has_not_enough_free_secs(sbi, 0, 0)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900487 mutex_lock(&sbi->gc_mutex);
Jaegeuk Kime066b832017-04-13 15:17:00 -0700488 f2fs_gc(sbi, false, false, NULL_SEGNO);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900489 }
490}
491
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900492void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi)
493{
Chao Yu64c74a72018-05-26 18:03:34 +0800494 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
495 return;
496
Chao Yu1dcc3362015-02-05 17:57:31 +0800497 /* try to shrink extent cache when there is no enough memory */
Chao Yu4d57b862018-05-30 00:20:41 +0800498 if (!f2fs_available_free_memory(sbi, EXTENT_CACHE))
Jaegeuk Kim554df792015-06-19 13:41:23 -0700499 f2fs_shrink_extent_tree(sbi, EXTENT_CACHE_SHRINK_NUMBER);
Chao Yu1dcc3362015-02-05 17:57:31 +0800500
Jaegeuk Kim1b38dc82015-06-19 15:36:07 -0700501 /* check the # of cached NAT entries */
Chao Yu4d57b862018-05-30 00:20:41 +0800502 if (!f2fs_available_free_memory(sbi, NAT_ENTRIES))
503 f2fs_try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK);
Jaegeuk Kim1b38dc82015-06-19 15:36:07 -0700504
Chao Yu4d57b862018-05-30 00:20:41 +0800505 if (!f2fs_available_free_memory(sbi, FREE_NIDS))
506 f2fs_try_to_free_nids(sbi, MAX_FREE_NIDS);
Jaegeuk Kimad4edb82016-06-16 16:41:49 -0700507 else
Chao Yu4d57b862018-05-30 00:20:41 +0800508 f2fs_build_free_nids(sbi, false, false);
Chao Yu31696582015-07-28 18:33:46 +0800509
Chao Yufd8c8ca2018-07-25 19:16:21 +0800510 if (!is_idle(sbi) &&
511 (!excess_dirty_nats(sbi) && !excess_dirty_nodes(sbi)))
Jaegeuk Kimf455c8a2016-12-05 11:37:14 -0800512 return;
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -0800513
Jaegeuk Kim88a70a62014-12-10 15:20:48 -0800514 /* checkpoint is the only way to shrink partial cached entries */
Chao Yu4d57b862018-05-30 00:20:41 +0800515 if (!f2fs_available_free_memory(sbi, NAT_ENTRIES) ||
516 !f2fs_available_free_memory(sbi, INO_ENTRIES) ||
Chao Yu7d768d22016-01-18 18:31:18 +0800517 excess_prefree_segs(sbi) ||
518 excess_dirty_nats(sbi) ||
Chao Yufd8c8ca2018-07-25 19:16:21 +0800519 excess_dirty_nodes(sbi) ||
Jaegeuk Kimf455c8a2016-12-05 11:37:14 -0800520 f2fs_time_over(sbi, CP_TIME)) {
Chao Yue9f5b8b2016-02-14 18:54:33 +0800521 if (test_opt(sbi, DATA_FLUSH)) {
522 struct blk_plug plug;
523
524 blk_start_plug(&plug);
Chao Yu4d57b862018-05-30 00:20:41 +0800525 f2fs_sync_dirty_inodes(sbi, FILE_INODE);
Chao Yue9f5b8b2016-02-14 18:54:33 +0800526 blk_finish_plug(&plug);
527 }
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900528 f2fs_sync_fs(sbi->sb, true);
Jaegeuk Kim42190d22016-01-09 13:45:17 -0800529 stat_inc_bg_cp_count(sbi->stat_info);
Chao Yu36b35a02015-12-17 17:13:28 +0800530 }
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900531}
532
Kinglong Mee20fda56b2017-03-04 22:13:10 +0800533static int __submit_flush_wait(struct f2fs_sb_info *sbi,
534 struct block_device *bdev)
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700535{
Chao Yud62fe972017-10-28 16:52:31 +0800536 struct bio *bio = f2fs_bio_alloc(sbi, 0, true);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700537 int ret;
538
Jan Kara3adc5fcb2017-05-02 17:03:47 +0200539 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200540 bio_set_dev(bio, bdev);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700541 ret = submit_bio_wait(bio);
542 bio_put(bio);
Kinglong Mee20fda56b2017-03-04 22:13:10 +0800543
544 trace_f2fs_issue_flush(bdev, test_opt(sbi, NOBARRIER),
545 test_opt(sbi, FLUSH_MERGE), ret);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700546 return ret;
547}
548
Chao Yu39d787b2017-09-29 13:59:38 +0800549static int submit_flush_wait(struct f2fs_sb_info *sbi, nid_t ino)
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700550{
Chao Yu39d787b2017-09-29 13:59:38 +0800551 int ret = 0;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700552 int i;
553
Chao Yu39d787b2017-09-29 13:59:38 +0800554 if (!sbi->s_ndevs)
555 return __submit_flush_wait(sbi, sbi->sb->s_bdev);
Kinglong Mee20fda56b2017-03-04 22:13:10 +0800556
Chao Yu39d787b2017-09-29 13:59:38 +0800557 for (i = 0; i < sbi->s_ndevs; i++) {
Chao Yu4d57b862018-05-30 00:20:41 +0800558 if (!f2fs_is_dirty_device(sbi, ino, i, FLUSH_INO))
Chao Yu39d787b2017-09-29 13:59:38 +0800559 continue;
Kinglong Mee20fda56b2017-03-04 22:13:10 +0800560 ret = __submit_flush_wait(sbi, FDEV(i).bdev);
561 if (ret)
562 break;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700563 }
564 return ret;
565}
566
Gu Zheng2163d192014-04-27 14:21:33 +0800567static int issue_flush_thread(void *data)
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900568{
569 struct f2fs_sb_info *sbi = data;
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800570 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800571 wait_queue_head_t *q = &fcc->flush_wait_queue;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900572repeat:
573 if (kthread_should_stop())
574 return 0;
575
Chao Yudc6febb2017-07-22 08:52:23 +0800576 sb_start_intwrite(sbi->sb);
577
Gu Zheng721bd4d2014-09-05 18:31:00 +0800578 if (!llist_empty(&fcc->issue_list)) {
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900579 struct flush_cmd *cmd, *next;
580 int ret;
581
Gu Zheng721bd4d2014-09-05 18:31:00 +0800582 fcc->dispatch_list = llist_del_all(&fcc->issue_list);
583 fcc->dispatch_list = llist_reverse_order(fcc->dispatch_list);
584
Chao Yu39d787b2017-09-29 13:59:38 +0800585 cmd = llist_entry(fcc->dispatch_list, struct flush_cmd, llnode);
586
587 ret = submit_flush_wait(sbi, cmd->ino);
Chao Yu8b8dd652017-03-25 17:19:58 +0800588 atomic_inc(&fcc->issued_flush);
589
Gu Zheng721bd4d2014-09-05 18:31:00 +0800590 llist_for_each_entry_safe(cmd, next,
591 fcc->dispatch_list, llnode) {
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900592 cmd->ret = ret;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900593 complete(&cmd->wait);
594 }
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800595 fcc->dispatch_list = NULL;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900596 }
597
Chao Yudc6febb2017-07-22 08:52:23 +0800598 sb_end_intwrite(sbi->sb);
599
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800600 wait_event_interruptible(*q,
Gu Zheng721bd4d2014-09-05 18:31:00 +0800601 kthread_should_stop() || !llist_empty(&fcc->issue_list));
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900602 goto repeat;
603}
604
Chao Yu39d787b2017-09-29 13:59:38 +0800605int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino)
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900606{
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800607 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
Chao Yuadf8d902014-05-08 17:00:35 +0800608 struct flush_cmd cmd;
Chao Yu8b8dd652017-03-25 17:19:58 +0800609 int ret;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900610
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700611 if (test_opt(sbi, NOBARRIER))
612 return 0;
613
Chao Yu8b8dd652017-03-25 17:19:58 +0800614 if (!test_opt(sbi, FLUSH_MERGE)) {
Chao Yu39d787b2017-09-29 13:59:38 +0800615 ret = submit_flush_wait(sbi, ino);
Chao Yu8b8dd652017-03-25 17:19:58 +0800616 atomic_inc(&fcc->issued_flush);
617 return ret;
618 }
619
Chao Yu39d787b2017-09-29 13:59:38 +0800620 if (atomic_inc_return(&fcc->issing_flush) == 1 || sbi->s_ndevs > 1) {
621 ret = submit_flush_wait(sbi, ino);
Chao Yu8b8dd652017-03-25 17:19:58 +0800622 atomic_dec(&fcc->issing_flush);
623
624 atomic_inc(&fcc->issued_flush);
Jaegeuk Kim740432f2015-08-14 11:43:56 -0700625 return ret;
626 }
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900627
Chao Yu39d787b2017-09-29 13:59:38 +0800628 cmd.ino = ino;
Chao Yuadf8d902014-05-08 17:00:35 +0800629 init_completion(&cmd.wait);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900630
Gu Zheng721bd4d2014-09-05 18:31:00 +0800631 llist_add(&cmd.llnode, &fcc->issue_list);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900632
Chao Yu6f890df2017-08-21 22:53:45 +0800633 /* update issue_list before we wake up issue_flush thread */
634 smp_mb();
635
636 if (waitqueue_active(&fcc->flush_wait_queue))
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800637 wake_up(&fcc->flush_wait_queue);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900638
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800639 if (fcc->f2fs_issue_flush) {
640 wait_for_completion(&cmd.wait);
Chao Yu8b8dd652017-03-25 17:19:58 +0800641 atomic_dec(&fcc->issing_flush);
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800642 } else {
Chao Yud3238692017-08-31 18:56:06 +0800643 struct llist_node *list;
644
645 list = llist_del_all(&fcc->issue_list);
646 if (!list) {
647 wait_for_completion(&cmd.wait);
648 atomic_dec(&fcc->issing_flush);
649 } else {
650 struct flush_cmd *tmp, *next;
651
Chao Yu39d787b2017-09-29 13:59:38 +0800652 ret = submit_flush_wait(sbi, ino);
Chao Yud3238692017-08-31 18:56:06 +0800653
654 llist_for_each_entry_safe(tmp, next, list, llnode) {
655 if (tmp == &cmd) {
656 cmd.ret = ret;
657 atomic_dec(&fcc->issing_flush);
658 continue;
659 }
660 tmp->ret = ret;
661 complete(&tmp->wait);
662 }
663 }
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800664 }
Chao Yuadf8d902014-05-08 17:00:35 +0800665
666 return cmd.ret;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900667}
668
Chao Yu4d57b862018-05-30 00:20:41 +0800669int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi)
Gu Zheng2163d192014-04-27 14:21:33 +0800670{
671 dev_t dev = sbi->sb->s_bdev->bd_dev;
672 struct flush_cmd_control *fcc;
673 int err = 0;
674
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800675 if (SM_I(sbi)->fcc_info) {
676 fcc = SM_I(sbi)->fcc_info;
Yunlong Songd871cd02017-06-24 15:57:19 +0800677 if (fcc->f2fs_issue_flush)
678 return err;
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800679 goto init_thread;
680 }
681
Chao Yuacbf0542017-11-30 19:28:17 +0800682 fcc = f2fs_kzalloc(sbi, sizeof(struct flush_cmd_control), GFP_KERNEL);
Gu Zheng2163d192014-04-27 14:21:33 +0800683 if (!fcc)
684 return -ENOMEM;
Chao Yu8b8dd652017-03-25 17:19:58 +0800685 atomic_set(&fcc->issued_flush, 0);
686 atomic_set(&fcc->issing_flush, 0);
Gu Zheng2163d192014-04-27 14:21:33 +0800687 init_waitqueue_head(&fcc->flush_wait_queue);
Gu Zheng721bd4d2014-09-05 18:31:00 +0800688 init_llist_head(&fcc->issue_list);
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800689 SM_I(sbi)->fcc_info = fcc;
Yunlei Hed4fdf8b2017-06-01 16:43:51 +0800690 if (!test_opt(sbi, FLUSH_MERGE))
691 return err;
692
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800693init_thread:
Gu Zheng2163d192014-04-27 14:21:33 +0800694 fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi,
695 "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev));
696 if (IS_ERR(fcc->f2fs_issue_flush)) {
697 err = PTR_ERR(fcc->f2fs_issue_flush);
698 kfree(fcc);
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800699 SM_I(sbi)->fcc_info = NULL;
Gu Zheng2163d192014-04-27 14:21:33 +0800700 return err;
701 }
Gu Zheng2163d192014-04-27 14:21:33 +0800702
703 return err;
704}
705
Chao Yu4d57b862018-05-30 00:20:41 +0800706void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free)
Gu Zheng2163d192014-04-27 14:21:33 +0800707{
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800708 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
Gu Zheng2163d192014-04-27 14:21:33 +0800709
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800710 if (fcc && fcc->f2fs_issue_flush) {
711 struct task_struct *flush_thread = fcc->f2fs_issue_flush;
712
713 fcc->f2fs_issue_flush = NULL;
714 kthread_stop(flush_thread);
715 }
716 if (free) {
717 kfree(fcc);
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800718 SM_I(sbi)->fcc_info = NULL;
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800719 }
Gu Zheng2163d192014-04-27 14:21:33 +0800720}
721
Chao Yu1228b482017-09-29 13:59:39 +0800722int f2fs_flush_device_cache(struct f2fs_sb_info *sbi)
723{
724 int ret = 0, i;
725
726 if (!sbi->s_ndevs)
727 return 0;
728
729 for (i = 1; i < sbi->s_ndevs; i++) {
730 if (!f2fs_test_bit(i, (char *)&sbi->dirty_device))
731 continue;
732 ret = __submit_flush_wait(sbi, FDEV(i).bdev);
733 if (ret)
734 break;
735
736 spin_lock(&sbi->dev_lock);
737 f2fs_clear_bit(i, (char *)&sbi->dirty_device);
738 spin_unlock(&sbi->dev_lock);
739 }
740
741 return ret;
742}
743
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900744static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
745 enum dirty_type dirty_type)
746{
747 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
748
749 /* need not be added */
750 if (IS_CURSEG(sbi, segno))
751 return;
752
753 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
754 dirty_i->nr_dirty[dirty_type]++;
755
756 if (dirty_type == DIRTY) {
757 struct seg_entry *sentry = get_seg_entry(sbi, segno);
Changman Lee4625d6a2013-10-25 17:31:57 +0900758 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900759
Jaegeuk Kimec325b52014-09-02 16:24:11 -0700760 if (unlikely(t >= DIRTY)) {
761 f2fs_bug_on(sbi, 1);
762 return;
763 }
Changman Lee4625d6a2013-10-25 17:31:57 +0900764 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
765 dirty_i->nr_dirty[t]++;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900766 }
767}
768
769static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
770 enum dirty_type dirty_type)
771{
772 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
773
774 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
775 dirty_i->nr_dirty[dirty_type]--;
776
777 if (dirty_type == DIRTY) {
Changman Lee4625d6a2013-10-25 17:31:57 +0900778 struct seg_entry *sentry = get_seg_entry(sbi, segno);
779 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900780
Changman Lee4625d6a2013-10-25 17:31:57 +0900781 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
782 dirty_i->nr_dirty[t]--;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900783
Jaegeuk Kim302bd342017-04-07 14:33:22 -0700784 if (get_valid_blocks(sbi, segno, true) == 0)
Jaegeuk Kim4ddb1a42017-04-07 15:08:17 -0700785 clear_bit(GET_SEC_FROM_SEG(sbi, segno),
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +0900786 dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900787 }
788}
789
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900790/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900791 * Should not occur error such as -ENOMEM.
792 * Adding dirty entry into seglist is not critical operation.
793 * If a given segment is one of current working segments, it won't be added.
794 */
Haicheng Li8d8451a2013-06-13 16:59:28 +0800795static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900796{
797 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
798 unsigned short valid_blocks;
799
800 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
801 return;
802
803 mutex_lock(&dirty_i->seglist_lock);
804
Jaegeuk Kim302bd342017-04-07 14:33:22 -0700805 valid_blocks = get_valid_blocks(sbi, segno, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900806
807 if (valid_blocks == 0) {
808 __locate_dirty_segment(sbi, segno, PRE);
809 __remove_dirty_segment(sbi, segno, DIRTY);
810 } else if (valid_blocks < sbi->blocks_per_seg) {
811 __locate_dirty_segment(sbi, segno, DIRTY);
812 } else {
813 /* Recovery routine with SSR needs this */
814 __remove_dirty_segment(sbi, segno, DIRTY);
815 }
816
817 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900818}
819
Chao Yu004b6862017-04-14 23:24:55 +0800820static struct discard_cmd *__create_discard_cmd(struct f2fs_sb_info *sbi,
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800821 struct block_device *bdev, block_t lstart,
822 block_t start, block_t len)
Chao Yu275b66b2016-08-29 23:58:34 +0800823{
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -0800824 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Chao Yuba48a332017-04-15 14:09:37 +0800825 struct list_head *pend_list;
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800826 struct discard_cmd *dc;
Chao Yu275b66b2016-08-29 23:58:34 +0800827
Chao Yuba48a332017-04-15 14:09:37 +0800828 f2fs_bug_on(sbi, !len);
829
830 pend_list = &dcc->pend_list[plist_idx(len)];
831
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800832 dc = f2fs_kmem_cache_alloc(discard_cmd_slab, GFP_NOFS);
833 INIT_LIST_HEAD(&dc->list);
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800834 dc->bdev = bdev;
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800835 dc->lstart = lstart;
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800836 dc->start = start;
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800837 dc->len = len;
Chao Yuec9895a2017-04-26 17:39:54 +0800838 dc->ref = 0;
Jaegeuk Kim15469962017-01-09 20:32:07 -0800839 dc->state = D_PREP;
Chao Yu35ec7d52018-08-06 22:43:50 +0800840 dc->issuing = 0;
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800841 dc->error = 0;
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800842 init_completion(&dc->wait);
Chao Yu22d375d2017-04-05 18:19:48 +0800843 list_add_tail(&dc->list, pend_list);
Chao Yu35ec7d52018-08-06 22:43:50 +0800844 spin_lock_init(&dc->lock);
845 dc->bio_ref = 0;
Chao Yu5f323662017-03-25 17:19:59 +0800846 atomic_inc(&dcc->discard_cmd_cnt);
Chao Yud84d1cb2017-04-18 19:27:39 +0800847 dcc->undiscard_blks += len;
Chao Yu004b6862017-04-14 23:24:55 +0800848
849 return dc;
Jaegeuk Kim15469962017-01-09 20:32:07 -0800850}
851
Chao Yu004b6862017-04-14 23:24:55 +0800852static struct discard_cmd *__attach_discard_cmd(struct f2fs_sb_info *sbi,
853 struct block_device *bdev, block_t lstart,
854 block_t start, block_t len,
855 struct rb_node *parent, struct rb_node **p)
856{
857 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
858 struct discard_cmd *dc;
859
860 dc = __create_discard_cmd(sbi, bdev, lstart, start, len);
861
862 rb_link_node(&dc->rb_node, parent, p);
863 rb_insert_color(&dc->rb_node, &dcc->root);
864
865 return dc;
866}
867
868static void __detach_discard_cmd(struct discard_cmd_control *dcc,
869 struct discard_cmd *dc)
Jaegeuk Kim15469962017-01-09 20:32:07 -0800870{
Jaegeuk Kimdcc91652017-01-11 10:20:04 -0800871 if (dc->state == D_DONE)
Chao Yu35ec7d52018-08-06 22:43:50 +0800872 atomic_sub(dc->issuing, &dcc->issing_discard);
Chao Yu004b6862017-04-14 23:24:55 +0800873
874 list_del(&dc->list);
875 rb_erase(&dc->rb_node, &dcc->root);
Chao Yud84d1cb2017-04-18 19:27:39 +0800876 dcc->undiscard_blks -= dc->len;
Chao Yu004b6862017-04-14 23:24:55 +0800877
878 kmem_cache_free(discard_cmd_slab, dc);
879
880 atomic_dec(&dcc->discard_cmd_cnt);
881}
882
883static void __remove_discard_cmd(struct f2fs_sb_info *sbi,
884 struct discard_cmd *dc)
885{
886 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Chao Yu35ec7d52018-08-06 22:43:50 +0800887 unsigned long flags;
Jaegeuk Kimdcc91652017-01-11 10:20:04 -0800888
Chao Yu2ec6f2e2017-10-04 09:08:36 +0800889 trace_f2fs_remove_discard(dc->bdev, dc->start, dc->len);
890
Chao Yu35ec7d52018-08-06 22:43:50 +0800891 spin_lock_irqsave(&dc->lock, flags);
892 if (dc->bio_ref) {
893 spin_unlock_irqrestore(&dc->lock, flags);
894 return;
895 }
896 spin_unlock_irqrestore(&dc->lock, flags);
897
Chao Yud9703d92017-06-05 18:29:07 +0800898 f2fs_bug_on(sbi, dc->ref);
899
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800900 if (dc->error == -EOPNOTSUPP)
901 dc->error = 0;
Jaegeuk Kim15469962017-01-09 20:32:07 -0800902
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800903 if (dc->error)
Jaegeuk Kim15469962017-01-09 20:32:07 -0800904 f2fs_msg(sbi->sb, KERN_INFO,
Chao Yu04dfc232017-05-19 23:46:43 +0800905 "Issue discard(%u, %u, %u) failed, ret: %d",
906 dc->lstart, dc->start, dc->len, dc->error);
Chao Yu004b6862017-04-14 23:24:55 +0800907 __detach_discard_cmd(dcc, dc);
Chao Yu275b66b2016-08-29 23:58:34 +0800908}
909
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800910static void f2fs_submit_discard_endio(struct bio *bio)
911{
912 struct discard_cmd *dc = (struct discard_cmd *)bio->bi_private;
Chao Yu35ec7d52018-08-06 22:43:50 +0800913 unsigned long flags;
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800914
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200915 dc->error = blk_status_to_errno(bio->bi_status);
Chao Yu35ec7d52018-08-06 22:43:50 +0800916
917 spin_lock_irqsave(&dc->lock, flags);
918 dc->bio_ref--;
919 if (!dc->bio_ref && dc->state == D_SUBMIT) {
920 dc->state = D_DONE;
921 complete_all(&dc->wait);
922 }
923 spin_unlock_irqrestore(&dc->lock, flags);
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800924 bio_put(bio);
925}
926
Wei Yongjun94b1e102018-01-05 09:41:20 +0000927static void __check_sit_bitmap(struct f2fs_sb_info *sbi,
Chao Yu6915ea92017-06-30 17:19:02 +0800928 block_t start, block_t end)
929{
930#ifdef CONFIG_F2FS_CHECK_FS
931 struct seg_entry *sentry;
932 unsigned int segno;
933 block_t blk = start;
934 unsigned long offset, size, max_blocks = sbi->blocks_per_seg;
935 unsigned long *map;
936
937 while (blk < end) {
938 segno = GET_SEGNO(sbi, blk);
939 sentry = get_seg_entry(sbi, segno);
940 offset = GET_BLKOFF_FROM_SEG0(sbi, blk);
941
Yunlong Song008396e2017-08-04 17:07:15 +0800942 if (end < START_BLOCK(sbi, segno + 1))
943 size = GET_BLKOFF_FROM_SEG0(sbi, end);
944 else
945 size = max_blocks;
Chao Yu6915ea92017-06-30 17:19:02 +0800946 map = (unsigned long *)(sentry->cur_valid_map);
947 offset = __find_rev_next_bit(map, size, offset);
948 f2fs_bug_on(sbi, offset != size);
Yunlong Song008396e2017-08-04 17:07:15 +0800949 blk = START_BLOCK(sbi, segno + 1);
Chao Yu6915ea92017-06-30 17:19:02 +0800950 }
951#endif
952}
953
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -0700954static void __init_discard_policy(struct f2fs_sb_info *sbi,
955 struct discard_policy *dpolicy,
956 int discard_type, unsigned int granularity)
957{
958 /* common policy */
959 dpolicy->type = discard_type;
960 dpolicy->sync = true;
Chao Yu20ee4382018-07-08 22:11:01 +0800961 dpolicy->ordered = false;
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -0700962 dpolicy->granularity = granularity;
963
964 dpolicy->max_requests = DEF_MAX_DISCARD_REQUEST;
965 dpolicy->io_aware_gran = MAX_PLIST_NUM;
966
967 if (discard_type == DPOLICY_BG) {
968 dpolicy->min_interval = DEF_MIN_DISCARD_ISSUE_TIME;
Yunlei Hef9d1dce2018-04-08 15:11:11 +0800969 dpolicy->mid_interval = DEF_MID_DISCARD_ISSUE_TIME;
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -0700970 dpolicy->max_interval = DEF_MAX_DISCARD_ISSUE_TIME;
971 dpolicy->io_aware = true;
Chao Yucba60842018-04-10 15:43:09 +0800972 dpolicy->sync = false;
Chao Yu20ee4382018-07-08 22:11:01 +0800973 dpolicy->ordered = true;
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -0700974 if (utilization(sbi) > DEF_DISCARD_URGENT_UTIL) {
975 dpolicy->granularity = 1;
976 dpolicy->max_interval = DEF_MIN_DISCARD_ISSUE_TIME;
977 }
978 } else if (discard_type == DPOLICY_FORCE) {
979 dpolicy->min_interval = DEF_MIN_DISCARD_ISSUE_TIME;
Yunlei Hef9d1dce2018-04-08 15:11:11 +0800980 dpolicy->mid_interval = DEF_MID_DISCARD_ISSUE_TIME;
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -0700981 dpolicy->max_interval = DEF_MAX_DISCARD_ISSUE_TIME;
982 dpolicy->io_aware = false;
983 } else if (discard_type == DPOLICY_FSTRIM) {
984 dpolicy->io_aware = false;
985 } else if (discard_type == DPOLICY_UMOUNT) {
Yunlei He241b4932018-04-04 17:29:05 +0800986 dpolicy->max_requests = UINT_MAX;
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -0700987 dpolicy->io_aware = false;
988 }
989}
990
Chao Yu35ec7d52018-08-06 22:43:50 +0800991static void __update_discard_tree_range(struct f2fs_sb_info *sbi,
992 struct block_device *bdev, block_t lstart,
993 block_t start, block_t len);
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800994/* this function is copied from blkdev_issue_discard from block/blk-lib.c */
Chao Yu6b9cb122018-08-08 10:14:55 +0800995static int __submit_discard_cmd(struct f2fs_sb_info *sbi,
Chao Yu78997b52017-10-04 09:08:34 +0800996 struct discard_policy *dpolicy,
Chao Yu35ec7d52018-08-06 22:43:50 +0800997 struct discard_cmd *dc,
998 unsigned int *issued)
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800999{
Chao Yu35ec7d52018-08-06 22:43:50 +08001000 struct block_device *bdev = dc->bdev;
1001 struct request_queue *q = bdev_get_queue(bdev);
1002 unsigned int max_discard_blocks =
1003 SECTOR_TO_BLOCK(q->limits.max_discard_sectors);
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001004 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Chao Yu78997b52017-10-04 09:08:34 +08001005 struct list_head *wait_list = (dpolicy->type == DPOLICY_FSTRIM) ?
1006 &(dcc->fstrim_list) : &(dcc->wait_list);
Chao Yu78997b52017-10-04 09:08:34 +08001007 int flag = dpolicy->sync ? REQ_SYNC : 0;
Chao Yu35ec7d52018-08-06 22:43:50 +08001008 block_t lstart, start, len, total_len;
1009 int err = 0;
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001010
1011 if (dc->state != D_PREP)
Chao Yu6b9cb122018-08-08 10:14:55 +08001012 return 0;
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001013
Yunlei Hed6184772018-04-13 11:08:05 +08001014 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK))
Chao Yu6b9cb122018-08-08 10:14:55 +08001015 return 0;
Yunlei Hed6184772018-04-13 11:08:05 +08001016
Chao Yu35ec7d52018-08-06 22:43:50 +08001017 trace_f2fs_issue_discard(bdev, dc->start, dc->len);
Chao Yu0243a5f2017-04-15 14:09:38 +08001018
Chao Yu35ec7d52018-08-06 22:43:50 +08001019 lstart = dc->lstart;
1020 start = dc->start;
1021 len = dc->len;
1022 total_len = len;
1023
1024 dc->len = 0;
1025
1026 while (total_len && *issued < dpolicy->max_requests && !err) {
1027 struct bio *bio = NULL;
1028 unsigned long flags;
1029 bool last = true;
1030
1031 if (len > max_discard_blocks) {
1032 len = max_discard_blocks;
1033 last = false;
1034 }
1035
1036 (*issued)++;
1037 if (*issued == dpolicy->max_requests)
1038 last = true;
1039
1040 dc->len += len;
1041
Chao Yub83dcfe2018-08-06 20:30:18 +08001042 if (time_to_inject(sbi, FAULT_DISCARD)) {
1043 f2fs_show_injection_info(FAULT_DISCARD);
1044 err = -EIO;
1045 goto submit;
1046 }
Chao Yu35ec7d52018-08-06 22:43:50 +08001047 err = __blkdev_issue_discard(bdev,
1048 SECTOR_FROM_BLOCK(start),
1049 SECTOR_FROM_BLOCK(len),
1050 GFP_NOFS, 0, &bio);
Chao Yub83dcfe2018-08-06 20:30:18 +08001051submit:
Chao Yu6b9cb122018-08-08 10:14:55 +08001052 if (err) {
Chao Yu35ec7d52018-08-06 22:43:50 +08001053 spin_lock_irqsave(&dc->lock, flags);
1054 if (dc->state == D_PARTIAL)
1055 dc->state = D_SUBMIT;
1056 spin_unlock_irqrestore(&dc->lock, flags);
1057
Chao Yu6b9cb122018-08-08 10:14:55 +08001058 break;
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001059 }
Chao Yu35ec7d52018-08-06 22:43:50 +08001060
Chao Yu6b9cb122018-08-08 10:14:55 +08001061 f2fs_bug_on(sbi, !bio);
1062
1063 /*
1064 * should keep before submission to avoid D_DONE
1065 * right away
1066 */
1067 spin_lock_irqsave(&dc->lock, flags);
1068 if (last)
1069 dc->state = D_SUBMIT;
1070 else
1071 dc->state = D_PARTIAL;
1072 dc->bio_ref++;
1073 spin_unlock_irqrestore(&dc->lock, flags);
1074
1075 atomic_inc(&dcc->issing_discard);
1076 dc->issuing++;
1077 list_move_tail(&dc->list, wait_list);
1078
1079 /* sanity check on discard range */
1080 __check_sit_bitmap(sbi, start, start + len);
1081
1082 bio->bi_private = dc;
1083 bio->bi_end_io = f2fs_submit_discard_endio;
1084 bio->bi_opf |= flag;
1085 submit_bio(bio);
1086
1087 atomic_inc(&dcc->issued_discard);
1088
1089 f2fs_update_iostat(sbi, FS_DISCARD, 1);
1090
Chao Yu35ec7d52018-08-06 22:43:50 +08001091 lstart += len;
1092 start += len;
1093 total_len -= len;
1094 len = total_len;
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001095 }
Chao Yu35ec7d52018-08-06 22:43:50 +08001096
Chao Yu6b9cb122018-08-08 10:14:55 +08001097 if (!err && len)
Chao Yu35ec7d52018-08-06 22:43:50 +08001098 __update_discard_tree_range(sbi, bdev, lstart, start, len);
Chao Yu6b9cb122018-08-08 10:14:55 +08001099 return err;
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001100}
1101
Chao Yu004b6862017-04-14 23:24:55 +08001102static struct discard_cmd *__insert_discard_tree(struct f2fs_sb_info *sbi,
1103 struct block_device *bdev, block_t lstart,
1104 block_t start, block_t len,
1105 struct rb_node **insert_p,
1106 struct rb_node *insert_parent)
1107{
1108 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Colin Ian Kingdca69512017-10-19 12:58:21 +02001109 struct rb_node **p;
Chao Yu004b6862017-04-14 23:24:55 +08001110 struct rb_node *parent = NULL;
1111 struct discard_cmd *dc = NULL;
1112
1113 if (insert_p && insert_parent) {
1114 parent = insert_parent;
1115 p = insert_p;
1116 goto do_insert;
1117 }
1118
Chao Yu4d57b862018-05-30 00:20:41 +08001119 p = f2fs_lookup_rb_tree_for_insert(sbi, &dcc->root, &parent, lstart);
Chao Yu004b6862017-04-14 23:24:55 +08001120do_insert:
1121 dc = __attach_discard_cmd(sbi, bdev, lstart, start, len, parent, p);
1122 if (!dc)
1123 return NULL;
1124
1125 return dc;
1126}
1127
Chao Yuba48a332017-04-15 14:09:37 +08001128static void __relocate_discard_cmd(struct discard_cmd_control *dcc,
1129 struct discard_cmd *dc)
1130{
1131 list_move_tail(&dc->list, &dcc->pend_list[plist_idx(dc->len)]);
1132}
1133
Chao Yu004b6862017-04-14 23:24:55 +08001134static void __punch_discard_cmd(struct f2fs_sb_info *sbi,
1135 struct discard_cmd *dc, block_t blkaddr)
1136{
Chao Yuba48a332017-04-15 14:09:37 +08001137 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Chao Yu004b6862017-04-14 23:24:55 +08001138 struct discard_info di = dc->di;
1139 bool modified = false;
1140
1141 if (dc->state == D_DONE || dc->len == 1) {
1142 __remove_discard_cmd(sbi, dc);
1143 return;
1144 }
1145
Chao Yud84d1cb2017-04-18 19:27:39 +08001146 dcc->undiscard_blks -= di.len;
1147
Chao Yu004b6862017-04-14 23:24:55 +08001148 if (blkaddr > di.lstart) {
1149 dc->len = blkaddr - dc->lstart;
Chao Yud84d1cb2017-04-18 19:27:39 +08001150 dcc->undiscard_blks += dc->len;
Chao Yuba48a332017-04-15 14:09:37 +08001151 __relocate_discard_cmd(dcc, dc);
Chao Yu004b6862017-04-14 23:24:55 +08001152 modified = true;
1153 }
1154
1155 if (blkaddr < di.lstart + di.len - 1) {
1156 if (modified) {
1157 __insert_discard_tree(sbi, dc->bdev, blkaddr + 1,
1158 di.start + blkaddr + 1 - di.lstart,
1159 di.lstart + di.len - 1 - blkaddr,
1160 NULL, NULL);
1161 } else {
1162 dc->lstart++;
1163 dc->len--;
1164 dc->start++;
Chao Yud84d1cb2017-04-18 19:27:39 +08001165 dcc->undiscard_blks += dc->len;
Chao Yuba48a332017-04-15 14:09:37 +08001166 __relocate_discard_cmd(dcc, dc);
Chao Yu004b6862017-04-14 23:24:55 +08001167 }
1168 }
1169}
1170
1171static void __update_discard_tree_range(struct f2fs_sb_info *sbi,
1172 struct block_device *bdev, block_t lstart,
1173 block_t start, block_t len)
1174{
1175 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1176 struct discard_cmd *prev_dc = NULL, *next_dc = NULL;
1177 struct discard_cmd *dc;
1178 struct discard_info di = {0};
1179 struct rb_node **insert_p = NULL, *insert_parent = NULL;
Chao Yu35ec7d52018-08-06 22:43:50 +08001180 struct request_queue *q = bdev_get_queue(bdev);
1181 unsigned int max_discard_blocks =
1182 SECTOR_TO_BLOCK(q->limits.max_discard_sectors);
Chao Yu004b6862017-04-14 23:24:55 +08001183 block_t end = lstart + len;
1184
Chao Yu4d57b862018-05-30 00:20:41 +08001185 dc = (struct discard_cmd *)f2fs_lookup_rb_tree_ret(&dcc->root,
Chao Yu004b6862017-04-14 23:24:55 +08001186 NULL, lstart,
1187 (struct rb_entry **)&prev_dc,
1188 (struct rb_entry **)&next_dc,
1189 &insert_p, &insert_parent, true);
1190 if (dc)
1191 prev_dc = dc;
1192
1193 if (!prev_dc) {
1194 di.lstart = lstart;
1195 di.len = next_dc ? next_dc->lstart - lstart : len;
1196 di.len = min(di.len, len);
1197 di.start = start;
1198 }
1199
1200 while (1) {
1201 struct rb_node *node;
1202 bool merged = false;
1203 struct discard_cmd *tdc = NULL;
1204
1205 if (prev_dc) {
1206 di.lstart = prev_dc->lstart + prev_dc->len;
1207 if (di.lstart < lstart)
1208 di.lstart = lstart;
1209 if (di.lstart >= end)
1210 break;
1211
1212 if (!next_dc || next_dc->lstart > end)
1213 di.len = end - di.lstart;
1214 else
1215 di.len = next_dc->lstart - di.lstart;
1216 di.start = start + di.lstart - lstart;
1217 }
1218
1219 if (!di.len)
1220 goto next;
1221
1222 if (prev_dc && prev_dc->state == D_PREP &&
1223 prev_dc->bdev == bdev &&
Chao Yu35ec7d52018-08-06 22:43:50 +08001224 __is_discard_back_mergeable(&di, &prev_dc->di,
1225 max_discard_blocks)) {
Chao Yu004b6862017-04-14 23:24:55 +08001226 prev_dc->di.len += di.len;
Chao Yud84d1cb2017-04-18 19:27:39 +08001227 dcc->undiscard_blks += di.len;
Chao Yuba48a332017-04-15 14:09:37 +08001228 __relocate_discard_cmd(dcc, prev_dc);
Chao Yu004b6862017-04-14 23:24:55 +08001229 di = prev_dc->di;
1230 tdc = prev_dc;
1231 merged = true;
1232 }
1233
1234 if (next_dc && next_dc->state == D_PREP &&
1235 next_dc->bdev == bdev &&
Chao Yu35ec7d52018-08-06 22:43:50 +08001236 __is_discard_front_mergeable(&di, &next_dc->di,
1237 max_discard_blocks)) {
Chao Yu004b6862017-04-14 23:24:55 +08001238 next_dc->di.lstart = di.lstart;
1239 next_dc->di.len += di.len;
1240 next_dc->di.start = di.start;
Chao Yud84d1cb2017-04-18 19:27:39 +08001241 dcc->undiscard_blks += di.len;
Chao Yuba48a332017-04-15 14:09:37 +08001242 __relocate_discard_cmd(dcc, next_dc);
Chao Yu004b6862017-04-14 23:24:55 +08001243 if (tdc)
1244 __remove_discard_cmd(sbi, tdc);
Chao Yu004b6862017-04-14 23:24:55 +08001245 merged = true;
1246 }
1247
Chao Yudf0f6b42017-04-17 18:21:43 +08001248 if (!merged) {
Chao Yu004b6862017-04-14 23:24:55 +08001249 __insert_discard_tree(sbi, bdev, di.lstart, di.start,
1250 di.len, NULL, NULL);
Chao Yudf0f6b42017-04-17 18:21:43 +08001251 }
Chao Yu004b6862017-04-14 23:24:55 +08001252 next:
1253 prev_dc = next_dc;
1254 if (!prev_dc)
1255 break;
1256
1257 node = rb_next(&prev_dc->rb_node);
1258 next_dc = rb_entry_safe(node, struct discard_cmd, rb_node);
1259 }
Chao Yu004b6862017-04-14 23:24:55 +08001260}
1261
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001262static int __queue_discard_cmd(struct f2fs_sb_info *sbi,
1263 struct block_device *bdev, block_t blkstart, block_t blklen)
1264{
1265 block_t lblkstart = blkstart;
1266
Chao Yu0243a5f2017-04-15 14:09:38 +08001267 trace_f2fs_queue_discard(bdev, blkstart, blklen);
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001268
1269 if (sbi->s_ndevs) {
1270 int devi = f2fs_target_device_index(sbi, blkstart);
1271
1272 blkstart -= FDEV(devi).start_blk;
1273 }
Chao Yu35ec7d52018-08-06 22:43:50 +08001274 mutex_lock(&SM_I(sbi)->dcc_info->cmd_lock);
Chao Yu004b6862017-04-14 23:24:55 +08001275 __update_discard_tree_range(sbi, bdev, lblkstart, blkstart, blklen);
Chao Yu35ec7d52018-08-06 22:43:50 +08001276 mutex_unlock(&SM_I(sbi)->dcc_info->cmd_lock);
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001277 return 0;
1278}
1279
Chao Yu20ee4382018-07-08 22:11:01 +08001280static unsigned int __issue_discard_cmd_orderly(struct f2fs_sb_info *sbi,
1281 struct discard_policy *dpolicy)
1282{
1283 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1284 struct discard_cmd *prev_dc = NULL, *next_dc = NULL;
1285 struct rb_node **insert_p = NULL, *insert_parent = NULL;
1286 struct discard_cmd *dc;
1287 struct blk_plug plug;
1288 unsigned int pos = dcc->next_pos;
1289 unsigned int issued = 0;
1290 bool io_interrupted = false;
1291
1292 mutex_lock(&dcc->cmd_lock);
1293 dc = (struct discard_cmd *)f2fs_lookup_rb_tree_ret(&dcc->root,
1294 NULL, pos,
1295 (struct rb_entry **)&prev_dc,
1296 (struct rb_entry **)&next_dc,
1297 &insert_p, &insert_parent, true);
1298 if (!dc)
1299 dc = next_dc;
1300
1301 blk_start_plug(&plug);
1302
1303 while (dc) {
1304 struct rb_node *node;
Chao Yu6b9cb122018-08-08 10:14:55 +08001305 int err = 0;
Chao Yu20ee4382018-07-08 22:11:01 +08001306
1307 if (dc->state != D_PREP)
1308 goto next;
1309
1310 if (dpolicy->io_aware && !is_idle(sbi)) {
1311 io_interrupted = true;
1312 break;
1313 }
1314
1315 dcc->next_pos = dc->lstart + dc->len;
Chao Yu6b9cb122018-08-08 10:14:55 +08001316 err = __submit_discard_cmd(sbi, dpolicy, dc, &issued);
Chao Yu20ee4382018-07-08 22:11:01 +08001317
Chao Yu35ec7d52018-08-06 22:43:50 +08001318 if (issued >= dpolicy->max_requests)
Chao Yu20ee4382018-07-08 22:11:01 +08001319 break;
1320next:
1321 node = rb_next(&dc->rb_node);
Chao Yu6b9cb122018-08-08 10:14:55 +08001322 if (err)
1323 __remove_discard_cmd(sbi, dc);
Chao Yu20ee4382018-07-08 22:11:01 +08001324 dc = rb_entry_safe(node, struct discard_cmd, rb_node);
1325 }
1326
1327 blk_finish_plug(&plug);
1328
1329 if (!dc)
1330 dcc->next_pos = 0;
1331
1332 mutex_unlock(&dcc->cmd_lock);
1333
1334 if (!issued && io_interrupted)
1335 issued = -1;
1336
1337 return issued;
1338}
1339
Chao Yu78997b52017-10-04 09:08:34 +08001340static int __issue_discard_cmd(struct f2fs_sb_info *sbi,
1341 struct discard_policy *dpolicy)
Chao Yubd5b0732017-04-25 20:21:37 +08001342{
1343 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1344 struct list_head *pend_list;
1345 struct discard_cmd *dc, *tmp;
1346 struct blk_plug plug;
Chao Yu522d1712018-07-08 22:08:09 +08001347 int i, issued = 0;
Chao Yue6c6de12017-09-12 21:35:12 +08001348 bool io_interrupted = false;
Chao Yubd5b0732017-04-25 20:21:37 +08001349
Chao Yu78997b52017-10-04 09:08:34 +08001350 for (i = MAX_PLIST_NUM - 1; i >= 0; i--) {
1351 if (i + 1 < dpolicy->granularity)
1352 break;
Chao Yu20ee4382018-07-08 22:11:01 +08001353
1354 if (i < DEFAULT_DISCARD_GRANULARITY && dpolicy->ordered)
1355 return __issue_discard_cmd_orderly(sbi, dpolicy);
1356
Chao Yubd5b0732017-04-25 20:21:37 +08001357 pend_list = &dcc->pend_list[i];
Chao Yu33da62c2017-10-04 09:08:35 +08001358
1359 mutex_lock(&dcc->cmd_lock);
Chao Yu49c60c62018-01-08 18:48:33 +08001360 if (list_empty(pend_list))
1361 goto next;
Chao Yu67fce702018-06-22 16:06:59 +08001362 if (unlikely(dcc->rbtree_check))
1363 f2fs_bug_on(sbi, !f2fs_check_rb_tree_consistence(sbi,
1364 &dcc->root));
Chao Yu33da62c2017-10-04 09:08:35 +08001365 blk_start_plug(&plug);
Chao Yubd5b0732017-04-25 20:21:37 +08001366 list_for_each_entry_safe(dc, tmp, pend_list, list) {
1367 f2fs_bug_on(sbi, dc->state != D_PREP);
1368
Chao Yuecc9aa02017-10-04 09:08:33 +08001369 if (dpolicy->io_aware && i < dpolicy->io_aware_gran &&
1370 !is_idle(sbi)) {
Chao Yue6c6de12017-09-12 21:35:12 +08001371 io_interrupted = true;
Chao Yu522d1712018-07-08 22:08:09 +08001372 break;
Chao Yue6c6de12017-09-12 21:35:12 +08001373 }
1374
Chao Yu35ec7d52018-08-06 22:43:50 +08001375 __submit_discard_cmd(sbi, dpolicy, dc, &issued);
Chao Yu522d1712018-07-08 22:08:09 +08001376
Chao Yu35ec7d52018-08-06 22:43:50 +08001377 if (issued >= dpolicy->max_requests)
Chao Yu33da62c2017-10-04 09:08:35 +08001378 break;
Chao Yubd5b0732017-04-25 20:21:37 +08001379 }
Chao Yu33da62c2017-10-04 09:08:35 +08001380 blk_finish_plug(&plug);
Chao Yu49c60c62018-01-08 18:48:33 +08001381next:
Chao Yu33da62c2017-10-04 09:08:35 +08001382 mutex_unlock(&dcc->cmd_lock);
1383
Chao Yu522d1712018-07-08 22:08:09 +08001384 if (issued >= dpolicy->max_requests || io_interrupted)
Chao Yu33da62c2017-10-04 09:08:35 +08001385 break;
Chao Yubd5b0732017-04-25 20:21:37 +08001386 }
Chao Yu969d1b12017-08-07 23:09:56 +08001387
Chao Yue6c6de12017-09-12 21:35:12 +08001388 if (!issued && io_interrupted)
1389 issued = -1;
1390
Chao Yu969d1b12017-08-07 23:09:56 +08001391 return issued;
1392}
1393
Chao Yucf5c7592017-10-04 09:08:37 +08001394static bool __drop_discard_cmd(struct f2fs_sb_info *sbi)
Chao Yu969d1b12017-08-07 23:09:56 +08001395{
1396 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1397 struct list_head *pend_list;
1398 struct discard_cmd *dc, *tmp;
1399 int i;
Chao Yucf5c7592017-10-04 09:08:37 +08001400 bool dropped = false;
Chao Yu969d1b12017-08-07 23:09:56 +08001401
1402 mutex_lock(&dcc->cmd_lock);
1403 for (i = MAX_PLIST_NUM - 1; i >= 0; i--) {
1404 pend_list = &dcc->pend_list[i];
1405 list_for_each_entry_safe(dc, tmp, pend_list, list) {
1406 f2fs_bug_on(sbi, dc->state != D_PREP);
1407 __remove_discard_cmd(sbi, dc);
Chao Yucf5c7592017-10-04 09:08:37 +08001408 dropped = true;
Chao Yu969d1b12017-08-07 23:09:56 +08001409 }
1410 }
1411 mutex_unlock(&dcc->cmd_lock);
Chao Yucf5c7592017-10-04 09:08:37 +08001412
1413 return dropped;
Chao Yubd5b0732017-04-25 20:21:37 +08001414}
1415
Chao Yu4d57b862018-05-30 00:20:41 +08001416void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi)
Chao Yu7950e9a2018-01-18 17:23:29 +08001417{
1418 __drop_discard_cmd(sbi);
1419}
1420
Chao Yu0ea80512017-10-28 16:52:32 +08001421static unsigned int __wait_one_discard_bio(struct f2fs_sb_info *sbi,
Chao Yu2a510c002017-06-05 18:29:06 +08001422 struct discard_cmd *dc)
1423{
1424 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Chao Yu0ea80512017-10-28 16:52:32 +08001425 unsigned int len = 0;
Chao Yu2a510c002017-06-05 18:29:06 +08001426
1427 wait_for_completion_io(&dc->wait);
1428 mutex_lock(&dcc->cmd_lock);
1429 f2fs_bug_on(sbi, dc->state != D_DONE);
1430 dc->ref--;
Chao Yu0ea80512017-10-28 16:52:32 +08001431 if (!dc->ref) {
1432 if (!dc->error)
1433 len = dc->len;
Chao Yu2a510c002017-06-05 18:29:06 +08001434 __remove_discard_cmd(sbi, dc);
Chao Yu0ea80512017-10-28 16:52:32 +08001435 }
Chao Yu2a510c002017-06-05 18:29:06 +08001436 mutex_unlock(&dcc->cmd_lock);
Chao Yu0ea80512017-10-28 16:52:32 +08001437
1438 return len;
Chao Yu2a510c002017-06-05 18:29:06 +08001439}
1440
Chao Yu0ea80512017-10-28 16:52:32 +08001441static unsigned int __wait_discard_cmd_range(struct f2fs_sb_info *sbi,
Chao Yu78997b52017-10-04 09:08:34 +08001442 struct discard_policy *dpolicy,
1443 block_t start, block_t end)
Chao Yu63a94fa2017-04-25 20:21:38 +08001444{
1445 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Chao Yu78997b52017-10-04 09:08:34 +08001446 struct list_head *wait_list = (dpolicy->type == DPOLICY_FSTRIM) ?
1447 &(dcc->fstrim_list) : &(dcc->wait_list);
Chao Yu63a94fa2017-04-25 20:21:38 +08001448 struct discard_cmd *dc, *tmp;
Chao Yu6afae632017-05-19 23:46:45 +08001449 bool need_wait;
Chao Yu0ea80512017-10-28 16:52:32 +08001450 unsigned int trimmed = 0;
Chao Yu6afae632017-05-19 23:46:45 +08001451
1452next:
1453 need_wait = false;
Chao Yu63a94fa2017-04-25 20:21:38 +08001454
1455 mutex_lock(&dcc->cmd_lock);
1456 list_for_each_entry_safe(dc, tmp, wait_list, list) {
Chao Yu84126632017-10-04 09:08:32 +08001457 if (dc->lstart + dc->len <= start || end <= dc->lstart)
1458 continue;
Chao Yu78997b52017-10-04 09:08:34 +08001459 if (dc->len < dpolicy->granularity)
Chao Yu84126632017-10-04 09:08:32 +08001460 continue;
Chao Yu78997b52017-10-04 09:08:34 +08001461 if (dc->state == D_DONE && !dc->ref) {
Chao Yu63a94fa2017-04-25 20:21:38 +08001462 wait_for_completion_io(&dc->wait);
Chao Yu0ea80512017-10-28 16:52:32 +08001463 if (!dc->error)
1464 trimmed += dc->len;
Chao Yu63a94fa2017-04-25 20:21:38 +08001465 __remove_discard_cmd(sbi, dc);
Chao Yu6afae632017-05-19 23:46:45 +08001466 } else {
1467 dc->ref++;
1468 need_wait = true;
1469 break;
Chao Yu63a94fa2017-04-25 20:21:38 +08001470 }
1471 }
1472 mutex_unlock(&dcc->cmd_lock);
Chao Yu6afae632017-05-19 23:46:45 +08001473
1474 if (need_wait) {
Chao Yu0ea80512017-10-28 16:52:32 +08001475 trimmed += __wait_one_discard_bio(sbi, dc);
Chao Yu6afae632017-05-19 23:46:45 +08001476 goto next;
1477 }
Chao Yu0ea80512017-10-28 16:52:32 +08001478
1479 return trimmed;
Chao Yu63a94fa2017-04-25 20:21:38 +08001480}
1481
Chao Yu01f9cf62018-06-25 20:33:24 +08001482static unsigned int __wait_all_discard_cmd(struct f2fs_sb_info *sbi,
Chao Yu78997b52017-10-04 09:08:34 +08001483 struct discard_policy *dpolicy)
Chao Yu84126632017-10-04 09:08:32 +08001484{
Jaegeuk Kim9a997182018-05-24 13:57:26 -07001485 struct discard_policy dp;
Chao Yu01f9cf62018-06-25 20:33:24 +08001486 unsigned int discard_blks;
Jaegeuk Kim9a997182018-05-24 13:57:26 -07001487
Chao Yu01f9cf62018-06-25 20:33:24 +08001488 if (dpolicy)
1489 return __wait_discard_cmd_range(sbi, dpolicy, 0, UINT_MAX);
Jaegeuk Kim9a997182018-05-24 13:57:26 -07001490
1491 /* wait all */
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -07001492 __init_discard_policy(sbi, &dp, DPOLICY_FSTRIM, 1);
Chao Yu01f9cf62018-06-25 20:33:24 +08001493 discard_blks = __wait_discard_cmd_range(sbi, &dp, 0, UINT_MAX);
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -07001494 __init_discard_policy(sbi, &dp, DPOLICY_UMOUNT, 1);
Chao Yu01f9cf62018-06-25 20:33:24 +08001495 discard_blks += __wait_discard_cmd_range(sbi, &dp, 0, UINT_MAX);
1496
1497 return discard_blks;
Chao Yu84126632017-10-04 09:08:32 +08001498}
1499
Jaegeuk Kim4e6a8d92016-12-29 14:07:53 -08001500/* This should be covered by global mutex, &sit_i->sentry_lock */
Wei Yongjun94b1e102018-01-05 09:41:20 +00001501static void f2fs_wait_discard_bio(struct f2fs_sb_info *sbi, block_t blkaddr)
Chao Yu275b66b2016-08-29 23:58:34 +08001502{
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001503 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Chao Yu004b6862017-04-14 23:24:55 +08001504 struct discard_cmd *dc;
Chao Yuec9895a2017-04-26 17:39:54 +08001505 bool need_wait = false;
Chao Yu275b66b2016-08-29 23:58:34 +08001506
Jaegeuk Kim15469962017-01-09 20:32:07 -08001507 mutex_lock(&dcc->cmd_lock);
Chao Yu4d57b862018-05-30 00:20:41 +08001508 dc = (struct discard_cmd *)f2fs_lookup_rb_tree(&dcc->root,
1509 NULL, blkaddr);
Chao Yu004b6862017-04-14 23:24:55 +08001510 if (dc) {
Chao Yuec9895a2017-04-26 17:39:54 +08001511 if (dc->state == D_PREP) {
1512 __punch_discard_cmd(sbi, dc, blkaddr);
1513 } else {
1514 dc->ref++;
1515 need_wait = true;
1516 }
Chao Yu275b66b2016-08-29 23:58:34 +08001517 }
Chao Yud4314132017-04-05 18:19:49 +08001518 mutex_unlock(&dcc->cmd_lock);
Chao Yuec9895a2017-04-26 17:39:54 +08001519
Chao Yu2a510c002017-06-05 18:29:06 +08001520 if (need_wait)
1521 __wait_one_discard_bio(sbi, dc);
Chao Yud4314132017-04-05 18:19:49 +08001522}
Chao Yu22d375d2017-04-05 18:19:48 +08001523
Chao Yu4d57b862018-05-30 00:20:41 +08001524void f2fs_stop_discard_thread(struct f2fs_sb_info *sbi)
Chao Yucce13252017-06-29 23:17:45 +08001525{
1526 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1527
1528 if (dcc && dcc->f2fs_issue_discard) {
1529 struct task_struct *discard_thread = dcc->f2fs_issue_discard;
1530
1531 dcc->f2fs_issue_discard = NULL;
1532 kthread_stop(discard_thread);
Jaegeuk Kim15469962017-01-09 20:32:07 -08001533 }
1534}
1535
Chao Yu84126632017-10-04 09:08:32 +08001536/* This comes from f2fs_put_super */
Chao Yucf5c7592017-10-04 09:08:37 +08001537bool f2fs_wait_discard_bios(struct f2fs_sb_info *sbi)
Chao Yu275b66b2016-08-29 23:58:34 +08001538{
Chao Yu969d1b12017-08-07 23:09:56 +08001539 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Chao Yu78997b52017-10-04 09:08:34 +08001540 struct discard_policy dpolicy;
Chao Yucf5c7592017-10-04 09:08:37 +08001541 bool dropped;
Chao Yu969d1b12017-08-07 23:09:56 +08001542
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -07001543 __init_discard_policy(sbi, &dpolicy, DPOLICY_UMOUNT,
1544 dcc->discard_granularity);
Chao Yu78997b52017-10-04 09:08:34 +08001545 __issue_discard_cmd(sbi, &dpolicy);
Chao Yucf5c7592017-10-04 09:08:37 +08001546 dropped = __drop_discard_cmd(sbi);
Chao Yucf5c7592017-10-04 09:08:37 +08001547
Jaegeuk Kim9a997182018-05-24 13:57:26 -07001548 /* just to make sure there is no pending discard commands */
1549 __wait_all_discard_cmd(sbi, NULL);
Chao Yu2482c432018-07-08 22:16:53 +08001550
1551 f2fs_bug_on(sbi, atomic_read(&dcc->discard_cmd_cnt));
Chao Yucf5c7592017-10-04 09:08:37 +08001552 return dropped;
Chao Yu969d1b12017-08-07 23:09:56 +08001553}
1554
Jaegeuk Kim15469962017-01-09 20:32:07 -08001555static int issue_discard_thread(void *data)
1556{
1557 struct f2fs_sb_info *sbi = data;
1558 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1559 wait_queue_head_t *q = &dcc->discard_wait_queue;
Chao Yu78997b52017-10-04 09:08:34 +08001560 struct discard_policy dpolicy;
Chao Yu969d1b12017-08-07 23:09:56 +08001561 unsigned int wait_ms = DEF_MIN_DISCARD_ISSUE_TIME;
1562 int issued;
Jaegeuk Kim15469962017-01-09 20:32:07 -08001563
Jaegeuk Kim1d7be272017-05-17 10:36:58 -07001564 set_freezable();
Jaegeuk Kim15469962017-01-09 20:32:07 -08001565
Jaegeuk Kim1d7be272017-05-17 10:36:58 -07001566 do {
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -07001567 __init_discard_policy(sbi, &dpolicy, DPOLICY_BG,
Chao Yu78997b52017-10-04 09:08:34 +08001568 dcc->discard_granularity);
1569
Chao Yu969d1b12017-08-07 23:09:56 +08001570 wait_event_interruptible_timeout(*q,
1571 kthread_should_stop() || freezing(current) ||
1572 dcc->discard_wake,
1573 msecs_to_jiffies(wait_ms));
Sheng Yong35a9a762018-05-08 17:51:34 +08001574
1575 if (dcc->discard_wake)
1576 dcc->discard_wake = 0;
1577
Jaegeuk Kim1d7be272017-05-17 10:36:58 -07001578 if (try_to_freeze())
1579 continue;
Chao Yu3b60d802018-01-25 18:57:27 +08001580 if (f2fs_readonly(sbi->sb))
1581 continue;
Jaegeuk Kim1d7be272017-05-17 10:36:58 -07001582 if (kthread_should_stop())
1583 return 0;
Yunlei Hed6184772018-04-13 11:08:05 +08001584 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) {
1585 wait_ms = dpolicy.max_interval;
1586 continue;
1587 }
Jaegeuk Kim15469962017-01-09 20:32:07 -08001588
Jaegeuk Kim5b0e9532018-05-07 14:22:40 -07001589 if (sbi->gc_mode == GC_URGENT)
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -07001590 __init_discard_policy(sbi, &dpolicy, DPOLICY_FORCE, 1);
Jaegeuk Kim1d7be272017-05-17 10:36:58 -07001591
Chao Yudc6febb2017-07-22 08:52:23 +08001592 sb_start_intwrite(sbi->sb);
1593
Chao Yu78997b52017-10-04 09:08:34 +08001594 issued = __issue_discard_cmd(sbi, &dpolicy);
Yunlei Hef9d1dce2018-04-08 15:11:11 +08001595 if (issued > 0) {
Chao Yu78997b52017-10-04 09:08:34 +08001596 __wait_all_discard_cmd(sbi, &dpolicy);
1597 wait_ms = dpolicy.min_interval;
Yunlei Hef9d1dce2018-04-08 15:11:11 +08001598 } else if (issued == -1){
1599 wait_ms = dpolicy.mid_interval;
Chao Yu969d1b12017-08-07 23:09:56 +08001600 } else {
Chao Yu78997b52017-10-04 09:08:34 +08001601 wait_ms = dpolicy.max_interval;
Chao Yu969d1b12017-08-07 23:09:56 +08001602 }
Jaegeuk Kim1d7be272017-05-17 10:36:58 -07001603
Chao Yudc6febb2017-07-22 08:52:23 +08001604 sb_end_intwrite(sbi->sb);
1605
Jaegeuk Kim1d7be272017-05-17 10:36:58 -07001606 } while (!kthread_should_stop());
1607 return 0;
Jaegeuk Kim15469962017-01-09 20:32:07 -08001608}
1609
Damien Le Moalf46e88092016-10-28 17:45:06 +09001610#ifdef CONFIG_BLK_DEV_ZONED
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001611static int __f2fs_issue_discard_zone(struct f2fs_sb_info *sbi,
1612 struct block_device *bdev, block_t blkstart, block_t blklen)
Damien Le Moalf46e88092016-10-28 17:45:06 +09001613{
Jaegeuk Kim925922852017-02-22 20:18:35 -08001614 sector_t sector, nr_sects;
Kinglong Mee10a875f2017-03-08 09:49:53 +08001615 block_t lblkstart = blkstart;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001616 int devi = 0;
Damien Le Moalf46e88092016-10-28 17:45:06 +09001617
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001618 if (sbi->s_ndevs) {
1619 devi = f2fs_target_device_index(sbi, blkstart);
1620 blkstart -= FDEV(devi).start_blk;
1621 }
Damien Le Moalf46e88092016-10-28 17:45:06 +09001622
1623 /*
1624 * We need to know the type of the zone: for conventional zones,
1625 * use regular discard if the drive supports it. For sequential
1626 * zones, reset the zone write pointer.
1627 */
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001628 switch (get_blkz_type(sbi, bdev, blkstart)) {
Damien Le Moalf46e88092016-10-28 17:45:06 +09001629
1630 case BLK_ZONE_TYPE_CONVENTIONAL:
1631 if (!blk_queue_discard(bdev_get_queue(bdev)))
1632 return 0;
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001633 return __queue_discard_cmd(sbi, bdev, lblkstart, blklen);
Damien Le Moalf46e88092016-10-28 17:45:06 +09001634 case BLK_ZONE_TYPE_SEQWRITE_REQ:
1635 case BLK_ZONE_TYPE_SEQWRITE_PREF:
Jaegeuk Kim925922852017-02-22 20:18:35 -08001636 sector = SECTOR_FROM_BLOCK(blkstart);
1637 nr_sects = SECTOR_FROM_BLOCK(blklen);
1638
1639 if (sector & (bdev_zone_sectors(bdev) - 1) ||
1640 nr_sects != bdev_zone_sectors(bdev)) {
1641 f2fs_msg(sbi->sb, KERN_INFO,
1642 "(%d) %s: Unaligned discard attempted (block %x + %x)",
1643 devi, sbi->s_ndevs ? FDEV(devi).path: "",
1644 blkstart, blklen);
1645 return -EIO;
1646 }
Jaegeuk Kimd50aaee2017-02-15 11:14:06 -08001647 trace_f2fs_issue_reset_zone(bdev, blkstart);
Damien Le Moalf46e88092016-10-28 17:45:06 +09001648 return blkdev_reset_zones(bdev, sector,
1649 nr_sects, GFP_NOFS);
1650 default:
1651 /* Unknown zone type: broken device ? */
1652 return -EIO;
1653 }
1654}
1655#endif
1656
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001657static int __issue_discard_async(struct f2fs_sb_info *sbi,
1658 struct block_device *bdev, block_t blkstart, block_t blklen)
1659{
1660#ifdef CONFIG_BLK_DEV_ZONED
Sheng Yongccd31cb2018-02-06 12:31:17 +08001661 if (f2fs_sb_has_blkzoned(sbi->sb) &&
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001662 bdev_zoned_model(bdev) != BLK_ZONED_NONE)
1663 return __f2fs_issue_discard_zone(sbi, bdev, blkstart, blklen);
1664#endif
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001665 return __queue_discard_cmd(sbi, bdev, blkstart, blklen);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001666}
1667
Jaegeuk Kim1e87a782014-04-15 13:57:55 +09001668static int f2fs_issue_discard(struct f2fs_sb_info *sbi,
Jaegeuk Kim37208872013-11-12 16:55:17 +09001669 block_t blkstart, block_t blklen)
1670{
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001671 sector_t start = blkstart, len = 0;
1672 struct block_device *bdev;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001673 struct seg_entry *se;
1674 unsigned int offset;
1675 block_t i;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001676 int err = 0;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001677
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001678 bdev = f2fs_target_device(sbi, blkstart, NULL);
1679
1680 for (i = blkstart; i < blkstart + blklen; i++, len++) {
1681 if (i != start) {
1682 struct block_device *bdev2 =
1683 f2fs_target_device(sbi, i, NULL);
1684
1685 if (bdev2 != bdev) {
1686 err = __issue_discard_async(sbi, bdev,
1687 start, len);
1688 if (err)
1689 return err;
1690 bdev = bdev2;
1691 start = i;
1692 len = 0;
1693 }
1694 }
1695
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001696 se = get_seg_entry(sbi, GET_SEGNO(sbi, i));
1697 offset = GET_BLKOFF_FROM_SEG0(sbi, i);
1698
1699 if (!f2fs_test_and_set_bit(offset, se->discard_map))
1700 sbi->discard_blks--;
1701 }
Damien Le Moalf46e88092016-10-28 17:45:06 +09001702
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001703 if (len)
1704 err = __issue_discard_async(sbi, bdev, start, len);
1705 return err;
Jaegeuk Kim1e87a782014-04-15 13:57:55 +09001706}
1707
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08001708static bool add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc,
1709 bool check_only)
Jaegeuk Kimadf49832014-10-28 22:27:59 -07001710{
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001711 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
1712 int max_blocks = sbi->blocks_per_seg;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001713 struct seg_entry *se = get_seg_entry(sbi, cpc->trim_start);
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001714 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
1715 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001716 unsigned long *discard_map = (unsigned long *)se->discard_map;
Jaegeuk Kim60a3b782015-02-10 16:44:29 -08001717 unsigned long *dmap = SIT_I(sbi)->tmp_map;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001718 unsigned int start = 0, end = -1;
Chao Yuc473f1a2017-04-27 20:40:39 +08001719 bool force = (cpc->reason & CP_DISCARD);
Chao Yua7eeb8232017-03-28 18:18:50 +08001720 struct discard_entry *de = NULL;
Chao Yu46f84c22017-04-15 14:09:36 +08001721 struct list_head *head = &SM_I(sbi)->dcc_info->entry_list;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001722 int i;
1723
Jaegeuk Kim3e025742016-08-02 10:56:40 -07001724 if (se->valid_blocks == max_blocks || !f2fs_discard_en(sbi))
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08001725 return false;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001726
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001727 if (!force) {
1728 if (!test_opt(sbi, DISCARD) || !se->valid_blocks ||
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001729 SM_I(sbi)->dcc_info->nr_discards >=
1730 SM_I(sbi)->dcc_info->max_discards)
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08001731 return false;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001732 }
1733
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001734 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
1735 for (i = 0; i < entries; i++)
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001736 dmap[i] = force ? ~ckpt_map[i] & ~discard_map[i] :
Jaegeuk Kimd7bc2482014-12-12 13:53:41 -08001737 (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001738
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001739 while (force || SM_I(sbi)->dcc_info->nr_discards <=
1740 SM_I(sbi)->dcc_info->max_discards) {
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001741 start = __find_rev_next_bit(dmap, max_blocks, end + 1);
1742 if (start >= max_blocks)
1743 break;
1744
1745 end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
Yunlei Hec7b41e12016-07-07 12:13:33 +08001746 if (force && start && end != max_blocks
1747 && (end - start) < cpc->trim_minlen)
1748 continue;
1749
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08001750 if (check_only)
1751 return true;
1752
Chao Yua7eeb8232017-03-28 18:18:50 +08001753 if (!de) {
1754 de = f2fs_kmem_cache_alloc(discard_entry_slab,
1755 GFP_F2FS_ZERO);
1756 de->start_blkaddr = START_BLOCK(sbi, cpc->trim_start);
1757 list_add_tail(&de->list, head);
1758 }
1759
1760 for (i = start; i < end; i++)
1761 __set_bit_le(i, (void *)de->discard_map);
1762
1763 SM_I(sbi)->dcc_info->nr_discards += end - start;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001764 }
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08001765 return false;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001766}
1767
Chao Yuaf8ff652018-04-25 17:38:29 +08001768static void release_discard_addr(struct discard_entry *entry)
1769{
1770 list_del(&entry->list);
1771 kmem_cache_free(discard_entry_slab, entry);
1772}
1773
Chao Yu4d57b862018-05-30 00:20:41 +08001774void f2fs_release_discard_addrs(struct f2fs_sb_info *sbi)
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001775{
Chao Yu46f84c22017-04-15 14:09:36 +08001776 struct list_head *head = &(SM_I(sbi)->dcc_info->entry_list);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001777 struct discard_entry *entry, *this;
1778
1779 /* drop caches */
Chao Yuaf8ff652018-04-25 17:38:29 +08001780 list_for_each_entry_safe(entry, this, head, list)
1781 release_discard_addr(entry);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001782}
1783
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001784/*
Chao Yu4d57b862018-05-30 00:20:41 +08001785 * Should call f2fs_clear_prefree_segments after checkpoint is done.
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001786 */
1787static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
1788{
1789 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Chao Yub65ee142014-08-04 10:10:07 +08001790 unsigned int segno;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001791
1792 mutex_lock(&dirty_i->seglist_lock);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001793 for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001794 __set_test_and_free(sbi, segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001795 mutex_unlock(&dirty_i->seglist_lock);
1796}
1797
Chao Yu4d57b862018-05-30 00:20:41 +08001798void f2fs_clear_prefree_segments(struct f2fs_sb_info *sbi,
1799 struct cp_control *cpc)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001800{
Chao Yu969d1b12017-08-07 23:09:56 +08001801 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1802 struct list_head *head = &dcc->entry_list;
Chao Yu2d7b8222014-03-29 11:33:17 +08001803 struct discard_entry *entry, *this;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001804 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Changman Lee29e59c12013-11-11 09:24:37 +09001805 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
Changman Lee29e59c12013-11-11 09:24:37 +09001806 unsigned int start = 0, end = -1;
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001807 unsigned int secno, start_segno;
Chao Yuc473f1a2017-04-27 20:40:39 +08001808 bool force = (cpc->reason & CP_DISCARD);
Yunlong Songad6672b2018-07-19 20:58:15 +08001809 bool need_align = test_opt(sbi, LFS) && sbi->segs_per_sec > 1;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001810
1811 mutex_lock(&dirty_i->seglist_lock);
Changman Lee29e59c12013-11-11 09:24:37 +09001812
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001813 while (1) {
Changman Lee29e59c12013-11-11 09:24:37 +09001814 int i;
Yunlong Songad6672b2018-07-19 20:58:15 +08001815
1816 if (need_align && end != -1)
1817 end--;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001818 start = find_next_bit(prefree_map, MAIN_SEGS(sbi), end + 1);
1819 if (start >= MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001820 break;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001821 end = find_next_zero_bit(prefree_map, MAIN_SEGS(sbi),
1822 start + 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001823
Yunlong Songad6672b2018-07-19 20:58:15 +08001824 if (need_align) {
1825 start = rounddown(start, sbi->segs_per_sec);
1826 end = roundup(end, sbi->segs_per_sec);
1827 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001828
Yunlong Songad6672b2018-07-19 20:58:15 +08001829 for (i = start; i < end; i++) {
1830 if (test_and_clear_bit(i, prefree_map))
1831 dirty_i->nr_dirty[PRE]--;
1832 }
Changman Lee29e59c12013-11-11 09:24:37 +09001833
Yunlei He650d3c42016-12-22 11:46:24 +08001834 if (!test_opt(sbi, DISCARD))
Changman Lee29e59c12013-11-11 09:24:37 +09001835 continue;
1836
Yunlei He650d3c42016-12-22 11:46:24 +08001837 if (force && start >= cpc->trim_start &&
1838 (end - 1) <= cpc->trim_end)
1839 continue;
1840
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001841 if (!test_opt(sbi, LFS) || sbi->segs_per_sec == 1) {
1842 f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
Jaegeuk Kim37208872013-11-12 16:55:17 +09001843 (end - start) << sbi->log_blocks_per_seg);
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001844 continue;
1845 }
1846next:
Jaegeuk Kim4ddb1a42017-04-07 15:08:17 -07001847 secno = GET_SEC_FROM_SEG(sbi, start);
1848 start_segno = GET_SEG_FROM_SEC(sbi, secno);
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001849 if (!IS_CURSEC(sbi, secno) &&
Jaegeuk Kim302bd342017-04-07 14:33:22 -07001850 !get_valid_blocks(sbi, start, true))
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001851 f2fs_issue_discard(sbi, START_BLOCK(sbi, start_segno),
1852 sbi->segs_per_sec << sbi->log_blocks_per_seg);
1853
1854 start = start_segno + sbi->segs_per_sec;
1855 if (start < end)
1856 goto next;
Jaegeuk Kim8b107f52017-02-27 11:57:11 -08001857 else
1858 end = start - 1;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001859 }
1860 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001861
1862 /* send small discards */
Chao Yu2d7b8222014-03-29 11:33:17 +08001863 list_for_each_entry_safe(entry, this, head, list) {
Chao Yua7eeb8232017-03-28 18:18:50 +08001864 unsigned int cur_pos = 0, next_pos, len, total_len = 0;
1865 bool is_valid = test_bit_le(0, entry->discard_map);
1866
1867find_next:
1868 if (is_valid) {
1869 next_pos = find_next_zero_bit_le(entry->discard_map,
1870 sbi->blocks_per_seg, cur_pos);
1871 len = next_pos - cur_pos;
1872
Sheng Yongccd31cb2018-02-06 12:31:17 +08001873 if (f2fs_sb_has_blkzoned(sbi->sb) ||
Damien Le Moalacfd28102017-05-26 17:04:40 +09001874 (force && len < cpc->trim_minlen))
Chao Yua7eeb8232017-03-28 18:18:50 +08001875 goto skip;
1876
1877 f2fs_issue_discard(sbi, entry->start_blkaddr + cur_pos,
1878 len);
Chao Yua7eeb8232017-03-28 18:18:50 +08001879 total_len += len;
1880 } else {
1881 next_pos = find_next_bit_le(entry->discard_map,
1882 sbi->blocks_per_seg, cur_pos);
1883 }
Jaegeuk Kim836b5a62015-04-30 22:50:06 -07001884skip:
Chao Yua7eeb8232017-03-28 18:18:50 +08001885 cur_pos = next_pos;
1886 is_valid = !is_valid;
1887
1888 if (cur_pos < sbi->blocks_per_seg)
1889 goto find_next;
1890
Chao Yuaf8ff652018-04-25 17:38:29 +08001891 release_discard_addr(entry);
Chao Yu969d1b12017-08-07 23:09:56 +08001892 dcc->nr_discards -= total_len;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001893 }
Chao Yu34e159d2017-04-25 00:21:34 +08001894
Jaegeuk Kim01983c72017-08-22 21:15:43 -07001895 wake_up_discard_thread(sbi, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001896}
1897
Jaegeuk Kim8ed59742017-01-29 14:27:02 +09001898static int create_discard_cmd_control(struct f2fs_sb_info *sbi)
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001899{
Jaegeuk Kim15469962017-01-09 20:32:07 -08001900 dev_t dev = sbi->sb->s_bdev->bd_dev;
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001901 struct discard_cmd_control *dcc;
Chao Yuba48a332017-04-15 14:09:37 +08001902 int err = 0, i;
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001903
1904 if (SM_I(sbi)->dcc_info) {
1905 dcc = SM_I(sbi)->dcc_info;
1906 goto init_thread;
1907 }
1908
Chao Yuacbf0542017-11-30 19:28:17 +08001909 dcc = f2fs_kzalloc(sbi, sizeof(struct discard_cmd_control), GFP_KERNEL);
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001910 if (!dcc)
1911 return -ENOMEM;
1912
Chao Yu969d1b12017-08-07 23:09:56 +08001913 dcc->discard_granularity = DEFAULT_DISCARD_GRANULARITY;
Chao Yu46f84c22017-04-15 14:09:36 +08001914 INIT_LIST_HEAD(&dcc->entry_list);
Chao Yu78997b52017-10-04 09:08:34 +08001915 for (i = 0; i < MAX_PLIST_NUM; i++)
Chao Yuba48a332017-04-15 14:09:37 +08001916 INIT_LIST_HEAD(&dcc->pend_list[i]);
Chao Yu46f84c22017-04-15 14:09:36 +08001917 INIT_LIST_HEAD(&dcc->wait_list);
Chao Yu84126632017-10-04 09:08:32 +08001918 INIT_LIST_HEAD(&dcc->fstrim_list);
Jaegeuk Kim15469962017-01-09 20:32:07 -08001919 mutex_init(&dcc->cmd_lock);
Chao Yu8b8dd652017-03-25 17:19:58 +08001920 atomic_set(&dcc->issued_discard, 0);
1921 atomic_set(&dcc->issing_discard, 0);
Chao Yu5f323662017-03-25 17:19:59 +08001922 atomic_set(&dcc->discard_cmd_cnt, 0);
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001923 dcc->nr_discards = 0;
Chao Yud618eba2017-04-25 00:21:35 +08001924 dcc->max_discards = MAIN_SEGS(sbi) << sbi->log_blocks_per_seg;
Chao Yud84d1cb2017-04-18 19:27:39 +08001925 dcc->undiscard_blks = 0;
Chao Yu20ee4382018-07-08 22:11:01 +08001926 dcc->next_pos = 0;
Chao Yu004b6862017-04-14 23:24:55 +08001927 dcc->root = RB_ROOT;
Chao Yu67fce702018-06-22 16:06:59 +08001928 dcc->rbtree_check = false;
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001929
Jaegeuk Kim15469962017-01-09 20:32:07 -08001930 init_waitqueue_head(&dcc->discard_wait_queue);
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001931 SM_I(sbi)->dcc_info = dcc;
1932init_thread:
Jaegeuk Kim15469962017-01-09 20:32:07 -08001933 dcc->f2fs_issue_discard = kthread_run(issue_discard_thread, sbi,
1934 "f2fs_discard-%u:%u", MAJOR(dev), MINOR(dev));
1935 if (IS_ERR(dcc->f2fs_issue_discard)) {
1936 err = PTR_ERR(dcc->f2fs_issue_discard);
1937 kfree(dcc);
1938 SM_I(sbi)->dcc_info = NULL;
1939 return err;
1940 }
1941
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001942 return err;
1943}
1944
Chao Yuf0994052017-03-27 18:14:04 +08001945static void destroy_discard_cmd_control(struct f2fs_sb_info *sbi)
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001946{
1947 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1948
Chao Yuf0994052017-03-27 18:14:04 +08001949 if (!dcc)
1950 return;
1951
Chao Yu4d57b862018-05-30 00:20:41 +08001952 f2fs_stop_discard_thread(sbi);
Chao Yuf0994052017-03-27 18:14:04 +08001953
1954 kfree(dcc);
1955 SM_I(sbi)->dcc_info = NULL;
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001956}
1957
Chao Yu184a5cd2014-09-04 18:13:01 +08001958static bool __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001959{
1960 struct sit_info *sit_i = SIT_I(sbi);
Chao Yu184a5cd2014-09-04 18:13:01 +08001961
1962 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001963 sit_i->dirty_sentries++;
Chao Yu184a5cd2014-09-04 18:13:01 +08001964 return false;
1965 }
1966
1967 return true;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001968}
1969
1970static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
1971 unsigned int segno, int modified)
1972{
1973 struct seg_entry *se = get_seg_entry(sbi, segno);
1974 se->type = type;
1975 if (modified)
1976 __mark_sit_entry_dirty(sbi, segno);
1977}
1978
1979static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
1980{
1981 struct seg_entry *se;
1982 unsigned int segno, offset;
1983 long int new_vblocks;
Yunlong Song6415fed2017-08-02 21:20:13 +08001984 bool exist;
1985#ifdef CONFIG_F2FS_CHECK_FS
1986 bool mir_exist;
1987#endif
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001988
1989 segno = GET_SEGNO(sbi, blkaddr);
1990
1991 se = get_seg_entry(sbi, segno);
1992 new_vblocks = se->valid_blocks + del;
Jaegeuk Kim491c0852014-02-04 13:01:10 +09001993 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001994
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001995 f2fs_bug_on(sbi, (new_vblocks >> (sizeof(unsigned short) << 3) ||
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001996 (new_vblocks > sbi->blocks_per_seg)));
1997
1998 se->valid_blocks = new_vblocks;
Chao Yua1f72ac22018-06-04 23:20:17 +08001999 se->mtime = get_mtime(sbi, false);
2000 if (se->mtime > SIT_I(sbi)->max_mtime)
2001 SIT_I(sbi)->max_mtime = se->mtime;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002002
2003 /* Update valid block bitmap */
2004 if (del > 0) {
Yunlong Song6415fed2017-08-02 21:20:13 +08002005 exist = f2fs_test_and_set_bit(offset, se->cur_valid_map);
Chao Yu355e7892017-01-07 18:51:01 +08002006#ifdef CONFIG_F2FS_CHECK_FS
Yunlong Song6415fed2017-08-02 21:20:13 +08002007 mir_exist = f2fs_test_and_set_bit(offset,
2008 se->cur_valid_map_mir);
2009 if (unlikely(exist != mir_exist)) {
2010 f2fs_msg(sbi->sb, KERN_ERR, "Inconsistent error "
2011 "when setting bitmap, blk:%u, old bit:%d",
2012 blkaddr, exist);
Jaegeuk Kim05796762014-09-02 16:05:00 -07002013 f2fs_bug_on(sbi, 1);
Chao Yu355e7892017-01-07 18:51:01 +08002014 }
Yunlong Song6415fed2017-08-02 21:20:13 +08002015#endif
2016 if (unlikely(exist)) {
2017 f2fs_msg(sbi->sb, KERN_ERR,
2018 "Bitmap was wrongly set, blk:%u", blkaddr);
2019 f2fs_bug_on(sbi, 1);
Yunlong Song35ee82c2017-08-02 22:16:54 +08002020 se->valid_blocks--;
2021 del = 0;
Yunlong Song6415fed2017-08-02 21:20:13 +08002022 }
2023
Jaegeuk Kim3e025742016-08-02 10:56:40 -07002024 if (f2fs_discard_en(sbi) &&
2025 !f2fs_test_and_set_bit(offset, se->discard_map))
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07002026 sbi->discard_blks--;
Jaegeuk Kim720037f2017-03-06 11:59:56 -08002027
2028 /* don't overwrite by SSR to keep node chain */
Yunlei He5d7881c2018-03-07 16:22:50 +08002029 if (IS_NODESEG(se->type)) {
Jaegeuk Kim720037f2017-03-06 11:59:56 -08002030 if (!f2fs_test_and_set_bit(offset, se->ckpt_valid_map))
2031 se->ckpt_valid_blocks++;
2032 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002033 } else {
Yunlong Song6415fed2017-08-02 21:20:13 +08002034 exist = f2fs_test_and_clear_bit(offset, se->cur_valid_map);
Chao Yu355e7892017-01-07 18:51:01 +08002035#ifdef CONFIG_F2FS_CHECK_FS
Yunlong Song6415fed2017-08-02 21:20:13 +08002036 mir_exist = f2fs_test_and_clear_bit(offset,
2037 se->cur_valid_map_mir);
2038 if (unlikely(exist != mir_exist)) {
2039 f2fs_msg(sbi->sb, KERN_ERR, "Inconsistent error "
2040 "when clearing bitmap, blk:%u, old bit:%d",
2041 blkaddr, exist);
Jaegeuk Kim05796762014-09-02 16:05:00 -07002042 f2fs_bug_on(sbi, 1);
Chao Yu355e7892017-01-07 18:51:01 +08002043 }
Yunlong Song6415fed2017-08-02 21:20:13 +08002044#endif
2045 if (unlikely(!exist)) {
2046 f2fs_msg(sbi->sb, KERN_ERR,
2047 "Bitmap was wrongly cleared, blk:%u", blkaddr);
2048 f2fs_bug_on(sbi, 1);
Yunlong Song35ee82c2017-08-02 22:16:54 +08002049 se->valid_blocks++;
2050 del = 0;
Yunlong Song6415fed2017-08-02 21:20:13 +08002051 }
2052
Jaegeuk Kim3e025742016-08-02 10:56:40 -07002053 if (f2fs_discard_en(sbi) &&
2054 f2fs_test_and_clear_bit(offset, se->discard_map))
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07002055 sbi->discard_blks++;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002056 }
2057 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
2058 se->ckpt_valid_blocks += del;
2059
2060 __mark_sit_entry_dirty(sbi, segno);
2061
2062 /* update total number of valid blocks to be written in ckpt area */
2063 SIT_I(sbi)->written_valid_blocks += del;
2064
2065 if (sbi->segs_per_sec > 1)
2066 get_sec_entry(sbi, segno)->valid_blocks += del;
2067}
2068
Chao Yu4d57b862018-05-30 00:20:41 +08002069void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002070{
2071 unsigned int segno = GET_SEGNO(sbi, addr);
2072 struct sit_info *sit_i = SIT_I(sbi);
2073
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002074 f2fs_bug_on(sbi, addr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002075 if (addr == NEW_ADDR)
2076 return;
2077
2078 /* add it into sit main buffer */
Chao Yu3d26fa62017-10-30 17:49:53 +08002079 down_write(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002080
2081 update_sit_entry(sbi, addr, -1);
2082
2083 /* add it into dirty seglist */
2084 locate_dirty_segment(sbi, segno);
2085
Chao Yu3d26fa62017-10-30 17:49:53 +08002086 up_write(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002087}
2088
Chao Yu4d57b862018-05-30 00:20:41 +08002089bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr)
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07002090{
2091 struct sit_info *sit_i = SIT_I(sbi);
2092 unsigned int segno, offset;
2093 struct seg_entry *se;
2094 bool is_cp = false;
2095
Chao Yue1da7872018-06-05 17:44:11 +08002096 if (!is_valid_data_blkaddr(sbi, blkaddr))
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07002097 return true;
2098
Chao Yu3d26fa62017-10-30 17:49:53 +08002099 down_read(&sit_i->sentry_lock);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07002100
2101 segno = GET_SEGNO(sbi, blkaddr);
2102 se = get_seg_entry(sbi, segno);
2103 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
2104
2105 if (f2fs_test_bit(offset, se->ckpt_valid_map))
2106 is_cp = true;
2107
Chao Yu3d26fa62017-10-30 17:49:53 +08002108 up_read(&sit_i->sentry_lock);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07002109
2110 return is_cp;
2111}
2112
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002113/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002114 * This function should be resided under the curseg_mutex lock
2115 */
2116static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
Haicheng Lie79efe32013-06-13 16:59:27 +08002117 struct f2fs_summary *sum)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002118{
2119 struct curseg_info *curseg = CURSEG_I(sbi, type);
2120 void *addr = curseg->sum_blk;
Haicheng Lie79efe32013-06-13 16:59:27 +08002121 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002122 memcpy(addr, sum, sizeof(struct f2fs_summary));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002123}
2124
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002125/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002126 * Calculate the number of current summary pages for writing
2127 */
Chao Yu4d57b862018-05-30 00:20:41 +08002128int f2fs_npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002129{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002130 int valid_sum_count = 0;
Fan Li9a479382013-10-29 16:21:47 +08002131 int i, sum_in_page;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002132
2133 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
2134 if (sbi->ckpt->alloc_type[i] == SSR)
2135 valid_sum_count += sbi->blocks_per_seg;
Chao Yu3fa06d72014-12-09 14:21:46 +08002136 else {
2137 if (for_ra)
2138 valid_sum_count += le16_to_cpu(
2139 F2FS_CKPT(sbi)->cur_data_blkoff[i]);
2140 else
2141 valid_sum_count += curseg_blkoff(sbi, i);
2142 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002143 }
2144
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002145 sum_in_page = (PAGE_SIZE - 2 * SUM_JOURNAL_SIZE -
Fan Li9a479382013-10-29 16:21:47 +08002146 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
2147 if (valid_sum_count <= sum_in_page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002148 return 1;
Fan Li9a479382013-10-29 16:21:47 +08002149 else if ((valid_sum_count - sum_in_page) <=
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002150 (PAGE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002151 return 2;
2152 return 3;
2153}
2154
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002155/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002156 * Caller should put this summary page
2157 */
Chao Yu4d57b862018-05-30 00:20:41 +08002158struct page *f2fs_get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002159{
Chao Yu77357302018-07-17 00:02:17 +08002160 return f2fs_get_meta_page_nofail(sbi, GET_SUM_BLOCK(sbi, segno));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002161}
2162
Chao Yu4d57b862018-05-30 00:20:41 +08002163void f2fs_update_meta_page(struct f2fs_sb_info *sbi,
2164 void *src, block_t blk_addr)
Chao Yu381722d2015-05-19 17:40:04 +08002165{
Chao Yu4d57b862018-05-30 00:20:41 +08002166 struct page *page = f2fs_grab_meta_page(sbi, blk_addr);
Chao Yu381722d2015-05-19 17:40:04 +08002167
Chao Yu0537b812017-11-02 20:41:02 +08002168 memcpy(page_address(page), src, PAGE_SIZE);
Chao Yu381722d2015-05-19 17:40:04 +08002169 set_page_dirty(page);
2170 f2fs_put_page(page, 1);
2171}
2172
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002173static void write_sum_page(struct f2fs_sb_info *sbi,
2174 struct f2fs_summary_block *sum_blk, block_t blk_addr)
2175{
Chao Yu4d57b862018-05-30 00:20:41 +08002176 f2fs_update_meta_page(sbi, (void *)sum_blk, blk_addr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002177}
2178
Chao Yub7ad7512016-02-19 18:08:46 +08002179static void write_current_sum_page(struct f2fs_sb_info *sbi,
2180 int type, block_t blk_addr)
2181{
2182 struct curseg_info *curseg = CURSEG_I(sbi, type);
Chao Yu4d57b862018-05-30 00:20:41 +08002183 struct page *page = f2fs_grab_meta_page(sbi, blk_addr);
Chao Yub7ad7512016-02-19 18:08:46 +08002184 struct f2fs_summary_block *src = curseg->sum_blk;
2185 struct f2fs_summary_block *dst;
2186
2187 dst = (struct f2fs_summary_block *)page_address(page);
Chao Yu81114ba2018-04-09 20:25:06 +08002188 memset(dst, 0, PAGE_SIZE);
Chao Yub7ad7512016-02-19 18:08:46 +08002189
2190 mutex_lock(&curseg->curseg_mutex);
2191
2192 down_read(&curseg->journal_rwsem);
2193 memcpy(&dst->journal, curseg->journal, SUM_JOURNAL_SIZE);
2194 up_read(&curseg->journal_rwsem);
2195
2196 memcpy(dst->entries, src->entries, SUM_ENTRY_SIZE);
2197 memcpy(&dst->footer, &src->footer, SUM_FOOTER_SIZE);
2198
2199 mutex_unlock(&curseg->curseg_mutex);
2200
2201 set_page_dirty(page);
2202 f2fs_put_page(page, 1);
2203}
2204
Jaegeuk Kima7881892017-04-20 13:51:57 -07002205static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
2206{
2207 struct curseg_info *curseg = CURSEG_I(sbi, type);
2208 unsigned int segno = curseg->segno + 1;
2209 struct free_segmap_info *free_i = FREE_I(sbi);
2210
2211 if (segno < MAIN_SEGS(sbi) && segno % sbi->segs_per_sec)
2212 return !test_bit(segno, free_i->free_segmap);
2213 return 0;
2214}
2215
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002216/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002217 * Find a new segment from the free segments bitmap to right order
2218 * This function should be returned with success, otherwise BUG
2219 */
2220static void get_new_segment(struct f2fs_sb_info *sbi,
2221 unsigned int *newseg, bool new_sec, int dir)
2222{
2223 struct free_segmap_info *free_i = FREE_I(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002224 unsigned int segno, secno, zoneno;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002225 unsigned int total_zones = MAIN_SECS(sbi) / sbi->secs_per_zone;
Jaegeuk Kim4ddb1a42017-04-07 15:08:17 -07002226 unsigned int hint = GET_SEC_FROM_SEG(sbi, *newseg);
2227 unsigned int old_zoneno = GET_ZONE_FROM_SEG(sbi, *newseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002228 unsigned int left_start = hint;
2229 bool init = true;
2230 int go_left = 0;
2231 int i;
2232
Chao Yu1a118cc2015-02-11 18:20:38 +08002233 spin_lock(&free_i->segmap_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002234
2235 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
2236 segno = find_next_zero_bit(free_i->free_segmap,
Jaegeuk Kim4ddb1a42017-04-07 15:08:17 -07002237 GET_SEG_FROM_SEC(sbi, hint + 1), *newseg + 1);
2238 if (segno < GET_SEG_FROM_SEC(sbi, hint + 1))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002239 goto got_it;
2240 }
2241find_other_zone:
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002242 secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint);
2243 if (secno >= MAIN_SECS(sbi)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002244 if (dir == ALLOC_RIGHT) {
2245 secno = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002246 MAIN_SECS(sbi), 0);
2247 f2fs_bug_on(sbi, secno >= MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002248 } else {
2249 go_left = 1;
2250 left_start = hint - 1;
2251 }
2252 }
2253 if (go_left == 0)
2254 goto skip_left;
2255
2256 while (test_bit(left_start, free_i->free_secmap)) {
2257 if (left_start > 0) {
2258 left_start--;
2259 continue;
2260 }
2261 left_start = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002262 MAIN_SECS(sbi), 0);
2263 f2fs_bug_on(sbi, left_start >= MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002264 break;
2265 }
2266 secno = left_start;
2267skip_left:
Jaegeuk Kim4ddb1a42017-04-07 15:08:17 -07002268 segno = GET_SEG_FROM_SEC(sbi, secno);
2269 zoneno = GET_ZONE_FROM_SEC(sbi, secno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002270
2271 /* give up on finding another zone */
2272 if (!init)
2273 goto got_it;
2274 if (sbi->secs_per_zone == 1)
2275 goto got_it;
2276 if (zoneno == old_zoneno)
2277 goto got_it;
2278 if (dir == ALLOC_LEFT) {
2279 if (!go_left && zoneno + 1 >= total_zones)
2280 goto got_it;
2281 if (go_left && zoneno == 0)
2282 goto got_it;
2283 }
2284 for (i = 0; i < NR_CURSEG_TYPE; i++)
2285 if (CURSEG_I(sbi, i)->zone == zoneno)
2286 break;
2287
2288 if (i < NR_CURSEG_TYPE) {
2289 /* zone is in user, try another */
2290 if (go_left)
2291 hint = zoneno * sbi->secs_per_zone - 1;
2292 else if (zoneno + 1 >= total_zones)
2293 hint = 0;
2294 else
2295 hint = (zoneno + 1) * sbi->secs_per_zone;
2296 init = false;
2297 goto find_other_zone;
2298 }
2299got_it:
2300 /* set it as dirty segment in free segmap */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002301 f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002302 __set_inuse(sbi, segno);
2303 *newseg = segno;
Chao Yu1a118cc2015-02-11 18:20:38 +08002304 spin_unlock(&free_i->segmap_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002305}
2306
2307static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
2308{
2309 struct curseg_info *curseg = CURSEG_I(sbi, type);
2310 struct summary_footer *sum_footer;
2311
2312 curseg->segno = curseg->next_segno;
Jaegeuk Kim4ddb1a42017-04-07 15:08:17 -07002313 curseg->zone = GET_ZONE_FROM_SEG(sbi, curseg->segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002314 curseg->next_blkoff = 0;
2315 curseg->next_segno = NULL_SEGNO;
2316
2317 sum_footer = &(curseg->sum_blk->footer);
2318 memset(sum_footer, 0, sizeof(struct summary_footer));
2319 if (IS_DATASEG(type))
2320 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
2321 if (IS_NODESEG(type))
2322 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
2323 __set_sit_entry_type(sbi, type, curseg->segno, modified);
2324}
2325
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -04002326static unsigned int __get_next_segno(struct f2fs_sb_info *sbi, int type)
2327{
Jaegeuk Kima7881892017-04-20 13:51:57 -07002328 /* if segs_per_sec is large than 1, we need to keep original policy. */
2329 if (sbi->segs_per_sec != 1)
2330 return CURSEG_I(sbi, type)->segno;
2331
Yunlong Songb94929d2018-01-29 11:37:45 +08002332 if (test_opt(sbi, NOHEAP) &&
2333 (type == CURSEG_HOT_DATA || IS_NODESEG(type)))
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -04002334 return 0;
2335
Jaegeuk Kime066b832017-04-13 15:17:00 -07002336 if (SIT_I(sbi)->last_victim[ALLOC_NEXT])
2337 return SIT_I(sbi)->last_victim[ALLOC_NEXT];
Jaegeuk Kim07939622018-02-18 08:50:49 -08002338
2339 /* find segments from 0 to reuse freed segments */
Chao Yu63189b72018-03-08 14:22:56 +08002340 if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_REUSE)
Jaegeuk Kim07939622018-02-18 08:50:49 -08002341 return 0;
2342
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -04002343 return CURSEG_I(sbi, type)->segno;
2344}
2345
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002346/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002347 * Allocate a current working segment.
2348 * This function always allocates a free segment in LFS manner.
2349 */
2350static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
2351{
2352 struct curseg_info *curseg = CURSEG_I(sbi, type);
2353 unsigned int segno = curseg->segno;
2354 int dir = ALLOC_LEFT;
2355
2356 write_sum_page(sbi, curseg->sum_blk,
Haicheng Li81fb5e82013-05-14 18:20:28 +08002357 GET_SUM_BLOCK(sbi, segno));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002358 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
2359 dir = ALLOC_RIGHT;
2360
2361 if (test_opt(sbi, NOHEAP))
2362 dir = ALLOC_RIGHT;
2363
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -04002364 segno = __get_next_segno(sbi, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002365 get_new_segment(sbi, &segno, new_sec, dir);
2366 curseg->next_segno = segno;
2367 reset_curseg(sbi, type, 1);
2368 curseg->alloc_type = LFS;
2369}
2370
2371static void __next_free_blkoff(struct f2fs_sb_info *sbi,
2372 struct curseg_info *seg, block_t start)
2373{
2374 struct seg_entry *se = get_seg_entry(sbi, seg->segno);
Changman Leee81c93c2013-11-15 13:21:16 +09002375 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
Jaegeuk Kim60a3b782015-02-10 16:44:29 -08002376 unsigned long *target_map = SIT_I(sbi)->tmp_map;
Changman Leee81c93c2013-11-15 13:21:16 +09002377 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
2378 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
2379 int i, pos;
2380
2381 for (i = 0; i < entries; i++)
2382 target_map[i] = ckpt_map[i] | cur_map[i];
2383
2384 pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
2385
2386 seg->next_blkoff = pos;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002387}
2388
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002389/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002390 * If a segment is written by LFS manner, next block offset is just obtained
2391 * by increasing the current block offset. However, if a segment is written by
2392 * SSR manner, next block offset obtained by calling __next_free_blkoff
2393 */
2394static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
2395 struct curseg_info *seg)
2396{
2397 if (seg->alloc_type == SSR)
2398 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
2399 else
2400 seg->next_blkoff++;
2401}
2402
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002403/*
arter97e1c42042014-08-06 23:22:50 +09002404 * This function always allocates a used segment(from dirty seglist) by SSR
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002405 * manner, so it should recover the existing segment information of valid blocks
2406 */
Chao Yu025d63a2017-08-30 18:04:48 +08002407static void change_curseg(struct f2fs_sb_info *sbi, int type)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002408{
2409 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2410 struct curseg_info *curseg = CURSEG_I(sbi, type);
2411 unsigned int new_segno = curseg->next_segno;
2412 struct f2fs_summary_block *sum_node;
2413 struct page *sum_page;
2414
2415 write_sum_page(sbi, curseg->sum_blk,
2416 GET_SUM_BLOCK(sbi, curseg->segno));
2417 __set_test_and_inuse(sbi, new_segno);
2418
2419 mutex_lock(&dirty_i->seglist_lock);
2420 __remove_dirty_segment(sbi, new_segno, PRE);
2421 __remove_dirty_segment(sbi, new_segno, DIRTY);
2422 mutex_unlock(&dirty_i->seglist_lock);
2423
2424 reset_curseg(sbi, type, 1);
2425 curseg->alloc_type = SSR;
2426 __next_free_blkoff(sbi, curseg, 0);
2427
Chao Yu4d57b862018-05-30 00:20:41 +08002428 sum_page = f2fs_get_sum_page(sbi, new_segno);
Chao Yu025d63a2017-08-30 18:04:48 +08002429 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
2430 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
2431 f2fs_put_page(sum_page, 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002432}
2433
Jaegeuk Kim43727522013-02-04 15:11:17 +09002434static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
2435{
2436 struct curseg_info *curseg = CURSEG_I(sbi, type);
2437 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
Jaegeuk Kime066b832017-04-13 15:17:00 -07002438 unsigned segno = NULL_SEGNO;
Chao Yud27c3d82017-02-24 18:46:00 +08002439 int i, cnt;
2440 bool reversed = false;
Jaegeuk Kimc192f7a2017-02-22 17:10:18 -08002441
Chao Yu4d57b862018-05-30 00:20:41 +08002442 /* f2fs_need_SSR() already forces to do this */
Jaegeuk Kime066b832017-04-13 15:17:00 -07002443 if (v_ops->get_victim(sbi, &segno, BG_GC, type, SSR)) {
2444 curseg->next_segno = segno;
Jaegeuk Kimc192f7a2017-02-22 17:10:18 -08002445 return 1;
Jaegeuk Kime066b832017-04-13 15:17:00 -07002446 }
Jaegeuk Kim43727522013-02-04 15:11:17 +09002447
Jaegeuk Kim70d625c2017-02-22 17:02:32 -08002448 /* For node segments, let's do SSR more intensively */
2449 if (IS_NODESEG(type)) {
Chao Yud27c3d82017-02-24 18:46:00 +08002450 if (type >= CURSEG_WARM_NODE) {
2451 reversed = true;
2452 i = CURSEG_COLD_NODE;
2453 } else {
2454 i = CURSEG_HOT_NODE;
2455 }
2456 cnt = NR_CURSEG_NODE_TYPE;
Jaegeuk Kim70d625c2017-02-22 17:02:32 -08002457 } else {
Chao Yud27c3d82017-02-24 18:46:00 +08002458 if (type >= CURSEG_WARM_DATA) {
2459 reversed = true;
2460 i = CURSEG_COLD_DATA;
2461 } else {
2462 i = CURSEG_HOT_DATA;
2463 }
2464 cnt = NR_CURSEG_DATA_TYPE;
Jaegeuk Kim70d625c2017-02-22 17:02:32 -08002465 }
Jaegeuk Kim43727522013-02-04 15:11:17 +09002466
Chao Yud27c3d82017-02-24 18:46:00 +08002467 for (; cnt-- > 0; reversed ? i-- : i++) {
Jaegeuk Kimc192f7a2017-02-22 17:10:18 -08002468 if (i == type)
2469 continue;
Jaegeuk Kime066b832017-04-13 15:17:00 -07002470 if (v_ops->get_victim(sbi, &segno, BG_GC, i, SSR)) {
2471 curseg->next_segno = segno;
Jaegeuk Kim43727522013-02-04 15:11:17 +09002472 return 1;
Jaegeuk Kime066b832017-04-13 15:17:00 -07002473 }
Jaegeuk Kimc192f7a2017-02-22 17:10:18 -08002474 }
Jaegeuk Kim43727522013-02-04 15:11:17 +09002475 return 0;
2476}
2477
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002478/*
2479 * flush out current segment and replace it with new segment
2480 * This function should be returned with success, otherwise BUG
2481 */
2482static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
2483 int type, bool force)
2484{
Jaegeuk Kima7881892017-04-20 13:51:57 -07002485 struct curseg_info *curseg = CURSEG_I(sbi, type);
2486
Gu Zheng7b405272013-08-19 09:41:15 +08002487 if (force)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002488 new_curseg(sbi, type, true);
Jaegeuk Kim5b6c6be2017-02-14 19:32:51 -08002489 else if (!is_set_ckpt_flags(sbi, CP_CRC_RECOVERY_FLAG) &&
2490 type == CURSEG_WARM_NODE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002491 new_curseg(sbi, type, false);
Jaegeuk Kima7881892017-04-20 13:51:57 -07002492 else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
2493 new_curseg(sbi, type, false);
Chao Yu4d57b862018-05-30 00:20:41 +08002494 else if (f2fs_need_SSR(sbi) && get_ssr_segment(sbi, type))
Chao Yu025d63a2017-08-30 18:04:48 +08002495 change_curseg(sbi, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002496 else
2497 new_curseg(sbi, type, false);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +09002498
Jaegeuk Kima7881892017-04-20 13:51:57 -07002499 stat_inc_seg_type(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002500}
2501
Chao Yu4d57b862018-05-30 00:20:41 +08002502void f2fs_allocate_new_segments(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002503{
Jaegeuk Kim6ae1be12016-11-11 12:31:40 -08002504 struct curseg_info *curseg;
2505 unsigned int old_segno;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002506 int i;
2507
Chao Yu3d26fa62017-10-30 17:49:53 +08002508 down_write(&SIT_I(sbi)->sentry_lock);
2509
Jaegeuk Kim6ae1be12016-11-11 12:31:40 -08002510 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
2511 curseg = CURSEG_I(sbi, i);
2512 old_segno = curseg->segno;
2513 SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
2514 locate_dirty_segment(sbi, old_segno);
2515 }
Chao Yu3d26fa62017-10-30 17:49:53 +08002516
2517 up_write(&SIT_I(sbi)->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002518}
2519
2520static const struct segment_allocation default_salloc_ops = {
2521 .allocate_segment = allocate_segment_by_default,
2522};
2523
Chao Yu4d57b862018-05-30 00:20:41 +08002524bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi,
2525 struct cp_control *cpc)
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08002526{
2527 __u64 trim_start = cpc->trim_start;
2528 bool has_candidate = false;
2529
Chao Yu3d26fa62017-10-30 17:49:53 +08002530 down_write(&SIT_I(sbi)->sentry_lock);
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08002531 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++) {
2532 if (add_discard_addrs(sbi, cpc, true)) {
2533 has_candidate = true;
2534 break;
2535 }
2536 }
Chao Yu3d26fa62017-10-30 17:49:53 +08002537 up_write(&SIT_I(sbi)->sentry_lock);
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08002538
2539 cpc->trim_start = trim_start;
2540 return has_candidate;
2541}
2542
Chao Yu01f9cf62018-06-25 20:33:24 +08002543static unsigned int __issue_discard_cmd_range(struct f2fs_sb_info *sbi,
Jaegeuk Kim9a997182018-05-24 13:57:26 -07002544 struct discard_policy *dpolicy,
2545 unsigned int start, unsigned int end)
2546{
2547 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
2548 struct discard_cmd *prev_dc = NULL, *next_dc = NULL;
2549 struct rb_node **insert_p = NULL, *insert_parent = NULL;
2550 struct discard_cmd *dc;
2551 struct blk_plug plug;
2552 int issued;
Chao Yu01f9cf62018-06-25 20:33:24 +08002553 unsigned int trimmed = 0;
Jaegeuk Kim9a997182018-05-24 13:57:26 -07002554
2555next:
2556 issued = 0;
2557
2558 mutex_lock(&dcc->cmd_lock);
Chao Yu67fce702018-06-22 16:06:59 +08002559 if (unlikely(dcc->rbtree_check))
2560 f2fs_bug_on(sbi, !f2fs_check_rb_tree_consistence(sbi,
2561 &dcc->root));
Jaegeuk Kim9a997182018-05-24 13:57:26 -07002562
Chao Yu4d57b862018-05-30 00:20:41 +08002563 dc = (struct discard_cmd *)f2fs_lookup_rb_tree_ret(&dcc->root,
Jaegeuk Kim9a997182018-05-24 13:57:26 -07002564 NULL, start,
2565 (struct rb_entry **)&prev_dc,
2566 (struct rb_entry **)&next_dc,
2567 &insert_p, &insert_parent, true);
2568 if (!dc)
2569 dc = next_dc;
2570
2571 blk_start_plug(&plug);
2572
2573 while (dc && dc->lstart <= end) {
2574 struct rb_node *node;
Chao Yu6b9cb122018-08-08 10:14:55 +08002575 int err = 0;
Jaegeuk Kim9a997182018-05-24 13:57:26 -07002576
2577 if (dc->len < dpolicy->granularity)
2578 goto skip;
2579
2580 if (dc->state != D_PREP) {
2581 list_move_tail(&dc->list, &dcc->fstrim_list);
2582 goto skip;
2583 }
2584
Chao Yu6b9cb122018-08-08 10:14:55 +08002585 err = __submit_discard_cmd(sbi, dpolicy, dc, &issued);
Jaegeuk Kim9a997182018-05-24 13:57:26 -07002586
Chao Yu35ec7d52018-08-06 22:43:50 +08002587 if (issued >= dpolicy->max_requests) {
Jaegeuk Kim9a997182018-05-24 13:57:26 -07002588 start = dc->lstart + dc->len;
2589
Chao Yu6b9cb122018-08-08 10:14:55 +08002590 if (err)
2591 __remove_discard_cmd(sbi, dc);
2592
Jaegeuk Kim9a997182018-05-24 13:57:26 -07002593 blk_finish_plug(&plug);
2594 mutex_unlock(&dcc->cmd_lock);
Chao Yu01f9cf62018-06-25 20:33:24 +08002595 trimmed += __wait_all_discard_cmd(sbi, NULL);
Jaegeuk Kim9a997182018-05-24 13:57:26 -07002596 congestion_wait(BLK_RW_ASYNC, HZ/50);
2597 goto next;
2598 }
2599skip:
2600 node = rb_next(&dc->rb_node);
Chao Yu6b9cb122018-08-08 10:14:55 +08002601 if (err)
2602 __remove_discard_cmd(sbi, dc);
Jaegeuk Kim9a997182018-05-24 13:57:26 -07002603 dc = rb_entry_safe(node, struct discard_cmd, rb_node);
2604
2605 if (fatal_signal_pending(current))
2606 break;
2607 }
2608
2609 blk_finish_plug(&plug);
2610 mutex_unlock(&dcc->cmd_lock);
Chao Yu01f9cf62018-06-25 20:33:24 +08002611
2612 return trimmed;
Jaegeuk Kim9a997182018-05-24 13:57:26 -07002613}
2614
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002615int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range)
2616{
Jaegeuk Kimf7ef9b82015-02-09 12:02:44 -08002617 __u64 start = F2FS_BYTES_TO_BLK(range->start);
2618 __u64 end = start + F2FS_BYTES_TO_BLK(range->len) - 1;
Chao Yu377224c2018-04-09 10:25:23 +08002619 unsigned int start_segno, end_segno;
Chao Yu84126632017-10-04 09:08:32 +08002620 block_t start_block, end_block;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002621 struct cp_control cpc;
Chao Yu78997b52017-10-04 09:08:34 +08002622 struct discard_policy dpolicy;
Chao Yu0ea80512017-10-28 16:52:32 +08002623 unsigned long long trimmed = 0;
Chao Yuc34f42e2015-12-23 17:50:30 +08002624 int err = 0;
Yunlong Songad6672b2018-07-19 20:58:15 +08002625 bool need_align = test_opt(sbi, LFS) && sbi->segs_per_sec > 1;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002626
Jaegeuk Kim836b5a62015-04-30 22:50:06 -07002627 if (start >= MAX_BLKADDR(sbi) || range->len < sbi->blocksize)
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002628 return -EINVAL;
2629
Chao Yu3f16ecd92018-08-08 17:36:29 +08002630 if (end < MAIN_BLKADDR(sbi))
2631 goto out;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002632
Yunlei Heed214a12016-09-01 10:14:39 +08002633 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) {
2634 f2fs_msg(sbi->sb, KERN_WARNING,
2635 "Found FS corruption, run fsck to fix.");
Chao Yu3d165dc2018-04-08 20:39:03 +08002636 return -EIO;
Yunlei Heed214a12016-09-01 10:14:39 +08002637 }
2638
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002639 /* start/end segment number in main_area */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002640 start_segno = (start <= MAIN_BLKADDR(sbi)) ? 0 : GET_SEGNO(sbi, start);
2641 end_segno = (end >= MAX_BLKADDR(sbi)) ? MAIN_SEGS(sbi) - 1 :
2642 GET_SEGNO(sbi, end);
Yunlong Songad6672b2018-07-19 20:58:15 +08002643 if (need_align) {
2644 start_segno = rounddown(start_segno, sbi->segs_per_sec);
2645 end_segno = roundup(end_segno + 1, sbi->segs_per_sec) - 1;
2646 }
Chao Yu84126632017-10-04 09:08:32 +08002647
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002648 cpc.reason = CP_DISCARD;
Jaegeuk Kim836b5a62015-04-30 22:50:06 -07002649 cpc.trim_minlen = max_t(__u64, 1, F2FS_BYTES_TO_BLK(range->minlen));
Chao Yu377224c2018-04-09 10:25:23 +08002650 cpc.trim_start = start_segno;
2651 cpc.trim_end = end_segno;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002652
Chao Yu377224c2018-04-09 10:25:23 +08002653 if (sbi->discard_blks == 0)
2654 goto out;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07002655
Chao Yu377224c2018-04-09 10:25:23 +08002656 mutex_lock(&sbi->gc_mutex);
Chao Yu4d57b862018-05-30 00:20:41 +08002657 err = f2fs_write_checkpoint(sbi, &cpc);
Chao Yu377224c2018-04-09 10:25:23 +08002658 mutex_unlock(&sbi->gc_mutex);
2659 if (err)
2660 goto out;
Chao Yu84126632017-10-04 09:08:32 +08002661
Jaegeuk Kime555da92018-05-31 10:20:48 -07002662 /*
2663 * We filed discard candidates, but actually we don't need to wait for
2664 * all of them, since they'll be issued in idle time along with runtime
2665 * discard option. User configuration looks like using runtime discard
2666 * or periodic fstrim instead of it.
2667 */
Jaegeuk Kim5a615492018-06-20 21:27:21 -07002668 if (test_opt(sbi, DISCARD))
2669 goto out;
2670
2671 start_block = START_BLOCK(sbi, start_segno);
2672 end_block = START_BLOCK(sbi, end_segno + 1);
2673
2674 __init_discard_policy(sbi, &dpolicy, DPOLICY_FSTRIM, cpc.trim_minlen);
Chao Yu01f9cf62018-06-25 20:33:24 +08002675 trimmed = __issue_discard_cmd_range(sbi, &dpolicy,
2676 start_block, end_block);
Jaegeuk Kim5a615492018-06-20 21:27:21 -07002677
Chao Yu01f9cf62018-06-25 20:33:24 +08002678 trimmed += __wait_discard_cmd_range(sbi, &dpolicy,
Chao Yu0ea80512017-10-28 16:52:32 +08002679 start_block, end_block);
Chao Yu377224c2018-04-09 10:25:23 +08002680out:
Chao Yu6eae2692018-08-05 23:09:00 +08002681 if (!err)
2682 range->len = F2FS_BLK_TO_BYTES(trimmed);
Chao Yuc34f42e2015-12-23 17:50:30 +08002683 return err;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002684}
2685
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002686static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
2687{
2688 struct curseg_info *curseg = CURSEG_I(sbi, type);
2689 if (curseg->next_blkoff < sbi->blocks_per_seg)
2690 return true;
2691 return false;
2692}
2693
Chao Yu4d57b862018-05-30 00:20:41 +08002694int f2fs_rw_hint_to_seg_type(enum rw_hint hint)
Hyunchul Lee4f0a03d32017-11-09 14:51:27 +09002695{
2696 switch (hint) {
2697 case WRITE_LIFE_SHORT:
2698 return CURSEG_HOT_DATA;
2699 case WRITE_LIFE_EXTREME:
2700 return CURSEG_COLD_DATA;
2701 default:
2702 return CURSEG_WARM_DATA;
2703 }
2704}
2705
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09002706/* This returns write hints for each segment type. This hints will be
2707 * passed down to block layer. There are mapping tables which depend on
2708 * the mount option 'whint_mode'.
2709 *
2710 * 1) whint_mode=off. F2FS only passes down WRITE_LIFE_NOT_SET.
2711 *
2712 * 2) whint_mode=user-based. F2FS tries to pass down hints given by users.
2713 *
2714 * User F2FS Block
2715 * ---- ---- -----
2716 * META WRITE_LIFE_NOT_SET
2717 * HOT_NODE "
2718 * WARM_NODE "
2719 * COLD_NODE "
2720 * ioctl(COLD) COLD_DATA WRITE_LIFE_EXTREME
2721 * extension list " "
2722 *
2723 * -- buffered io
2724 * WRITE_LIFE_EXTREME COLD_DATA WRITE_LIFE_EXTREME
2725 * WRITE_LIFE_SHORT HOT_DATA WRITE_LIFE_SHORT
2726 * WRITE_LIFE_NOT_SET WARM_DATA WRITE_LIFE_NOT_SET
2727 * WRITE_LIFE_NONE " "
2728 * WRITE_LIFE_MEDIUM " "
2729 * WRITE_LIFE_LONG " "
2730 *
2731 * -- direct io
2732 * WRITE_LIFE_EXTREME COLD_DATA WRITE_LIFE_EXTREME
2733 * WRITE_LIFE_SHORT HOT_DATA WRITE_LIFE_SHORT
2734 * WRITE_LIFE_NOT_SET WARM_DATA WRITE_LIFE_NOT_SET
2735 * WRITE_LIFE_NONE " WRITE_LIFE_NONE
2736 * WRITE_LIFE_MEDIUM " WRITE_LIFE_MEDIUM
2737 * WRITE_LIFE_LONG " WRITE_LIFE_LONG
2738 *
Hyunchul Leef2e703f2018-01-31 11:36:58 +09002739 * 3) whint_mode=fs-based. F2FS passes down hints with its policy.
2740 *
2741 * User F2FS Block
2742 * ---- ---- -----
2743 * META WRITE_LIFE_MEDIUM;
2744 * HOT_NODE WRITE_LIFE_NOT_SET
2745 * WARM_NODE "
2746 * COLD_NODE WRITE_LIFE_NONE
2747 * ioctl(COLD) COLD_DATA WRITE_LIFE_EXTREME
2748 * extension list " "
2749 *
2750 * -- buffered io
2751 * WRITE_LIFE_EXTREME COLD_DATA WRITE_LIFE_EXTREME
2752 * WRITE_LIFE_SHORT HOT_DATA WRITE_LIFE_SHORT
2753 * WRITE_LIFE_NOT_SET WARM_DATA WRITE_LIFE_LONG
2754 * WRITE_LIFE_NONE " "
2755 * WRITE_LIFE_MEDIUM " "
2756 * WRITE_LIFE_LONG " "
2757 *
2758 * -- direct io
2759 * WRITE_LIFE_EXTREME COLD_DATA WRITE_LIFE_EXTREME
2760 * WRITE_LIFE_SHORT HOT_DATA WRITE_LIFE_SHORT
2761 * WRITE_LIFE_NOT_SET WARM_DATA WRITE_LIFE_NOT_SET
2762 * WRITE_LIFE_NONE " WRITE_LIFE_NONE
2763 * WRITE_LIFE_MEDIUM " WRITE_LIFE_MEDIUM
2764 * WRITE_LIFE_LONG " WRITE_LIFE_LONG
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09002765 */
2766
Chao Yu4d57b862018-05-30 00:20:41 +08002767enum rw_hint f2fs_io_type_to_rw_hint(struct f2fs_sb_info *sbi,
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09002768 enum page_type type, enum temp_type temp)
2769{
Chao Yu63189b72018-03-08 14:22:56 +08002770 if (F2FS_OPTION(sbi).whint_mode == WHINT_MODE_USER) {
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09002771 if (type == DATA) {
Hyunchul Leef2e703f2018-01-31 11:36:58 +09002772 if (temp == WARM)
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09002773 return WRITE_LIFE_NOT_SET;
Hyunchul Leef2e703f2018-01-31 11:36:58 +09002774 else if (temp == HOT)
2775 return WRITE_LIFE_SHORT;
2776 else if (temp == COLD)
2777 return WRITE_LIFE_EXTREME;
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09002778 } else {
2779 return WRITE_LIFE_NOT_SET;
2780 }
Chao Yu63189b72018-03-08 14:22:56 +08002781 } else if (F2FS_OPTION(sbi).whint_mode == WHINT_MODE_FS) {
Hyunchul Leef2e703f2018-01-31 11:36:58 +09002782 if (type == DATA) {
2783 if (temp == WARM)
2784 return WRITE_LIFE_LONG;
2785 else if (temp == HOT)
2786 return WRITE_LIFE_SHORT;
2787 else if (temp == COLD)
2788 return WRITE_LIFE_EXTREME;
2789 } else if (type == NODE) {
2790 if (temp == WARM || temp == HOT)
2791 return WRITE_LIFE_NOT_SET;
2792 else if (temp == COLD)
2793 return WRITE_LIFE_NONE;
2794 } else if (type == META) {
2795 return WRITE_LIFE_MEDIUM;
2796 }
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09002797 }
Hyunchul Leef2e703f2018-01-31 11:36:58 +09002798 return WRITE_LIFE_NOT_SET;
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09002799}
2800
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002801static int __get_segment_type_2(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002802{
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002803 if (fio->type == DATA)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002804 return CURSEG_HOT_DATA;
2805 else
2806 return CURSEG_HOT_NODE;
2807}
2808
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002809static int __get_segment_type_4(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002810{
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002811 if (fio->type == DATA) {
2812 struct inode *inode = fio->page->mapping->host;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002813
2814 if (S_ISDIR(inode->i_mode))
2815 return CURSEG_HOT_DATA;
2816 else
2817 return CURSEG_COLD_DATA;
2818 } else {
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002819 if (IS_DNODE(fio->page) && is_cold_node(fio->page))
Jaegeuk Kima344b9f2014-11-05 20:05:53 -08002820 return CURSEG_WARM_NODE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002821 else
2822 return CURSEG_COLD_NODE;
2823 }
2824}
2825
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002826static int __get_segment_type_6(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002827{
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002828 if (fio->type == DATA) {
2829 struct inode *inode = fio->page->mapping->host;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002830
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002831 if (is_cold_data(fio->page) || file_is_cold(inode))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002832 return CURSEG_COLD_DATA;
Chao Yub6a06cb2018-02-28 17:07:27 +08002833 if (file_is_hot(inode) ||
Chao Yub4c3ca82018-04-26 17:05:50 +08002834 is_inode_flag_set(inode, FI_HOT_DATA) ||
Chao Yu2079f112018-07-17 20:41:48 +08002835 f2fs_is_atomic_file(inode) ||
2836 f2fs_is_volatile_file(inode))
Jaegeuk Kimef095d12017-03-24 20:05:13 -04002837 return CURSEG_HOT_DATA;
Chao Yu4d57b862018-05-30 00:20:41 +08002838 return f2fs_rw_hint_to_seg_type(inode->i_write_hint);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002839 } else {
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002840 if (IS_DNODE(fio->page))
2841 return is_cold_node(fio->page) ? CURSEG_WARM_NODE :
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002842 CURSEG_HOT_NODE;
Jaegeuk Kimef095d12017-03-24 20:05:13 -04002843 return CURSEG_COLD_NODE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002844 }
2845}
2846
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002847static int __get_segment_type(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002848{
Jaegeuk Kima912b542017-05-10 11:18:25 -07002849 int type = 0;
2850
Chao Yu63189b72018-03-08 14:22:56 +08002851 switch (F2FS_OPTION(fio->sbi).active_logs) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002852 case 2:
Jaegeuk Kima912b542017-05-10 11:18:25 -07002853 type = __get_segment_type_2(fio);
2854 break;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002855 case 4:
Jaegeuk Kima912b542017-05-10 11:18:25 -07002856 type = __get_segment_type_4(fio);
2857 break;
2858 case 6:
2859 type = __get_segment_type_6(fio);
2860 break;
2861 default:
2862 f2fs_bug_on(fio->sbi, true);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002863 }
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002864
Jaegeuk Kima912b542017-05-10 11:18:25 -07002865 if (IS_HOT(type))
2866 fio->temp = HOT;
2867 else if (IS_WARM(type))
2868 fio->temp = WARM;
2869 else
2870 fio->temp = COLD;
2871 return type;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002872}
2873
Chao Yu4d57b862018-05-30 00:20:41 +08002874void f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09002875 block_t old_blkaddr, block_t *new_blkaddr,
Chao Yufb830fc2017-05-19 23:37:01 +08002876 struct f2fs_summary *sum, int type,
2877 struct f2fs_io_info *fio, bool add_list)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002878{
2879 struct sit_info *sit_i = SIT_I(sbi);
Jaegeuk Kim6ae1be12016-11-11 12:31:40 -08002880 struct curseg_info *curseg = CURSEG_I(sbi, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002881
Chao Yu2b603112017-11-02 20:41:03 +08002882 down_read(&SM_I(sbi)->curseg_lock);
2883
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002884 mutex_lock(&curseg->curseg_mutex);
Chao Yu3d26fa62017-10-30 17:49:53 +08002885 down_write(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002886
2887 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002888
Jaegeuk Kim4e6a8d92016-12-29 14:07:53 -08002889 f2fs_wait_discard_bio(sbi, *new_blkaddr);
2890
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002891 /*
2892 * __add_sum_entry should be resided under the curseg_mutex
2893 * because, this function updates a summary entry in the
2894 * current summary block.
2895 */
Haicheng Lie79efe32013-06-13 16:59:27 +08002896 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002897
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002898 __refresh_next_blkoff(sbi, curseg);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +09002899
2900 stat_inc_block_count(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002901
Yunlong Song65f1b802017-10-30 09:33:41 +08002902 /*
2903 * SIT information should be updated before segment allocation,
2904 * since SSR needs latest valid block information.
2905 */
2906 update_sit_entry(sbi, *new_blkaddr, 1);
2907 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
2908 update_sit_entry(sbi, old_blkaddr, -1);
2909
Yunlong Song3436c4b2017-02-21 16:59:26 +08002910 if (!__has_curseg_space(sbi, type))
2911 sit_i->s_ops->allocate_segment(sbi, type, false);
Yunlong Song65f1b802017-10-30 09:33:41 +08002912
Jaegeuk Kimc6f82fe92017-04-04 16:45:30 -07002913 /*
Yunlong Song65f1b802017-10-30 09:33:41 +08002914 * segment dirty status should be updated after segment allocation,
2915 * so we just need to update status only one time after previous
2916 * segment being closed.
Jaegeuk Kimc6f82fe92017-04-04 16:45:30 -07002917 */
Yunlong Song65f1b802017-10-30 09:33:41 +08002918 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
2919 locate_dirty_segment(sbi, GET_SEGNO(sbi, *new_blkaddr));
Yunlong Song3436c4b2017-02-21 16:59:26 +08002920
Chao Yu3d26fa62017-10-30 17:49:53 +08002921 up_write(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002922
Chao Yu704956e2017-07-31 20:19:09 +08002923 if (page && IS_NODESEG(type)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002924 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
2925
Chao Yu704956e2017-07-31 20:19:09 +08002926 f2fs_inode_chksum_set(sbi, page);
2927 }
2928
Chao Yufb830fc2017-05-19 23:37:01 +08002929 if (add_list) {
2930 struct f2fs_bio_info *io;
2931
2932 INIT_LIST_HEAD(&fio->list);
2933 fio->in_list = true;
Chao Yufe16efe2018-05-28 23:47:18 +08002934 fio->retry = false;
Chao Yufb830fc2017-05-19 23:37:01 +08002935 io = sbi->write_io[fio->type] + fio->temp;
2936 spin_lock(&io->io_lock);
2937 list_add_tail(&fio->list, &io->io_list);
2938 spin_unlock(&io->io_lock);
2939 }
2940
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09002941 mutex_unlock(&curseg->curseg_mutex);
Chao Yu2b603112017-11-02 20:41:03 +08002942
2943 up_read(&SM_I(sbi)->curseg_lock);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09002944}
2945
Chao Yu39d787b2017-09-29 13:59:38 +08002946static void update_device_state(struct f2fs_io_info *fio)
2947{
2948 struct f2fs_sb_info *sbi = fio->sbi;
2949 unsigned int devidx;
2950
2951 if (!sbi->s_ndevs)
2952 return;
2953
2954 devidx = f2fs_target_device_index(sbi, fio->new_blkaddr);
2955
2956 /* update device state for fsync */
Chao Yu4d57b862018-05-30 00:20:41 +08002957 f2fs_set_dirty_device(sbi, fio->ino, devidx, FLUSH_INO);
Chao Yu1228b482017-09-29 13:59:39 +08002958
2959 /* update device state for checkpoint */
2960 if (!f2fs_test_bit(devidx, (char *)&sbi->dirty_device)) {
2961 spin_lock(&sbi->dev_lock);
2962 f2fs_set_bit(devidx, (char *)&sbi->dirty_device);
2963 spin_unlock(&sbi->dev_lock);
2964 }
Chao Yu39d787b2017-09-29 13:59:38 +08002965}
2966
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002967static void do_write_page(struct f2fs_summary *sum, struct f2fs_io_info *fio)
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09002968{
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002969 int type = __get_segment_type(fio);
Chao Yu107a8052018-05-26 09:00:13 +08002970 bool keep_order = (test_opt(fio->sbi, LFS) && type == CURSEG_COLD_DATA);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09002971
Chao Yu107a8052018-05-26 09:00:13 +08002972 if (keep_order)
2973 down_read(&fio->sbi->io_order_lock);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08002974reallocate:
Chao Yu4d57b862018-05-30 00:20:41 +08002975 f2fs_allocate_data_block(fio->sbi, fio->page, fio->old_blkaddr,
Chao Yufb830fc2017-05-19 23:37:01 +08002976 &fio->new_blkaddr, sum, type, fio, true);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09002977
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002978 /* writeout dirty page into bdev */
Chao Yufe16efe2018-05-28 23:47:18 +08002979 f2fs_submit_page_write(fio);
2980 if (fio->retry) {
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08002981 fio->old_blkaddr = fio->new_blkaddr;
2982 goto reallocate;
2983 }
Chao Yufe16efe2018-05-28 23:47:18 +08002984
2985 update_device_state(fio);
2986
Chao Yu107a8052018-05-26 09:00:13 +08002987 if (keep_order)
2988 up_read(&fio->sbi->io_order_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002989}
2990
Chao Yu4d57b862018-05-30 00:20:41 +08002991void f2fs_do_write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
Chao Yub0af6d42017-08-02 23:21:48 +08002992 enum iostat_type io_type)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002993{
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002994 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002995 .sbi = sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002996 .type = META,
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09002997 .temp = HOT,
Mike Christie04d328d2016-06-05 14:31:55 -05002998 .op = REQ_OP_WRITE,
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002999 .op_flags = REQ_SYNC | REQ_META | REQ_PRIO,
Chao Yu7a9d7542016-02-22 18:36:38 +08003000 .old_blkaddr = page->index,
3001 .new_blkaddr = page->index,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07003002 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07003003 .encrypted_page = NULL,
Chao Yufb830fc2017-05-19 23:37:01 +08003004 .in_list = false,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09003005 };
3006
Chao Yu2b947002015-10-12 17:04:21 +08003007 if (unlikely(page->index >= MAIN_BLKADDR(sbi)))
Mike Christie04d328d2016-06-05 14:31:55 -05003008 fio.op_flags &= ~REQ_META;
Chao Yu2b947002015-10-12 17:04:21 +08003009
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003010 set_page_writeback(page);
Jaegeuk Kim17c50032018-04-11 23:09:04 -07003011 ClearPageError(page);
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07003012 f2fs_submit_page_write(&fio);
Chao Yub0af6d42017-08-02 23:21:48 +08003013
3014 f2fs_update_iostat(sbi, io_type, F2FS_BLKSIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003015}
3016
Chao Yu4d57b862018-05-30 00:20:41 +08003017void f2fs_do_write_node_page(unsigned int nid, struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003018{
3019 struct f2fs_summary sum;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07003020
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003021 set_summary(&sum, nid, 0, 0);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07003022 do_write_page(&sum, fio);
Chao Yub0af6d42017-08-02 23:21:48 +08003023
3024 f2fs_update_iostat(fio->sbi, fio->io_type, F2FS_BLKSIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003025}
3026
Chao Yu4d57b862018-05-30 00:20:41 +08003027void f2fs_outplace_write_data(struct dnode_of_data *dn,
3028 struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003029{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07003030 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003031 struct f2fs_summary sum;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003032
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07003033 f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR);
Chao Yu77357302018-07-17 00:02:17 +08003034 set_summary(&sum, dn->nid, dn->ofs_in_node, fio->version);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07003035 do_write_page(&sum, fio);
Chao Yuf28b3432016-02-24 17:16:47 +08003036 f2fs_update_data_blkaddr(dn, fio->new_blkaddr);
Chao Yub0af6d42017-08-02 23:21:48 +08003037
3038 f2fs_update_iostat(sbi, fio->io_type, F2FS_BLKSIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003039}
3040
Chao Yu4d57b862018-05-30 00:20:41 +08003041int f2fs_inplace_write_data(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003042{
Chao Yub0af6d42017-08-02 23:21:48 +08003043 int err;
Yunlei Hed21b0f22018-03-26 17:32:23 +08003044 struct f2fs_sb_info *sbi = fio->sbi;
Chao Yub0af6d42017-08-02 23:21:48 +08003045
Chao Yu7a9d7542016-02-22 18:36:38 +08003046 fio->new_blkaddr = fio->old_blkaddr;
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09003047 /* i/o temperature is needed for passing down write hints */
3048 __get_segment_type(fio);
Yunlei Hed21b0f22018-03-26 17:32:23 +08003049
3050 f2fs_bug_on(sbi, !IS_DATASEG(get_seg_entry(sbi,
3051 GET_SEGNO(sbi, fio->new_blkaddr))->type));
3052
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07003053 stat_inc_inplace_blocks(fio->sbi);
Chao Yub0af6d42017-08-02 23:21:48 +08003054
3055 err = f2fs_submit_page_bio(fio);
Chao Yu39d787b2017-09-29 13:59:38 +08003056 if (!err)
3057 update_device_state(fio);
Chao Yub0af6d42017-08-02 23:21:48 +08003058
3059 f2fs_update_iostat(fio->sbi, fio->io_type, F2FS_BLKSIZE);
3060
3061 return err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003062}
3063
Chao Yu2b603112017-11-02 20:41:03 +08003064static inline int __f2fs_get_curseg(struct f2fs_sb_info *sbi,
3065 unsigned int segno)
3066{
3067 int i;
3068
3069 for (i = CURSEG_HOT_DATA; i < NO_CHECK_TYPE; i++) {
3070 if (CURSEG_I(sbi, i)->segno == segno)
3071 break;
3072 }
3073 return i;
3074}
3075
Chao Yu4d57b862018-05-30 00:20:41 +08003076void f2fs_do_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
Chao Yu19f106b2015-05-06 13:08:06 +08003077 block_t old_blkaddr, block_t new_blkaddr,
Chao Yu28bc1062016-02-06 14:40:34 +08003078 bool recover_curseg, bool recover_newaddr)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003079{
3080 struct sit_info *sit_i = SIT_I(sbi);
3081 struct curseg_info *curseg;
3082 unsigned int segno, old_cursegno;
3083 struct seg_entry *se;
3084 int type;
Chao Yu19f106b2015-05-06 13:08:06 +08003085 unsigned short old_blkoff;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003086
3087 segno = GET_SEGNO(sbi, new_blkaddr);
3088 se = get_seg_entry(sbi, segno);
3089 type = se->type;
3090
Chao Yu2b603112017-11-02 20:41:03 +08003091 down_write(&SM_I(sbi)->curseg_lock);
3092
Chao Yu19f106b2015-05-06 13:08:06 +08003093 if (!recover_curseg) {
3094 /* for recovery flow */
3095 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
3096 if (old_blkaddr == NULL_ADDR)
3097 type = CURSEG_COLD_DATA;
3098 else
3099 type = CURSEG_WARM_DATA;
3100 }
3101 } else {
Chao Yu2b603112017-11-02 20:41:03 +08003102 if (IS_CURSEG(sbi, segno)) {
3103 /* se->type is volatile as SSR allocation */
3104 type = __f2fs_get_curseg(sbi, segno);
3105 f2fs_bug_on(sbi, type == NO_CHECK_TYPE);
3106 } else {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003107 type = CURSEG_WARM_DATA;
Chao Yu2b603112017-11-02 20:41:03 +08003108 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003109 }
Chao Yu19f106b2015-05-06 13:08:06 +08003110
Yunlong Song2c190502018-01-04 15:02:02 +08003111 f2fs_bug_on(sbi, !IS_DATASEG(type));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003112 curseg = CURSEG_I(sbi, type);
3113
3114 mutex_lock(&curseg->curseg_mutex);
Chao Yu3d26fa62017-10-30 17:49:53 +08003115 down_write(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003116
3117 old_cursegno = curseg->segno;
Chao Yu19f106b2015-05-06 13:08:06 +08003118 old_blkoff = curseg->next_blkoff;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003119
3120 /* change the current segment */
3121 if (segno != curseg->segno) {
3122 curseg->next_segno = segno;
Chao Yu025d63a2017-08-30 18:04:48 +08003123 change_curseg(sbi, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003124 }
3125
Jaegeuk Kim491c0852014-02-04 13:01:10 +09003126 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
Haicheng Lie79efe32013-06-13 16:59:27 +08003127 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003128
Chao Yu28bc1062016-02-06 14:40:34 +08003129 if (!recover_curseg || recover_newaddr)
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07003130 update_sit_entry(sbi, new_blkaddr, 1);
3131 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
3132 update_sit_entry(sbi, old_blkaddr, -1);
3133
3134 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
3135 locate_dirty_segment(sbi, GET_SEGNO(sbi, new_blkaddr));
3136
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003137 locate_dirty_segment(sbi, old_cursegno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003138
Chao Yu19f106b2015-05-06 13:08:06 +08003139 if (recover_curseg) {
3140 if (old_cursegno != curseg->segno) {
3141 curseg->next_segno = old_cursegno;
Chao Yu025d63a2017-08-30 18:04:48 +08003142 change_curseg(sbi, type);
Chao Yu19f106b2015-05-06 13:08:06 +08003143 }
3144 curseg->next_blkoff = old_blkoff;
3145 }
3146
Chao Yu3d26fa62017-10-30 17:49:53 +08003147 up_write(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003148 mutex_unlock(&curseg->curseg_mutex);
Chao Yu2b603112017-11-02 20:41:03 +08003149 up_write(&SM_I(sbi)->curseg_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003150}
3151
Chao Yu528e3452015-05-28 19:15:35 +08003152void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
3153 block_t old_addr, block_t new_addr,
Chao Yu28bc1062016-02-06 14:40:34 +08003154 unsigned char version, bool recover_curseg,
3155 bool recover_newaddr)
Chao Yu528e3452015-05-28 19:15:35 +08003156{
3157 struct f2fs_summary sum;
3158
3159 set_summary(&sum, dn->nid, dn->ofs_in_node, version);
3160
Chao Yu4d57b862018-05-30 00:20:41 +08003161 f2fs_do_replace_block(sbi, &sum, old_addr, new_addr,
Chao Yu28bc1062016-02-06 14:40:34 +08003162 recover_curseg, recover_newaddr);
Chao Yu528e3452015-05-28 19:15:35 +08003163
Chao Yuf28b3432016-02-24 17:16:47 +08003164 f2fs_update_data_blkaddr(dn, new_addr);
Chao Yu528e3452015-05-28 19:15:35 +08003165}
3166
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09003167void f2fs_wait_on_page_writeback(struct page *page,
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08003168 enum page_type type, bool ordered)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09003169{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09003170 if (PageWriteback(page)) {
Jaegeuk Kim40813632014-09-02 15:31:18 -07003171 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
3172
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07003173 f2fs_submit_merged_write_cond(sbi, page->mapping->host,
3174 0, page->index, type);
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08003175 if (ordered)
3176 wait_on_page_writeback(page);
3177 else
3178 wait_for_stable_page(page);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09003179 }
3180}
3181
Jaegeuk Kimd4c759e2017-09-05 17:04:35 -07003182void f2fs_wait_on_block_writeback(struct f2fs_sb_info *sbi, block_t blkaddr)
Chao Yu08b39fb2015-10-08 13:27:34 +08003183{
3184 struct page *cpage;
3185
Chao Yue1da7872018-06-05 17:44:11 +08003186 if (!is_valid_data_blkaddr(sbi, blkaddr))
Chao Yu08b39fb2015-10-08 13:27:34 +08003187 return;
3188
Chao Yu08b39fb2015-10-08 13:27:34 +08003189 cpage = find_lock_page(META_MAPPING(sbi), blkaddr);
3190 if (cpage) {
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08003191 f2fs_wait_on_page_writeback(cpage, DATA, true);
Chao Yu08b39fb2015-10-08 13:27:34 +08003192 f2fs_put_page(cpage, 1);
3193 }
3194}
3195
Chao Yu77357302018-07-17 00:02:17 +08003196static int read_compacted_summaries(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003197{
3198 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
3199 struct curseg_info *seg_i;
3200 unsigned char *kaddr;
3201 struct page *page;
3202 block_t start;
3203 int i, j, offset;
3204
3205 start = start_sum_block(sbi);
3206
Chao Yu4d57b862018-05-30 00:20:41 +08003207 page = f2fs_get_meta_page(sbi, start++);
Chao Yu77357302018-07-17 00:02:17 +08003208 if (IS_ERR(page))
3209 return PTR_ERR(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003210 kaddr = (unsigned char *)page_address(page);
3211
3212 /* Step 1: restore nat cache */
3213 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08003214 memcpy(seg_i->journal, kaddr, SUM_JOURNAL_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003215
3216 /* Step 2: restore sit cache */
3217 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08003218 memcpy(seg_i->journal, kaddr + SUM_JOURNAL_SIZE, SUM_JOURNAL_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003219 offset = 2 * SUM_JOURNAL_SIZE;
3220
3221 /* Step 3: restore summary entries */
3222 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
3223 unsigned short blk_off;
3224 unsigned int segno;
3225
3226 seg_i = CURSEG_I(sbi, i);
3227 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
3228 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
3229 seg_i->next_segno = segno;
3230 reset_curseg(sbi, i, 0);
3231 seg_i->alloc_type = ckpt->alloc_type[i];
3232 seg_i->next_blkoff = blk_off;
3233
3234 if (seg_i->alloc_type == SSR)
3235 blk_off = sbi->blocks_per_seg;
3236
3237 for (j = 0; j < blk_off; j++) {
3238 struct f2fs_summary *s;
3239 s = (struct f2fs_summary *)(kaddr + offset);
3240 seg_i->sum_blk->entries[j] = *s;
3241 offset += SUMMARY_SIZE;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003242 if (offset + SUMMARY_SIZE <= PAGE_SIZE -
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003243 SUM_FOOTER_SIZE)
3244 continue;
3245
3246 f2fs_put_page(page, 1);
3247 page = NULL;
3248
Chao Yu4d57b862018-05-30 00:20:41 +08003249 page = f2fs_get_meta_page(sbi, start++);
Chao Yu77357302018-07-17 00:02:17 +08003250 if (IS_ERR(page))
3251 return PTR_ERR(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003252 kaddr = (unsigned char *)page_address(page);
3253 offset = 0;
3254 }
3255 }
3256 f2fs_put_page(page, 1);
Chao Yu77357302018-07-17 00:02:17 +08003257 return 0;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003258}
3259
3260static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
3261{
3262 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
3263 struct f2fs_summary_block *sum;
3264 struct curseg_info *curseg;
3265 struct page *new;
3266 unsigned short blk_off;
3267 unsigned int segno = 0;
3268 block_t blk_addr = 0;
Chao Yu77357302018-07-17 00:02:17 +08003269 int err = 0;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003270
3271 /* get segment number and block addr */
3272 if (IS_DATASEG(type)) {
3273 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
3274 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
3275 CURSEG_HOT_DATA]);
Jaegeuk Kim119ee912015-01-29 11:45:33 -08003276 if (__exist_node_summaries(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003277 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
3278 else
3279 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
3280 } else {
3281 segno = le32_to_cpu(ckpt->cur_node_segno[type -
3282 CURSEG_HOT_NODE]);
3283 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
3284 CURSEG_HOT_NODE]);
Jaegeuk Kim119ee912015-01-29 11:45:33 -08003285 if (__exist_node_summaries(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003286 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
3287 type - CURSEG_HOT_NODE);
3288 else
3289 blk_addr = GET_SUM_BLOCK(sbi, segno);
3290 }
3291
Chao Yu4d57b862018-05-30 00:20:41 +08003292 new = f2fs_get_meta_page(sbi, blk_addr);
Chao Yu77357302018-07-17 00:02:17 +08003293 if (IS_ERR(new))
3294 return PTR_ERR(new);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003295 sum = (struct f2fs_summary_block *)page_address(new);
3296
3297 if (IS_NODESEG(type)) {
Jaegeuk Kim119ee912015-01-29 11:45:33 -08003298 if (__exist_node_summaries(sbi)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003299 struct f2fs_summary *ns = &sum->entries[0];
3300 int i;
3301 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
3302 ns->version = 0;
3303 ns->ofs_in_node = 0;
3304 }
3305 } else {
Chao Yu77357302018-07-17 00:02:17 +08003306 err = f2fs_restore_node_summary(sbi, segno, sum);
3307 if (err)
3308 goto out;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003309 }
3310 }
3311
3312 /* set uncompleted segment to curseg */
3313 curseg = CURSEG_I(sbi, type);
3314 mutex_lock(&curseg->curseg_mutex);
Chao Yub7ad7512016-02-19 18:08:46 +08003315
3316 /* update journal info */
3317 down_write(&curseg->journal_rwsem);
3318 memcpy(curseg->journal, &sum->journal, SUM_JOURNAL_SIZE);
3319 up_write(&curseg->journal_rwsem);
3320
3321 memcpy(curseg->sum_blk->entries, sum->entries, SUM_ENTRY_SIZE);
3322 memcpy(&curseg->sum_blk->footer, &sum->footer, SUM_FOOTER_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003323 curseg->next_segno = segno;
3324 reset_curseg(sbi, type, 0);
3325 curseg->alloc_type = ckpt->alloc_type[type];
3326 curseg->next_blkoff = blk_off;
3327 mutex_unlock(&curseg->curseg_mutex);
Chao Yu77357302018-07-17 00:02:17 +08003328out:
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003329 f2fs_put_page(new, 1);
Chao Yu77357302018-07-17 00:02:17 +08003330 return err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003331}
3332
3333static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
3334{
Jin Qian21d3f8e2017-06-01 11:18:30 -07003335 struct f2fs_journal *sit_j = CURSEG_I(sbi, CURSEG_COLD_DATA)->journal;
3336 struct f2fs_journal *nat_j = CURSEG_I(sbi, CURSEG_HOT_DATA)->journal;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003337 int type = CURSEG_HOT_DATA;
Chao Yue4fc5fb2014-03-17 16:36:24 +08003338 int err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003339
Chao Yuaaec2b12016-09-20 11:04:18 +08003340 if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG)) {
Chao Yu4d57b862018-05-30 00:20:41 +08003341 int npages = f2fs_npages_for_summary_flush(sbi, true);
Chao Yu3fa06d72014-12-09 14:21:46 +08003342
3343 if (npages >= 2)
Chao Yu4d57b862018-05-30 00:20:41 +08003344 f2fs_ra_meta_pages(sbi, start_sum_block(sbi), npages,
Chao Yu26879fb2015-10-12 17:05:59 +08003345 META_CP, true);
Chao Yu3fa06d72014-12-09 14:21:46 +08003346
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003347 /* restore for compacted data summary */
Chao Yu77357302018-07-17 00:02:17 +08003348 err = read_compacted_summaries(sbi);
3349 if (err)
3350 return err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003351 type = CURSEG_HOT_NODE;
3352 }
3353
Jaegeuk Kim119ee912015-01-29 11:45:33 -08003354 if (__exist_node_summaries(sbi))
Chao Yu4d57b862018-05-30 00:20:41 +08003355 f2fs_ra_meta_pages(sbi, sum_blk_addr(sbi, NR_CURSEG_TYPE, type),
Chao Yu26879fb2015-10-12 17:05:59 +08003356 NR_CURSEG_TYPE - type, META_CP, true);
Chao Yu3fa06d72014-12-09 14:21:46 +08003357
Chao Yue4fc5fb2014-03-17 16:36:24 +08003358 for (; type <= CURSEG_COLD_NODE; type++) {
3359 err = read_normal_summaries(sbi, type);
3360 if (err)
3361 return err;
3362 }
3363
Jin Qian21d3f8e2017-06-01 11:18:30 -07003364 /* sanity check for summary blocks */
3365 if (nats_in_cursum(nat_j) > NAT_JOURNAL_ENTRIES ||
3366 sits_in_cursum(sit_j) > SIT_JOURNAL_ENTRIES)
3367 return -EINVAL;
3368
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003369 return 0;
3370}
3371
3372static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
3373{
3374 struct page *page;
3375 unsigned char *kaddr;
3376 struct f2fs_summary *summary;
3377 struct curseg_info *seg_i;
3378 int written_size = 0;
3379 int i, j;
3380
Chao Yu4d57b862018-05-30 00:20:41 +08003381 page = f2fs_grab_meta_page(sbi, blkaddr++);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003382 kaddr = (unsigned char *)page_address(page);
Chao Yu81114ba2018-04-09 20:25:06 +08003383 memset(kaddr, 0, PAGE_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003384
3385 /* Step 1: write nat cache */
3386 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08003387 memcpy(kaddr, seg_i->journal, SUM_JOURNAL_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003388 written_size += SUM_JOURNAL_SIZE;
3389
3390 /* Step 2: write sit cache */
3391 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08003392 memcpy(kaddr + written_size, seg_i->journal, SUM_JOURNAL_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003393 written_size += SUM_JOURNAL_SIZE;
3394
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003395 /* Step 3: write summary entries */
3396 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
3397 unsigned short blkoff;
3398 seg_i = CURSEG_I(sbi, i);
3399 if (sbi->ckpt->alloc_type[i] == SSR)
3400 blkoff = sbi->blocks_per_seg;
3401 else
3402 blkoff = curseg_blkoff(sbi, i);
3403
3404 for (j = 0; j < blkoff; j++) {
3405 if (!page) {
Chao Yu4d57b862018-05-30 00:20:41 +08003406 page = f2fs_grab_meta_page(sbi, blkaddr++);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003407 kaddr = (unsigned char *)page_address(page);
Chao Yu81114ba2018-04-09 20:25:06 +08003408 memset(kaddr, 0, PAGE_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003409 written_size = 0;
3410 }
3411 summary = (struct f2fs_summary *)(kaddr + written_size);
3412 *summary = seg_i->sum_blk->entries[j];
3413 written_size += SUMMARY_SIZE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003414
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003415 if (written_size + SUMMARY_SIZE <= PAGE_SIZE -
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003416 SUM_FOOTER_SIZE)
3417 continue;
3418
Chao Yue8d61a72013-10-24 15:08:28 +08003419 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003420 f2fs_put_page(page, 1);
3421 page = NULL;
3422 }
3423 }
Chao Yue8d61a72013-10-24 15:08:28 +08003424 if (page) {
3425 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003426 f2fs_put_page(page, 1);
Chao Yue8d61a72013-10-24 15:08:28 +08003427 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003428}
3429
3430static void write_normal_summaries(struct f2fs_sb_info *sbi,
3431 block_t blkaddr, int type)
3432{
3433 int i, end;
3434 if (IS_DATASEG(type))
3435 end = type + NR_CURSEG_DATA_TYPE;
3436 else
3437 end = type + NR_CURSEG_NODE_TYPE;
3438
Chao Yub7ad7512016-02-19 18:08:46 +08003439 for (i = type; i < end; i++)
3440 write_current_sum_page(sbi, i, blkaddr + (i - type));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003441}
3442
Chao Yu4d57b862018-05-30 00:20:41 +08003443void f2fs_write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003444{
Chao Yuaaec2b12016-09-20 11:04:18 +08003445 if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003446 write_compacted_summaries(sbi, start_blk);
3447 else
3448 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
3449}
3450
Chao Yu4d57b862018-05-30 00:20:41 +08003451void f2fs_write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003452{
Jaegeuk Kim119ee912015-01-29 11:45:33 -08003453 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003454}
3455
Chao Yu4d57b862018-05-30 00:20:41 +08003456int f2fs_lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003457 unsigned int val, int alloc)
3458{
3459 int i;
3460
3461 if (type == NAT_JOURNAL) {
Chao Yudfc08a12016-02-14 18:50:40 +08003462 for (i = 0; i < nats_in_cursum(journal); i++) {
3463 if (le32_to_cpu(nid_in_journal(journal, i)) == val)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003464 return i;
3465 }
Chao Yudfc08a12016-02-14 18:50:40 +08003466 if (alloc && __has_cursum_space(journal, 1, NAT_JOURNAL))
3467 return update_nats_in_cursum(journal, 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003468 } else if (type == SIT_JOURNAL) {
Chao Yudfc08a12016-02-14 18:50:40 +08003469 for (i = 0; i < sits_in_cursum(journal); i++)
3470 if (le32_to_cpu(segno_in_journal(journal, i)) == val)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003471 return i;
Chao Yudfc08a12016-02-14 18:50:40 +08003472 if (alloc && __has_cursum_space(journal, 1, SIT_JOURNAL))
3473 return update_sits_in_cursum(journal, 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003474 }
3475 return -1;
3476}
3477
3478static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
3479 unsigned int segno)
3480{
Chao Yu77357302018-07-17 00:02:17 +08003481 return f2fs_get_meta_page_nofail(sbi, current_sit_addr(sbi, segno));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003482}
3483
3484static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
3485 unsigned int start)
3486{
3487 struct sit_info *sit_i = SIT_I(sbi);
Yunlei He068c3cd2018-01-25 17:27:11 +08003488 struct page *page;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003489 pgoff_t src_off, dst_off;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003490
3491 src_off = current_sit_addr(sbi, start);
3492 dst_off = next_sit_addr(sbi, src_off);
3493
Chao Yu4d57b862018-05-30 00:20:41 +08003494 page = f2fs_grab_meta_page(sbi, dst_off);
Yunlei He068c3cd2018-01-25 17:27:11 +08003495 seg_info_to_sit_page(sbi, page, start);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003496
Yunlei He068c3cd2018-01-25 17:27:11 +08003497 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003498 set_to_next_sit(sit_i, start);
3499
Yunlei He068c3cd2018-01-25 17:27:11 +08003500 return page;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003501}
3502
Chao Yu184a5cd2014-09-04 18:13:01 +08003503static struct sit_entry_set *grab_sit_entry_set(void)
3504{
3505 struct sit_entry_set *ses =
Jaegeuk Kim80c54502015-08-20 08:51:56 -07003506 f2fs_kmem_cache_alloc(sit_entry_set_slab, GFP_NOFS);
Chao Yu184a5cd2014-09-04 18:13:01 +08003507
3508 ses->entry_cnt = 0;
3509 INIT_LIST_HEAD(&ses->set_list);
3510 return ses;
3511}
3512
3513static void release_sit_entry_set(struct sit_entry_set *ses)
3514{
3515 list_del(&ses->set_list);
3516 kmem_cache_free(sit_entry_set_slab, ses);
3517}
3518
3519static void adjust_sit_entry_set(struct sit_entry_set *ses,
3520 struct list_head *head)
3521{
3522 struct sit_entry_set *next = ses;
3523
3524 if (list_is_last(&ses->set_list, head))
3525 return;
3526
3527 list_for_each_entry_continue(next, head, set_list)
3528 if (ses->entry_cnt <= next->entry_cnt)
3529 break;
3530
3531 list_move_tail(&ses->set_list, &next->set_list);
3532}
3533
3534static void add_sit_entry(unsigned int segno, struct list_head *head)
3535{
3536 struct sit_entry_set *ses;
3537 unsigned int start_segno = START_SEGNO(segno);
3538
3539 list_for_each_entry(ses, head, set_list) {
3540 if (ses->start_segno == start_segno) {
3541 ses->entry_cnt++;
3542 adjust_sit_entry_set(ses, head);
3543 return;
3544 }
3545 }
3546
3547 ses = grab_sit_entry_set();
3548
3549 ses->start_segno = start_segno;
3550 ses->entry_cnt++;
3551 list_add(&ses->set_list, head);
3552}
3553
3554static void add_sits_in_set(struct f2fs_sb_info *sbi)
3555{
3556 struct f2fs_sm_info *sm_info = SM_I(sbi);
3557 struct list_head *set_list = &sm_info->sit_entry_set;
3558 unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap;
Chao Yu184a5cd2014-09-04 18:13:01 +08003559 unsigned int segno;
3560
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003561 for_each_set_bit(segno, bitmap, MAIN_SEGS(sbi))
Chao Yu184a5cd2014-09-04 18:13:01 +08003562 add_sit_entry(segno, set_list);
3563}
3564
3565static void remove_sits_in_journal(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003566{
3567 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08003568 struct f2fs_journal *journal = curseg->journal;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003569 int i;
3570
Chao Yub7ad7512016-02-19 18:08:46 +08003571 down_write(&curseg->journal_rwsem);
Chao Yudfc08a12016-02-14 18:50:40 +08003572 for (i = 0; i < sits_in_cursum(journal); i++) {
Chao Yu184a5cd2014-09-04 18:13:01 +08003573 unsigned int segno;
3574 bool dirtied;
3575
Chao Yudfc08a12016-02-14 18:50:40 +08003576 segno = le32_to_cpu(segno_in_journal(journal, i));
Chao Yu184a5cd2014-09-04 18:13:01 +08003577 dirtied = __mark_sit_entry_dirty(sbi, segno);
3578
3579 if (!dirtied)
3580 add_sit_entry(segno, &SM_I(sbi)->sit_entry_set);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003581 }
Chao Yudfc08a12016-02-14 18:50:40 +08003582 update_sits_in_cursum(journal, -i);
Chao Yub7ad7512016-02-19 18:08:46 +08003583 up_write(&curseg->journal_rwsem);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003584}
3585
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09003586/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003587 * CP calls this function, which flushes SIT entries including sit_journal,
3588 * and moves prefree segs to free segs.
3589 */
Chao Yu4d57b862018-05-30 00:20:41 +08003590void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003591{
3592 struct sit_info *sit_i = SIT_I(sbi);
3593 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
3594 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08003595 struct f2fs_journal *journal = curseg->journal;
Chao Yu184a5cd2014-09-04 18:13:01 +08003596 struct sit_entry_set *ses, *tmp;
3597 struct list_head *head = &SM_I(sbi)->sit_entry_set;
Chao Yu184a5cd2014-09-04 18:13:01 +08003598 bool to_journal = true;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003599 struct seg_entry *se;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003600
Chao Yu3d26fa62017-10-30 17:49:53 +08003601 down_write(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003602
Wanpeng Li2b11a742015-02-27 16:52:50 +08003603 if (!sit_i->dirty_sentries)
3604 goto out;
3605
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003606 /*
Chao Yu184a5cd2014-09-04 18:13:01 +08003607 * add and account sit entries of dirty bitmap in sit entry
3608 * set temporarily
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003609 */
Chao Yu184a5cd2014-09-04 18:13:01 +08003610 add_sits_in_set(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003611
Chao Yu184a5cd2014-09-04 18:13:01 +08003612 /*
3613 * if there are no enough space in journal to store dirty sit
3614 * entries, remove all entries from journal and add and account
3615 * them in sit entry set.
3616 */
Chao Yudfc08a12016-02-14 18:50:40 +08003617 if (!__has_cursum_space(journal, sit_i->dirty_sentries, SIT_JOURNAL))
Chao Yu184a5cd2014-09-04 18:13:01 +08003618 remove_sits_in_journal(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003619
Chao Yu184a5cd2014-09-04 18:13:01 +08003620 /*
3621 * there are two steps to flush sit entries:
3622 * #1, flush sit entries to journal in current cold data summary block.
3623 * #2, flush sit entries to sit page.
3624 */
3625 list_for_each_entry_safe(ses, tmp, head, set_list) {
Jaegeuk Kim4a257ed2014-10-16 11:43:30 -07003626 struct page *page = NULL;
Chao Yu184a5cd2014-09-04 18:13:01 +08003627 struct f2fs_sit_block *raw_sit = NULL;
3628 unsigned int start_segno = ses->start_segno;
3629 unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003630 (unsigned long)MAIN_SEGS(sbi));
Chao Yu184a5cd2014-09-04 18:13:01 +08003631 unsigned int segno = start_segno;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09003632
Chao Yu184a5cd2014-09-04 18:13:01 +08003633 if (to_journal &&
Chao Yudfc08a12016-02-14 18:50:40 +08003634 !__has_cursum_space(journal, ses->entry_cnt, SIT_JOURNAL))
Chao Yu184a5cd2014-09-04 18:13:01 +08003635 to_journal = false;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003636
Chao Yub7ad7512016-02-19 18:08:46 +08003637 if (to_journal) {
3638 down_write(&curseg->journal_rwsem);
3639 } else {
Chao Yu184a5cd2014-09-04 18:13:01 +08003640 page = get_next_sit_page(sbi, start_segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003641 raw_sit = page_address(page);
3642 }
3643
Chao Yu184a5cd2014-09-04 18:13:01 +08003644 /* flush dirty sit entries in region of current sit set */
3645 for_each_set_bit_from(segno, bitmap, end) {
3646 int offset, sit_offset;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003647
3648 se = get_seg_entry(sbi, segno);
Zhikang Zhang56b07e72018-04-09 04:28:41 +08003649#ifdef CONFIG_F2FS_CHECK_FS
3650 if (memcmp(se->cur_valid_map, se->cur_valid_map_mir,
3651 SIT_VBLOCK_MAP_SIZE))
3652 f2fs_bug_on(sbi, 1);
3653#endif
Chao Yu184a5cd2014-09-04 18:13:01 +08003654
3655 /* add discard candidates */
Chao Yuc473f1a2017-04-27 20:40:39 +08003656 if (!(cpc->reason & CP_DISCARD)) {
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003657 cpc->trim_start = segno;
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08003658 add_discard_addrs(sbi, cpc, false);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003659 }
Chao Yu184a5cd2014-09-04 18:13:01 +08003660
3661 if (to_journal) {
Chao Yu4d57b862018-05-30 00:20:41 +08003662 offset = f2fs_lookup_journal_in_cursum(journal,
Chao Yu184a5cd2014-09-04 18:13:01 +08003663 SIT_JOURNAL, segno, 1);
3664 f2fs_bug_on(sbi, offset < 0);
Chao Yudfc08a12016-02-14 18:50:40 +08003665 segno_in_journal(journal, offset) =
Chao Yu184a5cd2014-09-04 18:13:01 +08003666 cpu_to_le32(segno);
3667 seg_info_to_raw_sit(se,
Chao Yudfc08a12016-02-14 18:50:40 +08003668 &sit_in_journal(journal, offset));
Zhikang Zhang56b07e72018-04-09 04:28:41 +08003669 check_block_count(sbi, segno,
3670 &sit_in_journal(journal, offset));
Chao Yu184a5cd2014-09-04 18:13:01 +08003671 } else {
3672 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
3673 seg_info_to_raw_sit(se,
3674 &raw_sit->entries[sit_offset]);
Zhikang Zhang56b07e72018-04-09 04:28:41 +08003675 check_block_count(sbi, segno,
3676 &raw_sit->entries[sit_offset]);
Chao Yu184a5cd2014-09-04 18:13:01 +08003677 }
3678
3679 __clear_bit(segno, bitmap);
3680 sit_i->dirty_sentries--;
3681 ses->entry_cnt--;
3682 }
3683
Chao Yub7ad7512016-02-19 18:08:46 +08003684 if (to_journal)
3685 up_write(&curseg->journal_rwsem);
3686 else
Chao Yu184a5cd2014-09-04 18:13:01 +08003687 f2fs_put_page(page, 1);
3688
3689 f2fs_bug_on(sbi, ses->entry_cnt);
3690 release_sit_entry_set(ses);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003691 }
Chao Yu184a5cd2014-09-04 18:13:01 +08003692
3693 f2fs_bug_on(sbi, !list_empty(head));
3694 f2fs_bug_on(sbi, sit_i->dirty_sentries);
Chao Yu184a5cd2014-09-04 18:13:01 +08003695out:
Chao Yuc473f1a2017-04-27 20:40:39 +08003696 if (cpc->reason & CP_DISCARD) {
Yunlei He650d3c42016-12-22 11:46:24 +08003697 __u64 trim_start = cpc->trim_start;
3698
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003699 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++)
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08003700 add_discard_addrs(sbi, cpc, false);
Yunlei He650d3c42016-12-22 11:46:24 +08003701
3702 cpc->trim_start = trim_start;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003703 }
Chao Yu3d26fa62017-10-30 17:49:53 +08003704 up_write(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003705
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003706 set_prefree_as_free_segments(sbi);
3707}
3708
3709static int build_sit_info(struct f2fs_sb_info *sbi)
3710{
3711 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003712 struct sit_info *sit_i;
3713 unsigned int sit_segs, start;
Chao Yuae27d622017-01-07 18:52:34 +08003714 char *src_bitmap;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003715 unsigned int bitmap_size;
3716
3717 /* allocate memory for SIT information */
Chao Yuacbf0542017-11-30 19:28:17 +08003718 sit_i = f2fs_kzalloc(sbi, sizeof(struct sit_info), GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003719 if (!sit_i)
3720 return -ENOMEM;
3721
3722 SM_I(sbi)->sit_info = sit_i;
3723
Kees Cook9d2a7892018-06-12 14:28:35 -07003724 sit_i->sentries =
3725 f2fs_kvzalloc(sbi, array_size(sizeof(struct seg_entry),
3726 MAIN_SEGS(sbi)),
3727 GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003728 if (!sit_i->sentries)
3729 return -ENOMEM;
3730
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003731 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Chao Yu628b3d12017-11-30 19:28:18 +08003732 sit_i->dirty_sentries_bitmap = f2fs_kvzalloc(sbi, bitmap_size,
3733 GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003734 if (!sit_i->dirty_sentries_bitmap)
3735 return -ENOMEM;
3736
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003737 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003738 sit_i->sentries[start].cur_valid_map
Chao Yuacbf0542017-11-30 19:28:17 +08003739 = f2fs_kzalloc(sbi, SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003740 sit_i->sentries[start].ckpt_valid_map
Chao Yuacbf0542017-11-30 19:28:17 +08003741 = f2fs_kzalloc(sbi, SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07003742 if (!sit_i->sentries[start].cur_valid_map ||
Jaegeuk Kim3e025742016-08-02 10:56:40 -07003743 !sit_i->sentries[start].ckpt_valid_map)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003744 return -ENOMEM;
Jaegeuk Kim3e025742016-08-02 10:56:40 -07003745
Chao Yu355e7892017-01-07 18:51:01 +08003746#ifdef CONFIG_F2FS_CHECK_FS
3747 sit_i->sentries[start].cur_valid_map_mir
Chao Yuacbf0542017-11-30 19:28:17 +08003748 = f2fs_kzalloc(sbi, SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
Chao Yu355e7892017-01-07 18:51:01 +08003749 if (!sit_i->sentries[start].cur_valid_map_mir)
3750 return -ENOMEM;
3751#endif
3752
Jaegeuk Kim3e025742016-08-02 10:56:40 -07003753 if (f2fs_discard_en(sbi)) {
3754 sit_i->sentries[start].discard_map
Chao Yuacbf0542017-11-30 19:28:17 +08003755 = f2fs_kzalloc(sbi, SIT_VBLOCK_MAP_SIZE,
3756 GFP_KERNEL);
Jaegeuk Kim3e025742016-08-02 10:56:40 -07003757 if (!sit_i->sentries[start].discard_map)
3758 return -ENOMEM;
3759 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003760 }
3761
Chao Yuacbf0542017-11-30 19:28:17 +08003762 sit_i->tmp_map = f2fs_kzalloc(sbi, SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
Jaegeuk Kim60a3b782015-02-10 16:44:29 -08003763 if (!sit_i->tmp_map)
3764 return -ENOMEM;
3765
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003766 if (sbi->segs_per_sec > 1) {
Kees Cook9d2a7892018-06-12 14:28:35 -07003767 sit_i->sec_entries =
3768 f2fs_kvzalloc(sbi, array_size(sizeof(struct sec_entry),
3769 MAIN_SECS(sbi)),
3770 GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003771 if (!sit_i->sec_entries)
3772 return -ENOMEM;
3773 }
3774
3775 /* get information related with SIT */
3776 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
3777
3778 /* setup SIT bitmap from ckeckpoint pack */
3779 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
3780 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
3781
Chao Yuae27d622017-01-07 18:52:34 +08003782 sit_i->sit_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
3783 if (!sit_i->sit_bitmap)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003784 return -ENOMEM;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003785
Chao Yuae27d622017-01-07 18:52:34 +08003786#ifdef CONFIG_F2FS_CHECK_FS
3787 sit_i->sit_bitmap_mir = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
3788 if (!sit_i->sit_bitmap_mir)
3789 return -ENOMEM;
3790#endif
3791
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003792 /* init SIT information */
3793 sit_i->s_ops = &default_salloc_ops;
3794
3795 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
3796 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
Jaegeuk Kimc79b7ff2016-11-14 18:20:10 -08003797 sit_i->written_valid_blocks = 0;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003798 sit_i->bitmap_size = bitmap_size;
3799 sit_i->dirty_sentries = 0;
3800 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
3801 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
Deepa Dinamani48fbfe52017-05-08 15:59:10 -07003802 sit_i->mounted_time = ktime_get_real_seconds();
Chao Yu3d26fa62017-10-30 17:49:53 +08003803 init_rwsem(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003804 return 0;
3805}
3806
3807static int build_free_segmap(struct f2fs_sb_info *sbi)
3808{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003809 struct free_segmap_info *free_i;
3810 unsigned int bitmap_size, sec_bitmap_size;
3811
3812 /* allocate memory for free segmap information */
Chao Yuacbf0542017-11-30 19:28:17 +08003813 free_i = f2fs_kzalloc(sbi, sizeof(struct free_segmap_info), GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003814 if (!free_i)
3815 return -ENOMEM;
3816
3817 SM_I(sbi)->free_info = free_i;
3818
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003819 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Chao Yu628b3d12017-11-30 19:28:18 +08003820 free_i->free_segmap = f2fs_kvmalloc(sbi, bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003821 if (!free_i->free_segmap)
3822 return -ENOMEM;
3823
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003824 sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
Chao Yu628b3d12017-11-30 19:28:18 +08003825 free_i->free_secmap = f2fs_kvmalloc(sbi, sec_bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003826 if (!free_i->free_secmap)
3827 return -ENOMEM;
3828
3829 /* set all segments as dirty temporarily */
3830 memset(free_i->free_segmap, 0xff, bitmap_size);
3831 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
3832
3833 /* init free segmap information */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003834 free_i->start_segno = GET_SEGNO_FROM_SEG0(sbi, MAIN_BLKADDR(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003835 free_i->free_segments = 0;
3836 free_i->free_sections = 0;
Chao Yu1a118cc2015-02-11 18:20:38 +08003837 spin_lock_init(&free_i->segmap_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003838 return 0;
3839}
3840
3841static int build_curseg(struct f2fs_sb_info *sbi)
3842{
Namjae Jeon1042d602012-12-01 10:56:13 +09003843 struct curseg_info *array;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003844 int i;
3845
Kees Cook026f0502018-06-12 14:28:23 -07003846 array = f2fs_kzalloc(sbi, array_size(NR_CURSEG_TYPE, sizeof(*array)),
3847 GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003848 if (!array)
3849 return -ENOMEM;
3850
3851 SM_I(sbi)->curseg_array = array;
3852
3853 for (i = 0; i < NR_CURSEG_TYPE; i++) {
3854 mutex_init(&array[i].curseg_mutex);
Chao Yuacbf0542017-11-30 19:28:17 +08003855 array[i].sum_blk = f2fs_kzalloc(sbi, PAGE_SIZE, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003856 if (!array[i].sum_blk)
3857 return -ENOMEM;
Chao Yub7ad7512016-02-19 18:08:46 +08003858 init_rwsem(&array[i].journal_rwsem);
Chao Yuacbf0542017-11-30 19:28:17 +08003859 array[i].journal = f2fs_kzalloc(sbi,
3860 sizeof(struct f2fs_journal), GFP_KERNEL);
Chao Yub7ad7512016-02-19 18:08:46 +08003861 if (!array[i].journal)
3862 return -ENOMEM;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003863 array[i].segno = NULL_SEGNO;
3864 array[i].next_blkoff = 0;
3865 }
3866 return restore_curseg_summaries(sbi);
3867}
3868
Jaegeuk Kimc39a1b32017-12-19 19:16:34 -08003869static int build_sit_entries(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003870{
3871 struct sit_info *sit_i = SIT_I(sbi);
3872 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08003873 struct f2fs_journal *journal = curseg->journal;
Yunlei He9c094042016-09-24 12:29:18 +08003874 struct seg_entry *se;
3875 struct f2fs_sit_entry sit;
Chao Yu74de5932013-11-22 09:09:59 +08003876 int sit_blk_cnt = SIT_BLK_CNT(sbi);
3877 unsigned int i, start, end;
3878 unsigned int readed, start_blk = 0;
Jaegeuk Kimc39a1b32017-12-19 19:16:34 -08003879 int err = 0;
Jaegeuk Kim8a29c122018-04-24 21:34:05 -06003880 block_t total_node_blocks = 0;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003881
Chao Yu74de5932013-11-22 09:09:59 +08003882 do {
Chao Yu4d57b862018-05-30 00:20:41 +08003883 readed = f2fs_ra_meta_pages(sbi, start_blk, BIO_MAX_PAGES,
Jaegeuk Kim664ba972016-10-18 11:07:45 -07003884 META_SIT, true);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003885
Chao Yu74de5932013-11-22 09:09:59 +08003886 start = start_blk * sit_i->sents_per_block;
3887 end = (start_blk + readed) * sit_i->sents_per_block;
3888
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003889 for (; start < end && start < MAIN_SEGS(sbi); start++) {
Chao Yu74de5932013-11-22 09:09:59 +08003890 struct f2fs_sit_block *sit_blk;
Chao Yu74de5932013-11-22 09:09:59 +08003891 struct page *page;
3892
Yunlei He9c094042016-09-24 12:29:18 +08003893 se = &sit_i->sentries[start];
Chao Yu74de5932013-11-22 09:09:59 +08003894 page = get_current_sit_page(sbi, start);
3895 sit_blk = (struct f2fs_sit_block *)page_address(page);
3896 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
3897 f2fs_put_page(page, 1);
Chao Yud600af232016-08-19 23:13:47 +08003898
Jaegeuk Kimc39a1b32017-12-19 19:16:34 -08003899 err = check_block_count(sbi, start, &sit);
3900 if (err)
3901 return err;
Chao Yu74de5932013-11-22 09:09:59 +08003902 seg_info_from_raw_sit(se, &sit);
Jaegeuk Kim8a29c122018-04-24 21:34:05 -06003903 if (IS_NODESEG(se->type))
3904 total_node_blocks += se->valid_blocks;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07003905
3906 /* build discard map only one time */
Jaegeuk Kim3e025742016-08-02 10:56:40 -07003907 if (f2fs_discard_en(sbi)) {
Chao Yu1f43e2a2017-04-28 13:56:08 +08003908 if (is_set_ckpt_flags(sbi, CP_TRIMMED_FLAG)) {
3909 memset(se->discard_map, 0xff,
3910 SIT_VBLOCK_MAP_SIZE);
3911 } else {
3912 memcpy(se->discard_map,
3913 se->cur_valid_map,
3914 SIT_VBLOCK_MAP_SIZE);
3915 sbi->discard_blks +=
3916 sbi->blocks_per_seg -
3917 se->valid_blocks;
3918 }
Jaegeuk Kim3e025742016-08-02 10:56:40 -07003919 }
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07003920
Chao Yud600af232016-08-19 23:13:47 +08003921 if (sbi->segs_per_sec > 1)
3922 get_sec_entry(sbi, start)->valid_blocks +=
3923 se->valid_blocks;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003924 }
Chao Yu74de5932013-11-22 09:09:59 +08003925 start_blk += readed;
3926 } while (start_blk < sit_blk_cnt);
Chao Yud600af232016-08-19 23:13:47 +08003927
3928 down_read(&curseg->journal_rwsem);
3929 for (i = 0; i < sits_in_cursum(journal); i++) {
Chao Yud600af232016-08-19 23:13:47 +08003930 unsigned int old_valid_blocks;
3931
3932 start = le32_to_cpu(segno_in_journal(journal, i));
Jaegeuk Kimb2ca3742018-04-24 15:44:16 -06003933 if (start >= MAIN_SEGS(sbi)) {
3934 f2fs_msg(sbi->sb, KERN_ERR,
3935 "Wrong journal entry on segno %u",
3936 start);
3937 set_sbi_flag(sbi, SBI_NEED_FSCK);
3938 err = -EINVAL;
3939 break;
3940 }
3941
Chao Yud600af232016-08-19 23:13:47 +08003942 se = &sit_i->sentries[start];
3943 sit = sit_in_journal(journal, i);
3944
3945 old_valid_blocks = se->valid_blocks;
Jaegeuk Kim8a29c122018-04-24 21:34:05 -06003946 if (IS_NODESEG(se->type))
3947 total_node_blocks -= old_valid_blocks;
Chao Yud600af232016-08-19 23:13:47 +08003948
Jaegeuk Kimc39a1b32017-12-19 19:16:34 -08003949 err = check_block_count(sbi, start, &sit);
3950 if (err)
3951 break;
Chao Yud600af232016-08-19 23:13:47 +08003952 seg_info_from_raw_sit(se, &sit);
Jaegeuk Kim8a29c122018-04-24 21:34:05 -06003953 if (IS_NODESEG(se->type))
3954 total_node_blocks += se->valid_blocks;
Chao Yud600af232016-08-19 23:13:47 +08003955
3956 if (f2fs_discard_en(sbi)) {
Chao Yu1f43e2a2017-04-28 13:56:08 +08003957 if (is_set_ckpt_flags(sbi, CP_TRIMMED_FLAG)) {
3958 memset(se->discard_map, 0xff,
3959 SIT_VBLOCK_MAP_SIZE);
3960 } else {
3961 memcpy(se->discard_map, se->cur_valid_map,
3962 SIT_VBLOCK_MAP_SIZE);
Chao Yua9af3fd2018-04-25 19:38:17 +08003963 sbi->discard_blks += old_valid_blocks;
3964 sbi->discard_blks -= se->valid_blocks;
Chao Yu1f43e2a2017-04-28 13:56:08 +08003965 }
Chao Yud600af232016-08-19 23:13:47 +08003966 }
3967
Chao Yua9af3fd2018-04-25 19:38:17 +08003968 if (sbi->segs_per_sec > 1) {
Chao Yud600af232016-08-19 23:13:47 +08003969 get_sec_entry(sbi, start)->valid_blocks +=
Chao Yua9af3fd2018-04-25 19:38:17 +08003970 se->valid_blocks;
3971 get_sec_entry(sbi, start)->valid_blocks -=
3972 old_valid_blocks;
3973 }
Chao Yud600af232016-08-19 23:13:47 +08003974 }
3975 up_read(&curseg->journal_rwsem);
Jaegeuk Kim8a29c122018-04-24 21:34:05 -06003976
3977 if (!err && total_node_blocks != valid_node_count(sbi)) {
3978 f2fs_msg(sbi->sb, KERN_ERR,
3979 "SIT is corrupted node# %u vs %u",
3980 total_node_blocks, valid_node_count(sbi));
3981 set_sbi_flag(sbi, SBI_NEED_FSCK);
3982 err = -EINVAL;
3983 }
3984
Jaegeuk Kimc39a1b32017-12-19 19:16:34 -08003985 return err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003986}
3987
3988static void init_free_segmap(struct f2fs_sb_info *sbi)
3989{
3990 unsigned int start;
3991 int type;
3992
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003993 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003994 struct seg_entry *sentry = get_seg_entry(sbi, start);
3995 if (!sentry->valid_blocks)
3996 __set_free(sbi, start);
Jaegeuk Kimc79b7ff2016-11-14 18:20:10 -08003997 else
3998 SIT_I(sbi)->written_valid_blocks +=
3999 sentry->valid_blocks;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004000 }
4001
4002 /* set use the current segments */
4003 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
4004 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
4005 __set_test_and_inuse(sbi, curseg_t->segno);
4006 }
4007}
4008
4009static void init_dirty_segmap(struct f2fs_sb_info *sbi)
4010{
4011 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
4012 struct free_segmap_info *free_i = FREE_I(sbi);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07004013 unsigned int segno = 0, offset = 0;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004014 unsigned short valid_blocks;
4015
Namjae Jeon8736fbf2013-06-16 09:49:11 +09004016 while (1) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004017 /* find dirty segment based on free segmap */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07004018 segno = find_next_inuse(free_i, MAIN_SEGS(sbi), offset);
4019 if (segno >= MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004020 break;
4021 offset = segno + 1;
Jaegeuk Kim302bd342017-04-07 14:33:22 -07004022 valid_blocks = get_valid_blocks(sbi, segno, false);
Jaegeuk Kimec325b52014-09-02 16:24:11 -07004023 if (valid_blocks == sbi->blocks_per_seg || !valid_blocks)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004024 continue;
Jaegeuk Kimec325b52014-09-02 16:24:11 -07004025 if (valid_blocks > sbi->blocks_per_seg) {
4026 f2fs_bug_on(sbi, 1);
4027 continue;
4028 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004029 mutex_lock(&dirty_i->seglist_lock);
4030 __locate_dirty_segment(sbi, segno, DIRTY);
4031 mutex_unlock(&dirty_i->seglist_lock);
4032 }
4033}
4034
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09004035static int init_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004036{
4037 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07004038 unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004039
Chao Yu628b3d12017-11-30 19:28:18 +08004040 dirty_i->victim_secmap = f2fs_kvzalloc(sbi, bitmap_size, GFP_KERNEL);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09004041 if (!dirty_i->victim_secmap)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004042 return -ENOMEM;
4043 return 0;
4044}
4045
4046static int build_dirty_segmap(struct f2fs_sb_info *sbi)
4047{
4048 struct dirty_seglist_info *dirty_i;
4049 unsigned int bitmap_size, i;
4050
4051 /* allocate memory for dirty segments list information */
Chao Yuacbf0542017-11-30 19:28:17 +08004052 dirty_i = f2fs_kzalloc(sbi, sizeof(struct dirty_seglist_info),
4053 GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004054 if (!dirty_i)
4055 return -ENOMEM;
4056
4057 SM_I(sbi)->dirty_info = dirty_i;
4058 mutex_init(&dirty_i->seglist_lock);
4059
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07004060 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004061
4062 for (i = 0; i < NR_DIRTY_TYPE; i++) {
Chao Yu628b3d12017-11-30 19:28:18 +08004063 dirty_i->dirty_segmap[i] = f2fs_kvzalloc(sbi, bitmap_size,
4064 GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004065 if (!dirty_i->dirty_segmap[i])
4066 return -ENOMEM;
4067 }
4068
4069 init_dirty_segmap(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09004070 return init_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004071}
4072
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09004073/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004074 * Update min, max modified time for cost-benefit GC algorithm
4075 */
4076static void init_min_max_mtime(struct f2fs_sb_info *sbi)
4077{
4078 struct sit_info *sit_i = SIT_I(sbi);
4079 unsigned int segno;
4080
Chao Yu3d26fa62017-10-30 17:49:53 +08004081 down_write(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004082
Chao Yu5ad25442018-05-15 18:59:55 +08004083 sit_i->min_mtime = ULLONG_MAX;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004084
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07004085 for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004086 unsigned int i;
4087 unsigned long long mtime = 0;
4088
4089 for (i = 0; i < sbi->segs_per_sec; i++)
4090 mtime += get_seg_entry(sbi, segno + i)->mtime;
4091
4092 mtime = div_u64(mtime, sbi->segs_per_sec);
4093
4094 if (sit_i->min_mtime > mtime)
4095 sit_i->min_mtime = mtime;
4096 }
Chao Yua1f72ac22018-06-04 23:20:17 +08004097 sit_i->max_mtime = get_mtime(sbi, false);
Chao Yu3d26fa62017-10-30 17:49:53 +08004098 up_write(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004099}
4100
Chao Yu4d57b862018-05-30 00:20:41 +08004101int f2fs_build_segment_manager(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004102{
4103 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
4104 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Namjae Jeon1042d602012-12-01 10:56:13 +09004105 struct f2fs_sm_info *sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004106 int err;
4107
Chao Yuacbf0542017-11-30 19:28:17 +08004108 sm_info = f2fs_kzalloc(sbi, sizeof(struct f2fs_sm_info), GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004109 if (!sm_info)
4110 return -ENOMEM;
4111
4112 /* init sm info */
4113 sbi->sm_info = sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004114 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
4115 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
4116 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
4117 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
4118 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
4119 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
4120 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
Jaegeuk Kim58c41032014-03-19 14:17:21 +09004121 sm_info->rec_prefree_segments = sm_info->main_segments *
4122 DEF_RECLAIM_PREFREE_SEGMENTS / 100;
Jaegeuk Kim44a83492016-07-13 18:23:35 -07004123 if (sm_info->rec_prefree_segments > DEF_MAX_RECLAIM_PREFREE_SEGMENTS)
4124 sm_info->rec_prefree_segments = DEF_MAX_RECLAIM_PREFREE_SEGMENTS;
4125
Jaegeuk Kim52763a42016-06-13 09:47:48 -07004126 if (!test_opt(sbi, LFS))
4127 sm_info->ipu_policy = 1 << F2FS_IPU_FSYNC;
Jaegeuk Kim216fbd62013-11-07 13:13:42 +09004128 sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -07004129 sm_info->min_fsync_blocks = DEF_MIN_FSYNC_BLOCKS;
Jaegeuk Kim853137c2018-08-09 17:53:34 -07004130 sm_info->min_seq_blocks = sbi->blocks_per_seg * sbi->segs_per_sec;
Jaegeuk Kimef095d12017-03-24 20:05:13 -04004131 sm_info->min_hot_blocks = DEF_MIN_HOT_BLOCKS;
Chao Yua2a12b62017-10-28 16:52:33 +08004132 sm_info->min_ssr_sections = reserved_sections(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004133
Chao Yu184a5cd2014-09-04 18:13:01 +08004134 INIT_LIST_HEAD(&sm_info->sit_entry_set);
4135
Chao Yu2b603112017-11-02 20:41:03 +08004136 init_rwsem(&sm_info->curseg_lock);
4137
Yunlei Hed4fdf8b2017-06-01 16:43:51 +08004138 if (!f2fs_readonly(sbi->sb)) {
Chao Yu4d57b862018-05-30 00:20:41 +08004139 err = f2fs_create_flush_cmd_control(sbi);
Gu Zheng2163d192014-04-27 14:21:33 +08004140 if (err)
Gu Zhenga688b9d9e2014-04-27 14:21:21 +08004141 return err;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +09004142 }
4143
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08004144 err = create_discard_cmd_control(sbi);
4145 if (err)
4146 return err;
4147
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004148 err = build_sit_info(sbi);
4149 if (err)
4150 return err;
4151 err = build_free_segmap(sbi);
4152 if (err)
4153 return err;
4154 err = build_curseg(sbi);
4155 if (err)
4156 return err;
4157
4158 /* reinit free segmap based on SIT */
Jaegeuk Kimc39a1b32017-12-19 19:16:34 -08004159 err = build_sit_entries(sbi);
4160 if (err)
4161 return err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004162
4163 init_free_segmap(sbi);
4164 err = build_dirty_segmap(sbi);
4165 if (err)
4166 return err;
4167
4168 init_min_max_mtime(sbi);
4169 return 0;
4170}
4171
4172static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
4173 enum dirty_type dirty_type)
4174{
4175 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
4176
4177 mutex_lock(&dirty_i->seglist_lock);
Jaegeuk Kim39307a82015-09-22 13:50:47 -07004178 kvfree(dirty_i->dirty_segmap[dirty_type]);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004179 dirty_i->nr_dirty[dirty_type] = 0;
4180 mutex_unlock(&dirty_i->seglist_lock);
4181}
4182
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09004183static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004184{
4185 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim39307a82015-09-22 13:50:47 -07004186 kvfree(dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004187}
4188
4189static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
4190{
4191 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
4192 int i;
4193
4194 if (!dirty_i)
4195 return;
4196
4197 /* discard pre-free/dirty segments list */
4198 for (i = 0; i < NR_DIRTY_TYPE; i++)
4199 discard_dirty_segmap(sbi, i);
4200
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09004201 destroy_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004202 SM_I(sbi)->dirty_info = NULL;
4203 kfree(dirty_i);
4204}
4205
4206static void destroy_curseg(struct f2fs_sb_info *sbi)
4207{
4208 struct curseg_info *array = SM_I(sbi)->curseg_array;
4209 int i;
4210
4211 if (!array)
4212 return;
4213 SM_I(sbi)->curseg_array = NULL;
Chao Yub7ad7512016-02-19 18:08:46 +08004214 for (i = 0; i < NR_CURSEG_TYPE; i++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004215 kfree(array[i].sum_blk);
Chao Yub7ad7512016-02-19 18:08:46 +08004216 kfree(array[i].journal);
4217 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004218 kfree(array);
4219}
4220
4221static void destroy_free_segmap(struct f2fs_sb_info *sbi)
4222{
4223 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
4224 if (!free_i)
4225 return;
4226 SM_I(sbi)->free_info = NULL;
Jaegeuk Kim39307a82015-09-22 13:50:47 -07004227 kvfree(free_i->free_segmap);
4228 kvfree(free_i->free_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004229 kfree(free_i);
4230}
4231
4232static void destroy_sit_info(struct f2fs_sb_info *sbi)
4233{
4234 struct sit_info *sit_i = SIT_I(sbi);
4235 unsigned int start;
4236
4237 if (!sit_i)
4238 return;
4239
4240 if (sit_i->sentries) {
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07004241 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004242 kfree(sit_i->sentries[start].cur_valid_map);
Chao Yu355e7892017-01-07 18:51:01 +08004243#ifdef CONFIG_F2FS_CHECK_FS
4244 kfree(sit_i->sentries[start].cur_valid_map_mir);
4245#endif
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004246 kfree(sit_i->sentries[start].ckpt_valid_map);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07004247 kfree(sit_i->sentries[start].discard_map);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004248 }
4249 }
Jaegeuk Kim60a3b782015-02-10 16:44:29 -08004250 kfree(sit_i->tmp_map);
4251
Jaegeuk Kim39307a82015-09-22 13:50:47 -07004252 kvfree(sit_i->sentries);
4253 kvfree(sit_i->sec_entries);
4254 kvfree(sit_i->dirty_sentries_bitmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004255
4256 SM_I(sbi)->sit_info = NULL;
4257 kfree(sit_i->sit_bitmap);
Chao Yuae27d622017-01-07 18:52:34 +08004258#ifdef CONFIG_F2FS_CHECK_FS
4259 kfree(sit_i->sit_bitmap_mir);
4260#endif
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004261 kfree(sit_i);
4262}
4263
Chao Yu4d57b862018-05-30 00:20:41 +08004264void f2fs_destroy_segment_manager(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004265{
4266 struct f2fs_sm_info *sm_info = SM_I(sbi);
Gu Zhenga688b9d9e2014-04-27 14:21:21 +08004267
Chao Yu3b03f722013-11-06 09:12:04 +08004268 if (!sm_info)
4269 return;
Chao Yu4d57b862018-05-30 00:20:41 +08004270 f2fs_destroy_flush_cmd_control(sbi, true);
Chao Yuf0994052017-03-27 18:14:04 +08004271 destroy_discard_cmd_control(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004272 destroy_dirty_segmap(sbi);
4273 destroy_curseg(sbi);
4274 destroy_free_segmap(sbi);
4275 destroy_sit_info(sbi);
4276 sbi->sm_info = NULL;
4277 kfree(sm_info);
4278}
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09004279
Chao Yu4d57b862018-05-30 00:20:41 +08004280int __init f2fs_create_segment_manager_caches(void)
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09004281{
4282 discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
Gu Zhenge8512d22014-03-07 18:43:28 +08004283 sizeof(struct discard_entry));
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09004284 if (!discard_entry_slab)
Chao Yu184a5cd2014-09-04 18:13:01 +08004285 goto fail;
4286
Jaegeuk Kimb01a9202017-01-09 14:13:03 -08004287 discard_cmd_slab = f2fs_kmem_cache_create("discard_cmd",
4288 sizeof(struct discard_cmd));
4289 if (!discard_cmd_slab)
Chao Yu6ab2a302016-09-05 12:28:26 +08004290 goto destroy_discard_entry;
Chao Yu275b66b2016-08-29 23:58:34 +08004291
Chao Yu184a5cd2014-09-04 18:13:01 +08004292 sit_entry_set_slab = f2fs_kmem_cache_create("sit_entry_set",
Changman Leec9ee0082014-11-21 15:42:07 +09004293 sizeof(struct sit_entry_set));
Chao Yu184a5cd2014-09-04 18:13:01 +08004294 if (!sit_entry_set_slab)
Jaegeuk Kimb01a9202017-01-09 14:13:03 -08004295 goto destroy_discard_cmd;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07004296
4297 inmem_entry_slab = f2fs_kmem_cache_create("inmem_page_entry",
4298 sizeof(struct inmem_pages));
4299 if (!inmem_entry_slab)
4300 goto destroy_sit_entry_set;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09004301 return 0;
Chao Yu184a5cd2014-09-04 18:13:01 +08004302
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07004303destroy_sit_entry_set:
4304 kmem_cache_destroy(sit_entry_set_slab);
Jaegeuk Kimb01a9202017-01-09 14:13:03 -08004305destroy_discard_cmd:
4306 kmem_cache_destroy(discard_cmd_slab);
Chao Yu6ab2a302016-09-05 12:28:26 +08004307destroy_discard_entry:
Chao Yu184a5cd2014-09-04 18:13:01 +08004308 kmem_cache_destroy(discard_entry_slab);
4309fail:
4310 return -ENOMEM;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09004311}
4312
Chao Yu4d57b862018-05-30 00:20:41 +08004313void f2fs_destroy_segment_manager_caches(void)
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09004314{
Chao Yu184a5cd2014-09-04 18:13:01 +08004315 kmem_cache_destroy(sit_entry_set_slab);
Jaegeuk Kimb01a9202017-01-09 14:13:03 -08004316 kmem_cache_destroy(discard_cmd_slab);
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09004317 kmem_cache_destroy(discard_entry_slab);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07004318 kmem_cache_destroy(inmem_entry_slab);
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09004319}