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 vfs *afs_cacheVfsp;
29 /* Support for UFS and VXFS caches. The assumption here is that the size of
30 * a cache file also does not exceed 32 bits.
33 /* Initialized in osi_InitCacheFSType(). Used to determine inode type. */
34 int afs_CacheFSType = -1;
36 /* pointer to VXFS routine to access vnodes by inode number */
37 int (*vxfs_vx_vp_byino) ();
39 /* Initialize the cache operations. Called while initializing cache files. */
41 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)
70 ("afs_InitDualFSCacheOps: modlookup(vx_vp_byino) failed");
72 afs_CacheFSType = AFS_SUN_VXFS_CACHE;
76 afs_CacheFSType = AFS_SUN_UFS_CACHE;
81 VnodeToIno(vnode_t * vp)
86 vattr.va_mask = AT_FSID | AT_NODEID; /* quick return using this mask. */
88 code = VOP_GETATTR(vp, &vattr, 0, &afs_osi_cred, NULL);
90 code = VOP_GETATTR(vp, &vattr, 0, &afs_osi_cred);
93 osi_Panic("VnodeToIno");
95 return vattr.va_nodeid;
99 VnodeToDev(vnode_t * vp)
104 vattr.va_mask = AT_FSID | AT_NODEID; /* quick return using this mask. */
106 #ifdef AFS_SUN511_ENV
107 code = VOP_GETATTR(vp, &vattr, 0, &afs_osi_cred, NULL);
109 code = VOP_GETATTR(vp, &vattr, 0, &afs_osi_cred);
113 osi_Panic("VnodeToDev");
115 return (dev_t) vattr.va_fsid;
119 VnodeToSize(vnode_t * vp)
125 * We lock xosi in osi_Stat, so we probably should
126 * lock it here too - RWH.
128 ObtainWriteLock(&afs_xosi, 578);
129 vattr.va_mask = AT_SIZE;
131 #ifdef AFS_SUN511_ENV
132 code = VOP_GETATTR(vp, &vattr, 0, &afs_osi_cred, NULL);
134 code = VOP_GETATTR(vp, &vattr, 0, &afs_osi_cred);
138 osi_Panic("VnodeToSize");
140 ReleaseWriteLock(&afs_xosi);
141 return (afs_int32) (vattr.va_size);
145 osi_VxfsOpen(afs_dcache_id_t *ainode)
148 struct osi_file *afile = NULL;
151 afile = (struct osi_file *)osi_AllocSmallSpace(sizeof(struct osi_file));
153 code = (*vxfs_vx_vp_byino) (afs_cacheVfsp, &vp, (unsigned int)ainode->ufs);
156 osi_FreeSmallSpace(afile);
157 osi_Panic("VxfsOpen: vx_vp_byino failed");
160 afile->size = VnodeToSize(afile->vnode);
162 afile->proc = (int (*)())0;
163 return (void *)afile;
165 #endif /* AFS_HAVE_VXFS */
168 osi_UfsOpen(afs_dcache_id_t *ainode)
170 #ifdef AFS_CACHE_VNODE_PATH
175 struct osi_file *afile = NULL;
178 #ifdef AFS_CACHE_VNODE_PATH
180 struct pathname lookpn;
182 struct osi_stat tstat;
183 afile = (struct osi_file *)osi_AllocSmallSpace(sizeof(struct osi_file));
187 * AFS_CACHE_VNODE_PATH can be used with any file system, including ZFS or tmpfs.
188 * The ainode is not an inode number but a path.
190 #ifdef AFS_CACHE_VNODE_PATH
191 /* Can not use vn_open or lookupname, they use user's CRED()
192 * We need to run as root So must use low level lookuppnvp
193 * assume fname starts with /
196 code = pn_get_buf(ainode->ufs, AFS_UIOSYS, &lookpn, namebuf, sizeof(namebuf));
198 osi_Panic("UfsOpen: pn_get_buf failed %ld %s", code, ainode->ufs);
200 VN_HOLD(rootdir); /* released in loopuppnvp */
201 code = lookuppnvp(&lookpn, NULL, FOLLOW, NULL, &vp,
202 rootdir, rootdir, &afs_osi_cred);
204 osi_Panic("UfsOpen: lookuppnvp failed %ld %s", code, ainode->ufs);
206 #ifdef AFS_SUN511_ENV
207 code = VOP_OPEN(&vp, FREAD|FWRITE, &afs_osi_cred, NULL);
209 code = VOP_OPEN(&vp, FREAD|FWRITE, &afs_osi_cred);
213 osi_Panic("UfsOpen: VOP_OPEN failed %ld %s", code, ainode->ufs);
217 igetinode(afs_cacheVfsp, (dev_t) cacheDev.dev, ainode->ufs, &ip,
222 osi_FreeSmallSpace(afile);
223 osi_Panic("UfsOpen: igetinode failed %ld %s", code, ainode->ufs);
225 #ifdef AFS_CACHE_VNODE_PATH
227 code = afs_osi_Stat(afile, &tstat);
228 afile->size = tstat.size;
230 afile->vnode = ITOV(ip);
231 afile->size = VTOI(afile->vnode)->i_size;
234 afile->proc = (int (*)())0;
235 return (void *)afile;
239 * In Solaris 7 we use 64 bit inode numbers
242 osi_UFSOpen(afs_dcache_id_t *ainode)
244 extern int cacheDiskType;
245 AFS_STATCNT(osi_UFSOpen);
246 if (cacheDiskType != AFS_FCACHE_TYPE_UFS) {
247 osi_Panic("UFSOpen called for non-UFS cache\n");
249 if (!afs_osicred_initialized) {
250 /* valid for alpha_osf, SunOS, Ultrix */
251 memset(&afs_osi_cred, 0, sizeof(afs_ucred_t));
252 crhold(&afs_osi_cred); /* don't let it evaporate, since it is static */
253 afs_osicred_initialized = 1;
256 if (afs_CacheFSType == AFS_SUN_VXFS_CACHE)
257 return osi_VxfsOpen(ainode);
259 return osi_UfsOpen(ainode);
263 afs_osi_Stat(struct osi_file *afile, struct osi_stat *astat)
267 AFS_STATCNT(osi_Stat);
268 ObtainWriteLock(&afs_xosi, 320);
269 /* Ufs doesn't seem to care about the flags so we pass 0 for now */
270 tvattr.va_mask = AT_ALL;
272 #ifdef AFS_SUN511_ENV
273 code = VOP_GETATTR(afile->vnode, &tvattr, 0, &afs_osi_cred, NULL);
275 code = VOP_GETATTR(afile->vnode, &tvattr, 0, &afs_osi_cred);
279 astat->size = tvattr.va_size;
280 astat->mtime = tvattr.va_mtime.tv_sec;
281 astat->atime = tvattr.va_atime.tv_sec;
283 ReleaseWriteLock(&afs_xosi);
288 osi_UFSClose(struct osi_file *afile)
290 AFS_STATCNT(osi_Close);
292 AFS_RELE(afile->vnode);
295 osi_FreeSmallSpace(afile);
300 osi_UFSTruncate(struct osi_file *afile, afs_int32 asize)
302 afs_ucred_t *oldCred;
305 struct osi_stat tstat;
306 AFS_STATCNT(osi_Truncate);
308 /* This routine only shrinks files, and most systems
309 * have very slow truncates, even when the file is already
310 * small enough. Check now and save some time.
312 code = afs_osi_Stat(afile, &tstat);
313 if (code || tstat.size <= asize)
315 ObtainWriteLock(&afs_xosi, 321);
316 tvattr.va_mask = AT_SIZE;
317 tvattr.va_size = asize;
319 * The only time a flag is used (ATTR_UTIME) is when we're changing the time
322 #ifdef AFS_SUN510_ENV
326 code = VOP_SETATTR(afile->vnode, &tvattr, 0, &afs_osi_cred, &ct);
329 code = VOP_SETATTR(afile->vnode, &tvattr, 0, &afs_osi_cred);
332 ReleaseWriteLock(&afs_xosi);
337 osi_DisableAtimes(struct vnode *avp)
339 if (afs_CacheFSType == AFS_SUN_UFS_CACHE) {
340 #ifndef AFS_CACHE_VNODE_PATH
341 struct inode *ip = VTOI(avp);
342 rw_enter(&ip->i_contents, RW_READER);
343 mutex_enter(&ip->i_tlock);
345 mutex_exit(&ip->i_tlock);
346 rw_exit(&ip->i_contents);
352 /* Generic read interface */
354 afs_osi_Read(struct osi_file *afile, int offset, void *aptr,
357 afs_ucred_t *oldCred;
358 #if defined(AFS_SUN57_ENV)
365 AFS_STATCNT(osi_Read);
368 * If the osi_file passed in is NULL, panic only if AFS is not shutting
369 * down. No point in crashing when we are already shutting down
372 if (!afs_shuttingdown)
373 osi_Panic("osi_Read called with null param");
379 afile->offset = offset;
382 gop_rdwr(UIO_READ, afile->vnode, (caddr_t) aptr, asize, afile->offset,
383 AFS_UIOSYS, 0, 0, &afs_osi_cred, &resid);
386 code = asize - resid;
387 afile->offset += code;
388 osi_DisableAtimes(afile->vnode);
390 afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, resid,
391 ICL_TYPE_INT32, code);
397 /* Generic write interface */
399 afs_osi_Write(struct osi_file *afile, afs_int32 offset, void *aptr,
402 afs_ucred_t *oldCred;
403 #if defined(AFS_SUN57_ENV)
409 AFS_STATCNT(osi_Write);
411 osi_Panic("afs_osi_Write called with null param");
413 afile->offset = offset;
416 gop_rdwr(UIO_WRITE, afile->vnode, (caddr_t) aptr, asize,
417 afile->offset, AFS_UIOSYS, 0, RLIM64_INFINITY, &afs_osi_cred,
421 code = asize - resid;
422 afile->offset += code;
427 (*afile->proc) (afile, code);
433 /* This work should be handled by physstrat in ca/machdep.c.
434 This routine written from the RT NFS port strategy routine.
435 It has been generalized a bit, but should still be pretty clear. */
437 afs_osi_MapStrategy(int (*aproc) (), struct buf *bp)
439 afs_int32 returnCode;
441 AFS_STATCNT(osi_MapStrategy);
442 returnCode = (*aproc) (bp);
450 shutdown_osifile(void)
452 AFS_STATCNT(shutdown_osifile);
453 if (afs_cold_shutdown) {
454 afs_osicred_initialized = 0;