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