ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / fs / nfs / symlink.c
1 /*
2  *  linux/fs/nfs/symlink.c
3  *
4  *  Copyright (C) 1992  Rick Sladkey
5  *
6  *  Optimization changes Copyright (C) 1994 Florian La Roche
7  *
8  *  Jun 7 1999, cache symlink lookups in the page cache.  -DaveM
9  *
10  *  nfs symlink handling code
11  */
12
13 #define NFS_NEED_XDR_TYPES
14 #include <linux/time.h>
15 #include <linux/errno.h>
16 #include <linux/sunrpc/clnt.h>
17 #include <linux/nfs.h>
18 #include <linux/nfs2.h>
19 #include <linux/nfs_fs.h>
20 #include <linux/pagemap.h>
21 #include <linux/stat.h>
22 #include <linux/mm.h>
23 #include <linux/slab.h>
24 #include <linux/string.h>
25 #include <linux/smp_lock.h>
26
27 /* Symlink caching in the page cache is even more simplistic
28  * and straight-forward than readdir caching.
29  */
30 static int nfs_symlink_filler(struct inode *inode, struct page *page)
31 {
32         int error;
33
34         /* We place the length at the beginning of the page,
35          * in host byte order, followed by the string.  The
36          * XDR response verification will NULL terminate it.
37          */
38         lock_kernel();
39         error = NFS_PROTO(inode)->readlink(inode, page);
40         unlock_kernel();
41         if (error < 0)
42                 goto error;
43         SetPageUptodate(page);
44         unlock_page(page);
45         return 0;
46
47 error:
48         SetPageError(page);
49         unlock_page(page);
50         return -EIO;
51 }
52
53 static char *nfs_getlink(struct inode *inode, struct page **ppage)
54 {
55         struct page *page;
56         u32 *p;
57
58         page = ERR_PTR(nfs_revalidate_inode(NFS_SERVER(inode), inode));
59         if (page)
60                 goto read_failed;
61         page = read_cache_page(&inode->i_data, 0,
62                                 (filler_t *)nfs_symlink_filler, inode);
63         if (IS_ERR(page))
64                 goto read_failed;
65         if (!PageUptodate(page))
66                 goto getlink_read_error;
67         *ppage = page;
68         p = kmap(page);
69         return (char*)(p+1);
70
71 getlink_read_error:
72         page_cache_release(page);
73         page = ERR_PTR(-EIO);
74 read_failed:
75         return (char*)page;
76 }
77
78 static int nfs_readlink(struct dentry *dentry, char __user *buffer, int buflen)
79 {
80         struct inode *inode = dentry->d_inode;
81         struct page *page = NULL;
82         int res = vfs_readlink(dentry,buffer,buflen,nfs_getlink(inode,&page));
83         if (page) {
84                 kunmap(page);
85                 page_cache_release(page);
86         }
87         return res;
88 }
89
90 static int nfs_follow_link(struct dentry *dentry, struct nameidata *nd)
91 {
92         struct inode *inode = dentry->d_inode;
93         struct page *page = NULL;
94         int res = vfs_follow_link(nd, nfs_getlink(inode,&page));
95         if (page) {
96                 kunmap(page);
97                 page_cache_release(page);
98         }
99         return res;
100 }
101
102 /*
103  * symlinks can't do much...
104  */
105 struct inode_operations nfs_symlink_inode_operations = {
106         .readlink       = nfs_readlink,
107         .follow_link    = nfs_follow_link,
108         .getattr        = nfs_getattr,
109         .setattr        = nfs_setattr,
110 };