xref: /linux/fs/squashfs/inode.c (revision c25822ccaa40fbc8ec38510397e0fbb8b1b0c0e2)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Squashfs - a compressed read only filesystem for Linux
4  *
5  * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
6  * Phillip Lougher <phillip@squashfs.org.uk>
7  *
8  * inode.c
9  */
10 
11 /*
12  * This file implements code to create and read inodes from disk.
13  *
14  * Inodes in Squashfs are identified by a 48-bit inode which encodes the
15  * location of the compressed metadata block containing the inode, and the byte
16  * offset into that block where the inode is placed (<block, offset>).
17  *
18  * To maximise compression there are different inodes for each file type
19  * (regular file, directory, device, etc.), the inode contents and length
20  * varying with the type.
21  *
22  * To further maximise compression, two types of regular file inode and
23  * directory inode are defined: inodes optimised for frequently occurring
24  * regular files and directories, and extended types where extra
25  * information has to be stored.
26  */
27 
28 #include <linux/fs.h>
29 #include <linux/vfs.h>
30 #include <linux/xattr.h>
31 #include <linux/pagemap.h>
32 
33 #include "squashfs_fs.h"
34 #include "squashfs_fs_sb.h"
35 #include "squashfs_fs_i.h"
36 #include "squashfs.h"
37 #include "xattr.h"
38 
39 /*
40  * Initialise VFS inode with the base inode information common to all
41  * Squashfs inode types.  Sqsh_ino contains the unswapped base inode
42  * off disk.
43  */
44 static int squashfs_new_inode(struct super_block *sb, struct inode *inode,
45 				struct squashfs_base_inode *sqsh_ino)
46 {
47 	uid_t i_uid;
48 	gid_t i_gid;
49 	int err;
50 
51 	inode->i_ino = le32_to_cpu(sqsh_ino->inode_number);
52 	if (inode->i_ino == 0)
53 		return -EINVAL;
54 
55 	err = squashfs_get_id(sb, le16_to_cpu(sqsh_ino->uid), &i_uid);
56 	if (err)
57 		return err;
58 
59 	err = squashfs_get_id(sb, le16_to_cpu(sqsh_ino->guid), &i_gid);
60 	if (err)
61 		return err;
62 
63 	i_uid_write(inode, i_uid);
64 	i_gid_write(inode, i_gid);
65 	inode_set_mtime(inode, le32_to_cpu(sqsh_ino->mtime), 0);
66 	inode_set_atime(inode, inode_get_mtime_sec(inode), 0);
67 	inode_set_ctime(inode, inode_get_mtime_sec(inode), 0);
68 	inode->i_mode = le16_to_cpu(sqsh_ino->mode);
69 	inode->i_size = 0;
70 
71 	/* File type must not be set at this moment, for it will later be set by the caller. */
72 	if (inode->i_mode & S_IFMT)
73 		err = -EIO;
74 
75 	return err;
76 }
77 
78 
79 struct inode *squashfs_iget(struct super_block *sb, long long ino,
80 				unsigned int ino_number)
81 {
82 	struct inode *inode = iget_locked(sb, ino_number);
83 	int err;
84 
85 	TRACE("Entered squashfs_iget\n");
86 
87 	if (!inode)
88 		return ERR_PTR(-ENOMEM);
89 	if (!(inode->i_state & I_NEW))
90 		return inode;
91 
92 	err = squashfs_read_inode(inode, ino);
93 	if (err) {
94 		iget_failed(inode);
95 		return ERR_PTR(err);
96 	}
97 
98 	unlock_new_inode(inode);
99 	return inode;
100 }
101 
102 
103 /*
104  * Initialise VFS inode by reading inode from inode table (compressed
105  * metadata).  The format and amount of data read depends on type.
106  */
107 int squashfs_read_inode(struct inode *inode, long long ino)
108 {
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;
116 
117 	TRACE("Entered squashfs_read_inode\n");
118 
119 	/*
120 	 * Read inode base common to all inode types.
121 	 */
122 	err = squashfs_read_metadata(sb, sqshb_ino, &block,
123 				&offset, sizeof(*sqshb_ino));
124 	if (err < 0)
125 		goto failed_read;
126 
127 	err = squashfs_new_inode(sb, inode, sqshb_ino);
128 	if (err)
129 		goto failed_read;
130 
131 	block = SQUASHFS_INODE_BLK(ino) + msblk->inode_table;
132 	offset = SQUASHFS_INODE_OFFSET(ino);
133 
134 	type = le16_to_cpu(sqshb_ino->inode_type);
135 	switch (type) {
136 	case SQUASHFS_REG_TYPE: {
137 		unsigned int frag_offset, frag;
138 		int frag_size;
139 		u64 frag_blk;
140 		struct squashfs_reg_inode *sqsh_ino = &squashfs_ino.reg;
141 
142 		err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
143 							sizeof(*sqsh_ino));
144 		if (err < 0)
145 			goto failed_read;
146 
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);
151 			if (frag_size < 0) {
152 				err = frag_size;
153 				goto failed_read;
154 			}
155 		} else {
156 			frag_blk = SQUASHFS_INVALID_BLK;
157 			frag_size = 0;
158 			frag_offset = 0;
159 		}
160 
161 		set_nlink(inode, 1);
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;
173 
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);
177 		break;
178 	}
179 	case SQUASHFS_LREG_TYPE: {
180 		unsigned int frag_offset, frag;
181 		int frag_size;
182 		u64 frag_blk;
183 		struct squashfs_lreg_inode *sqsh_ino = &squashfs_ino.lreg;
184 
185 		err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
186 							sizeof(*sqsh_ino));
187 		if (err < 0)
188 			goto failed_read;
189 
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);
194 			if (frag_size < 0) {
195 				err = frag_size;
196 				goto failed_read;
197 			}
198 		} else {
199 			frag_blk = SQUASHFS_INVALID_BLK;
200 			frag_size = 0;
201 			frag_offset = 0;
202 		}
203 
204 		xattr_id = le32_to_cpu(sqsh_ino->xattr);
205 		set_nlink(inode, 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) + 511) >> 9;
212 
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;
220 
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);
224 		break;
225 	}
226 	case SQUASHFS_DIR_TYPE: {
227 		struct squashfs_dir_inode *sqsh_ino = &squashfs_ino.dir;
228 
229 		err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
230 				sizeof(*sqsh_ino));
231 		if (err < 0)
232 			goto failed_read;
233 
234 		set_nlink(inode, 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);
243 
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));
248 		break;
249 	}
250 	case SQUASHFS_LDIR_TYPE: {
251 		struct squashfs_ldir_inode *sqsh_ino = &squashfs_ino.ldir;
252 
253 		err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
254 				sizeof(*sqsh_ino));
255 		if (err < 0)
256 			goto failed_read;
257 
258 		xattr_id = le32_to_cpu(sqsh_ino->xattr);
259 		set_nlink(inode, 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);
270 
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));
275 		break;
276 	}
277 	case SQUASHFS_SYMLINK_TYPE:
278 	case SQUASHFS_LSYMLINK_TYPE: {
279 		struct squashfs_symlink_inode *sqsh_ino = &squashfs_ino.symlink;
280 
281 		err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
282 				sizeof(*sqsh_ino));
283 		if (err < 0)
284 			goto failed_read;
285 
286 		inode->i_size = le32_to_cpu(sqsh_ino->symlink_size);
287 		if (inode->i_size > PAGE_SIZE) {
288 			ERROR("Corrupted symlink\n");
289 			return -EINVAL;
290 		}
291 
292 		set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
293 		inode->i_op = &squashfs_symlink_inode_ops;
294 		inode_nohighmem(inode);
295 		inode->i_data.a_ops = &squashfs_symlink_aops;
296 		inode->i_mode |= S_IFLNK;
297 		squashfs_i(inode)->start = block;
298 		squashfs_i(inode)->offset = offset;
299 
300 		if (type == SQUASHFS_LSYMLINK_TYPE) {
301 			__le32 xattr;
302 
303 			err = squashfs_read_metadata(sb, NULL, &block,
304 						&offset, inode->i_size);
305 			if (err < 0)
306 				goto failed_read;
307 			err = squashfs_read_metadata(sb, &xattr, &block,
308 						&offset, sizeof(xattr));
309 			if (err < 0)
310 				goto failed_read;
311 			xattr_id = le32_to_cpu(xattr);
312 		}
313 
314 		TRACE("Symbolic link inode %x:%x, start_block %llx, offset "
315 				"%x\n", SQUASHFS_INODE_BLK(ino), offset,
316 				block, offset);
317 		break;
318 	}
319 	case SQUASHFS_BLKDEV_TYPE:
320 	case SQUASHFS_CHRDEV_TYPE: {
321 		struct squashfs_dev_inode *sqsh_ino = &squashfs_ino.dev;
322 		unsigned int rdev;
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_CHRDEV_TYPE)
330 			inode->i_mode |= S_IFCHR;
331 		else
332 			inode->i_mode |= S_IFBLK;
333 		set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
334 		rdev = le32_to_cpu(sqsh_ino->rdev);
335 		init_special_inode(inode, inode->i_mode, new_decode_dev(rdev));
336 
337 		TRACE("Device inode %x:%x, rdev %x\n",
338 				SQUASHFS_INODE_BLK(ino), offset, rdev);
339 		break;
340 	}
341 	case SQUASHFS_LBLKDEV_TYPE:
342 	case SQUASHFS_LCHRDEV_TYPE: {
343 		struct squashfs_ldev_inode *sqsh_ino = &squashfs_ino.ldev;
344 		unsigned int rdev;
345 
346 		err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
347 				sizeof(*sqsh_ino));
348 		if (err < 0)
349 			goto failed_read;
350 
351 		if (type == SQUASHFS_LCHRDEV_TYPE)
352 			inode->i_mode |= S_IFCHR;
353 		else
354 			inode->i_mode |= S_IFBLK;
355 		xattr_id = le32_to_cpu(sqsh_ino->xattr);
356 		inode->i_op = &squashfs_inode_ops;
357 		set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
358 		rdev = le32_to_cpu(sqsh_ino->rdev);
359 		init_special_inode(inode, inode->i_mode, new_decode_dev(rdev));
360 
361 		TRACE("Device inode %x:%x, rdev %x\n",
362 				SQUASHFS_INODE_BLK(ino), offset, rdev);
363 		break;
364 	}
365 	case SQUASHFS_FIFO_TYPE:
366 	case SQUASHFS_SOCKET_TYPE: {
367 		struct squashfs_ipc_inode *sqsh_ino = &squashfs_ino.ipc;
368 
369 		err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
370 				sizeof(*sqsh_ino));
371 		if (err < 0)
372 			goto failed_read;
373 
374 		if (type == SQUASHFS_FIFO_TYPE)
375 			inode->i_mode |= S_IFIFO;
376 		else
377 			inode->i_mode |= S_IFSOCK;
378 		set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
379 		init_special_inode(inode, inode->i_mode, 0);
380 		break;
381 	}
382 	case SQUASHFS_LFIFO_TYPE:
383 	case SQUASHFS_LSOCKET_TYPE: {
384 		struct squashfs_lipc_inode *sqsh_ino = &squashfs_ino.lipc;
385 
386 		err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
387 				sizeof(*sqsh_ino));
388 		if (err < 0)
389 			goto failed_read;
390 
391 		if (type == SQUASHFS_LFIFO_TYPE)
392 			inode->i_mode |= S_IFIFO;
393 		else
394 			inode->i_mode |= S_IFSOCK;
395 		xattr_id = le32_to_cpu(sqsh_ino->xattr);
396 		inode->i_op = &squashfs_inode_ops;
397 		set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
398 		init_special_inode(inode, inode->i_mode, 0);
399 		break;
400 	}
401 	default:
402 		ERROR("Unknown inode type %d in squashfs_iget!\n", type);
403 		return -EINVAL;
404 	}
405 
406 	if (xattr_id != SQUASHFS_INVALID_XATTR && msblk->xattr_id_table) {
407 		err = squashfs_xattr_lookup(sb, xattr_id,
408 					&squashfs_i(inode)->xattr_count,
409 					&squashfs_i(inode)->xattr_size,
410 					&squashfs_i(inode)->xattr);
411 		if (err < 0)
412 			goto failed_read;
413 		inode->i_blocks += ((squashfs_i(inode)->xattr_size - 1) >> 9)
414 				+ 1;
415 	} else
416 		squashfs_i(inode)->xattr_count = 0;
417 
418 	return 0;
419 
420 failed_read:
421 	ERROR("Unable to read inode 0x%llx\n", ino);
422 	return err;
423 }
424 
425 
426 const struct inode_operations squashfs_inode_ops = {
427 	.listxattr = squashfs_listxattr
428 };
429 
430