f2fs: add debug msgs in the recovery routine
[platform/adaptation/renesas_rcar/renesas_kernel.git] / fs / f2fs / recovery.c
1 /*
2  * 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
17 static struct kmem_cache *fsync_entry_slab;
18
19 bool 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
27 static 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
41 static 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         nid_t pino = le32_to_cpu(raw_inode->i_pino);
46         struct qstr name;
47         struct f2fs_dir_entry *de;
48         struct page *page;
49         struct inode *dir;
50         int err = 0;
51
52         dir = check_dirty_dir_inode(F2FS_SB(inode->i_sb), pino);
53         if (!dir) {
54                 dir = f2fs_iget(inode->i_sb, pino);
55                 if (IS_ERR(dir)) {
56                         err = PTR_ERR(dir);
57                         goto out;
58                 }
59                 set_inode_flag(F2FS_I(dir), FI_DELAY_IPUT);
60         }
61
62         name.len = le32_to_cpu(raw_inode->i_namelen);
63         name.name = raw_inode->i_name;
64
65         de = f2fs_find_entry(dir, &name, &page);
66         if (de) {
67                 kunmap(page);
68                 f2fs_put_page(page, 0);
69         } else {
70                 err = __f2fs_add_link(dir, &name, inode);
71         }
72 out:
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);
76         kunmap(ipage);
77         return err;
78 }
79
80 static int recover_inode(struct inode *inode, struct page *node_page)
81 {
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);
85
86         if (!IS_INODE(node_page))
87                 return 0;
88
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);
97
98         if (is_dent_dnode(node_page))
99                 return recover_dentry(node_page, inode);
100
101         f2fs_msg(inode->i_sb, KERN_NOTICE, "recover_inode: ino = %x, name = %s",
102                         ino_of_node(node_page), raw_inode->i_name);
103         return 0;
104 }
105
106 static int find_fsync_dnodes(struct f2fs_sb_info *sbi, struct list_head *head)
107 {
108         unsigned long long cp_ver = le64_to_cpu(sbi->ckpt->checkpoint_ver);
109         struct curseg_info *curseg;
110         struct page *page;
111         block_t blkaddr;
112         int err = 0;
113
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;
117
118         /* read node page */
119         page = alloc_page(GFP_F2FS_ZERO);
120         if (IS_ERR(page))
121                 return PTR_ERR(page);
122         lock_page(page);
123
124         while (1) {
125                 struct fsync_inode_entry *entry;
126
127                 err = f2fs_readpage(sbi, page, blkaddr, READ_SYNC);
128                 if (err)
129                         goto out;
130
131                 lock_page(page);
132
133                 if (cp_ver != cpver_of_node(page))
134                         break;
135
136                 if (!is_fsync_dnode(page))
137                         goto next;
138
139                 entry = get_fsync_inode(head, ino_of_node(page));
140                 if (entry) {
141                         if (IS_INODE(page) && is_dent_dnode(page))
142                                 set_inode_flag(F2FS_I(entry->inode),
143                                                         FI_INC_LINK);
144                 } else {
145                         if (IS_INODE(page) && is_dent_dnode(page)) {
146                                 err = recover_inode_page(sbi, page);
147                                 if (err)
148                                         break;
149                         }
150
151                         /* add this fsync inode to the list */
152                         entry = kmem_cache_alloc(fsync_entry_slab, GFP_NOFS);
153                         if (!entry) {
154                                 err = -ENOMEM;
155                                 break;
156                         }
157
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);
162                                 break;
163                         }
164                         list_add_tail(&entry->list, head);
165                 }
166                 entry->blkaddr = blkaddr;
167
168                 err = recover_inode(entry->inode, page);
169                 if (err && err != -ENOENT)
170                         break;
171 next:
172                 /* check next segment */
173                 blkaddr = next_blkaddr_of_node(page);
174         }
175         unlock_page(page);
176 out:
177         __free_pages(page, 0);
178         return err;
179 }
180
181 static void destroy_fsync_dnodes(struct f2fs_sb_info *sbi,
182                                         struct list_head *head)
183 {
184         struct fsync_inode_entry *entry, *tmp;
185
186         list_for_each_entry_safe(entry, tmp, head, list) {
187                 iput(entry->inode);
188                 list_del(&entry->list);
189                 kmem_cache_free(fsync_entry_slab, entry);
190         }
191 }
192
193 static void check_index_in_prev_nodes(struct f2fs_sb_info *sbi,
194                                                 block_t blkaddr)
195 {
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;
201         nid_t ino;
202         void *kaddr;
203         struct inode *inode;
204         struct page *node_page;
205         block_t bidx;
206         int i;
207
208         sentry = get_seg_entry(sbi, segno);
209         if (!f2fs_test_bit(blkoff, sentry->cur_valid_map))
210                 return;
211
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];
217                         break;
218                 }
219         }
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);
227         }
228
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);
235
236         /* Deallocate previous index in the node page */
237         inode = f2fs_iget(sbi->sb, ino);
238         if (IS_ERR(inode))
239                 return;
240
241         truncate_hole(inode, bidx, bidx + 1);
242         iput(inode);
243 }
244
245 static int do_recover_data(struct f2fs_sb_info *sbi, struct inode *inode,
246                                         struct page *page, block_t blkaddr)
247 {
248         unsigned int start, end;
249         struct dnode_of_data dn;
250         struct f2fs_summary sum;
251         struct node_info ni;
252         int err = 0, recovered = 0;
253         int ilock;
254
255         start = start_bidx_of_node(ofs_of_node(page));
256         if (IS_INODE(page))
257                 end = start + ADDRS_PER_INODE;
258         else
259                 end = start + ADDRS_PER_BLOCK;
260
261         ilock = mutex_lock_op(sbi);
262         set_new_dnode(&dn, inode, NULL, NULL, 0);
263
264         err = get_dnode_of_data(&dn, start, ALLOC_NODE);
265         if (err) {
266                 mutex_unlock_op(sbi, ilock);
267                 return err;
268         }
269
270         wait_on_page_writeback(dn.node_page);
271
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));
275
276         for (; start < end; start++) {
277                 block_t src, dest;
278
279                 src = datablock_addr(dn.node_page, dn.ofs_in_node);
280                 dest = datablock_addr(page, dn.ofs_in_node);
281
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 */
286                                 BUG_ON(err);
287                         }
288
289                         /* Check the previous node page having this index */
290                         check_index_in_prev_nodes(sbi, dest);
291
292                         set_summary(&sum, dn.nid, dn.ofs_in_node, ni.version);
293
294                         /* write dummy data page */
295                         recover_data_page(sbi, NULL, &sum, src, dest);
296                         update_extent_cache(dest, &dn);
297                         recovered++;
298                 }
299                 dn.ofs_in_node++;
300         }
301
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);
306
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);
311
312         recover_node_page(sbi, dn.node_page, &sum, &ni, blkaddr);
313         f2fs_put_dnode(&dn);
314         mutex_unlock_op(sbi, ilock);
315
316         f2fs_msg(sbi->sb, KERN_NOTICE, "recover_data: ino = %lx, "
317                         "recovered_data = %d blocks",
318                         inode->i_ino, recovered);
319         return 0;
320 }
321
322 static int recover_data(struct f2fs_sb_info *sbi,
323                                 struct list_head *head, int type)
324 {
325         unsigned long long cp_ver = le64_to_cpu(sbi->ckpt->checkpoint_ver);
326         struct curseg_info *curseg;
327         struct page *page;
328         int err = 0;
329         block_t blkaddr;
330
331         /* get node pages in the current segment */
332         curseg = CURSEG_I(sbi, type);
333         blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
334
335         /* read node page */
336         page = alloc_page(GFP_NOFS | __GFP_ZERO);
337         if (IS_ERR(page))
338                 return -ENOMEM;
339
340         lock_page(page);
341
342         while (1) {
343                 struct fsync_inode_entry *entry;
344
345                 err = f2fs_readpage(sbi, page, blkaddr, READ_SYNC);
346                 if (err)
347                         goto out;
348
349                 lock_page(page);
350
351                 if (cp_ver != cpver_of_node(page))
352                         goto unlock_out;
353
354                 entry = get_fsync_inode(head, ino_of_node(page));
355                 if (!entry)
356                         goto next;
357
358                 err = do_recover_data(sbi, entry->inode, page, blkaddr);
359                 if (err)
360                         goto out;
361
362                 if (entry->blkaddr == blkaddr) {
363                         iput(entry->inode);
364                         list_del(&entry->list);
365                         kmem_cache_free(fsync_entry_slab, entry);
366                 }
367 next:
368                 /* check next segment */
369                 blkaddr = next_blkaddr_of_node(page);
370         }
371 unlock_out:
372         unlock_page(page);
373 out:
374         __free_pages(page, 0);
375
376         if (!err)
377                 allocate_new_segments(sbi);
378         return err;
379 }
380
381 int recover_fsync_data(struct f2fs_sb_info *sbi)
382 {
383         struct list_head inode_list;
384         int err;
385
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))
389                 return -ENOMEM;
390
391         INIT_LIST_HEAD(&inode_list);
392
393         /* step #1: find fsynced inode numbers */
394         sbi->por_doing = 1;
395         err = find_fsync_dnodes(sbi, &inode_list);
396         if (err)
397                 goto out;
398
399         if (list_empty(&inode_list))
400                 goto out;
401
402         /* step #2: recover data */
403         err = recover_data(sbi, &inode_list, CURSEG_WARM_NODE);
404         BUG_ON(!list_empty(&inode_list));
405 out:
406         destroy_fsync_dnodes(sbi, &inode_list);
407         kmem_cache_destroy(fsync_entry_slab);
408         sbi->por_doing = 0;
409         write_checkpoint(sbi, false);
410         return err;
411 }