162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Squashfs - a compressed read only filesystem for Linux 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008 662306a36Sopenharmony_ci * Phillip Lougher <phillip@squashfs.org.uk> 762306a36Sopenharmony_ci * 862306a36Sopenharmony_ci * export.c 962306a36Sopenharmony_ci */ 1062306a36Sopenharmony_ci 1162306a36Sopenharmony_ci/* 1262306a36Sopenharmony_ci * This file implements code to make Squashfs filesystems exportable (NFS etc.) 1362306a36Sopenharmony_ci * 1462306a36Sopenharmony_ci * The export code uses an inode lookup table to map inode numbers passed in 1562306a36Sopenharmony_ci * filehandles to an inode location on disk. This table is stored compressed 1662306a36Sopenharmony_ci * into metadata blocks. A second index table is used to locate these. This 1762306a36Sopenharmony_ci * second index table for speed of access (and because it is small) is read at 1862306a36Sopenharmony_ci * mount time and cached in memory. 1962306a36Sopenharmony_ci * 2062306a36Sopenharmony_ci * The inode lookup table is used only by the export code, inode disk 2162306a36Sopenharmony_ci * locations are directly encoded in directories, enabling direct access 2262306a36Sopenharmony_ci * without an intermediate lookup for all operations except the export ops. 2362306a36Sopenharmony_ci */ 2462306a36Sopenharmony_ci 2562306a36Sopenharmony_ci#include <linux/fs.h> 2662306a36Sopenharmony_ci#include <linux/vfs.h> 2762306a36Sopenharmony_ci#include <linux/dcache.h> 2862306a36Sopenharmony_ci#include <linux/exportfs.h> 2962306a36Sopenharmony_ci#include <linux/slab.h> 3062306a36Sopenharmony_ci 3162306a36Sopenharmony_ci#include "squashfs_fs.h" 3262306a36Sopenharmony_ci#include "squashfs_fs_sb.h" 3362306a36Sopenharmony_ci#include "squashfs_fs_i.h" 3462306a36Sopenharmony_ci#include "squashfs.h" 3562306a36Sopenharmony_ci 3662306a36Sopenharmony_ci/* 3762306a36Sopenharmony_ci * Look-up inode number (ino) in table, returning the inode location. 3862306a36Sopenharmony_ci */ 3962306a36Sopenharmony_cistatic long long squashfs_inode_lookup(struct super_block *sb, int ino_num) 4062306a36Sopenharmony_ci{ 4162306a36Sopenharmony_ci struct squashfs_sb_info *msblk = sb->s_fs_info; 4262306a36Sopenharmony_ci int blk = SQUASHFS_LOOKUP_BLOCK(ino_num - 1); 4362306a36Sopenharmony_ci int offset = SQUASHFS_LOOKUP_BLOCK_OFFSET(ino_num - 1); 4462306a36Sopenharmony_ci u64 start; 4562306a36Sopenharmony_ci __le64 ino; 4662306a36Sopenharmony_ci int err; 4762306a36Sopenharmony_ci 4862306a36Sopenharmony_ci TRACE("Entered squashfs_inode_lookup, inode_number = %d\n", ino_num); 4962306a36Sopenharmony_ci 5062306a36Sopenharmony_ci if (ino_num == 0 || (ino_num - 1) >= msblk->inodes) 5162306a36Sopenharmony_ci return -EINVAL; 5262306a36Sopenharmony_ci 5362306a36Sopenharmony_ci start = le64_to_cpu(msblk->inode_lookup_table[blk]); 5462306a36Sopenharmony_ci 5562306a36Sopenharmony_ci err = squashfs_read_metadata(sb, &ino, &start, &offset, sizeof(ino)); 5662306a36Sopenharmony_ci if (err < 0) 5762306a36Sopenharmony_ci return err; 5862306a36Sopenharmony_ci 5962306a36Sopenharmony_ci TRACE("squashfs_inode_lookup, inode = 0x%llx\n", 6062306a36Sopenharmony_ci (u64) le64_to_cpu(ino)); 6162306a36Sopenharmony_ci 6262306a36Sopenharmony_ci return le64_to_cpu(ino); 6362306a36Sopenharmony_ci} 6462306a36Sopenharmony_ci 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_cistatic struct dentry *squashfs_export_iget(struct super_block *sb, 6762306a36Sopenharmony_ci unsigned int ino_num) 6862306a36Sopenharmony_ci{ 6962306a36Sopenharmony_ci long long ino; 7062306a36Sopenharmony_ci struct dentry *dentry = ERR_PTR(-ENOENT); 7162306a36Sopenharmony_ci 7262306a36Sopenharmony_ci TRACE("Entered squashfs_export_iget\n"); 7362306a36Sopenharmony_ci 7462306a36Sopenharmony_ci ino = squashfs_inode_lookup(sb, ino_num); 7562306a36Sopenharmony_ci if (ino >= 0) 7662306a36Sopenharmony_ci dentry = d_obtain_alias(squashfs_iget(sb, ino, ino_num)); 7762306a36Sopenharmony_ci 7862306a36Sopenharmony_ci return dentry; 7962306a36Sopenharmony_ci} 8062306a36Sopenharmony_ci 8162306a36Sopenharmony_ci 8262306a36Sopenharmony_cistatic struct dentry *squashfs_fh_to_dentry(struct super_block *sb, 8362306a36Sopenharmony_ci struct fid *fid, int fh_len, int fh_type) 8462306a36Sopenharmony_ci{ 8562306a36Sopenharmony_ci if ((fh_type != FILEID_INO32_GEN && fh_type != FILEID_INO32_GEN_PARENT) 8662306a36Sopenharmony_ci || fh_len < 2) 8762306a36Sopenharmony_ci return NULL; 8862306a36Sopenharmony_ci 8962306a36Sopenharmony_ci return squashfs_export_iget(sb, fid->i32.ino); 9062306a36Sopenharmony_ci} 9162306a36Sopenharmony_ci 9262306a36Sopenharmony_ci 9362306a36Sopenharmony_cistatic struct dentry *squashfs_fh_to_parent(struct super_block *sb, 9462306a36Sopenharmony_ci struct fid *fid, int fh_len, int fh_type) 9562306a36Sopenharmony_ci{ 9662306a36Sopenharmony_ci if (fh_type != FILEID_INO32_GEN_PARENT || fh_len < 4) 9762306a36Sopenharmony_ci return NULL; 9862306a36Sopenharmony_ci 9962306a36Sopenharmony_ci return squashfs_export_iget(sb, fid->i32.parent_ino); 10062306a36Sopenharmony_ci} 10162306a36Sopenharmony_ci 10262306a36Sopenharmony_ci 10362306a36Sopenharmony_cistatic struct dentry *squashfs_get_parent(struct dentry *child) 10462306a36Sopenharmony_ci{ 10562306a36Sopenharmony_ci struct inode *inode = d_inode(child); 10662306a36Sopenharmony_ci unsigned int parent_ino = squashfs_i(inode)->parent; 10762306a36Sopenharmony_ci 10862306a36Sopenharmony_ci return squashfs_export_iget(inode->i_sb, parent_ino); 10962306a36Sopenharmony_ci} 11062306a36Sopenharmony_ci 11162306a36Sopenharmony_ci 11262306a36Sopenharmony_ci/* 11362306a36Sopenharmony_ci * Read uncompressed inode lookup table indexes off disk into memory 11462306a36Sopenharmony_ci */ 11562306a36Sopenharmony_ci__le64 *squashfs_read_inode_lookup_table(struct super_block *sb, 11662306a36Sopenharmony_ci u64 lookup_table_start, u64 next_table, unsigned int inodes) 11762306a36Sopenharmony_ci{ 11862306a36Sopenharmony_ci unsigned int length = SQUASHFS_LOOKUP_BLOCK_BYTES(inodes); 11962306a36Sopenharmony_ci unsigned int indexes = SQUASHFS_LOOKUP_BLOCKS(inodes); 12062306a36Sopenharmony_ci int n; 12162306a36Sopenharmony_ci __le64 *table; 12262306a36Sopenharmony_ci u64 start, end; 12362306a36Sopenharmony_ci 12462306a36Sopenharmony_ci TRACE("In read_inode_lookup_table, length %d\n", length); 12562306a36Sopenharmony_ci 12662306a36Sopenharmony_ci /* Sanity check values */ 12762306a36Sopenharmony_ci 12862306a36Sopenharmony_ci /* there should always be at least one inode */ 12962306a36Sopenharmony_ci if (inodes == 0) 13062306a36Sopenharmony_ci return ERR_PTR(-EINVAL); 13162306a36Sopenharmony_ci 13262306a36Sopenharmony_ci /* 13362306a36Sopenharmony_ci * The computed size of the lookup table (length bytes) should exactly 13462306a36Sopenharmony_ci * match the table start and end points 13562306a36Sopenharmony_ci */ 13662306a36Sopenharmony_ci if (length != (next_table - lookup_table_start)) 13762306a36Sopenharmony_ci return ERR_PTR(-EINVAL); 13862306a36Sopenharmony_ci 13962306a36Sopenharmony_ci table = squashfs_read_table(sb, lookup_table_start, length); 14062306a36Sopenharmony_ci if (IS_ERR(table)) 14162306a36Sopenharmony_ci return table; 14262306a36Sopenharmony_ci 14362306a36Sopenharmony_ci /* 14462306a36Sopenharmony_ci * table0], table[1], ... table[indexes - 1] store the locations 14562306a36Sopenharmony_ci * of the compressed inode lookup blocks. Each entry should be 14662306a36Sopenharmony_ci * less than the next (i.e. table[0] < table[1]), and the difference 14762306a36Sopenharmony_ci * between them should be SQUASHFS_METADATA_SIZE or less. 14862306a36Sopenharmony_ci * table[indexes - 1] should be less than lookup_table_start, and 14962306a36Sopenharmony_ci * again the difference should be SQUASHFS_METADATA_SIZE or less 15062306a36Sopenharmony_ci */ 15162306a36Sopenharmony_ci for (n = 0; n < (indexes - 1); n++) { 15262306a36Sopenharmony_ci start = le64_to_cpu(table[n]); 15362306a36Sopenharmony_ci end = le64_to_cpu(table[n + 1]); 15462306a36Sopenharmony_ci 15562306a36Sopenharmony_ci if (start >= end 15662306a36Sopenharmony_ci || (end - start) > 15762306a36Sopenharmony_ci (SQUASHFS_METADATA_SIZE + SQUASHFS_BLOCK_OFFSET)) { 15862306a36Sopenharmony_ci kfree(table); 15962306a36Sopenharmony_ci return ERR_PTR(-EINVAL); 16062306a36Sopenharmony_ci } 16162306a36Sopenharmony_ci } 16262306a36Sopenharmony_ci 16362306a36Sopenharmony_ci start = le64_to_cpu(table[indexes - 1]); 16462306a36Sopenharmony_ci if (start >= lookup_table_start || 16562306a36Sopenharmony_ci (lookup_table_start - start) > 16662306a36Sopenharmony_ci (SQUASHFS_METADATA_SIZE + SQUASHFS_BLOCK_OFFSET)) { 16762306a36Sopenharmony_ci kfree(table); 16862306a36Sopenharmony_ci return ERR_PTR(-EINVAL); 16962306a36Sopenharmony_ci } 17062306a36Sopenharmony_ci 17162306a36Sopenharmony_ci return table; 17262306a36Sopenharmony_ci} 17362306a36Sopenharmony_ci 17462306a36Sopenharmony_ci 17562306a36Sopenharmony_ciconst struct export_operations squashfs_export_ops = { 17662306a36Sopenharmony_ci .fh_to_dentry = squashfs_fh_to_dentry, 17762306a36Sopenharmony_ci .fh_to_parent = squashfs_fh_to_parent, 17862306a36Sopenharmony_ci .get_parent = squashfs_get_parent 17962306a36Sopenharmony_ci}; 180