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