Commit | Line | Data |
---|---|---|
7c1a000d | 1 | // SPDX-License-Identifier: GPL-2.0 |
0a8165d7 | 2 | /* |
d624c96f JK |
3 | * fs/f2fs/recovery.c |
4 | * | |
5 | * Copyright (c) 2012 Samsung Electronics Co., Ltd. | |
6 | * http://www.samsung.com/ | |
d624c96f | 7 | */ |
7ad08a58 | 8 | #include <asm/unaligned.h> |
d624c96f JK |
9 | #include <linux/fs.h> |
10 | #include <linux/f2fs_fs.h> | |
11 | #include "f2fs.h" | |
12 | #include "node.h" | |
13 | #include "segment.h" | |
14 | ||
441ac5cb JK |
15 | /* |
16 | * Roll forward recovery scenarios. | |
17 | * | |
18 | * [Term] F: fsync_mark, D: dentry_mark | |
19 | * | |
20 | * 1. inode(x) | CP | inode(x) | dnode(F) | |
21 | * -> Update the latest inode(x). | |
22 | * | |
23 | * 2. inode(x) | CP | inode(F) | dnode(F) | |
24 | * -> No problem. | |
25 | * | |
26 | * 3. inode(x) | CP | dnode(F) | inode(x) | |
27 | * -> Recover to the latest dnode(F), and drop the last inode(x) | |
28 | * | |
29 | * 4. inode(x) | CP | dnode(F) | inode(F) | |
30 | * -> No problem. | |
31 | * | |
32 | * 5. CP | inode(x) | dnode(F) | |
33 | * -> The inode(DF) was missing. Should drop this dnode(F). | |
34 | * | |
35 | * 6. CP | inode(DF) | dnode(F) | |
36 | * -> No problem. | |
37 | * | |
38 | * 7. CP | dnode(F) | inode(DF) | |
39 | * -> If f2fs_iget fails, then goto next to find inode(DF). | |
40 | * | |
41 | * 8. CP | dnode(F) | inode(x) | |
42 | * -> If f2fs_iget fails, then goto next to find inode(DF). | |
43 | * But it will fail due to no inode(DF). | |
44 | */ | |
45 | ||
d624c96f JK |
46 | static struct kmem_cache *fsync_entry_slab; |
47 | ||
4d57b86d | 48 | bool f2fs_space_for_roll_forward(struct f2fs_sb_info *sbi) |
d624c96f | 49 | { |
41382ec4 JK |
50 | s64 nalloc = percpu_counter_sum_positive(&sbi->alloc_valid_block_count); |
51 | ||
52 | if (sbi->last_valid_block_count + nalloc > sbi->user_block_count) | |
d624c96f JK |
53 | return false; |
54 | return true; | |
55 | } | |
56 | ||
57 | static struct fsync_inode_entry *get_fsync_inode(struct list_head *head, | |
58 | nid_t ino) | |
59 | { | |
d624c96f JK |
60 | struct fsync_inode_entry *entry; |
61 | ||
2d7b822a | 62 | list_for_each_entry(entry, head, list) |
d624c96f JK |
63 | if (entry->inode->i_ino == ino) |
64 | return entry; | |
2d7b822a | 65 | |
d624c96f JK |
66 | return NULL; |
67 | } | |
68 | ||
f4702d61 | 69 | static struct fsync_inode_entry *add_fsync_inode(struct f2fs_sb_info *sbi, |
4b2414d0 | 70 | struct list_head *head, nid_t ino, bool quota_inode) |
3f8ab270 | 71 | { |
e8ea9b3d | 72 | struct inode *inode; |
3f8ab270 | 73 | struct fsync_inode_entry *entry; |
4b2414d0 | 74 | int err; |
3f8ab270 | 75 | |
e8ea9b3d | 76 | inode = f2fs_iget_retry(sbi->sb, ino); |
f4702d61 JK |
77 | if (IS_ERR(inode)) |
78 | return ERR_CAST(inode); | |
79 | ||
4b2414d0 CY |
80 | err = dquot_initialize(inode); |
81 | if (err) | |
82 | goto err_out; | |
83 | ||
84 | if (quota_inode) { | |
85 | err = dquot_alloc_inode(inode); | |
86 | if (err) | |
87 | goto err_out; | |
88 | } | |
89 | ||
e8ea9b3d | 90 | entry = f2fs_kmem_cache_alloc(fsync_entry_slab, GFP_F2FS_ZERO); |
3f8ab270 CY |
91 | entry->inode = inode; |
92 | list_add_tail(&entry->list, head); | |
93 | ||
94 | return entry; | |
4b2414d0 CY |
95 | err_out: |
96 | iput(inode); | |
97 | return ERR_PTR(err); | |
3f8ab270 CY |
98 | } |
99 | ||
26b5a079 | 100 | static void del_fsync_inode(struct fsync_inode_entry *entry, int drop) |
3f8ab270 | 101 | { |
26b5a079 SY |
102 | if (drop) { |
103 | /* inode should not be recovered, drop it */ | |
104 | f2fs_inode_synced(entry->inode); | |
105 | } | |
3f8ab270 CY |
106 | iput(entry->inode); |
107 | list_del(&entry->list); | |
108 | kmem_cache_free(fsync_entry_slab, entry); | |
109 | } | |
110 | ||
43c780ba EB |
111 | static int init_recovered_filename(const struct inode *dir, |
112 | struct f2fs_inode *raw_inode, | |
113 | struct f2fs_filename *fname, | |
114 | struct qstr *usr_fname) | |
115 | { | |
116 | int err; | |
117 | ||
118 | memset(fname, 0, sizeof(*fname)); | |
119 | fname->disk_name.len = le32_to_cpu(raw_inode->i_namelen); | |
120 | fname->disk_name.name = raw_inode->i_name; | |
121 | ||
122 | if (WARN_ON(fname->disk_name.len > F2FS_NAME_LEN)) | |
123 | return -ENAMETOOLONG; | |
124 | ||
125 | if (!IS_ENCRYPTED(dir)) { | |
126 | usr_fname->name = fname->disk_name.name; | |
127 | usr_fname->len = fname->disk_name.len; | |
128 | fname->usr_fname = usr_fname; | |
129 | } | |
130 | ||
131 | /* Compute the hash of the filename */ | |
7ad08a58 DR |
132 | if (IS_ENCRYPTED(dir) && IS_CASEFOLDED(dir)) { |
133 | /* | |
134 | * In this case the hash isn't computable without the key, so it | |
135 | * was saved on-disk. | |
136 | */ | |
137 | if (fname->disk_name.len + sizeof(f2fs_hash_t) > F2FS_NAME_LEN) | |
138 | return -EINVAL; | |
139 | fname->hash = get_unaligned((f2fs_hash_t *) | |
140 | &raw_inode->i_name[fname->disk_name.len]); | |
141 | } else if (IS_CASEFOLDED(dir)) { | |
43c780ba EB |
142 | err = f2fs_init_casefolded_name(dir, fname); |
143 | if (err) | |
144 | return err; | |
145 | f2fs_hash_filename(dir, fname); | |
146 | #ifdef CONFIG_UNICODE | |
147 | /* Case-sensitive match is fine for recovery */ | |
148 | kfree(fname->cf_name.name); | |
149 | fname->cf_name.name = NULL; | |
150 | #endif | |
151 | } else { | |
152 | f2fs_hash_filename(dir, fname); | |
153 | } | |
154 | return 0; | |
155 | } | |
156 | ||
f61cce5b CY |
157 | static int recover_dentry(struct inode *inode, struct page *ipage, |
158 | struct list_head *dir_list) | |
d624c96f | 159 | { |
58bfaf44 | 160 | struct f2fs_inode *raw_inode = F2FS_INODE(ipage); |
74d0b917 | 161 | nid_t pino = le32_to_cpu(raw_inode->i_pino); |
6b8213d9 | 162 | struct f2fs_dir_entry *de; |
43c780ba EB |
163 | struct f2fs_filename fname; |
164 | struct qstr usr_fname; | |
d624c96f | 165 | struct page *page; |
6b8213d9 | 166 | struct inode *dir, *einode; |
f61cce5b | 167 | struct fsync_inode_entry *entry; |
d624c96f | 168 | int err = 0; |
e7ba108a | 169 | char *name; |
d624c96f | 170 | |
f61cce5b CY |
171 | entry = get_fsync_inode(dir_list, pino); |
172 | if (!entry) { | |
4b2414d0 CY |
173 | entry = add_fsync_inode(F2FS_I_SB(inode), dir_list, |
174 | pino, false); | |
f4702d61 JK |
175 | if (IS_ERR(entry)) { |
176 | dir = ERR_CAST(entry); | |
177 | err = PTR_ERR(entry); | |
f61cce5b CY |
178 | goto out; |
179 | } | |
ed57c27f JK |
180 | } |
181 | ||
f61cce5b | 182 | dir = entry->inode; |
43c780ba EB |
183 | err = init_recovered_filename(dir, raw_inode, &fname, &usr_fname); |
184 | if (err) | |
f61cce5b | 185 | goto out; |
6b8213d9 | 186 | retry: |
e7ba108a | 187 | de = __f2fs_find_entry(dir, &fname, &page); |
418f6c27 | 188 | if (de && inode->i_ino == le32_to_cpu(de->ino)) |
bdbc90fa | 189 | goto out_put; |
418f6c27 | 190 | |
6b8213d9 | 191 | if (de) { |
e8ea9b3d | 192 | einode = f2fs_iget_retry(inode->i_sb, le32_to_cpu(de->ino)); |
6b8213d9 JK |
193 | if (IS_ERR(einode)) { |
194 | WARN_ON(1); | |
5c1f9927 CY |
195 | err = PTR_ERR(einode); |
196 | if (err == -ENOENT) | |
6b8213d9 | 197 | err = -EEXIST; |
bdbc90fa | 198 | goto out_put; |
2e5558f4 | 199 | } |
4b2414d0 CY |
200 | |
201 | err = dquot_initialize(einode); | |
202 | if (err) { | |
203 | iput(einode); | |
bdbc90fa | 204 | goto out_put; |
4b2414d0 CY |
205 | } |
206 | ||
4d57b86d | 207 | err = f2fs_acquire_orphan_inode(F2FS_I_SB(inode)); |
2e5558f4 RK |
208 | if (err) { |
209 | iput(einode); | |
bdbc90fa | 210 | goto out_put; |
6b8213d9 | 211 | } |
dbeacf02 | 212 | f2fs_delete_entry(de, page, dir, einode); |
6b8213d9 JK |
213 | iput(einode); |
214 | goto retry; | |
91246c21 CY |
215 | } else if (IS_ERR(page)) { |
216 | err = PTR_ERR(page); | |
217 | } else { | |
4d57b86d | 218 | err = f2fs_add_dentry(dir, &fname, inode, |
91246c21 | 219 | inode->i_ino, inode->i_mode); |
d624c96f | 220 | } |
e8ea9b3d JK |
221 | if (err == -ENOMEM) |
222 | goto retry; | |
2e5558f4 RK |
223 | goto out; |
224 | ||
bdbc90fa | 225 | out_put: |
2e5558f4 | 226 | f2fs_put_page(page, 0); |
d624c96f | 227 | out: |
e7ba108a SL |
228 | if (file_enc_name(inode)) |
229 | name = "<encrypted>"; | |
230 | else | |
231 | name = raw_inode->i_name; | |
dcbb4c10 JP |
232 | f2fs_notice(F2FS_I_SB(inode), "%s: ino = %x, name = %s, dir = %lx, err = %d", |
233 | __func__, ino_of_node(ipage), name, | |
234 | IS_ERR(dir) ? 0 : dir->i_ino, err); | |
d624c96f JK |
235 | return err; |
236 | } | |
237 | ||
af033b2a CY |
238 | static int recover_quota_data(struct inode *inode, struct page *page) |
239 | { | |
240 | struct f2fs_inode *raw = F2FS_INODE(page); | |
241 | struct iattr attr; | |
242 | uid_t i_uid = le32_to_cpu(raw->i_uid); | |
243 | gid_t i_gid = le32_to_cpu(raw->i_gid); | |
244 | int err; | |
245 | ||
246 | memset(&attr, 0, sizeof(attr)); | |
247 | ||
248 | attr.ia_uid = make_kuid(inode->i_sb->s_user_ns, i_uid); | |
249 | attr.ia_gid = make_kgid(inode->i_sb->s_user_ns, i_gid); | |
250 | ||
251 | if (!uid_eq(attr.ia_uid, inode->i_uid)) | |
252 | attr.ia_valid |= ATTR_UID; | |
253 | if (!gid_eq(attr.ia_gid, inode->i_gid)) | |
254 | attr.ia_valid |= ATTR_GID; | |
255 | ||
256 | if (!attr.ia_valid) | |
257 | return 0; | |
258 | ||
259 | err = dquot_transfer(inode, &attr); | |
260 | if (err) | |
261 | set_sbi_flag(F2FS_I_SB(inode), SBI_QUOTA_NEED_REPAIR); | |
262 | return err; | |
263 | } | |
264 | ||
37a086f0 JK |
265 | static void recover_inline_flags(struct inode *inode, struct f2fs_inode *ri) |
266 | { | |
267 | if (ri->i_inline & F2FS_PIN_FILE) | |
268 | set_inode_flag(inode, FI_PIN_FILE); | |
269 | else | |
270 | clear_inode_flag(inode, FI_PIN_FILE); | |
271 | if (ri->i_inline & F2FS_DATA_EXIST) | |
272 | set_inode_flag(inode, FI_DATA_EXIST); | |
273 | else | |
274 | clear_inode_flag(inode, FI_DATA_EXIST); | |
37a086f0 JK |
275 | } |
276 | ||
af033b2a | 277 | static int recover_inode(struct inode *inode, struct page *page) |
d624c96f | 278 | { |
441ac5cb | 279 | struct f2fs_inode *raw = F2FS_INODE(page); |
e7d55452 | 280 | char *name; |
af033b2a | 281 | int err; |
441ac5cb JK |
282 | |
283 | inode->i_mode = le16_to_cpu(raw->i_mode); | |
af033b2a CY |
284 | |
285 | err = recover_quota_data(inode, page); | |
286 | if (err) | |
287 | return err; | |
288 | ||
dc4cd125 CY |
289 | i_uid_write(inode, le32_to_cpu(raw->i_uid)); |
290 | i_gid_write(inode, le32_to_cpu(raw->i_gid)); | |
f4474aa6 CY |
291 | |
292 | if (raw->i_inline & F2FS_EXTRA_ATTR) { | |
7beb01f7 | 293 | if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)) && |
f4474aa6 CY |
294 | F2FS_FITS_IN_INODE(raw, le16_to_cpu(raw->i_extra_isize), |
295 | i_projid)) { | |
296 | projid_t i_projid; | |
78130819 | 297 | kprojid_t kprojid; |
f4474aa6 CY |
298 | |
299 | i_projid = (projid_t)le32_to_cpu(raw->i_projid); | |
78130819 CY |
300 | kprojid = make_kprojid(&init_user_ns, i_projid); |
301 | ||
302 | if (!projid_eq(kprojid, F2FS_I(inode)->i_projid)) { | |
303 | err = f2fs_transfer_project_quota(inode, | |
304 | kprojid); | |
305 | if (err) | |
306 | return err; | |
307 | F2FS_I(inode)->i_projid = kprojid; | |
308 | } | |
f4474aa6 CY |
309 | } |
310 | } | |
311 | ||
fc9581c8 | 312 | f2fs_i_size_write(inode, le64_to_cpu(raw->i_size)); |
9f0552e0 | 313 | inode->i_atime.tv_sec = le64_to_cpu(raw->i_atime); |
441ac5cb JK |
314 | inode->i_ctime.tv_sec = le64_to_cpu(raw->i_ctime); |
315 | inode->i_mtime.tv_sec = le64_to_cpu(raw->i_mtime); | |
9f0552e0 | 316 | inode->i_atime.tv_nsec = le32_to_cpu(raw->i_atime_nsec); |
441ac5cb JK |
317 | inode->i_ctime.tv_nsec = le32_to_cpu(raw->i_ctime_nsec); |
318 | inode->i_mtime.tv_nsec = le32_to_cpu(raw->i_mtime_nsec); | |
f356fe0c | 319 | |
26787236 | 320 | F2FS_I(inode)->i_advise = raw->i_advise; |
19c73a69 | 321 | F2FS_I(inode)->i_flags = le32_to_cpu(raw->i_flags); |
0c093b59 | 322 | f2fs_set_inode_flags(inode); |
7de36cf3 CY |
323 | F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN] = |
324 | le16_to_cpu(raw->i_gc_failures); | |
26787236 | 325 | |
37a086f0 JK |
326 | recover_inline_flags(inode, raw); |
327 | ||
4a1728ca CY |
328 | f2fs_mark_inode_dirty_sync(inode, true); |
329 | ||
e7d55452 JK |
330 | if (file_enc_name(inode)) |
331 | name = "<encrypted>"; | |
332 | else | |
333 | name = F2FS_INODE(page)->i_name; | |
334 | ||
dcbb4c10 JP |
335 | f2fs_notice(F2FS_I_SB(inode), "recover_inode: ino = %x, name = %s, inline = %x", |
336 | ino_of_node(page), name, raw->i_inline); | |
af033b2a | 337 | return 0; |
d624c96f JK |
338 | } |
339 | ||
d40d30c5 JK |
340 | static int find_fsync_dnodes(struct f2fs_sb_info *sbi, struct list_head *head, |
341 | bool check_only) | |
d624c96f | 342 | { |
d624c96f | 343 | struct curseg_info *curseg; |
4c521f49 | 344 | struct page *page = NULL; |
d624c96f | 345 | block_t blkaddr; |
fb0e72c8 | 346 | unsigned int loop_cnt = 0; |
82902c06 CY |
347 | unsigned int free_blocks = MAIN_SEGS(sbi) * sbi->blocks_per_seg - |
348 | valid_user_blocks(sbi); | |
d624c96f JK |
349 | int err = 0; |
350 | ||
351 | /* get node pages in the current segment */ | |
352 | curseg = CURSEG_I(sbi, CURSEG_WARM_NODE); | |
695fd1ed | 353 | blkaddr = NEXT_FREE_BLKADDR(sbi, curseg); |
d624c96f | 354 | |
d624c96f JK |
355 | while (1) { |
356 | struct fsync_inode_entry *entry; | |
357 | ||
e1da7872 | 358 | if (!f2fs_is_valid_blkaddr(sbi, blkaddr, META_POR)) |
4c521f49 | 359 | return 0; |
d624c96f | 360 | |
4d57b86d | 361 | page = f2fs_get_tmp_page(sbi, blkaddr); |
7735730d CY |
362 | if (IS_ERR(page)) { |
363 | err = PTR_ERR(page); | |
364 | break; | |
365 | } | |
393ff91f | 366 | |
98838579 CY |
367 | if (!is_recoverable_dnode(page)) { |
368 | f2fs_put_page(page, 1); | |
f356fe0c | 369 | break; |
98838579 | 370 | } |
d624c96f JK |
371 | |
372 | if (!is_fsync_dnode(page)) | |
373 | goto next; | |
374 | ||
375 | entry = get_fsync_inode(head, ino_of_node(page)); | |
d47b8715 | 376 | if (!entry) { |
4b2414d0 CY |
377 | bool quota_inode = false; |
378 | ||
d40d30c5 JK |
379 | if (!check_only && |
380 | IS_INODE(page) && is_dent_dnode(page)) { | |
4d57b86d | 381 | err = f2fs_recover_inode_page(sbi, page); |
98838579 CY |
382 | if (err) { |
383 | f2fs_put_page(page, 1); | |
f356fe0c | 384 | break; |
98838579 | 385 | } |
4b2414d0 | 386 | quota_inode = true; |
d624c96f JK |
387 | } |
388 | ||
441ac5cb JK |
389 | /* |
390 | * CP | dnode(F) | inode(DF) | |
391 | * For this case, we should not give up now. | |
392 | */ | |
4b2414d0 CY |
393 | entry = add_fsync_inode(sbi, head, ino_of_node(page), |
394 | quota_inode); | |
f4702d61 JK |
395 | if (IS_ERR(entry)) { |
396 | err = PTR_ERR(entry); | |
8fbc418f JK |
397 | if (err == -ENOENT) { |
398 | err = 0; | |
441ac5cb | 399 | goto next; |
8fbc418f | 400 | } |
98838579 | 401 | f2fs_put_page(page, 1); |
f356fe0c | 402 | break; |
d624c96f | 403 | } |
d624c96f | 404 | } |
addbe45b JK |
405 | entry->blkaddr = blkaddr; |
406 | ||
608514de JK |
407 | if (IS_INODE(page) && is_dent_dnode(page)) |
408 | entry->last_dentry = blkaddr; | |
d624c96f | 409 | next: |
fb0e72c8 CY |
410 | /* sanity check in order to detect looped node chain */ |
411 | if (++loop_cnt >= free_blocks || | |
412 | blkaddr == next_blkaddr_of_node(page)) { | |
dcbb4c10 JP |
413 | f2fs_notice(sbi, "%s: detect looped node chain, blkaddr:%u, next:%u", |
414 | __func__, blkaddr, | |
415 | next_blkaddr_of_node(page)); | |
98838579 | 416 | f2fs_put_page(page, 1); |
fb0e72c8 CY |
417 | err = -EINVAL; |
418 | break; | |
419 | } | |
420 | ||
d624c96f JK |
421 | /* check next segment */ |
422 | blkaddr = next_blkaddr_of_node(page); | |
4c521f49 | 423 | f2fs_put_page(page, 1); |
635aee1f | 424 | |
4d57b86d | 425 | f2fs_ra_meta_pages_cond(sbi, blkaddr); |
d624c96f | 426 | } |
d624c96f JK |
427 | return err; |
428 | } | |
429 | ||
26b5a079 | 430 | static void destroy_fsync_dnodes(struct list_head *head, int drop) |
d624c96f | 431 | { |
d8b79b2f DC |
432 | struct fsync_inode_entry *entry, *tmp; |
433 | ||
3f8ab270 | 434 | list_for_each_entry_safe(entry, tmp, head, list) |
26b5a079 | 435 | del_fsync_inode(entry, drop); |
d624c96f JK |
436 | } |
437 | ||
39cf72cf | 438 | static int check_index_in_prev_nodes(struct f2fs_sb_info *sbi, |
b292dcab | 439 | block_t blkaddr, struct dnode_of_data *dn) |
d624c96f JK |
440 | { |
441 | struct seg_entry *sentry; | |
442 | unsigned int segno = GET_SEGNO(sbi, blkaddr); | |
491c0854 | 443 | unsigned short blkoff = GET_BLKOFF_FROM_SEG0(sbi, blkaddr); |
f6517cfc | 444 | struct f2fs_summary_block *sum_node; |
d624c96f | 445 | struct f2fs_summary sum; |
f6517cfc | 446 | struct page *sum_page, *node_page; |
c9ef4810 | 447 | struct dnode_of_data tdn = *dn; |
b292dcab | 448 | nid_t ino, nid; |
d624c96f | 449 | struct inode *inode; |
de93653f | 450 | unsigned int offset; |
d624c96f JK |
451 | block_t bidx; |
452 | int i; | |
453 | ||
454 | sentry = get_seg_entry(sbi, segno); | |
455 | if (!f2fs_test_bit(blkoff, sentry->cur_valid_map)) | |
39cf72cf | 456 | return 0; |
d624c96f JK |
457 | |
458 | /* Get the previous summary */ | |
125c9fb1 | 459 | for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) { |
d624c96f JK |
460 | struct curseg_info *curseg = CURSEG_I(sbi, i); |
461 | if (curseg->segno == segno) { | |
462 | sum = curseg->sum_blk->entries[blkoff]; | |
f6517cfc | 463 | goto got_it; |
d624c96f JK |
464 | } |
465 | } | |
d624c96f | 466 | |
4d57b86d | 467 | sum_page = f2fs_get_sum_page(sbi, segno); |
edc55aaf JK |
468 | if (IS_ERR(sum_page)) |
469 | return PTR_ERR(sum_page); | |
f6517cfc JK |
470 | sum_node = (struct f2fs_summary_block *)page_address(sum_page); |
471 | sum = sum_node->entries[blkoff]; | |
472 | f2fs_put_page(sum_page, 1); | |
473 | got_it: | |
b292dcab JK |
474 | /* Use the locked dnode page and inode */ |
475 | nid = le32_to_cpu(sum.nid); | |
476 | if (dn->inode->i_ino == nid) { | |
b292dcab | 477 | tdn.nid = nid; |
c9ef4810 JK |
478 | if (!dn->inode_page_locked) |
479 | lock_page(dn->inode_page); | |
b292dcab | 480 | tdn.node_page = dn->inode_page; |
060dd67b | 481 | tdn.ofs_in_node = le16_to_cpu(sum.ofs_in_node); |
c9ef4810 | 482 | goto truncate_out; |
b292dcab | 483 | } else if (dn->nid == nid) { |
060dd67b | 484 | tdn.ofs_in_node = le16_to_cpu(sum.ofs_in_node); |
c9ef4810 | 485 | goto truncate_out; |
b292dcab JK |
486 | } |
487 | ||
d624c96f | 488 | /* Get the node page */ |
4d57b86d | 489 | node_page = f2fs_get_node_page(sbi, nid); |
39cf72cf JK |
490 | if (IS_ERR(node_page)) |
491 | return PTR_ERR(node_page); | |
de93653f JK |
492 | |
493 | offset = ofs_of_node(node_page); | |
d624c96f JK |
494 | ino = ino_of_node(node_page); |
495 | f2fs_put_page(node_page, 1); | |
496 | ||
60979115 | 497 | if (ino != dn->inode->i_ino) { |
4b2414d0 CY |
498 | int ret; |
499 | ||
60979115 | 500 | /* Deallocate previous index in the node page */ |
e8ea9b3d | 501 | inode = f2fs_iget_retry(sbi->sb, ino); |
60979115 JK |
502 | if (IS_ERR(inode)) |
503 | return PTR_ERR(inode); | |
4b2414d0 CY |
504 | |
505 | ret = dquot_initialize(inode); | |
506 | if (ret) { | |
507 | iput(inode); | |
508 | return ret; | |
509 | } | |
60979115 JK |
510 | } else { |
511 | inode = dn->inode; | |
512 | } | |
06025f4d | 513 | |
4d57b86d CY |
514 | bidx = f2fs_start_bidx_of_node(offset, inode) + |
515 | le16_to_cpu(sum.ofs_in_node); | |
de93653f | 516 | |
c9ef4810 JK |
517 | /* |
518 | * if inode page is locked, unlock temporarily, but its reference | |
519 | * count keeps alive. | |
520 | */ | |
521 | if (ino == dn->inode->i_ino && dn->inode_page_locked) | |
522 | unlock_page(dn->inode_page); | |
523 | ||
524 | set_new_dnode(&tdn, inode, NULL, NULL, 0); | |
4d57b86d | 525 | if (f2fs_get_dnode_of_data(&tdn, bidx, LOOKUP_NODE)) |
c9ef4810 JK |
526 | goto out; |
527 | ||
528 | if (tdn.data_blkaddr == blkaddr) | |
4d57b86d | 529 | f2fs_truncate_data_blocks_range(&tdn, 1); |
c9ef4810 JK |
530 | |
531 | f2fs_put_dnode(&tdn); | |
532 | out: | |
533 | if (ino != dn->inode->i_ino) | |
60979115 | 534 | iput(inode); |
c9ef4810 JK |
535 | else if (dn->inode_page_locked) |
536 | lock_page(dn->inode_page); | |
537 | return 0; | |
538 | ||
539 | truncate_out: | |
a2ced1ce | 540 | if (f2fs_data_blkaddr(&tdn) == blkaddr) |
4d57b86d | 541 | f2fs_truncate_data_blocks_range(&tdn, 1); |
c9ef4810 JK |
542 | if (dn->inode->i_ino == nid && !dn->inode_page_locked) |
543 | unlock_page(dn->inode_page); | |
39cf72cf | 544 | return 0; |
d624c96f JK |
545 | } |
546 | ||
6ead1142 | 547 | static int do_recover_data(struct f2fs_sb_info *sbi, struct inode *inode, |
e17d488b | 548 | struct page *page) |
d624c96f | 549 | { |
d624c96f | 550 | struct dnode_of_data dn; |
d624c96f | 551 | struct node_info ni; |
81ca7350 | 552 | unsigned int start, end; |
f356fe0c | 553 | int err = 0, recovered = 0; |
d624c96f | 554 | |
1c35a90e JK |
555 | /* step 1: recover xattr */ |
556 | if (IS_INODE(page)) { | |
9627a7b3 CY |
557 | err = f2fs_recover_inline_xattr(inode, page); |
558 | if (err) | |
559 | goto out; | |
1c35a90e | 560 | } else if (f2fs_has_xattr_block(ofs_of_node(page))) { |
4d57b86d | 561 | err = f2fs_recover_xattr_data(inode, page); |
d260081c CY |
562 | if (!err) |
563 | recovered++; | |
1e1bb4ba | 564 | goto out; |
1c35a90e | 565 | } |
1e1bb4ba | 566 | |
1c35a90e | 567 | /* step 2: recover inline data */ |
9627a7b3 CY |
568 | err = f2fs_recover_inline_data(inode, page); |
569 | if (err) { | |
570 | if (err == 1) | |
571 | err = 0; | |
abb2366c | 572 | goto out; |
9627a7b3 | 573 | } |
abb2366c | 574 | |
1c35a90e | 575 | /* step 3: recover data indices */ |
4d57b86d | 576 | start = f2fs_start_bidx_of_node(ofs_of_node(page), inode); |
81ca7350 | 577 | end = start + ADDRS_PER_PAGE(page, inode); |
d624c96f JK |
578 | |
579 | set_new_dnode(&dn, inode, NULL, NULL, 0); | |
e8ea9b3d | 580 | retry_dn: |
4d57b86d | 581 | err = f2fs_get_dnode_of_data(&dn, start, ALLOC_NODE); |
e8ea9b3d JK |
582 | if (err) { |
583 | if (err == -ENOMEM) { | |
5df7731f | 584 | congestion_wait(BLK_RW_ASYNC, DEFAULT_IO_TIMEOUT); |
e8ea9b3d JK |
585 | goto retry_dn; |
586 | } | |
1e1bb4ba | 587 | goto out; |
e8ea9b3d | 588 | } |
d624c96f | 589 | |
bae0ee7a | 590 | f2fs_wait_on_page_writeback(dn.node_page, NODE, true, true); |
d624c96f | 591 | |
7735730d CY |
592 | err = f2fs_get_node_info(sbi, dn.nid, &ni); |
593 | if (err) | |
594 | goto err; | |
595 | ||
9850cf4a | 596 | f2fs_bug_on(sbi, ni.ino != ino_of_node(page)); |
22d61e28 CY |
597 | |
598 | if (ofs_of_node(dn.node_page) != ofs_of_node(page)) { | |
dcbb4c10 JP |
599 | f2fs_warn(sbi, "Inconsistent ofs_of_node, ino:%lu, ofs:%u, %u", |
600 | inode->i_ino, ofs_of_node(dn.node_page), | |
601 | ofs_of_node(page)); | |
10f966bb | 602 | err = -EFSCORRUPTED; |
22d61e28 CY |
603 | goto err; |
604 | } | |
d624c96f | 605 | |
12a8343e | 606 | for (; start < end; start++, dn.ofs_in_node++) { |
d624c96f JK |
607 | block_t src, dest; |
608 | ||
a2ced1ce CY |
609 | src = f2fs_data_blkaddr(&dn); |
610 | dest = data_blkaddr(dn.inode, page, dn.ofs_in_node); | |
d624c96f | 611 | |
93770ab7 CY |
612 | if (__is_valid_data_blkaddr(src) && |
613 | !f2fs_is_valid_blkaddr(sbi, src, META_POR)) { | |
10f966bb | 614 | err = -EFSCORRUPTED; |
93770ab7 CY |
615 | goto err; |
616 | } | |
617 | ||
618 | if (__is_valid_data_blkaddr(dest) && | |
619 | !f2fs_is_valid_blkaddr(sbi, dest, META_POR)) { | |
10f966bb | 620 | err = -EFSCORRUPTED; |
93770ab7 CY |
621 | goto err; |
622 | } | |
623 | ||
12a8343e CY |
624 | /* skip recovering if dest is the same as src */ |
625 | if (src == dest) | |
626 | continue; | |
627 | ||
628 | /* dest is invalid, just invalidate src block */ | |
629 | if (dest == NULL_ADDR) { | |
4d57b86d | 630 | f2fs_truncate_data_blocks_range(&dn, 1); |
12a8343e CY |
631 | continue; |
632 | } | |
633 | ||
26787236 | 634 | if (!file_keep_isize(inode) && |
dba79f38 CY |
635 | (i_size_read(inode) <= ((loff_t)start << PAGE_SHIFT))) |
636 | f2fs_i_size_write(inode, | |
637 | (loff_t)(start + 1) << PAGE_SHIFT); | |
26de9b11 | 638 | |
12a8343e CY |
639 | /* |
640 | * dest is reserved block, invalidate src block | |
641 | * and then reserve one new block in dnode page. | |
642 | */ | |
643 | if (dest == NEW_ADDR) { | |
4d57b86d CY |
644 | f2fs_truncate_data_blocks_range(&dn, 1); |
645 | f2fs_reserve_new_block(&dn); | |
12a8343e CY |
646 | continue; |
647 | } | |
648 | ||
649 | /* dest is valid block, try to recover from src to dest */ | |
e1da7872 | 650 | if (f2fs_is_valid_blkaddr(sbi, dest, META_POR)) { |
e03b07d9 | 651 | |
d624c96f | 652 | if (src == NULL_ADDR) { |
4d57b86d | 653 | err = f2fs_reserve_new_block(&dn); |
7fa750a1 AB |
654 | while (err && |
655 | IS_ENABLED(CONFIG_F2FS_FAULT_INJECTION)) | |
4d57b86d | 656 | err = f2fs_reserve_new_block(&dn); |
d624c96f | 657 | /* We should not get -ENOSPC */ |
9850cf4a | 658 | f2fs_bug_on(sbi, err); |
6f3ec995 JK |
659 | if (err) |
660 | goto err; | |
d624c96f | 661 | } |
e8ea9b3d | 662 | retry_prev: |
d624c96f | 663 | /* Check the previous node page having this index */ |
39cf72cf | 664 | err = check_index_in_prev_nodes(sbi, dest, &dn); |
e8ea9b3d JK |
665 | if (err) { |
666 | if (err == -ENOMEM) { | |
5df7731f CY |
667 | congestion_wait(BLK_RW_ASYNC, |
668 | DEFAULT_IO_TIMEOUT); | |
e8ea9b3d JK |
669 | goto retry_prev; |
670 | } | |
39cf72cf | 671 | goto err; |
e8ea9b3d | 672 | } |
d624c96f | 673 | |
d624c96f | 674 | /* write dummy data page */ |
528e3459 | 675 | f2fs_replace_block(sbi, &dn, src, dest, |
28bc106b | 676 | ni.version, false, false); |
f356fe0c | 677 | recovered++; |
d624c96f | 678 | } |
d624c96f JK |
679 | } |
680 | ||
d624c96f JK |
681 | copy_node_footer(dn.node_page, page); |
682 | fill_node_footer(dn.node_page, dn.nid, ni.ino, | |
683 | ofs_of_node(page), false); | |
684 | set_page_dirty(dn.node_page); | |
39cf72cf | 685 | err: |
d624c96f | 686 | f2fs_put_dnode(&dn); |
1e1bb4ba | 687 | out: |
dcbb4c10 JP |
688 | f2fs_notice(sbi, "recover_data: ino = %lx (i_size: %s) recovered = %d, err = %d", |
689 | inode->i_ino, file_keep_isize(inode) ? "keep" : "recover", | |
690 | recovered, err); | |
39cf72cf | 691 | return err; |
d624c96f JK |
692 | } |
693 | ||
f61cce5b | 694 | static int recover_data(struct f2fs_sb_info *sbi, struct list_head *inode_list, |
26b5a079 | 695 | struct list_head *tmp_inode_list, struct list_head *dir_list) |
d624c96f | 696 | { |
d624c96f | 697 | struct curseg_info *curseg; |
4c521f49 | 698 | struct page *page = NULL; |
6ead1142 | 699 | int err = 0; |
d624c96f JK |
700 | block_t blkaddr; |
701 | ||
702 | /* get node pages in the current segment */ | |
b7973f23 | 703 | curseg = CURSEG_I(sbi, CURSEG_WARM_NODE); |
d624c96f JK |
704 | blkaddr = NEXT_FREE_BLKADDR(sbi, curseg); |
705 | ||
d624c96f JK |
706 | while (1) { |
707 | struct fsync_inode_entry *entry; | |
708 | ||
e1da7872 | 709 | if (!f2fs_is_valid_blkaddr(sbi, blkaddr, META_POR)) |
4c521f49 | 710 | break; |
d624c96f | 711 | |
4d57b86d | 712 | f2fs_ra_meta_pages_cond(sbi, blkaddr); |
635aee1f | 713 | |
4d57b86d | 714 | page = f2fs_get_tmp_page(sbi, blkaddr); |
7735730d CY |
715 | if (IS_ERR(page)) { |
716 | err = PTR_ERR(page); | |
717 | break; | |
718 | } | |
393ff91f | 719 | |
a468f0ef | 720 | if (!is_recoverable_dnode(page)) { |
4c521f49 | 721 | f2fs_put_page(page, 1); |
45856aff | 722 | break; |
4c521f49 | 723 | } |
d624c96f | 724 | |
f61cce5b | 725 | entry = get_fsync_inode(inode_list, ino_of_node(page)); |
d624c96f JK |
726 | if (!entry) |
727 | goto next; | |
441ac5cb JK |
728 | /* |
729 | * inode(x) | CP | inode(x) | dnode(F) | |
730 | * In this case, we can lose the latest inode(x). | |
c52e1b10 | 731 | * So, call recover_inode for the inode update. |
441ac5cb | 732 | */ |
af033b2a CY |
733 | if (IS_INODE(page)) { |
734 | err = recover_inode(entry->inode, page); | |
98838579 CY |
735 | if (err) { |
736 | f2fs_put_page(page, 1); | |
af033b2a | 737 | break; |
98838579 | 738 | } |
af033b2a | 739 | } |
c52e1b10 | 740 | if (entry->last_dentry == blkaddr) { |
f61cce5b | 741 | err = recover_dentry(entry->inode, page, dir_list); |
c52e1b10 JK |
742 | if (err) { |
743 | f2fs_put_page(page, 1); | |
744 | break; | |
745 | } | |
746 | } | |
e17d488b | 747 | err = do_recover_data(sbi, entry->inode, page); |
4c521f49 JK |
748 | if (err) { |
749 | f2fs_put_page(page, 1); | |
45856aff | 750 | break; |
4c521f49 | 751 | } |
d624c96f | 752 | |
3f8ab270 | 753 | if (entry->blkaddr == blkaddr) |
26b5a079 | 754 | list_move_tail(&entry->list, tmp_inode_list); |
d624c96f JK |
755 | next: |
756 | /* check next segment */ | |
757 | blkaddr = next_blkaddr_of_node(page); | |
4c521f49 | 758 | f2fs_put_page(page, 1); |
d624c96f | 759 | } |
6ead1142 | 760 | if (!err) |
901d745f | 761 | f2fs_allocate_new_segments(sbi); |
6ead1142 | 762 | return err; |
d624c96f JK |
763 | } |
764 | ||
4d57b86d | 765 | int f2fs_recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only) |
d624c96f | 766 | { |
26b5a079 | 767 | struct list_head inode_list, tmp_inode_list; |
f61cce5b | 768 | struct list_head dir_list; |
6ead1142 | 769 | int err; |
6781eabb | 770 | int ret = 0; |
4b2414d0 | 771 | unsigned long s_flags = sbi->sb->s_flags; |
aabe5136 | 772 | bool need_writecp = false; |
c426d991 | 773 | bool fix_curseg_write_pointer = false; |
ea676733 JK |
774 | #ifdef CONFIG_QUOTA |
775 | int quota_enabled; | |
776 | #endif | |
d624c96f | 777 | |
1751e8a6 | 778 | if (s_flags & SB_RDONLY) { |
dcbb4c10 | 779 | f2fs_info(sbi, "recover fsync data on readonly fs"); |
1751e8a6 | 780 | sbi->sb->s_flags &= ~SB_RDONLY; |
4b2414d0 CY |
781 | } |
782 | ||
783 | #ifdef CONFIG_QUOTA | |
784 | /* Needed for iput() to work correctly and not trash data */ | |
1751e8a6 | 785 | sbi->sb->s_flags |= SB_ACTIVE; |
4b2414d0 | 786 | /* Turn on quotas so that they are updated correctly */ |
1751e8a6 | 787 | quota_enabled = f2fs_enable_quota_files(sbi, s_flags & SB_RDONLY); |
4b2414d0 CY |
788 | #endif |
789 | ||
d624c96f | 790 | fsync_entry_slab = f2fs_kmem_cache_create("f2fs_fsync_inode_entry", |
e8512d2e | 791 | sizeof(struct fsync_inode_entry)); |
4b2414d0 CY |
792 | if (!fsync_entry_slab) { |
793 | err = -ENOMEM; | |
794 | goto out; | |
795 | } | |
d624c96f JK |
796 | |
797 | INIT_LIST_HEAD(&inode_list); | |
26b5a079 | 798 | INIT_LIST_HEAD(&tmp_inode_list); |
f61cce5b | 799 | INIT_LIST_HEAD(&dir_list); |
d624c96f | 800 | |
14f4e690 | 801 | /* prevent checkpoint */ |
8769918b | 802 | down_write(&sbi->cp_global_sem); |
14f4e690 | 803 | |
315df839 | 804 | /* step #1: find fsynced inode numbers */ |
d40d30c5 | 805 | err = find_fsync_dnodes(sbi, &inode_list, check_only); |
6781eabb | 806 | if (err || list_empty(&inode_list)) |
4b2414d0 | 807 | goto skip; |
d624c96f | 808 | |
6781eabb JK |
809 | if (check_only) { |
810 | ret = 1; | |
4b2414d0 | 811 | goto skip; |
6781eabb | 812 | } |
d624c96f | 813 | |
aabe5136 | 814 | need_writecp = true; |
691c6fd2 | 815 | |
d624c96f | 816 | /* step #2: recover data */ |
26b5a079 | 817 | err = recover_data(sbi, &inode_list, &tmp_inode_list, &dir_list); |
b307384e | 818 | if (!err) |
9850cf4a | 819 | f2fs_bug_on(sbi, !list_empty(&inode_list)); |
26b5a079 SY |
820 | else { |
821 | /* restore s_flags to let iput() trash data */ | |
822 | sbi->sb->s_flags = s_flags; | |
823 | } | |
4b2414d0 | 824 | skip: |
c426d991 SK |
825 | fix_curseg_write_pointer = !check_only || list_empty(&inode_list); |
826 | ||
26b5a079 SY |
827 | destroy_fsync_dnodes(&inode_list, err); |
828 | destroy_fsync_dnodes(&tmp_inode_list, err); | |
cf2271e7 | 829 | |
4c521f49 JK |
830 | /* truncate meta pages to be used by the recovery */ |
831 | truncate_inode_pages_range(META_MAPPING(sbi), | |
09cbfeaf | 832 | (loff_t)MAIN_BLKADDR(sbi) << PAGE_SHIFT, -1); |
4c521f49 | 833 | |
cf2271e7 JK |
834 | if (err) { |
835 | truncate_inode_pages_final(NODE_MAPPING(sbi)); | |
836 | truncate_inode_pages_final(META_MAPPING(sbi)); | |
837 | } | |
c426d991 SK |
838 | |
839 | /* | |
840 | * If fsync data succeeds or there is no fsync data to recover, | |
841 | * and the f2fs is not read only, check and fix zoned block devices' | |
842 | * write pointer consistency. | |
843 | */ | |
844 | if (!err && fix_curseg_write_pointer && !f2fs_readonly(sbi->sb) && | |
845 | f2fs_sb_has_blkzoned(sbi)) { | |
846 | err = f2fs_fix_curseg_write_pointer(sbi); | |
847 | ret = err; | |
848 | } | |
849 | ||
850 | if (!err) | |
851 | clear_sbi_flag(sbi, SBI_POR_DOING); | |
852 | ||
8769918b | 853 | up_write(&sbi->cp_global_sem); |
a468f0ef | 854 | |
9e1e6df4 | 855 | /* let's drop all the directory inodes for clean checkpoint */ |
26b5a079 | 856 | destroy_fsync_dnodes(&dir_list, err); |
9e1e6df4 | 857 | |
1378752b CY |
858 | if (need_writecp) { |
859 | set_sbi_flag(sbi, SBI_IS_RECOVERED); | |
860 | ||
861 | if (!err) { | |
862 | struct cp_control cpc = { | |
863 | .reason = CP_RECOVERY, | |
864 | }; | |
865 | err = f2fs_write_checkpoint(sbi, &cpc); | |
866 | } | |
cf2271e7 | 867 | } |
f61cce5b | 868 | |
f61cce5b | 869 | kmem_cache_destroy(fsync_entry_slab); |
4b2414d0 CY |
870 | out: |
871 | #ifdef CONFIG_QUOTA | |
872 | /* Turn quotas off */ | |
ea676733 JK |
873 | if (quota_enabled) |
874 | f2fs_quota_off_umount(sbi->sb); | |
4b2414d0 | 875 | #endif |
1751e8a6 | 876 | sbi->sb->s_flags = s_flags; /* Restore SB_RDONLY status */ |
4b2414d0 | 877 | |
6781eabb | 878 | return ret ? ret: err; |
d624c96f | 879 | } |