export.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. * export.c
  22. */
  23. /*
  24. * This file implements code to make Squashfs filesystems exportable (NFS etc.)
  25. *
  26. * The export code uses an inode lookup table to map inode numbers passed in
  27. * filehandles to an inode location on disk. This table is stored compressed
  28. * into metadata blocks. A second index table is used to locate these. This
  29. * second index table for speed of access (and because it is small) is read at
  30. * mount time and cached in memory.
  31. *
  32. * The inode lookup table is used only by the export code, inode disk
  33. * locations are directly encoded in directories, enabling direct access
  34. * without an intermediate lookup for all operations except the export ops.
  35. */
  36. #include <linux/fs.h>
  37. #include <linux/vfs.h>
  38. #include <linux/dcache.h>
  39. #include <linux/exportfs.h>
  40. #include <linux/slab.h>
  41. #include "squashfs_fs.h"
  42. #include "squashfs_fs_sb.h"
  43. #include "squashfs_fs_i.h"
  44. #include "squashfs.h"
  45. /*
  46. * Look-up inode number (ino) in table, returning the inode location.
  47. */
  48. static long long squashfs_inode_lookup(struct super_block *sb, int ino_num)
  49. {
  50. struct squashfs_sb_info *msblk = sb->s_fs_info;
  51. int blk = SQUASHFS_LOOKUP_BLOCK(ino_num - 1);
  52. int offset = SQUASHFS_LOOKUP_BLOCK_OFFSET(ino_num - 1);
  53. u64 start;
  54. __le64 ino;
  55. int err;
  56. TRACE("Entered squashfs_inode_lookup, inode_number = %d\n", ino_num);
  57. if (ino_num == 0 || (ino_num - 1) >= msblk->inodes)
  58. return -EINVAL;
  59. start = le64_to_cpu(msblk->inode_lookup_table[blk]);
  60. err = squashfs_read_metadata(sb, &ino, &start, &offset, sizeof(ino));
  61. if (err < 0)
  62. return err;
  63. TRACE("squashfs_inode_lookup, inode = 0x%llx\n",
  64. (u64) le64_to_cpu(ino));
  65. return le64_to_cpu(ino);
  66. }
  67. static struct dentry *squashfs_export_iget(struct super_block *sb,
  68. unsigned int ino_num)
  69. {
  70. long long ino;
  71. struct dentry *dentry = ERR_PTR(-ENOENT);
  72. TRACE("Entered squashfs_export_iget\n");
  73. ino = squashfs_inode_lookup(sb, ino_num);
  74. if (ino >= 0)
  75. dentry = d_obtain_alias(squashfs_iget(sb, ino, ino_num));
  76. return dentry;
  77. }
  78. static struct dentry *squashfs_fh_to_dentry(struct super_block *sb,
  79. struct fid *fid, int fh_len, int fh_type)
  80. {
  81. if ((fh_type != FILEID_INO32_GEN && fh_type != FILEID_INO32_GEN_PARENT)
  82. || fh_len < 2)
  83. return NULL;
  84. return squashfs_export_iget(sb, fid->i32.ino);
  85. }
  86. static struct dentry *squashfs_fh_to_parent(struct super_block *sb,
  87. struct fid *fid, int fh_len, int fh_type)
  88. {
  89. if (fh_type != FILEID_INO32_GEN_PARENT || fh_len < 4)
  90. return NULL;
  91. return squashfs_export_iget(sb, fid->i32.parent_ino);
  92. }
  93. static struct dentry *squashfs_get_parent(struct dentry *child)
  94. {
  95. struct inode *inode = d_inode(child);
  96. unsigned int parent_ino = squashfs_i(inode)->parent;
  97. return squashfs_export_iget(inode->i_sb, parent_ino);
  98. }
  99. /*
  100. * Read uncompressed inode lookup table indexes off disk into memory
  101. */
  102. __le64 *squashfs_read_inode_lookup_table(struct super_block *sb,
  103. u64 lookup_table_start, u64 next_table, unsigned int inodes)
  104. {
  105. unsigned int length = SQUASHFS_LOOKUP_BLOCK_BYTES(inodes);
  106. unsigned int indexes = SQUASHFS_LOOKUP_BLOCKS(inodes);
  107. int n;
  108. __le64 *table;
  109. u64 start, end;
  110. TRACE("In read_inode_lookup_table, length %d\n", length);
  111. /* Sanity check values */
  112. /* there should always be at least one inode */
  113. if (inodes == 0)
  114. return ERR_PTR(-EINVAL);
  115. /*
  116. * The computed size of the lookup table (length bytes) should exactly
  117. * match the table start and end points
  118. */
  119. if (length != (next_table - lookup_table_start))
  120. return ERR_PTR(-EINVAL);
  121. table = squashfs_read_table(sb, lookup_table_start, length);
  122. if (IS_ERR(table))
  123. return table;
  124. /*
  125. * table0], table[1], ... table[indexes - 1] store the locations
  126. * of the compressed inode lookup blocks. Each entry should be
  127. * less than the next (i.e. table[0] < table[1]), and the difference
  128. * between them should be SQUASHFS_METADATA_SIZE or less.
  129. * table[indexes - 1] should be less than lookup_table_start, and
  130. * again the difference should be SQUASHFS_METADATA_SIZE or less
  131. */
  132. for (n = 0; n < (indexes - 1); n++) {
  133. start = le64_to_cpu(table[n]);
  134. end = le64_to_cpu(table[n + 1]);
  135. if (start >= end
  136. || (end - start) >
  137. (SQUASHFS_METADATA_SIZE + SQUASHFS_BLOCK_OFFSET)) {
  138. kfree(table);
  139. return ERR_PTR(-EINVAL);
  140. }
  141. }
  142. start = le64_to_cpu(table[indexes - 1]);
  143. if (start >= lookup_table_start ||
  144. (lookup_table_start - start) >
  145. (SQUASHFS_METADATA_SIZE + SQUASHFS_BLOCK_OFFSET)) {
  146. kfree(table);
  147. return ERR_PTR(-EINVAL);
  148. }
  149. return table;
  150. }
  151. const struct export_operations squashfs_export_ops = {
  152. .fh_to_dentry = squashfs_fh_to_dentry,
  153. .fh_to_parent = squashfs_fh_to_parent,
  154. .get_parent = squashfs_get_parent
  155. };