Whamcloud - gitweb
LU-2800 autoconf: remove obsolete autoconf options
[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, 2012, 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 <lustre_lite.h>
44 #include "llite_internal.h"
45
46 static int ll_readlink_internal(struct inode *inode,
47                                 struct ptlrpc_request **request, char **symname)
48 {
49         struct ll_inode_info *lli = ll_i2info(inode);
50         struct ll_sb_info *sbi = ll_i2sbi(inode);
51         int rc, symlen = i_size_read(inode) + 1;
52         struct mdt_body *body;
53         struct md_op_data *op_data;
54         ENTRY;
55
56         *request = NULL;
57
58         if (lli->lli_symlink_name) {
59                 int print_limit = min_t(int, PAGE_SIZE - 128, symlen);
60
61                 *symname = lli->lli_symlink_name;
62                 /* If the total CDEBUG() size is larger than a page, it
63                  * will print a warning to the console, avoid this by
64                  * printing just the last part of the symlink. */
65                 CDEBUG(D_INODE, "using cached symlink %s%.*s, len = %d\n",
66                        print_limit < symlen ? "..." : "", print_limit,
67                        (*symname) + symlen - print_limit, symlen);
68                 RETURN(0);
69         }
70
71         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, symlen,
72                                      LUSTRE_OPC_ANY, NULL);
73         if (IS_ERR(op_data))
74                 RETURN(PTR_ERR(op_data));
75
76         op_data->op_valid = OBD_MD_LINKNAME;
77         rc = md_getattr(sbi->ll_md_exp, op_data, request);
78         ll_finish_md_op_data(op_data);
79         if (rc) {
80                 if (rc != -ENOENT)
81                         CERROR("inode %lu: rc = %d\n", inode->i_ino, 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->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->eadatasize != symlen) {
94                 CERROR("inode %lu: symlink length %d not expected %d\n",
95                         inode->i_ino, body->eadatasize - 1, symlen - 1);
96                 GOTO(failed, rc = -EPROTO);
97         }
98
99         *symname = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_MD);
100         if (*symname == NULL ||
101             strnlen(*symname, symlen) != symlen - 1) {
102                 /* not full/NULL terminated */
103                 CERROR("inode %lu: symlink not NULL terminated string"
104                         "of length %d\n", inode->i_ino, 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 static int ll_readlink(struct dentry *dentry, char *buffer, int buflen)
121 {
122         struct inode *inode = dentry->d_inode;
123         struct ptlrpc_request *request;
124         char *symname;
125         int rc;
126         ENTRY;
127
128         CDEBUG(D_VFSTRACE, "VFS Op\n");
129
130         ll_inode_size_lock(inode);
131         rc = ll_readlink_internal(inode, &request, &symname);
132         if (rc)
133                 GOTO(out, rc);
134
135         rc = vfs_readlink(dentry, buffer, buflen, symname);
136  out:
137         ptlrpc_req_finished(request);
138         ll_inode_size_unlock(inode);
139         RETURN(rc);
140 }
141
142 static void *ll_follow_link(struct dentry *dentry, struct nameidata *nd)
143 {
144         struct inode *inode = dentry->d_inode;
145         struct ptlrpc_request *request = NULL;
146         int rc;
147         char *symname;
148         ENTRY;
149
150         CDEBUG(D_VFSTRACE, "VFS Op\n");
151         /* Limit the recursive symlink depth to 5 instead of default
152          * 8 links when kernel has 4k stack to prevent stack overflow.
153          * For 8k stacks we need to limit it to 7 for local servers. */
154         if (THREAD_SIZE < 8192 && current->link_count >= 6) {
155                 rc = -ELOOP;
156         } else if (THREAD_SIZE == 8192 && current->link_count >= 8) {
157                 rc = -ELOOP;
158         } else {
159                 ll_inode_size_lock(inode);
160                 rc = ll_readlink_internal(inode, &request, &symname);
161                 ll_inode_size_unlock(inode);
162         }
163         if (rc) {
164                 ptlrpc_req_finished(request);
165                 request = NULL;
166                 symname = ERR_PTR(rc);
167         }
168
169         nd_set_link(nd, symname);
170         /* symname may contain a pointer to the request message buffer,
171          * we delay request releasing until ll_put_link then.
172          */
173         RETURN(request);
174 }
175
176 static void ll_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
177 {
178         ptlrpc_req_finished(cookie);
179 }
180
181 struct inode_operations ll_fast_symlink_inode_operations = {
182         .readlink       = ll_readlink,
183         .setattr        = ll_setattr,
184         .follow_link    = ll_follow_link,
185         .put_link       = ll_put_link,
186         .getattr        = ll_getattr,
187         .permission     = ll_inode_permission,
188         .setxattr       = ll_setxattr,
189         .getxattr       = ll_getxattr,
190         .listxattr      = ll_listxattr,
191         .removexattr    = ll_removexattr,
192 };