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