41#include <sys/kernel.h>
46#include <sys/malloc.h>
49#include <sys/resourcevar.h>
50#include <sys/rwlock.h>
51#include <sys/vmmeter.h>
53#include <vm/vm_object.h>
54#include <vm/vm_page.h>
55#include <sys/sysctl.h>
62 struct vn_clusterw *vnc,
struct buf *last_bp,
int gbflags);
64 daddr_t lbn, daddr_t blkno,
long size,
int run,
int gbflags,
70 "Cluster write-behind; 0: disable, 1: enable, 2: backed off");
74 "Cluster read-ahead max block count");
78 "Cluster read min block count");
94cluster_read(
struct vnode *vp, u_quad_t filesize, daddr_t lblkno,
long size,
95 struct ucred *cred,
long totread,
int seqcount,
int gbflags,
98 struct buf *bp, *rbp, *reqbp;
101 daddr_t blkno, origblkno;
102 int maxra, racluster;
109 if (!unmapped_buf_allowed)
110 gbflags &= ~GB_UNMAPPED;
116 racluster = vp->v_mount->mnt_iosize_max / size;
119 maxra = min(
nbuf/8, maxra);
120 if (((u_quad_t)(lblkno + maxra + 1) * size) > filesize)
121 maxra = (filesize / size) - lblkno;
126 error =
getblkx(vp, lblkno, lblkno, size, 0, 0, gbflags, &bp);
131 gbflags &= ~GB_NOSPARSE;
140 if (bp->b_flags & B_CACHE) {
143 }
else if ((bp->b_flags & B_RAM) == 0) {
146 bp->b_flags &= ~B_RAM;
148 for (i = 1; i < maxra; i++) {
153 rbp =
gbincore(&vp->v_bufobj, lblkno+i);
154 if (rbp == NULL || (rbp->b_flags & B_INVAL))
162 if ((((i % racluster) == (racluster - 1)) ||
164 && (0 == BUF_LOCK(rbp,
165 LK_EXCLUSIVE | LK_NOWAIT, NULL))) {
166 rbp->b_flags |= B_RAM;
182 off_t firstread = bp->b_offset;
186 KASSERT(bp->b_offset != NOOFFSET,
187 (
"cluster_read: no buffer offset"));
195 if (minread > totread)
202 if (firstread + totread > filesize)
203 totread = filesize - firstread;
204 nblks = howmany(totread, size);
205 if (nblks > racluster)
212 error = VOP_BMAP(vp, lblkno, NULL,
213 &blkno, &ncontig, NULL);
217 if (error || blkno == -1)
227 ncontig = min(ncontig + 1, nblks);
231 blkno, size, nblks, gbflags, bp);
232 lblkno += (bp->b_bufsize / size);
234 bp->b_flags |= B_RAM;
235 bp->b_iocmd = BIO_READ;
244 if ((bp->b_flags & B_CLUSTER) == 0) {
247 bp->b_flags &= ~B_INVAL;
248 bp->b_ioflags &= ~BIO_ERROR;
249 if ((bp->b_flags & B_ASYNC) || bp->b_iodone != NULL)
251 bp->b_iooffset = dbtob(bp->b_blkno);
255 PROC_LOCK(td->td_proc);
256 racct_add_buf(td->td_proc, bp, 0);
257 PROC_UNLOCK(td->td_proc);
260 td->td_ru.ru_inblock++;
266 while (lblkno < (origblkno + maxra)) {
267 error = VOP_BMAP(vp, lblkno, NULL, &blkno, &ncontig, NULL);
280 ncontig = min(ncontig + 1, racluster);
282 size, ncontig, gbflags, NULL);
283 lblkno += (rbp->b_bufsize / size);
284 if (rbp->b_flags & B_DELWRI) {
289 rbp =
getblk(vp, lblkno, size, 0, 0, gbflags);
291 if (rbp->b_flags & B_DELWRI) {
295 rbp->b_flags |= B_ASYNC | B_RAM;
296 rbp->b_iocmd = BIO_READ;
297 rbp->b_blkno = blkno;
299 if (rbp->b_flags & B_CACHE) {
300 rbp->b_flags &= ~B_ASYNC;
304 if ((rbp->b_flags & B_CLUSTER) == 0) {
307 rbp->b_flags &= ~B_INVAL;
308 rbp->b_ioflags &= ~BIO_ERROR;
309 if ((rbp->b_flags & B_ASYNC) || rbp->b_iodone != NULL)
311 rbp->b_iooffset = dbtob(rbp->b_blkno);
315 PROC_LOCK(td->td_proc);
316 racct_add_buf(td->td_proc, rbp, 0);
317 PROC_UNLOCK(td->td_proc);
320 td->td_ru.ru_inblock++;
344 daddr_t blkno,
long size,
int run,
int gbflags,
struct buf *fbp)
346 struct buf *bp, *tbp;
350 int i, inc, j, k, toff;
352 KASSERT(size == vp->v_mount->mnt_stat.f_iosize,
353 (
"cluster_rbuild: size %ld != f_iosize %jd\n",
354 size, (intmax_t)vp->v_mount->mnt_stat.f_iosize));
359 while ((u_quad_t) size * (lbn + run) > filesize) {
365 tbp->b_iocmd = BIO_READ;
367 tbp =
getblk(vp, lbn, size, 0, 0, gbflags);
368 if (tbp->b_flags & B_CACHE)
370 tbp->b_flags |= B_ASYNC | B_RAM;
371 tbp->b_iocmd = BIO_READ;
373 tbp->b_blkno = blkno;
374 if ( (tbp->b_flags & B_MALLOC) ||
375 ((tbp->b_flags & B_VMIO) == 0) || (run <= 1) )
381 MPASS((bp->b_flags & B_MAXPHYS) != 0);
389 bp->b_flags = B_ASYNC | B_CLUSTER | B_VMIO;
390 if ((gbflags & GB_UNMAPPED) != 0) {
393 bp->b_data = (
char *)((vm_offset_t)bp->b_data |
394 ((vm_offset_t)tbp->b_data & PAGE_MASK));
396 bp->b_iocmd = BIO_READ;
400 bp->b_offset = tbp->b_offset;
401 KASSERT(bp->b_offset != NOOFFSET, (
"cluster_rbuild: no buffer offset"));
404 TAILQ_INIT(&bp->b_cluster.cluster_head);
411 for (bn = blkno, i = 0; i < run; ++i, bn += inc) {
413 vm_object_pip_add(tbp->b_bufobj->bo_object,
417 if ((bp->b_npages * PAGE_SIZE) +
418 round_page(size) > vp->v_mount->mnt_iosize_max) {
422 tbp =
getblk(vp, lbn + i, size, 0, 0, GB_LOCK_NOWAIT |
423 (gbflags & GB_UNMAPPED));
438 if ((tbp->b_vflags & BV_BKGRDINPROG) ||
439 (tbp->b_flags & B_CACHE) ||
440 (tbp->b_flags & B_VMIO) == 0) {
452 for (j = 0; tsize > 0; j++) {
453 toff = off & PAGE_MASK;
455 if (toff + tinc > PAGE_SIZE)
456 tinc = PAGE_SIZE - toff;
457 if (vm_page_trysbusy(tbp->b_pages[j]) == 0)
459 if ((tbp->b_pages[j]->valid &
460 vm_page_bits(toff, tinc)) != 0) {
461 vm_page_sunbusy(tbp->b_pages[j]);
464 vm_object_pip_add(tbp->b_bufobj->bo_object, 1);
470 vm_object_pip_wakeupn(tbp->b_bufobj->bo_object,
472 for (k = 0; k < j; k++)
473 vm_page_sunbusy(tbp->b_pages[k]);
481 if ((fbp && (i == 1)) || (i == (run - 1)))
482 tbp->b_flags |= B_RAM;
491 tbp->b_flags |= B_ASYNC;
492 tbp->b_iocmd = BIO_READ;
493 if (tbp->b_blkno == tbp->b_lblkno) {
495 }
else if (tbp->b_blkno != bn) {
504 TAILQ_INSERT_TAIL(&bp->b_cluster.cluster_head,
505 tbp, b_cluster.cluster_entry);
506 for (j = 0; j < tbp->b_npages; j += 1) {
510 if ((bp->b_npages == 0) ||
511 (bp->b_pages[bp->b_npages-1] != m)) {
512 bp->b_pages[bp->b_npages] = m;
515 if (vm_page_all_valid(m))
516 tbp->b_pages[j] = bogus_page;
524 if (tbp->b_bcount != size)
525 printf(
"warning: tbp->b_bcount wrong %ld vs %ld\n", tbp->b_bcount, size);
526 if (tbp->b_bufsize != size)
527 printf(
"warning: tbp->b_bufsize wrong %ld vs %ld\n", tbp->b_bufsize, size);
528 bp->b_bcount += size;
529 bp->b_bufsize += size;
536 for (j = 0; j < bp->b_npages; j++) {
537 if (vm_page_all_valid(bp->b_pages[j]))
538 bp->b_pages[j] = bogus_page;
540 if (bp->b_bufsize > bp->b_kvasize)
541 panic(
"cluster_rbuild: b_bufsize(%ld) > b_kvasize(%d)\n",
542 bp->b_bufsize, bp->b_kvasize);
544 if (buf_mapped(bp)) {
545 pmap_qenter(trunc_page((vm_offset_t) bp->b_data),
546 (vm_page_t *)bp->b_pages, bp->b_npages);
560 struct buf *nbp, *tbp;
566 if (bp->b_ioflags & BIO_ERROR)
569 if (buf_mapped(bp)) {
570 pmap_qremove(trunc_page((vm_offset_t) bp->b_data),
577 for (tbp = TAILQ_FIRST(&bp->b_cluster.cluster_head);
579 nbp = TAILQ_NEXT(&tbp->b_cluster, cluster_entry);
581 tbp->b_ioflags |= BIO_ERROR;
582 tbp->b_error = error;
584 tbp->b_dirtyoff = tbp->b_dirtyend = 0;
585 tbp->b_flags &= ~B_INVAL;
586 tbp->b_ioflags &= ~BIO_ERROR;
594 if (tbp->b_flags & B_DIRECT)
595 tbp->b_flags |= B_RELBUF;
647 u_quad_t filesize,
int seqcount,
int gbflags)
650 int maxclen, cursize;
654 if (!unmapped_buf_allowed)
655 gbflags &= ~GB_UNMAPPED;
657 if (vp->v_type == VREG) {
658 async = DOINGASYNC(vp);
659 lblocksize = vp->v_mount->mnt_stat.f_iosize;
662 lblocksize = bp->b_bufsize;
665 KASSERT(bp->b_offset != NOOFFSET, (
"cluster_write: no buffer offset"));
669 vnc->v_lasta = vnc->v_clen = vnc->v_cstart = vnc->v_lastw = 0;
671 if (vnc->v_clen == 0 || lbn != vnc->v_lastw + 1 ||
672 (bp->b_blkno != vnc->v_lasta + btodb(lblocksize))) {
673 maxclen = vp->v_mount->mnt_iosize_max / lblocksize - 1;
674 if (vnc->v_clen != 0) {
691 cursize = vnc->v_lastw - vnc->v_cstart + 1;
692 if ((u_quad_t)bp->b_offset + lblocksize != filesize ||
693 lbn != vnc->v_lastw + 1 || vnc->v_clen <= cursize) {
694 if (!async && seqcount > 0) {
696 vnc->v_cstart, cursize, gbflags);
699 struct buf **bpp, **endbp;
700 struct cluster_save *buflist;
704 if (buflist == NULL) {
712 endbp = &buflist->bs_children
713 [buflist->bs_nchildren - 1];
714 if (VOP_REALLOCBLKS(vp, buflist)) {
723 for (bpp = buflist->bs_children;
726 free(buflist, M_SEGMENT);
729 lblocksize, vnc->v_cstart,
736 for (bpp = buflist->bs_children;
739 free(buflist, M_SEGMENT);
741 vnc->v_lasta = bp->b_blkno;
751 if (vp->v_type == VREG &&
752 (u_quad_t) bp->b_offset + lblocksize != filesize &&
753 bp->b_blkno == bp->b_lblkno &&
754 (VOP_BMAP(vp, lbn, NULL, &bp->b_blkno, &maxclen,
755 NULL) != 0 || bp->b_blkno == -1)) {
760 vnc->v_cstart = lbn + 1;
764 vnc->v_clen = maxclen;
766 if (!async && maxclen == 0) {
767 vnc->v_cstart = lbn + 1;
773 }
else if (lbn == vnc->v_cstart + vnc->v_clen) {
783 vnc->v_clen + 1, gbflags);
786 vnc->v_cstart = lbn + 1;
787 }
else if (vm_page_count_severe()) {
814 struct buf *bp, *tbp;
817 int totalwritten = 0;
818 int dbsize = btodb(size);
820 if (!unmapped_buf_allowed)
821 gbflags &= ~GB_UNMAPPED;
831 if ((tbp =
gbincore(&vp->v_bufobj, start_lbn)) == NULL ||
832 (tbp->b_vflags & BV_BKGRDINPROG)) {
839 LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, BO_LOCKPTR(bo))) {
844 if ((tbp->b_flags & (B_INVAL | B_DELWRI)) != B_DELWRI) {
851 tbp->b_flags &= ~B_DONE;
861 if (((tbp->b_flags & (B_CLUSTEROK | B_MALLOC | B_VMIO)) !=
862 (B_CLUSTEROK | B_VMIO)) ||
863 (tbp->b_bcount != tbp->b_bufsize) ||
864 (tbp->b_bcount != size) ||
867 totalwritten += tbp->b_bufsize;
873 MPASS((bp->b_flags & B_MAXPHYS) != 0);
879 TAILQ_INIT(&bp->b_cluster.cluster_head);
883 if (tbp->b_wcred != NOCRED)
884 bp->b_wcred =
crhold(tbp->b_wcred);
886 bp->b_blkno = tbp->b_blkno;
887 bp->b_lblkno = tbp->b_lblkno;
888 bp->b_offset = tbp->b_offset;
896 if ((gbflags & GB_UNMAPPED) == 0 ||
897 (tbp->b_flags & B_VMIO) == 0) {
898 bp->b_data = (
char *)((vm_offset_t)bp->b_data |
899 ((vm_offset_t)tbp->b_data & PAGE_MASK));
903 bp->b_flags |= B_CLUSTER | (tbp->b_flags & (B_VMIO |
912 for (i = 0; i < len; ++i, ++start_lbn) {
919 if ((tbp =
gbincore(bo, start_lbn)) == NULL ||
920 (tbp->b_vflags & BV_BKGRDINPROG)) {
933 LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK,
937 if ((tbp->b_flags & (B_VMIO | B_CLUSTEROK |
938 B_INVAL | B_DELWRI | B_NEEDCOMMIT))
939 != (B_DELWRI | B_CLUSTEROK |
940 (bp->b_flags & (B_VMIO | B_NEEDCOMMIT))) ||
941 tbp->b_wcred != bp->b_wcred) {
951 if ((tbp->b_bcount != size) ||
952 ((bp->b_blkno + (dbsize * i)) !=
954 ((tbp->b_npages + bp->b_npages) >
955 (vp->v_mount->mnt_iosize_max / PAGE_SIZE))) {
966 tbp->b_flags &= ~B_DONE;
976 if (tbp->b_flags & B_VMIO) {
982 for (j = 0; j < tbp->b_npages; j += 1) {
984 if (vm_page_trysbusy(m) == 0) {
985 for (j--; j >= 0; j--)
993 vm_object_pip_add(tbp->b_bufobj->bo_object,
995 for (j = 0; j < tbp->b_npages; j += 1) {
997 if ((bp->b_npages == 0) ||
998 (bp->b_pages[bp->b_npages - 1] != m)) {
999 bp->b_pages[bp->b_npages] = m;
1004 bp->b_bcount += size;
1005 bp->b_bufsize += size;
1011 bp->b_flags |= (tbp->b_flags & B_BARRIER);
1012 tbp->b_flags &= ~(B_DONE | B_BARRIER);
1013 tbp->b_flags |= B_ASYNC;
1014 tbp->b_ioflags &= ~BIO_ERROR;
1015 tbp->b_iocmd = BIO_WRITE;
1020 buf_track(tbp, __func__);
1021 TAILQ_INSERT_TAIL(&bp->b_cluster.cluster_head,
1022 tbp, b_cluster.cluster_entry);
1025 if (buf_mapped(bp)) {
1026 pmap_qenter(trunc_page((vm_offset_t) bp->b_data),
1027 (vm_page_t *)bp->b_pages, bp->b_npages);
1029 if (bp->b_bufsize > bp->b_kvasize)
1031 "cluster_wbuild: b_bufsize(%ld) > b_kvasize(%d)\n",
1032 bp->b_bufsize, bp->b_kvasize);
1033 totalwritten += bp->b_bufsize;
1035 bp->b_dirtyend = bp->b_bufsize;
1040 return totalwritten;
1047static struct cluster_save *
1049 struct buf *last_bp,
int gbflags)
1051 struct cluster_save *buflist;
1054 int i, j, len, error;
1056 len = vnc->v_lastw - vnc->v_cstart + 1;
1057 buflist =
malloc(
sizeof(
struct buf *) * (len + 1) +
sizeof(*buflist),
1058 M_SEGMENT, M_WAITOK);
1059 buflist->bs_nchildren = 0;
1060 buflist->bs_children = (
struct buf **) (buflist + 1);
1061 for (lbn = vnc->v_cstart, i = 0; i < len; lbn++, i++) {
1062 error = bread_gb(vp, lbn, last_bp->b_bcount, NOCRED,
1069 for (j = 0; j < i; j++)
1070 brelse(buflist->bs_children[j]);
1071 free(buflist, M_SEGMENT);
1074 buflist->bs_children[i] = bp;
1075 if (bp->b_blkno == bp->b_lblkno)
1076 VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno,
1079 buflist->bs_children[i] = bp = last_bp;
1080 if (bp->b_blkno == bp->b_lblkno)
1081 VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL, NULL);
1082 buflist->bs_nchildren = i + 1;
void *() malloc(size_t size, struct malloc_type *mtp, int flags)
void free(void *addr, struct malloc_type *mtp)
struct ucred * crhold(struct ucred *cr)
void panic(const char *fmt,...)
int printf(const char *fmt,...)
void bufdone(struct buf *bp)
caddr_t __read_mostly unmapped_buf
void bremfree(struct buf *bp)
int bufwait(struct buf *bp)
void bawrite(struct buf *bp)
void vfs_busy_pages_acquire(struct buf *bp)
struct buf * getblk(struct vnode *vp, daddr_t blkno, int size, int slpflag, int slptimeo, int flags)
void bufobj_wref(struct bufobj *bo)
void vfs_busy_pages(struct buf *bp, int clear_modify)
void bundirty(struct buf *bp)
int getblkx(struct vnode *vp, daddr_t blkno, daddr_t dblkno, int size, int slpflag, int slptimeo, int flags, struct buf **bpp)
void bqrelse(struct buf *bp)
void bdwrite(struct buf *bp)
void brelse(struct buf *bp)
void cluster_init_vn(struct vn_clusterw *vnc)
int cluster_wbuild(struct vnode *vp, long size, daddr_t start_lbn, int len, int gbflags)
static void cluster_init(void *)
int cluster_read(struct vnode *vp, u_quad_t filesize, daddr_t lblkno, long size, struct ucred *cred, long totread, int seqcount, int gbflags, struct buf **bpp)
SYSCTL_INT(_vfs, OID_AUTO, write_behind, CTLFLAG_RW, &write_behind, 0, "Cluster write-behind; 0: disable, 1: enable, 2: backed off")
static __inline int cluster_wbuild_wb(struct vnode *vp, long size, daddr_t start_lbn, int len, int gbflags)
void cluster_write(struct vnode *vp, struct vn_clusterw *vnc, struct buf *bp, u_quad_t filesize, int seqcount, int gbflags)
static struct cluster_save * cluster_collectbufs(struct vnode *vp, struct vn_clusterw *vnc, struct buf *last_bp, int gbflags)
SYSINIT(cluster, SI_SUB_CPU, SI_ORDER_ANY, cluster_init, NULL)
static uma_zone_t cluster_pbuf_zone
static struct buf * cluster_rbuild(struct vnode *vp, u_quad_t filesize, daddr_t lbn, daddr_t blkno, long size, int run, int gbflags, struct buf *fbp)
static void cluster_callback(struct buf *)
static MALLOC_DEFINE(M_SEGMENT, "cl_savebuf", "cluster_save buffer")
void reassignbuf(struct buf *bp)
struct buf * gbincore(struct bufobj *bo, daddr_t lblkno)