symlink.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Squashfs - a compressed read only filesystem for Linux
  3. *
  4. * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
  5. * Phillip Lougher <phillip@squashfs.org.uk>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2,
  10. * or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. *
  21. * symlink.c
  22. */
  23. /*
  24. * This file implements code to handle symbolic links.
  25. *
  26. * The data contents of symbolic links are stored inside the symbolic
  27. * link inode within the inode table. This allows the normally small symbolic
  28. * link to be compressed as part of the inode table, achieving much greater
  29. * compression than if the symbolic link was compressed individually.
  30. */
  31. #include <linux/fs.h>
  32. #include <linux/vfs.h>
  33. #include <linux/kernel.h>
  34. #include <linux/string.h>
  35. #include <linux/pagemap.h>
  36. #include <linux/xattr.h>
  37. #include "squashfs_fs.h"
  38. #include "squashfs_fs_sb.h"
  39. #include "squashfs_fs_i.h"
  40. #include "squashfs.h"
  41. #include "xattr.h"
  42. static int squashfs_symlink_readpage(struct file *file, struct page *page)
  43. {
  44. struct inode *inode = page->mapping->host;
  45. struct super_block *sb = inode->i_sb;
  46. struct squashfs_sb_info *msblk = sb->s_fs_info;
  47. int index = page->index << PAGE_SHIFT;
  48. u64 block = squashfs_i(inode)->start;
  49. int offset = squashfs_i(inode)->offset;
  50. int length = min_t(int, i_size_read(inode) - index, PAGE_SIZE);
  51. int bytes, copied;
  52. void *pageaddr;
  53. struct squashfs_cache_entry *entry;
  54. TRACE("Entered squashfs_symlink_readpage, page index %ld, start block "
  55. "%llx, offset %x\n", page->index, block, offset);
  56. /*
  57. * Skip index bytes into symlink metadata.
  58. */
  59. if (index) {
  60. bytes = squashfs_read_metadata(sb, NULL, &block, &offset,
  61. index);
  62. if (bytes < 0) {
  63. ERROR("Unable to read symlink [%llx:%x]\n",
  64. squashfs_i(inode)->start,
  65. squashfs_i(inode)->offset);
  66. goto error_out;
  67. }
  68. }
  69. /*
  70. * Read length bytes from symlink metadata. Squashfs_read_metadata
  71. * is not used here because it can sleep and we want to use
  72. * kmap_atomic to map the page. Instead call the underlying
  73. * squashfs_cache_get routine. As length bytes may overlap metadata
  74. * blocks, we may need to call squashfs_cache_get multiple times.
  75. */
  76. for (bytes = 0; bytes < length; offset = 0, bytes += copied) {
  77. entry = squashfs_cache_get(sb, msblk->block_cache, block, 0);
  78. if (entry->error) {
  79. ERROR("Unable to read symlink [%llx:%x]\n",
  80. squashfs_i(inode)->start,
  81. squashfs_i(inode)->offset);
  82. squashfs_cache_put(entry);
  83. goto error_out;
  84. }
  85. pageaddr = kmap_atomic(page);
  86. copied = squashfs_copy_data(pageaddr + bytes, entry, offset,
  87. length - bytes);
  88. if (copied == length - bytes)
  89. memset(pageaddr + length, 0, PAGE_SIZE - length);
  90. else
  91. block = entry->next_index;
  92. kunmap_atomic(pageaddr);
  93. squashfs_cache_put(entry);
  94. }
  95. flush_dcache_page(page);
  96. SetPageUptodate(page);
  97. unlock_page(page);
  98. return 0;
  99. error_out:
  100. SetPageError(page);
  101. unlock_page(page);
  102. return 0;
  103. }
  104. const struct address_space_operations squashfs_symlink_aops = {
  105. .readpage = squashfs_symlink_readpage
  106. };
  107. const struct inode_operations squashfs_symlink_inode_ops = {
  108. .get_link = page_get_link,
  109. .listxattr = squashfs_listxattr
  110. };