osi_UFSOpen returns struct osi_file *
[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 struct osi_file *
145 osi_VxfsOpen(afs_dcache_id_t *ainode)
146 {
147     struct vnode *vp;
148     register 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 afile;
164 }
165 #endif /* AFS_HAVE_VXFS */
166
167 struct osi_file *
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     register struct osi_file *afile = NULL;
176     afs_int32 code = 0;
177     int dummy;
178     char fname[1024];
179 #ifdef AFS_CACHE_VNODE_PATH
180     char namebuf[1024];
181     struct pathname lookpn;
182 #endif
183     struct osi_stat tstat;
184     afile = (struct osi_file *)osi_AllocSmallSpace(sizeof(struct osi_file));
185     AFS_GUNLOCK();
186
187 /*
188  * AFS_CACHE_VNODE_PATH can be used with any file system, including ZFS or tmpfs.
189  * The ainode is not an inode number but a signed index used to generate file names. 
190  */
191 #ifdef AFS_CACHE_VNODE_PATH
192         switch (ainode->ufs) {
193         case AFS_CACHE_CELLS_INODE:
194             snprintf(fname, 1024, "%s/%s", afs_cachebasedir, "CellItems");
195             break;
196         case AFS_CACHE_ITEMS_INODE:
197             snprintf(fname, 1024, "%s/%s", afs_cachebasedir, "CacheItems");
198             break;
199         case AFS_CACHE_VOLUME_INODE:
200             snprintf(fname, 1024, "%s/%s", afs_cachebasedir, "VolumeItems");
201             break;
202         default:
203             dummy = ainode->ufs / afs_numfilesperdir;
204             snprintf(fname, 1024, "%s/D%d/V%d", afs_cachebasedir, dummy, ainode->ufs);
205     }
206                 
207         /* Can not use vn_open or lookupname, they use user's CRED() 
208      * We need to run as root So must use low level lookuppnvp 
209      * assume fname starts with /
210          */
211
212         code = pn_get_buf(fname, AFS_UIOSYS, &lookpn, namebuf, sizeof(namebuf));
213     if (code != 0) 
214         osi_Panic("UfsOpen: pn_get_buf failed %ld %s %ld", code, fname, ainode->ufs);
215  
216         VN_HOLD(rootdir); /* released in loopuppnvp */
217         code = lookuppnvp(&lookpn, NULL, FOLLOW, NULL, &vp, 
218            rootdir, rootdir, &afs_osi_cred); 
219     if (code != 0)  
220         osi_Panic("UfsOpen: lookuppnvp failed %ld %s %ld", code, fname, ainode->ufs);
221         
222 #ifdef AFS_SUN511_ENV
223     code = VOP_OPEN(&vp, FREAD|FWRITE, &afs_osi_cred, NULL);
224 #else
225     code = VOP_OPEN(&vp, FREAD|FWRITE, &afs_osi_cred);
226 #endif
227
228     if (code != 0)
229         osi_Panic("UfsOpen: VOP_OPEN failed %ld %s %ld", code, fname, ainode->ufs);
230
231 #else
232     code =
233         igetinode(afs_cacheVfsp, (dev_t) cacheDev.dev, ainode->ufs, &ip,
234                   CRED(), &dummy);
235 #endif
236     AFS_GLOCK();
237     if (code) {
238         osi_FreeSmallSpace(afile);
239         osi_Panic("UfsOpen: igetinode failed %ld %s %ld", code, fname, ainode->ufs);
240     }
241 #ifdef AFS_CACHE_VNODE_PATH
242         afile->vnode = vp;
243         code = afs_osi_Stat(afile, &tstat);
244         afile->size = tstat.size;
245 #else
246     afile->vnode = ITOV(ip);
247     afile->size = VTOI(afile->vnode)->i_size;
248 #endif
249     afile->offset = 0;
250     afile->proc = (int (*)())0;
251     return afile;
252 }
253
254 /**
255   * In Solaris 7 we use 64 bit inode numbers
256   */
257 struct osi_file *
258 osi_UFSOpen(afs_dcache_id_t *ainode)
259 {
260     extern int cacheDiskType;
261     AFS_STATCNT(osi_UFSOpen);
262     if (cacheDiskType != AFS_FCACHE_TYPE_UFS) {
263         osi_Panic("UFSOpen called for non-UFS cache\n");
264     }
265     if (!afs_osicred_initialized) {
266         /* valid for alpha_osf, SunOS, Ultrix */
267         memset(&afs_osi_cred, 0, sizeof(afs_ucred_t));
268         crhold(&afs_osi_cred);  /* don't let it evaporate, since it is static */
269         afs_osicred_initialized = 1;
270     }
271 #ifdef AFS_HAVE_VXFS
272     if (afs_CacheFSType == AFS_SUN_VXFS_CACHE)
273         return osi_VxfsOpen(ainode);
274 #endif
275     return osi_UfsOpen(ainode);
276 }
277
278 int
279 afs_osi_Stat(register struct osi_file *afile, register struct osi_stat *astat)
280 {
281     register afs_int32 code;
282     struct vattr tvattr;
283     AFS_STATCNT(osi_Stat);
284     ObtainWriteLock(&afs_xosi, 320);
285     /* Ufs doesn't seem to care about the flags so we pass 0 for now */
286     tvattr.va_mask = AT_ALL;
287     AFS_GUNLOCK();
288 #ifdef AFS_SUN511_ENV 
289     code = VOP_GETATTR(afile->vnode, &tvattr, 0, &afs_osi_cred, NULL);
290 #else
291     code = VOP_GETATTR(afile->vnode, &tvattr, 0, &afs_osi_cred);
292 #endif
293     AFS_GLOCK();
294     if (code == 0) {
295         astat->size = tvattr.va_size;
296         astat->mtime = tvattr.va_mtime.tv_sec;
297         astat->atime = tvattr.va_atime.tv_sec;
298     }
299     ReleaseWriteLock(&afs_xosi);
300     return code;
301 }
302
303 int
304 osi_UFSClose(register struct osi_file *afile)
305 {
306     AFS_STATCNT(osi_Close);
307     if (afile->vnode) {
308         AFS_RELE(afile->vnode);
309     }
310
311     osi_FreeSmallSpace(afile);
312     return 0;
313 }
314
315 int
316 osi_UFSTruncate(register struct osi_file *afile, afs_int32 asize)
317 {
318     afs_ucred_t *oldCred;
319     struct vattr tvattr;
320     register afs_int32 code;
321     struct osi_stat tstat;
322     AFS_STATCNT(osi_Truncate);
323
324     /* This routine only shrinks files, and most systems
325      * have very slow truncates, even when the file is already
326      * small enough.  Check now and save some time.
327      */
328     code = afs_osi_Stat(afile, &tstat);
329     if (code || tstat.size <= asize)
330         return code;
331     ObtainWriteLock(&afs_xosi, 321);
332     tvattr.va_mask = AT_SIZE;
333     tvattr.va_size = asize;
334     /*
335      * The only time a flag is used (ATTR_UTIME) is when we're changing the time 
336      */
337     AFS_GUNLOCK();
338 #ifdef AFS_SUN510_ENV
339     {
340         caller_context_t ct;
341
342         code = VOP_SETATTR(afile->vnode, &tvattr, 0, &afs_osi_cred, &ct);
343     }
344 #else
345     code = VOP_SETATTR(afile->vnode, &tvattr, 0, &afs_osi_cred);
346 #endif
347     AFS_GLOCK();
348     ReleaseWriteLock(&afs_xosi);
349     return code;
350 }
351
352 void
353 osi_DisableAtimes(struct vnode *avp)
354 {
355     if (afs_CacheFSType == AFS_SUN_UFS_CACHE) {
356 #ifndef AFS_CACHE_VNODE_PATH 
357         struct inode *ip = VTOI(avp);
358         rw_enter(&ip->i_contents, RW_READER);
359         mutex_enter(&ip->i_tlock);
360         ip->i_flag &= ~IACC;
361         mutex_exit(&ip->i_tlock);
362         rw_exit(&ip->i_contents);
363 #endif
364     }
365 }
366
367
368 /* Generic read interface */
369 int
370 afs_osi_Read(register struct osi_file *afile, int offset, void *aptr,
371              afs_int32 asize)
372 {
373     afs_ucred_t *oldCred;
374 #if defined(AFS_SUN57_ENV)
375     ssize_t resid;
376 #else
377     int resid;
378 #endif
379     register afs_int32 code;
380     register afs_int32 cnt1 = 0;
381     AFS_STATCNT(osi_Read);
382
383     /**
384       * If the osi_file passed in is NULL, panic only if AFS is not shutting
385       * down. No point in crashing when we are already shutting down
386       */
387     if (!afile) {
388         if (!afs_shuttingdown)
389             osi_Panic("osi_Read called with null param");
390         else
391             return EIO;
392     }
393
394     if (offset != -1)
395         afile->offset = offset;
396     AFS_GUNLOCK();
397     code =
398         gop_rdwr(UIO_READ, afile->vnode, (caddr_t) aptr, asize, afile->offset,
399                  AFS_UIOSYS, 0, 0, &afs_osi_cred, &resid);
400     AFS_GLOCK();
401     if (code == 0) {
402         code = asize - resid;
403         afile->offset += code;
404         osi_DisableAtimes(afile->vnode);
405     } else {
406         afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, resid,
407                    ICL_TYPE_INT32, code);
408         code = -1;
409     }
410     return code;
411 }
412
413 /* Generic write interface */
414 int
415 afs_osi_Write(register struct osi_file *afile, afs_int32 offset, void *aptr,
416               afs_int32 asize)
417 {
418     afs_ucred_t *oldCred;
419 #if defined(AFS_SUN57_ENV)
420     ssize_t resid;
421 #else
422     int resid;
423 #endif
424     register afs_int32 code;
425     AFS_STATCNT(osi_Write);
426     if (!afile)
427         osi_Panic("afs_osi_Write called with null param");
428     if (offset != -1)
429         afile->offset = offset;
430     AFS_GUNLOCK();
431     code =
432         gop_rdwr(UIO_WRITE, afile->vnode, (caddr_t) aptr, asize,
433                  afile->offset, AFS_UIOSYS, 0, RLIM64_INFINITY, &afs_osi_cred,
434                  &resid);
435     AFS_GLOCK();
436     if (code == 0) {
437         code = asize - resid;
438         afile->offset += code;
439     } else {
440         code = -1;
441     }
442     if (afile->proc) {
443         (*afile->proc) (afile, code);
444     }
445     return code;
446 }
447
448
449 /*  This work should be handled by physstrat in ca/machdep.c.
450     This routine written from the RT NFS port strategy routine.
451     It has been generalized a bit, but should still be pretty clear. */
452 int
453 afs_osi_MapStrategy(int (*aproc) (), register struct buf *bp)
454 {
455     afs_int32 returnCode;
456
457     AFS_STATCNT(osi_MapStrategy);
458     returnCode = (*aproc) (bp);
459
460     return returnCode;
461 }
462
463
464
465 void
466 shutdown_osifile(void)
467 {
468     AFS_STATCNT(shutdown_osifile);
469     if (afs_cold_shutdown) {
470         afs_osicred_initialized = 0;
471     }
472 }