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