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@infradead.org>
12 * David Howells <dhowells@redhat.com>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
20 #include <linux/pagemap.h>
21 #include <linux/sched.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 #ifdef CONFIG_AFS_FSCACHE
64 if (vnode
->status
.size
!= inode
->i_size
)
65 fscache_attr_changed(vnode
->cache
);
68 inode
->i_nlink
= vnode
->status
.nlink
;
69 inode
->i_uid
= vnode
->status
.owner
;
71 inode
->i_size
= vnode
->status
.size
;
72 inode
->i_ctime
.tv_sec
= vnode
->status
.mtime_server
;
73 inode
->i_ctime
.tv_nsec
= 0;
74 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
;
76 inode
->i_version
= vnode
->fid
.unique
;
77 inode
->i_mapping
->a_ops
= &afs_fs_aops
;
79 /* check to see whether a symbolic link is really a mountpoint */
80 if (vnode
->status
.type
== AFS_FTYPE_SYMLINK
) {
81 afs_mntpt_check_symlink(vnode
, key
);
83 if (test_bit(AFS_VNODE_MOUNTPOINT
, &vnode
->flags
)) {
84 inode
->i_mode
= S_IFDIR
| vnode
->status
.mode
;
85 inode
->i_op
= &afs_mntpt_inode_operations
;
86 inode
->i_fop
= &afs_mntpt_file_operations
;
96 static int afs_iget5_test(struct inode
*inode
, void *opaque
)
98 struct afs_iget_data
*data
= opaque
;
100 return inode
->i_ino
== data
->fid
.vnode
&&
101 inode
->i_version
== data
->fid
.unique
;
105 * iget5() inode initialiser
107 static int afs_iget5_set(struct inode
*inode
, void *opaque
)
109 struct afs_iget_data
*data
= opaque
;
110 struct afs_vnode
*vnode
= AFS_FS_I(inode
);
112 inode
->i_ino
= data
->fid
.vnode
;
113 inode
->i_version
= data
->fid
.unique
;
114 vnode
->fid
= data
->fid
;
115 vnode
->volume
= data
->volume
;
123 struct inode
*afs_iget(struct super_block
*sb
, struct key
*key
,
124 struct afs_fid
*fid
, struct afs_file_status
*status
,
125 struct afs_callback
*cb
)
127 struct afs_iget_data data
= { .fid
= *fid
};
128 struct afs_super_info
*as
;
129 struct afs_vnode
*vnode
;
133 _enter(",{%x:%u.%u},,", fid
->vid
, fid
->vnode
, fid
->unique
);
136 data
.volume
= as
->volume
;
138 inode
= iget5_locked(sb
, fid
->vnode
, afs_iget5_test
, afs_iget5_set
,
141 _leave(" = -ENOMEM");
142 return ERR_PTR(-ENOMEM
);
145 _debug("GOT INODE %p { vl=%x vn=%x, u=%x }",
146 inode
, fid
->vid
, fid
->vnode
, fid
->unique
);
148 vnode
= AFS_FS_I(inode
);
150 /* deal with an existing inode */
151 if (!(inode
->i_state
& I_NEW
)) {
152 _leave(" = %p", inode
);
157 /* it's a remotely extant inode */
158 set_bit(AFS_VNODE_CB_BROKEN
, &vnode
->flags
);
159 ret
= afs_vnode_fetch_status(vnode
, NULL
, key
);
163 /* it's an inode we just created */
164 memcpy(&vnode
->status
, status
, sizeof(vnode
->status
));
167 /* it's a symlink we just created (the fileserver
168 * didn't give us a callback) */
169 vnode
->cb_version
= 0;
170 vnode
->cb_expiry
= 0;
172 vnode
->cb_expires
= get_seconds();
174 vnode
->cb_version
= cb
->version
;
175 vnode
->cb_expiry
= cb
->expiry
;
176 vnode
->cb_type
= cb
->type
;
177 vnode
->cb_expires
= vnode
->cb_expiry
+ get_seconds();
181 /* set up caching before mapping the status, as map-status reads the
182 * first page of symlinks to see if they're really mountpoints */
183 inode
->i_size
= vnode
->status
.size
;
184 #ifdef CONFIG_AFS_FSCACHE
185 vnode
->cache
= fscache_acquire_cookie(vnode
->volume
->cache
,
186 &afs_vnode_cache_index_def
,
190 ret
= afs_inode_map_status(vnode
, key
);
195 clear_bit(AFS_VNODE_UNSET
, &vnode
->flags
);
196 inode
->i_flags
|= S_NOATIME
;
197 unlock_new_inode(inode
);
198 _leave(" = %p [CB { v=%u t=%u }]", inode
, vnode
->cb_version
, vnode
->cb_type
);
203 #ifdef CONFIG_AFS_FSCACHE
204 fscache_relinquish_cookie(vnode
->cache
, 0);
208 _leave(" = %d [bad]", ret
);
213 * mark the data attached to an inode as obsolete due to a write on the server
214 * - might also want to ditch all the outstanding writes and dirty pages
216 void afs_zap_data(struct afs_vnode
*vnode
)
218 _enter("{%x:%u}", vnode
->fid
.vid
, vnode
->fid
.vnode
);
220 /* nuke all the non-dirty pages that aren't locked, mapped or being
221 * written back in a regular file and completely discard the pages in a
222 * directory or symlink */
223 if (S_ISREG(vnode
->vfs_inode
.i_mode
))
224 invalidate_remote_inode(&vnode
->vfs_inode
);
226 invalidate_inode_pages2(vnode
->vfs_inode
.i_mapping
);
230 * validate a vnode/inode
231 * - there are several things we need to check
232 * - parent dir data changes (rm, rmdir, rename, mkdir, create, link,
234 * - parent dir metadata changed (security changes)
235 * - dentry data changed (write, truncate)
236 * - dentry metadata changed (security changes)
238 int afs_validate(struct afs_vnode
*vnode
, struct key
*key
)
242 _enter("{v={%x:%u} fl=%lx},%x",
243 vnode
->fid
.vid
, vnode
->fid
.vnode
, vnode
->flags
,
246 if (vnode
->cb_promised
&&
247 !test_bit(AFS_VNODE_CB_BROKEN
, &vnode
->flags
) &&
248 !test_bit(AFS_VNODE_MODIFIED
, &vnode
->flags
) &&
249 !test_bit(AFS_VNODE_ZAP_DATA
, &vnode
->flags
)) {
250 if (vnode
->cb_expires
< get_seconds() + 10) {
251 _debug("callback expired");
252 set_bit(AFS_VNODE_CB_BROKEN
, &vnode
->flags
);
258 if (test_bit(AFS_VNODE_DELETED
, &vnode
->flags
))
261 mutex_lock(&vnode
->validate_lock
);
263 /* if the promise has expired, we need to check the server again to get
264 * a new promise - note that if the (parent) directory's metadata was
265 * changed then the security may be different and we may no longer have
267 if (!vnode
->cb_promised
||
268 test_bit(AFS_VNODE_CB_BROKEN
, &vnode
->flags
)) {
269 _debug("not promised");
270 ret
= afs_vnode_fetch_status(vnode
, NULL
, key
);
273 _debug("new promise [fl=%lx]", vnode
->flags
);
276 if (test_bit(AFS_VNODE_DELETED
, &vnode
->flags
)) {
277 _debug("file already deleted");
282 /* if the vnode's data version number changed then its contents are
284 if (test_and_clear_bit(AFS_VNODE_ZAP_DATA
, &vnode
->flags
))
287 clear_bit(AFS_VNODE_MODIFIED
, &vnode
->flags
);
288 mutex_unlock(&vnode
->validate_lock
);
294 mutex_unlock(&vnode
->validate_lock
);
295 _leave(" = %d", ret
);
300 * read the attributes of an inode
302 int afs_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
,
307 inode
= dentry
->d_inode
;
309 _enter("{ ino=%lu v=%llu }", inode
->i_ino
,
310 (unsigned long long)inode
->i_version
);
312 generic_fillattr(inode
, stat
);
319 void afs_clear_inode(struct inode
*inode
)
321 struct afs_permits
*permits
;
322 struct afs_vnode
*vnode
;
324 vnode
= AFS_FS_I(inode
);
326 _enter("{%x:%u.%d} v=%u x=%u t=%u }",
334 _debug("CLEAR INODE %p", inode
);
336 ASSERTCMP(inode
->i_ino
, ==, vnode
->fid
.vnode
);
338 afs_give_up_callback(vnode
);
341 spin_lock(&vnode
->server
->fs_lock
);
342 rb_erase(&vnode
->server_rb
, &vnode
->server
->fs_vnodes
);
343 spin_unlock(&vnode
->server
->fs_lock
);
344 afs_put_server(vnode
->server
);
345 vnode
->server
= NULL
;
348 ASSERT(list_empty(&vnode
->writebacks
));
349 ASSERT(!vnode
->cb_promised
);
351 #ifdef CONFIG_AFS_FSCACHE
352 fscache_relinquish_cookie(vnode
->cache
, 0);
356 mutex_lock(&vnode
->permits_lock
);
357 permits
= vnode
->permits
;
358 rcu_assign_pointer(vnode
->permits
, NULL
);
359 mutex_unlock(&vnode
->permits_lock
);
361 call_rcu(&permits
->rcu
, afs_zap_permits
);
367 * set the attributes of an inode
369 int afs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
371 struct afs_vnode
*vnode
= AFS_FS_I(dentry
->d_inode
);
375 _enter("{%x:%u},{n=%s},%x",
376 vnode
->fid
.vid
, vnode
->fid
.vnode
, dentry
->d_name
.name
,
379 if (!(attr
->ia_valid
& (ATTR_SIZE
| ATTR_MODE
| ATTR_UID
| ATTR_GID
|
381 _leave(" = 0 [unsupported]");
385 /* flush any dirty data outstanding on a regular file */
386 if (S_ISREG(vnode
->vfs_inode
.i_mode
)) {
387 filemap_write_and_wait(vnode
->vfs_inode
.i_mapping
);
388 afs_writeback_all(vnode
);
391 if (attr
->ia_valid
& ATTR_FILE
) {
392 key
= attr
->ia_file
->private_data
;
394 key
= afs_request_key(vnode
->volume
->cell
);
401 ret
= afs_vnode_setattr(vnode
, key
, attr
);
402 if (!(attr
->ia_valid
& ATTR_FILE
))
406 _leave(" = %d", ret
);