blob: ba0fc2ec7caf2152864fbdbe4ef5a7ea8b5b7a61 [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
Jaegeuk Kim441ac5c2014-09-15 16:46:08 -070017/*
18 * Roll forward recovery scenarios.
19 *
20 * [Term] F: fsync_mark, D: dentry_mark
21 *
22 * 1. inode(x) | CP | inode(x) | dnode(F)
23 * -> Update the latest inode(x).
24 *
25 * 2. inode(x) | CP | inode(F) | dnode(F)
26 * -> No problem.
27 *
28 * 3. inode(x) | CP | dnode(F) | inode(x)
29 * -> Recover to the latest dnode(F), and drop the last inode(x)
30 *
31 * 4. inode(x) | CP | dnode(F) | inode(F)
32 * -> No problem.
33 *
34 * 5. CP | inode(x) | dnode(F)
35 * -> The inode(DF) was missing. Should drop this dnode(F).
36 *
37 * 6. CP | inode(DF) | dnode(F)
38 * -> No problem.
39 *
40 * 7. CP | dnode(F) | inode(DF)
41 * -> If f2fs_iget fails, then goto next to find inode(DF).
42 *
43 * 8. CP | dnode(F) | inode(x)
44 * -> If f2fs_iget fails, then goto next to find inode(DF).
45 * But it will fail due to no inode(DF).
46 */
47
Jaegeuk Kimd624c962012-11-02 17:13:32 +090048static struct kmem_cache *fsync_entry_slab;
49
50bool space_for_roll_forward(struct f2fs_sb_info *sbi)
51{
Jaegeuk Kim41382ec2016-05-16 11:06:50 -070052 s64 nalloc = percpu_counter_sum_positive(&sbi->alloc_valid_block_count);
53
54 if (sbi->last_valid_block_count + nalloc > sbi->user_block_count)
Jaegeuk Kimd624c962012-11-02 17:13:32 +090055 return false;
56 return true;
57}
58
59static struct fsync_inode_entry *get_fsync_inode(struct list_head *head,
60 nid_t ino)
61{
Jaegeuk Kimd624c962012-11-02 17:13:32 +090062 struct fsync_inode_entry *entry;
63
Chao Yu2d7b8222014-03-29 11:33:17 +080064 list_for_each_entry(entry, head, list)
Jaegeuk Kimd624c962012-11-02 17:13:32 +090065 if (entry->inode->i_ino == ino)
66 return entry;
Chao Yu2d7b8222014-03-29 11:33:17 +080067
Jaegeuk Kimd624c962012-11-02 17:13:32 +090068 return NULL;
69}
70
Chao Yu3f8ab272016-04-29 20:13:37 +080071static struct fsync_inode_entry *add_fsync_inode(struct list_head *head,
72 struct inode *inode)
73{
74 struct fsync_inode_entry *entry;
75
76 entry = kmem_cache_alloc(fsync_entry_slab, GFP_F2FS_ZERO);
77 if (!entry)
78 return NULL;
79
80 entry->inode = inode;
81 list_add_tail(&entry->list, head);
82
83 return entry;
84}
85
86static void del_fsync_inode(struct fsync_inode_entry *entry)
87{
88 iput(entry->inode);
89 list_del(&entry->list);
90 kmem_cache_free(fsync_entry_slab, entry);
91}
92
Chao Yuf61cce52016-05-07 16:15:05 +080093static int recover_dentry(struct inode *inode, struct page *ipage,
94 struct list_head *dir_list)
Jaegeuk Kimd624c962012-11-02 17:13:32 +090095{
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +090096 struct f2fs_inode *raw_inode = F2FS_INODE(ipage);
Jaegeuk Kim74d0b912013-05-15 16:40:02 +090097 nid_t pino = le32_to_cpu(raw_inode->i_pino);
Jaegeuk Kim6b8213d2013-05-28 09:19:22 +090098 struct f2fs_dir_entry *de;
Shuoran Liue7ba1082016-08-29 11:27:56 +080099 struct fscrypt_name fname;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900100 struct page *page;
Jaegeuk Kim6b8213d2013-05-28 09:19:22 +0900101 struct inode *dir, *einode;
Chao Yuf61cce52016-05-07 16:15:05 +0800102 struct fsync_inode_entry *entry;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900103 int err = 0;
Shuoran Liue7ba1082016-08-29 11:27:56 +0800104 char *name;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900105
Chao Yuf61cce52016-05-07 16:15:05 +0800106 entry = get_fsync_inode(dir_list, pino);
107 if (!entry) {
108 dir = f2fs_iget(inode->i_sb, pino);
109 if (IS_ERR(dir)) {
110 err = PTR_ERR(dir);
111 goto out;
112 }
113
114 entry = add_fsync_inode(dir_list, dir);
115 if (!entry) {
116 err = -ENOMEM;
117 iput(dir);
118 goto out;
119 }
Jaegeuk Kimed57c272014-04-15 11:19:28 +0900120 }
121
Chao Yuf61cce52016-05-07 16:15:05 +0800122 dir = entry->inode;
123
Shuoran Liue7ba1082016-08-29 11:27:56 +0800124 memset(&fname, 0, sizeof(struct fscrypt_name));
125 fname.disk_name.len = le32_to_cpu(raw_inode->i_namelen);
126 fname.disk_name.name = raw_inode->i_name;
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700127
Shuoran Liue7ba1082016-08-29 11:27:56 +0800128 if (unlikely(fname.disk_name.len > F2FS_NAME_LEN)) {
Chao Yud96b14312013-12-23 11:12:21 +0800129 WARN_ON(1);
130 err = -ENAMETOOLONG;
Chao Yuf61cce52016-05-07 16:15:05 +0800131 goto out;
Chao Yud96b14312013-12-23 11:12:21 +0800132 }
Jaegeuk Kim6b8213d2013-05-28 09:19:22 +0900133retry:
Shuoran Liue7ba1082016-08-29 11:27:56 +0800134 de = __f2fs_find_entry(dir, &fname, &page);
Jaegeuk Kim418f6c22015-03-31 18:03:29 -0700135 if (de && inode->i_ino == le32_to_cpu(de->ino))
Russ W. Knize2e5558f2013-09-24 09:40:57 -0500136 goto out_unmap_put;
Jaegeuk Kim418f6c22015-03-31 18:03:29 -0700137
Jaegeuk Kim6b8213d2013-05-28 09:19:22 +0900138 if (de) {
139 einode = f2fs_iget(inode->i_sb, le32_to_cpu(de->ino));
140 if (IS_ERR(einode)) {
141 WARN_ON(1);
Chao Yu5c1f9922014-04-28 17:58:34 +0800142 err = PTR_ERR(einode);
143 if (err == -ENOENT)
Jaegeuk Kim6b8213d2013-05-28 09:19:22 +0900144 err = -EEXIST;
Russ W. Knize2e5558f2013-09-24 09:40:57 -0500145 goto out_unmap_put;
146 }
Jaegeuk Kim40813632014-09-02 15:31:18 -0700147 err = acquire_orphan_inode(F2FS_I_SB(inode));
Russ W. Knize2e5558f2013-09-24 09:40:57 -0500148 if (err) {
149 iput(einode);
150 goto out_unmap_put;
Jaegeuk Kim6b8213d2013-05-28 09:19:22 +0900151 }
Chao Yudbeacf02014-09-24 18:17:04 +0800152 f2fs_delete_entry(de, page, dir, einode);
Jaegeuk Kim6b8213d2013-05-28 09:19:22 +0900153 iput(einode);
154 goto retry;
Chao Yu91246c22016-07-19 08:27:47 +0800155 } else if (IS_ERR(page)) {
156 err = PTR_ERR(page);
157 } else {
Shuoran Liue7ba1082016-08-29 11:27:56 +0800158 err = __f2fs_do_add_link(dir, &fname, inode,
Chao Yu91246c22016-07-19 08:27:47 +0800159 inode->i_ino, inode->i_mode);
Jaegeuk Kim6b8213d2013-05-28 09:19:22 +0900160 }
Russ W. Knize2e5558f2013-09-24 09:40:57 -0500161 goto out;
162
163out_unmap_put:
Jaegeuk Kim9486ba42014-11-21 16:36:28 -0800164 f2fs_dentry_kunmap(dir, page);
Russ W. Knize2e5558f2013-09-24 09:40:57 -0500165 f2fs_put_page(page, 0);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900166out:
Shuoran Liue7ba1082016-08-29 11:27:56 +0800167 if (file_enc_name(inode))
168 name = "<encrypted>";
169 else
170 name = raw_inode->i_name;
Chris Fries6c311ec2014-01-17 14:44:39 -0600171 f2fs_msg(inode->i_sb, KERN_NOTICE,
172 "%s: ino = %x, name = %s, dir = %lx, err = %d",
Shuoran Liue7ba1082016-08-29 11:27:56 +0800173 __func__, ino_of_node(ipage), name,
Dan Carpenterf28c06f2013-05-23 13:02:13 +0300174 IS_ERR(dir) ? 0 : dir->i_ino, err);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900175 return err;
176}
177
Jaegeuk Kimc52e1b12014-09-11 14:29:06 -0700178static void recover_inode(struct inode *inode, struct page *page)
Jaegeuk Kim441ac5c2014-09-15 16:46:08 -0700179{
180 struct f2fs_inode *raw = F2FS_INODE(page);
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700181 char *name;
Jaegeuk Kim441ac5c2014-09-15 16:46:08 -0700182
183 inode->i_mode = le16_to_cpu(raw->i_mode);
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700184 f2fs_i_size_write(inode, le64_to_cpu(raw->i_size));
Jaegeuk Kim441ac5c2014-09-15 16:46:08 -0700185 inode->i_atime.tv_sec = le64_to_cpu(raw->i_mtime);
186 inode->i_ctime.tv_sec = le64_to_cpu(raw->i_ctime);
187 inode->i_mtime.tv_sec = le64_to_cpu(raw->i_mtime);
188 inode->i_atime.tv_nsec = le32_to_cpu(raw->i_mtime_nsec);
189 inode->i_ctime.tv_nsec = le32_to_cpu(raw->i_ctime_nsec);
190 inode->i_mtime.tv_nsec = le32_to_cpu(raw->i_mtime_nsec);
Jaegeuk Kimf356fe02013-05-16 15:04:49 +0900191
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700192 if (file_enc_name(inode))
193 name = "<encrypted>";
194 else
195 name = F2FS_INODE(page)->i_name;
196
Jaegeuk Kimf356fe02013-05-16 15:04:49 +0900197 f2fs_msg(inode->i_sb, KERN_NOTICE, "recover_inode: ino = %x, name = %s",
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700198 ino_of_node(page), name);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900199}
200
Jaegeuk Kim807b1e12015-12-03 14:14:40 -0800201static bool is_same_inode(struct inode *inode, struct page *ipage)
202{
203 struct f2fs_inode *ri = F2FS_INODE(ipage);
204 struct timespec disk;
205
206 if (!IS_INODE(ipage))
207 return true;
208
209 disk.tv_sec = le64_to_cpu(ri->i_ctime);
210 disk.tv_nsec = le32_to_cpu(ri->i_ctime_nsec);
211 if (timespec_compare(&inode->i_ctime, &disk) > 0)
212 return false;
213
214 disk.tv_sec = le64_to_cpu(ri->i_atime);
215 disk.tv_nsec = le32_to_cpu(ri->i_atime_nsec);
216 if (timespec_compare(&inode->i_atime, &disk) > 0)
217 return false;
218
219 disk.tv_sec = le64_to_cpu(ri->i_mtime);
220 disk.tv_nsec = le32_to_cpu(ri->i_mtime_nsec);
221 if (timespec_compare(&inode->i_mtime, &disk) > 0)
222 return false;
223
224 return true;
225}
226
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900227static int find_fsync_dnodes(struct f2fs_sb_info *sbi, struct list_head *head)
228{
Jaegeuk Kimd71b5562013-08-09 15:03:21 +0900229 unsigned long long cp_ver = cur_cp_version(F2FS_CKPT(sbi));
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900230 struct curseg_info *curseg;
Chao Yu3f8ab272016-04-29 20:13:37 +0800231 struct inode *inode;
Jaegeuk Kim4c521f492014-09-11 13:49:55 -0700232 struct page *page = NULL;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900233 block_t blkaddr;
234 int err = 0;
235
236 /* get node pages in the current segment */
237 curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
Chao Yu695fd1e2014-02-27 19:52:21 +0800238 blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900239
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900240 while (1) {
241 struct fsync_inode_entry *entry;
242
Chao Yuf0c9cad2015-04-18 18:05:36 +0800243 if (!is_valid_blkaddr(sbi, blkaddr, META_POR))
Jaegeuk Kim4c521f492014-09-11 13:49:55 -0700244 return 0;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900245
Chao Yu2b947002015-10-12 17:04:21 +0800246 page = get_tmp_page(sbi, blkaddr);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900247
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900248 if (cp_ver != cpver_of_node(page))
Jaegeuk Kimf356fe02013-05-16 15:04:49 +0900249 break;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900250
251 if (!is_fsync_dnode(page))
252 goto next;
253
254 entry = get_fsync_inode(head, ino_of_node(page));
Jaegeuk Kim807b1e12015-12-03 14:14:40 -0800255 if (entry) {
256 if (!is_same_inode(entry->inode, page))
257 goto next;
258 } else {
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900259 if (IS_INODE(page) && is_dent_dnode(page)) {
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900260 err = recover_inode_page(sbi, page);
261 if (err)
Jaegeuk Kimf356fe02013-05-16 15:04:49 +0900262 break;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900263 }
264
Jaegeuk Kim441ac5c2014-09-15 16:46:08 -0700265 /*
266 * CP | dnode(F) | inode(DF)
267 * For this case, we should not give up now.
268 */
Chao Yu3f8ab272016-04-29 20:13:37 +0800269 inode = f2fs_iget(sbi->sb, ino_of_node(page));
270 if (IS_ERR(inode)) {
271 err = PTR_ERR(inode);
Jaegeuk Kim8fbc4182015-02-24 18:01:46 -0800272 if (err == -ENOENT) {
273 err = 0;
Jaegeuk Kim441ac5c2014-09-15 16:46:08 -0700274 goto next;
Jaegeuk Kim8fbc4182015-02-24 18:01:46 -0800275 }
Jaegeuk Kimf356fe02013-05-16 15:04:49 +0900276 break;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900277 }
Chao Yu3f8ab272016-04-29 20:13:37 +0800278
279 /* add this fsync inode to the list */
280 entry = add_fsync_inode(head, inode);
281 if (!entry) {
282 err = -ENOMEM;
283 iput(inode);
284 break;
285 }
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900286 }
Jaegeuk Kimaddbe452013-05-15 10:49:13 +0900287 entry->blkaddr = blkaddr;
288
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700289 if (IS_INODE(page) && is_dent_dnode(page))
290 entry->last_dentry = blkaddr;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900291next:
292 /* check next segment */
293 blkaddr = next_blkaddr_of_node(page);
Jaegeuk Kim4c521f492014-09-11 13:49:55 -0700294 f2fs_put_page(page, 1);
Chao Yu635aee12014-12-08 15:02:52 +0800295
296 ra_meta_pages_cond(sbi, blkaddr);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900297 }
Jaegeuk Kim4c521f492014-09-11 13:49:55 -0700298 f2fs_put_page(page, 1);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900299 return err;
300}
301
Gu Zheng5ebefc52013-06-27 09:28:54 +0800302static void destroy_fsync_dnodes(struct list_head *head)
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900303{
Dan Carpenterd8b79b22013-01-20 18:02:58 +0300304 struct fsync_inode_entry *entry, *tmp;
305
Chao Yu3f8ab272016-04-29 20:13:37 +0800306 list_for_each_entry_safe(entry, tmp, head, list)
307 del_fsync_inode(entry);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900308}
309
Jaegeuk Kim39cf72c2013-05-22 08:20:01 +0900310static int check_index_in_prev_nodes(struct f2fs_sb_info *sbi,
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900311 block_t blkaddr, struct dnode_of_data *dn)
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900312{
313 struct seg_entry *sentry;
314 unsigned int segno = GET_SEGNO(sbi, blkaddr);
Jaegeuk Kim491c0852014-02-04 13:01:10 +0900315 unsigned short blkoff = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
Jaegeuk Kimf6517cf2014-01-28 14:54:07 +0900316 struct f2fs_summary_block *sum_node;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900317 struct f2fs_summary sum;
Jaegeuk Kimf6517cf2014-01-28 14:54:07 +0900318 struct page *sum_page, *node_page;
Jaegeuk Kimc9ef4812015-03-26 18:46:38 -0700319 struct dnode_of_data tdn = *dn;
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900320 nid_t ino, nid;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900321 struct inode *inode;
Jaegeuk Kimde936532013-08-12 21:08:03 +0900322 unsigned int offset;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900323 block_t bidx;
324 int i;
325
326 sentry = get_seg_entry(sbi, segno);
327 if (!f2fs_test_bit(blkoff, sentry->cur_valid_map))
Jaegeuk Kim39cf72c2013-05-22 08:20:01 +0900328 return 0;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900329
330 /* Get the previous summary */
331 for (i = CURSEG_WARM_DATA; i <= CURSEG_COLD_DATA; i++) {
332 struct curseg_info *curseg = CURSEG_I(sbi, i);
333 if (curseg->segno == segno) {
334 sum = curseg->sum_blk->entries[blkoff];
Jaegeuk Kimf6517cf2014-01-28 14:54:07 +0900335 goto got_it;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900336 }
337 }
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900338
Jaegeuk Kimf6517cf2014-01-28 14:54:07 +0900339 sum_page = get_sum_page(sbi, segno);
340 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
341 sum = sum_node->entries[blkoff];
342 f2fs_put_page(sum_page, 1);
343got_it:
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900344 /* Use the locked dnode page and inode */
345 nid = le32_to_cpu(sum.nid);
346 if (dn->inode->i_ino == nid) {
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900347 tdn.nid = nid;
Jaegeuk Kimc9ef4812015-03-26 18:46:38 -0700348 if (!dn->inode_page_locked)
349 lock_page(dn->inode_page);
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900350 tdn.node_page = dn->inode_page;
Jaegeuk Kim060dd672013-06-24 07:47:23 +0900351 tdn.ofs_in_node = le16_to_cpu(sum.ofs_in_node);
Jaegeuk Kimc9ef4812015-03-26 18:46:38 -0700352 goto truncate_out;
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900353 } else if (dn->nid == nid) {
Jaegeuk Kim060dd672013-06-24 07:47:23 +0900354 tdn.ofs_in_node = le16_to_cpu(sum.ofs_in_node);
Jaegeuk Kimc9ef4812015-03-26 18:46:38 -0700355 goto truncate_out;
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900356 }
357
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900358 /* Get the node page */
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900359 node_page = get_node_page(sbi, nid);
Jaegeuk Kim39cf72c2013-05-22 08:20:01 +0900360 if (IS_ERR(node_page))
361 return PTR_ERR(node_page);
Jaegeuk Kimde936532013-08-12 21:08:03 +0900362
363 offset = ofs_of_node(node_page);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900364 ino = ino_of_node(node_page);
365 f2fs_put_page(node_page, 1);
366
Jaegeuk Kim60979112014-09-13 00:35:58 +0900367 if (ino != dn->inode->i_ino) {
368 /* Deallocate previous index in the node page */
369 inode = f2fs_iget(sbi->sb, ino);
370 if (IS_ERR(inode))
371 return PTR_ERR(inode);
372 } else {
373 inode = dn->inode;
374 }
Namjae Jeon06025f42012-12-22 12:09:43 +0900375
Chao Yu81ca7352016-01-26 15:39:35 +0800376 bidx = start_bidx_of_node(offset, inode) + le16_to_cpu(sum.ofs_in_node);
Jaegeuk Kimde936532013-08-12 21:08:03 +0900377
Jaegeuk Kimc9ef4812015-03-26 18:46:38 -0700378 /*
379 * if inode page is locked, unlock temporarily, but its reference
380 * count keeps alive.
381 */
382 if (ino == dn->inode->i_ino && dn->inode_page_locked)
383 unlock_page(dn->inode_page);
384
385 set_new_dnode(&tdn, inode, NULL, NULL, 0);
386 if (get_dnode_of_data(&tdn, bidx, LOOKUP_NODE))
387 goto out;
388
389 if (tdn.data_blkaddr == blkaddr)
390 truncate_data_blocks_range(&tdn, 1);
391
392 f2fs_put_dnode(&tdn);
393out:
394 if (ino != dn->inode->i_ino)
Jaegeuk Kim60979112014-09-13 00:35:58 +0900395 iput(inode);
Jaegeuk Kimc9ef4812015-03-26 18:46:38 -0700396 else if (dn->inode_page_locked)
397 lock_page(dn->inode_page);
398 return 0;
399
400truncate_out:
401 if (datablock_addr(tdn.node_page, tdn.ofs_in_node) == blkaddr)
402 truncate_data_blocks_range(&tdn, 1);
403 if (dn->inode->i_ino == nid && !dn->inode_page_locked)
404 unlock_page(dn->inode_page);
Jaegeuk Kim39cf72c2013-05-22 08:20:01 +0900405 return 0;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900406}
407
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900408static int do_recover_data(struct f2fs_sb_info *sbi, struct inode *inode,
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900409 struct page *page, block_t blkaddr)
410{
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900411 struct dnode_of_data dn;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900412 struct node_info ni;
Chao Yu81ca7352016-01-26 15:39:35 +0800413 unsigned int start, end;
Jaegeuk Kimf356fe02013-05-16 15:04:49 +0900414 int err = 0, recovered = 0;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900415
Jaegeuk Kim1c35a902014-08-07 23:49:17 -0700416 /* step 1: recover xattr */
417 if (IS_INODE(page)) {
418 recover_inline_xattr(inode, page);
419 } else if (f2fs_has_xattr_block(ofs_of_node(page))) {
Jaegeuk Kimbc4a1f82015-01-22 14:48:28 -0800420 /*
421 * Deprecated; xattr blocks should be found from cold log.
422 * But, we should remain this for backward compatibility.
423 */
Jaegeuk Kim1c35a902014-08-07 23:49:17 -0700424 recover_xattr_data(inode, page, blkaddr);
425 goto out;
426 }
Chao Yu70cfed82014-08-02 15:26:04 +0800427
Jaegeuk Kim1c35a902014-08-07 23:49:17 -0700428 /* step 2: recover inline data */
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900429 if (recover_inline_data(inode, page))
430 goto out;
431
Jaegeuk Kim1c35a902014-08-07 23:49:17 -0700432 /* step 3: recover data indices */
Chao Yu81ca7352016-01-26 15:39:35 +0800433 start = start_bidx_of_node(ofs_of_node(page), inode);
434 end = start + ADDRS_PER_PAGE(page, inode);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900435
436 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900437
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900438 err = get_dnode_of_data(&dn, start, ALLOC_NODE);
Chao Yu72235542015-09-25 17:54:56 +0800439 if (err)
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900440 goto out;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900441
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800442 f2fs_wait_on_page_writeback(dn.node_page, NODE, true);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900443
444 get_node_info(sbi, dn.nid, &ni);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700445 f2fs_bug_on(sbi, ni.ino != ino_of_node(page));
446 f2fs_bug_on(sbi, ofs_of_node(dn.node_page) != ofs_of_node(page));
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900447
Chao Yu12a83432015-08-05 17:23:54 +0800448 for (; start < end; start++, dn.ofs_in_node++) {
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900449 block_t src, dest;
450
451 src = datablock_addr(dn.node_page, dn.ofs_in_node);
452 dest = datablock_addr(page, dn.ofs_in_node);
453
Chao Yu12a83432015-08-05 17:23:54 +0800454 /* skip recovering if dest is the same as src */
455 if (src == dest)
456 continue;
457
458 /* dest is invalid, just invalidate src block */
459 if (dest == NULL_ADDR) {
460 truncate_data_blocks_range(&dn, 1);
461 continue;
462 }
463
Jaegeuk Kim26de9b12016-05-20 20:42:37 -0700464 if ((start + 1) << PAGE_SHIFT > i_size_read(inode))
465 f2fs_i_size_write(inode, (start + 1) << PAGE_SHIFT);
466
Chao Yu12a83432015-08-05 17:23:54 +0800467 /*
468 * dest is reserved block, invalidate src block
469 * and then reserve one new block in dnode page.
470 */
471 if (dest == NEW_ADDR) {
472 truncate_data_blocks_range(&dn, 1);
Jaegeuk Kim3b9b10f2016-05-11 09:13:13 -0700473 reserve_new_block(&dn);
Chao Yu12a83432015-08-05 17:23:54 +0800474 continue;
475 }
476
477 /* dest is valid block, try to recover from src to dest */
478 if (is_valid_blkaddr(sbi, dest, META_POR)) {
Jaegeuk Kime03b07d2015-04-01 19:38:20 -0700479
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900480 if (src == NULL_ADDR) {
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900481 err = reserve_new_block(&dn);
Jaegeuk Kim975756c2016-05-19 11:57:21 -0700482#ifdef CONFIG_F2FS_FAULT_INJECTION
483 while (err)
484 err = reserve_new_block(&dn);
485#endif
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900486 /* We should not get -ENOSPC */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700487 f2fs_bug_on(sbi, err);
Jaegeuk Kim6f3ec992016-07-19 19:30:06 -0700488 if (err)
489 goto err;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900490 }
491
492 /* Check the previous node page having this index */
Jaegeuk Kim39cf72c2013-05-22 08:20:01 +0900493 err = check_index_in_prev_nodes(sbi, dest, &dn);
494 if (err)
495 goto err;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900496
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900497 /* write dummy data page */
Chao Yu528e3452015-05-28 19:15:35 +0800498 f2fs_replace_block(sbi, &dn, src, dest,
Chao Yu28bc1062016-02-06 14:40:34 +0800499 ni.version, false, false);
Jaegeuk Kimf356fe02013-05-16 15:04:49 +0900500 recovered++;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900501 }
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900502 }
503
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900504 copy_node_footer(dn.node_page, page);
505 fill_node_footer(dn.node_page, dn.nid, ni.ino,
506 ofs_of_node(page), false);
507 set_page_dirty(dn.node_page);
Jaegeuk Kim39cf72c2013-05-22 08:20:01 +0900508err:
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900509 f2fs_put_dnode(&dn);
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900510out:
Chris Fries6c311ec2014-01-17 14:44:39 -0600511 f2fs_msg(sbi->sb, KERN_NOTICE,
512 "recover_data: ino = %lx, recovered = %d blocks, err = %d",
513 inode->i_ino, recovered, err);
Jaegeuk Kim39cf72c2013-05-22 08:20:01 +0900514 return err;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900515}
516
Chao Yuf61cce52016-05-07 16:15:05 +0800517static int recover_data(struct f2fs_sb_info *sbi, struct list_head *inode_list,
518 struct list_head *dir_list)
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900519{
Jaegeuk Kimd71b5562013-08-09 15:03:21 +0900520 unsigned long long cp_ver = cur_cp_version(F2FS_CKPT(sbi));
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900521 struct curseg_info *curseg;
Jaegeuk Kim4c521f492014-09-11 13:49:55 -0700522 struct page *page = NULL;
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900523 int err = 0;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900524 block_t blkaddr;
525
526 /* get node pages in the current segment */
Chao Yub7973f22015-12-01 11:43:59 +0800527 curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900528 blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
529
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900530 while (1) {
531 struct fsync_inode_entry *entry;
532
Chao Yuf0c9cad2015-04-18 18:05:36 +0800533 if (!is_valid_blkaddr(sbi, blkaddr, META_POR))
Jaegeuk Kim45856af2013-05-20 10:26:09 +0900534 break;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900535
Chao Yu635aee12014-12-08 15:02:52 +0800536 ra_meta_pages_cond(sbi, blkaddr);
537
Chao Yu2b947002015-10-12 17:04:21 +0800538 page = get_tmp_page(sbi, blkaddr);
Jaegeuk Kim4c521f492014-09-11 13:49:55 -0700539
540 if (cp_ver != cpver_of_node(page)) {
541 f2fs_put_page(page, 1);
542 break;
543 }
544
Chao Yuf61cce52016-05-07 16:15:05 +0800545 entry = get_fsync_inode(inode_list, ino_of_node(page));
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900546 if (!entry)
547 goto next;
Jaegeuk Kim441ac5c2014-09-15 16:46:08 -0700548 /*
549 * inode(x) | CP | inode(x) | dnode(F)
550 * In this case, we can lose the latest inode(x).
Jaegeuk Kimc52e1b12014-09-11 14:29:06 -0700551 * So, call recover_inode for the inode update.
Jaegeuk Kim441ac5c2014-09-15 16:46:08 -0700552 */
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700553 if (IS_INODE(page))
Jaegeuk Kimc52e1b12014-09-11 14:29:06 -0700554 recover_inode(entry->inode, page);
555 if (entry->last_dentry == blkaddr) {
Chao Yuf61cce52016-05-07 16:15:05 +0800556 err = recover_dentry(entry->inode, page, dir_list);
Jaegeuk Kimc52e1b12014-09-11 14:29:06 -0700557 if (err) {
558 f2fs_put_page(page, 1);
559 break;
560 }
561 }
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900562 err = do_recover_data(sbi, entry->inode, page, blkaddr);
Jaegeuk Kim4c521f492014-09-11 13:49:55 -0700563 if (err) {
564 f2fs_put_page(page, 1);
Jaegeuk Kim45856af2013-05-20 10:26:09 +0900565 break;
Jaegeuk Kim4c521f492014-09-11 13:49:55 -0700566 }
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900567
Chao Yu3f8ab272016-04-29 20:13:37 +0800568 if (entry->blkaddr == blkaddr)
569 del_fsync_inode(entry);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900570next:
571 /* check next segment */
572 blkaddr = next_blkaddr_of_node(page);
Jaegeuk Kim4c521f492014-09-11 13:49:55 -0700573 f2fs_put_page(page, 1);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900574 }
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900575 if (!err)
576 allocate_new_segments(sbi);
577 return err;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900578}
579
Jaegeuk Kim6781eab2016-03-23 16:12:58 -0700580int recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only)
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900581{
Jaegeuk Kimcf2271e2014-07-25 15:47:25 -0700582 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900583 struct list_head inode_list;
Chao Yuf61cce52016-05-07 16:15:05 +0800584 struct list_head dir_list;
Jaegeuk Kimcf2271e2014-07-25 15:47:25 -0700585 block_t blkaddr;
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900586 int err;
Jaegeuk Kim6781eab2016-03-23 16:12:58 -0700587 int ret = 0;
Haicheng Liaabe5132013-10-23 12:39:32 +0800588 bool need_writecp = false;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900589
590 fsync_entry_slab = f2fs_kmem_cache_create("f2fs_fsync_inode_entry",
Gu Zhenge8512d22014-03-07 18:43:28 +0800591 sizeof(struct fsync_inode_entry));
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900592 if (!fsync_entry_slab)
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900593 return -ENOMEM;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900594
595 INIT_LIST_HEAD(&inode_list);
Chao Yuf61cce52016-05-07 16:15:05 +0800596 INIT_LIST_HEAD(&dir_list);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900597
Jaegeuk Kim14f4e692014-08-13 16:30:46 -0700598 /* prevent checkpoint */
599 mutex_lock(&sbi->cp_mutex);
600
Jaegeuk Kimcf2271e2014-07-25 15:47:25 -0700601 blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
602
Jaegeuk Kim315df832015-08-11 12:45:39 -0700603 /* step #1: find fsynced inode numbers */
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900604 err = find_fsync_dnodes(sbi, &inode_list);
Jaegeuk Kim6781eab2016-03-23 16:12:58 -0700605 if (err || list_empty(&inode_list))
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900606 goto out;
607
Jaegeuk Kim6781eab2016-03-23 16:12:58 -0700608 if (check_only) {
609 ret = 1;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900610 goto out;
Jaegeuk Kim6781eab2016-03-23 16:12:58 -0700611 }
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900612
Haicheng Liaabe5132013-10-23 12:39:32 +0800613 need_writecp = true;
Chao Yu691c6fd2013-09-24 09:26:24 +0800614
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900615 /* step #2: recover data */
Chao Yuf61cce52016-05-07 16:15:05 +0800616 err = recover_data(sbi, &inode_list, &dir_list);
Jaegeuk Kimb3073842014-08-08 10:18:43 -0700617 if (!err)
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700618 f2fs_bug_on(sbi, !list_empty(&inode_list));
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900619out:
Gu Zheng5ebefc52013-06-27 09:28:54 +0800620 destroy_fsync_dnodes(&inode_list);
Jaegeuk Kimcf2271e2014-07-25 15:47:25 -0700621
Jaegeuk Kim4c521f492014-09-11 13:49:55 -0700622 /* truncate meta pages to be used by the recovery */
623 truncate_inode_pages_range(META_MAPPING(sbi),
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300624 (loff_t)MAIN_BLKADDR(sbi) << PAGE_SHIFT, -1);
Jaegeuk Kim4c521f492014-09-11 13:49:55 -0700625
Jaegeuk Kimcf2271e2014-07-25 15:47:25 -0700626 if (err) {
627 truncate_inode_pages_final(NODE_MAPPING(sbi));
628 truncate_inode_pages_final(META_MAPPING(sbi));
629 }
630
Chao Yucaf00472015-01-28 17:48:42 +0800631 clear_sbi_flag(sbi, SBI_POR_DOING);
Jaegeuk Kimcf2271e2014-07-25 15:47:25 -0700632 if (err) {
Chao Yue90c2d22015-07-28 18:36:47 +0800633 bool invalidate = false;
634
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700635 if (test_opt(sbi, LFS)) {
636 update_meta_page(sbi, NULL, blkaddr);
Chao Yue90c2d22015-07-28 18:36:47 +0800637 invalidate = true;
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700638 } else if (discard_next_dnode(sbi, blkaddr)) {
639 invalidate = true;
640 }
Jaegeuk Kimcf2271e2014-07-25 15:47:25 -0700641
Chao Yu275b66b2016-08-29 23:58:34 +0800642 f2fs_wait_all_discard_bio(sbi);
643
Jaegeuk Kimcf2271e2014-07-25 15:47:25 -0700644 /* Flush all the NAT/SIT pages */
645 while (get_pages(sbi, F2FS_DIRTY_META))
646 sync_meta_pages(sbi, META, LONG_MAX);
Chao Yue90c2d22015-07-28 18:36:47 +0800647
648 /* invalidate temporary meta page */
649 if (invalidate)
650 invalidate_mapping_pages(META_MAPPING(sbi),
651 blkaddr, blkaddr);
652
Jaegeuk Kim14f4e692014-08-13 16:30:46 -0700653 set_ckpt_flags(sbi->ckpt, CP_ERROR_FLAG);
654 mutex_unlock(&sbi->cp_mutex);
Jaegeuk Kimcf2271e2014-07-25 15:47:25 -0700655 } else if (need_writecp) {
Jaegeuk Kim75ab4cb2014-09-20 21:57:51 -0700656 struct cp_control cpc = {
Jaegeuk Kim10027552015-04-09 17:03:53 -0700657 .reason = CP_RECOVERY,
Jaegeuk Kim75ab4cb2014-09-20 21:57:51 -0700658 };
Jaegeuk Kim14f4e692014-08-13 16:30:46 -0700659 mutex_unlock(&sbi->cp_mutex);
Chao Yuc34f42e2015-12-23 17:50:30 +0800660 err = write_checkpoint(sbi, &cpc);
Jaegeuk Kim14f4e692014-08-13 16:30:46 -0700661 } else {
662 mutex_unlock(&sbi->cp_mutex);
Jaegeuk Kimcf2271e2014-07-25 15:47:25 -0700663 }
Chao Yuf61cce52016-05-07 16:15:05 +0800664
665 destroy_fsync_dnodes(&dir_list);
666 kmem_cache_destroy(fsync_entry_slab);
Jaegeuk Kim6781eab2016-03-23 16:12:58 -0700667 return ret ? ret: err;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900668}