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>
22 #include <linux/sched.h>
25 struct afs_iget_data {
27 struct afs_volume *volume; /* volume on which resides */
31 * map the AFS file status to the inode member variables
33 static int afs_inode_map_status(struct afs_vnode *vnode, struct key *key)
35 struct inode *inode = AFS_VNODE_TO_I(vnode);
37 _debug("FS: ft=%d lk=%d sz=%llu ver=%Lu mod=%hu",
40 (unsigned long long) vnode->status.size,
41 vnode->status.data_version,
44 switch (vnode->status.type) {
46 inode->i_mode = S_IFREG | vnode->status.mode;
47 inode->i_op = &afs_file_inode_operations;
48 inode->i_fop = &afs_file_operations;
51 inode->i_mode = S_IFDIR | vnode->status.mode;
52 inode->i_op = &afs_dir_inode_operations;
53 inode->i_fop = &afs_dir_file_operations;
55 case AFS_FTYPE_SYMLINK:
56 inode->i_mode = S_IFLNK | vnode->status.mode;
57 inode->i_op = &page_symlink_inode_operations;
60 printk("kAFS: AFS vnode with undefined type\n");
64 inode->i_nlink = vnode->status.nlink;
65 inode->i_uid = vnode->status.owner;
67 inode->i_size = vnode->status.size;
68 inode->i_ctime.tv_sec = vnode->status.mtime_server;
69 inode->i_ctime.tv_nsec = 0;
70 inode->i_atime = inode->i_mtime = inode->i_ctime;
72 inode->i_version = vnode->fid.unique;
73 inode->i_mapping->a_ops = &afs_fs_aops;
75 /* check to see whether a symbolic link is really a mountpoint */
76 if (vnode->status.type == AFS_FTYPE_SYMLINK) {
77 afs_mntpt_check_symlink(vnode, key);
79 if (test_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags)) {
80 inode->i_mode = S_IFDIR | vnode->status.mode;
81 inode->i_op = &afs_mntpt_inode_operations;
82 inode->i_fop = &afs_mntpt_file_operations;
92 static int afs_iget5_test(struct inode *inode, void *opaque)
94 struct afs_iget_data *data = opaque;
96 return inode->i_ino == data->fid.vnode &&
97 inode->i_version == data->fid.unique;
101 * iget5() inode initialiser
103 static int afs_iget5_set(struct inode *inode, void *opaque)
105 struct afs_iget_data *data = opaque;
106 struct afs_vnode *vnode = AFS_FS_I(inode);
108 inode->i_ino = data->fid.vnode;
109 inode->i_version = data->fid.unique;
110 vnode->fid = data->fid;
111 vnode->volume = data->volume;
119 struct inode *afs_iget(struct super_block *sb, struct key *key,
120 struct afs_fid *fid, struct afs_file_status *status,
121 struct afs_callback *cb)
123 struct afs_iget_data data = { .fid = *fid };
124 struct afs_super_info *as;
125 struct afs_vnode *vnode;
129 _enter(",{%x:%u.%u},,", fid->vid, fid->vnode, fid->unique);
132 data.volume = as->volume;
134 inode = iget5_locked(sb, fid->vnode, afs_iget5_test, afs_iget5_set,
137 _leave(" = -ENOMEM");
138 return ERR_PTR(-ENOMEM);
141 _debug("GOT INODE %p { vl=%x vn=%x, u=%x }",
142 inode, fid->vid, fid->vnode, fid->unique);
144 vnode = AFS_FS_I(inode);
146 /* deal with an existing inode */
147 if (!(inode->i_state & I_NEW)) {
148 _leave(" = %p", inode);
152 #ifdef AFS_CACHING_SUPPORT
153 /* set up caching before reading the status, as fetch-status reads the
154 * first page of symlinks to see if they're really mntpts */
155 cachefs_acquire_cookie(vnode->volume->cache,
162 /* it's a remotely extant inode */
163 set_bit(AFS_VNODE_CB_BROKEN, &vnode->flags);
164 ret = afs_vnode_fetch_status(vnode, NULL, key);
168 /* it's an inode we just created */
169 memcpy(&vnode->status, status, sizeof(vnode->status));
172 /* it's a symlink we just created (the fileserver
173 * didn't give us a callback) */
174 vnode->cb_version = 0;
175 vnode->cb_expiry = 0;
177 vnode->cb_expires = get_seconds();
179 vnode->cb_version = cb->version;
180 vnode->cb_expiry = cb->expiry;
181 vnode->cb_type = cb->type;
182 vnode->cb_expires = vnode->cb_expiry + get_seconds();
186 ret = afs_inode_map_status(vnode, key);
191 clear_bit(AFS_VNODE_UNSET, &vnode->flags);
192 inode->i_flags |= S_NOATIME;
193 unlock_new_inode(inode);
194 _leave(" = %p [CB { v=%u t=%u }]", inode, vnode->cb_version, vnode->cb_type);
199 make_bad_inode(inode);
200 unlock_new_inode(inode);
203 _leave(" = %d [bad]", ret);
208 * mark the data attached to an inode as obsolete due to a write on the server
209 * - might also want to ditch all the outstanding writes and dirty pages
211 void afs_zap_data(struct afs_vnode *vnode)
213 _enter("{%x:%u}", vnode->fid.vid, vnode->fid.vnode);
215 /* nuke all the non-dirty pages that aren't locked, mapped or being
216 * written back in a regular file and completely discard the pages in a
217 * directory or symlink */
218 if (S_ISREG(vnode->vfs_inode.i_mode))
219 invalidate_remote_inode(&vnode->vfs_inode);
221 invalidate_inode_pages2(vnode->vfs_inode.i_mapping);
225 * validate a vnode/inode
226 * - there are several things we need to check
227 * - parent dir data changes (rm, rmdir, rename, mkdir, create, link,
229 * - parent dir metadata changed (security changes)
230 * - dentry data changed (write, truncate)
231 * - dentry metadata changed (security changes)
233 int afs_validate(struct afs_vnode *vnode, struct key *key)
237 _enter("{v={%x:%u} fl=%lx},%x",
238 vnode->fid.vid, vnode->fid.vnode, vnode->flags,
241 if (vnode->cb_promised &&
242 !test_bit(AFS_VNODE_CB_BROKEN, &vnode->flags) &&
243 !test_bit(AFS_VNODE_MODIFIED, &vnode->flags) &&
244 !test_bit(AFS_VNODE_ZAP_DATA, &vnode->flags)) {
245 if (vnode->cb_expires < get_seconds() + 10) {
246 _debug("callback expired");
247 set_bit(AFS_VNODE_CB_BROKEN, &vnode->flags);
253 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
256 mutex_lock(&vnode->validate_lock);
258 /* if the promise has expired, we need to check the server again to get
259 * a new promise - note that if the (parent) directory's metadata was
260 * changed then the security may be different and we may no longer have
262 if (!vnode->cb_promised ||
263 test_bit(AFS_VNODE_CB_BROKEN, &vnode->flags)) {
264 _debug("not promised");
265 ret = afs_vnode_fetch_status(vnode, NULL, key);
268 _debug("new promise [fl=%lx]", vnode->flags);
271 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
272 _debug("file already deleted");
277 /* if the vnode's data version number changed then its contents are
279 if (test_and_clear_bit(AFS_VNODE_ZAP_DATA, &vnode->flags))
282 clear_bit(AFS_VNODE_MODIFIED, &vnode->flags);
283 mutex_unlock(&vnode->validate_lock);
289 mutex_unlock(&vnode->validate_lock);
290 _leave(" = %d", ret);
295 * read the attributes of an inode
297 int afs_getattr(struct vfsmount *mnt, struct dentry *dentry,
302 inode = dentry->d_inode;
304 _enter("{ ino=%lu v=%llu }", inode->i_ino,
305 (unsigned long long)inode->i_version);
307 generic_fillattr(inode, stat);
314 void afs_clear_inode(struct inode *inode)
316 struct afs_permits *permits;
317 struct afs_vnode *vnode;
319 vnode = AFS_FS_I(inode);
321 _enter("{%x:%u.%d} v=%u x=%u t=%u }",
329 _debug("CLEAR INODE %p", inode);
331 ASSERTCMP(inode->i_ino, ==, vnode->fid.vnode);
333 afs_give_up_callback(vnode);
336 spin_lock(&vnode->server->fs_lock);
337 rb_erase(&vnode->server_rb, &vnode->server->fs_vnodes);
338 spin_unlock(&vnode->server->fs_lock);
339 afs_put_server(vnode->server);
340 vnode->server = NULL;
343 ASSERT(list_empty(&vnode->writebacks));
344 ASSERT(!vnode->cb_promised);
346 #ifdef AFS_CACHING_SUPPORT
347 cachefs_relinquish_cookie(vnode->cache, 0);
351 mutex_lock(&vnode->permits_lock);
352 permits = vnode->permits;
353 rcu_assign_pointer(vnode->permits, NULL);
354 mutex_unlock(&vnode->permits_lock);
356 call_rcu(&permits->rcu, afs_zap_permits);
362 * set the attributes of an inode
364 int afs_setattr(struct dentry *dentry, struct iattr *attr)
366 struct afs_vnode *vnode = AFS_FS_I(dentry->d_inode);
370 _enter("{%x:%u},{n=%s},%x",
371 vnode->fid.vid, vnode->fid.vnode, dentry->d_name.name,
374 if (!(attr->ia_valid & (ATTR_SIZE | ATTR_MODE | ATTR_UID | ATTR_GID |
376 _leave(" = 0 [unsupported]");
380 /* flush any dirty data outstanding on a regular file */
381 if (S_ISREG(vnode->vfs_inode.i_mode)) {
382 filemap_write_and_wait(vnode->vfs_inode.i_mapping);
383 afs_writeback_all(vnode);
386 if (attr->ia_valid & ATTR_FILE) {
387 key = attr->ia_file->private_data;
389 key = afs_request_key(vnode->volume->cell);
396 ret = afs_vnode_setattr(vnode, key, attr);
397 if (!(attr->ia_valid & ATTR_FILE))
401 _leave(" = %d", ret);