1 /*
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 #include "trace.h"
24 #include <trace/events/f2fs.h>
25
26 static struct kmem_cache *ino_entry_slab;
27 struct kmem_cache *inode_entry_slab;
28
f2fs_stop_checkpoint(struct f2fs_sb_info * sbi,bool end_io)29 void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io)
30 {
31 set_ckpt_flags(sbi, CP_ERROR_FLAG);
32 if (!end_io)
33 f2fs_flush_merged_writes(sbi);
34 }
35
36 /*
37 * We guarantee no failure on the returned page.
38 */
grab_meta_page(struct f2fs_sb_info * sbi,pgoff_t index)39 struct page *grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
40 {
41 struct address_space *mapping = META_MAPPING(sbi);
42 struct page *page = NULL;
43 repeat:
44 page = f2fs_grab_cache_page(mapping, index, false);
45 if (!page) {
46 cond_resched();
47 goto repeat;
48 }
49 f2fs_wait_on_page_writeback(page, META, true);
50 if (!PageUptodate(page))
51 SetPageUptodate(page);
52 return page;
53 }
54
55 /*
56 * We guarantee no failure on the returned page.
57 */
__get_meta_page(struct f2fs_sb_info * sbi,pgoff_t index,bool is_meta)58 static struct page *__get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index,
59 bool is_meta)
60 {
61 struct address_space *mapping = META_MAPPING(sbi);
62 struct page *page;
63 struct f2fs_io_info fio = {
64 .sbi = sbi,
65 .type = META,
66 .op = REQ_OP_READ,
67 .op_flags = REQ_META | REQ_PRIO,
68 .old_blkaddr = index,
69 .new_blkaddr = index,
70 .encrypted_page = NULL,
71 .is_meta = is_meta,
72 };
73
74 if (unlikely(!is_meta))
75 fio.op_flags &= ~REQ_META;
76 repeat:
77 page = f2fs_grab_cache_page(mapping, index, false);
78 if (!page) {
79 cond_resched();
80 goto repeat;
81 }
82 if (PageUptodate(page))
83 goto out;
84
85 fio.page = page;
86
87 if (f2fs_submit_page_bio(&fio)) {
88 f2fs_put_page(page, 1);
89 goto repeat;
90 }
91
92 lock_page(page);
93 if (unlikely(page->mapping != mapping)) {
94 f2fs_put_page(page, 1);
95 goto repeat;
96 }
97
98 /*
99 * if there is any IO error when accessing device, make our filesystem
100 * readonly and make sure do not write checkpoint with non-uptodate
101 * meta page.
102 */
103 if (unlikely(!PageUptodate(page)))
104 f2fs_stop_checkpoint(sbi, false);
105 out:
106 return page;
107 }
108
get_meta_page(struct f2fs_sb_info * sbi,pgoff_t index)109 struct page *get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
110 {
111 return __get_meta_page(sbi, index, true);
112 }
113
114 /* for POR only */
get_tmp_page(struct f2fs_sb_info * sbi,pgoff_t index)115 struct page *get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index)
116 {
117 return __get_meta_page(sbi, index, false);
118 }
119
is_valid_blkaddr(struct f2fs_sb_info * sbi,block_t blkaddr,int type)120 bool is_valid_blkaddr(struct f2fs_sb_info *sbi, block_t blkaddr, int type)
121 {
122 switch (type) {
123 case META_NAT:
124 break;
125 case META_SIT:
126 if (unlikely(blkaddr >= SIT_BLK_CNT(sbi)))
127 return false;
128 break;
129 case META_SSA:
130 if (unlikely(blkaddr >= MAIN_BLKADDR(sbi) ||
131 blkaddr < SM_I(sbi)->ssa_blkaddr))
132 return false;
133 break;
134 case META_CP:
135 if (unlikely(blkaddr >= SIT_I(sbi)->sit_base_addr ||
136 blkaddr < __start_cp_addr(sbi)))
137 return false;
138 break;
139 case META_POR:
140 if (unlikely(blkaddr >= MAX_BLKADDR(sbi) ||
141 blkaddr < MAIN_BLKADDR(sbi)))
142 return false;
143 break;
144 default:
145 BUG();
146 }
147
148 return true;
149 }
150
151 /*
152 * Readahead CP/NAT/SIT/SSA pages
153 */
ra_meta_pages(struct f2fs_sb_info * sbi,block_t start,int nrpages,int type,bool sync)154 int ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
155 int type, bool sync)
156 {
157 struct page *page;
158 block_t blkno = start;
159 struct f2fs_io_info fio = {
160 .sbi = sbi,
161 .type = META,
162 .op = REQ_OP_READ,
163 .op_flags = sync ? (REQ_META | REQ_PRIO) : REQ_RAHEAD,
164 .encrypted_page = NULL,
165 .in_list = false,
166 .is_meta = (type != META_POR),
167 };
168 struct blk_plug plug;
169
170 if (unlikely(type == META_POR))
171 fio.op_flags &= ~REQ_META;
172
173 blk_start_plug(&plug);
174 for (; nrpages-- > 0; blkno++) {
175
176 if (!is_valid_blkaddr(sbi, blkno, type))
177 goto out;
178
179 switch (type) {
180 case META_NAT:
181 if (unlikely(blkno >=
182 NAT_BLOCK_OFFSET(NM_I(sbi)->max_nid)))
183 blkno = 0;
184 /* get nat block addr */
185 fio.new_blkaddr = current_nat_addr(sbi,
186 blkno * NAT_ENTRY_PER_BLOCK);
187 break;
188 case META_SIT:
189 /* get sit block addr */
190 fio.new_blkaddr = current_sit_addr(sbi,
191 blkno * SIT_ENTRY_PER_BLOCK);
192 break;
193 case META_SSA:
194 case META_CP:
195 case META_POR:
196 fio.new_blkaddr = blkno;
197 break;
198 default:
199 BUG();
200 }
201
202 page = f2fs_grab_cache_page(META_MAPPING(sbi),
203 fio.new_blkaddr, false);
204 if (!page)
205 continue;
206 if (PageUptodate(page)) {
207 f2fs_put_page(page, 1);
208 continue;
209 }
210
211 fio.page = page;
212 f2fs_submit_page_bio(&fio);
213 f2fs_put_page(page, 0);
214 }
215 out:
216 blk_finish_plug(&plug);
217 return blkno - start;
218 }
219
ra_meta_pages_cond(struct f2fs_sb_info * sbi,pgoff_t index)220 void ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index)
221 {
222 struct page *page;
223 bool readahead = false;
224
225 page = find_get_page(META_MAPPING(sbi), index);
226 if (!page || !PageUptodate(page))
227 readahead = true;
228 f2fs_put_page(page, 0);
229
230 if (readahead)
231 ra_meta_pages(sbi, index, BIO_MAX_PAGES, META_POR, true);
232 }
233
__f2fs_write_meta_page(struct page * page,struct writeback_control * wbc,enum iostat_type io_type)234 static int __f2fs_write_meta_page(struct page *page,
235 struct writeback_control *wbc,
236 enum iostat_type io_type)
237 {
238 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
239
240 trace_f2fs_writepage(page, META);
241
242 if (unlikely(f2fs_cp_error(sbi))) {
243 dec_page_count(sbi, F2FS_DIRTY_META);
244 unlock_page(page);
245 return 0;
246 }
247 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
248 goto redirty_out;
249 if (wbc->for_reclaim && page->index < GET_SUM_BLOCK(sbi, 0))
250 goto redirty_out;
251
252 write_meta_page(sbi, page, io_type);
253 dec_page_count(sbi, F2FS_DIRTY_META);
254
255 if (wbc->for_reclaim)
256 f2fs_submit_merged_write_cond(sbi, page->mapping->host,
257 0, page->index, META);
258
259 unlock_page(page);
260
261 if (unlikely(f2fs_cp_error(sbi)))
262 f2fs_submit_merged_write(sbi, META);
263
264 return 0;
265
266 redirty_out:
267 redirty_page_for_writepage(wbc, page);
268 return AOP_WRITEPAGE_ACTIVATE;
269 }
270
f2fs_write_meta_page(struct page * page,struct writeback_control * wbc)271 static int f2fs_write_meta_page(struct page *page,
272 struct writeback_control *wbc)
273 {
274 return __f2fs_write_meta_page(page, wbc, FS_META_IO);
275 }
276
f2fs_write_meta_pages(struct address_space * mapping,struct writeback_control * wbc)277 static int f2fs_write_meta_pages(struct address_space *mapping,
278 struct writeback_control *wbc)
279 {
280 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
281 long diff, written;
282
283 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
284 goto skip_write;
285
286 /* collect a number of dirty meta pages and write together */
287 if (wbc->for_kupdate ||
288 get_pages(sbi, F2FS_DIRTY_META) < nr_pages_to_skip(sbi, META))
289 goto skip_write;
290
291 /* if locked failed, cp will flush dirty pages instead */
292 if (!mutex_trylock(&sbi->cp_mutex))
293 goto skip_write;
294
295 trace_f2fs_writepages(mapping->host, wbc, META);
296 diff = nr_pages_to_write(sbi, META, wbc);
297 written = sync_meta_pages(sbi, META, wbc->nr_to_write, FS_META_IO);
298 mutex_unlock(&sbi->cp_mutex);
299 wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff);
300 return 0;
301
302 skip_write:
303 wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_META);
304 trace_f2fs_writepages(mapping->host, wbc, META);
305 return 0;
306 }
307
sync_meta_pages(struct f2fs_sb_info * sbi,enum page_type type,long nr_to_write,enum iostat_type io_type)308 long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
309 long nr_to_write, enum iostat_type io_type)
310 {
311 struct address_space *mapping = META_MAPPING(sbi);
312 pgoff_t index = 0, end = ULONG_MAX, prev = ULONG_MAX;
313 struct pagevec pvec;
314 long nwritten = 0;
315 struct writeback_control wbc = {
316 .for_reclaim = 0,
317 };
318 struct blk_plug plug;
319
320 pagevec_init(&pvec, 0);
321
322 blk_start_plug(&plug);
323
324 while (index <= end) {
325 int i, nr_pages;
326 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
327 PAGECACHE_TAG_DIRTY,
328 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
329 if (unlikely(nr_pages == 0))
330 break;
331
332 for (i = 0; i < nr_pages; i++) {
333 struct page *page = pvec.pages[i];
334
335 if (prev == ULONG_MAX)
336 prev = page->index - 1;
337 if (nr_to_write != LONG_MAX && page->index != prev + 1) {
338 pagevec_release(&pvec);
339 goto stop;
340 }
341
342 lock_page(page);
343
344 if (unlikely(page->mapping != mapping)) {
345 continue_unlock:
346 unlock_page(page);
347 continue;
348 }
349 if (!PageDirty(page)) {
350 /* someone wrote it for us */
351 goto continue_unlock;
352 }
353
354 f2fs_wait_on_page_writeback(page, META, true);
355
356 BUG_ON(PageWriteback(page));
357 if (!clear_page_dirty_for_io(page))
358 goto continue_unlock;
359
360 if (__f2fs_write_meta_page(page, &wbc, io_type)) {
361 unlock_page(page);
362 break;
363 }
364 nwritten++;
365 prev = page->index;
366 if (unlikely(nwritten >= nr_to_write))
367 break;
368 }
369 pagevec_release(&pvec);
370 cond_resched();
371 }
372 stop:
373 if (nwritten)
374 f2fs_submit_merged_write(sbi, type);
375
376 blk_finish_plug(&plug);
377
378 return nwritten;
379 }
380
f2fs_set_meta_page_dirty(struct page * page)381 static int f2fs_set_meta_page_dirty(struct page *page)
382 {
383 trace_f2fs_set_page_dirty(page, META);
384
385 if (!PageUptodate(page))
386 SetPageUptodate(page);
387 if (!PageDirty(page)) {
388 __set_page_dirty_nobuffers(page);
389 inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_META);
390 SetPagePrivate(page);
391 f2fs_trace_pid(page);
392 return 1;
393 }
394 return 0;
395 }
396
397 const struct address_space_operations f2fs_meta_aops = {
398 .writepage = f2fs_write_meta_page,
399 .writepages = f2fs_write_meta_pages,
400 .set_page_dirty = f2fs_set_meta_page_dirty,
401 .invalidatepage = f2fs_invalidate_page,
402 .releasepage = f2fs_release_page,
403 #ifdef CONFIG_MIGRATION
404 .migratepage = f2fs_migrate_page,
405 #endif
406 };
407
__add_ino_entry(struct f2fs_sb_info * sbi,nid_t ino,unsigned int devidx,int type)408 static void __add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino,
409 unsigned int devidx, int type)
410 {
411 struct inode_management *im = &sbi->im[type];
412 struct ino_entry *e, *tmp;
413
414 tmp = f2fs_kmem_cache_alloc(ino_entry_slab, GFP_NOFS);
415
416 radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
417
418 spin_lock(&im->ino_lock);
419 e = radix_tree_lookup(&im->ino_root, ino);
420 if (!e) {
421 e = tmp;
422 if (unlikely(radix_tree_insert(&im->ino_root, ino, e)))
423 f2fs_bug_on(sbi, 1);
424
425 memset(e, 0, sizeof(struct ino_entry));
426 e->ino = ino;
427
428 list_add_tail(&e->list, &im->ino_list);
429 if (type != ORPHAN_INO)
430 im->ino_num++;
431 }
432
433 if (type == FLUSH_INO)
434 f2fs_set_bit(devidx, (char *)&e->dirty_device);
435
436 spin_unlock(&im->ino_lock);
437 radix_tree_preload_end();
438
439 if (e != tmp)
440 kmem_cache_free(ino_entry_slab, tmp);
441 }
442
__remove_ino_entry(struct f2fs_sb_info * sbi,nid_t ino,int type)443 static void __remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
444 {
445 struct inode_management *im = &sbi->im[type];
446 struct ino_entry *e;
447
448 spin_lock(&im->ino_lock);
449 e = radix_tree_lookup(&im->ino_root, ino);
450 if (e) {
451 list_del(&e->list);
452 radix_tree_delete(&im->ino_root, ino);
453 im->ino_num--;
454 spin_unlock(&im->ino_lock);
455 kmem_cache_free(ino_entry_slab, e);
456 return;
457 }
458 spin_unlock(&im->ino_lock);
459 }
460
add_ino_entry(struct f2fs_sb_info * sbi,nid_t ino,int type)461 void add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
462 {
463 /* add new dirty ino entry into list */
464 __add_ino_entry(sbi, ino, 0, type);
465 }
466
remove_ino_entry(struct f2fs_sb_info * sbi,nid_t ino,int type)467 void remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
468 {
469 /* remove dirty ino entry from list */
470 __remove_ino_entry(sbi, ino, type);
471 }
472
473 /* mode should be APPEND_INO or UPDATE_INO */
exist_written_data(struct f2fs_sb_info * sbi,nid_t ino,int mode)474 bool exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode)
475 {
476 struct inode_management *im = &sbi->im[mode];
477 struct ino_entry *e;
478
479 spin_lock(&im->ino_lock);
480 e = radix_tree_lookup(&im->ino_root, ino);
481 spin_unlock(&im->ino_lock);
482 return e ? true : false;
483 }
484
release_ino_entry(struct f2fs_sb_info * sbi,bool all)485 void release_ino_entry(struct f2fs_sb_info *sbi, bool all)
486 {
487 struct ino_entry *e, *tmp;
488 int i;
489
490 for (i = all ? ORPHAN_INO : APPEND_INO; i < MAX_INO_ENTRY; i++) {
491 struct inode_management *im = &sbi->im[i];
492
493 spin_lock(&im->ino_lock);
494 list_for_each_entry_safe(e, tmp, &im->ino_list, list) {
495 list_del(&e->list);
496 radix_tree_delete(&im->ino_root, e->ino);
497 kmem_cache_free(ino_entry_slab, e);
498 im->ino_num--;
499 }
500 spin_unlock(&im->ino_lock);
501 }
502 }
503
set_dirty_device(struct f2fs_sb_info * sbi,nid_t ino,unsigned int devidx,int type)504 void set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
505 unsigned int devidx, int type)
506 {
507 __add_ino_entry(sbi, ino, devidx, type);
508 }
509
is_dirty_device(struct f2fs_sb_info * sbi,nid_t ino,unsigned int devidx,int type)510 bool is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
511 unsigned int devidx, int type)
512 {
513 struct inode_management *im = &sbi->im[type];
514 struct ino_entry *e;
515 bool is_dirty = false;
516
517 spin_lock(&im->ino_lock);
518 e = radix_tree_lookup(&im->ino_root, ino);
519 if (e && f2fs_test_bit(devidx, (char *)&e->dirty_device))
520 is_dirty = true;
521 spin_unlock(&im->ino_lock);
522 return is_dirty;
523 }
524
acquire_orphan_inode(struct f2fs_sb_info * sbi)525 int acquire_orphan_inode(struct f2fs_sb_info *sbi)
526 {
527 struct inode_management *im = &sbi->im[ORPHAN_INO];
528 int err = 0;
529
530 spin_lock(&im->ino_lock);
531
532 #ifdef CONFIG_F2FS_FAULT_INJECTION
533 if (time_to_inject(sbi, FAULT_ORPHAN)) {
534 spin_unlock(&im->ino_lock);
535 f2fs_show_injection_info(FAULT_ORPHAN);
536 return -ENOSPC;
537 }
538 #endif
539 if (unlikely(im->ino_num >= sbi->max_orphans))
540 err = -ENOSPC;
541 else
542 im->ino_num++;
543 spin_unlock(&im->ino_lock);
544
545 return err;
546 }
547
release_orphan_inode(struct f2fs_sb_info * sbi)548 void release_orphan_inode(struct f2fs_sb_info *sbi)
549 {
550 struct inode_management *im = &sbi->im[ORPHAN_INO];
551
552 spin_lock(&im->ino_lock);
553 f2fs_bug_on(sbi, im->ino_num == 0);
554 im->ino_num--;
555 spin_unlock(&im->ino_lock);
556 }
557
add_orphan_inode(struct inode * inode)558 void add_orphan_inode(struct inode *inode)
559 {
560 /* add new orphan ino entry into list */
561 __add_ino_entry(F2FS_I_SB(inode), inode->i_ino, 0, ORPHAN_INO);
562 update_inode_page(inode);
563 }
564
remove_orphan_inode(struct f2fs_sb_info * sbi,nid_t ino)565 void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
566 {
567 /* remove orphan entry from orphan list */
568 __remove_ino_entry(sbi, ino, ORPHAN_INO);
569 }
570
recover_orphan_inode(struct f2fs_sb_info * sbi,nid_t ino)571 static int recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
572 {
573 struct inode *inode;
574 struct node_info ni;
575 int err = acquire_orphan_inode(sbi);
576
577 if (err)
578 goto err_out;
579
580 __add_ino_entry(sbi, ino, 0, ORPHAN_INO);
581
582 inode = f2fs_iget_retry(sbi->sb, ino);
583 if (IS_ERR(inode)) {
584 /*
585 * there should be a bug that we can't find the entry
586 * to orphan inode.
587 */
588 f2fs_bug_on(sbi, PTR_ERR(inode) == -ENOENT);
589 return PTR_ERR(inode);
590 }
591
592 err = dquot_initialize(inode);
593 if (err)
594 goto err_out;
595
596 dquot_initialize(inode);
597 clear_nlink(inode);
598
599 /* truncate all the data during iput */
600 iput(inode);
601
602 get_node_info(sbi, ino, &ni);
603
604 /* ENOMEM was fully retried in f2fs_evict_inode. */
605 if (ni.blk_addr != NULL_ADDR) {
606 err = -EIO;
607 goto err_out;
608 }
609 __remove_ino_entry(sbi, ino, ORPHAN_INO);
610 return 0;
611
612 err_out:
613 set_sbi_flag(sbi, SBI_NEED_FSCK);
614 f2fs_msg(sbi->sb, KERN_WARNING,
615 "%s: orphan failed (ino=%x), run fsck to fix.",
616 __func__, ino);
617 return err;
618 }
619
recover_orphan_inodes(struct f2fs_sb_info * sbi)620 int recover_orphan_inodes(struct f2fs_sb_info *sbi)
621 {
622 block_t start_blk, orphan_blocks, i, j;
623 unsigned int s_flags = sbi->sb->s_flags;
624 int err = 0;
625 #ifdef CONFIG_QUOTA
626 int quota_enabled;
627 #endif
628
629 if (!is_set_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG))
630 return 0;
631
632 if (s_flags & MS_RDONLY) {
633 f2fs_msg(sbi->sb, KERN_INFO, "orphan cleanup on readonly fs");
634 sbi->sb->s_flags &= ~MS_RDONLY;
635 }
636
637 #ifdef CONFIG_QUOTA
638 /* Needed for iput() to work correctly and not trash data */
639 sbi->sb->s_flags |= MS_ACTIVE;
640
641 /* Turn on quotas so that they are updated correctly */
642 quota_enabled = f2fs_enable_quota_files(sbi, s_flags & MS_RDONLY);
643 #endif
644
645 start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi);
646 orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi);
647
648 ra_meta_pages(sbi, start_blk, orphan_blocks, META_CP, true);
649
650 for (i = 0; i < orphan_blocks; i++) {
651 struct page *page = get_meta_page(sbi, start_blk + i);
652 struct f2fs_orphan_block *orphan_blk;
653
654 orphan_blk = (struct f2fs_orphan_block *)page_address(page);
655 for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) {
656 nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
657 err = recover_orphan_inode(sbi, ino);
658 if (err) {
659 f2fs_put_page(page, 1);
660 goto out;
661 }
662 }
663 f2fs_put_page(page, 1);
664 }
665 /* clear Orphan Flag */
666 clear_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG);
667 out:
668 #ifdef CONFIG_QUOTA
669 /* Turn quotas off */
670 if (quota_enabled)
671 f2fs_quota_off_umount(sbi->sb);
672 #endif
673 sbi->sb->s_flags = s_flags; /* Restore MS_RDONLY status */
674
675 return err;
676 }
677
write_orphan_inodes(struct f2fs_sb_info * sbi,block_t start_blk)678 static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk)
679 {
680 struct list_head *head;
681 struct f2fs_orphan_block *orphan_blk = NULL;
682 unsigned int nentries = 0;
683 unsigned short index = 1;
684 unsigned short orphan_blocks;
685 struct page *page = NULL;
686 struct ino_entry *orphan = NULL;
687 struct inode_management *im = &sbi->im[ORPHAN_INO];
688
689 orphan_blocks = GET_ORPHAN_BLOCKS(im->ino_num);
690
691 /*
692 * we don't need to do spin_lock(&im->ino_lock) here, since all the
693 * orphan inode operations are covered under f2fs_lock_op().
694 * And, spin_lock should be avoided due to page operations below.
695 */
696 head = &im->ino_list;
697
698 /* loop for each orphan inode entry and write them in Jornal block */
699 list_for_each_entry(orphan, head, list) {
700 if (!page) {
701 page = grab_meta_page(sbi, start_blk++);
702 orphan_blk =
703 (struct f2fs_orphan_block *)page_address(page);
704 memset(orphan_blk, 0, sizeof(*orphan_blk));
705 }
706
707 orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino);
708
709 if (nentries == F2FS_ORPHANS_PER_BLOCK) {
710 /*
711 * an orphan block is full of 1020 entries,
712 * then we need to flush current orphan blocks
713 * and bring another one in memory
714 */
715 orphan_blk->blk_addr = cpu_to_le16(index);
716 orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
717 orphan_blk->entry_count = cpu_to_le32(nentries);
718 set_page_dirty(page);
719 f2fs_put_page(page, 1);
720 index++;
721 nentries = 0;
722 page = NULL;
723 }
724 }
725
726 if (page) {
727 orphan_blk->blk_addr = cpu_to_le16(index);
728 orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
729 orphan_blk->entry_count = cpu_to_le32(nentries);
730 set_page_dirty(page);
731 f2fs_put_page(page, 1);
732 }
733 }
734
get_checkpoint_version(struct f2fs_sb_info * sbi,block_t cp_addr,struct f2fs_checkpoint ** cp_block,struct page ** cp_page,unsigned long long * version)735 static int get_checkpoint_version(struct f2fs_sb_info *sbi, block_t cp_addr,
736 struct f2fs_checkpoint **cp_block, struct page **cp_page,
737 unsigned long long *version)
738 {
739 unsigned long blk_size = sbi->blocksize;
740 size_t crc_offset = 0;
741 __u32 crc = 0;
742
743 *cp_page = get_meta_page(sbi, cp_addr);
744 *cp_block = (struct f2fs_checkpoint *)page_address(*cp_page);
745
746 crc_offset = le32_to_cpu((*cp_block)->checksum_offset);
747 if (crc_offset > (blk_size - sizeof(__le32))) {
748 f2fs_msg(sbi->sb, KERN_WARNING,
749 "invalid crc_offset: %zu", crc_offset);
750 return -EINVAL;
751 }
752
753 crc = cur_cp_crc(*cp_block);
754 if (!f2fs_crc_valid(sbi, crc, *cp_block, crc_offset)) {
755 f2fs_msg(sbi->sb, KERN_WARNING, "invalid crc value");
756 return -EINVAL;
757 }
758
759 *version = cur_cp_version(*cp_block);
760 return 0;
761 }
762
validate_checkpoint(struct f2fs_sb_info * sbi,block_t cp_addr,unsigned long long * version)763 static struct page *validate_checkpoint(struct f2fs_sb_info *sbi,
764 block_t cp_addr, unsigned long long *version)
765 {
766 struct page *cp_page_1 = NULL, *cp_page_2 = NULL;
767 struct f2fs_checkpoint *cp_block = NULL;
768 unsigned long long cur_version = 0, pre_version = 0;
769 int err;
770
771 err = get_checkpoint_version(sbi, cp_addr, &cp_block,
772 &cp_page_1, version);
773 if (err)
774 goto invalid_cp1;
775 pre_version = *version;
776
777 cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1;
778 err = get_checkpoint_version(sbi, cp_addr, &cp_block,
779 &cp_page_2, version);
780 if (err)
781 goto invalid_cp2;
782 cur_version = *version;
783
784 if (cur_version == pre_version) {
785 *version = cur_version;
786 f2fs_put_page(cp_page_2, 1);
787 return cp_page_1;
788 }
789 invalid_cp2:
790 f2fs_put_page(cp_page_2, 1);
791 invalid_cp1:
792 f2fs_put_page(cp_page_1, 1);
793 return NULL;
794 }
795
get_valid_checkpoint(struct f2fs_sb_info * sbi)796 int get_valid_checkpoint(struct f2fs_sb_info *sbi)
797 {
798 struct f2fs_checkpoint *cp_block;
799 struct f2fs_super_block *fsb = sbi->raw_super;
800 struct page *cp1, *cp2, *cur_page;
801 unsigned long blk_size = sbi->blocksize;
802 unsigned long long cp1_version = 0, cp2_version = 0;
803 unsigned long long cp_start_blk_no;
804 unsigned int cp_blks = 1 + __cp_payload(sbi);
805 block_t cp_blk_no;
806 int i;
807
808 sbi->ckpt = f2fs_kzalloc(sbi, cp_blks * blk_size, GFP_KERNEL);
809 if (!sbi->ckpt)
810 return -ENOMEM;
811 /*
812 * Finding out valid cp block involves read both
813 * sets( cp pack1 and cp pack 2)
814 */
815 cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr);
816 cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version);
817
818 /* The second checkpoint pack should start at the next segment */
819 cp_start_blk_no += ((unsigned long long)1) <<
820 le32_to_cpu(fsb->log_blocks_per_seg);
821 cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version);
822
823 if (cp1 && cp2) {
824 if (ver_after(cp2_version, cp1_version))
825 cur_page = cp2;
826 else
827 cur_page = cp1;
828 } else if (cp1) {
829 cur_page = cp1;
830 } else if (cp2) {
831 cur_page = cp2;
832 } else {
833 goto fail_no_cp;
834 }
835
836 cp_block = (struct f2fs_checkpoint *)page_address(cur_page);
837 memcpy(sbi->ckpt, cp_block, blk_size);
838
839 /* Sanity checking of checkpoint */
840 if (sanity_check_ckpt(sbi))
841 goto free_fail_no_cp;
842
843 if (cur_page == cp1)
844 sbi->cur_cp_pack = 1;
845 else
846 sbi->cur_cp_pack = 2;
847
848 if (cp_blks <= 1)
849 goto done;
850
851 cp_blk_no = le32_to_cpu(fsb->cp_blkaddr);
852 if (cur_page == cp2)
853 cp_blk_no += 1 << le32_to_cpu(fsb->log_blocks_per_seg);
854
855 for (i = 1; i < cp_blks; i++) {
856 void *sit_bitmap_ptr;
857 unsigned char *ckpt = (unsigned char *)sbi->ckpt;
858
859 cur_page = get_meta_page(sbi, cp_blk_no + i);
860 sit_bitmap_ptr = page_address(cur_page);
861 memcpy(ckpt + i * blk_size, sit_bitmap_ptr, blk_size);
862 f2fs_put_page(cur_page, 1);
863 }
864 done:
865 f2fs_put_page(cp1, 1);
866 f2fs_put_page(cp2, 1);
867 return 0;
868
869 free_fail_no_cp:
870 f2fs_put_page(cp1, 1);
871 f2fs_put_page(cp2, 1);
872 fail_no_cp:
873 kfree(sbi->ckpt);
874 return -EINVAL;
875 }
876
__add_dirty_inode(struct inode * inode,enum inode_type type)877 static void __add_dirty_inode(struct inode *inode, enum inode_type type)
878 {
879 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
880 int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
881
882 if (is_inode_flag_set(inode, flag))
883 return;
884
885 set_inode_flag(inode, flag);
886 if (!f2fs_is_volatile_file(inode))
887 list_add_tail(&F2FS_I(inode)->dirty_list,
888 &sbi->inode_list[type]);
889 stat_inc_dirty_inode(sbi, type);
890 }
891
__remove_dirty_inode(struct inode * inode,enum inode_type type)892 static void __remove_dirty_inode(struct inode *inode, enum inode_type type)
893 {
894 int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
895
896 if (get_dirty_pages(inode) || !is_inode_flag_set(inode, flag))
897 return;
898
899 list_del_init(&F2FS_I(inode)->dirty_list);
900 clear_inode_flag(inode, flag);
901 stat_dec_dirty_inode(F2FS_I_SB(inode), type);
902 }
903
update_dirty_page(struct inode * inode,struct page * page)904 void update_dirty_page(struct inode *inode, struct page *page)
905 {
906 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
907 enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
908
909 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
910 !S_ISLNK(inode->i_mode))
911 return;
912
913 spin_lock(&sbi->inode_lock[type]);
914 if (type != FILE_INODE || test_opt(sbi, DATA_FLUSH))
915 __add_dirty_inode(inode, type);
916 inode_inc_dirty_pages(inode);
917 spin_unlock(&sbi->inode_lock[type]);
918
919 SetPagePrivate(page);
920 f2fs_trace_pid(page);
921 }
922
remove_dirty_inode(struct inode * inode)923 void remove_dirty_inode(struct inode *inode)
924 {
925 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
926 enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
927
928 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
929 !S_ISLNK(inode->i_mode))
930 return;
931
932 if (type == FILE_INODE && !test_opt(sbi, DATA_FLUSH))
933 return;
934
935 spin_lock(&sbi->inode_lock[type]);
936 __remove_dirty_inode(inode, type);
937 spin_unlock(&sbi->inode_lock[type]);
938 }
939
sync_dirty_inodes(struct f2fs_sb_info * sbi,enum inode_type type)940 int sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type)
941 {
942 struct list_head *head;
943 struct inode *inode;
944 struct f2fs_inode_info *fi;
945 bool is_dir = (type == DIR_INODE);
946 unsigned long ino = 0;
947
948 trace_f2fs_sync_dirty_inodes_enter(sbi->sb, is_dir,
949 get_pages(sbi, is_dir ?
950 F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
951 retry:
952 if (unlikely(f2fs_cp_error(sbi)))
953 return -EIO;
954
955 spin_lock(&sbi->inode_lock[type]);
956
957 head = &sbi->inode_list[type];
958 if (list_empty(head)) {
959 spin_unlock(&sbi->inode_lock[type]);
960 trace_f2fs_sync_dirty_inodes_exit(sbi->sb, is_dir,
961 get_pages(sbi, is_dir ?
962 F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
963 return 0;
964 }
965 fi = list_first_entry(head, struct f2fs_inode_info, dirty_list);
966 inode = igrab(&fi->vfs_inode);
967 spin_unlock(&sbi->inode_lock[type]);
968 if (inode) {
969 unsigned long cur_ino = inode->i_ino;
970
971 if (is_dir)
972 F2FS_I(inode)->cp_task = current;
973
974 filemap_fdatawrite(inode->i_mapping);
975
976 if (is_dir)
977 F2FS_I(inode)->cp_task = NULL;
978
979 iput(inode);
980 /* We need to give cpu to another writers. */
981 if (ino == cur_ino) {
982 congestion_wait(BLK_RW_ASYNC, HZ/50);
983 cond_resched();
984 } else {
985 ino = cur_ino;
986 }
987 } else {
988 /*
989 * We should submit bio, since it exists several
990 * wribacking dentry pages in the freeing inode.
991 */
992 f2fs_submit_merged_write(sbi, DATA);
993 cond_resched();
994 }
995 goto retry;
996 }
997
f2fs_sync_inode_meta(struct f2fs_sb_info * sbi)998 int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi)
999 {
1000 struct list_head *head = &sbi->inode_list[DIRTY_META];
1001 struct inode *inode;
1002 struct f2fs_inode_info *fi;
1003 s64 total = get_pages(sbi, F2FS_DIRTY_IMETA);
1004
1005 while (total--) {
1006 if (unlikely(f2fs_cp_error(sbi)))
1007 return -EIO;
1008
1009 spin_lock(&sbi->inode_lock[DIRTY_META]);
1010 if (list_empty(head)) {
1011 spin_unlock(&sbi->inode_lock[DIRTY_META]);
1012 return 0;
1013 }
1014 fi = list_first_entry(head, struct f2fs_inode_info,
1015 gdirty_list);
1016 inode = igrab(&fi->vfs_inode);
1017 spin_unlock(&sbi->inode_lock[DIRTY_META]);
1018 if (inode) {
1019 sync_inode_metadata(inode, 0);
1020
1021 /* it's on eviction */
1022 if (is_inode_flag_set(inode, FI_DIRTY_INODE))
1023 update_inode_page(inode);
1024 iput(inode);
1025 }
1026 }
1027 return 0;
1028 }
1029
__prepare_cp_block(struct f2fs_sb_info * sbi)1030 static void __prepare_cp_block(struct f2fs_sb_info *sbi)
1031 {
1032 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1033 struct f2fs_nm_info *nm_i = NM_I(sbi);
1034 nid_t last_nid = nm_i->next_scan_nid;
1035
1036 next_free_nid(sbi, &last_nid);
1037 ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi));
1038 ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi));
1039 ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi));
1040 ckpt->next_free_nid = cpu_to_le32(last_nid);
1041 }
1042
1043 /*
1044 * Freeze all the FS-operations for checkpoint.
1045 */
block_operations(struct f2fs_sb_info * sbi)1046 static int block_operations(struct f2fs_sb_info *sbi)
1047 {
1048 struct writeback_control wbc = {
1049 .sync_mode = WB_SYNC_ALL,
1050 .nr_to_write = LONG_MAX,
1051 .for_reclaim = 0,
1052 };
1053 struct blk_plug plug;
1054 int err = 0;
1055
1056 blk_start_plug(&plug);
1057
1058 retry_flush_dents:
1059 f2fs_lock_all(sbi);
1060 /* write all the dirty dentry pages */
1061 if (get_pages(sbi, F2FS_DIRTY_DENTS)) {
1062 f2fs_unlock_all(sbi);
1063 err = sync_dirty_inodes(sbi, DIR_INODE);
1064 if (err)
1065 goto out;
1066 cond_resched();
1067 goto retry_flush_dents;
1068 }
1069
1070 /*
1071 * POR: we should ensure that there are no dirty node pages
1072 * until finishing nat/sit flush. inode->i_blocks can be updated.
1073 */
1074 down_write(&sbi->node_change);
1075
1076 if (get_pages(sbi, F2FS_DIRTY_IMETA)) {
1077 up_write(&sbi->node_change);
1078 f2fs_unlock_all(sbi);
1079 err = f2fs_sync_inode_meta(sbi);
1080 if (err)
1081 goto out;
1082 cond_resched();
1083 goto retry_flush_dents;
1084 }
1085
1086 retry_flush_nodes:
1087 down_write(&sbi->node_write);
1088
1089 if (get_pages(sbi, F2FS_DIRTY_NODES)) {
1090 up_write(&sbi->node_write);
1091 err = sync_node_pages(sbi, &wbc, false, FS_CP_NODE_IO);
1092 if (err) {
1093 up_write(&sbi->node_change);
1094 f2fs_unlock_all(sbi);
1095 goto out;
1096 }
1097 cond_resched();
1098 goto retry_flush_nodes;
1099 }
1100
1101 /*
1102 * sbi->node_change is used only for AIO write_begin path which produces
1103 * dirty node blocks and some checkpoint values by block allocation.
1104 */
1105 __prepare_cp_block(sbi);
1106 up_write(&sbi->node_change);
1107 out:
1108 blk_finish_plug(&plug);
1109 return err;
1110 }
1111
unblock_operations(struct f2fs_sb_info * sbi)1112 static void unblock_operations(struct f2fs_sb_info *sbi)
1113 {
1114 up_write(&sbi->node_write);
1115 f2fs_unlock_all(sbi);
1116 }
1117
wait_on_all_pages_writeback(struct f2fs_sb_info * sbi)1118 static void wait_on_all_pages_writeback(struct f2fs_sb_info *sbi)
1119 {
1120 DEFINE_WAIT(wait);
1121
1122 for (;;) {
1123 prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE);
1124
1125 if (!get_pages(sbi, F2FS_WB_CP_DATA))
1126 break;
1127
1128 io_schedule_timeout(5*HZ);
1129 }
1130 finish_wait(&sbi->cp_wait, &wait);
1131 }
1132
update_ckpt_flags(struct f2fs_sb_info * sbi,struct cp_control * cpc)1133 static void update_ckpt_flags(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1134 {
1135 unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num;
1136 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1137 unsigned long flags;
1138
1139 spin_lock_irqsave(&sbi->cp_lock, flags);
1140
1141 if ((cpc->reason & CP_UMOUNT) &&
1142 le32_to_cpu(ckpt->cp_pack_total_block_count) >
1143 sbi->blocks_per_seg - NM_I(sbi)->nat_bits_blocks)
1144 disable_nat_bits(sbi, false);
1145
1146 if (cpc->reason & CP_TRIMMED)
1147 __set_ckpt_flags(ckpt, CP_TRIMMED_FLAG);
1148 else
1149 __clear_ckpt_flags(ckpt, CP_TRIMMED_FLAG);
1150
1151 if (cpc->reason & CP_UMOUNT)
1152 __set_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
1153 else
1154 __clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
1155
1156 if (cpc->reason & CP_FASTBOOT)
1157 __set_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
1158 else
1159 __clear_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
1160
1161 if (orphan_num)
1162 __set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
1163 else
1164 __clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
1165
1166 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK))
1167 __set_ckpt_flags(ckpt, CP_FSCK_FLAG);
1168
1169 /* set this flag to activate crc|cp_ver for recovery */
1170 __set_ckpt_flags(ckpt, CP_CRC_RECOVERY_FLAG);
1171 __clear_ckpt_flags(ckpt, CP_NOCRC_RECOVERY_FLAG);
1172
1173 spin_unlock_irqrestore(&sbi->cp_lock, flags);
1174 }
1175
commit_checkpoint(struct f2fs_sb_info * sbi,void * src,block_t blk_addr)1176 static void commit_checkpoint(struct f2fs_sb_info *sbi,
1177 void *src, block_t blk_addr)
1178 {
1179 struct writeback_control wbc = {
1180 .for_reclaim = 0,
1181 };
1182
1183 /*
1184 * pagevec_lookup_tag and lock_page again will take
1185 * some extra time. Therefore, update_meta_pages and
1186 * sync_meta_pages are combined in this function.
1187 */
1188 struct page *page = grab_meta_page(sbi, blk_addr);
1189 int err;
1190
1191 memcpy(page_address(page), src, PAGE_SIZE);
1192 set_page_dirty(page);
1193
1194 f2fs_wait_on_page_writeback(page, META, true);
1195 f2fs_bug_on(sbi, PageWriteback(page));
1196 if (unlikely(!clear_page_dirty_for_io(page)))
1197 f2fs_bug_on(sbi, 1);
1198
1199 /* writeout cp pack 2 page */
1200 err = __f2fs_write_meta_page(page, &wbc, FS_CP_META_IO);
1201 f2fs_bug_on(sbi, err);
1202
1203 f2fs_put_page(page, 0);
1204
1205 /* submit checkpoint (with barrier if NOBARRIER is not set) */
1206 f2fs_submit_merged_write(sbi, META_FLUSH);
1207 }
1208
do_checkpoint(struct f2fs_sb_info * sbi,struct cp_control * cpc)1209 static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1210 {
1211 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1212 struct f2fs_nm_info *nm_i = NM_I(sbi);
1213 unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num, flags;
1214 block_t start_blk;
1215 unsigned int data_sum_blocks, orphan_blocks;
1216 __u32 crc32 = 0;
1217 int i;
1218 int cp_payload_blks = __cp_payload(sbi);
1219 struct super_block *sb = sbi->sb;
1220 struct curseg_info *seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
1221 u64 kbytes_written;
1222 int err;
1223
1224 /* Flush all the NAT/SIT pages */
1225 while (get_pages(sbi, F2FS_DIRTY_META)) {
1226 sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO);
1227 if (unlikely(f2fs_cp_error(sbi)))
1228 return -EIO;
1229 }
1230
1231 /*
1232 * modify checkpoint
1233 * version number is already updated
1234 */
1235 ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi));
1236 ckpt->free_segment_count = cpu_to_le32(free_segments(sbi));
1237 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
1238 ckpt->cur_node_segno[i] =
1239 cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE));
1240 ckpt->cur_node_blkoff[i] =
1241 cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE));
1242 ckpt->alloc_type[i + CURSEG_HOT_NODE] =
1243 curseg_alloc_type(sbi, i + CURSEG_HOT_NODE);
1244 }
1245 for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
1246 ckpt->cur_data_segno[i] =
1247 cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA));
1248 ckpt->cur_data_blkoff[i] =
1249 cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA));
1250 ckpt->alloc_type[i + CURSEG_HOT_DATA] =
1251 curseg_alloc_type(sbi, i + CURSEG_HOT_DATA);
1252 }
1253
1254 /* 2 cp + n data seg summary + orphan inode blocks */
1255 data_sum_blocks = npages_for_summary_flush(sbi, false);
1256 spin_lock_irqsave(&sbi->cp_lock, flags);
1257 if (data_sum_blocks < NR_CURSEG_DATA_TYPE)
1258 __set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
1259 else
1260 __clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
1261 spin_unlock_irqrestore(&sbi->cp_lock, flags);
1262
1263 orphan_blocks = GET_ORPHAN_BLOCKS(orphan_num);
1264 ckpt->cp_pack_start_sum = cpu_to_le32(1 + cp_payload_blks +
1265 orphan_blocks);
1266
1267 if (__remain_node_summaries(cpc->reason))
1268 ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS+
1269 cp_payload_blks + data_sum_blocks +
1270 orphan_blocks + NR_CURSEG_NODE_TYPE);
1271 else
1272 ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS +
1273 cp_payload_blks + data_sum_blocks +
1274 orphan_blocks);
1275
1276 /* update ckpt flag for checkpoint */
1277 update_ckpt_flags(sbi, cpc);
1278
1279 /* update SIT/NAT bitmap */
1280 get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP));
1281 get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP));
1282
1283 crc32 = f2fs_crc32(sbi, ckpt, le32_to_cpu(ckpt->checksum_offset));
1284 *((__le32 *)((unsigned char *)ckpt +
1285 le32_to_cpu(ckpt->checksum_offset)))
1286 = cpu_to_le32(crc32);
1287
1288 start_blk = __start_cp_next_addr(sbi);
1289
1290 /* write nat bits */
1291 if (enabled_nat_bits(sbi, cpc)) {
1292 __u64 cp_ver = cur_cp_version(ckpt);
1293 block_t blk;
1294
1295 cp_ver |= ((__u64)crc32 << 32);
1296 *(__le64 *)nm_i->nat_bits = cpu_to_le64(cp_ver);
1297
1298 blk = start_blk + sbi->blocks_per_seg - nm_i->nat_bits_blocks;
1299 for (i = 0; i < nm_i->nat_bits_blocks; i++)
1300 update_meta_page(sbi, nm_i->nat_bits +
1301 (i << F2FS_BLKSIZE_BITS), blk + i);
1302
1303 /* Flush all the NAT BITS pages */
1304 while (get_pages(sbi, F2FS_DIRTY_META)) {
1305 sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO);
1306 if (unlikely(f2fs_cp_error(sbi)))
1307 return -EIO;
1308 }
1309 }
1310
1311 /* write out checkpoint buffer at block 0 */
1312 update_meta_page(sbi, ckpt, start_blk++);
1313
1314 for (i = 1; i < 1 + cp_payload_blks; i++)
1315 update_meta_page(sbi, (char *)ckpt + i * F2FS_BLKSIZE,
1316 start_blk++);
1317
1318 if (orphan_num) {
1319 write_orphan_inodes(sbi, start_blk);
1320 start_blk += orphan_blocks;
1321 }
1322
1323 write_data_summaries(sbi, start_blk);
1324 start_blk += data_sum_blocks;
1325
1326 /* Record write statistics in the hot node summary */
1327 kbytes_written = sbi->kbytes_written;
1328 if (sb->s_bdev->bd_part)
1329 kbytes_written += BD_PART_WRITTEN(sbi);
1330
1331 seg_i->journal->info.kbytes_written = cpu_to_le64(kbytes_written);
1332
1333 if (__remain_node_summaries(cpc->reason)) {
1334 write_node_summaries(sbi, start_blk);
1335 start_blk += NR_CURSEG_NODE_TYPE;
1336 }
1337
1338 /* update user_block_counts */
1339 sbi->last_valid_block_count = sbi->total_valid_block_count;
1340 percpu_counter_set(&sbi->alloc_valid_block_count, 0);
1341
1342 /* Here, we have one bio having CP pack except cp pack 2 page */
1343 sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO);
1344
1345 /* wait for previous submitted meta pages writeback */
1346 wait_on_all_pages_writeback(sbi);
1347
1348 if (unlikely(f2fs_cp_error(sbi)))
1349 return -EIO;
1350
1351 /* flush all device cache */
1352 err = f2fs_flush_device_cache(sbi);
1353 if (err)
1354 return err;
1355
1356 /* barrier and flush checkpoint cp pack 2 page if it can */
1357 commit_checkpoint(sbi, ckpt, start_blk);
1358 wait_on_all_pages_writeback(sbi);
1359
1360 release_ino_entry(sbi, false);
1361
1362 if (unlikely(f2fs_cp_error(sbi)))
1363 return -EIO;
1364
1365 clear_sbi_flag(sbi, SBI_IS_DIRTY);
1366 clear_sbi_flag(sbi, SBI_NEED_CP);
1367 __set_cp_next_pack(sbi);
1368
1369 /*
1370 * redirty superblock if metadata like node page or inode cache is
1371 * updated during writing checkpoint.
1372 */
1373 if (get_pages(sbi, F2FS_DIRTY_NODES) ||
1374 get_pages(sbi, F2FS_DIRTY_IMETA))
1375 set_sbi_flag(sbi, SBI_IS_DIRTY);
1376
1377 f2fs_bug_on(sbi, get_pages(sbi, F2FS_DIRTY_DENTS));
1378
1379 return 0;
1380 }
1381
1382 /*
1383 * We guarantee that this checkpoint procedure will not fail.
1384 */
write_checkpoint(struct f2fs_sb_info * sbi,struct cp_control * cpc)1385 int write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1386 {
1387 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1388 unsigned long long ckpt_ver;
1389 int err = 0;
1390
1391 mutex_lock(&sbi->cp_mutex);
1392
1393 if (!is_sbi_flag_set(sbi, SBI_IS_DIRTY) &&
1394 ((cpc->reason & CP_FASTBOOT) || (cpc->reason & CP_SYNC) ||
1395 ((cpc->reason & CP_DISCARD) && !sbi->discard_blks)))
1396 goto out;
1397 if (unlikely(f2fs_cp_error(sbi))) {
1398 err = -EIO;
1399 goto out;
1400 }
1401 if (f2fs_readonly(sbi->sb)) {
1402 err = -EROFS;
1403 goto out;
1404 }
1405
1406 trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "start block_ops");
1407
1408 err = block_operations(sbi);
1409 if (err)
1410 goto out;
1411
1412 trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish block_ops");
1413
1414 f2fs_flush_merged_writes(sbi);
1415
1416 /* this is the case of multiple fstrims without any changes */
1417 if (cpc->reason & CP_DISCARD) {
1418 if (!exist_trim_candidates(sbi, cpc)) {
1419 unblock_operations(sbi);
1420 goto out;
1421 }
1422
1423 if (NM_I(sbi)->dirty_nat_cnt == 0 &&
1424 SIT_I(sbi)->dirty_sentries == 0 &&
1425 prefree_segments(sbi) == 0) {
1426 flush_sit_entries(sbi, cpc);
1427 clear_prefree_segments(sbi, cpc);
1428 unblock_operations(sbi);
1429 goto out;
1430 }
1431 }
1432
1433 /*
1434 * update checkpoint pack index
1435 * Increase the version number so that
1436 * SIT entries and seg summaries are written at correct place
1437 */
1438 ckpt_ver = cur_cp_version(ckpt);
1439 ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver);
1440
1441 /* write cached NAT/SIT entries to NAT/SIT area */
1442 flush_nat_entries(sbi, cpc);
1443 flush_sit_entries(sbi, cpc);
1444
1445 /* unlock all the fs_lock[] in do_checkpoint() */
1446 err = do_checkpoint(sbi, cpc);
1447 if (err)
1448 release_discard_addrs(sbi);
1449 else
1450 clear_prefree_segments(sbi, cpc);
1451
1452 unblock_operations(sbi);
1453 stat_inc_cp_count(sbi->stat_info);
1454
1455 if (cpc->reason & CP_RECOVERY)
1456 f2fs_msg(sbi->sb, KERN_NOTICE,
1457 "checkpoint: version = %llx", ckpt_ver);
1458
1459 /* do checkpoint periodically */
1460 f2fs_update_time(sbi, CP_TIME);
1461 trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish checkpoint");
1462 out:
1463 mutex_unlock(&sbi->cp_mutex);
1464 return err;
1465 }
1466
init_ino_entry_info(struct f2fs_sb_info * sbi)1467 void init_ino_entry_info(struct f2fs_sb_info *sbi)
1468 {
1469 int i;
1470
1471 for (i = 0; i < MAX_INO_ENTRY; i++) {
1472 struct inode_management *im = &sbi->im[i];
1473
1474 INIT_RADIX_TREE(&im->ino_root, GFP_ATOMIC);
1475 spin_lock_init(&im->ino_lock);
1476 INIT_LIST_HEAD(&im->ino_list);
1477 im->ino_num = 0;
1478 }
1479
1480 sbi->max_orphans = (sbi->blocks_per_seg - F2FS_CP_PACKS -
1481 NR_CURSEG_TYPE - __cp_payload(sbi)) *
1482 F2FS_ORPHANS_PER_BLOCK;
1483 }
1484
create_checkpoint_caches(void)1485 int __init create_checkpoint_caches(void)
1486 {
1487 ino_entry_slab = f2fs_kmem_cache_create("f2fs_ino_entry",
1488 sizeof(struct ino_entry));
1489 if (!ino_entry_slab)
1490 return -ENOMEM;
1491 inode_entry_slab = f2fs_kmem_cache_create("f2fs_inode_entry",
1492 sizeof(struct inode_entry));
1493 if (!inode_entry_slab) {
1494 kmem_cache_destroy(ino_entry_slab);
1495 return -ENOMEM;
1496 }
1497 return 0;
1498 }
1499
destroy_checkpoint_caches(void)1500 void destroy_checkpoint_caches(void)
1501 {
1502 kmem_cache_destroy(ino_entry_slab);
1503 kmem_cache_destroy(inode_entry_slab);
1504 }
1505