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