Catch up to dynamically-sized cr_groups in FBSD80
[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 #elif defined(AFS_FBSD50_ENV)
56     VOP_UNLOCK(vp, 0, curthread);
57 #else
58     VOP_UNLOCK(vp, 0, curproc);
59 #endif
60     afile->vnode = vp;
61     afile->size = VTOI(vp)->i_size;
62     afile->offset = 0;
63     afile->proc = NULL;
64     return (void *)afile;
65 }
66
67 int
68 afs_osi_Stat(register struct osi_file *afile, register struct osi_stat *astat)
69 {
70     register afs_int32 code;
71     struct vattr tvattr;
72     AFS_STATCNT(osi_Stat);
73     ObtainWriteLock(&afs_xosi, 320);
74     AFS_GUNLOCK();
75 #if defined(AFS_FBSD80_ENV)
76     vn_lock(afile->vnode, LK_EXCLUSIVE | LK_RETRY);
77     code = VOP_GETATTR(afile->vnode, &tvattr, afs_osi_credp);
78     VOP_UNLOCK(afile->vnode, 0);
79 #elif defined(AFS_FBSD50_ENV)
80     vn_lock(afile->vnode, LK_EXCLUSIVE | LK_RETRY, curthread);
81     code = VOP_GETATTR(afile->vnode, &tvattr, afs_osi_credp, curthread);
82     VOP_UNLOCK(afile->vnode, LK_EXCLUSIVE, curthread);
83 #else
84     code = VOP_GETATTR(afile->vnode, &tvattr, afs_osi_credp, curproc);
85 #endif
86     AFS_GLOCK();
87     if (code == 0) {
88         astat->size = tvattr.va_size;
89         astat->mtime = tvattr.va_mtime.tv_sec;
90         astat->atime = tvattr.va_atime.tv_sec;
91     }
92     ReleaseWriteLock(&afs_xosi);
93     return code;
94 }
95
96 int
97 osi_UFSClose(register struct osi_file *afile)
98 {
99     AFS_STATCNT(osi_Close);
100     if (afile->vnode) {
101         AFS_RELE(afile->vnode);
102     }
103
104     osi_FreeSmallSpace(afile);
105     return 0;
106 }
107
108 int
109 osi_UFSTruncate(register struct osi_file *afile, afs_int32 asize)
110 {
111     struct vattr tvattr;
112     struct vnode *vp;
113     register afs_int32 code, glocked;
114     AFS_STATCNT(osi_Truncate);
115
116     ObtainWriteLock(&afs_xosi, 321);
117     vp = afile->vnode;
118     /*
119      * This routine only shrinks files, and most systems
120      * have very slow truncates, even when the file is already
121      * small enough.  Check now and save some time.
122      */
123     glocked = ISAFS_GLOCK();
124     if (glocked)
125       AFS_GUNLOCK();
126 #if defined(AFS_FBSD80_ENV)
127     vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
128     code = VOP_GETATTR(afile->vnode, &tvattr, afs_osi_credp);
129 #elif defined(AFS_FBSD50_ENV)
130     vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
131     code = VOP_GETATTR(afile->vnode, &tvattr, afs_osi_credp, curthread);
132 #else
133     vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curproc);
134     code = VOP_GETATTR(afile->vnode, &tvattr, afs_osi_credp, curproc);
135 #endif
136     if (code != 0 || tvattr.va_size <= asize)
137         goto out;
138
139     VATTR_NULL(&tvattr);
140     tvattr.va_size = asize;
141 #if defined(AFS_FBSD80_ENV)
142     code = VOP_SETATTR(vp, &tvattr, afs_osi_credp);
143 #elif defined(AFS_FBSD50_ENV)
144     code = VOP_SETATTR(vp, &tvattr, afs_osi_credp, curthread);
145 #else
146     code = VOP_SETATTR(vp, &tvattr, afs_osi_credp, curproc);
147 #endif
148
149 out:
150 #if defined(AFS_FBSD80_ENV)
151     VOP_UNLOCK(vp, 0);
152 #elif defined(AFS_FBSD50_ENV)
153     VOP_UNLOCK(vp, LK_EXCLUSIVE, curthread);
154 #else
155     VOP_UNLOCK(vp, LK_EXCLUSIVE, curproc);
156 #endif
157     if (glocked)
158       AFS_GLOCK();
159     ReleaseWriteLock(&afs_xosi);
160     return code;
161 }
162
163 void
164 osi_DisableAtimes(struct vnode *avp)
165 {
166     struct inode *ip = VTOI(avp);
167     ip->i_flag &= ~IN_ACCESS;
168 }
169
170
171 /* Generic read interface */
172 int
173 afs_osi_Read(register struct osi_file *afile, int offset, void *aptr,
174              afs_int32 asize)
175 {
176     unsigned int resid;
177     register afs_int32 code;
178     AFS_STATCNT(osi_Read);
179
180     /**
181       * If the osi_file passed in is NULL, panic only if AFS is not shutting
182       * down. No point in crashing when we are already shutting down
183       */
184     if (!afile) {
185         if (!afs_shuttingdown)
186             osi_Panic("osi_Read called with null param");
187         else
188             return EIO;
189     }
190
191     if (offset != -1)
192         afile->offset = offset;
193     AFS_GUNLOCK();
194     code =
195         gop_rdwr(UIO_READ, afile->vnode, (caddr_t) aptr, asize, afile->offset,
196                  AFS_UIOSYS, IO_UNIT, afs_osi_credp, &resid);
197     AFS_GLOCK();
198     if (code == 0) {
199         code = asize - resid;
200         afile->offset += code;
201         osi_DisableAtimes(afile->vnode);
202     } else {
203         afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, resid,
204                    ICL_TYPE_INT32, code);
205         code = -1;
206     }
207     return code;
208 }
209
210 /* Generic write interface */
211 int
212 afs_osi_Write(register struct osi_file *afile, afs_int32 offset, void *aptr,
213               afs_int32 asize)
214 {
215     unsigned int resid;
216     register afs_int32 code;
217     AFS_STATCNT(osi_Write);
218     if (!afile)
219         osi_Panic("afs_osi_Write called with null param");
220     if (offset != -1)
221         afile->offset = offset;
222     {
223         AFS_GUNLOCK();
224         code =
225             gop_rdwr(UIO_WRITE, afile->vnode, (caddr_t) aptr, asize,
226                      afile->offset, AFS_UIOSYS, IO_UNIT, afs_osi_credp,
227                      &resid);
228         AFS_GLOCK();
229     }
230     if (code == 0) {
231         code = asize - resid;
232         afile->offset += code;
233     } else {
234         code = -1;
235     }
236     if (afile->proc) {
237         (*afile->proc) (afile, code);
238     }
239     return code;
240 }
241
242
243 /*  This work should be handled by physstrat in ca/machdep.c.
244     This routine written from the RT NFS port strategy routine.
245     It has been generalized a bit, but should still be pretty clear. */
246 int
247 afs_osi_MapStrategy(int (*aproc) (), register struct buf *bp)
248 {
249     afs_int32 returnCode;
250
251     AFS_STATCNT(osi_MapStrategy);
252     returnCode = (*aproc) (bp);
253
254     return returnCode;
255 }
256
257
258
259 void
260 shutdown_osifile(void)
261 {
262     AFS_STATCNT(shutdown_osifile);
263     if (afs_cold_shutdown) {
264         afs_osicred_initialized = 0;
265     }
266 }