From owner-dev-commits-src-all@freebsd.org Wed Aug 25 21:35:14 2021 Return-Path: Delivered-To: dev-commits-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 261F465B833; Wed, 25 Aug 2021 21:35:14 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Gvzm20Q0Xz4lqp; Wed, 25 Aug 2021 21:35:14 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id DC51D23DE0; Wed, 25 Aug 2021 21:35:13 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 17PLZDMn004059; Wed, 25 Aug 2021 21:35:13 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 17PLZDQR004058; Wed, 25 Aug 2021 21:35:13 GMT (envelope-from git) Date: Wed, 25 Aug 2021 21:35:13 GMT Message-Id: <202108252135.17PLZDQR004058@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Ka Ho Ng Subject: git: 8d7cd10ba633 - main - tmpfs: Implement VOP_DEALLOCATE MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: khng X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 8d7cd10ba633309a2fa8c0d6475f85e0266e3d94 Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-all@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commit messages for all branches of the src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 25 Aug 2021 21:35:14 -0000 The branch main has been updated by khng: URL: https://cgit.FreeBSD.org/src/commit/?id=8d7cd10ba633309a2fa8c0d6475f85e0266e3d94 commit 8d7cd10ba633309a2fa8c0d6475f85e0266e3d94 Author: Ka Ho Ng AuthorDate: 2021-08-25 21:34:35 +0000 Commit: Ka Ho Ng CommitDate: 2021-08-25 21:34:54 +0000 tmpfs: Implement VOP_DEALLOCATE Implementing VOP_DEALLOCATE to allow hole-punching in the same manner as POSIX shared memory's fspacectl(SPACECTL_DEALLOC) support. Sponsored by: The FreeBSD Foundation Reviewed by: kib Differential Revision: https://reviews.freebsd.org/D31684 --- sys/fs/tmpfs/tmpfs.h | 1 + sys/fs/tmpfs/tmpfs_subr.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++ sys/fs/tmpfs/tmpfs_vnops.c | 7 ++++ 3 files changed, 93 insertions(+) diff --git a/sys/fs/tmpfs/tmpfs.h b/sys/fs/tmpfs/tmpfs.h index 99368d67aaaa..bfa12b0382bc 100644 --- a/sys/fs/tmpfs/tmpfs.h +++ b/sys/fs/tmpfs/tmpfs.h @@ -459,6 +459,7 @@ int tmpfs_dir_getdents(struct tmpfs_mount *, struct tmpfs_node *, int tmpfs_dir_whiteout_add(struct vnode *, struct componentname *); void tmpfs_dir_whiteout_remove(struct vnode *, struct componentname *); int tmpfs_reg_resize(struct vnode *, off_t, boolean_t); +int tmpfs_reg_punch_hole(struct vnode *vp, off_t *, off_t *); int tmpfs_chflags(struct vnode *, u_long, struct ucred *, struct thread *); int tmpfs_chmod(struct vnode *, mode_t, struct ucred *, struct thread *); int tmpfs_chown(struct vnode *, uid_t, gid_t, struct ucred *, diff --git a/sys/fs/tmpfs/tmpfs_subr.c b/sys/fs/tmpfs/tmpfs_subr.c index e746a7455860..1b7521cf0b0d 100644 --- a/sys/fs/tmpfs/tmpfs_subr.c +++ b/sys/fs/tmpfs/tmpfs_subr.c @@ -1775,6 +1775,91 @@ tmpfs_reg_resize(struct vnode *vp, off_t newsize, boolean_t ignerr) return (0); } +/* + * Punch hole in the aobj associated with the regular file pointed to by 'vp'. + * Requests completely beyond the end-of-file are converted to no-op. + * + * Returns 0 on success or error code from tmpfs_partial_page_invalidate() on + * failure. + */ +int +tmpfs_reg_punch_hole(struct vnode *vp, off_t *offset, off_t *length) +{ + struct tmpfs_mount *tmp; + struct tmpfs_node *node; + vm_object_t object; + vm_pindex_t pistart, pi, piend; + int startofs, endofs, end; + off_t off, len; + int error; + + KASSERT(*length <= OFF_MAX - *offset, ("%s: offset + length overflows", + __func__)); + node = VP_TO_TMPFS_NODE(vp); + KASSERT(node->tn_type == VREG, ("%s: node is not regular file", + __func__)); + object = node->tn_reg.tn_aobj; + tmp = VFS_TO_TMPFS(vp->v_mount); + off = *offset; + len = omin(node->tn_size - off, *length); + startofs = off & PAGE_MASK; + endofs = (off + len) & PAGE_MASK; + pistart = OFF_TO_IDX(off); + piend = OFF_TO_IDX(off + len); + pi = OFF_TO_IDX((vm_ooffset_t)off + PAGE_MASK); + error = 0; + + /* Handle the case when offset is on or beyond file size. */ + if (len <= 0) { + *length = 0; + return (0); + } + + VM_OBJECT_WLOCK(object); + + /* + * If there is a partial page at the beginning of the hole-punching + * request, fill the partial page with zeroes. + */ + if (startofs != 0) { + end = pistart != piend ? PAGE_SIZE : endofs; + error = tmpfs_partial_page_invalidate(object, pistart, startofs, + end, FALSE); + if (error != 0) + goto out; + off += end - startofs; + len -= end - startofs; + } + + /* + * Toss away the full pages in the affected area. + */ + if (pi < piend) { + vm_object_page_remove(object, pi, piend, 0); + off += IDX_TO_OFF(piend - pi); + len -= IDX_TO_OFF(piend - pi); + } + + /* + * If there is a partial page at the end of the hole-punching request, + * fill the partial page with zeroes. + */ + if (endofs != 0 && pistart != piend) { + error = tmpfs_partial_page_invalidate(object, piend, 0, endofs, + FALSE); + if (error != 0) + goto out; + off += endofs; + len -= endofs; + } + +out: + VM_OBJECT_WUNLOCK(object); + *offset = off; + *length = len; + return (error); +} + void tmpfs_check_mtime(struct vnode *vp) { diff --git a/sys/fs/tmpfs/tmpfs_vnops.c b/sys/fs/tmpfs/tmpfs_vnops.c index 326a5132990d..d8c74cecdfe4 100644 --- a/sys/fs/tmpfs/tmpfs_vnops.c +++ b/sys/fs/tmpfs/tmpfs_vnops.c @@ -695,6 +695,12 @@ out: return (error); } +static int +tmpfs_deallocate(struct vop_deallocate_args *v) +{ + return (tmpfs_reg_punch_hole(v->a_vp, v->a_offset, v->a_len)); +} + static int tmpfs_fsync(struct vop_fsync_args *v) { @@ -1840,6 +1846,7 @@ struct vop_vector tmpfs_vnodeop_entries = { .vop_read = tmpfs_read, .vop_read_pgcache = tmpfs_read_pgcache, .vop_write = tmpfs_write, + .vop_deallocate = tmpfs_deallocate, .vop_fsync = tmpfs_fsync, .vop_remove = tmpfs_remove, .vop_link = tmpfs_link,