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