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"
15 #include "../afs/sysincludes.h" /* Standard vendor system headers */
16 #include "../afs/afsincludes.h" /* Afs-based standard headers */
17 #include "../afs/afs_stats.h" /* afs statistics */
18 #include "../afs/osi_inode.h"
21 int afs_osicred_initialized=0;
22 struct AFS_UCRED afs_osi_cred;
23 afs_lock_t afs_xosi; /* lock is for tvattr */
24 extern struct osi_dev cacheDev;
25 extern struct mount *afs_cacheVfsp;
26 int afs_CacheFSType = -1;
28 /* Initialize the cache operations. Called while initializing cache files. */
29 void afs_InitDualFSCacheOps(struct vnode *vp)
32 static int inited = 0;
40 if (strncmp("hfs", vp->v_mount->mnt_vfc->vfc_name, 3) == 0)
41 afs_CacheFSType = AFS_APPL_HFS_CACHE;
43 if (strncmp("ufs", vp->v_mount->mnt_vfc->vfc_name, 3) == 0)
44 afs_CacheFSType = AFS_APPL_UFS_CACHE;
46 osi_Panic("Unknown cache vnode type\n");
49 ino_t VnodeToIno(vnode_t *avp)
53 if (afs_CacheFSType == AFS_APPL_UFS_CACHE) {
54 struct inode *ip = VTOI(avp);
56 } else if (afs_CacheFSType == AFS_APPL_HFS_CACHE) {
59 if (VOP_GETATTR(avp, &va, &afs_osi_cred, current_proc()))
60 osi_Panic("VOP_GETATTR failed in VnodeToIno\n");
63 struct hfsnode *hp = VTOH(avp);
67 osi_Panic("VnodeToIno called before cacheops initialized\n");
72 dev_t VnodeToDev(vnode_t *avp)
76 if (afs_CacheFSType == AFS_APPL_UFS_CACHE) {
77 struct inode *ip = VTOI(avp);
80 if (afs_CacheFSType == AFS_APPL_HFS_CACHE) {
81 #ifndef VTOH /* slow, but works */
83 if (VOP_GETATTR(avp, &va, &afs_osi_cred, current_proc()))
84 osi_Panic("VOP_GETATTR failed in VnodeToDev\n");
85 return va.va_fsid; /* XXX they say it's the dev.... */
87 struct hfsnode *hp = VTOH(avp);
91 osi_Panic("VnodeToDev called before cacheops initialized\n");
94 void *osi_UFSOpen(ainode)
99 register struct osi_file *afile = NULL;
100 extern int cacheDiskType;
103 AFS_STATCNT(osi_UFSOpen);
104 if(cacheDiskType != AFS_FCACHE_TYPE_UFS) {
105 osi_Panic("UFSOpen called for non-UFS cache\n");
107 if (!afs_osicred_initialized) {
108 /* valid for alpha_osf, SunOS, Ultrix */
109 bzero((char *)&afs_osi_cred, sizeof(struct AFS_UCRED));
110 afs_osi_cred.cr_ref++;
111 afs_osi_cred.cr_ngroups=1;
112 afs_osicred_initialized = 1;
114 afile = (struct osi_file *) osi_AllocSmallSpace(sizeof(struct osi_file));
116 if (afs_CacheFSType == AFS_APPL_HFS_CACHE)
117 code = igetinode(afs_cacheVfsp, (dev_t) cacheDev.dev, &ainode, &vp, &va, &dummy); /* XXX hfs is broken */
119 if (afs_CacheFSType == AFS_APPL_UFS_CACHE)
120 code = igetinode(afs_cacheVfsp, (dev_t) cacheDev.dev, (ino_t)ainode, &vp, &va, &dummy);
122 panic("osi_UFSOpen called before cacheops initialized\n");
125 osi_FreeSmallSpace(afile);
126 osi_Panic("UFSOpen: igetinode failed");
129 afile->size = va.va_size;
131 afile->proc = (int (*)()) 0;
132 afile->inum = ainode; /* for hint validity checking */
133 return (void *)afile;
136 afs_osi_Stat(afile, astat)
137 register struct osi_file *afile;
138 register struct osi_stat *astat; {
139 register afs_int32 code;
141 AFS_STATCNT(osi_Stat);
142 MObtainWriteLock(&afs_xosi,320);
144 code=VOP_GETATTR(afile->vnode, &tvattr, &afs_osi_cred, current_proc());
147 astat->size = tvattr.va_size;
148 astat->blksize = tvattr.va_blocksize;
149 astat->mtime = tvattr.va_mtime.tv_sec;
150 astat->atime = tvattr.va_atime.tv_sec;
152 MReleaseWriteLock(&afs_xosi);
157 register struct osi_file *afile;
159 AFS_STATCNT(osi_Close);
161 AFS_RELE(afile->vnode);
164 osi_FreeSmallSpace(afile);
168 osi_UFSTruncate(afile, asize)
169 register struct osi_file *afile;
171 struct AFS_UCRED *oldCred;
173 register afs_int32 code;
174 struct osi_stat tstat;
175 AFS_STATCNT(osi_Truncate);
177 /* This routine only shrinks files, and most systems
178 * have very slow truncates, even when the file is already
179 * small enough. Check now and save some time.
181 code = afs_osi_Stat(afile, &tstat);
182 if (code || tstat.size <= asize) return code;
183 MObtainWriteLock(&afs_xosi,321);
185 tvattr.va_size = asize;
187 code=VOP_SETATTR(afile->vnode, &tvattr, &afs_osi_cred, current_proc());
189 MReleaseWriteLock(&afs_xosi);
193 void osi_DisableAtimes(avp)
198 if (afs_CacheFSType == AFS_APPL_UFS_CACHE) {
199 struct inode *ip = VTOI(avp);
200 ip->i_flag &= ~IN_ACCESS;
202 #ifdef VTOH /* can't do this without internals */
203 else if (afs_CacheFSType == AFS_APPL_HFS_CACHE) {
204 struct hfsnode *hp = VTOH(avp);
205 hp->h_nodeflags &= ~IN_ACCESS;
211 /* Generic read interface */
212 afs_osi_Read(afile, offset, aptr, asize)
213 register struct osi_file *afile;
217 struct AFS_UCRED *oldCred;
219 register afs_int32 code;
220 AFS_STATCNT(osi_Read);
223 * If the osi_file passed in is NULL, panic only if AFS is not shutting
224 * down. No point in crashing when we are already shutting down
227 if ( !afs_shuttingdown )
228 osi_Panic("osi_Read called with null param");
233 if (offset != -1) afile->offset = offset;
235 code = gop_rdwr(UIO_READ, afile->vnode, (caddr_t) aptr, asize, afile->offset,
236 AFS_UIOSYS, IO_UNIT, &afs_osi_cred, &resid);
239 code = asize - resid;
240 afile->offset += code;
241 osi_DisableAtimes(afile->vnode);
244 afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, resid,
245 ICL_TYPE_INT32, code);
251 /* Generic write interface */
252 afs_osi_Write(afile, offset, aptr, asize)
253 register struct osi_file *afile;
257 struct AFS_UCRED *oldCred;
259 register afs_int32 code;
260 AFS_STATCNT(osi_Write);
262 osi_Panic("afs_osi_Write called with null param");
263 if (offset != -1) afile->offset = offset;
266 code = gop_rdwr(UIO_WRITE, afile->vnode, (caddr_t) aptr, asize, afile->offset,
267 AFS_UIOSYS, IO_UNIT, &afs_osi_cred, &resid);
271 code = asize - resid;
272 afile->offset += code;
278 (*afile->proc)(afile, code);
290 extern int afs_cold_shutdown;
292 AFS_STATCNT(shutdown_osifile);
293 if (afs_cold_shutdown) {
294 afs_osicred_initialized = 0;