Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 1 | /* |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 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" |
Namjae Jeon | 2af4bd6 | 2013-04-23 18:26:54 +0900 | [diff] [blame] | 23 | #include <trace/events/f2fs.h> |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 24 | |
| 25 | static struct kmem_cache *orphan_entry_slab; |
| 26 | static struct kmem_cache *inode_entry_slab; |
| 27 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 28 | /* |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 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 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 48 | /* |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 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 | } |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 61 | if (PageUptodate(page)) |
| 62 | goto out; |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 63 | |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 64 | if (f2fs_submit_page_bio(sbi, page, index, |
| 65 | READ_SYNC | REQ_META | REQ_PRIO)) |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 66 | goto repeat; |
| 67 | |
| 68 | lock_page(page); |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 69 | if (unlikely(page->mapping != mapping)) { |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 70 | f2fs_put_page(page, 1); |
| 71 | goto repeat; |
| 72 | } |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 73 | out: |
| 74 | mark_page_accessed(page); |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 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); |
Jaegeuk Kim | 577e349 | 2013-01-24 19:56:11 +0900 | [diff] [blame] | 83 | |
| 84 | /* Should not write any meta pages, if any IO error was occurred */ |
Chao Yu | cfb271d | 2013-12-05 17:15:22 +0800 | [diff] [blame] | 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; |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 91 | |
| 92 | wait_on_page_writeback(page); |
| 93 | |
Jaegeuk Kim | 577e349 | 2013-01-24 19:56:11 +0900 | [diff] [blame] | 94 | write_meta_page(sbi, page); |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 95 | dec_page_count(sbi, F2FS_DIRTY_META); |
Jaegeuk Kim | 577e349 | 2013-01-24 19:56:11 +0900 | [diff] [blame] | 96 | unlock_page(page); |
| 97 | return 0; |
Chao Yu | cfb271d | 2013-12-05 17:15:22 +0800 | [diff] [blame] | 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; |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 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); |
Chao Yu | cfb271d | 2013-12-05 17:15:22 +0800 | [diff] [blame] | 145 | if (unlikely(nr_pages == 0)) |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 146 | break; |
| 147 | |
| 148 | for (i = 0; i < nr_pages; i++) { |
| 149 | struct page *page = pvec.pages[i]; |
| 150 | lock_page(page); |
Jaegeuk Kim | 5d56b67 | 2013-10-29 15:14:54 +0900 | [diff] [blame] | 151 | f2fs_bug_on(page->mapping != mapping); |
| 152 | f2fs_bug_on(!PageDirty(page)); |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 153 | clear_page_dirty_for_io(page); |
Jaegeuk Kim | 577e349 | 2013-01-24 19:56:11 +0900 | [diff] [blame] | 154 | if (f2fs_write_meta_page(page, &wbc)) { |
| 155 | unlock_page(page); |
| 156 | break; |
| 157 | } |
Chao Yu | cfb271d | 2013-12-05 17:15:22 +0800 | [diff] [blame] | 158 | nwritten++; |
| 159 | if (unlikely(nwritten >= nr_to_write)) |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 160 | break; |
| 161 | } |
| 162 | pagevec_release(&pvec); |
| 163 | cond_resched(); |
| 164 | } |
| 165 | |
| 166 | if (nwritten) |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame^] | 167 | f2fs_submit_merged_bio(sbi, type, WRITE); |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 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 | |
Jaegeuk Kim | 26c6b88 | 2013-10-24 17:53:29 +0900 | [diff] [blame] | 177 | trace_f2fs_set_page_dirty(page, META); |
| 178 | |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 179 | SetPageUptodate(page); |
| 180 | if (!PageDirty(page)) { |
| 181 | __set_page_dirty_nobuffers(page); |
| 182 | inc_page_count(sbi, F2FS_DIRTY_META); |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 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 | |
Jaegeuk Kim | cbd56e7 | 2013-07-30 11:36:53 +0900 | [diff] [blame] | 194 | int acquire_orphan_inode(struct f2fs_sb_info *sbi) |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 195 | { |
| 196 | unsigned int max_orphans; |
| 197 | int err = 0; |
| 198 | |
| 199 | /* |
Chao Yu | 6947eea | 2013-11-29 16:37:00 +0800 | [diff] [blame] | 200 | * considering 512 blocks in a segment 8 blocks are needed for cp |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 201 | * and log segment summaries. Remaining blocks are used to keep |
| 202 | * orphan entries with the limitation one reserved segment |
Chao Yu | 6947eea | 2013-11-29 16:37:00 +0800 | [diff] [blame] | 203 | * for cp pack we can have max 1020*504 orphan entries |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 204 | */ |
Chao Yu | 6947eea | 2013-11-29 16:37:00 +0800 | [diff] [blame] | 205 | max_orphans = (sbi->blocks_per_seg - 2 - NR_CURSEG_TYPE) |
| 206 | * F2FS_ORPHANS_PER_BLOCK; |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 207 | mutex_lock(&sbi->orphan_inode_mutex); |
Chao Yu | cfb271d | 2013-12-05 17:15:22 +0800 | [diff] [blame] | 208 | if (unlikely(sbi->n_orphans >= max_orphans)) |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 209 | err = -ENOSPC; |
Jaegeuk Kim | cbd56e7 | 2013-07-30 11:36:53 +0900 | [diff] [blame] | 210 | else |
| 211 | sbi->n_orphans++; |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 212 | mutex_unlock(&sbi->orphan_inode_mutex); |
| 213 | return err; |
| 214 | } |
| 215 | |
Jaegeuk Kim | cbd56e7 | 2013-07-30 11:36:53 +0900 | [diff] [blame] | 216 | void release_orphan_inode(struct f2fs_sb_info *sbi) |
| 217 | { |
| 218 | mutex_lock(&sbi->orphan_inode_mutex); |
Jaegeuk Kim | 5d56b67 | 2013-10-29 15:14:54 +0900 | [diff] [blame] | 219 | f2fs_bug_on(sbi->n_orphans == 0); |
Jaegeuk Kim | cbd56e7 | 2013-07-30 11:36:53 +0900 | [diff] [blame] | 220 | sbi->n_orphans--; |
| 221 | mutex_unlock(&sbi->orphan_inode_mutex); |
| 222 | } |
| 223 | |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 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 | } |
Gu Zheng | 7bd5938 | 2013-10-22 14:52:26 +0800 | [diff] [blame] | 239 | |
| 240 | new = f2fs_kmem_cache_alloc(orphan_entry_slab, GFP_ATOMIC); |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 241 | new->ino = ino; |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 242 | |
| 243 | /* add new_oentry into list which is sorted by inode number */ |
majianpeng | a2617dc | 2013-01-29 16:19:02 +0800 | [diff] [blame] | 244 | if (orphan) |
| 245 | list_add(&new->list, this->prev); |
| 246 | else |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 247 | list_add_tail(&new->list, head); |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 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 | { |
Gu Zheng | 60ed9a0 | 2013-07-19 16:24:06 +0800 | [diff] [blame] | 254 | struct list_head *head; |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 255 | struct orphan_inode_entry *orphan; |
| 256 | |
| 257 | mutex_lock(&sbi->orphan_inode_mutex); |
| 258 | head = &sbi->orphan_inode_list; |
Gu Zheng | 60ed9a0 | 2013-07-19 16:24:06 +0800 | [diff] [blame] | 259 | list_for_each_entry(orphan, head, list) { |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 260 | if (orphan->ino == ino) { |
| 261 | list_del(&orphan->list); |
| 262 | kmem_cache_free(orphan_entry_slab, orphan); |
Jaegeuk Kim | 5d56b67 | 2013-10-29 15:14:54 +0900 | [diff] [blame] | 263 | f2fs_bug_on(sbi->n_orphans == 0); |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 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); |
Jaegeuk Kim | 5d56b67 | 2013-10-29 15:14:54 +0900 | [diff] [blame] | 274 | f2fs_bug_on(IS_ERR(inode)); |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 275 | clear_nlink(inode); |
| 276 | |
| 277 | /* truncate all the data during iput */ |
| 278 | iput(inode); |
| 279 | } |
| 280 | |
Chao Yu | 8f99a94 | 2013-11-28 15:43:43 +0800 | [diff] [blame] | 281 | void recover_orphan_inodes(struct f2fs_sb_info *sbi) |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 282 | { |
| 283 | block_t start_blk, orphan_blkaddr, i, j; |
| 284 | |
Jaegeuk Kim | 25ca923 | 2012-11-28 16:12:41 +0900 | [diff] [blame] | 285 | if (!is_set_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG)) |
Chao Yu | 8f99a94 | 2013-11-28 15:43:43 +0800 | [diff] [blame] | 286 | return; |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 287 | |
Haicheng Li | aabe513 | 2013-10-23 12:39:32 +0800 | [diff] [blame] | 288 | sbi->por_doing = true; |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 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 */ |
Jaegeuk Kim | 25ca923 | 2012-11-28 16:12:41 +0900 | [diff] [blame] | 304 | clear_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG); |
Haicheng Li | aabe513 | 2013-10-23 12:39:32 +0800 | [diff] [blame] | 305 | sbi->por_doing = false; |
Chao Yu | 8f99a94 | 2013-11-28 15:43:43 +0800 | [diff] [blame] | 306 | return; |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk) |
| 310 | { |
Gu Zheng | 502c6e0 | 2013-11-19 18:03:58 +0800 | [diff] [blame] | 311 | struct list_head *head; |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 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; |
Gu Zheng | 502c6e0 | 2013-11-19 18:03:58 +0800 | [diff] [blame] | 317 | struct orphan_inode_entry *orphan = NULL; |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 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 */ |
Gu Zheng | 502c6e0 | 2013-11-19 18:03:58 +0800 | [diff] [blame] | 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 | } |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 333 | |
Gu Zheng | 3679556 | 2013-11-26 16:44:16 +0800 | [diff] [blame] | 334 | orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino); |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 335 | |
Gu Zheng | 3679556 | 2013-11-26 16:44:16 +0800 | [diff] [blame] | 336 | if (nentries == F2FS_ORPHANS_PER_BLOCK) { |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 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 | } |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 352 | } |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 353 | |
Gu Zheng | 502c6e0 | 2013-11-19 18:03:58 +0800 | [diff] [blame] | 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); |
| 360 | } |
| 361 | |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 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; |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 372 | size_t crc_offset; |
Jaegeuk Kim | 7e586fa | 2013-06-19 20:47:19 +0900 | [diff] [blame] | 373 | __u32 crc = 0; |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 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 | |
Jaegeuk Kim | 7e586fa | 2013-06-19 20:47:19 +0900 | [diff] [blame] | 384 | crc = le32_to_cpu(*((__u32 *)((unsigned char *)cp_block + crc_offset))); |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 385 | if (!f2fs_crc_valid(crc, cp_block, crc_offset)) |
| 386 | goto invalid_cp1; |
| 387 | |
Jaegeuk Kim | d71b556 | 2013-08-09 15:03:21 +0900 | [diff] [blame] | 388 | pre_version = cur_cp_version(cp_block); |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 389 | |
| 390 | /* Read the 2nd cp block in this CP pack */ |
Jaegeuk Kim | 25ca923 | 2012-11-28 16:12:41 +0900 | [diff] [blame] | 391 | cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1; |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 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 | |
Jaegeuk Kim | 7e586fa | 2013-06-19 20:47:19 +0900 | [diff] [blame] | 399 | crc = le32_to_cpu(*((__u32 *)((unsigned char *)cp_block + crc_offset))); |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 400 | if (!f2fs_crc_valid(crc, cp_block, crc_offset)) |
| 401 | goto invalid_cp2; |
| 402 | |
Jaegeuk Kim | d71b556 | 2013-08-09 15:03:21 +0900 | [diff] [blame] | 403 | cur_version = cur_cp_version(cp_block); |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 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 */ |
Jaegeuk Kim | f9a4e6d | 2013-11-28 12:44:05 +0900 | [diff] [blame] | 437 | cp_start_blk_no += ((unsigned long long)1) << |
| 438 | le32_to_cpu(fsb->log_blocks_per_seg); |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 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 | |
Jaegeuk Kim | 5deb826 | 2013-06-05 17:42:45 +0900 | [diff] [blame] | 466 | static int __add_dirty_inode(struct inode *inode, struct dir_inode_entry *new) |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 467 | { |
| 468 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
| 469 | struct list_head *head = &sbi->dir_inode_list; |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 470 | struct list_head *this; |
| 471 | |
Jaegeuk Kim | 5deb826 | 2013-06-05 17:42:45 +0900 | [diff] [blame] | 472 | list_for_each(this, head) { |
| 473 | struct dir_inode_entry *entry; |
| 474 | entry = list_entry(this, struct dir_inode_entry, list); |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 475 | if (unlikely(entry->inode == inode)) |
Jaegeuk Kim | 5deb826 | 2013-06-05 17:42:45 +0900 | [diff] [blame] | 476 | return -EEXIST; |
| 477 | } |
| 478 | list_add_tail(&new->list, head); |
Jaegeuk Kim | dcdfff6 | 2013-10-22 20:56:10 +0900 | [diff] [blame] | 479 | stat_inc_dirty_dir(sbi); |
Jaegeuk Kim | 5deb826 | 2013-06-05 17:42:45 +0900 | [diff] [blame] | 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 | |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 488 | if (!S_ISDIR(inode->i_mode)) |
| 489 | return; |
Gu Zheng | 7bd5938 | 2013-10-22 14:52:26 +0800 | [diff] [blame] | 490 | |
| 491 | new = f2fs_kmem_cache_alloc(inode_entry_slab, GFP_NOFS); |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 492 | new->inode = inode; |
| 493 | INIT_LIST_HEAD(&new->list); |
| 494 | |
| 495 | spin_lock(&sbi->dir_inode_lock); |
Jaegeuk Kim | 5deb826 | 2013-06-05 17:42:45 +0900 | [diff] [blame] | 496 | if (__add_dirty_inode(inode, new)) |
| 497 | kmem_cache_free(inode_entry_slab, new); |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 498 | |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 499 | inc_page_count(sbi, F2FS_DIRTY_DENTS); |
| 500 | inode_inc_dirty_dents(inode); |
| 501 | SetPagePrivate(page); |
Jaegeuk Kim | 5deb826 | 2013-06-05 17:42:45 +0900 | [diff] [blame] | 502 | spin_unlock(&sbi->dir_inode_lock); |
| 503 | } |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 504 | |
Jaegeuk Kim | 5deb826 | 2013-06-05 17:42:45 +0900 | [diff] [blame] | 505 | void add_dirty_dir_inode(struct inode *inode) |
| 506 | { |
| 507 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
Gu Zheng | 7bd5938 | 2013-10-22 14:52:26 +0800 | [diff] [blame] | 508 | struct dir_inode_entry *new = |
| 509 | f2fs_kmem_cache_alloc(inode_entry_slab, GFP_NOFS); |
| 510 | |
Jaegeuk Kim | 5deb826 | 2013-06-05 17:42:45 +0900 | [diff] [blame] | 511 | 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 Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 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); |
Gu Zheng | ce3b7d8 | 2013-11-19 18:03:47 +0800 | [diff] [blame] | 523 | |
| 524 | struct list_head *this, *head; |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 525 | |
| 526 | if (!S_ISDIR(inode->i_mode)) |
| 527 | return; |
| 528 | |
| 529 | spin_lock(&sbi->dir_inode_lock); |
Jaegeuk Kim | 3b10b1f | 2013-05-27 10:32:01 +0900 | [diff] [blame] | 530 | if (atomic_read(&F2FS_I(inode)->dirty_dents)) { |
| 531 | spin_unlock(&sbi->dir_inode_lock); |
| 532 | return; |
| 533 | } |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 534 | |
Gu Zheng | ce3b7d8 | 2013-11-19 18:03:47 +0800 | [diff] [blame] | 535 | head = &sbi->dir_inode_list; |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 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); |
Jaegeuk Kim | dcdfff6 | 2013-10-22 20:56:10 +0900 | [diff] [blame] | 542 | stat_dec_dirty_dir(sbi); |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 543 | break; |
| 544 | } |
| 545 | } |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 546 | spin_unlock(&sbi->dir_inode_lock); |
Jaegeuk Kim | 74d0b91 | 2013-05-15 16:40:02 +0900 | [diff] [blame] | 547 | |
| 548 | /* Only from the recovery routine */ |
Jaegeuk Kim | afc3eda | 2013-05-28 09:59:27 +0900 | [diff] [blame] | 549 | if (is_inode_flag_set(F2FS_I(inode), FI_DELAY_IPUT)) { |
| 550 | clear_inode_flag(F2FS_I(inode), FI_DELAY_IPUT); |
Jaegeuk Kim | 74d0b91 | 2013-05-15 16:40:02 +0900 | [diff] [blame] | 551 | iput(inode); |
Jaegeuk Kim | afc3eda | 2013-05-28 09:59:27 +0900 | [diff] [blame] | 552 | } |
Jaegeuk Kim | 74d0b91 | 2013-05-15 16:40:02 +0900 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | struct inode *check_dirty_dir_inode(struct f2fs_sb_info *sbi, nid_t ino) |
| 556 | { |
Gu Zheng | ce3b7d8 | 2013-11-19 18:03:47 +0800 | [diff] [blame] | 557 | |
| 558 | struct list_head *this, *head; |
Jaegeuk Kim | 74d0b91 | 2013-05-15 16:40:02 +0900 | [diff] [blame] | 559 | struct inode *inode = NULL; |
| 560 | |
| 561 | spin_lock(&sbi->dir_inode_lock); |
Gu Zheng | ce3b7d8 | 2013-11-19 18:03:47 +0800 | [diff] [blame] | 562 | |
| 563 | head = &sbi->dir_inode_list; |
Jaegeuk Kim | 74d0b91 | 2013-05-15 16:40:02 +0900 | [diff] [blame] | 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; |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | void sync_dirty_dir_inodes(struct f2fs_sb_info *sbi) |
| 577 | { |
Gu Zheng | ce3b7d8 | 2013-11-19 18:03:47 +0800 | [diff] [blame] | 578 | struct list_head *head; |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 579 | struct dir_inode_entry *entry; |
| 580 | struct inode *inode; |
| 581 | retry: |
| 582 | spin_lock(&sbi->dir_inode_lock); |
Gu Zheng | ce3b7d8 | 2013-11-19 18:03:47 +0800 | [diff] [blame] | 583 | |
| 584 | head = &sbi->dir_inode_list; |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 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 | */ |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame^] | 600 | f2fs_submit_merged_bio(sbi, DATA, WRITE); |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 601 | } |
| 602 | goto retry; |
| 603 | } |
| 604 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 605 | /* |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 606 | * Freeze all the FS-operations for checkpoint. |
| 607 | */ |
Jaegeuk Kim | 4372752 | 2013-02-04 15:11:17 +0900 | [diff] [blame] | 608 | static void block_operations(struct f2fs_sb_info *sbi) |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 609 | { |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 610 | struct writeback_control wbc = { |
| 611 | .sync_mode = WB_SYNC_ALL, |
| 612 | .nr_to_write = LONG_MAX, |
| 613 | .for_reclaim = 0, |
| 614 | }; |
Jaegeuk Kim | c718379b | 2013-04-24 13:19:56 +0900 | [diff] [blame] | 615 | struct blk_plug plug; |
| 616 | |
| 617 | blk_start_plug(&plug); |
| 618 | |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 619 | retry_flush_dents: |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 620 | f2fs_lock_all(sbi); |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 621 | /* write all the dirty dentry pages */ |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 622 | if (get_pages(sbi, F2FS_DIRTY_DENTS)) { |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 623 | f2fs_unlock_all(sbi); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 624 | sync_dirty_dir_inodes(sbi); |
| 625 | goto retry_flush_dents; |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 626 | } |
| 627 | |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 628 | /* |
| 629 | * POR: we should ensure that there is no dirty node pages |
| 630 | * until finishing nat/sit flush. |
| 631 | */ |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 632 | retry_flush_nodes: |
| 633 | mutex_lock(&sbi->node_write); |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 634 | |
| 635 | if (get_pages(sbi, F2FS_DIRTY_NODES)) { |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 636 | mutex_unlock(&sbi->node_write); |
| 637 | sync_node_pages(sbi, 0, &wbc); |
| 638 | goto retry_flush_nodes; |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 639 | } |
Jaegeuk Kim | c718379b | 2013-04-24 13:19:56 +0900 | [diff] [blame] | 640 | blk_finish_plug(&plug); |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | static void unblock_operations(struct f2fs_sb_info *sbi) |
| 644 | { |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 645 | mutex_unlock(&sbi->node_write); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 646 | f2fs_unlock_all(sbi); |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 647 | } |
| 648 | |
Changman Lee | fb51b5e | 2013-11-07 12:48:25 +0900 | [diff] [blame] | 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 | |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 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; |
Jaegeuk Kim | 7e586fa | 2013-06-19 20:47:19 +0900 | [diff] [blame] | 671 | __u32 crc32 = 0; |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 672 | void *kaddr; |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 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) |
Jaegeuk Kim | 25ca923 | 2012-11-28 16:12:41 +0900 | [diff] [blame] | 712 | set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG); |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 713 | else |
Jaegeuk Kim | 25ca923 | 2012-11-28 16:12:41 +0900 | [diff] [blame] | 714 | clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG); |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 715 | |
| 716 | orphan_blocks = (sbi->n_orphans + F2FS_ORPHANS_PER_BLOCK - 1) |
| 717 | / F2FS_ORPHANS_PER_BLOCK; |
Jaegeuk Kim | 25ca923 | 2012-11-28 16:12:41 +0900 | [diff] [blame] | 718 | ckpt->cp_pack_start_sum = cpu_to_le32(1 + orphan_blocks); |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 719 | |
| 720 | if (is_umount) { |
Jaegeuk Kim | 25ca923 | 2012-11-28 16:12:41 +0900 | [diff] [blame] | 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); |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 724 | } else { |
Jaegeuk Kim | 25ca923 | 2012-11-28 16:12:41 +0900 | [diff] [blame] | 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); |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | if (sbi->n_orphans) |
Jaegeuk Kim | 25ca923 | 2012-11-28 16:12:41 +0900 | [diff] [blame] | 731 | set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG); |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 732 | else |
Jaegeuk Kim | 25ca923 | 2012-11-28 16:12:41 +0900 | [diff] [blame] | 733 | clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG); |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 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)); |
Jaegeuk Kim | 7e586fa | 2013-06-19 20:47:19 +0900 | [diff] [blame] | 740 | *((__le32 *)((unsigned char *)ckpt + |
| 741 | le32_to_cpu(ckpt->checksum_offset))) |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 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 */ |
Changman Lee | fb51b5e | 2013-11-07 12:48:25 +0900 | [diff] [blame] | 773 | wait_on_all_pages_writeback(sbi); |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 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 */ |
Jaegeuk Kim | 577e349 | 2013-01-24 19:56:11 +0900 | [diff] [blame] | 783 | sync_meta_pages(sbi, META_FLUSH, LONG_MAX); |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 784 | |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 785 | if (unlikely(!is_set_ckpt_flags(ckpt, CP_ERROR_FLAG))) { |
Jaegeuk Kim | 577e349 | 2013-01-24 19:56:11 +0900 | [diff] [blame] | 786 | clear_prefree_segments(sbi); |
| 787 | F2FS_RESET_SB_DIRT(sbi); |
| 788 | } |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 789 | } |
| 790 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 791 | /* |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 792 | * We guarantee that this checkpoint procedure should not fail. |
| 793 | */ |
Jaegeuk Kim | 4372752 | 2013-02-04 15:11:17 +0900 | [diff] [blame] | 794 | void write_checkpoint(struct f2fs_sb_info *sbi, bool is_umount) |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 795 | { |
| 796 | struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); |
| 797 | unsigned long long ckpt_ver; |
| 798 | |
Namjae Jeon | 2af4bd6 | 2013-04-23 18:26:54 +0900 | [diff] [blame] | 799 | trace_f2fs_write_checkpoint(sbi->sb, is_umount, "start block_ops"); |
| 800 | |
Jaegeuk Kim | 4372752 | 2013-02-04 15:11:17 +0900 | [diff] [blame] | 801 | mutex_lock(&sbi->cp_mutex); |
| 802 | block_operations(sbi); |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 803 | |
Namjae Jeon | 2af4bd6 | 2013-04-23 18:26:54 +0900 | [diff] [blame] | 804 | trace_f2fs_write_checkpoint(sbi->sb, is_umount, "finish block_ops"); |
| 805 | |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame^] | 806 | f2fs_submit_merged_bio(sbi, DATA, WRITE); |
| 807 | f2fs_submit_merged_bio(sbi, NODE, WRITE); |
| 808 | f2fs_submit_merged_bio(sbi, META, WRITE); |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 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 | */ |
Jaegeuk Kim | d71b556 | 2013-08-09 15:03:21 +0900 | [diff] [blame] | 815 | ckpt_ver = cur_cp_version(ckpt); |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 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 | |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 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); |
Namjae Jeon | 2af4bd6 | 2013-04-23 18:26:54 +0900 | [diff] [blame] | 827 | |
| 828 | trace_f2fs_write_checkpoint(sbi->sb, is_umount, "finish checkpoint"); |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 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 | |
Namjae Jeon | 6e6093a | 2013-01-17 00:08:30 +0900 | [diff] [blame] | 838 | int __init create_checkpoint_caches(void) |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 839 | { |
| 840 | orphan_entry_slab = f2fs_kmem_cache_create("f2fs_orphan_entry", |
| 841 | sizeof(struct orphan_inode_entry), NULL); |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 842 | if (!orphan_entry_slab) |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 843 | return -ENOMEM; |
| 844 | inode_entry_slab = f2fs_kmem_cache_create("f2fs_dirty_dir_entry", |
| 845 | sizeof(struct dir_inode_entry), NULL); |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 846 | if (!inode_entry_slab) { |
Jaegeuk Kim | 127e670 | 2012-11-02 17:08:18 +0900 | [diff] [blame] | 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 | } |