Whamcloud - gitweb
land 0.5.20.3 b_devel onto HEAD (b_devel will remain)
[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         int rc, symlen = inode->i_size + 1;
40         ENTRY;
41
42         *request = NULL;
43
44         if (lli->lli_symlink_name) {
45                 *symname = lli->lli_symlink_name;
46                 CDEBUG(D_INODE, "using cached symlink %s\n", *symname);
47                 RETURN(0);
48         }
49
50         rc = mdc_getattr(&sbi->ll_mdc_conn, inode->i_ino, S_IFLNK,
51                          OBD_MD_LINKNAME, symlen, request);
52         if (rc) {
53                 CERROR("inode %lu: rc = %d\n", inode->i_ino, rc);
54                 RETURN(rc);
55         }
56
57         *symname = lustre_msg_buf((*request)->rq_repmsg, 1);
58
59         OBD_ALLOC(lli->lli_symlink_name, symlen);
60         /* do not return an error if we cannot cache the symlink locally */
61         if (lli->lli_symlink_name)
62                 memcpy(lli->lli_symlink_name, *symname, symlen);
63
64         RETURN(0);
65 }
66
67 static int ll_readlink(struct dentry *dentry, char *buffer, int buflen)
68 {
69         struct inode *inode = dentry->d_inode;
70         struct ll_inode_info *lli = ll_i2info(inode);
71         struct ptlrpc_request *request;
72         char *symname;
73         int rc;
74         ENTRY;
75
76         CDEBUG(D_VFSTRACE, "VFS Op\n");
77         /* on symlinks lli_open_sem protects lli_symlink_name allocation/data */
78         down(&lli->lli_open_sem);
79         rc = ll_readlink_internal(inode, &request, &symname);
80         if (rc)
81                 GOTO(out, rc);
82
83         rc = vfs_readlink(dentry, buffer, buflen, symname);
84  out:
85         up(&lli->lli_open_sem);
86         ptlrpc_req_finished(request);
87
88         RETURN(rc);
89 }
90
91 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
92 static int ll_follow_link(struct dentry *dentry, struct nameidata *nd,
93                           struct lookup_intent *it)
94 {
95         struct inode *inode = dentry->d_inode;
96         struct ll_inode_info *lli = ll_i2info(inode);
97         struct ptlrpc_request *request;
98         int op = 0, mode = 0, rc;
99         char *symname;
100         ENTRY;
101
102         CDEBUG(D_VFSTRACE, "VFS Op\n");
103         if (it != NULL) {
104                 op = it->it_op;
105                 mode = it->it_mode;
106
107                 ll_intent_release(dentry, it);
108         }
109
110         down(&lli->lli_open_sem);
111         rc = ll_readlink_internal(inode, &request, &symname);
112         up(&lli->lli_open_sem);
113         if (rc)
114                 GOTO(out, rc);
115
116         if (it != NULL) {
117                 it->it_op = op;
118                 it->it_mode = mode;
119         }
120
121         rc = vfs_follow_link_it(nd, symname, it);
122  out:
123         ptlrpc_req_finished(request);
124
125         RETURN(rc);
126 }
127 #else
128 static int ll_follow_link(struct dentry *dentry, struct nameidata *nd)
129 {
130         struct inode *inode = dentry->d_inode;
131         struct ll_inode_info *lli = ll_i2info(inode);
132         struct ptlrpc_request *request;
133         int op = 0, mode = 0, rc;
134         char *symname;
135         ENTRY;
136
137         op = nd->it.it_op;
138         mode = nd->it.it_mode;
139
140         ll_intent_release(dentry, &nd->it);
141
142         down(&lli->lli_open_sem);
143
144         rc = ll_readlink_internal(inode, &request, &symname);
145         if (rc)
146                 GOTO(out, rc);
147
148         nd->it.it_op = op;
149         nd->it.it_mode = mode;
150
151         rc = vfs_follow_link(nd, symname);
152  out:
153         up(&lli->lli_open_sem);
154         ptlrpc_req_finished(request);
155
156         RETURN(rc);
157 }
158 #endif
159
160 extern int ll_inode_revalidate(struct dentry *dentry);
161 extern int ll_setattr(struct dentry *de, struct iattr *attr);
162 struct inode_operations ll_fast_symlink_inode_operations = {
163         readlink:       ll_readlink,
164         setattr:        ll_setattr,
165         setattr_raw:    ll_setattr_raw,
166         follow_link2:   ll_follow_link,
167 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
168         revalidate:     ll_inode_revalidate
169 #endif
170 };