Phillip Lougher | 07972dd | 2009-01-05 08:46:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Squashfs - a compressed read only filesystem for Linux |
| 3 | * |
| 4 | * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008 |
Phillip Lougher | d7f2ff6 | 2011-05-26 10:39:56 +0100 | [diff] [blame] | 5 | * Phillip Lougher <phillip@squashfs.org.uk> |
Phillip Lougher | 07972dd | 2009-01-05 08:46:23 +0000 | [diff] [blame] | 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 | * 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 Lougher | 07972dd | 2009-01-05 08:46:23 +0000 | [diff] [blame] | 33 | |
| 34 | #include "squashfs_fs.h" |
| 35 | #include "squashfs_fs_sb.h" |
| 36 | #include "squashfs_fs_i.h" |
| 37 | #include "squashfs.h" |
| 38 | |
| 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 |
| 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 | */ |
| 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) |
| 54 | { |
| 55 | struct squashfs_sb_info *msblk = sb->s_fs_info; |
| 56 | int err, i, index, length = 0; |
| 57 | struct squashfs_dir_index dir_index; |
| 58 | |
| 59 | TRACE("Entered get_dir_index_using_offset, i_count %d, f_pos %lld\n", |
| 60 | i_count, f_pos); |
| 61 | |
| 62 | /* |
| 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. |
| 66 | */ |
Phillip Lougher | 2158d3f | 2012-03-09 02:27:49 +0000 | [diff] [blame] | 67 | if (f_pos <= 3) |
Phillip Lougher | 07972dd | 2009-01-05 08:46:23 +0000 | [diff] [blame] | 68 | return f_pos; |
| 69 | f_pos -= 3; |
| 70 | |
| 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; |
| 76 | |
| 77 | index = le32_to_cpu(dir_index.index); |
| 78 | if (index > f_pos) |
| 79 | /* |
| 80 | * Found the index we're looking for. |
| 81 | */ |
| 82 | break; |
| 83 | |
| 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; |
| 88 | |
| 89 | length = index; |
| 90 | *next_block = le32_to_cpu(dir_index.start_block) + |
| 91 | msblk->directory_table; |
| 92 | } |
| 93 | |
| 94 | *next_offset = (length + *next_offset) % SQUASHFS_METADATA_SIZE; |
| 95 | |
| 96 | /* |
| 97 | * Translate back from internal f_pos to external f_pos. |
| 98 | */ |
| 99 | return length + 3; |
| 100 | } |
| 101 | |
| 102 | |
Al Viro | 5f6039c | 2013-05-16 01:17:58 -0400 | [diff] [blame] | 103 | static int squashfs_readdir(struct file *file, struct dir_context *ctx) |
Phillip Lougher | 07972dd | 2009-01-05 08:46:23 +0000 | [diff] [blame] | 104 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 105 | struct inode *inode = file_inode(file); |
Phillip Lougher | 07972dd | 2009-01-05 08:46:23 +0000 | [diff] [blame] | 106 | struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info; |
| 107 | u64 block = squashfs_i(inode)->start + msblk->directory_table; |
Phillip Lougher | 68a8c39 | 2012-03-06 23:02:49 +0000 | [diff] [blame] | 108 | int offset = squashfs_i(inode)->offset, length, dir_count, size, |
Phillip Lougher | 07972dd | 2009-01-05 08:46:23 +0000 | [diff] [blame] | 109 | type, err; |
| 110 | unsigned int inode_number; |
| 111 | struct squashfs_dir_header dirh; |
| 112 | struct squashfs_dir_entry *dire; |
| 113 | |
| 114 | TRACE("Entered squashfs_readdir [%llx:%x]\n", block, offset); |
| 115 | |
| 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; |
| 120 | } |
| 121 | |
| 122 | /* |
| 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. |
| 126 | * |
| 127 | * It also means that the external f_pos is offset by 3 from the |
| 128 | * on-disk directory f_pos. |
| 129 | */ |
Al Viro | 5f6039c | 2013-05-16 01:17:58 -0400 | [diff] [blame] | 130 | while (ctx->pos < 3) { |
Phillip Lougher | 07972dd | 2009-01-05 08:46:23 +0000 | [diff] [blame] | 131 | char *name; |
| 132 | int i_ino; |
| 133 | |
Al Viro | 5f6039c | 2013-05-16 01:17:58 -0400 | [diff] [blame] | 134 | if (ctx->pos == 0) { |
Phillip Lougher | 07972dd | 2009-01-05 08:46:23 +0000 | [diff] [blame] | 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; |
| 142 | } |
| 143 | |
Al Viro | 5f6039c | 2013-05-16 01:17:58 -0400 | [diff] [blame] | 144 | if (!dir_emit(ctx, name, size, i_ino, |
| 145 | squashfs_filetype_table[1])) |
Phillip Lougher | 07972dd | 2009-01-05 08:46:23 +0000 | [diff] [blame] | 146 | goto finish; |
Phillip Lougher | 07972dd | 2009-01-05 08:46:23 +0000 | [diff] [blame] | 147 | |
Al Viro | 5f6039c | 2013-05-16 01:17:58 -0400 | [diff] [blame] | 148 | ctx->pos += size; |
Phillip Lougher | 07972dd | 2009-01-05 08:46:23 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | length = get_dir_index_using_offset(inode->i_sb, &block, &offset, |
| 152 | squashfs_i(inode)->dir_idx_start, |
| 153 | squashfs_i(inode)->dir_idx_offset, |
| 154 | squashfs_i(inode)->dir_idx_cnt, |
Al Viro | 5f6039c | 2013-05-16 01:17:58 -0400 | [diff] [blame] | 155 | ctx->pos); |
Phillip Lougher | 07972dd | 2009-01-05 08:46:23 +0000 | [diff] [blame] | 156 | |
| 157 | while (length < i_size_read(inode)) { |
| 158 | /* |
| 159 | * Read directory header |
| 160 | */ |
| 161 | err = squashfs_read_metadata(inode->i_sb, &dirh, &block, |
| 162 | &offset, sizeof(dirh)); |
| 163 | if (err < 0) |
| 164 | goto failed_read; |
| 165 | |
| 166 | length += sizeof(dirh); |
| 167 | |
| 168 | dir_count = le32_to_cpu(dirh.count) + 1; |
Phillip Lougher | 44cff8a | 2011-03-15 22:09:55 +0000 | [diff] [blame] | 169 | |
Ajeet Yadav | 4826d83 | 2012-02-02 13:04:49 +0530 | [diff] [blame] | 170 | if (dir_count > SQUASHFS_DIR_COUNT) |
Phillip Lougher | 44cff8a | 2011-03-15 22:09:55 +0000 | [diff] [blame] | 171 | goto failed_read; |
| 172 | |
Phillip Lougher | 07972dd | 2009-01-05 08:46:23 +0000 | [diff] [blame] | 173 | while (dir_count--) { |
| 174 | /* |
| 175 | * Read directory entry. |
| 176 | */ |
| 177 | err = squashfs_read_metadata(inode->i_sb, dire, &block, |
| 178 | &offset, sizeof(*dire)); |
| 179 | if (err < 0) |
| 180 | goto failed_read; |
| 181 | |
| 182 | size = le16_to_cpu(dire->size) + 1; |
| 183 | |
Phillip Lougher | 44cff8a | 2011-03-15 22:09:55 +0000 | [diff] [blame] | 184 | /* size should never be larger than SQUASHFS_NAME_LEN */ |
| 185 | if (size > SQUASHFS_NAME_LEN) |
| 186 | goto failed_read; |
| 187 | |
Phillip Lougher | 07972dd | 2009-01-05 08:46:23 +0000 | [diff] [blame] | 188 | err = squashfs_read_metadata(inode->i_sb, dire->name, |
| 189 | &block, &offset, size); |
| 190 | if (err < 0) |
| 191 | goto failed_read; |
| 192 | |
| 193 | length += sizeof(*dire) + size; |
| 194 | |
Al Viro | 5f6039c | 2013-05-16 01:17:58 -0400 | [diff] [blame] | 195 | if (ctx->pos >= length) |
Phillip Lougher | 07972dd | 2009-01-05 08:46:23 +0000 | [diff] [blame] | 196 | continue; |
| 197 | |
| 198 | dire->name[size] = '\0'; |
| 199 | inode_number = le32_to_cpu(dirh.inode_number) + |
| 200 | ((short) le16_to_cpu(dire->inode_number)); |
| 201 | type = le16_to_cpu(dire->type); |
| 202 | |
Al Viro | 5f6039c | 2013-05-16 01:17:58 -0400 | [diff] [blame] | 203 | if (!dir_emit(ctx, dire->name, size, |
Phillip Lougher | 07972dd | 2009-01-05 08:46:23 +0000 | [diff] [blame] | 204 | inode_number, |
Al Viro | 5f6039c | 2013-05-16 01:17:58 -0400 | [diff] [blame] | 205 | squashfs_filetype_table[type])) |
Phillip Lougher | 07972dd | 2009-01-05 08:46:23 +0000 | [diff] [blame] | 206 | goto finish; |
Phillip Lougher | 07972dd | 2009-01-05 08:46:23 +0000 | [diff] [blame] | 207 | |
Al Viro | 5f6039c | 2013-05-16 01:17:58 -0400 | [diff] [blame] | 208 | ctx->pos = length; |
Phillip Lougher | 07972dd | 2009-01-05 08:46:23 +0000 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
| 212 | finish: |
| 213 | kfree(dire); |
| 214 | return 0; |
| 215 | |
| 216 | failed_read: |
| 217 | ERROR("Unable to read directory block [%llx:%x]\n", block, offset); |
| 218 | kfree(dire); |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | |
| 223 | const struct file_operations squashfs_dir_ops = { |
| 224 | .read = generic_read_dir, |
Al Viro | 5f6039c | 2013-05-16 01:17:58 -0400 | [diff] [blame] | 225 | .iterate = squashfs_readdir, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 226 | .llseek = default_llseek, |
Phillip Lougher | 07972dd | 2009-01-05 08:46:23 +0000 | [diff] [blame] | 227 | }; |