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