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