pull-prototypes-to-head-20020821
[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("$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
19
20 int afs_osicred_initialized=0;
21 struct  AFS_UCRED afs_osi_cred;
22 afs_lock_t afs_xosi;            /* lock is for tvattr */
23 extern struct osi_dev cacheDev;
24 extern struct mount *afs_cacheVfsp;
25
26
27 void *osi_UFSOpen(afs_int32 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     }
38     if (!afs_osicred_initialized) {
39         /* valid for alpha_osf, SunOS, Ultrix */
40         memset((char *)&afs_osi_cred, 0, sizeof(struct AFS_UCRED));
41         afs_osi_cred.cr_ref++;
42         afs_osicred_initialized = 1;
43     }
44     afile = (struct osi_file *) osi_AllocSmallSpace(sizeof(struct osi_file));
45     AFS_GUNLOCK();
46     code = igetinode(afs_cacheVfsp, (dev_t) cacheDev.dev, (ino_t)ainode, &ip, &dummy);
47     AFS_GLOCK();
48     if (code) {
49         osi_FreeSmallSpace(afile);
50         osi_Panic("UFSOpen: igetinode failed");
51     }
52     IN_UNLOCK(ip);
53     afile->vnode = ITOV(ip);
54     afile->size = VTOI(afile->vnode)->i_size;
55     afile->offset = 0;
56     afile->proc = (int (*)()) 0;
57     afile->inum = ainode;        /* for hint validity checking */
58     return (void *)afile;
59 }
60
61 int afs_osi_Stat(register struct osi_file *afile, register struct osi_stat *astat)
62 {
63     register afs_int32 code;
64     struct vattr tvattr;
65     AFS_STATCNT(osi_Stat);
66     MObtainWriteLock(&afs_xosi,320);
67     AFS_GUNLOCK();
68     VOP_GETATTR(afile->vnode, &tvattr, &afs_osi_cred, code);
69     AFS_GLOCK();
70     if (code == 0) {
71         astat->size = tvattr.va_size;
72         astat->blksize = tvattr.va_blocksize;
73         astat->mtime = tvattr.va_mtime.tv_sec;
74         astat->atime = tvattr.va_atime.tv_sec;
75     }
76     MReleaseWriteLock(&afs_xosi);
77     return code;
78 }
79
80 int osi_UFSClose(register struct osi_file *afile)
81   {
82       AFS_STATCNT(osi_Close);
83       if(afile->vnode) {
84         AFS_RELE(afile->vnode);
85       }
86       
87       osi_FreeSmallSpace(afile);
88       return 0;
89   }
90
91 int 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) return code;
105     MObtainWriteLock(&afs_xosi,321);    
106     VATTR_NULL(&tvattr);
107     /* note that this credential swapping stuff is only necessary because
108         of ufs's references directly to cred instead of to
109         credentials parameter.  Probably should fix ufs some day. */
110     oldCred = curproc->p_cred->pc_ucred; /* remember old credentials pointer  */
111     curproc->p_cred->pc_ucred = &afs_osi_cred;
112     /* temporarily use superuser credentials */
113     tvattr.va_size = asize;
114     AFS_GUNLOCK();
115     VOP_SETATTR(afile->vnode, &tvattr, &afs_osi_cred, code);
116     AFS_GLOCK();
117     curproc->p_cred->pc_ucred = oldCred;     /* restore */
118     MReleaseWriteLock(&afs_xosi);
119     return code;
120 }
121
122 void osi_DisableAtimes(struct vnode *avp)
123 {
124    struct inode *ip = VTOI(avp);
125    ip->i_flag &= ~IACC;
126 }
127
128
129 /* Generic read interface */
130 int afs_osi_Read(register struct osi_file *afile, int offset, void *aptr, afs_int32 asize)
131 {
132     struct AFS_UCRED *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) afile->offset = offset;
150     AFS_GUNLOCK();
151     code = gop_rdwr(UIO_READ, afile->vnode, (caddr_t) aptr, asize, afile->offset,
152                   AFS_UIOSYS, IO_UNIT, &afs_osi_cred, &resid);
153     AFS_GLOCK();
154     if (code == 0) {
155         code = asize - resid;
156         afile->offset += code;
157         osi_DisableAtimes(afile->vnode);
158     }
159     else {
160         afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, resid,
161                  ICL_TYPE_INT32, code);
162         code = -1;
163     }
164     return code;
165 }
166
167 /* Generic write interface */
168 int afs_osi_Write(register struct osi_file *afile, afs_int32 offset, void *aptr, afs_int32 asize)
169 {
170     struct AFS_UCRED *oldCred;
171     unsigned int resid;
172     register afs_int32 code;
173     AFS_STATCNT(osi_Write);
174     if ( !afile )
175         osi_Panic("afs_osi_Write called with null param");
176     if (offset != -1) afile->offset = offset;
177     {
178         struct ucred *tmpcred = curproc->p_cred->pc_ucred;
179         curproc->p_cred->pc_ucred = &afs_osi_cred;
180         AFS_GUNLOCK();
181         code = gop_rdwr(UIO_WRITE, afile->vnode, (caddr_t) aptr, asize, afile->offset,
182                   AFS_UIOSYS, IO_UNIT, &afs_osi_cred, &resid);
183         AFS_GLOCK();
184         curproc->p_cred->pc_ucred = tmpcred;
185     }
186     if (code == 0) {
187         code = asize - resid;
188         afile->offset += code;
189     }
190     else {
191         code = -1;
192     }
193     if (afile->proc) {
194         (*afile->proc)(afile, code);
195     }
196     return code;
197 }
198
199
200 /*  This work should be handled by physstrat in ca/machdep.c.
201     This routine written from the RT NFS port strategy routine.
202     It has been generalized a bit, but should still be pretty clear. */
203 int afs_osi_MapStrategy(int (*aproc)(), register struct buf *bp)
204 {
205     afs_int32 returnCode;
206
207     AFS_STATCNT(osi_MapStrategy);
208     returnCode = (*aproc) (bp);
209
210     return returnCode;
211 }
212
213
214
215 void shutdown_osifile(void)
216 {
217   extern int afs_cold_shutdown;
218
219   AFS_STATCNT(shutdown_osifile);
220   if (afs_cold_shutdown) {
221     afs_osicred_initialized = 0;
222   }
223 }
224