Whamcloud - gitweb
Branch 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         int rc, symlen = i_size_read(inode) + 1;
38         struct mdt_body *body;
39         struct obd_capa *oc;
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         oc = ll_mdscapa_get(inode);
51         rc = md_getattr(sbi->ll_md_exp, ll_inode2fid(inode), oc,
52                         OBD_MD_LINKNAME, symlen, request);
53         capa_put(oc);
54         if (rc) {
55                 if (rc != -ENOENT)
56                         CERROR("inode %lu: rc = %d\n", inode->i_ino, rc);
57                 GOTO (failed, rc);
58         }
59
60         body = lustre_msg_buf((*request)->rq_repmsg, REPLY_REC_OFF,
61                               sizeof(*body));
62         LASSERT(body != NULL);
63         LASSERT(lustre_rep_swabbed(*request, REPLY_REC_OFF));
64
65         if ((body->valid & OBD_MD_LINKNAME) == 0) {
66                 CERROR("OBD_MD_LINKNAME not set on reply\n");
67                 GOTO(failed, rc = -EPROTO);
68         }
69         
70         LASSERT(symlen != 0);
71         if (body->eadatasize != symlen) {
72                 CERROR("inode %lu: symlink length %d not expected %d\n",
73                         inode->i_ino, body->eadatasize - 1, symlen - 1);
74                 GOTO(failed, rc = -EPROTO);
75         }
76
77         *symname = lustre_msg_buf((*request)->rq_repmsg, REPLY_REC_OFF + 1,
78                                   symlen);
79         if (*symname == NULL ||
80             strnlen (*symname, symlen) != symlen - 1) {
81                 /* not full/NULL terminated */
82                 CERROR("inode %lu: symlink not NULL terminated string"
83                         "of length %d\n", inode->i_ino, symlen - 1);
84                 GOTO(failed, rc = -EPROTO);
85         }
86
87         OBD_ALLOC(lli->lli_symlink_name, symlen);
88         /* do not return an error if we cannot cache the symlink locally */
89         if (lli->lli_symlink_name) {
90                 memcpy(lli->lli_symlink_name, *symname, symlen);
91                 ptlrpc_req_finished (*request);
92                 *request = NULL;
93                 *symname = lli->lli_symlink_name;
94         }
95
96         RETURN(0);
97
98  failed:
99         ptlrpc_req_finished (*request);
100         RETURN (rc);
101 }
102
103 static int ll_readlink(struct dentry *dentry, char *buffer, int buflen)
104 {
105         struct inode *inode = dentry->d_inode;
106         struct ll_inode_info *lli = ll_i2info(inode);
107         struct ptlrpc_request *request;
108         char *symname;
109         int rc;
110         ENTRY;
111
112         CDEBUG(D_VFSTRACE, "VFS Op\n");
113         /* on symlinks lli_open_sem protects lli_symlink_name allocation/data */
114         down(&lli->lli_size_sem);
115         rc = ll_readlink_internal(inode, &request, &symname);
116         if (rc)
117                 GOTO(out, rc);
118
119         rc = vfs_readlink(dentry, buffer, buflen, symname);
120         ptlrpc_req_finished(request);
121  out:
122         up(&lli->lli_size_sem);
123         RETURN(rc);
124 }
125
126 #ifdef HAVE_COOKIE_FOLLOW_LINK
127 # define LL_FOLLOW_LINK_RETURN_TYPE void *
128 #else
129 # define LL_FOLLOW_LINK_RETURN_TYPE int
130 #endif
131
132 static LL_FOLLOW_LINK_RETURN_TYPE ll_follow_link(struct dentry *dentry,
133                                                  struct nameidata *nd)
134 {
135         struct inode *inode = dentry->d_inode;
136         struct ll_inode_info *lli = ll_i2info(inode);
137 #ifdef HAVE_VFS_INTENT_PATCHES
138         struct lookup_intent *it = ll_nd2it(nd);
139 #endif
140         struct ptlrpc_request *request;
141         int rc;
142         char *symname;
143         ENTRY;
144
145 #ifdef HAVE_VFS_INTENT_PATCHES
146         if (it != NULL) {
147                 int op = it->it_op;
148                 int mode = it->it_create_mode;
149
150                 ll_intent_release(it);
151                 it->it_op = op;
152                 it->it_create_mode = mode;
153         }
154 #endif
155
156         CDEBUG(D_VFSTRACE, "VFS Op\n");
157         down(&lli->lli_size_sem);
158         rc = ll_readlink_internal(inode, &request, &symname);
159         up(&lli->lli_size_sem);
160         if (rc) {
161                 path_release(nd); /* Kernel assumes that ->follow_link()
162                                      releases nameidata on error */
163                 GOTO(out, rc);
164         }
165
166 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,8))
167         rc = vfs_follow_link(nd, symname);
168 #else
169 #ifdef HAVE_COOKIE_FOLLOW_LINK
170         nd_set_link(nd, symname);
171         /* @symname may contain a pointer to the request message buffer,
172            we delay request releasing until ll_put_link then. */
173         RETURN(request);
174 #else
175         if (request != NULL) {
176                 /* falling back to recursive follow link if the request
177                  * needs to be cleaned up still. */
178         rc = vfs_follow_link(nd, symname);
179                 GOTO(out, rc);
180         }
181         nd_set_link(nd, symname);
182         RETURN(0);
183 #endif
184 #endif
185 out:
186         ptlrpc_req_finished(request);
187 #ifdef HAVE_COOKIE_FOLLOW_LINK
188         RETURN(ERR_PTR(rc));
189 #else
190         RETURN(rc);
191 #endif
192 }
193
194 #ifdef HAVE_COOKIE_FOLLOW_LINK
195 static void ll_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
196 {
197         ptlrpc_req_finished(cookie);
198 }
199 #endif
200
201 struct inode_operations ll_fast_symlink_inode_operations = {
202         .readlink       = ll_readlink,
203         .setattr        = ll_setattr,
204 #ifdef HAVE_VFS_INTENT_PATCHES
205         .setattr_raw    = ll_setattr_raw,
206 #endif
207         .follow_link    = ll_follow_link,
208 #ifdef HAVE_COOKIE_FOLLOW_LINK
209         .put_link       = ll_put_link,
210 #endif
211         .getattr        = ll_getattr,
212         .permission     = ll_inode_permission,
213         .setxattr       = ll_setxattr,
214         .getxattr       = ll_getxattr,
215         .listxattr      = ll_listxattr,
216         .removexattr    = ll_removexattr,
217 };