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