dir.c 5.3 KB

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