freebsd-20040310
[openafs.git] / src / afs / OBSD / 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 "afs/afsincludes.h"    /* Afs-based standard headers */
18 #include "afs/afs_stats.h"      /* afs statistics */
19
20
21 int afs_osicred_initialized;
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 mount *afs_cacheVfsp;
26
27
28 void *
29 osi_UFSOpen(afs_int32 ainode)
30 {
31     struct osi_file *afile;
32     struct vnode *vp;
33     extern int cacheDiskType;
34     afs_int32 code;
35
36     AFS_STATCNT(osi_UFSOpen);
37     if (cacheDiskType != AFS_FCACHE_TYPE_UFS)
38         osi_Panic("UFSOpen called for non-UFS cache\n");
39     afile = (struct osi_file *)osi_AllocSmallSpace(sizeof(struct osi_file));
40     AFS_GUNLOCK();
41     code = VFS_VGET(cacheDev.mp, (ino_t) ainode, &vp);
42     AFS_GLOCK();
43     if (code == 0 && vp->v_type == VNON)
44         code = ENOENT;
45     if (code) {
46         osi_FreeSmallSpace(afile);
47         osi_Panic("UFSOpen: igetinode failed");
48     }
49     VOP_UNLOCK(vp, 0, curproc);
50     afile->vnode = vp;
51     afile->size = VTOI(vp)->i_ffs_size;
52     afile->offset = 0;
53     afile->proc = NULL;
54     afile->inum = ainode;       /* for hint validity checking */
55     return (void *)afile;
56 }
57
58 int
59 afs_osi_Stat(struct osi_file *afile, struct osi_stat *astat)
60 {
61     afs_int32 code;
62     struct vattr tvattr;
63
64     AFS_STATCNT(osi_Stat);
65     MObtainWriteLock(&afs_xosi, 320);
66     AFS_GUNLOCK();
67     code = VOP_GETATTR(afile->vnode, &tvattr, afs_osi_credp, curproc);
68     AFS_GLOCK();
69     if (code == 0) {
70         astat->size = afile->size = tvattr.va_size;
71         astat->blksize = tvattr.va_blocksize;
72         astat->mtime = tvattr.va_mtime.tv_sec;
73         astat->atime = tvattr.va_atime.tv_sec;
74     }
75     MReleaseWriteLock(&afs_xosi);
76     return code;
77 }
78
79 int
80 osi_UFSClose(struct osi_file *afile)
81 {
82     AFS_STATCNT(osi_Close);
83
84     if (afile->vnode)
85         AFS_RELE(afile->vnode);
86
87     osi_FreeSmallSpace(afile);
88     return 0;
89 }
90
91 int
92 osi_UFSTruncate(struct osi_file *afile, afs_int32 asize)
93 {
94     struct vattr tvattr;
95     afs_int32 code;
96     struct osi_stat tstat;
97
98     AFS_STATCNT(osi_Truncate);
99
100     /*
101      * This routine only shrinks files, and most systems
102      * have very slow truncates, even when the file is already
103      * small enough.  Check now and save some time.
104      */
105     code = afs_osi_Stat(afile, &tstat);
106     if (code || tstat.size <= asize)
107         return code;
108
109     MObtainWriteLock(&afs_xosi, 321);
110     VATTR_NULL(&tvattr);
111     tvattr.va_size = asize;
112     AFS_GUNLOCK();
113     VOP_LOCK(afile->vnode, LK_EXCLUSIVE | LK_RETRY, curproc);
114     code = VOP_SETATTR(afile->vnode, &tvattr, afs_osi_credp, curproc);
115     VOP_UNLOCK(afile->vnode, 0, curproc);
116     AFS_GLOCK();
117     if (code == 0)
118         afile->size = asize;
119     MReleaseWriteLock(&afs_xosi);
120     return code;
121 }
122
123 void
124 osi_DisableAtimes(struct vnode *avp)
125 {
126 #if 0
127     VTOI(avp)->i_flag &= ~IN_ACCESS;
128 #endif
129 }
130
131
132 /* Generic read interface */
133 int
134 afs_osi_Read(struct osi_file *afile, int offset, void *aptr, afs_int32 asize)
135 {
136     unsigned int resid;
137     afs_int32 code;
138
139     AFS_STATCNT(osi_Read);
140
141     /*
142      * If the osi_file passed in is NULL, panic only if AFS is not shutting
143      * down. No point in crashing when we are already shutting down
144      */
145     if (!afile) {
146         if (!afs_shuttingdown)
147             osi_Panic("osi_Read called with null param");
148         else
149             return EIO;
150     }
151
152     if (offset != -1)
153         afile->offset = offset;
154     AFS_GUNLOCK();
155     code =
156         vn_rdwr(UIO_READ, afile->vnode, aptr, asize, afile->offset,
157                 AFS_UIOSYS, IO_UNIT, afs_osi_credp, &resid, curproc);
158     AFS_GLOCK();
159     if (code == 0) {
160         code = asize - resid;
161         afile->offset += code;
162         osi_DisableAtimes(afile->vnode);
163     } else {
164         afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, resid,
165                    ICL_TYPE_INT32, code);
166         code = -1;
167     }
168     return code;
169 }
170
171 /* Generic write interface */
172 int
173 afs_osi_Write(struct osi_file *afile, afs_int32 offset, void *aptr,
174               afs_int32 asize)
175 {
176     unsigned int resid;
177     afs_int32 code;
178
179     AFS_STATCNT(osi_Write);
180     if (!afile)
181         osi_Panic("afs_osi_Write called with null afile");
182     if (offset != -1)
183         afile->offset = offset;
184
185     AFS_GUNLOCK();
186     VOP_LOCK(afile->vnode, LK_EXCLUSIVE | LK_RETRY, curproc);
187     code =
188         vn_rdwr(UIO_WRITE, afile->vnode, (caddr_t) aptr, asize, afile->offset,
189                 AFS_UIOSYS, IO_UNIT, afs_osi_credp, &resid, curproc);
190     VOP_UNLOCK(afile->vnode, 0, curproc);
191     AFS_GLOCK();
192
193     if (code == 0) {
194         code = asize - resid;
195         afile->offset += code;
196         if (afile->offset > afile->size)
197             afile->size = afile->offset;
198     } else
199         code = -1;
200
201     if (afile->proc)
202         (*afile->proc) (afile, code);
203
204     return code;
205 }
206
207 /*
208  * This work should be handled by physstrat in ca/machdep.c.  This routine
209  * written from the RT NFS port strategy routine.  It has been generalized a
210  * bit, but should still be pretty clear.
211  */
212 int
213 afs_osi_MapStrategy(int (*aproc) (), struct buf *bp)
214 {
215     afs_int32 returnCode;
216
217     AFS_STATCNT(osi_MapStrategy);
218     returnCode = (*aproc) (bp);
219
220     return returnCode;
221 }
222
223 void
224 shutdown_osifile(void)
225 {
226     AFS_STATCNT(shutdown_osifile);
227     if (afs_cold_shutdown)
228         afs_osicred_initialized = 0;
229 }