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 nid_t pino = le32_to_cpu(raw_inode->i_pino);
47 struct f2fs_dir_entry *de;
52 dir = check_dirty_dir_inode(F2FS_SB(inode->i_sb), pino);
54 dir = f2fs_iget(inode->i_sb, pino);
59 set_inode_flag(F2FS_I(dir), FI_DELAY_IPUT);
62 name.len = le32_to_cpu(raw_inode->i_namelen);
63 name.name = raw_inode->i_name;
65 de = f2fs_find_entry(dir, &name, &page);
68 f2fs_put_page(page, 0);
70 err = __f2fs_add_link(dir, &name, inode);
73 f2fs_msg(inode->i_sb, KERN_NOTICE, "recover_inode and its dentry: "
74 "ino = %x, name = %s, dir = %lx, err = %d",
75 ino_of_node(ipage), raw_inode->i_name, dir->i_ino, err);
80 static int recover_inode(struct inode *inode, struct page *node_page)
82 void *kaddr = page_address(node_page);
83 struct f2fs_node *raw_node = (struct f2fs_node *)kaddr;
84 struct f2fs_inode *raw_inode = &(raw_node->i);
86 if (!IS_INODE(node_page))
89 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
90 i_size_write(inode, le64_to_cpu(raw_inode->i_size));
91 inode->i_atime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
92 inode->i_ctime.tv_sec = le64_to_cpu(raw_inode->i_ctime);
93 inode->i_mtime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
94 inode->i_atime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec);
95 inode->i_ctime.tv_nsec = le32_to_cpu(raw_inode->i_ctime_nsec);
96 inode->i_mtime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec);
98 if (is_dent_dnode(node_page))
99 return recover_dentry(node_page, inode);
101 f2fs_msg(inode->i_sb, KERN_NOTICE, "recover_inode: ino = %x, name = %s",
102 ino_of_node(node_page), raw_inode->i_name);
106 static int find_fsync_dnodes(struct f2fs_sb_info *sbi, struct list_head *head)
108 unsigned long long cp_ver = le64_to_cpu(sbi->ckpt->checkpoint_ver);
109 struct curseg_info *curseg;
114 /* get node pages in the current segment */
115 curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
116 blkaddr = START_BLOCK(sbi, curseg->segno) + curseg->next_blkoff;
119 page = alloc_page(GFP_F2FS_ZERO);
121 return PTR_ERR(page);
125 struct fsync_inode_entry *entry;
127 err = f2fs_readpage(sbi, page, blkaddr, READ_SYNC);
133 if (cp_ver != cpver_of_node(page))
136 if (!is_fsync_dnode(page))
139 entry = get_fsync_inode(head, ino_of_node(page));
141 if (IS_INODE(page) && is_dent_dnode(page))
142 set_inode_flag(F2FS_I(entry->inode),
145 if (IS_INODE(page) && is_dent_dnode(page)) {
146 err = recover_inode_page(sbi, page);
151 /* add this fsync inode to the list */
152 entry = kmem_cache_alloc(fsync_entry_slab, GFP_NOFS);
158 entry->inode = f2fs_iget(sbi->sb, ino_of_node(page));
159 if (IS_ERR(entry->inode)) {
160 err = PTR_ERR(entry->inode);
161 kmem_cache_free(fsync_entry_slab, entry);
164 list_add_tail(&entry->list, head);
166 entry->blkaddr = blkaddr;
168 err = recover_inode(entry->inode, page);
169 if (err && err != -ENOENT)
172 /* check next segment */
173 blkaddr = next_blkaddr_of_node(page);
177 __free_pages(page, 0);
181 static void destroy_fsync_dnodes(struct f2fs_sb_info *sbi,
182 struct list_head *head)
184 struct fsync_inode_entry *entry, *tmp;
186 list_for_each_entry_safe(entry, tmp, head, list) {
188 list_del(&entry->list);
189 kmem_cache_free(fsync_entry_slab, entry);
193 static void check_index_in_prev_nodes(struct f2fs_sb_info *sbi,
196 struct seg_entry *sentry;
197 unsigned int segno = GET_SEGNO(sbi, blkaddr);
198 unsigned short blkoff = GET_SEGOFF_FROM_SEG0(sbi, blkaddr) &
199 (sbi->blocks_per_seg - 1);
200 struct f2fs_summary sum;
204 struct page *node_page;
208 sentry = get_seg_entry(sbi, segno);
209 if (!f2fs_test_bit(blkoff, sentry->cur_valid_map))
212 /* Get the previous summary */
213 for (i = CURSEG_WARM_DATA; i <= CURSEG_COLD_DATA; i++) {
214 struct curseg_info *curseg = CURSEG_I(sbi, i);
215 if (curseg->segno == segno) {
216 sum = curseg->sum_blk->entries[blkoff];
220 if (i > CURSEG_COLD_DATA) {
221 struct page *sum_page = get_sum_page(sbi, segno);
222 struct f2fs_summary_block *sum_node;
223 kaddr = page_address(sum_page);
224 sum_node = (struct f2fs_summary_block *)kaddr;
225 sum = sum_node->entries[blkoff];
226 f2fs_put_page(sum_page, 1);
229 /* Get the node page */
230 node_page = get_node_page(sbi, le32_to_cpu(sum.nid));
231 bidx = start_bidx_of_node(ofs_of_node(node_page)) +
232 le16_to_cpu(sum.ofs_in_node);
233 ino = ino_of_node(node_page);
234 f2fs_put_page(node_page, 1);
236 /* Deallocate previous index in the node page */
237 inode = f2fs_iget(sbi->sb, ino);
241 truncate_hole(inode, bidx, bidx + 1);
245 static int do_recover_data(struct f2fs_sb_info *sbi, struct inode *inode,
246 struct page *page, block_t blkaddr)
248 unsigned int start, end;
249 struct dnode_of_data dn;
250 struct f2fs_summary sum;
252 int err = 0, recovered = 0;
255 start = start_bidx_of_node(ofs_of_node(page));
257 end = start + ADDRS_PER_INODE;
259 end = start + ADDRS_PER_BLOCK;
261 ilock = mutex_lock_op(sbi);
262 set_new_dnode(&dn, inode, NULL, NULL, 0);
264 err = get_dnode_of_data(&dn, start, ALLOC_NODE);
266 mutex_unlock_op(sbi, ilock);
270 wait_on_page_writeback(dn.node_page);
272 get_node_info(sbi, dn.nid, &ni);
273 BUG_ON(ni.ino != ino_of_node(page));
274 BUG_ON(ofs_of_node(dn.node_page) != ofs_of_node(page));
276 for (; start < end; start++) {
279 src = datablock_addr(dn.node_page, dn.ofs_in_node);
280 dest = datablock_addr(page, dn.ofs_in_node);
282 if (src != dest && dest != NEW_ADDR && dest != NULL_ADDR) {
283 if (src == NULL_ADDR) {
284 int err = reserve_new_block(&dn);
285 /* We should not get -ENOSPC */
289 /* Check the previous node page having this index */
290 check_index_in_prev_nodes(sbi, dest);
292 set_summary(&sum, dn.nid, dn.ofs_in_node, ni.version);
294 /* write dummy data page */
295 recover_data_page(sbi, NULL, &sum, src, dest);
296 update_extent_cache(dest, &dn);
302 /* write node page in place */
303 set_summary(&sum, dn.nid, 0, 0);
304 if (IS_INODE(dn.node_page))
305 sync_inode_page(&dn);
307 copy_node_footer(dn.node_page, page);
308 fill_node_footer(dn.node_page, dn.nid, ni.ino,
309 ofs_of_node(page), false);
310 set_page_dirty(dn.node_page);
312 recover_node_page(sbi, dn.node_page, &sum, &ni, blkaddr);
314 mutex_unlock_op(sbi, ilock);
316 f2fs_msg(sbi->sb, KERN_NOTICE, "recover_data: ino = %lx, "
317 "recovered_data = %d blocks",
318 inode->i_ino, recovered);
322 static int recover_data(struct f2fs_sb_info *sbi,
323 struct list_head *head, int type)
325 unsigned long long cp_ver = le64_to_cpu(sbi->ckpt->checkpoint_ver);
326 struct curseg_info *curseg;
331 /* get node pages in the current segment */
332 curseg = CURSEG_I(sbi, type);
333 blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
336 page = alloc_page(GFP_NOFS | __GFP_ZERO);
343 struct fsync_inode_entry *entry;
345 err = f2fs_readpage(sbi, page, blkaddr, READ_SYNC);
351 if (cp_ver != cpver_of_node(page))
354 entry = get_fsync_inode(head, ino_of_node(page));
358 err = do_recover_data(sbi, entry->inode, page, blkaddr);
362 if (entry->blkaddr == blkaddr) {
364 list_del(&entry->list);
365 kmem_cache_free(fsync_entry_slab, entry);
368 /* check next segment */
369 blkaddr = next_blkaddr_of_node(page);
374 __free_pages(page, 0);
377 allocate_new_segments(sbi);
381 int recover_fsync_data(struct f2fs_sb_info *sbi)
383 struct list_head inode_list;
386 fsync_entry_slab = f2fs_kmem_cache_create("f2fs_fsync_inode_entry",
387 sizeof(struct fsync_inode_entry), NULL);
388 if (unlikely(!fsync_entry_slab))
391 INIT_LIST_HEAD(&inode_list);
393 /* step #1: find fsynced inode numbers */
395 err = find_fsync_dnodes(sbi, &inode_list);
399 if (list_empty(&inode_list))
402 /* step #2: recover data */
403 err = recover_data(sbi, &inode_list, CURSEG_WARM_NODE);
404 BUG_ON(!list_empty(&inode_list));
406 destroy_fsync_dnodes(sbi, &inode_list);
407 kmem_cache_destroy(fsync_entry_slab);
409 write_checkpoint(sbi, false);