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