23cc447358624abbb1d366de2f3ba35adea1e0a6
[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     afile->inum = ainode->ufs;  /* for hint validity checking */
66     return (void *)afile;
67 }
68
69 int
70 afs_osi_Stat(register struct osi_file *afile, register struct osi_stat *astat)
71 {
72     register afs_int32 code;
73     struct vattr tvattr;
74     AFS_STATCNT(osi_Stat);
75     MObtainWriteLock(&afs_xosi, 320);
76     AFS_GUNLOCK();
77     tvattr.va_mask = AT_SIZE | AT_BLKSIZE | AT_MTIME | AT_ATIME;
78     AFS_VOP_GETATTR(afile->vnode, &tvattr, 0, &afs_osi_cred, code);
79     AFS_GLOCK();
80     if (code == 0) {
81         astat->size = tvattr.va_size;
82         astat->mtime = tvattr.va_mtime.tv_sec;
83         astat->atime = tvattr.va_atime.tv_sec;
84     }
85     MReleaseWriteLock(&afs_xosi);
86     return code;
87 }
88
89 int
90 osi_UFSClose(register struct osi_file *afile)
91 {
92     AFS_STATCNT(osi_Close);
93     if (afile->vnode) {
94         VN_RELE(afile->vnode);
95     }
96
97     osi_FreeSmallSpace(afile);
98     return 0;
99 }
100
101 int
102 osi_UFSTruncate(register struct osi_file *afile, afs_int32 asize)
103 {
104     afs_ucred_t *oldCred;
105     struct vattr tvattr;
106     register afs_int32 code;
107     struct osi_stat tstat;
108     mon_state_t ms;
109     AFS_STATCNT(osi_Truncate);
110
111     /* This routine only shrinks files, and most systems
112      * have very slow truncates, even when the file is already
113      * small enough.  Check now and save some time.
114      */
115     code = afs_osi_Stat(afile, &tstat);
116     if (code || tstat.size <= asize)
117         return code;
118     MObtainWriteLock(&afs_xosi, 321);
119     AFS_GUNLOCK();
120     tvattr.va_mask = AT_SIZE;
121     tvattr.va_size = asize;
122     AFS_VOP_SETATTR(afile->vnode, &tvattr, 0, &afs_osi_cred, code);
123     AFS_GLOCK();
124     MReleaseWriteLock(&afs_xosi);
125     return code;
126 }
127
128
129 /* Generic read interface */
130 int
131 afs_osi_Read(register struct osi_file *afile, int offset, void *aptr,
132              afs_int32 asize)
133 {
134     afs_ucred_t *oldCred;
135     ssize_t resid;
136     register afs_int32 code;
137     register afs_int32 cnt1 = 0;
138     AFS_STATCNT(osi_Read);
139
140     /**
141       * If the osi_file passed in is NULL, panic only if AFS is not shutting
142       * down. No point in crashing when we are already shutting down
143       */
144     if (!afile) {
145         if (!afs_shuttingdown)
146             osi_Panic("osi_Read called with null param");
147         else
148             return EIO;
149     }
150
151     if (offset != -1)
152         afile->offset = offset;
153     AFS_GUNLOCK();
154     code =
155         gop_rdwr(UIO_READ, afile->vnode, (caddr_t) aptr, asize, afile->offset,
156                  AFS_UIOSYS, 0, 0x7fffffff, &afs_osi_cred, &resid);
157     AFS_GLOCK();
158     if (code == 0) {
159         code = asize - resid;
160         afile->offset += code;
161     } else {
162         afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, resid,
163                    ICL_TYPE_INT32, code);
164         code = -1;
165     }
166     return code;
167 }
168
169 /* Generic write interface */
170 int
171 afs_osi_Write(register struct osi_file *afile, afs_int32 offset, void *aptr,
172               afs_int32 asize)
173 {
174     afs_ucred_t *oldCred;
175     ssize_t resid;
176     register afs_int32 code;
177     AFS_STATCNT(osi_Write);
178     if (!afile)
179         osi_Panic("afs_osi_Write called with null param");
180     if (offset != -1)
181         afile->offset = offset;
182     AFS_GUNLOCK();
183     code =
184         gop_rdwr(UIO_WRITE, afile->vnode, (caddr_t) aptr, asize,
185                  afile->offset, AFS_UIOSYS, 0, 0x7fffffff, &afs_osi_cred,
186                  &resid);
187     AFS_GLOCK();
188     if (code == 0) {
189         code = asize - resid;
190         afile->offset += code;
191     } else {
192         if (code == ENOSPC)
193             afs_warnuser
194                 ("\n\n\n*** Cache partition is FULL - Decrease cachesize!!! ***\n\n");
195         code = -1;
196     }
197     if (afile->proc) {
198         (*afile->proc) (afile, code);
199     }
200     return code;
201 }
202
203
204 /*  This work should be handled by physstrat in ca/machdep.c.
205     This routine written from the RT NFS port strategy routine.
206     It has been generalized a bit, but should still be pretty clear. */
207 int
208 afs_osi_MapStrategy(int (*aproc) (), register struct buf *bp)
209 {
210     afs_int32 returnCode;
211
212     AFS_STATCNT(osi_MapStrategy);
213     returnCode = (*aproc) (bp);
214
215     return returnCode;
216 }
217
218
219
220 void
221 shutdown_osifile(void)
222 {
223     AFS_STATCNT(shutdown_osifile);
224     if (afs_cold_shutdown) {
225         afs_osicred_initialized = 0;
226     }
227 }