blob: 8073b6532cf0473f7cb16d75764b3aa93d985a32 [file] [log] [blame]
Phillip Lougher12260142009-01-05 08:46:25 +00001/*
2 * Squashfs - a compressed read only filesystem for Linux
3 *
4 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
Phillip Lougherd7f2ff62011-05-26 10:39:56 +01005 * Phillip Lougher <phillip@squashfs.org.uk>
Phillip Lougher12260142009-01-05 08:46:25 +00006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2,
10 * or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 * export.c
22 */
23
24/*
25 * This file implements code to make Squashfs filesystems exportable (NFS etc.)
26 *
27 * The export code uses an inode lookup table to map inode numbers passed in
28 * filehandles to an inode location on disk. This table is stored compressed
29 * into metadata blocks. A second index table is used to locate these. This
30 * second index table for speed of access (and because it is small) is read at
31 * mount time and cached in memory.
32 *
33 * The inode lookup table is used only by the export code, inode disk
34 * locations are directly encoded in directories, enabling direct access
35 * without an intermediate lookup for all operations except the export ops.
36 */
37
38#include <linux/fs.h>
39#include <linux/vfs.h>
40#include <linux/dcache.h>
41#include <linux/exportfs.h>
Pekka Enberg23516dc2009-03-24 10:56:39 +020042#include <linux/slab.h>
Phillip Lougher12260142009-01-05 08:46:25 +000043
44#include "squashfs_fs.h"
45#include "squashfs_fs_sb.h"
46#include "squashfs_fs_i.h"
47#include "squashfs.h"
48
49/*
50 * Look-up inode number (ino) in table, returning the inode location.
51 */
52static long long squashfs_inode_lookup(struct super_block *sb, int ino_num)
53{
54 struct squashfs_sb_info *msblk = sb->s_fs_info;
55 int blk = SQUASHFS_LOOKUP_BLOCK(ino_num - 1);
56 int offset = SQUASHFS_LOOKUP_BLOCK_OFFSET(ino_num - 1);
57 u64 start = le64_to_cpu(msblk->inode_lookup_table[blk]);
58 __le64 ino;
59 int err;
60
61 TRACE("Entered squashfs_inode_lookup, inode_number = %d\n", ino_num);
62
63 err = squashfs_read_metadata(sb, &ino, &start, &offset, sizeof(ino));
64 if (err < 0)
65 return err;
66
67 TRACE("squashfs_inode_lookup, inode = 0x%llx\n",
68 (u64) le64_to_cpu(ino));
69
70 return le64_to_cpu(ino);
71}
72
73
74static struct dentry *squashfs_export_iget(struct super_block *sb,
75 unsigned int ino_num)
76{
77 long long ino;
78 struct dentry *dentry = ERR_PTR(-ENOENT);
79
80 TRACE("Entered squashfs_export_iget\n");
81
82 ino = squashfs_inode_lookup(sb, ino_num);
83 if (ino >= 0)
84 dentry = d_obtain_alias(squashfs_iget(sb, ino, ino_num));
85
86 return dentry;
87}
88
89
90static struct dentry *squashfs_fh_to_dentry(struct super_block *sb,
91 struct fid *fid, int fh_len, int fh_type)
92{
93 if ((fh_type != FILEID_INO32_GEN && fh_type != FILEID_INO32_GEN_PARENT)
94 || fh_len < 2)
95 return NULL;
96
97 return squashfs_export_iget(sb, fid->i32.ino);
98}
99
100
101static struct dentry *squashfs_fh_to_parent(struct super_block *sb,
102 struct fid *fid, int fh_len, int fh_type)
103{
104 if (fh_type != FILEID_INO32_GEN_PARENT || fh_len < 4)
105 return NULL;
106
107 return squashfs_export_iget(sb, fid->i32.parent_ino);
108}
109
110
111static struct dentry *squashfs_get_parent(struct dentry *child)
112{
David Howells2b0143b2015-03-17 22:25:59 +0000113 struct inode *inode = d_inode(child);
Phillip Lougher12260142009-01-05 08:46:25 +0000114 unsigned int parent_ino = squashfs_i(inode)->parent;
115
116 return squashfs_export_iget(inode->i_sb, parent_ino);
117}
118
119
120/*
121 * Read uncompressed inode lookup table indexes off disk into memory
122 */
123__le64 *squashfs_read_inode_lookup_table(struct super_block *sb,
Phillip Lougherac51a0a2011-05-24 04:15:21 +0100124 u64 lookup_table_start, u64 next_table, unsigned int inodes)
Phillip Lougher12260142009-01-05 08:46:25 +0000125{
126 unsigned int length = SQUASHFS_LOOKUP_BLOCK_BYTES(inodes);
Phillip Lougherac51a0a2011-05-24 04:15:21 +0100127 __le64 *table;
Phillip Lougher12260142009-01-05 08:46:25 +0000128
129 TRACE("In read_inode_lookup_table, length %d\n", length);
130
Phillip Lougherac51a0a2011-05-24 04:15:21 +0100131 /* Sanity check values */
132
133 /* there should always be at least one inode */
134 if (inodes == 0)
135 return ERR_PTR(-EINVAL);
136
137 /* length bytes should not extend into the next table - this check
138 * also traps instances where lookup_table_start is incorrectly larger
139 * than the next table start
140 */
141 if (lookup_table_start + length > next_table)
142 return ERR_PTR(-EINVAL);
143
144 table = squashfs_read_table(sb, lookup_table_start, length);
145
146 /*
147 * table[0] points to the first inode lookup table metadata block,
148 * this should be less than lookup_table_start
149 */
Phillip Lougherd5b72ce2011-05-29 00:38:46 +0100150 if (!IS_ERR(table) && le64_to_cpu(table[0]) >= lookup_table_start) {
Phillip Lougherac51a0a2011-05-24 04:15:21 +0100151 kfree(table);
152 return ERR_PTR(-EINVAL);
153 }
154
155 return table;
Phillip Lougher12260142009-01-05 08:46:25 +0000156}
157
158
159const struct export_operations squashfs_export_ops = {
160 .fh_to_dentry = squashfs_fh_to_dentry,
161 .fh_to_parent = squashfs_fh_to_parent,
162 .get_parent = squashfs_get_parent
163};