From nobody Wed Oct 19 17:24:23 2022 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4MsyJm1ZMNz4gT2G; Wed, 19 Oct 2022 17:24:24 +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 4MsyJl6Q9Mz3hgd; Wed, 19 Oct 2022 17:24:23 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1666200263; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=bLWCs5/rUUPGnJURMtt86LODEjHiywXElacUhl5Ov9o=; b=Mn3UurZZWg3aPDBL9iA9B6rFbmFH/fsfj2SSCv7VuVGWvAop163VcEWHg1utjZLGSynHQ0 IS2roSCDV5qGuy1+LiSd2d/Nm3e5775c1Zvg5vIjj0Yj8fQOTXlaj3w2d1/cV4n95KNmlW dD0H5T4QduemPWPy1PBuSnasoBWEiZ1v0vvZPO9RWFdYD/gg4KuTZPB0E8xni+xXSJYiIf 0K2fRSqVbTVbhd24+TwdD7uTbfHU3OIDRJN3Ns5DaMD0Ytv3dzTJ3x/7H7oJWBiXIkqP7j NOf27jVwJOqTZQ+A5t8XddaaKmuJXWf5WoCJrdCfBqDqlCa2oa0Dfc2FCB7yqg== 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 4MsyJl5VrBzc2H; Wed, 19 Oct 2022 17:24:23 +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 29JHONaM073899; Wed, 19 Oct 2022 17:24:23 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 29JHON9J073898; Wed, 19 Oct 2022 17:24:23 GMT (envelope-from git) Date: Wed, 19 Oct 2022 17:24:23 GMT Message-Id: <202210191724.29JHON9J073898@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Konstantin Belousov Subject: git: 85cff1455a8c - main - tmpfs: implement FIOSEEKDATA and FIOSEEKHOLE List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 85cff1455a8c3422c6ba8a9f895b5d65118f3499 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1666200263; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=bLWCs5/rUUPGnJURMtt86LODEjHiywXElacUhl5Ov9o=; b=H04HsPDWBaZrLP+5cScCIQu7rte0Z2Cl8xknAH2Bj0fDsRa7Gkd57cw8xTqoDgyHR5pMiA /5Jc6oPKh8lXnrRPyTOQ13lIAmsz4C3Cy6ocYCDm0O4hriXeve9jFAUMXCozrfpFiX9Iu+ ulL/CbBTzHZhaKrbK0kBCJsIlgqXVwsAmdqE/phY+IzOHO3EEIJ41bO7f47DPfx0OmEY92 M/468LZmTyez2jRtrxptkGraXGFSCZHU9ldmZmxARvZZmcn7+Hw5xTWdsAq8uVNkdqOW3a 9pxVNDXoNwhkHAAj/YHonpfWdbCCnWZoBsnwSx4tVm6zzz6+3rIHBTn9hanObQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1666200263; a=rsa-sha256; cv=none; b=o3TSWm+Vc0CG836EdLuPoEszk54nfHiZdf2JD5jZ6MYPdp0zJpfCRmLOSDLMoa1ibyvzIV 0WUgjBC/7w7j/cdxLAFdwp+WtkwcBylUhKRT245scYaidA1V06fJZJJYB2zesA/Ly+2wuK LqbIrlU6nO7mC0xtkWn+/WhsCy3RsfO9UeSdyNGUrG5BcMKeNby9dpQ8GWwzziQ4Hag7Sv i6bNHsjIdPE1QLjBPu9dw7ujnF05xweIjE3hKTfgVgvVkecNm4QWF1IyIdZ/UC7yhxJygL q/ZopMKlr+FT39+wCf6Dob/TjGnm7R8e9/XNJk8f+kOHq7SWEV47qAmPT8XpRg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=85cff1455a8c3422c6ba8a9f895b5d65118f3499 commit 85cff1455a8c3422c6ba8a9f895b5d65118f3499 Author: Konstantin Belousov AuthorDate: 2022-10-17 15:26:43 +0000 Commit: Konstantin Belousov CommitDate: 2022-10-19 17:24:07 +0000 tmpfs: implement FIOSEEKDATA and FIOSEEKHOLE providing the support for lseek(2) SEEK_DATA and SEEK_HOLE. Reviewed by: markj Tested by: pho Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D37024 --- sys/fs/tmpfs/tmpfs_vnops.c | 130 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) diff --git a/sys/fs/tmpfs/tmpfs_vnops.c b/sys/fs/tmpfs/tmpfs_vnops.c index 9bcf835aa627..aaeaa843eb43 100644 --- a/sys/fs/tmpfs/tmpfs_vnops.c +++ b/sys/fs/tmpfs/tmpfs_vnops.c @@ -43,6 +43,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -63,6 +64,9 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include +#include +#include #include #include @@ -1820,6 +1824,131 @@ restart_locked: return (ENOENT); } +static off_t +tmpfs_seek_data_locked(vm_object_t obj, off_t noff) +{ + vm_page_t m; + vm_pindex_t p, p_m, p_swp; + + p = OFF_TO_IDX(noff); + m = vm_page_find_least(obj, p); + + /* + * Microoptimize the most common case for SEEK_DATA, where + * there is no hole and the page is resident. + */ + if (m != NULL && vm_page_any_valid(m) && m->pindex == p) + return (noff); + + p_swp = swap_pager_find_least(obj, p); + if (p_swp == p) + return (noff); + + p_m = m == NULL ? obj->size : m->pindex; + return (IDX_TO_OFF(MIN(p_m, p_swp))); +} + +static off_t +tmpfs_seek_next(off_t noff) +{ + return (noff + PAGE_SIZE - (noff & PAGE_MASK)); +} + +static int +tmpfs_seek_clamp(struct tmpfs_node *tn, off_t *noff, bool seekdata) +{ + if (*noff < tn->tn_size) + return (0); + if (seekdata) + return (ENXIO); + *noff = tn->tn_size; + return (0); +} + +static off_t +tmpfs_seek_hole_locked(vm_object_t obj, off_t noff) +{ + vm_page_t m; + vm_pindex_t p, p_swp; + + for (;; noff = tmpfs_seek_next(noff)) { + /* + * Walk over the largest sequential run of the valid pages. + */ + for (m = vm_page_lookup(obj, OFF_TO_IDX(noff)); + m != NULL && vm_page_any_valid(m); + m = vm_page_next(m), noff = tmpfs_seek_next(noff)) + ; + + /* + * Found a hole in the object's page queue. Check if + * there is a hole in the swap at the same place. + */ + p = OFF_TO_IDX(noff); + p_swp = swap_pager_find_least(obj, p); + if (p_swp != p) { + noff = IDX_TO_OFF(p); + break; + } + } + return (noff); +} + +static int +tmpfs_seek_datahole(struct vnode *vp, off_t *off, bool seekdata) +{ + struct tmpfs_node *tn; + vm_object_t obj; + off_t noff; + int error; + + if (vp->v_type != VREG) + return (ENOTTY); + tn = VP_TO_TMPFS_NODE(vp); + noff = *off; + if (noff < 0) + return (ENXIO); + error = tmpfs_seek_clamp(tn, &noff, seekdata); + if (error != 0) + return (error); + obj = tn->tn_reg.tn_aobj; + + VM_OBJECT_RLOCK(obj); + noff = seekdata ? tmpfs_seek_data_locked(obj, noff) : + tmpfs_seek_hole_locked(obj, noff); + VM_OBJECT_RUNLOCK(obj); + + error = tmpfs_seek_clamp(tn, &noff, seekdata); + if (error == 0) + *off = noff; + return (error); +} + +static int +tmpfs_ioctl(struct vop_ioctl_args *ap) +{ + struct vnode *vp = ap->a_vp; + int error = 0; + + switch (ap->a_command) { + case FIOSEEKDATA: + case FIOSEEKHOLE: + error = vn_lock(vp, LK_SHARED); + if (error != 0) { + error = EBADF; + break; + } + error = tmpfs_seek_datahole(vp, (off_t *)ap->a_data, + ap->a_command == FIOSEEKDATA); + VOP_UNLOCK(vp); + break; + default: + error = ENOTTY; + break; + } + return (error); +} + /* * Vnode operations vector used for files stored in a tmpfs file system. */ @@ -1863,6 +1992,7 @@ struct vop_vector tmpfs_vnodeop_entries = { .vop_unlock = vop_unlock, .vop_islocked = vop_islocked, .vop_add_writecount = vop_stdadd_writecount_nomsync, + .vop_ioctl = tmpfs_ioctl, }; VFS_VOP_VECTOR_REGISTER(tmpfs_vnodeop_entries);