symlink.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. * symlink.c
  9. */
  10. /*
  11. * This file implements code to handle symbolic links.
  12. *
  13. * The data contents of symbolic links are stored inside the symbolic
  14. * link inode within the inode table. This allows the normally small symbolic
  15. * link to be compressed as part of the inode table, achieving much greater
  16. * compression than if the symbolic link was compressed individually.
  17. */
  18. #include <linux/fs.h>
  19. #include <linux/vfs.h>
  20. #include <linux/kernel.h>
  21. #include <linux/string.h>
  22. #include <linux/pagemap.h>
  23. #include <linux/xattr.h>
  24. #include "squashfs_fs.h"
  25. #include "squashfs_fs_sb.h"
  26. #include "squashfs_fs_i.h"
  27. #include "squashfs.h"
  28. #include "xattr.h"
  29. static int squashfs_symlink_read_folio(struct file *file, struct folio *folio)
  30. {
  31. struct inode *inode = folio->mapping->host;
  32. struct super_block *sb = inode->i_sb;
  33. struct squashfs_sb_info *msblk = sb->s_fs_info;
  34. int index = folio_pos(folio);
  35. u64 block = squashfs_i(inode)->start;
  36. int offset = squashfs_i(inode)->offset;
  37. int length = min_t(int, i_size_read(inode) - index, PAGE_SIZE);
  38. int bytes, copied, error;
  39. void *pageaddr;
  40. struct squashfs_cache_entry *entry;
  41. TRACE("Entered squashfs_symlink_readpage, page index %ld, start block "
  42. "%llx, offset %x\n", folio->index, block, offset);
  43. /*
  44. * Skip index bytes into symlink metadata.
  45. */
  46. if (index) {
  47. bytes = squashfs_read_metadata(sb, NULL, &block, &offset,
  48. index);
  49. if (bytes < 0) {
  50. ERROR("Unable to read symlink [%llx:%x]\n",
  51. squashfs_i(inode)->start,
  52. squashfs_i(inode)->offset);
  53. error = bytes;
  54. goto out;
  55. }
  56. }
  57. /*
  58. * Read length bytes from symlink metadata. Squashfs_read_metadata
  59. * is not used here because it can sleep and we want to use
  60. * kmap_local to map the folio. Instead call the underlying
  61. * squashfs_cache_get routine. As length bytes may overlap metadata
  62. * blocks, we may need to call squashfs_cache_get multiple times.
  63. */
  64. for (bytes = 0; bytes < length; offset = 0, bytes += copied) {
  65. entry = squashfs_cache_get(sb, msblk->block_cache, block, 0);
  66. if (entry->error) {
  67. ERROR("Unable to read symlink [%llx:%x]\n",
  68. squashfs_i(inode)->start,
  69. squashfs_i(inode)->offset);
  70. squashfs_cache_put(entry);
  71. error = entry->error;
  72. goto out;
  73. }
  74. pageaddr = kmap_local_folio(folio, 0);
  75. copied = squashfs_copy_data(pageaddr + bytes, entry, offset,
  76. length - bytes);
  77. if (copied == length - bytes)
  78. memset(pageaddr + length, 0, PAGE_SIZE - length);
  79. else
  80. block = entry->next_index;
  81. kunmap_local(pageaddr);
  82. squashfs_cache_put(entry);
  83. }
  84. flush_dcache_folio(folio);
  85. error = 0;
  86. out:
  87. folio_end_read(folio, error == 0);
  88. return error;
  89. }
  90. const struct address_space_operations squashfs_symlink_aops = {
  91. .read_folio = squashfs_symlink_read_folio
  92. };
  93. const struct inode_operations squashfs_symlink_inode_ops = {
  94. .get_link = page_get_link,
  95. .listxattr = squashfs_listxattr
  96. };