Wed Jun 17 20:49:00 2009 UTC ()
Pull up following revision(s) (requested by rmind in ticket #811):
	sys/kern/sys_generic.c: revision 1.122 via patch
Updates to f_flag need to be made with atomics.


(bouyer)
diff -r1.120 -r1.120.6.1 src/sys/kern/sys_generic.c

cvs diff -r1.120 -r1.120.6.1 src/sys/kern/sys_generic.c (switch to unified diff)

--- src/sys/kern/sys_generic.c 2008/07/02 16:45:20 1.120
+++ src/sys/kern/sys_generic.c 2009/06/17 20:49:00 1.120.6.1
@@ -1,646 +1,645 @@ @@ -1,646 +1,645 @@
1/* $NetBSD: sys_generic.c,v 1.120 2008/07/02 16:45:20 matt Exp $ */ 1/* $NetBSD: sys_generic.c,v 1.120.6.1 2009/06/17 20:49:00 bouyer Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2007, 2008 The NetBSD Foundation, Inc. 4 * Copyright (c) 2007, 2008, 2009 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran. 8 * by Andrew Doran.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright 15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the 16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution. 17 * documentation and/or other materials provided with the distribution.
18 * 18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE. 29 * POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31 31
32/* 32/*
33 * Copyright (c) 1982, 1986, 1989, 1993 33 * Copyright (c) 1982, 1986, 1989, 1993
34 * The Regents of the University of California. All rights reserved. 34 * The Regents of the University of California. All rights reserved.
35 * (c) UNIX System Laboratories, Inc. 35 * (c) UNIX System Laboratories, Inc.
36 * All or some portions of this file are derived from material licensed 36 * All or some portions of this file are derived from material licensed
37 * to the University of California by American Telephone and Telegraph 37 * to the University of California by American Telephone and Telegraph
38 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 38 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
39 * the permission of UNIX System Laboratories, Inc. 39 * the permission of UNIX System Laboratories, Inc.
40 * 40 *
41 * Redistribution and use in source and binary forms, with or without 41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions 42 * modification, are permitted provided that the following conditions
43 * are met: 43 * are met:
44 * 1. Redistributions of source code must retain the above copyright 44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer. 45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright 46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the 47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution. 48 * documentation and/or other materials provided with the distribution.
49 * 3. Neither the name of the University nor the names of its contributors 49 * 3. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software 50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission. 51 * without specific prior written permission.
52 * 52 *
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE. 63 * SUCH DAMAGE.
64 * 64 *
65 * @(#)sys_generic.c 8.9 (Berkeley) 2/14/95 65 * @(#)sys_generic.c 8.9 (Berkeley) 2/14/95
66 */ 66 */
67 67
68/* 68/*
69 * System calls relating to files. 69 * System calls relating to files.
70 */ 70 */
71 71
72#include <sys/cdefs.h> 72#include <sys/cdefs.h>
73__KERNEL_RCSID(0, "$NetBSD: sys_generic.c,v 1.120 2008/07/02 16:45:20 matt Exp $"); 73__KERNEL_RCSID(0, "$NetBSD: sys_generic.c,v 1.120.6.1 2009/06/17 20:49:00 bouyer Exp $");
74 74
75#include <sys/param.h> 75#include <sys/param.h>
76#include <sys/systm.h> 76#include <sys/systm.h>
77#include <sys/filedesc.h> 77#include <sys/filedesc.h>
78#include <sys/ioctl.h> 78#include <sys/ioctl.h>
79#include <sys/file.h> 79#include <sys/file.h>
80#include <sys/proc.h> 80#include <sys/proc.h>
81#include <sys/socketvar.h> 81#include <sys/socketvar.h>
82#include <sys/signalvar.h> 82#include <sys/signalvar.h>
83#include <sys/uio.h> 83#include <sys/uio.h>
84#include <sys/kernel.h> 84#include <sys/kernel.h>
85#include <sys/stat.h> 85#include <sys/stat.h>
86#include <sys/kmem.h> 86#include <sys/kmem.h>
87#include <sys/poll.h> 87#include <sys/poll.h>
88#include <sys/vnode.h> 88#include <sys/vnode.h>
89#include <sys/mount.h> 89#include <sys/mount.h>
90#include <sys/syscallargs.h> 90#include <sys/syscallargs.h>
91#include <sys/ktrace.h> 91#include <sys/ktrace.h>
 92#include <sys/atomic.h>
92 93
93#include <uvm/uvm_extern.h> 94#include <uvm/uvm_extern.h>
94 95
95/* 96/*
96 * Read system call. 97 * Read system call.
97 */ 98 */
98/* ARGSUSED */ 99/* ARGSUSED */
99int 100int
100sys_read(struct lwp *l, const struct sys_read_args *uap, register_t *retval) 101sys_read(struct lwp *l, const struct sys_read_args *uap, register_t *retval)
101{ 102{
102 /* { 103 /* {
103 syscallarg(int) fd; 104 syscallarg(int) fd;
104 syscallarg(void *) buf; 105 syscallarg(void *) buf;
105 syscallarg(size_t) nbyte; 106 syscallarg(size_t) nbyte;
106 } */ 107 } */
107 file_t *fp; 108 file_t *fp;
108 int fd; 109 int fd;
109 110
110 fd = SCARG(uap, fd); 111 fd = SCARG(uap, fd);
111 112
112 if ((fp = fd_getfile(fd)) == NULL) 113 if ((fp = fd_getfile(fd)) == NULL)
113 return (EBADF); 114 return (EBADF);
114 115
115 if ((fp->f_flag & FREAD) == 0) { 116 if ((fp->f_flag & FREAD) == 0) {
116 fd_putfile(fd); 117 fd_putfile(fd);
117 return (EBADF); 118 return (EBADF);
118 } 119 }
119 120
120 /* dofileread() will unuse the descriptor for us */ 121 /* dofileread() will unuse the descriptor for us */
121 return (dofileread(fd, fp, SCARG(uap, buf), SCARG(uap, nbyte), 122 return (dofileread(fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
122 &fp->f_offset, FOF_UPDATE_OFFSET, retval)); 123 &fp->f_offset, FOF_UPDATE_OFFSET, retval));
123} 124}
124 125
125int 126int
126dofileread(int fd, struct file *fp, void *buf, size_t nbyte, 127dofileread(int fd, struct file *fp, void *buf, size_t nbyte,
127 off_t *offset, int flags, register_t *retval) 128 off_t *offset, int flags, register_t *retval)
128{ 129{
129 struct iovec aiov; 130 struct iovec aiov;
130 struct uio auio; 131 struct uio auio;
131 size_t cnt; 132 size_t cnt;
132 int error; 133 int error;
133 lwp_t *l; 134 lwp_t *l;
134 135
135 l = curlwp; 136 l = curlwp;
136 137
137 aiov.iov_base = (void *)buf; 138 aiov.iov_base = (void *)buf;
138 aiov.iov_len = nbyte; 139 aiov.iov_len = nbyte;
139 auio.uio_iov = &aiov; 140 auio.uio_iov = &aiov;
140 auio.uio_iovcnt = 1; 141 auio.uio_iovcnt = 1;
141 auio.uio_resid = nbyte; 142 auio.uio_resid = nbyte;
142 auio.uio_rw = UIO_READ; 143 auio.uio_rw = UIO_READ;
143 auio.uio_vmspace = l->l_proc->p_vmspace; 144 auio.uio_vmspace = l->l_proc->p_vmspace;
144 145
145 /* 146 /*
146 * Reads return ssize_t because -1 is returned on error. Therefore 147 * Reads return ssize_t because -1 is returned on error. Therefore
147 * we must restrict the length to SSIZE_MAX to avoid garbage return 148 * we must restrict the length to SSIZE_MAX to avoid garbage return
148 * values. 149 * values.
149 */ 150 */
150 if (auio.uio_resid > SSIZE_MAX) { 151 if (auio.uio_resid > SSIZE_MAX) {
151 error = EINVAL; 152 error = EINVAL;
152 goto out; 153 goto out;
153 } 154 }
154 155
155 cnt = auio.uio_resid; 156 cnt = auio.uio_resid;
156 error = (*fp->f_ops->fo_read)(fp, offset, &auio, fp->f_cred, flags); 157 error = (*fp->f_ops->fo_read)(fp, offset, &auio, fp->f_cred, flags);
157 if (error) 158 if (error)
158 if (auio.uio_resid != cnt && (error == ERESTART || 159 if (auio.uio_resid != cnt && (error == ERESTART ||
159 error == EINTR || error == EWOULDBLOCK)) 160 error == EINTR || error == EWOULDBLOCK))
160 error = 0; 161 error = 0;
161 cnt -= auio.uio_resid; 162 cnt -= auio.uio_resid;
162 ktrgenio(fd, UIO_READ, buf, cnt, error); 163 ktrgenio(fd, UIO_READ, buf, cnt, error);
163 *retval = cnt; 164 *retval = cnt;
164 out: 165 out:
165 fd_putfile(fd); 166 fd_putfile(fd);
166 return (error); 167 return (error);
167} 168}
168 169
169/* 170/*
170 * Scatter read system call. 171 * Scatter read system call.
171 */ 172 */
172int 173int
173sys_readv(struct lwp *l, const struct sys_readv_args *uap, register_t *retval) 174sys_readv(struct lwp *l, const struct sys_readv_args *uap, register_t *retval)
174{ 175{
175 /* { 176 /* {
176 syscallarg(int) fd; 177 syscallarg(int) fd;
177 syscallarg(const struct iovec *) iovp; 178 syscallarg(const struct iovec *) iovp;
178 syscallarg(int) iovcnt; 179 syscallarg(int) iovcnt;
179 } */ 180 } */
180 181
181 return do_filereadv(SCARG(uap, fd), SCARG(uap, iovp), 182 return do_filereadv(SCARG(uap, fd), SCARG(uap, iovp),
182 SCARG(uap, iovcnt), NULL, FOF_UPDATE_OFFSET, retval); 183 SCARG(uap, iovcnt), NULL, FOF_UPDATE_OFFSET, retval);
183} 184}
184 185
185int 186int
186do_filereadv(int fd, const struct iovec *iovp, int iovcnt, 187do_filereadv(int fd, const struct iovec *iovp, int iovcnt,
187 off_t *offset, int flags, register_t *retval) 188 off_t *offset, int flags, register_t *retval)
188{ 189{
189 struct uio auio; 190 struct uio auio;
190 struct iovec *iov, *needfree = NULL, aiov[UIO_SMALLIOV]; 191 struct iovec *iov, *needfree = NULL, aiov[UIO_SMALLIOV];
191 int i, error; 192 int i, error;
192 size_t cnt; 193 size_t cnt;
193 u_int iovlen; 194 u_int iovlen;
194 struct file *fp; 195 struct file *fp;
195 struct iovec *ktriov = NULL; 196 struct iovec *ktriov = NULL;
196 197
197 if (iovcnt == 0) 198 if (iovcnt == 0)
198 return EINVAL; 199 return EINVAL;
199 200
200 if ((fp = fd_getfile(fd)) == NULL) 201 if ((fp = fd_getfile(fd)) == NULL)
201 return EBADF; 202 return EBADF;
202 203
203 if ((fp->f_flag & FREAD) == 0) { 204 if ((fp->f_flag & FREAD) == 0) {
204 fd_putfile(fd); 205 fd_putfile(fd);
205 return EBADF; 206 return EBADF;
206 } 207 }
207 208
208 if (offset == NULL) 209 if (offset == NULL)
209 offset = &fp->f_offset; 210 offset = &fp->f_offset;
210 else { 211 else {
211 struct vnode *vp = fp->f_data; 212 struct vnode *vp = fp->f_data;
212 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) { 213 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
213 error = ESPIPE; 214 error = ESPIPE;
214 goto out; 215 goto out;
215 } 216 }
216 /* 217 /*
217 * Test that the device is seekable ? 218 * Test that the device is seekable ?
218 * XXX This works because no file systems actually 219 * XXX This works because no file systems actually
219 * XXX take any action on the seek operation. 220 * XXX take any action on the seek operation.
220 */ 221 */
221 error = VOP_SEEK(vp, fp->f_offset, *offset, fp->f_cred); 222 error = VOP_SEEK(vp, fp->f_offset, *offset, fp->f_cred);
222 if (error != 0) 223 if (error != 0)
223 goto out; 224 goto out;
224 } 225 }
225 226
226 iovlen = iovcnt * sizeof(struct iovec); 227 iovlen = iovcnt * sizeof(struct iovec);
227 if (flags & FOF_IOV_SYSSPACE) 228 if (flags & FOF_IOV_SYSSPACE)
228 iov = __UNCONST(iovp); 229 iov = __UNCONST(iovp);
229 else { 230 else {
230 iov = aiov; 231 iov = aiov;
231 if ((u_int)iovcnt > UIO_SMALLIOV) { 232 if ((u_int)iovcnt > UIO_SMALLIOV) {
232 if ((u_int)iovcnt > IOV_MAX) { 233 if ((u_int)iovcnt > IOV_MAX) {
233 error = EINVAL; 234 error = EINVAL;
234 goto out; 235 goto out;
235 } 236 }
236 iov = kmem_alloc(iovlen, KM_SLEEP); 237 iov = kmem_alloc(iovlen, KM_SLEEP);
237 if (iov == NULL) { 238 if (iov == NULL) {
238 error = ENOMEM; 239 error = ENOMEM;
239 goto out; 240 goto out;
240 } 241 }
241 needfree = iov; 242 needfree = iov;
242 } 243 }
243 error = copyin(iovp, iov, iovlen); 244 error = copyin(iovp, iov, iovlen);
244 if (error) 245 if (error)
245 goto done; 246 goto done;
246 } 247 }
247 248
248 auio.uio_iov = iov; 249 auio.uio_iov = iov;
249 auio.uio_iovcnt = iovcnt; 250 auio.uio_iovcnt = iovcnt;
250 auio.uio_rw = UIO_READ; 251 auio.uio_rw = UIO_READ;
251 auio.uio_vmspace = curproc->p_vmspace; 252 auio.uio_vmspace = curproc->p_vmspace;
252 253
253 auio.uio_resid = 0; 254 auio.uio_resid = 0;
254 for (i = 0; i < iovcnt; i++, iov++) { 255 for (i = 0; i < iovcnt; i++, iov++) {
255 auio.uio_resid += iov->iov_len; 256 auio.uio_resid += iov->iov_len;
256 /* 257 /*
257 * Reads return ssize_t because -1 is returned on error. 258 * Reads return ssize_t because -1 is returned on error.
258 * Therefore we must restrict the length to SSIZE_MAX to 259 * Therefore we must restrict the length to SSIZE_MAX to
259 * avoid garbage return values. 260 * avoid garbage return values.
260 */ 261 */
261 if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) { 262 if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
262 error = EINVAL; 263 error = EINVAL;
263 goto done; 264 goto done;
264 } 265 }
265 } 266 }
266 267
267 /* 268 /*
268 * if tracing, save a copy of iovec 269 * if tracing, save a copy of iovec
269 */ 270 */
270 if (ktrpoint(KTR_GENIO)) { 271 if (ktrpoint(KTR_GENIO)) {
271 ktriov = kmem_alloc(iovlen, KM_SLEEP); 272 ktriov = kmem_alloc(iovlen, KM_SLEEP);
272 if (ktriov != NULL) 273 if (ktriov != NULL)
273 memcpy(ktriov, auio.uio_iov, iovlen); 274 memcpy(ktriov, auio.uio_iov, iovlen);
274 } 275 }
275 276
276 cnt = auio.uio_resid; 277 cnt = auio.uio_resid;
277 error = (*fp->f_ops->fo_read)(fp, offset, &auio, fp->f_cred, flags); 278 error = (*fp->f_ops->fo_read)(fp, offset, &auio, fp->f_cred, flags);
278 if (error) 279 if (error)
279 if (auio.uio_resid != cnt && (error == ERESTART || 280 if (auio.uio_resid != cnt && (error == ERESTART ||
280 error == EINTR || error == EWOULDBLOCK)) 281 error == EINTR || error == EWOULDBLOCK))
281 error = 0; 282 error = 0;
282 cnt -= auio.uio_resid; 283 cnt -= auio.uio_resid;
283 *retval = cnt; 284 *retval = cnt;
284 285
285 if (ktriov != NULL) { 286 if (ktriov != NULL) {
286 ktrgeniov(fd, UIO_READ, ktriov, cnt, error); 287 ktrgeniov(fd, UIO_READ, ktriov, cnt, error);
287 kmem_free(ktriov, iovlen); 288 kmem_free(ktriov, iovlen);
288 } 289 }
289 290
290 done: 291 done:
291 if (needfree) 292 if (needfree)
292 kmem_free(needfree, iovlen); 293 kmem_free(needfree, iovlen);
293 out: 294 out:
294 fd_putfile(fd); 295 fd_putfile(fd);
295 return (error); 296 return (error);
296} 297}
297 298
298/* 299/*
299 * Write system call 300 * Write system call
300 */ 301 */
301int 302int
302sys_write(struct lwp *l, const struct sys_write_args *uap, register_t *retval) 303sys_write(struct lwp *l, const struct sys_write_args *uap, register_t *retval)
303{ 304{
304 /* { 305 /* {
305 syscallarg(int) fd; 306 syscallarg(int) fd;
306 syscallarg(const void *) buf; 307 syscallarg(const void *) buf;
307 syscallarg(size_t) nbyte; 308 syscallarg(size_t) nbyte;
308 } */ 309 } */
309 file_t *fp; 310 file_t *fp;
310 int fd; 311 int fd;
311 312
312 fd = SCARG(uap, fd); 313 fd = SCARG(uap, fd);
313 314
314 if ((fp = fd_getfile(fd)) == NULL) 315 if ((fp = fd_getfile(fd)) == NULL)
315 return (EBADF); 316 return (EBADF);
316 317
317 if ((fp->f_flag & FWRITE) == 0) { 318 if ((fp->f_flag & FWRITE) == 0) {
318 fd_putfile(fd); 319 fd_putfile(fd);
319 return (EBADF); 320 return (EBADF);
320 } 321 }
321 322
322 /* dofilewrite() will unuse the descriptor for us */ 323 /* dofilewrite() will unuse the descriptor for us */
323 return (dofilewrite(fd, fp, SCARG(uap, buf), SCARG(uap, nbyte), 324 return (dofilewrite(fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
324 &fp->f_offset, FOF_UPDATE_OFFSET, retval)); 325 &fp->f_offset, FOF_UPDATE_OFFSET, retval));
325} 326}
326 327
327int 328int
328dofilewrite(int fd, struct file *fp, const void *buf, 329dofilewrite(int fd, struct file *fp, const void *buf,
329 size_t nbyte, off_t *offset, int flags, register_t *retval) 330 size_t nbyte, off_t *offset, int flags, register_t *retval)
330{ 331{
331 struct iovec aiov; 332 struct iovec aiov;
332 struct uio auio; 333 struct uio auio;
333 size_t cnt; 334 size_t cnt;
334 int error; 335 int error;
335 336
336 aiov.iov_base = __UNCONST(buf); /* XXXUNCONST kills const */ 337 aiov.iov_base = __UNCONST(buf); /* XXXUNCONST kills const */
337 aiov.iov_len = nbyte; 338 aiov.iov_len = nbyte;
338 auio.uio_iov = &aiov; 339 auio.uio_iov = &aiov;
339 auio.uio_iovcnt = 1; 340 auio.uio_iovcnt = 1;
340 auio.uio_resid = nbyte; 341 auio.uio_resid = nbyte;
341 auio.uio_rw = UIO_WRITE; 342 auio.uio_rw = UIO_WRITE;
342 auio.uio_vmspace = curproc->p_vmspace; 343 auio.uio_vmspace = curproc->p_vmspace;
343 344
344 /* 345 /*
345 * Writes return ssize_t because -1 is returned on error. Therefore 346 * Writes return ssize_t because -1 is returned on error. Therefore
346 * we must restrict the length to SSIZE_MAX to avoid garbage return 347 * we must restrict the length to SSIZE_MAX to avoid garbage return
347 * values. 348 * values.
348 */ 349 */
349 if (auio.uio_resid > SSIZE_MAX) { 350 if (auio.uio_resid > SSIZE_MAX) {
350 error = EINVAL; 351 error = EINVAL;
351 goto out; 352 goto out;
352 } 353 }
353 354
354 cnt = auio.uio_resid; 355 cnt = auio.uio_resid;
355 error = (*fp->f_ops->fo_write)(fp, offset, &auio, fp->f_cred, flags); 356 error = (*fp->f_ops->fo_write)(fp, offset, &auio, fp->f_cred, flags);
356 if (error) { 357 if (error) {
357 if (auio.uio_resid != cnt && (error == ERESTART || 358 if (auio.uio_resid != cnt && (error == ERESTART ||
358 error == EINTR || error == EWOULDBLOCK)) 359 error == EINTR || error == EWOULDBLOCK))
359 error = 0; 360 error = 0;
360 if (error == EPIPE) { 361 if (error == EPIPE) {
361 mutex_enter(proc_lock); 362 mutex_enter(proc_lock);
362 psignal(curproc, SIGPIPE); 363 psignal(curproc, SIGPIPE);
363 mutex_exit(proc_lock); 364 mutex_exit(proc_lock);
364 } 365 }
365 } 366 }
366 cnt -= auio.uio_resid; 367 cnt -= auio.uio_resid;
367 ktrgenio(fd, UIO_WRITE, buf, cnt, error); 368 ktrgenio(fd, UIO_WRITE, buf, cnt, error);
368 *retval = cnt; 369 *retval = cnt;
369 out: 370 out:
370 fd_putfile(fd); 371 fd_putfile(fd);
371 return (error); 372 return (error);
372} 373}
373 374
374/* 375/*
375 * Gather write system call 376 * Gather write system call
376 */ 377 */
377int 378int
378sys_writev(struct lwp *l, const struct sys_writev_args *uap, register_t *retval) 379sys_writev(struct lwp *l, const struct sys_writev_args *uap, register_t *retval)
379{ 380{
380 /* { 381 /* {
381 syscallarg(int) fd; 382 syscallarg(int) fd;
382 syscallarg(const struct iovec *) iovp; 383 syscallarg(const struct iovec *) iovp;
383 syscallarg(int) iovcnt; 384 syscallarg(int) iovcnt;
384 } */ 385 } */
385 386
386 return do_filewritev(SCARG(uap, fd), SCARG(uap, iovp), 387 return do_filewritev(SCARG(uap, fd), SCARG(uap, iovp),
387 SCARG(uap, iovcnt), NULL, FOF_UPDATE_OFFSET, retval); 388 SCARG(uap, iovcnt), NULL, FOF_UPDATE_OFFSET, retval);
388} 389}
389 390
390int 391int
391do_filewritev(int fd, const struct iovec *iovp, int iovcnt, 392do_filewritev(int fd, const struct iovec *iovp, int iovcnt,
392 off_t *offset, int flags, register_t *retval) 393 off_t *offset, int flags, register_t *retval)
393{ 394{
394 struct uio auio; 395 struct uio auio;
395 struct iovec *iov, *needfree = NULL, aiov[UIO_SMALLIOV]; 396 struct iovec *iov, *needfree = NULL, aiov[UIO_SMALLIOV];
396 int i, error; 397 int i, error;
397 size_t cnt; 398 size_t cnt;
398 u_int iovlen; 399 u_int iovlen;
399 struct file *fp; 400 struct file *fp;
400 struct iovec *ktriov = NULL; 401 struct iovec *ktriov = NULL;
401 402
402 if (iovcnt == 0) 403 if (iovcnt == 0)
403 return EINVAL; 404 return EINVAL;
404 405
405 if ((fp = fd_getfile(fd)) == NULL) 406 if ((fp = fd_getfile(fd)) == NULL)
406 return EBADF; 407 return EBADF;
407 408
408 if ((fp->f_flag & FWRITE) == 0) { 409 if ((fp->f_flag & FWRITE) == 0) {
409 fd_putfile(fd); 410 fd_putfile(fd);
410 return EBADF; 411 return EBADF;
411 } 412 }
412 413
413 if (offset == NULL) 414 if (offset == NULL)
414 offset = &fp->f_offset; 415 offset = &fp->f_offset;
415 else { 416 else {
416 struct vnode *vp = fp->f_data; 417 struct vnode *vp = fp->f_data;
417 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) { 418 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
418 error = ESPIPE; 419 error = ESPIPE;
419 goto out; 420 goto out;
420 } 421 }
421 /* 422 /*
422 * Test that the device is seekable ? 423 * Test that the device is seekable ?
423 * XXX This works because no file systems actually 424 * XXX This works because no file systems actually
424 * XXX take any action on the seek operation. 425 * XXX take any action on the seek operation.
425 */ 426 */
426 error = VOP_SEEK(vp, fp->f_offset, *offset, fp->f_cred); 427 error = VOP_SEEK(vp, fp->f_offset, *offset, fp->f_cred);
427 if (error != 0) 428 if (error != 0)
428 goto out; 429 goto out;
429 } 430 }
430 431
431 iovlen = iovcnt * sizeof(struct iovec); 432 iovlen = iovcnt * sizeof(struct iovec);
432 if (flags & FOF_IOV_SYSSPACE) 433 if (flags & FOF_IOV_SYSSPACE)
433 iov = __UNCONST(iovp); 434 iov = __UNCONST(iovp);
434 else { 435 else {
435 iov = aiov; 436 iov = aiov;
436 if ((u_int)iovcnt > UIO_SMALLIOV) { 437 if ((u_int)iovcnt > UIO_SMALLIOV) {
437 if ((u_int)iovcnt > IOV_MAX) { 438 if ((u_int)iovcnt > IOV_MAX) {
438 error = EINVAL; 439 error = EINVAL;
439 goto out; 440 goto out;
440 } 441 }
441 iov = kmem_alloc(iovlen, KM_SLEEP); 442 iov = kmem_alloc(iovlen, KM_SLEEP);
442 if (iov == NULL) { 443 if (iov == NULL) {
443 error = ENOMEM; 444 error = ENOMEM;
444 goto out; 445 goto out;
445 } 446 }
446 needfree = iov; 447 needfree = iov;
447 } 448 }
448 error = copyin(iovp, iov, iovlen); 449 error = copyin(iovp, iov, iovlen);
449 if (error) 450 if (error)
450 goto done; 451 goto done;
451 } 452 }
452 453
453 auio.uio_iov = iov; 454 auio.uio_iov = iov;
454 auio.uio_iovcnt = iovcnt; 455 auio.uio_iovcnt = iovcnt;
455 auio.uio_rw = UIO_WRITE; 456 auio.uio_rw = UIO_WRITE;
456 auio.uio_vmspace = curproc->p_vmspace; 457 auio.uio_vmspace = curproc->p_vmspace;
457 458
458 auio.uio_resid = 0; 459 auio.uio_resid = 0;
459 for (i = 0; i < iovcnt; i++, iov++) { 460 for (i = 0; i < iovcnt; i++, iov++) {
460 auio.uio_resid += iov->iov_len; 461 auio.uio_resid += iov->iov_len;
461 /* 462 /*
462 * Writes return ssize_t because -1 is returned on error. 463 * Writes return ssize_t because -1 is returned on error.
463 * Therefore we must restrict the length to SSIZE_MAX to 464 * Therefore we must restrict the length to SSIZE_MAX to
464 * avoid garbage return values. 465 * avoid garbage return values.
465 */ 466 */
466 if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) { 467 if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
467 error = EINVAL; 468 error = EINVAL;
468 goto done; 469 goto done;
469 } 470 }
470 } 471 }
471 472
472 /* 473 /*
473 * if tracing, save a copy of iovec 474 * if tracing, save a copy of iovec
474 */ 475 */
475 if (ktrpoint(KTR_GENIO)) { 476 if (ktrpoint(KTR_GENIO)) {
476 ktriov = kmem_alloc(iovlen, KM_SLEEP); 477 ktriov = kmem_alloc(iovlen, KM_SLEEP);
477 if (ktriov != NULL) 478 if (ktriov != NULL)
478 memcpy(ktriov, auio.uio_iov, iovlen); 479 memcpy(ktriov, auio.uio_iov, iovlen);
479 } 480 }
480 481
481 cnt = auio.uio_resid; 482 cnt = auio.uio_resid;
482 error = (*fp->f_ops->fo_write)(fp, offset, &auio, fp->f_cred, flags); 483 error = (*fp->f_ops->fo_write)(fp, offset, &auio, fp->f_cred, flags);
483 if (error) { 484 if (error) {
484 if (auio.uio_resid != cnt && (error == ERESTART || 485 if (auio.uio_resid != cnt && (error == ERESTART ||
485 error == EINTR || error == EWOULDBLOCK)) 486 error == EINTR || error == EWOULDBLOCK))
486 error = 0; 487 error = 0;
487 if (error == EPIPE) { 488 if (error == EPIPE) {
488 mutex_enter(proc_lock); 489 mutex_enter(proc_lock);
489 psignal(curproc, SIGPIPE); 490 psignal(curproc, SIGPIPE);
490 mutex_exit(proc_lock); 491 mutex_exit(proc_lock);
491 } 492 }
492 } 493 }
493 cnt -= auio.uio_resid; 494 cnt -= auio.uio_resid;
494 *retval = cnt; 495 *retval = cnt;
495 496
496 if (ktriov != NULL) { 497 if (ktriov != NULL) {
497 ktrgeniov(fd, UIO_WRITE, ktriov, cnt, error); 498 ktrgeniov(fd, UIO_WRITE, ktriov, cnt, error);
498 kmem_free(ktriov, iovlen); 499 kmem_free(ktriov, iovlen);
499 } 500 }
500 501
501 done: 502 done:
502 if (needfree) 503 if (needfree)
503 kmem_free(needfree, iovlen); 504 kmem_free(needfree, iovlen);
504 out: 505 out:
505 fd_putfile(fd); 506 fd_putfile(fd);
506 return (error); 507 return (error);
507} 508}
508 509
509/* 510/*
510 * Ioctl system call 511 * Ioctl system call
511 */ 512 */
512/* ARGSUSED */ 513/* ARGSUSED */
513int 514int
514sys_ioctl(struct lwp *l, const struct sys_ioctl_args *uap, register_t *retval) 515sys_ioctl(struct lwp *l, const struct sys_ioctl_args *uap, register_t *retval)
515{ 516{
516 /* { 517 /* {
517 syscallarg(int) fd; 518 syscallarg(int) fd;
518 syscallarg(u_long) com; 519 syscallarg(u_long) com;
519 syscallarg(void *) data; 520 syscallarg(void *) data;
520 } */ 521 } */
521 struct file *fp; 522 struct file *fp;
522 proc_t *p; 523 proc_t *p;
523 struct filedesc *fdp; 524 struct filedesc *fdp;
524 u_long com; 525 u_long com;
525 int error; 526 int error;
526 u_int size; 527 u_int size;
527 void *data, *memp; 528 void *data, *memp;
528#define STK_PARAMS 128 529#define STK_PARAMS 128
529 u_long stkbuf[STK_PARAMS/sizeof(u_long)]; 530 u_long stkbuf[STK_PARAMS/sizeof(u_long)];
530 fdfile_t *ff; 531 fdfile_t *ff;
531 532
532 error = 0; 533 error = 0;
533 p = l->l_proc; 534 p = l->l_proc;
534 fdp = p->p_fd; 535 fdp = p->p_fd;
535 536
536 if ((fp = fd_getfile(SCARG(uap, fd))) == NULL) 537 if ((fp = fd_getfile(SCARG(uap, fd))) == NULL)
537 return (EBADF); 538 return (EBADF);
538 539
539 if ((fp->f_flag & (FREAD | FWRITE)) == 0) { 540 if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
540 error = EBADF; 541 error = EBADF;
541 com = 0; 542 com = 0;
542 goto out; 543 goto out;
543 } 544 }
544 545
545 ff = fdp->fd_ofiles[SCARG(uap, fd)]; 546 ff = fdp->fd_ofiles[SCARG(uap, fd)];
546 switch (com = SCARG(uap, com)) { 547 switch (com = SCARG(uap, com)) {
547 case FIONCLEX: 548 case FIONCLEX:
548 ff->ff_exclose = false; 549 ff->ff_exclose = false;
549 goto out; 550 goto out;
550 551
551 case FIOCLEX: 552 case FIOCLEX:
552 ff->ff_exclose = true; 553 ff->ff_exclose = true;
553 fdp->fd_exclose = true; 554 fdp->fd_exclose = true;
554 goto out; 555 goto out;
555 } 556 }
556 557
557 /* 558 /*
558 * Interpret high order word to find amount of data to be 559 * Interpret high order word to find amount of data to be
559 * copied to/from the user's address space. 560 * copied to/from the user's address space.
560 */ 561 */
561 size = IOCPARM_LEN(com); 562 size = IOCPARM_LEN(com);
562 if (size > IOCPARM_MAX) { 563 if (size > IOCPARM_MAX) {
563 error = ENOTTY; 564 error = ENOTTY;
564 goto out; 565 goto out;
565 } 566 }
566 memp = NULL; 567 memp = NULL;
567 if (size > sizeof(stkbuf)) { 568 if (size > sizeof(stkbuf)) {
568 memp = kmem_alloc(size, KM_SLEEP); 569 memp = kmem_alloc(size, KM_SLEEP);
569 data = memp; 570 data = memp;
570 } else 571 } else
571 data = (void *)stkbuf; 572 data = (void *)stkbuf;
572 if (com&IOC_IN) { 573 if (com&IOC_IN) {
573 if (size) { 574 if (size) {
574 error = copyin(SCARG(uap, data), data, size); 575 error = copyin(SCARG(uap, data), data, size);
575 if (error) { 576 if (error) {
576 if (memp) 577 if (memp)
577 kmem_free(memp, size); 578 kmem_free(memp, size);
578 goto out; 579 goto out;
579 } 580 }
580 ktrgenio(SCARG(uap, fd), UIO_WRITE, SCARG(uap, data), 581 ktrgenio(SCARG(uap, fd), UIO_WRITE, SCARG(uap, data),
581 size, 0); 582 size, 0);
582 } else 583 } else
583 *(void **)data = SCARG(uap, data); 584 *(void **)data = SCARG(uap, data);
584 } else if ((com&IOC_OUT) && size) 585 } else if ((com&IOC_OUT) && size)
585 /* 586 /*
586 * Zero the buffer so the user always 587 * Zero the buffer so the user always
587 * gets back something deterministic. 588 * gets back something deterministic.
588 */ 589 */
589 memset(data, 0, size); 590 memset(data, 0, size);
590 else if (com&IOC_VOID) 591 else if (com&IOC_VOID)
591 *(void **)data = SCARG(uap, data); 592 *(void **)data = SCARG(uap, data);
592 593
593 switch (com) { 594 switch (com) {
594 595
595 case FIONBIO: 596 case FIONBIO:
596 FILE_LOCK(fp); 597 /* XXX Code block is not atomic */
597 if (*(int *)data != 0) 598 if (*(int *)data != 0)
598 fp->f_flag |= FNONBLOCK; 599 atomic_or_uint(&fp->f_flag, FNONBLOCK);
599 else 600 else
600 fp->f_flag &= ~FNONBLOCK; 601 atomic_and_uint(&fp->f_flag, ~FNONBLOCK);
601 FILE_UNLOCK(fp); 
602 error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, data); 602 error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, data);
603 break; 603 break;
604 604
605 case FIOASYNC: 605 case FIOASYNC:
606 FILE_LOCK(fp); 606 /* XXX Code block is not atomic */
607 if (*(int *)data != 0) 607 if (*(int *)data != 0)
608 fp->f_flag |= FASYNC; 608 atomic_or_uint(&fp->f_flag, FASYNC);
609 else 609 else
610 fp->f_flag &= ~FASYNC; 610 atomic_and_uint(&fp->f_flag, ~FASYNC);
611 FILE_UNLOCK(fp); 
612 error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, data); 611 error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, data);
613 break; 612 break;
614 613
615 default: 614 default:
616 error = (*fp->f_ops->fo_ioctl)(fp, com, data); 615 error = (*fp->f_ops->fo_ioctl)(fp, com, data);
617 /* 616 /*
618 * Copy any data to user, size was 617 * Copy any data to user, size was
619 * already set and checked above. 618 * already set and checked above.
620 */ 619 */
621 if (error == 0 && (com&IOC_OUT) && size) { 620 if (error == 0 && (com&IOC_OUT) && size) {
622 error = copyout(data, SCARG(uap, data), size); 621 error = copyout(data, SCARG(uap, data), size);
623 ktrgenio(SCARG(uap, fd), UIO_READ, SCARG(uap, data), 622 ktrgenio(SCARG(uap, fd), UIO_READ, SCARG(uap, data),
624 size, error); 623 size, error);
625 } 624 }
626 break; 625 break;
627 } 626 }
628 if (memp) 627 if (memp)
629 kmem_free(memp, size); 628 kmem_free(memp, size);
630 out: 629 out:
631 fd_putfile(SCARG(uap, fd)); 630 fd_putfile(SCARG(uap, fd));
632 switch (error) { 631 switch (error) {
633 case -1: 632 case -1:
634 printf("sys_ioctl: _IO%s%s('%c', %lu, %lu) returned -1: " 633 printf("sys_ioctl: _IO%s%s('%c', %lu, %lu) returned -1: "
635 "pid=%d comm=%s\n", 634 "pid=%d comm=%s\n",
636 (com & IOC_IN) ? "W" : "", (com & IOC_OUT) ? "R" : "", 635 (com & IOC_IN) ? "W" : "", (com & IOC_OUT) ? "R" : "",
637 (char)IOCGROUP(com), (com & 0xff), IOCPARM_LEN(com), 636 (char)IOCGROUP(com), (com & 0xff), IOCPARM_LEN(com),
638 p->p_pid, p->p_comm); 637 p->p_pid, p->p_comm);
639 /* FALLTHROUGH */ 638 /* FALLTHROUGH */
640 case EPASSTHROUGH: 639 case EPASSTHROUGH:
641 error = ENOTTY; 640 error = ENOTTY;
642 /* FALLTHROUGH */ 641 /* FALLTHROUGH */
643 default: 642 default:
644 return (error); 643 return (error);
645 } 644 }
646} 645}