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