dir.c 5.2 KB

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