blob: 3891600499939895600bfc9aed37c6f2b5e1d53d [file] [log] [blame]
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001/*
Jaegeuk Kim127e6702012-11-02 17:08:18 +09002 * 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"
Jaegeuk Kim9e4ded32014-12-17 19:58:58 -080023#include "trace.h"
Namjae Jeon2af4bd62013-04-23 18:26:54 +090024#include <trace/events/f2fs.h>
Jaegeuk Kim127e6702012-11-02 17:08:18 +090025
Jaegeuk Kim6451e042014-07-25 15:47:17 -070026static struct kmem_cache *ino_entry_slab;
Chao Yu06292072014-12-29 15:56:18 +080027struct kmem_cache *inode_entry_slab;
Jaegeuk Kim127e6702012-11-02 17:08:18 +090028
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -070029void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io)
30{
31 set_ckpt_flags(sbi->ckpt, CP_ERROR_FLAG);
32 sbi->sb->s_flags |= MS_RDONLY;
33 if (!end_io)
34 f2fs_flush_merged_bios(sbi);
35}
36
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +090037/*
Jaegeuk Kim127e6702012-11-02 17:08:18 +090038 * We guarantee no failure on the returned page.
39 */
40struct page *grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
41{
Gu Zheng9df27d92014-01-20 18:37:04 +080042 struct address_space *mapping = META_MAPPING(sbi);
Jaegeuk Kim127e6702012-11-02 17:08:18 +090043 struct page *page = NULL;
44repeat:
Jaegeuk Kim300e1292016-04-29 16:11:53 -070045 page = f2fs_grab_cache_page(mapping, index, false);
Jaegeuk Kim127e6702012-11-02 17:08:18 +090046 if (!page) {
47 cond_resched();
48 goto repeat;
49 }
Jaegeuk Kimfec1d652016-01-20 23:43:51 +080050 f2fs_wait_on_page_writeback(page, META, true);
Jaegeuk Kim127e6702012-11-02 17:08:18 +090051 SetPageUptodate(page);
52 return page;
53}
54
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +090055/*
Jaegeuk Kim127e6702012-11-02 17:08:18 +090056 * We guarantee no failure on the returned page.
57 */
Chao Yu2b947002015-10-12 17:04:21 +080058static struct page *__get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index,
59 bool is_meta)
Jaegeuk Kim127e6702012-11-02 17:08:18 +090060{
Gu Zheng9df27d92014-01-20 18:37:04 +080061 struct address_space *mapping = META_MAPPING(sbi);
Jaegeuk Kim127e6702012-11-02 17:08:18 +090062 struct page *page;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -080063 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -070064 .sbi = sbi,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -080065 .type = META,
66 .rw = READ_SYNC | REQ_META | REQ_PRIO,
Chao Yu7a9d7542016-02-22 18:36:38 +080067 .old_blkaddr = index,
68 .new_blkaddr = index,
Jaegeuk Kim4375a332015-04-23 12:04:33 -070069 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -080070 };
Chao Yu2b947002015-10-12 17:04:21 +080071
72 if (unlikely(!is_meta))
73 fio.rw &= ~REQ_META;
Jaegeuk Kim127e6702012-11-02 17:08:18 +090074repeat:
Jaegeuk Kim300e1292016-04-29 16:11:53 -070075 page = f2fs_grab_cache_page(mapping, index, false);
Jaegeuk Kim127e6702012-11-02 17:08:18 +090076 if (!page) {
77 cond_resched();
78 goto repeat;
79 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +090080 if (PageUptodate(page))
81 goto out;
Jaegeuk Kim127e6702012-11-02 17:08:18 +090082
Jaegeuk Kim05ca3632015-04-23 14:38:15 -070083 fio.page = page;
84
Jaegeuk Kim86531d62015-07-15 13:08:21 -070085 if (f2fs_submit_page_bio(&fio)) {
86 f2fs_put_page(page, 1);
Jaegeuk Kim393ff912013-03-08 21:29:23 +090087 goto repeat;
Jaegeuk Kim86531d62015-07-15 13:08:21 -070088 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +090089
90 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090091 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +090092 f2fs_put_page(page, 1);
93 goto repeat;
94 }
Chao Yuf3f338c2015-07-29 17:33:13 +080095
96 /*
97 * if there is any IO error when accessing device, make our filesystem
98 * readonly and make sure do not write checkpoint with non-uptodate
99 * meta page.
100 */
101 if (unlikely(!PageUptodate(page)))
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -0700102 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900103out:
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900104 return page;
105}
106
Chao Yu2b947002015-10-12 17:04:21 +0800107struct page *get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
108{
109 return __get_meta_page(sbi, index, true);
110}
111
112/* for POR only */
113struct page *get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index)
114{
115 return __get_meta_page(sbi, index, false);
116}
117
Chao Yuf0c9cad2015-04-18 18:05:36 +0800118bool is_valid_blkaddr(struct f2fs_sb_info *sbi, block_t blkaddr, int type)
Chao Yu662befd2014-02-07 16:11:53 +0800119{
120 switch (type) {
121 case META_NAT:
Chao Yu66b00c12014-12-08 14:59:17 +0800122 break;
Chao Yu662befd2014-02-07 16:11:53 +0800123 case META_SIT:
Chao Yu66b00c12014-12-08 14:59:17 +0800124 if (unlikely(blkaddr >= SIT_BLK_CNT(sbi)))
125 return false;
126 break;
Chao Yu81c1a0f12014-02-27 19:12:24 +0800127 case META_SSA:
Chao Yu66b00c12014-12-08 14:59:17 +0800128 if (unlikely(blkaddr >= MAIN_BLKADDR(sbi) ||
129 blkaddr < SM_I(sbi)->ssa_blkaddr))
130 return false;
131 break;
Chao Yu662befd2014-02-07 16:11:53 +0800132 case META_CP:
Chao Yu66b00c12014-12-08 14:59:17 +0800133 if (unlikely(blkaddr >= SIT_I(sbi)->sit_base_addr ||
134 blkaddr < __start_cp_addr(sbi)))
135 return false;
136 break;
Jaegeuk Kim4c521f492014-09-11 13:49:55 -0700137 case META_POR:
Chao Yu66b00c12014-12-08 14:59:17 +0800138 if (unlikely(blkaddr >= MAX_BLKADDR(sbi) ||
139 blkaddr < MAIN_BLKADDR(sbi)))
140 return false;
141 break;
Chao Yu662befd2014-02-07 16:11:53 +0800142 default:
143 BUG();
144 }
Chao Yu66b00c12014-12-08 14:59:17 +0800145
146 return true;
Chao Yu662befd2014-02-07 16:11:53 +0800147}
148
149/*
Chao Yu81c1a0f12014-02-27 19:12:24 +0800150 * Readahead CP/NAT/SIT/SSA pages
Chao Yu662befd2014-02-07 16:11:53 +0800151 */
Chao Yu26879fb2015-10-12 17:05:59 +0800152int ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
153 int type, bool sync)
Chao Yu662befd2014-02-07 16:11:53 +0800154{
Chao Yu662befd2014-02-07 16:11:53 +0800155 struct page *page;
Jaegeuk Kim4c521f492014-09-11 13:49:55 -0700156 block_t blkno = start;
Chao Yu662befd2014-02-07 16:11:53 +0800157 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700158 .sbi = sbi,
Chao Yu662befd2014-02-07 16:11:53 +0800159 .type = META,
Chao Yu26879fb2015-10-12 17:05:59 +0800160 .rw = sync ? (READ_SYNC | REQ_META | REQ_PRIO) : READA,
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700161 .encrypted_page = NULL,
Chao Yu662befd2014-02-07 16:11:53 +0800162 };
Chao Yue9f5b8b2016-02-14 18:54:33 +0800163 struct blk_plug plug;
Chao Yu662befd2014-02-07 16:11:53 +0800164
Chao Yu2b947002015-10-12 17:04:21 +0800165 if (unlikely(type == META_POR))
166 fio.rw &= ~REQ_META;
167
Chao Yue9f5b8b2016-02-14 18:54:33 +0800168 blk_start_plug(&plug);
Chao Yu662befd2014-02-07 16:11:53 +0800169 for (; nrpages-- > 0; blkno++) {
Chao Yu662befd2014-02-07 16:11:53 +0800170
Chao Yu66b00c12014-12-08 14:59:17 +0800171 if (!is_valid_blkaddr(sbi, blkno, type))
172 goto out;
173
Chao Yu662befd2014-02-07 16:11:53 +0800174 switch (type) {
175 case META_NAT:
Chao Yu66b00c12014-12-08 14:59:17 +0800176 if (unlikely(blkno >=
177 NAT_BLOCK_OFFSET(NM_I(sbi)->max_nid)))
Chao Yu662befd2014-02-07 16:11:53 +0800178 blkno = 0;
Chao Yu66b00c12014-12-08 14:59:17 +0800179 /* get nat block addr */
Chao Yu7a9d7542016-02-22 18:36:38 +0800180 fio.new_blkaddr = current_nat_addr(sbi,
Chao Yu662befd2014-02-07 16:11:53 +0800181 blkno * NAT_ENTRY_PER_BLOCK);
182 break;
183 case META_SIT:
184 /* get sit block addr */
Chao Yu7a9d7542016-02-22 18:36:38 +0800185 fio.new_blkaddr = current_sit_addr(sbi,
Chao Yu662befd2014-02-07 16:11:53 +0800186 blkno * SIT_ENTRY_PER_BLOCK);
Chao Yu662befd2014-02-07 16:11:53 +0800187 break;
Chao Yu81c1a0f12014-02-27 19:12:24 +0800188 case META_SSA:
Chao Yu662befd2014-02-07 16:11:53 +0800189 case META_CP:
Jaegeuk Kim4c521f492014-09-11 13:49:55 -0700190 case META_POR:
Chao Yu7a9d7542016-02-22 18:36:38 +0800191 fio.new_blkaddr = blkno;
Chao Yu662befd2014-02-07 16:11:53 +0800192 break;
193 default:
194 BUG();
195 }
196
Jaegeuk Kim300e1292016-04-29 16:11:53 -0700197 page = f2fs_grab_cache_page(META_MAPPING(sbi),
198 fio.new_blkaddr, false);
Chao Yu662befd2014-02-07 16:11:53 +0800199 if (!page)
200 continue;
201 if (PageUptodate(page)) {
Chao Yu662befd2014-02-07 16:11:53 +0800202 f2fs_put_page(page, 1);
203 continue;
204 }
205
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700206 fio.page = page;
Chao Yu7a9d7542016-02-22 18:36:38 +0800207 fio.old_blkaddr = fio.new_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700208 f2fs_submit_page_mbio(&fio);
Chao Yu662befd2014-02-07 16:11:53 +0800209 f2fs_put_page(page, 0);
210 }
211out:
212 f2fs_submit_merged_bio(sbi, META, READ);
Chao Yue9f5b8b2016-02-14 18:54:33 +0800213 blk_finish_plug(&plug);
Chao Yu662befd2014-02-07 16:11:53 +0800214 return blkno - start;
215}
216
Chao Yu635aee12014-12-08 15:02:52 +0800217void ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index)
218{
219 struct page *page;
220 bool readahead = false;
221
222 page = find_get_page(META_MAPPING(sbi), index);
Jaegeuk Kim4da7bf52016-04-06 11:27:03 -0700223 if (!page || !PageUptodate(page))
Chao Yu635aee12014-12-08 15:02:52 +0800224 readahead = true;
225 f2fs_put_page(page, 0);
226
227 if (readahead)
Chao Yu26879fb2015-10-12 17:05:59 +0800228 ra_meta_pages(sbi, index, MAX_BIO_BLOCKS(sbi), META_POR, true);
Chao Yu635aee12014-12-08 15:02:52 +0800229}
230
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900231static int f2fs_write_meta_page(struct page *page,
232 struct writeback_control *wbc)
233{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700234 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900235
Chao Yuecda0de2014-05-06 16:48:26 +0800236 trace_f2fs_writepage(page, META);
237
Chao Yucaf00472015-01-28 17:48:42 +0800238 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
Chao Yucfb271d2013-12-05 17:15:22 +0800239 goto redirty_out;
Jaegeuk Kim857dc4e2014-11-19 11:03:34 -0800240 if (wbc->for_reclaim && page->index < GET_SUM_BLOCK(sbi, 0))
Chao Yucfb271d2013-12-05 17:15:22 +0800241 goto redirty_out;
Jaegeuk Kim1e968fd2014-08-11 16:49:25 -0700242 if (unlikely(f2fs_cp_error(sbi)))
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -0700243 goto redirty_out;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900244
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900245 write_meta_page(sbi, page);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900246 dec_page_count(sbi, F2FS_DIRTY_META);
Chao Yu0c3a5792016-01-18 18:28:11 +0800247
248 if (wbc->for_reclaim)
249 f2fs_submit_merged_bio_cond(sbi, NULL, page, 0, META, WRITE);
250
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900251 unlock_page(page);
Jaegeuk Kim857dc4e2014-11-19 11:03:34 -0800252
Chao Yu0c3a5792016-01-18 18:28:11 +0800253 if (unlikely(f2fs_cp_error(sbi)))
Jaegeuk Kim857dc4e2014-11-19 11:03:34 -0800254 f2fs_submit_merged_bio(sbi, META, WRITE);
Chao Yu0c3a5792016-01-18 18:28:11 +0800255
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900256 return 0;
Chao Yucfb271d2013-12-05 17:15:22 +0800257
258redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +0900259 redirty_page_for_writepage(wbc, page);
Chao Yucfb271d2013-12-05 17:15:22 +0800260 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900261}
262
263static int f2fs_write_meta_pages(struct address_space *mapping,
264 struct writeback_control *wbc)
265{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700266 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +0900267 long diff, written;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900268
Jaegeuk Kim5459aa92013-12-17 17:28:41 +0900269 /* collect a number of dirty meta pages and write together */
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +0900270 if (wbc->for_kupdate ||
271 get_pages(sbi, F2FS_DIRTY_META) < nr_pages_to_skip(sbi, META))
Jaegeuk Kimd3baf952014-03-18 13:43:05 +0900272 goto skip_write;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900273
Yunlei Hed31c7c32016-02-04 16:14:00 +0800274 trace_f2fs_writepages(mapping->host, wbc, META);
275
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900276 /* if mounting is failed, skip writing node pages */
277 mutex_lock(&sbi->cp_mutex);
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +0900278 diff = nr_pages_to_write(sbi, META, wbc);
279 written = sync_meta_pages(sbi, META, wbc->nr_to_write);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900280 mutex_unlock(&sbi->cp_mutex);
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +0900281 wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900282 return 0;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +0900283
284skip_write:
285 wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_META);
Yunlei Hed31c7c32016-02-04 16:14:00 +0800286 trace_f2fs_writepages(mapping->host, wbc, META);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +0900287 return 0;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900288}
289
290long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
291 long nr_to_write)
292{
Gu Zheng9df27d92014-01-20 18:37:04 +0800293 struct address_space *mapping = META_MAPPING(sbi);
Chao Yu80dd9c02016-02-24 17:20:44 +0800294 pgoff_t index = 0, end = ULONG_MAX, prev = ULONG_MAX;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900295 struct pagevec pvec;
296 long nwritten = 0;
297 struct writeback_control wbc = {
298 .for_reclaim = 0,
299 };
Chao Yue9f5b8b2016-02-14 18:54:33 +0800300 struct blk_plug plug;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900301
302 pagevec_init(&pvec, 0);
303
Chao Yue9f5b8b2016-02-14 18:54:33 +0800304 blk_start_plug(&plug);
305
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900306 while (index <= end) {
307 int i, nr_pages;
308 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
309 PAGECACHE_TAG_DIRTY,
310 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
Chao Yucfb271d2013-12-05 17:15:22 +0800311 if (unlikely(nr_pages == 0))
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900312 break;
313
314 for (i = 0; i < nr_pages; i++) {
315 struct page *page = pvec.pages[i];
Jaegeuk Kim203681f2014-02-05 13:03:57 +0900316
Chao Yu80dd9c02016-02-24 17:20:44 +0800317 if (prev == ULONG_MAX)
Jaegeuk Kim6066d8c2015-10-01 16:42:55 -0700318 prev = page->index - 1;
319 if (nr_to_write != LONG_MAX && page->index != prev + 1) {
320 pagevec_release(&pvec);
321 goto stop;
322 }
323
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900324 lock_page(page);
Jaegeuk Kim203681f2014-02-05 13:03:57 +0900325
326 if (unlikely(page->mapping != mapping)) {
327continue_unlock:
328 unlock_page(page);
329 continue;
330 }
331 if (!PageDirty(page)) {
332 /* someone wrote it for us */
333 goto continue_unlock;
334 }
335
Jaegeuk Kimfa3d2bd2016-01-28 11:48:52 -0800336 f2fs_wait_on_page_writeback(page, META, true);
337
338 BUG_ON(PageWriteback(page));
Jaegeuk Kim203681f2014-02-05 13:03:57 +0900339 if (!clear_page_dirty_for_io(page))
340 goto continue_unlock;
341
Chao Yu97dc3fd2015-02-16 16:19:22 +0800342 if (mapping->a_ops->writepage(page, &wbc)) {
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900343 unlock_page(page);
344 break;
345 }
Chao Yucfb271d2013-12-05 17:15:22 +0800346 nwritten++;
Jaegeuk Kim6066d8c2015-10-01 16:42:55 -0700347 prev = page->index;
Chao Yucfb271d2013-12-05 17:15:22 +0800348 if (unlikely(nwritten >= nr_to_write))
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900349 break;
350 }
351 pagevec_release(&pvec);
352 cond_resched();
353 }
Jaegeuk Kim6066d8c2015-10-01 16:42:55 -0700354stop:
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900355 if (nwritten)
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900356 f2fs_submit_merged_bio(sbi, type, WRITE);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900357
Chao Yue9f5b8b2016-02-14 18:54:33 +0800358 blk_finish_plug(&plug);
359
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900360 return nwritten;
361}
362
363static int f2fs_set_meta_page_dirty(struct page *page)
364{
Jaegeuk Kim26c6b882013-10-24 17:53:29 +0900365 trace_f2fs_set_page_dirty(page, META);
366
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900367 SetPageUptodate(page);
368 if (!PageDirty(page)) {
369 __set_page_dirty_nobuffers(page);
Jaegeuk Kim40813632014-09-02 15:31:18 -0700370 inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_META);
Chao Yu16018392015-01-19 20:24:37 +0800371 SetPagePrivate(page);
Jaegeuk Kim9e4ded32014-12-17 19:58:58 -0800372 f2fs_trace_pid(page);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900373 return 1;
374 }
375 return 0;
376}
377
378const struct address_space_operations f2fs_meta_aops = {
379 .writepage = f2fs_write_meta_page,
380 .writepages = f2fs_write_meta_pages,
381 .set_page_dirty = f2fs_set_meta_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +0800382 .invalidatepage = f2fs_invalidate_page,
383 .releasepage = f2fs_release_page,
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900384};
385
Jaegeuk Kim6451e042014-07-25 15:47:17 -0700386static void __add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
Jaegeuk Kim953e6cc2014-07-25 15:47:16 -0700387{
Chao Yu67298802014-11-18 11:18:36 +0800388 struct inode_management *im = &sbi->im[type];
Jaegeuk Kim80c54502015-08-20 08:51:56 -0700389 struct ino_entry *e, *tmp;
390
391 tmp = f2fs_kmem_cache_alloc(ino_entry_slab, GFP_NOFS);
Jaegeuk Kim39efac42014-07-24 18:15:17 -0700392retry:
Jaegeuk Kim80c54502015-08-20 08:51:56 -0700393 radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
Jaegeuk Kim769ec6e2014-12-03 20:47:26 -0800394
Chao Yu67298802014-11-18 11:18:36 +0800395 spin_lock(&im->ino_lock);
Chao Yu67298802014-11-18 11:18:36 +0800396 e = radix_tree_lookup(&im->ino_root, ino);
Jaegeuk Kim39efac42014-07-24 18:15:17 -0700397 if (!e) {
Jaegeuk Kim80c54502015-08-20 08:51:56 -0700398 e = tmp;
Chao Yu67298802014-11-18 11:18:36 +0800399 if (radix_tree_insert(&im->ino_root, ino, e)) {
400 spin_unlock(&im->ino_lock);
Jaegeuk Kim769ec6e2014-12-03 20:47:26 -0800401 radix_tree_preload_end();
Jaegeuk Kim39efac42014-07-24 18:15:17 -0700402 goto retry;
403 }
404 memset(e, 0, sizeof(struct ino_entry));
405 e->ino = ino;
406
Chao Yu67298802014-11-18 11:18:36 +0800407 list_add_tail(&e->list, &im->ino_list);
Jaegeuk Kim8c402942014-11-06 15:16:04 -0800408 if (type != ORPHAN_INO)
Chao Yu67298802014-11-18 11:18:36 +0800409 im->ino_num++;
Jaegeuk Kim39efac42014-07-24 18:15:17 -0700410 }
Chao Yu67298802014-11-18 11:18:36 +0800411 spin_unlock(&im->ino_lock);
Jaegeuk Kim769ec6e2014-12-03 20:47:26 -0800412 radix_tree_preload_end();
Jaegeuk Kim80c54502015-08-20 08:51:56 -0700413
414 if (e != tmp)
415 kmem_cache_free(ino_entry_slab, tmp);
Jaegeuk Kim953e6cc2014-07-25 15:47:16 -0700416}
417
Jaegeuk Kim6451e042014-07-25 15:47:17 -0700418static void __remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
Jaegeuk Kim953e6cc2014-07-25 15:47:16 -0700419{
Chao Yu67298802014-11-18 11:18:36 +0800420 struct inode_management *im = &sbi->im[type];
Jaegeuk Kim6451e042014-07-25 15:47:17 -0700421 struct ino_entry *e;
Jaegeuk Kim953e6cc2014-07-25 15:47:16 -0700422
Chao Yu67298802014-11-18 11:18:36 +0800423 spin_lock(&im->ino_lock);
424 e = radix_tree_lookup(&im->ino_root, ino);
Jaegeuk Kim39efac42014-07-24 18:15:17 -0700425 if (e) {
426 list_del(&e->list);
Chao Yu67298802014-11-18 11:18:36 +0800427 radix_tree_delete(&im->ino_root, ino);
428 im->ino_num--;
429 spin_unlock(&im->ino_lock);
Jaegeuk Kim39efac42014-07-24 18:15:17 -0700430 kmem_cache_free(ino_entry_slab, e);
431 return;
Jaegeuk Kim953e6cc2014-07-25 15:47:16 -0700432 }
Chao Yu67298802014-11-18 11:18:36 +0800433 spin_unlock(&im->ino_lock);
Jaegeuk Kim953e6cc2014-07-25 15:47:16 -0700434}
435
Chao Yua49324f2015-12-15 13:29:47 +0800436void add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
Jaegeuk Kimfff04f92014-07-25 07:40:59 -0700437{
438 /* add new dirty ino entry into list */
439 __add_ino_entry(sbi, ino, type);
440}
441
Chao Yua49324f2015-12-15 13:29:47 +0800442void remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
Jaegeuk Kimfff04f92014-07-25 07:40:59 -0700443{
444 /* remove dirty ino entry from list */
445 __remove_ino_entry(sbi, ino, type);
446}
447
448/* mode should be APPEND_INO or UPDATE_INO */
449bool exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode)
450{
Chao Yu67298802014-11-18 11:18:36 +0800451 struct inode_management *im = &sbi->im[mode];
Jaegeuk Kimfff04f92014-07-25 07:40:59 -0700452 struct ino_entry *e;
Chao Yu67298802014-11-18 11:18:36 +0800453
454 spin_lock(&im->ino_lock);
455 e = radix_tree_lookup(&im->ino_root, ino);
456 spin_unlock(&im->ino_lock);
Jaegeuk Kimfff04f92014-07-25 07:40:59 -0700457 return e ? true : false;
458}
459
Jaegeuk Kim74ef9242016-05-02 22:09:56 -0700460void release_ino_entry(struct f2fs_sb_info *sbi, bool all)
Jaegeuk Kimfff04f92014-07-25 07:40:59 -0700461{
462 struct ino_entry *e, *tmp;
463 int i;
464
Jaegeuk Kim74ef9242016-05-02 22:09:56 -0700465 for (i = all ? ORPHAN_INO: APPEND_INO; i <= UPDATE_INO; i++) {
Chao Yu67298802014-11-18 11:18:36 +0800466 struct inode_management *im = &sbi->im[i];
467
468 spin_lock(&im->ino_lock);
469 list_for_each_entry_safe(e, tmp, &im->ino_list, list) {
Jaegeuk Kimfff04f92014-07-25 07:40:59 -0700470 list_del(&e->list);
Chao Yu67298802014-11-18 11:18:36 +0800471 radix_tree_delete(&im->ino_root, e->ino);
Jaegeuk Kimfff04f92014-07-25 07:40:59 -0700472 kmem_cache_free(ino_entry_slab, e);
Chao Yu67298802014-11-18 11:18:36 +0800473 im->ino_num--;
Jaegeuk Kimfff04f92014-07-25 07:40:59 -0700474 }
Chao Yu67298802014-11-18 11:18:36 +0800475 spin_unlock(&im->ino_lock);
Jaegeuk Kimfff04f92014-07-25 07:40:59 -0700476 }
477}
478
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900479int acquire_orphan_inode(struct f2fs_sb_info *sbi)
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900480{
Chao Yu67298802014-11-18 11:18:36 +0800481 struct inode_management *im = &sbi->im[ORPHAN_INO];
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900482 int err = 0;
483
Chao Yu67298802014-11-18 11:18:36 +0800484 spin_lock(&im->ino_lock);
Jaegeuk Kimcb789422016-04-29 16:29:22 -0700485
486#ifdef CONFIG_F2FS_FAULT_INJECTION
487 if (time_to_inject(FAULT_ORPHAN)) {
488 spin_unlock(&im->ino_lock);
489 return -ENOSPC;
490 }
491#endif
Chao Yu67298802014-11-18 11:18:36 +0800492 if (unlikely(im->ino_num >= sbi->max_orphans))
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900493 err = -ENOSPC;
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900494 else
Chao Yu67298802014-11-18 11:18:36 +0800495 im->ino_num++;
496 spin_unlock(&im->ino_lock);
Gu Zheng0d47c1a2013-12-26 18:24:19 +0800497
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900498 return err;
499}
500
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900501void release_orphan_inode(struct f2fs_sb_info *sbi)
502{
Chao Yu67298802014-11-18 11:18:36 +0800503 struct inode_management *im = &sbi->im[ORPHAN_INO];
504
505 spin_lock(&im->ino_lock);
506 f2fs_bug_on(sbi, im->ino_num == 0);
507 im->ino_num--;
508 spin_unlock(&im->ino_lock);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900509}
510
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900511void add_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
512{
Jaegeuk Kim39efac42014-07-24 18:15:17 -0700513 /* add new orphan ino entry into list */
Jaegeuk Kim6451e042014-07-25 15:47:17 -0700514 __add_ino_entry(sbi, ino, ORPHAN_INO);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900515}
516
517void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
518{
Jaegeuk Kim953e6cc2014-07-25 15:47:16 -0700519 /* remove orphan entry from orphan list */
Jaegeuk Kim6451e042014-07-25 15:47:17 -0700520 __remove_ino_entry(sbi, ino, ORPHAN_INO);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900521}
522
Chao Yu8c14bfa2015-08-07 17:58:43 +0800523static int recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900524{
Chao Yu8c14bfa2015-08-07 17:58:43 +0800525 struct inode *inode;
526
527 inode = f2fs_iget(sbi->sb, ino);
528 if (IS_ERR(inode)) {
529 /*
530 * there should be a bug that we can't find the entry
531 * to orphan inode.
532 */
533 f2fs_bug_on(sbi, PTR_ERR(inode) == -ENOENT);
534 return PTR_ERR(inode);
535 }
536
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900537 clear_nlink(inode);
538
539 /* truncate all the data during iput */
540 iput(inode);
Chao Yu8c14bfa2015-08-07 17:58:43 +0800541 return 0;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900542}
543
Chao Yu8c14bfa2015-08-07 17:58:43 +0800544int recover_orphan_inodes(struct f2fs_sb_info *sbi)
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900545{
Wanpeng Li3c642982015-02-26 07:57:21 +0800546 block_t start_blk, orphan_blocks, i, j;
Chao Yu8c14bfa2015-08-07 17:58:43 +0800547 int err;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900548
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900549 if (!is_set_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG))
Chao Yu8c14bfa2015-08-07 17:58:43 +0800550 return 0;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900551
Wanpeng Li55141482015-02-26 07:57:20 +0800552 start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi);
Wanpeng Li3c642982015-02-26 07:57:21 +0800553 orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900554
Chao Yu26879fb2015-10-12 17:05:59 +0800555 ra_meta_pages(sbi, start_blk, orphan_blocks, META_CP, true);
Chao Yu662befd2014-02-07 16:11:53 +0800556
Wanpeng Li3c642982015-02-26 07:57:21 +0800557 for (i = 0; i < orphan_blocks; i++) {
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900558 struct page *page = get_meta_page(sbi, start_blk + i);
559 struct f2fs_orphan_block *orphan_blk;
560
561 orphan_blk = (struct f2fs_orphan_block *)page_address(page);
562 for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) {
563 nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
Chao Yu8c14bfa2015-08-07 17:58:43 +0800564 err = recover_orphan_inode(sbi, ino);
565 if (err) {
566 f2fs_put_page(page, 1);
Chao Yu8c14bfa2015-08-07 17:58:43 +0800567 return err;
568 }
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900569 }
570 f2fs_put_page(page, 1);
571 }
572 /* clear Orphan Flag */
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900573 clear_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG);
Chao Yu8c14bfa2015-08-07 17:58:43 +0800574 return 0;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900575}
576
577static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk)
578{
Gu Zheng502c6e02013-11-19 18:03:58 +0800579 struct list_head *head;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900580 struct f2fs_orphan_block *orphan_blk = NULL;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900581 unsigned int nentries = 0;
Chao Yubd936f82015-07-13 17:44:25 +0800582 unsigned short index = 1;
Jaegeuk Kim8c402942014-11-06 15:16:04 -0800583 unsigned short orphan_blocks;
Gu Zheng45319292014-01-10 18:09:02 +0800584 struct page *page = NULL;
Jaegeuk Kim6451e042014-07-25 15:47:17 -0700585 struct ino_entry *orphan = NULL;
Chao Yu67298802014-11-18 11:18:36 +0800586 struct inode_management *im = &sbi->im[ORPHAN_INO];
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900587
Chao Yu67298802014-11-18 11:18:36 +0800588 orphan_blocks = GET_ORPHAN_BLOCKS(im->ino_num);
Jaegeuk Kim8c402942014-11-06 15:16:04 -0800589
Jaegeuk Kimd6c67a42015-05-01 11:08:59 -0700590 /*
591 * we don't need to do spin_lock(&im->ino_lock) here, since all the
592 * orphan inode operations are covered under f2fs_lock_op().
593 * And, spin_lock should be avoided due to page operations below.
594 */
Chao Yu67298802014-11-18 11:18:36 +0800595 head = &im->ino_list;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900596
597 /* loop for each orphan inode entry and write them in Jornal block */
Gu Zheng502c6e02013-11-19 18:03:58 +0800598 list_for_each_entry(orphan, head, list) {
599 if (!page) {
Chao Yubd936f82015-07-13 17:44:25 +0800600 page = grab_meta_page(sbi, start_blk++);
Gu Zheng502c6e02013-11-19 18:03:58 +0800601 orphan_blk =
602 (struct f2fs_orphan_block *)page_address(page);
603 memset(orphan_blk, 0, sizeof(*orphan_blk));
604 }
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900605
Gu Zheng36795562013-11-26 16:44:16 +0800606 orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900607
Gu Zheng36795562013-11-26 16:44:16 +0800608 if (nentries == F2FS_ORPHANS_PER_BLOCK) {
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900609 /*
610 * an orphan block is full of 1020 entries,
611 * then we need to flush current orphan blocks
612 * and bring another one in memory
613 */
614 orphan_blk->blk_addr = cpu_to_le16(index);
615 orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
616 orphan_blk->entry_count = cpu_to_le32(nentries);
617 set_page_dirty(page);
618 f2fs_put_page(page, 1);
619 index++;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900620 nentries = 0;
621 page = NULL;
622 }
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900623 }
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900624
Gu Zheng502c6e02013-11-19 18:03:58 +0800625 if (page) {
626 orphan_blk->blk_addr = cpu_to_le16(index);
627 orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
628 orphan_blk->entry_count = cpu_to_le32(nentries);
629 set_page_dirty(page);
630 f2fs_put_page(page, 1);
631 }
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900632}
633
634static struct page *validate_checkpoint(struct f2fs_sb_info *sbi,
635 block_t cp_addr, unsigned long long *version)
636{
637 struct page *cp_page_1, *cp_page_2 = NULL;
638 unsigned long blk_size = sbi->blocksize;
639 struct f2fs_checkpoint *cp_block;
640 unsigned long long cur_version = 0, pre_version = 0;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900641 size_t crc_offset;
Jaegeuk Kim7e586fa2013-06-19 20:47:19 +0900642 __u32 crc = 0;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900643
644 /* Read the 1st cp block in this CP pack */
645 cp_page_1 = get_meta_page(sbi, cp_addr);
646
647 /* get the version number */
648 cp_block = (struct f2fs_checkpoint *)page_address(cp_page_1);
649 crc_offset = le32_to_cpu(cp_block->checksum_offset);
650 if (crc_offset >= blk_size)
651 goto invalid_cp1;
652
Jaegeuk Kim29e70432015-02-10 16:23:12 -0800653 crc = le32_to_cpu(*((__le32 *)((unsigned char *)cp_block + crc_offset)));
Keith Mok43b65732016-03-02 12:04:24 -0800654 if (!f2fs_crc_valid(sbi, crc, cp_block, crc_offset))
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900655 goto invalid_cp1;
656
Jaegeuk Kimd71b5562013-08-09 15:03:21 +0900657 pre_version = cur_cp_version(cp_block);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900658
659 /* Read the 2nd cp block in this CP pack */
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900660 cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900661 cp_page_2 = get_meta_page(sbi, cp_addr);
662
663 cp_block = (struct f2fs_checkpoint *)page_address(cp_page_2);
664 crc_offset = le32_to_cpu(cp_block->checksum_offset);
665 if (crc_offset >= blk_size)
666 goto invalid_cp2;
667
Jaegeuk Kim29e70432015-02-10 16:23:12 -0800668 crc = le32_to_cpu(*((__le32 *)((unsigned char *)cp_block + crc_offset)));
Keith Mok43b65732016-03-02 12:04:24 -0800669 if (!f2fs_crc_valid(sbi, crc, cp_block, crc_offset))
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900670 goto invalid_cp2;
671
Jaegeuk Kimd71b5562013-08-09 15:03:21 +0900672 cur_version = cur_cp_version(cp_block);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900673
674 if (cur_version == pre_version) {
675 *version = cur_version;
676 f2fs_put_page(cp_page_2, 1);
677 return cp_page_1;
678 }
679invalid_cp2:
680 f2fs_put_page(cp_page_2, 1);
681invalid_cp1:
682 f2fs_put_page(cp_page_1, 1);
683 return NULL;
684}
685
686int get_valid_checkpoint(struct f2fs_sb_info *sbi)
687{
688 struct f2fs_checkpoint *cp_block;
689 struct f2fs_super_block *fsb = sbi->raw_super;
690 struct page *cp1, *cp2, *cur_page;
691 unsigned long blk_size = sbi->blocksize;
692 unsigned long long cp1_version = 0, cp2_version = 0;
693 unsigned long long cp_start_blk_no;
Wanpeng Li55141482015-02-26 07:57:20 +0800694 unsigned int cp_blks = 1 + __cp_payload(sbi);
Changman Lee1dbe4152014-05-12 12:27:43 +0900695 block_t cp_blk_no;
696 int i;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900697
Changman Lee1dbe4152014-05-12 12:27:43 +0900698 sbi->ckpt = kzalloc(cp_blks * blk_size, GFP_KERNEL);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900699 if (!sbi->ckpt)
700 return -ENOMEM;
701 /*
702 * Finding out valid cp block involves read both
703 * sets( cp pack1 and cp pack 2)
704 */
705 cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr);
706 cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version);
707
708 /* The second checkpoint pack should start at the next segment */
Jaegeuk Kimf9a4e6d2013-11-28 12:44:05 +0900709 cp_start_blk_no += ((unsigned long long)1) <<
710 le32_to_cpu(fsb->log_blocks_per_seg);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900711 cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version);
712
713 if (cp1 && cp2) {
714 if (ver_after(cp2_version, cp1_version))
715 cur_page = cp2;
716 else
717 cur_page = cp1;
718 } else if (cp1) {
719 cur_page = cp1;
720 } else if (cp2) {
721 cur_page = cp2;
722 } else {
723 goto fail_no_cp;
724 }
725
726 cp_block = (struct f2fs_checkpoint *)page_address(cur_page);
727 memcpy(sbi->ckpt, cp_block, blk_size);
728
Shawn Lin984ec632016-02-17 11:26:32 +0800729 /* Sanity checking of checkpoint */
730 if (sanity_check_ckpt(sbi))
731 goto fail_no_cp;
732
Changman Lee1dbe4152014-05-12 12:27:43 +0900733 if (cp_blks <= 1)
734 goto done;
735
736 cp_blk_no = le32_to_cpu(fsb->cp_blkaddr);
737 if (cur_page == cp2)
738 cp_blk_no += 1 << le32_to_cpu(fsb->log_blocks_per_seg);
739
740 for (i = 1; i < cp_blks; i++) {
741 void *sit_bitmap_ptr;
742 unsigned char *ckpt = (unsigned char *)sbi->ckpt;
743
744 cur_page = get_meta_page(sbi, cp_blk_no + i);
745 sit_bitmap_ptr = page_address(cur_page);
746 memcpy(ckpt + i * blk_size, sit_bitmap_ptr, blk_size);
747 f2fs_put_page(cur_page, 1);
748 }
749done:
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900750 f2fs_put_page(cp1, 1);
751 f2fs_put_page(cp2, 1);
752 return 0;
753
754fail_no_cp:
755 kfree(sbi->ckpt);
756 return -EINVAL;
757}
758
Chao Yuc227f912015-12-16 13:09:20 +0800759static void __add_dirty_inode(struct inode *inode, enum inode_type type)
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900760{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700761 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu2710fd72015-12-15 13:30:45 +0800762 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yuc227f912015-12-16 13:09:20 +0800763 int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900764
Chao Yuc227f912015-12-16 13:09:20 +0800765 if (is_inode_flag_set(fi, flag))
Chao Yu2710fd72015-12-15 13:30:45 +0800766 return;
Chao Yu2d7b8222014-03-29 11:33:17 +0800767
Chao Yuc227f912015-12-16 13:09:20 +0800768 set_inode_flag(fi, flag);
769 list_add_tail(&fi->dirty_list, &sbi->inode_list[type]);
Chao Yu33fbd512015-12-17 17:14:44 +0800770 stat_inc_dirty_inode(sbi, type);
Jaegeuk Kim5deb8262013-06-05 17:42:45 +0900771}
772
Chao Yuc227f912015-12-16 13:09:20 +0800773static void __remove_dirty_inode(struct inode *inode, enum inode_type type)
Chao Yu6ad76092015-12-15 13:31:40 +0800774{
Chao Yu6ad76092015-12-15 13:31:40 +0800775 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yuc227f912015-12-16 13:09:20 +0800776 int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
Chao Yu6ad76092015-12-15 13:31:40 +0800777
778 if (get_dirty_pages(inode) ||
Chao Yuc227f912015-12-16 13:09:20 +0800779 !is_inode_flag_set(F2FS_I(inode), flag))
Chao Yu6ad76092015-12-15 13:31:40 +0800780 return;
781
782 list_del_init(&fi->dirty_list);
Chao Yuc227f912015-12-16 13:09:20 +0800783 clear_inode_flag(fi, flag);
Chao Yu33fbd512015-12-17 17:14:44 +0800784 stat_dec_dirty_inode(F2FS_I_SB(inode), type);
Chao Yu6ad76092015-12-15 13:31:40 +0800785}
786
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -0700787void update_dirty_page(struct inode *inode, struct page *page)
Jaegeuk Kim5deb8262013-06-05 17:42:45 +0900788{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700789 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yuc227f912015-12-16 13:09:20 +0800790 enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
Jaegeuk Kim5deb8262013-06-05 17:42:45 +0900791
Chao Yu5ac9f362015-06-29 18:14:10 +0800792 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
793 !S_ISLNK(inode->i_mode))
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900794 return;
Gu Zheng7bd59382013-10-22 14:52:26 +0800795
Jaegeuk Kim10aa97c2016-05-16 10:33:40 -0700796 if (type != FILE_INODE || test_opt(sbi, DATA_FLUSH)) {
797 spin_lock(&sbi->inode_lock[type]);
798 __add_dirty_inode(inode, type);
799 spin_unlock(&sbi->inode_lock[type]);
800 }
Chao Yucf0ee0f2014-04-02 08:55:00 +0800801
Yunlei Heb951a4e2016-05-13 14:57:43 +0800802 inode_inc_dirty_pages(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -0700803 SetPagePrivate(page);
Jaegeuk Kim9e4ded32014-12-17 19:58:58 -0800804 f2fs_trace_pid(page);
Jaegeuk Kim5deb8262013-06-05 17:42:45 +0900805}
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900806
Chao Yuc227f912015-12-16 13:09:20 +0800807void remove_dirty_inode(struct inode *inode)
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900808{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700809 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yuc227f912015-12-16 13:09:20 +0800810 enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900811
Chao Yuc227f912015-12-16 13:09:20 +0800812 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
813 !S_ISLNK(inode->i_mode))
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900814 return;
815
Jaegeuk Kim10aa97c2016-05-16 10:33:40 -0700816 if (type == FILE_INODE && !test_opt(sbi, DATA_FLUSH))
817 return;
818
Chao Yuc227f912015-12-16 13:09:20 +0800819 spin_lock(&sbi->inode_lock[type]);
820 __remove_dirty_inode(inode, type);
821 spin_unlock(&sbi->inode_lock[type]);
Jaegeuk Kim74d0b912013-05-15 16:40:02 +0900822}
823
Chao Yu6d5a1492015-12-24 18:04:56 +0800824int sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type)
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900825{
Gu Zhengce3b7d82013-11-19 18:03:47 +0800826 struct list_head *head;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900827 struct inode *inode;
Chao Yu2710fd72015-12-15 13:30:45 +0800828 struct f2fs_inode_info *fi;
Chao Yu4cf18532015-12-17 17:17:16 +0800829 bool is_dir = (type == DIR_INODE);
830
831 trace_f2fs_sync_dirty_inodes_enter(sbi->sb, is_dir,
832 get_pages(sbi, is_dir ?
833 F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900834retry:
Jaegeuk Kimaf41d3e2014-10-17 14:14:16 -0700835 if (unlikely(f2fs_cp_error(sbi)))
Chao Yu6d5a1492015-12-24 18:04:56 +0800836 return -EIO;
Jaegeuk Kimaf41d3e2014-10-17 14:14:16 -0700837
Chao Yuc227f912015-12-16 13:09:20 +0800838 spin_lock(&sbi->inode_lock[type]);
Gu Zhengce3b7d82013-11-19 18:03:47 +0800839
Chao Yuc227f912015-12-16 13:09:20 +0800840 head = &sbi->inode_list[type];
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900841 if (list_empty(head)) {
Chao Yuc227f912015-12-16 13:09:20 +0800842 spin_unlock(&sbi->inode_lock[type]);
Chao Yu4cf18532015-12-17 17:17:16 +0800843 trace_f2fs_sync_dirty_inodes_exit(sbi->sb, is_dir,
844 get_pages(sbi, is_dir ?
845 F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
Chao Yu6d5a1492015-12-24 18:04:56 +0800846 return 0;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900847 }
Chao Yu2710fd72015-12-15 13:30:45 +0800848 fi = list_entry(head->next, struct f2fs_inode_info, dirty_list);
849 inode = igrab(&fi->vfs_inode);
Chao Yuc227f912015-12-16 13:09:20 +0800850 spin_unlock(&sbi->inode_lock[type]);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900851 if (inode) {
Jaegeuk Kim87d6f892014-03-18 12:40:49 +0900852 filemap_fdatawrite(inode->i_mapping);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900853 iput(inode);
854 } else {
855 /*
856 * We should submit bio, since it exists several
857 * wribacking dentry pages in the freeing inode.
858 */
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900859 f2fs_submit_merged_bio(sbi, DATA, WRITE);
Sebastian Andrzej Siewior7ecebe52015-02-27 13:13:14 +0100860 cond_resched();
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900861 }
862 goto retry;
863}
864
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900865/*
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900866 * Freeze all the FS-operations for checkpoint.
867 */
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -0700868static int block_operations(struct f2fs_sb_info *sbi)
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900869{
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900870 struct writeback_control wbc = {
871 .sync_mode = WB_SYNC_ALL,
872 .nr_to_write = LONG_MAX,
873 .for_reclaim = 0,
874 };
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900875 struct blk_plug plug;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -0700876 int err = 0;
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900877
878 blk_start_plug(&plug);
879
Jaegeuk Kim39936832012-11-22 16:21:29 +0900880retry_flush_dents:
Gu Zhenge4795562013-09-27 18:08:30 +0800881 f2fs_lock_all(sbi);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900882 /* write all the dirty dentry pages */
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900883 if (get_pages(sbi, F2FS_DIRTY_DENTS)) {
Gu Zhenge4795562013-09-27 18:08:30 +0800884 f2fs_unlock_all(sbi);
Chao Yu6d5a1492015-12-24 18:04:56 +0800885 err = sync_dirty_inodes(sbi, DIR_INODE);
886 if (err)
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -0700887 goto out;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900888 goto retry_flush_dents;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900889 }
890
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900891 /*
arter97e1c42042014-08-06 23:22:50 +0900892 * POR: we should ensure that there are no dirty node pages
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900893 * until finishing nat/sit flush.
894 */
Jaegeuk Kim39936832012-11-22 16:21:29 +0900895retry_flush_nodes:
Chao Yub3582c62014-07-03 18:58:39 +0800896 down_write(&sbi->node_write);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900897
898 if (get_pages(sbi, F2FS_DIRTY_NODES)) {
Chao Yub3582c62014-07-03 18:58:39 +0800899 up_write(&sbi->node_write);
Jaegeuk Kim52681372016-04-13 16:24:44 -0700900 err = sync_node_pages(sbi, &wbc);
Chao Yu6d5a1492015-12-24 18:04:56 +0800901 if (err) {
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -0700902 f2fs_unlock_all(sbi);
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -0700903 goto out;
904 }
Jaegeuk Kim39936832012-11-22 16:21:29 +0900905 goto retry_flush_nodes;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900906 }
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -0700907out:
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900908 blk_finish_plug(&plug);
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -0700909 return err;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900910}
911
912static void unblock_operations(struct f2fs_sb_info *sbi)
913{
Chao Yub3582c62014-07-03 18:58:39 +0800914 up_write(&sbi->node_write);
Gu Zhenge4795562013-09-27 18:08:30 +0800915 f2fs_unlock_all(sbi);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900916}
917
Changman Leefb51b5e2013-11-07 12:48:25 +0900918static void wait_on_all_pages_writeback(struct f2fs_sb_info *sbi)
919{
920 DEFINE_WAIT(wait);
921
922 for (;;) {
923 prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE);
924
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700925 if (!atomic_read(&sbi->nr_wb_bios))
Changman Leefb51b5e2013-11-07 12:48:25 +0900926 break;
927
Yunlei He0ff21642016-02-23 12:07:56 +0800928 io_schedule_timeout(5*HZ);
Changman Leefb51b5e2013-11-07 12:48:25 +0900929 }
930 finish_wait(&sbi->cp_wait, &wait);
931}
932
Chao Yuc34f42e2015-12-23 17:50:30 +0800933static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900934{
935 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Jaegeuk Kimcf2271e2014-07-25 15:47:25 -0700936 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
Huang Ying77041822014-09-12 20:19:48 +0800937 struct f2fs_nm_info *nm_i = NM_I(sbi);
Chao Yu67298802014-11-18 11:18:36 +0800938 unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num;
Huang Ying77041822014-09-12 20:19:48 +0800939 nid_t last_nid = nm_i->next_scan_nid;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900940 block_t start_blk;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900941 unsigned int data_sum_blocks, orphan_blocks;
Jaegeuk Kim7e586fa2013-06-19 20:47:19 +0900942 __u32 crc32 = 0;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900943 int i;
Wanpeng Li55141482015-02-26 07:57:20 +0800944 int cp_payload_blks = __cp_payload(sbi);
Chao Yue90c2d22015-07-28 18:36:47 +0800945 block_t discard_blk = NEXT_FREE_BLKADDR(sbi, curseg);
946 bool invalidate = false;
Shuoran Liu8f1dbbb2016-01-27 09:57:30 +0800947 struct super_block *sb = sbi->sb;
948 struct curseg_info *seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
949 u64 kbytes_written;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900950
Jaegeuk Kim1e87a782014-04-15 13:57:55 +0900951 /*
952 * This avoids to conduct wrong roll-forward operations and uses
953 * metapages, so should be called prior to sync_meta_pages below.
954 */
Chao Yue90c2d22015-07-28 18:36:47 +0800955 if (discard_next_dnode(sbi, discard_blk))
956 invalidate = true;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900957
958 /* Flush all the NAT/SIT pages */
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -0700959 while (get_pages(sbi, F2FS_DIRTY_META)) {
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900960 sync_meta_pages(sbi, META, LONG_MAX);
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -0700961 if (unlikely(f2fs_cp_error(sbi)))
Chao Yuc34f42e2015-12-23 17:50:30 +0800962 return -EIO;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -0700963 }
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900964
965 next_free_nid(sbi, &last_nid);
966
967 /*
968 * modify checkpoint
969 * version number is already updated
970 */
971 ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi));
972 ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi));
973 ckpt->free_segment_count = cpu_to_le32(free_segments(sbi));
Chao Yub5b82202014-08-22 16:17:38 +0800974 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900975 ckpt->cur_node_segno[i] =
976 cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE));
977 ckpt->cur_node_blkoff[i] =
978 cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE));
979 ckpt->alloc_type[i + CURSEG_HOT_NODE] =
980 curseg_alloc_type(sbi, i + CURSEG_HOT_NODE);
981 }
Chao Yub5b82202014-08-22 16:17:38 +0800982 for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900983 ckpt->cur_data_segno[i] =
984 cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA));
985 ckpt->cur_data_blkoff[i] =
986 cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA));
987 ckpt->alloc_type[i + CURSEG_HOT_DATA] =
988 curseg_alloc_type(sbi, i + CURSEG_HOT_DATA);
989 }
990
991 ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi));
992 ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi));
993 ckpt->next_free_nid = cpu_to_le32(last_nid);
994
995 /* 2 cp + n data seg summary + orphan inode blocks */
Chao Yu3fa06d72014-12-09 14:21:46 +0800996 data_sum_blocks = npages_for_summary_flush(sbi, false);
Chao Yub5b82202014-08-22 16:17:38 +0800997 if (data_sum_blocks < NR_CURSEG_DATA_TYPE)
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900998 set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900999 else
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001000 clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
Jaegeuk Kim127e6702012-11-02 17:08:18 +09001001
Chao Yu67298802014-11-18 11:18:36 +08001002 orphan_blocks = GET_ORPHAN_BLOCKS(orphan_num);
Changman Lee1dbe4152014-05-12 12:27:43 +09001003 ckpt->cp_pack_start_sum = cpu_to_le32(1 + cp_payload_blks +
1004 orphan_blocks);
Jaegeuk Kim127e6702012-11-02 17:08:18 +09001005
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001006 if (__remain_node_summaries(cpc->reason))
Chao Yub5b82202014-08-22 16:17:38 +08001007 ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS+
Changman Lee1dbe4152014-05-12 12:27:43 +09001008 cp_payload_blks + data_sum_blocks +
1009 orphan_blocks + NR_CURSEG_NODE_TYPE);
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001010 else
Chao Yub5b82202014-08-22 16:17:38 +08001011 ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS +
Changman Lee1dbe4152014-05-12 12:27:43 +09001012 cp_payload_blks + data_sum_blocks +
1013 orphan_blocks);
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001014
1015 if (cpc->reason == CP_UMOUNT)
1016 set_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
1017 else
1018 clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
1019
1020 if (cpc->reason == CP_FASTBOOT)
1021 set_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
1022 else
1023 clear_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
Jaegeuk Kim127e6702012-11-02 17:08:18 +09001024
Chao Yu67298802014-11-18 11:18:36 +08001025 if (orphan_num)
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001026 set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
Jaegeuk Kim127e6702012-11-02 17:08:18 +09001027 else
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001028 clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
Jaegeuk Kim127e6702012-11-02 17:08:18 +09001029
Chao Yucaf00472015-01-28 17:48:42 +08001030 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK))
Jaegeuk Kim2ae4c672014-09-02 15:43:52 -07001031 set_ckpt_flags(ckpt, CP_FSCK_FLAG);
1032
Jaegeuk Kim127e6702012-11-02 17:08:18 +09001033 /* update SIT/NAT bitmap */
1034 get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP));
1035 get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP));
1036
Keith Mok43b65732016-03-02 12:04:24 -08001037 crc32 = f2fs_crc32(sbi, ckpt, le32_to_cpu(ckpt->checksum_offset));
Jaegeuk Kim7e586fa2013-06-19 20:47:19 +09001038 *((__le32 *)((unsigned char *)ckpt +
1039 le32_to_cpu(ckpt->checksum_offset)))
Jaegeuk Kim127e6702012-11-02 17:08:18 +09001040 = cpu_to_le32(crc32);
1041
1042 start_blk = __start_cp_addr(sbi);
1043
Jaegeuk Kima7230d12015-09-16 14:06:54 -07001044 /* need to wait for end_io results */
1045 wait_on_all_pages_writeback(sbi);
1046 if (unlikely(f2fs_cp_error(sbi)))
Chao Yuc34f42e2015-12-23 17:50:30 +08001047 return -EIO;
Jaegeuk Kima7230d12015-09-16 14:06:54 -07001048
Jaegeuk Kim127e6702012-11-02 17:08:18 +09001049 /* write out checkpoint buffer at block 0 */
Chao Yu381722d2015-05-19 17:40:04 +08001050 update_meta_page(sbi, ckpt, start_blk++);
Jaegeuk Kim127e6702012-11-02 17:08:18 +09001051
Chao Yu381722d2015-05-19 17:40:04 +08001052 for (i = 1; i < 1 + cp_payload_blks; i++)
1053 update_meta_page(sbi, (char *)ckpt + i * F2FS_BLKSIZE,
1054 start_blk++);
Changman Lee1dbe4152014-05-12 12:27:43 +09001055
Chao Yu67298802014-11-18 11:18:36 +08001056 if (orphan_num) {
Jaegeuk Kim127e6702012-11-02 17:08:18 +09001057 write_orphan_inodes(sbi, start_blk);
1058 start_blk += orphan_blocks;
1059 }
1060
1061 write_data_summaries(sbi, start_blk);
1062 start_blk += data_sum_blocks;
Shuoran Liu8f1dbbb2016-01-27 09:57:30 +08001063
1064 /* Record write statistics in the hot node summary */
1065 kbytes_written = sbi->kbytes_written;
1066 if (sb->s_bdev->bd_part)
1067 kbytes_written += BD_PART_WRITTEN(sbi);
1068
Chao Yub7ad7512016-02-19 18:08:46 +08001069 seg_i->journal->info.kbytes_written = cpu_to_le64(kbytes_written);
Shuoran Liu8f1dbbb2016-01-27 09:57:30 +08001070
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001071 if (__remain_node_summaries(cpc->reason)) {
Jaegeuk Kim127e6702012-11-02 17:08:18 +09001072 write_node_summaries(sbi, start_blk);
1073 start_blk += NR_CURSEG_NODE_TYPE;
1074 }
1075
1076 /* writeout checkpoint block */
Chao Yu381722d2015-05-19 17:40:04 +08001077 update_meta_page(sbi, ckpt, start_blk);
Jaegeuk Kim127e6702012-11-02 17:08:18 +09001078
1079 /* wait for previous submitted node/meta pages writeback */
Changman Leefb51b5e2013-11-07 12:48:25 +09001080 wait_on_all_pages_writeback(sbi);
Jaegeuk Kim127e6702012-11-02 17:08:18 +09001081
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001082 if (unlikely(f2fs_cp_error(sbi)))
Chao Yuc34f42e2015-12-23 17:50:30 +08001083 return -EIO;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001084
Chao Yu80dd9c02016-02-24 17:20:44 +08001085 filemap_fdatawait_range(NODE_MAPPING(sbi), 0, LLONG_MAX);
1086 filemap_fdatawait_range(META_MAPPING(sbi), 0, LLONG_MAX);
Jaegeuk Kim127e6702012-11-02 17:08:18 +09001087
1088 /* update user_block_counts */
1089 sbi->last_valid_block_count = sbi->total_valid_block_count;
Jaegeuk Kim41382ec2016-05-16 11:06:50 -07001090 percpu_counter_set(&sbi->alloc_valid_block_count, 0);
Jaegeuk Kim127e6702012-11-02 17:08:18 +09001091
1092 /* Here, we only have one bio having CP pack */
Jaegeuk Kim577e3492013-01-24 19:56:11 +09001093 sync_meta_pages(sbi, META_FLUSH, LONG_MAX);
Jaegeuk Kim127e6702012-11-02 17:08:18 +09001094
Jaegeuk Kim6a8f8ca2014-10-29 14:37:22 -07001095 /* wait for previous submitted meta pages writeback */
1096 wait_on_all_pages_writeback(sbi);
1097
Chao Yue90c2d22015-07-28 18:36:47 +08001098 /*
1099 * invalidate meta page which is used temporarily for zeroing out
1100 * block at the end of warm node chain.
1101 */
1102 if (invalidate)
1103 invalidate_mapping_pages(META_MAPPING(sbi), discard_blk,
1104 discard_blk);
1105
Jaegeuk Kim74ef9242016-05-02 22:09:56 -07001106 release_ino_entry(sbi, false);
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001107
1108 if (unlikely(f2fs_cp_error(sbi)))
Chao Yuc34f42e2015-12-23 17:50:30 +08001109 return -EIO;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001110
Jaegeuk Kim836b5a62015-04-30 22:50:06 -07001111 clear_prefree_segments(sbi, cpc);
Chao Yucaf00472015-01-28 17:48:42 +08001112 clear_sbi_flag(sbi, SBI_IS_DIRTY);
Chao Yuc34f42e2015-12-23 17:50:30 +08001113
1114 return 0;
Jaegeuk Kim127e6702012-11-02 17:08:18 +09001115}
1116
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001117/*
arter97e1c42042014-08-06 23:22:50 +09001118 * We guarantee that this checkpoint procedure will not fail.
Jaegeuk Kim127e6702012-11-02 17:08:18 +09001119 */
Chao Yuc34f42e2015-12-23 17:50:30 +08001120int write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
Jaegeuk Kim127e6702012-11-02 17:08:18 +09001121{
1122 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1123 unsigned long long ckpt_ver;
Chao Yuc34f42e2015-12-23 17:50:30 +08001124 int err = 0;
Jaegeuk Kim127e6702012-11-02 17:08:18 +09001125
Jaegeuk Kim43727522013-02-04 15:11:17 +09001126 mutex_lock(&sbi->cp_mutex);
Jaegeuk Kim85010172014-08-11 18:37:46 -07001127
Chao Yucaf00472015-01-28 17:48:42 +08001128 if (!is_sbi_flag_set(sbi, SBI_IS_DIRTY) &&
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001129 (cpc->reason == CP_FASTBOOT || cpc->reason == CP_SYNC ||
1130 (cpc->reason == CP_DISCARD && !sbi->discard_blks)))
Jaegeuk Kim85010172014-08-11 18:37:46 -07001131 goto out;
Chao Yuc34f42e2015-12-23 17:50:30 +08001132 if (unlikely(f2fs_cp_error(sbi))) {
1133 err = -EIO;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001134 goto out;
Chao Yuc34f42e2015-12-23 17:50:30 +08001135 }
1136 if (f2fs_readonly(sbi->sb)) {
1137 err = -EROFS;
Jaegeuk Kim11504a82015-01-23 18:43:45 -08001138 goto out;
Chao Yuc34f42e2015-12-23 17:50:30 +08001139 }
Wanpeng Li2bda5422015-02-27 15:56:16 +08001140
1141 trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "start block_ops");
1142
Chao Yuc34f42e2015-12-23 17:50:30 +08001143 err = block_operations(sbi);
1144 if (err)
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001145 goto out;
Jaegeuk Kim127e6702012-11-02 17:08:18 +09001146
Jaegeuk Kim75ab4cb2014-09-20 21:57:51 -07001147 trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish block_ops");
Namjae Jeon2af4bd62013-04-23 18:26:54 +09001148
Chao Yu406657d2016-02-24 17:17:55 +08001149 f2fs_flush_merged_bios(sbi);
Jaegeuk Kim127e6702012-11-02 17:08:18 +09001150
1151 /*
1152 * update checkpoint pack index
1153 * Increase the version number so that
1154 * SIT entries and seg summaries are written at correct place
1155 */
Jaegeuk Kimd71b5562013-08-09 15:03:21 +09001156 ckpt_ver = cur_cp_version(ckpt);
Jaegeuk Kim127e6702012-11-02 17:08:18 +09001157 ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver);
1158
1159 /* write cached NAT/SIT entries to NAT/SIT area */
1160 flush_nat_entries(sbi);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001161 flush_sit_entries(sbi, cpc);
Jaegeuk Kim127e6702012-11-02 17:08:18 +09001162
Jaegeuk Kim127e6702012-11-02 17:08:18 +09001163 /* unlock all the fs_lock[] in do_checkpoint() */
Chao Yuc34f42e2015-12-23 17:50:30 +08001164 err = do_checkpoint(sbi, cpc);
Jaegeuk Kim127e6702012-11-02 17:08:18 +09001165
1166 unblock_operations(sbi);
Changman Lee942e0be2014-02-13 15:12:29 +09001167 stat_inc_cp_count(sbi->stat_info);
Jaegeuk Kim10027552015-04-09 17:03:53 -07001168
1169 if (cpc->reason == CP_RECOVERY)
1170 f2fs_msg(sbi->sb, KERN_NOTICE,
1171 "checkpoint: version = %llx", ckpt_ver);
Jaegeuk Kim60b99b42015-10-05 14:49:57 -07001172
1173 /* do checkpoint periodically */
Jaegeuk Kim6beceb52016-01-08 15:51:50 -08001174 f2fs_update_time(sbi, CP_TIME);
Jaegeuk Kim55d1cdb2015-12-15 16:07:14 -08001175 trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish checkpoint");
Jaegeuk Kim85010172014-08-11 18:37:46 -07001176out:
1177 mutex_unlock(&sbi->cp_mutex);
Chao Yuc34f42e2015-12-23 17:50:30 +08001178 return err;
Jaegeuk Kim127e6702012-11-02 17:08:18 +09001179}
1180
Jaegeuk Kim6451e042014-07-25 15:47:17 -07001181void init_ino_entry_info(struct f2fs_sb_info *sbi)
Jaegeuk Kim127e6702012-11-02 17:08:18 +09001182{
Jaegeuk Kim6451e042014-07-25 15:47:17 -07001183 int i;
1184
1185 for (i = 0; i < MAX_INO_ENTRY; i++) {
Chao Yu67298802014-11-18 11:18:36 +08001186 struct inode_management *im = &sbi->im[i];
1187
1188 INIT_RADIX_TREE(&im->ino_root, GFP_ATOMIC);
1189 spin_lock_init(&im->ino_lock);
1190 INIT_LIST_HEAD(&im->ino_list);
1191 im->ino_num = 0;
Jaegeuk Kim6451e042014-07-25 15:47:17 -07001192 }
1193
Chao Yub5b82202014-08-22 16:17:38 +08001194 sbi->max_orphans = (sbi->blocks_per_seg - F2FS_CP_PACKS -
Wanpeng Li14b42812015-02-27 17:38:13 +08001195 NR_CURSEG_TYPE - __cp_payload(sbi)) *
1196 F2FS_ORPHANS_PER_BLOCK;
Jaegeuk Kim127e6702012-11-02 17:08:18 +09001197}
1198
Namjae Jeon6e6093a2013-01-17 00:08:30 +09001199int __init create_checkpoint_caches(void)
Jaegeuk Kim127e6702012-11-02 17:08:18 +09001200{
Jaegeuk Kim6451e042014-07-25 15:47:17 -07001201 ino_entry_slab = f2fs_kmem_cache_create("f2fs_ino_entry",
1202 sizeof(struct ino_entry));
1203 if (!ino_entry_slab)
Jaegeuk Kim127e6702012-11-02 17:08:18 +09001204 return -ENOMEM;
Chao Yu06292072014-12-29 15:56:18 +08001205 inode_entry_slab = f2fs_kmem_cache_create("f2fs_inode_entry",
1206 sizeof(struct inode_entry));
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001207 if (!inode_entry_slab) {
Jaegeuk Kim6451e042014-07-25 15:47:17 -07001208 kmem_cache_destroy(ino_entry_slab);
Jaegeuk Kim127e6702012-11-02 17:08:18 +09001209 return -ENOMEM;
1210 }
1211 return 0;
1212}
1213
1214void destroy_checkpoint_caches(void)
1215{
Jaegeuk Kim6451e042014-07-25 15:47:17 -07001216 kmem_cache_destroy(ino_entry_slab);
Jaegeuk Kim127e6702012-11-02 17:08:18 +09001217 kmem_cache_destroy(inode_entry_slab);
1218}