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