Whamcloud - gitweb
LU-1415 tests: Handle OFD procfs changes
[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, Whamcloud, Inc.
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/smp_lock.h>
41 #include <linux/version.h>
42 #define DEBUG_SUBSYSTEM S_LLITE
43
44 #include <lustre_lite.h>
45 #include "llite_internal.h"
46
47 static int ll_readlink_internal(struct inode *inode,
48                                 struct ptlrpc_request **request, char **symname)
49 {
50         struct ll_inode_info *lli = ll_i2info(inode);
51         struct ll_sb_info *sbi = ll_i2sbi(inode);
52         int rc, symlen = i_size_read(inode) + 1;
53         struct mdt_body *body;
54         struct md_op_data *op_data;
55         ENTRY;
56
57         *request = NULL;
58
59         if (lli->lli_symlink_name) {
60                 int print_limit = min_t(int, PAGE_SIZE - 128, symlen);
61
62                 *symname = lli->lli_symlink_name;
63                 /* If the total CDEBUG() size is larger than a page, it
64                  * will print a warning to the console, avoid this by
65                  * printing just the last part of the symlink. */
66                 CDEBUG(D_INODE, "using cached symlink %s%.*s, len = %d\n",
67                        print_limit < symlen ? "..." : "", print_limit,
68                        (*symname) + symlen - print_limit, symlen);
69                 RETURN(0);
70         }
71
72         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, symlen,
73                                      LUSTRE_OPC_ANY, NULL);
74         if (IS_ERR(op_data))
75                 RETURN(PTR_ERR(op_data));
76
77         op_data->op_valid = OBD_MD_LINKNAME;
78         rc = md_getattr(sbi->ll_md_exp, op_data, request);
79         ll_finish_md_op_data(op_data);
80         if (rc) {
81                 if (rc != -ENOENT)
82                         CERROR("inode %lu: rc = %d\n", inode->i_ino, 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->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->eadatasize != symlen) {
95                 CERROR("inode %lu: symlink length %d not expected %d\n",
96                         inode->i_ino, body->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 == NULL ||
102             strnlen(*symname, symlen) != symlen - 1) {
103                 /* not full/NULL terminated */
104                 CERROR("inode %lu: symlink not NULL terminated string"
105                         "of length %d\n", inode->i_ino, 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 static int ll_readlink(struct dentry *dentry, char *buffer, int buflen)
122 {
123         struct inode *inode = dentry->d_inode;
124         struct ptlrpc_request *request;
125         char *symname;
126         int rc;
127         ENTRY;
128
129         CDEBUG(D_VFSTRACE, "VFS Op\n");
130
131         ll_inode_size_lock(inode);
132         rc = ll_readlink_internal(inode, &request, &symname);
133         if (rc)
134                 GOTO(out, rc);
135
136         rc = vfs_readlink(dentry, buffer, buflen, symname);
137  out:
138         ptlrpc_req_finished(request);
139         ll_inode_size_unlock(inode);
140         RETURN(rc);
141 }
142
143 #ifdef HAVE_COOKIE_FOLLOW_LINK
144 # define LL_FOLLOW_LINK_RETURN_TYPE void *
145 #else
146 # define LL_FOLLOW_LINK_RETURN_TYPE int
147 #endif
148
149 static LL_FOLLOW_LINK_RETURN_TYPE ll_follow_link(struct dentry *dentry,
150                                                  struct nameidata *nd)
151 {
152         struct inode *inode = dentry->d_inode;
153         struct ptlrpc_request *request = NULL;
154         int rc;
155         char *symname;
156         ENTRY;
157
158         CDEBUG(D_VFSTRACE, "VFS Op\n");
159         /* Limit the recursive symlink depth to 5 instead of default
160          * 8 links when kernel has 4k stack to prevent stack overflow.
161          * For 8k stacks we need to limit it to 7 for local servers. */
162         if (THREAD_SIZE < 8192 && current->link_count >= 6) {
163                 rc = -ELOOP;
164         } else if (THREAD_SIZE == 8192 && current->link_count >= 8) {
165                 rc = -ELOOP;
166         } else {
167                 ll_inode_size_lock(inode);
168                 rc = ll_readlink_internal(inode, &request, &symname);
169                 ll_inode_size_unlock(inode);
170         }
171         if (rc) {
172                 cfs_path_put(nd); /* Kernel assumes that ->follow_link()
173                                      releases nameidata on error */
174                 GOTO(out, rc);
175         }
176
177 #ifdef HAVE_COOKIE_FOLLOW_LINK
178         nd_set_link(nd, symname);
179         /* @symname may contain a pointer to the request message buffer,
180            we delay request releasing until ll_put_link then. */
181         RETURN(request);
182 #else
183         if (lli->lli_symlink_name == NULL) {
184                 /* falling back to recursive follow link if the request
185                  * needs to be cleaned up still. */
186                 rc = vfs_follow_link(nd, symname);
187                 GOTO(out, rc);
188         }
189         nd_set_link(nd, symname);
190         RETURN(0);
191 #endif
192 out:
193         ptlrpc_req_finished(request);
194 #ifdef HAVE_COOKIE_FOLLOW_LINK
195         RETURN(ERR_PTR(rc));
196 #else
197         RETURN(rc);
198 #endif
199 }
200
201 #ifdef HAVE_COOKIE_FOLLOW_LINK
202 static void ll_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
203 {
204         ptlrpc_req_finished(cookie);
205 }
206 #endif
207
208 struct inode_operations ll_fast_symlink_inode_operations = {
209         .readlink       = ll_readlink,
210         .setattr        = ll_setattr,
211         .follow_link    = ll_follow_link,
212 #ifdef HAVE_COOKIE_FOLLOW_LINK
213         .put_link       = ll_put_link,
214 #endif
215         .getattr        = ll_getattr,
216         .permission     = ll_inode_permission,
217         .setxattr       = ll_setxattr,
218         .getxattr       = ll_getxattr,
219         .listxattr      = ll_listxattr,
220         .removexattr    = ll_removexattr,
221 };