blob: dcd8e860aad3aefa2bcfd88ef9adcfdf243a4cdd [file] [log] [blame]
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001/*
Jaegeuk Kimd624c962012-11-02 17:13:32 +09002 * 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
17static struct kmem_cache *fsync_entry_slab;
18
19bool 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
27static 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
41static int recover_dentry(struct page *ipage, struct inode *inode)
42{
Jaegeuk Kim9a55ed62013-05-20 10:23:40 +090043 void *kaddr = page_address(ipage);
44 struct f2fs_node *raw_node = (struct f2fs_node *)kaddr;
Jaegeuk Kimd624c962012-11-02 17:13:32 +090045 struct f2fs_inode *raw_inode = &(raw_node->i);
Jaegeuk Kim74d0b912013-05-15 16:40:02 +090046 nid_t pino = le32_to_cpu(raw_inode->i_pino);
Al Virob7f7a5e2013-01-25 16:15:43 -050047 struct qstr name;
Jaegeuk Kimd624c962012-11-02 17:13:32 +090048 struct page *page;
49 struct inode *dir;
50 int err = 0;
51
Jaegeuk Kim74d0b912013-05-15 16:40:02 +090052 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 Kimd624c962012-11-02 17:13:32 +090060 }
61
Al Virob7f7a5e2013-01-25 16:15:43 -050062 name.len = le32_to_cpu(raw_inode->i_namelen);
63 name.name = raw_inode->i_name;
Jaegeuk Kimd624c962012-11-02 17:13:32 +090064
Jaegeuk Kim9a55ed62013-05-20 10:23:40 +090065 if (f2fs_find_entry(dir, &name, &page)) {
Jaegeuk Kimd624c962012-11-02 17:13:32 +090066 kunmap(page);
67 f2fs_put_page(page, 0);
68 } else {
Jaegeuk Kim90b2fc62013-02-12 07:17:20 +090069 err = __f2fs_add_link(dir, &name, inode);
Jaegeuk Kimd624c962012-11-02 17:13:32 +090070 }
Jaegeuk Kimd624c962012-11-02 17:13:32 +090071out:
Jaegeuk Kimf356fe02013-05-16 15:04:49 +090072 f2fs_msg(inode->i_sb, KERN_NOTICE, "recover_inode and its dentry: "
73 "ino = %x, name = %s, dir = %lx, err = %d",
74 ino_of_node(ipage), raw_inode->i_name, dir->i_ino, err);
Jaegeuk Kimd624c962012-11-02 17:13:32 +090075 return err;
76}
77
78static int recover_inode(struct inode *inode, struct page *node_page)
79{
80 void *kaddr = page_address(node_page);
81 struct f2fs_node *raw_node = (struct f2fs_node *)kaddr;
82 struct f2fs_inode *raw_inode = &(raw_node->i);
83
Jaegeuk Kimf356fe02013-05-16 15:04:49 +090084 if (!IS_INODE(node_page))
85 return 0;
86
Jaegeuk Kim25ca9232012-11-28 16:12:41 +090087 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
Jaegeuk Kimd624c962012-11-02 17:13:32 +090088 i_size_write(inode, le64_to_cpu(raw_inode->i_size));
89 inode->i_atime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
90 inode->i_ctime.tv_sec = le64_to_cpu(raw_inode->i_ctime);
91 inode->i_mtime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
92 inode->i_atime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec);
93 inode->i_ctime.tv_nsec = le32_to_cpu(raw_inode->i_ctime_nsec);
94 inode->i_mtime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec);
95
Jaegeuk Kimf356fe02013-05-16 15:04:49 +090096 if (is_dent_dnode(node_page))
97 return recover_dentry(node_page, inode);
98
99 f2fs_msg(inode->i_sb, KERN_NOTICE, "recover_inode: ino = %x, name = %s",
100 ino_of_node(node_page), raw_inode->i_name);
101 return 0;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900102}
103
104static int find_fsync_dnodes(struct f2fs_sb_info *sbi, struct list_head *head)
105{
106 unsigned long long cp_ver = le64_to_cpu(sbi->ckpt->checkpoint_ver);
107 struct curseg_info *curseg;
108 struct page *page;
109 block_t blkaddr;
110 int err = 0;
111
112 /* get node pages in the current segment */
113 curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
114 blkaddr = START_BLOCK(sbi, curseg->segno) + curseg->next_blkoff;
115
116 /* read node page */
117 page = alloc_page(GFP_F2FS_ZERO);
118 if (IS_ERR(page))
119 return PTR_ERR(page);
120 lock_page(page);
121
122 while (1) {
123 struct fsync_inode_entry *entry;
124
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900125 err = f2fs_readpage(sbi, page, blkaddr, READ_SYNC);
126 if (err)
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900127 goto out;
128
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900129 lock_page(page);
130
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900131 if (cp_ver != cpver_of_node(page))
Jaegeuk Kimf356fe02013-05-16 15:04:49 +0900132 break;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900133
134 if (!is_fsync_dnode(page))
135 goto next;
136
137 entry = get_fsync_inode(head, ino_of_node(page));
138 if (entry) {
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900139 if (IS_INODE(page) && is_dent_dnode(page))
140 set_inode_flag(F2FS_I(entry->inode),
141 FI_INC_LINK);
142 } else {
143 if (IS_INODE(page) && is_dent_dnode(page)) {
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900144 err = recover_inode_page(sbi, page);
145 if (err)
Jaegeuk Kimf356fe02013-05-16 15:04:49 +0900146 break;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900147 }
148
149 /* add this fsync inode to the list */
150 entry = kmem_cache_alloc(fsync_entry_slab, GFP_NOFS);
151 if (!entry) {
152 err = -ENOMEM;
Jaegeuk Kimf356fe02013-05-16 15:04:49 +0900153 break;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900154 }
155
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900156 entry->inode = f2fs_iget(sbi->sb, ino_of_node(page));
157 if (IS_ERR(entry->inode)) {
158 err = PTR_ERR(entry->inode);
Namjae Jeonfd8bb652012-12-22 12:10:12 +0900159 kmem_cache_free(fsync_entry_slab, entry);
Jaegeuk Kimf356fe02013-05-16 15:04:49 +0900160 break;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900161 }
Namjae Jeonfd8bb652012-12-22 12:10:12 +0900162 list_add_tail(&entry->list, head);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900163 }
Jaegeuk Kimaddbe452013-05-15 10:49:13 +0900164 entry->blkaddr = blkaddr;
165
Jaegeuk Kimf356fe02013-05-16 15:04:49 +0900166 err = recover_inode(entry->inode, page);
167 if (err && err != -ENOENT)
168 break;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900169next:
170 /* check next segment */
171 blkaddr = next_blkaddr_of_node(page);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900172 }
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900173 unlock_page(page);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900174out:
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900175 __free_pages(page, 0);
176 return err;
177}
178
179static void destroy_fsync_dnodes(struct f2fs_sb_info *sbi,
180 struct list_head *head)
181{
Dan Carpenterd8b79b22013-01-20 18:02:58 +0300182 struct fsync_inode_entry *entry, *tmp;
183
184 list_for_each_entry_safe(entry, tmp, head, list) {
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900185 iput(entry->inode);
186 list_del(&entry->list);
187 kmem_cache_free(fsync_entry_slab, entry);
188 }
189}
190
Jaegeuk Kim39cf72c2013-05-22 08:20:01 +0900191static int check_index_in_prev_nodes(struct f2fs_sb_info *sbi,
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900192 block_t blkaddr, struct dnode_of_data *dn)
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900193{
194 struct seg_entry *sentry;
195 unsigned int segno = GET_SEGNO(sbi, blkaddr);
196 unsigned short blkoff = GET_SEGOFF_FROM_SEG0(sbi, blkaddr) &
197 (sbi->blocks_per_seg - 1);
198 struct f2fs_summary sum;
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900199 nid_t ino, nid;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900200 void *kaddr;
201 struct inode *inode;
202 struct page *node_page;
203 block_t bidx;
204 int i;
205
206 sentry = get_seg_entry(sbi, segno);
207 if (!f2fs_test_bit(blkoff, sentry->cur_valid_map))
Jaegeuk Kim39cf72c2013-05-22 08:20:01 +0900208 return 0;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900209
210 /* Get the previous summary */
211 for (i = CURSEG_WARM_DATA; i <= CURSEG_COLD_DATA; i++) {
212 struct curseg_info *curseg = CURSEG_I(sbi, i);
213 if (curseg->segno == segno) {
214 sum = curseg->sum_blk->entries[blkoff];
215 break;
216 }
217 }
218 if (i > CURSEG_COLD_DATA) {
219 struct page *sum_page = get_sum_page(sbi, segno);
220 struct f2fs_summary_block *sum_node;
221 kaddr = page_address(sum_page);
222 sum_node = (struct f2fs_summary_block *)kaddr;
223 sum = sum_node->entries[blkoff];
224 f2fs_put_page(sum_page, 1);
225 }
226
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900227 /* Use the locked dnode page and inode */
228 nid = le32_to_cpu(sum.nid);
229 if (dn->inode->i_ino == nid) {
230 struct dnode_of_data tdn = *dn;
231 tdn.nid = nid;
232 tdn.node_page = dn->inode_page;
233 tdn.ofs_in_node = sum.ofs_in_node;
234 truncate_data_blocks_range(&tdn, 1);
Jaegeuk Kim39cf72c2013-05-22 08:20:01 +0900235 return 0;
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900236 } else if (dn->nid == nid) {
237 struct dnode_of_data tdn = *dn;
238 tdn.ofs_in_node = sum.ofs_in_node;
239 truncate_data_blocks_range(&tdn, 1);
Jaegeuk Kim39cf72c2013-05-22 08:20:01 +0900240 return 0;
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900241 }
242
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900243 /* Get the node page */
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900244 node_page = get_node_page(sbi, nid);
Jaegeuk Kim39cf72c2013-05-22 08:20:01 +0900245 if (IS_ERR(node_page))
246 return PTR_ERR(node_page);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900247 bidx = start_bidx_of_node(ofs_of_node(node_page)) +
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900248 le16_to_cpu(sum.ofs_in_node);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900249 ino = ino_of_node(node_page);
250 f2fs_put_page(node_page, 1);
251
252 /* Deallocate previous index in the node page */
Jaegeuk Kimd4686d562013-01-31 15:36:04 +0900253 inode = f2fs_iget(sbi->sb, ino);
Namjae Jeon06025f42012-12-22 12:09:43 +0900254 if (IS_ERR(inode))
Jaegeuk Kim39cf72c2013-05-22 08:20:01 +0900255 return PTR_ERR(inode);
Namjae Jeon06025f42012-12-22 12:09:43 +0900256
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900257 truncate_hole(inode, bidx, bidx + 1);
258 iput(inode);
Jaegeuk Kim39cf72c2013-05-22 08:20:01 +0900259 return 0;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900260}
261
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900262static int do_recover_data(struct f2fs_sb_info *sbi, struct inode *inode,
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900263 struct page *page, block_t blkaddr)
264{
265 unsigned int start, end;
266 struct dnode_of_data dn;
267 struct f2fs_summary sum;
268 struct node_info ni;
Jaegeuk Kimf356fe02013-05-16 15:04:49 +0900269 int err = 0, recovered = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900270 int ilock;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900271
272 start = start_bidx_of_node(ofs_of_node(page));
273 if (IS_INODE(page))
274 end = start + ADDRS_PER_INODE;
275 else
276 end = start + ADDRS_PER_BLOCK;
277
Jaegeuk Kim39936832012-11-22 16:21:29 +0900278 ilock = mutex_lock_op(sbi);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900279 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900280
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900281 err = get_dnode_of_data(&dn, start, ALLOC_NODE);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900282 if (err) {
283 mutex_unlock_op(sbi, ilock);
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900284 return err;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900285 }
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900286
287 wait_on_page_writeback(dn.node_page);
288
289 get_node_info(sbi, dn.nid, &ni);
290 BUG_ON(ni.ino != ino_of_node(page));
291 BUG_ON(ofs_of_node(dn.node_page) != ofs_of_node(page));
292
293 for (; start < end; start++) {
294 block_t src, dest;
295
296 src = datablock_addr(dn.node_page, dn.ofs_in_node);
297 dest = datablock_addr(page, dn.ofs_in_node);
298
299 if (src != dest && dest != NEW_ADDR && dest != NULL_ADDR) {
300 if (src == NULL_ADDR) {
301 int err = reserve_new_block(&dn);
302 /* We should not get -ENOSPC */
303 BUG_ON(err);
304 }
305
306 /* Check the previous node page having this index */
Jaegeuk Kim39cf72c2013-05-22 08:20:01 +0900307 err = check_index_in_prev_nodes(sbi, dest, &dn);
308 if (err)
309 goto err;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900310
311 set_summary(&sum, dn.nid, dn.ofs_in_node, ni.version);
312
313 /* write dummy data page */
314 recover_data_page(sbi, NULL, &sum, src, dest);
315 update_extent_cache(dest, &dn);
Jaegeuk Kimf356fe02013-05-16 15:04:49 +0900316 recovered++;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900317 }
318 dn.ofs_in_node++;
319 }
320
321 /* write node page in place */
322 set_summary(&sum, dn.nid, 0, 0);
323 if (IS_INODE(dn.node_page))
324 sync_inode_page(&dn);
325
326 copy_node_footer(dn.node_page, page);
327 fill_node_footer(dn.node_page, dn.nid, ni.ino,
328 ofs_of_node(page), false);
329 set_page_dirty(dn.node_page);
330
331 recover_node_page(sbi, dn.node_page, &sum, &ni, blkaddr);
Jaegeuk Kim39cf72c2013-05-22 08:20:01 +0900332err:
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900333 f2fs_put_dnode(&dn);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900334 mutex_unlock_op(sbi, ilock);
Jaegeuk Kimf356fe02013-05-16 15:04:49 +0900335
336 f2fs_msg(sbi->sb, KERN_NOTICE, "recover_data: ino = %lx, "
Jaegeuk Kim39cf72c2013-05-22 08:20:01 +0900337 "recovered_data = %d blocks, err = %d",
338 inode->i_ino, recovered, err);
339 return err;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900340}
341
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900342static int recover_data(struct f2fs_sb_info *sbi,
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900343 struct list_head *head, int type)
344{
345 unsigned long long cp_ver = le64_to_cpu(sbi->ckpt->checkpoint_ver);
346 struct curseg_info *curseg;
347 struct page *page;
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900348 int err = 0;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900349 block_t blkaddr;
350
351 /* get node pages in the current segment */
352 curseg = CURSEG_I(sbi, type);
353 blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
354
355 /* read node page */
356 page = alloc_page(GFP_NOFS | __GFP_ZERO);
357 if (IS_ERR(page))
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900358 return -ENOMEM;
359
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900360 lock_page(page);
361
362 while (1) {
363 struct fsync_inode_entry *entry;
364
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900365 err = f2fs_readpage(sbi, page, blkaddr, READ_SYNC);
366 if (err)
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900367 goto out;
368
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900369 lock_page(page);
370
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900371 if (cp_ver != cpver_of_node(page))
Jaegeuk Kim45856af2013-05-20 10:26:09 +0900372 break;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900373
374 entry = get_fsync_inode(head, ino_of_node(page));
375 if (!entry)
376 goto next;
377
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900378 err = do_recover_data(sbi, entry->inode, page, blkaddr);
379 if (err)
Jaegeuk Kim45856af2013-05-20 10:26:09 +0900380 break;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900381
382 if (entry->blkaddr == blkaddr) {
383 iput(entry->inode);
384 list_del(&entry->list);
385 kmem_cache_free(fsync_entry_slab, entry);
386 }
387next:
388 /* check next segment */
389 blkaddr = next_blkaddr_of_node(page);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900390 }
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900391 unlock_page(page);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900392out:
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900393 __free_pages(page, 0);
394
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900395 if (!err)
396 allocate_new_segments(sbi);
397 return err;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900398}
399
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900400int recover_fsync_data(struct f2fs_sb_info *sbi)
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900401{
402 struct list_head inode_list;
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900403 int err;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900404
405 fsync_entry_slab = f2fs_kmem_cache_create("f2fs_fsync_inode_entry",
406 sizeof(struct fsync_inode_entry), NULL);
407 if (unlikely(!fsync_entry_slab))
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900408 return -ENOMEM;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900409
410 INIT_LIST_HEAD(&inode_list);
411
412 /* step #1: find fsynced inode numbers */
Jaegeuk Kim8c26d7d2013-05-15 16:12:18 +0900413 sbi->por_doing = 1;
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900414 err = find_fsync_dnodes(sbi, &inode_list);
415 if (err)
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900416 goto out;
417
418 if (list_empty(&inode_list))
419 goto out;
420
421 /* step #2: recover data */
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900422 err = recover_data(sbi, &inode_list, CURSEG_WARM_NODE);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900423 BUG_ON(!list_empty(&inode_list));
424out:
425 destroy_fsync_dnodes(sbi, &inode_list);
426 kmem_cache_destroy(fsync_entry_slab);
Jaegeuk Kim8c26d7d2013-05-15 16:12:18 +0900427 sbi->por_doing = 0;
Jaegeuk Kim2c2c1492013-05-20 14:48:49 +0900428 if (!err)
429 write_checkpoint(sbi, false);
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900430 return err;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900431}