Import Upstream version 1.8.5
[hcoop/debian/openafs.git] / src / afs / OBSD / osi_file.c
CommitLineData
805e021f
CE
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
19int afs_osicred_initialized;
20afs_ucred_t afs_osi_cred;
21extern struct osi_dev cacheDev;
22extern struct mount *afs_cacheVfsp;
23
24
25void *
26osi_UFSOpen(afs_dcache_id_t *ainode)
27{
28 struct osi_file *afile;
29 struct vnode *vp;
30 extern int cacheDiskType;
31 afs_int32 code;
32
33 AFS_STATCNT(osi_UFSOpen);
34 if (cacheDiskType != AFS_FCACHE_TYPE_UFS)
35 osi_Panic("UFSOpen called for non-UFS cache\n");
36 afile = osi_AllocSmallSpace(sizeof(struct osi_file));
37 AFS_GUNLOCK();
38 code = VFS_VGET(cacheDev.mp, ainode->ufs, &vp);
39 AFS_GLOCK();
40 if (code == 0 && vp->v_type == VNON)
41 code = ENOENT;
42 if (code) {
43 osi_FreeSmallSpace(afile);
44 osi_Panic("UFSOpen: igetinode failed");
45 }
46 VOP_UNLOCK(vp, 0, curproc);
47 afile->vnode = vp;
48#ifdef AFS_OBSD39_ENV
49 afile->size = VTOI(vp)->i_ffs1_size;
50#else
51 afile->size = VTOI(vp)->i_ffs_size;
52#endif
53 afile->offset = 0;
54 afile->proc = NULL;
55 return (void *)afile;
56}
57
58int
59afs_osi_Stat(struct osi_file *afile, struct osi_stat *astat)
60{
61 afs_int32 code;
62 struct vattr tvattr;
63
64 AFS_STATCNT(osi_Stat);
65 AFS_GUNLOCK();
66 code = VOP_GETATTR(afile->vnode, &tvattr, afs_osi_credp, curproc);
67 AFS_GLOCK();
68 if (code == 0) {
69 astat->size = afile->size = tvattr.va_size;
70 astat->mtime = tvattr.va_mtime.tv_sec;
71 astat->atime = tvattr.va_atime.tv_sec;
72 }
73 return code;
74}
75
76int
77osi_UFSClose(struct osi_file *afile)
78{
79 AFS_STATCNT(osi_Close);
80
81 if (afile->vnode)
82 AFS_RELE(afile->vnode);
83
84 osi_FreeSmallSpace(afile);
85 return 0;
86}
87
88int
89osi_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 VATTR_NULL(&tvattr);
107 tvattr.va_size = asize;
108 AFS_GUNLOCK();
109 VOP_LOCK(afile->vnode, LK_EXCLUSIVE | LK_RETRY, curproc);
110 code = VOP_SETATTR(afile->vnode, &tvattr, afs_osi_credp, curproc);
111 VOP_UNLOCK(afile->vnode, 0, curproc);
112 AFS_GLOCK();
113 if (code == 0)
114 afile->size = asize;
115 return code;
116}
117
118void
119osi_DisableAtimes(struct vnode *avp)
120{
121#if 0
122 VTOI(avp)->i_flag &= ~IN_ACCESS;
123#endif
124}
125
126
127/* Generic read interface */
128int
129afs_osi_Read(struct osi_file *afile, int offset, void *aptr, afs_int32 asize)
130{
131 size_t 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 == AFS_RUNNING)
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 =
151 vn_rdwr(UIO_READ, afile->vnode, aptr, asize, afile->offset,
152 AFS_UIOSYS, IO_UNIT, afs_osi_credp, &resid, curproc);
153 AFS_GLOCK();
154 if (code == 0) {
155 code = asize - resid;
156 afile->offset += code;
157 osi_DisableAtimes(afile->vnode);
158 } else {
159 afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32,
160 (unsigned int) resid, ICL_TYPE_INT32, code);
161 if (code > 0) {
162 code = -code;
163 }
164 }
165 return code;
166}
167
168/* Generic write interface */
169int
170afs_osi_Write(struct osi_file *afile, afs_int32 offset, void *aptr,
171 afs_int32 asize)
172{
173 size_t resid;
174 afs_int32 code;
175
176 AFS_STATCNT(osi_Write);
177 if (!afile)
178 osi_Panic("afs_osi_Write called with null afile");
179 if (offset != -1)
180 afile->offset = offset;
181
182 AFS_GUNLOCK();
183 VOP_LOCK(afile->vnode, LK_EXCLUSIVE | LK_RETRY, curproc);
184 code =
185 vn_rdwr(UIO_WRITE, afile->vnode, (caddr_t) aptr, asize, afile->offset,
186 AFS_UIOSYS, IO_UNIT, afs_osi_credp, &resid, curproc);
187 VOP_UNLOCK(afile->vnode, 0, curproc);
188 AFS_GLOCK();
189
190 if (code == 0) {
191 code = asize - resid;
192 afile->offset += code;
193 if (afile->offset > afile->size)
194 afile->size = afile->offset;
195 } else {
196 if (code > 0) {
197 code = -code;
198 }
199 }
200
201 if (afile->proc)
202 (*afile->proc) (afile, code);
203
204 return code;
205}
206
207/*
208 * This work should be handled by physstrat in ca/machdep.c. This routine
209 * written from the RT NFS port strategy routine. It has been generalized a
210 * bit, but should still be pretty clear.
211 */
212int
213afs_osi_MapStrategy(int (*aproc) (), struct buf *bp)
214{
215 afs_int32 returnCode;
216
217 AFS_STATCNT(osi_MapStrategy);
218 returnCode = (*aproc) (bp);
219
220 return returnCode;
221}
222
223void
224shutdown_osifile(void)
225{
226 AFS_STATCNT(shutdown_osifile);
227 if (afs_cold_shutdown)
228 afs_osicred_initialized = 0;
229}