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"
24 #include "h/smp_lock.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 struct super_block *afs_read_super(struct super_block *sb, void *data, int silent);
45 int afs_fill_super(struct super_block *sb, void *data, int silent);
48 * VFS entry for Linux - installed in init_module
49 * Linux mounts file systems by:
50 * 1) register_filesystem(&afs_file_system) - done in init_module
51 * 2) Mount call comes to us via do_mount -> read_super -> afs_read_super.
52 * We are expected to setup the super_block. See afs_read_super.
57 * read the "super block" for AFS - roughly eguivalent to struct vfs.
58 * dev, covered, s_rd_only, s_dirt, and s_type will be set by read_super.
60 #ifdef GET_SB_HAS_STRUCT_VFSMOUNT
62 afs_get_sb(struct file_system_type *fs_type, int flags,
63 const char *dev_name, void *data, struct vfsmount *mnt) {
64 return get_sb_nodev(fs_type, flags, data, afs_fill_super, mnt);
67 static struct superblock *
68 afs_get_sb(struct file_system_type *fs_type, int flags,
69 const char *dev_name, void *data)
70 return get_sb_nodev(fs_type, flags, data, afs_fill_super);
74 struct file_system_type afs_fs_type = {
78 .kill_sb = kill_anon_super,
79 .fs_flags = FS_BINARY_MOUNTDATA,
82 struct backing_dev_info afs_backing_dev_info = {
87 afs_fill_super(struct super_block *sb, void *data, int silent)
92 if (afs_was_mounted) {
94 ("You must reload the AFS kernel extensions before remounting AFS.\n");
100 /* Set basics of super_block */
101 __module_get(THIS_MODULE);
104 sb->s_blocksize = 1024;
105 sb->s_blocksize_bits = 10;
106 sb->s_magic = AFS_VFSMAGIC;
107 sb->s_op = &afs_sops; /* Super block (vfs) ops */
108 #if defined(HAVE_BDI_INIT)
109 bdi_init(&afs_backing_dev_info);
111 #if !defined(AFS_NONFSTRANS)
112 sb->s_export_op = &afs_export_ops;
114 #if defined(MAX_NON_LFS)
115 #ifdef AFS_64BIT_CLIENT
116 #if !defined(MAX_LFS_FILESIZE)
117 #if BITS_PER_LONG==32
118 #define MAX_LFS_FILESIZE (((u64)PAGE_CACHE_SIZE << (BITS_PER_LONG-1))-1)
119 #elif BITS_PER_LONG==64
120 #define MAX_LFS_FILESIZE 0x7fffffffffffffff
123 sb->s_maxbytes = MAX_LFS_FILESIZE;
125 sb->s_maxbytes = MAX_NON_LFS;
130 afs_globalVFS = NULL;
131 module_put(THIS_MODULE);
135 return code ? -EINVAL : 0;
139 /* afs_root - stat the root of the file system. AFS global held on entry. */
141 afs_root(struct super_block *afsp)
143 register afs_int32 code = 0;
144 struct vrequest treq;
145 register struct vcache *tvp = 0;
147 AFS_STATCNT(afs_root);
148 if (afs_globalVp && (afs_globalVp->f.states & CStatd)) {
151 cred_t *credp = crref();
154 afs_PutVCache(afs_globalVp);
158 if (!(code = afs_InitReq(&treq, credp)) && !(code = afs_CheckInit())) {
159 tvp = afs_GetVCache(&afs_rootFid, &treq, NULL, NULL);
161 struct inode *ip = AFSTOV(tvp);
164 afs_getattr(tvp, &vattr, credp);
165 afs_fill_inode(ip, &vattr);
167 /* setup super_block and mount point inode. */
169 afsp->s_root = d_alloc_root(ip);
170 afsp->s_root->d_op = &afs_dentry_operations;
177 afs_Trace2(afs_iclSetp, CM_TRACE_VFSROOT, ICL_TYPE_POINTER, afs_globalVp,
178 ICL_TYPE_INT32, code);
182 /* super_operations */
185 * Linux version of setattr call. What to change is in the iattr struct.
186 * We need to set bits in both the Linux inode as well as the vcache.
189 afs_notify_change(struct dentry *dp, struct iattr *iattrp)
192 cred_t *credp = crref();
193 struct inode *ip = dp->d_inode;
197 iattr2vattr(&vattr, iattrp); /* Convert for AFS vnodeops call. */
201 code = afs_setattr(VTOAFS(ip), &vattr, credp);
203 afs_getattr(VTOAFS(ip), &vattr, credp);
204 vattr2inode(ip, &vattr);
213 #if defined(STRUCT_SUPER_HAS_ALLOC_INODE)
214 #if defined(HAVE_KMEM_CACHE_T)
215 static kmem_cache_t *afs_inode_cachep;
217 struct kmem_cache *afs_inode_cachep;
220 static struct inode *
221 afs_alloc_inode(struct super_block *sb)
225 #if defined(SLAB_KERNEL)
226 vcp = (struct vcache *) kmem_cache_alloc(afs_inode_cachep, SLAB_KERNEL);
228 vcp = (struct vcache *) kmem_cache_alloc(afs_inode_cachep, GFP_KERNEL);
237 afs_destroy_inode(struct inode *inode)
239 kmem_cache_free(afs_inode_cachep, inode);
243 #if defined(HAVE_KMEM_CACHE_T)
244 init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
245 #elif defined(KMEM_CACHE_INIT)
246 init_once(struct kmem_cache * cachep, void * foo)
248 init_once(void * foo, struct kmem_cache * cachep, unsigned long flags)
251 struct vcache *vcp = (struct vcache *) foo;
253 #if defined(SLAB_CTOR_VERIFY)
254 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
255 SLAB_CTOR_CONSTRUCTOR)
257 inode_init_once(AFSTOV(vcp));
261 afs_init_inodecache(void)
263 #ifndef SLAB_RECLAIM_ACCOUNT
264 #define SLAB_RECLAIM_ACCOUNT 0
267 #if defined(KMEM_CACHE_TAKES_DTOR)
268 afs_inode_cachep = kmem_cache_create("afs_inode_cache",
269 sizeof(struct vcache),
270 0, SLAB_HWCACHE_ALIGN | SLAB_RECLAIM_ACCOUNT,
273 afs_inode_cachep = kmem_cache_create("afs_inode_cache",
274 sizeof(struct vcache),
275 0, SLAB_HWCACHE_ALIGN | SLAB_RECLAIM_ACCOUNT,
278 if (afs_inode_cachep == NULL)
284 afs_destroy_inodecache(void)
286 if (afs_inode_cachep)
287 (void) kmem_cache_destroy(afs_inode_cachep);
291 afs_init_inodecache(void)
297 afs_destroy_inodecache(void)
304 afs_clear_inode(struct inode *ip)
306 struct vcache *vcp = VTOAFS(ip);
308 if (vcp->vlruq.prev || vcp->vlruq.next)
309 osi_Panic("inode freed while on LRU");
311 osi_Panic("inode freed while still hashed");
313 #if !defined(STRUCT_SUPER_HAS_ALLOC_INODE)
314 afs_osi_Free(ip->u.generic_ip, sizeof(struct vcache));
319 * Called from unmount to release super_block. */
321 afs_put_super(struct super_block *sbp)
324 AFS_STATCNT(afs_unmount);
329 osi_linux_free_inode_pages(); /* invalidate and release remaining AFS inodes. */
331 mntput(afs_cacheMnt);
333 osi_linux_verify_alloced_memory();
334 #if defined(HAVE_BDI_INIT)
335 bdi_destroy(&afs_backing_dev_info);
340 module_put(THIS_MODULE);
345 * statp is in user space, so we need to cobble together a statfs, then
349 #if defined(STATFS_TAKES_DENTRY)
350 afs_statfs(struct dentry *dentry, struct kstatfs *statp)
352 afs_statfs(struct super_block *sbp, struct kstatfs *statp)
355 memset(statp, 0, sizeof(*statp));
357 AFS_STATCNT(afs_statfs);
359 /* hardcode in case that which is giveth is taken away */
360 statp->f_type = 0x5346414F;
361 #if defined(STATFS_TAKES_DENTRY)
362 statp->f_bsize = dentry->d_sb->s_blocksize;
364 statp->f_bsize = sbp->s_blocksize;
366 statp->f_blocks = statp->f_bfree = statp->f_bavail = statp->f_files =
367 statp->f_ffree = 9000000;
368 statp->f_fsid.val[0] = AFS_VFSMAGIC;
369 statp->f_fsid.val[1] = AFS_VFSFSID;
370 statp->f_namelen = 256;
375 struct super_operations afs_sops = {
376 #if defined(STRUCT_SUPER_HAS_ALLOC_INODE)
377 .alloc_inode = afs_alloc_inode,
378 .destroy_inode = afs_destroy_inode,
380 .clear_inode = afs_clear_inode,
381 .put_super = afs_put_super,
382 .statfs = afs_statfs,
385 /************** Support routines ************************/
388 * Set iattr data into vattr. Assume vattr cleared before call.
391 iattr2vattr(struct vattr *vattrp, struct iattr *iattrp)
393 vattrp->va_mask = iattrp->ia_valid;
394 if (iattrp->ia_valid & ATTR_MODE)
395 vattrp->va_mode = iattrp->ia_mode;
396 if (iattrp->ia_valid & ATTR_UID)
397 vattrp->va_uid = iattrp->ia_uid;
398 if (iattrp->ia_valid & ATTR_GID)
399 vattrp->va_gid = iattrp->ia_gid;
400 if (iattrp->ia_valid & ATTR_SIZE)
401 vattrp->va_size = iattrp->ia_size;
402 if (iattrp->ia_valid & ATTR_ATIME) {
403 vattrp->va_atime.tv_sec = iattrp->ia_atime.tv_sec;
404 vattrp->va_atime.tv_usec = 0;
406 if (iattrp->ia_valid & ATTR_MTIME) {
407 vattrp->va_mtime.tv_sec = iattrp->ia_mtime.tv_sec;
408 vattrp->va_mtime.tv_usec = 0;
410 if (iattrp->ia_valid & ATTR_CTIME) {
411 vattrp->va_ctime.tv_sec = iattrp->ia_ctime.tv_sec;
412 vattrp->va_ctime.tv_usec = 0;
417 * Rewrite the inode cache from the attr. Assumes all vattr fields are valid.
420 vattr2inode(struct inode *ip, struct vattr *vp)
422 ip->i_ino = vp->va_nodeid;
423 ip->i_nlink = vp->va_nlink;
424 ip->i_blocks = vp->va_blocks;
425 #ifdef STRUCT_INODE_HAS_I_BLKBITS
426 ip->i_blkbits = AFS_BLKBITS;
428 #ifdef STRUCT_INODE_HAS_I_BLKSIZE
429 ip->i_blksize = vp->va_blocksize;
431 ip->i_rdev = vp->va_rdev;
432 ip->i_mode = vp->va_mode;
433 ip->i_uid = vp->va_uid;
434 ip->i_gid = vp->va_gid;
435 i_size_write(ip, vp->va_size);
436 ip->i_atime.tv_sec = vp->va_atime.tv_sec;
437 ip->i_atime.tv_nsec = 0;
438 ip->i_mtime.tv_sec = vp->va_mtime.tv_sec;
439 /* Set the mtime nanoseconds to the sysname generation number.
440 * This convinces NFS clients that all directories have changed
441 * any time the sysname list changes.
443 ip->i_mtime.tv_nsec = afs_sysnamegen;
444 ip->i_ctime.tv_sec = vp->va_ctime.tv_sec;
445 ip->i_ctime.tv_nsec = 0;
448 /* osi_linux_free_inode_pages
450 * Free all vnodes remaining in the afs hash. Must be done before
451 * shutting down afs and freeing all memory.
454 osi_linux_free_inode_pages(void)
457 struct vcache *tvc, *nvc;
458 extern struct vcache *afs_vhashT[VCSIZE];
460 for (i = 0; i < VCSIZE; i++) {
461 for (tvc = afs_vhashT[i]; tvc; ) {
465 if (afs_FlushVCache(tvc, &slept)) /* slept always 0 for linux? */
466 printf("Failed to invalidate all pages on inode 0x%p\n", tvc);