blob: 9a8d7d415a74903e857278bcb23ce632e628e200 [file] [log] [blame]
Chao Yu7c1a0002018-09-12 09:16:07 +08001// SPDX-License-Identifier: GPL-2.0
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003 * fs/f2fs/segment.c
4 *
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09007 */
8#include <linux/fs.h>
9#include <linux/f2fs_fs.h>
10#include <linux/bio.h>
11#include <linux/blkdev.h>
Geert Uytterhoeven690e4a32012-12-19 22:19:30 +010012#include <linux/prefetch.h>
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +090013#include <linux/kthread.h>
Chao Yu74de5932013-11-22 09:09:59 +080014#include <linux/swap.h>
Jaegeuk Kim60b99b42015-10-05 14:49:57 -070015#include <linux/timer.h>
Jaegeuk Kim1d7be272017-05-17 10:36:58 -070016#include <linux/freezer.h>
Jaegeuk Kim1eb1ef42017-09-09 12:03:23 -070017#include <linux/sched/signal.h>
Jaegeuk Kim351df4b2012-11-02 17:09:16 +090018
19#include "f2fs.h"
20#include "segment.h"
21#include "node.h"
Jaegeuk Kim5f656542017-08-15 21:27:19 -070022#include "gc.h"
Jaegeuk Kim9e4ded32014-12-17 19:58:58 -080023#include "trace.h"
Namjae Jeon6ec178d2013-04-23 17:51:43 +090024#include <trace/events/f2fs.h>
Jaegeuk Kim351df4b2012-11-02 17:09:16 +090025
Changman Lee9a7f1432013-11-15 10:42:51 +090026#define __reverse_ffz(x) __reverse_ffs(~(x))
27
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +090028static struct kmem_cache *discard_entry_slab;
Jaegeuk Kimb01a9202017-01-09 14:13:03 -080029static struct kmem_cache *discard_cmd_slab;
Chao Yu184a5cd2014-09-04 18:13:01 +080030static struct kmem_cache *sit_entry_set_slab;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -070031static struct kmem_cache *inmem_entry_slab;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +090032
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070033static unsigned long __reverse_ulong(unsigned char *str)
34{
35 unsigned long tmp = 0;
36 int shift = 24, idx = 0;
37
38#if BITS_PER_LONG == 64
39 shift = 56;
40#endif
41 while (shift >= 0) {
42 tmp |= (unsigned long)str[idx++] << shift;
43 shift -= BITS_PER_BYTE;
44 }
45 return tmp;
46}
47
Changman Lee9a7f1432013-11-15 10:42:51 +090048/*
49 * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since
50 * MSB and LSB are reversed in a byte by f2fs_set_bit.
51 */
52static inline unsigned long __reverse_ffs(unsigned long word)
53{
54 int num = 0;
55
56#if BITS_PER_LONG == 64
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070057 if ((word & 0xffffffff00000000UL) == 0)
Changman Lee9a7f1432013-11-15 10:42:51 +090058 num += 32;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070059 else
Changman Lee9a7f1432013-11-15 10:42:51 +090060 word >>= 32;
Changman Lee9a7f1432013-11-15 10:42:51 +090061#endif
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070062 if ((word & 0xffff0000) == 0)
Changman Lee9a7f1432013-11-15 10:42:51 +090063 num += 16;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070064 else
Changman Lee9a7f1432013-11-15 10:42:51 +090065 word >>= 16;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070066
67 if ((word & 0xff00) == 0)
Changman Lee9a7f1432013-11-15 10:42:51 +090068 num += 8;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070069 else
Changman Lee9a7f1432013-11-15 10:42:51 +090070 word >>= 8;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070071
Changman Lee9a7f1432013-11-15 10:42:51 +090072 if ((word & 0xf0) == 0)
73 num += 4;
74 else
75 word >>= 4;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070076
Changman Lee9a7f1432013-11-15 10:42:51 +090077 if ((word & 0xc) == 0)
78 num += 2;
79 else
80 word >>= 2;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070081
Changman Lee9a7f1432013-11-15 10:42:51 +090082 if ((word & 0x2) == 0)
83 num += 1;
84 return num;
85}
86
87/*
arter97e1c42042014-08-06 23:22:50 +090088 * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c because
Changman Lee9a7f1432013-11-15 10:42:51 +090089 * f2fs_set_bit makes MSB and LSB reversed in a byte.
Fan Li692223d2015-11-12 08:43:04 +080090 * @size must be integral times of unsigned long.
Changman Lee9a7f1432013-11-15 10:42:51 +090091 * Example:
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070092 * MSB <--> LSB
93 * f2fs_set_bit(0, bitmap) => 1000 0000
94 * f2fs_set_bit(7, bitmap) => 0000 0001
Changman Lee9a7f1432013-11-15 10:42:51 +090095 */
96static unsigned long __find_rev_next_bit(const unsigned long *addr,
97 unsigned long size, unsigned long offset)
98{
99 const unsigned long *p = addr + BIT_WORD(offset);
Fan Li692223d2015-11-12 08:43:04 +0800100 unsigned long result = size;
Changman Lee9a7f1432013-11-15 10:42:51 +0900101 unsigned long tmp;
Changman Lee9a7f1432013-11-15 10:42:51 +0900102
103 if (offset >= size)
104 return size;
105
Fan Li692223d2015-11-12 08:43:04 +0800106 size -= (offset & ~(BITS_PER_LONG - 1));
Changman Lee9a7f1432013-11-15 10:42:51 +0900107 offset %= BITS_PER_LONG;
Changman Lee9a7f1432013-11-15 10:42:51 +0900108
Fan Li692223d2015-11-12 08:43:04 +0800109 while (1) {
110 if (*p == 0)
111 goto pass;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700112
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700113 tmp = __reverse_ulong((unsigned char *)p);
Fan Li692223d2015-11-12 08:43:04 +0800114
115 tmp &= ~0UL >> offset;
116 if (size < BITS_PER_LONG)
117 tmp &= (~0UL << (BITS_PER_LONG - size));
Changman Lee9a7f1432013-11-15 10:42:51 +0900118 if (tmp)
Fan Li692223d2015-11-12 08:43:04 +0800119 goto found;
120pass:
121 if (size <= BITS_PER_LONG)
122 break;
Changman Lee9a7f1432013-11-15 10:42:51 +0900123 size -= BITS_PER_LONG;
Fan Li692223d2015-11-12 08:43:04 +0800124 offset = 0;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700125 p++;
Changman Lee9a7f1432013-11-15 10:42:51 +0900126 }
Fan Li692223d2015-11-12 08:43:04 +0800127 return result;
128found:
129 return result - size + __reverse_ffs(tmp);
Changman Lee9a7f1432013-11-15 10:42:51 +0900130}
131
132static unsigned long __find_rev_next_zero_bit(const unsigned long *addr,
133 unsigned long size, unsigned long offset)
134{
135 const unsigned long *p = addr + BIT_WORD(offset);
Jaegeuk Kim80609442015-12-04 16:51:13 -0800136 unsigned long result = size;
Changman Lee9a7f1432013-11-15 10:42:51 +0900137 unsigned long tmp;
Changman Lee9a7f1432013-11-15 10:42:51 +0900138
139 if (offset >= size)
140 return size;
141
Jaegeuk Kim80609442015-12-04 16:51:13 -0800142 size -= (offset & ~(BITS_PER_LONG - 1));
Changman Lee9a7f1432013-11-15 10:42:51 +0900143 offset %= BITS_PER_LONG;
Changman Lee9a7f1432013-11-15 10:42:51 +0900144
Jaegeuk Kim80609442015-12-04 16:51:13 -0800145 while (1) {
146 if (*p == ~0UL)
147 goto pass;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700148
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700149 tmp = __reverse_ulong((unsigned char *)p);
Jaegeuk Kim80609442015-12-04 16:51:13 -0800150
151 if (offset)
152 tmp |= ~0UL << (BITS_PER_LONG - offset);
153 if (size < BITS_PER_LONG)
154 tmp |= ~0UL >> size;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700155 if (tmp != ~0UL)
Jaegeuk Kim80609442015-12-04 16:51:13 -0800156 goto found;
157pass:
158 if (size <= BITS_PER_LONG)
159 break;
Changman Lee9a7f1432013-11-15 10:42:51 +0900160 size -= BITS_PER_LONG;
Jaegeuk Kim80609442015-12-04 16:51:13 -0800161 offset = 0;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700162 p++;
Changman Lee9a7f1432013-11-15 10:42:51 +0900163 }
Jaegeuk Kim80609442015-12-04 16:51:13 -0800164 return result;
165found:
166 return result - size + __reverse_ffz(tmp);
Changman Lee9a7f1432013-11-15 10:42:51 +0900167}
168
Chao Yu4d57b862018-05-30 00:20:41 +0800169bool f2fs_need_SSR(struct f2fs_sb_info *sbi)
Jaegeuk Kimb3a97a22017-09-09 11:11:04 -0700170{
171 int node_secs = get_blocktype_secs(sbi, F2FS_DIRTY_NODES);
172 int dent_secs = get_blocktype_secs(sbi, F2FS_DIRTY_DENTS);
173 int imeta_secs = get_blocktype_secs(sbi, F2FS_DIRTY_IMETA);
174
175 if (test_opt(sbi, LFS))
176 return false;
Jaegeuk Kim5b0e9532018-05-07 14:22:40 -0700177 if (sbi->gc_mode == GC_URGENT)
Jaegeuk Kimb3a97a22017-09-09 11:11:04 -0700178 return true;
179
180 return free_sections(sbi) <= (node_secs + 2 * dent_secs + imeta_secs +
Chao Yua2a12b62017-10-28 16:52:33 +0800181 SM_I(sbi)->min_ssr_sections + reserved_sections(sbi));
Jaegeuk Kimb3a97a22017-09-09 11:11:04 -0700182}
183
Chao Yu4d57b862018-05-30 00:20:41 +0800184void f2fs_register_inmem_page(struct inode *inode, struct page *page)
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700185{
Jaegeuk Kim57864ae2017-10-18 19:05:57 -0700186 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700187 struct f2fs_inode_info *fi = F2FS_I(inode);
188 struct inmem_pages *new;
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800189
Jaegeuk Kim9e4ded32014-12-17 19:58:58 -0800190 f2fs_trace_pid(page);
Jaegeuk Kim0722b102014-12-05 11:58:02 -0800191
Chao Yudecd36b2015-08-07 18:42:09 +0800192 set_page_private(page, (unsigned long)ATOMIC_WRITTEN_PAGE);
193 SetPagePrivate(page);
194
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700195 new = f2fs_kmem_cache_alloc(inmem_entry_slab, GFP_NOFS);
196
197 /* add atomic page indices to the list */
198 new->page = page;
199 INIT_LIST_HEAD(&new->list);
Chao Yudecd36b2015-08-07 18:42:09 +0800200
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700201 /* increase reference count with clean state */
202 mutex_lock(&fi->inmem_lock);
203 get_page(page);
204 list_add_tail(&new->list, &fi->inmem_pages);
Jaegeuk Kim57864ae2017-10-18 19:05:57 -0700205 spin_lock(&sbi->inode_lock[ATOMIC_FILE]);
206 if (list_empty(&fi->inmem_ilist))
207 list_add_tail(&fi->inmem_ilist, &sbi->inode_list[ATOMIC_FILE]);
208 spin_unlock(&sbi->inode_lock[ATOMIC_FILE]);
Jaegeuk Kim8dcf2ff72014-12-05 17:18:15 -0800209 inc_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700210 mutex_unlock(&fi->inmem_lock);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -0700211
212 trace_f2fs_register_inmem_page(page, INMEM);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700213}
214
Chao Yu28bc1062016-02-06 14:40:34 +0800215static int __revoke_inmem_pages(struct inode *inode,
216 struct list_head *head, bool drop, bool recover)
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700217{
Chao Yu28bc1062016-02-06 14:40:34 +0800218 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700219 struct inmem_pages *cur, *tmp;
Chao Yu28bc1062016-02-06 14:40:34 +0800220 int err = 0;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700221
Chao Yu29b96b52016-02-06 14:38:29 +0800222 list_for_each_entry_safe(cur, tmp, head, list) {
Chao Yu28bc1062016-02-06 14:40:34 +0800223 struct page *page = cur->page;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700224
Chao Yu28bc1062016-02-06 14:40:34 +0800225 if (drop)
226 trace_f2fs_commit_inmem_page(page, INMEM_DROP);
227
228 lock_page(page);
229
Chao Yue5e57322018-04-23 10:36:13 +0800230 f2fs_wait_on_page_writeback(page, DATA, true);
231
Chao Yu28bc1062016-02-06 14:40:34 +0800232 if (recover) {
233 struct dnode_of_data dn;
234 struct node_info ni;
235
236 trace_f2fs_commit_inmem_page(page, INMEM_REVOKE);
Chao Yu7f2b4e82017-08-08 19:09:08 +0800237retry:
Chao Yu28bc1062016-02-06 14:40:34 +0800238 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +0800239 err = f2fs_get_dnode_of_data(&dn, page->index,
240 LOOKUP_NODE);
Chao Yu7f2b4e82017-08-08 19:09:08 +0800241 if (err) {
242 if (err == -ENOMEM) {
243 congestion_wait(BLK_RW_ASYNC, HZ/50);
244 cond_resched();
245 goto retry;
246 }
Chao Yu28bc1062016-02-06 14:40:34 +0800247 err = -EAGAIN;
248 goto next;
249 }
Chao Yu77357302018-07-17 00:02:17 +0800250
251 err = f2fs_get_node_info(sbi, dn.nid, &ni);
252 if (err) {
253 f2fs_put_dnode(&dn);
254 return err;
255 }
256
Daeho Jeongf1d25642018-01-10 16:49:10 +0900257 if (cur->old_addr == NEW_ADDR) {
Chao Yu4d57b862018-05-30 00:20:41 +0800258 f2fs_invalidate_blocks(sbi, dn.data_blkaddr);
Daeho Jeongf1d25642018-01-10 16:49:10 +0900259 f2fs_update_data_blkaddr(&dn, NEW_ADDR);
260 } else
261 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
Chao Yu28bc1062016-02-06 14:40:34 +0800262 cur->old_addr, ni.version, true, true);
263 f2fs_put_dnode(&dn);
264 }
265next:
Jaegeuk Kim63c52d72016-04-12 14:11:03 -0700266 /* we don't need to invalidate this in the sccessful status */
267 if (drop || recover)
268 ClearPageUptodate(page);
Chao Yu28bc1062016-02-06 14:40:34 +0800269 set_page_private(page, 0);
Chao Yuc81ced02016-04-29 20:13:36 +0800270 ClearPagePrivate(page);
Chao Yu28bc1062016-02-06 14:40:34 +0800271 f2fs_put_page(page, 1);
Chao Yudecd36b2015-08-07 18:42:09 +0800272
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700273 list_del(&cur->list);
274 kmem_cache_free(inmem_entry_slab, cur);
Jaegeuk Kim8dcf2ff72014-12-05 17:18:15 -0800275 dec_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700276 }
Chao Yu28bc1062016-02-06 14:40:34 +0800277 return err;
Chao Yu29b96b52016-02-06 14:38:29 +0800278}
279
Chao Yu4d57b862018-05-30 00:20:41 +0800280void f2fs_drop_inmem_pages_all(struct f2fs_sb_info *sbi, bool gc_failure)
Jaegeuk Kim57864ae2017-10-18 19:05:57 -0700281{
282 struct list_head *head = &sbi->inode_list[ATOMIC_FILE];
283 struct inode *inode;
284 struct f2fs_inode_info *fi;
285next:
286 spin_lock(&sbi->inode_lock[ATOMIC_FILE]);
287 if (list_empty(head)) {
288 spin_unlock(&sbi->inode_lock[ATOMIC_FILE]);
289 return;
290 }
291 fi = list_first_entry(head, struct f2fs_inode_info, inmem_ilist);
292 inode = igrab(&fi->vfs_inode);
293 spin_unlock(&sbi->inode_lock[ATOMIC_FILE]);
294
295 if (inode) {
Chao Yu2ef79ec2018-05-07 20:28:54 +0800296 if (gc_failure) {
297 if (fi->i_gc_failures[GC_FAILURE_ATOMIC])
298 goto drop;
299 goto skip;
300 }
301drop:
302 set_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
Chao Yu4d57b862018-05-30 00:20:41 +0800303 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim57864ae2017-10-18 19:05:57 -0700304 iput(inode);
305 }
Chao Yu2ef79ec2018-05-07 20:28:54 +0800306skip:
Jaegeuk Kim57864ae2017-10-18 19:05:57 -0700307 congestion_wait(BLK_RW_ASYNC, HZ/50);
308 cond_resched();
309 goto next;
310}
311
Chao Yu4d57b862018-05-30 00:20:41 +0800312void f2fs_drop_inmem_pages(struct inode *inode)
Chao Yu29b96b52016-02-06 14:38:29 +0800313{
Jaegeuk Kim57864ae2017-10-18 19:05:57 -0700314 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu29b96b52016-02-06 14:38:29 +0800315 struct f2fs_inode_info *fi = F2FS_I(inode);
316
317 mutex_lock(&fi->inmem_lock);
Chao Yu28bc1062016-02-06 14:40:34 +0800318 __revoke_inmem_pages(inode, &fi->inmem_pages, true, false);
Jaegeuk Kim57864ae2017-10-18 19:05:57 -0700319 spin_lock(&sbi->inode_lock[ATOMIC_FILE]);
320 if (!list_empty(&fi->inmem_ilist))
321 list_del_init(&fi->inmem_ilist);
322 spin_unlock(&sbi->inode_lock[ATOMIC_FILE]);
Chao Yu29b96b52016-02-06 14:38:29 +0800323 mutex_unlock(&fi->inmem_lock);
Chao Yu5fe45742017-01-07 18:50:26 +0800324
325 clear_inode_flag(inode, FI_ATOMIC_FILE);
Chao Yu2ef79ec2018-05-07 20:28:54 +0800326 fi->i_gc_failures[GC_FAILURE_ATOMIC] = 0;
Chao Yu5fe45742017-01-07 18:50:26 +0800327 stat_dec_atomic_write(inode);
Chao Yu29b96b52016-02-06 14:38:29 +0800328}
329
Chao Yu4d57b862018-05-30 00:20:41 +0800330void f2fs_drop_inmem_page(struct inode *inode, struct page *page)
Jaegeuk Kim8c242db2017-03-17 09:55:52 +0800331{
332 struct f2fs_inode_info *fi = F2FS_I(inode);
333 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
334 struct list_head *head = &fi->inmem_pages;
335 struct inmem_pages *cur = NULL;
336
337 f2fs_bug_on(sbi, !IS_ATOMIC_WRITTEN_PAGE(page));
338
339 mutex_lock(&fi->inmem_lock);
340 list_for_each_entry(cur, head, list) {
341 if (cur->page == page)
342 break;
343 }
344
Sheng Yongd0891e82018-04-17 17:12:27 +0800345 f2fs_bug_on(sbi, list_empty(head) || cur->page != page);
Jaegeuk Kim8c242db2017-03-17 09:55:52 +0800346 list_del(&cur->list);
347 mutex_unlock(&fi->inmem_lock);
348
349 dec_page_count(sbi, F2FS_INMEM_PAGES);
350 kmem_cache_free(inmem_entry_slab, cur);
351
352 ClearPageUptodate(page);
353 set_page_private(page, 0);
354 ClearPagePrivate(page);
355 f2fs_put_page(page, 0);
356
357 trace_f2fs_commit_inmem_page(page, INMEM_INVALIDATE);
358}
359
Chao Yu4d57b862018-05-30 00:20:41 +0800360static int __f2fs_commit_inmem_pages(struct inode *inode)
Chao Yu29b96b52016-02-06 14:38:29 +0800361{
362 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
363 struct f2fs_inode_info *fi = F2FS_I(inode);
364 struct inmem_pages *cur, *tmp;
365 struct f2fs_io_info fio = {
366 .sbi = sbi,
Chao Yu39d787b2017-09-29 13:59:38 +0800367 .ino = inode->i_ino,
Chao Yu29b96b52016-02-06 14:38:29 +0800368 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -0500369 .op = REQ_OP_WRITE,
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600370 .op_flags = REQ_SYNC | REQ_PRIO,
Chao Yub0af6d42017-08-02 23:21:48 +0800371 .io_type = FS_DATA_IO,
Chao Yu29b96b52016-02-06 14:38:29 +0800372 };
Chao Yucf52b272018-04-23 10:36:14 +0800373 struct list_head revoke_list;
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800374 pgoff_t last_idx = ULONG_MAX;
Chao Yu29b96b52016-02-06 14:38:29 +0800375 int err = 0;
376
Chao Yucf52b272018-04-23 10:36:14 +0800377 INIT_LIST_HEAD(&revoke_list);
378
Chao Yu29b96b52016-02-06 14:38:29 +0800379 list_for_each_entry_safe(cur, tmp, &fi->inmem_pages, list) {
Chao Yu28bc1062016-02-06 14:40:34 +0800380 struct page *page = cur->page;
381
382 lock_page(page);
383 if (page->mapping == inode->i_mapping) {
384 trace_f2fs_commit_inmem_page(page, INMEM);
385
386 set_page_dirty(page);
387 f2fs_wait_on_page_writeback(page, DATA, true);
Chao Yu933439c2016-10-11 22:57:01 +0800388 if (clear_page_dirty_for_io(page)) {
Chao Yu29b96b52016-02-06 14:38:29 +0800389 inode_dec_dirty_pages(inode);
Chao Yu4d57b862018-05-30 00:20:41 +0800390 f2fs_remove_dirty_inode(inode);
Chao Yu933439c2016-10-11 22:57:01 +0800391 }
Jaegeuk Kim640cc182017-07-19 10:59:55 -0700392retry:
Chao Yu28bc1062016-02-06 14:40:34 +0800393 fio.page = page;
Hou Pengyange959c8f2017-04-25 12:45:13 +0000394 fio.old_blkaddr = NULL_ADDR;
Jaegeuk Kim4d978072017-04-26 11:11:12 -0700395 fio.encrypted_page = NULL;
Jaegeuk Kimcc156202017-05-12 13:51:34 -0700396 fio.need_lock = LOCK_DONE;
Chao Yu4d57b862018-05-30 00:20:41 +0800397 err = f2fs_do_write_data_page(&fio);
Chao Yu29b96b52016-02-06 14:38:29 +0800398 if (err) {
Jaegeuk Kim640cc182017-07-19 10:59:55 -0700399 if (err == -ENOMEM) {
400 congestion_wait(BLK_RW_ASYNC, HZ/50);
401 cond_resched();
402 goto retry;
403 }
Chao Yu28bc1062016-02-06 14:40:34 +0800404 unlock_page(page);
Chao Yu29b96b52016-02-06 14:38:29 +0800405 break;
406 }
Chao Yu28bc1062016-02-06 14:40:34 +0800407 /* record old blkaddr for revoking */
408 cur->old_addr = fio.old_blkaddr;
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800409 last_idx = page->index;
Chao Yu29b96b52016-02-06 14:38:29 +0800410 }
Chao Yu28bc1062016-02-06 14:40:34 +0800411 unlock_page(page);
Chao Yucf52b272018-04-23 10:36:14 +0800412 list_move_tail(&cur->list, &revoke_list);
Chao Yu29b96b52016-02-06 14:38:29 +0800413 }
414
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800415 if (last_idx != ULONG_MAX)
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700416 f2fs_submit_merged_write_cond(sbi, inode, 0, last_idx, DATA);
Chao Yu28bc1062016-02-06 14:40:34 +0800417
Chao Yu28bc1062016-02-06 14:40:34 +0800418 if (err) {
Chao Yu28bc1062016-02-06 14:40:34 +0800419 /*
420 * try to revoke all committed pages, but still we could fail
421 * due to no memory or other reason, if that happened, EAGAIN
422 * will be returned, which means in such case, transaction is
423 * already not integrity, caller should use journal to do the
424 * recovery or rewrite & commit last transaction. For other
425 * error number, revoking was done by filesystem itself.
426 */
Chao Yucf52b272018-04-23 10:36:14 +0800427 err = __revoke_inmem_pages(inode, &revoke_list, false, true);
Chao Yu28bc1062016-02-06 14:40:34 +0800428
429 /* drop all uncommitted pages */
430 __revoke_inmem_pages(inode, &fi->inmem_pages, true, false);
Chao Yucf52b272018-04-23 10:36:14 +0800431 } else {
432 __revoke_inmem_pages(inode, &revoke_list, false, false);
Chao Yu28bc1062016-02-06 14:40:34 +0800433 }
Chao Yucf52b272018-04-23 10:36:14 +0800434
435 return err;
436}
437
Chao Yu4d57b862018-05-30 00:20:41 +0800438int f2fs_commit_inmem_pages(struct inode *inode)
Chao Yucf52b272018-04-23 10:36:14 +0800439{
440 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
441 struct f2fs_inode_info *fi = F2FS_I(inode);
442 int err;
443
444 f2fs_balance_fs(sbi, true);
Chao Yucf52b272018-04-23 10:36:14 +0800445
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +0900446 down_write(&fi->i_gc_rwsem[WRITE]);
447
448 f2fs_lock_op(sbi);
Chao Yucf52b272018-04-23 10:36:14 +0800449 set_inode_flag(inode, FI_ATOMIC_COMMIT);
450
451 mutex_lock(&fi->inmem_lock);
Chao Yu4d57b862018-05-30 00:20:41 +0800452 err = __f2fs_commit_inmem_pages(inode);
Chao Yucf52b272018-04-23 10:36:14 +0800453
Jaegeuk Kim57864ae2017-10-18 19:05:57 -0700454 spin_lock(&sbi->inode_lock[ATOMIC_FILE]);
455 if (!list_empty(&fi->inmem_ilist))
456 list_del_init(&fi->inmem_ilist);
457 spin_unlock(&sbi->inode_lock[ATOMIC_FILE]);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700458 mutex_unlock(&fi->inmem_lock);
459
Chao Yu5fe45742017-01-07 18:50:26 +0800460 clear_inode_flag(inode, FI_ATOMIC_COMMIT);
461
Chao Yu29b96b52016-02-06 14:38:29 +0800462 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +0900463 up_write(&fi->i_gc_rwsem[WRITE]);
464
Jaegeuk Kimedb27de2015-07-25 00:52:52 -0700465 return err;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700466}
467
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900468/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900469 * This function balances dirty node and dentry pages.
470 * In addition, it controls garbage collection.
471 */
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800472void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900473{
Chao Yu55523512017-02-25 11:08:28 +0800474 if (time_to_inject(sbi, FAULT_CHECKPOINT)) {
475 f2fs_show_injection_info(FAULT_CHECKPOINT);
Chao Yu0f348022016-09-26 19:45:55 +0800476 f2fs_stop_checkpoint(sbi, false);
Chao Yu55523512017-02-25 11:08:28 +0800477 }
Chao Yu0f348022016-09-26 19:45:55 +0800478
Jaegeuk Kime589c2c2016-06-02 15:24:24 -0700479 /* balance_fs_bg is able to be pending */
Jaegeuk Kima7881892017-04-20 13:51:57 -0700480 if (need && excess_cached_nats(sbi))
Jaegeuk Kime589c2c2016-06-02 15:24:24 -0700481 f2fs_balance_fs_bg(sbi);
482
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900483 /*
Jaegeuk Kim029cd282012-12-21 17:20:21 +0900484 * We should do GC or end up with checkpoint, if there are so many dirty
485 * dir/node pages without enough free segments.
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900486 */
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -0700487 if (has_not_enough_free_secs(sbi, 0, 0)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900488 mutex_lock(&sbi->gc_mutex);
Jaegeuk Kime066b832017-04-13 15:17:00 -0700489 f2fs_gc(sbi, false, false, NULL_SEGNO);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900490 }
491}
492
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900493void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi)
494{
Chao Yu64c74a72018-05-26 18:03:34 +0800495 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
496 return;
497
Chao Yu1dcc3362015-02-05 17:57:31 +0800498 /* try to shrink extent cache when there is no enough memory */
Chao Yu4d57b862018-05-30 00:20:41 +0800499 if (!f2fs_available_free_memory(sbi, EXTENT_CACHE))
Jaegeuk Kim554df792015-06-19 13:41:23 -0700500 f2fs_shrink_extent_tree(sbi, EXTENT_CACHE_SHRINK_NUMBER);
Chao Yu1dcc3362015-02-05 17:57:31 +0800501
Jaegeuk Kim1b38dc82015-06-19 15:36:07 -0700502 /* check the # of cached NAT entries */
Chao Yu4d57b862018-05-30 00:20:41 +0800503 if (!f2fs_available_free_memory(sbi, NAT_ENTRIES))
504 f2fs_try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK);
Jaegeuk Kim1b38dc82015-06-19 15:36:07 -0700505
Chao Yu4d57b862018-05-30 00:20:41 +0800506 if (!f2fs_available_free_memory(sbi, FREE_NIDS))
507 f2fs_try_to_free_nids(sbi, MAX_FREE_NIDS);
Jaegeuk Kimad4edb82016-06-16 16:41:49 -0700508 else
Chao Yu4d57b862018-05-30 00:20:41 +0800509 f2fs_build_free_nids(sbi, false, false);
Chao Yu31696582015-07-28 18:33:46 +0800510
Chao Yufd8c8ca2018-07-25 19:16:21 +0800511 if (!is_idle(sbi) &&
512 (!excess_dirty_nats(sbi) && !excess_dirty_nodes(sbi)))
Jaegeuk Kimf455c8a2016-12-05 11:37:14 -0800513 return;
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -0800514
Jaegeuk Kim88a70a62014-12-10 15:20:48 -0800515 /* checkpoint is the only way to shrink partial cached entries */
Chao Yu4d57b862018-05-30 00:20:41 +0800516 if (!f2fs_available_free_memory(sbi, NAT_ENTRIES) ||
517 !f2fs_available_free_memory(sbi, INO_ENTRIES) ||
Chao Yu7d768d22016-01-18 18:31:18 +0800518 excess_prefree_segs(sbi) ||
519 excess_dirty_nats(sbi) ||
Chao Yufd8c8ca2018-07-25 19:16:21 +0800520 excess_dirty_nodes(sbi) ||
Jaegeuk Kimf455c8a2016-12-05 11:37:14 -0800521 f2fs_time_over(sbi, CP_TIME)) {
Chao Yue9f5b8b2016-02-14 18:54:33 +0800522 if (test_opt(sbi, DATA_FLUSH)) {
523 struct blk_plug plug;
524
525 blk_start_plug(&plug);
Chao Yu4d57b862018-05-30 00:20:41 +0800526 f2fs_sync_dirty_inodes(sbi, FILE_INODE);
Chao Yue9f5b8b2016-02-14 18:54:33 +0800527 blk_finish_plug(&plug);
528 }
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900529 f2fs_sync_fs(sbi->sb, true);
Jaegeuk Kim42190d22016-01-09 13:45:17 -0800530 stat_inc_bg_cp_count(sbi->stat_info);
Chao Yu36b35a02015-12-17 17:13:28 +0800531 }
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900532}
533
Kinglong Mee20fda56b2017-03-04 22:13:10 +0800534static int __submit_flush_wait(struct f2fs_sb_info *sbi,
535 struct block_device *bdev)
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700536{
Chao Yud62fe972017-10-28 16:52:31 +0800537 struct bio *bio = f2fs_bio_alloc(sbi, 0, true);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700538 int ret;
539
Jan Kara3adc5fcb2017-05-02 17:03:47 +0200540 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200541 bio_set_dev(bio, bdev);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700542 ret = submit_bio_wait(bio);
543 bio_put(bio);
Kinglong Mee20fda56b2017-03-04 22:13:10 +0800544
545 trace_f2fs_issue_flush(bdev, test_opt(sbi, NOBARRIER),
546 test_opt(sbi, FLUSH_MERGE), ret);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700547 return ret;
548}
549
Chao Yu39d787b2017-09-29 13:59:38 +0800550static int submit_flush_wait(struct f2fs_sb_info *sbi, nid_t ino)
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700551{
Chao Yu39d787b2017-09-29 13:59:38 +0800552 int ret = 0;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700553 int i;
554
Chao Yu39d787b2017-09-29 13:59:38 +0800555 if (!sbi->s_ndevs)
556 return __submit_flush_wait(sbi, sbi->sb->s_bdev);
Kinglong Mee20fda56b2017-03-04 22:13:10 +0800557
Chao Yu39d787b2017-09-29 13:59:38 +0800558 for (i = 0; i < sbi->s_ndevs; i++) {
Chao Yu4d57b862018-05-30 00:20:41 +0800559 if (!f2fs_is_dirty_device(sbi, ino, i, FLUSH_INO))
Chao Yu39d787b2017-09-29 13:59:38 +0800560 continue;
Kinglong Mee20fda56b2017-03-04 22:13:10 +0800561 ret = __submit_flush_wait(sbi, FDEV(i).bdev);
562 if (ret)
563 break;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700564 }
565 return ret;
566}
567
Gu Zheng2163d192014-04-27 14:21:33 +0800568static int issue_flush_thread(void *data)
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900569{
570 struct f2fs_sb_info *sbi = data;
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800571 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800572 wait_queue_head_t *q = &fcc->flush_wait_queue;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900573repeat:
574 if (kthread_should_stop())
575 return 0;
576
Chao Yudc6febb2017-07-22 08:52:23 +0800577 sb_start_intwrite(sbi->sb);
578
Gu Zheng721bd4d2014-09-05 18:31:00 +0800579 if (!llist_empty(&fcc->issue_list)) {
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900580 struct flush_cmd *cmd, *next;
581 int ret;
582
Gu Zheng721bd4d2014-09-05 18:31:00 +0800583 fcc->dispatch_list = llist_del_all(&fcc->issue_list);
584 fcc->dispatch_list = llist_reverse_order(fcc->dispatch_list);
585
Chao Yu39d787b2017-09-29 13:59:38 +0800586 cmd = llist_entry(fcc->dispatch_list, struct flush_cmd, llnode);
587
588 ret = submit_flush_wait(sbi, cmd->ino);
Chao Yu8b8dd652017-03-25 17:19:58 +0800589 atomic_inc(&fcc->issued_flush);
590
Gu Zheng721bd4d2014-09-05 18:31:00 +0800591 llist_for_each_entry_safe(cmd, next,
592 fcc->dispatch_list, llnode) {
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900593 cmd->ret = ret;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900594 complete(&cmd->wait);
595 }
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800596 fcc->dispatch_list = NULL;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900597 }
598
Chao Yudc6febb2017-07-22 08:52:23 +0800599 sb_end_intwrite(sbi->sb);
600
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800601 wait_event_interruptible(*q,
Gu Zheng721bd4d2014-09-05 18:31:00 +0800602 kthread_should_stop() || !llist_empty(&fcc->issue_list));
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900603 goto repeat;
604}
605
Chao Yu39d787b2017-09-29 13:59:38 +0800606int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino)
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900607{
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800608 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
Chao Yuadf8d902014-05-08 17:00:35 +0800609 struct flush_cmd cmd;
Chao Yu8b8dd652017-03-25 17:19:58 +0800610 int ret;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900611
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700612 if (test_opt(sbi, NOBARRIER))
613 return 0;
614
Chao Yu8b8dd652017-03-25 17:19:58 +0800615 if (!test_opt(sbi, FLUSH_MERGE)) {
Chao Yu39d787b2017-09-29 13:59:38 +0800616 ret = submit_flush_wait(sbi, ino);
Chao Yu8b8dd652017-03-25 17:19:58 +0800617 atomic_inc(&fcc->issued_flush);
618 return ret;
619 }
620
Chao Yu39d787b2017-09-29 13:59:38 +0800621 if (atomic_inc_return(&fcc->issing_flush) == 1 || sbi->s_ndevs > 1) {
622 ret = submit_flush_wait(sbi, ino);
Chao Yu8b8dd652017-03-25 17:19:58 +0800623 atomic_dec(&fcc->issing_flush);
624
625 atomic_inc(&fcc->issued_flush);
Jaegeuk Kim740432f2015-08-14 11:43:56 -0700626 return ret;
627 }
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900628
Chao Yu39d787b2017-09-29 13:59:38 +0800629 cmd.ino = ino;
Chao Yuadf8d902014-05-08 17:00:35 +0800630 init_completion(&cmd.wait);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900631
Gu Zheng721bd4d2014-09-05 18:31:00 +0800632 llist_add(&cmd.llnode, &fcc->issue_list);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900633
Chao Yu6f890df2017-08-21 22:53:45 +0800634 /* update issue_list before we wake up issue_flush thread */
635 smp_mb();
636
637 if (waitqueue_active(&fcc->flush_wait_queue))
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800638 wake_up(&fcc->flush_wait_queue);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900639
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800640 if (fcc->f2fs_issue_flush) {
641 wait_for_completion(&cmd.wait);
Chao Yu8b8dd652017-03-25 17:19:58 +0800642 atomic_dec(&fcc->issing_flush);
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800643 } else {
Chao Yud3238692017-08-31 18:56:06 +0800644 struct llist_node *list;
645
646 list = llist_del_all(&fcc->issue_list);
647 if (!list) {
648 wait_for_completion(&cmd.wait);
649 atomic_dec(&fcc->issing_flush);
650 } else {
651 struct flush_cmd *tmp, *next;
652
Chao Yu39d787b2017-09-29 13:59:38 +0800653 ret = submit_flush_wait(sbi, ino);
Chao Yud3238692017-08-31 18:56:06 +0800654
655 llist_for_each_entry_safe(tmp, next, list, llnode) {
656 if (tmp == &cmd) {
657 cmd.ret = ret;
658 atomic_dec(&fcc->issing_flush);
659 continue;
660 }
661 tmp->ret = ret;
662 complete(&tmp->wait);
663 }
664 }
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800665 }
Chao Yuadf8d902014-05-08 17:00:35 +0800666
667 return cmd.ret;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900668}
669
Chao Yu4d57b862018-05-30 00:20:41 +0800670int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi)
Gu Zheng2163d192014-04-27 14:21:33 +0800671{
672 dev_t dev = sbi->sb->s_bdev->bd_dev;
673 struct flush_cmd_control *fcc;
674 int err = 0;
675
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800676 if (SM_I(sbi)->fcc_info) {
677 fcc = SM_I(sbi)->fcc_info;
Yunlong Songd871cd02017-06-24 15:57:19 +0800678 if (fcc->f2fs_issue_flush)
679 return err;
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800680 goto init_thread;
681 }
682
Chao Yuacbf0542017-11-30 19:28:17 +0800683 fcc = f2fs_kzalloc(sbi, sizeof(struct flush_cmd_control), GFP_KERNEL);
Gu Zheng2163d192014-04-27 14:21:33 +0800684 if (!fcc)
685 return -ENOMEM;
Chao Yu8b8dd652017-03-25 17:19:58 +0800686 atomic_set(&fcc->issued_flush, 0);
687 atomic_set(&fcc->issing_flush, 0);
Gu Zheng2163d192014-04-27 14:21:33 +0800688 init_waitqueue_head(&fcc->flush_wait_queue);
Gu Zheng721bd4d2014-09-05 18:31:00 +0800689 init_llist_head(&fcc->issue_list);
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800690 SM_I(sbi)->fcc_info = fcc;
Yunlei Hed4fdf8b2017-06-01 16:43:51 +0800691 if (!test_opt(sbi, FLUSH_MERGE))
692 return err;
693
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800694init_thread:
Gu Zheng2163d192014-04-27 14:21:33 +0800695 fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi,
696 "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev));
697 if (IS_ERR(fcc->f2fs_issue_flush)) {
698 err = PTR_ERR(fcc->f2fs_issue_flush);
699 kfree(fcc);
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800700 SM_I(sbi)->fcc_info = NULL;
Gu Zheng2163d192014-04-27 14:21:33 +0800701 return err;
702 }
Gu Zheng2163d192014-04-27 14:21:33 +0800703
704 return err;
705}
706
Chao Yu4d57b862018-05-30 00:20:41 +0800707void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free)
Gu Zheng2163d192014-04-27 14:21:33 +0800708{
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800709 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
Gu Zheng2163d192014-04-27 14:21:33 +0800710
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800711 if (fcc && fcc->f2fs_issue_flush) {
712 struct task_struct *flush_thread = fcc->f2fs_issue_flush;
713
714 fcc->f2fs_issue_flush = NULL;
715 kthread_stop(flush_thread);
716 }
717 if (free) {
718 kfree(fcc);
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800719 SM_I(sbi)->fcc_info = NULL;
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800720 }
Gu Zheng2163d192014-04-27 14:21:33 +0800721}
722
Chao Yu1228b482017-09-29 13:59:39 +0800723int f2fs_flush_device_cache(struct f2fs_sb_info *sbi)
724{
725 int ret = 0, i;
726
727 if (!sbi->s_ndevs)
728 return 0;
729
730 for (i = 1; i < sbi->s_ndevs; i++) {
731 if (!f2fs_test_bit(i, (char *)&sbi->dirty_device))
732 continue;
733 ret = __submit_flush_wait(sbi, FDEV(i).bdev);
734 if (ret)
735 break;
736
737 spin_lock(&sbi->dev_lock);
738 f2fs_clear_bit(i, (char *)&sbi->dirty_device);
739 spin_unlock(&sbi->dev_lock);
740 }
741
742 return ret;
743}
744
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900745static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
746 enum dirty_type dirty_type)
747{
748 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
749
750 /* need not be added */
751 if (IS_CURSEG(sbi, segno))
752 return;
753
754 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
755 dirty_i->nr_dirty[dirty_type]++;
756
757 if (dirty_type == DIRTY) {
758 struct seg_entry *sentry = get_seg_entry(sbi, segno);
Changman Lee4625d6a2013-10-25 17:31:57 +0900759 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900760
Jaegeuk Kimec325b52014-09-02 16:24:11 -0700761 if (unlikely(t >= DIRTY)) {
762 f2fs_bug_on(sbi, 1);
763 return;
764 }
Changman Lee4625d6a2013-10-25 17:31:57 +0900765 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
766 dirty_i->nr_dirty[t]++;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900767 }
768}
769
770static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
771 enum dirty_type dirty_type)
772{
773 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
774
775 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
776 dirty_i->nr_dirty[dirty_type]--;
777
778 if (dirty_type == DIRTY) {
Changman Lee4625d6a2013-10-25 17:31:57 +0900779 struct seg_entry *sentry = get_seg_entry(sbi, segno);
780 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900781
Changman Lee4625d6a2013-10-25 17:31:57 +0900782 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
783 dirty_i->nr_dirty[t]--;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900784
Jaegeuk Kim302bd342017-04-07 14:33:22 -0700785 if (get_valid_blocks(sbi, segno, true) == 0)
Jaegeuk Kim4ddb1a42017-04-07 15:08:17 -0700786 clear_bit(GET_SEC_FROM_SEG(sbi, segno),
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +0900787 dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900788 }
789}
790
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900791/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900792 * Should not occur error such as -ENOMEM.
793 * Adding dirty entry into seglist is not critical operation.
794 * If a given segment is one of current working segments, it won't be added.
795 */
Haicheng Li8d8451a2013-06-13 16:59:28 +0800796static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900797{
798 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
799 unsigned short valid_blocks;
800
801 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
802 return;
803
804 mutex_lock(&dirty_i->seglist_lock);
805
Jaegeuk Kim302bd342017-04-07 14:33:22 -0700806 valid_blocks = get_valid_blocks(sbi, segno, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900807
808 if (valid_blocks == 0) {
809 __locate_dirty_segment(sbi, segno, PRE);
810 __remove_dirty_segment(sbi, segno, DIRTY);
811 } else if (valid_blocks < sbi->blocks_per_seg) {
812 __locate_dirty_segment(sbi, segno, DIRTY);
813 } else {
814 /* Recovery routine with SSR needs this */
815 __remove_dirty_segment(sbi, segno, DIRTY);
816 }
817
818 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900819}
820
Chao Yu004b6862017-04-14 23:24:55 +0800821static struct discard_cmd *__create_discard_cmd(struct f2fs_sb_info *sbi,
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800822 struct block_device *bdev, block_t lstart,
823 block_t start, block_t len)
Chao Yu275b66b2016-08-29 23:58:34 +0800824{
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -0800825 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Chao Yuba48a332017-04-15 14:09:37 +0800826 struct list_head *pend_list;
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800827 struct discard_cmd *dc;
Chao Yu275b66b2016-08-29 23:58:34 +0800828
Chao Yuba48a332017-04-15 14:09:37 +0800829 f2fs_bug_on(sbi, !len);
830
831 pend_list = &dcc->pend_list[plist_idx(len)];
832
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800833 dc = f2fs_kmem_cache_alloc(discard_cmd_slab, GFP_NOFS);
834 INIT_LIST_HEAD(&dc->list);
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800835 dc->bdev = bdev;
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800836 dc->lstart = lstart;
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800837 dc->start = start;
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800838 dc->len = len;
Chao Yuec9895a2017-04-26 17:39:54 +0800839 dc->ref = 0;
Jaegeuk Kim15469962017-01-09 20:32:07 -0800840 dc->state = D_PREP;
Chao Yu35ec7d52018-08-06 22:43:50 +0800841 dc->issuing = 0;
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800842 dc->error = 0;
Jaegeuk Kimb01a9202017-01-09 14:13:03 -0800843 init_completion(&dc->wait);
Chao Yu22d375d2017-04-05 18:19:48 +0800844 list_add_tail(&dc->list, pend_list);
Chao Yu35ec7d52018-08-06 22:43:50 +0800845 spin_lock_init(&dc->lock);
846 dc->bio_ref = 0;
Chao Yu5f323662017-03-25 17:19:59 +0800847 atomic_inc(&dcc->discard_cmd_cnt);
Chao Yud84d1cb2017-04-18 19:27:39 +0800848 dcc->undiscard_blks += len;
Chao Yu004b6862017-04-14 23:24:55 +0800849
850 return dc;
Jaegeuk Kim15469962017-01-09 20:32:07 -0800851}
852
Chao Yu004b6862017-04-14 23:24:55 +0800853static struct discard_cmd *__attach_discard_cmd(struct f2fs_sb_info *sbi,
854 struct block_device *bdev, block_t lstart,
855 block_t start, block_t len,
856 struct rb_node *parent, struct rb_node **p)
857{
858 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
859 struct discard_cmd *dc;
860
861 dc = __create_discard_cmd(sbi, bdev, lstart, start, len);
862
863 rb_link_node(&dc->rb_node, parent, p);
864 rb_insert_color(&dc->rb_node, &dcc->root);
865
866 return dc;
867}
868
869static void __detach_discard_cmd(struct discard_cmd_control *dcc,
870 struct discard_cmd *dc)
Jaegeuk Kim15469962017-01-09 20:32:07 -0800871{
Jaegeuk Kimdcc91652017-01-11 10:20:04 -0800872 if (dc->state == D_DONE)
Chao Yu35ec7d52018-08-06 22:43:50 +0800873 atomic_sub(dc->issuing, &dcc->issing_discard);
Chao Yu004b6862017-04-14 23:24:55 +0800874
875 list_del(&dc->list);
876 rb_erase(&dc->rb_node, &dcc->root);
Chao Yud84d1cb2017-04-18 19:27:39 +0800877 dcc->undiscard_blks -= dc->len;
Chao Yu004b6862017-04-14 23:24:55 +0800878
879 kmem_cache_free(discard_cmd_slab, dc);
880
881 atomic_dec(&dcc->discard_cmd_cnt);
882}
883
884static void __remove_discard_cmd(struct f2fs_sb_info *sbi,
885 struct discard_cmd *dc)
886{
887 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Chao Yu35ec7d52018-08-06 22:43:50 +0800888 unsigned long flags;
Jaegeuk Kimdcc91652017-01-11 10:20:04 -0800889
Chao Yu2ec6f2e2017-10-04 09:08:36 +0800890 trace_f2fs_remove_discard(dc->bdev, dc->start, dc->len);
891
Chao Yu35ec7d52018-08-06 22:43:50 +0800892 spin_lock_irqsave(&dc->lock, flags);
893 if (dc->bio_ref) {
894 spin_unlock_irqrestore(&dc->lock, flags);
895 return;
896 }
897 spin_unlock_irqrestore(&dc->lock, flags);
898
Chao Yud9703d92017-06-05 18:29:07 +0800899 f2fs_bug_on(sbi, dc->ref);
900
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800901 if (dc->error == -EOPNOTSUPP)
902 dc->error = 0;
Jaegeuk Kim15469962017-01-09 20:32:07 -0800903
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800904 if (dc->error)
Chao Yu22d7ea12018-08-22 17:17:47 +0800905 printk_ratelimited(
906 "%sF2FS-fs: Issue discard(%u, %u, %u) failed, ret: %d",
907 KERN_INFO, dc->lstart, dc->start, dc->len, dc->error);
Chao Yu004b6862017-04-14 23:24:55 +0800908 __detach_discard_cmd(dcc, dc);
Chao Yu275b66b2016-08-29 23:58:34 +0800909}
910
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800911static void f2fs_submit_discard_endio(struct bio *bio)
912{
913 struct discard_cmd *dc = (struct discard_cmd *)bio->bi_private;
Chao Yu35ec7d52018-08-06 22:43:50 +0800914 unsigned long flags;
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800915
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200916 dc->error = blk_status_to_errno(bio->bi_status);
Chao Yu35ec7d52018-08-06 22:43:50 +0800917
918 spin_lock_irqsave(&dc->lock, flags);
919 dc->bio_ref--;
920 if (!dc->bio_ref && dc->state == D_SUBMIT) {
921 dc->state = D_DONE;
922 complete_all(&dc->wait);
923 }
924 spin_unlock_irqrestore(&dc->lock, flags);
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800925 bio_put(bio);
926}
927
Wei Yongjun94b1e102018-01-05 09:41:20 +0000928static void __check_sit_bitmap(struct f2fs_sb_info *sbi,
Chao Yu6915ea92017-06-30 17:19:02 +0800929 block_t start, block_t end)
930{
931#ifdef CONFIG_F2FS_CHECK_FS
932 struct seg_entry *sentry;
933 unsigned int segno;
934 block_t blk = start;
935 unsigned long offset, size, max_blocks = sbi->blocks_per_seg;
936 unsigned long *map;
937
938 while (blk < end) {
939 segno = GET_SEGNO(sbi, blk);
940 sentry = get_seg_entry(sbi, segno);
941 offset = GET_BLKOFF_FROM_SEG0(sbi, blk);
942
Yunlong Song008396e2017-08-04 17:07:15 +0800943 if (end < START_BLOCK(sbi, segno + 1))
944 size = GET_BLKOFF_FROM_SEG0(sbi, end);
945 else
946 size = max_blocks;
Chao Yu6915ea92017-06-30 17:19:02 +0800947 map = (unsigned long *)(sentry->cur_valid_map);
948 offset = __find_rev_next_bit(map, size, offset);
949 f2fs_bug_on(sbi, offset != size);
Yunlong Song008396e2017-08-04 17:07:15 +0800950 blk = START_BLOCK(sbi, segno + 1);
Chao Yu6915ea92017-06-30 17:19:02 +0800951 }
952#endif
953}
954
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -0700955static void __init_discard_policy(struct f2fs_sb_info *sbi,
956 struct discard_policy *dpolicy,
957 int discard_type, unsigned int granularity)
958{
959 /* common policy */
960 dpolicy->type = discard_type;
961 dpolicy->sync = true;
Chao Yu20ee4382018-07-08 22:11:01 +0800962 dpolicy->ordered = false;
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -0700963 dpolicy->granularity = granularity;
964
965 dpolicy->max_requests = DEF_MAX_DISCARD_REQUEST;
966 dpolicy->io_aware_gran = MAX_PLIST_NUM;
967
968 if (discard_type == DPOLICY_BG) {
969 dpolicy->min_interval = DEF_MIN_DISCARD_ISSUE_TIME;
Yunlei Hef9d1dce2018-04-08 15:11:11 +0800970 dpolicy->mid_interval = DEF_MID_DISCARD_ISSUE_TIME;
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -0700971 dpolicy->max_interval = DEF_MAX_DISCARD_ISSUE_TIME;
972 dpolicy->io_aware = true;
Chao Yucba60842018-04-10 15:43:09 +0800973 dpolicy->sync = false;
Chao Yu20ee4382018-07-08 22:11:01 +0800974 dpolicy->ordered = true;
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -0700975 if (utilization(sbi) > DEF_DISCARD_URGENT_UTIL) {
976 dpolicy->granularity = 1;
977 dpolicy->max_interval = DEF_MIN_DISCARD_ISSUE_TIME;
978 }
979 } else if (discard_type == DPOLICY_FORCE) {
980 dpolicy->min_interval = DEF_MIN_DISCARD_ISSUE_TIME;
Yunlei Hef9d1dce2018-04-08 15:11:11 +0800981 dpolicy->mid_interval = DEF_MID_DISCARD_ISSUE_TIME;
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -0700982 dpolicy->max_interval = DEF_MAX_DISCARD_ISSUE_TIME;
983 dpolicy->io_aware = false;
984 } else if (discard_type == DPOLICY_FSTRIM) {
985 dpolicy->io_aware = false;
986 } else if (discard_type == DPOLICY_UMOUNT) {
Yunlei He241b4932018-04-04 17:29:05 +0800987 dpolicy->max_requests = UINT_MAX;
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -0700988 dpolicy->io_aware = false;
989 }
990}
991
Chao Yu35ec7d52018-08-06 22:43:50 +0800992static void __update_discard_tree_range(struct f2fs_sb_info *sbi,
993 struct block_device *bdev, block_t lstart,
994 block_t start, block_t len);
Jaegeuk Kimc81abe32017-03-07 18:02:02 -0800995/* this function is copied from blkdev_issue_discard from block/blk-lib.c */
Chao Yu6b9cb122018-08-08 10:14:55 +0800996static int __submit_discard_cmd(struct f2fs_sb_info *sbi,
Chao Yu78997b52017-10-04 09:08:34 +0800997 struct discard_policy *dpolicy,
Chao Yu35ec7d52018-08-06 22:43:50 +0800998 struct discard_cmd *dc,
999 unsigned int *issued)
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001000{
Chao Yu35ec7d52018-08-06 22:43:50 +08001001 struct block_device *bdev = dc->bdev;
1002 struct request_queue *q = bdev_get_queue(bdev);
1003 unsigned int max_discard_blocks =
1004 SECTOR_TO_BLOCK(q->limits.max_discard_sectors);
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001005 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Chao Yu78997b52017-10-04 09:08:34 +08001006 struct list_head *wait_list = (dpolicy->type == DPOLICY_FSTRIM) ?
1007 &(dcc->fstrim_list) : &(dcc->wait_list);
Chao Yu78997b52017-10-04 09:08:34 +08001008 int flag = dpolicy->sync ? REQ_SYNC : 0;
Chao Yu35ec7d52018-08-06 22:43:50 +08001009 block_t lstart, start, len, total_len;
1010 int err = 0;
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001011
1012 if (dc->state != D_PREP)
Chao Yu6b9cb122018-08-08 10:14:55 +08001013 return 0;
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001014
Yunlei Hed6184772018-04-13 11:08:05 +08001015 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK))
Chao Yu6b9cb122018-08-08 10:14:55 +08001016 return 0;
Yunlei Hed6184772018-04-13 11:08:05 +08001017
Chao Yu35ec7d52018-08-06 22:43:50 +08001018 trace_f2fs_issue_discard(bdev, dc->start, dc->len);
Chao Yu0243a5f2017-04-15 14:09:38 +08001019
Chao Yu35ec7d52018-08-06 22:43:50 +08001020 lstart = dc->lstart;
1021 start = dc->start;
1022 len = dc->len;
1023 total_len = len;
1024
1025 dc->len = 0;
1026
1027 while (total_len && *issued < dpolicy->max_requests && !err) {
1028 struct bio *bio = NULL;
1029 unsigned long flags;
1030 bool last = true;
1031
1032 if (len > max_discard_blocks) {
1033 len = max_discard_blocks;
1034 last = false;
1035 }
1036
1037 (*issued)++;
1038 if (*issued == dpolicy->max_requests)
1039 last = true;
1040
1041 dc->len += len;
1042
Chao Yub83dcfe2018-08-06 20:30:18 +08001043 if (time_to_inject(sbi, FAULT_DISCARD)) {
1044 f2fs_show_injection_info(FAULT_DISCARD);
1045 err = -EIO;
1046 goto submit;
1047 }
Chao Yu35ec7d52018-08-06 22:43:50 +08001048 err = __blkdev_issue_discard(bdev,
1049 SECTOR_FROM_BLOCK(start),
1050 SECTOR_FROM_BLOCK(len),
1051 GFP_NOFS, 0, &bio);
Chao Yub83dcfe2018-08-06 20:30:18 +08001052submit:
Chao Yu6b9cb122018-08-08 10:14:55 +08001053 if (err) {
Chao Yu35ec7d52018-08-06 22:43:50 +08001054 spin_lock_irqsave(&dc->lock, flags);
1055 if (dc->state == D_PARTIAL)
1056 dc->state = D_SUBMIT;
1057 spin_unlock_irqrestore(&dc->lock, flags);
1058
Chao Yu6b9cb122018-08-08 10:14:55 +08001059 break;
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001060 }
Chao Yu35ec7d52018-08-06 22:43:50 +08001061
Chao Yu6b9cb122018-08-08 10:14:55 +08001062 f2fs_bug_on(sbi, !bio);
1063
1064 /*
1065 * should keep before submission to avoid D_DONE
1066 * right away
1067 */
1068 spin_lock_irqsave(&dc->lock, flags);
1069 if (last)
1070 dc->state = D_SUBMIT;
1071 else
1072 dc->state = D_PARTIAL;
1073 dc->bio_ref++;
1074 spin_unlock_irqrestore(&dc->lock, flags);
1075
1076 atomic_inc(&dcc->issing_discard);
1077 dc->issuing++;
1078 list_move_tail(&dc->list, wait_list);
1079
1080 /* sanity check on discard range */
1081 __check_sit_bitmap(sbi, start, start + len);
1082
1083 bio->bi_private = dc;
1084 bio->bi_end_io = f2fs_submit_discard_endio;
1085 bio->bi_opf |= flag;
1086 submit_bio(bio);
1087
1088 atomic_inc(&dcc->issued_discard);
1089
1090 f2fs_update_iostat(sbi, FS_DISCARD, 1);
1091
Chao Yu35ec7d52018-08-06 22:43:50 +08001092 lstart += len;
1093 start += len;
1094 total_len -= len;
1095 len = total_len;
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001096 }
Chao Yu35ec7d52018-08-06 22:43:50 +08001097
Chao Yu6b9cb122018-08-08 10:14:55 +08001098 if (!err && len)
Chao Yu35ec7d52018-08-06 22:43:50 +08001099 __update_discard_tree_range(sbi, bdev, lstart, start, len);
Chao Yu6b9cb122018-08-08 10:14:55 +08001100 return err;
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001101}
1102
Chao Yu004b6862017-04-14 23:24:55 +08001103static struct discard_cmd *__insert_discard_tree(struct f2fs_sb_info *sbi,
1104 struct block_device *bdev, block_t lstart,
1105 block_t start, block_t len,
1106 struct rb_node **insert_p,
1107 struct rb_node *insert_parent)
1108{
1109 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Colin Ian Kingdca69512017-10-19 12:58:21 +02001110 struct rb_node **p;
Chao Yu004b6862017-04-14 23:24:55 +08001111 struct rb_node *parent = NULL;
1112 struct discard_cmd *dc = NULL;
1113
1114 if (insert_p && insert_parent) {
1115 parent = insert_parent;
1116 p = insert_p;
1117 goto do_insert;
1118 }
1119
Chao Yu4d57b862018-05-30 00:20:41 +08001120 p = f2fs_lookup_rb_tree_for_insert(sbi, &dcc->root, &parent, lstart);
Chao Yu004b6862017-04-14 23:24:55 +08001121do_insert:
1122 dc = __attach_discard_cmd(sbi, bdev, lstart, start, len, parent, p);
1123 if (!dc)
1124 return NULL;
1125
1126 return dc;
1127}
1128
Chao Yuba48a332017-04-15 14:09:37 +08001129static void __relocate_discard_cmd(struct discard_cmd_control *dcc,
1130 struct discard_cmd *dc)
1131{
1132 list_move_tail(&dc->list, &dcc->pend_list[plist_idx(dc->len)]);
1133}
1134
Chao Yu004b6862017-04-14 23:24:55 +08001135static void __punch_discard_cmd(struct f2fs_sb_info *sbi,
1136 struct discard_cmd *dc, block_t blkaddr)
1137{
Chao Yuba48a332017-04-15 14:09:37 +08001138 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Chao Yu004b6862017-04-14 23:24:55 +08001139 struct discard_info di = dc->di;
1140 bool modified = false;
1141
1142 if (dc->state == D_DONE || dc->len == 1) {
1143 __remove_discard_cmd(sbi, dc);
1144 return;
1145 }
1146
Chao Yud84d1cb2017-04-18 19:27:39 +08001147 dcc->undiscard_blks -= di.len;
1148
Chao Yu004b6862017-04-14 23:24:55 +08001149 if (blkaddr > di.lstart) {
1150 dc->len = blkaddr - dc->lstart;
Chao Yud84d1cb2017-04-18 19:27:39 +08001151 dcc->undiscard_blks += dc->len;
Chao Yuba48a332017-04-15 14:09:37 +08001152 __relocate_discard_cmd(dcc, dc);
Chao Yu004b6862017-04-14 23:24:55 +08001153 modified = true;
1154 }
1155
1156 if (blkaddr < di.lstart + di.len - 1) {
1157 if (modified) {
1158 __insert_discard_tree(sbi, dc->bdev, blkaddr + 1,
1159 di.start + blkaddr + 1 - di.lstart,
1160 di.lstart + di.len - 1 - blkaddr,
1161 NULL, NULL);
1162 } else {
1163 dc->lstart++;
1164 dc->len--;
1165 dc->start++;
Chao Yud84d1cb2017-04-18 19:27:39 +08001166 dcc->undiscard_blks += dc->len;
Chao Yuba48a332017-04-15 14:09:37 +08001167 __relocate_discard_cmd(dcc, dc);
Chao Yu004b6862017-04-14 23:24:55 +08001168 }
1169 }
1170}
1171
1172static void __update_discard_tree_range(struct f2fs_sb_info *sbi,
1173 struct block_device *bdev, block_t lstart,
1174 block_t start, block_t len)
1175{
1176 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1177 struct discard_cmd *prev_dc = NULL, *next_dc = NULL;
1178 struct discard_cmd *dc;
1179 struct discard_info di = {0};
1180 struct rb_node **insert_p = NULL, *insert_parent = NULL;
Chao Yu35ec7d52018-08-06 22:43:50 +08001181 struct request_queue *q = bdev_get_queue(bdev);
1182 unsigned int max_discard_blocks =
1183 SECTOR_TO_BLOCK(q->limits.max_discard_sectors);
Chao Yu004b6862017-04-14 23:24:55 +08001184 block_t end = lstart + len;
1185
Chao Yu4d57b862018-05-30 00:20:41 +08001186 dc = (struct discard_cmd *)f2fs_lookup_rb_tree_ret(&dcc->root,
Chao Yu004b6862017-04-14 23:24:55 +08001187 NULL, lstart,
1188 (struct rb_entry **)&prev_dc,
1189 (struct rb_entry **)&next_dc,
1190 &insert_p, &insert_parent, true);
1191 if (dc)
1192 prev_dc = dc;
1193
1194 if (!prev_dc) {
1195 di.lstart = lstart;
1196 di.len = next_dc ? next_dc->lstart - lstart : len;
1197 di.len = min(di.len, len);
1198 di.start = start;
1199 }
1200
1201 while (1) {
1202 struct rb_node *node;
1203 bool merged = false;
1204 struct discard_cmd *tdc = NULL;
1205
1206 if (prev_dc) {
1207 di.lstart = prev_dc->lstart + prev_dc->len;
1208 if (di.lstart < lstart)
1209 di.lstart = lstart;
1210 if (di.lstart >= end)
1211 break;
1212
1213 if (!next_dc || next_dc->lstart > end)
1214 di.len = end - di.lstart;
1215 else
1216 di.len = next_dc->lstart - di.lstart;
1217 di.start = start + di.lstart - lstart;
1218 }
1219
1220 if (!di.len)
1221 goto next;
1222
1223 if (prev_dc && prev_dc->state == D_PREP &&
1224 prev_dc->bdev == bdev &&
Chao Yu35ec7d52018-08-06 22:43:50 +08001225 __is_discard_back_mergeable(&di, &prev_dc->di,
1226 max_discard_blocks)) {
Chao Yu004b6862017-04-14 23:24:55 +08001227 prev_dc->di.len += di.len;
Chao Yud84d1cb2017-04-18 19:27:39 +08001228 dcc->undiscard_blks += di.len;
Chao Yuba48a332017-04-15 14:09:37 +08001229 __relocate_discard_cmd(dcc, prev_dc);
Chao Yu004b6862017-04-14 23:24:55 +08001230 di = prev_dc->di;
1231 tdc = prev_dc;
1232 merged = true;
1233 }
1234
1235 if (next_dc && next_dc->state == D_PREP &&
1236 next_dc->bdev == bdev &&
Chao Yu35ec7d52018-08-06 22:43:50 +08001237 __is_discard_front_mergeable(&di, &next_dc->di,
1238 max_discard_blocks)) {
Chao Yu004b6862017-04-14 23:24:55 +08001239 next_dc->di.lstart = di.lstart;
1240 next_dc->di.len += di.len;
1241 next_dc->di.start = di.start;
Chao Yud84d1cb2017-04-18 19:27:39 +08001242 dcc->undiscard_blks += di.len;
Chao Yuba48a332017-04-15 14:09:37 +08001243 __relocate_discard_cmd(dcc, next_dc);
Chao Yu004b6862017-04-14 23:24:55 +08001244 if (tdc)
1245 __remove_discard_cmd(sbi, tdc);
Chao Yu004b6862017-04-14 23:24:55 +08001246 merged = true;
1247 }
1248
Chao Yudf0f6b42017-04-17 18:21:43 +08001249 if (!merged) {
Chao Yu004b6862017-04-14 23:24:55 +08001250 __insert_discard_tree(sbi, bdev, di.lstart, di.start,
1251 di.len, NULL, NULL);
Chao Yudf0f6b42017-04-17 18:21:43 +08001252 }
Chao Yu004b6862017-04-14 23:24:55 +08001253 next:
1254 prev_dc = next_dc;
1255 if (!prev_dc)
1256 break;
1257
1258 node = rb_next(&prev_dc->rb_node);
1259 next_dc = rb_entry_safe(node, struct discard_cmd, rb_node);
1260 }
Chao Yu004b6862017-04-14 23:24:55 +08001261}
1262
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001263static int __queue_discard_cmd(struct f2fs_sb_info *sbi,
1264 struct block_device *bdev, block_t blkstart, block_t blklen)
1265{
1266 block_t lblkstart = blkstart;
1267
Chao Yu0243a5f2017-04-15 14:09:38 +08001268 trace_f2fs_queue_discard(bdev, blkstart, blklen);
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001269
1270 if (sbi->s_ndevs) {
1271 int devi = f2fs_target_device_index(sbi, blkstart);
1272
1273 blkstart -= FDEV(devi).start_blk;
1274 }
Chao Yu35ec7d52018-08-06 22:43:50 +08001275 mutex_lock(&SM_I(sbi)->dcc_info->cmd_lock);
Chao Yu004b6862017-04-14 23:24:55 +08001276 __update_discard_tree_range(sbi, bdev, lblkstart, blkstart, blklen);
Chao Yu35ec7d52018-08-06 22:43:50 +08001277 mutex_unlock(&SM_I(sbi)->dcc_info->cmd_lock);
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001278 return 0;
1279}
1280
Chao Yu20ee4382018-07-08 22:11:01 +08001281static unsigned int __issue_discard_cmd_orderly(struct f2fs_sb_info *sbi,
1282 struct discard_policy *dpolicy)
1283{
1284 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1285 struct discard_cmd *prev_dc = NULL, *next_dc = NULL;
1286 struct rb_node **insert_p = NULL, *insert_parent = NULL;
1287 struct discard_cmd *dc;
1288 struct blk_plug plug;
1289 unsigned int pos = dcc->next_pos;
1290 unsigned int issued = 0;
1291 bool io_interrupted = false;
1292
1293 mutex_lock(&dcc->cmd_lock);
1294 dc = (struct discard_cmd *)f2fs_lookup_rb_tree_ret(&dcc->root,
1295 NULL, pos,
1296 (struct rb_entry **)&prev_dc,
1297 (struct rb_entry **)&next_dc,
1298 &insert_p, &insert_parent, true);
1299 if (!dc)
1300 dc = next_dc;
1301
1302 blk_start_plug(&plug);
1303
1304 while (dc) {
1305 struct rb_node *node;
Chao Yu6b9cb122018-08-08 10:14:55 +08001306 int err = 0;
Chao Yu20ee4382018-07-08 22:11:01 +08001307
1308 if (dc->state != D_PREP)
1309 goto next;
1310
1311 if (dpolicy->io_aware && !is_idle(sbi)) {
1312 io_interrupted = true;
1313 break;
1314 }
1315
1316 dcc->next_pos = dc->lstart + dc->len;
Chao Yu6b9cb122018-08-08 10:14:55 +08001317 err = __submit_discard_cmd(sbi, dpolicy, dc, &issued);
Chao Yu20ee4382018-07-08 22:11:01 +08001318
Chao Yu35ec7d52018-08-06 22:43:50 +08001319 if (issued >= dpolicy->max_requests)
Chao Yu20ee4382018-07-08 22:11:01 +08001320 break;
1321next:
1322 node = rb_next(&dc->rb_node);
Chao Yu6b9cb122018-08-08 10:14:55 +08001323 if (err)
1324 __remove_discard_cmd(sbi, dc);
Chao Yu20ee4382018-07-08 22:11:01 +08001325 dc = rb_entry_safe(node, struct discard_cmd, rb_node);
1326 }
1327
1328 blk_finish_plug(&plug);
1329
1330 if (!dc)
1331 dcc->next_pos = 0;
1332
1333 mutex_unlock(&dcc->cmd_lock);
1334
1335 if (!issued && io_interrupted)
1336 issued = -1;
1337
1338 return issued;
1339}
1340
Chao Yu78997b52017-10-04 09:08:34 +08001341static int __issue_discard_cmd(struct f2fs_sb_info *sbi,
1342 struct discard_policy *dpolicy)
Chao Yubd5b0732017-04-25 20:21:37 +08001343{
1344 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1345 struct list_head *pend_list;
1346 struct discard_cmd *dc, *tmp;
1347 struct blk_plug plug;
Chao Yu522d1712018-07-08 22:08:09 +08001348 int i, issued = 0;
Chao Yue6c6de12017-09-12 21:35:12 +08001349 bool io_interrupted = false;
Chao Yubd5b0732017-04-25 20:21:37 +08001350
Chao Yu78997b52017-10-04 09:08:34 +08001351 for (i = MAX_PLIST_NUM - 1; i >= 0; i--) {
1352 if (i + 1 < dpolicy->granularity)
1353 break;
Chao Yu20ee4382018-07-08 22:11:01 +08001354
1355 if (i < DEFAULT_DISCARD_GRANULARITY && dpolicy->ordered)
1356 return __issue_discard_cmd_orderly(sbi, dpolicy);
1357
Chao Yubd5b0732017-04-25 20:21:37 +08001358 pend_list = &dcc->pend_list[i];
Chao Yu33da62c2017-10-04 09:08:35 +08001359
1360 mutex_lock(&dcc->cmd_lock);
Chao Yu49c60c62018-01-08 18:48:33 +08001361 if (list_empty(pend_list))
1362 goto next;
Chao Yu67fce702018-06-22 16:06:59 +08001363 if (unlikely(dcc->rbtree_check))
1364 f2fs_bug_on(sbi, !f2fs_check_rb_tree_consistence(sbi,
1365 &dcc->root));
Chao Yu33da62c2017-10-04 09:08:35 +08001366 blk_start_plug(&plug);
Chao Yubd5b0732017-04-25 20:21:37 +08001367 list_for_each_entry_safe(dc, tmp, pend_list, list) {
1368 f2fs_bug_on(sbi, dc->state != D_PREP);
1369
Chao Yuecc9aa02017-10-04 09:08:33 +08001370 if (dpolicy->io_aware && i < dpolicy->io_aware_gran &&
1371 !is_idle(sbi)) {
Chao Yue6c6de12017-09-12 21:35:12 +08001372 io_interrupted = true;
Chao Yu522d1712018-07-08 22:08:09 +08001373 break;
Chao Yue6c6de12017-09-12 21:35:12 +08001374 }
1375
Chao Yu35ec7d52018-08-06 22:43:50 +08001376 __submit_discard_cmd(sbi, dpolicy, dc, &issued);
Chao Yu522d1712018-07-08 22:08:09 +08001377
Chao Yu35ec7d52018-08-06 22:43:50 +08001378 if (issued >= dpolicy->max_requests)
Chao Yu33da62c2017-10-04 09:08:35 +08001379 break;
Chao Yubd5b0732017-04-25 20:21:37 +08001380 }
Chao Yu33da62c2017-10-04 09:08:35 +08001381 blk_finish_plug(&plug);
Chao Yu49c60c62018-01-08 18:48:33 +08001382next:
Chao Yu33da62c2017-10-04 09:08:35 +08001383 mutex_unlock(&dcc->cmd_lock);
1384
Chao Yu522d1712018-07-08 22:08:09 +08001385 if (issued >= dpolicy->max_requests || io_interrupted)
Chao Yu33da62c2017-10-04 09:08:35 +08001386 break;
Chao Yubd5b0732017-04-25 20:21:37 +08001387 }
Chao Yu969d1b12017-08-07 23:09:56 +08001388
Chao Yue6c6de12017-09-12 21:35:12 +08001389 if (!issued && io_interrupted)
1390 issued = -1;
1391
Chao Yu969d1b12017-08-07 23:09:56 +08001392 return issued;
1393}
1394
Chao Yucf5c7592017-10-04 09:08:37 +08001395static bool __drop_discard_cmd(struct f2fs_sb_info *sbi)
Chao Yu969d1b12017-08-07 23:09:56 +08001396{
1397 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1398 struct list_head *pend_list;
1399 struct discard_cmd *dc, *tmp;
1400 int i;
Chao Yucf5c7592017-10-04 09:08:37 +08001401 bool dropped = false;
Chao Yu969d1b12017-08-07 23:09:56 +08001402
1403 mutex_lock(&dcc->cmd_lock);
1404 for (i = MAX_PLIST_NUM - 1; i >= 0; i--) {
1405 pend_list = &dcc->pend_list[i];
1406 list_for_each_entry_safe(dc, tmp, pend_list, list) {
1407 f2fs_bug_on(sbi, dc->state != D_PREP);
1408 __remove_discard_cmd(sbi, dc);
Chao Yucf5c7592017-10-04 09:08:37 +08001409 dropped = true;
Chao Yu969d1b12017-08-07 23:09:56 +08001410 }
1411 }
1412 mutex_unlock(&dcc->cmd_lock);
Chao Yucf5c7592017-10-04 09:08:37 +08001413
1414 return dropped;
Chao Yubd5b0732017-04-25 20:21:37 +08001415}
1416
Chao Yu4d57b862018-05-30 00:20:41 +08001417void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi)
Chao Yu7950e9a2018-01-18 17:23:29 +08001418{
1419 __drop_discard_cmd(sbi);
1420}
1421
Chao Yu0ea80512017-10-28 16:52:32 +08001422static unsigned int __wait_one_discard_bio(struct f2fs_sb_info *sbi,
Chao Yu2a510c002017-06-05 18:29:06 +08001423 struct discard_cmd *dc)
1424{
1425 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Chao Yu0ea80512017-10-28 16:52:32 +08001426 unsigned int len = 0;
Chao Yu2a510c002017-06-05 18:29:06 +08001427
1428 wait_for_completion_io(&dc->wait);
1429 mutex_lock(&dcc->cmd_lock);
1430 f2fs_bug_on(sbi, dc->state != D_DONE);
1431 dc->ref--;
Chao Yu0ea80512017-10-28 16:52:32 +08001432 if (!dc->ref) {
1433 if (!dc->error)
1434 len = dc->len;
Chao Yu2a510c002017-06-05 18:29:06 +08001435 __remove_discard_cmd(sbi, dc);
Chao Yu0ea80512017-10-28 16:52:32 +08001436 }
Chao Yu2a510c002017-06-05 18:29:06 +08001437 mutex_unlock(&dcc->cmd_lock);
Chao Yu0ea80512017-10-28 16:52:32 +08001438
1439 return len;
Chao Yu2a510c002017-06-05 18:29:06 +08001440}
1441
Chao Yu0ea80512017-10-28 16:52:32 +08001442static unsigned int __wait_discard_cmd_range(struct f2fs_sb_info *sbi,
Chao Yu78997b52017-10-04 09:08:34 +08001443 struct discard_policy *dpolicy,
1444 block_t start, block_t end)
Chao Yu63a94fa2017-04-25 20:21:38 +08001445{
1446 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Chao Yu78997b52017-10-04 09:08:34 +08001447 struct list_head *wait_list = (dpolicy->type == DPOLICY_FSTRIM) ?
1448 &(dcc->fstrim_list) : &(dcc->wait_list);
Chao Yu63a94fa2017-04-25 20:21:38 +08001449 struct discard_cmd *dc, *tmp;
Chao Yu6afae632017-05-19 23:46:45 +08001450 bool need_wait;
Chao Yu0ea80512017-10-28 16:52:32 +08001451 unsigned int trimmed = 0;
Chao Yu6afae632017-05-19 23:46:45 +08001452
1453next:
1454 need_wait = false;
Chao Yu63a94fa2017-04-25 20:21:38 +08001455
1456 mutex_lock(&dcc->cmd_lock);
1457 list_for_each_entry_safe(dc, tmp, wait_list, list) {
Chao Yu84126632017-10-04 09:08:32 +08001458 if (dc->lstart + dc->len <= start || end <= dc->lstart)
1459 continue;
Chao Yu78997b52017-10-04 09:08:34 +08001460 if (dc->len < dpolicy->granularity)
Chao Yu84126632017-10-04 09:08:32 +08001461 continue;
Chao Yu78997b52017-10-04 09:08:34 +08001462 if (dc->state == D_DONE && !dc->ref) {
Chao Yu63a94fa2017-04-25 20:21:38 +08001463 wait_for_completion_io(&dc->wait);
Chao Yu0ea80512017-10-28 16:52:32 +08001464 if (!dc->error)
1465 trimmed += dc->len;
Chao Yu63a94fa2017-04-25 20:21:38 +08001466 __remove_discard_cmd(sbi, dc);
Chao Yu6afae632017-05-19 23:46:45 +08001467 } else {
1468 dc->ref++;
1469 need_wait = true;
1470 break;
Chao Yu63a94fa2017-04-25 20:21:38 +08001471 }
1472 }
1473 mutex_unlock(&dcc->cmd_lock);
Chao Yu6afae632017-05-19 23:46:45 +08001474
1475 if (need_wait) {
Chao Yu0ea80512017-10-28 16:52:32 +08001476 trimmed += __wait_one_discard_bio(sbi, dc);
Chao Yu6afae632017-05-19 23:46:45 +08001477 goto next;
1478 }
Chao Yu0ea80512017-10-28 16:52:32 +08001479
1480 return trimmed;
Chao Yu63a94fa2017-04-25 20:21:38 +08001481}
1482
Chao Yu01f9cf62018-06-25 20:33:24 +08001483static unsigned int __wait_all_discard_cmd(struct f2fs_sb_info *sbi,
Chao Yu78997b52017-10-04 09:08:34 +08001484 struct discard_policy *dpolicy)
Chao Yu84126632017-10-04 09:08:32 +08001485{
Jaegeuk Kim9a997182018-05-24 13:57:26 -07001486 struct discard_policy dp;
Chao Yu01f9cf62018-06-25 20:33:24 +08001487 unsigned int discard_blks;
Jaegeuk Kim9a997182018-05-24 13:57:26 -07001488
Chao Yu01f9cf62018-06-25 20:33:24 +08001489 if (dpolicy)
1490 return __wait_discard_cmd_range(sbi, dpolicy, 0, UINT_MAX);
Jaegeuk Kim9a997182018-05-24 13:57:26 -07001491
1492 /* wait all */
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -07001493 __init_discard_policy(sbi, &dp, DPOLICY_FSTRIM, 1);
Chao Yu01f9cf62018-06-25 20:33:24 +08001494 discard_blks = __wait_discard_cmd_range(sbi, &dp, 0, UINT_MAX);
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -07001495 __init_discard_policy(sbi, &dp, DPOLICY_UMOUNT, 1);
Chao Yu01f9cf62018-06-25 20:33:24 +08001496 discard_blks += __wait_discard_cmd_range(sbi, &dp, 0, UINT_MAX);
1497
1498 return discard_blks;
Chao Yu84126632017-10-04 09:08:32 +08001499}
1500
Jaegeuk Kim4e6a8d92016-12-29 14:07:53 -08001501/* This should be covered by global mutex, &sit_i->sentry_lock */
Wei Yongjun94b1e102018-01-05 09:41:20 +00001502static void f2fs_wait_discard_bio(struct f2fs_sb_info *sbi, block_t blkaddr)
Chao Yu275b66b2016-08-29 23:58:34 +08001503{
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001504 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Chao Yu004b6862017-04-14 23:24:55 +08001505 struct discard_cmd *dc;
Chao Yuec9895a2017-04-26 17:39:54 +08001506 bool need_wait = false;
Chao Yu275b66b2016-08-29 23:58:34 +08001507
Jaegeuk Kim15469962017-01-09 20:32:07 -08001508 mutex_lock(&dcc->cmd_lock);
Chao Yu4d57b862018-05-30 00:20:41 +08001509 dc = (struct discard_cmd *)f2fs_lookup_rb_tree(&dcc->root,
1510 NULL, blkaddr);
Chao Yu004b6862017-04-14 23:24:55 +08001511 if (dc) {
Chao Yuec9895a2017-04-26 17:39:54 +08001512 if (dc->state == D_PREP) {
1513 __punch_discard_cmd(sbi, dc, blkaddr);
1514 } else {
1515 dc->ref++;
1516 need_wait = true;
1517 }
Chao Yu275b66b2016-08-29 23:58:34 +08001518 }
Chao Yud4314132017-04-05 18:19:49 +08001519 mutex_unlock(&dcc->cmd_lock);
Chao Yuec9895a2017-04-26 17:39:54 +08001520
Chao Yu2a510c002017-06-05 18:29:06 +08001521 if (need_wait)
1522 __wait_one_discard_bio(sbi, dc);
Chao Yud4314132017-04-05 18:19:49 +08001523}
Chao Yu22d375d2017-04-05 18:19:48 +08001524
Chao Yu4d57b862018-05-30 00:20:41 +08001525void f2fs_stop_discard_thread(struct f2fs_sb_info *sbi)
Chao Yucce13252017-06-29 23:17:45 +08001526{
1527 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1528
1529 if (dcc && dcc->f2fs_issue_discard) {
1530 struct task_struct *discard_thread = dcc->f2fs_issue_discard;
1531
1532 dcc->f2fs_issue_discard = NULL;
1533 kthread_stop(discard_thread);
Jaegeuk Kim15469962017-01-09 20:32:07 -08001534 }
1535}
1536
Chao Yu84126632017-10-04 09:08:32 +08001537/* This comes from f2fs_put_super */
Chao Yucf5c7592017-10-04 09:08:37 +08001538bool f2fs_wait_discard_bios(struct f2fs_sb_info *sbi)
Chao Yu275b66b2016-08-29 23:58:34 +08001539{
Chao Yu969d1b12017-08-07 23:09:56 +08001540 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Chao Yu78997b52017-10-04 09:08:34 +08001541 struct discard_policy dpolicy;
Chao Yucf5c7592017-10-04 09:08:37 +08001542 bool dropped;
Chao Yu969d1b12017-08-07 23:09:56 +08001543
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -07001544 __init_discard_policy(sbi, &dpolicy, DPOLICY_UMOUNT,
1545 dcc->discard_granularity);
Chao Yu78997b52017-10-04 09:08:34 +08001546 __issue_discard_cmd(sbi, &dpolicy);
Chao Yucf5c7592017-10-04 09:08:37 +08001547 dropped = __drop_discard_cmd(sbi);
Chao Yucf5c7592017-10-04 09:08:37 +08001548
Jaegeuk Kim9a997182018-05-24 13:57:26 -07001549 /* just to make sure there is no pending discard commands */
1550 __wait_all_discard_cmd(sbi, NULL);
Chao Yu2482c432018-07-08 22:16:53 +08001551
1552 f2fs_bug_on(sbi, atomic_read(&dcc->discard_cmd_cnt));
Chao Yucf5c7592017-10-04 09:08:37 +08001553 return dropped;
Chao Yu969d1b12017-08-07 23:09:56 +08001554}
1555
Jaegeuk Kim15469962017-01-09 20:32:07 -08001556static int issue_discard_thread(void *data)
1557{
1558 struct f2fs_sb_info *sbi = data;
1559 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1560 wait_queue_head_t *q = &dcc->discard_wait_queue;
Chao Yu78997b52017-10-04 09:08:34 +08001561 struct discard_policy dpolicy;
Chao Yu969d1b12017-08-07 23:09:56 +08001562 unsigned int wait_ms = DEF_MIN_DISCARD_ISSUE_TIME;
1563 int issued;
Sahitya Tummalaabde73c2018-08-31 15:09:26 +05301564 unsigned long interval = sbi->interval_time[REQ_TIME] * HZ;
1565 long delta;
Jaegeuk Kim15469962017-01-09 20:32:07 -08001566
Jaegeuk Kim1d7be272017-05-17 10:36:58 -07001567 set_freezable();
Jaegeuk Kim15469962017-01-09 20:32:07 -08001568
Jaegeuk Kim1d7be272017-05-17 10:36:58 -07001569 do {
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -07001570 __init_discard_policy(sbi, &dpolicy, DPOLICY_BG,
Chao Yu78997b52017-10-04 09:08:34 +08001571 dcc->discard_granularity);
1572
Chao Yu969d1b12017-08-07 23:09:56 +08001573 wait_event_interruptible_timeout(*q,
1574 kthread_should_stop() || freezing(current) ||
1575 dcc->discard_wake,
1576 msecs_to_jiffies(wait_ms));
Sheng Yong35a9a762018-05-08 17:51:34 +08001577
1578 if (dcc->discard_wake)
1579 dcc->discard_wake = 0;
1580
Jaegeuk Kim1d7be272017-05-17 10:36:58 -07001581 if (try_to_freeze())
1582 continue;
Chao Yu3b60d802018-01-25 18:57:27 +08001583 if (f2fs_readonly(sbi->sb))
1584 continue;
Jaegeuk Kim1d7be272017-05-17 10:36:58 -07001585 if (kthread_should_stop())
1586 return 0;
Yunlei Hed6184772018-04-13 11:08:05 +08001587 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) {
1588 wait_ms = dpolicy.max_interval;
1589 continue;
1590 }
Jaegeuk Kim15469962017-01-09 20:32:07 -08001591
Jaegeuk Kim5b0e9532018-05-07 14:22:40 -07001592 if (sbi->gc_mode == GC_URGENT)
Jaegeuk Kim8bb4f252018-05-29 09:58:42 -07001593 __init_discard_policy(sbi, &dpolicy, DPOLICY_FORCE, 1);
Jaegeuk Kim1d7be272017-05-17 10:36:58 -07001594
Chao Yudc6febb2017-07-22 08:52:23 +08001595 sb_start_intwrite(sbi->sb);
1596
Chao Yu78997b52017-10-04 09:08:34 +08001597 issued = __issue_discard_cmd(sbi, &dpolicy);
Yunlei Hef9d1dce2018-04-08 15:11:11 +08001598 if (issued > 0) {
Chao Yu78997b52017-10-04 09:08:34 +08001599 __wait_all_discard_cmd(sbi, &dpolicy);
1600 wait_ms = dpolicy.min_interval;
Yunlei Hef9d1dce2018-04-08 15:11:11 +08001601 } else if (issued == -1){
Sahitya Tummalaabde73c2018-08-31 15:09:26 +05301602 delta = (sbi->last_time[REQ_TIME] + interval) - jiffies;
1603 if (delta > 0)
1604 wait_ms = jiffies_to_msecs(delta);
1605 else
1606 wait_ms = dpolicy.mid_interval;
Chao Yu969d1b12017-08-07 23:09:56 +08001607 } else {
Chao Yu78997b52017-10-04 09:08:34 +08001608 wait_ms = dpolicy.max_interval;
Chao Yu969d1b12017-08-07 23:09:56 +08001609 }
Jaegeuk Kim1d7be272017-05-17 10:36:58 -07001610
Chao Yudc6febb2017-07-22 08:52:23 +08001611 sb_end_intwrite(sbi->sb);
1612
Jaegeuk Kim1d7be272017-05-17 10:36:58 -07001613 } while (!kthread_should_stop());
1614 return 0;
Jaegeuk Kim15469962017-01-09 20:32:07 -08001615}
1616
Damien Le Moalf46e88092016-10-28 17:45:06 +09001617#ifdef CONFIG_BLK_DEV_ZONED
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001618static int __f2fs_issue_discard_zone(struct f2fs_sb_info *sbi,
1619 struct block_device *bdev, block_t blkstart, block_t blklen)
Damien Le Moalf46e88092016-10-28 17:45:06 +09001620{
Jaegeuk Kim925922852017-02-22 20:18:35 -08001621 sector_t sector, nr_sects;
Kinglong Mee10a875f2017-03-08 09:49:53 +08001622 block_t lblkstart = blkstart;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001623 int devi = 0;
Damien Le Moalf46e88092016-10-28 17:45:06 +09001624
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001625 if (sbi->s_ndevs) {
1626 devi = f2fs_target_device_index(sbi, blkstart);
1627 blkstart -= FDEV(devi).start_blk;
1628 }
Damien Le Moalf46e88092016-10-28 17:45:06 +09001629
1630 /*
1631 * We need to know the type of the zone: for conventional zones,
1632 * use regular discard if the drive supports it. For sequential
1633 * zones, reset the zone write pointer.
1634 */
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001635 switch (get_blkz_type(sbi, bdev, blkstart)) {
Damien Le Moalf46e88092016-10-28 17:45:06 +09001636
1637 case BLK_ZONE_TYPE_CONVENTIONAL:
1638 if (!blk_queue_discard(bdev_get_queue(bdev)))
1639 return 0;
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001640 return __queue_discard_cmd(sbi, bdev, lblkstart, blklen);
Damien Le Moalf46e88092016-10-28 17:45:06 +09001641 case BLK_ZONE_TYPE_SEQWRITE_REQ:
1642 case BLK_ZONE_TYPE_SEQWRITE_PREF:
Jaegeuk Kim925922852017-02-22 20:18:35 -08001643 sector = SECTOR_FROM_BLOCK(blkstart);
1644 nr_sects = SECTOR_FROM_BLOCK(blklen);
1645
1646 if (sector & (bdev_zone_sectors(bdev) - 1) ||
1647 nr_sects != bdev_zone_sectors(bdev)) {
1648 f2fs_msg(sbi->sb, KERN_INFO,
1649 "(%d) %s: Unaligned discard attempted (block %x + %x)",
1650 devi, sbi->s_ndevs ? FDEV(devi).path: "",
1651 blkstart, blklen);
1652 return -EIO;
1653 }
Jaegeuk Kimd50aaee2017-02-15 11:14:06 -08001654 trace_f2fs_issue_reset_zone(bdev, blkstart);
Damien Le Moalf46e88092016-10-28 17:45:06 +09001655 return blkdev_reset_zones(bdev, sector,
1656 nr_sects, GFP_NOFS);
1657 default:
1658 /* Unknown zone type: broken device ? */
1659 return -EIO;
1660 }
1661}
1662#endif
1663
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001664static int __issue_discard_async(struct f2fs_sb_info *sbi,
1665 struct block_device *bdev, block_t blkstart, block_t blklen)
1666{
1667#ifdef CONFIG_BLK_DEV_ZONED
Sheng Yongccd31cb2018-02-06 12:31:17 +08001668 if (f2fs_sb_has_blkzoned(sbi->sb) &&
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001669 bdev_zoned_model(bdev) != BLK_ZONED_NONE)
1670 return __f2fs_issue_discard_zone(sbi, bdev, blkstart, blklen);
1671#endif
Jaegeuk Kimc81abe32017-03-07 18:02:02 -08001672 return __queue_discard_cmd(sbi, bdev, blkstart, blklen);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001673}
1674
Jaegeuk Kim1e87a782014-04-15 13:57:55 +09001675static int f2fs_issue_discard(struct f2fs_sb_info *sbi,
Jaegeuk Kim37208872013-11-12 16:55:17 +09001676 block_t blkstart, block_t blklen)
1677{
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001678 sector_t start = blkstart, len = 0;
1679 struct block_device *bdev;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001680 struct seg_entry *se;
1681 unsigned int offset;
1682 block_t i;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001683 int err = 0;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001684
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001685 bdev = f2fs_target_device(sbi, blkstart, NULL);
1686
1687 for (i = blkstart; i < blkstart + blklen; i++, len++) {
1688 if (i != start) {
1689 struct block_device *bdev2 =
1690 f2fs_target_device(sbi, i, NULL);
1691
1692 if (bdev2 != bdev) {
1693 err = __issue_discard_async(sbi, bdev,
1694 start, len);
1695 if (err)
1696 return err;
1697 bdev = bdev2;
1698 start = i;
1699 len = 0;
1700 }
1701 }
1702
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001703 se = get_seg_entry(sbi, GET_SEGNO(sbi, i));
1704 offset = GET_BLKOFF_FROM_SEG0(sbi, i);
1705
1706 if (!f2fs_test_and_set_bit(offset, se->discard_map))
1707 sbi->discard_blks--;
1708 }
Damien Le Moalf46e88092016-10-28 17:45:06 +09001709
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001710 if (len)
1711 err = __issue_discard_async(sbi, bdev, start, len);
1712 return err;
Jaegeuk Kim1e87a782014-04-15 13:57:55 +09001713}
1714
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08001715static bool add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc,
1716 bool check_only)
Jaegeuk Kimadf49832014-10-28 22:27:59 -07001717{
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001718 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
1719 int max_blocks = sbi->blocks_per_seg;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001720 struct seg_entry *se = get_seg_entry(sbi, cpc->trim_start);
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001721 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
1722 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001723 unsigned long *discard_map = (unsigned long *)se->discard_map;
Jaegeuk Kim60a3b782015-02-10 16:44:29 -08001724 unsigned long *dmap = SIT_I(sbi)->tmp_map;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001725 unsigned int start = 0, end = -1;
Chao Yuc473f1a2017-04-27 20:40:39 +08001726 bool force = (cpc->reason & CP_DISCARD);
Chao Yua7eeb8232017-03-28 18:18:50 +08001727 struct discard_entry *de = NULL;
Chao Yu46f84c22017-04-15 14:09:36 +08001728 struct list_head *head = &SM_I(sbi)->dcc_info->entry_list;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001729 int i;
1730
Chao Yu7d20c8a2018-09-04 03:52:17 +08001731 if (se->valid_blocks == max_blocks || !f2fs_hw_support_discard(sbi))
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08001732 return false;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001733
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001734 if (!force) {
Chao Yu7d20c8a2018-09-04 03:52:17 +08001735 if (!f2fs_realtime_discard_enable(sbi) || !se->valid_blocks ||
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001736 SM_I(sbi)->dcc_info->nr_discards >=
1737 SM_I(sbi)->dcc_info->max_discards)
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08001738 return false;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001739 }
1740
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001741 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
1742 for (i = 0; i < entries; i++)
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001743 dmap[i] = force ? ~ckpt_map[i] & ~discard_map[i] :
Jaegeuk Kimd7bc2482014-12-12 13:53:41 -08001744 (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001745
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001746 while (force || SM_I(sbi)->dcc_info->nr_discards <=
1747 SM_I(sbi)->dcc_info->max_discards) {
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001748 start = __find_rev_next_bit(dmap, max_blocks, end + 1);
1749 if (start >= max_blocks)
1750 break;
1751
1752 end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
Yunlei Hec7b41e12016-07-07 12:13:33 +08001753 if (force && start && end != max_blocks
1754 && (end - start) < cpc->trim_minlen)
1755 continue;
1756
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08001757 if (check_only)
1758 return true;
1759
Chao Yua7eeb8232017-03-28 18:18:50 +08001760 if (!de) {
1761 de = f2fs_kmem_cache_alloc(discard_entry_slab,
1762 GFP_F2FS_ZERO);
1763 de->start_blkaddr = START_BLOCK(sbi, cpc->trim_start);
1764 list_add_tail(&de->list, head);
1765 }
1766
1767 for (i = start; i < end; i++)
1768 __set_bit_le(i, (void *)de->discard_map);
1769
1770 SM_I(sbi)->dcc_info->nr_discards += end - start;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001771 }
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08001772 return false;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001773}
1774
Chao Yuaf8ff652018-04-25 17:38:29 +08001775static void release_discard_addr(struct discard_entry *entry)
1776{
1777 list_del(&entry->list);
1778 kmem_cache_free(discard_entry_slab, entry);
1779}
1780
Chao Yu4d57b862018-05-30 00:20:41 +08001781void f2fs_release_discard_addrs(struct f2fs_sb_info *sbi)
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001782{
Chao Yu46f84c22017-04-15 14:09:36 +08001783 struct list_head *head = &(SM_I(sbi)->dcc_info->entry_list);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001784 struct discard_entry *entry, *this;
1785
1786 /* drop caches */
Chao Yuaf8ff652018-04-25 17:38:29 +08001787 list_for_each_entry_safe(entry, this, head, list)
1788 release_discard_addr(entry);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001789}
1790
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001791/*
Chao Yu4d57b862018-05-30 00:20:41 +08001792 * Should call f2fs_clear_prefree_segments after checkpoint is done.
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001793 */
1794static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
1795{
1796 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Chao Yub65ee142014-08-04 10:10:07 +08001797 unsigned int segno;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001798
1799 mutex_lock(&dirty_i->seglist_lock);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001800 for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001801 __set_test_and_free(sbi, segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001802 mutex_unlock(&dirty_i->seglist_lock);
1803}
1804
Chao Yu4d57b862018-05-30 00:20:41 +08001805void f2fs_clear_prefree_segments(struct f2fs_sb_info *sbi,
1806 struct cp_control *cpc)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001807{
Chao Yu969d1b12017-08-07 23:09:56 +08001808 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1809 struct list_head *head = &dcc->entry_list;
Chao Yu2d7b8222014-03-29 11:33:17 +08001810 struct discard_entry *entry, *this;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001811 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Changman Lee29e59c12013-11-11 09:24:37 +09001812 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
Changman Lee29e59c12013-11-11 09:24:37 +09001813 unsigned int start = 0, end = -1;
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001814 unsigned int secno, start_segno;
Chao Yuc473f1a2017-04-27 20:40:39 +08001815 bool force = (cpc->reason & CP_DISCARD);
Yunlong Songad6672b2018-07-19 20:58:15 +08001816 bool need_align = test_opt(sbi, LFS) && sbi->segs_per_sec > 1;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001817
1818 mutex_lock(&dirty_i->seglist_lock);
Changman Lee29e59c12013-11-11 09:24:37 +09001819
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001820 while (1) {
Changman Lee29e59c12013-11-11 09:24:37 +09001821 int i;
Yunlong Songad6672b2018-07-19 20:58:15 +08001822
1823 if (need_align && end != -1)
1824 end--;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001825 start = find_next_bit(prefree_map, MAIN_SEGS(sbi), end + 1);
1826 if (start >= MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001827 break;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001828 end = find_next_zero_bit(prefree_map, MAIN_SEGS(sbi),
1829 start + 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001830
Yunlong Songad6672b2018-07-19 20:58:15 +08001831 if (need_align) {
1832 start = rounddown(start, sbi->segs_per_sec);
1833 end = roundup(end, sbi->segs_per_sec);
1834 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001835
Yunlong Songad6672b2018-07-19 20:58:15 +08001836 for (i = start; i < end; i++) {
1837 if (test_and_clear_bit(i, prefree_map))
1838 dirty_i->nr_dirty[PRE]--;
1839 }
Changman Lee29e59c12013-11-11 09:24:37 +09001840
Chao Yu7d20c8a2018-09-04 03:52:17 +08001841 if (!f2fs_realtime_discard_enable(sbi))
Changman Lee29e59c12013-11-11 09:24:37 +09001842 continue;
1843
Yunlei He650d3c42016-12-22 11:46:24 +08001844 if (force && start >= cpc->trim_start &&
1845 (end - 1) <= cpc->trim_end)
1846 continue;
1847
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001848 if (!test_opt(sbi, LFS) || sbi->segs_per_sec == 1) {
1849 f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
Jaegeuk Kim37208872013-11-12 16:55:17 +09001850 (end - start) << sbi->log_blocks_per_seg);
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001851 continue;
1852 }
1853next:
Jaegeuk Kim4ddb1a42017-04-07 15:08:17 -07001854 secno = GET_SEC_FROM_SEG(sbi, start);
1855 start_segno = GET_SEG_FROM_SEC(sbi, secno);
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001856 if (!IS_CURSEC(sbi, secno) &&
Jaegeuk Kim302bd342017-04-07 14:33:22 -07001857 !get_valid_blocks(sbi, start, true))
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001858 f2fs_issue_discard(sbi, START_BLOCK(sbi, start_segno),
1859 sbi->segs_per_sec << sbi->log_blocks_per_seg);
1860
1861 start = start_segno + sbi->segs_per_sec;
1862 if (start < end)
1863 goto next;
Jaegeuk Kim8b107f52017-02-27 11:57:11 -08001864 else
1865 end = start - 1;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001866 }
1867 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001868
1869 /* send small discards */
Chao Yu2d7b8222014-03-29 11:33:17 +08001870 list_for_each_entry_safe(entry, this, head, list) {
Chao Yua7eeb8232017-03-28 18:18:50 +08001871 unsigned int cur_pos = 0, next_pos, len, total_len = 0;
1872 bool is_valid = test_bit_le(0, entry->discard_map);
1873
1874find_next:
1875 if (is_valid) {
1876 next_pos = find_next_zero_bit_le(entry->discard_map,
1877 sbi->blocks_per_seg, cur_pos);
1878 len = next_pos - cur_pos;
1879
Sheng Yongccd31cb2018-02-06 12:31:17 +08001880 if (f2fs_sb_has_blkzoned(sbi->sb) ||
Damien Le Moalacfd28102017-05-26 17:04:40 +09001881 (force && len < cpc->trim_minlen))
Chao Yua7eeb8232017-03-28 18:18:50 +08001882 goto skip;
1883
1884 f2fs_issue_discard(sbi, entry->start_blkaddr + cur_pos,
1885 len);
Chao Yua7eeb8232017-03-28 18:18:50 +08001886 total_len += len;
1887 } else {
1888 next_pos = find_next_bit_le(entry->discard_map,
1889 sbi->blocks_per_seg, cur_pos);
1890 }
Jaegeuk Kim836b5a62015-04-30 22:50:06 -07001891skip:
Chao Yua7eeb8232017-03-28 18:18:50 +08001892 cur_pos = next_pos;
1893 is_valid = !is_valid;
1894
1895 if (cur_pos < sbi->blocks_per_seg)
1896 goto find_next;
1897
Chao Yuaf8ff652018-04-25 17:38:29 +08001898 release_discard_addr(entry);
Chao Yu969d1b12017-08-07 23:09:56 +08001899 dcc->nr_discards -= total_len;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001900 }
Chao Yu34e159d2017-04-25 00:21:34 +08001901
Jaegeuk Kim01983c72017-08-22 21:15:43 -07001902 wake_up_discard_thread(sbi, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001903}
1904
Jaegeuk Kim8ed59742017-01-29 14:27:02 +09001905static int create_discard_cmd_control(struct f2fs_sb_info *sbi)
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001906{
Jaegeuk Kim15469962017-01-09 20:32:07 -08001907 dev_t dev = sbi->sb->s_bdev->bd_dev;
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001908 struct discard_cmd_control *dcc;
Chao Yuba48a332017-04-15 14:09:37 +08001909 int err = 0, i;
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001910
1911 if (SM_I(sbi)->dcc_info) {
1912 dcc = SM_I(sbi)->dcc_info;
1913 goto init_thread;
1914 }
1915
Chao Yuacbf0542017-11-30 19:28:17 +08001916 dcc = f2fs_kzalloc(sbi, sizeof(struct discard_cmd_control), GFP_KERNEL);
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001917 if (!dcc)
1918 return -ENOMEM;
1919
Chao Yu969d1b12017-08-07 23:09:56 +08001920 dcc->discard_granularity = DEFAULT_DISCARD_GRANULARITY;
Chao Yu46f84c22017-04-15 14:09:36 +08001921 INIT_LIST_HEAD(&dcc->entry_list);
Chao Yu78997b52017-10-04 09:08:34 +08001922 for (i = 0; i < MAX_PLIST_NUM; i++)
Chao Yuba48a332017-04-15 14:09:37 +08001923 INIT_LIST_HEAD(&dcc->pend_list[i]);
Chao Yu46f84c22017-04-15 14:09:36 +08001924 INIT_LIST_HEAD(&dcc->wait_list);
Chao Yu84126632017-10-04 09:08:32 +08001925 INIT_LIST_HEAD(&dcc->fstrim_list);
Jaegeuk Kim15469962017-01-09 20:32:07 -08001926 mutex_init(&dcc->cmd_lock);
Chao Yu8b8dd652017-03-25 17:19:58 +08001927 atomic_set(&dcc->issued_discard, 0);
1928 atomic_set(&dcc->issing_discard, 0);
Chao Yu5f323662017-03-25 17:19:59 +08001929 atomic_set(&dcc->discard_cmd_cnt, 0);
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001930 dcc->nr_discards = 0;
Chao Yud618eba2017-04-25 00:21:35 +08001931 dcc->max_discards = MAIN_SEGS(sbi) << sbi->log_blocks_per_seg;
Chao Yud84d1cb2017-04-18 19:27:39 +08001932 dcc->undiscard_blks = 0;
Chao Yu20ee4382018-07-08 22:11:01 +08001933 dcc->next_pos = 0;
Chao Yu004b6862017-04-14 23:24:55 +08001934 dcc->root = RB_ROOT;
Chao Yu67fce702018-06-22 16:06:59 +08001935 dcc->rbtree_check = false;
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001936
Jaegeuk Kim15469962017-01-09 20:32:07 -08001937 init_waitqueue_head(&dcc->discard_wait_queue);
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001938 SM_I(sbi)->dcc_info = dcc;
1939init_thread:
Jaegeuk Kim15469962017-01-09 20:32:07 -08001940 dcc->f2fs_issue_discard = kthread_run(issue_discard_thread, sbi,
1941 "f2fs_discard-%u:%u", MAJOR(dev), MINOR(dev));
1942 if (IS_ERR(dcc->f2fs_issue_discard)) {
1943 err = PTR_ERR(dcc->f2fs_issue_discard);
1944 kfree(dcc);
1945 SM_I(sbi)->dcc_info = NULL;
1946 return err;
1947 }
1948
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001949 return err;
1950}
1951
Chao Yuf0994052017-03-27 18:14:04 +08001952static void destroy_discard_cmd_control(struct f2fs_sb_info *sbi)
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001953{
1954 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1955
Chao Yuf0994052017-03-27 18:14:04 +08001956 if (!dcc)
1957 return;
1958
Chao Yu4d57b862018-05-30 00:20:41 +08001959 f2fs_stop_discard_thread(sbi);
Chao Yuf0994052017-03-27 18:14:04 +08001960
1961 kfree(dcc);
1962 SM_I(sbi)->dcc_info = NULL;
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08001963}
1964
Chao Yu184a5cd2014-09-04 18:13:01 +08001965static bool __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001966{
1967 struct sit_info *sit_i = SIT_I(sbi);
Chao Yu184a5cd2014-09-04 18:13:01 +08001968
1969 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001970 sit_i->dirty_sentries++;
Chao Yu184a5cd2014-09-04 18:13:01 +08001971 return false;
1972 }
1973
1974 return true;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001975}
1976
1977static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
1978 unsigned int segno, int modified)
1979{
1980 struct seg_entry *se = get_seg_entry(sbi, segno);
1981 se->type = type;
1982 if (modified)
1983 __mark_sit_entry_dirty(sbi, segno);
1984}
1985
1986static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
1987{
1988 struct seg_entry *se;
1989 unsigned int segno, offset;
1990 long int new_vblocks;
Yunlong Song6415fed2017-08-02 21:20:13 +08001991 bool exist;
1992#ifdef CONFIG_F2FS_CHECK_FS
1993 bool mir_exist;
1994#endif
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001995
1996 segno = GET_SEGNO(sbi, blkaddr);
1997
1998 se = get_seg_entry(sbi, segno);
1999 new_vblocks = se->valid_blocks + del;
Jaegeuk Kim491c0852014-02-04 13:01:10 +09002000 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002001
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002002 f2fs_bug_on(sbi, (new_vblocks >> (sizeof(unsigned short) << 3) ||
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002003 (new_vblocks > sbi->blocks_per_seg)));
2004
2005 se->valid_blocks = new_vblocks;
Chao Yua1f72ac22018-06-04 23:20:17 +08002006 se->mtime = get_mtime(sbi, false);
2007 if (se->mtime > SIT_I(sbi)->max_mtime)
2008 SIT_I(sbi)->max_mtime = se->mtime;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002009
2010 /* Update valid block bitmap */
2011 if (del > 0) {
Yunlong Song6415fed2017-08-02 21:20:13 +08002012 exist = f2fs_test_and_set_bit(offset, se->cur_valid_map);
Chao Yu355e7892017-01-07 18:51:01 +08002013#ifdef CONFIG_F2FS_CHECK_FS
Yunlong Song6415fed2017-08-02 21:20:13 +08002014 mir_exist = f2fs_test_and_set_bit(offset,
2015 se->cur_valid_map_mir);
2016 if (unlikely(exist != mir_exist)) {
2017 f2fs_msg(sbi->sb, KERN_ERR, "Inconsistent error "
2018 "when setting bitmap, blk:%u, old bit:%d",
2019 blkaddr, exist);
Jaegeuk Kim05796762014-09-02 16:05:00 -07002020 f2fs_bug_on(sbi, 1);
Chao Yu355e7892017-01-07 18:51:01 +08002021 }
Yunlong Song6415fed2017-08-02 21:20:13 +08002022#endif
2023 if (unlikely(exist)) {
2024 f2fs_msg(sbi->sb, KERN_ERR,
2025 "Bitmap was wrongly set, blk:%u", blkaddr);
2026 f2fs_bug_on(sbi, 1);
Yunlong Song35ee82c2017-08-02 22:16:54 +08002027 se->valid_blocks--;
2028 del = 0;
Yunlong Song6415fed2017-08-02 21:20:13 +08002029 }
2030
Chao Yu7d20c8a2018-09-04 03:52:17 +08002031 if (!f2fs_test_and_set_bit(offset, se->discard_map))
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07002032 sbi->discard_blks--;
Jaegeuk Kim720037f2017-03-06 11:59:56 -08002033
2034 /* don't overwrite by SSR to keep node chain */
Yunlei He5d7881c2018-03-07 16:22:50 +08002035 if (IS_NODESEG(se->type)) {
Jaegeuk Kim720037f2017-03-06 11:59:56 -08002036 if (!f2fs_test_and_set_bit(offset, se->ckpt_valid_map))
2037 se->ckpt_valid_blocks++;
2038 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002039 } else {
Yunlong Song6415fed2017-08-02 21:20:13 +08002040 exist = f2fs_test_and_clear_bit(offset, se->cur_valid_map);
Chao Yu355e7892017-01-07 18:51:01 +08002041#ifdef CONFIG_F2FS_CHECK_FS
Yunlong Song6415fed2017-08-02 21:20:13 +08002042 mir_exist = f2fs_test_and_clear_bit(offset,
2043 se->cur_valid_map_mir);
2044 if (unlikely(exist != mir_exist)) {
2045 f2fs_msg(sbi->sb, KERN_ERR, "Inconsistent error "
2046 "when clearing bitmap, blk:%u, old bit:%d",
2047 blkaddr, exist);
Jaegeuk Kim05796762014-09-02 16:05:00 -07002048 f2fs_bug_on(sbi, 1);
Chao Yu355e7892017-01-07 18:51:01 +08002049 }
Yunlong Song6415fed2017-08-02 21:20:13 +08002050#endif
2051 if (unlikely(!exist)) {
2052 f2fs_msg(sbi->sb, KERN_ERR,
2053 "Bitmap was wrongly cleared, blk:%u", blkaddr);
2054 f2fs_bug_on(sbi, 1);
Yunlong Song35ee82c2017-08-02 22:16:54 +08002055 se->valid_blocks++;
2056 del = 0;
Yunlong Song6415fed2017-08-02 21:20:13 +08002057 }
2058
Chao Yu7d20c8a2018-09-04 03:52:17 +08002059 if (f2fs_test_and_clear_bit(offset, se->discard_map))
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07002060 sbi->discard_blks++;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002061 }
2062 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
2063 se->ckpt_valid_blocks += del;
2064
2065 __mark_sit_entry_dirty(sbi, segno);
2066
2067 /* update total number of valid blocks to be written in ckpt area */
2068 SIT_I(sbi)->written_valid_blocks += del;
2069
2070 if (sbi->segs_per_sec > 1)
2071 get_sec_entry(sbi, segno)->valid_blocks += del;
2072}
2073
Chao Yu4d57b862018-05-30 00:20:41 +08002074void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002075{
2076 unsigned int segno = GET_SEGNO(sbi, addr);
2077 struct sit_info *sit_i = SIT_I(sbi);
2078
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002079 f2fs_bug_on(sbi, addr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002080 if (addr == NEW_ADDR)
2081 return;
2082
Chao Yu6aa58d82018-08-14 22:37:25 +08002083 invalidate_mapping_pages(META_MAPPING(sbi), addr, addr);
2084
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002085 /* add it into sit main buffer */
Chao Yu3d26fa62017-10-30 17:49:53 +08002086 down_write(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002087
2088 update_sit_entry(sbi, addr, -1);
2089
2090 /* add it into dirty seglist */
2091 locate_dirty_segment(sbi, segno);
2092
Chao Yu3d26fa62017-10-30 17:49:53 +08002093 up_write(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002094}
2095
Chao Yu4d57b862018-05-30 00:20:41 +08002096bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr)
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07002097{
2098 struct sit_info *sit_i = SIT_I(sbi);
2099 unsigned int segno, offset;
2100 struct seg_entry *se;
2101 bool is_cp = false;
2102
Chao Yue1da7872018-06-05 17:44:11 +08002103 if (!is_valid_data_blkaddr(sbi, blkaddr))
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07002104 return true;
2105
Chao Yu3d26fa62017-10-30 17:49:53 +08002106 down_read(&sit_i->sentry_lock);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07002107
2108 segno = GET_SEGNO(sbi, blkaddr);
2109 se = get_seg_entry(sbi, segno);
2110 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
2111
2112 if (f2fs_test_bit(offset, se->ckpt_valid_map))
2113 is_cp = true;
2114
Chao Yu3d26fa62017-10-30 17:49:53 +08002115 up_read(&sit_i->sentry_lock);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07002116
2117 return is_cp;
2118}
2119
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002120/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002121 * This function should be resided under the curseg_mutex lock
2122 */
2123static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
Haicheng Lie79efe32013-06-13 16:59:27 +08002124 struct f2fs_summary *sum)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002125{
2126 struct curseg_info *curseg = CURSEG_I(sbi, type);
2127 void *addr = curseg->sum_blk;
Haicheng Lie79efe32013-06-13 16:59:27 +08002128 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002129 memcpy(addr, sum, sizeof(struct f2fs_summary));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002130}
2131
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002132/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002133 * Calculate the number of current summary pages for writing
2134 */
Chao Yu4d57b862018-05-30 00:20:41 +08002135int f2fs_npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002136{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002137 int valid_sum_count = 0;
Fan Li9a479382013-10-29 16:21:47 +08002138 int i, sum_in_page;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002139
2140 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
2141 if (sbi->ckpt->alloc_type[i] == SSR)
2142 valid_sum_count += sbi->blocks_per_seg;
Chao Yu3fa06d72014-12-09 14:21:46 +08002143 else {
2144 if (for_ra)
2145 valid_sum_count += le16_to_cpu(
2146 F2FS_CKPT(sbi)->cur_data_blkoff[i]);
2147 else
2148 valid_sum_count += curseg_blkoff(sbi, i);
2149 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002150 }
2151
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002152 sum_in_page = (PAGE_SIZE - 2 * SUM_JOURNAL_SIZE -
Fan Li9a479382013-10-29 16:21:47 +08002153 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
2154 if (valid_sum_count <= sum_in_page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002155 return 1;
Fan Li9a479382013-10-29 16:21:47 +08002156 else if ((valid_sum_count - sum_in_page) <=
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002157 (PAGE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002158 return 2;
2159 return 3;
2160}
2161
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002162/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002163 * Caller should put this summary page
2164 */
Chao Yu4d57b862018-05-30 00:20:41 +08002165struct page *f2fs_get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002166{
Chao Yu77357302018-07-17 00:02:17 +08002167 return f2fs_get_meta_page_nofail(sbi, GET_SUM_BLOCK(sbi, segno));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002168}
2169
Chao Yu4d57b862018-05-30 00:20:41 +08002170void f2fs_update_meta_page(struct f2fs_sb_info *sbi,
2171 void *src, block_t blk_addr)
Chao Yu381722d2015-05-19 17:40:04 +08002172{
Chao Yu4d57b862018-05-30 00:20:41 +08002173 struct page *page = f2fs_grab_meta_page(sbi, blk_addr);
Chao Yu381722d2015-05-19 17:40:04 +08002174
Chao Yu0537b812017-11-02 20:41:02 +08002175 memcpy(page_address(page), src, PAGE_SIZE);
Chao Yu381722d2015-05-19 17:40:04 +08002176 set_page_dirty(page);
2177 f2fs_put_page(page, 1);
2178}
2179
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002180static void write_sum_page(struct f2fs_sb_info *sbi,
2181 struct f2fs_summary_block *sum_blk, block_t blk_addr)
2182{
Chao Yu4d57b862018-05-30 00:20:41 +08002183 f2fs_update_meta_page(sbi, (void *)sum_blk, blk_addr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002184}
2185
Chao Yub7ad7512016-02-19 18:08:46 +08002186static void write_current_sum_page(struct f2fs_sb_info *sbi,
2187 int type, block_t blk_addr)
2188{
2189 struct curseg_info *curseg = CURSEG_I(sbi, type);
Chao Yu4d57b862018-05-30 00:20:41 +08002190 struct page *page = f2fs_grab_meta_page(sbi, blk_addr);
Chao Yub7ad7512016-02-19 18:08:46 +08002191 struct f2fs_summary_block *src = curseg->sum_blk;
2192 struct f2fs_summary_block *dst;
2193
2194 dst = (struct f2fs_summary_block *)page_address(page);
Chao Yu81114ba2018-04-09 20:25:06 +08002195 memset(dst, 0, PAGE_SIZE);
Chao Yub7ad7512016-02-19 18:08:46 +08002196
2197 mutex_lock(&curseg->curseg_mutex);
2198
2199 down_read(&curseg->journal_rwsem);
2200 memcpy(&dst->journal, curseg->journal, SUM_JOURNAL_SIZE);
2201 up_read(&curseg->journal_rwsem);
2202
2203 memcpy(dst->entries, src->entries, SUM_ENTRY_SIZE);
2204 memcpy(&dst->footer, &src->footer, SUM_FOOTER_SIZE);
2205
2206 mutex_unlock(&curseg->curseg_mutex);
2207
2208 set_page_dirty(page);
2209 f2fs_put_page(page, 1);
2210}
2211
Jaegeuk Kima7881892017-04-20 13:51:57 -07002212static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
2213{
2214 struct curseg_info *curseg = CURSEG_I(sbi, type);
2215 unsigned int segno = curseg->segno + 1;
2216 struct free_segmap_info *free_i = FREE_I(sbi);
2217
2218 if (segno < MAIN_SEGS(sbi) && segno % sbi->segs_per_sec)
2219 return !test_bit(segno, free_i->free_segmap);
2220 return 0;
2221}
2222
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002223/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002224 * Find a new segment from the free segments bitmap to right order
2225 * This function should be returned with success, otherwise BUG
2226 */
2227static void get_new_segment(struct f2fs_sb_info *sbi,
2228 unsigned int *newseg, bool new_sec, int dir)
2229{
2230 struct free_segmap_info *free_i = FREE_I(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002231 unsigned int segno, secno, zoneno;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002232 unsigned int total_zones = MAIN_SECS(sbi) / sbi->secs_per_zone;
Jaegeuk Kim4ddb1a42017-04-07 15:08:17 -07002233 unsigned int hint = GET_SEC_FROM_SEG(sbi, *newseg);
2234 unsigned int old_zoneno = GET_ZONE_FROM_SEG(sbi, *newseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002235 unsigned int left_start = hint;
2236 bool init = true;
2237 int go_left = 0;
2238 int i;
2239
Chao Yu1a118cc2015-02-11 18:20:38 +08002240 spin_lock(&free_i->segmap_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002241
2242 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
2243 segno = find_next_zero_bit(free_i->free_segmap,
Jaegeuk Kim4ddb1a42017-04-07 15:08:17 -07002244 GET_SEG_FROM_SEC(sbi, hint + 1), *newseg + 1);
2245 if (segno < GET_SEG_FROM_SEC(sbi, hint + 1))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002246 goto got_it;
2247 }
2248find_other_zone:
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002249 secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint);
2250 if (secno >= MAIN_SECS(sbi)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002251 if (dir == ALLOC_RIGHT) {
2252 secno = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002253 MAIN_SECS(sbi), 0);
2254 f2fs_bug_on(sbi, secno >= MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002255 } else {
2256 go_left = 1;
2257 left_start = hint - 1;
2258 }
2259 }
2260 if (go_left == 0)
2261 goto skip_left;
2262
2263 while (test_bit(left_start, free_i->free_secmap)) {
2264 if (left_start > 0) {
2265 left_start--;
2266 continue;
2267 }
2268 left_start = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002269 MAIN_SECS(sbi), 0);
2270 f2fs_bug_on(sbi, left_start >= MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002271 break;
2272 }
2273 secno = left_start;
2274skip_left:
Jaegeuk Kim4ddb1a42017-04-07 15:08:17 -07002275 segno = GET_SEG_FROM_SEC(sbi, secno);
2276 zoneno = GET_ZONE_FROM_SEC(sbi, secno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002277
2278 /* give up on finding another zone */
2279 if (!init)
2280 goto got_it;
2281 if (sbi->secs_per_zone == 1)
2282 goto got_it;
2283 if (zoneno == old_zoneno)
2284 goto got_it;
2285 if (dir == ALLOC_LEFT) {
2286 if (!go_left && zoneno + 1 >= total_zones)
2287 goto got_it;
2288 if (go_left && zoneno == 0)
2289 goto got_it;
2290 }
2291 for (i = 0; i < NR_CURSEG_TYPE; i++)
2292 if (CURSEG_I(sbi, i)->zone == zoneno)
2293 break;
2294
2295 if (i < NR_CURSEG_TYPE) {
2296 /* zone is in user, try another */
2297 if (go_left)
2298 hint = zoneno * sbi->secs_per_zone - 1;
2299 else if (zoneno + 1 >= total_zones)
2300 hint = 0;
2301 else
2302 hint = (zoneno + 1) * sbi->secs_per_zone;
2303 init = false;
2304 goto find_other_zone;
2305 }
2306got_it:
2307 /* set it as dirty segment in free segmap */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002308 f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002309 __set_inuse(sbi, segno);
2310 *newseg = segno;
Chao Yu1a118cc2015-02-11 18:20:38 +08002311 spin_unlock(&free_i->segmap_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002312}
2313
2314static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
2315{
2316 struct curseg_info *curseg = CURSEG_I(sbi, type);
2317 struct summary_footer *sum_footer;
2318
2319 curseg->segno = curseg->next_segno;
Jaegeuk Kim4ddb1a42017-04-07 15:08:17 -07002320 curseg->zone = GET_ZONE_FROM_SEG(sbi, curseg->segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002321 curseg->next_blkoff = 0;
2322 curseg->next_segno = NULL_SEGNO;
2323
2324 sum_footer = &(curseg->sum_blk->footer);
2325 memset(sum_footer, 0, sizeof(struct summary_footer));
2326 if (IS_DATASEG(type))
2327 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
2328 if (IS_NODESEG(type))
2329 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
2330 __set_sit_entry_type(sbi, type, curseg->segno, modified);
2331}
2332
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -04002333static unsigned int __get_next_segno(struct f2fs_sb_info *sbi, int type)
2334{
Jaegeuk Kima7881892017-04-20 13:51:57 -07002335 /* if segs_per_sec is large than 1, we need to keep original policy. */
2336 if (sbi->segs_per_sec != 1)
2337 return CURSEG_I(sbi, type)->segno;
2338
Yunlong Songb94929d2018-01-29 11:37:45 +08002339 if (test_opt(sbi, NOHEAP) &&
2340 (type == CURSEG_HOT_DATA || IS_NODESEG(type)))
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -04002341 return 0;
2342
Jaegeuk Kime066b832017-04-13 15:17:00 -07002343 if (SIT_I(sbi)->last_victim[ALLOC_NEXT])
2344 return SIT_I(sbi)->last_victim[ALLOC_NEXT];
Jaegeuk Kim07939622018-02-18 08:50:49 -08002345
2346 /* find segments from 0 to reuse freed segments */
Chao Yu63189b72018-03-08 14:22:56 +08002347 if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_REUSE)
Jaegeuk Kim07939622018-02-18 08:50:49 -08002348 return 0;
2349
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -04002350 return CURSEG_I(sbi, type)->segno;
2351}
2352
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002353/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002354 * Allocate a current working segment.
2355 * This function always allocates a free segment in LFS manner.
2356 */
2357static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
2358{
2359 struct curseg_info *curseg = CURSEG_I(sbi, type);
2360 unsigned int segno = curseg->segno;
2361 int dir = ALLOC_LEFT;
2362
2363 write_sum_page(sbi, curseg->sum_blk,
Haicheng Li81fb5e82013-05-14 18:20:28 +08002364 GET_SUM_BLOCK(sbi, segno));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002365 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
2366 dir = ALLOC_RIGHT;
2367
2368 if (test_opt(sbi, NOHEAP))
2369 dir = ALLOC_RIGHT;
2370
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -04002371 segno = __get_next_segno(sbi, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002372 get_new_segment(sbi, &segno, new_sec, dir);
2373 curseg->next_segno = segno;
2374 reset_curseg(sbi, type, 1);
2375 curseg->alloc_type = LFS;
2376}
2377
2378static void __next_free_blkoff(struct f2fs_sb_info *sbi,
2379 struct curseg_info *seg, block_t start)
2380{
2381 struct seg_entry *se = get_seg_entry(sbi, seg->segno);
Changman Leee81c93c2013-11-15 13:21:16 +09002382 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
Jaegeuk Kim60a3b782015-02-10 16:44:29 -08002383 unsigned long *target_map = SIT_I(sbi)->tmp_map;
Changman Leee81c93c2013-11-15 13:21:16 +09002384 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
2385 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
2386 int i, pos;
2387
2388 for (i = 0; i < entries; i++)
2389 target_map[i] = ckpt_map[i] | cur_map[i];
2390
2391 pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
2392
2393 seg->next_blkoff = pos;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002394}
2395
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002396/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002397 * If a segment is written by LFS manner, next block offset is just obtained
2398 * by increasing the current block offset. However, if a segment is written by
2399 * SSR manner, next block offset obtained by calling __next_free_blkoff
2400 */
2401static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
2402 struct curseg_info *seg)
2403{
2404 if (seg->alloc_type == SSR)
2405 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
2406 else
2407 seg->next_blkoff++;
2408}
2409
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002410/*
arter97e1c42042014-08-06 23:22:50 +09002411 * This function always allocates a used segment(from dirty seglist) by SSR
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002412 * manner, so it should recover the existing segment information of valid blocks
2413 */
Chao Yu025d63a2017-08-30 18:04:48 +08002414static void change_curseg(struct f2fs_sb_info *sbi, int type)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002415{
2416 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2417 struct curseg_info *curseg = CURSEG_I(sbi, type);
2418 unsigned int new_segno = curseg->next_segno;
2419 struct f2fs_summary_block *sum_node;
2420 struct page *sum_page;
2421
2422 write_sum_page(sbi, curseg->sum_blk,
2423 GET_SUM_BLOCK(sbi, curseg->segno));
2424 __set_test_and_inuse(sbi, new_segno);
2425
2426 mutex_lock(&dirty_i->seglist_lock);
2427 __remove_dirty_segment(sbi, new_segno, PRE);
2428 __remove_dirty_segment(sbi, new_segno, DIRTY);
2429 mutex_unlock(&dirty_i->seglist_lock);
2430
2431 reset_curseg(sbi, type, 1);
2432 curseg->alloc_type = SSR;
2433 __next_free_blkoff(sbi, curseg, 0);
2434
Chao Yu4d57b862018-05-30 00:20:41 +08002435 sum_page = f2fs_get_sum_page(sbi, new_segno);
Chao Yu025d63a2017-08-30 18:04:48 +08002436 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
2437 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
2438 f2fs_put_page(sum_page, 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002439}
2440
Jaegeuk Kim43727522013-02-04 15:11:17 +09002441static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
2442{
2443 struct curseg_info *curseg = CURSEG_I(sbi, type);
2444 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
Jaegeuk Kime066b832017-04-13 15:17:00 -07002445 unsigned segno = NULL_SEGNO;
Chao Yud27c3d82017-02-24 18:46:00 +08002446 int i, cnt;
2447 bool reversed = false;
Jaegeuk Kimc192f7a2017-02-22 17:10:18 -08002448
Chao Yu4d57b862018-05-30 00:20:41 +08002449 /* f2fs_need_SSR() already forces to do this */
Jaegeuk Kime066b832017-04-13 15:17:00 -07002450 if (v_ops->get_victim(sbi, &segno, BG_GC, type, SSR)) {
2451 curseg->next_segno = segno;
Jaegeuk Kimc192f7a2017-02-22 17:10:18 -08002452 return 1;
Jaegeuk Kime066b832017-04-13 15:17:00 -07002453 }
Jaegeuk Kim43727522013-02-04 15:11:17 +09002454
Jaegeuk Kim70d625c2017-02-22 17:02:32 -08002455 /* For node segments, let's do SSR more intensively */
2456 if (IS_NODESEG(type)) {
Chao Yud27c3d82017-02-24 18:46:00 +08002457 if (type >= CURSEG_WARM_NODE) {
2458 reversed = true;
2459 i = CURSEG_COLD_NODE;
2460 } else {
2461 i = CURSEG_HOT_NODE;
2462 }
2463 cnt = NR_CURSEG_NODE_TYPE;
Jaegeuk Kim70d625c2017-02-22 17:02:32 -08002464 } else {
Chao Yud27c3d82017-02-24 18:46:00 +08002465 if (type >= CURSEG_WARM_DATA) {
2466 reversed = true;
2467 i = CURSEG_COLD_DATA;
2468 } else {
2469 i = CURSEG_HOT_DATA;
2470 }
2471 cnt = NR_CURSEG_DATA_TYPE;
Jaegeuk Kim70d625c2017-02-22 17:02:32 -08002472 }
Jaegeuk Kim43727522013-02-04 15:11:17 +09002473
Chao Yud27c3d82017-02-24 18:46:00 +08002474 for (; cnt-- > 0; reversed ? i-- : i++) {
Jaegeuk Kimc192f7a2017-02-22 17:10:18 -08002475 if (i == type)
2476 continue;
Jaegeuk Kime066b832017-04-13 15:17:00 -07002477 if (v_ops->get_victim(sbi, &segno, BG_GC, i, SSR)) {
2478 curseg->next_segno = segno;
Jaegeuk Kim43727522013-02-04 15:11:17 +09002479 return 1;
Jaegeuk Kime066b832017-04-13 15:17:00 -07002480 }
Jaegeuk Kimc192f7a2017-02-22 17:10:18 -08002481 }
Jaegeuk Kim43727522013-02-04 15:11:17 +09002482 return 0;
2483}
2484
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002485/*
2486 * flush out current segment and replace it with new segment
2487 * This function should be returned with success, otherwise BUG
2488 */
2489static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
2490 int type, bool force)
2491{
Jaegeuk Kima7881892017-04-20 13:51:57 -07002492 struct curseg_info *curseg = CURSEG_I(sbi, type);
2493
Gu Zheng7b405272013-08-19 09:41:15 +08002494 if (force)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002495 new_curseg(sbi, type, true);
Jaegeuk Kim5b6c6be2017-02-14 19:32:51 -08002496 else if (!is_set_ckpt_flags(sbi, CP_CRC_RECOVERY_FLAG) &&
2497 type == CURSEG_WARM_NODE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002498 new_curseg(sbi, type, false);
Jaegeuk Kima7881892017-04-20 13:51:57 -07002499 else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
2500 new_curseg(sbi, type, false);
Chao Yu4d57b862018-05-30 00:20:41 +08002501 else if (f2fs_need_SSR(sbi) && get_ssr_segment(sbi, type))
Chao Yu025d63a2017-08-30 18:04:48 +08002502 change_curseg(sbi, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002503 else
2504 new_curseg(sbi, type, false);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +09002505
Jaegeuk Kima7881892017-04-20 13:51:57 -07002506 stat_inc_seg_type(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002507}
2508
Chao Yu4d57b862018-05-30 00:20:41 +08002509void f2fs_allocate_new_segments(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002510{
Jaegeuk Kim6ae1be12016-11-11 12:31:40 -08002511 struct curseg_info *curseg;
2512 unsigned int old_segno;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002513 int i;
2514
Chao Yu3d26fa62017-10-30 17:49:53 +08002515 down_write(&SIT_I(sbi)->sentry_lock);
2516
Jaegeuk Kim6ae1be12016-11-11 12:31:40 -08002517 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
2518 curseg = CURSEG_I(sbi, i);
2519 old_segno = curseg->segno;
2520 SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
2521 locate_dirty_segment(sbi, old_segno);
2522 }
Chao Yu3d26fa62017-10-30 17:49:53 +08002523
2524 up_write(&SIT_I(sbi)->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002525}
2526
2527static const struct segment_allocation default_salloc_ops = {
2528 .allocate_segment = allocate_segment_by_default,
2529};
2530
Chao Yu4d57b862018-05-30 00:20:41 +08002531bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi,
2532 struct cp_control *cpc)
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08002533{
2534 __u64 trim_start = cpc->trim_start;
2535 bool has_candidate = false;
2536
Chao Yu3d26fa62017-10-30 17:49:53 +08002537 down_write(&SIT_I(sbi)->sentry_lock);
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08002538 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++) {
2539 if (add_discard_addrs(sbi, cpc, true)) {
2540 has_candidate = true;
2541 break;
2542 }
2543 }
Chao Yu3d26fa62017-10-30 17:49:53 +08002544 up_write(&SIT_I(sbi)->sentry_lock);
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08002545
2546 cpc->trim_start = trim_start;
2547 return has_candidate;
2548}
2549
Chao Yu01f9cf62018-06-25 20:33:24 +08002550static unsigned int __issue_discard_cmd_range(struct f2fs_sb_info *sbi,
Jaegeuk Kim9a997182018-05-24 13:57:26 -07002551 struct discard_policy *dpolicy,
2552 unsigned int start, unsigned int end)
2553{
2554 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
2555 struct discard_cmd *prev_dc = NULL, *next_dc = NULL;
2556 struct rb_node **insert_p = NULL, *insert_parent = NULL;
2557 struct discard_cmd *dc;
2558 struct blk_plug plug;
2559 int issued;
Chao Yu01f9cf62018-06-25 20:33:24 +08002560 unsigned int trimmed = 0;
Jaegeuk Kim9a997182018-05-24 13:57:26 -07002561
2562next:
2563 issued = 0;
2564
2565 mutex_lock(&dcc->cmd_lock);
Chao Yu67fce702018-06-22 16:06:59 +08002566 if (unlikely(dcc->rbtree_check))
2567 f2fs_bug_on(sbi, !f2fs_check_rb_tree_consistence(sbi,
2568 &dcc->root));
Jaegeuk Kim9a997182018-05-24 13:57:26 -07002569
Chao Yu4d57b862018-05-30 00:20:41 +08002570 dc = (struct discard_cmd *)f2fs_lookup_rb_tree_ret(&dcc->root,
Jaegeuk Kim9a997182018-05-24 13:57:26 -07002571 NULL, start,
2572 (struct rb_entry **)&prev_dc,
2573 (struct rb_entry **)&next_dc,
2574 &insert_p, &insert_parent, true);
2575 if (!dc)
2576 dc = next_dc;
2577
2578 blk_start_plug(&plug);
2579
2580 while (dc && dc->lstart <= end) {
2581 struct rb_node *node;
Chao Yu6b9cb122018-08-08 10:14:55 +08002582 int err = 0;
Jaegeuk Kim9a997182018-05-24 13:57:26 -07002583
2584 if (dc->len < dpolicy->granularity)
2585 goto skip;
2586
2587 if (dc->state != D_PREP) {
2588 list_move_tail(&dc->list, &dcc->fstrim_list);
2589 goto skip;
2590 }
2591
Chao Yu6b9cb122018-08-08 10:14:55 +08002592 err = __submit_discard_cmd(sbi, dpolicy, dc, &issued);
Jaegeuk Kim9a997182018-05-24 13:57:26 -07002593
Chao Yu35ec7d52018-08-06 22:43:50 +08002594 if (issued >= dpolicy->max_requests) {
Jaegeuk Kim9a997182018-05-24 13:57:26 -07002595 start = dc->lstart + dc->len;
2596
Chao Yu6b9cb122018-08-08 10:14:55 +08002597 if (err)
2598 __remove_discard_cmd(sbi, dc);
2599
Jaegeuk Kim9a997182018-05-24 13:57:26 -07002600 blk_finish_plug(&plug);
2601 mutex_unlock(&dcc->cmd_lock);
Chao Yu01f9cf62018-06-25 20:33:24 +08002602 trimmed += __wait_all_discard_cmd(sbi, NULL);
Jaegeuk Kim9a997182018-05-24 13:57:26 -07002603 congestion_wait(BLK_RW_ASYNC, HZ/50);
2604 goto next;
2605 }
2606skip:
2607 node = rb_next(&dc->rb_node);
Chao Yu6b9cb122018-08-08 10:14:55 +08002608 if (err)
2609 __remove_discard_cmd(sbi, dc);
Jaegeuk Kim9a997182018-05-24 13:57:26 -07002610 dc = rb_entry_safe(node, struct discard_cmd, rb_node);
2611
2612 if (fatal_signal_pending(current))
2613 break;
2614 }
2615
2616 blk_finish_plug(&plug);
2617 mutex_unlock(&dcc->cmd_lock);
Chao Yu01f9cf62018-06-25 20:33:24 +08002618
2619 return trimmed;
Jaegeuk Kim9a997182018-05-24 13:57:26 -07002620}
2621
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002622int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range)
2623{
Jaegeuk Kimf7ef9b82015-02-09 12:02:44 -08002624 __u64 start = F2FS_BYTES_TO_BLK(range->start);
2625 __u64 end = start + F2FS_BYTES_TO_BLK(range->len) - 1;
Chao Yu377224c2018-04-09 10:25:23 +08002626 unsigned int start_segno, end_segno;
Chao Yu84126632017-10-04 09:08:32 +08002627 block_t start_block, end_block;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002628 struct cp_control cpc;
Chao Yu78997b52017-10-04 09:08:34 +08002629 struct discard_policy dpolicy;
Chao Yu0ea80512017-10-28 16:52:32 +08002630 unsigned long long trimmed = 0;
Chao Yuc34f42e2015-12-23 17:50:30 +08002631 int err = 0;
Yunlong Songad6672b2018-07-19 20:58:15 +08002632 bool need_align = test_opt(sbi, LFS) && sbi->segs_per_sec > 1;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002633
Jaegeuk Kim836b5a62015-04-30 22:50:06 -07002634 if (start >= MAX_BLKADDR(sbi) || range->len < sbi->blocksize)
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002635 return -EINVAL;
2636
Chao Yu3f16ecd92018-08-08 17:36:29 +08002637 if (end < MAIN_BLKADDR(sbi))
2638 goto out;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002639
Yunlei Heed214a12016-09-01 10:14:39 +08002640 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) {
2641 f2fs_msg(sbi->sb, KERN_WARNING,
2642 "Found FS corruption, run fsck to fix.");
Chao Yu3d165dc2018-04-08 20:39:03 +08002643 return -EIO;
Yunlei Heed214a12016-09-01 10:14:39 +08002644 }
2645
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002646 /* start/end segment number in main_area */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002647 start_segno = (start <= MAIN_BLKADDR(sbi)) ? 0 : GET_SEGNO(sbi, start);
2648 end_segno = (end >= MAX_BLKADDR(sbi)) ? MAIN_SEGS(sbi) - 1 :
2649 GET_SEGNO(sbi, end);
Yunlong Songad6672b2018-07-19 20:58:15 +08002650 if (need_align) {
2651 start_segno = rounddown(start_segno, sbi->segs_per_sec);
2652 end_segno = roundup(end_segno + 1, sbi->segs_per_sec) - 1;
2653 }
Chao Yu84126632017-10-04 09:08:32 +08002654
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002655 cpc.reason = CP_DISCARD;
Jaegeuk Kim836b5a62015-04-30 22:50:06 -07002656 cpc.trim_minlen = max_t(__u64, 1, F2FS_BYTES_TO_BLK(range->minlen));
Chao Yu377224c2018-04-09 10:25:23 +08002657 cpc.trim_start = start_segno;
2658 cpc.trim_end = end_segno;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002659
Chao Yu377224c2018-04-09 10:25:23 +08002660 if (sbi->discard_blks == 0)
2661 goto out;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07002662
Chao Yu377224c2018-04-09 10:25:23 +08002663 mutex_lock(&sbi->gc_mutex);
Chao Yu4d57b862018-05-30 00:20:41 +08002664 err = f2fs_write_checkpoint(sbi, &cpc);
Chao Yu377224c2018-04-09 10:25:23 +08002665 mutex_unlock(&sbi->gc_mutex);
2666 if (err)
2667 goto out;
Chao Yu84126632017-10-04 09:08:32 +08002668
Jaegeuk Kime555da92018-05-31 10:20:48 -07002669 /*
2670 * We filed discard candidates, but actually we don't need to wait for
2671 * all of them, since they'll be issued in idle time along with runtime
2672 * discard option. User configuration looks like using runtime discard
2673 * or periodic fstrim instead of it.
2674 */
Chao Yu7d20c8a2018-09-04 03:52:17 +08002675 if (f2fs_realtime_discard_enable(sbi))
Jaegeuk Kim5a615492018-06-20 21:27:21 -07002676 goto out;
2677
2678 start_block = START_BLOCK(sbi, start_segno);
2679 end_block = START_BLOCK(sbi, end_segno + 1);
2680
2681 __init_discard_policy(sbi, &dpolicy, DPOLICY_FSTRIM, cpc.trim_minlen);
Chao Yu01f9cf62018-06-25 20:33:24 +08002682 trimmed = __issue_discard_cmd_range(sbi, &dpolicy,
2683 start_block, end_block);
Jaegeuk Kim5a615492018-06-20 21:27:21 -07002684
Chao Yu01f9cf62018-06-25 20:33:24 +08002685 trimmed += __wait_discard_cmd_range(sbi, &dpolicy,
Chao Yu0ea80512017-10-28 16:52:32 +08002686 start_block, end_block);
Chao Yu377224c2018-04-09 10:25:23 +08002687out:
Chao Yu6eae2692018-08-05 23:09:00 +08002688 if (!err)
2689 range->len = F2FS_BLK_TO_BYTES(trimmed);
Chao Yuc34f42e2015-12-23 17:50:30 +08002690 return err;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002691}
2692
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002693static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
2694{
2695 struct curseg_info *curseg = CURSEG_I(sbi, type);
2696 if (curseg->next_blkoff < sbi->blocks_per_seg)
2697 return true;
2698 return false;
2699}
2700
Chao Yu4d57b862018-05-30 00:20:41 +08002701int f2fs_rw_hint_to_seg_type(enum rw_hint hint)
Hyunchul Lee4f0a03d32017-11-09 14:51:27 +09002702{
2703 switch (hint) {
2704 case WRITE_LIFE_SHORT:
2705 return CURSEG_HOT_DATA;
2706 case WRITE_LIFE_EXTREME:
2707 return CURSEG_COLD_DATA;
2708 default:
2709 return CURSEG_WARM_DATA;
2710 }
2711}
2712
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09002713/* This returns write hints for each segment type. This hints will be
2714 * passed down to block layer. There are mapping tables which depend on
2715 * the mount option 'whint_mode'.
2716 *
2717 * 1) whint_mode=off. F2FS only passes down WRITE_LIFE_NOT_SET.
2718 *
2719 * 2) whint_mode=user-based. F2FS tries to pass down hints given by users.
2720 *
2721 * User F2FS Block
2722 * ---- ---- -----
2723 * META WRITE_LIFE_NOT_SET
2724 * HOT_NODE "
2725 * WARM_NODE "
2726 * COLD_NODE "
2727 * ioctl(COLD) COLD_DATA WRITE_LIFE_EXTREME
2728 * extension list " "
2729 *
2730 * -- buffered io
2731 * WRITE_LIFE_EXTREME COLD_DATA WRITE_LIFE_EXTREME
2732 * WRITE_LIFE_SHORT HOT_DATA WRITE_LIFE_SHORT
2733 * WRITE_LIFE_NOT_SET WARM_DATA WRITE_LIFE_NOT_SET
2734 * WRITE_LIFE_NONE " "
2735 * WRITE_LIFE_MEDIUM " "
2736 * WRITE_LIFE_LONG " "
2737 *
2738 * -- direct io
2739 * WRITE_LIFE_EXTREME COLD_DATA WRITE_LIFE_EXTREME
2740 * WRITE_LIFE_SHORT HOT_DATA WRITE_LIFE_SHORT
2741 * WRITE_LIFE_NOT_SET WARM_DATA WRITE_LIFE_NOT_SET
2742 * WRITE_LIFE_NONE " WRITE_LIFE_NONE
2743 * WRITE_LIFE_MEDIUM " WRITE_LIFE_MEDIUM
2744 * WRITE_LIFE_LONG " WRITE_LIFE_LONG
2745 *
Hyunchul Leef2e703f2018-01-31 11:36:58 +09002746 * 3) whint_mode=fs-based. F2FS passes down hints with its policy.
2747 *
2748 * User F2FS Block
2749 * ---- ---- -----
2750 * META WRITE_LIFE_MEDIUM;
2751 * HOT_NODE WRITE_LIFE_NOT_SET
2752 * WARM_NODE "
2753 * COLD_NODE WRITE_LIFE_NONE
2754 * ioctl(COLD) COLD_DATA WRITE_LIFE_EXTREME
2755 * extension list " "
2756 *
2757 * -- buffered io
2758 * WRITE_LIFE_EXTREME COLD_DATA WRITE_LIFE_EXTREME
2759 * WRITE_LIFE_SHORT HOT_DATA WRITE_LIFE_SHORT
2760 * WRITE_LIFE_NOT_SET WARM_DATA WRITE_LIFE_LONG
2761 * WRITE_LIFE_NONE " "
2762 * WRITE_LIFE_MEDIUM " "
2763 * WRITE_LIFE_LONG " "
2764 *
2765 * -- direct io
2766 * WRITE_LIFE_EXTREME COLD_DATA WRITE_LIFE_EXTREME
2767 * WRITE_LIFE_SHORT HOT_DATA WRITE_LIFE_SHORT
2768 * WRITE_LIFE_NOT_SET WARM_DATA WRITE_LIFE_NOT_SET
2769 * WRITE_LIFE_NONE " WRITE_LIFE_NONE
2770 * WRITE_LIFE_MEDIUM " WRITE_LIFE_MEDIUM
2771 * WRITE_LIFE_LONG " WRITE_LIFE_LONG
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09002772 */
2773
Chao Yu4d57b862018-05-30 00:20:41 +08002774enum rw_hint f2fs_io_type_to_rw_hint(struct f2fs_sb_info *sbi,
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09002775 enum page_type type, enum temp_type temp)
2776{
Chao Yu63189b72018-03-08 14:22:56 +08002777 if (F2FS_OPTION(sbi).whint_mode == WHINT_MODE_USER) {
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09002778 if (type == DATA) {
Hyunchul Leef2e703f2018-01-31 11:36:58 +09002779 if (temp == WARM)
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09002780 return WRITE_LIFE_NOT_SET;
Hyunchul Leef2e703f2018-01-31 11:36:58 +09002781 else if (temp == HOT)
2782 return WRITE_LIFE_SHORT;
2783 else if (temp == COLD)
2784 return WRITE_LIFE_EXTREME;
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09002785 } else {
2786 return WRITE_LIFE_NOT_SET;
2787 }
Chao Yu63189b72018-03-08 14:22:56 +08002788 } else if (F2FS_OPTION(sbi).whint_mode == WHINT_MODE_FS) {
Hyunchul Leef2e703f2018-01-31 11:36:58 +09002789 if (type == DATA) {
2790 if (temp == WARM)
2791 return WRITE_LIFE_LONG;
2792 else if (temp == HOT)
2793 return WRITE_LIFE_SHORT;
2794 else if (temp == COLD)
2795 return WRITE_LIFE_EXTREME;
2796 } else if (type == NODE) {
2797 if (temp == WARM || temp == HOT)
2798 return WRITE_LIFE_NOT_SET;
2799 else if (temp == COLD)
2800 return WRITE_LIFE_NONE;
2801 } else if (type == META) {
2802 return WRITE_LIFE_MEDIUM;
2803 }
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09002804 }
Hyunchul Leef2e703f2018-01-31 11:36:58 +09002805 return WRITE_LIFE_NOT_SET;
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09002806}
2807
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002808static int __get_segment_type_2(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002809{
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002810 if (fio->type == DATA)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002811 return CURSEG_HOT_DATA;
2812 else
2813 return CURSEG_HOT_NODE;
2814}
2815
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002816static int __get_segment_type_4(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002817{
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002818 if (fio->type == DATA) {
2819 struct inode *inode = fio->page->mapping->host;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002820
2821 if (S_ISDIR(inode->i_mode))
2822 return CURSEG_HOT_DATA;
2823 else
2824 return CURSEG_COLD_DATA;
2825 } else {
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002826 if (IS_DNODE(fio->page) && is_cold_node(fio->page))
Jaegeuk Kima344b9f2014-11-05 20:05:53 -08002827 return CURSEG_WARM_NODE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002828 else
2829 return CURSEG_COLD_NODE;
2830 }
2831}
2832
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002833static int __get_segment_type_6(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002834{
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002835 if (fio->type == DATA) {
2836 struct inode *inode = fio->page->mapping->host;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002837
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002838 if (is_cold_data(fio->page) || file_is_cold(inode))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002839 return CURSEG_COLD_DATA;
Chao Yub6a06cb2018-02-28 17:07:27 +08002840 if (file_is_hot(inode) ||
Chao Yub4c3ca82018-04-26 17:05:50 +08002841 is_inode_flag_set(inode, FI_HOT_DATA) ||
Chao Yu2079f112018-07-17 20:41:48 +08002842 f2fs_is_atomic_file(inode) ||
2843 f2fs_is_volatile_file(inode))
Jaegeuk Kimef095d12017-03-24 20:05:13 -04002844 return CURSEG_HOT_DATA;
Chao Yu4d57b862018-05-30 00:20:41 +08002845 return f2fs_rw_hint_to_seg_type(inode->i_write_hint);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002846 } else {
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002847 if (IS_DNODE(fio->page))
2848 return is_cold_node(fio->page) ? CURSEG_WARM_NODE :
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002849 CURSEG_HOT_NODE;
Jaegeuk Kimef095d12017-03-24 20:05:13 -04002850 return CURSEG_COLD_NODE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002851 }
2852}
2853
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002854static int __get_segment_type(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002855{
Jaegeuk Kima912b542017-05-10 11:18:25 -07002856 int type = 0;
2857
Chao Yu63189b72018-03-08 14:22:56 +08002858 switch (F2FS_OPTION(fio->sbi).active_logs) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002859 case 2:
Jaegeuk Kima912b542017-05-10 11:18:25 -07002860 type = __get_segment_type_2(fio);
2861 break;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002862 case 4:
Jaegeuk Kima912b542017-05-10 11:18:25 -07002863 type = __get_segment_type_4(fio);
2864 break;
2865 case 6:
2866 type = __get_segment_type_6(fio);
2867 break;
2868 default:
2869 f2fs_bug_on(fio->sbi, true);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002870 }
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002871
Jaegeuk Kima912b542017-05-10 11:18:25 -07002872 if (IS_HOT(type))
2873 fio->temp = HOT;
2874 else if (IS_WARM(type))
2875 fio->temp = WARM;
2876 else
2877 fio->temp = COLD;
2878 return type;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002879}
2880
Chao Yu4d57b862018-05-30 00:20:41 +08002881void f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09002882 block_t old_blkaddr, block_t *new_blkaddr,
Chao Yufb830fc2017-05-19 23:37:01 +08002883 struct f2fs_summary *sum, int type,
2884 struct f2fs_io_info *fio, bool add_list)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002885{
2886 struct sit_info *sit_i = SIT_I(sbi);
Jaegeuk Kim6ae1be12016-11-11 12:31:40 -08002887 struct curseg_info *curseg = CURSEG_I(sbi, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002888
Chao Yu2b603112017-11-02 20:41:03 +08002889 down_read(&SM_I(sbi)->curseg_lock);
2890
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002891 mutex_lock(&curseg->curseg_mutex);
Chao Yu3d26fa62017-10-30 17:49:53 +08002892 down_write(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002893
2894 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002895
Jaegeuk Kim4e6a8d92016-12-29 14:07:53 -08002896 f2fs_wait_discard_bio(sbi, *new_blkaddr);
2897
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002898 /*
2899 * __add_sum_entry should be resided under the curseg_mutex
2900 * because, this function updates a summary entry in the
2901 * current summary block.
2902 */
Haicheng Lie79efe32013-06-13 16:59:27 +08002903 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002904
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002905 __refresh_next_blkoff(sbi, curseg);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +09002906
2907 stat_inc_block_count(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002908
Yunlong Song65f1b802017-10-30 09:33:41 +08002909 /*
2910 * SIT information should be updated before segment allocation,
2911 * since SSR needs latest valid block information.
2912 */
2913 update_sit_entry(sbi, *new_blkaddr, 1);
2914 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
2915 update_sit_entry(sbi, old_blkaddr, -1);
2916
Yunlong Song3436c4b2017-02-21 16:59:26 +08002917 if (!__has_curseg_space(sbi, type))
2918 sit_i->s_ops->allocate_segment(sbi, type, false);
Yunlong Song65f1b802017-10-30 09:33:41 +08002919
Jaegeuk Kimc6f82fe92017-04-04 16:45:30 -07002920 /*
Yunlong Song65f1b802017-10-30 09:33:41 +08002921 * segment dirty status should be updated after segment allocation,
2922 * so we just need to update status only one time after previous
2923 * segment being closed.
Jaegeuk Kimc6f82fe92017-04-04 16:45:30 -07002924 */
Yunlong Song65f1b802017-10-30 09:33:41 +08002925 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
2926 locate_dirty_segment(sbi, GET_SEGNO(sbi, *new_blkaddr));
Yunlong Song3436c4b2017-02-21 16:59:26 +08002927
Chao Yu3d26fa62017-10-30 17:49:53 +08002928 up_write(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002929
Chao Yu704956e2017-07-31 20:19:09 +08002930 if (page && IS_NODESEG(type)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002931 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
2932
Chao Yu704956e2017-07-31 20:19:09 +08002933 f2fs_inode_chksum_set(sbi, page);
2934 }
2935
Chao Yufb830fc2017-05-19 23:37:01 +08002936 if (add_list) {
2937 struct f2fs_bio_info *io;
2938
2939 INIT_LIST_HEAD(&fio->list);
2940 fio->in_list = true;
Chao Yufe16efe2018-05-28 23:47:18 +08002941 fio->retry = false;
Chao Yufb830fc2017-05-19 23:37:01 +08002942 io = sbi->write_io[fio->type] + fio->temp;
2943 spin_lock(&io->io_lock);
2944 list_add_tail(&fio->list, &io->io_list);
2945 spin_unlock(&io->io_lock);
2946 }
2947
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09002948 mutex_unlock(&curseg->curseg_mutex);
Chao Yu2b603112017-11-02 20:41:03 +08002949
2950 up_read(&SM_I(sbi)->curseg_lock);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09002951}
2952
Chao Yu39d787b2017-09-29 13:59:38 +08002953static void update_device_state(struct f2fs_io_info *fio)
2954{
2955 struct f2fs_sb_info *sbi = fio->sbi;
2956 unsigned int devidx;
2957
2958 if (!sbi->s_ndevs)
2959 return;
2960
2961 devidx = f2fs_target_device_index(sbi, fio->new_blkaddr);
2962
2963 /* update device state for fsync */
Chao Yu4d57b862018-05-30 00:20:41 +08002964 f2fs_set_dirty_device(sbi, fio->ino, devidx, FLUSH_INO);
Chao Yu1228b482017-09-29 13:59:39 +08002965
2966 /* update device state for checkpoint */
2967 if (!f2fs_test_bit(devidx, (char *)&sbi->dirty_device)) {
2968 spin_lock(&sbi->dev_lock);
2969 f2fs_set_bit(devidx, (char *)&sbi->dirty_device);
2970 spin_unlock(&sbi->dev_lock);
2971 }
Chao Yu39d787b2017-09-29 13:59:38 +08002972}
2973
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002974static void do_write_page(struct f2fs_summary *sum, struct f2fs_io_info *fio)
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09002975{
Jaegeuk Kim81377bd2017-05-10 14:19:54 -07002976 int type = __get_segment_type(fio);
Chao Yu107a8052018-05-26 09:00:13 +08002977 bool keep_order = (test_opt(fio->sbi, LFS) && type == CURSEG_COLD_DATA);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09002978
Chao Yu107a8052018-05-26 09:00:13 +08002979 if (keep_order)
2980 down_read(&fio->sbi->io_order_lock);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08002981reallocate:
Chao Yu4d57b862018-05-30 00:20:41 +08002982 f2fs_allocate_data_block(fio->sbi, fio->page, fio->old_blkaddr,
Chao Yufb830fc2017-05-19 23:37:01 +08002983 &fio->new_blkaddr, sum, type, fio, true);
Chao Yu6aa58d82018-08-14 22:37:25 +08002984 if (GET_SEGNO(fio->sbi, fio->old_blkaddr) != NULL_SEGNO)
2985 invalidate_mapping_pages(META_MAPPING(fio->sbi),
2986 fio->old_blkaddr, fio->old_blkaddr);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09002987
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002988 /* writeout dirty page into bdev */
Chao Yufe16efe2018-05-28 23:47:18 +08002989 f2fs_submit_page_write(fio);
2990 if (fio->retry) {
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08002991 fio->old_blkaddr = fio->new_blkaddr;
2992 goto reallocate;
2993 }
Chao Yufe16efe2018-05-28 23:47:18 +08002994
2995 update_device_state(fio);
2996
Chao Yu107a8052018-05-26 09:00:13 +08002997 if (keep_order)
2998 up_read(&fio->sbi->io_order_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002999}
3000
Chao Yu4d57b862018-05-30 00:20:41 +08003001void f2fs_do_write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
Chao Yub0af6d42017-08-02 23:21:48 +08003002 enum iostat_type io_type)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003003{
Jaegeuk Kim458e6192013-12-11 13:54:01 +09003004 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07003005 .sbi = sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09003006 .type = META,
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09003007 .temp = HOT,
Mike Christie04d328d2016-06-05 14:31:55 -05003008 .op = REQ_OP_WRITE,
Christoph Hellwig70fd7612016-11-01 07:40:10 -06003009 .op_flags = REQ_SYNC | REQ_META | REQ_PRIO,
Chao Yu7a9d7542016-02-22 18:36:38 +08003010 .old_blkaddr = page->index,
3011 .new_blkaddr = page->index,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07003012 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07003013 .encrypted_page = NULL,
Chao Yufb830fc2017-05-19 23:37:01 +08003014 .in_list = false,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09003015 };
3016
Chao Yu2b947002015-10-12 17:04:21 +08003017 if (unlikely(page->index >= MAIN_BLKADDR(sbi)))
Mike Christie04d328d2016-06-05 14:31:55 -05003018 fio.op_flags &= ~REQ_META;
Chao Yu2b947002015-10-12 17:04:21 +08003019
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003020 set_page_writeback(page);
Jaegeuk Kim17c50032018-04-11 23:09:04 -07003021 ClearPageError(page);
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07003022 f2fs_submit_page_write(&fio);
Chao Yub0af6d42017-08-02 23:21:48 +08003023
3024 f2fs_update_iostat(sbi, io_type, F2FS_BLKSIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003025}
3026
Chao Yu4d57b862018-05-30 00:20:41 +08003027void f2fs_do_write_node_page(unsigned int nid, struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003028{
3029 struct f2fs_summary sum;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07003030
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003031 set_summary(&sum, nid, 0, 0);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07003032 do_write_page(&sum, fio);
Chao Yub0af6d42017-08-02 23:21:48 +08003033
3034 f2fs_update_iostat(fio->sbi, fio->io_type, F2FS_BLKSIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003035}
3036
Chao Yu4d57b862018-05-30 00:20:41 +08003037void f2fs_outplace_write_data(struct dnode_of_data *dn,
3038 struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003039{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07003040 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003041 struct f2fs_summary sum;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003042
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07003043 f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR);
Chao Yu77357302018-07-17 00:02:17 +08003044 set_summary(&sum, dn->nid, dn->ofs_in_node, fio->version);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07003045 do_write_page(&sum, fio);
Chao Yuf28b3432016-02-24 17:16:47 +08003046 f2fs_update_data_blkaddr(dn, fio->new_blkaddr);
Chao Yub0af6d42017-08-02 23:21:48 +08003047
3048 f2fs_update_iostat(sbi, fio->io_type, F2FS_BLKSIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003049}
3050
Chao Yu4d57b862018-05-30 00:20:41 +08003051int f2fs_inplace_write_data(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003052{
Chao Yub0af6d42017-08-02 23:21:48 +08003053 int err;
Yunlei Hed21b0f22018-03-26 17:32:23 +08003054 struct f2fs_sb_info *sbi = fio->sbi;
Chao Yub0af6d42017-08-02 23:21:48 +08003055
Chao Yu7a9d7542016-02-22 18:36:38 +08003056 fio->new_blkaddr = fio->old_blkaddr;
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09003057 /* i/o temperature is needed for passing down write hints */
3058 __get_segment_type(fio);
Yunlei Hed21b0f22018-03-26 17:32:23 +08003059
3060 f2fs_bug_on(sbi, !IS_DATASEG(get_seg_entry(sbi,
3061 GET_SEGNO(sbi, fio->new_blkaddr))->type));
3062
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07003063 stat_inc_inplace_blocks(fio->sbi);
Chao Yub0af6d42017-08-02 23:21:48 +08003064
3065 err = f2fs_submit_page_bio(fio);
Chao Yu39d787b2017-09-29 13:59:38 +08003066 if (!err)
3067 update_device_state(fio);
Chao Yub0af6d42017-08-02 23:21:48 +08003068
3069 f2fs_update_iostat(fio->sbi, fio->io_type, F2FS_BLKSIZE);
3070
3071 return err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003072}
3073
Chao Yu2b603112017-11-02 20:41:03 +08003074static inline int __f2fs_get_curseg(struct f2fs_sb_info *sbi,
3075 unsigned int segno)
3076{
3077 int i;
3078
3079 for (i = CURSEG_HOT_DATA; i < NO_CHECK_TYPE; i++) {
3080 if (CURSEG_I(sbi, i)->segno == segno)
3081 break;
3082 }
3083 return i;
3084}
3085
Chao Yu4d57b862018-05-30 00:20:41 +08003086void f2fs_do_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
Chao Yu19f106b2015-05-06 13:08:06 +08003087 block_t old_blkaddr, block_t new_blkaddr,
Chao Yu28bc1062016-02-06 14:40:34 +08003088 bool recover_curseg, bool recover_newaddr)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003089{
3090 struct sit_info *sit_i = SIT_I(sbi);
3091 struct curseg_info *curseg;
3092 unsigned int segno, old_cursegno;
3093 struct seg_entry *se;
3094 int type;
Chao Yu19f106b2015-05-06 13:08:06 +08003095 unsigned short old_blkoff;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003096
3097 segno = GET_SEGNO(sbi, new_blkaddr);
3098 se = get_seg_entry(sbi, segno);
3099 type = se->type;
3100
Chao Yu2b603112017-11-02 20:41:03 +08003101 down_write(&SM_I(sbi)->curseg_lock);
3102
Chao Yu19f106b2015-05-06 13:08:06 +08003103 if (!recover_curseg) {
3104 /* for recovery flow */
3105 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
3106 if (old_blkaddr == NULL_ADDR)
3107 type = CURSEG_COLD_DATA;
3108 else
3109 type = CURSEG_WARM_DATA;
3110 }
3111 } else {
Chao Yu2b603112017-11-02 20:41:03 +08003112 if (IS_CURSEG(sbi, segno)) {
3113 /* se->type is volatile as SSR allocation */
3114 type = __f2fs_get_curseg(sbi, segno);
3115 f2fs_bug_on(sbi, type == NO_CHECK_TYPE);
3116 } else {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003117 type = CURSEG_WARM_DATA;
Chao Yu2b603112017-11-02 20:41:03 +08003118 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003119 }
Chao Yu19f106b2015-05-06 13:08:06 +08003120
Yunlong Song2c190502018-01-04 15:02:02 +08003121 f2fs_bug_on(sbi, !IS_DATASEG(type));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003122 curseg = CURSEG_I(sbi, type);
3123
3124 mutex_lock(&curseg->curseg_mutex);
Chao Yu3d26fa62017-10-30 17:49:53 +08003125 down_write(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003126
3127 old_cursegno = curseg->segno;
Chao Yu19f106b2015-05-06 13:08:06 +08003128 old_blkoff = curseg->next_blkoff;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003129
3130 /* change the current segment */
3131 if (segno != curseg->segno) {
3132 curseg->next_segno = segno;
Chao Yu025d63a2017-08-30 18:04:48 +08003133 change_curseg(sbi, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003134 }
3135
Jaegeuk Kim491c0852014-02-04 13:01:10 +09003136 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
Haicheng Lie79efe32013-06-13 16:59:27 +08003137 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003138
Chao Yu28bc1062016-02-06 14:40:34 +08003139 if (!recover_curseg || recover_newaddr)
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07003140 update_sit_entry(sbi, new_blkaddr, 1);
Chao Yu6aa58d82018-08-14 22:37:25 +08003141 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO) {
3142 invalidate_mapping_pages(META_MAPPING(sbi),
3143 old_blkaddr, old_blkaddr);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07003144 update_sit_entry(sbi, old_blkaddr, -1);
Chao Yu6aa58d82018-08-14 22:37:25 +08003145 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07003146
3147 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
3148 locate_dirty_segment(sbi, GET_SEGNO(sbi, new_blkaddr));
3149
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003150 locate_dirty_segment(sbi, old_cursegno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003151
Chao Yu19f106b2015-05-06 13:08:06 +08003152 if (recover_curseg) {
3153 if (old_cursegno != curseg->segno) {
3154 curseg->next_segno = old_cursegno;
Chao Yu025d63a2017-08-30 18:04:48 +08003155 change_curseg(sbi, type);
Chao Yu19f106b2015-05-06 13:08:06 +08003156 }
3157 curseg->next_blkoff = old_blkoff;
3158 }
3159
Chao Yu3d26fa62017-10-30 17:49:53 +08003160 up_write(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003161 mutex_unlock(&curseg->curseg_mutex);
Chao Yu2b603112017-11-02 20:41:03 +08003162 up_write(&SM_I(sbi)->curseg_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003163}
3164
Chao Yu528e3452015-05-28 19:15:35 +08003165void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
3166 block_t old_addr, block_t new_addr,
Chao Yu28bc1062016-02-06 14:40:34 +08003167 unsigned char version, bool recover_curseg,
3168 bool recover_newaddr)
Chao Yu528e3452015-05-28 19:15:35 +08003169{
3170 struct f2fs_summary sum;
3171
3172 set_summary(&sum, dn->nid, dn->ofs_in_node, version);
3173
Chao Yu4d57b862018-05-30 00:20:41 +08003174 f2fs_do_replace_block(sbi, &sum, old_addr, new_addr,
Chao Yu28bc1062016-02-06 14:40:34 +08003175 recover_curseg, recover_newaddr);
Chao Yu528e3452015-05-28 19:15:35 +08003176
Chao Yuf28b3432016-02-24 17:16:47 +08003177 f2fs_update_data_blkaddr(dn, new_addr);
Chao Yu528e3452015-05-28 19:15:35 +08003178}
3179
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09003180void f2fs_wait_on_page_writeback(struct page *page,
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08003181 enum page_type type, bool ordered)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09003182{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09003183 if (PageWriteback(page)) {
Jaegeuk Kim40813632014-09-02 15:31:18 -07003184 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
3185
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07003186 f2fs_submit_merged_write_cond(sbi, page->mapping->host,
3187 0, page->index, type);
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08003188 if (ordered)
3189 wait_on_page_writeback(page);
3190 else
3191 wait_for_stable_page(page);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09003192 }
3193}
3194
Jaegeuk Kim0ded69f2018-08-22 21:18:00 -07003195void f2fs_wait_on_block_writeback(struct inode *inode, block_t blkaddr)
Chao Yu08b39fb2015-10-08 13:27:34 +08003196{
Jaegeuk Kim0ded69f2018-08-22 21:18:00 -07003197 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu08b39fb2015-10-08 13:27:34 +08003198 struct page *cpage;
3199
Jaegeuk Kim0ded69f2018-08-22 21:18:00 -07003200 if (!f2fs_post_read_required(inode))
3201 return;
3202
Chao Yue1da7872018-06-05 17:44:11 +08003203 if (!is_valid_data_blkaddr(sbi, blkaddr))
Chao Yu08b39fb2015-10-08 13:27:34 +08003204 return;
3205
Chao Yu08b39fb2015-10-08 13:27:34 +08003206 cpage = find_lock_page(META_MAPPING(sbi), blkaddr);
3207 if (cpage) {
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08003208 f2fs_wait_on_page_writeback(cpage, DATA, true);
Chao Yu08b39fb2015-10-08 13:27:34 +08003209 f2fs_put_page(cpage, 1);
3210 }
3211}
3212
Chao Yu77357302018-07-17 00:02:17 +08003213static int read_compacted_summaries(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003214{
3215 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
3216 struct curseg_info *seg_i;
3217 unsigned char *kaddr;
3218 struct page *page;
3219 block_t start;
3220 int i, j, offset;
3221
3222 start = start_sum_block(sbi);
3223
Chao Yu4d57b862018-05-30 00:20:41 +08003224 page = f2fs_get_meta_page(sbi, start++);
Chao Yu77357302018-07-17 00:02:17 +08003225 if (IS_ERR(page))
3226 return PTR_ERR(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003227 kaddr = (unsigned char *)page_address(page);
3228
3229 /* Step 1: restore nat cache */
3230 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08003231 memcpy(seg_i->journal, kaddr, SUM_JOURNAL_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003232
3233 /* Step 2: restore sit cache */
3234 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08003235 memcpy(seg_i->journal, kaddr + SUM_JOURNAL_SIZE, SUM_JOURNAL_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003236 offset = 2 * SUM_JOURNAL_SIZE;
3237
3238 /* Step 3: restore summary entries */
3239 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
3240 unsigned short blk_off;
3241 unsigned int segno;
3242
3243 seg_i = CURSEG_I(sbi, i);
3244 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
3245 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
3246 seg_i->next_segno = segno;
3247 reset_curseg(sbi, i, 0);
3248 seg_i->alloc_type = ckpt->alloc_type[i];
3249 seg_i->next_blkoff = blk_off;
3250
3251 if (seg_i->alloc_type == SSR)
3252 blk_off = sbi->blocks_per_seg;
3253
3254 for (j = 0; j < blk_off; j++) {
3255 struct f2fs_summary *s;
3256 s = (struct f2fs_summary *)(kaddr + offset);
3257 seg_i->sum_blk->entries[j] = *s;
3258 offset += SUMMARY_SIZE;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003259 if (offset + SUMMARY_SIZE <= PAGE_SIZE -
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003260 SUM_FOOTER_SIZE)
3261 continue;
3262
3263 f2fs_put_page(page, 1);
3264 page = NULL;
3265
Chao Yu4d57b862018-05-30 00:20:41 +08003266 page = f2fs_get_meta_page(sbi, start++);
Chao Yu77357302018-07-17 00:02:17 +08003267 if (IS_ERR(page))
3268 return PTR_ERR(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003269 kaddr = (unsigned char *)page_address(page);
3270 offset = 0;
3271 }
3272 }
3273 f2fs_put_page(page, 1);
Chao Yu77357302018-07-17 00:02:17 +08003274 return 0;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003275}
3276
3277static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
3278{
3279 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
3280 struct f2fs_summary_block *sum;
3281 struct curseg_info *curseg;
3282 struct page *new;
3283 unsigned short blk_off;
3284 unsigned int segno = 0;
3285 block_t blk_addr = 0;
Chao Yu77357302018-07-17 00:02:17 +08003286 int err = 0;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003287
3288 /* get segment number and block addr */
3289 if (IS_DATASEG(type)) {
3290 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
3291 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
3292 CURSEG_HOT_DATA]);
Jaegeuk Kim119ee912015-01-29 11:45:33 -08003293 if (__exist_node_summaries(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003294 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
3295 else
3296 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
3297 } else {
3298 segno = le32_to_cpu(ckpt->cur_node_segno[type -
3299 CURSEG_HOT_NODE]);
3300 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
3301 CURSEG_HOT_NODE]);
Jaegeuk Kim119ee912015-01-29 11:45:33 -08003302 if (__exist_node_summaries(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003303 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
3304 type - CURSEG_HOT_NODE);
3305 else
3306 blk_addr = GET_SUM_BLOCK(sbi, segno);
3307 }
3308
Chao Yu4d57b862018-05-30 00:20:41 +08003309 new = f2fs_get_meta_page(sbi, blk_addr);
Chao Yu77357302018-07-17 00:02:17 +08003310 if (IS_ERR(new))
3311 return PTR_ERR(new);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003312 sum = (struct f2fs_summary_block *)page_address(new);
3313
3314 if (IS_NODESEG(type)) {
Jaegeuk Kim119ee912015-01-29 11:45:33 -08003315 if (__exist_node_summaries(sbi)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003316 struct f2fs_summary *ns = &sum->entries[0];
3317 int i;
3318 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
3319 ns->version = 0;
3320 ns->ofs_in_node = 0;
3321 }
3322 } else {
Chao Yu77357302018-07-17 00:02:17 +08003323 err = f2fs_restore_node_summary(sbi, segno, sum);
3324 if (err)
3325 goto out;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003326 }
3327 }
3328
3329 /* set uncompleted segment to curseg */
3330 curseg = CURSEG_I(sbi, type);
3331 mutex_lock(&curseg->curseg_mutex);
Chao Yub7ad7512016-02-19 18:08:46 +08003332
3333 /* update journal info */
3334 down_write(&curseg->journal_rwsem);
3335 memcpy(curseg->journal, &sum->journal, SUM_JOURNAL_SIZE);
3336 up_write(&curseg->journal_rwsem);
3337
3338 memcpy(curseg->sum_blk->entries, sum->entries, SUM_ENTRY_SIZE);
3339 memcpy(&curseg->sum_blk->footer, &sum->footer, SUM_FOOTER_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003340 curseg->next_segno = segno;
3341 reset_curseg(sbi, type, 0);
3342 curseg->alloc_type = ckpt->alloc_type[type];
3343 curseg->next_blkoff = blk_off;
3344 mutex_unlock(&curseg->curseg_mutex);
Chao Yu77357302018-07-17 00:02:17 +08003345out:
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003346 f2fs_put_page(new, 1);
Chao Yu77357302018-07-17 00:02:17 +08003347 return err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003348}
3349
3350static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
3351{
Jin Qian21d3f8e2017-06-01 11:18:30 -07003352 struct f2fs_journal *sit_j = CURSEG_I(sbi, CURSEG_COLD_DATA)->journal;
3353 struct f2fs_journal *nat_j = CURSEG_I(sbi, CURSEG_HOT_DATA)->journal;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003354 int type = CURSEG_HOT_DATA;
Chao Yue4fc5fb2014-03-17 16:36:24 +08003355 int err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003356
Chao Yuaaec2b12016-09-20 11:04:18 +08003357 if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG)) {
Chao Yu4d57b862018-05-30 00:20:41 +08003358 int npages = f2fs_npages_for_summary_flush(sbi, true);
Chao Yu3fa06d72014-12-09 14:21:46 +08003359
3360 if (npages >= 2)
Chao Yu4d57b862018-05-30 00:20:41 +08003361 f2fs_ra_meta_pages(sbi, start_sum_block(sbi), npages,
Chao Yu26879fb2015-10-12 17:05:59 +08003362 META_CP, true);
Chao Yu3fa06d72014-12-09 14:21:46 +08003363
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003364 /* restore for compacted data summary */
Chao Yu77357302018-07-17 00:02:17 +08003365 err = read_compacted_summaries(sbi);
3366 if (err)
3367 return err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003368 type = CURSEG_HOT_NODE;
3369 }
3370
Jaegeuk Kim119ee912015-01-29 11:45:33 -08003371 if (__exist_node_summaries(sbi))
Chao Yu4d57b862018-05-30 00:20:41 +08003372 f2fs_ra_meta_pages(sbi, sum_blk_addr(sbi, NR_CURSEG_TYPE, type),
Chao Yu26879fb2015-10-12 17:05:59 +08003373 NR_CURSEG_TYPE - type, META_CP, true);
Chao Yu3fa06d72014-12-09 14:21:46 +08003374
Chao Yue4fc5fb2014-03-17 16:36:24 +08003375 for (; type <= CURSEG_COLD_NODE; type++) {
3376 err = read_normal_summaries(sbi, type);
3377 if (err)
3378 return err;
3379 }
3380
Jin Qian21d3f8e2017-06-01 11:18:30 -07003381 /* sanity check for summary blocks */
3382 if (nats_in_cursum(nat_j) > NAT_JOURNAL_ENTRIES ||
3383 sits_in_cursum(sit_j) > SIT_JOURNAL_ENTRIES)
3384 return -EINVAL;
3385
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003386 return 0;
3387}
3388
3389static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
3390{
3391 struct page *page;
3392 unsigned char *kaddr;
3393 struct f2fs_summary *summary;
3394 struct curseg_info *seg_i;
3395 int written_size = 0;
3396 int i, j;
3397
Chao Yu4d57b862018-05-30 00:20:41 +08003398 page = f2fs_grab_meta_page(sbi, blkaddr++);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003399 kaddr = (unsigned char *)page_address(page);
Chao Yu81114ba2018-04-09 20:25:06 +08003400 memset(kaddr, 0, PAGE_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003401
3402 /* Step 1: write nat cache */
3403 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08003404 memcpy(kaddr, seg_i->journal, SUM_JOURNAL_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003405 written_size += SUM_JOURNAL_SIZE;
3406
3407 /* Step 2: write sit cache */
3408 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08003409 memcpy(kaddr + written_size, seg_i->journal, SUM_JOURNAL_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003410 written_size += SUM_JOURNAL_SIZE;
3411
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003412 /* Step 3: write summary entries */
3413 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
3414 unsigned short blkoff;
3415 seg_i = CURSEG_I(sbi, i);
3416 if (sbi->ckpt->alloc_type[i] == SSR)
3417 blkoff = sbi->blocks_per_seg;
3418 else
3419 blkoff = curseg_blkoff(sbi, i);
3420
3421 for (j = 0; j < blkoff; j++) {
3422 if (!page) {
Chao Yu4d57b862018-05-30 00:20:41 +08003423 page = f2fs_grab_meta_page(sbi, blkaddr++);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003424 kaddr = (unsigned char *)page_address(page);
Chao Yu81114ba2018-04-09 20:25:06 +08003425 memset(kaddr, 0, PAGE_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003426 written_size = 0;
3427 }
3428 summary = (struct f2fs_summary *)(kaddr + written_size);
3429 *summary = seg_i->sum_blk->entries[j];
3430 written_size += SUMMARY_SIZE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003431
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003432 if (written_size + SUMMARY_SIZE <= PAGE_SIZE -
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003433 SUM_FOOTER_SIZE)
3434 continue;
3435
Chao Yue8d61a72013-10-24 15:08:28 +08003436 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003437 f2fs_put_page(page, 1);
3438 page = NULL;
3439 }
3440 }
Chao Yue8d61a72013-10-24 15:08:28 +08003441 if (page) {
3442 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003443 f2fs_put_page(page, 1);
Chao Yue8d61a72013-10-24 15:08:28 +08003444 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003445}
3446
3447static void write_normal_summaries(struct f2fs_sb_info *sbi,
3448 block_t blkaddr, int type)
3449{
3450 int i, end;
3451 if (IS_DATASEG(type))
3452 end = type + NR_CURSEG_DATA_TYPE;
3453 else
3454 end = type + NR_CURSEG_NODE_TYPE;
3455
Chao Yub7ad7512016-02-19 18:08:46 +08003456 for (i = type; i < end; i++)
3457 write_current_sum_page(sbi, i, blkaddr + (i - type));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003458}
3459
Chao Yu4d57b862018-05-30 00:20:41 +08003460void f2fs_write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003461{
Chao Yuaaec2b12016-09-20 11:04:18 +08003462 if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003463 write_compacted_summaries(sbi, start_blk);
3464 else
3465 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
3466}
3467
Chao Yu4d57b862018-05-30 00:20:41 +08003468void f2fs_write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003469{
Jaegeuk Kim119ee912015-01-29 11:45:33 -08003470 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003471}
3472
Chao Yu4d57b862018-05-30 00:20:41 +08003473int f2fs_lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003474 unsigned int val, int alloc)
3475{
3476 int i;
3477
3478 if (type == NAT_JOURNAL) {
Chao Yudfc08a12016-02-14 18:50:40 +08003479 for (i = 0; i < nats_in_cursum(journal); i++) {
3480 if (le32_to_cpu(nid_in_journal(journal, i)) == val)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003481 return i;
3482 }
Chao Yudfc08a12016-02-14 18:50:40 +08003483 if (alloc && __has_cursum_space(journal, 1, NAT_JOURNAL))
3484 return update_nats_in_cursum(journal, 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003485 } else if (type == SIT_JOURNAL) {
Chao Yudfc08a12016-02-14 18:50:40 +08003486 for (i = 0; i < sits_in_cursum(journal); i++)
3487 if (le32_to_cpu(segno_in_journal(journal, i)) == val)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003488 return i;
Chao Yudfc08a12016-02-14 18:50:40 +08003489 if (alloc && __has_cursum_space(journal, 1, SIT_JOURNAL))
3490 return update_sits_in_cursum(journal, 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003491 }
3492 return -1;
3493}
3494
3495static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
3496 unsigned int segno)
3497{
Chao Yu77357302018-07-17 00:02:17 +08003498 return f2fs_get_meta_page_nofail(sbi, current_sit_addr(sbi, segno));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003499}
3500
3501static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
3502 unsigned int start)
3503{
3504 struct sit_info *sit_i = SIT_I(sbi);
Yunlei He068c3cd2018-01-25 17:27:11 +08003505 struct page *page;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003506 pgoff_t src_off, dst_off;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003507
3508 src_off = current_sit_addr(sbi, start);
3509 dst_off = next_sit_addr(sbi, src_off);
3510
Chao Yu4d57b862018-05-30 00:20:41 +08003511 page = f2fs_grab_meta_page(sbi, dst_off);
Yunlei He068c3cd2018-01-25 17:27:11 +08003512 seg_info_to_sit_page(sbi, page, start);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003513
Yunlei He068c3cd2018-01-25 17:27:11 +08003514 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003515 set_to_next_sit(sit_i, start);
3516
Yunlei He068c3cd2018-01-25 17:27:11 +08003517 return page;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003518}
3519
Chao Yu184a5cd2014-09-04 18:13:01 +08003520static struct sit_entry_set *grab_sit_entry_set(void)
3521{
3522 struct sit_entry_set *ses =
Jaegeuk Kim80c54502015-08-20 08:51:56 -07003523 f2fs_kmem_cache_alloc(sit_entry_set_slab, GFP_NOFS);
Chao Yu184a5cd2014-09-04 18:13:01 +08003524
3525 ses->entry_cnt = 0;
3526 INIT_LIST_HEAD(&ses->set_list);
3527 return ses;
3528}
3529
3530static void release_sit_entry_set(struct sit_entry_set *ses)
3531{
3532 list_del(&ses->set_list);
3533 kmem_cache_free(sit_entry_set_slab, ses);
3534}
3535
3536static void adjust_sit_entry_set(struct sit_entry_set *ses,
3537 struct list_head *head)
3538{
3539 struct sit_entry_set *next = ses;
3540
3541 if (list_is_last(&ses->set_list, head))
3542 return;
3543
3544 list_for_each_entry_continue(next, head, set_list)
3545 if (ses->entry_cnt <= next->entry_cnt)
3546 break;
3547
3548 list_move_tail(&ses->set_list, &next->set_list);
3549}
3550
3551static void add_sit_entry(unsigned int segno, struct list_head *head)
3552{
3553 struct sit_entry_set *ses;
3554 unsigned int start_segno = START_SEGNO(segno);
3555
3556 list_for_each_entry(ses, head, set_list) {
3557 if (ses->start_segno == start_segno) {
3558 ses->entry_cnt++;
3559 adjust_sit_entry_set(ses, head);
3560 return;
3561 }
3562 }
3563
3564 ses = grab_sit_entry_set();
3565
3566 ses->start_segno = start_segno;
3567 ses->entry_cnt++;
3568 list_add(&ses->set_list, head);
3569}
3570
3571static void add_sits_in_set(struct f2fs_sb_info *sbi)
3572{
3573 struct f2fs_sm_info *sm_info = SM_I(sbi);
3574 struct list_head *set_list = &sm_info->sit_entry_set;
3575 unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap;
Chao Yu184a5cd2014-09-04 18:13:01 +08003576 unsigned int segno;
3577
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003578 for_each_set_bit(segno, bitmap, MAIN_SEGS(sbi))
Chao Yu184a5cd2014-09-04 18:13:01 +08003579 add_sit_entry(segno, set_list);
3580}
3581
3582static void remove_sits_in_journal(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003583{
3584 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08003585 struct f2fs_journal *journal = curseg->journal;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003586 int i;
3587
Chao Yub7ad7512016-02-19 18:08:46 +08003588 down_write(&curseg->journal_rwsem);
Chao Yudfc08a12016-02-14 18:50:40 +08003589 for (i = 0; i < sits_in_cursum(journal); i++) {
Chao Yu184a5cd2014-09-04 18:13:01 +08003590 unsigned int segno;
3591 bool dirtied;
3592
Chao Yudfc08a12016-02-14 18:50:40 +08003593 segno = le32_to_cpu(segno_in_journal(journal, i));
Chao Yu184a5cd2014-09-04 18:13:01 +08003594 dirtied = __mark_sit_entry_dirty(sbi, segno);
3595
3596 if (!dirtied)
3597 add_sit_entry(segno, &SM_I(sbi)->sit_entry_set);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003598 }
Chao Yudfc08a12016-02-14 18:50:40 +08003599 update_sits_in_cursum(journal, -i);
Chao Yub7ad7512016-02-19 18:08:46 +08003600 up_write(&curseg->journal_rwsem);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003601}
3602
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09003603/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003604 * CP calls this function, which flushes SIT entries including sit_journal,
3605 * and moves prefree segs to free segs.
3606 */
Chao Yu4d57b862018-05-30 00:20:41 +08003607void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003608{
3609 struct sit_info *sit_i = SIT_I(sbi);
3610 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
3611 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08003612 struct f2fs_journal *journal = curseg->journal;
Chao Yu184a5cd2014-09-04 18:13:01 +08003613 struct sit_entry_set *ses, *tmp;
3614 struct list_head *head = &SM_I(sbi)->sit_entry_set;
Chao Yu184a5cd2014-09-04 18:13:01 +08003615 bool to_journal = true;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003616 struct seg_entry *se;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003617
Chao Yu3d26fa62017-10-30 17:49:53 +08003618 down_write(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003619
Wanpeng Li2b11a742015-02-27 16:52:50 +08003620 if (!sit_i->dirty_sentries)
3621 goto out;
3622
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003623 /*
Chao Yu184a5cd2014-09-04 18:13:01 +08003624 * add and account sit entries of dirty bitmap in sit entry
3625 * set temporarily
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003626 */
Chao Yu184a5cd2014-09-04 18:13:01 +08003627 add_sits_in_set(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003628
Chao Yu184a5cd2014-09-04 18:13:01 +08003629 /*
3630 * if there are no enough space in journal to store dirty sit
3631 * entries, remove all entries from journal and add and account
3632 * them in sit entry set.
3633 */
Chao Yudfc08a12016-02-14 18:50:40 +08003634 if (!__has_cursum_space(journal, sit_i->dirty_sentries, SIT_JOURNAL))
Chao Yu184a5cd2014-09-04 18:13:01 +08003635 remove_sits_in_journal(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003636
Chao Yu184a5cd2014-09-04 18:13:01 +08003637 /*
3638 * there are two steps to flush sit entries:
3639 * #1, flush sit entries to journal in current cold data summary block.
3640 * #2, flush sit entries to sit page.
3641 */
3642 list_for_each_entry_safe(ses, tmp, head, set_list) {
Jaegeuk Kim4a257ed2014-10-16 11:43:30 -07003643 struct page *page = NULL;
Chao Yu184a5cd2014-09-04 18:13:01 +08003644 struct f2fs_sit_block *raw_sit = NULL;
3645 unsigned int start_segno = ses->start_segno;
3646 unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003647 (unsigned long)MAIN_SEGS(sbi));
Chao Yu184a5cd2014-09-04 18:13:01 +08003648 unsigned int segno = start_segno;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09003649
Chao Yu184a5cd2014-09-04 18:13:01 +08003650 if (to_journal &&
Chao Yudfc08a12016-02-14 18:50:40 +08003651 !__has_cursum_space(journal, ses->entry_cnt, SIT_JOURNAL))
Chao Yu184a5cd2014-09-04 18:13:01 +08003652 to_journal = false;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003653
Chao Yub7ad7512016-02-19 18:08:46 +08003654 if (to_journal) {
3655 down_write(&curseg->journal_rwsem);
3656 } else {
Chao Yu184a5cd2014-09-04 18:13:01 +08003657 page = get_next_sit_page(sbi, start_segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003658 raw_sit = page_address(page);
3659 }
3660
Chao Yu184a5cd2014-09-04 18:13:01 +08003661 /* flush dirty sit entries in region of current sit set */
3662 for_each_set_bit_from(segno, bitmap, end) {
3663 int offset, sit_offset;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003664
3665 se = get_seg_entry(sbi, segno);
Zhikang Zhang56b07e72018-04-09 04:28:41 +08003666#ifdef CONFIG_F2FS_CHECK_FS
3667 if (memcmp(se->cur_valid_map, se->cur_valid_map_mir,
3668 SIT_VBLOCK_MAP_SIZE))
3669 f2fs_bug_on(sbi, 1);
3670#endif
Chao Yu184a5cd2014-09-04 18:13:01 +08003671
3672 /* add discard candidates */
Chao Yuc473f1a2017-04-27 20:40:39 +08003673 if (!(cpc->reason & CP_DISCARD)) {
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003674 cpc->trim_start = segno;
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08003675 add_discard_addrs(sbi, cpc, false);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003676 }
Chao Yu184a5cd2014-09-04 18:13:01 +08003677
3678 if (to_journal) {
Chao Yu4d57b862018-05-30 00:20:41 +08003679 offset = f2fs_lookup_journal_in_cursum(journal,
Chao Yu184a5cd2014-09-04 18:13:01 +08003680 SIT_JOURNAL, segno, 1);
3681 f2fs_bug_on(sbi, offset < 0);
Chao Yudfc08a12016-02-14 18:50:40 +08003682 segno_in_journal(journal, offset) =
Chao Yu184a5cd2014-09-04 18:13:01 +08003683 cpu_to_le32(segno);
3684 seg_info_to_raw_sit(se,
Chao Yudfc08a12016-02-14 18:50:40 +08003685 &sit_in_journal(journal, offset));
Zhikang Zhang56b07e72018-04-09 04:28:41 +08003686 check_block_count(sbi, segno,
3687 &sit_in_journal(journal, offset));
Chao Yu184a5cd2014-09-04 18:13:01 +08003688 } else {
3689 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
3690 seg_info_to_raw_sit(se,
3691 &raw_sit->entries[sit_offset]);
Zhikang Zhang56b07e72018-04-09 04:28:41 +08003692 check_block_count(sbi, segno,
3693 &raw_sit->entries[sit_offset]);
Chao Yu184a5cd2014-09-04 18:13:01 +08003694 }
3695
3696 __clear_bit(segno, bitmap);
3697 sit_i->dirty_sentries--;
3698 ses->entry_cnt--;
3699 }
3700
Chao Yub7ad7512016-02-19 18:08:46 +08003701 if (to_journal)
3702 up_write(&curseg->journal_rwsem);
3703 else
Chao Yu184a5cd2014-09-04 18:13:01 +08003704 f2fs_put_page(page, 1);
3705
3706 f2fs_bug_on(sbi, ses->entry_cnt);
3707 release_sit_entry_set(ses);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003708 }
Chao Yu184a5cd2014-09-04 18:13:01 +08003709
3710 f2fs_bug_on(sbi, !list_empty(head));
3711 f2fs_bug_on(sbi, sit_i->dirty_sentries);
Chao Yu184a5cd2014-09-04 18:13:01 +08003712out:
Chao Yuc473f1a2017-04-27 20:40:39 +08003713 if (cpc->reason & CP_DISCARD) {
Yunlei He650d3c42016-12-22 11:46:24 +08003714 __u64 trim_start = cpc->trim_start;
3715
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003716 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++)
Jaegeuk Kim25290fa2016-12-29 22:06:15 -08003717 add_discard_addrs(sbi, cpc, false);
Yunlei He650d3c42016-12-22 11:46:24 +08003718
3719 cpc->trim_start = trim_start;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003720 }
Chao Yu3d26fa62017-10-30 17:49:53 +08003721 up_write(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003722
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003723 set_prefree_as_free_segments(sbi);
3724}
3725
3726static int build_sit_info(struct f2fs_sb_info *sbi)
3727{
3728 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003729 struct sit_info *sit_i;
3730 unsigned int sit_segs, start;
Chao Yuae27d622017-01-07 18:52:34 +08003731 char *src_bitmap;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003732 unsigned int bitmap_size;
3733
3734 /* allocate memory for SIT information */
Chao Yuacbf0542017-11-30 19:28:17 +08003735 sit_i = f2fs_kzalloc(sbi, sizeof(struct sit_info), GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003736 if (!sit_i)
3737 return -ENOMEM;
3738
3739 SM_I(sbi)->sit_info = sit_i;
3740
Kees Cook9d2a7892018-06-12 14:28:35 -07003741 sit_i->sentries =
3742 f2fs_kvzalloc(sbi, array_size(sizeof(struct seg_entry),
3743 MAIN_SEGS(sbi)),
3744 GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003745 if (!sit_i->sentries)
3746 return -ENOMEM;
3747
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003748 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Chao Yu628b3d12017-11-30 19:28:18 +08003749 sit_i->dirty_sentries_bitmap = f2fs_kvzalloc(sbi, bitmap_size,
3750 GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003751 if (!sit_i->dirty_sentries_bitmap)
3752 return -ENOMEM;
3753
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003754 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003755 sit_i->sentries[start].cur_valid_map
Chao Yuacbf0542017-11-30 19:28:17 +08003756 = f2fs_kzalloc(sbi, SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003757 sit_i->sentries[start].ckpt_valid_map
Chao Yuacbf0542017-11-30 19:28:17 +08003758 = f2fs_kzalloc(sbi, SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07003759 if (!sit_i->sentries[start].cur_valid_map ||
Jaegeuk Kim3e025742016-08-02 10:56:40 -07003760 !sit_i->sentries[start].ckpt_valid_map)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003761 return -ENOMEM;
Jaegeuk Kim3e025742016-08-02 10:56:40 -07003762
Chao Yu355e7892017-01-07 18:51:01 +08003763#ifdef CONFIG_F2FS_CHECK_FS
3764 sit_i->sentries[start].cur_valid_map_mir
Chao Yuacbf0542017-11-30 19:28:17 +08003765 = f2fs_kzalloc(sbi, SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
Chao Yu355e7892017-01-07 18:51:01 +08003766 if (!sit_i->sentries[start].cur_valid_map_mir)
3767 return -ENOMEM;
3768#endif
3769
Chao Yu7d20c8a2018-09-04 03:52:17 +08003770 sit_i->sentries[start].discard_map
3771 = f2fs_kzalloc(sbi, SIT_VBLOCK_MAP_SIZE,
3772 GFP_KERNEL);
3773 if (!sit_i->sentries[start].discard_map)
3774 return -ENOMEM;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003775 }
3776
Chao Yuacbf0542017-11-30 19:28:17 +08003777 sit_i->tmp_map = f2fs_kzalloc(sbi, SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
Jaegeuk Kim60a3b782015-02-10 16:44:29 -08003778 if (!sit_i->tmp_map)
3779 return -ENOMEM;
3780
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003781 if (sbi->segs_per_sec > 1) {
Kees Cook9d2a7892018-06-12 14:28:35 -07003782 sit_i->sec_entries =
3783 f2fs_kvzalloc(sbi, array_size(sizeof(struct sec_entry),
3784 MAIN_SECS(sbi)),
3785 GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003786 if (!sit_i->sec_entries)
3787 return -ENOMEM;
3788 }
3789
3790 /* get information related with SIT */
3791 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
3792
3793 /* setup SIT bitmap from ckeckpoint pack */
3794 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
3795 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
3796
Chao Yuae27d622017-01-07 18:52:34 +08003797 sit_i->sit_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
3798 if (!sit_i->sit_bitmap)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003799 return -ENOMEM;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003800
Chao Yuae27d622017-01-07 18:52:34 +08003801#ifdef CONFIG_F2FS_CHECK_FS
3802 sit_i->sit_bitmap_mir = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
3803 if (!sit_i->sit_bitmap_mir)
3804 return -ENOMEM;
3805#endif
3806
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003807 /* init SIT information */
3808 sit_i->s_ops = &default_salloc_ops;
3809
3810 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
3811 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
Jaegeuk Kimc79b7ff2016-11-14 18:20:10 -08003812 sit_i->written_valid_blocks = 0;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003813 sit_i->bitmap_size = bitmap_size;
3814 sit_i->dirty_sentries = 0;
3815 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
3816 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
Deepa Dinamani48fbfe52017-05-08 15:59:10 -07003817 sit_i->mounted_time = ktime_get_real_seconds();
Chao Yu3d26fa62017-10-30 17:49:53 +08003818 init_rwsem(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003819 return 0;
3820}
3821
3822static int build_free_segmap(struct f2fs_sb_info *sbi)
3823{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003824 struct free_segmap_info *free_i;
3825 unsigned int bitmap_size, sec_bitmap_size;
3826
3827 /* allocate memory for free segmap information */
Chao Yuacbf0542017-11-30 19:28:17 +08003828 free_i = f2fs_kzalloc(sbi, sizeof(struct free_segmap_info), GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003829 if (!free_i)
3830 return -ENOMEM;
3831
3832 SM_I(sbi)->free_info = free_i;
3833
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003834 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Chao Yu628b3d12017-11-30 19:28:18 +08003835 free_i->free_segmap = f2fs_kvmalloc(sbi, bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003836 if (!free_i->free_segmap)
3837 return -ENOMEM;
3838
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003839 sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
Chao Yu628b3d12017-11-30 19:28:18 +08003840 free_i->free_secmap = f2fs_kvmalloc(sbi, sec_bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003841 if (!free_i->free_secmap)
3842 return -ENOMEM;
3843
3844 /* set all segments as dirty temporarily */
3845 memset(free_i->free_segmap, 0xff, bitmap_size);
3846 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
3847
3848 /* init free segmap information */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003849 free_i->start_segno = GET_SEGNO_FROM_SEG0(sbi, MAIN_BLKADDR(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003850 free_i->free_segments = 0;
3851 free_i->free_sections = 0;
Chao Yu1a118cc2015-02-11 18:20:38 +08003852 spin_lock_init(&free_i->segmap_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003853 return 0;
3854}
3855
3856static int build_curseg(struct f2fs_sb_info *sbi)
3857{
Namjae Jeon1042d602012-12-01 10:56:13 +09003858 struct curseg_info *array;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003859 int i;
3860
Kees Cook026f0502018-06-12 14:28:23 -07003861 array = f2fs_kzalloc(sbi, array_size(NR_CURSEG_TYPE, sizeof(*array)),
3862 GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003863 if (!array)
3864 return -ENOMEM;
3865
3866 SM_I(sbi)->curseg_array = array;
3867
3868 for (i = 0; i < NR_CURSEG_TYPE; i++) {
3869 mutex_init(&array[i].curseg_mutex);
Chao Yuacbf0542017-11-30 19:28:17 +08003870 array[i].sum_blk = f2fs_kzalloc(sbi, PAGE_SIZE, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003871 if (!array[i].sum_blk)
3872 return -ENOMEM;
Chao Yub7ad7512016-02-19 18:08:46 +08003873 init_rwsem(&array[i].journal_rwsem);
Chao Yuacbf0542017-11-30 19:28:17 +08003874 array[i].journal = f2fs_kzalloc(sbi,
3875 sizeof(struct f2fs_journal), GFP_KERNEL);
Chao Yub7ad7512016-02-19 18:08:46 +08003876 if (!array[i].journal)
3877 return -ENOMEM;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003878 array[i].segno = NULL_SEGNO;
3879 array[i].next_blkoff = 0;
3880 }
3881 return restore_curseg_summaries(sbi);
3882}
3883
Jaegeuk Kimc39a1b32017-12-19 19:16:34 -08003884static int build_sit_entries(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003885{
3886 struct sit_info *sit_i = SIT_I(sbi);
3887 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08003888 struct f2fs_journal *journal = curseg->journal;
Yunlei He9c094042016-09-24 12:29:18 +08003889 struct seg_entry *se;
3890 struct f2fs_sit_entry sit;
Chao Yu74de5932013-11-22 09:09:59 +08003891 int sit_blk_cnt = SIT_BLK_CNT(sbi);
3892 unsigned int i, start, end;
3893 unsigned int readed, start_blk = 0;
Jaegeuk Kimc39a1b32017-12-19 19:16:34 -08003894 int err = 0;
Jaegeuk Kim8a29c122018-04-24 21:34:05 -06003895 block_t total_node_blocks = 0;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003896
Chao Yu74de5932013-11-22 09:09:59 +08003897 do {
Chao Yu4d57b862018-05-30 00:20:41 +08003898 readed = f2fs_ra_meta_pages(sbi, start_blk, BIO_MAX_PAGES,
Jaegeuk Kim664ba972016-10-18 11:07:45 -07003899 META_SIT, true);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003900
Chao Yu74de5932013-11-22 09:09:59 +08003901 start = start_blk * sit_i->sents_per_block;
3902 end = (start_blk + readed) * sit_i->sents_per_block;
3903
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003904 for (; start < end && start < MAIN_SEGS(sbi); start++) {
Chao Yu74de5932013-11-22 09:09:59 +08003905 struct f2fs_sit_block *sit_blk;
Chao Yu74de5932013-11-22 09:09:59 +08003906 struct page *page;
3907
Yunlei He9c094042016-09-24 12:29:18 +08003908 se = &sit_i->sentries[start];
Chao Yu74de5932013-11-22 09:09:59 +08003909 page = get_current_sit_page(sbi, start);
3910 sit_blk = (struct f2fs_sit_block *)page_address(page);
3911 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
3912 f2fs_put_page(page, 1);
Chao Yud600af232016-08-19 23:13:47 +08003913
Jaegeuk Kimc39a1b32017-12-19 19:16:34 -08003914 err = check_block_count(sbi, start, &sit);
3915 if (err)
3916 return err;
Chao Yu74de5932013-11-22 09:09:59 +08003917 seg_info_from_raw_sit(se, &sit);
Jaegeuk Kim8a29c122018-04-24 21:34:05 -06003918 if (IS_NODESEG(se->type))
3919 total_node_blocks += se->valid_blocks;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07003920
3921 /* build discard map only one time */
Chao Yu7d20c8a2018-09-04 03:52:17 +08003922 if (is_set_ckpt_flags(sbi, CP_TRIMMED_FLAG)) {
3923 memset(se->discard_map, 0xff,
3924 SIT_VBLOCK_MAP_SIZE);
3925 } else {
3926 memcpy(se->discard_map,
3927 se->cur_valid_map,
3928 SIT_VBLOCK_MAP_SIZE);
3929 sbi->discard_blks +=
3930 sbi->blocks_per_seg -
3931 se->valid_blocks;
Jaegeuk Kim3e025742016-08-02 10:56:40 -07003932 }
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07003933
Chao Yud600af232016-08-19 23:13:47 +08003934 if (sbi->segs_per_sec > 1)
3935 get_sec_entry(sbi, start)->valid_blocks +=
3936 se->valid_blocks;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003937 }
Chao Yu74de5932013-11-22 09:09:59 +08003938 start_blk += readed;
3939 } while (start_blk < sit_blk_cnt);
Chao Yud600af232016-08-19 23:13:47 +08003940
3941 down_read(&curseg->journal_rwsem);
3942 for (i = 0; i < sits_in_cursum(journal); i++) {
Chao Yud600af232016-08-19 23:13:47 +08003943 unsigned int old_valid_blocks;
3944
3945 start = le32_to_cpu(segno_in_journal(journal, i));
Jaegeuk Kimb2ca3742018-04-24 15:44:16 -06003946 if (start >= MAIN_SEGS(sbi)) {
3947 f2fs_msg(sbi->sb, KERN_ERR,
3948 "Wrong journal entry on segno %u",
3949 start);
3950 set_sbi_flag(sbi, SBI_NEED_FSCK);
3951 err = -EINVAL;
3952 break;
3953 }
3954
Chao Yud600af232016-08-19 23:13:47 +08003955 se = &sit_i->sentries[start];
3956 sit = sit_in_journal(journal, i);
3957
3958 old_valid_blocks = se->valid_blocks;
Jaegeuk Kim8a29c122018-04-24 21:34:05 -06003959 if (IS_NODESEG(se->type))
3960 total_node_blocks -= old_valid_blocks;
Chao Yud600af232016-08-19 23:13:47 +08003961
Jaegeuk Kimc39a1b32017-12-19 19:16:34 -08003962 err = check_block_count(sbi, start, &sit);
3963 if (err)
3964 break;
Chao Yud600af232016-08-19 23:13:47 +08003965 seg_info_from_raw_sit(se, &sit);
Jaegeuk Kim8a29c122018-04-24 21:34:05 -06003966 if (IS_NODESEG(se->type))
3967 total_node_blocks += se->valid_blocks;
Chao Yud600af232016-08-19 23:13:47 +08003968
Chao Yu7d20c8a2018-09-04 03:52:17 +08003969 if (is_set_ckpt_flags(sbi, CP_TRIMMED_FLAG)) {
3970 memset(se->discard_map, 0xff, SIT_VBLOCK_MAP_SIZE);
3971 } else {
3972 memcpy(se->discard_map, se->cur_valid_map,
3973 SIT_VBLOCK_MAP_SIZE);
3974 sbi->discard_blks += old_valid_blocks;
3975 sbi->discard_blks -= se->valid_blocks;
Chao Yud600af232016-08-19 23:13:47 +08003976 }
3977
Chao Yua9af3fd2018-04-25 19:38:17 +08003978 if (sbi->segs_per_sec > 1) {
Chao Yud600af232016-08-19 23:13:47 +08003979 get_sec_entry(sbi, start)->valid_blocks +=
Chao Yua9af3fd2018-04-25 19:38:17 +08003980 se->valid_blocks;
3981 get_sec_entry(sbi, start)->valid_blocks -=
3982 old_valid_blocks;
3983 }
Chao Yud600af232016-08-19 23:13:47 +08003984 }
3985 up_read(&curseg->journal_rwsem);
Jaegeuk Kim8a29c122018-04-24 21:34:05 -06003986
3987 if (!err && total_node_blocks != valid_node_count(sbi)) {
3988 f2fs_msg(sbi->sb, KERN_ERR,
3989 "SIT is corrupted node# %u vs %u",
3990 total_node_blocks, valid_node_count(sbi));
3991 set_sbi_flag(sbi, SBI_NEED_FSCK);
3992 err = -EINVAL;
3993 }
3994
Jaegeuk Kimc39a1b32017-12-19 19:16:34 -08003995 return err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003996}
3997
3998static void init_free_segmap(struct f2fs_sb_info *sbi)
3999{
4000 unsigned int start;
4001 int type;
4002
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07004003 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004004 struct seg_entry *sentry = get_seg_entry(sbi, start);
4005 if (!sentry->valid_blocks)
4006 __set_free(sbi, start);
Jaegeuk Kimc79b7ff2016-11-14 18:20:10 -08004007 else
4008 SIT_I(sbi)->written_valid_blocks +=
4009 sentry->valid_blocks;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004010 }
4011
4012 /* set use the current segments */
4013 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
4014 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
4015 __set_test_and_inuse(sbi, curseg_t->segno);
4016 }
4017}
4018
4019static void init_dirty_segmap(struct f2fs_sb_info *sbi)
4020{
4021 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
4022 struct free_segmap_info *free_i = FREE_I(sbi);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07004023 unsigned int segno = 0, offset = 0;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004024 unsigned short valid_blocks;
4025
Namjae Jeon8736fbf2013-06-16 09:49:11 +09004026 while (1) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004027 /* find dirty segment based on free segmap */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07004028 segno = find_next_inuse(free_i, MAIN_SEGS(sbi), offset);
4029 if (segno >= MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004030 break;
4031 offset = segno + 1;
Jaegeuk Kim302bd342017-04-07 14:33:22 -07004032 valid_blocks = get_valid_blocks(sbi, segno, false);
Jaegeuk Kimec325b52014-09-02 16:24:11 -07004033 if (valid_blocks == sbi->blocks_per_seg || !valid_blocks)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004034 continue;
Jaegeuk Kimec325b52014-09-02 16:24:11 -07004035 if (valid_blocks > sbi->blocks_per_seg) {
4036 f2fs_bug_on(sbi, 1);
4037 continue;
4038 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004039 mutex_lock(&dirty_i->seglist_lock);
4040 __locate_dirty_segment(sbi, segno, DIRTY);
4041 mutex_unlock(&dirty_i->seglist_lock);
4042 }
4043}
4044
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09004045static int init_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004046{
4047 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07004048 unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004049
Chao Yu628b3d12017-11-30 19:28:18 +08004050 dirty_i->victim_secmap = f2fs_kvzalloc(sbi, bitmap_size, GFP_KERNEL);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09004051 if (!dirty_i->victim_secmap)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004052 return -ENOMEM;
4053 return 0;
4054}
4055
4056static int build_dirty_segmap(struct f2fs_sb_info *sbi)
4057{
4058 struct dirty_seglist_info *dirty_i;
4059 unsigned int bitmap_size, i;
4060
4061 /* allocate memory for dirty segments list information */
Chao Yuacbf0542017-11-30 19:28:17 +08004062 dirty_i = f2fs_kzalloc(sbi, sizeof(struct dirty_seglist_info),
4063 GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004064 if (!dirty_i)
4065 return -ENOMEM;
4066
4067 SM_I(sbi)->dirty_info = dirty_i;
4068 mutex_init(&dirty_i->seglist_lock);
4069
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07004070 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004071
4072 for (i = 0; i < NR_DIRTY_TYPE; i++) {
Chao Yu628b3d12017-11-30 19:28:18 +08004073 dirty_i->dirty_segmap[i] = f2fs_kvzalloc(sbi, bitmap_size,
4074 GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004075 if (!dirty_i->dirty_segmap[i])
4076 return -ENOMEM;
4077 }
4078
4079 init_dirty_segmap(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09004080 return init_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004081}
4082
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09004083/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004084 * Update min, max modified time for cost-benefit GC algorithm
4085 */
4086static void init_min_max_mtime(struct f2fs_sb_info *sbi)
4087{
4088 struct sit_info *sit_i = SIT_I(sbi);
4089 unsigned int segno;
4090
Chao Yu3d26fa62017-10-30 17:49:53 +08004091 down_write(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004092
Chao Yu5ad25442018-05-15 18:59:55 +08004093 sit_i->min_mtime = ULLONG_MAX;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004094
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07004095 for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004096 unsigned int i;
4097 unsigned long long mtime = 0;
4098
4099 for (i = 0; i < sbi->segs_per_sec; i++)
4100 mtime += get_seg_entry(sbi, segno + i)->mtime;
4101
4102 mtime = div_u64(mtime, sbi->segs_per_sec);
4103
4104 if (sit_i->min_mtime > mtime)
4105 sit_i->min_mtime = mtime;
4106 }
Chao Yua1f72ac22018-06-04 23:20:17 +08004107 sit_i->max_mtime = get_mtime(sbi, false);
Chao Yu3d26fa62017-10-30 17:49:53 +08004108 up_write(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004109}
4110
Chao Yu4d57b862018-05-30 00:20:41 +08004111int f2fs_build_segment_manager(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004112{
4113 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
4114 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Namjae Jeon1042d602012-12-01 10:56:13 +09004115 struct f2fs_sm_info *sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004116 int err;
4117
Chao Yuacbf0542017-11-30 19:28:17 +08004118 sm_info = f2fs_kzalloc(sbi, sizeof(struct f2fs_sm_info), GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004119 if (!sm_info)
4120 return -ENOMEM;
4121
4122 /* init sm info */
4123 sbi->sm_info = sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004124 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
4125 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
4126 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
4127 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
4128 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
4129 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
4130 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
Jaegeuk Kim58c41032014-03-19 14:17:21 +09004131 sm_info->rec_prefree_segments = sm_info->main_segments *
4132 DEF_RECLAIM_PREFREE_SEGMENTS / 100;
Jaegeuk Kim44a83492016-07-13 18:23:35 -07004133 if (sm_info->rec_prefree_segments > DEF_MAX_RECLAIM_PREFREE_SEGMENTS)
4134 sm_info->rec_prefree_segments = DEF_MAX_RECLAIM_PREFREE_SEGMENTS;
4135
Jaegeuk Kim52763a42016-06-13 09:47:48 -07004136 if (!test_opt(sbi, LFS))
4137 sm_info->ipu_policy = 1 << F2FS_IPU_FSYNC;
Jaegeuk Kim216fbd62013-11-07 13:13:42 +09004138 sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -07004139 sm_info->min_fsync_blocks = DEF_MIN_FSYNC_BLOCKS;
Jaegeuk Kim853137c2018-08-09 17:53:34 -07004140 sm_info->min_seq_blocks = sbi->blocks_per_seg * sbi->segs_per_sec;
Jaegeuk Kimef095d12017-03-24 20:05:13 -04004141 sm_info->min_hot_blocks = DEF_MIN_HOT_BLOCKS;
Chao Yua2a12b62017-10-28 16:52:33 +08004142 sm_info->min_ssr_sections = reserved_sections(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004143
Chao Yu184a5cd2014-09-04 18:13:01 +08004144 INIT_LIST_HEAD(&sm_info->sit_entry_set);
4145
Chao Yu2b603112017-11-02 20:41:03 +08004146 init_rwsem(&sm_info->curseg_lock);
4147
Yunlei Hed4fdf8b2017-06-01 16:43:51 +08004148 if (!f2fs_readonly(sbi->sb)) {
Chao Yu4d57b862018-05-30 00:20:41 +08004149 err = f2fs_create_flush_cmd_control(sbi);
Gu Zheng2163d192014-04-27 14:21:33 +08004150 if (err)
Gu Zhenga688b9d9e2014-04-27 14:21:21 +08004151 return err;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +09004152 }
4153
Jaegeuk Kim0b54fb82017-01-11 14:40:24 -08004154 err = create_discard_cmd_control(sbi);
4155 if (err)
4156 return err;
4157
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004158 err = build_sit_info(sbi);
4159 if (err)
4160 return err;
4161 err = build_free_segmap(sbi);
4162 if (err)
4163 return err;
4164 err = build_curseg(sbi);
4165 if (err)
4166 return err;
4167
4168 /* reinit free segmap based on SIT */
Jaegeuk Kimc39a1b32017-12-19 19:16:34 -08004169 err = build_sit_entries(sbi);
4170 if (err)
4171 return err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004172
4173 init_free_segmap(sbi);
4174 err = build_dirty_segmap(sbi);
4175 if (err)
4176 return err;
4177
4178 init_min_max_mtime(sbi);
4179 return 0;
4180}
4181
4182static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
4183 enum dirty_type dirty_type)
4184{
4185 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
4186
4187 mutex_lock(&dirty_i->seglist_lock);
Jaegeuk Kim39307a82015-09-22 13:50:47 -07004188 kvfree(dirty_i->dirty_segmap[dirty_type]);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004189 dirty_i->nr_dirty[dirty_type] = 0;
4190 mutex_unlock(&dirty_i->seglist_lock);
4191}
4192
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09004193static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004194{
4195 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim39307a82015-09-22 13:50:47 -07004196 kvfree(dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004197}
4198
4199static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
4200{
4201 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
4202 int i;
4203
4204 if (!dirty_i)
4205 return;
4206
4207 /* discard pre-free/dirty segments list */
4208 for (i = 0; i < NR_DIRTY_TYPE; i++)
4209 discard_dirty_segmap(sbi, i);
4210
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09004211 destroy_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004212 SM_I(sbi)->dirty_info = NULL;
4213 kfree(dirty_i);
4214}
4215
4216static void destroy_curseg(struct f2fs_sb_info *sbi)
4217{
4218 struct curseg_info *array = SM_I(sbi)->curseg_array;
4219 int i;
4220
4221 if (!array)
4222 return;
4223 SM_I(sbi)->curseg_array = NULL;
Chao Yub7ad7512016-02-19 18:08:46 +08004224 for (i = 0; i < NR_CURSEG_TYPE; i++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004225 kfree(array[i].sum_blk);
Chao Yub7ad7512016-02-19 18:08:46 +08004226 kfree(array[i].journal);
4227 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004228 kfree(array);
4229}
4230
4231static void destroy_free_segmap(struct f2fs_sb_info *sbi)
4232{
4233 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
4234 if (!free_i)
4235 return;
4236 SM_I(sbi)->free_info = NULL;
Jaegeuk Kim39307a82015-09-22 13:50:47 -07004237 kvfree(free_i->free_segmap);
4238 kvfree(free_i->free_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004239 kfree(free_i);
4240}
4241
4242static void destroy_sit_info(struct f2fs_sb_info *sbi)
4243{
4244 struct sit_info *sit_i = SIT_I(sbi);
4245 unsigned int start;
4246
4247 if (!sit_i)
4248 return;
4249
4250 if (sit_i->sentries) {
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07004251 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004252 kfree(sit_i->sentries[start].cur_valid_map);
Chao Yu355e7892017-01-07 18:51:01 +08004253#ifdef CONFIG_F2FS_CHECK_FS
4254 kfree(sit_i->sentries[start].cur_valid_map_mir);
4255#endif
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004256 kfree(sit_i->sentries[start].ckpt_valid_map);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07004257 kfree(sit_i->sentries[start].discard_map);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004258 }
4259 }
Jaegeuk Kim60a3b782015-02-10 16:44:29 -08004260 kfree(sit_i->tmp_map);
4261
Jaegeuk Kim39307a82015-09-22 13:50:47 -07004262 kvfree(sit_i->sentries);
4263 kvfree(sit_i->sec_entries);
4264 kvfree(sit_i->dirty_sentries_bitmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004265
4266 SM_I(sbi)->sit_info = NULL;
4267 kfree(sit_i->sit_bitmap);
Chao Yuae27d622017-01-07 18:52:34 +08004268#ifdef CONFIG_F2FS_CHECK_FS
4269 kfree(sit_i->sit_bitmap_mir);
4270#endif
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004271 kfree(sit_i);
4272}
4273
Chao Yu4d57b862018-05-30 00:20:41 +08004274void f2fs_destroy_segment_manager(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004275{
4276 struct f2fs_sm_info *sm_info = SM_I(sbi);
Gu Zhenga688b9d9e2014-04-27 14:21:21 +08004277
Chao Yu3b03f722013-11-06 09:12:04 +08004278 if (!sm_info)
4279 return;
Chao Yu4d57b862018-05-30 00:20:41 +08004280 f2fs_destroy_flush_cmd_control(sbi, true);
Chao Yuf0994052017-03-27 18:14:04 +08004281 destroy_discard_cmd_control(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09004282 destroy_dirty_segmap(sbi);
4283 destroy_curseg(sbi);
4284 destroy_free_segmap(sbi);
4285 destroy_sit_info(sbi);
4286 sbi->sm_info = NULL;
4287 kfree(sm_info);
4288}
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09004289
Chao Yu4d57b862018-05-30 00:20:41 +08004290int __init f2fs_create_segment_manager_caches(void)
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09004291{
4292 discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
Gu Zhenge8512d22014-03-07 18:43:28 +08004293 sizeof(struct discard_entry));
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09004294 if (!discard_entry_slab)
Chao Yu184a5cd2014-09-04 18:13:01 +08004295 goto fail;
4296
Jaegeuk Kimb01a9202017-01-09 14:13:03 -08004297 discard_cmd_slab = f2fs_kmem_cache_create("discard_cmd",
4298 sizeof(struct discard_cmd));
4299 if (!discard_cmd_slab)
Chao Yu6ab2a302016-09-05 12:28:26 +08004300 goto destroy_discard_entry;
Chao Yu275b66b2016-08-29 23:58:34 +08004301
Chao Yu184a5cd2014-09-04 18:13:01 +08004302 sit_entry_set_slab = f2fs_kmem_cache_create("sit_entry_set",
Changman Leec9ee0082014-11-21 15:42:07 +09004303 sizeof(struct sit_entry_set));
Chao Yu184a5cd2014-09-04 18:13:01 +08004304 if (!sit_entry_set_slab)
Jaegeuk Kimb01a9202017-01-09 14:13:03 -08004305 goto destroy_discard_cmd;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07004306
4307 inmem_entry_slab = f2fs_kmem_cache_create("inmem_page_entry",
4308 sizeof(struct inmem_pages));
4309 if (!inmem_entry_slab)
4310 goto destroy_sit_entry_set;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09004311 return 0;
Chao Yu184a5cd2014-09-04 18:13:01 +08004312
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07004313destroy_sit_entry_set:
4314 kmem_cache_destroy(sit_entry_set_slab);
Jaegeuk Kimb01a9202017-01-09 14:13:03 -08004315destroy_discard_cmd:
4316 kmem_cache_destroy(discard_cmd_slab);
Chao Yu6ab2a302016-09-05 12:28:26 +08004317destroy_discard_entry:
Chao Yu184a5cd2014-09-04 18:13:01 +08004318 kmem_cache_destroy(discard_entry_slab);
4319fail:
4320 return -ENOMEM;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09004321}
4322
Chao Yu4d57b862018-05-30 00:20:41 +08004323void f2fs_destroy_segment_manager_caches(void)
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09004324{
Chao Yu184a5cd2014-09-04 18:13:01 +08004325 kmem_cache_destroy(sit_entry_set_slab);
Jaegeuk Kimb01a9202017-01-09 14:13:03 -08004326 kmem_cache_destroy(discard_cmd_slab);
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09004327 kmem_cache_destroy(discard_entry_slab);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07004328 kmem_cache_destroy(inmem_entry_slab);
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09004329}