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