Whamcloud - gitweb
LU-8056 llite: inode_operations interface changed in 4.5
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2014, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #include <linux/fs.h>
38 #include <linux/mm.h>
39 #include <linux/stat.h>
40 #include <linux/version.h>
41 #define DEBUG_SUBSYSTEM S_LLITE
42
43 #include "llite_internal.h"
44
45 static int ll_readlink_internal(struct inode *inode,
46                                 struct ptlrpc_request **request, char **symname)
47 {
48         struct ll_inode_info *lli = ll_i2info(inode);
49         struct ll_sb_info *sbi = ll_i2sbi(inode);
50         int rc, symlen = i_size_read(inode) + 1;
51         struct mdt_body *body;
52         struct md_op_data *op_data;
53         ENTRY;
54
55         *request = NULL;
56
57         if (lli->lli_symlink_name) {
58                 int print_limit = min_t(int, PAGE_SIZE - 128, symlen);
59
60                 *symname = lli->lli_symlink_name;
61                 /* If the total CDEBUG() size is larger than a page, it
62                  * will print a warning to the console, avoid this by
63                  * printing just the last part of the symlink. */
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_get_fsname(inode->i_sb, NULL, 0),
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                        ll_get_fsname(inode->i_sb, NULL, 0),
97                        PFID(ll_inode2fid(inode)), body->mbo_eadatasize - 1,
98                        symlen - 1);
99                 GOTO(failed, rc = -EPROTO);
100         }
101
102         *symname = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_MD);
103         if (*symname == NULL ||
104             strnlen(*symname, symlen) != symlen - 1) {
105                 /* not full/NULL terminated */
106                 CERROR("%s: inode "DFID": symlink not NULL terminated string"
107                        "of length %d\n", ll_get_fsname(inode->i_sb, NULL, 0),
108                        PFID(ll_inode2fid(inode)), symlen - 1);
109                 GOTO(failed, rc = -EPROTO);
110         }
111
112         OBD_ALLOC(lli->lli_symlink_name, symlen);
113         /* do not return an error if we cannot cache the symlink locally */
114         if (lli->lli_symlink_name) {
115                 memcpy(lli->lli_symlink_name, *symname, symlen);
116                 *symname = lli->lli_symlink_name;
117         }
118         RETURN(0);
119
120 failed:
121         RETURN (rc);
122 }
123
124 #ifdef HAVE_SYMLINK_OPS_USE_NAMEIDATA
125 static void ll_put_link(struct dentry *dentry,
126                         struct nameidata *nd, void *cookie)
127 #else
128 # ifdef HAVE_IOP_GET_LINK
129 static void ll_put_link(void *cookie)
130 # else
131 static void ll_put_link(struct inode *unused, void *cookie)
132 # endif
133 #endif
134 {
135         ptlrpc_req_finished(cookie);
136 }
137
138 #ifdef HAVE_SYMLINK_OPS_USE_NAMEIDATA
139 static void *ll_follow_link(struct dentry *dentry, struct nameidata *nd)
140 {
141         struct inode *inode = dentry->d_inode;
142         struct ptlrpc_request *request = NULL;
143         int rc;
144         char *symname = NULL;
145         ENTRY;
146
147         CDEBUG(D_VFSTRACE, "VFS Op\n");
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         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         /* symname may contain a pointer to the request message buffer,
168          * we delay request releasing until ll_put_link then.
169          */
170         RETURN(request);
171 }
172 #else
173 # ifdef HAVE_IOP_GET_LINK
174 static const char *ll_get_link(struct dentry *dentry,
175                                struct inode *inode,
176                                struct delayed_call *done)
177 {
178         struct ptlrpc_request *request;
179         char *symname = NULL;
180         int rc;
181
182         ENTRY;
183         CDEBUG(D_VFSTRACE, "VFS Op\n");
184         if (!dentry)
185                 RETURN(ERR_PTR(-ECHILD));
186         ll_inode_size_lock(inode);
187         rc = ll_readlink_internal(inode, &request, &symname);
188         ll_inode_size_unlock(inode);
189         if (rc < 0) {
190                 ptlrpc_req_finished(request);
191                 return ERR_PTR(rc);
192         }
193
194         /* symname may contain a pointer to the request message buffer,
195          * we delay request releasing then.
196          */
197         set_delayed_call(done, ll_put_link, request);
198         RETURN(symname);
199 }
200 # else
201 static const char *ll_follow_link(struct dentry *dentry, void **cookie)
202 {
203         struct inode *inode = d_inode(dentry);
204         struct ptlrpc_request *request;
205         char *symname = NULL;
206         int rc;
207         ENTRY;
208
209         CDEBUG(D_VFSTRACE, "VFS Op\n");
210         ll_inode_size_lock(inode);
211         rc = ll_readlink_internal(inode, &request, &symname);
212         ll_inode_size_unlock(inode);
213         if (rc < 0) {
214                 ptlrpc_req_finished(request);
215                 return ERR_PTR(rc);
216         }
217
218         /* symname may contain a pointer to the request message buffer,
219          * we delay request releasing until ll_put_link then.
220          */
221         *cookie = request;
222         RETURN(symname);
223 }
224 # endif /* HAVE_IOP_GET_LINK */
225 #endif /* HAVE_SYMLINK_OPS_USE_NAMEIDATA */
226
227 struct inode_operations ll_fast_symlink_inode_operations = {
228         .readlink       = generic_readlink,
229         .setattr        = ll_setattr,
230 #ifdef HAVE_IOP_GET_LINK
231         .get_link       = ll_get_link,
232 #else
233         .follow_link    = ll_follow_link,
234         .put_link       = ll_put_link,
235 #endif
236         .getattr        = ll_getattr,
237         .permission     = ll_inode_permission,
238         .setxattr       = ll_setxattr,
239         .getxattr       = ll_getxattr,
240         .listxattr      = ll_listxattr,
241         .removexattr    = ll_removexattr,
242 };