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