initial-freebsd-port-work-20010414
[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 "../afs/param.h"       /* Should be always first */
11 #include "../afs/sysincludes.h" /* Standard vendor system headers */
12 #include "../afs/afsincludes.h" /* Afs-based standard headers */
13 #include "../afs/afs_stats.h"  /* afs statistics */
14
15
16 int afs_osicred_initialized=0;
17 struct  AFS_UCRED afs_osi_cred;
18 afs_lock_t afs_xosi;            /* lock is for tvattr */
19 extern struct osi_dev cacheDev;
20 extern struct mount *afs_cacheVfsp;
21
22
23 void *osi_UFSOpen(ainode)
24     afs_int32 ainode;
25 {
26     struct inode *ip;
27     register struct osi_file *afile = NULL;
28     extern int cacheDiskType;
29     afs_int32 code = 0;
30     int dummy;
31     AFS_STATCNT(osi_UFSOpen);
32     if(cacheDiskType != AFS_FCACHE_TYPE_UFS) {
33         osi_Panic("UFSOpen called for non-UFS cache\n");
34     }
35     if (!afs_osicred_initialized) {
36         /* valid for alpha_osf, SunOS, Ultrix */
37         bzero((char *)&afs_osi_cred, sizeof(struct AFS_UCRED));
38         afs_osi_cred.cr_ref++;
39         afs_osicred_initialized = 1;
40     }
41     afile = (struct osi_file *) osi_AllocSmallSpace(sizeof(struct osi_file));
42     AFS_GUNLOCK();
43     code = igetinode(afs_cacheVfsp, (dev_t) cacheDev.dev, (ino_t)ainode, &ip, &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 afs_osi_Stat(afile, astat)
59     register struct osi_file *afile;
60     register struct osi_stat *astat; {
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_cred, code);
67     AFS_GLOCK();
68     if (code == 0) {
69         astat->size = tvattr.va_size;
70         astat->blksize = tvattr.va_blocksize;
71         astat->mtime = tvattr.va_mtime.tv_sec;
72         astat->atime = tvattr.va_atime.tv_sec;
73     }
74     MReleaseWriteLock(&afs_xosi);
75     return code;
76 }
77
78 osi_UFSClose(afile)
79      register struct osi_file *afile;
80   {
81       AFS_STATCNT(osi_Close);
82       if(afile->vnode) {
83         AFS_RELE(afile->vnode);
84       }
85       
86       osi_FreeSmallSpace(afile);
87       return 0;
88   }
89
90 osi_UFSTruncate(afile, asize)
91     register struct osi_file *afile;
92     afs_int32 asize; {
93     struct AFS_UCRED *oldCred;
94     struct vattr tvattr;
95     register afs_int32 code;
96     struct osi_stat tstat;
97     AFS_STATCNT(osi_Truncate);
98
99     /* This routine only shrinks files, and most systems
100      * have very slow truncates, even when the file is already
101      * small enough.  Check now and save some time.
102      */
103     code = afs_osi_Stat(afile, &tstat);
104     if (code || tstat.size <= asize) 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_cred;
112     /* temporarily use superuser credentials */
113     tvattr.va_size = asize;
114     AFS_GUNLOCK();
115     VOP_SETATTR(afile->vnode, &tvattr, &afs_osi_cred, code);
116     AFS_GLOCK();
117     curproc->p_cred->pc_ucred = oldCred;     /* restore */
118     MReleaseWriteLock(&afs_xosi);
119     return code;
120 }
121
122 void osi_DisableAtimes(avp)
123 struct vnode *avp;
124 {
125    struct inode *ip = VTOI(avp);
126    ip->i_flag &= ~IACC;
127 }
128
129
130 /* Generic read interface */
131 afs_osi_Read(afile, offset, aptr, asize)
132     register struct osi_file *afile;
133     int offset;
134     char *aptr;
135     afs_int32 asize; {
136     struct AFS_UCRED *oldCred;
137     unsigned int resid;
138     register afs_int32 code;
139     register afs_int32 cnt1=0;
140     AFS_STATCNT(osi_Read);
141
142     /**
143       * If the osi_file passed in is NULL, panic only if AFS is not shutting
144       * down. No point in crashing when we are already shutting down
145       */
146     if ( !afile ) {
147         if ( !afs_shuttingdown )
148             osi_Panic("osi_Read called with null param");
149         else
150             return EIO;
151     }
152
153     if (offset != -1) afile->offset = offset;
154     AFS_GUNLOCK();
155     code = gop_rdwr(UIO_READ, afile->vnode, (caddr_t) aptr, asize, afile->offset,
156                   AFS_UIOSYS, IO_UNIT, &afs_osi_cred, &resid);
157     AFS_GLOCK();
158     if (code == 0) {
159         code = asize - resid;
160         afile->offset += code;
161         osi_DisableAtimes(afile->vnode);
162     }
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 afs_osi_Write(afile, offset, aptr, asize)
173     register struct osi_file *afile;
174     char *aptr;
175     afs_int32 offset;
176     afs_int32 asize; {
177     struct AFS_UCRED *oldCred;
178     unsigned int resid;
179     register afs_int32 code;
180     AFS_STATCNT(osi_Write);
181     if ( !afile )
182         osi_Panic("afs_osi_Write called with null param");
183     if (offset != -1) afile->offset = offset;
184     {
185         struct ucred *tmpcred = curproc->p_cred->pc_ucred;
186         curproc->p_cred->pc_ucred = &afs_osi_cred;
187         AFS_GUNLOCK();
188         code = gop_rdwr(UIO_WRITE, afile->vnode, (caddr_t) aptr, asize, afile->offset,
189                   AFS_UIOSYS, IO_UNIT, &afs_osi_cred, &resid);
190         AFS_GLOCK();
191         curproc->p_cred->pc_ucred = tmpcred;
192     }
193     if (code == 0) {
194         code = asize - resid;
195         afile->offset += code;
196     }
197     else {
198         code = -1;
199     }
200     if (afile->proc) {
201         (*afile->proc)(afile, code);
202     }
203     return code;
204 }
205
206
207 /*  This work should be handled by physstrat in ca/machdep.c.
208     This routine written from the RT NFS port strategy routine.
209     It has been generalized a bit, but should still be pretty clear. */
210 int afs_osi_MapStrategy(aproc, bp)
211         int (*aproc)();
212         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()
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 }
234