symlink.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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_readpage(struct file *file, struct page *page)
  30. {
  31. struct inode *inode = page->mapping->host;
  32. struct super_block *sb = inode->i_sb;
  33. struct squashfs_sb_info *msblk = sb->s_fs_info;
  34. int index = page->index << PAGE_SHIFT;
  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;
  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", page->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. goto error_out;
  54. }
  55. }
  56. /*
  57. * Read length bytes from symlink metadata. Squashfs_read_metadata
  58. * is not used here because it can sleep and we want to use
  59. * kmap_atomic to map the page. Instead call the underlying
  60. * squashfs_cache_get routine. As length bytes may overlap metadata
  61. * blocks, we may need to call squashfs_cache_get multiple times.
  62. */
  63. for (bytes = 0; bytes < length; offset = 0, bytes += copied) {
  64. entry = squashfs_cache_get(sb, msblk->block_cache, block, 0);
  65. if (entry->error) {
  66. ERROR("Unable to read symlink [%llx:%x]\n",
  67. squashfs_i(inode)->start,
  68. squashfs_i(inode)->offset);
  69. squashfs_cache_put(entry);
  70. goto error_out;
  71. }
  72. pageaddr = kmap_atomic(page);
  73. copied = squashfs_copy_data(pageaddr + bytes, entry, offset,
  74. length - bytes);
  75. if (copied == length - bytes)
  76. memset(pageaddr + length, 0, PAGE_SIZE - length);
  77. else
  78. block = entry->next_index;
  79. kunmap_atomic(pageaddr);
  80. squashfs_cache_put(entry);
  81. }
  82. flush_dcache_page(page);
  83. SetPageUptodate(page);
  84. unlock_page(page);
  85. return 0;
  86. error_out:
  87. SetPageError(page);
  88. unlock_page(page);
  89. return 0;
  90. }
  91. const struct address_space_operations squashfs_symlink_aops = {
  92. .readpage = squashfs_symlink_readpage
  93. };
  94. const struct inode_operations squashfs_symlink_inode_ops = {
  95. .get_link = page_get_link,
  96. .listxattr = squashfs_listxattr
  97. };