]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/f2fs/recovery.c
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
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.
12 #include <linux/f2fs_fs.h>
17 static struct kmem_cache
*fsync_entry_slab
;
19 bool space_for_roll_forward(struct f2fs_sb_info
*sbi
)
21 if (sbi
->last_valid_block_count
+ sbi
->alloc_valid_block_count
22 > sbi
->user_block_count
)
27 static struct fsync_inode_entry
*get_fsync_inode(struct list_head
*head
,
30 struct list_head
*this;
31 struct fsync_inode_entry
*entry
;
33 list_for_each(this, head
) {
34 entry
= list_entry(this, struct fsync_inode_entry
, list
);
35 if (entry
->inode
->i_ino
== ino
)
41 static int recover_dentry(struct page
*ipage
, struct inode
*inode
)
43 struct f2fs_node
*raw_node
= (struct f2fs_node
*)kmap(ipage
);
44 struct f2fs_inode
*raw_inode
= &(raw_node
->i
);
45 struct dentry dent
, parent
;
46 struct f2fs_dir_entry
*de
;
51 if (!is_dent_dnode(ipage
))
54 dir
= f2fs_iget(inode
->i_sb
, le32_to_cpu(raw_inode
->i_pino
));
61 dent
.d_parent
= &parent
;
62 dent
.d_name
.len
= le32_to_cpu(raw_inode
->i_namelen
);
63 dent
.d_name
.name
= raw_inode
->i_name
;
65 de
= f2fs_find_entry(dir
, &dent
.d_name
, &page
);
68 f2fs_put_page(page
, 0);
70 err
= f2fs_add_link(&dent
, inode
);
78 static int recover_inode(struct inode
*inode
, struct page
*node_page
)
80 void *kaddr
= page_address(node_page
);
81 struct f2fs_node
*raw_node
= (struct f2fs_node
*)kaddr
;
82 struct f2fs_inode
*raw_inode
= &(raw_node
->i
);
84 inode
->i_mode
= le16_to_cpu(raw_inode
->i_mode
);
85 i_size_write(inode
, le64_to_cpu(raw_inode
->i_size
));
86 inode
->i_atime
.tv_sec
= le64_to_cpu(raw_inode
->i_mtime
);
87 inode
->i_ctime
.tv_sec
= le64_to_cpu(raw_inode
->i_ctime
);
88 inode
->i_mtime
.tv_sec
= le64_to_cpu(raw_inode
->i_mtime
);
89 inode
->i_atime
.tv_nsec
= le32_to_cpu(raw_inode
->i_mtime_nsec
);
90 inode
->i_ctime
.tv_nsec
= le32_to_cpu(raw_inode
->i_ctime_nsec
);
91 inode
->i_mtime
.tv_nsec
= le32_to_cpu(raw_inode
->i_mtime_nsec
);
93 return recover_dentry(node_page
, inode
);
96 static int find_fsync_dnodes(struct f2fs_sb_info
*sbi
, struct list_head
*head
)
98 unsigned long long cp_ver
= le64_to_cpu(sbi
->ckpt
->checkpoint_ver
);
99 struct curseg_info
*curseg
;
104 /* get node pages in the current segment */
105 curseg
= CURSEG_I(sbi
, CURSEG_WARM_NODE
);
106 blkaddr
= START_BLOCK(sbi
, curseg
->segno
) + curseg
->next_blkoff
;
109 page
= alloc_page(GFP_F2FS_ZERO
);
111 return PTR_ERR(page
);
115 struct fsync_inode_entry
*entry
;
117 if (f2fs_readpage(sbi
, page
, blkaddr
, READ_SYNC
))
120 if (cp_ver
!= cpver_of_node(page
))
123 if (!is_fsync_dnode(page
))
126 entry
= get_fsync_inode(head
, ino_of_node(page
));
128 entry
->blkaddr
= blkaddr
;
129 if (IS_INODE(page
) && is_dent_dnode(page
))
130 set_inode_flag(F2FS_I(entry
->inode
),
133 if (IS_INODE(page
) && is_dent_dnode(page
)) {
134 if (recover_inode_page(sbi
, page
)) {
140 /* add this fsync inode to the list */
141 entry
= kmem_cache_alloc(fsync_entry_slab
, GFP_NOFS
);
147 entry
->inode
= f2fs_iget(sbi
->sb
, ino_of_node(page
));
148 if (IS_ERR(entry
->inode
)) {
149 err
= PTR_ERR(entry
->inode
);
150 kmem_cache_free(fsync_entry_slab
, entry
);
154 list_add_tail(&entry
->list
, head
);
155 entry
->blkaddr
= blkaddr
;
157 if (IS_INODE(page
)) {
158 err
= recover_inode(entry
->inode
, page
);
163 /* check next segment */
164 blkaddr
= next_blkaddr_of_node(page
);
165 ClearPageUptodate(page
);
169 __free_pages(page
, 0);
173 static void destroy_fsync_dnodes(struct f2fs_sb_info
*sbi
,
174 struct list_head
*head
)
176 struct fsync_inode_entry
*entry
, *tmp
;
178 list_for_each_entry_safe(entry
, tmp
, head
, list
) {
180 list_del(&entry
->list
);
181 kmem_cache_free(fsync_entry_slab
, entry
);
185 static void check_index_in_prev_nodes(struct f2fs_sb_info
*sbi
,
188 struct seg_entry
*sentry
;
189 unsigned int segno
= GET_SEGNO(sbi
, blkaddr
);
190 unsigned short blkoff
= GET_SEGOFF_FROM_SEG0(sbi
, blkaddr
) &
191 (sbi
->blocks_per_seg
- 1);
192 struct f2fs_summary sum
;
196 struct page
*node_page
;
200 sentry
= get_seg_entry(sbi
, segno
);
201 if (!f2fs_test_bit(blkoff
, sentry
->cur_valid_map
))
204 /* Get the previous summary */
205 for (i
= CURSEG_WARM_DATA
; i
<= CURSEG_COLD_DATA
; i
++) {
206 struct curseg_info
*curseg
= CURSEG_I(sbi
, i
);
207 if (curseg
->segno
== segno
) {
208 sum
= curseg
->sum_blk
->entries
[blkoff
];
212 if (i
> CURSEG_COLD_DATA
) {
213 struct page
*sum_page
= get_sum_page(sbi
, segno
);
214 struct f2fs_summary_block
*sum_node
;
215 kaddr
= page_address(sum_page
);
216 sum_node
= (struct f2fs_summary_block
*)kaddr
;
217 sum
= sum_node
->entries
[blkoff
];
218 f2fs_put_page(sum_page
, 1);
221 /* Get the node page */
222 node_page
= get_node_page(sbi
, le32_to_cpu(sum
.nid
));
223 bidx
= start_bidx_of_node(ofs_of_node(node_page
)) +
224 le16_to_cpu(sum
.ofs_in_node
);
225 ino
= ino_of_node(node_page
);
226 f2fs_put_page(node_page
, 1);
228 /* Deallocate previous index in the node page */
229 inode
= f2fs_iget_nowait(sbi
->sb
, ino
);
233 truncate_hole(inode
, bidx
, bidx
+ 1);
237 static void do_recover_data(struct f2fs_sb_info
*sbi
, struct inode
*inode
,
238 struct page
*page
, block_t blkaddr
)
240 unsigned int start
, end
;
241 struct dnode_of_data dn
;
242 struct f2fs_summary sum
;
245 start
= start_bidx_of_node(ofs_of_node(page
));
247 end
= start
+ ADDRS_PER_INODE
;
249 end
= start
+ ADDRS_PER_BLOCK
;
251 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
252 if (get_dnode_of_data(&dn
, start
, 0))
255 wait_on_page_writeback(dn
.node_page
);
257 get_node_info(sbi
, dn
.nid
, &ni
);
258 BUG_ON(ni
.ino
!= ino_of_node(page
));
259 BUG_ON(ofs_of_node(dn
.node_page
) != ofs_of_node(page
));
261 for (; start
< end
; start
++) {
264 src
= datablock_addr(dn
.node_page
, dn
.ofs_in_node
);
265 dest
= datablock_addr(page
, dn
.ofs_in_node
);
267 if (src
!= dest
&& dest
!= NEW_ADDR
&& dest
!= NULL_ADDR
) {
268 if (src
== NULL_ADDR
) {
269 int err
= reserve_new_block(&dn
);
270 /* We should not get -ENOSPC */
274 /* Check the previous node page having this index */
275 check_index_in_prev_nodes(sbi
, dest
);
277 set_summary(&sum
, dn
.nid
, dn
.ofs_in_node
, ni
.version
);
279 /* write dummy data page */
280 recover_data_page(sbi
, NULL
, &sum
, src
, dest
);
281 update_extent_cache(dest
, &dn
);
286 /* write node page in place */
287 set_summary(&sum
, dn
.nid
, 0, 0);
288 if (IS_INODE(dn
.node_page
))
289 sync_inode_page(&dn
);
291 copy_node_footer(dn
.node_page
, page
);
292 fill_node_footer(dn
.node_page
, dn
.nid
, ni
.ino
,
293 ofs_of_node(page
), false);
294 set_page_dirty(dn
.node_page
);
296 recover_node_page(sbi
, dn
.node_page
, &sum
, &ni
, blkaddr
);
300 static void recover_data(struct f2fs_sb_info
*sbi
,
301 struct list_head
*head
, int type
)
303 unsigned long long cp_ver
= le64_to_cpu(sbi
->ckpt
->checkpoint_ver
);
304 struct curseg_info
*curseg
;
308 /* get node pages in the current segment */
309 curseg
= CURSEG_I(sbi
, type
);
310 blkaddr
= NEXT_FREE_BLKADDR(sbi
, curseg
);
313 page
= alloc_page(GFP_NOFS
| __GFP_ZERO
);
319 struct fsync_inode_entry
*entry
;
321 if (f2fs_readpage(sbi
, page
, blkaddr
, READ_SYNC
))
324 if (cp_ver
!= cpver_of_node(page
))
327 entry
= get_fsync_inode(head
, ino_of_node(page
));
331 do_recover_data(sbi
, entry
->inode
, page
, blkaddr
);
333 if (entry
->blkaddr
== blkaddr
) {
335 list_del(&entry
->list
);
336 kmem_cache_free(fsync_entry_slab
, entry
);
339 /* check next segment */
340 blkaddr
= next_blkaddr_of_node(page
);
341 ClearPageUptodate(page
);
345 __free_pages(page
, 0);
347 allocate_new_segments(sbi
);
350 void recover_fsync_data(struct f2fs_sb_info
*sbi
)
352 struct list_head inode_list
;
354 fsync_entry_slab
= f2fs_kmem_cache_create("f2fs_fsync_inode_entry",
355 sizeof(struct fsync_inode_entry
), NULL
);
356 if (unlikely(!fsync_entry_slab
))
359 INIT_LIST_HEAD(&inode_list
);
361 /* step #1: find fsynced inode numbers */
362 if (find_fsync_dnodes(sbi
, &inode_list
))
365 if (list_empty(&inode_list
))
368 /* step #2: recover data */
370 recover_data(sbi
, &inode_list
, CURSEG_WARM_NODE
);
372 BUG_ON(!list_empty(&inode_list
));
374 destroy_fsync_dnodes(sbi
, &inode_list
);
375 kmem_cache_destroy(fsync_entry_slab
);
376 write_checkpoint(sbi
, false, false);