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