2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
11 * VFS operations for Linux
13 * super_block operations should return negated errno to Linux.
15 #include <afsconfig.h>
16 #include "afs/param.h"
19 #define __NO_VERSION__ /* don't define kernel_version in module.h */
20 #include <linux/module.h> /* early to avoid printf->printk mapping */
21 #include "afs/sysincludes.h"
22 #include "afsincludes.h"
23 #include "afs/afs_stats.h"
25 #include "osi_compat.h"
27 struct vcache *afs_globalVp = 0;
28 struct vfs *afs_globalVFS = 0;
29 struct vfsmount *afs_cacheMnt;
30 int afs_was_mounted = 0; /* Used to force reload if mount/unmount/mount */
32 extern struct super_operations afs_sops;
33 #if !defined(AFS_NONFSTRANS)
34 extern struct export_operations afs_export_ops;
36 extern afs_rwlock_t afs_xvcache;
37 extern struct afs_q VLRU;
39 extern struct dentry_operations afs_dentry_operations;
41 /* Forward declarations */
42 static void iattr2vattr(struct vattr *vattrp, struct iattr *iattrp);
43 static int afs_root(struct super_block *afsp);
44 int afs_fill_super(struct super_block *sb, void *data, int silent);
48 * afs_mount (2.6.37+) and afs_get_sb (2.6.36-) are the entry
49 * points from the vfs when mounting afs. The super block
50 * structure is setup in the afs_fill_super callback function.
53 #if defined(STRUCT_FILE_SYSTEM_TYPE_HAS_MOUNT)
54 static struct dentry *
55 afs_mount(struct file_system_type *fs_type, int flags,
56 const char *dev_name, void *data) {
57 return mount_nodev(fs_type, flags, data, afs_fill_super);
59 #elif defined(GET_SB_HAS_STRUCT_VFSMOUNT)
61 afs_get_sb(struct file_system_type *fs_type, int flags,
62 const char *dev_name, void *data, struct vfsmount *mnt) {
63 return get_sb_nodev(fs_type, flags, data, afs_fill_super, mnt);
66 static struct super_block *
67 afs_get_sb(struct file_system_type *fs_type, int flags,
68 const char *dev_name, void *data) {
69 return get_sb_nodev(fs_type, flags, data, afs_fill_super);
73 struct file_system_type afs_fs_type = {
76 #if defined(STRUCT_FILE_SYSTEM_TYPE_HAS_MOUNT)
81 .kill_sb = kill_anon_super,
82 .fs_flags = FS_BINARY_MOUNTDATA,
85 struct backing_dev_info *afs_backing_dev_info;
88 afs_fill_super(struct super_block *sb, void *data, int silent)
93 if (afs_was_mounted) {
95 ("You must reload the AFS kernel extensions before remounting AFS.\n");
101 /* Set basics of super_block */
102 __module_get(THIS_MODULE);
105 sb->s_flags |= MS_NOATIME;
106 sb->s_blocksize = 1024;
107 sb->s_blocksize_bits = 10;
108 sb->s_magic = AFS_VFSMAGIC;
109 sb->s_op = &afs_sops; /* Super block (vfs) ops */
111 #if defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
112 sb->s_d_op = &afs_dentry_operations;
115 /* used for inodes backing_dev_info field, also */
116 afs_backing_dev_info = osi_Alloc(sizeof(struct backing_dev_info));
117 #if defined(HAVE_LINUX_BDI_INIT)
118 bdi_init(afs_backing_dev_info);
120 #if defined(STRUCT_BACKING_DEV_INFO_HAS_NAME)
121 afs_backing_dev_info->name = "openafs";
123 afs_backing_dev_info->ra_pages = 32;
124 #if defined (STRUCT_SUPER_BLOCK_HAS_S_BDI)
125 sb->s_bdi = afs_backing_dev_info;
126 /* The name specified here will appear in the flushing thread name - flush-afs */
127 bdi_register(afs_backing_dev_info, NULL, "afs");
129 #if !defined(AFS_NONFSTRANS)
130 sb->s_export_op = &afs_export_ops;
132 #if defined(MAX_NON_LFS)
133 #ifdef AFS_64BIT_CLIENT
134 #if !defined(MAX_LFS_FILESIZE)
135 #if BITS_PER_LONG==32
136 #define MAX_LFS_FILESIZE (((u64)PAGE_CACHE_SIZE << (BITS_PER_LONG-1))-1)
137 #elif BITS_PER_LONG==64
138 #define MAX_LFS_FILESIZE 0x7fffffffffffffff
141 sb->s_maxbytes = MAX_LFS_FILESIZE;
143 sb->s_maxbytes = MAX_NON_LFS;
148 afs_globalVFS = NULL;
149 osi_linux_free_inode_pages();
150 module_put(THIS_MODULE);
154 return code ? -EINVAL : 0;
158 /* afs_root - stat the root of the file system. AFS global held on entry. */
160 afs_root(struct super_block *afsp)
163 struct vrequest treq;
164 struct vcache *tvp = 0;
166 AFS_STATCNT(afs_root);
167 if (afs_globalVp && (afs_globalVp->f.states & CStatd)) {
170 cred_t *credp = crref();
173 afs_PutVCache(afs_globalVp);
177 if (!(code = afs_InitReq(&treq, credp)) && !(code = afs_CheckInit())) {
178 tvp = afs_GetVCache(&afs_rootFid, &treq, NULL, NULL);
180 struct inode *ip = AFSTOV(tvp);
183 afs_getattr(tvp, &vattr, credp);
184 afs_fill_inode(ip, &vattr);
186 /* setup super_block and mount point inode. */
188 afsp->s_root = d_alloc_root(ip);
189 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
190 afsp->s_root->d_op = &afs_dentry_operations;
198 afs_Trace2(afs_iclSetp, CM_TRACE_VFSROOT, ICL_TYPE_POINTER, afs_globalVp,
199 ICL_TYPE_INT32, code);
203 /* super_operations */
206 * Linux version of setattr call. What to change is in the iattr struct.
207 * We need to set bits in both the Linux inode as well as the vcache.
210 afs_notify_change(struct dentry *dp, struct iattr *iattrp)
213 cred_t *credp = crref();
214 struct inode *ip = dp->d_inode;
218 iattr2vattr(&vattr, iattrp); /* Convert for AFS vnodeops call. */
221 code = afs_setattr(VTOAFS(ip), &vattr, credp);
223 afs_getattr(VTOAFS(ip), &vattr, credp);
224 vattr2inode(ip, &vattr);
232 #if defined(STRUCT_SUPER_OPERATIONS_HAS_ALLOC_INODE)
233 static afs_kmem_cache_t *afs_inode_cachep;
235 static struct inode *
236 afs_alloc_inode(struct super_block *sb)
240 vcp = (struct vcache *) kmem_cache_alloc(afs_inode_cachep, KALLOC_TYPE);
248 afs_destroy_inode(struct inode *inode)
250 kmem_cache_free(afs_inode_cachep, inode);
254 init_once(void * foo)
256 struct vcache *vcp = (struct vcache *) foo;
258 inode_init_once(AFSTOV(vcp));
262 afs_init_inodecache(void)
264 #if defined(KMEM_CACHE_TAKES_DTOR)
265 afs_inode_cachep = kmem_cache_create("afs_inode_cache",
266 sizeof(struct vcache), 0,
267 SLAB_HWCACHE_ALIGN | SLAB_RECLAIM_ACCOUNT, init_once_func, NULL);
269 afs_inode_cachep = kmem_cache_create("afs_inode_cache",
270 sizeof(struct vcache), 0,
271 SLAB_HWCACHE_ALIGN | SLAB_RECLAIM_ACCOUNT, init_once_func);
273 if (afs_inode_cachep == NULL)
279 afs_destroy_inodecache(void)
281 if (afs_inode_cachep)
282 (void) kmem_cache_destroy(afs_inode_cachep);
286 afs_init_inodecache(void)
292 afs_destroy_inodecache(void)
298 #if defined(STRUCT_SUPER_OPERATIONS_HAS_EVICT_INODE)
300 afs_evict_inode(struct inode *ip)
302 struct vcache *vcp = VTOAFS(ip);
304 if (vcp->vlruq.prev || vcp->vlruq.next)
305 osi_Panic("inode freed while on LRU");
307 osi_Panic("inode freed while still hashed");
309 truncate_inode_pages(&ip->i_data, 0);
312 #if !defined(STRUCT_SUPER_OPERATIONS_HAS_ALLOC_INODE)
313 afs_osi_Free(ip->u.generic_ip, sizeof(struct vcache));
318 afs_clear_inode(struct inode *ip)
320 struct vcache *vcp = VTOAFS(ip);
322 if (vcp->vlruq.prev || vcp->vlruq.next)
323 osi_Panic("inode freed while on LRU");
325 osi_Panic("inode freed while still hashed");
327 #if !defined(STRUCT_SUPER_OPERATIONS_HAS_ALLOC_INODE)
328 afs_osi_Free(ip->u.generic_ip, sizeof(struct vcache));
334 * Called from unmount to release super_block. */
336 afs_put_super(struct super_block *sbp)
339 AFS_STATCNT(afs_unmount);
344 osi_linux_free_inode_pages(); /* invalidate and release remaining AFS inodes. */
346 mntput(afs_cacheMnt);
348 osi_linux_verify_alloced_memory();
349 #if defined(HAVE_LINUX_BDI_INIT)
350 bdi_destroy(afs_backing_dev_info);
352 osi_Free(afs_backing_dev_info, sizeof(struct backing_dev_info));
356 module_put(THIS_MODULE);
361 * statp is in user space, so we need to cobble together a statfs, then
365 #if defined(STATFS_TAKES_DENTRY)
366 afs_statfs(struct dentry *dentry, struct kstatfs *statp)
368 afs_statfs(struct super_block *sbp, struct kstatfs *statp)
371 memset(statp, 0, sizeof(*statp));
373 AFS_STATCNT(afs_statfs);
375 /* hardcode in case that which is giveth is taken away */
376 statp->f_type = 0x5346414F;
377 #if defined(STATFS_TAKES_DENTRY)
378 statp->f_bsize = dentry->d_sb->s_blocksize;
380 statp->f_bsize = sbp->s_blocksize;
382 statp->f_blocks = statp->f_bfree = statp->f_bavail = statp->f_files =
383 statp->f_ffree = 9000000;
384 statp->f_fsid.val[0] = AFS_VFSMAGIC;
385 statp->f_fsid.val[1] = AFS_VFSFSID;
386 statp->f_namelen = 256;
391 struct super_operations afs_sops = {
392 #if defined(STRUCT_SUPER_OPERATIONS_HAS_ALLOC_INODE)
393 .alloc_inode = afs_alloc_inode,
394 .destroy_inode = afs_destroy_inode,
396 #if defined(STRUCT_SUPER_OPERATIONS_HAS_EVICT_INODE)
397 .evict_inode = afs_evict_inode,
399 .clear_inode = afs_clear_inode,
401 .put_super = afs_put_super,
402 .statfs = afs_statfs,
405 /************** Support routines ************************/
408 * Set iattr data into vattr. Assume vattr cleared before call.
411 iattr2vattr(struct vattr *vattrp, struct iattr *iattrp)
413 vattrp->va_mask = iattrp->ia_valid;
414 if (iattrp->ia_valid & ATTR_MODE)
415 vattrp->va_mode = iattrp->ia_mode;
416 if (iattrp->ia_valid & ATTR_UID)
417 vattrp->va_uid = iattrp->ia_uid;
418 if (iattrp->ia_valid & ATTR_GID)
419 vattrp->va_gid = iattrp->ia_gid;
420 if (iattrp->ia_valid & ATTR_SIZE)
421 vattrp->va_size = iattrp->ia_size;
422 if (iattrp->ia_valid & ATTR_ATIME) {
423 vattrp->va_atime.tv_sec = iattrp->ia_atime.tv_sec;
424 vattrp->va_atime.tv_usec = 0;
426 if (iattrp->ia_valid & ATTR_MTIME) {
427 vattrp->va_mtime.tv_sec = iattrp->ia_mtime.tv_sec;
428 vattrp->va_mtime.tv_usec = 0;
430 if (iattrp->ia_valid & ATTR_CTIME) {
431 vattrp->va_ctime.tv_sec = iattrp->ia_ctime.tv_sec;
432 vattrp->va_ctime.tv_usec = 0;
437 * Rewrite the inode cache from the attr. Assumes all vattr fields are valid.
440 vattr2inode(struct inode *ip, struct vattr *vp)
442 ip->i_ino = vp->va_nodeid;
443 ip->i_nlink = vp->va_nlink;
444 ip->i_blocks = vp->va_blocks;
445 #ifdef STRUCT_INODE_HAS_I_BLKBITS
446 ip->i_blkbits = AFS_BLKBITS;
448 #ifdef STRUCT_INODE_HAS_I_BLKSIZE
449 ip->i_blksize = vp->va_blocksize;
451 ip->i_rdev = vp->va_rdev;
452 ip->i_mode = vp->va_mode;
453 ip->i_uid = vp->va_uid;
454 ip->i_gid = vp->va_gid;
455 i_size_write(ip, vp->va_size);
456 ip->i_atime.tv_sec = vp->va_atime.tv_sec;
457 ip->i_atime.tv_nsec = 0;
458 ip->i_mtime.tv_sec = vp->va_mtime.tv_sec;
459 /* Set the mtime nanoseconds to the sysname generation number.
460 * This convinces NFS clients that all directories have changed
461 * any time the sysname list changes.
463 ip->i_mtime.tv_nsec = afs_sysnamegen;
464 ip->i_ctime.tv_sec = vp->va_ctime.tv_sec;
465 ip->i_ctime.tv_nsec = 0;
468 /* osi_linux_free_inode_pages
470 * Free all vnodes remaining in the afs hash. Must be done before
471 * shutting down afs and freeing all memory.
474 osi_linux_free_inode_pages(void)
477 struct vcache *tvc, *nvc;
478 extern struct vcache *afs_vhashT[VCSIZE];
480 for (i = 0; i < VCSIZE; i++) {
481 for (tvc = afs_vhashT[i]; tvc; ) {
485 if (afs_FlushVCache(tvc, &slept)) /* slept always 0 for linux? */
486 printf("Failed to invalidate all pages on inode 0x%p\n", tvc);