f2fs: move f2fs_balance_fs from truncate to punch_hole
[linux-block.git] / fs / f2fs / checkpoint.c
CommitLineData
0a8165d7 1/*
127e670a
JK
2 * fs/f2fs/checkpoint.c
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include <linux/fs.h>
12#include <linux/bio.h>
13#include <linux/mpage.h>
14#include <linux/writeback.h>
15#include <linux/blkdev.h>
16#include <linux/f2fs_fs.h>
17#include <linux/pagevec.h>
18#include <linux/swap.h>
19
20#include "f2fs.h"
21#include "node.h"
22#include "segment.h"
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
JK
545{
546 int t;
547 struct writeback_control wbc = {
548 .sync_mode = WB_SYNC_ALL,
549 .nr_to_write = LONG_MAX,
550 .for_reclaim = 0,
551 };
552
553 /* Stop renaming operation */
554 mutex_lock_op(sbi, RENAME);
555 mutex_lock_op(sbi, DENTRY_OPS);
556
557retry_dents:
558 /* write all the dirty dentry pages */
559 sync_dirty_dir_inodes(sbi);
560
561 mutex_lock_op(sbi, DATA_WRITE);
562 if (get_pages(sbi, F2FS_DIRTY_DENTS)) {
563 mutex_unlock_op(sbi, DATA_WRITE);
564 goto retry_dents;
565 }
566
567 /* block all the operations */
568 for (t = DATA_NEW; t <= NODE_TRUNC; t++)
569 mutex_lock_op(sbi, t);
570
571 mutex_lock(&sbi->write_inode);
572
573 /*
574 * POR: we should ensure that there is no dirty node pages
575 * until finishing nat/sit flush.
576 */
577retry:
578 sync_node_pages(sbi, 0, &wbc);
579
580 mutex_lock_op(sbi, NODE_WRITE);
581
582 if (get_pages(sbi, F2FS_DIRTY_NODES)) {
583 mutex_unlock_op(sbi, NODE_WRITE);
584 goto retry;
585 }
586 mutex_unlock(&sbi->write_inode);
587}
588
589static void unblock_operations(struct f2fs_sb_info *sbi)
590{
591 int t;
592 for (t = NODE_WRITE; t >= RENAME; t--)
593 mutex_unlock_op(sbi, t);
594}
595
596static void do_checkpoint(struct f2fs_sb_info *sbi, bool is_umount)
597{
598 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
599 nid_t last_nid = 0;
600 block_t start_blk;
601 struct page *cp_page;
602 unsigned int data_sum_blocks, orphan_blocks;
25ca923b 603 unsigned int crc32 = 0;
127e670a 604 void *kaddr;
127e670a
JK
605 int i;
606
607 /* Flush all the NAT/SIT pages */
608 while (get_pages(sbi, F2FS_DIRTY_META))
609 sync_meta_pages(sbi, META, LONG_MAX);
610
611 next_free_nid(sbi, &last_nid);
612
613 /*
614 * modify checkpoint
615 * version number is already updated
616 */
617 ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi));
618 ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi));
619 ckpt->free_segment_count = cpu_to_le32(free_segments(sbi));
620 for (i = 0; i < 3; i++) {
621 ckpt->cur_node_segno[i] =
622 cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE));
623 ckpt->cur_node_blkoff[i] =
624 cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE));
625 ckpt->alloc_type[i + CURSEG_HOT_NODE] =
626 curseg_alloc_type(sbi, i + CURSEG_HOT_NODE);
627 }
628 for (i = 0; i < 3; i++) {
629 ckpt->cur_data_segno[i] =
630 cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA));
631 ckpt->cur_data_blkoff[i] =
632 cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA));
633 ckpt->alloc_type[i + CURSEG_HOT_DATA] =
634 curseg_alloc_type(sbi, i + CURSEG_HOT_DATA);
635 }
636
637 ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi));
638 ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi));
639 ckpt->next_free_nid = cpu_to_le32(last_nid);
640
641 /* 2 cp + n data seg summary + orphan inode blocks */
642 data_sum_blocks = npages_for_summary_flush(sbi);
643 if (data_sum_blocks < 3)
25ca923b 644 set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
127e670a 645 else
25ca923b 646 clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
127e670a
JK
647
648 orphan_blocks = (sbi->n_orphans + F2FS_ORPHANS_PER_BLOCK - 1)
649 / F2FS_ORPHANS_PER_BLOCK;
25ca923b 650 ckpt->cp_pack_start_sum = cpu_to_le32(1 + orphan_blocks);
127e670a
JK
651
652 if (is_umount) {
25ca923b
JK
653 set_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
654 ckpt->cp_pack_total_block_count = cpu_to_le32(2 +
655 data_sum_blocks + orphan_blocks + NR_CURSEG_NODE_TYPE);
127e670a 656 } else {
25ca923b
JK
657 clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
658 ckpt->cp_pack_total_block_count = cpu_to_le32(2 +
659 data_sum_blocks + orphan_blocks);
127e670a
JK
660 }
661
662 if (sbi->n_orphans)
25ca923b 663 set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
127e670a 664 else
25ca923b 665 clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
127e670a
JK
666
667 /* update SIT/NAT bitmap */
668 get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP));
669 get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP));
670
671 crc32 = f2fs_crc32(ckpt, le32_to_cpu(ckpt->checksum_offset));
25ca923b 672 *(__le32 *)((unsigned char *)ckpt +
127e670a
JK
673 le32_to_cpu(ckpt->checksum_offset))
674 = cpu_to_le32(crc32);
675
676 start_blk = __start_cp_addr(sbi);
677
678 /* write out checkpoint buffer at block 0 */
679 cp_page = grab_meta_page(sbi, start_blk++);
680 kaddr = page_address(cp_page);
681 memcpy(kaddr, ckpt, (1 << sbi->log_blocksize));
682 set_page_dirty(cp_page);
683 f2fs_put_page(cp_page, 1);
684
685 if (sbi->n_orphans) {
686 write_orphan_inodes(sbi, start_blk);
687 start_blk += orphan_blocks;
688 }
689
690 write_data_summaries(sbi, start_blk);
691 start_blk += data_sum_blocks;
692 if (is_umount) {
693 write_node_summaries(sbi, start_blk);
694 start_blk += NR_CURSEG_NODE_TYPE;
695 }
696
697 /* writeout checkpoint block */
698 cp_page = grab_meta_page(sbi, start_blk);
699 kaddr = page_address(cp_page);
700 memcpy(kaddr, ckpt, (1 << sbi->log_blocksize));
701 set_page_dirty(cp_page);
702 f2fs_put_page(cp_page, 1);
703
704 /* wait for previous submitted node/meta pages writeback */
705 while (get_pages(sbi, F2FS_WRITEBACK))
706 congestion_wait(BLK_RW_ASYNC, HZ / 50);
707
708 filemap_fdatawait_range(sbi->node_inode->i_mapping, 0, LONG_MAX);
709 filemap_fdatawait_range(sbi->meta_inode->i_mapping, 0, LONG_MAX);
710
711 /* update user_block_counts */
712 sbi->last_valid_block_count = sbi->total_valid_block_count;
713 sbi->alloc_valid_block_count = 0;
714
715 /* Here, we only have one bio having CP pack */
577e3495 716 sync_meta_pages(sbi, META_FLUSH, LONG_MAX);
127e670a 717
577e3495
JK
718 if (!is_set_ckpt_flags(ckpt, CP_ERROR_FLAG)) {
719 clear_prefree_segments(sbi);
720 F2FS_RESET_SB_DIRT(sbi);
721 }
127e670a
JK
722}
723
0a8165d7 724/*
127e670a
JK
725 * We guarantee that this checkpoint procedure should not fail.
726 */
43727527 727void write_checkpoint(struct f2fs_sb_info *sbi, bool is_umount)
127e670a
JK
728{
729 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
730 unsigned long long ckpt_ver;
731
43727527
JK
732 mutex_lock(&sbi->cp_mutex);
733 block_operations(sbi);
127e670a
JK
734
735 f2fs_submit_bio(sbi, DATA, true);
736 f2fs_submit_bio(sbi, NODE, true);
737 f2fs_submit_bio(sbi, META, true);
738
739 /*
740 * update checkpoint pack index
741 * Increase the version number so that
742 * SIT entries and seg summaries are written at correct place
743 */
744 ckpt_ver = le64_to_cpu(ckpt->checkpoint_ver);
745 ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver);
746
747 /* write cached NAT/SIT entries to NAT/SIT area */
748 flush_nat_entries(sbi);
749 flush_sit_entries(sbi);
750
127e670a
JK
751 /* unlock all the fs_lock[] in do_checkpoint() */
752 do_checkpoint(sbi, is_umount);
753
754 unblock_operations(sbi);
755 mutex_unlock(&sbi->cp_mutex);
756}
757
758void init_orphan_info(struct f2fs_sb_info *sbi)
759{
760 mutex_init(&sbi->orphan_inode_mutex);
761 INIT_LIST_HEAD(&sbi->orphan_inode_list);
762 sbi->n_orphans = 0;
763}
764
6e6093a8 765int __init create_checkpoint_caches(void)
127e670a
JK
766{
767 orphan_entry_slab = f2fs_kmem_cache_create("f2fs_orphan_entry",
768 sizeof(struct orphan_inode_entry), NULL);
769 if (unlikely(!orphan_entry_slab))
770 return -ENOMEM;
771 inode_entry_slab = f2fs_kmem_cache_create("f2fs_dirty_dir_entry",
772 sizeof(struct dir_inode_entry), NULL);
773 if (unlikely(!inode_entry_slab)) {
774 kmem_cache_destroy(orphan_entry_slab);
775 return -ENOMEM;
776 }
777 return 0;
778}
779
780void destroy_checkpoint_caches(void)
781{
782 kmem_cache_destroy(orphan_entry_slab);
783 kmem_cache_destroy(inode_entry_slab);
784}