162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Squashfs - a compressed read only filesystem for Linux
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
662306a36Sopenharmony_ci * Phillip Lougher <phillip@squashfs.org.uk>
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci * inode.c
962306a36Sopenharmony_ci */
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci/*
1262306a36Sopenharmony_ci * This file implements code to create and read inodes from disk.
1362306a36Sopenharmony_ci *
1462306a36Sopenharmony_ci * Inodes in Squashfs are identified by a 48-bit inode which encodes the
1562306a36Sopenharmony_ci * location of the compressed metadata block containing the inode, and the byte
1662306a36Sopenharmony_ci * offset into that block where the inode is placed (<block, offset>).
1762306a36Sopenharmony_ci *
1862306a36Sopenharmony_ci * To maximise compression there are different inodes for each file type
1962306a36Sopenharmony_ci * (regular file, directory, device, etc.), the inode contents and length
2062306a36Sopenharmony_ci * varying with the type.
2162306a36Sopenharmony_ci *
2262306a36Sopenharmony_ci * To further maximise compression, two types of regular file inode and
2362306a36Sopenharmony_ci * directory inode are defined: inodes optimised for frequently occurring
2462306a36Sopenharmony_ci * regular files and directories, and extended types where extra
2562306a36Sopenharmony_ci * information has to be stored.
2662306a36Sopenharmony_ci */
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci#include <linux/fs.h>
2962306a36Sopenharmony_ci#include <linux/vfs.h>
3062306a36Sopenharmony_ci#include <linux/xattr.h>
3162306a36Sopenharmony_ci#include <linux/pagemap.h>
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci#include "squashfs_fs.h"
3462306a36Sopenharmony_ci#include "squashfs_fs_sb.h"
3562306a36Sopenharmony_ci#include "squashfs_fs_i.h"
3662306a36Sopenharmony_ci#include "squashfs.h"
3762306a36Sopenharmony_ci#include "xattr.h"
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci/*
4062306a36Sopenharmony_ci * Initialise VFS inode with the base inode information common to all
4162306a36Sopenharmony_ci * Squashfs inode types.  Sqsh_ino contains the unswapped base inode
4262306a36Sopenharmony_ci * off disk.
4362306a36Sopenharmony_ci */
4462306a36Sopenharmony_cistatic int squashfs_new_inode(struct super_block *sb, struct inode *inode,
4562306a36Sopenharmony_ci				struct squashfs_base_inode *sqsh_ino)
4662306a36Sopenharmony_ci{
4762306a36Sopenharmony_ci	uid_t i_uid;
4862306a36Sopenharmony_ci	gid_t i_gid;
4962306a36Sopenharmony_ci	int err;
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci	err = squashfs_get_id(sb, le16_to_cpu(sqsh_ino->uid), &i_uid);
5262306a36Sopenharmony_ci	if (err)
5362306a36Sopenharmony_ci		return err;
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci	err = squashfs_get_id(sb, le16_to_cpu(sqsh_ino->guid), &i_gid);
5662306a36Sopenharmony_ci	if (err)
5762306a36Sopenharmony_ci		return err;
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci	i_uid_write(inode, i_uid);
6062306a36Sopenharmony_ci	i_gid_write(inode, i_gid);
6162306a36Sopenharmony_ci	inode->i_ino = le32_to_cpu(sqsh_ino->inode_number);
6262306a36Sopenharmony_ci	inode->i_mtime.tv_sec = le32_to_cpu(sqsh_ino->mtime);
6362306a36Sopenharmony_ci	inode->i_atime.tv_sec = inode->i_mtime.tv_sec;
6462306a36Sopenharmony_ci	inode_set_ctime(inode, inode->i_mtime.tv_sec, 0);
6562306a36Sopenharmony_ci	inode->i_mode = le16_to_cpu(sqsh_ino->mode);
6662306a36Sopenharmony_ci	inode->i_size = 0;
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci	return err;
6962306a36Sopenharmony_ci}
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_cistruct inode *squashfs_iget(struct super_block *sb, long long ino,
7362306a36Sopenharmony_ci				unsigned int ino_number)
7462306a36Sopenharmony_ci{
7562306a36Sopenharmony_ci	struct inode *inode = iget_locked(sb, ino_number);
7662306a36Sopenharmony_ci	int err;
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ci	TRACE("Entered squashfs_iget\n");
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci	if (!inode)
8162306a36Sopenharmony_ci		return ERR_PTR(-ENOMEM);
8262306a36Sopenharmony_ci	if (!(inode->i_state & I_NEW))
8362306a36Sopenharmony_ci		return inode;
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci	err = squashfs_read_inode(inode, ino);
8662306a36Sopenharmony_ci	if (err) {
8762306a36Sopenharmony_ci		iget_failed(inode);
8862306a36Sopenharmony_ci		return ERR_PTR(err);
8962306a36Sopenharmony_ci	}
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci	unlock_new_inode(inode);
9262306a36Sopenharmony_ci	return inode;
9362306a36Sopenharmony_ci}
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci/*
9762306a36Sopenharmony_ci * Initialise VFS inode by reading inode from inode table (compressed
9862306a36Sopenharmony_ci * metadata).  The format and amount of data read depends on type.
9962306a36Sopenharmony_ci */
10062306a36Sopenharmony_ciint squashfs_read_inode(struct inode *inode, long long ino)
10162306a36Sopenharmony_ci{
10262306a36Sopenharmony_ci	struct super_block *sb = inode->i_sb;
10362306a36Sopenharmony_ci	struct squashfs_sb_info *msblk = sb->s_fs_info;
10462306a36Sopenharmony_ci	u64 block = SQUASHFS_INODE_BLK(ino) + msblk->inode_table;
10562306a36Sopenharmony_ci	int err, type, offset = SQUASHFS_INODE_OFFSET(ino);
10662306a36Sopenharmony_ci	union squashfs_inode squashfs_ino;
10762306a36Sopenharmony_ci	struct squashfs_base_inode *sqshb_ino = &squashfs_ino.base;
10862306a36Sopenharmony_ci	int xattr_id = SQUASHFS_INVALID_XATTR;
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci	TRACE("Entered squashfs_read_inode\n");
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci	/*
11362306a36Sopenharmony_ci	 * Read inode base common to all inode types.
11462306a36Sopenharmony_ci	 */
11562306a36Sopenharmony_ci	err = squashfs_read_metadata(sb, sqshb_ino, &block,
11662306a36Sopenharmony_ci				&offset, sizeof(*sqshb_ino));
11762306a36Sopenharmony_ci	if (err < 0)
11862306a36Sopenharmony_ci		goto failed_read;
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ci	err = squashfs_new_inode(sb, inode, sqshb_ino);
12162306a36Sopenharmony_ci	if (err)
12262306a36Sopenharmony_ci		goto failed_read;
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci	block = SQUASHFS_INODE_BLK(ino) + msblk->inode_table;
12562306a36Sopenharmony_ci	offset = SQUASHFS_INODE_OFFSET(ino);
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci	type = le16_to_cpu(sqshb_ino->inode_type);
12862306a36Sopenharmony_ci	switch (type) {
12962306a36Sopenharmony_ci	case SQUASHFS_REG_TYPE: {
13062306a36Sopenharmony_ci		unsigned int frag_offset, frag;
13162306a36Sopenharmony_ci		int frag_size;
13262306a36Sopenharmony_ci		u64 frag_blk;
13362306a36Sopenharmony_ci		struct squashfs_reg_inode *sqsh_ino = &squashfs_ino.reg;
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci		err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
13662306a36Sopenharmony_ci							sizeof(*sqsh_ino));
13762306a36Sopenharmony_ci		if (err < 0)
13862306a36Sopenharmony_ci			goto failed_read;
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci		frag = le32_to_cpu(sqsh_ino->fragment);
14162306a36Sopenharmony_ci		if (frag != SQUASHFS_INVALID_FRAG) {
14262306a36Sopenharmony_ci			frag_offset = le32_to_cpu(sqsh_ino->offset);
14362306a36Sopenharmony_ci			frag_size = squashfs_frag_lookup(sb, frag, &frag_blk);
14462306a36Sopenharmony_ci			if (frag_size < 0) {
14562306a36Sopenharmony_ci				err = frag_size;
14662306a36Sopenharmony_ci				goto failed_read;
14762306a36Sopenharmony_ci			}
14862306a36Sopenharmony_ci		} else {
14962306a36Sopenharmony_ci			frag_blk = SQUASHFS_INVALID_BLK;
15062306a36Sopenharmony_ci			frag_size = 0;
15162306a36Sopenharmony_ci			frag_offset = 0;
15262306a36Sopenharmony_ci		}
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_ci		set_nlink(inode, 1);
15562306a36Sopenharmony_ci		inode->i_size = le32_to_cpu(sqsh_ino->file_size);
15662306a36Sopenharmony_ci		inode->i_fop = &generic_ro_fops;
15762306a36Sopenharmony_ci		inode->i_mode |= S_IFREG;
15862306a36Sopenharmony_ci		inode->i_blocks = ((inode->i_size - 1) >> 9) + 1;
15962306a36Sopenharmony_ci		squashfs_i(inode)->fragment_block = frag_blk;
16062306a36Sopenharmony_ci		squashfs_i(inode)->fragment_size = frag_size;
16162306a36Sopenharmony_ci		squashfs_i(inode)->fragment_offset = frag_offset;
16262306a36Sopenharmony_ci		squashfs_i(inode)->start = le32_to_cpu(sqsh_ino->start_block);
16362306a36Sopenharmony_ci		squashfs_i(inode)->block_list_start = block;
16462306a36Sopenharmony_ci		squashfs_i(inode)->offset = offset;
16562306a36Sopenharmony_ci		inode->i_data.a_ops = &squashfs_aops;
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ci		TRACE("File inode %x:%x, start_block %llx, block_list_start "
16862306a36Sopenharmony_ci			"%llx, offset %x\n", SQUASHFS_INODE_BLK(ino),
16962306a36Sopenharmony_ci			offset, squashfs_i(inode)->start, block, offset);
17062306a36Sopenharmony_ci		break;
17162306a36Sopenharmony_ci	}
17262306a36Sopenharmony_ci	case SQUASHFS_LREG_TYPE: {
17362306a36Sopenharmony_ci		unsigned int frag_offset, frag;
17462306a36Sopenharmony_ci		int frag_size;
17562306a36Sopenharmony_ci		u64 frag_blk;
17662306a36Sopenharmony_ci		struct squashfs_lreg_inode *sqsh_ino = &squashfs_ino.lreg;
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ci		err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
17962306a36Sopenharmony_ci							sizeof(*sqsh_ino));
18062306a36Sopenharmony_ci		if (err < 0)
18162306a36Sopenharmony_ci			goto failed_read;
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_ci		frag = le32_to_cpu(sqsh_ino->fragment);
18462306a36Sopenharmony_ci		if (frag != SQUASHFS_INVALID_FRAG) {
18562306a36Sopenharmony_ci			frag_offset = le32_to_cpu(sqsh_ino->offset);
18662306a36Sopenharmony_ci			frag_size = squashfs_frag_lookup(sb, frag, &frag_blk);
18762306a36Sopenharmony_ci			if (frag_size < 0) {
18862306a36Sopenharmony_ci				err = frag_size;
18962306a36Sopenharmony_ci				goto failed_read;
19062306a36Sopenharmony_ci			}
19162306a36Sopenharmony_ci		} else {
19262306a36Sopenharmony_ci			frag_blk = SQUASHFS_INVALID_BLK;
19362306a36Sopenharmony_ci			frag_size = 0;
19462306a36Sopenharmony_ci			frag_offset = 0;
19562306a36Sopenharmony_ci		}
19662306a36Sopenharmony_ci
19762306a36Sopenharmony_ci		xattr_id = le32_to_cpu(sqsh_ino->xattr);
19862306a36Sopenharmony_ci		set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
19962306a36Sopenharmony_ci		inode->i_size = le64_to_cpu(sqsh_ino->file_size);
20062306a36Sopenharmony_ci		inode->i_op = &squashfs_inode_ops;
20162306a36Sopenharmony_ci		inode->i_fop = &generic_ro_fops;
20262306a36Sopenharmony_ci		inode->i_mode |= S_IFREG;
20362306a36Sopenharmony_ci		inode->i_blocks = (inode->i_size -
20462306a36Sopenharmony_ci				le64_to_cpu(sqsh_ino->sparse) + 511) >> 9;
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_ci		squashfs_i(inode)->fragment_block = frag_blk;
20762306a36Sopenharmony_ci		squashfs_i(inode)->fragment_size = frag_size;
20862306a36Sopenharmony_ci		squashfs_i(inode)->fragment_offset = frag_offset;
20962306a36Sopenharmony_ci		squashfs_i(inode)->start = le64_to_cpu(sqsh_ino->start_block);
21062306a36Sopenharmony_ci		squashfs_i(inode)->block_list_start = block;
21162306a36Sopenharmony_ci		squashfs_i(inode)->offset = offset;
21262306a36Sopenharmony_ci		inode->i_data.a_ops = &squashfs_aops;
21362306a36Sopenharmony_ci
21462306a36Sopenharmony_ci		TRACE("File inode %x:%x, start_block %llx, block_list_start "
21562306a36Sopenharmony_ci			"%llx, offset %x\n", SQUASHFS_INODE_BLK(ino),
21662306a36Sopenharmony_ci			offset, squashfs_i(inode)->start, block, offset);
21762306a36Sopenharmony_ci		break;
21862306a36Sopenharmony_ci	}
21962306a36Sopenharmony_ci	case SQUASHFS_DIR_TYPE: {
22062306a36Sopenharmony_ci		struct squashfs_dir_inode *sqsh_ino = &squashfs_ino.dir;
22162306a36Sopenharmony_ci
22262306a36Sopenharmony_ci		err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
22362306a36Sopenharmony_ci				sizeof(*sqsh_ino));
22462306a36Sopenharmony_ci		if (err < 0)
22562306a36Sopenharmony_ci			goto failed_read;
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_ci		set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
22862306a36Sopenharmony_ci		inode->i_size = le16_to_cpu(sqsh_ino->file_size);
22962306a36Sopenharmony_ci		inode->i_op = &squashfs_dir_inode_ops;
23062306a36Sopenharmony_ci		inode->i_fop = &squashfs_dir_ops;
23162306a36Sopenharmony_ci		inode->i_mode |= S_IFDIR;
23262306a36Sopenharmony_ci		squashfs_i(inode)->start = le32_to_cpu(sqsh_ino->start_block);
23362306a36Sopenharmony_ci		squashfs_i(inode)->offset = le16_to_cpu(sqsh_ino->offset);
23462306a36Sopenharmony_ci		squashfs_i(inode)->dir_idx_cnt = 0;
23562306a36Sopenharmony_ci		squashfs_i(inode)->parent = le32_to_cpu(sqsh_ino->parent_inode);
23662306a36Sopenharmony_ci
23762306a36Sopenharmony_ci		TRACE("Directory inode %x:%x, start_block %llx, offset %x\n",
23862306a36Sopenharmony_ci				SQUASHFS_INODE_BLK(ino), offset,
23962306a36Sopenharmony_ci				squashfs_i(inode)->start,
24062306a36Sopenharmony_ci				le16_to_cpu(sqsh_ino->offset));
24162306a36Sopenharmony_ci		break;
24262306a36Sopenharmony_ci	}
24362306a36Sopenharmony_ci	case SQUASHFS_LDIR_TYPE: {
24462306a36Sopenharmony_ci		struct squashfs_ldir_inode *sqsh_ino = &squashfs_ino.ldir;
24562306a36Sopenharmony_ci
24662306a36Sopenharmony_ci		err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
24762306a36Sopenharmony_ci				sizeof(*sqsh_ino));
24862306a36Sopenharmony_ci		if (err < 0)
24962306a36Sopenharmony_ci			goto failed_read;
25062306a36Sopenharmony_ci
25162306a36Sopenharmony_ci		xattr_id = le32_to_cpu(sqsh_ino->xattr);
25262306a36Sopenharmony_ci		set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
25362306a36Sopenharmony_ci		inode->i_size = le32_to_cpu(sqsh_ino->file_size);
25462306a36Sopenharmony_ci		inode->i_op = &squashfs_dir_inode_ops;
25562306a36Sopenharmony_ci		inode->i_fop = &squashfs_dir_ops;
25662306a36Sopenharmony_ci		inode->i_mode |= S_IFDIR;
25762306a36Sopenharmony_ci		squashfs_i(inode)->start = le32_to_cpu(sqsh_ino->start_block);
25862306a36Sopenharmony_ci		squashfs_i(inode)->offset = le16_to_cpu(sqsh_ino->offset);
25962306a36Sopenharmony_ci		squashfs_i(inode)->dir_idx_start = block;
26062306a36Sopenharmony_ci		squashfs_i(inode)->dir_idx_offset = offset;
26162306a36Sopenharmony_ci		squashfs_i(inode)->dir_idx_cnt = le16_to_cpu(sqsh_ino->i_count);
26262306a36Sopenharmony_ci		squashfs_i(inode)->parent = le32_to_cpu(sqsh_ino->parent_inode);
26362306a36Sopenharmony_ci
26462306a36Sopenharmony_ci		TRACE("Long directory inode %x:%x, start_block %llx, offset "
26562306a36Sopenharmony_ci				"%x\n", SQUASHFS_INODE_BLK(ino), offset,
26662306a36Sopenharmony_ci				squashfs_i(inode)->start,
26762306a36Sopenharmony_ci				le16_to_cpu(sqsh_ino->offset));
26862306a36Sopenharmony_ci		break;
26962306a36Sopenharmony_ci	}
27062306a36Sopenharmony_ci	case SQUASHFS_SYMLINK_TYPE:
27162306a36Sopenharmony_ci	case SQUASHFS_LSYMLINK_TYPE: {
27262306a36Sopenharmony_ci		struct squashfs_symlink_inode *sqsh_ino = &squashfs_ino.symlink;
27362306a36Sopenharmony_ci
27462306a36Sopenharmony_ci		err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
27562306a36Sopenharmony_ci				sizeof(*sqsh_ino));
27662306a36Sopenharmony_ci		if (err < 0)
27762306a36Sopenharmony_ci			goto failed_read;
27862306a36Sopenharmony_ci
27962306a36Sopenharmony_ci		set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
28062306a36Sopenharmony_ci		inode->i_size = le32_to_cpu(sqsh_ino->symlink_size);
28162306a36Sopenharmony_ci		inode->i_op = &squashfs_symlink_inode_ops;
28262306a36Sopenharmony_ci		inode_nohighmem(inode);
28362306a36Sopenharmony_ci		inode->i_data.a_ops = &squashfs_symlink_aops;
28462306a36Sopenharmony_ci		inode->i_mode |= S_IFLNK;
28562306a36Sopenharmony_ci		squashfs_i(inode)->start = block;
28662306a36Sopenharmony_ci		squashfs_i(inode)->offset = offset;
28762306a36Sopenharmony_ci
28862306a36Sopenharmony_ci		if (type == SQUASHFS_LSYMLINK_TYPE) {
28962306a36Sopenharmony_ci			__le32 xattr;
29062306a36Sopenharmony_ci
29162306a36Sopenharmony_ci			err = squashfs_read_metadata(sb, NULL, &block,
29262306a36Sopenharmony_ci						&offset, inode->i_size);
29362306a36Sopenharmony_ci			if (err < 0)
29462306a36Sopenharmony_ci				goto failed_read;
29562306a36Sopenharmony_ci			err = squashfs_read_metadata(sb, &xattr, &block,
29662306a36Sopenharmony_ci						&offset, sizeof(xattr));
29762306a36Sopenharmony_ci			if (err < 0)
29862306a36Sopenharmony_ci				goto failed_read;
29962306a36Sopenharmony_ci			xattr_id = le32_to_cpu(xattr);
30062306a36Sopenharmony_ci		}
30162306a36Sopenharmony_ci
30262306a36Sopenharmony_ci		TRACE("Symbolic link inode %x:%x, start_block %llx, offset "
30362306a36Sopenharmony_ci				"%x\n", SQUASHFS_INODE_BLK(ino), offset,
30462306a36Sopenharmony_ci				block, offset);
30562306a36Sopenharmony_ci		break;
30662306a36Sopenharmony_ci	}
30762306a36Sopenharmony_ci	case SQUASHFS_BLKDEV_TYPE:
30862306a36Sopenharmony_ci	case SQUASHFS_CHRDEV_TYPE: {
30962306a36Sopenharmony_ci		struct squashfs_dev_inode *sqsh_ino = &squashfs_ino.dev;
31062306a36Sopenharmony_ci		unsigned int rdev;
31162306a36Sopenharmony_ci
31262306a36Sopenharmony_ci		err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
31362306a36Sopenharmony_ci				sizeof(*sqsh_ino));
31462306a36Sopenharmony_ci		if (err < 0)
31562306a36Sopenharmony_ci			goto failed_read;
31662306a36Sopenharmony_ci
31762306a36Sopenharmony_ci		if (type == SQUASHFS_CHRDEV_TYPE)
31862306a36Sopenharmony_ci			inode->i_mode |= S_IFCHR;
31962306a36Sopenharmony_ci		else
32062306a36Sopenharmony_ci			inode->i_mode |= S_IFBLK;
32162306a36Sopenharmony_ci		set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
32262306a36Sopenharmony_ci		rdev = le32_to_cpu(sqsh_ino->rdev);
32362306a36Sopenharmony_ci		init_special_inode(inode, inode->i_mode, new_decode_dev(rdev));
32462306a36Sopenharmony_ci
32562306a36Sopenharmony_ci		TRACE("Device inode %x:%x, rdev %x\n",
32662306a36Sopenharmony_ci				SQUASHFS_INODE_BLK(ino), offset, rdev);
32762306a36Sopenharmony_ci		break;
32862306a36Sopenharmony_ci	}
32962306a36Sopenharmony_ci	case SQUASHFS_LBLKDEV_TYPE:
33062306a36Sopenharmony_ci	case SQUASHFS_LCHRDEV_TYPE: {
33162306a36Sopenharmony_ci		struct squashfs_ldev_inode *sqsh_ino = &squashfs_ino.ldev;
33262306a36Sopenharmony_ci		unsigned int rdev;
33362306a36Sopenharmony_ci
33462306a36Sopenharmony_ci		err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
33562306a36Sopenharmony_ci				sizeof(*sqsh_ino));
33662306a36Sopenharmony_ci		if (err < 0)
33762306a36Sopenharmony_ci			goto failed_read;
33862306a36Sopenharmony_ci
33962306a36Sopenharmony_ci		if (type == SQUASHFS_LCHRDEV_TYPE)
34062306a36Sopenharmony_ci			inode->i_mode |= S_IFCHR;
34162306a36Sopenharmony_ci		else
34262306a36Sopenharmony_ci			inode->i_mode |= S_IFBLK;
34362306a36Sopenharmony_ci		xattr_id = le32_to_cpu(sqsh_ino->xattr);
34462306a36Sopenharmony_ci		inode->i_op = &squashfs_inode_ops;
34562306a36Sopenharmony_ci		set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
34662306a36Sopenharmony_ci		rdev = le32_to_cpu(sqsh_ino->rdev);
34762306a36Sopenharmony_ci		init_special_inode(inode, inode->i_mode, new_decode_dev(rdev));
34862306a36Sopenharmony_ci
34962306a36Sopenharmony_ci		TRACE("Device inode %x:%x, rdev %x\n",
35062306a36Sopenharmony_ci				SQUASHFS_INODE_BLK(ino), offset, rdev);
35162306a36Sopenharmony_ci		break;
35262306a36Sopenharmony_ci	}
35362306a36Sopenharmony_ci	case SQUASHFS_FIFO_TYPE:
35462306a36Sopenharmony_ci	case SQUASHFS_SOCKET_TYPE: {
35562306a36Sopenharmony_ci		struct squashfs_ipc_inode *sqsh_ino = &squashfs_ino.ipc;
35662306a36Sopenharmony_ci
35762306a36Sopenharmony_ci		err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
35862306a36Sopenharmony_ci				sizeof(*sqsh_ino));
35962306a36Sopenharmony_ci		if (err < 0)
36062306a36Sopenharmony_ci			goto failed_read;
36162306a36Sopenharmony_ci
36262306a36Sopenharmony_ci		if (type == SQUASHFS_FIFO_TYPE)
36362306a36Sopenharmony_ci			inode->i_mode |= S_IFIFO;
36462306a36Sopenharmony_ci		else
36562306a36Sopenharmony_ci			inode->i_mode |= S_IFSOCK;
36662306a36Sopenharmony_ci		set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
36762306a36Sopenharmony_ci		init_special_inode(inode, inode->i_mode, 0);
36862306a36Sopenharmony_ci		break;
36962306a36Sopenharmony_ci	}
37062306a36Sopenharmony_ci	case SQUASHFS_LFIFO_TYPE:
37162306a36Sopenharmony_ci	case SQUASHFS_LSOCKET_TYPE: {
37262306a36Sopenharmony_ci		struct squashfs_lipc_inode *sqsh_ino = &squashfs_ino.lipc;
37362306a36Sopenharmony_ci
37462306a36Sopenharmony_ci		err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
37562306a36Sopenharmony_ci				sizeof(*sqsh_ino));
37662306a36Sopenharmony_ci		if (err < 0)
37762306a36Sopenharmony_ci			goto failed_read;
37862306a36Sopenharmony_ci
37962306a36Sopenharmony_ci		if (type == SQUASHFS_LFIFO_TYPE)
38062306a36Sopenharmony_ci			inode->i_mode |= S_IFIFO;
38162306a36Sopenharmony_ci		else
38262306a36Sopenharmony_ci			inode->i_mode |= S_IFSOCK;
38362306a36Sopenharmony_ci		xattr_id = le32_to_cpu(sqsh_ino->xattr);
38462306a36Sopenharmony_ci		inode->i_op = &squashfs_inode_ops;
38562306a36Sopenharmony_ci		set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
38662306a36Sopenharmony_ci		init_special_inode(inode, inode->i_mode, 0);
38762306a36Sopenharmony_ci		break;
38862306a36Sopenharmony_ci	}
38962306a36Sopenharmony_ci	default:
39062306a36Sopenharmony_ci		ERROR("Unknown inode type %d in squashfs_iget!\n", type);
39162306a36Sopenharmony_ci		return -EINVAL;
39262306a36Sopenharmony_ci	}
39362306a36Sopenharmony_ci
39462306a36Sopenharmony_ci	if (xattr_id != SQUASHFS_INVALID_XATTR && msblk->xattr_id_table) {
39562306a36Sopenharmony_ci		err = squashfs_xattr_lookup(sb, xattr_id,
39662306a36Sopenharmony_ci					&squashfs_i(inode)->xattr_count,
39762306a36Sopenharmony_ci					&squashfs_i(inode)->xattr_size,
39862306a36Sopenharmony_ci					&squashfs_i(inode)->xattr);
39962306a36Sopenharmony_ci		if (err < 0)
40062306a36Sopenharmony_ci			goto failed_read;
40162306a36Sopenharmony_ci		inode->i_blocks += ((squashfs_i(inode)->xattr_size - 1) >> 9)
40262306a36Sopenharmony_ci				+ 1;
40362306a36Sopenharmony_ci	} else
40462306a36Sopenharmony_ci		squashfs_i(inode)->xattr_count = 0;
40562306a36Sopenharmony_ci
40662306a36Sopenharmony_ci	return 0;
40762306a36Sopenharmony_ci
40862306a36Sopenharmony_cifailed_read:
40962306a36Sopenharmony_ci	ERROR("Unable to read inode 0x%llx\n", ino);
41062306a36Sopenharmony_ci	return err;
41162306a36Sopenharmony_ci}
41262306a36Sopenharmony_ci
41362306a36Sopenharmony_ci
41462306a36Sopenharmony_ciconst struct inode_operations squashfs_inode_ops = {
41562306a36Sopenharmony_ci	.listxattr = squashfs_listxattr
41662306a36Sopenharmony_ci};
41762306a36Sopenharmony_ci
418