Whamcloud - gitweb
- merge 0.7rc1 from b_devel to HEAD (20030612 merge point)
[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 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
28 #include <asm/statfs.h>
29 #endif
30 #define DEBUG_SUBSYSTEM S_LLITE
31
32 #include <linux/lustre_lite.h>
33
34 static int ll_readlink_internal(struct inode *inode,
35                                 struct ptlrpc_request **request, char **symname)
36 {
37         struct ll_inode_info *lli = ll_i2info(inode);
38         struct ll_sb_info *sbi = ll_i2sbi(inode);
39         struct ll_fid fid;
40         struct mds_body *body;
41         int rc, symlen = inode->i_size + 1;
42         ENTRY;
43
44         *request = NULL;
45
46         if (lli->lli_symlink_name) {
47                 *symname = lli->lli_symlink_name;
48                 CDEBUG(D_INODE, "using cached symlink %s\n", *symname);
49                 RETURN(0);
50         }
51
52         ll_inode2fid(&fid, inode);
53         rc = mdc_getattr(&sbi->ll_mdc_conn, &fid,
54                          OBD_MD_LINKNAME, symlen, request);
55         if (rc) {
56                 CERROR("inode %lu: rc = %d\n", inode->i_ino, rc);
57                 RETURN(rc);
58         }
59
60         body = lustre_msg_buf ((*request)->rq_repmsg, 0, sizeof (*body));
61         LASSERT (body != NULL);
62         LASSERT_REPSWABBED (*request, 0);
63
64         if ((body->valid & OBD_MD_LINKNAME) == 0) {
65                 CERROR ("OBD_MD_LINKNAME not set on reply\n");
66                 GOTO (failed, rc = -EPROTO);
67         }
68         
69         LASSERT (symlen != 0);
70         if (body->eadatasize != symlen) {
71                 CERROR ("inode %lu: symlink length %d not expected %d\n",
72                         inode->i_ino, body->eadatasize - 1, symlen - 1);
73                 GOTO (failed, rc = -EPROTO);
74         }
75
76         *symname = lustre_msg_buf ((*request)->rq_repmsg, 1, symlen);
77         if (*symname == NULL ||
78             strnlen (*symname, symlen) != symlen - 1) {
79                 /* not full/NULL terminated */
80                 CERROR ("inode %lu: symlink not NULL terminated string"
81                         "of length %d\n", inode->i_ino, symlen - 1);
82                 GOTO (failed, rc = -EPROTO);
83         }
84
85         OBD_ALLOC(lli->lli_symlink_name, symlen);
86         /* do not return an error if we cannot cache the symlink locally */
87         if (lli->lli_symlink_name)
88                 memcpy(lli->lli_symlink_name, *symname, symlen);
89
90         RETURN(0);
91
92  failed:
93         ptlrpc_req_finished (*request);
94         RETURN (-EPROTO);
95 }
96
97 static int ll_readlink(struct dentry *dentry, char *buffer, int buflen)
98 {
99         struct inode *inode = dentry->d_inode;
100         struct ll_inode_info *lli = ll_i2info(inode);
101         struct ptlrpc_request *request;
102         char *symname;
103         int rc;
104         ENTRY;
105
106         CDEBUG(D_VFSTRACE, "VFS Op\n");
107         /* on symlinks lli_open_sem protects lli_symlink_name allocation/data */
108         down(&lli->lli_open_sem);
109         rc = ll_readlink_internal(inode, &request, &symname);
110         if (rc)
111                 GOTO(out, rc);
112
113         rc = vfs_readlink(dentry, buffer, buflen, symname);
114         ptlrpc_req_finished(request);
115  out:
116         up(&lli->lli_open_sem);
117         RETURN(rc);
118 }
119
120 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
121 static int ll_follow_link(struct dentry *dentry, struct nameidata *nd,
122                           struct lookup_intent *it)
123 {
124         struct inode *inode = dentry->d_inode;
125         struct ll_inode_info *lli = ll_i2info(inode);
126         struct ptlrpc_request *request;
127         int op = 0, mode = 0, rc;
128         char *symname;
129         ENTRY;
130
131         CDEBUG(D_VFSTRACE, "VFS Op\n");
132         if (it != NULL) {
133                 op = it->it_op;
134                 mode = it->it_mode;
135
136                 ll_intent_release(dentry, it);
137         }
138
139         down(&lli->lli_open_sem);
140         rc = ll_readlink_internal(inode, &request, &symname);
141         up(&lli->lli_open_sem);
142         if (rc)
143                 GOTO(out, rc);
144
145         if (it != NULL) {
146                 it->it_op = op;
147                 it->it_mode = mode;
148         }
149
150         rc = vfs_follow_link_it(nd, symname, it);
151         ptlrpc_req_finished(request);
152  out:
153         RETURN(rc);
154 }
155 #else
156 static int ll_follow_link(struct dentry *dentry, struct nameidata *nd)
157 {
158         struct inode *inode = dentry->d_inode;
159         struct ll_inode_info *lli = ll_i2info(inode);
160         struct ptlrpc_request *request;
161         int op = 0, mode = 0, rc;
162         char *symname;
163         ENTRY;
164
165         op = nd->it.it_op;
166         mode = nd->it.it_mode;
167
168         ll_intent_release(dentry, &nd->it);
169
170         down(&lli->lli_open_sem);
171
172         rc = ll_readlink_internal(inode, &request, &symname);
173         if (rc)
174                 GOTO(out, rc);
175
176         nd->it.it_op = op;
177         nd->it.it_mode = mode;
178
179         rc = vfs_follow_link(nd, symname);
180         ptlrpc_req_finished(request);
181  out:
182         up(&lli->lli_open_sem);
183
184         RETURN(rc);
185 }
186 #endif
187
188 extern int ll_inode_revalidate(struct dentry *dentry);
189 extern int ll_setattr(struct dentry *de, struct iattr *attr);
190 struct inode_operations ll_fast_symlink_inode_operations = {
191         readlink:       ll_readlink,
192         setattr:        ll_setattr,
193         setattr_raw:    ll_setattr_raw,
194         follow_link2:   ll_follow_link,
195 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
196         revalidate:     ll_inode_revalidate
197 #endif
198 };