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 vfs *afs_cacheVfsp;
31 /* Support for UFS and VXFS caches. The assumption here is that the size of
32 * a cache file also does not exceed 32 bits.
35 /* Initialized in osi_InitCacheFSType(). Used to determine inode type. */
36 int afs_CacheFSType = -1;
38 /* pointer to VXFS routine to access vnodes by inode number */
39 int (*vxfs_vx_vp_byino) ();
41 /* Initialize the cache operations. Called while initializing cache files. */
43 afs_InitDualFSCacheOps(struct vnode *vp)
46 static int inited = 0;
49 struct statvfs64 vfst;
50 #else /* AFS_SUN56_ENV */
52 #endif /* AFS_SUN56_ENV */
63 osi_Panic("afs_InitDualFSCacheOps: vp->v_vfsp is NULL");
64 code = VFS_STATVFS(vfsp, &vfst);
66 osi_Panic("afs_InitDualFSCacheOps: statvfs failed");
68 if (strcmp(vfst.f_basetype, "vxfs") == 0) {
69 vxfs_vx_vp_byino = (int (*)())modlookup("vxfs", "vx_vp_byino");
70 if (vxfs_vx_vp_byino == NULL)
72 ("afs_InitDualFSCacheOps: modlookup(vx_vp_byino) failed");
74 afs_CacheFSType = AFS_SUN_VXFS_CACHE;
78 afs_CacheFSType = AFS_SUN_UFS_CACHE;
83 VnodeToIno(vnode_t * vp)
88 vattr.va_mask = AT_FSID | AT_NODEID; /* quick return using this mask. */
90 code = VOP_GETATTR(vp, &vattr, 0, &afs_osi_cred, NULL);
92 code = VOP_GETATTR(vp, &vattr, 0, &afs_osi_cred);
95 osi_Panic("VnodeToIno");
97 return vattr.va_nodeid;
101 VnodeToDev(vnode_t * vp)
106 vattr.va_mask = AT_FSID | AT_NODEID; /* quick return using this mask. */
108 #ifdef AFS_SUN511_ENV
109 code = VOP_GETATTR(vp, &vattr, 0, &afs_osi_cred, NULL);
111 code = VOP_GETATTR(vp, &vattr, 0, &afs_osi_cred);
115 osi_Panic("VnodeToDev");
117 return (dev_t) vattr.va_fsid;
121 VnodeToSize(vnode_t * vp)
127 * We lock xosi in osi_Stat, so we probably should
128 * lock it here too - RWH.
130 MObtainWriteLock(&afs_xosi, 578);
131 vattr.va_mask = AT_SIZE;
133 #ifdef AFS_SUN511_ENV
134 code = VOP_GETATTR(vp, &vattr, 0, &afs_osi_cred, NULL);
136 code = VOP_GETATTR(vp, &vattr, 0, &afs_osi_cred);
140 osi_Panic("VnodeToSize");
142 MReleaseWriteLock(&afs_xosi);
143 return (afs_int32) (vattr.va_size);
147 osi_VxfsOpen(afs_int32 ainode)
150 register struct osi_file *afile = NULL;
153 afile = (struct osi_file *)osi_AllocSmallSpace(sizeof(struct osi_file));
155 code = (*vxfs_vx_vp_byino) (afs_cacheVfsp, &vp, (unsigned int)ainode);
158 osi_FreeSmallSpace(afile);
159 osi_Panic("VxfsOpen: vx_vp_byino failed");
162 afile->size = VnodeToSize(afile->vnode);
164 afile->proc = (int (*)())0;
165 afile->inum = ainode; /* for hint validity checking */
166 return (void *)afile;
168 #endif /* AFS_HAVE_VXFS */
170 #if defined(AFS_SUN57_64BIT_ENV)
172 osi_UfsOpen(ino_t ainode)
175 osi_UfsOpen(afs_int32 ainode)
178 #ifdef AFS_CACHE_VNODE_PATH
183 register struct osi_file *afile = NULL;
185 #ifdef AFS_CACHE_VNODE_PATH
189 struct pathname lookpn;
191 struct osi_stat tstat;
192 afile = (struct osi_file *)osi_AllocSmallSpace(sizeof(struct osi_file));
196 * AFS_CACHE_VNODE_PATH can be used with any file system, including ZFS or tmpfs.
197 * The ainode is not an inode number but a signed index used to generate file names.
199 #ifdef AFS_CACHE_VNODE_PATH
201 case AFS_CACHE_CELLS_INODE:
202 snprintf(fname, 1024, "%s/%s", afs_cachebasedir, "CellItems");
204 case AFS_CACHE_ITEMS_INODE:
205 snprintf(fname, 1024, "%s/%s", afs_cachebasedir, "CacheItems");
207 case AFS_CACHE_VOLUME_INODE:
208 snprintf(fname, 1024, "%s/%s", afs_cachebasedir, "VolumeItems");
211 dummy = ainode / afs_numfilesperdir;
212 snprintf(fname, 1024, "%s/D%d/V%d", afs_cachebasedir, dummy, ainode);
215 /* Can not use vn_open or lookupname, they use user's CRED()
216 * We need to run as root So must use low level lookuppnvp
217 * assume fname starts with /
220 code = pn_get_buf(fname, AFS_UIOSYS, &lookpn, namebuf, sizeof(namebuf));
222 osi_Panic("UfsOpen: pn_get_buf failed %ld %s %ld", code, fname, ainode);
224 VN_HOLD(rootdir); /* released in loopuppnvp */
225 code = lookuppnvp(&lookpn, NULL, FOLLOW, NULL, &vp,
226 rootdir, rootdir, &afs_osi_cred);
228 osi_Panic("UfsOpen: lookuppnvp failed %ld %s %ld", code, fname, ainode);
230 #ifdef AFS_SUN511_ENV
231 code = VOP_OPEN(&vp, FREAD|FWRITE, &afs_osi_cred, NULL);
233 code = VOP_OPEN(&vp, FREAD|FWRITE, &afs_osi_cred);
237 osi_Panic("UfsOpen: VOP_OPEN failed %ld %s %ld", code, fname, ainode);
241 igetinode(afs_cacheVfsp, (dev_t) cacheDev.dev, (ino_t) ainode, &ip,
246 osi_FreeSmallSpace(afile);
247 osi_Panic("UfsOpen: igetinode failed %ld %s %ld", code, fname, ainode);
249 #ifdef AFS_CACHE_VNODE_PATH
251 code = afs_osi_Stat(afile, &tstat);
252 afile->size = tstat.size;
254 afile->vnode = ITOV(ip);
255 afile->size = VTOI(afile->vnode)->i_size;
258 afile->proc = (int (*)())0;
259 afile->inum = ainode; /* for hint validity checking */
260 return (void *)afile;
264 * In Solaris 7 we use 64 bit inode numbers
266 #if defined(AFS_SUN57_64BIT_ENV)
268 osi_UFSOpen(ino_t ainode)
271 osi_UFSOpen(afs_int32 ainode)
274 extern int cacheDiskType;
275 AFS_STATCNT(osi_UFSOpen);
276 if (cacheDiskType != AFS_FCACHE_TYPE_UFS) {
277 osi_Panic("UFSOpen called for non-UFS cache\n");
279 if (!afs_osicred_initialized) {
280 /* valid for alpha_osf, SunOS, Ultrix */
281 memset((char *)&afs_osi_cred, 0, sizeof(struct AFS_UCRED));
282 crhold(&afs_osi_cred); /* don't let it evaporate, since it is static */
283 afs_osicred_initialized = 1;
286 if (afs_CacheFSType == AFS_SUN_VXFS_CACHE)
287 return osi_VxfsOpen(ainode);
289 return osi_UfsOpen(ainode);
293 afs_osi_Stat(register struct osi_file *afile, register struct osi_stat *astat)
295 register afs_int32 code;
297 AFS_STATCNT(osi_Stat);
298 MObtainWriteLock(&afs_xosi, 320);
299 /* Ufs doesn't seem to care about the flags so we pass 0 for now */
300 tvattr.va_mask = AT_ALL;
302 #ifdef AFS_SUN511_ENV
303 code = VOP_GETATTR(afile->vnode, &tvattr, 0, &afs_osi_cred, NULL);
305 code = VOP_GETATTR(afile->vnode, &tvattr, 0, &afs_osi_cred);
309 astat->size = tvattr.va_size;
310 astat->mtime = tvattr.va_mtime.tv_sec;
311 astat->atime = tvattr.va_atime.tv_sec;
313 MReleaseWriteLock(&afs_xosi);
318 osi_UFSClose(register struct osi_file *afile)
320 AFS_STATCNT(osi_Close);
322 AFS_RELE(afile->vnode);
325 osi_FreeSmallSpace(afile);
330 osi_UFSTruncate(register struct osi_file *afile, afs_int32 asize)
332 struct AFS_UCRED *oldCred;
334 register afs_int32 code;
335 struct osi_stat tstat;
336 AFS_STATCNT(osi_Truncate);
338 /* This routine only shrinks files, and most systems
339 * have very slow truncates, even when the file is already
340 * small enough. Check now and save some time.
342 code = afs_osi_Stat(afile, &tstat);
343 if (code || tstat.size <= asize)
345 MObtainWriteLock(&afs_xosi, 321);
346 tvattr.va_mask = AT_SIZE;
347 tvattr.va_size = asize;
349 * The only time a flag is used (ATTR_UTIME) is when we're changing the time
352 #ifdef AFS_SUN510_ENV
356 code = VOP_SETATTR(afile->vnode, &tvattr, 0, &afs_osi_cred, &ct);
359 code = VOP_SETATTR(afile->vnode, &tvattr, 0, &afs_osi_cred);
362 MReleaseWriteLock(&afs_xosi);
367 osi_DisableAtimes(struct vnode *avp)
369 if (afs_CacheFSType == AFS_SUN_UFS_CACHE) {
370 #ifndef AFS_CACHE_VNODE_PATH
371 struct inode *ip = VTOI(avp);
372 rw_enter(&ip->i_contents, RW_READER);
373 mutex_enter(&ip->i_tlock);
375 mutex_exit(&ip->i_tlock);
376 rw_exit(&ip->i_contents);
382 /* Generic read interface */
384 afs_osi_Read(register struct osi_file *afile, int offset, void *aptr,
387 struct AFS_UCRED *oldCred;
388 #if defined(AFS_SUN57_ENV)
393 register afs_int32 code;
394 register afs_int32 cnt1 = 0;
395 AFS_STATCNT(osi_Read);
398 * If the osi_file passed in is NULL, panic only if AFS is not shutting
399 * down. No point in crashing when we are already shutting down
402 if (!afs_shuttingdown)
403 osi_Panic("osi_Read called with null param");
409 afile->offset = offset;
412 gop_rdwr(UIO_READ, afile->vnode, (caddr_t) aptr, asize, afile->offset,
413 AFS_UIOSYS, 0, 0, &afs_osi_cred, &resid);
416 code = asize - resid;
417 afile->offset += code;
418 osi_DisableAtimes(afile->vnode);
420 afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, resid,
421 ICL_TYPE_INT32, code);
427 /* Generic write interface */
429 afs_osi_Write(register struct osi_file *afile, afs_int32 offset, void *aptr,
432 struct AFS_UCRED *oldCred;
433 #if defined(AFS_SUN57_ENV)
438 register afs_int32 code;
439 AFS_STATCNT(osi_Write);
441 osi_Panic("afs_osi_Write called with null param");
443 afile->offset = offset;
446 gop_rdwr(UIO_WRITE, afile->vnode, (caddr_t) aptr, asize,
447 afile->offset, AFS_UIOSYS, 0, RLIM64_INFINITY, &afs_osi_cred,
451 code = asize - resid;
452 afile->offset += code;
457 (*afile->proc) (afile, code);
463 /* This work should be handled by physstrat in ca/machdep.c.
464 This routine written from the RT NFS port strategy routine.
465 It has been generalized a bit, but should still be pretty clear. */
467 afs_osi_MapStrategy(int (*aproc) (), register struct buf *bp)
469 afs_int32 returnCode;
471 AFS_STATCNT(osi_MapStrategy);
472 returnCode = (*aproc) (bp);
480 shutdown_osifile(void)
482 AFS_STATCNT(shutdown_osifile);
483 if (afs_cold_shutdown) {
484 afs_osicred_initialized = 0;