Orange Pi5 kernel

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

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * linux/fs/befs/datastream.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2001 Will Dyson <will_dyson@pobox.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Based on portions of file.c by Makoto Kato <m_kato@ga2.so-net.ne.jp>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * Many thanks to Dominic Giampaolo, author of "Practical File System
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * Design with the Be File System", for such a helpful book.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/buffer_head.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include "befs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include "datastream.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include "io.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) const befs_inode_addr BAD_IADDR = { 0, 0, 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) static int befs_find_brun_direct(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 				 const befs_data_stream *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 				 befs_blocknr_t blockno, befs_block_run *run);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) static int befs_find_brun_indirect(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 				   const befs_data_stream *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 				   befs_blocknr_t blockno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 				   befs_block_run *run);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) static int befs_find_brun_dblindirect(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 				      const befs_data_stream *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 				      befs_blocknr_t blockno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 				      befs_block_run *run);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  * befs_read_datastream - get buffer_head containing data, starting from pos.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  * @sb: Filesystem superblock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  * @ds: datastream to find data with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  * @pos: start of data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  * @off: offset of data in buffer_head->b_data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  * Returns pointer to buffer_head containing data starting with offset @off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  * if you don't need to know offset just set @off = NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) struct buffer_head *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) befs_read_datastream(struct super_block *sb, const befs_data_stream *ds,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		     befs_off_t pos, uint *off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	befs_block_run run;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	befs_blocknr_t block;	/* block coresponding to pos */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	befs_debug(sb, "---> %s %llu", __func__, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	block = pos >> BEFS_SB(sb)->block_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	if (off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		*off = pos - (block << BEFS_SB(sb)->block_shift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	if (befs_fblock2brun(sb, ds, block, &run) != BEFS_OK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		befs_error(sb, "BeFS: Error finding disk addr of block %lu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 			   (unsigned long)block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		befs_debug(sb, "<--- %s ERROR", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	bh = befs_bread_iaddr(sb, run);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	if (!bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		befs_error(sb, "BeFS: Error reading block %lu from datastream",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 			   (unsigned long)block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	befs_debug(sb, "<--- %s read data, starting at %llu", __func__, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	return bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80)  * befs_fblock2brun - give back block run for fblock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81)  * @sb: the superblock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82)  * @data: datastream to read from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83)  * @fblock: the blocknumber with the file position to find
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84)  * @run: The found run is passed back through this pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86)  * Takes a file position and gives back a brun who's starting block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87)  * is block number fblock of the file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89)  * Returns BEFS_OK or BEFS_ERR.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91)  * Calls specialized functions for each of the three possible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92)  * datastream regions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) befs_fblock2brun(struct super_block *sb, const befs_data_stream *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		 befs_blocknr_t fblock, befs_block_run *run)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	befs_off_t pos = fblock << BEFS_SB(sb)->block_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	if (pos < data->max_direct_range) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		err = befs_find_brun_direct(sb, data, fblock, run);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	} else if (pos < data->max_indirect_range) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		err = befs_find_brun_indirect(sb, data, fblock, run);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	} else if (pos < data->max_double_indirect_range) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		err = befs_find_brun_dblindirect(sb, data, fblock, run);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		befs_error(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 			   "befs_fblock2brun() was asked to find block %lu, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 			   "which is not mapped by the datastream\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 			   (unsigned long)fblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		err = BEFS_ERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)  * befs_read_lsmylink - read long symlink from datastream.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)  * @sb: Filesystem superblock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)  * @ds: Datastream to read from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)  * @buff: Buffer in which to place long symlink data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)  * @len: Length of the long symlink in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)  * Returns the number of bytes read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) size_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) befs_read_lsymlink(struct super_block *sb, const befs_data_stream *ds,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		   void *buff, befs_off_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	befs_off_t bytes_read = 0;	/* bytes readed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	u16 plen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	befs_debug(sb, "---> %s length: %llu", __func__, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	while (bytes_read < len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		bh = befs_read_datastream(sb, ds, bytes_read, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		if (!bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 			befs_error(sb, "BeFS: Error reading datastream block "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 				   "starting from %llu", bytes_read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 			befs_debug(sb, "<--- %s ERROR", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 			return bytes_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		plen = ((bytes_read + BEFS_SB(sb)->block_size) < len) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		    BEFS_SB(sb)->block_size : len - bytes_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		memcpy(buff + bytes_read, bh->b_data, plen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		bytes_read += plen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	befs_debug(sb, "<--- %s read %u bytes", __func__, (unsigned int)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		   bytes_read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	return bytes_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^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)  * befs_count_blocks - blocks used by a file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)  * @sb: Filesystem superblock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)  * @ds: Datastream of the file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)  * Counts the number of fs blocks that the file represented by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)  * inode occupies on the filesystem, counting both regular file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)  * data and filesystem metadata (and eventually attribute data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)  * when we support attributes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) befs_blocknr_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) befs_count_blocks(struct super_block *sb, const befs_data_stream *ds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	befs_blocknr_t blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	befs_blocknr_t datablocks;	/* File data blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	befs_blocknr_t metablocks;	/* FS metadata blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	struct befs_sb_info *befs_sb = BEFS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	befs_debug(sb, "---> %s", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	datablocks = ds->size >> befs_sb->block_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	if (ds->size & (befs_sb->block_size - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		datablocks += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	metablocks = 1;		/* Start with 1 block for inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	/* Size of indirect block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	if (ds->size > ds->max_direct_range)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		metablocks += ds->indirect.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	 * Double indir block, plus all the indirect blocks it maps.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	 * In the double-indirect range, all block runs of data are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	 * BEFS_DBLINDIR_BRUN_LEN blocks long. Therefore, we know
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	 * how many data block runs are in the double-indirect region,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	 * and from that we know how many indirect blocks it takes to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	 * map them. We assume that the indirect blocks are also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	 * BEFS_DBLINDIR_BRUN_LEN blocks long.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	if (ds->size > ds->max_indirect_range && ds->max_indirect_range != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		uint dbl_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		uint dbl_bruns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		uint indirblocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		dbl_bytes =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		    ds->max_double_indirect_range - ds->max_indirect_range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		dbl_bruns =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		    dbl_bytes / (befs_sb->block_size * BEFS_DBLINDIR_BRUN_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		indirblocks = dbl_bruns / befs_iaddrs_per_block(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		metablocks += ds->double_indirect.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		metablocks += indirblocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	blocks = datablocks + metablocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	befs_debug(sb, "<--- %s %u blocks", __func__, (unsigned int)blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	return blocks;
^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)  * befs_find_brun_direct - find a direct block run in the datastream
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)  * @sb: the superblock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)  * @data: the datastream
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)  * @blockno: the blocknumber to find
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)  * @run: The found run is passed back through this pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)  * Finds the block run that starts at file block number blockno
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)  * in the file represented by the datastream data, if that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)  * blockno is in the direct region of the datastream.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)  * Return value is BEFS_OK if the blockrun is found, BEFS_ERR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)  * otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)  * Algorithm:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)  * Linear search. Checks each element of array[] to see if it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)  * contains the blockno-th filesystem block. This is necessary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)  * because the block runs map variable amounts of data. Simply
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)  * keeps a count of the number of blocks searched so far (sum),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)  * incrementing this by the length of each block run as we come
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)  * across it. Adds sum to *count before returning (this is so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)  * you can search multiple arrays that are logicaly one array,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)  * as in the indirect region code).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)  * When/if blockno is found, if blockno is inside of a block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)  * run as stored on disk, we offset the start and length members
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)  * of the block run, so that blockno is the start and len is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)  * still valid (the run ends in the same place).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) befs_find_brun_direct(struct super_block *sb, const befs_data_stream *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		      befs_blocknr_t blockno, befs_block_run *run)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	const befs_block_run *array = data->direct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	befs_blocknr_t sum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	befs_debug(sb, "---> %s, find %lu", __func__, (unsigned long)blockno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	for (i = 0, sum = 0; i < BEFS_NUM_DIRECT_BLOCKS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	     sum += array[i].len, i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		if (blockno >= sum && blockno < sum + (array[i].len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 			int offset = blockno - sum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 			run->allocation_group = array[i].allocation_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 			run->start = array[i].start + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 			run->len = array[i].len - offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 			befs_debug(sb, "---> %s, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 				   "found %lu at direct[%d]", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 				   (unsigned long)blockno, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 			return BEFS_OK;
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	befs_error(sb, "%s failed to find file block %lu", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		   (unsigned long)blockno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	befs_debug(sb, "---> %s ERROR", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	return BEFS_ERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)  * befs_find_brun_indirect - find a block run in the datastream
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)  * @sb: the superblock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)  * @data: the datastream
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)  * @blockno: the blocknumber to find
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)  * @run: The found run is passed back through this pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)  * Finds the block run that starts at file block number blockno
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)  * in the file represented by the datastream data, if that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)  * blockno is in the indirect region of the datastream.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)  * Return value is BEFS_OK if the blockrun is found, BEFS_ERR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)  * otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)  * Algorithm:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)  * For each block in the indirect run of the datastream, read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)  * it in and search through it for search_blk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)  * XXX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)  * Really should check to make sure blockno is inside indirect
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)  * region.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) befs_find_brun_indirect(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 			const befs_data_stream *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 			befs_blocknr_t blockno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 			befs_block_run *run)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	int i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	befs_blocknr_t sum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	befs_blocknr_t indir_start_blk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	befs_blocknr_t search_blk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	struct buffer_head *indirblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	befs_disk_block_run *array;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	befs_block_run indirect = data->indirect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	befs_blocknr_t indirblockno = iaddr2blockno(sb, &indirect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	int arraylen = befs_iaddrs_per_block(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	befs_debug(sb, "---> %s, find %lu", __func__, (unsigned long)blockno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	indir_start_blk = data->max_direct_range >> BEFS_SB(sb)->block_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	search_blk = blockno - indir_start_blk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	/* Examine blocks of the indirect run one at a time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	for (i = 0; i < indirect.len; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		indirblock = sb_bread(sb, indirblockno + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		if (indirblock == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 			befs_error(sb, "---> %s failed to read "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 				   "disk block %lu from the indirect brun",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 				   __func__, (unsigned long)indirblockno + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 			befs_debug(sb, "<--- %s ERROR", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 			return BEFS_ERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		array = (befs_disk_block_run *) indirblock->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		for (j = 0; j < arraylen; ++j) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 			int len = fs16_to_cpu(sb, array[j].len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 			if (search_blk >= sum && search_blk < sum + len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 				int offset = search_blk - sum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 				run->allocation_group =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 				    fs32_to_cpu(sb, array[j].allocation_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 				run->start =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 				    fs16_to_cpu(sb, array[j].start) + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 				run->len =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 				    fs16_to_cpu(sb, array[j].len) - offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 				brelse(indirblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 				befs_debug(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 					   "<--- %s found file block "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 					   "%lu at indirect[%d]", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 					   (unsigned long)blockno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 					   j + (i * arraylen));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 				return BEFS_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 			sum += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		brelse(indirblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	/* Only fallthrough is an error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	befs_error(sb, "BeFS: %s failed to find "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		   "file block %lu", __func__, (unsigned long)blockno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	befs_debug(sb, "<--- %s ERROR", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	return BEFS_ERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)  * befs_find_brun_dblindirect - find a block run in the datastream
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)  * @sb: the superblock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)  * @data: the datastream
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)  * @blockno: the blocknumber to find
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)  * @run: The found run is passed back through this pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)  * Finds the block run that starts at file block number blockno
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)  * in the file represented by the datastream data, if that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)  * blockno is in the double-indirect region of the datastream.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)  * Return value is BEFS_OK if the blockrun is found, BEFS_ERR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)  * otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)  * Algorithm:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)  * The block runs in the double-indirect region are different.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)  * They are always allocated 4 fs blocks at a time, so each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)  * block run maps a constant amount of file data. This means
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)  * that we can directly calculate how many block runs into the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)  * double-indirect region we need to go to get to the one that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)  * maps a particular filesystem block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)  * We do this in two stages. First we calculate which of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)  * inode addresses in the double-indirect block will point us
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)  * to the indirect block that contains the mapping for the data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)  * then we calculate which of the inode addresses in that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)  * indirect block maps the data block we are after.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)  * Oh, and once we've done that, we actually read in the blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)  * that contain the inode addresses we calculated above. Even
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)  * though the double-indirect run may be several blocks long,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)  * we can calculate which of those blocks will contain the index
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)  * we are after and only read that one. We then follow it to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)  * the indirect block and perform a similar process to find
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)  * the actual block run that maps the data block we are interested
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)  * in.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)  * Then we offset the run as in befs_find_brun_array() and we are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)  * done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) befs_find_brun_dblindirect(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 			   const befs_data_stream *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 			   befs_blocknr_t blockno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 			   befs_block_run *run)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	int dblindir_indx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	int indir_indx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	int offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	int dbl_which_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	int which_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	int dbl_block_indx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	int block_indx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	off_t dblindir_leftover;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	befs_blocknr_t blockno_at_run_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	struct buffer_head *dbl_indir_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	struct buffer_head *indir_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	befs_block_run indir_run;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	befs_disk_inode_addr *iaddr_array;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	befs_blocknr_t indir_start_blk =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	    data->max_indirect_range >> BEFS_SB(sb)->block_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	off_t dbl_indir_off = blockno - indir_start_blk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	/* number of data blocks mapped by each of the iaddrs in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	 * the indirect block pointed to by the double indirect block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	size_t iblklen = BEFS_DBLINDIR_BRUN_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	/* number of data blocks mapped by each of the iaddrs in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	 * the double indirect block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	size_t diblklen = iblklen * befs_iaddrs_per_block(sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	    * BEFS_DBLINDIR_BRUN_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	befs_debug(sb, "---> %s find %lu", __func__, (unsigned long)blockno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	/* First, discover which of the double_indir->indir blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	 * contains pos. Then figure out how much of pos that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	 * accounted for. Then discover which of the iaddrs in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	 * the indirect block contains pos.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	dblindir_indx = dbl_indir_off / diblklen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	dblindir_leftover = dbl_indir_off % diblklen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	indir_indx = dblindir_leftover / diblklen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	/* Read double indirect block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	dbl_which_block = dblindir_indx / befs_iaddrs_per_block(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	if (dbl_which_block > data->double_indirect.len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 		befs_error(sb, "The double-indirect index calculated by "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 			   "%s, %d, is outside the range "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 			   "of the double-indirect block", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 			   dblindir_indx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 		return BEFS_ERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	dbl_indir_block =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	    sb_bread(sb, iaddr2blockno(sb, &data->double_indirect) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 					dbl_which_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	if (dbl_indir_block == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		befs_error(sb, "%s couldn't read the "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 			   "double-indirect block at blockno %lu", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 			   (unsigned long)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 			   iaddr2blockno(sb, &data->double_indirect) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 			   dbl_which_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		return BEFS_ERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	dbl_block_indx =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	    dblindir_indx - (dbl_which_block * befs_iaddrs_per_block(sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	iaddr_array = (befs_disk_inode_addr *) dbl_indir_block->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	indir_run = fsrun_to_cpu(sb, iaddr_array[dbl_block_indx]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	brelse(dbl_indir_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	/* Read indirect block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	which_block = indir_indx / befs_iaddrs_per_block(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	if (which_block > indir_run.len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 		befs_error(sb, "The indirect index calculated by "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 			   "%s, %d, is outside the range "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 			   "of the indirect block", __func__, indir_indx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 		return BEFS_ERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	indir_block =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	    sb_bread(sb, iaddr2blockno(sb, &indir_run) + which_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	if (indir_block == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 		befs_error(sb, "%s couldn't read the indirect block "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 			   "at blockno %lu", __func__, (unsigned long)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 			   iaddr2blockno(sb, &indir_run) + which_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 		return BEFS_ERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	block_indx = indir_indx - (which_block * befs_iaddrs_per_block(sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	iaddr_array = (befs_disk_inode_addr *) indir_block->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	*run = fsrun_to_cpu(sb, iaddr_array[block_indx]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	brelse(indir_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	blockno_at_run_start = indir_start_blk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	blockno_at_run_start += diblklen * dblindir_indx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	blockno_at_run_start += iblklen * indir_indx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	offset = blockno - blockno_at_run_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	run->start += offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	run->len -= offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	befs_debug(sb, "Found file block %lu in double_indirect[%d][%d],"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 		   " double_indirect_leftover = %lu", (unsigned long)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 		   blockno, dblindir_indx, indir_indx, dblindir_leftover);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	return BEFS_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) }