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_inode
*raw_inode
= F2FS_INODE(ipage
);
44 nid_t pino
= le32_to_cpu(raw_inode
->i_pino
);
45 struct f2fs_dir_entry
*de
;
48 struct inode
*dir
, *einode
;
51 dir
= check_dirty_dir_inode(F2FS_SB(inode
->i_sb
), pino
);
53 dir
= f2fs_iget(inode
->i_sb
, pino
);
58 set_inode_flag(F2FS_I(dir
), FI_DELAY_IPUT
);
59 add_dirty_dir_inode(dir
);
62 name
.len
= le32_to_cpu(raw_inode
->i_namelen
);
63 name
.name
= raw_inode
->i_name
;
65 if (unlikely(name
.len
> F2FS_NAME_LEN
)) {
71 de
= f2fs_find_entry(dir
, &name
, &page
);
72 if (de
&& inode
->i_ino
== le32_to_cpu(de
->ino
))
75 einode
= f2fs_iget(inode
->i_sb
, le32_to_cpu(de
->ino
));
78 if (PTR_ERR(einode
) == -ENOENT
)
82 err
= acquire_orphan_inode(F2FS_SB(inode
->i_sb
));
87 f2fs_delete_entry(de
, page
, einode
);
91 err
= __f2fs_add_link(dir
, &name
, inode
);
96 f2fs_put_page(page
, 0);
98 f2fs_msg(inode
->i_sb
, KERN_NOTICE
,
99 "%s: ino = %x, name = %s, dir = %lx, err = %d",
100 __func__
, ino_of_node(ipage
), raw_inode
->i_name
,
101 IS_ERR(dir
) ? 0 : dir
->i_ino
, err
);
105 static int recover_inode(struct inode
*inode
, struct page
*node_page
)
107 struct f2fs_inode
*raw_inode
= F2FS_INODE(node_page
);
109 if (!IS_INODE(node_page
))
112 inode
->i_mode
= le16_to_cpu(raw_inode
->i_mode
);
113 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
);
121 if (is_dent_dnode(node_page
))
122 return recover_dentry(node_page
, inode
);
124 f2fs_msg(inode
->i_sb
, KERN_NOTICE
, "recover_inode: ino = %x, name = %s",
125 ino_of_node(node_page
), raw_inode
->i_name
);
129 static int find_fsync_dnodes(struct f2fs_sb_info
*sbi
, struct list_head
*head
)
131 unsigned long long cp_ver
= cur_cp_version(F2FS_CKPT(sbi
));
132 struct curseg_info
*curseg
;
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
;
142 page
= alloc_page(GFP_F2FS_ZERO
);
148 struct fsync_inode_entry
*entry
;
150 err
= f2fs_submit_page_bio(sbi
, page
, blkaddr
, READ_SYNC
);
156 if (cp_ver
!= cpver_of_node(page
))
159 if (!is_fsync_dnode(page
))
162 entry
= get_fsync_inode(head
, ino_of_node(page
));
164 if (IS_INODE(page
) && is_dent_dnode(page
))
165 set_inode_flag(F2FS_I(entry
->inode
),
168 if (IS_INODE(page
) && is_dent_dnode(page
)) {
169 err
= recover_inode_page(sbi
, page
);
174 /* add this fsync inode to the list */
175 entry
= kmem_cache_alloc(fsync_entry_slab
, GFP_NOFS
);
181 entry
->inode
= f2fs_iget(sbi
->sb
, ino_of_node(page
));
182 if (IS_ERR(entry
->inode
)) {
183 err
= PTR_ERR(entry
->inode
);
184 kmem_cache_free(fsync_entry_slab
, entry
);
187 list_add_tail(&entry
->list
, head
);
189 entry
->blkaddr
= blkaddr
;
191 err
= recover_inode(entry
->inode
, page
);
192 if (err
&& err
!= -ENOENT
)
195 /* check next segment */
196 blkaddr
= next_blkaddr_of_node(page
);
200 __free_pages(page
, 0);
205 static void destroy_fsync_dnodes(struct list_head
*head
)
207 struct fsync_inode_entry
*entry
, *tmp
;
209 list_for_each_entry_safe(entry
, tmp
, head
, list
) {
211 list_del(&entry
->list
);
212 kmem_cache_free(fsync_entry_slab
, entry
);
216 static int check_index_in_prev_nodes(struct f2fs_sb_info
*sbi
,
217 block_t blkaddr
, struct dnode_of_data
*dn
)
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
;
227 struct page
*node_page
;
232 sentry
= get_seg_entry(sbi
, segno
);
233 if (!f2fs_test_bit(blkoff
, sentry
->cur_valid_map
))
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
];
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);
253 /* 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
;
258 tdn
.node_page
= dn
->inode_page
;
259 tdn
.ofs_in_node
= le16_to_cpu(sum
.ofs_in_node
);
260 truncate_data_blocks_range(&tdn
, 1);
262 } else if (dn
->nid
== nid
) {
263 struct dnode_of_data tdn
= *dn
;
264 tdn
.ofs_in_node
= le16_to_cpu(sum
.ofs_in_node
);
265 truncate_data_blocks_range(&tdn
, 1);
269 /* Get the node page */
270 node_page
= get_node_page(sbi
, nid
);
271 if (IS_ERR(node_page
))
272 return PTR_ERR(node_page
);
274 offset
= ofs_of_node(node_page
);
275 ino
= ino_of_node(node_page
);
276 f2fs_put_page(node_page
, 1);
278 /* Deallocate previous index in the node page */
279 inode
= f2fs_iget(sbi
->sb
, ino
);
281 return PTR_ERR(inode
);
283 bidx
= start_bidx_of_node(offset
, F2FS_I(inode
)) +
284 le16_to_cpu(sum
.ofs_in_node
);
286 truncate_hole(inode
, bidx
, bidx
+ 1);
291 static int do_recover_data(struct f2fs_sb_info
*sbi
, struct inode
*inode
,
292 struct page
*page
, block_t blkaddr
)
294 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
295 unsigned int start
, end
;
296 struct dnode_of_data dn
;
297 struct f2fs_summary sum
;
299 int err
= 0, recovered
= 0;
301 if (recover_inline_data(inode
, page
))
304 start
= start_bidx_of_node(ofs_of_node(page
), fi
);
306 end
= start
+ ADDRS_PER_INODE(fi
);
308 end
= start
+ ADDRS_PER_BLOCK
;
312 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
314 err
= get_dnode_of_data(&dn
, start
, ALLOC_NODE
);
320 wait_on_page_writeback(dn
.node_page
);
322 get_node_info(sbi
, dn
.nid
, &ni
);
323 f2fs_bug_on(ni
.ino
!= ino_of_node(page
));
324 f2fs_bug_on(ofs_of_node(dn
.node_page
) != ofs_of_node(page
));
326 for (; start
< end
; start
++) {
329 src
= datablock_addr(dn
.node_page
, dn
.ofs_in_node
);
330 dest
= datablock_addr(page
, dn
.ofs_in_node
);
332 if (src
!= dest
&& dest
!= NEW_ADDR
&& dest
!= NULL_ADDR
) {
333 if (src
== NULL_ADDR
) {
334 err
= reserve_new_block(&dn
);
335 /* We should not get -ENOSPC */
339 /* Check the previous node page having this index */
340 err
= check_index_in_prev_nodes(sbi
, dest
, &dn
);
344 set_summary(&sum
, dn
.nid
, dn
.ofs_in_node
, ni
.version
);
346 /* write dummy data page */
347 recover_data_page(sbi
, NULL
, &sum
, src
, dest
);
348 update_extent_cache(dest
, &dn
);
354 /* write node page in place */
355 set_summary(&sum
, dn
.nid
, 0, 0);
356 if (IS_INODE(dn
.node_page
))
357 sync_inode_page(&dn
);
359 copy_node_footer(dn
.node_page
, page
);
360 fill_node_footer(dn
.node_page
, dn
.nid
, ni
.ino
,
361 ofs_of_node(page
), false);
362 set_page_dirty(dn
.node_page
);
364 recover_node_page(sbi
, dn
.node_page
, &sum
, &ni
, blkaddr
);
369 f2fs_msg(sbi
->sb
, KERN_NOTICE
,
370 "recover_data: ino = %lx, recovered = %d blocks, err = %d",
371 inode
->i_ino
, recovered
, err
);
375 static int recover_data(struct f2fs_sb_info
*sbi
,
376 struct list_head
*head
, int type
)
378 unsigned long long cp_ver
= cur_cp_version(F2FS_CKPT(sbi
));
379 struct curseg_info
*curseg
;
384 /* get node pages in the current segment */
385 curseg
= CURSEG_I(sbi
, type
);
386 blkaddr
= NEXT_FREE_BLKADDR(sbi
, curseg
);
389 page
= alloc_page(GFP_F2FS_ZERO
);
396 struct fsync_inode_entry
*entry
;
398 err
= f2fs_submit_page_bio(sbi
, page
, blkaddr
, READ_SYNC
);
404 if (cp_ver
!= cpver_of_node(page
))
407 entry
= get_fsync_inode(head
, ino_of_node(page
));
411 err
= do_recover_data(sbi
, entry
->inode
, page
, blkaddr
);
415 if (entry
->blkaddr
== blkaddr
) {
417 list_del(&entry
->list
);
418 kmem_cache_free(fsync_entry_slab
, entry
);
421 /* check next segment */
422 blkaddr
= next_blkaddr_of_node(page
);
426 __free_pages(page
, 0);
429 allocate_new_segments(sbi
);
433 int recover_fsync_data(struct f2fs_sb_info
*sbi
)
435 struct list_head inode_list
;
437 bool need_writecp
= false;
439 fsync_entry_slab
= f2fs_kmem_cache_create("f2fs_fsync_inode_entry",
440 sizeof(struct fsync_inode_entry
), NULL
);
441 if (!fsync_entry_slab
)
444 INIT_LIST_HEAD(&inode_list
);
446 /* step #1: find fsynced inode numbers */
447 sbi
->por_doing
= true;
448 err
= find_fsync_dnodes(sbi
, &inode_list
);
452 if (list_empty(&inode_list
))
457 /* step #2: recover data */
458 err
= recover_data(sbi
, &inode_list
, CURSEG_WARM_NODE
);
459 f2fs_bug_on(!list_empty(&inode_list
));
461 destroy_fsync_dnodes(&inode_list
);
462 kmem_cache_destroy(fsync_entry_slab
);
463 sbi
->por_doing
= false;
464 if (!err
&& need_writecp
)
465 write_checkpoint(sbi
, false);