From nobody Wed Oct 26 00:44:06 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 4MxqnL6FZ5z4gWWJ; Wed, 26 Oct 2022 00:44:06 +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 4MxqnL5Sg6z42JY; Wed, 26 Oct 2022 00:44:06 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1666745046; 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=pJm2cF3M9R9v8uaFWSrB4YiQCzKaPhSAjKjJH4ykJwk=; b=jWPh09sn92DOnpl9wryE8y4B2eiYV7NDCsV1LmF0aBfmwML+czmXEVErodZRm3fYhuLr4Z Bkv2nqaMysFM2Y97Q719R9XPzMpl5W9/R4CaXObBOU24SKTCkO5wBqoy/n/jCP8S8at6mV 3WYhUhHolIZNda+1hkDtAPm/wLbhtIG7xkqNxOmjW5xh7mnipaz1w2Om2sjM1+UutjLAMA ZBMs29iBdMMwmKKik3SDB7yPci0caeXJY/+nLP7fquAupVdGbVP60O9HeYk5A9V6D+9+R+ lTgrMAM/boLD2M3/5vheAZAAJl1oDaBiXxc+xx/t41hTjBeuMOE/eCrCLwW8ww== 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 4MxqnL4V4JzxrR; Wed, 26 Oct 2022 00:44:06 +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 29Q0i61W029658; Wed, 26 Oct 2022 00:44:06 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 29Q0i6k6029657; Wed, 26 Oct 2022 00:44:06 GMT (envelope-from git) Date: Wed, 26 Oct 2022 00:44:06 GMT Message-Id: <202210260044.29Q0i6k6029657@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Konstantin Belousov Subject: git: d48d1f29888c - stable/13 - 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/stable/13 X-Git-Reftype: branch X-Git-Commit: d48d1f29888cf1dd68ca5b06974ab9005beb932c Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1666745046; 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=pJm2cF3M9R9v8uaFWSrB4YiQCzKaPhSAjKjJH4ykJwk=; b=sca77Xi3yAT1ixKyagb8AGRrhK6/IlH4SQjNzrK16tDkRYkQDZvH86Ibetdj6xIOLb2lvn d9qDQaf245rag0rTU1eTZ06xHsgVhxoG1D6kS/TG8lM4RuviQIDDQGgxGEUI9Hc5kbD/ew hGf0gpHGO2s5PrI3YIlWj7bmwOtZmWrZtdnYOExRNtp9eCk8FY3GntfkME0UrufvJFuFyr Wu/FPKhXzAvqtS7d8vrppdyw4UcUGcYhcUs9/Yf0tKvWUQXTPckOBrTBfJ6waOZaAvGtFE SgoUSXxFBNSy2vTq22XtMYGIo/wFBfigkwihWvwREPns/LS/hTySBHAQaK84sA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1666745046; a=rsa-sha256; cv=none; b=RuhbrhustFw7vczpy3eorpH/d8p5/EVYtKxa3tuF6Jq/12hPbAlXRSOQvJLoi+2lbKW93+ iqwK4URmVDnJvGrCrjGue/QfMynkM+S3CI84JwFk50N2HVDXjPrzrJNqU+3hT4i21f8ckR +pWzYdtp7+cAGFRFq5mFS5Cpw6fIFcGI72owsOduSi5aq0ySuY7dOCu8fWjDpDzmDegmKI 11wgjCRQliTxZKXTnwa4ZFFDEnQy3S2jqhFTU/pD1HtAqvSrWw23+Ho78ol6yBSE/2ZFaG AYw6MyhTm7RxlbgIgBivBOickGxmh14fRzEF8oi4QuCCdxb/it+oCF4M1y+QRA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=d48d1f29888cf1dd68ca5b06974ab9005beb932c commit d48d1f29888cf1dd68ca5b06974ab9005beb932c Author: Konstantin Belousov AuthorDate: 2022-10-17 15:26:43 +0000 Commit: Konstantin Belousov CommitDate: 2022-10-26 00:43:43 +0000 tmpfs: implement FIOSEEKDATA and FIOSEEKHOLE (cherry picked from commit 85cff1455a8c3422c6ba8a9f895b5d65118f3499) --- 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 fdadad364ca2..825e3571b599 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 @@ -1824,6 +1828,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. */ @@ -1865,6 +1994,7 @@ struct vop_vector tmpfs_vnodeop_entries = { .vop_lock1 = vop_lock, .vop_unlock = vop_unlock, .vop_islocked = vop_islocked, + .vop_ioctl = tmpfs_ioctl, }; VFS_VOP_VECTOR_REGISTER(tmpfs_vnodeop_entries);