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