]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/f2fs/checkpoint.c
f2fs: avoid frequent background GC
[mirror_ubuntu-bionic-kernel.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"
2af4bd6c 23#include <trace/events/f2fs.h>
127e670a
JK
24
25static struct kmem_cache *orphan_entry_slab;
26static struct kmem_cache *inode_entry_slab;
27
0a8165d7 28/*
127e670a
JK
29 * We guarantee no failure on the returned page.
30 */
31struct page *grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
32{
33 struct address_space *mapping = sbi->meta_inode->i_mapping;
34 struct page *page = NULL;
35repeat:
36 page = grab_cache_page(mapping, index);
37 if (!page) {
38 cond_resched();
39 goto repeat;
40 }
41
42 /* We wait writeback only inside grab_meta_page() */
43 wait_on_page_writeback(page);
44 SetPageUptodate(page);
45 return page;
46}
47
0a8165d7 48/*
127e670a
JK
49 * We guarantee no failure on the returned page.
50 */
51struct page *get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
52{
53 struct address_space *mapping = sbi->meta_inode->i_mapping;
54 struct page *page;
55repeat:
56 page = grab_cache_page(mapping, index);
57 if (!page) {
58 cond_resched();
59 goto repeat;
60 }
393ff91f
JK
61 if (PageUptodate(page))
62 goto out;
63
64 if (f2fs_readpage(sbi, page, index, READ_SYNC))
127e670a 65 goto repeat;
127e670a 66
393ff91f
JK
67 lock_page(page);
68out:
69 mark_page_accessed(page);
127e670a
JK
70 return page;
71}
72
73static int f2fs_write_meta_page(struct page *page,
74 struct writeback_control *wbc)
75{
76 struct inode *inode = page->mapping->host;
77 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
127e670a 78
577e3495
JK
79 /* Should not write any meta pages, if any IO error was occurred */
80 if (wbc->for_reclaim ||
81 is_set_ckpt_flags(F2FS_CKPT(sbi), CP_ERROR_FLAG)) {
82 dec_page_count(sbi, F2FS_DIRTY_META);
127e670a
JK
83 wbc->pages_skipped++;
84 set_page_dirty(page);
577e3495 85 return AOP_WRITEPAGE_ACTIVATE;
127e670a
JK
86 }
87
577e3495 88 wait_on_page_writeback(page);
127e670a 89
577e3495
JK
90 write_meta_page(sbi, page);
91 dec_page_count(sbi, F2FS_DIRTY_META);
92 unlock_page(page);
93 return 0;
127e670a
JK
94}
95
96static int f2fs_write_meta_pages(struct address_space *mapping,
97 struct writeback_control *wbc)
98{
99 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
100 struct block_device *bdev = sbi->sb->s_bdev;
101 long written;
102
103 if (wbc->for_kupdate)
104 return 0;
105
106 if (get_pages(sbi, F2FS_DIRTY_META) == 0)
107 return 0;
108
109 /* if mounting is failed, skip writing node pages */
110 mutex_lock(&sbi->cp_mutex);
111 written = sync_meta_pages(sbi, META, bio_get_nr_vecs(bdev));
112 mutex_unlock(&sbi->cp_mutex);
113 wbc->nr_to_write -= written;
114 return 0;
115}
116
117long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
118 long nr_to_write)
119{
120 struct address_space *mapping = sbi->meta_inode->i_mapping;
121 pgoff_t index = 0, end = LONG_MAX;
122 struct pagevec pvec;
123 long nwritten = 0;
124 struct writeback_control wbc = {
125 .for_reclaim = 0,
126 };
127
128 pagevec_init(&pvec, 0);
129
130 while (index <= end) {
131 int i, nr_pages;
132 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
133 PAGECACHE_TAG_DIRTY,
134 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
135 if (nr_pages == 0)
136 break;
137
138 for (i = 0; i < nr_pages; i++) {
139 struct page *page = pvec.pages[i];
140 lock_page(page);
141 BUG_ON(page->mapping != mapping);
142 BUG_ON(!PageDirty(page));
143 clear_page_dirty_for_io(page);
577e3495
JK
144 if (f2fs_write_meta_page(page, &wbc)) {
145 unlock_page(page);
146 break;
147 }
127e670a
JK
148 if (nwritten++ >= nr_to_write)
149 break;
150 }
151 pagevec_release(&pvec);
152 cond_resched();
153 }
154
155 if (nwritten)
156 f2fs_submit_bio(sbi, type, nr_to_write == LONG_MAX);
157
158 return nwritten;
159}
160
161static int f2fs_set_meta_page_dirty(struct page *page)
162{
163 struct address_space *mapping = page->mapping;
164 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
165
166 SetPageUptodate(page);
167 if (!PageDirty(page)) {
168 __set_page_dirty_nobuffers(page);
169 inc_page_count(sbi, F2FS_DIRTY_META);
127e670a
JK
170 return 1;
171 }
172 return 0;
173}
174
175const struct address_space_operations f2fs_meta_aops = {
176 .writepage = f2fs_write_meta_page,
177 .writepages = f2fs_write_meta_pages,
178 .set_page_dirty = f2fs_set_meta_page_dirty,
179};
180
181int check_orphan_space(struct f2fs_sb_info *sbi)
182{
183 unsigned int max_orphans;
184 int err = 0;
185
186 /*
187 * considering 512 blocks in a segment 5 blocks are needed for cp
188 * and log segment summaries. Remaining blocks are used to keep
189 * orphan entries with the limitation one reserved segment
190 * for cp pack we can have max 1020*507 orphan entries
191 */
192 max_orphans = (sbi->blocks_per_seg - 5) * F2FS_ORPHANS_PER_BLOCK;
193 mutex_lock(&sbi->orphan_inode_mutex);
194 if (sbi->n_orphans >= max_orphans)
195 err = -ENOSPC;
196 mutex_unlock(&sbi->orphan_inode_mutex);
197 return err;
198}
199
200void add_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
201{
202 struct list_head *head, *this;
203 struct orphan_inode_entry *new = NULL, *orphan = NULL;
204
205 mutex_lock(&sbi->orphan_inode_mutex);
206 head = &sbi->orphan_inode_list;
207 list_for_each(this, head) {
208 orphan = list_entry(this, struct orphan_inode_entry, list);
209 if (orphan->ino == ino)
210 goto out;
211 if (orphan->ino > ino)
212 break;
213 orphan = NULL;
214 }
215retry:
216 new = kmem_cache_alloc(orphan_entry_slab, GFP_ATOMIC);
217 if (!new) {
218 cond_resched();
219 goto retry;
220 }
221 new->ino = ino;
127e670a
JK
222
223 /* add new_oentry into list which is sorted by inode number */
a2617dc6 224 if (orphan)
225 list_add(&new->list, this->prev);
226 else
127e670a 227 list_add_tail(&new->list, head);
a2617dc6 228
127e670a
JK
229 sbi->n_orphans++;
230out:
231 mutex_unlock(&sbi->orphan_inode_mutex);
232}
233
234void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
235{
236 struct list_head *this, *next, *head;
237 struct orphan_inode_entry *orphan;
238
239 mutex_lock(&sbi->orphan_inode_mutex);
240 head = &sbi->orphan_inode_list;
241 list_for_each_safe(this, next, head) {
242 orphan = list_entry(this, struct orphan_inode_entry, list);
243 if (orphan->ino == ino) {
244 list_del(&orphan->list);
245 kmem_cache_free(orphan_entry_slab, orphan);
246 sbi->n_orphans--;
247 break;
248 }
249 }
250 mutex_unlock(&sbi->orphan_inode_mutex);
251}
252
253static void recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
254{
255 struct inode *inode = f2fs_iget(sbi->sb, ino);
256 BUG_ON(IS_ERR(inode));
257 clear_nlink(inode);
258
259 /* truncate all the data during iput */
260 iput(inode);
261}
262
263int recover_orphan_inodes(struct f2fs_sb_info *sbi)
264{
265 block_t start_blk, orphan_blkaddr, i, j;
266
25ca923b 267 if (!is_set_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG))
127e670a
JK
268 return 0;
269
270 sbi->por_doing = 1;
271 start_blk = __start_cp_addr(sbi) + 1;
272 orphan_blkaddr = __start_sum_addr(sbi) - 1;
273
274 for (i = 0; i < orphan_blkaddr; i++) {
275 struct page *page = get_meta_page(sbi, start_blk + i);
276 struct f2fs_orphan_block *orphan_blk;
277
278 orphan_blk = (struct f2fs_orphan_block *)page_address(page);
279 for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) {
280 nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
281 recover_orphan_inode(sbi, ino);
282 }
283 f2fs_put_page(page, 1);
284 }
285 /* clear Orphan Flag */
25ca923b 286 clear_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG);
127e670a
JK
287 sbi->por_doing = 0;
288 return 0;
289}
290
291static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk)
292{
293 struct list_head *head, *this, *next;
294 struct f2fs_orphan_block *orphan_blk = NULL;
295 struct page *page = NULL;
296 unsigned int nentries = 0;
297 unsigned short index = 1;
298 unsigned short orphan_blocks;
299
300 orphan_blocks = (unsigned short)((sbi->n_orphans +
301 (F2FS_ORPHANS_PER_BLOCK - 1)) / F2FS_ORPHANS_PER_BLOCK);
302
303 mutex_lock(&sbi->orphan_inode_mutex);
304 head = &sbi->orphan_inode_list;
305
306 /* loop for each orphan inode entry and write them in Jornal block */
307 list_for_each_safe(this, next, head) {
308 struct orphan_inode_entry *orphan;
309
310 orphan = list_entry(this, struct orphan_inode_entry, list);
311
312 if (nentries == F2FS_ORPHANS_PER_BLOCK) {
313 /*
314 * an orphan block is full of 1020 entries,
315 * then we need to flush current orphan blocks
316 * and bring another one in memory
317 */
318 orphan_blk->blk_addr = cpu_to_le16(index);
319 orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
320 orphan_blk->entry_count = cpu_to_le32(nentries);
321 set_page_dirty(page);
322 f2fs_put_page(page, 1);
323 index++;
324 start_blk++;
325 nentries = 0;
326 page = NULL;
327 }
328 if (page)
329 goto page_exist;
330
331 page = grab_meta_page(sbi, start_blk);
332 orphan_blk = (struct f2fs_orphan_block *)page_address(page);
333 memset(orphan_blk, 0, sizeof(*orphan_blk));
334page_exist:
335 orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino);
336 }
337 if (!page)
338 goto end;
339
340 orphan_blk->blk_addr = cpu_to_le16(index);
341 orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
342 orphan_blk->entry_count = cpu_to_le32(nentries);
343 set_page_dirty(page);
344 f2fs_put_page(page, 1);
345end:
346 mutex_unlock(&sbi->orphan_inode_mutex);
347}
348
349static struct page *validate_checkpoint(struct f2fs_sb_info *sbi,
350 block_t cp_addr, unsigned long long *version)
351{
352 struct page *cp_page_1, *cp_page_2 = NULL;
353 unsigned long blk_size = sbi->blocksize;
354 struct f2fs_checkpoint *cp_block;
355 unsigned long long cur_version = 0, pre_version = 0;
356 unsigned int crc = 0;
357 size_t crc_offset;
358
359 /* Read the 1st cp block in this CP pack */
360 cp_page_1 = get_meta_page(sbi, cp_addr);
361
362 /* get the version number */
363 cp_block = (struct f2fs_checkpoint *)page_address(cp_page_1);
364 crc_offset = le32_to_cpu(cp_block->checksum_offset);
365 if (crc_offset >= blk_size)
366 goto invalid_cp1;
367
368 crc = *(unsigned int *)((unsigned char *)cp_block + crc_offset);
369 if (!f2fs_crc_valid(crc, cp_block, crc_offset))
370 goto invalid_cp1;
371
372 pre_version = le64_to_cpu(cp_block->checkpoint_ver);
373
374 /* Read the 2nd cp block in this CP pack */
25ca923b 375 cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1;
127e670a
JK
376 cp_page_2 = get_meta_page(sbi, cp_addr);
377
378 cp_block = (struct f2fs_checkpoint *)page_address(cp_page_2);
379 crc_offset = le32_to_cpu(cp_block->checksum_offset);
380 if (crc_offset >= blk_size)
381 goto invalid_cp2;
382
383 crc = *(unsigned int *)((unsigned char *)cp_block + crc_offset);
384 if (!f2fs_crc_valid(crc, cp_block, crc_offset))
385 goto invalid_cp2;
386
387 cur_version = le64_to_cpu(cp_block->checkpoint_ver);
388
389 if (cur_version == pre_version) {
390 *version = cur_version;
391 f2fs_put_page(cp_page_2, 1);
392 return cp_page_1;
393 }
394invalid_cp2:
395 f2fs_put_page(cp_page_2, 1);
396invalid_cp1:
397 f2fs_put_page(cp_page_1, 1);
398 return NULL;
399}
400
401int get_valid_checkpoint(struct f2fs_sb_info *sbi)
402{
403 struct f2fs_checkpoint *cp_block;
404 struct f2fs_super_block *fsb = sbi->raw_super;
405 struct page *cp1, *cp2, *cur_page;
406 unsigned long blk_size = sbi->blocksize;
407 unsigned long long cp1_version = 0, cp2_version = 0;
408 unsigned long long cp_start_blk_no;
409
410 sbi->ckpt = kzalloc(blk_size, GFP_KERNEL);
411 if (!sbi->ckpt)
412 return -ENOMEM;
413 /*
414 * Finding out valid cp block involves read both
415 * sets( cp pack1 and cp pack 2)
416 */
417 cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr);
418 cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version);
419
420 /* The second checkpoint pack should start at the next segment */
421 cp_start_blk_no += 1 << le32_to_cpu(fsb->log_blocks_per_seg);
422 cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version);
423
424 if (cp1 && cp2) {
425 if (ver_after(cp2_version, cp1_version))
426 cur_page = cp2;
427 else
428 cur_page = cp1;
429 } else if (cp1) {
430 cur_page = cp1;
431 } else if (cp2) {
432 cur_page = cp2;
433 } else {
434 goto fail_no_cp;
435 }
436
437 cp_block = (struct f2fs_checkpoint *)page_address(cur_page);
438 memcpy(sbi->ckpt, cp_block, blk_size);
439
440 f2fs_put_page(cp1, 1);
441 f2fs_put_page(cp2, 1);
442 return 0;
443
444fail_no_cp:
445 kfree(sbi->ckpt);
446 return -EINVAL;
447}
448
449void set_dirty_dir_page(struct inode *inode, struct page *page)
450{
451 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
452 struct list_head *head = &sbi->dir_inode_list;
453 struct dir_inode_entry *new;
454 struct list_head *this;
455
456 if (!S_ISDIR(inode->i_mode))
457 return;
458retry:
459 new = kmem_cache_alloc(inode_entry_slab, GFP_NOFS);
460 if (!new) {
461 cond_resched();
462 goto retry;
463 }
464 new->inode = inode;
465 INIT_LIST_HEAD(&new->list);
466
467 spin_lock(&sbi->dir_inode_lock);
468 list_for_each(this, head) {
469 struct dir_inode_entry *entry;
470 entry = list_entry(this, struct dir_inode_entry, list);
471 if (entry->inode == inode) {
472 kmem_cache_free(inode_entry_slab, new);
473 goto out;
474 }
475 }
476 list_add_tail(&new->list, head);
477 sbi->n_dirty_dirs++;
478
479 BUG_ON(!S_ISDIR(inode->i_mode));
480out:
481 inc_page_count(sbi, F2FS_DIRTY_DENTS);
482 inode_inc_dirty_dents(inode);
483 SetPagePrivate(page);
484
485 spin_unlock(&sbi->dir_inode_lock);
486}
487
488void remove_dirty_dir_inode(struct inode *inode)
489{
490 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
491 struct list_head *head = &sbi->dir_inode_list;
492 struct list_head *this;
493
494 if (!S_ISDIR(inode->i_mode))
495 return;
496
497 spin_lock(&sbi->dir_inode_lock);
498 if (atomic_read(&F2FS_I(inode)->dirty_dents))
499 goto out;
500
501 list_for_each(this, head) {
502 struct dir_inode_entry *entry;
503 entry = list_entry(this, struct dir_inode_entry, list);
504 if (entry->inode == inode) {
505 list_del(&entry->list);
506 kmem_cache_free(inode_entry_slab, entry);
507 sbi->n_dirty_dirs--;
508 break;
509 }
510 }
511out:
512 spin_unlock(&sbi->dir_inode_lock);
513}
514
515void sync_dirty_dir_inodes(struct f2fs_sb_info *sbi)
516{
517 struct list_head *head = &sbi->dir_inode_list;
518 struct dir_inode_entry *entry;
519 struct inode *inode;
520retry:
521 spin_lock(&sbi->dir_inode_lock);
522 if (list_empty(head)) {
523 spin_unlock(&sbi->dir_inode_lock);
524 return;
525 }
526 entry = list_entry(head->next, struct dir_inode_entry, list);
527 inode = igrab(entry->inode);
528 spin_unlock(&sbi->dir_inode_lock);
529 if (inode) {
530 filemap_flush(inode->i_mapping);
531 iput(inode);
532 } else {
533 /*
534 * We should submit bio, since it exists several
535 * wribacking dentry pages in the freeing inode.
536 */
537 f2fs_submit_bio(sbi, DATA, true);
538 }
539 goto retry;
540}
541
0a8165d7 542/*
127e670a
JK
543 * Freeze all the FS-operations for checkpoint.
544 */
43727527 545static void block_operations(struct f2fs_sb_info *sbi)
127e670a 546{
127e670a
JK
547 struct writeback_control wbc = {
548 .sync_mode = WB_SYNC_ALL,
549 .nr_to_write = LONG_MAX,
550 .for_reclaim = 0,
551 };
39936837
JK
552retry_flush_dents:
553 mutex_lock_all(sbi);
127e670a 554
127e670a 555 /* write all the dirty dentry pages */
127e670a 556 if (get_pages(sbi, F2FS_DIRTY_DENTS)) {
39936837
JK
557 mutex_unlock_all(sbi);
558 sync_dirty_dir_inodes(sbi);
559 goto retry_flush_dents;
127e670a
JK
560 }
561
127e670a
JK
562 /*
563 * POR: we should ensure that there is no dirty node pages
564 * until finishing nat/sit flush.
565 */
39936837
JK
566retry_flush_nodes:
567 mutex_lock(&sbi->node_write);
127e670a
JK
568
569 if (get_pages(sbi, F2FS_DIRTY_NODES)) {
39936837
JK
570 mutex_unlock(&sbi->node_write);
571 sync_node_pages(sbi, 0, &wbc);
572 goto retry_flush_nodes;
127e670a 573 }
127e670a
JK
574}
575
576static void unblock_operations(struct f2fs_sb_info *sbi)
577{
39936837
JK
578 mutex_unlock(&sbi->node_write);
579 mutex_unlock_all(sbi);
127e670a
JK
580}
581
582static void do_checkpoint(struct f2fs_sb_info *sbi, bool is_umount)
583{
584 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
585 nid_t last_nid = 0;
586 block_t start_blk;
587 struct page *cp_page;
588 unsigned int data_sum_blocks, orphan_blocks;
25ca923b 589 unsigned int crc32 = 0;
127e670a 590 void *kaddr;
127e670a
JK
591 int i;
592
593 /* Flush all the NAT/SIT pages */
594 while (get_pages(sbi, F2FS_DIRTY_META))
595 sync_meta_pages(sbi, META, LONG_MAX);
596
597 next_free_nid(sbi, &last_nid);
598
599 /*
600 * modify checkpoint
601 * version number is already updated
602 */
603 ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi));
604 ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi));
605 ckpt->free_segment_count = cpu_to_le32(free_segments(sbi));
606 for (i = 0; i < 3; i++) {
607 ckpt->cur_node_segno[i] =
608 cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE));
609 ckpt->cur_node_blkoff[i] =
610 cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE));
611 ckpt->alloc_type[i + CURSEG_HOT_NODE] =
612 curseg_alloc_type(sbi, i + CURSEG_HOT_NODE);
613 }
614 for (i = 0; i < 3; i++) {
615 ckpt->cur_data_segno[i] =
616 cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA));
617 ckpt->cur_data_blkoff[i] =
618 cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA));
619 ckpt->alloc_type[i + CURSEG_HOT_DATA] =
620 curseg_alloc_type(sbi, i + CURSEG_HOT_DATA);
621 }
622
623 ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi));
624 ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi));
625 ckpt->next_free_nid = cpu_to_le32(last_nid);
626
627 /* 2 cp + n data seg summary + orphan inode blocks */
628 data_sum_blocks = npages_for_summary_flush(sbi);
629 if (data_sum_blocks < 3)
25ca923b 630 set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
127e670a 631 else
25ca923b 632 clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
127e670a
JK
633
634 orphan_blocks = (sbi->n_orphans + F2FS_ORPHANS_PER_BLOCK - 1)
635 / F2FS_ORPHANS_PER_BLOCK;
25ca923b 636 ckpt->cp_pack_start_sum = cpu_to_le32(1 + orphan_blocks);
127e670a
JK
637
638 if (is_umount) {
25ca923b
JK
639 set_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
640 ckpt->cp_pack_total_block_count = cpu_to_le32(2 +
641 data_sum_blocks + orphan_blocks + NR_CURSEG_NODE_TYPE);
127e670a 642 } else {
25ca923b
JK
643 clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
644 ckpt->cp_pack_total_block_count = cpu_to_le32(2 +
645 data_sum_blocks + orphan_blocks);
127e670a
JK
646 }
647
648 if (sbi->n_orphans)
25ca923b 649 set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
127e670a 650 else
25ca923b 651 clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
127e670a
JK
652
653 /* update SIT/NAT bitmap */
654 get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP));
655 get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP));
656
657 crc32 = f2fs_crc32(ckpt, le32_to_cpu(ckpt->checksum_offset));
25ca923b 658 *(__le32 *)((unsigned char *)ckpt +
127e670a
JK
659 le32_to_cpu(ckpt->checksum_offset))
660 = cpu_to_le32(crc32);
661
662 start_blk = __start_cp_addr(sbi);
663
664 /* write out checkpoint buffer at block 0 */
665 cp_page = grab_meta_page(sbi, start_blk++);
666 kaddr = page_address(cp_page);
667 memcpy(kaddr, ckpt, (1 << sbi->log_blocksize));
668 set_page_dirty(cp_page);
669 f2fs_put_page(cp_page, 1);
670
671 if (sbi->n_orphans) {
672 write_orphan_inodes(sbi, start_blk);
673 start_blk += orphan_blocks;
674 }
675
676 write_data_summaries(sbi, start_blk);
677 start_blk += data_sum_blocks;
678 if (is_umount) {
679 write_node_summaries(sbi, start_blk);
680 start_blk += NR_CURSEG_NODE_TYPE;
681 }
682
683 /* writeout checkpoint block */
684 cp_page = grab_meta_page(sbi, start_blk);
685 kaddr = page_address(cp_page);
686 memcpy(kaddr, ckpt, (1 << sbi->log_blocksize));
687 set_page_dirty(cp_page);
688 f2fs_put_page(cp_page, 1);
689
690 /* wait for previous submitted node/meta pages writeback */
691 while (get_pages(sbi, F2FS_WRITEBACK))
692 congestion_wait(BLK_RW_ASYNC, HZ / 50);
693
694 filemap_fdatawait_range(sbi->node_inode->i_mapping, 0, LONG_MAX);
695 filemap_fdatawait_range(sbi->meta_inode->i_mapping, 0, LONG_MAX);
696
697 /* update user_block_counts */
698 sbi->last_valid_block_count = sbi->total_valid_block_count;
699 sbi->alloc_valid_block_count = 0;
700
701 /* Here, we only have one bio having CP pack */
577e3495 702 sync_meta_pages(sbi, META_FLUSH, LONG_MAX);
127e670a 703
577e3495
JK
704 if (!is_set_ckpt_flags(ckpt, CP_ERROR_FLAG)) {
705 clear_prefree_segments(sbi);
706 F2FS_RESET_SB_DIRT(sbi);
707 }
127e670a
JK
708}
709
0a8165d7 710/*
127e670a
JK
711 * We guarantee that this checkpoint procedure should not fail.
712 */
43727527 713void write_checkpoint(struct f2fs_sb_info *sbi, bool is_umount)
127e670a
JK
714{
715 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
716 unsigned long long ckpt_ver;
717
2af4bd6c
NJ
718 trace_f2fs_write_checkpoint(sbi->sb, is_umount, "start block_ops");
719
43727527
JK
720 mutex_lock(&sbi->cp_mutex);
721 block_operations(sbi);
127e670a 722
2af4bd6c
NJ
723 trace_f2fs_write_checkpoint(sbi->sb, is_umount, "finish block_ops");
724
127e670a
JK
725 f2fs_submit_bio(sbi, DATA, true);
726 f2fs_submit_bio(sbi, NODE, true);
727 f2fs_submit_bio(sbi, META, true);
728
729 /*
730 * update checkpoint pack index
731 * Increase the version number so that
732 * SIT entries and seg summaries are written at correct place
733 */
734 ckpt_ver = le64_to_cpu(ckpt->checkpoint_ver);
735 ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver);
736
737 /* write cached NAT/SIT entries to NAT/SIT area */
738 flush_nat_entries(sbi);
739 flush_sit_entries(sbi);
740
127e670a
JK
741 /* unlock all the fs_lock[] in do_checkpoint() */
742 do_checkpoint(sbi, is_umount);
743
744 unblock_operations(sbi);
745 mutex_unlock(&sbi->cp_mutex);
2af4bd6c
NJ
746
747 trace_f2fs_write_checkpoint(sbi->sb, is_umount, "finish checkpoint");
127e670a
JK
748}
749
750void init_orphan_info(struct f2fs_sb_info *sbi)
751{
752 mutex_init(&sbi->orphan_inode_mutex);
753 INIT_LIST_HEAD(&sbi->orphan_inode_list);
754 sbi->n_orphans = 0;
755}
756
6e6093a8 757int __init create_checkpoint_caches(void)
127e670a
JK
758{
759 orphan_entry_slab = f2fs_kmem_cache_create("f2fs_orphan_entry",
760 sizeof(struct orphan_inode_entry), NULL);
761 if (unlikely(!orphan_entry_slab))
762 return -ENOMEM;
763 inode_entry_slab = f2fs_kmem_cache_create("f2fs_dirty_dir_entry",
764 sizeof(struct dir_inode_entry), NULL);
765 if (unlikely(!inode_entry_slab)) {
766 kmem_cache_destroy(orphan_entry_slab);
767 return -ENOMEM;
768 }
769 return 0;
770}
771
772void destroy_checkpoint_caches(void)
773{
774 kmem_cache_destroy(orphan_entry_slab);
775 kmem_cache_destroy(inode_entry_slab);
776}