Whamcloud - gitweb
b=16098
[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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see [sun.com URL with a
20  * copy of GPLv2].
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #include <linux/fs.h>
38 #include <linux/mm.h>
39 #include <linux/stat.h>
40 #include <linux/smp_lock.h>
41 #include <linux/version.h>
42 #define DEBUG_SUBSYSTEM S_LLITE
43
44 #include <lustre_lite.h>
45 #include "llite_internal.h"
46
47 static int ll_readlink_internal(struct inode *inode,
48                                 struct ptlrpc_request **request, char **symname)
49 {
50         struct ll_inode_info *lli = ll_i2info(inode);
51         struct ll_sb_info *sbi = ll_i2sbi(inode);
52         int rc, symlen = i_size_read(inode) + 1;
53         struct mdt_body *body;
54         struct obd_capa *oc;
55         ENTRY;
56
57         *request = NULL;
58
59         if (lli->lli_symlink_name) {
60                 *symname = lli->lli_symlink_name;
61                 CDEBUG(D_INODE, "using cached symlink %s\n", *symname);
62                 RETURN(0);
63         }
64
65         oc = ll_mdscapa_get(inode);
66         rc = md_getattr(sbi->ll_md_exp, ll_inode2fid(inode), oc,
67                         OBD_MD_LINKNAME, symlen, request);
68         capa_put(oc);
69         if (rc) {
70                 if (rc != -ENOENT)
71                         CERROR("inode %lu: rc = %d\n", inode->i_ino, rc);
72                 GOTO (failed, rc);
73         }
74
75         body = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
76         LASSERT(body != NULL);
77         if ((body->valid & OBD_MD_LINKNAME) == 0) {
78                 CERROR("OBD_MD_LINKNAME not set on reply\n");
79                 GOTO(failed, rc = -EPROTO);
80         }
81         
82         LASSERT(symlen != 0);
83         if (body->eadatasize != symlen) {
84                 CERROR("inode %lu: symlink length %d not expected %d\n",
85                         inode->i_ino, body->eadatasize - 1, symlen - 1);
86                 GOTO(failed, rc = -EPROTO);
87         }
88
89         *symname = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_MD);
90         if (*symname == NULL ||
91             strnlen(*symname, symlen) != symlen - 1) {
92                 /* not full/NULL terminated */
93                 CERROR("inode %lu: symlink not NULL terminated string"
94                         "of length %d\n", inode->i_ino, symlen - 1);
95                 GOTO(failed, rc = -EPROTO);
96         }
97
98         OBD_ALLOC(lli->lli_symlink_name, symlen);
99         /* do not return an error if we cannot cache the symlink locally */
100         if (lli->lli_symlink_name) {
101                 memcpy(lli->lli_symlink_name, *symname, symlen);
102                 ptlrpc_req_finished (*request);
103                 *request = NULL;
104                 *symname = lli->lli_symlink_name;
105         }
106
107         RETURN(0);
108
109  failed:
110         ptlrpc_req_finished (*request);
111         RETURN (rc);
112 }
113
114 static int ll_readlink(struct dentry *dentry, char *buffer, int buflen)
115 {
116         struct inode *inode = dentry->d_inode;
117         struct ll_inode_info *lli = ll_i2info(inode);
118         struct ptlrpc_request *request;
119         char *symname;
120         int rc;
121         ENTRY;
122
123         CDEBUG(D_VFSTRACE, "VFS Op\n");
124         /* on symlinks lli_open_sem protects lli_symlink_name allocation/data */
125         down(&lli->lli_size_sem);
126         rc = ll_readlink_internal(inode, &request, &symname);
127         if (rc)
128                 GOTO(out, rc);
129
130         rc = vfs_readlink(dentry, buffer, buflen, symname);
131         ptlrpc_req_finished(request);
132  out:
133         up(&lli->lli_size_sem);
134         RETURN(rc);
135 }
136
137 #ifdef HAVE_COOKIE_FOLLOW_LINK
138 # define LL_FOLLOW_LINK_RETURN_TYPE void *
139 #else
140 # define LL_FOLLOW_LINK_RETURN_TYPE int
141 #endif
142
143 static LL_FOLLOW_LINK_RETURN_TYPE ll_follow_link(struct dentry *dentry,
144                                                  struct nameidata *nd)
145 {
146         struct inode *inode = dentry->d_inode;
147         struct ll_inode_info *lli = ll_i2info(inode);
148 #ifdef HAVE_VFS_INTENT_PATCHES
149         struct lookup_intent *it = ll_nd2it(nd);
150 #endif
151         struct ptlrpc_request *request;
152         int rc;
153         char *symname;
154         ENTRY;
155
156 #ifdef HAVE_VFS_INTENT_PATCHES
157         if (it != NULL) {
158                 int op = it->it_op;
159                 int mode = it->it_create_mode;
160
161                 ll_intent_release(it);
162                 it->it_op = op;
163                 it->it_create_mode = mode;
164         }
165 #endif
166
167         CDEBUG(D_VFSTRACE, "VFS Op\n");
168         down(&lli->lli_size_sem);
169         rc = ll_readlink_internal(inode, &request, &symname);
170         up(&lli->lli_size_sem);
171         if (rc) {
172                 path_release(nd); /* Kernel assumes that ->follow_link()
173                                      releases nameidata on error */
174                 GOTO(out, rc);
175         }
176
177 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,8))
178         rc = vfs_follow_link(nd, symname);
179 #else
180 #ifdef HAVE_COOKIE_FOLLOW_LINK
181         nd_set_link(nd, symname);
182         /* @symname may contain a pointer to the request message buffer,
183            we delay request releasing until ll_put_link then. */
184         RETURN(request);
185 #else
186         if (request != NULL) {
187                 /* falling back to recursive follow link if the request
188                  * needs to be cleaned up still. */
189         rc = vfs_follow_link(nd, symname);
190                 GOTO(out, rc);
191         }
192         nd_set_link(nd, symname);
193         RETURN(0);
194 #endif
195 #endif
196 out:
197         ptlrpc_req_finished(request);
198 #ifdef HAVE_COOKIE_FOLLOW_LINK
199         RETURN(ERR_PTR(rc));
200 #else
201         RETURN(rc);
202 #endif
203 }
204
205 #ifdef HAVE_COOKIE_FOLLOW_LINK
206 static void ll_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
207 {
208         ptlrpc_req_finished(cookie);
209 }
210 #endif
211
212 struct inode_operations ll_fast_symlink_inode_operations = {
213         .readlink       = ll_readlink,
214         .setattr        = ll_setattr,
215 #ifdef HAVE_VFS_INTENT_PATCHES
216         .setattr_raw    = ll_setattr_raw,
217 #endif
218         .follow_link    = ll_follow_link,
219 #ifdef HAVE_COOKIE_FOLLOW_LINK
220         .put_link       = ll_put_link,
221 #endif
222         .getattr        = ll_getattr,
223         .permission     = ll_inode_permission,
224         .setxattr       = ll_setxattr,
225         .getxattr       = ll_getxattr,
226         .listxattr      = ll_listxattr,
227         .removexattr    = ll_removexattr,
228 };