4944bd4f340695fb541285365a2d5026a9b4fc68
[openafs.git] / src / afs / IRIX / 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 <afsconfig.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 int afs_osicred_initialized=0;
20 struct  AFS_UCRED afs_osi_cred;
21 afs_lock_t afs_xosi;            /* lock is for tvattr */
22 extern struct osi_dev cacheDev;
23 extern struct vfs *afs_cacheVfsp;
24
25
26 /* As of 6.2, we support either XFS or EFS clients. osi_UFSOpen
27  * now vectors to the correct EFS or XFS function. If new functionality is
28  * added which accesses the inode, that will also need EFS/XFS variants.
29  */
30 #ifdef AFS_SGI_EFS_IOPS_ENV
31 vnode_t *afs_EFSIGetVnode(ino_t ainode)
32 {
33     struct inode *ip;
34     int error;
35
36     if ((error = igetinode(afs_cacheVfsp, (dev_t)cacheDev.dev, ainode, &ip))) {
37         osi_Panic("afs_EFSIGetVnode: igetinode failed, error=%d", error);
38     }
39     /* We don't care about atimes on the cache files, so disable them.  I'm not
40      * sure that this is the right place to do this: it should be *after* readi 
41      * and getattr and stuff. 
42      */
43     ip->i_flags &= ~(ISYN|IACC);
44     iunlock(ip);
45     return (EFS_ITOV(ip));
46 }    
47 #endif /* AFS_SGI_EFS_IOPS_ENV */
48
49 vnode_t *afs_XFSIGetVnode(ino_t ainode)
50 {
51     struct xfs_inode *ip;
52     int error;
53     vnode_t *vp;
54
55     if ((error =
56          xfs_igetinode(afs_cacheVfsp, (dev_t)cacheDev.dev, ainode, &ip))) {
57         osi_Panic("afs_XFSIGetVnode: xfs_igetinode failed, error=%d", error);
58     }
59     vp = XFS_ITOV(ip);
60     return vp;
61 }
62
63 /* Force to 64 bits, even for EFS filesystems. */
64 void *osi_UFSOpen(ino_t ainode)
65 {
66     struct inode *ip;
67     register struct osi_file *afile = NULL;
68     extern int cacheDiskType;
69     afs_int32 code = 0;
70     int dummy;
71     AFS_STATCNT(osi_UFSOpen);
72     if(cacheDiskType != AFS_FCACHE_TYPE_UFS) {
73         osi_Panic("UFSOpen called for non-UFS cache\n");
74     }
75     if (!afs_osicred_initialized) {
76         /* valid for alpha_osf, SunOS, Ultrix */
77         bzero((char *)&afs_osi_cred, sizeof(struct AFS_UCRED));
78         crhold(&afs_osi_cred);  /* don't let it evaporate, since it is static */
79         afs_osicred_initialized = 1;
80     }
81     afile = (struct osi_file *) osi_AllocSmallSpace(sizeof(struct osi_file));
82     AFS_GUNLOCK();
83     afile->vnode = AFS_SGI_IGETVNODE(ainode);
84     AFS_GLOCK();
85     afile->size = VnodeToSize(afile->vnode);
86     afile->offset = 0;
87     afile->proc = (int (*)()) 0;
88     afile->inum = ainode;        /* for hint validity checking */
89     return (void *)afile;
90 }
91
92 afs_osi_Stat(afile, astat)
93     register struct osi_file *afile;
94     register struct osi_stat *astat; {
95     register afs_int32 code;
96     struct vattr tvattr;
97     AFS_STATCNT(osi_Stat);
98     MObtainWriteLock(&afs_xosi,320);
99     AFS_GUNLOCK();
100     tvattr.va_mask = AT_SIZE|AT_BLKSIZE|AT_MTIME|AT_ATIME;
101     AFS_VOP_GETATTR(afile->vnode, &tvattr, 0, &afs_osi_cred, code);
102     AFS_GLOCK();
103     if (code == 0) {
104         astat->size = tvattr.va_size;
105         astat->blksize = tvattr.va_blocksize;
106         astat->mtime = tvattr.va_mtime.tv_sec;
107         astat->atime = tvattr.va_atime.tv_sec;
108     }
109     MReleaseWriteLock(&afs_xosi);
110     return code;
111 }
112
113 osi_UFSClose(afile)
114      register struct osi_file *afile;
115   {
116       AFS_STATCNT(osi_Close);
117       if(afile->vnode) {
118         VN_RELE(afile->vnode);
119       }
120       
121       osi_FreeSmallSpace(afile);
122       return 0;
123   }
124
125 osi_UFSTruncate(afile, asize)
126     register struct osi_file *afile;
127     afs_int32 asize; {
128     struct AFS_UCRED *oldCred;
129     struct vattr tvattr;
130     register afs_int32 code;
131     struct osi_stat tstat;
132     mon_state_t ms;
133     AFS_STATCNT(osi_Truncate);
134
135     /* This routine only shrinks files, and most systems
136      * have very slow truncates, even when the file is already
137      * small enough.  Check now and save some time.
138      */
139     code = afs_osi_Stat(afile, &tstat);
140     if (code || tstat.size <= asize) return code;
141     MObtainWriteLock(&afs_xosi,321);    
142     AFS_GUNLOCK();
143     tvattr.va_mask = AT_SIZE;
144     tvattr.va_size = asize;
145     AFS_VOP_SETATTR(afile->vnode, &tvattr, 0, &afs_osi_cred, code);
146     AFS_GLOCK();
147     MReleaseWriteLock(&afs_xosi);
148     return code;
149 }
150
151 #ifdef AFS_SGI_EFS_IOPS_ENV
152 void osi_DisableAtimes(avp)
153 struct vnode *avp;
154 {
155    if (afs_CacheFSType == AFS_SGI_EFS_CACHE) 
156    {
157    struct inode *ip = EFS_VTOI(avp);
158    ip->i_flags &= ~IACC;
159    }
160
161 }
162 #endif /* AFS_SGI_EFS_IOPS_ENV */
163
164
165 /* Generic read interface */
166 afs_osi_Read(afile, offset, aptr, asize)
167     register struct osi_file *afile;
168     int offset;
169     char *aptr;
170     afs_int32 asize; {
171     struct AFS_UCRED *oldCred;
172     ssize_t resid;
173     register afs_int32 code;
174     register afs_int32 cnt1=0;
175     AFS_STATCNT(osi_Read);
176     
177     /**
178       * If the osi_file passed in is NULL, panic only if AFS is not shutting
179       * down. No point in crashing when we are already shutting down
180       */
181     if ( !afile ) {
182         if ( !afs_shuttingdown )
183             osi_Panic("osi_Read called with null param");
184         else
185            return EIO;
186     }
187  
188     if (offset != -1) afile->offset = offset;
189     AFS_GUNLOCK();
190     code = gop_rdwr(UIO_READ, afile->vnode, (caddr_t) aptr, asize, afile->offset,
191                   AFS_UIOSYS, 0, 0x7fffffff, &afs_osi_cred, &resid);
192     AFS_GLOCK();
193     if (code == 0) {
194         code = asize - resid;
195         afile->offset += code;
196 #ifdef AFS_SGI_EFS_IOPS_ENV
197         osi_DisableAtimes(afile->vnode);
198 #endif /* AFS_SGI_EFS_IOPS_ENV */
199     }
200     else {
201         afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, resid,
202                  ICL_TYPE_INT32, code);
203         code = -1;
204     }
205     return code;
206 }
207
208 /* Generic write interface */
209 afs_osi_Write(afile, offset, aptr, asize)
210     register struct osi_file *afile;
211     char *aptr;
212     afs_int32 offset;
213     afs_int32 asize; {
214     struct AFS_UCRED *oldCred;
215     ssize_t resid;
216     register afs_int32 code;
217     AFS_STATCNT(osi_Write);
218     if ( !afile )
219         osi_Panic("afs_osi_Write called with null param");
220     if (offset != -1) afile->offset = offset;
221     AFS_GUNLOCK();
222     code = gop_rdwr(UIO_WRITE, afile->vnode, (caddr_t) aptr, asize, afile->offset,
223                   AFS_UIOSYS, 0, 0x7fffffff, &afs_osi_cred, &resid);
224     AFS_GLOCK();
225     if (code == 0) {
226         code = asize - resid;
227         afile->offset += code;
228     }
229     else {
230         if (code == ENOSPC) afs_warnuser("\n\n\n*** Cache partition is FULL - Decrease cachesize!!! ***\n\n");
231         code = -1;
232     }
233     if (afile->proc) {
234         (*afile->proc)(afile, code);
235     }
236     return code;
237 }
238
239
240 /*  This work should be handled by physstrat in ca/machdep.c.
241     This routine written from the RT NFS port strategy routine.
242     It has been generalized a bit, but should still be pretty clear. */
243 int afs_osi_MapStrategy(aproc, bp)
244         int (*aproc)();
245         register struct buf *bp;
246 {
247     afs_int32 returnCode;
248
249     AFS_STATCNT(osi_MapStrategy);
250     returnCode = (*aproc) (bp);
251
252     return returnCode;
253 }
254
255
256
257 void
258 shutdown_osifile()
259 {
260   extern int afs_cold_shutdown;
261
262   AFS_STATCNT(shutdown_osifile);
263   if (afs_cold_shutdown) {
264     afs_osicred_initialized = 0;
265   }
266 }
267