dir.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. struct squashfs_dir_index dir_index;
  53. TRACE("Entered get_dir_index_using_offset, i_count %d, f_pos %lld\n",
  54. i_count, f_pos);
  55. /*
  56. * Translate from external f_pos to the internal f_pos. This
  57. * is offset by 3 because we invent "." and ".." entries which are
  58. * not actually stored in the directory.
  59. */
  60. if (f_pos < 3)
  61. return f_pos;
  62. f_pos -= 3;
  63. for (i = 0; i < i_count; i++) {
  64. err = squashfs_read_metadata(sb, &dir_index, &index_start,
  65. &index_offset, sizeof(dir_index));
  66. if (err < 0)
  67. break;
  68. index = le32_to_cpu(dir_index.index);
  69. if (index > f_pos)
  70. /*
  71. * Found the index we're looking for.
  72. */
  73. break;
  74. err = squashfs_read_metadata(sb, NULL, &index_start,
  75. &index_offset, le32_to_cpu(dir_index.size) + 1);
  76. if (err < 0)
  77. break;
  78. length = index;
  79. *next_block = le32_to_cpu(dir_index.start_block) +
  80. msblk->directory_table;
  81. }
  82. *next_offset = (length + *next_offset) % SQUASHFS_METADATA_SIZE;
  83. /*
  84. * Translate back from internal f_pos to external f_pos.
  85. */
  86. return length + 3;
  87. }
  88. static int squashfs_readdir(struct file *file, void *dirent, filldir_t filldir)
  89. {
  90. struct inode *inode = file->f_dentry->d_inode;
  91. struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
  92. u64 block = squashfs_i(inode)->start + msblk->directory_table;
  93. int offset = squashfs_i(inode)->offset, length = 0, dir_count, size,
  94. type, err;
  95. unsigned int inode_number;
  96. struct squashfs_dir_header dirh;
  97. struct squashfs_dir_entry *dire;
  98. TRACE("Entered squashfs_readdir [%llx:%x]\n", block, offset);
  99. dire = kmalloc(sizeof(*dire) + SQUASHFS_NAME_LEN + 1, GFP_KERNEL);
  100. if (dire == NULL) {
  101. ERROR("Failed to allocate squashfs_dir_entry\n");
  102. goto finish;
  103. }
  104. /*
  105. * Return "." and ".." entries as the first two filenames in the
  106. * directory. To maximise compression these two entries are not
  107. * stored in the directory, and so we invent them here.
  108. *
  109. * It also means that the external f_pos is offset by 3 from the
  110. * on-disk directory f_pos.
  111. */
  112. while (file->f_pos < 3) {
  113. char *name;
  114. int i_ino;
  115. if (file->f_pos == 0) {
  116. name = ".";
  117. size = 1;
  118. i_ino = inode->i_ino;
  119. } else {
  120. name = "..";
  121. size = 2;
  122. i_ino = squashfs_i(inode)->parent;
  123. }
  124. TRACE("Calling filldir(%p, %s, %d, %lld, %d, %d)\n",
  125. dirent, name, size, file->f_pos, i_ino,
  126. squashfs_filetype_table[1]);
  127. if (filldir(dirent, name, size, file->f_pos, i_ino,
  128. squashfs_filetype_table[1]) < 0) {
  129. TRACE("Filldir returned less than 0\n");
  130. goto finish;
  131. }
  132. file->f_pos += size;
  133. }
  134. length = get_dir_index_using_offset(inode->i_sb, &block, &offset,
  135. squashfs_i(inode)->dir_idx_start,
  136. squashfs_i(inode)->dir_idx_offset,
  137. squashfs_i(inode)->dir_idx_cnt,
  138. file->f_pos);
  139. while (length < i_size_read(inode)) {
  140. /*
  141. * Read directory header
  142. */
  143. err = squashfs_read_metadata(inode->i_sb, &dirh, &block,
  144. &offset, sizeof(dirh));
  145. if (err < 0)
  146. goto failed_read;
  147. length += sizeof(dirh);
  148. dir_count = le32_to_cpu(dirh.count) + 1;
  149. /* dir_count should never be larger than 256 */
  150. if (dir_count > 256)
  151. goto failed_read;
  152. while (dir_count--) {
  153. /*
  154. * Read directory entry.
  155. */
  156. err = squashfs_read_metadata(inode->i_sb, dire, &block,
  157. &offset, sizeof(*dire));
  158. if (err < 0)
  159. goto failed_read;
  160. size = le16_to_cpu(dire->size) + 1;
  161. /* size should never be larger than SQUASHFS_NAME_LEN */
  162. if (size > SQUASHFS_NAME_LEN)
  163. goto failed_read;
  164. err = squashfs_read_metadata(inode->i_sb, dire->name,
  165. &block, &offset, size);
  166. if (err < 0)
  167. goto failed_read;
  168. length += sizeof(*dire) + size;
  169. if (file->f_pos >= length)
  170. continue;
  171. dire->name[size] = '\0';
  172. inode_number = le32_to_cpu(dirh.inode_number) +
  173. ((short) le16_to_cpu(dire->inode_number));
  174. type = le16_to_cpu(dire->type);
  175. TRACE("Calling filldir(%p, %s, %d, %lld, %x:%x, %d, %d)"
  176. "\n", dirent, dire->name, size,
  177. file->f_pos,
  178. le32_to_cpu(dirh.start_block),
  179. le16_to_cpu(dire->offset),
  180. inode_number,
  181. squashfs_filetype_table[type]);
  182. if (filldir(dirent, dire->name, size, file->f_pos,
  183. inode_number,
  184. squashfs_filetype_table[type]) < 0) {
  185. TRACE("Filldir returned less than 0\n");
  186. goto finish;
  187. }
  188. file->f_pos = length;
  189. }
  190. }
  191. finish:
  192. kfree(dire);
  193. return 0;
  194. failed_read:
  195. ERROR("Unable to read directory block [%llx:%x]\n", block, offset);
  196. kfree(dire);
  197. return 0;
  198. }
  199. const struct file_operations squashfs_dir_ops = {
  200. .read = generic_read_dir,
  201. .readdir = squashfs_readdir,
  202. .llseek = default_llseek,
  203. };