blob: e2a3e1a8eae984fcccc83f42d8e65dfaa659b5eb [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);
45 struct dentry dent, parent;
46 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
60 parent.d_inode = dir;
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;
64
65 de = f2fs_find_entry(dir, &dent.d_name, &page);
66 if (de) {
67 kunmap(page);
68 f2fs_put_page(page, 0);
69 } else {
Jaegeuk Kimc335a862013-01-03 09:33:20 +090070 err = f2fs_add_link(&dent, inode);
Jaegeuk Kimd624c962012-11-02 17:13:32 +090071 }
72 iput(dir);
73out:
74 kunmap(ipage);
75 return err;
76}
77
78static int recover_inode(struct inode *inode, struct page *node_page)
79{
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);
83
Jaegeuk Kim25ca9232012-11-28 16:12:41 +090084 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
Jaegeuk Kimd624c962012-11-02 17:13:32 +090085 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);
92
93 return recover_dentry(node_page, inode);
94}
95
96static int find_fsync_dnodes(struct f2fs_sb_info *sbi, struct list_head *head)
97{
98 unsigned long long cp_ver = le64_to_cpu(sbi->ckpt->checkpoint_ver);
99 struct curseg_info *curseg;
100 struct page *page;
101 block_t blkaddr;
102 int err = 0;
103
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;
107
108 /* read node page */
109 page = alloc_page(GFP_F2FS_ZERO);
110 if (IS_ERR(page))
111 return PTR_ERR(page);
112 lock_page(page);
113
114 while (1) {
115 struct fsync_inode_entry *entry;
116
117 if (f2fs_readpage(sbi, page, blkaddr, READ_SYNC))
118 goto out;
119
120 if (cp_ver != cpver_of_node(page))
121 goto out;
122
123 if (!is_fsync_dnode(page))
124 goto next;
125
126 entry = get_fsync_inode(head, ino_of_node(page));
127 if (entry) {
128 entry->blkaddr = blkaddr;
129 if (IS_INODE(page) && is_dent_dnode(page))
130 set_inode_flag(F2FS_I(entry->inode),
131 FI_INC_LINK);
132 } else {
133 if (IS_INODE(page) && is_dent_dnode(page)) {
134 if (recover_inode_page(sbi, page)) {
135 err = -ENOMEM;
136 goto out;
137 }
138 }
139
140 /* add this fsync inode to the list */
141 entry = kmem_cache_alloc(fsync_entry_slab, GFP_NOFS);
142 if (!entry) {
143 err = -ENOMEM;
144 goto out;
145 }
146
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900147 entry->inode = f2fs_iget(sbi->sb, ino_of_node(page));
148 if (IS_ERR(entry->inode)) {
149 err = PTR_ERR(entry->inode);
Namjae Jeonfd8bb652012-12-22 12:10:12 +0900150 kmem_cache_free(fsync_entry_slab, entry);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900151 goto out;
152 }
Namjae Jeonfd8bb652012-12-22 12:10:12 +0900153
Namjae Jeonfd8bb652012-12-22 12:10:12 +0900154 list_add_tail(&entry->list, head);
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900155 entry->blkaddr = blkaddr;
156 }
157 if (IS_INODE(page)) {
158 err = recover_inode(entry->inode, page);
159 if (err)
160 goto out;
161 }
162next:
163 /* check next segment */
164 blkaddr = next_blkaddr_of_node(page);
165 ClearPageUptodate(page);
166 }
167out:
168 unlock_page(page);
169 __free_pages(page, 0);
170 return err;
171}
172
173static void destroy_fsync_dnodes(struct f2fs_sb_info *sbi,
174 struct list_head *head)
175{
Dan Carpenterd8b79b22013-01-20 18:02:58 +0300176 struct fsync_inode_entry *entry, *tmp;
177
178 list_for_each_entry_safe(entry, tmp, head, list) {
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900179 iput(entry->inode);
180 list_del(&entry->list);
181 kmem_cache_free(fsync_entry_slab, entry);
182 }
183}
184
185static void check_index_in_prev_nodes(struct f2fs_sb_info *sbi,
186 block_t blkaddr)
187{
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;
193 nid_t ino;
194 void *kaddr;
195 struct inode *inode;
196 struct page *node_page;
197 block_t bidx;
198 int i;
199
200 sentry = get_seg_entry(sbi, segno);
201 if (!f2fs_test_bit(blkoff, sentry->cur_valid_map))
202 return;
203
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];
209 break;
210 }
211 }
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);
219 }
220
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);
227
228 /* Deallocate previous index in the node page */
Jaegeuk Kimd4686d562013-01-31 15:36:04 +0900229 inode = f2fs_iget(sbi->sb, ino);
Namjae Jeon06025f42012-12-22 12:09:43 +0900230 if (IS_ERR(inode))
231 return;
232
Jaegeuk Kimd624c962012-11-02 17:13:32 +0900233 truncate_hole(inode, bidx, bidx + 1);
234 iput(inode);
235}
236
237static void do_recover_data(struct f2fs_sb_info *sbi, struct inode *inode,
238 struct page *page, block_t blkaddr)
239{
240 unsigned int start, end;
241 struct dnode_of_data dn;
242 struct f2fs_summary sum;
243 struct node_info ni;
244
245 start = start_bidx_of_node(ofs_of_node(page));
246 if (IS_INODE(page))
247 end = start + ADDRS_PER_INODE;
248 else
249 end = start + ADDRS_PER_BLOCK;
250
251 set_new_dnode(&dn, inode, NULL, NULL, 0);
252 if (get_dnode_of_data(&dn, start, 0))
253 return;
254
255 wait_on_page_writeback(dn.node_page);
256
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));
260
261 for (; start < end; start++) {
262 block_t src, dest;
263
264 src = datablock_addr(dn.node_page, dn.ofs_in_node);
265 dest = datablock_addr(page, dn.ofs_in_node);
266
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 */
271 BUG_ON(err);
272 }
273
274 /* Check the previous node page having this index */
275 check_index_in_prev_nodes(sbi, dest);
276
277 set_summary(&sum, dn.nid, dn.ofs_in_node, ni.version);
278
279 /* write dummy data page */
280 recover_data_page(sbi, NULL, &sum, src, dest);
281 update_extent_cache(dest, &dn);
282 }
283 dn.ofs_in_node++;
284 }
285
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);
290
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);
295
296 recover_node_page(sbi, dn.node_page, &sum, &ni, blkaddr);
297 f2fs_put_dnode(&dn);
298}
299
300static void recover_data(struct f2fs_sb_info *sbi,
301 struct list_head *head, int type)
302{
303 unsigned long long cp_ver = le64_to_cpu(sbi->ckpt->checkpoint_ver);
304 struct curseg_info *curseg;
305 struct page *page;
306 block_t blkaddr;
307
308 /* get node pages in the current segment */
309 curseg = CURSEG_I(sbi, type);
310 blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
311
312 /* read node page */
313 page = alloc_page(GFP_NOFS | __GFP_ZERO);
314 if (IS_ERR(page))
315 return;
316 lock_page(page);
317
318 while (1) {
319 struct fsync_inode_entry *entry;
320
321 if (f2fs_readpage(sbi, page, blkaddr, READ_SYNC))
322 goto out;
323
324 if (cp_ver != cpver_of_node(page))
325 goto out;
326
327 entry = get_fsync_inode(head, ino_of_node(page));
328 if (!entry)
329 goto next;
330
331 do_recover_data(sbi, entry->inode, page, blkaddr);
332
333 if (entry->blkaddr == blkaddr) {
334 iput(entry->inode);
335 list_del(&entry->list);
336 kmem_cache_free(fsync_entry_slab, entry);
337 }
338next:
339 /* check next segment */
340 blkaddr = next_blkaddr_of_node(page);
341 ClearPageUptodate(page);
342 }
343out:
344 unlock_page(page);
345 __free_pages(page, 0);
346
347 allocate_new_segments(sbi);
348}
349
350void recover_fsync_data(struct f2fs_sb_info *sbi)
351{
352 struct list_head inode_list;
353
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))
357 return;
358
359 INIT_LIST_HEAD(&inode_list);
360
361 /* step #1: find fsynced inode numbers */
362 if (find_fsync_dnodes(sbi, &inode_list))
363 goto out;
364
365 if (list_empty(&inode_list))
366 goto out;
367
368 /* step #2: recover data */
369 sbi->por_doing = 1;
370 recover_data(sbi, &inode_list, CURSEG_WARM_NODE);
371 sbi->por_doing = 0;
372 BUG_ON(!list_empty(&inode_list));
373out:
374 destroy_fsync_dnodes(sbi, &inode_list);
375 kmem_cache_destroy(fsync_entry_slab);
376 write_checkpoint(sbi, false, false);
377}