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.
32 * Copyright (c) 2011, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
41 #include <linux/stat.h>
42 #include <linux/smp_lock.h>
43 #include <linux/version.h>
44 #define DEBUG_SUBSYSTEM S_LLITE
46 #include <lustre_lite.h>
47 #include "llite_internal.h"
49 static int ll_readlink_internal(struct inode *inode,
50 struct ptlrpc_request **request, char **symname)
52 struct ll_inode_info *lli = ll_i2info(inode);
53 struct ll_sb_info *sbi = ll_i2sbi(inode);
54 int rc, symlen = i_size_read(inode) + 1;
55 struct mdt_body *body;
56 struct md_op_data *op_data;
61 if (lli->lli_symlink_name) {
62 *symname = lli->lli_symlink_name;
63 CDEBUG(D_INODE, "using cached symlink %s\n", *symname);
67 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, symlen,
68 LUSTRE_OPC_ANY, NULL);
70 RETURN(PTR_ERR(op_data));
72 op_data->op_valid = OBD_MD_LINKNAME;
73 rc = md_getattr(sbi->ll_md_exp, op_data, request);
74 ll_finish_md_op_data(op_data);
77 CERROR("inode %lu: rc = %d\n", inode->i_ino, rc);
81 body = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
82 LASSERT(body != NULL);
83 if ((body->valid & OBD_MD_LINKNAME) == 0) {
84 CERROR("OBD_MD_LINKNAME not set on reply\n");
85 GOTO(failed, rc = -EPROTO);
89 if (body->eadatasize != symlen) {
90 CERROR("inode %lu: symlink length %d not expected %d\n",
91 inode->i_ino, body->eadatasize - 1, symlen - 1);
92 GOTO(failed, rc = -EPROTO);
95 *symname = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_MD);
96 if (*symname == NULL ||
97 strnlen(*symname, symlen) != symlen - 1) {
98 /* not full/NULL terminated */
99 CERROR("inode %lu: symlink not NULL terminated string"
100 "of length %d\n", inode->i_ino, symlen - 1);
101 GOTO(failed, rc = -EPROTO);
104 OBD_ALLOC(lli->lli_symlink_name, symlen);
105 /* do not return an error if we cannot cache the symlink locally */
106 if (lli->lli_symlink_name) {
107 memcpy(lli->lli_symlink_name, *symname, symlen);
108 *symname = lli->lli_symlink_name;
116 static int ll_readlink(struct dentry *dentry, char *buffer, int buflen)
118 struct inode *inode = dentry->d_inode;
119 struct ptlrpc_request *request;
124 CDEBUG(D_VFSTRACE, "VFS Op\n");
126 ll_inode_size_lock(inode, 0);
127 rc = ll_readlink_internal(inode, &request, &symname);
131 rc = vfs_readlink(dentry, buffer, buflen, symname);
133 ptlrpc_req_finished(request);
134 ll_inode_size_unlock(inode, 0);
138 #ifdef HAVE_COOKIE_FOLLOW_LINK
139 # define LL_FOLLOW_LINK_RETURN_TYPE void *
141 # define LL_FOLLOW_LINK_RETURN_TYPE int
144 static LL_FOLLOW_LINK_RETURN_TYPE ll_follow_link(struct dentry *dentry,
145 struct nameidata *nd)
147 struct inode *inode = dentry->d_inode;
148 struct ptlrpc_request *request = NULL;
153 CDEBUG(D_VFSTRACE, "VFS Op\n");
154 /* Limit the recursive symlink depth to 5 instead of default
155 * 8 links when kernel has 4k stack to prevent stack overflow.
156 * For 8k stacks we need to limit it to 7 for local servers. */
157 if (THREAD_SIZE < 8192 && current->link_count >= 6) {
159 } else if (THREAD_SIZE == 8192 && current->link_count >= 8) {
162 ll_inode_size_lock(inode, 0);
163 rc = ll_readlink_internal(inode, &request, &symname);
164 ll_inode_size_unlock(inode, 0);
167 cfs_path_put(nd); /* Kernel assumes that ->follow_link()
168 releases nameidata on error */
172 #ifdef HAVE_COOKIE_FOLLOW_LINK
173 nd_set_link(nd, symname);
174 /* @symname may contain a pointer to the request message buffer,
175 we delay request releasing until ll_put_link then. */
178 if (lli->lli_symlink_name == NULL) {
179 /* falling back to recursive follow link if the request
180 * needs to be cleaned up still. */
181 rc = vfs_follow_link(nd, symname);
184 nd_set_link(nd, symname);
188 ptlrpc_req_finished(request);
189 #ifdef HAVE_COOKIE_FOLLOW_LINK
196 #ifdef HAVE_COOKIE_FOLLOW_LINK
197 static void ll_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
199 ptlrpc_req_finished(cookie);
203 struct inode_operations ll_fast_symlink_inode_operations = {
204 .readlink = ll_readlink,
205 .setattr = ll_setattr,
206 .follow_link = ll_follow_link,
207 #ifdef HAVE_COOKIE_FOLLOW_LINK
208 .put_link = ll_put_link,
210 .getattr = ll_getattr,
211 .permission = ll_inode_permission,
212 .setxattr = ll_setxattr,
213 .getxattr = ll_getxattr,
214 .listxattr = ll_listxattr,
215 .removexattr = ll_removexattr,