// SPDX-License-Identifier: GPL-2.0 /* * linux/fs/befs/datastream.c * * Copyright (C) 2001 Will Dyson <will_dyson@pobox.com> * * Based on portions of file.c by Makoto Kato <m_kato@ga2.so-net.ne.jp> * * Many thanks to Dominic Giampaolo, author of "Practical File System * Design with the Be File System", for such a helpful book. *
*/
/** * befs_read_datastream - get buffer_head containing data, starting from pos. * @sb: Filesystem superblock * @ds: datastream to find data with * @pos: start of data * @off: offset of data in buffer_head->b_data * * Returns pointer to buffer_head containing data starting with offset @off, * if you don't need to know offset just set @off = NULL.
*/ struct buffer_head *
befs_read_datastream(struct super_block *sb, const befs_data_stream *ds,
befs_off_t pos, uint *off)
{ struct buffer_head *bh;
befs_block_run run;
befs_blocknr_t block; /* block coresponding to pos */
if (befs_fblock2brun(sb, ds, block, &run) != BEFS_OK) {
befs_error(sb, "BeFS: Error finding disk addr of block %lu",
(unsignedlong)block);
befs_debug(sb, "<--- %s ERROR", __func__); return NULL;
}
bh = befs_bread_iaddr(sb, run); if (!bh) {
befs_error(sb, "BeFS: Error reading block %lu from datastream",
(unsignedlong)block); return NULL;
}
befs_debug(sb, "<--- %s read data, starting at %llu", __func__, pos);
return bh;
}
/** * befs_fblock2brun - give back block run for fblock * @sb: the superblock * @data: datastream to read from * @fblock: the blocknumber with the file position to find * @run: The found run is passed back through this pointer * * Takes a file position and gives back a brun who's starting block * is block number fblock of the file. * * Returns BEFS_OK or BEFS_ERR. * * Calls specialized functions for each of the three possible * datastream regions.
*/ int
befs_fblock2brun(struct super_block *sb, const befs_data_stream *data,
befs_blocknr_t fblock, befs_block_run *run)
{ int err;
befs_off_t pos = fblock << BEFS_SB(sb)->block_shift;
} else {
befs_error(sb, "befs_fblock2brun() was asked to find block %lu, " "which is not mapped by the datastream\n",
(unsignedlong)fblock);
err = BEFS_ERR;
} return err;
}
/** * befs_read_lsmylink - read long symlink from datastream. * @sb: Filesystem superblock * @ds: Datastream to read from * @buff: Buffer in which to place long symlink data * @len: Length of the long symlink in bytes * * Returns the number of bytes read
*/
size_t
befs_read_lsymlink(struct super_block *sb, const befs_data_stream *ds, void *buff, befs_off_t len)
{
befs_off_t bytes_read = 0; /* bytes readed */
u16 plen; struct buffer_head *bh;
/** * befs_count_blocks - blocks used by a file * @sb: Filesystem superblock * @ds: Datastream of the file * * Counts the number of fs blocks that the file represented by * inode occupies on the filesystem, counting both regular file * data and filesystem metadata (and eventually attribute data * when we support attributes)
*/
metablocks = 1; /* Start with 1 block for inode */
/* Size of indirect block */ if (ds->size > ds->max_direct_range)
metablocks += ds->indirect.len;
/* * Double indir block, plus all the indirect blocks it maps. * In the double-indirect range, all block runs of data are * BEFS_DBLINDIR_BRUN_LEN blocks long. Therefore, we know * how many data block runs are in the double-indirect region, * and from that we know how many indirect blocks it takes to * map them. We assume that the indirect blocks are also * BEFS_DBLINDIR_BRUN_LEN blocks long.
*/ if (ds->size > ds->max_indirect_range && ds->max_indirect_range != 0) {
uint dbl_bytes;
uint dbl_bruns;
uint indirblocks;
/** * befs_find_brun_direct - find a direct block run in the datastream * @sb: the superblock * @data: the datastream * @blockno: the blocknumber to find * @run: The found run is passed back through this pointer * * 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 direct region of the datastream. * * Return value is BEFS_OK if the blockrun is found, BEFS_ERR * otherwise. * * Algorithm: * Linear search. Checks each element of array[] to see if it * contains the blockno-th filesystem block. This is necessary * because the block runs map variable amounts of data. Simply * keeps a count of the number of blocks searched so far (sum), * incrementing this by the length of each block run as we come * across it. Adds sum to *count before returning (this is so * you can search multiple arrays that are logicaly one array, * as in the indirect region code). * * When/if blockno is found, if blockno is inside of a block * run as stored on disk, we offset the start and length members * of the block run, so that blockno is the start and len is * still valid (the run ends in the same place).
*/ staticint
befs_find_brun_direct(struct super_block *sb, const befs_data_stream *data,
befs_blocknr_t blockno, befs_block_run *run)
{ int i; const befs_block_run *array = data->direct;
befs_blocknr_t sum;
for (i = 0, sum = 0; i < BEFS_NUM_DIRECT_BLOCKS;
sum += array[i].len, i++) { if (blockno >= sum && blockno < sum + (array[i].len)) { int offset = blockno - sum;
/** * befs_find_brun_indirect - find a block run in the datastream * @sb: the superblock * @data: the datastream * @blockno: the blocknumber to find * @run: The found run is passed back through this pointer * * 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. * * 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.
*/ staticint
befs_find_brun_indirect(struct super_block *sb, const 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;
/* Examine blocks of the indirect run one at a time */ for (i = 0; i < indirect.len; i++) {
indirblock = sb_bread(sb, indirblockno + i); if (indirblock == NULL) {
befs_error(sb, "---> %s failed to read " "disk block %lu from the indirect brun",
__func__, (unsignedlong)indirblockno + i);
befs_debug(sb, "<--- %s ERROR", __func__); return BEFS_ERR;
}
/** * befs_find_brun_dblindirect - find a block run in the datastream * @sb: the superblock * @data: the datastream * @blockno: the blocknumber to find * @run: The found run is passed back through this pointer * * 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. * * 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.
*/ staticint
befs_find_brun_dblindirect(struct super_block *sb, const 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;
/* 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;
/* 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.
*/
/* 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 " "%s, %d, is outside the range " "of the double-indirect block", __func__,
dblindir_indx); return BEFS_ERR;
}
/* 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 " "%s, %d, is outside the range " "of the indirect block", __func__, indir_indx); return BEFS_ERR;
}
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.