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"
15 #include "afs/sysincludes.h" /* Standard vendor system headers */
16 #include "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(afs_int32 ainode)
131 register struct osi_file *afile = NULL;
134 afile = (struct osi_file *) osi_AllocSmallSpace(sizeof(struct osi_file));
136 code = (*vxfs_vx_vp_byino)(afs_cacheVfsp, &vp, (unsigned int)ainode);
139 osi_FreeSmallSpace(afile);
140 osi_Panic("VxfsOpen: vx_vp_byino failed");
143 afile->size = VnodeToSize(afile->vnode);
145 afile->proc = (int (*)()) 0;
146 afile->inum = ainode; /* for hint validity checking */
147 return (void *)afile;
149 #endif /* AFS_HAVE_VXFS */
151 #if defined(AFS_SUN57_64BIT_ENV)
152 void *osi_UfsOpen(ino_t ainode)
154 void *osi_UfsOpen(afs_int32 ainode)
158 register struct osi_file *afile = NULL;
161 afile = (struct osi_file *) osi_AllocSmallSpace(sizeof(struct osi_file));
163 code = igetinode(afs_cacheVfsp, (dev_t) cacheDev.dev, (ino_t)ainode, &ip, CRED(),&dummy);
166 osi_FreeSmallSpace(afile);
167 osi_Panic("UfsOpen: igetinode failed");
169 afile->vnode = ITOV(ip);
170 afile->size = VTOI(afile->vnode)->i_size;
172 afile->proc = (int (*)()) 0;
173 afile->inum = ainode; /* for hint validity checking */
174 return (void *)afile;
178 * In Solaris 7 we use 64 bit inode numbers
180 #if defined(AFS_SUN57_64BIT_ENV)
181 void *osi_UFSOpen(ino_t ainode)
183 void *osi_UFSOpen(afs_int32 ainode)
186 extern int cacheDiskType;
187 AFS_STATCNT(osi_UFSOpen);
188 if(cacheDiskType != AFS_FCACHE_TYPE_UFS) {
189 osi_Panic("UFSOpen called for non-UFS cache\n");
191 if (!afs_osicred_initialized) {
192 /* valid for alpha_osf, SunOS, Ultrix */
193 memset((char *)&afs_osi_cred, 0, sizeof(struct AFS_UCRED));
194 crhold(&afs_osi_cred); /* don't let it evaporate, since it is static */
195 afs_osicred_initialized = 1;
198 if (afs_CacheFSType == AFS_SUN_VXFS_CACHE)
199 return osi_VxfsOpen(ainode);
201 return osi_UfsOpen(ainode);
204 int afs_osi_Stat(register struct osi_file *afile, 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);
225 int osi_UFSClose(register struct osi_file *afile)
227 AFS_STATCNT(osi_Close);
229 AFS_RELE(afile->vnode);
232 osi_FreeSmallSpace(afile);
236 int osi_UFSTruncate(register struct osi_file *afile, afs_int32 asize)
238 struct AFS_UCRED *oldCred;
240 register afs_int32 code;
241 struct osi_stat tstat;
242 AFS_STATCNT(osi_Truncate);
244 /* This routine only shrinks files, and most systems
245 * have very slow truncates, even when the file is already
246 * small enough. Check now and save some time.
248 code = afs_osi_Stat(afile, &tstat);
249 if (code || tstat.size <= asize) return code;
250 MObtainWriteLock(&afs_xosi,321);
251 tvattr.va_mask = AT_SIZE;
252 tvattr.va_size = asize;
254 * The only time a flag is used (ATTR_UTIME) is when we're changing the time
257 code = VOP_SETATTR(afile->vnode, &tvattr, 0, &afs_osi_cred);
259 MReleaseWriteLock(&afs_xosi);
263 void osi_DisableAtimes(struct vnode *avp)
265 if (afs_CacheFSType == AFS_SUN_UFS_CACHE) {
266 struct inode *ip = VTOI(avp);
267 rw_enter(&ip->i_contents, RW_READER);
268 mutex_enter(&ip->i_tlock);
270 mutex_exit(&ip->i_tlock);
271 rw_exit(&ip->i_contents);
276 /* Generic read interface */
277 int afs_osi_Read(register struct osi_file *afile, int offset, void *aptr, afs_int32 asize)
279 struct AFS_UCRED *oldCred;
280 #if defined(AFS_SUN57_ENV)
285 register afs_int32 code;
286 register afs_int32 cnt1=0;
287 AFS_STATCNT(osi_Read);
290 * If the osi_file passed in is NULL, panic only if AFS is not shutting
291 * down. No point in crashing when we are already shutting down
294 if ( !afs_shuttingdown )
295 osi_Panic("osi_Read called with null param");
300 if (offset != -1) afile->offset = offset;
302 code = gop_rdwr(UIO_READ, afile->vnode, (caddr_t) aptr, asize, afile->offset,
303 AFS_UIOSYS, 0, 0, &afs_osi_cred, &resid);
306 code = asize - resid;
307 afile->offset += code;
308 osi_DisableAtimes(afile->vnode);
311 afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, resid,
312 ICL_TYPE_INT32, code);
318 /* Generic write interface */
319 int afs_osi_Write(register struct osi_file *afile, afs_int32 offset, void *aptr, afs_int32 asize)
321 struct AFS_UCRED *oldCred;
322 #if defined(AFS_SUN57_ENV)
327 register afs_int32 code;
328 AFS_STATCNT(osi_Write);
330 osi_Panic("afs_osi_Write called with null param");
331 if (offset != -1) afile->offset = offset;
333 code = gop_rdwr(UIO_WRITE, afile->vnode, (caddr_t) aptr, asize,
334 afile->offset, AFS_UIOSYS, 0, RLIM64_INFINITY,
335 &afs_osi_cred, &resid);
338 code = asize - resid;
339 afile->offset += code;
345 (*afile->proc)(afile, code);
351 /* This work should be handled by physstrat in ca/machdep.c.
352 This routine written from the RT NFS port strategy routine.
353 It has been generalized a bit, but should still be pretty clear. */
354 int afs_osi_MapStrategy(int (*aproc)(), register struct buf *bp)
356 afs_int32 returnCode;
358 AFS_STATCNT(osi_MapStrategy);
359 returnCode = (*aproc) (bp);
366 void shutdown_osifile(void)
368 extern int afs_cold_shutdown;
370 AFS_STATCNT(shutdown_osifile);
371 if (afs_cold_shutdown) {
372 afs_osicred_initialized = 0;