namei.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. * namei.c
  9. */
  10. /*
  11. * This file implements code to do filename lookup in directories.
  12. *
  13. * Like inodes, directories are packed into compressed metadata blocks, stored
  14. * in a directory table. Directories are accessed using the start address of
  15. * the metablock containing the directory and the offset into the
  16. * decompressed block (<block, offset>).
  17. *
  18. * Directories are organised in a slightly complex way, and are not simply
  19. * a list of file names. The organisation takes advantage of the
  20. * fact that (in most cases) the inodes of the files will be in the same
  21. * compressed metadata block, and therefore, can share the start block.
  22. * Directories are therefore organised in a two level list, a directory
  23. * header containing the shared start block value, and a sequence of directory
  24. * entries, each of which share the shared start block. A new directory header
  25. * is written once/if the inode start block changes. The directory
  26. * header/directory entry list is repeated as many times as necessary.
  27. *
  28. * Directories are sorted, and can contain a directory index to speed up
  29. * file lookup. Directory indexes store one entry per metablock, each entry
  30. * storing the index/filename mapping to the first directory header
  31. * in each metadata block. Directories are sorted in alphabetical order,
  32. * and at lookup the index is scanned linearly looking for the first filename
  33. * alphabetically larger than the filename being looked up. At this point the
  34. * location of the metadata block the filename is in has been found.
  35. * The general idea of the index is ensure only one metadata block needs to be
  36. * decompressed to do a lookup irrespective of the length of the directory.
  37. * This scheme has the advantage that it doesn't require extra memory overhead
  38. * and doesn't require much extra storage on disk.
  39. */
  40. #include <linux/fs.h>
  41. #include <linux/vfs.h>
  42. #include <linux/slab.h>
  43. #include <linux/string.h>
  44. #include <linux/dcache.h>
  45. #include <linux/xattr.h>
  46. #include "squashfs_fs.h"
  47. #include "squashfs_fs_sb.h"
  48. #include "squashfs_fs_i.h"
  49. #include "squashfs.h"
  50. #include "xattr.h"
  51. /*
  52. * Lookup name in the directory index, returning the location of the metadata
  53. * block containing it, and the directory index this represents.
  54. *
  55. * If we get an error reading the index then return the part of the index
  56. * (if any) we have managed to read - the index isn't essential, just
  57. * quicker.
  58. */
  59. static int get_dir_index_using_name(struct super_block *sb,
  60. u64 *next_block, int *next_offset, u64 index_start,
  61. int index_offset, int i_count, const char *name)
  62. {
  63. struct squashfs_sb_info *msblk = sb->s_fs_info;
  64. int i, length = 0, err;
  65. unsigned int size;
  66. struct squashfs_dir_index *index;
  67. TRACE("Entered get_dir_index_using_name, i_count %d\n", i_count);
  68. index = kmalloc(sizeof(*index) + SQUASHFS_NAME_LEN + 1, GFP_KERNEL);
  69. if (index == NULL) {
  70. ERROR("Failed to allocate squashfs_dir_index\n");
  71. goto out;
  72. }
  73. for (i = 0; i < i_count; i++) {
  74. err = squashfs_read_metadata(sb, index, &index_start,
  75. &index_offset, sizeof(*index));
  76. if (err < 0)
  77. break;
  78. size = le32_to_cpu(index->size) + 1;
  79. if (size > SQUASHFS_NAME_LEN)
  80. break;
  81. err = squashfs_read_metadata(sb, index->name, &index_start,
  82. &index_offset, size);
  83. if (err < 0)
  84. break;
  85. index->name[size] = '\0';
  86. if (strcmp(index->name, name) > 0)
  87. break;
  88. length = le32_to_cpu(index->index);
  89. *next_block = le32_to_cpu(index->start_block) +
  90. msblk->directory_table;
  91. }
  92. *next_offset = (length + *next_offset) % SQUASHFS_METADATA_SIZE;
  93. kfree(index);
  94. out:
  95. /*
  96. * Return index (f_pos) of the looked up metadata block. Translate
  97. * from internal f_pos to external f_pos which is offset by 3 because
  98. * we invent "." and ".." entries which are not actually stored in the
  99. * directory.
  100. */
  101. return length + 3;
  102. }
  103. static struct dentry *squashfs_lookup(struct inode *dir, struct dentry *dentry,
  104. unsigned int flags)
  105. {
  106. const unsigned char *name = dentry->d_name.name;
  107. int len = dentry->d_name.len;
  108. struct inode *inode = NULL;
  109. struct squashfs_sb_info *msblk = dir->i_sb->s_fs_info;
  110. struct squashfs_dir_header dirh;
  111. struct squashfs_dir_entry *dire;
  112. u64 block = squashfs_i(dir)->start + msblk->directory_table;
  113. int offset = squashfs_i(dir)->offset;
  114. int err, length;
  115. unsigned int dir_count, size;
  116. TRACE("Entered squashfs_lookup [%llx:%x]\n", block, offset);
  117. dire = kmalloc(sizeof(*dire) + SQUASHFS_NAME_LEN + 1, GFP_KERNEL);
  118. if (dire == NULL) {
  119. ERROR("Failed to allocate squashfs_dir_entry\n");
  120. return ERR_PTR(-ENOMEM);
  121. }
  122. if (len > SQUASHFS_NAME_LEN) {
  123. err = -ENAMETOOLONG;
  124. goto failed;
  125. }
  126. length = get_dir_index_using_name(dir->i_sb, &block, &offset,
  127. squashfs_i(dir)->dir_idx_start,
  128. squashfs_i(dir)->dir_idx_offset,
  129. squashfs_i(dir)->dir_idx_cnt, name);
  130. while (length < i_size_read(dir)) {
  131. /*
  132. * Read directory header.
  133. */
  134. err = squashfs_read_metadata(dir->i_sb, &dirh, &block,
  135. &offset, sizeof(dirh));
  136. if (err < 0)
  137. goto read_failure;
  138. length += sizeof(dirh);
  139. dir_count = le32_to_cpu(dirh.count) + 1;
  140. if (dir_count > SQUASHFS_DIR_COUNT)
  141. goto data_error;
  142. while (dir_count--) {
  143. /*
  144. * Read directory entry.
  145. */
  146. err = squashfs_read_metadata(dir->i_sb, dire, &block,
  147. &offset, sizeof(*dire));
  148. if (err < 0)
  149. goto read_failure;
  150. size = le16_to_cpu(dire->size) + 1;
  151. /* size should never be larger than SQUASHFS_NAME_LEN */
  152. if (size > SQUASHFS_NAME_LEN)
  153. goto data_error;
  154. err = squashfs_read_metadata(dir->i_sb, dire->name,
  155. &block, &offset, size);
  156. if (err < 0)
  157. goto read_failure;
  158. length += sizeof(*dire) + size;
  159. if (name[0] < dire->name[0])
  160. goto exit_lookup;
  161. if (len == size && !strncmp(name, dire->name, len)) {
  162. unsigned int blk, off, ino_num;
  163. long long ino;
  164. blk = le32_to_cpu(dirh.start_block);
  165. off = le16_to_cpu(dire->offset);
  166. ino_num = le32_to_cpu(dirh.inode_number) +
  167. (short) le16_to_cpu(dire->inode_number);
  168. ino = SQUASHFS_MKINODE(blk, off);
  169. TRACE("calling squashfs_iget for directory "
  170. "entry %s, inode %x:%x, %d\n", name,
  171. blk, off, ino_num);
  172. inode = squashfs_iget(dir->i_sb, ino, ino_num);
  173. goto exit_lookup;
  174. }
  175. }
  176. }
  177. exit_lookup:
  178. kfree(dire);
  179. return d_splice_alias(inode, dentry);
  180. data_error:
  181. err = -EIO;
  182. read_failure:
  183. ERROR("Unable to read directory block [%llx:%x]\n",
  184. squashfs_i(dir)->start + msblk->directory_table,
  185. squashfs_i(dir)->offset);
  186. failed:
  187. kfree(dire);
  188. return ERR_PTR(err);
  189. }
  190. const struct inode_operations squashfs_dir_inode_ops = {
  191. .lookup = squashfs_lookup,
  192. .listxattr = squashfs_listxattr
  193. };