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>
22 #include <linux/mount.h>
23 #include <linux/namei.h>
26 struct afs_iget_data
{
28 struct afs_volume
*volume
; /* volume on which resides */
31 static const struct inode_operations afs_symlink_inode_operations
= {
32 .get_link
= page_get_link
,
33 .listxattr
= afs_listxattr
,
37 * map the AFS file status to the inode member variables
39 static int afs_inode_map_status(struct afs_vnode
*vnode
, struct key
*key
)
41 struct inode
*inode
= AFS_VNODE_TO_I(vnode
);
43 _debug("FS: ft=%d lk=%d sz=%llu ver=%Lu mod=%hu",
46 (unsigned long long) vnode
->status
.size
,
47 vnode
->status
.data_version
,
50 switch (vnode
->status
.type
) {
52 inode
->i_mode
= S_IFREG
| vnode
->status
.mode
;
53 inode
->i_op
= &afs_file_inode_operations
;
54 inode
->i_fop
= &afs_file_operations
;
57 inode
->i_mode
= S_IFDIR
| vnode
->status
.mode
;
58 inode
->i_op
= &afs_dir_inode_operations
;
59 inode
->i_fop
= &afs_dir_file_operations
;
61 case AFS_FTYPE_SYMLINK
:
62 /* Symlinks with a mode of 0644 are actually mountpoints. */
63 if ((vnode
->status
.mode
& 0777) == 0644) {
64 inode
->i_flags
|= S_AUTOMOUNT
;
66 spin_lock(&vnode
->lock
);
67 set_bit(AFS_VNODE_MOUNTPOINT
, &vnode
->flags
);
68 spin_unlock(&vnode
->lock
);
70 inode
->i_mode
= S_IFDIR
| 0555;
71 inode
->i_op
= &afs_mntpt_inode_operations
;
72 inode
->i_fop
= &afs_mntpt_file_operations
;
74 inode
->i_mode
= S_IFLNK
| vnode
->status
.mode
;
75 inode
->i_op
= &afs_symlink_inode_operations
;
77 inode_nohighmem(inode
);
80 printk("kAFS: AFS vnode with undefined type\n");
84 #ifdef CONFIG_AFS_FSCACHE
85 if (vnode
->status
.size
!= inode
->i_size
)
86 fscache_attr_changed(vnode
->cache
);
89 set_nlink(inode
, vnode
->status
.nlink
);
90 inode
->i_uid
= vnode
->status
.owner
;
91 inode
->i_gid
= vnode
->status
.group
;
92 inode
->i_size
= vnode
->status
.size
;
93 inode
->i_ctime
.tv_sec
= vnode
->status
.mtime_client
;
94 inode
->i_ctime
.tv_nsec
= 0;
95 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
;
97 inode
->i_generation
= vnode
->fid
.unique
;
98 inode
->i_version
= vnode
->status
.data_version
;
99 inode
->i_mapping
->a_ops
= &afs_fs_aops
;
106 static int afs_iget5_test(struct inode
*inode
, void *opaque
)
108 struct afs_iget_data
*data
= opaque
;
110 return inode
->i_ino
== data
->fid
.vnode
&&
111 inode
->i_generation
== data
->fid
.unique
;
115 * iget5() comparator for inode created by autocell operations
117 * These pseudo inodes don't match anything.
119 static int afs_iget5_autocell_test(struct inode
*inode
, void *opaque
)
125 * iget5() inode initialiser
127 static int afs_iget5_set(struct inode
*inode
, void *opaque
)
129 struct afs_iget_data
*data
= opaque
;
130 struct afs_vnode
*vnode
= AFS_FS_I(inode
);
132 inode
->i_ino
= data
->fid
.vnode
;
133 inode
->i_generation
= data
->fid
.unique
;
134 vnode
->fid
= data
->fid
;
135 vnode
->volume
= data
->volume
;
141 * inode retrieval for autocell
143 struct inode
*afs_iget_autocell(struct inode
*dir
, const char *dev_name
,
144 int namesz
, struct key
*key
)
146 struct afs_iget_data data
;
147 struct afs_super_info
*as
;
148 struct afs_vnode
*vnode
;
149 struct super_block
*sb
;
151 static atomic_t afs_autocell_ino
;
153 _enter("{%x:%u},%*.*s,",
154 AFS_FS_I(dir
)->fid
.vid
, AFS_FS_I(dir
)->fid
.vnode
,
155 namesz
, namesz
, dev_name
?: "");
159 data
.volume
= as
->volume
;
160 data
.fid
.vid
= as
->volume
->vid
;
164 inode
= iget5_locked(sb
, atomic_inc_return(&afs_autocell_ino
),
165 afs_iget5_autocell_test
, afs_iget5_set
,
168 _leave(" = -ENOMEM");
169 return ERR_PTR(-ENOMEM
);
172 _debug("GOT INODE %p { ino=%lu, vl=%x, vn=%x, u=%x }",
173 inode
, inode
->i_ino
, data
.fid
.vid
, data
.fid
.vnode
,
176 vnode
= AFS_FS_I(inode
);
178 /* there shouldn't be an existing inode */
179 BUG_ON(!(inode
->i_state
& I_NEW
));
182 inode
->i_mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
183 inode
->i_op
= &afs_autocell_inode_operations
;
185 inode
->i_uid
= GLOBAL_ROOT_UID
;
186 inode
->i_gid
= GLOBAL_ROOT_GID
;
187 inode
->i_ctime
.tv_sec
= get_seconds();
188 inode
->i_ctime
.tv_nsec
= 0;
189 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
;
191 inode
->i_version
= 0;
192 inode
->i_generation
= 0;
194 set_bit(AFS_VNODE_PSEUDODIR
, &vnode
->flags
);
195 set_bit(AFS_VNODE_MOUNTPOINT
, &vnode
->flags
);
196 inode
->i_flags
|= S_AUTOMOUNT
| S_NOATIME
;
197 unlock_new_inode(inode
);
198 _leave(" = %p", inode
);
205 struct inode
*afs_iget(struct super_block
*sb
, struct key
*key
,
206 struct afs_fid
*fid
, struct afs_file_status
*status
,
207 struct afs_callback
*cb
)
209 struct afs_iget_data data
= { .fid
= *fid
};
210 struct afs_super_info
*as
;
211 struct afs_vnode
*vnode
;
215 _enter(",{%x:%u.%u},,", fid
->vid
, fid
->vnode
, fid
->unique
);
218 data
.volume
= as
->volume
;
220 inode
= iget5_locked(sb
, fid
->vnode
, afs_iget5_test
, afs_iget5_set
,
223 _leave(" = -ENOMEM");
224 return ERR_PTR(-ENOMEM
);
227 _debug("GOT INODE %p { vl=%x vn=%x, u=%x }",
228 inode
, fid
->vid
, fid
->vnode
, fid
->unique
);
230 vnode
= AFS_FS_I(inode
);
232 /* deal with an existing inode */
233 if (!(inode
->i_state
& I_NEW
)) {
234 _leave(" = %p", inode
);
239 /* it's a remotely extant inode */
240 set_bit(AFS_VNODE_CB_BROKEN
, &vnode
->flags
);
241 ret
= afs_vnode_fetch_status(vnode
, NULL
, key
);
245 /* it's an inode we just created */
246 memcpy(&vnode
->status
, status
, sizeof(vnode
->status
));
249 /* it's a symlink we just created (the fileserver
250 * didn't give us a callback) */
251 vnode
->cb_version
= 0;
252 vnode
->cb_expiry
= 0;
254 vnode
->cb_expires
= ktime_get_real_seconds();
256 vnode
->cb_version
= cb
->version
;
257 vnode
->cb_expiry
= cb
->expiry
;
258 vnode
->cb_type
= cb
->type
;
259 vnode
->cb_expires
= vnode
->cb_expiry
+
260 ktime_get_real_seconds();
264 /* set up caching before mapping the status, as map-status reads the
265 * first page of symlinks to see if they're really mountpoints */
266 inode
->i_size
= vnode
->status
.size
;
267 #ifdef CONFIG_AFS_FSCACHE
268 vnode
->cache
= fscache_acquire_cookie(vnode
->volume
->cache
,
269 &afs_vnode_cache_index_def
,
273 ret
= afs_inode_map_status(vnode
, key
);
278 clear_bit(AFS_VNODE_UNSET
, &vnode
->flags
);
279 inode
->i_flags
|= S_NOATIME
;
280 unlock_new_inode(inode
);
281 _leave(" = %p [CB { v=%u t=%u }]", inode
, vnode
->cb_version
, vnode
->cb_type
);
286 #ifdef CONFIG_AFS_FSCACHE
287 fscache_relinquish_cookie(vnode
->cache
, 0);
291 _leave(" = %d [bad]", ret
);
296 * mark the data attached to an inode as obsolete due to a write on the server
297 * - might also want to ditch all the outstanding writes and dirty pages
299 void afs_zap_data(struct afs_vnode
*vnode
)
301 _enter("{%x:%u}", vnode
->fid
.vid
, vnode
->fid
.vnode
);
303 /* nuke all the non-dirty pages that aren't locked, mapped or being
304 * written back in a regular file and completely discard the pages in a
305 * directory or symlink */
306 if (S_ISREG(vnode
->vfs_inode
.i_mode
))
307 invalidate_remote_inode(&vnode
->vfs_inode
);
309 invalidate_inode_pages2(vnode
->vfs_inode
.i_mapping
);
313 * validate a vnode/inode
314 * - there are several things we need to check
315 * - parent dir data changes (rm, rmdir, rename, mkdir, create, link,
317 * - parent dir metadata changed (security changes)
318 * - dentry data changed (write, truncate)
319 * - dentry metadata changed (security changes)
321 int afs_validate(struct afs_vnode
*vnode
, struct key
*key
)
325 _enter("{v={%x:%u} fl=%lx},%x",
326 vnode
->fid
.vid
, vnode
->fid
.vnode
, vnode
->flags
,
329 if (vnode
->cb_promised
&&
330 !test_bit(AFS_VNODE_CB_BROKEN
, &vnode
->flags
) &&
331 !test_bit(AFS_VNODE_MODIFIED
, &vnode
->flags
) &&
332 !test_bit(AFS_VNODE_ZAP_DATA
, &vnode
->flags
)) {
333 if (vnode
->cb_expires
< ktime_get_real_seconds() + 10) {
334 _debug("callback expired");
335 set_bit(AFS_VNODE_CB_BROKEN
, &vnode
->flags
);
341 if (test_bit(AFS_VNODE_DELETED
, &vnode
->flags
))
344 mutex_lock(&vnode
->validate_lock
);
346 /* if the promise has expired, we need to check the server again to get
347 * a new promise - note that if the (parent) directory's metadata was
348 * changed then the security may be different and we may no longer have
350 if (!vnode
->cb_promised
||
351 test_bit(AFS_VNODE_CB_BROKEN
, &vnode
->flags
)) {
352 _debug("not promised");
353 ret
= afs_vnode_fetch_status(vnode
, NULL
, key
);
356 _debug("new promise [fl=%lx]", vnode
->flags
);
359 if (test_bit(AFS_VNODE_DELETED
, &vnode
->flags
)) {
360 _debug("file already deleted");
365 /* if the vnode's data version number changed then its contents are
367 if (test_and_clear_bit(AFS_VNODE_ZAP_DATA
, &vnode
->flags
))
370 clear_bit(AFS_VNODE_MODIFIED
, &vnode
->flags
);
371 mutex_unlock(&vnode
->validate_lock
);
377 mutex_unlock(&vnode
->validate_lock
);
378 _leave(" = %d", ret
);
383 * read the attributes of an inode
385 int afs_getattr(const struct path
*path
, struct kstat
*stat
,
386 u32 request_mask
, unsigned int query_flags
)
388 struct inode
*inode
= d_inode(path
->dentry
);
390 _enter("{ ino=%lu v=%u }", inode
->i_ino
, inode
->i_generation
);
392 generic_fillattr(inode
, stat
);
397 * discard an AFS inode
399 int afs_drop_inode(struct inode
*inode
)
403 if (test_bit(AFS_VNODE_PSEUDODIR
, &AFS_FS_I(inode
)->flags
))
404 return generic_delete_inode(inode
);
406 return generic_drop_inode(inode
);
412 void afs_evict_inode(struct inode
*inode
)
414 struct afs_permits
*permits
;
415 struct afs_vnode
*vnode
;
417 vnode
= AFS_FS_I(inode
);
419 _enter("{%x:%u.%d} v=%u x=%u t=%u }",
427 _debug("CLEAR INODE %p", inode
);
429 ASSERTCMP(inode
->i_ino
, ==, vnode
->fid
.vnode
);
431 truncate_inode_pages_final(&inode
->i_data
);
434 afs_give_up_callback(vnode
);
437 spin_lock(&vnode
->server
->fs_lock
);
438 rb_erase(&vnode
->server_rb
, &vnode
->server
->fs_vnodes
);
439 spin_unlock(&vnode
->server
->fs_lock
);
440 afs_put_server(vnode
->server
);
441 vnode
->server
= NULL
;
444 ASSERT(list_empty(&vnode
->writebacks
));
445 ASSERT(!vnode
->cb_promised
);
447 #ifdef CONFIG_AFS_FSCACHE
448 fscache_relinquish_cookie(vnode
->cache
, 0);
452 mutex_lock(&vnode
->permits_lock
);
453 permits
= vnode
->permits
;
454 RCU_INIT_POINTER(vnode
->permits
, NULL
);
455 mutex_unlock(&vnode
->permits_lock
);
457 call_rcu(&permits
->rcu
, afs_zap_permits
);
463 * set the attributes of an inode
465 int afs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
467 struct afs_vnode
*vnode
= AFS_FS_I(d_inode(dentry
));
471 _enter("{%x:%u},{n=%pd},%x",
472 vnode
->fid
.vid
, vnode
->fid
.vnode
, dentry
,
475 if (!(attr
->ia_valid
& (ATTR_SIZE
| ATTR_MODE
| ATTR_UID
| ATTR_GID
|
477 _leave(" = 0 [unsupported]");
481 /* flush any dirty data outstanding on a regular file */
482 if (S_ISREG(vnode
->vfs_inode
.i_mode
)) {
483 filemap_write_and_wait(vnode
->vfs_inode
.i_mapping
);
484 afs_writeback_all(vnode
);
487 if (attr
->ia_valid
& ATTR_FILE
) {
488 key
= attr
->ia_file
->private_data
;
490 key
= afs_request_key(vnode
->volume
->cell
);
497 ret
= afs_vnode_setattr(vnode
, key
, attr
);
498 if (!(attr
->ia_valid
& ATTR_FILE
))
502 _leave(" = %d", ret
);