]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/f2fs/segment.c
f2fs: fix to avoid overflow when left shifting page offset
[mirror_ubuntu-artful-kernel.git] / fs / f2fs / segment.c
CommitLineData
0a8165d7 1/*
351df4b2
JK
2 * fs/f2fs/segment.c
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include <linux/fs.h>
12#include <linux/f2fs_fs.h>
13#include <linux/bio.h>
14#include <linux/blkdev.h>
690e4a3e 15#include <linux/prefetch.h>
6b4afdd7 16#include <linux/kthread.h>
74de593a 17#include <linux/swap.h>
60b99b48 18#include <linux/timer.h>
351df4b2
JK
19
20#include "f2fs.h"
21#include "segment.h"
22#include "node.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
88b88a66
JK
169void register_inmem_page(struct inode *inode, struct page *page)
170{
171 struct f2fs_inode_info *fi = F2FS_I(inode);
172 struct inmem_pages *new;
9be32d72 173
9e4ded3f 174 f2fs_trace_pid(page);
0722b101 175
decd36b6
CY
176 set_page_private(page, (unsigned long)ATOMIC_WRITTEN_PAGE);
177 SetPagePrivate(page);
178
88b88a66
JK
179 new = f2fs_kmem_cache_alloc(inmem_entry_slab, GFP_NOFS);
180
181 /* add atomic page indices to the list */
182 new->page = page;
183 INIT_LIST_HEAD(&new->list);
decd36b6 184
88b88a66
JK
185 /* increase reference count with clean state */
186 mutex_lock(&fi->inmem_lock);
187 get_page(page);
188 list_add_tail(&new->list, &fi->inmem_pages);
8dcf2ff7 189 inc_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
88b88a66 190 mutex_unlock(&fi->inmem_lock);
8ce67cb0
JK
191
192 trace_f2fs_register_inmem_page(page, INMEM);
88b88a66
JK
193}
194
28bc106b
CY
195static int __revoke_inmem_pages(struct inode *inode,
196 struct list_head *head, bool drop, bool recover)
29b96b54 197{
28bc106b 198 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
29b96b54 199 struct inmem_pages *cur, *tmp;
28bc106b 200 int err = 0;
29b96b54
CY
201
202 list_for_each_entry_safe(cur, tmp, head, list) {
28bc106b
CY
203 struct page *page = cur->page;
204
205 if (drop)
206 trace_f2fs_commit_inmem_page(page, INMEM_DROP);
207
208 lock_page(page);
29b96b54 209
28bc106b
CY
210 if (recover) {
211 struct dnode_of_data dn;
212 struct node_info ni;
213
214 trace_f2fs_commit_inmem_page(page, INMEM_REVOKE);
215
216 set_new_dnode(&dn, inode, NULL, NULL, 0);
217 if (get_dnode_of_data(&dn, page->index, LOOKUP_NODE)) {
218 err = -EAGAIN;
219 goto next;
220 }
221 get_node_info(sbi, dn.nid, &ni);
222 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
223 cur->old_addr, ni.version, true, true);
224 f2fs_put_dnode(&dn);
225 }
226next:
63c52d78
JK
227 /* we don't need to invalidate this in the sccessful status */
228 if (drop || recover)
229 ClearPageUptodate(page);
28bc106b 230 set_page_private(page, 0);
c81ced05 231 ClearPagePrivate(page);
28bc106b 232 f2fs_put_page(page, 1);
29b96b54
CY
233
234 list_del(&cur->list);
235 kmem_cache_free(inmem_entry_slab, cur);
236 dec_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
237 }
28bc106b 238 return err;
29b96b54
CY
239}
240
241void drop_inmem_pages(struct inode *inode)
242{
243 struct f2fs_inode_info *fi = F2FS_I(inode);
244
245 mutex_lock(&fi->inmem_lock);
28bc106b 246 __revoke_inmem_pages(inode, &fi->inmem_pages, true, false);
29b96b54 247 mutex_unlock(&fi->inmem_lock);
5fe45743
CY
248
249 clear_inode_flag(inode, FI_ATOMIC_FILE);
250 stat_dec_atomic_write(inode);
29b96b54
CY
251}
252
28bc106b
CY
253static int __commit_inmem_pages(struct inode *inode,
254 struct list_head *revoke_list)
88b88a66
JK
255{
256 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
257 struct f2fs_inode_info *fi = F2FS_I(inode);
258 struct inmem_pages *cur, *tmp;
88b88a66 259 struct f2fs_io_info fio = {
05ca3632 260 .sbi = sbi,
88b88a66 261 .type = DATA,
04d328de 262 .op = REQ_OP_WRITE,
70fd7614 263 .op_flags = REQ_SYNC | REQ_PRIO,
4375a336 264 .encrypted_page = NULL,
88b88a66 265 };
29b96b54 266 bool submit_bio = false;
edb27dee 267 int err = 0;
88b88a66 268
88b88a66 269 list_for_each_entry_safe(cur, tmp, &fi->inmem_pages, list) {
28bc106b
CY
270 struct page *page = cur->page;
271
272 lock_page(page);
273 if (page->mapping == inode->i_mapping) {
274 trace_f2fs_commit_inmem_page(page, INMEM);
275
276 set_page_dirty(page);
277 f2fs_wait_on_page_writeback(page, DATA, true);
933439c8 278 if (clear_page_dirty_for_io(page)) {
29b96b54 279 inode_dec_dirty_pages(inode);
933439c8
CY
280 remove_dirty_inode(inode);
281 }
28bc106b
CY
282
283 fio.page = page;
29b96b54
CY
284 err = do_write_data_page(&fio);
285 if (err) {
28bc106b 286 unlock_page(page);
29b96b54 287 break;
70c640b1 288 }
29b96b54 289
28bc106b
CY
290 /* record old blkaddr for revoking */
291 cur->old_addr = fio.old_blkaddr;
decd36b6 292
28bc106b
CY
293 submit_bio = true;
294 }
295 unlock_page(page);
296 list_move_tail(&cur->list, revoke_list);
88b88a66 297 }
29b96b54
CY
298
299 if (submit_bio)
300 f2fs_submit_merged_bio_cond(sbi, inode, NULL, 0, DATA, WRITE);
28bc106b
CY
301
302 if (!err)
303 __revoke_inmem_pages(inode, revoke_list, false, false);
304
29b96b54
CY
305 return err;
306}
307
308int commit_inmem_pages(struct inode *inode)
309{
310 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
311 struct f2fs_inode_info *fi = F2FS_I(inode);
28bc106b
CY
312 struct list_head revoke_list;
313 int err;
29b96b54 314
28bc106b 315 INIT_LIST_HEAD(&revoke_list);
29b96b54
CY
316 f2fs_balance_fs(sbi, true);
317 f2fs_lock_op(sbi);
318
5fe45743
CY
319 set_inode_flag(inode, FI_ATOMIC_COMMIT);
320
29b96b54 321 mutex_lock(&fi->inmem_lock);
28bc106b
CY
322 err = __commit_inmem_pages(inode, &revoke_list);
323 if (err) {
324 int ret;
325 /*
326 * try to revoke all committed pages, but still we could fail
327 * due to no memory or other reason, if that happened, EAGAIN
328 * will be returned, which means in such case, transaction is
329 * already not integrity, caller should use journal to do the
330 * recovery or rewrite & commit last transaction. For other
331 * error number, revoking was done by filesystem itself.
332 */
333 ret = __revoke_inmem_pages(inode, &revoke_list, false, true);
334 if (ret)
335 err = ret;
336
337 /* drop all uncommitted pages */
338 __revoke_inmem_pages(inode, &fi->inmem_pages, true, false);
339 }
88b88a66
JK
340 mutex_unlock(&fi->inmem_lock);
341
5fe45743
CY
342 clear_inode_flag(inode, FI_ATOMIC_COMMIT);
343
29b96b54 344 f2fs_unlock_op(sbi);
edb27dee 345 return err;
88b88a66
JK
346}
347
0a8165d7 348/*
351df4b2
JK
349 * This function balances dirty node and dentry pages.
350 * In addition, it controls garbage collection.
351 */
2c4db1a6 352void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need)
351df4b2 353{
0f348028
CY
354#ifdef CONFIG_F2FS_FAULT_INJECTION
355 if (time_to_inject(sbi, FAULT_CHECKPOINT))
356 f2fs_stop_checkpoint(sbi, false);
357#endif
358
2c4db1a6
JK
359 if (!need)
360 return;
e589c2c4
JK
361
362 /* balance_fs_bg is able to be pending */
363 if (excess_cached_nats(sbi))
364 f2fs_balance_fs_bg(sbi);
365
351df4b2 366 /*
029cd28c
JK
367 * We should do GC or end up with checkpoint, if there are so many dirty
368 * dir/node pages without enough free segments.
351df4b2 369 */
7f3037a5 370 if (has_not_enough_free_secs(sbi, 0, 0)) {
351df4b2 371 mutex_lock(&sbi->gc_mutex);
7702bdbe 372 f2fs_gc(sbi, false, false);
351df4b2
JK
373 }
374}
375
4660f9c0
JK
376void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi)
377{
1dcc336b 378 /* try to shrink extent cache when there is no enough memory */
554df79e
JK
379 if (!available_free_memory(sbi, EXTENT_CACHE))
380 f2fs_shrink_extent_tree(sbi, EXTENT_CACHE_SHRINK_NUMBER);
1dcc336b 381
1b38dc8e
JK
382 /* check the # of cached NAT entries */
383 if (!available_free_memory(sbi, NAT_ENTRIES))
384 try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK);
385
31696580 386 if (!available_free_memory(sbi, FREE_NIDS))
ad4edb83
JK
387 try_to_free_nids(sbi, MAX_FREE_NIDS);
388 else
3a2ad567 389 build_free_nids(sbi, false);
31696580 390
f455c8a5
JK
391 if (!is_idle(sbi))
392 return;
31696580 393
1b38dc8e
JK
394 /* checkpoint is the only way to shrink partial cached entries */
395 if (!available_free_memory(sbi, NAT_ENTRIES) ||
60b99b48 396 !available_free_memory(sbi, INO_ENTRIES) ||
7d768d2c
CY
397 excess_prefree_segs(sbi) ||
398 excess_dirty_nats(sbi) ||
f455c8a5 399 f2fs_time_over(sbi, CP_TIME)) {
e9f5b8b8
CY
400 if (test_opt(sbi, DATA_FLUSH)) {
401 struct blk_plug plug;
402
403 blk_start_plug(&plug);
36b35a0d 404 sync_dirty_inodes(sbi, FILE_INODE);
e9f5b8b8
CY
405 blk_finish_plug(&plug);
406 }
4660f9c0 407 f2fs_sync_fs(sbi->sb, true);
42190d2a 408 stat_inc_bg_cp_count(sbi->stat_info);
36b35a0d 409 }
4660f9c0
JK
410}
411
3c62be17
JK
412static int __submit_flush_wait(struct block_device *bdev)
413{
414 struct bio *bio = f2fs_bio_alloc(0);
415 int ret;
416
09cb6464 417 bio->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
3c62be17
JK
418 bio->bi_bdev = bdev;
419 ret = submit_bio_wait(bio);
420 bio_put(bio);
421 return ret;
422}
423
424static int submit_flush_wait(struct f2fs_sb_info *sbi)
425{
426 int ret = __submit_flush_wait(sbi->sb->s_bdev);
427 int i;
428
429 if (sbi->s_ndevs && !ret) {
430 for (i = 1; i < sbi->s_ndevs; i++) {
431 ret = __submit_flush_wait(FDEV(i).bdev);
432 if (ret)
433 break;
434 }
435 }
436 return ret;
437}
438
2163d198 439static int issue_flush_thread(void *data)
6b4afdd7
JK
440{
441 struct f2fs_sb_info *sbi = data;
b01a9201 442 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
a688b9d9 443 wait_queue_head_t *q = &fcc->flush_wait_queue;
6b4afdd7
JK
444repeat:
445 if (kthread_should_stop())
446 return 0;
447
721bd4d5 448 if (!llist_empty(&fcc->issue_list)) {
6b4afdd7
JK
449 struct flush_cmd *cmd, *next;
450 int ret;
451
721bd4d5
GZ
452 fcc->dispatch_list = llist_del_all(&fcc->issue_list);
453 fcc->dispatch_list = llist_reverse_order(fcc->dispatch_list);
454
3c62be17 455 ret = submit_flush_wait(sbi);
721bd4d5
GZ
456 llist_for_each_entry_safe(cmd, next,
457 fcc->dispatch_list, llnode) {
6b4afdd7 458 cmd->ret = ret;
6b4afdd7
JK
459 complete(&cmd->wait);
460 }
a688b9d9 461 fcc->dispatch_list = NULL;
6b4afdd7
JK
462 }
463
a688b9d9 464 wait_event_interruptible(*q,
721bd4d5 465 kthread_should_stop() || !llist_empty(&fcc->issue_list));
6b4afdd7
JK
466 goto repeat;
467}
468
469int f2fs_issue_flush(struct f2fs_sb_info *sbi)
470{
b01a9201 471 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
adf8d90b 472 struct flush_cmd cmd;
6b4afdd7 473
24a9ee0f
JK
474 trace_f2fs_issue_flush(sbi->sb, test_opt(sbi, NOBARRIER),
475 test_opt(sbi, FLUSH_MERGE));
476
0f7b2abd
JK
477 if (test_opt(sbi, NOBARRIER))
478 return 0;
479
0a87f664 480 if (!test_opt(sbi, FLUSH_MERGE) || !atomic_read(&fcc->submit_flush)) {
740432f8
JK
481 int ret;
482
0a87f664 483 atomic_inc(&fcc->submit_flush);
3c62be17 484 ret = submit_flush_wait(sbi);
0a87f664 485 atomic_dec(&fcc->submit_flush);
740432f8
JK
486 return ret;
487 }
6b4afdd7 488
adf8d90b 489 init_completion(&cmd.wait);
6b4afdd7 490
0a87f664 491 atomic_inc(&fcc->submit_flush);
721bd4d5 492 llist_add(&cmd.llnode, &fcc->issue_list);
6b4afdd7 493
a688b9d9
GZ
494 if (!fcc->dispatch_list)
495 wake_up(&fcc->flush_wait_queue);
6b4afdd7 496
5eba8c5d
JK
497 if (fcc->f2fs_issue_flush) {
498 wait_for_completion(&cmd.wait);
499 atomic_dec(&fcc->submit_flush);
500 } else {
501 llist_del_all(&fcc->issue_list);
502 atomic_set(&fcc->submit_flush, 0);
503 }
adf8d90b
CY
504
505 return cmd.ret;
6b4afdd7
JK
506}
507
2163d198
GZ
508int create_flush_cmd_control(struct f2fs_sb_info *sbi)
509{
510 dev_t dev = sbi->sb->s_bdev->bd_dev;
511 struct flush_cmd_control *fcc;
512 int err = 0;
513
b01a9201
JK
514 if (SM_I(sbi)->fcc_info) {
515 fcc = SM_I(sbi)->fcc_info;
5eba8c5d
JK
516 goto init_thread;
517 }
518
2163d198
GZ
519 fcc = kzalloc(sizeof(struct flush_cmd_control), GFP_KERNEL);
520 if (!fcc)
521 return -ENOMEM;
0a87f664 522 atomic_set(&fcc->submit_flush, 0);
2163d198 523 init_waitqueue_head(&fcc->flush_wait_queue);
721bd4d5 524 init_llist_head(&fcc->issue_list);
b01a9201 525 SM_I(sbi)->fcc_info = fcc;
5eba8c5d 526init_thread:
2163d198
GZ
527 fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi,
528 "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev));
529 if (IS_ERR(fcc->f2fs_issue_flush)) {
530 err = PTR_ERR(fcc->f2fs_issue_flush);
531 kfree(fcc);
b01a9201 532 SM_I(sbi)->fcc_info = NULL;
2163d198
GZ
533 return err;
534 }
2163d198
GZ
535
536 return err;
537}
538
5eba8c5d 539void destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free)
2163d198 540{
b01a9201 541 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
2163d198 542
5eba8c5d
JK
543 if (fcc && fcc->f2fs_issue_flush) {
544 struct task_struct *flush_thread = fcc->f2fs_issue_flush;
545
546 fcc->f2fs_issue_flush = NULL;
547 kthread_stop(flush_thread);
548 }
549 if (free) {
550 kfree(fcc);
b01a9201 551 SM_I(sbi)->fcc_info = NULL;
5eba8c5d 552 }
2163d198
GZ
553}
554
351df4b2
JK
555static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
556 enum dirty_type dirty_type)
557{
558 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
559
560 /* need not be added */
561 if (IS_CURSEG(sbi, segno))
562 return;
563
564 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
565 dirty_i->nr_dirty[dirty_type]++;
566
567 if (dirty_type == DIRTY) {
568 struct seg_entry *sentry = get_seg_entry(sbi, segno);
4625d6aa 569 enum dirty_type t = sentry->type;
b2f2c390 570
ec325b52
JK
571 if (unlikely(t >= DIRTY)) {
572 f2fs_bug_on(sbi, 1);
573 return;
574 }
4625d6aa
CL
575 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
576 dirty_i->nr_dirty[t]++;
351df4b2
JK
577 }
578}
579
580static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
581 enum dirty_type dirty_type)
582{
583 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
584
585 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
586 dirty_i->nr_dirty[dirty_type]--;
587
588 if (dirty_type == DIRTY) {
4625d6aa
CL
589 struct seg_entry *sentry = get_seg_entry(sbi, segno);
590 enum dirty_type t = sentry->type;
591
592 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
593 dirty_i->nr_dirty[t]--;
b2f2c390 594
5ec4e49f
JK
595 if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0)
596 clear_bit(GET_SECNO(sbi, segno),
597 dirty_i->victim_secmap);
351df4b2
JK
598 }
599}
600
0a8165d7 601/*
351df4b2
JK
602 * Should not occur error such as -ENOMEM.
603 * Adding dirty entry into seglist is not critical operation.
604 * If a given segment is one of current working segments, it won't be added.
605 */
8d8451af 606static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
351df4b2
JK
607{
608 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
609 unsigned short valid_blocks;
610
611 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
612 return;
613
614 mutex_lock(&dirty_i->seglist_lock);
615
616 valid_blocks = get_valid_blocks(sbi, segno, 0);
617
618 if (valid_blocks == 0) {
619 __locate_dirty_segment(sbi, segno, PRE);
620 __remove_dirty_segment(sbi, segno, DIRTY);
621 } else if (valid_blocks < sbi->blocks_per_seg) {
622 __locate_dirty_segment(sbi, segno, DIRTY);
623 } else {
624 /* Recovery routine with SSR needs this */
625 __remove_dirty_segment(sbi, segno, DIRTY);
626 }
627
628 mutex_unlock(&dirty_i->seglist_lock);
351df4b2
JK
629}
630
15469963 631static void __add_discard_cmd(struct f2fs_sb_info *sbi,
4e6a8d9b 632 struct bio *bio, block_t lstart, block_t len)
275b66b0 633{
0b54fb84
JK
634 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
635 struct list_head *cmd_list = &(dcc->discard_cmd_list);
b01a9201 636 struct discard_cmd *dc;
275b66b0 637
b01a9201
JK
638 dc = f2fs_kmem_cache_alloc(discard_cmd_slab, GFP_NOFS);
639 INIT_LIST_HEAD(&dc->list);
640 dc->bio = bio;
15469963 641 bio->bi_private = dc;
b01a9201
JK
642 dc->lstart = lstart;
643 dc->len = len;
15469963 644 dc->state = D_PREP;
b01a9201 645 init_completion(&dc->wait);
15469963
JK
646
647 mutex_lock(&dcc->cmd_lock);
0b54fb84 648 list_add_tail(&dc->list, cmd_list);
15469963
JK
649 mutex_unlock(&dcc->cmd_lock);
650}
651
652static void __remove_discard_cmd(struct f2fs_sb_info *sbi, struct discard_cmd *dc)
653{
654 int err = dc->bio->bi_error;
275b66b0 655
dcc9165d
JK
656 if (dc->state == D_DONE)
657 atomic_dec(&(SM_I(sbi)->dcc_info->submit_discard));
658
15469963
JK
659 if (err == -EOPNOTSUPP)
660 err = 0;
661
662 if (err)
663 f2fs_msg(sbi->sb, KERN_INFO,
664 "Issue discard failed, ret: %d", err);
665 bio_put(dc->bio);
666 list_del(&dc->list);
667 kmem_cache_free(discard_cmd_slab, dc);
275b66b0
CY
668}
669
4e6a8d9b
JK
670/* This should be covered by global mutex, &sit_i->sentry_lock */
671void f2fs_wait_discard_bio(struct f2fs_sb_info *sbi, block_t blkaddr)
275b66b0 672{
0b54fb84
JK
673 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
674 struct list_head *wait_list = &(dcc->discard_cmd_list);
b01a9201 675 struct discard_cmd *dc, *tmp;
275b66b0 676
15469963 677 mutex_lock(&dcc->cmd_lock);
b01a9201 678 list_for_each_entry_safe(dc, tmp, wait_list, list) {
275b66b0 679
15469963
JK
680 if (blkaddr == NULL_ADDR) {
681 if (dc->state == D_PREP) {
682 dc->state = D_SUBMIT;
683 submit_bio(dc->bio);
dcc9165d 684 atomic_inc(&dcc->submit_discard);
15469963
JK
685 }
686 wait_for_completion_io(&dc->wait);
687
688 __remove_discard_cmd(sbi, dc);
689 continue;
690 }
691
692 if (dc->lstart <= blkaddr && blkaddr < dc->lstart + dc->len) {
693 if (dc->state == D_SUBMIT)
b01a9201 694 wait_for_completion_io(&dc->wait);
4e6a8d9b 695 else
15469963 696 __remove_discard_cmd(sbi, dc);
4e6a8d9b 697 }
275b66b0 698 }
15469963 699 mutex_unlock(&dcc->cmd_lock);
275b66b0
CY
700}
701
b01a9201 702static void f2fs_submit_discard_endio(struct bio *bio)
275b66b0 703{
b01a9201 704 struct discard_cmd *dc = (struct discard_cmd *)bio->bi_private;
275b66b0 705
b01a9201 706 complete(&dc->wait);
15469963 707 dc->state = D_DONE;
275b66b0
CY
708}
709
15469963
JK
710static int issue_discard_thread(void *data)
711{
712 struct f2fs_sb_info *sbi = data;
713 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
714 wait_queue_head_t *q = &dcc->discard_wait_queue;
715 struct list_head *cmd_list = &dcc->discard_cmd_list;
716 struct discard_cmd *dc, *tmp;
717 struct blk_plug plug;
718 int iter = 0;
719repeat:
720 if (kthread_should_stop())
721 return 0;
722
723 blk_start_plug(&plug);
724
725 mutex_lock(&dcc->cmd_lock);
726 list_for_each_entry_safe(dc, tmp, cmd_list, list) {
727 if (dc->state == D_PREP) {
728 dc->state = D_SUBMIT;
729 submit_bio(dc->bio);
dcc9165d 730 atomic_inc(&dcc->submit_discard);
15469963
JK
731 if (iter++ > DISCARD_ISSUE_RATE)
732 break;
733 } else if (dc->state == D_DONE) {
734 __remove_discard_cmd(sbi, dc);
735 }
736 }
737 mutex_unlock(&dcc->cmd_lock);
738
739 blk_finish_plug(&plug);
740
741 iter = 0;
742 congestion_wait(BLK_RW_SYNC, HZ/50);
743
744 wait_event_interruptible(*q,
745 kthread_should_stop() || !list_empty(&dcc->discard_cmd_list));
746 goto repeat;
747}
748
749
275b66b0 750/* this function is copied from blkdev_issue_discard from block/blk-lib.c */
f46e8809 751static int __f2fs_issue_discard_async(struct f2fs_sb_info *sbi,
3c62be17 752 struct block_device *bdev, block_t blkstart, block_t blklen)
275b66b0 753{
275b66b0 754 struct bio *bio = NULL;
4e6a8d9b 755 block_t lblkstart = blkstart;
275b66b0
CY
756 int err;
757
f46e8809
DLM
758 trace_f2fs_issue_discard(sbi->sb, blkstart, blklen);
759
3c62be17
JK
760 if (sbi->s_ndevs) {
761 int devi = f2fs_target_device_index(sbi, blkstart);
762
763 blkstart -= FDEV(devi).start_blk;
764 }
f46e8809
DLM
765 err = __blkdev_issue_discard(bdev,
766 SECTOR_FROM_BLOCK(blkstart),
767 SECTOR_FROM_BLOCK(blklen),
768 GFP_NOFS, 0, &bio);
275b66b0 769 if (!err && bio) {
b01a9201 770 bio->bi_end_io = f2fs_submit_discard_endio;
275b66b0 771 bio->bi_opf |= REQ_SYNC;
15469963
JK
772
773 __add_discard_cmd(sbi, bio, lblkstart, blklen);
774 wake_up(&SM_I(sbi)->dcc_info->discard_wait_queue);
275b66b0 775 }
275b66b0
CY
776 return err;
777}
778
f46e8809 779#ifdef CONFIG_BLK_DEV_ZONED
3c62be17
JK
780static int __f2fs_issue_discard_zone(struct f2fs_sb_info *sbi,
781 struct block_device *bdev, block_t blkstart, block_t blklen)
f46e8809 782{
f46e8809 783 sector_t nr_sects = SECTOR_FROM_BLOCK(blklen);
3c62be17
JK
784 sector_t sector;
785 int devi = 0;
786
787 if (sbi->s_ndevs) {
788 devi = f2fs_target_device_index(sbi, blkstart);
789 blkstart -= FDEV(devi).start_blk;
790 }
791 sector = SECTOR_FROM_BLOCK(blkstart);
f46e8809 792
f99e8648
DLM
793 if (sector & (bdev_zone_sectors(bdev) - 1) ||
794 nr_sects != bdev_zone_sectors(bdev)) {
f46e8809 795 f2fs_msg(sbi->sb, KERN_INFO,
3c62be17
JK
796 "(%d) %s: Unaligned discard attempted (block %x + %x)",
797 devi, sbi->s_ndevs ? FDEV(devi).path: "",
798 blkstart, blklen);
f46e8809
DLM
799 return -EIO;
800 }
801
802 /*
803 * We need to know the type of the zone: for conventional zones,
804 * use regular discard if the drive supports it. For sequential
805 * zones, reset the zone write pointer.
806 */
3c62be17 807 switch (get_blkz_type(sbi, bdev, blkstart)) {
f46e8809
DLM
808
809 case BLK_ZONE_TYPE_CONVENTIONAL:
810 if (!blk_queue_discard(bdev_get_queue(bdev)))
811 return 0;
3c62be17 812 return __f2fs_issue_discard_async(sbi, bdev, blkstart, blklen);
f46e8809
DLM
813 case BLK_ZONE_TYPE_SEQWRITE_REQ:
814 case BLK_ZONE_TYPE_SEQWRITE_PREF:
126606c7 815 trace_f2fs_issue_reset_zone(sbi->sb, blkstart);
f46e8809
DLM
816 return blkdev_reset_zones(bdev, sector,
817 nr_sects, GFP_NOFS);
818 default:
819 /* Unknown zone type: broken device ? */
820 return -EIO;
821 }
822}
823#endif
824
3c62be17
JK
825static int __issue_discard_async(struct f2fs_sb_info *sbi,
826 struct block_device *bdev, block_t blkstart, block_t blklen)
827{
828#ifdef CONFIG_BLK_DEV_ZONED
829 if (f2fs_sb_mounted_blkzoned(sbi->sb) &&
830 bdev_zoned_model(bdev) != BLK_ZONED_NONE)
831 return __f2fs_issue_discard_zone(sbi, bdev, blkstart, blklen);
832#endif
833 return __f2fs_issue_discard_async(sbi, bdev, blkstart, blklen);
834}
835
1e87a78d 836static int f2fs_issue_discard(struct f2fs_sb_info *sbi,
37208879
JK
837 block_t blkstart, block_t blklen)
838{
3c62be17
JK
839 sector_t start = blkstart, len = 0;
840 struct block_device *bdev;
a66cdd98
JK
841 struct seg_entry *se;
842 unsigned int offset;
843 block_t i;
3c62be17
JK
844 int err = 0;
845
846 bdev = f2fs_target_device(sbi, blkstart, NULL);
847
848 for (i = blkstart; i < blkstart + blklen; i++, len++) {
849 if (i != start) {
850 struct block_device *bdev2 =
851 f2fs_target_device(sbi, i, NULL);
852
853 if (bdev2 != bdev) {
854 err = __issue_discard_async(sbi, bdev,
855 start, len);
856 if (err)
857 return err;
858 bdev = bdev2;
859 start = i;
860 len = 0;
861 }
862 }
a66cdd98 863
a66cdd98
JK
864 se = get_seg_entry(sbi, GET_SEGNO(sbi, i));
865 offset = GET_BLKOFF_FROM_SEG0(sbi, i);
866
867 if (!f2fs_test_and_set_bit(offset, se->discard_map))
868 sbi->discard_blks--;
869 }
f46e8809 870
3c62be17
JK
871 if (len)
872 err = __issue_discard_async(sbi, bdev, start, len);
873 return err;
1e87a78d
JK
874}
875
adf4983b 876static void __add_discard_entry(struct f2fs_sb_info *sbi,
a66cdd98
JK
877 struct cp_control *cpc, struct seg_entry *se,
878 unsigned int start, unsigned int end)
b2955550 879{
0b54fb84 880 struct list_head *head = &SM_I(sbi)->dcc_info->discard_entry_list;
adf4983b
JK
881 struct discard_entry *new, *last;
882
883 if (!list_empty(head)) {
884 last = list_last_entry(head, struct discard_entry, list);
885 if (START_BLOCK(sbi, cpc->trim_start) + start ==
886 last->blkaddr + last->len) {
887 last->len += end - start;
888 goto done;
889 }
890 }
891
892 new = f2fs_kmem_cache_alloc(discard_entry_slab, GFP_NOFS);
893 INIT_LIST_HEAD(&new->list);
894 new->blkaddr = START_BLOCK(sbi, cpc->trim_start) + start;
895 new->len = end - start;
896 list_add_tail(&new->list, head);
897done:
0b54fb84 898 SM_I(sbi)->dcc_info->nr_discards += end - start;
adf4983b
JK
899}
900
25290fa5
JK
901static bool add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc,
902 bool check_only)
adf4983b 903{
b2955550
JK
904 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
905 int max_blocks = sbi->blocks_per_seg;
4b2fecc8 906 struct seg_entry *se = get_seg_entry(sbi, cpc->trim_start);
b2955550
JK
907 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
908 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
a66cdd98 909 unsigned long *discard_map = (unsigned long *)se->discard_map;
60a3b782 910 unsigned long *dmap = SIT_I(sbi)->tmp_map;
b2955550 911 unsigned int start = 0, end = -1;
4b2fecc8 912 bool force = (cpc->reason == CP_DISCARD);
b2955550
JK
913 int i;
914
3e025740 915 if (se->valid_blocks == max_blocks || !f2fs_discard_en(sbi))
25290fa5 916 return false;
b2955550 917
a66cdd98
JK
918 if (!force) {
919 if (!test_opt(sbi, DISCARD) || !se->valid_blocks ||
0b54fb84
JK
920 SM_I(sbi)->dcc_info->nr_discards >=
921 SM_I(sbi)->dcc_info->max_discards)
25290fa5 922 return false;
4b2fecc8
JK
923 }
924
b2955550
JK
925 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
926 for (i = 0; i < entries; i++)
a66cdd98 927 dmap[i] = force ? ~ckpt_map[i] & ~discard_map[i] :
d7bc2484 928 (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
b2955550 929
0b54fb84
JK
930 while (force || SM_I(sbi)->dcc_info->nr_discards <=
931 SM_I(sbi)->dcc_info->max_discards) {
b2955550
JK
932 start = __find_rev_next_bit(dmap, max_blocks, end + 1);
933 if (start >= max_blocks)
934 break;
935
936 end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
c7b41e16
YH
937 if (force && start && end != max_blocks
938 && (end - start) < cpc->trim_minlen)
939 continue;
940
25290fa5
JK
941 if (check_only)
942 return true;
943
a66cdd98 944 __add_discard_entry(sbi, cpc, se, start, end);
b2955550 945 }
25290fa5 946 return false;
b2955550
JK
947}
948
4b2fecc8
JK
949void release_discard_addrs(struct f2fs_sb_info *sbi)
950{
0b54fb84 951 struct list_head *head = &(SM_I(sbi)->dcc_info->discard_entry_list);
4b2fecc8
JK
952 struct discard_entry *entry, *this;
953
954 /* drop caches */
955 list_for_each_entry_safe(entry, this, head, list) {
956 list_del(&entry->list);
957 kmem_cache_free(discard_entry_slab, entry);
958 }
959}
960
0a8165d7 961/*
351df4b2
JK
962 * Should call clear_prefree_segments after checkpoint is done.
963 */
964static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
965{
966 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
b65ee148 967 unsigned int segno;
351df4b2
JK
968
969 mutex_lock(&dirty_i->seglist_lock);
7cd8558b 970 for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], MAIN_SEGS(sbi))
351df4b2 971 __set_test_and_free(sbi, segno);
351df4b2
JK
972 mutex_unlock(&dirty_i->seglist_lock);
973}
974
836b5a63 975void clear_prefree_segments(struct f2fs_sb_info *sbi, struct cp_control *cpc)
351df4b2 976{
0b54fb84 977 struct list_head *head = &(SM_I(sbi)->dcc_info->discard_entry_list);
2d7b822a 978 struct discard_entry *entry, *this;
351df4b2 979 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
29e59c14 980 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
29e59c14 981 unsigned int start = 0, end = -1;
36abef4e 982 unsigned int secno, start_segno;
c24a0fd6 983 bool force = (cpc->reason == CP_DISCARD);
351df4b2
JK
984
985 mutex_lock(&dirty_i->seglist_lock);
29e59c14 986
351df4b2 987 while (1) {
29e59c14 988 int i;
7cd8558b
JK
989 start = find_next_bit(prefree_map, MAIN_SEGS(sbi), end + 1);
990 if (start >= MAIN_SEGS(sbi))
351df4b2 991 break;
7cd8558b
JK
992 end = find_next_zero_bit(prefree_map, MAIN_SEGS(sbi),
993 start + 1);
29e59c14
CL
994
995 for (i = start; i < end; i++)
996 clear_bit(i, prefree_map);
997
998 dirty_i->nr_dirty[PRE] -= end - start;
999
650d3c4e 1000 if (!test_opt(sbi, DISCARD))
29e59c14 1001 continue;
351df4b2 1002
650d3c4e
YH
1003 if (force && start >= cpc->trim_start &&
1004 (end - 1) <= cpc->trim_end)
1005 continue;
1006
36abef4e
JK
1007 if (!test_opt(sbi, LFS) || sbi->segs_per_sec == 1) {
1008 f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
37208879 1009 (end - start) << sbi->log_blocks_per_seg);
36abef4e
JK
1010 continue;
1011 }
1012next:
1013 secno = GET_SECNO(sbi, start);
1014 start_segno = secno * sbi->segs_per_sec;
1015 if (!IS_CURSEC(sbi, secno) &&
1016 !get_valid_blocks(sbi, start, sbi->segs_per_sec))
1017 f2fs_issue_discard(sbi, START_BLOCK(sbi, start_segno),
1018 sbi->segs_per_sec << sbi->log_blocks_per_seg);
1019
1020 start = start_segno + sbi->segs_per_sec;
1021 if (start < end)
1022 goto next;
351df4b2
JK
1023 }
1024 mutex_unlock(&dirty_i->seglist_lock);
b2955550
JK
1025
1026 /* send small discards */
2d7b822a 1027 list_for_each_entry_safe(entry, this, head, list) {
c24a0fd6 1028 if (force && entry->len < cpc->trim_minlen)
836b5a63 1029 goto skip;
37208879 1030 f2fs_issue_discard(sbi, entry->blkaddr, entry->len);
f56aa1c5 1031 cpc->trimmed += entry->len;
836b5a63 1032skip:
b2955550 1033 list_del(&entry->list);
0b54fb84 1034 SM_I(sbi)->dcc_info->nr_discards -= entry->len;
b2955550
JK
1035 kmem_cache_free(discard_entry_slab, entry);
1036 }
351df4b2
JK
1037}
1038
0b54fb84
JK
1039int create_discard_cmd_control(struct f2fs_sb_info *sbi)
1040{
15469963 1041 dev_t dev = sbi->sb->s_bdev->bd_dev;
0b54fb84
JK
1042 struct discard_cmd_control *dcc;
1043 int err = 0;
1044
1045 if (SM_I(sbi)->dcc_info) {
1046 dcc = SM_I(sbi)->dcc_info;
1047 goto init_thread;
1048 }
1049
1050 dcc = kzalloc(sizeof(struct discard_cmd_control), GFP_KERNEL);
1051 if (!dcc)
1052 return -ENOMEM;
1053
1054 INIT_LIST_HEAD(&dcc->discard_entry_list);
1055 INIT_LIST_HEAD(&dcc->discard_cmd_list);
15469963 1056 mutex_init(&dcc->cmd_lock);
dcc9165d 1057 atomic_set(&dcc->submit_discard, 0);
0b54fb84
JK
1058 dcc->nr_discards = 0;
1059 dcc->max_discards = 0;
1060
15469963 1061 init_waitqueue_head(&dcc->discard_wait_queue);
0b54fb84
JK
1062 SM_I(sbi)->dcc_info = dcc;
1063init_thread:
15469963
JK
1064 dcc->f2fs_issue_discard = kthread_run(issue_discard_thread, sbi,
1065 "f2fs_discard-%u:%u", MAJOR(dev), MINOR(dev));
1066 if (IS_ERR(dcc->f2fs_issue_discard)) {
1067 err = PTR_ERR(dcc->f2fs_issue_discard);
1068 kfree(dcc);
1069 SM_I(sbi)->dcc_info = NULL;
1070 return err;
1071 }
1072
0b54fb84
JK
1073 return err;
1074}
1075
1076void destroy_discard_cmd_control(struct f2fs_sb_info *sbi, bool free)
1077{
1078 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1079
15469963
JK
1080 if (dcc && dcc->f2fs_issue_discard) {
1081 struct task_struct *discard_thread = dcc->f2fs_issue_discard;
1082
1083 dcc->f2fs_issue_discard = NULL;
1084 kthread_stop(discard_thread);
1085 }
0b54fb84
JK
1086 if (free) {
1087 kfree(dcc);
1088 SM_I(sbi)->dcc_info = NULL;
1089 }
1090}
1091
184a5cd2 1092static bool __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
351df4b2
JK
1093{
1094 struct sit_info *sit_i = SIT_I(sbi);
184a5cd2
CY
1095
1096 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap)) {
351df4b2 1097 sit_i->dirty_sentries++;
184a5cd2
CY
1098 return false;
1099 }
1100
1101 return true;
351df4b2
JK
1102}
1103
1104static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
1105 unsigned int segno, int modified)
1106{
1107 struct seg_entry *se = get_seg_entry(sbi, segno);
1108 se->type = type;
1109 if (modified)
1110 __mark_sit_entry_dirty(sbi, segno);
1111}
1112
1113static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
1114{
1115 struct seg_entry *se;
1116 unsigned int segno, offset;
1117 long int new_vblocks;
1118
1119 segno = GET_SEGNO(sbi, blkaddr);
1120
1121 se = get_seg_entry(sbi, segno);
1122 new_vblocks = se->valid_blocks + del;
491c0854 1123 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
351df4b2 1124
9850cf4a 1125 f2fs_bug_on(sbi, (new_vblocks >> (sizeof(unsigned short) << 3) ||
351df4b2
JK
1126 (new_vblocks > sbi->blocks_per_seg)));
1127
1128 se->valid_blocks = new_vblocks;
1129 se->mtime = get_mtime(sbi);
1130 SIT_I(sbi)->max_mtime = se->mtime;
1131
1132 /* Update valid block bitmap */
1133 if (del > 0) {
355e7891
CY
1134 if (f2fs_test_and_set_bit(offset, se->cur_valid_map)) {
1135#ifdef CONFIG_F2FS_CHECK_FS
1136 if (f2fs_test_and_set_bit(offset,
1137 se->cur_valid_map_mir))
1138 f2fs_bug_on(sbi, 1);
1139 else
1140 WARN_ON(1);
1141#else
05796763 1142 f2fs_bug_on(sbi, 1);
355e7891
CY
1143#endif
1144 }
3e025740
JK
1145 if (f2fs_discard_en(sbi) &&
1146 !f2fs_test_and_set_bit(offset, se->discard_map))
a66cdd98 1147 sbi->discard_blks--;
351df4b2 1148 } else {
355e7891
CY
1149 if (!f2fs_test_and_clear_bit(offset, se->cur_valid_map)) {
1150#ifdef CONFIG_F2FS_CHECK_FS
1151 if (!f2fs_test_and_clear_bit(offset,
1152 se->cur_valid_map_mir))
1153 f2fs_bug_on(sbi, 1);
1154 else
1155 WARN_ON(1);
1156#else
05796763 1157 f2fs_bug_on(sbi, 1);
355e7891
CY
1158#endif
1159 }
3e025740
JK
1160 if (f2fs_discard_en(sbi) &&
1161 f2fs_test_and_clear_bit(offset, se->discard_map))
a66cdd98 1162 sbi->discard_blks++;
351df4b2
JK
1163 }
1164 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
1165 se->ckpt_valid_blocks += del;
1166
1167 __mark_sit_entry_dirty(sbi, segno);
1168
1169 /* update total number of valid blocks to be written in ckpt area */
1170 SIT_I(sbi)->written_valid_blocks += del;
1171
1172 if (sbi->segs_per_sec > 1)
1173 get_sec_entry(sbi, segno)->valid_blocks += del;
1174}
1175
5e443818 1176void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new)
351df4b2 1177{
5e443818
JK
1178 update_sit_entry(sbi, new, 1);
1179 if (GET_SEGNO(sbi, old) != NULL_SEGNO)
1180 update_sit_entry(sbi, old, -1);
1181
1182 locate_dirty_segment(sbi, GET_SEGNO(sbi, old));
1183 locate_dirty_segment(sbi, GET_SEGNO(sbi, new));
351df4b2
JK
1184}
1185
1186void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
1187{
1188 unsigned int segno = GET_SEGNO(sbi, addr);
1189 struct sit_info *sit_i = SIT_I(sbi);
1190
9850cf4a 1191 f2fs_bug_on(sbi, addr == NULL_ADDR);
351df4b2
JK
1192 if (addr == NEW_ADDR)
1193 return;
1194
1195 /* add it into sit main buffer */
1196 mutex_lock(&sit_i->sentry_lock);
1197
1198 update_sit_entry(sbi, addr, -1);
1199
1200 /* add it into dirty seglist */
1201 locate_dirty_segment(sbi, segno);
1202
1203 mutex_unlock(&sit_i->sentry_lock);
1204}
1205
6e2c64ad
JK
1206bool is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr)
1207{
1208 struct sit_info *sit_i = SIT_I(sbi);
1209 unsigned int segno, offset;
1210 struct seg_entry *se;
1211 bool is_cp = false;
1212
1213 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR)
1214 return true;
1215
1216 mutex_lock(&sit_i->sentry_lock);
1217
1218 segno = GET_SEGNO(sbi, blkaddr);
1219 se = get_seg_entry(sbi, segno);
1220 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
1221
1222 if (f2fs_test_bit(offset, se->ckpt_valid_map))
1223 is_cp = true;
1224
1225 mutex_unlock(&sit_i->sentry_lock);
1226
1227 return is_cp;
1228}
1229
0a8165d7 1230/*
351df4b2
JK
1231 * This function should be resided under the curseg_mutex lock
1232 */
1233static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
e79efe3b 1234 struct f2fs_summary *sum)
351df4b2
JK
1235{
1236 struct curseg_info *curseg = CURSEG_I(sbi, type);
1237 void *addr = curseg->sum_blk;
e79efe3b 1238 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
351df4b2 1239 memcpy(addr, sum, sizeof(struct f2fs_summary));
351df4b2
JK
1240}
1241
0a8165d7 1242/*
351df4b2
JK
1243 * Calculate the number of current summary pages for writing
1244 */
3fa06d7b 1245int npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra)
351df4b2 1246{
351df4b2 1247 int valid_sum_count = 0;
9a47938b 1248 int i, sum_in_page;
351df4b2
JK
1249
1250 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1251 if (sbi->ckpt->alloc_type[i] == SSR)
1252 valid_sum_count += sbi->blocks_per_seg;
3fa06d7b
CY
1253 else {
1254 if (for_ra)
1255 valid_sum_count += le16_to_cpu(
1256 F2FS_CKPT(sbi)->cur_data_blkoff[i]);
1257 else
1258 valid_sum_count += curseg_blkoff(sbi, i);
1259 }
351df4b2
JK
1260 }
1261
09cbfeaf 1262 sum_in_page = (PAGE_SIZE - 2 * SUM_JOURNAL_SIZE -
9a47938b
FL
1263 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
1264 if (valid_sum_count <= sum_in_page)
351df4b2 1265 return 1;
9a47938b 1266 else if ((valid_sum_count - sum_in_page) <=
09cbfeaf 1267 (PAGE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
351df4b2
JK
1268 return 2;
1269 return 3;
1270}
1271
0a8165d7 1272/*
351df4b2
JK
1273 * Caller should put this summary page
1274 */
1275struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
1276{
1277 return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
1278}
1279
381722d2 1280void update_meta_page(struct f2fs_sb_info *sbi, void *src, block_t blk_addr)
351df4b2
JK
1281{
1282 struct page *page = grab_meta_page(sbi, blk_addr);
381722d2
CY
1283 void *dst = page_address(page);
1284
1285 if (src)
09cbfeaf 1286 memcpy(dst, src, PAGE_SIZE);
381722d2 1287 else
09cbfeaf 1288 memset(dst, 0, PAGE_SIZE);
351df4b2
JK
1289 set_page_dirty(page);
1290 f2fs_put_page(page, 1);
1291}
1292
381722d2
CY
1293static void write_sum_page(struct f2fs_sb_info *sbi,
1294 struct f2fs_summary_block *sum_blk, block_t blk_addr)
1295{
1296 update_meta_page(sbi, (void *)sum_blk, blk_addr);
1297}
1298
b7ad7512
CY
1299static void write_current_sum_page(struct f2fs_sb_info *sbi,
1300 int type, block_t blk_addr)
1301{
1302 struct curseg_info *curseg = CURSEG_I(sbi, type);
1303 struct page *page = grab_meta_page(sbi, blk_addr);
1304 struct f2fs_summary_block *src = curseg->sum_blk;
1305 struct f2fs_summary_block *dst;
1306
1307 dst = (struct f2fs_summary_block *)page_address(page);
1308
1309 mutex_lock(&curseg->curseg_mutex);
1310
1311 down_read(&curseg->journal_rwsem);
1312 memcpy(&dst->journal, curseg->journal, SUM_JOURNAL_SIZE);
1313 up_read(&curseg->journal_rwsem);
1314
1315 memcpy(dst->entries, src->entries, SUM_ENTRY_SIZE);
1316 memcpy(&dst->footer, &src->footer, SUM_FOOTER_SIZE);
1317
1318 mutex_unlock(&curseg->curseg_mutex);
1319
1320 set_page_dirty(page);
1321 f2fs_put_page(page, 1);
1322}
1323
60374688
JK
1324static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
1325{
1326 struct curseg_info *curseg = CURSEG_I(sbi, type);
81fb5e87 1327 unsigned int segno = curseg->segno + 1;
60374688
JK
1328 struct free_segmap_info *free_i = FREE_I(sbi);
1329
7cd8558b 1330 if (segno < MAIN_SEGS(sbi) && segno % sbi->segs_per_sec)
81fb5e87 1331 return !test_bit(segno, free_i->free_segmap);
60374688
JK
1332 return 0;
1333}
1334
0a8165d7 1335/*
351df4b2
JK
1336 * Find a new segment from the free segments bitmap to right order
1337 * This function should be returned with success, otherwise BUG
1338 */
1339static void get_new_segment(struct f2fs_sb_info *sbi,
1340 unsigned int *newseg, bool new_sec, int dir)
1341{
1342 struct free_segmap_info *free_i = FREE_I(sbi);
351df4b2 1343 unsigned int segno, secno, zoneno;
7cd8558b 1344 unsigned int total_zones = MAIN_SECS(sbi) / sbi->secs_per_zone;
351df4b2
JK
1345 unsigned int hint = *newseg / sbi->segs_per_sec;
1346 unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
1347 unsigned int left_start = hint;
1348 bool init = true;
1349 int go_left = 0;
1350 int i;
1351
1a118ccf 1352 spin_lock(&free_i->segmap_lock);
351df4b2
JK
1353
1354 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
1355 segno = find_next_zero_bit(free_i->free_segmap,
0ab14356
CY
1356 (hint + 1) * sbi->segs_per_sec, *newseg + 1);
1357 if (segno < (hint + 1) * sbi->segs_per_sec)
351df4b2
JK
1358 goto got_it;
1359 }
1360find_other_zone:
7cd8558b
JK
1361 secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint);
1362 if (secno >= MAIN_SECS(sbi)) {
351df4b2
JK
1363 if (dir == ALLOC_RIGHT) {
1364 secno = find_next_zero_bit(free_i->free_secmap,
7cd8558b
JK
1365 MAIN_SECS(sbi), 0);
1366 f2fs_bug_on(sbi, secno >= MAIN_SECS(sbi));
351df4b2
JK
1367 } else {
1368 go_left = 1;
1369 left_start = hint - 1;
1370 }
1371 }
1372 if (go_left == 0)
1373 goto skip_left;
1374
1375 while (test_bit(left_start, free_i->free_secmap)) {
1376 if (left_start > 0) {
1377 left_start--;
1378 continue;
1379 }
1380 left_start = find_next_zero_bit(free_i->free_secmap,
7cd8558b
JK
1381 MAIN_SECS(sbi), 0);
1382 f2fs_bug_on(sbi, left_start >= MAIN_SECS(sbi));
351df4b2
JK
1383 break;
1384 }
1385 secno = left_start;
1386skip_left:
1387 hint = secno;
1388 segno = secno * sbi->segs_per_sec;
1389 zoneno = secno / sbi->secs_per_zone;
1390
1391 /* give up on finding another zone */
1392 if (!init)
1393 goto got_it;
1394 if (sbi->secs_per_zone == 1)
1395 goto got_it;
1396 if (zoneno == old_zoneno)
1397 goto got_it;
1398 if (dir == ALLOC_LEFT) {
1399 if (!go_left && zoneno + 1 >= total_zones)
1400 goto got_it;
1401 if (go_left && zoneno == 0)
1402 goto got_it;
1403 }
1404 for (i = 0; i < NR_CURSEG_TYPE; i++)
1405 if (CURSEG_I(sbi, i)->zone == zoneno)
1406 break;
1407
1408 if (i < NR_CURSEG_TYPE) {
1409 /* zone is in user, try another */
1410 if (go_left)
1411 hint = zoneno * sbi->secs_per_zone - 1;
1412 else if (zoneno + 1 >= total_zones)
1413 hint = 0;
1414 else
1415 hint = (zoneno + 1) * sbi->secs_per_zone;
1416 init = false;
1417 goto find_other_zone;
1418 }
1419got_it:
1420 /* set it as dirty segment in free segmap */
9850cf4a 1421 f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap));
351df4b2
JK
1422 __set_inuse(sbi, segno);
1423 *newseg = segno;
1a118ccf 1424 spin_unlock(&free_i->segmap_lock);
351df4b2
JK
1425}
1426
1427static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
1428{
1429 struct curseg_info *curseg = CURSEG_I(sbi, type);
1430 struct summary_footer *sum_footer;
1431
1432 curseg->segno = curseg->next_segno;
1433 curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
1434 curseg->next_blkoff = 0;
1435 curseg->next_segno = NULL_SEGNO;
1436
1437 sum_footer = &(curseg->sum_blk->footer);
1438 memset(sum_footer, 0, sizeof(struct summary_footer));
1439 if (IS_DATASEG(type))
1440 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
1441 if (IS_NODESEG(type))
1442 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
1443 __set_sit_entry_type(sbi, type, curseg->segno, modified);
1444}
1445
0a8165d7 1446/*
351df4b2
JK
1447 * Allocate a current working segment.
1448 * This function always allocates a free segment in LFS manner.
1449 */
1450static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
1451{
1452 struct curseg_info *curseg = CURSEG_I(sbi, type);
1453 unsigned int segno = curseg->segno;
1454 int dir = ALLOC_LEFT;
1455
1456 write_sum_page(sbi, curseg->sum_blk,
81fb5e87 1457 GET_SUM_BLOCK(sbi, segno));
351df4b2
JK
1458 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
1459 dir = ALLOC_RIGHT;
1460
1461 if (test_opt(sbi, NOHEAP))
1462 dir = ALLOC_RIGHT;
1463
1464 get_new_segment(sbi, &segno, new_sec, dir);
1465 curseg->next_segno = segno;
1466 reset_curseg(sbi, type, 1);
1467 curseg->alloc_type = LFS;
1468}
1469
1470static void __next_free_blkoff(struct f2fs_sb_info *sbi,
1471 struct curseg_info *seg, block_t start)
1472{
1473 struct seg_entry *se = get_seg_entry(sbi, seg->segno);
e81c93cf 1474 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
60a3b782 1475 unsigned long *target_map = SIT_I(sbi)->tmp_map;
e81c93cf
CL
1476 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
1477 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
1478 int i, pos;
1479
1480 for (i = 0; i < entries; i++)
1481 target_map[i] = ckpt_map[i] | cur_map[i];
1482
1483 pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
1484
1485 seg->next_blkoff = pos;
351df4b2
JK
1486}
1487
0a8165d7 1488/*
351df4b2
JK
1489 * If a segment is written by LFS manner, next block offset is just obtained
1490 * by increasing the current block offset. However, if a segment is written by
1491 * SSR manner, next block offset obtained by calling __next_free_blkoff
1492 */
1493static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
1494 struct curseg_info *seg)
1495{
1496 if (seg->alloc_type == SSR)
1497 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
1498 else
1499 seg->next_blkoff++;
1500}
1501
0a8165d7 1502/*
e1c42045 1503 * This function always allocates a used segment(from dirty seglist) by SSR
351df4b2
JK
1504 * manner, so it should recover the existing segment information of valid blocks
1505 */
1506static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
1507{
1508 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1509 struct curseg_info *curseg = CURSEG_I(sbi, type);
1510 unsigned int new_segno = curseg->next_segno;
1511 struct f2fs_summary_block *sum_node;
1512 struct page *sum_page;
1513
1514 write_sum_page(sbi, curseg->sum_blk,
1515 GET_SUM_BLOCK(sbi, curseg->segno));
1516 __set_test_and_inuse(sbi, new_segno);
1517
1518 mutex_lock(&dirty_i->seglist_lock);
1519 __remove_dirty_segment(sbi, new_segno, PRE);
1520 __remove_dirty_segment(sbi, new_segno, DIRTY);
1521 mutex_unlock(&dirty_i->seglist_lock);
1522
1523 reset_curseg(sbi, type, 1);
1524 curseg->alloc_type = SSR;
1525 __next_free_blkoff(sbi, curseg, 0);
1526
1527 if (reuse) {
1528 sum_page = get_sum_page(sbi, new_segno);
1529 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
1530 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
1531 f2fs_put_page(sum_page, 1);
1532 }
1533}
1534
43727527
JK
1535static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
1536{
1537 struct curseg_info *curseg = CURSEG_I(sbi, type);
1538 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
1539
7f3037a5 1540 if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0, 0))
43727527
JK
1541 return v_ops->get_victim(sbi,
1542 &(curseg)->next_segno, BG_GC, type, SSR);
1543
1544 /* For data segments, let's do SSR more intensively */
1545 for (; type >= CURSEG_HOT_DATA; type--)
1546 if (v_ops->get_victim(sbi, &(curseg)->next_segno,
1547 BG_GC, type, SSR))
1548 return 1;
1549 return 0;
1550}
1551
351df4b2
JK
1552/*
1553 * flush out current segment and replace it with new segment
1554 * This function should be returned with success, otherwise BUG
1555 */
1556static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
1557 int type, bool force)
1558{
1559 struct curseg_info *curseg = CURSEG_I(sbi, type);
351df4b2 1560
7b405275 1561 if (force)
351df4b2 1562 new_curseg(sbi, type, true);
7b405275 1563 else if (type == CURSEG_WARM_NODE)
351df4b2 1564 new_curseg(sbi, type, false);
60374688
JK
1565 else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
1566 new_curseg(sbi, type, false);
351df4b2
JK
1567 else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
1568 change_curseg(sbi, type, true);
1569 else
1570 new_curseg(sbi, type, false);
dcdfff65
JK
1571
1572 stat_inc_seg_type(sbi, curseg);
351df4b2
JK
1573}
1574
1575void allocate_new_segments(struct f2fs_sb_info *sbi)
1576{
6ae1be13
JK
1577 struct curseg_info *curseg;
1578 unsigned int old_segno;
351df4b2
JK
1579 int i;
1580
6ae1be13
JK
1581 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1582 curseg = CURSEG_I(sbi, i);
1583 old_segno = curseg->segno;
1584 SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
1585 locate_dirty_segment(sbi, old_segno);
1586 }
351df4b2
JK
1587}
1588
1589static const struct segment_allocation default_salloc_ops = {
1590 .allocate_segment = allocate_segment_by_default,
1591};
1592
25290fa5
JK
1593bool exist_trim_candidates(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1594{
1595 __u64 trim_start = cpc->trim_start;
1596 bool has_candidate = false;
1597
1598 mutex_lock(&SIT_I(sbi)->sentry_lock);
1599 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++) {
1600 if (add_discard_addrs(sbi, cpc, true)) {
1601 has_candidate = true;
1602 break;
1603 }
1604 }
1605 mutex_unlock(&SIT_I(sbi)->sentry_lock);
1606
1607 cpc->trim_start = trim_start;
1608 return has_candidate;
1609}
1610
4b2fecc8
JK
1611int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range)
1612{
f7ef9b83
JK
1613 __u64 start = F2FS_BYTES_TO_BLK(range->start);
1614 __u64 end = start + F2FS_BYTES_TO_BLK(range->len) - 1;
4b2fecc8
JK
1615 unsigned int start_segno, end_segno;
1616 struct cp_control cpc;
c34f42e2 1617 int err = 0;
4b2fecc8 1618
836b5a63 1619 if (start >= MAX_BLKADDR(sbi) || range->len < sbi->blocksize)
4b2fecc8
JK
1620 return -EINVAL;
1621
9bd27ae4 1622 cpc.trimmed = 0;
7cd8558b 1623 if (end <= MAIN_BLKADDR(sbi))
4b2fecc8
JK
1624 goto out;
1625
ed214a11
YH
1626 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) {
1627 f2fs_msg(sbi->sb, KERN_WARNING,
1628 "Found FS corruption, run fsck to fix.");
1629 goto out;
1630 }
1631
4b2fecc8 1632 /* start/end segment number in main_area */
7cd8558b
JK
1633 start_segno = (start <= MAIN_BLKADDR(sbi)) ? 0 : GET_SEGNO(sbi, start);
1634 end_segno = (end >= MAX_BLKADDR(sbi)) ? MAIN_SEGS(sbi) - 1 :
1635 GET_SEGNO(sbi, end);
4b2fecc8 1636 cpc.reason = CP_DISCARD;
836b5a63 1637 cpc.trim_minlen = max_t(__u64, 1, F2FS_BYTES_TO_BLK(range->minlen));
4b2fecc8
JK
1638
1639 /* do checkpoint to issue discard commands safely */
bba681cb
JK
1640 for (; start_segno <= end_segno; start_segno = cpc.trim_end + 1) {
1641 cpc.trim_start = start_segno;
a66cdd98
JK
1642
1643 if (sbi->discard_blks == 0)
1644 break;
1645 else if (sbi->discard_blks < BATCHED_TRIM_BLOCKS(sbi))
1646 cpc.trim_end = end_segno;
1647 else
1648 cpc.trim_end = min_t(unsigned int,
1649 rounddown(start_segno +
bba681cb
JK
1650 BATCHED_TRIM_SEGMENTS(sbi),
1651 sbi->segs_per_sec) - 1, end_segno);
1652
1653 mutex_lock(&sbi->gc_mutex);
c34f42e2 1654 err = write_checkpoint(sbi, &cpc);
bba681cb 1655 mutex_unlock(&sbi->gc_mutex);
e9328353
CY
1656 if (err)
1657 break;
74fa5f3d
CY
1658
1659 schedule();
bba681cb 1660 }
4b2fecc8 1661out:
f7ef9b83 1662 range->len = F2FS_BLK_TO_BYTES(cpc.trimmed);
c34f42e2 1663 return err;
4b2fecc8
JK
1664}
1665
351df4b2
JK
1666static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
1667{
1668 struct curseg_info *curseg = CURSEG_I(sbi, type);
1669 if (curseg->next_blkoff < sbi->blocks_per_seg)
1670 return true;
1671 return false;
1672}
1673
1674static int __get_segment_type_2(struct page *page, enum page_type p_type)
1675{
1676 if (p_type == DATA)
1677 return CURSEG_HOT_DATA;
1678 else
1679 return CURSEG_HOT_NODE;
1680}
1681
1682static int __get_segment_type_4(struct page *page, enum page_type p_type)
1683{
1684 if (p_type == DATA) {
1685 struct inode *inode = page->mapping->host;
1686
1687 if (S_ISDIR(inode->i_mode))
1688 return CURSEG_HOT_DATA;
1689 else
1690 return CURSEG_COLD_DATA;
1691 } else {
a344b9fd
JK
1692 if (IS_DNODE(page) && is_cold_node(page))
1693 return CURSEG_WARM_NODE;
351df4b2
JK
1694 else
1695 return CURSEG_COLD_NODE;
1696 }
1697}
1698
1699static int __get_segment_type_6(struct page *page, enum page_type p_type)
1700{
1701 if (p_type == DATA) {
1702 struct inode *inode = page->mapping->host;
1703
1704 if (S_ISDIR(inode->i_mode))
1705 return CURSEG_HOT_DATA;
354a3399 1706 else if (is_cold_data(page) || file_is_cold(inode))
351df4b2
JK
1707 return CURSEG_COLD_DATA;
1708 else
1709 return CURSEG_WARM_DATA;
1710 } else {
1711 if (IS_DNODE(page))
1712 return is_cold_node(page) ? CURSEG_WARM_NODE :
1713 CURSEG_HOT_NODE;
1714 else
1715 return CURSEG_COLD_NODE;
1716 }
1717}
1718
1719static int __get_segment_type(struct page *page, enum page_type p_type)
1720{
4081363f 1721 switch (F2FS_P_SB(page)->active_logs) {
351df4b2
JK
1722 case 2:
1723 return __get_segment_type_2(page, p_type);
1724 case 4:
1725 return __get_segment_type_4(page, p_type);
351df4b2 1726 }
12a67146 1727 /* NR_CURSEG_TYPE(6) logs by default */
9850cf4a
JK
1728 f2fs_bug_on(F2FS_P_SB(page),
1729 F2FS_P_SB(page)->active_logs != NR_CURSEG_TYPE);
12a67146 1730 return __get_segment_type_6(page, p_type);
351df4b2
JK
1731}
1732
bfad7c2d
JK
1733void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
1734 block_t old_blkaddr, block_t *new_blkaddr,
1735 struct f2fs_summary *sum, int type)
351df4b2
JK
1736{
1737 struct sit_info *sit_i = SIT_I(sbi);
6ae1be13 1738 struct curseg_info *curseg = CURSEG_I(sbi, type);
351df4b2
JK
1739
1740 mutex_lock(&curseg->curseg_mutex);
21cb1d99 1741 mutex_lock(&sit_i->sentry_lock);
351df4b2
JK
1742
1743 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
351df4b2 1744
4e6a8d9b
JK
1745 f2fs_wait_discard_bio(sbi, *new_blkaddr);
1746
351df4b2
JK
1747 /*
1748 * __add_sum_entry should be resided under the curseg_mutex
1749 * because, this function updates a summary entry in the
1750 * current summary block.
1751 */
e79efe3b 1752 __add_sum_entry(sbi, type, sum);
351df4b2 1753
351df4b2 1754 __refresh_next_blkoff(sbi, curseg);
dcdfff65
JK
1755
1756 stat_inc_block_count(sbi, curseg);
351df4b2 1757
5e443818
JK
1758 if (!__has_curseg_space(sbi, type))
1759 sit_i->s_ops->allocate_segment(sbi, type, false);
351df4b2
JK
1760 /*
1761 * SIT information should be updated before segment allocation,
1762 * since SSR needs latest valid block information.
1763 */
1764 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
5e443818 1765
351df4b2
JK
1766 mutex_unlock(&sit_i->sentry_lock);
1767
bfad7c2d 1768 if (page && IS_NODESEG(type))
351df4b2
JK
1769 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
1770
bfad7c2d
JK
1771 mutex_unlock(&curseg->curseg_mutex);
1772}
1773
05ca3632 1774static void do_write_page(struct f2fs_summary *sum, struct f2fs_io_info *fio)
bfad7c2d 1775{
05ca3632 1776 int type = __get_segment_type(fio->page, fio->type);
0a595eba 1777 int err;
bfad7c2d 1778
7dfeaa32
JK
1779 if (fio->type == NODE || fio->type == DATA)
1780 mutex_lock(&fio->sbi->wio_mutex[fio->type]);
0a595eba 1781reallocate:
7a9d7548
CY
1782 allocate_data_block(fio->sbi, fio->page, fio->old_blkaddr,
1783 &fio->new_blkaddr, sum, type);
bfad7c2d 1784
351df4b2 1785 /* writeout dirty page into bdev */
0a595eba
JK
1786 err = f2fs_submit_page_mbio(fio);
1787 if (err == -EAGAIN) {
1788 fio->old_blkaddr = fio->new_blkaddr;
1789 goto reallocate;
1790 }
7dfeaa32
JK
1791
1792 if (fio->type == NODE || fio->type == DATA)
1793 mutex_unlock(&fio->sbi->wio_mutex[fio->type]);
351df4b2
JK
1794}
1795
577e3495 1796void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
351df4b2 1797{
458e6197 1798 struct f2fs_io_info fio = {
05ca3632 1799 .sbi = sbi,
458e6197 1800 .type = META,
04d328de 1801 .op = REQ_OP_WRITE,
70fd7614 1802 .op_flags = REQ_SYNC | REQ_META | REQ_PRIO,
7a9d7548
CY
1803 .old_blkaddr = page->index,
1804 .new_blkaddr = page->index,
05ca3632 1805 .page = page,
4375a336 1806 .encrypted_page = NULL,
458e6197
JK
1807 };
1808
2b947003 1809 if (unlikely(page->index >= MAIN_BLKADDR(sbi)))
04d328de 1810 fio.op_flags &= ~REQ_META;
2b947003 1811
351df4b2 1812 set_page_writeback(page);
05ca3632 1813 f2fs_submit_page_mbio(&fio);
351df4b2
JK
1814}
1815
05ca3632 1816void write_node_page(unsigned int nid, struct f2fs_io_info *fio)
351df4b2
JK
1817{
1818 struct f2fs_summary sum;
05ca3632 1819
351df4b2 1820 set_summary(&sum, nid, 0, 0);
05ca3632 1821 do_write_page(&sum, fio);
351df4b2
JK
1822}
1823
05ca3632 1824void write_data_page(struct dnode_of_data *dn, struct f2fs_io_info *fio)
351df4b2 1825{
05ca3632 1826 struct f2fs_sb_info *sbi = fio->sbi;
351df4b2
JK
1827 struct f2fs_summary sum;
1828 struct node_info ni;
1829
9850cf4a 1830 f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR);
351df4b2
JK
1831 get_node_info(sbi, dn->nid, &ni);
1832 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
05ca3632 1833 do_write_page(&sum, fio);
f28b3434 1834 f2fs_update_data_blkaddr(dn, fio->new_blkaddr);
351df4b2
JK
1835}
1836
05ca3632 1837void rewrite_data_page(struct f2fs_io_info *fio)
351df4b2 1838{
7a9d7548 1839 fio->new_blkaddr = fio->old_blkaddr;
05ca3632
JK
1840 stat_inc_inplace_blocks(fio->sbi);
1841 f2fs_submit_page_mbio(fio);
351df4b2
JK
1842}
1843
4356e48e 1844void __f2fs_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
19f106bc 1845 block_t old_blkaddr, block_t new_blkaddr,
28bc106b 1846 bool recover_curseg, bool recover_newaddr)
351df4b2
JK
1847{
1848 struct sit_info *sit_i = SIT_I(sbi);
1849 struct curseg_info *curseg;
1850 unsigned int segno, old_cursegno;
1851 struct seg_entry *se;
1852 int type;
19f106bc 1853 unsigned short old_blkoff;
351df4b2
JK
1854
1855 segno = GET_SEGNO(sbi, new_blkaddr);
1856 se = get_seg_entry(sbi, segno);
1857 type = se->type;
1858
19f106bc
CY
1859 if (!recover_curseg) {
1860 /* for recovery flow */
1861 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
1862 if (old_blkaddr == NULL_ADDR)
1863 type = CURSEG_COLD_DATA;
1864 else
1865 type = CURSEG_WARM_DATA;
1866 }
1867 } else {
1868 if (!IS_CURSEG(sbi, segno))
351df4b2
JK
1869 type = CURSEG_WARM_DATA;
1870 }
19f106bc 1871
351df4b2
JK
1872 curseg = CURSEG_I(sbi, type);
1873
1874 mutex_lock(&curseg->curseg_mutex);
1875 mutex_lock(&sit_i->sentry_lock);
1876
1877 old_cursegno = curseg->segno;
19f106bc 1878 old_blkoff = curseg->next_blkoff;
351df4b2
JK
1879
1880 /* change the current segment */
1881 if (segno != curseg->segno) {
1882 curseg->next_segno = segno;
1883 change_curseg(sbi, type, true);
1884 }
1885
491c0854 1886 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
e79efe3b 1887 __add_sum_entry(sbi, type, sum);
351df4b2 1888
28bc106b 1889 if (!recover_curseg || recover_newaddr)
6e2c64ad
JK
1890 update_sit_entry(sbi, new_blkaddr, 1);
1891 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
1892 update_sit_entry(sbi, old_blkaddr, -1);
1893
1894 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
1895 locate_dirty_segment(sbi, GET_SEGNO(sbi, new_blkaddr));
1896
351df4b2 1897 locate_dirty_segment(sbi, old_cursegno);
351df4b2 1898
19f106bc
CY
1899 if (recover_curseg) {
1900 if (old_cursegno != curseg->segno) {
1901 curseg->next_segno = old_cursegno;
1902 change_curseg(sbi, type, true);
1903 }
1904 curseg->next_blkoff = old_blkoff;
1905 }
1906
351df4b2
JK
1907 mutex_unlock(&sit_i->sentry_lock);
1908 mutex_unlock(&curseg->curseg_mutex);
1909}
1910
528e3459
CY
1911void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
1912 block_t old_addr, block_t new_addr,
28bc106b
CY
1913 unsigned char version, bool recover_curseg,
1914 bool recover_newaddr)
528e3459
CY
1915{
1916 struct f2fs_summary sum;
1917
1918 set_summary(&sum, dn->nid, dn->ofs_in_node, version);
1919
28bc106b
CY
1920 __f2fs_replace_block(sbi, &sum, old_addr, new_addr,
1921 recover_curseg, recover_newaddr);
528e3459 1922
f28b3434 1923 f2fs_update_data_blkaddr(dn, new_addr);
528e3459
CY
1924}
1925
93dfe2ac 1926void f2fs_wait_on_page_writeback(struct page *page,
fec1d657 1927 enum page_type type, bool ordered)
93dfe2ac 1928{
93dfe2ac 1929 if (PageWriteback(page)) {
4081363f
JK
1930 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
1931
0c3a5797 1932 f2fs_submit_merged_bio_cond(sbi, NULL, page, 0, type, WRITE);
fec1d657
JK
1933 if (ordered)
1934 wait_on_page_writeback(page);
1935 else
1936 wait_for_stable_page(page);
93dfe2ac
JK
1937 }
1938}
1939
08b39fbd
CY
1940void f2fs_wait_on_encrypted_page_writeback(struct f2fs_sb_info *sbi,
1941 block_t blkaddr)
1942{
1943 struct page *cpage;
1944
5d4c0af4 1945 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR)
08b39fbd
CY
1946 return;
1947
08b39fbd
CY
1948 cpage = find_lock_page(META_MAPPING(sbi), blkaddr);
1949 if (cpage) {
fec1d657 1950 f2fs_wait_on_page_writeback(cpage, DATA, true);
08b39fbd
CY
1951 f2fs_put_page(cpage, 1);
1952 }
1953}
1954
351df4b2
JK
1955static int read_compacted_summaries(struct f2fs_sb_info *sbi)
1956{
1957 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1958 struct curseg_info *seg_i;
1959 unsigned char *kaddr;
1960 struct page *page;
1961 block_t start;
1962 int i, j, offset;
1963
1964 start = start_sum_block(sbi);
1965
1966 page = get_meta_page(sbi, start++);
1967 kaddr = (unsigned char *)page_address(page);
1968
1969 /* Step 1: restore nat cache */
1970 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
b7ad7512 1971 memcpy(seg_i->journal, kaddr, SUM_JOURNAL_SIZE);
351df4b2
JK
1972
1973 /* Step 2: restore sit cache */
1974 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
b7ad7512 1975 memcpy(seg_i->journal, kaddr + SUM_JOURNAL_SIZE, SUM_JOURNAL_SIZE);
351df4b2
JK
1976 offset = 2 * SUM_JOURNAL_SIZE;
1977
1978 /* Step 3: restore summary entries */
1979 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1980 unsigned short blk_off;
1981 unsigned int segno;
1982
1983 seg_i = CURSEG_I(sbi, i);
1984 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
1985 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
1986 seg_i->next_segno = segno;
1987 reset_curseg(sbi, i, 0);
1988 seg_i->alloc_type = ckpt->alloc_type[i];
1989 seg_i->next_blkoff = blk_off;
1990
1991 if (seg_i->alloc_type == SSR)
1992 blk_off = sbi->blocks_per_seg;
1993
1994 for (j = 0; j < blk_off; j++) {
1995 struct f2fs_summary *s;
1996 s = (struct f2fs_summary *)(kaddr + offset);
1997 seg_i->sum_blk->entries[j] = *s;
1998 offset += SUMMARY_SIZE;
09cbfeaf 1999 if (offset + SUMMARY_SIZE <= PAGE_SIZE -
351df4b2
JK
2000 SUM_FOOTER_SIZE)
2001 continue;
2002
2003 f2fs_put_page(page, 1);
2004 page = NULL;
2005
2006 page = get_meta_page(sbi, start++);
2007 kaddr = (unsigned char *)page_address(page);
2008 offset = 0;
2009 }
2010 }
2011 f2fs_put_page(page, 1);
2012 return 0;
2013}
2014
2015static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
2016{
2017 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
2018 struct f2fs_summary_block *sum;
2019 struct curseg_info *curseg;
2020 struct page *new;
2021 unsigned short blk_off;
2022 unsigned int segno = 0;
2023 block_t blk_addr = 0;
2024
2025 /* get segment number and block addr */
2026 if (IS_DATASEG(type)) {
2027 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
2028 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
2029 CURSEG_HOT_DATA]);
119ee914 2030 if (__exist_node_summaries(sbi))
351df4b2
JK
2031 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
2032 else
2033 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
2034 } else {
2035 segno = le32_to_cpu(ckpt->cur_node_segno[type -
2036 CURSEG_HOT_NODE]);
2037 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
2038 CURSEG_HOT_NODE]);
119ee914 2039 if (__exist_node_summaries(sbi))
351df4b2
JK
2040 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
2041 type - CURSEG_HOT_NODE);
2042 else
2043 blk_addr = GET_SUM_BLOCK(sbi, segno);
2044 }
2045
2046 new = get_meta_page(sbi, blk_addr);
2047 sum = (struct f2fs_summary_block *)page_address(new);
2048
2049 if (IS_NODESEG(type)) {
119ee914 2050 if (__exist_node_summaries(sbi)) {
351df4b2
JK
2051 struct f2fs_summary *ns = &sum->entries[0];
2052 int i;
2053 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
2054 ns->version = 0;
2055 ns->ofs_in_node = 0;
2056 }
2057 } else {
d653788a
GZ
2058 int err;
2059
2060 err = restore_node_summary(sbi, segno, sum);
2061 if (err) {
351df4b2 2062 f2fs_put_page(new, 1);
d653788a 2063 return err;
351df4b2
JK
2064 }
2065 }
2066 }
2067
2068 /* set uncompleted segment to curseg */
2069 curseg = CURSEG_I(sbi, type);
2070 mutex_lock(&curseg->curseg_mutex);
b7ad7512
CY
2071
2072 /* update journal info */
2073 down_write(&curseg->journal_rwsem);
2074 memcpy(curseg->journal, &sum->journal, SUM_JOURNAL_SIZE);
2075 up_write(&curseg->journal_rwsem);
2076
2077 memcpy(curseg->sum_blk->entries, sum->entries, SUM_ENTRY_SIZE);
2078 memcpy(&curseg->sum_blk->footer, &sum->footer, SUM_FOOTER_SIZE);
351df4b2
JK
2079 curseg->next_segno = segno;
2080 reset_curseg(sbi, type, 0);
2081 curseg->alloc_type = ckpt->alloc_type[type];
2082 curseg->next_blkoff = blk_off;
2083 mutex_unlock(&curseg->curseg_mutex);
2084 f2fs_put_page(new, 1);
2085 return 0;
2086}
2087
2088static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
2089{
2090 int type = CURSEG_HOT_DATA;
e4fc5fbf 2091 int err;
351df4b2 2092
aaec2b1d 2093 if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG)) {
3fa06d7b
CY
2094 int npages = npages_for_summary_flush(sbi, true);
2095
2096 if (npages >= 2)
2097 ra_meta_pages(sbi, start_sum_block(sbi), npages,
26879fb1 2098 META_CP, true);
3fa06d7b 2099
351df4b2
JK
2100 /* restore for compacted data summary */
2101 if (read_compacted_summaries(sbi))
2102 return -EINVAL;
2103 type = CURSEG_HOT_NODE;
2104 }
2105
119ee914 2106 if (__exist_node_summaries(sbi))
3fa06d7b 2107 ra_meta_pages(sbi, sum_blk_addr(sbi, NR_CURSEG_TYPE, type),
26879fb1 2108 NR_CURSEG_TYPE - type, META_CP, true);
3fa06d7b 2109
e4fc5fbf
CY
2110 for (; type <= CURSEG_COLD_NODE; type++) {
2111 err = read_normal_summaries(sbi, type);
2112 if (err)
2113 return err;
2114 }
2115
351df4b2
JK
2116 return 0;
2117}
2118
2119static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
2120{
2121 struct page *page;
2122 unsigned char *kaddr;
2123 struct f2fs_summary *summary;
2124 struct curseg_info *seg_i;
2125 int written_size = 0;
2126 int i, j;
2127
2128 page = grab_meta_page(sbi, blkaddr++);
2129 kaddr = (unsigned char *)page_address(page);
2130
2131 /* Step 1: write nat cache */
2132 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
b7ad7512 2133 memcpy(kaddr, seg_i->journal, SUM_JOURNAL_SIZE);
351df4b2
JK
2134 written_size += SUM_JOURNAL_SIZE;
2135
2136 /* Step 2: write sit cache */
2137 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
b7ad7512 2138 memcpy(kaddr + written_size, seg_i->journal, SUM_JOURNAL_SIZE);
351df4b2
JK
2139 written_size += SUM_JOURNAL_SIZE;
2140
351df4b2
JK
2141 /* Step 3: write summary entries */
2142 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
2143 unsigned short blkoff;
2144 seg_i = CURSEG_I(sbi, i);
2145 if (sbi->ckpt->alloc_type[i] == SSR)
2146 blkoff = sbi->blocks_per_seg;
2147 else
2148 blkoff = curseg_blkoff(sbi, i);
2149
2150 for (j = 0; j < blkoff; j++) {
2151 if (!page) {
2152 page = grab_meta_page(sbi, blkaddr++);
2153 kaddr = (unsigned char *)page_address(page);
2154 written_size = 0;
2155 }
2156 summary = (struct f2fs_summary *)(kaddr + written_size);
2157 *summary = seg_i->sum_blk->entries[j];
2158 written_size += SUMMARY_SIZE;
351df4b2 2159
09cbfeaf 2160 if (written_size + SUMMARY_SIZE <= PAGE_SIZE -
351df4b2
JK
2161 SUM_FOOTER_SIZE)
2162 continue;
2163
e8d61a74 2164 set_page_dirty(page);
351df4b2
JK
2165 f2fs_put_page(page, 1);
2166 page = NULL;
2167 }
2168 }
e8d61a74
CY
2169 if (page) {
2170 set_page_dirty(page);
351df4b2 2171 f2fs_put_page(page, 1);
e8d61a74 2172 }
351df4b2
JK
2173}
2174
2175static void write_normal_summaries(struct f2fs_sb_info *sbi,
2176 block_t blkaddr, int type)
2177{
2178 int i, end;
2179 if (IS_DATASEG(type))
2180 end = type + NR_CURSEG_DATA_TYPE;
2181 else
2182 end = type + NR_CURSEG_NODE_TYPE;
2183
b7ad7512
CY
2184 for (i = type; i < end; i++)
2185 write_current_sum_page(sbi, i, blkaddr + (i - type));
351df4b2
JK
2186}
2187
2188void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
2189{
aaec2b1d 2190 if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG))
351df4b2
JK
2191 write_compacted_summaries(sbi, start_blk);
2192 else
2193 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
2194}
2195
2196void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
2197{
119ee914 2198 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
351df4b2
JK
2199}
2200
dfc08a12 2201int lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
351df4b2
JK
2202 unsigned int val, int alloc)
2203{
2204 int i;
2205
2206 if (type == NAT_JOURNAL) {
dfc08a12
CY
2207 for (i = 0; i < nats_in_cursum(journal); i++) {
2208 if (le32_to_cpu(nid_in_journal(journal, i)) == val)
351df4b2
JK
2209 return i;
2210 }
dfc08a12
CY
2211 if (alloc && __has_cursum_space(journal, 1, NAT_JOURNAL))
2212 return update_nats_in_cursum(journal, 1);
351df4b2 2213 } else if (type == SIT_JOURNAL) {
dfc08a12
CY
2214 for (i = 0; i < sits_in_cursum(journal); i++)
2215 if (le32_to_cpu(segno_in_journal(journal, i)) == val)
351df4b2 2216 return i;
dfc08a12
CY
2217 if (alloc && __has_cursum_space(journal, 1, SIT_JOURNAL))
2218 return update_sits_in_cursum(journal, 1);
351df4b2
JK
2219 }
2220 return -1;
2221}
2222
2223static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
2224 unsigned int segno)
2225{
2cc22186 2226 return get_meta_page(sbi, current_sit_addr(sbi, segno));
351df4b2
JK
2227}
2228
2229static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
2230 unsigned int start)
2231{
2232 struct sit_info *sit_i = SIT_I(sbi);
2233 struct page *src_page, *dst_page;
2234 pgoff_t src_off, dst_off;
2235 void *src_addr, *dst_addr;
2236
2237 src_off = current_sit_addr(sbi, start);
2238 dst_off = next_sit_addr(sbi, src_off);
2239
2240 /* get current sit block page without lock */
2241 src_page = get_meta_page(sbi, src_off);
2242 dst_page = grab_meta_page(sbi, dst_off);
9850cf4a 2243 f2fs_bug_on(sbi, PageDirty(src_page));
351df4b2
JK
2244
2245 src_addr = page_address(src_page);
2246 dst_addr = page_address(dst_page);
09cbfeaf 2247 memcpy(dst_addr, src_addr, PAGE_SIZE);
351df4b2
JK
2248
2249 set_page_dirty(dst_page);
2250 f2fs_put_page(src_page, 1);
2251
2252 set_to_next_sit(sit_i, start);
2253
2254 return dst_page;
2255}
2256
184a5cd2
CY
2257static struct sit_entry_set *grab_sit_entry_set(void)
2258{
2259 struct sit_entry_set *ses =
80c54505 2260 f2fs_kmem_cache_alloc(sit_entry_set_slab, GFP_NOFS);
184a5cd2
CY
2261
2262 ses->entry_cnt = 0;
2263 INIT_LIST_HEAD(&ses->set_list);
2264 return ses;
2265}
2266
2267static void release_sit_entry_set(struct sit_entry_set *ses)
2268{
2269 list_del(&ses->set_list);
2270 kmem_cache_free(sit_entry_set_slab, ses);
2271}
2272
2273static void adjust_sit_entry_set(struct sit_entry_set *ses,
2274 struct list_head *head)
2275{
2276 struct sit_entry_set *next = ses;
2277
2278 if (list_is_last(&ses->set_list, head))
2279 return;
2280
2281 list_for_each_entry_continue(next, head, set_list)
2282 if (ses->entry_cnt <= next->entry_cnt)
2283 break;
2284
2285 list_move_tail(&ses->set_list, &next->set_list);
2286}
2287
2288static void add_sit_entry(unsigned int segno, struct list_head *head)
2289{
2290 struct sit_entry_set *ses;
2291 unsigned int start_segno = START_SEGNO(segno);
2292
2293 list_for_each_entry(ses, head, set_list) {
2294 if (ses->start_segno == start_segno) {
2295 ses->entry_cnt++;
2296 adjust_sit_entry_set(ses, head);
2297 return;
2298 }
2299 }
2300
2301 ses = grab_sit_entry_set();
2302
2303 ses->start_segno = start_segno;
2304 ses->entry_cnt++;
2305 list_add(&ses->set_list, head);
2306}
2307
2308static void add_sits_in_set(struct f2fs_sb_info *sbi)
2309{
2310 struct f2fs_sm_info *sm_info = SM_I(sbi);
2311 struct list_head *set_list = &sm_info->sit_entry_set;
2312 unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap;
184a5cd2
CY
2313 unsigned int segno;
2314
7cd8558b 2315 for_each_set_bit(segno, bitmap, MAIN_SEGS(sbi))
184a5cd2
CY
2316 add_sit_entry(segno, set_list);
2317}
2318
2319static void remove_sits_in_journal(struct f2fs_sb_info *sbi)
351df4b2
JK
2320{
2321 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
b7ad7512 2322 struct f2fs_journal *journal = curseg->journal;
351df4b2
JK
2323 int i;
2324
b7ad7512 2325 down_write(&curseg->journal_rwsem);
dfc08a12 2326 for (i = 0; i < sits_in_cursum(journal); i++) {
184a5cd2
CY
2327 unsigned int segno;
2328 bool dirtied;
2329
dfc08a12 2330 segno = le32_to_cpu(segno_in_journal(journal, i));
184a5cd2
CY
2331 dirtied = __mark_sit_entry_dirty(sbi, segno);
2332
2333 if (!dirtied)
2334 add_sit_entry(segno, &SM_I(sbi)->sit_entry_set);
351df4b2 2335 }
dfc08a12 2336 update_sits_in_cursum(journal, -i);
b7ad7512 2337 up_write(&curseg->journal_rwsem);
351df4b2
JK
2338}
2339
0a8165d7 2340/*
351df4b2
JK
2341 * CP calls this function, which flushes SIT entries including sit_journal,
2342 * and moves prefree segs to free segs.
2343 */
4b2fecc8 2344void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
351df4b2
JK
2345{
2346 struct sit_info *sit_i = SIT_I(sbi);
2347 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
2348 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
b7ad7512 2349 struct f2fs_journal *journal = curseg->journal;
184a5cd2
CY
2350 struct sit_entry_set *ses, *tmp;
2351 struct list_head *head = &SM_I(sbi)->sit_entry_set;
184a5cd2 2352 bool to_journal = true;
4b2fecc8 2353 struct seg_entry *se;
351df4b2 2354
351df4b2
JK
2355 mutex_lock(&sit_i->sentry_lock);
2356
2b11a74b
WL
2357 if (!sit_i->dirty_sentries)
2358 goto out;
2359
351df4b2 2360 /*
184a5cd2
CY
2361 * add and account sit entries of dirty bitmap in sit entry
2362 * set temporarily
351df4b2 2363 */
184a5cd2 2364 add_sits_in_set(sbi);
351df4b2 2365
184a5cd2
CY
2366 /*
2367 * if there are no enough space in journal to store dirty sit
2368 * entries, remove all entries from journal and add and account
2369 * them in sit entry set.
2370 */
dfc08a12 2371 if (!__has_cursum_space(journal, sit_i->dirty_sentries, SIT_JOURNAL))
184a5cd2 2372 remove_sits_in_journal(sbi);
b2955550 2373
184a5cd2
CY
2374 /*
2375 * there are two steps to flush sit entries:
2376 * #1, flush sit entries to journal in current cold data summary block.
2377 * #2, flush sit entries to sit page.
2378 */
2379 list_for_each_entry_safe(ses, tmp, head, set_list) {
4a257ed6 2380 struct page *page = NULL;
184a5cd2
CY
2381 struct f2fs_sit_block *raw_sit = NULL;
2382 unsigned int start_segno = ses->start_segno;
2383 unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK,
7cd8558b 2384 (unsigned long)MAIN_SEGS(sbi));
184a5cd2
CY
2385 unsigned int segno = start_segno;
2386
2387 if (to_journal &&
dfc08a12 2388 !__has_cursum_space(journal, ses->entry_cnt, SIT_JOURNAL))
184a5cd2
CY
2389 to_journal = false;
2390
b7ad7512
CY
2391 if (to_journal) {
2392 down_write(&curseg->journal_rwsem);
2393 } else {
184a5cd2
CY
2394 page = get_next_sit_page(sbi, start_segno);
2395 raw_sit = page_address(page);
351df4b2 2396 }
351df4b2 2397
184a5cd2
CY
2398 /* flush dirty sit entries in region of current sit set */
2399 for_each_set_bit_from(segno, bitmap, end) {
2400 int offset, sit_offset;
4b2fecc8
JK
2401
2402 se = get_seg_entry(sbi, segno);
184a5cd2
CY
2403
2404 /* add discard candidates */
d7bc2484 2405 if (cpc->reason != CP_DISCARD) {
4b2fecc8 2406 cpc->trim_start = segno;
25290fa5 2407 add_discard_addrs(sbi, cpc, false);
4b2fecc8 2408 }
184a5cd2
CY
2409
2410 if (to_journal) {
dfc08a12 2411 offset = lookup_journal_in_cursum(journal,
184a5cd2
CY
2412 SIT_JOURNAL, segno, 1);
2413 f2fs_bug_on(sbi, offset < 0);
dfc08a12 2414 segno_in_journal(journal, offset) =
184a5cd2
CY
2415 cpu_to_le32(segno);
2416 seg_info_to_raw_sit(se,
dfc08a12 2417 &sit_in_journal(journal, offset));
184a5cd2
CY
2418 } else {
2419 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
2420 seg_info_to_raw_sit(se,
2421 &raw_sit->entries[sit_offset]);
2422 }
351df4b2 2423
184a5cd2
CY
2424 __clear_bit(segno, bitmap);
2425 sit_i->dirty_sentries--;
2426 ses->entry_cnt--;
351df4b2
JK
2427 }
2428
b7ad7512
CY
2429 if (to_journal)
2430 up_write(&curseg->journal_rwsem);
2431 else
184a5cd2
CY
2432 f2fs_put_page(page, 1);
2433
2434 f2fs_bug_on(sbi, ses->entry_cnt);
2435 release_sit_entry_set(ses);
351df4b2 2436 }
184a5cd2
CY
2437
2438 f2fs_bug_on(sbi, !list_empty(head));
2439 f2fs_bug_on(sbi, sit_i->dirty_sentries);
184a5cd2 2440out:
4b2fecc8 2441 if (cpc->reason == CP_DISCARD) {
650d3c4e
YH
2442 __u64 trim_start = cpc->trim_start;
2443
4b2fecc8 2444 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++)
25290fa5 2445 add_discard_addrs(sbi, cpc, false);
650d3c4e
YH
2446
2447 cpc->trim_start = trim_start;
4b2fecc8 2448 }
351df4b2 2449 mutex_unlock(&sit_i->sentry_lock);
351df4b2 2450
351df4b2
JK
2451 set_prefree_as_free_segments(sbi);
2452}
2453
2454static int build_sit_info(struct f2fs_sb_info *sbi)
2455{
2456 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
351df4b2
JK
2457 struct sit_info *sit_i;
2458 unsigned int sit_segs, start;
ae27d62e 2459 char *src_bitmap;
351df4b2
JK
2460 unsigned int bitmap_size;
2461
2462 /* allocate memory for SIT information */
2463 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
2464 if (!sit_i)
2465 return -ENOMEM;
2466
2467 SM_I(sbi)->sit_info = sit_i;
2468
39307a8e
JK
2469 sit_i->sentries = f2fs_kvzalloc(MAIN_SEGS(sbi) *
2470 sizeof(struct seg_entry), GFP_KERNEL);
351df4b2
JK
2471 if (!sit_i->sentries)
2472 return -ENOMEM;
2473
7cd8558b 2474 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
39307a8e 2475 sit_i->dirty_sentries_bitmap = f2fs_kvzalloc(bitmap_size, GFP_KERNEL);
351df4b2
JK
2476 if (!sit_i->dirty_sentries_bitmap)
2477 return -ENOMEM;
2478
7cd8558b 2479 for (start = 0; start < MAIN_SEGS(sbi); start++) {
351df4b2
JK
2480 sit_i->sentries[start].cur_valid_map
2481 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
2482 sit_i->sentries[start].ckpt_valid_map
2483 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
a66cdd98 2484 if (!sit_i->sentries[start].cur_valid_map ||
3e025740 2485 !sit_i->sentries[start].ckpt_valid_map)
351df4b2 2486 return -ENOMEM;
3e025740 2487
355e7891
CY
2488#ifdef CONFIG_F2FS_CHECK_FS
2489 sit_i->sentries[start].cur_valid_map_mir
2490 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
2491 if (!sit_i->sentries[start].cur_valid_map_mir)
2492 return -ENOMEM;
2493#endif
2494
3e025740
JK
2495 if (f2fs_discard_en(sbi)) {
2496 sit_i->sentries[start].discard_map
2497 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
2498 if (!sit_i->sentries[start].discard_map)
2499 return -ENOMEM;
2500 }
351df4b2
JK
2501 }
2502
60a3b782
JK
2503 sit_i->tmp_map = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
2504 if (!sit_i->tmp_map)
2505 return -ENOMEM;
2506
351df4b2 2507 if (sbi->segs_per_sec > 1) {
39307a8e
JK
2508 sit_i->sec_entries = f2fs_kvzalloc(MAIN_SECS(sbi) *
2509 sizeof(struct sec_entry), GFP_KERNEL);
351df4b2
JK
2510 if (!sit_i->sec_entries)
2511 return -ENOMEM;
2512 }
2513
2514 /* get information related with SIT */
2515 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
2516
2517 /* setup SIT bitmap from ckeckpoint pack */
2518 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
2519 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
2520
ae27d62e
CY
2521 sit_i->sit_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
2522 if (!sit_i->sit_bitmap)
351df4b2 2523 return -ENOMEM;
351df4b2 2524
ae27d62e
CY
2525#ifdef CONFIG_F2FS_CHECK_FS
2526 sit_i->sit_bitmap_mir = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
2527 if (!sit_i->sit_bitmap_mir)
2528 return -ENOMEM;
2529#endif
2530
351df4b2
JK
2531 /* init SIT information */
2532 sit_i->s_ops = &default_salloc_ops;
2533
2534 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
2535 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
c79b7ff1 2536 sit_i->written_valid_blocks = 0;
351df4b2
JK
2537 sit_i->bitmap_size = bitmap_size;
2538 sit_i->dirty_sentries = 0;
2539 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
2540 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
2541 sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
2542 mutex_init(&sit_i->sentry_lock);
2543 return 0;
2544}
2545
2546static int build_free_segmap(struct f2fs_sb_info *sbi)
2547{
351df4b2
JK
2548 struct free_segmap_info *free_i;
2549 unsigned int bitmap_size, sec_bitmap_size;
2550
2551 /* allocate memory for free segmap information */
2552 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
2553 if (!free_i)
2554 return -ENOMEM;
2555
2556 SM_I(sbi)->free_info = free_i;
2557
7cd8558b 2558 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
39307a8e 2559 free_i->free_segmap = f2fs_kvmalloc(bitmap_size, GFP_KERNEL);
351df4b2
JK
2560 if (!free_i->free_segmap)
2561 return -ENOMEM;
2562
7cd8558b 2563 sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
39307a8e 2564 free_i->free_secmap = f2fs_kvmalloc(sec_bitmap_size, GFP_KERNEL);
351df4b2
JK
2565 if (!free_i->free_secmap)
2566 return -ENOMEM;
2567
2568 /* set all segments as dirty temporarily */
2569 memset(free_i->free_segmap, 0xff, bitmap_size);
2570 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
2571
2572 /* init free segmap information */
7cd8558b 2573 free_i->start_segno = GET_SEGNO_FROM_SEG0(sbi, MAIN_BLKADDR(sbi));
351df4b2
JK
2574 free_i->free_segments = 0;
2575 free_i->free_sections = 0;
1a118ccf 2576 spin_lock_init(&free_i->segmap_lock);
351df4b2
JK
2577 return 0;
2578}
2579
2580static int build_curseg(struct f2fs_sb_info *sbi)
2581{
1042d60f 2582 struct curseg_info *array;
351df4b2
JK
2583 int i;
2584
b434babf 2585 array = kcalloc(NR_CURSEG_TYPE, sizeof(*array), GFP_KERNEL);
351df4b2
JK
2586 if (!array)
2587 return -ENOMEM;
2588
2589 SM_I(sbi)->curseg_array = array;
2590
2591 for (i = 0; i < NR_CURSEG_TYPE; i++) {
2592 mutex_init(&array[i].curseg_mutex);
09cbfeaf 2593 array[i].sum_blk = kzalloc(PAGE_SIZE, GFP_KERNEL);
351df4b2
JK
2594 if (!array[i].sum_blk)
2595 return -ENOMEM;
b7ad7512
CY
2596 init_rwsem(&array[i].journal_rwsem);
2597 array[i].journal = kzalloc(sizeof(struct f2fs_journal),
2598 GFP_KERNEL);
2599 if (!array[i].journal)
2600 return -ENOMEM;
351df4b2
JK
2601 array[i].segno = NULL_SEGNO;
2602 array[i].next_blkoff = 0;
2603 }
2604 return restore_curseg_summaries(sbi);
2605}
2606
2607static void build_sit_entries(struct f2fs_sb_info *sbi)
2608{
2609 struct sit_info *sit_i = SIT_I(sbi);
2610 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
b7ad7512 2611 struct f2fs_journal *journal = curseg->journal;
9c094040
YH
2612 struct seg_entry *se;
2613 struct f2fs_sit_entry sit;
74de593a
CY
2614 int sit_blk_cnt = SIT_BLK_CNT(sbi);
2615 unsigned int i, start, end;
2616 unsigned int readed, start_blk = 0;
351df4b2 2617
74de593a 2618 do {
664ba972
JK
2619 readed = ra_meta_pages(sbi, start_blk, BIO_MAX_PAGES,
2620 META_SIT, true);
74de593a
CY
2621
2622 start = start_blk * sit_i->sents_per_block;
2623 end = (start_blk + readed) * sit_i->sents_per_block;
2624
7cd8558b 2625 for (; start < end && start < MAIN_SEGS(sbi); start++) {
74de593a 2626 struct f2fs_sit_block *sit_blk;
74de593a
CY
2627 struct page *page;
2628
9c094040 2629 se = &sit_i->sentries[start];
74de593a
CY
2630 page = get_current_sit_page(sbi, start);
2631 sit_blk = (struct f2fs_sit_block *)page_address(page);
2632 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
2633 f2fs_put_page(page, 1);
d600af23 2634
74de593a
CY
2635 check_block_count(sbi, start, &sit);
2636 seg_info_from_raw_sit(se, &sit);
a66cdd98
JK
2637
2638 /* build discard map only one time */
3e025740
JK
2639 if (f2fs_discard_en(sbi)) {
2640 memcpy(se->discard_map, se->cur_valid_map,
2641 SIT_VBLOCK_MAP_SIZE);
2642 sbi->discard_blks += sbi->blocks_per_seg -
2643 se->valid_blocks;
2644 }
a66cdd98 2645
d600af23
CY
2646 if (sbi->segs_per_sec > 1)
2647 get_sec_entry(sbi, start)->valid_blocks +=
2648 se->valid_blocks;
351df4b2 2649 }
74de593a
CY
2650 start_blk += readed;
2651 } while (start_blk < sit_blk_cnt);
d600af23
CY
2652
2653 down_read(&curseg->journal_rwsem);
2654 for (i = 0; i < sits_in_cursum(journal); i++) {
d600af23
CY
2655 unsigned int old_valid_blocks;
2656
2657 start = le32_to_cpu(segno_in_journal(journal, i));
2658 se = &sit_i->sentries[start];
2659 sit = sit_in_journal(journal, i);
2660
2661 old_valid_blocks = se->valid_blocks;
2662
2663 check_block_count(sbi, start, &sit);
2664 seg_info_from_raw_sit(se, &sit);
2665
2666 if (f2fs_discard_en(sbi)) {
2667 memcpy(se->discard_map, se->cur_valid_map,
2668 SIT_VBLOCK_MAP_SIZE);
2669 sbi->discard_blks += old_valid_blocks -
2670 se->valid_blocks;
2671 }
2672
2673 if (sbi->segs_per_sec > 1)
2674 get_sec_entry(sbi, start)->valid_blocks +=
2675 se->valid_blocks - old_valid_blocks;
2676 }
2677 up_read(&curseg->journal_rwsem);
351df4b2
JK
2678}
2679
2680static void init_free_segmap(struct f2fs_sb_info *sbi)
2681{
2682 unsigned int start;
2683 int type;
2684
7cd8558b 2685 for (start = 0; start < MAIN_SEGS(sbi); start++) {
351df4b2
JK
2686 struct seg_entry *sentry = get_seg_entry(sbi, start);
2687 if (!sentry->valid_blocks)
2688 __set_free(sbi, start);
c79b7ff1
JK
2689 else
2690 SIT_I(sbi)->written_valid_blocks +=
2691 sentry->valid_blocks;
351df4b2
JK
2692 }
2693
2694 /* set use the current segments */
2695 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
2696 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
2697 __set_test_and_inuse(sbi, curseg_t->segno);
2698 }
2699}
2700
2701static void init_dirty_segmap(struct f2fs_sb_info *sbi)
2702{
2703 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2704 struct free_segmap_info *free_i = FREE_I(sbi);
7cd8558b 2705 unsigned int segno = 0, offset = 0;
351df4b2
JK
2706 unsigned short valid_blocks;
2707
8736fbf0 2708 while (1) {
351df4b2 2709 /* find dirty segment based on free segmap */
7cd8558b
JK
2710 segno = find_next_inuse(free_i, MAIN_SEGS(sbi), offset);
2711 if (segno >= MAIN_SEGS(sbi))
351df4b2
JK
2712 break;
2713 offset = segno + 1;
2714 valid_blocks = get_valid_blocks(sbi, segno, 0);
ec325b52 2715 if (valid_blocks == sbi->blocks_per_seg || !valid_blocks)
351df4b2 2716 continue;
ec325b52
JK
2717 if (valid_blocks > sbi->blocks_per_seg) {
2718 f2fs_bug_on(sbi, 1);
2719 continue;
2720 }
351df4b2
JK
2721 mutex_lock(&dirty_i->seglist_lock);
2722 __locate_dirty_segment(sbi, segno, DIRTY);
2723 mutex_unlock(&dirty_i->seglist_lock);
2724 }
2725}
2726
5ec4e49f 2727static int init_victim_secmap(struct f2fs_sb_info *sbi)
351df4b2
JK
2728{
2729 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
7cd8558b 2730 unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
351df4b2 2731
39307a8e 2732 dirty_i->victim_secmap = f2fs_kvzalloc(bitmap_size, GFP_KERNEL);
5ec4e49f 2733 if (!dirty_i->victim_secmap)
351df4b2
JK
2734 return -ENOMEM;
2735 return 0;
2736}
2737
2738static int build_dirty_segmap(struct f2fs_sb_info *sbi)
2739{
2740 struct dirty_seglist_info *dirty_i;
2741 unsigned int bitmap_size, i;
2742
2743 /* allocate memory for dirty segments list information */
2744 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
2745 if (!dirty_i)
2746 return -ENOMEM;
2747
2748 SM_I(sbi)->dirty_info = dirty_i;
2749 mutex_init(&dirty_i->seglist_lock);
2750
7cd8558b 2751 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
351df4b2
JK
2752
2753 for (i = 0; i < NR_DIRTY_TYPE; i++) {
39307a8e 2754 dirty_i->dirty_segmap[i] = f2fs_kvzalloc(bitmap_size, GFP_KERNEL);
351df4b2
JK
2755 if (!dirty_i->dirty_segmap[i])
2756 return -ENOMEM;
2757 }
2758
2759 init_dirty_segmap(sbi);
5ec4e49f 2760 return init_victim_secmap(sbi);
351df4b2
JK
2761}
2762
0a8165d7 2763/*
351df4b2
JK
2764 * Update min, max modified time for cost-benefit GC algorithm
2765 */
2766static void init_min_max_mtime(struct f2fs_sb_info *sbi)
2767{
2768 struct sit_info *sit_i = SIT_I(sbi);
2769 unsigned int segno;
2770
2771 mutex_lock(&sit_i->sentry_lock);
2772
2773 sit_i->min_mtime = LLONG_MAX;
2774
7cd8558b 2775 for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) {
351df4b2
JK
2776 unsigned int i;
2777 unsigned long long mtime = 0;
2778
2779 for (i = 0; i < sbi->segs_per_sec; i++)
2780 mtime += get_seg_entry(sbi, segno + i)->mtime;
2781
2782 mtime = div_u64(mtime, sbi->segs_per_sec);
2783
2784 if (sit_i->min_mtime > mtime)
2785 sit_i->min_mtime = mtime;
2786 }
2787 sit_i->max_mtime = get_mtime(sbi);
2788 mutex_unlock(&sit_i->sentry_lock);
2789}
2790
2791int build_segment_manager(struct f2fs_sb_info *sbi)
2792{
2793 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
2794 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1042d60f 2795 struct f2fs_sm_info *sm_info;
351df4b2
JK
2796 int err;
2797
2798 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
2799 if (!sm_info)
2800 return -ENOMEM;
2801
2802 /* init sm info */
2803 sbi->sm_info = sm_info;
351df4b2
JK
2804 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
2805 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
2806 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
2807 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
2808 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
2809 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
2810 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
58c41035
JK
2811 sm_info->rec_prefree_segments = sm_info->main_segments *
2812 DEF_RECLAIM_PREFREE_SEGMENTS / 100;
44a83499
JK
2813 if (sm_info->rec_prefree_segments > DEF_MAX_RECLAIM_PREFREE_SEGMENTS)
2814 sm_info->rec_prefree_segments = DEF_MAX_RECLAIM_PREFREE_SEGMENTS;
2815
52763a4b
JK
2816 if (!test_opt(sbi, LFS))
2817 sm_info->ipu_policy = 1 << F2FS_IPU_FSYNC;
216fbd64 2818 sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
c1ce1b02 2819 sm_info->min_fsync_blocks = DEF_MIN_FSYNC_BLOCKS;
351df4b2 2820
bba681cb
JK
2821 sm_info->trim_sections = DEF_BATCHED_TRIM_SECTIONS;
2822
184a5cd2
CY
2823 INIT_LIST_HEAD(&sm_info->sit_entry_set);
2824
b270ad6f 2825 if (test_opt(sbi, FLUSH_MERGE) && !f2fs_readonly(sbi->sb)) {
2163d198
GZ
2826 err = create_flush_cmd_control(sbi);
2827 if (err)
a688b9d9 2828 return err;
6b4afdd7
JK
2829 }
2830
0b54fb84
JK
2831 err = create_discard_cmd_control(sbi);
2832 if (err)
2833 return err;
2834
351df4b2
JK
2835 err = build_sit_info(sbi);
2836 if (err)
2837 return err;
2838 err = build_free_segmap(sbi);
2839 if (err)
2840 return err;
2841 err = build_curseg(sbi);
2842 if (err)
2843 return err;
2844
2845 /* reinit free segmap based on SIT */
2846 build_sit_entries(sbi);
2847
2848 init_free_segmap(sbi);
2849 err = build_dirty_segmap(sbi);
2850 if (err)
2851 return err;
2852
2853 init_min_max_mtime(sbi);
2854 return 0;
2855}
2856
2857static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
2858 enum dirty_type dirty_type)
2859{
2860 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2861
2862 mutex_lock(&dirty_i->seglist_lock);
39307a8e 2863 kvfree(dirty_i->dirty_segmap[dirty_type]);
351df4b2
JK
2864 dirty_i->nr_dirty[dirty_type] = 0;
2865 mutex_unlock(&dirty_i->seglist_lock);
2866}
2867
5ec4e49f 2868static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
351df4b2
JK
2869{
2870 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
39307a8e 2871 kvfree(dirty_i->victim_secmap);
351df4b2
JK
2872}
2873
2874static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
2875{
2876 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2877 int i;
2878
2879 if (!dirty_i)
2880 return;
2881
2882 /* discard pre-free/dirty segments list */
2883 for (i = 0; i < NR_DIRTY_TYPE; i++)
2884 discard_dirty_segmap(sbi, i);
2885
5ec4e49f 2886 destroy_victim_secmap(sbi);
351df4b2
JK
2887 SM_I(sbi)->dirty_info = NULL;
2888 kfree(dirty_i);
2889}
2890
2891static void destroy_curseg(struct f2fs_sb_info *sbi)
2892{
2893 struct curseg_info *array = SM_I(sbi)->curseg_array;
2894 int i;
2895
2896 if (!array)
2897 return;
2898 SM_I(sbi)->curseg_array = NULL;
b7ad7512 2899 for (i = 0; i < NR_CURSEG_TYPE; i++) {
351df4b2 2900 kfree(array[i].sum_blk);
b7ad7512
CY
2901 kfree(array[i].journal);
2902 }
351df4b2
JK
2903 kfree(array);
2904}
2905
2906static void destroy_free_segmap(struct f2fs_sb_info *sbi)
2907{
2908 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
2909 if (!free_i)
2910 return;
2911 SM_I(sbi)->free_info = NULL;
39307a8e
JK
2912 kvfree(free_i->free_segmap);
2913 kvfree(free_i->free_secmap);
351df4b2
JK
2914 kfree(free_i);
2915}
2916
2917static void destroy_sit_info(struct f2fs_sb_info *sbi)
2918{
2919 struct sit_info *sit_i = SIT_I(sbi);
2920 unsigned int start;
2921
2922 if (!sit_i)
2923 return;
2924
2925 if (sit_i->sentries) {
7cd8558b 2926 for (start = 0; start < MAIN_SEGS(sbi); start++) {
351df4b2 2927 kfree(sit_i->sentries[start].cur_valid_map);
355e7891
CY
2928#ifdef CONFIG_F2FS_CHECK_FS
2929 kfree(sit_i->sentries[start].cur_valid_map_mir);
2930#endif
351df4b2 2931 kfree(sit_i->sentries[start].ckpt_valid_map);
a66cdd98 2932 kfree(sit_i->sentries[start].discard_map);
351df4b2
JK
2933 }
2934 }
60a3b782
JK
2935 kfree(sit_i->tmp_map);
2936
39307a8e
JK
2937 kvfree(sit_i->sentries);
2938 kvfree(sit_i->sec_entries);
2939 kvfree(sit_i->dirty_sentries_bitmap);
351df4b2
JK
2940
2941 SM_I(sbi)->sit_info = NULL;
2942 kfree(sit_i->sit_bitmap);
ae27d62e
CY
2943#ifdef CONFIG_F2FS_CHECK_FS
2944 kfree(sit_i->sit_bitmap_mir);
2945#endif
351df4b2
JK
2946 kfree(sit_i);
2947}
2948
2949void destroy_segment_manager(struct f2fs_sb_info *sbi)
2950{
2951 struct f2fs_sm_info *sm_info = SM_I(sbi);
a688b9d9 2952
3b03f724
CY
2953 if (!sm_info)
2954 return;
5eba8c5d 2955 destroy_flush_cmd_control(sbi, true);
0b54fb84 2956 destroy_discard_cmd_control(sbi, true);
351df4b2
JK
2957 destroy_dirty_segmap(sbi);
2958 destroy_curseg(sbi);
2959 destroy_free_segmap(sbi);
2960 destroy_sit_info(sbi);
2961 sbi->sm_info = NULL;
2962 kfree(sm_info);
2963}
7fd9e544
JK
2964
2965int __init create_segment_manager_caches(void)
2966{
2967 discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
e8512d2e 2968 sizeof(struct discard_entry));
7fd9e544 2969 if (!discard_entry_slab)
184a5cd2
CY
2970 goto fail;
2971
b01a9201
JK
2972 discard_cmd_slab = f2fs_kmem_cache_create("discard_cmd",
2973 sizeof(struct discard_cmd));
2974 if (!discard_cmd_slab)
6ab2a308 2975 goto destroy_discard_entry;
275b66b0 2976
184a5cd2 2977 sit_entry_set_slab = f2fs_kmem_cache_create("sit_entry_set",
c9ee0085 2978 sizeof(struct sit_entry_set));
184a5cd2 2979 if (!sit_entry_set_slab)
b01a9201 2980 goto destroy_discard_cmd;
88b88a66
JK
2981
2982 inmem_entry_slab = f2fs_kmem_cache_create("inmem_page_entry",
2983 sizeof(struct inmem_pages));
2984 if (!inmem_entry_slab)
2985 goto destroy_sit_entry_set;
7fd9e544 2986 return 0;
184a5cd2 2987
88b88a66
JK
2988destroy_sit_entry_set:
2989 kmem_cache_destroy(sit_entry_set_slab);
b01a9201
JK
2990destroy_discard_cmd:
2991 kmem_cache_destroy(discard_cmd_slab);
6ab2a308 2992destroy_discard_entry:
184a5cd2
CY
2993 kmem_cache_destroy(discard_entry_slab);
2994fail:
2995 return -ENOMEM;
7fd9e544
JK
2996}
2997
2998void destroy_segment_manager_caches(void)
2999{
184a5cd2 3000 kmem_cache_destroy(sit_entry_set_slab);
b01a9201 3001 kmem_cache_destroy(discard_cmd_slab);
7fd9e544 3002 kmem_cache_destroy(discard_entry_slab);
88b88a66 3003 kmem_cache_destroy(inmem_entry_slab);
7fd9e544 3004}