ACPI: sleep: another HP DMI entry for init_set_sci_en_on_resume
[linux-2.6/mini2440.git] / fs / nilfs2 / recovery.c
blobd80cc71be7499cd62be748324eec1be57ecfe43a
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, rb->ino, (unsigned long long)rb->blkoff);
556 if (!err2)
557 err2 = err;
558 next:
559 iput(inode); /* iput(NULL) is just ignored */
560 list_del_init(&rb->list);
561 kfree(rb);
563 return err2;
567 * nilfs_do_roll_forward - salvage logical segments newer than the latest
568 * checkpoint
569 * @sbi: nilfs_sb_info
570 * @nilfs: the_nilfs
571 * @ri: pointer to a nilfs_recovery_info
573 static int nilfs_do_roll_forward(struct the_nilfs *nilfs,
574 struct nilfs_sb_info *sbi,
575 struct nilfs_recovery_info *ri)
577 struct nilfs_segsum_info ssi;
578 sector_t pseg_start;
579 sector_t seg_start, seg_end; /* Starting/ending DBN of full segment */
580 unsigned long nsalvaged_blocks = 0;
581 u64 seg_seq;
582 __u64 segnum, nextnum = 0;
583 int empty_seg = 0;
584 int err = 0, ret;
585 LIST_HEAD(dsync_blocks); /* list of data blocks to be recovered */
586 enum {
587 RF_INIT_ST,
588 RF_DSYNC_ST, /* scanning data-sync segments */
590 int state = RF_INIT_ST;
592 nilfs_attach_writer(nilfs, sbi);
593 pseg_start = ri->ri_lsegs_start;
594 seg_seq = ri->ri_lsegs_start_seq;
595 segnum = nilfs_get_segnum_of_block(nilfs, pseg_start);
596 nilfs_get_segment_range(nilfs, segnum, &seg_start, &seg_end);
598 while (segnum != ri->ri_segnum || pseg_start <= ri->ri_pseg_start) {
600 ret = load_segment_summary(sbi, pseg_start, seg_seq, &ssi, 1);
601 if (ret) {
602 if (ret == NILFS_SEG_FAIL_IO) {
603 err = -EIO;
604 goto failed;
606 goto strayed;
608 if (unlikely(NILFS_SEG_HAS_SR(&ssi)))
609 goto confused;
611 /* Found a valid partial segment; do recovery actions */
612 nextnum = nilfs_get_segnum_of_block(nilfs, ssi.next);
613 empty_seg = 0;
614 nilfs->ns_ctime = ssi.ctime;
615 if (!(ssi.flags & NILFS_SS_GC))
616 nilfs->ns_nongc_ctime = ssi.ctime;
618 switch (state) {
619 case RF_INIT_ST:
620 if (!NILFS_SEG_LOGBGN(&ssi) || !NILFS_SEG_DSYNC(&ssi))
621 goto try_next_pseg;
622 state = RF_DSYNC_ST;
623 /* Fall through */
624 case RF_DSYNC_ST:
625 if (!NILFS_SEG_DSYNC(&ssi))
626 goto confused;
628 err = collect_blocks_from_segsum(
629 sbi, pseg_start, &ssi, &dsync_blocks);
630 if (unlikely(err))
631 goto failed;
632 if (NILFS_SEG_LOGEND(&ssi)) {
633 err = recover_dsync_blocks(
634 sbi, &dsync_blocks, &nsalvaged_blocks);
635 if (unlikely(err))
636 goto failed;
637 state = RF_INIT_ST;
639 break; /* Fall through to try_next_pseg */
642 try_next_pseg:
643 if (pseg_start == ri->ri_lsegs_end)
644 break;
645 pseg_start += ssi.nblocks;
646 if (pseg_start < seg_end)
647 continue;
648 goto feed_segment;
650 strayed:
651 if (pseg_start == ri->ri_lsegs_end)
652 break;
654 feed_segment:
655 /* Looking to the next full segment */
656 if (empty_seg++)
657 break;
658 seg_seq++;
659 segnum = nextnum;
660 nilfs_get_segment_range(nilfs, segnum, &seg_start, &seg_end);
661 pseg_start = seg_start;
664 if (nsalvaged_blocks) {
665 printk(KERN_INFO "NILFS (device %s): salvaged %lu blocks\n",
666 sbi->s_super->s_id, nsalvaged_blocks);
667 ri->ri_need_recovery = NILFS_RECOVERY_ROLLFORWARD_DONE;
669 out:
670 dispose_recovery_list(&dsync_blocks);
671 nilfs_detach_writer(sbi->s_nilfs, sbi);
672 return err;
674 confused:
675 err = -EINVAL;
676 failed:
677 printk(KERN_ERR
678 "NILFS (device %s): Error roll-forwarding "
679 "(err=%d, pseg block=%llu). ",
680 sbi->s_super->s_id, err, (unsigned long long)pseg_start);
681 goto out;
684 static void nilfs_finish_roll_forward(struct the_nilfs *nilfs,
685 struct nilfs_sb_info *sbi,
686 struct nilfs_recovery_info *ri)
688 struct buffer_head *bh;
689 int err;
691 if (nilfs_get_segnum_of_block(nilfs, ri->ri_lsegs_start) !=
692 nilfs_get_segnum_of_block(nilfs, ri->ri_super_root))
693 return;
695 bh = sb_getblk(sbi->s_super, ri->ri_lsegs_start);
696 BUG_ON(!bh);
697 memset(bh->b_data, 0, bh->b_size);
698 set_buffer_dirty(bh);
699 err = sync_dirty_buffer(bh);
700 if (unlikely(err))
701 printk(KERN_WARNING
702 "NILFS warning: buffer sync write failed during "
703 "post-cleaning of recovery.\n");
704 brelse(bh);
708 * nilfs_recover_logical_segments - salvage logical segments written after
709 * the latest super root
710 * @nilfs: the_nilfs
711 * @sbi: nilfs_sb_info
712 * @ri: pointer to a nilfs_recovery_info struct to store search results.
714 * Return Value: On success, 0 is returned. On error, one of the following
715 * negative error code is returned.
717 * %-EINVAL - Inconsistent filesystem state.
719 * %-EIO - I/O error
721 * %-ENOSPC - No space left on device (only in a panic state).
723 * %-ERESTARTSYS - Interrupted.
725 * %-ENOMEM - Insufficient memory available.
727 int nilfs_recover_logical_segments(struct the_nilfs *nilfs,
728 struct nilfs_sb_info *sbi,
729 struct nilfs_recovery_info *ri)
731 int err;
733 if (ri->ri_lsegs_start == 0 || ri->ri_lsegs_end == 0)
734 return 0;
736 err = nilfs_attach_checkpoint(sbi, ri->ri_cno);
737 if (unlikely(err)) {
738 printk(KERN_ERR
739 "NILFS: error loading the latest checkpoint.\n");
740 return err;
743 err = nilfs_do_roll_forward(nilfs, sbi, ri);
744 if (unlikely(err))
745 goto failed;
747 if (ri->ri_need_recovery == NILFS_RECOVERY_ROLLFORWARD_DONE) {
748 err = nilfs_prepare_segment_for_recovery(nilfs, sbi, ri);
749 if (unlikely(err)) {
750 printk(KERN_ERR "NILFS: Error preparing segments for "
751 "recovery.\n");
752 goto failed;
755 err = nilfs_attach_segment_constructor(sbi);
756 if (unlikely(err))
757 goto failed;
759 set_nilfs_discontinued(nilfs);
760 err = nilfs_construct_segment(sbi->s_super);
761 nilfs_detach_segment_constructor(sbi);
763 if (unlikely(err)) {
764 printk(KERN_ERR "NILFS: Oops! recovery failed. "
765 "(err=%d)\n", err);
766 goto failed;
769 nilfs_finish_roll_forward(nilfs, sbi, ri);
772 nilfs_detach_checkpoint(sbi);
773 return 0;
775 failed:
776 nilfs_detach_checkpoint(sbi);
777 nilfs_mdt_clear(nilfs->ns_cpfile);
778 nilfs_mdt_clear(nilfs->ns_sufile);
779 nilfs_mdt_clear(nilfs->ns_dat);
780 return err;
784 * nilfs_search_super_root - search the latest valid super root
785 * @nilfs: the_nilfs
786 * @sbi: nilfs_sb_info
787 * @ri: pointer to a nilfs_recovery_info struct to store search results.
789 * nilfs_search_super_root() looks for the latest super-root from a partial
790 * segment pointed by the superblock. It sets up struct the_nilfs through
791 * this search. It fills nilfs_recovery_info (ri) required for recovery.
793 * Return Value: On success, 0 is returned. On error, one of the following
794 * negative error code is returned.
796 * %-EINVAL - No valid segment found
798 * %-EIO - I/O error
800 int nilfs_search_super_root(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi,
801 struct nilfs_recovery_info *ri)
803 struct nilfs_segsum_info ssi;
804 sector_t pseg_start, pseg_end, sr_pseg_start = 0;
805 sector_t seg_start, seg_end; /* range of full segment (block number) */
806 u64 seg_seq;
807 __u64 segnum, nextnum = 0;
808 __u64 cno;
809 LIST_HEAD(segments);
810 int empty_seg = 0, scan_newer = 0;
811 int ret;
813 pseg_start = nilfs->ns_last_pseg;
814 seg_seq = nilfs->ns_last_seq;
815 cno = nilfs->ns_last_cno;
816 segnum = nilfs_get_segnum_of_block(nilfs, pseg_start);
818 /* Calculate range of segment */
819 nilfs_get_segment_range(nilfs, segnum, &seg_start, &seg_end);
821 for (;;) {
822 /* Load segment summary */
823 ret = load_segment_summary(sbi, pseg_start, seg_seq, &ssi, 1);
824 if (ret) {
825 if (ret == NILFS_SEG_FAIL_IO)
826 goto failed;
827 goto strayed;
829 pseg_end = pseg_start + ssi.nblocks - 1;
830 if (unlikely(pseg_end > seg_end)) {
831 ret = NILFS_SEG_FAIL_CONSISTENCY;
832 goto strayed;
835 /* A valid partial segment */
836 ri->ri_pseg_start = pseg_start;
837 ri->ri_seq = seg_seq;
838 ri->ri_segnum = segnum;
839 nextnum = nilfs_get_segnum_of_block(nilfs, ssi.next);
840 ri->ri_nextnum = nextnum;
841 empty_seg = 0;
843 if (!NILFS_SEG_HAS_SR(&ssi)) {
844 if (!scan_newer) {
845 /* This will never happen because a superblock
846 (last_segment) always points to a pseg
847 having a super root. */
848 ret = NILFS_SEG_FAIL_CONSISTENCY;
849 goto failed;
851 if (!ri->ri_lsegs_start && NILFS_SEG_LOGBGN(&ssi)) {
852 ri->ri_lsegs_start = pseg_start;
853 ri->ri_lsegs_start_seq = seg_seq;
855 if (NILFS_SEG_LOGEND(&ssi))
856 ri->ri_lsegs_end = pseg_start;
857 goto try_next_pseg;
860 /* A valid super root was found. */
861 ri->ri_cno = cno++;
862 ri->ri_super_root = pseg_end;
863 ri->ri_lsegs_start = ri->ri_lsegs_end = 0;
865 nilfs_dispose_segment_list(&segments);
866 nilfs->ns_pseg_offset = (sr_pseg_start = pseg_start)
867 + ssi.nblocks - seg_start;
868 nilfs->ns_seg_seq = seg_seq;
869 nilfs->ns_segnum = segnum;
870 nilfs->ns_cno = cno; /* nilfs->ns_cno = ri->ri_cno + 1 */
871 nilfs->ns_ctime = ssi.ctime;
872 nilfs->ns_nextnum = nextnum;
874 if (scan_newer)
875 ri->ri_need_recovery = NILFS_RECOVERY_SR_UPDATED;
876 else {
877 if (nilfs->ns_mount_state & NILFS_VALID_FS)
878 goto super_root_found;
879 scan_newer = 1;
882 /* reset region for roll-forward */
883 pseg_start += ssi.nblocks;
884 if (pseg_start < seg_end)
885 continue;
886 goto feed_segment;
888 try_next_pseg:
889 /* Standing on a course, or met an inconsistent state */
890 pseg_start += ssi.nblocks;
891 if (pseg_start < seg_end)
892 continue;
893 goto feed_segment;
895 strayed:
896 /* Off the trail */
897 if (!scan_newer)
899 * This can happen if a checkpoint was written without
900 * barriers, or as a result of an I/O failure.
902 goto failed;
904 feed_segment:
905 /* Looking to the next full segment */
906 if (empty_seg++)
907 goto super_root_found; /* found a valid super root */
909 ret = nilfs_segment_list_add(&segments, segnum);
910 if (unlikely(ret))
911 goto failed;
913 seg_seq++;
914 segnum = nextnum;
915 nilfs_get_segment_range(nilfs, segnum, &seg_start, &seg_end);
916 pseg_start = seg_start;
919 super_root_found:
920 /* Updating pointers relating to the latest checkpoint */
921 list_splice(&segments, ri->ri_used_segments.prev);
922 nilfs->ns_last_pseg = sr_pseg_start;
923 nilfs->ns_last_seq = nilfs->ns_seg_seq;
924 nilfs->ns_last_cno = ri->ri_cno;
925 return 0;
927 failed:
928 nilfs_dispose_segment_list(&segments);
929 return (ret < 0) ? ret : nilfs_warn_segment_error(ret);