f2fs: don't do checkpoint if error is occurred
[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         void *kaddr = page_address(ipage);
44         struct f2fs_node *raw_node = (struct f2fs_node *)kaddr;
45         struct f2fs_inode *raw_inode = &(raw_node->i);
46         nid_t pino = le32_to_cpu(raw_inode->i_pino);
47         struct qstr name;
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         if (f2fs_find_entry(dir, &name, &page)) {
66                 kunmap(page);
67                 f2fs_put_page(page, 0);
68         } else {
69                 err = __f2fs_add_link(dir, &name, inode);
70         }
71 out:
72         f2fs_msg(inode->i_sb, KERN_NOTICE, "recover_inode and its dentry: "
73                         "ino = %x, name = %s, dir = %lx, err = %d",
74                         ino_of_node(ipage), raw_inode->i_name, dir->i_ino, err);
75         return err;
76 }
77
78 static 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
84         if (!IS_INODE(node_page))
85                 return 0;
86
87         inode->i_mode = le16_to_cpu(raw_inode->i_mode);
88         i_size_write(inode, le64_to_cpu(raw_inode->i_size));
89         inode->i_atime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
90         inode->i_ctime.tv_sec = le64_to_cpu(raw_inode->i_ctime);
91         inode->i_mtime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
92         inode->i_atime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec);
93         inode->i_ctime.tv_nsec = le32_to_cpu(raw_inode->i_ctime_nsec);
94         inode->i_mtime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec);
95
96         if (is_dent_dnode(node_page))
97                 return recover_dentry(node_page, inode);
98
99         f2fs_msg(inode->i_sb, KERN_NOTICE, "recover_inode: ino = %x, name = %s",
100                         ino_of_node(node_page), raw_inode->i_name);
101         return 0;
102 }
103
104 static int find_fsync_dnodes(struct f2fs_sb_info *sbi, struct list_head *head)
105 {
106         unsigned long long cp_ver = le64_to_cpu(sbi->ckpt->checkpoint_ver);
107         struct curseg_info *curseg;
108         struct page *page;
109         block_t blkaddr;
110         int err = 0;
111
112         /* get node pages in the current segment */
113         curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
114         blkaddr = START_BLOCK(sbi, curseg->segno) + curseg->next_blkoff;
115
116         /* read node page */
117         page = alloc_page(GFP_F2FS_ZERO);
118         if (IS_ERR(page))
119                 return PTR_ERR(page);
120         lock_page(page);
121
122         while (1) {
123                 struct fsync_inode_entry *entry;
124
125                 err = f2fs_readpage(sbi, page, blkaddr, READ_SYNC);
126                 if (err)
127                         goto out;
128
129                 lock_page(page);
130
131                 if (cp_ver != cpver_of_node(page))
132                         break;
133
134                 if (!is_fsync_dnode(page))
135                         goto next;
136
137                 entry = get_fsync_inode(head, ino_of_node(page));
138                 if (entry) {
139                         if (IS_INODE(page) && is_dent_dnode(page))
140                                 set_inode_flag(F2FS_I(entry->inode),
141                                                         FI_INC_LINK);
142                 } else {
143                         if (IS_INODE(page) && is_dent_dnode(page)) {
144                                 err = recover_inode_page(sbi, page);
145                                 if (err)
146                                         break;
147                         }
148
149                         /* add this fsync inode to the list */
150                         entry = kmem_cache_alloc(fsync_entry_slab, GFP_NOFS);
151                         if (!entry) {
152                                 err = -ENOMEM;
153                                 break;
154                         }
155
156                         entry->inode = f2fs_iget(sbi->sb, ino_of_node(page));
157                         if (IS_ERR(entry->inode)) {
158                                 err = PTR_ERR(entry->inode);
159                                 kmem_cache_free(fsync_entry_slab, entry);
160                                 break;
161                         }
162                         list_add_tail(&entry->list, head);
163                 }
164                 entry->blkaddr = blkaddr;
165
166                 err = recover_inode(entry->inode, page);
167                 if (err && err != -ENOENT)
168                         break;
169 next:
170                 /* check next segment */
171                 blkaddr = next_blkaddr_of_node(page);
172         }
173         unlock_page(page);
174 out:
175         __free_pages(page, 0);
176         return err;
177 }
178
179 static void destroy_fsync_dnodes(struct f2fs_sb_info *sbi,
180                                         struct list_head *head)
181 {
182         struct fsync_inode_entry *entry, *tmp;
183
184         list_for_each_entry_safe(entry, tmp, head, list) {
185                 iput(entry->inode);
186                 list_del(&entry->list);
187                 kmem_cache_free(fsync_entry_slab, entry);
188         }
189 }
190
191 static void check_index_in_prev_nodes(struct f2fs_sb_info *sbi,
192                                                 block_t blkaddr)
193 {
194         struct seg_entry *sentry;
195         unsigned int segno = GET_SEGNO(sbi, blkaddr);
196         unsigned short blkoff = GET_SEGOFF_FROM_SEG0(sbi, blkaddr) &
197                                         (sbi->blocks_per_seg - 1);
198         struct f2fs_summary sum;
199         nid_t ino;
200         void *kaddr;
201         struct inode *inode;
202         struct page *node_page;
203         block_t bidx;
204         int i;
205
206         sentry = get_seg_entry(sbi, segno);
207         if (!f2fs_test_bit(blkoff, sentry->cur_valid_map))
208                 return;
209
210         /* Get the previous summary */
211         for (i = CURSEG_WARM_DATA; i <= CURSEG_COLD_DATA; i++) {
212                 struct curseg_info *curseg = CURSEG_I(sbi, i);
213                 if (curseg->segno == segno) {
214                         sum = curseg->sum_blk->entries[blkoff];
215                         break;
216                 }
217         }
218         if (i > CURSEG_COLD_DATA) {
219                 struct page *sum_page = get_sum_page(sbi, segno);
220                 struct f2fs_summary_block *sum_node;
221                 kaddr = page_address(sum_page);
222                 sum_node = (struct f2fs_summary_block *)kaddr;
223                 sum = sum_node->entries[blkoff];
224                 f2fs_put_page(sum_page, 1);
225         }
226
227         /* Get the node page */
228         node_page = get_node_page(sbi, le32_to_cpu(sum.nid));
229         bidx = start_bidx_of_node(ofs_of_node(node_page)) +
230                                 le16_to_cpu(sum.ofs_in_node);
231         ino = ino_of_node(node_page);
232         f2fs_put_page(node_page, 1);
233
234         /* Deallocate previous index in the node page */
235         inode = f2fs_iget(sbi->sb, ino);
236         if (IS_ERR(inode))
237                 return;
238
239         truncate_hole(inode, bidx, bidx + 1);
240         iput(inode);
241 }
242
243 static int do_recover_data(struct f2fs_sb_info *sbi, struct inode *inode,
244                                         struct page *page, block_t blkaddr)
245 {
246         unsigned int start, end;
247         struct dnode_of_data dn;
248         struct f2fs_summary sum;
249         struct node_info ni;
250         int err = 0, recovered = 0;
251         int ilock;
252
253         start = start_bidx_of_node(ofs_of_node(page));
254         if (IS_INODE(page))
255                 end = start + ADDRS_PER_INODE;
256         else
257                 end = start + ADDRS_PER_BLOCK;
258
259         ilock = mutex_lock_op(sbi);
260         set_new_dnode(&dn, inode, NULL, NULL, 0);
261
262         err = get_dnode_of_data(&dn, start, ALLOC_NODE);
263         if (err) {
264                 mutex_unlock_op(sbi, ilock);
265                 return err;
266         }
267
268         wait_on_page_writeback(dn.node_page);
269
270         get_node_info(sbi, dn.nid, &ni);
271         BUG_ON(ni.ino != ino_of_node(page));
272         BUG_ON(ofs_of_node(dn.node_page) != ofs_of_node(page));
273
274         for (; start < end; start++) {
275                 block_t src, dest;
276
277                 src = datablock_addr(dn.node_page, dn.ofs_in_node);
278                 dest = datablock_addr(page, dn.ofs_in_node);
279
280                 if (src != dest && dest != NEW_ADDR && dest != NULL_ADDR) {
281                         if (src == NULL_ADDR) {
282                                 int err = reserve_new_block(&dn);
283                                 /* We should not get -ENOSPC */
284                                 BUG_ON(err);
285                         }
286
287                         /* Check the previous node page having this index */
288                         check_index_in_prev_nodes(sbi, dest);
289
290                         set_summary(&sum, dn.nid, dn.ofs_in_node, ni.version);
291
292                         /* write dummy data page */
293                         recover_data_page(sbi, NULL, &sum, src, dest);
294                         update_extent_cache(dest, &dn);
295                         recovered++;
296                 }
297                 dn.ofs_in_node++;
298         }
299
300         /* write node page in place */
301         set_summary(&sum, dn.nid, 0, 0);
302         if (IS_INODE(dn.node_page))
303                 sync_inode_page(&dn);
304
305         copy_node_footer(dn.node_page, page);
306         fill_node_footer(dn.node_page, dn.nid, ni.ino,
307                                         ofs_of_node(page), false);
308         set_page_dirty(dn.node_page);
309
310         recover_node_page(sbi, dn.node_page, &sum, &ni, blkaddr);
311         f2fs_put_dnode(&dn);
312         mutex_unlock_op(sbi, ilock);
313
314         f2fs_msg(sbi->sb, KERN_NOTICE, "recover_data: ino = %lx, "
315                         "recovered_data = %d blocks",
316                         inode->i_ino, recovered);
317         return 0;
318 }
319
320 static int recover_data(struct f2fs_sb_info *sbi,
321                                 struct list_head *head, int type)
322 {
323         unsigned long long cp_ver = le64_to_cpu(sbi->ckpt->checkpoint_ver);
324         struct curseg_info *curseg;
325         struct page *page;
326         int err = 0;
327         block_t blkaddr;
328
329         /* get node pages in the current segment */
330         curseg = CURSEG_I(sbi, type);
331         blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
332
333         /* read node page */
334         page = alloc_page(GFP_NOFS | __GFP_ZERO);
335         if (IS_ERR(page))
336                 return -ENOMEM;
337
338         lock_page(page);
339
340         while (1) {
341                 struct fsync_inode_entry *entry;
342
343                 err = f2fs_readpage(sbi, page, blkaddr, READ_SYNC);
344                 if (err)
345                         goto out;
346
347                 lock_page(page);
348
349                 if (cp_ver != cpver_of_node(page))
350                         break;
351
352                 entry = get_fsync_inode(head, ino_of_node(page));
353                 if (!entry)
354                         goto next;
355
356                 err = do_recover_data(sbi, entry->inode, page, blkaddr);
357                 if (err)
358                         break;
359
360                 if (entry->blkaddr == blkaddr) {
361                         iput(entry->inode);
362                         list_del(&entry->list);
363                         kmem_cache_free(fsync_entry_slab, entry);
364                 }
365 next:
366                 /* check next segment */
367                 blkaddr = next_blkaddr_of_node(page);
368         }
369         unlock_page(page);
370 out:
371         __free_pages(page, 0);
372
373         if (!err)
374                 allocate_new_segments(sbi);
375         return err;
376 }
377
378 int recover_fsync_data(struct f2fs_sb_info *sbi)
379 {
380         struct list_head inode_list;
381         int err;
382
383         fsync_entry_slab = f2fs_kmem_cache_create("f2fs_fsync_inode_entry",
384                         sizeof(struct fsync_inode_entry), NULL);
385         if (unlikely(!fsync_entry_slab))
386                 return -ENOMEM;
387
388         INIT_LIST_HEAD(&inode_list);
389
390         /* step #1: find fsynced inode numbers */
391         sbi->por_doing = 1;
392         err = find_fsync_dnodes(sbi, &inode_list);
393         if (err)
394                 goto out;
395
396         if (list_empty(&inode_list))
397                 goto out;
398
399         /* step #2: recover data */
400         err = recover_data(sbi, &inode_list, CURSEG_WARM_NODE);
401         BUG_ON(!list_empty(&inode_list));
402 out:
403         destroy_fsync_dnodes(sbi, &inode_list);
404         kmem_cache_destroy(fsync_entry_slab);
405         sbi->por_doing = 0;
406         if (!err)
407                 write_checkpoint(sbi, false);
408         return err;
409 }