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