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