b016a76201b09a71ecca87259320183923676d0b
[openafs.git] / src / afs / DUX / 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(afs_int32 ainode)
28 {
29     static struct vnode *tags_vnode = NULL;
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     afile = (struct osi_file *) osi_AllocSmallSpace(sizeof(struct osi_file));
40     AFS_GUNLOCK();
41
42     switch(afs_cacheVfsp->m_stat.f_type) {
43     case MOUNT_UFS:
44        code = igetinode(afs_cacheVfsp, (dev_t) cacheDev.dev, (ino_t)ainode, &ip, &dummy);
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 = NULL;
54        afile->inum = ainode;        /* for hint validity checking */
55        break;
56     case MOUNT_MSFS: {
57        char path[1024];
58        struct nameidata nd, *ndp = &nd;
59        struct utask_nd utnd = { NULL, NULL };
60        struct vattr attr;
61
62        memset(&nd, 0, sizeof(nd));
63        ndp->ni_utnd = &utnd;
64        ndp->ni_nameiop = LOOKUP;
65        ndp->ni_cred = &afs_osi_cred;
66        ndp->ni_segflg  = UIO_SYSSPACE;
67
68        /* get hold of a vnode for the .tags directory, so we can
69            lookup files relative to it */
70        if(tags_vnode == NULL) {
71            ndp->ni_cdir = afs_cacheVfsp->m_vnodecovered;
72            strcpy(path, afs_cacheVfsp->m_stat.f_mntonname);
73            strcat(path, "/.tags");
74            ndp->ni_dirp = path;
75            if((code = namei(ndp)))
76                osi_Panic("failed to lookup %s (%d)", path, code);
77            tags_vnode = ndp->ni_vp;
78        }
79        sprintf(path, "%d", ainode);
80        ndp->ni_dirp = path;
81        ndp->ni_cdir = tags_vnode;
82        if((code = namei(ndp)))
83            osi_Panic("failed to lookup %s (%d)", path, code);
84
85        /* XXX this sucks, chances are we're going to do this again right
86           away, but apparently we can't just set the size to 0 */
87        VOP_GETATTR(ndp->ni_vp, &attr, &afs_osi_cred, code);
88        if(code) 
89            osi_Panic("failed to stat %s (%d)", path, code);
90
91        afile->vnode = ndp->ni_vp;
92        afile->size = attr.va_size;
93        afile->offset = 0;
94        afile->proc = NULL;
95        afile->inum = ainode;        /* for hint validity checking */
96        break;
97     }
98     default:
99         osi_Panic("UFSOpen called for unknown cache-type (%d)", 
100                   afs_cacheVfsp->m_stat.f_type);
101     }
102
103     AFS_GLOCK();
104     return afile;
105 }
106
107 int afs_osi_Stat(register struct osi_file *afile, register struct osi_stat *astat)
108 {
109     register afs_int32 code;
110     struct vattr tvattr;
111     AFS_STATCNT(osi_Stat);
112     MObtainWriteLock(&afs_xosi,320);
113     AFS_GUNLOCK();
114     VOP_GETATTR(afile->vnode, &tvattr, &afs_osi_cred, code);
115     AFS_GLOCK();
116     if (code == 0) {
117         astat->size = tvattr.va_size;
118         astat->blksize = tvattr.va_blocksize;
119         astat->mtime = tvattr.va_mtime.tv_sec;
120         astat->atime = tvattr.va_atime.tv_sec;
121     }
122     MReleaseWriteLock(&afs_xosi);
123     return code;
124 }
125
126 int osi_UFSClose(register struct osi_file *afile)
127 {
128       AFS_STATCNT(osi_Close);
129       if(afile->vnode) {
130         AFS_RELE(afile->vnode);
131       }
132       
133       osi_FreeSmallSpace(afile);
134       return 0;
135 }
136
137 int osi_UFSTruncate(register struct osi_file *afile, afs_int32 asize)
138 {
139     struct AFS_UCRED *oldCred;
140     struct vattr tvattr;
141     register afs_int32 code;
142     struct osi_stat tstat;
143     AFS_STATCNT(osi_Truncate);
144
145     /* This routine only shrinks files, and most systems
146      * have very slow truncates, even when the file is already
147      * small enough.  Check now and save some time.
148      */
149     code = afs_osi_Stat(afile, &tstat);
150     if (code || tstat.size <= asize) return code;
151     MObtainWriteLock(&afs_xosi,321);    
152     VATTR_NULL(&tvattr);
153     /* note that this credential swapping stuff is only necessary because
154         of ufs's references directly to u.u_cred instead of to
155         credentials parameter.  Probably should fix ufs some day. */
156     oldCred = u.u_cred;     /* remember old credentials pointer  */
157     u.u_cred = &afs_osi_cred;   /* temporarily use superuser credentials */
158     tvattr.va_size = asize;
159     AFS_GUNLOCK();
160     VOP_SETATTR(afile->vnode, &tvattr, &afs_osi_cred, code);
161     AFS_GLOCK();
162     u.u_cred = oldCred;     /* restore */
163     MReleaseWriteLock(&afs_xosi);
164     return code;
165 }
166
167 void osi_DisableAtimes(struct vnode *avp)
168 {
169    struct inode *ip;
170    assert(avp->v_tag == VT_UFS);
171    ip = VTOI(avp);
172    ip->i_flag &= ~IACC;
173 }
174
175
176 /* Generic read interface */
177 int afs_osi_Read(register struct osi_file *afile, int offset, void *aptr, afs_int32 asize)
178 {
179     struct AFS_UCRED *oldCred;
180     unsigned int resid;
181     register afs_int32 code;
182     register afs_int32 cnt1=0;
183     AFS_STATCNT(osi_Read);
184
185     /**
186       * If the osi_file passed in is NULL, panic only if AFS is not shutting
187       * down. No point in crashing when we are already shutting down
188       */
189     if ( !afile ) {
190         if ( !afs_shuttingdown )
191             osi_Panic("osi_Read called with null param");
192         else
193             return EIO;
194     }
195
196     if (offset != -1) afile->offset = offset;
197     AFS_GUNLOCK();
198     code = gop_rdwr(UIO_READ, afile->vnode, (caddr_t) aptr, asize, afile->offset,
199                   AFS_UIOSYS, IO_UNIT, &afs_osi_cred, &resid);
200     AFS_GLOCK();
201     if (code == 0) {
202         code = asize - resid;
203         afile->offset += code;
204         osi_DisableAtimes(afile->vnode);
205     }
206     else {
207         afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, resid,
208                  ICL_TYPE_INT32, code);
209         code = -1;
210     }
211     return code;
212 }
213
214 /* Generic write interface */
215 int afs_osi_Write(register struct osi_file *afile, afs_int32 offset, void *aptr, afs_int32 asize)
216 {
217     struct AFS_UCRED *oldCred;
218     unsigned int resid;
219     register afs_int32 code;
220     AFS_STATCNT(osi_Write);
221     if ( !afile )
222         osi_Panic("afs_osi_Write called with null param");
223     if (offset != -1) afile->offset = offset;
224     {
225         struct ucred *tmpcred = u.u_cred;
226         u.u_cred = &afs_osi_cred;
227         AFS_GUNLOCK();
228         code = gop_rdwr(UIO_WRITE, afile->vnode, (caddr_t) aptr, asize, afile->offset,
229                   AFS_UIOSYS, IO_UNIT, &afs_osi_cred, &resid);
230         AFS_GLOCK();
231         u.u_cred = tmpcred;
232     }
233     if (code == 0) {
234         code = asize - resid;
235         afile->offset += code;
236     }
237     else {
238         code = -1;
239     }
240     if (afile->proc) {
241         (*afile->proc)(afile, code);
242     }
243     return code;
244 }
245
246
247 /*  This work should be handled by physstrat in ca/machdep.c.
248     This routine written from the RT NFS port strategy routine.
249     It has been generalized a bit, but should still be pretty clear. */
250 int afs_osi_MapStrategy(int (*aproc)(), register struct buf *bp)
251 {
252     afs_int32 returnCode;
253
254     AFS_STATCNT(osi_MapStrategy);
255     returnCode = (*aproc) (bp);
256
257     return returnCode;
258 }
259
260
261
262 void shutdown_osifile(void)
263 {
264   extern int afs_cold_shutdown;
265
266   AFS_STATCNT(shutdown_osifile);
267   if (afs_cold_shutdown) {
268     afs_osicred_initialized = 0;
269   }
270 }
271