2 * Squashfs - a compressed read only filesystem for Linux
4 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
5 * Phillip Lougher <phillip@squashfs.org.uk>
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.
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.
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.
25 * This file implements code to read directories from disk.
27 * See namei.c for a description of directory organisation on disk.
31 #include <linux/vfs.h>
32 #include <linux/slab.h>
34 #include "squashfs_fs.h"
35 #include "squashfs_fs_sb.h"
36 #include "squashfs_fs_i.h"
39 static const unsigned char squashfs_filetype_table[] = {
40 DT_UNKNOWN, DT_DIR, DT_REG, DT_LNK, DT_BLK, DT_CHR, DT_FIFO, DT_SOCK
44 * Lookup offset (f_pos) in the directory index, returning the
45 * metadata block containing it.
47 * If we get an error reading the index then return the part of the index
48 * (if any) we have managed to read - the index isn't essential, just
51 static int get_dir_index_using_offset(struct super_block *sb,
52 u64 *next_block, int *next_offset, u64 index_start, int index_offset,
53 int i_count, u64 f_pos)
55 struct squashfs_sb_info *msblk = sb->s_fs_info;
56 int err, i, index, length = 0;
58 struct squashfs_dir_index dir_index;
60 TRACE("Entered get_dir_index_using_offset, i_count %d, f_pos %lld\n",
64 * Translate from external f_pos to the internal f_pos. This
65 * is offset by 3 because we invent "." and ".." entries which are
66 * not actually stored in the directory.
72 for (i = 0; i < i_count; i++) {
73 err = squashfs_read_metadata(sb, &dir_index, &index_start,
74 &index_offset, sizeof(dir_index));
78 index = le32_to_cpu(dir_index.index);
81 * Found the index we're looking for.
85 size = le32_to_cpu(dir_index.size) + 1;
87 /* size should never be larger than SQUASHFS_NAME_LEN */
88 if (size > SQUASHFS_NAME_LEN)
91 err = squashfs_read_metadata(sb, NULL, &index_start,
97 *next_block = le32_to_cpu(dir_index.start_block) +
98 msblk->directory_table;
101 *next_offset = (length + *next_offset) % SQUASHFS_METADATA_SIZE;
104 * Translate back from internal f_pos to external f_pos.
110 static int squashfs_readdir(struct file *file, void *dirent, filldir_t filldir)
112 struct inode *inode = file_inode(file);
113 struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
114 u64 block = squashfs_i(inode)->start + msblk->directory_table;
115 int offset = squashfs_i(inode)->offset, length, err;
116 unsigned int inode_number, dir_count, size, type;
117 struct squashfs_dir_header dirh;
118 struct squashfs_dir_entry *dire;
120 TRACE("Entered squashfs_readdir [%llx:%x]\n", block, offset);
122 dire = kmalloc(sizeof(*dire) + SQUASHFS_NAME_LEN + 1, GFP_KERNEL);
124 ERROR("Failed to allocate squashfs_dir_entry\n");
129 * Return "." and ".." entries as the first two filenames in the
130 * directory. To maximise compression these two entries are not
131 * stored in the directory, and so we invent them here.
133 * It also means that the external f_pos is offset by 3 from the
134 * on-disk directory f_pos.
136 while (file->f_pos < 3) {
140 if (file->f_pos == 0) {
143 i_ino = inode->i_ino;
147 i_ino = squashfs_i(inode)->parent;
150 TRACE("Calling filldir(%p, %s, %d, %lld, %d, %d)\n",
151 dirent, name, size, file->f_pos, i_ino,
152 squashfs_filetype_table[1]);
154 if (filldir(dirent, name, size, file->f_pos, i_ino,
155 squashfs_filetype_table[1]) < 0) {
156 TRACE("Filldir returned less than 0\n");
163 length = get_dir_index_using_offset(inode->i_sb, &block, &offset,
164 squashfs_i(inode)->dir_idx_start,
165 squashfs_i(inode)->dir_idx_offset,
166 squashfs_i(inode)->dir_idx_cnt,
169 while (length < i_size_read(inode)) {
171 * Read directory header
173 err = squashfs_read_metadata(inode->i_sb, &dirh, &block,
174 &offset, sizeof(dirh));
178 length += sizeof(dirh);
180 dir_count = le32_to_cpu(dirh.count) + 1;
182 if (dir_count > SQUASHFS_DIR_COUNT)
185 while (dir_count--) {
187 * Read directory entry.
189 err = squashfs_read_metadata(inode->i_sb, dire, &block,
190 &offset, sizeof(*dire));
194 size = le16_to_cpu(dire->size) + 1;
196 /* size should never be larger than SQUASHFS_NAME_LEN */
197 if (size > SQUASHFS_NAME_LEN)
200 err = squashfs_read_metadata(inode->i_sb, dire->name,
201 &block, &offset, size);
205 length += sizeof(*dire) + size;
207 if (file->f_pos >= length)
210 dire->name[size] = '\0';
211 inode_number = le32_to_cpu(dirh.inode_number) +
212 ((short) le16_to_cpu(dire->inode_number));
213 type = le16_to_cpu(dire->type);
215 if (type > SQUASHFS_MAX_DIR_TYPE)
218 TRACE("Calling filldir(%p, %s, %d, %lld, %x:%x, %d, %d)"
219 "\n", dirent, dire->name, size,
221 le32_to_cpu(dirh.start_block),
222 le16_to_cpu(dire->offset),
224 squashfs_filetype_table[type]);
226 if (filldir(dirent, dire->name, size, file->f_pos,
228 squashfs_filetype_table[type]) < 0) {
229 TRACE("Filldir returned less than 0\n");
233 file->f_pos = length;
242 ERROR("Unable to read directory block [%llx:%x]\n", block, offset);
248 const struct file_operations squashfs_dir_ops = {
249 .read = generic_read_dir,
250 .readdir = squashfs_readdir,
251 .llseek = default_llseek,