x86/amd-iommu: Add per IOMMU reference counting
[linux/fpc-iii.git] / fs / nilfs2 / recovery.c
blob6dc83591d1182164c5fdaf4b27e92d20b794d4a6
1 /*
2 * recovery.c - NILFS recovery logic
4 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
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.
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.
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
20 * Written by Ryusuke Konishi <ryusuke@osrg.net>
23 #include <linux/buffer_head.h>
24 #include <linux/blkdev.h>
25 #include <linux/swap.h>
26 #include <linux/crc32.h>
27 #include "nilfs.h"
28 #include "segment.h"
29 #include "sufile.h"
30 #include "page.h"
31 #include "segbuf.h"
34 * Segment check result
36 enum {
37 NILFS_SEG_VALID,
38 NILFS_SEG_NO_SUPER_ROOT,
39 NILFS_SEG_FAIL_IO,
40 NILFS_SEG_FAIL_MAGIC,
41 NILFS_SEG_FAIL_SEQ,
42 NILFS_SEG_FAIL_CHECKSUM_SEGSUM,
43 NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT,
44 NILFS_SEG_FAIL_CHECKSUM_FULL,
45 NILFS_SEG_FAIL_CONSISTENCY,
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;
59 static int nilfs_warn_segment_error(int err)
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_SEGSUM:
75 printk(KERN_WARNING
76 "NILFS warning: Checksum error in segment summary\n");
77 break;
78 case NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT:
79 printk(KERN_WARNING
80 "NILFS warning: Checksum error in super root\n");
81 break;
82 case NILFS_SEG_FAIL_CHECKSUM_FULL:
83 printk(KERN_WARNING
84 "NILFS warning: Checksum error in segment payload\n");
85 break;
86 case NILFS_SEG_FAIL_CONSISTENCY:
87 printk(KERN_WARNING
88 "NILFS warning: Inconsistent segment\n");
89 break;
90 case NILFS_SEG_NO_SUPER_ROOT:
91 printk(KERN_WARNING
92 "NILFS warning: No super root in the last segment\n");
93 break;
95 return -EINVAL;
98 static void store_segsum_info(struct nilfs_segsum_info *ssi,
99 struct nilfs_segment_summary *sum,
100 unsigned int blocksize)
102 ssi->flags = le16_to_cpu(sum->ss_flags);
103 ssi->seg_seq = le64_to_cpu(sum->ss_seq);
104 ssi->ctime = le64_to_cpu(sum->ss_create);
105 ssi->next = le64_to_cpu(sum->ss_next);
106 ssi->nblocks = le32_to_cpu(sum->ss_nblocks);
107 ssi->nfinfo = le32_to_cpu(sum->ss_nfinfo);
108 ssi->sumbytes = le32_to_cpu(sum->ss_sumbytes);
110 ssi->nsumblk = DIV_ROUND_UP(ssi->sumbytes, blocksize);
111 ssi->nfileblk = ssi->nblocks - ssi->nsumblk - !!NILFS_SEG_HAS_SR(ssi);
115 * calc_crc_cont - check CRC of blocks continuously
116 * @sbi: nilfs_sb_info
117 * @bhs: buffer head of start block
118 * @sum: place to store result
119 * @offset: offset bytes in the first block
120 * @check_bytes: number of bytes to be checked
121 * @start: DBN of start block
122 * @nblock: number of blocks to be checked
124 static int calc_crc_cont(struct nilfs_sb_info *sbi, struct buffer_head *bhs,
125 u32 *sum, unsigned long offset, u64 check_bytes,
126 sector_t start, unsigned long nblock)
128 unsigned long blocksize = sbi->s_super->s_blocksize;
129 unsigned long size;
130 u32 crc;
132 BUG_ON(offset >= blocksize);
133 check_bytes -= offset;
134 size = min_t(u64, check_bytes, blocksize - offset);
135 crc = crc32_le(sbi->s_nilfs->ns_crc_seed,
136 (unsigned char *)bhs->b_data + offset, size);
137 if (--nblock > 0) {
138 do {
139 struct buffer_head *bh
140 = sb_bread(sbi->s_super, ++start);
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);
149 *sum = crc;
150 return 0;
154 * nilfs_read_super_root_block - read super root block
155 * @sb: super_block
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
160 int nilfs_read_super_root_block(struct super_block *sb, sector_t sr_block,
161 struct buffer_head **pbh, int check)
163 struct buffer_head *bh_sr;
164 struct nilfs_super_root *sr;
165 u32 crc;
166 int ret;
168 *pbh = NULL;
169 bh_sr = sb_bread(sb, sr_block);
170 if (unlikely(!bh_sr)) {
171 ret = NILFS_SEG_FAIL_IO;
172 goto failed;
175 sr = (struct nilfs_super_root *)bh_sr->b_data;
176 if (check) {
177 unsigned bytes = le16_to_cpu(sr->sr_bytes);
179 if (bytes == 0 || bytes > sb->s_blocksize) {
180 ret = NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT;
181 goto failed_bh;
183 if (calc_crc_cont(NILFS_SB(sb), bh_sr, &crc,
184 sizeof(sr->sr_sum), bytes, sr_block, 1)) {
185 ret = NILFS_SEG_FAIL_IO;
186 goto failed_bh;
188 if (crc != le32_to_cpu(sr->sr_sum)) {
189 ret = NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT;
190 goto failed_bh;
193 *pbh = bh_sr;
194 return 0;
196 failed_bh:
197 brelse(bh_sr);
199 failed:
200 return nilfs_warn_segment_error(ret);
204 * load_segment_summary - read segment summary of the specified partial segment
205 * @sbi: nilfs_sb_info
206 * @pseg_start: start disk block number of partial segment
207 * @seg_seq: sequence number requested
208 * @ssi: pointer to nilfs_segsum_info struct to store information
209 * @full_check: full check flag
210 * (0: only checks segment summary CRC, 1: data CRC)
212 static int
213 load_segment_summary(struct nilfs_sb_info *sbi, sector_t pseg_start,
214 u64 seg_seq, struct nilfs_segsum_info *ssi,
215 int full_check)
217 struct buffer_head *bh_sum;
218 struct nilfs_segment_summary *sum;
219 unsigned long offset, nblock;
220 u64 check_bytes;
221 u32 crc, crc_sum;
222 int ret = NILFS_SEG_FAIL_IO;
224 bh_sum = sb_bread(sbi->s_super, pseg_start);
225 if (!bh_sum)
226 goto out;
228 sum = (struct nilfs_segment_summary *)bh_sum->b_data;
230 /* Check consistency of segment summary */
231 if (le32_to_cpu(sum->ss_magic) != NILFS_SEGSUM_MAGIC) {
232 ret = NILFS_SEG_FAIL_MAGIC;
233 goto failed;
235 store_segsum_info(ssi, sum, sbi->s_super->s_blocksize);
236 if (seg_seq != ssi->seg_seq) {
237 ret = NILFS_SEG_FAIL_SEQ;
238 goto failed;
240 if (full_check) {
241 offset = sizeof(sum->ss_datasum);
242 check_bytes =
243 ((u64)ssi->nblocks << sbi->s_super->s_blocksize_bits);
244 nblock = ssi->nblocks;
245 crc_sum = le32_to_cpu(sum->ss_datasum);
246 ret = NILFS_SEG_FAIL_CHECKSUM_FULL;
247 } else { /* only checks segment summary */
248 offset = sizeof(sum->ss_datasum) + sizeof(sum->ss_sumsum);
249 check_bytes = ssi->sumbytes;
250 nblock = ssi->nsumblk;
251 crc_sum = le32_to_cpu(sum->ss_sumsum);
252 ret = NILFS_SEG_FAIL_CHECKSUM_SEGSUM;
255 if (unlikely(nblock == 0 ||
256 nblock > sbi->s_nilfs->ns_blocks_per_segment)) {
257 /* This limits the number of blocks read in the CRC check */
258 ret = NILFS_SEG_FAIL_CONSISTENCY;
259 goto failed;
261 if (calc_crc_cont(sbi, bh_sum, &crc, offset, check_bytes,
262 pseg_start, nblock)) {
263 ret = NILFS_SEG_FAIL_IO;
264 goto failed;
266 if (crc == crc_sum)
267 ret = 0;
268 failed:
269 brelse(bh_sum);
270 out:
271 return ret;
274 static void *segsum_get(struct super_block *sb, struct buffer_head **pbh,
275 unsigned int *offset, unsigned int bytes)
277 void *ptr;
278 sector_t blocknr;
280 BUG_ON((*pbh)->b_size < *offset);
281 if (bytes > (*pbh)->b_size - *offset) {
282 blocknr = (*pbh)->b_blocknr;
283 brelse(*pbh);
284 *pbh = sb_bread(sb, blocknr + 1);
285 if (unlikely(!*pbh))
286 return NULL;
287 *offset = 0;
289 ptr = (*pbh)->b_data + *offset;
290 *offset += bytes;
291 return ptr;
294 static void segsum_skip(struct super_block *sb, struct buffer_head **pbh,
295 unsigned int *offset, unsigned int bytes,
296 unsigned long count)
298 unsigned int rest_item_in_current_block
299 = ((*pbh)->b_size - *offset) / bytes;
301 if (count <= rest_item_in_current_block) {
302 *offset += bytes * count;
303 } else {
304 sector_t blocknr = (*pbh)->b_blocknr;
305 unsigned int nitem_per_block = (*pbh)->b_size / bytes;
306 unsigned int bcnt;
308 count -= rest_item_in_current_block;
309 bcnt = DIV_ROUND_UP(count, nitem_per_block);
310 *offset = bytes * (count - (bcnt - 1) * nitem_per_block);
312 brelse(*pbh);
313 *pbh = sb_bread(sb, blocknr + bcnt);
317 static int
318 collect_blocks_from_segsum(struct nilfs_sb_info *sbi, sector_t sum_blocknr,
319 struct nilfs_segsum_info *ssi,
320 struct list_head *head)
322 struct buffer_head *bh;
323 unsigned int offset;
324 unsigned long nfinfo = ssi->nfinfo;
325 sector_t blocknr = sum_blocknr + ssi->nsumblk;
326 ino_t ino;
327 int err = -EIO;
329 if (!nfinfo)
330 return 0;
332 bh = sb_bread(sbi->s_super, sum_blocknr);
333 if (unlikely(!bh))
334 goto out;
336 offset = le16_to_cpu(
337 ((struct nilfs_segment_summary *)bh->b_data)->ss_bytes);
338 for (;;) {
339 unsigned long nblocks, ndatablk, nnodeblk;
340 struct nilfs_finfo *finfo;
342 finfo = segsum_get(sbi->s_super, &bh, &offset, sizeof(*finfo));
343 if (unlikely(!finfo))
344 goto out;
346 ino = le64_to_cpu(finfo->fi_ino);
347 nblocks = le32_to_cpu(finfo->fi_nblocks);
348 ndatablk = le32_to_cpu(finfo->fi_ndatablk);
349 nnodeblk = nblocks - ndatablk;
351 while (ndatablk-- > 0) {
352 struct nilfs_recovery_block *rb;
353 struct nilfs_binfo_v *binfo;
355 binfo = segsum_get(sbi->s_super, &bh, &offset,
356 sizeof(*binfo));
357 if (unlikely(!binfo))
358 goto out;
360 rb = kmalloc(sizeof(*rb), GFP_NOFS);
361 if (unlikely(!rb)) {
362 err = -ENOMEM;
363 goto out;
365 rb->ino = ino;
366 rb->blocknr = blocknr++;
367 rb->vblocknr = le64_to_cpu(binfo->bi_vblocknr);
368 rb->blkoff = le64_to_cpu(binfo->bi_blkoff);
369 /* INIT_LIST_HEAD(&rb->list); */
370 list_add_tail(&rb->list, head);
372 if (--nfinfo == 0)
373 break;
374 blocknr += nnodeblk; /* always 0 for the data sync segments */
375 segsum_skip(sbi->s_super, &bh, &offset, sizeof(__le64),
376 nnodeblk);
377 if (unlikely(!bh))
378 goto out;
380 err = 0;
381 out:
382 brelse(bh); /* brelse(NULL) is just ignored */
383 return err;
386 static void dispose_recovery_list(struct list_head *head)
388 while (!list_empty(head)) {
389 struct nilfs_recovery_block *rb
390 = list_entry(head->next,
391 struct nilfs_recovery_block, list);
392 list_del(&rb->list);
393 kfree(rb);
397 struct nilfs_segment_entry {
398 struct list_head list;
399 __u64 segnum;
402 static int nilfs_segment_list_add(struct list_head *head, __u64 segnum)
404 struct nilfs_segment_entry *ent = kmalloc(sizeof(*ent), GFP_NOFS);
406 if (unlikely(!ent))
407 return -ENOMEM;
409 ent->segnum = segnum;
410 INIT_LIST_HEAD(&ent->list);
411 list_add_tail(&ent->list, head);
412 return 0;
415 void nilfs_dispose_segment_list(struct list_head *head)
417 while (!list_empty(head)) {
418 struct nilfs_segment_entry *ent
419 = list_entry(head->next,
420 struct nilfs_segment_entry, list);
421 list_del(&ent->list);
422 kfree(ent);
426 static int nilfs_prepare_segment_for_recovery(struct the_nilfs *nilfs,
427 struct nilfs_sb_info *sbi,
428 struct nilfs_recovery_info *ri)
430 struct list_head *head = &ri->ri_used_segments;
431 struct nilfs_segment_entry *ent, *n;
432 struct inode *sufile = nilfs->ns_sufile;
433 __u64 segnum[4];
434 int err;
435 int i;
437 segnum[0] = nilfs->ns_segnum;
438 segnum[1] = nilfs->ns_nextnum;
439 segnum[2] = ri->ri_segnum;
440 segnum[3] = ri->ri_nextnum;
442 nilfs_attach_writer(nilfs, sbi);
444 * Releasing the next segment of the latest super root.
445 * The next segment is invalidated by this recovery.
447 err = nilfs_sufile_free(sufile, segnum[1]);
448 if (unlikely(err))
449 goto failed;
451 for (i = 1; i < 4; i++) {
452 err = nilfs_segment_list_add(head, segnum[i]);
453 if (unlikely(err))
454 goto failed;
458 * Collecting segments written after the latest super root.
459 * These are marked dirty to avoid being reallocated in the next write.
461 list_for_each_entry_safe(ent, n, head, list) {
462 if (ent->segnum != segnum[0]) {
463 err = nilfs_sufile_scrap(sufile, ent->segnum);
464 if (unlikely(err))
465 goto failed;
467 list_del(&ent->list);
468 kfree(ent);
471 /* Allocate new segments for recovery */
472 err = nilfs_sufile_alloc(sufile, &segnum[0]);
473 if (unlikely(err))
474 goto failed;
476 nilfs->ns_pseg_offset = 0;
477 nilfs->ns_seg_seq = ri->ri_seq + 2;
478 nilfs->ns_nextnum = nilfs->ns_segnum = segnum[0];
480 failed:
481 /* No need to recover sufile because it will be destroyed on error */
482 nilfs_detach_writer(nilfs, sbi);
483 return err;
486 static int nilfs_recovery_copy_block(struct nilfs_sb_info *sbi,
487 struct nilfs_recovery_block *rb,
488 struct page *page)
490 struct buffer_head *bh_org;
491 void *kaddr;
493 bh_org = sb_bread(sbi->s_super, rb->blocknr);
494 if (unlikely(!bh_org))
495 return -EIO;
497 kaddr = kmap_atomic(page, KM_USER0);
498 memcpy(kaddr + bh_offset(bh_org), bh_org->b_data, bh_org->b_size);
499 kunmap_atomic(kaddr, KM_USER0);
500 brelse(bh_org);
501 return 0;
504 static int recover_dsync_blocks(struct nilfs_sb_info *sbi,
505 struct list_head *head,
506 unsigned long *nr_salvaged_blocks)
508 struct inode *inode;
509 struct nilfs_recovery_block *rb, *n;
510 unsigned blocksize = sbi->s_super->s_blocksize;
511 struct page *page;
512 loff_t pos;
513 int err = 0, err2 = 0;
515 list_for_each_entry_safe(rb, n, head, list) {
516 inode = nilfs_iget(sbi->s_super, rb->ino);
517 if (IS_ERR(inode)) {
518 err = PTR_ERR(inode);
519 inode = NULL;
520 goto failed_inode;
523 pos = rb->blkoff << inode->i_blkbits;
524 page = NULL;
525 err = block_write_begin(NULL, inode->i_mapping, pos, blocksize,
526 0, &page, NULL, nilfs_get_block);
527 if (unlikely(err))
528 goto failed_inode;
530 err = nilfs_recovery_copy_block(sbi, rb, page);
531 if (unlikely(err))
532 goto failed_page;
534 err = nilfs_set_file_dirty(sbi, inode, 1);
535 if (unlikely(err))
536 goto failed_page;
538 block_write_end(NULL, inode->i_mapping, pos, blocksize,
539 blocksize, page, NULL);
541 unlock_page(page);
542 page_cache_release(page);
544 (*nr_salvaged_blocks)++;
545 goto next;
547 failed_page:
548 unlock_page(page);
549 page_cache_release(page);
551 failed_inode:
552 printk(KERN_WARNING
553 "NILFS warning: error recovering data block "
554 "(err=%d, ino=%lu, block-offset=%llu)\n",
555 err, (unsigned long)rb->ino,
556 (unsigned long long)rb->blkoff);
557 if (!err2)
558 err2 = err;
559 next:
560 iput(inode); /* iput(NULL) is just ignored */
561 list_del_init(&rb->list);
562 kfree(rb);
564 return err2;
568 * nilfs_do_roll_forward - salvage logical segments newer than the latest
569 * checkpoint
570 * @sbi: nilfs_sb_info
571 * @nilfs: the_nilfs
572 * @ri: pointer to a nilfs_recovery_info
574 static int nilfs_do_roll_forward(struct the_nilfs *nilfs,
575 struct nilfs_sb_info *sbi,
576 struct nilfs_recovery_info *ri)
578 struct nilfs_segsum_info ssi;
579 sector_t pseg_start;
580 sector_t seg_start, seg_end; /* Starting/ending DBN of full segment */
581 unsigned long nsalvaged_blocks = 0;
582 u64 seg_seq;
583 __u64 segnum, nextnum = 0;
584 int empty_seg = 0;
585 int err = 0, ret;
586 LIST_HEAD(dsync_blocks); /* list of data blocks to be recovered */
587 enum {
588 RF_INIT_ST,
589 RF_DSYNC_ST, /* scanning data-sync segments */
591 int state = RF_INIT_ST;
593 nilfs_attach_writer(nilfs, sbi);
594 pseg_start = ri->ri_lsegs_start;
595 seg_seq = ri->ri_lsegs_start_seq;
596 segnum = nilfs_get_segnum_of_block(nilfs, pseg_start);
597 nilfs_get_segment_range(nilfs, segnum, &seg_start, &seg_end);
599 while (segnum != ri->ri_segnum || pseg_start <= ri->ri_pseg_start) {
601 ret = load_segment_summary(sbi, pseg_start, seg_seq, &ssi, 1);
602 if (ret) {
603 if (ret == NILFS_SEG_FAIL_IO) {
604 err = -EIO;
605 goto failed;
607 goto strayed;
609 if (unlikely(NILFS_SEG_HAS_SR(&ssi)))
610 goto confused;
612 /* Found a valid partial segment; do recovery actions */
613 nextnum = nilfs_get_segnum_of_block(nilfs, ssi.next);
614 empty_seg = 0;
615 nilfs->ns_ctime = ssi.ctime;
616 if (!(ssi.flags & NILFS_SS_GC))
617 nilfs->ns_nongc_ctime = ssi.ctime;
619 switch (state) {
620 case RF_INIT_ST:
621 if (!NILFS_SEG_LOGBGN(&ssi) || !NILFS_SEG_DSYNC(&ssi))
622 goto try_next_pseg;
623 state = RF_DSYNC_ST;
624 /* Fall through */
625 case RF_DSYNC_ST:
626 if (!NILFS_SEG_DSYNC(&ssi))
627 goto confused;
629 err = collect_blocks_from_segsum(
630 sbi, pseg_start, &ssi, &dsync_blocks);
631 if (unlikely(err))
632 goto failed;
633 if (NILFS_SEG_LOGEND(&ssi)) {
634 err = recover_dsync_blocks(
635 sbi, &dsync_blocks, &nsalvaged_blocks);
636 if (unlikely(err))
637 goto failed;
638 state = RF_INIT_ST;
640 break; /* Fall through to try_next_pseg */
643 try_next_pseg:
644 if (pseg_start == ri->ri_lsegs_end)
645 break;
646 pseg_start += ssi.nblocks;
647 if (pseg_start < seg_end)
648 continue;
649 goto feed_segment;
651 strayed:
652 if (pseg_start == ri->ri_lsegs_end)
653 break;
655 feed_segment:
656 /* Looking to the next full segment */
657 if (empty_seg++)
658 break;
659 seg_seq++;
660 segnum = nextnum;
661 nilfs_get_segment_range(nilfs, segnum, &seg_start, &seg_end);
662 pseg_start = seg_start;
665 if (nsalvaged_blocks) {
666 printk(KERN_INFO "NILFS (device %s): salvaged %lu blocks\n",
667 sbi->s_super->s_id, nsalvaged_blocks);
668 ri->ri_need_recovery = NILFS_RECOVERY_ROLLFORWARD_DONE;
670 out:
671 dispose_recovery_list(&dsync_blocks);
672 nilfs_detach_writer(sbi->s_nilfs, sbi);
673 return err;
675 confused:
676 err = -EINVAL;
677 failed:
678 printk(KERN_ERR
679 "NILFS (device %s): Error roll-forwarding "
680 "(err=%d, pseg block=%llu). ",
681 sbi->s_super->s_id, err, (unsigned long long)pseg_start);
682 goto out;
685 static void nilfs_finish_roll_forward(struct the_nilfs *nilfs,
686 struct nilfs_sb_info *sbi,
687 struct nilfs_recovery_info *ri)
689 struct buffer_head *bh;
690 int err;
692 if (nilfs_get_segnum_of_block(nilfs, ri->ri_lsegs_start) !=
693 nilfs_get_segnum_of_block(nilfs, ri->ri_super_root))
694 return;
696 bh = sb_getblk(sbi->s_super, ri->ri_lsegs_start);
697 BUG_ON(!bh);
698 memset(bh->b_data, 0, bh->b_size);
699 set_buffer_dirty(bh);
700 err = sync_dirty_buffer(bh);
701 if (unlikely(err))
702 printk(KERN_WARNING
703 "NILFS warning: buffer sync write failed during "
704 "post-cleaning of recovery.\n");
705 brelse(bh);
709 * nilfs_recover_logical_segments - salvage logical segments written after
710 * the latest super root
711 * @nilfs: the_nilfs
712 * @sbi: nilfs_sb_info
713 * @ri: pointer to a nilfs_recovery_info struct to store search results.
715 * Return Value: On success, 0 is returned. On error, one of the following
716 * negative error code is returned.
718 * %-EINVAL - Inconsistent filesystem state.
720 * %-EIO - I/O error
722 * %-ENOSPC - No space left on device (only in a panic state).
724 * %-ERESTARTSYS - Interrupted.
726 * %-ENOMEM - Insufficient memory available.
728 int nilfs_recover_logical_segments(struct the_nilfs *nilfs,
729 struct nilfs_sb_info *sbi,
730 struct nilfs_recovery_info *ri)
732 int err;
734 if (ri->ri_lsegs_start == 0 || ri->ri_lsegs_end == 0)
735 return 0;
737 err = nilfs_attach_checkpoint(sbi, ri->ri_cno);
738 if (unlikely(err)) {
739 printk(KERN_ERR
740 "NILFS: error loading the latest checkpoint.\n");
741 return err;
744 err = nilfs_do_roll_forward(nilfs, sbi, ri);
745 if (unlikely(err))
746 goto failed;
748 if (ri->ri_need_recovery == NILFS_RECOVERY_ROLLFORWARD_DONE) {
749 err = nilfs_prepare_segment_for_recovery(nilfs, sbi, ri);
750 if (unlikely(err)) {
751 printk(KERN_ERR "NILFS: Error preparing segments for "
752 "recovery.\n");
753 goto failed;
756 err = nilfs_attach_segment_constructor(sbi);
757 if (unlikely(err))
758 goto failed;
760 set_nilfs_discontinued(nilfs);
761 err = nilfs_construct_segment(sbi->s_super);
762 nilfs_detach_segment_constructor(sbi);
764 if (unlikely(err)) {
765 printk(KERN_ERR "NILFS: Oops! recovery failed. "
766 "(err=%d)\n", err);
767 goto failed;
770 nilfs_finish_roll_forward(nilfs, sbi, ri);
773 nilfs_detach_checkpoint(sbi);
774 return 0;
776 failed:
777 nilfs_detach_checkpoint(sbi);
778 nilfs_mdt_clear(nilfs->ns_cpfile);
779 nilfs_mdt_clear(nilfs->ns_sufile);
780 nilfs_mdt_clear(nilfs->ns_dat);
781 return err;
785 * nilfs_search_super_root - search the latest valid super root
786 * @nilfs: the_nilfs
787 * @sbi: nilfs_sb_info
788 * @ri: pointer to a nilfs_recovery_info struct to store search results.
790 * nilfs_search_super_root() looks for the latest super-root from a partial
791 * segment pointed by the superblock. It sets up struct the_nilfs through
792 * this search. It fills nilfs_recovery_info (ri) required for recovery.
794 * Return Value: On success, 0 is returned. On error, one of the following
795 * negative error code is returned.
797 * %-EINVAL - No valid segment found
799 * %-EIO - I/O error
801 int nilfs_search_super_root(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi,
802 struct nilfs_recovery_info *ri)
804 struct nilfs_segsum_info ssi;
805 sector_t pseg_start, pseg_end, sr_pseg_start = 0;
806 sector_t seg_start, seg_end; /* range of full segment (block number) */
807 u64 seg_seq;
808 __u64 segnum, nextnum = 0;
809 __u64 cno;
810 LIST_HEAD(segments);
811 int empty_seg = 0, scan_newer = 0;
812 int ret;
814 pseg_start = nilfs->ns_last_pseg;
815 seg_seq = nilfs->ns_last_seq;
816 cno = nilfs->ns_last_cno;
817 segnum = nilfs_get_segnum_of_block(nilfs, pseg_start);
819 /* Calculate range of segment */
820 nilfs_get_segment_range(nilfs, segnum, &seg_start, &seg_end);
822 for (;;) {
823 /* Load segment summary */
824 ret = load_segment_summary(sbi, pseg_start, seg_seq, &ssi, 1);
825 if (ret) {
826 if (ret == NILFS_SEG_FAIL_IO)
827 goto failed;
828 goto strayed;
830 pseg_end = pseg_start + ssi.nblocks - 1;
831 if (unlikely(pseg_end > seg_end)) {
832 ret = NILFS_SEG_FAIL_CONSISTENCY;
833 goto strayed;
836 /* A valid partial segment */
837 ri->ri_pseg_start = pseg_start;
838 ri->ri_seq = seg_seq;
839 ri->ri_segnum = segnum;
840 nextnum = nilfs_get_segnum_of_block(nilfs, ssi.next);
841 ri->ri_nextnum = nextnum;
842 empty_seg = 0;
844 if (!NILFS_SEG_HAS_SR(&ssi)) {
845 if (!scan_newer) {
846 /* This will never happen because a superblock
847 (last_segment) always points to a pseg
848 having a super root. */
849 ret = NILFS_SEG_FAIL_CONSISTENCY;
850 goto failed;
852 if (!ri->ri_lsegs_start && NILFS_SEG_LOGBGN(&ssi)) {
853 ri->ri_lsegs_start = pseg_start;
854 ri->ri_lsegs_start_seq = seg_seq;
856 if (NILFS_SEG_LOGEND(&ssi))
857 ri->ri_lsegs_end = pseg_start;
858 goto try_next_pseg;
861 /* A valid super root was found. */
862 ri->ri_cno = cno++;
863 ri->ri_super_root = pseg_end;
864 ri->ri_lsegs_start = ri->ri_lsegs_end = 0;
866 nilfs_dispose_segment_list(&segments);
867 nilfs->ns_pseg_offset = (sr_pseg_start = pseg_start)
868 + ssi.nblocks - seg_start;
869 nilfs->ns_seg_seq = seg_seq;
870 nilfs->ns_segnum = segnum;
871 nilfs->ns_cno = cno; /* nilfs->ns_cno = ri->ri_cno + 1 */
872 nilfs->ns_ctime = ssi.ctime;
873 nilfs->ns_nextnum = nextnum;
875 if (scan_newer)
876 ri->ri_need_recovery = NILFS_RECOVERY_SR_UPDATED;
877 else {
878 if (nilfs->ns_mount_state & NILFS_VALID_FS)
879 goto super_root_found;
880 scan_newer = 1;
883 /* reset region for roll-forward */
884 pseg_start += ssi.nblocks;
885 if (pseg_start < seg_end)
886 continue;
887 goto feed_segment;
889 try_next_pseg:
890 /* Standing on a course, or met an inconsistent state */
891 pseg_start += ssi.nblocks;
892 if (pseg_start < seg_end)
893 continue;
894 goto feed_segment;
896 strayed:
897 /* Off the trail */
898 if (!scan_newer)
900 * This can happen if a checkpoint was written without
901 * barriers, or as a result of an I/O failure.
903 goto failed;
905 feed_segment:
906 /* Looking to the next full segment */
907 if (empty_seg++)
908 goto super_root_found; /* found a valid super root */
910 ret = nilfs_segment_list_add(&segments, segnum);
911 if (unlikely(ret))
912 goto failed;
914 seg_seq++;
915 segnum = nextnum;
916 nilfs_get_segment_range(nilfs, segnum, &seg_start, &seg_end);
917 pseg_start = seg_start;
920 super_root_found:
921 /* Updating pointers relating to the latest checkpoint */
922 list_splice(&segments, ri->ri_used_segments.prev);
923 nilfs->ns_last_pseg = sr_pseg_start;
924 nilfs->ns_last_seq = nilfs->ns_seg_seq;
925 nilfs->ns_last_cno = ri->ri_cno;
926 return 0;
928 failed:
929 nilfs_dispose_segment_list(&segments);
930 return (ret < 0) ? ret : nilfs_warn_segment_error(ret);