nilfs2: rename nilfs_recover_logical_segments function
[pandora-kernel.git] / fs / nilfs2 / recovery.c
1 /*
2  * recovery.c - NILFS recovery logic
3  *
4  * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * Written by Ryusuke Konishi <ryusuke@osrg.net>
21  */
22
23 #include <linux/buffer_head.h>
24 #include <linux/blkdev.h>
25 #include <linux/swap.h>
26 #include <linux/slab.h>
27 #include <linux/crc32.h>
28 #include "nilfs.h"
29 #include "segment.h"
30 #include "sufile.h"
31 #include "page.h"
32 #include "segbuf.h"
33
34 /*
35  * Segment check result
36  */
37 enum {
38         NILFS_SEG_VALID,
39         NILFS_SEG_NO_SUPER_ROOT,
40         NILFS_SEG_FAIL_IO,
41         NILFS_SEG_FAIL_MAGIC,
42         NILFS_SEG_FAIL_SEQ,
43         NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT,
44         NILFS_SEG_FAIL_CHECKSUM_FULL,
45         NILFS_SEG_FAIL_CONSISTENCY,
46 };
47
48 /* work structure for recovery */
49 struct nilfs_recovery_block {
50         ino_t ino;              /* Inode number of the file that this block
51                                    belongs to */
52         sector_t blocknr;       /* block number */
53         __u64 vblocknr;         /* virtual block number */
54         unsigned long blkoff;   /* File offset of the data block (per block) */
55         struct list_head list;
56 };
57
58
59 static int nilfs_warn_segment_error(int err)
60 {
61         switch (err) {
62         case NILFS_SEG_FAIL_IO:
63                 printk(KERN_WARNING
64                        "NILFS warning: I/O error on loading last segment\n");
65                 return -EIO;
66         case NILFS_SEG_FAIL_MAGIC:
67                 printk(KERN_WARNING
68                        "NILFS warning: Segment magic number invalid\n");
69                 break;
70         case NILFS_SEG_FAIL_SEQ:
71                 printk(KERN_WARNING
72                        "NILFS warning: Sequence number mismatch\n");
73                 break;
74         case NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT:
75                 printk(KERN_WARNING
76                        "NILFS warning: Checksum error in super root\n");
77                 break;
78         case NILFS_SEG_FAIL_CHECKSUM_FULL:
79                 printk(KERN_WARNING
80                        "NILFS warning: Checksum error in segment payload\n");
81                 break;
82         case NILFS_SEG_FAIL_CONSISTENCY:
83                 printk(KERN_WARNING
84                        "NILFS warning: Inconsistent segment\n");
85                 break;
86         case NILFS_SEG_NO_SUPER_ROOT:
87                 printk(KERN_WARNING
88                        "NILFS warning: No super root in the last segment\n");
89                 break;
90         }
91         return -EINVAL;
92 }
93
94 static void store_segsum_info(struct nilfs_segsum_info *ssi,
95                               struct nilfs_segment_summary *sum,
96                               unsigned int blocksize)
97 {
98         ssi->flags = le16_to_cpu(sum->ss_flags);
99         ssi->seg_seq = le64_to_cpu(sum->ss_seq);
100         ssi->ctime = le64_to_cpu(sum->ss_create);
101         ssi->next = le64_to_cpu(sum->ss_next);
102         ssi->nblocks = le32_to_cpu(sum->ss_nblocks);
103         ssi->nfinfo = le32_to_cpu(sum->ss_nfinfo);
104         ssi->sumbytes = le32_to_cpu(sum->ss_sumbytes);
105
106         ssi->nsumblk = DIV_ROUND_UP(ssi->sumbytes, blocksize);
107         ssi->nfileblk = ssi->nblocks - ssi->nsumblk - !!NILFS_SEG_HAS_SR(ssi);
108
109         /* need to verify ->ss_bytes field if read ->ss_cno */
110 }
111
112 /**
113  * nilfs_compute_checksum - compute checksum of blocks continuously
114  * @nilfs: nilfs object
115  * @bhs: buffer head of start block
116  * @sum: place to store result
117  * @offset: offset bytes in the first block
118  * @check_bytes: number of bytes to be checked
119  * @start: DBN of start block
120  * @nblock: number of blocks to be checked
121  */
122 static int nilfs_compute_checksum(struct the_nilfs *nilfs,
123                                   struct buffer_head *bhs, u32 *sum,
124                                   unsigned long offset, u64 check_bytes,
125                                   sector_t start, unsigned long nblock)
126 {
127         unsigned int blocksize = nilfs->ns_blocksize;
128         unsigned long size;
129         u32 crc;
130
131         BUG_ON(offset >= blocksize);
132         check_bytes -= offset;
133         size = min_t(u64, check_bytes, blocksize - offset);
134         crc = crc32_le(nilfs->ns_crc_seed,
135                        (unsigned char *)bhs->b_data + offset, size);
136         if (--nblock > 0) {
137                 do {
138                         struct buffer_head *bh;
139
140                         bh = __bread(nilfs->ns_bdev, ++start, blocksize);
141                         if (!bh)
142                                 return -EIO;
143                         check_bytes -= size;
144                         size = min_t(u64, check_bytes, blocksize);
145                         crc = crc32_le(crc, bh->b_data, size);
146                         brelse(bh);
147                 } while (--nblock > 0);
148         }
149         *sum = crc;
150         return 0;
151 }
152
153 /**
154  * nilfs_read_super_root_block - read super root block
155  * @nilfs: nilfs object
156  * @sr_block: disk block number of the super root block
157  * @pbh: address of a buffer_head pointer to return super root buffer
158  * @check: CRC check flag
159  */
160 int nilfs_read_super_root_block(struct the_nilfs *nilfs, sector_t sr_block,
161                                 struct buffer_head **pbh, int check)
162 {
163         struct buffer_head *bh_sr;
164         struct nilfs_super_root *sr;
165         u32 crc;
166         int ret;
167
168         *pbh = NULL;
169         bh_sr = __bread(nilfs->ns_bdev, sr_block, nilfs->ns_blocksize);
170         if (unlikely(!bh_sr)) {
171                 ret = NILFS_SEG_FAIL_IO;
172                 goto failed;
173         }
174
175         sr = (struct nilfs_super_root *)bh_sr->b_data;
176         if (check) {
177                 unsigned bytes = le16_to_cpu(sr->sr_bytes);
178
179                 if (bytes == 0 || bytes > nilfs->ns_blocksize) {
180                         ret = NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT;
181                         goto failed_bh;
182                 }
183                 if (nilfs_compute_checksum(
184                             nilfs, bh_sr, &crc, sizeof(sr->sr_sum), bytes,
185                             sr_block, 1)) {
186                         ret = NILFS_SEG_FAIL_IO;
187                         goto failed_bh;
188                 }
189                 if (crc != le32_to_cpu(sr->sr_sum)) {
190                         ret = NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT;
191                         goto failed_bh;
192                 }
193         }
194         *pbh = bh_sr;
195         return 0;
196
197  failed_bh:
198         brelse(bh_sr);
199
200  failed:
201         return nilfs_warn_segment_error(ret);
202 }
203
204 /**
205  * load_segment_summary - read segment summary of the specified partial segment
206  * @nilfs: nilfs object
207  * @pseg_start: start disk block number of partial segment
208  * @seg_seq: sequence number requested
209  * @ssi: pointer to nilfs_segsum_info struct to store information
210  */
211 static int
212 load_segment_summary(struct the_nilfs *nilfs, sector_t pseg_start,
213                      u64 seg_seq, struct nilfs_segsum_info *ssi)
214 {
215         struct buffer_head *bh_sum;
216         struct nilfs_segment_summary *sum;
217         unsigned long nblock;
218         u32 crc;
219         int ret = NILFS_SEG_FAIL_IO;
220
221         bh_sum = __bread(nilfs->ns_bdev, pseg_start, nilfs->ns_blocksize);
222         if (!bh_sum)
223                 goto out;
224
225         sum = (struct nilfs_segment_summary *)bh_sum->b_data;
226
227         /* Check consistency of segment summary */
228         if (le32_to_cpu(sum->ss_magic) != NILFS_SEGSUM_MAGIC) {
229                 ret = NILFS_SEG_FAIL_MAGIC;
230                 goto failed;
231         }
232         store_segsum_info(ssi, sum, nilfs->ns_blocksize);
233         if (seg_seq != ssi->seg_seq) {
234                 ret = NILFS_SEG_FAIL_SEQ;
235                 goto failed;
236         }
237
238         nblock = ssi->nblocks;
239         if (unlikely(nblock == 0 || nblock > nilfs->ns_blocks_per_segment)) {
240                 /* This limits the number of blocks read in the CRC check */
241                 ret = NILFS_SEG_FAIL_CONSISTENCY;
242                 goto failed;
243         }
244         if (nilfs_compute_checksum(nilfs, bh_sum, &crc, sizeof(sum->ss_datasum),
245                                    ((u64)nblock << nilfs->ns_blocksize_bits),
246                                    pseg_start, nblock)) {
247                 ret = NILFS_SEG_FAIL_IO;
248                 goto failed;
249         }
250         if (crc == le32_to_cpu(sum->ss_datasum))
251                 ret = 0;
252         else
253                 ret = NILFS_SEG_FAIL_CHECKSUM_FULL;
254  failed:
255         brelse(bh_sum);
256  out:
257         return ret;
258 }
259
260 /**
261  * nilfs_read_summary_info - read an item on summary blocks of a log
262  * @nilfs: nilfs object
263  * @pbh: the current buffer head on summary blocks [in, out]
264  * @offset: the current byte offset on summary blocks [in, out]
265  * @bytes: byte size of the item to be read
266  */
267 static void *nilfs_read_summary_info(struct the_nilfs *nilfs,
268                                      struct buffer_head **pbh,
269                                      unsigned int *offset, unsigned int bytes)
270 {
271         void *ptr;
272         sector_t blocknr;
273
274         BUG_ON((*pbh)->b_size < *offset);
275         if (bytes > (*pbh)->b_size - *offset) {
276                 blocknr = (*pbh)->b_blocknr;
277                 brelse(*pbh);
278                 *pbh = __bread(nilfs->ns_bdev, blocknr + 1,
279                                nilfs->ns_blocksize);
280                 if (unlikely(!*pbh))
281                         return NULL;
282                 *offset = 0;
283         }
284         ptr = (*pbh)->b_data + *offset;
285         *offset += bytes;
286         return ptr;
287 }
288
289 /**
290  * nilfs_skip_summary_info - skip items on summary blocks of a log
291  * @nilfs: nilfs object
292  * @pbh: the current buffer head on summary blocks [in, out]
293  * @offset: the current byte offset on summary blocks [in, out]
294  * @bytes: byte size of the item to be skipped
295  * @count: number of items to be skipped
296  */
297 static void nilfs_skip_summary_info(struct the_nilfs *nilfs,
298                                     struct buffer_head **pbh,
299                                     unsigned int *offset, unsigned int bytes,
300                                     unsigned long count)
301 {
302         unsigned int rest_item_in_current_block
303                 = ((*pbh)->b_size - *offset) / bytes;
304
305         if (count <= rest_item_in_current_block) {
306                 *offset += bytes * count;
307         } else {
308                 sector_t blocknr = (*pbh)->b_blocknr;
309                 unsigned int nitem_per_block = (*pbh)->b_size / bytes;
310                 unsigned int bcnt;
311
312                 count -= rest_item_in_current_block;
313                 bcnt = DIV_ROUND_UP(count, nitem_per_block);
314                 *offset = bytes * (count - (bcnt - 1) * nitem_per_block);
315
316                 brelse(*pbh);
317                 *pbh = __bread(nilfs->ns_bdev, blocknr + bcnt,
318                                nilfs->ns_blocksize);
319         }
320 }
321
322 /**
323  * nilfs_scan_dsync_log - get block information of a log written for data sync
324  * @nilfs: nilfs object
325  * @start_blocknr: start block number of the log
326  * @ssi: log summary information
327  * @head: list head to add nilfs_recovery_block struct
328  */
329 static int nilfs_scan_dsync_log(struct the_nilfs *nilfs, sector_t start_blocknr,
330                                 struct nilfs_segsum_info *ssi,
331                                 struct list_head *head)
332 {
333         struct buffer_head *bh;
334         unsigned int offset;
335         unsigned long nfinfo = ssi->nfinfo;
336         sector_t blocknr = start_blocknr + ssi->nsumblk;
337         ino_t ino;
338         int err = -EIO;
339
340         if (!nfinfo)
341                 return 0;
342
343         bh = __bread(nilfs->ns_bdev, start_blocknr, nilfs->ns_blocksize);
344         if (unlikely(!bh))
345                 goto out;
346
347         offset = le16_to_cpu(
348                 ((struct nilfs_segment_summary *)bh->b_data)->ss_bytes);
349         for (;;) {
350                 unsigned long nblocks, ndatablk, nnodeblk;
351                 struct nilfs_finfo *finfo;
352
353                 finfo = nilfs_read_summary_info(nilfs, &bh, &offset,
354                                                 sizeof(*finfo));
355                 if (unlikely(!finfo))
356                         goto out;
357
358                 ino = le64_to_cpu(finfo->fi_ino);
359                 nblocks = le32_to_cpu(finfo->fi_nblocks);
360                 ndatablk = le32_to_cpu(finfo->fi_ndatablk);
361                 nnodeblk = nblocks - ndatablk;
362
363                 while (ndatablk-- > 0) {
364                         struct nilfs_recovery_block *rb;
365                         struct nilfs_binfo_v *binfo;
366
367                         binfo = nilfs_read_summary_info(nilfs, &bh, &offset,
368                                                         sizeof(*binfo));
369                         if (unlikely(!binfo))
370                                 goto out;
371
372                         rb = kmalloc(sizeof(*rb), GFP_NOFS);
373                         if (unlikely(!rb)) {
374                                 err = -ENOMEM;
375                                 goto out;
376                         }
377                         rb->ino = ino;
378                         rb->blocknr = blocknr++;
379                         rb->vblocknr = le64_to_cpu(binfo->bi_vblocknr);
380                         rb->blkoff = le64_to_cpu(binfo->bi_blkoff);
381                         /* INIT_LIST_HEAD(&rb->list); */
382                         list_add_tail(&rb->list, head);
383                 }
384                 if (--nfinfo == 0)
385                         break;
386                 blocknr += nnodeblk; /* always 0 for data sync logs */
387                 nilfs_skip_summary_info(nilfs, &bh, &offset, sizeof(__le64),
388                                         nnodeblk);
389                 if (unlikely(!bh))
390                         goto out;
391         }
392         err = 0;
393  out:
394         brelse(bh);   /* brelse(NULL) is just ignored */
395         return err;
396 }
397
398 static void dispose_recovery_list(struct list_head *head)
399 {
400         while (!list_empty(head)) {
401                 struct nilfs_recovery_block *rb
402                         = list_entry(head->next,
403                                      struct nilfs_recovery_block, list);
404                 list_del(&rb->list);
405                 kfree(rb);
406         }
407 }
408
409 struct nilfs_segment_entry {
410         struct list_head        list;
411         __u64                   segnum;
412 };
413
414 static int nilfs_segment_list_add(struct list_head *head, __u64 segnum)
415 {
416         struct nilfs_segment_entry *ent = kmalloc(sizeof(*ent), GFP_NOFS);
417
418         if (unlikely(!ent))
419                 return -ENOMEM;
420
421         ent->segnum = segnum;
422         INIT_LIST_HEAD(&ent->list);
423         list_add_tail(&ent->list, head);
424         return 0;
425 }
426
427 void nilfs_dispose_segment_list(struct list_head *head)
428 {
429         while (!list_empty(head)) {
430                 struct nilfs_segment_entry *ent
431                         = list_entry(head->next,
432                                      struct nilfs_segment_entry, list);
433                 list_del(&ent->list);
434                 kfree(ent);
435         }
436 }
437
438 static int nilfs_prepare_segment_for_recovery(struct the_nilfs *nilfs,
439                                               struct nilfs_sb_info *sbi,
440                                               struct nilfs_recovery_info *ri)
441 {
442         struct list_head *head = &ri->ri_used_segments;
443         struct nilfs_segment_entry *ent, *n;
444         struct inode *sufile = nilfs->ns_sufile;
445         __u64 segnum[4];
446         int err;
447         int i;
448
449         segnum[0] = nilfs->ns_segnum;
450         segnum[1] = nilfs->ns_nextnum;
451         segnum[2] = ri->ri_segnum;
452         segnum[3] = ri->ri_nextnum;
453
454         nilfs_attach_writer(nilfs, sbi);
455         /*
456          * Releasing the next segment of the latest super root.
457          * The next segment is invalidated by this recovery.
458          */
459         err = nilfs_sufile_free(sufile, segnum[1]);
460         if (unlikely(err))
461                 goto failed;
462
463         for (i = 1; i < 4; i++) {
464                 err = nilfs_segment_list_add(head, segnum[i]);
465                 if (unlikely(err))
466                         goto failed;
467         }
468
469         /*
470          * Collecting segments written after the latest super root.
471          * These are marked dirty to avoid being reallocated in the next write.
472          */
473         list_for_each_entry_safe(ent, n, head, list) {
474                 if (ent->segnum != segnum[0]) {
475                         err = nilfs_sufile_scrap(sufile, ent->segnum);
476                         if (unlikely(err))
477                                 goto failed;
478                 }
479                 list_del(&ent->list);
480                 kfree(ent);
481         }
482
483         /* Allocate new segments for recovery */
484         err = nilfs_sufile_alloc(sufile, &segnum[0]);
485         if (unlikely(err))
486                 goto failed;
487
488         nilfs->ns_pseg_offset = 0;
489         nilfs->ns_seg_seq = ri->ri_seq + 2;
490         nilfs->ns_nextnum = nilfs->ns_segnum = segnum[0];
491
492  failed:
493         /* No need to recover sufile because it will be destroyed on error */
494         nilfs_detach_writer(nilfs, sbi);
495         return err;
496 }
497
498 static int nilfs_recovery_copy_block(struct the_nilfs *nilfs,
499                                      struct nilfs_recovery_block *rb,
500                                      struct page *page)
501 {
502         struct buffer_head *bh_org;
503         void *kaddr;
504
505         bh_org = __bread(nilfs->ns_bdev, rb->blocknr, nilfs->ns_blocksize);
506         if (unlikely(!bh_org))
507                 return -EIO;
508
509         kaddr = kmap_atomic(page, KM_USER0);
510         memcpy(kaddr + bh_offset(bh_org), bh_org->b_data, bh_org->b_size);
511         kunmap_atomic(kaddr, KM_USER0);
512         brelse(bh_org);
513         return 0;
514 }
515
516 static int nilfs_recover_dsync_blocks(struct the_nilfs *nilfs,
517                                       struct nilfs_sb_info *sbi,
518                                       struct list_head *head,
519                                       unsigned long *nr_salvaged_blocks)
520 {
521         struct inode *inode;
522         struct nilfs_recovery_block *rb, *n;
523         unsigned blocksize = nilfs->ns_blocksize;
524         struct page *page;
525         loff_t pos;
526         int err = 0, err2 = 0;
527
528         list_for_each_entry_safe(rb, n, head, list) {
529                 inode = nilfs_iget(sbi->s_super, rb->ino);
530                 if (IS_ERR(inode)) {
531                         err = PTR_ERR(inode);
532                         inode = NULL;
533                         goto failed_inode;
534                 }
535
536                 pos = rb->blkoff << inode->i_blkbits;
537                 page = NULL;
538                 err = block_write_begin(NULL, inode->i_mapping, pos, blocksize,
539                                         0, &page, NULL, nilfs_get_block);
540                 if (unlikely(err))
541                         goto failed_inode;
542
543                 err = nilfs_recovery_copy_block(nilfs, rb, page);
544                 if (unlikely(err))
545                         goto failed_page;
546
547                 err = nilfs_set_file_dirty(sbi, inode, 1);
548                 if (unlikely(err))
549                         goto failed_page;
550
551                 block_write_end(NULL, inode->i_mapping, pos, blocksize,
552                                 blocksize, page, NULL);
553
554                 unlock_page(page);
555                 page_cache_release(page);
556
557                 (*nr_salvaged_blocks)++;
558                 goto next;
559
560  failed_page:
561                 unlock_page(page);
562                 page_cache_release(page);
563
564  failed_inode:
565                 printk(KERN_WARNING
566                        "NILFS warning: error recovering data block "
567                        "(err=%d, ino=%lu, block-offset=%llu)\n",
568                        err, (unsigned long)rb->ino,
569                        (unsigned long long)rb->blkoff);
570                 if (!err2)
571                         err2 = err;
572  next:
573                 iput(inode); /* iput(NULL) is just ignored */
574                 list_del_init(&rb->list);
575                 kfree(rb);
576         }
577         return err2;
578 }
579
580 /**
581  * nilfs_do_roll_forward - salvage logical segments newer than the latest
582  * checkpoint
583  * @nilfs: nilfs object
584  * @sbi: nilfs_sb_info
585  * @ri: pointer to a nilfs_recovery_info
586  */
587 static int nilfs_do_roll_forward(struct the_nilfs *nilfs,
588                                  struct nilfs_sb_info *sbi,
589                                  struct nilfs_recovery_info *ri)
590 {
591         struct nilfs_segsum_info ssi;
592         sector_t pseg_start;
593         sector_t seg_start, seg_end;  /* Starting/ending DBN of full segment */
594         unsigned long nsalvaged_blocks = 0;
595         u64 seg_seq;
596         __u64 segnum, nextnum = 0;
597         int empty_seg = 0;
598         int err = 0, ret;
599         LIST_HEAD(dsync_blocks);  /* list of data blocks to be recovered */
600         enum {
601                 RF_INIT_ST,
602                 RF_DSYNC_ST,   /* scanning data-sync segments */
603         };
604         int state = RF_INIT_ST;
605
606         nilfs_attach_writer(nilfs, sbi);
607         pseg_start = ri->ri_lsegs_start;
608         seg_seq = ri->ri_lsegs_start_seq;
609         segnum = nilfs_get_segnum_of_block(nilfs, pseg_start);
610         nilfs_get_segment_range(nilfs, segnum, &seg_start, &seg_end);
611
612         while (segnum != ri->ri_segnum || pseg_start <= ri->ri_pseg_start) {
613
614                 ret = load_segment_summary(nilfs, pseg_start, seg_seq, &ssi);
615                 if (ret) {
616                         if (ret == NILFS_SEG_FAIL_IO) {
617                                 err = -EIO;
618                                 goto failed;
619                         }
620                         goto strayed;
621                 }
622                 if (unlikely(NILFS_SEG_HAS_SR(&ssi)))
623                         goto confused;
624
625                 /* Found a valid partial segment; do recovery actions */
626                 nextnum = nilfs_get_segnum_of_block(nilfs, ssi.next);
627                 empty_seg = 0;
628                 nilfs->ns_ctime = ssi.ctime;
629                 if (!(ssi.flags & NILFS_SS_GC))
630                         nilfs->ns_nongc_ctime = ssi.ctime;
631
632                 switch (state) {
633                 case RF_INIT_ST:
634                         if (!NILFS_SEG_LOGBGN(&ssi) || !NILFS_SEG_DSYNC(&ssi))
635                                 goto try_next_pseg;
636                         state = RF_DSYNC_ST;
637                         /* Fall through */
638                 case RF_DSYNC_ST:
639                         if (!NILFS_SEG_DSYNC(&ssi))
640                                 goto confused;
641
642                         err = nilfs_scan_dsync_log(nilfs, pseg_start, &ssi,
643                                                    &dsync_blocks);
644                         if (unlikely(err))
645                                 goto failed;
646                         if (NILFS_SEG_LOGEND(&ssi)) {
647                                 err = nilfs_recover_dsync_blocks(
648                                         nilfs, sbi, &dsync_blocks,
649                                         &nsalvaged_blocks);
650                                 if (unlikely(err))
651                                         goto failed;
652                                 state = RF_INIT_ST;
653                         }
654                         break; /* Fall through to try_next_pseg */
655                 }
656
657  try_next_pseg:
658                 if (pseg_start == ri->ri_lsegs_end)
659                         break;
660                 pseg_start += ssi.nblocks;
661                 if (pseg_start < seg_end)
662                         continue;
663                 goto feed_segment;
664
665  strayed:
666                 if (pseg_start == ri->ri_lsegs_end)
667                         break;
668
669  feed_segment:
670                 /* Looking to the next full segment */
671                 if (empty_seg++)
672                         break;
673                 seg_seq++;
674                 segnum = nextnum;
675                 nilfs_get_segment_range(nilfs, segnum, &seg_start, &seg_end);
676                 pseg_start = seg_start;
677         }
678
679         if (nsalvaged_blocks) {
680                 printk(KERN_INFO "NILFS (device %s): salvaged %lu blocks\n",
681                        sbi->s_super->s_id, nsalvaged_blocks);
682                 ri->ri_need_recovery = NILFS_RECOVERY_ROLLFORWARD_DONE;
683         }
684  out:
685         dispose_recovery_list(&dsync_blocks);
686         nilfs_detach_writer(nilfs, sbi);
687         return err;
688
689  confused:
690         err = -EINVAL;
691  failed:
692         printk(KERN_ERR
693                "NILFS (device %s): Error roll-forwarding "
694                "(err=%d, pseg block=%llu). ",
695                sbi->s_super->s_id, err, (unsigned long long)pseg_start);
696         goto out;
697 }
698
699 static void nilfs_finish_roll_forward(struct the_nilfs *nilfs,
700                                       struct nilfs_recovery_info *ri)
701 {
702         struct buffer_head *bh;
703         int err;
704
705         if (nilfs_get_segnum_of_block(nilfs, ri->ri_lsegs_start) !=
706             nilfs_get_segnum_of_block(nilfs, ri->ri_super_root))
707                 return;
708
709         bh = __getblk(nilfs->ns_bdev, ri->ri_lsegs_start, nilfs->ns_blocksize);
710         BUG_ON(!bh);
711         memset(bh->b_data, 0, bh->b_size);
712         set_buffer_dirty(bh);
713         err = sync_dirty_buffer(bh);
714         if (unlikely(err))
715                 printk(KERN_WARNING
716                        "NILFS warning: buffer sync write failed during "
717                        "post-cleaning of recovery.\n");
718         brelse(bh);
719 }
720
721 /**
722  * nilfs_salvage_orphan_logs - salvage logs written after the latest checkpoint
723  * @nilfs: nilfs object
724  * @sbi: nilfs_sb_info
725  * @ri: pointer to a nilfs_recovery_info struct to store search results.
726  *
727  * Return Value: On success, 0 is returned.  On error, one of the following
728  * negative error code is returned.
729  *
730  * %-EINVAL - Inconsistent filesystem state.
731  *
732  * %-EIO - I/O error
733  *
734  * %-ENOSPC - No space left on device (only in a panic state).
735  *
736  * %-ERESTARTSYS - Interrupted.
737  *
738  * %-ENOMEM - Insufficient memory available.
739  */
740 int nilfs_salvage_orphan_logs(struct the_nilfs *nilfs,
741                               struct nilfs_sb_info *sbi,
742                               struct nilfs_recovery_info *ri)
743 {
744         int err;
745
746         if (ri->ri_lsegs_start == 0 || ri->ri_lsegs_end == 0)
747                 return 0;
748
749         err = nilfs_attach_checkpoint(sbi, ri->ri_cno);
750         if (unlikely(err)) {
751                 printk(KERN_ERR
752                        "NILFS: error loading the latest checkpoint.\n");
753                 return err;
754         }
755
756         err = nilfs_do_roll_forward(nilfs, sbi, ri);
757         if (unlikely(err))
758                 goto failed;
759
760         if (ri->ri_need_recovery == NILFS_RECOVERY_ROLLFORWARD_DONE) {
761                 err = nilfs_prepare_segment_for_recovery(nilfs, sbi, ri);
762                 if (unlikely(err)) {
763                         printk(KERN_ERR "NILFS: Error preparing segments for "
764                                "recovery.\n");
765                         goto failed;
766                 }
767
768                 err = nilfs_attach_segment_constructor(sbi);
769                 if (unlikely(err))
770                         goto failed;
771
772                 set_nilfs_discontinued(nilfs);
773                 err = nilfs_construct_segment(sbi->s_super);
774                 nilfs_detach_segment_constructor(sbi);
775
776                 if (unlikely(err)) {
777                         printk(KERN_ERR "NILFS: Oops! recovery failed. "
778                                "(err=%d)\n", err);
779                         goto failed;
780                 }
781
782                 nilfs_finish_roll_forward(nilfs, ri);
783         }
784
785  failed:
786         nilfs_detach_checkpoint(sbi);
787         return err;
788 }
789
790 /**
791  * nilfs_search_super_root - search the latest valid super root
792  * @nilfs: the_nilfs
793  * @ri: pointer to a nilfs_recovery_info struct to store search results.
794  *
795  * nilfs_search_super_root() looks for the latest super-root from a partial
796  * segment pointed by the superblock.  It sets up struct the_nilfs through
797  * this search. It fills nilfs_recovery_info (ri) required for recovery.
798  *
799  * Return Value: On success, 0 is returned.  On error, one of the following
800  * negative error code is returned.
801  *
802  * %-EINVAL - No valid segment found
803  *
804  * %-EIO - I/O error
805  */
806 int nilfs_search_super_root(struct the_nilfs *nilfs,
807                             struct nilfs_recovery_info *ri)
808 {
809         struct nilfs_segsum_info ssi;
810         sector_t pseg_start, pseg_end, sr_pseg_start = 0;
811         sector_t seg_start, seg_end; /* range of full segment (block number) */
812         sector_t b, end;
813         u64 seg_seq;
814         __u64 segnum, nextnum = 0;
815         __u64 cno;
816         LIST_HEAD(segments);
817         int empty_seg = 0, scan_newer = 0;
818         int ret;
819
820         pseg_start = nilfs->ns_last_pseg;
821         seg_seq = nilfs->ns_last_seq;
822         cno = nilfs->ns_last_cno;
823         segnum = nilfs_get_segnum_of_block(nilfs, pseg_start);
824
825         /* Calculate range of segment */
826         nilfs_get_segment_range(nilfs, segnum, &seg_start, &seg_end);
827
828         /* Read ahead segment */
829         b = seg_start;
830         while (b <= seg_end)
831                 __breadahead(nilfs->ns_bdev, b++, nilfs->ns_blocksize);
832
833         for (;;) {
834                 ret = load_segment_summary(nilfs, pseg_start, seg_seq, &ssi);
835                 if (ret) {
836                         if (ret == NILFS_SEG_FAIL_IO)
837                                 goto failed;
838                         goto strayed;
839                 }
840                 pseg_end = pseg_start + ssi.nblocks - 1;
841                 if (unlikely(pseg_end > seg_end)) {
842                         ret = NILFS_SEG_FAIL_CONSISTENCY;
843                         goto strayed;
844                 }
845
846                 /* A valid partial segment */
847                 ri->ri_pseg_start = pseg_start;
848                 ri->ri_seq = seg_seq;
849                 ri->ri_segnum = segnum;
850                 nextnum = nilfs_get_segnum_of_block(nilfs, ssi.next);
851                 ri->ri_nextnum = nextnum;
852                 empty_seg = 0;
853
854                 if (!NILFS_SEG_HAS_SR(&ssi) && !scan_newer) {
855                         /* This will never happen because a superblock
856                            (last_segment) always points to a pseg
857                            having a super root. */
858                         ret = NILFS_SEG_FAIL_CONSISTENCY;
859                         goto failed;
860                 }
861
862                 if (pseg_start == seg_start) {
863                         nilfs_get_segment_range(nilfs, nextnum, &b, &end);
864                         while (b <= end)
865                                 __breadahead(nilfs->ns_bdev, b++,
866                                              nilfs->ns_blocksize);
867                 }
868                 if (!NILFS_SEG_HAS_SR(&ssi)) {
869                         if (!ri->ri_lsegs_start && NILFS_SEG_LOGBGN(&ssi)) {
870                                 ri->ri_lsegs_start = pseg_start;
871                                 ri->ri_lsegs_start_seq = seg_seq;
872                         }
873                         if (NILFS_SEG_LOGEND(&ssi))
874                                 ri->ri_lsegs_end = pseg_start;
875                         goto try_next_pseg;
876                 }
877
878                 /* A valid super root was found. */
879                 ri->ri_cno = cno++;
880                 ri->ri_super_root = pseg_end;
881                 ri->ri_lsegs_start = ri->ri_lsegs_end = 0;
882
883                 nilfs_dispose_segment_list(&segments);
884                 nilfs->ns_pseg_offset = (sr_pseg_start = pseg_start)
885                         + ssi.nblocks - seg_start;
886                 nilfs->ns_seg_seq = seg_seq;
887                 nilfs->ns_segnum = segnum;
888                 nilfs->ns_cno = cno;  /* nilfs->ns_cno = ri->ri_cno + 1 */
889                 nilfs->ns_ctime = ssi.ctime;
890                 nilfs->ns_nextnum = nextnum;
891
892                 if (scan_newer)
893                         ri->ri_need_recovery = NILFS_RECOVERY_SR_UPDATED;
894                 else {
895                         if (nilfs->ns_mount_state & NILFS_VALID_FS)
896                                 goto super_root_found;
897                         scan_newer = 1;
898                 }
899
900                 /* reset region for roll-forward */
901                 pseg_start += ssi.nblocks;
902                 if (pseg_start < seg_end)
903                         continue;
904                 goto feed_segment;
905
906  try_next_pseg:
907                 /* Standing on a course, or met an inconsistent state */
908                 pseg_start += ssi.nblocks;
909                 if (pseg_start < seg_end)
910                         continue;
911                 goto feed_segment;
912
913  strayed:
914                 /* Off the trail */
915                 if (!scan_newer)
916                         /*
917                          * This can happen if a checkpoint was written without
918                          * barriers, or as a result of an I/O failure.
919                          */
920                         goto failed;
921
922  feed_segment:
923                 /* Looking to the next full segment */
924                 if (empty_seg++)
925                         goto super_root_found; /* found a valid super root */
926
927                 ret = nilfs_segment_list_add(&segments, segnum);
928                 if (unlikely(ret))
929                         goto failed;
930
931                 seg_seq++;
932                 segnum = nextnum;
933                 nilfs_get_segment_range(nilfs, segnum, &seg_start, &seg_end);
934                 pseg_start = seg_start;
935         }
936
937  super_root_found:
938         /* Updating pointers relating to the latest checkpoint */
939         list_splice_tail(&segments, &ri->ri_used_segments);
940         nilfs->ns_last_pseg = sr_pseg_start;
941         nilfs->ns_last_seq = nilfs->ns_seg_seq;
942         nilfs->ns_last_cno = ri->ri_cno;
943         return 0;
944
945  failed:
946         nilfs_dispose_segment_list(&segments);
947         return (ret < 0) ? ret : nilfs_warn_segment_error(ret);
948 }