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