^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) * dir.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 read directories from disk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * See namei.c for a description of directory organisation on disk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/vfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include "squashfs_fs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include "squashfs_fs_sb.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include "squashfs_fs_i.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include "squashfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static const unsigned char squashfs_filetype_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) DT_UNKNOWN, DT_DIR, DT_REG, DT_LNK, DT_BLK, DT_CHR, DT_FIFO, DT_SOCK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * Lookup offset (f_pos) in the directory index, returning the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * metadata block containing it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * If we get an error reading the index then return the part of the index
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * (if any) we have managed to read - the index isn't essential, just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * quicker.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static int get_dir_index_using_offset(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) u64 *next_block, int *next_offset, u64 index_start, int index_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) int i_count, u64 f_pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct squashfs_sb_info *msblk = sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) int err, i, index, length = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) unsigned int size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct squashfs_dir_index dir_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) TRACE("Entered get_dir_index_using_offset, i_count %d, f_pos %lld\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) i_count, f_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * Translate from external f_pos to the internal f_pos. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * is offset by 3 because we invent "." and ".." entries which are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * not actually stored in the directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (f_pos <= 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return f_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) f_pos -= 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) for (i = 0; i < i_count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) err = squashfs_read_metadata(sb, &dir_index, &index_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) &index_offset, sizeof(dir_index));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) index = le32_to_cpu(dir_index.index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) if (index > f_pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * Found the index we're looking for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) size = le32_to_cpu(dir_index.size) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) /* size should never be larger than SQUASHFS_NAME_LEN */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (size > SQUASHFS_NAME_LEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) err = squashfs_read_metadata(sb, NULL, &index_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) &index_offset, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) length = index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) *next_block = le32_to_cpu(dir_index.start_block) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) msblk->directory_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) *next_offset = (length + *next_offset) % SQUASHFS_METADATA_SIZE;
^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) * Translate back from internal f_pos to external f_pos.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) return length + 3;
^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) static int squashfs_readdir(struct file *file, struct dir_context *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) u64 block = squashfs_i(inode)->start + msblk->directory_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) int offset = squashfs_i(inode)->offset, length, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) unsigned int inode_number, dir_count, size, type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) struct squashfs_dir_header dirh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) struct squashfs_dir_entry *dire;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) TRACE("Entered squashfs_readdir [%llx:%x]\n", block, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) dire = kmalloc(sizeof(*dire) + SQUASHFS_NAME_LEN + 1, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (dire == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) ERROR("Failed to allocate squashfs_dir_entry\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) goto finish;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * Return "." and ".." entries as the first two filenames in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * directory. To maximise compression these two entries are not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * stored in the directory, and so we invent them here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * It also means that the external f_pos is offset by 3 from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) * on-disk directory f_pos.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) while (ctx->pos < 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) int i_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (ctx->pos == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) name = ".";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) size = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) i_ino = inode->i_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) name = "..";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) size = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) i_ino = squashfs_i(inode)->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (!dir_emit(ctx, name, size, i_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) squashfs_filetype_table[1]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) goto finish;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) ctx->pos += size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) length = get_dir_index_using_offset(inode->i_sb, &block, &offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) squashfs_i(inode)->dir_idx_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) squashfs_i(inode)->dir_idx_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) squashfs_i(inode)->dir_idx_cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) ctx->pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) while (length < i_size_read(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) * Read directory header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) err = squashfs_read_metadata(inode->i_sb, &dirh, &block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) &offset, sizeof(dirh));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) goto failed_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) length += sizeof(dirh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) dir_count = le32_to_cpu(dirh.count) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (dir_count > SQUASHFS_DIR_COUNT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) goto failed_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) while (dir_count--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) * Read directory entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) err = squashfs_read_metadata(inode->i_sb, dire, &block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) &offset, sizeof(*dire));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) goto failed_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) size = le16_to_cpu(dire->size) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) /* size should never be larger than SQUASHFS_NAME_LEN */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if (size > SQUASHFS_NAME_LEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) goto failed_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) err = squashfs_read_metadata(inode->i_sb, dire->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) &block, &offset, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) goto failed_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) length += sizeof(*dire) + size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) if (ctx->pos >= length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) dire->name[size] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) inode_number = le32_to_cpu(dirh.inode_number) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) ((short) le16_to_cpu(dire->inode_number));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) type = le16_to_cpu(dire->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) if (type > SQUASHFS_MAX_DIR_TYPE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) goto failed_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) if (!dir_emit(ctx, dire->name, size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) inode_number,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) squashfs_filetype_table[type]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) goto finish;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) ctx->pos = length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) finish:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) kfree(dire);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) failed_read:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) ERROR("Unable to read directory block [%llx:%x]\n", block, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) kfree(dire);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) const struct file_operations squashfs_dir_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) .read = generic_read_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) .iterate_shared = squashfs_readdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) .llseek = generic_file_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) };