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