13ce462de68058940a020b26634589acb3a241a9
[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 "../afs/param.h"       /* Should be always first */
11 #include "../afs/sysincludes.h" /* Standard vendor system headers */
12 #include "../afs/afsincludes.h" /* Afs-based standard headers */
13 #include "../afs/afs_stats.h"  /* afs statistics */
14 #include "../afs/osi_inode.h"   /* igetinode() */
15
16
17 int afs_osicred_initialized=0;
18 struct  AFS_UCRED afs_osi_cred;
19 afs_lock_t afs_xosi;            /* lock is for tvattr */
20 extern struct osi_dev cacheDev;
21 extern struct vfs *afs_cacheVfsp;
22
23
24 void *osi_UFSOpen(ainode)
25     afs_int32 ainode;
26 {
27     struct inode *ip;
28     register struct osi_file *afile = NULL;
29     extern int cacheDiskType;
30     afs_int32 code = 0;
31     int dummy;
32     AFS_STATCNT(osi_UFSOpen);
33     if(cacheDiskType != AFS_FCACHE_TYPE_UFS) {
34         osi_Panic("UFSOpen called for non-UFS cache\n");
35     }
36     if (!afs_osicred_initialized) {
37         /* valid for alpha_osf, SunOS, Ultrix */
38         bzero((char *)&afs_osi_cred, sizeof(struct AFS_UCRED));
39         crhold(&afs_osi_cred);  /* don't let it evaporate, since it is static */
40         afs_osicred_initialized = 1;
41     }
42     afile = (struct osi_file *) osi_AllocSmallSpace(sizeof(struct osi_file));
43     setuerror(0);
44     AFS_GUNLOCK();
45     ip = (struct inode *) igetinode(afs_cacheVfsp, (dev_t) cacheDev.dev, (ino_t)ainode,&dummy);
46     AFS_GLOCK();
47     if (getuerror()) {
48         osi_FreeSmallSpace(afile);
49         osi_Panic("UFSOpen: igetinode failed");
50     }
51     iunlock(ip);
52     afile->vnode = ITOV(ip);
53     afile->size = VTOI(afile->vnode)->i_size;
54     afile->offset = 0;
55     afile->proc = (int (*)()) 0;
56     afile->inum = ainode;        /* for hint validity checking */
57     return (void *)afile;
58 }
59
60 afs_osi_Stat(afile, astat)
61     register struct osi_file *afile;
62     register struct osi_stat *astat; {
63     register afs_int32 code;
64     struct vattr tvattr;
65     AFS_STATCNT(osi_Stat);
66     MObtainWriteLock(&afs_xosi,320);
67     AFS_GUNLOCK();
68     code = VOP_GETATTR(afile->vnode, &tvattr, &afs_osi_cred, VSYNC);
69     AFS_GLOCK();
70     if (code == 0) {
71         astat->size = tvattr.va_size;
72         astat->blksize = tvattr.va_blocksize;
73         astat->mtime = tvattr.va_mtime.tv_sec;
74         astat->atime = tvattr.va_atime.tv_sec;
75     }
76     MReleaseWriteLock(&afs_xosi);
77     return code;
78 }
79
80 osi_UFSClose(afile)
81      register struct osi_file *afile;
82   {
83       AFS_STATCNT(osi_Close);
84       if(afile->vnode) {
85         AFS_RELE(afile->vnode);
86       }
87       
88       osi_FreeSmallSpace(afile);
89       return 0;
90   }
91
92 osi_UFSTruncate(afile, asize)
93     register struct osi_file *afile;
94     afs_int32 asize; {
95     struct AFS_UCRED *oldCred;
96     struct vattr tvattr;
97     register afs_int32 code;
98     struct osi_stat tstat;
99     AFS_STATCNT(osi_Truncate);
100
101     /* This routine only shrinks files, and most systems
102      * have very slow truncates, even when the file is already
103      * small enough.  Check now and save some time.
104      */
105     code = afs_osi_Stat(afile, &tstat);
106     if (code || tstat.size <= asize) return code;
107     MObtainWriteLock(&afs_xosi,321);    
108     VATTR_NULL(&tvattr);
109     /* note that this credential swapping stuff is only necessary because
110         of ufs's references directly to u.u_cred instead of to
111         credentials parameter.  Probably should fix ufs some day. */
112     oldCred = p_cred(u.u_procp);
113     set_p_cred(u.u_procp, &afs_osi_cred);
114     tvattr.va_size = asize;
115     AFS_GUNLOCK();
116     code = VOP_SETATTR(afile->vnode, &tvattr, &afs_osi_cred, 0);
117     AFS_GLOCK();
118     set_p_cred(u.u_procp, oldCred);     /* restore */
119     MReleaseWriteLock(&afs_xosi);
120     return code;
121 }
122
123 void osi_DisableAtimes(avp)
124 struct vnode *avp;
125 {
126    struct inode *ip = VTOI(avp);
127    ip->i_flag &= ~IACC;
128 }
129
130
131 /* Generic read interface */
132 afs_osi_Read(afile, offset, aptr, asize)
133     register struct osi_file *afile;
134     int offset;
135     char *aptr;
136     afs_int32 asize; {
137     struct AFS_UCRED *oldCred;
138     long resid;
139     register afs_int32 code;
140     register afs_int32 cnt1=0;
141     AFS_STATCNT(osi_Read);
142
143     /**
144       * If the osi_file passed in is NULL, panic only if AFS is not shutting
145       * down. No point in crashing when we are already shutting down
146       */
147     if ( !afile ) {
148         if ( !afs_shuttingdown )
149             osi_Panic("osi_Read called with null param");
150         else
151             return EIO;
152     }
153
154     if (offset != -1) afile->offset = offset;
155 retry_IO:
156     AFS_GUNLOCK();
157     code = gop_rdwr(UIO_READ, afile->vnode, (caddr_t) aptr, asize, afile->offset,
158                   AFS_UIOSYS, IO_UNIT, &resid);
159     AFS_GLOCK();
160     if (code == 0) {
161         code = asize - resid;
162         afile->offset += code;
163         osi_DisableAtimes(afile->vnode);
164     }
165     else {
166         afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, (afs_int32) resid,
167                  ICL_TYPE_INT32, code);
168         /*
169          * To handle periodic low-level EFAULT failures that we've seen with the
170          * Weitek chip; in all observed failed cases a second read succeeded.
171          */
172         if ((code == EFAULT) && (cnt1++ < 5)) {
173             afs_stats_cmperf.osiread_efaults++;
174             goto retry_IO;
175         }
176         setuerror(code);
177         code = -1;
178     }
179     return code;
180 }
181
182 /* Generic write interface */
183 afs_osi_Write(afile, offset, aptr, asize)
184     register struct osi_file *afile;
185     char *aptr;
186     afs_int32 offset;
187     afs_int32 asize; {
188     struct AFS_UCRED *oldCred;
189     long resid;
190     register afs_int32 code;
191     AFS_STATCNT(osi_Write);
192     if ( !afile )
193         osi_Panic("afs_osi_Write called with null param");
194     if (offset != -1) afile->offset = offset;
195     AFS_GUNLOCK();
196     code = gop_rdwr(UIO_WRITE, afile->vnode, (caddr_t) aptr, asize, afile->offset,
197                   AFS_UIOSYS, IO_UNIT, &resid);
198     AFS_GLOCK();
199     if (code == 0) {
200         code = asize - resid;
201         afile->offset += code;
202     }
203     else {
204         if (code == ENOSPC) afs_warnuser("\n\n\n*** Cache partition is FULL - Decrease cachesize!!! ***\n\n");
205         setuerror(code);
206         code = -1;
207     }
208     if (afile->proc) {
209         (*afile->proc)(afile, code);
210     }
211     return code;
212 }
213
214
215 void
216 shutdown_osifile()
217 {
218   extern int afs_cold_shutdown;
219
220   AFS_STATCNT(shutdown_osifile);
221   if (afs_cold_shutdown) {
222     afs_osicred_initialized = 0;
223   }
224 }
225