blob: 4b9429cfd999214b76c0a3b113f1dd7cd9b0cf67 [file] [log] [blame]
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001/*
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"
23#include "trace.h"
24#include <trace/events/f2fs.h>
25
26static struct kmem_cache *ino_entry_slab;
27struct kmem_cache *inode_entry_slab;
28
29/*
30 * We guarantee no failure on the returned page.
31 */
32struct page *grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
33{
34 struct address_space *mapping = META_MAPPING(sbi);
35 struct page *page = NULL;
36repeat:
37 page = grab_cache_page(mapping, index);
38 if (!page) {
39 cond_resched();
40 goto repeat;
41 }
Jaegeuk Kim3e0b2f42016-01-20 23:43:51 +080042 f2fs_wait_on_page_writeback(page, META, true);
Jaegeuk Kim315f4552015-11-29 09:25:08 -080043 SetPageUptodate(page);
44 return page;
45}
46
47/*
48 * We guarantee no failure on the returned page.
49 */
50static struct page *__get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index,
51 bool is_meta)
52{
53 struct address_space *mapping = META_MAPPING(sbi);
54 struct page *page;
55 struct f2fs_io_info fio = {
56 .sbi = sbi,
57 .type = META,
58 .rw = READ_SYNC | REQ_META | REQ_PRIO,
59 .blk_addr = index,
60 .encrypted_page = NULL,
61 };
62
63 if (unlikely(!is_meta))
64 fio.rw &= ~REQ_META;
65repeat:
66 page = grab_cache_page(mapping, index);
67 if (!page) {
68 cond_resched();
69 goto repeat;
70 }
71 if (PageUptodate(page))
72 goto out;
73
74 fio.page = page;
75
76 if (f2fs_submit_page_bio(&fio)) {
77 f2fs_put_page(page, 1);
78 goto repeat;
79 }
80
81 lock_page(page);
82 if (unlikely(page->mapping != mapping)) {
83 f2fs_put_page(page, 1);
84 goto repeat;
85 }
86
87 /*
88 * if there is any IO error when accessing device, make our filesystem
89 * readonly and make sure do not write checkpoint with non-uptodate
90 * meta page.
91 */
92 if (unlikely(!PageUptodate(page)))
93 f2fs_stop_checkpoint(sbi);
94out:
95 mark_page_accessed(page);
96 return page;
97}
98
99struct page *get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
100{
101 return __get_meta_page(sbi, index, true);
102}
103
104/* for POR only */
105struct page *get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index)
106{
107 return __get_meta_page(sbi, index, false);
108}
109
110bool is_valid_blkaddr(struct f2fs_sb_info *sbi, block_t blkaddr, int type)
111{
112 switch (type) {
113 case META_NAT:
114 break;
115 case META_SIT:
116 if (unlikely(blkaddr >= SIT_BLK_CNT(sbi)))
117 return false;
118 break;
119 case META_SSA:
120 if (unlikely(blkaddr >= MAIN_BLKADDR(sbi) ||
121 blkaddr < SM_I(sbi)->ssa_blkaddr))
122 return false;
123 break;
124 case META_CP:
125 if (unlikely(blkaddr >= SIT_I(sbi)->sit_base_addr ||
126 blkaddr < __start_cp_addr(sbi)))
127 return false;
128 break;
129 case META_POR:
130 if (unlikely(blkaddr >= MAX_BLKADDR(sbi) ||
131 blkaddr < MAIN_BLKADDR(sbi)))
132 return false;
133 break;
134 default:
135 BUG();
136 }
137
138 return true;
139}
140
141/*
142 * Readahead CP/NAT/SIT/SSA pages
143 */
144int ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
145 int type, bool sync)
146{
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800147 struct page *page;
148 block_t blkno = start;
149 struct f2fs_io_info fio = {
150 .sbi = sbi,
151 .type = META,
152 .rw = sync ? (READ_SYNC | REQ_META | REQ_PRIO) : READA,
153 .encrypted_page = NULL,
154 };
Chao Yu8be7c502016-02-14 18:54:33 +0800155 struct blk_plug plug;
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800156
157 if (unlikely(type == META_POR))
158 fio.rw &= ~REQ_META;
159
Chao Yu8be7c502016-02-14 18:54:33 +0800160 blk_start_plug(&plug);
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800161 for (; nrpages-- > 0; blkno++) {
162
163 if (!is_valid_blkaddr(sbi, blkno, type))
164 goto out;
165
166 switch (type) {
167 case META_NAT:
168 if (unlikely(blkno >=
169 NAT_BLOCK_OFFSET(NM_I(sbi)->max_nid)))
170 blkno = 0;
171 /* get nat block addr */
172 fio.blk_addr = current_nat_addr(sbi,
173 blkno * NAT_ENTRY_PER_BLOCK);
174 break;
175 case META_SIT:
176 /* get sit block addr */
177 fio.blk_addr = current_sit_addr(sbi,
178 blkno * SIT_ENTRY_PER_BLOCK);
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800179 break;
180 case META_SSA:
181 case META_CP:
182 case META_POR:
183 fio.blk_addr = blkno;
184 break;
185 default:
186 BUG();
187 }
188
189 page = grab_cache_page(META_MAPPING(sbi), fio.blk_addr);
190 if (!page)
191 continue;
192 if (PageUptodate(page)) {
193 f2fs_put_page(page, 1);
194 continue;
195 }
196
197 fio.page = page;
198 f2fs_submit_page_mbio(&fio);
199 f2fs_put_page(page, 0);
200 }
201out:
202 f2fs_submit_merged_bio(sbi, META, READ);
Chao Yu8be7c502016-02-14 18:54:33 +0800203 blk_finish_plug(&plug);
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800204 return blkno - start;
205}
206
207void ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index)
208{
209 struct page *page;
210 bool readahead = false;
211
212 page = find_get_page(META_MAPPING(sbi), index);
213 if (!page || (page && !PageUptodate(page)))
214 readahead = true;
215 f2fs_put_page(page, 0);
216
217 if (readahead)
218 ra_meta_pages(sbi, index, MAX_BIO_BLOCKS(sbi), META_POR, true);
219}
220
221static int f2fs_write_meta_page(struct page *page,
222 struct writeback_control *wbc)
223{
224 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
225
226 trace_f2fs_writepage(page, META);
227
228 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
229 goto redirty_out;
230 if (wbc->for_reclaim && page->index < GET_SUM_BLOCK(sbi, 0))
231 goto redirty_out;
232 if (unlikely(f2fs_cp_error(sbi)))
233 goto redirty_out;
234
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800235 write_meta_page(sbi, page);
236 dec_page_count(sbi, F2FS_DIRTY_META);
Chao Yufd2bbb12016-01-18 18:28:11 +0800237
238 if (wbc->for_reclaim)
239 f2fs_submit_merged_bio_cond(sbi, NULL, page, 0, META, WRITE);
240
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800241 unlock_page(page);
242
Chao Yufd2bbb12016-01-18 18:28:11 +0800243 if (unlikely(f2fs_cp_error(sbi)))
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800244 f2fs_submit_merged_bio(sbi, META, WRITE);
Chao Yufd2bbb12016-01-18 18:28:11 +0800245
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800246 return 0;
247
248redirty_out:
249 redirty_page_for_writepage(wbc, page);
250 return AOP_WRITEPAGE_ACTIVATE;
251}
252
253static int f2fs_write_meta_pages(struct address_space *mapping,
254 struct writeback_control *wbc)
255{
256 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
257 long diff, written;
258
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800259 /* collect a number of dirty meta pages and write together */
260 if (wbc->for_kupdate ||
261 get_pages(sbi, F2FS_DIRTY_META) < nr_pages_to_skip(sbi, META))
262 goto skip_write;
263
Yunlei He4dcaaa22016-02-04 16:14:00 +0800264 trace_f2fs_writepages(mapping->host, wbc, META);
265
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800266 /* if mounting is failed, skip writing node pages */
267 mutex_lock(&sbi->cp_mutex);
268 diff = nr_pages_to_write(sbi, META, wbc);
269 written = sync_meta_pages(sbi, META, wbc->nr_to_write);
270 mutex_unlock(&sbi->cp_mutex);
271 wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff);
272 return 0;
273
274skip_write:
275 wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_META);
Yunlei He4dcaaa22016-02-04 16:14:00 +0800276 trace_f2fs_writepages(mapping->host, wbc, META);
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800277 return 0;
278}
279
280long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
281 long nr_to_write)
282{
283 struct address_space *mapping = META_MAPPING(sbi);
284 pgoff_t index = 0, end = LONG_MAX, prev = LONG_MAX;
285 struct pagevec pvec;
286 long nwritten = 0;
287 struct writeback_control wbc = {
288 .for_reclaim = 0,
289 };
Chao Yu8be7c502016-02-14 18:54:33 +0800290 struct blk_plug plug;
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800291
292 pagevec_init(&pvec, 0);
293
Chao Yu8be7c502016-02-14 18:54:33 +0800294 blk_start_plug(&plug);
295
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800296 while (index <= end) {
297 int i, nr_pages;
298 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
299 PAGECACHE_TAG_DIRTY,
300 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
301 if (unlikely(nr_pages == 0))
302 break;
303
304 for (i = 0; i < nr_pages; i++) {
305 struct page *page = pvec.pages[i];
306
307 if (prev == LONG_MAX)
308 prev = page->index - 1;
309 if (nr_to_write != LONG_MAX && page->index != prev + 1) {
310 pagevec_release(&pvec);
311 goto stop;
312 }
313
314 lock_page(page);
315
316 if (unlikely(page->mapping != mapping)) {
317continue_unlock:
318 unlock_page(page);
319 continue;
320 }
321 if (!PageDirty(page)) {
322 /* someone wrote it for us */
323 goto continue_unlock;
324 }
325
Jaegeuk Kimdeed9ad2016-01-28 11:48:52 -0800326 f2fs_wait_on_page_writeback(page, META, true);
327
328 BUG_ON(PageWriteback(page));
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800329 if (!clear_page_dirty_for_io(page))
330 goto continue_unlock;
331
332 if (mapping->a_ops->writepage(page, &wbc)) {
333 unlock_page(page);
334 break;
335 }
336 nwritten++;
337 prev = page->index;
338 if (unlikely(nwritten >= nr_to_write))
339 break;
340 }
341 pagevec_release(&pvec);
342 cond_resched();
343 }
344stop:
345 if (nwritten)
346 f2fs_submit_merged_bio(sbi, type, WRITE);
347
Chao Yu8be7c502016-02-14 18:54:33 +0800348 blk_finish_plug(&plug);
349
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800350 return nwritten;
351}
352
353static int f2fs_set_meta_page_dirty(struct page *page)
354{
355 trace_f2fs_set_page_dirty(page, META);
356
357 SetPageUptodate(page);
358 if (!PageDirty(page)) {
359 __set_page_dirty_nobuffers(page);
360 inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_META);
361 SetPagePrivate(page);
362 f2fs_trace_pid(page);
363 return 1;
364 }
365 return 0;
366}
367
368const struct address_space_operations f2fs_meta_aops = {
369 .writepage = f2fs_write_meta_page,
370 .writepages = f2fs_write_meta_pages,
371 .set_page_dirty = f2fs_set_meta_page_dirty,
372 .invalidatepage = f2fs_invalidate_page,
373 .releasepage = f2fs_release_page,
374};
375
376static void __add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
377{
378 struct inode_management *im = &sbi->im[type];
379 struct ino_entry *e, *tmp;
380
381 tmp = f2fs_kmem_cache_alloc(ino_entry_slab, GFP_NOFS);
382retry:
383 radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
384
385 spin_lock(&im->ino_lock);
386 e = radix_tree_lookup(&im->ino_root, ino);
387 if (!e) {
388 e = tmp;
389 if (radix_tree_insert(&im->ino_root, ino, e)) {
390 spin_unlock(&im->ino_lock);
391 radix_tree_preload_end();
392 goto retry;
393 }
394 memset(e, 0, sizeof(struct ino_entry));
395 e->ino = ino;
396
397 list_add_tail(&e->list, &im->ino_list);
398 if (type != ORPHAN_INO)
399 im->ino_num++;
400 }
401 spin_unlock(&im->ino_lock);
402 radix_tree_preload_end();
403
404 if (e != tmp)
405 kmem_cache_free(ino_entry_slab, tmp);
406}
407
408static void __remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
409{
410 struct inode_management *im = &sbi->im[type];
411 struct ino_entry *e;
412
413 spin_lock(&im->ino_lock);
414 e = radix_tree_lookup(&im->ino_root, ino);
415 if (e) {
416 list_del(&e->list);
417 radix_tree_delete(&im->ino_root, ino);
418 im->ino_num--;
419 spin_unlock(&im->ino_lock);
420 kmem_cache_free(ino_entry_slab, e);
421 return;
422 }
423 spin_unlock(&im->ino_lock);
424}
425
Chao Yudbc32c22015-12-15 13:29:47 +0800426void add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800427{
428 /* add new dirty ino entry into list */
429 __add_ino_entry(sbi, ino, type);
430}
431
Chao Yudbc32c22015-12-15 13:29:47 +0800432void remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800433{
434 /* remove dirty ino entry from list */
435 __remove_ino_entry(sbi, ino, type);
436}
437
438/* mode should be APPEND_INO or UPDATE_INO */
439bool exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode)
440{
441 struct inode_management *im = &sbi->im[mode];
442 struct ino_entry *e;
443
444 spin_lock(&im->ino_lock);
445 e = radix_tree_lookup(&im->ino_root, ino);
446 spin_unlock(&im->ino_lock);
447 return e ? true : false;
448}
449
Chao Yudbc32c22015-12-15 13:29:47 +0800450void release_ino_entry(struct f2fs_sb_info *sbi)
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800451{
452 struct ino_entry *e, *tmp;
453 int i;
454
455 for (i = APPEND_INO; i <= UPDATE_INO; i++) {
456 struct inode_management *im = &sbi->im[i];
457
458 spin_lock(&im->ino_lock);
459 list_for_each_entry_safe(e, tmp, &im->ino_list, list) {
460 list_del(&e->list);
461 radix_tree_delete(&im->ino_root, e->ino);
462 kmem_cache_free(ino_entry_slab, e);
463 im->ino_num--;
464 }
465 spin_unlock(&im->ino_lock);
466 }
467}
468
469int acquire_orphan_inode(struct f2fs_sb_info *sbi)
470{
471 struct inode_management *im = &sbi->im[ORPHAN_INO];
472 int err = 0;
473
474 spin_lock(&im->ino_lock);
475 if (unlikely(im->ino_num >= sbi->max_orphans))
476 err = -ENOSPC;
477 else
478 im->ino_num++;
479 spin_unlock(&im->ino_lock);
480
481 return err;
482}
483
484void release_orphan_inode(struct f2fs_sb_info *sbi)
485{
486 struct inode_management *im = &sbi->im[ORPHAN_INO];
487
488 spin_lock(&im->ino_lock);
489 f2fs_bug_on(sbi, im->ino_num == 0);
490 im->ino_num--;
491 spin_unlock(&im->ino_lock);
492}
493
494void add_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
495{
496 /* add new orphan ino entry into list */
497 __add_ino_entry(sbi, ino, ORPHAN_INO);
498}
499
500void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
501{
502 /* remove orphan entry from orphan list */
503 __remove_ino_entry(sbi, ino, ORPHAN_INO);
504}
505
506static int recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
507{
508 struct inode *inode;
509
510 inode = f2fs_iget(sbi->sb, ino);
511 if (IS_ERR(inode)) {
512 /*
513 * there should be a bug that we can't find the entry
514 * to orphan inode.
515 */
516 f2fs_bug_on(sbi, PTR_ERR(inode) == -ENOENT);
517 return PTR_ERR(inode);
518 }
519
520 clear_nlink(inode);
521
522 /* truncate all the data during iput */
523 iput(inode);
524 return 0;
525}
526
527int recover_orphan_inodes(struct f2fs_sb_info *sbi)
528{
529 block_t start_blk, orphan_blocks, i, j;
530 int err;
531
532 if (!is_set_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG))
533 return 0;
534
535 start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi);
536 orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi);
537
538 ra_meta_pages(sbi, start_blk, orphan_blocks, META_CP, true);
539
540 for (i = 0; i < orphan_blocks; i++) {
541 struct page *page = get_meta_page(sbi, start_blk + i);
542 struct f2fs_orphan_block *orphan_blk;
543
544 orphan_blk = (struct f2fs_orphan_block *)page_address(page);
545 for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) {
546 nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
547 err = recover_orphan_inode(sbi, ino);
548 if (err) {
549 f2fs_put_page(page, 1);
550 return err;
551 }
552 }
553 f2fs_put_page(page, 1);
554 }
555 /* clear Orphan Flag */
556 clear_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG);
557 return 0;
558}
559
560static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk)
561{
562 struct list_head *head;
563 struct f2fs_orphan_block *orphan_blk = NULL;
564 unsigned int nentries = 0;
565 unsigned short index = 1;
566 unsigned short orphan_blocks;
567 struct page *page = NULL;
568 struct ino_entry *orphan = NULL;
569 struct inode_management *im = &sbi->im[ORPHAN_INO];
570
571 orphan_blocks = GET_ORPHAN_BLOCKS(im->ino_num);
572
573 /*
574 * we don't need to do spin_lock(&im->ino_lock) here, since all the
575 * orphan inode operations are covered under f2fs_lock_op().
576 * And, spin_lock should be avoided due to page operations below.
577 */
578 head = &im->ino_list;
579
580 /* loop for each orphan inode entry and write them in Jornal block */
581 list_for_each_entry(orphan, head, list) {
582 if (!page) {
583 page = grab_meta_page(sbi, start_blk++);
584 orphan_blk =
585 (struct f2fs_orphan_block *)page_address(page);
586 memset(orphan_blk, 0, sizeof(*orphan_blk));
587 }
588
589 orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino);
590
591 if (nentries == F2FS_ORPHANS_PER_BLOCK) {
592 /*
593 * an orphan block is full of 1020 entries,
594 * then we need to flush current orphan blocks
595 * and bring another one in memory
596 */
597 orphan_blk->blk_addr = cpu_to_le16(index);
598 orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
599 orphan_blk->entry_count = cpu_to_le32(nentries);
600 set_page_dirty(page);
601 f2fs_put_page(page, 1);
602 index++;
603 nentries = 0;
604 page = NULL;
605 }
606 }
607
608 if (page) {
609 orphan_blk->blk_addr = cpu_to_le16(index);
610 orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
611 orphan_blk->entry_count = cpu_to_le32(nentries);
612 set_page_dirty(page);
613 f2fs_put_page(page, 1);
614 }
615}
616
617static struct page *validate_checkpoint(struct f2fs_sb_info *sbi,
618 block_t cp_addr, unsigned long long *version)
619{
620 struct page *cp_page_1, *cp_page_2 = NULL;
621 unsigned long blk_size = sbi->blocksize;
622 struct f2fs_checkpoint *cp_block;
623 unsigned long long cur_version = 0, pre_version = 0;
624 size_t crc_offset;
625 __u32 crc = 0;
626
627 /* Read the 1st cp block in this CP pack */
628 cp_page_1 = get_meta_page(sbi, cp_addr);
629
630 /* get the version number */
631 cp_block = (struct f2fs_checkpoint *)page_address(cp_page_1);
632 crc_offset = le32_to_cpu(cp_block->checksum_offset);
633 if (crc_offset >= blk_size)
634 goto invalid_cp1;
635
636 crc = le32_to_cpu(*((__le32 *)((unsigned char *)cp_block + crc_offset)));
637 if (!f2fs_crc_valid(crc, cp_block, crc_offset))
638 goto invalid_cp1;
639
640 pre_version = cur_cp_version(cp_block);
641
642 /* Read the 2nd cp block in this CP pack */
643 cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1;
644 cp_page_2 = get_meta_page(sbi, cp_addr);
645
646 cp_block = (struct f2fs_checkpoint *)page_address(cp_page_2);
647 crc_offset = le32_to_cpu(cp_block->checksum_offset);
648 if (crc_offset >= blk_size)
649 goto invalid_cp2;
650
651 crc = le32_to_cpu(*((__le32 *)((unsigned char *)cp_block + crc_offset)));
652 if (!f2fs_crc_valid(crc, cp_block, crc_offset))
653 goto invalid_cp2;
654
655 cur_version = cur_cp_version(cp_block);
656
657 if (cur_version == pre_version) {
658 *version = cur_version;
659 f2fs_put_page(cp_page_2, 1);
660 return cp_page_1;
661 }
662invalid_cp2:
663 f2fs_put_page(cp_page_2, 1);
664invalid_cp1:
665 f2fs_put_page(cp_page_1, 1);
666 return NULL;
667}
668
669int get_valid_checkpoint(struct f2fs_sb_info *sbi)
670{
671 struct f2fs_checkpoint *cp_block;
672 struct f2fs_super_block *fsb = sbi->raw_super;
673 struct page *cp1, *cp2, *cur_page;
674 unsigned long blk_size = sbi->blocksize;
675 unsigned long long cp1_version = 0, cp2_version = 0;
676 unsigned long long cp_start_blk_no;
677 unsigned int cp_blks = 1 + __cp_payload(sbi);
678 block_t cp_blk_no;
679 int i;
680
681 sbi->ckpt = kzalloc(cp_blks * blk_size, GFP_KERNEL);
682 if (!sbi->ckpt)
683 return -ENOMEM;
684 /*
685 * Finding out valid cp block involves read both
686 * sets( cp pack1 and cp pack 2)
687 */
688 cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr);
689 cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version);
690
691 /* The second checkpoint pack should start at the next segment */
692 cp_start_blk_no += ((unsigned long long)1) <<
693 le32_to_cpu(fsb->log_blocks_per_seg);
694 cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version);
695
696 if (cp1 && cp2) {
697 if (ver_after(cp2_version, cp1_version))
698 cur_page = cp2;
699 else
700 cur_page = cp1;
701 } else if (cp1) {
702 cur_page = cp1;
703 } else if (cp2) {
704 cur_page = cp2;
705 } else {
706 goto fail_no_cp;
707 }
708
709 cp_block = (struct f2fs_checkpoint *)page_address(cur_page);
710 memcpy(sbi->ckpt, cp_block, blk_size);
711
712 if (cp_blks <= 1)
713 goto done;
714
715 cp_blk_no = le32_to_cpu(fsb->cp_blkaddr);
716 if (cur_page == cp2)
717 cp_blk_no += 1 << le32_to_cpu(fsb->log_blocks_per_seg);
718
719 for (i = 1; i < cp_blks; i++) {
720 void *sit_bitmap_ptr;
721 unsigned char *ckpt = (unsigned char *)sbi->ckpt;
722
723 cur_page = get_meta_page(sbi, cp_blk_no + i);
724 sit_bitmap_ptr = page_address(cur_page);
725 memcpy(ckpt + i * blk_size, sit_bitmap_ptr, blk_size);
726 f2fs_put_page(cur_page, 1);
727 }
728done:
729 f2fs_put_page(cp1, 1);
730 f2fs_put_page(cp2, 1);
731 return 0;
732
733fail_no_cp:
734 kfree(sbi->ckpt);
735 return -EINVAL;
736}
737
Chao Yufb1825c2015-12-16 13:09:20 +0800738static void __add_dirty_inode(struct inode *inode, enum inode_type type)
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800739{
740 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yubc2a2142015-12-15 13:30:45 +0800741 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yufb1825c2015-12-16 13:09:20 +0800742 int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800743
Chao Yufb1825c2015-12-16 13:09:20 +0800744 if (is_inode_flag_set(fi, flag))
Chao Yubc2a2142015-12-15 13:30:45 +0800745 return;
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800746
Chao Yufb1825c2015-12-16 13:09:20 +0800747 set_inode_flag(fi, flag);
748 list_add_tail(&fi->dirty_list, &sbi->inode_list[type]);
Chao Yu0b90d982015-12-17 17:14:44 +0800749 stat_inc_dirty_inode(sbi, type);
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800750}
751
Chao Yufb1825c2015-12-16 13:09:20 +0800752static void __remove_dirty_inode(struct inode *inode, enum inode_type type)
Chao Yu0c591542015-12-15 13:31:40 +0800753{
Chao Yu0c591542015-12-15 13:31:40 +0800754 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yufb1825c2015-12-16 13:09:20 +0800755 int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
Chao Yu0c591542015-12-15 13:31:40 +0800756
757 if (get_dirty_pages(inode) ||
Chao Yufb1825c2015-12-16 13:09:20 +0800758 !is_inode_flag_set(F2FS_I(inode), flag))
Chao Yu0c591542015-12-15 13:31:40 +0800759 return;
760
761 list_del_init(&fi->dirty_list);
Chao Yufb1825c2015-12-16 13:09:20 +0800762 clear_inode_flag(fi, flag);
Chao Yu0b90d982015-12-17 17:14:44 +0800763 stat_dec_dirty_inode(F2FS_I_SB(inode), type);
Chao Yu0c591542015-12-15 13:31:40 +0800764}
765
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800766void update_dirty_page(struct inode *inode, struct page *page)
767{
768 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yufb1825c2015-12-16 13:09:20 +0800769 enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800770
771 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
772 !S_ISLNK(inode->i_mode))
773 return;
774
Chao Yufb1825c2015-12-16 13:09:20 +0800775 spin_lock(&sbi->inode_lock[type]);
776 __add_dirty_inode(inode, type);
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800777 inode_inc_dirty_pages(inode);
Chao Yufb1825c2015-12-16 13:09:20 +0800778 spin_unlock(&sbi->inode_lock[type]);
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800779
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800780 SetPagePrivate(page);
781 f2fs_trace_pid(page);
782}
783
784void add_dirty_dir_inode(struct inode *inode)
785{
786 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800787
Chao Yufb1825c2015-12-16 13:09:20 +0800788 spin_lock(&sbi->inode_lock[DIR_INODE]);
789 __add_dirty_inode(inode, DIR_INODE);
790 spin_unlock(&sbi->inode_lock[DIR_INODE]);
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800791}
792
Chao Yufb1825c2015-12-16 13:09:20 +0800793void remove_dirty_inode(struct inode *inode)
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800794{
795 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yubc2a2142015-12-15 13:30:45 +0800796 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yufb1825c2015-12-16 13:09:20 +0800797 enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800798
Chao Yufb1825c2015-12-16 13:09:20 +0800799 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
800 !S_ISLNK(inode->i_mode))
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800801 return;
802
Chao Yufb1825c2015-12-16 13:09:20 +0800803 spin_lock(&sbi->inode_lock[type]);
804 __remove_dirty_inode(inode, type);
805 spin_unlock(&sbi->inode_lock[type]);
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800806
807 /* Only from the recovery routine */
Chao Yubc2a2142015-12-15 13:30:45 +0800808 if (is_inode_flag_set(fi, FI_DELAY_IPUT)) {
809 clear_inode_flag(fi, FI_DELAY_IPUT);
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800810 iput(inode);
811 }
812}
813
Chao Yu167a3162015-12-24 18:04:56 +0800814int sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type)
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800815{
816 struct list_head *head;
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800817 struct inode *inode;
Chao Yubc2a2142015-12-15 13:30:45 +0800818 struct f2fs_inode_info *fi;
Chao Yu74729a92015-12-17 17:17:16 +0800819 bool is_dir = (type == DIR_INODE);
820
821 trace_f2fs_sync_dirty_inodes_enter(sbi->sb, is_dir,
822 get_pages(sbi, is_dir ?
823 F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800824retry:
825 if (unlikely(f2fs_cp_error(sbi)))
Chao Yu167a3162015-12-24 18:04:56 +0800826 return -EIO;
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800827
Chao Yufb1825c2015-12-16 13:09:20 +0800828 spin_lock(&sbi->inode_lock[type]);
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800829
Chao Yufb1825c2015-12-16 13:09:20 +0800830 head = &sbi->inode_list[type];
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800831 if (list_empty(head)) {
Chao Yufb1825c2015-12-16 13:09:20 +0800832 spin_unlock(&sbi->inode_lock[type]);
Chao Yu74729a92015-12-17 17:17:16 +0800833 trace_f2fs_sync_dirty_inodes_exit(sbi->sb, is_dir,
834 get_pages(sbi, is_dir ?
835 F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
Chao Yu167a3162015-12-24 18:04:56 +0800836 return 0;
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800837 }
Chao Yubc2a2142015-12-15 13:30:45 +0800838 fi = list_entry(head->next, struct f2fs_inode_info, dirty_list);
839 inode = igrab(&fi->vfs_inode);
Chao Yufb1825c2015-12-16 13:09:20 +0800840 spin_unlock(&sbi->inode_lock[type]);
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800841 if (inode) {
842 filemap_fdatawrite(inode->i_mapping);
843 iput(inode);
844 } else {
845 /*
846 * We should submit bio, since it exists several
847 * wribacking dentry pages in the freeing inode.
848 */
849 f2fs_submit_merged_bio(sbi, DATA, WRITE);
850 cond_resched();
851 }
852 goto retry;
853}
854
855/*
856 * Freeze all the FS-operations for checkpoint.
857 */
858static int block_operations(struct f2fs_sb_info *sbi)
859{
860 struct writeback_control wbc = {
861 .sync_mode = WB_SYNC_ALL,
862 .nr_to_write = LONG_MAX,
863 .for_reclaim = 0,
864 };
865 struct blk_plug plug;
866 int err = 0;
867
868 blk_start_plug(&plug);
869
870retry_flush_dents:
871 f2fs_lock_all(sbi);
872 /* write all the dirty dentry pages */
873 if (get_pages(sbi, F2FS_DIRTY_DENTS)) {
874 f2fs_unlock_all(sbi);
Chao Yu167a3162015-12-24 18:04:56 +0800875 err = sync_dirty_inodes(sbi, DIR_INODE);
876 if (err)
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800877 goto out;
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800878 goto retry_flush_dents;
879 }
880
881 /*
882 * POR: we should ensure that there are no dirty node pages
883 * until finishing nat/sit flush.
884 */
885retry_flush_nodes:
886 down_write(&sbi->node_write);
887
888 if (get_pages(sbi, F2FS_DIRTY_NODES)) {
889 up_write(&sbi->node_write);
Chao Yu167a3162015-12-24 18:04:56 +0800890 err = sync_node_pages(sbi, 0, &wbc);
891 if (err) {
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800892 f2fs_unlock_all(sbi);
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800893 goto out;
894 }
895 goto retry_flush_nodes;
896 }
897out:
898 blk_finish_plug(&plug);
899 return err;
900}
901
902static void unblock_operations(struct f2fs_sb_info *sbi)
903{
904 up_write(&sbi->node_write);
905 f2fs_unlock_all(sbi);
906}
907
908static void wait_on_all_pages_writeback(struct f2fs_sb_info *sbi)
909{
910 DEFINE_WAIT(wait);
911
912 for (;;) {
913 prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE);
914
915 if (!get_pages(sbi, F2FS_WRITEBACK))
916 break;
917
918 io_schedule();
919 }
920 finish_wait(&sbi->cp_wait, &wait);
921}
922
Chao Yub962dc12015-12-23 17:50:30 +0800923static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800924{
925 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
926 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
927 struct f2fs_nm_info *nm_i = NM_I(sbi);
928 unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num;
929 nid_t last_nid = nm_i->next_scan_nid;
930 block_t start_blk;
931 unsigned int data_sum_blocks, orphan_blocks;
932 __u32 crc32 = 0;
933 int i;
934 int cp_payload_blks = __cp_payload(sbi);
935 block_t discard_blk = NEXT_FREE_BLKADDR(sbi, curseg);
936 bool invalidate = false;
Shuoran Liufa8a1d72016-01-27 09:57:30 +0800937 struct super_block *sb = sbi->sb;
938 struct curseg_info *seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
939 u64 kbytes_written;
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800940
941 /*
942 * This avoids to conduct wrong roll-forward operations and uses
943 * metapages, so should be called prior to sync_meta_pages below.
944 */
945 if (discard_next_dnode(sbi, discard_blk))
946 invalidate = true;
947
948 /* Flush all the NAT/SIT pages */
949 while (get_pages(sbi, F2FS_DIRTY_META)) {
950 sync_meta_pages(sbi, META, LONG_MAX);
951 if (unlikely(f2fs_cp_error(sbi)))
Chao Yub962dc12015-12-23 17:50:30 +0800952 return -EIO;
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800953 }
954
955 next_free_nid(sbi, &last_nid);
956
957 /*
958 * modify checkpoint
959 * version number is already updated
960 */
961 ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi));
962 ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi));
963 ckpt->free_segment_count = cpu_to_le32(free_segments(sbi));
964 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
965 ckpt->cur_node_segno[i] =
966 cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE));
967 ckpt->cur_node_blkoff[i] =
968 cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE));
969 ckpt->alloc_type[i + CURSEG_HOT_NODE] =
970 curseg_alloc_type(sbi, i + CURSEG_HOT_NODE);
971 }
972 for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
973 ckpt->cur_data_segno[i] =
974 cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA));
975 ckpt->cur_data_blkoff[i] =
976 cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA));
977 ckpt->alloc_type[i + CURSEG_HOT_DATA] =
978 curseg_alloc_type(sbi, i + CURSEG_HOT_DATA);
979 }
980
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 /* 2 cp + n data seg summary + orphan inode blocks */
986 data_sum_blocks = npages_for_summary_flush(sbi, false);
987 if (data_sum_blocks < NR_CURSEG_DATA_TYPE)
988 set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
989 else
990 clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
991
992 orphan_blocks = GET_ORPHAN_BLOCKS(orphan_num);
993 ckpt->cp_pack_start_sum = cpu_to_le32(1 + cp_payload_blks +
994 orphan_blocks);
995
996 if (__remain_node_summaries(cpc->reason))
997 ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS+
998 cp_payload_blks + data_sum_blocks +
999 orphan_blocks + NR_CURSEG_NODE_TYPE);
1000 else
1001 ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS +
1002 cp_payload_blks + data_sum_blocks +
1003 orphan_blocks);
1004
1005 if (cpc->reason == CP_UMOUNT)
1006 set_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
1007 else
1008 clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
1009
1010 if (cpc->reason == CP_FASTBOOT)
1011 set_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
1012 else
1013 clear_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
1014
1015 if (orphan_num)
1016 set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
1017 else
1018 clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
1019
1020 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK))
1021 set_ckpt_flags(ckpt, CP_FSCK_FLAG);
1022
1023 /* update SIT/NAT bitmap */
1024 get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP));
1025 get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP));
1026
1027 crc32 = f2fs_crc32(ckpt, le32_to_cpu(ckpt->checksum_offset));
1028 *((__le32 *)((unsigned char *)ckpt +
1029 le32_to_cpu(ckpt->checksum_offset)))
1030 = cpu_to_le32(crc32);
1031
1032 start_blk = __start_cp_addr(sbi);
1033
1034 /* need to wait for end_io results */
1035 wait_on_all_pages_writeback(sbi);
1036 if (unlikely(f2fs_cp_error(sbi)))
Chao Yub962dc12015-12-23 17:50:30 +08001037 return -EIO;
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001038
1039 /* write out checkpoint buffer at block 0 */
1040 update_meta_page(sbi, ckpt, start_blk++);
1041
1042 for (i = 1; i < 1 + cp_payload_blks; i++)
1043 update_meta_page(sbi, (char *)ckpt + i * F2FS_BLKSIZE,
1044 start_blk++);
1045
1046 if (orphan_num) {
1047 write_orphan_inodes(sbi, start_blk);
1048 start_blk += orphan_blocks;
1049 }
1050
1051 write_data_summaries(sbi, start_blk);
1052 start_blk += data_sum_blocks;
Shuoran Liufa8a1d72016-01-27 09:57:30 +08001053
1054 /* Record write statistics in the hot node summary */
1055 kbytes_written = sbi->kbytes_written;
1056 if (sb->s_bdev->bd_part)
1057 kbytes_written += BD_PART_WRITTEN(sbi);
1058
Chao Yud59981d2016-02-14 18:50:40 +08001059 seg_i->sum_blk->journal.info.kbytes_written = cpu_to_le64(kbytes_written);
Shuoran Liufa8a1d72016-01-27 09:57:30 +08001060
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001061 if (__remain_node_summaries(cpc->reason)) {
1062 write_node_summaries(sbi, start_blk);
1063 start_blk += NR_CURSEG_NODE_TYPE;
1064 }
1065
1066 /* writeout checkpoint block */
1067 update_meta_page(sbi, ckpt, start_blk);
1068
1069 /* wait for previous submitted node/meta pages writeback */
1070 wait_on_all_pages_writeback(sbi);
1071
1072 if (unlikely(f2fs_cp_error(sbi)))
Chao Yub962dc12015-12-23 17:50:30 +08001073 return -EIO;
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001074
1075 filemap_fdatawait_range(NODE_MAPPING(sbi), 0, LONG_MAX);
1076 filemap_fdatawait_range(META_MAPPING(sbi), 0, LONG_MAX);
1077
1078 /* update user_block_counts */
1079 sbi->last_valid_block_count = sbi->total_valid_block_count;
1080 sbi->alloc_valid_block_count = 0;
1081
1082 /* Here, we only have one bio having CP pack */
1083 sync_meta_pages(sbi, META_FLUSH, LONG_MAX);
1084
1085 /* wait for previous submitted meta pages writeback */
1086 wait_on_all_pages_writeback(sbi);
1087
1088 /*
1089 * invalidate meta page which is used temporarily for zeroing out
1090 * block at the end of warm node chain.
1091 */
1092 if (invalidate)
1093 invalidate_mapping_pages(META_MAPPING(sbi), discard_blk,
1094 discard_blk);
1095
Chao Yudbc32c22015-12-15 13:29:47 +08001096 release_ino_entry(sbi);
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001097
1098 if (unlikely(f2fs_cp_error(sbi)))
Chao Yub962dc12015-12-23 17:50:30 +08001099 return -EIO;
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001100
1101 clear_prefree_segments(sbi, cpc);
1102 clear_sbi_flag(sbi, SBI_IS_DIRTY);
Chao Yub962dc12015-12-23 17:50:30 +08001103
1104 return 0;
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001105}
1106
1107/*
1108 * We guarantee that this checkpoint procedure will not fail.
1109 */
Chao Yub962dc12015-12-23 17:50:30 +08001110int write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001111{
1112 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1113 unsigned long long ckpt_ver;
Chao Yub962dc12015-12-23 17:50:30 +08001114 int err = 0;
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001115
1116 mutex_lock(&sbi->cp_mutex);
1117
1118 if (!is_sbi_flag_set(sbi, SBI_IS_DIRTY) &&
1119 (cpc->reason == CP_FASTBOOT || cpc->reason == CP_SYNC ||
1120 (cpc->reason == CP_DISCARD && !sbi->discard_blks)))
1121 goto out;
Chao Yub962dc12015-12-23 17:50:30 +08001122 if (unlikely(f2fs_cp_error(sbi))) {
1123 err = -EIO;
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001124 goto out;
Chao Yub962dc12015-12-23 17:50:30 +08001125 }
1126 if (f2fs_readonly(sbi->sb)) {
1127 err = -EROFS;
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001128 goto out;
Chao Yub962dc12015-12-23 17:50:30 +08001129 }
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001130
1131 trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "start block_ops");
1132
Chao Yub962dc12015-12-23 17:50:30 +08001133 err = block_operations(sbi);
1134 if (err)
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001135 goto out;
1136
1137 trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish block_ops");
1138
1139 f2fs_submit_merged_bio(sbi, DATA, WRITE);
1140 f2fs_submit_merged_bio(sbi, NODE, WRITE);
1141 f2fs_submit_merged_bio(sbi, META, WRITE);
1142
1143 /*
1144 * update checkpoint pack index
1145 * Increase the version number so that
1146 * SIT entries and seg summaries are written at correct place
1147 */
1148 ckpt_ver = cur_cp_version(ckpt);
1149 ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver);
1150
1151 /* write cached NAT/SIT entries to NAT/SIT area */
1152 flush_nat_entries(sbi);
1153 flush_sit_entries(sbi, cpc);
1154
1155 /* unlock all the fs_lock[] in do_checkpoint() */
Chao Yub962dc12015-12-23 17:50:30 +08001156 err = do_checkpoint(sbi, cpc);
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001157
1158 unblock_operations(sbi);
1159 stat_inc_cp_count(sbi->stat_info);
1160
1161 if (cpc->reason == CP_RECOVERY)
1162 f2fs_msg(sbi->sb, KERN_NOTICE,
1163 "checkpoint: version = %llx", ckpt_ver);
1164
1165 /* do checkpoint periodically */
Jaegeuk Kimdedfbf82016-01-08 15:51:50 -08001166 f2fs_update_time(sbi, CP_TIME);
Jaegeuk Kim1f167ac2015-12-15 16:07:14 -08001167 trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish checkpoint");
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001168out:
1169 mutex_unlock(&sbi->cp_mutex);
Chao Yub962dc12015-12-23 17:50:30 +08001170 return err;
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001171}
1172
1173void init_ino_entry_info(struct f2fs_sb_info *sbi)
1174{
1175 int i;
1176
1177 for (i = 0; i < MAX_INO_ENTRY; i++) {
1178 struct inode_management *im = &sbi->im[i];
1179
1180 INIT_RADIX_TREE(&im->ino_root, GFP_ATOMIC);
1181 spin_lock_init(&im->ino_lock);
1182 INIT_LIST_HEAD(&im->ino_list);
1183 im->ino_num = 0;
1184 }
1185
1186 sbi->max_orphans = (sbi->blocks_per_seg - F2FS_CP_PACKS -
1187 NR_CURSEG_TYPE - __cp_payload(sbi)) *
1188 F2FS_ORPHANS_PER_BLOCK;
1189}
1190
1191int __init create_checkpoint_caches(void)
1192{
1193 ino_entry_slab = f2fs_kmem_cache_create("f2fs_ino_entry",
1194 sizeof(struct ino_entry));
1195 if (!ino_entry_slab)
1196 return -ENOMEM;
1197 inode_entry_slab = f2fs_kmem_cache_create("f2fs_inode_entry",
1198 sizeof(struct inode_entry));
1199 if (!inode_entry_slab) {
1200 kmem_cache_destroy(ino_entry_slab);
1201 return -ENOMEM;
1202 }
1203 return 0;
1204}
1205
1206void destroy_checkpoint_caches(void)
1207{
1208 kmem_cache_destroy(ino_entry_slab);
1209 kmem_cache_destroy(inode_entry_slab);
1210}