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 afs_lock_t afs_xosi; /* lock is for tvattr */
23 extern struct osi_dev cacheDev;
24 extern struct mount *afs_cacheVfsp;
25 int afs_CacheFSType = -1;
27 /* Initialize the cache operations. Called while initializing cache files. */
29 afs_InitDualFSCacheOps(struct vnode *vp)
32 static int inited = 0;
33 #ifdef AFS_DARWIN80_ENV
34 char *buffer = (char*)_MALLOC(MFSNAMELEN, M_TEMP, M_WAITOK);
43 #ifdef AFS_DARWIN80_ENV
44 vfs_name(vnode_mount(vp), buffer);
45 if (strncmp("hfs", buffer, 3) == 0)
47 if (strncmp("hfs", vp->v_mount->mnt_vfc->vfc_name, 3) == 0)
49 afs_CacheFSType = AFS_APPL_HFS_CACHE;
50 #ifdef AFS_DARWIN80_ENV
51 else if (strncmp("ufs", buffer, 3) == 0)
53 else if (strncmp("ufs", vp->v_mount->mnt_vfc->vfc_name, 3) == 0)
55 afs_CacheFSType = AFS_APPL_UFS_CACHE;
56 #ifdef AFS_DARWIN80_ENV
57 else if (strncmp("apfs", buffer, 4) == 0)
58 afs_CacheFSType = AFS_APPL_APFS_CACHE;
61 osi_Panic("Unknown cache vnode type\n");
62 #ifdef AFS_DARWIN80_ENV
63 _FREE(buffer, M_TEMP);
68 VnodeToIno(vnode_t avp)
72 #ifndef AFS_DARWIN80_ENV
73 if (afs_CacheFSType == AFS_APPL_UFS_CACHE) {
74 struct inode *ip = VTOI(avp);
76 } else if (afs_CacheFSType == AFS_APPL_HFS_CACHE) {
78 #if defined(AFS_DARWIN80_ENV)
81 VATTR_WANTED(&va, va_fileid);
82 if (vnode_getattr(avp, &va, afs_osi_ctxtp))
83 osi_Panic("VOP_GETATTR failed in VnodeToIno\n");
84 if (!VATTR_ALL_SUPPORTED(&va))
85 osi_Panic("VOP_GETATTR unsupported fileid in VnodeToIno\n");
89 if (VOP_GETATTR(avp, &va, &afs_osi_cred, current_proc()))
90 osi_Panic("VOP_GETATTR failed in VnodeToIno\n");
93 struct hfsnode *hp = VTOH(avp);
96 #ifndef AFS_DARWIN80_ENV
98 osi_Panic("VnodeToIno called before cacheops initialized\n");
105 VnodeToDev(vnode_t avp)
107 #ifndef AFS_DARWIN80_ENV
108 if (afs_CacheFSType == AFS_APPL_UFS_CACHE) {
109 struct inode *ip = VTOI(avp);
111 } else if (afs_CacheFSType == AFS_APPL_HFS_CACHE) {
113 #if defined(AFS_DARWIN80_ENV)
116 VATTR_WANTED(&va, va_fsid);
117 if (vnode_getattr(avp, &va, afs_osi_ctxtp))
118 osi_Panic("VOP_GETATTR failed in VnodeToDev\n");
119 if (!VATTR_ALL_SUPPORTED(&va))
120 osi_Panic("VOP_GETATTR unsupported fsid in VnodeToIno\n");
121 return va.va_fsid; /* XXX they say it's the dev.... */
124 if (VOP_GETATTR(avp, &va, &afs_osi_cred, current_proc()))
125 osi_Panic("VOP_GETATTR failed in VnodeToDev\n");
126 return va.va_fsid; /* XXX they say it's the dev.... */
128 struct hfsnode *hp = VTOH(avp);
131 #ifndef AFS_DARWIN80_ENV
133 osi_Panic("VnodeToDev called before cacheops initialized\n");
138 osi_UFSOpen(afs_dcache_id_t *ainode)
142 struct osi_file *afile = NULL;
143 extern int cacheDiskType;
147 struct osi_stat tstat;
149 AFS_STATCNT(osi_UFSOpen);
150 if (cacheDiskType != AFS_FCACHE_TYPE_UFS) {
151 osi_Panic("UFSOpen called for non-UFS cache\n");
153 if (!afs_osicred_initialized) {
154 /* valid for alpha_osf, SunOS, Ultrix */
155 memset(&afs_osi_cred, 0, sizeof(afs_ucred_t));
156 afs_osi_cred.cr_ref++;
157 #ifndef AFS_DARWIN110_ENV
158 afs_osi_cred.cr_ngroups = 1;
160 afs_osicred_initialized = 1;
162 afile = osi_AllocSmallSpace(sizeof(struct osi_file));
164 #ifdef AFS_CACHE_VNODE_PATH
166 osi_Panic("No cache inode\n");
169 code = vnode_open(ainode->ufs, O_RDWR, 0, 0, &vp, afs_osi_ctxtp);
171 #ifndef AFS_DARWIN80_ENV
172 if (afs_CacheFSType == AFS_APPL_HFS_CACHE)
173 code = igetinode(afs_cacheVfsp, (dev_t) cacheDev.dev, &ainode->ufs, &vp, &va, &dummy); /* XXX hfs is broken */
174 else if (afs_CacheFSType == AFS_APPL_UFS_CACHE)
177 igetinode(afs_cacheVfsp, (dev_t) cacheDev.dev, (ino_t) ainode->ufs,
179 #ifndef AFS_DARWIN80_ENV
181 panic("osi_UFSOpen called before cacheops initialized\n");
186 osi_FreeSmallSpace(afile);
187 osi_Panic("UFSOpen: igetinode failed");
191 afile->proc = (int (*)())0;
192 #ifndef AFS_CACHE_VNODE_PATH
193 afile->size = va.va_size;
195 code = afs_osi_Stat(afile, &tstat);
196 afile->size = tstat.size;
198 return (void *)afile;
202 afs_osi_Stat(struct osi_file *afile, struct osi_stat *astat)
206 AFS_STATCNT(osi_Stat);
207 ObtainWriteLock(&afs_xosi, 320);
209 #ifdef AFS_DARWIN80_ENV
211 VATTR_WANTED(&tvattr, va_size);
212 VATTR_WANTED(&tvattr, va_blocksize);
213 VATTR_WANTED(&tvattr, va_mtime);
214 VATTR_WANTED(&tvattr, va_atime);
215 code = vnode_getattr(afile->vnode, &tvattr, afs_osi_ctxtp);
216 if (code == 0 && !VATTR_ALL_SUPPORTED(&tvattr))
219 code = VOP_GETATTR(afile->vnode, &tvattr, &afs_osi_cred, current_proc());
223 astat->size = tvattr.va_size;
224 astat->mtime = tvattr.va_mtime.tv_sec;
225 astat->atime = tvattr.va_atime.tv_sec;
227 ReleaseWriteLock(&afs_xosi);
232 osi_UFSClose(struct osi_file *afile)
234 AFS_STATCNT(osi_Close);
236 #ifdef AFS_DARWIN80_ENV
237 vnode_close(afile->vnode, O_RDWR, afs_osi_ctxtp);
239 AFS_RELE(afile->vnode);
243 osi_FreeSmallSpace(afile);
248 osi_UFSTruncate(struct osi_file *afile, afs_int32 asize)
250 afs_ucred_t *oldCred;
253 struct osi_stat tstat;
254 AFS_STATCNT(osi_Truncate);
256 /* This routine only shrinks files, and most systems
257 * have very slow truncates, even when the file is already
258 * small enough. Check now and save some time.
260 code = afs_osi_Stat(afile, &tstat);
261 if (code || tstat.size <= asize)
263 ObtainWriteLock(&afs_xosi, 321);
265 #ifdef AFS_DARWIN80_ENV
267 VATTR_SET(&tvattr, va_size, asize);
268 code = vnode_setattr(afile->vnode, &tvattr, afs_osi_ctxtp);
271 tvattr.va_size = asize;
272 code = VOP_SETATTR(afile->vnode, &tvattr, &afs_osi_cred, current_proc());
275 ReleaseWriteLock(&afs_xosi);
280 #ifdef AFS_DARWIN80_ENV
281 osi_DisableAtimes(vnode_t avp)
283 osi_DisableAtimes(struct vnode *avp)
286 #ifdef AFS_DARWIN80_ENV
287 struct vnode_attr vap;
290 VATTR_CLEAR_SUPPORTED(&vap, va_access_time);
291 vnode_setattr(avp, &vap, afs_osi_ctxtp);
293 if (afs_CacheFSType == AFS_APPL_UFS_CACHE) {
294 struct inode *ip = VTOI(avp);
295 ip->i_flag &= ~IN_ACCESS;
297 #ifdef VTOH /* can't do this without internals */
298 else if (afs_CacheFSType == AFS_APPL_HFS_CACHE) {
299 struct hfsnode *hp = VTOH(avp);
300 hp->h_nodeflags &= ~IN_ACCESS;
307 /* Generic read interface */
309 afs_osi_Read(struct osi_file *afile, int offset, void *aptr,
312 afs_ucred_t *oldCred;
315 #ifdef AFS_DARWIN80_ENV
318 AFS_STATCNT(osi_Read);
321 * If the osi_file passed in is NULL, panic only if AFS is not shutting
322 * down. No point in crashing when we are already shutting down
325 if (afs_shuttingdown == AFS_RUNNING)
326 osi_Panic("osi_Read called with null param");
332 afile->offset = offset;
334 #ifdef AFS_DARWIN80_ENV
335 uio=uio_create(1, afile->offset, AFS_UIOSYS, UIO_READ);
336 uio_addiov(uio, CAST_USER_ADDR_T(aptr), asize);
337 code = VNOP_READ(afile->vnode, uio, IO_UNIT, afs_osi_ctxtp);
338 resid = AFS_UIO_RESID(uio);
342 gop_rdwr(UIO_READ, afile->vnode, (caddr_t) aptr, asize, afile->offset,
343 AFS_UIOSYS, IO_UNIT, &afs_osi_cred, &resid);
347 code = asize - resid;
348 afile->offset += code;
349 osi_DisableAtimes(afile->vnode);
351 afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, resid,
352 ICL_TYPE_INT32, code);
360 /* Generic write interface */
362 afs_osi_Write(struct osi_file *afile, afs_int32 offset, void *aptr,
365 afs_ucred_t *oldCred;
368 #ifdef AFS_DARWIN80_ENV
371 AFS_STATCNT(osi_Write);
373 osi_Panic("afs_osi_Write called with null param");
375 afile->offset = offset;
378 #ifdef AFS_DARWIN80_ENV
379 uio=uio_create(1, afile->offset, AFS_UIOSYS, UIO_WRITE);
380 uio_addiov(uio, CAST_USER_ADDR_T(aptr), asize);
381 code = VNOP_WRITE(afile->vnode, uio, IO_UNIT, afs_osi_ctxtp);
382 resid = AFS_UIO_RESID(uio);
386 gop_rdwr(UIO_WRITE, afile->vnode, (caddr_t) aptr, asize,
387 afile->offset, AFS_UIOSYS, IO_UNIT, &afs_osi_cred,
393 code = asize - resid;
394 afile->offset += code;
401 (*afile->proc) (afile, code);
411 shutdown_osifile(void)
413 AFS_STATCNT(shutdown_osifile);
414 if (afs_cold_shutdown) {
415 afs_osicred_initialized = 0;