dir.c 5.9 KB

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