Whamcloud - gitweb
- fixed direct access to obd->obd_namespace in llite. That should be delegated to...
[fs/lustre-release.git] / lustre / llite / symlink.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (c) 2002 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
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.
11  *
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.
16  *
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.
20  */
21
22 #include <linux/fs.h>
23 #include <linux/mm.h>
24 #include <linux/stat.h>
25 #include <linux/smp_lock.h>
26 #include <linux/version.h>
27 #define DEBUG_SUBSYSTEM S_LLITE
28
29 #include <linux/lustre_lite.h>
30 #include "llite_internal.h"
31
32 static int ll_readlink_internal(struct inode *inode,
33                                 struct ptlrpc_request **request, char **symname)
34 {
35         struct ll_inode_info *lli = ll_i2info(inode);
36         struct ll_sb_info *sbi = ll_i2sbi(inode);
37         int rc, symlen = inode->i_size + 1;
38         struct mdt_body *body;
39         ENTRY;
40
41         *request = NULL;
42
43         if (lli->lli_symlink_name) {
44                 *symname = lli->lli_symlink_name;
45                 CDEBUG(D_INODE, "using cached symlink %s\n", *symname);
46                 RETURN(0);
47         }
48
49         rc = md_getattr(sbi->ll_md_exp, ll_inode2fid(inode),
50                         OBD_MD_LINKNAME, symlen, request);
51         if (rc) {
52                 if (rc != -ENOENT)
53                         CERROR("inode %lu: rc = %d\n", inode->i_ino, rc);
54                 GOTO (failed, rc);
55         }
56
57         body = lustre_msg_buf ((*request)->rq_repmsg, 0, sizeof (*body));
58         LASSERT (body != NULL);
59         LASSERT_REPSWABBED (*request, 0);
60
61         if ((body->valid & OBD_MD_LINKNAME) == 0) {
62                 CERROR ("OBD_MD_LINKNAME not set on reply\n");
63                 GOTO (failed, rc = -EPROTO);
64         }
65         
66         LASSERT (symlen != 0);
67         if (body->eadatasize != symlen) {
68                 CERROR ("inode %lu: symlink length %d not expected %d\n",
69                         inode->i_ino, body->eadatasize - 1, symlen - 1);
70                 GOTO (failed, rc = -EPROTO);
71         }
72
73         *symname = lustre_msg_buf ((*request)->rq_repmsg, 1, symlen);
74         if (*symname == NULL ||
75             strnlen (*symname, symlen) != symlen - 1) {
76                 /* not full/NULL terminated */
77                 CERROR ("inode %lu: symlink not NULL terminated string"
78                         "of length %d\n", inode->i_ino, symlen - 1);
79                 GOTO (failed, rc = -EPROTO);
80         }
81
82         OBD_ALLOC(lli->lli_symlink_name, symlen);
83         /* do not return an error if we cannot cache the symlink locally */
84         if (lli->lli_symlink_name)
85                 memcpy(lli->lli_symlink_name, *symname, symlen);
86
87         RETURN(0);
88
89  failed:
90         ptlrpc_req_finished (*request);
91         RETURN (rc);
92 }
93
94 static int ll_readlink(struct dentry *dentry, char *buffer, int buflen)
95 {
96         struct inode *inode = dentry->d_inode;
97         struct ll_inode_info *lli = ll_i2info(inode);
98         struct ptlrpc_request *request;
99         char *symname;
100         int rc;
101         ENTRY;
102
103         CDEBUG(D_VFSTRACE, "VFS Op\n");
104         /* on symlinks lli_open_sem protects lli_symlink_name allocation/data */
105         down(&lli->lli_open_sem);
106         rc = ll_readlink_internal(inode, &request, &symname);
107         if (rc)
108                 GOTO(out, rc);
109
110         rc = vfs_readlink(dentry, buffer, buflen, symname);
111         ptlrpc_req_finished(request);
112  out:
113         up(&lli->lli_open_sem);
114         RETURN(rc);
115 }
116
117 static int ll_follow_link(struct dentry *dentry, struct nameidata *nd)
118 {
119         struct inode *inode = dentry->d_inode;
120         struct ll_inode_info *lli = ll_i2info(inode);
121         struct lookup_intent *it = ll_nd2it(nd);
122         struct ptlrpc_request *request;
123         int rc;
124         char *symname;
125         ENTRY;
126
127         if (it != NULL) {
128                 int op = it->it_op;
129                 int mode = it->it_create_mode;
130
131                 ll_intent_release(it);
132                 it->it_op = op;
133                 it->it_create_mode = mode;
134         }
135
136         CDEBUG(D_VFSTRACE, "VFS Op\n");
137         down(&lli->lli_open_sem);
138         rc = ll_readlink_internal(inode, &request, &symname);
139         up(&lli->lli_open_sem);
140         if (rc) {
141                 path_release(nd); /* Kernel assumes that ->follow_link()
142                                      releases nameidata on error */
143                 GOTO(out, rc);
144         }
145
146         rc = vfs_follow_link(nd, symname);
147         ptlrpc_req_finished(request);
148  out:
149         RETURN(rc);
150 }
151
152 struct inode_operations ll_fast_symlink_inode_operations = {
153         .readlink       = ll_readlink,
154         .setattr        = ll_setattr,
155         .setattr_raw    = ll_setattr_raw,
156         .follow_link    = ll_follow_link,
157 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
158         .revalidate_it  = ll_inode_revalidate_it,
159 #else 
160         .getattr_it     = ll_getattr,
161 #endif
162         .permission     = ll_inode_permission,
163         .setxattr       = ll_setxattr,
164         .getxattr       = ll_getxattr,
165         .listxattr      = ll_listxattr,
166         .removexattr    = ll_removexattr,
167 };