Whamcloud - gitweb
- unland b_fid to HEAD
[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         struct ll_fid fid;
38         struct mds_body *body;
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         ll_inode2fid(&fid, inode);
51         rc = md_getattr(sbi->ll_mdc_exp, &fid, OBD_MD_LINKNAME, symlen,
52                         request);
53         if (rc) {
54                 if (rc != -ENOENT)
55                         CERROR("inode %lu: rc = %d\n", inode->i_ino, rc);
56                 RETURN(rc);
57         }
58
59         body = lustre_msg_buf ((*request)->rq_repmsg, 0, sizeof (*body));
60         LASSERT (body != NULL);
61         LASSERT_REPSWABBED (*request, 0);
62
63         if ((body->valid & OBD_MD_LINKNAME) == 0) {
64                 CERROR ("OBD_MD_LINKNAME not set on reply\n");
65                 GOTO (failed, rc = -EPROTO);
66         }
67         
68         LASSERT (symlen != 0);
69         if (body->eadatasize != symlen) {
70                 CERROR ("inode %lu: symlink length %d not expected %d\n",
71                         inode->i_ino, body->eadatasize - 1, symlen - 1);
72                 GOTO (failed, rc = -EPROTO);
73         }
74
75         *symname = lustre_msg_buf ((*request)->rq_repmsg, 1, symlen);
76         if (*symname == NULL ||
77             strnlen (*symname, symlen) != symlen - 1) {
78                 /* not full/NULL terminated */
79                 CERROR ("inode %lu: symlink not NULL terminated string"
80                         "of length %d\n", inode->i_ino, symlen - 1);
81                 GOTO (failed, rc = -EPROTO);
82         }
83
84         OBD_ALLOC(lli->lli_symlink_name, symlen);
85         /* do not return an error if we cannot cache the symlink locally */
86         if (lli->lli_symlink_name)
87                 memcpy(lli->lli_symlink_name, *symname, symlen);
88
89         RETURN(0);
90
91  failed:
92         ptlrpc_req_finished (*request);
93         RETURN (-EPROTO);
94 }
95
96 static int ll_readlink(struct dentry *dentry, char *buffer, int buflen)
97 {
98         struct inode *inode = dentry->d_inode;
99         struct ll_inode_info *lli = ll_i2info(inode);
100         struct ptlrpc_request *request;
101         char *symname;
102         int rc;
103         ENTRY;
104
105         CDEBUG(D_VFSTRACE, "VFS Op\n");
106         /* on symlinks lli_open_sem protects lli_symlink_name allocation/data */
107         down(&lli->lli_open_sem);
108         rc = ll_readlink_internal(inode, &request, &symname);
109         if (rc)
110                 GOTO(out, rc);
111
112         rc = vfs_readlink(dentry, buffer, buflen, symname);
113         ptlrpc_req_finished(request);
114  out:
115         up(&lli->lli_open_sem);
116         RETURN(rc);
117 }
118
119 static int ll_follow_link(struct dentry *dentry, struct nameidata *nd)
120 {
121         struct inode *inode = dentry->d_inode;
122         struct ll_inode_info *lli = ll_i2info(inode);
123         struct lookup_intent *it = ll_nd2it(nd);
124         struct ptlrpc_request *request;
125         int rc;
126         char *symname;
127         ENTRY;
128
129         if (it != NULL) {
130                 int op = it->it_op;
131                 int mode = it->it_create_mode;
132
133                 ll_intent_release(it);
134                 it->it_op = op;
135                 it->it_create_mode = mode;
136         }
137
138         CDEBUG(D_VFSTRACE, "VFS Op\n");
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         rc = vfs_follow_link(nd, symname);
146         ptlrpc_req_finished(request);
147  out:
148         RETURN(rc);
149 }
150
151 struct inode_operations ll_fast_symlink_inode_operations = {
152         .readlink       = ll_readlink,
153         .setattr        = ll_setattr,
154         .setattr_raw    = ll_setattr_raw,
155         .follow_link    = ll_follow_link,
156 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
157         .revalidate_it  = ll_inode_revalidate_it
158 #else 
159         .getattr_it     = ll_getattr
160 #endif
161 };