blob: 2b1b8fe5e03765cdb91cc6311176d31a8ea3d054 [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
5 * Phillip Lougher <phillip@lougher.demon.co.uk>
6 *
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>
42#include <linux/zlib.h>
Pekka Enberg23516dc2009-03-24 10:56:39 +020043#include <linux/slab.h>
Phillip Lougher12260142009-01-05 08:46:25 +000044
45#include "squashfs_fs.h"
46#include "squashfs_fs_sb.h"
47#include "squashfs_fs_i.h"
48#include "squashfs.h"
49
50/*
51 * Look-up inode number (ino) in table, returning the inode location.
52 */
53static long long squashfs_inode_lookup(struct super_block *sb, int ino_num)
54{
55 struct squashfs_sb_info *msblk = sb->s_fs_info;
56 int blk = SQUASHFS_LOOKUP_BLOCK(ino_num - 1);
57 int offset = SQUASHFS_LOOKUP_BLOCK_OFFSET(ino_num - 1);
58 u64 start = le64_to_cpu(msblk->inode_lookup_table[blk]);
59 __le64 ino;
60 int err;
61
62 TRACE("Entered squashfs_inode_lookup, inode_number = %d\n", ino_num);
63
64 err = squashfs_read_metadata(sb, &ino, &start, &offset, sizeof(ino));
65 if (err < 0)
66 return err;
67
68 TRACE("squashfs_inode_lookup, inode = 0x%llx\n",
69 (u64) le64_to_cpu(ino));
70
71 return le64_to_cpu(ino);
72}
73
74
75static struct dentry *squashfs_export_iget(struct super_block *sb,
76 unsigned int ino_num)
77{
78 long long ino;
79 struct dentry *dentry = ERR_PTR(-ENOENT);
80
81 TRACE("Entered squashfs_export_iget\n");
82
83 ino = squashfs_inode_lookup(sb, ino_num);
84 if (ino >= 0)
85 dentry = d_obtain_alias(squashfs_iget(sb, ino, ino_num));
86
87 return dentry;
88}
89
90
91static struct dentry *squashfs_fh_to_dentry(struct super_block *sb,
92 struct fid *fid, int fh_len, int fh_type)
93{
94 if ((fh_type != FILEID_INO32_GEN && fh_type != FILEID_INO32_GEN_PARENT)
95 || fh_len < 2)
96 return NULL;
97
98 return squashfs_export_iget(sb, fid->i32.ino);
99}
100
101
102static struct dentry *squashfs_fh_to_parent(struct super_block *sb,
103 struct fid *fid, int fh_len, int fh_type)
104{
105 if (fh_type != FILEID_INO32_GEN_PARENT || fh_len < 4)
106 return NULL;
107
108 return squashfs_export_iget(sb, fid->i32.parent_ino);
109}
110
111
112static struct dentry *squashfs_get_parent(struct dentry *child)
113{
114 struct inode *inode = child->d_inode;
115 unsigned int parent_ino = squashfs_i(inode)->parent;
116
117 return squashfs_export_iget(inode->i_sb, parent_ino);
118}
119
120
121/*
122 * Read uncompressed inode lookup table indexes off disk into memory
123 */
124__le64 *squashfs_read_inode_lookup_table(struct super_block *sb,
125 u64 lookup_table_start, unsigned int inodes)
126{
127 unsigned int length = SQUASHFS_LOOKUP_BLOCK_BYTES(inodes);
128 __le64 *inode_lookup_table;
129 int err;
130
131 TRACE("In read_inode_lookup_table, length %d\n", length);
132
133 /* Allocate inode lookup table indexes */
134 inode_lookup_table = kmalloc(length, GFP_KERNEL);
135 if (inode_lookup_table == NULL) {
136 ERROR("Failed to allocate inode lookup table\n");
137 return ERR_PTR(-ENOMEM);
138 }
139
140 err = squashfs_read_table(sb, inode_lookup_table, lookup_table_start,
141 length);
142 if (err < 0) {
143 ERROR("unable to read inode lookup table\n");
144 kfree(inode_lookup_table);
145 return ERR_PTR(err);
146 }
147
148 return inode_lookup_table;
149}
150
151
152const struct export_operations squashfs_export_ops = {
153 .fh_to_dentry = squashfs_fh_to_dentry,
154 .fh_to_parent = squashfs_fh_to_parent,
155 .get_parent = squashfs_get_parent
156};