blob: 900c8bb1c3b8af49c7aeb055b4f846095135a128 [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>
22#include "volume.h"
23#include "vnode.h"
24#include "super.h"
25#include "internal.h"
26
27struct afs_iget_data {
28 struct afs_fid fid;
29 struct afs_volume *volume; /* volume on which resides */
30};
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032/*
33 * map the AFS file status to the inode member variables
34 */
35static int afs_inode_map_status(struct afs_vnode *vnode)
36{
37 struct inode *inode = AFS_VNODE_TO_I(vnode);
38
39 _debug("FS: ft=%d lk=%d sz=%Zu ver=%Lu mod=%hu",
40 vnode->status.type,
41 vnode->status.nlink,
42 vnode->status.size,
43 vnode->status.version,
44 vnode->status.mode);
45
46 switch (vnode->status.type) {
47 case AFS_FTYPE_FILE:
48 inode->i_mode = S_IFREG | vnode->status.mode;
49 inode->i_op = &afs_file_inode_operations;
Christoph Hellwiga463ddd2005-11-07 00:59:46 -080050 inode->i_fop = &generic_ro_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 break;
52 case AFS_FTYPE_DIR:
53 inode->i_mode = S_IFDIR | vnode->status.mode;
54 inode->i_op = &afs_dir_inode_operations;
55 inode->i_fop = &afs_dir_file_operations;
56 break;
57 case AFS_FTYPE_SYMLINK:
58 inode->i_mode = S_IFLNK | vnode->status.mode;
59 inode->i_op = &page_symlink_inode_operations;
60 break;
61 default:
62 printk("kAFS: AFS vnode with undefined type\n");
63 return -EBADMSG;
64 }
65
66 inode->i_nlink = vnode->status.nlink;
67 inode->i_uid = vnode->status.owner;
68 inode->i_gid = 0;
69 inode->i_size = vnode->status.size;
70 inode->i_ctime.tv_sec = vnode->status.mtime_server;
71 inode->i_ctime.tv_nsec = 0;
72 inode->i_atime = inode->i_mtime = inode->i_ctime;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 inode->i_blocks = 0;
74 inode->i_version = vnode->fid.unique;
75 inode->i_mapping->a_ops = &afs_fs_aops;
76
77 /* check to see whether a symbolic link is really a mountpoint */
78 if (vnode->status.type == AFS_FTYPE_SYMLINK) {
79 afs_mntpt_check_symlink(vnode);
80
81 if (vnode->flags & AFS_VNODE_MOUNTPOINT) {
82 inode->i_mode = S_IFDIR | vnode->status.mode;
83 inode->i_op = &afs_mntpt_inode_operations;
84 inode->i_fop = &afs_mntpt_file_operations;
85 }
86 }
87
88 return 0;
David Howellsec268152007-04-26 15:49:28 -070089}
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/*
92 * attempt to fetch the status of an inode, coelescing multiple simultaneous
93 * fetches
94 */
95static int afs_inode_fetch_status(struct inode *inode)
96{
97 struct afs_vnode *vnode;
98 int ret;
99
100 vnode = AFS_FS_I(inode);
101
102 ret = afs_vnode_fetch_status(vnode);
103
104 if (ret == 0)
105 ret = afs_inode_map_status(vnode);
106
107 return ret;
David Howellsec268152007-04-26 15:49:28 -0700108}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110/*
111 * iget5() comparator
112 */
113static int afs_iget5_test(struct inode *inode, void *opaque)
114{
115 struct afs_iget_data *data = opaque;
116
117 return inode->i_ino == data->fid.vnode &&
118 inode->i_version == data->fid.unique;
David Howellsec268152007-04-26 15:49:28 -0700119}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121/*
122 * iget5() inode initialiser
123 */
124static int afs_iget5_set(struct inode *inode, void *opaque)
125{
126 struct afs_iget_data *data = opaque;
127 struct afs_vnode *vnode = AFS_FS_I(inode);
128
129 inode->i_ino = data->fid.vnode;
130 inode->i_version = data->fid.unique;
131 vnode->fid = data->fid;
132 vnode->volume = data->volume;
133
134 return 0;
David Howellsec268152007-04-26 15:49:28 -0700135}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137/*
138 * inode retrieval
139 */
140inline int afs_iget(struct super_block *sb, struct afs_fid *fid,
141 struct inode **_inode)
142{
143 struct afs_iget_data data = { .fid = *fid };
144 struct afs_super_info *as;
145 struct afs_vnode *vnode;
146 struct inode *inode;
147 int ret;
148
149 _enter(",{%u,%u,%u},,", fid->vid, fid->vnode, fid->unique);
150
151 as = sb->s_fs_info;
152 data.volume = as->volume;
153
154 inode = iget5_locked(sb, fid->vnode, afs_iget5_test, afs_iget5_set,
155 &data);
156 if (!inode) {
157 _leave(" = -ENOMEM");
158 return -ENOMEM;
159 }
160
161 vnode = AFS_FS_I(inode);
162
163 /* deal with an existing inode */
164 if (!(inode->i_state & I_NEW)) {
165 ret = afs_vnode_fetch_status(vnode);
David Howellsec268152007-04-26 15:49:28 -0700166 if (ret == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 *_inode = inode;
168 else
169 iput(inode);
170 _leave(" = %d", ret);
171 return ret;
172 }
173
174#ifdef AFS_CACHING_SUPPORT
175 /* set up caching before reading the status, as fetch-status reads the
176 * first page of symlinks to see if they're really mntpts */
177 cachefs_acquire_cookie(vnode->volume->cache,
178 NULL,
179 vnode,
180 &vnode->cache);
181#endif
182
183 /* okay... it's a new inode */
184 inode->i_flags |= S_NOATIME;
185 vnode->flags |= AFS_VNODE_CHANGED;
186 ret = afs_inode_fetch_status(inode);
187 if (ret<0)
188 goto bad_inode;
189
190 /* success */
191 unlock_new_inode(inode);
192
193 *_inode = inode;
194 _leave(" = 0 [CB { v=%u x=%lu t=%u }]",
195 vnode->cb_version,
196 vnode->cb_timeout.timo_jif,
197 vnode->cb_type);
198 return 0;
199
200 /* failure */
David Howellsec268152007-04-26 15:49:28 -0700201bad_inode:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 make_bad_inode(inode);
203 unlock_new_inode(inode);
204 iput(inode);
205
206 _leave(" = %d [bad]", ret);
207 return ret;
David Howellsec268152007-04-26 15:49:28 -0700208}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210/*
211 * read the attributes of an inode
212 */
213int afs_inode_getattr(struct vfsmount *mnt, struct dentry *dentry,
214 struct kstat *stat)
215{
216 struct afs_vnode *vnode;
217 struct inode *inode;
218 int ret;
219
220 inode = dentry->d_inode;
221
222 _enter("{ ino=%lu v=%lu }", inode->i_ino, inode->i_version);
223
224 vnode = AFS_FS_I(inode);
225
226 ret = afs_inode_fetch_status(inode);
227 if (ret == -ENOENT) {
228 _leave(" = %d [%d %p]",
229 ret, atomic_read(&dentry->d_count), dentry->d_inode);
230 return ret;
David Howellsec268152007-04-26 15:49:28 -0700231 } else if (ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 make_bad_inode(inode);
233 _leave(" = %d", ret);
234 return ret;
235 }
236
237 /* transfer attributes from the inode structure to the stat
238 * structure */
239 generic_fillattr(inode, stat);
240
241 _leave(" = 0 CB { v=%u x=%u t=%u }",
242 vnode->cb_version,
243 vnode->cb_expiry,
244 vnode->cb_type);
245
246 return 0;
David Howellsec268152007-04-26 15:49:28 -0700247}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249/*
250 * clear an AFS inode
251 */
252void afs_clear_inode(struct inode *inode)
253{
254 struct afs_vnode *vnode;
255
256 vnode = AFS_FS_I(inode);
257
258 _enter("ino=%lu { vn=%08x v=%u x=%u t=%u }",
259 inode->i_ino,
260 vnode->fid.vnode,
261 vnode->cb_version,
262 vnode->cb_expiry,
263 vnode->cb_type
264 );
265
266 BUG_ON(inode->i_ino != vnode->fid.vnode);
267
268 afs_vnode_give_up_callback(vnode);
269
270#ifdef AFS_CACHING_SUPPORT
271 cachefs_relinquish_cookie(vnode->cache, 0);
272 vnode->cache = NULL;
273#endif
274
275 _leave("");
David Howellsec268152007-04-26 15:49:28 -0700276}