kernel - TMPFS - Features, don't sync on umount, enforce snocache on root
[dragonfly.git] / sys / kern / kern_physio.c
blobf930f4e6ff5b4076d30108539a591064dd4fe8a7
1 /*
2 * Copyright (c) 1994 John S. Dyson
3 * All rights reserved.
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 immediately at the beginning of the file, without modification,
10 * this list of conditions, and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Absolutely no warranty of function or purpose is made by the author
15 * John S. Dyson.
16 * 4. Modifications may be freely made to this file if the above conditions
17 * are met.
19 * $FreeBSD: src/sys/kern/kern_physio.c,v 1.46.2.4 2003/11/14 09:51:47 simokawa Exp $
20 * $DragonFly: src/sys/kern/kern_physio.c,v 1.27 2008/08/22 08:47:56 swildner Exp $
23 #include <sys/param.h>
24 #include <sys/systm.h>
25 #include <sys/buf.h>
26 #include <sys/conf.h>
27 #include <sys/proc.h>
28 #include <sys/uio.h>
29 #include <sys/device.h>
30 #include <sys/thread2.h>
32 #include <vm/vm.h>
33 #include <vm/vm_extern.h>
35 static int
36 physio(cdev_t dev, struct uio *uio, int ioflag)
38 int i;
39 int error;
40 int saflags;
41 int iolen;
42 int bcount;
43 int bounceit;
44 caddr_t ubase;
45 struct buf *bp;
47 bp = getpbuf(NULL);
48 saflags = bp->b_flags;
49 error = 0;
51 /* XXX: sanity check */
52 if (dev->si_iosize_max < PAGE_SIZE) {
53 kprintf("WARNING: %s si_iosize_max=%d, using DFLTPHYS.\n",
54 devtoname(dev), dev->si_iosize_max);
55 dev->si_iosize_max = DFLTPHYS;
58 /* Must be a real uio */
59 KKASSERT(uio->uio_segflg != UIO_NOCOPY);
61 for (i = 0; i < uio->uio_iovcnt; i++) {
62 while (uio->uio_iov[i].iov_len) {
63 if (uio->uio_rw == UIO_READ)
64 bp->b_cmd = BUF_CMD_READ;
65 else
66 bp->b_cmd = BUF_CMD_WRITE;
67 bp->b_flags = saflags;
68 bcount = uio->uio_iov[i].iov_len;
70 reinitbufbio(bp); /* clear translation cache */
71 bp->b_bio1.bio_offset = uio->uio_offset;
72 bp->b_bio1.bio_done = biodone_sync;
73 bp->b_bio1.bio_flags |= BIO_SYNC;
75 /*
76 * Setup for mapping the request into kernel memory.
78 * We can only write as much as fits in a pbuf,
79 * which is MAXPHYS, and no larger then the device's
80 * ability.
82 * If not using bounce pages the base address of the
83 * user mapping into the pbuf may be offset, further
84 * reducing how much will actually fit in the pbuf.
86 if (bcount > dev->si_iosize_max)
87 bcount = dev->si_iosize_max;
89 ubase = uio->uio_iov[i].iov_base;
90 bounceit = (int)(((vm_offset_t)ubase) & 15);
91 iolen = ((vm_offset_t)ubase) & PAGE_MASK;
92 if (bounceit) {
93 if (bcount > bp->b_kvasize)
94 bcount = bp->b_kvasize;
95 } else {
96 if ((bcount + iolen) > bp->b_kvasize) {
97 bcount = bp->b_kvasize;
98 if (iolen != 0)
99 bcount -= PAGE_SIZE;
104 * If we have to use a bounce buffer allocate kernel
105 * memory and copyin/copyout. Otherwise map the
106 * user buffer directly into kernel memory without
107 * copying.
109 if (uio->uio_segflg == UIO_USERSPACE) {
110 if (bounceit) {
111 bp->b_data = bp->b_kvabase;
112 bp->b_bcount = bcount;
113 vm_hold_load_pages(bp, (vm_offset_t)bp->b_data, (vm_offset_t)bp->b_data + bcount);
114 if (uio->uio_rw == UIO_WRITE) {
115 error = copyin(ubase, bp->b_data, bcount);
116 if (error) {
117 vm_hold_free_pages(bp, (vm_offset_t)bp->b_data, (vm_offset_t)bp->b_data + bcount);
118 goto doerror;
121 } else if (vmapbuf(bp, ubase, bcount) < 0) {
122 error = EFAULT;
123 goto doerror;
125 } else {
126 bp->b_data = uio->uio_iov[i].iov_base;
127 bp->b_bcount = bcount;
129 dev_dstrategy(dev, &bp->b_bio1);
130 biowait(&bp->b_bio1, "physstr");
132 iolen = bp->b_bcount - bp->b_resid;
133 if (uio->uio_segflg == UIO_USERSPACE) {
134 if (bounceit) {
135 if (uio->uio_rw == UIO_READ && iolen) {
136 error = copyout(bp->b_data, ubase, iolen);
137 if (error) {
138 bp->b_flags |= B_ERROR;
139 bp->b_error = error;
142 vm_hold_free_pages(bp, (vm_offset_t)bp->b_data, (vm_offset_t)bp->b_data + bcount);
143 } else {
144 vunmapbuf(bp);
147 if (iolen == 0 && !(bp->b_flags & B_ERROR))
148 goto doerror; /* EOF */
149 uio->uio_iov[i].iov_len -= iolen;
150 uio->uio_iov[i].iov_base = (char *)uio->uio_iov[i].iov_base + iolen;
151 uio->uio_resid -= iolen;
152 uio->uio_offset += iolen;
153 if (bp->b_flags & B_ERROR) {
154 error = bp->b_error;
155 goto doerror;
159 doerror:
160 relpbuf(bp, NULL);
161 return (error);
165 physread(struct dev_read_args *ap)
167 return(physio(ap->a_head.a_dev, ap->a_uio, ap->a_ioflag));
171 physwrite(struct dev_write_args *ap)
173 return(physio(ap->a_head.a_dev, ap->a_uio, ap->a_ioflag));