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;
35 #ifdef AFS_DARWIN80_ENV
36 char *buffer = (char*)_MALLOC(MFSNAMELEN, M_TEMP, M_WAITOK);
45 #ifdef AFS_DARWIN80_ENV
46 vfs_name(vnode_mount(vp), buffer);
47 if (strncmp("hfs", buffer, 3) == 0)
49 if (strncmp("hfs", vp->v_mount->mnt_vfc->vfc_name, 3) == 0)
51 afs_CacheFSType = AFS_APPL_HFS_CACHE;
52 #ifdef AFS_DARWIN80_ENV
53 else if (strncmp("ufs", buffer, 3) == 0)
55 else if (strncmp("ufs", vp->v_mount->mnt_vfc->vfc_name, 3) == 0)
57 afs_CacheFSType = AFS_APPL_UFS_CACHE;
59 osi_Panic("Unknown cache vnode type\n");
60 #ifdef AFS_DARWIN80_ENV
61 _FREE(buffer, M_TEMP);
66 #ifdef AFS_DARWIN80_ENV
67 VnodeToIno(vnode_t avp)
69 VnodeToIno(vnode_t * avp)
74 #ifndef AFS_DARWIN80_ENV
75 if (afs_CacheFSType == AFS_APPL_UFS_CACHE) {
76 struct inode *ip = VTOI(avp);
78 } else if (afs_CacheFSType == AFS_APPL_HFS_CACHE) {
80 #if defined(AFS_DARWIN80_ENV)
83 VATTR_WANTED(&va, va_fileid);
84 if (vnode_getattr(avp, &va, afs_osi_ctxtp))
85 osi_Panic("VOP_GETATTR failed in VnodeToIno\n");
86 if (!VATTR_ALL_SUPPORTED(&va))
87 osi_Panic("VOP_GETATTR unsupported fileid in VnodeToIno\n");
91 if (VOP_GETATTR(avp, &va, &afs_osi_cred, current_proc()))
92 osi_Panic("VOP_GETATTR failed in VnodeToIno\n");
95 struct hfsnode *hp = VTOH(avp);
98 #ifndef AFS_DARWIN80_ENV
100 osi_Panic("VnodeToIno called before cacheops initialized\n");
107 #ifdef AFS_DARWIN80_ENV
108 VnodeToDev(vnode_t avp)
110 VnodeToDev(vnode_t * avp)
113 #ifndef AFS_DARWIN80_ENV
114 if (afs_CacheFSType == AFS_APPL_UFS_CACHE) {
115 struct inode *ip = VTOI(avp);
117 } else if (afs_CacheFSType == AFS_APPL_HFS_CACHE) {
119 #if defined(AFS_DARWIN80_ENV)
122 VATTR_WANTED(&va, va_fsid);
123 if (vnode_getattr(avp, &va, afs_osi_ctxtp))
124 osi_Panic("VOP_GETATTR failed in VnodeToDev\n");
125 if (!VATTR_ALL_SUPPORTED(&va))
126 osi_Panic("VOP_GETATTR unsupported fsid in VnodeToIno\n");
127 return va.va_fsid; /* XXX they say it's the dev.... */
130 if (VOP_GETATTR(avp, &va, &afs_osi_cred, current_proc()))
131 osi_Panic("VOP_GETATTR failed in VnodeToDev\n");
132 return va.va_fsid; /* XXX they say it's the dev.... */
134 struct hfsnode *hp = VTOH(avp);
137 #ifndef AFS_DARWIN80_ENV
139 osi_Panic("VnodeToDev called before cacheops initialized\n");
144 osi_UFSOpen(afs_int32 ainode)
148 register struct osi_file *afile = NULL;
149 extern int cacheDiskType;
152 AFS_STATCNT(osi_UFSOpen);
153 if (cacheDiskType != AFS_FCACHE_TYPE_UFS) {
154 osi_Panic("UFSOpen called for non-UFS cache\n");
156 if (!afs_osicred_initialized) {
157 /* valid for alpha_osf, SunOS, Ultrix */
158 memset((char *)&afs_osi_cred, 0, sizeof(struct AFS_UCRED));
159 afs_osi_cred.cr_ref++;
160 afs_osi_cred.cr_ngroups = 1;
161 afs_osicred_initialized = 1;
163 afile = (struct osi_file *)osi_AllocSmallSpace(sizeof(struct osi_file));
165 #ifndef AFS_DARWIN80_ENV
166 if (afs_CacheFSType == AFS_APPL_HFS_CACHE)
167 code = igetinode(afs_cacheVfsp, (dev_t) cacheDev.dev, &ainode, &vp, &va, &dummy); /* XXX hfs is broken */
168 else if (afs_CacheFSType == AFS_APPL_UFS_CACHE)
171 igetinode(afs_cacheVfsp, (dev_t) cacheDev.dev, (ino_t) ainode,
173 #ifndef AFS_DARWIN80_ENV
175 panic("osi_UFSOpen called before cacheops initialized\n");
179 osi_FreeSmallSpace(afile);
180 osi_Panic("UFSOpen: igetinode failed");
183 afile->size = va.va_size;
185 afile->proc = (int (*)())0;
186 afile->inum = ainode; /* for hint validity checking */
187 return (void *)afile;
191 afs_osi_Stat(register struct osi_file *afile, register struct osi_stat *astat)
193 register afs_int32 code;
195 AFS_STATCNT(osi_Stat);
196 MObtainWriteLock(&afs_xosi, 320);
198 #ifdef AFS_DARWIN80_ENV
200 VATTR_WANTED(&tvattr, va_size);
201 VATTR_WANTED(&tvattr, va_blocksize);
202 VATTR_WANTED(&tvattr, va_mtime);
203 VATTR_WANTED(&tvattr, va_atime);
204 code = vnode_getattr(afile->vnode, &tvattr, afs_osi_ctxtp);
205 if (code == 0 && !VATTR_ALL_SUPPORTED(&tvattr))
208 code = VOP_GETATTR(afile->vnode, &tvattr, &afs_osi_cred, current_proc());
212 astat->size = tvattr.va_size;
213 astat->blksize = tvattr.va_blocksize;
214 astat->mtime = tvattr.va_mtime.tv_sec;
215 astat->atime = tvattr.va_atime.tv_sec;
217 MReleaseWriteLock(&afs_xosi);
222 osi_UFSClose(register struct osi_file *afile)
224 AFS_STATCNT(osi_Close);
226 #ifdef AFS_DARWIN80_ENV
227 vnode_close(afile->vnode, O_RDWR, afs_osi_ctxtp);
229 AFS_RELE(afile->vnode);
233 osi_FreeSmallSpace(afile);
238 osi_UFSTruncate(register struct osi_file *afile, afs_int32 asize)
240 struct AFS_UCRED *oldCred;
242 register afs_int32 code;
243 struct osi_stat tstat;
244 AFS_STATCNT(osi_Truncate);
246 /* This routine only shrinks files, and most systems
247 * have very slow truncates, even when the file is already
248 * small enough. Check now and save some time.
250 code = afs_osi_Stat(afile, &tstat);
251 if (code || tstat.size <= asize)
253 MObtainWriteLock(&afs_xosi, 321);
255 #ifdef AFS_DARWIN80_ENV
257 VATTR_SET(&tvattr, va_size, asize);
258 code = vnode_getattr(afile->vnode, &tvattr, afs_osi_ctxtp);
261 tvattr.va_size = asize;
262 code = VOP_SETATTR(afile->vnode, &tvattr, &afs_osi_cred, current_proc());
265 MReleaseWriteLock(&afs_xosi);
270 #ifdef AFS_DARWIN80_ENV
271 osi_DisableAtimes(vnode_t avp)
273 osi_DisableAtimes(struct vnode *avp)
276 #ifdef AFS_DARWIN80_ENV
277 struct vnode_attr vap;
280 VATTR_CLEAR_SUPPORTED(&vap, va_access_time);
281 vnode_setattr(avp, &vap, afs_osi_ctxtp);
283 if (afs_CacheFSType == AFS_APPL_UFS_CACHE) {
284 struct inode *ip = VTOI(avp);
285 ip->i_flag &= ~IN_ACCESS;
287 #ifdef VTOH /* can't do this without internals */
288 else if (afs_CacheFSType == AFS_APPL_HFS_CACHE) {
289 struct hfsnode *hp = VTOH(avp);
290 hp->h_nodeflags &= ~IN_ACCESS;
297 /* Generic read interface */
299 afs_osi_Read(register struct osi_file *afile, int offset, void *aptr,
302 struct AFS_UCRED *oldCred;
304 register afs_int32 code;
305 #ifdef AFS_DARWIN80_ENV
308 AFS_STATCNT(osi_Read);
311 * If the osi_file passed in is NULL, panic only if AFS is not shutting
312 * down. No point in crashing when we are already shutting down
315 if (!afs_shuttingdown)
316 osi_Panic("osi_Read called with null param");
322 afile->offset = offset;
324 #ifdef AFS_DARWIN80_ENV
325 uio=uio_create(1, afile->offset, AFS_UIOSYS, UIO_READ);
326 uio_addiov(uio, CAST_USER_ADDR_T(aptr), asize);
327 code = VNOP_READ(afile->vnode, uio, IO_UNIT, afs_osi_ctxtp);
328 resid = AFS_UIO_RESID(uio);
332 gop_rdwr(UIO_READ, afile->vnode, (caddr_t) aptr, asize, afile->offset,
333 AFS_UIOSYS, IO_UNIT, &afs_osi_cred, &resid);
337 code = asize - resid;
338 afile->offset += code;
339 osi_DisableAtimes(afile->vnode);
341 afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, resid,
342 ICL_TYPE_INT32, code);
348 /* Generic write interface */
350 afs_osi_Write(register struct osi_file *afile, afs_int32 offset, void *aptr,
353 struct AFS_UCRED *oldCred;
355 register afs_int32 code;
356 #ifdef AFS_DARWIN80_ENV
359 AFS_STATCNT(osi_Write);
361 osi_Panic("afs_osi_Write called with null param");
363 afile->offset = offset;
366 #ifdef AFS_DARWIN80_ENV
367 uio=uio_create(1, afile->offset, AFS_UIOSYS, UIO_WRITE);
368 uio_addiov(uio, CAST_USER_ADDR_T(aptr), asize);
369 code = VNOP_WRITE(afile->vnode, uio, IO_UNIT, afs_osi_ctxtp);
370 resid = AFS_UIO_RESID(uio);
374 gop_rdwr(UIO_WRITE, afile->vnode, (caddr_t) aptr, asize,
375 afile->offset, AFS_UIOSYS, IO_UNIT, &afs_osi_cred,
381 code = asize - resid;
382 afile->offset += code;
387 (*afile->proc) (afile, code);
397 shutdown_osifile(void)
399 extern int afs_cold_shutdown;
401 AFS_STATCNT(shutdown_osifile);
402 if (afs_cold_shutdown) {
403 afs_osicred_initialized = 0;