symlink.c 2.59 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 *  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
14
#include <linux/time.h>
Linus Torvalds's avatar
Linus Torvalds committed
15 16 17 18 19 20 21 22
#include <linux/errno.h>
#include <linux/sunrpc/clnt.h>
#include <linux/nfs.h>
#include <linux/nfs2.h>
#include <linux/nfs_fs.h>
#include <linux/pagemap.h>
#include <linux/stat.h>
#include <linux/mm.h>
Linus Torvalds's avatar
Linus Torvalds committed
23
#include <linux/slab.h>
Linus Torvalds's avatar
Linus Torvalds committed
24 25
#include <linux/string.h>
#include <linux/smp_lock.h>
26
#include <linux/namei.h>
Linus Torvalds's avatar
Linus Torvalds committed
27 28 29

/* Symlink caching in the page cache is even more simplistic
 * and straight-forward than readdir caching.
30
 *
31
 * At the beginning of the page we store pointer to struct page in question,
32 33
 * simplifying nfs_put_link() (if inode got invalidated we can't find the page
 * to be freed via pagecache lookup).
34
 * The NUL-terminated string follows immediately thereafter.
Linus Torvalds's avatar
Linus Torvalds committed
35
 */
36 37 38

struct nfs_symlink {
	struct page *page;
39
	char body[0];
40
};
41

Linus Torvalds's avatar
Linus Torvalds committed
42 43
static int nfs_symlink_filler(struct inode *inode, struct page *page)
{
44 45
	const unsigned int pgbase = offsetof(struct nfs_symlink, body);
	const unsigned int pglen = PAGE_SIZE - pgbase;
Linus Torvalds's avatar
Linus Torvalds committed
46 47 48
	int error;

	lock_kernel();
49
	error = NFS_PROTO(inode)->readlink(inode, page, pgbase, pglen);
Linus Torvalds's avatar
Linus Torvalds committed
50 51 52 53
	unlock_kernel();
	if (error < 0)
		goto error;
	SetPageUptodate(page);
Andrew Morton's avatar
Andrew Morton committed
54
	unlock_page(page);
Linus Torvalds's avatar
Linus Torvalds committed
55 56 57 58
	return 0;

error:
	SetPageError(page);
Andrew Morton's avatar
Andrew Morton committed
59
	unlock_page(page);
Linus Torvalds's avatar
Linus Torvalds committed
60 61 62
	return -EIO;
}

63
static int nfs_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds's avatar
Linus Torvalds committed
64
{
65
	struct inode *inode = dentry->d_inode;
Linus Torvalds's avatar
Linus Torvalds committed
66
	struct page *page;
67 68 69
	struct nfs_symlink *p;
	void *err = ERR_PTR(nfs_revalidate_inode(NFS_SERVER(inode), inode));
	if (err)
70
		goto read_failed;
Linus Torvalds's avatar
Linus Torvalds committed
71 72
	page = read_cache_page(&inode->i_data, 0,
				(filler_t *)nfs_symlink_filler, inode);
73 74
	if (IS_ERR(page)) {
		err = page;
Linus Torvalds's avatar
Linus Torvalds committed
75
		goto read_failed;
76 77 78
	}
	if (!PageUptodate(page)) {
		err = ERR_PTR(-EIO);
Linus Torvalds's avatar
Linus Torvalds committed
79
		goto getlink_read_error;
80
	}
Linus Torvalds's avatar
Linus Torvalds committed
81
	p = kmap(page);
82 83 84
	p->page = page;
	nd_set_link(nd, p->body);
	return 0;
85

Linus Torvalds's avatar
Linus Torvalds committed
86 87 88
getlink_read_error:
	page_cache_release(page);
read_failed:
89 90
	nd_set_link(nd, err);
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
91 92
}

93
static void nfs_put_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds's avatar
Linus Torvalds committed
94
{
95 96 97 98 99 100 101
	char *s = nd_get_link(nd);
	if (!IS_ERR(s)) {
		struct nfs_symlink *p;
		struct page *page;

		p = container_of(s, struct nfs_symlink, body[0]);
		page = p->page;
Linus Torvalds's avatar
Linus Torvalds committed
102 103 104 105 106 107 108 109 110 111

		kunmap(page);
		page_cache_release(page);
	}
}

/*
 * symlinks can't do much...
 */
struct inode_operations nfs_symlink_inode_operations = {
112
	.readlink	= generic_readlink,
113
	.follow_link	= nfs_follow_link,
114
	.put_link	= nfs_put_link,
115 116
	.getattr	= nfs_getattr,
	.setattr	= nfs_setattr,
Linus Torvalds's avatar
Linus Torvalds committed
117
};