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