nilfs2: get rid of ns_free_segments_count
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / nilfs2 / recovery.c
blobf5d9c3f954aeecb4ed8afd9805f59ac8e4bead70
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/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"
35 * Segment check result
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,
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_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;
91 return -EINVAL;
94 /**
95 * nilfs_compute_checksum - compute checksum of blocks continuously
96 * @nilfs: nilfs object
97 * @bhs: buffer head of start block
98 * @sum: place to store result
99 * @offset: offset bytes in the first block
100 * @check_bytes: number of bytes to be checked
101 * @start: DBN of start block
102 * @nblock: number of blocks to be checked
104 static int nilfs_compute_checksum(struct the_nilfs *nilfs,
105 struct buffer_head *bhs, u32 *sum,
106 unsigned long offset, u64 check_bytes,
107 sector_t start, unsigned long nblock)
109 unsigned int blocksize = nilfs->ns_blocksize;
110 unsigned long size;
111 u32 crc;
113 BUG_ON(offset >= blocksize);
114 check_bytes -= offset;
115 size = min_t(u64, check_bytes, blocksize - offset);
116 crc = crc32_le(nilfs->ns_crc_seed,
117 (unsigned char *)bhs->b_data + offset, size);
118 if (--nblock > 0) {
119 do {
120 struct buffer_head *bh;
122 bh = __bread(nilfs->ns_bdev, ++start, blocksize);
123 if (!bh)
124 return -EIO;
125 check_bytes -= size;
126 size = min_t(u64, check_bytes, blocksize);
127 crc = crc32_le(crc, bh->b_data, size);
128 brelse(bh);
129 } while (--nblock > 0);
131 *sum = crc;
132 return 0;
136 * nilfs_read_super_root_block - read super root block
137 * @nilfs: nilfs object
138 * @sr_block: disk block number of the super root block
139 * @pbh: address of a buffer_head pointer to return super root buffer
140 * @check: CRC check flag
142 int nilfs_read_super_root_block(struct the_nilfs *nilfs, sector_t sr_block,
143 struct buffer_head **pbh, int check)
145 struct buffer_head *bh_sr;
146 struct nilfs_super_root *sr;
147 u32 crc;
148 int ret;
150 *pbh = NULL;
151 bh_sr = __bread(nilfs->ns_bdev, sr_block, nilfs->ns_blocksize);
152 if (unlikely(!bh_sr)) {
153 ret = NILFS_SEG_FAIL_IO;
154 goto failed;
157 sr = (struct nilfs_super_root *)bh_sr->b_data;
158 if (check) {
159 unsigned bytes = le16_to_cpu(sr->sr_bytes);
161 if (bytes == 0 || bytes > nilfs->ns_blocksize) {
162 ret = NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT;
163 goto failed_bh;
165 if (nilfs_compute_checksum(
166 nilfs, bh_sr, &crc, sizeof(sr->sr_sum), bytes,
167 sr_block, 1)) {
168 ret = NILFS_SEG_FAIL_IO;
169 goto failed_bh;
171 if (crc != le32_to_cpu(sr->sr_sum)) {
172 ret = NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT;
173 goto failed_bh;
176 *pbh = bh_sr;
177 return 0;
179 failed_bh:
180 brelse(bh_sr);
182 failed:
183 return nilfs_warn_segment_error(ret);
187 * nilfs_read_log_header - read summary header of the specified log
188 * @nilfs: nilfs object
189 * @start_blocknr: start block number of the log
190 * @sum: pointer to return segment summary structure
192 static struct buffer_head *
193 nilfs_read_log_header(struct the_nilfs *nilfs, sector_t start_blocknr,
194 struct nilfs_segment_summary **sum)
196 struct buffer_head *bh_sum;
198 bh_sum = __bread(nilfs->ns_bdev, start_blocknr, nilfs->ns_blocksize);
199 if (bh_sum)
200 *sum = (struct nilfs_segment_summary *)bh_sum->b_data;
201 return bh_sum;
205 * nilfs_validate_log - verify consistency of log
206 * @nilfs: nilfs object
207 * @seg_seq: sequence number of segment
208 * @bh_sum: buffer head of summary block
209 * @sum: segment summary struct
211 static int nilfs_validate_log(struct the_nilfs *nilfs, u64 seg_seq,
212 struct buffer_head *bh_sum,
213 struct nilfs_segment_summary *sum)
215 unsigned long nblock;
216 u32 crc;
217 int ret;
219 ret = NILFS_SEG_FAIL_MAGIC;
220 if (le32_to_cpu(sum->ss_magic) != NILFS_SEGSUM_MAGIC)
221 goto out;
223 ret = NILFS_SEG_FAIL_SEQ;
224 if (le64_to_cpu(sum->ss_seq) != seg_seq)
225 goto out;
227 nblock = le32_to_cpu(sum->ss_nblocks);
228 ret = NILFS_SEG_FAIL_CONSISTENCY;
229 if (unlikely(nblock == 0 || nblock > nilfs->ns_blocks_per_segment))
230 /* This limits the number of blocks read in the CRC check */
231 goto out;
233 ret = NILFS_SEG_FAIL_IO;
234 if (nilfs_compute_checksum(nilfs, bh_sum, &crc, sizeof(sum->ss_datasum),
235 ((u64)nblock << nilfs->ns_blocksize_bits),
236 bh_sum->b_blocknr, nblock))
237 goto out;
239 ret = NILFS_SEG_FAIL_CHECKSUM_FULL;
240 if (crc != le32_to_cpu(sum->ss_datasum))
241 goto out;
242 ret = 0;
243 out:
244 return ret;
248 * nilfs_read_summary_info - read an item on summary blocks of a log
249 * @nilfs: nilfs object
250 * @pbh: the current buffer head on summary blocks [in, out]
251 * @offset: the current byte offset on summary blocks [in, out]
252 * @bytes: byte size of the item to be read
254 static void *nilfs_read_summary_info(struct the_nilfs *nilfs,
255 struct buffer_head **pbh,
256 unsigned int *offset, unsigned int bytes)
258 void *ptr;
259 sector_t blocknr;
261 BUG_ON((*pbh)->b_size < *offset);
262 if (bytes > (*pbh)->b_size - *offset) {
263 blocknr = (*pbh)->b_blocknr;
264 brelse(*pbh);
265 *pbh = __bread(nilfs->ns_bdev, blocknr + 1,
266 nilfs->ns_blocksize);
267 if (unlikely(!*pbh))
268 return NULL;
269 *offset = 0;
271 ptr = (*pbh)->b_data + *offset;
272 *offset += bytes;
273 return ptr;
277 * nilfs_skip_summary_info - skip items on summary blocks of a log
278 * @nilfs: nilfs object
279 * @pbh: the current buffer head on summary blocks [in, out]
280 * @offset: the current byte offset on summary blocks [in, out]
281 * @bytes: byte size of the item to be skipped
282 * @count: number of items to be skipped
284 static void nilfs_skip_summary_info(struct the_nilfs *nilfs,
285 struct buffer_head **pbh,
286 unsigned int *offset, unsigned int bytes,
287 unsigned long count)
289 unsigned int rest_item_in_current_block
290 = ((*pbh)->b_size - *offset) / bytes;
292 if (count <= rest_item_in_current_block) {
293 *offset += bytes * count;
294 } else {
295 sector_t blocknr = (*pbh)->b_blocknr;
296 unsigned int nitem_per_block = (*pbh)->b_size / bytes;
297 unsigned int bcnt;
299 count -= rest_item_in_current_block;
300 bcnt = DIV_ROUND_UP(count, nitem_per_block);
301 *offset = bytes * (count - (bcnt - 1) * nitem_per_block);
303 brelse(*pbh);
304 *pbh = __bread(nilfs->ns_bdev, blocknr + bcnt,
305 nilfs->ns_blocksize);
310 * nilfs_scan_dsync_log - get block information of a log written for data sync
311 * @nilfs: nilfs object
312 * @start_blocknr: start block number of the log
313 * @sum: log summary information
314 * @head: list head to add nilfs_recovery_block struct
316 static int nilfs_scan_dsync_log(struct the_nilfs *nilfs, sector_t start_blocknr,
317 struct nilfs_segment_summary *sum,
318 struct list_head *head)
320 struct buffer_head *bh;
321 unsigned int offset;
322 u32 nfinfo, sumbytes;
323 sector_t blocknr;
324 ino_t ino;
325 int err = -EIO;
327 nfinfo = le32_to_cpu(sum->ss_nfinfo);
328 if (!nfinfo)
329 return 0;
331 sumbytes = le32_to_cpu(sum->ss_sumbytes);
332 blocknr = start_blocknr + DIV_ROUND_UP(sumbytes, nilfs->ns_blocksize);
333 bh = __bread(nilfs->ns_bdev, start_blocknr, nilfs->ns_blocksize);
334 if (unlikely(!bh))
335 goto out;
337 offset = le16_to_cpu(sum->ss_bytes);
338 for (;;) {
339 unsigned long nblocks, ndatablk, nnodeblk;
340 struct nilfs_finfo *finfo;
342 finfo = nilfs_read_summary_info(nilfs, &bh, &offset,
343 sizeof(*finfo));
344 if (unlikely(!finfo))
345 goto out;
347 ino = le64_to_cpu(finfo->fi_ino);
348 nblocks = le32_to_cpu(finfo->fi_nblocks);
349 ndatablk = le32_to_cpu(finfo->fi_ndatablk);
350 nnodeblk = nblocks - ndatablk;
352 while (ndatablk-- > 0) {
353 struct nilfs_recovery_block *rb;
354 struct nilfs_binfo_v *binfo;
356 binfo = nilfs_read_summary_info(nilfs, &bh, &offset,
357 sizeof(*binfo));
358 if (unlikely(!binfo))
359 goto out;
361 rb = kmalloc(sizeof(*rb), GFP_NOFS);
362 if (unlikely(!rb)) {
363 err = -ENOMEM;
364 goto out;
366 rb->ino = ino;
367 rb->blocknr = blocknr++;
368 rb->vblocknr = le64_to_cpu(binfo->bi_vblocknr);
369 rb->blkoff = le64_to_cpu(binfo->bi_blkoff);
370 /* INIT_LIST_HEAD(&rb->list); */
371 list_add_tail(&rb->list, head);
373 if (--nfinfo == 0)
374 break;
375 blocknr += nnodeblk; /* always 0 for data sync logs */
376 nilfs_skip_summary_info(nilfs, &bh, &offset, sizeof(__le64),
377 nnodeblk);
378 if (unlikely(!bh))
379 goto out;
381 err = 0;
382 out:
383 brelse(bh); /* brelse(NULL) is just ignored */
384 return err;
387 static void dispose_recovery_list(struct list_head *head)
389 while (!list_empty(head)) {
390 struct nilfs_recovery_block *rb
391 = list_entry(head->next,
392 struct nilfs_recovery_block, list);
393 list_del(&rb->list);
394 kfree(rb);
398 struct nilfs_segment_entry {
399 struct list_head list;
400 __u64 segnum;
403 static int nilfs_segment_list_add(struct list_head *head, __u64 segnum)
405 struct nilfs_segment_entry *ent = kmalloc(sizeof(*ent), GFP_NOFS);
407 if (unlikely(!ent))
408 return -ENOMEM;
410 ent->segnum = segnum;
411 INIT_LIST_HEAD(&ent->list);
412 list_add_tail(&ent->list, head);
413 return 0;
416 void nilfs_dispose_segment_list(struct list_head *head)
418 while (!list_empty(head)) {
419 struct nilfs_segment_entry *ent
420 = list_entry(head->next,
421 struct nilfs_segment_entry, list);
422 list_del(&ent->list);
423 kfree(ent);
427 static int nilfs_prepare_segment_for_recovery(struct the_nilfs *nilfs,
428 struct nilfs_sb_info *sbi,
429 struct nilfs_recovery_info *ri)
431 struct list_head *head = &ri->ri_used_segments;
432 struct nilfs_segment_entry *ent, *n;
433 struct inode *sufile = nilfs->ns_sufile;
434 __u64 segnum[4];
435 int err;
436 int i;
438 segnum[0] = nilfs->ns_segnum;
439 segnum[1] = nilfs->ns_nextnum;
440 segnum[2] = ri->ri_segnum;
441 segnum[3] = ri->ri_nextnum;
443 nilfs_attach_writer(nilfs, sbi);
445 * Releasing the next segment of the latest super root.
446 * The next segment is invalidated by this recovery.
448 err = nilfs_sufile_free(sufile, segnum[1]);
449 if (unlikely(err))
450 goto failed;
452 for (i = 1; i < 4; i++) {
453 err = nilfs_segment_list_add(head, segnum[i]);
454 if (unlikely(err))
455 goto failed;
459 * Collecting segments written after the latest super root.
460 * These are marked dirty to avoid being reallocated in the next write.
462 list_for_each_entry_safe(ent, n, head, list) {
463 if (ent->segnum != segnum[0]) {
464 err = nilfs_sufile_scrap(sufile, ent->segnum);
465 if (unlikely(err))
466 goto failed;
468 list_del(&ent->list);
469 kfree(ent);
472 /* Allocate new segments for recovery */
473 err = nilfs_sufile_alloc(sufile, &segnum[0]);
474 if (unlikely(err))
475 goto failed;
477 nilfs->ns_pseg_offset = 0;
478 nilfs->ns_seg_seq = ri->ri_seq + 2;
479 nilfs->ns_nextnum = nilfs->ns_segnum = segnum[0];
481 failed:
482 /* No need to recover sufile because it will be destroyed on error */
483 nilfs_detach_writer(nilfs, sbi);
484 return err;
487 static int nilfs_recovery_copy_block(struct the_nilfs *nilfs,
488 struct nilfs_recovery_block *rb,
489 struct page *page)
491 struct buffer_head *bh_org;
492 void *kaddr;
494 bh_org = __bread(nilfs->ns_bdev, rb->blocknr, nilfs->ns_blocksize);
495 if (unlikely(!bh_org))
496 return -EIO;
498 kaddr = kmap_atomic(page, KM_USER0);
499 memcpy(kaddr + bh_offset(bh_org), bh_org->b_data, bh_org->b_size);
500 kunmap_atomic(kaddr, KM_USER0);
501 brelse(bh_org);
502 return 0;
505 static int nilfs_recover_dsync_blocks(struct the_nilfs *nilfs,
506 struct nilfs_sb_info *sbi,
507 struct list_head *head,
508 unsigned long *nr_salvaged_blocks)
510 struct inode *inode;
511 struct nilfs_recovery_block *rb, *n;
512 unsigned blocksize = nilfs->ns_blocksize;
513 struct page *page;
514 loff_t pos;
515 int err = 0, err2 = 0;
517 list_for_each_entry_safe(rb, n, head, list) {
518 inode = nilfs_iget(sbi->s_super, rb->ino);
519 if (IS_ERR(inode)) {
520 err = PTR_ERR(inode);
521 inode = NULL;
522 goto failed_inode;
525 pos = rb->blkoff << inode->i_blkbits;
526 page = NULL;
527 err = block_write_begin(NULL, inode->i_mapping, pos, blocksize,
528 0, &page, NULL, nilfs_get_block);
529 if (unlikely(err))
530 goto failed_inode;
532 err = nilfs_recovery_copy_block(nilfs, rb, page);
533 if (unlikely(err))
534 goto failed_page;
536 err = nilfs_set_file_dirty(sbi, inode, 1);
537 if (unlikely(err))
538 goto failed_page;
540 block_write_end(NULL, inode->i_mapping, pos, blocksize,
541 blocksize, page, NULL);
543 unlock_page(page);
544 page_cache_release(page);
546 (*nr_salvaged_blocks)++;
547 goto next;
549 failed_page:
550 unlock_page(page);
551 page_cache_release(page);
553 failed_inode:
554 printk(KERN_WARNING
555 "NILFS warning: error recovering data block "
556 "(err=%d, ino=%lu, block-offset=%llu)\n",
557 err, (unsigned long)rb->ino,
558 (unsigned long long)rb->blkoff);
559 if (!err2)
560 err2 = err;
561 next:
562 iput(inode); /* iput(NULL) is just ignored */
563 list_del_init(&rb->list);
564 kfree(rb);
566 return err2;
570 * nilfs_do_roll_forward - salvage logical segments newer than the latest
571 * checkpoint
572 * @nilfs: nilfs object
573 * @sbi: nilfs_sb_info
574 * @ri: pointer to a nilfs_recovery_info
576 static int nilfs_do_roll_forward(struct the_nilfs *nilfs,
577 struct nilfs_sb_info *sbi,
578 struct nilfs_recovery_info *ri)
580 struct buffer_head *bh_sum = NULL;
581 struct nilfs_segment_summary *sum;
582 sector_t pseg_start;
583 sector_t seg_start, seg_end; /* Starting/ending DBN of full segment */
584 unsigned long nsalvaged_blocks = 0;
585 unsigned int flags;
586 u64 seg_seq;
587 __u64 segnum, nextnum = 0;
588 int empty_seg = 0;
589 int err = 0, ret;
590 LIST_HEAD(dsync_blocks); /* list of data blocks to be recovered */
591 enum {
592 RF_INIT_ST,
593 RF_DSYNC_ST, /* scanning data-sync segments */
595 int state = RF_INIT_ST;
597 nilfs_attach_writer(nilfs, sbi);
598 pseg_start = ri->ri_lsegs_start;
599 seg_seq = ri->ri_lsegs_start_seq;
600 segnum = nilfs_get_segnum_of_block(nilfs, pseg_start);
601 nilfs_get_segment_range(nilfs, segnum, &seg_start, &seg_end);
603 while (segnum != ri->ri_segnum || pseg_start <= ri->ri_pseg_start) {
604 brelse(bh_sum);
605 bh_sum = nilfs_read_log_header(nilfs, pseg_start, &sum);
606 if (!bh_sum) {
607 err = -EIO;
608 goto failed;
611 ret = nilfs_validate_log(nilfs, seg_seq, bh_sum, sum);
612 if (ret) {
613 if (ret == NILFS_SEG_FAIL_IO) {
614 err = -EIO;
615 goto failed;
617 goto strayed;
620 flags = le16_to_cpu(sum->ss_flags);
621 if (flags & NILFS_SS_SR)
622 goto confused;
624 /* Found a valid partial segment; do recovery actions */
625 nextnum = nilfs_get_segnum_of_block(nilfs,
626 le64_to_cpu(sum->ss_next));
627 empty_seg = 0;
628 nilfs->ns_ctime = le64_to_cpu(sum->ss_create);
629 if (!(flags & NILFS_SS_GC))
630 nilfs->ns_nongc_ctime = nilfs->ns_ctime;
632 switch (state) {
633 case RF_INIT_ST:
634 if (!(flags & NILFS_SS_LOGBGN) ||
635 !(flags & NILFS_SS_SYNDT))
636 goto try_next_pseg;
637 state = RF_DSYNC_ST;
638 /* Fall through */
639 case RF_DSYNC_ST:
640 if (!(flags & NILFS_SS_SYNDT))
641 goto confused;
643 err = nilfs_scan_dsync_log(nilfs, pseg_start, sum,
644 &dsync_blocks);
645 if (unlikely(err))
646 goto failed;
647 if (flags & NILFS_SS_LOGEND) {
648 err = nilfs_recover_dsync_blocks(
649 nilfs, sbi, &dsync_blocks,
650 &nsalvaged_blocks);
651 if (unlikely(err))
652 goto failed;
653 state = RF_INIT_ST;
655 break; /* Fall through to try_next_pseg */
658 try_next_pseg:
659 if (pseg_start == ri->ri_lsegs_end)
660 break;
661 pseg_start += le32_to_cpu(sum->ss_nblocks);
662 if (pseg_start < seg_end)
663 continue;
664 goto feed_segment;
666 strayed:
667 if (pseg_start == ri->ri_lsegs_end)
668 break;
670 feed_segment:
671 /* Looking to the next full segment */
672 if (empty_seg++)
673 break;
674 seg_seq++;
675 segnum = nextnum;
676 nilfs_get_segment_range(nilfs, segnum, &seg_start, &seg_end);
677 pseg_start = seg_start;
680 if (nsalvaged_blocks) {
681 printk(KERN_INFO "NILFS (device %s): salvaged %lu blocks\n",
682 sbi->s_super->s_id, nsalvaged_blocks);
683 ri->ri_need_recovery = NILFS_RECOVERY_ROLLFORWARD_DONE;
685 out:
686 brelse(bh_sum);
687 dispose_recovery_list(&dsync_blocks);
688 nilfs_detach_writer(nilfs, sbi);
689 return err;
691 confused:
692 err = -EINVAL;
693 failed:
694 printk(KERN_ERR
695 "NILFS (device %s): Error roll-forwarding "
696 "(err=%d, pseg block=%llu). ",
697 sbi->s_super->s_id, err, (unsigned long long)pseg_start);
698 goto out;
701 static void nilfs_finish_roll_forward(struct the_nilfs *nilfs,
702 struct nilfs_recovery_info *ri)
704 struct buffer_head *bh;
705 int err;
707 if (nilfs_get_segnum_of_block(nilfs, ri->ri_lsegs_start) !=
708 nilfs_get_segnum_of_block(nilfs, ri->ri_super_root))
709 return;
711 bh = __getblk(nilfs->ns_bdev, ri->ri_lsegs_start, nilfs->ns_blocksize);
712 BUG_ON(!bh);
713 memset(bh->b_data, 0, bh->b_size);
714 set_buffer_dirty(bh);
715 err = sync_dirty_buffer(bh);
716 if (unlikely(err))
717 printk(KERN_WARNING
718 "NILFS warning: buffer sync write failed during "
719 "post-cleaning of recovery.\n");
720 brelse(bh);
724 * nilfs_salvage_orphan_logs - salvage logs written after the latest checkpoint
725 * @nilfs: nilfs object
726 * @sbi: nilfs_sb_info
727 * @ri: pointer to a nilfs_recovery_info struct to store search results.
729 * Return Value: On success, 0 is returned. On error, one of the following
730 * negative error code is returned.
732 * %-EINVAL - Inconsistent filesystem state.
734 * %-EIO - I/O error
736 * %-ENOSPC - No space left on device (only in a panic state).
738 * %-ERESTARTSYS - Interrupted.
740 * %-ENOMEM - Insufficient memory available.
742 int nilfs_salvage_orphan_logs(struct the_nilfs *nilfs,
743 struct nilfs_sb_info *sbi,
744 struct nilfs_recovery_info *ri)
746 int err;
748 if (ri->ri_lsegs_start == 0 || ri->ri_lsegs_end == 0)
749 return 0;
751 err = nilfs_attach_checkpoint(sbi, ri->ri_cno);
752 if (unlikely(err)) {
753 printk(KERN_ERR
754 "NILFS: error loading the latest checkpoint.\n");
755 return err;
758 err = nilfs_do_roll_forward(nilfs, sbi, ri);
759 if (unlikely(err))
760 goto failed;
762 if (ri->ri_need_recovery == NILFS_RECOVERY_ROLLFORWARD_DONE) {
763 err = nilfs_prepare_segment_for_recovery(nilfs, sbi, ri);
764 if (unlikely(err)) {
765 printk(KERN_ERR "NILFS: Error preparing segments for "
766 "recovery.\n");
767 goto failed;
770 err = nilfs_attach_segment_constructor(sbi);
771 if (unlikely(err))
772 goto failed;
774 set_nilfs_discontinued(nilfs);
775 err = nilfs_construct_segment(sbi->s_super);
776 nilfs_detach_segment_constructor(sbi);
778 if (unlikely(err)) {
779 printk(KERN_ERR "NILFS: Oops! recovery failed. "
780 "(err=%d)\n", err);
781 goto failed;
784 nilfs_finish_roll_forward(nilfs, ri);
787 failed:
788 nilfs_detach_checkpoint(sbi);
789 return err;
793 * nilfs_search_super_root - search the latest valid super root
794 * @nilfs: the_nilfs
795 * @ri: pointer to a nilfs_recovery_info struct to store search results.
797 * nilfs_search_super_root() looks for the latest super-root from a partial
798 * segment pointed by the superblock. It sets up struct the_nilfs through
799 * this search. It fills nilfs_recovery_info (ri) required for recovery.
801 * Return Value: On success, 0 is returned. On error, one of the following
802 * negative error code is returned.
804 * %-EINVAL - No valid segment found
806 * %-EIO - I/O error
808 int nilfs_search_super_root(struct the_nilfs *nilfs,
809 struct nilfs_recovery_info *ri)
811 struct buffer_head *bh_sum = NULL;
812 struct nilfs_segment_summary *sum;
813 sector_t pseg_start, pseg_end, sr_pseg_start = 0;
814 sector_t seg_start, seg_end; /* range of full segment (block number) */
815 sector_t b, end;
816 unsigned long nblocks;
817 unsigned int flags;
818 u64 seg_seq;
819 __u64 segnum, nextnum = 0;
820 __u64 cno;
821 LIST_HEAD(segments);
822 int empty_seg = 0, scan_newer = 0;
823 int ret;
825 pseg_start = nilfs->ns_last_pseg;
826 seg_seq = nilfs->ns_last_seq;
827 cno = nilfs->ns_last_cno;
828 segnum = nilfs_get_segnum_of_block(nilfs, pseg_start);
830 /* Calculate range of segment */
831 nilfs_get_segment_range(nilfs, segnum, &seg_start, &seg_end);
833 /* Read ahead segment */
834 b = seg_start;
835 while (b <= seg_end)
836 __breadahead(nilfs->ns_bdev, b++, nilfs->ns_blocksize);
838 for (;;) {
839 brelse(bh_sum);
840 ret = NILFS_SEG_FAIL_IO;
841 bh_sum = nilfs_read_log_header(nilfs, pseg_start, &sum);
842 if (!bh_sum)
843 goto failed;
845 ret = nilfs_validate_log(nilfs, seg_seq, bh_sum, sum);
846 if (ret) {
847 if (ret == NILFS_SEG_FAIL_IO)
848 goto failed;
849 goto strayed;
852 nblocks = le32_to_cpu(sum->ss_nblocks);
853 pseg_end = pseg_start + nblocks - 1;
854 if (unlikely(pseg_end > seg_end)) {
855 ret = NILFS_SEG_FAIL_CONSISTENCY;
856 goto strayed;
859 /* A valid partial segment */
860 ri->ri_pseg_start = pseg_start;
861 ri->ri_seq = seg_seq;
862 ri->ri_segnum = segnum;
863 nextnum = nilfs_get_segnum_of_block(nilfs,
864 le64_to_cpu(sum->ss_next));
865 ri->ri_nextnum = nextnum;
866 empty_seg = 0;
868 flags = le16_to_cpu(sum->ss_flags);
869 if (!(flags & NILFS_SS_SR) && !scan_newer) {
870 /* This will never happen because a superblock
871 (last_segment) always points to a pseg
872 having a super root. */
873 ret = NILFS_SEG_FAIL_CONSISTENCY;
874 goto failed;
877 if (pseg_start == seg_start) {
878 nilfs_get_segment_range(nilfs, nextnum, &b, &end);
879 while (b <= end)
880 __breadahead(nilfs->ns_bdev, b++,
881 nilfs->ns_blocksize);
883 if (!(flags & NILFS_SS_SR)) {
884 if (!ri->ri_lsegs_start && (flags & NILFS_SS_LOGBGN)) {
885 ri->ri_lsegs_start = pseg_start;
886 ri->ri_lsegs_start_seq = seg_seq;
888 if (flags & NILFS_SS_LOGEND)
889 ri->ri_lsegs_end = pseg_start;
890 goto try_next_pseg;
893 /* A valid super root was found. */
894 ri->ri_cno = cno++;
895 ri->ri_super_root = pseg_end;
896 ri->ri_lsegs_start = ri->ri_lsegs_end = 0;
898 nilfs_dispose_segment_list(&segments);
899 sr_pseg_start = pseg_start;
900 nilfs->ns_pseg_offset = pseg_start + nblocks - seg_start;
901 nilfs->ns_seg_seq = seg_seq;
902 nilfs->ns_segnum = segnum;
903 nilfs->ns_cno = cno; /* nilfs->ns_cno = ri->ri_cno + 1 */
904 nilfs->ns_ctime = le64_to_cpu(sum->ss_create);
905 nilfs->ns_nextnum = nextnum;
907 if (scan_newer)
908 ri->ri_need_recovery = NILFS_RECOVERY_SR_UPDATED;
909 else {
910 if (nilfs->ns_mount_state & NILFS_VALID_FS)
911 goto super_root_found;
912 scan_newer = 1;
915 try_next_pseg:
916 /* Standing on a course, or met an inconsistent state */
917 pseg_start += nblocks;
918 if (pseg_start < seg_end)
919 continue;
920 goto feed_segment;
922 strayed:
923 /* Off the trail */
924 if (!scan_newer)
926 * This can happen if a checkpoint was written without
927 * barriers, or as a result of an I/O failure.
929 goto failed;
931 feed_segment:
932 /* Looking to the next full segment */
933 if (empty_seg++)
934 goto super_root_found; /* found a valid super root */
936 ret = nilfs_segment_list_add(&segments, segnum);
937 if (unlikely(ret))
938 goto failed;
940 seg_seq++;
941 segnum = nextnum;
942 nilfs_get_segment_range(nilfs, segnum, &seg_start, &seg_end);
943 pseg_start = seg_start;
946 super_root_found:
947 /* Updating pointers relating to the latest checkpoint */
948 brelse(bh_sum);
949 list_splice_tail(&segments, &ri->ri_used_segments);
950 nilfs->ns_last_pseg = sr_pseg_start;
951 nilfs->ns_last_seq = nilfs->ns_seg_seq;
952 nilfs->ns_last_cno = ri->ri_cno;
953 return 0;
955 failed:
956 brelse(bh_sum);
957 nilfs_dispose_segment_list(&segments);
958 return (ret < 0) ? ret : nilfs_warn_segment_error(ret);