f2fs: catch up to v4.14-rc1
[GitHub/exynos8895/android_kernel_samsung_universal8895.git] / fs / f2fs / checkpoint.c
CommitLineData
0a8165d7 1/*
127e670a
JK
2 * fs/f2fs/checkpoint.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/bio.h>
13#include <linux/mpage.h>
14#include <linux/writeback.h>
15#include <linux/blkdev.h>
16#include <linux/f2fs_fs.h>
17#include <linux/pagevec.h>
18#include <linux/swap.h>
19
20#include "f2fs.h"
21#include "node.h"
22#include "segment.h"
9e4ded3f 23#include "trace.h"
2af4bd6c 24#include <trace/events/f2fs.h>
127e670a 25
6451e041 26static struct kmem_cache *ino_entry_slab;
06292073 27struct kmem_cache *inode_entry_slab;
127e670a 28
c1286ff4
JK
29void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io)
30{
31 set_ckpt_flags(sbi, CP_ERROR_FLAG);
32 sbi->sb->s_flags |= MS_RDONLY;
33 if (!end_io)
13f00235 34 f2fs_flush_merged_writes(sbi);
c1286ff4
JK
35}
36
0a8165d7 37/*
127e670a
JK
38 * We guarantee no failure on the returned page.
39 */
40struct page *grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
41{
9df27d98 42 struct address_space *mapping = META_MAPPING(sbi);
127e670a
JK
43 struct page *page = NULL;
44repeat:
c1286ff4 45 page = f2fs_grab_cache_page(mapping, index, false);
127e670a
JK
46 if (!page) {
47 cond_resched();
48 goto repeat;
49 }
c1286ff4
JK
50 f2fs_wait_on_page_writeback(page, META, true);
51 if (!PageUptodate(page))
52 SetPageUptodate(page);
127e670a
JK
53 return page;
54}
55
0a8165d7 56/*
127e670a
JK
57 * We guarantee no failure on the returned page.
58 */
2b947003
CY
59static struct page *__get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index,
60 bool is_meta)
127e670a 61{
9df27d98 62 struct address_space *mapping = META_MAPPING(sbi);
127e670a 63 struct page *page;
cf04e8eb 64 struct f2fs_io_info fio = {
05ca3632 65 .sbi = sbi,
cf04e8eb 66 .type = META,
dc45fd9e
JK
67 .op = REQ_OP_READ,
68 .op_flags = REQ_SYNC | REQ_META | REQ_PRIO,
c1286ff4
JK
69 .old_blkaddr = index,
70 .new_blkaddr = index,
4375a336 71 .encrypted_page = NULL,
cf04e8eb 72 };
2b947003
CY
73
74 if (unlikely(!is_meta))
dc45fd9e 75 fio.op_flags &= ~REQ_META;
127e670a 76repeat:
c1286ff4 77 page = f2fs_grab_cache_page(mapping, index, false);
127e670a
JK
78 if (!page) {
79 cond_resched();
80 goto repeat;
81 }
393ff91f
JK
82 if (PageUptodate(page))
83 goto out;
84
05ca3632
JK
85 fio.page = page;
86
86531d6b
JK
87 if (f2fs_submit_page_bio(&fio)) {
88 f2fs_put_page(page, 1);
127e670a 89 goto repeat;
86531d6b 90 }
127e670a 91
393ff91f 92 lock_page(page);
6bacf52f 93 if (unlikely(page->mapping != mapping)) {
afcb7ca0
JK
94 f2fs_put_page(page, 1);
95 goto repeat;
96 }
f3f338ca
CY
97
98 /*
99 * if there is any IO error when accessing device, make our filesystem
100 * readonly and make sure do not write checkpoint with non-uptodate
101 * meta page.
102 */
103 if (unlikely(!PageUptodate(page)))
c1286ff4 104 f2fs_stop_checkpoint(sbi, false);
393ff91f 105out:
127e670a
JK
106 return page;
107}
108
2b947003
CY
109struct page *get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
110{
111 return __get_meta_page(sbi, index, true);
112}
113
114/* for POR only */
115struct page *get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index)
116{
117 return __get_meta_page(sbi, index, false);
118}
119
f0c9cada 120bool is_valid_blkaddr(struct f2fs_sb_info *sbi, block_t blkaddr, int type)
662befda
CY
121{
122 switch (type) {
123 case META_NAT:
66b00c18 124 break;
662befda 125 case META_SIT:
66b00c18
CY
126 if (unlikely(blkaddr >= SIT_BLK_CNT(sbi)))
127 return false;
128 break;
81c1a0f1 129 case META_SSA:
66b00c18
CY
130 if (unlikely(blkaddr >= MAIN_BLKADDR(sbi) ||
131 blkaddr < SM_I(sbi)->ssa_blkaddr))
132 return false;
133 break;
662befda 134 case META_CP:
66b00c18
CY
135 if (unlikely(blkaddr >= SIT_I(sbi)->sit_base_addr ||
136 blkaddr < __start_cp_addr(sbi)))
137 return false;
138 break;
4c521f49 139 case META_POR:
66b00c18
CY
140 if (unlikely(blkaddr >= MAX_BLKADDR(sbi) ||
141 blkaddr < MAIN_BLKADDR(sbi)))
142 return false;
143 break;
662befda
CY
144 default:
145 BUG();
146 }
66b00c18
CY
147
148 return true;
662befda
CY
149}
150
151/*
81c1a0f1 152 * Readahead CP/NAT/SIT/SSA pages
662befda 153 */
26879fb1
CY
154int ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
155 int type, bool sync)
662befda 156{
662befda 157 struct page *page;
4c521f49 158 block_t blkno = start;
662befda 159 struct f2fs_io_info fio = {
05ca3632 160 .sbi = sbi,
662befda 161 .type = META,
dc45fd9e
JK
162 .op = REQ_OP_READ,
163 .op_flags = sync ? (REQ_SYNC | REQ_META | REQ_PRIO) :
164 REQ_RAHEAD,
4375a336 165 .encrypted_page = NULL,
13f00235 166 .in_list = false,
662befda 167 };
c1286ff4 168 struct blk_plug plug;
662befda 169
2b947003 170 if (unlikely(type == META_POR))
dc45fd9e 171 fio.op_flags &= ~REQ_META;
2b947003 172
c1286ff4 173 blk_start_plug(&plug);
662befda 174 for (; nrpages-- > 0; blkno++) {
662befda 175
66b00c18
CY
176 if (!is_valid_blkaddr(sbi, blkno, type))
177 goto out;
178
662befda
CY
179 switch (type) {
180 case META_NAT:
66b00c18
CY
181 if (unlikely(blkno >=
182 NAT_BLOCK_OFFSET(NM_I(sbi)->max_nid)))
662befda 183 blkno = 0;
66b00c18 184 /* get nat block addr */
c1286ff4 185 fio.new_blkaddr = current_nat_addr(sbi,
662befda
CY
186 blkno * NAT_ENTRY_PER_BLOCK);
187 break;
188 case META_SIT:
189 /* get sit block addr */
c1286ff4 190 fio.new_blkaddr = current_sit_addr(sbi,
662befda 191 blkno * SIT_ENTRY_PER_BLOCK);
662befda 192 break;
81c1a0f1 193 case META_SSA:
662befda 194 case META_CP:
4c521f49 195 case META_POR:
c1286ff4 196 fio.new_blkaddr = blkno;
662befda
CY
197 break;
198 default:
199 BUG();
200 }
201
c1286ff4
JK
202 page = f2fs_grab_cache_page(META_MAPPING(sbi),
203 fio.new_blkaddr, false);
662befda
CY
204 if (!page)
205 continue;
206 if (PageUptodate(page)) {
662befda
CY
207 f2fs_put_page(page, 1);
208 continue;
209 }
210
05ca3632 211 fio.page = page;
13f00235 212 f2fs_submit_page_bio(&fio);
662befda
CY
213 f2fs_put_page(page, 0);
214 }
215out:
c1286ff4 216 blk_finish_plug(&plug);
662befda
CY
217 return blkno - start;
218}
219
635aee1f
CY
220void ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index)
221{
222 struct page *page;
223 bool readahead = false;
224
225 page = find_get_page(META_MAPPING(sbi), index);
c1286ff4 226 if (!page || !PageUptodate(page))
635aee1f
CY
227 readahead = true;
228 f2fs_put_page(page, 0);
229
230 if (readahead)
04030d21 231 ra_meta_pages(sbi, index, BIO_MAX_PAGES, META_POR, true);
635aee1f
CY
232}
233
13f00235
JK
234static int __f2fs_write_meta_page(struct page *page,
235 struct writeback_control *wbc,
236 enum iostat_type io_type)
127e670a 237{
4081363f 238 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
127e670a 239
ecda0de3
CY
240 trace_f2fs_writepage(page, META);
241
caf0047e 242 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
cfb271d4 243 goto redirty_out;
857dc4e0 244 if (wbc->for_reclaim && page->index < GET_SUM_BLOCK(sbi, 0))
cfb271d4 245 goto redirty_out;
1e968fdf 246 if (unlikely(f2fs_cp_error(sbi)))
cf779cab 247 goto redirty_out;
127e670a 248
13f00235 249 write_meta_page(sbi, page, io_type);
577e3495 250 dec_page_count(sbi, F2FS_DIRTY_META);
857dc4e0
JK
251
252 if (wbc->for_reclaim)
13f00235
JK
253 f2fs_submit_merged_write_cond(sbi, page->mapping->host,
254 0, page->index, META);
c1286ff4
JK
255
256 unlock_page(page);
257
258 if (unlikely(f2fs_cp_error(sbi)))
13f00235 259 f2fs_submit_merged_write(sbi, META);
c1286ff4 260
577e3495 261 return 0;
cfb271d4
CY
262
263redirty_out:
76f60268 264 redirty_page_for_writepage(wbc, page);
cfb271d4 265 return AOP_WRITEPAGE_ACTIVATE;
127e670a
JK
266}
267
13f00235
JK
268static int f2fs_write_meta_page(struct page *page,
269 struct writeback_control *wbc)
270{
271 return __f2fs_write_meta_page(page, wbc, FS_META_IO);
272}
273
127e670a
JK
274static int f2fs_write_meta_pages(struct address_space *mapping,
275 struct writeback_control *wbc)
276{
4081363f 277 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
50c8cdb3 278 long diff, written;
127e670a 279
13f00235
JK
280 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
281 goto skip_write;
282
5459aa97 283 /* collect a number of dirty meta pages and write together */
50c8cdb3
JK
284 if (wbc->for_kupdate ||
285 get_pages(sbi, F2FS_DIRTY_META) < nr_pages_to_skip(sbi, META))
d3baf95d 286 goto skip_write;
127e670a 287
13f00235
JK
288 /* if locked failed, cp will flush dirty pages instead */
289 if (!mutex_trylock(&sbi->cp_mutex))
290 goto skip_write;
c1286ff4 291
13f00235 292 trace_f2fs_writepages(mapping->host, wbc, META);
50c8cdb3 293 diff = nr_pages_to_write(sbi, META, wbc);
13f00235 294 written = sync_meta_pages(sbi, META, wbc->nr_to_write, FS_META_IO);
127e670a 295 mutex_unlock(&sbi->cp_mutex);
50c8cdb3 296 wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff);
127e670a 297 return 0;
d3baf95d
JK
298
299skip_write:
300 wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_META);
c1286ff4 301 trace_f2fs_writepages(mapping->host, wbc, META);
d3baf95d 302 return 0;
127e670a
JK
303}
304
305long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
13f00235 306 long nr_to_write, enum iostat_type io_type)
127e670a 307{
9df27d98 308 struct address_space *mapping = META_MAPPING(sbi);
c1286ff4 309 pgoff_t index = 0, end = ULONG_MAX, prev = ULONG_MAX;
127e670a
JK
310 struct pagevec pvec;
311 long nwritten = 0;
312 struct writeback_control wbc = {
313 .for_reclaim = 0,
314 };
c1286ff4 315 struct blk_plug plug;
127e670a
JK
316
317 pagevec_init(&pvec, 0);
318
c1286ff4
JK
319 blk_start_plug(&plug);
320
127e670a
JK
321 while (index <= end) {
322 int i, nr_pages;
323 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
324 PAGECACHE_TAG_DIRTY,
325 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
cfb271d4 326 if (unlikely(nr_pages == 0))
127e670a
JK
327 break;
328
329 for (i = 0; i < nr_pages; i++) {
330 struct page *page = pvec.pages[i];
203681f6 331
c1286ff4 332 if (prev == ULONG_MAX)
6066d8cd
JK
333 prev = page->index - 1;
334 if (nr_to_write != LONG_MAX && page->index != prev + 1) {
335 pagevec_release(&pvec);
336 goto stop;
337 }
338
127e670a 339 lock_page(page);
203681f6
JK
340
341 if (unlikely(page->mapping != mapping)) {
342continue_unlock:
343 unlock_page(page);
344 continue;
345 }
346 if (!PageDirty(page)) {
347 /* someone wrote it for us */
348 goto continue_unlock;
349 }
350
c1286ff4
JK
351 f2fs_wait_on_page_writeback(page, META, true);
352
353 BUG_ON(PageWriteback(page));
203681f6
JK
354 if (!clear_page_dirty_for_io(page))
355 goto continue_unlock;
356
13f00235 357 if (__f2fs_write_meta_page(page, &wbc, io_type)) {
577e3495
JK
358 unlock_page(page);
359 break;
360 }
cfb271d4 361 nwritten++;
6066d8cd 362 prev = page->index;
cfb271d4 363 if (unlikely(nwritten >= nr_to_write))
127e670a
JK
364 break;
365 }
366 pagevec_release(&pvec);
367 cond_resched();
368 }
6066d8cd 369stop:
127e670a 370 if (nwritten)
13f00235 371 f2fs_submit_merged_write(sbi, type);
127e670a 372
c1286ff4
JK
373 blk_finish_plug(&plug);
374
127e670a
JK
375 return nwritten;
376}
377
378static int f2fs_set_meta_page_dirty(struct page *page)
379{
26c6b887
JK
380 trace_f2fs_set_page_dirty(page, META);
381
c1286ff4
JK
382 if (!PageUptodate(page))
383 SetPageUptodate(page);
127e670a 384 if (!PageDirty(page)) {
c1286ff4 385 f2fs_set_page_dirty_nobuffers(page);
4081363f 386 inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_META);
1601839e 387 SetPagePrivate(page);
9e4ded3f 388 f2fs_trace_pid(page);
127e670a
JK
389 return 1;
390 }
391 return 0;
392}
393
394const struct address_space_operations f2fs_meta_aops = {
395 .writepage = f2fs_write_meta_page,
396 .writepages = f2fs_write_meta_pages,
397 .set_page_dirty = f2fs_set_meta_page_dirty,
487261f3
CY
398 .invalidatepage = f2fs_invalidate_page,
399 .releasepage = f2fs_release_page,
c1286ff4
JK
400#ifdef CONFIG_MIGRATION
401 .migratepage = f2fs_migrate_page,
402#endif
127e670a
JK
403};
404
6451e041 405static void __add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
953e6cc6 406{
67298804 407 struct inode_management *im = &sbi->im[type];
80c54505
JK
408 struct ino_entry *e, *tmp;
409
410 tmp = f2fs_kmem_cache_alloc(ino_entry_slab, GFP_NOFS);
39efac41 411retry:
80c54505 412 radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
769ec6e5 413
67298804 414 spin_lock(&im->ino_lock);
67298804 415 e = radix_tree_lookup(&im->ino_root, ino);
39efac41 416 if (!e) {
80c54505 417 e = tmp;
67298804
CY
418 if (radix_tree_insert(&im->ino_root, ino, e)) {
419 spin_unlock(&im->ino_lock);
769ec6e5 420 radix_tree_preload_end();
39efac41
JK
421 goto retry;
422 }
423 memset(e, 0, sizeof(struct ino_entry));
424 e->ino = ino;
953e6cc6 425
67298804 426 list_add_tail(&e->list, &im->ino_list);
8c402946 427 if (type != ORPHAN_INO)
67298804 428 im->ino_num++;
39efac41 429 }
67298804 430 spin_unlock(&im->ino_lock);
769ec6e5 431 radix_tree_preload_end();
80c54505
JK
432
433 if (e != tmp)
434 kmem_cache_free(ino_entry_slab, tmp);
953e6cc6
JK
435}
436
6451e041 437static void __remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
953e6cc6 438{
67298804 439 struct inode_management *im = &sbi->im[type];
6451e041 440 struct ino_entry *e;
953e6cc6 441
67298804
CY
442 spin_lock(&im->ino_lock);
443 e = radix_tree_lookup(&im->ino_root, ino);
39efac41
JK
444 if (e) {
445 list_del(&e->list);
67298804
CY
446 radix_tree_delete(&im->ino_root, ino);
447 im->ino_num--;
448 spin_unlock(&im->ino_lock);
39efac41
JK
449 kmem_cache_free(ino_entry_slab, e);
450 return;
953e6cc6 451 }
67298804 452 spin_unlock(&im->ino_lock);
953e6cc6
JK
453}
454
c1286ff4 455void add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
fff04f90
JK
456{
457 /* add new dirty ino entry into list */
458 __add_ino_entry(sbi, ino, type);
459}
460
c1286ff4 461void remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
fff04f90
JK
462{
463 /* remove dirty ino entry from list */
464 __remove_ino_entry(sbi, ino, type);
465}
466
467/* mode should be APPEND_INO or UPDATE_INO */
468bool exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode)
469{
67298804 470 struct inode_management *im = &sbi->im[mode];
fff04f90 471 struct ino_entry *e;
67298804
CY
472
473 spin_lock(&im->ino_lock);
474 e = radix_tree_lookup(&im->ino_root, ino);
475 spin_unlock(&im->ino_lock);
fff04f90
JK
476 return e ? true : false;
477}
478
c1286ff4 479void release_ino_entry(struct f2fs_sb_info *sbi, bool all)
fff04f90
JK
480{
481 struct ino_entry *e, *tmp;
482 int i;
483
c1286ff4 484 for (i = all ? ORPHAN_INO: APPEND_INO; i <= UPDATE_INO; i++) {
67298804
CY
485 struct inode_management *im = &sbi->im[i];
486
487 spin_lock(&im->ino_lock);
488 list_for_each_entry_safe(e, tmp, &im->ino_list, list) {
fff04f90 489 list_del(&e->list);
67298804 490 radix_tree_delete(&im->ino_root, e->ino);
fff04f90 491 kmem_cache_free(ino_entry_slab, e);
67298804 492 im->ino_num--;
fff04f90 493 }
67298804 494 spin_unlock(&im->ino_lock);
fff04f90
JK
495 }
496}
497
cbd56e7d 498int acquire_orphan_inode(struct f2fs_sb_info *sbi)
127e670a 499{
67298804 500 struct inode_management *im = &sbi->im[ORPHAN_INO];
127e670a
JK
501 int err = 0;
502
67298804 503 spin_lock(&im->ino_lock);
c1286ff4
JK
504
505#ifdef CONFIG_F2FS_FAULT_INJECTION
506 if (time_to_inject(sbi, FAULT_ORPHAN)) {
507 spin_unlock(&im->ino_lock);
13f00235 508 f2fs_show_injection_info(FAULT_ORPHAN);
c1286ff4
JK
509 return -ENOSPC;
510 }
511#endif
67298804 512 if (unlikely(im->ino_num >= sbi->max_orphans))
127e670a 513 err = -ENOSPC;
cbd56e7d 514 else
67298804
CY
515 im->ino_num++;
516 spin_unlock(&im->ino_lock);
0d47c1ad 517
127e670a
JK
518 return err;
519}
520
cbd56e7d
JK
521void release_orphan_inode(struct f2fs_sb_info *sbi)
522{
67298804
CY
523 struct inode_management *im = &sbi->im[ORPHAN_INO];
524
525 spin_lock(&im->ino_lock);
526 f2fs_bug_on(sbi, im->ino_num == 0);
527 im->ino_num--;
528 spin_unlock(&im->ino_lock);
cbd56e7d
JK
529}
530
c1286ff4 531void add_orphan_inode(struct inode *inode)
127e670a 532{
39efac41 533 /* add new orphan ino entry into list */
c1286ff4
JK
534 __add_ino_entry(F2FS_I_SB(inode), inode->i_ino, ORPHAN_INO);
535 update_inode_page(inode);
127e670a
JK
536}
537
538void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
539{
953e6cc6 540 /* remove orphan entry from orphan list */
6451e041 541 __remove_ino_entry(sbi, ino, ORPHAN_INO);
127e670a
JK
542}
543
8c14bfad 544static int recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
127e670a 545{
8c14bfad 546 struct inode *inode;
c1286ff4
JK
547 struct node_info ni;
548 int err = acquire_orphan_inode(sbi);
549
550 if (err) {
551 set_sbi_flag(sbi, SBI_NEED_FSCK);
552 f2fs_msg(sbi->sb, KERN_WARNING,
553 "%s: orphan failed (ino=%x), run fsck to fix.",
554 __func__, ino);
555 return err;
556 }
557
558 __add_ino_entry(sbi, ino, ORPHAN_INO);
8c14bfad 559
c1286ff4 560 inode = f2fs_iget_retry(sbi->sb, ino);
8c14bfad
CY
561 if (IS_ERR(inode)) {
562 /*
563 * there should be a bug that we can't find the entry
564 * to orphan inode.
565 */
566 f2fs_bug_on(sbi, PTR_ERR(inode) == -ENOENT);
567 return PTR_ERR(inode);
568 }
569
127e670a
JK
570 clear_nlink(inode);
571
572 /* truncate all the data during iput */
573 iput(inode);
c1286ff4
JK
574
575 get_node_info(sbi, ino, &ni);
576
577 /* ENOMEM was fully retried in f2fs_evict_inode. */
578 if (ni.blk_addr != NULL_ADDR) {
579 set_sbi_flag(sbi, SBI_NEED_FSCK);
580 f2fs_msg(sbi->sb, KERN_WARNING,
13f00235 581 "%s: orphan failed (ino=%x) by kernel, retry mount.",
c1286ff4
JK
582 __func__, ino);
583 return -EIO;
584 }
585 __remove_ino_entry(sbi, ino, ORPHAN_INO);
8c14bfad 586 return 0;
127e670a
JK
587}
588
8c14bfad 589int recover_orphan_inodes(struct f2fs_sb_info *sbi)
127e670a 590{
3c642985 591 block_t start_blk, orphan_blocks, i, j;
13f00235
JK
592 unsigned int s_flags = sbi->sb->s_flags;
593 int err = 0;
127e670a 594
c1286ff4 595 if (!is_set_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG))
8c14bfad 596 return 0;
127e670a 597
13f00235
JK
598 if (s_flags & MS_RDONLY) {
599 f2fs_msg(sbi->sb, KERN_INFO, "orphan cleanup on readonly fs");
600 sbi->sb->s_flags &= ~MS_RDONLY;
601 }
602
603#ifdef CONFIG_QUOTA
604 /* Needed for iput() to work correctly and not trash data */
605 sbi->sb->s_flags |= MS_ACTIVE;
606 /* Turn on quotas so that they are updated correctly */
607 f2fs_enable_quota_files(sbi);
608#endif
609
55141486 610 start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi);
3c642985 611 orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi);
127e670a 612
26879fb1 613 ra_meta_pages(sbi, start_blk, orphan_blocks, META_CP, true);
662befda 614
3c642985 615 for (i = 0; i < orphan_blocks; i++) {
127e670a
JK
616 struct page *page = get_meta_page(sbi, start_blk + i);
617 struct f2fs_orphan_block *orphan_blk;
618
619 orphan_blk = (struct f2fs_orphan_block *)page_address(page);
620 for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) {
621 nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
8c14bfad
CY
622 err = recover_orphan_inode(sbi, ino);
623 if (err) {
624 f2fs_put_page(page, 1);
13f00235 625 goto out;
8c14bfad 626 }
127e670a
JK
627 }
628 f2fs_put_page(page, 1);
629 }
630 /* clear Orphan Flag */
c1286ff4 631 clear_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG);
13f00235
JK
632out:
633#ifdef CONFIG_QUOTA
634 /* Turn quotas off */
635 f2fs_quota_off_umount(sbi->sb);
636#endif
637 sbi->sb->s_flags = s_flags; /* Restore MS_RDONLY status */
638
639 return err;
127e670a
JK
640}
641
642static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk)
643{
502c6e0b 644 struct list_head *head;
127e670a 645 struct f2fs_orphan_block *orphan_blk = NULL;
127e670a 646 unsigned int nentries = 0;
bd936f84 647 unsigned short index = 1;
8c402946 648 unsigned short orphan_blocks;
4531929e 649 struct page *page = NULL;
6451e041 650 struct ino_entry *orphan = NULL;
67298804 651 struct inode_management *im = &sbi->im[ORPHAN_INO];
127e670a 652
67298804 653 orphan_blocks = GET_ORPHAN_BLOCKS(im->ino_num);
8c402946 654
d6c67a4f
JK
655 /*
656 * we don't need to do spin_lock(&im->ino_lock) here, since all the
657 * orphan inode operations are covered under f2fs_lock_op().
658 * And, spin_lock should be avoided due to page operations below.
659 */
67298804 660 head = &im->ino_list;
127e670a
JK
661
662 /* loop for each orphan inode entry and write them in Jornal block */
502c6e0b
GZ
663 list_for_each_entry(orphan, head, list) {
664 if (!page) {
bd936f84 665 page = grab_meta_page(sbi, start_blk++);
502c6e0b
GZ
666 orphan_blk =
667 (struct f2fs_orphan_block *)page_address(page);
668 memset(orphan_blk, 0, sizeof(*orphan_blk));
669 }
127e670a 670
36795567 671 orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino);
127e670a 672
36795567 673 if (nentries == F2FS_ORPHANS_PER_BLOCK) {
127e670a
JK
674 /*
675 * an orphan block is full of 1020 entries,
676 * then we need to flush current orphan blocks
677 * and bring another one in memory
678 */
679 orphan_blk->blk_addr = cpu_to_le16(index);
680 orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
681 orphan_blk->entry_count = cpu_to_le32(nentries);
682 set_page_dirty(page);
683 f2fs_put_page(page, 1);
684 index++;
127e670a
JK
685 nentries = 0;
686 page = NULL;
687 }
502c6e0b 688 }
127e670a 689
502c6e0b
GZ
690 if (page) {
691 orphan_blk->blk_addr = cpu_to_le16(index);
692 orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
693 orphan_blk->entry_count = cpu_to_le32(nentries);
694 set_page_dirty(page);
695 f2fs_put_page(page, 1);
127e670a 696 }
127e670a
JK
697}
698
c1286ff4
JK
699static int get_checkpoint_version(struct f2fs_sb_info *sbi, block_t cp_addr,
700 struct f2fs_checkpoint **cp_block, struct page **cp_page,
701 unsigned long long *version)
127e670a 702{
127e670a 703 unsigned long blk_size = sbi->blocksize;
c1286ff4 704 size_t crc_offset = 0;
7e586fa0 705 __u32 crc = 0;
127e670a 706
c1286ff4
JK
707 *cp_page = get_meta_page(sbi, cp_addr);
708 *cp_block = (struct f2fs_checkpoint *)page_address(*cp_page);
127e670a 709
c1286ff4 710 crc_offset = le32_to_cpu((*cp_block)->checksum_offset);
13f00235 711 if (crc_offset > (blk_size - sizeof(__le32))) {
c1286ff4
JK
712 f2fs_msg(sbi->sb, KERN_WARNING,
713 "invalid crc_offset: %zu", crc_offset);
714 return -EINVAL;
715 }
127e670a 716
13f00235 717 crc = cur_cp_crc(*cp_block);
c1286ff4
JK
718 if (!f2fs_crc_valid(sbi, crc, *cp_block, crc_offset)) {
719 f2fs_msg(sbi->sb, KERN_WARNING, "invalid crc value");
720 return -EINVAL;
721 }
127e670a 722
c1286ff4
JK
723 *version = cur_cp_version(*cp_block);
724 return 0;
725}
127e670a 726
c1286ff4
JK
727static struct page *validate_checkpoint(struct f2fs_sb_info *sbi,
728 block_t cp_addr, unsigned long long *version)
729{
730 struct page *cp_page_1 = NULL, *cp_page_2 = NULL;
731 struct f2fs_checkpoint *cp_block = NULL;
732 unsigned long long cur_version = 0, pre_version = 0;
733 int err;
127e670a 734
c1286ff4
JK
735 err = get_checkpoint_version(sbi, cp_addr, &cp_block,
736 &cp_page_1, version);
737 if (err)
738 goto invalid_cp1;
739 pre_version = *version;
127e670a 740
c1286ff4
JK
741 cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1;
742 err = get_checkpoint_version(sbi, cp_addr, &cp_block,
743 &cp_page_2, version);
744 if (err)
127e670a 745 goto invalid_cp2;
c1286ff4 746 cur_version = *version;
127e670a
JK
747
748 if (cur_version == pre_version) {
749 *version = cur_version;
750 f2fs_put_page(cp_page_2, 1);
751 return cp_page_1;
752 }
753invalid_cp2:
754 f2fs_put_page(cp_page_2, 1);
755invalid_cp1:
756 f2fs_put_page(cp_page_1, 1);
757 return NULL;
758}
759
760int get_valid_checkpoint(struct f2fs_sb_info *sbi)
761{
762 struct f2fs_checkpoint *cp_block;
763 struct f2fs_super_block *fsb = sbi->raw_super;
764 struct page *cp1, *cp2, *cur_page;
765 unsigned long blk_size = sbi->blocksize;
766 unsigned long long cp1_version = 0, cp2_version = 0;
767 unsigned long long cp_start_blk_no;
55141486 768 unsigned int cp_blks = 1 + __cp_payload(sbi);
1dbe4152
CL
769 block_t cp_blk_no;
770 int i;
127e670a 771
1dbe4152 772 sbi->ckpt = kzalloc(cp_blks * blk_size, GFP_KERNEL);
127e670a
JK
773 if (!sbi->ckpt)
774 return -ENOMEM;
775 /*
776 * Finding out valid cp block involves read both
777 * sets( cp pack1 and cp pack 2)
778 */
779 cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr);
780 cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version);
781
782 /* The second checkpoint pack should start at the next segment */
f9a4e6df
JK
783 cp_start_blk_no += ((unsigned long long)1) <<
784 le32_to_cpu(fsb->log_blocks_per_seg);
127e670a
JK
785 cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version);
786
787 if (cp1 && cp2) {
788 if (ver_after(cp2_version, cp1_version))
789 cur_page = cp2;
790 else
791 cur_page = cp1;
792 } else if (cp1) {
793 cur_page = cp1;
794 } else if (cp2) {
795 cur_page = cp2;
796 } else {
797 goto fail_no_cp;
798 }
799
800 cp_block = (struct f2fs_checkpoint *)page_address(cur_page);
801 memcpy(sbi->ckpt, cp_block, blk_size);
802
c1286ff4
JK
803 /* Sanity checking of checkpoint */
804 if (sanity_check_ckpt(sbi))
80ea4ddb 805 goto free_fail_no_cp;
c1286ff4 806
6b266c3a
JK
807 if (cur_page == cp1)
808 sbi->cur_cp_pack = 1;
809 else
810 sbi->cur_cp_pack = 2;
811
1dbe4152
CL
812 if (cp_blks <= 1)
813 goto done;
814
815 cp_blk_no = le32_to_cpu(fsb->cp_blkaddr);
816 if (cur_page == cp2)
817 cp_blk_no += 1 << le32_to_cpu(fsb->log_blocks_per_seg);
818
819 for (i = 1; i < cp_blks; i++) {
820 void *sit_bitmap_ptr;
821 unsigned char *ckpt = (unsigned char *)sbi->ckpt;
822
823 cur_page = get_meta_page(sbi, cp_blk_no + i);
824 sit_bitmap_ptr = page_address(cur_page);
825 memcpy(ckpt + i * blk_size, sit_bitmap_ptr, blk_size);
826 f2fs_put_page(cur_page, 1);
827 }
828done:
127e670a
JK
829 f2fs_put_page(cp1, 1);
830 f2fs_put_page(cp2, 1);
831 return 0;
832
80ea4ddb
JK
833free_fail_no_cp:
834 f2fs_put_page(cp1, 1);
835 f2fs_put_page(cp2, 1);
127e670a
JK
836fail_no_cp:
837 kfree(sbi->ckpt);
838 return -EINVAL;
839}
840
c1286ff4 841static void __add_dirty_inode(struct inode *inode, enum inode_type type)
127e670a 842{
4081363f 843 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
c1286ff4 844 int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
127e670a 845
c1286ff4
JK
846 if (is_inode_flag_set(inode, flag))
847 return;
2d7b822a 848
c1286ff4 849 set_inode_flag(inode, flag);
13f00235
JK
850 if (!f2fs_is_volatile_file(inode))
851 list_add_tail(&F2FS_I(inode)->dirty_list,
852 &sbi->inode_list[type]);
c1286ff4
JK
853 stat_inc_dirty_inode(sbi, type);
854}
855
856static void __remove_dirty_inode(struct inode *inode, enum inode_type type)
857{
858 int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
859
860 if (get_dirty_pages(inode) || !is_inode_flag_set(inode, flag))
861 return;
862
863 list_del_init(&F2FS_I(inode)->dirty_list);
864 clear_inode_flag(inode, flag);
865 stat_dec_dirty_inode(F2FS_I_SB(inode), type);
5deb8267
JK
866}
867
a7ffdbe2 868void update_dirty_page(struct inode *inode, struct page *page)
5deb8267 869{
4081363f 870 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
c1286ff4 871 enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
5deb8267 872
5ac9f36f
CY
873 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
874 !S_ISLNK(inode->i_mode))
127e670a 875 return;
7bd59381 876
c1286ff4
JK
877 spin_lock(&sbi->inode_lock[type]);
878 if (type != FILE_INODE || test_opt(sbi, DATA_FLUSH))
879 __add_dirty_inode(inode, type);
a7ffdbe2 880 inode_inc_dirty_pages(inode);
c1286ff4 881 spin_unlock(&sbi->inode_lock[type]);
cf0ee0f0 882
a7ffdbe2 883 SetPagePrivate(page);
9e4ded3f 884 f2fs_trace_pid(page);
5deb8267
JK
885}
886
c1286ff4 887void remove_dirty_inode(struct inode *inode)
127e670a 888{
4081363f 889 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
c1286ff4 890 enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
127e670a 891
c1286ff4
JK
892 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
893 !S_ISLNK(inode->i_mode))
127e670a
JK
894 return;
895
c1286ff4 896 if (type == FILE_INODE && !test_opt(sbi, DATA_FLUSH))
3b10b1fd 897 return;
127e670a 898
c1286ff4
JK
899 spin_lock(&sbi->inode_lock[type]);
900 __remove_dirty_inode(inode, type);
901 spin_unlock(&sbi->inode_lock[type]);
74d0b917
JK
902}
903
c1286ff4 904int sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type)
127e670a 905{
ce3b7d80 906 struct list_head *head;
127e670a 907 struct inode *inode;
c1286ff4
JK
908 struct f2fs_inode_info *fi;
909 bool is_dir = (type == DIR_INODE);
13f00235 910 unsigned long ino = 0;
c1286ff4
JK
911
912 trace_f2fs_sync_dirty_inodes_enter(sbi->sb, is_dir,
913 get_pages(sbi, is_dir ?
914 F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
127e670a 915retry:
af41d3ee 916 if (unlikely(f2fs_cp_error(sbi)))
c1286ff4 917 return -EIO;
af41d3ee 918
c1286ff4 919 spin_lock(&sbi->inode_lock[type]);
ce3b7d80 920
c1286ff4 921 head = &sbi->inode_list[type];
127e670a 922 if (list_empty(head)) {
c1286ff4
JK
923 spin_unlock(&sbi->inode_lock[type]);
924 trace_f2fs_sync_dirty_inodes_exit(sbi->sb, is_dir,
925 get_pages(sbi, is_dir ?
926 F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
927 return 0;
127e670a 928 }
7129702a 929 fi = list_first_entry(head, struct f2fs_inode_info, dirty_list);
c1286ff4
JK
930 inode = igrab(&fi->vfs_inode);
931 spin_unlock(&sbi->inode_lock[type]);
127e670a 932 if (inode) {
13f00235
JK
933 unsigned long cur_ino = inode->i_ino;
934
935 if (is_dir)
936 F2FS_I(inode)->cp_task = current;
937
87d6f890 938 filemap_fdatawrite(inode->i_mapping);
13f00235
JK
939
940 if (is_dir)
941 F2FS_I(inode)->cp_task = NULL;
942
127e670a 943 iput(inode);
13f00235
JK
944 /* We need to give cpu to another writers. */
945 if (ino == cur_ino) {
946 congestion_wait(BLK_RW_ASYNC, HZ/50);
947 cond_resched();
948 } else {
949 ino = cur_ino;
950 }
127e670a
JK
951 } else {
952 /*
953 * We should submit bio, since it exists several
954 * wribacking dentry pages in the freeing inode.
955 */
13f00235 956 f2fs_submit_merged_write(sbi, DATA);
7ecebe5e 957 cond_resched();
127e670a
JK
958 }
959 goto retry;
960}
961
c1286ff4
JK
962int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi)
963{
964 struct list_head *head = &sbi->inode_list[DIRTY_META];
965 struct inode *inode;
966 struct f2fs_inode_info *fi;
967 s64 total = get_pages(sbi, F2FS_DIRTY_IMETA);
968
969 while (total--) {
970 if (unlikely(f2fs_cp_error(sbi)))
971 return -EIO;
972
973 spin_lock(&sbi->inode_lock[DIRTY_META]);
974 if (list_empty(head)) {
975 spin_unlock(&sbi->inode_lock[DIRTY_META]);
976 return 0;
977 }
7129702a 978 fi = list_first_entry(head, struct f2fs_inode_info,
c1286ff4
JK
979 gdirty_list);
980 inode = igrab(&fi->vfs_inode);
981 spin_unlock(&sbi->inode_lock[DIRTY_META]);
982 if (inode) {
a15f017e
JK
983 sync_inode_metadata(inode, 0);
984
985 /* it's on eviction */
986 if (is_inode_flag_set(inode, FI_DIRTY_INODE))
987 update_inode_page(inode);
c1286ff4
JK
988 iput(inode);
989 }
990 };
991 return 0;
992}
993
13f00235
JK
994static void __prepare_cp_block(struct f2fs_sb_info *sbi)
995{
996 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
997 struct f2fs_nm_info *nm_i = NM_I(sbi);
998 nid_t last_nid = nm_i->next_scan_nid;
999
1000 next_free_nid(sbi, &last_nid);
1001 ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi));
1002 ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi));
1003 ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi));
1004 ckpt->next_free_nid = cpu_to_le32(last_nid);
1005}
1006
0a8165d7 1007/*
127e670a
JK
1008 * Freeze all the FS-operations for checkpoint.
1009 */
cf779cab 1010static int block_operations(struct f2fs_sb_info *sbi)
127e670a 1011{
127e670a
JK
1012 struct writeback_control wbc = {
1013 .sync_mode = WB_SYNC_ALL,
1014 .nr_to_write = LONG_MAX,
1015 .for_reclaim = 0,
1016 };
c718379b 1017 struct blk_plug plug;
cf779cab 1018 int err = 0;
c718379b
JK
1019
1020 blk_start_plug(&plug);
1021
39936837 1022retry_flush_dents:
e479556b 1023 f2fs_lock_all(sbi);
127e670a 1024 /* write all the dirty dentry pages */
127e670a 1025 if (get_pages(sbi, F2FS_DIRTY_DENTS)) {
e479556b 1026 f2fs_unlock_all(sbi);
c1286ff4
JK
1027 err = sync_dirty_inodes(sbi, DIR_INODE);
1028 if (err)
1029 goto out;
13f00235 1030 cond_resched();
c1286ff4
JK
1031 goto retry_flush_dents;
1032 }
1033
13f00235
JK
1034 /*
1035 * POR: we should ensure that there are no dirty node pages
1036 * until finishing nat/sit flush. inode->i_blocks can be updated.
1037 */
1038 down_write(&sbi->node_change);
1039
c1286ff4 1040 if (get_pages(sbi, F2FS_DIRTY_IMETA)) {
13f00235 1041 up_write(&sbi->node_change);
c1286ff4
JK
1042 f2fs_unlock_all(sbi);
1043 err = f2fs_sync_inode_meta(sbi);
1044 if (err)
cf779cab 1045 goto out;
13f00235 1046 cond_resched();
39936837 1047 goto retry_flush_dents;
127e670a
JK
1048 }
1049
39936837 1050retry_flush_nodes:
b3582c68 1051 down_write(&sbi->node_write);
127e670a
JK
1052
1053 if (get_pages(sbi, F2FS_DIRTY_NODES)) {
b3582c68 1054 up_write(&sbi->node_write);
13f00235 1055 err = sync_node_pages(sbi, &wbc, false, FS_CP_NODE_IO);
c1286ff4 1056 if (err) {
13f00235 1057 up_write(&sbi->node_change);
cf779cab 1058 f2fs_unlock_all(sbi);
cf779cab
JK
1059 goto out;
1060 }
13f00235 1061 cond_resched();
39936837 1062 goto retry_flush_nodes;
127e670a 1063 }
13f00235
JK
1064
1065 /*
1066 * sbi->node_change is used only for AIO write_begin path which produces
1067 * dirty node blocks and some checkpoint values by block allocation.
1068 */
1069 __prepare_cp_block(sbi);
1070 up_write(&sbi->node_change);
cf779cab 1071out:
c718379b 1072 blk_finish_plug(&plug);
cf779cab 1073 return err;
127e670a
JK
1074}
1075
1076static void unblock_operations(struct f2fs_sb_info *sbi)
1077{
b3582c68 1078 up_write(&sbi->node_write);
e479556b 1079 f2fs_unlock_all(sbi);
127e670a
JK
1080}
1081
fb51b5ef
CL
1082static void wait_on_all_pages_writeback(struct f2fs_sb_info *sbi)
1083{
1084 DEFINE_WAIT(wait);
1085
1086 for (;;) {
1087 prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE);
1088
9e266223 1089 if (!get_pages(sbi, F2FS_WB_CP_DATA))
fb51b5ef
CL
1090 break;
1091
c1286ff4 1092 io_schedule_timeout(5*HZ);
fb51b5ef
CL
1093 }
1094 finish_wait(&sbi->cp_wait, &wait);
1095}
1096
c1286ff4
JK
1097static void update_ckpt_flags(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1098{
1099 unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num;
1100 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
13f00235
JK
1101 unsigned long flags;
1102
1103 spin_lock_irqsave(&sbi->cp_lock, flags);
1104
1105 if ((cpc->reason & CP_UMOUNT) &&
1106 le32_to_cpu(ckpt->cp_pack_total_block_count) >
1107 sbi->blocks_per_seg - NM_I(sbi)->nat_bits_blocks)
1108 disable_nat_bits(sbi, false);
c1286ff4 1109
13f00235
JK
1110 if (cpc->reason & CP_TRIMMED)
1111 __set_ckpt_flags(ckpt, CP_TRIMMED_FLAG);
c1286ff4 1112
13f00235 1113 if (cpc->reason & CP_UMOUNT)
c1286ff4
JK
1114 __set_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
1115 else
1116 __clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
1117
13f00235 1118 if (cpc->reason & CP_FASTBOOT)
c1286ff4
JK
1119 __set_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
1120 else
1121 __clear_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
1122
1123 if (orphan_num)
1124 __set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
1125 else
1126 __clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
1127
1128 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK))
1129 __set_ckpt_flags(ckpt, CP_FSCK_FLAG);
1130
1131 /* set this flag to activate crc|cp_ver for recovery */
1132 __set_ckpt_flags(ckpt, CP_CRC_RECOVERY_FLAG);
1133
13f00235 1134 spin_unlock_irqrestore(&sbi->cp_lock, flags);
c1286ff4
JK
1135}
1136
1137static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
127e670a
JK
1138{
1139 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
77041823 1140 struct f2fs_nm_info *nm_i = NM_I(sbi);
13f00235 1141 unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num, flags;
127e670a 1142 block_t start_blk;
127e670a 1143 unsigned int data_sum_blocks, orphan_blocks;
7e586fa0 1144 __u32 crc32 = 0;
127e670a 1145 int i;
55141486 1146 int cp_payload_blks = __cp_payload(sbi);
c1286ff4
JK
1147 struct super_block *sb = sbi->sb;
1148 struct curseg_info *seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
1149 u64 kbytes_written;
127e670a
JK
1150
1151 /* Flush all the NAT/SIT pages */
cf779cab 1152 while (get_pages(sbi, F2FS_DIRTY_META)) {
13f00235 1153 sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO);
cf779cab 1154 if (unlikely(f2fs_cp_error(sbi)))
c1286ff4 1155 return -EIO;
cf779cab 1156 }
127e670a 1157
127e670a
JK
1158 /*
1159 * modify checkpoint
1160 * version number is already updated
1161 */
1162 ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi));
127e670a 1163 ckpt->free_segment_count = cpu_to_le32(free_segments(sbi));
b5b82205 1164 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
127e670a
JK
1165 ckpt->cur_node_segno[i] =
1166 cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE));
1167 ckpt->cur_node_blkoff[i] =
1168 cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE));
1169 ckpt->alloc_type[i + CURSEG_HOT_NODE] =
1170 curseg_alloc_type(sbi, i + CURSEG_HOT_NODE);
1171 }
b5b82205 1172 for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
127e670a
JK
1173 ckpt->cur_data_segno[i] =
1174 cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA));
1175 ckpt->cur_data_blkoff[i] =
1176 cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA));
1177 ckpt->alloc_type[i + CURSEG_HOT_DATA] =
1178 curseg_alloc_type(sbi, i + CURSEG_HOT_DATA);
1179 }
1180
127e670a 1181 /* 2 cp + n data seg summary + orphan inode blocks */
3fa06d7b 1182 data_sum_blocks = npages_for_summary_flush(sbi, false);
13f00235 1183 spin_lock_irqsave(&sbi->cp_lock, flags);
b5b82205 1184 if (data_sum_blocks < NR_CURSEG_DATA_TYPE)
c1286ff4 1185 __set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
127e670a 1186 else
c1286ff4 1187 __clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
13f00235 1188 spin_unlock_irqrestore(&sbi->cp_lock, flags);
127e670a 1189
67298804 1190 orphan_blocks = GET_ORPHAN_BLOCKS(orphan_num);
1dbe4152
CL
1191 ckpt->cp_pack_start_sum = cpu_to_le32(1 + cp_payload_blks +
1192 orphan_blocks);
127e670a 1193
119ee914 1194 if (__remain_node_summaries(cpc->reason))
b5b82205 1195 ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS+
1dbe4152
CL
1196 cp_payload_blks + data_sum_blocks +
1197 orphan_blocks + NR_CURSEG_NODE_TYPE);
119ee914 1198 else
b5b82205 1199 ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS +
1dbe4152
CL
1200 cp_payload_blks + data_sum_blocks +
1201 orphan_blocks);
119ee914 1202
c1286ff4
JK
1203 /* update ckpt flag for checkpoint */
1204 update_ckpt_flags(sbi, cpc);
2ae4c673 1205
127e670a
JK
1206 /* update SIT/NAT bitmap */
1207 get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP));
1208 get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP));
1209
c1286ff4 1210 crc32 = f2fs_crc32(sbi, ckpt, le32_to_cpu(ckpt->checksum_offset));
7e586fa0
JK
1211 *((__le32 *)((unsigned char *)ckpt +
1212 le32_to_cpu(ckpt->checksum_offset)))
127e670a
JK
1213 = cpu_to_le32(crc32);
1214
6b266c3a 1215 start_blk = __start_cp_next_addr(sbi);
127e670a 1216
13f00235
JK
1217 /* write nat bits */
1218 if (enabled_nat_bits(sbi, cpc)) {
1219 __u64 cp_ver = cur_cp_version(ckpt);
1220 block_t blk;
1221
1222 cp_ver |= ((__u64)crc32 << 32);
1223 *(__le64 *)nm_i->nat_bits = cpu_to_le64(cp_ver);
1224
1225 blk = start_blk + sbi->blocks_per_seg - nm_i->nat_bits_blocks;
1226 for (i = 0; i < nm_i->nat_bits_blocks; i++)
1227 update_meta_page(sbi, nm_i->nat_bits +
1228 (i << F2FS_BLKSIZE_BITS), blk + i);
1229
1230 /* Flush all the NAT BITS pages */
1231 while (get_pages(sbi, F2FS_DIRTY_META)) {
1232 sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO);
1233 if (unlikely(f2fs_cp_error(sbi)))
1234 return -EIO;
1235 }
1236 }
1237
a7230d16
JK
1238 /* need to wait for end_io results */
1239 wait_on_all_pages_writeback(sbi);
1240 if (unlikely(f2fs_cp_error(sbi)))
c1286ff4 1241 return -EIO;
a7230d16 1242
127e670a 1243 /* write out checkpoint buffer at block 0 */
381722d2
CY
1244 update_meta_page(sbi, ckpt, start_blk++);
1245
1246 for (i = 1; i < 1 + cp_payload_blks; i++)
1247 update_meta_page(sbi, (char *)ckpt + i * F2FS_BLKSIZE,
1248 start_blk++);
1dbe4152 1249
67298804 1250 if (orphan_num) {
127e670a
JK
1251 write_orphan_inodes(sbi, start_blk);
1252 start_blk += orphan_blocks;
1253 }
1254
1255 write_data_summaries(sbi, start_blk);
1256 start_blk += data_sum_blocks;
c1286ff4
JK
1257
1258 /* Record write statistics in the hot node summary */
1259 kbytes_written = sbi->kbytes_written;
1260 if (sb->s_bdev->bd_part)
1261 kbytes_written += BD_PART_WRITTEN(sbi);
1262
1263 seg_i->journal->info.kbytes_written = cpu_to_le64(kbytes_written);
1264
119ee914 1265 if (__remain_node_summaries(cpc->reason)) {
127e670a
JK
1266 write_node_summaries(sbi, start_blk);
1267 start_blk += NR_CURSEG_NODE_TYPE;
1268 }
1269
1270 /* writeout checkpoint block */
381722d2 1271 update_meta_page(sbi, ckpt, start_blk);
127e670a
JK
1272
1273 /* wait for previous submitted node/meta pages writeback */
fb51b5ef 1274 wait_on_all_pages_writeback(sbi);
127e670a 1275
cf779cab 1276 if (unlikely(f2fs_cp_error(sbi)))
c1286ff4 1277 return -EIO;
cf779cab 1278
c1286ff4
JK
1279 filemap_fdatawait_range(NODE_MAPPING(sbi), 0, LLONG_MAX);
1280 filemap_fdatawait_range(META_MAPPING(sbi), 0, LLONG_MAX);
127e670a
JK
1281
1282 /* update user_block_counts */
1283 sbi->last_valid_block_count = sbi->total_valid_block_count;
c1286ff4 1284 percpu_counter_set(&sbi->alloc_valid_block_count, 0);
127e670a
JK
1285
1286 /* Here, we only have one bio having CP pack */
13f00235 1287 sync_meta_pages(sbi, META_FLUSH, LONG_MAX, FS_CP_META_IO);
127e670a 1288
6a8f8ca5
JK
1289 /* wait for previous submitted meta pages writeback */
1290 wait_on_all_pages_writeback(sbi);
1291
c1286ff4 1292 release_ino_entry(sbi, false);
cf779cab
JK
1293
1294 if (unlikely(f2fs_cp_error(sbi)))
c1286ff4 1295 return -EIO;
cf779cab 1296
caf0047e 1297 clear_sbi_flag(sbi, SBI_IS_DIRTY);
c1286ff4 1298 clear_sbi_flag(sbi, SBI_NEED_CP);
6b266c3a 1299 __set_cp_next_pack(sbi);
c1286ff4
JK
1300
1301 /*
1302 * redirty superblock if metadata like node page or inode cache is
1303 * updated during writing checkpoint.
1304 */
1305 if (get_pages(sbi, F2FS_DIRTY_NODES) ||
1306 get_pages(sbi, F2FS_DIRTY_IMETA))
1307 set_sbi_flag(sbi, SBI_IS_DIRTY);
1308
1309 f2fs_bug_on(sbi, get_pages(sbi, F2FS_DIRTY_DENTS));
1310
1311 return 0;
127e670a
JK
1312}
1313
0a8165d7 1314/*
e1c42045 1315 * We guarantee that this checkpoint procedure will not fail.
127e670a 1316 */
c1286ff4 1317int write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
127e670a
JK
1318{
1319 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1320 unsigned long long ckpt_ver;
c1286ff4 1321 int err = 0;
127e670a 1322
43727527 1323 mutex_lock(&sbi->cp_mutex);
8501017e 1324
caf0047e 1325 if (!is_sbi_flag_set(sbi, SBI_IS_DIRTY) &&
13f00235
JK
1326 ((cpc->reason & CP_FASTBOOT) || (cpc->reason & CP_SYNC) ||
1327 ((cpc->reason & CP_DISCARD) && !sbi->discard_blks)))
8501017e 1328 goto out;
c1286ff4
JK
1329 if (unlikely(f2fs_cp_error(sbi))) {
1330 err = -EIO;
cf779cab 1331 goto out;
c1286ff4
JK
1332 }
1333 if (f2fs_readonly(sbi->sb)) {
1334 err = -EROFS;
11504a8e 1335 goto out;
c1286ff4 1336 }
2bda542d
WL
1337
1338 trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "start block_ops");
1339
c1286ff4
JK
1340 err = block_operations(sbi);
1341 if (err)
cf779cab 1342 goto out;
127e670a 1343
75ab4cb8 1344 trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish block_ops");
2af4bd6c 1345
13f00235 1346 f2fs_flush_merged_writes(sbi);
c1286ff4
JK
1347
1348 /* this is the case of multiple fstrims without any changes */
13f00235 1349 if (cpc->reason & CP_DISCARD) {
556f5ba3
JK
1350 if (!exist_trim_candidates(sbi, cpc)) {
1351 unblock_operations(sbi);
1352 goto out;
1353 }
1354
d051ccbd
JK
1355 if (NM_I(sbi)->dirty_nat_cnt == 0 &&
1356 SIT_I(sbi)->dirty_sentries == 0 &&
1357 prefree_segments(sbi) == 0) {
1358 flush_sit_entries(sbi, cpc);
1359 clear_prefree_segments(sbi, cpc);
1360 unblock_operations(sbi);
1361 goto out;
1362 }
c1286ff4 1363 }
127e670a
JK
1364
1365 /*
1366 * update checkpoint pack index
1367 * Increase the version number so that
1368 * SIT entries and seg summaries are written at correct place
1369 */
d71b5564 1370 ckpt_ver = cur_cp_version(ckpt);
127e670a
JK
1371 ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver);
1372
1373 /* write cached NAT/SIT entries to NAT/SIT area */
13f00235 1374 flush_nat_entries(sbi, cpc);
4b2fecc8 1375 flush_sit_entries(sbi, cpc);
127e670a 1376
127e670a 1377 /* unlock all the fs_lock[] in do_checkpoint() */
c1286ff4 1378 err = do_checkpoint(sbi, cpc);
132263dd 1379 if (err)
ab38818b 1380 release_discard_addrs(sbi);
132263dd 1381 else
ab38818b 1382 clear_prefree_segments(sbi, cpc);
127e670a
JK
1383
1384 unblock_operations(sbi);
942e0be6 1385 stat_inc_cp_count(sbi->stat_info);
10027551 1386
13f00235 1387 if (cpc->reason & CP_RECOVERY)
10027551
JK
1388 f2fs_msg(sbi->sb, KERN_NOTICE,
1389 "checkpoint: version = %llx", ckpt_ver);
60b99b48
JK
1390
1391 /* do checkpoint periodically */
c1286ff4
JK
1392 f2fs_update_time(sbi, CP_TIME);
1393 trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish checkpoint");
8501017e
JK
1394out:
1395 mutex_unlock(&sbi->cp_mutex);
c1286ff4 1396 return err;
127e670a
JK
1397}
1398
6451e041 1399void init_ino_entry_info(struct f2fs_sb_info *sbi)
127e670a 1400{
6451e041
JK
1401 int i;
1402
1403 for (i = 0; i < MAX_INO_ENTRY; i++) {
67298804
CY
1404 struct inode_management *im = &sbi->im[i];
1405
1406 INIT_RADIX_TREE(&im->ino_root, GFP_ATOMIC);
1407 spin_lock_init(&im->ino_lock);
1408 INIT_LIST_HEAD(&im->ino_list);
1409 im->ino_num = 0;
6451e041
JK
1410 }
1411
b5b82205 1412 sbi->max_orphans = (sbi->blocks_per_seg - F2FS_CP_PACKS -
14b42817
WL
1413 NR_CURSEG_TYPE - __cp_payload(sbi)) *
1414 F2FS_ORPHANS_PER_BLOCK;
127e670a
JK
1415}
1416
6e6093a8 1417int __init create_checkpoint_caches(void)
127e670a 1418{
6451e041
JK
1419 ino_entry_slab = f2fs_kmem_cache_create("f2fs_ino_entry",
1420 sizeof(struct ino_entry));
1421 if (!ino_entry_slab)
127e670a 1422 return -ENOMEM;
06292073
CY
1423 inode_entry_slab = f2fs_kmem_cache_create("f2fs_inode_entry",
1424 sizeof(struct inode_entry));
6bacf52f 1425 if (!inode_entry_slab) {
6451e041 1426 kmem_cache_destroy(ino_entry_slab);
127e670a
JK
1427 return -ENOMEM;
1428 }
1429 return 0;
1430}
1431
1432void destroy_checkpoint_caches(void)
1433{
6451e041 1434 kmem_cache_destroy(ino_entry_slab);
127e670a
JK
1435 kmem_cache_destroy(inode_entry_slab);
1436}