32debc327462008eef3264cd7d9939034fe5fc60
[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
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
18
19 int afs_osicred_initialized = 0;
20 #ifndef AFS_FBSD80_ENV  /* cr_groups is now malloc()'d */
21 afs_ucred_t afs_osi_cred;
22 #endif
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_dcache_id_t *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(afs_cacheVfsp, (ino_t) ainode->ufs, LK_EXCLUSIVE, &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 #if defined(AFS_FBSD80_ENV)
50     VOP_UNLOCK(vp, 0);
51 #else
52     VOP_UNLOCK(vp, 0, curthread);
53 #endif
54     afile->vnode = vp;
55     afile->size = VTOI(vp)->i_size;
56     afile->offset = 0;
57     afile->proc = NULL;
58     return (void *)afile;
59 }
60
61 int
62 afs_osi_Stat(struct osi_file *afile, struct osi_stat *astat)
63 {
64     afs_int32 code;
65     struct vattr tvattr;
66     AFS_STATCNT(osi_Stat);
67     ObtainWriteLock(&afs_xosi, 320);
68     AFS_GUNLOCK();
69 #if defined(AFS_FBSD80_ENV)
70     vn_lock(afile->vnode, LK_EXCLUSIVE | LK_RETRY);
71     code = VOP_GETATTR(afile->vnode, &tvattr, afs_osi_credp);
72     VOP_UNLOCK(afile->vnode, 0);
73 #else
74     vn_lock(afile->vnode, LK_EXCLUSIVE | LK_RETRY, curthread);
75     code = VOP_GETATTR(afile->vnode, &tvattr, afs_osi_credp, curthread);
76     VOP_UNLOCK(afile->vnode, LK_EXCLUSIVE, curthread);
77 #endif
78     AFS_GLOCK();
79     if (code == 0) {
80         astat->size = tvattr.va_size;
81         astat->mtime = tvattr.va_mtime.tv_sec;
82         astat->atime = tvattr.va_atime.tv_sec;
83     }
84     ReleaseWriteLock(&afs_xosi);
85     return code;
86 }
87
88 int
89 osi_UFSClose(struct osi_file *afile)
90 {
91     AFS_STATCNT(osi_Close);
92     if (afile->vnode) {
93         AFS_RELE(afile->vnode);
94     }
95
96     osi_FreeSmallSpace(afile);
97     return 0;
98 }
99
100 int
101 osi_UFSTruncate(struct osi_file *afile, afs_int32 asize)
102 {
103     struct vattr tvattr;
104     struct vnode *vp;
105     afs_int32 code, glocked;
106     AFS_STATCNT(osi_Truncate);
107
108     ObtainWriteLock(&afs_xosi, 321);
109     vp = afile->vnode;
110     /*
111      * This routine only shrinks files, and most systems
112      * have very slow truncates, even when the file is already
113      * small enough.  Check now and save some time.
114      */
115     glocked = ISAFS_GLOCK();
116     if (glocked)
117       AFS_GUNLOCK();
118 #if defined(AFS_FBSD80_ENV)
119     vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
120     code = VOP_GETATTR(afile->vnode, &tvattr, afs_osi_credp);
121 #else
122     vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
123     code = VOP_GETATTR(afile->vnode, &tvattr, afs_osi_credp, curthread);
124 #endif
125     if (code != 0 || tvattr.va_size <= asize)
126         goto out;
127
128     VATTR_NULL(&tvattr);
129     tvattr.va_size = asize;
130 #if defined(AFS_FBSD80_ENV)
131     code = VOP_SETATTR(vp, &tvattr, afs_osi_credp);
132 #else
133     code = VOP_SETATTR(vp, &tvattr, afs_osi_credp, curthread);
134 #endif
135
136 out:
137 #if defined(AFS_FBSD80_ENV)
138     VOP_UNLOCK(vp, 0);
139 #else
140     VOP_UNLOCK(vp, LK_EXCLUSIVE, curthread);
141 #endif
142     if (glocked)
143       AFS_GLOCK();
144     ReleaseWriteLock(&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(struct osi_file *afile, int offset, void *aptr,
159              afs_int32 asize)
160 {
161     int resid;
162     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         if (code > 0) {
191             code = -code;
192         }
193     }
194     return code;
195 }
196
197 /* Generic write interface */
198 int
199 afs_osi_Write(struct osi_file *afile, afs_int32 offset, void *aptr,
200               afs_int32 asize)
201 {
202     unsigned int resid;
203     afs_int32 code;
204     AFS_STATCNT(osi_Write);
205     if (!afile)
206         osi_Panic("afs_osi_Write called with null param");
207     if (offset != -1)
208         afile->offset = offset;
209     {
210         AFS_GUNLOCK();
211         code =
212             gop_rdwr(UIO_WRITE, afile->vnode, (caddr_t) aptr, asize,
213                      afile->offset, AFS_UIOSYS, IO_UNIT, afs_osi_credp,
214                      &resid);
215         AFS_GLOCK();
216     }
217     if (code == 0) {
218         code = asize - resid;
219         afile->offset += code;
220     } else {
221         if (code > 0) {
222             code = -code;
223         }
224     }
225     if (afile->proc) {
226         (*afile->proc) (afile, code);
227     }
228     return code;
229 }
230
231
232 /*  This work should be handled by physstrat in ca/machdep.c.
233     This routine written from the RT NFS port strategy routine.
234     It has been generalized a bit, but should still be pretty clear. */
235 int
236 afs_osi_MapStrategy(int (*aproc) (), struct buf *bp)
237 {
238     afs_int32 returnCode;
239
240     AFS_STATCNT(osi_MapStrategy);
241     returnCode = (*aproc) (bp);
242
243     return returnCode;
244 }
245
246
247
248 void
249 shutdown_osifile(void)
250 {
251     AFS_STATCNT(shutdown_osifile);
252     if (afs_cold_shutdown) {
253         afs_osicred_initialized = 0;
254     }
255 }