Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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