1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
39 #include <linux/stat.h>
40 #include <linux/smp_lock.h>
41 #include <linux/version.h>
42 #define DEBUG_SUBSYSTEM S_LLITE
44 #include <lustre_lite.h>
45 #include "llite_internal.h"
47 static int ll_readlink_internal(struct inode *inode,
48 struct ptlrpc_request **request, char **symname)
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;
59 if (lli->lli_symlink_name) {
60 *symname = lli->lli_symlink_name;
61 CDEBUG(D_INODE, "using cached symlink %s\n", *symname);
65 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, symlen,
66 LUSTRE_OPC_ANY, NULL);
68 RETURN(PTR_ERR(op_data));
70 op_data->op_valid = OBD_MD_LINKNAME;
71 rc = md_getattr(sbi->ll_md_exp, op_data, request);
72 ll_finish_md_op_data(op_data);
75 CERROR("inode %lu: rc = %d\n", inode->i_ino, rc);
79 body = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
80 LASSERT(body != NULL);
81 if ((body->valid & OBD_MD_LINKNAME) == 0) {
82 CERROR("OBD_MD_LINKNAME not set on reply\n");
83 GOTO(failed, rc = -EPROTO);
87 if (body->eadatasize != symlen) {
88 CERROR("inode %lu: symlink length %d not expected %d\n",
89 inode->i_ino, body->eadatasize - 1, symlen - 1);
90 GOTO(failed, rc = -EPROTO);
93 *symname = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_MD);
94 if (*symname == NULL ||
95 strnlen(*symname, symlen) != symlen - 1) {
96 /* not full/NULL terminated */
97 CERROR("inode %lu: symlink not NULL terminated string"
98 "of length %d\n", inode->i_ino, symlen - 1);
99 GOTO(failed, rc = -EPROTO);
102 OBD_ALLOC(lli->lli_symlink_name, symlen);
103 /* do not return an error if we cannot cache the symlink locally */
104 if (lli->lli_symlink_name) {
105 memcpy(lli->lli_symlink_name, *symname, symlen);
106 *symname = lli->lli_symlink_name;
114 static int ll_readlink(struct dentry *dentry, char *buffer, int buflen)
116 struct inode *inode = dentry->d_inode;
117 struct ptlrpc_request *request;
122 CDEBUG(D_VFSTRACE, "VFS Op\n");
123 /* on symlinks lli_open_sem protects lli_symlink_name allocation/data */
124 ll_inode_size_lock(inode, 0);
125 rc = ll_readlink_internal(inode, &request, &symname);
129 rc = vfs_readlink(dentry, buffer, buflen, symname);
131 ptlrpc_req_finished(request);
132 ll_inode_size_unlock(inode, 0);
136 #ifdef HAVE_COOKIE_FOLLOW_LINK
137 # define LL_FOLLOW_LINK_RETURN_TYPE void *
139 # define LL_FOLLOW_LINK_RETURN_TYPE int
142 static LL_FOLLOW_LINK_RETURN_TYPE ll_follow_link(struct dentry *dentry,
143 struct nameidata *nd)
145 struct inode *inode = dentry->d_inode;
146 struct ptlrpc_request *request = NULL;
151 CDEBUG(D_VFSTRACE, "VFS Op\n");
152 /* Limit the recursive symlink depth to 5 instead of default
153 * 8 links when kernel has 4k stack to prevent stack overflow.
154 * For 8k stacks we need to limit it to 7 for local servers. */
155 if (THREAD_SIZE < 8192 && current->link_count >= 6) {
157 } else if (THREAD_SIZE == 8192 && current->link_count >= 8) {
160 ll_inode_size_lock(inode, 0);
161 rc = ll_readlink_internal(inode, &request, &symname);
162 ll_inode_size_unlock(inode, 0);
165 cfs_path_put(nd); /* Kernel assumes that ->follow_link()
166 releases nameidata on error */
170 #ifdef HAVE_COOKIE_FOLLOW_LINK
171 nd_set_link(nd, symname);
172 /* @symname may contain a pointer to the request message buffer,
173 we delay request releasing until ll_put_link then. */
176 if (lli->lli_symlink_name == NULL) {
177 /* falling back to recursive follow link if the request
178 * needs to be cleaned up still. */
179 rc = vfs_follow_link(nd, symname);
182 nd_set_link(nd, symname);
186 ptlrpc_req_finished(request);
187 #ifdef HAVE_COOKIE_FOLLOW_LINK
194 #ifdef HAVE_COOKIE_FOLLOW_LINK
195 static void ll_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
197 ptlrpc_req_finished(cookie);
201 struct inode_operations ll_fast_symlink_inode_operations = {
202 .readlink = ll_readlink,
203 .setattr = ll_setattr,
204 .follow_link = ll_follow_link,
205 #ifdef HAVE_COOKIE_FOLLOW_LINK
206 .put_link = ll_put_link,
208 .getattr = ll_getattr,
209 .permission = ll_inode_permission,
210 .setxattr = ll_setxattr,
211 .getxattr = ll_getxattr,
212 .listxattr = ll_listxattr,
213 .removexattr = ll_removexattr,