blob: 2273362282996d68f73bc6f498651e061dc4ccd8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 2002 Red Hat, Inc. All rights reserved.
3 *
4 * This software may be freely redistributed under the terms of the
5 * GNU General Public License.
6 *
7 * You should have received a copy of the GNU General Public License
8 * along with this program; if not, write to the Free Software
9 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
10 *
11 * Authors: David Woodhouse <dwmw2@cambridge.redhat.com>
12 * David Howells <dhowells@redhat.com>
13 *
14 */
15
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/slab.h>
20#include <linux/fs.h>
21#include <linux/pagemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "internal.h"
23
24struct afs_iget_data {
25 struct afs_fid fid;
26 struct afs_volume *volume; /* volume on which resides */
27};
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029/*
30 * map the AFS file status to the inode member variables
31 */
David Howells00d3b7a2007-04-26 15:57:07 -070032static int afs_inode_map_status(struct afs_vnode *vnode, struct key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
34 struct inode *inode = AFS_VNODE_TO_I(vnode);
35
36 _debug("FS: ft=%d lk=%d sz=%Zu ver=%Lu mod=%hu",
37 vnode->status.type,
38 vnode->status.nlink,
39 vnode->status.size,
David Howells08e0e7c2007-04-26 15:55:03 -070040 vnode->status.data_version,
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 vnode->status.mode);
42
43 switch (vnode->status.type) {
44 case AFS_FTYPE_FILE:
45 inode->i_mode = S_IFREG | vnode->status.mode;
46 inode->i_op = &afs_file_inode_operations;
David Howells00d3b7a2007-04-26 15:57:07 -070047 inode->i_fop = &afs_file_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 break;
49 case AFS_FTYPE_DIR:
50 inode->i_mode = S_IFDIR | vnode->status.mode;
51 inode->i_op = &afs_dir_inode_operations;
52 inode->i_fop = &afs_dir_file_operations;
53 break;
54 case AFS_FTYPE_SYMLINK:
55 inode->i_mode = S_IFLNK | vnode->status.mode;
56 inode->i_op = &page_symlink_inode_operations;
57 break;
58 default:
59 printk("kAFS: AFS vnode with undefined type\n");
60 return -EBADMSG;
61 }
62
63 inode->i_nlink = vnode->status.nlink;
64 inode->i_uid = vnode->status.owner;
65 inode->i_gid = 0;
66 inode->i_size = vnode->status.size;
67 inode->i_ctime.tv_sec = vnode->status.mtime_server;
68 inode->i_ctime.tv_nsec = 0;
69 inode->i_atime = inode->i_mtime = inode->i_ctime;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 inode->i_blocks = 0;
71 inode->i_version = vnode->fid.unique;
72 inode->i_mapping->a_ops = &afs_fs_aops;
73
74 /* check to see whether a symbolic link is really a mountpoint */
75 if (vnode->status.type == AFS_FTYPE_SYMLINK) {
David Howells00d3b7a2007-04-26 15:57:07 -070076 afs_mntpt_check_symlink(vnode, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
David Howells08e0e7c2007-04-26 15:55:03 -070078 if (test_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 inode->i_mode = S_IFDIR | vnode->status.mode;
80 inode->i_op = &afs_mntpt_inode_operations;
81 inode->i_fop = &afs_mntpt_file_operations;
82 }
83 }
84
85 return 0;
David Howellsec268152007-04-26 15:49:28 -070086}
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Linus Torvalds1da177e2005-04-16 15:20:36 -070088/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 * iget5() comparator
90 */
91static int afs_iget5_test(struct inode *inode, void *opaque)
92{
93 struct afs_iget_data *data = opaque;
94
95 return inode->i_ino == data->fid.vnode &&
96 inode->i_version == data->fid.unique;
David Howellsec268152007-04-26 15:49:28 -070097}
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Linus Torvalds1da177e2005-04-16 15:20:36 -070099/*
100 * iget5() inode initialiser
101 */
102static int afs_iget5_set(struct inode *inode, void *opaque)
103{
104 struct afs_iget_data *data = opaque;
105 struct afs_vnode *vnode = AFS_FS_I(inode);
106
107 inode->i_ino = data->fid.vnode;
108 inode->i_version = data->fid.unique;
109 vnode->fid = data->fid;
110 vnode->volume = data->volume;
111
112 return 0;
David Howellsec268152007-04-26 15:49:28 -0700113}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115/*
116 * inode retrieval
117 */
David Howells00d3b7a2007-04-26 15:57:07 -0700118inline struct inode *afs_iget(struct super_block *sb, struct key *key,
119 struct afs_fid *fid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
121 struct afs_iget_data data = { .fid = *fid };
122 struct afs_super_info *as;
123 struct afs_vnode *vnode;
124 struct inode *inode;
125 int ret;
126
127 _enter(",{%u,%u,%u},,", fid->vid, fid->vnode, fid->unique);
128
129 as = sb->s_fs_info;
130 data.volume = as->volume;
131
132 inode = iget5_locked(sb, fid->vnode, afs_iget5_test, afs_iget5_set,
133 &data);
134 if (!inode) {
135 _leave(" = -ENOMEM");
David Howells08e0e7c2007-04-26 15:55:03 -0700136 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 }
138
David Howells08e0e7c2007-04-26 15:55:03 -0700139 _debug("GOT INODE %p { vl=%x vn=%x, u=%x }",
140 inode, fid->vid, fid->vnode, fid->unique);
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 vnode = AFS_FS_I(inode);
143
144 /* deal with an existing inode */
145 if (!(inode->i_state & I_NEW)) {
David Howells08e0e7c2007-04-26 15:55:03 -0700146 _leave(" = %p", inode);
147 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 }
149
150#ifdef AFS_CACHING_SUPPORT
151 /* set up caching before reading the status, as fetch-status reads the
152 * first page of symlinks to see if they're really mntpts */
153 cachefs_acquire_cookie(vnode->volume->cache,
154 NULL,
155 vnode,
156 &vnode->cache);
157#endif
158
159 /* okay... it's a new inode */
David Howells08e0e7c2007-04-26 15:55:03 -0700160 set_bit(AFS_VNODE_CB_BROKEN, &vnode->flags);
David Howells00d3b7a2007-04-26 15:57:07 -0700161 ret = afs_vnode_fetch_status(vnode, NULL, key);
David Howells08e0e7c2007-04-26 15:55:03 -0700162 if (ret < 0)
163 goto bad_inode;
David Howells00d3b7a2007-04-26 15:57:07 -0700164 ret = afs_inode_map_status(vnode, key);
David Howells08e0e7c2007-04-26 15:55:03 -0700165 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 goto bad_inode;
167
168 /* success */
David Howells08e0e7c2007-04-26 15:55:03 -0700169 inode->i_flags |= S_NOATIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 unlock_new_inode(inode);
David Howells08e0e7c2007-04-26 15:55:03 -0700171 _leave(" = %p [CB { v=%u t=%u }]", inode, vnode->cb_version, vnode->cb_type);
172 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 /* failure */
David Howellsec268152007-04-26 15:49:28 -0700175bad_inode:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 make_bad_inode(inode);
177 unlock_new_inode(inode);
178 iput(inode);
179
180 _leave(" = %d [bad]", ret);
David Howells08e0e7c2007-04-26 15:55:03 -0700181 return ERR_PTR(ret);
David Howellsec268152007-04-26 15:49:28 -0700182}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184/*
185 * read the attributes of an inode
186 */
187int afs_inode_getattr(struct vfsmount *mnt, struct dentry *dentry,
188 struct kstat *stat)
189{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 inode = dentry->d_inode;
193
194 _enter("{ ino=%lu v=%lu }", inode->i_ino, inode->i_version);
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 generic_fillattr(inode, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return 0;
David Howellsec268152007-04-26 15:49:28 -0700198}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200/*
201 * clear an AFS inode
202 */
203void afs_clear_inode(struct inode *inode)
204{
David Howells00d3b7a2007-04-26 15:57:07 -0700205 struct afs_permits *permits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 struct afs_vnode *vnode;
207
208 vnode = AFS_FS_I(inode);
209
210 _enter("ino=%lu { vn=%08x v=%u x=%u t=%u }",
211 inode->i_ino,
212 vnode->fid.vnode,
213 vnode->cb_version,
214 vnode->cb_expiry,
David Howells08e0e7c2007-04-26 15:55:03 -0700215 vnode->cb_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
David Howells08e0e7c2007-04-26 15:55:03 -0700217 _debug("CLEAR INODE %p", inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
David Howells08e0e7c2007-04-26 15:55:03 -0700219 ASSERTCMP(inode->i_ino, ==, vnode->fid.vnode);
220
221 afs_give_up_callback(vnode);
222
223 if (vnode->server) {
224 spin_lock(&vnode->server->fs_lock);
225 rb_erase(&vnode->server_rb, &vnode->server->fs_vnodes);
226 spin_unlock(&vnode->server->fs_lock);
227 afs_put_server(vnode->server);
228 vnode->server = NULL;
229 }
230
231 ASSERT(!vnode->cb_promised);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233#ifdef AFS_CACHING_SUPPORT
234 cachefs_relinquish_cookie(vnode->cache, 0);
235 vnode->cache = NULL;
236#endif
237
David Howells00d3b7a2007-04-26 15:57:07 -0700238 mutex_lock(&vnode->permits_lock);
239 permits = vnode->permits;
240 rcu_assign_pointer(vnode->permits, NULL);
241 mutex_unlock(&vnode->permits_lock);
242 if (permits)
243 call_rcu(&permits->rcu, afs_zap_permits);
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 _leave("");
David Howellsec268152007-04-26 15:49:28 -0700246}