2 * Copyright (c) 2002 Red Hat, Inc. All rights reserved.
4 * This software may be freely redistributed under the terms of the
5 * GNU General Public License.
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.
11 * Authors: David Woodhouse <dwmw2@cambridge.redhat.com>
12 * David Howells <dhowells@redhat.com>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/slab.h>
21 #include <linux/pagemap.h>
24 struct afs_iget_data {
26 struct afs_volume *volume; /* volume on which resides */
30 * map the AFS file status to the inode member variables
32 static int afs_inode_map_status(struct afs_vnode *vnode, struct key *key)
34 struct inode *inode = AFS_VNODE_TO_I(vnode);
36 _debug("FS: ft=%d lk=%d sz=%llu ver=%Lu mod=%hu",
39 (unsigned long long) vnode->status.size,
40 vnode->status.data_version,
43 switch (vnode->status.type) {
45 inode->i_mode = S_IFREG | vnode->status.mode;
46 inode->i_op = &afs_file_inode_operations;
47 inode->i_fop = &afs_file_operations;
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;
54 case AFS_FTYPE_SYMLINK:
55 inode->i_mode = S_IFLNK | vnode->status.mode;
56 inode->i_op = &page_symlink_inode_operations;
59 printk("kAFS: AFS vnode with undefined type\n");
63 inode->i_nlink = vnode->status.nlink;
64 inode->i_uid = vnode->status.owner;
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;
71 inode->i_version = vnode->fid.unique;
72 inode->i_mapping->a_ops = &afs_fs_aops;
74 /* check to see whether a symbolic link is really a mountpoint */
75 if (vnode->status.type == AFS_FTYPE_SYMLINK) {
76 afs_mntpt_check_symlink(vnode, key);
78 if (test_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags)) {
79 inode->i_mode = S_IFDIR | vnode->status.mode;
80 inode->i_op = &afs_mntpt_inode_operations;
81 inode->i_fop = &afs_mntpt_file_operations;
91 static int afs_iget5_test(struct inode *inode, void *opaque)
93 struct afs_iget_data *data = opaque;
95 return inode->i_ino == data->fid.vnode &&
96 inode->i_version == data->fid.unique;
100 * iget5() inode initialiser
102 static int afs_iget5_set(struct inode *inode, void *opaque)
104 struct afs_iget_data *data = opaque;
105 struct afs_vnode *vnode = AFS_FS_I(inode);
107 inode->i_ino = data->fid.vnode;
108 inode->i_version = data->fid.unique;
109 vnode->fid = data->fid;
110 vnode->volume = data->volume;
118 struct 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)
122 struct afs_iget_data data = { .fid = *fid };
123 struct afs_super_info *as;
124 struct afs_vnode *vnode;
128 _enter(",{%x:%u.%u},,", fid->vid, fid->vnode, fid->unique);
131 data.volume = as->volume;
133 inode = iget5_locked(sb, fid->vnode, afs_iget5_test, afs_iget5_set,
136 _leave(" = -ENOMEM");
137 return ERR_PTR(-ENOMEM);
140 _debug("GOT INODE %p { vl=%x vn=%x, u=%x }",
141 inode, fid->vid, fid->vnode, fid->unique);
143 vnode = AFS_FS_I(inode);
145 /* deal with an existing inode */
146 if (!(inode->i_state & I_NEW)) {
147 _leave(" = %p", inode);
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,
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);
167 /* it's an inode we just created */
168 memcpy(&vnode->status, status, sizeof(vnode->status));
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;
176 vnode->cb_expires = get_seconds();
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();
185 ret = afs_inode_map_status(vnode, key);
190 clear_bit(AFS_VNODE_UNSET, &vnode->flags);
191 inode->i_flags |= S_NOATIME;
192 unlock_new_inode(inode);
193 _leave(" = %p [CB { v=%u t=%u }]", inode, vnode->cb_version, vnode->cb_type);
198 make_bad_inode(inode);
199 unlock_new_inode(inode);
202 _leave(" = %d [bad]", ret);
207 * 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
210 void afs_zap_data(struct afs_vnode *vnode)
212 _enter("zap data {%x:%u}", vnode->fid.vid, vnode->fid.vnode);
214 /* nuke all the non-dirty pages that aren't locked, mapped or being
216 invalidate_remote_inode(&vnode->vfs_inode);
220 * validate a vnode/inode
221 * - there are several things we need to check
222 * - parent dir data changes (rm, rmdir, rename, mkdir, create, link,
224 * - parent dir metadata changed (security changes)
225 * - dentry data changed (write, truncate)
226 * - dentry metadata changed (security changes)
228 int afs_validate(struct afs_vnode *vnode, struct key *key)
232 _enter("{v={%x:%u} fl=%lx},%x",
233 vnode->fid.vid, vnode->fid.vnode, vnode->flags,
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);
248 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
251 mutex_lock(&vnode->validate_lock);
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
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);
263 _debug("new promise [fl=%lx]", vnode->flags);
266 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
267 _debug("file already deleted");
272 /* if the vnode's data version number changed then its contents are
274 if (test_and_clear_bit(AFS_VNODE_ZAP_DATA, &vnode->flags))
277 clear_bit(AFS_VNODE_MODIFIED, &vnode->flags);
278 mutex_unlock(&vnode->validate_lock);
284 mutex_unlock(&vnode->validate_lock);
285 _leave(" = %d", ret);
290 * read the attributes of an inode
292 int afs_getattr(struct vfsmount *mnt, struct dentry *dentry,
297 inode = dentry->d_inode;
299 _enter("{ ino=%lu v=%lu }", inode->i_ino, inode->i_version);
301 generic_fillattr(inode, stat);
308 void afs_clear_inode(struct inode *inode)
310 struct afs_permits *permits;
311 struct afs_vnode *vnode;
313 vnode = AFS_FS_I(inode);
315 _enter("{%x:%u.%d} v=%u x=%u t=%u }",
323 _debug("CLEAR INODE %p", inode);
325 ASSERTCMP(inode->i_ino, ==, vnode->fid.vnode);
327 afs_give_up_callback(vnode);
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;
337 ASSERT(list_empty(&vnode->writebacks));
338 ASSERT(!vnode->cb_promised);
340 #ifdef AFS_CACHING_SUPPORT
341 cachefs_relinquish_cookie(vnode->cache, 0);
345 mutex_lock(&vnode->permits_lock);
346 permits = vnode->permits;
347 rcu_assign_pointer(vnode->permits, NULL);
348 mutex_unlock(&vnode->permits_lock);
350 call_rcu(&permits->rcu, afs_zap_permits);
356 * set the attributes of an inode
358 int afs_setattr(struct dentry *dentry, struct iattr *attr)
360 struct afs_vnode *vnode = AFS_FS_I(dentry->d_inode);
364 _enter("{%x:%u},{n=%s},%x",
365 vnode->fid.vid, vnode->fid.vnode, dentry->d_name.name,
368 if (!(attr->ia_valid & (ATTR_SIZE | ATTR_MODE | ATTR_UID | ATTR_GID |
370 _leave(" = 0 [unsupported]");
374 /* flush any dirty data outstanding on a regular file */
375 if (S_ISREG(vnode->vfs_inode.i_mode)) {
376 filemap_write_and_wait(vnode->vfs_inode.i_mapping);
377 afs_writeback_all(vnode);
380 if (attr->ia_valid & ATTR_FILE) {
381 key = attr->ia_file->private_data;
383 key = afs_request_key(vnode->volume->cell);
390 ret = afs_vnode_setattr(vnode, key, attr);
391 if (!(attr->ia_valid & ATTR_FILE))
395 _leave(" = %d", ret);