Whamcloud - gitweb
LU-8851 nodemap: add uid/gid only flags to control mapping
[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 static int ll_readlink_internal(struct inode *inode,
42                                 struct ptlrpc_request **request, char **symname)
43 {
44         struct ll_inode_info *lli = ll_i2info(inode);
45         struct ll_sb_info *sbi = ll_i2sbi(inode);
46         int rc, symlen = i_size_read(inode) + 1;
47         struct mdt_body *body;
48         struct md_op_data *op_data;
49         ENTRY;
50
51         *request = NULL;
52
53         if (lli->lli_symlink_name) {
54                 int print_limit = min_t(int, PAGE_SIZE - 128, symlen);
55
56                 *symname = lli->lli_symlink_name;
57                 /* If the total CDEBUG() size is larger than a page, it
58                  * will print a warning to the console, avoid this by
59                  * printing just the last part of the symlink. */
60                 CDEBUG(D_INODE, "using cached symlink %s%.*s, len = %d\n",
61                        print_limit < symlen ? "..." : "", print_limit,
62                        (*symname) + symlen - print_limit, symlen);
63                 RETURN(0);
64         }
65
66         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, symlen,
67                                      LUSTRE_OPC_ANY, NULL);
68         if (IS_ERR(op_data))
69                 RETURN(PTR_ERR(op_data));
70
71         op_data->op_valid = OBD_MD_LINKNAME;
72         rc = md_getattr(sbi->ll_md_exp, op_data, request);
73         ll_finish_md_op_data(op_data);
74         if (rc) {
75                 if (rc != -ENOENT)
76                         CERROR("%s: inode "DFID": rc = %d\n",
77                                ll_get_fsname(inode->i_sb, NULL, 0),
78                                PFID(ll_inode2fid(inode)), rc);
79                 GOTO (failed, rc);
80         }
81
82         body = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
83         LASSERT(body != NULL);
84         if ((body->mbo_valid & OBD_MD_LINKNAME) == 0) {
85                 CERROR("OBD_MD_LINKNAME not set on reply\n");
86                 GOTO(failed, rc = -EPROTO);
87         }
88
89         LASSERT(symlen != 0);
90         if (body->mbo_eadatasize != symlen) {
91                 CERROR("%s: inode "DFID": symlink length %d not expected %d\n",
92                        ll_get_fsname(inode->i_sb, NULL, 0),
93                        PFID(ll_inode2fid(inode)), body->mbo_eadatasize - 1,
94                        symlen - 1);
95                 GOTO(failed, rc = -EPROTO);
96         }
97
98         *symname = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_MD);
99         if (*symname == NULL ||
100             strnlen(*symname, symlen) != symlen - 1) {
101                 /* not full/NULL terminated */
102                 CERROR("%s: inode "DFID": symlink not NULL terminated string"
103                        "of length %d\n", ll_get_fsname(inode->i_sb, NULL, 0),
104                        PFID(ll_inode2fid(inode)), symlen - 1);
105                 GOTO(failed, rc = -EPROTO);
106         }
107
108         OBD_ALLOC(lli->lli_symlink_name, symlen);
109         /* do not return an error if we cannot cache the symlink locally */
110         if (lli->lli_symlink_name) {
111                 memcpy(lli->lli_symlink_name, *symname, symlen);
112                 *symname = lli->lli_symlink_name;
113         }
114         RETURN(0);
115
116 failed:
117         RETURN (rc);
118 }
119
120 #ifdef HAVE_SYMLINK_OPS_USE_NAMEIDATA
121 static void ll_put_link(struct dentry *dentry,
122                         struct nameidata *nd, void *cookie)
123 #else
124 # ifdef HAVE_IOP_GET_LINK
125 static void ll_put_link(void *cookie)
126 # else
127 static void ll_put_link(struct inode *unused, void *cookie)
128 # endif
129 #endif
130 {
131         ptlrpc_req_finished(cookie);
132 }
133
134 #ifdef HAVE_SYMLINK_OPS_USE_NAMEIDATA
135 static void *ll_follow_link(struct dentry *dentry, struct nameidata *nd)
136 {
137         struct inode *inode = dentry->d_inode;
138         struct ptlrpc_request *request = NULL;
139         int rc;
140         char *symname = NULL;
141         ENTRY;
142
143         CDEBUG(D_VFSTRACE, "VFS Op\n");
144         /* Limit the recursive symlink depth to 5 instead of default
145          * 8 links when kernel has 4k stack to prevent stack overflow.
146          * For 8k stacks we need to limit it to 7 for local servers. */
147         if (THREAD_SIZE < 8192 && current->link_count >= 6) {
148                 rc = -ELOOP;
149         } else if (THREAD_SIZE == 8192 && current->link_count >= 8) {
150                 rc = -ELOOP;
151         } else {
152                 ll_inode_size_lock(inode);
153                 rc = ll_readlink_internal(inode, &request, &symname);
154                 ll_inode_size_unlock(inode);
155         }
156         if (rc) {
157                 ptlrpc_req_finished(request);
158                 request = NULL;
159                 symname = ERR_PTR(rc);
160         }
161
162         nd_set_link(nd, symname);
163         /* symname may contain a pointer to the request message buffer,
164          * we delay request releasing until ll_put_link then.
165          */
166         RETURN(request);
167 }
168 #else
169 # ifdef HAVE_IOP_GET_LINK
170 static const char *ll_get_link(struct dentry *dentry,
171                                struct inode *inode,
172                                struct delayed_call *done)
173 {
174         struct ptlrpc_request *request;
175         char *symname = NULL;
176         int rc;
177
178         ENTRY;
179         CDEBUG(D_VFSTRACE, "VFS Op\n");
180         if (!dentry)
181                 RETURN(ERR_PTR(-ECHILD));
182         ll_inode_size_lock(inode);
183         rc = ll_readlink_internal(inode, &request, &symname);
184         ll_inode_size_unlock(inode);
185         if (rc < 0) {
186                 ptlrpc_req_finished(request);
187                 return ERR_PTR(rc);
188         }
189
190         /* symname may contain a pointer to the request message buffer,
191          * we delay request releasing then.
192          */
193         set_delayed_call(done, ll_put_link, request);
194         RETURN(symname);
195 }
196 # else
197 static const char *ll_follow_link(struct dentry *dentry, void **cookie)
198 {
199         struct inode *inode = d_inode(dentry);
200         struct ptlrpc_request *request;
201         char *symname = NULL;
202         int rc;
203         ENTRY;
204
205         CDEBUG(D_VFSTRACE, "VFS Op\n");
206         ll_inode_size_lock(inode);
207         rc = ll_readlink_internal(inode, &request, &symname);
208         ll_inode_size_unlock(inode);
209         if (rc < 0) {
210                 ptlrpc_req_finished(request);
211                 return ERR_PTR(rc);
212         }
213
214         /* symname may contain a pointer to the request message buffer,
215          * we delay request releasing until ll_put_link then.
216          */
217         *cookie = request;
218         RETURN(symname);
219 }
220 # endif /* HAVE_IOP_GET_LINK */
221 #endif /* HAVE_SYMLINK_OPS_USE_NAMEIDATA */
222
223 struct inode_operations ll_fast_symlink_inode_operations = {
224         .readlink       = generic_readlink,
225         .setattr        = ll_setattr,
226 #ifdef HAVE_IOP_GET_LINK
227         .get_link       = ll_get_link,
228 #else
229         .follow_link    = ll_follow_link,
230         .put_link       = ll_put_link,
231 #endif
232         .getattr        = ll_getattr,
233         .permission     = ll_inode_permission,
234         .setxattr       = ll_setxattr,
235         .getxattr       = ll_getxattr,
236         .listxattr      = ll_listxattr,
237         .removexattr    = ll_removexattr,
238 };