Update to the new thread world order for FBSD
[openafs.git] / src / afs / FBSD / osi_file.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 #include <afsconfig.h>
11 #include "afs/param.h"
12
13
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
18
19 int afs_osicred_initialized = 0;
20 #ifndef AFS_FBSD80_ENV  /* cr_groups is now malloc()'d */
21 afs_ucred_t afs_osi_cred;
22 #endif
23 afs_lock_t afs_xosi;            /* lock is for tvattr */
24 extern struct osi_dev cacheDev;
25 extern struct mount *afs_cacheVfsp;
26
27
28 void *
29 osi_UFSOpen(afs_dcache_id_t *ainode)
30 {
31     struct osi_file *afile;
32     struct vnode *vp;
33     extern int cacheDiskType;
34     afs_int32 code;
35
36     AFS_STATCNT(osi_UFSOpen);
37     if (cacheDiskType != AFS_FCACHE_TYPE_UFS)
38         osi_Panic("UFSOpen called for non-UFS cache\n");
39     afile = (struct osi_file *)osi_AllocSmallSpace(sizeof(struct osi_file));
40     AFS_GUNLOCK();
41 #if defined(AFS_FBSD50_ENV)
42     code = VFS_VGET(afs_cacheVfsp, (ino_t) ainode->ufs, LK_EXCLUSIVE, &vp);
43 #else
44     code = VFS_VGET(afs_cacheVfsp, (ino_t) ainode->ufs, &vp);
45 #endif
46     AFS_GLOCK();
47     if (code == 0 && vp->v_type == VNON)
48         code = ENOENT;
49     if (code) {
50         osi_FreeSmallSpace(afile);
51         osi_Panic("UFSOpen: igetinode failed");
52     }
53 #if defined(AFS_FBSD80_ENV)
54     VOP_UNLOCK(vp, 0);
55 #else
56     VOP_UNLOCK(vp, 0, curthread);
57 #endif
58     afile->vnode = vp;
59     afile->size = VTOI(vp)->i_size;
60     afile->offset = 0;
61     afile->proc = NULL;
62     return (void *)afile;
63 }
64
65 int
66 afs_osi_Stat(register struct osi_file *afile, register struct osi_stat *astat)
67 {
68     register afs_int32 code;
69     struct vattr tvattr;
70     AFS_STATCNT(osi_Stat);
71     ObtainWriteLock(&afs_xosi, 320);
72     AFS_GUNLOCK();
73 #if defined(AFS_FBSD80_ENV)
74     vn_lock(afile->vnode, LK_EXCLUSIVE | LK_RETRY);
75     code = VOP_GETATTR(afile->vnode, &tvattr, afs_osi_credp);
76     VOP_UNLOCK(afile->vnode, 0);
77 #else
78     vn_lock(afile->vnode, LK_EXCLUSIVE | LK_RETRY, curthread);
79     code = VOP_GETATTR(afile->vnode, &tvattr, afs_osi_credp, curthread);
80     VOP_UNLOCK(afile->vnode, LK_EXCLUSIVE, curthread);
81 #endif
82     AFS_GLOCK();
83     if (code == 0) {
84         astat->size = tvattr.va_size;
85         astat->mtime = tvattr.va_mtime.tv_sec;
86         astat->atime = tvattr.va_atime.tv_sec;
87     }
88     ReleaseWriteLock(&afs_xosi);
89     return code;
90 }
91
92 int
93 osi_UFSClose(register struct osi_file *afile)
94 {
95     AFS_STATCNT(osi_Close);
96     if (afile->vnode) {
97         AFS_RELE(afile->vnode);
98     }
99
100     osi_FreeSmallSpace(afile);
101     return 0;
102 }
103
104 int
105 osi_UFSTruncate(register struct osi_file *afile, afs_int32 asize)
106 {
107     struct vattr tvattr;
108     struct vnode *vp;
109     register afs_int32 code, glocked;
110     AFS_STATCNT(osi_Truncate);
111
112     ObtainWriteLock(&afs_xosi, 321);
113     vp = afile->vnode;
114     /*
115      * This routine only shrinks files, and most systems
116      * have very slow truncates, even when the file is already
117      * small enough.  Check now and save some time.
118      */
119     glocked = ISAFS_GLOCK();
120     if (glocked)
121       AFS_GUNLOCK();
122 #if defined(AFS_FBSD80_ENV)
123     vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
124     code = VOP_GETATTR(afile->vnode, &tvattr, afs_osi_credp);
125 #else
126     vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
127     code = VOP_GETATTR(afile->vnode, &tvattr, afs_osi_credp, curthread);
128 #endif
129     if (code != 0 || tvattr.va_size <= asize)
130         goto out;
131
132     VATTR_NULL(&tvattr);
133     tvattr.va_size = asize;
134 #if defined(AFS_FBSD80_ENV)
135     code = VOP_SETATTR(vp, &tvattr, afs_osi_credp);
136 #else
137     code = VOP_SETATTR(vp, &tvattr, afs_osi_credp, curthread);
138 #endif
139
140 out:
141 #if defined(AFS_FBSD80_ENV)
142     VOP_UNLOCK(vp, 0);
143 #else
144     VOP_UNLOCK(vp, LK_EXCLUSIVE, curthread);
145 #endif
146     if (glocked)
147       AFS_GLOCK();
148     ReleaseWriteLock(&afs_xosi);
149     return code;
150 }
151
152 void
153 osi_DisableAtimes(struct vnode *avp)
154 {
155     struct inode *ip = VTOI(avp);
156     ip->i_flag &= ~IN_ACCESS;
157 }
158
159
160 /* Generic read interface */
161 int
162 afs_osi_Read(register struct osi_file *afile, int offset, void *aptr,
163              afs_int32 asize)
164 {
165     unsigned int resid;
166     register afs_int32 code;
167     AFS_STATCNT(osi_Read);
168
169     /**
170       * If the osi_file passed in is NULL, panic only if AFS is not shutting
171       * down. No point in crashing when we are already shutting down
172       */
173     if (!afile) {
174         if (!afs_shuttingdown)
175             osi_Panic("osi_Read called with null param");
176         else
177             return EIO;
178     }
179
180     if (offset != -1)
181         afile->offset = offset;
182     AFS_GUNLOCK();
183     code =
184         gop_rdwr(UIO_READ, afile->vnode, (caddr_t) aptr, asize, afile->offset,
185                  AFS_UIOSYS, IO_UNIT, afs_osi_credp, &resid);
186     AFS_GLOCK();
187     if (code == 0) {
188         code = asize - resid;
189         afile->offset += code;
190         osi_DisableAtimes(afile->vnode);
191     } else {
192         afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, resid,
193                    ICL_TYPE_INT32, code);
194         code = -1;
195     }
196     return code;
197 }
198
199 /* Generic write interface */
200 int
201 afs_osi_Write(register struct osi_file *afile, afs_int32 offset, void *aptr,
202               afs_int32 asize)
203 {
204     unsigned int resid;
205     register afs_int32 code;
206     AFS_STATCNT(osi_Write);
207     if (!afile)
208         osi_Panic("afs_osi_Write called with null param");
209     if (offset != -1)
210         afile->offset = offset;
211     {
212         AFS_GUNLOCK();
213         code =
214             gop_rdwr(UIO_WRITE, afile->vnode, (caddr_t) aptr, asize,
215                      afile->offset, AFS_UIOSYS, IO_UNIT, afs_osi_credp,
216                      &resid);
217         AFS_GLOCK();
218     }
219     if (code == 0) {
220         code = asize - resid;
221         afile->offset += code;
222     } else {
223         code = -1;
224     }
225     if (afile->proc) {
226         (*afile->proc) (afile, code);
227     }
228     return code;
229 }
230
231
232 /*  This work should be handled by physstrat in ca/machdep.c.
233     This routine written from the RT NFS port strategy routine.
234     It has been generalized a bit, but should still be pretty clear. */
235 int
236 afs_osi_MapStrategy(int (*aproc) (), register struct buf *bp)
237 {
238     afs_int32 returnCode;
239
240     AFS_STATCNT(osi_MapStrategy);
241     returnCode = (*aproc) (bp);
242
243     return returnCode;
244 }
245
246
247
248 void
249 shutdown_osifile(void)
250 {
251     AFS_STATCNT(shutdown_osifile);
252     if (afs_cold_shutdown) {
253         afs_osicred_initialized = 0;
254     }
255 }