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"
15 #include "../afs/sysincludes.h" /* Standard vendor system headers */
16 #include "../afs/afsincludes.h" /* Afs-based standard headers */
17 #include "../afs/afs_stats.h" /* afs statistics */
20 int afs_osicred_initialized=0;
21 struct AFS_UCRED afs_osi_cred;
22 afs_lock_t afs_xosi; /* lock is for tvattr */
23 extern struct osi_dev cacheDev;
24 extern struct mount *afs_cacheVfsp;
27 void *osi_UFSOpen(afs_int32 ainode)
29 static struct vnode *tags_vnode = NULL;
31 register struct osi_file *afile = NULL;
32 extern int cacheDiskType;
35 AFS_STATCNT(osi_UFSOpen);
36 if(cacheDiskType != AFS_FCACHE_TYPE_UFS)
37 osi_Panic("UFSOpen called for non-UFS cache\n");
39 afile = (struct osi_file *) osi_AllocSmallSpace(sizeof(struct osi_file));
42 switch(afs_cacheVfsp->m_stat.f_type) {
44 code = igetinode(afs_cacheVfsp, (dev_t) cacheDev.dev, (ino_t)ainode, &ip, &dummy);
46 osi_FreeSmallSpace(afile);
47 osi_Panic("UFSOpen: igetinode failed");
50 afile->vnode = ITOV(ip);
51 afile->size = VTOI(afile->vnode)->i_size;
54 afile->inum = ainode; /* for hint validity checking */
58 struct nameidata nd, *ndp = &nd;
59 struct utask_nd utnd = { NULL, NULL };
62 memset(&nd, 0, sizeof(nd));
64 ndp->ni_nameiop = LOOKUP;
65 ndp->ni_cred = &afs_osi_cred;
66 ndp->ni_segflg = UIO_SYSSPACE;
68 /* get hold of a vnode for the .tags directory, so we can
69 lookup files relative to it */
70 if(tags_vnode == NULL) {
71 ndp->ni_cdir = afs_cacheVfsp->m_vnodecovered;
72 strcpy(path, afs_cacheVfsp->m_stat.f_mntonname);
73 strcat(path, "/.tags");
75 if((code = namei(ndp)))
76 osi_Panic("failed to lookup %s (%d)", path, code);
77 tags_vnode = ndp->ni_vp;
79 sprintf(path, "%d", ainode);
81 ndp->ni_cdir = tags_vnode;
82 if((code = namei(ndp)))
83 osi_Panic("failed to lookup %s (%d)", path, code);
85 /* XXX this sucks, chances are we're going to do this again right
86 away, but apparently we can't just set the size to 0 */
87 VOP_GETATTR(ndp->ni_vp, &attr, &afs_osi_cred, code);
89 osi_Panic("failed to stat %s (%d)", path, code);
91 afile->vnode = ndp->ni_vp;
92 afile->size = attr.va_size;
95 afile->inum = ainode; /* for hint validity checking */
99 osi_Panic("UFSOpen called for unknown cache-type (%d)",
100 afs_cacheVfsp->m_stat.f_type);
107 int afs_osi_Stat(register struct osi_file *afile, register struct osi_stat *astat)
109 register afs_int32 code;
111 AFS_STATCNT(osi_Stat);
112 MObtainWriteLock(&afs_xosi,320);
114 VOP_GETATTR(afile->vnode, &tvattr, &afs_osi_cred, code);
117 astat->size = tvattr.va_size;
118 astat->blksize = tvattr.va_blocksize;
119 astat->mtime = tvattr.va_mtime.tv_sec;
120 astat->atime = tvattr.va_atime.tv_sec;
122 MReleaseWriteLock(&afs_xosi);
126 int osi_UFSClose(register struct osi_file *afile)
128 AFS_STATCNT(osi_Close);
130 AFS_RELE(afile->vnode);
133 osi_FreeSmallSpace(afile);
137 int osi_UFSTruncate(register struct osi_file *afile, afs_int32 asize)
139 struct AFS_UCRED *oldCred;
141 register afs_int32 code;
142 struct osi_stat tstat;
143 AFS_STATCNT(osi_Truncate);
145 /* This routine only shrinks files, and most systems
146 * have very slow truncates, even when the file is already
147 * small enough. Check now and save some time.
149 code = afs_osi_Stat(afile, &tstat);
150 if (code || tstat.size <= asize) return code;
151 MObtainWriteLock(&afs_xosi,321);
153 /* note that this credential swapping stuff is only necessary because
154 of ufs's references directly to u.u_cred instead of to
155 credentials parameter. Probably should fix ufs some day. */
156 oldCred = u.u_cred; /* remember old credentials pointer */
157 u.u_cred = &afs_osi_cred; /* temporarily use superuser credentials */
158 tvattr.va_size = asize;
160 VOP_SETATTR(afile->vnode, &tvattr, &afs_osi_cred, code);
162 u.u_cred = oldCred; /* restore */
163 MReleaseWriteLock(&afs_xosi);
167 void osi_DisableAtimes(struct vnode *avp)
170 assert(avp->v_tag == VT_UFS);
176 /* Generic read interface */
177 int afs_osi_Read(register struct osi_file *afile, int offset, void *aptr, afs_int32 asize)
179 struct AFS_UCRED *oldCred;
181 register afs_int32 code;
182 register afs_int32 cnt1=0;
183 AFS_STATCNT(osi_Read);
186 * If the osi_file passed in is NULL, panic only if AFS is not shutting
187 * down. No point in crashing when we are already shutting down
190 if ( !afs_shuttingdown )
191 osi_Panic("osi_Read called with null param");
196 if (offset != -1) afile->offset = offset;
198 code = gop_rdwr(UIO_READ, afile->vnode, (caddr_t) aptr, asize, afile->offset,
199 AFS_UIOSYS, IO_UNIT, &afs_osi_cred, &resid);
202 code = asize - resid;
203 afile->offset += code;
204 osi_DisableAtimes(afile->vnode);
207 afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, resid,
208 ICL_TYPE_INT32, code);
214 /* Generic write interface */
215 int afs_osi_Write(register struct osi_file *afile, afs_int32 offset, void *aptr, afs_int32 asize)
217 struct AFS_UCRED *oldCred;
219 register afs_int32 code;
220 AFS_STATCNT(osi_Write);
222 osi_Panic("afs_osi_Write called with null param");
223 if (offset != -1) afile->offset = offset;
225 struct ucred *tmpcred = u.u_cred;
226 u.u_cred = &afs_osi_cred;
228 code = gop_rdwr(UIO_WRITE, afile->vnode, (caddr_t) aptr, asize, afile->offset,
229 AFS_UIOSYS, IO_UNIT, &afs_osi_cred, &resid);
234 code = asize - resid;
235 afile->offset += code;
241 (*afile->proc)(afile, code);
247 /* This work should be handled by physstrat in ca/machdep.c.
248 This routine written from the RT NFS port strategy routine.
249 It has been generalized a bit, but should still be pretty clear. */
250 int afs_osi_MapStrategy(int (*aproc)(), register struct buf *bp)
252 afs_int32 returnCode;
254 AFS_STATCNT(osi_MapStrategy);
255 returnCode = (*aproc) (bp);
262 void shutdown_osifile(void)
264 extern int afs_cold_shutdown;
266 AFS_STATCNT(shutdown_osifile);
267 if (afs_cold_shutdown) {
268 afs_osicred_initialized = 0;