git: daa2c99c89f2 - main - p9fs: implement working putpages (fix mmap write)

From: Doug Rabson <dfr_at_FreeBSD.org>
Date: Mon, 24 Jun 2024 16:12:45 UTC
The branch main has been updated by dfr:

URL: https://cgit.FreeBSD.org/src/commit/?id=daa2c99c89f224200911aa6c8cdee07bc354947a

commit daa2c99c89f224200911aa6c8cdee07bc354947a
Author:     Val Packett <val@packett.cool>
AuthorDate: 2024-06-24 13:37:58 +0000
Commit:     Doug Rabson <dfr@FreeBSD.org>
CommitDate: 2024-06-24 16:11:47 +0000

    p9fs: implement working putpages (fix mmap write)
    
    Mostly copied from smbfs. This driver in its current state has the exact
    same issue that prevents the generic putpages implementation from
    working.
    
    Sponsored by:           https://www.patreon.com/valpackett
    Reviewed by:            dfr
    Differential Revision:  https://reviews.freebsd.org/D45639
    MFC after:              3 months
---
 sys/fs/p9fs/p9fs_vfsops.c |  6 ++++
 sys/fs/p9fs/p9fs_vnops.c  | 79 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 85 insertions(+)

diff --git a/sys/fs/p9fs/p9fs_vfsops.c b/sys/fs/p9fs/p9fs_vfsops.c
index 6cc65aca35d3..f674c866ce9d 100644
--- a/sys/fs/p9fs/p9fs_vfsops.c
+++ b/sys/fs/p9fs/p9fs_vfsops.c
@@ -35,6 +35,7 @@
 #include <sys/mount.h>
 #include <sys/sysctl.h>
 #include <sys/vnode.h>
+#include <sys/buf.h>
 #include <vm/uma.h>
 
 #include <fs/p9fs/p9fs_proto.h>
@@ -53,6 +54,7 @@ static uma_zone_t p9fs_node_zone;
 uma_zone_t p9fs_io_buffer_zone;
 uma_zone_t p9fs_getattr_zone;
 uma_zone_t p9fs_setattr_zone;
+uma_zone_t p9fs_pbuf_zone;
 extern struct vop_vector p9fs_vnops;
 
 /* option parsing */
@@ -106,6 +108,9 @@ p9fs_init(struct vfsconf *vfsp)
 	p9fs_setattr_zone = uma_zcreate("p9fs setattr zone",
 	    sizeof(struct p9_iattr_dotl), NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
 
+	/* Create the putpages zone */
+	p9fs_pbuf_zone = pbuf_zsecond_create("p9fs pbuf zone", nswbuf / 2);
+
 	/*
 	 * Create the io_buffer zone pool to keep things simpler in case of
 	 * multiple threads. Each thread works with its own so there is no
@@ -126,6 +131,7 @@ p9fs_uninit(struct vfsconf *vfsp)
 	uma_zdestroy(p9fs_io_buffer_zone);
 	uma_zdestroy(p9fs_getattr_zone);
 	uma_zdestroy(p9fs_setattr_zone);
+	uma_zdestroy(p9fs_pbuf_zone);
 
 	return (0);
 }
diff --git a/sys/fs/p9fs/p9fs_vnops.c b/sys/fs/p9fs/p9fs_vnops.c
index 77162c0a4aff..5556e60f4bae 100644
--- a/sys/fs/p9fs/p9fs_vnops.c
+++ b/sys/fs/p9fs/p9fs_vnops.c
@@ -39,10 +39,14 @@
 #include <sys/priv.h>
 #include <sys/stat.h>
 #include <sys/vnode.h>
+#include <sys/rwlock.h>
+#include <sys/vmmeter.h>
 
 #include <vm/vm.h>
 #include <vm/vm_extern.h>
 #include <vm/vm_object.h>
+#include <vm/vm_page.h>
+#include <vm/vm_pager.h>
 #include <vm/vnode_pager.h>
 
 #include <fs/p9fs/p9_client.h>
@@ -62,6 +66,7 @@ static MALLOC_DEFINE(M_P9UIOV, "uio", "UIOV structures for strategy in p9fs");
 extern uma_zone_t p9fs_io_buffer_zone;
 extern uma_zone_t p9fs_getattr_zone;
 extern uma_zone_t p9fs_setattr_zone;
+extern uma_zone_t p9fs_pbuf_zone;
 /* For the root vnode's vnops. */
 struct vop_vector p9fs_vnops;
 
@@ -2126,6 +2131,79 @@ out:
 	return (error);
 }
 
+/*
+ * Put VM pages, synchronously.
+ * XXX: like smbfs, cannot use vop_stdputpages due to mapping requirement
+ */
+static int
+p9fs_putpages(struct vop_putpages_args *ap)
+{
+	struct uio uio;
+	struct iovec iov;
+	int i, error, npages, count;
+	off_t offset;
+	int *rtvals;
+	struct vnode *vp;
+	struct thread *td;
+	struct ucred *cred;
+	struct p9fs_node *np;
+	vm_page_t *pages;
+	vm_offset_t kva;
+	struct buf *bp;
+
+	vp = ap->a_vp;
+	np = P9FS_VTON(vp);
+	td = curthread;
+	cred = curthread->td_ucred;
+	pages = ap->a_m;
+	count = ap->a_count;
+	rtvals = ap->a_rtvals;
+	npages = btoc(count);
+	offset = IDX_TO_OFF(pages[0]->pindex);
+
+	/*
+	 * When putting pages, do not extend file past EOF.
+	 */
+	if (offset + count > np->inode.i_size) {
+		count = np->inode.i_size - offset;
+		if (count < 0)
+			count = 0;
+	}
+
+	for (i = 0; i < npages; i++)
+		rtvals[i] = VM_PAGER_ERROR;
+
+	bp = uma_zalloc(p9fs_pbuf_zone, M_WAITOK);
+	kva = (vm_offset_t) bp->b_data;
+	pmap_qenter(kva, pages, npages);
+
+	VM_CNT_INC(v_vnodeout);
+	VM_CNT_ADD(v_vnodepgsout, count);
+
+	iov.iov_base = (caddr_t) kva;
+	iov.iov_len = count;
+	uio.uio_iov = &iov;
+	uio.uio_iovcnt = 1;
+	uio.uio_offset = offset;
+	uio.uio_resid = count;
+	uio.uio_segflg = UIO_SYSSPACE;
+	uio.uio_rw = UIO_WRITE;
+	uio.uio_td = td;
+
+	P9_DEBUG(VOPS, "of=%jd,resid=%zd\n", (intmax_t)uio.uio_offset, uio.uio_resid);
+
+	error = VOP_WRITE(vp, &uio, vnode_pager_putpages_ioflags(ap->a_sync),
+	    cred);
+
+	pmap_qremove(kva, npages);
+	uma_zfree(p9fs_pbuf_zone, bp);
+
+	if (error == 0)
+		vnode_pager_undirty_pages(pages, rtvals, count - uio.uio_resid,
+		    np->inode.i_size - offset, npages * PAGE_SIZE);
+
+	return (rtvals[0]);
+}
 
 struct vop_vector p9fs_vnops = {
 	.vop_default =		&default_vnodeops,
@@ -2150,5 +2228,6 @@ struct vop_vector p9fs_vnops = {
 	.vop_rename =           p9fs_rename,
 	.vop_link =		p9fs_link,
 	.vop_readlink =		p9fs_readlink,
+	.vop_putpages =		p9fs_putpages,
 };
 VFS_VOP_VECTOR_REGISTER(p9fs_vnops);