dir.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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@lougher.demon.co.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 <linux/zlib.h>
  32. #include "squashfs_fs.h"
  33. #include "squashfs_fs_sb.h"
  34. #include "squashfs_fs_i.h"
  35. #include "squashfs.h"
  36. static const unsigned char squashfs_filetype_table[] = {
  37. DT_UNKNOWN, DT_DIR, DT_REG, DT_LNK, DT_BLK, DT_CHR, DT_FIFO, DT_SOCK
  38. };
  39. /*
  40. * Lookup offset (f_pos) in the directory index, returning the
  41. * metadata block containing it.
  42. *
  43. * If we get an error reading the index then return the part of the index
  44. * (if any) we have managed to read - the index isn't essential, just
  45. * quicker.
  46. */
  47. static int get_dir_index_using_offset(struct super_block *sb,
  48. u64 *next_block, int *next_offset, u64 index_start, int index_offset,
  49. int i_count, u64 f_pos)
  50. {
  51. struct squashfs_sb_info *msblk = sb->s_fs_info;
  52. int err, i, index, length = 0;
  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. err = squashfs_read_metadata(sb, NULL, &index_start,
  76. &index_offset, le32_to_cpu(dir_index.size) + 1);
  77. if (err < 0)
  78. break;
  79. length = index;
  80. *next_block = le32_to_cpu(dir_index.start_block) +
  81. msblk->directory_table;
  82. }
  83. *next_offset = (length + *next_offset) % SQUASHFS_METADATA_SIZE;
  84. /*
  85. * Translate back from internal f_pos to external f_pos.
  86. */
  87. return length + 3;
  88. }
  89. static int squashfs_readdir(struct file *file, void *dirent, filldir_t filldir)
  90. {
  91. struct inode *inode = file->f_dentry->d_inode;
  92. struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
  93. u64 block = squashfs_i(inode)->start + msblk->directory_table;
  94. int offset = squashfs_i(inode)->offset, length = 0, dir_count, size,
  95. type, err;
  96. unsigned int inode_number;
  97. struct squashfs_dir_header dirh;
  98. struct squashfs_dir_entry *dire;
  99. TRACE("Entered squashfs_readdir [%llx:%x]\n", block, offset);
  100. dire = kmalloc(sizeof(*dire) + SQUASHFS_NAME_LEN + 1, GFP_KERNEL);
  101. if (dire == NULL) {
  102. ERROR("Failed to allocate squashfs_dir_entry\n");
  103. goto finish;
  104. }
  105. /*
  106. * Return "." and ".." entries as the first two filenames in the
  107. * directory. To maximise compression these two entries are not
  108. * stored in the directory, and so we invent them here.
  109. *
  110. * It also means that the external f_pos is offset by 3 from the
  111. * on-disk directory f_pos.
  112. */
  113. while (file->f_pos < 3) {
  114. char *name;
  115. int i_ino;
  116. if (file->f_pos == 0) {
  117. name = ".";
  118. size = 1;
  119. i_ino = inode->i_ino;
  120. } else {
  121. name = "..";
  122. size = 2;
  123. i_ino = squashfs_i(inode)->parent;
  124. }
  125. TRACE("Calling filldir(%p, %s, %d, %lld, %d, %d)\n",
  126. dirent, name, size, file->f_pos, i_ino,
  127. squashfs_filetype_table[1]);
  128. if (filldir(dirent, name, size, file->f_pos, i_ino,
  129. squashfs_filetype_table[1]) < 0) {
  130. TRACE("Filldir returned less than 0\n");
  131. goto finish;
  132. }
  133. file->f_pos += size;
  134. }
  135. length = get_dir_index_using_offset(inode->i_sb, &block, &offset,
  136. squashfs_i(inode)->dir_idx_start,
  137. squashfs_i(inode)->dir_idx_offset,
  138. squashfs_i(inode)->dir_idx_cnt,
  139. file->f_pos);
  140. while (length < i_size_read(inode)) {
  141. /*
  142. * Read directory header
  143. */
  144. err = squashfs_read_metadata(inode->i_sb, &dirh, &block,
  145. &offset, sizeof(dirh));
  146. if (err < 0)
  147. goto failed_read;
  148. length += sizeof(dirh);
  149. dir_count = le32_to_cpu(dirh.count) + 1;
  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. err = squashfs_read_metadata(inode->i_sb, dire->name,
  160. &block, &offset, size);
  161. if (err < 0)
  162. goto failed_read;
  163. length += sizeof(*dire) + size;
  164. if (file->f_pos >= length)
  165. continue;
  166. dire->name[size] = '\0';
  167. inode_number = le32_to_cpu(dirh.inode_number) +
  168. ((short) le16_to_cpu(dire->inode_number));
  169. type = le16_to_cpu(dire->type);
  170. TRACE("Calling filldir(%p, %s, %d, %lld, %x:%x, %d, %d)"
  171. "\n", dirent, dire->name, size,
  172. file->f_pos,
  173. le32_to_cpu(dirh.start_block),
  174. le16_to_cpu(dire->offset),
  175. inode_number,
  176. squashfs_filetype_table[type]);
  177. if (filldir(dirent, dire->name, size, file->f_pos,
  178. inode_number,
  179. squashfs_filetype_table[type]) < 0) {
  180. TRACE("Filldir returned less than 0\n");
  181. goto finish;
  182. }
  183. file->f_pos = length;
  184. }
  185. }
  186. finish:
  187. kfree(dire);
  188. return 0;
  189. failed_read:
  190. ERROR("Unable to read directory block [%llx:%x]\n", block, offset);
  191. kfree(dire);
  192. return 0;
  193. }
  194. const struct file_operations squashfs_dir_ops = {
  195. .read = generic_read_dir,
  196. .readdir = squashfs_readdir
  197. };