pull-prototypes-to-head-20020821
[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("$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(afs_int32 ainode)
95 {
96     struct vnode *vp;
97     struct vattr va;
98     register struct osi_file *afile = NULL;
99     extern int cacheDiskType;
100     afs_int32 code = 0;
101     int dummy;
102     AFS_STATCNT(osi_UFSOpen);
103     if(cacheDiskType != AFS_FCACHE_TYPE_UFS) {
104         osi_Panic("UFSOpen called for non-UFS cache\n");
105     }
106     if (!afs_osicred_initialized) {
107         /* valid for alpha_osf, SunOS, Ultrix */
108         memset((char *)&afs_osi_cred, 0, sizeof(struct AFS_UCRED));
109         afs_osi_cred.cr_ref++;
110         afs_osi_cred.cr_ngroups=1;
111         afs_osicred_initialized = 1;
112     }
113     afile = (struct osi_file *) osi_AllocSmallSpace(sizeof(struct osi_file));
114     AFS_GUNLOCK();
115     if (afs_CacheFSType == AFS_APPL_HFS_CACHE) 
116        code = igetinode(afs_cacheVfsp, (dev_t) cacheDev.dev, &ainode, &vp, &va, &dummy); /* XXX hfs is broken */
117     else
118         if (afs_CacheFSType == AFS_APPL_UFS_CACHE)
119             code = igetinode(afs_cacheVfsp, (dev_t) cacheDev.dev, (ino_t)ainode, &vp, &va, &dummy);
120         else
121             panic("osi_UFSOpen called before cacheops initialized\n");
122     AFS_GLOCK();
123     if (code) {
124         osi_FreeSmallSpace(afile);
125         osi_Panic("UFSOpen: igetinode failed");
126     }
127     afile->vnode = vp;
128     afile->size = va.va_size;
129     afile->offset = 0;
130     afile->proc = (int (*)()) 0;
131     afile->inum = ainode;        /* for hint validity checking */
132     return (void *)afile;
133 }
134
135 int afs_osi_Stat(register struct osi_file *afile, register struct osi_stat *astat)
136 {
137     register afs_int32 code;
138     struct vattr tvattr;
139     AFS_STATCNT(osi_Stat);
140     MObtainWriteLock(&afs_xosi,320);
141     AFS_GUNLOCK();
142     code=VOP_GETATTR(afile->vnode, &tvattr, &afs_osi_cred, current_proc());
143     AFS_GLOCK();
144     if (code == 0) {
145         astat->size = tvattr.va_size;
146         astat->blksize = tvattr.va_blocksize;
147         astat->mtime = tvattr.va_mtime.tv_sec;
148         astat->atime = tvattr.va_atime.tv_sec;
149     }
150     MReleaseWriteLock(&afs_xosi);
151     return code;
152 }
153
154 int osi_UFSClose(register struct osi_file *afile)
155 {
156       AFS_STATCNT(osi_Close);
157       if(afile->vnode) {
158         AFS_RELE(afile->vnode);
159       }
160       
161       osi_FreeSmallSpace(afile);
162       return 0;
163 }
164
165 int osi_UFSTruncate(register struct osi_file *afile, afs_int32 asize)
166 {
167     struct AFS_UCRED *oldCred;
168     struct vattr tvattr;
169     register afs_int32 code;
170     struct osi_stat tstat;
171     AFS_STATCNT(osi_Truncate);
172
173     /* This routine only shrinks files, and most systems
174      * have very slow truncates, even when the file is already
175      * small enough.  Check now and save some time.
176      */
177     code = afs_osi_Stat(afile, &tstat);
178     if (code || tstat.size <= asize) return code;
179     MObtainWriteLock(&afs_xosi,321);    
180     VATTR_NULL(&tvattr);
181     tvattr.va_size = asize;
182     AFS_GUNLOCK();
183     code=VOP_SETATTR(afile->vnode, &tvattr, &afs_osi_cred, current_proc());
184     AFS_GLOCK();
185     MReleaseWriteLock(&afs_xosi);
186     return code;
187 }
188
189 void osi_DisableAtimes(struct vnode *avp)
190 {
191
192
193    if (afs_CacheFSType == AFS_APPL_UFS_CACHE) {
194       struct inode *ip = VTOI(avp);
195       ip->i_flag &= ~IN_ACCESS;
196    }
197 #ifdef VTOH /* can't do this without internals */
198    else if (afs_CacheFSType == AFS_APPL_HFS_CACHE) {
199       struct hfsnode *hp = VTOH(avp);
200       hp->h_nodeflags &= ~IN_ACCESS;
201    }
202 #endif
203 }
204
205
206 /* Generic read interface */
207 int afs_osi_Read(register struct osi_file *afile, int offset, void *aptr, afs_int32 asize)
208 {
209     struct AFS_UCRED *oldCred;
210     unsigned int resid;
211     register afs_int32 code;
212     AFS_STATCNT(osi_Read);
213
214     /**
215       * If the osi_file passed in is NULL, panic only if AFS is not shutting
216       * down. No point in crashing when we are already shutting down
217       */
218     if ( !afile ) {
219         if ( !afs_shuttingdown )
220             osi_Panic("osi_Read called with null param");
221         else
222             return EIO;
223     }
224
225     if (offset != -1) afile->offset = offset;
226     AFS_GUNLOCK();
227     code = gop_rdwr(UIO_READ, afile->vnode, (caddr_t) aptr, asize, afile->offset,
228                   AFS_UIOSYS, IO_UNIT, &afs_osi_cred, &resid);
229     AFS_GLOCK();
230     if (code == 0) {
231         code = asize - resid;
232         afile->offset += code;
233         osi_DisableAtimes(afile->vnode);
234     }
235     else {
236         afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, resid,
237                  ICL_TYPE_INT32, code);
238         code = -1;
239     }
240     return code;
241 }
242
243 /* Generic write interface */
244 int afs_osi_Write(register struct osi_file *afile, afs_int32 offset, void *aptr, afs_int32 asize)
245 {
246     struct AFS_UCRED *oldCred;
247     unsigned int resid;
248     register afs_int32 code;
249     AFS_STATCNT(osi_Write);
250     if ( !afile )
251         osi_Panic("afs_osi_Write called with null param");
252     if (offset != -1) afile->offset = offset;
253     {
254         AFS_GUNLOCK();
255         code = gop_rdwr(UIO_WRITE, afile->vnode, (caddr_t) aptr, asize, afile->offset,
256                   AFS_UIOSYS, IO_UNIT, &afs_osi_cred, &resid);
257         AFS_GLOCK();
258     }
259     if (code == 0) {
260         code = asize - resid;
261         afile->offset += code;
262     }
263     else {
264         code = -1;
265     }
266     if (afile->proc) {
267         (*afile->proc)(afile, code);
268     }
269     return code;
270 }
271
272
273
274
275
276 void shutdown_osifile(void)
277 {
278   extern int afs_cold_shutdown;
279
280   AFS_STATCNT(shutdown_osifile);
281   if (afs_cold_shutdown) {
282     afs_osicred_initialized = 0;
283   }
284 }
285