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