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