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