blob: 9c984cc42cc9935f7ae7f531aef17b55882c119f [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
David Howells260a9802007-04-26 15:59:35 -070036 _debug("FS: ft=%d lk=%d sz=%llu ver=%Lu mod=%hu",
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 vnode->status.type,
38 vnode->status.nlink,
David S. Millerba3e0e12007-04-26 16:06:22 -070039 (unsigned long long) 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 Howells260a9802007-04-26 15:59:35 -0700118struct inode *afs_iget(struct super_block *sb, struct key *key,
119 struct afs_fid *fid, struct afs_file_status *status,
120 struct afs_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
122 struct afs_iget_data data = { .fid = *fid };
123 struct afs_super_info *as;
124 struct afs_vnode *vnode;
125 struct inode *inode;
126 int ret;
127
David Howells416351f2007-05-09 02:33:45 -0700128 _enter(",{%x:%u.%u},,", fid->vid, fid->vnode, fid->unique);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 as = sb->s_fs_info;
131 data.volume = as->volume;
132
133 inode = iget5_locked(sb, fid->vnode, afs_iget5_test, afs_iget5_set,
134 &data);
135 if (!inode) {
136 _leave(" = -ENOMEM");
David Howells08e0e7c2007-04-26 15:55:03 -0700137 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 }
139
David Howells08e0e7c2007-04-26 15:55:03 -0700140 _debug("GOT INODE %p { vl=%x vn=%x, u=%x }",
141 inode, fid->vid, fid->vnode, fid->unique);
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 vnode = AFS_FS_I(inode);
144
145 /* deal with an existing inode */
146 if (!(inode->i_state & I_NEW)) {
David Howells08e0e7c2007-04-26 15:55:03 -0700147 _leave(" = %p", inode);
148 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 }
150
151#ifdef AFS_CACHING_SUPPORT
152 /* set up caching before reading the status, as fetch-status reads the
153 * first page of symlinks to see if they're really mntpts */
154 cachefs_acquire_cookie(vnode->volume->cache,
155 NULL,
156 vnode,
157 &vnode->cache);
158#endif
159
David Howells260a9802007-04-26 15:59:35 -0700160 if (!status) {
161 /* it's a remotely extant inode */
162 set_bit(AFS_VNODE_CB_BROKEN, &vnode->flags);
163 ret = afs_vnode_fetch_status(vnode, NULL, key);
164 if (ret < 0)
165 goto bad_inode;
166 } else {
167 /* it's an inode we just created */
168 memcpy(&vnode->status, status, sizeof(vnode->status));
169
170 if (!cb) {
171 /* it's a symlink we just created (the fileserver
172 * didn't give us a callback) */
173 vnode->cb_version = 0;
174 vnode->cb_expiry = 0;
175 vnode->cb_type = 0;
176 vnode->cb_expires = get_seconds();
177 } else {
178 vnode->cb_version = cb->version;
179 vnode->cb_expiry = cb->expiry;
180 vnode->cb_type = cb->type;
181 vnode->cb_expires = vnode->cb_expiry + get_seconds();
182 }
183 }
184
David Howells00d3b7a2007-04-26 15:57:07 -0700185 ret = afs_inode_map_status(vnode, key);
David Howells08e0e7c2007-04-26 15:55:03 -0700186 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 goto bad_inode;
188
189 /* success */
David Howells260a9802007-04-26 15:59:35 -0700190 clear_bit(AFS_VNODE_UNSET, &vnode->flags);
David Howells08e0e7c2007-04-26 15:55:03 -0700191 inode->i_flags |= S_NOATIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 unlock_new_inode(inode);
David Howells08e0e7c2007-04-26 15:55:03 -0700193 _leave(" = %p [CB { v=%u t=%u }]", inode, vnode->cb_version, vnode->cb_type);
194 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 /* failure */
David Howellsec268152007-04-26 15:49:28 -0700197bad_inode:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 make_bad_inode(inode);
199 unlock_new_inode(inode);
200 iput(inode);
201
202 _leave(" = %d [bad]", ret);
David Howells08e0e7c2007-04-26 15:55:03 -0700203 return ERR_PTR(ret);
David Howellsec268152007-04-26 15:49:28 -0700204}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206/*
David Howells416351f2007-05-09 02:33:45 -0700207 * mark the data attached to an inode as obsolete due to a write on the server
208 * - might also want to ditch all the outstanding writes and dirty pages
209 */
210void afs_zap_data(struct afs_vnode *vnode)
211{
212 kenter("zap data {%x:%u}", vnode->fid.vid, vnode->fid.vnode);
213
214 /* nuke all the non-dirty pages that aren't locked, mapped or being
215 * written back */
216 invalidate_remote_inode(&vnode->vfs_inode);
217}
218
219/*
David Howells260a9802007-04-26 15:59:35 -0700220 * validate a vnode/inode
221 * - there are several things we need to check
222 * - parent dir data changes (rm, rmdir, rename, mkdir, create, link,
223 * symlink)
224 * - parent dir metadata changed (security changes)
225 * - dentry data changed (write, truncate)
226 * - dentry metadata changed (security changes)
227 */
228int afs_validate(struct afs_vnode *vnode, struct key *key)
229{
230 int ret;
231
232 _enter("{v={%x:%u} fl=%lx},%x",
233 vnode->fid.vid, vnode->fid.vnode, vnode->flags,
234 key_serial(key));
235
236 if (vnode->cb_promised &&
237 !test_bit(AFS_VNODE_CB_BROKEN, &vnode->flags) &&
238 !test_bit(AFS_VNODE_MODIFIED, &vnode->flags) &&
239 !test_bit(AFS_VNODE_ZAP_DATA, &vnode->flags)) {
240 if (vnode->cb_expires < get_seconds() + 10) {
241 _debug("callback expired");
242 set_bit(AFS_VNODE_CB_BROKEN, &vnode->flags);
243 } else {
244 goto valid;
245 }
246 }
247
248 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
249 goto valid;
250
251 mutex_lock(&vnode->validate_lock);
252
253 /* if the promise has expired, we need to check the server again to get
254 * a new promise - note that if the (parent) directory's metadata was
255 * changed then the security may be different and we may no longer have
256 * access */
257 if (!vnode->cb_promised ||
258 test_bit(AFS_VNODE_CB_BROKEN, &vnode->flags)) {
259 _debug("not promised");
260 ret = afs_vnode_fetch_status(vnode, NULL, key);
261 if (ret < 0)
262 goto error_unlock;
263 _debug("new promise [fl=%lx]", vnode->flags);
264 }
265
266 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
267 _debug("file already deleted");
268 ret = -ESTALE;
269 goto error_unlock;
270 }
271
272 /* if the vnode's data version number changed then its contents are
273 * different */
David Howells416351f2007-05-09 02:33:45 -0700274 if (test_and_clear_bit(AFS_VNODE_ZAP_DATA, &vnode->flags))
275 afs_zap_data(vnode);
David Howells260a9802007-04-26 15:59:35 -0700276
277 clear_bit(AFS_VNODE_MODIFIED, &vnode->flags);
278 mutex_unlock(&vnode->validate_lock);
279valid:
280 _leave(" = 0");
281 return 0;
282
283error_unlock:
284 mutex_unlock(&vnode->validate_lock);
285 _leave(" = %d", ret);
286 return ret;
287}
288
289/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 * read the attributes of an inode
291 */
David Howells416351f2007-05-09 02:33:45 -0700292int afs_getattr(struct vfsmount *mnt, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 struct kstat *stat)
294{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 inode = dentry->d_inode;
298
299 _enter("{ ino=%lu v=%lu }", inode->i_ino, inode->i_version);
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 generic_fillattr(inode, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return 0;
David Howellsec268152007-04-26 15:49:28 -0700303}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305/*
306 * clear an AFS inode
307 */
308void afs_clear_inode(struct inode *inode)
309{
David Howells00d3b7a2007-04-26 15:57:07 -0700310 struct afs_permits *permits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 struct afs_vnode *vnode;
312
313 vnode = AFS_FS_I(inode);
314
David Howells416351f2007-05-09 02:33:45 -0700315 _enter("{%x:%u.%d} v=%u x=%u t=%u }",
David Howells260a9802007-04-26 15:59:35 -0700316 vnode->fid.vid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 vnode->fid.vnode,
David Howells260a9802007-04-26 15:59:35 -0700318 vnode->fid.unique,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 vnode->cb_version,
320 vnode->cb_expiry,
David Howells08e0e7c2007-04-26 15:55:03 -0700321 vnode->cb_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
David Howells08e0e7c2007-04-26 15:55:03 -0700323 _debug("CLEAR INODE %p", inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
David Howells08e0e7c2007-04-26 15:55:03 -0700325 ASSERTCMP(inode->i_ino, ==, vnode->fid.vnode);
326
327 afs_give_up_callback(vnode);
328
329 if (vnode->server) {
330 spin_lock(&vnode->server->fs_lock);
331 rb_erase(&vnode->server_rb, &vnode->server->fs_vnodes);
332 spin_unlock(&vnode->server->fs_lock);
333 afs_put_server(vnode->server);
334 vnode->server = NULL;
335 }
336
337 ASSERT(!vnode->cb_promised);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339#ifdef AFS_CACHING_SUPPORT
340 cachefs_relinquish_cookie(vnode->cache, 0);
341 vnode->cache = NULL;
342#endif
343
David Howells00d3b7a2007-04-26 15:57:07 -0700344 mutex_lock(&vnode->permits_lock);
345 permits = vnode->permits;
346 rcu_assign_pointer(vnode->permits, NULL);
347 mutex_unlock(&vnode->permits_lock);
348 if (permits)
349 call_rcu(&permits->rcu, afs_zap_permits);
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 _leave("");
David Howellsec268152007-04-26 15:49:28 -0700352}