Merge tag 'for-linus-5.7-rc1b-tag' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.git] / fs / f2fs / checkpoint.c
CommitLineData
7c1a000d 1// SPDX-License-Identifier: GPL-2.0
0a8165d7 2/*
127e670a
JK
3 * fs/f2fs/checkpoint.c
4 *
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
127e670a
JK
7 */
8#include <linux/fs.h>
9#include <linux/bio.h>
10#include <linux/mpage.h>
11#include <linux/writeback.h>
12#include <linux/blkdev.h>
13#include <linux/f2fs_fs.h>
14#include <linux/pagevec.h>
15#include <linux/swap.h>
16
17#include "f2fs.h"
18#include "node.h"
19#include "segment.h"
9e4ded3f 20#include "trace.h"
2af4bd6c 21#include <trace/events/f2fs.h>
127e670a 22
6451e041 23static struct kmem_cache *ino_entry_slab;
4d57b86d 24struct kmem_cache *f2fs_inode_entry_slab;
127e670a 25
38f91ca8
JK
26void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io)
27{
d494500a 28 f2fs_build_fault_attr(sbi, 0, 0);
aaec2b1d 29 set_ckpt_flags(sbi, CP_ERROR_FLAG);
38f91ca8 30 if (!end_io)
b9109b0e 31 f2fs_flush_merged_writes(sbi);
38f91ca8
JK
32}
33
0a8165d7 34/*
127e670a
JK
35 * We guarantee no failure on the returned page.
36 */
4d57b86d 37struct page *f2fs_grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
127e670a 38{
9df27d98 39 struct address_space *mapping = META_MAPPING(sbi);
127e670a
JK
40 struct page *page = NULL;
41repeat:
300e129c 42 page = f2fs_grab_cache_page(mapping, index, false);
127e670a
JK
43 if (!page) {
44 cond_resched();
45 goto repeat;
46 }
bae0ee7a 47 f2fs_wait_on_page_writeback(page, META, true, true);
237c0790
JK
48 if (!PageUptodate(page))
49 SetPageUptodate(page);
127e670a
JK
50 return page;
51}
52
2b947003
CY
53static struct page *__get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index,
54 bool is_meta)
127e670a 55{
9df27d98 56 struct address_space *mapping = META_MAPPING(sbi);
127e670a 57 struct page *page;
cf04e8eb 58 struct f2fs_io_info fio = {
05ca3632 59 .sbi = sbi,
cf04e8eb 60 .type = META,
04d328de 61 .op = REQ_OP_READ,
70fd7614 62 .op_flags = REQ_META | REQ_PRIO,
7a9d7548
CY
63 .old_blkaddr = index,
64 .new_blkaddr = index,
4375a336 65 .encrypted_page = NULL,
6dc3a126 66 .is_por = !is_meta,
cf04e8eb 67 };
7735730d 68 int err;
2b947003
CY
69
70 if (unlikely(!is_meta))
04d328de 71 fio.op_flags &= ~REQ_META;
127e670a 72repeat:
300e129c 73 page = f2fs_grab_cache_page(mapping, index, false);
127e670a
JK
74 if (!page) {
75 cond_resched();
76 goto repeat;
77 }
393ff91f
JK
78 if (PageUptodate(page))
79 goto out;
80
05ca3632
JK
81 fio.page = page;
82
7735730d
CY
83 err = f2fs_submit_page_bio(&fio);
84 if (err) {
85 f2fs_put_page(page, 1);
86 return ERR_PTR(err);
86531d6b 87 }
127e670a 88
393ff91f 89 lock_page(page);
6bacf52f 90 if (unlikely(page->mapping != mapping)) {
afcb7ca0
JK
91 f2fs_put_page(page, 1);
92 goto repeat;
93 }
f3f338ca 94
81114baa 95 if (unlikely(!PageUptodate(page))) {
7735730d
CY
96 f2fs_put_page(page, 1);
97 return ERR_PTR(-EIO);
81114baa 98 }
393ff91f 99out:
127e670a
JK
100 return page;
101}
102
4d57b86d 103struct page *f2fs_get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
2b947003
CY
104{
105 return __get_meta_page(sbi, index, true);
106}
107
7735730d
CY
108struct page *f2fs_get_meta_page_nofail(struct f2fs_sb_info *sbi, pgoff_t index)
109{
110 struct page *page;
111 int count = 0;
112
113retry:
114 page = __get_meta_page(sbi, index, true);
115 if (IS_ERR(page)) {
116 if (PTR_ERR(page) == -EIO &&
117 ++count <= DEFAULT_RETRY_IO_COUNT)
118 goto retry;
7735730d 119 f2fs_stop_checkpoint(sbi, false);
7735730d 120 }
7735730d
CY
121 return page;
122}
123
2b947003 124/* for POR only */
4d57b86d 125struct page *f2fs_get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index)
2b947003
CY
126{
127 return __get_meta_page(sbi, index, false);
128}
129
93770ab7
CY
130static bool __is_bitmap_valid(struct f2fs_sb_info *sbi, block_t blkaddr,
131 int type)
132{
133 struct seg_entry *se;
134 unsigned int segno, offset;
135 bool exist;
136
137 if (type != DATA_GENERIC_ENHANCE && type != DATA_GENERIC_ENHANCE_READ)
138 return true;
139
140 segno = GET_SEGNO(sbi, blkaddr);
141 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
142 se = get_seg_entry(sbi, segno);
143
144 exist = f2fs_test_bit(offset, se->cur_valid_map);
145 if (!exist && type == DATA_GENERIC_ENHANCE) {
dcbb4c10
JP
146 f2fs_err(sbi, "Inconsistent error blkaddr:%u, sit bitmap:%d",
147 blkaddr, exist);
93770ab7
CY
148 set_sbi_flag(sbi, SBI_NEED_FSCK);
149 WARN_ON(1);
150 }
151 return exist;
152}
153
e1da7872 154bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
4d57b86d 155 block_t blkaddr, int type)
662befda
CY
156{
157 switch (type) {
158 case META_NAT:
66b00c18 159 break;
662befda 160 case META_SIT:
66b00c18
CY
161 if (unlikely(blkaddr >= SIT_BLK_CNT(sbi)))
162 return false;
163 break;
81c1a0f1 164 case META_SSA:
66b00c18
CY
165 if (unlikely(blkaddr >= MAIN_BLKADDR(sbi) ||
166 blkaddr < SM_I(sbi)->ssa_blkaddr))
167 return false;
168 break;
662befda 169 case META_CP:
66b00c18
CY
170 if (unlikely(blkaddr >= SIT_I(sbi)->sit_base_addr ||
171 blkaddr < __start_cp_addr(sbi)))
172 return false;
173 break;
4c521f49 174 case META_POR:
93770ab7
CY
175 if (unlikely(blkaddr >= MAX_BLKADDR(sbi) ||
176 blkaddr < MAIN_BLKADDR(sbi)))
177 return false;
178 break;
e1da7872 179 case DATA_GENERIC:
93770ab7
CY
180 case DATA_GENERIC_ENHANCE:
181 case DATA_GENERIC_ENHANCE_READ:
66b00c18 182 if (unlikely(blkaddr >= MAX_BLKADDR(sbi) ||
93770ab7 183 blkaddr < MAIN_BLKADDR(sbi))) {
dcbb4c10
JP
184 f2fs_warn(sbi, "access invalid blkaddr:%u",
185 blkaddr);
93770ab7
CY
186 set_sbi_flag(sbi, SBI_NEED_FSCK);
187 WARN_ON(1);
66b00c18 188 return false;
93770ab7
CY
189 } else {
190 return __is_bitmap_valid(sbi, blkaddr, type);
c9b60788 191 }
66b00c18 192 break;
e1da7872
CY
193 case META_GENERIC:
194 if (unlikely(blkaddr < SEG0_BLKADDR(sbi) ||
195 blkaddr >= MAIN_BLKADDR(sbi)))
196 return false;
197 break;
662befda
CY
198 default:
199 BUG();
200 }
66b00c18
CY
201
202 return true;
662befda
CY
203}
204
205/*
7a88ddb5 206 * Readahead CP/NAT/SIT/SSA/POR pages
662befda 207 */
4d57b86d 208int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
26879fb1 209 int type, bool sync)
662befda 210{
662befda 211 struct page *page;
4c521f49 212 block_t blkno = start;
662befda 213 struct f2fs_io_info fio = {
05ca3632 214 .sbi = sbi,
662befda 215 .type = META,
04d328de 216 .op = REQ_OP_READ,
70fd7614 217 .op_flags = sync ? (REQ_META | REQ_PRIO) : REQ_RAHEAD,
4375a336 218 .encrypted_page = NULL,
fb830fc5 219 .in_list = false,
6dc3a126 220 .is_por = (type == META_POR),
662befda 221 };
e9f5b8b8 222 struct blk_plug plug;
662befda 223
2b947003 224 if (unlikely(type == META_POR))
04d328de 225 fio.op_flags &= ~REQ_META;
2b947003 226
e9f5b8b8 227 blk_start_plug(&plug);
662befda 228 for (; nrpages-- > 0; blkno++) {
662befda 229
e1da7872 230 if (!f2fs_is_valid_blkaddr(sbi, blkno, type))
66b00c18
CY
231 goto out;
232
662befda
CY
233 switch (type) {
234 case META_NAT:
66b00c18
CY
235 if (unlikely(blkno >=
236 NAT_BLOCK_OFFSET(NM_I(sbi)->max_nid)))
662befda 237 blkno = 0;
66b00c18 238 /* get nat block addr */
7a9d7548 239 fio.new_blkaddr = current_nat_addr(sbi,
662befda
CY
240 blkno * NAT_ENTRY_PER_BLOCK);
241 break;
242 case META_SIT:
243 /* get sit block addr */
7a9d7548 244 fio.new_blkaddr = current_sit_addr(sbi,
662befda 245 blkno * SIT_ENTRY_PER_BLOCK);
662befda 246 break;
81c1a0f1 247 case META_SSA:
662befda 248 case META_CP:
4c521f49 249 case META_POR:
7a9d7548 250 fio.new_blkaddr = blkno;
662befda
CY
251 break;
252 default:
253 BUG();
254 }
255
300e129c
JK
256 page = f2fs_grab_cache_page(META_MAPPING(sbi),
257 fio.new_blkaddr, false);
662befda
CY
258 if (!page)
259 continue;
260 if (PageUptodate(page)) {
662befda
CY
261 f2fs_put_page(page, 1);
262 continue;
263 }
264
05ca3632 265 fio.page = page;
1919ffc0 266 f2fs_submit_page_bio(&fio);
662befda
CY
267 f2fs_put_page(page, 0);
268 }
269out:
e9f5b8b8 270 blk_finish_plug(&plug);
662befda
CY
271 return blkno - start;
272}
273
4d57b86d 274void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index)
635aee1f
CY
275{
276 struct page *page;
277 bool readahead = false;
278
279 page = find_get_page(META_MAPPING(sbi), index);
4da7bf5a 280 if (!page || !PageUptodate(page))
635aee1f
CY
281 readahead = true;
282 f2fs_put_page(page, 0);
283
284 if (readahead)
4d57b86d 285 f2fs_ra_meta_pages(sbi, index, BIO_MAX_PAGES, META_POR, true);
635aee1f
CY
286}
287
b0af6d49
CY
288static int __f2fs_write_meta_page(struct page *page,
289 struct writeback_control *wbc,
290 enum iostat_type io_type)
127e670a 291{
4081363f 292 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
127e670a 293
ecda0de3
CY
294 trace_f2fs_writepage(page, META);
295
af697c0f
JK
296 if (unlikely(f2fs_cp_error(sbi)))
297 goto redirty_out;
caf0047e 298 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
cfb271d4 299 goto redirty_out;
857dc4e0 300 if (wbc->for_reclaim && page->index < GET_SUM_BLOCK(sbi, 0))
cfb271d4 301 goto redirty_out;
127e670a 302
4d57b86d 303 f2fs_do_write_meta_page(sbi, page, io_type);
577e3495 304 dec_page_count(sbi, F2FS_DIRTY_META);
0c3a5797
CY
305
306 if (wbc->for_reclaim)
bab475c5 307 f2fs_submit_merged_write_cond(sbi, NULL, page, 0, META);
0c3a5797 308
577e3495 309 unlock_page(page);
857dc4e0 310
0c3a5797 311 if (unlikely(f2fs_cp_error(sbi)))
b9109b0e 312 f2fs_submit_merged_write(sbi, META);
0c3a5797 313
577e3495 314 return 0;
cfb271d4
CY
315
316redirty_out:
76f60268 317 redirty_page_for_writepage(wbc, page);
cfb271d4 318 return AOP_WRITEPAGE_ACTIVATE;
127e670a
JK
319}
320
b0af6d49
CY
321static int f2fs_write_meta_page(struct page *page,
322 struct writeback_control *wbc)
323{
324 return __f2fs_write_meta_page(page, wbc, FS_META_IO);
325}
326
127e670a
JK
327static int f2fs_write_meta_pages(struct address_space *mapping,
328 struct writeback_control *wbc)
329{
4081363f 330 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
50c8cdb3 331 long diff, written;
127e670a 332
0771fcc7
CY
333 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
334 goto skip_write;
335
5459aa97 336 /* collect a number of dirty meta pages and write together */
812a9597
JK
337 if (wbc->sync_mode != WB_SYNC_ALL &&
338 get_pages(sbi, F2FS_DIRTY_META) <
339 nr_pages_to_skip(sbi, META))
d3baf95d 340 goto skip_write;
127e670a 341
a29d0e0b
YH
342 /* if locked failed, cp will flush dirty pages instead */
343 if (!mutex_trylock(&sbi->cp_mutex))
344 goto skip_write;
d31c7c3f 345
a29d0e0b 346 trace_f2fs_writepages(mapping->host, wbc, META);
50c8cdb3 347 diff = nr_pages_to_write(sbi, META, wbc);
4d57b86d 348 written = f2fs_sync_meta_pages(sbi, META, wbc->nr_to_write, FS_META_IO);
127e670a 349 mutex_unlock(&sbi->cp_mutex);
50c8cdb3 350 wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff);
127e670a 351 return 0;
d3baf95d
JK
352
353skip_write:
354 wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_META);
d31c7c3f 355 trace_f2fs_writepages(mapping->host, wbc, META);
d3baf95d 356 return 0;
127e670a
JK
357}
358
4d57b86d 359long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
b0af6d49 360 long nr_to_write, enum iostat_type io_type)
127e670a 361{
9df27d98 362 struct address_space *mapping = META_MAPPING(sbi);
028a63a6 363 pgoff_t index = 0, prev = ULONG_MAX;
127e670a
JK
364 struct pagevec pvec;
365 long nwritten = 0;
028a63a6 366 int nr_pages;
127e670a
JK
367 struct writeback_control wbc = {
368 .for_reclaim = 0,
369 };
e9f5b8b8 370 struct blk_plug plug;
127e670a 371
86679820 372 pagevec_init(&pvec);
127e670a 373
e9f5b8b8
CY
374 blk_start_plug(&plug);
375
028a63a6 376 while ((nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
67fd707f 377 PAGECACHE_TAG_DIRTY))) {
028a63a6 378 int i;
127e670a
JK
379
380 for (i = 0; i < nr_pages; i++) {
381 struct page *page = pvec.pages[i];
203681f6 382
80dd9c0e 383 if (prev == ULONG_MAX)
6066d8cd
JK
384 prev = page->index - 1;
385 if (nr_to_write != LONG_MAX && page->index != prev + 1) {
386 pagevec_release(&pvec);
387 goto stop;
388 }
389
127e670a 390 lock_page(page);
203681f6
JK
391
392 if (unlikely(page->mapping != mapping)) {
393continue_unlock:
394 unlock_page(page);
395 continue;
396 }
397 if (!PageDirty(page)) {
398 /* someone wrote it for us */
399 goto continue_unlock;
400 }
401
bae0ee7a 402 f2fs_wait_on_page_writeback(page, META, true, true);
fa3d2bdf 403
203681f6
JK
404 if (!clear_page_dirty_for_io(page))
405 goto continue_unlock;
406
b0af6d49 407 if (__f2fs_write_meta_page(page, &wbc, io_type)) {
577e3495
JK
408 unlock_page(page);
409 break;
410 }
cfb271d4 411 nwritten++;
6066d8cd 412 prev = page->index;
cfb271d4 413 if (unlikely(nwritten >= nr_to_write))
127e670a
JK
414 break;
415 }
416 pagevec_release(&pvec);
417 cond_resched();
418 }
6066d8cd 419stop:
127e670a 420 if (nwritten)
b9109b0e 421 f2fs_submit_merged_write(sbi, type);
127e670a 422
e9f5b8b8
CY
423 blk_finish_plug(&plug);
424
127e670a
JK
425 return nwritten;
426}
427
428static int f2fs_set_meta_page_dirty(struct page *page)
429{
26c6b887
JK
430 trace_f2fs_set_page_dirty(page, META);
431
237c0790
JK
432 if (!PageUptodate(page))
433 SetPageUptodate(page);
127e670a 434 if (!PageDirty(page)) {
b87078ad 435 __set_page_dirty_nobuffers(page);
4081363f 436 inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_META);
240a5915 437 f2fs_set_page_private(page, 0);
9e4ded3f 438 f2fs_trace_pid(page);
127e670a
JK
439 return 1;
440 }
441 return 0;
442}
443
444const struct address_space_operations f2fs_meta_aops = {
445 .writepage = f2fs_write_meta_page,
446 .writepages = f2fs_write_meta_pages,
447 .set_page_dirty = f2fs_set_meta_page_dirty,
487261f3
CY
448 .invalidatepage = f2fs_invalidate_page,
449 .releasepage = f2fs_release_page,
5b7a487c
WG
450#ifdef CONFIG_MIGRATION
451 .migratepage = f2fs_migrate_page,
452#endif
127e670a
JK
453};
454
39d787be
CY
455static void __add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino,
456 unsigned int devidx, int type)
953e6cc6 457{
67298804 458 struct inode_management *im = &sbi->im[type];
80c54505
JK
459 struct ino_entry *e, *tmp;
460
461 tmp = f2fs_kmem_cache_alloc(ino_entry_slab, GFP_NOFS);
19526d74 462
80c54505 463 radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
769ec6e5 464
67298804 465 spin_lock(&im->ino_lock);
67298804 466 e = radix_tree_lookup(&im->ino_root, ino);
39efac41 467 if (!e) {
80c54505 468 e = tmp;
19526d74
CY
469 if (unlikely(radix_tree_insert(&im->ino_root, ino, e)))
470 f2fs_bug_on(sbi, 1);
471
39efac41
JK
472 memset(e, 0, sizeof(struct ino_entry));
473 e->ino = ino;
953e6cc6 474
67298804 475 list_add_tail(&e->list, &im->ino_list);
8c402946 476 if (type != ORPHAN_INO)
67298804 477 im->ino_num++;
39efac41 478 }
39d787be
CY
479
480 if (type == FLUSH_INO)
481 f2fs_set_bit(devidx, (char *)&e->dirty_device);
482
67298804 483 spin_unlock(&im->ino_lock);
769ec6e5 484 radix_tree_preload_end();
80c54505
JK
485
486 if (e != tmp)
487 kmem_cache_free(ino_entry_slab, tmp);
953e6cc6
JK
488}
489
6451e041 490static void __remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
953e6cc6 491{
67298804 492 struct inode_management *im = &sbi->im[type];
6451e041 493 struct ino_entry *e;
953e6cc6 494
67298804
CY
495 spin_lock(&im->ino_lock);
496 e = radix_tree_lookup(&im->ino_root, ino);
39efac41
JK
497 if (e) {
498 list_del(&e->list);
67298804
CY
499 radix_tree_delete(&im->ino_root, ino);
500 im->ino_num--;
501 spin_unlock(&im->ino_lock);
39efac41
JK
502 kmem_cache_free(ino_entry_slab, e);
503 return;
953e6cc6 504 }
67298804 505 spin_unlock(&im->ino_lock);
953e6cc6
JK
506}
507
4d57b86d 508void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
fff04f90
JK
509{
510 /* add new dirty ino entry into list */
39d787be 511 __add_ino_entry(sbi, ino, 0, type);
fff04f90
JK
512}
513
4d57b86d 514void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
fff04f90
JK
515{
516 /* remove dirty ino entry from list */
517 __remove_ino_entry(sbi, ino, type);
518}
519
520/* mode should be APPEND_INO or UPDATE_INO */
4d57b86d 521bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode)
fff04f90 522{
67298804 523 struct inode_management *im = &sbi->im[mode];
fff04f90 524 struct ino_entry *e;
67298804
CY
525
526 spin_lock(&im->ino_lock);
527 e = radix_tree_lookup(&im->ino_root, ino);
528 spin_unlock(&im->ino_lock);
fff04f90
JK
529 return e ? true : false;
530}
531
4d57b86d 532void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all)
fff04f90
JK
533{
534 struct ino_entry *e, *tmp;
535 int i;
536
39d787be 537 for (i = all ? ORPHAN_INO : APPEND_INO; i < MAX_INO_ENTRY; i++) {
67298804
CY
538 struct inode_management *im = &sbi->im[i];
539
540 spin_lock(&im->ino_lock);
541 list_for_each_entry_safe(e, tmp, &im->ino_list, list) {
fff04f90 542 list_del(&e->list);
67298804 543 radix_tree_delete(&im->ino_root, e->ino);
fff04f90 544 kmem_cache_free(ino_entry_slab, e);
67298804 545 im->ino_num--;
fff04f90 546 }
67298804 547 spin_unlock(&im->ino_lock);
fff04f90
JK
548 }
549}
550
4d57b86d 551void f2fs_set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
39d787be
CY
552 unsigned int devidx, int type)
553{
554 __add_ino_entry(sbi, ino, devidx, type);
555}
556
4d57b86d 557bool f2fs_is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
39d787be
CY
558 unsigned int devidx, int type)
559{
560 struct inode_management *im = &sbi->im[type];
561 struct ino_entry *e;
562 bool is_dirty = false;
563
564 spin_lock(&im->ino_lock);
565 e = radix_tree_lookup(&im->ino_root, ino);
566 if (e && f2fs_test_bit(devidx, (char *)&e->dirty_device))
567 is_dirty = true;
568 spin_unlock(&im->ino_lock);
569 return is_dirty;
570}
571
4d57b86d 572int f2fs_acquire_orphan_inode(struct f2fs_sb_info *sbi)
127e670a 573{
67298804 574 struct inode_management *im = &sbi->im[ORPHAN_INO];
127e670a
JK
575 int err = 0;
576
67298804 577 spin_lock(&im->ino_lock);
cb78942b 578
1ecc0c5c 579 if (time_to_inject(sbi, FAULT_ORPHAN)) {
cb78942b 580 spin_unlock(&im->ino_lock);
c45d6002 581 f2fs_show_injection_info(sbi, FAULT_ORPHAN);
cb78942b
JK
582 return -ENOSPC;
583 }
7fa750a1 584
67298804 585 if (unlikely(im->ino_num >= sbi->max_orphans))
127e670a 586 err = -ENOSPC;
cbd56e7d 587 else
67298804
CY
588 im->ino_num++;
589 spin_unlock(&im->ino_lock);
0d47c1ad 590
127e670a
JK
591 return err;
592}
593
4d57b86d 594void f2fs_release_orphan_inode(struct f2fs_sb_info *sbi)
cbd56e7d 595{
67298804
CY
596 struct inode_management *im = &sbi->im[ORPHAN_INO];
597
598 spin_lock(&im->ino_lock);
599 f2fs_bug_on(sbi, im->ino_num == 0);
600 im->ino_num--;
601 spin_unlock(&im->ino_lock);
cbd56e7d
JK
602}
603
4d57b86d 604void f2fs_add_orphan_inode(struct inode *inode)
127e670a 605{
39efac41 606 /* add new orphan ino entry into list */
39d787be 607 __add_ino_entry(F2FS_I_SB(inode), inode->i_ino, 0, ORPHAN_INO);
4d57b86d 608 f2fs_update_inode_page(inode);
127e670a
JK
609}
610
4d57b86d 611void f2fs_remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
127e670a 612{
953e6cc6 613 /* remove orphan entry from orphan list */
6451e041 614 __remove_ino_entry(sbi, ino, ORPHAN_INO);
127e670a
JK
615}
616
8c14bfad 617static int recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
127e670a 618{
8c14bfad 619 struct inode *inode;
5905f9af 620 struct node_info ni;
76a45e3c 621 int err;
8c14bfad 622
5905f9af 623 inode = f2fs_iget_retry(sbi->sb, ino);
8c14bfad
CY
624 if (IS_ERR(inode)) {
625 /*
626 * there should be a bug that we can't find the entry
627 * to orphan inode.
628 */
629 f2fs_bug_on(sbi, PTR_ERR(inode) == -ENOENT);
630 return PTR_ERR(inode);
631 }
632
0f9ec2a8 633 err = dquot_initialize(inode);
a515d12f
SY
634 if (err) {
635 iput(inode);
0f9ec2a8 636 goto err_out;
a515d12f 637 }
0f9ec2a8 638
127e670a
JK
639 clear_nlink(inode);
640
641 /* truncate all the data during iput */
642 iput(inode);
5905f9af 643
7735730d
CY
644 err = f2fs_get_node_info(sbi, ino, &ni);
645 if (err)
646 goto err_out;
5905f9af
JK
647
648 /* ENOMEM was fully retried in f2fs_evict_inode. */
649 if (ni.blk_addr != NULL_ADDR) {
0f9ec2a8
JK
650 err = -EIO;
651 goto err_out;
5905f9af 652 }
8c14bfad 653 return 0;
0f9ec2a8
JK
654
655err_out:
656 set_sbi_flag(sbi, SBI_NEED_FSCK);
dcbb4c10
JP
657 f2fs_warn(sbi, "%s: orphan failed (ino=%x), run fsck to fix.",
658 __func__, ino);
0f9ec2a8 659 return err;
127e670a
JK
660}
661
4d57b86d 662int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
127e670a 663{
3c642985 664 block_t start_blk, orphan_blocks, i, j;
4b2414d0
CY
665 unsigned int s_flags = sbi->sb->s_flags;
666 int err = 0;
ea676733
JK
667#ifdef CONFIG_QUOTA
668 int quota_enabled;
669#endif
127e670a 670
aaec2b1d 671 if (!is_set_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG))
8c14bfad 672 return 0;
127e670a 673
b61af314 674 if (bdev_read_only(sbi->sb->s_bdev)) {
dcbb4c10 675 f2fs_info(sbi, "write access unavailable, skipping orphan cleanup");
b61af314
CY
676 return 0;
677 }
678
1751e8a6 679 if (s_flags & SB_RDONLY) {
dcbb4c10 680 f2fs_info(sbi, "orphan cleanup on readonly fs");
1751e8a6 681 sbi->sb->s_flags &= ~SB_RDONLY;
4b2414d0
CY
682 }
683
684#ifdef CONFIG_QUOTA
685 /* Needed for iput() to work correctly and not trash data */
1751e8a6 686 sbi->sb->s_flags |= SB_ACTIVE;
ea676733 687
76cf05d7
SY
688 /*
689 * Turn on quotas which were not enabled for read-only mounts if
690 * filesystem has quota feature, so that they are updated correctly.
691 */
1751e8a6 692 quota_enabled = f2fs_enable_quota_files(sbi, s_flags & SB_RDONLY);
4b2414d0
CY
693#endif
694
55141486 695 start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi);
3c642985 696 orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi);
127e670a 697
4d57b86d 698 f2fs_ra_meta_pages(sbi, start_blk, orphan_blocks, META_CP, true);
662befda 699
3c642985 700 for (i = 0; i < orphan_blocks; i++) {
7735730d 701 struct page *page;
127e670a
JK
702 struct f2fs_orphan_block *orphan_blk;
703
7735730d
CY
704 page = f2fs_get_meta_page(sbi, start_blk + i);
705 if (IS_ERR(page)) {
706 err = PTR_ERR(page);
707 goto out;
708 }
709
127e670a
JK
710 orphan_blk = (struct f2fs_orphan_block *)page_address(page);
711 for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) {
712 nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
8c14bfad
CY
713 err = recover_orphan_inode(sbi, ino);
714 if (err) {
715 f2fs_put_page(page, 1);
4b2414d0 716 goto out;
8c14bfad 717 }
127e670a
JK
718 }
719 f2fs_put_page(page, 1);
720 }
721 /* clear Orphan Flag */
aaec2b1d 722 clear_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG);
4b2414d0 723out:
1378752b
CY
724 set_sbi_flag(sbi, SBI_IS_RECOVERED);
725
4b2414d0
CY
726#ifdef CONFIG_QUOTA
727 /* Turn quotas off */
ea676733
JK
728 if (quota_enabled)
729 f2fs_quota_off_umount(sbi->sb);
4b2414d0 730#endif
1751e8a6 731 sbi->sb->s_flags = s_flags; /* Restore SB_RDONLY status */
4b2414d0
CY
732
733 return err;
127e670a
JK
734}
735
736static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk)
737{
502c6e0b 738 struct list_head *head;
127e670a 739 struct f2fs_orphan_block *orphan_blk = NULL;
127e670a 740 unsigned int nentries = 0;
bd936f84 741 unsigned short index = 1;
8c402946 742 unsigned short orphan_blocks;
4531929e 743 struct page *page = NULL;
6451e041 744 struct ino_entry *orphan = NULL;
67298804 745 struct inode_management *im = &sbi->im[ORPHAN_INO];
127e670a 746
67298804 747 orphan_blocks = GET_ORPHAN_BLOCKS(im->ino_num);
8c402946 748
d6c67a4f
JK
749 /*
750 * we don't need to do spin_lock(&im->ino_lock) here, since all the
751 * orphan inode operations are covered under f2fs_lock_op().
752 * And, spin_lock should be avoided due to page operations below.
753 */
67298804 754 head = &im->ino_list;
127e670a
JK
755
756 /* loop for each orphan inode entry and write them in Jornal block */
502c6e0b
GZ
757 list_for_each_entry(orphan, head, list) {
758 if (!page) {
4d57b86d 759 page = f2fs_grab_meta_page(sbi, start_blk++);
502c6e0b
GZ
760 orphan_blk =
761 (struct f2fs_orphan_block *)page_address(page);
762 memset(orphan_blk, 0, sizeof(*orphan_blk));
763 }
127e670a 764
36795567 765 orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino);
127e670a 766
36795567 767 if (nentries == F2FS_ORPHANS_PER_BLOCK) {
127e670a
JK
768 /*
769 * an orphan block is full of 1020 entries,
770 * then we need to flush current orphan blocks
771 * and bring another one in memory
772 */
773 orphan_blk->blk_addr = cpu_to_le16(index);
774 orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
775 orphan_blk->entry_count = cpu_to_le32(nentries);
776 set_page_dirty(page);
777 f2fs_put_page(page, 1);
778 index++;
127e670a
JK
779 nentries = 0;
780 page = NULL;
781 }
502c6e0b 782 }
127e670a 783
502c6e0b
GZ
784 if (page) {
785 orphan_blk->blk_addr = cpu_to_le16(index);
786 orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
787 orphan_blk->entry_count = cpu_to_le32(nentries);
788 set_page_dirty(page);
789 f2fs_put_page(page, 1);
127e670a 790 }
127e670a
JK
791}
792
d7eb8f1c
CY
793static __u32 f2fs_checkpoint_chksum(struct f2fs_sb_info *sbi,
794 struct f2fs_checkpoint *ckpt)
795{
796 unsigned int chksum_ofs = le32_to_cpu(ckpt->checksum_offset);
797 __u32 chksum;
798
799 chksum = f2fs_crc32(sbi, ckpt, chksum_ofs);
800 if (chksum_ofs < CP_CHKSUM_OFFSET) {
801 chksum_ofs += sizeof(chksum);
802 chksum = f2fs_chksum(sbi, chksum, (__u8 *)ckpt + chksum_ofs,
803 F2FS_BLKSIZE - chksum_ofs);
804 }
805 return chksum;
806}
807
fc0065ad
TY
808static int get_checkpoint_version(struct f2fs_sb_info *sbi, block_t cp_addr,
809 struct f2fs_checkpoint **cp_block, struct page **cp_page,
810 unsigned long long *version)
127e670a 811{
fc0065ad 812 size_t crc_offset = 0;
d7eb8f1c 813 __u32 crc;
127e670a 814
4d57b86d 815 *cp_page = f2fs_get_meta_page(sbi, cp_addr);
7735730d
CY
816 if (IS_ERR(*cp_page))
817 return PTR_ERR(*cp_page);
818
fc0065ad 819 *cp_block = (struct f2fs_checkpoint *)page_address(*cp_page);
127e670a 820
fc0065ad 821 crc_offset = le32_to_cpu((*cp_block)->checksum_offset);
d7eb8f1c
CY
822 if (crc_offset < CP_MIN_CHKSUM_OFFSET ||
823 crc_offset > CP_CHKSUM_OFFSET) {
d3f07c04 824 f2fs_put_page(*cp_page, 1);
dcbb4c10 825 f2fs_warn(sbi, "invalid crc_offset: %zu", crc_offset);
fc0065ad
TY
826 return -EINVAL;
827 }
127e670a 828
d7eb8f1c
CY
829 crc = f2fs_checkpoint_chksum(sbi, *cp_block);
830 if (crc != cur_cp_crc(*cp_block)) {
d3f07c04 831 f2fs_put_page(*cp_page, 1);
dcbb4c10 832 f2fs_warn(sbi, "invalid crc value");
fc0065ad
TY
833 return -EINVAL;
834 }
127e670a 835
fc0065ad
TY
836 *version = cur_cp_version(*cp_block);
837 return 0;
838}
127e670a 839
fc0065ad
TY
840static struct page *validate_checkpoint(struct f2fs_sb_info *sbi,
841 block_t cp_addr, unsigned long long *version)
842{
843 struct page *cp_page_1 = NULL, *cp_page_2 = NULL;
844 struct f2fs_checkpoint *cp_block = NULL;
845 unsigned long long cur_version = 0, pre_version = 0;
846 int err;
127e670a 847
fc0065ad
TY
848 err = get_checkpoint_version(sbi, cp_addr, &cp_block,
849 &cp_page_1, version);
850 if (err)
d3f07c04 851 return NULL;
c9b60788
CY
852
853 if (le32_to_cpu(cp_block->cp_pack_total_block_count) >
854 sbi->blocks_per_seg) {
dcbb4c10
JP
855 f2fs_warn(sbi, "invalid cp_pack_total_block_count:%u",
856 le32_to_cpu(cp_block->cp_pack_total_block_count));
d3f07c04 857 goto invalid_cp;
c9b60788 858 }
fc0065ad 859 pre_version = *version;
127e670a 860
fc0065ad
TY
861 cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1;
862 err = get_checkpoint_version(sbi, cp_addr, &cp_block,
863 &cp_page_2, version);
864 if (err)
d3f07c04 865 goto invalid_cp;
fc0065ad 866 cur_version = *version;
127e670a
JK
867
868 if (cur_version == pre_version) {
869 *version = cur_version;
870 f2fs_put_page(cp_page_2, 1);
871 return cp_page_1;
872 }
127e670a 873 f2fs_put_page(cp_page_2, 1);
d3f07c04 874invalid_cp:
127e670a
JK
875 f2fs_put_page(cp_page_1, 1);
876 return NULL;
877}
878
4d57b86d 879int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi)
127e670a
JK
880{
881 struct f2fs_checkpoint *cp_block;
882 struct f2fs_super_block *fsb = sbi->raw_super;
883 struct page *cp1, *cp2, *cur_page;
884 unsigned long blk_size = sbi->blocksize;
885 unsigned long long cp1_version = 0, cp2_version = 0;
886 unsigned long long cp_start_blk_no;
55141486 887 unsigned int cp_blks = 1 + __cp_payload(sbi);
1dbe4152
CL
888 block_t cp_blk_no;
889 int i;
10f966bb 890 int err;
127e670a 891
026f0507
KC
892 sbi->ckpt = f2fs_kzalloc(sbi, array_size(blk_size, cp_blks),
893 GFP_KERNEL);
127e670a
JK
894 if (!sbi->ckpt)
895 return -ENOMEM;
896 /*
897 * Finding out valid cp block involves read both
7a88ddb5 898 * sets( cp pack 1 and cp pack 2)
127e670a
JK
899 */
900 cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr);
901 cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version);
902
903 /* The second checkpoint pack should start at the next segment */
f9a4e6df
JK
904 cp_start_blk_no += ((unsigned long long)1) <<
905 le32_to_cpu(fsb->log_blocks_per_seg);
127e670a
JK
906 cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version);
907
908 if (cp1 && cp2) {
909 if (ver_after(cp2_version, cp1_version))
910 cur_page = cp2;
911 else
912 cur_page = cp1;
913 } else if (cp1) {
914 cur_page = cp1;
915 } else if (cp2) {
916 cur_page = cp2;
917 } else {
10f966bb 918 err = -EFSCORRUPTED;
127e670a
JK
919 goto fail_no_cp;
920 }
921
922 cp_block = (struct f2fs_checkpoint *)page_address(cur_page);
923 memcpy(sbi->ckpt, cp_block, blk_size);
924
8508e44a
JK
925 if (cur_page == cp1)
926 sbi->cur_cp_pack = 1;
927 else
928 sbi->cur_cp_pack = 2;
984ec63c 929
e494c2f9 930 /* Sanity checking of checkpoint */
10f966bb
CY
931 if (f2fs_sanity_check_ckpt(sbi)) {
932 err = -EFSCORRUPTED;
e494c2f9 933 goto free_fail_no_cp;
10f966bb 934 }
e494c2f9 935
1dbe4152
CL
936 if (cp_blks <= 1)
937 goto done;
938
939 cp_blk_no = le32_to_cpu(fsb->cp_blkaddr);
940 if (cur_page == cp2)
941 cp_blk_no += 1 << le32_to_cpu(fsb->log_blocks_per_seg);
942
943 for (i = 1; i < cp_blks; i++) {
944 void *sit_bitmap_ptr;
945 unsigned char *ckpt = (unsigned char *)sbi->ckpt;
946
4d57b86d 947 cur_page = f2fs_get_meta_page(sbi, cp_blk_no + i);
10f966bb
CY
948 if (IS_ERR(cur_page)) {
949 err = PTR_ERR(cur_page);
7735730d 950 goto free_fail_no_cp;
10f966bb 951 }
1dbe4152
CL
952 sit_bitmap_ptr = page_address(cur_page);
953 memcpy(ckpt + i * blk_size, sit_bitmap_ptr, blk_size);
954 f2fs_put_page(cur_page, 1);
955 }
956done:
127e670a
JK
957 f2fs_put_page(cp1, 1);
958 f2fs_put_page(cp2, 1);
959 return 0;
960
a2125ff7
JK
961free_fail_no_cp:
962 f2fs_put_page(cp1, 1);
963 f2fs_put_page(cp2, 1);
127e670a 964fail_no_cp:
5222595d 965 kvfree(sbi->ckpt);
10f966bb 966 return err;
127e670a
JK
967}
968
c227f912 969static void __add_dirty_inode(struct inode *inode, enum inode_type type)
127e670a 970{
4081363f 971 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
c227f912 972 int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
127e670a 973
91942321 974 if (is_inode_flag_set(inode, flag))
2710fd7e 975 return;
2d7b822a 976
91942321 977 set_inode_flag(inode, flag);
99f4b917
CY
978 if (!f2fs_is_volatile_file(inode))
979 list_add_tail(&F2FS_I(inode)->dirty_list,
980 &sbi->inode_list[type]);
33fbd510 981 stat_inc_dirty_inode(sbi, type);
5deb8267
JK
982}
983
c227f912 984static void __remove_dirty_inode(struct inode *inode, enum inode_type type)
6ad7609a 985{
c227f912 986 int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
6ad7609a 987
91942321 988 if (get_dirty_pages(inode) || !is_inode_flag_set(inode, flag))
6ad7609a
CY
989 return;
990
91942321
JK
991 list_del_init(&F2FS_I(inode)->dirty_list);
992 clear_inode_flag(inode, flag);
33fbd510 993 stat_dec_dirty_inode(F2FS_I_SB(inode), type);
6ad7609a
CY
994}
995
4d57b86d 996void f2fs_update_dirty_page(struct inode *inode, struct page *page)
5deb8267 997{
4081363f 998 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
c227f912 999 enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
5deb8267 1000
5ac9f36f
CY
1001 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
1002 !S_ISLNK(inode->i_mode))
127e670a 1003 return;
7bd59381 1004
1c4bf763
JK
1005 spin_lock(&sbi->inode_lock[type]);
1006 if (type != FILE_INODE || test_opt(sbi, DATA_FLUSH))
10aa97c3 1007 __add_dirty_inode(inode, type);
b951a4ec 1008 inode_inc_dirty_pages(inode);
1c4bf763
JK
1009 spin_unlock(&sbi->inode_lock[type]);
1010
240a5915 1011 f2fs_set_page_private(page, 0);
9e4ded3f 1012 f2fs_trace_pid(page);
5deb8267
JK
1013}
1014
4d57b86d 1015void f2fs_remove_dirty_inode(struct inode *inode)
127e670a 1016{
4081363f 1017 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
c227f912 1018 enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
127e670a 1019
c227f912
CY
1020 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
1021 !S_ISLNK(inode->i_mode))
127e670a
JK
1022 return;
1023
10aa97c3
JK
1024 if (type == FILE_INODE && !test_opt(sbi, DATA_FLUSH))
1025 return;
1026
c227f912
CY
1027 spin_lock(&sbi->inode_lock[type]);
1028 __remove_dirty_inode(inode, type);
1029 spin_unlock(&sbi->inode_lock[type]);
74d0b917
JK
1030}
1031
4d57b86d 1032int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type)
127e670a 1033{
ce3b7d80 1034 struct list_head *head;
127e670a 1035 struct inode *inode;
2710fd7e 1036 struct f2fs_inode_info *fi;
4cf18537 1037 bool is_dir = (type == DIR_INODE);
4db08d01 1038 unsigned long ino = 0;
4cf18537
CY
1039
1040 trace_f2fs_sync_dirty_inodes_enter(sbi->sb, is_dir,
1041 get_pages(sbi, is_dir ?
1042 F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
127e670a 1043retry:
af41d3ee 1044 if (unlikely(f2fs_cp_error(sbi)))
6d5a1495 1045 return -EIO;
af41d3ee 1046
c227f912 1047 spin_lock(&sbi->inode_lock[type]);
ce3b7d80 1048
c227f912 1049 head = &sbi->inode_list[type];
127e670a 1050 if (list_empty(head)) {
c227f912 1051 spin_unlock(&sbi->inode_lock[type]);
4cf18537
CY
1052 trace_f2fs_sync_dirty_inodes_exit(sbi->sb, is_dir,
1053 get_pages(sbi, is_dir ?
1054 F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
6d5a1495 1055 return 0;
127e670a 1056 }
939afa94 1057 fi = list_first_entry(head, struct f2fs_inode_info, dirty_list);
2710fd7e 1058 inode = igrab(&fi->vfs_inode);
c227f912 1059 spin_unlock(&sbi->inode_lock[type]);
127e670a 1060 if (inode) {
4db08d01
JK
1061 unsigned long cur_ino = inode->i_ino;
1062
186857c5 1063 F2FS_I(inode)->cp_task = current;
b0af6d49 1064
87d6f890 1065 filemap_fdatawrite(inode->i_mapping);
b0af6d49 1066
186857c5 1067 F2FS_I(inode)->cp_task = NULL;
b0af6d49 1068
127e670a 1069 iput(inode);
4db08d01 1070 /* We need to give cpu to another writers. */
2a63531a 1071 if (ino == cur_ino)
4db08d01 1072 cond_resched();
2a63531a 1073 else
4db08d01 1074 ino = cur_ino;
127e670a
JK
1075 } else {
1076 /*
1077 * We should submit bio, since it exists several
1078 * wribacking dentry pages in the freeing inode.
1079 */
b9109b0e 1080 f2fs_submit_merged_write(sbi, DATA);
7ecebe5e 1081 cond_resched();
127e670a
JK
1082 }
1083 goto retry;
1084}
1085
0f18b462
JK
1086int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi)
1087{
1088 struct list_head *head = &sbi->inode_list[DIRTY_META];
1089 struct inode *inode;
1090 struct f2fs_inode_info *fi;
1091 s64 total = get_pages(sbi, F2FS_DIRTY_IMETA);
1092
1093 while (total--) {
1094 if (unlikely(f2fs_cp_error(sbi)))
1095 return -EIO;
1096
1097 spin_lock(&sbi->inode_lock[DIRTY_META]);
1098 if (list_empty(head)) {
1099 spin_unlock(&sbi->inode_lock[DIRTY_META]);
1100 return 0;
1101 }
939afa94 1102 fi = list_first_entry(head, struct f2fs_inode_info,
0f18b462
JK
1103 gdirty_list);
1104 inode = igrab(&fi->vfs_inode);
1105 spin_unlock(&sbi->inode_lock[DIRTY_META]);
1106 if (inode) {
18340edc
JK
1107 sync_inode_metadata(inode, 0);
1108
1109 /* it's on eviction */
1110 if (is_inode_flag_set(inode, FI_DIRTY_INODE))
4d57b86d 1111 f2fs_update_inode_page(inode);
0f18b462
JK
1112 iput(inode);
1113 }
dee668c1 1114 }
0f18b462
JK
1115 return 0;
1116}
1117
59c9081b
YH
1118static void __prepare_cp_block(struct f2fs_sb_info *sbi)
1119{
1120 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1121 struct f2fs_nm_info *nm_i = NM_I(sbi);
1122 nid_t last_nid = nm_i->next_scan_nid;
1123
1124 next_free_nid(sbi, &last_nid);
1125 ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi));
1126 ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi));
1127 ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi));
1128 ckpt->next_free_nid = cpu_to_le32(last_nid);
1129}
1130
af033b2a
CY
1131static bool __need_flush_quota(struct f2fs_sb_info *sbi)
1132{
db6ec53b
JK
1133 bool ret = false;
1134
af033b2a
CY
1135 if (!is_journalled_quota(sbi))
1136 return false;
db6ec53b
JK
1137
1138 down_write(&sbi->quota_sem);
1139 if (is_sbi_flag_set(sbi, SBI_QUOTA_SKIP_FLUSH)) {
1140 ret = false;
1141 } else if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_REPAIR)) {
1142 ret = false;
1143 } else if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_FLUSH)) {
1144 clear_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH);
1145 ret = true;
1146 } else if (get_pages(sbi, F2FS_DIRTY_QDATA)) {
1147 ret = true;
1148 }
1149 up_write(&sbi->quota_sem);
1150 return ret;
af033b2a
CY
1151}
1152
0a8165d7 1153/*
127e670a
JK
1154 * Freeze all the FS-operations for checkpoint.
1155 */
cf779cab 1156static int block_operations(struct f2fs_sb_info *sbi)
127e670a 1157{
127e670a
JK
1158 struct writeback_control wbc = {
1159 .sync_mode = WB_SYNC_ALL,
1160 .nr_to_write = LONG_MAX,
1161 .for_reclaim = 0,
1162 };
c718379b 1163 struct blk_plug plug;
af033b2a 1164 int err = 0, cnt = 0;
c718379b
JK
1165
1166 blk_start_plug(&plug);
1167
af033b2a 1168retry_flush_quotas:
db6ec53b 1169 f2fs_lock_all(sbi);
af033b2a
CY
1170 if (__need_flush_quota(sbi)) {
1171 int locked;
1172
1173 if (++cnt > DEFAULT_RETRY_QUOTA_FLUSH_COUNT) {
1174 set_sbi_flag(sbi, SBI_QUOTA_SKIP_FLUSH);
db6ec53b 1175 set_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH);
af033b2a
CY
1176 goto retry_flush_dents;
1177 }
db6ec53b 1178 f2fs_unlock_all(sbi);
af033b2a
CY
1179
1180 /* only failed during mount/umount/freeze/quotactl */
1181 locked = down_read_trylock(&sbi->sb->s_umount);
1182 f2fs_quota_sync(sbi->sb, -1);
1183 if (locked)
1184 up_read(&sbi->sb->s_umount);
af033b2a
CY
1185 cond_resched();
1186 goto retry_flush_quotas;
1187 }
1188
1189retry_flush_dents:
127e670a 1190 /* write all the dirty dentry pages */
127e670a 1191 if (get_pages(sbi, F2FS_DIRTY_DENTS)) {
e479556b 1192 f2fs_unlock_all(sbi);
4d57b86d 1193 err = f2fs_sync_dirty_inodes(sbi, DIR_INODE);
6d5a1495 1194 if (err)
cf779cab 1195 goto out;
30973883 1196 cond_resched();
af033b2a 1197 goto retry_flush_quotas;
127e670a
JK
1198 }
1199
59c9081b
YH
1200 /*
1201 * POR: we should ensure that there are no dirty node pages
1202 * until finishing nat/sit flush. inode->i_blocks can be updated.
1203 */
1204 down_write(&sbi->node_change);
1205
0f18b462 1206 if (get_pages(sbi, F2FS_DIRTY_IMETA)) {
59c9081b 1207 up_write(&sbi->node_change);
0f18b462
JK
1208 f2fs_unlock_all(sbi);
1209 err = f2fs_sync_inode_meta(sbi);
1210 if (err)
1211 goto out;
30973883 1212 cond_resched();
af033b2a 1213 goto retry_flush_quotas;
0f18b462
JK
1214 }
1215
39936837 1216retry_flush_nodes:
b3582c68 1217 down_write(&sbi->node_write);
127e670a
JK
1218
1219 if (get_pages(sbi, F2FS_DIRTY_NODES)) {
b3582c68 1220 up_write(&sbi->node_write);
c29fd0c0 1221 atomic_inc(&sbi->wb_sync_req[NODE]);
4d57b86d 1222 err = f2fs_sync_node_pages(sbi, &wbc, false, FS_CP_NODE_IO);
c29fd0c0 1223 atomic_dec(&sbi->wb_sync_req[NODE]);
6d5a1495 1224 if (err) {
59c9081b 1225 up_write(&sbi->node_change);
cf779cab 1226 f2fs_unlock_all(sbi);
cf779cab
JK
1227 goto out;
1228 }
30973883 1229 cond_resched();
39936837 1230 goto retry_flush_nodes;
127e670a 1231 }
59c9081b
YH
1232
1233 /*
1234 * sbi->node_change is used only for AIO write_begin path which produces
1235 * dirty node blocks and some checkpoint values by block allocation.
1236 */
1237 __prepare_cp_block(sbi);
1238 up_write(&sbi->node_change);
cf779cab 1239out:
c718379b 1240 blk_finish_plug(&plug);
cf779cab 1241 return err;
127e670a
JK
1242}
1243
1244static void unblock_operations(struct f2fs_sb_info *sbi)
1245{
b3582c68 1246 up_write(&sbi->node_write);
e479556b 1247 f2fs_unlock_all(sbi);
127e670a
JK
1248}
1249
bf22c3cc 1250void f2fs_wait_on_all_pages(struct f2fs_sb_info *sbi, int type)
fb51b5ef
CL
1251{
1252 DEFINE_WAIT(wait);
1253
1254 for (;;) {
1255 prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE);
1256
bf22c3cc 1257 if (!get_pages(sbi, type))
fb51b5ef
CL
1258 break;
1259
af697c0f
JK
1260 if (unlikely(f2fs_cp_error(sbi)))
1261 break;
1262
5df7731f 1263 io_schedule_timeout(DEFAULT_IO_TIMEOUT);
fb51b5ef
CL
1264 }
1265 finish_wait(&sbi->cp_wait, &wait);
1266}
1267
e4c5d848
JK
1268static void update_ckpt_flags(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1269{
1270 unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num;
1271 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
d1aa2453 1272 unsigned long flags;
e4c5d848 1273
d1aa2453 1274 spin_lock_irqsave(&sbi->cp_lock, flags);
e4c5d848 1275
c473f1a9 1276 if ((cpc->reason & CP_UMOUNT) &&
10047f53 1277 le32_to_cpu(ckpt->cp_pack_total_block_count) >
22ad0b6a
JK
1278 sbi->blocks_per_seg - NM_I(sbi)->nat_bits_blocks)
1279 disable_nat_bits(sbi, false);
1280
1f43e2ad
CY
1281 if (cpc->reason & CP_TRIMMED)
1282 __set_ckpt_flags(ckpt, CP_TRIMMED_FLAG);
cd36d7a1
CY
1283 else
1284 __clear_ckpt_flags(ckpt, CP_TRIMMED_FLAG);
1f43e2ad 1285
c473f1a9 1286 if (cpc->reason & CP_UMOUNT)
e4c5d848
JK
1287 __set_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
1288 else
1289 __clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
1290
c473f1a9 1291 if (cpc->reason & CP_FASTBOOT)
e4c5d848
JK
1292 __set_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
1293 else
1294 __clear_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
1295
1296 if (orphan_num)
1297 __set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
1298 else
1299 __clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
1300
c84ef3c5 1301 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK))
e4c5d848
JK
1302 __set_ckpt_flags(ckpt, CP_FSCK_FLAG);
1303
c84ef3c5
ST
1304 if (is_sbi_flag_set(sbi, SBI_IS_RESIZEFS))
1305 __set_ckpt_flags(ckpt, CP_RESIZEFS_FLAG);
1306 else
1307 __clear_ckpt_flags(ckpt, CP_RESIZEFS_FLAG);
1308
4354994f
DR
1309 if (is_sbi_flag_set(sbi, SBI_CP_DISABLED))
1310 __set_ckpt_flags(ckpt, CP_DISABLED_FLAG);
1311 else
1312 __clear_ckpt_flags(ckpt, CP_DISABLED_FLAG);
1313
db610a64
JK
1314 if (is_sbi_flag_set(sbi, SBI_CP_DISABLED_QUICK))
1315 __set_ckpt_flags(ckpt, CP_DISABLED_QUICK_FLAG);
1316 else
1317 __clear_ckpt_flags(ckpt, CP_DISABLED_QUICK_FLAG);
1318
af033b2a
CY
1319 if (is_sbi_flag_set(sbi, SBI_QUOTA_SKIP_FLUSH))
1320 __set_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG);
bc88ac96
JK
1321 else
1322 __clear_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG);
af033b2a
CY
1323
1324 if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_REPAIR))
1325 __set_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG);
1326
e4c5d848
JK
1327 /* set this flag to activate crc|cp_ver for recovery */
1328 __set_ckpt_flags(ckpt, CP_CRC_RECOVERY_FLAG);
f2367923 1329 __clear_ckpt_flags(ckpt, CP_NOCRC_RECOVERY_FLAG);
e4c5d848 1330
d1aa2453 1331 spin_unlock_irqrestore(&sbi->cp_lock, flags);
e4c5d848
JK
1332}
1333
46706d59
GX
1334static void commit_checkpoint(struct f2fs_sb_info *sbi,
1335 void *src, block_t blk_addr)
1336{
1337 struct writeback_control wbc = {
1338 .for_reclaim = 0,
1339 };
1340
1341 /*
1342 * pagevec_lookup_tag and lock_page again will take
4d57b86d
CY
1343 * some extra time. Therefore, f2fs_update_meta_pages and
1344 * f2fs_sync_meta_pages are combined in this function.
46706d59 1345 */
4d57b86d 1346 struct page *page = f2fs_grab_meta_page(sbi, blk_addr);
46706d59
GX
1347 int err;
1348
bae0ee7a 1349 f2fs_wait_on_page_writeback(page, META, true, true);
8d64d365
CY
1350
1351 memcpy(page_address(page), src, PAGE_SIZE);
1352
1353 set_page_dirty(page);
46706d59
GX
1354 if (unlikely(!clear_page_dirty_for_io(page)))
1355 f2fs_bug_on(sbi, 1);
1356
1357 /* writeout cp pack 2 page */
1358 err = __f2fs_write_meta_page(page, &wbc, FS_CP_META_IO);
af697c0f
JK
1359 if (unlikely(err && f2fs_cp_error(sbi))) {
1360 f2fs_put_page(page, 1);
1361 return;
1362 }
46706d59 1363
af697c0f 1364 f2fs_bug_on(sbi, err);
46706d59
GX
1365 f2fs_put_page(page, 0);
1366
1367 /* submit checkpoint (with barrier if NOBARRIER is not set) */
1368 f2fs_submit_merged_write(sbi, META_FLUSH);
1369}
1370
c34f42e2 1371static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
127e670a
JK
1372{
1373 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
77041823 1374 struct f2fs_nm_info *nm_i = NM_I(sbi);
d1aa2453 1375 unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num, flags;
127e670a 1376 block_t start_blk;
127e670a 1377 unsigned int data_sum_blocks, orphan_blocks;
7e586fa0 1378 __u32 crc32 = 0;
127e670a 1379 int i;
55141486 1380 int cp_payload_blks = __cp_payload(sbi);
8f1dbbbb
SL
1381 struct super_block *sb = sbi->sb;
1382 struct curseg_info *seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
1383 u64 kbytes_written;
1228b482 1384 int err;
127e670a
JK
1385
1386 /* Flush all the NAT/SIT pages */
8ec18bff 1387 f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO);
127e670a 1388
7a88ddb5 1389 /* start to update checkpoint, cp ver is already updated previously */
a1f72ac2 1390 ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi, true));
127e670a 1391 ckpt->free_segment_count = cpu_to_le32(free_segments(sbi));
b5b82205 1392 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
127e670a
JK
1393 ckpt->cur_node_segno[i] =
1394 cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE));
1395 ckpt->cur_node_blkoff[i] =
1396 cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE));
1397 ckpt->alloc_type[i + CURSEG_HOT_NODE] =
1398 curseg_alloc_type(sbi, i + CURSEG_HOT_NODE);
1399 }
b5b82205 1400 for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
127e670a
JK
1401 ckpt->cur_data_segno[i] =
1402 cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA));
1403 ckpt->cur_data_blkoff[i] =
1404 cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA));
1405 ckpt->alloc_type[i + CURSEG_HOT_DATA] =
1406 curseg_alloc_type(sbi, i + CURSEG_HOT_DATA);
1407 }
1408
127e670a 1409 /* 2 cp + n data seg summary + orphan inode blocks */
4d57b86d 1410 data_sum_blocks = f2fs_npages_for_summary_flush(sbi, false);
d1aa2453 1411 spin_lock_irqsave(&sbi->cp_lock, flags);
b5b82205 1412 if (data_sum_blocks < NR_CURSEG_DATA_TYPE)
aaec2b1d 1413 __set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
127e670a 1414 else
aaec2b1d 1415 __clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
d1aa2453 1416 spin_unlock_irqrestore(&sbi->cp_lock, flags);
127e670a 1417
67298804 1418 orphan_blocks = GET_ORPHAN_BLOCKS(orphan_num);
1dbe4152
CL
1419 ckpt->cp_pack_start_sum = cpu_to_le32(1 + cp_payload_blks +
1420 orphan_blocks);
127e670a 1421
119ee914 1422 if (__remain_node_summaries(cpc->reason))
b5b82205 1423 ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS+
1dbe4152
CL
1424 cp_payload_blks + data_sum_blocks +
1425 orphan_blocks + NR_CURSEG_NODE_TYPE);
119ee914 1426 else
b5b82205 1427 ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS +
1dbe4152
CL
1428 cp_payload_blks + data_sum_blocks +
1429 orphan_blocks);
119ee914 1430
e4c5d848
JK
1431 /* update ckpt flag for checkpoint */
1432 update_ckpt_flags(sbi, cpc);
a468f0ef 1433
127e670a
JK
1434 /* update SIT/NAT bitmap */
1435 get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP));
1436 get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP));
1437
d7eb8f1c 1438 crc32 = f2fs_checkpoint_chksum(sbi, ckpt);
7e586fa0
JK
1439 *((__le32 *)((unsigned char *)ckpt +
1440 le32_to_cpu(ckpt->checksum_offset)))
127e670a
JK
1441 = cpu_to_le32(crc32);
1442
8508e44a 1443 start_blk = __start_cp_next_addr(sbi);
127e670a 1444
22ad0b6a
JK
1445 /* write nat bits */
1446 if (enabled_nat_bits(sbi, cpc)) {
1447 __u64 cp_ver = cur_cp_version(ckpt);
22ad0b6a
JK
1448 block_t blk;
1449
1450 cp_ver |= ((__u64)crc32 << 32);
1451 *(__le64 *)nm_i->nat_bits = cpu_to_le64(cp_ver);
1452
1453 blk = start_blk + sbi->blocks_per_seg - nm_i->nat_bits_blocks;
1454 for (i = 0; i < nm_i->nat_bits_blocks; i++)
4d57b86d 1455 f2fs_update_meta_page(sbi, nm_i->nat_bits +
22ad0b6a 1456 (i << F2FS_BLKSIZE_BITS), blk + i);
22ad0b6a
JK
1457 }
1458
127e670a 1459 /* write out checkpoint buffer at block 0 */
4d57b86d 1460 f2fs_update_meta_page(sbi, ckpt, start_blk++);
381722d2
CY
1461
1462 for (i = 1; i < 1 + cp_payload_blks; i++)
4d57b86d 1463 f2fs_update_meta_page(sbi, (char *)ckpt + i * F2FS_BLKSIZE,
381722d2 1464 start_blk++);
1dbe4152 1465
67298804 1466 if (orphan_num) {
127e670a
JK
1467 write_orphan_inodes(sbi, start_blk);
1468 start_blk += orphan_blocks;
1469 }
1470
4d57b86d 1471 f2fs_write_data_summaries(sbi, start_blk);
127e670a 1472 start_blk += data_sum_blocks;
8f1dbbbb
SL
1473
1474 /* Record write statistics in the hot node summary */
1475 kbytes_written = sbi->kbytes_written;
1476 if (sb->s_bdev->bd_part)
1477 kbytes_written += BD_PART_WRITTEN(sbi);
1478
b7ad7512 1479 seg_i->journal->info.kbytes_written = cpu_to_le64(kbytes_written);
8f1dbbbb 1480
119ee914 1481 if (__remain_node_summaries(cpc->reason)) {
4d57b86d 1482 f2fs_write_node_summaries(sbi, start_blk);
127e670a
JK
1483 start_blk += NR_CURSEG_NODE_TYPE;
1484 }
1485
46706d59
GX
1486 /* update user_block_counts */
1487 sbi->last_valid_block_count = sbi->total_valid_block_count;
1488 percpu_counter_set(&sbi->alloc_valid_block_count, 0);
127e670a 1489
46706d59 1490 /* Here, we have one bio having CP pack except cp pack 2 page */
4d57b86d 1491 f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO);
bf22c3cc
ST
1492 /* Wait for all dirty meta pages to be submitted for IO */
1493 f2fs_wait_on_all_pages(sbi, F2FS_DIRTY_META);
46706d59
GX
1494
1495 /* wait for previous submitted meta pages writeback */
bf22c3cc 1496 f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA);
127e670a 1497
46706d59
GX
1498 /* flush all device cache */
1499 err = f2fs_flush_device_cache(sbi);
1500 if (err)
1501 return err;
127e670a 1502
46706d59
GX
1503 /* barrier and flush checkpoint cp pack 2 page if it can */
1504 commit_checkpoint(sbi, ckpt, start_blk);
bf22c3cc 1505 f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA);
6a8f8ca5 1506
18767e62 1507 /*
542989b6
EB
1508 * invalidate intermediate page cache borrowed from meta inode which are
1509 * used for migration of encrypted or verity inode's blocks.
18767e62 1510 */
542989b6 1511 if (f2fs_sb_has_encrypt(sbi) || f2fs_sb_has_verity(sbi))
18767e62
CY
1512 invalidate_mapping_pages(META_MAPPING(sbi),
1513 MAIN_BLKADDR(sbi), MAX_BLKADDR(sbi) - 1);
1514
4d57b86d 1515 f2fs_release_ino_entry(sbi, false);
cf779cab 1516
50fa53ec
CY
1517 f2fs_reset_fsync_node_info(sbi);
1518
caf0047e 1519 clear_sbi_flag(sbi, SBI_IS_DIRTY);
bbf156f7 1520 clear_sbi_flag(sbi, SBI_NEED_CP);
af033b2a 1521 clear_sbi_flag(sbi, SBI_QUOTA_SKIP_FLUSH);
c9c8ed50
CY
1522
1523 spin_lock(&sbi->stat_lock);
4354994f 1524 sbi->unusable_block_count = 0;
c9c8ed50
CY
1525 spin_unlock(&sbi->stat_lock);
1526
8508e44a 1527 __set_cp_next_pack(sbi);
c34f42e2 1528
c2a080ae
CY
1529 /*
1530 * redirty superblock if metadata like node page or inode cache is
1531 * updated during writing checkpoint.
1532 */
1533 if (get_pages(sbi, F2FS_DIRTY_NODES) ||
1534 get_pages(sbi, F2FS_DIRTY_IMETA))
1535 set_sbi_flag(sbi, SBI_IS_DIRTY);
1536
1537 f2fs_bug_on(sbi, get_pages(sbi, F2FS_DIRTY_DENTS));
1538
af697c0f 1539 return unlikely(f2fs_cp_error(sbi)) ? -EIO : 0;
127e670a
JK
1540}
1541
4d57b86d 1542int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
127e670a
JK
1543{
1544 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1545 unsigned long long ckpt_ver;
c34f42e2 1546 int err = 0;
127e670a 1547
f5a131bb
CY
1548 if (f2fs_readonly(sbi->sb) || f2fs_hw_is_readonly(sbi))
1549 return -EROFS;
1550
4354994f
DR
1551 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
1552 if (cpc->reason != CP_PAUSE)
1553 return 0;
dcbb4c10 1554 f2fs_warn(sbi, "Start checkpoint disabled!");
4354994f 1555 }
43727527 1556 mutex_lock(&sbi->cp_mutex);
8501017e 1557
caf0047e 1558 if (!is_sbi_flag_set(sbi, SBI_IS_DIRTY) &&
c473f1a9
CY
1559 ((cpc->reason & CP_FASTBOOT) || (cpc->reason & CP_SYNC) ||
1560 ((cpc->reason & CP_DISCARD) && !sbi->discard_blks)))
8501017e 1561 goto out;
c34f42e2
CY
1562 if (unlikely(f2fs_cp_error(sbi))) {
1563 err = -EIO;
cf779cab 1564 goto out;
c34f42e2 1565 }
2bda542d
WL
1566
1567 trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "start block_ops");
1568
c34f42e2
CY
1569 err = block_operations(sbi);
1570 if (err)
cf779cab 1571 goto out;
127e670a 1572
75ab4cb8 1573 trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish block_ops");
2af4bd6c 1574
b9109b0e 1575 f2fs_flush_merged_writes(sbi);
127e670a 1576
58cce381 1577 /* this is the case of multiple fstrims without any changes */
c473f1a9 1578 if (cpc->reason & CP_DISCARD) {
4d57b86d 1579 if (!f2fs_exist_trim_candidates(sbi, cpc)) {
25290fa5
JK
1580 unblock_operations(sbi);
1581 goto out;
1582 }
1583
0333ad4e
JK
1584 if (NM_I(sbi)->dirty_nat_cnt == 0 &&
1585 SIT_I(sbi)->dirty_sentries == 0 &&
1586 prefree_segments(sbi) == 0) {
4d57b86d
CY
1587 f2fs_flush_sit_entries(sbi, cpc);
1588 f2fs_clear_prefree_segments(sbi, cpc);
0333ad4e
JK
1589 unblock_operations(sbi);
1590 goto out;
1591 }
58cce381
YH
1592 }
1593
127e670a
JK
1594 /*
1595 * update checkpoint pack index
1596 * Increase the version number so that
1597 * SIT entries and seg summaries are written at correct place
1598 */
d71b5564 1599 ckpt_ver = cur_cp_version(ckpt);
127e670a
JK
1600 ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver);
1601
1602 /* write cached NAT/SIT entries to NAT/SIT area */
edc55aaf
JK
1603 err = f2fs_flush_nat_entries(sbi, cpc);
1604 if (err)
1605 goto stop;
1606
4d57b86d 1607 f2fs_flush_sit_entries(sbi, cpc);
127e670a 1608
c34f42e2 1609 err = do_checkpoint(sbi, cpc);
4e6a8d9b 1610 if (err)
4d57b86d 1611 f2fs_release_discard_addrs(sbi);
4e6a8d9b 1612 else
4d57b86d 1613 f2fs_clear_prefree_segments(sbi, cpc);
edc55aaf 1614stop:
127e670a 1615 unblock_operations(sbi);
942e0be6 1616 stat_inc_cp_count(sbi->stat_info);
10027551 1617
c473f1a9 1618 if (cpc->reason & CP_RECOVERY)
dcbb4c10 1619 f2fs_notice(sbi, "checkpoint: version = %llx", ckpt_ver);
60b99b48 1620
7a88ddb5 1621 /* update CP_TIME to trigger checkpoint periodically */
6beceb54 1622 f2fs_update_time(sbi, CP_TIME);
55d1cdb2 1623 trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish checkpoint");
8501017e
JK
1624out:
1625 mutex_unlock(&sbi->cp_mutex);
c34f42e2 1626 return err;
127e670a
JK
1627}
1628
4d57b86d 1629void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi)
127e670a 1630{
6451e041
JK
1631 int i;
1632
1633 for (i = 0; i < MAX_INO_ENTRY; i++) {
67298804
CY
1634 struct inode_management *im = &sbi->im[i];
1635
1636 INIT_RADIX_TREE(&im->ino_root, GFP_ATOMIC);
1637 spin_lock_init(&im->ino_lock);
1638 INIT_LIST_HEAD(&im->ino_list);
1639 im->ino_num = 0;
6451e041
JK
1640 }
1641
b5b82205 1642 sbi->max_orphans = (sbi->blocks_per_seg - F2FS_CP_PACKS -
14b42817
WL
1643 NR_CURSEG_TYPE - __cp_payload(sbi)) *
1644 F2FS_ORPHANS_PER_BLOCK;
127e670a
JK
1645}
1646
4d57b86d 1647int __init f2fs_create_checkpoint_caches(void)
127e670a 1648{
6451e041
JK
1649 ino_entry_slab = f2fs_kmem_cache_create("f2fs_ino_entry",
1650 sizeof(struct ino_entry));
1651 if (!ino_entry_slab)
127e670a 1652 return -ENOMEM;
4d57b86d 1653 f2fs_inode_entry_slab = f2fs_kmem_cache_create("f2fs_inode_entry",
06292073 1654 sizeof(struct inode_entry));
4d57b86d 1655 if (!f2fs_inode_entry_slab) {
6451e041 1656 kmem_cache_destroy(ino_entry_slab);
127e670a
JK
1657 return -ENOMEM;
1658 }
1659 return 0;
1660}
1661
4d57b86d 1662void f2fs_destroy_checkpoint_caches(void)
127e670a 1663{
6451e041 1664 kmem_cache_destroy(ino_entry_slab);
4d57b86d 1665 kmem_cache_destroy(f2fs_inode_entry_slab);
127e670a 1666}