Merge branch 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/davej/cpufreq
[linux-2.6-block.git] / fs / squashfs / symlink.c
CommitLineData
1dc4bba3
PL
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@lougher.demon.co.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/*
25 * This file implements code to handle symbolic links.
26 *
27 * The data contents of symbolic links are stored inside the symbolic
28 * link inode within the inode table. This allows the normally small symbolic
29 * link to be compressed as part of the inode table, achieving much greater
30 * compression than if the symbolic link was compressed individually.
31 */
32
33#include <linux/fs.h>
34#include <linux/vfs.h>
35#include <linux/kernel.h>
36#include <linux/slab.h>
37#include <linux/string.h>
38#include <linux/pagemap.h>
1dc4bba3
PL
39
40#include "squashfs_fs.h"
41#include "squashfs_fs_sb.h"
42#include "squashfs_fs_i.h"
43#include "squashfs.h"
44
45static int squashfs_symlink_readpage(struct file *file, struct page *page)
46{
47 struct inode *inode = page->mapping->host;
48 struct super_block *sb = inode->i_sb;
49 struct squashfs_sb_info *msblk = sb->s_fs_info;
50 int index = page->index << PAGE_CACHE_SHIFT;
51 u64 block = squashfs_i(inode)->start;
52 int offset = squashfs_i(inode)->offset;
53 int length = min_t(int, i_size_read(inode) - index, PAGE_CACHE_SIZE);
54 int bytes, copied;
55 void *pageaddr;
56 struct squashfs_cache_entry *entry;
57
58 TRACE("Entered squashfs_symlink_readpage, page index %ld, start block "
59 "%llx, offset %x\n", page->index, block, offset);
60
61 /*
62 * Skip index bytes into symlink metadata.
63 */
64 if (index) {
65 bytes = squashfs_read_metadata(sb, NULL, &block, &offset,
66 index);
67 if (bytes < 0) {
68 ERROR("Unable to read symlink [%llx:%x]\n",
69 squashfs_i(inode)->start,
70 squashfs_i(inode)->offset);
71 goto error_out;
72 }
73 }
74
75 /*
76 * Read length bytes from symlink metadata. Squashfs_read_metadata
77 * is not used here because it can sleep and we want to use
78 * kmap_atomic to map the page. Instead call the underlying
79 * squashfs_cache_get routine. As length bytes may overlap metadata
80 * blocks, we may need to call squashfs_cache_get multiple times.
81 */
82 for (bytes = 0; bytes < length; offset = 0, bytes += copied) {
83 entry = squashfs_cache_get(sb, msblk->block_cache, block, 0);
84 if (entry->error) {
85 ERROR("Unable to read symlink [%llx:%x]\n",
86 squashfs_i(inode)->start,
87 squashfs_i(inode)->offset);
88 squashfs_cache_put(entry);
89 goto error_out;
90 }
91
92 pageaddr = kmap_atomic(page, KM_USER0);
93 copied = squashfs_copy_data(pageaddr + bytes, entry, offset,
94 length - bytes);
95 if (copied == length - bytes)
96 memset(pageaddr + length, 0, PAGE_CACHE_SIZE - length);
97 else
98 block = entry->next_index;
99 kunmap_atomic(pageaddr, KM_USER0);
100 squashfs_cache_put(entry);
101 }
102
103 flush_dcache_page(page);
104 SetPageUptodate(page);
105 unlock_page(page);
106 return 0;
107
108error_out:
109 SetPageError(page);
110 unlock_page(page);
111 return 0;
112}
113
114
115const struct address_space_operations squashfs_symlink_aops = {
116 .readpage = squashfs_symlink_readpage
117};