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