]>
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 JK |
24 | |
25 | static struct kmem_cache *orphan_entry_slab; | |
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: | |
36 | page = grab_cache_page(mapping, index); | |
37 | if (!page) { | |
38 | cond_resched(); | |
39 | goto repeat; | |
40 | } | |
41 | ||
42 | /* We wait writeback only inside grab_meta_page() */ | |
43 | wait_on_page_writeback(page); | |
44 | SetPageUptodate(page); | |
45 | return page; | |
46 | } | |
47 | ||
0a8165d7 | 48 | /* |
127e670a JK |
49 | * We guarantee no failure on the returned page. |
50 | */ | |
51 | struct page *get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index) | |
52 | { | |
9df27d98 | 53 | struct address_space *mapping = META_MAPPING(sbi); |
127e670a JK |
54 | struct page *page; |
55 | repeat: | |
56 | page = grab_cache_page(mapping, index); | |
57 | if (!page) { | |
58 | cond_resched(); | |
59 | goto repeat; | |
60 | } | |
393ff91f JK |
61 | if (PageUptodate(page)) |
62 | goto out; | |
63 | ||
93dfe2ac JK |
64 | if (f2fs_submit_page_bio(sbi, page, index, |
65 | READ_SYNC | REQ_META | REQ_PRIO)) | |
127e670a | 66 | goto repeat; |
127e670a | 67 | |
393ff91f | 68 | lock_page(page); |
6bacf52f | 69 | if (unlikely(page->mapping != mapping)) { |
afcb7ca0 JK |
70 | f2fs_put_page(page, 1); |
71 | goto repeat; | |
72 | } | |
393ff91f JK |
73 | out: |
74 | mark_page_accessed(page); | |
127e670a JK |
75 | return page; |
76 | } | |
77 | ||
78 | static int f2fs_write_meta_page(struct page *page, | |
79 | struct writeback_control *wbc) | |
80 | { | |
81 | struct inode *inode = page->mapping->host; | |
82 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); | |
127e670a | 83 | |
203681f6 | 84 | if (unlikely(sbi->por_doing)) |
cfb271d4 | 85 | goto redirty_out; |
cfb271d4 CY |
86 | if (wbc->for_reclaim) |
87 | goto redirty_out; | |
127e670a | 88 | |
203681f6 JK |
89 | /* Should not write any meta pages, if any IO error was occurred */ |
90 | if (unlikely(is_set_ckpt_flags(F2FS_CKPT(sbi), CP_ERROR_FLAG))) | |
91 | goto no_write; | |
127e670a | 92 | |
203681f6 | 93 | wait_on_page_writeback(page); |
577e3495 | 94 | write_meta_page(sbi, page); |
203681f6 | 95 | no_write: |
577e3495 JK |
96 | dec_page_count(sbi, F2FS_DIRTY_META); |
97 | unlock_page(page); | |
98 | return 0; | |
cfb271d4 CY |
99 | |
100 | redirty_out: | |
101 | dec_page_count(sbi, F2FS_DIRTY_META); | |
102 | wbc->pages_skipped++; | |
103 | set_page_dirty(page); | |
104 | return AOP_WRITEPAGE_ACTIVATE; | |
127e670a JK |
105 | } |
106 | ||
107 | static int f2fs_write_meta_pages(struct address_space *mapping, | |
108 | struct writeback_control *wbc) | |
109 | { | |
110 | struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb); | |
5459aa97 | 111 | int nrpages = MAX_BIO_BLOCKS(max_hw_blocks(sbi)); |
127e670a JK |
112 | long written; |
113 | ||
114 | if (wbc->for_kupdate) | |
115 | return 0; | |
116 | ||
5459aa97 JK |
117 | /* collect a number of dirty meta pages and write together */ |
118 | if (get_pages(sbi, F2FS_DIRTY_META) < nrpages) | |
127e670a JK |
119 | return 0; |
120 | ||
121 | /* if mounting is failed, skip writing node pages */ | |
122 | mutex_lock(&sbi->cp_mutex); | |
5459aa97 | 123 | written = sync_meta_pages(sbi, META, nrpages); |
127e670a JK |
124 | mutex_unlock(&sbi->cp_mutex); |
125 | wbc->nr_to_write -= written; | |
126 | return 0; | |
127 | } | |
128 | ||
129 | long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type, | |
130 | long nr_to_write) | |
131 | { | |
9df27d98 | 132 | struct address_space *mapping = META_MAPPING(sbi); |
127e670a JK |
133 | pgoff_t index = 0, end = LONG_MAX; |
134 | struct pagevec pvec; | |
135 | long nwritten = 0; | |
136 | struct writeback_control wbc = { | |
137 | .for_reclaim = 0, | |
138 | }; | |
139 | ||
140 | pagevec_init(&pvec, 0); | |
141 | ||
142 | while (index <= end) { | |
143 | int i, nr_pages; | |
144 | nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, | |
145 | PAGECACHE_TAG_DIRTY, | |
146 | min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1); | |
cfb271d4 | 147 | if (unlikely(nr_pages == 0)) |
127e670a JK |
148 | break; |
149 | ||
150 | for (i = 0; i < nr_pages; i++) { | |
151 | struct page *page = pvec.pages[i]; | |
203681f6 | 152 | |
127e670a | 153 | lock_page(page); |
203681f6 JK |
154 | |
155 | if (unlikely(page->mapping != mapping)) { | |
156 | continue_unlock: | |
157 | unlock_page(page); | |
158 | continue; | |
159 | } | |
160 | if (!PageDirty(page)) { | |
161 | /* someone wrote it for us */ | |
162 | goto continue_unlock; | |
163 | } | |
164 | ||
165 | if (!clear_page_dirty_for_io(page)) | |
166 | goto continue_unlock; | |
167 | ||
577e3495 JK |
168 | if (f2fs_write_meta_page(page, &wbc)) { |
169 | unlock_page(page); | |
170 | break; | |
171 | } | |
cfb271d4 CY |
172 | nwritten++; |
173 | if (unlikely(nwritten >= nr_to_write)) | |
127e670a JK |
174 | break; |
175 | } | |
176 | pagevec_release(&pvec); | |
177 | cond_resched(); | |
178 | } | |
179 | ||
180 | if (nwritten) | |
458e6197 | 181 | f2fs_submit_merged_bio(sbi, type, WRITE); |
127e670a JK |
182 | |
183 | return nwritten; | |
184 | } | |
185 | ||
186 | static int f2fs_set_meta_page_dirty(struct page *page) | |
187 | { | |
188 | struct address_space *mapping = page->mapping; | |
189 | struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb); | |
190 | ||
26c6b887 JK |
191 | trace_f2fs_set_page_dirty(page, META); |
192 | ||
127e670a JK |
193 | SetPageUptodate(page); |
194 | if (!PageDirty(page)) { | |
195 | __set_page_dirty_nobuffers(page); | |
196 | inc_page_count(sbi, F2FS_DIRTY_META); | |
127e670a JK |
197 | return 1; |
198 | } | |
199 | return 0; | |
200 | } | |
201 | ||
202 | const struct address_space_operations f2fs_meta_aops = { | |
203 | .writepage = f2fs_write_meta_page, | |
204 | .writepages = f2fs_write_meta_pages, | |
205 | .set_page_dirty = f2fs_set_meta_page_dirty, | |
206 | }; | |
207 | ||
cbd56e7d | 208 | int acquire_orphan_inode(struct f2fs_sb_info *sbi) |
127e670a | 209 | { |
127e670a JK |
210 | int err = 0; |
211 | ||
17b692f6 | 212 | spin_lock(&sbi->orphan_inode_lock); |
0d47c1ad | 213 | if (unlikely(sbi->n_orphans >= sbi->max_orphans)) |
127e670a | 214 | err = -ENOSPC; |
cbd56e7d JK |
215 | else |
216 | sbi->n_orphans++; | |
17b692f6 | 217 | spin_unlock(&sbi->orphan_inode_lock); |
0d47c1ad | 218 | |
127e670a JK |
219 | return err; |
220 | } | |
221 | ||
cbd56e7d JK |
222 | void release_orphan_inode(struct f2fs_sb_info *sbi) |
223 | { | |
17b692f6 | 224 | spin_lock(&sbi->orphan_inode_lock); |
5d56b671 | 225 | f2fs_bug_on(sbi->n_orphans == 0); |
cbd56e7d | 226 | sbi->n_orphans--; |
17b692f6 | 227 | spin_unlock(&sbi->orphan_inode_lock); |
cbd56e7d JK |
228 | } |
229 | ||
127e670a JK |
230 | void add_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino) |
231 | { | |
232 | struct list_head *head, *this; | |
233 | struct orphan_inode_entry *new = NULL, *orphan = NULL; | |
234 | ||
c1ef3725 GZ |
235 | new = f2fs_kmem_cache_alloc(orphan_entry_slab, GFP_ATOMIC); |
236 | new->ino = ino; | |
237 | ||
17b692f6 | 238 | spin_lock(&sbi->orphan_inode_lock); |
127e670a JK |
239 | head = &sbi->orphan_inode_list; |
240 | list_for_each(this, head) { | |
241 | orphan = list_entry(this, struct orphan_inode_entry, list); | |
c1ef3725 | 242 | if (orphan->ino == ino) { |
17b692f6 | 243 | spin_unlock(&sbi->orphan_inode_lock); |
c1ef3725 GZ |
244 | kmem_cache_free(orphan_entry_slab, new); |
245 | return; | |
246 | } | |
247 | ||
127e670a JK |
248 | if (orphan->ino > ino) |
249 | break; | |
250 | orphan = NULL; | |
251 | } | |
7bd59381 | 252 | |
127e670a | 253 | /* add new_oentry into list which is sorted by inode number */ |
a2617dc6 | 254 | if (orphan) |
255 | list_add(&new->list, this->prev); | |
256 | else | |
127e670a | 257 | list_add_tail(&new->list, head); |
17b692f6 | 258 | spin_unlock(&sbi->orphan_inode_lock); |
127e670a JK |
259 | } |
260 | ||
261 | void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino) | |
262 | { | |
60ed9a0f | 263 | struct list_head *head; |
127e670a JK |
264 | struct orphan_inode_entry *orphan; |
265 | ||
17b692f6 | 266 | spin_lock(&sbi->orphan_inode_lock); |
127e670a | 267 | head = &sbi->orphan_inode_list; |
60ed9a0f | 268 | list_for_each_entry(orphan, head, list) { |
127e670a JK |
269 | if (orphan->ino == ino) { |
270 | list_del(&orphan->list); | |
271 | kmem_cache_free(orphan_entry_slab, orphan); | |
5d56b671 | 272 | f2fs_bug_on(sbi->n_orphans == 0); |
127e670a JK |
273 | sbi->n_orphans--; |
274 | break; | |
275 | } | |
276 | } | |
17b692f6 | 277 | spin_unlock(&sbi->orphan_inode_lock); |
127e670a JK |
278 | } |
279 | ||
280 | static void recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino) | |
281 | { | |
282 | struct inode *inode = f2fs_iget(sbi->sb, ino); | |
5d56b671 | 283 | f2fs_bug_on(IS_ERR(inode)); |
127e670a JK |
284 | clear_nlink(inode); |
285 | ||
286 | /* truncate all the data during iput */ | |
287 | iput(inode); | |
288 | } | |
289 | ||
8f99a946 | 290 | void recover_orphan_inodes(struct f2fs_sb_info *sbi) |
127e670a JK |
291 | { |
292 | block_t start_blk, orphan_blkaddr, i, j; | |
293 | ||
25ca923b | 294 | if (!is_set_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG)) |
8f99a946 | 295 | return; |
127e670a | 296 | |
aabe5136 | 297 | sbi->por_doing = true; |
127e670a JK |
298 | start_blk = __start_cp_addr(sbi) + 1; |
299 | orphan_blkaddr = __start_sum_addr(sbi) - 1; | |
300 | ||
301 | for (i = 0; i < orphan_blkaddr; i++) { | |
302 | struct page *page = get_meta_page(sbi, start_blk + i); | |
303 | struct f2fs_orphan_block *orphan_blk; | |
304 | ||
305 | orphan_blk = (struct f2fs_orphan_block *)page_address(page); | |
306 | for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) { | |
307 | nid_t ino = le32_to_cpu(orphan_blk->ino[j]); | |
308 | recover_orphan_inode(sbi, ino); | |
309 | } | |
310 | f2fs_put_page(page, 1); | |
311 | } | |
312 | /* clear Orphan Flag */ | |
25ca923b | 313 | clear_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG); |
aabe5136 | 314 | sbi->por_doing = false; |
8f99a946 | 315 | return; |
127e670a JK |
316 | } |
317 | ||
318 | static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk) | |
319 | { | |
502c6e0b | 320 | struct list_head *head; |
127e670a | 321 | struct f2fs_orphan_block *orphan_blk = NULL; |
127e670a | 322 | unsigned int nentries = 0; |
4531929e GZ |
323 | unsigned short index; |
324 | unsigned short orphan_blocks = (unsigned short)((sbi->n_orphans + | |
325 | (F2FS_ORPHANS_PER_BLOCK - 1)) / F2FS_ORPHANS_PER_BLOCK); | |
326 | struct page *page = NULL; | |
502c6e0b | 327 | struct orphan_inode_entry *orphan = NULL; |
127e670a | 328 | |
4531929e | 329 | for (index = 0; index < orphan_blocks; index++) |
63f5384c | 330 | grab_meta_page(sbi, start_blk + index); |
127e670a | 331 | |
4531929e | 332 | index = 1; |
17b692f6 | 333 | spin_lock(&sbi->orphan_inode_lock); |
127e670a JK |
334 | head = &sbi->orphan_inode_list; |
335 | ||
336 | /* loop for each orphan inode entry and write them in Jornal block */ | |
502c6e0b GZ |
337 | list_for_each_entry(orphan, head, list) { |
338 | if (!page) { | |
63f5384c GZ |
339 | page = find_get_page(META_MAPPING(sbi), start_blk++); |
340 | f2fs_bug_on(!page); | |
502c6e0b GZ |
341 | orphan_blk = |
342 | (struct f2fs_orphan_block *)page_address(page); | |
343 | memset(orphan_blk, 0, sizeof(*orphan_blk)); | |
63f5384c | 344 | f2fs_put_page(page, 0); |
502c6e0b | 345 | } |
127e670a | 346 | |
36795567 | 347 | orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino); |
127e670a | 348 | |
36795567 | 349 | if (nentries == F2FS_ORPHANS_PER_BLOCK) { |
127e670a JK |
350 | /* |
351 | * an orphan block is full of 1020 entries, | |
352 | * then we need to flush current orphan blocks | |
353 | * and bring another one in memory | |
354 | */ | |
355 | orphan_blk->blk_addr = cpu_to_le16(index); | |
356 | orphan_blk->blk_count = cpu_to_le16(orphan_blocks); | |
357 | orphan_blk->entry_count = cpu_to_le32(nentries); | |
358 | set_page_dirty(page); | |
359 | f2fs_put_page(page, 1); | |
360 | index++; | |
127e670a JK |
361 | nentries = 0; |
362 | page = NULL; | |
363 | } | |
502c6e0b | 364 | } |
127e670a | 365 | |
502c6e0b GZ |
366 | if (page) { |
367 | orphan_blk->blk_addr = cpu_to_le16(index); | |
368 | orphan_blk->blk_count = cpu_to_le16(orphan_blocks); | |
369 | orphan_blk->entry_count = cpu_to_le32(nentries); | |
370 | set_page_dirty(page); | |
371 | f2fs_put_page(page, 1); | |
127e670a | 372 | } |
502c6e0b | 373 | |
17b692f6 | 374 | spin_unlock(&sbi->orphan_inode_lock); |
127e670a JK |
375 | } |
376 | ||
377 | static struct page *validate_checkpoint(struct f2fs_sb_info *sbi, | |
378 | block_t cp_addr, unsigned long long *version) | |
379 | { | |
380 | struct page *cp_page_1, *cp_page_2 = NULL; | |
381 | unsigned long blk_size = sbi->blocksize; | |
382 | struct f2fs_checkpoint *cp_block; | |
383 | unsigned long long cur_version = 0, pre_version = 0; | |
127e670a | 384 | size_t crc_offset; |
7e586fa0 | 385 | __u32 crc = 0; |
127e670a JK |
386 | |
387 | /* Read the 1st cp block in this CP pack */ | |
388 | cp_page_1 = get_meta_page(sbi, cp_addr); | |
389 | ||
390 | /* get the version number */ | |
391 | cp_block = (struct f2fs_checkpoint *)page_address(cp_page_1); | |
392 | crc_offset = le32_to_cpu(cp_block->checksum_offset); | |
393 | if (crc_offset >= blk_size) | |
394 | goto invalid_cp1; | |
395 | ||
7e586fa0 | 396 | crc = le32_to_cpu(*((__u32 *)((unsigned char *)cp_block + crc_offset))); |
127e670a JK |
397 | if (!f2fs_crc_valid(crc, cp_block, crc_offset)) |
398 | goto invalid_cp1; | |
399 | ||
d71b5564 | 400 | pre_version = cur_cp_version(cp_block); |
127e670a JK |
401 | |
402 | /* Read the 2nd cp block in this CP pack */ | |
25ca923b | 403 | cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1; |
127e670a JK |
404 | cp_page_2 = get_meta_page(sbi, cp_addr); |
405 | ||
406 | cp_block = (struct f2fs_checkpoint *)page_address(cp_page_2); | |
407 | crc_offset = le32_to_cpu(cp_block->checksum_offset); | |
408 | if (crc_offset >= blk_size) | |
409 | goto invalid_cp2; | |
410 | ||
7e586fa0 | 411 | crc = le32_to_cpu(*((__u32 *)((unsigned char *)cp_block + crc_offset))); |
127e670a JK |
412 | if (!f2fs_crc_valid(crc, cp_block, crc_offset)) |
413 | goto invalid_cp2; | |
414 | ||
d71b5564 | 415 | cur_version = cur_cp_version(cp_block); |
127e670a JK |
416 | |
417 | if (cur_version == pre_version) { | |
418 | *version = cur_version; | |
419 | f2fs_put_page(cp_page_2, 1); | |
420 | return cp_page_1; | |
421 | } | |
422 | invalid_cp2: | |
423 | f2fs_put_page(cp_page_2, 1); | |
424 | invalid_cp1: | |
425 | f2fs_put_page(cp_page_1, 1); | |
426 | return NULL; | |
427 | } | |
428 | ||
429 | int get_valid_checkpoint(struct f2fs_sb_info *sbi) | |
430 | { | |
431 | struct f2fs_checkpoint *cp_block; | |
432 | struct f2fs_super_block *fsb = sbi->raw_super; | |
433 | struct page *cp1, *cp2, *cur_page; | |
434 | unsigned long blk_size = sbi->blocksize; | |
435 | unsigned long long cp1_version = 0, cp2_version = 0; | |
436 | unsigned long long cp_start_blk_no; | |
437 | ||
438 | sbi->ckpt = kzalloc(blk_size, GFP_KERNEL); | |
439 | if (!sbi->ckpt) | |
440 | return -ENOMEM; | |
441 | /* | |
442 | * Finding out valid cp block involves read both | |
443 | * sets( cp pack1 and cp pack 2) | |
444 | */ | |
445 | cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr); | |
446 | cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version); | |
447 | ||
448 | /* The second checkpoint pack should start at the next segment */ | |
f9a4e6df JK |
449 | cp_start_blk_no += ((unsigned long long)1) << |
450 | le32_to_cpu(fsb->log_blocks_per_seg); | |
127e670a JK |
451 | cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version); |
452 | ||
453 | if (cp1 && cp2) { | |
454 | if (ver_after(cp2_version, cp1_version)) | |
455 | cur_page = cp2; | |
456 | else | |
457 | cur_page = cp1; | |
458 | } else if (cp1) { | |
459 | cur_page = cp1; | |
460 | } else if (cp2) { | |
461 | cur_page = cp2; | |
462 | } else { | |
463 | goto fail_no_cp; | |
464 | } | |
465 | ||
466 | cp_block = (struct f2fs_checkpoint *)page_address(cur_page); | |
467 | memcpy(sbi->ckpt, cp_block, blk_size); | |
468 | ||
469 | f2fs_put_page(cp1, 1); | |
470 | f2fs_put_page(cp2, 1); | |
471 | return 0; | |
472 | ||
473 | fail_no_cp: | |
474 | kfree(sbi->ckpt); | |
475 | return -EINVAL; | |
476 | } | |
477 | ||
5deb8267 | 478 | static int __add_dirty_inode(struct inode *inode, struct dir_inode_entry *new) |
127e670a JK |
479 | { |
480 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); | |
481 | struct list_head *head = &sbi->dir_inode_list; | |
127e670a JK |
482 | struct list_head *this; |
483 | ||
5deb8267 JK |
484 | list_for_each(this, head) { |
485 | struct dir_inode_entry *entry; | |
486 | entry = list_entry(this, struct dir_inode_entry, list); | |
6bacf52f | 487 | if (unlikely(entry->inode == inode)) |
5deb8267 JK |
488 | return -EEXIST; |
489 | } | |
490 | list_add_tail(&new->list, head); | |
dcdfff65 | 491 | stat_inc_dirty_dir(sbi); |
5deb8267 JK |
492 | return 0; |
493 | } | |
494 | ||
495 | void set_dirty_dir_page(struct inode *inode, struct page *page) | |
496 | { | |
497 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); | |
498 | struct dir_inode_entry *new; | |
499 | ||
127e670a JK |
500 | if (!S_ISDIR(inode->i_mode)) |
501 | return; | |
7bd59381 GZ |
502 | |
503 | new = f2fs_kmem_cache_alloc(inode_entry_slab, GFP_NOFS); | |
127e670a JK |
504 | new->inode = inode; |
505 | INIT_LIST_HEAD(&new->list); | |
506 | ||
507 | spin_lock(&sbi->dir_inode_lock); | |
5deb8267 JK |
508 | if (__add_dirty_inode(inode, new)) |
509 | kmem_cache_free(inode_entry_slab, new); | |
127e670a | 510 | |
127e670a JK |
511 | inode_inc_dirty_dents(inode); |
512 | SetPagePrivate(page); | |
5deb8267 JK |
513 | spin_unlock(&sbi->dir_inode_lock); |
514 | } | |
515 | ||
516 | void add_dirty_dir_inode(struct inode *inode) | |
517 | { | |
518 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); | |
7bd59381 GZ |
519 | struct dir_inode_entry *new = |
520 | f2fs_kmem_cache_alloc(inode_entry_slab, GFP_NOFS); | |
521 | ||
5deb8267 JK |
522 | new->inode = inode; |
523 | INIT_LIST_HEAD(&new->list); | |
127e670a | 524 | |
5deb8267 JK |
525 | spin_lock(&sbi->dir_inode_lock); |
526 | if (__add_dirty_inode(inode, new)) | |
527 | kmem_cache_free(inode_entry_slab, new); | |
127e670a JK |
528 | spin_unlock(&sbi->dir_inode_lock); |
529 | } | |
530 | ||
531 | void remove_dirty_dir_inode(struct inode *inode) | |
532 | { | |
533 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); | |
ce3b7d80 GZ |
534 | |
535 | struct list_head *this, *head; | |
127e670a JK |
536 | |
537 | if (!S_ISDIR(inode->i_mode)) | |
538 | return; | |
539 | ||
540 | spin_lock(&sbi->dir_inode_lock); | |
3b10b1fd JK |
541 | if (atomic_read(&F2FS_I(inode)->dirty_dents)) { |
542 | spin_unlock(&sbi->dir_inode_lock); | |
543 | return; | |
544 | } | |
127e670a | 545 | |
ce3b7d80 | 546 | head = &sbi->dir_inode_list; |
127e670a JK |
547 | list_for_each(this, head) { |
548 | struct dir_inode_entry *entry; | |
549 | entry = list_entry(this, struct dir_inode_entry, list); | |
550 | if (entry->inode == inode) { | |
551 | list_del(&entry->list); | |
552 | kmem_cache_free(inode_entry_slab, entry); | |
dcdfff65 | 553 | stat_dec_dirty_dir(sbi); |
127e670a JK |
554 | break; |
555 | } | |
556 | } | |
127e670a | 557 | spin_unlock(&sbi->dir_inode_lock); |
74d0b917 JK |
558 | |
559 | /* Only from the recovery routine */ | |
afc3eda2 JK |
560 | if (is_inode_flag_set(F2FS_I(inode), FI_DELAY_IPUT)) { |
561 | clear_inode_flag(F2FS_I(inode), FI_DELAY_IPUT); | |
74d0b917 | 562 | iput(inode); |
afc3eda2 | 563 | } |
74d0b917 JK |
564 | } |
565 | ||
566 | struct inode *check_dirty_dir_inode(struct f2fs_sb_info *sbi, nid_t ino) | |
567 | { | |
ce3b7d80 GZ |
568 | |
569 | struct list_head *this, *head; | |
74d0b917 JK |
570 | struct inode *inode = NULL; |
571 | ||
572 | spin_lock(&sbi->dir_inode_lock); | |
ce3b7d80 GZ |
573 | |
574 | head = &sbi->dir_inode_list; | |
74d0b917 JK |
575 | list_for_each(this, head) { |
576 | struct dir_inode_entry *entry; | |
577 | entry = list_entry(this, struct dir_inode_entry, list); | |
578 | if (entry->inode->i_ino == ino) { | |
579 | inode = entry->inode; | |
580 | break; | |
581 | } | |
582 | } | |
583 | spin_unlock(&sbi->dir_inode_lock); | |
584 | return inode; | |
127e670a JK |
585 | } |
586 | ||
587 | void sync_dirty_dir_inodes(struct f2fs_sb_info *sbi) | |
588 | { | |
ce3b7d80 | 589 | struct list_head *head; |
127e670a JK |
590 | struct dir_inode_entry *entry; |
591 | struct inode *inode; | |
592 | retry: | |
593 | spin_lock(&sbi->dir_inode_lock); | |
ce3b7d80 GZ |
594 | |
595 | head = &sbi->dir_inode_list; | |
127e670a JK |
596 | if (list_empty(head)) { |
597 | spin_unlock(&sbi->dir_inode_lock); | |
598 | return; | |
599 | } | |
600 | entry = list_entry(head->next, struct dir_inode_entry, list); | |
601 | inode = igrab(entry->inode); | |
602 | spin_unlock(&sbi->dir_inode_lock); | |
603 | if (inode) { | |
604 | filemap_flush(inode->i_mapping); | |
605 | iput(inode); | |
606 | } else { | |
607 | /* | |
608 | * We should submit bio, since it exists several | |
609 | * wribacking dentry pages in the freeing inode. | |
610 | */ | |
458e6197 | 611 | f2fs_submit_merged_bio(sbi, DATA, WRITE); |
127e670a JK |
612 | } |
613 | goto retry; | |
614 | } | |
615 | ||
0a8165d7 | 616 | /* |
127e670a JK |
617 | * Freeze all the FS-operations for checkpoint. |
618 | */ | |
43727527 | 619 | static void block_operations(struct f2fs_sb_info *sbi) |
127e670a | 620 | { |
127e670a JK |
621 | struct writeback_control wbc = { |
622 | .sync_mode = WB_SYNC_ALL, | |
623 | .nr_to_write = LONG_MAX, | |
624 | .for_reclaim = 0, | |
625 | }; | |
c718379b JK |
626 | struct blk_plug plug; |
627 | ||
628 | blk_start_plug(&plug); | |
629 | ||
39936837 | 630 | retry_flush_dents: |
e479556b | 631 | f2fs_lock_all(sbi); |
127e670a | 632 | /* write all the dirty dentry pages */ |
127e670a | 633 | if (get_pages(sbi, F2FS_DIRTY_DENTS)) { |
e479556b | 634 | f2fs_unlock_all(sbi); |
39936837 JK |
635 | sync_dirty_dir_inodes(sbi); |
636 | goto retry_flush_dents; | |
127e670a JK |
637 | } |
638 | ||
127e670a JK |
639 | /* |
640 | * POR: we should ensure that there is no dirty node pages | |
641 | * until finishing nat/sit flush. | |
642 | */ | |
39936837 JK |
643 | retry_flush_nodes: |
644 | mutex_lock(&sbi->node_write); | |
127e670a JK |
645 | |
646 | if (get_pages(sbi, F2FS_DIRTY_NODES)) { | |
39936837 JK |
647 | mutex_unlock(&sbi->node_write); |
648 | sync_node_pages(sbi, 0, &wbc); | |
649 | goto retry_flush_nodes; | |
127e670a | 650 | } |
c718379b | 651 | blk_finish_plug(&plug); |
127e670a JK |
652 | } |
653 | ||
654 | static void unblock_operations(struct f2fs_sb_info *sbi) | |
655 | { | |
39936837 | 656 | mutex_unlock(&sbi->node_write); |
e479556b | 657 | f2fs_unlock_all(sbi); |
127e670a JK |
658 | } |
659 | ||
fb51b5ef CL |
660 | static void wait_on_all_pages_writeback(struct f2fs_sb_info *sbi) |
661 | { | |
662 | DEFINE_WAIT(wait); | |
663 | ||
664 | for (;;) { | |
665 | prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE); | |
666 | ||
667 | if (!get_pages(sbi, F2FS_WRITEBACK)) | |
668 | break; | |
669 | ||
670 | io_schedule(); | |
671 | } | |
672 | finish_wait(&sbi->cp_wait, &wait); | |
673 | } | |
674 | ||
127e670a JK |
675 | static void do_checkpoint(struct f2fs_sb_info *sbi, bool is_umount) |
676 | { | |
677 | struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); | |
678 | nid_t last_nid = 0; | |
679 | block_t start_blk; | |
680 | struct page *cp_page; | |
681 | unsigned int data_sum_blocks, orphan_blocks; | |
7e586fa0 | 682 | __u32 crc32 = 0; |
127e670a | 683 | void *kaddr; |
127e670a JK |
684 | int i; |
685 | ||
686 | /* Flush all the NAT/SIT pages */ | |
687 | while (get_pages(sbi, F2FS_DIRTY_META)) | |
688 | sync_meta_pages(sbi, META, LONG_MAX); | |
689 | ||
690 | next_free_nid(sbi, &last_nid); | |
691 | ||
692 | /* | |
693 | * modify checkpoint | |
694 | * version number is already updated | |
695 | */ | |
696 | ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi)); | |
697 | ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi)); | |
698 | ckpt->free_segment_count = cpu_to_le32(free_segments(sbi)); | |
699 | for (i = 0; i < 3; i++) { | |
700 | ckpt->cur_node_segno[i] = | |
701 | cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE)); | |
702 | ckpt->cur_node_blkoff[i] = | |
703 | cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE)); | |
704 | ckpt->alloc_type[i + CURSEG_HOT_NODE] = | |
705 | curseg_alloc_type(sbi, i + CURSEG_HOT_NODE); | |
706 | } | |
707 | for (i = 0; i < 3; i++) { | |
708 | ckpt->cur_data_segno[i] = | |
709 | cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA)); | |
710 | ckpt->cur_data_blkoff[i] = | |
711 | cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA)); | |
712 | ckpt->alloc_type[i + CURSEG_HOT_DATA] = | |
713 | curseg_alloc_type(sbi, i + CURSEG_HOT_DATA); | |
714 | } | |
715 | ||
716 | ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi)); | |
717 | ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi)); | |
718 | ckpt->next_free_nid = cpu_to_le32(last_nid); | |
719 | ||
720 | /* 2 cp + n data seg summary + orphan inode blocks */ | |
721 | data_sum_blocks = npages_for_summary_flush(sbi); | |
722 | if (data_sum_blocks < 3) | |
25ca923b | 723 | set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG); |
127e670a | 724 | else |
25ca923b | 725 | clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG); |
127e670a JK |
726 | |
727 | orphan_blocks = (sbi->n_orphans + F2FS_ORPHANS_PER_BLOCK - 1) | |
728 | / F2FS_ORPHANS_PER_BLOCK; | |
25ca923b | 729 | ckpt->cp_pack_start_sum = cpu_to_le32(1 + orphan_blocks); |
127e670a JK |
730 | |
731 | if (is_umount) { | |
25ca923b JK |
732 | set_ckpt_flags(ckpt, CP_UMOUNT_FLAG); |
733 | ckpt->cp_pack_total_block_count = cpu_to_le32(2 + | |
734 | data_sum_blocks + orphan_blocks + NR_CURSEG_NODE_TYPE); | |
127e670a | 735 | } else { |
25ca923b JK |
736 | clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG); |
737 | ckpt->cp_pack_total_block_count = cpu_to_le32(2 + | |
738 | data_sum_blocks + orphan_blocks); | |
127e670a JK |
739 | } |
740 | ||
741 | if (sbi->n_orphans) | |
25ca923b | 742 | set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG); |
127e670a | 743 | else |
25ca923b | 744 | clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG); |
127e670a JK |
745 | |
746 | /* update SIT/NAT bitmap */ | |
747 | get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP)); | |
748 | get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP)); | |
749 | ||
750 | crc32 = f2fs_crc32(ckpt, le32_to_cpu(ckpt->checksum_offset)); | |
7e586fa0 JK |
751 | *((__le32 *)((unsigned char *)ckpt + |
752 | le32_to_cpu(ckpt->checksum_offset))) | |
127e670a JK |
753 | = cpu_to_le32(crc32); |
754 | ||
755 | start_blk = __start_cp_addr(sbi); | |
756 | ||
757 | /* write out checkpoint buffer at block 0 */ | |
758 | cp_page = grab_meta_page(sbi, start_blk++); | |
759 | kaddr = page_address(cp_page); | |
760 | memcpy(kaddr, ckpt, (1 << sbi->log_blocksize)); | |
761 | set_page_dirty(cp_page); | |
762 | f2fs_put_page(cp_page, 1); | |
763 | ||
764 | if (sbi->n_orphans) { | |
765 | write_orphan_inodes(sbi, start_blk); | |
766 | start_blk += orphan_blocks; | |
767 | } | |
768 | ||
769 | write_data_summaries(sbi, start_blk); | |
770 | start_blk += data_sum_blocks; | |
771 | if (is_umount) { | |
772 | write_node_summaries(sbi, start_blk); | |
773 | start_blk += NR_CURSEG_NODE_TYPE; | |
774 | } | |
775 | ||
776 | /* writeout checkpoint block */ | |
777 | cp_page = grab_meta_page(sbi, start_blk); | |
778 | kaddr = page_address(cp_page); | |
779 | memcpy(kaddr, ckpt, (1 << sbi->log_blocksize)); | |
780 | set_page_dirty(cp_page); | |
781 | f2fs_put_page(cp_page, 1); | |
782 | ||
783 | /* wait for previous submitted node/meta pages writeback */ | |
fb51b5ef | 784 | wait_on_all_pages_writeback(sbi); |
127e670a | 785 | |
4ef51a8f | 786 | filemap_fdatawait_range(NODE_MAPPING(sbi), 0, LONG_MAX); |
9df27d98 | 787 | filemap_fdatawait_range(META_MAPPING(sbi), 0, LONG_MAX); |
127e670a JK |
788 | |
789 | /* update user_block_counts */ | |
790 | sbi->last_valid_block_count = sbi->total_valid_block_count; | |
791 | sbi->alloc_valid_block_count = 0; | |
792 | ||
793 | /* Here, we only have one bio having CP pack */ | |
577e3495 | 794 | sync_meta_pages(sbi, META_FLUSH, LONG_MAX); |
127e670a | 795 | |
6bacf52f | 796 | if (unlikely(!is_set_ckpt_flags(ckpt, CP_ERROR_FLAG))) { |
577e3495 JK |
797 | clear_prefree_segments(sbi); |
798 | F2FS_RESET_SB_DIRT(sbi); | |
799 | } | |
127e670a JK |
800 | } |
801 | ||
0a8165d7 | 802 | /* |
127e670a JK |
803 | * We guarantee that this checkpoint procedure should not fail. |
804 | */ | |
43727527 | 805 | void write_checkpoint(struct f2fs_sb_info *sbi, bool is_umount) |
127e670a JK |
806 | { |
807 | struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); | |
808 | unsigned long long ckpt_ver; | |
809 | ||
2af4bd6c NJ |
810 | trace_f2fs_write_checkpoint(sbi->sb, is_umount, "start block_ops"); |
811 | ||
43727527 JK |
812 | mutex_lock(&sbi->cp_mutex); |
813 | block_operations(sbi); | |
127e670a | 814 | |
2af4bd6c NJ |
815 | trace_f2fs_write_checkpoint(sbi->sb, is_umount, "finish block_ops"); |
816 | ||
458e6197 JK |
817 | f2fs_submit_merged_bio(sbi, DATA, WRITE); |
818 | f2fs_submit_merged_bio(sbi, NODE, WRITE); | |
819 | f2fs_submit_merged_bio(sbi, META, WRITE); | |
127e670a JK |
820 | |
821 | /* | |
822 | * update checkpoint pack index | |
823 | * Increase the version number so that | |
824 | * SIT entries and seg summaries are written at correct place | |
825 | */ | |
d71b5564 | 826 | ckpt_ver = cur_cp_version(ckpt); |
127e670a JK |
827 | ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver); |
828 | ||
829 | /* write cached NAT/SIT entries to NAT/SIT area */ | |
830 | flush_nat_entries(sbi); | |
831 | flush_sit_entries(sbi); | |
832 | ||
127e670a JK |
833 | /* unlock all the fs_lock[] in do_checkpoint() */ |
834 | do_checkpoint(sbi, is_umount); | |
835 | ||
836 | unblock_operations(sbi); | |
837 | mutex_unlock(&sbi->cp_mutex); | |
2af4bd6c NJ |
838 | |
839 | trace_f2fs_write_checkpoint(sbi->sb, is_umount, "finish checkpoint"); | |
127e670a JK |
840 | } |
841 | ||
842 | void init_orphan_info(struct f2fs_sb_info *sbi) | |
843 | { | |
17b692f6 | 844 | spin_lock_init(&sbi->orphan_inode_lock); |
127e670a JK |
845 | INIT_LIST_HEAD(&sbi->orphan_inode_list); |
846 | sbi->n_orphans = 0; | |
0d47c1ad GZ |
847 | /* |
848 | * considering 512 blocks in a segment 8 blocks are needed for cp | |
849 | * and log segment summaries. Remaining blocks are used to keep | |
850 | * orphan entries with the limitation one reserved segment | |
851 | * for cp pack we can have max 1020*504 orphan entries | |
852 | */ | |
853 | sbi->max_orphans = (sbi->blocks_per_seg - 2 - NR_CURSEG_TYPE) | |
854 | * F2FS_ORPHANS_PER_BLOCK; | |
127e670a JK |
855 | } |
856 | ||
6e6093a8 | 857 | int __init create_checkpoint_caches(void) |
127e670a JK |
858 | { |
859 | orphan_entry_slab = f2fs_kmem_cache_create("f2fs_orphan_entry", | |
860 | sizeof(struct orphan_inode_entry), NULL); | |
6bacf52f | 861 | if (!orphan_entry_slab) |
127e670a JK |
862 | return -ENOMEM; |
863 | inode_entry_slab = f2fs_kmem_cache_create("f2fs_dirty_dir_entry", | |
864 | sizeof(struct dir_inode_entry), NULL); | |
6bacf52f | 865 | if (!inode_entry_slab) { |
127e670a JK |
866 | kmem_cache_destroy(orphan_entry_slab); |
867 | return -ENOMEM; | |
868 | } | |
869 | return 0; | |
870 | } | |
871 | ||
872 | void destroy_checkpoint_caches(void) | |
873 | { | |
874 | kmem_cache_destroy(orphan_entry_slab); | |
875 | kmem_cache_destroy(inode_entry_slab); | |
876 | } |