Whamcloud - gitweb
b=22310 temporary fix: align readahead window end to 1M rpc boundary.
[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
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
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 md_op_data *op_data;
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         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, symlen,
66                                      LUSTRE_OPC_ANY, NULL);
67         op_data->op_valid = OBD_MD_LINKNAME;
68         rc = md_getattr(sbi->ll_md_exp, op_data, request);
69         ll_finish_md_op_data(op_data);
70         if (rc) {
71                 if (rc != -ENOENT)
72                         CERROR("inode %lu: rc = %d\n", inode->i_ino, rc);
73                 GOTO (failed, rc);
74         }
75
76         body = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
77         LASSERT(body != NULL);
78         if ((body->valid & OBD_MD_LINKNAME) == 0) {
79                 CERROR("OBD_MD_LINKNAME not set on reply\n");
80                 GOTO(failed, rc = -EPROTO);
81         }
82
83         LASSERT(symlen != 0);
84         if (body->eadatasize != symlen) {
85                 CERROR("inode %lu: symlink length %d not expected %d\n",
86                         inode->i_ino, body->eadatasize - 1, symlen - 1);
87                 GOTO(failed, rc = -EPROTO);
88         }
89
90         *symname = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_MD);
91         if (*symname == NULL ||
92             strnlen(*symname, symlen) != symlen - 1) {
93                 /* not full/NULL terminated */
94                 CERROR("inode %lu: symlink not NULL terminated string"
95                         "of length %d\n", inode->i_ino, symlen - 1);
96                 GOTO(failed, rc = -EPROTO);
97         }
98
99         OBD_ALLOC(lli->lli_symlink_name, symlen);
100         /* do not return an error if we cannot cache the symlink locally */
101         if (lli->lli_symlink_name) {
102                 memcpy(lli->lli_symlink_name, *symname, symlen);
103                 *symname = lli->lli_symlink_name;
104         }
105         RETURN(0);
106
107 failed:
108         RETURN (rc);
109 }
110
111 static int ll_readlink(struct dentry *dentry, char *buffer, int buflen)
112 {
113         struct inode *inode = dentry->d_inode;
114         struct ptlrpc_request *request;
115         char *symname;
116         int rc;
117         ENTRY;
118
119         CDEBUG(D_VFSTRACE, "VFS Op\n");
120         /* on symlinks lli_open_sem protects lli_symlink_name allocation/data */
121         ll_inode_size_lock(inode, 0);
122         rc = ll_readlink_internal(inode, &request, &symname);
123         if (rc)
124                 GOTO(out, rc);
125
126         rc = vfs_readlink(dentry, buffer, buflen, symname);
127  out:
128         ptlrpc_req_finished(request);
129         ll_inode_size_unlock(inode, 0);
130         RETURN(rc);
131 }
132
133 #ifdef HAVE_COOKIE_FOLLOW_LINK
134 # define LL_FOLLOW_LINK_RETURN_TYPE void *
135 #else
136 # define LL_FOLLOW_LINK_RETURN_TYPE int
137 #endif
138
139 static LL_FOLLOW_LINK_RETURN_TYPE ll_follow_link(struct dentry *dentry,
140                                                  struct nameidata *nd)
141 {
142         struct inode *inode = dentry->d_inode;
143 #ifdef HAVE_VFS_INTENT_PATCHES
144         struct lookup_intent *it = ll_nd2it(nd);
145 #endif
146         struct ptlrpc_request *request = NULL;
147         int rc;
148         char *symname;
149         ENTRY;
150
151 #ifdef HAVE_VFS_INTENT_PATCHES
152         if (it != NULL) {
153                 int op = it->it_op;
154                 int mode = it->it_create_mode;
155
156                 ll_intent_release(it);
157                 it->it_op = op;
158                 it->it_create_mode = mode;
159         }
160 #endif
161
162         CDEBUG(D_VFSTRACE, "VFS Op\n");
163         /* Limit the recursive symlink depth to 5 instead of default
164          * 8 links when kernel has 4k stack to prevent stack overflow.
165          * For 8k stacks we need to limit it to 7 for local servers. */
166         if (THREAD_SIZE < 8192 && current->link_count >= 6) {
167                 rc = -ELOOP;
168         } else if (THREAD_SIZE == 8192 && current->link_count >= 8) {
169                 rc = -ELOOP;
170         } else {
171                 ll_inode_size_lock(inode, 0);
172                 rc = ll_readlink_internal(inode, &request, &symname);
173                 ll_inode_size_unlock(inode, 0);
174         }
175         if (rc) {
176                 cfs_path_put(nd); /* Kernel assumes that ->follow_link()
177                                      releases nameidata on error */
178                 GOTO(out, rc);
179         }
180
181 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,8))
182         rc = vfs_follow_link(nd, symname);
183 #else
184 #ifdef HAVE_COOKIE_FOLLOW_LINK
185         nd_set_link(nd, symname);
186         /* @symname may contain a pointer to the request message buffer,
187            we delay request releasing until ll_put_link then. */
188         RETURN(request);
189 #else
190         if (lli->lli_symlink_name == NULL) {
191                 /* falling back to recursive follow link if the request
192                  * needs to be cleaned up still. */
193                 rc = vfs_follow_link(nd, symname);
194                 GOTO(out, rc);
195         }
196         nd_set_link(nd, symname);
197         RETURN(0);
198 #endif
199 #endif
200 out:
201         ptlrpc_req_finished(request);
202 #ifdef HAVE_COOKIE_FOLLOW_LINK
203         RETURN(ERR_PTR(rc));
204 #else
205         RETURN(rc);
206 #endif
207 }
208
209 #ifdef HAVE_COOKIE_FOLLOW_LINK
210 static void ll_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
211 {
212         ptlrpc_req_finished(cookie);
213 }
214 #endif
215
216 struct inode_operations ll_fast_symlink_inode_operations = {
217         .readlink       = ll_readlink,
218         .setattr        = ll_setattr,
219 #ifdef HAVE_VFS_INTENT_PATCHES
220         .setattr_raw    = ll_setattr_raw,
221 #endif
222         .follow_link    = ll_follow_link,
223 #ifdef HAVE_COOKIE_FOLLOW_LINK
224         .put_link       = ll_put_link,
225 #endif
226         .getattr        = ll_getattr,
227         .permission     = ll_inode_permission,
228         .setxattr       = ll_setxattr,
229         .getxattr       = ll_getxattr,
230         .listxattr      = ll_listxattr,
231         .removexattr    = ll_removexattr,
232 };