Whamcloud - gitweb
Merge b_md into 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 #define DEBUG_SUBSYSTEM S_LLITE
27
28 #include <linux/lustre_lite.h>
29
30 static int ll_readlink_internal(struct inode *inode,
31                                 struct ptlrpc_request **request, char **symname)
32 {
33         struct ll_inode_info *lli = ll_i2info(inode);
34         struct ll_sb_info *sbi = ll_i2sbi(inode);
35         int rc, symlen = inode->i_size + 1;
36         ENTRY;
37
38         *request = NULL;
39
40         if (lli->lli_symlink_name) {
41                 *symname = lli->lli_symlink_name;
42                 CDEBUG(D_INODE, "using cached symlink %s\n", *symname);
43                 RETURN(0);
44         }
45
46         rc = mdc_getattr(&sbi->ll_mdc_conn, inode->i_ino, S_IFLNK,
47                          OBD_MD_LINKNAME, symlen, request);
48         if (rc) {
49                 CERROR("inode %lu: rc = %d\n", inode->i_ino, rc);
50                 RETURN(rc);
51         }
52
53         *symname = lustre_msg_buf((*request)->rq_repmsg, 1);
54
55         OBD_ALLOC(lli->lli_symlink_name, symlen);
56         /* do not return an error if we cannot cache the symlink locally */
57         if (lli->lli_symlink_name)
58                 memcpy(lli->lli_symlink_name, *symname, symlen);
59
60         RETURN(0);
61 }
62
63 static int ll_readlink(struct dentry *dentry, char *buffer, int buflen)
64 {
65         struct inode *inode = dentry->d_inode;
66         struct ll_inode_info *lli = ll_i2info(inode);
67         struct ptlrpc_request *request;
68         char *symname;
69         int rc;
70         ENTRY;
71
72         /* on symlinks lli_open_sem protects lli_symlink_name allocation/data */
73         down(&lli->lli_open_sem);
74         rc = ll_readlink_internal(inode, &request, &symname);
75         if (rc)
76                 GOTO(out, rc);
77
78         rc = vfs_readlink(dentry, buffer, buflen, symname);
79  out:
80         up(&lli->lli_open_sem);
81         ptlrpc_req_finished(request);
82
83         RETURN(rc);
84 }
85
86 static int ll_follow_link(struct dentry *dentry, struct nameidata *nd,
87                           struct lookup_intent *it)
88 {
89         struct inode *inode = dentry->d_inode;
90         struct ll_inode_info *lli = ll_i2info(inode);
91         struct ptlrpc_request *request;
92         int op = 0, mode = 0, rc;
93         char *symname;
94         ENTRY;
95
96         if (it != NULL) {
97                 op = it->it_op;
98                 mode = it->it_mode;
99
100                 ll_intent_release(dentry, it);
101         }
102
103         down(&lli->lli_open_sem);
104         rc = ll_readlink_internal(inode, &request, &symname);
105         up(&lli->lli_open_sem);
106         if (rc)
107                 GOTO(out, rc);
108
109         if (it != NULL) {
110                 it->it_op = op;
111                 it->it_mode = mode;
112         }
113
114         rc = vfs_follow_link_it(nd, symname, it);
115  out:
116         ptlrpc_req_finished(request);
117
118         RETURN(rc);
119 }
120
121 extern int ll_inode_revalidate(struct dentry *dentry);
122 extern int ll_setattr(struct dentry *de, struct iattr *attr);
123 struct inode_operations ll_fast_symlink_inode_operations = {
124         readlink:       ll_readlink,
125         setattr:        ll_setattr,
126         follow_link2:   ll_follow_link,
127         revalidate:     ll_inode_revalidate
128 };