blob: 96e690b6f0faafdb07671076b0828c545b5b8d47 [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 Kim58bfaf42013-12-26 16:30:41 +090043 struct f2fs_inode *raw_inode = F2FS_INODE(ipage);
Jaegeuk Kim74d0b912013-05-15 16:40:02 +090044 nid_t pino = le32_to_cpu(raw_inode->i_pino);
Jaegeuk Kim6b8213d2013-05-28 09:19:22 +090045 struct f2fs_dir_entry *de;
Al Virob7f7a5e2013-01-25 16:15:43 -050046 struct qstr name;
Jaegeuk Kimd624c962012-11-02 17:13:32 +090047 struct page *page;
Jaegeuk Kim6b8213d2013-05-28 09:19:22 +090048 struct inode *dir, *einode;
Jaegeuk Kimd624c962012-11-02 17:13:32 +090049 int err = 0;
50
Jaegeuk Kim74d0b912013-05-15 16:40:02 +090051 dir = check_dirty_dir_inode(F2FS_SB(inode->i_sb), pino);
52 if (!dir) {
53 dir = f2fs_iget(inode->i_sb, pino);
54 if (IS_ERR(dir)) {
55 err = PTR_ERR(dir);
56 goto out;
57 }
58 set_inode_flag(F2FS_I(dir), FI_DELAY_IPUT);
Jaegeuk Kim5deb8262013-06-05 17:42:45 +090059 add_dirty_dir_inode(dir);
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;
Chao Yud96b14312013-12-23 11:12:21 +080064
65 if (unlikely(name.len > F2FS_NAME_LEN)) {
66 WARN_ON(1);
67 err = -ENAMETOOLONG;
68 goto out;
69 }
Jaegeuk Kim6b8213d2013-05-28 09:19:22 +090070retry:
71 de = f2fs_find_entry(dir, &name, &page);
Russ W. Knize2e5558f2013-09-24 09:40:57 -050072 if (de && inode->i_ino == le32_to_cpu(de->ino))
73 goto out_unmap_put;
Jaegeuk Kim6b8213d2013-05-28 09:19:22 +090074 if (de) {
75 einode = f2fs_iget(inode->i_sb, le32_to_cpu(de->ino));
76 if (IS_ERR(einode)) {
77 WARN_ON(1);
78 if (PTR_ERR(einode) == -ENOENT)
79 err = -EEXIST;
Russ W. Knize2e5558f2013-09-24 09:40:57 -050080 goto out_unmap_put;
81 }
82 err = acquire_orphan_inode(F2FS_SB(inode->i_sb));
83 if (err) {
84 iput(einode);
85 goto out_unmap_put;
Jaegeuk Kim6b8213d2013-05-28 09:19:22 +090086 }
87 f2fs_delete_entry(de, page, einode);
88 iput(einode);
89 goto retry;
90 }
91 err = __f2fs_add_link(dir, &name, inode);
Russ W. Knize2e5558f2013-09-24 09:40:57 -050092 goto out;
93
94out_unmap_put:
95 kunmap(page);
96 f2fs_put_page(page, 0);
Jaegeuk Kimd624c962012-11-02 17:13:32 +090097out:
Jaegeuk Kimf356fe02013-05-16 15:04:49 +090098 f2fs_msg(inode->i_sb, KERN_NOTICE, "recover_inode and its dentry: "
99 "ino = %x, name = %s, dir = %lx, err = %d",
Dan Carpenterf28c06f2013-05-23 13:02:13 +0300100 ino_of_node(ipage), raw_inode->i_name,
101 IS_ERR(dir) ? 0 : dir->i_ino, err);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900102 return err;
103}
104
105static int recover_inode(struct inode *inode, struct page *node_page)
106{
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900107 struct f2fs_inode *raw_inode = F2FS_INODE(node_page);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900108
Jaegeuk Kimf356fe02013-05-16 15:04:49 +0900109 if (!IS_INODE(node_page))
110 return 0;
111
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900112 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900113 i_size_write(inode, le64_to_cpu(raw_inode->i_size));
114 inode->i_atime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
115 inode->i_ctime.tv_sec = le64_to_cpu(raw_inode->i_ctime);
116 inode->i_mtime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
117 inode->i_atime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec);
118 inode->i_ctime.tv_nsec = le32_to_cpu(raw_inode->i_ctime_nsec);
119 inode->i_mtime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec);
120
Jaegeuk Kimf356fe02013-05-16 15:04:49 +0900121 if (is_dent_dnode(node_page))
122 return recover_dentry(node_page, inode);
123
124 f2fs_msg(inode->i_sb, KERN_NOTICE, "recover_inode: ino = %x, name = %s",
125 ino_of_node(node_page), raw_inode->i_name);
126 return 0;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900127}
128
129static int find_fsync_dnodes(struct f2fs_sb_info *sbi, struct list_head *head)
130{
Jaegeuk Kimd71b5562013-08-09 15:03:21 +0900131 unsigned long long cp_ver = cur_cp_version(F2FS_CKPT(sbi));
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900132 struct curseg_info *curseg;
133 struct page *page;
134 block_t blkaddr;
135 int err = 0;
136
137 /* get node pages in the current segment */
138 curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
139 blkaddr = START_BLOCK(sbi, curseg->segno) + curseg->next_blkoff;
140
141 /* read node page */
142 page = alloc_page(GFP_F2FS_ZERO);
Dan Carpentere27dae42013-08-15 08:54:56 +0300143 if (!page)
144 return -ENOMEM;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900145 lock_page(page);
146
147 while (1) {
148 struct fsync_inode_entry *entry;
149
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900150 err = f2fs_submit_page_bio(sbi, page, blkaddr, READ_SYNC);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900151 if (err)
Chao Yub9987a22013-12-05 09:54:56 +0800152 return err;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900153
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900154 lock_page(page);
155
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900156 if (cp_ver != cpver_of_node(page))
Jaegeuk Kimf356fe02013-05-16 15:04:49 +0900157 break;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900158
159 if (!is_fsync_dnode(page))
160 goto next;
161
162 entry = get_fsync_inode(head, ino_of_node(page));
163 if (entry) {
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900164 if (IS_INODE(page) && is_dent_dnode(page))
165 set_inode_flag(F2FS_I(entry->inode),
166 FI_INC_LINK);
167 } else {
168 if (IS_INODE(page) && is_dent_dnode(page)) {
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900169 err = recover_inode_page(sbi, page);
170 if (err)
Jaegeuk Kimf356fe02013-05-16 15:04:49 +0900171 break;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900172 }
173
174 /* add this fsync inode to the list */
175 entry = kmem_cache_alloc(fsync_entry_slab, GFP_NOFS);
176 if (!entry) {
177 err = -ENOMEM;
Jaegeuk Kimf356fe02013-05-16 15:04:49 +0900178 break;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900179 }
180
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900181 entry->inode = f2fs_iget(sbi->sb, ino_of_node(page));
182 if (IS_ERR(entry->inode)) {
183 err = PTR_ERR(entry->inode);
Namjae Jeonfd8bb652012-12-22 12:10:12 +0900184 kmem_cache_free(fsync_entry_slab, entry);
Jaegeuk Kimf356fe02013-05-16 15:04:49 +0900185 break;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900186 }
Namjae Jeonfd8bb652012-12-22 12:10:12 +0900187 list_add_tail(&entry->list, head);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900188 }
Jaegeuk Kimaddbe452013-05-15 10:49:13 +0900189 entry->blkaddr = blkaddr;
190
Jaegeuk Kimf356fe02013-05-16 15:04:49 +0900191 err = recover_inode(entry->inode, page);
192 if (err && err != -ENOENT)
193 break;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900194next:
195 /* check next segment */
196 blkaddr = next_blkaddr_of_node(page);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900197 }
Chao Yub9987a22013-12-05 09:54:56 +0800198
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900199 unlock_page(page);
200 __free_pages(page, 0);
Chao Yub9987a22013-12-05 09:54:56 +0800201
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900202 return err;
203}
204
Gu Zheng5ebefc52013-06-27 09:28:54 +0800205static void destroy_fsync_dnodes(struct list_head *head)
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900206{
Dan Carpenterd8b79b22013-01-20 18:02:58 +0300207 struct fsync_inode_entry *entry, *tmp;
208
209 list_for_each_entry_safe(entry, tmp, head, list) {
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900210 iput(entry->inode);
211 list_del(&entry->list);
212 kmem_cache_free(fsync_entry_slab, entry);
213 }
214}
215
Jaegeuk Kim39cf72c2013-05-22 08:20:01 +0900216static int check_index_in_prev_nodes(struct f2fs_sb_info *sbi,
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900217 block_t blkaddr, struct dnode_of_data *dn)
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900218{
219 struct seg_entry *sentry;
220 unsigned int segno = GET_SEGNO(sbi, blkaddr);
221 unsigned short blkoff = GET_SEGOFF_FROM_SEG0(sbi, blkaddr) &
222 (sbi->blocks_per_seg - 1);
223 struct f2fs_summary sum;
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900224 nid_t ino, nid;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900225 void *kaddr;
226 struct inode *inode;
227 struct page *node_page;
Jaegeuk Kimde936532013-08-12 21:08:03 +0900228 unsigned int offset;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900229 block_t bidx;
230 int i;
231
232 sentry = get_seg_entry(sbi, segno);
233 if (!f2fs_test_bit(blkoff, sentry->cur_valid_map))
Jaegeuk Kim39cf72c2013-05-22 08:20:01 +0900234 return 0;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900235
236 /* Get the previous summary */
237 for (i = CURSEG_WARM_DATA; i <= CURSEG_COLD_DATA; i++) {
238 struct curseg_info *curseg = CURSEG_I(sbi, i);
239 if (curseg->segno == segno) {
240 sum = curseg->sum_blk->entries[blkoff];
241 break;
242 }
243 }
244 if (i > CURSEG_COLD_DATA) {
245 struct page *sum_page = get_sum_page(sbi, segno);
246 struct f2fs_summary_block *sum_node;
247 kaddr = page_address(sum_page);
248 sum_node = (struct f2fs_summary_block *)kaddr;
249 sum = sum_node->entries[blkoff];
250 f2fs_put_page(sum_page, 1);
251 }
252
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900253 /* Use the locked dnode page and inode */
254 nid = le32_to_cpu(sum.nid);
255 if (dn->inode->i_ino == nid) {
256 struct dnode_of_data tdn = *dn;
257 tdn.nid = nid;
258 tdn.node_page = dn->inode_page;
Jaegeuk Kim060dd672013-06-24 07:47:23 +0900259 tdn.ofs_in_node = le16_to_cpu(sum.ofs_in_node);
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900260 truncate_data_blocks_range(&tdn, 1);
Jaegeuk Kim39cf72c2013-05-22 08:20:01 +0900261 return 0;
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900262 } else if (dn->nid == nid) {
263 struct dnode_of_data tdn = *dn;
Jaegeuk Kim060dd672013-06-24 07:47:23 +0900264 tdn.ofs_in_node = le16_to_cpu(sum.ofs_in_node);
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900265 truncate_data_blocks_range(&tdn, 1);
Jaegeuk Kim39cf72c2013-05-22 08:20:01 +0900266 return 0;
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900267 }
268
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900269 /* Get the node page */
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900270 node_page = get_node_page(sbi, nid);
Jaegeuk Kim39cf72c2013-05-22 08:20:01 +0900271 if (IS_ERR(node_page))
272 return PTR_ERR(node_page);
Jaegeuk Kimde936532013-08-12 21:08:03 +0900273
274 offset = ofs_of_node(node_page);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900275 ino = ino_of_node(node_page);
276 f2fs_put_page(node_page, 1);
277
278 /* Deallocate previous index in the node page */
Jaegeuk Kimd4686d562013-01-31 15:36:04 +0900279 inode = f2fs_iget(sbi->sb, ino);
Namjae Jeon06025f42012-12-22 12:09:43 +0900280 if (IS_ERR(inode))
Jaegeuk Kim39cf72c2013-05-22 08:20:01 +0900281 return PTR_ERR(inode);
Namjae Jeon06025f42012-12-22 12:09:43 +0900282
Jaegeuk Kimde936532013-08-12 21:08:03 +0900283 bidx = start_bidx_of_node(offset, F2FS_I(inode)) +
284 le16_to_cpu(sum.ofs_in_node);
285
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900286 truncate_hole(inode, bidx, bidx + 1);
287 iput(inode);
Jaegeuk Kim39cf72c2013-05-22 08:20:01 +0900288 return 0;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900289}
290
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900291static int do_recover_data(struct f2fs_sb_info *sbi, struct inode *inode,
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900292 struct page *page, block_t blkaddr)
293{
Jaegeuk Kimde936532013-08-12 21:08:03 +0900294 struct f2fs_inode_info *fi = F2FS_I(inode);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900295 unsigned int start, end;
296 struct dnode_of_data dn;
297 struct f2fs_summary sum;
298 struct node_info ni;
Jaegeuk Kimf356fe02013-05-16 15:04:49 +0900299 int err = 0, recovered = 0;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900300
Jaegeuk Kimde936532013-08-12 21:08:03 +0900301 start = start_bidx_of_node(ofs_of_node(page), fi);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900302 if (IS_INODE(page))
Jaegeuk Kimde936532013-08-12 21:08:03 +0900303 end = start + ADDRS_PER_INODE(fi);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900304 else
305 end = start + ADDRS_PER_BLOCK;
306
Gu Zhenge4795562013-09-27 18:08:30 +0800307 f2fs_lock_op(sbi);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900308 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900309
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900310 err = get_dnode_of_data(&dn, start, ALLOC_NODE);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900311 if (err) {
Gu Zhenge4795562013-09-27 18:08:30 +0800312 f2fs_unlock_op(sbi);
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900313 return err;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900314 }
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900315
316 wait_on_page_writeback(dn.node_page);
317
318 get_node_info(sbi, dn.nid, &ni);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900319 f2fs_bug_on(ni.ino != ino_of_node(page));
320 f2fs_bug_on(ofs_of_node(dn.node_page) != ofs_of_node(page));
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900321
322 for (; start < end; start++) {
323 block_t src, dest;
324
325 src = datablock_addr(dn.node_page, dn.ofs_in_node);
326 dest = datablock_addr(page, dn.ofs_in_node);
327
328 if (src != dest && dest != NEW_ADDR && dest != NULL_ADDR) {
329 if (src == NULL_ADDR) {
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900330 err = reserve_new_block(&dn);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900331 /* We should not get -ENOSPC */
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900332 f2fs_bug_on(err);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900333 }
334
335 /* Check the previous node page having this index */
Jaegeuk Kim39cf72c2013-05-22 08:20:01 +0900336 err = check_index_in_prev_nodes(sbi, dest, &dn);
337 if (err)
338 goto err;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900339
340 set_summary(&sum, dn.nid, dn.ofs_in_node, ni.version);
341
342 /* write dummy data page */
343 recover_data_page(sbi, NULL, &sum, src, dest);
344 update_extent_cache(dest, &dn);
Jaegeuk Kimf356fe02013-05-16 15:04:49 +0900345 recovered++;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900346 }
347 dn.ofs_in_node++;
348 }
349
350 /* write node page in place */
351 set_summary(&sum, dn.nid, 0, 0);
352 if (IS_INODE(dn.node_page))
353 sync_inode_page(&dn);
354
355 copy_node_footer(dn.node_page, page);
356 fill_node_footer(dn.node_page, dn.nid, ni.ino,
357 ofs_of_node(page), false);
358 set_page_dirty(dn.node_page);
359
360 recover_node_page(sbi, dn.node_page, &sum, &ni, blkaddr);
Jaegeuk Kim39cf72c2013-05-22 08:20:01 +0900361err:
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900362 f2fs_put_dnode(&dn);
Gu Zhenge4795562013-09-27 18:08:30 +0800363 f2fs_unlock_op(sbi);
Jaegeuk Kimf356fe02013-05-16 15:04:49 +0900364
365 f2fs_msg(sbi->sb, KERN_NOTICE, "recover_data: ino = %lx, "
Jaegeuk Kim39cf72c2013-05-22 08:20:01 +0900366 "recovered_data = %d blocks, err = %d",
367 inode->i_ino, recovered, err);
368 return err;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900369}
370
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900371static int recover_data(struct f2fs_sb_info *sbi,
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900372 struct list_head *head, int type)
373{
Jaegeuk Kimd71b5562013-08-09 15:03:21 +0900374 unsigned long long cp_ver = cur_cp_version(F2FS_CKPT(sbi));
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900375 struct curseg_info *curseg;
376 struct page *page;
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900377 int err = 0;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900378 block_t blkaddr;
379
380 /* get node pages in the current segment */
381 curseg = CURSEG_I(sbi, type);
382 blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
383
384 /* read node page */
Chao Yua0acdfe2013-12-05 09:54:00 +0800385 page = alloc_page(GFP_F2FS_ZERO);
Dan Carpentere27dae42013-08-15 08:54:56 +0300386 if (!page)
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900387 return -ENOMEM;
388
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900389 lock_page(page);
390
391 while (1) {
392 struct fsync_inode_entry *entry;
393
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900394 err = f2fs_submit_page_bio(sbi, page, blkaddr, READ_SYNC);
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900395 if (err)
Chao Yub9987a22013-12-05 09:54:56 +0800396 return err;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900397
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900398 lock_page(page);
399
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900400 if (cp_ver != cpver_of_node(page))
Jaegeuk Kim45856af2013-05-20 10:26:09 +0900401 break;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900402
403 entry = get_fsync_inode(head, ino_of_node(page));
404 if (!entry)
405 goto next;
406
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900407 err = do_recover_data(sbi, entry->inode, page, blkaddr);
408 if (err)
Jaegeuk Kim45856af2013-05-20 10:26:09 +0900409 break;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900410
411 if (entry->blkaddr == blkaddr) {
412 iput(entry->inode);
413 list_del(&entry->list);
414 kmem_cache_free(fsync_entry_slab, entry);
415 }
416next:
417 /* check next segment */
418 blkaddr = next_blkaddr_of_node(page);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900419 }
Chao Yub9987a22013-12-05 09:54:56 +0800420
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900421 unlock_page(page);
422 __free_pages(page, 0);
423
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900424 if (!err)
425 allocate_new_segments(sbi);
426 return err;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900427}
428
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900429int recover_fsync_data(struct f2fs_sb_info *sbi)
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900430{
431 struct list_head inode_list;
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900432 int err;
Haicheng Liaabe5132013-10-23 12:39:32 +0800433 bool need_writecp = false;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900434
435 fsync_entry_slab = f2fs_kmem_cache_create("f2fs_fsync_inode_entry",
436 sizeof(struct fsync_inode_entry), NULL);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900437 if (!fsync_entry_slab)
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900438 return -ENOMEM;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900439
440 INIT_LIST_HEAD(&inode_list);
441
442 /* step #1: find fsynced inode numbers */
Haicheng Liaabe5132013-10-23 12:39:32 +0800443 sbi->por_doing = true;
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900444 err = find_fsync_dnodes(sbi, &inode_list);
445 if (err)
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900446 goto out;
447
448 if (list_empty(&inode_list))
449 goto out;
450
Haicheng Liaabe5132013-10-23 12:39:32 +0800451 need_writecp = true;
Chao Yu691c6fd2013-09-24 09:26:24 +0800452
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900453 /* step #2: recover data */
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900454 err = recover_data(sbi, &inode_list, CURSEG_WARM_NODE);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900455 f2fs_bug_on(!list_empty(&inode_list));
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900456out:
Gu Zheng5ebefc52013-06-27 09:28:54 +0800457 destroy_fsync_dnodes(&inode_list);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900458 kmem_cache_destroy(fsync_entry_slab);
Haicheng Liaabe5132013-10-23 12:39:32 +0800459 sbi->por_doing = false;
Chao Yu691c6fd2013-09-24 09:26:24 +0800460 if (!err && need_writecp)
Jaegeuk Kim2c2c1492013-05-20 14:48:49 +0900461 write_checkpoint(sbi, false);
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900462 return err;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900463}