From nobody Sat Oct 08 00:39:10 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 4MkmWy5Cmlz4fGMF; Sat, 8 Oct 2022 00:39:10 +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 4MkmWy3sf9z3p8n; Sat, 8 Oct 2022 00:39:10 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1665189550; 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=dLS2UP/lVj6gn4W8g10BhAsaWo2NxNxfTP28TpJPlBY=; b=hB932Mb5Jz3LlyYlVEhZVyUkFykaalTDOkRRg4Fk8g7tR/0slvj6D3wGUP7Zg91MkZoJHu pCN2ZfSnQ7EzQ5ZnbFbwtZYQEBqvyJmFbClMBcFEpmA+VM4kCeLxI4Y+LckaoNElQZTRLU pUS2eJv3ontuhZqHhFOb6cAIMTxMYuMXsvu/JpABz78mCtTt1HkLDn2SMsva6Cg13uQ3O9 wD9fQMt+/9wVm5lKDaO+UYLg/h3Pmd+aB4XVpsBffV5ric1JQBTfeWJifiamnEWH80krhQ ehOIdcWW2R0ufCVEEjSm6D9QGin2SEI1R4Vprza2E6LCRVppCdHxgsCEiHRhOg== 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 4MkmWy2zgMzmZx; Sat, 8 Oct 2022 00:39:10 +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 2980dAKV034381; Sat, 8 Oct 2022 00:39:10 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 2980dA8O034380; Sat, 8 Oct 2022 00:39:10 GMT (envelope-from git) Date: Sat, 8 Oct 2022 00:39:10 GMT Message-Id: <202210080039.2980dA8O034380@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: f0ebdb4254e8 - stable/13 - Add vn_rlimit_fsizex() and vn_rlimit_fsizex_res() 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: f0ebdb4254e81feb726a58a38a399a95f307f656 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1665189550; 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=dLS2UP/lVj6gn4W8g10BhAsaWo2NxNxfTP28TpJPlBY=; b=bmBQOZcTW95qBn2Beua//tAKed7zL9s/NMPMIKLYufx8qMsaX727upj+96D0sZvsYbDXmD V9EI5aY3RoCxS476Sq6j060JyqFCzIqZwANCb0epntbGil76QQZ93XoeA9GNZdy7qe0np+ QM6gXtFyD0145fId1vKR9KlHhGilDHSqT5JoNA1pvT2hHeoK7Y/bVWrcAHVGob0tmU2kfz 2AkOmkg01gMNda/RGjl9fa8FbO51SSu0tBMyp7iswu3k6iMmoCg9Gz1ZbOsmfkiHD3sqo/ GhBxlZWnuBi4BQrmrv2M4Bfhu+hOD6kzUx40r/QVPTxR8PxvS9eE8npnjgAjDw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1665189550; a=rsa-sha256; cv=none; b=tHoE6GIL61tYTr+aD2EpNN7S7n0pVucqK0/ORQqa/VBsz1Dql1KmUcrsCETMBLuTg6kGh3 KIvhrScb9Yd8FvKO71eEbN1IdsicftynKtiwCx2rpCTbxhiM37HmZWOjqlTNXeAgzNAOJg zqbH+uuCKiYLIWCVQyeffJBHd1Bs+hV7wTtmnPzaNOPpjj2DYtyMxo8LllMM8fQhSKuzAB N2ToV0/wK8nN+tTJdpSYJ5+eKjlKUAjJt2ajvSn/2j2g8RTlhcMfSGs8ylqYISkIZNWsWD 9hQOelCAWLp5zJMHDhcPBR8PCALd9GHDd6cbmh/HC/29OPumAk1wDbeKCGGurA== 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=f0ebdb4254e81feb726a58a38a399a95f307f656 commit f0ebdb4254e81feb726a58a38a399a95f307f656 Author: Konstantin Belousov AuthorDate: 2022-09-18 11:46:19 +0000 Commit: Konstantin Belousov CommitDate: 2022-10-08 00:29:36 +0000 Add vn_rlimit_fsizex() and vn_rlimit_fsizex_res() PR: 164793 (cherry picked from commit 1b4b75171ee3f2213b7671878a910fd5ddb3306e) --- sys/kern/vfs_vnops.c | 92 +++++++++++++++++++++++++++++++++++++++++++++------- sys/sys/vnode.h | 3 ++ 2 files changed, 83 insertions(+), 12 deletions(-) diff --git a/sys/kern/vfs_vnops.c b/sys/kern/vfs_vnops.c index a7043ae818f5..af4320f44eb5 100644 --- a/sys/kern/vfs_vnops.c +++ b/sys/kern/vfs_vnops.c @@ -2378,39 +2378,107 @@ vn_rlimit_trunc(u_quad_t size, struct thread *td) return (EFBIG); } -int -vn_rlimit_fsize(const struct vnode *vp, const struct uio *uio, - struct thread *td) +static int +vn_rlimit_fsizex1(const struct vnode *vp, struct uio *uio, off_t maxfsz, + bool adj, struct thread *td) { off_t lim; bool ktr_write; - if (td == NULL) + if (vp->v_type != VREG) + return (0); + + /* + * Handle file system maximum file size. + */ + if (maxfsz != 0 && uio->uio_offset + uio->uio_resid > maxfsz) { + if (!adj || uio->uio_offset >= maxfsz) + return (EFBIG); + uio->uio_resid = maxfsz - uio->uio_offset; + } + + /* + * This is kernel write (e.g. vnode_pager) or accounting + * write, ignore limit. + */ + if (td == NULL || (td->td_pflags2 & TDP2_ACCT) != 0) return (0); /* - * There are conditions where the limit is to be ignored. - * However, since it is almost never reached, check it first. + * Calculate file size limit. */ ktr_write = (td->td_pflags & TDP_INKTRACE) != 0; - lim = lim_cur(td, RLIMIT_FSIZE); - if (__predict_false(ktr_write)) - lim = td->td_ktr_io_lim; + lim = __predict_false(ktr_write) ? td->td_ktr_io_lim : + lim_cur(td, RLIMIT_FSIZE); + + /* + * Is the limit reached? + */ if (__predict_true((uoff_t)uio->uio_offset + uio->uio_resid <= lim)) return (0); /* - * The limit is reached. + * Prepared filesystems can handle writes truncated to the + * file size limit. */ - if (vp->v_type != VREG || - (td->td_pflags2 & TDP2_ACCT) != 0) + if (adj && (uoff_t)uio->uio_offset < lim) { + uio->uio_resid = lim - (uoff_t)uio->uio_offset; return (0); + } if (!ktr_write || ktr_filesize_limit_signal) vn_send_sigxfsz(td->td_proc); return (EFBIG); } +/* + * Helper for VOP_WRITE() implementations, the common code to + * handle maximum supported file size on the filesystem, and + * RLIMIT_FSIZE, except for special writes from accounting subsystem + * and ktrace. + * + * For maximum file size (maxfsz argument): + * - return EFBIG if uio_offset is beyond it + * - otherwise, clamp uio_resid if write would extend file beyond maxfsz. + * + * For RLIMIT_FSIZE: + * - return EFBIG and send SIGXFSZ if uio_offset is beyond the limit + * - otherwise, clamp uio_resid if write would extend file beyond limit. + * + * If clamping occured, the adjustment for uio_resid is stored in + * *resid_adj, to be re-applied by vn_rlimit_fsizex_res() on return + * from the VOP. + */ +int +vn_rlimit_fsizex(const struct vnode *vp, struct uio *uio, off_t maxfsz, + ssize_t *resid_adj, struct thread *td) +{ + ssize_t resid_orig; + int error; + bool adj; + + resid_orig = uio->uio_resid; + adj = resid_adj != NULL; + error = vn_rlimit_fsizex1(vp, uio, maxfsz, adj, td); + if (adj) + *resid_adj = resid_orig - uio->uio_resid; + return (error); +} + +void +vn_rlimit_fsizex_res(struct uio *uio, ssize_t resid_adj) +{ + uio->uio_resid += resid_adj; +} + +int +vn_rlimit_fsize(const struct vnode *vp, const struct uio *uio, + struct thread *td) +{ + return (vn_rlimit_fsizex(vp, __DECONST(struct uio *, uio), 0, NULL, + td)); +} + int vn_chmod(struct file *fp, mode_t mode, struct ucred *active_cred, struct thread *td) diff --git a/sys/sys/vnode.h b/sys/sys/vnode.h index 599b42b98239..529c69ccf7c7 100644 --- a/sys/sys/vnode.h +++ b/sys/sys/vnode.h @@ -779,6 +779,9 @@ int vn_rdwr_inchunks(enum uio_rw rw, struct vnode *vp, void *base, int vn_read_from_obj(struct vnode *vp, struct uio *uio); int vn_rlimit_fsize(const struct vnode *vp, const struct uio *uio, struct thread *td); +int vn_rlimit_fsizex(const struct vnode *vp, struct uio *uio, + off_t maxfsz, ssize_t *resid_adj, struct thread *td); +void vn_rlimit_fsizex_res(struct uio *uio, ssize_t resid_adj); int vn_rlimit_trunc(u_quad_t size, struct thread *td); int vn_start_write(struct vnode *vp, struct mount **mpp, int flags); int vn_start_secondary_write(struct vnode *vp, struct mount **mpp,