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
10 #include <afsconfig.h>
11 #include "afs/param.h"
16 #include "afs/sysincludes.h" /* Standard vendor system headers */
17 #include "afsincludes.h" /* Afs-based standard headers */
18 #include "afs/afs_stats.h" /* afs statistics */
19 #include "afs/osi_inode.h"
22 int afs_osicred_initialized = 0;
23 struct AFS_UCRED afs_osi_cred;
24 afs_lock_t afs_xosi; /* lock is for tvattr */
25 extern struct osi_dev cacheDev;
26 extern struct mount *afs_cacheVfsp;
27 int afs_CacheFSType = -1;
29 /* Initialize the cache operations. Called while initializing cache files. */
31 afs_InitDualFSCacheOps(struct vnode *vp)
34 static int inited = 0;
42 if (strncmp("hfs", vp->v_mount->mnt_vfc->vfc_name, 3) == 0)
43 afs_CacheFSType = AFS_APPL_HFS_CACHE;
44 else if (strncmp("ufs", vp->v_mount->mnt_vfc->vfc_name, 3) == 0)
45 afs_CacheFSType = AFS_APPL_UFS_CACHE;
47 osi_Panic("Unknown cache vnode type\n");
51 VnodeToIno(vnode_t * avp)
55 if (afs_CacheFSType == AFS_APPL_UFS_CACHE) {
56 struct inode *ip = VTOI(avp);
58 } else if (afs_CacheFSType == AFS_APPL_HFS_CACHE) {
61 if (VOP_GETATTR(avp, &va, &afs_osi_cred, current_proc()))
62 osi_Panic("VOP_GETATTR failed in VnodeToIno\n");
65 struct hfsnode *hp = VTOH(avp);
69 osi_Panic("VnodeToIno called before cacheops initialized\n");
75 VnodeToDev(vnode_t * avp)
79 if (afs_CacheFSType == AFS_APPL_UFS_CACHE) {
80 struct inode *ip = VTOI(avp);
82 } else if (afs_CacheFSType == AFS_APPL_HFS_CACHE) {
83 #ifndef VTOH /* slow, but works */
85 if (VOP_GETATTR(avp, &va, &afs_osi_cred, current_proc()))
86 osi_Panic("VOP_GETATTR failed in VnodeToDev\n");
87 return va.va_fsid; /* XXX they say it's the dev.... */
89 struct hfsnode *hp = VTOH(avp);
93 osi_Panic("VnodeToDev called before cacheops initialized\n");
97 osi_UFSOpen(afs_int32 ainode)
101 register struct osi_file *afile = NULL;
102 extern int cacheDiskType;
105 AFS_STATCNT(osi_UFSOpen);
106 if (cacheDiskType != AFS_FCACHE_TYPE_UFS) {
107 osi_Panic("UFSOpen called for non-UFS cache\n");
109 if (!afs_osicred_initialized) {
110 /* valid for alpha_osf, SunOS, Ultrix */
111 memset((char *)&afs_osi_cred, 0, sizeof(struct AFS_UCRED));
112 afs_osi_cred.cr_ref++;
113 afs_osi_cred.cr_ngroups = 1;
114 afs_osicred_initialized = 1;
116 afile = (struct osi_file *)osi_AllocSmallSpace(sizeof(struct osi_file));
118 if (afs_CacheFSType == AFS_APPL_HFS_CACHE)
119 code = igetinode(afs_cacheVfsp, (dev_t) cacheDev.dev, &ainode, &vp, &va, &dummy); /* XXX hfs is broken */
120 else if (afs_CacheFSType == AFS_APPL_UFS_CACHE)
122 igetinode(afs_cacheVfsp, (dev_t) cacheDev.dev, (ino_t) ainode,
125 panic("osi_UFSOpen called before cacheops initialized\n");
128 osi_FreeSmallSpace(afile);
129 osi_Panic("UFSOpen: igetinode failed");
132 afile->size = va.va_size;
134 afile->proc = (int (*)())0;
135 afile->inum = ainode; /* for hint validity checking */
136 return (void *)afile;
140 afs_osi_Stat(register struct osi_file *afile, register struct osi_stat *astat)
142 register afs_int32 code;
144 AFS_STATCNT(osi_Stat);
145 MObtainWriteLock(&afs_xosi, 320);
147 code = VOP_GETATTR(afile->vnode, &tvattr, &afs_osi_cred, current_proc());
150 astat->size = tvattr.va_size;
151 astat->blksize = tvattr.va_blocksize;
152 astat->mtime = tvattr.va_mtime.tv_sec;
153 astat->atime = tvattr.va_atime.tv_sec;
155 MReleaseWriteLock(&afs_xosi);
160 osi_UFSClose(register struct osi_file *afile)
162 AFS_STATCNT(osi_Close);
164 AFS_RELE(afile->vnode);
167 osi_FreeSmallSpace(afile);
172 osi_UFSTruncate(register struct osi_file *afile, afs_int32 asize)
174 struct AFS_UCRED *oldCred;
176 register afs_int32 code;
177 struct osi_stat tstat;
178 AFS_STATCNT(osi_Truncate);
180 /* This routine only shrinks files, and most systems
181 * have very slow truncates, even when the file is already
182 * small enough. Check now and save some time.
184 code = afs_osi_Stat(afile, &tstat);
185 if (code || tstat.size <= asize)
187 MObtainWriteLock(&afs_xosi, 321);
189 tvattr.va_size = asize;
191 code = VOP_SETATTR(afile->vnode, &tvattr, &afs_osi_cred, current_proc());
193 MReleaseWriteLock(&afs_xosi);
198 osi_DisableAtimes(struct vnode *avp)
202 if (afs_CacheFSType == AFS_APPL_UFS_CACHE) {
203 struct inode *ip = VTOI(avp);
204 ip->i_flag &= ~IN_ACCESS;
206 #ifdef VTOH /* can't do this without internals */
207 else if (afs_CacheFSType == AFS_APPL_HFS_CACHE) {
208 struct hfsnode *hp = VTOH(avp);
209 hp->h_nodeflags &= ~IN_ACCESS;
215 /* Generic read interface */
217 afs_osi_Read(register struct osi_file *afile, int offset, void *aptr,
220 struct AFS_UCRED *oldCred;
222 register afs_int32 code;
223 AFS_STATCNT(osi_Read);
226 * If the osi_file passed in is NULL, panic only if AFS is not shutting
227 * down. No point in crashing when we are already shutting down
230 if (!afs_shuttingdown)
231 osi_Panic("osi_Read called with null param");
237 afile->offset = offset;
240 gop_rdwr(UIO_READ, afile->vnode, (caddr_t) aptr, asize, afile->offset,
241 AFS_UIOSYS, IO_UNIT, &afs_osi_cred, &resid);
244 code = asize - resid;
245 afile->offset += code;
246 osi_DisableAtimes(afile->vnode);
248 afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, resid,
249 ICL_TYPE_INT32, code);
255 /* Generic write interface */
257 afs_osi_Write(register struct osi_file *afile, afs_int32 offset, void *aptr,
260 struct AFS_UCRED *oldCred;
262 register afs_int32 code;
263 AFS_STATCNT(osi_Write);
265 osi_Panic("afs_osi_Write called with null param");
267 afile->offset = offset;
271 gop_rdwr(UIO_WRITE, afile->vnode, (caddr_t) aptr, asize,
272 afile->offset, AFS_UIOSYS, IO_UNIT, &afs_osi_cred,
277 code = asize - resid;
278 afile->offset += code;
283 (*afile->proc) (afile, code);
293 shutdown_osifile(void)
295 extern int afs_cold_shutdown;
297 AFS_STATCNT(shutdown_osifile);
298 if (afs_cold_shutdown) {
299 afs_osicred_initialized = 0;