1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Squashfs - a compressed read only filesystem for Linux
5 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
6 * Phillip Lougher <phillip@squashfs.org.uk>
12 * This file implements code to make Squashfs filesystems exportable (NFS etc.)
14 * The export code uses an inode lookup table to map inode numbers passed in
15 * filehandles to an inode location on disk. This table is stored compressed
16 * into metadata blocks. A second index table is used to locate these. This
17 * second index table for speed of access (and because it is small) is read at
18 * mount time and cached in memory.
20 * The inode lookup table is used only by the export code, inode disk
21 * locations are directly encoded in directories, enabling direct access
22 * without an intermediate lookup for all operations except the export ops.
26 #include <linux/vfs.h>
27 #include <linux/dcache.h>
28 #include <linux/exportfs.h>
29 #include <linux/slab.h>
31 #include "squashfs_fs.h"
32 #include "squashfs_fs_sb.h"
33 #include "squashfs_fs_i.h"
37 * Look-up inode number (ino) in table, returning the inode location.
39 static long long squashfs_inode_lookup(struct super_block
*sb
, int ino_num
)
41 struct squashfs_sb_info
*msblk
= sb
->s_fs_info
;
42 int blk
= SQUASHFS_LOOKUP_BLOCK(ino_num
- 1);
43 int offset
= SQUASHFS_LOOKUP_BLOCK_OFFSET(ino_num
- 1);
44 u64 start
= le64_to_cpu(msblk
->inode_lookup_table
[blk
]);
48 TRACE("Entered squashfs_inode_lookup, inode_number = %d\n", ino_num
);
50 err
= squashfs_read_metadata(sb
, &ino
, &start
, &offset
, sizeof(ino
));
54 TRACE("squashfs_inode_lookup, inode = 0x%llx\n",
55 (u64
) le64_to_cpu(ino
));
57 return le64_to_cpu(ino
);
61 static struct dentry
*squashfs_export_iget(struct super_block
*sb
,
65 struct dentry
*dentry
= ERR_PTR(-ENOENT
);
67 TRACE("Entered squashfs_export_iget\n");
69 ino
= squashfs_inode_lookup(sb
, ino_num
);
71 dentry
= d_obtain_alias(squashfs_iget(sb
, ino
, ino_num
));
77 static struct dentry
*squashfs_fh_to_dentry(struct super_block
*sb
,
78 struct fid
*fid
, int fh_len
, int fh_type
)
80 if ((fh_type
!= FILEID_INO32_GEN
&& fh_type
!= FILEID_INO32_GEN_PARENT
)
84 return squashfs_export_iget(sb
, fid
->i32
.ino
);
88 static struct dentry
*squashfs_fh_to_parent(struct super_block
*sb
,
89 struct fid
*fid
, int fh_len
, int fh_type
)
91 if (fh_type
!= FILEID_INO32_GEN_PARENT
|| fh_len
< 4)
94 return squashfs_export_iget(sb
, fid
->i32
.parent_ino
);
98 static struct dentry
*squashfs_get_parent(struct dentry
*child
)
100 struct inode
*inode
= d_inode(child
);
101 unsigned int parent_ino
= squashfs_i(inode
)->parent
;
103 return squashfs_export_iget(inode
->i_sb
, parent_ino
);
108 * Read uncompressed inode lookup table indexes off disk into memory
110 __le64
*squashfs_read_inode_lookup_table(struct super_block
*sb
,
111 u64 lookup_table_start
, u64 next_table
, unsigned int inodes
)
113 unsigned int length
= SQUASHFS_LOOKUP_BLOCK_BYTES(inodes
);
116 TRACE("In read_inode_lookup_table, length %d\n", length
);
118 /* Sanity check values */
120 /* there should always be at least one inode */
122 return ERR_PTR(-EINVAL
);
124 /* length bytes should not extend into the next table - this check
125 * also traps instances where lookup_table_start is incorrectly larger
126 * than the next table start
128 if (lookup_table_start
+ length
> next_table
)
129 return ERR_PTR(-EINVAL
);
131 table
= squashfs_read_table(sb
, lookup_table_start
, length
);
134 * table[0] points to the first inode lookup table metadata block,
135 * this should be less than lookup_table_start
137 if (!IS_ERR(table
) && le64_to_cpu(table
[0]) >= lookup_table_start
) {
139 return ERR_PTR(-EINVAL
);
146 const struct export_operations squashfs_export_ops
= {
147 .fh_to_dentry
= squashfs_fh_to_dentry
,
148 .fh_to_parent
= squashfs_fh_to_parent
,
149 .get_parent
= squashfs_get_parent