Whamcloud - gitweb
Merge "LU-9121 lnet: User Defined Selection Policy (UDSP)"
[fs/lustre-release.git] / lustre / llite / symlink.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2014, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32
33 #include <linux/fs.h>
34 #include <linux/mm.h>
35 #include <linux/stat.h>
36 #include <linux/version.h>
37 #define DEBUG_SUBSYSTEM S_LLITE
38
39 #include "llite_internal.h"
40
41 /* Must be called with lli_size_mutex locked */
42 static int ll_readlink_internal(struct inode *inode,
43                                 struct ptlrpc_request **request, char **symname)
44 {
45         struct ll_inode_info *lli = ll_i2info(inode);
46         struct ll_sb_info *sbi = ll_i2sbi(inode);
47         int rc, symlen = i_size_read(inode) + 1;
48         struct mdt_body *body;
49         struct md_op_data *op_data;
50
51         ENTRY;
52
53         *request = NULL;
54
55         if (lli->lli_symlink_name) {
56                 int print_limit = min_t(int, PAGE_SIZE - 128, symlen);
57
58                 *symname = lli->lli_symlink_name;
59                 /*
60                  * If the total CDEBUG() size is larger than a page, it
61                  * will print a warning to the console, avoid this by
62                  * printing just the last part of the symlink.
63                  */
64                 CDEBUG(D_INODE, "using cached symlink %s%.*s, len = %d\n",
65                        print_limit < symlen ? "..." : "", print_limit,
66                        (*symname) + symlen - print_limit, symlen);
67                 RETURN(0);
68         }
69
70         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, symlen,
71                                      LUSTRE_OPC_ANY, NULL);
72         if (IS_ERR(op_data))
73                 RETURN(PTR_ERR(op_data));
74
75         op_data->op_valid = OBD_MD_LINKNAME;
76         rc = md_getattr(sbi->ll_md_exp, op_data, request);
77         ll_finish_md_op_data(op_data);
78         if (rc) {
79                 if (rc != -ENOENT)
80                         CERROR("%s: inode "DFID": rc = %d\n",
81                                ll_i2sbi(inode)->ll_fsname,
82                                PFID(ll_inode2fid(inode)), rc);
83                 GOTO(failed, rc);
84         }
85
86         body = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
87         LASSERT(body != NULL);
88         if ((body->mbo_valid & OBD_MD_LINKNAME) == 0) {
89                 CERROR("OBD_MD_LINKNAME not set on reply\n");
90                 GOTO(failed, rc = -EPROTO);
91         }
92
93         LASSERT(symlen != 0);
94         if (body->mbo_eadatasize != symlen) {
95                 CERROR("%s: inode "DFID": symlink length %d not expected %d\n",
96                        sbi->ll_fsname, PFID(ll_inode2fid(inode)),
97                        body->mbo_eadatasize - 1, symlen - 1);
98                 GOTO(failed, rc = -EPROTO);
99         }
100
101         *symname = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_MD);
102         if (!*symname || strnlen(*symname, symlen) != symlen - 1) {
103                 /* not full/NULL terminated */
104                 CERROR("%s: inode "DFID": symlink not NULL terminated string of length %d\n",
105                        sbi->ll_fsname,
106                        PFID(ll_inode2fid(inode)), symlen - 1);
107                 GOTO(failed, rc = -EPROTO);
108         }
109
110         OBD_ALLOC(lli->lli_symlink_name, symlen);
111         /* do not return an error if we cannot cache the symlink locally */
112         if (lli->lli_symlink_name) {
113                 memcpy(lli->lli_symlink_name, *symname, symlen);
114                 *symname = lli->lli_symlink_name;
115         }
116         RETURN(0);
117
118 failed:
119         RETURN(rc);
120 }
121
122 #ifdef HAVE_SYMLINK_OPS_USE_NAMEIDATA
123 static void ll_put_link(struct dentry *dentry,
124                         struct nameidata *nd, void *cookie)
125 #else
126 # ifdef HAVE_IOP_GET_LINK
127 static void ll_put_link(void *cookie)
128 # else
129 static void ll_put_link(struct inode *unused, void *cookie)
130 # endif
131 #endif
132 {
133         ptlrpc_req_finished(cookie);
134 }
135
136 #ifdef HAVE_SYMLINK_OPS_USE_NAMEIDATA
137 static void *ll_follow_link(struct dentry *dentry, struct nameidata *nd)
138 {
139         struct inode *inode = dentry->d_inode;
140         struct ptlrpc_request *request = NULL;
141         int rc;
142         char *symname = NULL;
143
144         ENTRY;
145
146         CDEBUG(D_VFSTRACE, "VFS Op\n");
147         /*
148          * Limit the recursive symlink depth to 5 instead of default
149          * 8 links when kernel has 4k stack to prevent stack overflow.
150          * For 8k stacks we need to limit it to 7 for local servers.
151          */
152         if (THREAD_SIZE < 8192 && current->link_count >= 6) {
153                 rc = -ELOOP;
154         } else if (THREAD_SIZE == 8192 && current->link_count >= 8) {
155                 rc = -ELOOP;
156         } else {
157                 ll_inode_size_lock(inode);
158                 rc = ll_readlink_internal(inode, &request, &symname);
159                 ll_inode_size_unlock(inode);
160         }
161         if (rc) {
162                 ptlrpc_req_finished(request);
163                 request = NULL;
164                 symname = ERR_PTR(rc);
165         }
166
167         nd_set_link(nd, symname);
168         /*
169          * symname may contain a pointer to the request message buffer,
170          * we delay request releasing until ll_put_link then.
171          */
172         RETURN(request);
173 }
174 #else
175 # ifdef HAVE_IOP_GET_LINK
176 static const char *ll_get_link(struct dentry *dentry,
177                                struct inode *inode,
178                                struct delayed_call *done)
179 {
180         struct ptlrpc_request *request;
181         char *symname = NULL;
182         int rc;
183
184         ENTRY;
185         CDEBUG(D_VFSTRACE, "VFS Op\n");
186         if (!dentry)
187                 RETURN(ERR_PTR(-ECHILD));
188         ll_inode_size_lock(inode);
189         rc = ll_readlink_internal(inode, &request, &symname);
190         ll_inode_size_unlock(inode);
191         if (rc < 0) {
192                 ptlrpc_req_finished(request);
193                 return ERR_PTR(rc);
194         }
195
196         /*
197          * symname may contain a pointer to the request message buffer,
198          * we delay request releasing then.
199          */
200         set_delayed_call(done, ll_put_link, request);
201         RETURN(symname);
202 }
203 # else
204 static const char *ll_follow_link(struct dentry *dentry, void **cookie)
205 {
206         struct inode *inode = d_inode(dentry);
207         struct ptlrpc_request *request;
208         char *symname = NULL;
209         int rc;
210
211         ENTRY;
212
213         CDEBUG(D_VFSTRACE, "VFS Op\n");
214         ll_inode_size_lock(inode);
215         rc = ll_readlink_internal(inode, &request, &symname);
216         ll_inode_size_unlock(inode);
217         if (rc < 0) {
218                 ptlrpc_req_finished(request);
219                 return ERR_PTR(rc);
220         }
221
222         /*
223          * symname may contain a pointer to the request message buffer,
224          * we delay request releasing until ll_put_link then.
225          */
226         *cookie = request;
227         RETURN(symname);
228 }
229 # endif /* HAVE_IOP_GET_LINK */
230 #endif /* HAVE_SYMLINK_OPS_USE_NAMEIDATA */
231
232 struct inode_operations ll_fast_symlink_inode_operations = {
233 #ifdef HAVE_IOP_GENERIC_READLINK
234         .readlink       = generic_readlink,
235 #endif
236         .setattr        = ll_setattr,
237 #ifdef HAVE_IOP_GET_LINK
238         .get_link       = ll_get_link,
239 #else
240         .follow_link    = ll_follow_link,
241         .put_link       = ll_put_link,
242 #endif
243         .getattr        = ll_getattr,
244         .permission     = ll_inode_permission,
245 #ifdef HAVE_IOP_XATTR
246         .setxattr       = ll_setxattr,
247         .getxattr       = ll_getxattr,
248         .removexattr    = ll_removexattr,
249 #endif
250         .listxattr      = ll_listxattr,
251 };