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 VnodeToIno(vnode_t avp)
70 #ifndef AFS_DARWIN80_ENV
71 if (afs_CacheFSType == AFS_APPL_UFS_CACHE) {
72 struct inode *ip = VTOI(avp);
74 } else if (afs_CacheFSType == AFS_APPL_HFS_CACHE) {
76 #if defined(AFS_DARWIN80_ENV)
79 VATTR_WANTED(&va, va_fileid);
80 if (vnode_getattr(avp, &va, afs_osi_ctxtp))
81 osi_Panic("VOP_GETATTR failed in VnodeToIno\n");
82 if (!VATTR_ALL_SUPPORTED(&va))
83 osi_Panic("VOP_GETATTR unsupported fileid in VnodeToIno\n");
87 if (VOP_GETATTR(avp, &va, &afs_osi_cred, current_proc()))
88 osi_Panic("VOP_GETATTR failed in VnodeToIno\n");
91 struct hfsnode *hp = VTOH(avp);
94 #ifndef AFS_DARWIN80_ENV
96 osi_Panic("VnodeToIno called before cacheops initialized\n");
103 VnodeToDev(vnode_t avp)
105 #ifndef AFS_DARWIN80_ENV
106 if (afs_CacheFSType == AFS_APPL_UFS_CACHE) {
107 struct inode *ip = VTOI(avp);
109 } else if (afs_CacheFSType == AFS_APPL_HFS_CACHE) {
111 #if defined(AFS_DARWIN80_ENV)
114 VATTR_WANTED(&va, va_fsid);
115 if (vnode_getattr(avp, &va, afs_osi_ctxtp))
116 osi_Panic("VOP_GETATTR failed in VnodeToDev\n");
117 if (!VATTR_ALL_SUPPORTED(&va))
118 osi_Panic("VOP_GETATTR unsupported fsid in VnodeToIno\n");
119 return va.va_fsid; /* XXX they say it's the dev.... */
122 if (VOP_GETATTR(avp, &va, &afs_osi_cred, current_proc()))
123 osi_Panic("VOP_GETATTR failed in VnodeToDev\n");
124 return va.va_fsid; /* XXX they say it's the dev.... */
126 struct hfsnode *hp = VTOH(avp);
129 #ifndef AFS_DARWIN80_ENV
131 osi_Panic("VnodeToDev called before cacheops initialized\n");
136 osi_UFSOpen(afs_int32 ainode)
140 register struct osi_file *afile = NULL;
141 extern int cacheDiskType;
145 struct osi_stat tstat;
147 AFS_STATCNT(osi_UFSOpen);
148 if (cacheDiskType != AFS_FCACHE_TYPE_UFS) {
149 osi_Panic("UFSOpen called for non-UFS cache\n");
151 if (!afs_osicred_initialized) {
152 /* valid for alpha_osf, SunOS, Ultrix */
153 memset((char *)&afs_osi_cred, 0, sizeof(struct AFS_UCRED));
154 afs_osi_cred.cr_ref++;
155 afs_osi_cred.cr_ngroups = 1;
156 afs_osicred_initialized = 1;
158 afile = (struct osi_file *)osi_AllocSmallSpace(sizeof(struct osi_file));
160 #ifdef AFS_CACHE_VNODE_PATH
163 case AFS_CACHE_CELLS_INODE:
164 snprintf(fname, 1024, "%s/%s", afs_cachebasedir, "CellItems");
166 case AFS_CACHE_ITEMS_INODE:
167 snprintf(fname, 1024, "%s/%s", afs_cachebasedir, "CacheItems");
169 case AFS_CACHE_VOLUME_INODE:
170 snprintf(fname, 1024, "%s/%s", afs_cachebasedir, "VolumeItems");
173 osi_Panic("Invalid negative inode");
176 dummy = ainode / afs_numfilesperdir;
177 snprintf(fname, 1024, "%s/D%d/V%d", afs_cachebasedir, dummy, ainode);
180 code = vnode_open(fname, O_RDWR, 0, 0, &vp, afs_osi_ctxtp);
182 #ifndef AFS_DARWIN80_ENV
183 if (afs_CacheFSType == AFS_APPL_HFS_CACHE)
184 code = igetinode(afs_cacheVfsp, (dev_t) cacheDev.dev, &ainode, &vp, &va, &dummy); /* XXX hfs is broken */
185 else if (afs_CacheFSType == AFS_APPL_UFS_CACHE)
188 igetinode(afs_cacheVfsp, (dev_t) cacheDev.dev, (ino_t) ainode,
190 #ifndef AFS_DARWIN80_ENV
192 panic("osi_UFSOpen called before cacheops initialized\n");
197 osi_FreeSmallSpace(afile);
198 osi_Panic("UFSOpen: igetinode failed");
202 afile->proc = (int (*)())0;
203 afile->inum = ainode; /* for hint validity checking */
204 #ifndef AFS_CACHE_VNODE_PATH
205 afile->size = va.va_size;
207 code = afs_osi_Stat(afile, &tstat);
208 afile->size = tstat.size;
210 return (void *)afile;
214 afs_osi_Stat(register struct osi_file *afile, register struct osi_stat *astat)
216 register afs_int32 code;
218 AFS_STATCNT(osi_Stat);
219 MObtainWriteLock(&afs_xosi, 320);
221 #ifdef AFS_DARWIN80_ENV
223 VATTR_WANTED(&tvattr, va_size);
224 VATTR_WANTED(&tvattr, va_blocksize);
225 VATTR_WANTED(&tvattr, va_mtime);
226 VATTR_WANTED(&tvattr, va_atime);
227 code = vnode_getattr(afile->vnode, &tvattr, afs_osi_ctxtp);
228 if (code == 0 && !VATTR_ALL_SUPPORTED(&tvattr))
231 code = VOP_GETATTR(afile->vnode, &tvattr, &afs_osi_cred, current_proc());
235 astat->size = tvattr.va_size;
236 astat->mtime = tvattr.va_mtime.tv_sec;
237 astat->atime = tvattr.va_atime.tv_sec;
239 MReleaseWriteLock(&afs_xosi);
244 osi_UFSClose(register struct osi_file *afile)
246 AFS_STATCNT(osi_Close);
248 #ifdef AFS_DARWIN80_ENV
249 vnode_close(afile->vnode, O_RDWR, afs_osi_ctxtp);
251 AFS_RELE(afile->vnode);
255 osi_FreeSmallSpace(afile);
260 osi_UFSTruncate(register struct osi_file *afile, afs_int32 asize)
262 struct AFS_UCRED *oldCred;
264 register afs_int32 code;
265 struct osi_stat tstat;
266 AFS_STATCNT(osi_Truncate);
268 /* This routine only shrinks files, and most systems
269 * have very slow truncates, even when the file is already
270 * small enough. Check now and save some time.
272 code = afs_osi_Stat(afile, &tstat);
273 if (code || tstat.size <= asize)
275 MObtainWriteLock(&afs_xosi, 321);
277 #ifdef AFS_DARWIN80_ENV
279 VATTR_SET(&tvattr, va_size, asize);
280 code = vnode_setattr(afile->vnode, &tvattr, afs_osi_ctxtp);
283 tvattr.va_size = asize;
284 code = VOP_SETATTR(afile->vnode, &tvattr, &afs_osi_cred, current_proc());
287 MReleaseWriteLock(&afs_xosi);
292 #ifdef AFS_DARWIN80_ENV
293 osi_DisableAtimes(vnode_t avp)
295 osi_DisableAtimes(struct vnode *avp)
298 #ifdef AFS_DARWIN80_ENV
299 struct vnode_attr vap;
302 VATTR_CLEAR_SUPPORTED(&vap, va_access_time);
303 vnode_setattr(avp, &vap, afs_osi_ctxtp);
305 if (afs_CacheFSType == AFS_APPL_UFS_CACHE) {
306 struct inode *ip = VTOI(avp);
307 ip->i_flag &= ~IN_ACCESS;
309 #ifdef VTOH /* can't do this without internals */
310 else if (afs_CacheFSType == AFS_APPL_HFS_CACHE) {
311 struct hfsnode *hp = VTOH(avp);
312 hp->h_nodeflags &= ~IN_ACCESS;
319 /* Generic read interface */
321 afs_osi_Read(register struct osi_file *afile, int offset, void *aptr,
324 struct AFS_UCRED *oldCred;
326 register afs_int32 code;
327 #ifdef AFS_DARWIN80_ENV
330 AFS_STATCNT(osi_Read);
333 * If the osi_file passed in is NULL, panic only if AFS is not shutting
334 * down. No point in crashing when we are already shutting down
337 if (!afs_shuttingdown)
338 osi_Panic("osi_Read called with null param");
344 afile->offset = offset;
346 #ifdef AFS_DARWIN80_ENV
347 uio=uio_create(1, afile->offset, AFS_UIOSYS, UIO_READ);
348 uio_addiov(uio, CAST_USER_ADDR_T(aptr), asize);
349 code = VNOP_READ(afile->vnode, uio, IO_UNIT, afs_osi_ctxtp);
350 resid = AFS_UIO_RESID(uio);
354 gop_rdwr(UIO_READ, afile->vnode, (caddr_t) aptr, asize, afile->offset,
355 AFS_UIOSYS, IO_UNIT, &afs_osi_cred, &resid);
359 code = asize - resid;
360 afile->offset += code;
361 osi_DisableAtimes(afile->vnode);
363 afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, resid,
364 ICL_TYPE_INT32, code);
370 /* Generic write interface */
372 afs_osi_Write(register struct osi_file *afile, afs_int32 offset, void *aptr,
375 struct AFS_UCRED *oldCred;
377 register afs_int32 code;
378 #ifdef AFS_DARWIN80_ENV
381 AFS_STATCNT(osi_Write);
383 osi_Panic("afs_osi_Write called with null param");
385 afile->offset = offset;
388 #ifdef AFS_DARWIN80_ENV
389 uio=uio_create(1, afile->offset, AFS_UIOSYS, UIO_WRITE);
390 uio_addiov(uio, CAST_USER_ADDR_T(aptr), asize);
391 code = VNOP_WRITE(afile->vnode, uio, IO_UNIT, afs_osi_ctxtp);
392 resid = AFS_UIO_RESID(uio);
396 gop_rdwr(UIO_WRITE, afile->vnode, (caddr_t) aptr, asize,
397 afile->offset, AFS_UIOSYS, IO_UNIT, &afs_osi_cred,
403 code = asize - resid;
404 afile->offset += code;
409 (*afile->proc) (afile, code);
419 shutdown_osifile(void)
421 extern int afs_cold_shutdown;
423 AFS_STATCNT(shutdown_osifile);
424 if (afs_cold_shutdown) {
425 afs_osicred_initialized = 0;