openbsd-20030301
[openafs.git] / src / afs / OBSD / 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;
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 osi_file *afile;
30     struct vnode *vp;
31     extern int cacheDiskType;
32     afs_int32 code;
33
34     AFS_STATCNT(osi_UFSOpen);
35     if (cacheDiskType != AFS_FCACHE_TYPE_UFS)
36         osi_Panic("UFSOpen called for non-UFS cache\n");
37     if (!afs_osicred_initialized) {
38         /* valid for alpha_osf, SunOS, Ultrix */
39         memset(&afs_osi_cred, 0, sizeof(struct AFS_UCRED));
40         crhold(&afs_osi_cred);
41         afs_osicred_initialized = 1;
42     }
43     afile = (struct osi_file *) osi_AllocSmallSpace(sizeof(struct osi_file));
44     code = VFS_VGET(cacheDev.mp, (ino_t) ainode, &vp);
45     if (code) {
46         osi_FreeSmallSpace(afile);
47         osi_Panic("UFSOpen: igetinode failed");
48     }
49     afile->vnode = vp;
50     afile->size = VTOI(vp)->i_ffs_size;
51     afile->offset = 0;
52     afile->proc = NULL;
53     afile->inum = ainode;       /* for hint validity checking */
54     VOP_UNLOCK(vp, 0, curproc);
55     return (void *) afile;
56 }
57
58 int afs_osi_Stat(struct osi_file *afile, struct osi_stat *astat)
59 {
60     afs_int32 code;
61     struct vattr tvattr;
62
63     AFS_STATCNT(osi_Stat);
64     MObtainWriteLock(&afs_xosi, 320);
65     AFS_GUNLOCK();
66     code = VOP_GETATTR(afile->vnode, &tvattr, &afs_osi_cred, curproc);
67     AFS_GLOCK();
68     if (code == 0) {
69         astat->size = afile->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 osi_UFSClose(struct osi_file *afile)
79 {
80     AFS_STATCNT(osi_Close);
81
82     if (afile->vnode)
83         AFS_RELE(afile->vnode);
84
85     osi_FreeSmallSpace(afile);
86     return 0;
87 }
88
89 int osi_UFSTruncate(struct osi_file *afile, afs_int32 asize)
90 {
91     struct vattr tvattr;
92     afs_int32 code;
93     struct osi_stat tstat;
94
95     AFS_STATCNT(osi_Truncate);
96
97     /*
98      * This routine only shrinks files, and most systems
99      * have very slow truncates, even when the file is already
100      * small enough.  Check now and save some time.
101      */
102     code = afs_osi_Stat(afile, &tstat);
103     if (code || tstat.size <= asize)
104         return code;
105
106     MObtainWriteLock(&afs_xosi,321);
107     VATTR_NULL(&tvattr);
108     tvattr.va_size = asize;
109     AFS_GUNLOCK();
110     VOP_LOCK(afile->vnode, LK_EXCLUSIVE | LK_RETRY, curproc);
111     code = VOP_SETATTR(afile->vnode, &tvattr, &afs_osi_cred, curproc);
112     VOP_UNLOCK(afile->vnode, 0, curproc);
113     AFS_GLOCK();
114     if (code == 0)
115         afile->size = asize;
116     MReleaseWriteLock(&afs_xosi);
117     return code;
118 }
119
120 void osi_DisableAtimes(struct vnode *avp)
121 {
122 #if 0
123     VTOI(avp)->i_flag &= ~IN_ACCESS;
124 #endif
125 }
126
127
128 /* Generic read interface */
129 int afs_osi_Read(struct osi_file *afile, int offset, void *aptr, afs_int32 asize)
130 {
131     unsigned int resid;
132     afs_int32 code;
133
134     AFS_STATCNT(osi_Read);
135
136     /*
137      * If the osi_file passed in is NULL, panic only if AFS is not shutting
138      * down. No point in crashing when we are already shutting down
139      */
140     if (!afile) {
141         if (!afs_shuttingdown)
142             osi_Panic("osi_Read called with null param");
143         else
144             return EIO;
145     }
146
147     if (offset != -1)
148         afile->offset = offset;
149     AFS_GUNLOCK();
150     code = vn_rdwr(UIO_READ, afile->vnode, aptr, asize, afile->offset,
151                    AFS_UIOSYS, IO_UNIT, &afs_osi_cred, &resid, curproc);
152     AFS_GLOCK();
153     if (code == 0) {
154         code = asize - resid;
155         afile->offset += code;
156         osi_DisableAtimes(afile->vnode);
157     } else {
158         afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, resid,
159                    ICL_TYPE_INT32, code);
160         code = -1;
161     }
162     return code;
163 }
164
165 /* Generic write interface */
166 int afs_osi_Write(struct osi_file *afile, afs_int32 offset, void *aptr, afs_int32 asize)
167 {
168     unsigned int resid;
169     afs_int32 code;
170
171     AFS_STATCNT(osi_Write);
172     if (!afile)
173         osi_Panic("afs_osi_Write called with null afile");
174     if (offset != -1)
175         afile->offset = offset;
176
177     AFS_GUNLOCK();
178     VOP_LOCK(afile->vnode, LK_EXCLUSIVE | LK_RETRY, curproc);
179     code = vn_rdwr(UIO_WRITE, afile->vnode, (caddr_t) aptr, asize, afile->offset,
180                    AFS_UIOSYS, IO_UNIT, &afs_osi_cred, &resid, curproc);
181     VOP_UNLOCK(afile->vnode, 0, curproc);
182     AFS_GLOCK();
183
184     if (code == 0) {
185         code = asize - resid;
186         afile->offset += code;
187         if (afile->offset > afile->size)
188             afile->size = afile->offset;
189     } else
190         code = -1;
191
192     if (afile->proc)
193         (*afile->proc)(afile, code);
194
195     return code;
196 }
197
198 /*
199  * This work should be handled by physstrat in ca/machdep.c.  This routine
200  * written from the RT NFS port strategy routine.  It has been generalized a
201  * bit, but should still be pretty clear.
202  */
203 int afs_osi_MapStrategy(int (*aproc)(), 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 void shutdown_osifile(void)
214 {
215     AFS_STATCNT(shutdown_osifile);
216     if (afs_cold_shutdown)
217         afs_osicred_initialized = 0;
218 }