namei.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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@lougher.demon.co.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 "squashfs_fs.h"
  59. #include "squashfs_fs_sb.h"
  60. #include "squashfs_fs_i.h"
  61. #include "squashfs.h"
  62. /*
  63. * Lookup name in the directory index, returning the location of the metadata
  64. * block containing it, and the directory index this represents.
  65. *
  66. * If we get an error reading the index then return the part of the index
  67. * (if any) we have managed to read - the index isn't essential, just
  68. * quicker.
  69. */
  70. static int get_dir_index_using_name(struct super_block *sb,
  71. u64 *next_block, int *next_offset, u64 index_start,
  72. int index_offset, int i_count, const char *name,
  73. int len)
  74. {
  75. struct squashfs_sb_info *msblk = sb->s_fs_info;
  76. int i, size, length = 0, err;
  77. struct squashfs_dir_index *index;
  78. char *str;
  79. TRACE("Entered get_dir_index_using_name, i_count %d\n", i_count);
  80. index = kmalloc(sizeof(*index) + SQUASHFS_NAME_LEN * 2 + 2, GFP_KERNEL);
  81. if (index == NULL) {
  82. ERROR("Failed to allocate squashfs_dir_index\n");
  83. goto out;
  84. }
  85. str = &index->name[SQUASHFS_NAME_LEN + 1];
  86. strncpy(str, name, len);
  87. str[len] = '\0';
  88. for (i = 0; i < i_count; i++) {
  89. err = squashfs_read_metadata(sb, index, &index_start,
  90. &index_offset, sizeof(*index));
  91. if (err < 0)
  92. break;
  93. size = le32_to_cpu(index->size) + 1;
  94. err = squashfs_read_metadata(sb, index->name, &index_start,
  95. &index_offset, size);
  96. if (err < 0)
  97. break;
  98. index->name[size] = '\0';
  99. if (strcmp(index->name, str) > 0)
  100. break;
  101. length = le32_to_cpu(index->index);
  102. *next_block = le32_to_cpu(index->start_block) +
  103. msblk->directory_table;
  104. }
  105. *next_offset = (length + *next_offset) % SQUASHFS_METADATA_SIZE;
  106. kfree(index);
  107. out:
  108. /*
  109. * Return index (f_pos) of the looked up metadata block. Translate
  110. * from internal f_pos to external f_pos which is offset by 3 because
  111. * we invent "." and ".." entries which are not actually stored in the
  112. * directory.
  113. */
  114. return length + 3;
  115. }
  116. static struct dentry *squashfs_lookup(struct inode *dir, struct dentry *dentry,
  117. struct nameidata *nd)
  118. {
  119. const unsigned char *name = dentry->d_name.name;
  120. int len = dentry->d_name.len;
  121. struct inode *inode = NULL;
  122. struct squashfs_sb_info *msblk = dir->i_sb->s_fs_info;
  123. struct squashfs_dir_header dirh;
  124. struct squashfs_dir_entry *dire;
  125. u64 block = squashfs_i(dir)->start + msblk->directory_table;
  126. int offset = squashfs_i(dir)->offset;
  127. int err, length = 0, dir_count, size;
  128. TRACE("Entered squashfs_lookup [%llx:%x]\n", block, offset);
  129. dire = kmalloc(sizeof(*dire) + SQUASHFS_NAME_LEN + 1, GFP_KERNEL);
  130. if (dire == NULL) {
  131. ERROR("Failed to allocate squashfs_dir_entry\n");
  132. return ERR_PTR(-ENOMEM);
  133. }
  134. if (len > SQUASHFS_NAME_LEN) {
  135. err = -ENAMETOOLONG;
  136. goto failed;
  137. }
  138. length = get_dir_index_using_name(dir->i_sb, &block, &offset,
  139. squashfs_i(dir)->dir_idx_start,
  140. squashfs_i(dir)->dir_idx_offset,
  141. squashfs_i(dir)->dir_idx_cnt, name, len);
  142. while (length < i_size_read(dir)) {
  143. /*
  144. * Read directory header.
  145. */
  146. err = squashfs_read_metadata(dir->i_sb, &dirh, &block,
  147. &offset, sizeof(dirh));
  148. if (err < 0)
  149. goto read_failure;
  150. length += sizeof(dirh);
  151. dir_count = le32_to_cpu(dirh.count) + 1;
  152. while (dir_count--) {
  153. /*
  154. * Read directory entry.
  155. */
  156. err = squashfs_read_metadata(dir->i_sb, dire, &block,
  157. &offset, sizeof(*dire));
  158. if (err < 0)
  159. goto read_failure;
  160. size = le16_to_cpu(dire->size) + 1;
  161. err = squashfs_read_metadata(dir->i_sb, dire->name,
  162. &block, &offset, size);
  163. if (err < 0)
  164. goto read_failure;
  165. length += sizeof(*dire) + size;
  166. if (name[0] < dire->name[0])
  167. goto exit_lookup;
  168. if (len == size && !strncmp(name, dire->name, len)) {
  169. unsigned int blk, off, ino_num;
  170. long long ino;
  171. blk = le32_to_cpu(dirh.start_block);
  172. off = le16_to_cpu(dire->offset);
  173. ino_num = le32_to_cpu(dirh.inode_number) +
  174. (short) le16_to_cpu(dire->inode_number);
  175. ino = SQUASHFS_MKINODE(blk, off);
  176. TRACE("calling squashfs_iget for directory "
  177. "entry %s, inode %x:%x, %d\n", name,
  178. blk, off, ino_num);
  179. inode = squashfs_iget(dir->i_sb, ino, ino_num);
  180. if (IS_ERR(inode)) {
  181. err = PTR_ERR(inode);
  182. goto failed;
  183. }
  184. goto exit_lookup;
  185. }
  186. }
  187. }
  188. exit_lookup:
  189. kfree(dire);
  190. if (inode)
  191. return d_splice_alias(inode, dentry);
  192. d_add(dentry, inode);
  193. return ERR_PTR(0);
  194. read_failure:
  195. ERROR("Unable to read directory block [%llx:%x]\n",
  196. squashfs_i(dir)->start + msblk->directory_table,
  197. squashfs_i(dir)->offset);
  198. failed:
  199. kfree(dire);
  200. return ERR_PTR(err);
  201. }
  202. const struct inode_operations squashfs_dir_inode_ops = {
  203. .lookup = squashfs_lookup
  204. };