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