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