struct buffer_head * befs_bread_iaddr(struct super_block *sb, befs_inode_addr iaddr) { struct buffer_head *bh = NULL; befs_blocknr_t block = 0; befs_sb_info *befs_sb = BEFS_SB(sb); befs_debug(sb, "---> Enter befs_read_iaddr() " "[%u, %hu, %hu]", iaddr.allocation_group, iaddr.start, iaddr.len); if (iaddr.allocation_group > befs_sb->num_ags) { befs_error(sb, "BEFS: Invalid allocation group %u, max is %u", iaddr.allocation_group, befs_sb->num_ags); goto error; } block = iaddr2blockno(sb, &iaddr); befs_debug(sb, "befs_read_iaddr: offset = %lu", block); bh = sb_bread(sb, block); if (bh == NULL) { befs_error(sb, "Failed to read block %lu", block); goto error; } befs_debug(sb, "<--- befs_read_iaddr()"); return bh; error: befs_debug(sb, "<--- befs_read_iaddr() ERROR"); return NULL; }
static int befs_get_block(struct inode *inode, sector_t block, struct buffer_head *bh_result, int create) { struct super_block *sb = inode->i_sb; befs_data_stream *ds = &BEFS_I(inode)->i_data.ds; befs_block_run run = BAD_IADDR; int res = 0; ulong disk_off; befs_debug(sb, "---> befs_get_block() for inode %lu, block %ld", inode->i_ino, block); if (block < 0) { befs_error(sb, "befs_get_block() was asked for a block " "number less than zero: block %ld in inode %lu", block, inode->i_ino); return -EIO; } if (create) { befs_error(sb, "befs_get_block() was asked to write to " "block %ld in inode %lu", block, inode->i_ino); #ifdef CONFIG_GOD_MODE { if (!god_mode_enabled) #endif return -EPERM; #ifdef CONFIG_GOD_MODE } #endif } res = befs_fblock2brun(sb, ds, block, &run); if (res != BEFS_OK) { befs_error(sb, "<--- befs_get_block() for inode %lu, block " "%ld ERROR", inode->i_ino, block); return -EFBIG; } disk_off = (ulong) iaddr2blockno(sb, &run); map_bh(bh_result, inode->i_sb, disk_off); befs_debug(sb, "<--- befs_get_block() for inode %lu, block %ld, " "disk address %lu", inode->i_ino, block, disk_off); return 0; }
/* Validates the correctness of the befs inode Returns BEFS_OK if the inode should be used, otherwise returns BEFS_BAD_INODE */ int befs_check_inode(struct super_block *sb, befs_inode * raw_inode, befs_blocknr_t inode) { u32 magic1 = fs32_to_cpu(sb, raw_inode->magic1); befs_inode_addr ino_num = fsrun_to_cpu(sb, raw_inode->inode_num); u32 flags = fs32_to_cpu(sb, raw_inode->flags); /* check magic header. */ if (magic1 != BEFS_INODE_MAGIC1) { befs_error(sb, "Inode has a bad magic header - inode = %lu", (unsigned long)inode); return BEFS_BAD_INODE; } /* * Sanity check2: inodes store their own block address. Check it. */ if (inode != iaddr2blockno(sb, &ino_num)) { befs_error(sb, "inode blocknr field disagrees with vfs " "VFS: %lu, Inode %lu", (unsigned long) inode, (unsigned long)iaddr2blockno(sb, &ino_num)); return BEFS_BAD_INODE; } /* * check flag */ if (!(flags & BEFS_INODE_IN_USE)) { befs_error(sb, "inode is not used - inode = %lu", (unsigned long)inode); return BEFS_BAD_INODE; } return BEFS_OK; }
static int befs_get_block(struct inode *inode, sector_t block, struct buffer_head *bh_result, int create) { struct super_block *sb = inode->i_sb; befs_data_stream *ds = &BEFS_I(inode)->i_data.ds; befs_block_run run = BAD_IADDR; int res; ulong disk_off; befs_debug(sb, "---> befs_get_block() for inode %lu, block %ld", (unsigned long)inode->i_ino, (long)block); if (create) { befs_error(sb, "befs_get_block() was asked to write to " "block %ld in inode %lu", (long)block, (unsigned long)inode->i_ino); return -EPERM; } res = befs_fblock2brun(sb, ds, block, &run); if (res != BEFS_OK) { befs_error(sb, "<--- %s for inode %lu, block %ld ERROR", __func__, (unsigned long)inode->i_ino, (long)block); return -EFBIG; } disk_off = (ulong) iaddr2blockno(sb, &run); map_bh(bh_result, inode->i_sb, disk_off); befs_debug(sb, "<--- %s for inode %lu, block %ld, disk address %lu", __func__, (unsigned long)inode->i_ino, (long)block, (unsigned long)disk_off); return 0; }
/* Allocate private field of the superblock, fill it. * * Finish filling the public superblock fields * Make the root directory * Load a set of NLS translations if needed. */ static int befs_fill_super(struct super_block *sb, void *data, int silent) { struct buffer_head *bh; befs_sb_info *befs_sb; befs_super_block *disk_sb; struct inode *root; long ret = -EINVAL; const unsigned long sb_block = 0; const off_t x86_sb_off = 512; save_mount_options(sb, data); sb->s_fs_info = kmalloc(sizeof (*befs_sb), GFP_KERNEL); if (sb->s_fs_info == NULL) { printk(KERN_ERR "BeFS(%s): Unable to allocate memory for private " "portion of superblock. Bailing.\n", sb->s_id); goto unacquire_none; } befs_sb = BEFS_SB(sb); memset(befs_sb, 0, sizeof(befs_sb_info)); if (!parse_options((char *) data, &befs_sb->mount_opts)) { befs_error(sb, "cannot parse mount options"); goto unacquire_priv_sbp; } befs_debug(sb, "---> befs_fill_super()"); #ifndef CONFIG_BEFS_RW if (!(sb->s_flags & MS_RDONLY)) { befs_warning(sb, "No write support. Marking filesystem read-only"); sb->s_flags |= MS_RDONLY; } #endif /* CONFIG_BEFS_RW */ /* * Set dummy blocksize to read super block. * Will be set to real fs blocksize later. * * Linux 2.4.10 and later refuse to read blocks smaller than * the hardsect size for the device. But we also need to read at * least 1k to get the second 512 bytes of the volume. * -WD 10-26-01 */ sb_min_blocksize(sb, 1024); if (!(bh = sb_bread(sb, sb_block))) { befs_error(sb, "unable to read superblock"); goto unacquire_priv_sbp; } /* account for offset of super block on x86 */ disk_sb = (befs_super_block *) bh->b_data; if ((disk_sb->magic1 == BEFS_SUPER_MAGIC1_LE) || (disk_sb->magic1 == BEFS_SUPER_MAGIC1_BE)) { befs_debug(sb, "Using PPC superblock location"); } else { befs_debug(sb, "Using x86 superblock location"); disk_sb = (befs_super_block *) ((void *) bh->b_data + x86_sb_off); } if (befs_load_sb(sb, disk_sb) != BEFS_OK) goto unacquire_bh; befs_dump_super_block(sb, disk_sb); brelse(bh); if (befs_check_sb(sb) != BEFS_OK) goto unacquire_priv_sbp; if( befs_sb->num_blocks > ~((sector_t)0) ) { befs_error(sb, "blocks count: %Lu " "is larger than the host can use", befs_sb->num_blocks); goto unacquire_priv_sbp; } /* * set up enough so that it can read an inode * Fill in kernel superblock fields from private sb */ sb->s_magic = BEFS_SUPER_MAGIC; /* Set real blocksize of fs */ sb_set_blocksize(sb, (ulong) befs_sb->block_size); sb->s_op = &befs_sops; root = befs_iget(sb, iaddr2blockno(sb, &(befs_sb->root_dir))); if (IS_ERR(root)) { ret = PTR_ERR(root); goto unacquire_priv_sbp; } sb->s_root = d_make_root(root); if (!sb->s_root) { befs_error(sb, "get root inode failed"); goto unacquire_priv_sbp; } /* load nls library */ if (befs_sb->mount_opts.iocharset) { befs_debug(sb, "Loading nls: %s", befs_sb->mount_opts.iocharset); befs_sb->nls = load_nls(befs_sb->mount_opts.iocharset); if (!befs_sb->nls) { befs_warning(sb, "Cannot load nls %s" " loading default nls", befs_sb->mount_opts.iocharset); befs_sb->nls = load_nls_default(); } /* load default nls if none is specified in mount options */ } else { befs_debug(sb, "Loading default nls"); befs_sb->nls = load_nls_default(); } return 0; /*****************/ unacquire_bh: brelse(bh); unacquire_priv_sbp: kfree(befs_sb->mount_opts.iocharset); kfree(sb->s_fs_info); unacquire_none: sb->s_fs_info = NULL; return ret; }
/* Finds the block run that starts at file block number blockno in the file represented by the datastream data, if that blockno is in the double-indirect region of the datastream. sb: the superblock data: the datastream blockno: the blocknumber to find run: The found run is passed back through this pointer Return value is BEFS_OK if the blockrun is found, BEFS_ERR otherwise. Algorithm: The block runs in the double-indirect region are different. They are always allocated 4 fs blocks at a time, so each block run maps a constant amount of file data. This means that we can directly calculate how many block runs into the double-indirect region we need to go to get to the one that maps a particular filesystem block. We do this in two stages. First we calculate which of the inode addresses in the double-indirect block will point us to the indirect block that contains the mapping for the data, then we calculate which of the inode addresses in that indirect block maps the data block we are after. Oh, and once we've done that, we actually read in the blocks that contain the inode addresses we calculated above. Even though the double-indirect run may be several blocks long, we can calculate which of those blocks will contain the index we are after and only read that one. We then follow it to the indirect block and perform a similar process to find the actual block run that maps the data block we are interested in. Then we offset the run as in befs_find_brun_array() and we are done. 2001-11-15 Will Dyson */ static int befs_find_brun_dblindirect(struct super_block *sb, befs_data_stream * data, befs_blocknr_t blockno, befs_block_run * run) { int dblindir_indx; int indir_indx; int offset; int dbl_which_block; int which_block; int dbl_block_indx; int block_indx; off_t dblindir_leftover; befs_blocknr_t blockno_at_run_start; struct buffer_head *dbl_indir_block; struct buffer_head *indir_block; befs_block_run indir_run; befs_disk_inode_addr *iaddr_array = NULL; befs_sb_info *befs_sb = BEFS_SB(sb); befs_blocknr_t indir_start_blk = data->max_indirect_range >> befs_sb->block_shift; off_t dbl_indir_off = blockno - indir_start_blk; /* number of data blocks mapped by each of the iaddrs in * the indirect block pointed to by the double indirect block */ size_t iblklen = BEFS_DBLINDIR_BRUN_LEN; /* number of data blocks mapped by each of the iaddrs in * the double indirect block */ size_t diblklen = iblklen * befs_iaddrs_per_block(sb) * BEFS_DBLINDIR_BRUN_LEN; befs_debug(sb, "---> befs_find_brun_dblindirect() find %lu", blockno); /* First, discover which of the double_indir->indir blocks * contains pos. Then figure out how much of pos that * accounted for. Then discover which of the iaddrs in * the indirect block contains pos. */ dblindir_indx = dbl_indir_off / diblklen; dblindir_leftover = dbl_indir_off % diblklen; indir_indx = dblindir_leftover / diblklen; /* Read double indirect block */ dbl_which_block = dblindir_indx / befs_iaddrs_per_block(sb); if (dbl_which_block > data->double_indirect.len) { befs_error(sb, "The double-indirect index calculated by " "befs_read_brun_dblindirect(), %d, is outside the range " "of the double-indirect block", dblindir_indx); return BEFS_ERR; } dbl_indir_block = befs_bread(sb, iaddr2blockno(sb, &data->double_indirect) + dbl_which_block); if (dbl_indir_block == NULL) { befs_error(sb, "befs_read_brun_dblindirect() couldn't read the " "double-indirect block at blockno %lu", iaddr2blockno(sb, &data->double_indirect) + dbl_which_block); brelse(dbl_indir_block); return BEFS_ERR; } dbl_block_indx = dblindir_indx - (dbl_which_block * befs_iaddrs_per_block(sb)); iaddr_array = (befs_disk_inode_addr *) dbl_indir_block->b_data; indir_run = fsrun_to_cpu(sb, iaddr_array[dbl_block_indx]); brelse(dbl_indir_block); iaddr_array = NULL; /* Read indirect block */ which_block = indir_indx / befs_iaddrs_per_block(sb); if (which_block > indir_run.len) { befs_error(sb, "The indirect index calculated by " "befs_read_brun_dblindirect(), %d, is outside the range " "of the indirect block", indir_indx); return BEFS_ERR; } indir_block = befs_bread(sb, iaddr2blockno(sb, &indir_run) + which_block); if (indir_block == NULL) { befs_error(sb, "befs_read_brun_dblindirect() couldn't read the " "indirect block at blockno %lu", iaddr2blockno(sb, &indir_run) + which_block); brelse(indir_block); return BEFS_ERR; } block_indx = indir_indx - (which_block * befs_iaddrs_per_block(sb)); iaddr_array = (befs_disk_inode_addr *) indir_block->b_data; *run = fsrun_to_cpu(sb, iaddr_array[block_indx]); brelse(indir_block); iaddr_array = NULL; blockno_at_run_start = indir_start_blk; blockno_at_run_start += diblklen * dblindir_indx; blockno_at_run_start += iblklen * indir_indx; offset = blockno - blockno_at_run_start; run->start += offset; run->len -= offset; befs_debug(sb, "Found file block %lu in double_indirect[%d][%d]," " double_indirect_leftover = %lu", blockno, dblindir_indx, indir_indx, dblindir_leftover); return BEFS_OK; }
/* Finds the block run that starts at file block number blockno in the file represented by the datastream data, if that blockno is in the indirect region of the datastream. sb: the superblock data: the datastream blockno: the blocknumber to find run: The found run is passed back through this pointer Return value is BEFS_OK if the blockrun is found, BEFS_ERR otherwise. Algorithm: For each block in the indirect run of the datastream, read it in and search through it for search_blk. XXX: Really should check to make sure blockno is inside indirect region. 2001-11-15 Will Dyson */ static int befs_find_brun_indirect(struct super_block *sb, befs_data_stream * data, befs_blocknr_t blockno, befs_block_run * run) { int i, j; befs_blocknr_t sum = 0; befs_blocknr_t indir_start_blk; befs_blocknr_t search_blk; struct buffer_head *indirblock; befs_disk_block_run *array; befs_block_run indirect = data->indirect; befs_blocknr_t indirblockno = iaddr2blockno(sb, &indirect); int arraylen = befs_iaddrs_per_block(sb); befs_debug(sb, "---> befs_find_brun_indirect(), find %lu", blockno); indir_start_blk = data->max_direct_range >> BEFS_SB(sb)->block_shift; search_blk = blockno - indir_start_blk; /* Examine blocks of the indirect run one at a time */ for (i = 0; i < indirect.len; i++) { indirblock = befs_bread(sb, indirblockno + i); if (indirblock == NULL) { befs_debug(sb, "---> befs_find_brun_indirect() failed to " "read disk block %lu from the indirect brun", indirblockno + i); return BEFS_ERR; } array = (befs_disk_block_run *) indirblock->b_data; for (j = 0; j < arraylen; ++j) { int len = fs16_to_cpu(sb, array[j].len); if (search_blk >= sum && search_blk < sum + len) { int offset = search_blk - sum; run->allocation_group = fs32_to_cpu(sb, array[j].allocation_group); run->start = fs16_to_cpu(sb, array[j].start) + offset; run->len = fs16_to_cpu(sb, array[j].len) - offset; brelse(indirblock); befs_debug(sb, "<--- befs_find_brun_indirect() found " "file block %lu at indirect[%d]", blockno, j + (i * arraylen)); return BEFS_OK; } sum += len; } brelse(indirblock); } /* Only fallthrough is an error */ befs_error(sb, "BeFS: befs_find_brun_indirect() failed to find " "file block %lu", blockno); befs_debug(sb, "<--- befs_find_brun_indirect() ERROR"); return BEFS_ERR; }