From nobody Sat Sep 24 16:42:37 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 4MZZZ55myLz4d3V9; Sat, 24 Sep 2022 16:42:37 +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 4MZZZ54TB0z3Gc3; Sat, 24 Sep 2022 16:42:37 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1664037757; 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=GaDGjEmnjdw9VsARvRcR3kKGOUekEDl6vIYeBeG7vg4=; b=c+Pwl94DCdKFf6yv3I1mh0jzYt4N2iIN8oxWS/9t0O5lGj8sNJwrxlx8uwFxAxeSs0I8Fq m/Ynvs7YivGiWi66tT6VVQfSwrgp00iURgtB6jZjlhBKW9YNWAtoMF1vlea2knvLd76ZNg jQxrVZNYGfNySSuWo2oF/+GUNNq3mxxMwhSZHo3NINbw8ZYb1DhTMV0meBxpU8JMGg5jLQ MRO7hbDjZNVg7rvtEzRFI6bjDuFb2nz/bLPcRPE2y3fR0dkShqjMagzWdllOJHMaLI0MBT lOeiR82UrDPwYtwMz9SkeGETeMoCar4AfRDlLF8FwPK16MNcSH8TRei0RckpXA== 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 4MZZZ53WNRz17m9; Sat, 24 Sep 2022 16:42:37 +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 28OGgbbp058143; Sat, 24 Sep 2022 16:42:37 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 28OGgbq2058142; Sat, 24 Sep 2022 16:42:37 GMT (envelope-from git) Date: Sat, 24 Sep 2022 16:42:37 GMT Message-Id: <202209241642.28OGgbq2058142@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: 1b4b75171ee3 - main - 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/main X-Git-Reftype: branch X-Git-Commit: 1b4b75171ee3f2213b7671878a910fd5ddb3306e Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1664037757; 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=GaDGjEmnjdw9VsARvRcR3kKGOUekEDl6vIYeBeG7vg4=; b=C38QRvfRxdmUV5Egvs0gqP7NdHqXfqJi/40KH1NqeTiSMnBaRKfNomQ6OLrw/1telNH0xu oe8hwY6EQE4t3QR7vHvBqsh8641mgNum2IcxsWqc2IpMZTzpacxi3vSlmqnOTTL6UHBfgh 1jPHqlOWhomzQ/YuujvJoiI874OZE2qzS3UVcOoBrfrMMVGkPziaElr8XdNUkZkZkAHTUG BOQA75cDbrjgkeeCypn5n3WKBxO09zuLhzjWU/n2ZvqCirvT4shDOQ9s03Id+SI7iGdckv z8oVre2LcqgzQUI0Glezj7dYZm5ZWLIDyE1ermWr/Km7sGFKZhWZ2u2ghjdI2Q== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1664037757; a=rsa-sha256; cv=none; b=D5KhzfxUOAk07FLcybcAvp5c1hExltPWXaQGIa9MIq7QM/SoeXoYH0MOi6BZS7I2DdRmQv CtN9sWmeBHnn3UTRaaIysO30J2vUoUqRT2y4sFgahLuwNAoNCqug9qr2QugFYEgksaIsUS ZstpVllvv0CN3mch4a4OjDAO9GEFzggvSuWDtQ/UQjBoR3tNdIi8Fjb8YUhN1id26QLASj BdhIT9Kif65DGGSZ7Ven5s15rIsl/Wbis5OXVEmFI/iN41ElDGp1qeUCYG+Wj8MU5EiTBS IHJ8OxrH56GWl3JWsgS9Jdv1LV569ftr/CXi0qaqX3PaegbJMEATLo6AQqiUfg== 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=1b4b75171ee3f2213b7671878a910fd5ddb3306e commit 1b4b75171ee3f2213b7671878a910fd5ddb3306e Author: Konstantin Belousov AuthorDate: 2022-09-18 11:46:19 +0000 Commit: Konstantin Belousov CommitDate: 2022-09-24 16:41:33 +0000 Add vn_rlimit_fsizex() and vn_rlimit_fsizex_res() The vn_rlimit_fsizex() function: - checks that the write does not exceed RLIMIT_FSIZE limit and fs maximum supported file size - truncates write length if it exceeds the RLIMIT_FSIZE or max file size, but there are some bytes to write - sends SIGXFSZ if RLIMIT_FSIZE would be exceed otherwise POSIX mandates the truncated write in case when some bytes can be written but whole write request fails the RLIMIT_FSIZE check. The function is supposed to be used from VOP_WRITE()s. Due to pecularity in the VFS generic write syscall layer, uio_resid must correctly reflect the written amount (noted by markj). Provide the dual vn_rlimit_fsizex_res() function to correct uio_resid after the clamp done in vn_rlimit_fsizex() on VOP_WRITE() return. PR: 164793 Reviewed by: asomers, jah, markj Tested by: pho Sponsored by: The FreeBSD Foundation MFC after: 2 weeks Differential revision: https://reviews.freebsd.org/D36625 --- 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 6eef39a37f25..21f3f99a6741 100644 --- a/sys/kern/vfs_vnops.c +++ b/sys/kern/vfs_vnops.c @@ -2389,39 +2389,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 da17170cbb2d..63b3c23bd072 100644 --- a/sys/sys/vnode.h +++ b/sys/sys/vnode.h @@ -786,6 +786,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,