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 MObtainWriteLock(&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 MReleaseWriteLock(&afs_xosi);
141 return (afs_int32) (vattr.va_size);
145 osi_VxfsOpen(afs_dcache_id_t *ainode)
148 register 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 afile->inum = ainode->ufs; /* for hint validity checking */
164 return (void *)afile;
166 #endif /* AFS_HAVE_VXFS */
169 osi_UfsOpen(afs_dcache_id_t *ainode)
171 #ifdef AFS_CACHE_VNODE_PATH
176 register struct osi_file *afile = NULL;
180 #ifdef AFS_CACHE_VNODE_PATH
182 struct pathname lookpn;
184 struct osi_stat tstat;
185 afile = (struct osi_file *)osi_AllocSmallSpace(sizeof(struct osi_file));
189 * AFS_CACHE_VNODE_PATH can be used with any file system, including ZFS or tmpfs.
190 * The ainode is not an inode number but a signed index used to generate file names.
192 #ifdef AFS_CACHE_VNODE_PATH
193 switch (ainode->ufs) {
194 case AFS_CACHE_CELLS_INODE:
195 snprintf(fname, 1024, "%s/%s", afs_cachebasedir, "CellItems");
197 case AFS_CACHE_ITEMS_INODE:
198 snprintf(fname, 1024, "%s/%s", afs_cachebasedir, "CacheItems");
200 case AFS_CACHE_VOLUME_INODE:
201 snprintf(fname, 1024, "%s/%s", afs_cachebasedir, "VolumeItems");
204 dummy = ainode->ufs / afs_numfilesperdir;
205 snprintf(fname, 1024, "%s/D%d/V%d", afs_cachebasedir, dummy, ainode->ufs);
208 /* Can not use vn_open or lookupname, they use user's CRED()
209 * We need to run as root So must use low level lookuppnvp
210 * assume fname starts with /
213 code = pn_get_buf(fname, AFS_UIOSYS, &lookpn, namebuf, sizeof(namebuf));
215 osi_Panic("UfsOpen: pn_get_buf failed %ld %s %ld", code, fname, ainode->ufs);
217 VN_HOLD(rootdir); /* released in loopuppnvp */
218 code = lookuppnvp(&lookpn, NULL, FOLLOW, NULL, &vp,
219 rootdir, rootdir, &afs_osi_cred);
221 osi_Panic("UfsOpen: lookuppnvp failed %ld %s %ld", code, fname, ainode->ufs);
223 #ifdef AFS_SUN511_ENV
224 code = VOP_OPEN(&vp, FREAD|FWRITE, &afs_osi_cred, NULL);
226 code = VOP_OPEN(&vp, FREAD|FWRITE, &afs_osi_cred);
230 osi_Panic("UfsOpen: VOP_OPEN failed %ld %s %ld", code, fname, ainode->ufs);
234 igetinode(afs_cacheVfsp, (dev_t) cacheDev.dev, ainode->ufs, &ip,
239 osi_FreeSmallSpace(afile);
240 osi_Panic("UfsOpen: igetinode failed %ld %s %ld", code, fname, ainode->ufs);
242 #ifdef AFS_CACHE_VNODE_PATH
244 code = afs_osi_Stat(afile, &tstat);
245 afile->size = tstat.size;
247 afile->vnode = ITOV(ip);
248 afile->size = VTOI(afile->vnode)->i_size;
251 afile->proc = (int (*)())0;
252 afile->inum = ainode->ufs; /* for hint validity checking */
253 return (void *)afile;
257 * In Solaris 7 we use 64 bit inode numbers
260 osi_UFSOpen(afs_dcache_id_t *ainode)
262 extern int cacheDiskType;
263 AFS_STATCNT(osi_UFSOpen);
264 if (cacheDiskType != AFS_FCACHE_TYPE_UFS) {
265 osi_Panic("UFSOpen called for non-UFS cache\n");
267 if (!afs_osicred_initialized) {
268 /* valid for alpha_osf, SunOS, Ultrix */
269 memset((char *)&afs_osi_cred, 0, sizeof(afs_ucred_t));
270 crhold(&afs_osi_cred); /* don't let it evaporate, since it is static */
271 afs_osicred_initialized = 1;
274 if (afs_CacheFSType == AFS_SUN_VXFS_CACHE)
275 return osi_VxfsOpen(ainode);
277 return osi_UfsOpen(ainode);
281 afs_osi_Stat(register struct osi_file *afile, register struct osi_stat *astat)
283 register afs_int32 code;
285 AFS_STATCNT(osi_Stat);
286 MObtainWriteLock(&afs_xosi, 320);
287 /* Ufs doesn't seem to care about the flags so we pass 0 for now */
288 tvattr.va_mask = AT_ALL;
290 #ifdef AFS_SUN511_ENV
291 code = VOP_GETATTR(afile->vnode, &tvattr, 0, &afs_osi_cred, NULL);
293 code = VOP_GETATTR(afile->vnode, &tvattr, 0, &afs_osi_cred);
297 astat->size = tvattr.va_size;
298 astat->mtime = tvattr.va_mtime.tv_sec;
299 astat->atime = tvattr.va_atime.tv_sec;
301 MReleaseWriteLock(&afs_xosi);
306 osi_UFSClose(register struct osi_file *afile)
308 AFS_STATCNT(osi_Close);
310 AFS_RELE(afile->vnode);
313 osi_FreeSmallSpace(afile);
318 osi_UFSTruncate(register struct osi_file *afile, afs_int32 asize)
320 afs_ucred_t *oldCred;
322 register afs_int32 code;
323 struct osi_stat tstat;
324 AFS_STATCNT(osi_Truncate);
326 /* This routine only shrinks files, and most systems
327 * have very slow truncates, even when the file is already
328 * small enough. Check now and save some time.
330 code = afs_osi_Stat(afile, &tstat);
331 if (code || tstat.size <= asize)
333 MObtainWriteLock(&afs_xosi, 321);
334 tvattr.va_mask = AT_SIZE;
335 tvattr.va_size = asize;
337 * The only time a flag is used (ATTR_UTIME) is when we're changing the time
340 #ifdef AFS_SUN510_ENV
344 code = VOP_SETATTR(afile->vnode, &tvattr, 0, &afs_osi_cred, &ct);
347 code = VOP_SETATTR(afile->vnode, &tvattr, 0, &afs_osi_cred);
350 MReleaseWriteLock(&afs_xosi);
355 osi_DisableAtimes(struct vnode *avp)
357 if (afs_CacheFSType == AFS_SUN_UFS_CACHE) {
358 #ifndef AFS_CACHE_VNODE_PATH
359 struct inode *ip = VTOI(avp);
360 rw_enter(&ip->i_contents, RW_READER);
361 mutex_enter(&ip->i_tlock);
363 mutex_exit(&ip->i_tlock);
364 rw_exit(&ip->i_contents);
370 /* Generic read interface */
372 afs_osi_Read(register struct osi_file *afile, int offset, void *aptr,
375 afs_ucred_t *oldCred;
376 #if defined(AFS_SUN57_ENV)
381 register afs_int32 code;
382 register afs_int32 cnt1 = 0;
383 AFS_STATCNT(osi_Read);
386 * If the osi_file passed in is NULL, panic only if AFS is not shutting
387 * down. No point in crashing when we are already shutting down
390 if (!afs_shuttingdown)
391 osi_Panic("osi_Read called with null param");
397 afile->offset = offset;
400 gop_rdwr(UIO_READ, afile->vnode, (caddr_t) aptr, asize, afile->offset,
401 AFS_UIOSYS, 0, 0, &afs_osi_cred, &resid);
404 code = asize - resid;
405 afile->offset += code;
406 osi_DisableAtimes(afile->vnode);
408 afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, resid,
409 ICL_TYPE_INT32, code);
415 /* Generic write interface */
417 afs_osi_Write(register struct osi_file *afile, afs_int32 offset, void *aptr,
420 afs_ucred_t *oldCred;
421 #if defined(AFS_SUN57_ENV)
426 register afs_int32 code;
427 AFS_STATCNT(osi_Write);
429 osi_Panic("afs_osi_Write called with null param");
431 afile->offset = offset;
434 gop_rdwr(UIO_WRITE, afile->vnode, (caddr_t) aptr, asize,
435 afile->offset, AFS_UIOSYS, 0, RLIM64_INFINITY, &afs_osi_cred,
439 code = asize - resid;
440 afile->offset += code;
445 (*afile->proc) (afile, code);
451 /* This work should be handled by physstrat in ca/machdep.c.
452 This routine written from the RT NFS port strategy routine.
453 It has been generalized a bit, but should still be pretty clear. */
455 afs_osi_MapStrategy(int (*aproc) (), register struct buf *bp)
457 afs_int32 returnCode;
459 AFS_STATCNT(osi_MapStrategy);
460 returnCode = (*aproc) (bp);
468 shutdown_osifile(void)
470 AFS_STATCNT(shutdown_osifile);
471 if (afs_cold_shutdown) {
472 afs_osicred_initialized = 0;