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-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * inode.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (c) 1999 Al Smith
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Portions derived from work (c) 1995,1996 Christian Vogelgsang,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *              and from work (c) 1998 Mike Shaver.
^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/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include "efs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/efs_fs_sb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) static int efs_readpage(struct file *file, struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 	return block_read_full_page(page,efs_get_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) static sector_t _efs_bmap(struct address_space *mapping, sector_t block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	return generic_block_bmap(mapping,block,efs_get_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) static const struct address_space_operations efs_aops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	.readpage = efs_readpage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	.bmap = _efs_bmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) static inline void extent_copy(efs_extent *src, efs_extent *dst) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	 * this is slightly evil. it doesn't just copy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	 * efs_extent from src to dst, it also mangles
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	 * the bits so that dst ends up in cpu byte-order.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	dst->cooked.ex_magic  =  (unsigned int) src->raw[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	dst->cooked.ex_bn     = ((unsigned int) src->raw[1] << 16) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 				((unsigned int) src->raw[2] <<  8) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 				((unsigned int) src->raw[3] <<  0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	dst->cooked.ex_length =  (unsigned int) src->raw[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	dst->cooked.ex_offset = ((unsigned int) src->raw[5] << 16) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 				((unsigned int) src->raw[6] <<  8) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 				((unsigned int) src->raw[7] <<  0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	return;
^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) struct inode *efs_iget(struct super_block *super, unsigned long ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	int i, inode_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	dev_t device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	u32 rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	struct efs_sb_info    *sb = SUPER_INFO(super);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	struct efs_inode_info *in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	efs_block_t block, offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	struct efs_dinode *efs_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	inode = iget_locked(super, ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	if (!inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	if (!(inode->i_state & I_NEW))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	in = INODE_INFO(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	** EFS layout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	** |   cylinder group    |   cylinder group    |   cylinder group ..etc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	** |inodes|data          |inodes|data          |inodes|data       ..etc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	** work out the inode block index, (considering initially that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	** inodes are stored as consecutive blocks). then work out the block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	** number of that inode given the above layout, and finally the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	** offset of the inode within that block.
^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) 	inode_index = inode->i_ino /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		(EFS_BLOCKSIZE / sizeof(struct efs_dinode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	block = sb->fs_start + sb->first_block + 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		(sb->group_size * (inode_index / sb->inode_blocks)) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		(inode_index % sb->inode_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	offset = (inode->i_ino %
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 			(EFS_BLOCKSIZE / sizeof(struct efs_dinode))) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		sizeof(struct efs_dinode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	bh = sb_bread(inode->i_sb, block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	if (!bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		pr_warn("%s() failed at block %d\n", __func__, block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		goto read_inode_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	efs_inode = (struct efs_dinode *) (bh->b_data + offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98)     
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	inode->i_mode  = be16_to_cpu(efs_inode->di_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	set_nlink(inode, be16_to_cpu(efs_inode->di_nlink));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	i_uid_write(inode, (uid_t)be16_to_cpu(efs_inode->di_uid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	i_gid_write(inode, (gid_t)be16_to_cpu(efs_inode->di_gid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	inode->i_size  = be32_to_cpu(efs_inode->di_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	inode->i_atime.tv_sec = be32_to_cpu(efs_inode->di_atime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	inode->i_mtime.tv_sec = be32_to_cpu(efs_inode->di_mtime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	inode->i_ctime.tv_sec = be32_to_cpu(efs_inode->di_ctime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	inode->i_atime.tv_nsec = inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	/* this is the number of blocks in the file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	if (inode->i_size == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		inode->i_blocks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		inode->i_blocks = ((inode->i_size - 1) >> EFS_BLOCKSIZE_BITS) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	rdev = be16_to_cpu(efs_inode->di_u.di_dev.odev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	if (rdev == 0xffff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		rdev = be32_to_cpu(efs_inode->di_u.di_dev.ndev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		if (sysv_major(rdev) > 0xfff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 			device = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 			device = MKDEV(sysv_major(rdev), sysv_minor(rdev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		device = old_decode_dev(rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	/* get the number of extents for this object */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	in->numextents = be16_to_cpu(efs_inode->di_numextents);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	in->lastextent = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	/* copy the extents contained within the inode to memory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	for(i = 0; i < EFS_DIRECTEXTENTS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		extent_copy(&(efs_inode->di_u.di_extents[i]), &(in->extents[i]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		if (i < in->numextents && in->extents[i].cooked.ex_magic != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 			pr_warn("extent %d has bad magic number in inode %lu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 				i, inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 			brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 			goto read_inode_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	pr_debug("efs_iget(): inode %lu, extents %d, mode %o\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		 inode->i_ino, in->numextents, inode->i_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	switch (inode->i_mode & S_IFMT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		case S_IFDIR: 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 			inode->i_op = &efs_dir_inode_operations; 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 			inode->i_fop = &efs_dir_operations; 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		case S_IFREG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 			inode->i_fop = &generic_ro_fops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 			inode->i_data.a_ops = &efs_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		case S_IFLNK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 			inode->i_op = &page_symlink_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 			inode_nohighmem(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 			inode->i_data.a_ops = &efs_symlink_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		case S_IFCHR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		case S_IFBLK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		case S_IFIFO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 			init_special_inode(inode, inode->i_mode, device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 			pr_warn("unsupported inode mode %o\n", inode->i_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 			goto read_inode_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	unlock_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)         
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) read_inode_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	pr_warn("failed to read inode %lu\n", inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	iget_failed(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	return ERR_PTR(-EIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) static inline efs_block_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) efs_extent_check(efs_extent *ptr, efs_block_t block, struct efs_sb_info *sb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	efs_block_t start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	efs_block_t length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	efs_block_t offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	 * given an extent and a logical block within a file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	 * can this block be found within this extent ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	start  = ptr->cooked.ex_bn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	length = ptr->cooked.ex_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	offset = ptr->cooked.ex_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	if ((block >= offset) && (block < offset+length)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		return(sb->fs_start + start + block - offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) efs_block_t efs_map_block(struct inode *inode, efs_block_t block) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	struct efs_sb_info    *sb = SUPER_INFO(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	struct efs_inode_info *in = INODE_INFO(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	struct buffer_head    *bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	int cur, last, first = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	int ibase, ioffset, dirext, direxts, indext, indexts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	efs_block_t iblock, result = 0, lastblock = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	efs_extent ext, *exts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	last = in->lastextent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	if (in->numextents <= EFS_DIRECTEXTENTS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		/* first check the last extent we returned */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		if ((result = efs_extent_check(&in->extents[last], block, sb)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 			return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)     
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		/* if we only have one extent then nothing can be found */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		if (in->numextents == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 			pr_err("%s() failed to map (1 extent)\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 			return 0;
^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) 		direxts = in->numextents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		 * check the stored extents in the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		 * start with next extent and check forwards
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		for(dirext = 1; dirext < direxts; dirext++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 			cur = (last + dirext) % in->numextents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 			if ((result = efs_extent_check(&in->extents[cur], block, sb))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 				in->lastextent = cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 				return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		pr_err("%s() failed to map block %u (dir)\n", __func__, block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		return 0;
^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) 	pr_debug("%s(): indirect search for logical block %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		 __func__, block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	direxts = in->extents[0].cooked.ex_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	indexts = in->numextents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	for(indext = 0; indext < indexts; indext++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		cur = (last + indext) % indexts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		 * work out which direct extent contains `cur'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		 * also compute ibase: i.e. the number of the first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		 * indirect extent contained within direct extent `cur'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		ibase = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		for(dirext = 0; cur < ibase && dirext < direxts; dirext++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 			ibase += in->extents[dirext].cooked.ex_length *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 				(EFS_BLOCKSIZE / sizeof(efs_extent));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		if (dirext == direxts) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 			/* should never happen */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 			pr_err("couldn't find direct extent for indirect extent %d (block %u)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 			       cur, block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 			if (bh) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 			return 0;
^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) 		/* work out block number and offset of this indirect extent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		iblock = sb->fs_start + in->extents[dirext].cooked.ex_bn +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 			(cur - ibase) /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 			(EFS_BLOCKSIZE / sizeof(efs_extent));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		ioffset = (cur - ibase) %
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 			(EFS_BLOCKSIZE / sizeof(efs_extent));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		if (first || lastblock != iblock) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 			if (bh) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 			bh = sb_bread(inode->i_sb, iblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 			if (!bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 				pr_err("%s() failed at block %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 				       __func__, iblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 				return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 			pr_debug("%s(): read indirect extent block %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 				 __func__, iblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 			first = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 			lastblock = iblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		exts = (efs_extent *) bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		extent_copy(&(exts[ioffset]), &ext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		if (ext.cooked.ex_magic != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 			pr_err("extent %d has bad magic number in block %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 			       cur, iblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 			if (bh) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		if ((result = efs_extent_check(&ext, block, sb))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 			if (bh) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 			in->lastextent = cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 			return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	if (bh) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	pr_err("%s() failed to map block %u (indir)\n", __func__, block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) }  
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) MODULE_IMPORT_NS(ANDROID_GKI_VFS_EXPORT_ONLY);