blob: f8c074961e0a96126caf33ac08c29a4212033e2b [file] [log] [blame]
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001/*
Jaegeuk Kim127e6702012-11-02 17:08:18 +09002 * 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"
Namjae Jeon2af4bd62013-04-23 18:26:54 +090023#include <trace/events/f2fs.h>
Jaegeuk Kim127e6702012-11-02 17:08:18 +090024
25static struct kmem_cache *orphan_entry_slab;
26static struct kmem_cache *inode_entry_slab;
27
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +090028/*
Jaegeuk Kim127e6702012-11-02 17:08:18 +090029 * We guarantee no failure on the returned page.
30 */
31struct 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;
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
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +090048/*
Jaegeuk Kim127e6702012-11-02 17:08:18 +090049 * We guarantee no failure on the returned page.
50 */
51struct 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;
55repeat:
56 page = grab_cache_page(mapping, index);
57 if (!page) {
58 cond_resched();
59 goto repeat;
60 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +090061 if (PageUptodate(page))
62 goto out;
Jaegeuk Kim127e6702012-11-02 17:08:18 +090063
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090064 if (f2fs_submit_page_bio(sbi, page, index,
65 READ_SYNC | REQ_META | REQ_PRIO))
Jaegeuk Kim393ff912013-03-08 21:29:23 +090066 goto repeat;
67
68 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090069 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +090070 f2fs_put_page(page, 1);
71 goto repeat;
72 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +090073out:
74 mark_page_accessed(page);
Jaegeuk Kim127e6702012-11-02 17:08:18 +090075 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);
Jaegeuk Kim577e3492013-01-24 19:56:11 +090083
84 /* Should not write any meta pages, if any IO error was occurred */
Chao Yucfb271d2013-12-05 17:15:22 +080085 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;
Jaegeuk Kim127e6702012-11-02 17:08:18 +090091
92 wait_on_page_writeback(page);
93
Jaegeuk Kim577e3492013-01-24 19:56:11 +090094 write_meta_page(sbi, page);
Jaegeuk Kim127e6702012-11-02 17:08:18 +090095 dec_page_count(sbi, F2FS_DIRTY_META);
Jaegeuk Kim577e3492013-01-24 19:56:11 +090096 unlock_page(page);
97 return 0;
Chao Yucfb271d2013-12-05 17:15:22 +080098
99redirty_out:
100 dec_page_count(sbi, F2FS_DIRTY_META);
101 wbc->pages_skipped++;
102 set_page_dirty(page);
103 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900104}
105
106static 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
127long 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);
Chao Yucfb271d2013-12-05 17:15:22 +0800145 if (unlikely(nr_pages == 0))
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900146 break;
147
148 for (i = 0; i < nr_pages; i++) {
149 struct page *page = pvec.pages[i];
150 lock_page(page);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900151 f2fs_bug_on(page->mapping != mapping);
152 f2fs_bug_on(!PageDirty(page));
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900153 clear_page_dirty_for_io(page);
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900154 if (f2fs_write_meta_page(page, &wbc)) {
155 unlock_page(page);
156 break;
157 }
Chao Yucfb271d2013-12-05 17:15:22 +0800158 nwritten++;
159 if (unlikely(nwritten >= nr_to_write))
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900160 break;
161 }
162 pagevec_release(&pvec);
163 cond_resched();
164 }
165
166 if (nwritten)
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900167 f2fs_submit_merged_bio(sbi, type, WRITE);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900168
169 return nwritten;
170}
171
172static 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
Jaegeuk Kim26c6b882013-10-24 17:53:29 +0900177 trace_f2fs_set_page_dirty(page, META);
178
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900179 SetPageUptodate(page);
180 if (!PageDirty(page)) {
181 __set_page_dirty_nobuffers(page);
182 inc_page_count(sbi, F2FS_DIRTY_META);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900183 return 1;
184 }
185 return 0;
186}
187
188const 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
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900194int acquire_orphan_inode(struct f2fs_sb_info *sbi)
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900195{
196 unsigned int max_orphans;
197 int err = 0;
198
199 /*
Chao Yu6947eea2013-11-29 16:37:00 +0800200 * considering 512 blocks in a segment 8 blocks are needed for cp
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900201 * and log segment summaries. Remaining blocks are used to keep
202 * orphan entries with the limitation one reserved segment
Chao Yu6947eea2013-11-29 16:37:00 +0800203 * for cp pack we can have max 1020*504 orphan entries
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900204 */
Chao Yu6947eea2013-11-29 16:37:00 +0800205 max_orphans = (sbi->blocks_per_seg - 2 - NR_CURSEG_TYPE)
206 * F2FS_ORPHANS_PER_BLOCK;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900207 mutex_lock(&sbi->orphan_inode_mutex);
Chao Yucfb271d2013-12-05 17:15:22 +0800208 if (unlikely(sbi->n_orphans >= max_orphans))
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900209 err = -ENOSPC;
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900210 else
211 sbi->n_orphans++;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900212 mutex_unlock(&sbi->orphan_inode_mutex);
213 return err;
214}
215
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900216void release_orphan_inode(struct f2fs_sb_info *sbi)
217{
218 mutex_lock(&sbi->orphan_inode_mutex);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900219 f2fs_bug_on(sbi->n_orphans == 0);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900220 sbi->n_orphans--;
221 mutex_unlock(&sbi->orphan_inode_mutex);
222}
223
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900224void 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 }
Gu Zheng7bd59382013-10-22 14:52:26 +0800239
240 new = f2fs_kmem_cache_alloc(orphan_entry_slab, GFP_ATOMIC);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900241 new->ino = ino;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900242
243 /* add new_oentry into list which is sorted by inode number */
majianpenga2617dc2013-01-29 16:19:02 +0800244 if (orphan)
245 list_add(&new->list, this->prev);
246 else
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900247 list_add_tail(&new->list, head);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900248out:
249 mutex_unlock(&sbi->orphan_inode_mutex);
250}
251
252void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
253{
Gu Zheng60ed9a02013-07-19 16:24:06 +0800254 struct list_head *head;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900255 struct orphan_inode_entry *orphan;
256
257 mutex_lock(&sbi->orphan_inode_mutex);
258 head = &sbi->orphan_inode_list;
Gu Zheng60ed9a02013-07-19 16:24:06 +0800259 list_for_each_entry(orphan, head, list) {
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900260 if (orphan->ino == ino) {
261 list_del(&orphan->list);
262 kmem_cache_free(orphan_entry_slab, orphan);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900263 f2fs_bug_on(sbi->n_orphans == 0);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900264 sbi->n_orphans--;
265 break;
266 }
267 }
268 mutex_unlock(&sbi->orphan_inode_mutex);
269}
270
271static void recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
272{
273 struct inode *inode = f2fs_iget(sbi->sb, ino);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900274 f2fs_bug_on(IS_ERR(inode));
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900275 clear_nlink(inode);
276
277 /* truncate all the data during iput */
278 iput(inode);
279}
280
Chao Yu8f99a942013-11-28 15:43:43 +0800281void recover_orphan_inodes(struct f2fs_sb_info *sbi)
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900282{
283 block_t start_blk, orphan_blkaddr, i, j;
284
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900285 if (!is_set_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG))
Chao Yu8f99a942013-11-28 15:43:43 +0800286 return;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900287
Haicheng Liaabe5132013-10-23 12:39:32 +0800288 sbi->por_doing = true;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900289 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 */
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900304 clear_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG);
Haicheng Liaabe5132013-10-23 12:39:32 +0800305 sbi->por_doing = false;
Chao Yu8f99a942013-11-28 15:43:43 +0800306 return;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900307}
308
309static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk)
310{
Gu Zheng502c6e02013-11-19 18:03:58 +0800311 struct list_head *head;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900312 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;
Gu Zheng502c6e02013-11-19 18:03:58 +0800317 struct orphan_inode_entry *orphan = NULL;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900318
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 */
Gu Zheng502c6e02013-11-19 18:03:58 +0800326 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 }
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900333
Gu Zheng36795562013-11-26 16:44:16 +0800334 orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900335
Gu Zheng36795562013-11-26 16:44:16 +0800336 if (nentries == F2FS_ORPHANS_PER_BLOCK) {
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900337 /*
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 }
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900352 }
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900353
Gu Zheng502c6e02013-11-19 18:03:58 +0800354 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);
360 }
361
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900362 mutex_unlock(&sbi->orphan_inode_mutex);
363}
364
365static 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;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900372 size_t crc_offset;
Jaegeuk Kim7e586fa2013-06-19 20:47:19 +0900373 __u32 crc = 0;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900374
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
Jaegeuk Kim7e586fa2013-06-19 20:47:19 +0900384 crc = le32_to_cpu(*((__u32 *)((unsigned char *)cp_block + crc_offset)));
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900385 if (!f2fs_crc_valid(crc, cp_block, crc_offset))
386 goto invalid_cp1;
387
Jaegeuk Kimd71b5562013-08-09 15:03:21 +0900388 pre_version = cur_cp_version(cp_block);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900389
390 /* Read the 2nd cp block in this CP pack */
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900391 cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900392 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
Jaegeuk Kim7e586fa2013-06-19 20:47:19 +0900399 crc = le32_to_cpu(*((__u32 *)((unsigned char *)cp_block + crc_offset)));
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900400 if (!f2fs_crc_valid(crc, cp_block, crc_offset))
401 goto invalid_cp2;
402
Jaegeuk Kimd71b5562013-08-09 15:03:21 +0900403 cur_version = cur_cp_version(cp_block);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900404
405 if (cur_version == pre_version) {
406 *version = cur_version;
407 f2fs_put_page(cp_page_2, 1);
408 return cp_page_1;
409 }
410invalid_cp2:
411 f2fs_put_page(cp_page_2, 1);
412invalid_cp1:
413 f2fs_put_page(cp_page_1, 1);
414 return NULL;
415}
416
417int 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 */
Jaegeuk Kimf9a4e6d2013-11-28 12:44:05 +0900437 cp_start_blk_no += ((unsigned long long)1) <<
438 le32_to_cpu(fsb->log_blocks_per_seg);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900439 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
461fail_no_cp:
462 kfree(sbi->ckpt);
463 return -EINVAL;
464}
465
Jaegeuk Kim5deb8262013-06-05 17:42:45 +0900466static int __add_dirty_inode(struct inode *inode, struct dir_inode_entry *new)
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900467{
468 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
469 struct list_head *head = &sbi->dir_inode_list;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900470 struct list_head *this;
471
Jaegeuk Kim5deb8262013-06-05 17:42:45 +0900472 list_for_each(this, head) {
473 struct dir_inode_entry *entry;
474 entry = list_entry(this, struct dir_inode_entry, list);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900475 if (unlikely(entry->inode == inode))
Jaegeuk Kim5deb8262013-06-05 17:42:45 +0900476 return -EEXIST;
477 }
478 list_add_tail(&new->list, head);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +0900479 stat_inc_dirty_dir(sbi);
Jaegeuk Kim5deb8262013-06-05 17:42:45 +0900480 return 0;
481}
482
483void 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
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900488 if (!S_ISDIR(inode->i_mode))
489 return;
Gu Zheng7bd59382013-10-22 14:52:26 +0800490
491 new = f2fs_kmem_cache_alloc(inode_entry_slab, GFP_NOFS);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900492 new->inode = inode;
493 INIT_LIST_HEAD(&new->list);
494
495 spin_lock(&sbi->dir_inode_lock);
Jaegeuk Kim5deb8262013-06-05 17:42:45 +0900496 if (__add_dirty_inode(inode, new))
497 kmem_cache_free(inode_entry_slab, new);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900498
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900499 inc_page_count(sbi, F2FS_DIRTY_DENTS);
500 inode_inc_dirty_dents(inode);
501 SetPagePrivate(page);
Jaegeuk Kim5deb8262013-06-05 17:42:45 +0900502 spin_unlock(&sbi->dir_inode_lock);
503}
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900504
Jaegeuk Kim5deb8262013-06-05 17:42:45 +0900505void add_dirty_dir_inode(struct inode *inode)
506{
507 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Gu Zheng7bd59382013-10-22 14:52:26 +0800508 struct dir_inode_entry *new =
509 f2fs_kmem_cache_alloc(inode_entry_slab, GFP_NOFS);
510
Jaegeuk Kim5deb8262013-06-05 17:42:45 +0900511 new->inode = inode;
512 INIT_LIST_HEAD(&new->list);
513
514 spin_lock(&sbi->dir_inode_lock);
515 if (__add_dirty_inode(inode, new))
516 kmem_cache_free(inode_entry_slab, new);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900517 spin_unlock(&sbi->dir_inode_lock);
518}
519
520void remove_dirty_dir_inode(struct inode *inode)
521{
522 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Gu Zhengce3b7d82013-11-19 18:03:47 +0800523
524 struct list_head *this, *head;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900525
526 if (!S_ISDIR(inode->i_mode))
527 return;
528
529 spin_lock(&sbi->dir_inode_lock);
Jaegeuk Kim3b10b1f2013-05-27 10:32:01 +0900530 if (atomic_read(&F2FS_I(inode)->dirty_dents)) {
531 spin_unlock(&sbi->dir_inode_lock);
532 return;
533 }
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900534
Gu Zhengce3b7d82013-11-19 18:03:47 +0800535 head = &sbi->dir_inode_list;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900536 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);
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +0900542 stat_dec_dirty_dir(sbi);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900543 break;
544 }
545 }
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900546 spin_unlock(&sbi->dir_inode_lock);
Jaegeuk Kim74d0b912013-05-15 16:40:02 +0900547
548 /* Only from the recovery routine */
Jaegeuk Kimafc3eda2013-05-28 09:59:27 +0900549 if (is_inode_flag_set(F2FS_I(inode), FI_DELAY_IPUT)) {
550 clear_inode_flag(F2FS_I(inode), FI_DELAY_IPUT);
Jaegeuk Kim74d0b912013-05-15 16:40:02 +0900551 iput(inode);
Jaegeuk Kimafc3eda2013-05-28 09:59:27 +0900552 }
Jaegeuk Kim74d0b912013-05-15 16:40:02 +0900553}
554
555struct inode *check_dirty_dir_inode(struct f2fs_sb_info *sbi, nid_t ino)
556{
Gu Zhengce3b7d82013-11-19 18:03:47 +0800557
558 struct list_head *this, *head;
Jaegeuk Kim74d0b912013-05-15 16:40:02 +0900559 struct inode *inode = NULL;
560
561 spin_lock(&sbi->dir_inode_lock);
Gu Zhengce3b7d82013-11-19 18:03:47 +0800562
563 head = &sbi->dir_inode_list;
Jaegeuk Kim74d0b912013-05-15 16:40:02 +0900564 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;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900574}
575
576void sync_dirty_dir_inodes(struct f2fs_sb_info *sbi)
577{
Gu Zhengce3b7d82013-11-19 18:03:47 +0800578 struct list_head *head;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900579 struct dir_inode_entry *entry;
580 struct inode *inode;
581retry:
582 spin_lock(&sbi->dir_inode_lock);
Gu Zhengce3b7d82013-11-19 18:03:47 +0800583
584 head = &sbi->dir_inode_list;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900585 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 */
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900600 f2fs_submit_merged_bio(sbi, DATA, WRITE);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900601 }
602 goto retry;
603}
604
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900605/*
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900606 * Freeze all the FS-operations for checkpoint.
607 */
Jaegeuk Kim43727522013-02-04 15:11:17 +0900608static void block_operations(struct f2fs_sb_info *sbi)
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900609{
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900610 struct writeback_control wbc = {
611 .sync_mode = WB_SYNC_ALL,
612 .nr_to_write = LONG_MAX,
613 .for_reclaim = 0,
614 };
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900615 struct blk_plug plug;
616
617 blk_start_plug(&plug);
618
Jaegeuk Kim39936832012-11-22 16:21:29 +0900619retry_flush_dents:
Gu Zhenge4795562013-09-27 18:08:30 +0800620 f2fs_lock_all(sbi);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900621 /* write all the dirty dentry pages */
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900622 if (get_pages(sbi, F2FS_DIRTY_DENTS)) {
Gu Zhenge4795562013-09-27 18:08:30 +0800623 f2fs_unlock_all(sbi);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900624 sync_dirty_dir_inodes(sbi);
625 goto retry_flush_dents;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900626 }
627
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900628 /*
629 * POR: we should ensure that there is no dirty node pages
630 * until finishing nat/sit flush.
631 */
Jaegeuk Kim39936832012-11-22 16:21:29 +0900632retry_flush_nodes:
633 mutex_lock(&sbi->node_write);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900634
635 if (get_pages(sbi, F2FS_DIRTY_NODES)) {
Jaegeuk Kim39936832012-11-22 16:21:29 +0900636 mutex_unlock(&sbi->node_write);
637 sync_node_pages(sbi, 0, &wbc);
638 goto retry_flush_nodes;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900639 }
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900640 blk_finish_plug(&plug);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900641}
642
643static void unblock_operations(struct f2fs_sb_info *sbi)
644{
Jaegeuk Kim39936832012-11-22 16:21:29 +0900645 mutex_unlock(&sbi->node_write);
Gu Zhenge4795562013-09-27 18:08:30 +0800646 f2fs_unlock_all(sbi);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900647}
648
Changman Leefb51b5e2013-11-07 12:48:25 +0900649static 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
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900664static 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;
Jaegeuk Kim7e586fa2013-06-19 20:47:19 +0900671 __u32 crc32 = 0;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900672 void *kaddr;
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900673 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)
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900712 set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900713 else
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900714 clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900715
716 orphan_blocks = (sbi->n_orphans + F2FS_ORPHANS_PER_BLOCK - 1)
717 / F2FS_ORPHANS_PER_BLOCK;
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900718 ckpt->cp_pack_start_sum = cpu_to_le32(1 + orphan_blocks);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900719
720 if (is_umount) {
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900721 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);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900724 } else {
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900725 clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
726 ckpt->cp_pack_total_block_count = cpu_to_le32(2 +
727 data_sum_blocks + orphan_blocks);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900728 }
729
730 if (sbi->n_orphans)
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900731 set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900732 else
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900733 clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900734
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));
Jaegeuk Kim7e586fa2013-06-19 20:47:19 +0900740 *((__le32 *)((unsigned char *)ckpt +
741 le32_to_cpu(ckpt->checksum_offset)))
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900742 = 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 */
Changman Leefb51b5e2013-11-07 12:48:25 +0900773 wait_on_all_pages_writeback(sbi);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900774
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 */
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900783 sync_meta_pages(sbi, META_FLUSH, LONG_MAX);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900784
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900785 if (unlikely(!is_set_ckpt_flags(ckpt, CP_ERROR_FLAG))) {
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900786 clear_prefree_segments(sbi);
787 F2FS_RESET_SB_DIRT(sbi);
788 }
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900789}
790
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900791/*
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900792 * We guarantee that this checkpoint procedure should not fail.
793 */
Jaegeuk Kim43727522013-02-04 15:11:17 +0900794void write_checkpoint(struct f2fs_sb_info *sbi, bool is_umount)
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900795{
796 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
797 unsigned long long ckpt_ver;
798
Namjae Jeon2af4bd62013-04-23 18:26:54 +0900799 trace_f2fs_write_checkpoint(sbi->sb, is_umount, "start block_ops");
800
Jaegeuk Kim43727522013-02-04 15:11:17 +0900801 mutex_lock(&sbi->cp_mutex);
802 block_operations(sbi);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900803
Namjae Jeon2af4bd62013-04-23 18:26:54 +0900804 trace_f2fs_write_checkpoint(sbi->sb, is_umount, "finish block_ops");
805
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900806 f2fs_submit_merged_bio(sbi, DATA, WRITE);
807 f2fs_submit_merged_bio(sbi, NODE, WRITE);
808 f2fs_submit_merged_bio(sbi, META, WRITE);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900809
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 */
Jaegeuk Kimd71b5562013-08-09 15:03:21 +0900815 ckpt_ver = cur_cp_version(ckpt);
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900816 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
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900822 /* 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);
Namjae Jeon2af4bd62013-04-23 18:26:54 +0900827
828 trace_f2fs_write_checkpoint(sbi->sb, is_umount, "finish checkpoint");
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900829}
830
831void 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
Namjae Jeon6e6093a2013-01-17 00:08:30 +0900838int __init create_checkpoint_caches(void)
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900839{
840 orphan_entry_slab = f2fs_kmem_cache_create("f2fs_orphan_entry",
841 sizeof(struct orphan_inode_entry), NULL);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900842 if (!orphan_entry_slab)
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900843 return -ENOMEM;
844 inode_entry_slab = f2fs_kmem_cache_create("f2fs_dirty_dir_entry",
845 sizeof(struct dir_inode_entry), NULL);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900846 if (!inode_entry_slab) {
Jaegeuk Kim127e6702012-11-02 17:08:18 +0900847 kmem_cache_destroy(orphan_entry_slab);
848 return -ENOMEM;
849 }
850 return 0;
851}
852
853void destroy_checkpoint_caches(void)
854{
855 kmem_cache_destroy(orphan_entry_slab);
856 kmem_cache_destroy(inode_entry_slab);
857}