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 "../afs/param.h" /* Should be always first */
11 #include <afsconfig.h>
15 #include "../afs/sysincludes.h" /* Standard vendor system headers */
16 #include "../afs/afsincludes.h" /* Afs-based standard headers */
17 #include "../afs/afs_stats.h" /* afs statistics */
18 #include "../afs/osi_inode.h"
21 int afs_osicred_initialized=0;
22 struct AFS_UCRED afs_osi_cred;
23 afs_lock_t afs_xosi; /* lock is for tvattr */
24 extern struct osi_dev cacheDev;
25 extern struct vfs *afs_cacheVfsp;
30 /* Support for UFS and VXFS caches. The assumption here is that the size of
31 * a cache file also does not exceed 32 bits.
34 /* Initialized in osi_InitCacheFSType(). Used to determine inode type. */
35 int afs_CacheFSType = -1;
37 /* pointer to VXFS routine to access vnodes by inode number */
38 int (*vxfs_vx_vp_byino)();
40 /* Initialize the cache operations. Called while initializing cache files. */
41 void afs_InitDualFSCacheOps(struct vnode *vp)
44 static int inited = 0;
47 struct statvfs64 vfst;
48 #else /* AFS_SUN56_ENV */
50 #endif /* AFS_SUN56_ENV */
61 osi_Panic("afs_InitDualFSCacheOps: vp->v_vfsp is NULL");
62 code = VFS_STATVFS(vfsp, &vfst);
64 osi_Panic("afs_InitDualFSCacheOps: statvfs failed");
66 if (strcmp(vfst.f_basetype, "vxfs") == 0) {
67 vxfs_vx_vp_byino = (int (*)())modlookup("vxfs", "vx_vp_byino");
68 if (vxfs_vx_vp_byino == NULL)
69 osi_Panic("afs_InitDualFSCacheOps: modlookup(vx_vp_byino) failed");
71 afs_CacheFSType = AFS_SUN_VXFS_CACHE;
75 afs_CacheFSType = AFS_SUN_UFS_CACHE;
79 ino_t VnodeToIno(vnode_t *vp)
84 vattr.va_mask = AT_FSID|AT_NODEID; /* quick return using this mask. */
85 code = VOP_GETATTR(vp, &vattr, 0, &afs_osi_cred);
87 osi_Panic("VnodeToIno");
89 return vattr.va_nodeid;
92 dev_t VnodeToDev(vnode_t *vp)
97 vattr.va_mask = AT_FSID|AT_NODEID; /* quick return using this mask. */
99 code = VOP_GETATTR(vp, &vattr, 0, &afs_osi_cred);
102 osi_Panic("VnodeToDev");
104 return (dev_t)vattr.va_fsid;
107 afs_int32 VnodeToSize(vnode_t *vp)
113 * We lock xosi in osi_Stat, so we probably should
114 * lock it here too - RWH.
116 MObtainWriteLock(&afs_xosi,578);
117 vattr.va_mask = AT_SIZE;
119 code = VOP_GETATTR(vp, &vattr, 0, &afs_osi_cred);
122 osi_Panic("VnodeToSize");
124 MReleaseWriteLock(&afs_xosi);
125 return (afs_int32)(vattr.va_size);
128 void *osi_VxfsOpen(ainode)
132 register struct osi_file *afile = NULL;
135 afile = (struct osi_file *) osi_AllocSmallSpace(sizeof(struct osi_file));
137 code = (*vxfs_vx_vp_byino)(afs_cacheVfsp, &vp, (unsigned int)ainode);
140 osi_FreeSmallSpace(afile);
141 osi_Panic("VxfsOpen: vx_vp_byino failed");
144 afile->size = VnodeToSize(afile->vnode);
146 afile->proc = (int (*)()) 0;
147 afile->inum = ainode; /* for hint validity checking */
148 return (void *)afile;
150 #endif /* AFS_HAVE_VXFS */
152 void *osi_UfsOpen(ainode)
156 register struct osi_file *afile = NULL;
159 afile = (struct osi_file *) osi_AllocSmallSpace(sizeof(struct osi_file));
161 code = igetinode(afs_cacheVfsp, (dev_t) cacheDev.dev, (ino_t)ainode, &ip, CRED(),&dummy);
164 osi_FreeSmallSpace(afile);
165 osi_Panic("UfsOpen: igetinode failed");
167 afile->vnode = ITOV(ip);
168 afile->size = VTOI(afile->vnode)->i_size;
170 afile->proc = (int (*)()) 0;
171 afile->inum = ainode; /* for hint validity checking */
172 return (void *)afile;
176 * In Solaris 7 we use 64 bit inode numbers
178 #if defined(AFS_SUN57_64BIT_ENV)
179 void *osi_UFSOpen(ino_t ainode)
181 void *osi_UFSOpen(ainode)
185 extern int cacheDiskType;
186 AFS_STATCNT(osi_UFSOpen);
187 if(cacheDiskType != AFS_FCACHE_TYPE_UFS) {
188 osi_Panic("UFSOpen called for non-UFS cache\n");
190 if (!afs_osicred_initialized) {
191 /* valid for alpha_osf, SunOS, Ultrix */
192 bzero((char *)&afs_osi_cred, sizeof(struct AFS_UCRED));
193 crhold(&afs_osi_cred); /* don't let it evaporate, since it is static */
194 afs_osicred_initialized = 1;
197 if (afs_CacheFSType == AFS_SUN_VXFS_CACHE)
198 return osi_VxfsOpen(ainode);
200 return osi_UfsOpen(ainode);
203 afs_osi_Stat(afile, astat)
204 register struct osi_file *afile;
205 register struct osi_stat *astat; {
206 register afs_int32 code;
208 AFS_STATCNT(osi_Stat);
209 MObtainWriteLock(&afs_xosi,320);
210 /* Ufs doesn't seem to care about the flags so we pass 0 for now */
211 tvattr.va_mask = AT_ALL;
213 code = VOP_GETATTR(afile->vnode, &tvattr, 0, &afs_osi_cred);
216 astat->size = tvattr.va_size;
217 astat->blksize = tvattr.va_blksize;
218 astat->mtime = tvattr.va_mtime.tv_sec;
219 astat->atime = tvattr.va_atime.tv_sec;
221 MReleaseWriteLock(&afs_xosi);
226 register struct osi_file *afile;
228 AFS_STATCNT(osi_Close);
230 AFS_RELE(afile->vnode);
233 osi_FreeSmallSpace(afile);
237 osi_UFSTruncate(afile, asize)
238 register struct osi_file *afile;
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) return code;
252 MObtainWriteLock(&afs_xosi,321);
253 tvattr.va_mask = AT_SIZE;
254 tvattr.va_size = asize;
256 * The only time a flag is used (ATTR_UTIME) is when we're changing the time
259 code = VOP_SETATTR(afile->vnode, &tvattr, 0, &afs_osi_cred);
261 MReleaseWriteLock(&afs_xosi);
265 void osi_DisableAtimes(avp)
268 if (afs_CacheFSType == AFS_SUN_UFS_CACHE) {
269 struct inode *ip = VTOI(avp);
270 rw_enter(&ip->i_contents, RW_READER);
271 mutex_enter(&ip->i_tlock);
273 mutex_exit(&ip->i_tlock);
274 rw_exit(&ip->i_contents);
279 /* Generic read interface */
280 afs_osi_Read(afile, offset, aptr, asize)
281 register struct osi_file *afile;
285 struct AFS_UCRED *oldCred;
286 #if defined(AFS_SUN57_ENV)
291 register afs_int32 code;
292 register afs_int32 cnt1=0;
293 AFS_STATCNT(osi_Read);
296 * If the osi_file passed in is NULL, panic only if AFS is not shutting
297 * down. No point in crashing when we are already shutting down
300 if ( !afs_shuttingdown )
301 osi_Panic("osi_Read called with null param");
306 if (offset != -1) afile->offset = offset;
308 code = gop_rdwr(UIO_READ, afile->vnode, (caddr_t) aptr, asize, afile->offset,
309 AFS_UIOSYS, 0, 0, &afs_osi_cred, &resid);
312 code = asize - resid;
313 afile->offset += code;
314 osi_DisableAtimes(afile->vnode);
317 afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, resid,
318 ICL_TYPE_INT32, code);
324 /* Generic write interface */
325 afs_osi_Write(afile, offset, aptr, asize)
326 register struct osi_file *afile;
330 struct AFS_UCRED *oldCred;
331 #if defined(AFS_SUN57_ENV)
336 register afs_int32 code;
337 AFS_STATCNT(osi_Write);
339 osi_Panic("afs_osi_Write called with null param");
340 if (offset != -1) afile->offset = offset;
342 code = gop_rdwr(UIO_WRITE, afile->vnode, (caddr_t) aptr, asize, afile->offset,
343 AFS_UIOSYS, 0, (u.u_rlimit[RLIMIT_FSIZE].rlim_cur), &afs_osi_cred, &resid);
346 code = asize - resid;
347 afile->offset += code;
353 (*afile->proc)(afile, code);
359 /* This work should be handled by physstrat in ca/machdep.c.
360 This routine written from the RT NFS port strategy routine.
361 It has been generalized a bit, but should still be pretty clear. */
362 int afs_osi_MapStrategy(aproc, bp)
364 register struct buf *bp;
366 afs_int32 returnCode;
368 AFS_STATCNT(osi_MapStrategy);
369 returnCode = (*aproc) (bp);
379 extern int afs_cold_shutdown;
381 AFS_STATCNT(shutdown_osifile);
382 if (afs_cold_shutdown) {
383 afs_osicred_initialized = 0;