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;
46 struct statvfs64 vfst;
57 osi_Panic("afs_InitDualFSCacheOps: vp->v_vfsp is NULL");
58 code = VFS_STATVFS(vfsp, &vfst);
60 osi_Panic("afs_InitDualFSCacheOps: statvfs failed");
62 if (strcmp(vfst.f_basetype, "vxfs") == 0) {
63 vxfs_vx_vp_byino = (int (*)())modlookup("vxfs", "vx_vp_byino");
64 if (vxfs_vx_vp_byino == NULL)
66 ("afs_InitDualFSCacheOps: modlookup(vx_vp_byino) failed");
68 afs_CacheFSType = AFS_SUN_VXFS_CACHE;
72 afs_CacheFSType = AFS_SUN_UFS_CACHE;
77 VnodeToIno(vnode_t * vp)
82 vattr.va_mask = AT_FSID | AT_NODEID; /* quick return using this mask. */
84 code = VOP_GETATTR(vp, &vattr, 0, &afs_osi_cred, NULL);
86 code = VOP_GETATTR(vp, &vattr, 0, &afs_osi_cred);
89 osi_Panic("VnodeToIno");
91 return vattr.va_nodeid;
95 VnodeToDev(vnode_t * vp)
100 vattr.va_mask = AT_FSID | AT_NODEID; /* quick return using this mask. */
102 #ifdef AFS_SUN511_ENV
103 code = VOP_GETATTR(vp, &vattr, 0, &afs_osi_cred, NULL);
105 code = VOP_GETATTR(vp, &vattr, 0, &afs_osi_cred);
109 osi_Panic("VnodeToDev");
111 return (dev_t) vattr.va_fsid;
115 VnodeToSize(vnode_t * vp)
121 * We lock xosi in osi_Stat, so we probably should
122 * lock it here too - RWH.
124 ObtainWriteLock(&afs_xosi, 578);
125 vattr.va_mask = AT_SIZE;
127 #ifdef AFS_SUN511_ENV
128 code = VOP_GETATTR(vp, &vattr, 0, &afs_osi_cred, NULL);
130 code = VOP_GETATTR(vp, &vattr, 0, &afs_osi_cred);
134 osi_Panic("VnodeToSize");
136 ReleaseWriteLock(&afs_xosi);
137 return (afs_int32) (vattr.va_size);
141 osi_VxfsOpen(afs_dcache_id_t *ainode)
144 struct osi_file *afile = NULL;
147 afile = (struct osi_file *)osi_AllocSmallSpace(sizeof(struct osi_file));
149 code = (*vxfs_vx_vp_byino) (afs_cacheVfsp, &vp, (unsigned int)ainode->ufs);
152 osi_FreeSmallSpace(afile);
153 osi_Panic("VxfsOpen: vx_vp_byino failed");
156 afile->size = VnodeToSize(afile->vnode);
158 afile->proc = (int (*)())0;
159 return (void *)afile;
161 #endif /* AFS_HAVE_VXFS */
164 osi_UfsOpen(afs_dcache_id_t *ainode)
166 #ifdef AFS_CACHE_VNODE_PATH
171 struct osi_file *afile = NULL;
174 #ifdef AFS_CACHE_VNODE_PATH
176 struct pathname lookpn;
178 struct osi_stat tstat;
179 afile = (struct osi_file *)osi_AllocSmallSpace(sizeof(struct osi_file));
183 * AFS_CACHE_VNODE_PATH can be used with any file system, including ZFS or tmpfs.
184 * The ainode is not an inode number but a path.
186 #ifdef AFS_CACHE_VNODE_PATH
187 /* Can not use vn_open or lookupname, they use user's CRED()
188 * We need to run as root So must use low level lookuppnvp
189 * assume fname starts with /
192 code = pn_get_buf(ainode->ufs, AFS_UIOSYS, &lookpn, namebuf, sizeof(namebuf));
194 osi_Panic("UfsOpen: pn_get_buf failed %ld %s", code, ainode->ufs);
196 VN_HOLD(rootdir); /* released in loopuppnvp */
197 code = lookuppnvp(&lookpn, NULL, FOLLOW, NULL, &vp,
198 rootdir, rootdir, &afs_osi_cred);
200 osi_Panic("UfsOpen: lookuppnvp failed %ld %s", code, ainode->ufs);
202 #ifdef AFS_SUN511_ENV
203 code = VOP_OPEN(&vp, FREAD|FWRITE, &afs_osi_cred, NULL);
205 code = VOP_OPEN(&vp, FREAD|FWRITE, &afs_osi_cred);
209 osi_Panic("UfsOpen: VOP_OPEN failed %ld %s", code, ainode->ufs);
213 igetinode(afs_cacheVfsp, (dev_t) cacheDev.dev, ainode->ufs, &ip,
218 osi_FreeSmallSpace(afile);
219 osi_Panic("UfsOpen: igetinode failed %ld %s", code, ainode->ufs);
221 #ifdef AFS_CACHE_VNODE_PATH
223 code = afs_osi_Stat(afile, &tstat);
224 afile->size = tstat.size;
226 afile->vnode = ITOV(ip);
227 afile->size = VTOI(afile->vnode)->i_size;
230 afile->proc = (int (*)())0;
231 return (void *)afile;
235 * In Solaris 7 we use 64 bit inode numbers
238 osi_UFSOpen(afs_dcache_id_t *ainode)
240 extern int cacheDiskType;
241 AFS_STATCNT(osi_UFSOpen);
242 if (cacheDiskType != AFS_FCACHE_TYPE_UFS) {
243 osi_Panic("UFSOpen called for non-UFS cache\n");
245 if (!afs_osicred_initialized) {
246 /* valid for alpha_osf, SunOS, Ultrix */
247 memset(&afs_osi_cred, 0, sizeof(afs_ucred_t));
248 crhold(&afs_osi_cred); /* don't let it evaporate, since it is static */
249 afs_osicred_initialized = 1;
252 if (afs_CacheFSType == AFS_SUN_VXFS_CACHE)
253 return osi_VxfsOpen(ainode);
255 return osi_UfsOpen(ainode);
259 afs_osi_Stat(struct osi_file *afile, struct osi_stat *astat)
263 AFS_STATCNT(osi_Stat);
264 ObtainWriteLock(&afs_xosi, 320);
265 /* Ufs doesn't seem to care about the flags so we pass 0 for now */
266 tvattr.va_mask = AT_ALL;
268 #ifdef AFS_SUN511_ENV
269 code = VOP_GETATTR(afile->vnode, &tvattr, 0, &afs_osi_cred, NULL);
271 code = VOP_GETATTR(afile->vnode, &tvattr, 0, &afs_osi_cred);
275 astat->size = tvattr.va_size;
276 astat->mtime = tvattr.va_mtime.tv_sec;
277 astat->atime = tvattr.va_atime.tv_sec;
279 ReleaseWriteLock(&afs_xosi);
284 osi_UFSClose(struct osi_file *afile)
286 AFS_STATCNT(osi_Close);
288 AFS_RELE(afile->vnode);
291 osi_FreeSmallSpace(afile);
296 osi_UFSTruncate(struct osi_file *afile, afs_int32 asize)
298 afs_ucred_t *oldCred;
301 struct osi_stat tstat;
302 AFS_STATCNT(osi_Truncate);
304 /* This routine only shrinks files, and most systems
305 * have very slow truncates, even when the file is already
306 * small enough. Check now and save some time.
308 code = afs_osi_Stat(afile, &tstat);
309 if (code || tstat.size <= asize)
311 ObtainWriteLock(&afs_xosi, 321);
312 tvattr.va_mask = AT_SIZE;
313 tvattr.va_size = asize;
315 * The only time a flag is used (ATTR_UTIME) is when we're changing the time
318 #ifdef AFS_SUN510_ENV
322 code = VOP_SETATTR(afile->vnode, &tvattr, 0, &afs_osi_cred, &ct);
325 code = VOP_SETATTR(afile->vnode, &tvattr, 0, &afs_osi_cred);
328 ReleaseWriteLock(&afs_xosi);
333 osi_DisableAtimes(struct vnode *avp)
335 if (afs_CacheFSType == AFS_SUN_UFS_CACHE) {
336 #ifndef AFS_CACHE_VNODE_PATH
337 struct inode *ip = VTOI(avp);
338 rw_enter(&ip->i_contents, RW_READER);
339 mutex_enter(&ip->i_tlock);
341 mutex_exit(&ip->i_tlock);
342 rw_exit(&ip->i_contents);
348 /* Generic read interface */
350 afs_osi_Read(struct osi_file *afile, int offset, void *aptr,
353 afs_ucred_t *oldCred;
357 AFS_STATCNT(osi_Read);
360 * If the osi_file passed in is NULL, panic only if AFS is not shutting
361 * down. No point in crashing when we are already shutting down
364 if (!afs_shuttingdown)
365 osi_Panic("osi_Read called with null param");
371 afile->offset = offset;
374 gop_rdwr(UIO_READ, afile->vnode, (caddr_t) aptr, asize, afile->offset,
375 AFS_UIOSYS, 0, 0, &afs_osi_cred, &resid);
378 code = asize - resid;
379 afile->offset += code;
380 osi_DisableAtimes(afile->vnode);
382 afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, resid,
383 ICL_TYPE_INT32, code);
391 /* Generic write interface */
393 afs_osi_Write(struct osi_file *afile, afs_int32 offset, void *aptr,
396 afs_ucred_t *oldCred;
399 AFS_STATCNT(osi_Write);
401 osi_Panic("afs_osi_Write called with null param");
403 afile->offset = offset;
406 gop_rdwr(UIO_WRITE, afile->vnode, (caddr_t) aptr, asize,
407 afile->offset, AFS_UIOSYS, 0, RLIM64_INFINITY, &afs_osi_cred,
411 code = asize - resid;
412 afile->offset += code;
419 (*afile->proc) (afile, code);
425 /* This work should be handled by physstrat in ca/machdep.c.
426 This routine written from the RT NFS port strategy routine.
427 It has been generalized a bit, but should still be pretty clear. */
429 afs_osi_MapStrategy(int (*aproc) (), struct buf *bp)
431 afs_int32 returnCode;
433 AFS_STATCNT(osi_MapStrategy);
434 returnCode = (*aproc) (bp);
442 shutdown_osifile(void)
444 AFS_STATCNT(shutdown_osifile);
445 if (afs_cold_shutdown) {
446 afs_osicred_initialized = 0;