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