blob: 51a7c6986f06095177b2f3fcd46a5301dc366bbe [file] [log] [blame]
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002 * fs/f2fs/segment.c
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include <linux/fs.h>
12#include <linux/f2fs_fs.h>
13#include <linux/bio.h>
14#include <linux/blkdev.h>
Geert Uytterhoeven690e4a32012-12-19 22:19:30 +010015#include <linux/prefetch.h>
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +090016#include <linux/kthread.h>
Chao Yu74de5932013-11-22 09:09:59 +080017#include <linux/swap.h>
Jaegeuk Kim60b99b42015-10-05 14:49:57 -070018#include <linux/timer.h>
Jaegeuk Kimd430af22017-05-17 10:36:58 -070019#include <linux/freezer.h>
Jaegeuk Kim351df4b2012-11-02 17:09:16 +090020
21#include "f2fs.h"
22#include "segment.h"
23#include "node.h"
Jaegeuk Kim766330d2017-08-15 21:27:19 -070024#include "gc.h"
Jaegeuk Kim9e4ded32014-12-17 19:58:58 -080025#include "trace.h"
Namjae Jeon6ec178d2013-04-23 17:51:43 +090026#include <trace/events/f2fs.h>
Jaegeuk Kim351df4b2012-11-02 17:09:16 +090027
Changman Lee9a7f1432013-11-15 10:42:51 +090028#define __reverse_ffz(x) __reverse_ffs(~(x))
29
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +090030static struct kmem_cache *discard_entry_slab;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070031static struct kmem_cache *discard_cmd_slab;
Chao Yu184a5cd2014-09-04 18:13:01 +080032static struct kmem_cache *sit_entry_set_slab;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -070033static struct kmem_cache *inmem_entry_slab;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +090034
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070035static unsigned long __reverse_ulong(unsigned char *str)
36{
37 unsigned long tmp = 0;
38 int shift = 24, idx = 0;
39
40#if BITS_PER_LONG == 64
41 shift = 56;
42#endif
43 while (shift >= 0) {
44 tmp |= (unsigned long)str[idx++] << shift;
45 shift -= BITS_PER_BYTE;
46 }
47 return tmp;
48}
49
Changman Lee9a7f1432013-11-15 10:42:51 +090050/*
51 * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since
52 * MSB and LSB are reversed in a byte by f2fs_set_bit.
53 */
54static inline unsigned long __reverse_ffs(unsigned long word)
55{
56 int num = 0;
57
58#if BITS_PER_LONG == 64
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070059 if ((word & 0xffffffff00000000UL) == 0)
Changman Lee9a7f1432013-11-15 10:42:51 +090060 num += 32;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070061 else
Changman Lee9a7f1432013-11-15 10:42:51 +090062 word >>= 32;
Changman Lee9a7f1432013-11-15 10:42:51 +090063#endif
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070064 if ((word & 0xffff0000) == 0)
Changman Lee9a7f1432013-11-15 10:42:51 +090065 num += 16;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070066 else
Changman Lee9a7f1432013-11-15 10:42:51 +090067 word >>= 16;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070068
69 if ((word & 0xff00) == 0)
Changman Lee9a7f1432013-11-15 10:42:51 +090070 num += 8;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070071 else
Changman Lee9a7f1432013-11-15 10:42:51 +090072 word >>= 8;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070073
Changman Lee9a7f1432013-11-15 10:42:51 +090074 if ((word & 0xf0) == 0)
75 num += 4;
76 else
77 word >>= 4;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070078
Changman Lee9a7f1432013-11-15 10:42:51 +090079 if ((word & 0xc) == 0)
80 num += 2;
81 else
82 word >>= 2;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070083
Changman Lee9a7f1432013-11-15 10:42:51 +090084 if ((word & 0x2) == 0)
85 num += 1;
86 return num;
87}
88
89/*
arter97e1c42042014-08-06 23:22:50 +090090 * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c because
Changman Lee9a7f1432013-11-15 10:42:51 +090091 * f2fs_set_bit makes MSB and LSB reversed in a byte.
Fan Li692223d2015-11-12 08:43:04 +080092 * @size must be integral times of unsigned long.
Changman Lee9a7f1432013-11-15 10:42:51 +090093 * Example:
Jaegeuk Kimf96999c2015-10-20 15:17:19 -070094 * MSB <--> LSB
95 * f2fs_set_bit(0, bitmap) => 1000 0000
96 * f2fs_set_bit(7, bitmap) => 0000 0001
Changman Lee9a7f1432013-11-15 10:42:51 +090097 */
98static unsigned long __find_rev_next_bit(const unsigned long *addr,
99 unsigned long size, unsigned long offset)
100{
101 const unsigned long *p = addr + BIT_WORD(offset);
Fan Li692223d2015-11-12 08:43:04 +0800102 unsigned long result = size;
Changman Lee9a7f1432013-11-15 10:42:51 +0900103 unsigned long tmp;
Changman Lee9a7f1432013-11-15 10:42:51 +0900104
105 if (offset >= size)
106 return size;
107
Fan Li692223d2015-11-12 08:43:04 +0800108 size -= (offset & ~(BITS_PER_LONG - 1));
Changman Lee9a7f1432013-11-15 10:42:51 +0900109 offset %= BITS_PER_LONG;
Changman Lee9a7f1432013-11-15 10:42:51 +0900110
Fan Li692223d2015-11-12 08:43:04 +0800111 while (1) {
112 if (*p == 0)
113 goto pass;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700114
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700115 tmp = __reverse_ulong((unsigned char *)p);
Fan Li692223d2015-11-12 08:43:04 +0800116
117 tmp &= ~0UL >> offset;
118 if (size < BITS_PER_LONG)
119 tmp &= (~0UL << (BITS_PER_LONG - size));
Changman Lee9a7f1432013-11-15 10:42:51 +0900120 if (tmp)
Fan Li692223d2015-11-12 08:43:04 +0800121 goto found;
122pass:
123 if (size <= BITS_PER_LONG)
124 break;
Changman Lee9a7f1432013-11-15 10:42:51 +0900125 size -= BITS_PER_LONG;
Fan Li692223d2015-11-12 08:43:04 +0800126 offset = 0;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700127 p++;
Changman Lee9a7f1432013-11-15 10:42:51 +0900128 }
Fan Li692223d2015-11-12 08:43:04 +0800129 return result;
130found:
131 return result - size + __reverse_ffs(tmp);
Changman Lee9a7f1432013-11-15 10:42:51 +0900132}
133
134static unsigned long __find_rev_next_zero_bit(const unsigned long *addr,
135 unsigned long size, unsigned long offset)
136{
137 const unsigned long *p = addr + BIT_WORD(offset);
Jaegeuk Kim80609442015-12-04 16:51:13 -0800138 unsigned long result = size;
Changman Lee9a7f1432013-11-15 10:42:51 +0900139 unsigned long tmp;
Changman Lee9a7f1432013-11-15 10:42:51 +0900140
141 if (offset >= size)
142 return size;
143
Jaegeuk Kim80609442015-12-04 16:51:13 -0800144 size -= (offset & ~(BITS_PER_LONG - 1));
Changman Lee9a7f1432013-11-15 10:42:51 +0900145 offset %= BITS_PER_LONG;
Changman Lee9a7f1432013-11-15 10:42:51 +0900146
Jaegeuk Kim80609442015-12-04 16:51:13 -0800147 while (1) {
148 if (*p == ~0UL)
149 goto pass;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700150
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700151 tmp = __reverse_ulong((unsigned char *)p);
Jaegeuk Kim80609442015-12-04 16:51:13 -0800152
153 if (offset)
154 tmp |= ~0UL << (BITS_PER_LONG - offset);
155 if (size < BITS_PER_LONG)
156 tmp |= ~0UL >> size;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700157 if (tmp != ~0UL)
Jaegeuk Kim80609442015-12-04 16:51:13 -0800158 goto found;
159pass:
160 if (size <= BITS_PER_LONG)
161 break;
Changman Lee9a7f1432013-11-15 10:42:51 +0900162 size -= BITS_PER_LONG;
Jaegeuk Kim80609442015-12-04 16:51:13 -0800163 offset = 0;
Jaegeuk Kimf96999c2015-10-20 15:17:19 -0700164 p++;
Changman Lee9a7f1432013-11-15 10:42:51 +0900165 }
Jaegeuk Kim80609442015-12-04 16:51:13 -0800166 return result;
167found:
168 return result - size + __reverse_ffz(tmp);
Changman Lee9a7f1432013-11-15 10:42:51 +0900169}
170
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700171void register_inmem_page(struct inode *inode, struct page *page)
172{
173 struct f2fs_inode_info *fi = F2FS_I(inode);
174 struct inmem_pages *new;
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800175
Jaegeuk Kim9e4ded32014-12-17 19:58:58 -0800176 f2fs_trace_pid(page);
Jaegeuk Kim0722b102014-12-05 11:58:02 -0800177
Chao Yudecd36b2015-08-07 18:42:09 +0800178 set_page_private(page, (unsigned long)ATOMIC_WRITTEN_PAGE);
179 SetPagePrivate(page);
180
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700181 new = f2fs_kmem_cache_alloc(inmem_entry_slab, GFP_NOFS);
182
183 /* add atomic page indices to the list */
184 new->page = page;
185 INIT_LIST_HEAD(&new->list);
Chao Yudecd36b2015-08-07 18:42:09 +0800186
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700187 /* increase reference count with clean state */
188 mutex_lock(&fi->inmem_lock);
189 get_page(page);
190 list_add_tail(&new->list, &fi->inmem_pages);
Jaegeuk Kim8dcf2ff72014-12-05 17:18:15 -0800191 inc_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700192 mutex_unlock(&fi->inmem_lock);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -0700193
194 trace_f2fs_register_inmem_page(page, INMEM);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700195}
196
Chao Yu28bc1062016-02-06 14:40:34 +0800197static int __revoke_inmem_pages(struct inode *inode,
198 struct list_head *head, bool drop, bool recover)
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700199{
Chao Yu28bc1062016-02-06 14:40:34 +0800200 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700201 struct inmem_pages *cur, *tmp;
Chao Yu28bc1062016-02-06 14:40:34 +0800202 int err = 0;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700203
Chao Yu29b96b52016-02-06 14:38:29 +0800204 list_for_each_entry_safe(cur, tmp, head, list) {
Chao Yu28bc1062016-02-06 14:40:34 +0800205 struct page *page = cur->page;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700206
Chao Yu28bc1062016-02-06 14:40:34 +0800207 if (drop)
208 trace_f2fs_commit_inmem_page(page, INMEM_DROP);
209
210 lock_page(page);
211
212 if (recover) {
213 struct dnode_of_data dn;
214 struct node_info ni;
215
216 trace_f2fs_commit_inmem_page(page, INMEM_REVOKE);
Chao Yu4b634f362017-08-08 19:09:08 +0800217retry:
Chao Yu28bc1062016-02-06 14:40:34 +0800218 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4b634f362017-08-08 19:09:08 +0800219 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
220 if (err) {
221 if (err == -ENOMEM) {
222 congestion_wait(BLK_RW_ASYNC, HZ/50);
223 cond_resched();
224 goto retry;
225 }
Chao Yu28bc1062016-02-06 14:40:34 +0800226 err = -EAGAIN;
227 goto next;
228 }
229 get_node_info(sbi, dn.nid, &ni);
230 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
231 cur->old_addr, ni.version, true, true);
232 f2fs_put_dnode(&dn);
233 }
234next:
Jaegeuk Kim63c52d72016-04-12 14:11:03 -0700235 /* we don't need to invalidate this in the sccessful status */
236 if (drop || recover)
237 ClearPageUptodate(page);
Chao Yu28bc1062016-02-06 14:40:34 +0800238 set_page_private(page, 0);
Chao Yuc81ced02016-04-29 20:13:36 +0800239 ClearPagePrivate(page);
Chao Yu28bc1062016-02-06 14:40:34 +0800240 f2fs_put_page(page, 1);
Chao Yudecd36b2015-08-07 18:42:09 +0800241
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700242 list_del(&cur->list);
243 kmem_cache_free(inmem_entry_slab, cur);
Jaegeuk Kim8dcf2ff72014-12-05 17:18:15 -0800244 dec_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700245 }
Chao Yu28bc1062016-02-06 14:40:34 +0800246 return err;
Chao Yu29b96b52016-02-06 14:38:29 +0800247}
248
249void drop_inmem_pages(struct inode *inode)
250{
251 struct f2fs_inode_info *fi = F2FS_I(inode);
252
253 mutex_lock(&fi->inmem_lock);
Chao Yu28bc1062016-02-06 14:40:34 +0800254 __revoke_inmem_pages(inode, &fi->inmem_pages, true, false);
Chao Yu29b96b52016-02-06 14:38:29 +0800255 mutex_unlock(&fi->inmem_lock);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700256
257 clear_inode_flag(inode, FI_ATOMIC_FILE);
258 stat_dec_atomic_write(inode);
259}
260
261void drop_inmem_page(struct inode *inode, struct page *page)
262{
263 struct f2fs_inode_info *fi = F2FS_I(inode);
264 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
265 struct list_head *head = &fi->inmem_pages;
266 struct inmem_pages *cur = NULL;
267
268 f2fs_bug_on(sbi, !IS_ATOMIC_WRITTEN_PAGE(page));
269
270 mutex_lock(&fi->inmem_lock);
271 list_for_each_entry(cur, head, list) {
272 if (cur->page == page)
273 break;
274 }
275
276 f2fs_bug_on(sbi, !cur || cur->page != page);
277 list_del(&cur->list);
278 mutex_unlock(&fi->inmem_lock);
279
280 dec_page_count(sbi, F2FS_INMEM_PAGES);
281 kmem_cache_free(inmem_entry_slab, cur);
282
283 ClearPageUptodate(page);
284 set_page_private(page, 0);
285 ClearPagePrivate(page);
286 f2fs_put_page(page, 0);
287
288 trace_f2fs_commit_inmem_page(page, INMEM_INVALIDATE);
Chao Yu29b96b52016-02-06 14:38:29 +0800289}
290
Chao Yu28bc1062016-02-06 14:40:34 +0800291static int __commit_inmem_pages(struct inode *inode,
292 struct list_head *revoke_list)
Chao Yu29b96b52016-02-06 14:38:29 +0800293{
294 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
295 struct f2fs_inode_info *fi = F2FS_I(inode);
296 struct inmem_pages *cur, *tmp;
297 struct f2fs_io_info fio = {
298 .sbi = sbi,
299 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -0500300 .op = REQ_OP_WRITE,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700301 .op_flags = REQ_SYNC | REQ_PRIO,
Chao Yuc0fe4882017-08-02 23:21:48 +0800302 .io_type = FS_DATA_IO,
Chao Yu29b96b52016-02-06 14:38:29 +0800303 };
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700304 pgoff_t last_idx = ULONG_MAX;
Chao Yu29b96b52016-02-06 14:38:29 +0800305 int err = 0;
306
307 list_for_each_entry_safe(cur, tmp, &fi->inmem_pages, list) {
Chao Yu28bc1062016-02-06 14:40:34 +0800308 struct page *page = cur->page;
309
310 lock_page(page);
311 if (page->mapping == inode->i_mapping) {
312 trace_f2fs_commit_inmem_page(page, INMEM);
313
314 set_page_dirty(page);
315 f2fs_wait_on_page_writeback(page, DATA, true);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700316 if (clear_page_dirty_for_io(page)) {
Chao Yu29b96b52016-02-06 14:38:29 +0800317 inode_dec_dirty_pages(inode);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700318 remove_dirty_inode(inode);
319 }
Jaegeuk Kim847413f2017-07-19 10:59:55 -0700320retry:
Chao Yu28bc1062016-02-06 14:40:34 +0800321 fio.page = page;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700322 fio.old_blkaddr = NULL_ADDR;
323 fio.encrypted_page = NULL;
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -0700324 fio.need_lock = LOCK_DONE;
Chao Yu29b96b52016-02-06 14:38:29 +0800325 err = do_write_data_page(&fio);
326 if (err) {
Jaegeuk Kim847413f2017-07-19 10:59:55 -0700327 if (err == -ENOMEM) {
328 congestion_wait(BLK_RW_ASYNC, HZ/50);
329 cond_resched();
330 goto retry;
331 }
Chao Yu28bc1062016-02-06 14:40:34 +0800332 unlock_page(page);
Chao Yu29b96b52016-02-06 14:38:29 +0800333 break;
334 }
Chao Yu28bc1062016-02-06 14:40:34 +0800335 /* record old blkaddr for revoking */
336 cur->old_addr = fio.old_blkaddr;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700337 last_idx = page->index;
Chao Yu29b96b52016-02-06 14:38:29 +0800338 }
Chao Yu28bc1062016-02-06 14:40:34 +0800339 unlock_page(page);
340 list_move_tail(&cur->list, revoke_list);
Chao Yu29b96b52016-02-06 14:38:29 +0800341 }
342
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700343 if (last_idx != ULONG_MAX)
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -0700344 f2fs_submit_merged_write_cond(sbi, inode, 0, last_idx, DATA);
Chao Yu28bc1062016-02-06 14:40:34 +0800345
346 if (!err)
347 __revoke_inmem_pages(inode, revoke_list, false, false);
348
Chao Yu29b96b52016-02-06 14:38:29 +0800349 return err;
350}
351
352int commit_inmem_pages(struct inode *inode)
353{
354 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
355 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yu28bc1062016-02-06 14:40:34 +0800356 struct list_head revoke_list;
357 int err;
Chao Yu29b96b52016-02-06 14:38:29 +0800358
Chao Yu28bc1062016-02-06 14:40:34 +0800359 INIT_LIST_HEAD(&revoke_list);
Chao Yu29b96b52016-02-06 14:38:29 +0800360 f2fs_balance_fs(sbi, true);
361 f2fs_lock_op(sbi);
362
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700363 set_inode_flag(inode, FI_ATOMIC_COMMIT);
364
Chao Yu29b96b52016-02-06 14:38:29 +0800365 mutex_lock(&fi->inmem_lock);
Chao Yu28bc1062016-02-06 14:40:34 +0800366 err = __commit_inmem_pages(inode, &revoke_list);
367 if (err) {
368 int ret;
369 /*
370 * try to revoke all committed pages, but still we could fail
371 * due to no memory or other reason, if that happened, EAGAIN
372 * will be returned, which means in such case, transaction is
373 * already not integrity, caller should use journal to do the
374 * recovery or rewrite & commit last transaction. For other
375 * error number, revoking was done by filesystem itself.
376 */
377 ret = __revoke_inmem_pages(inode, &revoke_list, false, true);
378 if (ret)
379 err = ret;
380
381 /* drop all uncommitted pages */
382 __revoke_inmem_pages(inode, &fi->inmem_pages, true, false);
383 }
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700384 mutex_unlock(&fi->inmem_lock);
385
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700386 clear_inode_flag(inode, FI_ATOMIC_COMMIT);
387
Chao Yu29b96b52016-02-06 14:38:29 +0800388 f2fs_unlock_op(sbi);
Jaegeuk Kimedb27de2015-07-25 00:52:52 -0700389 return err;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700390}
391
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900392/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900393 * This function balances dirty node and dentry pages.
394 * In addition, it controls garbage collection.
395 */
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800396void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900397{
Chao Yu0f348022016-09-26 19:45:55 +0800398#ifdef CONFIG_F2FS_FAULT_INJECTION
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700399 if (time_to_inject(sbi, FAULT_CHECKPOINT)) {
400 f2fs_show_injection_info(FAULT_CHECKPOINT);
Chao Yu0f348022016-09-26 19:45:55 +0800401 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700402 }
Chao Yu0f348022016-09-26 19:45:55 +0800403#endif
404
Jaegeuk Kime589c2c2016-06-02 15:24:24 -0700405 /* balance_fs_bg is able to be pending */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700406 if (need && excess_cached_nats(sbi))
Jaegeuk Kime589c2c2016-06-02 15:24:24 -0700407 f2fs_balance_fs_bg(sbi);
408
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900409 /*
Jaegeuk Kim029cd282012-12-21 17:20:21 +0900410 * We should do GC or end up with checkpoint, if there are so many dirty
411 * dir/node pages without enough free segments.
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900412 */
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -0700413 if (has_not_enough_free_secs(sbi, 0, 0)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900414 mutex_lock(&sbi->gc_mutex);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700415 f2fs_gc(sbi, false, false, NULL_SEGNO);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900416 }
417}
418
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900419void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi)
420{
Chao Yu1dcc3362015-02-05 17:57:31 +0800421 /* try to shrink extent cache when there is no enough memory */
Jaegeuk Kim554df792015-06-19 13:41:23 -0700422 if (!available_free_memory(sbi, EXTENT_CACHE))
423 f2fs_shrink_extent_tree(sbi, EXTENT_CACHE_SHRINK_NUMBER);
Chao Yu1dcc3362015-02-05 17:57:31 +0800424
Jaegeuk Kim1b38dc82015-06-19 15:36:07 -0700425 /* check the # of cached NAT entries */
426 if (!available_free_memory(sbi, NAT_ENTRIES))
427 try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK);
428
Chao Yu31696582015-07-28 18:33:46 +0800429 if (!available_free_memory(sbi, FREE_NIDS))
Jaegeuk Kimad4edb82016-06-16 16:41:49 -0700430 try_to_free_nids(sbi, MAX_FREE_NIDS);
431 else
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700432 build_free_nids(sbi, false, false);
433
434 if (!is_idle(sbi) && !excess_dirty_nats(sbi))
435 return;
Chao Yu31696582015-07-28 18:33:46 +0800436
Jaegeuk Kim1b38dc82015-06-19 15:36:07 -0700437 /* checkpoint is the only way to shrink partial cached entries */
438 if (!available_free_memory(sbi, NAT_ENTRIES) ||
Jaegeuk Kim60b99b42015-10-05 14:49:57 -0700439 !available_free_memory(sbi, INO_ENTRIES) ||
Chao Yu7d768d22016-01-18 18:31:18 +0800440 excess_prefree_segs(sbi) ||
441 excess_dirty_nats(sbi) ||
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700442 f2fs_time_over(sbi, CP_TIME)) {
Chao Yue9f5b8b2016-02-14 18:54:33 +0800443 if (test_opt(sbi, DATA_FLUSH)) {
444 struct blk_plug plug;
445
446 blk_start_plug(&plug);
Chao Yu36b35a02015-12-17 17:13:28 +0800447 sync_dirty_inodes(sbi, FILE_INODE);
Chao Yue9f5b8b2016-02-14 18:54:33 +0800448 blk_finish_plug(&plug);
449 }
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900450 f2fs_sync_fs(sbi->sb, true);
Jaegeuk Kim42190d22016-01-09 13:45:17 -0800451 stat_inc_bg_cp_count(sbi->stat_info);
Chao Yu36b35a02015-12-17 17:13:28 +0800452 }
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900453}
454
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700455static int __submit_flush_wait(struct f2fs_sb_info *sbi,
456 struct block_device *bdev)
457{
458 struct bio *bio = f2fs_bio_alloc(0);
459 int ret;
460
461 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH;
462 bio->bi_bdev = bdev;
463 ret = submit_bio_wait(bio);
464 bio_put(bio);
465
466 trace_f2fs_issue_flush(bdev, test_opt(sbi, NOBARRIER),
467 test_opt(sbi, FLUSH_MERGE), ret);
468 return ret;
469}
470
471static int submit_flush_wait(struct f2fs_sb_info *sbi)
472{
473 int ret = __submit_flush_wait(sbi, sbi->sb->s_bdev);
474 int i;
475
476 if (!sbi->s_ndevs || ret)
477 return ret;
478
479 for (i = 1; i < sbi->s_ndevs; i++) {
480 ret = __submit_flush_wait(sbi, FDEV(i).bdev);
481 if (ret)
482 break;
483 }
484 return ret;
485}
486
Gu Zheng2163d192014-04-27 14:21:33 +0800487static int issue_flush_thread(void *data)
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900488{
489 struct f2fs_sb_info *sbi = data;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700490 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800491 wait_queue_head_t *q = &fcc->flush_wait_queue;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900492repeat:
493 if (kthread_should_stop())
494 return 0;
495
Chao Yuce8bd4b2017-07-22 08:52:23 +0800496 sb_start_intwrite(sbi->sb);
497
Gu Zheng721bd4d2014-09-05 18:31:00 +0800498 if (!llist_empty(&fcc->issue_list)) {
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900499 struct flush_cmd *cmd, *next;
500 int ret;
501
Gu Zheng721bd4d2014-09-05 18:31:00 +0800502 fcc->dispatch_list = llist_del_all(&fcc->issue_list);
503 fcc->dispatch_list = llist_reverse_order(fcc->dispatch_list);
504
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700505 ret = submit_flush_wait(sbi);
506 atomic_inc(&fcc->issued_flush);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900507
Gu Zheng721bd4d2014-09-05 18:31:00 +0800508 llist_for_each_entry_safe(cmd, next,
509 fcc->dispatch_list, llnode) {
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900510 cmd->ret = ret;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900511 complete(&cmd->wait);
512 }
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800513 fcc->dispatch_list = NULL;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900514 }
515
Chao Yuce8bd4b2017-07-22 08:52:23 +0800516 sb_end_intwrite(sbi->sb);
517
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800518 wait_event_interruptible(*q,
Gu Zheng721bd4d2014-09-05 18:31:00 +0800519 kthread_should_stop() || !llist_empty(&fcc->issue_list));
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900520 goto repeat;
521}
522
523int f2fs_issue_flush(struct f2fs_sb_info *sbi)
524{
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700525 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
Chao Yuadf8d902014-05-08 17:00:35 +0800526 struct flush_cmd cmd;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700527 int ret;
Jaegeuk Kim24a9ee02014-07-25 17:46:10 -0700528
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700529 if (test_opt(sbi, NOBARRIER))
530 return 0;
531
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700532 if (!test_opt(sbi, FLUSH_MERGE)) {
533 ret = submit_flush_wait(sbi);
534 atomic_inc(&fcc->issued_flush);
535 return ret;
536 }
Jaegeuk Kim740432f2015-08-14 11:43:56 -0700537
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700538 if (!atomic_read(&fcc->issing_flush)) {
539 atomic_inc(&fcc->issing_flush);
540 ret = submit_flush_wait(sbi);
541 atomic_dec(&fcc->issing_flush);
542
543 atomic_inc(&fcc->issued_flush);
Jaegeuk Kim740432f2015-08-14 11:43:56 -0700544 return ret;
545 }
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900546
Chao Yuadf8d902014-05-08 17:00:35 +0800547 init_completion(&cmd.wait);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900548
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700549 atomic_inc(&fcc->issing_flush);
Gu Zheng721bd4d2014-09-05 18:31:00 +0800550 llist_add(&cmd.llnode, &fcc->issue_list);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900551
Chao Yu4aab88e2017-08-21 22:53:45 +0800552 /* update issue_list before we wake up issue_flush thread */
553 smp_mb();
554
555 if (waitqueue_active(&fcc->flush_wait_queue))
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800556 wake_up(&fcc->flush_wait_queue);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900557
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700558 if (fcc->f2fs_issue_flush) {
559 wait_for_completion(&cmd.wait);
560 atomic_dec(&fcc->issing_flush);
561 } else {
562 llist_del_all(&fcc->issue_list);
563 atomic_set(&fcc->issing_flush, 0);
564 }
Chao Yuadf8d902014-05-08 17:00:35 +0800565
566 return cmd.ret;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900567}
568
Gu Zheng2163d192014-04-27 14:21:33 +0800569int create_flush_cmd_control(struct f2fs_sb_info *sbi)
570{
571 dev_t dev = sbi->sb->s_bdev->bd_dev;
572 struct flush_cmd_control *fcc;
573 int err = 0;
574
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700575 if (SM_I(sbi)->fcc_info) {
576 fcc = SM_I(sbi)->fcc_info;
Yunlong Song471cd232017-06-24 15:57:19 +0800577 if (fcc->f2fs_issue_flush)
578 return err;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700579 goto init_thread;
580 }
581
Gu Zheng2163d192014-04-27 14:21:33 +0800582 fcc = kzalloc(sizeof(struct flush_cmd_control), GFP_KERNEL);
583 if (!fcc)
584 return -ENOMEM;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700585 atomic_set(&fcc->issued_flush, 0);
586 atomic_set(&fcc->issing_flush, 0);
Gu Zheng2163d192014-04-27 14:21:33 +0800587 init_waitqueue_head(&fcc->flush_wait_queue);
Gu Zheng721bd4d2014-09-05 18:31:00 +0800588 init_llist_head(&fcc->issue_list);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700589 SM_I(sbi)->fcc_info = fcc;
Yunlei Head9baf42017-06-01 16:43:51 +0800590 if (!test_opt(sbi, FLUSH_MERGE))
591 return err;
592
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700593init_thread:
Gu Zheng2163d192014-04-27 14:21:33 +0800594 fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi,
595 "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev));
596 if (IS_ERR(fcc->f2fs_issue_flush)) {
597 err = PTR_ERR(fcc->f2fs_issue_flush);
598 kfree(fcc);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700599 SM_I(sbi)->fcc_info = NULL;
Gu Zheng2163d192014-04-27 14:21:33 +0800600 return err;
601 }
Gu Zheng2163d192014-04-27 14:21:33 +0800602
603 return err;
604}
605
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700606void destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free)
Gu Zheng2163d192014-04-27 14:21:33 +0800607{
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700608 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
Gu Zheng2163d192014-04-27 14:21:33 +0800609
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700610 if (fcc && fcc->f2fs_issue_flush) {
611 struct task_struct *flush_thread = fcc->f2fs_issue_flush;
612
613 fcc->f2fs_issue_flush = NULL;
614 kthread_stop(flush_thread);
615 }
616 if (free) {
617 kfree(fcc);
618 SM_I(sbi)->fcc_info = NULL;
619 }
Gu Zheng2163d192014-04-27 14:21:33 +0800620}
621
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900622static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
623 enum dirty_type dirty_type)
624{
625 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
626
627 /* need not be added */
628 if (IS_CURSEG(sbi, segno))
629 return;
630
631 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
632 dirty_i->nr_dirty[dirty_type]++;
633
634 if (dirty_type == DIRTY) {
635 struct seg_entry *sentry = get_seg_entry(sbi, segno);
Changman Lee4625d6a2013-10-25 17:31:57 +0900636 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900637
Jaegeuk Kimec325b52014-09-02 16:24:11 -0700638 if (unlikely(t >= DIRTY)) {
639 f2fs_bug_on(sbi, 1);
640 return;
641 }
Changman Lee4625d6a2013-10-25 17:31:57 +0900642 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
643 dirty_i->nr_dirty[t]++;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900644 }
645}
646
647static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
648 enum dirty_type dirty_type)
649{
650 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
651
652 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
653 dirty_i->nr_dirty[dirty_type]--;
654
655 if (dirty_type == DIRTY) {
Changman Lee4625d6a2013-10-25 17:31:57 +0900656 struct seg_entry *sentry = get_seg_entry(sbi, segno);
657 enum dirty_type t = sentry->type;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900658
Changman Lee4625d6a2013-10-25 17:31:57 +0900659 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
660 dirty_i->nr_dirty[t]--;
Jaegeuk Kimb2f2c392013-04-01 13:52:09 +0900661
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700662 if (get_valid_blocks(sbi, segno, true) == 0)
663 clear_bit(GET_SEC_FROM_SEG(sbi, segno),
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +0900664 dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900665 }
666}
667
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900668/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900669 * Should not occur error such as -ENOMEM.
670 * Adding dirty entry into seglist is not critical operation.
671 * If a given segment is one of current working segments, it won't be added.
672 */
Haicheng Li8d8451a2013-06-13 16:59:28 +0800673static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900674{
675 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
676 unsigned short valid_blocks;
677
678 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
679 return;
680
681 mutex_lock(&dirty_i->seglist_lock);
682
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700683 valid_blocks = get_valid_blocks(sbi, segno, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900684
685 if (valid_blocks == 0) {
686 __locate_dirty_segment(sbi, segno, PRE);
687 __remove_dirty_segment(sbi, segno, DIRTY);
688 } else if (valid_blocks < sbi->blocks_per_seg) {
689 __locate_dirty_segment(sbi, segno, DIRTY);
690 } else {
691 /* Recovery routine with SSR needs this */
692 __remove_dirty_segment(sbi, segno, DIRTY);
693 }
694
695 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +0900696}
697
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700698static struct discard_cmd *__create_discard_cmd(struct f2fs_sb_info *sbi,
699 struct block_device *bdev, block_t lstart,
700 block_t start, block_t len)
Chao Yu275b66b2016-08-29 23:58:34 +0800701{
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700702 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
703 struct list_head *pend_list;
704 struct discard_cmd *dc;
Chao Yu275b66b2016-08-29 23:58:34 +0800705
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700706 f2fs_bug_on(sbi, !len);
Chao Yu275b66b2016-08-29 23:58:34 +0800707
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700708 pend_list = &dcc->pend_list[plist_idx(len)];
709
710 dc = f2fs_kmem_cache_alloc(discard_cmd_slab, GFP_NOFS);
711 INIT_LIST_HEAD(&dc->list);
712 dc->bdev = bdev;
713 dc->lstart = lstart;
714 dc->start = start;
715 dc->len = len;
716 dc->ref = 0;
717 dc->state = D_PREP;
718 dc->error = 0;
719 init_completion(&dc->wait);
720 list_add_tail(&dc->list, pend_list);
721 atomic_inc(&dcc->discard_cmd_cnt);
722 dcc->undiscard_blks += len;
723
724 return dc;
Chao Yu275b66b2016-08-29 23:58:34 +0800725}
726
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700727static struct discard_cmd *__attach_discard_cmd(struct f2fs_sb_info *sbi,
728 struct block_device *bdev, block_t lstart,
729 block_t start, block_t len,
730 struct rb_node *parent, struct rb_node **p)
Chao Yu275b66b2016-08-29 23:58:34 +0800731{
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700732 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
733 struct discard_cmd *dc;
Chao Yu275b66b2016-08-29 23:58:34 +0800734
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700735 dc = __create_discard_cmd(sbi, bdev, lstart, start, len);
Chao Yu275b66b2016-08-29 23:58:34 +0800736
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700737 rb_link_node(&dc->rb_node, parent, p);
738 rb_insert_color(&dc->rb_node, &dcc->root);
Chao Yu275b66b2016-08-29 23:58:34 +0800739
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700740 return dc;
Chao Yu275b66b2016-08-29 23:58:34 +0800741}
742
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700743static void __detach_discard_cmd(struct discard_cmd_control *dcc,
744 struct discard_cmd *dc)
Chao Yu275b66b2016-08-29 23:58:34 +0800745{
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700746 if (dc->state == D_DONE)
747 atomic_dec(&dcc->issing_discard);
Chao Yu275b66b2016-08-29 23:58:34 +0800748
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700749 list_del(&dc->list);
750 rb_erase(&dc->rb_node, &dcc->root);
751 dcc->undiscard_blks -= dc->len;
752
753 kmem_cache_free(discard_cmd_slab, dc);
754
755 atomic_dec(&dcc->discard_cmd_cnt);
756}
757
758static void __remove_discard_cmd(struct f2fs_sb_info *sbi,
759 struct discard_cmd *dc)
760{
761 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
762
Chao Yueb6c48e2017-06-05 18:29:07 +0800763 f2fs_bug_on(sbi, dc->ref);
764
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700765 if (dc->error == -EOPNOTSUPP)
766 dc->error = 0;
767
768 if (dc->error)
769 f2fs_msg(sbi->sb, KERN_INFO,
Chao Yufa681212017-05-19 23:46:43 +0800770 "Issue discard(%u, %u, %u) failed, ret: %d",
771 dc->lstart, dc->start, dc->len, dc->error);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700772 __detach_discard_cmd(dcc, dc);
773}
774
775static void f2fs_submit_discard_endio(struct bio *bio)
776{
777 struct discard_cmd *dc = (struct discard_cmd *)bio->bi_private;
778
779 dc->error = bio->bi_error;
780 dc->state = D_DONE;
Chao Yub1f5c762017-05-19 23:46:44 +0800781 complete_all(&dc->wait);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700782 bio_put(bio);
Chao Yu275b66b2016-08-29 23:58:34 +0800783}
784
Chao Yu280fd0f2017-06-30 17:19:02 +0800785void __check_sit_bitmap(struct f2fs_sb_info *sbi,
786 block_t start, block_t end)
787{
788#ifdef CONFIG_F2FS_CHECK_FS
789 struct seg_entry *sentry;
790 unsigned int segno;
791 block_t blk = start;
792 unsigned long offset, size, max_blocks = sbi->blocks_per_seg;
793 unsigned long *map;
794
795 while (blk < end) {
796 segno = GET_SEGNO(sbi, blk);
797 sentry = get_seg_entry(sbi, segno);
798 offset = GET_BLKOFF_FROM_SEG0(sbi, blk);
799
Yunlong Song55824ab2017-08-04 17:07:15 +0800800 if (end < START_BLOCK(sbi, segno + 1))
801 size = GET_BLKOFF_FROM_SEG0(sbi, end);
802 else
803 size = max_blocks;
Chao Yu280fd0f2017-06-30 17:19:02 +0800804 map = (unsigned long *)(sentry->cur_valid_map);
805 offset = __find_rev_next_bit(map, size, offset);
806 f2fs_bug_on(sbi, offset != size);
Yunlong Song55824ab2017-08-04 17:07:15 +0800807 blk = START_BLOCK(sbi, segno + 1);
Chao Yu280fd0f2017-06-30 17:19:02 +0800808 }
809#endif
810}
811
Chao Yu275b66b2016-08-29 23:58:34 +0800812/* this function is copied from blkdev_issue_discard from block/blk-lib.c */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700813static void __submit_discard_cmd(struct f2fs_sb_info *sbi,
814 struct discard_cmd *dc)
Chao Yu275b66b2016-08-29 23:58:34 +0800815{
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700816 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Chao Yu275b66b2016-08-29 23:58:34 +0800817 struct bio *bio = NULL;
Chao Yu275b66b2016-08-29 23:58:34 +0800818
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700819 if (dc->state != D_PREP)
820 return;
Chao Yu275b66b2016-08-29 23:58:34 +0800821
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700822 trace_f2fs_issue_discard(dc->bdev, dc->start, dc->len);
823
824 dc->error = __blkdev_issue_discard(dc->bdev,
825 SECTOR_FROM_BLOCK(dc->start),
826 SECTOR_FROM_BLOCK(dc->len),
827 GFP_NOFS, 0, &bio);
828 if (!dc->error) {
829 /* should keep before submission to avoid D_DONE right away */
830 dc->state = D_SUBMIT;
831 atomic_inc(&dcc->issued_discard);
832 atomic_inc(&dcc->issing_discard);
833 if (bio) {
834 bio->bi_private = dc;
835 bio->bi_end_io = f2fs_submit_discard_endio;
836 bio->bi_opf |= REQ_SYNC;
837 submit_bio(bio);
838 list_move_tail(&dc->list, &dcc->wait_list);
Chao Yu280fd0f2017-06-30 17:19:02 +0800839 __check_sit_bitmap(sbi, dc->start, dc->start + dc->len);
Chao Yuc0fe4882017-08-02 23:21:48 +0800840
841 f2fs_update_iostat(sbi, FS_DISCARD, 1);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700842 }
843 } else {
844 __remove_discard_cmd(sbi, dc);
845 }
846}
847
848static struct discard_cmd *__insert_discard_tree(struct f2fs_sb_info *sbi,
849 struct block_device *bdev, block_t lstart,
850 block_t start, block_t len,
851 struct rb_node **insert_p,
852 struct rb_node *insert_parent)
853{
854 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
855 struct rb_node **p = &dcc->root.rb_node;
856 struct rb_node *parent = NULL;
857 struct discard_cmd *dc = NULL;
858
859 if (insert_p && insert_parent) {
860 parent = insert_parent;
861 p = insert_p;
862 goto do_insert;
Chao Yu275b66b2016-08-29 23:58:34 +0800863 }
864
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700865 p = __lookup_rb_tree_for_insert(sbi, &dcc->root, &parent, lstart);
866do_insert:
867 dc = __attach_discard_cmd(sbi, bdev, lstart, start, len, parent, p);
868 if (!dc)
869 return NULL;
870
871 return dc;
872}
873
874static void __relocate_discard_cmd(struct discard_cmd_control *dcc,
875 struct discard_cmd *dc)
876{
877 list_move_tail(&dc->list, &dcc->pend_list[plist_idx(dc->len)]);
878}
879
880static void __punch_discard_cmd(struct f2fs_sb_info *sbi,
881 struct discard_cmd *dc, block_t blkaddr)
882{
883 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
884 struct discard_info di = dc->di;
885 bool modified = false;
886
887 if (dc->state == D_DONE || dc->len == 1) {
888 __remove_discard_cmd(sbi, dc);
889 return;
890 }
891
892 dcc->undiscard_blks -= di.len;
893
894 if (blkaddr > di.lstart) {
895 dc->len = blkaddr - dc->lstart;
896 dcc->undiscard_blks += dc->len;
897 __relocate_discard_cmd(dcc, dc);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700898 modified = true;
899 }
900
901 if (blkaddr < di.lstart + di.len - 1) {
902 if (modified) {
903 __insert_discard_tree(sbi, dc->bdev, blkaddr + 1,
904 di.start + blkaddr + 1 - di.lstart,
905 di.lstart + di.len - 1 - blkaddr,
906 NULL, NULL);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700907 } else {
908 dc->lstart++;
909 dc->len--;
910 dc->start++;
911 dcc->undiscard_blks += dc->len;
912 __relocate_discard_cmd(dcc, dc);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700913 }
914 }
915}
916
917static void __update_discard_tree_range(struct f2fs_sb_info *sbi,
918 struct block_device *bdev, block_t lstart,
919 block_t start, block_t len)
920{
921 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
922 struct discard_cmd *prev_dc = NULL, *next_dc = NULL;
923 struct discard_cmd *dc;
924 struct discard_info di = {0};
925 struct rb_node **insert_p = NULL, *insert_parent = NULL;
926 block_t end = lstart + len;
927
928 mutex_lock(&dcc->cmd_lock);
929
930 dc = (struct discard_cmd *)__lookup_rb_tree_ret(&dcc->root,
931 NULL, lstart,
932 (struct rb_entry **)&prev_dc,
933 (struct rb_entry **)&next_dc,
934 &insert_p, &insert_parent, true);
935 if (dc)
936 prev_dc = dc;
937
938 if (!prev_dc) {
939 di.lstart = lstart;
940 di.len = next_dc ? next_dc->lstart - lstart : len;
941 di.len = min(di.len, len);
942 di.start = start;
943 }
944
945 while (1) {
946 struct rb_node *node;
947 bool merged = false;
948 struct discard_cmd *tdc = NULL;
949
950 if (prev_dc) {
951 di.lstart = prev_dc->lstart + prev_dc->len;
952 if (di.lstart < lstart)
953 di.lstart = lstart;
954 if (di.lstart >= end)
955 break;
956
957 if (!next_dc || next_dc->lstart > end)
958 di.len = end - di.lstart;
959 else
960 di.len = next_dc->lstart - di.lstart;
961 di.start = start + di.lstart - lstart;
962 }
963
964 if (!di.len)
965 goto next;
966
967 if (prev_dc && prev_dc->state == D_PREP &&
968 prev_dc->bdev == bdev &&
969 __is_discard_back_mergeable(&di, &prev_dc->di)) {
970 prev_dc->di.len += di.len;
971 dcc->undiscard_blks += di.len;
972 __relocate_discard_cmd(dcc, prev_dc);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700973 di = prev_dc->di;
974 tdc = prev_dc;
975 merged = true;
976 }
977
978 if (next_dc && next_dc->state == D_PREP &&
979 next_dc->bdev == bdev &&
980 __is_discard_front_mergeable(&di, &next_dc->di)) {
981 next_dc->di.lstart = di.lstart;
982 next_dc->di.len += di.len;
983 next_dc->di.start = di.start;
984 dcc->undiscard_blks += di.len;
985 __relocate_discard_cmd(dcc, next_dc);
986 if (tdc)
987 __remove_discard_cmd(sbi, tdc);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700988 merged = true;
989 }
990
991 if (!merged) {
992 __insert_discard_tree(sbi, bdev, di.lstart, di.start,
993 di.len, NULL, NULL);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700994 }
995 next:
996 prev_dc = next_dc;
997 if (!prev_dc)
998 break;
999
1000 node = rb_next(&prev_dc->rb_node);
1001 next_dc = rb_entry_safe(node, struct discard_cmd, rb_node);
1002 }
1003
1004 mutex_unlock(&dcc->cmd_lock);
1005}
1006
1007static int __queue_discard_cmd(struct f2fs_sb_info *sbi,
1008 struct block_device *bdev, block_t blkstart, block_t blklen)
1009{
1010 block_t lblkstart = blkstart;
1011
1012 trace_f2fs_queue_discard(bdev, blkstart, blklen);
1013
1014 if (sbi->s_ndevs) {
1015 int devi = f2fs_target_device_index(sbi, blkstart);
1016
1017 blkstart -= FDEV(devi).start_blk;
1018 }
1019 __update_discard_tree_range(sbi, bdev, lblkstart, blkstart, blklen);
1020 return 0;
1021}
1022
Chao Yuefe24da2017-08-07 23:09:56 +08001023static int __issue_discard_cmd(struct f2fs_sb_info *sbi, bool issue_cond)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001024{
1025 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1026 struct list_head *pend_list;
1027 struct discard_cmd *dc, *tmp;
1028 struct blk_plug plug;
Chao Yuefe24da2017-08-07 23:09:56 +08001029 int iter = 0, issued = 0;
1030 int i;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001031
1032 mutex_lock(&dcc->cmd_lock);
Yunlei He050e8332017-05-19 14:42:12 +08001033 f2fs_bug_on(sbi,
1034 !__check_rb_tree_consistence(sbi, &dcc->root));
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001035 blk_start_plug(&plug);
Chao Yuefe24da2017-08-07 23:09:56 +08001036 for (i = MAX_PLIST_NUM - 1;
1037 i >= 0 && plist_issue(dcc->pend_list_tag[i]); i--) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001038 pend_list = &dcc->pend_list[i];
1039 list_for_each_entry_safe(dc, tmp, pend_list, list) {
1040 f2fs_bug_on(sbi, dc->state != D_PREP);
1041
Chao Yuefe24da2017-08-07 23:09:56 +08001042 /* Hurry up to finish fstrim */
1043 if (dcc->pend_list_tag[i] & P_TRIM) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001044 __submit_discard_cmd(sbi, dc);
Chao Yuefe24da2017-08-07 23:09:56 +08001045 issued++;
1046 continue;
1047 }
1048
1049 if (!issue_cond || is_idle(sbi)) {
1050 issued++;
1051 __submit_discard_cmd(sbi, dc);
1052 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001053 if (issue_cond && iter++ > DISCARD_ISSUE_RATE)
1054 goto out;
1055 }
Chao Yuefe24da2017-08-07 23:09:56 +08001056 if (list_empty(pend_list) && dcc->pend_list_tag[i] & P_TRIM)
1057 dcc->pend_list_tag[i] &= (~P_TRIM);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001058 }
1059out:
1060 blk_finish_plug(&plug);
1061 mutex_unlock(&dcc->cmd_lock);
Chao Yuefe24da2017-08-07 23:09:56 +08001062
1063 return issued;
1064}
1065
1066static void __drop_discard_cmd(struct f2fs_sb_info *sbi)
1067{
1068 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1069 struct list_head *pend_list;
1070 struct discard_cmd *dc, *tmp;
1071 int i;
1072
1073 mutex_lock(&dcc->cmd_lock);
1074 for (i = MAX_PLIST_NUM - 1; i >= 0; i--) {
1075 pend_list = &dcc->pend_list[i];
1076 list_for_each_entry_safe(dc, tmp, pend_list, list) {
1077 f2fs_bug_on(sbi, dc->state != D_PREP);
1078 __remove_discard_cmd(sbi, dc);
1079 }
1080 }
1081 mutex_unlock(&dcc->cmd_lock);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001082}
1083
Chao Yua7aa97d2017-06-05 18:29:06 +08001084static void __wait_one_discard_bio(struct f2fs_sb_info *sbi,
1085 struct discard_cmd *dc)
1086{
1087 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1088
1089 wait_for_completion_io(&dc->wait);
1090 mutex_lock(&dcc->cmd_lock);
1091 f2fs_bug_on(sbi, dc->state != D_DONE);
1092 dc->ref--;
1093 if (!dc->ref)
1094 __remove_discard_cmd(sbi, dc);
1095 mutex_unlock(&dcc->cmd_lock);
1096}
1097
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001098static void __wait_discard_cmd(struct f2fs_sb_info *sbi, bool wait_cond)
1099{
1100 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1101 struct list_head *wait_list = &(dcc->wait_list);
1102 struct discard_cmd *dc, *tmp;
Chao Yu3215e782017-05-19 23:46:45 +08001103 bool need_wait;
1104
1105next:
1106 need_wait = false;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001107
1108 mutex_lock(&dcc->cmd_lock);
1109 list_for_each_entry_safe(dc, tmp, wait_list, list) {
Chao Yu3215e782017-05-19 23:46:45 +08001110 if (!wait_cond || (dc->state == D_DONE && !dc->ref)) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001111 wait_for_completion_io(&dc->wait);
1112 __remove_discard_cmd(sbi, dc);
Chao Yu3215e782017-05-19 23:46:45 +08001113 } else {
1114 dc->ref++;
1115 need_wait = true;
1116 break;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001117 }
1118 }
1119 mutex_unlock(&dcc->cmd_lock);
Chao Yu3215e782017-05-19 23:46:45 +08001120
1121 if (need_wait) {
Chao Yua7aa97d2017-06-05 18:29:06 +08001122 __wait_one_discard_bio(sbi, dc);
Chao Yu3215e782017-05-19 23:46:45 +08001123 goto next;
1124 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001125}
1126
1127/* This should be covered by global mutex, &sit_i->sentry_lock */
1128void f2fs_wait_discard_bio(struct f2fs_sb_info *sbi, block_t blkaddr)
1129{
1130 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1131 struct discard_cmd *dc;
1132 bool need_wait = false;
1133
1134 mutex_lock(&dcc->cmd_lock);
1135 dc = (struct discard_cmd *)__lookup_rb_tree(&dcc->root, NULL, blkaddr);
1136 if (dc) {
1137 if (dc->state == D_PREP) {
1138 __punch_discard_cmd(sbi, dc, blkaddr);
1139 } else {
1140 dc->ref++;
1141 need_wait = true;
1142 }
1143 }
1144 mutex_unlock(&dcc->cmd_lock);
1145
Chao Yua7aa97d2017-06-05 18:29:06 +08001146 if (need_wait)
1147 __wait_one_discard_bio(sbi, dc);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001148}
1149
Chao Yud9d85cc2017-06-29 23:17:45 +08001150void stop_discard_thread(struct f2fs_sb_info *sbi)
1151{
1152 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1153
1154 if (dcc && dcc->f2fs_issue_discard) {
1155 struct task_struct *discard_thread = dcc->f2fs_issue_discard;
1156
1157 dcc->f2fs_issue_discard = NULL;
1158 kthread_stop(discard_thread);
1159 }
1160}
1161
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001162/* This comes from f2fs_put_super */
1163void f2fs_wait_discard_bios(struct f2fs_sb_info *sbi)
1164{
1165 __issue_discard_cmd(sbi, false);
Chao Yuefe24da2017-08-07 23:09:56 +08001166 __drop_discard_cmd(sbi);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001167 __wait_discard_cmd(sbi, false);
1168}
1169
Chao Yuefe24da2017-08-07 23:09:56 +08001170static void mark_discard_range_all(struct f2fs_sb_info *sbi)
1171{
1172 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1173 int i;
1174
1175 mutex_lock(&dcc->cmd_lock);
1176 for (i = 0; i < MAX_PLIST_NUM; i++)
1177 dcc->pend_list_tag[i] |= P_TRIM;
1178 mutex_unlock(&dcc->cmd_lock);
1179}
1180
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001181static int issue_discard_thread(void *data)
1182{
1183 struct f2fs_sb_info *sbi = data;
1184 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1185 wait_queue_head_t *q = &dcc->discard_wait_queue;
Chao Yuefe24da2017-08-07 23:09:56 +08001186 unsigned int wait_ms = DEF_MIN_DISCARD_ISSUE_TIME;
1187 int issued;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001188
Jaegeuk Kimd430af22017-05-17 10:36:58 -07001189 set_freezable();
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001190
Jaegeuk Kimd430af22017-05-17 10:36:58 -07001191 do {
Chao Yuefe24da2017-08-07 23:09:56 +08001192 wait_event_interruptible_timeout(*q,
1193 kthread_should_stop() || freezing(current) ||
1194 dcc->discard_wake,
1195 msecs_to_jiffies(wait_ms));
Jaegeuk Kimd430af22017-05-17 10:36:58 -07001196 if (try_to_freeze())
1197 continue;
1198 if (kthread_should_stop())
1199 return 0;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001200
Jaegeuk Kim766330d2017-08-15 21:27:19 -07001201 if (dcc->discard_wake) {
Chao Yuefe24da2017-08-07 23:09:56 +08001202 dcc->discard_wake = 0;
Jaegeuk Kim766330d2017-08-15 21:27:19 -07001203 if (sbi->gc_thread && sbi->gc_thread->gc_urgent)
1204 mark_discard_range_all(sbi);
1205 }
Chao Yuefe24da2017-08-07 23:09:56 +08001206
Chao Yuce8bd4b2017-07-22 08:52:23 +08001207 sb_start_intwrite(sbi->sb);
1208
Chao Yuefe24da2017-08-07 23:09:56 +08001209 issued = __issue_discard_cmd(sbi, true);
1210 if (issued) {
1211 __wait_discard_cmd(sbi, true);
1212 wait_ms = DEF_MIN_DISCARD_ISSUE_TIME;
1213 } else {
1214 wait_ms = DEF_MAX_DISCARD_ISSUE_TIME;
1215 }
Jaegeuk Kimd430af22017-05-17 10:36:58 -07001216
Chao Yuce8bd4b2017-07-22 08:52:23 +08001217 sb_end_intwrite(sbi->sb);
1218
Jaegeuk Kimd430af22017-05-17 10:36:58 -07001219 } while (!kthread_should_stop());
1220 return 0;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001221}
1222
1223#ifdef CONFIG_BLK_DEV_ZONED
1224static int __f2fs_issue_discard_zone(struct f2fs_sb_info *sbi,
1225 struct block_device *bdev, block_t blkstart, block_t blklen)
1226{
1227 sector_t sector, nr_sects;
1228 block_t lblkstart = blkstart;
1229 int devi = 0;
1230
1231 if (sbi->s_ndevs) {
1232 devi = f2fs_target_device_index(sbi, blkstart);
1233 blkstart -= FDEV(devi).start_blk;
1234 }
1235
1236 /*
1237 * We need to know the type of the zone: for conventional zones,
1238 * use regular discard if the drive supports it. For sequential
1239 * zones, reset the zone write pointer.
1240 */
1241 switch (get_blkz_type(sbi, bdev, blkstart)) {
1242
1243 case BLK_ZONE_TYPE_CONVENTIONAL:
1244 if (!blk_queue_discard(bdev_get_queue(bdev)))
1245 return 0;
1246 return __queue_discard_cmd(sbi, bdev, lblkstart, blklen);
1247 case BLK_ZONE_TYPE_SEQWRITE_REQ:
1248 case BLK_ZONE_TYPE_SEQWRITE_PREF:
1249 sector = SECTOR_FROM_BLOCK(blkstart);
1250 nr_sects = SECTOR_FROM_BLOCK(blklen);
1251
1252 if (sector & (bdev_zone_sectors(bdev) - 1) ||
1253 nr_sects != bdev_zone_sectors(bdev)) {
1254 f2fs_msg(sbi->sb, KERN_INFO,
1255 "(%d) %s: Unaligned discard attempted (block %x + %x)",
1256 devi, sbi->s_ndevs ? FDEV(devi).path: "",
1257 blkstart, blklen);
1258 return -EIO;
1259 }
1260 trace_f2fs_issue_reset_zone(bdev, blkstart);
1261 return blkdev_reset_zones(bdev, sector,
1262 nr_sects, GFP_NOFS);
1263 default:
1264 /* Unknown zone type: broken device ? */
1265 return -EIO;
1266 }
1267}
1268#endif
1269
1270static int __issue_discard_async(struct f2fs_sb_info *sbi,
1271 struct block_device *bdev, block_t blkstart, block_t blklen)
1272{
1273#ifdef CONFIG_BLK_DEV_ZONED
1274 if (f2fs_sb_mounted_blkzoned(sbi->sb) &&
1275 bdev_zoned_model(bdev) != BLK_ZONED_NONE)
1276 return __f2fs_issue_discard_zone(sbi, bdev, blkstart, blklen);
1277#endif
1278 return __queue_discard_cmd(sbi, bdev, blkstart, blklen);
Chao Yu275b66b2016-08-29 23:58:34 +08001279}
1280
Jaegeuk Kim1e87a782014-04-15 13:57:55 +09001281static int f2fs_issue_discard(struct f2fs_sb_info *sbi,
Jaegeuk Kim37208872013-11-12 16:55:17 +09001282 block_t blkstart, block_t blklen)
1283{
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001284 sector_t start = blkstart, len = 0;
1285 struct block_device *bdev;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001286 struct seg_entry *se;
1287 unsigned int offset;
1288 block_t i;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001289 int err = 0;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001290
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001291 bdev = f2fs_target_device(sbi, blkstart, NULL);
1292
1293 for (i = blkstart; i < blkstart + blklen; i++, len++) {
1294 if (i != start) {
1295 struct block_device *bdev2 =
1296 f2fs_target_device(sbi, i, NULL);
1297
1298 if (bdev2 != bdev) {
1299 err = __issue_discard_async(sbi, bdev,
1300 start, len);
1301 if (err)
1302 return err;
1303 bdev = bdev2;
1304 start = i;
1305 len = 0;
1306 }
1307 }
1308
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001309 se = get_seg_entry(sbi, GET_SEGNO(sbi, i));
1310 offset = GET_BLKOFF_FROM_SEG0(sbi, i);
1311
1312 if (!f2fs_test_and_set_bit(offset, se->discard_map))
1313 sbi->discard_blks--;
1314 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001315
1316 if (len)
1317 err = __issue_discard_async(sbi, bdev, start, len);
1318 return err;
Jaegeuk Kim1e87a782014-04-15 13:57:55 +09001319}
1320
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001321static bool add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc,
1322 bool check_only)
Jaegeuk Kimadf49832014-10-28 22:27:59 -07001323{
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001324 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
1325 int max_blocks = sbi->blocks_per_seg;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001326 struct seg_entry *se = get_seg_entry(sbi, cpc->trim_start);
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001327 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
1328 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001329 unsigned long *discard_map = (unsigned long *)se->discard_map;
Jaegeuk Kim60a3b782015-02-10 16:44:29 -08001330 unsigned long *dmap = SIT_I(sbi)->tmp_map;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001331 unsigned int start = 0, end = -1;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001332 bool force = (cpc->reason & CP_DISCARD);
1333 struct discard_entry *de = NULL;
1334 struct list_head *head = &SM_I(sbi)->dcc_info->entry_list;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001335 int i;
1336
Jaegeuk Kim3e025742016-08-02 10:56:40 -07001337 if (se->valid_blocks == max_blocks || !f2fs_discard_en(sbi))
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001338 return false;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001339
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001340 if (!force) {
1341 if (!test_opt(sbi, DISCARD) || !se->valid_blocks ||
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001342 SM_I(sbi)->dcc_info->nr_discards >=
1343 SM_I(sbi)->dcc_info->max_discards)
1344 return false;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001345 }
1346
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001347 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
1348 for (i = 0; i < entries; i++)
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001349 dmap[i] = force ? ~ckpt_map[i] & ~discard_map[i] :
Jaegeuk Kimd7bc2482014-12-12 13:53:41 -08001350 (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001351
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001352 while (force || SM_I(sbi)->dcc_info->nr_discards <=
1353 SM_I(sbi)->dcc_info->max_discards) {
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001354 start = __find_rev_next_bit(dmap, max_blocks, end + 1);
1355 if (start >= max_blocks)
1356 break;
1357
1358 end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
Yunlei Hec7b41e12016-07-07 12:13:33 +08001359 if (force && start && end != max_blocks
1360 && (end - start) < cpc->trim_minlen)
1361 continue;
1362
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001363 if (check_only)
1364 return true;
1365
1366 if (!de) {
1367 de = f2fs_kmem_cache_alloc(discard_entry_slab,
1368 GFP_F2FS_ZERO);
1369 de->start_blkaddr = START_BLOCK(sbi, cpc->trim_start);
1370 list_add_tail(&de->list, head);
1371 }
1372
1373 for (i = start; i < end; i++)
1374 __set_bit_le(i, (void *)de->discard_map);
1375
1376 SM_I(sbi)->dcc_info->nr_discards += end - start;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001377 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001378 return false;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001379}
1380
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001381void release_discard_addrs(struct f2fs_sb_info *sbi)
1382{
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001383 struct list_head *head = &(SM_I(sbi)->dcc_info->entry_list);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001384 struct discard_entry *entry, *this;
1385
1386 /* drop caches */
1387 list_for_each_entry_safe(entry, this, head, list) {
1388 list_del(&entry->list);
1389 kmem_cache_free(discard_entry_slab, entry);
1390 }
1391}
1392
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001393/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001394 * Should call clear_prefree_segments after checkpoint is done.
1395 */
1396static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
1397{
1398 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Chao Yub65ee142014-08-04 10:10:07 +08001399 unsigned int segno;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001400
1401 mutex_lock(&dirty_i->seglist_lock);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001402 for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001403 __set_test_and_free(sbi, segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001404 mutex_unlock(&dirty_i->seglist_lock);
1405}
1406
Jaegeuk Kim836b5a62015-04-30 22:50:06 -07001407void clear_prefree_segments(struct f2fs_sb_info *sbi, struct cp_control *cpc)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001408{
Chao Yuefe24da2017-08-07 23:09:56 +08001409 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1410 struct list_head *head = &dcc->entry_list;
Chao Yu2d7b8222014-03-29 11:33:17 +08001411 struct discard_entry *entry, *this;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001412 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Changman Lee29e59c12013-11-11 09:24:37 +09001413 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
Changman Lee29e59c12013-11-11 09:24:37 +09001414 unsigned int start = 0, end = -1;
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001415 unsigned int secno, start_segno;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001416 bool force = (cpc->reason & CP_DISCARD);
Chao Yu275b66b2016-08-29 23:58:34 +08001417
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001418 mutex_lock(&dirty_i->seglist_lock);
Changman Lee29e59c12013-11-11 09:24:37 +09001419
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001420 while (1) {
Changman Lee29e59c12013-11-11 09:24:37 +09001421 int i;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001422 start = find_next_bit(prefree_map, MAIN_SEGS(sbi), end + 1);
1423 if (start >= MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001424 break;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001425 end = find_next_zero_bit(prefree_map, MAIN_SEGS(sbi),
1426 start + 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001427
Changman Lee29e59c12013-11-11 09:24:37 +09001428 for (i = start; i < end; i++)
1429 clear_bit(i, prefree_map);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001430
Changman Lee29e59c12013-11-11 09:24:37 +09001431 dirty_i->nr_dirty[PRE] -= end - start;
1432
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001433 if (!test_opt(sbi, DISCARD))
Changman Lee29e59c12013-11-11 09:24:37 +09001434 continue;
1435
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001436 if (force && start >= cpc->trim_start &&
1437 (end - 1) <= cpc->trim_end)
1438 continue;
1439
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001440 if (!test_opt(sbi, LFS) || sbi->segs_per_sec == 1) {
1441 f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
Jaegeuk Kim37208872013-11-12 16:55:17 +09001442 (end - start) << sbi->log_blocks_per_seg);
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001443 continue;
1444 }
1445next:
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001446 secno = GET_SEC_FROM_SEG(sbi, start);
1447 start_segno = GET_SEG_FROM_SEC(sbi, secno);
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001448 if (!IS_CURSEC(sbi, secno) &&
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001449 !get_valid_blocks(sbi, start, true))
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001450 f2fs_issue_discard(sbi, START_BLOCK(sbi, start_segno),
1451 sbi->segs_per_sec << sbi->log_blocks_per_seg);
1452
1453 start = start_segno + sbi->segs_per_sec;
1454 if (start < end)
1455 goto next;
Jaegeuk Kim8c53efc32017-02-27 11:57:11 -08001456 else
1457 end = start - 1;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001458 }
1459 mutex_unlock(&dirty_i->seglist_lock);
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001460
1461 /* send small discards */
Chao Yu2d7b8222014-03-29 11:33:17 +08001462 list_for_each_entry_safe(entry, this, head, list) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001463 unsigned int cur_pos = 0, next_pos, len, total_len = 0;
1464 bool is_valid = test_bit_le(0, entry->discard_map);
1465
1466find_next:
1467 if (is_valid) {
1468 next_pos = find_next_zero_bit_le(entry->discard_map,
1469 sbi->blocks_per_seg, cur_pos);
1470 len = next_pos - cur_pos;
1471
Damien Le Moal57a91232017-05-26 17:04:40 +09001472 if (f2fs_sb_mounted_blkzoned(sbi->sb) ||
1473 (force && len < cpc->trim_minlen))
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001474 goto skip;
1475
1476 f2fs_issue_discard(sbi, entry->start_blkaddr + cur_pos,
1477 len);
1478 cpc->trimmed += len;
1479 total_len += len;
1480 } else {
1481 next_pos = find_next_bit_le(entry->discard_map,
1482 sbi->blocks_per_seg, cur_pos);
1483 }
Jaegeuk Kim836b5a62015-04-30 22:50:06 -07001484skip:
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001485 cur_pos = next_pos;
1486 is_valid = !is_valid;
1487
1488 if (cur_pos < sbi->blocks_per_seg)
1489 goto find_next;
1490
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001491 list_del(&entry->list);
Chao Yuefe24da2017-08-07 23:09:56 +08001492 dcc->nr_discards -= total_len;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09001493 kmem_cache_free(discard_entry_slab, entry);
1494 }
Chao Yu275b66b2016-08-29 23:58:34 +08001495
Chao Yuefe24da2017-08-07 23:09:56 +08001496 dcc->discard_wake = 1;
1497 wake_up_interruptible_all(&dcc->discard_wait_queue);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001498}
1499
1500static int create_discard_cmd_control(struct f2fs_sb_info *sbi)
1501{
1502 dev_t dev = sbi->sb->s_bdev->bd_dev;
1503 struct discard_cmd_control *dcc;
1504 int err = 0, i;
1505
1506 if (SM_I(sbi)->dcc_info) {
1507 dcc = SM_I(sbi)->dcc_info;
1508 goto init_thread;
1509 }
1510
1511 dcc = kzalloc(sizeof(struct discard_cmd_control), GFP_KERNEL);
1512 if (!dcc)
1513 return -ENOMEM;
1514
Chao Yuefe24da2017-08-07 23:09:56 +08001515 dcc->discard_granularity = DEFAULT_DISCARD_GRANULARITY;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001516 INIT_LIST_HEAD(&dcc->entry_list);
Chao Yuefe24da2017-08-07 23:09:56 +08001517 for (i = 0; i < MAX_PLIST_NUM; i++) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001518 INIT_LIST_HEAD(&dcc->pend_list[i]);
Chao Yuefe24da2017-08-07 23:09:56 +08001519 if (i >= dcc->discard_granularity - 1)
1520 dcc->pend_list_tag[i] |= P_ACTIVE;
1521 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001522 INIT_LIST_HEAD(&dcc->wait_list);
1523 mutex_init(&dcc->cmd_lock);
1524 atomic_set(&dcc->issued_discard, 0);
1525 atomic_set(&dcc->issing_discard, 0);
1526 atomic_set(&dcc->discard_cmd_cnt, 0);
1527 dcc->nr_discards = 0;
1528 dcc->max_discards = MAIN_SEGS(sbi) << sbi->log_blocks_per_seg;
1529 dcc->undiscard_blks = 0;
1530 dcc->root = RB_ROOT;
1531
1532 init_waitqueue_head(&dcc->discard_wait_queue);
1533 SM_I(sbi)->dcc_info = dcc;
1534init_thread:
1535 dcc->f2fs_issue_discard = kthread_run(issue_discard_thread, sbi,
1536 "f2fs_discard-%u:%u", MAJOR(dev), MINOR(dev));
1537 if (IS_ERR(dcc->f2fs_issue_discard)) {
1538 err = PTR_ERR(dcc->f2fs_issue_discard);
1539 kfree(dcc);
1540 SM_I(sbi)->dcc_info = NULL;
1541 return err;
1542 }
1543
1544 return err;
1545}
1546
1547static void destroy_discard_cmd_control(struct f2fs_sb_info *sbi)
1548{
1549 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1550
1551 if (!dcc)
1552 return;
1553
Chao Yud9d85cc2017-06-29 23:17:45 +08001554 stop_discard_thread(sbi);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001555
1556 kfree(dcc);
1557 SM_I(sbi)->dcc_info = NULL;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001558}
1559
Chao Yu184a5cd2014-09-04 18:13:01 +08001560static bool __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001561{
1562 struct sit_info *sit_i = SIT_I(sbi);
Chao Yu184a5cd2014-09-04 18:13:01 +08001563
1564 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001565 sit_i->dirty_sentries++;
Chao Yu184a5cd2014-09-04 18:13:01 +08001566 return false;
1567 }
1568
1569 return true;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001570}
1571
1572static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
1573 unsigned int segno, int modified)
1574{
1575 struct seg_entry *se = get_seg_entry(sbi, segno);
1576 se->type = type;
1577 if (modified)
1578 __mark_sit_entry_dirty(sbi, segno);
1579}
1580
1581static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
1582{
1583 struct seg_entry *se;
1584 unsigned int segno, offset;
1585 long int new_vblocks;
Yunlong Song5ccdd092017-08-02 21:20:13 +08001586 bool exist;
1587#ifdef CONFIG_F2FS_CHECK_FS
1588 bool mir_exist;
1589#endif
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001590
1591 segno = GET_SEGNO(sbi, blkaddr);
1592
1593 se = get_seg_entry(sbi, segno);
1594 new_vblocks = se->valid_blocks + del;
Jaegeuk Kim491c0852014-02-04 13:01:10 +09001595 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001596
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001597 f2fs_bug_on(sbi, (new_vblocks >> (sizeof(unsigned short) << 3) ||
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001598 (new_vblocks > sbi->blocks_per_seg)));
1599
1600 se->valid_blocks = new_vblocks;
1601 se->mtime = get_mtime(sbi);
1602 SIT_I(sbi)->max_mtime = se->mtime;
1603
1604 /* Update valid block bitmap */
1605 if (del > 0) {
Yunlong Song5ccdd092017-08-02 21:20:13 +08001606 exist = f2fs_test_and_set_bit(offset, se->cur_valid_map);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001607#ifdef CONFIG_F2FS_CHECK_FS
Yunlong Song5ccdd092017-08-02 21:20:13 +08001608 mir_exist = f2fs_test_and_set_bit(offset,
1609 se->cur_valid_map_mir);
1610 if (unlikely(exist != mir_exist)) {
1611 f2fs_msg(sbi->sb, KERN_ERR, "Inconsistent error "
1612 "when setting bitmap, blk:%u, old bit:%d",
1613 blkaddr, exist);
Jaegeuk Kim05796762014-09-02 16:05:00 -07001614 f2fs_bug_on(sbi, 1);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001615 }
Yunlong Song5ccdd092017-08-02 21:20:13 +08001616#endif
1617 if (unlikely(exist)) {
1618 f2fs_msg(sbi->sb, KERN_ERR,
1619 "Bitmap was wrongly set, blk:%u", blkaddr);
1620 f2fs_bug_on(sbi, 1);
Yunlong Song5fd827b2017-08-02 22:16:54 +08001621 se->valid_blocks--;
1622 del = 0;
Yunlong Song5ccdd092017-08-02 21:20:13 +08001623 }
1624
Jaegeuk Kim3e025742016-08-02 10:56:40 -07001625 if (f2fs_discard_en(sbi) &&
1626 !f2fs_test_and_set_bit(offset, se->discard_map))
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001627 sbi->discard_blks--;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001628
1629 /* don't overwrite by SSR to keep node chain */
1630 if (se->type == CURSEG_WARM_NODE) {
1631 if (!f2fs_test_and_set_bit(offset, se->ckpt_valid_map))
1632 se->ckpt_valid_blocks++;
1633 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001634 } else {
Yunlong Song5ccdd092017-08-02 21:20:13 +08001635 exist = f2fs_test_and_clear_bit(offset, se->cur_valid_map);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001636#ifdef CONFIG_F2FS_CHECK_FS
Yunlong Song5ccdd092017-08-02 21:20:13 +08001637 mir_exist = f2fs_test_and_clear_bit(offset,
1638 se->cur_valid_map_mir);
1639 if (unlikely(exist != mir_exist)) {
1640 f2fs_msg(sbi->sb, KERN_ERR, "Inconsistent error "
1641 "when clearing bitmap, blk:%u, old bit:%d",
1642 blkaddr, exist);
Jaegeuk Kim05796762014-09-02 16:05:00 -07001643 f2fs_bug_on(sbi, 1);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001644 }
Yunlong Song5ccdd092017-08-02 21:20:13 +08001645#endif
1646 if (unlikely(!exist)) {
1647 f2fs_msg(sbi->sb, KERN_ERR,
1648 "Bitmap was wrongly cleared, blk:%u", blkaddr);
1649 f2fs_bug_on(sbi, 1);
Yunlong Song5fd827b2017-08-02 22:16:54 +08001650 se->valid_blocks++;
1651 del = 0;
Yunlong Song5ccdd092017-08-02 21:20:13 +08001652 }
1653
Jaegeuk Kim3e025742016-08-02 10:56:40 -07001654 if (f2fs_discard_en(sbi) &&
1655 f2fs_test_and_clear_bit(offset, se->discard_map))
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001656 sbi->discard_blks++;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001657 }
1658 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
1659 se->ckpt_valid_blocks += del;
1660
1661 __mark_sit_entry_dirty(sbi, segno);
1662
1663 /* update total number of valid blocks to be written in ckpt area */
1664 SIT_I(sbi)->written_valid_blocks += del;
1665
1666 if (sbi->segs_per_sec > 1)
1667 get_sec_entry(sbi, segno)->valid_blocks += del;
1668}
1669
Jaegeuk Kim5e443812014-01-28 12:22:14 +09001670void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001671{
Jaegeuk Kim5e443812014-01-28 12:22:14 +09001672 update_sit_entry(sbi, new, 1);
1673 if (GET_SEGNO(sbi, old) != NULL_SEGNO)
1674 update_sit_entry(sbi, old, -1);
1675
1676 locate_dirty_segment(sbi, GET_SEGNO(sbi, old));
1677 locate_dirty_segment(sbi, GET_SEGNO(sbi, new));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001678}
1679
1680void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
1681{
1682 unsigned int segno = GET_SEGNO(sbi, addr);
1683 struct sit_info *sit_i = SIT_I(sbi);
1684
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001685 f2fs_bug_on(sbi, addr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001686 if (addr == NEW_ADDR)
1687 return;
1688
1689 /* add it into sit main buffer */
1690 mutex_lock(&sit_i->sentry_lock);
1691
1692 update_sit_entry(sbi, addr, -1);
1693
1694 /* add it into dirty seglist */
1695 locate_dirty_segment(sbi, segno);
1696
1697 mutex_unlock(&sit_i->sentry_lock);
1698}
1699
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001700bool is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr)
1701{
1702 struct sit_info *sit_i = SIT_I(sbi);
1703 unsigned int segno, offset;
1704 struct seg_entry *se;
1705 bool is_cp = false;
1706
1707 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR)
1708 return true;
1709
1710 mutex_lock(&sit_i->sentry_lock);
1711
1712 segno = GET_SEGNO(sbi, blkaddr);
1713 se = get_seg_entry(sbi, segno);
1714 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
1715
1716 if (f2fs_test_bit(offset, se->ckpt_valid_map))
1717 is_cp = true;
1718
1719 mutex_unlock(&sit_i->sentry_lock);
1720
1721 return is_cp;
1722}
1723
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001724/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001725 * This function should be resided under the curseg_mutex lock
1726 */
1727static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
Haicheng Lie79efe32013-06-13 16:59:27 +08001728 struct f2fs_summary *sum)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001729{
1730 struct curseg_info *curseg = CURSEG_I(sbi, type);
1731 void *addr = curseg->sum_blk;
Haicheng Lie79efe32013-06-13 16:59:27 +08001732 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001733 memcpy(addr, sum, sizeof(struct f2fs_summary));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001734}
1735
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001736/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001737 * Calculate the number of current summary pages for writing
1738 */
Chao Yu3fa06d72014-12-09 14:21:46 +08001739int npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001740{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001741 int valid_sum_count = 0;
Fan Li9a479382013-10-29 16:21:47 +08001742 int i, sum_in_page;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001743
1744 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1745 if (sbi->ckpt->alloc_type[i] == SSR)
1746 valid_sum_count += sbi->blocks_per_seg;
Chao Yu3fa06d72014-12-09 14:21:46 +08001747 else {
1748 if (for_ra)
1749 valid_sum_count += le16_to_cpu(
1750 F2FS_CKPT(sbi)->cur_data_blkoff[i]);
1751 else
1752 valid_sum_count += curseg_blkoff(sbi, i);
1753 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001754 }
1755
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001756 sum_in_page = (PAGE_SIZE - 2 * SUM_JOURNAL_SIZE -
Fan Li9a479382013-10-29 16:21:47 +08001757 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
1758 if (valid_sum_count <= sum_in_page)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001759 return 1;
Fan Li9a479382013-10-29 16:21:47 +08001760 else if ((valid_sum_count - sum_in_page) <=
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001761 (PAGE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001762 return 2;
1763 return 3;
1764}
1765
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001766/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001767 * Caller should put this summary page
1768 */
1769struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
1770{
1771 return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
1772}
1773
Chao Yu381722d2015-05-19 17:40:04 +08001774void update_meta_page(struct f2fs_sb_info *sbi, void *src, block_t blk_addr)
1775{
1776 struct page *page = grab_meta_page(sbi, blk_addr);
1777 void *dst = page_address(page);
1778
1779 if (src)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001780 memcpy(dst, src, PAGE_SIZE);
Chao Yu381722d2015-05-19 17:40:04 +08001781 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001782 memset(dst, 0, PAGE_SIZE);
Chao Yu381722d2015-05-19 17:40:04 +08001783 set_page_dirty(page);
1784 f2fs_put_page(page, 1);
1785}
1786
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001787static void write_sum_page(struct f2fs_sb_info *sbi,
1788 struct f2fs_summary_block *sum_blk, block_t blk_addr)
1789{
Chao Yu381722d2015-05-19 17:40:04 +08001790 update_meta_page(sbi, (void *)sum_blk, blk_addr);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001791}
1792
Chao Yub7ad7512016-02-19 18:08:46 +08001793static void write_current_sum_page(struct f2fs_sb_info *sbi,
1794 int type, block_t blk_addr)
1795{
1796 struct curseg_info *curseg = CURSEG_I(sbi, type);
1797 struct page *page = grab_meta_page(sbi, blk_addr);
1798 struct f2fs_summary_block *src = curseg->sum_blk;
1799 struct f2fs_summary_block *dst;
1800
1801 dst = (struct f2fs_summary_block *)page_address(page);
1802
1803 mutex_lock(&curseg->curseg_mutex);
1804
1805 down_read(&curseg->journal_rwsem);
1806 memcpy(&dst->journal, curseg->journal, SUM_JOURNAL_SIZE);
1807 up_read(&curseg->journal_rwsem);
1808
1809 memcpy(dst->entries, src->entries, SUM_ENTRY_SIZE);
1810 memcpy(&dst->footer, &src->footer, SUM_FOOTER_SIZE);
1811
1812 mutex_unlock(&curseg->curseg_mutex);
1813
1814 set_page_dirty(page);
1815 f2fs_put_page(page, 1);
1816}
1817
Jaegeuk Kim60374682013-03-31 13:58:51 +09001818static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
1819{
1820 struct curseg_info *curseg = CURSEG_I(sbi, type);
Haicheng Li81fb5e82013-05-14 18:20:28 +08001821 unsigned int segno = curseg->segno + 1;
Jaegeuk Kim60374682013-03-31 13:58:51 +09001822 struct free_segmap_info *free_i = FREE_I(sbi);
1823
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001824 if (segno < MAIN_SEGS(sbi) && segno % sbi->segs_per_sec)
Haicheng Li81fb5e82013-05-14 18:20:28 +08001825 return !test_bit(segno, free_i->free_segmap);
Jaegeuk Kim60374682013-03-31 13:58:51 +09001826 return 0;
1827}
1828
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001829/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001830 * Find a new segment from the free segments bitmap to right order
1831 * This function should be returned with success, otherwise BUG
1832 */
1833static void get_new_segment(struct f2fs_sb_info *sbi,
1834 unsigned int *newseg, bool new_sec, int dir)
1835{
1836 struct free_segmap_info *free_i = FREE_I(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001837 unsigned int segno, secno, zoneno;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001838 unsigned int total_zones = MAIN_SECS(sbi) / sbi->secs_per_zone;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001839 unsigned int hint = GET_SEC_FROM_SEG(sbi, *newseg);
1840 unsigned int old_zoneno = GET_ZONE_FROM_SEG(sbi, *newseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001841 unsigned int left_start = hint;
1842 bool init = true;
1843 int go_left = 0;
1844 int i;
1845
Chao Yu1a118cc2015-02-11 18:20:38 +08001846 spin_lock(&free_i->segmap_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001847
1848 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
1849 segno = find_next_zero_bit(free_i->free_segmap,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001850 GET_SEG_FROM_SEC(sbi, hint + 1), *newseg + 1);
1851 if (segno < GET_SEG_FROM_SEC(sbi, hint + 1))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001852 goto got_it;
1853 }
1854find_other_zone:
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001855 secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint);
1856 if (secno >= MAIN_SECS(sbi)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001857 if (dir == ALLOC_RIGHT) {
1858 secno = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001859 MAIN_SECS(sbi), 0);
1860 f2fs_bug_on(sbi, secno >= MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001861 } else {
1862 go_left = 1;
1863 left_start = hint - 1;
1864 }
1865 }
1866 if (go_left == 0)
1867 goto skip_left;
1868
1869 while (test_bit(left_start, free_i->free_secmap)) {
1870 if (left_start > 0) {
1871 left_start--;
1872 continue;
1873 }
1874 left_start = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07001875 MAIN_SECS(sbi), 0);
1876 f2fs_bug_on(sbi, left_start >= MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001877 break;
1878 }
1879 secno = left_start;
1880skip_left:
1881 hint = secno;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001882 segno = GET_SEG_FROM_SEC(sbi, secno);
1883 zoneno = GET_ZONE_FROM_SEC(sbi, secno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001884
1885 /* give up on finding another zone */
1886 if (!init)
1887 goto got_it;
1888 if (sbi->secs_per_zone == 1)
1889 goto got_it;
1890 if (zoneno == old_zoneno)
1891 goto got_it;
1892 if (dir == ALLOC_LEFT) {
1893 if (!go_left && zoneno + 1 >= total_zones)
1894 goto got_it;
1895 if (go_left && zoneno == 0)
1896 goto got_it;
1897 }
1898 for (i = 0; i < NR_CURSEG_TYPE; i++)
1899 if (CURSEG_I(sbi, i)->zone == zoneno)
1900 break;
1901
1902 if (i < NR_CURSEG_TYPE) {
1903 /* zone is in user, try another */
1904 if (go_left)
1905 hint = zoneno * sbi->secs_per_zone - 1;
1906 else if (zoneno + 1 >= total_zones)
1907 hint = 0;
1908 else
1909 hint = (zoneno + 1) * sbi->secs_per_zone;
1910 init = false;
1911 goto find_other_zone;
1912 }
1913got_it:
1914 /* set it as dirty segment in free segmap */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001915 f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001916 __set_inuse(sbi, segno);
1917 *newseg = segno;
Chao Yu1a118cc2015-02-11 18:20:38 +08001918 spin_unlock(&free_i->segmap_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001919}
1920
1921static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
1922{
1923 struct curseg_info *curseg = CURSEG_I(sbi, type);
1924 struct summary_footer *sum_footer;
1925
1926 curseg->segno = curseg->next_segno;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001927 curseg->zone = GET_ZONE_FROM_SEG(sbi, curseg->segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001928 curseg->next_blkoff = 0;
1929 curseg->next_segno = NULL_SEGNO;
1930
1931 sum_footer = &(curseg->sum_blk->footer);
1932 memset(sum_footer, 0, sizeof(struct summary_footer));
1933 if (IS_DATASEG(type))
1934 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
1935 if (IS_NODESEG(type))
1936 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
1937 __set_sit_entry_type(sbi, type, curseg->segno, modified);
1938}
1939
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001940static unsigned int __get_next_segno(struct f2fs_sb_info *sbi, int type)
1941{
1942 /* if segs_per_sec is large than 1, we need to keep original policy. */
1943 if (sbi->segs_per_sec != 1)
1944 return CURSEG_I(sbi, type)->segno;
1945
1946 if (type == CURSEG_HOT_DATA || IS_NODESEG(type))
1947 return 0;
1948
1949 if (SIT_I(sbi)->last_victim[ALLOC_NEXT])
1950 return SIT_I(sbi)->last_victim[ALLOC_NEXT];
1951 return CURSEG_I(sbi, type)->segno;
1952}
1953
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001954/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001955 * Allocate a current working segment.
1956 * This function always allocates a free segment in LFS manner.
1957 */
1958static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
1959{
1960 struct curseg_info *curseg = CURSEG_I(sbi, type);
1961 unsigned int segno = curseg->segno;
1962 int dir = ALLOC_LEFT;
1963
1964 write_sum_page(sbi, curseg->sum_blk,
Haicheng Li81fb5e82013-05-14 18:20:28 +08001965 GET_SUM_BLOCK(sbi, segno));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001966 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
1967 dir = ALLOC_RIGHT;
1968
1969 if (test_opt(sbi, NOHEAP))
1970 dir = ALLOC_RIGHT;
1971
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001972 segno = __get_next_segno(sbi, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001973 get_new_segment(sbi, &segno, new_sec, dir);
1974 curseg->next_segno = segno;
1975 reset_curseg(sbi, type, 1);
1976 curseg->alloc_type = LFS;
1977}
1978
1979static void __next_free_blkoff(struct f2fs_sb_info *sbi,
1980 struct curseg_info *seg, block_t start)
1981{
1982 struct seg_entry *se = get_seg_entry(sbi, seg->segno);
Changman Leee81c93c2013-11-15 13:21:16 +09001983 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
Jaegeuk Kim60a3b782015-02-10 16:44:29 -08001984 unsigned long *target_map = SIT_I(sbi)->tmp_map;
Changman Leee81c93c2013-11-15 13:21:16 +09001985 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
1986 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
1987 int i, pos;
1988
1989 for (i = 0; i < entries; i++)
1990 target_map[i] = ckpt_map[i] | cur_map[i];
1991
1992 pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
1993
1994 seg->next_blkoff = pos;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001995}
1996
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001997/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09001998 * If a segment is written by LFS manner, next block offset is just obtained
1999 * by increasing the current block offset. However, if a segment is written by
2000 * SSR manner, next block offset obtained by calling __next_free_blkoff
2001 */
2002static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
2003 struct curseg_info *seg)
2004{
2005 if (seg->alloc_type == SSR)
2006 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
2007 else
2008 seg->next_blkoff++;
2009}
2010
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002011/*
arter97e1c42042014-08-06 23:22:50 +09002012 * This function always allocates a used segment(from dirty seglist) by SSR
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002013 * manner, so it should recover the existing segment information of valid blocks
2014 */
2015static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
2016{
2017 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2018 struct curseg_info *curseg = CURSEG_I(sbi, type);
2019 unsigned int new_segno = curseg->next_segno;
2020 struct f2fs_summary_block *sum_node;
2021 struct page *sum_page;
2022
2023 write_sum_page(sbi, curseg->sum_blk,
2024 GET_SUM_BLOCK(sbi, curseg->segno));
2025 __set_test_and_inuse(sbi, new_segno);
2026
2027 mutex_lock(&dirty_i->seglist_lock);
2028 __remove_dirty_segment(sbi, new_segno, PRE);
2029 __remove_dirty_segment(sbi, new_segno, DIRTY);
2030 mutex_unlock(&dirty_i->seglist_lock);
2031
2032 reset_curseg(sbi, type, 1);
2033 curseg->alloc_type = SSR;
2034 __next_free_blkoff(sbi, curseg, 0);
2035
2036 if (reuse) {
2037 sum_page = get_sum_page(sbi, new_segno);
2038 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
2039 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
2040 f2fs_put_page(sum_page, 1);
2041 }
2042}
2043
Jaegeuk Kim43727522013-02-04 15:11:17 +09002044static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
2045{
2046 struct curseg_info *curseg = CURSEG_I(sbi, type);
2047 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002048 unsigned segno = NULL_SEGNO;
2049 int i, cnt;
2050 bool reversed = false;
Jaegeuk Kim43727522013-02-04 15:11:17 +09002051
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002052 /* need_SSR() already forces to do this */
2053 if (v_ops->get_victim(sbi, &segno, BG_GC, type, SSR)) {
2054 curseg->next_segno = segno;
2055 return 1;
2056 }
Jaegeuk Kim43727522013-02-04 15:11:17 +09002057
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002058 /* For node segments, let's do SSR more intensively */
2059 if (IS_NODESEG(type)) {
2060 if (type >= CURSEG_WARM_NODE) {
2061 reversed = true;
2062 i = CURSEG_COLD_NODE;
2063 } else {
2064 i = CURSEG_HOT_NODE;
2065 }
2066 cnt = NR_CURSEG_NODE_TYPE;
2067 } else {
2068 if (type >= CURSEG_WARM_DATA) {
2069 reversed = true;
2070 i = CURSEG_COLD_DATA;
2071 } else {
2072 i = CURSEG_HOT_DATA;
2073 }
2074 cnt = NR_CURSEG_DATA_TYPE;
2075 }
2076
2077 for (; cnt-- > 0; reversed ? i-- : i++) {
2078 if (i == type)
2079 continue;
2080 if (v_ops->get_victim(sbi, &segno, BG_GC, i, SSR)) {
2081 curseg->next_segno = segno;
Jaegeuk Kim43727522013-02-04 15:11:17 +09002082 return 1;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002083 }
2084 }
Jaegeuk Kim43727522013-02-04 15:11:17 +09002085 return 0;
2086}
2087
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002088/*
2089 * flush out current segment and replace it with new segment
2090 * This function should be returned with success, otherwise BUG
2091 */
2092static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
2093 int type, bool force)
2094{
2095 struct curseg_info *curseg = CURSEG_I(sbi, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002096
Gu Zheng7b405272013-08-19 09:41:15 +08002097 if (force)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002098 new_curseg(sbi, type, true);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002099 else if (!is_set_ckpt_flags(sbi, CP_CRC_RECOVERY_FLAG) &&
2100 type == CURSEG_WARM_NODE)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002101 new_curseg(sbi, type, false);
Jaegeuk Kim60374682013-03-31 13:58:51 +09002102 else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
2103 new_curseg(sbi, type, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002104 else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
2105 change_curseg(sbi, type, true);
2106 else
2107 new_curseg(sbi, type, false);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +09002108
2109 stat_inc_seg_type(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002110}
2111
2112void allocate_new_segments(struct f2fs_sb_info *sbi)
2113{
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002114 struct curseg_info *curseg;
2115 unsigned int old_segno;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002116 int i;
2117
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002118 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
2119 curseg = CURSEG_I(sbi, i);
2120 old_segno = curseg->segno;
2121 SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
2122 locate_dirty_segment(sbi, old_segno);
2123 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002124}
2125
2126static const struct segment_allocation default_salloc_ops = {
2127 .allocate_segment = allocate_segment_by_default,
2128};
2129
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002130bool exist_trim_candidates(struct f2fs_sb_info *sbi, struct cp_control *cpc)
2131{
2132 __u64 trim_start = cpc->trim_start;
2133 bool has_candidate = false;
2134
2135 mutex_lock(&SIT_I(sbi)->sentry_lock);
2136 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++) {
2137 if (add_discard_addrs(sbi, cpc, true)) {
2138 has_candidate = true;
2139 break;
2140 }
2141 }
2142 mutex_unlock(&SIT_I(sbi)->sentry_lock);
2143
2144 cpc->trim_start = trim_start;
2145 return has_candidate;
2146}
2147
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002148int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range)
2149{
Jaegeuk Kimf7ef9b82015-02-09 12:02:44 -08002150 __u64 start = F2FS_BYTES_TO_BLK(range->start);
2151 __u64 end = start + F2FS_BYTES_TO_BLK(range->len) - 1;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002152 unsigned int start_segno, end_segno;
2153 struct cp_control cpc;
Chao Yuc34f42e2015-12-23 17:50:30 +08002154 int err = 0;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002155
Jaegeuk Kim836b5a62015-04-30 22:50:06 -07002156 if (start >= MAX_BLKADDR(sbi) || range->len < sbi->blocksize)
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002157 return -EINVAL;
2158
Jan Kara9bd27ae2014-10-21 14:07:33 +02002159 cpc.trimmed = 0;
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002160 if (end <= MAIN_BLKADDR(sbi))
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002161 goto out;
2162
Yunlei Heed214a12016-09-01 10:14:39 +08002163 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) {
2164 f2fs_msg(sbi->sb, KERN_WARNING,
2165 "Found FS corruption, run fsck to fix.");
2166 goto out;
2167 }
2168
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002169 /* start/end segment number in main_area */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002170 start_segno = (start <= MAIN_BLKADDR(sbi)) ? 0 : GET_SEGNO(sbi, start);
2171 end_segno = (end >= MAX_BLKADDR(sbi)) ? MAIN_SEGS(sbi) - 1 :
2172 GET_SEGNO(sbi, end);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002173 cpc.reason = CP_DISCARD;
Jaegeuk Kim836b5a62015-04-30 22:50:06 -07002174 cpc.trim_minlen = max_t(__u64, 1, F2FS_BYTES_TO_BLK(range->minlen));
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002175
2176 /* do checkpoint to issue discard commands safely */
Jaegeuk Kimbba681c2015-01-26 17:41:23 -08002177 for (; start_segno <= end_segno; start_segno = cpc.trim_end + 1) {
2178 cpc.trim_start = start_segno;
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07002179
2180 if (sbi->discard_blks == 0)
2181 break;
2182 else if (sbi->discard_blks < BATCHED_TRIM_BLOCKS(sbi))
2183 cpc.trim_end = end_segno;
2184 else
2185 cpc.trim_end = min_t(unsigned int,
2186 rounddown(start_segno +
Jaegeuk Kimbba681c2015-01-26 17:41:23 -08002187 BATCHED_TRIM_SEGMENTS(sbi),
2188 sbi->segs_per_sec) - 1, end_segno);
2189
2190 mutex_lock(&sbi->gc_mutex);
Chao Yuc34f42e2015-12-23 17:50:30 +08002191 err = write_checkpoint(sbi, &cpc);
Jaegeuk Kimbba681c2015-01-26 17:41:23 -08002192 mutex_unlock(&sbi->gc_mutex);
Chao Yue9328352016-08-21 23:21:29 +08002193 if (err)
2194 break;
Chao Yu74fa5f32016-08-21 23:21:30 +08002195
2196 schedule();
Jaegeuk Kimbba681c2015-01-26 17:41:23 -08002197 }
Chao Yuefe24da2017-08-07 23:09:56 +08002198 /* It's time to issue all the filed discards */
2199 mark_discard_range_all(sbi);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002200out:
Jaegeuk Kimf7ef9b82015-02-09 12:02:44 -08002201 range->len = F2FS_BLK_TO_BYTES(cpc.trimmed);
Chao Yuc34f42e2015-12-23 17:50:30 +08002202 return err;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002203}
2204
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002205static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
2206{
2207 struct curseg_info *curseg = CURSEG_I(sbi, type);
2208 if (curseg->next_blkoff < sbi->blocks_per_seg)
2209 return true;
2210 return false;
2211}
2212
Jaegeuk Kim72cc4582017-05-10 14:19:54 -07002213static int __get_segment_type_2(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002214{
Jaegeuk Kim72cc4582017-05-10 14:19:54 -07002215 if (fio->type == DATA)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002216 return CURSEG_HOT_DATA;
2217 else
2218 return CURSEG_HOT_NODE;
2219}
2220
Jaegeuk Kim72cc4582017-05-10 14:19:54 -07002221static int __get_segment_type_4(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002222{
Jaegeuk Kim72cc4582017-05-10 14:19:54 -07002223 if (fio->type == DATA) {
2224 struct inode *inode = fio->page->mapping->host;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002225
2226 if (S_ISDIR(inode->i_mode))
2227 return CURSEG_HOT_DATA;
2228 else
2229 return CURSEG_COLD_DATA;
2230 } else {
Jaegeuk Kim72cc4582017-05-10 14:19:54 -07002231 if (IS_DNODE(fio->page) && is_cold_node(fio->page))
Jaegeuk Kima344b9f2014-11-05 20:05:53 -08002232 return CURSEG_WARM_NODE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002233 else
2234 return CURSEG_COLD_NODE;
2235 }
2236}
2237
Jaegeuk Kim72cc4582017-05-10 14:19:54 -07002238static int __get_segment_type_6(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002239{
Jaegeuk Kim72cc4582017-05-10 14:19:54 -07002240 if (fio->type == DATA) {
2241 struct inode *inode = fio->page->mapping->host;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002242
Jaegeuk Kim72cc4582017-05-10 14:19:54 -07002243 if (is_cold_data(fio->page) || file_is_cold(inode))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002244 return CURSEG_COLD_DATA;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002245 if (is_inode_flag_set(inode, FI_HOT_DATA))
2246 return CURSEG_HOT_DATA;
2247 return CURSEG_WARM_DATA;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002248 } else {
Jaegeuk Kim72cc4582017-05-10 14:19:54 -07002249 if (IS_DNODE(fio->page))
2250 return is_cold_node(fio->page) ? CURSEG_WARM_NODE :
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002251 CURSEG_HOT_NODE;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002252 return CURSEG_COLD_NODE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002253 }
2254}
2255
Jaegeuk Kim72cc4582017-05-10 14:19:54 -07002256static int __get_segment_type(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002257{
Jaegeuk Kimc4127262017-05-10 11:18:25 -07002258 int type = 0;
2259
Jaegeuk Kim72cc4582017-05-10 14:19:54 -07002260 switch (fio->sbi->active_logs) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002261 case 2:
Jaegeuk Kimc4127262017-05-10 11:18:25 -07002262 type = __get_segment_type_2(fio);
2263 break;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002264 case 4:
Jaegeuk Kimc4127262017-05-10 11:18:25 -07002265 type = __get_segment_type_4(fio);
2266 break;
2267 case 6:
2268 type = __get_segment_type_6(fio);
2269 break;
2270 default:
2271 f2fs_bug_on(fio->sbi, true);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002272 }
Jaegeuk Kim72cc4582017-05-10 14:19:54 -07002273
Jaegeuk Kimc4127262017-05-10 11:18:25 -07002274 if (IS_HOT(type))
2275 fio->temp = HOT;
2276 else if (IS_WARM(type))
2277 fio->temp = WARM;
2278 else
2279 fio->temp = COLD;
2280 return type;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002281}
2282
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09002283void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
2284 block_t old_blkaddr, block_t *new_blkaddr,
Chao Yuc52dc0f2017-05-19 23:37:01 +08002285 struct f2fs_summary *sum, int type,
2286 struct f2fs_io_info *fio, bool add_list)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002287{
2288 struct sit_info *sit_i = SIT_I(sbi);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002289 struct curseg_info *curseg = CURSEG_I(sbi, type);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002290
2291 mutex_lock(&curseg->curseg_mutex);
Jaegeuk Kim21cb1d92015-03-11 13:42:48 -04002292 mutex_lock(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002293
2294 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002295
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002296 f2fs_wait_discard_bio(sbi, *new_blkaddr);
2297
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002298 /*
2299 * __add_sum_entry should be resided under the curseg_mutex
2300 * because, this function updates a summary entry in the
2301 * current summary block.
2302 */
Haicheng Lie79efe32013-06-13 16:59:27 +08002303 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002304
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002305 __refresh_next_blkoff(sbi, curseg);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +09002306
2307 stat_inc_block_count(sbi, curseg);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002308
Jaegeuk Kim5e443812014-01-28 12:22:14 +09002309 if (!__has_curseg_space(sbi, type))
2310 sit_i->s_ops->allocate_segment(sbi, type, false);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002311 /*
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002312 * SIT information should be updated after segment allocation,
2313 * since we need to keep dirty segments precisely under SSR.
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002314 */
2315 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
Jaegeuk Kim5e443812014-01-28 12:22:14 +09002316
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002317 mutex_unlock(&sit_i->sentry_lock);
2318
Chao Yu43101252017-07-31 20:19:09 +08002319 if (page && IS_NODESEG(type)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002320 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
2321
Chao Yu43101252017-07-31 20:19:09 +08002322 f2fs_inode_chksum_set(sbi, page);
2323 }
2324
Chao Yuc52dc0f2017-05-19 23:37:01 +08002325 if (add_list) {
2326 struct f2fs_bio_info *io;
2327
2328 INIT_LIST_HEAD(&fio->list);
2329 fio->in_list = true;
2330 io = sbi->write_io[fio->type] + fio->temp;
2331 spin_lock(&io->io_lock);
2332 list_add_tail(&fio->list, &io->io_list);
2333 spin_unlock(&io->io_lock);
2334 }
2335
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09002336 mutex_unlock(&curseg->curseg_mutex);
2337}
2338
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002339static void do_write_page(struct f2fs_summary *sum, struct f2fs_io_info *fio)
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09002340{
Jaegeuk Kim72cc4582017-05-10 14:19:54 -07002341 int type = __get_segment_type(fio);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002342 int err;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09002343
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002344reallocate:
Chao Yu7a9d7542016-02-22 18:36:38 +08002345 allocate_data_block(fio->sbi, fio->page, fio->old_blkaddr,
Chao Yuc52dc0f2017-05-19 23:37:01 +08002346 &fio->new_blkaddr, sum, type, fio, true);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09002347
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002348 /* writeout dirty page into bdev */
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -07002349 err = f2fs_submit_page_write(fio);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002350 if (err == -EAGAIN) {
2351 fio->old_blkaddr = fio->new_blkaddr;
2352 goto reallocate;
2353 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002354}
2355
Chao Yuc0fe4882017-08-02 23:21:48 +08002356void write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
2357 enum iostat_type io_type)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002358{
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002359 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002360 .sbi = sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002361 .type = META,
Mike Christie04d328d2016-06-05 14:31:55 -05002362 .op = REQ_OP_WRITE,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002363 .op_flags = REQ_SYNC | REQ_META | REQ_PRIO,
Chao Yu7a9d7542016-02-22 18:36:38 +08002364 .old_blkaddr = page->index,
2365 .new_blkaddr = page->index,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002366 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07002367 .encrypted_page = NULL,
Chao Yuc52dc0f2017-05-19 23:37:01 +08002368 .in_list = false,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002369 };
2370
Chao Yu2b947002015-10-12 17:04:21 +08002371 if (unlikely(page->index >= MAIN_BLKADDR(sbi)))
Mike Christie04d328d2016-06-05 14:31:55 -05002372 fio.op_flags &= ~REQ_META;
Chao Yu2b947002015-10-12 17:04:21 +08002373
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002374 set_page_writeback(page);
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -07002375 f2fs_submit_page_write(&fio);
Chao Yuc0fe4882017-08-02 23:21:48 +08002376
2377 f2fs_update_iostat(sbi, io_type, F2FS_BLKSIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002378}
2379
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002380void write_node_page(unsigned int nid, struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002381{
2382 struct f2fs_summary sum;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002383
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002384 set_summary(&sum, nid, 0, 0);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002385 do_write_page(&sum, fio);
Chao Yuc0fe4882017-08-02 23:21:48 +08002386
2387 f2fs_update_iostat(fio->sbi, fio->io_type, F2FS_BLKSIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002388}
2389
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002390void write_data_page(struct dnode_of_data *dn, struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002391{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002392 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002393 struct f2fs_summary sum;
2394 struct node_info ni;
2395
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002396 f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002397 get_node_info(sbi, dn->nid, &ni);
2398 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002399 do_write_page(&sum, fio);
Chao Yuf28b3432016-02-24 17:16:47 +08002400 f2fs_update_data_blkaddr(dn, fio->new_blkaddr);
Chao Yuc0fe4882017-08-02 23:21:48 +08002401
2402 f2fs_update_iostat(sbi, fio->io_type, F2FS_BLKSIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002403}
2404
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002405int rewrite_data_page(struct f2fs_io_info *fio)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002406{
Chao Yuc0fe4882017-08-02 23:21:48 +08002407 int err;
2408
Chao Yu7a9d7542016-02-22 18:36:38 +08002409 fio->new_blkaddr = fio->old_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002410 stat_inc_inplace_blocks(fio->sbi);
Chao Yuc0fe4882017-08-02 23:21:48 +08002411
2412 err = f2fs_submit_page_bio(fio);
2413
2414 f2fs_update_iostat(fio->sbi, fio->io_type, F2FS_BLKSIZE);
2415
2416 return err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002417}
2418
Chao Yu4356e482016-02-23 17:52:43 +08002419void __f2fs_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
Chao Yu19f106b2015-05-06 13:08:06 +08002420 block_t old_blkaddr, block_t new_blkaddr,
Chao Yu28bc1062016-02-06 14:40:34 +08002421 bool recover_curseg, bool recover_newaddr)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002422{
2423 struct sit_info *sit_i = SIT_I(sbi);
2424 struct curseg_info *curseg;
2425 unsigned int segno, old_cursegno;
2426 struct seg_entry *se;
2427 int type;
Chao Yu19f106b2015-05-06 13:08:06 +08002428 unsigned short old_blkoff;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002429
2430 segno = GET_SEGNO(sbi, new_blkaddr);
2431 se = get_seg_entry(sbi, segno);
2432 type = se->type;
2433
Chao Yu19f106b2015-05-06 13:08:06 +08002434 if (!recover_curseg) {
2435 /* for recovery flow */
2436 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
2437 if (old_blkaddr == NULL_ADDR)
2438 type = CURSEG_COLD_DATA;
2439 else
2440 type = CURSEG_WARM_DATA;
2441 }
2442 } else {
2443 if (!IS_CURSEG(sbi, segno))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002444 type = CURSEG_WARM_DATA;
2445 }
Chao Yu19f106b2015-05-06 13:08:06 +08002446
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002447 curseg = CURSEG_I(sbi, type);
2448
2449 mutex_lock(&curseg->curseg_mutex);
2450 mutex_lock(&sit_i->sentry_lock);
2451
2452 old_cursegno = curseg->segno;
Chao Yu19f106b2015-05-06 13:08:06 +08002453 old_blkoff = curseg->next_blkoff;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002454
2455 /* change the current segment */
2456 if (segno != curseg->segno) {
2457 curseg->next_segno = segno;
2458 change_curseg(sbi, type, true);
2459 }
2460
Jaegeuk Kim491c0852014-02-04 13:01:10 +09002461 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
Haicheng Lie79efe32013-06-13 16:59:27 +08002462 __add_sum_entry(sbi, type, sum);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002463
Chao Yu28bc1062016-02-06 14:40:34 +08002464 if (!recover_curseg || recover_newaddr)
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07002465 update_sit_entry(sbi, new_blkaddr, 1);
2466 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
2467 update_sit_entry(sbi, old_blkaddr, -1);
2468
2469 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
2470 locate_dirty_segment(sbi, GET_SEGNO(sbi, new_blkaddr));
2471
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002472 locate_dirty_segment(sbi, old_cursegno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002473
Chao Yu19f106b2015-05-06 13:08:06 +08002474 if (recover_curseg) {
2475 if (old_cursegno != curseg->segno) {
2476 curseg->next_segno = old_cursegno;
2477 change_curseg(sbi, type, true);
2478 }
2479 curseg->next_blkoff = old_blkoff;
2480 }
2481
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002482 mutex_unlock(&sit_i->sentry_lock);
2483 mutex_unlock(&curseg->curseg_mutex);
2484}
2485
Chao Yu528e3452015-05-28 19:15:35 +08002486void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
2487 block_t old_addr, block_t new_addr,
Chao Yu28bc1062016-02-06 14:40:34 +08002488 unsigned char version, bool recover_curseg,
2489 bool recover_newaddr)
Chao Yu528e3452015-05-28 19:15:35 +08002490{
2491 struct f2fs_summary sum;
2492
2493 set_summary(&sum, dn->nid, dn->ofs_in_node, version);
2494
Chao Yu28bc1062016-02-06 14:40:34 +08002495 __f2fs_replace_block(sbi, &sum, old_addr, new_addr,
2496 recover_curseg, recover_newaddr);
Chao Yu528e3452015-05-28 19:15:35 +08002497
Chao Yuf28b3432016-02-24 17:16:47 +08002498 f2fs_update_data_blkaddr(dn, new_addr);
Chao Yu528e3452015-05-28 19:15:35 +08002499}
2500
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09002501void f2fs_wait_on_page_writeback(struct page *page,
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08002502 enum page_type type, bool ordered)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09002503{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09002504 if (PageWriteback(page)) {
Jaegeuk Kim40813632014-09-02 15:31:18 -07002505 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
2506
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -07002507 f2fs_submit_merged_write_cond(sbi, page->mapping->host,
2508 0, page->index, type);
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08002509 if (ordered)
2510 wait_on_page_writeback(page);
2511 else
2512 wait_for_stable_page(page);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09002513 }
2514}
2515
Chao Yu08b39fb2015-10-08 13:27:34 +08002516void f2fs_wait_on_encrypted_page_writeback(struct f2fs_sb_info *sbi,
2517 block_t blkaddr)
2518{
2519 struct page *cpage;
2520
Yunlei He5d4c0af2016-09-18 08:16:56 +08002521 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR)
Chao Yu08b39fb2015-10-08 13:27:34 +08002522 return;
2523
Chao Yu08b39fb2015-10-08 13:27:34 +08002524 cpage = find_lock_page(META_MAPPING(sbi), blkaddr);
2525 if (cpage) {
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08002526 f2fs_wait_on_page_writeback(cpage, DATA, true);
Chao Yu08b39fb2015-10-08 13:27:34 +08002527 f2fs_put_page(cpage, 1);
2528 }
2529}
2530
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002531static int read_compacted_summaries(struct f2fs_sb_info *sbi)
2532{
2533 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
2534 struct curseg_info *seg_i;
2535 unsigned char *kaddr;
2536 struct page *page;
2537 block_t start;
2538 int i, j, offset;
2539
2540 start = start_sum_block(sbi);
2541
2542 page = get_meta_page(sbi, start++);
2543 kaddr = (unsigned char *)page_address(page);
2544
2545 /* Step 1: restore nat cache */
2546 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08002547 memcpy(seg_i->journal, kaddr, SUM_JOURNAL_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002548
2549 /* Step 2: restore sit cache */
2550 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08002551 memcpy(seg_i->journal, kaddr + SUM_JOURNAL_SIZE, SUM_JOURNAL_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002552 offset = 2 * SUM_JOURNAL_SIZE;
2553
2554 /* Step 3: restore summary entries */
2555 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
2556 unsigned short blk_off;
2557 unsigned int segno;
2558
2559 seg_i = CURSEG_I(sbi, i);
2560 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
2561 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
2562 seg_i->next_segno = segno;
2563 reset_curseg(sbi, i, 0);
2564 seg_i->alloc_type = ckpt->alloc_type[i];
2565 seg_i->next_blkoff = blk_off;
2566
2567 if (seg_i->alloc_type == SSR)
2568 blk_off = sbi->blocks_per_seg;
2569
2570 for (j = 0; j < blk_off; j++) {
2571 struct f2fs_summary *s;
2572 s = (struct f2fs_summary *)(kaddr + offset);
2573 seg_i->sum_blk->entries[j] = *s;
2574 offset += SUMMARY_SIZE;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002575 if (offset + SUMMARY_SIZE <= PAGE_SIZE -
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002576 SUM_FOOTER_SIZE)
2577 continue;
2578
2579 f2fs_put_page(page, 1);
2580 page = NULL;
2581
2582 page = get_meta_page(sbi, start++);
2583 kaddr = (unsigned char *)page_address(page);
2584 offset = 0;
2585 }
2586 }
2587 f2fs_put_page(page, 1);
2588 return 0;
2589}
2590
2591static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
2592{
2593 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
2594 struct f2fs_summary_block *sum;
2595 struct curseg_info *curseg;
2596 struct page *new;
2597 unsigned short blk_off;
2598 unsigned int segno = 0;
2599 block_t blk_addr = 0;
2600
2601 /* get segment number and block addr */
2602 if (IS_DATASEG(type)) {
2603 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
2604 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
2605 CURSEG_HOT_DATA]);
Jaegeuk Kim119ee912015-01-29 11:45:33 -08002606 if (__exist_node_summaries(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002607 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
2608 else
2609 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
2610 } else {
2611 segno = le32_to_cpu(ckpt->cur_node_segno[type -
2612 CURSEG_HOT_NODE]);
2613 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
2614 CURSEG_HOT_NODE]);
Jaegeuk Kim119ee912015-01-29 11:45:33 -08002615 if (__exist_node_summaries(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002616 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
2617 type - CURSEG_HOT_NODE);
2618 else
2619 blk_addr = GET_SUM_BLOCK(sbi, segno);
2620 }
2621
2622 new = get_meta_page(sbi, blk_addr);
2623 sum = (struct f2fs_summary_block *)page_address(new);
2624
2625 if (IS_NODESEG(type)) {
Jaegeuk Kim119ee912015-01-29 11:45:33 -08002626 if (__exist_node_summaries(sbi)) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002627 struct f2fs_summary *ns = &sum->entries[0];
2628 int i;
2629 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
2630 ns->version = 0;
2631 ns->ofs_in_node = 0;
2632 }
2633 } else {
Gu Zhengd6537882014-03-07 18:43:36 +08002634 int err;
2635
2636 err = restore_node_summary(sbi, segno, sum);
2637 if (err) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002638 f2fs_put_page(new, 1);
Gu Zhengd6537882014-03-07 18:43:36 +08002639 return err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002640 }
2641 }
2642 }
2643
2644 /* set uncompleted segment to curseg */
2645 curseg = CURSEG_I(sbi, type);
2646 mutex_lock(&curseg->curseg_mutex);
Chao Yub7ad7512016-02-19 18:08:46 +08002647
2648 /* update journal info */
2649 down_write(&curseg->journal_rwsem);
2650 memcpy(curseg->journal, &sum->journal, SUM_JOURNAL_SIZE);
2651 up_write(&curseg->journal_rwsem);
2652
2653 memcpy(curseg->sum_blk->entries, sum->entries, SUM_ENTRY_SIZE);
2654 memcpy(&curseg->sum_blk->footer, &sum->footer, SUM_FOOTER_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002655 curseg->next_segno = segno;
2656 reset_curseg(sbi, type, 0);
2657 curseg->alloc_type = ckpt->alloc_type[type];
2658 curseg->next_blkoff = blk_off;
2659 mutex_unlock(&curseg->curseg_mutex);
2660 f2fs_put_page(new, 1);
2661 return 0;
2662}
2663
2664static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
2665{
Jin Qiana8d61752017-06-01 11:18:30 -07002666 struct f2fs_journal *sit_j = CURSEG_I(sbi, CURSEG_COLD_DATA)->journal;
2667 struct f2fs_journal *nat_j = CURSEG_I(sbi, CURSEG_HOT_DATA)->journal;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002668 int type = CURSEG_HOT_DATA;
Chao Yue4fc5fb2014-03-17 16:36:24 +08002669 int err;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002670
Chao Yuaaec2b12016-09-20 11:04:18 +08002671 if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG)) {
Chao Yu3fa06d72014-12-09 14:21:46 +08002672 int npages = npages_for_summary_flush(sbi, true);
2673
2674 if (npages >= 2)
2675 ra_meta_pages(sbi, start_sum_block(sbi), npages,
Chao Yu26879fb2015-10-12 17:05:59 +08002676 META_CP, true);
Chao Yu3fa06d72014-12-09 14:21:46 +08002677
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002678 /* restore for compacted data summary */
2679 if (read_compacted_summaries(sbi))
2680 return -EINVAL;
2681 type = CURSEG_HOT_NODE;
2682 }
2683
Jaegeuk Kim119ee912015-01-29 11:45:33 -08002684 if (__exist_node_summaries(sbi))
Chao Yu3fa06d72014-12-09 14:21:46 +08002685 ra_meta_pages(sbi, sum_blk_addr(sbi, NR_CURSEG_TYPE, type),
Chao Yu26879fb2015-10-12 17:05:59 +08002686 NR_CURSEG_TYPE - type, META_CP, true);
Chao Yu3fa06d72014-12-09 14:21:46 +08002687
Chao Yue4fc5fb2014-03-17 16:36:24 +08002688 for (; type <= CURSEG_COLD_NODE; type++) {
2689 err = read_normal_summaries(sbi, type);
2690 if (err)
2691 return err;
2692 }
2693
Jin Qiana8d61752017-06-01 11:18:30 -07002694 /* sanity check for summary blocks */
2695 if (nats_in_cursum(nat_j) > NAT_JOURNAL_ENTRIES ||
2696 sits_in_cursum(sit_j) > SIT_JOURNAL_ENTRIES)
2697 return -EINVAL;
2698
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002699 return 0;
2700}
2701
2702static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
2703{
2704 struct page *page;
2705 unsigned char *kaddr;
2706 struct f2fs_summary *summary;
2707 struct curseg_info *seg_i;
2708 int written_size = 0;
2709 int i, j;
2710
2711 page = grab_meta_page(sbi, blkaddr++);
2712 kaddr = (unsigned char *)page_address(page);
2713
2714 /* Step 1: write nat cache */
2715 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08002716 memcpy(kaddr, seg_i->journal, SUM_JOURNAL_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002717 written_size += SUM_JOURNAL_SIZE;
2718
2719 /* Step 2: write sit cache */
2720 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08002721 memcpy(kaddr + written_size, seg_i->journal, SUM_JOURNAL_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002722 written_size += SUM_JOURNAL_SIZE;
2723
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002724 /* Step 3: write summary entries */
2725 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
2726 unsigned short blkoff;
2727 seg_i = CURSEG_I(sbi, i);
2728 if (sbi->ckpt->alloc_type[i] == SSR)
2729 blkoff = sbi->blocks_per_seg;
2730 else
2731 blkoff = curseg_blkoff(sbi, i);
2732
2733 for (j = 0; j < blkoff; j++) {
2734 if (!page) {
2735 page = grab_meta_page(sbi, blkaddr++);
2736 kaddr = (unsigned char *)page_address(page);
2737 written_size = 0;
2738 }
2739 summary = (struct f2fs_summary *)(kaddr + written_size);
2740 *summary = seg_i->sum_blk->entries[j];
2741 written_size += SUMMARY_SIZE;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002742
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002743 if (written_size + SUMMARY_SIZE <= PAGE_SIZE -
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002744 SUM_FOOTER_SIZE)
2745 continue;
2746
Chao Yue8d61a72013-10-24 15:08:28 +08002747 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002748 f2fs_put_page(page, 1);
2749 page = NULL;
2750 }
2751 }
Chao Yue8d61a72013-10-24 15:08:28 +08002752 if (page) {
2753 set_page_dirty(page);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002754 f2fs_put_page(page, 1);
Chao Yue8d61a72013-10-24 15:08:28 +08002755 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002756}
2757
2758static void write_normal_summaries(struct f2fs_sb_info *sbi,
2759 block_t blkaddr, int type)
2760{
2761 int i, end;
2762 if (IS_DATASEG(type))
2763 end = type + NR_CURSEG_DATA_TYPE;
2764 else
2765 end = type + NR_CURSEG_NODE_TYPE;
2766
Chao Yub7ad7512016-02-19 18:08:46 +08002767 for (i = type; i < end; i++)
2768 write_current_sum_page(sbi, i, blkaddr + (i - type));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002769}
2770
2771void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
2772{
Chao Yuaaec2b12016-09-20 11:04:18 +08002773 if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002774 write_compacted_summaries(sbi, start_blk);
2775 else
2776 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
2777}
2778
2779void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
2780{
Jaegeuk Kim119ee912015-01-29 11:45:33 -08002781 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002782}
2783
Chao Yudfc08a12016-02-14 18:50:40 +08002784int lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002785 unsigned int val, int alloc)
2786{
2787 int i;
2788
2789 if (type == NAT_JOURNAL) {
Chao Yudfc08a12016-02-14 18:50:40 +08002790 for (i = 0; i < nats_in_cursum(journal); i++) {
2791 if (le32_to_cpu(nid_in_journal(journal, i)) == val)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002792 return i;
2793 }
Chao Yudfc08a12016-02-14 18:50:40 +08002794 if (alloc && __has_cursum_space(journal, 1, NAT_JOURNAL))
2795 return update_nats_in_cursum(journal, 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002796 } else if (type == SIT_JOURNAL) {
Chao Yudfc08a12016-02-14 18:50:40 +08002797 for (i = 0; i < sits_in_cursum(journal); i++)
2798 if (le32_to_cpu(segno_in_journal(journal, i)) == val)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002799 return i;
Chao Yudfc08a12016-02-14 18:50:40 +08002800 if (alloc && __has_cursum_space(journal, 1, SIT_JOURNAL))
2801 return update_sits_in_cursum(journal, 1);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002802 }
2803 return -1;
2804}
2805
2806static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
2807 unsigned int segno)
2808{
Gu Zheng2cc22182014-10-20 17:45:49 +08002809 return get_meta_page(sbi, current_sit_addr(sbi, segno));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002810}
2811
2812static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
2813 unsigned int start)
2814{
2815 struct sit_info *sit_i = SIT_I(sbi);
2816 struct page *src_page, *dst_page;
2817 pgoff_t src_off, dst_off;
2818 void *src_addr, *dst_addr;
2819
2820 src_off = current_sit_addr(sbi, start);
2821 dst_off = next_sit_addr(sbi, src_off);
2822
2823 /* get current sit block page without lock */
2824 src_page = get_meta_page(sbi, src_off);
2825 dst_page = grab_meta_page(sbi, dst_off);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002826 f2fs_bug_on(sbi, PageDirty(src_page));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002827
2828 src_addr = page_address(src_page);
2829 dst_addr = page_address(dst_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002830 memcpy(dst_addr, src_addr, PAGE_SIZE);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002831
2832 set_page_dirty(dst_page);
2833 f2fs_put_page(src_page, 1);
2834
2835 set_to_next_sit(sit_i, start);
2836
2837 return dst_page;
2838}
2839
Chao Yu184a5cd2014-09-04 18:13:01 +08002840static struct sit_entry_set *grab_sit_entry_set(void)
2841{
2842 struct sit_entry_set *ses =
Jaegeuk Kim80c54502015-08-20 08:51:56 -07002843 f2fs_kmem_cache_alloc(sit_entry_set_slab, GFP_NOFS);
Chao Yu184a5cd2014-09-04 18:13:01 +08002844
2845 ses->entry_cnt = 0;
2846 INIT_LIST_HEAD(&ses->set_list);
2847 return ses;
2848}
2849
2850static void release_sit_entry_set(struct sit_entry_set *ses)
2851{
2852 list_del(&ses->set_list);
2853 kmem_cache_free(sit_entry_set_slab, ses);
2854}
2855
2856static void adjust_sit_entry_set(struct sit_entry_set *ses,
2857 struct list_head *head)
2858{
2859 struct sit_entry_set *next = ses;
2860
2861 if (list_is_last(&ses->set_list, head))
2862 return;
2863
2864 list_for_each_entry_continue(next, head, set_list)
2865 if (ses->entry_cnt <= next->entry_cnt)
2866 break;
2867
2868 list_move_tail(&ses->set_list, &next->set_list);
2869}
2870
2871static void add_sit_entry(unsigned int segno, struct list_head *head)
2872{
2873 struct sit_entry_set *ses;
2874 unsigned int start_segno = START_SEGNO(segno);
2875
2876 list_for_each_entry(ses, head, set_list) {
2877 if (ses->start_segno == start_segno) {
2878 ses->entry_cnt++;
2879 adjust_sit_entry_set(ses, head);
2880 return;
2881 }
2882 }
2883
2884 ses = grab_sit_entry_set();
2885
2886 ses->start_segno = start_segno;
2887 ses->entry_cnt++;
2888 list_add(&ses->set_list, head);
2889}
2890
2891static void add_sits_in_set(struct f2fs_sb_info *sbi)
2892{
2893 struct f2fs_sm_info *sm_info = SM_I(sbi);
2894 struct list_head *set_list = &sm_info->sit_entry_set;
2895 unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap;
Chao Yu184a5cd2014-09-04 18:13:01 +08002896 unsigned int segno;
2897
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002898 for_each_set_bit(segno, bitmap, MAIN_SEGS(sbi))
Chao Yu184a5cd2014-09-04 18:13:01 +08002899 add_sit_entry(segno, set_list);
2900}
2901
2902static void remove_sits_in_journal(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002903{
2904 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08002905 struct f2fs_journal *journal = curseg->journal;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002906 int i;
2907
Chao Yub7ad7512016-02-19 18:08:46 +08002908 down_write(&curseg->journal_rwsem);
Chao Yudfc08a12016-02-14 18:50:40 +08002909 for (i = 0; i < sits_in_cursum(journal); i++) {
Chao Yu184a5cd2014-09-04 18:13:01 +08002910 unsigned int segno;
2911 bool dirtied;
2912
Chao Yudfc08a12016-02-14 18:50:40 +08002913 segno = le32_to_cpu(segno_in_journal(journal, i));
Chao Yu184a5cd2014-09-04 18:13:01 +08002914 dirtied = __mark_sit_entry_dirty(sbi, segno);
2915
2916 if (!dirtied)
2917 add_sit_entry(segno, &SM_I(sbi)->sit_entry_set);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002918 }
Chao Yudfc08a12016-02-14 18:50:40 +08002919 update_sits_in_cursum(journal, -i);
Chao Yub7ad7512016-02-19 18:08:46 +08002920 up_write(&curseg->journal_rwsem);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002921}
2922
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002923/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002924 * CP calls this function, which flushes SIT entries including sit_journal,
2925 * and moves prefree segs to free segs.
2926 */
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002927void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002928{
2929 struct sit_info *sit_i = SIT_I(sbi);
2930 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
2931 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08002932 struct f2fs_journal *journal = curseg->journal;
Chao Yu184a5cd2014-09-04 18:13:01 +08002933 struct sit_entry_set *ses, *tmp;
2934 struct list_head *head = &SM_I(sbi)->sit_entry_set;
Chao Yu184a5cd2014-09-04 18:13:01 +08002935 bool to_journal = true;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002936 struct seg_entry *se;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002937
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002938 mutex_lock(&sit_i->sentry_lock);
2939
Wanpeng Li2b11a742015-02-27 16:52:50 +08002940 if (!sit_i->dirty_sentries)
2941 goto out;
2942
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002943 /*
Chao Yu184a5cd2014-09-04 18:13:01 +08002944 * add and account sit entries of dirty bitmap in sit entry
2945 * set temporarily
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002946 */
Chao Yu184a5cd2014-09-04 18:13:01 +08002947 add_sits_in_set(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002948
Chao Yu184a5cd2014-09-04 18:13:01 +08002949 /*
2950 * if there are no enough space in journal to store dirty sit
2951 * entries, remove all entries from journal and add and account
2952 * them in sit entry set.
2953 */
Chao Yudfc08a12016-02-14 18:50:40 +08002954 if (!__has_cursum_space(journal, sit_i->dirty_sentries, SIT_JOURNAL))
Chao Yu184a5cd2014-09-04 18:13:01 +08002955 remove_sits_in_journal(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002956
Chao Yu184a5cd2014-09-04 18:13:01 +08002957 /*
2958 * there are two steps to flush sit entries:
2959 * #1, flush sit entries to journal in current cold data summary block.
2960 * #2, flush sit entries to sit page.
2961 */
2962 list_for_each_entry_safe(ses, tmp, head, set_list) {
Jaegeuk Kim4a257ed2014-10-16 11:43:30 -07002963 struct page *page = NULL;
Chao Yu184a5cd2014-09-04 18:13:01 +08002964 struct f2fs_sit_block *raw_sit = NULL;
2965 unsigned int start_segno = ses->start_segno;
2966 unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK,
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07002967 (unsigned long)MAIN_SEGS(sbi));
Chao Yu184a5cd2014-09-04 18:13:01 +08002968 unsigned int segno = start_segno;
Jaegeuk Kimb2955552013-11-12 14:49:56 +09002969
Chao Yu184a5cd2014-09-04 18:13:01 +08002970 if (to_journal &&
Chao Yudfc08a12016-02-14 18:50:40 +08002971 !__has_cursum_space(journal, ses->entry_cnt, SIT_JOURNAL))
Chao Yu184a5cd2014-09-04 18:13:01 +08002972 to_journal = false;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002973
Chao Yub7ad7512016-02-19 18:08:46 +08002974 if (to_journal) {
2975 down_write(&curseg->journal_rwsem);
2976 } else {
Chao Yu184a5cd2014-09-04 18:13:01 +08002977 page = get_next_sit_page(sbi, start_segno);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09002978 raw_sit = page_address(page);
2979 }
2980
Chao Yu184a5cd2014-09-04 18:13:01 +08002981 /* flush dirty sit entries in region of current sit set */
2982 for_each_set_bit_from(segno, bitmap, end) {
2983 int offset, sit_offset;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002984
2985 se = get_seg_entry(sbi, segno);
Chao Yu184a5cd2014-09-04 18:13:01 +08002986
2987 /* add discard candidates */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002988 if (!(cpc->reason & CP_DISCARD)) {
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002989 cpc->trim_start = segno;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002990 add_discard_addrs(sbi, cpc, false);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002991 }
Chao Yu184a5cd2014-09-04 18:13:01 +08002992
2993 if (to_journal) {
Chao Yudfc08a12016-02-14 18:50:40 +08002994 offset = lookup_journal_in_cursum(journal,
Chao Yu184a5cd2014-09-04 18:13:01 +08002995 SIT_JOURNAL, segno, 1);
2996 f2fs_bug_on(sbi, offset < 0);
Chao Yudfc08a12016-02-14 18:50:40 +08002997 segno_in_journal(journal, offset) =
Chao Yu184a5cd2014-09-04 18:13:01 +08002998 cpu_to_le32(segno);
2999 seg_info_to_raw_sit(se,
Chao Yudfc08a12016-02-14 18:50:40 +08003000 &sit_in_journal(journal, offset));
Chao Yu184a5cd2014-09-04 18:13:01 +08003001 } else {
3002 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
3003 seg_info_to_raw_sit(se,
3004 &raw_sit->entries[sit_offset]);
3005 }
3006
3007 __clear_bit(segno, bitmap);
3008 sit_i->dirty_sentries--;
3009 ses->entry_cnt--;
3010 }
3011
Chao Yub7ad7512016-02-19 18:08:46 +08003012 if (to_journal)
3013 up_write(&curseg->journal_rwsem);
3014 else
Chao Yu184a5cd2014-09-04 18:13:01 +08003015 f2fs_put_page(page, 1);
3016
3017 f2fs_bug_on(sbi, ses->entry_cnt);
3018 release_sit_entry_set(ses);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003019 }
Chao Yu184a5cd2014-09-04 18:13:01 +08003020
3021 f2fs_bug_on(sbi, !list_empty(head));
3022 f2fs_bug_on(sbi, sit_i->dirty_sentries);
Chao Yu184a5cd2014-09-04 18:13:01 +08003023out:
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003024 if (cpc->reason & CP_DISCARD) {
3025 __u64 trim_start = cpc->trim_start;
3026
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003027 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003028 add_discard_addrs(sbi, cpc, false);
3029
3030 cpc->trim_start = trim_start;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003031 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003032 mutex_unlock(&sit_i->sentry_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003033
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003034 set_prefree_as_free_segments(sbi);
3035}
3036
3037static int build_sit_info(struct f2fs_sb_info *sbi)
3038{
3039 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003040 struct sit_info *sit_i;
3041 unsigned int sit_segs, start;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003042 char *src_bitmap;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003043 unsigned int bitmap_size;
3044
3045 /* allocate memory for SIT information */
3046 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
3047 if (!sit_i)
3048 return -ENOMEM;
3049
3050 SM_I(sbi)->sit_info = sit_i;
3051
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003052 sit_i->sentries = kvzalloc(MAIN_SEGS(sbi) *
Jaegeuk Kim39307a82015-09-22 13:50:47 -07003053 sizeof(struct seg_entry), GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003054 if (!sit_i->sentries)
3055 return -ENOMEM;
3056
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003057 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003058 sit_i->dirty_sentries_bitmap = kvzalloc(bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003059 if (!sit_i->dirty_sentries_bitmap)
3060 return -ENOMEM;
3061
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003062 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003063 sit_i->sentries[start].cur_valid_map
3064 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
3065 sit_i->sentries[start].ckpt_valid_map
3066 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07003067 if (!sit_i->sentries[start].cur_valid_map ||
Jaegeuk Kim3e025742016-08-02 10:56:40 -07003068 !sit_i->sentries[start].ckpt_valid_map)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003069 return -ENOMEM;
Jaegeuk Kim3e025742016-08-02 10:56:40 -07003070
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003071#ifdef CONFIG_F2FS_CHECK_FS
3072 sit_i->sentries[start].cur_valid_map_mir
3073 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
3074 if (!sit_i->sentries[start].cur_valid_map_mir)
3075 return -ENOMEM;
3076#endif
3077
Jaegeuk Kim3e025742016-08-02 10:56:40 -07003078 if (f2fs_discard_en(sbi)) {
3079 sit_i->sentries[start].discard_map
3080 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
3081 if (!sit_i->sentries[start].discard_map)
3082 return -ENOMEM;
3083 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003084 }
3085
Jaegeuk Kim60a3b782015-02-10 16:44:29 -08003086 sit_i->tmp_map = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
3087 if (!sit_i->tmp_map)
3088 return -ENOMEM;
3089
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003090 if (sbi->segs_per_sec > 1) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003091 sit_i->sec_entries = kvzalloc(MAIN_SECS(sbi) *
Jaegeuk Kim39307a82015-09-22 13:50:47 -07003092 sizeof(struct sec_entry), GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003093 if (!sit_i->sec_entries)
3094 return -ENOMEM;
3095 }
3096
3097 /* get information related with SIT */
3098 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
3099
3100 /* setup SIT bitmap from ckeckpoint pack */
3101 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
3102 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
3103
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003104 sit_i->sit_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
3105 if (!sit_i->sit_bitmap)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003106 return -ENOMEM;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003107
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003108#ifdef CONFIG_F2FS_CHECK_FS
3109 sit_i->sit_bitmap_mir = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
3110 if (!sit_i->sit_bitmap_mir)
3111 return -ENOMEM;
3112#endif
3113
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003114 /* init SIT information */
3115 sit_i->s_ops = &default_salloc_ops;
3116
3117 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
3118 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003119 sit_i->written_valid_blocks = 0;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003120 sit_i->bitmap_size = bitmap_size;
3121 sit_i->dirty_sentries = 0;
3122 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
3123 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003124 sit_i->mounted_time = ktime_get_real_seconds();
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003125 mutex_init(&sit_i->sentry_lock);
3126 return 0;
3127}
3128
3129static int build_free_segmap(struct f2fs_sb_info *sbi)
3130{
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003131 struct free_segmap_info *free_i;
3132 unsigned int bitmap_size, sec_bitmap_size;
3133
3134 /* allocate memory for free segmap information */
3135 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
3136 if (!free_i)
3137 return -ENOMEM;
3138
3139 SM_I(sbi)->free_info = free_i;
3140
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003141 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003142 free_i->free_segmap = kvmalloc(bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003143 if (!free_i->free_segmap)
3144 return -ENOMEM;
3145
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003146 sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003147 free_i->free_secmap = kvmalloc(sec_bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003148 if (!free_i->free_secmap)
3149 return -ENOMEM;
3150
3151 /* set all segments as dirty temporarily */
3152 memset(free_i->free_segmap, 0xff, bitmap_size);
3153 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
3154
3155 /* init free segmap information */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003156 free_i->start_segno = GET_SEGNO_FROM_SEG0(sbi, MAIN_BLKADDR(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003157 free_i->free_segments = 0;
3158 free_i->free_sections = 0;
Chao Yu1a118cc2015-02-11 18:20:38 +08003159 spin_lock_init(&free_i->segmap_lock);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003160 return 0;
3161}
3162
3163static int build_curseg(struct f2fs_sb_info *sbi)
3164{
Namjae Jeon1042d602012-12-01 10:56:13 +09003165 struct curseg_info *array;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003166 int i;
3167
Fabian Frederickb434bab2014-06-23 18:39:15 +02003168 array = kcalloc(NR_CURSEG_TYPE, sizeof(*array), GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003169 if (!array)
3170 return -ENOMEM;
3171
3172 SM_I(sbi)->curseg_array = array;
3173
3174 for (i = 0; i < NR_CURSEG_TYPE; i++) {
3175 mutex_init(&array[i].curseg_mutex);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003176 array[i].sum_blk = kzalloc(PAGE_SIZE, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003177 if (!array[i].sum_blk)
3178 return -ENOMEM;
Chao Yub7ad7512016-02-19 18:08:46 +08003179 init_rwsem(&array[i].journal_rwsem);
3180 array[i].journal = kzalloc(sizeof(struct f2fs_journal),
3181 GFP_KERNEL);
3182 if (!array[i].journal)
3183 return -ENOMEM;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003184 array[i].segno = NULL_SEGNO;
3185 array[i].next_blkoff = 0;
3186 }
3187 return restore_curseg_summaries(sbi);
3188}
3189
3190static void build_sit_entries(struct f2fs_sb_info *sbi)
3191{
3192 struct sit_info *sit_i = SIT_I(sbi);
3193 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08003194 struct f2fs_journal *journal = curseg->journal;
Yunlei He9c094042016-09-24 12:29:18 +08003195 struct seg_entry *se;
3196 struct f2fs_sit_entry sit;
Chao Yu74de5932013-11-22 09:09:59 +08003197 int sit_blk_cnt = SIT_BLK_CNT(sbi);
3198 unsigned int i, start, end;
3199 unsigned int readed, start_blk = 0;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003200
Chao Yu74de5932013-11-22 09:09:59 +08003201 do {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003202 readed = ra_meta_pages(sbi, start_blk, BIO_MAX_PAGES,
3203 META_SIT, true);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003204
Chao Yu74de5932013-11-22 09:09:59 +08003205 start = start_blk * sit_i->sents_per_block;
3206 end = (start_blk + readed) * sit_i->sents_per_block;
3207
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003208 for (; start < end && start < MAIN_SEGS(sbi); start++) {
Chao Yu74de5932013-11-22 09:09:59 +08003209 struct f2fs_sit_block *sit_blk;
Chao Yu74de5932013-11-22 09:09:59 +08003210 struct page *page;
3211
Yunlei He9c094042016-09-24 12:29:18 +08003212 se = &sit_i->sentries[start];
Chao Yu74de5932013-11-22 09:09:59 +08003213 page = get_current_sit_page(sbi, start);
3214 sit_blk = (struct f2fs_sit_block *)page_address(page);
3215 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
3216 f2fs_put_page(page, 1);
Chao Yud600af232016-08-19 23:13:47 +08003217
Chao Yu74de5932013-11-22 09:09:59 +08003218 check_block_count(sbi, start, &sit);
3219 seg_info_from_raw_sit(se, &sit);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07003220
3221 /* build discard map only one time */
Jaegeuk Kim3e025742016-08-02 10:56:40 -07003222 if (f2fs_discard_en(sbi)) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003223 if (is_set_ckpt_flags(sbi, CP_TRIMMED_FLAG)) {
3224 memset(se->discard_map, 0xff,
3225 SIT_VBLOCK_MAP_SIZE);
3226 } else {
3227 memcpy(se->discard_map,
3228 se->cur_valid_map,
3229 SIT_VBLOCK_MAP_SIZE);
3230 sbi->discard_blks +=
3231 sbi->blocks_per_seg -
3232 se->valid_blocks;
3233 }
Jaegeuk Kim3e025742016-08-02 10:56:40 -07003234 }
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07003235
Chao Yud600af232016-08-19 23:13:47 +08003236 if (sbi->segs_per_sec > 1)
3237 get_sec_entry(sbi, start)->valid_blocks +=
3238 se->valid_blocks;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003239 }
Chao Yu74de5932013-11-22 09:09:59 +08003240 start_blk += readed;
3241 } while (start_blk < sit_blk_cnt);
Chao Yud600af232016-08-19 23:13:47 +08003242
3243 down_read(&curseg->journal_rwsem);
3244 for (i = 0; i < sits_in_cursum(journal); i++) {
Chao Yud600af232016-08-19 23:13:47 +08003245 unsigned int old_valid_blocks;
3246
3247 start = le32_to_cpu(segno_in_journal(journal, i));
3248 se = &sit_i->sentries[start];
3249 sit = sit_in_journal(journal, i);
3250
3251 old_valid_blocks = se->valid_blocks;
3252
3253 check_block_count(sbi, start, &sit);
3254 seg_info_from_raw_sit(se, &sit);
3255
3256 if (f2fs_discard_en(sbi)) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003257 if (is_set_ckpt_flags(sbi, CP_TRIMMED_FLAG)) {
3258 memset(se->discard_map, 0xff,
3259 SIT_VBLOCK_MAP_SIZE);
3260 } else {
3261 memcpy(se->discard_map, se->cur_valid_map,
3262 SIT_VBLOCK_MAP_SIZE);
3263 sbi->discard_blks += old_valid_blocks -
3264 se->valid_blocks;
3265 }
Chao Yud600af232016-08-19 23:13:47 +08003266 }
3267
3268 if (sbi->segs_per_sec > 1)
3269 get_sec_entry(sbi, start)->valid_blocks +=
3270 se->valid_blocks - old_valid_blocks;
3271 }
3272 up_read(&curseg->journal_rwsem);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003273}
3274
3275static void init_free_segmap(struct f2fs_sb_info *sbi)
3276{
3277 unsigned int start;
3278 int type;
3279
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003280 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003281 struct seg_entry *sentry = get_seg_entry(sbi, start);
3282 if (!sentry->valid_blocks)
3283 __set_free(sbi, start);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003284 else
3285 SIT_I(sbi)->written_valid_blocks +=
3286 sentry->valid_blocks;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003287 }
3288
3289 /* set use the current segments */
3290 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
3291 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
3292 __set_test_and_inuse(sbi, curseg_t->segno);
3293 }
3294}
3295
3296static void init_dirty_segmap(struct f2fs_sb_info *sbi)
3297{
3298 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
3299 struct free_segmap_info *free_i = FREE_I(sbi);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003300 unsigned int segno = 0, offset = 0;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003301 unsigned short valid_blocks;
3302
Namjae Jeon8736fbf2013-06-16 09:49:11 +09003303 while (1) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003304 /* find dirty segment based on free segmap */
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003305 segno = find_next_inuse(free_i, MAIN_SEGS(sbi), offset);
3306 if (segno >= MAIN_SEGS(sbi))
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003307 break;
3308 offset = segno + 1;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003309 valid_blocks = get_valid_blocks(sbi, segno, false);
Jaegeuk Kimec325b52014-09-02 16:24:11 -07003310 if (valid_blocks == sbi->blocks_per_seg || !valid_blocks)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003311 continue;
Jaegeuk Kimec325b52014-09-02 16:24:11 -07003312 if (valid_blocks > sbi->blocks_per_seg) {
3313 f2fs_bug_on(sbi, 1);
3314 continue;
3315 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003316 mutex_lock(&dirty_i->seglist_lock);
3317 __locate_dirty_segment(sbi, segno, DIRTY);
3318 mutex_unlock(&dirty_i->seglist_lock);
3319 }
3320}
3321
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09003322static int init_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003323{
3324 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003325 unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003326
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003327 dirty_i->victim_secmap = kvzalloc(bitmap_size, GFP_KERNEL);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09003328 if (!dirty_i->victim_secmap)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003329 return -ENOMEM;
3330 return 0;
3331}
3332
3333static int build_dirty_segmap(struct f2fs_sb_info *sbi)
3334{
3335 struct dirty_seglist_info *dirty_i;
3336 unsigned int bitmap_size, i;
3337
3338 /* allocate memory for dirty segments list information */
3339 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
3340 if (!dirty_i)
3341 return -ENOMEM;
3342
3343 SM_I(sbi)->dirty_info = dirty_i;
3344 mutex_init(&dirty_i->seglist_lock);
3345
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003346 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003347
3348 for (i = 0; i < NR_DIRTY_TYPE; i++) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003349 dirty_i->dirty_segmap[i] = kvzalloc(bitmap_size, GFP_KERNEL);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003350 if (!dirty_i->dirty_segmap[i])
3351 return -ENOMEM;
3352 }
3353
3354 init_dirty_segmap(sbi);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09003355 return init_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003356}
3357
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09003358/*
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003359 * Update min, max modified time for cost-benefit GC algorithm
3360 */
3361static void init_min_max_mtime(struct f2fs_sb_info *sbi)
3362{
3363 struct sit_info *sit_i = SIT_I(sbi);
3364 unsigned int segno;
3365
3366 mutex_lock(&sit_i->sentry_lock);
3367
3368 sit_i->min_mtime = LLONG_MAX;
3369
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003370 for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003371 unsigned int i;
3372 unsigned long long mtime = 0;
3373
3374 for (i = 0; i < sbi->segs_per_sec; i++)
3375 mtime += get_seg_entry(sbi, segno + i)->mtime;
3376
3377 mtime = div_u64(mtime, sbi->segs_per_sec);
3378
3379 if (sit_i->min_mtime > mtime)
3380 sit_i->min_mtime = mtime;
3381 }
3382 sit_i->max_mtime = get_mtime(sbi);
3383 mutex_unlock(&sit_i->sentry_lock);
3384}
3385
3386int build_segment_manager(struct f2fs_sb_info *sbi)
3387{
3388 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
3389 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Namjae Jeon1042d602012-12-01 10:56:13 +09003390 struct f2fs_sm_info *sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003391 int err;
3392
3393 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
3394 if (!sm_info)
3395 return -ENOMEM;
3396
3397 /* init sm info */
3398 sbi->sm_info = sm_info;
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003399 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
3400 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
3401 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
3402 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
3403 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
3404 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
3405 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
Jaegeuk Kim58c41032014-03-19 14:17:21 +09003406 sm_info->rec_prefree_segments = sm_info->main_segments *
3407 DEF_RECLAIM_PREFREE_SEGMENTS / 100;
Jaegeuk Kim44a83492016-07-13 18:23:35 -07003408 if (sm_info->rec_prefree_segments > DEF_MAX_RECLAIM_PREFREE_SEGMENTS)
3409 sm_info->rec_prefree_segments = DEF_MAX_RECLAIM_PREFREE_SEGMENTS;
3410
Jaegeuk Kim52763a42016-06-13 09:47:48 -07003411 if (!test_opt(sbi, LFS))
3412 sm_info->ipu_policy = 1 << F2FS_IPU_FSYNC;
Jaegeuk Kim216fbd62013-11-07 13:13:42 +09003413 sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -07003414 sm_info->min_fsync_blocks = DEF_MIN_FSYNC_BLOCKS;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003415 sm_info->min_hot_blocks = DEF_MIN_HOT_BLOCKS;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09003416
Jaegeuk Kimbba681c2015-01-26 17:41:23 -08003417 sm_info->trim_sections = DEF_BATCHED_TRIM_SECTIONS;
3418
Chao Yu184a5cd2014-09-04 18:13:01 +08003419 INIT_LIST_HEAD(&sm_info->sit_entry_set);
3420
Yunlei Head9baf42017-06-01 16:43:51 +08003421 if (!f2fs_readonly(sbi->sb)) {
Gu Zheng2163d192014-04-27 14:21:33 +08003422 err = create_flush_cmd_control(sbi);
3423 if (err)
Gu Zhenga688b9d9e2014-04-27 14:21:21 +08003424 return err;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +09003425 }
3426
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003427 err = create_discard_cmd_control(sbi);
3428 if (err)
3429 return err;
3430
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003431 err = build_sit_info(sbi);
3432 if (err)
3433 return err;
3434 err = build_free_segmap(sbi);
3435 if (err)
3436 return err;
3437 err = build_curseg(sbi);
3438 if (err)
3439 return err;
3440
3441 /* reinit free segmap based on SIT */
3442 build_sit_entries(sbi);
3443
3444 init_free_segmap(sbi);
3445 err = build_dirty_segmap(sbi);
3446 if (err)
3447 return err;
3448
3449 init_min_max_mtime(sbi);
3450 return 0;
3451}
3452
3453static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
3454 enum dirty_type dirty_type)
3455{
3456 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
3457
3458 mutex_lock(&dirty_i->seglist_lock);
Jaegeuk Kim39307a82015-09-22 13:50:47 -07003459 kvfree(dirty_i->dirty_segmap[dirty_type]);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003460 dirty_i->nr_dirty[dirty_type] = 0;
3461 mutex_unlock(&dirty_i->seglist_lock);
3462}
3463
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09003464static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003465{
3466 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim39307a82015-09-22 13:50:47 -07003467 kvfree(dirty_i->victim_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003468}
3469
3470static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
3471{
3472 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
3473 int i;
3474
3475 if (!dirty_i)
3476 return;
3477
3478 /* discard pre-free/dirty segments list */
3479 for (i = 0; i < NR_DIRTY_TYPE; i++)
3480 discard_dirty_segmap(sbi, i);
3481
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09003482 destroy_victim_secmap(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003483 SM_I(sbi)->dirty_info = NULL;
3484 kfree(dirty_i);
3485}
3486
3487static void destroy_curseg(struct f2fs_sb_info *sbi)
3488{
3489 struct curseg_info *array = SM_I(sbi)->curseg_array;
3490 int i;
3491
3492 if (!array)
3493 return;
3494 SM_I(sbi)->curseg_array = NULL;
Chao Yub7ad7512016-02-19 18:08:46 +08003495 for (i = 0; i < NR_CURSEG_TYPE; i++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003496 kfree(array[i].sum_blk);
Chao Yub7ad7512016-02-19 18:08:46 +08003497 kfree(array[i].journal);
3498 }
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003499 kfree(array);
3500}
3501
3502static void destroy_free_segmap(struct f2fs_sb_info *sbi)
3503{
3504 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
3505 if (!free_i)
3506 return;
3507 SM_I(sbi)->free_info = NULL;
Jaegeuk Kim39307a82015-09-22 13:50:47 -07003508 kvfree(free_i->free_segmap);
3509 kvfree(free_i->free_secmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003510 kfree(free_i);
3511}
3512
3513static void destroy_sit_info(struct f2fs_sb_info *sbi)
3514{
3515 struct sit_info *sit_i = SIT_I(sbi);
3516 unsigned int start;
3517
3518 if (!sit_i)
3519 return;
3520
3521 if (sit_i->sentries) {
Jaegeuk Kim7cd85582014-09-23 11:23:01 -07003522 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003523 kfree(sit_i->sentries[start].cur_valid_map);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003524#ifdef CONFIG_F2FS_CHECK_FS
3525 kfree(sit_i->sentries[start].cur_valid_map_mir);
3526#endif
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003527 kfree(sit_i->sentries[start].ckpt_valid_map);
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07003528 kfree(sit_i->sentries[start].discard_map);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003529 }
3530 }
Jaegeuk Kim60a3b782015-02-10 16:44:29 -08003531 kfree(sit_i->tmp_map);
3532
Jaegeuk Kim39307a82015-09-22 13:50:47 -07003533 kvfree(sit_i->sentries);
3534 kvfree(sit_i->sec_entries);
3535 kvfree(sit_i->dirty_sentries_bitmap);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003536
3537 SM_I(sbi)->sit_info = NULL;
3538 kfree(sit_i->sit_bitmap);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003539#ifdef CONFIG_F2FS_CHECK_FS
3540 kfree(sit_i->sit_bitmap_mir);
3541#endif
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003542 kfree(sit_i);
3543}
3544
3545void destroy_segment_manager(struct f2fs_sb_info *sbi)
3546{
3547 struct f2fs_sm_info *sm_info = SM_I(sbi);
Gu Zhenga688b9d9e2014-04-27 14:21:21 +08003548
Chao Yu3b03f722013-11-06 09:12:04 +08003549 if (!sm_info)
3550 return;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003551 destroy_flush_cmd_control(sbi, true);
3552 destroy_discard_cmd_control(sbi);
Jaegeuk Kim351df4b2012-11-02 17:09:16 +09003553 destroy_dirty_segmap(sbi);
3554 destroy_curseg(sbi);
3555 destroy_free_segmap(sbi);
3556 destroy_sit_info(sbi);
3557 sbi->sm_info = NULL;
3558 kfree(sm_info);
3559}
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09003560
3561int __init create_segment_manager_caches(void)
3562{
3563 discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
Gu Zhenge8512d22014-03-07 18:43:28 +08003564 sizeof(struct discard_entry));
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09003565 if (!discard_entry_slab)
Chao Yu184a5cd2014-09-04 18:13:01 +08003566 goto fail;
3567
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003568 discard_cmd_slab = f2fs_kmem_cache_create("discard_cmd",
3569 sizeof(struct discard_cmd));
3570 if (!discard_cmd_slab)
Chao Yu6ab2a302016-09-05 12:28:26 +08003571 goto destroy_discard_entry;
Chao Yu275b66b2016-08-29 23:58:34 +08003572
Chao Yu184a5cd2014-09-04 18:13:01 +08003573 sit_entry_set_slab = f2fs_kmem_cache_create("sit_entry_set",
Changman Leec9ee0082014-11-21 15:42:07 +09003574 sizeof(struct sit_entry_set));
Chao Yu184a5cd2014-09-04 18:13:01 +08003575 if (!sit_entry_set_slab)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003576 goto destroy_discard_cmd;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07003577
3578 inmem_entry_slab = f2fs_kmem_cache_create("inmem_page_entry",
3579 sizeof(struct inmem_pages));
3580 if (!inmem_entry_slab)
3581 goto destroy_sit_entry_set;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09003582 return 0;
Chao Yu184a5cd2014-09-04 18:13:01 +08003583
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07003584destroy_sit_entry_set:
3585 kmem_cache_destroy(sit_entry_set_slab);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003586destroy_discard_cmd:
3587 kmem_cache_destroy(discard_cmd_slab);
Chao Yu6ab2a302016-09-05 12:28:26 +08003588destroy_discard_entry:
Chao Yu184a5cd2014-09-04 18:13:01 +08003589 kmem_cache_destroy(discard_entry_slab);
3590fail:
3591 return -ENOMEM;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09003592}
3593
3594void destroy_segment_manager_caches(void)
3595{
Chao Yu184a5cd2014-09-04 18:13:01 +08003596 kmem_cache_destroy(sit_entry_set_slab);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003597 kmem_cache_destroy(discard_cmd_slab);
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09003598 kmem_cache_destroy(discard_entry_slab);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07003599 kmem_cache_destroy(inmem_entry_slab);
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09003600}