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"
14 #include "afs/sysincludes.h" /* Standard vendor system headers */
15 #include "afsincludes.h" /* Afs-based standard headers */
16 #include "afs/afs_stats.h" /* afs statistics */
17 #include "afs/osi_inode.h"
20 int afs_osicred_initialized = 0;
21 afs_ucred_t afs_osi_cred;
22 extern struct osi_dev cacheDev;
23 extern struct mount *afs_cacheVfsp;
24 int afs_CacheFSType = -1;
26 /* Initialize the cache operations. Called while initializing cache files. */
28 afs_InitDualFSCacheOps(struct vnode *vp)
31 static int inited = 0;
32 #ifdef AFS_DARWIN80_ENV
33 char *buffer = (char*)_MALLOC(MFSNAMELEN, M_TEMP, M_WAITOK);
42 #ifdef AFS_DARWIN80_ENV
43 vfs_name(vnode_mount(vp), buffer);
44 if (strncmp("hfs", buffer, 3) == 0)
46 if (strncmp("hfs", vp->v_mount->mnt_vfc->vfc_name, 3) == 0)
48 afs_CacheFSType = AFS_APPL_HFS_CACHE;
49 #ifdef AFS_DARWIN80_ENV
50 else if (strncmp("ufs", buffer, 3) == 0)
52 else if (strncmp("ufs", vp->v_mount->mnt_vfc->vfc_name, 3) == 0)
54 afs_CacheFSType = AFS_APPL_UFS_CACHE;
55 #ifdef AFS_DARWIN80_ENV
56 else if (strncmp("apfs", buffer, 4) == 0)
57 afs_CacheFSType = AFS_APPL_APFS_CACHE;
60 osi_Panic("Unknown cache vnode type\n");
61 #ifdef AFS_DARWIN80_ENV
62 _FREE(buffer, M_TEMP);
67 VnodeToIno(vnode_t avp)
71 #ifndef AFS_DARWIN80_ENV
72 if (afs_CacheFSType == AFS_APPL_UFS_CACHE) {
73 struct inode *ip = VTOI(avp);
75 } else if (afs_CacheFSType == AFS_APPL_HFS_CACHE) {
77 #if defined(AFS_DARWIN80_ENV)
80 VATTR_WANTED(&va, va_fileid);
81 if (vnode_getattr(avp, &va, afs_osi_ctxtp))
82 osi_Panic("VOP_GETATTR failed in VnodeToIno\n");
83 if (!VATTR_ALL_SUPPORTED(&va))
84 osi_Panic("VOP_GETATTR unsupported fileid in VnodeToIno\n");
88 if (VOP_GETATTR(avp, &va, &afs_osi_cred, current_proc()))
89 osi_Panic("VOP_GETATTR failed in VnodeToIno\n");
92 struct hfsnode *hp = VTOH(avp);
95 #ifndef AFS_DARWIN80_ENV
97 osi_Panic("VnodeToIno called before cacheops initialized\n");
104 VnodeToDev(vnode_t avp)
106 #ifndef AFS_DARWIN80_ENV
107 if (afs_CacheFSType == AFS_APPL_UFS_CACHE) {
108 struct inode *ip = VTOI(avp);
110 } else if (afs_CacheFSType == AFS_APPL_HFS_CACHE) {
112 #if defined(AFS_DARWIN80_ENV)
115 VATTR_WANTED(&va, va_fsid);
116 if (vnode_getattr(avp, &va, afs_osi_ctxtp))
117 osi_Panic("VOP_GETATTR failed in VnodeToDev\n");
118 if (!VATTR_ALL_SUPPORTED(&va))
119 osi_Panic("VOP_GETATTR unsupported fsid in VnodeToIno\n");
120 return va.va_fsid; /* XXX they say it's the dev.... */
123 if (VOP_GETATTR(avp, &va, &afs_osi_cred, current_proc()))
124 osi_Panic("VOP_GETATTR failed in VnodeToDev\n");
125 return va.va_fsid; /* XXX they say it's the dev.... */
127 struct hfsnode *hp = VTOH(avp);
130 #ifndef AFS_DARWIN80_ENV
132 osi_Panic("VnodeToDev called before cacheops initialized\n");
137 osi_UFSOpen(afs_dcache_id_t *ainode)
141 struct osi_file *afile = NULL;
142 extern int cacheDiskType;
146 struct osi_stat tstat;
148 AFS_STATCNT(osi_UFSOpen);
149 if (cacheDiskType != AFS_FCACHE_TYPE_UFS) {
150 osi_Panic("UFSOpen called for non-UFS cache\n");
152 if (!afs_osicred_initialized) {
153 memset(&afs_osi_cred, 0, sizeof(afs_ucred_t));
154 afs_osi_cred.cr_ref++;
155 #ifndef AFS_DARWIN110_ENV
156 afs_osi_cred.cr_ngroups = 1;
158 afs_osicred_initialized = 1;
160 afile = osi_AllocSmallSpace(sizeof(struct osi_file));
162 #ifdef AFS_CACHE_VNODE_PATH
164 osi_Panic("No cache inode\n");
167 code = vnode_open(ainode->ufs, O_RDWR, 0, 0, &vp, afs_osi_ctxtp);
169 #ifndef AFS_DARWIN80_ENV
170 if (afs_CacheFSType == AFS_APPL_HFS_CACHE)
171 code = igetinode(afs_cacheVfsp, (dev_t) cacheDev.dev, &ainode->ufs, &vp, &va, &dummy); /* XXX hfs is broken */
172 else if (afs_CacheFSType == AFS_APPL_UFS_CACHE)
175 igetinode(afs_cacheVfsp, (dev_t) cacheDev.dev, (ino_t) ainode->ufs,
177 #ifndef AFS_DARWIN80_ENV
179 panic("osi_UFSOpen called before cacheops initialized\n");
184 osi_FreeSmallSpace(afile);
185 osi_Panic("UFSOpen: igetinode failed");
189 afile->proc = (int (*)())0;
190 #ifndef AFS_CACHE_VNODE_PATH
191 afile->size = va.va_size;
193 code = afs_osi_Stat(afile, &tstat);
194 afile->size = tstat.size;
196 return (void *)afile;
200 afs_osi_Stat(struct osi_file *afile, struct osi_stat *astat)
204 AFS_STATCNT(osi_Stat);
206 #ifdef AFS_DARWIN80_ENV
208 VATTR_WANTED(&tvattr, va_size);
209 VATTR_WANTED(&tvattr, va_blocksize);
210 VATTR_WANTED(&tvattr, va_mtime);
211 VATTR_WANTED(&tvattr, va_atime);
212 code = vnode_getattr(afile->vnode, &tvattr, afs_osi_ctxtp);
213 if (code == 0 && !VATTR_ALL_SUPPORTED(&tvattr))
216 code = VOP_GETATTR(afile->vnode, &tvattr, &afs_osi_cred, current_proc());
220 astat->size = tvattr.va_size;
221 astat->mtime = tvattr.va_mtime.tv_sec;
222 astat->atime = tvattr.va_atime.tv_sec;
228 osi_UFSClose(struct osi_file *afile)
230 AFS_STATCNT(osi_Close);
232 #ifdef AFS_DARWIN80_ENV
233 vnode_close(afile->vnode, O_RDWR, afs_osi_ctxtp);
235 AFS_RELE(afile->vnode);
239 osi_FreeSmallSpace(afile);
244 osi_UFSTruncate(struct osi_file *afile, afs_int32 asize)
246 afs_ucred_t *oldCred;
249 struct osi_stat tstat;
250 AFS_STATCNT(osi_Truncate);
252 /* This routine only shrinks files, and most systems
253 * have very slow truncates, even when the file is already
254 * small enough. Check now and save some time.
256 code = afs_osi_Stat(afile, &tstat);
257 if (code || tstat.size <= asize)
260 #ifdef AFS_DARWIN80_ENV
262 VATTR_SET(&tvattr, va_size, asize);
263 code = vnode_setattr(afile->vnode, &tvattr, afs_osi_ctxtp);
266 tvattr.va_size = asize;
267 code = VOP_SETATTR(afile->vnode, &tvattr, &afs_osi_cred, current_proc());
274 #ifdef AFS_DARWIN80_ENV
275 osi_DisableAtimes(vnode_t avp)
277 osi_DisableAtimes(struct vnode *avp)
280 #ifdef AFS_DARWIN80_ENV
281 struct vnode_attr vap;
284 VATTR_CLEAR_SUPPORTED(&vap, va_access_time);
285 vnode_setattr(avp, &vap, afs_osi_ctxtp);
287 if (afs_CacheFSType == AFS_APPL_UFS_CACHE) {
288 struct inode *ip = VTOI(avp);
289 ip->i_flag &= ~IN_ACCESS;
291 #ifdef VTOH /* can't do this without internals */
292 else if (afs_CacheFSType == AFS_APPL_HFS_CACHE) {
293 struct hfsnode *hp = VTOH(avp);
294 hp->h_nodeflags &= ~IN_ACCESS;
301 /* Generic read interface */
303 afs_osi_Read(struct osi_file *afile, int offset, void *aptr,
306 afs_ucred_t *oldCred;
309 #ifdef AFS_DARWIN80_ENV
312 AFS_STATCNT(osi_Read);
315 * If the osi_file passed in is NULL, panic only if AFS is not shutting
316 * down. No point in crashing when we are already shutting down
319 if (afs_shuttingdown == AFS_RUNNING)
320 osi_Panic("osi_Read called with null param");
326 afile->offset = offset;
328 #ifdef AFS_DARWIN80_ENV
329 uio=uio_create(1, afile->offset, AFS_UIOSYS, UIO_READ);
330 uio_addiov(uio, CAST_USER_ADDR_T(aptr), asize);
331 code = VNOP_READ(afile->vnode, uio, IO_UNIT, afs_osi_ctxtp);
332 resid = AFS_UIO_RESID(uio);
336 gop_rdwr(UIO_READ, afile->vnode, (caddr_t) aptr, asize, afile->offset,
337 AFS_UIOSYS, IO_UNIT, &afs_osi_cred, &resid);
341 code = asize - resid;
342 afile->offset += code;
343 osi_DisableAtimes(afile->vnode);
345 afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, resid,
346 ICL_TYPE_INT32, code);
354 /* Generic write interface */
356 afs_osi_Write(struct osi_file *afile, afs_int32 offset, void *aptr,
359 afs_ucred_t *oldCred;
362 #ifdef AFS_DARWIN80_ENV
365 AFS_STATCNT(osi_Write);
367 osi_Panic("afs_osi_Write called with null param");
369 afile->offset = offset;
372 #ifdef AFS_DARWIN80_ENV
373 uio=uio_create(1, afile->offset, AFS_UIOSYS, UIO_WRITE);
374 uio_addiov(uio, CAST_USER_ADDR_T(aptr), asize);
375 code = VNOP_WRITE(afile->vnode, uio, IO_UNIT, afs_osi_ctxtp);
376 resid = AFS_UIO_RESID(uio);
380 gop_rdwr(UIO_WRITE, afile->vnode, (caddr_t) aptr, asize,
381 afile->offset, AFS_UIOSYS, IO_UNIT, &afs_osi_cred,
387 code = asize - resid;
388 afile->offset += code;
395 (*afile->proc) (afile, code);
405 shutdown_osifile(void)
407 AFS_STATCNT(shutdown_osifile);
408 if (afs_cold_shutdown) {
409 afs_osicred_initialized = 0;