summaryrefslogtreecommitdiffstats
path: root/sys/kern
diff options
context:
space:
mode:
authormckusick <mckusick@FreeBSD.org>2016-01-27 21:23:01 +0000
committermckusick <mckusick@FreeBSD.org>2016-01-27 21:23:01 +0000
commit0b10a802f877a41beb1b823c21a6a4b241ed8e28 (patch)
treef2447333b6b707e0c4a1f5b56e89972d313c010e /sys/kern
parent36b5b4fdf85dc4c5f943d37989db0d537856cdd7 (diff)
downloadFreeBSD-src-0b10a802f877a41beb1b823c21a6a4b241ed8e28.zip
FreeBSD-src-0b10a802f877a41beb1b823c21a6a4b241ed8e28.tar.gz
The bread() function was inconsistent about whether it would return
a buffer pointer in the event of an error (for some errors it would return a buffer pointer and for other errors it would not return a buffer pointer). The cluster_read() function was similarly inconsistent. Clients of these functions were inconsistent in handling errors. Some would assume that no buffer was returned after an error and would thus lose buffers under certain error conditions. Others would assume that brelse() should always be called after an error and would thus panic the system under certain error conditions. To correct both of these problems with minimal code churn, bread() and cluster_write() now always free the buffer when returning an error thus ensuring that buffers will never be lost. The brelse() routine checks for being passed a NULL buffer pointer and silently returns to avoid panics. Thus both approaches to handling error returns from bread() and cluster_read() will work correctly. Future code should be written assuming that bread() and cluster_read() will never return a buffer with an error, so should not attempt to brelse() the buffer when an error is returned. Reviewed by: kib
Diffstat (limited to 'sys/kern')
-rw-r--r--sys/kern/vfs_bio.c12
-rw-r--r--sys/kern/vfs_cluster.c18
2 files changed, 26 insertions, 4 deletions
diff --git a/sys/kern/vfs_bio.c b/sys/kern/vfs_bio.c
index 6b8f79f..eb392cb 100644
--- a/sys/kern/vfs_bio.c
+++ b/sys/kern/vfs_bio.c
@@ -1809,6 +1809,8 @@ breada(struct vnode * vp, daddr_t * rablkno, int * rabsize,
* must clear BIO_ERROR and B_INVAL prior to initiating I/O. If B_CACHE
* is set, the buffer is valid and we do not have to do anything, see
* getblk(). Also starts asynchronous I/O on read-ahead blocks.
+ *
+ * Always return a NULL buffer pointer (in bpp) when returning an error.
*/
int
breadn_flags(struct vnode *vp, daddr_t blkno, int size, daddr_t *rablkno,
@@ -1844,6 +1846,10 @@ breadn_flags(struct vnode *vp, daddr_t blkno, int size, daddr_t *rablkno,
if (readwait) {
rv = bufwait(bp);
+ if (rv != 0) {
+ brelse(bp);
+ *bpp = NULL;
+ }
}
return (rv);
}
@@ -2238,6 +2244,12 @@ brelse(struct buf *bp)
{
int qindex;
+ /*
+ * Many function erroneously call brelse with a NULL bp under rare
+ * error conditions. Simply return when called with a NULL bp.
+ */
+ if (bp == NULL)
+ return;
CTR3(KTR_BUF, "brelse(%p) vp %p flags %X",
bp, bp->b_vp, bp->b_flags);
KASSERT(!(bp->b_flags & (B_CLUSTER|B_PAGING)),
diff --git a/sys/kern/vfs_cluster.c b/sys/kern/vfs_cluster.c
index 476b28a..9871a50 100644
--- a/sys/kern/vfs_cluster.c
+++ b/sys/kern/vfs_cluster.c
@@ -119,6 +119,8 @@ cluster_read(struct vnode *vp, u_quad_t filesize, daddr_t lblkno, long size,
* get the requested block
*/
*bpp = reqbp = bp = getblk(vp, lblkno, size, 0, 0, gbflags);
+ if (bp == NULL)
+ return (EBUSY);
origblkno = lblkno;
/*
@@ -295,10 +297,18 @@ cluster_read(struct vnode *vp, u_quad_t filesize, daddr_t lblkno, long size,
curthread->td_ru.ru_inblock++;
}
- if (reqbp)
- return (bufwait(reqbp));
- else
- return (error);
+ if (reqbp) {
+ /*
+ * Like bread, always brelse() the buffer when
+ * returning an error.
+ */
+ error = bufwait(reqbp);
+ if (error != 0) {
+ brelse(reqbp);
+ *bpp = NULL;
+ }
+ }
+ return (error);
}
/*
OpenPOWER on IntegriCloud