freebsd-almost-working-client-20020216
[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("$Header$");
14
15 #include "../afs/sysincludes.h" /* Standard vendor system headers */
16 #include "../afs/afsincludes.h" /* Afs-based standard headers */
17 #include "../afs/afs_stats.h"  /* afs statistics */
18
19
20 int afs_osicred_initialized=0;
21 struct  AFS_UCRED afs_osi_cred;
22 afs_lock_t afs_xosi;            /* lock is for tvattr */
23 extern struct osi_dev cacheDev;
24 extern struct mount *afs_cacheVfsp;
25
26
27 void *osi_UFSOpen(ainode)
28     afs_int32 ainode;
29 {
30     struct inode *ip;
31     register struct osi_file *afile = NULL;
32     extern int cacheDiskType;
33     afs_int32 code = 0;
34     int dummy;
35     AFS_STATCNT(osi_UFSOpen);
36     if(cacheDiskType != AFS_FCACHE_TYPE_UFS) {
37         osi_Panic("UFSOpen called for non-UFS cache\n");
38     }
39     if (!afs_osicred_initialized) {
40         /* valid for alpha_osf, SunOS, Ultrix */
41         memset((char *)&afs_osi_cred, 0, sizeof(struct AFS_UCRED));
42         afs_osi_cred.cr_ref++;
43         afs_osicred_initialized = 1;
44     }
45     afile = (struct osi_file *) osi_AllocSmallSpace(sizeof(struct osi_file));
46     AFS_GUNLOCK();
47     code = igetinode(afs_cacheVfsp, (dev_t) cacheDev.dev, (ino_t)ainode, &ip, &dummy);
48     AFS_GLOCK();
49     if (code) {
50         osi_FreeSmallSpace(afile);
51         osi_Panic("UFSOpen: igetinode failed");
52     }
53     afile->vnode = ITOV(ip);
54     VOP_UNLOCK(afile->vnode, 0, curproc);
55     afile->size = VTOI(afile->vnode)->i_size;
56     afile->offset = 0;
57     afile->proc = (int (*)()) 0;
58     afile->inum = ainode;        /* for hint validity checking */
59     return (void *)afile;
60 }
61
62 afs_osi_Stat(afile, astat)
63     register struct osi_file *afile;
64     register struct osi_stat *astat; {
65     register afs_int32 code;
66     struct vattr tvattr;
67     AFS_STATCNT(osi_Stat);
68     MObtainWriteLock(&afs_xosi,320);
69     AFS_GUNLOCK();
70     code=VOP_GETATTR(afile->vnode, &tvattr, &afs_osi_cred, curproc);
71     AFS_GLOCK();
72     if (code == 0) {
73         astat->size = tvattr.va_size;
74         astat->blksize = tvattr.va_blocksize;
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 osi_UFSClose(afile)
83      register struct osi_file *afile;
84   {
85       AFS_STATCNT(osi_Close);
86       if(afile->vnode) {
87         AFS_RELE(afile->vnode);
88       }
89       
90       osi_FreeSmallSpace(afile);
91       return 0;
92   }
93
94 osi_UFSTruncate(afile, asize)
95     register struct osi_file *afile;
96     afs_int32 asize; {
97     struct vattr tvattr;
98     register afs_int32 code;
99     struct osi_stat tstat;
100     AFS_STATCNT(osi_Truncate);
101
102     /* This routine only shrinks files, and most systems
103      * have very slow truncates, even when the file is already
104      * small enough.  Check now and save some time.
105      */
106     code = afs_osi_Stat(afile, &tstat);
107     if (code || tstat.size <= asize) return code;
108     MObtainWriteLock(&afs_xosi,321);    
109     VATTR_NULL(&tvattr);
110     tvattr.va_size = asize;
111     AFS_GUNLOCK();
112     code=VOP_SETATTR(afile->vnode, &tvattr, &afs_osi_cred, curproc);
113     AFS_GLOCK();
114     MReleaseWriteLock(&afs_xosi);
115     return code;
116 }
117
118 void osi_DisableAtimes(avp)
119 struct vnode *avp;
120 {
121    struct inode *ip = VTOI(avp);
122    ip->i_flag &= ~IN_ACCESS;
123 }
124
125
126 /* Generic read interface */
127 afs_osi_Read(afile, offset, aptr, asize)
128     register struct osi_file *afile;
129     int offset;
130     char *aptr;
131     afs_int32 asize; {
132     unsigned int resid;
133     register afs_int32 code;
134     register afs_int32 cnt1=0;
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) afile->offset = offset;
149     AFS_GUNLOCK();
150     code = gop_rdwr(UIO_READ, afile->vnode, (caddr_t) aptr, asize, afile->offset,
151                   AFS_UIOSYS, IO_UNIT, &afs_osi_cred, &resid);
152     AFS_GLOCK();
153     if (code == 0) {
154         code = asize - resid;
155         afile->offset += code;
156         osi_DisableAtimes(afile->vnode);
157     }
158     else {
159         afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, resid,
160                  ICL_TYPE_INT32, code);
161         code = -1;
162     }
163     return code;
164 }
165
166 /* Generic write interface */
167 afs_osi_Write(afile, offset, aptr, asize)
168     register struct osi_file *afile;
169     char *aptr;
170     afs_int32 offset;
171     afs_int32 asize; {
172     unsigned int resid;
173     register afs_int32 code;
174     AFS_STATCNT(osi_Write);
175     if ( !afile )
176         osi_Panic("afs_osi_Write called with null param");
177     if (offset != -1) afile->offset = offset;
178     {
179         AFS_GUNLOCK();
180         code = gop_rdwr(UIO_WRITE, afile->vnode, (caddr_t) aptr, asize, afile->offset,
181                   AFS_UIOSYS, IO_UNIT, &afs_osi_cred, &resid);
182         AFS_GLOCK();
183     }
184     if (code == 0) {
185         code = asize - resid;
186         afile->offset += code;
187     }
188     else {
189         code = -1;
190     }
191     if (afile->proc) {
192         (*afile->proc)(afile, code);
193     }
194     return code;
195 }
196
197
198 /*  This work should be handled by physstrat in ca/machdep.c.
199     This routine written from the RT NFS port strategy routine.
200     It has been generalized a bit, but should still be pretty clear. */
201 int afs_osi_MapStrategy(aproc, bp)
202         int (*aproc)();
203         register struct buf *bp;
204 {
205     afs_int32 returnCode;
206
207     AFS_STATCNT(osi_MapStrategy);
208     returnCode = (*aproc) (bp);
209
210     return returnCode;
211 }
212
213
214
215 void
216 shutdown_osifile()
217 {
218   extern int afs_cold_shutdown;
219
220   AFS_STATCNT(shutdown_osifile);
221   if (afs_cold_shutdown) {
222     afs_osicred_initialized = 0;
223   }
224 }
225