HID: hiddev: Fix slab-out-of-bounds write in hiddev_ioctl_usage()
[linux/fpc-iii.git] / fs / f2fs / recovery.c
blob410354c334d75403cc7a2b6805a3b6102ca95ce9
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"
18 * Roll forward recovery scenarios.
20 * [Term] F: fsync_mark, D: dentry_mark
22 * 1. inode(x) | CP | inode(x) | dnode(F)
23 * -> Update the latest inode(x).
25 * 2. inode(x) | CP | inode(F) | dnode(F)
26 * -> No problem.
28 * 3. inode(x) | CP | dnode(F) | inode(x)
29 * -> Recover to the latest dnode(F), and drop the last inode(x)
31 * 4. inode(x) | CP | dnode(F) | inode(F)
32 * -> No problem.
34 * 5. CP | inode(x) | dnode(F)
35 * -> The inode(DF) was missing. Should drop this dnode(F).
37 * 6. CP | inode(DF) | dnode(F)
38 * -> No problem.
40 * 7. CP | dnode(F) | inode(DF)
41 * -> If f2fs_iget fails, then goto next to find inode(DF).
43 * 8. CP | dnode(F) | inode(x)
44 * -> If f2fs_iget fails, then goto next to find inode(DF).
45 * But it will fail due to no inode(DF).
48 static struct kmem_cache *fsync_entry_slab;
50 bool space_for_roll_forward(struct f2fs_sb_info *sbi)
52 if (sbi->last_valid_block_count + sbi->alloc_valid_block_count
53 > sbi->user_block_count)
54 return false;
55 return true;
58 static struct fsync_inode_entry *get_fsync_inode(struct list_head *head,
59 nid_t ino)
61 struct fsync_inode_entry *entry;
63 list_for_each_entry(entry, head, list)
64 if (entry->inode->i_ino == ino)
65 return entry;
67 return NULL;
70 static struct fsync_inode_entry *add_fsync_inode(struct list_head *head,
71 struct inode *inode)
73 struct fsync_inode_entry *entry;
75 entry = kmem_cache_alloc(fsync_entry_slab, GFP_F2FS_ZERO);
76 if (!entry)
77 return NULL;
79 entry->inode = inode;
80 list_add_tail(&entry->list, head);
82 return entry;
85 static void del_fsync_inode(struct fsync_inode_entry *entry)
87 iput(entry->inode);
88 list_del(&entry->list);
89 kmem_cache_free(fsync_entry_slab, entry);
92 static int recover_dentry(struct inode *inode, struct page *ipage,
93 struct list_head *dir_list)
95 struct f2fs_inode *raw_inode = F2FS_INODE(ipage);
96 nid_t pino = le32_to_cpu(raw_inode->i_pino);
97 struct f2fs_dir_entry *de;
98 struct qstr name;
99 struct page *page;
100 struct inode *dir, *einode;
101 struct fsync_inode_entry *entry;
102 int err = 0;
104 entry = get_fsync_inode(dir_list, pino);
105 if (!entry) {
106 dir = f2fs_iget(inode->i_sb, pino);
107 if (IS_ERR(dir)) {
108 err = PTR_ERR(dir);
109 goto out;
112 entry = add_fsync_inode(dir_list, dir);
113 if (!entry) {
114 err = -ENOMEM;
115 iput(dir);
116 goto out;
120 dir = entry->inode;
122 if (file_enc_name(inode))
123 return 0;
125 name.len = le32_to_cpu(raw_inode->i_namelen);
126 name.name = raw_inode->i_name;
128 if (unlikely(name.len > F2FS_NAME_LEN)) {
129 WARN_ON(1);
130 err = -ENAMETOOLONG;
131 goto out;
133 retry:
134 de = f2fs_find_entry(dir, &name, &page);
135 if (de && inode->i_ino == le32_to_cpu(de->ino))
136 goto out_unmap_put;
138 if (de) {
139 einode = f2fs_iget(inode->i_sb, le32_to_cpu(de->ino));
140 if (IS_ERR(einode)) {
141 WARN_ON(1);
142 err = PTR_ERR(einode);
143 if (err == -ENOENT)
144 err = -EEXIST;
145 goto out_unmap_put;
147 err = acquire_orphan_inode(F2FS_I_SB(inode));
148 if (err) {
149 iput(einode);
150 goto out_unmap_put;
152 f2fs_delete_entry(de, page, dir, einode);
153 iput(einode);
154 goto retry;
156 err = __f2fs_add_link(dir, &name, inode, inode->i_ino, inode->i_mode);
158 goto out;
160 out_unmap_put:
161 f2fs_dentry_kunmap(dir, page);
162 f2fs_put_page(page, 0);
163 out:
164 f2fs_msg(inode->i_sb, KERN_NOTICE,
165 "%s: ino = %x, name = %s, dir = %lx, err = %d",
166 __func__, ino_of_node(ipage), raw_inode->i_name,
167 IS_ERR(dir) ? 0 : dir->i_ino, err);
168 return err;
171 static void recover_inode(struct inode *inode, struct page *page)
173 struct f2fs_inode *raw = F2FS_INODE(page);
174 char *name;
176 inode->i_mode = le16_to_cpu(raw->i_mode);
177 i_size_write(inode, le64_to_cpu(raw->i_size));
178 inode->i_atime.tv_sec = le64_to_cpu(raw->i_mtime);
179 inode->i_ctime.tv_sec = le64_to_cpu(raw->i_ctime);
180 inode->i_mtime.tv_sec = le64_to_cpu(raw->i_mtime);
181 inode->i_atime.tv_nsec = le32_to_cpu(raw->i_mtime_nsec);
182 inode->i_ctime.tv_nsec = le32_to_cpu(raw->i_ctime_nsec);
183 inode->i_mtime.tv_nsec = le32_to_cpu(raw->i_mtime_nsec);
185 if (file_enc_name(inode))
186 name = "<encrypted>";
187 else
188 name = F2FS_INODE(page)->i_name;
190 f2fs_msg(inode->i_sb, KERN_NOTICE, "recover_inode: ino = %x, name = %s",
191 ino_of_node(page), name);
194 static int find_fsync_dnodes(struct f2fs_sb_info *sbi, struct list_head *head)
196 struct curseg_info *curseg;
197 struct inode *inode;
198 struct page *page = NULL;
199 block_t blkaddr;
200 int err = 0;
202 /* get node pages in the current segment */
203 curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
204 blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
206 ra_meta_pages(sbi, blkaddr, 1, META_POR, true);
208 while (1) {
209 struct fsync_inode_entry *entry;
211 if (!f2fs_is_valid_blkaddr(sbi, blkaddr, META_POR))
212 return 0;
214 page = get_tmp_page(sbi, blkaddr);
216 if (!is_recoverable_dnode(page))
217 break;
219 if (!is_fsync_dnode(page))
220 goto next;
222 entry = get_fsync_inode(head, ino_of_node(page));
223 if (!entry) {
224 if (IS_INODE(page) && is_dent_dnode(page)) {
225 err = recover_inode_page(sbi, page);
226 if (err)
227 break;
231 * CP | dnode(F) | inode(DF)
232 * For this case, we should not give up now.
234 inode = f2fs_iget(sbi->sb, ino_of_node(page));
235 if (IS_ERR(inode)) {
236 err = PTR_ERR(inode);
237 if (err == -ENOENT) {
238 err = 0;
239 goto next;
241 break;
244 /* add this fsync inode to the list */
245 entry = add_fsync_inode(head, inode);
246 if (!entry) {
247 err = -ENOMEM;
248 iput(inode);
249 break;
252 entry->blkaddr = blkaddr;
254 if (IS_INODE(page)) {
255 entry->last_inode = blkaddr;
256 if (is_dent_dnode(page))
257 entry->last_dentry = blkaddr;
259 next:
260 /* check next segment */
261 blkaddr = next_blkaddr_of_node(page);
262 f2fs_put_page(page, 1);
264 ra_meta_pages_cond(sbi, blkaddr);
266 f2fs_put_page(page, 1);
267 return err;
270 static void destroy_fsync_dnodes(struct list_head *head)
272 struct fsync_inode_entry *entry, *tmp;
274 list_for_each_entry_safe(entry, tmp, head, list)
275 del_fsync_inode(entry);
278 static int check_index_in_prev_nodes(struct f2fs_sb_info *sbi,
279 block_t blkaddr, struct dnode_of_data *dn)
281 struct seg_entry *sentry;
282 unsigned int segno = GET_SEGNO(sbi, blkaddr);
283 unsigned short blkoff = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
284 struct f2fs_summary_block *sum_node;
285 struct f2fs_summary sum;
286 struct page *sum_page, *node_page;
287 struct dnode_of_data tdn = *dn;
288 nid_t ino, nid;
289 struct inode *inode;
290 unsigned int offset;
291 block_t bidx;
292 int i;
294 sentry = get_seg_entry(sbi, segno);
295 if (!f2fs_test_bit(blkoff, sentry->cur_valid_map))
296 return 0;
298 /* Get the previous summary */
299 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
300 struct curseg_info *curseg = CURSEG_I(sbi, i);
301 if (curseg->segno == segno) {
302 sum = curseg->sum_blk->entries[blkoff];
303 goto got_it;
307 sum_page = get_sum_page(sbi, segno);
308 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
309 sum = sum_node->entries[blkoff];
310 f2fs_put_page(sum_page, 1);
311 got_it:
312 /* Use the locked dnode page and inode */
313 nid = le32_to_cpu(sum.nid);
314 if (dn->inode->i_ino == nid) {
315 tdn.nid = nid;
316 if (!dn->inode_page_locked)
317 lock_page(dn->inode_page);
318 tdn.node_page = dn->inode_page;
319 tdn.ofs_in_node = le16_to_cpu(sum.ofs_in_node);
320 goto truncate_out;
321 } else if (dn->nid == nid) {
322 tdn.ofs_in_node = le16_to_cpu(sum.ofs_in_node);
323 goto truncate_out;
326 /* Get the node page */
327 node_page = get_node_page(sbi, nid);
328 if (IS_ERR(node_page))
329 return PTR_ERR(node_page);
331 offset = ofs_of_node(node_page);
332 ino = ino_of_node(node_page);
333 f2fs_put_page(node_page, 1);
335 if (ino != dn->inode->i_ino) {
336 /* Deallocate previous index in the node page */
337 inode = f2fs_iget(sbi->sb, ino);
338 if (IS_ERR(inode))
339 return PTR_ERR(inode);
340 } else {
341 inode = dn->inode;
344 bidx = start_bidx_of_node(offset, F2FS_I(inode)) +
345 le16_to_cpu(sum.ofs_in_node);
348 * if inode page is locked, unlock temporarily, but its reference
349 * count keeps alive.
351 if (ino == dn->inode->i_ino && dn->inode_page_locked)
352 unlock_page(dn->inode_page);
354 set_new_dnode(&tdn, inode, NULL, NULL, 0);
355 if (get_dnode_of_data(&tdn, bidx, LOOKUP_NODE))
356 goto out;
358 if (tdn.data_blkaddr == blkaddr)
359 truncate_data_blocks_range(&tdn, 1);
361 f2fs_put_dnode(&tdn);
362 out:
363 if (ino != dn->inode->i_ino)
364 iput(inode);
365 else if (dn->inode_page_locked)
366 lock_page(dn->inode_page);
367 return 0;
369 truncate_out:
370 if (datablock_addr(tdn.node_page, tdn.ofs_in_node) == blkaddr)
371 truncate_data_blocks_range(&tdn, 1);
372 if (dn->inode->i_ino == nid && !dn->inode_page_locked)
373 unlock_page(dn->inode_page);
374 return 0;
377 static int do_recover_data(struct f2fs_sb_info *sbi, struct inode *inode,
378 struct page *page, block_t blkaddr)
380 struct f2fs_inode_info *fi = F2FS_I(inode);
381 unsigned int start, end;
382 struct dnode_of_data dn;
383 struct node_info ni;
384 int err = 0, recovered = 0;
386 /* step 1: recover xattr */
387 if (IS_INODE(page)) {
388 recover_inline_xattr(inode, page);
389 } else if (f2fs_has_xattr_block(ofs_of_node(page))) {
391 * Deprecated; xattr blocks should be found from cold log.
392 * But, we should remain this for backward compatibility.
394 recover_xattr_data(inode, page, blkaddr);
395 goto out;
398 /* step 2: recover inline data */
399 if (recover_inline_data(inode, page))
400 goto out;
402 /* step 3: recover data indices */
403 start = start_bidx_of_node(ofs_of_node(page), fi);
404 end = start + ADDRS_PER_PAGE(page, fi);
406 set_new_dnode(&dn, inode, NULL, NULL, 0);
408 err = get_dnode_of_data(&dn, start, ALLOC_NODE);
409 if (err)
410 goto out;
412 f2fs_wait_on_page_writeback(dn.node_page, NODE);
414 get_node_info(sbi, dn.nid, &ni);
415 f2fs_bug_on(sbi, ni.ino != ino_of_node(page));
417 if (ofs_of_node(dn.node_page) != ofs_of_node(page)) {
418 f2fs_msg(sbi->sb, KERN_WARNING,
419 "Inconsistent ofs_of_node, ino:%lu, ofs:%u, %u",
420 inode->i_ino, ofs_of_node(dn.node_page),
421 ofs_of_node(page));
422 err = -EFAULT;
423 goto err;
426 for (; start < end; start++, dn.ofs_in_node++) {
427 block_t src, dest;
429 src = datablock_addr(dn.node_page, dn.ofs_in_node);
430 dest = datablock_addr(page, dn.ofs_in_node);
432 /* skip recovering if dest is the same as src */
433 if (src == dest)
434 continue;
436 /* dest is invalid, just invalidate src block */
437 if (dest == NULL_ADDR) {
438 truncate_data_blocks_range(&dn, 1);
439 continue;
443 * dest is reserved block, invalidate src block
444 * and then reserve one new block in dnode page.
446 if (dest == NEW_ADDR) {
447 truncate_data_blocks_range(&dn, 1);
448 err = reserve_new_block(&dn);
449 f2fs_bug_on(sbi, err);
450 continue;
453 /* dest is valid block, try to recover from src to dest */
454 if (f2fs_is_valid_blkaddr(sbi, dest, META_POR)) {
456 if (src == NULL_ADDR) {
457 err = reserve_new_block(&dn);
458 /* We should not get -ENOSPC */
459 f2fs_bug_on(sbi, err);
462 /* Check the previous node page having this index */
463 err = check_index_in_prev_nodes(sbi, dest, &dn);
464 if (err)
465 goto err;
467 /* write dummy data page */
468 f2fs_replace_block(sbi, &dn, src, dest,
469 ni.version, false);
470 recovered++;
474 if (IS_INODE(dn.node_page))
475 sync_inode_page(&dn);
477 copy_node_footer(dn.node_page, page);
478 fill_node_footer(dn.node_page, dn.nid, ni.ino,
479 ofs_of_node(page), false);
480 set_page_dirty(dn.node_page);
481 err:
482 f2fs_put_dnode(&dn);
483 out:
484 f2fs_msg(sbi->sb, KERN_NOTICE,
485 "recover_data: ino = %lx, recovered = %d blocks, err = %d",
486 inode->i_ino, recovered, err);
487 return err;
490 static int recover_data(struct f2fs_sb_info *sbi, struct list_head *inode_list,
491 struct list_head *dir_list)
493 struct curseg_info *curseg;
494 struct page *page = NULL;
495 int err = 0;
496 block_t blkaddr;
498 /* get node pages in the current segment */
499 curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
500 blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
502 while (1) {
503 struct fsync_inode_entry *entry;
505 if (!f2fs_is_valid_blkaddr(sbi, blkaddr, META_POR))
506 break;
508 ra_meta_pages_cond(sbi, blkaddr);
510 page = get_tmp_page(sbi, blkaddr);
512 if (!is_recoverable_dnode(page)) {
513 f2fs_put_page(page, 1);
514 break;
517 entry = get_fsync_inode(inode_list, ino_of_node(page));
518 if (!entry)
519 goto next;
521 * inode(x) | CP | inode(x) | dnode(F)
522 * In this case, we can lose the latest inode(x).
523 * So, call recover_inode for the inode update.
525 if (entry->last_inode == blkaddr)
526 recover_inode(entry->inode, page);
527 if (entry->last_dentry == blkaddr) {
528 err = recover_dentry(entry->inode, page, dir_list);
529 if (err) {
530 f2fs_put_page(page, 1);
531 break;
534 err = do_recover_data(sbi, entry->inode, page, blkaddr);
535 if (err) {
536 f2fs_put_page(page, 1);
537 break;
540 if (entry->blkaddr == blkaddr)
541 del_fsync_inode(entry);
542 next:
543 /* check next segment */
544 blkaddr = next_blkaddr_of_node(page);
545 f2fs_put_page(page, 1);
547 if (!err)
548 allocate_new_segments(sbi);
549 return err;
552 int recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only)
554 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
555 struct list_head inode_list;
556 struct list_head dir_list;
557 block_t blkaddr;
558 int err;
559 int ret = 0;
560 bool need_writecp = false;
562 fsync_entry_slab = f2fs_kmem_cache_create("f2fs_fsync_inode_entry",
563 sizeof(struct fsync_inode_entry));
564 if (!fsync_entry_slab)
565 return -ENOMEM;
567 INIT_LIST_HEAD(&inode_list);
568 INIT_LIST_HEAD(&dir_list);
570 /* prevent checkpoint */
571 mutex_lock(&sbi->cp_mutex);
573 blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
575 /* step #1: find fsynced inode numbers */
576 err = find_fsync_dnodes(sbi, &inode_list);
577 if (err || list_empty(&inode_list))
578 goto out;
580 if (check_only) {
581 ret = 1;
582 goto out;
585 need_writecp = true;
587 /* step #2: recover data */
588 err = recover_data(sbi, &inode_list, &dir_list);
589 if (!err)
590 f2fs_bug_on(sbi, !list_empty(&inode_list));
591 out:
592 destroy_fsync_dnodes(&inode_list);
594 /* truncate meta pages to be used by the recovery */
595 truncate_inode_pages_range(META_MAPPING(sbi),
596 (loff_t)MAIN_BLKADDR(sbi) << PAGE_CACHE_SHIFT, -1);
598 if (err) {
599 truncate_inode_pages_final(NODE_MAPPING(sbi));
600 truncate_inode_pages_final(META_MAPPING(sbi));
603 clear_sbi_flag(sbi, SBI_POR_DOING);
604 if (err)
605 set_ckpt_flags(sbi->ckpt, CP_ERROR_FLAG);
606 mutex_unlock(&sbi->cp_mutex);
608 /* let's drop all the directory inodes for clean checkpoint */
609 destroy_fsync_dnodes(&dir_list);
611 if (!err && need_writecp) {
612 struct cp_control cpc = {
613 .reason = CP_RECOVERY,
615 write_checkpoint(sbi, &cpc);
618 kmem_cache_destroy(fsync_entry_slab);
619 return ret ? ret: err;