Remove 'M' variants of lock macros
[openafs.git] / src / afs / DARWIN / 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 #include "afs/osi_inode.h"
18
19
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 mount *afs_cacheVfsp;
25 int afs_CacheFSType = -1;
26
27 /* Initialize the cache operations. Called while initializing cache files. */
28 void
29 afs_InitDualFSCacheOps(struct vnode *vp)
30 {
31     int code;
32     static int inited = 0;
33 #ifdef AFS_DARWIN80_ENV
34     char *buffer = (char*)_MALLOC(MFSNAMELEN, M_TEMP, M_WAITOK);
35 #endif
36
37     if (inited)
38         return;
39     inited = 1;
40
41     if (vp == NULL)
42         return;
43 #ifdef AFS_DARWIN80_ENV
44     vfs_name(vnode_mount(vp), buffer);
45     if (strncmp("hfs", buffer, 3) == 0)
46 #else
47     if (strncmp("hfs", vp->v_mount->mnt_vfc->vfc_name, 3) == 0)
48 #endif
49         afs_CacheFSType = AFS_APPL_HFS_CACHE;
50 #ifdef AFS_DARWIN80_ENV
51     else if (strncmp("ufs", buffer, 3) == 0)
52 #else
53     else if (strncmp("ufs", vp->v_mount->mnt_vfc->vfc_name, 3) == 0)
54 #endif
55         afs_CacheFSType = AFS_APPL_UFS_CACHE;
56     else
57         osi_Panic("Unknown cache vnode type\n");
58 #ifdef AFS_DARWIN80_ENV
59     _FREE(buffer, M_TEMP);
60 #endif
61 }
62
63 ino_t
64 VnodeToIno(vnode_t avp)
65 {
66     unsigned long ret;
67
68 #ifndef AFS_DARWIN80_ENV
69     if (afs_CacheFSType == AFS_APPL_UFS_CACHE) {
70         struct inode *ip = VTOI(avp);
71         ret = ip->i_number;
72     } else if (afs_CacheFSType == AFS_APPL_HFS_CACHE) {
73 #endif
74 #if defined(AFS_DARWIN80_ENV) 
75         struct vattr va;
76         VATTR_INIT(&va);
77         VATTR_WANTED(&va, va_fileid);
78         if (vnode_getattr(avp, &va, afs_osi_ctxtp))
79             osi_Panic("VOP_GETATTR failed in VnodeToIno\n");
80         if (!VATTR_ALL_SUPPORTED(&va))
81             osi_Panic("VOP_GETATTR unsupported fileid in VnodeToIno\n");
82         ret = va.va_fileid;
83 #elif !defined(VTOH)
84         struct vattr va;
85         if (VOP_GETATTR(avp, &va, &afs_osi_cred, current_proc()))
86             osi_Panic("VOP_GETATTR failed in VnodeToIno\n");
87         ret = va.va_fileid;
88 #else
89         struct hfsnode *hp = VTOH(avp);
90         ret = H_FILEID(hp);
91 #endif
92 #ifndef AFS_DARWIN80_ENV
93     } else
94         osi_Panic("VnodeToIno called before cacheops initialized\n");
95 #endif
96     return ret;
97 }
98
99
100 dev_t
101 VnodeToDev(vnode_t avp)
102 {
103 #ifndef AFS_DARWIN80_ENV
104     if (afs_CacheFSType == AFS_APPL_UFS_CACHE) {
105         struct inode *ip = VTOI(avp);
106         return ip->i_dev;
107     } else if (afs_CacheFSType == AFS_APPL_HFS_CACHE) {
108 #endif
109 #if defined(AFS_DARWIN80_ENV) 
110         struct vattr va;
111         VATTR_INIT(&va);
112         VATTR_WANTED(&va, va_fsid);
113         if (vnode_getattr(avp, &va, afs_osi_ctxtp))
114             osi_Panic("VOP_GETATTR failed in VnodeToDev\n");
115         if (!VATTR_ALL_SUPPORTED(&va))
116             osi_Panic("VOP_GETATTR unsupported fsid in VnodeToIno\n");
117         return va.va_fsid;      /* XXX they say it's the dev.... */
118 #elif !defined(VTOH)
119         struct vattr va;
120         if (VOP_GETATTR(avp, &va, &afs_osi_cred, current_proc()))
121             osi_Panic("VOP_GETATTR failed in VnodeToDev\n");
122         return va.va_fsid;      /* XXX they say it's the dev.... */
123 #else
124         struct hfsnode *hp = VTOH(avp);
125         return H_DEV(hp);
126 #endif
127 #ifndef AFS_DARWIN80_ENV
128     } else
129         osi_Panic("VnodeToDev called before cacheops initialized\n");
130 #endif
131 }
132
133 void *
134 osi_UFSOpen(afs_dcache_id_t *ainode)
135 {
136     struct vnode *vp;
137     struct vattr va;
138     register struct osi_file *afile = NULL;
139     extern int cacheDiskType;
140     afs_int32 code = 0;
141     int dummy;
142     char fname[1024];
143     struct osi_stat tstat;
144
145     AFS_STATCNT(osi_UFSOpen);
146     if (cacheDiskType != AFS_FCACHE_TYPE_UFS) {
147         osi_Panic("UFSOpen called for non-UFS cache\n");
148     }
149     if (!afs_osicred_initialized) {
150         /* valid for alpha_osf, SunOS, Ultrix */
151         memset(&afs_osi_cred, 0, sizeof(afs_ucred_t));
152         afs_osi_cred.cr_ref++;
153         afs_osi_cred.cr_ngroups = 1;
154         afs_osicred_initialized = 1;
155     }
156     afile = (struct osi_file *)osi_AllocSmallSpace(sizeof(struct osi_file));
157     AFS_GUNLOCK();
158 #ifdef AFS_CACHE_VNODE_PATH
159     if (ainode->ufs < 0) {
160         switch (ainode->ufs) {
161         case AFS_CACHE_CELLS_INODE:
162             snprintf(fname, 1024, "%s/%s", afs_cachebasedir, "CellItems");
163             break;
164         case AFS_CACHE_ITEMS_INODE:
165             snprintf(fname, 1024, "%s/%s", afs_cachebasedir, "CacheItems");
166             break;
167         case AFS_CACHE_VOLUME_INODE:
168             snprintf(fname, 1024, "%s/%s", afs_cachebasedir, "VolumeItems");
169             break;
170         default:
171             osi_Panic("Invalid negative inode");
172         }
173     } else {
174         dummy = ainode->ufs / afs_numfilesperdir;
175         snprintf(fname, 1024, "%s/D%d/V%d", afs_cachebasedir, dummy, ainode->ufs);
176     }
177
178     code = vnode_open(fname, O_RDWR, 0, 0, &vp, afs_osi_ctxtp);
179 #else
180 #ifndef AFS_DARWIN80_ENV
181     if (afs_CacheFSType == AFS_APPL_HFS_CACHE)
182         code = igetinode(afs_cacheVfsp, (dev_t) cacheDev.dev, &ainode->ufs, &vp, &va, &dummy);  /* XXX hfs is broken */
183     else if (afs_CacheFSType == AFS_APPL_UFS_CACHE)
184 #endif
185         code =
186             igetinode(afs_cacheVfsp, (dev_t) cacheDev.dev, (ino_t) ainode->ufs,
187                       &vp, &va, &dummy);
188 #ifndef AFS_DARWIN80_ENV
189     else
190         panic("osi_UFSOpen called before cacheops initialized\n");
191 #endif
192 #endif
193     AFS_GLOCK();
194     if (code) {
195         osi_FreeSmallSpace(afile);
196         osi_Panic("UFSOpen: igetinode failed");
197     }
198     afile->vnode = vp;
199     afile->offset = 0;
200     afile->proc = (int (*)())0;
201     afile->inum = ainode->ufs;  /* for hint validity checking */
202 #ifndef AFS_CACHE_VNODE_PATH
203     afile->size = va.va_size;
204 #else
205     code = afs_osi_Stat(afile, &tstat);
206     afile->size = tstat.size;
207 #endif
208     return (void *)afile;
209 }
210
211 int
212 afs_osi_Stat(register struct osi_file *afile, register struct osi_stat *astat)
213 {
214     register afs_int32 code;
215     struct vattr tvattr;
216     AFS_STATCNT(osi_Stat);
217     ObtainWriteLock(&afs_xosi, 320);
218     AFS_GUNLOCK();
219 #ifdef AFS_DARWIN80_ENV
220     VATTR_INIT(&tvattr);
221     VATTR_WANTED(&tvattr, va_size);
222     VATTR_WANTED(&tvattr, va_blocksize);
223     VATTR_WANTED(&tvattr, va_mtime);
224     VATTR_WANTED(&tvattr, va_atime);
225     code = vnode_getattr(afile->vnode, &tvattr, afs_osi_ctxtp);
226     if (code == 0 && !VATTR_ALL_SUPPORTED(&tvattr))
227        code = EINVAL;
228 #else
229     code = VOP_GETATTR(afile->vnode, &tvattr, &afs_osi_cred, current_proc());
230 #endif
231     AFS_GLOCK();
232     if (code == 0) {
233         astat->size = tvattr.va_size;
234         astat->mtime = tvattr.va_mtime.tv_sec;
235         astat->atime = tvattr.va_atime.tv_sec;
236     }
237     ReleaseWriteLock(&afs_xosi);
238     return code;
239 }
240
241 int
242 osi_UFSClose(register struct osi_file *afile)
243 {
244     AFS_STATCNT(osi_Close);
245     if (afile->vnode) {
246 #ifdef AFS_DARWIN80_ENV
247         vnode_close(afile->vnode, O_RDWR, afs_osi_ctxtp);
248 #else
249         AFS_RELE(afile->vnode);
250 #endif
251     }
252
253     osi_FreeSmallSpace(afile);
254     return 0;
255 }
256
257 int
258 osi_UFSTruncate(register struct osi_file *afile, afs_int32 asize)
259 {
260     afs_ucred_t *oldCred;
261     struct vattr tvattr;
262     register afs_int32 code;
263     struct osi_stat tstat;
264     AFS_STATCNT(osi_Truncate);
265
266     /* This routine only shrinks files, and most systems
267      * have very slow truncates, even when the file is already
268      * small enough.  Check now and save some time.
269      */
270     code = afs_osi_Stat(afile, &tstat);
271     if (code || tstat.size <= asize)
272         return code;
273     ObtainWriteLock(&afs_xosi, 321);
274     AFS_GUNLOCK();
275 #ifdef AFS_DARWIN80_ENV
276     VATTR_INIT(&tvattr);
277     VATTR_SET(&tvattr, va_size, asize);
278     code = vnode_setattr(afile->vnode, &tvattr, afs_osi_ctxtp);
279 #else
280     VATTR_NULL(&tvattr);
281     tvattr.va_size = asize;
282     code = VOP_SETATTR(afile->vnode, &tvattr, &afs_osi_cred, current_proc());
283 #endif
284     AFS_GLOCK();
285     ReleaseWriteLock(&afs_xosi);
286     return code;
287 }
288
289 void
290 #ifdef AFS_DARWIN80_ENV
291 osi_DisableAtimes(vnode_t avp)
292 #else
293 osi_DisableAtimes(struct vnode *avp)
294 #endif
295 {
296 #ifdef AFS_DARWIN80_ENV
297     struct vnode_attr vap;
298
299     VATTR_INIT(&vap);
300     VATTR_CLEAR_SUPPORTED(&vap, va_access_time);
301     vnode_setattr(avp, &vap, afs_osi_ctxtp);
302 #else
303     if (afs_CacheFSType == AFS_APPL_UFS_CACHE) {
304         struct inode *ip = VTOI(avp);
305         ip->i_flag &= ~IN_ACCESS;
306     }
307 #ifdef VTOH                     /* can't do this without internals */
308     else if (afs_CacheFSType == AFS_APPL_HFS_CACHE) {
309         struct hfsnode *hp = VTOH(avp);
310         hp->h_nodeflags &= ~IN_ACCESS;
311     }
312 #endif
313 #endif
314 }
315
316
317 /* Generic read interface */
318 int
319 afs_osi_Read(register struct osi_file *afile, int offset, void *aptr,
320              afs_int32 asize)
321 {
322     afs_ucred_t *oldCred;
323     afs_size_t resid;
324     register afs_int32 code;
325 #ifdef AFS_DARWIN80_ENV
326     uio_t uio;
327 #endif
328     AFS_STATCNT(osi_Read);
329
330     /**
331       * If the osi_file passed in is NULL, panic only if AFS is not shutting
332       * down. No point in crashing when we are already shutting down
333       */
334     if (!afile) {
335         if (!afs_shuttingdown)
336             osi_Panic("osi_Read called with null param");
337         else
338             return EIO;
339     }
340
341     if (offset != -1)
342         afile->offset = offset;
343     AFS_GUNLOCK();
344 #ifdef AFS_DARWIN80_ENV
345     uio=uio_create(1, afile->offset, AFS_UIOSYS, UIO_READ);
346     uio_addiov(uio, CAST_USER_ADDR_T(aptr), asize);
347     code = VNOP_READ(afile->vnode, uio, IO_UNIT, afs_osi_ctxtp);
348     resid = AFS_UIO_RESID(uio);
349     uio_free(uio);
350 #else
351     code =
352         gop_rdwr(UIO_READ, afile->vnode, (caddr_t) aptr, asize, afile->offset,
353                  AFS_UIOSYS, IO_UNIT, &afs_osi_cred, &resid);
354 #endif
355     AFS_GLOCK();
356     if (code == 0) {
357         code = asize - resid;
358         afile->offset += code;
359         osi_DisableAtimes(afile->vnode);
360     } else {
361         afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, resid,
362                    ICL_TYPE_INT32, code);
363         code = -1;
364     }
365     return code;
366 }
367
368 /* Generic write interface */
369 int
370 afs_osi_Write(register struct osi_file *afile, afs_int32 offset, void *aptr,
371               afs_int32 asize)
372 {
373     afs_ucred_t *oldCred;
374     afs_size_t resid;
375     register afs_int32 code;
376 #ifdef AFS_DARWIN80_ENV
377     uio_t uio;
378 #endif
379     AFS_STATCNT(osi_Write);
380     if (!afile)
381         osi_Panic("afs_osi_Write called with null param");
382     if (offset != -1)
383         afile->offset = offset;
384     {
385         AFS_GUNLOCK();
386 #ifdef AFS_DARWIN80_ENV
387         uio=uio_create(1, afile->offset, AFS_UIOSYS, UIO_WRITE);
388         uio_addiov(uio, CAST_USER_ADDR_T(aptr), asize);
389         code = VNOP_WRITE(afile->vnode, uio, IO_UNIT, afs_osi_ctxtp);
390         resid = AFS_UIO_RESID(uio);
391         uio_free(uio);
392 #else
393         code =
394             gop_rdwr(UIO_WRITE, afile->vnode, (caddr_t) aptr, asize,
395                      afile->offset, AFS_UIOSYS, IO_UNIT, &afs_osi_cred,
396                      &resid);
397 #endif
398         AFS_GLOCK();
399     }
400     if (code == 0) {
401         code = asize - resid;
402         afile->offset += code;
403     } else {
404         code = -1;
405     }
406     if (afile->proc) {
407         (*afile->proc) (afile, code);
408     }
409     return code;
410 }
411
412
413
414
415
416 void
417 shutdown_osifile(void)
418 {
419     AFS_STATCNT(shutdown_osifile);
420     if (afs_cold_shutdown) {
421         afs_osicred_initialized = 0;
422     }
423 }