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