summaryrefslogtreecommitdiffstats
path: root/fs/nfs/symlink.c
blob: 1b1705ef2d23e444fb12fd2d6ec948053c39ff77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
 *  linux/fs/nfs/symlink.c
 *
 *  Copyright (C) 1992  Rick Sladkey
 *
 *  Optimization changes Copyright (C) 1994 Florian La Roche
 *
 *  Jun 7 1999, cache symlink lookups in the page cache.  -DaveM
 *
 *  nfs symlink handling code
 */

#define NFS_NEED_XDR_TYPES
#include <linux/sched.h>
#include <linux/errno.h>
#include <linux/sunrpc/clnt.h>
#include <linux/nfs_fs.h>
#include <linux/nfs.h>
#include <linux/pagemap.h>
#include <linux/stat.h>
#include <linux/mm.h>
#include <linux/malloc.h>
#include <linux/string.h>

#include <asm/uaccess.h>

static int nfs_readlink(struct dentry *, char *, int);
static struct dentry *nfs_follow_link(struct dentry *, struct dentry *, unsigned int);

/*
 * symlinks can't do much...
 */
struct inode_operations nfs_symlink_inode_operations = {
	NULL,			/* no file-operations */
	NULL,			/* create */
	NULL,			/* lookup */
	NULL,			/* link */
	NULL,			/* unlink */
	NULL,			/* symlink */
	NULL,			/* mkdir */
	NULL,			/* rmdir */
	NULL,			/* mknod */
	NULL,			/* rename */
	nfs_readlink,		/* readlink */
	nfs_follow_link,	/* follow_link */
	NULL,			/* get_block */
	NULL,			/* readpage */
	NULL,			/* writepage */
	NULL,			/* flushpage */
	NULL,			/* truncate */
	NULL,			/* permission */
	NULL,			/* smap */
	NULL			/* revalidate */
};

/* Symlink caching in the page cache is even more simplistic
 * and straight-forward than readdir caching.
 */
static struct page *try_to_get_symlink_page(struct dentry *dentry, struct inode *inode)
{
	struct nfs_readlinkargs rl_args;
	struct page *page, **hash, *page_cache;

	page = NULL;
	page_cache = page_cache_alloc();
	if (!page_cache)
		goto out;

	hash = page_hash(&inode->i_data, 0);
repeat:
	page = __find_lock_page(&inode->i_data, 0, hash);
	if (page) {
		page_cache_free(page_cache);
		goto unlock_out;
	}

	page = page_cache;
	if (add_to_page_cache_unique(page, &inode->i_data, 0, hash)) {
		page_cache_release(page);
		goto repeat;
	}

	/* We place the length at the beginning of the page,
	 * in host byte order, followed by the string.  The
	 * XDR response verification will NULL terminate it.
	 */
	rl_args.fh = NFS_FH(dentry);
	rl_args.buffer = (const void *)page_address(page_cache);
	if (rpc_call(NFS_CLIENT(inode), NFSPROC_READLINK,
		     &rl_args, NULL, 0) < 0)
		goto error;
	SetPageUptodate(page);
unlock_out:
	UnlockPage(page);
out:
	return page;

error:
	SetPageError(page);
	goto unlock_out;
}

static int nfs_readlink(struct dentry *dentry, char *buffer, int buflen)
{
	struct inode *inode = dentry->d_inode;
	struct page *page;
	u32 *p, len;

	/* Caller revalidated the directory inode already. */
	page = find_get_page(&inode->i_data, 0);
	if (!page)
		goto no_readlink_page;
	if (!Page_Uptodate(page))
		goto readlink_read_error;
success:
	p = (u32 *) page_address(page);
	len = *p++;
	if (len > buflen)
		len = buflen;
	copy_to_user(buffer, p, len);
	page_cache_release(page);
	return len;

no_readlink_page:
	page = try_to_get_symlink_page(dentry, inode);
	if (!page)
		goto no_page;
	if (Page_Uptodate(page))
		goto success;
readlink_read_error:
	page_cache_release(page);
no_page:
	return -EIO;
}

static struct dentry *
nfs_follow_link(struct dentry *dentry, struct dentry *base, unsigned int follow)
{
	struct dentry *result;
	struct inode *inode = dentry->d_inode;
	struct page *page;
	u32 *p;

	/* Caller revalidated the directory inode already. */
	page = find_get_page(&inode->i_data, 0);
	if (!page)
		goto no_followlink_page;
	if (!Page_Uptodate(page))
		goto followlink_read_error;
success:
	p = (u32 *) page_address(page);
	result = lookup_dentry((char *) (p + 1), base, follow);
	page_cache_release(page);
	return result;

no_followlink_page:
	page = try_to_get_symlink_page(dentry, inode);
	if (!page)
		goto no_page;
	if (Page_Uptodate(page))
		goto success;
followlink_read_error:
	page_cache_release(page);
no_page:
	return ERR_PTR(-EIO);
}