f6986bd79e75b5ae8997fc4041ac1b10a77fe8a9
[linux-2.6.git] / fs / ocfs2 / symlink.c
1 /* -*- mode: c; c-basic-offset: 8; -*-
2  * vim: noexpandtab sw=8 ts=8 sts=0:
3  *
4  *  linux/cluster/ssi/cfs/symlink.c
5  *
6  *      This program is free software; you can redistribute it and/or
7  *      modify it under the terms of the GNU General Public License as
8  *      published by the Free Software Foundation; either version 2 of
9  *      the License, or (at your option) any later version.
10  *
11  *      This program is distributed in the hope that it will be useful,
12  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *      MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE
14  *      or NON INFRINGEMENT.  See the GNU General Public License for more
15  *      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, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  *      Questions/Comments/Bugfixes to ssic-linux-devel@lists.sourceforge.net
22  *
23  *  Copyright (C) 1992  Rick Sladkey
24  *
25  *  Optimization changes Copyright (C) 1994 Florian La Roche
26  *
27  *  Jun 7 1999, cache symlink lookups in the page cache.  -DaveM
28  *
29  *  Portions Copyright (C) 2001 Compaq Computer Corporation
30  *
31  *  ocfs2 symlink handling code.
32  *
33  *  Copyright (C) 2004, 2005 Oracle.
34  *
35  */
36
37 #include <linux/fs.h>
38 #include <linux/types.h>
39 #include <linux/slab.h>
40 #include <linux/pagemap.h>
41 #include <linux/utsname.h>
42
43 #define MLOG_MASK_PREFIX ML_NAMEI
44 #include <cluster/masklog.h>
45
46 #include "ocfs2.h"
47
48 #include "alloc.h"
49 #include "file.h"
50 #include "inode.h"
51 #include "journal.h"
52 #include "symlink.h"
53
54 #include "buffer_head_io.h"
55
56 static char *ocfs2_page_getlink(struct dentry * dentry,
57                                 struct page **ppage);
58 static char *ocfs2_fast_symlink_getlink(struct inode *inode,
59                                         struct buffer_head **bh);
60
61 /* get the link contents into pagecache */
62 static char *ocfs2_page_getlink(struct dentry * dentry,
63                                 struct page **ppage)
64 {
65         struct page * page;
66         struct address_space *mapping = dentry->d_inode->i_mapping;
67         page = read_cache_page(mapping, 0,
68                                (filler_t *)mapping->a_ops->readpage, NULL);
69         if (IS_ERR(page))
70                 goto sync_fail;
71         wait_on_page_locked(page);
72         if (!PageUptodate(page))
73                 goto async_fail;
74         *ppage = page;
75         return kmap(page);
76
77 async_fail:
78         page_cache_release(page);
79         return ERR_PTR(-EIO);
80
81 sync_fail:
82         return (char*)page;
83 }
84
85 static char *ocfs2_fast_symlink_getlink(struct inode *inode,
86                                         struct buffer_head **bh)
87 {
88         int status;
89         char *link = NULL;
90         struct ocfs2_dinode *fe;
91
92         mlog_entry_void();
93
94         status = ocfs2_read_block(OCFS2_SB(inode->i_sb),
95                                   OCFS2_I(inode)->ip_blkno,
96                                   bh,
97                                   OCFS2_BH_CACHED,
98                                   inode);
99         if (status < 0) {
100                 mlog_errno(status);
101                 link = ERR_PTR(status);
102                 goto bail;
103         }
104
105         fe = (struct ocfs2_dinode *) (*bh)->b_data;
106         link = (char *) fe->id2.i_symlink;
107 bail:
108         mlog_exit(status);
109
110         return link;
111 }
112
113 static int ocfs2_readlink(struct dentry *dentry,
114                           char __user *buffer,
115                           int buflen)
116 {
117         int ret;
118         char *link;
119         struct buffer_head *bh = NULL;
120         struct inode *inode = dentry->d_inode;
121
122         mlog_entry_void();
123
124         link = ocfs2_fast_symlink_getlink(inode, &bh);
125         if (IS_ERR(link)) {
126                 ret = PTR_ERR(link);
127                 goto out;
128         }
129
130         ret = vfs_readlink(dentry, buffer, buflen, link);
131
132         brelse(bh);
133 out:
134         mlog_exit(ret);
135         return ret;
136 }
137
138 static void *ocfs2_follow_link(struct dentry *dentry,
139                                struct nameidata *nd)
140 {
141         int status;
142         char *link;
143         struct inode *inode = dentry->d_inode;
144         struct page *page = NULL;
145         struct buffer_head *bh = NULL;
146         
147         if (ocfs2_inode_is_fast_symlink(inode))
148                 link = ocfs2_fast_symlink_getlink(inode, &bh);
149         else
150                 link = ocfs2_page_getlink(dentry, &page);
151         if (IS_ERR(link)) {
152                 status = PTR_ERR(link);
153                 mlog_errno(status);
154                 goto bail;
155         }
156
157         status = vfs_follow_link(nd, link);
158         if (status)
159                 mlog_errno(status);
160 bail:
161         if (page) {
162                 kunmap(page);
163                 page_cache_release(page);
164         }
165         if (bh)
166                 brelse(bh);
167
168         return ERR_PTR(status);
169 }
170
171 struct inode_operations ocfs2_symlink_inode_operations = {
172         .readlink       = page_readlink,
173         .follow_link    = ocfs2_follow_link,
174         .getattr        = ocfs2_getattr,
175 };
176 struct inode_operations ocfs2_fast_symlink_inode_operations = {
177         .readlink       = ocfs2_readlink,
178         .follow_link    = ocfs2_follow_link,
179         .getattr        = ocfs2_getattr,
180 };