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