Skip to content

Commit 7bc211f

Browse files
author
Christoph Hellwig
committed
import basic uio support code from FreeBSD
1 parent 7bf893f commit 7bc211f

File tree

5 files changed

+223
-0
lines changed

5 files changed

+223
-0
lines changed

build.mak

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ arch/x64/boot32.o: loader.elf
8181
fs = fs/fs.o bootfs.o
8282

8383
fs += fs/vfs/main.o \
84+
fs/vfs/subr_uio.o \
8485
fs/vfs/vfs_conf.o \
8586
fs/vfs/vfs_lookup.o \
8687
fs/vfs/vfs_mount.o \

fs/vfs/subr_uio.c

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*-
2+
* Copyright (c) 1982, 1986, 1991, 1993
3+
* The Regents of the University of California. All rights reserved.
4+
* (c) UNIX System Laboratories, Inc.
5+
* All or some portions of this file are derived from material licensed
6+
* to the University of California by American Telephone and Telegraph
7+
* Co. or Unix System Laboratories, Inc. and are reproduced herein with
8+
* the permission of UNIX System Laboratories, Inc.
9+
*
10+
* Redistribution and use in source and binary forms, with or without
11+
* modification, are permitted provided that the following conditions
12+
* are met:
13+
* 1. Redistributions of source code must retain the above copyright
14+
* notice, this list of conditions and the following disclaimer.
15+
* 2. Redistributions in binary form must reproduce the above copyright
16+
* notice, this list of conditions and the following disclaimer in the
17+
* documentation and/or other materials provided with the distribution.
18+
* 4. Neither the name of the University nor the names of its contributors
19+
* may be used to endorse or promote products derived from this software
20+
* without specific prior written permission.
21+
*
22+
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25+
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32+
* SUCH DAMAGE.
33+
*
34+
* @(#)kern_subr.c 8.3 (Berkeley) 1/21/94
35+
*/
36+
37+
#include <assert.h>
38+
#include <errno.h>
39+
#include <stdlib.h>
40+
#include <string.h>
41+
#include "uio.h"
42+
43+
int
44+
uiomove(void *cp, int n, struct uio *uio)
45+
{
46+
assert(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE);
47+
48+
while (n > 0 && uio->uio_resid) {
49+
struct iovec *iov = uio->uio_iov;
50+
size_t cnt = iov->iov_len;
51+
if (cnt == 0) {
52+
uio->uio_iov++;
53+
uio->uio_iovcnt--;
54+
continue;
55+
}
56+
if (cnt > n)
57+
cnt = n;
58+
59+
if (uio->uio_rw == UIO_READ)
60+
memcpy(iov->iov_base, cp, cnt);
61+
else
62+
memcpy(cp, iov->iov_base, cnt);
63+
64+
iov->iov_base = (char *)iov->iov_base + cnt;
65+
iov->iov_len -= cnt;
66+
uio->uio_resid -= cnt;
67+
uio->uio_offset += cnt;
68+
cp = (char *)cp + cnt;
69+
n -= cnt;
70+
}
71+
72+
return 0;
73+
}
74+
75+
/*
76+
* Wrapper for uiomove() that validates the arguments against a known-good
77+
* kernel buffer. Currently, uiomove accepts a signed (n) argument, which
78+
* is almost definitely a bad thing, so we catch that here as well. We
79+
* return a runtime failure, but it might be desirable to generate a runtime
80+
* assertion failure instead.
81+
*/
82+
int
83+
uiomove_frombuf(void *buf, int buflen, struct uio *uio)
84+
{
85+
size_t offset, n;
86+
87+
if (uio->uio_offset < 0 || uio->uio_resid < 0 ||
88+
(offset = uio->uio_offset) != uio->uio_offset)
89+
return (EINVAL);
90+
if (buflen <= 0 || offset >= buflen)
91+
return (0);
92+
if ((n = buflen - offset) > IOSIZE_MAX)
93+
return (EINVAL);
94+
return (uiomove((char *)buf + offset, n, uio));
95+
}
96+
97+
/*
98+
* In OSv we don't really copy the iovect in because it lives in the same
99+
* address space. But keeping this abstraction still allows keeping the
100+
* checks in one place and eases porting code.
101+
*/
102+
int
103+
copyinuio(struct iovec *iovp, u_int iovcnt, struct uio **uiop)
104+
{
105+
struct uio *uio;
106+
int i;
107+
108+
*uiop = NULL;
109+
if (iovcnt > UIO_MAXIOV)
110+
return (EINVAL);
111+
uio = malloc(sizeof(*uio));
112+
113+
uio->uio_iov = iovp;
114+
uio->uio_iovcnt = iovcnt;
115+
uio->uio_offset = -1;
116+
uio->uio_resid = 0;
117+
for (i = 0; i < iovcnt; i++) {
118+
if (iovp->iov_len > IOSIZE_MAX - uio->uio_resid) {
119+
free(uio);
120+
return (EINVAL);
121+
}
122+
uio->uio_resid += iovp->iov_len;
123+
iovp++;
124+
}
125+
*uiop = uio;
126+
return (0);
127+
}

fs/vfs/uio.h

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*-
2+
* Copyright (c) 1982, 1986, 1993, 1994
3+
* The Regents of the University of California. All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
* 1. Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* 2. Redistributions in binary form must reproduce the above copyright
11+
* notice, this list of conditions and the following disclaimer in the
12+
* documentation and/or other materials provided with the distribution.
13+
* 4. Neither the name of the University nor the names of its contributors
14+
* may be used to endorse or promote products derived from this software
15+
* without specific prior written permission.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20+
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27+
* SUCH DAMAGE.
28+
*
29+
* @(#)uio.h 8.5 (Berkeley) 2/22/94
30+
* $FreeBSD$
31+
*/
32+
33+
#ifndef _UIO_H_
34+
#define _UIO_H_
35+
36+
#include <sys/types.h>
37+
#include <sys/uio.h>
38+
#include <limits.h>
39+
40+
enum uio_rw { UIO_READ, UIO_WRITE };
41+
42+
/*
43+
* Safe default to prevent possible overflows in user code, otherwise could
44+
* be SSIZE_T_MAX.
45+
*/
46+
#define IOSIZE_MAX INT_MAX
47+
48+
struct uio {
49+
struct iovec *uio_iov; /* scatter/gather list */
50+
int uio_iovcnt; /* length of scatter/gather list */
51+
off_t uio_offset; /* offset in target object */
52+
ssize_t uio_resid; /* remaining bytes to process */
53+
enum uio_rw uio_rw; /* operation */
54+
};
55+
56+
int copyinuio(struct iovec *iovp, u_int iovcnt, struct uio **uiop);
57+
int uiomove(void *cp, int n, struct uio *uio);
58+
59+
#endif /* !_UIO_H_ */

fs/vfs/vnode.h

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "file.h"
3636
#include "list.h"
3737
#include "dirent.h"
38+
#include "uio.h"
3839

3940
struct vfsops;
4041
struct vnops;

licenses/ucb+att.txt

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*-
2+
* Copyright (c) 1982, 1986, 1991, 1993
3+
* The Regents of the University of California. All rights reserved.
4+
* (c) UNIX System Laboratories, Inc.
5+
* All or some portions of this file are derived from material licensed
6+
* to the University of California by American Telephone and Telegraph
7+
* Co. or Unix System Laboratories, Inc. and are reproduced herein with
8+
* the permission of UNIX System Laboratories, Inc.
9+
*
10+
* Redistribution and use in source and binary forms, with or without
11+
* modification, are permitted provided that the following conditions
12+
* are met:
13+
* 1. Redistributions of source code must retain the above copyright
14+
* notice, this list of conditions and the following disclaimer.
15+
* 2. Redistributions in binary form must reproduce the above copyright
16+
* notice, this list of conditions and the following disclaimer in the
17+
* documentation and/or other materials provided with the distribution.
18+
* 4. Neither the name of the University nor the names of its contributors
19+
* may be used to endorse or promote products derived from this software
20+
* without specific prior written permission.
21+
*
22+
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25+
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32+
* SUCH DAMAGE.
33+
*
34+
* @(#)kern_subr.c 8.3 (Berkeley) 1/21/94
35+
*/

0 commit comments

Comments
 (0)