initial-darwin-support-20010327
[openafs.git] / src / afs / VNOPS / afs_vnop_strategy.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 /*
11  * Implements:
12  * afs_ustrategy
13  */
14
15 #include "../afs/param.h"       /* Should be always first */
16 #if !defined(AFS_HPUX_ENV) && !defined(AFS_SGI_ENV) && !defined(AFS_LINUX20_ENV)
17
18 #include "../afs/sysincludes.h" /* Standard vendor system headers */
19 #include "../afs/afsincludes.h" /* Afs-based standard headers */
20 #include "../afs/afs_stats.h" /* statistics */
21 #include "../afs/afs_cbqueue.h"
22 #include "../afs/nfsclient.h"
23 #include "../afs/afs_osidnlc.h"
24
25
26
27
28 #if     defined(AFS_SUN5_ENV) || defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV)
29 afs_ustrategy(abp, credp)
30     struct AFS_UCRED *credp;
31 #else
32 afs_ustrategy(abp)
33 #endif
34     register struct buf *abp; {
35     register afs_int32 code;
36     struct uio tuio;
37     register struct vcache *tvc = (struct vcache *) abp->b_vp;
38     register afs_int32 len = abp->b_bcount;
39 #if     !defined(AFS_SUN5_ENV) && !defined(AFS_OSF_ENV) && !defined(AFS_DARWIN_ENV)
40 #ifdef  AFS_AIX41_ENV
41     struct ucred *credp;
42 #else
43     struct AFS_UCRED *credp = u.u_cred;
44 #endif
45 #endif
46 #if     defined(AFS_SUN_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_DARWIN_ENV)
47     int async = abp->b_flags & B_ASYNC;
48 #endif
49     struct iovec tiovec[1];
50
51     AFS_STATCNT(afs_ustrategy);
52 #ifdef  AFS_AIX41_ENV
53     /*
54      * So that it won't change while reading it
55      */
56     ObtainReadLock(&tvc->lock);
57     if (tvc->credp) {   
58         credp = tvc->credp;
59         crhold(credp);
60     } else {
61         credp = crref();
62     }
63     ReleaseReadLock(&tvc->lock);
64     osi_Assert(credp);
65 #endif
66     if ((abp->b_flags & B_READ) == B_READ) {
67         /* read b_bcount bytes into kernel address b_un.b_addr starting
68             at byte DEV_BSIZE * b_blkno.  Bzero anything we can't read,
69             and finally call iodone(abp).  File is in abp->b_vp.  Credentials
70             are from u area??
71         */
72         tuio.afsio_iov = tiovec;
73         tuio.afsio_iovcnt = 1;
74 #if     defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
75         tuio.afsio_offset = (u_int) dbtob(abp->b_blkno);
76 #if     defined(AFS_SUN5_ENV)
77         tuio._uio_offset._p._u = 0;
78 #endif
79 #else
80         tuio.afsio_offset = DEV_BSIZE * abp->b_blkno;
81 #endif
82         tuio.afsio_seg = AFS_UIOSYS;
83 #ifdef AFS_UIOFMODE
84         tuio.afsio_fmode = 0;
85 #endif
86         tuio.afsio_resid = abp->b_bcount;
87         tiovec[0].iov_base = abp->b_un.b_addr;
88         tiovec[0].iov_len = abp->b_bcount;
89         /* are user's credentials valid here?  probably, but this
90              sure seems like the wrong things to do. */
91 #if     defined(AFS_SUN5_ENV)
92         code = afs_nlrdwr((struct vcache *) abp->b_vp, &tuio, UIO_READ, 0, credp);
93 #else
94         code = afs_rdwr((struct vcache *) abp->b_vp, &tuio, UIO_READ, 0, credp);
95 #endif
96         if (code == 0) {
97             if (tuio.afsio_resid > 0)
98                 bzero(abp->b_un.b_addr + abp->b_bcount - tuio.afsio_resid, tuio.afsio_resid);
99 #ifdef  AFS_AIX32_ENV
100             /*
101              * If we read a block that is past EOF and the user was not storing
102              * to it, go ahead and write protect the page. This way we will detect
103              * storing beyond EOF in the future
104              */
105             if (dbtob(abp->b_blkno) + abp->b_bcount > tvc->m.Length) {
106                 if ((abp->b_flags & B_PFSTORE) == 0) {
107                     AFS_GUNLOCK();
108                     vm_protectp(tvc->vmh, dbtob(abp->b_blkno)/PAGESIZE,
109                                 abp->b_bcount/PAGESIZE, RDONLY);
110                     AFS_GLOCK();
111                 }
112             }
113 #endif
114         }
115     }
116     else {
117         tuio.afsio_iov = tiovec;
118         tuio.afsio_iovcnt = 1;
119 #if     defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
120         tuio.afsio_offset = (u_int) dbtob(abp->b_blkno);
121 #ifdef  AFS_SUN5_ENV
122         tuio._uio_offset._p._u = 0;
123         tuio.uio_limit = u.u_rlimit[RLIMIT_FSIZE].rlim_cur;
124 #endif
125 #else
126         tuio.afsio_offset = DEV_BSIZE * abp->b_blkno;
127 #endif
128         tuio.afsio_seg = AFS_UIOSYS;
129 #ifdef AFS_UIOFMODE
130         tuio.afsio_fmode = 0;
131 #endif
132 #ifdef  AFS_AIX32_ENV   
133         /*
134          * XXX It this really right? Ideally we should always write block size multiple
135          * and not any arbitrary size, right? XXX
136          */
137         len = MIN(len, tvc->m.Length - dbtob(abp->b_blkno));
138 #endif
139 #ifdef  AFS_ALPHA_ENV
140         len = MIN(abp->b_bcount, ((struct vcache *)abp->b_vp)->m.Length - dbtob(abp->b_blkno));
141 #endif  /* AFS_ALPHA_ENV */
142         tuio.afsio_resid = len;
143         tiovec[0].iov_base = abp->b_un.b_addr;
144         tiovec[0].iov_len = len;
145         /* are user's credentials valid here?  probably, but this
146              sure seems like the wrong things to do. */
147 #if     defined(AFS_SUN5_ENV)
148         code = afs_nlrdwr((struct vcache *) abp->b_vp, &tuio, UIO_WRITE, 0, credp);
149 #else
150         code = afs_rdwr((struct vcache *) abp->b_vp, &tuio, UIO_WRITE, 0, credp);
151 #endif
152     }
153 #if     !defined(AFS_AIX32_ENV) && !defined(AFS_SUN5_ENV)
154 #ifdef AFS_DUX40_ENV
155     if (code) {
156         abp->b_error = code;
157         abp->b_flags |= B_ERROR;
158     }
159     biodone(abp);
160     if (code && !(abp->b_flags & B_READ)) {
161         /* prevent ubc from retrying writes */
162         AFS_GUNLOCK();
163         ubc_invalidate(((struct vnode *)tvc)->v_object,
164                        (vm_offset_t)dbtob(abp->b_blkno),
165                        PAGE_SIZE, B_INVAL);
166         AFS_GLOCK();
167     }
168 #else  /* AFS_DUX40_ENV */
169     iodone(abp);
170 #endif /* AFS_DUX40_ENV */
171 #endif
172 #ifdef  AFS_AIX32_ENV
173     crfree(credp);
174 #endif
175     afs_Trace3(afs_iclSetp, CM_TRACE_STRATEGYDONE, ICL_TYPE_POINTER, tvc,
176                ICL_TYPE_INT32, code,
177                ICL_TYPE_LONG, tuio.afsio_resid);
178     return code;
179 }
180
181 #endif /* !AFS_HPUX_ENV  && !AFS_SGI_ENV && !AFS_LINUX20_ENV */