export.c 4.7 KB

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