serial: sh-sci: Neaten dev_<level> uses
[linux/fpc-iii.git] / fs / f2fs / recovery.c
blob976a7a934db5ddf8ba7843f28cc8e1311c86a632
1 /*
2 * 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.
11 #include <linux/fs.h>
12 #include <linux/f2fs_fs.h>
13 #include "f2fs.h"
14 #include "node.h"
15 #include "segment.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)
23 return false;
24 return true;
27 static struct fsync_inode_entry *get_fsync_inode(struct list_head *head,
28 nid_t ino)
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)
36 return entry;
38 return NULL;
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;
46 struct qstr name;
47 struct page *page;
48 struct inode *dir, *einode;
49 int err = 0;
51 dir = check_dirty_dir_inode(F2FS_SB(inode->i_sb), pino);
52 if (!dir) {
53 dir = f2fs_iget(inode->i_sb, pino);
54 if (IS_ERR(dir)) {
55 err = PTR_ERR(dir);
56 goto out;
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)) {
66 WARN_ON(1);
67 err = -ENAMETOOLONG;
68 goto out;
70 retry:
71 de = f2fs_find_entry(dir, &name, &page);
72 if (de && inode->i_ino == le32_to_cpu(de->ino))
73 goto out_unmap_put;
74 if (de) {
75 einode = f2fs_iget(inode->i_sb, le32_to_cpu(de->ino));
76 if (IS_ERR(einode)) {
77 WARN_ON(1);
78 if (PTR_ERR(einode) == -ENOENT)
79 err = -EEXIST;
80 goto out_unmap_put;
82 err = acquire_orphan_inode(F2FS_SB(inode->i_sb));
83 if (err) {
84 iput(einode);
85 goto out_unmap_put;
87 f2fs_delete_entry(de, page, einode);
88 iput(einode);
89 goto retry;
91 err = __f2fs_add_link(dir, &name, inode);
92 goto out;
94 out_unmap_put:
95 kunmap(page);
96 f2fs_put_page(page, 0);
97 out:
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);
102 return 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))
110 return 0;
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);
126 return 0;
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;
133 struct page *page;
134 block_t blkaddr;
135 int err = 0;
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;
141 /* read node page */
142 page = alloc_page(GFP_F2FS_ZERO);
143 if (!page)
144 return -ENOMEM;
145 lock_page(page);
147 while (1) {
148 struct fsync_inode_entry *entry;
150 err = f2fs_submit_page_bio(sbi, page, blkaddr, READ_SYNC);
151 if (err)
152 return err;
154 lock_page(page);
156 if (cp_ver != cpver_of_node(page))
157 break;
159 if (!is_fsync_dnode(page))
160 goto next;
162 entry = get_fsync_inode(head, ino_of_node(page));
163 if (entry) {
164 if (IS_INODE(page) && is_dent_dnode(page))
165 set_inode_flag(F2FS_I(entry->inode),
166 FI_INC_LINK);
167 } else {
168 if (IS_INODE(page) && is_dent_dnode(page)) {
169 err = recover_inode_page(sbi, page);
170 if (err)
171 break;
174 /* add this fsync inode to the list */
175 entry = kmem_cache_alloc(fsync_entry_slab, GFP_NOFS);
176 if (!entry) {
177 err = -ENOMEM;
178 break;
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);
185 break;
187 list_add_tail(&entry->list, head);
189 entry->blkaddr = blkaddr;
191 err = recover_inode(entry->inode, page);
192 if (err && err != -ENOENT)
193 break;
194 next:
195 /* check next segment */
196 blkaddr = next_blkaddr_of_node(page);
199 unlock_page(page);
200 __free_pages(page, 0);
202 return err;
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) {
210 iput(entry->inode);
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;
224 nid_t ino, nid;
225 void *kaddr;
226 struct inode *inode;
227 struct page *node_page;
228 unsigned int offset;
229 block_t bidx;
230 int i;
232 sentry = get_seg_entry(sbi, segno);
233 if (!f2fs_test_bit(blkoff, sentry->cur_valid_map))
234 return 0;
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];
241 break;
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;
257 tdn.nid = nid;
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);
261 return 0;
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);
266 return 0;
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);
280 if (IS_ERR(inode))
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);
287 iput(inode);
288 return 0;
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;
298 struct node_info ni;
299 int err = 0, recovered = 0;
301 if (recover_inline_data(inode, page))
302 goto out;
304 start = start_bidx_of_node(ofs_of_node(page), fi);
305 if (IS_INODE(page))
306 end = start + ADDRS_PER_INODE(fi);
307 else
308 end = start + ADDRS_PER_BLOCK;
310 f2fs_lock_op(sbi);
312 set_new_dnode(&dn, inode, NULL, NULL, 0);
314 err = get_dnode_of_data(&dn, start, ALLOC_NODE);
315 if (err) {
316 f2fs_unlock_op(sbi);
317 goto out;
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++) {
327 block_t src, dest;
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 */
336 f2fs_bug_on(err);
339 /* Check the previous node page having this index */
340 err = check_index_in_prev_nodes(sbi, dest, &dn);
341 if (err)
342 goto err;
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);
349 recovered++;
351 dn.ofs_in_node++;
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);
365 err:
366 f2fs_put_dnode(&dn);
367 f2fs_unlock_op(sbi);
368 out:
369 f2fs_msg(sbi->sb, KERN_NOTICE,
370 "recover_data: ino = %lx, recovered = %d blocks, err = %d",
371 inode->i_ino, recovered, err);
372 return 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;
380 struct page *page;
381 int err = 0;
382 block_t blkaddr;
384 /* get node pages in the current segment */
385 curseg = CURSEG_I(sbi, type);
386 blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
388 /* read node page */
389 page = alloc_page(GFP_F2FS_ZERO);
390 if (!page)
391 return -ENOMEM;
393 lock_page(page);
395 while (1) {
396 struct fsync_inode_entry *entry;
398 err = f2fs_submit_page_bio(sbi, page, blkaddr, READ_SYNC);
399 if (err)
400 return err;
402 lock_page(page);
404 if (cp_ver != cpver_of_node(page))
405 break;
407 entry = get_fsync_inode(head, ino_of_node(page));
408 if (!entry)
409 goto next;
411 err = do_recover_data(sbi, entry->inode, page, blkaddr);
412 if (err)
413 break;
415 if (entry->blkaddr == blkaddr) {
416 iput(entry->inode);
417 list_del(&entry->list);
418 kmem_cache_free(fsync_entry_slab, entry);
420 next:
421 /* check next segment */
422 blkaddr = next_blkaddr_of_node(page);
425 unlock_page(page);
426 __free_pages(page, 0);
428 if (!err)
429 allocate_new_segments(sbi);
430 return err;
433 int recover_fsync_data(struct f2fs_sb_info *sbi)
435 struct list_head inode_list;
436 int err;
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)
442 return -ENOMEM;
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);
449 if (err)
450 goto out;
452 if (list_empty(&inode_list))
453 goto out;
455 need_writecp = true;
457 /* step #2: recover data */
458 err = recover_data(sbi, &inode_list, CURSEG_WARM_NODE);
459 f2fs_bug_on(!list_empty(&inode_list));
460 out:
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);
466 return err;