split-dcache-fixes-20050604
[openafs.git] / src / afs / afs_osi_uio.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"      /* statistics */
19 #include "afs/afs_cbqueue.h"
20 #include "afs/nfsclient.h"
21 #include "afs/afs_osidnlc.h"
22
23
24 /*
25  * UIO routines
26  */
27
28 /* routine to make copy of uio structure in ainuio, using aoutvec for space */
29 int
30 afsio_copy(struct uio *ainuio, struct uio *aoutuio,
31            register struct iovec *aoutvec)
32 {
33     register int i;
34     register struct iovec *tvec;
35
36     AFS_STATCNT(afsio_copy);
37     if (ainuio->afsio_iovcnt > AFS_MAXIOVCNT)
38         return EINVAL;
39     memcpy((char *)aoutuio, (char *)ainuio, sizeof(struct uio));
40     tvec = ainuio->afsio_iov;
41     aoutuio->afsio_iov = aoutvec;
42     for (i = 0; i < ainuio->afsio_iovcnt; i++) {
43         memcpy((char *)aoutvec, (char *)tvec, sizeof(struct iovec));
44         tvec++;                 /* too many compiler bugs to do this as one expr */
45         aoutvec++;
46     }
47     return 0;
48 }
49
50 /* trim the uio structure to the specified size */
51 int
52 afsio_trim(register struct uio *auio, register afs_int32 asize)
53 {
54     register int i;
55     register struct iovec *tv;
56
57     AFS_STATCNT(afsio_trim);
58     auio->afsio_resid = asize;
59     tv = auio->afsio_iov;
60     /* It isn't clear that multiple iovecs work ok (hasn't been tested!) */
61     for (i = 0;; i++, tv++) {
62         if (i >= auio->afsio_iovcnt || asize <= 0) {
63             /* we're done */
64             auio->afsio_iovcnt = i;
65             break;
66         }
67         if (tv->iov_len <= asize)
68             /* entire iovec is included */
69             asize -= tv->iov_len;       /* this many fewer bytes */
70         else {
71             /* this is the last one */
72             tv->iov_len = asize;
73             auio->afsio_iovcnt = i + 1;
74             break;
75         }
76     }
77     return 0;
78 }
79
80 /* skip asize bytes in the current uio structure */
81 int
82 afsio_skip(register struct uio *auio, register afs_int32 asize)
83 {
84     register struct iovec *tv;  /* pointer to current iovec */
85     register int cnt;
86
87     AFS_STATCNT(afsio_skip);
88     /* It isn't guaranteed that multiple iovecs work ok (hasn't been tested!) */
89     while (asize > 0 && auio->afsio_resid) {
90         tv = auio->afsio_iov;
91         cnt = tv->iov_len;
92         if (cnt == 0) {
93             auio->afsio_iov++;
94             auio->afsio_iovcnt--;
95             continue;
96         }
97         if (cnt > asize)
98             cnt = asize;
99         tv->iov_base = (char *)(tv->iov_base) + cnt;
100         tv->iov_len -= cnt;
101         auio->uio_resid -= cnt;
102         auio->uio_offset += cnt;
103         asize -= cnt;
104     }
105     return 0;
106 }