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;
57 osi_Panic("Unknown cache vnode type\n");
58 #ifdef AFS_DARWIN80_ENV
59 _FREE(buffer, M_TEMP);
64 VnodeToIno(vnode_t avp)
68 #ifndef AFS_DARWIN80_ENV
69 if (afs_CacheFSType == AFS_APPL_UFS_CACHE) {
70 struct inode *ip = VTOI(avp);
72 } else if (afs_CacheFSType == AFS_APPL_HFS_CACHE) {
74 #if defined(AFS_DARWIN80_ENV)
77 VATTR_WANTED(&va, va_fileid);
78 if (vnode_getattr(avp, &va, afs_osi_ctxtp))
79 osi_Panic("VOP_GETATTR failed in VnodeToIno\n");
80 if (!VATTR_ALL_SUPPORTED(&va))
81 osi_Panic("VOP_GETATTR unsupported fileid in VnodeToIno\n");
85 if (VOP_GETATTR(avp, &va, &afs_osi_cred, current_proc()))
86 osi_Panic("VOP_GETATTR failed in VnodeToIno\n");
89 struct hfsnode *hp = VTOH(avp);
92 #ifndef AFS_DARWIN80_ENV
94 osi_Panic("VnodeToIno called before cacheops initialized\n");
101 VnodeToDev(vnode_t avp)
103 #ifndef AFS_DARWIN80_ENV
104 if (afs_CacheFSType == AFS_APPL_UFS_CACHE) {
105 struct inode *ip = VTOI(avp);
107 } else if (afs_CacheFSType == AFS_APPL_HFS_CACHE) {
109 #if defined(AFS_DARWIN80_ENV)
112 VATTR_WANTED(&va, va_fsid);
113 if (vnode_getattr(avp, &va, afs_osi_ctxtp))
114 osi_Panic("VOP_GETATTR failed in VnodeToDev\n");
115 if (!VATTR_ALL_SUPPORTED(&va))
116 osi_Panic("VOP_GETATTR unsupported fsid in VnodeToIno\n");
117 return va.va_fsid; /* XXX they say it's the dev.... */
120 if (VOP_GETATTR(avp, &va, &afs_osi_cred, current_proc()))
121 osi_Panic("VOP_GETATTR failed in VnodeToDev\n");
122 return va.va_fsid; /* XXX they say it's the dev.... */
124 struct hfsnode *hp = VTOH(avp);
127 #ifndef AFS_DARWIN80_ENV
129 osi_Panic("VnodeToDev called before cacheops initialized\n");
134 osi_UFSOpen(afs_dcache_id_t *ainode)
138 register struct osi_file *afile = NULL;
139 extern int cacheDiskType;
143 struct osi_stat tstat;
145 AFS_STATCNT(osi_UFSOpen);
146 if (cacheDiskType != AFS_FCACHE_TYPE_UFS) {
147 osi_Panic("UFSOpen called for non-UFS cache\n");
149 if (!afs_osicred_initialized) {
150 /* valid for alpha_osf, SunOS, Ultrix */
151 memset(&afs_osi_cred, 0, sizeof(afs_ucred_t));
152 afs_osi_cred.cr_ref++;
153 afs_osi_cred.cr_ngroups = 1;
154 afs_osicred_initialized = 1;
156 afile = (struct osi_file *)osi_AllocSmallSpace(sizeof(struct osi_file));
158 #ifdef AFS_CACHE_VNODE_PATH
159 if (ainode->ufs < 0) {
160 switch (ainode->ufs) {
161 case AFS_CACHE_CELLS_INODE:
162 snprintf(fname, 1024, "%s/%s", afs_cachebasedir, "CellItems");
164 case AFS_CACHE_ITEMS_INODE:
165 snprintf(fname, 1024, "%s/%s", afs_cachebasedir, "CacheItems");
167 case AFS_CACHE_VOLUME_INODE:
168 snprintf(fname, 1024, "%s/%s", afs_cachebasedir, "VolumeItems");
171 osi_Panic("Invalid negative inode");
174 dummy = ainode->ufs / afs_numfilesperdir;
175 snprintf(fname, 1024, "%s/D%d/V%d", afs_cachebasedir, dummy, ainode->ufs);
178 code = vnode_open(fname, O_RDWR, 0, 0, &vp, afs_osi_ctxtp);
180 #ifndef AFS_DARWIN80_ENV
181 if (afs_CacheFSType == AFS_APPL_HFS_CACHE)
182 code = igetinode(afs_cacheVfsp, (dev_t) cacheDev.dev, &ainode->ufs, &vp, &va, &dummy); /* XXX hfs is broken */
183 else if (afs_CacheFSType == AFS_APPL_UFS_CACHE)
186 igetinode(afs_cacheVfsp, (dev_t) cacheDev.dev, (ino_t) ainode->ufs,
188 #ifndef AFS_DARWIN80_ENV
190 panic("osi_UFSOpen called before cacheops initialized\n");
195 osi_FreeSmallSpace(afile);
196 osi_Panic("UFSOpen: igetinode failed");
200 afile->proc = (int (*)())0;
201 #ifndef AFS_CACHE_VNODE_PATH
202 afile->size = va.va_size;
204 code = afs_osi_Stat(afile, &tstat);
205 afile->size = tstat.size;
207 return (void *)afile;
211 afs_osi_Stat(register struct osi_file *afile, register struct osi_stat *astat)
213 register afs_int32 code;
215 AFS_STATCNT(osi_Stat);
216 ObtainWriteLock(&afs_xosi, 320);
218 #ifdef AFS_DARWIN80_ENV
220 VATTR_WANTED(&tvattr, va_size);
221 VATTR_WANTED(&tvattr, va_blocksize);
222 VATTR_WANTED(&tvattr, va_mtime);
223 VATTR_WANTED(&tvattr, va_atime);
224 code = vnode_getattr(afile->vnode, &tvattr, afs_osi_ctxtp);
225 if (code == 0 && !VATTR_ALL_SUPPORTED(&tvattr))
228 code = VOP_GETATTR(afile->vnode, &tvattr, &afs_osi_cred, current_proc());
232 astat->size = tvattr.va_size;
233 astat->mtime = tvattr.va_mtime.tv_sec;
234 astat->atime = tvattr.va_atime.tv_sec;
236 ReleaseWriteLock(&afs_xosi);
241 osi_UFSClose(register struct osi_file *afile)
243 AFS_STATCNT(osi_Close);
245 #ifdef AFS_DARWIN80_ENV
246 vnode_close(afile->vnode, O_RDWR, afs_osi_ctxtp);
248 AFS_RELE(afile->vnode);
252 osi_FreeSmallSpace(afile);
257 osi_UFSTruncate(register struct osi_file *afile, afs_int32 asize)
259 afs_ucred_t *oldCred;
261 register afs_int32 code;
262 struct osi_stat tstat;
263 AFS_STATCNT(osi_Truncate);
265 /* This routine only shrinks files, and most systems
266 * have very slow truncates, even when the file is already
267 * small enough. Check now and save some time.
269 code = afs_osi_Stat(afile, &tstat);
270 if (code || tstat.size <= asize)
272 ObtainWriteLock(&afs_xosi, 321);
274 #ifdef AFS_DARWIN80_ENV
276 VATTR_SET(&tvattr, va_size, asize);
277 code = vnode_setattr(afile->vnode, &tvattr, afs_osi_ctxtp);
280 tvattr.va_size = asize;
281 code = VOP_SETATTR(afile->vnode, &tvattr, &afs_osi_cred, current_proc());
284 ReleaseWriteLock(&afs_xosi);
289 #ifdef AFS_DARWIN80_ENV
290 osi_DisableAtimes(vnode_t avp)
292 osi_DisableAtimes(struct vnode *avp)
295 #ifdef AFS_DARWIN80_ENV
296 struct vnode_attr vap;
299 VATTR_CLEAR_SUPPORTED(&vap, va_access_time);
300 vnode_setattr(avp, &vap, afs_osi_ctxtp);
302 if (afs_CacheFSType == AFS_APPL_UFS_CACHE) {
303 struct inode *ip = VTOI(avp);
304 ip->i_flag &= ~IN_ACCESS;
306 #ifdef VTOH /* can't do this without internals */
307 else if (afs_CacheFSType == AFS_APPL_HFS_CACHE) {
308 struct hfsnode *hp = VTOH(avp);
309 hp->h_nodeflags &= ~IN_ACCESS;
316 /* Generic read interface */
318 afs_osi_Read(register struct osi_file *afile, int offset, void *aptr,
321 afs_ucred_t *oldCred;
323 register afs_int32 code;
324 #ifdef AFS_DARWIN80_ENV
327 AFS_STATCNT(osi_Read);
330 * If the osi_file passed in is NULL, panic only if AFS is not shutting
331 * down. No point in crashing when we are already shutting down
334 if (!afs_shuttingdown)
335 osi_Panic("osi_Read called with null param");
341 afile->offset = offset;
343 #ifdef AFS_DARWIN80_ENV
344 uio=uio_create(1, afile->offset, AFS_UIOSYS, UIO_READ);
345 uio_addiov(uio, CAST_USER_ADDR_T(aptr), asize);
346 code = VNOP_READ(afile->vnode, uio, IO_UNIT, afs_osi_ctxtp);
347 resid = AFS_UIO_RESID(uio);
351 gop_rdwr(UIO_READ, afile->vnode, (caddr_t) aptr, asize, afile->offset,
352 AFS_UIOSYS, IO_UNIT, &afs_osi_cred, &resid);
356 code = asize - resid;
357 afile->offset += code;
358 osi_DisableAtimes(afile->vnode);
360 afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, resid,
361 ICL_TYPE_INT32, code);
367 /* Generic write interface */
369 afs_osi_Write(register struct osi_file *afile, afs_int32 offset, void *aptr,
372 afs_ucred_t *oldCred;
374 register afs_int32 code;
375 #ifdef AFS_DARWIN80_ENV
378 AFS_STATCNT(osi_Write);
380 osi_Panic("afs_osi_Write called with null param");
382 afile->offset = offset;
385 #ifdef AFS_DARWIN80_ENV
386 uio=uio_create(1, afile->offset, AFS_UIOSYS, UIO_WRITE);
387 uio_addiov(uio, CAST_USER_ADDR_T(aptr), asize);
388 code = VNOP_WRITE(afile->vnode, uio, IO_UNIT, afs_osi_ctxtp);
389 resid = AFS_UIO_RESID(uio);
393 gop_rdwr(UIO_WRITE, afile->vnode, (caddr_t) aptr, asize,
394 afile->offset, AFS_UIOSYS, IO_UNIT, &afs_osi_cred,
400 code = asize - resid;
401 afile->offset += code;
406 (*afile->proc) (afile, code);
416 shutdown_osifile(void)
418 AFS_STATCNT(shutdown_osifile);
419 if (afs_cold_shutdown) {
420 afs_osicred_initialized = 0;