blob: a5845f94a2a1790e9392e545bccfe6b3df5f545b [file] [log] [blame]
Phillip Lougher07972dd2009-01-05 08:46:23 +00001/*
2 * Squashfs - a compressed read only filesystem for Linux
3 *
4 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
Phillip Lougherd7f2ff62011-05-26 10:39:56 +01005 * Phillip Lougher <phillip@squashfs.org.uk>
Phillip Lougher07972dd2009-01-05 08:46:23 +00006 *
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 * dir.c
22 */
23
24/*
25 * This file implements code to read directories from disk.
26 *
27 * See namei.c for a description of directory organisation on disk.
28 */
29
30#include <linux/fs.h>
31#include <linux/vfs.h>
32#include <linux/slab.h>
Phillip Lougher07972dd2009-01-05 08:46:23 +000033
34#include "squashfs_fs.h"
35#include "squashfs_fs_sb.h"
36#include "squashfs_fs_i.h"
37#include "squashfs.h"
38
39static const unsigned char squashfs_filetype_table[] = {
40 DT_UNKNOWN, DT_DIR, DT_REG, DT_LNK, DT_BLK, DT_CHR, DT_FIFO, DT_SOCK
41};
42
43/*
44 * Lookup offset (f_pos) in the directory index, returning the
45 * metadata block containing it.
46 *
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
49 * quicker.
50 */
51static 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)
54{
55 struct squashfs_sb_info *msblk = sb->s_fs_info;
56 int err, i, index, length = 0;
Phillip Lougherf960cae2013-09-03 04:52:52 +010057 unsigned int size;
Phillip Lougher07972dd2009-01-05 08:46:23 +000058 struct squashfs_dir_index dir_index;
59
60 TRACE("Entered get_dir_index_using_offset, i_count %d, f_pos %lld\n",
61 i_count, f_pos);
62
63 /*
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.
67 */
Phillip Lougher2158d3f2012-03-09 02:27:49 +000068 if (f_pos <= 3)
Phillip Lougher07972dd2009-01-05 08:46:23 +000069 return f_pos;
70 f_pos -= 3;
71
72 for (i = 0; i < i_count; i++) {
73 err = squashfs_read_metadata(sb, &dir_index, &index_start,
74 &index_offset, sizeof(dir_index));
75 if (err < 0)
76 break;
77
78 index = le32_to_cpu(dir_index.index);
79 if (index > f_pos)
80 /*
81 * Found the index we're looking for.
82 */
83 break;
84
Phillip Lougherf960cae2013-09-03 04:52:52 +010085 size = le32_to_cpu(dir_index.size) + 1;
86
87 /* size should never be larger than SQUASHFS_NAME_LEN */
88 if (size > SQUASHFS_NAME_LEN)
89 break;
90
Phillip Lougher07972dd2009-01-05 08:46:23 +000091 err = squashfs_read_metadata(sb, NULL, &index_start,
Phillip Lougherf960cae2013-09-03 04:52:52 +010092 &index_offset, size);
Phillip Lougher07972dd2009-01-05 08:46:23 +000093 if (err < 0)
94 break;
95
96 length = index;
97 *next_block = le32_to_cpu(dir_index.start_block) +
98 msblk->directory_table;
99 }
100
101 *next_offset = (length + *next_offset) % SQUASHFS_METADATA_SIZE;
102
103 /*
104 * Translate back from internal f_pos to external f_pos.
105 */
106 return length + 3;
107}
108
109
Al Viro5f6039c2013-05-16 01:17:58 -0400110static int squashfs_readdir(struct file *file, struct dir_context *ctx)
Phillip Lougher07972dd2009-01-05 08:46:23 +0000111{
Al Viro496ad9a2013-01-23 17:07:38 -0500112 struct inode *inode = file_inode(file);
Phillip Lougher07972dd2009-01-05 08:46:23 +0000113 struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
114 u64 block = squashfs_i(inode)->start + msblk->directory_table;
Phillip Lougher9e012422013-09-04 02:58:12 +0100115 int offset = squashfs_i(inode)->offset, length, err;
116 unsigned int inode_number, dir_count, size, type;
Phillip Lougher07972dd2009-01-05 08:46:23 +0000117 struct squashfs_dir_header dirh;
118 struct squashfs_dir_entry *dire;
119
120 TRACE("Entered squashfs_readdir [%llx:%x]\n", block, offset);
121
122 dire = kmalloc(sizeof(*dire) + SQUASHFS_NAME_LEN + 1, GFP_KERNEL);
123 if (dire == NULL) {
124 ERROR("Failed to allocate squashfs_dir_entry\n");
125 goto finish;
126 }
127
128 /*
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.
132 *
133 * It also means that the external f_pos is offset by 3 from the
134 * on-disk directory f_pos.
135 */
Al Viro5f6039c2013-05-16 01:17:58 -0400136 while (ctx->pos < 3) {
Phillip Lougher07972dd2009-01-05 08:46:23 +0000137 char *name;
138 int i_ino;
139
Al Viro5f6039c2013-05-16 01:17:58 -0400140 if (ctx->pos == 0) {
Phillip Lougher07972dd2009-01-05 08:46:23 +0000141 name = ".";
142 size = 1;
143 i_ino = inode->i_ino;
144 } else {
145 name = "..";
146 size = 2;
147 i_ino = squashfs_i(inode)->parent;
148 }
149
Al Viro5f6039c2013-05-16 01:17:58 -0400150 if (!dir_emit(ctx, name, size, i_ino,
151 squashfs_filetype_table[1]))
Phillip Lougher07972dd2009-01-05 08:46:23 +0000152 goto finish;
Phillip Lougher07972dd2009-01-05 08:46:23 +0000153
Al Viro5f6039c2013-05-16 01:17:58 -0400154 ctx->pos += size;
Phillip Lougher07972dd2009-01-05 08:46:23 +0000155 }
156
157 length = get_dir_index_using_offset(inode->i_sb, &block, &offset,
158 squashfs_i(inode)->dir_idx_start,
159 squashfs_i(inode)->dir_idx_offset,
160 squashfs_i(inode)->dir_idx_cnt,
Al Viro5f6039c2013-05-16 01:17:58 -0400161 ctx->pos);
Phillip Lougher07972dd2009-01-05 08:46:23 +0000162
163 while (length < i_size_read(inode)) {
164 /*
165 * Read directory header
166 */
167 err = squashfs_read_metadata(inode->i_sb, &dirh, &block,
168 &offset, sizeof(dirh));
169 if (err < 0)
170 goto failed_read;
171
172 length += sizeof(dirh);
173
174 dir_count = le32_to_cpu(dirh.count) + 1;
Phillip Lougher44cff8a2011-03-15 22:09:55 +0000175
Ajeet Yadav4826d832012-02-02 13:04:49 +0530176 if (dir_count > SQUASHFS_DIR_COUNT)
Phillip Lougher44cff8a2011-03-15 22:09:55 +0000177 goto failed_read;
178
Phillip Lougher07972dd2009-01-05 08:46:23 +0000179 while (dir_count--) {
180 /*
181 * Read directory entry.
182 */
183 err = squashfs_read_metadata(inode->i_sb, dire, &block,
184 &offset, sizeof(*dire));
185 if (err < 0)
186 goto failed_read;
187
188 size = le16_to_cpu(dire->size) + 1;
189
Phillip Lougher44cff8a2011-03-15 22:09:55 +0000190 /* size should never be larger than SQUASHFS_NAME_LEN */
191 if (size > SQUASHFS_NAME_LEN)
192 goto failed_read;
193
Phillip Lougher07972dd2009-01-05 08:46:23 +0000194 err = squashfs_read_metadata(inode->i_sb, dire->name,
195 &block, &offset, size);
196 if (err < 0)
197 goto failed_read;
198
199 length += sizeof(*dire) + size;
200
Al Viro5f6039c2013-05-16 01:17:58 -0400201 if (ctx->pos >= length)
Phillip Lougher07972dd2009-01-05 08:46:23 +0000202 continue;
203
204 dire->name[size] = '\0';
205 inode_number = le32_to_cpu(dirh.inode_number) +
206 ((short) le16_to_cpu(dire->inode_number));
207 type = le16_to_cpu(dire->type);
208
Phillip Lougher9e012422013-09-04 02:58:12 +0100209 if (type > SQUASHFS_MAX_DIR_TYPE)
210 goto failed_read;
211
Al Viro5f6039c2013-05-16 01:17:58 -0400212 if (!dir_emit(ctx, dire->name, size,
Phillip Lougher07972dd2009-01-05 08:46:23 +0000213 inode_number,
Al Viro5f6039c2013-05-16 01:17:58 -0400214 squashfs_filetype_table[type]))
Phillip Lougher07972dd2009-01-05 08:46:23 +0000215 goto finish;
Phillip Lougher07972dd2009-01-05 08:46:23 +0000216
Al Viro5f6039c2013-05-16 01:17:58 -0400217 ctx->pos = length;
Phillip Lougher07972dd2009-01-05 08:46:23 +0000218 }
219 }
220
221finish:
222 kfree(dire);
223 return 0;
224
225failed_read:
226 ERROR("Unable to read directory block [%llx:%x]\n", block, offset);
227 kfree(dire);
228 return 0;
229}
230
231
232const struct file_operations squashfs_dir_ops = {
233 .read = generic_read_dir,
Al Virod3755702016-04-30 23:08:45 -0400234 .iterate_shared = squashfs_readdir,
235 .llseek = generic_file_llseek,
Phillip Lougher07972dd2009-01-05 08:46:23 +0000236};