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