18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Squashfs - a compressed read only filesystem for Linux
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
68c2ecf20Sopenharmony_ci * Phillip Lougher <phillip@squashfs.org.uk>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * symlink.c
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci/*
128c2ecf20Sopenharmony_ci * This file implements code to handle symbolic links.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * The data contents of symbolic links are stored inside the symbolic
158c2ecf20Sopenharmony_ci * link inode within the inode table.  This allows the normally small symbolic
168c2ecf20Sopenharmony_ci * link to be compressed as part of the inode table, achieving much greater
178c2ecf20Sopenharmony_ci * compression than if the symbolic link was compressed individually.
188c2ecf20Sopenharmony_ci */
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#include <linux/fs.h>
218c2ecf20Sopenharmony_ci#include <linux/vfs.h>
228c2ecf20Sopenharmony_ci#include <linux/kernel.h>
238c2ecf20Sopenharmony_ci#include <linux/string.h>
248c2ecf20Sopenharmony_ci#include <linux/pagemap.h>
258c2ecf20Sopenharmony_ci#include <linux/xattr.h>
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#include "squashfs_fs.h"
288c2ecf20Sopenharmony_ci#include "squashfs_fs_sb.h"
298c2ecf20Sopenharmony_ci#include "squashfs_fs_i.h"
308c2ecf20Sopenharmony_ci#include "squashfs.h"
318c2ecf20Sopenharmony_ci#include "xattr.h"
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_cistatic int squashfs_symlink_readpage(struct file *file, struct page *page)
348c2ecf20Sopenharmony_ci{
358c2ecf20Sopenharmony_ci	struct inode *inode = page->mapping->host;
368c2ecf20Sopenharmony_ci	struct super_block *sb = inode->i_sb;
378c2ecf20Sopenharmony_ci	struct squashfs_sb_info *msblk = sb->s_fs_info;
388c2ecf20Sopenharmony_ci	int index = page->index << PAGE_SHIFT;
398c2ecf20Sopenharmony_ci	u64 block = squashfs_i(inode)->start;
408c2ecf20Sopenharmony_ci	int offset = squashfs_i(inode)->offset;
418c2ecf20Sopenharmony_ci	int length = min_t(int, i_size_read(inode) - index, PAGE_SIZE);
428c2ecf20Sopenharmony_ci	int bytes, copied;
438c2ecf20Sopenharmony_ci	void *pageaddr;
448c2ecf20Sopenharmony_ci	struct squashfs_cache_entry *entry;
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	TRACE("Entered squashfs_symlink_readpage, page index %ld, start block "
478c2ecf20Sopenharmony_ci			"%llx, offset %x\n", page->index, block, offset);
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	/*
508c2ecf20Sopenharmony_ci	 * Skip index bytes into symlink metadata.
518c2ecf20Sopenharmony_ci	 */
528c2ecf20Sopenharmony_ci	if (index) {
538c2ecf20Sopenharmony_ci		bytes = squashfs_read_metadata(sb, NULL, &block, &offset,
548c2ecf20Sopenharmony_ci								index);
558c2ecf20Sopenharmony_ci		if (bytes < 0) {
568c2ecf20Sopenharmony_ci			ERROR("Unable to read symlink [%llx:%x]\n",
578c2ecf20Sopenharmony_ci				squashfs_i(inode)->start,
588c2ecf20Sopenharmony_ci				squashfs_i(inode)->offset);
598c2ecf20Sopenharmony_ci			goto error_out;
608c2ecf20Sopenharmony_ci		}
618c2ecf20Sopenharmony_ci	}
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	/*
648c2ecf20Sopenharmony_ci	 * Read length bytes from symlink metadata.  Squashfs_read_metadata
658c2ecf20Sopenharmony_ci	 * is not used here because it can sleep and we want to use
668c2ecf20Sopenharmony_ci	 * kmap_atomic to map the page.  Instead call the underlying
678c2ecf20Sopenharmony_ci	 * squashfs_cache_get routine.  As length bytes may overlap metadata
688c2ecf20Sopenharmony_ci	 * blocks, we may need to call squashfs_cache_get multiple times.
698c2ecf20Sopenharmony_ci	 */
708c2ecf20Sopenharmony_ci	for (bytes = 0; bytes < length; offset = 0, bytes += copied) {
718c2ecf20Sopenharmony_ci		entry = squashfs_cache_get(sb, msblk->block_cache, block, 0);
728c2ecf20Sopenharmony_ci		if (entry->error) {
738c2ecf20Sopenharmony_ci			ERROR("Unable to read symlink [%llx:%x]\n",
748c2ecf20Sopenharmony_ci				squashfs_i(inode)->start,
758c2ecf20Sopenharmony_ci				squashfs_i(inode)->offset);
768c2ecf20Sopenharmony_ci			squashfs_cache_put(entry);
778c2ecf20Sopenharmony_ci			goto error_out;
788c2ecf20Sopenharmony_ci		}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci		pageaddr = kmap_atomic(page);
818c2ecf20Sopenharmony_ci		copied = squashfs_copy_data(pageaddr + bytes, entry, offset,
828c2ecf20Sopenharmony_ci								length - bytes);
838c2ecf20Sopenharmony_ci		if (copied == length - bytes)
848c2ecf20Sopenharmony_ci			memset(pageaddr + length, 0, PAGE_SIZE - length);
858c2ecf20Sopenharmony_ci		else
868c2ecf20Sopenharmony_ci			block = entry->next_index;
878c2ecf20Sopenharmony_ci		kunmap_atomic(pageaddr);
888c2ecf20Sopenharmony_ci		squashfs_cache_put(entry);
898c2ecf20Sopenharmony_ci	}
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	flush_dcache_page(page);
928c2ecf20Sopenharmony_ci	SetPageUptodate(page);
938c2ecf20Sopenharmony_ci	unlock_page(page);
948c2ecf20Sopenharmony_ci	return 0;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_cierror_out:
978c2ecf20Sopenharmony_ci	SetPageError(page);
988c2ecf20Sopenharmony_ci	unlock_page(page);
998c2ecf20Sopenharmony_ci	return 0;
1008c2ecf20Sopenharmony_ci}
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ciconst struct address_space_operations squashfs_symlink_aops = {
1048c2ecf20Sopenharmony_ci	.readpage = squashfs_symlink_readpage
1058c2ecf20Sopenharmony_ci};
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ciconst struct inode_operations squashfs_symlink_inode_ops = {
1088c2ecf20Sopenharmony_ci	.get_link = page_get_link,
1098c2ecf20Sopenharmony_ci	.listxattr = squashfs_listxattr
1108c2ecf20Sopenharmony_ci};
1118c2ecf20Sopenharmony_ci
112