Revert "osi_UFSOpen returns struct osi_file *"
[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 <afsconfig.h>
11 #include "afs/param.h"
12
13
14 #include "afs/sysincludes.h"    /* Standard vendor system headers */
15 #include "afsincludes.h"        /* Afs-based standard headers */
16 #include "afs/afs_stats.h"      /* afs statistics */
17
18 afs_ucred_t afs_osi_cred;
19 int afs_osicred_initialized = 0;
20 afs_lock_t afs_xosi;            /* lock is for tvattr */
21 extern struct osi_dev cacheDev;
22 extern struct vfs *afs_cacheVfsp;
23
24 vnode_t *
25 afs_XFSIGetVnode(afs_dcache_id_t *ainode)
26 {
27     struct xfs_inode *ip;
28     int error;
29     vnode_t *vp;
30
31     if ((error =
32          xfs_igetinode(afs_cacheVfsp, (dev_t) cacheDev.dev, ainode->ufs, &ip))) {
33         osi_Panic("afs_XFSIGetVnode: xfs_igetinode failed, error=%d", error);
34     }
35     vp = XFS_ITOV(ip);
36     return vp;
37 }
38
39 /* Force to 64 bits, even for EFS filesystems. */
40 void *
41 osi_UFSOpen(afs_dcache_id_t *ainode)
42 {
43     struct inode *ip;
44     register struct osi_file *afile = NULL;
45     extern int cacheDiskType;
46     afs_int32 code = 0;
47     int dummy;
48     AFS_STATCNT(osi_UFSOpen);
49     if (cacheDiskType != AFS_FCACHE_TYPE_UFS) {
50         osi_Panic("UFSOpen called for non-UFS cache\n");
51     }
52     if (!afs_osicred_initialized) {
53         /* valid for alpha_osf, SunOS, Ultrix */
54         memset(&afs_osi_cred, 0, sizeof(afs_ucred_t));
55         crhold(&afs_osi_cred);  /* don't let it evaporate, since it is static */
56         afs_osicred_initialized = 1;
57     }
58     afile = (struct osi_file *)osi_AllocSmallSpace(sizeof(struct osi_file));
59     AFS_GUNLOCK();
60     afile->vnode = AFS_SGI_IGETVNODE(ainode->ufs);
61     AFS_GLOCK();
62     afile->size = VnodeToSize(afile->vnode);
63     afile->offset = 0;
64     afile->proc = (int (*)())0;
65     return (void *)afile;
66 }
67
68 int
69 afs_osi_Stat(register struct osi_file *afile, register struct osi_stat *astat)
70 {
71     register afs_int32 code;
72     struct vattr tvattr;
73     AFS_STATCNT(osi_Stat);
74     ObtainWriteLock(&afs_xosi, 320);
75     AFS_GUNLOCK();
76     tvattr.va_mask = AT_SIZE | AT_BLKSIZE | AT_MTIME | AT_ATIME;
77     AFS_VOP_GETATTR(afile->vnode, &tvattr, 0, &afs_osi_cred, code);
78     AFS_GLOCK();
79     if (code == 0) {
80         astat->size = tvattr.va_size;
81         astat->mtime = tvattr.va_mtime.tv_sec;
82         astat->atime = tvattr.va_atime.tv_sec;
83     }
84     ReleaseWriteLock(&afs_xosi);
85     return code;
86 }
87
88 int
89 osi_UFSClose(register struct osi_file *afile)
90 {
91     AFS_STATCNT(osi_Close);
92     if (afile->vnode) {
93         VN_RELE(afile->vnode);
94     }
95
96     osi_FreeSmallSpace(afile);
97     return 0;
98 }
99
100 int
101 osi_UFSTruncate(register struct osi_file *afile, afs_int32 asize)
102 {
103     afs_ucred_t *oldCred;
104     struct vattr tvattr;
105     register afs_int32 code;
106     struct osi_stat tstat;
107     mon_state_t ms;
108     AFS_STATCNT(osi_Truncate);
109
110     /* This routine only shrinks files, and most systems
111      * have very slow truncates, even when the file is already
112      * small enough.  Check now and save some time.
113      */
114     code = afs_osi_Stat(afile, &tstat);
115     if (code || tstat.size <= asize)
116         return code;
117     ObtainWriteLock(&afs_xosi, 321);
118     AFS_GUNLOCK();
119     tvattr.va_mask = AT_SIZE;
120     tvattr.va_size = asize;
121     AFS_VOP_SETATTR(afile->vnode, &tvattr, 0, &afs_osi_cred, code);
122     AFS_GLOCK();
123     ReleaseWriteLock(&afs_xosi);
124     return code;
125 }
126
127
128 /* Generic read interface */
129 int
130 afs_osi_Read(register struct osi_file *afile, int offset, void *aptr,
131              afs_int32 asize)
132 {
133     afs_ucred_t *oldCred;
134     ssize_t resid;
135     register afs_int32 code;
136     register afs_int32 cnt1 = 0;
137     AFS_STATCNT(osi_Read);
138
139     /**
140       * If the osi_file passed in is NULL, panic only if AFS is not shutting
141       * down. No point in crashing when we are already shutting down
142       */
143     if (!afile) {
144         if (!afs_shuttingdown)
145             osi_Panic("osi_Read called with null param");
146         else
147             return EIO;
148     }
149
150     if (offset != -1)
151         afile->offset = offset;
152     AFS_GUNLOCK();
153     code =
154         gop_rdwr(UIO_READ, afile->vnode, (caddr_t) aptr, asize, afile->offset,
155                  AFS_UIOSYS, 0, 0x7fffffff, &afs_osi_cred, &resid);
156     AFS_GLOCK();
157     if (code == 0) {
158         code = asize - resid;
159         afile->offset += code;
160     } else {
161         afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, resid,
162                    ICL_TYPE_INT32, code);
163         code = -1;
164     }
165     return code;
166 }
167
168 /* Generic write interface */
169 int
170 afs_osi_Write(register struct osi_file *afile, afs_int32 offset, void *aptr,
171               afs_int32 asize)
172 {
173     afs_ucred_t *oldCred;
174     ssize_t resid;
175     register afs_int32 code;
176     AFS_STATCNT(osi_Write);
177     if (!afile)
178         osi_Panic("afs_osi_Write called with null param");
179     if (offset != -1)
180         afile->offset = offset;
181     AFS_GUNLOCK();
182     code =
183         gop_rdwr(UIO_WRITE, afile->vnode, (caddr_t) aptr, asize,
184                  afile->offset, AFS_UIOSYS, 0, 0x7fffffff, &afs_osi_cred,
185                  &resid);
186     AFS_GLOCK();
187     if (code == 0) {
188         code = asize - resid;
189         afile->offset += code;
190     } else {
191         if (code == ENOSPC)
192             afs_warnuser
193                 ("\n\n\n*** Cache partition is FULL - Decrease cachesize!!! ***\n\n");
194         code = -1;
195     }
196     if (afile->proc) {
197         (*afile->proc) (afile, code);
198     }
199     return code;
200 }
201
202
203 /*  This work should be handled by physstrat in ca/machdep.c.
204     This routine written from the RT NFS port strategy routine.
205     It has been generalized a bit, but should still be pretty clear. */
206 int
207 afs_osi_MapStrategy(int (*aproc) (), register struct buf *bp)
208 {
209     afs_int32 returnCode;
210
211     AFS_STATCNT(osi_MapStrategy);
212     returnCode = (*aproc) (bp);
213
214     return returnCode;
215 }
216
217
218
219 void
220 shutdown_osifile(void)
221 {
222     AFS_STATCNT(shutdown_osifile);
223     if (afs_cold_shutdown) {
224         afs_osicred_initialized = 0;
225     }
226 }