convert-from-bsd-to-posix-string-and-memory-functions-20010807
[openafs.git] / src / afs / HPUX / 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 #include "../afs/osi_inode.h"   /* igetinode() */
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 *osi_UFSOpen(ainode)
29     afs_int32 ainode;
30 {
31     struct inode *ip;
32     register struct osi_file *afile = NULL;
33     extern int cacheDiskType;
34     afs_int32 code = 0;
35     int dummy;
36     AFS_STATCNT(osi_UFSOpen);
37     if(cacheDiskType != AFS_FCACHE_TYPE_UFS) {
38         osi_Panic("UFSOpen called for non-UFS cache\n");
39     }
40     if (!afs_osicred_initialized) {
41         /* valid for alpha_osf, SunOS, Ultrix */
42         memset((char *)&afs_osi_cred, 0, sizeof(struct AFS_UCRED));
43         crhold(&afs_osi_cred);  /* don't let it evaporate, since it is static */
44         afs_osicred_initialized = 1;
45     }
46     afile = (struct osi_file *) osi_AllocSmallSpace(sizeof(struct osi_file));
47     setuerror(0);
48     AFS_GUNLOCK();
49     ip = (struct inode *) igetinode(afs_cacheVfsp, (dev_t) cacheDev.dev, (ino_t)ainode,&dummy);
50     AFS_GLOCK();
51     if (getuerror()) {
52         osi_FreeSmallSpace(afile);
53         osi_Panic("UFSOpen: igetinode failed");
54     }
55     iunlock(ip);
56     afile->vnode = ITOV(ip);
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 = VOP_GETATTR(afile->vnode, &tvattr, &afs_osi_cred, VSYNC);
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         AFS_RELE(afile->vnode);
90       }
91       
92       osi_FreeSmallSpace(afile);
93       return 0;
94   }
95
96 osi_UFSTruncate(afile, asize)
97     register struct osi_file *afile;
98     afs_int32 asize; {
99     struct AFS_UCRED *oldCred;
100     struct vattr tvattr;
101     register afs_int32 code;
102     struct osi_stat tstat;
103     AFS_STATCNT(osi_Truncate);
104
105     /* This routine only shrinks files, and most systems
106      * have very slow truncates, even when the file is already
107      * small enough.  Check now and save some time.
108      */
109     code = afs_osi_Stat(afile, &tstat);
110     if (code || tstat.size <= asize) return code;
111     MObtainWriteLock(&afs_xosi,321);    
112     VATTR_NULL(&tvattr);
113     /* note that this credential swapping stuff is only necessary because
114         of ufs's references directly to u.u_cred instead of to
115         credentials parameter.  Probably should fix ufs some day. */
116     oldCred = p_cred(u.u_procp);
117     set_p_cred(u.u_procp, &afs_osi_cred);
118     tvattr.va_size = asize;
119     AFS_GUNLOCK();
120     code = VOP_SETATTR(afile->vnode, &tvattr, &afs_osi_cred, 0);
121     AFS_GLOCK();
122     set_p_cred(u.u_procp, oldCred);     /* restore */
123     MReleaseWriteLock(&afs_xosi);
124     return code;
125 }
126
127 void osi_DisableAtimes(avp)
128 struct vnode *avp;
129 {
130    struct inode *ip = VTOI(avp);
131    ip->i_flag &= ~IACC;
132 }
133
134
135 /* Generic read interface */
136 afs_osi_Read(afile, offset, aptr, asize)
137     register struct osi_file *afile;
138     int offset;
139     char *aptr;
140     afs_int32 asize; {
141     struct AFS_UCRED *oldCred;
142     long resid;
143     register afs_int32 code;
144     register afs_int32 cnt1=0;
145     AFS_STATCNT(osi_Read);
146
147     /**
148       * If the osi_file passed in is NULL, panic only if AFS is not shutting
149       * down. No point in crashing when we are already shutting down
150       */
151     if ( !afile ) {
152         if ( !afs_shuttingdown )
153             osi_Panic("osi_Read called with null param");
154         else
155             return EIO;
156     }
157
158     if (offset != -1) afile->offset = offset;
159 retry_IO:
160     AFS_GUNLOCK();
161     code = gop_rdwr(UIO_READ, afile->vnode, (caddr_t) aptr, asize, afile->offset,
162                   AFS_UIOSYS, IO_UNIT, &resid);
163     AFS_GLOCK();
164     if (code == 0) {
165         code = asize - resid;
166         afile->offset += code;
167         osi_DisableAtimes(afile->vnode);
168     }
169     else {
170         afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, (afs_int32) resid,
171                  ICL_TYPE_INT32, code);
172         /*
173          * To handle periodic low-level EFAULT failures that we've seen with the
174          * Weitek chip; in all observed failed cases a second read succeeded.
175          */
176         if ((code == EFAULT) && (cnt1++ < 5)) {
177             afs_stats_cmperf.osiread_efaults++;
178             goto retry_IO;
179         }
180         setuerror(code);
181         code = -1;
182     }
183     return code;
184 }
185
186 /* Generic write interface */
187 afs_osi_Write(afile, offset, aptr, asize)
188     register struct osi_file *afile;
189     char *aptr;
190     afs_int32 offset;
191     afs_int32 asize; {
192     struct AFS_UCRED *oldCred;
193     long resid;
194     register afs_int32 code;
195     AFS_STATCNT(osi_Write);
196     if ( !afile )
197         osi_Panic("afs_osi_Write called with null param");
198     if (offset != -1) afile->offset = offset;
199     AFS_GUNLOCK();
200     code = gop_rdwr(UIO_WRITE, afile->vnode, (caddr_t) aptr, asize, afile->offset,
201                   AFS_UIOSYS, IO_UNIT, &resid);
202     AFS_GLOCK();
203     if (code == 0) {
204         code = asize - resid;
205         afile->offset += code;
206     }
207     else {
208         if (code == ENOSPC) afs_warnuser("\n\n\n*** Cache partition is FULL - Decrease cachesize!!! ***\n\n");
209         setuerror(code);
210         code = -1;
211     }
212     if (afile->proc) {
213         (*afile->proc)(afile, code);
214     }
215     return code;
216 }
217
218
219 void
220 shutdown_osifile()
221 {
222   extern int afs_cold_shutdown;
223
224   AFS_STATCNT(shutdown_osifile);
225   if (afs_cold_shutdown) {
226     afs_osicred_initialized = 0;
227   }
228 }
229