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