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