blob: 01aae09bc62ff5440e926427136291d0c12ebb44 [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{
147 block_t prev_blk_addr = 0;
148 struct page *page;
149 block_t blkno = start;
150 struct f2fs_io_info fio = {
151 .sbi = sbi,
152 .type = META,
153 .rw = sync ? (READ_SYNC | REQ_META | REQ_PRIO) : READA,
154 .encrypted_page = NULL,
155 };
156
157 if (unlikely(type == META_POR))
158 fio.rw &= ~REQ_META;
159
160 for (; nrpages-- > 0; blkno++) {
161
162 if (!is_valid_blkaddr(sbi, blkno, type))
163 goto out;
164
165 switch (type) {
166 case META_NAT:
167 if (unlikely(blkno >=
168 NAT_BLOCK_OFFSET(NM_I(sbi)->max_nid)))
169 blkno = 0;
170 /* get nat block addr */
171 fio.blk_addr = current_nat_addr(sbi,
172 blkno * NAT_ENTRY_PER_BLOCK);
173 break;
174 case META_SIT:
175 /* get sit block addr */
176 fio.blk_addr = current_sit_addr(sbi,
177 blkno * SIT_ENTRY_PER_BLOCK);
178 if (blkno != start && prev_blk_addr + 1 != fio.blk_addr)
179 goto out;
180 prev_blk_addr = fio.blk_addr;
181 break;
182 case META_SSA:
183 case META_CP:
184 case META_POR:
185 fio.blk_addr = blkno;
186 break;
187 default:
188 BUG();
189 }
190
191 page = grab_cache_page(META_MAPPING(sbi), fio.blk_addr);
192 if (!page)
193 continue;
194 if (PageUptodate(page)) {
195 f2fs_put_page(page, 1);
196 continue;
197 }
198
199 fio.page = page;
200 f2fs_submit_page_mbio(&fio);
201 f2fs_put_page(page, 0);
202 }
203out:
204 f2fs_submit_merged_bio(sbi, META, READ);
205 return blkno - start;
206}
207
208void ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index)
209{
210 struct page *page;
211 bool readahead = false;
212
213 page = find_get_page(META_MAPPING(sbi), index);
214 if (!page || (page && !PageUptodate(page)))
215 readahead = true;
216 f2fs_put_page(page, 0);
217
218 if (readahead)
219 ra_meta_pages(sbi, index, MAX_BIO_BLOCKS(sbi), META_POR, true);
220}
221
222static int f2fs_write_meta_page(struct page *page,
223 struct writeback_control *wbc)
224{
225 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
226
227 trace_f2fs_writepage(page, META);
228
229 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
230 goto redirty_out;
231 if (wbc->for_reclaim && page->index < GET_SUM_BLOCK(sbi, 0))
232 goto redirty_out;
233 if (unlikely(f2fs_cp_error(sbi)))
234 goto redirty_out;
235
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800236 write_meta_page(sbi, page);
237 dec_page_count(sbi, F2FS_DIRTY_META);
Chao Yufd2bbb12016-01-18 18:28:11 +0800238
239 if (wbc->for_reclaim)
240 f2fs_submit_merged_bio_cond(sbi, NULL, page, 0, META, WRITE);
241
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800242 unlock_page(page);
243
Chao Yufd2bbb12016-01-18 18:28:11 +0800244 if (unlikely(f2fs_cp_error(sbi)))
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800245 f2fs_submit_merged_bio(sbi, META, WRITE);
Chao Yufd2bbb12016-01-18 18:28:11 +0800246
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800247 return 0;
248
249redirty_out:
250 redirty_page_for_writepage(wbc, page);
251 return AOP_WRITEPAGE_ACTIVATE;
252}
253
254static int f2fs_write_meta_pages(struct address_space *mapping,
255 struct writeback_control *wbc)
256{
257 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
258 long diff, written;
259
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800260 /* collect a number of dirty meta pages and write together */
261 if (wbc->for_kupdate ||
262 get_pages(sbi, F2FS_DIRTY_META) < nr_pages_to_skip(sbi, META))
263 goto skip_write;
264
Yunlei He4dcaaa22016-02-04 16:14:00 +0800265 trace_f2fs_writepages(mapping->host, wbc, META);
266
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800267 /* if mounting is failed, skip writing node pages */
268 mutex_lock(&sbi->cp_mutex);
269 diff = nr_pages_to_write(sbi, META, wbc);
270 written = sync_meta_pages(sbi, META, wbc->nr_to_write);
271 mutex_unlock(&sbi->cp_mutex);
272 wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff);
273 return 0;
274
275skip_write:
276 wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_META);
Yunlei He4dcaaa22016-02-04 16:14:00 +0800277 trace_f2fs_writepages(mapping->host, wbc, META);
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800278 return 0;
279}
280
281long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
282 long nr_to_write)
283{
284 struct address_space *mapping = META_MAPPING(sbi);
285 pgoff_t index = 0, end = LONG_MAX, prev = LONG_MAX;
286 struct pagevec pvec;
287 long nwritten = 0;
288 struct writeback_control wbc = {
289 .for_reclaim = 0,
290 };
291
292 pagevec_init(&pvec, 0);
293
294 while (index <= end) {
295 int i, nr_pages;
296 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
297 PAGECACHE_TAG_DIRTY,
298 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
299 if (unlikely(nr_pages == 0))
300 break;
301
302 for (i = 0; i < nr_pages; i++) {
303 struct page *page = pvec.pages[i];
304
305 if (prev == LONG_MAX)
306 prev = page->index - 1;
307 if (nr_to_write != LONG_MAX && page->index != prev + 1) {
308 pagevec_release(&pvec);
309 goto stop;
310 }
311
312 lock_page(page);
313
314 if (unlikely(page->mapping != mapping)) {
315continue_unlock:
316 unlock_page(page);
317 continue;
318 }
319 if (!PageDirty(page)) {
320 /* someone wrote it for us */
321 goto continue_unlock;
322 }
323
Jaegeuk Kimdeed9ad2016-01-28 11:48:52 -0800324 f2fs_wait_on_page_writeback(page, META, true);
325
326 BUG_ON(PageWriteback(page));
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800327 if (!clear_page_dirty_for_io(page))
328 goto continue_unlock;
329
330 if (mapping->a_ops->writepage(page, &wbc)) {
331 unlock_page(page);
332 break;
333 }
334 nwritten++;
335 prev = page->index;
336 if (unlikely(nwritten >= nr_to_write))
337 break;
338 }
339 pagevec_release(&pvec);
340 cond_resched();
341 }
342stop:
343 if (nwritten)
344 f2fs_submit_merged_bio(sbi, type, WRITE);
345
346 return nwritten;
347}
348
349static int f2fs_set_meta_page_dirty(struct page *page)
350{
351 trace_f2fs_set_page_dirty(page, META);
352
353 SetPageUptodate(page);
354 if (!PageDirty(page)) {
355 __set_page_dirty_nobuffers(page);
356 inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_META);
357 SetPagePrivate(page);
358 f2fs_trace_pid(page);
359 return 1;
360 }
361 return 0;
362}
363
364const struct address_space_operations f2fs_meta_aops = {
365 .writepage = f2fs_write_meta_page,
366 .writepages = f2fs_write_meta_pages,
367 .set_page_dirty = f2fs_set_meta_page_dirty,
368 .invalidatepage = f2fs_invalidate_page,
369 .releasepage = f2fs_release_page,
370};
371
372static void __add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
373{
374 struct inode_management *im = &sbi->im[type];
375 struct ino_entry *e, *tmp;
376
377 tmp = f2fs_kmem_cache_alloc(ino_entry_slab, GFP_NOFS);
378retry:
379 radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
380
381 spin_lock(&im->ino_lock);
382 e = radix_tree_lookup(&im->ino_root, ino);
383 if (!e) {
384 e = tmp;
385 if (radix_tree_insert(&im->ino_root, ino, e)) {
386 spin_unlock(&im->ino_lock);
387 radix_tree_preload_end();
388 goto retry;
389 }
390 memset(e, 0, sizeof(struct ino_entry));
391 e->ino = ino;
392
393 list_add_tail(&e->list, &im->ino_list);
394 if (type != ORPHAN_INO)
395 im->ino_num++;
396 }
397 spin_unlock(&im->ino_lock);
398 radix_tree_preload_end();
399
400 if (e != tmp)
401 kmem_cache_free(ino_entry_slab, tmp);
402}
403
404static void __remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
405{
406 struct inode_management *im = &sbi->im[type];
407 struct ino_entry *e;
408
409 spin_lock(&im->ino_lock);
410 e = radix_tree_lookup(&im->ino_root, ino);
411 if (e) {
412 list_del(&e->list);
413 radix_tree_delete(&im->ino_root, ino);
414 im->ino_num--;
415 spin_unlock(&im->ino_lock);
416 kmem_cache_free(ino_entry_slab, e);
417 return;
418 }
419 spin_unlock(&im->ino_lock);
420}
421
Chao Yudbc32c22015-12-15 13:29:47 +0800422void add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800423{
424 /* add new dirty ino entry into list */
425 __add_ino_entry(sbi, ino, type);
426}
427
Chao Yudbc32c22015-12-15 13:29:47 +0800428void remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800429{
430 /* remove dirty ino entry from list */
431 __remove_ino_entry(sbi, ino, type);
432}
433
434/* mode should be APPEND_INO or UPDATE_INO */
435bool exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode)
436{
437 struct inode_management *im = &sbi->im[mode];
438 struct ino_entry *e;
439
440 spin_lock(&im->ino_lock);
441 e = radix_tree_lookup(&im->ino_root, ino);
442 spin_unlock(&im->ino_lock);
443 return e ? true : false;
444}
445
Chao Yudbc32c22015-12-15 13:29:47 +0800446void release_ino_entry(struct f2fs_sb_info *sbi)
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800447{
448 struct ino_entry *e, *tmp;
449 int i;
450
451 for (i = APPEND_INO; i <= UPDATE_INO; i++) {
452 struct inode_management *im = &sbi->im[i];
453
454 spin_lock(&im->ino_lock);
455 list_for_each_entry_safe(e, tmp, &im->ino_list, list) {
456 list_del(&e->list);
457 radix_tree_delete(&im->ino_root, e->ino);
458 kmem_cache_free(ino_entry_slab, e);
459 im->ino_num--;
460 }
461 spin_unlock(&im->ino_lock);
462 }
463}
464
465int acquire_orphan_inode(struct f2fs_sb_info *sbi)
466{
467 struct inode_management *im = &sbi->im[ORPHAN_INO];
468 int err = 0;
469
470 spin_lock(&im->ino_lock);
471 if (unlikely(im->ino_num >= sbi->max_orphans))
472 err = -ENOSPC;
473 else
474 im->ino_num++;
475 spin_unlock(&im->ino_lock);
476
477 return err;
478}
479
480void release_orphan_inode(struct f2fs_sb_info *sbi)
481{
482 struct inode_management *im = &sbi->im[ORPHAN_INO];
483
484 spin_lock(&im->ino_lock);
485 f2fs_bug_on(sbi, im->ino_num == 0);
486 im->ino_num--;
487 spin_unlock(&im->ino_lock);
488}
489
490void add_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
491{
492 /* add new orphan ino entry into list */
493 __add_ino_entry(sbi, ino, ORPHAN_INO);
494}
495
496void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
497{
498 /* remove orphan entry from orphan list */
499 __remove_ino_entry(sbi, ino, ORPHAN_INO);
500}
501
502static int recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
503{
504 struct inode *inode;
505
506 inode = f2fs_iget(sbi->sb, ino);
507 if (IS_ERR(inode)) {
508 /*
509 * there should be a bug that we can't find the entry
510 * to orphan inode.
511 */
512 f2fs_bug_on(sbi, PTR_ERR(inode) == -ENOENT);
513 return PTR_ERR(inode);
514 }
515
516 clear_nlink(inode);
517
518 /* truncate all the data during iput */
519 iput(inode);
520 return 0;
521}
522
523int recover_orphan_inodes(struct f2fs_sb_info *sbi)
524{
525 block_t start_blk, orphan_blocks, i, j;
526 int err;
527
528 if (!is_set_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG))
529 return 0;
530
531 start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi);
532 orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi);
533
534 ra_meta_pages(sbi, start_blk, orphan_blocks, META_CP, true);
535
536 for (i = 0; i < orphan_blocks; i++) {
537 struct page *page = get_meta_page(sbi, start_blk + i);
538 struct f2fs_orphan_block *orphan_blk;
539
540 orphan_blk = (struct f2fs_orphan_block *)page_address(page);
541 for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) {
542 nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
543 err = recover_orphan_inode(sbi, ino);
544 if (err) {
545 f2fs_put_page(page, 1);
546 return err;
547 }
548 }
549 f2fs_put_page(page, 1);
550 }
551 /* clear Orphan Flag */
552 clear_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG);
553 return 0;
554}
555
556static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk)
557{
558 struct list_head *head;
559 struct f2fs_orphan_block *orphan_blk = NULL;
560 unsigned int nentries = 0;
561 unsigned short index = 1;
562 unsigned short orphan_blocks;
563 struct page *page = NULL;
564 struct ino_entry *orphan = NULL;
565 struct inode_management *im = &sbi->im[ORPHAN_INO];
566
567 orphan_blocks = GET_ORPHAN_BLOCKS(im->ino_num);
568
569 /*
570 * we don't need to do spin_lock(&im->ino_lock) here, since all the
571 * orphan inode operations are covered under f2fs_lock_op().
572 * And, spin_lock should be avoided due to page operations below.
573 */
574 head = &im->ino_list;
575
576 /* loop for each orphan inode entry and write them in Jornal block */
577 list_for_each_entry(orphan, head, list) {
578 if (!page) {
579 page = grab_meta_page(sbi, start_blk++);
580 orphan_blk =
581 (struct f2fs_orphan_block *)page_address(page);
582 memset(orphan_blk, 0, sizeof(*orphan_blk));
583 }
584
585 orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino);
586
587 if (nentries == F2FS_ORPHANS_PER_BLOCK) {
588 /*
589 * an orphan block is full of 1020 entries,
590 * then we need to flush current orphan blocks
591 * and bring another one in memory
592 */
593 orphan_blk->blk_addr = cpu_to_le16(index);
594 orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
595 orphan_blk->entry_count = cpu_to_le32(nentries);
596 set_page_dirty(page);
597 f2fs_put_page(page, 1);
598 index++;
599 nentries = 0;
600 page = NULL;
601 }
602 }
603
604 if (page) {
605 orphan_blk->blk_addr = cpu_to_le16(index);
606 orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
607 orphan_blk->entry_count = cpu_to_le32(nentries);
608 set_page_dirty(page);
609 f2fs_put_page(page, 1);
610 }
611}
612
613static struct page *validate_checkpoint(struct f2fs_sb_info *sbi,
614 block_t cp_addr, unsigned long long *version)
615{
616 struct page *cp_page_1, *cp_page_2 = NULL;
617 unsigned long blk_size = sbi->blocksize;
618 struct f2fs_checkpoint *cp_block;
619 unsigned long long cur_version = 0, pre_version = 0;
620 size_t crc_offset;
621 __u32 crc = 0;
622
623 /* Read the 1st cp block in this CP pack */
624 cp_page_1 = get_meta_page(sbi, cp_addr);
625
626 /* get the version number */
627 cp_block = (struct f2fs_checkpoint *)page_address(cp_page_1);
628 crc_offset = le32_to_cpu(cp_block->checksum_offset);
629 if (crc_offset >= blk_size)
630 goto invalid_cp1;
631
632 crc = le32_to_cpu(*((__le32 *)((unsigned char *)cp_block + crc_offset)));
633 if (!f2fs_crc_valid(crc, cp_block, crc_offset))
634 goto invalid_cp1;
635
636 pre_version = cur_cp_version(cp_block);
637
638 /* Read the 2nd cp block in this CP pack */
639 cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1;
640 cp_page_2 = get_meta_page(sbi, cp_addr);
641
642 cp_block = (struct f2fs_checkpoint *)page_address(cp_page_2);
643 crc_offset = le32_to_cpu(cp_block->checksum_offset);
644 if (crc_offset >= blk_size)
645 goto invalid_cp2;
646
647 crc = le32_to_cpu(*((__le32 *)((unsigned char *)cp_block + crc_offset)));
648 if (!f2fs_crc_valid(crc, cp_block, crc_offset))
649 goto invalid_cp2;
650
651 cur_version = cur_cp_version(cp_block);
652
653 if (cur_version == pre_version) {
654 *version = cur_version;
655 f2fs_put_page(cp_page_2, 1);
656 return cp_page_1;
657 }
658invalid_cp2:
659 f2fs_put_page(cp_page_2, 1);
660invalid_cp1:
661 f2fs_put_page(cp_page_1, 1);
662 return NULL;
663}
664
665int get_valid_checkpoint(struct f2fs_sb_info *sbi)
666{
667 struct f2fs_checkpoint *cp_block;
668 struct f2fs_super_block *fsb = sbi->raw_super;
669 struct page *cp1, *cp2, *cur_page;
670 unsigned long blk_size = sbi->blocksize;
671 unsigned long long cp1_version = 0, cp2_version = 0;
672 unsigned long long cp_start_blk_no;
673 unsigned int cp_blks = 1 + __cp_payload(sbi);
674 block_t cp_blk_no;
675 int i;
676
677 sbi->ckpt = kzalloc(cp_blks * blk_size, GFP_KERNEL);
678 if (!sbi->ckpt)
679 return -ENOMEM;
680 /*
681 * Finding out valid cp block involves read both
682 * sets( cp pack1 and cp pack 2)
683 */
684 cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr);
685 cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version);
686
687 /* The second checkpoint pack should start at the next segment */
688 cp_start_blk_no += ((unsigned long long)1) <<
689 le32_to_cpu(fsb->log_blocks_per_seg);
690 cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version);
691
692 if (cp1 && cp2) {
693 if (ver_after(cp2_version, cp1_version))
694 cur_page = cp2;
695 else
696 cur_page = cp1;
697 } else if (cp1) {
698 cur_page = cp1;
699 } else if (cp2) {
700 cur_page = cp2;
701 } else {
702 goto fail_no_cp;
703 }
704
705 cp_block = (struct f2fs_checkpoint *)page_address(cur_page);
706 memcpy(sbi->ckpt, cp_block, blk_size);
707
708 if (cp_blks <= 1)
709 goto done;
710
711 cp_blk_no = le32_to_cpu(fsb->cp_blkaddr);
712 if (cur_page == cp2)
713 cp_blk_no += 1 << le32_to_cpu(fsb->log_blocks_per_seg);
714
715 for (i = 1; i < cp_blks; i++) {
716 void *sit_bitmap_ptr;
717 unsigned char *ckpt = (unsigned char *)sbi->ckpt;
718
719 cur_page = get_meta_page(sbi, cp_blk_no + i);
720 sit_bitmap_ptr = page_address(cur_page);
721 memcpy(ckpt + i * blk_size, sit_bitmap_ptr, blk_size);
722 f2fs_put_page(cur_page, 1);
723 }
724done:
725 f2fs_put_page(cp1, 1);
726 f2fs_put_page(cp2, 1);
727 return 0;
728
729fail_no_cp:
730 kfree(sbi->ckpt);
731 return -EINVAL;
732}
733
Chao Yufb1825c2015-12-16 13:09:20 +0800734static void __add_dirty_inode(struct inode *inode, enum inode_type type)
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800735{
736 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yubc2a2142015-12-15 13:30:45 +0800737 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yufb1825c2015-12-16 13:09:20 +0800738 int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800739
Chao Yufb1825c2015-12-16 13:09:20 +0800740 if (is_inode_flag_set(fi, flag))
Chao Yubc2a2142015-12-15 13:30:45 +0800741 return;
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800742
Chao Yufb1825c2015-12-16 13:09:20 +0800743 set_inode_flag(fi, flag);
744 list_add_tail(&fi->dirty_list, &sbi->inode_list[type]);
Chao Yu0b90d982015-12-17 17:14:44 +0800745 stat_inc_dirty_inode(sbi, type);
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800746}
747
Chao Yufb1825c2015-12-16 13:09:20 +0800748static void __remove_dirty_inode(struct inode *inode, enum inode_type type)
Chao Yu0c591542015-12-15 13:31:40 +0800749{
Chao Yu0c591542015-12-15 13:31:40 +0800750 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yufb1825c2015-12-16 13:09:20 +0800751 int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
Chao Yu0c591542015-12-15 13:31:40 +0800752
753 if (get_dirty_pages(inode) ||
Chao Yufb1825c2015-12-16 13:09:20 +0800754 !is_inode_flag_set(F2FS_I(inode), flag))
Chao Yu0c591542015-12-15 13:31:40 +0800755 return;
756
757 list_del_init(&fi->dirty_list);
Chao Yufb1825c2015-12-16 13:09:20 +0800758 clear_inode_flag(fi, flag);
Chao Yu0b90d982015-12-17 17:14:44 +0800759 stat_dec_dirty_inode(F2FS_I_SB(inode), type);
Chao Yu0c591542015-12-15 13:31:40 +0800760}
761
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800762void update_dirty_page(struct inode *inode, struct page *page)
763{
764 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yufb1825c2015-12-16 13:09:20 +0800765 enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800766
767 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
768 !S_ISLNK(inode->i_mode))
769 return;
770
Chao Yufb1825c2015-12-16 13:09:20 +0800771 spin_lock(&sbi->inode_lock[type]);
772 __add_dirty_inode(inode, type);
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800773 inode_inc_dirty_pages(inode);
Chao Yufb1825c2015-12-16 13:09:20 +0800774 spin_unlock(&sbi->inode_lock[type]);
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800775
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800776 SetPagePrivate(page);
777 f2fs_trace_pid(page);
778}
779
780void add_dirty_dir_inode(struct inode *inode)
781{
782 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800783
Chao Yufb1825c2015-12-16 13:09:20 +0800784 spin_lock(&sbi->inode_lock[DIR_INODE]);
785 __add_dirty_inode(inode, DIR_INODE);
786 spin_unlock(&sbi->inode_lock[DIR_INODE]);
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800787}
788
Chao Yufb1825c2015-12-16 13:09:20 +0800789void remove_dirty_inode(struct inode *inode)
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800790{
791 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yubc2a2142015-12-15 13:30:45 +0800792 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yufb1825c2015-12-16 13:09:20 +0800793 enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800794
Chao Yufb1825c2015-12-16 13:09:20 +0800795 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
796 !S_ISLNK(inode->i_mode))
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800797 return;
798
Chao Yufb1825c2015-12-16 13:09:20 +0800799 spin_lock(&sbi->inode_lock[type]);
800 __remove_dirty_inode(inode, type);
801 spin_unlock(&sbi->inode_lock[type]);
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800802
803 /* Only from the recovery routine */
Chao Yubc2a2142015-12-15 13:30:45 +0800804 if (is_inode_flag_set(fi, FI_DELAY_IPUT)) {
805 clear_inode_flag(fi, FI_DELAY_IPUT);
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800806 iput(inode);
807 }
808}
809
Chao Yu167a3162015-12-24 18:04:56 +0800810int sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type)
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800811{
812 struct list_head *head;
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800813 struct inode *inode;
Chao Yubc2a2142015-12-15 13:30:45 +0800814 struct f2fs_inode_info *fi;
Chao Yu74729a92015-12-17 17:17:16 +0800815 bool is_dir = (type == DIR_INODE);
816
817 trace_f2fs_sync_dirty_inodes_enter(sbi->sb, is_dir,
818 get_pages(sbi, is_dir ?
819 F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800820retry:
821 if (unlikely(f2fs_cp_error(sbi)))
Chao Yu167a3162015-12-24 18:04:56 +0800822 return -EIO;
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800823
Chao Yufb1825c2015-12-16 13:09:20 +0800824 spin_lock(&sbi->inode_lock[type]);
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800825
Chao Yufb1825c2015-12-16 13:09:20 +0800826 head = &sbi->inode_list[type];
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800827 if (list_empty(head)) {
Chao Yufb1825c2015-12-16 13:09:20 +0800828 spin_unlock(&sbi->inode_lock[type]);
Chao Yu74729a92015-12-17 17:17:16 +0800829 trace_f2fs_sync_dirty_inodes_exit(sbi->sb, is_dir,
830 get_pages(sbi, is_dir ?
831 F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
Chao Yu167a3162015-12-24 18:04:56 +0800832 return 0;
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800833 }
Chao Yubc2a2142015-12-15 13:30:45 +0800834 fi = list_entry(head->next, struct f2fs_inode_info, dirty_list);
835 inode = igrab(&fi->vfs_inode);
Chao Yufb1825c2015-12-16 13:09:20 +0800836 spin_unlock(&sbi->inode_lock[type]);
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800837 if (inode) {
838 filemap_fdatawrite(inode->i_mapping);
839 iput(inode);
840 } else {
841 /*
842 * We should submit bio, since it exists several
843 * wribacking dentry pages in the freeing inode.
844 */
845 f2fs_submit_merged_bio(sbi, DATA, WRITE);
846 cond_resched();
847 }
848 goto retry;
849}
850
851/*
852 * Freeze all the FS-operations for checkpoint.
853 */
854static int block_operations(struct f2fs_sb_info *sbi)
855{
856 struct writeback_control wbc = {
857 .sync_mode = WB_SYNC_ALL,
858 .nr_to_write = LONG_MAX,
859 .for_reclaim = 0,
860 };
861 struct blk_plug plug;
862 int err = 0;
863
864 blk_start_plug(&plug);
865
866retry_flush_dents:
867 f2fs_lock_all(sbi);
868 /* write all the dirty dentry pages */
869 if (get_pages(sbi, F2FS_DIRTY_DENTS)) {
870 f2fs_unlock_all(sbi);
Chao Yu167a3162015-12-24 18:04:56 +0800871 err = sync_dirty_inodes(sbi, DIR_INODE);
872 if (err)
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800873 goto out;
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800874 goto retry_flush_dents;
875 }
876
877 /*
878 * POR: we should ensure that there are no dirty node pages
879 * until finishing nat/sit flush.
880 */
881retry_flush_nodes:
882 down_write(&sbi->node_write);
883
884 if (get_pages(sbi, F2FS_DIRTY_NODES)) {
885 up_write(&sbi->node_write);
Chao Yu167a3162015-12-24 18:04:56 +0800886 err = sync_node_pages(sbi, 0, &wbc);
887 if (err) {
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800888 f2fs_unlock_all(sbi);
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800889 goto out;
890 }
891 goto retry_flush_nodes;
892 }
893out:
894 blk_finish_plug(&plug);
895 return err;
896}
897
898static void unblock_operations(struct f2fs_sb_info *sbi)
899{
900 up_write(&sbi->node_write);
901 f2fs_unlock_all(sbi);
902}
903
904static void wait_on_all_pages_writeback(struct f2fs_sb_info *sbi)
905{
906 DEFINE_WAIT(wait);
907
908 for (;;) {
909 prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE);
910
911 if (!get_pages(sbi, F2FS_WRITEBACK))
912 break;
913
914 io_schedule();
915 }
916 finish_wait(&sbi->cp_wait, &wait);
917}
918
Chao Yub962dc12015-12-23 17:50:30 +0800919static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800920{
921 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
922 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
923 struct f2fs_nm_info *nm_i = NM_I(sbi);
924 unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num;
925 nid_t last_nid = nm_i->next_scan_nid;
926 block_t start_blk;
927 unsigned int data_sum_blocks, orphan_blocks;
928 __u32 crc32 = 0;
929 int i;
930 int cp_payload_blks = __cp_payload(sbi);
931 block_t discard_blk = NEXT_FREE_BLKADDR(sbi, curseg);
932 bool invalidate = false;
Shuoran Liufa8a1d72016-01-27 09:57:30 +0800933 struct super_block *sb = sbi->sb;
934 struct curseg_info *seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
935 u64 kbytes_written;
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800936
937 /*
938 * This avoids to conduct wrong roll-forward operations and uses
939 * metapages, so should be called prior to sync_meta_pages below.
940 */
941 if (discard_next_dnode(sbi, discard_blk))
942 invalidate = true;
943
944 /* Flush all the NAT/SIT pages */
945 while (get_pages(sbi, F2FS_DIRTY_META)) {
946 sync_meta_pages(sbi, META, LONG_MAX);
947 if (unlikely(f2fs_cp_error(sbi)))
Chao Yub962dc12015-12-23 17:50:30 +0800948 return -EIO;
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800949 }
950
951 next_free_nid(sbi, &last_nid);
952
953 /*
954 * modify checkpoint
955 * version number is already updated
956 */
957 ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi));
958 ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi));
959 ckpt->free_segment_count = cpu_to_le32(free_segments(sbi));
960 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
961 ckpt->cur_node_segno[i] =
962 cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE));
963 ckpt->cur_node_blkoff[i] =
964 cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE));
965 ckpt->alloc_type[i + CURSEG_HOT_NODE] =
966 curseg_alloc_type(sbi, i + CURSEG_HOT_NODE);
967 }
968 for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
969 ckpt->cur_data_segno[i] =
970 cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA));
971 ckpt->cur_data_blkoff[i] =
972 cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA));
973 ckpt->alloc_type[i + CURSEG_HOT_DATA] =
974 curseg_alloc_type(sbi, i + CURSEG_HOT_DATA);
975 }
976
977 ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi));
978 ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi));
979 ckpt->next_free_nid = cpu_to_le32(last_nid);
980
981 /* 2 cp + n data seg summary + orphan inode blocks */
982 data_sum_blocks = npages_for_summary_flush(sbi, false);
983 if (data_sum_blocks < NR_CURSEG_DATA_TYPE)
984 set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
985 else
986 clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
987
988 orphan_blocks = GET_ORPHAN_BLOCKS(orphan_num);
989 ckpt->cp_pack_start_sum = cpu_to_le32(1 + cp_payload_blks +
990 orphan_blocks);
991
992 if (__remain_node_summaries(cpc->reason))
993 ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS+
994 cp_payload_blks + data_sum_blocks +
995 orphan_blocks + NR_CURSEG_NODE_TYPE);
996 else
997 ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS +
998 cp_payload_blks + data_sum_blocks +
999 orphan_blocks);
1000
1001 if (cpc->reason == CP_UMOUNT)
1002 set_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
1003 else
1004 clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
1005
1006 if (cpc->reason == CP_FASTBOOT)
1007 set_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
1008 else
1009 clear_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
1010
1011 if (orphan_num)
1012 set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
1013 else
1014 clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
1015
1016 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK))
1017 set_ckpt_flags(ckpt, CP_FSCK_FLAG);
1018
1019 /* update SIT/NAT bitmap */
1020 get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP));
1021 get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP));
1022
1023 crc32 = f2fs_crc32(ckpt, le32_to_cpu(ckpt->checksum_offset));
1024 *((__le32 *)((unsigned char *)ckpt +
1025 le32_to_cpu(ckpt->checksum_offset)))
1026 = cpu_to_le32(crc32);
1027
1028 start_blk = __start_cp_addr(sbi);
1029
1030 /* need to wait for end_io results */
1031 wait_on_all_pages_writeback(sbi);
1032 if (unlikely(f2fs_cp_error(sbi)))
Chao Yub962dc12015-12-23 17:50:30 +08001033 return -EIO;
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001034
1035 /* write out checkpoint buffer at block 0 */
1036 update_meta_page(sbi, ckpt, start_blk++);
1037
1038 for (i = 1; i < 1 + cp_payload_blks; i++)
1039 update_meta_page(sbi, (char *)ckpt + i * F2FS_BLKSIZE,
1040 start_blk++);
1041
1042 if (orphan_num) {
1043 write_orphan_inodes(sbi, start_blk);
1044 start_blk += orphan_blocks;
1045 }
1046
1047 write_data_summaries(sbi, start_blk);
1048 start_blk += data_sum_blocks;
Shuoran Liufa8a1d72016-01-27 09:57:30 +08001049
1050 /* Record write statistics in the hot node summary */
1051 kbytes_written = sbi->kbytes_written;
1052 if (sb->s_bdev->bd_part)
1053 kbytes_written += BD_PART_WRITTEN(sbi);
1054
Chao Yud59981d2016-02-14 18:50:40 +08001055 seg_i->sum_blk->journal.info.kbytes_written = cpu_to_le64(kbytes_written);
Shuoran Liufa8a1d72016-01-27 09:57:30 +08001056
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001057 if (__remain_node_summaries(cpc->reason)) {
1058 write_node_summaries(sbi, start_blk);
1059 start_blk += NR_CURSEG_NODE_TYPE;
1060 }
1061
1062 /* writeout checkpoint block */
1063 update_meta_page(sbi, ckpt, start_blk);
1064
1065 /* wait for previous submitted node/meta pages writeback */
1066 wait_on_all_pages_writeback(sbi);
1067
1068 if (unlikely(f2fs_cp_error(sbi)))
Chao Yub962dc12015-12-23 17:50:30 +08001069 return -EIO;
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001070
1071 filemap_fdatawait_range(NODE_MAPPING(sbi), 0, LONG_MAX);
1072 filemap_fdatawait_range(META_MAPPING(sbi), 0, LONG_MAX);
1073
1074 /* update user_block_counts */
1075 sbi->last_valid_block_count = sbi->total_valid_block_count;
1076 sbi->alloc_valid_block_count = 0;
1077
1078 /* Here, we only have one bio having CP pack */
1079 sync_meta_pages(sbi, META_FLUSH, LONG_MAX);
1080
1081 /* wait for previous submitted meta pages writeback */
1082 wait_on_all_pages_writeback(sbi);
1083
1084 /*
1085 * invalidate meta page which is used temporarily for zeroing out
1086 * block at the end of warm node chain.
1087 */
1088 if (invalidate)
1089 invalidate_mapping_pages(META_MAPPING(sbi), discard_blk,
1090 discard_blk);
1091
Chao Yudbc32c22015-12-15 13:29:47 +08001092 release_ino_entry(sbi);
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001093
1094 if (unlikely(f2fs_cp_error(sbi)))
Chao Yub962dc12015-12-23 17:50:30 +08001095 return -EIO;
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001096
1097 clear_prefree_segments(sbi, cpc);
1098 clear_sbi_flag(sbi, SBI_IS_DIRTY);
Chao Yub962dc12015-12-23 17:50:30 +08001099
1100 return 0;
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001101}
1102
1103/*
1104 * We guarantee that this checkpoint procedure will not fail.
1105 */
Chao Yub962dc12015-12-23 17:50:30 +08001106int write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001107{
1108 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1109 unsigned long long ckpt_ver;
Chao Yub962dc12015-12-23 17:50:30 +08001110 int err = 0;
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001111
1112 mutex_lock(&sbi->cp_mutex);
1113
1114 if (!is_sbi_flag_set(sbi, SBI_IS_DIRTY) &&
1115 (cpc->reason == CP_FASTBOOT || cpc->reason == CP_SYNC ||
1116 (cpc->reason == CP_DISCARD && !sbi->discard_blks)))
1117 goto out;
Chao Yub962dc12015-12-23 17:50:30 +08001118 if (unlikely(f2fs_cp_error(sbi))) {
1119 err = -EIO;
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001120 goto out;
Chao Yub962dc12015-12-23 17:50:30 +08001121 }
1122 if (f2fs_readonly(sbi->sb)) {
1123 err = -EROFS;
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001124 goto out;
Chao Yub962dc12015-12-23 17:50:30 +08001125 }
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001126
1127 trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "start block_ops");
1128
Chao Yub962dc12015-12-23 17:50:30 +08001129 err = block_operations(sbi);
1130 if (err)
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001131 goto out;
1132
1133 trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish block_ops");
1134
1135 f2fs_submit_merged_bio(sbi, DATA, WRITE);
1136 f2fs_submit_merged_bio(sbi, NODE, WRITE);
1137 f2fs_submit_merged_bio(sbi, META, WRITE);
1138
1139 /*
1140 * update checkpoint pack index
1141 * Increase the version number so that
1142 * SIT entries and seg summaries are written at correct place
1143 */
1144 ckpt_ver = cur_cp_version(ckpt);
1145 ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver);
1146
1147 /* write cached NAT/SIT entries to NAT/SIT area */
1148 flush_nat_entries(sbi);
1149 flush_sit_entries(sbi, cpc);
1150
1151 /* unlock all the fs_lock[] in do_checkpoint() */
Chao Yub962dc12015-12-23 17:50:30 +08001152 err = do_checkpoint(sbi, cpc);
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001153
1154 unblock_operations(sbi);
1155 stat_inc_cp_count(sbi->stat_info);
1156
1157 if (cpc->reason == CP_RECOVERY)
1158 f2fs_msg(sbi->sb, KERN_NOTICE,
1159 "checkpoint: version = %llx", ckpt_ver);
1160
1161 /* do checkpoint periodically */
Jaegeuk Kimdedfbf82016-01-08 15:51:50 -08001162 f2fs_update_time(sbi, CP_TIME);
Jaegeuk Kim1f167ac2015-12-15 16:07:14 -08001163 trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish checkpoint");
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001164out:
1165 mutex_unlock(&sbi->cp_mutex);
Chao Yub962dc12015-12-23 17:50:30 +08001166 return err;
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001167}
1168
1169void init_ino_entry_info(struct f2fs_sb_info *sbi)
1170{
1171 int i;
1172
1173 for (i = 0; i < MAX_INO_ENTRY; i++) {
1174 struct inode_management *im = &sbi->im[i];
1175
1176 INIT_RADIX_TREE(&im->ino_root, GFP_ATOMIC);
1177 spin_lock_init(&im->ino_lock);
1178 INIT_LIST_HEAD(&im->ino_list);
1179 im->ino_num = 0;
1180 }
1181
1182 sbi->max_orphans = (sbi->blocks_per_seg - F2FS_CP_PACKS -
1183 NR_CURSEG_TYPE - __cp_payload(sbi)) *
1184 F2FS_ORPHANS_PER_BLOCK;
1185}
1186
1187int __init create_checkpoint_caches(void)
1188{
1189 ino_entry_slab = f2fs_kmem_cache_create("f2fs_ino_entry",
1190 sizeof(struct ino_entry));
1191 if (!ino_entry_slab)
1192 return -ENOMEM;
1193 inode_entry_slab = f2fs_kmem_cache_create("f2fs_inode_entry",
1194 sizeof(struct inode_entry));
1195 if (!inode_entry_slab) {
1196 kmem_cache_destroy(ino_entry_slab);
1197 return -ENOMEM;
1198 }
1199 return 0;
1200}
1201
1202void destroy_checkpoint_caches(void)
1203{
1204 kmem_cache_destroy(ino_entry_slab);
1205 kmem_cache_destroy(inode_entry_slab);
1206}