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