afs: Negate codes using a clear, standard method
[openafs.git] / src / afs / SOLARIS / 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 #include "afs/osi_inode.h"
18
19
20 int afs_osicred_initialized = 0;
21 afs_ucred_t afs_osi_cred;
22 afs_lock_t afs_xosi;            /* lock is for tvattr */
23 extern struct osi_dev cacheDev;
24 extern struct vfs *afs_cacheVfsp;
25
26
27 #ifdef AFS_HAVE_VXFS
28
29 /* Support for UFS and VXFS caches. The assumption here is that the size of
30  * a cache file also does not exceed 32 bits. 
31  */
32
33 /* Initialized in osi_InitCacheFSType(). Used to determine inode type. */
34 int afs_CacheFSType = -1;
35
36 /* pointer to VXFS routine to access vnodes by inode number */
37 int (*vxfs_vx_vp_byino) ();
38
39 /* Initialize the cache operations. Called while initializing cache files. */
40 void
41 afs_InitDualFSCacheOps(struct vnode *vp)
42 {
43     int code;
44     static int inited = 0;
45     struct vfs *vfsp;
46     struct statvfs64 vfst;
47
48     if (inited)
49         return;
50     inited = 1;
51
52     if (vp == NULL)
53         return;
54
55     vfsp = vp->v_vfsp;
56     if (vfsp == NULL)
57         osi_Panic("afs_InitDualFSCacheOps: vp->v_vfsp is NULL");
58     code = VFS_STATVFS(vfsp, &vfst);
59     if (code)
60         osi_Panic("afs_InitDualFSCacheOps: statvfs failed");
61
62     if (strcmp(vfst.f_basetype, "vxfs") == 0) {
63         vxfs_vx_vp_byino = (int (*)())modlookup("vxfs", "vx_vp_byino");
64         if (vxfs_vx_vp_byino == NULL)
65             osi_Panic
66                 ("afs_InitDualFSCacheOps: modlookup(vx_vp_byino) failed");
67
68         afs_CacheFSType = AFS_SUN_VXFS_CACHE;
69         return;
70     }
71
72     afs_CacheFSType = AFS_SUN_UFS_CACHE;
73     return;
74 }
75
76 ino_t
77 VnodeToIno(vnode_t * vp)
78 {
79     int code;
80     struct vattr vattr;
81
82     vattr.va_mask = AT_FSID | AT_NODEID;        /* quick return using this mask. */
83 #ifdef AFS_SUN511_ENV
84     code = VOP_GETATTR(vp, &vattr, 0, &afs_osi_cred, NULL);
85 #else
86     code = VOP_GETATTR(vp, &vattr, 0, &afs_osi_cred);
87 #endif
88     if (code) {
89         osi_Panic("VnodeToIno");
90     }
91     return vattr.va_nodeid;
92 }
93
94 dev_t
95 VnodeToDev(vnode_t * vp)
96 {
97     int code;
98     struct vattr vattr;
99
100     vattr.va_mask = AT_FSID | AT_NODEID;        /* quick return using this mask. */
101     AFS_GUNLOCK();
102 #ifdef AFS_SUN511_ENV
103     code = VOP_GETATTR(vp, &vattr, 0, &afs_osi_cred, NULL);
104 #else
105     code = VOP_GETATTR(vp, &vattr, 0, &afs_osi_cred);
106 #endif
107     AFS_GLOCK();
108     if (code) {
109         osi_Panic("VnodeToDev");
110     }
111     return (dev_t) vattr.va_fsid;
112 }
113
114 afs_int32
115 VnodeToSize(vnode_t * vp)
116 {
117     int code;
118     struct vattr vattr;
119
120     /*
121      * We lock xosi in osi_Stat, so we probably should
122      * lock it here too - RWH.
123      */
124     ObtainWriteLock(&afs_xosi, 578);
125     vattr.va_mask = AT_SIZE;
126     AFS_GUNLOCK();
127 #ifdef AFS_SUN511_ENV
128     code = VOP_GETATTR(vp, &vattr, 0, &afs_osi_cred, NULL);
129 #else
130     code = VOP_GETATTR(vp, &vattr, 0, &afs_osi_cred);
131 #endif
132     AFS_GLOCK();
133     if (code) {
134         osi_Panic("VnodeToSize");
135     }
136     ReleaseWriteLock(&afs_xosi);
137     return (afs_int32) (vattr.va_size);
138 }
139
140 void *
141 osi_VxfsOpen(afs_dcache_id_t *ainode)
142 {
143     struct vnode *vp;
144     struct osi_file *afile = NULL;
145     afs_int32 code = 0;
146     int dummy;
147     afile = (struct osi_file *)osi_AllocSmallSpace(sizeof(struct osi_file));
148     AFS_GUNLOCK();
149     code = (*vxfs_vx_vp_byino) (afs_cacheVfsp, &vp, (unsigned int)ainode->ufs);
150     AFS_GLOCK();
151     if (code) {
152         osi_FreeSmallSpace(afile);
153         osi_Panic("VxfsOpen: vx_vp_byino failed");
154     }
155     afile->vnode = vp;
156     afile->size = VnodeToSize(afile->vnode);
157     afile->offset = 0;
158     afile->proc = (int (*)())0;
159     return (void *)afile;
160 }
161 #endif /* AFS_HAVE_VXFS */
162
163 void *
164 osi_UfsOpen(afs_dcache_id_t *ainode)
165 {
166 #ifdef AFS_CACHE_VNODE_PATH
167     struct vnode *vp;
168 #else
169     struct inode *ip;
170 #endif
171     struct osi_file *afile = NULL;
172     afs_int32 code = 0;
173     int dummy;
174 #ifdef AFS_CACHE_VNODE_PATH
175     char namebuf[1024];
176     struct pathname lookpn;
177 #endif
178     struct osi_stat tstat;
179     afile = (struct osi_file *)osi_AllocSmallSpace(sizeof(struct osi_file));
180     AFS_GUNLOCK();
181
182 /*
183  * AFS_CACHE_VNODE_PATH can be used with any file system, including ZFS or tmpfs.
184  * The ainode is not an inode number but a path.
185  */
186 #ifdef AFS_CACHE_VNODE_PATH
187         /* Can not use vn_open or lookupname, they use user's CRED() 
188          * We need to run as root So must use low level lookuppnvp
189          * assume fname starts with /
190          */
191
192         code = pn_get_buf(ainode->ufs, AFS_UIOSYS, &lookpn, namebuf, sizeof(namebuf));
193     if (code != 0) 
194         osi_Panic("UfsOpen: pn_get_buf failed %ld %s", code, ainode->ufs);
195  
196         VN_HOLD(rootdir); /* released in loopuppnvp */
197         code = lookuppnvp(&lookpn, NULL, FOLLOW, NULL, &vp, 
198            rootdir, rootdir, &afs_osi_cred); 
199     if (code != 0)  
200         osi_Panic("UfsOpen: lookuppnvp failed %ld %s", code, ainode->ufs);
201         
202 #ifdef AFS_SUN511_ENV
203     code = VOP_OPEN(&vp, FREAD|FWRITE, &afs_osi_cred, NULL);
204 #else
205     code = VOP_OPEN(&vp, FREAD|FWRITE, &afs_osi_cred);
206 #endif
207
208     if (code != 0)
209         osi_Panic("UfsOpen: VOP_OPEN failed %ld %s", code, ainode->ufs);
210
211 #else
212     code =
213         igetinode(afs_cacheVfsp, (dev_t) cacheDev.dev, ainode->ufs, &ip,
214                   CRED(), &dummy);
215 #endif
216     AFS_GLOCK();
217     if (code) {
218         osi_FreeSmallSpace(afile);
219         osi_Panic("UfsOpen: igetinode failed %ld %s", code, ainode->ufs);
220     }
221 #ifdef AFS_CACHE_VNODE_PATH
222     afile->vnode = vp;
223     code = afs_osi_Stat(afile, &tstat);
224     afile->size = tstat.size;
225 #else
226     afile->vnode = ITOV(ip);
227     afile->size = VTOI(afile->vnode)->i_size;
228 #endif
229     afile->offset = 0;
230     afile->proc = (int (*)())0;
231     return (void *)afile;
232 }
233
234 /**
235   * In Solaris 7 we use 64 bit inode numbers
236   */
237 void *
238 osi_UFSOpen(afs_dcache_id_t *ainode)
239 {
240     extern int cacheDiskType;
241     AFS_STATCNT(osi_UFSOpen);
242     if (cacheDiskType != AFS_FCACHE_TYPE_UFS) {
243         osi_Panic("UFSOpen called for non-UFS cache\n");
244     }
245     if (!afs_osicred_initialized) {
246         /* valid for alpha_osf, SunOS, Ultrix */
247         memset(&afs_osi_cred, 0, sizeof(afs_ucred_t));
248         crhold(&afs_osi_cred);  /* don't let it evaporate, since it is static */
249         afs_osicred_initialized = 1;
250     }
251 #ifdef AFS_HAVE_VXFS
252     if (afs_CacheFSType == AFS_SUN_VXFS_CACHE)
253         return osi_VxfsOpen(ainode);
254 #endif
255     return osi_UfsOpen(ainode);
256 }
257
258 int
259 afs_osi_Stat(struct osi_file *afile, struct osi_stat *astat)
260 {
261     afs_int32 code;
262     struct vattr tvattr;
263     AFS_STATCNT(osi_Stat);
264     ObtainWriteLock(&afs_xosi, 320);
265     /* Ufs doesn't seem to care about the flags so we pass 0 for now */
266     tvattr.va_mask = AT_ALL;
267     AFS_GUNLOCK();
268 #ifdef AFS_SUN511_ENV 
269     code = VOP_GETATTR(afile->vnode, &tvattr, 0, &afs_osi_cred, NULL);
270 #else
271     code = VOP_GETATTR(afile->vnode, &tvattr, 0, &afs_osi_cred);
272 #endif
273     AFS_GLOCK();
274     if (code == 0) {
275         astat->size = tvattr.va_size;
276         astat->mtime = tvattr.va_mtime.tv_sec;
277         astat->atime = tvattr.va_atime.tv_sec;
278     }
279     ReleaseWriteLock(&afs_xosi);
280     return code;
281 }
282
283 int
284 osi_UFSClose(struct osi_file *afile)
285 {
286     AFS_STATCNT(osi_Close);
287     if (afile->vnode) {
288         AFS_RELE(afile->vnode);
289     }
290
291     osi_FreeSmallSpace(afile);
292     return 0;
293 }
294
295 int
296 osi_UFSTruncate(struct osi_file *afile, afs_int32 asize)
297 {
298     afs_ucred_t *oldCred;
299     struct vattr tvattr;
300     afs_int32 code;
301     struct osi_stat tstat;
302     AFS_STATCNT(osi_Truncate);
303
304     /* This routine only shrinks files, and most systems
305      * have very slow truncates, even when the file is already
306      * small enough.  Check now and save some time.
307      */
308     code = afs_osi_Stat(afile, &tstat);
309     if (code || tstat.size <= asize)
310         return code;
311     ObtainWriteLock(&afs_xosi, 321);
312     tvattr.va_mask = AT_SIZE;
313     tvattr.va_size = asize;
314     /*
315      * The only time a flag is used (ATTR_UTIME) is when we're changing the time 
316      */
317     AFS_GUNLOCK();
318 #ifdef AFS_SUN510_ENV
319     {
320         caller_context_t ct;
321
322         code = VOP_SETATTR(afile->vnode, &tvattr, 0, &afs_osi_cred, &ct);
323     }
324 #else
325     code = VOP_SETATTR(afile->vnode, &tvattr, 0, &afs_osi_cred);
326 #endif
327     AFS_GLOCK();
328     ReleaseWriteLock(&afs_xosi);
329     return code;
330 }
331
332 void
333 osi_DisableAtimes(struct vnode *avp)
334 {
335     if (afs_CacheFSType == AFS_SUN_UFS_CACHE) {
336 #ifndef AFS_CACHE_VNODE_PATH 
337         struct inode *ip = VTOI(avp);
338         rw_enter(&ip->i_contents, RW_READER);
339         mutex_enter(&ip->i_tlock);
340         ip->i_flag &= ~IACC;
341         mutex_exit(&ip->i_tlock);
342         rw_exit(&ip->i_contents);
343 #endif
344     }
345 }
346
347
348 /* Generic read interface */
349 int
350 afs_osi_Read(struct osi_file *afile, int offset, void *aptr,
351              afs_int32 asize)
352 {
353     afs_ucred_t *oldCred;
354     ssize_t resid;
355     afs_int32 code;
356     afs_int32 cnt1 = 0;
357     AFS_STATCNT(osi_Read);
358
359     /**
360       * If the osi_file passed in is NULL, panic only if AFS is not shutting
361       * down. No point in crashing when we are already shutting down
362       */
363     if (!afile) {
364         if (!afs_shuttingdown)
365             osi_Panic("osi_Read called with null param");
366         else
367             return -EIO;
368     }
369
370     if (offset != -1)
371         afile->offset = offset;
372     AFS_GUNLOCK();
373     code =
374         gop_rdwr(UIO_READ, afile->vnode, (caddr_t) aptr, asize, afile->offset,
375                  AFS_UIOSYS, 0, 0, &afs_osi_cred, &resid);
376     AFS_GLOCK();
377     if (code == 0) {
378         code = asize - resid;
379         afile->offset += code;
380         osi_DisableAtimes(afile->vnode);
381     } else {
382         afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, resid,
383                    ICL_TYPE_INT32, code);
384         if (code > 0) {
385             code = -code;
386         }
387     }
388     return code;
389 }
390
391 /* Generic write interface */
392 int
393 afs_osi_Write(struct osi_file *afile, afs_int32 offset, void *aptr,
394               afs_int32 asize)
395 {
396     afs_ucred_t *oldCred;
397     ssize_t resid;
398     afs_int32 code;
399     AFS_STATCNT(osi_Write);
400     if (!afile)
401         osi_Panic("afs_osi_Write called with null param");
402     if (offset != -1)
403         afile->offset = offset;
404     AFS_GUNLOCK();
405     code =
406         gop_rdwr(UIO_WRITE, afile->vnode, (caddr_t) aptr, asize,
407                  afile->offset, AFS_UIOSYS, 0, RLIM64_INFINITY, &afs_osi_cred,
408                  &resid);
409     AFS_GLOCK();
410     if (code == 0) {
411         code = asize - resid;
412         afile->offset += code;
413     } else {
414         if (code > 0) {
415             code = -code;
416         }
417     }
418     if (afile->proc) {
419         (*afile->proc) (afile, code);
420     }
421     return code;
422 }
423
424
425 /*  This work should be handled by physstrat in ca/machdep.c.
426     This routine written from the RT NFS port strategy routine.
427     It has been generalized a bit, but should still be pretty clear. */
428 int
429 afs_osi_MapStrategy(int (*aproc) (), struct buf *bp)
430 {
431     afs_int32 returnCode;
432
433     AFS_STATCNT(osi_MapStrategy);
434     returnCode = (*aproc) (bp);
435
436     return returnCode;
437 }
438
439
440
441 void
442 shutdown_osifile(void)
443 {
444     AFS_STATCNT(shutdown_osifile);
445     if (afs_cold_shutdown) {
446         afs_osicred_initialized = 0;
447     }
448 }