fba2125f93ba93ebe7fa1ff62191f73bd1f92c9d
[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
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 #include "afs/osi_inode.h"      /* igetinode() */
18
19
20 int afs_osicred_initialized = 0;
21 afs_ucred_t 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 *
28 osi_UFSOpen(afs_dcache_id_t *ainode)
29 {
30     struct inode *ip;
31     register struct osi_file *afile = 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((char *)&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(afs_cacheVfsp, (dev_t) cacheDev.dev,
49                                    (ino_t) ainode->ufs, &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->ufs;  /* for hint validity checking */
61     return (void *)afile;
62 }
63
64 int
65 afs_osi_Stat(register struct osi_file *afile, register struct osi_stat *astat)
66 {
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->mtime = tvattr.va_mtime.tv_sec;
77         astat->atime = tvattr.va_atime.tv_sec;
78     }
79     MReleaseWriteLock(&afs_xosi);
80     return code;
81 }
82
83 int
84 osi_UFSClose(register struct osi_file *afile)
85 {
86     AFS_STATCNT(osi_Close);
87     if (afile->vnode) {
88         AFS_RELE(afile->vnode);
89     }
90
91     osi_FreeSmallSpace(afile);
92     return 0;
93 }
94
95 int
96 osi_UFSTruncate(register struct osi_file *afile, afs_int32 asize)
97 {
98     afs_ucred_t *oldCred;
99     struct vattr tvattr;
100     register afs_int32 code;
101     struct osi_stat tstat;
102     AFS_STATCNT(osi_Truncate);
103
104     /* This routine only shrinks files, and most systems
105      * have very slow truncates, even when the file is already
106      * small enough.  Check now and save some time.
107      */
108     code = afs_osi_Stat(afile, &tstat);
109     if (code || tstat.size <= asize)
110         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
128 osi_DisableAtimes(struct vnode *avp)
129 {
130     struct inode *ip = VTOI(avp);
131     ip->i_flag &= ~IACC;
132 }
133
134
135 /* Generic read interface */
136 int
137 afs_osi_Read(register struct osi_file *afile, int offset, void *aptr,
138              afs_int32 asize)
139 {
140     afs_ucred_t *oldCred;
141     long resid;
142     register afs_int32 code;
143     register afs_int32 cnt1 = 0;
144     AFS_STATCNT(osi_Read);
145
146     /**
147       * If the osi_file passed in is NULL, panic only if AFS is not shutting
148       * down. No point in crashing when we are already shutting down
149       */
150     if (!afile) {
151         if (!afs_shuttingdown)
152             osi_Panic("osi_Read called with null param");
153         else
154             return EIO;
155     }
156
157     if (offset != -1)
158         afile->offset = offset;
159   retry_IO:
160     AFS_GUNLOCK();
161     code =
162         gop_rdwr(UIO_READ, afile->vnode, (caddr_t) aptr, asize, afile->offset,
163                  AFS_UIOSYS, IO_UNIT, &resid);
164     AFS_GLOCK();
165     if (code == 0) {
166         code = asize - resid;
167         afile->offset += code;
168         osi_DisableAtimes(afile->vnode);
169     } else {
170         afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32,
171                    (afs_int32) resid, 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 int
188 afs_osi_Write(register struct osi_file *afile, afs_int32 offset, void *aptr,
189               afs_int32 asize)
190 {
191     afs_ucred_t *oldCred;
192     long resid;
193     register afs_int32 code;
194     AFS_STATCNT(osi_Write);
195     if (!afile)
196         osi_Panic("afs_osi_Write called with null param");
197     if (offset != -1)
198         afile->offset = offset;
199     AFS_GUNLOCK();
200     code =
201         gop_rdwr(UIO_WRITE, afile->vnode, (caddr_t) aptr, asize,
202                  afile->offset, AFS_UIOSYS, IO_UNIT, &resid);
203     AFS_GLOCK();
204     if (code == 0) {
205         code = asize - resid;
206         afile->offset += code;
207     } else {
208         if (code == ENOSPC)
209             afs_warnuser
210                 ("\n\n\n*** Cache partition is FULL - Decrease cachesize!!! ***\n\n");
211         setuerror(code);
212         code = -1;
213     }
214     if (afile->proc) {
215         (*afile->proc) (afile, code);
216     }
217     return code;
218 }
219
220
221 void
222 shutdown_osifile(void)
223 {
224     AFS_STATCNT(shutdown_osifile);
225     if (afs_cold_shutdown) {
226         afs_osicred_initialized = 0;
227     }
228 }