84f6bf0f5f1bec011ca9e70a69622707f1205290
[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 "../afs/param.h"       /* Should be always first */
11 #include <afsconfig.h>
12
13 RCSID("$Header$");
14
15 #include "../afs/sysincludes.h" /* Standard vendor system headers */
16 #include "../afs/afsincludes.h" /* Afs-based standard headers */
17 #include "../afs/afs_stats.h"  /* afs statistics */
18 #include "../afs/osi_inode.h"
19
20
21 int afs_osicred_initialized=0;
22 struct  AFS_UCRED afs_osi_cred;
23 afs_lock_t afs_xosi;            /* lock is for tvattr */
24 extern struct osi_dev cacheDev;
25 extern struct mount *afs_cacheVfsp;
26 int afs_CacheFSType = -1;
27
28 /* Initialize the cache operations. Called while initializing cache files. */
29 void afs_InitDualFSCacheOps(struct vnode *vp)
30 {
31     int code;
32     static int inited = 0;
33
34     if (inited)
35         return;
36     inited = 1;
37
38     if (vp == NULL)
39         return;
40     if (strncmp("hfs", vp->v_mount->mnt_vfc->vfc_name, 3) == 0)
41        afs_CacheFSType = AFS_APPL_HFS_CACHE;
42     else 
43     if (strncmp("ufs", vp->v_mount->mnt_vfc->vfc_name, 3) == 0)
44        afs_CacheFSType = AFS_APPL_UFS_CACHE;
45     else
46        osi_Panic("Unknown cache vnode type\n");
47 }
48
49 ino_t VnodeToIno(vnode_t *avp)
50 {
51    unsigned long ret;
52
53    if (afs_CacheFSType == AFS_APPL_UFS_CACHE) {
54       struct inode *ip = VTOI(avp);
55       ret=ip->i_number;
56    } else if (afs_CacheFSType == AFS_APPL_HFS_CACHE) {
57 #ifndef VTOH
58       struct vattr va;
59       if (VOP_GETATTR(avp, &va, &afs_osi_cred, current_proc()))
60          osi_Panic("VOP_GETATTR failed in VnodeToIno\n"); 
61       ret=va.va_fileid;
62 #else
63       struct hfsnode *hp = VTOH(avp);
64       ret=H_FILEID(hp);
65 #endif
66    } else
67        osi_Panic("VnodeToIno called before cacheops initialized\n");
68    return ret;
69 }
70
71
72 dev_t VnodeToDev(vnode_t *avp)
73 {
74
75     
76    if (afs_CacheFSType == AFS_APPL_UFS_CACHE) {
77       struct inode *ip = VTOI(avp);
78       return ip->i_dev;
79    } else
80    if (afs_CacheFSType == AFS_APPL_HFS_CACHE) {
81 #ifndef VTOH /* slow, but works */
82       struct vattr va;
83       if (VOP_GETATTR(avp, &va, &afs_osi_cred, current_proc()))
84          osi_Panic("VOP_GETATTR failed in VnodeToDev\n"); 
85       return va.va_fsid; /* XXX they say it's the dev.... */
86 #else
87       struct hfsnode *hp = VTOH(avp);
88       return H_DEV(hp);
89 #endif
90    } else
91        osi_Panic("VnodeToDev called before cacheops initialized\n");
92 }
93
94 void *osi_UFSOpen(ainode)
95     afs_int32 ainode;
96 {
97     struct vnode *vp;
98     struct vattr va;
99     register struct osi_file *afile = NULL;
100     extern int cacheDiskType;
101     afs_int32 code = 0;
102     int dummy;
103     AFS_STATCNT(osi_UFSOpen);
104     if(cacheDiskType != AFS_FCACHE_TYPE_UFS) {
105         osi_Panic("UFSOpen called for non-UFS cache\n");
106     }
107     if (!afs_osicred_initialized) {
108         /* valid for alpha_osf, SunOS, Ultrix */
109         bzero((char *)&afs_osi_cred, sizeof(struct AFS_UCRED));
110         afs_osi_cred.cr_ref++;
111         afs_osi_cred.cr_ngroups=1;
112         afs_osicred_initialized = 1;
113     }
114     afile = (struct osi_file *) osi_AllocSmallSpace(sizeof(struct osi_file));
115     AFS_GUNLOCK();
116     if (afs_CacheFSType == AFS_APPL_HFS_CACHE) 
117        code = igetinode(afs_cacheVfsp, (dev_t) cacheDev.dev, &ainode, &vp, &va, &dummy); /* XXX hfs is broken */
118     else
119         if (afs_CacheFSType == AFS_APPL_UFS_CACHE)
120             code = igetinode(afs_cacheVfsp, (dev_t) cacheDev.dev, (ino_t)ainode, &vp, &va, &dummy);
121         else
122             panic("osi_UFSOpen called before cacheops initialized\n");
123     AFS_GLOCK();
124     if (code) {
125         osi_FreeSmallSpace(afile);
126         osi_Panic("UFSOpen: igetinode failed");
127     }
128     afile->vnode = vp;
129     afile->size = va.va_size;
130     afile->offset = 0;
131     afile->proc = (int (*)()) 0;
132     afile->inum = ainode;        /* for hint validity checking */
133     return (void *)afile;
134 }
135
136 afs_osi_Stat(afile, astat)
137     register struct osi_file *afile;
138     register struct osi_stat *astat; {
139     register afs_int32 code;
140     struct vattr tvattr;
141     AFS_STATCNT(osi_Stat);
142     MObtainWriteLock(&afs_xosi,320);
143     AFS_GUNLOCK();
144     code=VOP_GETATTR(afile->vnode, &tvattr, &afs_osi_cred, current_proc());
145     AFS_GLOCK();
146     if (code == 0) {
147         astat->size = tvattr.va_size;
148         astat->blksize = tvattr.va_blocksize;
149         astat->mtime = tvattr.va_mtime.tv_sec;
150         astat->atime = tvattr.va_atime.tv_sec;
151     }
152     MReleaseWriteLock(&afs_xosi);
153     return code;
154 }
155
156 osi_UFSClose(afile)
157      register struct osi_file *afile;
158   {
159       AFS_STATCNT(osi_Close);
160       if(afile->vnode) {
161         AFS_RELE(afile->vnode);
162       }
163       
164       osi_FreeSmallSpace(afile);
165       return 0;
166   }
167
168 osi_UFSTruncate(afile, asize)
169     register struct osi_file *afile;
170     afs_int32 asize; {
171     struct AFS_UCRED *oldCred;
172     struct vattr tvattr;
173     register afs_int32 code;
174     struct osi_stat tstat;
175     AFS_STATCNT(osi_Truncate);
176
177     /* This routine only shrinks files, and most systems
178      * have very slow truncates, even when the file is already
179      * small enough.  Check now and save some time.
180      */
181     code = afs_osi_Stat(afile, &tstat);
182     if (code || tstat.size <= asize) return code;
183     MObtainWriteLock(&afs_xosi,321);    
184     VATTR_NULL(&tvattr);
185     tvattr.va_size = asize;
186     AFS_GUNLOCK();
187     code=VOP_SETATTR(afile->vnode, &tvattr, &afs_osi_cred, current_proc());
188     AFS_GLOCK();
189     MReleaseWriteLock(&afs_xosi);
190     return code;
191 }
192
193 void osi_DisableAtimes(avp)
194 struct vnode *avp;
195 {
196
197
198    if (afs_CacheFSType == AFS_APPL_UFS_CACHE) {
199       struct inode *ip = VTOI(avp);
200       ip->i_flag &= ~IN_ACCESS;
201    }
202 #ifdef VTOH /* can't do this without internals */
203    else if (afs_CacheFSType == AFS_APPL_HFS_CACHE) {
204       struct hfsnode *hp = VTOH(avp);
205       hp->h_nodeflags &= ~IN_ACCESS;
206    }
207 #endif
208 }
209
210
211 /* Generic read interface */
212 afs_osi_Read(afile, offset, aptr, asize)
213     register struct osi_file *afile;
214     int offset;
215     char *aptr;
216     afs_int32 asize; {
217     struct AFS_UCRED *oldCred;
218     unsigned int resid;
219     register afs_int32 code;
220     AFS_STATCNT(osi_Read);
221
222     /**
223       * If the osi_file passed in is NULL, panic only if AFS is not shutting
224       * down. No point in crashing when we are already shutting down
225       */
226     if ( !afile ) {
227         if ( !afs_shuttingdown )
228             osi_Panic("osi_Read called with null param");
229         else
230             return EIO;
231     }
232
233     if (offset != -1) afile->offset = offset;
234     AFS_GUNLOCK();
235     code = gop_rdwr(UIO_READ, afile->vnode, (caddr_t) aptr, asize, afile->offset,
236                   AFS_UIOSYS, IO_UNIT, &afs_osi_cred, &resid);
237     AFS_GLOCK();
238     if (code == 0) {
239         code = asize - resid;
240         afile->offset += code;
241         osi_DisableAtimes(afile->vnode);
242     }
243     else {
244         afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, resid,
245                  ICL_TYPE_INT32, code);
246         code = -1;
247     }
248     return code;
249 }
250
251 /* Generic write interface */
252 afs_osi_Write(afile, offset, aptr, asize)
253     register struct osi_file *afile;
254     char *aptr;
255     afs_int32 offset;
256     afs_int32 asize; {
257     struct AFS_UCRED *oldCred;
258     unsigned int resid;
259     register afs_int32 code;
260     AFS_STATCNT(osi_Write);
261     if ( !afile )
262         osi_Panic("afs_osi_Write called with null param");
263     if (offset != -1) afile->offset = offset;
264     {
265         AFS_GUNLOCK();
266         code = gop_rdwr(UIO_WRITE, afile->vnode, (caddr_t) aptr, asize, afile->offset,
267                   AFS_UIOSYS, IO_UNIT, &afs_osi_cred, &resid);
268         AFS_GLOCK();
269     }
270     if (code == 0) {
271         code = asize - resid;
272         afile->offset += code;
273     }
274     else {
275         code = -1;
276     }
277     if (afile->proc) {
278         (*afile->proc)(afile, code);
279     }
280     return code;
281 }
282
283
284
285
286
287 void
288 shutdown_osifile()
289 {
290   extern int afs_cold_shutdown;
291
292   AFS_STATCNT(shutdown_osifile);
293   if (afs_cold_shutdown) {
294     afs_osicred_initialized = 0;
295   }
296 }
297