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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2)  * Compressed rom filesystem for Linux.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  * Copyright (C) 1999 Linus Torvalds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  * This file is released under the GPL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  */
^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)  * These are the VFS interfaces to the compressed rom filesystem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  * The actual compression is based on zlib, see the other files.
^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) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <linux/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <linux/pfn_t.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include <linux/ramfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include <linux/mtd/mtd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include <linux/mtd/super.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #include <linux/fs_context.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include <linux/vfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include <uapi/linux/cramfs_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #include "internal.h"
^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)  * cramfs super-block data in memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) struct cramfs_sb_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) 	unsigned long magic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 	unsigned long size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 	unsigned long blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 	unsigned long files;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 	void *linear_virt_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) 	resource_size_t linear_phys_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 	size_t mtd_point_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) static inline struct cramfs_sb_info *CRAMFS_SB(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 	return sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) static const struct super_operations cramfs_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) static const struct inode_operations cramfs_dir_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) static const struct file_operations cramfs_directory_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) static const struct file_operations cramfs_physmem_fops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) static const struct address_space_operations cramfs_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) static DEFINE_MUTEX(read_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) /* These macros may change in future, to provide better st_ino semantics. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) #define OFFSET(x)	((x)->i_ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) static unsigned long cramino(const struct cramfs_inode *cino, unsigned int offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 	if (!cino->offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 		return offset + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 	if (!cino->size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 		return offset + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 	 * The file mode test fixes buggy mkcramfs implementations where
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 	 * cramfs_inode->offset is set to a non zero value for entries
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 	 * which did not contain data, like devices node and fifos.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 	switch (cino->mode & S_IFMT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 	case S_IFREG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 	case S_IFDIR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 	case S_IFLNK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 		return cino->offset << 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	return offset + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) static struct inode *get_cramfs_inode(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 	const struct cramfs_inode *cramfs_inode, unsigned int offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 	static struct timespec64 zerotime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	inode = iget_locked(sb, cramino(cramfs_inode, offset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 	if (!inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 	if (!(inode->i_state & I_NEW))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 		return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 	switch (cramfs_inode->mode & S_IFMT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 	case S_IFREG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 		inode->i_fop = &generic_ro_fops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 		inode->i_data.a_ops = &cramfs_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 		if (IS_ENABLED(CONFIG_CRAMFS_MTD) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 		    CRAMFS_SB(sb)->flags & CRAMFS_FLAG_EXT_BLOCK_POINTERS &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 		    CRAMFS_SB(sb)->linear_phys_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 			inode->i_fop = &cramfs_physmem_fops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	case S_IFDIR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 		inode->i_op = &cramfs_dir_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 		inode->i_fop = &cramfs_directory_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 	case S_IFLNK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 		inode->i_op = &page_symlink_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 		inode_nohighmem(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 		inode->i_data.a_ops = &cramfs_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 		init_special_inode(inode, cramfs_inode->mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 				old_decode_dev(cramfs_inode->size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	inode->i_mode = cramfs_inode->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	i_uid_write(inode, cramfs_inode->uid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 	i_gid_write(inode, cramfs_inode->gid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 	/* if the lower 2 bits are zero, the inode contains data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 	if (!(inode->i_ino & 3)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 		inode->i_size = cramfs_inode->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 		inode->i_blocks = (cramfs_inode->size - 1) / 512 + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	/* Struct copy intentional */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	inode->i_mtime = inode->i_atime = inode->i_ctime = zerotime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	/* inode->i_nlink is left 1 - arguably wrong for directories,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	   but it's the best we can do without reading the directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	   contents.  1 yields the right result in GNU find, even
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	   without -noleaf option. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	unlock_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) }
^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)  * We have our own block cache: don't fill up the buffer cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149)  * with the rom-image, because the way the filesystem is set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150)  * up the accesses should be fairly regular and cached in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151)  * page cache and dentry tree anyway..
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153)  * This also acts as a way to guarantee contiguous areas of up to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154)  * BLKS_PER_BUF*PAGE_SIZE, so that the caller doesn't need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155)  * worry about end-of-buffer issues even when decompressing a full
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156)  * page cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158)  * Note: This is all optimized away at compile time when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159)  *       CONFIG_CRAMFS_BLOCKDEV=n.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) #define READ_BUFFERS (2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) /* NEXT_BUFFER(): Loop over [0..(READ_BUFFERS-1)]. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) #define NEXT_BUFFER(_ix) ((_ix) ^ 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166)  * BLKS_PER_BUF_SHIFT should be at least 2 to allow for "compressed"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167)  * data that takes up more space than the original and with unlucky
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168)  * alignment.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) #define BLKS_PER_BUF_SHIFT	(2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) #define BLKS_PER_BUF		(1 << BLKS_PER_BUF_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) #define BUFFER_SIZE		(BLKS_PER_BUF*PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) static unsigned char read_buffers[READ_BUFFERS][BUFFER_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) static unsigned buffer_blocknr[READ_BUFFERS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) static struct super_block *buffer_dev[READ_BUFFERS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) static int next_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180)  * Populate our block cache and return a pointer to it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) static void *cramfs_blkdev_read(struct super_block *sb, unsigned int offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 				unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 	struct address_space *mapping = sb->s_bdev->bd_inode->i_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	struct page *pages[BLKS_PER_BUF];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 	unsigned i, blocknr, buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	unsigned long devsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	char *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	if (!len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 	blocknr = offset >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 	offset &= PAGE_SIZE - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 	/* Check if an existing buffer already has the data.. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	for (i = 0; i < READ_BUFFERS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 		unsigned int blk_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 		if (buffer_dev[i] != sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 		if (blocknr < buffer_blocknr[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 		blk_offset = (blocknr - buffer_blocknr[i]) << PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 		blk_offset += offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 		if (blk_offset > BUFFER_SIZE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 		    blk_offset + len > BUFFER_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 		return read_buffers[i] + blk_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 	devsize = mapping->host->i_size >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 	/* Ok, read in BLKS_PER_BUF pages completely first. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 	for (i = 0; i < BLKS_PER_BUF; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 		struct page *page = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 		if (blocknr + i < devsize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 			page = read_mapping_page(mapping, blocknr + i, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 			/* synchronous error? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 			if (IS_ERR(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 				page = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 		pages[i] = page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	for (i = 0; i < BLKS_PER_BUF; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 		struct page *page = pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 		if (page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 			wait_on_page_locked(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 			if (!PageUptodate(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 				/* asynchronous error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 				put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 				pages[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 		}
^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) 	buffer = next_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 	next_buffer = NEXT_BUFFER(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	buffer_blocknr[buffer] = blocknr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	buffer_dev[buffer] = sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 	data = read_buffers[buffer];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	for (i = 0; i < BLKS_PER_BUF; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 		struct page *page = pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 		if (page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 			memcpy(data, kmap(page), PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 			kunmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 			put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 			memset(data, 0, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 		data += PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 	return read_buffers[buffer] + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) }
^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)  * Return a pointer to the linearly addressed cramfs image in memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) static void *cramfs_direct_read(struct super_block *sb, unsigned int offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 				unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 	struct cramfs_sb_info *sbi = CRAMFS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	if (!len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 	if (len > sbi->size || offset > sbi->size - len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 		return page_address(ZERO_PAGE(0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	return sbi->linear_virt_addr + offset;
^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)  * Returns a pointer to a buffer containing at least LEN bytes of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277)  * filesystem starting at byte offset OFFSET into the filesystem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) static void *cramfs_read(struct super_block *sb, unsigned int offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 			 unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 	struct cramfs_sb_info *sbi = CRAMFS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	if (IS_ENABLED(CONFIG_CRAMFS_MTD) && sbi->linear_virt_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 		return cramfs_direct_read(sb, offset, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	else if (IS_ENABLED(CONFIG_CRAMFS_BLOCKDEV))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 		return cramfs_blkdev_read(sb, offset, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293)  * For a mapping to be possible, we need a range of uncompressed and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294)  * contiguous blocks. Return the offset for the first block and number of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295)  * valid blocks for which that is true, or zero otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) static u32 cramfs_get_block_range(struct inode *inode, u32 pgoff, u32 *pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	struct cramfs_sb_info *sbi = CRAMFS_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	u32 *blockptrs, first_block_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	 * We can dereference memory directly here as this code may be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	 * reached only when there is a direct filesystem image mapping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	 * available in memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 	blockptrs = (u32 *)(sbi->linear_virt_addr + OFFSET(inode) + pgoff * 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	first_block_addr = blockptrs[0] & ~CRAMFS_BLK_FLAGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 		u32 block_off = i * (PAGE_SIZE >> CRAMFS_BLK_DIRECT_PTR_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 		u32 expect = (first_block_addr + block_off) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 			     CRAMFS_BLK_FLAG_DIRECT_PTR |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 			     CRAMFS_BLK_FLAG_UNCOMPRESSED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 		if (blockptrs[i] != expect) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 			pr_debug("range: block %d/%d got %#x expects %#x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 				 pgoff+i, pgoff + *pages - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 				 blockptrs[i], expect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 			if (i == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 				return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 	} while (++i < *pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	*pages = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	return first_block_addr << CRAMFS_BLK_DIRECT_PTR_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) #ifdef CONFIG_MMU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333)  * Return true if the last page of a file in the filesystem image contains
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334)  * some other data that doesn't belong to that file. It is assumed that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335)  * last block is CRAMFS_BLK_FLAG_DIRECT_PTR | CRAMFS_BLK_FLAG_UNCOMPRESSED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336)  * (verified by cramfs_get_block_range() and directly accessible in memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) static bool cramfs_last_page_is_shared(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	struct cramfs_sb_info *sbi = CRAMFS_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	u32 partial, last_page, blockaddr, *blockptrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 	char *tail_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 	partial = offset_in_page(inode->i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	if (!partial)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	last_page = inode->i_size >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 	blockptrs = (u32 *)(sbi->linear_virt_addr + OFFSET(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	blockaddr = blockptrs[last_page] & ~CRAMFS_BLK_FLAGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	blockaddr <<= CRAMFS_BLK_DIRECT_PTR_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	tail_data = sbi->linear_virt_addr + blockaddr + partial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 	return memchr_inv(tail_data, 0, PAGE_SIZE - partial) ? true : false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) static int cramfs_physmem_mmap(struct file *file, struct vm_area_struct *vma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 	struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 	struct cramfs_sb_info *sbi = CRAMFS_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 	unsigned int pages, max_pages, offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 	unsigned long address, pgoff = vma->vm_pgoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 	char *bailout_reason;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	ret = generic_file_readonly_mmap(file, vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 		return ret;
^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) 	 * Now try to pre-populate ptes for this vma with a direct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	 * mapping avoiding memory allocation when possible.
^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) 	/* Could COW work here? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 	bailout_reason = "vma is writable";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	if (vma->vm_flags & VM_WRITE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 		goto bailout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 	max_pages = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 	bailout_reason = "beyond file limit";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 	if (pgoff >= max_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 		goto bailout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	pages = min(vma_pages(vma), max_pages - pgoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	offset = cramfs_get_block_range(inode, pgoff, &pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 	bailout_reason = "unsuitable block layout";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	if (!offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 		goto bailout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	address = sbi->linear_phys_addr + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	bailout_reason = "data is not page aligned";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	if (!PAGE_ALIGNED(address))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 		goto bailout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	/* Don't map the last page if it contains some other data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 	if (pgoff + pages == max_pages && cramfs_last_page_is_shared(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 		pr_debug("mmap: %s: last page is shared\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 			 file_dentry(file)->d_name.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 		pages--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	if (!pages) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 		bailout_reason = "no suitable block remaining";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 		goto bailout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	if (pages == vma_pages(vma)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 		 * The entire vma is mappable. remap_pfn_range() will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 		 * make it distinguishable from a non-direct mapping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 		 * in /proc/<pid>/maps by substituting the file offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 		 * with the actual physical address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 		ret = remap_pfn_range(vma, vma->vm_start, address >> PAGE_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 				      pages * PAGE_SIZE, vma->vm_page_prot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 		 * Let's create a mixed map if we can't map it all.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 		 * The normal paging machinery will take care of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 		 * unpopulated ptes via cramfs_readpage().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 		int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 		vma->vm_flags |= VM_MIXEDMAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 		for (i = 0; i < pages && !ret; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 			vm_fault_t vmf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 			unsigned long off = i * PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 			pfn_t pfn = phys_to_pfn_t(address + off, PFN_DEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 			vmf = vmf_insert_mixed(vma, vma->vm_start + off, pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 			if (vmf & VM_FAULT_ERROR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 				ret = vm_fault_to_errno(vmf, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 		pr_debug("mapped %s[%lu] at 0x%08lx (%u/%lu pages) "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 			 "to vma 0x%08lx, page_prot 0x%llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 			 file_dentry(file)->d_name.name, pgoff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 			 address, pages, vma_pages(vma), vma->vm_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 			 (unsigned long long)pgprot_val(vma->vm_page_prot));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) bailout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	pr_debug("%s[%lu]: direct mmap impossible: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 		 file_dentry(file)->d_name.name, pgoff, bailout_reason);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	/* Didn't manage any direct map, but normal paging is still possible */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) #else /* CONFIG_MMU */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) static int cramfs_physmem_mmap(struct file *file, struct vm_area_struct *vma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) static unsigned long cramfs_physmem_get_unmapped_area(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 			unsigned long addr, unsigned long len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 			unsigned long pgoff, unsigned long flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 	struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	struct cramfs_sb_info *sbi = CRAMFS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	unsigned int pages, block_pages, max_pages, offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	pages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 	max_pages = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	if (pgoff >= max_pages || pages > max_pages - pgoff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	block_pages = pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	offset = cramfs_get_block_range(inode, pgoff, &block_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	if (!offset || block_pages != pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 		return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	addr = sbi->linear_phys_addr + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	pr_debug("get_unmapped for %s ofs %#lx siz %lu at 0x%08lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 		 file_dentry(file)->d_name.name, pgoff*PAGE_SIZE, len, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	return addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) static unsigned int cramfs_physmem_mmap_capabilities(struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 	return NOMMU_MAP_COPY | NOMMU_MAP_DIRECT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 	       NOMMU_MAP_READ | NOMMU_MAP_EXEC;
^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) #endif /* CONFIG_MMU */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) static const struct file_operations cramfs_physmem_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 	.llseek			= generic_file_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	.read_iter		= generic_file_read_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 	.splice_read		= generic_file_splice_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 	.mmap			= cramfs_physmem_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) #ifndef CONFIG_MMU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	.get_unmapped_area	= cramfs_physmem_get_unmapped_area,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 	.mmap_capabilities	= cramfs_physmem_mmap_capabilities,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) static void cramfs_kill_sb(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 	struct cramfs_sb_info *sbi = CRAMFS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	if (IS_ENABLED(CONFIG_CRAMFS_MTD) && sb->s_mtd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 		if (sbi && sbi->mtd_point_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 			mtd_unpoint(sb->s_mtd, 0, sbi->mtd_point_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 		kill_mtd_super(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	} else if (IS_ENABLED(CONFIG_CRAMFS_BLOCKDEV) && sb->s_bdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 		kill_block_super(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	kfree(sbi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) static int cramfs_reconfigure(struct fs_context *fc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	sync_filesystem(fc->root->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	fc->sb_flags |= SB_RDONLY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) static int cramfs_read_super(struct super_block *sb, struct fs_context *fc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 			     struct cramfs_super *super)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	struct cramfs_sb_info *sbi = CRAMFS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	unsigned long root_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	bool silent = fc->sb_flags & SB_SILENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	/* We don't know the real size yet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	sbi->size = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 	/* Read the first block and get the superblock from it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	mutex_lock(&read_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	memcpy(super, cramfs_read(sb, 0, sizeof(*super)), sizeof(*super));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	mutex_unlock(&read_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	/* Do sanity checks on the superblock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 	if (super->magic != CRAMFS_MAGIC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 		/* check for wrong endianness */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 		if (super->magic == CRAMFS_MAGIC_WEND) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 			if (!silent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 				errorfc(fc, "wrong endianness");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 		/* check at 512 byte offset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 		mutex_lock(&read_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 		memcpy(super,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 		       cramfs_read(sb, 512, sizeof(*super)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 		       sizeof(*super));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 		mutex_unlock(&read_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 		if (super->magic != CRAMFS_MAGIC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 			if (super->magic == CRAMFS_MAGIC_WEND && !silent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 				errorfc(fc, "wrong endianness");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 			else if (!silent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 				errorfc(fc, "wrong magic");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 	/* get feature flags first */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 	if (super->flags & ~CRAMFS_SUPPORTED_FLAGS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 		errorfc(fc, "unsupported filesystem features");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	/* Check that the root inode is in a sane state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	if (!S_ISDIR(super->root.mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 		errorfc(fc, "root is not a directory");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	/* correct strange, hard-coded permissions of mkcramfs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 	super->root.mode |= 0555;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	root_offset = super->root.offset << 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 	if (super->flags & CRAMFS_FLAG_FSID_VERSION_2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 		sbi->size = super->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 		sbi->blocks = super->fsid.blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 		sbi->files = super->fsid.files;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 		sbi->size = 1<<28;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 		sbi->blocks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 		sbi->files = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 	sbi->magic = super->magic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	sbi->flags = super->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	if (root_offset == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 		infofc(fc, "empty filesystem");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 	else if (!(super->flags & CRAMFS_FLAG_SHIFTED_ROOT_OFFSET) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 		 ((root_offset != sizeof(struct cramfs_super)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 		  (root_offset != 512 + sizeof(struct cramfs_super))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 		errorfc(fc, "bad root offset %lu", root_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) static int cramfs_finalize_super(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 				 struct cramfs_inode *cramfs_root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 	struct inode *root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 	/* Set it all up.. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 	sb->s_flags |= SB_RDONLY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	sb->s_time_min = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 	sb->s_time_max = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	sb->s_op = &cramfs_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	root = get_cramfs_inode(sb, cramfs_root, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 	if (IS_ERR(root))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 		return PTR_ERR(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	sb->s_root = d_make_root(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	if (!sb->s_root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) static int cramfs_blkdev_fill_super(struct super_block *sb, struct fs_context *fc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	struct cramfs_sb_info *sbi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	struct cramfs_super super;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	int i, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	sbi = kzalloc(sizeof(struct cramfs_sb_info), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	if (!sbi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	sb->s_fs_info = sbi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	/* Invalidate the read buffers on mount: think disk change.. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	for (i = 0; i < READ_BUFFERS; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 		buffer_blocknr[i] = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	err = cramfs_read_super(sb, fc, &super);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	return cramfs_finalize_super(sb, &super.root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) static int cramfs_mtd_fill_super(struct super_block *sb, struct fs_context *fc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	struct cramfs_sb_info *sbi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	struct cramfs_super super;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	sbi = kzalloc(sizeof(struct cramfs_sb_info), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 	if (!sbi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	sb->s_fs_info = sbi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	/* Map only one page for now.  Will remap it when fs size is known. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	err = mtd_point(sb->s_mtd, 0, PAGE_SIZE, &sbi->mtd_point_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 			&sbi->linear_virt_addr, &sbi->linear_phys_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 	if (err || sbi->mtd_point_size != PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 		pr_err("unable to get direct memory access to mtd:%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 		       sb->s_mtd->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 		return err ? : -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	pr_info("checking physical address %pap for linear cramfs image\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 		&sbi->linear_phys_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 	err = cramfs_read_super(sb, fc, &super);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	/* Remap the whole filesystem now */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	pr_info("linear cramfs image on mtd:%s appears to be %lu KB in size\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 		sb->s_mtd->name, sbi->size/1024);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	mtd_unpoint(sb->s_mtd, 0, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 	err = mtd_point(sb->s_mtd, 0, sbi->size, &sbi->mtd_point_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 			&sbi->linear_virt_addr, &sbi->linear_phys_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	if (err || sbi->mtd_point_size != sbi->size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 		pr_err("unable to get direct memory access to mtd:%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 		       sb->s_mtd->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 		return err ? : -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	return cramfs_finalize_super(sb, &super.root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) static int cramfs_statfs(struct dentry *dentry, struct kstatfs *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	struct super_block *sb = dentry->d_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 	u64 id = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	if (sb->s_bdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 		id = huge_encode_dev(sb->s_bdev->bd_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	else if (sb->s_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 		id = huge_encode_dev(sb->s_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 	buf->f_type = CRAMFS_MAGIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	buf->f_bsize = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 	buf->f_blocks = CRAMFS_SB(sb)->blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	buf->f_bfree = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	buf->f_bavail = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	buf->f_files = CRAMFS_SB(sb)->files;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	buf->f_ffree = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	buf->f_fsid = u64_to_fsid(id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	buf->f_namelen = CRAMFS_MAXPATHLEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699)  * Read a cramfs directory entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) static int cramfs_readdir(struct file *file, struct dir_context *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	unsigned int offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	/* Offset within the thing. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	if (ctx->pos >= inode->i_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 	offset = ctx->pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	/* Directory entries are always 4-byte aligned */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	if (offset & 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	buf = kmalloc(CRAMFS_MAXPATHLEN, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	while (offset < inode->i_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 		struct cramfs_inode *de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 		unsigned long nextoffset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 		char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 		ino_t ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 		umode_t mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 		int namelen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 		mutex_lock(&read_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 		de = cramfs_read(sb, OFFSET(inode) + offset, sizeof(*de)+CRAMFS_MAXPATHLEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 		name = (char *)(de+1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 		 * Namelengths on disk are shifted by two
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 		 * and the name padded out to 4-byte boundaries
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 		 * with zeroes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 		namelen = de->namelen << 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 		memcpy(buf, name, namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 		ino = cramino(de, OFFSET(inode) + offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 		mode = de->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 		mutex_unlock(&read_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 		nextoffset = offset + sizeof(*de) + namelen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 		for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 			if (!namelen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 				kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 				return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 			if (buf[namelen-1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 			namelen--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 		if (!dir_emit(ctx, buf, namelen, ino, mode >> 12))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 		ctx->pos = offset = nextoffset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762)  * Lookup and fill in the inode data..
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) static struct dentry *cramfs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 	unsigned int offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 	struct inode *inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 	int sorted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 	mutex_lock(&read_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	sorted = CRAMFS_SB(dir->i_sb)->flags & CRAMFS_FLAG_SORTED_DIRS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 	while (offset < dir->i_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 		struct cramfs_inode *de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 		char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 		int namelen, retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 		int dir_off = OFFSET(dir) + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 		de = cramfs_read(dir->i_sb, dir_off, sizeof(*de)+CRAMFS_MAXPATHLEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 		name = (char *)(de+1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 		/* Try to take advantage of sorted directories */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 		if (sorted && (dentry->d_name.name[0] < name[0]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 		namelen = de->namelen << 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 		offset += sizeof(*de) + namelen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 		/* Quick check that the name is roughly the right length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 		if (((dentry->d_name.len + 3) & ~3) != namelen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 		for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 			if (!namelen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 				inode = ERR_PTR(-EIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 			if (name[namelen-1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 			namelen--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 		if (namelen != dentry->d_name.len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 		retval = memcmp(dentry->d_name.name, name, namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 		if (retval > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 		if (!retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 			inode = get_cramfs_inode(dir->i_sb, de, dir_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 		/* else (retval < 0) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 		if (sorted)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	mutex_unlock(&read_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	return d_splice_alias(inode, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) static int cramfs_readpage(struct file *file, struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 	struct inode *inode = page->mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 	u32 maxblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	int bytes_filled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 	void *pgdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 	maxblock = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 	bytes_filled = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	pgdata = kmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	if (page->index < maxblock) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 		struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 		u32 blkptr_offset = OFFSET(inode) + page->index * 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 		u32 block_ptr, block_start, block_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 		bool uncompressed, direct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 		mutex_lock(&read_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 		block_ptr = *(u32 *) cramfs_read(sb, blkptr_offset, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 		uncompressed = (block_ptr & CRAMFS_BLK_FLAG_UNCOMPRESSED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 		direct = (block_ptr & CRAMFS_BLK_FLAG_DIRECT_PTR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 		block_ptr &= ~CRAMFS_BLK_FLAGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 		if (direct) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 			 * The block pointer is an absolute start pointer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 			 * shifted by 2 bits. The size is included in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 			 * first 2 bytes of the data block when compressed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 			 * or PAGE_SIZE otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 			block_start = block_ptr << CRAMFS_BLK_DIRECT_PTR_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 			if (uncompressed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 				block_len = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 				/* if last block: cap to file length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 				if (page->index == maxblock - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 					block_len =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 						offset_in_page(inode->i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 				block_len = *(u16 *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 					cramfs_read(sb, block_start, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 				block_start += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 			 * The block pointer indicates one past the end of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 			 * the current block (start of next block). If this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 			 * is the first block then it starts where the block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 			 * pointer table ends, otherwise its start comes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 			 * from the previous block's pointer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 			block_start = OFFSET(inode) + maxblock * 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 			if (page->index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 				block_start = *(u32 *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 					cramfs_read(sb, blkptr_offset - 4, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 			/* Beware... previous ptr might be a direct ptr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 			if (unlikely(block_start & CRAMFS_BLK_FLAG_DIRECT_PTR)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 				/* See comments on earlier code. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 				u32 prev_start = block_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 				block_start = prev_start & ~CRAMFS_BLK_FLAGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 				block_start <<= CRAMFS_BLK_DIRECT_PTR_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 				if (prev_start & CRAMFS_BLK_FLAG_UNCOMPRESSED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 					block_start += PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 				} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 					block_len = *(u16 *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 						cramfs_read(sb, block_start, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 					block_start += 2 + block_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 			block_start &= ~CRAMFS_BLK_FLAGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 			block_len = block_ptr - block_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 		if (block_len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 			; /* hole */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 		else if (unlikely(block_len > 2*PAGE_SIZE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 				  (uncompressed && block_len > PAGE_SIZE))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 			mutex_unlock(&read_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 			pr_err("bad data blocksize %u\n", block_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 		} else if (uncompressed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 			memcpy(pgdata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 			       cramfs_read(sb, block_start, block_len),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 			       block_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 			bytes_filled = block_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 			bytes_filled = cramfs_uncompress_block(pgdata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 				 PAGE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 				 cramfs_read(sb, block_start, block_len),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 				 block_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 		mutex_unlock(&read_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 		if (unlikely(bytes_filled < 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	memset(pgdata + bytes_filled, 0, PAGE_SIZE - bytes_filled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 	flush_dcache_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 	kunmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	SetPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 	unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	kunmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 	ClearPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	SetPageError(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) static const struct address_space_operations cramfs_aops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	.readpage = cramfs_readpage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934)  * Our operations:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938)  * A directory can only readdir
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) static const struct file_operations cramfs_directory_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	.llseek		= generic_file_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	.read		= generic_read_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 	.iterate_shared	= cramfs_readdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) static const struct inode_operations cramfs_dir_inode_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 	.lookup		= cramfs_lookup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) static const struct super_operations cramfs_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 	.statfs		= cramfs_statfs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) static int cramfs_get_tree(struct fs_context *fc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	int ret = -ENOPROTOOPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 	if (IS_ENABLED(CONFIG_CRAMFS_MTD)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 		ret = get_tree_mtd(fc, cramfs_mtd_fill_super);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 		if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 	if (IS_ENABLED(CONFIG_CRAMFS_BLOCKDEV))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 		ret = get_tree_bdev(fc, cramfs_blkdev_fill_super);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) static const struct fs_context_operations cramfs_context_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	.get_tree	= cramfs_get_tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	.reconfigure	= cramfs_reconfigure,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974)  * Set up the filesystem mount context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) static int cramfs_init_fs_context(struct fs_context *fc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	fc->ops = &cramfs_context_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) static struct file_system_type cramfs_fs_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	.owner		= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 	.name		= "cramfs",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	.init_fs_context = cramfs_init_fs_context,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	.kill_sb	= cramfs_kill_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	.fs_flags	= FS_REQUIRES_DEV,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) MODULE_ALIAS_FS("cramfs");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) static int __init init_cramfs_fs(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 	int rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 	rv = cramfs_uncompress_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 	if (rv < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 		return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	rv = register_filesystem(&cramfs_fs_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 	if (rv < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 		cramfs_uncompress_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) static void __exit exit_cramfs_fs(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	cramfs_uncompress_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	unregister_filesystem(&cramfs_fs_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) module_init(init_cramfs_fs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) module_exit(exit_cramfs_fs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) MODULE_IMPORT_NS(ANDROID_GKI_VFS_EXPORT_ONLY);