18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Squashfs - a compressed read only filesystem for Linux 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008 68c2ecf20Sopenharmony_ci * Phillip Lougher <phillip@squashfs.org.uk> 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * export.c 98c2ecf20Sopenharmony_ci */ 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci/* 128c2ecf20Sopenharmony_ci * This file implements code to make Squashfs filesystems exportable (NFS etc.) 138c2ecf20Sopenharmony_ci * 148c2ecf20Sopenharmony_ci * The export code uses an inode lookup table to map inode numbers passed in 158c2ecf20Sopenharmony_ci * filehandles to an inode location on disk. This table is stored compressed 168c2ecf20Sopenharmony_ci * into metadata blocks. A second index table is used to locate these. This 178c2ecf20Sopenharmony_ci * second index table for speed of access (and because it is small) is read at 188c2ecf20Sopenharmony_ci * mount time and cached in memory. 198c2ecf20Sopenharmony_ci * 208c2ecf20Sopenharmony_ci * The inode lookup table is used only by the export code, inode disk 218c2ecf20Sopenharmony_ci * locations are directly encoded in directories, enabling direct access 228c2ecf20Sopenharmony_ci * without an intermediate lookup for all operations except the export ops. 238c2ecf20Sopenharmony_ci */ 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci#include <linux/fs.h> 268c2ecf20Sopenharmony_ci#include <linux/vfs.h> 278c2ecf20Sopenharmony_ci#include <linux/dcache.h> 288c2ecf20Sopenharmony_ci#include <linux/exportfs.h> 298c2ecf20Sopenharmony_ci#include <linux/slab.h> 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci#include "squashfs_fs.h" 328c2ecf20Sopenharmony_ci#include "squashfs_fs_sb.h" 338c2ecf20Sopenharmony_ci#include "squashfs_fs_i.h" 348c2ecf20Sopenharmony_ci#include "squashfs.h" 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci/* 378c2ecf20Sopenharmony_ci * Look-up inode number (ino) in table, returning the inode location. 388c2ecf20Sopenharmony_ci */ 398c2ecf20Sopenharmony_cistatic long long squashfs_inode_lookup(struct super_block *sb, int ino_num) 408c2ecf20Sopenharmony_ci{ 418c2ecf20Sopenharmony_ci struct squashfs_sb_info *msblk = sb->s_fs_info; 428c2ecf20Sopenharmony_ci int blk = SQUASHFS_LOOKUP_BLOCK(ino_num - 1); 438c2ecf20Sopenharmony_ci int offset = SQUASHFS_LOOKUP_BLOCK_OFFSET(ino_num - 1); 448c2ecf20Sopenharmony_ci u64 start; 458c2ecf20Sopenharmony_ci __le64 ino; 468c2ecf20Sopenharmony_ci int err; 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci TRACE("Entered squashfs_inode_lookup, inode_number = %d\n", ino_num); 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci if (ino_num == 0 || (ino_num - 1) >= msblk->inodes) 518c2ecf20Sopenharmony_ci return -EINVAL; 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci start = le64_to_cpu(msblk->inode_lookup_table[blk]); 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci err = squashfs_read_metadata(sb, &ino, &start, &offset, sizeof(ino)); 568c2ecf20Sopenharmony_ci if (err < 0) 578c2ecf20Sopenharmony_ci return err; 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ci TRACE("squashfs_inode_lookup, inode = 0x%llx\n", 608c2ecf20Sopenharmony_ci (u64) le64_to_cpu(ino)); 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci return le64_to_cpu(ino); 638c2ecf20Sopenharmony_ci} 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_cistatic struct dentry *squashfs_export_iget(struct super_block *sb, 678c2ecf20Sopenharmony_ci unsigned int ino_num) 688c2ecf20Sopenharmony_ci{ 698c2ecf20Sopenharmony_ci long long ino; 708c2ecf20Sopenharmony_ci struct dentry *dentry = ERR_PTR(-ENOENT); 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci TRACE("Entered squashfs_export_iget\n"); 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci ino = squashfs_inode_lookup(sb, ino_num); 758c2ecf20Sopenharmony_ci if (ino >= 0) 768c2ecf20Sopenharmony_ci dentry = d_obtain_alias(squashfs_iget(sb, ino, ino_num)); 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci return dentry; 798c2ecf20Sopenharmony_ci} 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_cistatic struct dentry *squashfs_fh_to_dentry(struct super_block *sb, 838c2ecf20Sopenharmony_ci struct fid *fid, int fh_len, int fh_type) 848c2ecf20Sopenharmony_ci{ 858c2ecf20Sopenharmony_ci if ((fh_type != FILEID_INO32_GEN && fh_type != FILEID_INO32_GEN_PARENT) 868c2ecf20Sopenharmony_ci || fh_len < 2) 878c2ecf20Sopenharmony_ci return NULL; 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci return squashfs_export_iget(sb, fid->i32.ino); 908c2ecf20Sopenharmony_ci} 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_cistatic struct dentry *squashfs_fh_to_parent(struct super_block *sb, 948c2ecf20Sopenharmony_ci struct fid *fid, int fh_len, int fh_type) 958c2ecf20Sopenharmony_ci{ 968c2ecf20Sopenharmony_ci if (fh_type != FILEID_INO32_GEN_PARENT || fh_len < 4) 978c2ecf20Sopenharmony_ci return NULL; 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci return squashfs_export_iget(sb, fid->i32.parent_ino); 1008c2ecf20Sopenharmony_ci} 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_cistatic struct dentry *squashfs_get_parent(struct dentry *child) 1048c2ecf20Sopenharmony_ci{ 1058c2ecf20Sopenharmony_ci struct inode *inode = d_inode(child); 1068c2ecf20Sopenharmony_ci unsigned int parent_ino = squashfs_i(inode)->parent; 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci return squashfs_export_iget(inode->i_sb, parent_ino); 1098c2ecf20Sopenharmony_ci} 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci/* 1138c2ecf20Sopenharmony_ci * Read uncompressed inode lookup table indexes off disk into memory 1148c2ecf20Sopenharmony_ci */ 1158c2ecf20Sopenharmony_ci__le64 *squashfs_read_inode_lookup_table(struct super_block *sb, 1168c2ecf20Sopenharmony_ci u64 lookup_table_start, u64 next_table, unsigned int inodes) 1178c2ecf20Sopenharmony_ci{ 1188c2ecf20Sopenharmony_ci unsigned int length = SQUASHFS_LOOKUP_BLOCK_BYTES(inodes); 1198c2ecf20Sopenharmony_ci unsigned int indexes = SQUASHFS_LOOKUP_BLOCKS(inodes); 1208c2ecf20Sopenharmony_ci int n; 1218c2ecf20Sopenharmony_ci __le64 *table; 1228c2ecf20Sopenharmony_ci u64 start, end; 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci TRACE("In read_inode_lookup_table, length %d\n", length); 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci /* Sanity check values */ 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci /* there should always be at least one inode */ 1298c2ecf20Sopenharmony_ci if (inodes == 0) 1308c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci /* 1338c2ecf20Sopenharmony_ci * The computed size of the lookup table (length bytes) should exactly 1348c2ecf20Sopenharmony_ci * match the table start and end points 1358c2ecf20Sopenharmony_ci */ 1368c2ecf20Sopenharmony_ci if (length != (next_table - lookup_table_start)) 1378c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci table = squashfs_read_table(sb, lookup_table_start, length); 1408c2ecf20Sopenharmony_ci if (IS_ERR(table)) 1418c2ecf20Sopenharmony_ci return table; 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci /* 1448c2ecf20Sopenharmony_ci * table0], table[1], ... table[indexes - 1] store the locations 1458c2ecf20Sopenharmony_ci * of the compressed inode lookup blocks. Each entry should be 1468c2ecf20Sopenharmony_ci * less than the next (i.e. table[0] < table[1]), and the difference 1478c2ecf20Sopenharmony_ci * between them should be SQUASHFS_METADATA_SIZE or less. 1488c2ecf20Sopenharmony_ci * table[indexes - 1] should be less than lookup_table_start, and 1498c2ecf20Sopenharmony_ci * again the difference should be SQUASHFS_METADATA_SIZE or less 1508c2ecf20Sopenharmony_ci */ 1518c2ecf20Sopenharmony_ci for (n = 0; n < (indexes - 1); n++) { 1528c2ecf20Sopenharmony_ci start = le64_to_cpu(table[n]); 1538c2ecf20Sopenharmony_ci end = le64_to_cpu(table[n + 1]); 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci if (start >= end 1568c2ecf20Sopenharmony_ci || (end - start) > 1578c2ecf20Sopenharmony_ci (SQUASHFS_METADATA_SIZE + SQUASHFS_BLOCK_OFFSET)) { 1588c2ecf20Sopenharmony_ci kfree(table); 1598c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 1608c2ecf20Sopenharmony_ci } 1618c2ecf20Sopenharmony_ci } 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ci start = le64_to_cpu(table[indexes - 1]); 1648c2ecf20Sopenharmony_ci if (start >= lookup_table_start || 1658c2ecf20Sopenharmony_ci (lookup_table_start - start) > 1668c2ecf20Sopenharmony_ci (SQUASHFS_METADATA_SIZE + SQUASHFS_BLOCK_OFFSET)) { 1678c2ecf20Sopenharmony_ci kfree(table); 1688c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 1698c2ecf20Sopenharmony_ci } 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ci return table; 1728c2ecf20Sopenharmony_ci} 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_ciconst struct export_operations squashfs_export_ops = { 1768c2ecf20Sopenharmony_ci .fh_to_dentry = squashfs_fh_to_dentry, 1778c2ecf20Sopenharmony_ci .fh_to_parent = squashfs_fh_to_parent, 1788c2ecf20Sopenharmony_ci .get_parent = squashfs_get_parent 1798c2ecf20Sopenharmony_ci}; 180