Whamcloud - gitweb
LU-15220 libcfs: fix panic_notifier_list undeclared error
[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  */
31
32 #include <linux/fs.h>
33 #include <linux/mm.h>
34 #include <linux/stat.h>
35 #include <linux/version.h>
36 #define DEBUG_SUBSYSTEM S_LLITE
37
38 #include "llite_internal.h"
39
40 /* Must be called with lli_size_mutex locked */
41 /* HAVE_IOP_GET_LINK is defined from kernel 4.5, whereas
42  * IS_ENCRYPTED is brought by kernel 4.14.
43  * So there is no need to handle encryption case otherwise.
44  */
45 #ifdef HAVE_IOP_GET_LINK
46 static int ll_readlink_internal(struct inode *inode,
47                                 struct ptlrpc_request **request,
48                                 char **symname, struct delayed_call *done)
49 #else
50 static int ll_readlink_internal(struct inode *inode,
51                                 struct ptlrpc_request **request, char **symname)
52 #endif
53 {
54         struct ll_inode_info *lli = ll_i2info(inode);
55         struct ll_sb_info *sbi = ll_i2sbi(inode);
56         int rc, symlen = i_size_read(inode) + 1;
57         struct mdt_body *body;
58         struct md_op_data *op_data;
59
60         ENTRY;
61
62         *request = NULL;
63
64         if (lli->lli_symlink_name) {
65                 int print_limit = min_t(int, PAGE_SIZE - 128, symlen);
66
67                 *symname = lli->lli_symlink_name;
68                 /*
69                  * If the total CDEBUG() size is larger than a page, it
70                  * will print a warning to the console, avoid this by
71                  * printing just the last part of the symlink.
72                  */
73                 CDEBUG(D_INODE, "using cached symlink %s%.*s, len = %d\n",
74                        print_limit < symlen ? "..." : "", print_limit,
75                        (*symname) + symlen - print_limit, symlen);
76                 RETURN(0);
77         }
78
79         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, symlen,
80                                      LUSTRE_OPC_ANY, NULL);
81         if (IS_ERR(op_data))
82                 RETURN(PTR_ERR(op_data));
83
84         op_data->op_valid = OBD_MD_LINKNAME;
85         rc = md_getattr(sbi->ll_md_exp, op_data, request);
86         ll_finish_md_op_data(op_data);
87         if (rc) {
88                 if (rc != -ENOENT)
89                         CERROR("%s: inode "DFID": rc = %d\n",
90                                ll_i2sbi(inode)->ll_fsname,
91                                PFID(ll_inode2fid(inode)), rc);
92                 GOTO(failed, rc);
93         }
94
95         body = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
96         LASSERT(body != NULL);
97         if ((body->mbo_valid & OBD_MD_LINKNAME) == 0) {
98                 CERROR("OBD_MD_LINKNAME not set on reply\n");
99                 GOTO(failed, rc = -EPROTO);
100         }
101
102         LASSERT(symlen != 0);
103         if (body->mbo_eadatasize != symlen) {
104                 CERROR("%s: inode "DFID": symlink length %d not expected %d\n",
105                        sbi->ll_fsname, PFID(ll_inode2fid(inode)),
106                        body->mbo_eadatasize - 1, symlen - 1);
107                 GOTO(failed, rc = -EPROTO);
108         }
109
110         *symname = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_MD);
111         if (!*symname ||
112             (!IS_ENCRYPTED(inode) &&
113              strnlen(*symname, symlen) != symlen - 1)) {
114                 /* not full/NULL terminated */
115                 CERROR("%s: inode "DFID": symlink not NULL terminated string of length %d\n",
116                        sbi->ll_fsname,
117                        PFID(ll_inode2fid(inode)), symlen - 1);
118                 GOTO(failed, rc = -EPROTO);
119         }
120
121 #ifdef HAVE_IOP_GET_LINK
122         if (IS_ENCRYPTED(inode)) {
123                 const char *target = llcrypt_get_symlink(inode, *symname,
124                                                          symlen, done);
125                 if (IS_ERR(target))
126                         RETURN(PTR_ERR(target));
127                 symlen = strlen(target) + 1;
128                 *symname = (char *)target;
129
130                 /* Do not cache symlink targets encoded without the key,
131                  * since those become outdated once the key is added.
132                  */
133                 if (!llcrypt_has_encryption_key(inode))
134                         RETURN(0);
135         }
136 #endif
137
138         OBD_ALLOC(lli->lli_symlink_name, symlen);
139         /* do not return an error if we cannot cache the symlink locally */
140         if (lli->lli_symlink_name) {
141                 memcpy(lli->lli_symlink_name, *symname, symlen);
142                 *symname = lli->lli_symlink_name;
143         }
144         RETURN(0);
145
146 failed:
147         RETURN(rc);
148 }
149
150 #ifdef HAVE_SYMLINK_OPS_USE_NAMEIDATA
151 static void ll_put_link(struct dentry *dentry,
152                         struct nameidata *nd, void *cookie)
153 #else
154 # ifdef HAVE_IOP_GET_LINK
155 static void ll_put_link(void *cookie)
156 # else
157 static void ll_put_link(struct inode *unused, void *cookie)
158 # endif
159 #endif
160 {
161         ptlrpc_req_finished(cookie);
162 }
163
164 #ifdef HAVE_SYMLINK_OPS_USE_NAMEIDATA
165 static void *ll_follow_link(struct dentry *dentry, struct nameidata *nd)
166 {
167         struct inode *inode = dentry->d_inode;
168         struct ptlrpc_request *request = NULL;
169         int rc;
170         char *symname = NULL;
171
172         ENTRY;
173
174         CDEBUG(D_VFSTRACE, "VFS Op\n");
175         /*
176          * Limit the recursive symlink depth to 5 instead of default
177          * 8 links when kernel has 4k stack to prevent stack overflow.
178          * For 8k stacks we need to limit it to 7 for local servers.
179          */
180         if (THREAD_SIZE < 8192 && current->link_count >= 6) {
181                 rc = -ELOOP;
182         } else if (THREAD_SIZE == 8192 && current->link_count >= 8) {
183                 rc = -ELOOP;
184         } else {
185                 ll_inode_size_lock(inode);
186                 rc = ll_readlink_internal(inode, &request, &symname);
187                 ll_inode_size_unlock(inode);
188         }
189         if (rc) {
190                 ptlrpc_req_finished(request);
191                 request = NULL;
192                 symname = ERR_PTR(rc);
193         }
194
195         nd_set_link(nd, symname);
196         /*
197          * symname may contain a pointer to the request message buffer,
198          * we delay request releasing until ll_put_link then.
199          */
200         RETURN(request);
201 }
202 #else
203 # ifdef HAVE_IOP_GET_LINK
204 static const char *ll_get_link(struct dentry *dentry,
205                                struct inode *inode,
206                                struct delayed_call *done)
207 {
208         struct ptlrpc_request *request;
209         char *symname = NULL;
210         int rc;
211
212         ENTRY;
213         CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, inode="DFID"(%p)\n",
214                dentry, PFID(ll_inode2fid(inode)), inode);
215         if (!dentry)
216                 RETURN(ERR_PTR(-ECHILD));
217         ll_inode_size_lock(inode);
218         rc = ll_readlink_internal(inode, &request, &symname, done);
219         ll_inode_size_unlock(inode);
220         if (rc < 0) {
221                 ptlrpc_req_finished(request);
222                 return ERR_PTR(rc);
223         }
224
225         /*
226          * symname may contain a pointer to the request message buffer,
227          * we delay request releasing then.
228          */
229         set_delayed_call(done, ll_put_link, request);
230         RETURN(symname);
231 }
232 # else
233 static const char *ll_follow_link(struct dentry *dentry, void **cookie)
234 {
235         struct inode *inode = d_inode(dentry);
236         struct ptlrpc_request *request;
237         char *symname = NULL;
238         int rc;
239
240         ENTRY;
241
242         CDEBUG(D_VFSTRACE, "VFS Op\n");
243         ll_inode_size_lock(inode);
244         rc = ll_readlink_internal(inode, &request, &symname);
245         ll_inode_size_unlock(inode);
246         if (rc < 0) {
247                 ptlrpc_req_finished(request);
248                 return ERR_PTR(rc);
249         }
250
251         /*
252          * symname may contain a pointer to the request message buffer,
253          * we delay request releasing until ll_put_link then.
254          */
255         *cookie = request;
256         RETURN(symname);
257 }
258 # endif /* HAVE_IOP_GET_LINK */
259 #endif /* HAVE_SYMLINK_OPS_USE_NAMEIDATA */
260
261 #ifdef HAVE_INODEOPS_ENHANCED_GETATTR
262 /**
263  * ll_getattr_link() - link-specific getattr to set the correct st_size
264  *                     for encrypted symlinks
265  *
266  * Override st_size of encrypted symlinks to be the length of the decrypted
267  * symlink target (or the no-key encoded symlink target, if the key is
268  * unavailable) rather than the length of the encrypted symlink target. This is
269  * necessary for st_size to match the symlink target that userspace actually
270  * sees.  POSIX requires this, and some userspace programs depend on it.
271  *
272  * For non encrypted symlinks, this is a just calling ll_getattr().
273  * For encrypted symlinks, this additionally requires reading the symlink target
274  * from disk if needed, setting up the inode's encryption key if possible, and
275  * then decrypting or encoding the symlink target.  This makes lstat() more
276  * heavyweight than is normally the case.  However, decrypted symlink targets
277  * will be cached in ->i_link, so usually the symlink won't have to be read and
278  * decrypted again later if/when it is actually followed, readlink() is called,
279  * or lstat() is called again.
280  *
281  * Return: 0 on success, -errno on failure
282  */
283 static int ll_getattr_link(const struct path *path, struct kstat *stat,
284                            u32 request_mask, unsigned int flags)
285 {
286         struct dentry *dentry = path->dentry;
287         struct inode *inode = d_inode(dentry);
288         DEFINE_DELAYED_CALL(done);
289         const char *link;
290         int rc;
291
292         rc = ll_getattr(path, stat, request_mask, flags);
293         if (rc || !IS_ENCRYPTED(inode))
294                 return rc;
295
296         /*
297          * To get the symlink target that userspace will see (whether it's the
298          * decrypted target or the no-key encoded target), we can just get it
299          * in the same way the VFS does during path resolution and readlink().
300          */
301         link = READ_ONCE(inode->i_link);
302         if (!link) {
303                 link = inode->i_op->get_link(dentry, inode, &done);
304                 if (IS_ERR(link))
305                         return PTR_ERR(link);
306         }
307         stat->size = strlen(link);
308         do_delayed_call(&done);
309         return 0;
310 }
311 #else /* HAVE_INODEOPS_ENHANCED_GETATTR */
312 #define ll_getattr_link ll_getattr
313 #endif
314
315
316 const struct inode_operations ll_fast_symlink_inode_operations = {
317 #ifdef HAVE_IOP_GENERIC_READLINK
318         .readlink       = generic_readlink,
319 #endif
320         .setattr        = ll_setattr,
321 #ifdef HAVE_IOP_GET_LINK
322         .get_link       = ll_get_link,
323 #else
324         .follow_link    = ll_follow_link,
325         .put_link       = ll_put_link,
326 #endif
327         .getattr        = ll_getattr_link,
328         .permission     = ll_inode_permission,
329 #ifdef HAVE_IOP_XATTR
330         .setxattr       = ll_setxattr,
331         .getxattr       = ll_getxattr,
332         .removexattr    = ll_removexattr,
333 #endif
334         .listxattr      = ll_listxattr,
335 };