Whamcloud - gitweb
land b1_5 onto HEAD
[fs/lustre-release.git] / lustre / llite / symlink.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (c) 2002 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre 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 Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #include <linux/fs.h>
23 #include <linux/mm.h>
24 #include <linux/stat.h>
25 #include <linux/smp_lock.h>
26 #include <linux/version.h>
27 #define DEBUG_SUBSYSTEM S_LLITE
28
29 #include <lustre_lite.h>
30 #include "llite_internal.h"
31
32 static int ll_readlink_internal(struct inode *inode,
33                                 struct ptlrpc_request **request, char **symname)
34 {
35         struct ll_inode_info *lli = ll_i2info(inode);
36         struct ll_sb_info *sbi = ll_i2sbi(inode);
37         struct ll_fid fid;
38         struct mds_body *body;
39         int rc, symlen = inode->i_size + 1;
40         ENTRY;
41
42         *request = NULL;
43
44         if (lli->lli_symlink_name) {
45                 *symname = lli->lli_symlink_name;
46                 CDEBUG(D_INODE, "using cached symlink %s\n", *symname);
47                 RETURN(0);
48         }
49
50         ll_inode2fid(&fid, inode);
51         rc = mdc_getattr(sbi->ll_mdc_exp, &fid,
52                          OBD_MD_LINKNAME, symlen, request);
53         if (rc) {
54                 if (rc != -ENOENT)
55                         CERROR("inode %lu: rc = %d\n", inode->i_ino, rc);
56                 GOTO (failed, rc);
57         }
58
59         body = lustre_msg_buf((*request)->rq_repmsg, REPLY_REC_OFF,
60                               sizeof(*body));
61         LASSERT(body != NULL);
62         LASSERT_REPSWABBED(*request, REPLY_REC_OFF);
63
64         if ((body->valid & OBD_MD_LINKNAME) == 0) {
65                 CERROR("OBD_MD_LINKNAME not set on reply\n");
66                 GOTO(failed, rc = -EPROTO);
67         }
68         
69         LASSERT(symlen != 0);
70         if (body->eadatasize != symlen) {
71                 CERROR("inode %lu: symlink length %d not expected %d\n",
72                         inode->i_ino, body->eadatasize - 1, symlen - 1);
73                 GOTO(failed, rc = -EPROTO);
74         }
75
76         *symname = lustre_msg_buf((*request)->rq_repmsg, REPLY_REC_OFF + 1,
77                                   symlen);
78         if (*symname == NULL ||
79             strnlen (*symname, symlen) != symlen - 1) {
80                 /* not full/NULL terminated */
81                 CERROR("inode %lu: symlink not NULL terminated string"
82                         "of length %d\n", inode->i_ino, symlen - 1);
83                 GOTO(failed, rc = -EPROTO);
84         }
85
86         OBD_ALLOC(lli->lli_symlink_name, symlen);
87         /* do not return an error if we cannot cache the symlink locally */
88         if (lli->lli_symlink_name) {
89                 memcpy(lli->lli_symlink_name, *symname, symlen);
90                 ptlrpc_req_finished (*request);
91                 *request = NULL;
92                 *symname = lli->lli_symlink_name;
93         }
94
95         RETURN(0);
96
97  failed:
98         ptlrpc_req_finished (*request);
99         RETURN (rc);
100 }
101
102 static int ll_readlink(struct dentry *dentry, char *buffer, int buflen)
103 {
104         struct inode *inode = dentry->d_inode;
105         struct ll_inode_info *lli = ll_i2info(inode);
106         struct ptlrpc_request *request;
107         char *symname;
108         int rc;
109         ENTRY;
110
111         CDEBUG(D_VFSTRACE, "VFS Op\n");
112         /* on symlinks lli_open_sem protects lli_symlink_name allocation/data */
113         down(&lli->lli_open_sem);
114         rc = ll_readlink_internal(inode, &request, &symname);
115         if (rc)
116                 GOTO(out, rc);
117
118         rc = vfs_readlink(dentry, buffer, buflen, symname);
119         ptlrpc_req_finished(request);
120  out:
121         up(&lli->lli_open_sem);
122         RETURN(rc);
123 }
124
125 #ifdef HAVE_COOKIE_FOLLOW_LINK
126 # define LL_FOLLOW_LINK_RETURN_TYPE void *
127 #else
128 # define LL_FOLLOW_LINK_RETURN_TYPE int
129 #endif
130
131 static LL_FOLLOW_LINK_RETURN_TYPE ll_follow_link(struct dentry *dentry, struct nameidata *nd)
132 {
133         struct inode *inode = dentry->d_inode;
134         struct ll_inode_info *lli = ll_i2info(inode);
135 #ifdef LUSTRE_KERNEL_VERSION
136         struct lookup_intent *it = ll_nd2it(nd);
137 #endif
138         struct ptlrpc_request *request;
139         int rc;
140         char *symname;
141         ENTRY;
142
143 #ifdef LUSTRE_KERNEL_VERSION
144         if (it != NULL) {
145                 int op = it->it_op;
146                 int mode = it->it_create_mode;
147
148                 ll_intent_release(it);
149                 it->it_op = op;
150                 it->it_create_mode = mode;
151         }
152 #endif
153
154         CDEBUG(D_VFSTRACE, "VFS Op\n");
155         down(&lli->lli_open_sem);
156         rc = ll_readlink_internal(inode, &request, &symname);
157         up(&lli->lli_open_sem);
158         if (rc) {
159                 path_release(nd); /* Kernel assumes that ->follow_link()
160                                      releases nameidata on error */
161                 GOTO(out, rc);
162         }
163
164 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,8))
165         rc = vfs_follow_link(nd, symname);
166 #else
167 # ifdef HAVE_COOKIE_FOLLOW_LINK
168         nd_set_link(nd, symname);
169         /* @symname may contain a pointer to the request message buffer,
170            we delay request releasing until ll_put_link then. */
171         RETURN(request);
172 # else
173         if (request != NULL) {
174                 /* falling back to recursive follow link if the request
175                  * needs to be cleaned up still. */
176                 rc = vfs_follow_link(nd, symname);
177                 GOTO(out, rc);
178         }
179         nd_set_link(nd, symname);
180         RETURN(0);
181 # endif
182 #endif
183 out:
184         ptlrpc_req_finished(request);
185 #ifdef HAVE_COOKIE_FOLLOW_LINK
186         RETURN(ERR_PTR(rc));
187 #else
188         RETURN(rc);
189 #endif
190 }
191
192 #ifdef HAVE_COOKIE_FOLLOW_LINK
193 static void ll_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
194 {
195         ptlrpc_req_finished(cookie);
196 }
197 #endif
198
199 struct inode_operations ll_fast_symlink_inode_operations = {
200         .readlink       = ll_readlink,
201         .setattr        = ll_setattr,
202 #ifdef LUSTRE_KERNEL_VERSION
203         .setattr_raw    = ll_setattr_raw,
204 #endif
205         .follow_link    = ll_follow_link,
206 #ifdef HAVE_COOKIE_FOLLOW_LINK
207         .put_link       = ll_put_link,
208 #endif
209 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
210         .revalidate_it  = ll_inode_revalidate_it,
211 #else 
212         .getattr        = ll_getattr,
213 #endif
214         .permission     = ll_inode_permission,
215         .setxattr       = ll_setxattr,
216         .getxattr       = ll_getxattr,
217         .listxattr      = ll_listxattr,
218         .removexattr    = ll_removexattr,
219 };