blob: 49daaf669e41809d33b377c40c2f39e0f2e462c7 [file] [log] [blame]
Phillip Lougher6545b242009-01-05 08:46:22 +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 * inode.c
22 */
23
24/*
25 * This file implements code to create and read inodes from disk.
26 *
27 * Inodes in Squashfs are identified by a 48-bit inode which encodes the
28 * location of the compressed metadata block containing the inode, and the byte
29 * offset into that block where the inode is placed (<block, offset>).
30 *
31 * To maximise compression there are different inodes for each file type
32 * (regular file, directory, device, etc.), the inode contents and length
33 * varying with the type.
34 *
35 * To further maximise compression, two types of regular file inode and
36 * directory inode are defined: inodes optimised for frequently occurring
37 * regular files and directories, and extended types where extra
38 * information has to be stored.
39 */
40
41#include <linux/fs.h>
42#include <linux/vfs.h>
Phillip Lougher6545b242009-01-05 08:46:22 +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 * Initialise VFS inode with the base inode information common to all
51 * Squashfs inode types. Sqsh_ino contains the unswapped base inode
52 * off disk.
53 */
54static int squashfs_new_inode(struct super_block *sb, struct inode *inode,
55 struct squashfs_base_inode *sqsh_ino)
56{
57 int err;
58
59 err = squashfs_get_id(sb, le16_to_cpu(sqsh_ino->uid), &inode->i_uid);
60 if (err)
61 return err;
62
63 err = squashfs_get_id(sb, le16_to_cpu(sqsh_ino->guid), &inode->i_gid);
64 if (err)
65 return err;
66
67 inode->i_ino = le32_to_cpu(sqsh_ino->inode_number);
68 inode->i_mtime.tv_sec = le32_to_cpu(sqsh_ino->mtime);
69 inode->i_atime.tv_sec = inode->i_mtime.tv_sec;
70 inode->i_ctime.tv_sec = inode->i_mtime.tv_sec;
71 inode->i_mode = le16_to_cpu(sqsh_ino->mode);
72 inode->i_size = 0;
73
74 return err;
75}
76
77
78struct inode *squashfs_iget(struct super_block *sb, long long ino,
79 unsigned int ino_number)
80{
81 struct inode *inode = iget_locked(sb, ino_number);
82 int err;
83
84 TRACE("Entered squashfs_iget\n");
85
86 if (!inode)
87 return ERR_PTR(-ENOMEM);
88 if (!(inode->i_state & I_NEW))
89 return inode;
90
91 err = squashfs_read_inode(inode, ino);
92 if (err) {
93 iget_failed(inode);
94 return ERR_PTR(err);
95 }
96
97 unlock_new_inode(inode);
98 return inode;
99}
100
101
102/*
103 * Initialise VFS inode by reading inode from inode table (compressed
104 * metadata). The format and amount of data read depends on type.
105 */
106int squashfs_read_inode(struct inode *inode, long long ino)
107{
108 struct super_block *sb = inode->i_sb;
109 struct squashfs_sb_info *msblk = sb->s_fs_info;
110 u64 block = SQUASHFS_INODE_BLK(ino) + msblk->inode_table;
111 int err, type, offset = SQUASHFS_INODE_OFFSET(ino);
112 union squashfs_inode squashfs_ino;
113 struct squashfs_base_inode *sqshb_ino = &squashfs_ino.base;
114
115 TRACE("Entered squashfs_read_inode\n");
116
117 /*
118 * Read inode base common to all inode types.
119 */
120 err = squashfs_read_metadata(sb, sqshb_ino, &block,
121 &offset, sizeof(*sqshb_ino));
122 if (err < 0)
123 goto failed_read;
124
125 err = squashfs_new_inode(sb, inode, sqshb_ino);
126 if (err)
127 goto failed_read;
128
129 block = SQUASHFS_INODE_BLK(ino) + msblk->inode_table;
130 offset = SQUASHFS_INODE_OFFSET(ino);
131
132 type = le16_to_cpu(sqshb_ino->inode_type);
133 switch (type) {
134 case SQUASHFS_REG_TYPE: {
Roel Kluinf4f80562009-03-05 00:55:31 +0000135 unsigned int frag_offset, frag;
136 int frag_size;
Phillip Lougher6545b242009-01-05 08:46:22 +0000137 u64 frag_blk;
138 struct squashfs_reg_inode *sqsh_ino = &squashfs_ino.reg;
139
140 err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
141 sizeof(*sqsh_ino));
142 if (err < 0)
143 goto failed_read;
144
145 frag = le32_to_cpu(sqsh_ino->fragment);
146 if (frag != SQUASHFS_INVALID_FRAG) {
147 frag_offset = le32_to_cpu(sqsh_ino->offset);
148 frag_size = squashfs_frag_lookup(sb, frag, &frag_blk);
149 if (frag_size < 0) {
150 err = frag_size;
151 goto failed_read;
152 }
153 } else {
154 frag_blk = SQUASHFS_INVALID_BLK;
155 frag_size = 0;
156 frag_offset = 0;
157 }
158
159 inode->i_nlink = 1;
160 inode->i_size = le32_to_cpu(sqsh_ino->file_size);
161 inode->i_fop = &generic_ro_fops;
162 inode->i_mode |= S_IFREG;
163 inode->i_blocks = ((inode->i_size - 1) >> 9) + 1;
164 squashfs_i(inode)->fragment_block = frag_blk;
165 squashfs_i(inode)->fragment_size = frag_size;
166 squashfs_i(inode)->fragment_offset = frag_offset;
167 squashfs_i(inode)->start = le32_to_cpu(sqsh_ino->start_block);
168 squashfs_i(inode)->block_list_start = block;
169 squashfs_i(inode)->offset = offset;
170 inode->i_data.a_ops = &squashfs_aops;
171
172 TRACE("File inode %x:%x, start_block %llx, block_list_start "
173 "%llx, offset %x\n", SQUASHFS_INODE_BLK(ino),
174 offset, squashfs_i(inode)->start, block, offset);
175 break;
176 }
177 case SQUASHFS_LREG_TYPE: {
Roel Kluinf4f80562009-03-05 00:55:31 +0000178 unsigned int frag_offset, frag;
179 int frag_size;
Phillip Lougher6545b242009-01-05 08:46:22 +0000180 u64 frag_blk;
181 struct squashfs_lreg_inode *sqsh_ino = &squashfs_ino.lreg;
182
183 err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
184 sizeof(*sqsh_ino));
185 if (err < 0)
186 goto failed_read;
187
188 frag = le32_to_cpu(sqsh_ino->fragment);
189 if (frag != SQUASHFS_INVALID_FRAG) {
190 frag_offset = le32_to_cpu(sqsh_ino->offset);
191 frag_size = squashfs_frag_lookup(sb, frag, &frag_blk);
192 if (frag_size < 0) {
193 err = frag_size;
194 goto failed_read;
195 }
196 } else {
197 frag_blk = SQUASHFS_INVALID_BLK;
198 frag_size = 0;
199 frag_offset = 0;
200 }
201
202 inode->i_nlink = le32_to_cpu(sqsh_ino->nlink);
203 inode->i_size = le64_to_cpu(sqsh_ino->file_size);
204 inode->i_fop = &generic_ro_fops;
205 inode->i_mode |= S_IFREG;
206 inode->i_blocks = ((inode->i_size -
207 le64_to_cpu(sqsh_ino->sparse) - 1) >> 9) + 1;
208
209 squashfs_i(inode)->fragment_block = frag_blk;
210 squashfs_i(inode)->fragment_size = frag_size;
211 squashfs_i(inode)->fragment_offset = frag_offset;
212 squashfs_i(inode)->start = le64_to_cpu(sqsh_ino->start_block);
213 squashfs_i(inode)->block_list_start = block;
214 squashfs_i(inode)->offset = offset;
215 inode->i_data.a_ops = &squashfs_aops;
216
217 TRACE("File inode %x:%x, start_block %llx, block_list_start "
218 "%llx, offset %x\n", SQUASHFS_INODE_BLK(ino),
219 offset, squashfs_i(inode)->start, block, offset);
220 break;
221 }
222 case SQUASHFS_DIR_TYPE: {
223 struct squashfs_dir_inode *sqsh_ino = &squashfs_ino.dir;
224
225 err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
226 sizeof(*sqsh_ino));
227 if (err < 0)
228 goto failed_read;
229
230 inode->i_nlink = le32_to_cpu(sqsh_ino->nlink);
231 inode->i_size = le16_to_cpu(sqsh_ino->file_size);
232 inode->i_op = &squashfs_dir_inode_ops;
233 inode->i_fop = &squashfs_dir_ops;
234 inode->i_mode |= S_IFDIR;
235 squashfs_i(inode)->start = le32_to_cpu(sqsh_ino->start_block);
236 squashfs_i(inode)->offset = le16_to_cpu(sqsh_ino->offset);
237 squashfs_i(inode)->dir_idx_cnt = 0;
238 squashfs_i(inode)->parent = le32_to_cpu(sqsh_ino->parent_inode);
239
240 TRACE("Directory inode %x:%x, start_block %llx, offset %x\n",
241 SQUASHFS_INODE_BLK(ino), offset,
242 squashfs_i(inode)->start,
243 le16_to_cpu(sqsh_ino->offset));
244 break;
245 }
246 case SQUASHFS_LDIR_TYPE: {
247 struct squashfs_ldir_inode *sqsh_ino = &squashfs_ino.ldir;
248
249 err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
250 sizeof(*sqsh_ino));
251 if (err < 0)
252 goto failed_read;
253
254 inode->i_nlink = le32_to_cpu(sqsh_ino->nlink);
255 inode->i_size = le32_to_cpu(sqsh_ino->file_size);
256 inode->i_op = &squashfs_dir_inode_ops;
257 inode->i_fop = &squashfs_dir_ops;
258 inode->i_mode |= S_IFDIR;
259 squashfs_i(inode)->start = le32_to_cpu(sqsh_ino->start_block);
260 squashfs_i(inode)->offset = le16_to_cpu(sqsh_ino->offset);
261 squashfs_i(inode)->dir_idx_start = block;
262 squashfs_i(inode)->dir_idx_offset = offset;
263 squashfs_i(inode)->dir_idx_cnt = le16_to_cpu(sqsh_ino->i_count);
264 squashfs_i(inode)->parent = le32_to_cpu(sqsh_ino->parent_inode);
265
266 TRACE("Long directory inode %x:%x, start_block %llx, offset "
267 "%x\n", SQUASHFS_INODE_BLK(ino), offset,
268 squashfs_i(inode)->start,
269 le16_to_cpu(sqsh_ino->offset));
270 break;
271 }
272 case SQUASHFS_SYMLINK_TYPE:
273 case SQUASHFS_LSYMLINK_TYPE: {
274 struct squashfs_symlink_inode *sqsh_ino = &squashfs_ino.symlink;
275
276 err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
277 sizeof(*sqsh_ino));
278 if (err < 0)
279 goto failed_read;
280
281 inode->i_nlink = le32_to_cpu(sqsh_ino->nlink);
282 inode->i_size = le32_to_cpu(sqsh_ino->symlink_size);
283 inode->i_op = &page_symlink_inode_operations;
284 inode->i_data.a_ops = &squashfs_symlink_aops;
285 inode->i_mode |= S_IFLNK;
286 squashfs_i(inode)->start = block;
287 squashfs_i(inode)->offset = offset;
288
289 TRACE("Symbolic link inode %x:%x, start_block %llx, offset "
290 "%x\n", SQUASHFS_INODE_BLK(ino), offset,
291 block, offset);
292 break;
293 }
294 case SQUASHFS_BLKDEV_TYPE:
295 case SQUASHFS_CHRDEV_TYPE:
296 case SQUASHFS_LBLKDEV_TYPE:
297 case SQUASHFS_LCHRDEV_TYPE: {
298 struct squashfs_dev_inode *sqsh_ino = &squashfs_ino.dev;
299 unsigned int rdev;
300
301 err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
302 sizeof(*sqsh_ino));
303 if (err < 0)
304 goto failed_read;
305
306 if (type == SQUASHFS_CHRDEV_TYPE)
307 inode->i_mode |= S_IFCHR;
308 else
309 inode->i_mode |= S_IFBLK;
310 inode->i_nlink = le32_to_cpu(sqsh_ino->nlink);
311 rdev = le32_to_cpu(sqsh_ino->rdev);
312 init_special_inode(inode, inode->i_mode, new_decode_dev(rdev));
313
314 TRACE("Device inode %x:%x, rdev %x\n",
315 SQUASHFS_INODE_BLK(ino), offset, rdev);
316 break;
317 }
318 case SQUASHFS_FIFO_TYPE:
319 case SQUASHFS_SOCKET_TYPE:
320 case SQUASHFS_LFIFO_TYPE:
321 case SQUASHFS_LSOCKET_TYPE: {
322 struct squashfs_ipc_inode *sqsh_ino = &squashfs_ino.ipc;
323
324 err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
325 sizeof(*sqsh_ino));
326 if (err < 0)
327 goto failed_read;
328
329 if (type == SQUASHFS_FIFO_TYPE)
330 inode->i_mode |= S_IFIFO;
331 else
332 inode->i_mode |= S_IFSOCK;
333 inode->i_nlink = le32_to_cpu(sqsh_ino->nlink);
334 init_special_inode(inode, inode->i_mode, 0);
335 break;
336 }
337 default:
338 ERROR("Unknown inode type %d in squashfs_iget!\n", type);
339 return -EINVAL;
340 }
341
342 return 0;
343
344failed_read:
345 ERROR("Unable to read inode 0x%llx\n", ino);
346 return err;
347}