uio: Use switch statements when handling UIO_READ vs UIO_WRITE

This is mostly to reduce the diff with CheriBSD which adds additional
constants to enum uio_rw, but also matches the normal style used for
uio_segflg.

Reviewed by:	kib, emaste
Obtained from:	CheriBSD
Differential Revision:	https://reviews.freebsd.org/D45142
This commit is contained in:
John Baldwin 2024-05-10 13:43:36 -07:00
parent f75764fea3
commit 473c90ac04
16 changed files with 173 additions and 60 deletions

View File

@ -79,6 +79,7 @@ memrw(struct cdev *dev, struct uio *uio, int flags)
struct iovec *iov;
void *p;
ssize_t orig_resid;
vm_prot_t prot;
u_long v, vd;
u_int c;
int error;
@ -110,8 +111,16 @@ memrw(struct cdev *dev, struct uio *uio, int flags)
break;
}
if (!kernacc((void *)v, c, uio->uio_rw == UIO_READ ?
VM_PROT_READ : VM_PROT_WRITE)) {
switch (uio->uio_rw) {
case UIO_READ:
prot = VM_PROT_READ;
break;
case UIO_WRITE:
prot = VM_PROT_WRITE;
break;
}
if (!kernacc((void *)v, c, prot)) {
error = EFAULT;
break;
}

View File

@ -97,18 +97,26 @@ uiomove_fromphys(vm_page_t ma[], vm_offset_t offset, int n, struct uio *uio)
switch (uio->uio_segflg) {
case UIO_USERSPACE:
maybe_yield();
if (uio->uio_rw == UIO_READ)
switch (uio->uio_rw) {
case UIO_READ:
error = copyout(cp, iov->iov_base, cnt);
else
break;
case UIO_WRITE:
error = copyin(iov->iov_base, cp, cnt);
break;
}
if (error)
goto out;
break;
case UIO_SYSSPACE:
if (uio->uio_rw == UIO_READ)
switch (uio->uio_rw) {
case UIO_READ:
bcopy(cp, iov->iov_base, cnt);
else
break;
case UIO_WRITE:
bcopy(iov->iov_base, cp, cnt);
break;
}
break;
case UIO_NOCOPY:
break;

View File

@ -94,20 +94,28 @@ uiomove_fromphys(vm_page_t ma[], vm_offset_t offset, int n, struct uio *uio)
switch (uio->uio_segflg) {
case UIO_USERSPACE:
maybe_yield();
if (uio->uio_rw == UIO_READ)
switch (uio->uio_rw) {
case UIO_READ:
error = copyout(cp, iov->iov_base, cnt);
else
break;
case UIO_WRITE:
error = copyin(iov->iov_base, cp, cnt);
break;
}
if (error) {
sf_buf_free(sf);
goto out;
}
break;
case UIO_SYSSPACE:
if (uio->uio_rw == UIO_READ)
switch (uio->uio_rw) {
case UIO_READ:
bcopy(cp, iov->iov_base, cnt);
else
break;
case UIO_WRITE:
bcopy(iov->iov_base, cp, cnt);
break;
}
break;
case UIO_NOCOPY:
break;

View File

@ -49,6 +49,7 @@ memrw(struct cdev *dev, struct uio *uio, int flags)
struct vm_page m;
vm_page_t marr;
vm_offset_t off, v;
vm_prot_t prot;
u_int cnt;
int error;
@ -78,8 +79,16 @@ memrw(struct cdev *dev, struct uio *uio, int flags)
break;
}
if (!kernacc((void *)v, cnt, uio->uio_rw == UIO_READ ?
VM_PROT_READ : VM_PROT_WRITE)) {
switch (uio->uio_rw) {
case UIO_READ:
prot = VM_PROT_READ;
break;
case UIO_WRITE:
prot = VM_PROT_WRITE;
break;
}
if (!kernacc((void *)v, cnt, prot)) {
error = EFAULT;
break;
}

View File

@ -95,18 +95,26 @@ uiomove_fromphys(vm_page_t ma[], vm_offset_t offset, int n, struct uio *uio)
switch (uio->uio_segflg) {
case UIO_USERSPACE:
maybe_yield();
if (uio->uio_rw == UIO_READ)
switch (uio->uio_rw) {
case UIO_READ:
error = copyout(cp, iov->iov_base, cnt);
else
break;
case UIO_WRITE:
error = copyin(iov->iov_base, cp, cnt);
break;
}
if (error)
goto out;
break;
case UIO_SYSSPACE:
if (uio->uio_rw == UIO_READ)
switch (uio->uio_rw) {
case UIO_READ:
bcopy(cp, iov->iov_base, cnt);
else
break;
case UIO_WRITE:
bcopy(iov->iov_base, cp, cnt);
break;
}
break;
case UIO_NOCOPY:
break;

View File

@ -136,19 +136,28 @@ debugfs_fill(PFS_FILL_ARGS)
}
rc = -ENODEV;
if (uio->uio_rw == UIO_READ && d->dm_fops->read) {
rc = -ENOMEM;
buf = (char *) malloc(sb->s_size, M_DFSINT, M_ZERO | M_NOWAIT);
if (buf != NULL) {
rc = d->dm_fops->read(&lf, buf, sb->s_size, &off);
if (rc > 0)
sbuf_bcpy(sb, buf, strlen(buf));
switch (uio->uio_rw) {
case UIO_READ:
if (d->dm_fops->read != NULL) {
rc = -ENOMEM;
buf = malloc(sb->s_size, M_DFSINT, M_ZERO | M_NOWAIT);
if (buf != NULL) {
rc = d->dm_fops->read(&lf, buf, sb->s_size,
&off);
if (rc > 0)
sbuf_bcpy(sb, buf, strlen(buf));
free(buf, M_DFSINT);
free(buf, M_DFSINT);
}
}
} else if (uio->uio_rw == UIO_WRITE && d->dm_fops->write) {
sbuf_finish(sb);
rc = d->dm_fops->write(&lf, sbuf_data(sb), sbuf_len(sb), &off);
break;
case UIO_WRITE:
if (d->dm_fops->write != NULL) {
sbuf_finish(sb);
rc = d->dm_fops->write(&lf, sbuf_data(sb), sbuf_len(sb),
&off);
}
break;
}
if (d->dm_fops->release)

View File

@ -239,7 +239,8 @@ iicuio_move(struct iic_cdevpriv *priv, struct uio *uio, int last)
num_bytes = MIN(uio->uio_resid, sizeof(buffer));
transferred_bytes = 0;
if (uio->uio_rw == UIO_WRITE) {
switch (uio->uio_rw) {
case UIO_WRITE:
error = uiomove(buffer, num_bytes, uio);
while ((error == 0) && (transferred_bytes < num_bytes)) {
@ -248,13 +249,14 @@ iicuio_move(struct iic_cdevpriv *priv, struct uio *uio, int last)
num_bytes - transferred_bytes, &written_bytes, 0);
transferred_bytes += written_bytes;
}
} else if (uio->uio_rw == UIO_READ) {
break;
case UIO_READ:
error = iicbus_read(parent, buffer,
num_bytes, &transferred_bytes,
((uio->uio_resid <= sizeof(buffer)) ? last : 0), 0);
if (error == 0)
error = uiomove(buffer, transferred_bytes, uio);
break;
}
}
@ -290,10 +292,14 @@ iicuio(struct cdev *dev, struct uio *uio, int ioflag)
return (error);
}
if (uio->uio_rw == UIO_READ)
switch (uio->uio_rw) {
case UIO_READ:
addr = priv->addr | LSB;
else
break;
case UIO_WRITE:
addr = priv->addr & ~LSB;
break;
}
error = iicbus_start(parent, addr, 0);
if (error != 0)

View File

@ -45,9 +45,11 @@ procfs_doosrel(PFS_FILL_ARGS)
if (uio == NULL)
return (EOPNOTSUPP);
if (uio->uio_rw == UIO_READ) {
switch (uio->uio_rw) {
case UIO_READ:
sbuf_printf(sb, "%d\n", p->p_osrel);
} else {
break;
case UIO_WRITE:
sbuf_trim(sb);
sbuf_finish(sb);
pp = sbuf_data(sb);
@ -62,6 +64,7 @@ procfs_doosrel(PFS_FILL_ARGS)
osrel = ov;
}
p->p_osrel = osrel;
break;
}
return (0);
}

View File

@ -94,10 +94,14 @@ uiomove_fromphys(vm_page_t ma[], vm_offset_t offset, int n, struct uio *uio)
switch (uio->uio_segflg) {
case UIO_USERSPACE:
maybe_yield();
if (uio->uio_rw == UIO_READ)
switch (uio->uio_rw) {
case UIO_READ:
error = copyout(cp, iov->iov_base, cnt);
else
break;
case UIO_WRITE:
error = copyin(iov->iov_base, cp, cnt);
break;
}
if (error) {
sf_buf_free(sf);
sched_unpin();
@ -105,10 +109,14 @@ uiomove_fromphys(vm_page_t ma[], vm_offset_t offset, int n, struct uio *uio)
}
break;
case UIO_SYSSPACE:
if (uio->uio_rw == UIO_READ)
switch (uio->uio_rw) {
case UIO_READ:
bcopy(cp, iov->iov_base, cnt);
else
break;
case UIO_WRITE:
bcopy(iov->iov_base, cp, cnt);
break;
}
break;
case UIO_NOCOPY:
break;

View File

@ -116,14 +116,17 @@ physio(struct cdev *dev, struct uio *uio, int ioflag)
#ifdef RACCT
if (racct_enable) {
PROC_LOCK(curproc);
if (uio->uio_rw == UIO_READ) {
switch (uio->uio_rw) {
case UIO_READ:
racct_add_force(curproc, RACCT_READBPS,
uio->uio_iov[i].iov_len);
racct_add_force(curproc, RACCT_READIOPS, 1);
} else {
break;
case UIO_WRITE:
racct_add_force(curproc, RACCT_WRITEBPS,
uio->uio_iov[i].iov_len);
racct_add_force(curproc, RACCT_WRITEIOPS, 1);
break;
}
PROC_UNLOCK(curproc);
}
@ -131,12 +134,15 @@ physio(struct cdev *dev, struct uio *uio, int ioflag)
while (uio->uio_iov[i].iov_len) {
g_reset_bio(bp);
if (uio->uio_rw == UIO_READ) {
switch (uio->uio_rw) {
case UIO_READ:
bp->bio_cmd = BIO_READ;
curthread->td_ru.ru_inblock++;
} else {
break;
case UIO_WRITE:
bp->bio_cmd = BIO_WRITE;
curthread->td_ru.ru_oublock++;
break;
}
bp->bio_offset = uio->uio_offset;
base = uio->uio_iov[i].iov_base;

View File

@ -249,19 +249,27 @@ uiomove_faultflag(void *cp, int n, struct uio *uio, int nofault)
switch (uio->uio_segflg) {
case UIO_USERSPACE:
maybe_yield();
if (uio->uio_rw == UIO_READ)
switch (uio->uio_rw) {
case UIO_READ:
error = copyout(cp, iov->iov_base, cnt);
else
break;
case UIO_WRITE:
error = copyin(iov->iov_base, cp, cnt);
break;
}
if (error)
goto out;
break;
case UIO_SYSSPACE:
if (uio->uio_rw == UIO_READ)
switch (uio->uio_rw) {
case UIO_READ:
bcopy(cp, iov->iov_base, cnt);
else
break;
case UIO_WRITE:
bcopy(iov->iov_base, cp, cnt);
break;
}
break;
case UIO_NOCOPY:
break;

View File

@ -1259,12 +1259,15 @@ vn_io_fault_doio(struct vn_io_fault_args *args, struct uio *uio,
uio, args->cred, args->flags, td);
break;
case VN_IO_FAULT_VOP:
if (uio->uio_rw == UIO_READ) {
switch (uio->uio_rw) {
case UIO_READ:
error = VOP_READ(args->args.vop_args.vp, uio,
args->flags, args->cred);
} else if (uio->uio_rw == UIO_WRITE) {
break;
case UIO_WRITE:
error = VOP_WRITE(args->args.vop_args.vp, uio,
args->flags, args->cred);
break;
}
break;
default:

View File

@ -100,20 +100,28 @@ uiomove_fromphys(vm_page_t ma[], vm_offset_t offset, int n, struct uio *uio)
switch (uio->uio_segflg) {
case UIO_USERSPACE:
maybe_yield();
if (uio->uio_rw == UIO_READ)
switch (uio->uio_rw) {
case UIO_READ:
error = copyout(cp, iov->iov_base, cnt);
else
break;
case UIO_WRITE:
error = copyin(iov->iov_base, cp, cnt);
break;
}
if (error) {
sf_buf_free(sf);
goto out;
}
break;
case UIO_SYSSPACE:
if (uio->uio_rw == UIO_READ)
switch (uio->uio_rw) {
case UIO_READ:
bcopy(cp, iov->iov_base, cnt);
else
break;
case UIO_WRITE:
bcopy(iov->iov_base, cp, cnt);
break;
}
break;
case UIO_NOCOPY:
break;

View File

@ -50,6 +50,7 @@ memrw(struct cdev *dev, struct uio *uio, int flags)
struct iovec *iov;
struct vm_page m;
vm_page_t marr;
vm_prot_t prot;
u_int cnt;
int error;
@ -79,8 +80,16 @@ memrw(struct cdev *dev, struct uio *uio, int flags)
break;
}
if (!kernacc((void *)v, cnt, uio->uio_rw == UIO_READ ?
VM_PROT_READ : VM_PROT_WRITE)) {
switch (uio->uio_rw) {
case UIO_READ:
prot = VM_PROT_READ;
break;
case UIO_WRITE:
prot = VM_PROT_WRITE;
break;
}
if (!kernacc((void *)v, cnt, prot)) {
error = EFAULT;
break;
}

View File

@ -95,18 +95,26 @@ uiomove_fromphys(vm_page_t ma[], vm_offset_t offset, int n, struct uio *uio)
switch (uio->uio_segflg) {
case UIO_USERSPACE:
maybe_yield();
if (uio->uio_rw == UIO_READ)
switch (uio->uio_rw) {
case UIO_READ:
error = copyout(cp, iov->iov_base, cnt);
else
break;
case UIO_WRITE:
error = copyin(iov->iov_base, cp, cnt);
break;
}
if (error)
goto out;
break;
case UIO_SYSSPACE:
if (uio->uio_rw == UIO_READ)
switch (uio->uio_rw) {
case UIO_READ:
bcopy(cp, iov->iov_base, cnt);
else
break;
case UIO_WRITE:
bcopy(iov->iov_base, cp, cnt);
break;
}
break;
case UIO_NOCOPY:
break;

View File

@ -138,7 +138,8 @@ ffs_susp_rdwr(struct cdev *dev, struct uio *uio, int ioflag)
NOCRED, &bp);
if (error != 0)
goto out;
if (uio->uio_rw == UIO_WRITE) {
switch (uio->uio_rw) {
case UIO_WRITE:
error = copyin(base, bp->b_data, len);
if (error != 0) {
bp->b_flags |= B_INVAL | B_NOCACHE;
@ -148,11 +149,13 @@ ffs_susp_rdwr(struct cdev *dev, struct uio *uio, int ioflag)
error = bwrite(bp);
if (error != 0)
goto out;
} else {
break;
case UIO_READ:
error = copyout(bp->b_data, base, len);
brelse(bp);
if (error != 0)
goto out;
break;
}
uio->uio_iov[i].iov_base =
(char *)uio->uio_iov[i].iov_base + len;