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