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