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