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 #if !defined(AFS_LINUX26_ENV)
27 #if defined(AFS_LINUX24_ENV)
28 #include "h/smp_lock.h"
32 struct vcache *afs_globalVp = 0;
33 struct vfs *afs_globalVFS = 0;
34 #if defined(AFS_LINUX24_ENV)
35 struct vfsmount *afs_cacheMnt;
37 int afs_was_mounted = 0; /* Used to force reload if mount/unmount/mount */
39 extern struct super_operations afs_sops;
40 #if defined(AFS_LINUX26_ENV) && !defined(AFS_NONFSTRANS)
41 extern struct export_operations afs_export_ops;
43 extern afs_rwlock_t afs_xvcache;
44 extern struct afs_q VLRU;
46 extern struct dentry_operations afs_dentry_operations;
48 /* Forward declarations */
49 static void iattr2vattr(struct vattr *vattrp, struct iattr *iattrp);
50 static int afs_root(struct super_block *afsp);
51 struct super_block *afs_read_super(struct super_block *sb, void *data, int silent);
52 int afs_fill_super(struct super_block *sb, void *data, int silent);
55 * VFS entry for Linux - installed in init_module
56 * Linux mounts file systems by:
57 * 1) register_filesystem(&afs_file_system) - done in init_module
58 * 2) Mount call comes to us via do_mount -> read_super -> afs_read_super.
59 * We are expected to setup the super_block. See afs_read_super.
64 * read the "super block" for AFS - roughly eguivalent to struct vfs.
65 * dev, covered, s_rd_only, s_dirt, and s_type will be set by read_super.
67 #if defined(AFS_LINUX26_ENV)
68 #ifdef GET_SB_HAS_STRUCT_VFSMOUNT
70 afs_get_sb(struct file_system_type *fs_type, int flags,
71 const char *dev_name, void *data, struct vfsmount *mnt)
73 static struct superblock *
74 afs_get_sb(struct file_system_type *fs_type, int flags,
75 const char *dev_name, void *data)
78 #ifdef GET_SB_HAS_STRUCT_VFSMOUNT
79 return get_sb_nodev(fs_type, flags, data, afs_fill_super, mnt);
81 return get_sb_nodev(fs_type, flags, data, afs_fill_super);
86 struct file_system_type afs_fs_type = {
90 .kill_sb = kill_anon_super,
91 .fs_flags = FS_BINARY_MOUNTDATA,
93 #elif defined(AFS_LINUX24_ENV)
94 DECLARE_FSTYPE(afs_fs_type, "afs", afs_read_super, 0);
96 struct file_system_type afs_fs_type = {
97 "afs", /* name - used by mount operation. */
98 0, /* requires_dev - no for network filesystems. mount() will
99 * pass us an "unnamed" device. */
100 afs_read_super, /* wrapper to afs_mount */
101 NULL /* pointer to next file_system_type once registered. */
105 #if defined(AFS_LINUX26_ENV)
106 struct backing_dev_info afs_backing_dev_info = {
111 afs_fill_super(struct super_block *sb, void *data, int silent)
114 afs_read_super(struct super_block *sb, void *data, int silent)
120 if (afs_was_mounted) {
122 ("You must reload the AFS kernel extensions before remounting AFS.\n");
124 #if defined(AFS_LINUX26_ENV)
132 /* Set basics of super_block */
133 #if !defined(AFS_LINUX24_ENV)
136 #if defined(AFS_LINUX26_ENV)
137 __module_get(THIS_MODULE);
143 sb->s_blocksize = 1024;
144 sb->s_blocksize_bits = 10;
145 sb->s_magic = AFS_VFSMAGIC;
146 sb->s_op = &afs_sops; /* Super block (vfs) ops */
147 #if defined(HAVE_BDI_INIT)
148 bdi_init(&afs_backing_dev_info);
150 #if defined(AFS_LINUX26_ENV) && !defined(AFS_NONFSTRANS)
151 sb->s_export_op = &afs_export_ops;
153 #if defined(MAX_NON_LFS)
154 #ifdef AFS_64BIT_CLIENT
155 #if !defined(MAX_LFS_FILESIZE)
156 #if BITS_PER_LONG==32
157 #define MAX_LFS_FILESIZE (((u64)PAGE_CACHE_SIZE << (BITS_PER_LONG-1))-1)
158 #elif BITS_PER_LONG==64
159 #define MAX_LFS_FILESIZE 0x7fffffffffffffff
162 sb->s_maxbytes = MAX_LFS_FILESIZE;
164 sb->s_maxbytes = MAX_NON_LFS;
169 afs_globalVFS = NULL;
170 #if defined(AFS_LINUX26_ENV)
171 module_put(THIS_MODULE);
177 #if !defined(AFS_LINUX24_ENV)
182 #if defined(AFS_LINUX26_ENV)
183 return code ? -EINVAL : 0;
185 return code ? NULL : sb;
190 /* afs_root - stat the root of the file system. AFS global held on entry. */
192 afs_root(struct super_block *afsp)
194 register afs_int32 code = 0;
195 struct vrequest treq;
196 register struct vcache *tvp = 0;
198 AFS_STATCNT(afs_root);
199 if (afs_globalVp && (afs_globalVp->f.states & CStatd)) {
202 cred_t *credp = crref();
205 afs_PutVCache(afs_globalVp);
209 if (!(code = afs_InitReq(&treq, credp)) && !(code = afs_CheckInit())) {
210 tvp = afs_GetVCache(&afs_rootFid, &treq, NULL, NULL);
212 struct inode *ip = AFSTOV(tvp);
215 afs_getattr(tvp, &vattr, credp);
216 afs_fill_inode(ip, &vattr);
218 /* setup super_block and mount point inode. */
220 #if defined(AFS_LINUX24_ENV)
221 afsp->s_root = d_alloc_root(ip);
223 afsp->s_root = d_alloc_root(ip, NULL);
225 afsp->s_root->d_op = &afs_dentry_operations;
232 afs_Trace2(afs_iclSetp, CM_TRACE_VFSROOT, ICL_TYPE_POINTER, afs_globalVp,
233 ICL_TYPE_INT32, code);
237 /* super_operations */
240 * Linux version of setattr call. What to change is in the iattr struct.
241 * We need to set bits in both the Linux inode as well as the vcache.
244 afs_notify_change(struct dentry *dp, struct iattr *iattrp)
247 cred_t *credp = crref();
248 struct inode *ip = dp->d_inode;
252 iattr2vattr(&vattr, iattrp); /* Convert for AFS vnodeops call. */
254 #if defined(AFS_LINUX26_ENV)
258 code = afs_setattr(VTOAFS(ip), &vattr, credp);
260 afs_getattr(VTOAFS(ip), &vattr, credp);
261 vattr2inode(ip, &vattr);
264 #if defined(AFS_LINUX26_ENV)
272 #if defined(STRUCT_SUPER_HAS_ALLOC_INODE)
273 #if defined(HAVE_KMEM_CACHE_T)
274 static kmem_cache_t *afs_inode_cachep;
276 struct kmem_cache *afs_inode_cachep;
279 static struct inode *
280 afs_alloc_inode(struct super_block *sb)
284 #if defined(SLAB_KERNEL)
285 vcp = (struct vcache *) kmem_cache_alloc(afs_inode_cachep, SLAB_KERNEL);
287 vcp = (struct vcache *) kmem_cache_alloc(afs_inode_cachep, GFP_KERNEL);
296 afs_destroy_inode(struct inode *inode)
298 kmem_cache_free(afs_inode_cachep, inode);
302 #if defined(HAVE_KMEM_CACHE_T)
303 init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
305 #if defined(KMEM_CACHE_INIT)
306 init_once(struct kmem_cache * cachep, void * foo)
308 init_once(void * foo, struct kmem_cache * cachep, unsigned long flags)
312 struct vcache *vcp = (struct vcache *) foo;
314 #if defined(SLAB_CTOR_VERIFY)
315 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
316 SLAB_CTOR_CONSTRUCTOR)
318 inode_init_once(AFSTOV(vcp));
322 afs_init_inodecache(void)
324 #ifndef SLAB_RECLAIM_ACCOUNT
325 #define SLAB_RECLAIM_ACCOUNT 0
328 #if defined(KMEM_CACHE_TAKES_DTOR)
329 afs_inode_cachep = kmem_cache_create("afs_inode_cache",
330 sizeof(struct vcache),
331 0, SLAB_HWCACHE_ALIGN | SLAB_RECLAIM_ACCOUNT,
334 afs_inode_cachep = kmem_cache_create("afs_inode_cache",
335 sizeof(struct vcache),
336 0, SLAB_HWCACHE_ALIGN | SLAB_RECLAIM_ACCOUNT,
339 if (afs_inode_cachep == NULL)
345 afs_destroy_inodecache(void)
347 if (afs_inode_cachep)
348 (void) kmem_cache_destroy(afs_inode_cachep);
352 afs_init_inodecache(void)
358 afs_destroy_inodecache(void)
365 afs_clear_inode(struct inode *ip)
367 struct vcache *vcp = VTOAFS(ip);
369 if (vcp->vlruq.prev || vcp->vlruq.next)
370 osi_Panic("inode freed while on LRU");
372 osi_Panic("inode freed while still hashed");
374 #if !defined(STRUCT_SUPER_HAS_ALLOC_INODE)
375 afs_osi_Free(ip->u.generic_ip, sizeof(struct vcache));
380 * Called from unmount to release super_block. */
382 afs_put_super(struct super_block *sbp)
385 AFS_STATCNT(afs_unmount);
387 #if !defined(AFS_LINUX26_ENV)
397 osi_linux_free_inode_pages(); /* invalidate and release remaining AFS inodes. */
399 #if defined(AFS_LINUX24_ENV)
400 mntput(afs_cacheMnt);
403 osi_linux_verify_alloced_memory();
404 #if defined(HAVE_BDI_INIT)
405 bdi_destroy(&afs_backing_dev_info);
410 #if defined(AFS_LINUX26_ENV)
411 module_put(THIS_MODULE);
419 * statp is in user space, so we need to cobble together a statfs, then
422 #if defined(AFS_LINUX26_ENV)
424 #if defined(STATFS_TAKES_DENTRY)
425 afs_statfs(struct dentry *dentry, struct kstatfs *statp)
427 afs_statfs(struct super_block *sbp, struct kstatfs *statp)
429 #elif defined(AFS_LINUX24_ENV)
431 afs_statfs(struct super_block *sbp, struct statfs *statp)
434 afs_statfs(struct super_block *sbp, struct statfs *__statp, int size)
437 #if !defined(AFS_LINUX24_ENV)
438 struct statfs stat, *statp;
440 if (size < sizeof(struct statfs))
443 memset(&stat, 0, size);
446 memset(statp, 0, sizeof(*statp));
449 AFS_STATCNT(afs_statfs);
451 /* hardcode in case that which is giveth is taken away */
452 statp->f_type = 0x5346414F;
453 #if defined(STATFS_TAKES_DENTRY)
454 statp->f_bsize = dentry->d_sb->s_blocksize;
456 statp->f_bsize = sbp->s_blocksize;
458 statp->f_blocks = statp->f_bfree = statp->f_bavail = statp->f_files =
459 statp->f_ffree = 9000000;
460 statp->f_fsid.val[0] = AFS_VFSMAGIC;
461 statp->f_fsid.val[1] = AFS_VFSFSID;
462 statp->f_namelen = 256;
464 #if !defined(AFS_LINUX24_ENV)
465 memcpy_tofs(__statp, &stat, size);
470 struct super_operations afs_sops = {
471 #if defined(STRUCT_SUPER_HAS_ALLOC_INODE)
472 .alloc_inode = afs_alloc_inode,
473 .destroy_inode = afs_destroy_inode,
475 .clear_inode = afs_clear_inode,
476 .put_super = afs_put_super,
477 .statfs = afs_statfs,
478 #if !defined(AFS_LINUX24_ENV)
479 .notify_change = afs_notify_change,
483 /************** Support routines ************************/
486 * Set iattr data into vattr. Assume vattr cleared before call.
489 iattr2vattr(struct vattr *vattrp, struct iattr *iattrp)
491 vattrp->va_mask = iattrp->ia_valid;
492 if (iattrp->ia_valid & ATTR_MODE)
493 vattrp->va_mode = iattrp->ia_mode;
494 if (iattrp->ia_valid & ATTR_UID)
495 vattrp->va_uid = iattrp->ia_uid;
496 if (iattrp->ia_valid & ATTR_GID)
497 vattrp->va_gid = iattrp->ia_gid;
498 if (iattrp->ia_valid & ATTR_SIZE)
499 vattrp->va_size = iattrp->ia_size;
500 if (iattrp->ia_valid & ATTR_ATIME) {
501 #if defined(AFS_LINUX26_ENV)
502 vattrp->va_atime.tv_sec = iattrp->ia_atime.tv_sec;
504 vattrp->va_atime.tv_sec = iattrp->ia_atime;
506 vattrp->va_atime.tv_usec = 0;
508 if (iattrp->ia_valid & ATTR_MTIME) {
509 #if defined(AFS_LINUX26_ENV)
510 vattrp->va_mtime.tv_sec = iattrp->ia_mtime.tv_sec;
512 vattrp->va_mtime.tv_sec = iattrp->ia_mtime;
514 vattrp->va_mtime.tv_usec = 0;
516 if (iattrp->ia_valid & ATTR_CTIME) {
517 #if defined(AFS_LINUX26_ENV)
518 vattrp->va_ctime.tv_sec = iattrp->ia_ctime.tv_sec;
520 vattrp->va_ctime.tv_sec = iattrp->ia_ctime;
522 vattrp->va_ctime.tv_usec = 0;
527 * Rewrite the inode cache from the attr. Assumes all vattr fields are valid.
530 vattr2inode(struct inode *ip, struct vattr *vp)
532 ip->i_ino = vp->va_nodeid;
533 ip->i_nlink = vp->va_nlink;
534 ip->i_blocks = vp->va_blocks;
535 #ifdef STRUCT_INODE_HAS_I_BLKBITS
536 ip->i_blkbits = AFS_BLKBITS;
538 #ifdef STRUCT_INODE_HAS_I_BLKSIZE
539 ip->i_blksize = vp->va_blocksize;
541 ip->i_rdev = vp->va_rdev;
542 ip->i_mode = vp->va_mode;
543 ip->i_uid = vp->va_uid;
544 ip->i_gid = vp->va_gid;
545 i_size_write(ip, vp->va_size);
546 #if defined(AFS_LINUX26_ENV)
547 ip->i_atime.tv_sec = vp->va_atime.tv_sec;
548 ip->i_atime.tv_nsec = 0;
549 ip->i_mtime.tv_sec = vp->va_mtime.tv_sec;
550 /* Set the mtime nanoseconds to the sysname generation number.
551 * This convinces NFS clients that all directories have changed
552 * any time the sysname list changes.
554 ip->i_mtime.tv_nsec = afs_sysnamegen;
555 ip->i_ctime.tv_sec = vp->va_ctime.tv_sec;
556 ip->i_ctime.tv_nsec = 0;
558 ip->i_atime = vp->va_atime.tv_sec;
559 ip->i_mtime = vp->va_mtime.tv_sec;
560 ip->i_ctime = vp->va_ctime.tv_sec;
564 /* osi_linux_free_inode_pages
566 * Free all vnodes remaining in the afs hash. Must be done before
567 * shutting down afs and freeing all memory.
570 osi_linux_free_inode_pages(void)
573 struct vcache *tvc, *nvc;
574 extern struct vcache *afs_vhashT[VCSIZE];
576 for (i = 0; i < VCSIZE; i++) {
577 for (tvc = afs_vhashT[i]; tvc; ) {
581 if (afs_FlushVCache(tvc, &slept)) /* slept always 0 for linux? */
582 printf("Failed to invalidate all pages on inode 0x%p\n", tvc);