4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2014, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
34 #include <linux/stat.h>
35 #include <linux/version.h>
36 #define DEBUG_SUBSYSTEM S_LLITE
38 #include "llite_internal.h"
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.
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)
50 static int ll_readlink_internal(struct inode *inode,
51 struct ptlrpc_request **request, char **symname)
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;
64 if (lli->lli_symlink_name) {
65 int print_limit = min_t(int, PAGE_SIZE - 128, symlen);
67 *symname = lli->lli_symlink_name;
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.
73 CDEBUG(D_INODE, "using cached symlink %s%.*s, len = %d\n",
74 print_limit < symlen ? "..." : "", print_limit,
75 (*symname) + symlen - print_limit, symlen);
79 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, symlen,
80 LUSTRE_OPC_ANY, NULL);
82 RETURN(PTR_ERR(op_data));
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);
89 CERROR("%s: inode "DFID": rc = %d\n",
90 ll_i2sbi(inode)->ll_fsname,
91 PFID(ll_inode2fid(inode)), rc);
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);
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);
110 *symname = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_MD);
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",
117 PFID(ll_inode2fid(inode)), symlen - 1);
118 GOTO(failed, rc = -EPROTO);
121 #ifdef HAVE_IOP_GET_LINK
122 if (IS_ENCRYPTED(inode)) {
123 const char *target = llcrypt_get_symlink(inode, *symname,
126 RETURN(PTR_ERR(target));
127 symlen = strlen(target) + 1;
128 *symname = (char *)target;
130 /* Do not cache symlink targets encoded without the key,
131 * since those become outdated once the key is added.
133 if (!llcrypt_has_encryption_key(inode))
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;
150 #ifdef HAVE_SYMLINK_OPS_USE_NAMEIDATA
151 static void ll_put_link(struct dentry *dentry,
152 struct nameidata *nd, void *cookie)
154 # ifdef HAVE_IOP_GET_LINK
155 static void ll_put_link(void *cookie)
157 static void ll_put_link(struct inode *unused, void *cookie)
161 ptlrpc_req_finished(cookie);
164 #ifdef HAVE_SYMLINK_OPS_USE_NAMEIDATA
165 static void *ll_follow_link(struct dentry *dentry, struct nameidata *nd)
167 struct inode *inode = dentry->d_inode;
168 struct ptlrpc_request *request = NULL;
170 char *symname = NULL;
174 CDEBUG(D_VFSTRACE, "VFS Op\n");
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.
180 if (THREAD_SIZE < 8192 && current->link_count >= 6) {
182 } else if (THREAD_SIZE == 8192 && current->link_count >= 8) {
185 ll_inode_size_lock(inode);
186 rc = ll_readlink_internal(inode, &request, &symname);
187 ll_inode_size_unlock(inode);
190 ptlrpc_req_finished(request);
192 symname = ERR_PTR(rc);
195 nd_set_link(nd, symname);
197 * symname may contain a pointer to the request message buffer,
198 * we delay request releasing until ll_put_link then.
203 # ifdef HAVE_IOP_GET_LINK
204 static const char *ll_get_link(struct dentry *dentry,
206 struct delayed_call *done)
208 struct ptlrpc_request *request;
209 char *symname = NULL;
213 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, inode="DFID"(%p)\n",
214 dentry, PFID(ll_inode2fid(inode)), inode);
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);
221 ptlrpc_req_finished(request);
226 * symname may contain a pointer to the request message buffer,
227 * we delay request releasing then.
229 set_delayed_call(done, ll_put_link, request);
233 static const char *ll_follow_link(struct dentry *dentry, void **cookie)
235 struct inode *inode = d_inode(dentry);
236 struct ptlrpc_request *request;
237 char *symname = NULL;
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);
247 ptlrpc_req_finished(request);
252 * symname may contain a pointer to the request message buffer,
253 * we delay request releasing until ll_put_link then.
258 # endif /* HAVE_IOP_GET_LINK */
259 #endif /* HAVE_SYMLINK_OPS_USE_NAMEIDATA */
262 * ll_getattr_link() - link-specific getattr to set the correct st_size
263 * for encrypted symlinks
265 * Override st_size of encrypted symlinks to be the length of the decrypted
266 * symlink target (or the no-key encoded symlink target, if the key is
267 * unavailable) rather than the length of the encrypted symlink target. This is
268 * necessary for st_size to match the symlink target that userspace actually
269 * sees. POSIX requires this, and some userspace programs depend on it.
271 * For non encrypted symlinks, this is a just calling ll_getattr().
272 * For encrypted symlinks, this additionally requires reading the symlink target
273 * from disk if needed, setting up the inode's encryption key if possible, and
274 * then decrypting or encoding the symlink target. This makes lstat() more
275 * heavyweight than is normally the case. However, decrypted symlink targets
276 * will be cached in ->i_link, so usually the symlink won't have to be read and
277 * decrypted again later if/when it is actually followed, readlink() is called,
278 * or lstat() is called again.
280 * Return: 0 on success, -errno on failure
282 #if defined(HAVE_USER_NAMESPACE_ARG) || defined(HAVE_INODEOPS_ENHANCED_GETATTR)
283 static int ll_getattr_link(
284 #if defined(HAVE_USER_NAMESPACE_ARG)
285 struct mnt_idmap *map,
287 const struct path *path, struct kstat *stat,
288 u32 request_mask, unsigned int flags)
290 struct dentry *dentry = path->dentry;
291 struct inode *inode = d_inode(dentry);
292 DEFINE_DELAYED_CALL(done);
296 rc = ll_getattr(map, path, stat, request_mask, flags);
297 if (rc || !IS_ENCRYPTED(inode))
301 * To get the symlink target that userspace will see (whether it's the
302 * decrypted target or the no-key encoded target), we can just get it
303 * in the same way the VFS does during path resolution and readlink().
305 link = READ_ONCE(inode->i_link);
307 link = inode->i_op->get_link(dentry, inode, &done);
309 return PTR_ERR(link);
311 stat->size = strlen(link);
312 do_delayed_call(&done);
315 #else /* HAVE_INODEOPS_ENHANCED_GETATTR */
316 #define ll_getattr_link ll_getattr
319 const struct inode_operations ll_fast_symlink_inode_operations = {
320 #ifdef HAVE_IOP_GENERIC_READLINK
321 .readlink = generic_readlink,
323 .setattr = ll_setattr,
324 #ifdef HAVE_IOP_GET_LINK
325 .get_link = ll_get_link,
327 .follow_link = ll_follow_link,
328 .put_link = ll_put_link,
330 .getattr = ll_getattr_link,
331 .permission = ll_inode_permission,
332 #ifdef HAVE_IOP_XATTR
333 .setxattr = ll_setxattr,
334 .getxattr = ll_getxattr,
335 .removexattr = ll_removexattr,
337 .listxattr = ll_listxattr,