namei.c 7.4 KB

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