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