symlink.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. * 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 page *page = &folio->page;
  32. struct inode *inode = page->mapping->host;
  33. struct super_block *sb = inode->i_sb;
  34. struct squashfs_sb_info *msblk = sb->s_fs_info;
  35. int index = page->index << PAGE_SHIFT;
  36. u64 block = squashfs_i(inode)->start;
  37. int offset = squashfs_i(inode)->offset;
  38. int length = min_t(int, i_size_read(inode) - index, PAGE_SIZE);
  39. int bytes, copied;
  40. void *pageaddr;
  41. struct squashfs_cache_entry *entry;
  42. TRACE("Entered squashfs_symlink_readpage, page index %ld, start block "
  43. "%llx, offset %x\n", page->index, block, offset);
  44. /*
  45. * Skip index bytes into symlink metadata.
  46. */
  47. if (index) {
  48. bytes = squashfs_read_metadata(sb, NULL, &block, &offset,
  49. index);
  50. if (bytes < 0) {
  51. ERROR("Unable to read symlink [%llx:%x]\n",
  52. squashfs_i(inode)->start,
  53. squashfs_i(inode)->offset);
  54. goto error_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_atomic to map the page. 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. goto error_out;
  72. }
  73. pageaddr = kmap_atomic(page);
  74. copied = squashfs_copy_data(pageaddr + bytes, entry, offset,
  75. length - bytes);
  76. if (copied == length - bytes)
  77. memset(pageaddr + length, 0, PAGE_SIZE - length);
  78. else
  79. block = entry->next_index;
  80. kunmap_atomic(pageaddr);
  81. squashfs_cache_put(entry);
  82. }
  83. flush_dcache_page(page);
  84. SetPageUptodate(page);
  85. unlock_page(page);
  86. return 0;
  87. error_out:
  88. SetPageError(page);
  89. unlock_page(page);
  90. return 0;
  91. }
  92. const struct address_space_operations squashfs_symlink_aops = {
  93. .read_folio = squashfs_symlink_read_folio
  94. };
  95. const struct inode_operations squashfs_symlink_inode_ops = {
  96. .get_link = page_get_link,
  97. .listxattr = squashfs_listxattr
  98. };