Import Upstream version 1.8.5
[hcoop/debian/openafs.git] / src / afs / FBSD / 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 "afsincludes.h" /* Afs-based standard headers */
16#include "afs/afs_stats.h" /* afs statistics */
17
18
19int afs_osicred_initialized = 0;
20#ifndef AFS_FBSD80_ENV /* cr_groups is now malloc()'d */
21afs_ucred_t afs_osi_cred;
22#endif
23extern struct osi_dev cacheDev;
24extern struct mount *afs_cacheVfsp;
25
26
27void *
28osi_UFSOpen(afs_dcache_id_t *ainode)
29{
30 struct osi_file *afile;
31 struct vnode *vp;
32 extern int cacheDiskType;
33 afs_int32 code;
34
35 AFS_STATCNT(osi_UFSOpen);
36 if (cacheDiskType != AFS_FCACHE_TYPE_UFS)
37 osi_Panic("UFSOpen called for non-UFS cache\n");
38 afile = osi_AllocSmallSpace(sizeof(struct osi_file));
39 AFS_GUNLOCK();
40 code = VFS_VGET(afs_cacheVfsp, (ino_t) ainode->ufs, LK_EXCLUSIVE, &vp);
41 AFS_GLOCK();
42 if (code == 0 && vp->v_type == VNON)
43 code = ENOENT;
44 if (code) {
45 osi_FreeSmallSpace(afile);
46 osi_Panic("UFSOpen: igetinode failed");
47 }
48#if defined(AFS_FBSD80_ENV)
49 VOP_UNLOCK(vp, 0);
50#else
51 VOP_UNLOCK(vp, 0, curthread);
52#endif
53 afile->vnode = vp;
54 afile->size = VTOI(vp)->i_size;
55 afile->offset = 0;
56 afile->proc = NULL;
57 return (void *)afile;
58}
59
60int
61afs_osi_Stat(struct osi_file *afile, struct osi_stat *astat)
62{
63 afs_int32 code;
64 struct vattr tvattr;
65 AFS_STATCNT(osi_Stat);
66 AFS_GUNLOCK();
67#if defined(AFS_FBSD80_ENV)
68 vn_lock(afile->vnode, LK_EXCLUSIVE | LK_RETRY);
69 code = VOP_GETATTR(afile->vnode, &tvattr, afs_osi_credp);
70 VOP_UNLOCK(afile->vnode, 0);
71#else
72 vn_lock(afile->vnode, LK_EXCLUSIVE | LK_RETRY, curthread);
73 code = VOP_GETATTR(afile->vnode, &tvattr, afs_osi_credp, curthread);
74 VOP_UNLOCK(afile->vnode, LK_EXCLUSIVE, curthread);
75#endif
76 AFS_GLOCK();
77 if (code == 0) {
78 astat->size = tvattr.va_size;
79 astat->mtime = tvattr.va_mtime.tv_sec;
80 astat->atime = tvattr.va_atime.tv_sec;
81 }
82 return code;
83}
84
85int
86osi_UFSClose(struct osi_file *afile)
87{
88 AFS_STATCNT(osi_Close);
89 if (afile->vnode) {
90 AFS_RELE(afile->vnode);
91 }
92
93 osi_FreeSmallSpace(afile);
94 return 0;
95}
96
97int
98osi_UFSTruncate(struct osi_file *afile, afs_int32 asize)
99{
100 struct vattr tvattr;
101 struct vnode *vp;
102 afs_int32 code, glocked;
103 AFS_STATCNT(osi_Truncate);
104
105 vp = afile->vnode;
106 /*
107 * This routine only shrinks files, and most systems
108 * have very slow truncates, even when the file is already
109 * small enough. Check now and save some time.
110 */
111 glocked = ISAFS_GLOCK();
112 if (glocked)
113 AFS_GUNLOCK();
114#if defined(AFS_FBSD80_ENV)
115 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
116 code = VOP_GETATTR(afile->vnode, &tvattr, afs_osi_credp);
117#else
118 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
119 code = VOP_GETATTR(afile->vnode, &tvattr, afs_osi_credp, curthread);
120#endif
121 if (code != 0 || tvattr.va_size <= asize)
122 goto out;
123
124 VATTR_NULL(&tvattr);
125 tvattr.va_size = asize;
126#if defined(AFS_FBSD80_ENV)
127 code = VOP_SETATTR(vp, &tvattr, afs_osi_credp);
128#else
129 code = VOP_SETATTR(vp, &tvattr, afs_osi_credp, curthread);
130#endif
131
132out:
133#if defined(AFS_FBSD80_ENV)
134 VOP_UNLOCK(vp, 0);
135#else
136 VOP_UNLOCK(vp, LK_EXCLUSIVE, curthread);
137#endif
138 if (glocked)
139 AFS_GLOCK();
140 return code;
141}
142
143void
144osi_DisableAtimes(struct vnode *avp)
145{
146 struct inode *ip = VTOI(avp);
147 ip->i_flag &= ~IN_ACCESS;
148}
149
150
151/* Generic read interface */
152int
153afs_osi_Read(struct osi_file *afile, int offset, void *aptr,
154 afs_int32 asize)
155{
156#if (__FreeBSD_version >= 900505 && __FreeBSD_Version < 1000000) ||__FreeBSD_version >= 1000009
157 ssize_t resid;
158#else
159 int resid;
160#endif
161 afs_int32 code;
162 AFS_STATCNT(osi_Read);
163
164 /**
165 * If the osi_file passed in is NULL, panic only if AFS is not shutting
166 * down. No point in crashing when we are already shutting down
167 */
168 if (!afile) {
169 if (afs_shuttingdown == AFS_RUNNING)
170 osi_Panic("osi_Read called with null param");
171 else
172 return -EIO;
173 }
174
175 if (offset != -1)
176 afile->offset = offset;
177 AFS_GUNLOCK();
178 code =
179 gop_rdwr(UIO_READ, afile->vnode, (caddr_t) aptr, asize, afile->offset,
180 AFS_UIOSYS, IO_UNIT, afs_osi_credp, &resid);
181 AFS_GLOCK();
182 if (code == 0) {
183 code = asize - resid;
184 afile->offset += code;
185 osi_DisableAtimes(afile->vnode);
186 } else {
187 afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, (int)resid,
188 ICL_TYPE_INT32, code);
189 if (code > 0) {
190 code = -code;
191 }
192 }
193 return code;
194}
195
196/* Generic write interface */
197int
198afs_osi_Write(struct osi_file *afile, afs_int32 offset, void *aptr,
199 afs_int32 asize)
200{
201#if (__FreeBSD_version >= 900505 && __FreeBSD_Version < 1000000) ||__FreeBSD_version >= 1000009
202 ssize_t resid;
203#else
204 int resid;
205#endif
206 afs_int32 code;
207 AFS_STATCNT(osi_Write);
208 if (!afile)
209 osi_Panic("afs_osi_Write called with null param");
210 if (offset != -1)
211 afile->offset = offset;
212 {
213 AFS_GUNLOCK();
214 code =
215 gop_rdwr(UIO_WRITE, afile->vnode, (caddr_t) aptr, asize,
216 afile->offset, AFS_UIOSYS, IO_UNIT, afs_osi_credp,
217 &resid);
218 AFS_GLOCK();
219 }
220 if (code == 0) {
221 code = asize - resid;
222 afile->offset += code;
223 } else {
224 if (code > 0) {
225 code = -code;
226 }
227 }
228 if (afile->proc) {
229 (*afile->proc) (afile, code);
230 }
231 return code;
232}
233
234
235/* This work should be handled by physstrat in ca/machdep.c.
236 This routine written from the RT NFS port strategy routine.
237 It has been generalized a bit, but should still be pretty clear. */
238int
239afs_osi_MapStrategy(int (*aproc) (), struct buf *bp)
240{
241 afs_int32 returnCode;
242
243 AFS_STATCNT(osi_MapStrategy);
244 returnCode = (*aproc) (bp);
245
246 return returnCode;
247}
248
249
250
251void
252shutdown_osifile(void)
253{
254 AFS_STATCNT(shutdown_osifile);
255 if (afs_cold_shutdown) {
256 afs_osicred_initialized = 0;
257 }
258}