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