blob: 0dd2ce1b492f9ba268c3720af6d85d77cc644c9b [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",
Dan Carpenterf28c06f2013-05-23 13:02:13 +030074 ino_of_node(ipage), raw_inode->i_name,
75 IS_ERR(dir) ? 0 : dir->i_ino, err);
Jaegeuk Kimd624c962012-11-02 17:13:32 +090076 return err;
77}
78
79static int recover_inode(struct inode *inode, struct page *node_page)
80{
81 void *kaddr = page_address(node_page);
82 struct f2fs_node *raw_node = (struct f2fs_node *)kaddr;
83 struct f2fs_inode *raw_inode = &(raw_node->i);
84
Jaegeuk Kimf356fe02013-05-16 15:04:49 +090085 if (!IS_INODE(node_page))
86 return 0;
87
Jaegeuk Kim25ca9232012-11-28 16:12:41 +090088 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
Jaegeuk Kimd624c962012-11-02 17:13:32 +090089 i_size_write(inode, le64_to_cpu(raw_inode->i_size));
90 inode->i_atime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
91 inode->i_ctime.tv_sec = le64_to_cpu(raw_inode->i_ctime);
92 inode->i_mtime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
93 inode->i_atime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec);
94 inode->i_ctime.tv_nsec = le32_to_cpu(raw_inode->i_ctime_nsec);
95 inode->i_mtime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec);
96
Jaegeuk Kimf356fe02013-05-16 15:04:49 +090097 if (is_dent_dnode(node_page))
98 return recover_dentry(node_page, inode);
99
100 f2fs_msg(inode->i_sb, KERN_NOTICE, "recover_inode: ino = %x, name = %s",
101 ino_of_node(node_page), raw_inode->i_name);
102 return 0;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900103}
104
105static int find_fsync_dnodes(struct f2fs_sb_info *sbi, struct list_head *head)
106{
107 unsigned long long cp_ver = le64_to_cpu(sbi->ckpt->checkpoint_ver);
108 struct curseg_info *curseg;
109 struct page *page;
110 block_t blkaddr;
111 int err = 0;
112
113 /* get node pages in the current segment */
114 curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
115 blkaddr = START_BLOCK(sbi, curseg->segno) + curseg->next_blkoff;
116
117 /* read node page */
118 page = alloc_page(GFP_F2FS_ZERO);
119 if (IS_ERR(page))
120 return PTR_ERR(page);
121 lock_page(page);
122
123 while (1) {
124 struct fsync_inode_entry *entry;
125
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900126 err = f2fs_readpage(sbi, page, blkaddr, READ_SYNC);
127 if (err)
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900128 goto out;
129
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900130 lock_page(page);
131
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900132 if (cp_ver != cpver_of_node(page))
Jaegeuk Kimf356fe02013-05-16 15:04:49 +0900133 break;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900134
135 if (!is_fsync_dnode(page))
136 goto next;
137
138 entry = get_fsync_inode(head, ino_of_node(page));
139 if (entry) {
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900140 if (IS_INODE(page) && is_dent_dnode(page))
141 set_inode_flag(F2FS_I(entry->inode),
142 FI_INC_LINK);
143 } else {
144 if (IS_INODE(page) && is_dent_dnode(page)) {
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900145 err = recover_inode_page(sbi, page);
146 if (err)
Jaegeuk Kimf356fe02013-05-16 15:04:49 +0900147 break;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900148 }
149
150 /* add this fsync inode to the list */
151 entry = kmem_cache_alloc(fsync_entry_slab, GFP_NOFS);
152 if (!entry) {
153 err = -ENOMEM;
Jaegeuk Kimf356fe02013-05-16 15:04:49 +0900154 break;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900155 }
156
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900157 entry->inode = f2fs_iget(sbi->sb, ino_of_node(page));
158 if (IS_ERR(entry->inode)) {
159 err = PTR_ERR(entry->inode);
Namjae Jeonfd8bb652012-12-22 12:10:12 +0900160 kmem_cache_free(fsync_entry_slab, entry);
Jaegeuk Kimf356fe02013-05-16 15:04:49 +0900161 break;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900162 }
Namjae Jeonfd8bb652012-12-22 12:10:12 +0900163 list_add_tail(&entry->list, head);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900164 }
Jaegeuk Kimaddbe452013-05-15 10:49:13 +0900165 entry->blkaddr = blkaddr;
166
Jaegeuk Kimf356fe02013-05-16 15:04:49 +0900167 err = recover_inode(entry->inode, page);
168 if (err && err != -ENOENT)
169 break;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900170next:
171 /* check next segment */
172 blkaddr = next_blkaddr_of_node(page);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900173 }
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900174 unlock_page(page);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900175out:
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900176 __free_pages(page, 0);
177 return err;
178}
179
180static void destroy_fsync_dnodes(struct f2fs_sb_info *sbi,
181 struct list_head *head)
182{
Dan Carpenterd8b79b22013-01-20 18:02:58 +0300183 struct fsync_inode_entry *entry, *tmp;
184
185 list_for_each_entry_safe(entry, tmp, head, list) {
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900186 iput(entry->inode);
187 list_del(&entry->list);
188 kmem_cache_free(fsync_entry_slab, entry);
189 }
190}
191
Jaegeuk Kim39cf72c2013-05-22 08:20:01 +0900192static int check_index_in_prev_nodes(struct f2fs_sb_info *sbi,
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900193 block_t blkaddr, struct dnode_of_data *dn)
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900194{
195 struct seg_entry *sentry;
196 unsigned int segno = GET_SEGNO(sbi, blkaddr);
197 unsigned short blkoff = GET_SEGOFF_FROM_SEG0(sbi, blkaddr) &
198 (sbi->blocks_per_seg - 1);
199 struct f2fs_summary sum;
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900200 nid_t ino, nid;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900201 void *kaddr;
202 struct inode *inode;
203 struct page *node_page;
204 block_t bidx;
205 int i;
206
207 sentry = get_seg_entry(sbi, segno);
208 if (!f2fs_test_bit(blkoff, sentry->cur_valid_map))
Jaegeuk Kim39cf72c2013-05-22 08:20:01 +0900209 return 0;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900210
211 /* Get the previous summary */
212 for (i = CURSEG_WARM_DATA; i <= CURSEG_COLD_DATA; i++) {
213 struct curseg_info *curseg = CURSEG_I(sbi, i);
214 if (curseg->segno == segno) {
215 sum = curseg->sum_blk->entries[blkoff];
216 break;
217 }
218 }
219 if (i > CURSEG_COLD_DATA) {
220 struct page *sum_page = get_sum_page(sbi, segno);
221 struct f2fs_summary_block *sum_node;
222 kaddr = page_address(sum_page);
223 sum_node = (struct f2fs_summary_block *)kaddr;
224 sum = sum_node->entries[blkoff];
225 f2fs_put_page(sum_page, 1);
226 }
227
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900228 /* Use the locked dnode page and inode */
229 nid = le32_to_cpu(sum.nid);
230 if (dn->inode->i_ino == nid) {
231 struct dnode_of_data tdn = *dn;
232 tdn.nid = nid;
233 tdn.node_page = dn->inode_page;
234 tdn.ofs_in_node = sum.ofs_in_node;
235 truncate_data_blocks_range(&tdn, 1);
Jaegeuk Kim39cf72c2013-05-22 08:20:01 +0900236 return 0;
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900237 } else if (dn->nid == nid) {
238 struct dnode_of_data tdn = *dn;
239 tdn.ofs_in_node = sum.ofs_in_node;
240 truncate_data_blocks_range(&tdn, 1);
Jaegeuk Kim39cf72c2013-05-22 08:20:01 +0900241 return 0;
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900242 }
243
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900244 /* Get the node page */
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900245 node_page = get_node_page(sbi, nid);
Jaegeuk Kim39cf72c2013-05-22 08:20:01 +0900246 if (IS_ERR(node_page))
247 return PTR_ERR(node_page);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900248 bidx = start_bidx_of_node(ofs_of_node(node_page)) +
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900249 le16_to_cpu(sum.ofs_in_node);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900250 ino = ino_of_node(node_page);
251 f2fs_put_page(node_page, 1);
252
253 /* Deallocate previous index in the node page */
Jaegeuk Kimd4686d562013-01-31 15:36:04 +0900254 inode = f2fs_iget(sbi->sb, ino);
Namjae Jeon06025f42012-12-22 12:09:43 +0900255 if (IS_ERR(inode))
Jaegeuk Kim39cf72c2013-05-22 08:20:01 +0900256 return PTR_ERR(inode);
Namjae Jeon06025f42012-12-22 12:09:43 +0900257
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900258 truncate_hole(inode, bidx, bidx + 1);
259 iput(inode);
Jaegeuk Kim39cf72c2013-05-22 08:20:01 +0900260 return 0;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900261}
262
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900263static int do_recover_data(struct f2fs_sb_info *sbi, struct inode *inode,
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900264 struct page *page, block_t blkaddr)
265{
266 unsigned int start, end;
267 struct dnode_of_data dn;
268 struct f2fs_summary sum;
269 struct node_info ni;
Jaegeuk Kimf356fe02013-05-16 15:04:49 +0900270 int err = 0, recovered = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900271 int ilock;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900272
273 start = start_bidx_of_node(ofs_of_node(page));
274 if (IS_INODE(page))
275 end = start + ADDRS_PER_INODE;
276 else
277 end = start + ADDRS_PER_BLOCK;
278
Jaegeuk Kim39936832012-11-22 16:21:29 +0900279 ilock = mutex_lock_op(sbi);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900280 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900281
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900282 err = get_dnode_of_data(&dn, start, ALLOC_NODE);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900283 if (err) {
284 mutex_unlock_op(sbi, ilock);
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900285 return err;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900286 }
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900287
288 wait_on_page_writeback(dn.node_page);
289
290 get_node_info(sbi, dn.nid, &ni);
291 BUG_ON(ni.ino != ino_of_node(page));
292 BUG_ON(ofs_of_node(dn.node_page) != ofs_of_node(page));
293
294 for (; start < end; start++) {
295 block_t src, dest;
296
297 src = datablock_addr(dn.node_page, dn.ofs_in_node);
298 dest = datablock_addr(page, dn.ofs_in_node);
299
300 if (src != dest && dest != NEW_ADDR && dest != NULL_ADDR) {
301 if (src == NULL_ADDR) {
302 int err = reserve_new_block(&dn);
303 /* We should not get -ENOSPC */
304 BUG_ON(err);
305 }
306
307 /* Check the previous node page having this index */
Jaegeuk Kim39cf72c2013-05-22 08:20:01 +0900308 err = check_index_in_prev_nodes(sbi, dest, &dn);
309 if (err)
310 goto err;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900311
312 set_summary(&sum, dn.nid, dn.ofs_in_node, ni.version);
313
314 /* write dummy data page */
315 recover_data_page(sbi, NULL, &sum, src, dest);
316 update_extent_cache(dest, &dn);
Jaegeuk Kimf356fe02013-05-16 15:04:49 +0900317 recovered++;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900318 }
319 dn.ofs_in_node++;
320 }
321
322 /* write node page in place */
323 set_summary(&sum, dn.nid, 0, 0);
324 if (IS_INODE(dn.node_page))
325 sync_inode_page(&dn);
326
327 copy_node_footer(dn.node_page, page);
328 fill_node_footer(dn.node_page, dn.nid, ni.ino,
329 ofs_of_node(page), false);
330 set_page_dirty(dn.node_page);
331
332 recover_node_page(sbi, dn.node_page, &sum, &ni, blkaddr);
Jaegeuk Kim39cf72c2013-05-22 08:20:01 +0900333err:
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900334 f2fs_put_dnode(&dn);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900335 mutex_unlock_op(sbi, ilock);
Jaegeuk Kimf356fe02013-05-16 15:04:49 +0900336
337 f2fs_msg(sbi->sb, KERN_NOTICE, "recover_data: ino = %lx, "
Jaegeuk Kim39cf72c2013-05-22 08:20:01 +0900338 "recovered_data = %d blocks, err = %d",
339 inode->i_ino, recovered, err);
340 return err;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900341}
342
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900343static int recover_data(struct f2fs_sb_info *sbi,
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900344 struct list_head *head, int type)
345{
346 unsigned long long cp_ver = le64_to_cpu(sbi->ckpt->checkpoint_ver);
347 struct curseg_info *curseg;
348 struct page *page;
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900349 int err = 0;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900350 block_t blkaddr;
351
352 /* get node pages in the current segment */
353 curseg = CURSEG_I(sbi, type);
354 blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
355
356 /* read node page */
357 page = alloc_page(GFP_NOFS | __GFP_ZERO);
358 if (IS_ERR(page))
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900359 return -ENOMEM;
360
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900361 lock_page(page);
362
363 while (1) {
364 struct fsync_inode_entry *entry;
365
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900366 err = f2fs_readpage(sbi, page, blkaddr, READ_SYNC);
367 if (err)
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900368 goto out;
369
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900370 lock_page(page);
371
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900372 if (cp_ver != cpver_of_node(page))
Jaegeuk Kim45856af2013-05-20 10:26:09 +0900373 break;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900374
375 entry = get_fsync_inode(head, ino_of_node(page));
376 if (!entry)
377 goto next;
378
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900379 err = do_recover_data(sbi, entry->inode, page, blkaddr);
380 if (err)
Jaegeuk Kim45856af2013-05-20 10:26:09 +0900381 break;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900382
383 if (entry->blkaddr == blkaddr) {
384 iput(entry->inode);
385 list_del(&entry->list);
386 kmem_cache_free(fsync_entry_slab, entry);
387 }
388next:
389 /* check next segment */
390 blkaddr = next_blkaddr_of_node(page);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900391 }
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900392 unlock_page(page);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900393out:
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900394 __free_pages(page, 0);
395
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900396 if (!err)
397 allocate_new_segments(sbi);
398 return err;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900399}
400
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900401int recover_fsync_data(struct f2fs_sb_info *sbi)
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900402{
403 struct list_head inode_list;
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900404 int err;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900405
406 fsync_entry_slab = f2fs_kmem_cache_create("f2fs_fsync_inode_entry",
407 sizeof(struct fsync_inode_entry), NULL);
408 if (unlikely(!fsync_entry_slab))
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900409 return -ENOMEM;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900410
411 INIT_LIST_HEAD(&inode_list);
412
413 /* step #1: find fsynced inode numbers */
Jaegeuk Kim8c26d7d2013-05-15 16:12:18 +0900414 sbi->por_doing = 1;
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900415 err = find_fsync_dnodes(sbi, &inode_list);
416 if (err)
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900417 goto out;
418
419 if (list_empty(&inode_list))
420 goto out;
421
422 /* step #2: recover data */
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900423 err = recover_data(sbi, &inode_list, CURSEG_WARM_NODE);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900424 BUG_ON(!list_empty(&inode_list));
425out:
426 destroy_fsync_dnodes(sbi, &inode_list);
427 kmem_cache_destroy(fsync_entry_slab);
Jaegeuk Kim8c26d7d2013-05-15 16:12:18 +0900428 sbi->por_doing = 0;
Jaegeuk Kim2c2c1492013-05-20 14:48:49 +0900429 if (!err)
430 write_checkpoint(sbi, false);
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900431 return err;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900432}