f2fs: handle EIO not to break fs consistency
[GitHub/exynos8895/android_kernel_samsung_universal8895.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 24
6451e041 25static struct kmem_cache *ino_entry_slab;
127e670a
JK
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{
9df27d98 33 struct address_space *mapping = META_MAPPING(sbi);
127e670a
JK
34 struct page *page = NULL;
35repeat:
bde44686 36 page = grab_cache_page(mapping, index);
127e670a
JK
37 if (!page) {
38 cond_resched();
39 goto repeat;
40 }
bde44686 41 f2fs_wait_on_page_writeback(page, META);
127e670a
JK
42 SetPageUptodate(page);
43 return page;
44}
45
0a8165d7 46/*
127e670a
JK
47 * We guarantee no failure on the returned page.
48 */
49struct page *get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
50{
9df27d98 51 struct address_space *mapping = META_MAPPING(sbi);
127e670a
JK
52 struct page *page;
53repeat:
54 page = grab_cache_page(mapping, index);
55 if (!page) {
56 cond_resched();
57 goto repeat;
58 }
393ff91f
JK
59 if (PageUptodate(page))
60 goto out;
61
93dfe2ac
JK
62 if (f2fs_submit_page_bio(sbi, page, index,
63 READ_SYNC | REQ_META | REQ_PRIO))
127e670a 64 goto repeat;
127e670a 65
393ff91f 66 lock_page(page);
6bacf52f 67 if (unlikely(page->mapping != mapping)) {
afcb7ca0
JK
68 f2fs_put_page(page, 1);
69 goto repeat;
70 }
393ff91f 71out:
127e670a
JK
72 return page;
73}
74
b49ad51e 75static inline int get_max_meta_blks(struct f2fs_sb_info *sbi, int type)
662befda
CY
76{
77 switch (type) {
78 case META_NAT:
79 return NM_I(sbi)->max_nid / NAT_ENTRY_PER_BLOCK;
80 case META_SIT:
81 return SIT_BLK_CNT(sbi);
81c1a0f1 82 case META_SSA:
662befda
CY
83 case META_CP:
84 return 0;
85 default:
86 BUG();
87 }
88}
89
90/*
81c1a0f1 91 * Readahead CP/NAT/SIT/SSA pages
662befda
CY
92 */
93int ra_meta_pages(struct f2fs_sb_info *sbi, int start, int nrpages, int type)
94{
95 block_t prev_blk_addr = 0;
96 struct page *page;
97 int blkno = start;
98 int max_blks = get_max_meta_blks(sbi, type);
99
100 struct f2fs_io_info fio = {
101 .type = META,
102 .rw = READ_SYNC | REQ_META | REQ_PRIO
103 };
104
105 for (; nrpages-- > 0; blkno++) {
106 block_t blk_addr;
107
108 switch (type) {
109 case META_NAT:
110 /* get nat block addr */
111 if (unlikely(blkno >= max_blks))
112 blkno = 0;
113 blk_addr = current_nat_addr(sbi,
114 blkno * NAT_ENTRY_PER_BLOCK);
115 break;
116 case META_SIT:
117 /* get sit block addr */
118 if (unlikely(blkno >= max_blks))
119 goto out;
120 blk_addr = current_sit_addr(sbi,
121 blkno * SIT_ENTRY_PER_BLOCK);
122 if (blkno != start && prev_blk_addr + 1 != blk_addr)
123 goto out;
124 prev_blk_addr = blk_addr;
125 break;
81c1a0f1 126 case META_SSA:
662befda 127 case META_CP:
81c1a0f1 128 /* get ssa/cp block addr */
662befda
CY
129 blk_addr = blkno;
130 break;
131 default:
132 BUG();
133 }
134
135 page = grab_cache_page(META_MAPPING(sbi), blk_addr);
136 if (!page)
137 continue;
138 if (PageUptodate(page)) {
662befda
CY
139 f2fs_put_page(page, 1);
140 continue;
141 }
142
143 f2fs_submit_page_mbio(sbi, page, blk_addr, &fio);
662befda
CY
144 f2fs_put_page(page, 0);
145 }
146out:
147 f2fs_submit_merged_bio(sbi, META, READ);
148 return blkno - start;
149}
150
127e670a
JK
151static int f2fs_write_meta_page(struct page *page,
152 struct writeback_control *wbc)
153{
154 struct inode *inode = page->mapping->host;
155 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
127e670a 156
ecda0de3
CY
157 trace_f2fs_writepage(page, META);
158
203681f6 159 if (unlikely(sbi->por_doing))
cfb271d4 160 goto redirty_out;
cfb271d4
CY
161 if (wbc->for_reclaim)
162 goto redirty_out;
1e968fdf 163 if (unlikely(f2fs_cp_error(sbi)))
cf779cab 164 goto redirty_out;
127e670a 165
3cb5ad15 166 f2fs_wait_on_page_writeback(page, META);
577e3495
JK
167 write_meta_page(sbi, page);
168 dec_page_count(sbi, F2FS_DIRTY_META);
169 unlock_page(page);
170 return 0;
cfb271d4
CY
171
172redirty_out:
76f60268 173 redirty_page_for_writepage(wbc, page);
cfb271d4 174 return AOP_WRITEPAGE_ACTIVATE;
127e670a
JK
175}
176
177static int f2fs_write_meta_pages(struct address_space *mapping,
178 struct writeback_control *wbc)
179{
180 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
50c8cdb3 181 long diff, written;
127e670a 182
e5748434
CY
183 trace_f2fs_writepages(mapping->host, wbc, META);
184
5459aa97 185 /* collect a number of dirty meta pages and write together */
50c8cdb3
JK
186 if (wbc->for_kupdate ||
187 get_pages(sbi, F2FS_DIRTY_META) < nr_pages_to_skip(sbi, META))
d3baf95d 188 goto skip_write;
127e670a
JK
189
190 /* if mounting is failed, skip writing node pages */
191 mutex_lock(&sbi->cp_mutex);
50c8cdb3
JK
192 diff = nr_pages_to_write(sbi, META, wbc);
193 written = sync_meta_pages(sbi, META, wbc->nr_to_write);
127e670a 194 mutex_unlock(&sbi->cp_mutex);
50c8cdb3 195 wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff);
127e670a 196 return 0;
d3baf95d
JK
197
198skip_write:
199 wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_META);
200 return 0;
127e670a
JK
201}
202
203long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
204 long nr_to_write)
205{
9df27d98 206 struct address_space *mapping = META_MAPPING(sbi);
127e670a
JK
207 pgoff_t index = 0, end = LONG_MAX;
208 struct pagevec pvec;
209 long nwritten = 0;
210 struct writeback_control wbc = {
211 .for_reclaim = 0,
212 };
213
214 pagevec_init(&pvec, 0);
215
216 while (index <= end) {
217 int i, nr_pages;
218 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
219 PAGECACHE_TAG_DIRTY,
220 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
cfb271d4 221 if (unlikely(nr_pages == 0))
127e670a
JK
222 break;
223
224 for (i = 0; i < nr_pages; i++) {
225 struct page *page = pvec.pages[i];
203681f6 226
127e670a 227 lock_page(page);
203681f6
JK
228
229 if (unlikely(page->mapping != mapping)) {
230continue_unlock:
231 unlock_page(page);
232 continue;
233 }
234 if (!PageDirty(page)) {
235 /* someone wrote it for us */
236 goto continue_unlock;
237 }
238
239 if (!clear_page_dirty_for_io(page))
240 goto continue_unlock;
241
577e3495
JK
242 if (f2fs_write_meta_page(page, &wbc)) {
243 unlock_page(page);
244 break;
245 }
cfb271d4
CY
246 nwritten++;
247 if (unlikely(nwritten >= nr_to_write))
127e670a
JK
248 break;
249 }
250 pagevec_release(&pvec);
251 cond_resched();
252 }
253
254 if (nwritten)
458e6197 255 f2fs_submit_merged_bio(sbi, type, WRITE);
127e670a
JK
256
257 return nwritten;
258}
259
260static int f2fs_set_meta_page_dirty(struct page *page)
261{
262 struct address_space *mapping = page->mapping;
263 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
264
26c6b887
JK
265 trace_f2fs_set_page_dirty(page, META);
266
127e670a
JK
267 SetPageUptodate(page);
268 if (!PageDirty(page)) {
269 __set_page_dirty_nobuffers(page);
270 inc_page_count(sbi, F2FS_DIRTY_META);
127e670a
JK
271 return 1;
272 }
273 return 0;
274}
275
276const struct address_space_operations f2fs_meta_aops = {
277 .writepage = f2fs_write_meta_page,
278 .writepages = f2fs_write_meta_pages,
279 .set_page_dirty = f2fs_set_meta_page_dirty,
280};
281
6451e041 282static void __add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
953e6cc6 283{
39efac41
JK
284 struct ino_entry *e;
285retry:
6451e041 286 spin_lock(&sbi->ino_lock[type]);
39efac41
JK
287
288 e = radix_tree_lookup(&sbi->ino_root[type], ino);
289 if (!e) {
290 e = kmem_cache_alloc(ino_entry_slab, GFP_ATOMIC);
291 if (!e) {
6451e041 292 spin_unlock(&sbi->ino_lock[type]);
39efac41 293 goto retry;
953e6cc6 294 }
39efac41
JK
295 if (radix_tree_insert(&sbi->ino_root[type], ino, e)) {
296 spin_unlock(&sbi->ino_lock[type]);
297 kmem_cache_free(ino_entry_slab, e);
298 goto retry;
299 }
300 memset(e, 0, sizeof(struct ino_entry));
301 e->ino = ino;
953e6cc6 302
39efac41
JK
303 list_add_tail(&e->list, &sbi->ino_list[type]);
304 }
6451e041 305 spin_unlock(&sbi->ino_lock[type]);
953e6cc6
JK
306}
307
6451e041 308static void __remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
953e6cc6 309{
6451e041 310 struct ino_entry *e;
953e6cc6 311
6451e041 312 spin_lock(&sbi->ino_lock[type]);
39efac41
JK
313 e = radix_tree_lookup(&sbi->ino_root[type], ino);
314 if (e) {
315 list_del(&e->list);
316 radix_tree_delete(&sbi->ino_root[type], ino);
317 if (type == ORPHAN_INO)
953e6cc6 318 sbi->n_orphans--;
39efac41
JK
319 spin_unlock(&sbi->ino_lock[type]);
320 kmem_cache_free(ino_entry_slab, e);
321 return;
953e6cc6 322 }
6451e041 323 spin_unlock(&sbi->ino_lock[type]);
953e6cc6
JK
324}
325
fff04f90
JK
326void add_dirty_inode(struct f2fs_sb_info *sbi, nid_t ino, int type)
327{
328 /* add new dirty ino entry into list */
329 __add_ino_entry(sbi, ino, type);
330}
331
332void remove_dirty_inode(struct f2fs_sb_info *sbi, nid_t ino, int type)
333{
334 /* remove dirty ino entry from list */
335 __remove_ino_entry(sbi, ino, type);
336}
337
338/* mode should be APPEND_INO or UPDATE_INO */
339bool exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode)
340{
341 struct ino_entry *e;
342 spin_lock(&sbi->ino_lock[mode]);
343 e = radix_tree_lookup(&sbi->ino_root[mode], ino);
344 spin_unlock(&sbi->ino_lock[mode]);
345 return e ? true : false;
346}
347
6f12ac25 348void release_dirty_inode(struct f2fs_sb_info *sbi)
fff04f90
JK
349{
350 struct ino_entry *e, *tmp;
351 int i;
352
353 for (i = APPEND_INO; i <= UPDATE_INO; i++) {
354 spin_lock(&sbi->ino_lock[i]);
355 list_for_each_entry_safe(e, tmp, &sbi->ino_list[i], list) {
356 list_del(&e->list);
357 radix_tree_delete(&sbi->ino_root[i], e->ino);
358 kmem_cache_free(ino_entry_slab, e);
359 }
360 spin_unlock(&sbi->ino_lock[i]);
361 }
362}
363
cbd56e7d 364int acquire_orphan_inode(struct f2fs_sb_info *sbi)
127e670a 365{
127e670a
JK
366 int err = 0;
367
6451e041 368 spin_lock(&sbi->ino_lock[ORPHAN_INO]);
0d47c1ad 369 if (unlikely(sbi->n_orphans >= sbi->max_orphans))
127e670a 370 err = -ENOSPC;
cbd56e7d
JK
371 else
372 sbi->n_orphans++;
6451e041 373 spin_unlock(&sbi->ino_lock[ORPHAN_INO]);
0d47c1ad 374
127e670a
JK
375 return err;
376}
377
cbd56e7d
JK
378void release_orphan_inode(struct f2fs_sb_info *sbi)
379{
6451e041 380 spin_lock(&sbi->ino_lock[ORPHAN_INO]);
5d56b671 381 f2fs_bug_on(sbi->n_orphans == 0);
cbd56e7d 382 sbi->n_orphans--;
6451e041 383 spin_unlock(&sbi->ino_lock[ORPHAN_INO]);
cbd56e7d
JK
384}
385
127e670a
JK
386void add_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
387{
39efac41 388 /* add new orphan ino entry into list */
6451e041 389 __add_ino_entry(sbi, ino, ORPHAN_INO);
127e670a
JK
390}
391
392void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
393{
953e6cc6 394 /* remove orphan entry from orphan list */
6451e041 395 __remove_ino_entry(sbi, ino, ORPHAN_INO);
127e670a
JK
396}
397
398static void recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
399{
400 struct inode *inode = f2fs_iget(sbi->sb, ino);
5d56b671 401 f2fs_bug_on(IS_ERR(inode));
127e670a
JK
402 clear_nlink(inode);
403
404 /* truncate all the data during iput */
405 iput(inode);
406}
407
8f99a946 408void recover_orphan_inodes(struct f2fs_sb_info *sbi)
127e670a
JK
409{
410 block_t start_blk, orphan_blkaddr, i, j;
411
25ca923b 412 if (!is_set_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG))
8f99a946 413 return;
127e670a 414
aabe5136 415 sbi->por_doing = true;
1dbe4152
CL
416
417 start_blk = __start_cp_addr(sbi) + 1 +
418 le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
127e670a
JK
419 orphan_blkaddr = __start_sum_addr(sbi) - 1;
420
662befda
CY
421 ra_meta_pages(sbi, start_blk, orphan_blkaddr, META_CP);
422
127e670a
JK
423 for (i = 0; i < orphan_blkaddr; i++) {
424 struct page *page = get_meta_page(sbi, start_blk + i);
425 struct f2fs_orphan_block *orphan_blk;
426
427 orphan_blk = (struct f2fs_orphan_block *)page_address(page);
428 for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) {
429 nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
430 recover_orphan_inode(sbi, ino);
431 }
432 f2fs_put_page(page, 1);
433 }
434 /* clear Orphan Flag */
25ca923b 435 clear_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG);
aabe5136 436 sbi->por_doing = false;
8f99a946 437 return;
127e670a
JK
438}
439
440static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk)
441{
502c6e0b 442 struct list_head *head;
127e670a 443 struct f2fs_orphan_block *orphan_blk = NULL;
127e670a 444 unsigned int nentries = 0;
4531929e
GZ
445 unsigned short index;
446 unsigned short orphan_blocks = (unsigned short)((sbi->n_orphans +
447 (F2FS_ORPHANS_PER_BLOCK - 1)) / F2FS_ORPHANS_PER_BLOCK);
448 struct page *page = NULL;
6451e041 449 struct ino_entry *orphan = NULL;
127e670a 450
4531929e 451 for (index = 0; index < orphan_blocks; index++)
63f5384c 452 grab_meta_page(sbi, start_blk + index);
127e670a 453
4531929e 454 index = 1;
6451e041
JK
455 spin_lock(&sbi->ino_lock[ORPHAN_INO]);
456 head = &sbi->ino_list[ORPHAN_INO];
127e670a
JK
457
458 /* loop for each orphan inode entry and write them in Jornal block */
502c6e0b
GZ
459 list_for_each_entry(orphan, head, list) {
460 if (!page) {
63f5384c
GZ
461 page = find_get_page(META_MAPPING(sbi), start_blk++);
462 f2fs_bug_on(!page);
502c6e0b
GZ
463 orphan_blk =
464 (struct f2fs_orphan_block *)page_address(page);
465 memset(orphan_blk, 0, sizeof(*orphan_blk));
63f5384c 466 f2fs_put_page(page, 0);
502c6e0b 467 }
127e670a 468
36795567 469 orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino);
127e670a 470
36795567 471 if (nentries == F2FS_ORPHANS_PER_BLOCK) {
127e670a
JK
472 /*
473 * an orphan block is full of 1020 entries,
474 * then we need to flush current orphan blocks
475 * and bring another one in memory
476 */
477 orphan_blk->blk_addr = cpu_to_le16(index);
478 orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
479 orphan_blk->entry_count = cpu_to_le32(nentries);
480 set_page_dirty(page);
481 f2fs_put_page(page, 1);
482 index++;
127e670a
JK
483 nentries = 0;
484 page = NULL;
485 }
502c6e0b 486 }
127e670a 487
502c6e0b
GZ
488 if (page) {
489 orphan_blk->blk_addr = cpu_to_le16(index);
490 orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
491 orphan_blk->entry_count = cpu_to_le32(nentries);
492 set_page_dirty(page);
493 f2fs_put_page(page, 1);
127e670a 494 }
502c6e0b 495
6451e041 496 spin_unlock(&sbi->ino_lock[ORPHAN_INO]);
127e670a
JK
497}
498
499static struct page *validate_checkpoint(struct f2fs_sb_info *sbi,
500 block_t cp_addr, unsigned long long *version)
501{
502 struct page *cp_page_1, *cp_page_2 = NULL;
503 unsigned long blk_size = sbi->blocksize;
504 struct f2fs_checkpoint *cp_block;
505 unsigned long long cur_version = 0, pre_version = 0;
127e670a 506 size_t crc_offset;
7e586fa0 507 __u32 crc = 0;
127e670a
JK
508
509 /* Read the 1st cp block in this CP pack */
510 cp_page_1 = get_meta_page(sbi, cp_addr);
511
512 /* get the version number */
513 cp_block = (struct f2fs_checkpoint *)page_address(cp_page_1);
514 crc_offset = le32_to_cpu(cp_block->checksum_offset);
515 if (crc_offset >= blk_size)
516 goto invalid_cp1;
517
7e586fa0 518 crc = le32_to_cpu(*((__u32 *)((unsigned char *)cp_block + crc_offset)));
127e670a
JK
519 if (!f2fs_crc_valid(crc, cp_block, crc_offset))
520 goto invalid_cp1;
521
d71b5564 522 pre_version = cur_cp_version(cp_block);
127e670a
JK
523
524 /* Read the 2nd cp block in this CP pack */
25ca923b 525 cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1;
127e670a
JK
526 cp_page_2 = get_meta_page(sbi, cp_addr);
527
528 cp_block = (struct f2fs_checkpoint *)page_address(cp_page_2);
529 crc_offset = le32_to_cpu(cp_block->checksum_offset);
530 if (crc_offset >= blk_size)
531 goto invalid_cp2;
532
7e586fa0 533 crc = le32_to_cpu(*((__u32 *)((unsigned char *)cp_block + crc_offset)));
127e670a
JK
534 if (!f2fs_crc_valid(crc, cp_block, crc_offset))
535 goto invalid_cp2;
536
d71b5564 537 cur_version = cur_cp_version(cp_block);
127e670a
JK
538
539 if (cur_version == pre_version) {
540 *version = cur_version;
541 f2fs_put_page(cp_page_2, 1);
542 return cp_page_1;
543 }
544invalid_cp2:
545 f2fs_put_page(cp_page_2, 1);
546invalid_cp1:
547 f2fs_put_page(cp_page_1, 1);
548 return NULL;
549}
550
551int get_valid_checkpoint(struct f2fs_sb_info *sbi)
552{
553 struct f2fs_checkpoint *cp_block;
554 struct f2fs_super_block *fsb = sbi->raw_super;
555 struct page *cp1, *cp2, *cur_page;
556 unsigned long blk_size = sbi->blocksize;
557 unsigned long long cp1_version = 0, cp2_version = 0;
558 unsigned long long cp_start_blk_no;
1dbe4152
CL
559 unsigned int cp_blks = 1 + le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
560 block_t cp_blk_no;
561 int i;
127e670a 562
1dbe4152 563 sbi->ckpt = kzalloc(cp_blks * blk_size, GFP_KERNEL);
127e670a
JK
564 if (!sbi->ckpt)
565 return -ENOMEM;
566 /*
567 * Finding out valid cp block involves read both
568 * sets( cp pack1 and cp pack 2)
569 */
570 cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr);
571 cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version);
572
573 /* The second checkpoint pack should start at the next segment */
f9a4e6df
JK
574 cp_start_blk_no += ((unsigned long long)1) <<
575 le32_to_cpu(fsb->log_blocks_per_seg);
127e670a
JK
576 cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version);
577
578 if (cp1 && cp2) {
579 if (ver_after(cp2_version, cp1_version))
580 cur_page = cp2;
581 else
582 cur_page = cp1;
583 } else if (cp1) {
584 cur_page = cp1;
585 } else if (cp2) {
586 cur_page = cp2;
587 } else {
588 goto fail_no_cp;
589 }
590
591 cp_block = (struct f2fs_checkpoint *)page_address(cur_page);
592 memcpy(sbi->ckpt, cp_block, blk_size);
593
1dbe4152
CL
594 if (cp_blks <= 1)
595 goto done;
596
597 cp_blk_no = le32_to_cpu(fsb->cp_blkaddr);
598 if (cur_page == cp2)
599 cp_blk_no += 1 << le32_to_cpu(fsb->log_blocks_per_seg);
600
601 for (i = 1; i < cp_blks; i++) {
602 void *sit_bitmap_ptr;
603 unsigned char *ckpt = (unsigned char *)sbi->ckpt;
604
605 cur_page = get_meta_page(sbi, cp_blk_no + i);
606 sit_bitmap_ptr = page_address(cur_page);
607 memcpy(ckpt + i * blk_size, sit_bitmap_ptr, blk_size);
608 f2fs_put_page(cur_page, 1);
609 }
610done:
127e670a
JK
611 f2fs_put_page(cp1, 1);
612 f2fs_put_page(cp2, 1);
613 return 0;
614
615fail_no_cp:
616 kfree(sbi->ckpt);
617 return -EINVAL;
618}
619
5deb8267 620static int __add_dirty_inode(struct inode *inode, struct dir_inode_entry *new)
127e670a
JK
621{
622 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
127e670a 623
ed57c27f
JK
624 if (is_inode_flag_set(F2FS_I(inode), FI_DIRTY_DIR))
625 return -EEXIST;
2d7b822a 626
ed57c27f
JK
627 set_inode_flag(F2FS_I(inode), FI_DIRTY_DIR);
628 F2FS_I(inode)->dirty_dir = new;
629 list_add_tail(&new->list, &sbi->dir_inode_list);
dcdfff65 630 stat_inc_dirty_dir(sbi);
5deb8267
JK
631 return 0;
632}
633
634void set_dirty_dir_page(struct inode *inode, struct page *page)
635{
636 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
637 struct dir_inode_entry *new;
cf0ee0f0 638 int ret = 0;
5deb8267 639
127e670a
JK
640 if (!S_ISDIR(inode->i_mode))
641 return;
7bd59381
GZ
642
643 new = f2fs_kmem_cache_alloc(inode_entry_slab, GFP_NOFS);
127e670a
JK
644 new->inode = inode;
645 INIT_LIST_HEAD(&new->list);
646
647 spin_lock(&sbi->dir_inode_lock);
cf0ee0f0 648 ret = __add_dirty_inode(inode, new);
127e670a
JK
649 inode_inc_dirty_dents(inode);
650 SetPagePrivate(page);
5deb8267 651 spin_unlock(&sbi->dir_inode_lock);
cf0ee0f0
CY
652
653 if (ret)
654 kmem_cache_free(inode_entry_slab, new);
5deb8267
JK
655}
656
657void add_dirty_dir_inode(struct inode *inode)
658{
659 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
7bd59381
GZ
660 struct dir_inode_entry *new =
661 f2fs_kmem_cache_alloc(inode_entry_slab, GFP_NOFS);
cf0ee0f0 662 int ret = 0;
7bd59381 663
5deb8267
JK
664 new->inode = inode;
665 INIT_LIST_HEAD(&new->list);
127e670a 666
5deb8267 667 spin_lock(&sbi->dir_inode_lock);
cf0ee0f0 668 ret = __add_dirty_inode(inode, new);
127e670a 669 spin_unlock(&sbi->dir_inode_lock);
cf0ee0f0
CY
670
671 if (ret)
672 kmem_cache_free(inode_entry_slab, new);
127e670a
JK
673}
674
675void remove_dirty_dir_inode(struct inode *inode)
676{
677 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
2d7b822a 678 struct dir_inode_entry *entry;
127e670a
JK
679
680 if (!S_ISDIR(inode->i_mode))
681 return;
682
683 spin_lock(&sbi->dir_inode_lock);
ed57c27f
JK
684 if (get_dirty_dents(inode) ||
685 !is_inode_flag_set(F2FS_I(inode), FI_DIRTY_DIR)) {
3b10b1fd
JK
686 spin_unlock(&sbi->dir_inode_lock);
687 return;
688 }
127e670a 689
ed57c27f
JK
690 entry = F2FS_I(inode)->dirty_dir;
691 list_del(&entry->list);
692 F2FS_I(inode)->dirty_dir = NULL;
693 clear_inode_flag(F2FS_I(inode), FI_DIRTY_DIR);
694 stat_dec_dirty_dir(sbi);
127e670a 695 spin_unlock(&sbi->dir_inode_lock);
ed57c27f 696 kmem_cache_free(inode_entry_slab, entry);
74d0b917
JK
697
698 /* Only from the recovery routine */
afc3eda2
JK
699 if (is_inode_flag_set(F2FS_I(inode), FI_DELAY_IPUT)) {
700 clear_inode_flag(F2FS_I(inode), FI_DELAY_IPUT);
74d0b917 701 iput(inode);
afc3eda2 702 }
74d0b917
JK
703}
704
127e670a
JK
705void sync_dirty_dir_inodes(struct f2fs_sb_info *sbi)
706{
ce3b7d80 707 struct list_head *head;
127e670a
JK
708 struct dir_inode_entry *entry;
709 struct inode *inode;
710retry:
711 spin_lock(&sbi->dir_inode_lock);
ce3b7d80
GZ
712
713 head = &sbi->dir_inode_list;
127e670a
JK
714 if (list_empty(head)) {
715 spin_unlock(&sbi->dir_inode_lock);
716 return;
717 }
718 entry = list_entry(head->next, struct dir_inode_entry, list);
719 inode = igrab(entry->inode);
720 spin_unlock(&sbi->dir_inode_lock);
721 if (inode) {
87d6f890 722 filemap_fdatawrite(inode->i_mapping);
127e670a
JK
723 iput(inode);
724 } else {
725 /*
726 * We should submit bio, since it exists several
727 * wribacking dentry pages in the freeing inode.
728 */
458e6197 729 f2fs_submit_merged_bio(sbi, DATA, WRITE);
127e670a
JK
730 }
731 goto retry;
732}
733
0a8165d7 734/*
127e670a
JK
735 * Freeze all the FS-operations for checkpoint.
736 */
cf779cab 737static int block_operations(struct f2fs_sb_info *sbi)
127e670a 738{
127e670a
JK
739 struct writeback_control wbc = {
740 .sync_mode = WB_SYNC_ALL,
741 .nr_to_write = LONG_MAX,
742 .for_reclaim = 0,
743 };
c718379b 744 struct blk_plug plug;
cf779cab 745 int err = 0;
c718379b
JK
746
747 blk_start_plug(&plug);
748
39936837 749retry_flush_dents:
e479556b 750 f2fs_lock_all(sbi);
127e670a 751 /* write all the dirty dentry pages */
127e670a 752 if (get_pages(sbi, F2FS_DIRTY_DENTS)) {
e479556b 753 f2fs_unlock_all(sbi);
39936837 754 sync_dirty_dir_inodes(sbi);
cf779cab
JK
755 if (unlikely(f2fs_cp_error(sbi))) {
756 err = -EIO;
757 goto out;
758 }
39936837 759 goto retry_flush_dents;
127e670a
JK
760 }
761
127e670a 762 /*
e1c42045 763 * POR: we should ensure that there are no dirty node pages
127e670a
JK
764 * until finishing nat/sit flush.
765 */
39936837 766retry_flush_nodes:
b3582c68 767 down_write(&sbi->node_write);
127e670a
JK
768
769 if (get_pages(sbi, F2FS_DIRTY_NODES)) {
b3582c68 770 up_write(&sbi->node_write);
39936837 771 sync_node_pages(sbi, 0, &wbc);
cf779cab
JK
772 if (unlikely(f2fs_cp_error(sbi))) {
773 f2fs_unlock_all(sbi);
774 err = -EIO;
775 goto out;
776 }
39936837 777 goto retry_flush_nodes;
127e670a 778 }
cf779cab 779out:
c718379b 780 blk_finish_plug(&plug);
cf779cab 781 return err;
127e670a
JK
782}
783
784static void unblock_operations(struct f2fs_sb_info *sbi)
785{
b3582c68 786 up_write(&sbi->node_write);
e479556b 787 f2fs_unlock_all(sbi);
127e670a
JK
788}
789
fb51b5ef
CL
790static void wait_on_all_pages_writeback(struct f2fs_sb_info *sbi)
791{
792 DEFINE_WAIT(wait);
793
794 for (;;) {
795 prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE);
796
797 if (!get_pages(sbi, F2FS_WRITEBACK))
798 break;
799
800 io_schedule();
801 }
802 finish_wait(&sbi->cp_wait, &wait);
803}
804
127e670a
JK
805static void do_checkpoint(struct f2fs_sb_info *sbi, bool is_umount)
806{
807 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
cf2271e7 808 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
127e670a
JK
809 nid_t last_nid = 0;
810 block_t start_blk;
811 struct page *cp_page;
812 unsigned int data_sum_blocks, orphan_blocks;
7e586fa0 813 __u32 crc32 = 0;
127e670a 814 void *kaddr;
127e670a 815 int i;
1dbe4152 816 int cp_payload_blks = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
127e670a 817
1e87a78d
JK
818 /*
819 * This avoids to conduct wrong roll-forward operations and uses
820 * metapages, so should be called prior to sync_meta_pages below.
821 */
cf2271e7 822 discard_next_dnode(sbi, NEXT_FREE_BLKADDR(sbi, curseg));
127e670a
JK
823
824 /* Flush all the NAT/SIT pages */
cf779cab 825 while (get_pages(sbi, F2FS_DIRTY_META)) {
127e670a 826 sync_meta_pages(sbi, META, LONG_MAX);
cf779cab
JK
827 if (unlikely(f2fs_cp_error(sbi)))
828 return;
829 }
127e670a
JK
830
831 next_free_nid(sbi, &last_nid);
832
833 /*
834 * modify checkpoint
835 * version number is already updated
836 */
837 ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi));
838 ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi));
839 ckpt->free_segment_count = cpu_to_le32(free_segments(sbi));
840 for (i = 0; i < 3; i++) {
841 ckpt->cur_node_segno[i] =
842 cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE));
843 ckpt->cur_node_blkoff[i] =
844 cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE));
845 ckpt->alloc_type[i + CURSEG_HOT_NODE] =
846 curseg_alloc_type(sbi, i + CURSEG_HOT_NODE);
847 }
848 for (i = 0; i < 3; i++) {
849 ckpt->cur_data_segno[i] =
850 cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA));
851 ckpt->cur_data_blkoff[i] =
852 cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA));
853 ckpt->alloc_type[i + CURSEG_HOT_DATA] =
854 curseg_alloc_type(sbi, i + CURSEG_HOT_DATA);
855 }
856
857 ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi));
858 ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi));
859 ckpt->next_free_nid = cpu_to_le32(last_nid);
860
861 /* 2 cp + n data seg summary + orphan inode blocks */
862 data_sum_blocks = npages_for_summary_flush(sbi);
863 if (data_sum_blocks < 3)
25ca923b 864 set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
127e670a 865 else
25ca923b 866 clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
127e670a
JK
867
868 orphan_blocks = (sbi->n_orphans + F2FS_ORPHANS_PER_BLOCK - 1)
869 / F2FS_ORPHANS_PER_BLOCK;
1dbe4152
CL
870 ckpt->cp_pack_start_sum = cpu_to_le32(1 + cp_payload_blks +
871 orphan_blocks);
127e670a
JK
872
873 if (is_umount) {
25ca923b
JK
874 set_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
875 ckpt->cp_pack_total_block_count = cpu_to_le32(2 +
1dbe4152
CL
876 cp_payload_blks + data_sum_blocks +
877 orphan_blocks + NR_CURSEG_NODE_TYPE);
127e670a 878 } else {
25ca923b
JK
879 clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
880 ckpt->cp_pack_total_block_count = cpu_to_le32(2 +
1dbe4152
CL
881 cp_payload_blks + data_sum_blocks +
882 orphan_blocks);
127e670a
JK
883 }
884
885 if (sbi->n_orphans)
25ca923b 886 set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
127e670a 887 else
25ca923b 888 clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
127e670a
JK
889
890 /* update SIT/NAT bitmap */
891 get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP));
892 get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP));
893
894 crc32 = f2fs_crc32(ckpt, le32_to_cpu(ckpt->checksum_offset));
7e586fa0
JK
895 *((__le32 *)((unsigned char *)ckpt +
896 le32_to_cpu(ckpt->checksum_offset)))
127e670a
JK
897 = cpu_to_le32(crc32);
898
899 start_blk = __start_cp_addr(sbi);
900
901 /* write out checkpoint buffer at block 0 */
902 cp_page = grab_meta_page(sbi, start_blk++);
903 kaddr = page_address(cp_page);
904 memcpy(kaddr, ckpt, (1 << sbi->log_blocksize));
905 set_page_dirty(cp_page);
906 f2fs_put_page(cp_page, 1);
907
1dbe4152
CL
908 for (i = 1; i < 1 + cp_payload_blks; i++) {
909 cp_page = grab_meta_page(sbi, start_blk++);
910 kaddr = page_address(cp_page);
911 memcpy(kaddr, (char *)ckpt + i * F2FS_BLKSIZE,
912 (1 << sbi->log_blocksize));
913 set_page_dirty(cp_page);
914 f2fs_put_page(cp_page, 1);
915 }
916
127e670a
JK
917 if (sbi->n_orphans) {
918 write_orphan_inodes(sbi, start_blk);
919 start_blk += orphan_blocks;
920 }
921
922 write_data_summaries(sbi, start_blk);
923 start_blk += data_sum_blocks;
924 if (is_umount) {
925 write_node_summaries(sbi, start_blk);
926 start_blk += NR_CURSEG_NODE_TYPE;
927 }
928
929 /* writeout checkpoint block */
930 cp_page = grab_meta_page(sbi, start_blk);
931 kaddr = page_address(cp_page);
932 memcpy(kaddr, ckpt, (1 << sbi->log_blocksize));
933 set_page_dirty(cp_page);
934 f2fs_put_page(cp_page, 1);
935
936 /* wait for previous submitted node/meta pages writeback */
fb51b5ef 937 wait_on_all_pages_writeback(sbi);
127e670a 938
cf779cab
JK
939 if (unlikely(f2fs_cp_error(sbi)))
940 return;
941
4ef51a8f 942 filemap_fdatawait_range(NODE_MAPPING(sbi), 0, LONG_MAX);
9df27d98 943 filemap_fdatawait_range(META_MAPPING(sbi), 0, LONG_MAX);
127e670a
JK
944
945 /* update user_block_counts */
946 sbi->last_valid_block_count = sbi->total_valid_block_count;
947 sbi->alloc_valid_block_count = 0;
948
949 /* Here, we only have one bio having CP pack */
577e3495 950 sync_meta_pages(sbi, META_FLUSH, LONG_MAX);
127e670a 951
cf779cab
JK
952 release_dirty_inode(sbi);
953
954 if (unlikely(f2fs_cp_error(sbi)))
955 return;
956
957 clear_prefree_segments(sbi);
958 F2FS_RESET_SB_DIRT(sbi);
127e670a
JK
959}
960
0a8165d7 961/*
e1c42045 962 * We guarantee that this checkpoint procedure will not fail.
127e670a 963 */
43727527 964void write_checkpoint(struct f2fs_sb_info *sbi, bool is_umount)
127e670a
JK
965{
966 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
967 unsigned long long ckpt_ver;
968
2af4bd6c
NJ
969 trace_f2fs_write_checkpoint(sbi->sb, is_umount, "start block_ops");
970
43727527 971 mutex_lock(&sbi->cp_mutex);
8501017e
JK
972
973 if (!sbi->s_dirty)
974 goto out;
cf779cab
JK
975 if (unlikely(f2fs_cp_error(sbi)))
976 goto out;
977 if (block_operations(sbi))
978 goto out;
127e670a 979
2af4bd6c
NJ
980 trace_f2fs_write_checkpoint(sbi->sb, is_umount, "finish block_ops");
981
458e6197
JK
982 f2fs_submit_merged_bio(sbi, DATA, WRITE);
983 f2fs_submit_merged_bio(sbi, NODE, WRITE);
984 f2fs_submit_merged_bio(sbi, META, WRITE);
127e670a
JK
985
986 /*
987 * update checkpoint pack index
988 * Increase the version number so that
989 * SIT entries and seg summaries are written at correct place
990 */
d71b5564 991 ckpt_ver = cur_cp_version(ckpt);
127e670a
JK
992 ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver);
993
994 /* write cached NAT/SIT entries to NAT/SIT area */
995 flush_nat_entries(sbi);
996 flush_sit_entries(sbi);
997
127e670a
JK
998 /* unlock all the fs_lock[] in do_checkpoint() */
999 do_checkpoint(sbi, is_umount);
1000
1001 unblock_operations(sbi);
942e0be6 1002 stat_inc_cp_count(sbi->stat_info);
8501017e
JK
1003out:
1004 mutex_unlock(&sbi->cp_mutex);
2af4bd6c 1005 trace_f2fs_write_checkpoint(sbi->sb, is_umount, "finish checkpoint");
127e670a
JK
1006}
1007
6451e041 1008void init_ino_entry_info(struct f2fs_sb_info *sbi)
127e670a 1009{
6451e041
JK
1010 int i;
1011
1012 for (i = 0; i < MAX_INO_ENTRY; i++) {
39efac41 1013 INIT_RADIX_TREE(&sbi->ino_root[i], GFP_ATOMIC);
6451e041
JK
1014 spin_lock_init(&sbi->ino_lock[i]);
1015 INIT_LIST_HEAD(&sbi->ino_list[i]);
1016 }
1017
0d47c1ad
GZ
1018 /*
1019 * considering 512 blocks in a segment 8 blocks are needed for cp
1020 * and log segment summaries. Remaining blocks are used to keep
1021 * orphan entries with the limitation one reserved segment
1022 * for cp pack we can have max 1020*504 orphan entries
1023 */
6451e041 1024 sbi->n_orphans = 0;
0d47c1ad
GZ
1025 sbi->max_orphans = (sbi->blocks_per_seg - 2 - NR_CURSEG_TYPE)
1026 * F2FS_ORPHANS_PER_BLOCK;
127e670a
JK
1027}
1028
6e6093a8 1029int __init create_checkpoint_caches(void)
127e670a 1030{
6451e041
JK
1031 ino_entry_slab = f2fs_kmem_cache_create("f2fs_ino_entry",
1032 sizeof(struct ino_entry));
1033 if (!ino_entry_slab)
127e670a
JK
1034 return -ENOMEM;
1035 inode_entry_slab = f2fs_kmem_cache_create("f2fs_dirty_dir_entry",
e8512d2e 1036 sizeof(struct dir_inode_entry));
6bacf52f 1037 if (!inode_entry_slab) {
6451e041 1038 kmem_cache_destroy(ino_entry_slab);
127e670a
JK
1039 return -ENOMEM;
1040 }
1041 return 0;
1042}
1043
1044void destroy_checkpoint_caches(void)
1045{
6451e041 1046 kmem_cache_destroy(ino_entry_slab);
127e670a
JK
1047 kmem_cache_destroy(inode_entry_slab);
1048}