fragment.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. * fragment.c
  9. */
  10. /*
  11. * This file implements code to handle compressed fragments (tail-end packed
  12. * datablocks).
  13. *
  14. * Regular files contain a fragment index which is mapped to a fragment
  15. * location on disk and compressed size using a fragment lookup table.
  16. * Like everything in Squashfs this fragment lookup table is itself stored
  17. * compressed into metadata blocks. A second index table is used to locate
  18. * these. This second index table for speed of access (and because it
  19. * is small) is read at mount time and cached in memory.
  20. */
  21. #include <linux/fs.h>
  22. #include <linux/vfs.h>
  23. #include <linux/slab.h>
  24. #include "squashfs_fs.h"
  25. #include "squashfs_fs_sb.h"
  26. #include "squashfs.h"
  27. /*
  28. * Look-up fragment using the fragment index table. Return the on disk
  29. * location of the fragment and its compressed size
  30. */
  31. int squashfs_frag_lookup(struct super_block *sb, unsigned int fragment,
  32. u64 *fragment_block)
  33. {
  34. struct squashfs_sb_info *msblk = sb->s_fs_info;
  35. int block, offset, size;
  36. struct squashfs_fragment_entry fragment_entry;
  37. u64 start_block;
  38. if (fragment >= msblk->fragments)
  39. return -EIO;
  40. block = SQUASHFS_FRAGMENT_INDEX(fragment);
  41. offset = SQUASHFS_FRAGMENT_INDEX_OFFSET(fragment);
  42. start_block = le64_to_cpu(msblk->fragment_index[block]);
  43. size = squashfs_read_metadata(sb, &fragment_entry, &start_block,
  44. &offset, sizeof(fragment_entry));
  45. if (size < 0)
  46. return size;
  47. *fragment_block = le64_to_cpu(fragment_entry.start_block);
  48. return squashfs_block_size(fragment_entry.size);
  49. }
  50. /*
  51. * Read the uncompressed fragment lookup table indexes off disk into memory
  52. */
  53. __le64 *squashfs_read_fragment_index_table(struct super_block *sb,
  54. u64 fragment_table_start, u64 next_table, unsigned int fragments)
  55. {
  56. unsigned int length = SQUASHFS_FRAGMENT_INDEX_BYTES(fragments);
  57. __le64 *table;
  58. /*
  59. * Sanity check, length bytes should not extend into the next table -
  60. * this check also traps instances where fragment_table_start is
  61. * incorrectly larger than the next table start
  62. */
  63. if (fragment_table_start + length > next_table)
  64. return ERR_PTR(-EINVAL);
  65. table = squashfs_read_table(sb, fragment_table_start, length);
  66. /*
  67. * table[0] points to the first fragment table metadata block, this
  68. * should be less than fragment_table_start
  69. */
  70. if (!IS_ERR(table) && le64_to_cpu(table[0]) >= fragment_table_start) {
  71. kfree(table);
  72. return ERR_PTR(-EINVAL);
  73. }
  74. return table;
  75. }