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