2 * Squashfs - a compressed read only filesystem for Linux
4 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
5 * Phillip Lougher <phillip@lougher.demon.co.uk>
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.
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.
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.
25 * This file implements code to create and read inodes from disk.
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>).
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.
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.
42 #include <linux/vfs.h>
43 #include <linux/xattr.h>
45 #include "squashfs_fs.h"
46 #include "squashfs_fs_sb.h"
47 #include "squashfs_fs_i.h"
51 * Initialise VFS inode with the base inode information common to all
52 * Squashfs inode types. Sqsh_ino contains the unswapped base inode
55 static int squashfs_new_inode(struct super_block *sb, struct inode *inode,
56 struct squashfs_base_inode *sqsh_ino)
60 err = squashfs_get_id(sb, le16_to_cpu(sqsh_ino->uid), &inode->i_uid);
64 err = squashfs_get_id(sb, le16_to_cpu(sqsh_ino->guid), &inode->i_gid);
68 inode->i_ino = le32_to_cpu(sqsh_ino->inode_number);
69 inode->i_mtime.tv_sec = le32_to_cpu(sqsh_ino->mtime);
70 inode->i_atime.tv_sec = inode->i_mtime.tv_sec;
71 inode->i_ctime.tv_sec = inode->i_mtime.tv_sec;
72 inode->i_mode = le16_to_cpu(sqsh_ino->mode);
79 struct inode *squashfs_iget(struct super_block *sb, long long ino,
80 unsigned int ino_number)
82 struct inode *inode = iget_locked(sb, ino_number);
85 TRACE("Entered squashfs_iget\n");
88 return ERR_PTR(-ENOMEM);
89 if (!(inode->i_state & I_NEW))
92 err = squashfs_read_inode(inode, ino);
98 unlock_new_inode(inode);
104 * Initialise VFS inode by reading inode from inode table (compressed
105 * metadata). The format and amount of data read depends on type.
107 int squashfs_read_inode(struct inode *inode, long long ino)
109 struct super_block *sb = inode->i_sb;
110 struct squashfs_sb_info *msblk = sb->s_fs_info;
111 u64 block = SQUASHFS_INODE_BLK(ino) + msblk->inode_table;
112 int err, type, offset = SQUASHFS_INODE_OFFSET(ino);
113 union squashfs_inode squashfs_ino;
114 struct squashfs_base_inode *sqshb_ino = &squashfs_ino.base;
115 int xattr_id = SQUASHFS_INVALID_XATTR;
117 TRACE("Entered squashfs_read_inode\n");
120 * Read inode base common to all inode types.
122 err = squashfs_read_metadata(sb, sqshb_ino, &block,
123 &offset, sizeof(*sqshb_ino));
127 err = squashfs_new_inode(sb, inode, sqshb_ino);
131 block = SQUASHFS_INODE_BLK(ino) + msblk->inode_table;
132 offset = SQUASHFS_INODE_OFFSET(ino);
134 type = le16_to_cpu(sqshb_ino->inode_type);
136 case SQUASHFS_REG_TYPE: {
137 unsigned int frag_offset, frag;
140 struct squashfs_reg_inode *sqsh_ino = &squashfs_ino.reg;
142 err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
147 frag = le32_to_cpu(sqsh_ino->fragment);
148 if (frag != SQUASHFS_INVALID_FRAG) {
149 frag_offset = le32_to_cpu(sqsh_ino->offset);
150 frag_size = squashfs_frag_lookup(sb, frag, &frag_blk);
156 frag_blk = SQUASHFS_INVALID_BLK;
162 inode->i_size = le32_to_cpu(sqsh_ino->file_size);
163 inode->i_fop = &generic_ro_fops;
164 inode->i_mode |= S_IFREG;
165 inode->i_blocks = ((inode->i_size - 1) >> 9) + 1;
166 squashfs_i(inode)->fragment_block = frag_blk;
167 squashfs_i(inode)->fragment_size = frag_size;
168 squashfs_i(inode)->fragment_offset = frag_offset;
169 squashfs_i(inode)->start = le32_to_cpu(sqsh_ino->start_block);
170 squashfs_i(inode)->block_list_start = block;
171 squashfs_i(inode)->offset = offset;
172 inode->i_data.a_ops = &squashfs_aops;
174 TRACE("File inode %x:%x, start_block %llx, block_list_start "
175 "%llx, offset %x\n", SQUASHFS_INODE_BLK(ino),
176 offset, squashfs_i(inode)->start, block, offset);
179 case SQUASHFS_LREG_TYPE: {
180 unsigned int frag_offset, frag;
183 struct squashfs_lreg_inode *sqsh_ino = &squashfs_ino.lreg;
185 err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
190 frag = le32_to_cpu(sqsh_ino->fragment);
191 if (frag != SQUASHFS_INVALID_FRAG) {
192 frag_offset = le32_to_cpu(sqsh_ino->offset);
193 frag_size = squashfs_frag_lookup(sb, frag, &frag_blk);
199 frag_blk = SQUASHFS_INVALID_BLK;
204 xattr_id = le32_to_cpu(sqsh_ino->xattr);
205 inode->i_nlink = le32_to_cpu(sqsh_ino->nlink);
206 inode->i_size = le64_to_cpu(sqsh_ino->file_size);
207 inode->i_op = &squashfs_inode_ops;
208 inode->i_fop = &generic_ro_fops;
209 inode->i_mode |= S_IFREG;
210 inode->i_blocks = ((inode->i_size -
211 le64_to_cpu(sqsh_ino->sparse) - 1) >> 9) + 1;
213 squashfs_i(inode)->fragment_block = frag_blk;
214 squashfs_i(inode)->fragment_size = frag_size;
215 squashfs_i(inode)->fragment_offset = frag_offset;
216 squashfs_i(inode)->start = le64_to_cpu(sqsh_ino->start_block);
217 squashfs_i(inode)->block_list_start = block;
218 squashfs_i(inode)->offset = offset;
219 inode->i_data.a_ops = &squashfs_aops;
221 TRACE("File inode %x:%x, start_block %llx, block_list_start "
222 "%llx, offset %x\n", SQUASHFS_INODE_BLK(ino),
223 offset, squashfs_i(inode)->start, block, offset);
226 case SQUASHFS_DIR_TYPE: {
227 struct squashfs_dir_inode *sqsh_ino = &squashfs_ino.dir;
229 err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
234 inode->i_nlink = le32_to_cpu(sqsh_ino->nlink);
235 inode->i_size = le16_to_cpu(sqsh_ino->file_size);
236 inode->i_op = &squashfs_dir_inode_ops;
237 inode->i_fop = &squashfs_dir_ops;
238 inode->i_mode |= S_IFDIR;
239 squashfs_i(inode)->start = le32_to_cpu(sqsh_ino->start_block);
240 squashfs_i(inode)->offset = le16_to_cpu(sqsh_ino->offset);
241 squashfs_i(inode)->dir_idx_cnt = 0;
242 squashfs_i(inode)->parent = le32_to_cpu(sqsh_ino->parent_inode);
244 TRACE("Directory inode %x:%x, start_block %llx, offset %x\n",
245 SQUASHFS_INODE_BLK(ino), offset,
246 squashfs_i(inode)->start,
247 le16_to_cpu(sqsh_ino->offset));
250 case SQUASHFS_LDIR_TYPE: {
251 struct squashfs_ldir_inode *sqsh_ino = &squashfs_ino.ldir;
253 err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
258 xattr_id = le32_to_cpu(sqsh_ino->xattr);
259 inode->i_nlink = le32_to_cpu(sqsh_ino->nlink);
260 inode->i_size = le32_to_cpu(sqsh_ino->file_size);
261 inode->i_op = &squashfs_dir_inode_ops;
262 inode->i_fop = &squashfs_dir_ops;
263 inode->i_mode |= S_IFDIR;
264 squashfs_i(inode)->start = le32_to_cpu(sqsh_ino->start_block);
265 squashfs_i(inode)->offset = le16_to_cpu(sqsh_ino->offset);
266 squashfs_i(inode)->dir_idx_start = block;
267 squashfs_i(inode)->dir_idx_offset = offset;
268 squashfs_i(inode)->dir_idx_cnt = le16_to_cpu(sqsh_ino->i_count);
269 squashfs_i(inode)->parent = le32_to_cpu(sqsh_ino->parent_inode);
271 TRACE("Long directory inode %x:%x, start_block %llx, offset "
272 "%x\n", SQUASHFS_INODE_BLK(ino), offset,
273 squashfs_i(inode)->start,
274 le16_to_cpu(sqsh_ino->offset));
277 case SQUASHFS_SYMLINK_TYPE:
278 case SQUASHFS_LSYMLINK_TYPE: {
279 struct squashfs_symlink_inode *sqsh_ino = &squashfs_ino.symlink;
281 err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
286 inode->i_nlink = le32_to_cpu(sqsh_ino->nlink);
287 inode->i_size = le32_to_cpu(sqsh_ino->symlink_size);
288 inode->i_op = &squashfs_symlink_inode_ops;
289 inode->i_data.a_ops = &squashfs_symlink_aops;
290 inode->i_mode |= S_IFLNK;
291 squashfs_i(inode)->start = block;
292 squashfs_i(inode)->offset = offset;
294 if (type == SQUASHFS_LSYMLINK_TYPE) {
297 err = squashfs_read_metadata(sb, NULL, &block,
298 &offset, inode->i_size);
301 err = squashfs_read_metadata(sb, &xattr, &block,
302 &offset, sizeof(xattr));
305 xattr_id = le32_to_cpu(xattr);
308 TRACE("Symbolic link inode %x:%x, start_block %llx, offset "
309 "%x\n", SQUASHFS_INODE_BLK(ino), offset,
313 case SQUASHFS_BLKDEV_TYPE:
314 case SQUASHFS_CHRDEV_TYPE: {
315 struct squashfs_dev_inode *sqsh_ino = &squashfs_ino.dev;
318 err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
323 if (type == SQUASHFS_CHRDEV_TYPE)
324 inode->i_mode |= S_IFCHR;
326 inode->i_mode |= S_IFBLK;
327 inode->i_nlink = le32_to_cpu(sqsh_ino->nlink);
328 rdev = le32_to_cpu(sqsh_ino->rdev);
329 init_special_inode(inode, inode->i_mode, new_decode_dev(rdev));
331 TRACE("Device inode %x:%x, rdev %x\n",
332 SQUASHFS_INODE_BLK(ino), offset, rdev);
335 case SQUASHFS_LBLKDEV_TYPE:
336 case SQUASHFS_LCHRDEV_TYPE: {
337 struct squashfs_ldev_inode *sqsh_ino = &squashfs_ino.ldev;
340 err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
345 if (type == SQUASHFS_LCHRDEV_TYPE)
346 inode->i_mode |= S_IFCHR;
348 inode->i_mode |= S_IFBLK;
349 xattr_id = le32_to_cpu(sqsh_ino->xattr);
350 inode->i_op = &squashfs_inode_ops;
351 inode->i_nlink = le32_to_cpu(sqsh_ino->nlink);
352 rdev = le32_to_cpu(sqsh_ino->rdev);
353 init_special_inode(inode, inode->i_mode, new_decode_dev(rdev));
355 TRACE("Device inode %x:%x, rdev %x\n",
356 SQUASHFS_INODE_BLK(ino), offset, rdev);
359 case SQUASHFS_FIFO_TYPE:
360 case SQUASHFS_SOCKET_TYPE: {
361 struct squashfs_ipc_inode *sqsh_ino = &squashfs_ino.ipc;
363 err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
368 if (type == SQUASHFS_FIFO_TYPE)
369 inode->i_mode |= S_IFIFO;
371 inode->i_mode |= S_IFSOCK;
372 inode->i_nlink = le32_to_cpu(sqsh_ino->nlink);
373 init_special_inode(inode, inode->i_mode, 0);
376 case SQUASHFS_LFIFO_TYPE:
377 case SQUASHFS_LSOCKET_TYPE: {
378 struct squashfs_lipc_inode *sqsh_ino = &squashfs_ino.lipc;
380 err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
385 if (type == SQUASHFS_LFIFO_TYPE)
386 inode->i_mode |= S_IFIFO;
388 inode->i_mode |= S_IFSOCK;
389 xattr_id = le32_to_cpu(sqsh_ino->xattr);
390 inode->i_op = &squashfs_inode_ops;
391 inode->i_nlink = le32_to_cpu(sqsh_ino->nlink);
392 init_special_inode(inode, inode->i_mode, 0);
396 ERROR("Unknown inode type %d in squashfs_iget!\n", type);
400 if (xattr_id != SQUASHFS_INVALID_XATTR && msblk->xattr_id_table) {
401 err = squashfs_xattr_lookup(sb, xattr_id,
402 &squashfs_i(inode)->xattr_count,
403 &squashfs_i(inode)->xattr_size,
404 &squashfs_i(inode)->xattr);
407 inode->i_blocks += ((squashfs_i(inode)->xattr_size - 1) >> 9)
410 squashfs_i(inode)->xattr_count = 0;
415 ERROR("Unable to read inode 0x%llx\n", ino);
420 const struct inode_operations squashfs_inode_ops = {
421 .getxattr = generic_getxattr,
422 .listxattr = squashfs_listxattr