Import Upstream version 1.8.5
[hcoop/debian/openafs.git] / src / afs / AIX / 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 "afsincludes.h" /* Afs-based standard headers */
16 #include "afs/afs_stats.h" /* afs statistics */
17
18
19 int afs_osicred_initialized = 0;
20 afs_ucred_t afs_osi_cred;
21 extern struct osi_dev cacheDev;
22 extern struct vfs *afs_cacheVfsp;
23
24
25 void *
26 osi_UFSOpen(afs_dcache_id_t *ainode)
27 {
28 struct inode *ip;
29 struct osi_file *afile = NULL;
30 struct vnode *vp = 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(&afs_osi_cred, 0, sizeof(afs_ucred_t));
41 crhold(&afs_osi_cred); /* don't let it evaporate, since it is static */
42 afs_osicred_initialized = 1;
43 }
44 afile = osi_AllocSmallSpace(sizeof(struct osi_file));
45 setuerror(0);
46 AFS_GUNLOCK();
47 ip = (struct inode *)igetinode((dev_t) cacheDev.dev, afs_cacheVfsp,
48 (ino_t) ainode->ufs, &vp, &dummy);
49 AFS_GLOCK();
50 if (getuerror()) {
51 osi_FreeSmallSpace(afile);
52 osi_Panic("UFSOpen: igetinode failed");
53 }
54 afile->vnode = vp; /* Save the vnode pointer for the inode ip; also ip is already prele'ed in igetinode */
55 afile->size = VTOI(afile->vnode)->i_size;
56 afile->offset = 0;
57 afile->proc = (int (*)())0;
58 return (void *)afile;
59 }
60
61 int
62 afs_osi_Stat(struct osi_file *afile, struct osi_stat *astat)
63 {
64 afs_int32 code;
65 struct vattr tvattr;
66 AFS_STATCNT(osi_Stat);
67 AFS_GUNLOCK();
68 code = VNOP_GETATTR(afile->vnode, &tvattr, &afs_osi_cred);
69 AFS_GLOCK();
70 if (code == 0) {
71 astat->size = tvattr.va_size;
72 astat->mtime = tvattr.va_mtime.tv_sec;
73 astat->atime = tvattr.va_atime.tv_sec;
74 }
75 return code;
76 }
77
78 int
79 osi_UFSClose(struct osi_file *afile)
80 {
81 AFS_STATCNT(osi_Close);
82 if (afile->vnode) {
83 /* AIX writes entire data regions at a time when dumping core. We've
84 * seen a 26M write go through the system. When this happens, we run
85 * out of available pages. So, we'll flush the vnode's vm if we're short
86 * on space.
87 */
88 if (vmPageHog) {
89 int code;
90 if (afile->vnode->v_gnode->gn_seg) {
91 /* 524287 is the max number of pages for a file. See test in
92 * vm_writep.
93 */
94 code = vm_writep(afile->vnode->v_gnode->gn_seg, 0, 524287);
95 }
96 }
97 AFS_RELE(afile->vnode);
98 }
99
100 osi_FreeSmallSpace(afile);
101 return 0;
102 }
103
104 int
105 osi_UFSTruncate(struct osi_file *afile, afs_int32 asize)
106 {
107 afs_ucred_t *oldCred;
108 struct vattr tvattr;
109 afs_int32 code;
110 struct osi_stat tstat;
111 afs_int32 mode = FWRITE | FSYNC;
112 AFS_STATCNT(osi_Truncate);
113
114 /* This routine only shrinks files, and most systems
115 * have very slow truncates, even when the file is already
116 * small enough. Check now and save some time.
117 */
118 code = afs_osi_Stat(afile, &tstat);
119 if (code || tstat.size <= asize)
120 return code;
121 /*
122 * If we're truncating an unopened file to a non-zero length,
123 * we need to bind it to a vm segment
124 * Note that that the binding will actually happen inside
125 * jfs by xix_ftrunc; setting mode to 0 will enable that.
126 */
127 if (asize && !VTOGP(afile->vnode)->gn_seg)
128 mode = 0;
129 AFS_GUNLOCK();
130 code = VNOP_FTRUNC(afile->vnode, mode, asize, (caddr_t) 0, &afs_osi_cred);
131 AFS_GLOCK();
132 return code;
133 }
134
135 void
136 osi_DisableAtimes(struct vnode *avp)
137 {
138 struct inode *ip = VTOIP(avp);
139 ip->i_flag &= ~IACC;
140 }
141
142
143 /* Generic read interface */
144 int
145 afs_osi_Read(struct osi_file *afile, int offset, void *aptr,
146 afs_int32 asize)
147 {
148 afs_ucred_t *oldCred;
149 unsigned int resid;
150 afs_int32 code;
151 afs_int32 cnt1 = 0;
152 AFS_STATCNT(osi_Read);
153
154 /**
155 * If the osi_file passed in is NULL, panic only if AFS is not shutting
156 * down. No point in crashing when we are already shutting down
157 */
158 if (!afile) {
159 if (afs_shuttingdown == AFS_RUNNING)
160 osi_Panic("osi_Read called with null param");
161 else
162 return -EIO;
163 }
164
165 if (offset != -1)
166 afile->offset = offset;
167 retry_IO:
168 /* Note the difference in the way the afile->offset is passed (see comments in gop_rdwr() in afs_aix_subr.c for comments) */
169 AFS_GUNLOCK();
170 #ifdef AFS_64BIT_KERNEL
171 code =
172 gop_rdwr(UIO_READ, afile->vnode, (caddr_t) aptr, asize,
173 &afile->offset, AFS_UIOSYS, NULL, &resid);
174 #else
175 code =
176 gop_rdwr(UIO_READ, afile->vnode, (caddr_t) aptr, asize,
177 (off_t) & afile->offset, AFS_UIOSYS, NULL, &resid);
178 #endif
179 AFS_GLOCK();
180 if (code == 0) {
181 code = asize - resid;
182 afile->offset += code;
183 osi_DisableAtimes(afile->vnode);
184 } else {
185 afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, resid,
186 ICL_TYPE_INT32, code);
187 /*
188 * To handle periodic low-level EFAULT failures that we've seen with the
189 * Weitek chip; in all observed failed cases a second read succeeded.
190 */
191 if ((code == EFAULT) && (cnt1++ < 5)) {
192 afs_stats_cmperf.osiread_efaults++;
193 goto retry_IO;
194 }
195 setuerror(code);
196 if (code > 0) {
197 code = -code;
198 }
199 }
200 return code;
201 }
202
203 /* Generic write interface */
204 int
205 afs_osi_Write(struct osi_file *afile, afs_int32 offset, void *aptr,
206 afs_int32 asize)
207 {
208 afs_ucred_t *oldCred;
209 unsigned int resid;
210 afs_int32 code;
211 AFS_STATCNT(osi_Write);
212 if (!afile)
213 osi_Panic("afs_osi_Write called with null param");
214 if (offset != -1)
215 afile->offset = offset;
216 /* Note the difference in the way the afile->offset is passed (see comments in gop_rdwr() in afs_aix_subr.c for comments) */
217 AFS_GUNLOCK();
218 #ifdef AFS_64BIT_KERNEL
219 code =
220 gop_rdwr(UIO_WRITE, afile->vnode, (caddr_t) aptr, asize,
221 &afile->offset, AFS_UIOSYS, NULL, &resid);
222 #else
223 code =
224 gop_rdwr(UIO_WRITE, afile->vnode, (caddr_t) aptr, asize,
225 (off_t) & afile->offset, AFS_UIOSYS, NULL, &resid);
226 #endif
227 AFS_GLOCK();
228 if (code == 0) {
229 if (resid)
230 afs_Trace3(afs_iclSetp, CM_TRACE_WRITEFAILED, ICL_TYPE_INT32,
231 asize, ICL_TYPE_INT32, resid, ICL_TYPE_INT32, code);
232 code = asize - resid;
233 afile->offset += code;
234 } else {
235 afs_Trace3(afs_iclSetp, CM_TRACE_WRITEFAILED, ICL_TYPE_INT32, asize,
236 ICL_TYPE_INT32, resid, ICL_TYPE_INT32, code);
237 if (code == ENOSPC)
238 afs_WarnENOSPC();
239 setuerror(code);
240 if (code > 0) {
241 code = -code;
242 }
243 }
244 if (afile->proc) {
245 (*afile->proc) (afile, code);
246 }
247 return code;
248 }
249
250
251 /* This work should be handled by physstrat in ca/machdep.c.
252 This routine written from the RT NFS port strategy routine.
253 It has been generalized a bit, but should still be pretty clear. */
254 int
255 afs_osi_MapStrategy(int (*aproc) (), struct buf *bp)
256 {
257 afs_int32 returnCode;
258
259 AFS_STATCNT(osi_MapStrategy);
260 returnCode = (*aproc) (bp);
261
262 return returnCode;
263 }
264
265
266
267 void
268 shutdown_osifile(void)
269 {
270 AFS_STATCNT(shutdown_osifile);
271 if (afs_cold_shutdown) {
272 afs_osicred_initialized = 0;
273 }
274 }