]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - fs/f2fs/segment.c
f2fs: support zone capacity less than zone size
[mirror_ubuntu-jammy-kernel.git] / fs / f2fs / segment.c
CommitLineData
7c1a000d 1// SPDX-License-Identifier: GPL-2.0
0a8165d7 2/*
351df4b2
JK
3 * fs/f2fs/segment.c
4 *
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
351df4b2
JK
7 */
8#include <linux/fs.h>
9#include <linux/f2fs_fs.h>
10#include <linux/bio.h>
11#include <linux/blkdev.h>
690e4a3e 12#include <linux/prefetch.h>
6b4afdd7 13#include <linux/kthread.h>
74de593a 14#include <linux/swap.h>
60b99b48 15#include <linux/timer.h>
1d7be270 16#include <linux/freezer.h>
1eb1ef4a 17#include <linux/sched/signal.h>
351df4b2
JK
18
19#include "f2fs.h"
20#include "segment.h"
21#include "node.h"
5f656541 22#include "gc.h"
9e4ded3f 23#include "trace.h"
6ec178da 24#include <trace/events/f2fs.h>
351df4b2 25
9a7f143a
CL
26#define __reverse_ffz(x) __reverse_ffs(~(x))
27
7fd9e544 28static struct kmem_cache *discard_entry_slab;
b01a9201 29static struct kmem_cache *discard_cmd_slab;
184a5cd2 30static struct kmem_cache *sit_entry_set_slab;
88b88a66 31static struct kmem_cache *inmem_entry_slab;
7fd9e544 32
f96999c3
JK
33static unsigned long __reverse_ulong(unsigned char *str)
34{
35 unsigned long tmp = 0;
36 int shift = 24, idx = 0;
37
38#if BITS_PER_LONG == 64
39 shift = 56;
40#endif
41 while (shift >= 0) {
42 tmp |= (unsigned long)str[idx++] << shift;
43 shift -= BITS_PER_BYTE;
44 }
45 return tmp;
46}
47
9a7f143a
CL
48/*
49 * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since
50 * MSB and LSB are reversed in a byte by f2fs_set_bit.
51 */
52static inline unsigned long __reverse_ffs(unsigned long word)
53{
54 int num = 0;
55
56#if BITS_PER_LONG == 64
f96999c3 57 if ((word & 0xffffffff00000000UL) == 0)
9a7f143a 58 num += 32;
f96999c3 59 else
9a7f143a 60 word >>= 32;
9a7f143a 61#endif
f96999c3 62 if ((word & 0xffff0000) == 0)
9a7f143a 63 num += 16;
f96999c3 64 else
9a7f143a 65 word >>= 16;
f96999c3
JK
66
67 if ((word & 0xff00) == 0)
9a7f143a 68 num += 8;
f96999c3 69 else
9a7f143a 70 word >>= 8;
f96999c3 71
9a7f143a
CL
72 if ((word & 0xf0) == 0)
73 num += 4;
74 else
75 word >>= 4;
f96999c3 76
9a7f143a
CL
77 if ((word & 0xc) == 0)
78 num += 2;
79 else
80 word >>= 2;
f96999c3 81
9a7f143a
CL
82 if ((word & 0x2) == 0)
83 num += 1;
84 return num;
85}
86
87/*
e1c42045 88 * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c because
9a7f143a 89 * f2fs_set_bit makes MSB and LSB reversed in a byte.
692223d1 90 * @size must be integral times of unsigned long.
9a7f143a 91 * Example:
f96999c3
JK
92 * MSB <--> LSB
93 * f2fs_set_bit(0, bitmap) => 1000 0000
94 * f2fs_set_bit(7, bitmap) => 0000 0001
9a7f143a
CL
95 */
96static unsigned long __find_rev_next_bit(const unsigned long *addr,
97 unsigned long size, unsigned long offset)
98{
99 const unsigned long *p = addr + BIT_WORD(offset);
692223d1 100 unsigned long result = size;
9a7f143a 101 unsigned long tmp;
9a7f143a
CL
102
103 if (offset >= size)
104 return size;
105
692223d1 106 size -= (offset & ~(BITS_PER_LONG - 1));
9a7f143a 107 offset %= BITS_PER_LONG;
f96999c3 108
692223d1
FL
109 while (1) {
110 if (*p == 0)
111 goto pass;
9a7f143a 112
f96999c3 113 tmp = __reverse_ulong((unsigned char *)p);
692223d1
FL
114
115 tmp &= ~0UL >> offset;
116 if (size < BITS_PER_LONG)
117 tmp &= (~0UL << (BITS_PER_LONG - size));
9a7f143a 118 if (tmp)
692223d1
FL
119 goto found;
120pass:
121 if (size <= BITS_PER_LONG)
122 break;
9a7f143a 123 size -= BITS_PER_LONG;
692223d1 124 offset = 0;
f96999c3 125 p++;
9a7f143a 126 }
692223d1
FL
127 return result;
128found:
129 return result - size + __reverse_ffs(tmp);
9a7f143a
CL
130}
131
132static unsigned long __find_rev_next_zero_bit(const unsigned long *addr,
133 unsigned long size, unsigned long offset)
134{
135 const unsigned long *p = addr + BIT_WORD(offset);
80609448 136 unsigned long result = size;
9a7f143a 137 unsigned long tmp;
9a7f143a
CL
138
139 if (offset >= size)
140 return size;
141
80609448 142 size -= (offset & ~(BITS_PER_LONG - 1));
9a7f143a 143 offset %= BITS_PER_LONG;
80609448
JK
144
145 while (1) {
146 if (*p == ~0UL)
147 goto pass;
148
f96999c3 149 tmp = __reverse_ulong((unsigned char *)p);
80609448
JK
150
151 if (offset)
152 tmp |= ~0UL << (BITS_PER_LONG - offset);
153 if (size < BITS_PER_LONG)
154 tmp |= ~0UL >> size;
f96999c3 155 if (tmp != ~0UL)
80609448
JK
156 goto found;
157pass:
158 if (size <= BITS_PER_LONG)
159 break;
9a7f143a 160 size -= BITS_PER_LONG;
80609448 161 offset = 0;
f96999c3 162 p++;
9a7f143a 163 }
80609448
JK
164 return result;
165found:
166 return result - size + __reverse_ffz(tmp);
9a7f143a
CL
167}
168
4d57b86d 169bool f2fs_need_SSR(struct f2fs_sb_info *sbi)
b3a97a2a
JK
170{
171 int node_secs = get_blocktype_secs(sbi, F2FS_DIRTY_NODES);
172 int dent_secs = get_blocktype_secs(sbi, F2FS_DIRTY_DENTS);
173 int imeta_secs = get_blocktype_secs(sbi, F2FS_DIRTY_IMETA);
174
b0332a0f 175 if (f2fs_lfs_mode(sbi))
b3a97a2a 176 return false;
0e5e8111 177 if (sbi->gc_mode == GC_URGENT_HIGH)
b3a97a2a 178 return true;
4354994f
DR
179 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
180 return true;
b3a97a2a
JK
181
182 return free_sections(sbi) <= (node_secs + 2 * dent_secs + imeta_secs +
a2a12b67 183 SM_I(sbi)->min_ssr_sections + reserved_sections(sbi));
b3a97a2a
JK
184}
185
4d57b86d 186void f2fs_register_inmem_page(struct inode *inode, struct page *page)
88b88a66 187{
88b88a66 188 struct inmem_pages *new;
9be32d72 189
9e4ded3f 190 f2fs_trace_pid(page);
0722b101 191
240a5915 192 f2fs_set_page_private(page, (unsigned long)ATOMIC_WRITTEN_PAGE);
decd36b6 193
88b88a66
JK
194 new = f2fs_kmem_cache_alloc(inmem_entry_slab, GFP_NOFS);
195
196 /* add atomic page indices to the list */
197 new->page = page;
198 INIT_LIST_HEAD(&new->list);
decd36b6 199
88b88a66 200 /* increase reference count with clean state */
88b88a66 201 get_page(page);
743b620c
JK
202 mutex_lock(&F2FS_I(inode)->inmem_lock);
203 list_add_tail(&new->list, &F2FS_I(inode)->inmem_pages);
8dcf2ff7 204 inc_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
743b620c 205 mutex_unlock(&F2FS_I(inode)->inmem_lock);
8ce67cb0
JK
206
207 trace_f2fs_register_inmem_page(page, INMEM);
88b88a66
JK
208}
209
28bc106b 210static int __revoke_inmem_pages(struct inode *inode,
48432984
CY
211 struct list_head *head, bool drop, bool recover,
212 bool trylock)
29b96b54 213{
28bc106b 214 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
29b96b54 215 struct inmem_pages *cur, *tmp;
28bc106b 216 int err = 0;
29b96b54
CY
217
218 list_for_each_entry_safe(cur, tmp, head, list) {
28bc106b
CY
219 struct page *page = cur->page;
220
221 if (drop)
222 trace_f2fs_commit_inmem_page(page, INMEM_DROP);
223
48432984
CY
224 if (trylock) {
225 /*
226 * to avoid deadlock in between page lock and
227 * inmem_lock.
228 */
229 if (!trylock_page(page))
230 continue;
231 } else {
232 lock_page(page);
233 }
29b96b54 234
bae0ee7a 235 f2fs_wait_on_page_writeback(page, DATA, true, true);
e5e5732d 236
28bc106b
CY
237 if (recover) {
238 struct dnode_of_data dn;
239 struct node_info ni;
240
241 trace_f2fs_commit_inmem_page(page, INMEM_REVOKE);
7f2b4e8e 242retry:
28bc106b 243 set_new_dnode(&dn, inode, NULL, NULL, 0);
4d57b86d
CY
244 err = f2fs_get_dnode_of_data(&dn, page->index,
245 LOOKUP_NODE);
7f2b4e8e
CY
246 if (err) {
247 if (err == -ENOMEM) {
5df7731f
CY
248 congestion_wait(BLK_RW_ASYNC,
249 DEFAULT_IO_TIMEOUT);
7f2b4e8e
CY
250 cond_resched();
251 goto retry;
252 }
28bc106b
CY
253 err = -EAGAIN;
254 goto next;
255 }
7735730d
CY
256
257 err = f2fs_get_node_info(sbi, dn.nid, &ni);
258 if (err) {
259 f2fs_put_dnode(&dn);
260 return err;
261 }
262
f1d2564a 263 if (cur->old_addr == NEW_ADDR) {
4d57b86d 264 f2fs_invalidate_blocks(sbi, dn.data_blkaddr);
f1d2564a
DJ
265 f2fs_update_data_blkaddr(&dn, NEW_ADDR);
266 } else
267 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
28bc106b
CY
268 cur->old_addr, ni.version, true, true);
269 f2fs_put_dnode(&dn);
270 }
271next:
63c52d78 272 /* we don't need to invalidate this in the sccessful status */
2baf0781 273 if (drop || recover) {
63c52d78 274 ClearPageUptodate(page);
2baf0781
CY
275 clear_cold_data(page);
276 }
240a5915 277 f2fs_clear_page_private(page);
28bc106b 278 f2fs_put_page(page, 1);
29b96b54
CY
279
280 list_del(&cur->list);
281 kmem_cache_free(inmem_entry_slab, cur);
282 dec_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
283 }
28bc106b 284 return err;
29b96b54
CY
285}
286
4d57b86d 287void f2fs_drop_inmem_pages_all(struct f2fs_sb_info *sbi, bool gc_failure)
57864ae5
JK
288{
289 struct list_head *head = &sbi->inode_list[ATOMIC_FILE];
290 struct inode *inode;
291 struct f2fs_inode_info *fi;
677017d1
ST
292 unsigned int count = sbi->atomic_files;
293 unsigned int looped = 0;
57864ae5
JK
294next:
295 spin_lock(&sbi->inode_lock[ATOMIC_FILE]);
296 if (list_empty(head)) {
297 spin_unlock(&sbi->inode_lock[ATOMIC_FILE]);
298 return;
299 }
300 fi = list_first_entry(head, struct f2fs_inode_info, inmem_ilist);
301 inode = igrab(&fi->vfs_inode);
677017d1
ST
302 if (inode)
303 list_move_tail(&fi->inmem_ilist, head);
57864ae5
JK
304 spin_unlock(&sbi->inode_lock[ATOMIC_FILE]);
305
306 if (inode) {
2ef79ecb 307 if (gc_failure) {
677017d1
ST
308 if (!fi->i_gc_failures[GC_FAILURE_ATOMIC])
309 goto skip;
2ef79ecb 310 }
2ef79ecb 311 set_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
4d57b86d 312 f2fs_drop_inmem_pages(inode);
677017d1 313skip:
57864ae5
JK
314 iput(inode);
315 }
5df7731f 316 congestion_wait(BLK_RW_ASYNC, DEFAULT_IO_TIMEOUT);
57864ae5 317 cond_resched();
677017d1
ST
318 if (gc_failure) {
319 if (++looped >= count)
320 return;
321 }
57864ae5
JK
322 goto next;
323}
324
4d57b86d 325void f2fs_drop_inmem_pages(struct inode *inode)
29b96b54 326{
57864ae5 327 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
29b96b54
CY
328 struct f2fs_inode_info *fi = F2FS_I(inode);
329
48432984
CY
330 while (!list_empty(&fi->inmem_pages)) {
331 mutex_lock(&fi->inmem_lock);
332 __revoke_inmem_pages(inode, &fi->inmem_pages,
333 true, false, true);
48432984
CY
334 mutex_unlock(&fi->inmem_lock);
335 }
5fe45743 336
2ef79ecb 337 fi->i_gc_failures[GC_FAILURE_ATOMIC] = 0;
743b620c
JK
338
339 spin_lock(&sbi->inode_lock[ATOMIC_FILE]);
340 if (!list_empty(&fi->inmem_ilist))
341 list_del_init(&fi->inmem_ilist);
677017d1
ST
342 if (f2fs_is_atomic_file(inode)) {
343 clear_inode_flag(inode, FI_ATOMIC_FILE);
344 sbi->atomic_files--;
345 }
743b620c 346 spin_unlock(&sbi->inode_lock[ATOMIC_FILE]);
29b96b54
CY
347}
348
4d57b86d 349void f2fs_drop_inmem_page(struct inode *inode, struct page *page)
8c242db9
JK
350{
351 struct f2fs_inode_info *fi = F2FS_I(inode);
352 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
353 struct list_head *head = &fi->inmem_pages;
354 struct inmem_pages *cur = NULL;
355
356 f2fs_bug_on(sbi, !IS_ATOMIC_WRITTEN_PAGE(page));
357
358 mutex_lock(&fi->inmem_lock);
359 list_for_each_entry(cur, head, list) {
360 if (cur->page == page)
361 break;
362 }
363
d0891e84 364 f2fs_bug_on(sbi, list_empty(head) || cur->page != page);
8c242db9
JK
365 list_del(&cur->list);
366 mutex_unlock(&fi->inmem_lock);
367
368 dec_page_count(sbi, F2FS_INMEM_PAGES);
369 kmem_cache_free(inmem_entry_slab, cur);
370
371 ClearPageUptodate(page);
240a5915 372 f2fs_clear_page_private(page);
8c242db9
JK
373 f2fs_put_page(page, 0);
374
375 trace_f2fs_commit_inmem_page(page, INMEM_INVALIDATE);
376}
377
4d57b86d 378static int __f2fs_commit_inmem_pages(struct inode *inode)
88b88a66
JK
379{
380 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
381 struct f2fs_inode_info *fi = F2FS_I(inode);
382 struct inmem_pages *cur, *tmp;
88b88a66 383 struct f2fs_io_info fio = {
05ca3632 384 .sbi = sbi,
39d787be 385 .ino = inode->i_ino,
88b88a66 386 .type = DATA,
04d328de 387 .op = REQ_OP_WRITE,
70fd7614 388 .op_flags = REQ_SYNC | REQ_PRIO,
b0af6d49 389 .io_type = FS_DATA_IO,
88b88a66 390 };
cf52b27a 391 struct list_head revoke_list;
bab475c5 392 bool submit_bio = false;
edb27dee 393 int err = 0;
88b88a66 394
cf52b27a
CY
395 INIT_LIST_HEAD(&revoke_list);
396
88b88a66 397 list_for_each_entry_safe(cur, tmp, &fi->inmem_pages, list) {
28bc106b
CY
398 struct page *page = cur->page;
399
400 lock_page(page);
401 if (page->mapping == inode->i_mapping) {
402 trace_f2fs_commit_inmem_page(page, INMEM);
403
bae0ee7a 404 f2fs_wait_on_page_writeback(page, DATA, true, true);
8d64d365
CY
405
406 set_page_dirty(page);
933439c8 407 if (clear_page_dirty_for_io(page)) {
29b96b54 408 inode_dec_dirty_pages(inode);
4d57b86d 409 f2fs_remove_dirty_inode(inode);
933439c8 410 }
640cc189 411retry:
28bc106b 412 fio.page = page;
e959c8f5 413 fio.old_blkaddr = NULL_ADDR;
4d978078 414 fio.encrypted_page = NULL;
cc15620b 415 fio.need_lock = LOCK_DONE;
4d57b86d 416 err = f2fs_do_write_data_page(&fio);
29b96b54 417 if (err) {
640cc189 418 if (err == -ENOMEM) {
5df7731f
CY
419 congestion_wait(BLK_RW_ASYNC,
420 DEFAULT_IO_TIMEOUT);
640cc189
JK
421 cond_resched();
422 goto retry;
423 }
28bc106b 424 unlock_page(page);
29b96b54 425 break;
70c640b1 426 }
28bc106b
CY
427 /* record old blkaddr for revoking */
428 cur->old_addr = fio.old_blkaddr;
bab475c5 429 submit_bio = true;
28bc106b
CY
430 }
431 unlock_page(page);
cf52b27a 432 list_move_tail(&cur->list, &revoke_list);
88b88a66 433 }
29b96b54 434
bab475c5
CY
435 if (submit_bio)
436 f2fs_submit_merged_write_cond(sbi, inode, NULL, 0, DATA);
28bc106b 437
cf52b27a
CY
438 if (err) {
439 /*
440 * try to revoke all committed pages, but still we could fail
441 * due to no memory or other reason, if that happened, EAGAIN
442 * will be returned, which means in such case, transaction is
443 * already not integrity, caller should use journal to do the
444 * recovery or rewrite & commit last transaction. For other
445 * error number, revoking was done by filesystem itself.
446 */
48432984
CY
447 err = __revoke_inmem_pages(inode, &revoke_list,
448 false, true, false);
cf52b27a
CY
449
450 /* drop all uncommitted pages */
48432984
CY
451 __revoke_inmem_pages(inode, &fi->inmem_pages,
452 true, false, false);
cf52b27a 453 } else {
48432984
CY
454 __revoke_inmem_pages(inode, &revoke_list,
455 false, false, false);
cf52b27a 456 }
28bc106b 457
29b96b54
CY
458 return err;
459}
460
4d57b86d 461int f2fs_commit_inmem_pages(struct inode *inode)
29b96b54
CY
462{
463 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
464 struct f2fs_inode_info *fi = F2FS_I(inode);
28bc106b 465 int err;
29b96b54
CY
466
467 f2fs_balance_fs(sbi, true);
29b96b54 468
6f8d4455
JK
469 down_write(&fi->i_gc_rwsem[WRITE]);
470
471 f2fs_lock_op(sbi);
5fe45743
CY
472 set_inode_flag(inode, FI_ATOMIC_COMMIT);
473
29b96b54 474 mutex_lock(&fi->inmem_lock);
4d57b86d 475 err = __f2fs_commit_inmem_pages(inode);
88b88a66
JK
476 mutex_unlock(&fi->inmem_lock);
477
5fe45743
CY
478 clear_inode_flag(inode, FI_ATOMIC_COMMIT);
479
29b96b54 480 f2fs_unlock_op(sbi);
6f8d4455
JK
481 up_write(&fi->i_gc_rwsem[WRITE]);
482
edb27dee 483 return err;
88b88a66
JK
484}
485
0a8165d7 486/*
351df4b2
JK
487 * This function balances dirty node and dentry pages.
488 * In addition, it controls garbage collection.
489 */
2c4db1a6 490void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need)
351df4b2 491{
55523519 492 if (time_to_inject(sbi, FAULT_CHECKPOINT)) {
c45d6002 493 f2fs_show_injection_info(sbi, FAULT_CHECKPOINT);
0f348028 494 f2fs_stop_checkpoint(sbi, false);
55523519 495 }
0f348028 496
e589c2c4 497 /* balance_fs_bg is able to be pending */
a7881893 498 if (need && excess_cached_nats(sbi))
7bcd0cfa 499 f2fs_balance_fs_bg(sbi, false);
e589c2c4 500
00e09c0b 501 if (!f2fs_is_checkpoint_ready(sbi))
4354994f
DR
502 return;
503
351df4b2 504 /*
029cd28c
JK
505 * We should do GC or end up with checkpoint, if there are so many dirty
506 * dir/node pages without enough free segments.
351df4b2 507 */
7f3037a5 508 if (has_not_enough_free_secs(sbi, 0, 0)) {
fb24fea7 509 down_write(&sbi->gc_lock);
e066b83c 510 f2fs_gc(sbi, false, false, NULL_SEGNO);
351df4b2
JK
511 }
512}
513
7bcd0cfa 514void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi, bool from_bg)
4660f9c0 515{
64c74a7a
CY
516 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
517 return;
518
1dcc336b 519 /* try to shrink extent cache when there is no enough memory */
4d57b86d 520 if (!f2fs_available_free_memory(sbi, EXTENT_CACHE))
554df79e 521 f2fs_shrink_extent_tree(sbi, EXTENT_CACHE_SHRINK_NUMBER);
1dcc336b 522
1b38dc8e 523 /* check the # of cached NAT entries */
4d57b86d
CY
524 if (!f2fs_available_free_memory(sbi, NAT_ENTRIES))
525 f2fs_try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK);
1b38dc8e 526
4d57b86d
CY
527 if (!f2fs_available_free_memory(sbi, FREE_NIDS))
528 f2fs_try_to_free_nids(sbi, MAX_FREE_NIDS);
ad4edb83 529 else
4d57b86d 530 f2fs_build_free_nids(sbi, false, false);
31696580 531
a7d10cf3 532 if (!is_idle(sbi, REQ_TIME) &&
fd8c8caf 533 (!excess_dirty_nats(sbi) && !excess_dirty_nodes(sbi)))
f455c8a5 534 return;
31696580 535
1b38dc8e 536 /* checkpoint is the only way to shrink partial cached entries */
4d57b86d
CY
537 if (!f2fs_available_free_memory(sbi, NAT_ENTRIES) ||
538 !f2fs_available_free_memory(sbi, INO_ENTRIES) ||
7d768d2c
CY
539 excess_prefree_segs(sbi) ||
540 excess_dirty_nats(sbi) ||
fd8c8caf 541 excess_dirty_nodes(sbi) ||
f455c8a5 542 f2fs_time_over(sbi, CP_TIME)) {
7bcd0cfa 543 if (test_opt(sbi, DATA_FLUSH) && from_bg) {
e9f5b8b8
CY
544 struct blk_plug plug;
545
040d2bb3
CY
546 mutex_lock(&sbi->flush_lock);
547
e9f5b8b8 548 blk_start_plug(&plug);
4d57b86d 549 f2fs_sync_dirty_inodes(sbi, FILE_INODE);
e9f5b8b8 550 blk_finish_plug(&plug);
040d2bb3
CY
551
552 mutex_unlock(&sbi->flush_lock);
e9f5b8b8 553 }
4660f9c0 554 f2fs_sync_fs(sbi->sb, true);
42190d2a 555 stat_inc_bg_cp_count(sbi->stat_info);
36b35a0d 556 }
4660f9c0
JK
557}
558
20fda56b
KM
559static int __submit_flush_wait(struct f2fs_sb_info *sbi,
560 struct block_device *bdev)
3c62be17 561{
dc37910d 562 struct bio *bio;
3c62be17
JK
563 int ret;
564
dc37910d
CY
565 bio = f2fs_bio_alloc(sbi, 0, false);
566 if (!bio)
567 return -ENOMEM;
568
3adc5fcb 569 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH;
74d46992 570 bio_set_dev(bio, bdev);
3c62be17
JK
571 ret = submit_bio_wait(bio);
572 bio_put(bio);
20fda56b
KM
573
574 trace_f2fs_issue_flush(bdev, test_opt(sbi, NOBARRIER),
575 test_opt(sbi, FLUSH_MERGE), ret);
3c62be17
JK
576 return ret;
577}
578
39d787be 579static int submit_flush_wait(struct f2fs_sb_info *sbi, nid_t ino)
3c62be17 580{
39d787be 581 int ret = 0;
3c62be17
JK
582 int i;
583
0916878d 584 if (!f2fs_is_multi_device(sbi))
39d787be 585 return __submit_flush_wait(sbi, sbi->sb->s_bdev);
20fda56b 586
39d787be 587 for (i = 0; i < sbi->s_ndevs; i++) {
4d57b86d 588 if (!f2fs_is_dirty_device(sbi, ino, i, FLUSH_INO))
39d787be 589 continue;
20fda56b
KM
590 ret = __submit_flush_wait(sbi, FDEV(i).bdev);
591 if (ret)
592 break;
3c62be17
JK
593 }
594 return ret;
595}
596
2163d198 597static int issue_flush_thread(void *data)
6b4afdd7
JK
598{
599 struct f2fs_sb_info *sbi = data;
b01a9201 600 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
a688b9d9 601 wait_queue_head_t *q = &fcc->flush_wait_queue;
6b4afdd7
JK
602repeat:
603 if (kthread_should_stop())
604 return 0;
605
dc6febb6
CY
606 sb_start_intwrite(sbi->sb);
607
721bd4d5 608 if (!llist_empty(&fcc->issue_list)) {
6b4afdd7
JK
609 struct flush_cmd *cmd, *next;
610 int ret;
611
721bd4d5
GZ
612 fcc->dispatch_list = llist_del_all(&fcc->issue_list);
613 fcc->dispatch_list = llist_reverse_order(fcc->dispatch_list);
614
39d787be
CY
615 cmd = llist_entry(fcc->dispatch_list, struct flush_cmd, llnode);
616
617 ret = submit_flush_wait(sbi, cmd->ino);
8b8dd65f
CY
618 atomic_inc(&fcc->issued_flush);
619
721bd4d5
GZ
620 llist_for_each_entry_safe(cmd, next,
621 fcc->dispatch_list, llnode) {
6b4afdd7 622 cmd->ret = ret;
6b4afdd7
JK
623 complete(&cmd->wait);
624 }
a688b9d9 625 fcc->dispatch_list = NULL;
6b4afdd7
JK
626 }
627
dc6febb6
CY
628 sb_end_intwrite(sbi->sb);
629
a688b9d9 630 wait_event_interruptible(*q,
721bd4d5 631 kthread_should_stop() || !llist_empty(&fcc->issue_list));
6b4afdd7
JK
632 goto repeat;
633}
634
39d787be 635int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino)
6b4afdd7 636{
b01a9201 637 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
adf8d90b 638 struct flush_cmd cmd;
8b8dd65f 639 int ret;
6b4afdd7 640
0f7b2abd
JK
641 if (test_opt(sbi, NOBARRIER))
642 return 0;
643
8b8dd65f 644 if (!test_opt(sbi, FLUSH_MERGE)) {
72691af6 645 atomic_inc(&fcc->queued_flush);
39d787be 646 ret = submit_flush_wait(sbi, ino);
72691af6 647 atomic_dec(&fcc->queued_flush);
8b8dd65f
CY
648 atomic_inc(&fcc->issued_flush);
649 return ret;
650 }
740432f8 651
0916878d
DLM
652 if (atomic_inc_return(&fcc->queued_flush) == 1 ||
653 f2fs_is_multi_device(sbi)) {
39d787be 654 ret = submit_flush_wait(sbi, ino);
72691af6 655 atomic_dec(&fcc->queued_flush);
8b8dd65f
CY
656
657 atomic_inc(&fcc->issued_flush);
740432f8
JK
658 return ret;
659 }
6b4afdd7 660
39d787be 661 cmd.ino = ino;
adf8d90b 662 init_completion(&cmd.wait);
6b4afdd7 663
721bd4d5 664 llist_add(&cmd.llnode, &fcc->issue_list);
6b4afdd7 665
6f890df0
CY
666 /* update issue_list before we wake up issue_flush thread */
667 smp_mb();
668
669 if (waitqueue_active(&fcc->flush_wait_queue))
a688b9d9 670 wake_up(&fcc->flush_wait_queue);
6b4afdd7 671
5eba8c5d
JK
672 if (fcc->f2fs_issue_flush) {
673 wait_for_completion(&cmd.wait);
72691af6 674 atomic_dec(&fcc->queued_flush);
5eba8c5d 675 } else {
d3238691
CY
676 struct llist_node *list;
677
678 list = llist_del_all(&fcc->issue_list);
679 if (!list) {
680 wait_for_completion(&cmd.wait);
72691af6 681 atomic_dec(&fcc->queued_flush);
d3238691
CY
682 } else {
683 struct flush_cmd *tmp, *next;
684
39d787be 685 ret = submit_flush_wait(sbi, ino);
d3238691
CY
686
687 llist_for_each_entry_safe(tmp, next, list, llnode) {
688 if (tmp == &cmd) {
689 cmd.ret = ret;
72691af6 690 atomic_dec(&fcc->queued_flush);
d3238691
CY
691 continue;
692 }
693 tmp->ret = ret;
694 complete(&tmp->wait);
695 }
696 }
5eba8c5d 697 }
adf8d90b
CY
698
699 return cmd.ret;
6b4afdd7
JK
700}
701
4d57b86d 702int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi)
2163d198
GZ
703{
704 dev_t dev = sbi->sb->s_bdev->bd_dev;
705 struct flush_cmd_control *fcc;
706 int err = 0;
707
b01a9201
JK
708 if (SM_I(sbi)->fcc_info) {
709 fcc = SM_I(sbi)->fcc_info;
d871cd04
YS
710 if (fcc->f2fs_issue_flush)
711 return err;
5eba8c5d
JK
712 goto init_thread;
713 }
714
acbf054d 715 fcc = f2fs_kzalloc(sbi, sizeof(struct flush_cmd_control), GFP_KERNEL);
2163d198
GZ
716 if (!fcc)
717 return -ENOMEM;
8b8dd65f 718 atomic_set(&fcc->issued_flush, 0);
72691af6 719 atomic_set(&fcc->queued_flush, 0);
2163d198 720 init_waitqueue_head(&fcc->flush_wait_queue);
721bd4d5 721 init_llist_head(&fcc->issue_list);
b01a9201 722 SM_I(sbi)->fcc_info = fcc;
d4fdf8ba
YH
723 if (!test_opt(sbi, FLUSH_MERGE))
724 return err;
725
5eba8c5d 726init_thread:
2163d198
GZ
727 fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi,
728 "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev));
729 if (IS_ERR(fcc->f2fs_issue_flush)) {
730 err = PTR_ERR(fcc->f2fs_issue_flush);
5222595d 731 kvfree(fcc);
b01a9201 732 SM_I(sbi)->fcc_info = NULL;
2163d198
GZ
733 return err;
734 }
2163d198
GZ
735
736 return err;
737}
738
4d57b86d 739void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free)
2163d198 740{
b01a9201 741 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
2163d198 742
5eba8c5d
JK
743 if (fcc && fcc->f2fs_issue_flush) {
744 struct task_struct *flush_thread = fcc->f2fs_issue_flush;
745
746 fcc->f2fs_issue_flush = NULL;
747 kthread_stop(flush_thread);
748 }
749 if (free) {
5222595d 750 kvfree(fcc);
b01a9201 751 SM_I(sbi)->fcc_info = NULL;
5eba8c5d 752 }
2163d198
GZ
753}
754
1228b482
CY
755int f2fs_flush_device_cache(struct f2fs_sb_info *sbi)
756{
757 int ret = 0, i;
758
0916878d 759 if (!f2fs_is_multi_device(sbi))
1228b482
CY
760 return 0;
761
762 for (i = 1; i < sbi->s_ndevs; i++) {
763 if (!f2fs_test_bit(i, (char *)&sbi->dirty_device))
764 continue;
765 ret = __submit_flush_wait(sbi, FDEV(i).bdev);
766 if (ret)
767 break;
768
769 spin_lock(&sbi->dev_lock);
770 f2fs_clear_bit(i, (char *)&sbi->dirty_device);
771 spin_unlock(&sbi->dev_lock);
772 }
773
774 return ret;
775}
776
351df4b2
JK
777static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
778 enum dirty_type dirty_type)
779{
780 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
781
782 /* need not be added */
783 if (IS_CURSEG(sbi, segno))
784 return;
785
786 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
787 dirty_i->nr_dirty[dirty_type]++;
788
789 if (dirty_type == DIRTY) {
790 struct seg_entry *sentry = get_seg_entry(sbi, segno);
4625d6aa 791 enum dirty_type t = sentry->type;
b2f2c390 792
ec325b52
JK
793 if (unlikely(t >= DIRTY)) {
794 f2fs_bug_on(sbi, 1);
795 return;
796 }
4625d6aa
CL
797 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
798 dirty_i->nr_dirty[t]++;
da52f8ad
JQ
799
800 if (__is_large_section(sbi)) {
801 unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
123aaf77 802 block_t valid_blocks =
da52f8ad
JQ
803 get_valid_blocks(sbi, segno, true);
804
805 f2fs_bug_on(sbi, unlikely(!valid_blocks ||
806 valid_blocks == BLKS_PER_SEC(sbi)));
807
808 if (!IS_CURSEC(sbi, secno))
809 set_bit(secno, dirty_i->dirty_secmap);
810 }
351df4b2
JK
811 }
812}
813
814static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
815 enum dirty_type dirty_type)
816{
817 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
123aaf77 818 block_t valid_blocks;
351df4b2
JK
819
820 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
821 dirty_i->nr_dirty[dirty_type]--;
822
823 if (dirty_type == DIRTY) {
4625d6aa
CL
824 struct seg_entry *sentry = get_seg_entry(sbi, segno);
825 enum dirty_type t = sentry->type;
826
827 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
828 dirty_i->nr_dirty[t]--;
b2f2c390 829
da52f8ad
JQ
830 valid_blocks = get_valid_blocks(sbi, segno, true);
831 if (valid_blocks == 0) {
4ddb1a4d 832 clear_bit(GET_SEC_FROM_SEG(sbi, segno),
5ec4e49f 833 dirty_i->victim_secmap);
bbf9f7d9
ST
834#ifdef CONFIG_F2FS_CHECK_FS
835 clear_bit(segno, SIT_I(sbi)->invalid_segmap);
836#endif
837 }
da52f8ad
JQ
838 if (__is_large_section(sbi)) {
839 unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
840
841 if (!valid_blocks ||
842 valid_blocks == BLKS_PER_SEC(sbi)) {
843 clear_bit(secno, dirty_i->dirty_secmap);
844 return;
845 }
846
847 if (!IS_CURSEC(sbi, secno))
848 set_bit(secno, dirty_i->dirty_secmap);
849 }
351df4b2
JK
850 }
851}
852
0a8165d7 853/*
351df4b2
JK
854 * Should not occur error such as -ENOMEM.
855 * Adding dirty entry into seglist is not critical operation.
856 * If a given segment is one of current working segments, it won't be added.
857 */
8d8451af 858static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
351df4b2
JK
859{
860 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
4354994f 861 unsigned short valid_blocks, ckpt_valid_blocks;
de881df9 862 unsigned int usable_blocks;
351df4b2
JK
863
864 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
865 return;
866
de881df9 867 usable_blocks = f2fs_usable_blks_in_seg(sbi, segno);
351df4b2
JK
868 mutex_lock(&dirty_i->seglist_lock);
869
302bd348 870 valid_blocks = get_valid_blocks(sbi, segno, false);
4354994f 871 ckpt_valid_blocks = get_ckpt_valid_blocks(sbi, segno);
351df4b2 872
4354994f 873 if (valid_blocks == 0 && (!is_sbi_flag_set(sbi, SBI_CP_DISABLED) ||
de881df9 874 ckpt_valid_blocks == usable_blocks)) {
351df4b2
JK
875 __locate_dirty_segment(sbi, segno, PRE);
876 __remove_dirty_segment(sbi, segno, DIRTY);
de881df9 877 } else if (valid_blocks < usable_blocks) {
351df4b2
JK
878 __locate_dirty_segment(sbi, segno, DIRTY);
879 } else {
880 /* Recovery routine with SSR needs this */
881 __remove_dirty_segment(sbi, segno, DIRTY);
882 }
883
884 mutex_unlock(&dirty_i->seglist_lock);
351df4b2
JK
885}
886
4354994f
DR
887/* This moves currently empty dirty blocks to prefree. Must hold seglist_lock */
888void f2fs_dirty_to_prefree(struct f2fs_sb_info *sbi)
889{
890 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
891 unsigned int segno;
892
893 mutex_lock(&dirty_i->seglist_lock);
894 for_each_set_bit(segno, dirty_i->dirty_segmap[DIRTY], MAIN_SEGS(sbi)) {
895 if (get_valid_blocks(sbi, segno, false))
896 continue;
897 if (IS_CURSEG(sbi, segno))
898 continue;
899 __locate_dirty_segment(sbi, segno, PRE);
900 __remove_dirty_segment(sbi, segno, DIRTY);
901 }
902 mutex_unlock(&dirty_i->seglist_lock);
903}
904
4d3aed70 905block_t f2fs_get_unusable_blocks(struct f2fs_sb_info *sbi)
4354994f 906{
ae4ad7ea
DR
907 int ovp_hole_segs =
908 (overprovision_segments(sbi) - reserved_segments(sbi));
909 block_t ovp_holes = ovp_hole_segs << sbi->log_blocks_per_seg;
4d3aed70 910 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
4354994f 911 block_t holes[2] = {0, 0}; /* DATA and NODE */
4d3aed70 912 block_t unusable;
4354994f
DR
913 struct seg_entry *se;
914 unsigned int segno;
915
916 mutex_lock(&dirty_i->seglist_lock);
917 for_each_set_bit(segno, dirty_i->dirty_segmap[DIRTY], MAIN_SEGS(sbi)) {
918 se = get_seg_entry(sbi, segno);
919 if (IS_NODESEG(se->type))
de881df9
AR
920 holes[NODE] += f2fs_usable_blks_in_seg(sbi, segno) -
921 se->valid_blocks;
4354994f 922 else
de881df9
AR
923 holes[DATA] += f2fs_usable_blks_in_seg(sbi, segno) -
924 se->valid_blocks;
4354994f
DR
925 }
926 mutex_unlock(&dirty_i->seglist_lock);
927
4d3aed70
DR
928 unusable = holes[DATA] > holes[NODE] ? holes[DATA] : holes[NODE];
929 if (unusable > ovp_holes)
930 return unusable - ovp_holes;
931 return 0;
932}
933
934int f2fs_disable_cp_again(struct f2fs_sb_info *sbi, block_t unusable)
935{
936 int ovp_hole_segs =
937 (overprovision_segments(sbi) - reserved_segments(sbi));
938 if (unusable > F2FS_OPTION(sbi).unusable_cap)
4354994f 939 return -EAGAIN;
db610a64 940 if (is_sbi_flag_set(sbi, SBI_CP_DISABLED_QUICK) &&
ae4ad7ea 941 dirty_segments(sbi) > ovp_hole_segs)
db610a64 942 return -EAGAIN;
4354994f
DR
943 return 0;
944}
945
946/* This is only used by SBI_CP_DISABLED */
947static unsigned int get_free_segment(struct f2fs_sb_info *sbi)
948{
949 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
950 unsigned int segno = 0;
951
952 mutex_lock(&dirty_i->seglist_lock);
953 for_each_set_bit(segno, dirty_i->dirty_segmap[DIRTY], MAIN_SEGS(sbi)) {
954 if (get_valid_blocks(sbi, segno, false))
955 continue;
956 if (get_ckpt_valid_blocks(sbi, segno))
957 continue;
958 mutex_unlock(&dirty_i->seglist_lock);
959 return segno;
960 }
961 mutex_unlock(&dirty_i->seglist_lock);
962 return NULL_SEGNO;
963}
964
004b6862 965static struct discard_cmd *__create_discard_cmd(struct f2fs_sb_info *sbi,
c81abe34
JK
966 struct block_device *bdev, block_t lstart,
967 block_t start, block_t len)
275b66b0 968{
0b54fb84 969 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
ba48a33e 970 struct list_head *pend_list;
b01a9201 971 struct discard_cmd *dc;
275b66b0 972
ba48a33e
CY
973 f2fs_bug_on(sbi, !len);
974
975 pend_list = &dcc->pend_list[plist_idx(len)];
976
b01a9201
JK
977 dc = f2fs_kmem_cache_alloc(discard_cmd_slab, GFP_NOFS);
978 INIT_LIST_HEAD(&dc->list);
c81abe34 979 dc->bdev = bdev;
b01a9201 980 dc->lstart = lstart;
c81abe34 981 dc->start = start;
b01a9201 982 dc->len = len;
ec9895ad 983 dc->ref = 0;
15469963 984 dc->state = D_PREP;
72691af6 985 dc->queued = 0;
c81abe34 986 dc->error = 0;
b01a9201 987 init_completion(&dc->wait);
22d375dd 988 list_add_tail(&dc->list, pend_list);
35ec7d57
CY
989 spin_lock_init(&dc->lock);
990 dc->bio_ref = 0;
5f32366a 991 atomic_inc(&dcc->discard_cmd_cnt);
d84d1cbd 992 dcc->undiscard_blks += len;
004b6862
CY
993
994 return dc;
995}
996
997static struct discard_cmd *__attach_discard_cmd(struct f2fs_sb_info *sbi,
998 struct block_device *bdev, block_t lstart,
999 block_t start, block_t len,
4dada3fd
CY
1000 struct rb_node *parent, struct rb_node **p,
1001 bool leftmost)
004b6862
CY
1002{
1003 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1004 struct discard_cmd *dc;
1005
1006 dc = __create_discard_cmd(sbi, bdev, lstart, start, len);
1007
1008 rb_link_node(&dc->rb_node, parent, p);
4dada3fd 1009 rb_insert_color_cached(&dc->rb_node, &dcc->root, leftmost);
004b6862
CY
1010
1011 return dc;
15469963
JK
1012}
1013
004b6862
CY
1014static void __detach_discard_cmd(struct discard_cmd_control *dcc,
1015 struct discard_cmd *dc)
15469963 1016{
dcc9165d 1017 if (dc->state == D_DONE)
72691af6 1018 atomic_sub(dc->queued, &dcc->queued_discard);
004b6862
CY
1019
1020 list_del(&dc->list);
4dada3fd 1021 rb_erase_cached(&dc->rb_node, &dcc->root);
d84d1cbd 1022 dcc->undiscard_blks -= dc->len;
004b6862
CY
1023
1024 kmem_cache_free(discard_cmd_slab, dc);
1025
1026 atomic_dec(&dcc->discard_cmd_cnt);
1027}
1028
1029static void __remove_discard_cmd(struct f2fs_sb_info *sbi,
1030 struct discard_cmd *dc)
1031{
1032 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
35ec7d57 1033 unsigned long flags;
dcc9165d 1034
2ec6f2ef
CY
1035 trace_f2fs_remove_discard(dc->bdev, dc->start, dc->len);
1036
35ec7d57
CY
1037 spin_lock_irqsave(&dc->lock, flags);
1038 if (dc->bio_ref) {
1039 spin_unlock_irqrestore(&dc->lock, flags);
1040 return;
1041 }
1042 spin_unlock_irqrestore(&dc->lock, flags);
1043
d9703d90
CY
1044 f2fs_bug_on(sbi, dc->ref);
1045
c81abe34
JK
1046 if (dc->error == -EOPNOTSUPP)
1047 dc->error = 0;
15469963 1048
c81abe34 1049 if (dc->error)
22d7ea13 1050 printk_ratelimited(
c45d6002
CY
1051 "%sF2FS-fs (%s): Issue discard(%u, %u, %u) failed, ret: %d",
1052 KERN_INFO, sbi->sb->s_id,
1053 dc->lstart, dc->start, dc->len, dc->error);
004b6862 1054 __detach_discard_cmd(dcc, dc);
275b66b0
CY
1055}
1056
c81abe34
JK
1057static void f2fs_submit_discard_endio(struct bio *bio)
1058{
1059 struct discard_cmd *dc = (struct discard_cmd *)bio->bi_private;
35ec7d57 1060 unsigned long flags;
c81abe34 1061
35ec7d57 1062 spin_lock_irqsave(&dc->lock, flags);
3fa6a8c5
ST
1063 if (!dc->error)
1064 dc->error = blk_status_to_errno(bio->bi_status);
35ec7d57
CY
1065 dc->bio_ref--;
1066 if (!dc->bio_ref && dc->state == D_SUBMIT) {
1067 dc->state = D_DONE;
1068 complete_all(&dc->wait);
1069 }
1070 spin_unlock_irqrestore(&dc->lock, flags);
c81abe34
JK
1071 bio_put(bio);
1072}
1073
94b1e10e 1074static void __check_sit_bitmap(struct f2fs_sb_info *sbi,
6915ea9d
CY
1075 block_t start, block_t end)
1076{
1077#ifdef CONFIG_F2FS_CHECK_FS
1078 struct seg_entry *sentry;
1079 unsigned int segno;
1080 block_t blk = start;
1081 unsigned long offset, size, max_blocks = sbi->blocks_per_seg;
1082 unsigned long *map;
1083
1084 while (blk < end) {
1085 segno = GET_SEGNO(sbi, blk);
1086 sentry = get_seg_entry(sbi, segno);
1087 offset = GET_BLKOFF_FROM_SEG0(sbi, blk);
1088
008396e1
YS
1089 if (end < START_BLOCK(sbi, segno + 1))
1090 size = GET_BLKOFF_FROM_SEG0(sbi, end);
1091 else
1092 size = max_blocks;
6915ea9d
CY
1093 map = (unsigned long *)(sentry->cur_valid_map);
1094 offset = __find_rev_next_bit(map, size, offset);
1095 f2fs_bug_on(sbi, offset != size);
008396e1 1096 blk = START_BLOCK(sbi, segno + 1);
6915ea9d
CY
1097 }
1098#endif
1099}
1100
8bb4f253
JK
1101static void __init_discard_policy(struct f2fs_sb_info *sbi,
1102 struct discard_policy *dpolicy,
1103 int discard_type, unsigned int granularity)
1104{
1105 /* common policy */
1106 dpolicy->type = discard_type;
1107 dpolicy->sync = true;
20ee4382 1108 dpolicy->ordered = false;
8bb4f253
JK
1109 dpolicy->granularity = granularity;
1110
1111 dpolicy->max_requests = DEF_MAX_DISCARD_REQUEST;
1112 dpolicy->io_aware_gran = MAX_PLIST_NUM;
6ce48b0c 1113 dpolicy->timeout = false;
8bb4f253
JK
1114
1115 if (discard_type == DPOLICY_BG) {
1116 dpolicy->min_interval = DEF_MIN_DISCARD_ISSUE_TIME;
f9d1dced 1117 dpolicy->mid_interval = DEF_MID_DISCARD_ISSUE_TIME;
8bb4f253
JK
1118 dpolicy->max_interval = DEF_MAX_DISCARD_ISSUE_TIME;
1119 dpolicy->io_aware = true;
cba60849 1120 dpolicy->sync = false;
20ee4382 1121 dpolicy->ordered = true;
8bb4f253
JK
1122 if (utilization(sbi) > DEF_DISCARD_URGENT_UTIL) {
1123 dpolicy->granularity = 1;
1124 dpolicy->max_interval = DEF_MIN_DISCARD_ISSUE_TIME;
1125 }
1126 } else if (discard_type == DPOLICY_FORCE) {
1127 dpolicy->min_interval = DEF_MIN_DISCARD_ISSUE_TIME;
f9d1dced 1128 dpolicy->mid_interval = DEF_MID_DISCARD_ISSUE_TIME;
8bb4f253
JK
1129 dpolicy->max_interval = DEF_MAX_DISCARD_ISSUE_TIME;
1130 dpolicy->io_aware = false;
1131 } else if (discard_type == DPOLICY_FSTRIM) {
1132 dpolicy->io_aware = false;
1133 } else if (discard_type == DPOLICY_UMOUNT) {
1134 dpolicy->io_aware = false;
b8623253
JK
1135 /* we need to issue all to keep CP_TRIMMED_FLAG */
1136 dpolicy->granularity = 1;
6ce48b0c 1137 dpolicy->timeout = true;
8bb4f253
JK
1138 }
1139}
1140
35ec7d57
CY
1141static void __update_discard_tree_range(struct f2fs_sb_info *sbi,
1142 struct block_device *bdev, block_t lstart,
1143 block_t start, block_t len);
c81abe34 1144/* this function is copied from blkdev_issue_discard from block/blk-lib.c */
6b9cb124 1145static int __submit_discard_cmd(struct f2fs_sb_info *sbi,
78997b56 1146 struct discard_policy *dpolicy,
35ec7d57
CY
1147 struct discard_cmd *dc,
1148 unsigned int *issued)
c81abe34 1149{
35ec7d57
CY
1150 struct block_device *bdev = dc->bdev;
1151 struct request_queue *q = bdev_get_queue(bdev);
1152 unsigned int max_discard_blocks =
1153 SECTOR_TO_BLOCK(q->limits.max_discard_sectors);
c81abe34 1154 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
78997b56
CY
1155 struct list_head *wait_list = (dpolicy->type == DPOLICY_FSTRIM) ?
1156 &(dcc->fstrim_list) : &(dcc->wait_list);
78997b56 1157 int flag = dpolicy->sync ? REQ_SYNC : 0;
35ec7d57
CY
1158 block_t lstart, start, len, total_len;
1159 int err = 0;
c81abe34
JK
1160
1161 if (dc->state != D_PREP)
6b9cb124 1162 return 0;
c81abe34 1163
d6184774 1164 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK))
6b9cb124 1165 return 0;
d6184774 1166
35ec7d57
CY
1167 trace_f2fs_issue_discard(bdev, dc->start, dc->len);
1168
1169 lstart = dc->lstart;
1170 start = dc->start;
1171 len = dc->len;
1172 total_len = len;
1173
1174 dc->len = 0;
1175
1176 while (total_len && *issued < dpolicy->max_requests && !err) {
1177 struct bio *bio = NULL;
1178 unsigned long flags;
1179 bool last = true;
1180
1181 if (len > max_discard_blocks) {
1182 len = max_discard_blocks;
1183 last = false;
1184 }
1185
1186 (*issued)++;
1187 if (*issued == dpolicy->max_requests)
1188 last = true;
1189
1190 dc->len += len;
1191
b83dcfe6 1192 if (time_to_inject(sbi, FAULT_DISCARD)) {
c45d6002 1193 f2fs_show_injection_info(sbi, FAULT_DISCARD);
b83dcfe6
CY
1194 err = -EIO;
1195 goto submit;
1196 }
35ec7d57
CY
1197 err = __blkdev_issue_discard(bdev,
1198 SECTOR_FROM_BLOCK(start),
1199 SECTOR_FROM_BLOCK(len),
1200 GFP_NOFS, 0, &bio);
b83dcfe6 1201submit:
6b9cb124 1202 if (err) {
35ec7d57 1203 spin_lock_irqsave(&dc->lock, flags);
6b9cb124 1204 if (dc->state == D_PARTIAL)
35ec7d57 1205 dc->state = D_SUBMIT;
35ec7d57
CY
1206 spin_unlock_irqrestore(&dc->lock, flags);
1207
6b9cb124
CY
1208 break;
1209 }
35ec7d57 1210
6b9cb124 1211 f2fs_bug_on(sbi, !bio);
35ec7d57 1212
6b9cb124
CY
1213 /*
1214 * should keep before submission to avoid D_DONE
1215 * right away
1216 */
1217 spin_lock_irqsave(&dc->lock, flags);
1218 if (last)
1219 dc->state = D_SUBMIT;
1220 else
1221 dc->state = D_PARTIAL;
1222 dc->bio_ref++;
1223 spin_unlock_irqrestore(&dc->lock, flags);
35ec7d57 1224
72691af6
JK
1225 atomic_inc(&dcc->queued_discard);
1226 dc->queued++;
6b9cb124 1227 list_move_tail(&dc->list, wait_list);
b0af6d49 1228
6b9cb124 1229 /* sanity check on discard range */
9249dded 1230 __check_sit_bitmap(sbi, lstart, lstart + len);
35ec7d57 1231
6b9cb124
CY
1232 bio->bi_private = dc;
1233 bio->bi_end_io = f2fs_submit_discard_endio;
1234 bio->bi_opf |= flag;
1235 submit_bio(bio);
1236
1237 atomic_inc(&dcc->issued_discard);
1238
1239 f2fs_update_iostat(sbi, FS_DISCARD, 1);
35ec7d57
CY
1240
1241 lstart += len;
1242 start += len;
1243 total_len -= len;
1244 len = total_len;
c81abe34 1245 }
35ec7d57 1246
df423399
ST
1247 if (!err && len) {
1248 dcc->undiscard_blks -= len;
35ec7d57 1249 __update_discard_tree_range(sbi, bdev, lstart, start, len);
df423399 1250 }
6b9cb124 1251 return err;
c81abe34
JK
1252}
1253
47d0d7d7 1254static void __insert_discard_tree(struct f2fs_sb_info *sbi,
004b6862
CY
1255 struct block_device *bdev, block_t lstart,
1256 block_t start, block_t len,
1257 struct rb_node **insert_p,
1258 struct rb_node *insert_parent)
c81abe34 1259{
004b6862 1260 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
dca6951f 1261 struct rb_node **p;
004b6862 1262 struct rb_node *parent = NULL;
4dada3fd 1263 bool leftmost = true;
004b6862
CY
1264
1265 if (insert_p && insert_parent) {
1266 parent = insert_parent;
1267 p = insert_p;
1268 goto do_insert;
1269 }
c81abe34 1270
4dada3fd
CY
1271 p = f2fs_lookup_rb_tree_for_insert(sbi, &dcc->root, &parent,
1272 lstart, &leftmost);
004b6862 1273do_insert:
47d0d7d7 1274 __attach_discard_cmd(sbi, bdev, lstart, start, len, parent,
4dada3fd 1275 p, leftmost);
c81abe34
JK
1276}
1277
ba48a33e
CY
1278static void __relocate_discard_cmd(struct discard_cmd_control *dcc,
1279 struct discard_cmd *dc)
1280{
1281 list_move_tail(&dc->list, &dcc->pend_list[plist_idx(dc->len)]);
1282}
1283
3d6a650f
YH
1284static void __punch_discard_cmd(struct f2fs_sb_info *sbi,
1285 struct discard_cmd *dc, block_t blkaddr)
1286{
ba48a33e 1287 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
004b6862
CY
1288 struct discard_info di = dc->di;
1289 bool modified = false;
3d6a650f 1290
004b6862 1291 if (dc->state == D_DONE || dc->len == 1) {
3d6a650f
YH
1292 __remove_discard_cmd(sbi, dc);
1293 return;
1294 }
1295
d84d1cbd
CY
1296 dcc->undiscard_blks -= di.len;
1297
004b6862 1298 if (blkaddr > di.lstart) {
3d6a650f 1299 dc->len = blkaddr - dc->lstart;
d84d1cbd 1300 dcc->undiscard_blks += dc->len;
ba48a33e 1301 __relocate_discard_cmd(dcc, dc);
004b6862
CY
1302 modified = true;
1303 }
1304
1305 if (blkaddr < di.lstart + di.len - 1) {
1306 if (modified) {
1307 __insert_discard_tree(sbi, dc->bdev, blkaddr + 1,
1308 di.start + blkaddr + 1 - di.lstart,
1309 di.lstart + di.len - 1 - blkaddr,
1310 NULL, NULL);
1311 } else {
1312 dc->lstart++;
1313 dc->len--;
1314 dc->start++;
d84d1cbd 1315 dcc->undiscard_blks += dc->len;
ba48a33e 1316 __relocate_discard_cmd(dcc, dc);
004b6862 1317 }
3d6a650f
YH
1318 }
1319}
1320
004b6862
CY
1321static void __update_discard_tree_range(struct f2fs_sb_info *sbi,
1322 struct block_device *bdev, block_t lstart,
1323 block_t start, block_t len)
275b66b0 1324{
0b54fb84 1325 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
004b6862
CY
1326 struct discard_cmd *prev_dc = NULL, *next_dc = NULL;
1327 struct discard_cmd *dc;
1328 struct discard_info di = {0};
1329 struct rb_node **insert_p = NULL, *insert_parent = NULL;
35ec7d57
CY
1330 struct request_queue *q = bdev_get_queue(bdev);
1331 unsigned int max_discard_blocks =
1332 SECTOR_TO_BLOCK(q->limits.max_discard_sectors);
004b6862 1333 block_t end = lstart + len;
275b66b0 1334
4d57b86d 1335 dc = (struct discard_cmd *)f2fs_lookup_rb_tree_ret(&dcc->root,
004b6862
CY
1336 NULL, lstart,
1337 (struct rb_entry **)&prev_dc,
1338 (struct rb_entry **)&next_dc,
4dada3fd 1339 &insert_p, &insert_parent, true, NULL);
004b6862
CY
1340 if (dc)
1341 prev_dc = dc;
1342
1343 if (!prev_dc) {
1344 di.lstart = lstart;
1345 di.len = next_dc ? next_dc->lstart - lstart : len;
1346 di.len = min(di.len, len);
1347 di.start = start;
22d375dd 1348 }
15469963 1349
004b6862
CY
1350 while (1) {
1351 struct rb_node *node;
1352 bool merged = false;
1353 struct discard_cmd *tdc = NULL;
1354
1355 if (prev_dc) {
1356 di.lstart = prev_dc->lstart + prev_dc->len;
1357 if (di.lstart < lstart)
1358 di.lstart = lstart;
1359 if (di.lstart >= end)
1360 break;
1361
1362 if (!next_dc || next_dc->lstart > end)
1363 di.len = end - di.lstart;
1364 else
1365 di.len = next_dc->lstart - di.lstart;
1366 di.start = start + di.lstart - lstart;
1367 }
1368
1369 if (!di.len)
1370 goto next;
1371
1372 if (prev_dc && prev_dc->state == D_PREP &&
1373 prev_dc->bdev == bdev &&
35ec7d57
CY
1374 __is_discard_back_mergeable(&di, &prev_dc->di,
1375 max_discard_blocks)) {
004b6862 1376 prev_dc->di.len += di.len;
d84d1cbd 1377 dcc->undiscard_blks += di.len;
ba48a33e 1378 __relocate_discard_cmd(dcc, prev_dc);
004b6862
CY
1379 di = prev_dc->di;
1380 tdc = prev_dc;
1381 merged = true;
1382 }
1383
1384 if (next_dc && next_dc->state == D_PREP &&
1385 next_dc->bdev == bdev &&
35ec7d57
CY
1386 __is_discard_front_mergeable(&di, &next_dc->di,
1387 max_discard_blocks)) {
004b6862
CY
1388 next_dc->di.lstart = di.lstart;
1389 next_dc->di.len += di.len;
1390 next_dc->di.start = di.start;
d84d1cbd 1391 dcc->undiscard_blks += di.len;
ba48a33e 1392 __relocate_discard_cmd(dcc, next_dc);
004b6862
CY
1393 if (tdc)
1394 __remove_discard_cmd(sbi, tdc);
004b6862 1395 merged = true;
4e6a8d9b 1396 }
004b6862 1397
df0f6b44 1398 if (!merged) {
004b6862
CY
1399 __insert_discard_tree(sbi, bdev, di.lstart, di.start,
1400 di.len, NULL, NULL);
df0f6b44 1401 }
004b6862
CY
1402 next:
1403 prev_dc = next_dc;
1404 if (!prev_dc)
1405 break;
1406
1407 node = rb_next(&prev_dc->rb_node);
1408 next_dc = rb_entry_safe(node, struct discard_cmd, rb_node);
1409 }
004b6862
CY
1410}
1411
1412static int __queue_discard_cmd(struct f2fs_sb_info *sbi,
1413 struct block_device *bdev, block_t blkstart, block_t blklen)
1414{
1415 block_t lblkstart = blkstart;
1416
7f3d7719
DLM
1417 if (!f2fs_bdev_support_discard(bdev))
1418 return 0;
1419
0243a5f9 1420 trace_f2fs_queue_discard(bdev, blkstart, blklen);
004b6862 1421
0916878d 1422 if (f2fs_is_multi_device(sbi)) {
004b6862
CY
1423 int devi = f2fs_target_device_index(sbi, blkstart);
1424
1425 blkstart -= FDEV(devi).start_blk;
1426 }
35ec7d57 1427 mutex_lock(&SM_I(sbi)->dcc_info->cmd_lock);
004b6862 1428 __update_discard_tree_range(sbi, bdev, lblkstart, blkstart, blklen);
35ec7d57 1429 mutex_unlock(&SM_I(sbi)->dcc_info->cmd_lock);
004b6862
CY
1430 return 0;
1431}
1432
20ee4382
CY
1433static unsigned int __issue_discard_cmd_orderly(struct f2fs_sb_info *sbi,
1434 struct discard_policy *dpolicy)
1435{
1436 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1437 struct discard_cmd *prev_dc = NULL, *next_dc = NULL;
1438 struct rb_node **insert_p = NULL, *insert_parent = NULL;
1439 struct discard_cmd *dc;
1440 struct blk_plug plug;
1441 unsigned int pos = dcc->next_pos;
1442 unsigned int issued = 0;
1443 bool io_interrupted = false;
1444
1445 mutex_lock(&dcc->cmd_lock);
1446 dc = (struct discard_cmd *)f2fs_lookup_rb_tree_ret(&dcc->root,
1447 NULL, pos,
1448 (struct rb_entry **)&prev_dc,
1449 (struct rb_entry **)&next_dc,
4dada3fd 1450 &insert_p, &insert_parent, true, NULL);
20ee4382
CY
1451 if (!dc)
1452 dc = next_dc;
1453
1454 blk_start_plug(&plug);
1455
1456 while (dc) {
1457 struct rb_node *node;
6b9cb124 1458 int err = 0;
20ee4382
CY
1459
1460 if (dc->state != D_PREP)
1461 goto next;
1462
a7d10cf3 1463 if (dpolicy->io_aware && !is_idle(sbi, DISCARD_TIME)) {
20ee4382
CY
1464 io_interrupted = true;
1465 break;
1466 }
1467
1468 dcc->next_pos = dc->lstart + dc->len;
6b9cb124 1469 err = __submit_discard_cmd(sbi, dpolicy, dc, &issued);
20ee4382 1470
35ec7d57 1471 if (issued >= dpolicy->max_requests)
20ee4382
CY
1472 break;
1473next:
1474 node = rb_next(&dc->rb_node);
6b9cb124
CY
1475 if (err)
1476 __remove_discard_cmd(sbi, dc);
20ee4382
CY
1477 dc = rb_entry_safe(node, struct discard_cmd, rb_node);
1478 }
1479
1480 blk_finish_plug(&plug);
1481
1482 if (!dc)
1483 dcc->next_pos = 0;
1484
1485 mutex_unlock(&dcc->cmd_lock);
1486
1487 if (!issued && io_interrupted)
1488 issued = -1;
1489
1490 return issued;
1491}
141af6ba
ST
1492static unsigned int __wait_all_discard_cmd(struct f2fs_sb_info *sbi,
1493 struct discard_policy *dpolicy);
20ee4382 1494
78997b56
CY
1495static int __issue_discard_cmd(struct f2fs_sb_info *sbi,
1496 struct discard_policy *dpolicy)
bd5b0738
CY
1497{
1498 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1499 struct list_head *pend_list;
1500 struct discard_cmd *dc, *tmp;
1501 struct blk_plug plug;
141af6ba 1502 int i, issued;
e6c6de18 1503 bool io_interrupted = false;
bd5b0738 1504
6ce48b0c
CY
1505 if (dpolicy->timeout)
1506 f2fs_update_time(sbi, UMOUNT_DISCARD_TIMEOUT);
03f2c02d 1507
141af6ba
ST
1508retry:
1509 issued = 0;
78997b56 1510 for (i = MAX_PLIST_NUM - 1; i >= 0; i--) {
6ce48b0c
CY
1511 if (dpolicy->timeout &&
1512 f2fs_time_over(sbi, UMOUNT_DISCARD_TIMEOUT))
03f2c02d
JK
1513 break;
1514
78997b56
CY
1515 if (i + 1 < dpolicy->granularity)
1516 break;
20ee4382
CY
1517
1518 if (i < DEFAULT_DISCARD_GRANULARITY && dpolicy->ordered)
1519 return __issue_discard_cmd_orderly(sbi, dpolicy);
1520
bd5b0738 1521 pend_list = &dcc->pend_list[i];
33da62cf
CY
1522
1523 mutex_lock(&dcc->cmd_lock);
49c60c67
CY
1524 if (list_empty(pend_list))
1525 goto next;
67fce70b
CY
1526 if (unlikely(dcc->rbtree_check))
1527 f2fs_bug_on(sbi, !f2fs_check_rb_tree_consistence(sbi,
1528 &dcc->root));
33da62cf 1529 blk_start_plug(&plug);
bd5b0738
CY
1530 list_for_each_entry_safe(dc, tmp, pend_list, list) {
1531 f2fs_bug_on(sbi, dc->state != D_PREP);
1532
6ce48b0c
CY
1533 if (dpolicy->timeout &&
1534 f2fs_time_over(sbi, UMOUNT_DISCARD_TIMEOUT))
6e0cd4a9
HX
1535 break;
1536
ecc9aa00 1537 if (dpolicy->io_aware && i < dpolicy->io_aware_gran &&
a7d10cf3 1538 !is_idle(sbi, DISCARD_TIME)) {
e6c6de18 1539 io_interrupted = true;
522d1711 1540 break;
969d1b18 1541 }
e6c6de18 1542
35ec7d57 1543 __submit_discard_cmd(sbi, dpolicy, dc, &issued);
522d1711 1544
35ec7d57 1545 if (issued >= dpolicy->max_requests)
33da62cf 1546 break;
bd5b0738 1547 }
33da62cf 1548 blk_finish_plug(&plug);
49c60c67 1549next:
33da62cf
CY
1550 mutex_unlock(&dcc->cmd_lock);
1551
522d1711 1552 if (issued >= dpolicy->max_requests || io_interrupted)
33da62cf 1553 break;
bd5b0738 1554 }
969d1b18 1555
141af6ba
ST
1556 if (dpolicy->type == DPOLICY_UMOUNT && issued) {
1557 __wait_all_discard_cmd(sbi, dpolicy);
1558 goto retry;
1559 }
1560
e6c6de18
CY
1561 if (!issued && io_interrupted)
1562 issued = -1;
1563
969d1b18
CY
1564 return issued;
1565}
1566
cf5c759f 1567static bool __drop_discard_cmd(struct f2fs_sb_info *sbi)
969d1b18
CY
1568{
1569 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1570 struct list_head *pend_list;
1571 struct discard_cmd *dc, *tmp;
1572 int i;
cf5c759f 1573 bool dropped = false;
969d1b18
CY
1574
1575 mutex_lock(&dcc->cmd_lock);
1576 for (i = MAX_PLIST_NUM - 1; i >= 0; i--) {
1577 pend_list = &dcc->pend_list[i];
1578 list_for_each_entry_safe(dc, tmp, pend_list, list) {
1579 f2fs_bug_on(sbi, dc->state != D_PREP);
1580 __remove_discard_cmd(sbi, dc);
cf5c759f 1581 dropped = true;
969d1b18
CY
1582 }
1583 }
1584 mutex_unlock(&dcc->cmd_lock);
cf5c759f
CY
1585
1586 return dropped;
bd5b0738
CY
1587}
1588
4d57b86d 1589void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi)
7950e9ac
CY
1590{
1591 __drop_discard_cmd(sbi);
1592}
1593
0ea80512 1594static unsigned int __wait_one_discard_bio(struct f2fs_sb_info *sbi,
2a510c00
CY
1595 struct discard_cmd *dc)
1596{
1597 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
0ea80512 1598 unsigned int len = 0;
2a510c00
CY
1599
1600 wait_for_completion_io(&dc->wait);
1601 mutex_lock(&dcc->cmd_lock);
1602 f2fs_bug_on(sbi, dc->state != D_DONE);
1603 dc->ref--;
0ea80512
CY
1604 if (!dc->ref) {
1605 if (!dc->error)
1606 len = dc->len;
2a510c00 1607 __remove_discard_cmd(sbi, dc);
0ea80512 1608 }
2a510c00 1609 mutex_unlock(&dcc->cmd_lock);
0ea80512
CY
1610
1611 return len;
2a510c00
CY
1612}
1613
0ea80512 1614static unsigned int __wait_discard_cmd_range(struct f2fs_sb_info *sbi,
78997b56
CY
1615 struct discard_policy *dpolicy,
1616 block_t start, block_t end)
63a94fa1
CY
1617{
1618 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
78997b56
CY
1619 struct list_head *wait_list = (dpolicy->type == DPOLICY_FSTRIM) ?
1620 &(dcc->fstrim_list) : &(dcc->wait_list);
63a94fa1 1621 struct discard_cmd *dc, *tmp;
6afae633 1622 bool need_wait;
0ea80512 1623 unsigned int trimmed = 0;
6afae633
CY
1624
1625next:
1626 need_wait = false;
63a94fa1
CY
1627
1628 mutex_lock(&dcc->cmd_lock);
1629 list_for_each_entry_safe(dc, tmp, wait_list, list) {
8412663d
CY
1630 if (dc->lstart + dc->len <= start || end <= dc->lstart)
1631 continue;
78997b56 1632 if (dc->len < dpolicy->granularity)
8412663d 1633 continue;
78997b56 1634 if (dc->state == D_DONE && !dc->ref) {
63a94fa1 1635 wait_for_completion_io(&dc->wait);
0ea80512
CY
1636 if (!dc->error)
1637 trimmed += dc->len;
63a94fa1 1638 __remove_discard_cmd(sbi, dc);
6afae633
CY
1639 } else {
1640 dc->ref++;
1641 need_wait = true;
1642 break;
63a94fa1
CY
1643 }
1644 }
1645 mutex_unlock(&dcc->cmd_lock);
6afae633
CY
1646
1647 if (need_wait) {
0ea80512 1648 trimmed += __wait_one_discard_bio(sbi, dc);
6afae633
CY
1649 goto next;
1650 }
0ea80512
CY
1651
1652 return trimmed;
63a94fa1
CY
1653}
1654
01f9cf6d 1655static unsigned int __wait_all_discard_cmd(struct f2fs_sb_info *sbi,
78997b56 1656 struct discard_policy *dpolicy)
8412663d 1657{
9a997188 1658 struct discard_policy dp;
01f9cf6d 1659 unsigned int discard_blks;
9a997188 1660
01f9cf6d
CY
1661 if (dpolicy)
1662 return __wait_discard_cmd_range(sbi, dpolicy, 0, UINT_MAX);
9a997188
JK
1663
1664 /* wait all */
8bb4f253 1665 __init_discard_policy(sbi, &dp, DPOLICY_FSTRIM, 1);
01f9cf6d 1666 discard_blks = __wait_discard_cmd_range(sbi, &dp, 0, UINT_MAX);
8bb4f253 1667 __init_discard_policy(sbi, &dp, DPOLICY_UMOUNT, 1);
01f9cf6d
CY
1668 discard_blks += __wait_discard_cmd_range(sbi, &dp, 0, UINT_MAX);
1669
1670 return discard_blks;
8412663d
CY
1671}
1672
004b6862 1673/* This should be covered by global mutex, &sit_i->sentry_lock */
94b1e10e 1674static void f2fs_wait_discard_bio(struct f2fs_sb_info *sbi, block_t blkaddr)
004b6862
CY
1675{
1676 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1677 struct discard_cmd *dc;
ec9895ad 1678 bool need_wait = false;
004b6862
CY
1679
1680 mutex_lock(&dcc->cmd_lock);
4d57b86d
CY
1681 dc = (struct discard_cmd *)f2fs_lookup_rb_tree(&dcc->root,
1682 NULL, blkaddr);
004b6862 1683 if (dc) {
ec9895ad
CY
1684 if (dc->state == D_PREP) {
1685 __punch_discard_cmd(sbi, dc, blkaddr);
1686 } else {
1687 dc->ref++;
1688 need_wait = true;
1689 }
275b66b0 1690 }
d431413f 1691 mutex_unlock(&dcc->cmd_lock);
ec9895ad 1692
2a510c00
CY
1693 if (need_wait)
1694 __wait_one_discard_bio(sbi, dc);
d431413f
CY
1695}
1696
4d57b86d 1697void f2fs_stop_discard_thread(struct f2fs_sb_info *sbi)
cce13252
CY
1698{
1699 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1700
1701 if (dcc && dcc->f2fs_issue_discard) {
1702 struct task_struct *discard_thread = dcc->f2fs_issue_discard;
1703
1704 dcc->f2fs_issue_discard = NULL;
1705 kthread_stop(discard_thread);
ec9895ad 1706 }
d431413f
CY
1707}
1708
8412663d 1709/* This comes from f2fs_put_super */
03f2c02d 1710bool f2fs_issue_discard_timeout(struct f2fs_sb_info *sbi)
969d1b18
CY
1711{
1712 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
78997b56 1713 struct discard_policy dpolicy;
cf5c759f 1714 bool dropped;
969d1b18 1715
8bb4f253
JK
1716 __init_discard_policy(sbi, &dpolicy, DPOLICY_UMOUNT,
1717 dcc->discard_granularity);
78997b56 1718 __issue_discard_cmd(sbi, &dpolicy);
cf5c759f 1719 dropped = __drop_discard_cmd(sbi);
cf5c759f 1720
9a997188
JK
1721 /* just to make sure there is no pending discard commands */
1722 __wait_all_discard_cmd(sbi, NULL);
2482c432
CY
1723
1724 f2fs_bug_on(sbi, atomic_read(&dcc->discard_cmd_cnt));
cf5c759f 1725 return dropped;
969d1b18
CY
1726}
1727
15469963
JK
1728static int issue_discard_thread(void *data)
1729{
1730 struct f2fs_sb_info *sbi = data;
1731 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1732 wait_queue_head_t *q = &dcc->discard_wait_queue;
78997b56 1733 struct discard_policy dpolicy;
969d1b18
CY
1734 unsigned int wait_ms = DEF_MIN_DISCARD_ISSUE_TIME;
1735 int issued;
15469963 1736
1d7be270 1737 set_freezable();
15469963 1738
1d7be270 1739 do {
8bb4f253 1740 __init_discard_policy(sbi, &dpolicy, DPOLICY_BG,
78997b56
CY
1741 dcc->discard_granularity);
1742
969d1b18
CY
1743 wait_event_interruptible_timeout(*q,
1744 kthread_should_stop() || freezing(current) ||
1745 dcc->discard_wake,
1746 msecs_to_jiffies(wait_ms));
35a9a766
SY
1747
1748 if (dcc->discard_wake)
1749 dcc->discard_wake = 0;
1750
76c7bfb3
JK
1751 /* clean up pending candidates before going to sleep */
1752 if (atomic_read(&dcc->queued_discard))
1753 __wait_all_discard_cmd(sbi, NULL);
1754
1d7be270
JK
1755 if (try_to_freeze())
1756 continue;
3b60d802
CY
1757 if (f2fs_readonly(sbi->sb))
1758 continue;
1d7be270
JK
1759 if (kthread_should_stop())
1760 return 0;
d6184774
YH
1761 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) {
1762 wait_ms = dpolicy.max_interval;
1763 continue;
1764 }
15469963 1765
0e5e8111 1766 if (sbi->gc_mode == GC_URGENT_HIGH)
8bb4f253 1767 __init_discard_policy(sbi, &dpolicy, DPOLICY_FORCE, 1);
969d1b18 1768
dc6febb6
CY
1769 sb_start_intwrite(sbi->sb);
1770
78997b56 1771 issued = __issue_discard_cmd(sbi, &dpolicy);
f9d1dced 1772 if (issued > 0) {
78997b56
CY
1773 __wait_all_discard_cmd(sbi, &dpolicy);
1774 wait_ms = dpolicy.min_interval;
f9d1dced 1775 } else if (issued == -1){
a7d10cf3
ST
1776 wait_ms = f2fs_time_to_wait(sbi, DISCARD_TIME);
1777 if (!wait_ms)
abde73c7 1778 wait_ms = dpolicy.mid_interval;
969d1b18 1779 } else {
78997b56 1780 wait_ms = dpolicy.max_interval;
969d1b18 1781 }
1d7be270 1782
dc6febb6 1783 sb_end_intwrite(sbi->sb);
1d7be270 1784
1d7be270
JK
1785 } while (!kthread_should_stop());
1786 return 0;
15469963
JK
1787}
1788
f46e8809 1789#ifdef CONFIG_BLK_DEV_ZONED
3c62be17
JK
1790static int __f2fs_issue_discard_zone(struct f2fs_sb_info *sbi,
1791 struct block_device *bdev, block_t blkstart, block_t blklen)
f46e8809 1792{
92592285 1793 sector_t sector, nr_sects;
10a875f8 1794 block_t lblkstart = blkstart;
3c62be17
JK
1795 int devi = 0;
1796
0916878d 1797 if (f2fs_is_multi_device(sbi)) {
3c62be17 1798 devi = f2fs_target_device_index(sbi, blkstart);
95175daf
DLM
1799 if (blkstart < FDEV(devi).start_blk ||
1800 blkstart > FDEV(devi).end_blk) {
dcbb4c10 1801 f2fs_err(sbi, "Invalid block %x", blkstart);
95175daf
DLM
1802 return -EIO;
1803 }
3c62be17
JK
1804 blkstart -= FDEV(devi).start_blk;
1805 }
f46e8809 1806
95175daf
DLM
1807 /* For sequential zones, reset the zone write pointer */
1808 if (f2fs_blkz_is_seq(sbi, devi, blkstart)) {
92592285
JK
1809 sector = SECTOR_FROM_BLOCK(blkstart);
1810 nr_sects = SECTOR_FROM_BLOCK(blklen);
1811
1812 if (sector & (bdev_zone_sectors(bdev) - 1) ||
1813 nr_sects != bdev_zone_sectors(bdev)) {
dcbb4c10
JP
1814 f2fs_err(sbi, "(%d) %s: Unaligned zone reset attempted (block %x + %x)",
1815 devi, sbi->s_ndevs ? FDEV(devi).path : "",
1816 blkstart, blklen);
92592285
JK
1817 return -EIO;
1818 }
d50aaeec 1819 trace_f2fs_issue_reset_zone(bdev, blkstart);
6c1b1da5
AJ
1820 return blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET,
1821 sector, nr_sects, GFP_NOFS);
f46e8809 1822 }
95175daf
DLM
1823
1824 /* For conventional zones, use regular discard if supported */
95175daf 1825 return __queue_discard_cmd(sbi, bdev, lblkstart, blklen);
f46e8809
DLM
1826}
1827#endif
1828
3c62be17
JK
1829static int __issue_discard_async(struct f2fs_sb_info *sbi,
1830 struct block_device *bdev, block_t blkstart, block_t blklen)
1831{
1832#ifdef CONFIG_BLK_DEV_ZONED
7f3d7719 1833 if (f2fs_sb_has_blkzoned(sbi) && bdev_is_zoned(bdev))
3c62be17
JK
1834 return __f2fs_issue_discard_zone(sbi, bdev, blkstart, blklen);
1835#endif
c81abe34 1836 return __queue_discard_cmd(sbi, bdev, blkstart, blklen);
3c62be17
JK
1837}
1838
1e87a78d 1839static int f2fs_issue_discard(struct f2fs_sb_info *sbi,
37208879
JK
1840 block_t blkstart, block_t blklen)
1841{
3c62be17
JK
1842 sector_t start = blkstart, len = 0;
1843 struct block_device *bdev;
a66cdd98
JK
1844 struct seg_entry *se;
1845 unsigned int offset;
1846 block_t i;
3c62be17
JK
1847 int err = 0;
1848
1849 bdev = f2fs_target_device(sbi, blkstart, NULL);
1850
1851 for (i = blkstart; i < blkstart + blklen; i++, len++) {
1852 if (i != start) {
1853 struct block_device *bdev2 =
1854 f2fs_target_device(sbi, i, NULL);
1855
1856 if (bdev2 != bdev) {
1857 err = __issue_discard_async(sbi, bdev,
1858 start, len);
1859 if (err)
1860 return err;
1861 bdev = bdev2;
1862 start = i;
1863 len = 0;
1864 }
1865 }
a66cdd98 1866
a66cdd98
JK
1867 se = get_seg_entry(sbi, GET_SEGNO(sbi, i));
1868 offset = GET_BLKOFF_FROM_SEG0(sbi, i);
1869
1870 if (!f2fs_test_and_set_bit(offset, se->discard_map))
1871 sbi->discard_blks--;
1872 }
f46e8809 1873
3c62be17
JK
1874 if (len)
1875 err = __issue_discard_async(sbi, bdev, start, len);
1876 return err;
1e87a78d
JK
1877}
1878
25290fa5
JK
1879static bool add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc,
1880 bool check_only)
adf4983b 1881{
b2955550
JK
1882 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
1883 int max_blocks = sbi->blocks_per_seg;
4b2fecc8 1884 struct seg_entry *se = get_seg_entry(sbi, cpc->trim_start);
b2955550
JK
1885 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
1886 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
a66cdd98 1887 unsigned long *discard_map = (unsigned long *)se->discard_map;
60a3b782 1888 unsigned long *dmap = SIT_I(sbi)->tmp_map;
b2955550 1889 unsigned int start = 0, end = -1;
c473f1a9 1890 bool force = (cpc->reason & CP_DISCARD);
a7eeb823 1891 struct discard_entry *de = NULL;
46f84c2c 1892 struct list_head *head = &SM_I(sbi)->dcc_info->entry_list;
b2955550
JK
1893 int i;
1894
7d20c8ab 1895 if (se->valid_blocks == max_blocks || !f2fs_hw_support_discard(sbi))
25290fa5 1896 return false;
b2955550 1897
a66cdd98 1898 if (!force) {
7d20c8ab 1899 if (!f2fs_realtime_discard_enable(sbi) || !se->valid_blocks ||
0b54fb84
JK
1900 SM_I(sbi)->dcc_info->nr_discards >=
1901 SM_I(sbi)->dcc_info->max_discards)
25290fa5 1902 return false;
4b2fecc8
JK
1903 }
1904
b2955550
JK
1905 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
1906 for (i = 0; i < entries; i++)
a66cdd98 1907 dmap[i] = force ? ~ckpt_map[i] & ~discard_map[i] :
d7bc2484 1908 (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
b2955550 1909
0b54fb84
JK
1910 while (force || SM_I(sbi)->dcc_info->nr_discards <=
1911 SM_I(sbi)->dcc_info->max_discards) {
b2955550
JK
1912 start = __find_rev_next_bit(dmap, max_blocks, end + 1);
1913 if (start >= max_blocks)
1914 break;
1915
1916 end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
c7b41e16
YH
1917 if (force && start && end != max_blocks
1918 && (end - start) < cpc->trim_minlen)
1919 continue;
1920
25290fa5
JK
1921 if (check_only)
1922 return true;
1923
a7eeb823
CY
1924 if (!de) {
1925 de = f2fs_kmem_cache_alloc(discard_entry_slab,
1926 GFP_F2FS_ZERO);
1927 de->start_blkaddr = START_BLOCK(sbi, cpc->trim_start);
1928 list_add_tail(&de->list, head);
1929 }
1930
1931 for (i = start; i < end; i++)
1932 __set_bit_le(i, (void *)de->discard_map);
1933
1934 SM_I(sbi)->dcc_info->nr_discards += end - start;
b2955550 1935 }
25290fa5 1936 return false;
b2955550
JK
1937}
1938
af8ff65b
CY
1939static void release_discard_addr(struct discard_entry *entry)
1940{
1941 list_del(&entry->list);
1942 kmem_cache_free(discard_entry_slab, entry);
1943}
1944
4d57b86d 1945void f2fs_release_discard_addrs(struct f2fs_sb_info *sbi)
4b2fecc8 1946{
46f84c2c 1947 struct list_head *head = &(SM_I(sbi)->dcc_info->entry_list);
4b2fecc8
JK
1948 struct discard_entry *entry, *this;
1949
1950 /* drop caches */
af8ff65b
CY
1951 list_for_each_entry_safe(entry, this, head, list)
1952 release_discard_addr(entry);
4b2fecc8
JK
1953}
1954
0a8165d7 1955/*
4d57b86d 1956 * Should call f2fs_clear_prefree_segments after checkpoint is done.
351df4b2
JK
1957 */
1958static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
1959{
1960 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
b65ee148 1961 unsigned int segno;
351df4b2
JK
1962
1963 mutex_lock(&dirty_i->seglist_lock);
7cd8558b 1964 for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], MAIN_SEGS(sbi))
351df4b2 1965 __set_test_and_free(sbi, segno);
351df4b2
JK
1966 mutex_unlock(&dirty_i->seglist_lock);
1967}
1968
4d57b86d
CY
1969void f2fs_clear_prefree_segments(struct f2fs_sb_info *sbi,
1970 struct cp_control *cpc)
351df4b2 1971{
969d1b18
CY
1972 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1973 struct list_head *head = &dcc->entry_list;
2d7b822a 1974 struct discard_entry *entry, *this;
351df4b2 1975 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
29e59c14 1976 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
29e59c14 1977 unsigned int start = 0, end = -1;
36abef4e 1978 unsigned int secno, start_segno;
c473f1a9 1979 bool force = (cpc->reason & CP_DISCARD);
b0332a0f 1980 bool need_align = f2fs_lfs_mode(sbi) && __is_large_section(sbi);
351df4b2
JK
1981
1982 mutex_lock(&dirty_i->seglist_lock);
29e59c14 1983
351df4b2 1984 while (1) {
29e59c14 1985 int i;
ad6672bb
YS
1986
1987 if (need_align && end != -1)
1988 end--;
7cd8558b
JK
1989 start = find_next_bit(prefree_map, MAIN_SEGS(sbi), end + 1);
1990 if (start >= MAIN_SEGS(sbi))
351df4b2 1991 break;
7cd8558b
JK
1992 end = find_next_zero_bit(prefree_map, MAIN_SEGS(sbi),
1993 start + 1);
29e59c14 1994
ad6672bb
YS
1995 if (need_align) {
1996 start = rounddown(start, sbi->segs_per_sec);
1997 end = roundup(end, sbi->segs_per_sec);
1998 }
29e59c14 1999
ad6672bb
YS
2000 for (i = start; i < end; i++) {
2001 if (test_and_clear_bit(i, prefree_map))
2002 dirty_i->nr_dirty[PRE]--;
2003 }
29e59c14 2004
7d20c8ab 2005 if (!f2fs_realtime_discard_enable(sbi))
29e59c14 2006 continue;
351df4b2 2007
650d3c4e
YH
2008 if (force && start >= cpc->trim_start &&
2009 (end - 1) <= cpc->trim_end)
2010 continue;
2011
b0332a0f 2012 if (!f2fs_lfs_mode(sbi) || !__is_large_section(sbi)) {
36abef4e 2013 f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
37208879 2014 (end - start) << sbi->log_blocks_per_seg);
36abef4e
JK
2015 continue;
2016 }
2017next:
4ddb1a4d
JK
2018 secno = GET_SEC_FROM_SEG(sbi, start);
2019 start_segno = GET_SEG_FROM_SEC(sbi, secno);
36abef4e 2020 if (!IS_CURSEC(sbi, secno) &&
302bd348 2021 !get_valid_blocks(sbi, start, true))
36abef4e
JK
2022 f2fs_issue_discard(sbi, START_BLOCK(sbi, start_segno),
2023 sbi->segs_per_sec << sbi->log_blocks_per_seg);
2024
2025 start = start_segno + sbi->segs_per_sec;
2026 if (start < end)
2027 goto next;
8b107f5b
JK
2028 else
2029 end = start - 1;
351df4b2
JK
2030 }
2031 mutex_unlock(&dirty_i->seglist_lock);
b2955550
JK
2032
2033 /* send small discards */
2d7b822a 2034 list_for_each_entry_safe(entry, this, head, list) {
a7eeb823
CY
2035 unsigned int cur_pos = 0, next_pos, len, total_len = 0;
2036 bool is_valid = test_bit_le(0, entry->discard_map);
2037
2038find_next:
2039 if (is_valid) {
2040 next_pos = find_next_zero_bit_le(entry->discard_map,
2041 sbi->blocks_per_seg, cur_pos);
2042 len = next_pos - cur_pos;
2043
7beb01f7 2044 if (f2fs_sb_has_blkzoned(sbi) ||
acfd2810 2045 (force && len < cpc->trim_minlen))
a7eeb823
CY
2046 goto skip;
2047
2048 f2fs_issue_discard(sbi, entry->start_blkaddr + cur_pos,
2049 len);
a7eeb823
CY
2050 total_len += len;
2051 } else {
2052 next_pos = find_next_bit_le(entry->discard_map,
2053 sbi->blocks_per_seg, cur_pos);
2054 }
836b5a63 2055skip:
a7eeb823
CY
2056 cur_pos = next_pos;
2057 is_valid = !is_valid;
2058
2059 if (cur_pos < sbi->blocks_per_seg)
2060 goto find_next;
2061
af8ff65b 2062 release_discard_addr(entry);
969d1b18 2063 dcc->nr_discards -= total_len;
b2955550 2064 }
34e159da 2065
01983c71 2066 wake_up_discard_thread(sbi, false);
351df4b2
JK
2067}
2068
8ed59745 2069static int create_discard_cmd_control(struct f2fs_sb_info *sbi)
0b54fb84 2070{
15469963 2071 dev_t dev = sbi->sb->s_bdev->bd_dev;
0b54fb84 2072 struct discard_cmd_control *dcc;
ba48a33e 2073 int err = 0, i;
0b54fb84
JK
2074
2075 if (SM_I(sbi)->dcc_info) {
2076 dcc = SM_I(sbi)->dcc_info;
2077 goto init_thread;
2078 }
2079
acbf054d 2080 dcc = f2fs_kzalloc(sbi, sizeof(struct discard_cmd_control), GFP_KERNEL);
0b54fb84
JK
2081 if (!dcc)
2082 return -ENOMEM;
2083
969d1b18 2084 dcc->discard_granularity = DEFAULT_DISCARD_GRANULARITY;
46f84c2c 2085 INIT_LIST_HEAD(&dcc->entry_list);
78997b56 2086 for (i = 0; i < MAX_PLIST_NUM; i++)
ba48a33e 2087 INIT_LIST_HEAD(&dcc->pend_list[i]);
46f84c2c 2088 INIT_LIST_HEAD(&dcc->wait_list);
8412663d 2089 INIT_LIST_HEAD(&dcc->fstrim_list);
15469963 2090 mutex_init(&dcc->cmd_lock);
8b8dd65f 2091 atomic_set(&dcc->issued_discard, 0);
72691af6 2092 atomic_set(&dcc->queued_discard, 0);
5f32366a 2093 atomic_set(&dcc->discard_cmd_cnt, 0);
0b54fb84 2094 dcc->nr_discards = 0;
d618ebaf 2095 dcc->max_discards = MAIN_SEGS(sbi) << sbi->log_blocks_per_seg;
d84d1cbd 2096 dcc->undiscard_blks = 0;
20ee4382 2097 dcc->next_pos = 0;
4dada3fd 2098 dcc->root = RB_ROOT_CACHED;
67fce70b 2099 dcc->rbtree_check = false;
0b54fb84 2100
15469963 2101 init_waitqueue_head(&dcc->discard_wait_queue);
0b54fb84
JK
2102 SM_I(sbi)->dcc_info = dcc;
2103init_thread:
15469963
JK
2104 dcc->f2fs_issue_discard = kthread_run(issue_discard_thread, sbi,
2105 "f2fs_discard-%u:%u", MAJOR(dev), MINOR(dev));
2106 if (IS_ERR(dcc->f2fs_issue_discard)) {
2107 err = PTR_ERR(dcc->f2fs_issue_discard);
5222595d 2108 kvfree(dcc);
15469963
JK
2109 SM_I(sbi)->dcc_info = NULL;
2110 return err;
2111 }
2112
0b54fb84
JK
2113 return err;
2114}
2115
f099405f 2116static void destroy_discard_cmd_control(struct f2fs_sb_info *sbi)
0b54fb84
JK
2117{
2118 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
2119
f099405f
CY
2120 if (!dcc)
2121 return;
2122
4d57b86d 2123 f2fs_stop_discard_thread(sbi);
f099405f 2124
04f9287a
CY
2125 /*
2126 * Recovery can cache discard commands, so in error path of
2127 * fill_super(), it needs to give a chance to handle them.
2128 */
2129 if (unlikely(atomic_read(&dcc->discard_cmd_cnt)))
2130 f2fs_issue_discard_timeout(sbi);
2131
5222595d 2132 kvfree(dcc);
f099405f 2133 SM_I(sbi)->dcc_info = NULL;
0b54fb84
JK
2134}
2135
184a5cd2 2136static bool __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
351df4b2
JK
2137{
2138 struct sit_info *sit_i = SIT_I(sbi);
184a5cd2
CY
2139
2140 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap)) {
351df4b2 2141 sit_i->dirty_sentries++;
184a5cd2
CY
2142 return false;
2143 }
2144
2145 return true;
351df4b2
JK
2146}
2147
2148static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
2149 unsigned int segno, int modified)
2150{
2151 struct seg_entry *se = get_seg_entry(sbi, segno);
2152 se->type = type;
2153 if (modified)
2154 __mark_sit_entry_dirty(sbi, segno);
2155}
2156
2157static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
2158{
2159 struct seg_entry *se;
2160 unsigned int segno, offset;
2161 long int new_vblocks;
6415fedc
YS
2162 bool exist;
2163#ifdef CONFIG_F2FS_CHECK_FS
2164 bool mir_exist;
2165#endif
351df4b2
JK
2166
2167 segno = GET_SEGNO(sbi, blkaddr);
2168
2169 se = get_seg_entry(sbi, segno);
2170 new_vblocks = se->valid_blocks + del;
491c0854 2171 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
351df4b2 2172
9feffe14 2173 f2fs_bug_on(sbi, (new_vblocks < 0 ||
de881df9 2174 (new_vblocks > f2fs_usable_blks_in_seg(sbi, segno))));
351df4b2
JK
2175
2176 se->valid_blocks = new_vblocks;
a1f72ac2
CY
2177 se->mtime = get_mtime(sbi, false);
2178 if (se->mtime > SIT_I(sbi)->max_mtime)
2179 SIT_I(sbi)->max_mtime = se->mtime;
351df4b2
JK
2180
2181 /* Update valid block bitmap */
2182 if (del > 0) {
6415fedc 2183 exist = f2fs_test_and_set_bit(offset, se->cur_valid_map);
355e7891 2184#ifdef CONFIG_F2FS_CHECK_FS
6415fedc
YS
2185 mir_exist = f2fs_test_and_set_bit(offset,
2186 se->cur_valid_map_mir);
2187 if (unlikely(exist != mir_exist)) {
dcbb4c10
JP
2188 f2fs_err(sbi, "Inconsistent error when setting bitmap, blk:%u, old bit:%d",
2189 blkaddr, exist);
05796763 2190 f2fs_bug_on(sbi, 1);
6415fedc 2191 }
355e7891 2192#endif
6415fedc 2193 if (unlikely(exist)) {
dcbb4c10
JP
2194 f2fs_err(sbi, "Bitmap was wrongly set, blk:%u",
2195 blkaddr);
6415fedc 2196 f2fs_bug_on(sbi, 1);
35ee82ca
YS
2197 se->valid_blocks--;
2198 del = 0;
355e7891 2199 }
6415fedc 2200
7d20c8ab 2201 if (!f2fs_test_and_set_bit(offset, se->discard_map))
a66cdd98 2202 sbi->discard_blks--;
720037f9 2203
899fee36
CY
2204 /*
2205 * SSR should never reuse block which is checkpointed
2206 * or newly invalidated.
2207 */
2208 if (!is_sbi_flag_set(sbi, SBI_CP_DISABLED)) {
720037f9
JK
2209 if (!f2fs_test_and_set_bit(offset, se->ckpt_valid_map))
2210 se->ckpt_valid_blocks++;
2211 }
351df4b2 2212 } else {
6415fedc 2213 exist = f2fs_test_and_clear_bit(offset, se->cur_valid_map);
355e7891 2214#ifdef CONFIG_F2FS_CHECK_FS
6415fedc
YS
2215 mir_exist = f2fs_test_and_clear_bit(offset,
2216 se->cur_valid_map_mir);
2217 if (unlikely(exist != mir_exist)) {
dcbb4c10
JP
2218 f2fs_err(sbi, "Inconsistent error when clearing bitmap, blk:%u, old bit:%d",
2219 blkaddr, exist);
05796763 2220 f2fs_bug_on(sbi, 1);
6415fedc 2221 }
355e7891 2222#endif
6415fedc 2223 if (unlikely(!exist)) {
dcbb4c10
JP
2224 f2fs_err(sbi, "Bitmap was wrongly cleared, blk:%u",
2225 blkaddr);
6415fedc 2226 f2fs_bug_on(sbi, 1);
35ee82ca
YS
2227 se->valid_blocks++;
2228 del = 0;
4354994f
DR
2229 } else if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
2230 /*
2231 * If checkpoints are off, we must not reuse data that
2232 * was used in the previous checkpoint. If it was used
2233 * before, we must track that to know how much space we
2234 * really have.
2235 */
c9c8ed50
CY
2236 if (f2fs_test_bit(offset, se->ckpt_valid_map)) {
2237 spin_lock(&sbi->stat_lock);
4354994f 2238 sbi->unusable_block_count++;
c9c8ed50
CY
2239 spin_unlock(&sbi->stat_lock);
2240 }
355e7891 2241 }
6415fedc 2242
7d20c8ab 2243 if (f2fs_test_and_clear_bit(offset, se->discard_map))
a66cdd98 2244 sbi->discard_blks++;
351df4b2
JK
2245 }
2246 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
2247 se->ckpt_valid_blocks += del;
2248
2249 __mark_sit_entry_dirty(sbi, segno);
2250
2251 /* update total number of valid blocks to be written in ckpt area */
2252 SIT_I(sbi)->written_valid_blocks += del;
2253
2c70c5e3 2254 if (__is_large_section(sbi))
351df4b2
JK
2255 get_sec_entry(sbi, segno)->valid_blocks += del;
2256}
2257
4d57b86d 2258void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
351df4b2
JK
2259{
2260 unsigned int segno = GET_SEGNO(sbi, addr);
2261 struct sit_info *sit_i = SIT_I(sbi);
2262
9850cf4a 2263 f2fs_bug_on(sbi, addr == NULL_ADDR);
4c8ff709 2264 if (addr == NEW_ADDR || addr == COMPRESS_ADDR)
351df4b2
JK
2265 return;
2266
6aa58d8a
CY
2267 invalidate_mapping_pages(META_MAPPING(sbi), addr, addr);
2268
351df4b2 2269 /* add it into sit main buffer */
3d26fa6b 2270 down_write(&sit_i->sentry_lock);
351df4b2
JK
2271
2272 update_sit_entry(sbi, addr, -1);
2273
2274 /* add it into dirty seglist */
2275 locate_dirty_segment(sbi, segno);
2276
3d26fa6b 2277 up_write(&sit_i->sentry_lock);
351df4b2
JK
2278}
2279
4d57b86d 2280bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr)
6e2c64ad
JK
2281{
2282 struct sit_info *sit_i = SIT_I(sbi);
2283 unsigned int segno, offset;
2284 struct seg_entry *se;
2285 bool is_cp = false;
2286
93770ab7 2287 if (!__is_valid_data_blkaddr(blkaddr))
6e2c64ad
JK
2288 return true;
2289
3d26fa6b 2290 down_read(&sit_i->sentry_lock);
6e2c64ad
JK
2291
2292 segno = GET_SEGNO(sbi, blkaddr);
2293 se = get_seg_entry(sbi, segno);
2294 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
2295
2296 if (f2fs_test_bit(offset, se->ckpt_valid_map))
2297 is_cp = true;
2298
3d26fa6b 2299 up_read(&sit_i->sentry_lock);
6e2c64ad
JK
2300
2301 return is_cp;
2302}
2303
0a8165d7 2304/*
351df4b2
JK
2305 * This function should be resided under the curseg_mutex lock
2306 */
2307static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
e79efe3b 2308 struct f2fs_summary *sum)
351df4b2
JK
2309{
2310 struct curseg_info *curseg = CURSEG_I(sbi, type);
2311 void *addr = curseg->sum_blk;
e79efe3b 2312 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
351df4b2 2313 memcpy(addr, sum, sizeof(struct f2fs_summary));
351df4b2
JK
2314}
2315
0a8165d7 2316/*
351df4b2
JK
2317 * Calculate the number of current summary pages for writing
2318 */
4d57b86d 2319int f2fs_npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra)
351df4b2 2320{
351df4b2 2321 int valid_sum_count = 0;
9a47938b 2322 int i, sum_in_page;
351df4b2
JK
2323
2324 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
2325 if (sbi->ckpt->alloc_type[i] == SSR)
2326 valid_sum_count += sbi->blocks_per_seg;
3fa06d7b
CY
2327 else {
2328 if (for_ra)
2329 valid_sum_count += le16_to_cpu(
2330 F2FS_CKPT(sbi)->cur_data_blkoff[i]);
2331 else
2332 valid_sum_count += curseg_blkoff(sbi, i);
2333 }
351df4b2
JK
2334 }
2335
09cbfeaf 2336 sum_in_page = (PAGE_SIZE - 2 * SUM_JOURNAL_SIZE -
9a47938b
FL
2337 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
2338 if (valid_sum_count <= sum_in_page)
351df4b2 2339 return 1;
9a47938b 2340 else if ((valid_sum_count - sum_in_page) <=
09cbfeaf 2341 (PAGE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
351df4b2
JK
2342 return 2;
2343 return 3;
2344}
2345
0a8165d7 2346/*
351df4b2
JK
2347 * Caller should put this summary page
2348 */
4d57b86d 2349struct page *f2fs_get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
351df4b2 2350{
7735730d 2351 return f2fs_get_meta_page_nofail(sbi, GET_SUM_BLOCK(sbi, segno));
351df4b2
JK
2352}
2353
4d57b86d
CY
2354void f2fs_update_meta_page(struct f2fs_sb_info *sbi,
2355 void *src, block_t blk_addr)
351df4b2 2356{
4d57b86d 2357 struct page *page = f2fs_grab_meta_page(sbi, blk_addr);
381722d2 2358
0537b811 2359 memcpy(page_address(page), src, PAGE_SIZE);
351df4b2
JK
2360 set_page_dirty(page);
2361 f2fs_put_page(page, 1);
2362}
2363
381722d2
CY
2364static void write_sum_page(struct f2fs_sb_info *sbi,
2365 struct f2fs_summary_block *sum_blk, block_t blk_addr)
2366{
4d57b86d 2367 f2fs_update_meta_page(sbi, (void *)sum_blk, blk_addr);
381722d2
CY
2368}
2369
b7ad7512
CY
2370static void write_current_sum_page(struct f2fs_sb_info *sbi,
2371 int type, block_t blk_addr)
2372{
2373 struct curseg_info *curseg = CURSEG_I(sbi, type);
4d57b86d 2374 struct page *page = f2fs_grab_meta_page(sbi, blk_addr);
b7ad7512
CY
2375 struct f2fs_summary_block *src = curseg->sum_blk;
2376 struct f2fs_summary_block *dst;
2377
2378 dst = (struct f2fs_summary_block *)page_address(page);
81114baa 2379 memset(dst, 0, PAGE_SIZE);
b7ad7512
CY
2380
2381 mutex_lock(&curseg->curseg_mutex);
2382
2383 down_read(&curseg->journal_rwsem);
2384 memcpy(&dst->journal, curseg->journal, SUM_JOURNAL_SIZE);
2385 up_read(&curseg->journal_rwsem);
2386
2387 memcpy(dst->entries, src->entries, SUM_ENTRY_SIZE);
2388 memcpy(&dst->footer, &src->footer, SUM_FOOTER_SIZE);
2389
2390 mutex_unlock(&curseg->curseg_mutex);
2391
2392 set_page_dirty(page);
2393 f2fs_put_page(page, 1);
2394}
2395
a7881893
JK
2396static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
2397{
2398 struct curseg_info *curseg = CURSEG_I(sbi, type);
2399 unsigned int segno = curseg->segno + 1;
2400 struct free_segmap_info *free_i = FREE_I(sbi);
2401
2402 if (segno < MAIN_SEGS(sbi) && segno % sbi->segs_per_sec)
2403 return !test_bit(segno, free_i->free_segmap);
2404 return 0;
2405}
2406
0a8165d7 2407/*
351df4b2
JK
2408 * Find a new segment from the free segments bitmap to right order
2409 * This function should be returned with success, otherwise BUG
2410 */
2411static void get_new_segment(struct f2fs_sb_info *sbi,
2412 unsigned int *newseg, bool new_sec, int dir)
2413{
2414 struct free_segmap_info *free_i = FREE_I(sbi);
351df4b2 2415 unsigned int segno, secno, zoneno;
7cd8558b 2416 unsigned int total_zones = MAIN_SECS(sbi) / sbi->secs_per_zone;
4ddb1a4d
JK
2417 unsigned int hint = GET_SEC_FROM_SEG(sbi, *newseg);
2418 unsigned int old_zoneno = GET_ZONE_FROM_SEG(sbi, *newseg);
351df4b2
JK
2419 unsigned int left_start = hint;
2420 bool init = true;
2421 int go_left = 0;
2422 int i;
2423
1a118ccf 2424 spin_lock(&free_i->segmap_lock);
351df4b2
JK
2425
2426 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
2427 segno = find_next_zero_bit(free_i->free_segmap,
4ddb1a4d
JK
2428 GET_SEG_FROM_SEC(sbi, hint + 1), *newseg + 1);
2429 if (segno < GET_SEG_FROM_SEC(sbi, hint + 1))
351df4b2
JK
2430 goto got_it;
2431 }
2432find_other_zone:
7cd8558b
JK
2433 secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint);
2434 if (secno >= MAIN_SECS(sbi)) {
351df4b2
JK
2435 if (dir == ALLOC_RIGHT) {
2436 secno = find_next_zero_bit(free_i->free_secmap,
7cd8558b
JK
2437 MAIN_SECS(sbi), 0);
2438 f2fs_bug_on(sbi, secno >= MAIN_SECS(sbi));
351df4b2
JK
2439 } else {
2440 go_left = 1;
2441 left_start = hint - 1;
2442 }
2443 }
2444 if (go_left == 0)
2445 goto skip_left;
2446
2447 while (test_bit(left_start, free_i->free_secmap)) {
2448 if (left_start > 0) {
2449 left_start--;
2450 continue;
2451 }
2452 left_start = find_next_zero_bit(free_i->free_secmap,
7cd8558b
JK
2453 MAIN_SECS(sbi), 0);
2454 f2fs_bug_on(sbi, left_start >= MAIN_SECS(sbi));
351df4b2
JK
2455 break;
2456 }
2457 secno = left_start;
2458skip_left:
4ddb1a4d
JK
2459 segno = GET_SEG_FROM_SEC(sbi, secno);
2460 zoneno = GET_ZONE_FROM_SEC(sbi, secno);
351df4b2
JK
2461
2462 /* give up on finding another zone */
2463 if (!init)
2464 goto got_it;
2465 if (sbi->secs_per_zone == 1)
2466 goto got_it;
2467 if (zoneno == old_zoneno)
2468 goto got_it;
2469 if (dir == ALLOC_LEFT) {
2470 if (!go_left && zoneno + 1 >= total_zones)
2471 goto got_it;
2472 if (go_left && zoneno == 0)
2473 goto got_it;
2474 }
2475 for (i = 0; i < NR_CURSEG_TYPE; i++)
2476 if (CURSEG_I(sbi, i)->zone == zoneno)
2477 break;
2478
2479 if (i < NR_CURSEG_TYPE) {
2480 /* zone is in user, try another */
2481 if (go_left)
2482 hint = zoneno * sbi->secs_per_zone - 1;
2483 else if (zoneno + 1 >= total_zones)
2484 hint = 0;
2485 else
2486 hint = (zoneno + 1) * sbi->secs_per_zone;
2487 init = false;
2488 goto find_other_zone;
2489 }
2490got_it:
2491 /* set it as dirty segment in free segmap */
9850cf4a 2492 f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap));
351df4b2
JK
2493 __set_inuse(sbi, segno);
2494 *newseg = segno;
1a118ccf 2495 spin_unlock(&free_i->segmap_lock);
351df4b2
JK
2496}
2497
2498static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
2499{
2500 struct curseg_info *curseg = CURSEG_I(sbi, type);
2501 struct summary_footer *sum_footer;
2502
2503 curseg->segno = curseg->next_segno;
4ddb1a4d 2504 curseg->zone = GET_ZONE_FROM_SEG(sbi, curseg->segno);
351df4b2
JK
2505 curseg->next_blkoff = 0;
2506 curseg->next_segno = NULL_SEGNO;
2507
2508 sum_footer = &(curseg->sum_blk->footer);
2509 memset(sum_footer, 0, sizeof(struct summary_footer));
2510 if (IS_DATASEG(type))
2511 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
2512 if (IS_NODESEG(type))
2513 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
2514 __set_sit_entry_type(sbi, type, curseg->segno, modified);
2515}
2516
7a20b8a6
JK
2517static unsigned int __get_next_segno(struct f2fs_sb_info *sbi, int type)
2518{
a7881893 2519 /* if segs_per_sec is large than 1, we need to keep original policy. */
2c70c5e3 2520 if (__is_large_section(sbi))
a7881893
JK
2521 return CURSEG_I(sbi, type)->segno;
2522
4354994f
DR
2523 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2524 return 0;
2525
b94929d9
YS
2526 if (test_opt(sbi, NOHEAP) &&
2527 (type == CURSEG_HOT_DATA || IS_NODESEG(type)))
7a20b8a6
JK
2528 return 0;
2529
e066b83c
JK
2530 if (SIT_I(sbi)->last_victim[ALLOC_NEXT])
2531 return SIT_I(sbi)->last_victim[ALLOC_NEXT];
07939627
JK
2532
2533 /* find segments from 0 to reuse freed segments */
63189b78 2534 if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_REUSE)
07939627
JK
2535 return 0;
2536
7a20b8a6
JK
2537 return CURSEG_I(sbi, type)->segno;
2538}
2539
0a8165d7 2540/*
351df4b2
JK
2541 * Allocate a current working segment.
2542 * This function always allocates a free segment in LFS manner.
2543 */
2544static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
2545{
2546 struct curseg_info *curseg = CURSEG_I(sbi, type);
2547 unsigned int segno = curseg->segno;
2548 int dir = ALLOC_LEFT;
2549
2550 write_sum_page(sbi, curseg->sum_blk,
81fb5e87 2551 GET_SUM_BLOCK(sbi, segno));
351df4b2
JK
2552 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
2553 dir = ALLOC_RIGHT;
2554
2555 if (test_opt(sbi, NOHEAP))
2556 dir = ALLOC_RIGHT;
2557
7a20b8a6 2558 segno = __get_next_segno(sbi, type);
351df4b2
JK
2559 get_new_segment(sbi, &segno, new_sec, dir);
2560 curseg->next_segno = segno;
2561 reset_curseg(sbi, type, 1);
2562 curseg->alloc_type = LFS;
2563}
2564
2565static void __next_free_blkoff(struct f2fs_sb_info *sbi,
2566 struct curseg_info *seg, block_t start)
2567{
2568 struct seg_entry *se = get_seg_entry(sbi, seg->segno);
e81c93cf 2569 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
60a3b782 2570 unsigned long *target_map = SIT_I(sbi)->tmp_map;
e81c93cf
CL
2571 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
2572 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
2573 int i, pos;
2574
2575 for (i = 0; i < entries; i++)
2576 target_map[i] = ckpt_map[i] | cur_map[i];
2577
2578 pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
2579
2580 seg->next_blkoff = pos;
351df4b2
JK
2581}
2582
0a8165d7 2583/*
351df4b2
JK
2584 * If a segment is written by LFS manner, next block offset is just obtained
2585 * by increasing the current block offset. However, if a segment is written by
2586 * SSR manner, next block offset obtained by calling __next_free_blkoff
2587 */
2588static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
2589 struct curseg_info *seg)
2590{
2591 if (seg->alloc_type == SSR)
2592 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
2593 else
2594 seg->next_blkoff++;
2595}
2596
0a8165d7 2597/*
e1c42045 2598 * This function always allocates a used segment(from dirty seglist) by SSR
351df4b2
JK
2599 * manner, so it should recover the existing segment information of valid blocks
2600 */
025d63a4 2601static void change_curseg(struct f2fs_sb_info *sbi, int type)
351df4b2
JK
2602{
2603 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2604 struct curseg_info *curseg = CURSEG_I(sbi, type);
2605 unsigned int new_segno = curseg->next_segno;
2606 struct f2fs_summary_block *sum_node;
2607 struct page *sum_page;
2608
2609 write_sum_page(sbi, curseg->sum_blk,
2610 GET_SUM_BLOCK(sbi, curseg->segno));
2611 __set_test_and_inuse(sbi, new_segno);
2612
2613 mutex_lock(&dirty_i->seglist_lock);
2614 __remove_dirty_segment(sbi, new_segno, PRE);
2615 __remove_dirty_segment(sbi, new_segno, DIRTY);
2616 mutex_unlock(&dirty_i->seglist_lock);
2617
2618 reset_curseg(sbi, type, 1);
2619 curseg->alloc_type = SSR;
2620 __next_free_blkoff(sbi, curseg, 0);
2621
4d57b86d 2622 sum_page = f2fs_get_sum_page(sbi, new_segno);
edc55aaf 2623 f2fs_bug_on(sbi, IS_ERR(sum_page));
025d63a4
CY
2624 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
2625 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
2626 f2fs_put_page(sum_page, 1);
351df4b2
JK
2627}
2628
43727527
JK
2629static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
2630{
2631 struct curseg_info *curseg = CURSEG_I(sbi, type);
2632 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
e066b83c 2633 unsigned segno = NULL_SEGNO;
d27c3d89
CY
2634 int i, cnt;
2635 bool reversed = false;
c192f7a4 2636
4d57b86d 2637 /* f2fs_need_SSR() already forces to do this */
97767500 2638 if (!v_ops->get_victim(sbi, &segno, BG_GC, type, SSR)) {
e066b83c 2639 curseg->next_segno = segno;
c192f7a4 2640 return 1;
e066b83c 2641 }
43727527 2642
70d625cb
JK
2643 /* For node segments, let's do SSR more intensively */
2644 if (IS_NODESEG(type)) {
d27c3d89
CY
2645 if (type >= CURSEG_WARM_NODE) {
2646 reversed = true;
2647 i = CURSEG_COLD_NODE;
2648 } else {
2649 i = CURSEG_HOT_NODE;
2650 }
2651 cnt = NR_CURSEG_NODE_TYPE;
70d625cb 2652 } else {
d27c3d89
CY
2653 if (type >= CURSEG_WARM_DATA) {
2654 reversed = true;
2655 i = CURSEG_COLD_DATA;
2656 } else {
2657 i = CURSEG_HOT_DATA;
2658 }
2659 cnt = NR_CURSEG_DATA_TYPE;
70d625cb 2660 }
43727527 2661
d27c3d89 2662 for (; cnt-- > 0; reversed ? i-- : i++) {
c192f7a4
JK
2663 if (i == type)
2664 continue;
97767500 2665 if (!v_ops->get_victim(sbi, &segno, BG_GC, i, SSR)) {
e066b83c 2666 curseg->next_segno = segno;
43727527 2667 return 1;
e066b83c 2668 }
c192f7a4 2669 }
4354994f
DR
2670
2671 /* find valid_blocks=0 in dirty list */
2672 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
2673 segno = get_free_segment(sbi);
2674 if (segno != NULL_SEGNO) {
2675 curseg->next_segno = segno;
2676 return 1;
2677 }
2678 }
43727527
JK
2679 return 0;
2680}
2681
351df4b2
JK
2682/*
2683 * flush out current segment and replace it with new segment
2684 * This function should be returned with success, otherwise BUG
2685 */
2686static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
2687 int type, bool force)
2688{
a7881893
JK
2689 struct curseg_info *curseg = CURSEG_I(sbi, type);
2690
7b405275 2691 if (force)
351df4b2 2692 new_curseg(sbi, type, true);
5b6c6be2
JK
2693 else if (!is_set_ckpt_flags(sbi, CP_CRC_RECOVERY_FLAG) &&
2694 type == CURSEG_WARM_NODE)
351df4b2 2695 new_curseg(sbi, type, false);
4354994f
DR
2696 else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type) &&
2697 likely(!is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
a7881893 2698 new_curseg(sbi, type, false);
4d57b86d 2699 else if (f2fs_need_SSR(sbi) && get_ssr_segment(sbi, type))
025d63a4 2700 change_curseg(sbi, type);
351df4b2
JK
2701 else
2702 new_curseg(sbi, type, false);
dcdfff65 2703
a7881893 2704 stat_inc_seg_type(sbi, curseg);
351df4b2
JK
2705}
2706
0ef81833 2707void f2fs_allocate_segment_for_resize(struct f2fs_sb_info *sbi, int type,
04f0b2ea
QS
2708 unsigned int start, unsigned int end)
2709{
2710 struct curseg_info *curseg = CURSEG_I(sbi, type);
2711 unsigned int segno;
2712
2713 down_read(&SM_I(sbi)->curseg_lock);
2714 mutex_lock(&curseg->curseg_mutex);
2715 down_write(&SIT_I(sbi)->sentry_lock);
2716
2717 segno = CURSEG_I(sbi, type)->segno;
2718 if (segno < start || segno > end)
2719 goto unlock;
2720
2721 if (f2fs_need_SSR(sbi) && get_ssr_segment(sbi, type))
2722 change_curseg(sbi, type);
2723 else
2724 new_curseg(sbi, type, true);
2725
2726 stat_inc_seg_type(sbi, curseg);
2727
2728 locate_dirty_segment(sbi, segno);
2729unlock:
2730 up_write(&SIT_I(sbi)->sentry_lock);
2731
2732 if (segno != curseg->segno)
dcbb4c10
JP
2733 f2fs_notice(sbi, "For resize: curseg of type %d: %u ==> %u",
2734 type, segno, curseg->segno);
04f0b2ea
QS
2735
2736 mutex_unlock(&curseg->curseg_mutex);
2737 up_read(&SM_I(sbi)->curseg_lock);
2738}
2739
901d745f 2740static void __allocate_new_segment(struct f2fs_sb_info *sbi, int type)
351df4b2 2741{
901d745f 2742 struct curseg_info *curseg = CURSEG_I(sbi, type);
6ae1be13 2743 unsigned int old_segno;
351df4b2 2744
901d745f
CY
2745 if (!curseg->next_blkoff &&
2746 !get_valid_blocks(sbi, curseg->segno, false) &&
2747 !get_ckpt_valid_blocks(sbi, curseg->segno))
2748 return;
3d26fa6b 2749
901d745f
CY
2750 old_segno = curseg->segno;
2751 SIT_I(sbi)->s_ops->allocate_segment(sbi, type, true);
2752 locate_dirty_segment(sbi, old_segno);
2753}
f5a53edc 2754
901d745f
CY
2755void f2fs_allocate_new_segment(struct f2fs_sb_info *sbi, int type)
2756{
2757 down_write(&SIT_I(sbi)->sentry_lock);
2758 __allocate_new_segment(sbi, type);
2759 up_write(&SIT_I(sbi)->sentry_lock);
2760}
3d26fa6b 2761
901d745f
CY
2762void f2fs_allocate_new_segments(struct f2fs_sb_info *sbi)
2763{
2764 int i;
2765
2766 down_write(&SIT_I(sbi)->sentry_lock);
2767 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++)
2768 __allocate_new_segment(sbi, i);
3d26fa6b 2769 up_write(&SIT_I(sbi)->sentry_lock);
351df4b2
JK
2770}
2771
2772static const struct segment_allocation default_salloc_ops = {
2773 .allocate_segment = allocate_segment_by_default,
2774};
2775
4d57b86d
CY
2776bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi,
2777 struct cp_control *cpc)
25290fa5
JK
2778{
2779 __u64 trim_start = cpc->trim_start;
2780 bool has_candidate = false;
2781
3d26fa6b 2782 down_write(&SIT_I(sbi)->sentry_lock);
25290fa5
JK
2783 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++) {
2784 if (add_discard_addrs(sbi, cpc, true)) {
2785 has_candidate = true;
2786 break;
2787 }
2788 }
3d26fa6b 2789 up_write(&SIT_I(sbi)->sentry_lock);
25290fa5
JK
2790
2791 cpc->trim_start = trim_start;
2792 return has_candidate;
2793}
2794
01f9cf6d 2795static unsigned int __issue_discard_cmd_range(struct f2fs_sb_info *sbi,
9a997188
JK
2796 struct discard_policy *dpolicy,
2797 unsigned int start, unsigned int end)
2798{
2799 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
2800 struct discard_cmd *prev_dc = NULL, *next_dc = NULL;
2801 struct rb_node **insert_p = NULL, *insert_parent = NULL;
2802 struct discard_cmd *dc;
2803 struct blk_plug plug;
2804 int issued;
01f9cf6d 2805 unsigned int trimmed = 0;
9a997188
JK
2806
2807next:
2808 issued = 0;
2809
2810 mutex_lock(&dcc->cmd_lock);
67fce70b
CY
2811 if (unlikely(dcc->rbtree_check))
2812 f2fs_bug_on(sbi, !f2fs_check_rb_tree_consistence(sbi,
2813 &dcc->root));
9a997188 2814
4d57b86d 2815 dc = (struct discard_cmd *)f2fs_lookup_rb_tree_ret(&dcc->root,
9a997188
JK
2816 NULL, start,
2817 (struct rb_entry **)&prev_dc,
2818 (struct rb_entry **)&next_dc,
4dada3fd 2819 &insert_p, &insert_parent, true, NULL);
9a997188
JK
2820 if (!dc)
2821 dc = next_dc;
2822
2823 blk_start_plug(&plug);
2824
2825 while (dc && dc->lstart <= end) {
2826 struct rb_node *node;
6b9cb124 2827 int err = 0;
9a997188
JK
2828
2829 if (dc->len < dpolicy->granularity)
2830 goto skip;
2831
2832 if (dc->state != D_PREP) {
2833 list_move_tail(&dc->list, &dcc->fstrim_list);
2834 goto skip;
2835 }
2836
6b9cb124 2837 err = __submit_discard_cmd(sbi, dpolicy, dc, &issued);
9a997188 2838
35ec7d57 2839 if (issued >= dpolicy->max_requests) {
9a997188
JK
2840 start = dc->lstart + dc->len;
2841
6b9cb124
CY
2842 if (err)
2843 __remove_discard_cmd(sbi, dc);
2844
9a997188
JK
2845 blk_finish_plug(&plug);
2846 mutex_unlock(&dcc->cmd_lock);
01f9cf6d 2847 trimmed += __wait_all_discard_cmd(sbi, NULL);
5df7731f 2848 congestion_wait(BLK_RW_ASYNC, DEFAULT_IO_TIMEOUT);
9a997188
JK
2849 goto next;
2850 }
2851skip:
2852 node = rb_next(&dc->rb_node);
6b9cb124
CY
2853 if (err)
2854 __remove_discard_cmd(sbi, dc);
9a997188
JK
2855 dc = rb_entry_safe(node, struct discard_cmd, rb_node);
2856
2857 if (fatal_signal_pending(current))
2858 break;
2859 }
2860
2861 blk_finish_plug(&plug);
2862 mutex_unlock(&dcc->cmd_lock);
01f9cf6d
CY
2863
2864 return trimmed;
9a997188
JK
2865}
2866
4b2fecc8
JK
2867int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range)
2868{
f7ef9b83
JK
2869 __u64 start = F2FS_BYTES_TO_BLK(range->start);
2870 __u64 end = start + F2FS_BYTES_TO_BLK(range->len) - 1;
377224c4 2871 unsigned int start_segno, end_segno;
8412663d 2872 block_t start_block, end_block;
4b2fecc8 2873 struct cp_control cpc;
78997b56 2874 struct discard_policy dpolicy;
0ea80512 2875 unsigned long long trimmed = 0;
c34f42e2 2876 int err = 0;
b0332a0f 2877 bool need_align = f2fs_lfs_mode(sbi) && __is_large_section(sbi);
4b2fecc8 2878
836b5a63 2879 if (start >= MAX_BLKADDR(sbi) || range->len < sbi->blocksize)
4b2fecc8
JK
2880 return -EINVAL;
2881
3f16ecd9
CY
2882 if (end < MAIN_BLKADDR(sbi))
2883 goto out;
4b2fecc8 2884
ed214a11 2885 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) {
dcbb4c10 2886 f2fs_warn(sbi, "Found FS corruption, run fsck to fix.");
10f966bb 2887 return -EFSCORRUPTED;
ed214a11
YH
2888 }
2889
4b2fecc8 2890 /* start/end segment number in main_area */
7cd8558b
JK
2891 start_segno = (start <= MAIN_BLKADDR(sbi)) ? 0 : GET_SEGNO(sbi, start);
2892 end_segno = (end >= MAX_BLKADDR(sbi)) ? MAIN_SEGS(sbi) - 1 :
2893 GET_SEGNO(sbi, end);
ad6672bb
YS
2894 if (need_align) {
2895 start_segno = rounddown(start_segno, sbi->segs_per_sec);
2896 end_segno = roundup(end_segno + 1, sbi->segs_per_sec) - 1;
2897 }
8412663d 2898
4b2fecc8 2899 cpc.reason = CP_DISCARD;
836b5a63 2900 cpc.trim_minlen = max_t(__u64, 1, F2FS_BYTES_TO_BLK(range->minlen));
377224c4
CY
2901 cpc.trim_start = start_segno;
2902 cpc.trim_end = end_segno;
4b2fecc8 2903
377224c4
CY
2904 if (sbi->discard_blks == 0)
2905 goto out;
74fa5f3d 2906
fb24fea7 2907 down_write(&sbi->gc_lock);
4d57b86d 2908 err = f2fs_write_checkpoint(sbi, &cpc);
fb24fea7 2909 up_write(&sbi->gc_lock);
377224c4
CY
2910 if (err)
2911 goto out;
8412663d 2912
e555da9f
JK
2913 /*
2914 * We filed discard candidates, but actually we don't need to wait for
2915 * all of them, since they'll be issued in idle time along with runtime
2916 * discard option. User configuration looks like using runtime discard
2917 * or periodic fstrim instead of it.
2918 */
7d20c8ab 2919 if (f2fs_realtime_discard_enable(sbi))
5a615492
JK
2920 goto out;
2921
2922 start_block = START_BLOCK(sbi, start_segno);
2923 end_block = START_BLOCK(sbi, end_segno + 1);
2924
2925 __init_discard_policy(sbi, &dpolicy, DPOLICY_FSTRIM, cpc.trim_minlen);
01f9cf6d
CY
2926 trimmed = __issue_discard_cmd_range(sbi, &dpolicy,
2927 start_block, end_block);
5a615492 2928
01f9cf6d 2929 trimmed += __wait_discard_cmd_range(sbi, &dpolicy,
0ea80512 2930 start_block, end_block);
377224c4 2931out:
6eae2694
CY
2932 if (!err)
2933 range->len = F2FS_BLK_TO_BYTES(trimmed);
c34f42e2 2934 return err;
4b2fecc8
JK
2935}
2936
351df4b2
JK
2937static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
2938{
2939 struct curseg_info *curseg = CURSEG_I(sbi, type);
de881df9
AR
2940
2941 return curseg->next_blkoff < f2fs_usable_blks_in_seg(sbi,
2942 curseg->segno);
351df4b2
JK
2943}
2944
4d57b86d 2945int f2fs_rw_hint_to_seg_type(enum rw_hint hint)
4f0a03d3
HL
2946{
2947 switch (hint) {
2948 case WRITE_LIFE_SHORT:
2949 return CURSEG_HOT_DATA;
2950 case WRITE_LIFE_EXTREME:
2951 return CURSEG_COLD_DATA;
2952 default:
2953 return CURSEG_WARM_DATA;
2954 }
2955}
2956
0cdd3195
HL
2957/* This returns write hints for each segment type. This hints will be
2958 * passed down to block layer. There are mapping tables which depend on
2959 * the mount option 'whint_mode'.
2960 *
2961 * 1) whint_mode=off. F2FS only passes down WRITE_LIFE_NOT_SET.
2962 *
2963 * 2) whint_mode=user-based. F2FS tries to pass down hints given by users.
2964 *
2965 * User F2FS Block
2966 * ---- ---- -----
2967 * META WRITE_LIFE_NOT_SET
2968 * HOT_NODE "
2969 * WARM_NODE "
2970 * COLD_NODE "
2971 * ioctl(COLD) COLD_DATA WRITE_LIFE_EXTREME
2972 * extension list " "
2973 *
2974 * -- buffered io
2975 * WRITE_LIFE_EXTREME COLD_DATA WRITE_LIFE_EXTREME
2976 * WRITE_LIFE_SHORT HOT_DATA WRITE_LIFE_SHORT
2977 * WRITE_LIFE_NOT_SET WARM_DATA WRITE_LIFE_NOT_SET
2978 * WRITE_LIFE_NONE " "
2979 * WRITE_LIFE_MEDIUM " "
2980 * WRITE_LIFE_LONG " "
2981 *
2982 * -- direct io
2983 * WRITE_LIFE_EXTREME COLD_DATA WRITE_LIFE_EXTREME
2984 * WRITE_LIFE_SHORT HOT_DATA WRITE_LIFE_SHORT
2985 * WRITE_LIFE_NOT_SET WARM_DATA WRITE_LIFE_NOT_SET
2986 * WRITE_LIFE_NONE " WRITE_LIFE_NONE
2987 * WRITE_LIFE_MEDIUM " WRITE_LIFE_MEDIUM
2988 * WRITE_LIFE_LONG " WRITE_LIFE_LONG
2989 *
f2e703f9
HL
2990 * 3) whint_mode=fs-based. F2FS passes down hints with its policy.
2991 *
2992 * User F2FS Block
2993 * ---- ---- -----
2994 * META WRITE_LIFE_MEDIUM;
2995 * HOT_NODE WRITE_LIFE_NOT_SET
2996 * WARM_NODE "
2997 * COLD_NODE WRITE_LIFE_NONE
2998 * ioctl(COLD) COLD_DATA WRITE_LIFE_EXTREME
2999 * extension list " "
3000 *
3001 * -- buffered io
3002 * WRITE_LIFE_EXTREME COLD_DATA WRITE_LIFE_EXTREME
3003 * WRITE_LIFE_SHORT HOT_DATA WRITE_LIFE_SHORT
3004 * WRITE_LIFE_NOT_SET WARM_DATA WRITE_LIFE_LONG
3005 * WRITE_LIFE_NONE " "
3006 * WRITE_LIFE_MEDIUM " "
3007 * WRITE_LIFE_LONG " "
3008 *
3009 * -- direct io
3010 * WRITE_LIFE_EXTREME COLD_DATA WRITE_LIFE_EXTREME
3011 * WRITE_LIFE_SHORT HOT_DATA WRITE_LIFE_SHORT
3012 * WRITE_LIFE_NOT_SET WARM_DATA WRITE_LIFE_NOT_SET
3013 * WRITE_LIFE_NONE " WRITE_LIFE_NONE
3014 * WRITE_LIFE_MEDIUM " WRITE_LIFE_MEDIUM
3015 * WRITE_LIFE_LONG " WRITE_LIFE_LONG
0cdd3195
HL
3016 */
3017
4d57b86d 3018enum rw_hint f2fs_io_type_to_rw_hint(struct f2fs_sb_info *sbi,
0cdd3195
HL
3019 enum page_type type, enum temp_type temp)
3020{
63189b78 3021 if (F2FS_OPTION(sbi).whint_mode == WHINT_MODE_USER) {
0cdd3195 3022 if (type == DATA) {
f2e703f9 3023 if (temp == WARM)
0cdd3195 3024 return WRITE_LIFE_NOT_SET;
f2e703f9
HL
3025 else if (temp == HOT)
3026 return WRITE_LIFE_SHORT;
3027 else if (temp == COLD)
3028 return WRITE_LIFE_EXTREME;
0cdd3195
HL
3029 } else {
3030 return WRITE_LIFE_NOT_SET;
3031 }
63189b78 3032 } else if (F2FS_OPTION(sbi).whint_mode == WHINT_MODE_FS) {
f2e703f9
HL
3033 if (type == DATA) {
3034 if (temp == WARM)
3035 return WRITE_LIFE_LONG;
3036 else if (temp == HOT)
3037 return WRITE_LIFE_SHORT;
3038 else if (temp == COLD)
3039 return WRITE_LIFE_EXTREME;
3040 } else if (type == NODE) {
3041 if (temp == WARM || temp == HOT)
3042 return WRITE_LIFE_NOT_SET;
3043 else if (temp == COLD)
3044 return WRITE_LIFE_NONE;
3045 } else if (type == META) {
3046 return WRITE_LIFE_MEDIUM;
3047 }
0cdd3195 3048 }
f2e703f9 3049 return WRITE_LIFE_NOT_SET;
0cdd3195
HL
3050}
3051
81377bd6 3052static int __get_segment_type_2(struct f2fs_io_info *fio)
351df4b2 3053{
81377bd6 3054 if (fio->type == DATA)
351df4b2
JK
3055 return CURSEG_HOT_DATA;
3056 else
3057 return CURSEG_HOT_NODE;
3058}
3059
81377bd6 3060static int __get_segment_type_4(struct f2fs_io_info *fio)
351df4b2 3061{
81377bd6
JK
3062 if (fio->type == DATA) {
3063 struct inode *inode = fio->page->mapping->host;
351df4b2
JK
3064
3065 if (S_ISDIR(inode->i_mode))
3066 return CURSEG_HOT_DATA;
3067 else
3068 return CURSEG_COLD_DATA;
3069 } else {
81377bd6 3070 if (IS_DNODE(fio->page) && is_cold_node(fio->page))
a344b9fd 3071 return CURSEG_WARM_NODE;
351df4b2
JK
3072 else
3073 return CURSEG_COLD_NODE;
3074 }
3075}
3076
81377bd6 3077static int __get_segment_type_6(struct f2fs_io_info *fio)
351df4b2 3078{
81377bd6
JK
3079 if (fio->type == DATA) {
3080 struct inode *inode = fio->page->mapping->host;
351df4b2 3081
4c8ff709
CY
3082 if (is_cold_data(fio->page) || file_is_cold(inode) ||
3083 f2fs_compressed_file(inode))
351df4b2 3084 return CURSEG_COLD_DATA;
b6a06cbb 3085 if (file_is_hot(inode) ||
b4c3ca8b 3086 is_inode_flag_set(inode, FI_HOT_DATA) ||
2079f115
CY
3087 f2fs_is_atomic_file(inode) ||
3088 f2fs_is_volatile_file(inode))
ef095d19 3089 return CURSEG_HOT_DATA;
4d57b86d 3090 return f2fs_rw_hint_to_seg_type(inode->i_write_hint);
351df4b2 3091 } else {
81377bd6
JK
3092 if (IS_DNODE(fio->page))
3093 return is_cold_node(fio->page) ? CURSEG_WARM_NODE :
351df4b2 3094 CURSEG_HOT_NODE;
ef095d19 3095 return CURSEG_COLD_NODE;
351df4b2
JK
3096 }
3097}
3098
81377bd6 3099static int __get_segment_type(struct f2fs_io_info *fio)
351df4b2 3100{
a912b54d
JK
3101 int type = 0;
3102
63189b78 3103 switch (F2FS_OPTION(fio->sbi).active_logs) {
351df4b2 3104 case 2:
a912b54d
JK
3105 type = __get_segment_type_2(fio);
3106 break;
351df4b2 3107 case 4:
a912b54d
JK
3108 type = __get_segment_type_4(fio);
3109 break;
3110 case 6:
3111 type = __get_segment_type_6(fio);
3112 break;
3113 default:
3114 f2fs_bug_on(fio->sbi, true);
351df4b2 3115 }
81377bd6 3116
a912b54d
JK
3117 if (IS_HOT(type))
3118 fio->temp = HOT;
3119 else if (IS_WARM(type))
3120 fio->temp = WARM;
3121 else
3122 fio->temp = COLD;
3123 return type;
351df4b2
JK
3124}
3125
4d57b86d 3126void f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
bfad7c2d 3127 block_t old_blkaddr, block_t *new_blkaddr,
fb830fc5 3128 struct f2fs_summary *sum, int type,
f608c38c 3129 struct f2fs_io_info *fio)
351df4b2
JK
3130{
3131 struct sit_info *sit_i = SIT_I(sbi);
6ae1be13 3132 struct curseg_info *curseg = CURSEG_I(sbi, type);
f5a53edc
JK
3133 bool put_pin_sem = false;
3134
3135 if (type == CURSEG_COLD_DATA) {
3136 /* GC during CURSEG_COLD_DATA_PINNED allocation */
3137 if (down_read_trylock(&sbi->pin_sem)) {
3138 put_pin_sem = true;
3139 } else {
3140 type = CURSEG_WARM_DATA;
3141 curseg = CURSEG_I(sbi, type);
3142 }
3143 } else if (type == CURSEG_COLD_DATA_PINNED) {
3144 type = CURSEG_COLD_DATA;
3145 }
351df4b2 3146
2b60311d
CY
3147 down_read(&SM_I(sbi)->curseg_lock);
3148
351df4b2 3149 mutex_lock(&curseg->curseg_mutex);
3d26fa6b 3150 down_write(&sit_i->sentry_lock);
351df4b2
JK
3151
3152 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
351df4b2 3153
4e6a8d9b
JK
3154 f2fs_wait_discard_bio(sbi, *new_blkaddr);
3155
351df4b2
JK
3156 /*
3157 * __add_sum_entry should be resided under the curseg_mutex
3158 * because, this function updates a summary entry in the
3159 * current summary block.
3160 */
e79efe3b 3161 __add_sum_entry(sbi, type, sum);
351df4b2 3162
351df4b2 3163 __refresh_next_blkoff(sbi, curseg);
dcdfff65
JK
3164
3165 stat_inc_block_count(sbi, curseg);
351df4b2 3166
65f1b80b
YS
3167 /*
3168 * SIT information should be updated before segment allocation,
3169 * since SSR needs latest valid block information.
3170 */
3171 update_sit_entry(sbi, *new_blkaddr, 1);
3172 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
3173 update_sit_entry(sbi, old_blkaddr, -1);
3174
c6f82fe9
JK
3175 if (!__has_curseg_space(sbi, type))
3176 sit_i->s_ops->allocate_segment(sbi, type, false);
65f1b80b 3177
351df4b2 3178 /*
65f1b80b
YS
3179 * segment dirty status should be updated after segment allocation,
3180 * so we just need to update status only one time after previous
3181 * segment being closed.
351df4b2 3182 */
65f1b80b
YS
3183 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
3184 locate_dirty_segment(sbi, GET_SEGNO(sbi, *new_blkaddr));
5e443818 3185
3d26fa6b 3186 up_write(&sit_i->sentry_lock);
351df4b2 3187
704956ec 3188 if (page && IS_NODESEG(type)) {
351df4b2
JK
3189 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
3190
704956ec
CY
3191 f2fs_inode_chksum_set(sbi, page);
3192 }
3193
8223ecc4
CY
3194 if (F2FS_IO_ALIGNED(sbi))
3195 fio->retry = false;
3196
f608c38c 3197 if (fio) {
fb830fc5
CY
3198 struct f2fs_bio_info *io;
3199
3200 INIT_LIST_HEAD(&fio->list);
3201 fio->in_list = true;
3202 io = sbi->write_io[fio->type] + fio->temp;
3203 spin_lock(&io->io_lock);
3204 list_add_tail(&fio->list, &io->io_list);
3205 spin_unlock(&io->io_lock);
3206 }
3207
bfad7c2d 3208 mutex_unlock(&curseg->curseg_mutex);
2b60311d
CY
3209
3210 up_read(&SM_I(sbi)->curseg_lock);
f5a53edc
JK
3211
3212 if (put_pin_sem)
3213 up_read(&sbi->pin_sem);
bfad7c2d
JK
3214}
3215
39d787be
CY
3216static void update_device_state(struct f2fs_io_info *fio)
3217{
3218 struct f2fs_sb_info *sbi = fio->sbi;
3219 unsigned int devidx;
3220
0916878d 3221 if (!f2fs_is_multi_device(sbi))
39d787be
CY
3222 return;
3223
3224 devidx = f2fs_target_device_index(sbi, fio->new_blkaddr);
3225
3226 /* update device state for fsync */
4d57b86d 3227 f2fs_set_dirty_device(sbi, fio->ino, devidx, FLUSH_INO);
1228b482
CY
3228
3229 /* update device state for checkpoint */
3230 if (!f2fs_test_bit(devidx, (char *)&sbi->dirty_device)) {
3231 spin_lock(&sbi->dev_lock);
3232 f2fs_set_bit(devidx, (char *)&sbi->dirty_device);
3233 spin_unlock(&sbi->dev_lock);
3234 }
39d787be
CY
3235}
3236
05ca3632 3237static void do_write_page(struct f2fs_summary *sum, struct f2fs_io_info *fio)
bfad7c2d 3238{
81377bd6 3239 int type = __get_segment_type(fio);
b0332a0f 3240 bool keep_order = (f2fs_lfs_mode(fio->sbi) && type == CURSEG_COLD_DATA);
bfad7c2d 3241
107a805d
CY
3242 if (keep_order)
3243 down_read(&fio->sbi->io_order_lock);
0a595eba 3244reallocate:
4d57b86d 3245 f2fs_allocate_data_block(fio->sbi, fio->page, fio->old_blkaddr,
f608c38c 3246 &fio->new_blkaddr, sum, type, fio);
6aa58d8a
CY
3247 if (GET_SEGNO(fio->sbi, fio->old_blkaddr) != NULL_SEGNO)
3248 invalidate_mapping_pages(META_MAPPING(fio->sbi),
3249 fio->old_blkaddr, fio->old_blkaddr);
bfad7c2d 3250
351df4b2 3251 /* writeout dirty page into bdev */
fe16efe6
CY
3252 f2fs_submit_page_write(fio);
3253 if (fio->retry) {
0a595eba
JK
3254 fio->old_blkaddr = fio->new_blkaddr;
3255 goto reallocate;
3256 }
fe16efe6
CY
3257
3258 update_device_state(fio);
3259
107a805d
CY
3260 if (keep_order)
3261 up_read(&fio->sbi->io_order_lock);
351df4b2
JK
3262}
3263
4d57b86d 3264void f2fs_do_write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
b0af6d49 3265 enum iostat_type io_type)
351df4b2 3266{
458e6197 3267 struct f2fs_io_info fio = {
05ca3632 3268 .sbi = sbi,
458e6197 3269 .type = META,
0cdd3195 3270 .temp = HOT,
04d328de 3271 .op = REQ_OP_WRITE,
70fd7614 3272 .op_flags = REQ_SYNC | REQ_META | REQ_PRIO,
7a9d7548
CY
3273 .old_blkaddr = page->index,
3274 .new_blkaddr = page->index,
05ca3632 3275 .page = page,
4375a336 3276 .encrypted_page = NULL,
fb830fc5 3277 .in_list = false,
458e6197
JK
3278 };
3279
2b947003 3280 if (unlikely(page->index >= MAIN_BLKADDR(sbi)))
04d328de 3281 fio.op_flags &= ~REQ_META;
2b947003 3282
351df4b2 3283 set_page_writeback(page);
17c50035 3284 ClearPageError(page);
b9109b0e 3285 f2fs_submit_page_write(&fio);
b0af6d49 3286
b63e7be5 3287 stat_inc_meta_count(sbi, page->index);
b0af6d49 3288 f2fs_update_iostat(sbi, io_type, F2FS_BLKSIZE);
351df4b2
JK
3289}
3290
4d57b86d 3291void f2fs_do_write_node_page(unsigned int nid, struct f2fs_io_info *fio)
351df4b2
JK
3292{
3293 struct f2fs_summary sum;
05ca3632 3294
351df4b2 3295 set_summary(&sum, nid, 0, 0);
05ca3632 3296 do_write_page(&sum, fio);
b0af6d49
CY
3297
3298 f2fs_update_iostat(fio->sbi, fio->io_type, F2FS_BLKSIZE);
351df4b2
JK
3299}
3300
4d57b86d
CY
3301void f2fs_outplace_write_data(struct dnode_of_data *dn,
3302 struct f2fs_io_info *fio)
351df4b2 3303{
05ca3632 3304 struct f2fs_sb_info *sbi = fio->sbi;
351df4b2 3305 struct f2fs_summary sum;
351df4b2 3306
9850cf4a 3307 f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR);
7735730d 3308 set_summary(&sum, dn->nid, dn->ofs_in_node, fio->version);
05ca3632 3309 do_write_page(&sum, fio);
f28b3434 3310 f2fs_update_data_blkaddr(dn, fio->new_blkaddr);
b0af6d49
CY
3311
3312 f2fs_update_iostat(sbi, fio->io_type, F2FS_BLKSIZE);
351df4b2
JK
3313}
3314
4d57b86d 3315int f2fs_inplace_write_data(struct f2fs_io_info *fio)
351df4b2 3316{
b0af6d49 3317 int err;
d21b0f23 3318 struct f2fs_sb_info *sbi = fio->sbi;
05573d6c 3319 unsigned int segno;
b0af6d49 3320
7a9d7548 3321 fio->new_blkaddr = fio->old_blkaddr;
0cdd3195
HL
3322 /* i/o temperature is needed for passing down write hints */
3323 __get_segment_type(fio);
d21b0f23 3324
05573d6c
CY
3325 segno = GET_SEGNO(sbi, fio->new_blkaddr);
3326
3327 if (!IS_DATASEG(get_seg_entry(sbi, segno)->type)) {
3328 set_sbi_flag(sbi, SBI_NEED_FSCK);
2d821c12
CY
3329 f2fs_warn(sbi, "%s: incorrect segment(%u) type, run fsck to fix.",
3330 __func__, segno);
10f966bb 3331 return -EFSCORRUPTED;
05573d6c 3332 }
d21b0f23 3333
05ca3632 3334 stat_inc_inplace_blocks(fio->sbi);
b0af6d49 3335
0e7f4197 3336 if (fio->bio && !(SM_I(sbi)->ipu_policy & (1 << F2FS_IPU_NOCACHE)))
8648de2c
CY
3337 err = f2fs_merge_page_bio(fio);
3338 else
3339 err = f2fs_submit_page_bio(fio);
e46f6bd8 3340 if (!err) {
39d787be 3341 update_device_state(fio);
e46f6bd8
CY
3342 f2fs_update_iostat(fio->sbi, fio->io_type, F2FS_BLKSIZE);
3343 }
b0af6d49
CY
3344
3345 return err;
351df4b2
JK
3346}
3347
2b60311d
CY
3348static inline int __f2fs_get_curseg(struct f2fs_sb_info *sbi,
3349 unsigned int segno)
3350{
3351 int i;
3352
3353 for (i = CURSEG_HOT_DATA; i < NO_CHECK_TYPE; i++) {
3354 if (CURSEG_I(sbi, i)->segno == segno)
3355 break;
3356 }
3357 return i;
3358}
3359
4d57b86d 3360void f2fs_do_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
19f106bc 3361 block_t old_blkaddr, block_t new_blkaddr,
28bc106b 3362 bool recover_curseg, bool recover_newaddr)
351df4b2
JK
3363{
3364 struct sit_info *sit_i = SIT_I(sbi);
3365 struct curseg_info *curseg;
3366 unsigned int segno, old_cursegno;
3367 struct seg_entry *se;
3368 int type;
19f106bc 3369 unsigned short old_blkoff;
351df4b2
JK
3370
3371 segno = GET_SEGNO(sbi, new_blkaddr);
3372 se = get_seg_entry(sbi, segno);
3373 type = se->type;
3374
2b60311d
CY
3375 down_write(&SM_I(sbi)->curseg_lock);
3376
19f106bc
CY
3377 if (!recover_curseg) {
3378 /* for recovery flow */
3379 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
3380 if (old_blkaddr == NULL_ADDR)
3381 type = CURSEG_COLD_DATA;
3382 else
3383 type = CURSEG_WARM_DATA;
3384 }
3385 } else {
2b60311d
CY
3386 if (IS_CURSEG(sbi, segno)) {
3387 /* se->type is volatile as SSR allocation */
3388 type = __f2fs_get_curseg(sbi, segno);
3389 f2fs_bug_on(sbi, type == NO_CHECK_TYPE);
3390 } else {
351df4b2 3391 type = CURSEG_WARM_DATA;
2b60311d 3392 }
351df4b2 3393 }
19f106bc 3394
2c190504 3395 f2fs_bug_on(sbi, !IS_DATASEG(type));
351df4b2
JK
3396 curseg = CURSEG_I(sbi, type);
3397
3398 mutex_lock(&curseg->curseg_mutex);
3d26fa6b 3399 down_write(&sit_i->sentry_lock);
351df4b2
JK
3400
3401 old_cursegno = curseg->segno;
19f106bc 3402 old_blkoff = curseg->next_blkoff;
351df4b2
JK
3403
3404 /* change the current segment */
3405 if (segno != curseg->segno) {
3406 curseg->next_segno = segno;
025d63a4 3407 change_curseg(sbi, type);
351df4b2
JK
3408 }
3409
491c0854 3410 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
e79efe3b 3411 __add_sum_entry(sbi, type, sum);
351df4b2 3412
28bc106b 3413 if (!recover_curseg || recover_newaddr)
6e2c64ad 3414 update_sit_entry(sbi, new_blkaddr, 1);
6aa58d8a
CY
3415 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO) {
3416 invalidate_mapping_pages(META_MAPPING(sbi),
3417 old_blkaddr, old_blkaddr);
6e2c64ad 3418 update_sit_entry(sbi, old_blkaddr, -1);
6aa58d8a 3419 }
6e2c64ad
JK
3420
3421 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
3422 locate_dirty_segment(sbi, GET_SEGNO(sbi, new_blkaddr));
3423
351df4b2 3424 locate_dirty_segment(sbi, old_cursegno);
351df4b2 3425
19f106bc
CY
3426 if (recover_curseg) {
3427 if (old_cursegno != curseg->segno) {
3428 curseg->next_segno = old_cursegno;
025d63a4 3429 change_curseg(sbi, type);
19f106bc
CY
3430 }
3431 curseg->next_blkoff = old_blkoff;
3432 }
3433
3d26fa6b 3434 up_write(&sit_i->sentry_lock);
351df4b2 3435 mutex_unlock(&curseg->curseg_mutex);
2b60311d 3436 up_write(&SM_I(sbi)->curseg_lock);
351df4b2
JK
3437}
3438
528e3459
CY
3439void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
3440 block_t old_addr, block_t new_addr,
28bc106b
CY
3441 unsigned char version, bool recover_curseg,
3442 bool recover_newaddr)
528e3459
CY
3443{
3444 struct f2fs_summary sum;
3445
3446 set_summary(&sum, dn->nid, dn->ofs_in_node, version);
3447
4d57b86d 3448 f2fs_do_replace_block(sbi, &sum, old_addr, new_addr,
28bc106b 3449 recover_curseg, recover_newaddr);
528e3459 3450
f28b3434 3451 f2fs_update_data_blkaddr(dn, new_addr);
528e3459
CY
3452}
3453
93dfe2ac 3454void f2fs_wait_on_page_writeback(struct page *page,
bae0ee7a 3455 enum page_type type, bool ordered, bool locked)
93dfe2ac 3456{
93dfe2ac 3457 if (PageWriteback(page)) {
4081363f
JK
3458 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
3459
0b20fcec 3460 /* submit cached LFS IO */
bab475c5 3461 f2fs_submit_merged_write_cond(sbi, NULL, page, 0, type);
0b20fcec
CY
3462 /* sbumit cached IPU IO */
3463 f2fs_submit_merged_ipu_write(sbi, NULL, page);
bae0ee7a 3464 if (ordered) {
fec1d657 3465 wait_on_page_writeback(page);
bae0ee7a
CY
3466 f2fs_bug_on(sbi, locked && PageWriteback(page));
3467 } else {
fec1d657 3468 wait_for_stable_page(page);
bae0ee7a 3469 }
93dfe2ac
JK
3470 }
3471}
3472
0ded69f6 3473void f2fs_wait_on_block_writeback(struct inode *inode, block_t blkaddr)
08b39fbd 3474{
0ded69f6 3475 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
08b39fbd
CY
3476 struct page *cpage;
3477
0ded69f6
JK
3478 if (!f2fs_post_read_required(inode))
3479 return;
3480
93770ab7 3481 if (!__is_valid_data_blkaddr(blkaddr))
08b39fbd
CY
3482 return;
3483
08b39fbd
CY
3484 cpage = find_lock_page(META_MAPPING(sbi), blkaddr);
3485 if (cpage) {
bae0ee7a 3486 f2fs_wait_on_page_writeback(cpage, DATA, true, true);
08b39fbd
CY
3487 f2fs_put_page(cpage, 1);
3488 }
3489}
3490
1e78e8bd
ST
3491void f2fs_wait_on_block_writeback_range(struct inode *inode, block_t blkaddr,
3492 block_t len)
3493{
3494 block_t i;
3495
3496 for (i = 0; i < len; i++)
3497 f2fs_wait_on_block_writeback(inode, blkaddr + i);
3498}
3499
7735730d 3500static int read_compacted_summaries(struct f2fs_sb_info *sbi)
351df4b2
JK
3501{
3502 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
3503 struct curseg_info *seg_i;
3504 unsigned char *kaddr;
3505 struct page *page;
3506 block_t start;
3507 int i, j, offset;
3508
3509 start = start_sum_block(sbi);
3510
4d57b86d 3511 page = f2fs_get_meta_page(sbi, start++);
7735730d
CY
3512 if (IS_ERR(page))
3513 return PTR_ERR(page);
351df4b2
JK
3514 kaddr = (unsigned char *)page_address(page);
3515
3516 /* Step 1: restore nat cache */
3517 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
b7ad7512 3518 memcpy(seg_i->journal, kaddr, SUM_JOURNAL_SIZE);
351df4b2
JK
3519
3520 /* Step 2: restore sit cache */
3521 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
b7ad7512 3522 memcpy(seg_i->journal, kaddr + SUM_JOURNAL_SIZE, SUM_JOURNAL_SIZE);
351df4b2
JK
3523 offset = 2 * SUM_JOURNAL_SIZE;
3524
3525 /* Step 3: restore summary entries */
3526 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
3527 unsigned short blk_off;
3528 unsigned int segno;
3529
3530 seg_i = CURSEG_I(sbi, i);
3531 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
3532 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
3533 seg_i->next_segno = segno;
3534 reset_curseg(sbi, i, 0);
3535 seg_i->alloc_type = ckpt->alloc_type[i];
3536 seg_i->next_blkoff = blk_off;
3537
3538 if (seg_i->alloc_type == SSR)
3539 blk_off = sbi->blocks_per_seg;
3540
3541 for (j = 0; j < blk_off; j++) {
3542 struct f2fs_summary *s;
3543 s = (struct f2fs_summary *)(kaddr + offset);
3544 seg_i->sum_blk->entries[j] = *s;
3545 offset += SUMMARY_SIZE;
09cbfeaf 3546 if (offset + SUMMARY_SIZE <= PAGE_SIZE -
351df4b2
JK
3547 SUM_FOOTER_SIZE)
3548 continue;
3549
3550 f2fs_put_page(page, 1);
3551 page = NULL;
3552
4d57b86d 3553 page = f2fs_get_meta_page(sbi, start++);
7735730d
CY
3554 if (IS_ERR(page))
3555 return PTR_ERR(page);
351df4b2
JK
3556 kaddr = (unsigned char *)page_address(page);
3557 offset = 0;
3558 }
3559 }
3560 f2fs_put_page(page, 1);
7735730d 3561 return 0;
351df4b2
JK
3562}
3563
3564static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
3565{
3566 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
3567 struct f2fs_summary_block *sum;
3568 struct curseg_info *curseg;
3569 struct page *new;
3570 unsigned short blk_off;
3571 unsigned int segno = 0;
3572 block_t blk_addr = 0;
7735730d 3573 int err = 0;
351df4b2
JK
3574
3575 /* get segment number and block addr */
3576 if (IS_DATASEG(type)) {
3577 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
3578 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
3579 CURSEG_HOT_DATA]);
119ee914 3580 if (__exist_node_summaries(sbi))
351df4b2
JK
3581 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
3582 else
3583 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
3584 } else {
3585 segno = le32_to_cpu(ckpt->cur_node_segno[type -
3586 CURSEG_HOT_NODE]);
3587 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
3588 CURSEG_HOT_NODE]);
119ee914 3589 if (__exist_node_summaries(sbi))
351df4b2
JK
3590 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
3591 type - CURSEG_HOT_NODE);
3592 else
3593 blk_addr = GET_SUM_BLOCK(sbi, segno);
3594 }
3595
4d57b86d 3596 new = f2fs_get_meta_page(sbi, blk_addr);
7735730d
CY
3597 if (IS_ERR(new))
3598 return PTR_ERR(new);
351df4b2
JK
3599 sum = (struct f2fs_summary_block *)page_address(new);
3600
3601 if (IS_NODESEG(type)) {
119ee914 3602 if (__exist_node_summaries(sbi)) {
351df4b2
JK
3603 struct f2fs_summary *ns = &sum->entries[0];
3604 int i;
3605 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
3606 ns->version = 0;
3607 ns->ofs_in_node = 0;
3608 }
3609 } else {
7735730d
CY
3610 err = f2fs_restore_node_summary(sbi, segno, sum);
3611 if (err)
3612 goto out;
351df4b2
JK
3613 }
3614 }
3615
3616 /* set uncompleted segment to curseg */
3617 curseg = CURSEG_I(sbi, type);
3618 mutex_lock(&curseg->curseg_mutex);
b7ad7512
CY
3619
3620 /* update journal info */
3621 down_write(&curseg->journal_rwsem);
3622 memcpy(curseg->journal, &sum->journal, SUM_JOURNAL_SIZE);
3623 up_write(&curseg->journal_rwsem);
3624
3625 memcpy(curseg->sum_blk->entries, sum->entries, SUM_ENTRY_SIZE);
3626 memcpy(&curseg->sum_blk->footer, &sum->footer, SUM_FOOTER_SIZE);
351df4b2
JK
3627 curseg->next_segno = segno;
3628 reset_curseg(sbi, type, 0);
3629 curseg->alloc_type = ckpt->alloc_type[type];
3630 curseg->next_blkoff = blk_off;
3631 mutex_unlock(&curseg->curseg_mutex);
7735730d 3632out:
351df4b2 3633 f2fs_put_page(new, 1);
7735730d 3634 return err;
351df4b2
JK
3635}
3636
3637static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
3638{
21d3f8e1
JQ
3639 struct f2fs_journal *sit_j = CURSEG_I(sbi, CURSEG_COLD_DATA)->journal;
3640 struct f2fs_journal *nat_j = CURSEG_I(sbi, CURSEG_HOT_DATA)->journal;
351df4b2 3641 int type = CURSEG_HOT_DATA;
e4fc5fbf 3642 int err;
351df4b2 3643
aaec2b1d 3644 if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG)) {
4d57b86d 3645 int npages = f2fs_npages_for_summary_flush(sbi, true);
3fa06d7b
CY
3646
3647 if (npages >= 2)
4d57b86d 3648 f2fs_ra_meta_pages(sbi, start_sum_block(sbi), npages,
26879fb1 3649 META_CP, true);
3fa06d7b 3650
351df4b2 3651 /* restore for compacted data summary */
7735730d
CY
3652 err = read_compacted_summaries(sbi);
3653 if (err)
3654 return err;
351df4b2
JK
3655 type = CURSEG_HOT_NODE;
3656 }
3657
119ee914 3658 if (__exist_node_summaries(sbi))
4d57b86d 3659 f2fs_ra_meta_pages(sbi, sum_blk_addr(sbi, NR_CURSEG_TYPE, type),
26879fb1 3660 NR_CURSEG_TYPE - type, META_CP, true);
3fa06d7b 3661
e4fc5fbf
CY
3662 for (; type <= CURSEG_COLD_NODE; type++) {
3663 err = read_normal_summaries(sbi, type);
3664 if (err)
3665 return err;
3666 }
3667
21d3f8e1
JQ
3668 /* sanity check for summary blocks */
3669 if (nats_in_cursum(nat_j) > NAT_JOURNAL_ENTRIES ||
9227d522 3670 sits_in_cursum(sit_j) > SIT_JOURNAL_ENTRIES) {
dcbb4c10
JP
3671 f2fs_err(sbi, "invalid journal entries nats %u sits %u\n",
3672 nats_in_cursum(nat_j), sits_in_cursum(sit_j));
21d3f8e1 3673 return -EINVAL;
9227d522 3674 }
21d3f8e1 3675
351df4b2
JK
3676 return 0;
3677}
3678
3679static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
3680{
3681 struct page *page;
3682 unsigned char *kaddr;
3683 struct f2fs_summary *summary;
3684 struct curseg_info *seg_i;
3685 int written_size = 0;
3686 int i, j;
3687
4d57b86d 3688 page = f2fs_grab_meta_page(sbi, blkaddr++);
351df4b2 3689 kaddr = (unsigned char *)page_address(page);
81114baa 3690 memset(kaddr, 0, PAGE_SIZE);
351df4b2
JK
3691
3692 /* Step 1: write nat cache */
3693 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
b7ad7512 3694 memcpy(kaddr, seg_i->journal, SUM_JOURNAL_SIZE);
351df4b2
JK
3695 written_size += SUM_JOURNAL_SIZE;
3696
3697 /* Step 2: write sit cache */
3698 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
b7ad7512 3699 memcpy(kaddr + written_size, seg_i->journal, SUM_JOURNAL_SIZE);
351df4b2
JK
3700 written_size += SUM_JOURNAL_SIZE;
3701
351df4b2
JK
3702 /* Step 3: write summary entries */
3703 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
3704 unsigned short blkoff;
3705 seg_i = CURSEG_I(sbi, i);
3706 if (sbi->ckpt->alloc_type[i] == SSR)
3707 blkoff = sbi->blocks_per_seg;
3708 else
3709 blkoff = curseg_blkoff(sbi, i);
3710
3711 for (j = 0; j < blkoff; j++) {
3712 if (!page) {
4d57b86d 3713 page = f2fs_grab_meta_page(sbi, blkaddr++);
351df4b2 3714 kaddr = (unsigned char *)page_address(page);
81114baa 3715 memset(kaddr, 0, PAGE_SIZE);
351df4b2
JK
3716 written_size = 0;
3717 }
3718 summary = (struct f2fs_summary *)(kaddr + written_size);
3719 *summary = seg_i->sum_blk->entries[j];
3720 written_size += SUMMARY_SIZE;
351df4b2 3721
09cbfeaf 3722 if (written_size + SUMMARY_SIZE <= PAGE_SIZE -
351df4b2
JK
3723 SUM_FOOTER_SIZE)
3724 continue;
3725
e8d61a74 3726 set_page_dirty(page);
351df4b2
JK
3727 f2fs_put_page(page, 1);
3728 page = NULL;
3729 }
3730 }
e8d61a74
CY
3731 if (page) {
3732 set_page_dirty(page);
351df4b2 3733 f2fs_put_page(page, 1);
e8d61a74 3734 }
351df4b2
JK
3735}
3736
3737static void write_normal_summaries(struct f2fs_sb_info *sbi,
3738 block_t blkaddr, int type)
3739{
3740 int i, end;
3741 if (IS_DATASEG(type))
3742 end = type + NR_CURSEG_DATA_TYPE;
3743 else
3744 end = type + NR_CURSEG_NODE_TYPE;
3745
b7ad7512
CY
3746 for (i = type; i < end; i++)
3747 write_current_sum_page(sbi, i, blkaddr + (i - type));
351df4b2
JK
3748}
3749
4d57b86d 3750void f2fs_write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
351df4b2 3751{
aaec2b1d 3752 if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG))
351df4b2
JK
3753 write_compacted_summaries(sbi, start_blk);
3754 else
3755 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
3756}
3757
4d57b86d 3758void f2fs_write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
351df4b2 3759{
119ee914 3760 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
351df4b2
JK
3761}
3762
4d57b86d 3763int f2fs_lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
351df4b2
JK
3764 unsigned int val, int alloc)
3765{
3766 int i;
3767
3768 if (type == NAT_JOURNAL) {
dfc08a12
CY
3769 for (i = 0; i < nats_in_cursum(journal); i++) {
3770 if (le32_to_cpu(nid_in_journal(journal, i)) == val)
351df4b2
JK
3771 return i;
3772 }
dfc08a12
CY
3773 if (alloc && __has_cursum_space(journal, 1, NAT_JOURNAL))
3774 return update_nats_in_cursum(journal, 1);
351df4b2 3775 } else if (type == SIT_JOURNAL) {
dfc08a12
CY
3776 for (i = 0; i < sits_in_cursum(journal); i++)
3777 if (le32_to_cpu(segno_in_journal(journal, i)) == val)
351df4b2 3778 return i;
dfc08a12
CY
3779 if (alloc && __has_cursum_space(journal, 1, SIT_JOURNAL))
3780 return update_sits_in_cursum(journal, 1);
351df4b2
JK
3781 }
3782 return -1;
3783}
3784
3785static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
3786 unsigned int segno)
3787{
7735730d 3788 return f2fs_get_meta_page_nofail(sbi, current_sit_addr(sbi, segno));
351df4b2
JK
3789}
3790
3791static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
3792 unsigned int start)
3793{
3794 struct sit_info *sit_i = SIT_I(sbi);
068c3cd8 3795 struct page *page;
351df4b2 3796 pgoff_t src_off, dst_off;
351df4b2
JK
3797
3798 src_off = current_sit_addr(sbi, start);
3799 dst_off = next_sit_addr(sbi, src_off);
3800
4d57b86d 3801 page = f2fs_grab_meta_page(sbi, dst_off);
068c3cd8 3802 seg_info_to_sit_page(sbi, page, start);
351df4b2 3803
068c3cd8 3804 set_page_dirty(page);
351df4b2
JK
3805 set_to_next_sit(sit_i, start);
3806
068c3cd8 3807 return page;
351df4b2
JK
3808}
3809
184a5cd2
CY
3810static struct sit_entry_set *grab_sit_entry_set(void)
3811{
3812 struct sit_entry_set *ses =
80c54505 3813 f2fs_kmem_cache_alloc(sit_entry_set_slab, GFP_NOFS);
184a5cd2
CY
3814
3815 ses->entry_cnt = 0;
3816 INIT_LIST_HEAD(&ses->set_list);
3817 return ses;
3818}
3819
3820static void release_sit_entry_set(struct sit_entry_set *ses)
3821{
3822 list_del(&ses->set_list);
3823 kmem_cache_free(sit_entry_set_slab, ses);
3824}
3825
3826static void adjust_sit_entry_set(struct sit_entry_set *ses,
3827 struct list_head *head)
3828{
3829 struct sit_entry_set *next = ses;
3830
3831 if (list_is_last(&ses->set_list, head))
3832 return;
3833
3834 list_for_each_entry_continue(next, head, set_list)
3835 if (ses->entry_cnt <= next->entry_cnt)
3836 break;
3837
3838 list_move_tail(&ses->set_list, &next->set_list);
3839}
3840
3841static void add_sit_entry(unsigned int segno, struct list_head *head)
3842{
3843 struct sit_entry_set *ses;
3844 unsigned int start_segno = START_SEGNO(segno);
3845
3846 list_for_each_entry(ses, head, set_list) {
3847 if (ses->start_segno == start_segno) {
3848 ses->entry_cnt++;
3849 adjust_sit_entry_set(ses, head);
3850 return;
3851 }
3852 }
3853
3854 ses = grab_sit_entry_set();
3855
3856 ses->start_segno = start_segno;
3857 ses->entry_cnt++;
3858 list_add(&ses->set_list, head);
3859}
3860
3861static void add_sits_in_set(struct f2fs_sb_info *sbi)
3862{
3863 struct f2fs_sm_info *sm_info = SM_I(sbi);
3864 struct list_head *set_list = &sm_info->sit_entry_set;
3865 unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap;
184a5cd2
CY
3866 unsigned int segno;
3867
7cd8558b 3868 for_each_set_bit(segno, bitmap, MAIN_SEGS(sbi))
184a5cd2
CY
3869 add_sit_entry(segno, set_list);
3870}
3871
3872static void remove_sits_in_journal(struct f2fs_sb_info *sbi)
351df4b2
JK
3873{
3874 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
b7ad7512 3875 struct f2fs_journal *journal = curseg->journal;
351df4b2
JK
3876 int i;
3877
b7ad7512 3878 down_write(&curseg->journal_rwsem);
dfc08a12 3879 for (i = 0; i < sits_in_cursum(journal); i++) {
184a5cd2
CY
3880 unsigned int segno;
3881 bool dirtied;
3882
dfc08a12 3883 segno = le32_to_cpu(segno_in_journal(journal, i));
184a5cd2
CY
3884 dirtied = __mark_sit_entry_dirty(sbi, segno);
3885
3886 if (!dirtied)
3887 add_sit_entry(segno, &SM_I(sbi)->sit_entry_set);
351df4b2 3888 }
dfc08a12 3889 update_sits_in_cursum(journal, -i);
b7ad7512 3890 up_write(&curseg->journal_rwsem);
351df4b2
JK
3891}
3892
0a8165d7 3893/*
351df4b2
JK
3894 * CP calls this function, which flushes SIT entries including sit_journal,
3895 * and moves prefree segs to free segs.
3896 */
4d57b86d 3897void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
351df4b2
JK
3898{
3899 struct sit_info *sit_i = SIT_I(sbi);
3900 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
3901 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
b7ad7512 3902 struct f2fs_journal *journal = curseg->journal;
184a5cd2
CY
3903 struct sit_entry_set *ses, *tmp;
3904 struct list_head *head = &SM_I(sbi)->sit_entry_set;
04f0b2ea 3905 bool to_journal = !is_sbi_flag_set(sbi, SBI_IS_RESIZEFS);
4b2fecc8 3906 struct seg_entry *se;
351df4b2 3907
3d26fa6b 3908 down_write(&sit_i->sentry_lock);
351df4b2 3909
2b11a74b
WL
3910 if (!sit_i->dirty_sentries)
3911 goto out;
3912
351df4b2 3913 /*
184a5cd2
CY
3914 * add and account sit entries of dirty bitmap in sit entry
3915 * set temporarily
351df4b2 3916 */
184a5cd2 3917 add_sits_in_set(sbi);
351df4b2 3918
184a5cd2
CY
3919 /*
3920 * if there are no enough space in journal to store dirty sit
3921 * entries, remove all entries from journal and add and account
3922 * them in sit entry set.
3923 */
04f0b2ea
QS
3924 if (!__has_cursum_space(journal, sit_i->dirty_sentries, SIT_JOURNAL) ||
3925 !to_journal)
184a5cd2 3926 remove_sits_in_journal(sbi);
b2955550 3927
184a5cd2
CY
3928 /*
3929 * there are two steps to flush sit entries:
3930 * #1, flush sit entries to journal in current cold data summary block.
3931 * #2, flush sit entries to sit page.
3932 */
3933 list_for_each_entry_safe(ses, tmp, head, set_list) {
4a257ed6 3934 struct page *page = NULL;
184a5cd2
CY
3935 struct f2fs_sit_block *raw_sit = NULL;
3936 unsigned int start_segno = ses->start_segno;
3937 unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK,
7cd8558b 3938 (unsigned long)MAIN_SEGS(sbi));
184a5cd2
CY
3939 unsigned int segno = start_segno;
3940
3941 if (to_journal &&
dfc08a12 3942 !__has_cursum_space(journal, ses->entry_cnt, SIT_JOURNAL))
184a5cd2
CY
3943 to_journal = false;
3944
b7ad7512
CY
3945 if (to_journal) {
3946 down_write(&curseg->journal_rwsem);
3947 } else {
184a5cd2
CY
3948 page = get_next_sit_page(sbi, start_segno);
3949 raw_sit = page_address(page);
351df4b2 3950 }
351df4b2 3951
184a5cd2
CY
3952 /* flush dirty sit entries in region of current sit set */
3953 for_each_set_bit_from(segno, bitmap, end) {
3954 int offset, sit_offset;
4b2fecc8
JK
3955
3956 se = get_seg_entry(sbi, segno);
56b07e7e
ZZ
3957#ifdef CONFIG_F2FS_CHECK_FS
3958 if (memcmp(se->cur_valid_map, se->cur_valid_map_mir,
3959 SIT_VBLOCK_MAP_SIZE))
3960 f2fs_bug_on(sbi, 1);
3961#endif
184a5cd2
CY
3962
3963 /* add discard candidates */
c473f1a9 3964 if (!(cpc->reason & CP_DISCARD)) {
4b2fecc8 3965 cpc->trim_start = segno;
25290fa5 3966 add_discard_addrs(sbi, cpc, false);
4b2fecc8 3967 }
184a5cd2
CY
3968
3969 if (to_journal) {
4d57b86d 3970 offset = f2fs_lookup_journal_in_cursum(journal,
184a5cd2
CY
3971 SIT_JOURNAL, segno, 1);
3972 f2fs_bug_on(sbi, offset < 0);
dfc08a12 3973 segno_in_journal(journal, offset) =
184a5cd2
CY
3974 cpu_to_le32(segno);
3975 seg_info_to_raw_sit(se,
dfc08a12 3976 &sit_in_journal(journal, offset));
56b07e7e
ZZ
3977 check_block_count(sbi, segno,
3978 &sit_in_journal(journal, offset));
184a5cd2
CY
3979 } else {
3980 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
3981 seg_info_to_raw_sit(se,
3982 &raw_sit->entries[sit_offset]);
56b07e7e
ZZ
3983 check_block_count(sbi, segno,
3984 &raw_sit->entries[sit_offset]);
184a5cd2 3985 }
351df4b2 3986
184a5cd2
CY
3987 __clear_bit(segno, bitmap);
3988 sit_i->dirty_sentries--;
3989 ses->entry_cnt--;
351df4b2
JK
3990 }
3991
b7ad7512
CY
3992 if (to_journal)
3993 up_write(&curseg->journal_rwsem);
3994 else
184a5cd2
CY
3995 f2fs_put_page(page, 1);
3996
3997 f2fs_bug_on(sbi, ses->entry_cnt);
3998 release_sit_entry_set(ses);
351df4b2 3999 }
184a5cd2
CY
4000
4001 f2fs_bug_on(sbi, !list_empty(head));
4002 f2fs_bug_on(sbi, sit_i->dirty_sentries);
184a5cd2 4003out:
c473f1a9 4004 if (cpc->reason & CP_DISCARD) {
650d3c4e
YH
4005 __u64 trim_start = cpc->trim_start;
4006
4b2fecc8 4007 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++)
25290fa5 4008 add_discard_addrs(sbi, cpc, false);
650d3c4e
YH
4009
4010 cpc->trim_start = trim_start;
4b2fecc8 4011 }
3d26fa6b 4012 up_write(&sit_i->sentry_lock);
351df4b2 4013
351df4b2
JK
4014 set_prefree_as_free_segments(sbi);
4015}
4016
4017static int build_sit_info(struct f2fs_sb_info *sbi)
4018{
4019 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
351df4b2
JK
4020 struct sit_info *sit_i;
4021 unsigned int sit_segs, start;
2fde3dd1 4022 char *src_bitmap, *bitmap;
bbf9f7d9 4023 unsigned int bitmap_size, main_bitmap_size, sit_bitmap_size;
351df4b2
JK
4024
4025 /* allocate memory for SIT information */
acbf054d 4026 sit_i = f2fs_kzalloc(sbi, sizeof(struct sit_info), GFP_KERNEL);
351df4b2
JK
4027 if (!sit_i)
4028 return -ENOMEM;
4029
4030 SM_I(sbi)->sit_info = sit_i;
4031
9d2a789c
KC
4032 sit_i->sentries =
4033 f2fs_kvzalloc(sbi, array_size(sizeof(struct seg_entry),
4034 MAIN_SEGS(sbi)),
4035 GFP_KERNEL);
351df4b2
JK
4036 if (!sit_i->sentries)
4037 return -ENOMEM;
4038
bbf9f7d9
ST
4039 main_bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
4040 sit_i->dirty_sentries_bitmap = f2fs_kvzalloc(sbi, main_bitmap_size,
628b3d14 4041 GFP_KERNEL);
351df4b2
JK
4042 if (!sit_i->dirty_sentries_bitmap)
4043 return -ENOMEM;
4044
2fde3dd1
CY
4045#ifdef CONFIG_F2FS_CHECK_FS
4046 bitmap_size = MAIN_SEGS(sbi) * SIT_VBLOCK_MAP_SIZE * 4;
4047#else
4048 bitmap_size = MAIN_SEGS(sbi) * SIT_VBLOCK_MAP_SIZE * 3;
4049#endif
4050 sit_i->bitmap = f2fs_kvzalloc(sbi, bitmap_size, GFP_KERNEL);
4051 if (!sit_i->bitmap)
4052 return -ENOMEM;
4053
4054 bitmap = sit_i->bitmap;
4055
7cd8558b 4056 for (start = 0; start < MAIN_SEGS(sbi); start++) {
2fde3dd1
CY
4057 sit_i->sentries[start].cur_valid_map = bitmap;
4058 bitmap += SIT_VBLOCK_MAP_SIZE;
4059
4060 sit_i->sentries[start].ckpt_valid_map = bitmap;
4061 bitmap += SIT_VBLOCK_MAP_SIZE;
3e025740 4062
355e7891 4063#ifdef CONFIG_F2FS_CHECK_FS
2fde3dd1
CY
4064 sit_i->sentries[start].cur_valid_map_mir = bitmap;
4065 bitmap += SIT_VBLOCK_MAP_SIZE;
355e7891
CY
4066#endif
4067
2fde3dd1
CY
4068 sit_i->sentries[start].discard_map = bitmap;
4069 bitmap += SIT_VBLOCK_MAP_SIZE;
351df4b2
JK
4070 }
4071
acbf054d 4072 sit_i->tmp_map = f2fs_kzalloc(sbi, SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
60a3b782
JK
4073 if (!sit_i->tmp_map)
4074 return -ENOMEM;
4075
2c70c5e3 4076 if (__is_large_section(sbi)) {
9d2a789c
KC
4077 sit_i->sec_entries =
4078 f2fs_kvzalloc(sbi, array_size(sizeof(struct sec_entry),
4079 MAIN_SECS(sbi)),
4080 GFP_KERNEL);
351df4b2
JK
4081 if (!sit_i->sec_entries)
4082 return -ENOMEM;
4083 }
4084
4085 /* get information related with SIT */
4086 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
4087
4088 /* setup SIT bitmap from ckeckpoint pack */
bbf9f7d9 4089 sit_bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
351df4b2
JK
4090 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
4091
bbf9f7d9 4092 sit_i->sit_bitmap = kmemdup(src_bitmap, sit_bitmap_size, GFP_KERNEL);
ae27d62e 4093 if (!sit_i->sit_bitmap)
351df4b2 4094 return -ENOMEM;
351df4b2 4095
ae27d62e 4096#ifdef CONFIG_F2FS_CHECK_FS
bbf9f7d9
ST
4097 sit_i->sit_bitmap_mir = kmemdup(src_bitmap,
4098 sit_bitmap_size, GFP_KERNEL);
ae27d62e
CY
4099 if (!sit_i->sit_bitmap_mir)
4100 return -ENOMEM;
bbf9f7d9
ST
4101
4102 sit_i->invalid_segmap = f2fs_kvzalloc(sbi,
4103 main_bitmap_size, GFP_KERNEL);
4104 if (!sit_i->invalid_segmap)
4105 return -ENOMEM;
ae27d62e
CY
4106#endif
4107
351df4b2
JK
4108 /* init SIT information */
4109 sit_i->s_ops = &default_salloc_ops;
4110
4111 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
4112 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
c79b7ff1 4113 sit_i->written_valid_blocks = 0;
bbf9f7d9 4114 sit_i->bitmap_size = sit_bitmap_size;
351df4b2
JK
4115 sit_i->dirty_sentries = 0;
4116 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
4117 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
a7e679b5 4118 sit_i->mounted_time = ktime_get_boottime_seconds();
3d26fa6b 4119 init_rwsem(&sit_i->sentry_lock);
351df4b2
JK
4120 return 0;
4121}
4122
4123static int build_free_segmap(struct f2fs_sb_info *sbi)
4124{
351df4b2
JK
4125 struct free_segmap_info *free_i;
4126 unsigned int bitmap_size, sec_bitmap_size;
4127
4128 /* allocate memory for free segmap information */
acbf054d 4129 free_i = f2fs_kzalloc(sbi, sizeof(struct free_segmap_info), GFP_KERNEL);
351df4b2
JK
4130 if (!free_i)
4131 return -ENOMEM;
4132
4133 SM_I(sbi)->free_info = free_i;
4134
7cd8558b 4135 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
628b3d14 4136 free_i->free_segmap = f2fs_kvmalloc(sbi, bitmap_size, GFP_KERNEL);
351df4b2
JK
4137 if (!free_i->free_segmap)
4138 return -ENOMEM;
4139
7cd8558b 4140 sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
628b3d14 4141 free_i->free_secmap = f2fs_kvmalloc(sbi, sec_bitmap_size, GFP_KERNEL);
351df4b2
JK
4142 if (!free_i->free_secmap)
4143 return -ENOMEM;
4144
4145 /* set all segments as dirty temporarily */
4146 memset(free_i->free_segmap, 0xff, bitmap_size);
4147 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
4148
4149 /* init free segmap information */
7cd8558b 4150 free_i->start_segno = GET_SEGNO_FROM_SEG0(sbi, MAIN_BLKADDR(sbi));
351df4b2
JK
4151 free_i->free_segments = 0;
4152 free_i->free_sections = 0;
1a118ccf 4153 spin_lock_init(&free_i->segmap_lock);
351df4b2
JK
4154 return 0;
4155}
4156
4157static int build_curseg(struct f2fs_sb_info *sbi)
4158{
1042d60f 4159 struct curseg_info *array;
351df4b2
JK
4160 int i;
4161
026f0507
KC
4162 array = f2fs_kzalloc(sbi, array_size(NR_CURSEG_TYPE, sizeof(*array)),
4163 GFP_KERNEL);
351df4b2
JK
4164 if (!array)
4165 return -ENOMEM;
4166
4167 SM_I(sbi)->curseg_array = array;
4168
4169 for (i = 0; i < NR_CURSEG_TYPE; i++) {
4170 mutex_init(&array[i].curseg_mutex);
acbf054d 4171 array[i].sum_blk = f2fs_kzalloc(sbi, PAGE_SIZE, GFP_KERNEL);
351df4b2
JK
4172 if (!array[i].sum_blk)
4173 return -ENOMEM;
b7ad7512 4174 init_rwsem(&array[i].journal_rwsem);
acbf054d
CY
4175 array[i].journal = f2fs_kzalloc(sbi,
4176 sizeof(struct f2fs_journal), GFP_KERNEL);
b7ad7512
CY
4177 if (!array[i].journal)
4178 return -ENOMEM;
351df4b2
JK
4179 array[i].segno = NULL_SEGNO;
4180 array[i].next_blkoff = 0;
4181 }
4182 return restore_curseg_summaries(sbi);
4183}
4184
c39a1b34 4185static int build_sit_entries(struct f2fs_sb_info *sbi)
351df4b2
JK
4186{
4187 struct sit_info *sit_i = SIT_I(sbi);
4188 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
b7ad7512 4189 struct f2fs_journal *journal = curseg->journal;
9c094040
YH
4190 struct seg_entry *se;
4191 struct f2fs_sit_entry sit;
74de593a
CY
4192 int sit_blk_cnt = SIT_BLK_CNT(sbi);
4193 unsigned int i, start, end;
4194 unsigned int readed, start_blk = 0;
c39a1b34 4195 int err = 0;
8a29c126 4196 block_t total_node_blocks = 0;
351df4b2 4197
74de593a 4198 do {
4d57b86d 4199 readed = f2fs_ra_meta_pages(sbi, start_blk, BIO_MAX_PAGES,
664ba972 4200 META_SIT, true);
74de593a
CY
4201
4202 start = start_blk * sit_i->sents_per_block;
4203 end = (start_blk + readed) * sit_i->sents_per_block;
4204
7cd8558b 4205 for (; start < end && start < MAIN_SEGS(sbi); start++) {
74de593a 4206 struct f2fs_sit_block *sit_blk;
74de593a
CY
4207 struct page *page;
4208
9c094040 4209 se = &sit_i->sentries[start];
74de593a 4210 page = get_current_sit_page(sbi, start);
edc55aaf
JK
4211 if (IS_ERR(page))
4212 return PTR_ERR(page);
74de593a
CY
4213 sit_blk = (struct f2fs_sit_block *)page_address(page);
4214 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
4215 f2fs_put_page(page, 1);
d600af23 4216
c39a1b34
JK
4217 err = check_block_count(sbi, start, &sit);
4218 if (err)
4219 return err;
74de593a 4220 seg_info_from_raw_sit(se, &sit);
8a29c126
JK
4221 if (IS_NODESEG(se->type))
4222 total_node_blocks += se->valid_blocks;
a66cdd98
JK
4223
4224 /* build discard map only one time */
7d20c8ab
CY
4225 if (is_set_ckpt_flags(sbi, CP_TRIMMED_FLAG)) {
4226 memset(se->discard_map, 0xff,
4227 SIT_VBLOCK_MAP_SIZE);
4228 } else {
4229 memcpy(se->discard_map,
4230 se->cur_valid_map,
4231 SIT_VBLOCK_MAP_SIZE);
4232 sbi->discard_blks +=
4233 sbi->blocks_per_seg -
4234 se->valid_blocks;
3e025740 4235 }
a66cdd98 4236
2c70c5e3 4237 if (__is_large_section(sbi))
d600af23
CY
4238 get_sec_entry(sbi, start)->valid_blocks +=
4239 se->valid_blocks;
351df4b2 4240 }
74de593a
CY
4241 start_blk += readed;
4242 } while (start_blk < sit_blk_cnt);
d600af23
CY
4243
4244 down_read(&curseg->journal_rwsem);
4245 for (i = 0; i < sits_in_cursum(journal); i++) {
d600af23
CY
4246 unsigned int old_valid_blocks;
4247
4248 start = le32_to_cpu(segno_in_journal(journal, i));
b2ca374f 4249 if (start >= MAIN_SEGS(sbi)) {
dcbb4c10
JP
4250 f2fs_err(sbi, "Wrong journal entry on segno %u",
4251 start);
10f966bb 4252 err = -EFSCORRUPTED;
b2ca374f
JK
4253 break;
4254 }
4255
d600af23
CY
4256 se = &sit_i->sentries[start];
4257 sit = sit_in_journal(journal, i);
4258
4259 old_valid_blocks = se->valid_blocks;
8a29c126
JK
4260 if (IS_NODESEG(se->type))
4261 total_node_blocks -= old_valid_blocks;
d600af23 4262
c39a1b34
JK
4263 err = check_block_count(sbi, start, &sit);
4264 if (err)
4265 break;
d600af23 4266 seg_info_from_raw_sit(se, &sit);
8a29c126
JK
4267 if (IS_NODESEG(se->type))
4268 total_node_blocks += se->valid_blocks;
d600af23 4269
7d20c8ab
CY
4270 if (is_set_ckpt_flags(sbi, CP_TRIMMED_FLAG)) {
4271 memset(se->discard_map, 0xff, SIT_VBLOCK_MAP_SIZE);
4272 } else {
4273 memcpy(se->discard_map, se->cur_valid_map,
4274 SIT_VBLOCK_MAP_SIZE);
4275 sbi->discard_blks += old_valid_blocks;
4276 sbi->discard_blks -= se->valid_blocks;
d600af23
CY
4277 }
4278
2c70c5e3 4279 if (__is_large_section(sbi)) {
d600af23 4280 get_sec_entry(sbi, start)->valid_blocks +=
a9af3fdc
CY
4281 se->valid_blocks;
4282 get_sec_entry(sbi, start)->valid_blocks -=
4283 old_valid_blocks;
4284 }
d600af23
CY
4285 }
4286 up_read(&curseg->journal_rwsem);
8a29c126
JK
4287
4288 if (!err && total_node_blocks != valid_node_count(sbi)) {
dcbb4c10
JP
4289 f2fs_err(sbi, "SIT is corrupted node# %u vs %u",
4290 total_node_blocks, valid_node_count(sbi));
10f966bb 4291 err = -EFSCORRUPTED;
8a29c126
JK
4292 }
4293
c39a1b34 4294 return err;
351df4b2
JK
4295}
4296
4297static void init_free_segmap(struct f2fs_sb_info *sbi)
4298{
4299 unsigned int start;
4300 int type;
de881df9 4301 struct seg_entry *sentry;
351df4b2 4302
7cd8558b 4303 for (start = 0; start < MAIN_SEGS(sbi); start++) {
de881df9
AR
4304 if (f2fs_usable_blks_in_seg(sbi, start) == 0)
4305 continue;
4306 sentry = get_seg_entry(sbi, start);
351df4b2
JK
4307 if (!sentry->valid_blocks)
4308 __set_free(sbi, start);
c79b7ff1
JK
4309 else
4310 SIT_I(sbi)->written_valid_blocks +=
4311 sentry->valid_blocks;
351df4b2
JK
4312 }
4313
4314 /* set use the current segments */
4315 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
4316 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
4317 __set_test_and_inuse(sbi, curseg_t->segno);
4318 }
4319}
4320
4321static void init_dirty_segmap(struct f2fs_sb_info *sbi)
4322{
4323 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
4324 struct free_segmap_info *free_i = FREE_I(sbi);
da52f8ad 4325 unsigned int segno = 0, offset = 0, secno;
de881df9 4326 block_t valid_blocks, usable_blks_in_seg;
123aaf77 4327 block_t blks_per_sec = BLKS_PER_SEC(sbi);
351df4b2 4328
8736fbf0 4329 while (1) {
351df4b2 4330 /* find dirty segment based on free segmap */
7cd8558b
JK
4331 segno = find_next_inuse(free_i, MAIN_SEGS(sbi), offset);
4332 if (segno >= MAIN_SEGS(sbi))
351df4b2
JK
4333 break;
4334 offset = segno + 1;
302bd348 4335 valid_blocks = get_valid_blocks(sbi, segno, false);
de881df9
AR
4336 usable_blks_in_seg = f2fs_usable_blks_in_seg(sbi, segno);
4337 if (valid_blocks == usable_blks_in_seg || !valid_blocks)
351df4b2 4338 continue;
de881df9 4339 if (valid_blocks > usable_blks_in_seg) {
ec325b52
JK
4340 f2fs_bug_on(sbi, 1);
4341 continue;
4342 }
351df4b2
JK
4343 mutex_lock(&dirty_i->seglist_lock);
4344 __locate_dirty_segment(sbi, segno, DIRTY);
4345 mutex_unlock(&dirty_i->seglist_lock);
4346 }
da52f8ad
JQ
4347
4348 if (!__is_large_section(sbi))
4349 return;
4350
4351 mutex_lock(&dirty_i->seglist_lock);
4352 for (segno = 0; segno < MAIN_SECS(sbi); segno += blks_per_sec) {
4353 valid_blocks = get_valid_blocks(sbi, segno, true);
4354 secno = GET_SEC_FROM_SEG(sbi, segno);
4355
4356 if (!valid_blocks || valid_blocks == blks_per_sec)
4357 continue;
4358 if (IS_CURSEC(sbi, secno))
4359 continue;
4360 set_bit(secno, dirty_i->dirty_secmap);
4361 }
4362 mutex_unlock(&dirty_i->seglist_lock);
351df4b2
JK
4363}
4364
5ec4e49f 4365static int init_victim_secmap(struct f2fs_sb_info *sbi)
351df4b2
JK
4366{
4367 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
7cd8558b 4368 unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
351df4b2 4369
628b3d14 4370 dirty_i->victim_secmap = f2fs_kvzalloc(sbi, bitmap_size, GFP_KERNEL);
5ec4e49f 4371 if (!dirty_i->victim_secmap)
351df4b2
JK
4372 return -ENOMEM;
4373 return 0;
4374}
4375
4376static int build_dirty_segmap(struct f2fs_sb_info *sbi)
4377{
4378 struct dirty_seglist_info *dirty_i;
4379 unsigned int bitmap_size, i;
4380
4381 /* allocate memory for dirty segments list information */
acbf054d
CY
4382 dirty_i = f2fs_kzalloc(sbi, sizeof(struct dirty_seglist_info),
4383 GFP_KERNEL);
351df4b2
JK
4384 if (!dirty_i)
4385 return -ENOMEM;
4386
4387 SM_I(sbi)->dirty_info = dirty_i;
4388 mutex_init(&dirty_i->seglist_lock);
4389
7cd8558b 4390 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
351df4b2
JK
4391
4392 for (i = 0; i < NR_DIRTY_TYPE; i++) {
628b3d14
CY
4393 dirty_i->dirty_segmap[i] = f2fs_kvzalloc(sbi, bitmap_size,
4394 GFP_KERNEL);
351df4b2
JK
4395 if (!dirty_i->dirty_segmap[i])
4396 return -ENOMEM;
4397 }
4398
da52f8ad
JQ
4399 if (__is_large_section(sbi)) {
4400 bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
4401 dirty_i->dirty_secmap = f2fs_kvzalloc(sbi,
4402 bitmap_size, GFP_KERNEL);
4403 if (!dirty_i->dirty_secmap)
4404 return -ENOMEM;
4405 }
4406
351df4b2 4407 init_dirty_segmap(sbi);
5ec4e49f 4408 return init_victim_secmap(sbi);
351df4b2
JK
4409}
4410
c854f4d6
CY
4411static int sanity_check_curseg(struct f2fs_sb_info *sbi)
4412{
4413 int i;
4414
4415 /*
4416 * In LFS/SSR curseg, .next_blkoff should point to an unused blkaddr;
4417 * In LFS curseg, all blkaddr after .next_blkoff should be unused.
4418 */
4419 for (i = 0; i < NO_CHECK_TYPE; i++) {
4420 struct curseg_info *curseg = CURSEG_I(sbi, i);
4421 struct seg_entry *se = get_seg_entry(sbi, curseg->segno);
4422 unsigned int blkofs = curseg->next_blkoff;
4423
4424 if (f2fs_test_bit(blkofs, se->cur_valid_map))
4425 goto out;
4426
4427 if (curseg->alloc_type == SSR)
4428 continue;
4429
4430 for (blkofs += 1; blkofs < sbi->blocks_per_seg; blkofs++) {
4431 if (!f2fs_test_bit(blkofs, se->cur_valid_map))
4432 continue;
4433out:
dcbb4c10
JP
4434 f2fs_err(sbi,
4435 "Current segment's next free block offset is inconsistent with bitmap, logtype:%u, segno:%u, type:%u, next_blkoff:%u, blkofs:%u",
4436 i, curseg->segno, curseg->alloc_type,
4437 curseg->next_blkoff, blkofs);
10f966bb 4438 return -EFSCORRUPTED;
c854f4d6
CY
4439 }
4440 }
4441 return 0;
4442}
4443
c426d991
SK
4444#ifdef CONFIG_BLK_DEV_ZONED
4445
d508c94e
SK
4446static int check_zone_write_pointer(struct f2fs_sb_info *sbi,
4447 struct f2fs_dev_info *fdev,
4448 struct blk_zone *zone)
4449{
4450 unsigned int wp_segno, wp_blkoff, zone_secno, zone_segno, segno;
4451 block_t zone_block, wp_block, last_valid_block;
4452 unsigned int log_sectors_per_block = sbi->log_blocksize - SECTOR_SHIFT;
4453 int i, s, b, ret;
4454 struct seg_entry *se;
4455
4456 if (zone->type != BLK_ZONE_TYPE_SEQWRITE_REQ)
4457 return 0;
4458
4459 wp_block = fdev->start_blk + (zone->wp >> log_sectors_per_block);
4460 wp_segno = GET_SEGNO(sbi, wp_block);
4461 wp_blkoff = wp_block - START_BLOCK(sbi, wp_segno);
4462 zone_block = fdev->start_blk + (zone->start >> log_sectors_per_block);
4463 zone_segno = GET_SEGNO(sbi, zone_block);
4464 zone_secno = GET_SEC_FROM_SEG(sbi, zone_segno);
4465
4466 if (zone_segno >= MAIN_SEGS(sbi))
4467 return 0;
4468
4469 /*
4470 * Skip check of zones cursegs point to, since
4471 * fix_curseg_write_pointer() checks them.
4472 */
4473 for (i = 0; i < NO_CHECK_TYPE; i++)
4474 if (zone_secno == GET_SEC_FROM_SEG(sbi,
4475 CURSEG_I(sbi, i)->segno))
4476 return 0;
4477
4478 /*
4479 * Get last valid block of the zone.
4480 */
4481 last_valid_block = zone_block - 1;
4482 for (s = sbi->segs_per_sec - 1; s >= 0; s--) {
4483 segno = zone_segno + s;
4484 se = get_seg_entry(sbi, segno);
4485 for (b = sbi->blocks_per_seg - 1; b >= 0; b--)
4486 if (f2fs_test_bit(b, se->cur_valid_map)) {
4487 last_valid_block = START_BLOCK(sbi, segno) + b;
4488 break;
4489 }
4490 if (last_valid_block >= zone_block)
4491 break;
4492 }
4493
4494 /*
4495 * If last valid block is beyond the write pointer, report the
4496 * inconsistency. This inconsistency does not cause write error
4497 * because the zone will not be selected for write operation until
4498 * it get discarded. Just report it.
4499 */
4500 if (last_valid_block >= wp_block) {
4501 f2fs_notice(sbi, "Valid block beyond write pointer: "
4502 "valid block[0x%x,0x%x] wp[0x%x,0x%x]",
4503 GET_SEGNO(sbi, last_valid_block),
4504 GET_BLKOFF_FROM_SEG0(sbi, last_valid_block),
4505 wp_segno, wp_blkoff);
4506 return 0;
4507 }
4508
4509 /*
4510 * If there is no valid block in the zone and if write pointer is
4511 * not at zone start, reset the write pointer.
4512 */
4513 if (last_valid_block + 1 == zone_block && zone->wp != zone->start) {
4514 f2fs_notice(sbi,
4515 "Zone without valid block has non-zero write "
4516 "pointer. Reset the write pointer: wp[0x%x,0x%x]",
4517 wp_segno, wp_blkoff);
4518 ret = __f2fs_issue_discard_zone(sbi, fdev->bdev, zone_block,
4519 zone->len >> log_sectors_per_block);
4520 if (ret) {
4521 f2fs_err(sbi, "Discard zone failed: %s (errno=%d)",
4522 fdev->path, ret);
4523 return ret;
4524 }
4525 }
4526
4527 return 0;
4528}
4529
c426d991
SK
4530static struct f2fs_dev_info *get_target_zoned_dev(struct f2fs_sb_info *sbi,
4531 block_t zone_blkaddr)
4532{
4533 int i;
4534
4535 for (i = 0; i < sbi->s_ndevs; i++) {
4536 if (!bdev_is_zoned(FDEV(i).bdev))
4537 continue;
4538 if (sbi->s_ndevs == 1 || (FDEV(i).start_blk <= zone_blkaddr &&
4539 zone_blkaddr <= FDEV(i).end_blk))
4540 return &FDEV(i);
4541 }
4542
4543 return NULL;
4544}
4545
4546static int report_one_zone_cb(struct blk_zone *zone, unsigned int idx,
4547 void *data) {
4548 memcpy(data, zone, sizeof(struct blk_zone));
4549 return 0;
4550}
4551
4552static int fix_curseg_write_pointer(struct f2fs_sb_info *sbi, int type)
4553{
4554 struct curseg_info *cs = CURSEG_I(sbi, type);
4555 struct f2fs_dev_info *zbd;
4556 struct blk_zone zone;
4557 unsigned int cs_section, wp_segno, wp_blkoff, wp_sector_off;
4558 block_t cs_zone_block, wp_block;
4559 unsigned int log_sectors_per_block = sbi->log_blocksize - SECTOR_SHIFT;
4560 sector_t zone_sector;
4561 int err;
4562
4563 cs_section = GET_SEC_FROM_SEG(sbi, cs->segno);
4564 cs_zone_block = START_BLOCK(sbi, GET_SEG_FROM_SEC(sbi, cs_section));
4565
4566 zbd = get_target_zoned_dev(sbi, cs_zone_block);
4567 if (!zbd)
4568 return 0;
4569
4570 /* report zone for the sector the curseg points to */
4571 zone_sector = (sector_t)(cs_zone_block - zbd->start_blk)
4572 << log_sectors_per_block;
4573 err = blkdev_report_zones(zbd->bdev, zone_sector, 1,
4574 report_one_zone_cb, &zone);
4575 if (err != 1) {
4576 f2fs_err(sbi, "Report zone failed: %s errno=(%d)",
4577 zbd->path, err);
4578 return err;
4579 }
4580
4581 if (zone.type != BLK_ZONE_TYPE_SEQWRITE_REQ)
4582 return 0;
4583
4584 wp_block = zbd->start_blk + (zone.wp >> log_sectors_per_block);
4585 wp_segno = GET_SEGNO(sbi, wp_block);
4586 wp_blkoff = wp_block - START_BLOCK(sbi, wp_segno);
4587 wp_sector_off = zone.wp & GENMASK(log_sectors_per_block - 1, 0);
4588
4589 if (cs->segno == wp_segno && cs->next_blkoff == wp_blkoff &&
4590 wp_sector_off == 0)
4591 return 0;
4592
4593 f2fs_notice(sbi, "Unaligned curseg[%d] with write pointer: "
4594 "curseg[0x%x,0x%x] wp[0x%x,0x%x]",
4595 type, cs->segno, cs->next_blkoff, wp_segno, wp_blkoff);
4596
4597 f2fs_notice(sbi, "Assign new section to curseg[%d]: "
4598 "curseg[0x%x,0x%x]", type, cs->segno, cs->next_blkoff);
4599 allocate_segment_by_default(sbi, type, true);
4600
d508c94e
SK
4601 /* check consistency of the zone curseg pointed to */
4602 if (check_zone_write_pointer(sbi, zbd, &zone))
4603 return -EIO;
4604
c426d991
SK
4605 /* check newly assigned zone */
4606 cs_section = GET_SEC_FROM_SEG(sbi, cs->segno);
4607 cs_zone_block = START_BLOCK(sbi, GET_SEG_FROM_SEC(sbi, cs_section));
4608
4609 zbd = get_target_zoned_dev(sbi, cs_zone_block);
4610 if (!zbd)
4611 return 0;
4612
4613 zone_sector = (sector_t)(cs_zone_block - zbd->start_blk)
4614 << log_sectors_per_block;
4615 err = blkdev_report_zones(zbd->bdev, zone_sector, 1,
4616 report_one_zone_cb, &zone);
4617 if (err != 1) {
4618 f2fs_err(sbi, "Report zone failed: %s errno=(%d)",
4619 zbd->path, err);
4620 return err;
4621 }
4622
4623 if (zone.type != BLK_ZONE_TYPE_SEQWRITE_REQ)
4624 return 0;
4625
4626 if (zone.wp != zone.start) {
4627 f2fs_notice(sbi,
4628 "New zone for curseg[%d] is not yet discarded. "
4629 "Reset the zone: curseg[0x%x,0x%x]",
4630 type, cs->segno, cs->next_blkoff);
4631 err = __f2fs_issue_discard_zone(sbi, zbd->bdev,
4632 zone_sector >> log_sectors_per_block,
4633 zone.len >> log_sectors_per_block);
4634 if (err) {
4635 f2fs_err(sbi, "Discard zone failed: %s (errno=%d)",
4636 zbd->path, err);
4637 return err;
4638 }
4639 }
4640
4641 return 0;
4642}
4643
4644int f2fs_fix_curseg_write_pointer(struct f2fs_sb_info *sbi)
4645{
4646 int i, ret;
4647
4648 for (i = 0; i < NO_CHECK_TYPE; i++) {
4649 ret = fix_curseg_write_pointer(sbi, i);
4650 if (ret)
4651 return ret;
4652 }
4653
4654 return 0;
4655}
d508c94e
SK
4656
4657struct check_zone_write_pointer_args {
4658 struct f2fs_sb_info *sbi;
4659 struct f2fs_dev_info *fdev;
4660};
4661
4662static int check_zone_write_pointer_cb(struct blk_zone *zone, unsigned int idx,
4663 void *data) {
4664 struct check_zone_write_pointer_args *args;
4665 args = (struct check_zone_write_pointer_args *)data;
4666
4667 return check_zone_write_pointer(args->sbi, args->fdev, zone);
4668}
4669
4670int f2fs_check_write_pointer(struct f2fs_sb_info *sbi)
4671{
4672 int i, ret;
4673 struct check_zone_write_pointer_args args;
4674
4675 for (i = 0; i < sbi->s_ndevs; i++) {
4676 if (!bdev_is_zoned(FDEV(i).bdev))
4677 continue;
4678
4679 args.sbi = sbi;
4680 args.fdev = &FDEV(i);
4681 ret = blkdev_report_zones(FDEV(i).bdev, 0, BLK_ALL_ZONES,
4682 check_zone_write_pointer_cb, &args);
4683 if (ret < 0)
4684 return ret;
4685 }
4686
4687 return 0;
4688}
de881df9
AR
4689
4690static bool is_conv_zone(struct f2fs_sb_info *sbi, unsigned int zone_idx,
4691 unsigned int dev_idx)
4692{
4693 if (!bdev_is_zoned(FDEV(dev_idx).bdev))
4694 return true;
4695 return !test_bit(zone_idx, FDEV(dev_idx).blkz_seq);
4696}
4697
4698/* Return the zone index in the given device */
4699static unsigned int get_zone_idx(struct f2fs_sb_info *sbi, unsigned int secno,
4700 int dev_idx)
4701{
4702 block_t sec_start_blkaddr = START_BLOCK(sbi, GET_SEG_FROM_SEC(sbi, secno));
4703
4704 return (sec_start_blkaddr - FDEV(dev_idx).start_blk) >>
4705 sbi->log_blocks_per_blkz;
4706}
4707
4708/*
4709 * Return the usable segments in a section based on the zone's
4710 * corresponding zone capacity. Zone is equal to a section.
4711 */
4712static inline unsigned int f2fs_usable_zone_segs_in_sec(
4713 struct f2fs_sb_info *sbi, unsigned int segno)
4714{
4715 unsigned int dev_idx, zone_idx, unusable_segs_in_sec;
4716
4717 dev_idx = f2fs_target_device_index(sbi, START_BLOCK(sbi, segno));
4718 zone_idx = get_zone_idx(sbi, GET_SEC_FROM_SEG(sbi, segno), dev_idx);
4719
4720 /* Conventional zone's capacity is always equal to zone size */
4721 if (is_conv_zone(sbi, zone_idx, dev_idx))
4722 return sbi->segs_per_sec;
4723
4724 /*
4725 * If the zone_capacity_blocks array is NULL, then zone capacity
4726 * is equal to the zone size for all zones
4727 */
4728 if (!FDEV(dev_idx).zone_capacity_blocks)
4729 return sbi->segs_per_sec;
4730
4731 /* Get the segment count beyond zone capacity block */
4732 unusable_segs_in_sec = (sbi->blocks_per_blkz -
4733 FDEV(dev_idx).zone_capacity_blocks[zone_idx]) >>
4734 sbi->log_blocks_per_seg;
4735 return sbi->segs_per_sec - unusable_segs_in_sec;
4736}
4737
4738/*
4739 * Return the number of usable blocks in a segment. The number of blocks
4740 * returned is always equal to the number of blocks in a segment for
4741 * segments fully contained within a sequential zone capacity or a
4742 * conventional zone. For segments partially contained in a sequential
4743 * zone capacity, the number of usable blocks up to the zone capacity
4744 * is returned. 0 is returned in all other cases.
4745 */
4746static inline unsigned int f2fs_usable_zone_blks_in_seg(
4747 struct f2fs_sb_info *sbi, unsigned int segno)
4748{
4749 block_t seg_start, sec_start_blkaddr, sec_cap_blkaddr;
4750 unsigned int zone_idx, dev_idx, secno;
4751
4752 secno = GET_SEC_FROM_SEG(sbi, segno);
4753 seg_start = START_BLOCK(sbi, segno);
4754 dev_idx = f2fs_target_device_index(sbi, seg_start);
4755 zone_idx = get_zone_idx(sbi, secno, dev_idx);
4756
4757 /*
4758 * Conventional zone's capacity is always equal to zone size,
4759 * so, blocks per segment is unchanged.
4760 */
4761 if (is_conv_zone(sbi, zone_idx, dev_idx))
4762 return sbi->blocks_per_seg;
4763
4764 if (!FDEV(dev_idx).zone_capacity_blocks)
4765 return sbi->blocks_per_seg;
4766
4767 sec_start_blkaddr = START_BLOCK(sbi, GET_SEG_FROM_SEC(sbi, secno));
4768 sec_cap_blkaddr = sec_start_blkaddr +
4769 FDEV(dev_idx).zone_capacity_blocks[zone_idx];
4770
4771 /*
4772 * If segment starts before zone capacity and spans beyond
4773 * zone capacity, then usable blocks are from seg start to
4774 * zone capacity. If the segment starts after the zone capacity,
4775 * then there are no usable blocks.
4776 */
4777 if (seg_start >= sec_cap_blkaddr)
4778 return 0;
4779 if (seg_start + sbi->blocks_per_seg > sec_cap_blkaddr)
4780 return sec_cap_blkaddr - seg_start;
4781
4782 return sbi->blocks_per_seg;
4783}
c426d991
SK
4784#else
4785int f2fs_fix_curseg_write_pointer(struct f2fs_sb_info *sbi)
4786{
4787 return 0;
4788}
d508c94e
SK
4789
4790int f2fs_check_write_pointer(struct f2fs_sb_info *sbi)
4791{
4792 return 0;
4793}
de881df9
AR
4794
4795static inline unsigned int f2fs_usable_zone_blks_in_seg(struct f2fs_sb_info *sbi,
4796 unsigned int segno)
4797{
4798 return 0;
4799}
4800
4801static inline unsigned int f2fs_usable_zone_segs_in_sec(struct f2fs_sb_info *sbi,
4802 unsigned int segno)
4803{
4804 return 0;
4805}
c426d991 4806#endif
de881df9
AR
4807unsigned int f2fs_usable_blks_in_seg(struct f2fs_sb_info *sbi,
4808 unsigned int segno)
4809{
4810 if (f2fs_sb_has_blkzoned(sbi))
4811 return f2fs_usable_zone_blks_in_seg(sbi, segno);
4812
4813 return sbi->blocks_per_seg;
4814}
4815
4816unsigned int f2fs_usable_segs_in_sec(struct f2fs_sb_info *sbi,
4817 unsigned int segno)
4818{
4819 if (f2fs_sb_has_blkzoned(sbi))
4820 return f2fs_usable_zone_segs_in_sec(sbi, segno);
4821
4822 return sbi->segs_per_sec;
4823}
c426d991 4824
0a8165d7 4825/*
351df4b2
JK
4826 * Update min, max modified time for cost-benefit GC algorithm
4827 */
4828static void init_min_max_mtime(struct f2fs_sb_info *sbi)
4829{
4830 struct sit_info *sit_i = SIT_I(sbi);
4831 unsigned int segno;
4832
3d26fa6b 4833 down_write(&sit_i->sentry_lock);
351df4b2 4834
5ad25442 4835 sit_i->min_mtime = ULLONG_MAX;
351df4b2 4836
7cd8558b 4837 for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) {
351df4b2
JK
4838 unsigned int i;
4839 unsigned long long mtime = 0;
4840
4841 for (i = 0; i < sbi->segs_per_sec; i++)
4842 mtime += get_seg_entry(sbi, segno + i)->mtime;
4843
4844 mtime = div_u64(mtime, sbi->segs_per_sec);
4845
4846 if (sit_i->min_mtime > mtime)
4847 sit_i->min_mtime = mtime;
4848 }
a1f72ac2 4849 sit_i->max_mtime = get_mtime(sbi, false);
3d26fa6b 4850 up_write(&sit_i->sentry_lock);
351df4b2
JK
4851}
4852
4d57b86d 4853int f2fs_build_segment_manager(struct f2fs_sb_info *sbi)
351df4b2
JK
4854{
4855 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
4856 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1042d60f 4857 struct f2fs_sm_info *sm_info;
351df4b2
JK
4858 int err;
4859
acbf054d 4860 sm_info = f2fs_kzalloc(sbi, sizeof(struct f2fs_sm_info), GFP_KERNEL);
351df4b2
JK
4861 if (!sm_info)
4862 return -ENOMEM;
4863
4864 /* init sm info */
4865 sbi->sm_info = sm_info;
351df4b2
JK
4866 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
4867 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
4868 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
4869 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
4870 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
4871 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
4872 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
58c41035
JK
4873 sm_info->rec_prefree_segments = sm_info->main_segments *
4874 DEF_RECLAIM_PREFREE_SEGMENTS / 100;
44a83499
JK
4875 if (sm_info->rec_prefree_segments > DEF_MAX_RECLAIM_PREFREE_SEGMENTS)
4876 sm_info->rec_prefree_segments = DEF_MAX_RECLAIM_PREFREE_SEGMENTS;
4877
b0332a0f 4878 if (!f2fs_lfs_mode(sbi))
52763a4b 4879 sm_info->ipu_policy = 1 << F2FS_IPU_FSYNC;
216fbd64 4880 sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
c1ce1b02 4881 sm_info->min_fsync_blocks = DEF_MIN_FSYNC_BLOCKS;
853137ce 4882 sm_info->min_seq_blocks = sbi->blocks_per_seg * sbi->segs_per_sec;
ef095d19 4883 sm_info->min_hot_blocks = DEF_MIN_HOT_BLOCKS;
a2a12b67 4884 sm_info->min_ssr_sections = reserved_sections(sbi);
351df4b2 4885
184a5cd2
CY
4886 INIT_LIST_HEAD(&sm_info->sit_entry_set);
4887
2b60311d
CY
4888 init_rwsem(&sm_info->curseg_lock);
4889
d4fdf8ba 4890 if (!f2fs_readonly(sbi->sb)) {
4d57b86d 4891 err = f2fs_create_flush_cmd_control(sbi);
2163d198 4892 if (err)
a688b9d9 4893 return err;
6b4afdd7
JK
4894 }
4895
0b54fb84
JK
4896 err = create_discard_cmd_control(sbi);
4897 if (err)
4898 return err;
4899
351df4b2
JK
4900 err = build_sit_info(sbi);
4901 if (err)
4902 return err;
4903 err = build_free_segmap(sbi);
4904 if (err)
4905 return err;
4906 err = build_curseg(sbi);
4907 if (err)
4908 return err;
4909
4910 /* reinit free segmap based on SIT */
c39a1b34
JK
4911 err = build_sit_entries(sbi);
4912 if (err)
4913 return err;
351df4b2
JK
4914
4915 init_free_segmap(sbi);
4916 err = build_dirty_segmap(sbi);
4917 if (err)
4918 return err;
4919
c854f4d6
CY
4920 err = sanity_check_curseg(sbi);
4921 if (err)
4922 return err;
4923
351df4b2
JK
4924 init_min_max_mtime(sbi);
4925 return 0;
4926}
4927
4928static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
4929 enum dirty_type dirty_type)
4930{
4931 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
4932
4933 mutex_lock(&dirty_i->seglist_lock);
39307a8e 4934 kvfree(dirty_i->dirty_segmap[dirty_type]);
351df4b2
JK
4935 dirty_i->nr_dirty[dirty_type] = 0;
4936 mutex_unlock(&dirty_i->seglist_lock);
4937}
4938
5ec4e49f 4939static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
351df4b2
JK
4940{
4941 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
39307a8e 4942 kvfree(dirty_i->victim_secmap);
351df4b2
JK
4943}
4944
4945static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
4946{
4947 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
4948 int i;
4949
4950 if (!dirty_i)
4951 return;
4952
4953 /* discard pre-free/dirty segments list */
4954 for (i = 0; i < NR_DIRTY_TYPE; i++)
4955 discard_dirty_segmap(sbi, i);
4956
da52f8ad
JQ
4957 if (__is_large_section(sbi)) {
4958 mutex_lock(&dirty_i->seglist_lock);
4959 kvfree(dirty_i->dirty_secmap);
4960 mutex_unlock(&dirty_i->seglist_lock);
4961 }
4962
5ec4e49f 4963 destroy_victim_secmap(sbi);
351df4b2 4964 SM_I(sbi)->dirty_info = NULL;
5222595d 4965 kvfree(dirty_i);
351df4b2
JK
4966}
4967
4968static void destroy_curseg(struct f2fs_sb_info *sbi)
4969{
4970 struct curseg_info *array = SM_I(sbi)->curseg_array;
4971 int i;
4972
4973 if (!array)
4974 return;
4975 SM_I(sbi)->curseg_array = NULL;
b7ad7512 4976 for (i = 0; i < NR_CURSEG_TYPE; i++) {
5222595d
JK
4977 kvfree(array[i].sum_blk);
4978 kvfree(array[i].journal);
b7ad7512 4979 }
5222595d 4980 kvfree(array);
351df4b2
JK
4981}
4982
4983static void destroy_free_segmap(struct f2fs_sb_info *sbi)
4984{
4985 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
4986 if (!free_i)
4987 return;
4988 SM_I(sbi)->free_info = NULL;
39307a8e
JK
4989 kvfree(free_i->free_segmap);
4990 kvfree(free_i->free_secmap);
5222595d 4991 kvfree(free_i);
351df4b2
JK
4992}
4993
4994static void destroy_sit_info(struct f2fs_sb_info *sbi)
4995{
4996 struct sit_info *sit_i = SIT_I(sbi);
351df4b2
JK
4997
4998 if (!sit_i)
4999 return;
5000
2fde3dd1
CY
5001 if (sit_i->sentries)
5002 kvfree(sit_i->bitmap);
5222595d 5003 kvfree(sit_i->tmp_map);
60a3b782 5004
39307a8e
JK
5005 kvfree(sit_i->sentries);
5006 kvfree(sit_i->sec_entries);
5007 kvfree(sit_i->dirty_sentries_bitmap);
351df4b2
JK
5008
5009 SM_I(sbi)->sit_info = NULL;
5222595d 5010 kvfree(sit_i->sit_bitmap);
ae27d62e 5011#ifdef CONFIG_F2FS_CHECK_FS
5222595d 5012 kvfree(sit_i->sit_bitmap_mir);
bbf9f7d9 5013 kvfree(sit_i->invalid_segmap);
ae27d62e 5014#endif
5222595d 5015 kvfree(sit_i);
351df4b2
JK
5016}
5017
4d57b86d 5018void f2fs_destroy_segment_manager(struct f2fs_sb_info *sbi)
351df4b2
JK
5019{
5020 struct f2fs_sm_info *sm_info = SM_I(sbi);
a688b9d9 5021
3b03f724
CY
5022 if (!sm_info)
5023 return;
4d57b86d 5024 f2fs_destroy_flush_cmd_control(sbi, true);
f099405f 5025 destroy_discard_cmd_control(sbi);
351df4b2
JK
5026 destroy_dirty_segmap(sbi);
5027 destroy_curseg(sbi);
5028 destroy_free_segmap(sbi);
5029 destroy_sit_info(sbi);
5030 sbi->sm_info = NULL;
5222595d 5031 kvfree(sm_info);
351df4b2 5032}
7fd9e544 5033
4d57b86d 5034int __init f2fs_create_segment_manager_caches(void)
7fd9e544 5035{
98510003 5036 discard_entry_slab = f2fs_kmem_cache_create("f2fs_discard_entry",
e8512d2e 5037 sizeof(struct discard_entry));
7fd9e544 5038 if (!discard_entry_slab)
184a5cd2
CY
5039 goto fail;
5040
98510003 5041 discard_cmd_slab = f2fs_kmem_cache_create("f2fs_discard_cmd",
b01a9201
JK
5042 sizeof(struct discard_cmd));
5043 if (!discard_cmd_slab)
6ab2a308 5044 goto destroy_discard_entry;
275b66b0 5045
98510003 5046 sit_entry_set_slab = f2fs_kmem_cache_create("f2fs_sit_entry_set",
c9ee0085 5047 sizeof(struct sit_entry_set));
184a5cd2 5048 if (!sit_entry_set_slab)
b01a9201 5049 goto destroy_discard_cmd;
88b88a66 5050
98510003 5051 inmem_entry_slab = f2fs_kmem_cache_create("f2fs_inmem_page_entry",
88b88a66
JK
5052 sizeof(struct inmem_pages));
5053 if (!inmem_entry_slab)
5054 goto destroy_sit_entry_set;
7fd9e544 5055 return 0;
184a5cd2 5056
88b88a66
JK
5057destroy_sit_entry_set:
5058 kmem_cache_destroy(sit_entry_set_slab);
b01a9201
JK
5059destroy_discard_cmd:
5060 kmem_cache_destroy(discard_cmd_slab);
6ab2a308 5061destroy_discard_entry:
184a5cd2
CY
5062 kmem_cache_destroy(discard_entry_slab);
5063fail:
5064 return -ENOMEM;
7fd9e544
JK
5065}
5066
4d57b86d 5067void f2fs_destroy_segment_manager_caches(void)
7fd9e544 5068{
184a5cd2 5069 kmem_cache_destroy(sit_entry_set_slab);
b01a9201 5070 kmem_cache_destroy(discard_cmd_slab);
7fd9e544 5071 kmem_cache_destroy(discard_entry_slab);
88b88a66 5072 kmem_cache_destroy(inmem_entry_slab);
7fd9e544 5073}