blob: 8f5dff1989a83c382627458a7cb65a825abab371 [file] [log] [blame]
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001/*
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#include <trace/events/f2fs.h>
24
25static struct kmem_cache *orphan_entry_slab;
26static struct kmem_cache *inode_entry_slab;
27
28/*
29 * We guarantee no failure on the returned page.
30 */
31struct page *grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
32{
Changman Leeb1a94e82013-11-15 10:42:51 +090033 struct address_space *mapping = META_MAPPING(sbi);
Linus Torvalds8005ecc2012-12-20 13:54:51 -080034 struct page *page = NULL;
35repeat:
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
48/*
49 * We guarantee no failure on the returned page.
50 */
51struct page *get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
52{
Changman Leeb1a94e82013-11-15 10:42:51 +090053 struct address_space *mapping = META_MAPPING(sbi);
Linus Torvalds8005ecc2012-12-20 13:54:51 -080054 struct page *page;
55repeat:
56 page = grab_cache_page(mapping, index);
57 if (!page) {
58 cond_resched();
59 goto repeat;
60 }
61 if (PageUptodate(page))
62 goto out;
63
Changman Leeb1a94e82013-11-15 10:42:51 +090064 if (f2fs_submit_page_bio(sbi, page, index,
65 READ_SYNC | REQ_META | REQ_PRIO))
Linus Torvalds8005ecc2012-12-20 13:54:51 -080066 goto repeat;
67
68 lock_page(page);
Changman Leeb1a94e82013-11-15 10:42:51 +090069 if (unlikely(page->mapping != mapping)) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -080070 f2fs_put_page(page, 1);
71 goto repeat;
72 }
73out:
74 mark_page_accessed(page);
75 return page;
76}
77
78static 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);
83
Jaegeuk Kim4c57cbe2014-02-05 13:03:57 +090084 if (unlikely(sbi->por_doing))
Changman Leeb1a94e82013-11-15 10:42:51 +090085 goto redirty_out;
Changman Leeb1a94e82013-11-15 10:42:51 +090086 if (wbc->for_reclaim)
87 goto redirty_out;
Linus Torvalds8005ecc2012-12-20 13:54:51 -080088
Jaegeuk Kim4c57cbe2014-02-05 13:03:57 +090089 /* 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;
Linus Torvalds8005ecc2012-12-20 13:54:51 -080092
Jaegeuk Kim4c57cbe2014-02-05 13:03:57 +090093 wait_on_page_writeback(page);
Linus Torvalds8005ecc2012-12-20 13:54:51 -080094 write_meta_page(sbi, page);
Jaegeuk Kim4c57cbe2014-02-05 13:03:57 +090095no_write:
Linus Torvalds8005ecc2012-12-20 13:54:51 -080096 dec_page_count(sbi, F2FS_DIRTY_META);
97 unlock_page(page);
98 return 0;
Changman Leeb1a94e82013-11-15 10:42:51 +090099
100redirty_out:
101 dec_page_count(sbi, F2FS_DIRTY_META);
102 wbc->pages_skipped++;
103 set_page_dirty(page);
104 return AOP_WRITEPAGE_ACTIVATE;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800105}
106
107static 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);
Changman Leeb1a94e82013-11-15 10:42:51 +0900111 int nrpages = MAX_BIO_BLOCKS(max_hw_blocks(sbi));
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800112 long written;
113
114 if (wbc->for_kupdate)
115 return 0;
116
Changman Leeb1a94e82013-11-15 10:42:51 +0900117 /* collect a number of dirty meta pages and write together */
118 if (get_pages(sbi, F2FS_DIRTY_META) < nrpages)
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800119 return 0;
120
121 /* if mounting is failed, skip writing node pages */
122 mutex_lock(&sbi->cp_mutex);
Changman Leeb1a94e82013-11-15 10:42:51 +0900123 written = sync_meta_pages(sbi, META, nrpages);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800124 mutex_unlock(&sbi->cp_mutex);
125 wbc->nr_to_write -= written;
126 return 0;
127}
128
129long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
130 long nr_to_write)
131{
Changman Leeb1a94e82013-11-15 10:42:51 +0900132 struct address_space *mapping = META_MAPPING(sbi);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800133 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);
Changman Leeb1a94e82013-11-15 10:42:51 +0900147 if (unlikely(nr_pages == 0))
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800148 break;
149
150 for (i = 0; i < nr_pages; i++) {
151 struct page *page = pvec.pages[i];
Jaegeuk Kim4c57cbe2014-02-05 13:03:57 +0900152
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800153 lock_page(page);
Jaegeuk Kim4c57cbe2014-02-05 13:03:57 +0900154
155 if (unlikely(page->mapping != mapping)) {
156continue_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
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800168 if (f2fs_write_meta_page(page, &wbc)) {
169 unlock_page(page);
170 break;
171 }
Changman Leeb1a94e82013-11-15 10:42:51 +0900172 nwritten++;
173 if (unlikely(nwritten >= nr_to_write))
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800174 break;
175 }
176 pagevec_release(&pvec);
177 cond_resched();
178 }
179
180 if (nwritten)
Changman Leeb1a94e82013-11-15 10:42:51 +0900181 f2fs_submit_merged_bio(sbi, type, WRITE);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800182
183 return nwritten;
184}
185
186static 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
191 trace_f2fs_set_page_dirty(page, META);
192
193 SetPageUptodate(page);
194 if (!PageDirty(page)) {
195 __set_page_dirty_nobuffers(page);
196 inc_page_count(sbi, F2FS_DIRTY_META);
197 return 1;
198 }
199 return 0;
200}
201
202const 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
208int acquire_orphan_inode(struct f2fs_sb_info *sbi)
209{
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800210 int err = 0;
211
Changman Leeb1a94e82013-11-15 10:42:51 +0900212 spin_lock(&sbi->orphan_inode_lock);
213 if (unlikely(sbi->n_orphans >= sbi->max_orphans))
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800214 err = -ENOSPC;
215 else
216 sbi->n_orphans++;
Changman Leeb1a94e82013-11-15 10:42:51 +0900217 spin_unlock(&sbi->orphan_inode_lock);
218
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800219 return err;
220}
221
222void release_orphan_inode(struct f2fs_sb_info *sbi)
223{
Changman Leeb1a94e82013-11-15 10:42:51 +0900224 spin_lock(&sbi->orphan_inode_lock);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800225 f2fs_bug_on(sbi->n_orphans == 0);
226 sbi->n_orphans--;
Changman Leeb1a94e82013-11-15 10:42:51 +0900227 spin_unlock(&sbi->orphan_inode_lock);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800228}
229
230void 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
Changman Leeb1a94e82013-11-15 10:42:51 +0900235 new = f2fs_kmem_cache_alloc(orphan_entry_slab, GFP_ATOMIC);
236 new->ino = ino;
237
238 spin_lock(&sbi->orphan_inode_lock);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800239 head = &sbi->orphan_inode_list;
240 list_for_each(this, head) {
241 orphan = list_entry(this, struct orphan_inode_entry, list);
Changman Leeb1a94e82013-11-15 10:42:51 +0900242 if (orphan->ino == ino) {
243 spin_unlock(&sbi->orphan_inode_lock);
244 kmem_cache_free(orphan_entry_slab, new);
245 return;
246 }
247
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800248 if (orphan->ino > ino)
249 break;
250 orphan = NULL;
251 }
252
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800253 /* add new_oentry into list which is sorted by inode number */
254 if (orphan)
255 list_add(&new->list, this->prev);
256 else
257 list_add_tail(&new->list, head);
Changman Leeb1a94e82013-11-15 10:42:51 +0900258 spin_unlock(&sbi->orphan_inode_lock);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800259}
260
261void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
262{
263 struct list_head *head;
264 struct orphan_inode_entry *orphan;
265
Changman Leeb1a94e82013-11-15 10:42:51 +0900266 spin_lock(&sbi->orphan_inode_lock);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800267 head = &sbi->orphan_inode_list;
268 list_for_each_entry(orphan, head, list) {
269 if (orphan->ino == ino) {
270 list_del(&orphan->list);
271 kmem_cache_free(orphan_entry_slab, orphan);
272 f2fs_bug_on(sbi->n_orphans == 0);
273 sbi->n_orphans--;
274 break;
275 }
276 }
Changman Leeb1a94e82013-11-15 10:42:51 +0900277 spin_unlock(&sbi->orphan_inode_lock);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800278}
279
280static void recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
281{
282 struct inode *inode = f2fs_iget(sbi->sb, ino);
283 f2fs_bug_on(IS_ERR(inode));
284 clear_nlink(inode);
285
286 /* truncate all the data during iput */
287 iput(inode);
288}
289
Changman Leeb1a94e82013-11-15 10:42:51 +0900290void recover_orphan_inodes(struct f2fs_sb_info *sbi)
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800291{
292 block_t start_blk, orphan_blkaddr, i, j;
293
294 if (!is_set_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG))
Changman Leeb1a94e82013-11-15 10:42:51 +0900295 return;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800296
297 sbi->por_doing = true;
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 */
313 clear_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG);
314 sbi->por_doing = false;
Changman Leeb1a94e82013-11-15 10:42:51 +0900315 return;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800316}
317
318static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk)
319{
Changman Leeb1a94e82013-11-15 10:42:51 +0900320 struct list_head *head;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800321 struct f2fs_orphan_block *orphan_blk = NULL;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800322 unsigned int nentries = 0;
Changman Leeb1a94e82013-11-15 10:42:51 +0900323 unsigned short index;
324 unsigned short orphan_blocks = (unsigned short)((sbi->n_orphans +
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800325 (F2FS_ORPHANS_PER_BLOCK - 1)) / F2FS_ORPHANS_PER_BLOCK);
Changman Leeb1a94e82013-11-15 10:42:51 +0900326 struct page *page = NULL;
327 struct orphan_inode_entry *orphan = NULL;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800328
Changman Leeb1a94e82013-11-15 10:42:51 +0900329 for (index = 0; index < orphan_blocks; index++)
330 grab_meta_page(sbi, start_blk + index);
331
332 index = 1;
333 spin_lock(&sbi->orphan_inode_lock);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800334 head = &sbi->orphan_inode_list;
335
336 /* loop for each orphan inode entry and write them in Jornal block */
Changman Leeb1a94e82013-11-15 10:42:51 +0900337 list_for_each_entry(orphan, head, list) {
338 if (!page) {
339 page = find_get_page(META_MAPPING(sbi), start_blk++);
340 f2fs_bug_on(!page);
341 orphan_blk =
342 (struct f2fs_orphan_block *)page_address(page);
343 memset(orphan_blk, 0, sizeof(*orphan_blk));
344 f2fs_put_page(page, 0);
345 }
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800346
Changman Leeb1a94e82013-11-15 10:42:51 +0900347 orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800348
349 if (nentries == F2FS_ORPHANS_PER_BLOCK) {
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++;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800361 nentries = 0;
362 page = NULL;
363 }
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800364 }
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800365
Changman Leeb1a94e82013-11-15 10:42:51 +0900366 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);
372 }
373
374 spin_unlock(&sbi->orphan_inode_lock);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800375}
376
377static 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;
384 size_t crc_offset;
385 __u32 crc = 0;
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
396 crc = le32_to_cpu(*((__u32 *)((unsigned char *)cp_block + crc_offset)));
397 if (!f2fs_crc_valid(crc, cp_block, crc_offset))
398 goto invalid_cp1;
399
400 pre_version = cur_cp_version(cp_block);
401
402 /* Read the 2nd cp block in this CP pack */
403 cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1;
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
411 crc = le32_to_cpu(*((__u32 *)((unsigned char *)cp_block + crc_offset)));
412 if (!f2fs_crc_valid(crc, cp_block, crc_offset))
413 goto invalid_cp2;
414
415 cur_version = cur_cp_version(cp_block);
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 }
422invalid_cp2:
423 f2fs_put_page(cp_page_2, 1);
424invalid_cp1:
425 f2fs_put_page(cp_page_1, 1);
426 return NULL;
427}
428
429int 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 */
Changman Leeb1a94e82013-11-15 10:42:51 +0900449 cp_start_blk_no += ((unsigned long long)1) <<
450 le32_to_cpu(fsb->log_blocks_per_seg);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800451 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
473fail_no_cp:
474 kfree(sbi->ckpt);
475 return -EINVAL;
476}
477
478static int __add_dirty_inode(struct inode *inode, struct dir_inode_entry *new)
479{
480 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
481 struct list_head *head = &sbi->dir_inode_list;
482 struct list_head *this;
483
484 list_for_each(this, head) {
485 struct dir_inode_entry *entry;
486 entry = list_entry(this, struct dir_inode_entry, list);
Changman Leeb1a94e82013-11-15 10:42:51 +0900487 if (unlikely(entry->inode == inode))
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800488 return -EEXIST;
489 }
490 list_add_tail(&new->list, head);
491 stat_inc_dirty_dir(sbi);
492 return 0;
493}
494
495void 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
500 if (!S_ISDIR(inode->i_mode))
501 return;
502
503 new = f2fs_kmem_cache_alloc(inode_entry_slab, GFP_NOFS);
504 new->inode = inode;
505 INIT_LIST_HEAD(&new->list);
506
507 spin_lock(&sbi->dir_inode_lock);
508 if (__add_dirty_inode(inode, new))
509 kmem_cache_free(inode_entry_slab, new);
510
511 inc_page_count(sbi, F2FS_DIRTY_DENTS);
512 inode_inc_dirty_dents(inode);
513 SetPagePrivate(page);
514 spin_unlock(&sbi->dir_inode_lock);
515}
516
517void add_dirty_dir_inode(struct inode *inode)
518{
519 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
520 struct dir_inode_entry *new =
521 f2fs_kmem_cache_alloc(inode_entry_slab, GFP_NOFS);
522
523 new->inode = inode;
524 INIT_LIST_HEAD(&new->list);
525
526 spin_lock(&sbi->dir_inode_lock);
527 if (__add_dirty_inode(inode, new))
528 kmem_cache_free(inode_entry_slab, new);
529 spin_unlock(&sbi->dir_inode_lock);
530}
531
532void remove_dirty_dir_inode(struct inode *inode)
533{
534 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Changman Leeb1a94e82013-11-15 10:42:51 +0900535
536 struct list_head *this, *head;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800537
538 if (!S_ISDIR(inode->i_mode))
539 return;
540
541 spin_lock(&sbi->dir_inode_lock);
542 if (atomic_read(&F2FS_I(inode)->dirty_dents)) {
543 spin_unlock(&sbi->dir_inode_lock);
544 return;
545 }
546
Changman Leeb1a94e82013-11-15 10:42:51 +0900547 head = &sbi->dir_inode_list;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800548 list_for_each(this, head) {
549 struct dir_inode_entry *entry;
550 entry = list_entry(this, struct dir_inode_entry, list);
551 if (entry->inode == inode) {
552 list_del(&entry->list);
553 kmem_cache_free(inode_entry_slab, entry);
554 stat_dec_dirty_dir(sbi);
555 break;
556 }
557 }
558 spin_unlock(&sbi->dir_inode_lock);
559
560 /* Only from the recovery routine */
561 if (is_inode_flag_set(F2FS_I(inode), FI_DELAY_IPUT)) {
562 clear_inode_flag(F2FS_I(inode), FI_DELAY_IPUT);
563 iput(inode);
564 }
565}
566
567struct inode *check_dirty_dir_inode(struct f2fs_sb_info *sbi, nid_t ino)
568{
Changman Leeb1a94e82013-11-15 10:42:51 +0900569
570 struct list_head *this, *head;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800571 struct inode *inode = NULL;
572
573 spin_lock(&sbi->dir_inode_lock);
Changman Leeb1a94e82013-11-15 10:42:51 +0900574
575 head = &sbi->dir_inode_list;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800576 list_for_each(this, head) {
577 struct dir_inode_entry *entry;
578 entry = list_entry(this, struct dir_inode_entry, list);
579 if (entry->inode->i_ino == ino) {
580 inode = entry->inode;
581 break;
582 }
583 }
584 spin_unlock(&sbi->dir_inode_lock);
585 return inode;
586}
587
588void sync_dirty_dir_inodes(struct f2fs_sb_info *sbi)
589{
Changman Leeb1a94e82013-11-15 10:42:51 +0900590 struct list_head *head;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800591 struct dir_inode_entry *entry;
592 struct inode *inode;
593retry:
594 spin_lock(&sbi->dir_inode_lock);
Changman Leeb1a94e82013-11-15 10:42:51 +0900595
596 head = &sbi->dir_inode_list;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800597 if (list_empty(head)) {
598 spin_unlock(&sbi->dir_inode_lock);
599 return;
600 }
601 entry = list_entry(head->next, struct dir_inode_entry, list);
602 inode = igrab(entry->inode);
603 spin_unlock(&sbi->dir_inode_lock);
604 if (inode) {
605 filemap_flush(inode->i_mapping);
606 iput(inode);
607 } else {
608 /*
609 * We should submit bio, since it exists several
610 * wribacking dentry pages in the freeing inode.
611 */
Changman Leeb1a94e82013-11-15 10:42:51 +0900612 f2fs_submit_merged_bio(sbi, DATA, WRITE);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800613 }
614 goto retry;
615}
616
617/*
618 * Freeze all the FS-operations for checkpoint.
619 */
620static void block_operations(struct f2fs_sb_info *sbi)
621{
622 struct writeback_control wbc = {
623 .sync_mode = WB_SYNC_ALL,
624 .nr_to_write = LONG_MAX,
625 .for_reclaim = 0,
626 };
627 struct blk_plug plug;
628
629 blk_start_plug(&plug);
630
631retry_flush_dents:
632 f2fs_lock_all(sbi);
633 /* write all the dirty dentry pages */
634 if (get_pages(sbi, F2FS_DIRTY_DENTS)) {
635 f2fs_unlock_all(sbi);
636 sync_dirty_dir_inodes(sbi);
637 goto retry_flush_dents;
638 }
639
640 /*
641 * POR: we should ensure that there is no dirty node pages
642 * until finishing nat/sit flush.
643 */
644retry_flush_nodes:
645 mutex_lock(&sbi->node_write);
646
647 if (get_pages(sbi, F2FS_DIRTY_NODES)) {
648 mutex_unlock(&sbi->node_write);
649 sync_node_pages(sbi, 0, &wbc);
650 goto retry_flush_nodes;
651 }
652 blk_finish_plug(&plug);
653}
654
655static void unblock_operations(struct f2fs_sb_info *sbi)
656{
657 mutex_unlock(&sbi->node_write);
658 f2fs_unlock_all(sbi);
659}
660
661static void wait_on_all_pages_writeback(struct f2fs_sb_info *sbi)
662{
663 DEFINE_WAIT(wait);
664
665 for (;;) {
666 prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE);
667
668 if (!get_pages(sbi, F2FS_WRITEBACK))
669 break;
670
671 io_schedule();
672 }
673 finish_wait(&sbi->cp_wait, &wait);
674}
675
676static void do_checkpoint(struct f2fs_sb_info *sbi, bool is_umount)
677{
678 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
679 nid_t last_nid = 0;
680 block_t start_blk;
681 struct page *cp_page;
682 unsigned int data_sum_blocks, orphan_blocks;
683 __u32 crc32 = 0;
684 void *kaddr;
685 int i;
686
687 /* Flush all the NAT/SIT pages */
688 while (get_pages(sbi, F2FS_DIRTY_META))
689 sync_meta_pages(sbi, META, LONG_MAX);
690
691 next_free_nid(sbi, &last_nid);
692
693 /*
694 * modify checkpoint
695 * version number is already updated
696 */
697 ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi));
698 ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi));
699 ckpt->free_segment_count = cpu_to_le32(free_segments(sbi));
700 for (i = 0; i < 3; i++) {
701 ckpt->cur_node_segno[i] =
702 cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE));
703 ckpt->cur_node_blkoff[i] =
704 cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE));
705 ckpt->alloc_type[i + CURSEG_HOT_NODE] =
706 curseg_alloc_type(sbi, i + CURSEG_HOT_NODE);
707 }
708 for (i = 0; i < 3; i++) {
709 ckpt->cur_data_segno[i] =
710 cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA));
711 ckpt->cur_data_blkoff[i] =
712 cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA));
713 ckpt->alloc_type[i + CURSEG_HOT_DATA] =
714 curseg_alloc_type(sbi, i + CURSEG_HOT_DATA);
715 }
716
717 ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi));
718 ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi));
719 ckpt->next_free_nid = cpu_to_le32(last_nid);
720
721 /* 2 cp + n data seg summary + orphan inode blocks */
722 data_sum_blocks = npages_for_summary_flush(sbi);
723 if (data_sum_blocks < 3)
724 set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
725 else
726 clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
727
728 orphan_blocks = (sbi->n_orphans + F2FS_ORPHANS_PER_BLOCK - 1)
729 / F2FS_ORPHANS_PER_BLOCK;
730 ckpt->cp_pack_start_sum = cpu_to_le32(1 + orphan_blocks);
731
732 if (is_umount) {
733 set_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
734 ckpt->cp_pack_total_block_count = cpu_to_le32(2 +
735 data_sum_blocks + orphan_blocks + NR_CURSEG_NODE_TYPE);
736 } else {
737 clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
738 ckpt->cp_pack_total_block_count = cpu_to_le32(2 +
739 data_sum_blocks + orphan_blocks);
740 }
741
742 if (sbi->n_orphans)
743 set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
744 else
745 clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
746
747 /* update SIT/NAT bitmap */
748 get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP));
749 get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP));
750
751 crc32 = f2fs_crc32(ckpt, le32_to_cpu(ckpt->checksum_offset));
752 *((__le32 *)((unsigned char *)ckpt +
753 le32_to_cpu(ckpt->checksum_offset)))
754 = cpu_to_le32(crc32);
755
756 start_blk = __start_cp_addr(sbi);
757
758 /* write out checkpoint buffer at block 0 */
759 cp_page = grab_meta_page(sbi, start_blk++);
760 kaddr = page_address(cp_page);
761 memcpy(kaddr, ckpt, (1 << sbi->log_blocksize));
762 set_page_dirty(cp_page);
763 f2fs_put_page(cp_page, 1);
764
765 if (sbi->n_orphans) {
766 write_orphan_inodes(sbi, start_blk);
767 start_blk += orphan_blocks;
768 }
769
770 write_data_summaries(sbi, start_blk);
771 start_blk += data_sum_blocks;
772 if (is_umount) {
773 write_node_summaries(sbi, start_blk);
774 start_blk += NR_CURSEG_NODE_TYPE;
775 }
776
777 /* writeout checkpoint block */
778 cp_page = grab_meta_page(sbi, start_blk);
779 kaddr = page_address(cp_page);
780 memcpy(kaddr, ckpt, (1 << sbi->log_blocksize));
781 set_page_dirty(cp_page);
782 f2fs_put_page(cp_page, 1);
783
784 /* wait for previous submitted node/meta pages writeback */
785 wait_on_all_pages_writeback(sbi);
786
Changman Leeb1a94e82013-11-15 10:42:51 +0900787 filemap_fdatawait_range(NODE_MAPPING(sbi), 0, LONG_MAX);
788 filemap_fdatawait_range(META_MAPPING(sbi), 0, LONG_MAX);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800789
790 /* update user_block_counts */
791 sbi->last_valid_block_count = sbi->total_valid_block_count;
792 sbi->alloc_valid_block_count = 0;
793
794 /* Here, we only have one bio having CP pack */
795 sync_meta_pages(sbi, META_FLUSH, LONG_MAX);
796
Changman Leeb1a94e82013-11-15 10:42:51 +0900797 if (unlikely(!is_set_ckpt_flags(ckpt, CP_ERROR_FLAG))) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800798 clear_prefree_segments(sbi);
799 F2FS_RESET_SB_DIRT(sbi);
800 }
801}
802
803/*
804 * We guarantee that this checkpoint procedure should not fail.
805 */
806void write_checkpoint(struct f2fs_sb_info *sbi, bool is_umount)
807{
808 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
809 unsigned long long ckpt_ver;
810
811 trace_f2fs_write_checkpoint(sbi->sb, is_umount, "start block_ops");
812
813 mutex_lock(&sbi->cp_mutex);
814 block_operations(sbi);
815
816 trace_f2fs_write_checkpoint(sbi->sb, is_umount, "finish block_ops");
817
Changman Leeb1a94e82013-11-15 10:42:51 +0900818 f2fs_submit_merged_bio(sbi, DATA, WRITE);
819 f2fs_submit_merged_bio(sbi, NODE, WRITE);
820 f2fs_submit_merged_bio(sbi, META, WRITE);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800821
822 /*
823 * update checkpoint pack index
824 * Increase the version number so that
825 * SIT entries and seg summaries are written at correct place
826 */
827 ckpt_ver = cur_cp_version(ckpt);
828 ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver);
829
830 /* write cached NAT/SIT entries to NAT/SIT area */
831 flush_nat_entries(sbi);
832 flush_sit_entries(sbi);
833
834 /* unlock all the fs_lock[] in do_checkpoint() */
835 do_checkpoint(sbi, is_umount);
836
837 unblock_operations(sbi);
838 mutex_unlock(&sbi->cp_mutex);
839
840 trace_f2fs_write_checkpoint(sbi->sb, is_umount, "finish checkpoint");
841}
842
843void init_orphan_info(struct f2fs_sb_info *sbi)
844{
Changman Leeb1a94e82013-11-15 10:42:51 +0900845 spin_lock_init(&sbi->orphan_inode_lock);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800846 INIT_LIST_HEAD(&sbi->orphan_inode_list);
847 sbi->n_orphans = 0;
Changman Leeb1a94e82013-11-15 10:42:51 +0900848 /*
849 * considering 512 blocks in a segment 8 blocks are needed for cp
850 * and log segment summaries. Remaining blocks are used to keep
851 * orphan entries with the limitation one reserved segment
852 * for cp pack we can have max 1020*504 orphan entries
853 */
854 sbi->max_orphans = (sbi->blocks_per_seg - 2 - NR_CURSEG_TYPE)
855 * F2FS_ORPHANS_PER_BLOCK;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800856}
857
858int __init create_checkpoint_caches(void)
859{
860 orphan_entry_slab = f2fs_kmem_cache_create("f2fs_orphan_entry",
861 sizeof(struct orphan_inode_entry), NULL);
Changman Leeb1a94e82013-11-15 10:42:51 +0900862 if (!orphan_entry_slab)
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800863 return -ENOMEM;
864 inode_entry_slab = f2fs_kmem_cache_create("f2fs_dirty_dir_entry",
865 sizeof(struct dir_inode_entry), NULL);
Changman Leeb1a94e82013-11-15 10:42:51 +0900866 if (!inode_entry_slab) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800867 kmem_cache_destroy(orphan_entry_slab);
868 return -ENOMEM;
869 }
870 return 0;
871}
872
873void destroy_checkpoint_caches(void)
874{
875 kmem_cache_destroy(orphan_entry_slab);
876 kmem_cache_destroy(inode_entry_slab);
877}