Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 1 | /* |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 2 | * fs/f2fs/recovery.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/f2fs_fs.h> |
| 13 | #include "f2fs.h" |
| 14 | #include "node.h" |
| 15 | #include "segment.h" |
| 16 | |
| 17 | static struct kmem_cache *fsync_entry_slab; |
| 18 | |
| 19 | bool space_for_roll_forward(struct f2fs_sb_info *sbi) |
| 20 | { |
| 21 | if (sbi->last_valid_block_count + sbi->alloc_valid_block_count |
| 22 | > sbi->user_block_count) |
| 23 | return false; |
| 24 | return true; |
| 25 | } |
| 26 | |
| 27 | static struct fsync_inode_entry *get_fsync_inode(struct list_head *head, |
| 28 | nid_t ino) |
| 29 | { |
| 30 | struct list_head *this; |
| 31 | struct fsync_inode_entry *entry; |
| 32 | |
| 33 | list_for_each(this, head) { |
| 34 | entry = list_entry(this, struct fsync_inode_entry, list); |
| 35 | if (entry->inode->i_ino == ino) |
| 36 | return entry; |
| 37 | } |
| 38 | return NULL; |
| 39 | } |
| 40 | |
| 41 | static int recover_dentry(struct page *ipage, struct inode *inode) |
| 42 | { |
Jaegeuk Kim | 9a55ed6 | 2013-05-20 10:23:40 +0900 | [diff] [blame] | 43 | void *kaddr = page_address(ipage); |
| 44 | struct f2fs_node *raw_node = (struct f2fs_node *)kaddr; |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 45 | struct f2fs_inode *raw_inode = &(raw_node->i); |
Jaegeuk Kim | 74d0b91 | 2013-05-15 16:40:02 +0900 | [diff] [blame] | 46 | nid_t pino = le32_to_cpu(raw_inode->i_pino); |
Jaegeuk Kim | 6b8213d | 2013-05-28 09:19:22 +0900 | [diff] [blame] | 47 | struct f2fs_dir_entry *de; |
Al Viro | b7f7a5e | 2013-01-25 16:15:43 -0500 | [diff] [blame] | 48 | struct qstr name; |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 49 | struct page *page; |
Jaegeuk Kim | 6b8213d | 2013-05-28 09:19:22 +0900 | [diff] [blame] | 50 | struct inode *dir, *einode; |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 51 | int err = 0; |
| 52 | |
Jaegeuk Kim | 74d0b91 | 2013-05-15 16:40:02 +0900 | [diff] [blame] | 53 | dir = check_dirty_dir_inode(F2FS_SB(inode->i_sb), pino); |
| 54 | if (!dir) { |
| 55 | dir = f2fs_iget(inode->i_sb, pino); |
| 56 | if (IS_ERR(dir)) { |
| 57 | err = PTR_ERR(dir); |
| 58 | goto out; |
| 59 | } |
| 60 | set_inode_flag(F2FS_I(dir), FI_DELAY_IPUT); |
Jaegeuk Kim | 5deb826 | 2013-06-05 17:42:45 +0900 | [diff] [blame] | 61 | add_dirty_dir_inode(dir); |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 62 | } |
| 63 | |
Al Viro | b7f7a5e | 2013-01-25 16:15:43 -0500 | [diff] [blame] | 64 | name.len = le32_to_cpu(raw_inode->i_namelen); |
| 65 | name.name = raw_inode->i_name; |
Jaegeuk Kim | 6b8213d | 2013-05-28 09:19:22 +0900 | [diff] [blame] | 66 | retry: |
| 67 | de = f2fs_find_entry(dir, &name, &page); |
| 68 | if (de && inode->i_ino == le32_to_cpu(de->ino)) { |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 69 | kunmap(page); |
| 70 | f2fs_put_page(page, 0); |
Jaegeuk Kim | 6b8213d | 2013-05-28 09:19:22 +0900 | [diff] [blame] | 71 | goto out; |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 72 | } |
Jaegeuk Kim | 6b8213d | 2013-05-28 09:19:22 +0900 | [diff] [blame] | 73 | if (de) { |
| 74 | einode = f2fs_iget(inode->i_sb, le32_to_cpu(de->ino)); |
| 75 | if (IS_ERR(einode)) { |
| 76 | WARN_ON(1); |
| 77 | if (PTR_ERR(einode) == -ENOENT) |
| 78 | err = -EEXIST; |
| 79 | goto out; |
| 80 | } |
| 81 | f2fs_delete_entry(de, page, einode); |
| 82 | iput(einode); |
| 83 | goto retry; |
| 84 | } |
| 85 | err = __f2fs_add_link(dir, &name, inode); |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 86 | out: |
Jaegeuk Kim | f356fe0 | 2013-05-16 15:04:49 +0900 | [diff] [blame] | 87 | f2fs_msg(inode->i_sb, KERN_NOTICE, "recover_inode and its dentry: " |
| 88 | "ino = %x, name = %s, dir = %lx, err = %d", |
Dan Carpenter | f28c06f | 2013-05-23 13:02:13 +0300 | [diff] [blame] | 89 | ino_of_node(ipage), raw_inode->i_name, |
| 90 | IS_ERR(dir) ? 0 : dir->i_ino, err); |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 91 | return err; |
| 92 | } |
| 93 | |
| 94 | static int recover_inode(struct inode *inode, struct page *node_page) |
| 95 | { |
| 96 | void *kaddr = page_address(node_page); |
| 97 | struct f2fs_node *raw_node = (struct f2fs_node *)kaddr; |
| 98 | struct f2fs_inode *raw_inode = &(raw_node->i); |
| 99 | |
Jaegeuk Kim | f356fe0 | 2013-05-16 15:04:49 +0900 | [diff] [blame] | 100 | if (!IS_INODE(node_page)) |
| 101 | return 0; |
| 102 | |
Jaegeuk Kim | 25ca923 | 2012-11-28 16:12:41 +0900 | [diff] [blame] | 103 | inode->i_mode = le16_to_cpu(raw_inode->i_mode); |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 104 | i_size_write(inode, le64_to_cpu(raw_inode->i_size)); |
| 105 | inode->i_atime.tv_sec = le64_to_cpu(raw_inode->i_mtime); |
| 106 | inode->i_ctime.tv_sec = le64_to_cpu(raw_inode->i_ctime); |
| 107 | inode->i_mtime.tv_sec = le64_to_cpu(raw_inode->i_mtime); |
| 108 | inode->i_atime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec); |
| 109 | inode->i_ctime.tv_nsec = le32_to_cpu(raw_inode->i_ctime_nsec); |
| 110 | inode->i_mtime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec); |
| 111 | |
Jaegeuk Kim | f356fe0 | 2013-05-16 15:04:49 +0900 | [diff] [blame] | 112 | if (is_dent_dnode(node_page)) |
| 113 | return recover_dentry(node_page, inode); |
| 114 | |
| 115 | f2fs_msg(inode->i_sb, KERN_NOTICE, "recover_inode: ino = %x, name = %s", |
| 116 | ino_of_node(node_page), raw_inode->i_name); |
| 117 | return 0; |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static int find_fsync_dnodes(struct f2fs_sb_info *sbi, struct list_head *head) |
| 121 | { |
| 122 | unsigned long long cp_ver = le64_to_cpu(sbi->ckpt->checkpoint_ver); |
| 123 | struct curseg_info *curseg; |
| 124 | struct page *page; |
| 125 | block_t blkaddr; |
| 126 | int err = 0; |
| 127 | |
| 128 | /* get node pages in the current segment */ |
| 129 | curseg = CURSEG_I(sbi, CURSEG_WARM_NODE); |
| 130 | blkaddr = START_BLOCK(sbi, curseg->segno) + curseg->next_blkoff; |
| 131 | |
| 132 | /* read node page */ |
| 133 | page = alloc_page(GFP_F2FS_ZERO); |
| 134 | if (IS_ERR(page)) |
| 135 | return PTR_ERR(page); |
| 136 | lock_page(page); |
| 137 | |
| 138 | while (1) { |
| 139 | struct fsync_inode_entry *entry; |
| 140 | |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 141 | err = f2fs_readpage(sbi, page, blkaddr, READ_SYNC); |
| 142 | if (err) |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 143 | goto out; |
| 144 | |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 145 | lock_page(page); |
| 146 | |
Jaegeuk Kim | 6ead114 | 2013-03-20 19:01:06 +0900 | [diff] [blame] | 147 | if (cp_ver != cpver_of_node(page)) |
Jaegeuk Kim | f356fe0 | 2013-05-16 15:04:49 +0900 | [diff] [blame] | 148 | break; |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 149 | |
| 150 | if (!is_fsync_dnode(page)) |
| 151 | goto next; |
| 152 | |
| 153 | entry = get_fsync_inode(head, ino_of_node(page)); |
| 154 | if (entry) { |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 155 | if (IS_INODE(page) && is_dent_dnode(page)) |
| 156 | set_inode_flag(F2FS_I(entry->inode), |
| 157 | FI_INC_LINK); |
| 158 | } else { |
| 159 | if (IS_INODE(page) && is_dent_dnode(page)) { |
Jaegeuk Kim | 6ead114 | 2013-03-20 19:01:06 +0900 | [diff] [blame] | 160 | err = recover_inode_page(sbi, page); |
| 161 | if (err) |
Jaegeuk Kim | f356fe0 | 2013-05-16 15:04:49 +0900 | [diff] [blame] | 162 | break; |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /* add this fsync inode to the list */ |
| 166 | entry = kmem_cache_alloc(fsync_entry_slab, GFP_NOFS); |
| 167 | if (!entry) { |
| 168 | err = -ENOMEM; |
Jaegeuk Kim | f356fe0 | 2013-05-16 15:04:49 +0900 | [diff] [blame] | 169 | break; |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 170 | } |
| 171 | |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 172 | entry->inode = f2fs_iget(sbi->sb, ino_of_node(page)); |
| 173 | if (IS_ERR(entry->inode)) { |
| 174 | err = PTR_ERR(entry->inode); |
Namjae Jeon | fd8bb65 | 2012-12-22 12:10:12 +0900 | [diff] [blame] | 175 | kmem_cache_free(fsync_entry_slab, entry); |
Jaegeuk Kim | f356fe0 | 2013-05-16 15:04:49 +0900 | [diff] [blame] | 176 | break; |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 177 | } |
Namjae Jeon | fd8bb65 | 2012-12-22 12:10:12 +0900 | [diff] [blame] | 178 | list_add_tail(&entry->list, head); |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 179 | } |
Jaegeuk Kim | addbe45 | 2013-05-15 10:49:13 +0900 | [diff] [blame] | 180 | entry->blkaddr = blkaddr; |
| 181 | |
Jaegeuk Kim | f356fe0 | 2013-05-16 15:04:49 +0900 | [diff] [blame] | 182 | err = recover_inode(entry->inode, page); |
| 183 | if (err && err != -ENOENT) |
| 184 | break; |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 185 | next: |
| 186 | /* check next segment */ |
| 187 | blkaddr = next_blkaddr_of_node(page); |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 188 | } |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 189 | unlock_page(page); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 190 | out: |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 191 | __free_pages(page, 0); |
| 192 | return err; |
| 193 | } |
| 194 | |
Gu Zheng | 5ebefc5 | 2013-06-27 09:28:54 +0800 | [diff] [blame] | 195 | static void destroy_fsync_dnodes(struct list_head *head) |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 196 | { |
Dan Carpenter | d8b79b2 | 2013-01-20 18:02:58 +0300 | [diff] [blame] | 197 | struct fsync_inode_entry *entry, *tmp; |
| 198 | |
| 199 | list_for_each_entry_safe(entry, tmp, head, list) { |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 200 | iput(entry->inode); |
| 201 | list_del(&entry->list); |
| 202 | kmem_cache_free(fsync_entry_slab, entry); |
| 203 | } |
| 204 | } |
| 205 | |
Jaegeuk Kim | 39cf72c | 2013-05-22 08:20:01 +0900 | [diff] [blame] | 206 | static int check_index_in_prev_nodes(struct f2fs_sb_info *sbi, |
Jaegeuk Kim | b292dcab | 2013-05-22 08:02:02 +0900 | [diff] [blame] | 207 | block_t blkaddr, struct dnode_of_data *dn) |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 208 | { |
| 209 | struct seg_entry *sentry; |
| 210 | unsigned int segno = GET_SEGNO(sbi, blkaddr); |
| 211 | unsigned short blkoff = GET_SEGOFF_FROM_SEG0(sbi, blkaddr) & |
| 212 | (sbi->blocks_per_seg - 1); |
| 213 | struct f2fs_summary sum; |
Jaegeuk Kim | b292dcab | 2013-05-22 08:02:02 +0900 | [diff] [blame] | 214 | nid_t ino, nid; |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 215 | void *kaddr; |
| 216 | struct inode *inode; |
| 217 | struct page *node_page; |
| 218 | block_t bidx; |
| 219 | int i; |
| 220 | |
| 221 | sentry = get_seg_entry(sbi, segno); |
| 222 | if (!f2fs_test_bit(blkoff, sentry->cur_valid_map)) |
Jaegeuk Kim | 39cf72c | 2013-05-22 08:20:01 +0900 | [diff] [blame] | 223 | return 0; |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 224 | |
| 225 | /* Get the previous summary */ |
| 226 | for (i = CURSEG_WARM_DATA; i <= CURSEG_COLD_DATA; i++) { |
| 227 | struct curseg_info *curseg = CURSEG_I(sbi, i); |
| 228 | if (curseg->segno == segno) { |
| 229 | sum = curseg->sum_blk->entries[blkoff]; |
| 230 | break; |
| 231 | } |
| 232 | } |
| 233 | if (i > CURSEG_COLD_DATA) { |
| 234 | struct page *sum_page = get_sum_page(sbi, segno); |
| 235 | struct f2fs_summary_block *sum_node; |
| 236 | kaddr = page_address(sum_page); |
| 237 | sum_node = (struct f2fs_summary_block *)kaddr; |
| 238 | sum = sum_node->entries[blkoff]; |
| 239 | f2fs_put_page(sum_page, 1); |
| 240 | } |
| 241 | |
Jaegeuk Kim | b292dcab | 2013-05-22 08:02:02 +0900 | [diff] [blame] | 242 | /* Use the locked dnode page and inode */ |
| 243 | nid = le32_to_cpu(sum.nid); |
| 244 | if (dn->inode->i_ino == nid) { |
| 245 | struct dnode_of_data tdn = *dn; |
| 246 | tdn.nid = nid; |
| 247 | tdn.node_page = dn->inode_page; |
Jaegeuk Kim | 060dd67 | 2013-06-24 07:47:23 +0900 | [diff] [blame] | 248 | tdn.ofs_in_node = le16_to_cpu(sum.ofs_in_node); |
Jaegeuk Kim | b292dcab | 2013-05-22 08:02:02 +0900 | [diff] [blame] | 249 | truncate_data_blocks_range(&tdn, 1); |
Jaegeuk Kim | 39cf72c | 2013-05-22 08:20:01 +0900 | [diff] [blame] | 250 | return 0; |
Jaegeuk Kim | b292dcab | 2013-05-22 08:02:02 +0900 | [diff] [blame] | 251 | } else if (dn->nid == nid) { |
| 252 | struct dnode_of_data tdn = *dn; |
Jaegeuk Kim | 060dd67 | 2013-06-24 07:47:23 +0900 | [diff] [blame] | 253 | tdn.ofs_in_node = le16_to_cpu(sum.ofs_in_node); |
Jaegeuk Kim | b292dcab | 2013-05-22 08:02:02 +0900 | [diff] [blame] | 254 | truncate_data_blocks_range(&tdn, 1); |
Jaegeuk Kim | 39cf72c | 2013-05-22 08:20:01 +0900 | [diff] [blame] | 255 | return 0; |
Jaegeuk Kim | b292dcab | 2013-05-22 08:02:02 +0900 | [diff] [blame] | 256 | } |
| 257 | |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 258 | /* Get the node page */ |
Jaegeuk Kim | b292dcab | 2013-05-22 08:02:02 +0900 | [diff] [blame] | 259 | node_page = get_node_page(sbi, nid); |
Jaegeuk Kim | 39cf72c | 2013-05-22 08:20:01 +0900 | [diff] [blame] | 260 | if (IS_ERR(node_page)) |
| 261 | return PTR_ERR(node_page); |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 262 | bidx = start_bidx_of_node(ofs_of_node(node_page)) + |
Jaegeuk Kim | b292dcab | 2013-05-22 08:02:02 +0900 | [diff] [blame] | 263 | le16_to_cpu(sum.ofs_in_node); |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 264 | ino = ino_of_node(node_page); |
| 265 | f2fs_put_page(node_page, 1); |
| 266 | |
| 267 | /* Deallocate previous index in the node page */ |
Jaegeuk Kim | d4686d56 | 2013-01-31 15:36:04 +0900 | [diff] [blame] | 268 | inode = f2fs_iget(sbi->sb, ino); |
Namjae Jeon | 06025f4 | 2012-12-22 12:09:43 +0900 | [diff] [blame] | 269 | if (IS_ERR(inode)) |
Jaegeuk Kim | 39cf72c | 2013-05-22 08:20:01 +0900 | [diff] [blame] | 270 | return PTR_ERR(inode); |
Namjae Jeon | 06025f4 | 2012-12-22 12:09:43 +0900 | [diff] [blame] | 271 | |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 272 | truncate_hole(inode, bidx, bidx + 1); |
| 273 | iput(inode); |
Jaegeuk Kim | 39cf72c | 2013-05-22 08:20:01 +0900 | [diff] [blame] | 274 | return 0; |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 275 | } |
| 276 | |
Jaegeuk Kim | 6ead114 | 2013-03-20 19:01:06 +0900 | [diff] [blame] | 277 | static int do_recover_data(struct f2fs_sb_info *sbi, struct inode *inode, |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 278 | struct page *page, block_t blkaddr) |
| 279 | { |
| 280 | unsigned int start, end; |
| 281 | struct dnode_of_data dn; |
| 282 | struct f2fs_summary sum; |
| 283 | struct node_info ni; |
Jaegeuk Kim | f356fe0 | 2013-05-16 15:04:49 +0900 | [diff] [blame] | 284 | int err = 0, recovered = 0; |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 285 | int ilock; |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 286 | |
| 287 | start = start_bidx_of_node(ofs_of_node(page)); |
| 288 | if (IS_INODE(page)) |
| 289 | end = start + ADDRS_PER_INODE; |
| 290 | else |
| 291 | end = start + ADDRS_PER_BLOCK; |
| 292 | |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 293 | ilock = mutex_lock_op(sbi); |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 294 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 295 | |
Jaegeuk Kim | 6ead114 | 2013-03-20 19:01:06 +0900 | [diff] [blame] | 296 | err = get_dnode_of_data(&dn, start, ALLOC_NODE); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 297 | if (err) { |
| 298 | mutex_unlock_op(sbi, ilock); |
Jaegeuk Kim | 6ead114 | 2013-03-20 19:01:06 +0900 | [diff] [blame] | 299 | return err; |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 300 | } |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 301 | |
| 302 | wait_on_page_writeback(dn.node_page); |
| 303 | |
| 304 | get_node_info(sbi, dn.nid, &ni); |
| 305 | BUG_ON(ni.ino != ino_of_node(page)); |
| 306 | BUG_ON(ofs_of_node(dn.node_page) != ofs_of_node(page)); |
| 307 | |
| 308 | for (; start < end; start++) { |
| 309 | block_t src, dest; |
| 310 | |
| 311 | src = datablock_addr(dn.node_page, dn.ofs_in_node); |
| 312 | dest = datablock_addr(page, dn.ofs_in_node); |
| 313 | |
| 314 | if (src != dest && dest != NEW_ADDR && dest != NULL_ADDR) { |
| 315 | if (src == NULL_ADDR) { |
| 316 | int err = reserve_new_block(&dn); |
| 317 | /* We should not get -ENOSPC */ |
| 318 | BUG_ON(err); |
| 319 | } |
| 320 | |
| 321 | /* Check the previous node page having this index */ |
Jaegeuk Kim | 39cf72c | 2013-05-22 08:20:01 +0900 | [diff] [blame] | 322 | err = check_index_in_prev_nodes(sbi, dest, &dn); |
| 323 | if (err) |
| 324 | goto err; |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 325 | |
| 326 | set_summary(&sum, dn.nid, dn.ofs_in_node, ni.version); |
| 327 | |
| 328 | /* write dummy data page */ |
| 329 | recover_data_page(sbi, NULL, &sum, src, dest); |
| 330 | update_extent_cache(dest, &dn); |
Jaegeuk Kim | f356fe0 | 2013-05-16 15:04:49 +0900 | [diff] [blame] | 331 | recovered++; |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 332 | } |
| 333 | dn.ofs_in_node++; |
| 334 | } |
| 335 | |
| 336 | /* write node page in place */ |
| 337 | set_summary(&sum, dn.nid, 0, 0); |
| 338 | if (IS_INODE(dn.node_page)) |
| 339 | sync_inode_page(&dn); |
| 340 | |
| 341 | copy_node_footer(dn.node_page, page); |
| 342 | fill_node_footer(dn.node_page, dn.nid, ni.ino, |
| 343 | ofs_of_node(page), false); |
| 344 | set_page_dirty(dn.node_page); |
| 345 | |
| 346 | recover_node_page(sbi, dn.node_page, &sum, &ni, blkaddr); |
Jaegeuk Kim | 39cf72c | 2013-05-22 08:20:01 +0900 | [diff] [blame] | 347 | err: |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 348 | f2fs_put_dnode(&dn); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 349 | mutex_unlock_op(sbi, ilock); |
Jaegeuk Kim | f356fe0 | 2013-05-16 15:04:49 +0900 | [diff] [blame] | 350 | |
| 351 | f2fs_msg(sbi->sb, KERN_NOTICE, "recover_data: ino = %lx, " |
Jaegeuk Kim | 39cf72c | 2013-05-22 08:20:01 +0900 | [diff] [blame] | 352 | "recovered_data = %d blocks, err = %d", |
| 353 | inode->i_ino, recovered, err); |
| 354 | return err; |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 355 | } |
| 356 | |
Jaegeuk Kim | 6ead114 | 2013-03-20 19:01:06 +0900 | [diff] [blame] | 357 | static int recover_data(struct f2fs_sb_info *sbi, |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 358 | struct list_head *head, int type) |
| 359 | { |
| 360 | unsigned long long cp_ver = le64_to_cpu(sbi->ckpt->checkpoint_ver); |
| 361 | struct curseg_info *curseg; |
| 362 | struct page *page; |
Jaegeuk Kim | 6ead114 | 2013-03-20 19:01:06 +0900 | [diff] [blame] | 363 | int err = 0; |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 364 | block_t blkaddr; |
| 365 | |
| 366 | /* get node pages in the current segment */ |
| 367 | curseg = CURSEG_I(sbi, type); |
| 368 | blkaddr = NEXT_FREE_BLKADDR(sbi, curseg); |
| 369 | |
| 370 | /* read node page */ |
| 371 | page = alloc_page(GFP_NOFS | __GFP_ZERO); |
| 372 | if (IS_ERR(page)) |
Jaegeuk Kim | 6ead114 | 2013-03-20 19:01:06 +0900 | [diff] [blame] | 373 | return -ENOMEM; |
| 374 | |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 375 | lock_page(page); |
| 376 | |
| 377 | while (1) { |
| 378 | struct fsync_inode_entry *entry; |
| 379 | |
Jaegeuk Kim | 6ead114 | 2013-03-20 19:01:06 +0900 | [diff] [blame] | 380 | err = f2fs_readpage(sbi, page, blkaddr, READ_SYNC); |
| 381 | if (err) |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 382 | goto out; |
| 383 | |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 384 | lock_page(page); |
| 385 | |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 386 | if (cp_ver != cpver_of_node(page)) |
Jaegeuk Kim | 45856af | 2013-05-20 10:26:09 +0900 | [diff] [blame] | 387 | break; |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 388 | |
| 389 | entry = get_fsync_inode(head, ino_of_node(page)); |
| 390 | if (!entry) |
| 391 | goto next; |
| 392 | |
Jaegeuk Kim | 6ead114 | 2013-03-20 19:01:06 +0900 | [diff] [blame] | 393 | err = do_recover_data(sbi, entry->inode, page, blkaddr); |
| 394 | if (err) |
Jaegeuk Kim | 45856af | 2013-05-20 10:26:09 +0900 | [diff] [blame] | 395 | break; |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 396 | |
| 397 | if (entry->blkaddr == blkaddr) { |
| 398 | iput(entry->inode); |
| 399 | list_del(&entry->list); |
| 400 | kmem_cache_free(fsync_entry_slab, entry); |
| 401 | } |
| 402 | next: |
| 403 | /* check next segment */ |
| 404 | blkaddr = next_blkaddr_of_node(page); |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 405 | } |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 406 | unlock_page(page); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 407 | out: |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 408 | __free_pages(page, 0); |
| 409 | |
Jaegeuk Kim | 6ead114 | 2013-03-20 19:01:06 +0900 | [diff] [blame] | 410 | if (!err) |
| 411 | allocate_new_segments(sbi); |
| 412 | return err; |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 413 | } |
| 414 | |
Jaegeuk Kim | 6ead114 | 2013-03-20 19:01:06 +0900 | [diff] [blame] | 415 | int recover_fsync_data(struct f2fs_sb_info *sbi) |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 416 | { |
| 417 | struct list_head inode_list; |
Jaegeuk Kim | 6ead114 | 2013-03-20 19:01:06 +0900 | [diff] [blame] | 418 | int err; |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 419 | |
| 420 | fsync_entry_slab = f2fs_kmem_cache_create("f2fs_fsync_inode_entry", |
| 421 | sizeof(struct fsync_inode_entry), NULL); |
| 422 | if (unlikely(!fsync_entry_slab)) |
Jaegeuk Kim | 6ead114 | 2013-03-20 19:01:06 +0900 | [diff] [blame] | 423 | return -ENOMEM; |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 424 | |
| 425 | INIT_LIST_HEAD(&inode_list); |
| 426 | |
| 427 | /* step #1: find fsynced inode numbers */ |
Jaegeuk Kim | 8c26d7d | 2013-05-15 16:12:18 +0900 | [diff] [blame] | 428 | sbi->por_doing = 1; |
Jaegeuk Kim | 6ead114 | 2013-03-20 19:01:06 +0900 | [diff] [blame] | 429 | err = find_fsync_dnodes(sbi, &inode_list); |
| 430 | if (err) |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 431 | goto out; |
| 432 | |
| 433 | if (list_empty(&inode_list)) |
| 434 | goto out; |
| 435 | |
| 436 | /* step #2: recover data */ |
Jaegeuk Kim | 6ead114 | 2013-03-20 19:01:06 +0900 | [diff] [blame] | 437 | err = recover_data(sbi, &inode_list, CURSEG_WARM_NODE); |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 438 | BUG_ON(!list_empty(&inode_list)); |
| 439 | out: |
Gu Zheng | 5ebefc5 | 2013-06-27 09:28:54 +0800 | [diff] [blame] | 440 | destroy_fsync_dnodes(&inode_list); |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 441 | kmem_cache_destroy(fsync_entry_slab); |
Jaegeuk Kim | 8c26d7d | 2013-05-15 16:12:18 +0900 | [diff] [blame] | 442 | sbi->por_doing = 0; |
Jaegeuk Kim | 2c2c149 | 2013-05-20 14:48:49 +0900 | [diff] [blame] | 443 | if (!err) |
| 444 | write_checkpoint(sbi, false); |
Jaegeuk Kim | 6ead114 | 2013-03-20 19:01:06 +0900 | [diff] [blame] | 445 | return err; |
Jaegeuk Kim | d624c96 | 2012-11-02 17:13:32 +0900 | [diff] [blame] | 446 | } |