Revert "osi_UFSOpen returns struct osi_file *"
[openafs.git] / src / afs / NBSD / 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
14 #include "afs/sysincludes.h"    /* Standard vendor system headers */
15 #include "afsincludes.h"        /* Afs-based standard headers */
16 #include "afs/afs_stats.h"      /* afs statistics */
17
18
19 int afs_osicred_initialized = 0;
20 afs_ucred_t afs_osi_cred;
21 afs_lock_t afs_xosi;            /* lock is for tvattr */
22 extern struct osi_dev cacheDev;
23 extern struct mount *afs_cacheVfsp;
24
25
26 void *
27 osi_UFSOpen(afs_dcache_id_t *ainode)
28 {
29     struct inode *ip;
30     register struct osi_file *afile = NULL;
31     extern int cacheDiskType;
32     afs_int32 code = 0;
33     int dummy;
34     AFS_STATCNT(osi_UFSOpen);
35     if (cacheDiskType != AFS_FCACHE_TYPE_UFS)
36         osi_Panic("UFSOpen called for non-UFS cache\n");
37     afile = (struct osi_file *)osi_AllocSmallSpace(sizeof(struct osi_file));
38     AFS_GUNLOCK();
39     code =
40         igetinode(afs_cacheVfsp, (dev_t) cacheDev.dev, ainode->ufs, &ip,
41                   &dummy);
42     AFS_GLOCK();
43     if (code) {
44         osi_FreeSmallSpace(afile);
45         osi_Panic("UFSOpen: igetinode failed");
46     }
47     IN_UNLOCK(ip);
48     afile->vnode = ITOV(ip);
49     afile->size = VTOI(afile->vnode)->i_size;
50     afile->offset = 0;
51     afile->proc = (int (*)())0;
52     return (void *)afile;
53 }
54
55 int
56 afs_osi_Stat(register struct osi_file *afile, register struct osi_stat *astat)
57 {
58     register afs_int32 code;
59     struct vattr tvattr;
60     AFS_STATCNT(osi_Stat);
61     ObtainWriteLock(&afs_xosi, 320);
62     AFS_GUNLOCK();
63     VOP_GETATTR(afile->vnode, &tvattr, afs_osi_credp, code);
64     AFS_GLOCK();
65     if (code == 0) {
66         astat->size = tvattr.va_size;
67         astat->mtime = tvattr.va_mtime.tv_sec;
68         astat->atime = tvattr.va_atime.tv_sec;
69     }
70     ReleaseWriteLock(&afs_xosi);
71     return code;
72 }
73
74 int
75 osi_UFSClose(register struct osi_file *afile)
76 {
77     AFS_STATCNT(osi_Close);
78     if (afile->vnode) {
79         AFS_RELE(afile->vnode);
80     }
81
82     osi_FreeSmallSpace(afile);
83     return 0;
84 }
85
86 int
87 osi_UFSTruncate(register struct osi_file *afile, afs_int32 asize)
88 {
89     afs_ucred_t *oldCred;
90     struct vattr tvattr;
91     register afs_int32 code;
92     struct osi_stat tstat;
93     AFS_STATCNT(osi_Truncate);
94
95     /* This routine only shrinks files, and most systems
96      * have very slow truncates, even when the file is already
97      * small enough.  Check now and save some time.
98      */
99     code = afs_osi_Stat(afile, &tstat);
100     if (code || tstat.size <= asize)
101         return code;
102     ObtainWriteLock(&afs_xosi, 321);
103     VATTR_NULL(&tvattr);
104     /* note that this credential swapping stuff is only necessary because
105      * of ufs's references directly to cred instead of to
106      * credentials parameter.  Probably should fix ufs some day. */
107     oldCred = curproc->p_cred->pc_ucred;        /* remember old credentials pointer  */
108     curproc->p_cred->pc_ucred = afs_osi_credp;
109     /* temporarily use superuser credentials */
110     tvattr.va_size = asize;
111     AFS_GUNLOCK();
112     VOP_SETATTR(afile->vnode, &tvattr, afs_osi_credp, code);
113     AFS_GLOCK();
114     curproc->p_cred->pc_ucred = oldCred;        /* restore */
115     ReleaseWriteLock(&afs_xosi);
116     return code;
117 }
118
119 void
120 osi_DisableAtimes(struct vnode *avp)
121 {
122     struct inode *ip = VTOI(avp);
123     ip->i_flag &= ~IACC;
124 }
125
126
127 /* Generic read interface */
128 int
129 afs_osi_Read(register struct osi_file *afile, int offset, void *aptr,
130              afs_int32 asize)
131 {
132     afs_ucred_t *oldCred;
133     unsigned int resid;
134     register afs_int32 code;
135     register afs_int32 cnt1 = 0;
136     AFS_STATCNT(osi_Read);
137
138     /**
139       * If the osi_file passed in is NULL, panic only if AFS is not shutting
140       * down. No point in crashing when we are already shutting down
141       */
142     if (!afile) {
143         if (!afs_shuttingdown)
144             osi_Panic("osi_Read called with null param");
145         else
146             return EIO;
147     }
148
149     if (offset != -1)
150         afile->offset = offset;
151     AFS_GUNLOCK();
152     code =
153         gop_rdwr(UIO_READ, afile->vnode, (caddr_t) aptr, asize, afile->offset,
154                  AFS_UIOSYS, IO_UNIT, afs_osi_credp, &resid);
155     AFS_GLOCK();
156     if (code == 0) {
157         code = asize - resid;
158         afile->offset += code;
159         osi_DisableAtimes(afile->vnode);
160     } else {
161         afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, resid,
162                    ICL_TYPE_INT32, code);
163         code = -1;
164     }
165     return code;
166 }
167
168 /* Generic write interface */
169 int
170 afs_osi_Write(register struct osi_file *afile, afs_int32 offset, void *aptr,
171               afs_int32 asize)
172 {
173     afs_ucred_t *oldCred;
174     unsigned int resid;
175     register afs_int32 code;
176     AFS_STATCNT(osi_Write);
177     if (!afile)
178         osi_Panic("afs_osi_Write called with null param");
179     if (offset != -1)
180         afile->offset = offset;
181     {
182         struct ucred *tmpcred = curproc->p_cred->pc_ucred;
183         curproc->p_cred->pc_ucred = afs_osi_credp;
184         AFS_GUNLOCK();
185         code =
186             gop_rdwr(UIO_WRITE, afile->vnode, (caddr_t) aptr, asize,
187                      afile->offset, AFS_UIOSYS, IO_UNIT, afs_osi_credp,
188                      &resid);
189         AFS_GLOCK();
190         curproc->p_cred->pc_ucred = tmpcred;
191     }
192     if (code == 0) {
193         code = asize - resid;
194         afile->offset += code;
195     } else {
196         code = -1;
197     }
198     if (afile->proc) {
199         (*afile->proc) (afile, code);
200     }
201     return code;
202 }
203
204
205 /*  This work should be handled by physstrat in ca/machdep.c.
206     This routine written from the RT NFS port strategy routine.
207     It has been generalized a bit, but should still be pretty clear. */
208 int
209 afs_osi_MapStrategy(int (*aproc) (), register struct buf *bp)
210 {
211     afs_int32 returnCode;
212
213     AFS_STATCNT(osi_MapStrategy);
214     returnCode = (*aproc) (bp);
215
216     return returnCode;
217 }
218
219
220
221 void
222 shutdown_osifile(void)
223 {
224     AFS_STATCNT(shutdown_osifile);
225     if (afs_cold_shutdown) {
226         afs_osicred_initialized = 0;
227     }
228 }