168252eb5SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21dc4bba3SPhillip Lougher /*
31dc4bba3SPhillip Lougher * Squashfs - a compressed read only filesystem for Linux
41dc4bba3SPhillip Lougher *
51dc4bba3SPhillip Lougher * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
6d7f2ff67SPhillip Lougher * Phillip Lougher <phillip@squashfs.org.uk>
71dc4bba3SPhillip Lougher *
81dc4bba3SPhillip Lougher * symlink.c
91dc4bba3SPhillip Lougher */
101dc4bba3SPhillip Lougher
111dc4bba3SPhillip Lougher /*
121dc4bba3SPhillip Lougher * This file implements code to handle symbolic links.
131dc4bba3SPhillip Lougher *
141dc4bba3SPhillip Lougher * The data contents of symbolic links are stored inside the symbolic
151dc4bba3SPhillip Lougher * link inode within the inode table. This allows the normally small symbolic
161dc4bba3SPhillip Lougher * link to be compressed as part of the inode table, achieving much greater
171dc4bba3SPhillip Lougher * compression than if the symbolic link was compressed individually.
181dc4bba3SPhillip Lougher */
191dc4bba3SPhillip Lougher
201dc4bba3SPhillip Lougher #include <linux/fs.h>
211dc4bba3SPhillip Lougher #include <linux/vfs.h>
221dc4bba3SPhillip Lougher #include <linux/kernel.h>
231dc4bba3SPhillip Lougher #include <linux/string.h>
241dc4bba3SPhillip Lougher #include <linux/pagemap.h>
2567f66cc6SPhillip Lougher #include <linux/xattr.h>
261dc4bba3SPhillip Lougher
271dc4bba3SPhillip Lougher #include "squashfs_fs.h"
281dc4bba3SPhillip Lougher #include "squashfs_fs_sb.h"
291dc4bba3SPhillip Lougher #include "squashfs_fs_i.h"
301dc4bba3SPhillip Lougher #include "squashfs.h"
3101e5b4e4SPhillip Lougher #include "xattr.h"
321dc4bba3SPhillip Lougher
squashfs_symlink_read_folio(struct file * file,struct folio * folio)33*124cfc15SMatthew Wilcox (Oracle) static int squashfs_symlink_read_folio(struct file *file, struct folio *folio)
341dc4bba3SPhillip Lougher {
35*124cfc15SMatthew Wilcox (Oracle) struct page *page = &folio->page;
361dc4bba3SPhillip Lougher struct inode *inode = page->mapping->host;
371dc4bba3SPhillip Lougher struct super_block *sb = inode->i_sb;
381dc4bba3SPhillip Lougher struct squashfs_sb_info *msblk = sb->s_fs_info;
3909cbfeafSKirill A. Shutemov int index = page->index << PAGE_SHIFT;
401dc4bba3SPhillip Lougher u64 block = squashfs_i(inode)->start;
411dc4bba3SPhillip Lougher int offset = squashfs_i(inode)->offset;
4209cbfeafSKirill A. Shutemov int length = min_t(int, i_size_read(inode) - index, PAGE_SIZE);
431dc4bba3SPhillip Lougher int bytes, copied;
441dc4bba3SPhillip Lougher void *pageaddr;
451dc4bba3SPhillip Lougher struct squashfs_cache_entry *entry;
461dc4bba3SPhillip Lougher
471dc4bba3SPhillip Lougher TRACE("Entered squashfs_symlink_readpage, page index %ld, start block "
481dc4bba3SPhillip Lougher "%llx, offset %x\n", page->index, block, offset);
491dc4bba3SPhillip Lougher
501dc4bba3SPhillip Lougher /*
511dc4bba3SPhillip Lougher * Skip index bytes into symlink metadata.
521dc4bba3SPhillip Lougher */
531dc4bba3SPhillip Lougher if (index) {
541dc4bba3SPhillip Lougher bytes = squashfs_read_metadata(sb, NULL, &block, &offset,
551dc4bba3SPhillip Lougher index);
561dc4bba3SPhillip Lougher if (bytes < 0) {
571dc4bba3SPhillip Lougher ERROR("Unable to read symlink [%llx:%x]\n",
581dc4bba3SPhillip Lougher squashfs_i(inode)->start,
591dc4bba3SPhillip Lougher squashfs_i(inode)->offset);
601dc4bba3SPhillip Lougher goto error_out;
611dc4bba3SPhillip Lougher }
621dc4bba3SPhillip Lougher }
631dc4bba3SPhillip Lougher
641dc4bba3SPhillip Lougher /*
651dc4bba3SPhillip Lougher * Read length bytes from symlink metadata. Squashfs_read_metadata
661dc4bba3SPhillip Lougher * is not used here because it can sleep and we want to use
671dc4bba3SPhillip Lougher * kmap_atomic to map the page. Instead call the underlying
681dc4bba3SPhillip Lougher * squashfs_cache_get routine. As length bytes may overlap metadata
691dc4bba3SPhillip Lougher * blocks, we may need to call squashfs_cache_get multiple times.
701dc4bba3SPhillip Lougher */
711dc4bba3SPhillip Lougher for (bytes = 0; bytes < length; offset = 0, bytes += copied) {
721dc4bba3SPhillip Lougher entry = squashfs_cache_get(sb, msblk->block_cache, block, 0);
731dc4bba3SPhillip Lougher if (entry->error) {
741dc4bba3SPhillip Lougher ERROR("Unable to read symlink [%llx:%x]\n",
751dc4bba3SPhillip Lougher squashfs_i(inode)->start,
761dc4bba3SPhillip Lougher squashfs_i(inode)->offset);
771dc4bba3SPhillip Lougher squashfs_cache_put(entry);
781dc4bba3SPhillip Lougher goto error_out;
791dc4bba3SPhillip Lougher }
801dc4bba3SPhillip Lougher
8153b55e55SCong Wang pageaddr = kmap_atomic(page);
821dc4bba3SPhillip Lougher copied = squashfs_copy_data(pageaddr + bytes, entry, offset,
831dc4bba3SPhillip Lougher length - bytes);
841dc4bba3SPhillip Lougher if (copied == length - bytes)
8509cbfeafSKirill A. Shutemov memset(pageaddr + length, 0, PAGE_SIZE - length);
861dc4bba3SPhillip Lougher else
871dc4bba3SPhillip Lougher block = entry->next_index;
8853b55e55SCong Wang kunmap_atomic(pageaddr);
891dc4bba3SPhillip Lougher squashfs_cache_put(entry);
901dc4bba3SPhillip Lougher }
911dc4bba3SPhillip Lougher
921dc4bba3SPhillip Lougher flush_dcache_page(page);
931dc4bba3SPhillip Lougher SetPageUptodate(page);
941dc4bba3SPhillip Lougher unlock_page(page);
951dc4bba3SPhillip Lougher return 0;
961dc4bba3SPhillip Lougher
971dc4bba3SPhillip Lougher error_out:
981dc4bba3SPhillip Lougher SetPageError(page);
991dc4bba3SPhillip Lougher unlock_page(page);
1001dc4bba3SPhillip Lougher return 0;
1011dc4bba3SPhillip Lougher }
1021dc4bba3SPhillip Lougher
1031dc4bba3SPhillip Lougher
1041dc4bba3SPhillip Lougher const struct address_space_operations squashfs_symlink_aops = {
105*124cfc15SMatthew Wilcox (Oracle) .read_folio = squashfs_symlink_read_folio
1061dc4bba3SPhillip Lougher };
10767f66cc6SPhillip Lougher
10867f66cc6SPhillip Lougher const struct inode_operations squashfs_symlink_inode_ops = {
1096b255391SAl Viro .get_link = page_get_link,
11067f66cc6SPhillip Lougher .listxattr = squashfs_listxattr
11167f66cc6SPhillip Lougher };
11267f66cc6SPhillip Lougher
113