^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) * the_nilfs.c - the_nilfs shared structure.
^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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/buffer_head.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/backing-dev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/crc32.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "nilfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "segment.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include "alloc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include "cpfile.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include "sufile.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include "dat.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include "segbuf.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static int nilfs_valid_sb(struct nilfs_super_block *sbp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) void nilfs_set_last_segment(struct the_nilfs *nilfs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) sector_t start_blocknr, u64 seq, __u64 cno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) spin_lock(&nilfs->ns_last_segment_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) nilfs->ns_last_pseg = start_blocknr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) nilfs->ns_last_seq = seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) nilfs->ns_last_cno = cno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) if (!nilfs_sb_dirty(nilfs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) if (nilfs->ns_prev_seq == nilfs->ns_last_seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) goto stay_cursor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) set_nilfs_sb_dirty(nilfs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) nilfs->ns_prev_seq = nilfs->ns_last_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) stay_cursor:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) spin_unlock(&nilfs->ns_last_segment_lock);
^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) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * alloc_nilfs - allocate a nilfs object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * @sb: super block instance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * Return Value: On success, pointer to the_nilfs is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * On error, NULL is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) struct the_nilfs *alloc_nilfs(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) struct the_nilfs *nilfs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) nilfs = kzalloc(sizeof(*nilfs), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if (!nilfs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) nilfs->ns_sb = sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) nilfs->ns_bdev = sb->s_bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) atomic_set(&nilfs->ns_ndirtyblks, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) init_rwsem(&nilfs->ns_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) mutex_init(&nilfs->ns_snapshot_mount_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) INIT_LIST_HEAD(&nilfs->ns_dirty_files);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) INIT_LIST_HEAD(&nilfs->ns_gc_inodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) spin_lock_init(&nilfs->ns_inode_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) spin_lock_init(&nilfs->ns_next_gen_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) spin_lock_init(&nilfs->ns_last_segment_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) nilfs->ns_cptree = RB_ROOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) spin_lock_init(&nilfs->ns_cptree_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) init_rwsem(&nilfs->ns_segctor_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) nilfs->ns_sb_update_freq = NILFS_SB_FREQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return nilfs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^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) * destroy_nilfs - destroy nilfs object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * @nilfs: nilfs object to be released
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) void destroy_nilfs(struct the_nilfs *nilfs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) might_sleep();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if (nilfs_init(nilfs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) nilfs_sysfs_delete_device_group(nilfs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) brelse(nilfs->ns_sbh[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) brelse(nilfs->ns_sbh[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) kfree(nilfs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) static int nilfs_load_super_root(struct the_nilfs *nilfs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) struct super_block *sb, sector_t sr_block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) struct buffer_head *bh_sr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) struct nilfs_super_root *raw_sr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) struct nilfs_super_block **sbp = nilfs->ns_sbp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) struct nilfs_inode *rawi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) unsigned int dat_entry_size, segment_usage_size, checkpoint_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) unsigned int inode_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) err = nilfs_read_super_root_block(nilfs, sr_block, &bh_sr, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (unlikely(err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) down_read(&nilfs->ns_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) dat_entry_size = le16_to_cpu(sbp[0]->s_dat_entry_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) checkpoint_size = le16_to_cpu(sbp[0]->s_checkpoint_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) segment_usage_size = le16_to_cpu(sbp[0]->s_segment_usage_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) up_read(&nilfs->ns_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) inode_size = nilfs->ns_inode_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) rawi = (void *)bh_sr->b_data + NILFS_SR_DAT_OFFSET(inode_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) err = nilfs_dat_read(sb, dat_entry_size, rawi, &nilfs->ns_dat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) rawi = (void *)bh_sr->b_data + NILFS_SR_CPFILE_OFFSET(inode_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) err = nilfs_cpfile_read(sb, checkpoint_size, rawi, &nilfs->ns_cpfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) goto failed_dat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) rawi = (void *)bh_sr->b_data + NILFS_SR_SUFILE_OFFSET(inode_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) err = nilfs_sufile_read(sb, segment_usage_size, rawi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) &nilfs->ns_sufile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) goto failed_cpfile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) raw_sr = (struct nilfs_super_root *)bh_sr->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) nilfs->ns_nongc_ctime = le64_to_cpu(raw_sr->sr_nongc_ctime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) brelse(bh_sr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) failed_cpfile:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) iput(nilfs->ns_cpfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) failed_dat:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) iput(nilfs->ns_dat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) static void nilfs_init_recovery_info(struct nilfs_recovery_info *ri)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) memset(ri, 0, sizeof(*ri));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) INIT_LIST_HEAD(&ri->ri_used_segments);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) static void nilfs_clear_recovery_info(struct nilfs_recovery_info *ri)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) nilfs_dispose_segment_list(&ri->ri_used_segments);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) * nilfs_store_log_cursor - load log cursor from a super block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * @nilfs: nilfs object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * @sbp: buffer storing super block to be read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) * nilfs_store_log_cursor() reads the last position of the log
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) * containing a super root from a given super block, and initializes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) * relevant information on the nilfs object preparatory for log
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) * scanning and recovery.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) static int nilfs_store_log_cursor(struct the_nilfs *nilfs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) struct nilfs_super_block *sbp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) nilfs->ns_last_pseg = le64_to_cpu(sbp->s_last_pseg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) nilfs->ns_last_cno = le64_to_cpu(sbp->s_last_cno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) nilfs->ns_last_seq = le64_to_cpu(sbp->s_last_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) nilfs->ns_prev_seq = nilfs->ns_last_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) nilfs->ns_seg_seq = nilfs->ns_last_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) nilfs->ns_segnum =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) nilfs_get_segnum_of_block(nilfs, nilfs->ns_last_pseg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) nilfs->ns_cno = nilfs->ns_last_cno + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) if (nilfs->ns_segnum >= nilfs->ns_nsegments) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) nilfs_err(nilfs->ns_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) "pointed segment number is out of range: segnum=%llu, nsegments=%lu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) (unsigned long long)nilfs->ns_segnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) nilfs->ns_nsegments);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) * load_nilfs - load and recover the nilfs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * @nilfs: the_nilfs structure to be released
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) * @sb: super block isntance used to recover past segment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) * load_nilfs() searches and load the latest super root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) * attaches the last segment, and does recovery if needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) * The caller must call this exclusively for simultaneous mounts.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) int load_nilfs(struct the_nilfs *nilfs, struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) struct nilfs_recovery_info ri;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) unsigned int s_flags = sb->s_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) int really_read_only = bdev_read_only(nilfs->ns_bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) int valid_fs = nilfs_valid_fs(nilfs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (!valid_fs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) nilfs_warn(sb, "mounting unchecked fs");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) if (s_flags & SB_RDONLY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) nilfs_info(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) "recovery required for readonly filesystem");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) nilfs_info(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) "write access will be enabled during recovery");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) nilfs_init_recovery_info(&ri);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) err = nilfs_search_super_root(nilfs, &ri);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) if (unlikely(err)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) struct nilfs_super_block **sbp = nilfs->ns_sbp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) int blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) if (err != -EINVAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) goto scan_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) if (!nilfs_valid_sb(sbp[1])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) nilfs_warn(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) "unable to fall back to spare super block");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) goto scan_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) nilfs_info(sb, "trying rollback from an earlier position");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) * restore super block with its spare and reconfigure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) * relevant states of the nilfs object.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) memcpy(sbp[0], sbp[1], nilfs->ns_sbsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) nilfs->ns_crc_seed = le32_to_cpu(sbp[0]->s_crc_seed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) /* verify consistency between two super blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) blocksize = BLOCK_SIZE << le32_to_cpu(sbp[0]->s_log_block_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) if (blocksize != nilfs->ns_blocksize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) nilfs_warn(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) "blocksize differs between two super blocks (%d != %d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) blocksize, nilfs->ns_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) goto scan_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) err = nilfs_store_log_cursor(nilfs, sbp[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) goto scan_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) /* drop clean flag to allow roll-forward and recovery */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) nilfs->ns_mount_state &= ~NILFS_VALID_FS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) valid_fs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) err = nilfs_search_super_root(nilfs, &ri);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) goto scan_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) err = nilfs_load_super_root(nilfs, sb, ri.ri_super_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) if (unlikely(err)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) nilfs_err(sb, "error %d while loading super root", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) if (valid_fs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) goto skip_recovery;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) if (s_flags & SB_RDONLY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) __u64 features;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) if (nilfs_test_opt(nilfs, NORECOVERY)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) nilfs_info(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) "norecovery option specified, skipping roll-forward recovery");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) goto skip_recovery;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) features = le64_to_cpu(nilfs->ns_sbp[0]->s_feature_compat_ro) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) ~NILFS_FEATURE_COMPAT_RO_SUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) if (features) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) nilfs_err(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) "couldn't proceed with recovery because of unsupported optional features (%llx)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) (unsigned long long)features);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) err = -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) goto failed_unload;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) if (really_read_only) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) nilfs_err(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) "write access unavailable, cannot proceed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) err = -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) goto failed_unload;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) sb->s_flags &= ~SB_RDONLY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) } else if (nilfs_test_opt(nilfs, NORECOVERY)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) nilfs_err(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) "recovery cancelled because norecovery option was specified for a read/write mount");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) goto failed_unload;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) err = nilfs_salvage_orphan_logs(nilfs, sb, &ri);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) goto failed_unload;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) down_write(&nilfs->ns_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) nilfs->ns_mount_state |= NILFS_VALID_FS; /* set "clean" flag */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) err = nilfs_cleanup_super(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) up_write(&nilfs->ns_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) nilfs_err(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) "error %d updating super block. recovery unfinished.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) goto failed_unload;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) nilfs_info(sb, "recovery complete");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) skip_recovery:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) nilfs_clear_recovery_info(&ri);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) sb->s_flags = s_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) scan_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) nilfs_err(sb, "error %d while searching super root", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) failed_unload:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) iput(nilfs->ns_cpfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) iput(nilfs->ns_sufile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) iput(nilfs->ns_dat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) nilfs_clear_recovery_info(&ri);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) sb->s_flags = s_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) static unsigned long long nilfs_max_size(unsigned int blkbits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) unsigned int max_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) unsigned long long res = MAX_LFS_FILESIZE; /* page cache limit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) max_bits = blkbits + NILFS_BMAP_KEY_BIT; /* bmap size limit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) if (max_bits < 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) res = min_t(unsigned long long, res, (1ULL << max_bits) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) * nilfs_nrsvsegs - calculate the number of reserved segments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) * @nilfs: nilfs object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) * @nsegs: total number of segments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) unsigned long nilfs_nrsvsegs(struct the_nilfs *nilfs, unsigned long nsegs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) return max_t(unsigned long, NILFS_MIN_NRSVSEGS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) DIV_ROUND_UP(nsegs * nilfs->ns_r_segments_percentage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 100));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) void nilfs_set_nsegments(struct the_nilfs *nilfs, unsigned long nsegs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) nilfs->ns_nsegments = nsegs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) nilfs->ns_nrsvsegs = nilfs_nrsvsegs(nilfs, nsegs);
^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 int nilfs_store_disk_layout(struct the_nilfs *nilfs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) struct nilfs_super_block *sbp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) if (le32_to_cpu(sbp->s_rev_level) < NILFS_MIN_SUPP_REV) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) nilfs_err(nilfs->ns_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) "unsupported revision (superblock rev.=%d.%d, current rev.=%d.%d). Please check the version of mkfs.nilfs(2).",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) le32_to_cpu(sbp->s_rev_level),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) le16_to_cpu(sbp->s_minor_rev_level),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) NILFS_CURRENT_REV, NILFS_MINOR_REV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) nilfs->ns_sbsize = le16_to_cpu(sbp->s_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) if (nilfs->ns_sbsize > BLOCK_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) nilfs->ns_inode_size = le16_to_cpu(sbp->s_inode_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) if (nilfs->ns_inode_size > nilfs->ns_blocksize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) nilfs_err(nilfs->ns_sb, "too large inode size: %d bytes",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) nilfs->ns_inode_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) } else if (nilfs->ns_inode_size < NILFS_MIN_INODE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) nilfs_err(nilfs->ns_sb, "too small inode size: %d bytes",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) nilfs->ns_inode_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) nilfs->ns_first_ino = le32_to_cpu(sbp->s_first_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) nilfs->ns_blocks_per_segment = le32_to_cpu(sbp->s_blocks_per_segment);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) if (nilfs->ns_blocks_per_segment < NILFS_SEG_MIN_BLOCKS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) nilfs_err(nilfs->ns_sb, "too short segment: %lu blocks",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) nilfs->ns_blocks_per_segment);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) nilfs->ns_first_data_block = le64_to_cpu(sbp->s_first_data_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) nilfs->ns_r_segments_percentage =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) le32_to_cpu(sbp->s_r_segments_percentage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) if (nilfs->ns_r_segments_percentage < 1 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) nilfs->ns_r_segments_percentage > 99) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) nilfs_err(nilfs->ns_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) "invalid reserved segments percentage: %lu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) nilfs->ns_r_segments_percentage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) nilfs_set_nsegments(nilfs, le64_to_cpu(sbp->s_nsegments));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) nilfs->ns_crc_seed = le32_to_cpu(sbp->s_crc_seed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) static int nilfs_valid_sb(struct nilfs_super_block *sbp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) static unsigned char sum[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) const int sumoff = offsetof(struct nilfs_super_block, s_sum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) size_t bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) u32 crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) if (!sbp || le16_to_cpu(sbp->s_magic) != NILFS_SUPER_MAGIC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) bytes = le16_to_cpu(sbp->s_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) if (bytes < sumoff + 4 || bytes > BLOCK_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) crc = crc32_le(le32_to_cpu(sbp->s_crc_seed), (unsigned char *)sbp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) sumoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) crc = crc32_le(crc, sum, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) crc = crc32_le(crc, (unsigned char *)sbp + sumoff + 4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) bytes - sumoff - 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) return crc == le32_to_cpu(sbp->s_sum);
^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) static int nilfs_sb2_bad_offset(struct nilfs_super_block *sbp, u64 offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) return offset < ((le64_to_cpu(sbp->s_nsegments) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) le32_to_cpu(sbp->s_blocks_per_segment)) <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) (le32_to_cpu(sbp->s_log_block_size) + 10));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) static void nilfs_release_super_block(struct the_nilfs *nilfs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) for (i = 0; i < 2; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) if (nilfs->ns_sbp[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) brelse(nilfs->ns_sbh[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) nilfs->ns_sbh[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) nilfs->ns_sbp[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) void nilfs_fall_back_super_block(struct the_nilfs *nilfs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) brelse(nilfs->ns_sbh[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) nilfs->ns_sbh[1] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) nilfs->ns_sbp[1] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) void nilfs_swap_super_block(struct the_nilfs *nilfs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) struct buffer_head *tsbh = nilfs->ns_sbh[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) struct nilfs_super_block *tsbp = nilfs->ns_sbp[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) nilfs->ns_sbh[1] = tsbh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) nilfs->ns_sbp[1] = tsbp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) static int nilfs_load_super_block(struct the_nilfs *nilfs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) struct super_block *sb, int blocksize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) struct nilfs_super_block **sbpp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) struct nilfs_super_block **sbp = nilfs->ns_sbp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) struct buffer_head **sbh = nilfs->ns_sbh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) u64 sb2off = NILFS_SB2_OFFSET_BYTES(nilfs->ns_bdev->bd_inode->i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) int valid[2], swp = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) sbp[0] = nilfs_read_super_block(sb, NILFS_SB_OFFSET_BYTES, blocksize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) &sbh[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) sbp[1] = nilfs_read_super_block(sb, sb2off, blocksize, &sbh[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) if (!sbp[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) if (!sbp[1]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) nilfs_err(sb, "unable to read superblock");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) nilfs_warn(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) "unable to read primary superblock (blocksize = %d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) } else if (!sbp[1]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) nilfs_warn(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) "unable to read secondary superblock (blocksize = %d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) * Compare two super blocks and set 1 in swp if the secondary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) * super block is valid and newer. Otherwise, set 0 in swp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) valid[0] = nilfs_valid_sb(sbp[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) valid[1] = nilfs_valid_sb(sbp[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) swp = valid[1] && (!valid[0] ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) le64_to_cpu(sbp[1]->s_last_cno) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) le64_to_cpu(sbp[0]->s_last_cno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) if (valid[swp] && nilfs_sb2_bad_offset(sbp[swp], sb2off)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) brelse(sbh[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) sbh[1] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) sbp[1] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) valid[1] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) swp = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) if (!valid[swp]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) nilfs_release_super_block(nilfs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) nilfs_err(sb, "couldn't find nilfs on the device");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) if (!valid[!swp])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) nilfs_warn(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) "broken superblock, retrying with spare superblock (blocksize = %d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) if (swp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) nilfs_swap_super_block(nilfs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) nilfs->ns_sbwcount = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) nilfs->ns_prot_seq = le64_to_cpu(sbp[valid[1] & !swp]->s_last_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) *sbpp = sbp[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) * init_nilfs - initialize a NILFS instance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) * @nilfs: the_nilfs structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) * @sb: super block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) * @data: mount options
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) * init_nilfs() performs common initialization per block device (e.g.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) * reading the super block, getting disk layout information, initializing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) * shared fields in the_nilfs).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) * Return Value: On success, 0 is returned. On error, a negative error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) * code is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) int init_nilfs(struct the_nilfs *nilfs, struct super_block *sb, char *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) struct nilfs_super_block *sbp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) int blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) down_write(&nilfs->ns_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) blocksize = sb_min_blocksize(sb, NILFS_MIN_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) if (!blocksize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) nilfs_err(sb, "unable to set blocksize");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) err = nilfs_store_magic_and_option(sb, sbp, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) goto failed_sbh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) err = nilfs_check_feature_compatibility(sb, sbp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) goto failed_sbh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) if (blocksize < NILFS_MIN_BLOCK_SIZE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) blocksize > NILFS_MAX_BLOCK_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) nilfs_err(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) "couldn't mount because of unsupported filesystem blocksize %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) goto failed_sbh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) if (sb->s_blocksize != blocksize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) int hw_blocksize = bdev_logical_block_size(sb->s_bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) if (blocksize < hw_blocksize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) nilfs_err(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) "blocksize %d too small for device (sector-size = %d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) blocksize, hw_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) goto failed_sbh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) nilfs_release_super_block(nilfs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) sb_set_blocksize(sb, blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) * Not to failed_sbh; sbh is released automatically
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) * when reloading fails.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) nilfs->ns_blocksize_bits = sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) nilfs->ns_blocksize = blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) get_random_bytes(&nilfs->ns_next_generation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) sizeof(nilfs->ns_next_generation));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) err = nilfs_store_disk_layout(nilfs, sbp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) goto failed_sbh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) nilfs->ns_mount_state = le16_to_cpu(sbp->s_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) err = nilfs_store_log_cursor(nilfs, sbp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) goto failed_sbh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) err = nilfs_sysfs_create_device_group(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) goto failed_sbh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) set_nilfs_init(nilfs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) up_write(&nilfs->ns_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) failed_sbh:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) nilfs_release_super_block(nilfs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) int nilfs_discard_segments(struct the_nilfs *nilfs, __u64 *segnump,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) size_t nsegs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) sector_t seg_start, seg_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) sector_t start = 0, nblocks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) unsigned int sects_per_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) __u64 *sn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) sects_per_block = (1 << nilfs->ns_blocksize_bits) /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) bdev_logical_block_size(nilfs->ns_bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) for (sn = segnump; sn < segnump + nsegs; sn++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) nilfs_get_segment_range(nilfs, *sn, &seg_start, &seg_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) if (!nblocks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) start = seg_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) nblocks = seg_end - seg_start + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) } else if (start + nblocks == seg_start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) nblocks += seg_end - seg_start + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) ret = blkdev_issue_discard(nilfs->ns_bdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) start * sects_per_block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) nblocks * sects_per_block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) GFP_NOFS, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) nblocks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) if (nblocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) ret = blkdev_issue_discard(nilfs->ns_bdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) start * sects_per_block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) nblocks * sects_per_block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) GFP_NOFS, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) return ret;
^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) int nilfs_count_free_blocks(struct the_nilfs *nilfs, sector_t *nblocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) unsigned long ncleansegs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) *nblocks = (sector_t)ncleansegs * nilfs->ns_blocks_per_segment;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) int nilfs_near_disk_full(struct the_nilfs *nilfs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) unsigned long ncleansegs, nincsegs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) nincsegs = atomic_read(&nilfs->ns_ndirtyblks) /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) nilfs->ns_blocks_per_segment + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) return ncleansegs <= nilfs->ns_nrsvsegs + nincsegs;
^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) struct nilfs_root *nilfs_lookup_root(struct the_nilfs *nilfs, __u64 cno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) struct rb_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) struct nilfs_root *root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) spin_lock(&nilfs->ns_cptree_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) n = nilfs->ns_cptree.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) while (n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) root = rb_entry(n, struct nilfs_root, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) if (cno < root->cno) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) n = n->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) } else if (cno > root->cno) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) n = n->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) refcount_inc(&root->count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) spin_unlock(&nilfs->ns_cptree_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) return root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) spin_unlock(&nilfs->ns_cptree_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) struct nilfs_root *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) nilfs_find_or_create_root(struct the_nilfs *nilfs, __u64 cno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) struct rb_node **p, *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) struct nilfs_root *root, *new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) root = nilfs_lookup_root(nilfs, cno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) if (root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) return root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) new = kzalloc(sizeof(*root), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) if (!new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) spin_lock(&nilfs->ns_cptree_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) p = &nilfs->ns_cptree.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) while (*p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) parent = *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) root = rb_entry(parent, struct nilfs_root, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) if (cno < root->cno) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) p = &(*p)->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) } else if (cno > root->cno) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) p = &(*p)->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) refcount_inc(&root->count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) spin_unlock(&nilfs->ns_cptree_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) kfree(new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) return root;
^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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) new->cno = cno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) new->ifile = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) new->nilfs = nilfs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) refcount_set(&new->count, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) atomic64_set(&new->inodes_count, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) atomic64_set(&new->blocks_count, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) rb_link_node(&new->rb_node, parent, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) rb_insert_color(&new->rb_node, &nilfs->ns_cptree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) spin_unlock(&nilfs->ns_cptree_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) err = nilfs_sysfs_create_snapshot_group(new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) kfree(new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) new = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) return new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) void nilfs_put_root(struct nilfs_root *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) struct the_nilfs *nilfs = root->nilfs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) if (refcount_dec_and_lock(&root->count, &nilfs->ns_cptree_lock)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) rb_erase(&root->rb_node, &nilfs->ns_cptree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) spin_unlock(&nilfs->ns_cptree_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) nilfs_sysfs_delete_snapshot_group(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) iput(root->ifile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) kfree(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) }