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