convert-from-bsd-to-posix-string-and-memory-functions-20010807
[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("$Header$");
14
15 #include "../afs/sysincludes.h" /* Standard vendor system headers */
16 #include "../afs/afsincludes.h" /* Afs-based standard headers */
17 #include "../afs/afs_stats.h"  /* afs statistics */
18
19
20 int afs_osicred_initialized=0;
21 struct  AFS_UCRED afs_osi_cred;
22 afs_lock_t afs_xosi;            /* lock is for tvattr */
23 extern struct osi_dev cacheDev;
24 extern struct vfs *afs_cacheVfsp;
25
26
27 void *osi_UFSOpen(ainode)
28     afs_int32 ainode;
29 {
30     struct inode *ip;
31     register struct osi_file *afile = NULL;
32     extern struct vfs *rootvfs;
33     struct vnode *vp = (struct vnode *)0;
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, rootvfs, (ino_t)ainode, &vp,&dummy);
51     AFS_GLOCK();
52     if (getuerror()) {
53         osi_FreeSmallSpace(afile);
54         osi_Panic("UFSOpen: igetinode failed");
55     }
56     afile->vnode = vp;              /* Save the vnode pointer for the inode ip; also ip is already prele'ed in igetinode */
57     afile->size = VTOI(afile->vnode)->i_size;
58     afile->offset = 0;
59     afile->proc = (int (*)()) 0;
60     afile->inum = ainode;        /* for hint validity checking */
61     return (void *)afile;
62 }
63
64 afs_osi_Stat(afile, astat)
65     register struct osi_file *afile;
66     register struct osi_stat *astat; {
67     register afs_int32 code;
68     struct vattr tvattr;
69     AFS_STATCNT(osi_Stat);
70     MObtainWriteLock(&afs_xosi,320);
71     AFS_GUNLOCK();
72     code = VNOP_GETATTR(afile->vnode, &tvattr, &afs_osi_cred);
73     AFS_GLOCK();
74     if (code == 0) {
75         astat->size = tvattr.va_size;
76         astat->blksize = tvattr.va_blocksize;
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 osi_UFSClose(afile)
85      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 osi_UFSTruncate(afile, asize)
111     register struct osi_file *afile;
112     afs_int32 asize; {
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) return code;
126     MObtainWriteLock(&afs_xosi,321);    
127     /* 
128      * If we're truncating an unopened file to a non-zero length,
129      * we need to bind it to a vm segment    
130      * Note that  that the binding will actually happen inside
131      * jfs by xix_ftrunc; setting mode to 0 will enable that.
132      */
133     if (asize && !VTOGP(afile->vnode)->gn_seg) 
134         mode = 0;
135     AFS_GUNLOCK();
136     code = VNOP_FTRUNC(afile->vnode, mode, asize, (caddr_t)0, &afs_osi_cred);
137     AFS_GLOCK();
138     MReleaseWriteLock(&afs_xosi);
139     return code;
140 }
141
142 void osi_DisableAtimes(avp)
143 struct vnode *avp;
144 {
145    struct inode *ip = VTOIP(avp);
146    ip->i_flag &= ~IACC;
147 }
148
149
150 /* Generic read interface */
151 afs_osi_Read(afile, offset, aptr, asize)
152     register struct osi_file *afile;
153     int offset;
154     char *aptr;
155     afs_int32 asize; {
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) afile->offset = offset;
174 retry_IO:
175     /* Note the difference in the way the afile->offset is passed (see comments in gop_rdwr() in afs_aix_subr.c for comments) */
176     AFS_GUNLOCK();
177     code = gop_rdwr(UIO_READ, afile->vnode, (caddr_t) aptr, asize,
178                     (off_t)&afile->offset, AFS_UIOSYS, NULL, &resid);
179     AFS_GLOCK();
180     if (code == 0) {
181         code = asize - resid;
182         afile->offset += code;
183         osi_DisableAtimes(afile->vnode);
184     }
185     else {
186         afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, resid,
187                  ICL_TYPE_INT32, code);
188         /*
189          * To handle periodic low-level EFAULT failures that we've seen with the
190          * Weitek chip; in all observed failed cases a second read succeeded.
191          */
192         if ((code == EFAULT) && (cnt1++ < 5)) {
193             afs_stats_cmperf.osiread_efaults++;
194             goto retry_IO;
195         }
196         setuerror(code);
197         code = -1;
198     }
199     return code;
200 }
201
202 /* Generic write interface */
203 afs_osi_Write(afile, offset, aptr, asize)
204     register struct osi_file *afile;
205     char *aptr;
206     afs_int32 offset;
207     afs_int32 asize; {
208     struct AFS_UCRED *oldCred;
209     unsigned int resid;
210     register 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) afile->offset = offset;
215     /* Note the difference in the way the afile->offset is passed (see comments in gop_rdwr() in afs_aix_subr.c for comments) */
216     AFS_GUNLOCK();
217     code = gop_rdwr(UIO_WRITE, afile->vnode, (caddr_t) aptr, asize,
218                     (off_t)&afile->offset, AFS_UIOSYS, NULL, &resid);
219     AFS_GLOCK();
220     if (code == 0) {
221         code = asize - resid;
222         afile->offset += code;
223     }
224     else {
225         if (code == ENOSPC) afs_warnuser("\n\n\n*** Cache partition is FULL - Decrease cachesize!!! ***\n\n");
226         setuerror(code);
227         code = -1;
228     }
229     if (afile->proc) {
230         (*afile->proc)(afile, code);
231     }
232     return code;
233 }
234
235
236 /*  This work should be handled by physstrat in ca/machdep.c.
237     This routine written from the RT NFS port strategy routine.
238     It has been generalized a bit, but should still be pretty clear. */
239 int afs_osi_MapStrategy(aproc, bp)
240         int (*aproc)();
241         register struct buf *bp;
242 {
243     afs_int32 returnCode;
244
245     AFS_STATCNT(osi_MapStrategy);
246     returnCode = (*aproc) (bp);
247
248     return returnCode;
249 }
250
251
252
253 void
254 shutdown_osifile()
255 {
256   extern int afs_cold_shutdown;
257
258   AFS_STATCNT(shutdown_osifile);
259   if (afs_cold_shutdown) {
260     afs_osicred_initialized = 0;
261   }
262 }
263