From 7d5365c70ba37b88a87a6e5dc64ab5dbec5da90b Mon Sep 17 00:00:00 2001 From: Konstantin Belousov Date: Tue, 19 Mar 2013 14:27:14 +0000 Subject: [PATCH] Add a helper function vfs_bio_bzero_buf() to zero the portion of the buffer, transparently handling mapped or unmapped buffers. Its intent is to replace the use of bzero(bp->b_data) in cases where the buffer might be unmapped, to avoid unneeded upgrades. Sponsored by: The FreeBSD Foundation Tested by: pho --- sys/kern/vfs_bio.c | 26 ++++++++++++++++++++++++++ sys/sys/buf.h | 1 + 2 files changed, 27 insertions(+) diff --git a/sys/kern/vfs_bio.c b/sys/kern/vfs_bio.c index cded596bab6..6f790d230b5 100644 --- a/sys/kern/vfs_bio.c +++ b/sys/kern/vfs_bio.c @@ -4176,6 +4176,32 @@ vfs_bio_clrbuf(struct buf *bp) bp->b_resid = 0; } +void +vfs_bio_bzero_buf(struct buf *bp, int base, int size) +{ + vm_page_t m; + int i, n; + + if ((bp->b_flags & B_UNMAPPED) == 0) { + BUF_CHECK_MAPPED(bp); + bzero(bp->b_data + base, size); + } else { + BUF_CHECK_UNMAPPED(bp); + n = PAGE_SIZE - (base & PAGE_MASK); + VM_OBJECT_WLOCK(bp->b_bufobj->bo_object); + for (i = base / PAGE_SIZE; size > 0 && i < bp->b_npages; ++i) { + m = bp->b_pages[i]; + if (n > size) + n = size; + pmap_zero_page_area(m, base & PAGE_MASK, n); + base += n; + size -= n; + n = PAGE_SIZE; + } + VM_OBJECT_WUNLOCK(bp->b_bufobj->bo_object); + } +} + /* * vm_hold_load_pages and vm_hold_free_pages get pages into * a buffers address space. The pages are anonymous and are diff --git a/sys/sys/buf.h b/sys/sys/buf.h index cc8029e3791..d7025b0a47d 100644 --- a/sys/sys/buf.h +++ b/sys/sys/buf.h @@ -519,6 +519,7 @@ int cluster_read(struct vnode *, u_quad_t, daddr_t, long, struct ucred *, long, int, int, struct buf **); int cluster_wbuild(struct vnode *, long, daddr_t, int, int); void cluster_write(struct vnode *, struct buf *, u_quad_t, int, int); +void vfs_bio_bzero_buf(struct buf *bp, int base, int size); void vfs_bio_set_valid(struct buf *, int base, int size); void vfs_bio_clrbuf(struct buf *); void vfs_busy_pages(struct buf *, int clear_modify); -- 2.45.2