10a2b8466ae5bc9a43bbd4241b24164a6c30e4c6
[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
14 #include "afs/sysincludes.h"    /* Standard vendor system headers */
15 #include "afs/afsincludes.h"    /* Afs-based standard headers */
16 #include "afs/afs_stats.h"      /* afs statistics */
17
18
19 int afs_osicred_initialized;
20 AFS_UCRED 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 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     afile = (struct osi_file *)osi_AllocSmallSpace(sizeof(struct osi_file));
38     AFS_GUNLOCK();
39     code = VFS_VGET(cacheDev.mp, ainode->ufs, &vp);
40     AFS_GLOCK();
41     if (code == 0 && vp->v_type == VNON)
42         code = ENOENT;
43     if (code) {
44         osi_FreeSmallSpace(afile);
45         osi_Panic("UFSOpen: igetinode failed");
46     }
47     VOP_UNLOCK(vp, 0, curproc);
48     afile->vnode = vp;
49 #ifdef AFS_OBSD39_ENV
50     afile->size = VTOI(vp)->i_ffs1_size;
51 #else
52     afile->size = VTOI(vp)->i_ffs_size;
53 #endif
54     afile->offset = 0;
55     afile->proc = NULL;
56     afile->inum = ainode->ufs;  /* for hint validity checking */
57     return (void *)afile;
58 }
59
60 int
61 afs_osi_Stat(struct osi_file *afile, struct osi_stat *astat)
62 {
63     afs_int32 code;
64     struct vattr tvattr;
65
66     AFS_STATCNT(osi_Stat);
67     MObtainWriteLock(&afs_xosi, 320);
68     AFS_GUNLOCK();
69     code = VOP_GETATTR(afile->vnode, &tvattr, afs_osi_credp, curproc);
70     AFS_GLOCK();
71     if (code == 0) {
72         astat->size = afile->size = tvattr.va_size;
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
81 osi_UFSClose(struct osi_file *afile)
82 {
83     AFS_STATCNT(osi_Close);
84
85     if (afile->vnode)
86         AFS_RELE(afile->vnode);
87
88     osi_FreeSmallSpace(afile);
89     return 0;
90 }
91
92 int
93 osi_UFSTruncate(struct osi_file *afile, afs_int32 asize)
94 {
95     struct vattr tvattr;
96     afs_int32 code;
97     struct osi_stat tstat;
98
99     AFS_STATCNT(osi_Truncate);
100
101     /*
102      * This routine only shrinks files, and most systems
103      * have very slow truncates, even when the file is already
104      * small enough.  Check now and save some time.
105      */
106     code = afs_osi_Stat(afile, &tstat);
107     if (code || tstat.size <= asize)
108         return code;
109
110     MObtainWriteLock(&afs_xosi, 321);
111     VATTR_NULL(&tvattr);
112     tvattr.va_size = asize;
113     AFS_GUNLOCK();
114     VOP_LOCK(afile->vnode, LK_EXCLUSIVE | LK_RETRY, curproc);
115     code = VOP_SETATTR(afile->vnode, &tvattr, afs_osi_credp, curproc);
116     VOP_UNLOCK(afile->vnode, 0, curproc);
117     AFS_GLOCK();
118     if (code == 0)
119         afile->size = asize;
120     MReleaseWriteLock(&afs_xosi);
121     return code;
122 }
123
124 void
125 osi_DisableAtimes(struct vnode *avp)
126 {
127 #if 0
128     VTOI(avp)->i_flag &= ~IN_ACCESS;
129 #endif
130 }
131
132
133 /* Generic read interface */
134 int
135 afs_osi_Read(struct osi_file *afile, int offset, void *aptr, afs_int32 asize)
136 {
137     size_t resid;
138     afs_int32 code;
139
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         vn_rdwr(UIO_READ, afile->vnode, aptr, asize, afile->offset,
158                 AFS_UIOSYS, IO_UNIT, afs_osi_credp, &resid, curproc);
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,
166                    (unsigned int) resid, ICL_TYPE_INT32, code);
167         code = -1;
168     }
169     return code;
170 }
171
172 /* Generic write interface */
173 int
174 afs_osi_Write(struct osi_file *afile, afs_int32 offset, void *aptr,
175               afs_int32 asize)
176 {
177     size_t resid;
178     afs_int32 code;
179
180     AFS_STATCNT(osi_Write);
181     if (!afile)
182         osi_Panic("afs_osi_Write called with null afile");
183     if (offset != -1)
184         afile->offset = offset;
185
186     AFS_GUNLOCK();
187     VOP_LOCK(afile->vnode, LK_EXCLUSIVE | LK_RETRY, curproc);
188     code =
189         vn_rdwr(UIO_WRITE, afile->vnode, (caddr_t) aptr, asize, afile->offset,
190                 AFS_UIOSYS, IO_UNIT, afs_osi_credp, &resid, curproc);
191     VOP_UNLOCK(afile->vnode, 0, curproc);
192     AFS_GLOCK();
193
194     if (code == 0) {
195         code = asize - resid;
196         afile->offset += code;
197         if (afile->offset > afile->size)
198             afile->size = afile->offset;
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.  This routine
210  * written from the RT NFS port strategy routine.  It has been generalized a
211  * bit, but should still be pretty clear.
212  */
213 int
214 afs_osi_MapStrategy(int (*aproc) (), struct buf *bp)
215 {
216     afs_int32 returnCode;
217
218     AFS_STATCNT(osi_MapStrategy);
219     returnCode = (*aproc) (bp);
220
221     return returnCode;
222 }
223
224 void
225 shutdown_osifile(void)
226 {
227     AFS_STATCNT(shutdown_osifile);
228     if (afs_cold_shutdown)
229         afs_osicred_initialized = 0;
230 }