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