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