From patchwork Wed Sep 4 20:27:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13791359 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6EF6FCD4F25 for ; Wed, 4 Sep 2024 20:29:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F071C6B00E1; Wed, 4 Sep 2024 16:29:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E17286B00E5; Wed, 4 Sep 2024 16:29:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C1E986B00E6; Wed, 4 Sep 2024 16:29:25 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 99DFE6B00E1 for ; Wed, 4 Sep 2024 16:29:25 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 5BCA51A045B for ; Wed, 4 Sep 2024 20:29:25 +0000 (UTC) X-FDA: 82528195890.03.5104A76 Received: from mail-qk1-f174.google.com (mail-qk1-f174.google.com [209.85.222.174]) by imf26.hostedemail.com (Postfix) with ESMTP id 9A6DF140008 for ; Wed, 4 Sep 2024 20:29:23 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=toxicpanda-com.20230601.gappssmtp.com header.s=20230601 header.b=yOygZlHj; spf=none (imf26.hostedemail.com: domain of josef@toxicpanda.com has no SPF policy when checking 209.85.222.174) smtp.mailfrom=josef@toxicpanda.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725481715; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=03GJ1k40z8a5Psi/RqpgoqHfW5s1FJwp4eQtJBRMqtc=; b=KGoFORoDAxAekU5UUzyX91TonHSVYio/+/ILwgRdP/Led1a7emyGYl9lE4rnNla8ME7yTa ji4CVBCLM3UcuezUrfg+RkUEX7ZfxyUjSjV3pcFYhQdo5y/l6GEhlI2xyc/QYTQtTvuYFV 2ltTaGb4ZdrKumKz2TSTZ2/A2ENcI5U= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=toxicpanda-com.20230601.gappssmtp.com header.s=20230601 header.b=yOygZlHj; spf=none (imf26.hostedemail.com: domain of josef@toxicpanda.com has no SPF policy when checking 209.85.222.174) smtp.mailfrom=josef@toxicpanda.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725481715; a=rsa-sha256; cv=none; b=5him0wQn+IxtvyFqdhPVMIjvRE9cq3KvZWUvsyHz+qJ9cW+IbN4Sp71eOyB9hRGfKsuhD9 Ibb6QJxcCeGfNq7/nI/f+irfh86upO1+PNDsNdCf/vZnRMb83E+j1x4EiphkWKkR+CdF4a Sluuf5AYv9mDvOW4Ll9SnZv66aKDa6c= Received: by mail-qk1-f174.google.com with SMTP id af79cd13be357-7a80fe481a9so2348485a.0 for ; Wed, 04 Sep 2024 13:29:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1725481763; x=1726086563; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=03GJ1k40z8a5Psi/RqpgoqHfW5s1FJwp4eQtJBRMqtc=; b=yOygZlHj5dkxudXwMbspMj8aHMPB31fY9v8FdtUIghvMHm4o01ShOBS0NuH1/gHMUl w/UyE+JfKKsliduS4dNu7tkp4/CgIasodPPuvTXL7ecfZ4hJVu9+T1gUqMCfKrW4XrnW 3ijIZpPvFOjUtSxZFJW9alfxCtlPsXVBsm+YasyOjy1/FgAvYFm3wcZjMSDSfZ/x8lyE 9BVXRNExWYw03ff6vMGjaL/8ZBWfndYN9Sy8GMMhY9rKXgWJNgpkW+NhlLyXKoPepXIF 3JbM8khp1rpxJMXJ4fjh12soTSSmLvG+Q0teYVFePJF+qDiOwIHFYQwmnHwhctGGlL5E jjaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725481763; x=1726086563; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=03GJ1k40z8a5Psi/RqpgoqHfW5s1FJwp4eQtJBRMqtc=; b=knai40S2lPDe9Kw0qF6oX0wKzBfYgWb8Y7I8Zclb9CKFauLaCGKhLV6m03j7PmzLuy bT+9Jrgc2hlu9G9/J/NBZXzhkYc6AmgN7OMTOPOaAAcBM51nqcMoTJti+IxgKCdjSMQ7 O84jkhB0PpXNMooDluxFOaZihoynUrguRVP4leGpwyE3OoBafRvZE6mVfU8l4Q3WlSU0 xqn4kix7nZAg0D2ZaG9AOoSvqmHaAje5ncW+0g+iwSN6TMJFBIURZ1dtSq+hqg98YpYi AcMOIjxZ3bekYT3xP6sFRHAPEdNhb5Cle82zDDK5ywEQUKZVm//b9i25Tx/Z8D4g61PK dlNw== X-Forwarded-Encrypted: i=1; AJvYcCU/tepGpDI5cS9a8CrMU/fBcbp8R05qzPI3RdmzL/qrAFyhb6GvvM9iroLmmZH0xiwSIwo5z7HJcg==@kvack.org X-Gm-Message-State: AOJu0Yy5fDdkZIj1+xGplJTR56TOjryyrq/GNxi3Zknl5VTo/Hy8+Vd4 VXn3rSjqT8+DlqO5yUuStXPT8VsbsrmbhUkubR9FNZA1nccVg3wXdsdwrKLHrIs= X-Google-Smtp-Source: AGHT+IGvPgs+n6cvUE85SbneJf41dyWf81pG4sPn/0S6fFjWTLzcLSXA4cf6j/r1vB6Kg6DKnHidAQ== X-Received: by 2002:a05:620a:44ca:b0:79c:b8c:8e2b with SMTP id af79cd13be357-7a81d641ed8mr2000168185a.3.1725481762573; Wed, 04 Sep 2024 13:29:22 -0700 (PDT) Received: from localhost (syn-076-182-020-124.res.spectrum.com. [76.182.20.124]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7a98efeaffdsm15457285a.83.2024.09.04.13.29.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2024 13:29:22 -0700 (PDT) From: Josef Bacik To: kernel-team@fb.com, linux-fsdevel@vger.kernel.org, jack@suse.cz, amir73il@gmail.com, brauner@kernel.org, linux-xfs@vger.kernel.org, linux-bcachefs@vger.kernel.org, linux-btrfs@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v5 05/18] fanotify: introduce FAN_PRE_MODIFY permission event Date: Wed, 4 Sep 2024 16:27:55 -0400 Message-ID: <5142d1715dfecf58bc0a77eb410ca21d95e71cfc.1725481503.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 9A6DF140008 X-Stat-Signature: snctngxmk435fc6c7w3nrgf71gahfpex X-HE-Tag: 1725481763-873972 X-HE-Meta: U2FsdGVkX1+raCm7Mk8AuyTUswo5EiACGYiErGpQnv5n3YbG2/x5k9OeLF8KB/pdlTvvtUEPvjoZYtF8Du+TdOJwfD5DBMikztJgCyRjpt4KRXd6+nFYyRroItXx89qk7q5NDoBrLgG9Nzmb44c0uEOjRn+HsXwEJxTj35sVXr1h72UUIZL6wsHVtKy/P2J7w/H4881DL6+cF518ggprRW8K5vlds9ONpBbNiGXP+0DzzhMGBElAn0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ovNhOMjNUWQYIojvOzp+q8IIw3Dd/Atu3pO0wZ1tCIKXKHLI+BM1Bat5EAA54A/bG/miUQxdrrg2OK6zER85Rbw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000795, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Amir Goldstein Generate FAN_PRE_MODIFY permission event from fsnotify_file_perm() pre-write hook to notify fanotify listeners on an intent to make modification to a file. Like FAN_PRE_ACCESS, it is only allowed with FAN_CLASS_PRE_CONTENT and unlike FAN_MODIFY, it is only allowed on regular files. Like FAN_PRE_ACCESS, it is generated without sb_start_write() held, so it is safe to perform filesystem modifications in the context of event handler. This pre-content event is meant to be used by hierarchical storage managers that want to fill the content of files on first write access. Signed-off-by: Amir Goldstein Reviewed-by: Christian Brauner --- fs/notify/fanotify/fanotify.c | 3 ++- fs/notify/fanotify/fanotify_user.c | 2 ++ include/linux/fanotify.h | 3 ++- include/uapi/linux/fanotify.h | 1 + 4 files changed, 7 insertions(+), 2 deletions(-) diff --git a/fs/notify/fanotify/fanotify.c b/fs/notify/fanotify/fanotify.c index 7dac8e4486df..b163594843f5 100644 --- a/fs/notify/fanotify/fanotify.c +++ b/fs/notify/fanotify/fanotify.c @@ -911,8 +911,9 @@ static int fanotify_handle_event(struct fsnotify_group *group, u32 mask, BUILD_BUG_ON(FAN_FS_ERROR != FS_ERROR); BUILD_BUG_ON(FAN_RENAME != FS_RENAME); BUILD_BUG_ON(FAN_PRE_ACCESS != FS_PRE_ACCESS); + BUILD_BUG_ON(FAN_PRE_MODIFY != FS_PRE_MODIFY); - BUILD_BUG_ON(HWEIGHT32(ALL_FANOTIFY_EVENT_BITS) != 22); + BUILD_BUG_ON(HWEIGHT32(ALL_FANOTIFY_EVENT_BITS) != 23); mask = fanotify_group_event_mask(group, iter_info, &match_mask, mask, data, data_type, dir); diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c index c294849e474f..3a7101544f30 100644 --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c @@ -1673,6 +1673,8 @@ static int fanotify_events_supported(struct fsnotify_group *group, if (mask & FANOTIFY_PRE_CONTENT_EVENTS) { if (!is_dir && !d_is_reg(path->dentry)) return -EINVAL; + if (is_dir && mask & FAN_PRE_MODIFY) + return -EISDIR; } return 0; diff --git a/include/linux/fanotify.h b/include/linux/fanotify.h index 5c811baf44d2..ae6cb2688d52 100644 --- a/include/linux/fanotify.h +++ b/include/linux/fanotify.h @@ -92,7 +92,8 @@ #define FANOTIFY_CONTENT_PERM_EVENTS (FAN_OPEN_PERM | FAN_OPEN_EXEC_PERM | \ FAN_ACCESS_PERM) /* Pre-content events can be used to fill file content */ -#define FANOTIFY_PRE_CONTENT_EVENTS (FAN_PRE_ACCESS) +#define FANOTIFY_PRE_CONTENT_EVENTS (FAN_PRE_ACCESS | FAN_PRE_MODIFY) +#define FANOTIFY_PRE_MODIFY_EVENTS (FAN_PRE_MODIFY) /* Events that require a permission response from user */ #define FANOTIFY_PERM_EVENTS (FANOTIFY_CONTENT_PERM_EVENTS | \ diff --git a/include/uapi/linux/fanotify.h b/include/uapi/linux/fanotify.h index bcada21a3a2e..ac00fad66416 100644 --- a/include/uapi/linux/fanotify.h +++ b/include/uapi/linux/fanotify.h @@ -27,6 +27,7 @@ #define FAN_OPEN_EXEC_PERM 0x00040000 /* File open/exec in perm check */ #define FAN_PRE_ACCESS 0x00080000 /* Pre-content access hook */ +#define FAN_PRE_MODIFY 0x00100000 /* Pre-content modify hook */ #define FAN_EVENT_ON_CHILD 0x08000000 /* Interested in child events */