1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2002 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/stat.h>
25 #include <linux/smp_lock.h>
26 #include <linux/version.h>
27 #define DEBUG_SUBSYSTEM S_LLITE
29 #include <lustre_lite.h>
30 #include "llite_internal.h"
32 static int ll_readlink_internal(struct inode *inode,
33 struct ptlrpc_request **request, char **symname)
35 struct ll_inode_info *lli = ll_i2info(inode);
36 struct ll_sb_info *sbi = ll_i2sbi(inode);
37 int rc, symlen = i_size_read(inode) + 1;
38 struct mdt_body *body;
44 if (lli->lli_symlink_name) {
45 *symname = lli->lli_symlink_name;
46 CDEBUG(D_INODE, "using cached symlink %s\n", *symname);
50 oc = ll_mdscapa_get(inode);
51 rc = md_getattr(sbi->ll_md_exp, ll_inode2fid(inode), oc,
52 OBD_MD_LINKNAME, symlen, request);
56 CERROR("inode %lu: rc = %d\n", inode->i_ino, rc);
60 body = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
61 LASSERT(body != NULL);
62 if ((body->valid & OBD_MD_LINKNAME) == 0) {
63 CERROR("OBD_MD_LINKNAME not set on reply\n");
64 GOTO(failed, rc = -EPROTO);
68 if (body->eadatasize != symlen) {
69 CERROR("inode %lu: symlink length %d not expected %d\n",
70 inode->i_ino, body->eadatasize - 1, symlen - 1);
71 GOTO(failed, rc = -EPROTO);
74 *symname = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_MD);
75 if (*symname == NULL ||
76 strnlen(*symname, symlen) != symlen - 1) {
77 /* not full/NULL terminated */
78 CERROR("inode %lu: symlink not NULL terminated string"
79 "of length %d\n", inode->i_ino, symlen - 1);
80 GOTO(failed, rc = -EPROTO);
83 OBD_ALLOC(lli->lli_symlink_name, symlen);
84 /* do not return an error if we cannot cache the symlink locally */
85 if (lli->lli_symlink_name) {
86 memcpy(lli->lli_symlink_name, *symname, symlen);
87 ptlrpc_req_finished (*request);
89 *symname = lli->lli_symlink_name;
95 ptlrpc_req_finished (*request);
99 static int ll_readlink(struct dentry *dentry, char *buffer, int buflen)
101 struct inode *inode = dentry->d_inode;
102 struct ll_inode_info *lli = ll_i2info(inode);
103 struct ptlrpc_request *request;
108 CDEBUG(D_VFSTRACE, "VFS Op\n");
109 /* on symlinks lli_open_sem protects lli_symlink_name allocation/data */
110 down(&lli->lli_size_sem);
111 rc = ll_readlink_internal(inode, &request, &symname);
115 rc = vfs_readlink(dentry, buffer, buflen, symname);
116 ptlrpc_req_finished(request);
118 up(&lli->lli_size_sem);
122 #ifdef HAVE_COOKIE_FOLLOW_LINK
123 # define LL_FOLLOW_LINK_RETURN_TYPE void *
125 # define LL_FOLLOW_LINK_RETURN_TYPE int
128 static LL_FOLLOW_LINK_RETURN_TYPE ll_follow_link(struct dentry *dentry,
129 struct nameidata *nd)
131 struct inode *inode = dentry->d_inode;
132 struct ll_inode_info *lli = ll_i2info(inode);
133 #ifdef HAVE_VFS_INTENT_PATCHES
134 struct lookup_intent *it = ll_nd2it(nd);
136 struct ptlrpc_request *request;
141 #ifdef HAVE_VFS_INTENT_PATCHES
144 int mode = it->it_create_mode;
146 ll_intent_release(it);
148 it->it_create_mode = mode;
152 CDEBUG(D_VFSTRACE, "VFS Op\n");
153 down(&lli->lli_size_sem);
154 rc = ll_readlink_internal(inode, &request, &symname);
155 up(&lli->lli_size_sem);
157 path_release(nd); /* Kernel assumes that ->follow_link()
158 releases nameidata on error */
162 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,8))
163 rc = vfs_follow_link(nd, symname);
165 #ifdef HAVE_COOKIE_FOLLOW_LINK
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. */
171 if (request != NULL) {
172 /* falling back to recursive follow link if the request
173 * needs to be cleaned up still. */
174 rc = vfs_follow_link(nd, symname);
177 nd_set_link(nd, symname);
182 ptlrpc_req_finished(request);
183 #ifdef HAVE_COOKIE_FOLLOW_LINK
190 #ifdef HAVE_COOKIE_FOLLOW_LINK
191 static void ll_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
193 ptlrpc_req_finished(cookie);
197 struct inode_operations ll_fast_symlink_inode_operations = {
198 .readlink = ll_readlink,
199 .setattr = ll_setattr,
200 #ifdef HAVE_VFS_INTENT_PATCHES
201 .setattr_raw = ll_setattr_raw,
203 .follow_link = ll_follow_link,
204 #ifdef HAVE_COOKIE_FOLLOW_LINK
205 .put_link = ll_put_link,
207 .getattr = ll_getattr,
208 .permission = ll_inode_permission,
209 .setxattr = ll_setxattr,
210 .getxattr = ll_getxattr,
211 .listxattr = ll_listxattr,
212 .removexattr = ll_removexattr,