a83dbf2e5787cd3bba877d571ec49a4c33d547bf
[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     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 #ifndef AFS_DARWIN110_ENV
154         afs_osi_cred.cr_ngroups = 1;
155 #endif
156         afs_osicred_initialized = 1;
157     }
158     afile = osi_AllocSmallSpace(sizeof(struct osi_file));
159     AFS_GUNLOCK();
160 #ifdef AFS_CACHE_VNODE_PATH
161     if (!ainode->ufs) {
162         osi_Panic("No cache inode\n");
163     }
164
165     code = vnode_open(ainode->ufs, O_RDWR, 0, 0, &vp, afs_osi_ctxtp);
166 #else
167 #ifndef AFS_DARWIN80_ENV
168     if (afs_CacheFSType == AFS_APPL_HFS_CACHE)
169         code = igetinode(afs_cacheVfsp, (dev_t) cacheDev.dev, &ainode->ufs, &vp, &va, &dummy);  /* XXX hfs is broken */
170     else if (afs_CacheFSType == AFS_APPL_UFS_CACHE)
171 #endif
172         code =
173             igetinode(afs_cacheVfsp, (dev_t) cacheDev.dev, (ino_t) ainode->ufs,
174                       &vp, &va, &dummy);
175 #ifndef AFS_DARWIN80_ENV
176     else
177         panic("osi_UFSOpen called before cacheops initialized\n");
178 #endif
179 #endif
180     AFS_GLOCK();
181     if (code) {
182         osi_FreeSmallSpace(afile);
183         osi_Panic("UFSOpen: igetinode failed");
184     }
185     afile->vnode = vp;
186     afile->offset = 0;
187     afile->proc = (int (*)())0;
188 #ifndef AFS_CACHE_VNODE_PATH
189     afile->size = va.va_size;
190 #else
191     code = afs_osi_Stat(afile, &tstat);
192     afile->size = tstat.size;
193 #endif
194     return (void *)afile;
195 }
196
197 int
198 afs_osi_Stat(struct osi_file *afile, struct osi_stat *astat)
199 {
200     afs_int32 code;
201     struct vattr tvattr;
202     AFS_STATCNT(osi_Stat);
203     ObtainWriteLock(&afs_xosi, 320);
204     AFS_GUNLOCK();
205 #ifdef AFS_DARWIN80_ENV
206     VATTR_INIT(&tvattr);
207     VATTR_WANTED(&tvattr, va_size);
208     VATTR_WANTED(&tvattr, va_blocksize);
209     VATTR_WANTED(&tvattr, va_mtime);
210     VATTR_WANTED(&tvattr, va_atime);
211     code = vnode_getattr(afile->vnode, &tvattr, afs_osi_ctxtp);
212     if (code == 0 && !VATTR_ALL_SUPPORTED(&tvattr))
213        code = EINVAL;
214 #else
215     code = VOP_GETATTR(afile->vnode, &tvattr, &afs_osi_cred, current_proc());
216 #endif
217     AFS_GLOCK();
218     if (code == 0) {
219         astat->size = tvattr.va_size;
220         astat->mtime = tvattr.va_mtime.tv_sec;
221         astat->atime = tvattr.va_atime.tv_sec;
222     }
223     ReleaseWriteLock(&afs_xosi);
224     return code;
225 }
226
227 int
228 osi_UFSClose(struct osi_file *afile)
229 {
230     AFS_STATCNT(osi_Close);
231     if (afile->vnode) {
232 #ifdef AFS_DARWIN80_ENV
233         vnode_close(afile->vnode, O_RDWR, afs_osi_ctxtp);
234 #else
235         AFS_RELE(afile->vnode);
236 #endif
237     }
238
239     osi_FreeSmallSpace(afile);
240     return 0;
241 }
242
243 int
244 osi_UFSTruncate(struct osi_file *afile, afs_int32 asize)
245 {
246     afs_ucred_t *oldCred;
247     struct vattr tvattr;
248     afs_int32 code;
249     struct osi_stat tstat;
250     AFS_STATCNT(osi_Truncate);
251
252     /* This routine only shrinks files, and most systems
253      * have very slow truncates, even when the file is already
254      * small enough.  Check now and save some time.
255      */
256     code = afs_osi_Stat(afile, &tstat);
257     if (code || tstat.size <= asize)
258         return code;
259     ObtainWriteLock(&afs_xosi, 321);
260     AFS_GUNLOCK();
261 #ifdef AFS_DARWIN80_ENV
262     VATTR_INIT(&tvattr);
263     VATTR_SET(&tvattr, va_size, asize);
264     code = vnode_setattr(afile->vnode, &tvattr, afs_osi_ctxtp);
265 #else
266     VATTR_NULL(&tvattr);
267     tvattr.va_size = asize;
268     code = VOP_SETATTR(afile->vnode, &tvattr, &afs_osi_cred, current_proc());
269 #endif
270     AFS_GLOCK();
271     ReleaseWriteLock(&afs_xosi);
272     return code;
273 }
274
275 void
276 #ifdef AFS_DARWIN80_ENV
277 osi_DisableAtimes(vnode_t avp)
278 #else
279 osi_DisableAtimes(struct vnode *avp)
280 #endif
281 {
282 #ifdef AFS_DARWIN80_ENV
283     struct vnode_attr vap;
284
285     VATTR_INIT(&vap);
286     VATTR_CLEAR_SUPPORTED(&vap, va_access_time);
287     vnode_setattr(avp, &vap, afs_osi_ctxtp);
288 #else
289     if (afs_CacheFSType == AFS_APPL_UFS_CACHE) {
290         struct inode *ip = VTOI(avp);
291         ip->i_flag &= ~IN_ACCESS;
292     }
293 #ifdef VTOH                     /* can't do this without internals */
294     else if (afs_CacheFSType == AFS_APPL_HFS_CACHE) {
295         struct hfsnode *hp = VTOH(avp);
296         hp->h_nodeflags &= ~IN_ACCESS;
297     }
298 #endif
299 #endif
300 }
301
302
303 /* Generic read interface */
304 int
305 afs_osi_Read(struct osi_file *afile, int offset, void *aptr,
306              afs_int32 asize)
307 {
308     afs_ucred_t *oldCred;
309     afs_size_t resid;
310     afs_int32 code;
311 #ifdef AFS_DARWIN80_ENV
312     uio_t uio;
313 #endif
314     AFS_STATCNT(osi_Read);
315
316     /**
317       * If the osi_file passed in is NULL, panic only if AFS is not shutting
318       * down. No point in crashing when we are already shutting down
319       */
320     if (!afile) {
321         if (afs_shuttingdown == AFS_RUNNING)
322             osi_Panic("osi_Read called with null param");
323         else
324             return -EIO;
325     }
326
327     if (offset != -1)
328         afile->offset = offset;
329     AFS_GUNLOCK();
330 #ifdef AFS_DARWIN80_ENV
331     uio=uio_create(1, afile->offset, AFS_UIOSYS, UIO_READ);
332     uio_addiov(uio, CAST_USER_ADDR_T(aptr), asize);
333     code = VNOP_READ(afile->vnode, uio, IO_UNIT, afs_osi_ctxtp);
334     resid = AFS_UIO_RESID(uio);
335     uio_free(uio);
336 #else
337     code =
338         gop_rdwr(UIO_READ, afile->vnode, (caddr_t) aptr, asize, afile->offset,
339                  AFS_UIOSYS, IO_UNIT, &afs_osi_cred, &resid);
340 #endif
341     AFS_GLOCK();
342     if (code == 0) {
343         code = asize - resid;
344         afile->offset += code;
345         osi_DisableAtimes(afile->vnode);
346     } else {
347         afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, resid,
348                    ICL_TYPE_INT32, code);
349         if (code > 0) {
350             code = -code;
351         }
352     }
353     return code;
354 }
355
356 /* Generic write interface */
357 int
358 afs_osi_Write(struct osi_file *afile, afs_int32 offset, void *aptr,
359               afs_int32 asize)
360 {
361     afs_ucred_t *oldCred;
362     afs_size_t resid;
363     afs_int32 code;
364 #ifdef AFS_DARWIN80_ENV
365     uio_t uio;
366 #endif
367     AFS_STATCNT(osi_Write);
368     if (!afile)
369         osi_Panic("afs_osi_Write called with null param");
370     if (offset != -1)
371         afile->offset = offset;
372     {
373         AFS_GUNLOCK();
374 #ifdef AFS_DARWIN80_ENV
375         uio=uio_create(1, afile->offset, AFS_UIOSYS, UIO_WRITE);
376         uio_addiov(uio, CAST_USER_ADDR_T(aptr), asize);
377         code = VNOP_WRITE(afile->vnode, uio, IO_UNIT, afs_osi_ctxtp);
378         resid = AFS_UIO_RESID(uio);
379         uio_free(uio);
380 #else
381         code =
382             gop_rdwr(UIO_WRITE, afile->vnode, (caddr_t) aptr, asize,
383                      afile->offset, AFS_UIOSYS, IO_UNIT, &afs_osi_cred,
384                      &resid);
385 #endif
386         AFS_GLOCK();
387     }
388     if (code == 0) {
389         code = asize - resid;
390         afile->offset += code;
391     } else {
392         if (code > 0) {
393             code = -code;
394         }
395     }
396     if (afile->proc) {
397         (*afile->proc) (afile, code);
398     }
399     return code;
400 }
401
402
403
404
405
406 void
407 shutdown_osifile(void)
408 {
409     AFS_STATCNT(shutdown_osifile);
410     if (afs_cold_shutdown) {
411         afs_osicred_initialized = 0;
412     }
413 }