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