blob: 2d86eb26c493704bbcd9f2e4cc27d3bcdeabc682 [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
17static struct kmem_cache *fsync_entry_slab;
18
19bool space_for_roll_forward(struct f2fs_sb_info *sbi)
20{
21 if (sbi->last_valid_block_count + sbi->alloc_valid_block_count
22 > sbi->user_block_count)
23 return false;
24 return true;
25}
26
27static struct fsync_inode_entry *get_fsync_inode(struct list_head *head,
28 nid_t ino)
29{
30 struct list_head *this;
31 struct fsync_inode_entry *entry;
32
33 list_for_each(this, head) {
34 entry = list_entry(this, struct fsync_inode_entry, list);
35 if (entry->inode->i_ino == ino)
36 return entry;
37 }
38 return NULL;
39}
40
41static int recover_dentry(struct page *ipage, struct inode *inode)
42{
43 struct f2fs_node *raw_node = (struct f2fs_node *)kmap(ipage);
44 struct f2fs_inode *raw_inode = &(raw_node->i);
Al Virob7f7a5e2013-01-25 16:15:43 -050045 struct qstr name;
Jaegeuk Kimd624c962012-11-02 17:13:32 +090046 struct f2fs_dir_entry *de;
47 struct page *page;
48 struct inode *dir;
49 int err = 0;
50
51 if (!is_dent_dnode(ipage))
52 goto out;
53
54 dir = f2fs_iget(inode->i_sb, le32_to_cpu(raw_inode->i_pino));
55 if (IS_ERR(dir)) {
56 err = -EINVAL;
57 goto out;
58 }
59
Al Virob7f7a5e2013-01-25 16:15:43 -050060 name.len = le32_to_cpu(raw_inode->i_namelen);
61 name.name = raw_inode->i_name;
Jaegeuk Kimd624c962012-11-02 17:13:32 +090062
Al Virob7f7a5e2013-01-25 16:15:43 -050063 de = f2fs_find_entry(dir, &name, &page);
Jaegeuk Kimd624c962012-11-02 17:13:32 +090064 if (de) {
65 kunmap(page);
66 f2fs_put_page(page, 0);
67 } else {
Jaegeuk Kim90b2fc62013-02-12 07:17:20 +090068 err = __f2fs_add_link(dir, &name, inode);
Jaegeuk Kimd624c962012-11-02 17:13:32 +090069 }
70 iput(dir);
71out:
72 kunmap(ipage);
73 return err;
74}
75
76static int recover_inode(struct inode *inode, struct page *node_page)
77{
78 void *kaddr = page_address(node_page);
79 struct f2fs_node *raw_node = (struct f2fs_node *)kaddr;
80 struct f2fs_inode *raw_inode = &(raw_node->i);
81
Jaegeuk Kim25ca9232012-11-28 16:12:41 +090082 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
Jaegeuk Kimd624c962012-11-02 17:13:32 +090083 i_size_write(inode, le64_to_cpu(raw_inode->i_size));
84 inode->i_atime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
85 inode->i_ctime.tv_sec = le64_to_cpu(raw_inode->i_ctime);
86 inode->i_mtime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
87 inode->i_atime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec);
88 inode->i_ctime.tv_nsec = le32_to_cpu(raw_inode->i_ctime_nsec);
89 inode->i_mtime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec);
90
91 return recover_dentry(node_page, inode);
92}
93
94static int find_fsync_dnodes(struct f2fs_sb_info *sbi, struct list_head *head)
95{
96 unsigned long long cp_ver = le64_to_cpu(sbi->ckpt->checkpoint_ver);
97 struct curseg_info *curseg;
98 struct page *page;
99 block_t blkaddr;
100 int err = 0;
101
102 /* get node pages in the current segment */
103 curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
104 blkaddr = START_BLOCK(sbi, curseg->segno) + curseg->next_blkoff;
105
106 /* read node page */
107 page = alloc_page(GFP_F2FS_ZERO);
108 if (IS_ERR(page))
109 return PTR_ERR(page);
110 lock_page(page);
111
112 while (1) {
113 struct fsync_inode_entry *entry;
114
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900115 err = f2fs_readpage(sbi, page, blkaddr, READ_SYNC);
116 if (err)
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900117 goto out;
118
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900119 lock_page(page);
120
121 if (cp_ver != cpver_of_node(page)) {
122 err = -EINVAL;
123 goto unlock_out;
124 }
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900125
126 if (!is_fsync_dnode(page))
127 goto next;
128
129 entry = get_fsync_inode(head, ino_of_node(page));
130 if (entry) {
131 entry->blkaddr = blkaddr;
132 if (IS_INODE(page) && is_dent_dnode(page))
133 set_inode_flag(F2FS_I(entry->inode),
134 FI_INC_LINK);
135 } else {
136 if (IS_INODE(page) && is_dent_dnode(page)) {
137 if (recover_inode_page(sbi, page)) {
138 err = -ENOMEM;
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900139 goto unlock_out;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900140 }
141 }
142
143 /* add this fsync inode to the list */
144 entry = kmem_cache_alloc(fsync_entry_slab, GFP_NOFS);
145 if (!entry) {
146 err = -ENOMEM;
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900147 goto unlock_out;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900148 }
149
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900150 entry->inode = f2fs_iget(sbi->sb, ino_of_node(page));
151 if (IS_ERR(entry->inode)) {
152 err = PTR_ERR(entry->inode);
Namjae Jeonfd8bb652012-12-22 12:10:12 +0900153 kmem_cache_free(fsync_entry_slab, entry);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900154 goto unlock_out;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900155 }
Namjae Jeonfd8bb652012-12-22 12:10:12 +0900156
Namjae Jeonfd8bb652012-12-22 12:10:12 +0900157 list_add_tail(&entry->list, head);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900158 entry->blkaddr = blkaddr;
159 }
160 if (IS_INODE(page)) {
161 err = recover_inode(entry->inode, page);
162 if (err)
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900163 goto unlock_out;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900164 }
165next:
166 /* check next segment */
167 blkaddr = next_blkaddr_of_node(page);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900168 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900169unlock_out:
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900170 unlock_page(page);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900171out:
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900172 __free_pages(page, 0);
173 return err;
174}
175
176static void destroy_fsync_dnodes(struct f2fs_sb_info *sbi,
177 struct list_head *head)
178{
Dan Carpenterd8b79b22013-01-20 18:02:58 +0300179 struct fsync_inode_entry *entry, *tmp;
180
181 list_for_each_entry_safe(entry, tmp, head, list) {
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900182 iput(entry->inode);
183 list_del(&entry->list);
184 kmem_cache_free(fsync_entry_slab, entry);
185 }
186}
187
188static void check_index_in_prev_nodes(struct f2fs_sb_info *sbi,
189 block_t blkaddr)
190{
191 struct seg_entry *sentry;
192 unsigned int segno = GET_SEGNO(sbi, blkaddr);
193 unsigned short blkoff = GET_SEGOFF_FROM_SEG0(sbi, blkaddr) &
194 (sbi->blocks_per_seg - 1);
195 struct f2fs_summary sum;
196 nid_t ino;
197 void *kaddr;
198 struct inode *inode;
199 struct page *node_page;
200 block_t bidx;
201 int i;
202
203 sentry = get_seg_entry(sbi, segno);
204 if (!f2fs_test_bit(blkoff, sentry->cur_valid_map))
205 return;
206
207 /* Get the previous summary */
208 for (i = CURSEG_WARM_DATA; i <= CURSEG_COLD_DATA; i++) {
209 struct curseg_info *curseg = CURSEG_I(sbi, i);
210 if (curseg->segno == segno) {
211 sum = curseg->sum_blk->entries[blkoff];
212 break;
213 }
214 }
215 if (i > CURSEG_COLD_DATA) {
216 struct page *sum_page = get_sum_page(sbi, segno);
217 struct f2fs_summary_block *sum_node;
218 kaddr = page_address(sum_page);
219 sum_node = (struct f2fs_summary_block *)kaddr;
220 sum = sum_node->entries[blkoff];
221 f2fs_put_page(sum_page, 1);
222 }
223
224 /* Get the node page */
225 node_page = get_node_page(sbi, le32_to_cpu(sum.nid));
226 bidx = start_bidx_of_node(ofs_of_node(node_page)) +
227 le16_to_cpu(sum.ofs_in_node);
228 ino = ino_of_node(node_page);
229 f2fs_put_page(node_page, 1);
230
231 /* Deallocate previous index in the node page */
Jaegeuk Kimd4686d562013-01-31 15:36:04 +0900232 inode = f2fs_iget(sbi->sb, ino);
Namjae Jeon06025f42012-12-22 12:09:43 +0900233 if (IS_ERR(inode))
234 return;
235
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900236 truncate_hole(inode, bidx, bidx + 1);
237 iput(inode);
238}
239
240static void do_recover_data(struct f2fs_sb_info *sbi, struct inode *inode,
241 struct page *page, block_t blkaddr)
242{
243 unsigned int start, end;
244 struct dnode_of_data dn;
245 struct f2fs_summary sum;
246 struct node_info ni;
247
248 start = start_bidx_of_node(ofs_of_node(page));
249 if (IS_INODE(page))
250 end = start + ADDRS_PER_INODE;
251 else
252 end = start + ADDRS_PER_BLOCK;
253
254 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900255 if (get_dnode_of_data(&dn, start, ALLOC_NODE))
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900256 return;
257
258 wait_on_page_writeback(dn.node_page);
259
260 get_node_info(sbi, dn.nid, &ni);
261 BUG_ON(ni.ino != ino_of_node(page));
262 BUG_ON(ofs_of_node(dn.node_page) != ofs_of_node(page));
263
264 for (; start < end; start++) {
265 block_t src, dest;
266
267 src = datablock_addr(dn.node_page, dn.ofs_in_node);
268 dest = datablock_addr(page, dn.ofs_in_node);
269
270 if (src != dest && dest != NEW_ADDR && dest != NULL_ADDR) {
271 if (src == NULL_ADDR) {
272 int err = reserve_new_block(&dn);
273 /* We should not get -ENOSPC */
274 BUG_ON(err);
275 }
276
277 /* Check the previous node page having this index */
278 check_index_in_prev_nodes(sbi, dest);
279
280 set_summary(&sum, dn.nid, dn.ofs_in_node, ni.version);
281
282 /* write dummy data page */
283 recover_data_page(sbi, NULL, &sum, src, dest);
284 update_extent_cache(dest, &dn);
285 }
286 dn.ofs_in_node++;
287 }
288
289 /* write node page in place */
290 set_summary(&sum, dn.nid, 0, 0);
291 if (IS_INODE(dn.node_page))
292 sync_inode_page(&dn);
293
294 copy_node_footer(dn.node_page, page);
295 fill_node_footer(dn.node_page, dn.nid, ni.ino,
296 ofs_of_node(page), false);
297 set_page_dirty(dn.node_page);
298
299 recover_node_page(sbi, dn.node_page, &sum, &ni, blkaddr);
300 f2fs_put_dnode(&dn);
301}
302
303static void recover_data(struct f2fs_sb_info *sbi,
304 struct list_head *head, int type)
305{
306 unsigned long long cp_ver = le64_to_cpu(sbi->ckpt->checkpoint_ver);
307 struct curseg_info *curseg;
308 struct page *page;
309 block_t blkaddr;
310
311 /* get node pages in the current segment */
312 curseg = CURSEG_I(sbi, type);
313 blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
314
315 /* read node page */
316 page = alloc_page(GFP_NOFS | __GFP_ZERO);
317 if (IS_ERR(page))
318 return;
319 lock_page(page);
320
321 while (1) {
322 struct fsync_inode_entry *entry;
323
324 if (f2fs_readpage(sbi, page, blkaddr, READ_SYNC))
325 goto out;
326
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900327 lock_page(page);
328
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900329 if (cp_ver != cpver_of_node(page))
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900330 goto unlock_out;
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900331
332 entry = get_fsync_inode(head, ino_of_node(page));
333 if (!entry)
334 goto next;
335
336 do_recover_data(sbi, entry->inode, page, blkaddr);
337
338 if (entry->blkaddr == blkaddr) {
339 iput(entry->inode);
340 list_del(&entry->list);
341 kmem_cache_free(fsync_entry_slab, entry);
342 }
343next:
344 /* check next segment */
345 blkaddr = next_blkaddr_of_node(page);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900346 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900347unlock_out:
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900348 unlock_page(page);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900349out:
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900350 __free_pages(page, 0);
351
352 allocate_new_segments(sbi);
353}
354
355void recover_fsync_data(struct f2fs_sb_info *sbi)
356{
357 struct list_head inode_list;
358
359 fsync_entry_slab = f2fs_kmem_cache_create("f2fs_fsync_inode_entry",
360 sizeof(struct fsync_inode_entry), NULL);
361 if (unlikely(!fsync_entry_slab))
362 return;
363
364 INIT_LIST_HEAD(&inode_list);
365
366 /* step #1: find fsynced inode numbers */
367 if (find_fsync_dnodes(sbi, &inode_list))
368 goto out;
369
370 if (list_empty(&inode_list))
371 goto out;
372
373 /* step #2: recover data */
374 sbi->por_doing = 1;
375 recover_data(sbi, &inode_list, CURSEG_WARM_NODE);
376 sbi->por_doing = 0;
377 BUG_ON(!list_empty(&inode_list));
378out:
379 destroy_fsync_dnodes(sbi, &inode_list);
380 kmem_cache_destroy(fsync_entry_slab);
Jaegeuk Kim43727522013-02-04 15:11:17 +0900381 write_checkpoint(sbi, false);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900382}