spi-topcliff-pch: Fix issue for transmitting over 4KByte
[zen-stable.git] / fs / squashfs / dir.c
blob9dfe2ce0fb70f0b0caee3d14c9f0f3026bf5d3f4
1 /*
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.
21 * dir.c
25 * This file implements code to read directories from disk.
27 * See namei.c for a description of directory organisation on disk.
30 #include <linux/fs.h>
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"
37 #include "squashfs.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
49 * quicker.
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;
57 struct squashfs_dir_index dir_index;
59 TRACE("Entered get_dir_index_using_offset, i_count %d, f_pos %lld\n",
60 i_count, f_pos);
63 * Translate from external f_pos to the internal f_pos. This
64 * is offset by 3 because we invent "." and ".." entries which are
65 * not actually stored in the directory.
67 if (f_pos < 3)
68 return f_pos;
69 f_pos -= 3;
71 for (i = 0; i < i_count; i++) {
72 err = squashfs_read_metadata(sb, &dir_index, &index_start,
73 &index_offset, sizeof(dir_index));
74 if (err < 0)
75 break;
77 index = le32_to_cpu(dir_index.index);
78 if (index > f_pos)
80 * Found the index we're looking for.
82 break;
84 err = squashfs_read_metadata(sb, NULL, &index_start,
85 &index_offset, le32_to_cpu(dir_index.size) + 1);
86 if (err < 0)
87 break;
89 length = index;
90 *next_block = le32_to_cpu(dir_index.start_block) +
91 msblk->directory_table;
94 *next_offset = (length + *next_offset) % SQUASHFS_METADATA_SIZE;
97 * Translate back from internal f_pos to external f_pos.
99 return length + 3;
103 static int squashfs_readdir(struct file *file, void *dirent, filldir_t filldir)
105 struct inode *inode = file->f_dentry->d_inode;
106 struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
107 u64 block = squashfs_i(inode)->start + msblk->directory_table;
108 int offset = squashfs_i(inode)->offset, length = 0, dir_count, size,
109 type, err;
110 unsigned int inode_number;
111 struct squashfs_dir_header dirh;
112 struct squashfs_dir_entry *dire;
114 TRACE("Entered squashfs_readdir [%llx:%x]\n", block, offset);
116 dire = kmalloc(sizeof(*dire) + SQUASHFS_NAME_LEN + 1, GFP_KERNEL);
117 if (dire == NULL) {
118 ERROR("Failed to allocate squashfs_dir_entry\n");
119 goto finish;
123 * Return "." and ".." entries as the first two filenames in the
124 * directory. To maximise compression these two entries are not
125 * stored in the directory, and so we invent them here.
127 * It also means that the external f_pos is offset by 3 from the
128 * on-disk directory f_pos.
130 while (file->f_pos < 3) {
131 char *name;
132 int i_ino;
134 if (file->f_pos == 0) {
135 name = ".";
136 size = 1;
137 i_ino = inode->i_ino;
138 } else {
139 name = "..";
140 size = 2;
141 i_ino = squashfs_i(inode)->parent;
144 TRACE("Calling filldir(%p, %s, %d, %lld, %d, %d)\n",
145 dirent, name, size, file->f_pos, i_ino,
146 squashfs_filetype_table[1]);
148 if (filldir(dirent, name, size, file->f_pos, i_ino,
149 squashfs_filetype_table[1]) < 0) {
150 TRACE("Filldir returned less than 0\n");
151 goto finish;
154 file->f_pos += size;
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,
161 file->f_pos);
163 while (length < i_size_read(inode)) {
165 * Read directory header
167 err = squashfs_read_metadata(inode->i_sb, &dirh, &block,
168 &offset, sizeof(dirh));
169 if (err < 0)
170 goto failed_read;
172 length += sizeof(dirh);
174 dir_count = le32_to_cpu(dirh.count) + 1;
176 /* dir_count should never be larger than 256 */
177 if (dir_count > 256)
178 goto failed_read;
180 while (dir_count--) {
182 * Read directory entry.
184 err = squashfs_read_metadata(inode->i_sb, dire, &block,
185 &offset, sizeof(*dire));
186 if (err < 0)
187 goto failed_read;
189 size = le16_to_cpu(dire->size) + 1;
191 /* size should never be larger than SQUASHFS_NAME_LEN */
192 if (size > SQUASHFS_NAME_LEN)
193 goto failed_read;
195 err = squashfs_read_metadata(inode->i_sb, dire->name,
196 &block, &offset, size);
197 if (err < 0)
198 goto failed_read;
200 length += sizeof(*dire) + size;
202 if (file->f_pos >= length)
203 continue;
205 dire->name[size] = '\0';
206 inode_number = le32_to_cpu(dirh.inode_number) +
207 ((short) le16_to_cpu(dire->inode_number));
208 type = le16_to_cpu(dire->type);
210 TRACE("Calling filldir(%p, %s, %d, %lld, %x:%x, %d, %d)"
211 "\n", dirent, dire->name, size,
212 file->f_pos,
213 le32_to_cpu(dirh.start_block),
214 le16_to_cpu(dire->offset),
215 inode_number,
216 squashfs_filetype_table[type]);
218 if (filldir(dirent, dire->name, size, file->f_pos,
219 inode_number,
220 squashfs_filetype_table[type]) < 0) {
221 TRACE("Filldir returned less than 0\n");
222 goto finish;
225 file->f_pos = length;
229 finish:
230 kfree(dire);
231 return 0;
233 failed_read:
234 ERROR("Unable to read directory block [%llx:%x]\n", block, offset);
235 kfree(dire);
236 return 0;
240 const struct file_operations squashfs_dir_ops = {
241 .read = generic_read_dir,
242 .readdir = squashfs_readdir,
243 .llseek = default_llseek,