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 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 return (void *)afile;
165 #endif /* AFS_HAVE_VXFS */
168 osi_UfsOpen(afs_dcache_id_t *ainode)
170 #ifdef AFS_CACHE_VNODE_PATH
175 register struct osi_file *afile = NULL;
179 #ifdef AFS_CACHE_VNODE_PATH
181 struct pathname lookpn;
183 struct osi_stat tstat;
184 afile = (struct osi_file *)osi_AllocSmallSpace(sizeof(struct osi_file));
188 * AFS_CACHE_VNODE_PATH can be used with any file system, including ZFS or tmpfs.
189 * The ainode is not an inode number but a signed index used to generate file names.
191 #ifdef AFS_CACHE_VNODE_PATH
192 switch (ainode->ufs) {
193 case AFS_CACHE_CELLS_INODE:
194 snprintf(fname, 1024, "%s/%s", afs_cachebasedir, "CellItems");
196 case AFS_CACHE_ITEMS_INODE:
197 snprintf(fname, 1024, "%s/%s", afs_cachebasedir, "CacheItems");
199 case AFS_CACHE_VOLUME_INODE:
200 snprintf(fname, 1024, "%s/%s", afs_cachebasedir, "VolumeItems");
203 dummy = ainode->ufs / afs_numfilesperdir;
204 snprintf(fname, 1024, "%s/D%d/V%d", afs_cachebasedir, dummy, ainode->ufs);
207 /* Can not use vn_open or lookupname, they use user's CRED()
208 * We need to run as root So must use low level lookuppnvp
209 * assume fname starts with /
212 code = pn_get_buf(fname, AFS_UIOSYS, &lookpn, namebuf, sizeof(namebuf));
214 osi_Panic("UfsOpen: pn_get_buf failed %ld %s %ld", code, fname, ainode->ufs);
216 VN_HOLD(rootdir); /* released in loopuppnvp */
217 code = lookuppnvp(&lookpn, NULL, FOLLOW, NULL, &vp,
218 rootdir, rootdir, &afs_osi_cred);
220 osi_Panic("UfsOpen: lookuppnvp failed %ld %s %ld", code, fname, ainode->ufs);
222 #ifdef AFS_SUN511_ENV
223 code = VOP_OPEN(&vp, FREAD|FWRITE, &afs_osi_cred, NULL);
225 code = VOP_OPEN(&vp, FREAD|FWRITE, &afs_osi_cred);
229 osi_Panic("UfsOpen: VOP_OPEN failed %ld %s %ld", code, fname, ainode->ufs);
233 igetinode(afs_cacheVfsp, (dev_t) cacheDev.dev, ainode->ufs, &ip,
238 osi_FreeSmallSpace(afile);
239 osi_Panic("UfsOpen: igetinode failed %ld %s %ld", code, fname, ainode->ufs);
241 #ifdef AFS_CACHE_VNODE_PATH
243 code = afs_osi_Stat(afile, &tstat);
244 afile->size = tstat.size;
246 afile->vnode = ITOV(ip);
247 afile->size = VTOI(afile->vnode)->i_size;
250 afile->proc = (int (*)())0;
251 return (void *)afile;
255 * In Solaris 7 we use 64 bit inode numbers
258 osi_UFSOpen(afs_dcache_id_t *ainode)
260 extern int cacheDiskType;
261 AFS_STATCNT(osi_UFSOpen);
262 if (cacheDiskType != AFS_FCACHE_TYPE_UFS) {
263 osi_Panic("UFSOpen called for non-UFS cache\n");
265 if (!afs_osicred_initialized) {
266 /* valid for alpha_osf, SunOS, Ultrix */
267 memset(&afs_osi_cred, 0, sizeof(afs_ucred_t));
268 crhold(&afs_osi_cred); /* don't let it evaporate, since it is static */
269 afs_osicred_initialized = 1;
272 if (afs_CacheFSType == AFS_SUN_VXFS_CACHE)
273 return osi_VxfsOpen(ainode);
275 return osi_UfsOpen(ainode);
279 afs_osi_Stat(register struct osi_file *afile, register struct osi_stat *astat)
281 register afs_int32 code;
283 AFS_STATCNT(osi_Stat);
284 ObtainWriteLock(&afs_xosi, 320);
285 /* Ufs doesn't seem to care about the flags so we pass 0 for now */
286 tvattr.va_mask = AT_ALL;
288 #ifdef AFS_SUN511_ENV
289 code = VOP_GETATTR(afile->vnode, &tvattr, 0, &afs_osi_cred, NULL);
291 code = VOP_GETATTR(afile->vnode, &tvattr, 0, &afs_osi_cred);
295 astat->size = tvattr.va_size;
296 astat->mtime = tvattr.va_mtime.tv_sec;
297 astat->atime = tvattr.va_atime.tv_sec;
299 ReleaseWriteLock(&afs_xosi);
304 osi_UFSClose(register struct osi_file *afile)
306 AFS_STATCNT(osi_Close);
308 AFS_RELE(afile->vnode);
311 osi_FreeSmallSpace(afile);
316 osi_UFSTruncate(register struct osi_file *afile, afs_int32 asize)
318 afs_ucred_t *oldCred;
320 register afs_int32 code;
321 struct osi_stat tstat;
322 AFS_STATCNT(osi_Truncate);
324 /* This routine only shrinks files, and most systems
325 * have very slow truncates, even when the file is already
326 * small enough. Check now and save some time.
328 code = afs_osi_Stat(afile, &tstat);
329 if (code || tstat.size <= asize)
331 ObtainWriteLock(&afs_xosi, 321);
332 tvattr.va_mask = AT_SIZE;
333 tvattr.va_size = asize;
335 * The only time a flag is used (ATTR_UTIME) is when we're changing the time
338 #ifdef AFS_SUN510_ENV
342 code = VOP_SETATTR(afile->vnode, &tvattr, 0, &afs_osi_cred, &ct);
345 code = VOP_SETATTR(afile->vnode, &tvattr, 0, &afs_osi_cred);
348 ReleaseWriteLock(&afs_xosi);
353 osi_DisableAtimes(struct vnode *avp)
355 if (afs_CacheFSType == AFS_SUN_UFS_CACHE) {
356 #ifndef AFS_CACHE_VNODE_PATH
357 struct inode *ip = VTOI(avp);
358 rw_enter(&ip->i_contents, RW_READER);
359 mutex_enter(&ip->i_tlock);
361 mutex_exit(&ip->i_tlock);
362 rw_exit(&ip->i_contents);
368 /* Generic read interface */
370 afs_osi_Read(register struct osi_file *afile, int offset, void *aptr,
373 afs_ucred_t *oldCred;
374 #if defined(AFS_SUN57_ENV)
379 register afs_int32 code;
380 register afs_int32 cnt1 = 0;
381 AFS_STATCNT(osi_Read);
384 * If the osi_file passed in is NULL, panic only if AFS is not shutting
385 * down. No point in crashing when we are already shutting down
388 if (!afs_shuttingdown)
389 osi_Panic("osi_Read called with null param");
395 afile->offset = offset;
398 gop_rdwr(UIO_READ, afile->vnode, (caddr_t) aptr, asize, afile->offset,
399 AFS_UIOSYS, 0, 0, &afs_osi_cred, &resid);
402 code = asize - resid;
403 afile->offset += code;
404 osi_DisableAtimes(afile->vnode);
406 afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, resid,
407 ICL_TYPE_INT32, code);
413 /* Generic write interface */
415 afs_osi_Write(register struct osi_file *afile, afs_int32 offset, void *aptr,
418 afs_ucred_t *oldCred;
419 #if defined(AFS_SUN57_ENV)
424 register afs_int32 code;
425 AFS_STATCNT(osi_Write);
427 osi_Panic("afs_osi_Write called with null param");
429 afile->offset = offset;
432 gop_rdwr(UIO_WRITE, afile->vnode, (caddr_t) aptr, asize,
433 afile->offset, AFS_UIOSYS, 0, RLIM64_INFINITY, &afs_osi_cred,
437 code = asize - resid;
438 afile->offset += code;
443 (*afile->proc) (afile, code);
449 /* This work should be handled by physstrat in ca/machdep.c.
450 This routine written from the RT NFS port strategy routine.
451 It has been generalized a bit, but should still be pretty clear. */
453 afs_osi_MapStrategy(int (*aproc) (), register struct buf *bp)
455 afs_int32 returnCode;
457 AFS_STATCNT(osi_MapStrategy);
458 returnCode = (*aproc) (bp);
466 shutdown_osifile(void)
468 AFS_STATCNT(shutdown_osifile);
469 if (afs_cold_shutdown) {
470 afs_osicred_initialized = 0;