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