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