kernel - Adjust UFS and HAMMER to use uiomovebp()
[dragonfly.git] / sys / kern / kern_subr.c
CommitLineData
984263bc
MD
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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)kern_subr.c 8.3 (Berkeley) 1/21/94
39 * $FreeBSD: src/sys/kern/kern_subr.c,v 1.31.2.2 2002/04/21 08:09:37 bde Exp $
40 */
41
6bd9bbba
HP
42#include "opt_ddb.h"
43
984263bc
MD
44#include <sys/param.h>
45#include <sys/systm.h>
46#include <sys/kernel.h>
47#include <sys/proc.h>
48#include <sys/malloc.h>
49#include <sys/lock.h>
50#include <sys/resourcevar.h>
cbd0496d
JS
51#include <sys/sysctl.h>
52#include <sys/uio.h>
984263bc 53#include <sys/vnode.h>
a358fd45 54#include <sys/thread2.h>
25e80b06 55#include <machine/limits.h>
984263bc 56
5c5185ae
SG
57#include <cpu/lwbuf.h>
58
984263bc
MD
59#include <vm/vm.h>
60#include <vm/vm_page.h>
61#include <vm/vm_map.h>
62
cbd0496d
JS
63SYSCTL_INT(_kern, KERN_IOV_MAX, iov_max, CTLFLAG_RD, NULL, UIO_MAXIOV,
64 "Maximum number of elements in an I/O vector; sysconf(_SC_IOV_MAX)");
65
1301b4d8
MD
66/*
67 * UIO_READ: copy the kernelspace cp to the user or kernelspace UIO
8ddc6004 68 * UIO_WRITE: copy the user or kernelspace UIO to the kernelspace cp
1301b4d8
MD
69 *
70 * For userspace UIO's, uio_td must be the current thread.
e54488bb
MD
71 *
72 * The syscall interface is responsible for limiting the length to
73 * ssize_t for things like read() or write() which return the bytes
74 * read or written as ssize_t. These functions work with unsigned
75 * lengths.
1301b4d8 76 */
984263bc 77int
e54488bb 78uiomove(caddr_t cp, size_t n, struct uio *uio)
984263bc 79{
616516c8 80 thread_t td = curthread;
1fd87d54 81 struct iovec *iov;
e54488bb 82 size_t cnt;
984263bc
MD
83 int error = 0;
84 int save = 0;
85
86 KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
87 ("uiomove: mode"));
616516c8 88 KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == td,
984263bc
MD
89 ("uiomove proc"));
90
616516c8
MD
91 crit_enter();
92 save = td->td_flags & TDF_DEADLKTREAT;
93 td->td_flags |= TDF_DEADLKTREAT;
94 crit_exit();
984263bc
MD
95
96 while (n > 0 && uio->uio_resid) {
97 iov = uio->uio_iov;
98 cnt = iov->iov_len;
99 if (cnt == 0) {
100 uio->uio_iov++;
101 uio->uio_iovcnt--;
102 continue;
103 }
104 if (cnt > n)
105 cnt = n;
106
107 switch (uio->uio_segflg) {
108
109 case UIO_USERSPACE:
f9235b6d 110 lwkt_user_yield();
984263bc
MD
111 if (uio->uio_rw == UIO_READ)
112 error = copyout(cp, iov->iov_base, cnt);
113 else
114 error = copyin(iov->iov_base, cp, cnt);
115 if (error)
116 break;
117 break;
118
119 case UIO_SYSSPACE:
120 if (uio->uio_rw == UIO_READ)
121 bcopy((caddr_t)cp, iov->iov_base, cnt);
122 else
123 bcopy(iov->iov_base, (caddr_t)cp, cnt);
124 break;
125 case UIO_NOCOPY:
126 break;
127 }
656849c6 128 iov->iov_base = (char *)iov->iov_base + cnt;
984263bc
MD
129 iov->iov_len -= cnt;
130 uio->uio_resid -= cnt;
131 uio->uio_offset += cnt;
132 cp += cnt;
133 n -= cnt;
134 }
616516c8
MD
135 crit_enter();
136 td->td_flags = (td->td_flags & ~TDF_DEADLKTREAT) | save;
137 crit_exit();
984263bc
MD
138 return (error);
139}
c7841cbe
MD
140
141/*
68cdd773
MD
142 * This is the same as uiomove() except (cp, n) is within the bounds of
143 * the passed, locked buffer. Under certain circumstances a VM fault
144 * occuring with a locked buffer held can result in a deadlock or an
145 * attempt to recursively lock the buffer.
146 *
147 * This procedure deals with these cases.
148 *
149 * If the buffer represents a regular file, is B_CACHE, but the last VM page
150 * is not fully valid we fix-up the last VM page. This should handle the
151 * recursive lock issue.
152 *
153 * Deadlocks are another issue. We are holding the vp and the bp locked
154 * and could deadlock against a different vp and/or bp if another thread is
155 * trying to access us while we accessing it. The only solution here is
156 * to release the bp and vnode lock and do the uio to/from a system buffer,
157 * then regain the locks and copyback (if applicable). XXX TODO.
158 */
159int
160uiomovebp(struct buf *bp, caddr_t cp, size_t n, struct uio *uio)
161{
162 int count;
163 vm_page_t m;
164
165 if (bp->b_vp && bp->b_vp->v_type == VREG &&
166 (bp->b_flags & B_CACHE) &&
167 (count = bp->b_xio.xio_npages) != 0 &&
168 (m = bp->b_xio.xio_pages[count-1])->valid != VM_PAGE_BITS_ALL) {
169 vm_page_zero_invalid(m, TRUE);
170 }
171 return (uiomove(cp, n, uio));
172}
173
174/*
c7841cbe
MD
175 * Like uiomove() but copies zero-fill. Only allowed for UIO_READ,
176 * for obvious reasons.
177 */
178int
179uiomovez(size_t n, struct uio *uio)
180{
181 struct iovec *iov;
182 size_t cnt;
183 int error = 0;
184
185 KASSERT(uio->uio_rw == UIO_READ, ("uiomovez: mode"));
186 KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread,
187 ("uiomove proc"));
188
189 while (n > 0 && uio->uio_resid) {
190 iov = uio->uio_iov;
191 cnt = iov->iov_len;
192 if (cnt == 0) {
193 uio->uio_iov++;
194 uio->uio_iovcnt--;
195 continue;
196 }
197 if (cnt > n)
198 cnt = n;
199
200 switch (uio->uio_segflg) {
201 case UIO_USERSPACE:
202 error = copyout(ZeroPage, iov->iov_base, cnt);
203 if (error)
204 break;
205 break;
206 case UIO_SYSSPACE:
207 bzero(iov->iov_base, cnt);
208 break;
209 case UIO_NOCOPY:
210 break;
211 }
212 iov->iov_base = (char *)iov->iov_base + cnt;
213 iov->iov_len -= cnt;
214 uio->uio_resid -= cnt;
215 uio->uio_offset += cnt;
216 n -= cnt;
217 }
218 return (error);
219}
220
25e80b06
DR
221/*
222 * Wrapper for uiomove() that validates the arguments against a known-good
47538602
MD
223 * kernel buffer. This function automatically indexes the buffer by
224 * uio_offset and handles all range checking.
25e80b06
DR
225 */
226int
e54488bb 227uiomove_frombuf(void *buf, size_t buflen, struct uio *uio)
25e80b06 228{
e54488bb 229 size_t offset;
25e80b06 230
e54488bb
MD
231 offset = (size_t)uio->uio_offset;
232 if ((off_t)offset != uio->uio_offset)
25e80b06 233 return (EINVAL);
e54488bb 234 if (buflen == 0 || offset >= buflen)
25e80b06 235 return (0);
e54488bb 236 return (uiomove((char *)buf + offset, buflen - offset, uio));
984263bc
MD
237}
238
984263bc
MD
239/*
240 * Give next character to user as result of read.
241 */
242int
c972a82f 243ureadc(int c, struct uio *uio)
984263bc 244{
1fd87d54 245 struct iovec *iov;
656849c6 246 char *iov_base;
984263bc
MD
247
248again:
249 if (uio->uio_iovcnt == 0 || uio->uio_resid == 0)
250 panic("ureadc");
251 iov = uio->uio_iov;
252 if (iov->iov_len == 0) {
253 uio->uio_iovcnt--;
254 uio->uio_iov++;
255 goto again;
256 }
257 switch (uio->uio_segflg) {
258
259 case UIO_USERSPACE:
260 if (subyte(iov->iov_base, c) < 0)
261 return (EFAULT);
262 break;
263
264 case UIO_SYSSPACE:
656849c6
SW
265 iov_base = iov->iov_base;
266 *iov_base = c;
267 iov->iov_base = iov_base;
984263bc
MD
268 break;
269
984263bc
MD
270 case UIO_NOCOPY:
271 break;
272 }
656849c6 273 iov->iov_base = (char *)iov->iov_base + 1;
984263bc
MD
274 iov->iov_len--;
275 uio->uio_resid--;
276 uio->uio_offset++;
277 return (0);
278}
279
984263bc 280/*
1c86b25b
MD
281 * General routine to allocate a hash table. Make the hash table size a
282 * power of 2 greater or equal to the number of elements requested, and
283 * store the masking value in *hashmask.
984263bc
MD
284 */
285void *
c972a82f 286hashinit(int elements, struct malloc_type *type, u_long *hashmask)
984263bc
MD
287{
288 long hashsize;
289 LIST_HEAD(generic, generic) *hashtbl;
290 int i;
291
292 if (elements <= 0)
293 panic("hashinit: bad elements");
8f17cbc6 294 for (hashsize = 2; hashsize < elements; hashsize <<= 1)
984263bc 295 continue;
efda3bd0 296 hashtbl = kmalloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK);
984263bc
MD
297 for (i = 0; i < hashsize; i++)
298 LIST_INIT(&hashtbl[i]);
299 *hashmask = hashsize - 1;
300 return (hashtbl);
301}
302
c02197ac
MD
303/*
304 * This is a newer version which allocates a hash table of structures.
305 *
306 * The returned array will be zero'd. The caller is responsible for
307 * initializing the structures.
308 */
309void *
310hashinit_ext(int elements, size_t size, struct malloc_type *type,
311 u_long *hashmask)
312{
313 long hashsize;
314 void *hashtbl;
315
316 if (elements <= 0)
317 panic("hashinit: bad elements");
318 for (hashsize = 2; hashsize < elements; hashsize <<= 1)
319 continue;
320 hashtbl = kmalloc((size_t)hashsize * size, type, M_WAITOK | M_ZERO);
321 *hashmask = hashsize - 1;
322 return (hashtbl);
323}
324
984263bc
MD
325static int primes[] = { 1, 13, 31, 61, 127, 251, 509, 761, 1021, 1531, 2039,
326 2557, 3067, 3583, 4093, 4603, 5119, 5623, 6143, 6653,
327 7159, 7673, 8191, 12281, 16381, 24571, 32749 };
c157ff7a 328#define NPRIMES NELEM(primes)
984263bc
MD
329
330/*
331 * General routine to allocate a prime number sized hash table.
332 */
333void *
c972a82f 334phashinit(int elements, struct malloc_type *type, u_long *nentries)
984263bc
MD
335{
336 long hashsize;
337 LIST_HEAD(generic, generic) *hashtbl;
338 int i;
339
340 if (elements <= 0)
341 panic("phashinit: bad elements");
342 for (i = 1, hashsize = primes[1]; hashsize <= elements;) {
343 i++;
344 if (i == NPRIMES)
345 break;
346 hashsize = primes[i];
347 }
348 hashsize = primes[i - 1];
efda3bd0 349 hashtbl = kmalloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK);
984263bc
MD
350 for (i = 0; i < hashsize; i++)
351 LIST_INIT(&hashtbl[i]);
352 *nentries = hashsize;
353 return (hashtbl);
354}
75a872f8
DRJ
355
356/*
c02197ac
MD
357 * This is a newer version which allocates a hash table of structures
358 * in a prime-number size.
359 *
360 * The returned array will be zero'd. The caller is responsible for
361 * initializing the structures.
362 */
363void *
364phashinit_ext(int elements, size_t size, struct malloc_type *type,
365 u_long *nentries)
366{
367 long hashsize;
368 void *hashtbl;
369 int i;
370
371 if (elements <= 0)
372 panic("phashinit: bad elements");
373 for (i = 1, hashsize = primes[1]; hashsize <= elements;) {
374 i++;
375 if (i == NPRIMES)
376 break;
377 hashsize = primes[i];
378 }
379 hashsize = primes[i - 1];
380 hashtbl = kmalloc((size_t)hashsize * size, type, M_WAITOK | M_ZERO);
381 *nentries = hashsize;
382 return (hashtbl);
383}
384
385/*
75a872f8
DRJ
386 * Copyin an iovec. If the iovec array fits, use the preallocated small
387 * iovec structure. If it is too big, dynamically allocate an iovec array
388 * of sufficient size.
f832287e
MD
389 *
390 * MPSAFE
75a872f8
DRJ
391 */
392int
393iovec_copyin(struct iovec *uiov, struct iovec **kiov, struct iovec *siov,
e54488bb 394 size_t iov_cnt, size_t *iov_len)
75a872f8
DRJ
395{
396 struct iovec *iovp;
397 int error, i;
e54488bb 398 size_t len;
75a872f8 399
fe57a30b 400 if (iov_cnt > UIO_MAXIOV)
75a872f8 401 return EMSGSIZE;
fe57a30b 402 if (iov_cnt > UIO_SMALLIOV) {
884717e1
SW
403 *kiov = kmalloc(sizeof(struct iovec) * iov_cnt, M_IOV,
404 M_WAITOK);
75a872f8
DRJ
405 } else {
406 *kiov = siov;
407 }
408 error = copyin(uiov, *kiov, iov_cnt * sizeof(struct iovec));
ef5c76d7
MD
409 if (error == 0) {
410 *iov_len = 0;
411 for (i = 0, iovp = *kiov; i < iov_cnt; i++, iovp++) {
412 /*
413 * Check for both *iov_len overflows and out of
414 * range iovp->iov_len's. We limit to the
415 * capabilities of signed integers.
e54488bb
MD
416 *
417 * GCC4 - overflow check opt requires assign/test.
ef5c76d7 418 */
e54488bb
MD
419 len = *iov_len + iovp->iov_len;
420 if (len < *iov_len)
ef5c76d7 421 error = EINVAL;
e54488bb 422 *iov_len = len;
ef5c76d7
MD
423 }
424 }
e54488bb
MD
425
426 /*
427 * From userland disallow iovec's which exceed the sized size
428 * limit as the system calls return ssize_t.
429 *
430 * NOTE: Internal kernel interfaces can handle the unsigned
431 * limit.
432 */
433 if (error == 0 && (ssize_t)*iov_len < 0)
434 error = EINVAL;
435
75a872f8
DRJ
436 if (error)
437 iovec_free(kiov, siov);
438 return (error);
439}
a358fd45
MD
440
441
442/*
443 * Copyright (c) 2004 Alan L. Cox <alc@cs.rice.edu>
444 * Copyright (c) 1982, 1986, 1991, 1993
445 * The Regents of the University of California. All rights reserved.
446 * (c) UNIX System Laboratories, Inc.
447 * All or some portions of this file are derived from material licensed
448 * to the University of California by American Telephone and Telegraph
449 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
450 * the permission of UNIX System Laboratories, Inc.
451 *
452 * Redistribution and use in source and binary forms, with or without
453 * modification, are permitted provided that the following conditions
454 * are met:
455 * 1. Redistributions of source code must retain the above copyright
456 * notice, this list of conditions and the following disclaimer.
457 * 2. Redistributions in binary form must reproduce the above copyright
458 * notice, this list of conditions and the following disclaimer in the
459 * documentation and/or other materials provided with the distribution.
460 * 4. Neither the name of the University nor the names of its contributors
461 * may be used to endorse or promote products derived from this software
462 * without specific prior written permission.
463 *
464 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
465 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
466 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
467 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
468 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
469 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
471 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
472 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
473 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
474 * SUCH DAMAGE.
475 *
476 * @(#)kern_subr.c 8.3 (Berkeley) 1/21/94
477 * $FreeBSD: src/sys/i386/i386/uio_machdep.c,v 1.1 2004/03/21 20:28:36 alc Exp $
478 */
479
480/*
5c5185ae 481 * Implement uiomove(9) from physical memory using lwbuf's to reduce
a358fd45
MD
482 * the creation and destruction of ephemeral mappings.
483 */
484int
e54488bb 485uiomove_fromphys(vm_page_t *ma, vm_offset_t offset, size_t n, struct uio *uio)
a358fd45 486{
7a683a24 487 struct lwbuf lwb_cache;
5c5185ae 488 struct lwbuf *lwb;
a358fd45
MD
489 struct thread *td = curthread;
490 struct iovec *iov;
491 void *cp;
492 vm_offset_t page_offset;
493 vm_page_t m;
494 size_t cnt;
495 int error = 0;
496 int save = 0;
497
498 KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
499 ("uiomove_fromphys: mode"));
500 KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread,
501 ("uiomove_fromphys proc"));
502
503 crit_enter();
504 save = td->td_flags & TDF_DEADLKTREAT;
505 td->td_flags |= TDF_DEADLKTREAT;
506 crit_exit();
507
508 while (n > 0 && uio->uio_resid) {
509 iov = uio->uio_iov;
510 cnt = iov->iov_len;
511 if (cnt == 0) {
512 uio->uio_iov++;
513 uio->uio_iovcnt--;
514 continue;
515 }
516 if (cnt > n)
517 cnt = n;
518 page_offset = offset & PAGE_MASK;
519 cnt = min(cnt, PAGE_SIZE - page_offset);
520 m = ma[offset >> PAGE_SHIFT];
7a683a24 521 lwb = lwbuf_alloc(m, &lwb_cache);
5c5185ae 522 cp = (char *)lwbuf_kva(lwb) + page_offset;
a358fd45
MD
523 switch (uio->uio_segflg) {
524 case UIO_USERSPACE:
525 /*
526 * note: removed uioyield (it was the wrong place to
527 * put it).
528 */
529 if (uio->uio_rw == UIO_READ)
530 error = copyout(cp, iov->iov_base, cnt);
531 else
532 error = copyin(iov->iov_base, cp, cnt);
533 if (error) {
5c5185ae 534 lwbuf_free(lwb);
a358fd45
MD
535 goto out;
536 }
537 break;
538 case UIO_SYSSPACE:
539 if (uio->uio_rw == UIO_READ)
540 bcopy(cp, iov->iov_base, cnt);
541 else
542 bcopy(iov->iov_base, cp, cnt);
543 break;
544 case UIO_NOCOPY:
545 break;
546 }
5c5185ae 547 lwbuf_free(lwb);
a358fd45
MD
548 iov->iov_base = (char *)iov->iov_base + cnt;
549 iov->iov_len -= cnt;
550 uio->uio_resid -= cnt;
551 uio->uio_offset += cnt;
552 offset += cnt;
553 n -= cnt;
554 }
555out:
556 if (save == 0) {
557 crit_enter();
558 td->td_flags &= ~TDF_DEADLKTREAT;
559 crit_exit();
560 }
561 return (error);
562}
563