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