/* work structure for recovery */ struct nilfs_recovery_block {
ino_t ino; /* * Inode number of the file that this block * belongs to
*/
sector_t blocknr; /* block number */
__u64 vblocknr; /* virtual block number */ unsignedlong blkoff; /* File offset of the data block (per block) */ struct list_head list;
};
switch (err) { case NILFS_SEG_FAIL_IO:
nilfs_err(sb, "I/O error reading segment"); return -EIO; case NILFS_SEG_FAIL_MAGIC:
msg = "Magic number mismatch"; break; case NILFS_SEG_FAIL_SEQ:
msg = "Sequence number mismatch"; break; case NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT:
msg = "Checksum error in super root"; break; case NILFS_SEG_FAIL_CHECKSUM_FULL:
msg = "Checksum error in segment payload"; break; case NILFS_SEG_FAIL_CONSISTENCY:
msg = "Inconsistency found"; break; case NILFS_SEG_NO_SUPER_ROOT:
msg = "No super root in the last segment"; break; default:
nilfs_err(sb, "unrecognized segment error %d", err); return -EINVAL;
}
nilfs_warn(sb, "invalid segment: %s", msg); return -EINVAL;
}
/** * nilfs_compute_checksum - compute checksum of blocks continuously * @nilfs: nilfs object * @bhs: buffer head of start block * @sum: place to store result * @offset: offset bytes in the first block * @check_bytes: number of bytes to be checked * @start: DBN of start block * @nblock: number of blocks to be checked * * Return: 0 on success, or %-EIO if an I/O error occurs.
*/ staticint nilfs_compute_checksum(struct the_nilfs *nilfs, struct buffer_head *bhs, u32 *sum, unsignedlong offset, u64 check_bytes,
sector_t start, unsignedlong nblock)
{ unsignedint blocksize = nilfs->ns_blocksize; unsignedlong size;
u32 crc;
/** * nilfs_read_super_root_block - read super root block * @nilfs: nilfs object * @sr_block: disk block number of the super root block * @pbh: address of a buffer_head pointer to return super root buffer * @check: CRC check flag * * Return: 0 on success, or one of the following negative error codes on * failure: * * %-EINVAL - Super root block corrupted. * * %-EIO - I/O error.
*/ int nilfs_read_super_root_block(struct the_nilfs *nilfs, sector_t sr_block, struct buffer_head **pbh, int check)
{ struct buffer_head *bh_sr; struct nilfs_super_root *sr;
u32 crc; int ret;
*pbh = NULL;
bh_sr = __bread(nilfs->ns_bdev, sr_block, nilfs->ns_blocksize); if (unlikely(!bh_sr)) {
ret = NILFS_SEG_FAIL_IO; goto failed;
}
sr = (struct nilfs_super_root *)bh_sr->b_data; if (check) { unsignedint bytes = le16_to_cpu(sr->sr_bytes);
if (bytes == 0 || bytes > nilfs->ns_blocksize) {
ret = NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT; goto failed_bh;
} if (nilfs_compute_checksum(
nilfs, bh_sr, &crc, sizeof(sr->sr_sum), bytes,
sr_block, 1)) {
ret = NILFS_SEG_FAIL_IO; goto failed_bh;
} if (crc != le32_to_cpu(sr->sr_sum)) {
ret = NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT; goto failed_bh;
}
}
*pbh = bh_sr; return 0;
/** * nilfs_validate_log - verify consistency of log * @nilfs: nilfs object * @seg_seq: sequence number of segment * @bh_sum: buffer head of summary block * @sum: segment summary struct * * Return: 0 on success, or one of the following internal codes on failure: * * %NILFS_SEG_FAIL_MAGIC - Magic number mismatch. * * %NILFS_SEG_FAIL_SEQ - Sequence number mismatch. * * %NIFLS_SEG_FAIL_CONSISTENCY - Block count out of range. * * %NILFS_SEG_FAIL_IO - I/O error. * * %NILFS_SEG_FAIL_CHECKSUM_FULL - Full log checksum verification failed.
*/ staticint nilfs_validate_log(struct the_nilfs *nilfs, u64 seg_seq, struct buffer_head *bh_sum, struct nilfs_segment_summary *sum)
{ unsignedlong nblock;
u32 crc; int ret;
ret = NILFS_SEG_FAIL_MAGIC; if (le32_to_cpu(sum->ss_magic) != NILFS_SEGSUM_MAGIC) goto out;
ret = NILFS_SEG_FAIL_SEQ; if (le64_to_cpu(sum->ss_seq) != seg_seq) goto out;
nblock = le32_to_cpu(sum->ss_nblocks);
ret = NILFS_SEG_FAIL_CONSISTENCY; if (unlikely(nblock == 0 || nblock > nilfs->ns_blocks_per_segment)) /* This limits the number of blocks read in the CRC check */ goto out;
ret = NILFS_SEG_FAIL_IO; if (nilfs_compute_checksum(nilfs, bh_sum, &crc, sizeof(sum->ss_datasum),
((u64)nblock << nilfs->ns_blocksize_bits),
bh_sum->b_blocknr, nblock)) goto out;
ret = NILFS_SEG_FAIL_CHECKSUM_FULL; if (crc != le32_to_cpu(sum->ss_datasum)) goto out;
ret = 0;
out: return ret;
}
/** * nilfs_read_summary_info - read an item on summary blocks of a log * @nilfs: nilfs object * @pbh: the current buffer head on summary blocks [in, out] * @offset: the current byte offset on summary blocks [in, out] * @bytes: byte size of the item to be read * * Return: Kernel space address of current segment summary entry, or * NULL if an I/O error occurs.
*/ staticvoid *nilfs_read_summary_info(struct the_nilfs *nilfs, struct buffer_head **pbh, unsignedint *offset, unsignedint bytes)
{ void *ptr;
sector_t blocknr;
/** * nilfs_skip_summary_info - skip items on summary blocks of a log * @nilfs: nilfs object * @pbh: the current buffer head on summary blocks [in, out] * @offset: the current byte offset on summary blocks [in, out] * @bytes: byte size of the item to be skipped * @count: number of items to be skipped
*/ staticvoid nilfs_skip_summary_info(struct the_nilfs *nilfs, struct buffer_head **pbh, unsignedint *offset, unsignedint bytes, unsignedlong count)
{ unsignedint rest_item_in_current_block
= ((*pbh)->b_size - *offset) / bytes;
/** * nilfs_scan_dsync_log - get block information of a log written for data sync * @nilfs: nilfs object * @start_blocknr: start block number of the log * @sum: log summary information * @head: list head to add nilfs_recovery_block struct * * Return: 0 on success, or one of the following negative error codes on * failure: * * %-EIO - I/O error. * * %-ENOMEM - Insufficient memory available.
*/ staticint nilfs_scan_dsync_log(struct the_nilfs *nilfs, sector_t start_blocknr, struct nilfs_segment_summary *sum, struct list_head *head)
{ struct buffer_head *bh; unsignedint offset;
u32 nfinfo, sumbytes;
sector_t blocknr;
ino_t ino; int err = -EIO;
nfinfo = le32_to_cpu(sum->ss_nfinfo); if (!nfinfo) return 0;
/* * Releasing the next segment of the latest super root. * The next segment is invalidated by this recovery.
*/
err = nilfs_sufile_free(sufile, segnum[1]); if (unlikely(err)) { if (err == -ENOENT) {
nilfs_err(sb, "checkpoint log inconsistency at block %llu (segment %llu): next segment %llu is unallocated",
(unsignedlonglong)nilfs->ns_last_pseg,
(unsignedlonglong)nilfs->ns_segnum,
(unsignedlonglong)segnum[1]);
err = -EINVAL;
} goto failed;
}
for (i = 1; i < 4; i++) {
err = nilfs_segment_list_add(head, segnum[i]); if (unlikely(err)) goto failed;
}
/* * Collecting segments written after the latest super root. * These are marked dirty to avoid being reallocated in the next write.
*/
list_for_each_entry_safe(ent, n, head, list) { if (ent->segnum != segnum[0]) {
err = nilfs_sufile_scrap(sufile, ent->segnum); if (unlikely(err)) goto failed;
}
list_del(&ent->list);
kfree(ent);
}
/* Allocate new segments for recovery */
err = nilfs_sufile_alloc(sufile, &segnum[0]); if (unlikely(err)) goto failed;
ret = nilfs_validate_log(nilfs, seg_seq, bh_sum, sum); if (ret) { if (ret == NILFS_SEG_FAIL_IO) {
err = -EIO; goto failed;
} goto strayed;
}
flags = le16_to_cpu(sum->ss_flags); if (flags & NILFS_SS_SR) goto confused;
/* Found a valid partial segment; do recovery actions */
nextnum = nilfs_get_segnum_of_block(nilfs,
le64_to_cpu(sum->ss_next));
empty_seg = 0;
nilfs->ns_ctime = le64_to_cpu(sum->ss_create); if (!(flags & NILFS_SS_GC))
nilfs->ns_nongc_ctime = nilfs->ns_ctime;
switch (state) { case RF_INIT_ST: if (!(flags & NILFS_SS_LOGBGN) ||
!(flags & NILFS_SS_SYNDT)) goto try_next_pseg;
state = RF_DSYNC_ST;
fallthrough; case RF_DSYNC_ST: if (!(flags & NILFS_SS_SYNDT)) goto confused;
err = nilfs_scan_dsync_log(nilfs, pseg_start, sum,
&dsync_blocks); if (unlikely(err)) goto failed; if (flags & NILFS_SS_LOGEND) {
err = nilfs_recover_dsync_blocks(
nilfs, sb, root, &dsync_blocks,
&nsalvaged_blocks); if (unlikely(err)) goto failed;
state = RF_INIT_ST;
} break; /* Fall through to try_next_pseg */
}
try_next_pseg: if (pseg_start == ri->ri_lsegs_end) break;
pseg_start += le32_to_cpu(sum->ss_nblocks); if (pseg_start < seg_end) continue; goto feed_segment;
strayed: if (pseg_start == ri->ri_lsegs_end) break;
feed_segment: /* Looking to the next full segment */ if (empty_seg++) break;
seg_seq++;
segnum = nextnum;
nilfs_get_segment_range(nilfs, segnum, &seg_start, &seg_end);
pseg_start = seg_start;
}
err = sync_dirty_buffer(bh); if (unlikely(err))
nilfs_warn(nilfs->ns_sb, "buffer sync write failed during post-cleaning of recovery.");
brelse(bh);
}
/** * nilfs_abort_roll_forward - cleaning up after a failed rollforward recovery * @nilfs: nilfs object
*/ staticvoid nilfs_abort_roll_forward(struct the_nilfs *nilfs)
{ struct nilfs_inode_info *ii, *n;
LIST_HEAD(head);
/* Abandon inodes that have read recovery data */
spin_lock(&nilfs->ns_inode_lock);
list_splice_init(&nilfs->ns_dirty_files, &head);
spin_unlock(&nilfs->ns_inode_lock); if (list_empty(&head)) return;
set_nilfs_purging(nilfs);
list_for_each_entry_safe(ii, n, &head, i_dirty) {
spin_lock(&nilfs->ns_inode_lock);
list_del_init(&ii->i_dirty);
spin_unlock(&nilfs->ns_inode_lock);
/** * nilfs_salvage_orphan_logs - salvage logs written after the latest checkpoint * @nilfs: nilfs object * @sb: super block instance * @ri: pointer to a nilfs_recovery_info struct to store search results. * * Return: 0 on success, or one of the following negative error codes on * failure: * * %-EINVAL - Inconsistent filesystem state. * * %-EIO - I/O error. * * %-ENOMEM - Insufficient memory available. * * %-ENOSPC - No space left on device (only in a panic state). * * %-ERESTARTSYS - Interrupted.
*/ int nilfs_salvage_orphan_logs(struct the_nilfs *nilfs, struct super_block *sb, struct nilfs_recovery_info *ri)
{ struct nilfs_root *root; int err;
if (ri->ri_lsegs_start == 0 || ri->ri_lsegs_end == 0) return 0;
err = nilfs_attach_checkpoint(sb, ri->ri_cno, true, &root); if (unlikely(err)) {
nilfs_err(sb, "error %d loading the latest checkpoint", err); return err;
}
err = nilfs_do_roll_forward(nilfs, sb, root, ri); if (unlikely(err)) goto failed;
if (ri->ri_need_recovery == NILFS_RECOVERY_ROLLFORWARD_DONE) {
err = nilfs_prepare_segment_for_recovery(nilfs, sb, ri); if (unlikely(err)) {
nilfs_err(sb, "error %d preparing segment for recovery",
err); goto failed;
}
err = nilfs_attach_log_writer(sb, root); if (unlikely(err)) goto failed;
/** * nilfs_search_super_root - search the latest valid super root * @nilfs: the_nilfs * @ri: pointer to a nilfs_recovery_info struct to store search results. * * nilfs_search_super_root() looks for the latest super-root from a partial * segment pointed by the superblock. It sets up struct the_nilfs through * this search. It fills nilfs_recovery_info (ri) required for recovery. * * Return: 0 on success, or one of the following negative error codes on * failure: * * %-EINVAL - No valid segment found. * * %-EIO - I/O error. * * %-ENOMEM - Insufficient memory available.
*/ int nilfs_search_super_root(struct the_nilfs *nilfs, struct nilfs_recovery_info *ri)
{ struct buffer_head *bh_sum = NULL; struct nilfs_segment_summary *sum = NULL;
sector_t pseg_start, pseg_end, sr_pseg_start = 0;
sector_t seg_start, seg_end; /* range of full segment (block number) */
sector_t b, end; unsignedlong nblocks; unsignedint flags;
u64 seg_seq;
__u64 segnum, nextnum = 0;
__u64 cno;
LIST_HEAD(segments); int empty_seg = 0, scan_newer = 0; int ret;
flags = le16_to_cpu(sum->ss_flags); if (!(flags & NILFS_SS_SR) && !scan_newer) { /* * This will never happen because a superblock * (last_segment) always points to a pseg with * a super root.
*/
ret = NILFS_SEG_FAIL_CONSISTENCY; goto failed;
}
if (pseg_start == seg_start) {
nilfs_get_segment_range(nilfs, nextnum, &b, &end); while (b <= end)
__breadahead(nilfs->ns_bdev, b++,
nilfs->ns_blocksize);
} if (!(flags & NILFS_SS_SR)) { if (!ri->ri_lsegs_start && (flags & NILFS_SS_LOGBGN)) {
ri->ri_lsegs_start = pseg_start;
ri->ri_lsegs_start_seq = seg_seq;
} if (flags & NILFS_SS_LOGEND)
ri->ri_lsegs_end = pseg_start; goto try_next_pseg;
}
/* A valid super root was found. */
ri->ri_cno = cno++;
ri->ri_super_root = pseg_end;
ri->ri_lsegs_start = ri->ri_lsegs_end = 0;
if (scan_newer)
ri->ri_need_recovery = NILFS_RECOVERY_SR_UPDATED; else { if (nilfs->ns_mount_state & NILFS_VALID_FS) goto super_root_found;
scan_newer = 1;
}
try_next_pseg: /* Standing on a course, or met an inconsistent state */
pseg_start += nblocks; if (pseg_start < seg_end) continue; goto feed_segment;
strayed: /* Off the trail */ if (!scan_newer) /* * This can happen if a checkpoint was written without * barriers, or as a result of an I/O failure.
*/ goto failed;
feed_segment: /* Looking to the next full segment */ if (empty_seg++) goto super_root_found; /* found a valid super root */
ret = nilfs_segment_list_add(&segments, segnum); if (unlikely(ret)) goto failed;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.