From patchwork Wed Sep 4 20:27:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13791336 Received: from mail-qt1-f178.google.com (mail-qt1-f178.google.com [209.85.160.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D490214831E for ; Wed, 4 Sep 2024 20:29:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725481758; cv=none; b=ceatiljqH32XVsS6F+fZaWshEFMnV93XGisci9j/rZqNje6P1+kB+/ZFL94ZBgn7CHr0mkXOXLBjTBv4laCnomTjLTV3xKVisqQ56PqSL01MSqQ1KuZtMvPweZa+xFn19biISFZG1AmXXBEbr+ByycN7CbIJXbboJZY1H08tPa4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725481758; c=relaxed/simple; bh=2T/bTyebmmWzgd79hiDdf0J2vl5OhNotJm/x2K/voWI=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=cwRBZb0FTpcwFCV1PP+NxxO3D17vy9xTyFobYn2o6ye1CrV6H9FPAoFpA4Gt5+184o3QmGq3B+DHewwh4M4NOjMMV7FoE4FYvgt32pfxM2kM6yOR8nqe5hj1U09bKwp9JQGDepu7GhL7aLNDRLxYB7ISPnki83YZ0PJxyxxRW+U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com; spf=none smtp.mailfrom=toxicpanda.com; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b=QGKgH9cy; arc=none smtp.client-ip=209.85.160.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b="QGKgH9cy" Received: by mail-qt1-f178.google.com with SMTP id d75a77b69052e-4568780a168so364461cf.0 for ; Wed, 04 Sep 2024 13:29:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1725481756; x=1726086556; darn=vger.kernel.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=rE3z53SxlgArsKnVp16449O9VmLlGGSseshi4KP5xk8=; b=QGKgH9cyqIb53RGflzg5ytpxJ/TdVSrfwsFChKeYhwCmjjeqWZXzJdNlbkFBwgHa4I tc9WTa1npQFptdXkziuE2qMmBd3m9KUqhKgCz3kto91ROnBC4aO7qnYJnv+WkzfxAsaz BYCf9zxzZ2+QRyGj6JrcG0k51R+A3UWulme+0bPhy+lLU8snL4HqHNppdydDuO0GLNGf J+znv50aoz9ecDGP0zeuvQemz5dFIenkv4Jqq7UmdP1vZFR8jTPgGY+n7nmJdw0wsLNj VjzzP8+eGpEGhQM29JOJ7FC3HpQf1DThSteMOfTVRTJv7fNj9vXcidEBq6DbkhceDUoN 8gEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725481756; x=1726086556; 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=rE3z53SxlgArsKnVp16449O9VmLlGGSseshi4KP5xk8=; b=Ck1fVrqwdhCQwDzMCpqJreWZlcpSGsJdtvc8h/FoJY0T9PhIZkfEbu09FLFZ64aouv mbChBhEpTHNyVTHV5vGLuWYVaHOP2e86BRmxTsPxCTMUef+8oa81uFGM1M4jieVXkxZk yBBJh1xyoTKGxFP7OyOV5fS66SgX79dGFMxRV8/wrcmFx+BK0C2icgSK09/7rwcZPt6G MJDy+dp6ZTrqJ5LBrBa9sJJI3xts1OM2VTYDkhgyexeXTfErIpdAOddjC7YRnhTW2Rci SShHqsq6323buDYwCEDPFFKUJa+LNsWhsoWwx+v/HgY7g4TIwI4q8BWlbLpslkF+7YxV /lwQ== X-Forwarded-Encrypted: i=1; AJvYcCXJI0Vn0t88GS7lGONLg8U/9LtXChdJM7Y5hEwsOANcxD5YNjLrcsloTBbq5FSlazB6ElX75xc+sJqlFENM@vger.kernel.org X-Gm-Message-State: AOJu0YyrrIa4cK23/BRfhUYiOfm8CWXguxlesmVYeTHin1BK6IiK0KG7 97Wuj7mDcsbFO9/ah/6n9zTciWnp3I9YsNjpvD/WDkMgy6h3uvQiEPfmvpTUE34= X-Google-Smtp-Source: AGHT+IHwW5fXaHWnF6X5TRV/cQ4dUOF+w2NPeKDvoOEkYHuq6qKl8uAhehzsgXTy2K4uj8CzjjebGQ== X-Received: by 2002:a05:622a:2292:b0:456:7877:1e91 with SMTP id d75a77b69052e-456965f44ebmr193409851cf.27.1725481755768; Wed, 04 Sep 2024 13:29:15 -0700 (PDT) Received: from localhost (syn-076-182-020-124.res.spectrum.com. [76.182.20.124]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-45801b52ea3sm1489771cf.53.2024.09.04.13.29.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2024 13:29:15 -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 01/18] fanotify: don't skip extra event info if no info_mode is set Date: Wed, 4 Sep 2024 16:27:51 -0400 Message-ID: <6a659625a0d08fae894cc47352453a6be2579788.1725481503.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 New pre-content events will be path events but they will also carry additional range information. Remove the optimization to skip checking whether info structures need to be generated for path events. This results in no change in generated info structures for existing events. Signed-off-by: Josef Bacik Reviewed-by: Amir Goldstein --- fs/notify/fanotify/fanotify_user.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c index 9ec313e9f6e1..2e2fba8a9d20 100644 --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c @@ -160,9 +160,6 @@ static size_t fanotify_event_len(unsigned int info_mode, int fh_len; int dot_len = 0; - if (!info_mode) - return event_len; - if (fanotify_is_error_event(event->mask)) event_len += FANOTIFY_ERROR_INFO_LEN; @@ -740,12 +737,10 @@ static ssize_t copy_event_to_user(struct fsnotify_group *group, if (fanotify_is_perm_event(event->mask)) FANOTIFY_PERM(event)->fd = fd; - if (info_mode) { - ret = copy_info_records_to_user(event, info, info_mode, pidfd, - buf, count); - if (ret < 0) - goto out_close_fd; - } + ret = copy_info_records_to_user(event, info, info_mode, pidfd, + buf, count); + if (ret < 0) + goto out_close_fd; if (f) fd_install(fd, f); From patchwork Wed Sep 4 20:27:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13791337 Received: from mail-vs1-f54.google.com (mail-vs1-f54.google.com [209.85.217.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AFCAA149DFF for ; Wed, 4 Sep 2024 20:29:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.217.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725481760; cv=none; b=HJvukpFfd73L5jLYmSJ4iZdQU/RdSOI8zJ/LCzKMtwXLr33XD06nL5X2XvZF1ks89D/0rykWalyftPknvW+0lEMRYkfcH7sBCxKk3FxuZClpEkE+cL/QdRpHsok8tEtvpsObyCNG67YgqhpCG/HAilMSKKVbw+QVll3RyRRYXc8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725481760; c=relaxed/simple; bh=Xn48LHyloGlt4wSB8hOBR2UrfV1ArFm69qZkTTQGfyU=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=EUAxzx/yGMNsh9b4GmasXd4TSer0+T3PLq8cD03qgXicBIJTOy3eaO2Kk/5Np47zBicE6yfC4jgH+9fnb41+mfbK770ajshnNrUG3FoMsq2t4eYwU9A8dsHkGgNNtCMq+8wpmzcIUZs7TB+vazQgW9BVA9LswWmpcJtCrgqGJrw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com; spf=none smtp.mailfrom=toxicpanda.com; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b=c9b5TyRY; arc=none smtp.client-ip=209.85.217.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b="c9b5TyRY" Received: by mail-vs1-f54.google.com with SMTP id ada2fe7eead31-49bc42bec6dso289364137.0 for ; Wed, 04 Sep 2024 13:29:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1725481757; x=1726086557; darn=vger.kernel.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=G5YjJnCjztcen/Z+X5lHX0n7xhNQ/QripYlvqYqvblA=; b=c9b5TyRY4mkxF15us7zoWMx4fOYKG7wCeHZWdlUSoiFqod73427S7sVCK03G/Bx2Gx KkxnP+FzN96Vo6dMexkii/9tFzgkv9R7lgxFfqVNWdlPW5AI+SG4EQPHa06Z7IMOfccH 4uIemTuV7zHVw8fwb/+1s/Ash9WswS/5bOhB7f4u7Z4Wy0hxRdYl0YgJxXQ6Qy9id6io KdyNwjgYQURhmc1QTaRC872P1Wsbvkk6sv2IHZj/GccJp/G5q54xVQM+g7qdYAqIYu5O xqrNEhkhFrUUHKK0NJN/onWDi+jUZPclYYwqnSFdjafrsfZYtCaTXXmqUOu9ZfTsI3b1 Flbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725481757; x=1726086557; 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=G5YjJnCjztcen/Z+X5lHX0n7xhNQ/QripYlvqYqvblA=; b=P1dHFDMqWnDYGSDu1U07bHyotJrxbfNaVlfIhdtl0XeO4p+8KsUuqU0nUiqpKYI6kN zInwcYUpIhk/YafhNaCFdiKQIfCd6RmCgHy8L/To9gnL/SF4z8VyhQfdHIqmkxXYq6Z0 GJ8Ji8z32O9aSI5uley1BfH6IhJOyerK0OKkDJ/aLs1Sj8RFT+nw9JHbA1fLTk6bcWeU TZIeua88PbyJEw+w3moVsZSvt0qgiQYtijSx8DZhlpLEVyOj+1SBgy16tqzbkcorPLRg +2gAvncR4z/UxhZf0whhrjZuKa3HxMmcSkRaAKjqgJPV+LgNEp40vvI8whrgfFDYI6d+ 1nCw== X-Forwarded-Encrypted: i=1; AJvYcCW0ROUAVmaYu6EbucKJahKzTObsrq3s8pVxCpdYkxVJpGtoVowopKYqY3H9vE0XXNXy+V0isQ3GO5GLqRVw@vger.kernel.org X-Gm-Message-State: AOJu0Yx0yQSugkp5wY6IrG62GP2tbm+RWzep5ZtW6bnm+KsLu5viIXS/ XyMhB4kl8WchV28kr8fV2S1h9nmvyI5XHRxrUMeFfVjTOqXt/r+L2lgFNtcZPdg= X-Google-Smtp-Source: AGHT+IHvcict8JxrlQWrQU5plI6x5zmPl5v4oP6wc1pvF95llWkCs8Ce7IJ9EEARO1WLH0fb9o1Srw== X-Received: by 2002:a05:6102:c91:b0:498:d39d:9b7c with SMTP id ada2fe7eead31-49a77940af7mr14221150137.2.1725481757506; Wed, 04 Sep 2024 13:29:17 -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-7a98f00acf7sm15095385a.107.2024.09.04.13.29.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2024 13:29:16 -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 02/18] fsnotify: introduce pre-content permission event Date: Wed, 4 Sep 2024 16:27:52 -0400 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Amir Goldstein The new FS_PRE_ACCESS permission event is similar to FS_ACCESS_PERM, but it meant for a different use case of filling file content before access to a file range, so it has slightly different semantics. Generate FS_PRE_ACCESS/FS_ACCESS_PERM as two seperate events, same as we did for FS_OPEN_PERM/FS_OPEN_EXEC_PERM. FS_PRE_MODIFY is a new permission event, with similar semantics as FS_PRE_ACCESS, which is called before a file is modified. FS_ACCESS_PERM is reported also on blockdev and pipes, but the new pre-content events are only reported for regular files and dirs. The pre-content events are meant to be used by hierarchical storage managers that want to fill the content of files on first access. Signed-off-by: Amir Goldstein --- fs/notify/fsnotify.c | 2 +- include/linux/fsnotify.h | 27 ++++++++++++++++++++++++--- include/linux/fsnotify_backend.h | 13 +++++++++++-- security/selinux/hooks.c | 3 ++- 4 files changed, 38 insertions(+), 7 deletions(-) diff --git a/fs/notify/fsnotify.c b/fs/notify/fsnotify.c index 272c8a1dab3c..1ca4a8da7f29 100644 --- a/fs/notify/fsnotify.c +++ b/fs/notify/fsnotify.c @@ -621,7 +621,7 @@ static __init int fsnotify_init(void) { int ret; - BUILD_BUG_ON(HWEIGHT32(ALL_FSNOTIFY_BITS) != 23); + BUILD_BUG_ON(HWEIGHT32(ALL_FSNOTIFY_BITS) != 25); ret = init_srcu_struct(&fsnotify_mark_srcu); if (ret) diff --git a/include/linux/fsnotify.h b/include/linux/fsnotify.h index 278620e063ab..7600a0c045ba 100644 --- a/include/linux/fsnotify.h +++ b/include/linux/fsnotify.h @@ -133,12 +133,13 @@ static inline int fsnotify_file(struct file *file, __u32 mask) #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS /* - * fsnotify_file_area_perm - permission hook before access to file range + * fsnotify_file_area_perm - permission hook before access/modify of file range */ static inline int fsnotify_file_area_perm(struct file *file, int perm_mask, const loff_t *ppos, size_t count) { - __u32 fsnotify_mask = FS_ACCESS_PERM; + struct inode *inode = file_inode(file); + __u32 fsnotify_mask; /* * filesystem may be modified in the context of permission events @@ -147,7 +148,27 @@ static inline int fsnotify_file_area_perm(struct file *file, int perm_mask, */ lockdep_assert_once(file_write_not_started(file)); - if (!(perm_mask & MAY_READ)) + /* + * Generate FS_PRE_ACCESS/FS_ACCESS_PERM as two seperate events. + */ + if (perm_mask & MAY_READ) { + int ret = fsnotify_file(file, FS_ACCESS_PERM); + + if (ret) + return ret; + } + + /* + * Pre-content events are only reported for regular files and dirs. + */ + if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)) + return 0; + + if (perm_mask & MAY_WRITE) + fsnotify_mask = FS_PRE_MODIFY; + else if (perm_mask & (MAY_READ | MAY_ACCESS)) + fsnotify_mask = FS_PRE_ACCESS; + else return 0; return fsnotify_file(file, fsnotify_mask); diff --git a/include/linux/fsnotify_backend.h b/include/linux/fsnotify_backend.h index 8be029bc50b1..200a5e3b1cd4 100644 --- a/include/linux/fsnotify_backend.h +++ b/include/linux/fsnotify_backend.h @@ -56,6 +56,9 @@ #define FS_ACCESS_PERM 0x00020000 /* access event in a permissions hook */ #define FS_OPEN_EXEC_PERM 0x00040000 /* open/exec event in a permission hook */ +#define FS_PRE_ACCESS 0x00080000 /* Pre-content access hook */ +#define FS_PRE_MODIFY 0x00100000 /* Pre-content modify hook */ + /* * Set on inode mark that cares about things that happen to its children. * Always set for dnotify and inotify. @@ -77,8 +80,14 @@ */ #define ALL_FSNOTIFY_DIRENT_EVENTS (FS_CREATE | FS_DELETE | FS_MOVE | FS_RENAME) -#define ALL_FSNOTIFY_PERM_EVENTS (FS_OPEN_PERM | FS_ACCESS_PERM | \ - FS_OPEN_EXEC_PERM) +/* Content events can be used to inspect file content */ +#define FSNOTIFY_CONTENT_PERM_EVENTS (FS_OPEN_PERM | FS_OPEN_EXEC_PERM | \ + FS_ACCESS_PERM) +/* Pre-content events can be used to fill file content */ +#define FSNOTIFY_PRE_CONTENT_EVENTS (FS_PRE_ACCESS | FS_PRE_MODIFY) + +#define ALL_FSNOTIFY_PERM_EVENTS (FSNOTIFY_CONTENT_PERM_EVENTS | \ + FSNOTIFY_PRE_CONTENT_EVENTS) /* * This is a list of all events that may get sent to a parent that is watching diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c index 55c78c318ccd..2997edf3e7cd 100644 --- a/security/selinux/hooks.c +++ b/security/selinux/hooks.c @@ -3406,7 +3406,8 @@ static int selinux_path_notify(const struct path *path, u64 mask, perm |= FILE__WATCH_WITH_PERM; /* watches on read-like events need the file:watch_reads permission */ - if (mask & (FS_ACCESS | FS_ACCESS_PERM | FS_CLOSE_NOWRITE)) + if (mask & (FS_ACCESS | FS_ACCESS_PERM | FS_PRE_ACCESS | + FS_CLOSE_NOWRITE)) perm |= FILE__WATCH_READS; return path_has_perm(current_cred(), path, perm); From patchwork Wed Sep 4 20:27:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13791338 Received: from mail-oi1-f179.google.com (mail-oi1-f179.google.com [209.85.167.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 09EDB14D290 for ; Wed, 4 Sep 2024 20:29:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725481761; cv=none; b=tsegI4wuAWVfCuidsi+9vCXtLNLxGqyGmwepjmoHmS5Pqaifvcl5YHHYRRdMHIN1mxkT6OMDdVD7D7uRAc9W4FU23ZWRxjJItNx9ehYZu2xavcridzk7HMczG10ZOPvbDTkG63Dco7+vC0dZsYWxIOH+eHcCAlPiA20+4MGIkfI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725481761; c=relaxed/simple; bh=KQKrHptqPFx5UnDyLAwlPOx0/0+4MigGJ4Sd0QKK9Tg=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=d76V/BdHKvwee99ShDyyia3q8Bev+ltsakjmzlOau7SulYx+gf8Q9pEWr4kfJAmQ978KOPLd8TSO8MboaH9UPXY+YMwS8EQ8xJje7Fjj/8mNhFOlNywuhJXozvbgA95ZP0YZVvKGQOJSXRCz8GTPXrl37m0sxpCbjydfgWm+Q34= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com; spf=none smtp.mailfrom=toxicpanda.com; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b=fCGQpOzj; arc=none smtp.client-ip=209.85.167.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b="fCGQpOzj" Received: by mail-oi1-f179.google.com with SMTP id 5614622812f47-3df0dc53ec1so4229992b6e.1 for ; Wed, 04 Sep 2024 13:29:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1725481759; x=1726086559; darn=vger.kernel.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=jN13mVwJhuqjX4PmJ7GJ6BtR6lc23krv9oFHMk0R8ZY=; b=fCGQpOzj67BHKa7j5uyX2rMTxrVeys73ZNv9aIAgg73rj5vUzIMqFdhRPR+vRsak8Q ZBrO6q4GpKZX33YN3i9/9w3kD+TcCjN8sNhrFhy25aG6O7Ma55B5eoUQ4AKDNKf4xo/R duNpQlrU5IqX+posXKdYIRqrsg0YZNCeDXZr6uCvtFuSDuDQ+XvamEfpjk148Ho1/EYY UeRpT8N1B+8XOJW/zck9aVuz/qKSRkO09FxhELZZToNlVOIjAO8IPyO2vM31CjT2VXg4 NIoYE0qnEr7v10XHMPmmi+THB7H0BVUUeVmDKn8WLvm+RXWfGaSGPzdrcYiUBRURQf+K pCfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725481759; x=1726086559; 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=jN13mVwJhuqjX4PmJ7GJ6BtR6lc23krv9oFHMk0R8ZY=; b=gJ0ekBdv8ChQgogdpnYmKu5t/1A+rOHUON+KPUNdG0ayCkLM4JlSjEDsDIj53wQ0dv At+y0NNq631B4u3nRemI9+D9xarM7lnltWKrhMSN9Y4iaySzdEfAkLj/2ICc6eoAj04d m0RHN4WB0XMtUGMH4qadarn7Vq/FAqsNoNFrOCoK0rwlbz7Grm59mfK3L+CnYwnoKXw8 y9FJjrl0JPPMm+yG2/bWYcXWtGaDxNHPWeqwmi+5n5Q7wz2atp0KXBPxlucEHi22D52V G39fwFOgIfQ2CDIN9oukYJEASMR/p/eHg5MOu2N7UpZGX3zW8OdX1qmI6zswu0Ql3MmQ 5Wsw== X-Forwarded-Encrypted: i=1; AJvYcCVV/Rk7EEPKpcU9gzh3l6nnYT9GAb/Xlu18BPCaXUxfJlHNd1yLWrJuIKIcXxNPllDqME/QrPjrWpVPMiV5@vger.kernel.org X-Gm-Message-State: AOJu0Yxb3hUyuIu0tr9dxdR+qT5my8EH7JcEIga39Y0gALtYmxL3N++J virOOBUu99kun7yUxKjlphTZ6GS94bGYiQZSbk9YPo7/h85K/QSx0XAEz62WazI= X-Google-Smtp-Source: AGHT+IG+yDupiC0Kppt9fCxGX8WKPY89paC515rZx6+FiPwoVSHDWV3mC1QVmv+EWb3aMkbVDFXGwg== X-Received: by 2002:a05:6808:3c46:b0:3e0:70a:3abb with SMTP id 5614622812f47-3e0070a4179mr12332118b6e.11.1725481759225; Wed, 04 Sep 2024 13:29:19 -0700 (PDT) Received: from localhost (syn-076-182-020-124.res.spectrum.com. [76.182.20.124]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-45801b3277esm1506081cf.31.2024.09.04.13.29.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2024 13:29:18 -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 03/18] fsnotify: generate pre-content permission event on open Date: Wed, 4 Sep 2024 16:27:53 -0400 Message-ID: <4b235bf62c99f1f1196edc9da4258167314dc3c3.1725481503.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Amir Goldstein FS_PRE_ACCESS or FS_PRE_MODIFY will be generated on open depending on file open mode. The pre-content event will be generated in addition to FS_OPEN_PERM, but without sb_writers held and after file was truncated in case file was opened with O_CREAT and/or O_TRUNC. The event will have a range info of (0..0) to provide an opportunity to fill entire file content on open. Signed-off-by: Amir Goldstein Reviewed-by: Christian Brauner --- fs/namei.c | 9 +++++++++ include/linux/fsnotify.h | 10 +++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/fs/namei.c b/fs/namei.c index 3a4c40e12f78..c16487e3742d 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -3735,6 +3735,15 @@ static int do_open(struct nameidata *nd, } if (do_truncate) mnt_drop_write(nd->path.mnt); + + /* + * This permission hook is different than fsnotify_open_perm() hook. + * This is a pre-content hook that is called without sb_writers held + * and after the file was truncated. + */ + if (!error) + error = fsnotify_file_perm(file, MAY_OPEN); + return error; } diff --git a/include/linux/fsnotify.h b/include/linux/fsnotify.h index 7600a0c045ba..fb3837b8de4c 100644 --- a/include/linux/fsnotify.h +++ b/include/linux/fsnotify.h @@ -168,6 +168,10 @@ static inline int fsnotify_file_area_perm(struct file *file, int perm_mask, fsnotify_mask = FS_PRE_MODIFY; else if (perm_mask & (MAY_READ | MAY_ACCESS)) fsnotify_mask = FS_PRE_ACCESS; + else if (perm_mask & MAY_OPEN && file->f_mode & FMODE_WRITER) + fsnotify_mask = FS_PRE_MODIFY; + else if (perm_mask & MAY_OPEN) + fsnotify_mask = FS_PRE_ACCESS; else return 0; @@ -176,10 +180,14 @@ static inline int fsnotify_file_area_perm(struct file *file, int perm_mask, /* * fsnotify_file_perm - permission hook before file access + * + * Called from read()/write() with perm_mask MAY_READ/MAY_WRITE. + * Called from open() with MAY_OPEN without sb_writers held and after the file + * was truncated. Note that this is a different event from fsnotify_open_perm(). */ static inline int fsnotify_file_perm(struct file *file, int perm_mask) { - return fsnotify_file_area_perm(file, perm_mask, NULL, 0); + return fsnotify_file_area_perm(file, perm_mask, &file->f_pos, 0); } /* From patchwork Wed Sep 4 20:27:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13791339 Received: from mail-qk1-f175.google.com (mail-qk1-f175.google.com [209.85.222.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ECDB91527B1 for ; Wed, 4 Sep 2024 20:29:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725481763; cv=none; b=lXouh3Urnj1yKOIW4nUWsSldDBMaAwscUPq0HU+ZcQrS9K42+64H9WguiCZXBkBdMpznjF9Ahcrq/M7WLFI22IR5Q1wMuuV8qlKPK0SP3m+GhkDqAZrgRnOxkafgtc92Ehy5LG7SX5+vs/ifIMCKFoKwP3eoamxOywbGjiscFr8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725481763; c=relaxed/simple; bh=205ZgkeQOGfC9lat+UQrPYYg3l/LDds2VjsCRxYzGRM=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lB94eYuqi9L9NTLuk3Y38chZLb9DRtzIVH7gYUhJrLLIcW4s8xQK4LDlgcbRmkgnG6aPmMvIL4fQZ59oEJV2FJM3jdA8TUVmPlHdM7IXTjh1OJlGrKLzhJxYnYDD0GVuQ63/Rk4XWxApw/6B/vw5l7i69gDtQx9piB7aRPoTeJE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com; spf=none smtp.mailfrom=toxicpanda.com; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b=lfKFC/jU; arc=none smtp.client-ip=209.85.222.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b="lfKFC/jU" Received: by mail-qk1-f175.google.com with SMTP id af79cd13be357-7a9786c5ec2so680185a.2 for ; Wed, 04 Sep 2024 13:29:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1725481761; x=1726086561; darn=vger.kernel.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=wY5GQe96iyZHYx9I8f9pL6l7vs2y5u4HNtMRHVuMKgs=; b=lfKFC/jUrrTJNEGAGfnQm5IY4zkD4LojZyGj1C9isIkX135gfPRh0LZgCBMUbqnZH3 1hkLFS/DYPU6cSaSuKg0huC1j8by3rPoWrnVhHBjkv4c8AswN2HPVUt/zhirxwZbvA+h zL2ReZTGs6u6V0OZrKCi5nvVaH7HNviohxlrslbvjHBxJKX7J3l2iz5DT0bC9CjmVUIY J0d2xHthAsEBYXa+Vueo4u1F6e+DaC51xfUeVhIUWqVe572h+WHs2BKz0vJ2RGBveNdW kkpb44vMvugiO1yS+HFkuw0KNeSl3c5u8P6pAhfZ+yp/0qZm+eM5XSj95MKV2l6d4BPY KduQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725481761; x=1726086561; 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=wY5GQe96iyZHYx9I8f9pL6l7vs2y5u4HNtMRHVuMKgs=; b=G2oI0wd1ZVQXM+GBeQSyEoNpCMgs/o5+0Vm2eHlDCRy1PS7otydfwHcAGdFt1pvo/P xXHjlXNfUhOmYXT3FniYLZeUsh7NQ5Wp2C5ABL0euerdACWjPHdsHH+N1a3HzDgqVbpR HV9q9nRL21KKjxfXwv9bqNX4JAJwPKoymEszQdm1e9yEl95cHORnWCO9Ia1rrZ2AwLdd 32rBqGYPQ2fBtQSBpDx5A5NWqXVUl9961CLKDWJzKHMrGqMghYJKDZWuurKDqZOAuK6E +KEXUYYUugT4xc5nCp2ulYnfHHbQ4d5EGPqgJJb8vUwVPkMOpXuqpblfMxSk8Bncednm 9kHw== X-Forwarded-Encrypted: i=1; AJvYcCULofKt4PHCEuqkp8WsUDsXe6uTOLx/rFKkxmJlWQYOTCN5zAsNz6tP3gfpDaWZ0kczxD/APtfeMPI34siQ@vger.kernel.org X-Gm-Message-State: AOJu0Yz0A1m0GfexxGd2wLrX9jJ2Kr2xo6u3IjCIFdPOgi6s4aM0Bc+J hzZ+hKoBJ48US4nRoytKi4jxBmhl5zlwxQmoLGJSu7yyUM6436GgPYPutqGDJcI= X-Google-Smtp-Source: AGHT+IHn2YN4Yn9s+Pe3cKQRQlM1ZQppCBWH1jZTlFjC6uGrUZZrr+lLwvttH1GqC8If9lYaSoNH2g== X-Received: by 2002:a05:6214:3a89:b0:6c3:626f:2366 with SMTP id 6a1803df08f44-6c3626f243amr168729666d6.34.1725481760916; Wed, 04 Sep 2024 13:29:20 -0700 (PDT) Received: from localhost (syn-076-182-020-124.res.spectrum.com. [76.182.20.124]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6c520315369sm1569046d6.96.2024.09.04.13.29.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2024 13:29:20 -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 04/18] fanotify: introduce FAN_PRE_ACCESS permission event Date: Wed, 4 Sep 2024 16:27:54 -0400 Message-ID: <19cb8b3b5b93760bc6c6d47a8cbcd277952d7006.1725481503.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Amir Goldstein Similar to FAN_ACCESS_PERM permission event, but it is only allowed with class FAN_CLASS_PRE_CONTENT and only allowed on regular files and dirs. Unlike FAN_ACCESS_PERM, it is safe to write to the file being accessed in the context of the 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 read access. Signed-off-by: Amir Goldstein Reviewed-by: Christian Brauner --- fs/notify/fanotify/fanotify.c | 3 ++- fs/notify/fanotify/fanotify_user.c | 17 ++++++++++++++--- include/linux/fanotify.h | 14 ++++++++++---- include/uapi/linux/fanotify.h | 2 ++ 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/fs/notify/fanotify/fanotify.c b/fs/notify/fanotify/fanotify.c index 224bccaab4cc..7dac8e4486df 100644 --- a/fs/notify/fanotify/fanotify.c +++ b/fs/notify/fanotify/fanotify.c @@ -910,8 +910,9 @@ static int fanotify_handle_event(struct fsnotify_group *group, u32 mask, BUILD_BUG_ON(FAN_OPEN_EXEC_PERM != FS_OPEN_EXEC_PERM); 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(HWEIGHT32(ALL_FANOTIFY_EVENT_BITS) != 21); + BUILD_BUG_ON(HWEIGHT32(ALL_FANOTIFY_EVENT_BITS) != 22); 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 2e2fba8a9d20..c294849e474f 100644 --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c @@ -1628,6 +1628,7 @@ static int fanotify_events_supported(struct fsnotify_group *group, unsigned int flags) { unsigned int mark_type = flags & FANOTIFY_MARK_TYPE_BITS; + bool is_dir = d_is_dir(path->dentry); /* Strict validation of events in non-dir inode mask with v5.17+ APIs */ bool strict_dir_events = FAN_GROUP_FLAG(group, FAN_REPORT_TARGET_FID) || (mask & FAN_RENAME) || @@ -1665,9 +1666,15 @@ static int fanotify_events_supported(struct fsnotify_group *group, * but because we always allowed it, error only when using new APIs. */ if (strict_dir_events && mark_type == FAN_MARK_INODE && - !d_is_dir(path->dentry) && (mask & FANOTIFY_DIRONLY_EVENT_BITS)) + !is_dir && (mask & FANOTIFY_DIRONLY_EVENT_BITS)) return -ENOTDIR; + /* Pre-content events are only supported on regular files and dirs */ + if (mask & FANOTIFY_PRE_CONTENT_EVENTS) { + if (!is_dir && !d_is_reg(path->dentry)) + return -EINVAL; + } + return 0; } @@ -1769,11 +1776,15 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask, goto fput_and_out; /* - * Permission events require minimum priority FAN_CLASS_CONTENT. + * Permission events are not allowed for FAN_CLASS_NOTIF. + * Pre-content permission events are not allowed for FAN_CLASS_CONTENT. */ ret = -EINVAL; if (mask & FANOTIFY_PERM_EVENTS && - group->priority < FSNOTIFY_PRIO_CONTENT) + group->priority == FSNOTIFY_PRIO_NORMAL) + goto fput_and_out; + else if (mask & FANOTIFY_PRE_CONTENT_EVENTS && + group->priority == FSNOTIFY_PRIO_CONTENT) goto fput_and_out; if (mask & FAN_FS_ERROR && diff --git a/include/linux/fanotify.h b/include/linux/fanotify.h index 4f1c4f603118..5c811baf44d2 100644 --- a/include/linux/fanotify.h +++ b/include/linux/fanotify.h @@ -88,6 +88,16 @@ #define FANOTIFY_DIRENT_EVENTS (FAN_MOVE | FAN_CREATE | FAN_DELETE | \ FAN_RENAME) +/* Content events can be used to inspect file content */ +#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) + +/* Events that require a permission response from user */ +#define FANOTIFY_PERM_EVENTS (FANOTIFY_CONTENT_PERM_EVENTS | \ + FANOTIFY_PRE_CONTENT_EVENTS) + /* Events that can be reported with event->fd */ #define FANOTIFY_FD_EVENTS (FANOTIFY_PATH_EVENTS | FANOTIFY_PERM_EVENTS) @@ -103,10 +113,6 @@ FANOTIFY_INODE_EVENTS | \ FANOTIFY_ERROR_EVENTS) -/* Events that require a permission response from user */ -#define FANOTIFY_PERM_EVENTS (FAN_OPEN_PERM | FAN_ACCESS_PERM | \ - FAN_OPEN_EXEC_PERM) - /* Extra flags that may be reported with event or control handling of events */ #define FANOTIFY_EVENT_FLAGS (FAN_EVENT_ON_CHILD | FAN_ONDIR) diff --git a/include/uapi/linux/fanotify.h b/include/uapi/linux/fanotify.h index a37de58ca571..bcada21a3a2e 100644 --- a/include/uapi/linux/fanotify.h +++ b/include/uapi/linux/fanotify.h @@ -26,6 +26,8 @@ #define FAN_ACCESS_PERM 0x00020000 /* File accessed in perm check */ #define FAN_OPEN_EXEC_PERM 0x00040000 /* File open/exec in perm check */ +#define FAN_PRE_ACCESS 0x00080000 /* Pre-content access hook */ + #define FAN_EVENT_ON_CHILD 0x08000000 /* Interested in child events */ #define FAN_RENAME 0x10000000 /* File was renamed */ 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: 13791340 Received: from mail-qk1-f182.google.com (mail-qk1-f182.google.com [209.85.222.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B0BA81487D5 for ; Wed, 4 Sep 2024 20:29:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725481765; cv=none; b=VQ9gOVLcx7gmwts9gZJP4RdWzQo3LGS7TYWqYlbMyvQ5MTpwE3SMLdHeyYdX8HhbfJjumoNBS+oLi7bBBE1xPfEIN5k/UkP6yk+uW85dxZOVlSdzfUPoON+ww8pCr+x9VoEVAHnuWdSq83li/5vPUkKw/+ePVcH+2PjDvRKYmE8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725481765; c=relaxed/simple; bh=pD+LVjuiFGqxiYXNcE7SFfWih4a6NzbE/J64a1HUTSE=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=T9H6rlXaBljL+z0tNgyyByBBp/89QrukFPganmyDzrxSZCh0h/3nfRiqMdoRDIAhJLGoOix4e0lQVhooFSaOAyzLFGhPzjpU2OpXEaKjVBUgwb8JTKbrhMp/onR61EiSsEeDiaEI4Cjj0akRXwRFCiE64DOTwsa+s8XKWKdwFnA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com; spf=none smtp.mailfrom=toxicpanda.com; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b=W9s0SA7W; arc=none smtp.client-ip=209.85.222.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b="W9s0SA7W" Received: by mail-qk1-f182.google.com with SMTP id af79cd13be357-7a9786c5ec2so681385a.2 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=vger.kernel.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=W9s0SA7Wi+MA0art6xUELvad0zxjyj1ET9iOuQYhV5qoPRTJO9sVOqrQXJ+c5ZO8PV X/rZzl/oWdg1e5vJS+tcXoRYOH2lr7XNSWH+1WLA4IY9ClG1+uHBYtXxT284Yalvnazu nbP4LfVT/R+ILe8q9y62aekUl9M8zQAgaWbU1/0DN2Jg6+Ft17o8DduQgQVYeQufePy8 IEPoOJA6j/lYuQx4XnSPru1QGgnnXOSC9B/Qk81QPT9Oh2OJjcem9OpZA0cy8y32deT5 MXvzhu7whd6LlQx734mowlRfgaOzifTkWR7o6OpDK+3db52f5lpHMCGYZeeW5EpGiNmN d+dA== 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=RrAyFLkwtMqAn370QIH1wUPXhYit+Rqi/9s5XMY9TM9oz/bUgkJWPR69ltSPdzuVgQ l3yjhgR8P/ag6bwcOAS3NycvD7DIjpjWhMtrleTgNizPY9HLRMx4wr5xNEC3VSjMxpNz W2CXQw0yzGNFe1zxykPCJ1NcFu0fWo7jv8tiBY1Qzk6ZxeZ/dxO1rC0iSofbasKFLQhl UTU7HE2XTwlkGDatrEGTEro7KWWAe8SNkU+Rw6Er5Q3E8nf95ZwkF0MdtUfnML4W1Zkx BpG0lX2YnqvpAHucc0iZdGZYgyDMBaNKclMCGFhzaOSFIehzBRSheoYHbfb6zH1MK3rS 5S8Q== X-Forwarded-Encrypted: i=1; AJvYcCVGzW+2MlmHKJasYFysLg7JLFjZBiVw/I+toS8p99hNy9KI4VDS/MU/Y1ChbsWXwQZKAuU7eou4B4/YCFDI@vger.kernel.org X-Gm-Message-State: AOJu0YzZN+53r+OwH+fFHBDcBGPIHsdrBFidCWXyV3S9AL3cA0el1IPn EnIEVqmexjIazkJlIffYiMD92b3G3f/+k/8OxUsdGXXBBr25AxCTsC8NfWe812k= 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: Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 */ From patchwork Wed Sep 4 20:27:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13791341 Received: from mail-vs1-f49.google.com (mail-vs1-f49.google.com [209.85.217.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 64C9115575E for ; Wed, 4 Sep 2024 20:29:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.217.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725481767; cv=none; b=l3D5mT+xAH0G2/dFH662MhxUMENpd2IJ4KQqKaTwZgDxBaSUC0P8hboejuLREvCCwIzueEyWWRZM0Fn9AJGacEFfU8osKtAh4hm0v34CkUwof80CrdlLYVT1Z5hvBe5Sryfj1ajDamhPKni8+Cnq8ds5bh6UzajBAgNWPC5m0C0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725481767; c=relaxed/simple; bh=EUC6xHSolCsXCfEGgJe/uC9iBPwO+UdprLu222bnv+E=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZZ3Zkd3FPDRLc9lUsrXL5wXqKDgoGp6CyyVDAybAlSbueQIMISzzSDlobrApLOOF7qboYclAlZZr47cBNx05TtKEL9mn73+yYHzs2XUCixtMb6Tku/bHMWhWf/Ola5usrXv8E3jurCmQR8/bbMudyBwXNcbEvenyUdb39v+jE6E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com; spf=none smtp.mailfrom=toxicpanda.com; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b=GmpAbW84; arc=none smtp.client-ip=209.85.217.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b="GmpAbW84" Received: by mail-vs1-f49.google.com with SMTP id ada2fe7eead31-498d7ab8fefso2712166137.1 for ; Wed, 04 Sep 2024 13:29:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1725481764; x=1726086564; darn=vger.kernel.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=WSgX58UYc0emFHK3lK4cO5xgsT7V4KpoEDCer0WZNY4=; b=GmpAbW84QKAJFfHE614knUuBLj+/uOnQlQE9BbI4XT+JfGFF4K0x9DJ6UurngPGQwz NITRGMtL/df9L804hrlb4nT+K76WCWfp5a2PLMbOBOyTlF9EIqXs9OjkJqOhxrQ1DtIb C5n20V4Gxo/f7PdVjrx9DID23g5fKSODv/iZlQem6q1i/qXqUAgEzOV4dDWg5Vr5Zc+8 NlkchdXMIoygdothzyS4XCbCBvi8sVULapBCunji3igqdGMarxlMjIjDoQQ2h/FdapE8 t+F1HlCywxAPiOzko6qyeejVoUTF29CgoX5bGNwXtXO/t7y9TBAkDq4sz/DJ8Q4j7tH7 1hJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725481764; x=1726086564; 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=WSgX58UYc0emFHK3lK4cO5xgsT7V4KpoEDCer0WZNY4=; b=bMdAVfJ0evnuqV2oh+9v09qpkXOVlWqUFr4fKZrtzHkuy7YOuhAPJ2f2mv2xh5iMxD uzUcqN+o+Pv7QxPql/Cz4JVQY+VjO+YdabYInLtoahdDjrlhIb6klLkIv4d8l+A5puWK zlI8FkfMmz+b+eYb2izu+Cg2oDrHybtKbgHT2SI48u2KU4PxRlp3CHrOjPx/Aq5VsLhs UEjM7PdFpRkf7JYf3r8HFLQljp3yMd1E4UXPxqFVY9blBaxwgrIdMddp2vbfPGnvgBV+ x70IU4arz11MbjjGOUA8FJfO7P9bpD8IGT9zi3CyAb+4pS+vwJeMI5MKzvP8Ropyu7Zb steA== X-Forwarded-Encrypted: i=1; AJvYcCWvapMoZg+sukFcY9JNkuXOfMApZCnpVvDU/Ir9IJKFzywllLYNp1fdDihS3c6NAgCwVrdSBfUoHf4kFXBJ@vger.kernel.org X-Gm-Message-State: AOJu0YwnTEhpo5vtwSN2mw9ZdRVtVUMcWlYxKwdtU50f3AnYqUtUToxY wCP85gpXGutBFwt31ETOoeaIYHg8yy36SDTYEY6ndaLRNcS3SU9PMU3Q7C0RryU= X-Google-Smtp-Source: AGHT+IGc7mGWNoIW6t0qFMzy1JgoaW/y/pAizbNG4FOmezxVo9UEWHiWN81iBDFZM1s4pxXg42sQVA== X-Received: by 2002:a05:6102:442b:b0:498:cf81:3a40 with SMTP id ada2fe7eead31-49a7778c1admr18354171137.17.1725481764310; Wed, 04 Sep 2024 13:29:24 -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-7a98efeb028sm15207185a.84.2024.09.04.13.29.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2024 13:29:23 -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 06/18] fanotify: pass optional file access range in pre-content event Date: Wed, 4 Sep 2024 16:27:56 -0400 Message-ID: <87325af81514d7bd0b2236e14c613b7160651bda.1725481503.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Amir Goldstein We would like to add file range information to pre-content events. Pass a struct file_range with optional offset and length to event handler along with pre-content permission event. Signed-off-by: Amir Goldstein --- fs/notify/fanotify/fanotify.c | 10 ++++++++-- fs/notify/fanotify/fanotify.h | 2 ++ include/linux/fsnotify.h | 17 ++++++++++++++++- include/linux/fsnotify_backend.h | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 3 deletions(-) diff --git a/fs/notify/fanotify/fanotify.c b/fs/notify/fanotify/fanotify.c index b163594843f5..4e8dce39fa8f 100644 --- a/fs/notify/fanotify/fanotify.c +++ b/fs/notify/fanotify/fanotify.c @@ -549,9 +549,13 @@ static struct fanotify_event *fanotify_alloc_path_event(const struct path *path, return &pevent->fae; } -static struct fanotify_event *fanotify_alloc_perm_event(const struct path *path, +static struct fanotify_event *fanotify_alloc_perm_event(const void *data, + int data_type, gfp_t gfp) { + const struct path *path = fsnotify_data_path(data, data_type); + const struct file_range *range = + fsnotify_data_file_range(data, data_type); struct fanotify_perm_event *pevent; pevent = kmem_cache_alloc(fanotify_perm_event_cachep, gfp); @@ -565,6 +569,8 @@ static struct fanotify_event *fanotify_alloc_perm_event(const struct path *path, pevent->hdr.len = 0; pevent->state = FAN_EVENT_INIT; pevent->path = *path; + pevent->ppos = range ? range->ppos : NULL; + pevent->count = range ? range->count : 0; path_get(path); return &pevent->fae; @@ -802,7 +808,7 @@ static struct fanotify_event *fanotify_alloc_event( old_memcg = set_active_memcg(group->memcg); if (fanotify_is_perm_event(mask)) { - event = fanotify_alloc_perm_event(path, gfp); + event = fanotify_alloc_perm_event(data, data_type, gfp); } else if (fanotify_is_error_event(mask)) { event = fanotify_alloc_error_event(group, fsid, data, data_type, &hash); diff --git a/fs/notify/fanotify/fanotify.h b/fs/notify/fanotify/fanotify.h index e5ab33cae6a7..93598b7d5952 100644 --- a/fs/notify/fanotify/fanotify.h +++ b/fs/notify/fanotify/fanotify.h @@ -425,6 +425,8 @@ FANOTIFY_PE(struct fanotify_event *event) struct fanotify_perm_event { struct fanotify_event fae; struct path path; + const loff_t *ppos; /* optional file range info */ + size_t count; u32 response; /* userspace answer to the event */ unsigned short state; /* state of the event */ int fd; /* fd we passed to userspace for this event */ diff --git a/include/linux/fsnotify.h b/include/linux/fsnotify.h index fb3837b8de4c..9d001d328619 100644 --- a/include/linux/fsnotify.h +++ b/include/linux/fsnotify.h @@ -132,6 +132,21 @@ static inline int fsnotify_file(struct file *file, __u32 mask) } #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS +static inline int fsnotify_file_range(struct file *file, __u32 mask, + const loff_t *ppos, size_t count) +{ + struct file_range range; + + if (file->f_mode & FMODE_NONOTIFY) + return 0; + + range.path = &file->f_path; + range.ppos = ppos; + range.count = count; + return fsnotify_parent(range.path->dentry, mask, &range, + FSNOTIFY_EVENT_FILE_RANGE); +} + /* * fsnotify_file_area_perm - permission hook before access/modify of file range */ @@ -175,7 +190,7 @@ static inline int fsnotify_file_area_perm(struct file *file, int perm_mask, else return 0; - return fsnotify_file(file, fsnotify_mask); + return fsnotify_file_range(file, fsnotify_mask, ppos, count); } /* diff --git a/include/linux/fsnotify_backend.h b/include/linux/fsnotify_backend.h index 200a5e3b1cd4..276320846bfd 100644 --- a/include/linux/fsnotify_backend.h +++ b/include/linux/fsnotify_backend.h @@ -298,6 +298,7 @@ static inline void fsnotify_group_assert_locked(struct fsnotify_group *group) /* When calling fsnotify tell it if the data is a path or inode */ enum fsnotify_data_type { FSNOTIFY_EVENT_NONE, + FSNOTIFY_EVENT_FILE_RANGE, FSNOTIFY_EVENT_PATH, FSNOTIFY_EVENT_INODE, FSNOTIFY_EVENT_DENTRY, @@ -310,6 +311,17 @@ struct fs_error_report { struct super_block *sb; }; +struct file_range { + const struct path *path; + const loff_t *ppos; + size_t count; +}; + +static inline const struct path *file_range_path(const struct file_range *range) +{ + return range->path; +} + static inline struct inode *fsnotify_data_inode(const void *data, int data_type) { switch (data_type) { @@ -319,6 +331,8 @@ static inline struct inode *fsnotify_data_inode(const void *data, int data_type) return d_inode(data); case FSNOTIFY_EVENT_PATH: return d_inode(((const struct path *)data)->dentry); + case FSNOTIFY_EVENT_FILE_RANGE: + return d_inode(file_range_path(data)->dentry); case FSNOTIFY_EVENT_ERROR: return ((struct fs_error_report *)data)->inode; default: @@ -334,6 +348,8 @@ static inline struct dentry *fsnotify_data_dentry(const void *data, int data_typ return (struct dentry *)data; case FSNOTIFY_EVENT_PATH: return ((const struct path *)data)->dentry; + case FSNOTIFY_EVENT_FILE_RANGE: + return file_range_path(data)->dentry; default: return NULL; } @@ -345,6 +361,8 @@ static inline const struct path *fsnotify_data_path(const void *data, switch (data_type) { case FSNOTIFY_EVENT_PATH: return data; + case FSNOTIFY_EVENT_FILE_RANGE: + return file_range_path(data); default: return NULL; } @@ -360,6 +378,8 @@ static inline struct super_block *fsnotify_data_sb(const void *data, return ((struct dentry *)data)->d_sb; case FSNOTIFY_EVENT_PATH: return ((const struct path *)data)->dentry->d_sb; + case FSNOTIFY_EVENT_FILE_RANGE: + return file_range_path(data)->dentry->d_sb; case FSNOTIFY_EVENT_ERROR: return ((struct fs_error_report *) data)->sb; default: @@ -379,6 +399,18 @@ static inline struct fs_error_report *fsnotify_data_error_report( } } +static inline const struct file_range *fsnotify_data_file_range( + const void *data, + int data_type) +{ + switch (data_type) { + case FSNOTIFY_EVENT_FILE_RANGE: + return (struct file_range *)data; + default: + return NULL; + } +} + /* * Index to merged marks iterator array that correlates to a type of watch. * The type of watched object can be deduced from the iterator type, but not From patchwork Wed Sep 4 20:27:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13791342 Received: from mail-qv1-f52.google.com (mail-qv1-f52.google.com [209.85.219.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F0218158DD9 for ; Wed, 4 Sep 2024 20:29:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725481768; cv=none; b=WtdEZ6eiu5zXtfLPZ1wOYmGwS14DjHLEsGCGBGnf/67v9ZbmF431M9uN6fQxvJVl/iC01tG51Wei1Jq0lbgD1GnpvZDUSfxbL8ipiLhrbt67XldWCfWDiwVtUq/ScjS7bvq7RvdfWrNlidxth2Hw/QzbyVj5Hz0wI8f7Uq7z9Qg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725481768; c=relaxed/simple; bh=qausY9NaTn1Dmt/1fWvpjV6gSoo0TkpL7RjFx1j8ing=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jRyBgSaz/+pvCNJAlLAHSQh3VMBh6IegZ5Xb9B1xjDW6hzRCU9f5m0JQCwVMqWFzlULixPdOT1fPGdkd0RDxoq4a2KlGEvYpSE3UOk2d1XgiX1xgXviaKJ5582pE3ZFW5XBsmGuvO6xTqLIBQ810LQiKlFdfdwSu8qN1LpP3lNY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com; spf=none smtp.mailfrom=toxicpanda.com; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b=baH9sSF2; arc=none smtp.client-ip=209.85.219.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b="baH9sSF2" Received: by mail-qv1-f52.google.com with SMTP id 6a1803df08f44-6bf9ddfc2dcso85026d6.1 for ; Wed, 04 Sep 2024 13:29:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1725481766; x=1726086566; darn=vger.kernel.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=JmFE9zypkxWwtjgDkL/vbyJWxHrdDWRoGs2W5KUFOnA=; b=baH9sSF25BC9MoGa72zssTgL4tSh4NUw65xw5uzBY1Ah2S3c/4aaV3+ZPuuFsEo+vz VXOTNl8x4jAy0C7ecGAirSYKAmWXv4NzHV6yQJM4PqZS7aFREYL3eblUgynJY6L6nH0M szpM6SmlHWi42JXLOjCtPwA4gboAwY9v+R76KatEPunSWuj1G6RaiQmQxjS9CMR9Q6MP NodiiVjHaC/rxBfv9eRQNWQBW6wEgUWwwGtZxTjZFkwAMC3/h2lj3UuiMEE2Bl2lX8jW GXnrodJQ+MbQichckjWC8x+4m2knti2kBzLIR0r0GFkvkLQ69rbfn/lDMg6rWA8fS5Ws LoSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725481766; x=1726086566; 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=JmFE9zypkxWwtjgDkL/vbyJWxHrdDWRoGs2W5KUFOnA=; b=cJ7i/Kh+lKFqqlOICJ8N19UeOLpx+37UhyHB91FUXcgBirwo1Qz/iM2SsLUbGvkwYt Lxoh+6VNpr7L08aIYqImvdauXRV1vMMPg2GVbJEz9/3wPxWnNS4Dgp0Dz+4yFb+Aug/M wqRP4jhN53Wi1sWTjn4sJn3+Z65ut05yT4a5zj7vtiITTDwSy5qzX8tZNexAC32Y+LQa hUNDEpGPkiGTtsFbiQrDyEkUrnonXmDi5d+e3GvwZgCnslJnDqfQme2fQ+628oKMrpYB uF4cIN8Zke4TMRr3dD9fI2cUJ2Y9jMV+kqKUjyrM/TWS0z+w5tBVsfa/PrYrSoKK2+TZ bAGg== X-Forwarded-Encrypted: i=1; AJvYcCVHcAsC7QCLR9x0VXybk4vxOKMDAYG1pxURrrsEfHPOSp4AquLda5OJOuWOuMvXjn573dvPHJLHlbRFYtG3@vger.kernel.org X-Gm-Message-State: AOJu0YzkeB5ep58wEQZV3iyB6xeaaMz6JbjKLsMyIlkBx0LcBOTRbId6 GkdQwZEM6NrWRLd2ViaC4d03t+CnM7uOHchrCPS+l41HXN/IOmIIWgU73bTwnIg= X-Google-Smtp-Source: AGHT+IFz/IUidwqQ59xRLvRnlzpJcJ1KGg9KuPmXBEEett+13VoyzgSnYGWxrufSaJ5p3j4xy3X2+w== X-Received: by 2002:a05:6214:4b09:b0:6c3:54fe:ebef with SMTP id 6a1803df08f44-6c3558359fbmr194853636d6.43.1725481765951; Wed, 04 Sep 2024 13:29:25 -0700 (PDT) Received: from localhost (syn-076-182-020-124.res.spectrum.com. [76.182.20.124]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6c5201dedd8sm1621726d6.7.2024.09.04.13.29.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2024 13:29:25 -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 07/18] fanotify: rename a misnamed constant Date: Wed, 4 Sep 2024 16:27:57 -0400 Message-ID: <13c1df955c0e8af0aee2afce78b1ea1f2e3f8f66.1725481503.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Amir Goldstein FANOTIFY_PIDFD_INFO_HDR_LEN is not the length of the header. Signed-off-by: Amir Goldstein Reviewed-by: Christian Brauner --- fs/notify/fanotify/fanotify_user.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c index 3a7101544f30..5ece186d5c50 100644 --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c @@ -119,7 +119,7 @@ struct kmem_cache *fanotify_perm_event_cachep __ro_after_init; #define FANOTIFY_EVENT_ALIGN 4 #define FANOTIFY_FID_INFO_HDR_LEN \ (sizeof(struct fanotify_event_info_fid) + sizeof(struct file_handle)) -#define FANOTIFY_PIDFD_INFO_HDR_LEN \ +#define FANOTIFY_PIDFD_INFO_LEN \ sizeof(struct fanotify_event_info_pidfd) #define FANOTIFY_ERROR_INFO_LEN \ (sizeof(struct fanotify_event_info_error)) @@ -174,14 +174,14 @@ static size_t fanotify_event_len(unsigned int info_mode, dot_len = 1; } - if (info_mode & FAN_REPORT_PIDFD) - event_len += FANOTIFY_PIDFD_INFO_HDR_LEN; - if (fanotify_event_has_object_fh(event)) { fh_len = fanotify_event_object_fh_len(event); event_len += fanotify_fid_info_len(fh_len, dot_len); } + if (info_mode & FAN_REPORT_PIDFD) + event_len += FANOTIFY_PIDFD_INFO_LEN; + return event_len; } @@ -511,7 +511,7 @@ static int copy_pidfd_info_to_user(int pidfd, size_t count) { struct fanotify_event_info_pidfd info = { }; - size_t info_len = FANOTIFY_PIDFD_INFO_HDR_LEN; + size_t info_len = FANOTIFY_PIDFD_INFO_LEN; if (WARN_ON_ONCE(info_len > count)) return -EFAULT; From patchwork Wed Sep 4 20:27:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13791343 Received: from mail-qk1-f170.google.com (mail-qk1-f170.google.com [209.85.222.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DAA741714C1 for ; Wed, 4 Sep 2024 20:29:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725481770; cv=none; b=dFpSJvwPWv1mmre2nRH7eA3rnsxulM/XeD9qyK2oWce/dQCjQ831deeh82hLyxKOAxxTRW5uDMGmGm2z4Aoe91Rs9oyRXneESinGB8Lwe6qxZFNAeo16QTK3X8Fw/4l7lvP7iEtOZbhzczu2tU1eY3NzK7zVOKH50twpbP63GWw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725481770; c=relaxed/simple; bh=UkBoQACwZWNS66VUS7nQ/E+uy/3pz3xN2YqDEBSBDyw=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=A3vlJokbaIttDeFnQqlLZ9phAnPXSAqt1N24knSLIpf/jaTWwKkoNRVV5bhnYc4K85EWC0+w8m3WX0cqsW2SHk/soAoagc78ifrT+3YPXRnzzdKBbcErZXXi2TyQsTDk6o0c/XiJsHdf5TM6mrSINvq7vo6lTM8qpVLdpxy+kVU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com; spf=none smtp.mailfrom=toxicpanda.com; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b=mdhpT0tG; arc=none smtp.client-ip=209.85.222.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b="mdhpT0tG" Received: by mail-qk1-f170.google.com with SMTP id af79cd13be357-7a8049d4a4cso2839285a.0 for ; Wed, 04 Sep 2024 13:29:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1725481768; x=1726086568; darn=vger.kernel.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=kdFADlNMi9J5+Jo97YgtLX17z0QeGZLAaqk7oAQuL90=; b=mdhpT0tGPDmH7J6XLzDodwE6DGwnaSrY0JW+v7D6TVPxyccLmEFXz/cxq37IdLQLEA AyvZO0deX815OA++zxzdqC3KDTmSV4gsUjiWE/K86wYLFRMbr01w5UeH0t5SNp4Vg2/4 meaw6/HNo724mJzJJU8iEPvnRNHHCzcaCkRq5/Embp/YyByUk9iAWru1YDscrfby7MvS 2+ycnd7/xJX5nlQfWsFq+yaErpj15Z6lqpQVU5kS/kJ6/xleoqmr9hHnI5UAh/wK7Ykp 04xmVLXCqSmr+phqe+tAsVECuQi/eiWgZLcIqEPdmgPRXqy7gA1wiA6sFQHggyU+SxII 3iRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725481768; x=1726086568; 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=kdFADlNMi9J5+Jo97YgtLX17z0QeGZLAaqk7oAQuL90=; b=Fa05nRKa1266I1LuF+IbKMCBnKeqwxNHfDWBoV9keiSi5BxVgov/sHSxG/HB2h+oDD mjUwv4lMKElcJ7VC002MpEqHBdBT03wzU8MXcRgqySgGwLBTI+S/rxT6jpsvN1zYR8mI KH7l3ejPymkN8jZFs+zO/93hv+XsEjwlTJ+tzmXmcH+DRKjz/KBle95CLDIsH9Ec/OGB IbkYMANe9DBM0E7n2rEPoXLBvhqg4tFpO6PkgEwmr2wicMmqyyRG//3UgjWWJhan51nF QQagbMv0KBtqiCASQuH+ayjV7D60ytapKvKkKLfWefZOuvcxkkuDtijGsLZ4DTM0ODTw ZPeg== X-Forwarded-Encrypted: i=1; AJvYcCVUo6UlEZJe0avBOml7DzU1Xam1lRUudZ2kGNOQqOOm2QILlXH/woREewQHAGkTTdVD2pFpje8FVD6NuePs@vger.kernel.org X-Gm-Message-State: AOJu0Yyo3l38UQr+P3T5sPjhs6q/H1jX7l+1jMVkuJdpZ0FmuN9FMKsI dSL0h8H/mxgSHqYza+ljstjZoGo7pORT8MRPzXobYod5sbPSw4D+knVwM7fXiaE= X-Google-Smtp-Source: AGHT+IGx6um9uXhEhda5h73KKWCoNW2FF/dwyP15+JSEZ33BwCH3HCg2dZ3AYVIuYxLr9QIEDkXCMg== X-Received: by 2002:a05:620a:1724:b0:79e:fc9c:4bcb with SMTP id af79cd13be357-7a8931ddae4mr1887788985a.11.1725481767911; Wed, 04 Sep 2024 13:29:27 -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-7a98ef4a2ebsm15638785a.67.2024.09.04.13.29.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2024 13:29:27 -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 08/18] fanotify: report file range info with pre-content events Date: Wed, 4 Sep 2024 16:27:58 -0400 Message-ID: <0833eaada39f0a33e655b47f2d15a5a37cf39a78.1725481503.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Amir Goldstein With group class FAN_CLASS_PRE_CONTENT, report offset and length info along with FAN_PRE_ACCESS and FAN_PRE_MODIFY permission events. This information is meant to be used by hierarchical storage managers that want to fill partial content of files on first access to range. Signed-off-by: Amir Goldstein --- fs/notify/fanotify/fanotify.h | 8 +++++++ fs/notify/fanotify/fanotify_user.c | 38 ++++++++++++++++++++++++++++++ include/uapi/linux/fanotify.h | 8 +++++++ 3 files changed, 54 insertions(+) diff --git a/fs/notify/fanotify/fanotify.h b/fs/notify/fanotify/fanotify.h index 93598b7d5952..7f06355afa1f 100644 --- a/fs/notify/fanotify/fanotify.h +++ b/fs/notify/fanotify/fanotify.h @@ -448,6 +448,14 @@ static inline bool fanotify_is_perm_event(u32 mask) mask & FANOTIFY_PERM_EVENTS; } +static inline bool fanotify_event_has_access_range(struct fanotify_event *event) +{ + if (!(event->mask & FANOTIFY_PRE_CONTENT_EVENTS)) + return false; + + return FANOTIFY_PERM(event)->ppos; +} + static inline struct fanotify_event *FANOTIFY_E(struct fsnotify_event *fse) { return container_of(fse, struct fanotify_event, fse); diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c index 5ece186d5c50..ed56fe6f5ec7 100644 --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c @@ -123,6 +123,8 @@ struct kmem_cache *fanotify_perm_event_cachep __ro_after_init; sizeof(struct fanotify_event_info_pidfd) #define FANOTIFY_ERROR_INFO_LEN \ (sizeof(struct fanotify_event_info_error)) +#define FANOTIFY_RANGE_INFO_LEN \ + (sizeof(struct fanotify_event_info_range)) static int fanotify_fid_info_len(int fh_len, int name_len) { @@ -182,6 +184,9 @@ static size_t fanotify_event_len(unsigned int info_mode, if (info_mode & FAN_REPORT_PIDFD) event_len += FANOTIFY_PIDFD_INFO_LEN; + if (fanotify_event_has_access_range(event)) + event_len += FANOTIFY_RANGE_INFO_LEN; + return event_len; } @@ -526,6 +531,30 @@ static int copy_pidfd_info_to_user(int pidfd, return info_len; } +static size_t copy_range_info_to_user(struct fanotify_event *event, + char __user *buf, int count) +{ + struct fanotify_perm_event *pevent = FANOTIFY_PERM(event); + struct fanotify_event_info_range info = { }; + size_t info_len = FANOTIFY_RANGE_INFO_LEN; + + if (WARN_ON_ONCE(info_len > count)) + return -EFAULT; + + if (WARN_ON_ONCE(!pevent->ppos)) + return -EINVAL; + + info.hdr.info_type = FAN_EVENT_INFO_TYPE_RANGE; + info.hdr.len = info_len; + info.offset = *(pevent->ppos); + info.count = pevent->count; + + if (copy_to_user(buf, &info, info_len)) + return -EFAULT; + + return info_len; +} + static int copy_info_records_to_user(struct fanotify_event *event, struct fanotify_info *info, unsigned int info_mode, int pidfd, @@ -647,6 +676,15 @@ static int copy_info_records_to_user(struct fanotify_event *event, total_bytes += ret; } + if (fanotify_event_has_access_range(event)) { + ret = copy_range_info_to_user(event, buf, count); + if (ret < 0) + return ret; + buf += ret; + count -= ret; + total_bytes += ret; + } + return total_bytes; } diff --git a/include/uapi/linux/fanotify.h b/include/uapi/linux/fanotify.h index ac00fad66416..6136e8a9f9f3 100644 --- a/include/uapi/linux/fanotify.h +++ b/include/uapi/linux/fanotify.h @@ -145,6 +145,7 @@ struct fanotify_event_metadata { #define FAN_EVENT_INFO_TYPE_DFID 3 #define FAN_EVENT_INFO_TYPE_PIDFD 4 #define FAN_EVENT_INFO_TYPE_ERROR 5 +#define FAN_EVENT_INFO_TYPE_RANGE 6 /* Special info types for FAN_RENAME */ #define FAN_EVENT_INFO_TYPE_OLD_DFID_NAME 10 @@ -191,6 +192,13 @@ struct fanotify_event_info_error { __u32 error_count; }; +struct fanotify_event_info_range { + struct fanotify_event_info_header hdr; + __u32 pad; + __u64 offset; + __u64 count; +}; + /* * User space may need to record additional information about its decision. * The extra information type records what kind of information is included. From patchwork Wed Sep 4 20:27:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13791344 Received: from mail-oa1-f51.google.com (mail-oa1-f51.google.com [209.85.160.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9B65D17A5A6 for ; Wed, 4 Sep 2024 20:29:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725481773; cv=none; b=YCB9tRle7gXO6JdRe4TJlkiN5HtrV67y6gomxAZTKejeAlv63XBviS7t7fzPFV+4FcxlQ/TARzDSwuRU0juYccJfJw+MkMF4RK2Rk+34cVvReccXRNNYS7nGUJj1/VQ1NIid8RpYmRkQHrb4guwgrGt7uSpc/T5BeDf8fdybbDE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725481773; c=relaxed/simple; bh=rLw5E3Nr8xGnefhdw14RMig280QojdVrlp1xvQ092tU=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=e7QgYxSwZ1Ut/99+Q4kAHRiNWcb0Wuq1jGyt4dO2LLOjGCt/T5YsawIFoO1rs3aGVqlH47Z3I8wzZCh/ZSDRnw3Itnu2ED8CvnX32lM+BXae9c5Ohtr9UtNb5yxrb1U3SfO7Lo+U4t0XbSfWNDFSwZcJVod2Au6Ub0agqrYz5JM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com; spf=none smtp.mailfrom=toxicpanda.com; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b=Bn/qBV3m; arc=none smtp.client-ip=209.85.160.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b="Bn/qBV3m" Received: by mail-oa1-f51.google.com with SMTP id 586e51a60fabf-27830bce86bso51895fac.0 for ; Wed, 04 Sep 2024 13:29:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1725481769; x=1726086569; darn=vger.kernel.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=9jRcB1XulXFmj7DKhtdmn8n+Hih5C6C9O3bk81xEiZw=; b=Bn/qBV3m7VcPstty/RD4YSBN7kabLSI9Q80EWDBXBFcc46EGxRChm5bGiFmw7ZGAR6 1ql89+G+u2CHtuIUiLii4z29Ywf7y+s2dHcKrLbF/r1vMzrlHi1OA8uXeEVtzlQtKD6H tvqtRHr0a4WlJ6HQKYttJrdJh5Y4Kp5JZwHdJcZpzrJNBh0f6OrUCRmguIENUgxeyz56 yFK1zz3+5q7ylIALHX19RhOnlpMUc4CKmsGfI7BohILpZe/A6LcaD9l1giUShev/5qYE eQz7Em0UgQr9EmopoH6pbzgZN66L+qUmINjaUTH5UeSg58xhvCqXjlStAhvDfOOLkml5 u+dA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725481769; x=1726086569; 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=9jRcB1XulXFmj7DKhtdmn8n+Hih5C6C9O3bk81xEiZw=; b=jawVfayCP3jJCClApyg7i41P8tt/tUnz0yh9fjfwetqzLwX76JzglhN8qLRS41rUpN bEz9DJ3t73koHKbA94mbdPxZwjqznul7O1wF76t1SpfOGAiSa1h/BhOrdn73whNPqmSH te0CMWKCKkj3fZKtOtQhbtSGvEN9WzzXxvsCXFk/OgYAdiigr/f81p0QGEcc2WrsOOaq Xtt19+Y6A8b8cyo/TqhCB/CfUHEAn1r6EY4uaYRJ1N/TOQz6+YJ80xvszqIcbETpw1ED KtoM3+mLkW4lFHWcps/g+YnwxggXBXhC33Zlx6DyW4gSpxSNXB/Iv5+2reOyh5tyJt9I /EGw== X-Forwarded-Encrypted: i=1; AJvYcCW7Xw0EllgW+BOW23uG3lLAuJT8ZB5mQ+3ZCiuYjacXUlPizNsCkSLL3nNcIVErPslmyDEkb9s4xsMRoZX0@vger.kernel.org X-Gm-Message-State: AOJu0Yxg90fhlBQeeXJNdrdi3g/s6vHENhoL/HeV8AR5Z8qPRERQ/a/E /1oX716xC/aarrpu42P3y5SKbndsRLJY5vx9yczIY8FhCuoSWmLFv/W2ywr1EcQ= X-Google-Smtp-Source: AGHT+IFWlV+ptqOFoXLlPgR/wzgAif5CenGcM7a3jH83CCzPXpqmv1KE+P96IBtwqQpEGOVQpckR6A== X-Received: by 2002:a05:6870:a1a1:b0:270:5f17:b34 with SMTP id 586e51a60fabf-277900b31a5mr23054551fac.11.1725481769655; Wed, 04 Sep 2024 13:29:29 -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-7a98efed964sm15316985a.73.2024.09.04.13.29.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2024 13:29:29 -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 09/18] fanotify: allow to set errno in FAN_DENY permission response Date: Wed, 4 Sep 2024 16:27:59 -0400 Message-ID: <8c5ace4661500573fdc078b5e595b9397c13b859.1725481503.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Amir Goldstein With FAN_DENY response, user trying to perform the filesystem operation gets an error with errno set to EPERM. It is useful for hierarchical storage management (HSM) service to be able to deny access for reasons more diverse than EPERM, for example EAGAIN, if HSM could retry the operation later. Allow fanotify groups with priority FAN_CLASSS_PRE_CONTENT to responsd to permission events with the response value FAN_DENY_ERRNO(errno), instead of FAN_DENY to return a custom error. Limit custom error values to errors expected on read(2)/write(2) and open(2) of regular files. This list could be extended in the future. Userspace can test for legitimate values of FAN_DENY_ERRNO(errno) by writing a response to an fanotify group fd with a value of FAN_NOFD in the fd field of the response. The change in fanotify_response is backward compatible, because errno is written in the high 8 bits of the 32bit response field and old kernels reject respose value with high bits set. Signed-off-by: Amir Goldstein --- fs/notify/fanotify/fanotify.c | 19 +++++++++++---- fs/notify/fanotify/fanotify.h | 5 ++++ fs/notify/fanotify/fanotify_user.c | 37 ++++++++++++++++++++++++++---- include/linux/fanotify.h | 5 +++- include/uapi/linux/fanotify.h | 7 ++++++ 5 files changed, 62 insertions(+), 11 deletions(-) diff --git a/fs/notify/fanotify/fanotify.c b/fs/notify/fanotify/fanotify.c index 4e8dce39fa8f..2969f45a08dc 100644 --- a/fs/notify/fanotify/fanotify.c +++ b/fs/notify/fanotify/fanotify.c @@ -224,7 +224,8 @@ static int fanotify_get_response(struct fsnotify_group *group, struct fanotify_perm_event *event, struct fsnotify_iter_info *iter_info) { - int ret; + int ret, errno; + u32 decision; pr_debug("%s: group=%p event=%p\n", __func__, group, event); @@ -257,20 +258,28 @@ static int fanotify_get_response(struct fsnotify_group *group, goto out; } + decision = event->response & + (FANOTIFY_RESPONSE_ACCESS | FANOTIFY_RESPONSE_FLAGS); /* userspace responded, convert to something usable */ - switch (event->response & FANOTIFY_RESPONSE_ACCESS) { + switch (decision & FANOTIFY_RESPONSE_ACCESS) { case FAN_ALLOW: ret = 0; break; case FAN_DENY: + /* Check custom errno from pre-content events */ + errno = fanotify_get_response_errno(event->response); + if (errno) { + ret = -errno; + break; + } + fallthrough; default: ret = -EPERM; } /* Check if the response should be audited */ - if (event->response & FAN_AUDIT) - audit_fanotify(event->response & ~FAN_AUDIT, - &event->audit_rule); + if (decision & FAN_AUDIT) + audit_fanotify(decision & ~FAN_AUDIT, &event->audit_rule); pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__, group, event, ret); diff --git a/fs/notify/fanotify/fanotify.h b/fs/notify/fanotify/fanotify.h index 7f06355afa1f..9e93aba210c9 100644 --- a/fs/notify/fanotify/fanotify.h +++ b/fs/notify/fanotify/fanotify.h @@ -528,3 +528,8 @@ static inline unsigned int fanotify_mark_user_flags(struct fsnotify_mark *mark) return mflags; } + +static inline u32 fanotify_get_response_errno(int res) +{ + return res >> FAN_ERRNO_SHIFT; +} diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c index ed56fe6f5ec7..53eee8af34a0 100644 --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c @@ -337,11 +337,14 @@ static int process_access_response(struct fsnotify_group *group, struct fanotify_perm_event *event; int fd = response_struct->fd; u32 response = response_struct->response; + u32 decision = response & + (FANOTIFY_RESPONSE_ACCESS | FANOTIFY_RESPONSE_FLAGS); + int errno = fanotify_get_response_errno(response); int ret = info_len; struct fanotify_response_info_audit_rule friar; - pr_debug("%s: group=%p fd=%d response=%u buf=%p size=%zu\n", __func__, - group, fd, response, info, info_len); + pr_debug("%s: group=%p fd=%d response=%x errno=%d buf=%p size=%zu\n", + __func__, group, fd, response, errno, info, info_len); /* * make sure the response is valid, if invalid we do nothing and either * userspace can send a valid response or we will clean it up after the @@ -350,18 +353,42 @@ static int process_access_response(struct fsnotify_group *group, if (response & ~FANOTIFY_RESPONSE_VALID_MASK) return -EINVAL; - switch (response & FANOTIFY_RESPONSE_ACCESS) { + switch (decision & FANOTIFY_RESPONSE_ACCESS) { case FAN_ALLOW: + if (errno) + return -EINVAL; + break; case FAN_DENY: + /* Custom errno is supported only for pre-content groups */ + if (errno && group->priority != FSNOTIFY_PRIO_PRE_CONTENT) + return -EINVAL; + + /* + * Limit errno to values expected on open(2)/read(2)/write(2) + * of regular files. + */ + switch (errno) { + case 0: + case EIO: + case EPERM: + case EBUSY: + case ETXTBSY: + case EAGAIN: + case ENOSPC: + case EDQUOT: + break; + default: + return -EINVAL; + } break; default: return -EINVAL; } - if ((response & FAN_AUDIT) && !FAN_GROUP_FLAG(group, FAN_ENABLE_AUDIT)) + if ((decision & FAN_AUDIT) && !FAN_GROUP_FLAG(group, FAN_ENABLE_AUDIT)) return -EINVAL; - if (response & FAN_INFO) { + if (decision & FAN_INFO) { ret = process_access_response_info(info, info_len, &friar); if (ret < 0) return ret; diff --git a/include/linux/fanotify.h b/include/linux/fanotify.h index ae6cb2688d52..547514542669 100644 --- a/include/linux/fanotify.h +++ b/include/linux/fanotify.h @@ -132,7 +132,10 @@ /* These masks check for invalid bits in permission responses. */ #define FANOTIFY_RESPONSE_ACCESS (FAN_ALLOW | FAN_DENY) #define FANOTIFY_RESPONSE_FLAGS (FAN_AUDIT | FAN_INFO) -#define FANOTIFY_RESPONSE_VALID_MASK (FANOTIFY_RESPONSE_ACCESS | FANOTIFY_RESPONSE_FLAGS) +#define FANOTIFY_RESPONSE_ERRNO (FAN_ERRNO_MASK << FAN_ERRNO_SHIFT) +#define FANOTIFY_RESPONSE_VALID_MASK \ + (FANOTIFY_RESPONSE_ACCESS | FANOTIFY_RESPONSE_FLAGS | \ + FANOTIFY_RESPONSE_ERRNO) /* Do not use these old uapi constants internally */ #undef FAN_ALL_CLASS_BITS diff --git a/include/uapi/linux/fanotify.h b/include/uapi/linux/fanotify.h index 6136e8a9f9f3..34b5d634de1f 100644 --- a/include/uapi/linux/fanotify.h +++ b/include/uapi/linux/fanotify.h @@ -234,6 +234,13 @@ struct fanotify_response_info_audit_rule { /* Legit userspace responses to a _PERM event */ #define FAN_ALLOW 0x01 #define FAN_DENY 0x02 +/* errno other than EPERM can specified in upper byte of deny response */ +#define FAN_ERRNO_BITS 8 +#define FAN_ERRNO_SHIFT (32 - FAN_ERRNO_BITS) +#define FAN_ERRNO_MASK ((1 << FAN_ERRNO_BITS) - 1) +#define FAN_DENY_ERRNO(err) \ + (FAN_DENY | ((((__u32)(err)) & FAN_ERRNO_MASK) << FAN_ERRNO_SHIFT)) + #define FAN_AUDIT 0x10 /* Bitmask to create audit record for result */ #define FAN_INFO 0x20 /* Bitmask to indicate additional information */ From patchwork Wed Sep 4 20:28:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13791345 Received: from mail-qt1-f174.google.com (mail-qt1-f174.google.com [209.85.160.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 46707149DE8 for ; Wed, 4 Sep 2024 20:29:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725481773; cv=none; b=G4r7+6BhxzIcAWbUhbiQgVGi/RoThisq4coUrSXCyuNrKgnTy3ZfvmKFILvfnr+9kvgHR4hugG0uhH6o47I5hlxisdm5CxwIt3UZr1Iau+usoHzndLZ2AY6mTDfI9W7N7SZwh4VNXilISXdRylPOa4xuKq6Qvc0IoPrcTR0UnR8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725481773; c=relaxed/simple; bh=QlPOWd3iqjsIDqhqpz2rf1gaW/Uhb/TerJwERWAiY3E=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dcoy649qxkGcdViV+uINPpD6XFSQwfN2HBsiy6OCMYEHsYU+15e+yHiq7pHDtiQf6XFgCcTJDATADsRZJXksxckg5TQ7uFpLN80ToQRyxk5oKJForCgBKexvVkDIiej2RDB16aYJXN2+X92k/QiLmhKjyXR2Q/Zhj3H2v+YSyuo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com; spf=none smtp.mailfrom=toxicpanda.com; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b=W7+VAd2z; arc=none smtp.client-ip=209.85.160.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b="W7+VAd2z" Received: by mail-qt1-f174.google.com with SMTP id d75a77b69052e-457cde43e25so179001cf.3 for ; Wed, 04 Sep 2024 13:29:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1725481771; x=1726086571; darn=vger.kernel.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=hLbVQD1uWq7E7syR9oa0FyZv5hSgj9K2/YM9COMKj/I=; b=W7+VAd2zhTuaLEIoKFKndej1xjJTTgW/Viom1qgzEn3LuV1J5W+b5lGcmDrmfoTaVi Laa8iWBCAA3WfOtrWB/ioQtBCDCchs0jpIzpfxLE9AvwM/SeNisfVLWK0AeDBTgV/8p0 seZ71gsj1HJ5VH8+iMX/BUjKLkW9M7Hd0L2ntjTuucRkN3/HZgujSjfLmALIUsNeoSWU RnrDfA7Vg0qq3HtiQGvEFarppxzSscN3njQmY0CzicN2T1txJELsKETvAowsiwsa2TY4 0xChFONx/kuecJTuEFgzg5iSZ3yyB6eDLLTDpOq6qE+sxkbUbO5Q9vedbr90yNyNZWlO tbiw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725481771; x=1726086571; 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=hLbVQD1uWq7E7syR9oa0FyZv5hSgj9K2/YM9COMKj/I=; b=b3ldZdPmKo24lgAZmXC41YdEF3t+M4QOxDaMQkVE39qKzCWwATSC3h5MImb/oE3GI0 Rgq+hUsaz/4jQ20IUPVsDRK6eoI86umOiO0GUquAB8I7JW6hTX23CJJMa65LuavvueQ7 YwvB6j2SpZzLNFUI2I9zEIUyRliydfX9x1QvHaxOK+5oODtbMHHoPIrcMH1XTgJHylV5 uJYh1+g/+Fw76EYOKBnaySiEC/BSBfFIlV4ALWGbAgf2KFJTUvIzX3lBTo7/paW3VLEY Vg4xK/nSHbx6nv9Held0/gOB4PKyXE03kAWZGt/xd4nWZ+r7te0+eHvYlxQSIU887xvL 5rfg== X-Forwarded-Encrypted: i=1; AJvYcCWolpNZCwCcWM4IWgCHoVxlke+feNEmOElEMHkCS4k9rUlni55CAOgXkB482nw9/j/uvRGebC5Sfu2v+U7S@vger.kernel.org X-Gm-Message-State: AOJu0YxvtwEOipGSLuO21WwDreb2EyRV7KCelhOx4Yf5hq6g2tr/mazE kiIMcveLyWFM1kcgKUtktwA3Lar3KAZoAefBnw1T5zX7NPJZIklLIHDJlCjtSYU= X-Google-Smtp-Source: AGHT+IGdJ5rFqIo2R8SFFCo2OiCjnf+nE2kM7AO4Z/b+MhtME6M5x1ovZdZNYRb7zGAzpKTv2dvAYA== X-Received: by 2002:a05:622a:6209:b0:44f:ed41:6a02 with SMTP id d75a77b69052e-45705429a88mr138893691cf.57.1725481771257; Wed, 04 Sep 2024 13:29:31 -0700 (PDT) Received: from localhost (syn-076-182-020-124.res.spectrum.com. [76.182.20.124]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-45801b5f7aasm1500371cf.52.2024.09.04.13.29.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2024 13:29:30 -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 10/18] fs: add a flag to indicate the fs supports pre-content events Date: Wed, 4 Sep 2024 16:28:00 -0400 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The pre-content events require some extra thinking, especially around page faults. In order to make sure we don't advertise a feature working that doesn't actually work, add a flag to allow file systems to opt-in to this behavior. Signed-off-by: Josef Bacik Reviewed-by: Amir Goldstein --- fs/notify/fanotify/fanotify_user.c | 2 ++ include/linux/fs.h | 1 + include/linux/fsnotify.h | 4 ++++ 3 files changed, 7 insertions(+) diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c index 53eee8af34a0..936e9f9e0cbc 100644 --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c @@ -1736,6 +1736,8 @@ static int fanotify_events_supported(struct fsnotify_group *group, /* Pre-content events are only supported on regular files and dirs */ if (mask & FANOTIFY_PRE_CONTENT_EVENTS) { + if (!(path->mnt->mnt_sb->s_type->fs_flags & FS_ALLOW_HSM)) + return -EINVAL; if (!is_dir && !d_is_reg(path->dentry)) return -EINVAL; if (is_dir && mask & FAN_PRE_MODIFY) diff --git a/include/linux/fs.h b/include/linux/fs.h index fd34b5755c0b..5708e91d3625 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -2494,6 +2494,7 @@ struct file_system_type { #define FS_USERNS_MOUNT 8 /* Can be mounted by userns root */ #define FS_DISALLOW_NOTIFY_PERM 16 /* Disable fanotify permission events */ #define FS_ALLOW_IDMAP 32 /* FS has been updated to handle vfs idmappings. */ +#define FS_ALLOW_HSM 64 /* FS can handle fanotify pre-content events. */ #define FS_RENAME_DOES_D_MOVE 32768 /* FS will handle d_move() during rename() internally. */ int (*init_fs_context)(struct fs_context *); const struct fs_parameter_spec *parameters; diff --git a/include/linux/fsnotify.h b/include/linux/fsnotify.h index 9d001d328619..27992b548f0c 100644 --- a/include/linux/fsnotify.h +++ b/include/linux/fsnotify.h @@ -179,6 +179,10 @@ static inline int fsnotify_file_area_perm(struct file *file, int perm_mask, if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)) return 0; + /* The fs doesn't support pre-content events. */ + if (!(inode->i_sb->s_type->fs_flags & FS_ALLOW_HSM)) + return 0; + if (perm_mask & MAY_WRITE) fsnotify_mask = FS_PRE_MODIFY; else if (perm_mask & (MAY_READ | MAY_ACCESS)) From patchwork Wed Sep 4 20:28:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13791346 Received: from mail-yb1-f172.google.com (mail-yb1-f172.google.com [209.85.219.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2C6781E5018 for ; Wed, 4 Sep 2024 20:29:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725481775; cv=none; b=pZLqGdYHy+beWA6GZJJW3uuymScfJe9Oo87QZH76dpfFalPpE6sF7BbHC3drDxFLwz0k/IP8nNx3es1/lYGlewXG9wvJpuRlWVzZRnWlcp5oalkYkJyle9V4KFXrDzLGRIMJka4qm1aGp8LVj1DBE1lcao57OetlvX6RDrl6NG4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725481775; c=relaxed/simple; bh=RHm6Vcau9bymJmSH7LFX1AgubiQ8JArKmLFZ//38aUA=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JQ0YLQXbu+4YEwIQ12oi3DZ1V4D7Rc8BurNOYIuY6sYGfykJUgbQXgLTxIFzIdDWNfWEjfGG4yDPgoFmzBAmzmKpJQmVKL2Cr7g5yRImdu3DthKX6XwyuPtMYJqdxG6KI6DXbl58tpVMFJr7Gzo0//r1KMT73VZkEQ3KVw6kW4c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com; spf=none smtp.mailfrom=toxicpanda.com; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b=DkyRDa6X; arc=none smtp.client-ip=209.85.219.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b="DkyRDa6X" Received: by mail-yb1-f172.google.com with SMTP id 3f1490d57ef6-e1a74ee4c75so82569276.3 for ; Wed, 04 Sep 2024 13:29:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1725481773; x=1726086573; darn=vger.kernel.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=0zVu5c8eaWCvvk5EBO+fDxyCoY1kZOiTk4/wXy3iZ/k=; b=DkyRDa6XmYumakFghCrNcOBmEOvXbsH5FISDD4gJ67hadBHnFL5XTza/W3cfpy3q+m ruxY0iNipBvRq3cDFiE6Okc73JXO5RRz8l+8VcL9N5gdHjVW0pov9w9J5x7OMJAbPuSX 1o6CnpuJW6PrFzhZQ9Strozw1DEijSZOJRrAEC3ZWVbybq8cvjb46PAcReCVxWaHXAIW 6Zhx+2nFaDPRQCDxnsvzIDlgPjrrkMvuJTLgO9qY7u+yeOxnrenL1rHYzu1rTHhi70oP HspqZMPCxXCoSISGRLO9ZzWgGw9OZyoXhQ6NZtMRjiBlma/PVzIqcfqQS1NfTkhM/YAy 77ng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725481773; x=1726086573; 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=0zVu5c8eaWCvvk5EBO+fDxyCoY1kZOiTk4/wXy3iZ/k=; b=V9pTIZo0ZmVsmfhtv+cID2MUDKL1SCF5GcLqrOVBbqwBowIoCXlplr6GhKp7sKAJUn ZTVTbowhF/B/g7VUynzlXQkrCRCg0ukTxfYnwYW2gx1nB+CONop3uFD9s4apitjLiOFn wJjVGyOUwklOBHGjq7DM82NNF22qo0ut8o1yV3J5X3zNLDNZNWrPX8MPesjQ3aNYahKK /Kis3jMnzOX9Pj3+ZhjLLR+5LxDPWzAIMKv0VS/MUXXurB1INM02RYbhG/Qy7uHXmV17 MqpBO8KDgKvaT7kiFp5Y6+q1Dq4dV0SflW9H9b7dalcDLPd4lwB9XiArNsmBv4OcBi6m AZww== X-Forwarded-Encrypted: i=1; AJvYcCUUFsMTHooEu4wuEAD4vKg8XVvVAxztwrvNa4mIC44hSOqAZAFEguimCPuKl6RqHN8arCzD1x4xe5xdwdFk@vger.kernel.org X-Gm-Message-State: AOJu0YwChKkXXOy/pnFauCkFWmX0t8E7Qqs1pN25paowfzEAUTOUFkrb rKGVJm/VXe4pfr1fCgfAKM+CmtuZjiXdqMxnndOvIs1djyqzFcFxJy6FkQqyeBE= X-Google-Smtp-Source: AGHT+IFDFkzBbYpd9NLUztwVk1+npkB1ydP+fmMEnnhVo46mYhG5bHnYSQtzbSERWVZmLM1FzjZsHw== X-Received: by 2002:a05:6902:1b04:b0:e16:49d6:43e3 with SMTP id 3f1490d57ef6-e1a7a028d68mr20024706276.22.1725481773240; Wed, 04 Sep 2024 13:29:33 -0700 (PDT) Received: from localhost (syn-076-182-020-124.res.spectrum.com. [76.182.20.124]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6c520418d34sm1550836d6.119.2024.09.04.13.29.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2024 13:29:32 -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 11/18] fanotify: add a helper to check for pre content events Date: Wed, 4 Sep 2024 16:28:01 -0400 Message-ID: <288ff84b68a6b79a0526b6d53df2df5d184b5232.1725481503.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 We want to emit events during page fault, and calling into fanotify could be expensive, so add a helper to allow us to skip calling into fanotify from page fault. This will also be used to disable readahead for content watched files which will be handled in a subsequent patch. Reviewed-by: Christian Brauner Signed-off-by: Josef Bacik Reviewed-by: Amir Goldstein --- fs/notify/fsnotify.c | 15 +++++++++++++++ include/linux/fsnotify_backend.h | 14 ++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/fs/notify/fsnotify.c b/fs/notify/fsnotify.c index 1ca4a8da7f29..30badf8ba36c 100644 --- a/fs/notify/fsnotify.c +++ b/fs/notify/fsnotify.c @@ -201,6 +201,21 @@ static inline bool fsnotify_object_watched(struct inode *inode, __u32 mnt_mask, return mask & marks_mask & ALL_FSNOTIFY_EVENTS; } +#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS +bool fsnotify_file_has_pre_content_watches(struct file *file) +{ + struct inode *inode = file_inode(file); + __u32 mnt_mask = real_mount(file->f_path.mnt)->mnt_fsnotify_mask; + + if (!(inode->i_sb->s_type->fs_flags & FS_ALLOW_HSM)) + return false; + + return fsnotify_object_watched(inode, mnt_mask, + FSNOTIFY_PRE_CONTENT_EVENTS); +} +#endif + + /* * Notify this dentry's parent about a child's events with child name info * if parent is watching or if inode/sb/mount are interested in events with diff --git a/include/linux/fsnotify_backend.h b/include/linux/fsnotify_backend.h index 276320846bfd..b495a0676dd3 100644 --- a/include/linux/fsnotify_backend.h +++ b/include/linux/fsnotify_backend.h @@ -900,6 +900,15 @@ static inline void fsnotify_init_event(struct fsnotify_event *event) INIT_LIST_HEAD(&event->list); } +#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS +bool fsnotify_file_has_pre_content_watches(struct file *file); +#else +static inline bool fsnotify_file_has_pre_content_watches(struct file *file) +{ + return false; +} +#endif /* CONFIG_FANOTIFY_ACCESS_PERMISSIONS */ + #else static inline int fsnotify(__u32 mask, const void *data, int data_type, @@ -938,6 +947,11 @@ static inline u32 fsnotify_get_cookie(void) static inline void fsnotify_unmount_inodes(struct super_block *sb) {} +static inline bool fsnotify_file_has_pre_content_watches(struct file *file) +{ + return false; +} + #endif /* CONFIG_FSNOTIFY */ #endif /* __KERNEL __ */ From patchwork Wed Sep 4 20:28:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13791347 Received: from mail-yb1-f182.google.com (mail-yb1-f182.google.com [209.85.219.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D1F701E766C for ; Wed, 4 Sep 2024 20:29:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725481777; cv=none; b=kI5rBP61D1UzynpEQikXTKeMEviHqOrGMk5Knr50WDMF3RI6gm7eWRpHixKGVVC0eadsY4atIQzWsxeH+QMN6Fi5u3CZepE56XuzcQmyV2+dry2GXUiSzPscR2R5A7t7NaerfQGHuvSv32ugdmIpY+8QstV8YiPs/rP2gcw2694= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725481777; c=relaxed/simple; bh=RuLIdPYLf2U7vdfwd0GY223a6ukGYtQumd4qAvMMF1s=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lQ0ae+/+msNVk1nTf6uTWkjKRKGLirUwiiSD1jzqxTR3qv1rO9ku7biA1WANS2oit72BmbcBV2BjEscwHk3dLhJ5v+BMDtUrVe0lkNT3VBsTay/R8U23DdcZe05tHhxh4J0FweZi+MnPVRBF6JYObZ6MBnaRhOUK46PMS7VyYQE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com; spf=none smtp.mailfrom=toxicpanda.com; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b=gS/btsbC; arc=none smtp.client-ip=209.85.219.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b="gS/btsbC" Received: by mail-yb1-f182.google.com with SMTP id 3f1490d57ef6-e1a7c25e350so70921276.3 for ; Wed, 04 Sep 2024 13:29:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1725481775; x=1726086575; darn=vger.kernel.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=NQ0y3/dQBSJxG2OyPRkhEh3Vj0wjByustgiYia+MZqs=; b=gS/btsbCkaVsibWHpBg0kt0qu5RQqWF1TLzBkDq2d0ptRda1D93Q1KoT1oyTy4aMFd DOL23tsVJmF+KRkK9I6bf134iCQTXMv3gsbCpAH8HeUdxZXn+l3WuiHESKDFmKCc13MZ QI61Wy7lrNJkbd/Rk9Tdd9ZmzZWHMqS83anl5dTSDCk2+zudxpId4Wjjv3GJlJ6C9Ken TrLdmkb6RxsCX7E8z0Pi3HFMJnz+ruNcZWaaCcfNpLLU27iRzS6r+S70Y6UtIYFOyGGt 9XJ2MR53/0SDUm1PCJa3eQbJdSfpX45T09Cye3/l1ZuxayFIO7C5pKuIdCHTcHwHWGZu Yl/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725481775; x=1726086575; 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=NQ0y3/dQBSJxG2OyPRkhEh3Vj0wjByustgiYia+MZqs=; b=flNQN54BZphowQgpO0CquidDYUjog1ro9Vlr34yBrA5DWGbuGnSD+gWfkqnB4GUM5e IRDo8UASKxNqSL35AVjQ7f8q9lBsTqHrZvB//f5XI9O6LVS6YPW/dcnGWnKP5nsLjhh4 tByJJnRbMCx1EHSQfiNTD4WE8jOGHokBLplfAvgEwcl3AW7xwRVI6rBDdqBfYDE95SvG CiElsoiM9dYB+uys81jqi+HaVaZvtxlYH1rEN4gbV9ZtKff+zpuMaycoxh2gCXW8lg6Y 6LMtNZMWDBNCVi04dh8S48IKwhZeCGzdrsNFbEy7Oo5rl1SQagvva6Twt69Qgbq8Vl3a idVQ== X-Forwarded-Encrypted: i=1; AJvYcCU3fPFyahQczdhpWpkC1s4a53p5VtpoZUr57Z6ExwmLSomXgWuRD1eQpI9qJ98lrvjQ5BW6RM62Ru5EPtzg@vger.kernel.org X-Gm-Message-State: AOJu0Yzuc2AFXmS2BWCGmjy3/UnzIKqJcTPvc4UsbvZz7cNSj9qTtEQe 2dK76VrzpDIyAle2ku6DaCenMM7uPlk1ymIcj4U/t2/n2j5sk4q0Pz2NjItcxxY= X-Google-Smtp-Source: AGHT+IGBp2PIdRLM2XqIHSfjLgeh3BUtPWJbF41aaoGgznmohKpzreGJIvh+qUevQhYUH2lFWA7hgg== X-Received: by 2002:a05:6902:138d:b0:e0e:8adf:2e80 with SMTP id 3f1490d57ef6-e1a7a1a48abmr20532053276.44.1725481774821; Wed, 04 Sep 2024 13:29:34 -0700 (PDT) Received: from localhost (syn-076-182-020-124.res.spectrum.com. [76.182.20.124]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6c5201e4720sm1608386d6.51.2024.09.04.13.29.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2024 13:29:34 -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 12/18] fanotify: disable readahead if we have pre-content watches Date: Wed, 4 Sep 2024 16:28:02 -0400 Message-ID: <5ce248ad6e7b551c6d566fd4580795f7a3495352.1725481503.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 With page faults we can trigger readahead on the file, and then subsequent faults can find these pages and insert them into the file without emitting an fanotify event. To avoid this case, disable readahead if we have pre-content watches on the file. This way we are guaranteed to get an event for every range we attempt to access on a pre-content watched file. Reviewed-by: Christian Brauner Signed-off-by: Josef Bacik Reviewed-by: Amir Goldstein --- mm/filemap.c | 12 ++++++++++++ mm/readahead.c | 13 +++++++++++++ 2 files changed, 25 insertions(+) diff --git a/mm/filemap.c b/mm/filemap.c index ca8c8d889eef..8b1684b62177 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -3122,6 +3122,14 @@ static struct file *do_sync_mmap_readahead(struct vm_fault *vmf) unsigned long vm_flags = vmf->vma->vm_flags; unsigned int mmap_miss; + /* + * If we have pre-content watches we need to disable readahead to make + * sure that we don't populate our mapping with 0 filled pages that we + * never emitted an event for. + */ + if (fsnotify_file_has_pre_content_watches(file)) + return fpin; + #ifdef CONFIG_TRANSPARENT_HUGEPAGE /* Use the readahead code, even if readahead is disabled */ if ((vm_flags & VM_HUGEPAGE) && HPAGE_PMD_ORDER <= MAX_PAGECACHE_ORDER) { @@ -3190,6 +3198,10 @@ static struct file *do_async_mmap_readahead(struct vm_fault *vmf, struct file *fpin = NULL; unsigned int mmap_miss; + /* See comment in do_sync_mmap_readahead. */ + if (fsnotify_file_has_pre_content_watches(file)) + return fpin; + /* If we don't want any read-ahead, don't bother */ if (vmf->vma->vm_flags & VM_RAND_READ || !ra->ra_pages) return fpin; diff --git a/mm/readahead.c b/mm/readahead.c index 817b2a352d78..bc068d9218e3 100644 --- a/mm/readahead.c +++ b/mm/readahead.c @@ -128,6 +128,7 @@ #include #include #include +#include #include "internal.h" @@ -674,6 +675,14 @@ void page_cache_sync_ra(struct readahead_control *ractl, { bool do_forced_ra = ractl->file && (ractl->file->f_mode & FMODE_RANDOM); + /* + * If we have pre-content watches we need to disable readahead to make + * sure that we don't find 0 filled pages in cache that we never emitted + * events for. + */ + if (ractl->file && fsnotify_file_has_pre_content_watches(ractl->file)) + return; + /* * Even if readahead is disabled, issue this request as readahead * as we'll need it to satisfy the requested range. The forced @@ -704,6 +713,10 @@ void page_cache_async_ra(struct readahead_control *ractl, if (!ractl->ra->ra_pages) return; + /* See the comment in page_cache_sync_ra. */ + if (ractl->file && fsnotify_file_has_pre_content_watches(ractl->file)) + return; + /* * Same bit is used for PG_readahead and PG_reclaim. */ From patchwork Wed Sep 4 20:28:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13791348 Received: from mail-qt1-f182.google.com (mail-qt1-f182.google.com [209.85.160.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 764301E8B72 for ; Wed, 4 Sep 2024 20:29:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725481779; cv=none; b=kitsTwmJV9/kldltmruCuyFYRxsEqUhCPL/QzFLurn/e432hgdVlOWyZb0qhM7k9/hi5doTwJdy3nGqjfWlQ4mvKsM2WQh1cq+aqwrGNmMyhLFt70h8tFOZM9h30EHZTEYu//dtNDVJA3zzJ7SYBgeu+ogoPAEanV2HCQ1j0Hao= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725481779; c=relaxed/simple; bh=Q3VYbwM4CKv9HZE1IBds6JfYLVoY9EDuZvS5XcFcfQs=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=AxmbvDoI7GhxTH10ETFcA+eMyXDtyL/9+u0I2Jq9/5eZ8XWdJPhGFMHht22pX/jHOfGPCFLEJbNuXt9p/T9FhYHto5LHtZ7xKAzMeQq5zO8YqIHwhFlUIBG7h5wbaZEOP+bhdL2xKBUA1jwaW+6hxp8UjO/IxHV4OhT2J96gtHQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com; spf=none smtp.mailfrom=toxicpanda.com; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b=rFGXlMrQ; arc=none smtp.client-ip=209.85.160.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b="rFGXlMrQ" Received: by mail-qt1-f182.google.com with SMTP id d75a77b69052e-45685a3b1d8so11019451cf.1 for ; Wed, 04 Sep 2024 13:29:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1725481776; x=1726086576; darn=vger.kernel.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=IDO1PsR4jqBoDIDFjuXXy+JZDMdVbZ460dPfM2iRCeI=; b=rFGXlMrQlBl1XNlaEGF8dAN0WgYjbKKa2fUYRnD88abRpM7IISbC2CwttOfeqWimbN 8OctDG2LYr2ldzfkxiaV7ZR06BtkKqgca68/+gTItZqXZU82yBO5XNSCEz+WXfLPPgHa vzIbuoWagyRvIQ/DUW0QqlHiTuyQRmgKMp2KW3SpL+7+L3FhZs7bCOQYRptyiqN+AVvp BE4jWYPDXAyrPYnclkAJoTiO0UqI5D7uiAOiwm5BPBa1WKf+1flXgkAbElD+mhOBZo/a kIZKYVjYU8Y24/UNO2dTNknoLkJWv/XiJlbZ4gGIC77DHU9GdXjQ/K5jazKQsDgDE3e3 YGVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725481776; x=1726086576; 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=IDO1PsR4jqBoDIDFjuXXy+JZDMdVbZ460dPfM2iRCeI=; b=FtW4r1hyVv5ogd13wp7VOfoMmfaKVgkUcUayJ53x54KybMjadrJT+iNfSept81CRN0 ac2cG4rqVi0qSJVxUJhfmTUQWKdIx/a4TEuKChheUNb4gCwI4JKdJnF+fA438zz2nwtJ 71eTe4no1gInUGqNZbX+XOx6iC8tb/uW4z9wRoB+uBHKo0WowQfGxrRj6wM6H6jxhwzT 1hOUk0qG4W07WlWMPabXMLA3rsHPES8wk4RKidwizpQyKR8HJ/XjxbGJ9M/W/9JJygGn pdf7dr5xrHsj7EgGO88Xrqd149yBop9gjpFDZrHnpWSauc7VikIerodv0wxJyrdpDuaY 0JSw== X-Forwarded-Encrypted: i=1; AJvYcCVlcUd2PTT/0N4DyRX1USvT088ZsZqqPJQptFqDczMYcrXescrwxEYxsIpQaeapPHCQAQ8z/IAMhBQe7Z+C@vger.kernel.org X-Gm-Message-State: AOJu0Yze55fSL6DekJUS7MUHhiXii607DLGzHsEhwZ1jIaS7B9mRx4DB kHgPgFZxYWW4lPzfofGmDrw2fyjiP/vON65rmz8ufEQ1i4AwQuJ3ce1vqEX378E= X-Google-Smtp-Source: AGHT+IFq1hdENBAe17o88j4Kfx5nRP4kOzH63jEqqDXfFeqQM7cpPGSq1qeT7V7dadPcUVEqgl24EQ== X-Received: by 2002:a05:622a:1311:b0:447:f8b1:aeb9 with SMTP id d75a77b69052e-457f8bc54b2mr57102421cf.16.1725481776515; Wed, 04 Sep 2024 13:29:36 -0700 (PDT) Received: from localhost (syn-076-182-020-124.res.spectrum.com. [76.182.20.124]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-45801b4cacdsm1493041cf.42.2024.09.04.13.29.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2024 13:29:35 -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 13/18] mm: don't allow huge faults for files with pre content watches Date: Wed, 4 Sep 2024 16:28:03 -0400 Message-ID: <80e7221d9679032c2d5affc317957114e5d77657.1725481503.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 There's nothing stopping us from supporting this, we could simply pass the order into the helper and emit the proper length. However currently there's no tests to validate this works properly, so disable it until there's a desire to support this along with the appropriate tests. Reviewed-by: Christian Brauner Signed-off-by: Josef Bacik Reviewed-by: Amir Goldstein --- mm/memory.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/mm/memory.c b/mm/memory.c index d10e616d7389..3010bcc5e4f9 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -78,6 +78,7 @@ #include #include #include +#include #include @@ -5252,8 +5253,17 @@ static vm_fault_t do_numa_page(struct vm_fault *vmf) static inline vm_fault_t create_huge_pmd(struct vm_fault *vmf) { struct vm_area_struct *vma = vmf->vma; + struct file *file = vma->vm_file; if (vma_is_anonymous(vma)) return do_huge_pmd_anonymous_page(vmf); + /* + * Currently we just emit PAGE_SIZE for our fault events, so don't allow + * a huge fault if we have a pre content watch on this file. This would + * be trivial to support, but there would need to be tests to ensure + * this works properly and those don't exist currently. + */ + if (file && fsnotify_file_has_pre_content_watches(file)) + return VM_FAULT_FALLBACK; if (vma->vm_ops->huge_fault) return vma->vm_ops->huge_fault(vmf, PMD_ORDER); return VM_FAULT_FALLBACK; @@ -5263,6 +5273,7 @@ static inline vm_fault_t create_huge_pmd(struct vm_fault *vmf) static inline vm_fault_t wp_huge_pmd(struct vm_fault *vmf) { struct vm_area_struct *vma = vmf->vma; + struct file *file = vma->vm_file; const bool unshare = vmf->flags & FAULT_FLAG_UNSHARE; vm_fault_t ret; @@ -5277,6 +5288,9 @@ static inline vm_fault_t wp_huge_pmd(struct vm_fault *vmf) } if (vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) { + /* See comment in create_huge_pmd. */ + if (file && fsnotify_file_has_pre_content_watches(file)) + goto split; if (vma->vm_ops->huge_fault) { ret = vma->vm_ops->huge_fault(vmf, PMD_ORDER); if (!(ret & VM_FAULT_FALLBACK)) @@ -5296,9 +5310,13 @@ static vm_fault_t create_huge_pud(struct vm_fault *vmf) #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && \ defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) struct vm_area_struct *vma = vmf->vma; + struct file *file = vma->vm_file; /* No support for anonymous transparent PUD pages yet */ if (vma_is_anonymous(vma)) return VM_FAULT_FALLBACK; + /* See comment in create_huge_pmd. */ + if (file && fsnotify_file_has_pre_content_watches(file)) + return VM_FAULT_FALLBACK; if (vma->vm_ops->huge_fault) return vma->vm_ops->huge_fault(vmf, PUD_ORDER); #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ @@ -5310,12 +5328,16 @@ static vm_fault_t wp_huge_pud(struct vm_fault *vmf, pud_t orig_pud) #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && \ defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) struct vm_area_struct *vma = vmf->vma; + struct file *file = vma->vm_file; vm_fault_t ret; /* No support for anonymous transparent PUD pages yet */ if (vma_is_anonymous(vma)) goto split; if (vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) { + /* See comment in create_huge_pmd. */ + if (file && fsnotify_file_has_pre_content_watches(file)) + goto split; if (vma->vm_ops->huge_fault) { ret = vma->vm_ops->huge_fault(vmf, PUD_ORDER); if (!(ret & VM_FAULT_FALLBACK)) From patchwork Wed Sep 4 20:28:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13791349 Received: from mail-yb1-f179.google.com (mail-yb1-f179.google.com [209.85.219.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 248791E8B85 for ; Wed, 4 Sep 2024 20:29:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725481780; cv=none; b=tEdagO04fuRiXLnyNsgaM1yt6DBEzOSjMf3KNsGzTb8BuETncozZAAMrE6syWI19TapXwQF8ev5aEkAP241tc7rIIrasXbu6VYJFoLms60oRv7yHVLj8+sFQLWrjjOFXnAvSPpSaFuBWMmE/iSZ5VohLq+ulIIZXVDxzAlNjktw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725481780; c=relaxed/simple; bh=K07lhn/HFnq8Dqz7ymidJhSUR1D7kuClGgpotTVtGhM=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lnWgSNjNy0zmuAn1DxyE2X8C0CqjsZE/y3Hi2/S1KOQwmIqCo/6iHizs2oVSYHJQ0dSxihvcF6Q39fLYsgh0g0+Q9fDoeacnCvL6Ssq5+1GeQMBYzKd9N8+327rlwTyQNfO5SY9sKTY9/pT8kWxV11H6QAAlk683fi1Zknz6r/E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com; spf=none smtp.mailfrom=toxicpanda.com; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b=t6PmsxpM; arc=none smtp.client-ip=209.85.219.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b="t6PmsxpM" Received: by mail-yb1-f179.google.com with SMTP id 3f1490d57ef6-e1a9dc3f0a3so86544276.0 for ; Wed, 04 Sep 2024 13:29:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1725481778; x=1726086578; darn=vger.kernel.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=TdpAgT058g8D2uASHL0DyNBaXA20e/8A0vSavuo8wh4=; b=t6PmsxpMsywtEph2siajdK60/OcSRpMQPtKJnGROJRG9kI1Gp1/rNxorG/MqqLH+l+ H14YTRgFOkNBfL6siP+FKyN2IeTCw1iGaeGpmoQc/iLaRuWWopFoXdDK+Tc9TeNtK+/5 h67DTT4lnKQYncY5hWZojuuRih7kCOi8eAh25/kTc44WLtgEil3oC7NLP9wGJoF6etXL UiBU8d//Dut8uTuJKM+zXAVdAj8cBoTJDfqf7louS1A7C7Go4vro7X1XWicgya2693hF h5Ixr8/x1GnSx7ZxgwaOIWj9F/if3i8EHDiRuipRj6DGGcrfxI0KPPYbkLEyZy05uTwh 6T9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725481778; x=1726086578; 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=TdpAgT058g8D2uASHL0DyNBaXA20e/8A0vSavuo8wh4=; b=XrVUgSXYTxbR6yE5asqXg7/3ChK73HgepO0voKhrIJviCw62lfEzasgggkXlLeUhAu tRmIPu9xzSR3Y+IIUKVvOvLydi0WKriLSWP19OFeDVyqCrT2UIHTVze+EwWcpbBTSi5Q yv0I0rR5lOp4xn96sJwW+t472DB5Efm/wf5L7P8v/5rDBeuFLABWzIt+D1FfzJpDur3O Nv0ygFuFkAANvyb1CbqilSXmCFV9wV/6V2A6d9KwJEeHA1IwJ5DbJf2/OsaQbZJ2UbLE bOrG5u0+nCKwoPkyHA8thqrQIsFUnHtxj3CyjSb7guvQGxFsBJAy4UqApZchc+iE48E0 7WXw== X-Forwarded-Encrypted: i=1; AJvYcCUXBDUfyhtASlWUnWaVj9jSMPo2Q1FKZxPBFLKbS8LFRyrX3E8INkfFfSzuhu5Msn3JYNQ03eLDvDlx6n0q@vger.kernel.org X-Gm-Message-State: AOJu0YyQW5Hmi60+ZFnkMsMrYeI+Qj6GNYPfqTmIfP9k1gwqEYNx9tB1 QNpM6iOQ9E4gGRdgKo68r5JoCo7FRL7qAA67KwpXyw48g41ZxwCnXWNXOqmbFpg= X-Google-Smtp-Source: AGHT+IHeHitsPcQ1kfVrPGq0b4GF0aiDUGxULm3YTFoSiEMXAET19y3p8EGpUVDmPCTd+KLj6bMEOQ== X-Received: by 2002:a05:6902:70a:b0:e0e:7b3d:53fe with SMTP id 3f1490d57ef6-e1a79fd884emr23296230276.18.1725481778061; Wed, 04 Sep 2024 13:29:38 -0700 (PDT) Received: from localhost (syn-076-182-020-124.res.spectrum.com. [76.182.20.124]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6c520418d34sm1551586d6.119.2024.09.04.13.29.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2024 13:29:37 -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 14/18] fsnotify: generate pre-content permission event on page fault Date: Wed, 4 Sep 2024 16:28:04 -0400 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 FS_PRE_ACCESS or FS_PRE_MODIFY will be generated on page fault depending on the faulting method. This pre-content event is meant to be used by hierarchical storage managers that want to fill in the file content on first read access. Export a simple helper that file systems that have their own ->fault() will use, and have a more complicated helper to be do fancy things with in filemap_fault. Signed-off-by: Josef Bacik --- include/linux/mm.h | 1 + mm/filemap.c | 116 ++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 110 insertions(+), 7 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index ab3d78116043..89665732b404 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -3503,6 +3503,7 @@ extern vm_fault_t filemap_fault(struct vm_fault *vmf); extern vm_fault_t filemap_map_pages(struct vm_fault *vmf, pgoff_t start_pgoff, pgoff_t end_pgoff); extern vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf); +extern vm_fault_t filemap_fsnotify_fault(struct vm_fault *vmf); extern unsigned long stack_guard_gap; /* Generic expand stack which grows the stack according to GROWS{UP,DOWN} */ diff --git a/mm/filemap.c b/mm/filemap.c index 8b1684b62177..b2d29947ce7f 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -46,6 +46,7 @@ #include #include #include +#include #include #include #include "internal.h" @@ -3112,13 +3113,13 @@ static int lock_folio_maybe_drop_mmap(struct vm_fault *vmf, struct folio *folio, * that. If we didn't pin a file then we return NULL. The file that is * returned needs to be fput()'ed when we're done with it. */ -static struct file *do_sync_mmap_readahead(struct vm_fault *vmf) +static struct file *do_sync_mmap_readahead(struct vm_fault *vmf, + struct file *fpin) { struct file *file = vmf->vma->vm_file; struct file_ra_state *ra = &file->f_ra; struct address_space *mapping = file->f_mapping; DEFINE_READAHEAD(ractl, file, ra, mapping, vmf->pgoff); - struct file *fpin = NULL; unsigned long vm_flags = vmf->vma->vm_flags; unsigned int mmap_miss; @@ -3190,12 +3191,12 @@ static struct file *do_sync_mmap_readahead(struct vm_fault *vmf) * was pinned if we have to drop the mmap_lock in order to do IO. */ static struct file *do_async_mmap_readahead(struct vm_fault *vmf, - struct folio *folio) + struct folio *folio, + struct file *fpin) { struct file *file = vmf->vma->vm_file; struct file_ra_state *ra = &file->f_ra; DEFINE_READAHEAD(ractl, file, ra, file->f_mapping, vmf->pgoff); - struct file *fpin = NULL; unsigned int mmap_miss; /* See comment in do_sync_mmap_readahead. */ @@ -3260,6 +3261,93 @@ static vm_fault_t filemap_fault_recheck_pte_none(struct vm_fault *vmf) return ret; } +/* + * If we have pre-content watches on this file we will need to emit an event for + * this range. We will handle dropping the lock and emitting the event. + * + * If FAULT_FLAG_RETRY_NOWAIT is set then we'll return VM_FAULT_RETRY. + * + * If no event was emitted then *fpin will be NULL and we will return 0. + * + * If any error occurred we will return VM_FAULT_SIGBUS, *fpin could still be + * set and will need to have fput() called on it. + * + * If we emitted the event then we will return 0 and *fpin will be set, this + * must have fput() called on it, and the caller must call VM_FAULT_RETRY after + * any other operations it does in order to re-fault the page and make sure the + * appropriate locking is maintained. + * + * Return: the appropriate vm_fault_t return code, 0 on success. + */ +static vm_fault_t __filemap_fsnotify_fault(struct vm_fault *vmf, + struct file **fpin) +{ + struct file *file = vmf->vma->vm_file; + loff_t pos = vmf->pgoff << PAGE_SHIFT; + int mask = (vmf->flags & FAULT_FLAG_WRITE) ? MAY_WRITE : MAY_ACCESS; + int ret; + + /* + * We already did this and now we're retrying with everything locked, + * don't emit the event and continue. + */ + if (vmf->flags & FAULT_FLAG_TRIED) + return 0; + + /* No watches, return NULL. */ + if (!fsnotify_file_has_pre_content_watches(file)) + return 0; + + /* We are NOWAIT, we can't wait, just return EAGAIN. */ + if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT) + return VM_FAULT_RETRY; + + /* + * If this fails then we're not allowed to drop the fault lock, return a + * SIGBUS so we don't errantly populate pagecache with bogus data for + * this file. + */ + *fpin = maybe_unlock_mmap_for_io(vmf, *fpin); + if (*fpin == NULL) + return VM_FAULT_SIGBUS; + + /* + * We can't fput(*fpin) at this point because we could have been passed + * in fpin from a previous call. + */ + ret = fsnotify_file_area_perm(*fpin, mask, &pos, PAGE_SIZE); + if (ret) + return VM_FAULT_SIGBUS; + + return 0; +} + +/** + * filemap_fsnotify_fault - maybe emit a pre-content event. + * @vmf: struct vm_fault containing details of the fault. + * + * If we have a pre-content watch on this file we will emit an event for this + * range. If we return anything the fault caller should return immediately, we + * will return VM_FAULT_RETRY if we had to emit an event, which will trigger the + * fault again and then the fault handler will run the second time through. + * + * Return: a bitwise-OR of %VM_FAULT_ codes, 0 if nothing happened. + */ +vm_fault_t filemap_fsnotify_fault(struct vm_fault *vmf) +{ + struct file *fpin = NULL; + vm_fault_t ret; + + ret = __filemap_fsnotify_fault(vmf, &fpin); + if (fpin) { + fput(fpin); + if (!ret) + ret = VM_FAULT_RETRY; + } + return ret; +} +EXPORT_SYMBOL_GPL(filemap_fsnotify_fault); + /** * filemap_fault - read in file data for page fault handling * @vmf: struct vm_fault containing details of the fault @@ -3299,6 +3387,17 @@ vm_fault_t filemap_fault(struct vm_fault *vmf) if (unlikely(index >= max_idx)) return VM_FAULT_SIGBUS; + /* + * If we have pre-content watchers then we need to generate events on + * page fault so that we can populate any data before the fault. + */ + ret = __filemap_fsnotify_fault(vmf, &fpin); + if (unlikely(ret)) { + if (fpin) + fput(fpin); + return ret; + } + /* * Do we have something in the page cache already? */ @@ -3309,21 +3408,24 @@ vm_fault_t filemap_fault(struct vm_fault *vmf) * the lock. */ if (!(vmf->flags & FAULT_FLAG_TRIED)) - fpin = do_async_mmap_readahead(vmf, folio); + fpin = do_async_mmap_readahead(vmf, folio, fpin); if (unlikely(!folio_test_uptodate(folio))) { filemap_invalidate_lock_shared(mapping); mapping_locked = true; } } else { ret = filemap_fault_recheck_pte_none(vmf); - if (unlikely(ret)) + if (unlikely(ret)) { + if (fpin) + goto out_retry; return ret; + } /* No page in the page cache at all */ count_vm_event(PGMAJFAULT); count_memcg_event_mm(vmf->vma->vm_mm, PGMAJFAULT); ret = VM_FAULT_MAJOR; - fpin = do_sync_mmap_readahead(vmf); + fpin = do_sync_mmap_readahead(vmf, fpin); retry_find: /* * See comment in filemap_create_folio() why we need From patchwork Wed Sep 4 20:28:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13791350 Received: from mail-qv1-f42.google.com (mail-qv1-f42.google.com [209.85.219.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AEABC1E8B74 for ; Wed, 4 Sep 2024 20:29:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725481782; cv=none; b=E1cGj0EbRMeRf5+83x4e/V8yHrsUEB9bNYAyrYVNqo1KaQad4ymD/v2h3tExA2OTKpIs7nP/UjpWijkAaaQluw1Her2jMvLu/rHB7R04Xj+yQSrGpEmK+U/kQxDKncm+Al/y+9Ewgq2qO8ZlNsqFnP/FvU2ozUtunNTYy01daS8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725481782; c=relaxed/simple; bh=v+fnUlVkBveqAbvI7nBD4gzaysdb8UjtocYcN6UQfDg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=q7MdXFYDaYW+5zloqr3qUDVI1GgvcInUJL0y0qHq81CGE2esDZ29CUvB4kJy2DPXs6fHHqwNF6hBilSdfAqxueQqmX+eayHm8xHVO/yQan8QffHjdYY/G/lDfJcE04HOQYnU6r6S6klzei4vNrqBvNcoHNvMTyED0JTxk1V2O1I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com; spf=none smtp.mailfrom=toxicpanda.com; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b=xozlLTnA; arc=none smtp.client-ip=209.85.219.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b="xozlLTnA" Received: by mail-qv1-f42.google.com with SMTP id 6a1803df08f44-6c35a23b378so42106d6.2 for ; Wed, 04 Sep 2024 13:29:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1725481779; x=1726086579; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=RWEHhx5O7UFZKYepru/YJGo3B9Ht5vMlNzII31DWFDk=; b=xozlLTnAjOeOnOKBTmkEKY5eiNLyE1DduiUA6ApFLdwrnnvNFW1nGyubeZi+xjwymX C4qovljpGP7v9hCBKEe66utga9gbgkPNgls1hAucRI3DGMQ7wVf7ZdfWfxm2EeRwxTK/ rb7w/8gMh+e2Bt9XBgFDDrnL9tBGOY6cMgHQpFe4J0KMtRQ6uGZY2qUErkA1aEJyYnAE sr+0/XNRKG0Lc7tSg9WV5aLIxBSWkjf1zmsxeFXP0E6T+evTvA4VVTQuMyIrrnAXQOUf L2820uYw7Y06pf5Gq01lZqyjnNAuT9oKF4B6CUJIU0mkG49qIM3OkLJsjM+bLOrNSAOi 3twg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725481779; x=1726086579; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=RWEHhx5O7UFZKYepru/YJGo3B9Ht5vMlNzII31DWFDk=; b=Getj7Y24HOYr4hyL6CUgXjDHgHjUdT3Ts+S/pk9tzARpfXgJsz5efUaSxrbGwBRrUo 76xpbAPT8mF3YIjN+xn0RuUtiUBHAQBe1i5kM0vzI7VA8jcwErwNCLckEanmW1eblNDK ncuAJ8PmGbnrxoi1LFsS53WkJjdSsqc2b2FDCrbIJ1k6ot1ntYIpmuqcrdszorI5C5Te UuaLaglFjNidt5yzP+4yhNyzxd5cN/ij+2SXT0DwP2aTwUzxApJnsu5z8jYgSVUID1WD 4WUC55M5tY6vFMe06EGInDVXs01HY0r40uZJ21awgwmdIix5O+WPehABnM/NkI/j5hLh Mycw== X-Forwarded-Encrypted: i=1; AJvYcCX47meU0kbTOnfCLBFqryAfxhtv5KLREaTKQwN63YZynemlMkGJc9oKTgs8jbUGQ6yxrtaeu6SEni+gfxal@vger.kernel.org X-Gm-Message-State: AOJu0Ywz5dBnPSZmZS3Q2SZ77NCHOQF7uiB1c6XqIwH5bYDzugO4W7Ml d+IgobeUH4mW5e62lyY/JZieEbq+npgTotaRNpOoWAym+pwVwRLLu/IyMq2/ogE= X-Google-Smtp-Source: AGHT+IGNwdiKO4Frv0IpCUzBFB05rGHrErtdGiSxtnVsPoIgc9Az5OOPtCc2jc4JjLzRxF9YRhdyFw== X-Received: by 2002:a05:6214:498b:b0:6c3:5c0e:2bbd with SMTP id 6a1803df08f44-6c35c0e3262mr134228936d6.50.1725481779673; Wed, 04 Sep 2024 13:29:39 -0700 (PDT) Received: from localhost (syn-076-182-020-124.res.spectrum.com. [76.182.20.124]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6c52041874asm1535256d6.113.2024.09.04.13.29.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2024 13:29:39 -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 Cc: Kent Overstreet Subject: [PATCH v5 15/18] bcachefs: add pre-content fsnotify hook to fault Date: Wed, 4 Sep 2024 16:28:05 -0400 Message-ID: <89d4bd31a6faa634b9de4cb486498601f042bd7b.1725481503.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 bcachefs has its own locking around filemap_fault, so we have to make sure we do the fsnotify hook before the locking. Add the check to emit the event before the locking and return VM_FAULT_RETRY to retrigger the fault once the event has been emitted. Acked-by: Kent Overstreet Signed-off-by: Josef Bacik --- fs/bcachefs/fs-io-pagecache.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/fs/bcachefs/fs-io-pagecache.c b/fs/bcachefs/fs-io-pagecache.c index a9cc5cad9cc9..ce7968e4fd2f 100644 --- a/fs/bcachefs/fs-io-pagecache.c +++ b/fs/bcachefs/fs-io-pagecache.c @@ -570,6 +570,10 @@ vm_fault_t bch2_page_fault(struct vm_fault *vmf) if (fdm == mapping) return VM_FAULT_SIGBUS; + ret = filemap_fsnotify_fault(vmf); + if (unlikely(ret)) + return ret; + /* Lock ordering: */ if (fdm > mapping) { struct bch_inode_info *fdm_host = to_bch_ei(fdm->host); From patchwork Wed Sep 4 20:28:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13791351 Received: from mail-oi1-f173.google.com (mail-oi1-f173.google.com [209.85.167.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0C33F1E8B76 for ; Wed, 4 Sep 2024 20:29:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725481783; cv=none; b=PoYuXyU6kcLTFXDeIpqgfazSHT9Xv697UZ3IO79gJK/GRQTi/HoPrT0Yl7th7YTMLFAA3xEj/NcBLeoASv9cjcWWAr1+GXzBTLNkLMjB+/old5zK1Nx/4zFmN8jhrYZCpECyX6xEvY/uODOmLz7IOGXU+5xR9kquTFkK1kAY08Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725481783; c=relaxed/simple; bh=wT7ZMo9Fh5VSq1s78YBc49T5i3a6aTxkHXKOOzIvwHc=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=AutPQ7cyg9qu6mAcsoqwQSa8nB9jmCN93cQojGtI9DBItPou1xnNaZ28rxmIOsHSKn+9FvNT2SyUieuvkeGAkUjzJN8IQ5MI3fqg9yIDmQw5AInRh0Ic87Ds/QXcZo5XbjL9D7XTsO+EormivOd4qx6pgUcEeSOLKumE77KayQc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com; spf=none smtp.mailfrom=toxicpanda.com; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b=mrbXx0A6; arc=none smtp.client-ip=209.85.167.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b="mrbXx0A6" Received: by mail-oi1-f173.google.com with SMTP id 5614622812f47-3df1e26de08so2686922b6e.3 for ; Wed, 04 Sep 2024 13:29:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1725481781; x=1726086581; darn=vger.kernel.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=8Z8sRCIfMieTISA3wuOPUcg1dm1vDp7n73Jg7OelYjY=; b=mrbXx0A6WH/L9F/fPw/ggso+cBDHyN1A7GYeDwwPskP0oJm2agj6nqKbMLk43qqGT2 dIVvyTxbpUH2jTTkV2zEziSapTSRkwe6efAUMfczFha383yN2+CuTw6uzf8xVdqMgzVc Oe4qViReBFdwQYDxuq8V9hypR4kflvZbj/wdfKx6ZCWDVupnppn1QlUaFZNAYUfIfVnu Haw2a9VGKovzO/IqjQknOvYinhGLt+MKRU/PCk/caewdkcnHKCiQgGd1iNnlDuARJsUN 2iK7Z3yafW1WPsPa7auDME3hQ5ZKuEI285FanwPCqh879HZ1pc+31u5MRZCVfEOS896L e9IQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725481781; x=1726086581; 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=8Z8sRCIfMieTISA3wuOPUcg1dm1vDp7n73Jg7OelYjY=; b=p79uEjtrrBhkfIiNMwyCFJMBs0LJDa+/95ZW3DmoLmW0C9i62Vy+C+UI8h0gcRdzEg +YAv97t+KK7GPcL5Tng4vwLBYvrJRZ6q5Bu5ft0nkYJL9hsDxvYK2yWd2MZBycNA83BO 7engnVURlzLTP26UTuofOn2E8gZklL8PPJof+W36XI5BuMVl7OCTbxECphdLaSRWVJOb s6ex2lONPBoIUkCwR+Kp10yJHyQT1miOj1jsY0PWv+TbTeP119vWBq40nvEuDxKQT0qq h2g/t+0JXQT/rFdm7uZv4tai86zVe1cj9bQF9GuQAQGJYHbMumQyQldBhxcoAaFMOkuc QMhA== X-Forwarded-Encrypted: i=1; AJvYcCW9ijICqTWoHR/xMWX6alN+50CXfX060zt0TDJBHFn1p9VpeUELVrLtk8Ft3oPChR0A6OitGceZ+GNHqN2m@vger.kernel.org X-Gm-Message-State: AOJu0YwWTsAriv1EM9lRhCEi/dhPRfFO2L6QyVU6kIPCJVaCY7EcjAgs HDROg2oiY/MkDKoCv4ZVaR9HpiR2/xNLjwXg3wdGY7r4bopfp5RRF9O06WnNaNI= X-Google-Smtp-Source: AGHT+IETtHQ5ywMiUmPhP/0afBVUyPAVLJijgZTnYTfqkdZL3bCN8lBGOnkAyW3ORokI+fooL1bn0A== X-Received: by 2002:a05:6808:f13:b0:3e0:483:8bc1 with SMTP id 5614622812f47-3e004838dfdmr11138749b6e.43.1725481781057; Wed, 04 Sep 2024 13:29:41 -0700 (PDT) Received: from localhost (syn-076-182-020-124.res.spectrum.com. [76.182.20.124]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-45801cbc493sm1441111cf.69.2024.09.04.13.29.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2024 13:29:40 -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 16/18] xfs: add pre-content fsnotify hook for write faults Date: Wed, 4 Sep 2024 16:28:06 -0400 Message-ID: <12aebe1a4f039d0234ea74393a39614c0244f7e0.1725481503.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 xfs has it's own handling for write faults, so we need to add the pre-content fsnotify hook for this case. Reads go through filemap_fault so they're handled properly there. Signed-off-by: Josef Bacik --- fs/xfs/xfs_file.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/fs/xfs/xfs_file.c b/fs/xfs/xfs_file.c index 4cdc54dc9686..3e385756017f 100644 --- a/fs/xfs/xfs_file.c +++ b/fs/xfs/xfs_file.c @@ -1283,6 +1283,10 @@ xfs_write_fault( unsigned int lock_mode = XFS_MMAPLOCK_SHARED; vm_fault_t ret; + ret = filemap_fsnotify_fault(vmf); + if (unlikely(ret)) + return ret; + sb_start_pagefault(inode->i_sb); file_update_time(vmf->vma->vm_file); From patchwork Wed Sep 4 20:28:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13791353 Received: from mail-qv1-f53.google.com (mail-qv1-f53.google.com [209.85.219.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6EC851E974A for ; Wed, 4 Sep 2024 20:29:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725481787; cv=none; b=SFXPCRB7g3GHQGBvbk1S1r9N/zR7BIWvRNNeUR38BUXqeqOKvh3/hfMhZ5ZZ3o4/dXKr8NzZu2MnJ/giRWTaMjf4KG715fzxFIVtL0iRXyGqqYpqno2WiVkXraGpuHsrK7ltAlZVXCdAnKH6o2yme6yHa0SGJPk7OhEFmjES8Q4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725481787; c=relaxed/simple; bh=mlpkxER5ZXbhWxBFWH/+nNlNrq7abJQvDN93xPwV0N0=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QqkNEk/KD+wphQnQ+dBmGNDuUopXHUeJTMdCegVSom86L2TFBRX9FkGejMjHpfK4mM5DbJB9J+VUhQyevh7UjLQYvLtwLQ90L71hAavEf1hjPOMIZFTPZPb5S3eG7rHmM9oTalaRKzGdSU3PfdEaMFk62rhyY2o18sMKs2WAVyw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com; spf=none smtp.mailfrom=toxicpanda.com; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b=Kxe1sZSB; arc=none smtp.client-ip=209.85.219.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b="Kxe1sZSB" Received: by mail-qv1-f53.google.com with SMTP id 6a1803df08f44-6bf9ddfc2dcso86706d6.1 for ; Wed, 04 Sep 2024 13:29:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1725481782; x=1726086582; darn=vger.kernel.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=k2g5nWsdtLhh4I3qKZ9kVR48Zxv3ldXz3E9kfB37E8o=; b=Kxe1sZSBe9bkdYZJnRALn/HAAAEZMTeKx5eO5AvTJ1D3YwSVfn+q5BILaNQW8ER2gI mRsqj+KUHJ9z66im7l1ciGFzt9X9oXD3q4FjbbQeStUTGAd6/HYPBSW8vY3KM3CkbRJG Hqfe5P+PTSnrLsdQJr1+s9qkv/NAD7V7PMQtg3S94IcnTfuviqT7TkqwJTke7gTzi0K9 ggVWsakCpTAaB6upon39dRx8Qqe245H8JgVEmnUuvRcNqde+hkeUQXMJUvWslK2tB1UV SnFSr76ju62cmfyRuo5JBgN0ZxMAN/PxBuPJeqtog6yIzIehwqBwFtxPE+Z4TEVlM9op 3pRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725481782; x=1726086582; 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=k2g5nWsdtLhh4I3qKZ9kVR48Zxv3ldXz3E9kfB37E8o=; b=odT4cmL8AzNCevLMBBEoKQ2KFTPTj38DUpZYn8ghSldKaPb0Ju3iDcFC4mPTFHZGNU YP24ruc35jojEtr/FfJHyYWMs/EqJNt9X9wXHF401+Jv6UPbBtm82GkbRfud0/MWXUaw gGGkRfRqiis/wwnx2vl2T3cRuw2KhN34zKAufvYPwKBph/aMOYbAWj7AYujDt7UQUPJc s78QmRqRFH9IqdYGajLkPlsMt3p3vcXdHeVp0/CoENilfBio4Gz4TiV2UFO0L15QTSYR M/zpScd1HHMy5BETzIb2jkkkU8bBsDAblPFrlgsTi/GFXLNuDLxqZz1drozpvPYbpRy9 Z1pw== X-Forwarded-Encrypted: i=1; AJvYcCUluH7Nyc0LGi4qHdy0VYtwQcz0Og5JW9w1VfRDq8k90Se//xELGvoeBPrl5g8pTz7lJeuLxu0svWqWPw+p@vger.kernel.org X-Gm-Message-State: AOJu0YygqeTog22lZRc1mU+E7FPI7HRfU+9mIhhC4VGFut4aj+HF+1WT IhPZEC+FJJPHc10/47VHMWd2J8AjHJNTiAPEtiDi6NIgYucC9tmi/Ta4i9zzv7A= X-Google-Smtp-Source: AGHT+IGcGqc27V/yp+/MPiFXZlq6o8dH/S56y18i5ffiw87nR/uugOanZlUMV6j8hRmZXaYyxJ0xkA== X-Received: by 2002:a05:6214:4a09:b0:6c3:5ebb:9524 with SMTP id 6a1803df08f44-6c35ebb9733mr193501086d6.48.1725481782473; Wed, 04 Sep 2024 13:29:42 -0700 (PDT) Received: from localhost (syn-076-182-020-124.res.spectrum.com. [76.182.20.124]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6c520419ecfsm1552666d6.126.2024.09.04.13.29.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2024 13:29:42 -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 17/18] btrfs: disable defrag on pre-content watched files Date: Wed, 4 Sep 2024 16:28:07 -0400 Message-ID: <367fe83ae53839b1e88dc00b5b420035496d28ff.1725481503.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 We queue up inodes to be defrag'ed asynchronously, which means we do not have their original file for readahead. This means that the code to skip readahead on pre-content watched files will not run, and we could potentially read in empty pages. Handle this corner case by disabling defrag on files that are currently being watched for pre-content events. Signed-off-by: Josef Bacik --- fs/btrfs/ioctl.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c index e0a664b8a46a..529f7416814f 100644 --- a/fs/btrfs/ioctl.c +++ b/fs/btrfs/ioctl.c @@ -2640,6 +2640,15 @@ static int btrfs_ioctl_defrag(struct file *file, void __user *argp) goto out; } + /* + * Don't allow defrag on pre-content watched files, as it could + * populate the page cache with 0's via readahead. + */ + if (fsnotify_file_has_pre_content_watches(file)) { + ret = -EINVAL; + goto out; + } + if (argp) { if (copy_from_user(&range, argp, sizeof(range))) { ret = -EFAULT; From patchwork Wed Sep 4 20:28:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13791352 Received: from mail-qt1-f169.google.com (mail-qt1-f169.google.com [209.85.160.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 169C31E9775 for ; Wed, 4 Sep 2024 20:29:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725481786; cv=none; b=qlc/QzfUPljxvaj6FsRuuBY9ORHscw8sMhkYbfTnwVro/62ynDl2DSPhk6BiM+52p6Mm6IIXCh+xgPh66xGL4u6Q4DWW5BzArhDVySKq/0BbBSAEPP6wRlXGfxIAxlTDrTJ1JYaAUQBQdmZbTLFiNfkwooT7H8FAGkGfvXIKwp4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725481786; c=relaxed/simple; bh=gMZpVewk5XvpBsYlFMNDabJsDOEQn4P/spwMBIBwxlA=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nB3cqQ2xojmY/2RROMmsqGxcyor+OV8bjbdexKuLsfXEFszUoTkW8DCGm4JOrwbrq9syEwmBEX1TI/3yYia/hYhwuBZ0Rvbxd1fOYiCZKTLp2zU/yvaQyd8epjUU91prtIsmridxJe+Wgni137cffQjc3EQ9FSFJuFI5bpdLkHo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com; spf=none smtp.mailfrom=toxicpanda.com; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b=WP7o3YDW; arc=none smtp.client-ip=209.85.160.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b="WP7o3YDW" Received: by mail-qt1-f169.google.com with SMTP id d75a77b69052e-456954d0396so174921cf.3 for ; Wed, 04 Sep 2024 13:29:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1725481784; x=1726086584; darn=vger.kernel.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=Eq+9jML2+1Z0SY+HFrzR7GffDKDG9jp4/RSk9gnr/bU=; b=WP7o3YDWx4d2AEcbHWUemYHqy2KcYbp4F/W73qz7k/eGQrFuLVMCTseNBMISFnAqig 8OKEKWyuM6reaNGQkDiafQmpjqEXDBLyuCPt5z1YMFvDYFPsUg0qRYltUFCM5IkMbEI3 NkTeHSLNTir2erN7Fj6NGkL7m7zv2KpZ9kGWB/Yj2vxcyyaHSu0HTuMSG56nvSn4kX/5 +cQpmRgedx/LNfNeHIZgIEPVW1wAcuAMWqQS/NnhZU4Qn7g6YqgItx0wmOMvAMrPgz6m 6+3MSBfAZqm8TbulyxLPeyNVlzgzzDSBpj6jWGiTQCscIqvghf2D5RvTiyNUORLi7sur I2pA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725481784; x=1726086584; 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=Eq+9jML2+1Z0SY+HFrzR7GffDKDG9jp4/RSk9gnr/bU=; b=O00em9/ivpEUV/aFkBfgAa67i+ZuzkOVYVq1NsShFGvNM8eAU/zqRrgLIJ4QWEyC10 TlTfSfvORMSUjJzBlx7II/6cw+Z0JEU7wkyUn0SZuu4IJHwWrqP5GYPXMQvmAvntaJ3H vIQxr3znewKyJbECBMY5Z6iSFkwr6SP7YJJy8BNhSIHcjQEGbWZjo4IQw/u/2tNKWsYa lfVc/WLebpqXVPg1iAi4mBWHekxlmHKD9r4A8otE4urH5jLQuL198mc+iIvG354Jh3GE rotDYVi5YzXxG6JjdLntcc72U4T4ULh8t824uWff2SSfV3L88+PDVJw6KlWzU8TSfg8H B82g== X-Forwarded-Encrypted: i=1; AJvYcCUlUsU67h9a1crwIlp94gLUiBTA2LSo9y/yyIwW0wuJCoounz4JXJ7Ark4yr6+vbGTqhSoWIF6TP3bB6kss@vger.kernel.org X-Gm-Message-State: AOJu0YxbmX/7uk86nsJCgsDRdGcz8ZkTlK8SWXwG8CliDZHLf1NVJeZc T9EK9YcquHmBhcltjKKpIxsjzeWWtishtk0n5+nZZ+B9v86PumL5z7L1aEd8HWw= X-Google-Smtp-Source: AGHT+IEW2/ZH57qdIHT1xYE99y1ro7rMeuspqZzclDk+WG9y2YYBgpGHJd3Dr2WZ9zk9cnANiDJM0Q== X-Received: by 2002:a05:622a:993:b0:456:87c1:59c7 with SMTP id d75a77b69052e-4571a013180mr198617071cf.57.1725481784189; Wed, 04 Sep 2024 13:29:44 -0700 (PDT) Received: from localhost (syn-076-182-020-124.res.spectrum.com. [76.182.20.124]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-45801b5312bsm1502351cf.44.2024.09.04.13.29.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2024 13:29:43 -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 18/18] fs: enable pre-content events on supported file systems Date: Wed, 4 Sep 2024 16:28:08 -0400 Message-ID: <33151057684a62a89b45466d53671c6232c34a68.1725481503.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Now that all the code has been added for pre-content events, and the various file systems that need the page fault hooks for fsnotify have been updated, add FS_ALLOW_HSM to the currently tested file systems. Signed-off-by: Josef Bacik --- fs/bcachefs/fs.c | 2 +- fs/btrfs/super.c | 3 ++- fs/ext4/super.c | 6 +++--- fs/xfs/xfs_super.c | 2 +- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/fs/bcachefs/fs.c b/fs/bcachefs/fs.c index 3a5f49affa0a..f889a105643b 100644 --- a/fs/bcachefs/fs.c +++ b/fs/bcachefs/fs.c @@ -2124,7 +2124,7 @@ static struct file_system_type bcache_fs_type = { .name = "bcachefs", .init_fs_context = bch2_init_fs_context, .kill_sb = bch2_kill_sb, - .fs_flags = FS_REQUIRES_DEV, + .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_HSM, }; MODULE_ALIAS_FS("bcachefs"); diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c index 0eda8c21d861..201ed90a6083 100644 --- a/fs/btrfs/super.c +++ b/fs/btrfs/super.c @@ -2193,7 +2193,8 @@ static struct file_system_type btrfs_fs_type = { .init_fs_context = btrfs_init_fs_context, .parameters = btrfs_fs_parameters, .kill_sb = btrfs_kill_super, - .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA | FS_ALLOW_IDMAP, + .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA | + FS_ALLOW_IDMAP | FS_ALLOW_HSM, }; MODULE_ALIAS_FS("btrfs"); diff --git a/fs/ext4/super.c b/fs/ext4/super.c index e72145c4ae5a..a042216fb370 100644 --- a/fs/ext4/super.c +++ b/fs/ext4/super.c @@ -137,7 +137,7 @@ static struct file_system_type ext2_fs_type = { .init_fs_context = ext4_init_fs_context, .parameters = ext4_param_specs, .kill_sb = ext4_kill_sb, - .fs_flags = FS_REQUIRES_DEV, + .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_HSM, }; MODULE_ALIAS_FS("ext2"); MODULE_ALIAS("ext2"); @@ -153,7 +153,7 @@ static struct file_system_type ext3_fs_type = { .init_fs_context = ext4_init_fs_context, .parameters = ext4_param_specs, .kill_sb = ext4_kill_sb, - .fs_flags = FS_REQUIRES_DEV, + .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_HSM, }; MODULE_ALIAS_FS("ext3"); MODULE_ALIAS("ext3"); @@ -7298,7 +7298,7 @@ static struct file_system_type ext4_fs_type = { .init_fs_context = ext4_init_fs_context, .parameters = ext4_param_specs, .kill_sb = ext4_kill_sb, - .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP, + .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP | FS_ALLOW_HSM, }; MODULE_ALIAS_FS("ext4"); diff --git a/fs/xfs/xfs_super.c b/fs/xfs/xfs_super.c index 27e9f749c4c7..04a6ec7bc2ae 100644 --- a/fs/xfs/xfs_super.c +++ b/fs/xfs/xfs_super.c @@ -2052,7 +2052,7 @@ static struct file_system_type xfs_fs_type = { .init_fs_context = xfs_init_fs_context, .parameters = xfs_fs_parameters, .kill_sb = xfs_kill_sb, - .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP, + .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP | FS_ALLOW_HSM, }; MODULE_ALIAS_FS("xfs");