1/*
2 * Squashfs - a compressed read only filesystem for Linux
3 *
4 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
5 * Phillip Lougher <phillip@squashfs.org.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>
43#include <linux/xattr.h>
44
45#include "squashfs_fs.h"
46#include "squashfs_fs_sb.h"
47#include "squashfs_fs_i.h"
48#include "squashfs.h"
49#include "xattr.h"
50
51/*
52 * Initialise VFS inode with the base inode information common to all
53 * Squashfs inode types.  Sqsh_ino contains the unswapped base inode
54 * off disk.
55 */
56static int squashfs_new_inode(struct super_block *sb, struct inode *inode,
57				struct squashfs_base_inode *sqsh_ino)
58{
59	uid_t i_uid;
60	gid_t i_gid;
61	int err;
62
63	err = squashfs_get_id(sb, le16_to_cpu(sqsh_ino->uid), &i_uid);
64	if (err)
65		return err;
66
67	err = squashfs_get_id(sb, le16_to_cpu(sqsh_ino->guid), &i_gid);
68	if (err)
69		return err;
70
71	i_uid_write(inode, i_uid);
72	i_gid_write(inode, i_gid);
73	inode->i_ino = le32_to_cpu(sqsh_ino->inode_number);
74	inode->i_mtime.tv_sec = le32_to_cpu(sqsh_ino->mtime);
75	inode->i_atime.tv_sec = inode->i_mtime.tv_sec;
76	inode->i_ctime.tv_sec = inode->i_mtime.tv_sec;
77	inode->i_mode = le16_to_cpu(sqsh_ino->mode);
78	inode->i_size = 0;
79
80	return err;
81}
82
83
84struct inode *squashfs_iget(struct super_block *sb, long long ino,
85				unsigned int ino_number)
86{
87	struct inode *inode = iget_locked(sb, ino_number);
88	int err;
89
90	TRACE("Entered squashfs_iget\n");
91
92	if (!inode)
93		return ERR_PTR(-ENOMEM);
94	if (!(inode->i_state & I_NEW))
95		return inode;
96
97	err = squashfs_read_inode(inode, ino);
98	if (err) {
99		iget_failed(inode);
100		return ERR_PTR(err);
101	}
102
103	unlock_new_inode(inode);
104	return inode;
105}
106
107
108/*
109 * Initialise VFS inode by reading inode from inode table (compressed
110 * metadata).  The format and amount of data read depends on type.
111 */
112int squashfs_read_inode(struct inode *inode, long long ino)
113{
114	struct super_block *sb = inode->i_sb;
115	struct squashfs_sb_info *msblk = sb->s_fs_info;
116	u64 block = SQUASHFS_INODE_BLK(ino) + msblk->inode_table;
117	int err, type, offset = SQUASHFS_INODE_OFFSET(ino);
118	union squashfs_inode squashfs_ino;
119	struct squashfs_base_inode *sqshb_ino = &squashfs_ino.base;
120	int xattr_id = SQUASHFS_INVALID_XATTR;
121
122	TRACE("Entered squashfs_read_inode\n");
123
124	/*
125	 * Read inode base common to all inode types.
126	 */
127	err = squashfs_read_metadata(sb, sqshb_ino, &block,
128				&offset, sizeof(*sqshb_ino));
129	if (err < 0)
130		goto failed_read;
131
132	err = squashfs_new_inode(sb, inode, sqshb_ino);
133	if (err)
134		goto failed_read;
135
136	block = SQUASHFS_INODE_BLK(ino) + msblk->inode_table;
137	offset = SQUASHFS_INODE_OFFSET(ino);
138
139	type = le16_to_cpu(sqshb_ino->inode_type);
140	switch (type) {
141	case SQUASHFS_REG_TYPE: {
142		unsigned int frag_offset, frag;
143		int frag_size;
144		u64 frag_blk;
145		struct squashfs_reg_inode *sqsh_ino = &squashfs_ino.reg;
146
147		err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
148							sizeof(*sqsh_ino));
149		if (err < 0)
150			goto failed_read;
151
152		frag = le32_to_cpu(sqsh_ino->fragment);
153		if (frag != SQUASHFS_INVALID_FRAG) {
154			frag_offset = le32_to_cpu(sqsh_ino->offset);
155			frag_size = squashfs_frag_lookup(sb, frag, &frag_blk);
156			if (frag_size < 0) {
157				err = frag_size;
158				goto failed_read;
159			}
160		} else {
161			frag_blk = SQUASHFS_INVALID_BLK;
162			frag_size = 0;
163			frag_offset = 0;
164		}
165
166		set_nlink(inode, 1);
167		inode->i_size = le32_to_cpu(sqsh_ino->file_size);
168		inode->i_fop = &generic_ro_fops;
169		inode->i_mode |= S_IFREG;
170		inode->i_blocks = ((inode->i_size - 1) >> 9) + 1;
171		squashfs_i(inode)->fragment_block = frag_blk;
172		squashfs_i(inode)->fragment_size = frag_size;
173		squashfs_i(inode)->fragment_offset = frag_offset;
174		squashfs_i(inode)->start = le32_to_cpu(sqsh_ino->start_block);
175		squashfs_i(inode)->block_list_start = block;
176		squashfs_i(inode)->offset = offset;
177		inode->i_data.a_ops = &squashfs_aops;
178
179		TRACE("File inode %x:%x, start_block %llx, block_list_start "
180			"%llx, offset %x\n", SQUASHFS_INODE_BLK(ino),
181			offset, squashfs_i(inode)->start, block, offset);
182		break;
183	}
184	case SQUASHFS_LREG_TYPE: {
185		unsigned int frag_offset, frag;
186		int frag_size;
187		u64 frag_blk;
188		struct squashfs_lreg_inode *sqsh_ino = &squashfs_ino.lreg;
189
190		err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
191							sizeof(*sqsh_ino));
192		if (err < 0)
193			goto failed_read;
194
195		frag = le32_to_cpu(sqsh_ino->fragment);
196		if (frag != SQUASHFS_INVALID_FRAG) {
197			frag_offset = le32_to_cpu(sqsh_ino->offset);
198			frag_size = squashfs_frag_lookup(sb, frag, &frag_blk);
199			if (frag_size < 0) {
200				err = frag_size;
201				goto failed_read;
202			}
203		} else {
204			frag_blk = SQUASHFS_INVALID_BLK;
205			frag_size = 0;
206			frag_offset = 0;
207		}
208
209		xattr_id = le32_to_cpu(sqsh_ino->xattr);
210		set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
211		inode->i_size = le64_to_cpu(sqsh_ino->file_size);
212		inode->i_op = &squashfs_inode_ops;
213		inode->i_fop = &generic_ro_fops;
214		inode->i_mode |= S_IFREG;
215		inode->i_blocks = (inode->i_size -
216				le64_to_cpu(sqsh_ino->sparse) + 511) >> 9;
217
218		squashfs_i(inode)->fragment_block = frag_blk;
219		squashfs_i(inode)->fragment_size = frag_size;
220		squashfs_i(inode)->fragment_offset = frag_offset;
221		squashfs_i(inode)->start = le64_to_cpu(sqsh_ino->start_block);
222		squashfs_i(inode)->block_list_start = block;
223		squashfs_i(inode)->offset = offset;
224		inode->i_data.a_ops = &squashfs_aops;
225
226		TRACE("File inode %x:%x, start_block %llx, block_list_start "
227			"%llx, offset %x\n", SQUASHFS_INODE_BLK(ino),
228			offset, squashfs_i(inode)->start, block, offset);
229		break;
230	}
231	case SQUASHFS_DIR_TYPE: {
232		struct squashfs_dir_inode *sqsh_ino = &squashfs_ino.dir;
233
234		err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
235				sizeof(*sqsh_ino));
236		if (err < 0)
237			goto failed_read;
238
239		set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
240		inode->i_size = le16_to_cpu(sqsh_ino->file_size);
241		inode->i_op = &squashfs_dir_inode_ops;
242		inode->i_fop = &squashfs_dir_ops;
243		inode->i_mode |= S_IFDIR;
244		squashfs_i(inode)->start = le32_to_cpu(sqsh_ino->start_block);
245		squashfs_i(inode)->offset = le16_to_cpu(sqsh_ino->offset);
246		squashfs_i(inode)->dir_idx_cnt = 0;
247		squashfs_i(inode)->parent = le32_to_cpu(sqsh_ino->parent_inode);
248
249		TRACE("Directory inode %x:%x, start_block %llx, offset %x\n",
250				SQUASHFS_INODE_BLK(ino), offset,
251				squashfs_i(inode)->start,
252				le16_to_cpu(sqsh_ino->offset));
253		break;
254	}
255	case SQUASHFS_LDIR_TYPE: {
256		struct squashfs_ldir_inode *sqsh_ino = &squashfs_ino.ldir;
257
258		err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
259				sizeof(*sqsh_ino));
260		if (err < 0)
261			goto failed_read;
262
263		xattr_id = le32_to_cpu(sqsh_ino->xattr);
264		set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
265		inode->i_size = le32_to_cpu(sqsh_ino->file_size);
266		inode->i_op = &squashfs_dir_inode_ops;
267		inode->i_fop = &squashfs_dir_ops;
268		inode->i_mode |= S_IFDIR;
269		squashfs_i(inode)->start = le32_to_cpu(sqsh_ino->start_block);
270		squashfs_i(inode)->offset = le16_to_cpu(sqsh_ino->offset);
271		squashfs_i(inode)->dir_idx_start = block;
272		squashfs_i(inode)->dir_idx_offset = offset;
273		squashfs_i(inode)->dir_idx_cnt = le16_to_cpu(sqsh_ino->i_count);
274		squashfs_i(inode)->parent = le32_to_cpu(sqsh_ino->parent_inode);
275
276		TRACE("Long directory inode %x:%x, start_block %llx, offset "
277				"%x\n", SQUASHFS_INODE_BLK(ino), offset,
278				squashfs_i(inode)->start,
279				le16_to_cpu(sqsh_ino->offset));
280		break;
281	}
282	case SQUASHFS_SYMLINK_TYPE:
283	case SQUASHFS_LSYMLINK_TYPE: {
284		struct squashfs_symlink_inode *sqsh_ino = &squashfs_ino.symlink;
285
286		err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
287				sizeof(*sqsh_ino));
288		if (err < 0)
289			goto failed_read;
290
291		set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
292		inode->i_size = le32_to_cpu(sqsh_ino->symlink_size);
293		inode->i_op = &squashfs_symlink_inode_ops;
294		inode->i_data.a_ops = &squashfs_symlink_aops;
295		inode->i_mode |= S_IFLNK;
296		squashfs_i(inode)->start = block;
297		squashfs_i(inode)->offset = offset;
298
299		if (type == SQUASHFS_LSYMLINK_TYPE) {
300			__le32 xattr;
301
302			err = squashfs_read_metadata(sb, NULL, &block,
303						&offset, inode->i_size);
304			if (err < 0)
305				goto failed_read;
306			err = squashfs_read_metadata(sb, &xattr, &block,
307						&offset, sizeof(xattr));
308			if (err < 0)
309				goto failed_read;
310			xattr_id = le32_to_cpu(xattr);
311		}
312
313		TRACE("Symbolic link inode %x:%x, start_block %llx, offset "
314				"%x\n", SQUASHFS_INODE_BLK(ino), offset,
315				block, offset);
316		break;
317	}
318	case SQUASHFS_BLKDEV_TYPE:
319	case SQUASHFS_CHRDEV_TYPE: {
320		struct squashfs_dev_inode *sqsh_ino = &squashfs_ino.dev;
321		unsigned int rdev;
322
323		err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
324				sizeof(*sqsh_ino));
325		if (err < 0)
326			goto failed_read;
327
328		if (type == SQUASHFS_CHRDEV_TYPE)
329			inode->i_mode |= S_IFCHR;
330		else
331			inode->i_mode |= S_IFBLK;
332		set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
333		rdev = le32_to_cpu(sqsh_ino->rdev);
334		init_special_inode(inode, inode->i_mode, new_decode_dev(rdev));
335
336		TRACE("Device inode %x:%x, rdev %x\n",
337				SQUASHFS_INODE_BLK(ino), offset, rdev);
338		break;
339	}
340	case SQUASHFS_LBLKDEV_TYPE:
341	case SQUASHFS_LCHRDEV_TYPE: {
342		struct squashfs_ldev_inode *sqsh_ino = &squashfs_ino.ldev;
343		unsigned int rdev;
344
345		err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
346				sizeof(*sqsh_ino));
347		if (err < 0)
348			goto failed_read;
349
350		if (type == SQUASHFS_LCHRDEV_TYPE)
351			inode->i_mode |= S_IFCHR;
352		else
353			inode->i_mode |= S_IFBLK;
354		xattr_id = le32_to_cpu(sqsh_ino->xattr);
355		inode->i_op = &squashfs_inode_ops;
356		set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
357		rdev = le32_to_cpu(sqsh_ino->rdev);
358		init_special_inode(inode, inode->i_mode, new_decode_dev(rdev));
359
360		TRACE("Device inode %x:%x, rdev %x\n",
361				SQUASHFS_INODE_BLK(ino), offset, rdev);
362		break;
363	}
364	case SQUASHFS_FIFO_TYPE:
365	case SQUASHFS_SOCKET_TYPE: {
366		struct squashfs_ipc_inode *sqsh_ino = &squashfs_ino.ipc;
367
368		err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
369				sizeof(*sqsh_ino));
370		if (err < 0)
371			goto failed_read;
372
373		if (type == SQUASHFS_FIFO_TYPE)
374			inode->i_mode |= S_IFIFO;
375		else
376			inode->i_mode |= S_IFSOCK;
377		set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
378		init_special_inode(inode, inode->i_mode, 0);
379		break;
380	}
381	case SQUASHFS_LFIFO_TYPE:
382	case SQUASHFS_LSOCKET_TYPE: {
383		struct squashfs_lipc_inode *sqsh_ino = &squashfs_ino.lipc;
384
385		err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
386				sizeof(*sqsh_ino));
387		if (err < 0)
388			goto failed_read;
389
390		if (type == SQUASHFS_LFIFO_TYPE)
391			inode->i_mode |= S_IFIFO;
392		else
393			inode->i_mode |= S_IFSOCK;
394		xattr_id = le32_to_cpu(sqsh_ino->xattr);
395		inode->i_op = &squashfs_inode_ops;
396		set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
397		init_special_inode(inode, inode->i_mode, 0);
398		break;
399	}
400	default:
401		ERROR("Unknown inode type %d in squashfs_iget!\n", type);
402		return -EINVAL;
403	}
404
405	if (xattr_id != SQUASHFS_INVALID_XATTR && msblk->xattr_id_table) {
406		err = squashfs_xattr_lookup(sb, xattr_id,
407					&squashfs_i(inode)->xattr_count,
408					&squashfs_i(inode)->xattr_size,
409					&squashfs_i(inode)->xattr);
410		if (err < 0)
411			goto failed_read;
412		inode->i_blocks += ((squashfs_i(inode)->xattr_size - 1) >> 9)
413				+ 1;
414	} else
415		squashfs_i(inode)->xattr_count = 0;
416
417	return 0;
418
419failed_read:
420	ERROR("Unable to read inode 0x%llx\n", ino);
421	return err;
422}
423
424
425const struct inode_operations squashfs_inode_ops = {
426	.getxattr = generic_getxattr,
427	.listxattr = squashfs_listxattr
428};
429
430