All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ioannis Angelakopoulos <iangelak@redhat.com>
To: linux-fsdevel@vger.kernel.org, virtio-fs@redhat.com,
	linux-kernel@vger.kernel.org, jack@suse.cz, amir73il@gmail.com,
	viro@zeniv.linux.org.uk, miklos@szeredi.hu, vgoyal@redhat.com
Cc: Ioannis Angelakopoulos <iangelak@redhat.com>
Subject: [RFC PATCH 7/7] virtiofs: Add support for handling the remote fsnotify notifications
Date: Mon, 25 Oct 2021 16:46:34 -0400	[thread overview]
Message-ID: <20211025204634.2517-8-iangelak@redhat.com> (raw)
In-Reply-To: <20211025204634.2517-1-iangelak@redhat.com>

FUSE and specifically virtiofs should be able to handle the asynchronous
event notifications originating from the FUSE server. To this end we add
the FUSE_NOTIFY_FSNOTIFY switch case to the "virtio_fs_handle_notify" in
fs/fuse/virtio_fs.c to handle these specific notifications.

The event notification contains the information that a user space
application would receive when monitoring an inode for events. The
information is the mask of the inode watch, a file name corresponding to
the inode the remote event was generated for and finally, the inotify
cookie.

Then a new event should be generated corresponding to the event
notification received from the FUSE server. Specifically, FUSE in the guest
kernel will call the "__fsnotify" function in fs/notify/fsnotify.c to send
the event to user space.

Signed-off-by: Ioannis Angelakopoulos <iangelak@redhat.com>
---
 fs/fuse/virtio_fs.c | 64 +++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 62 insertions(+), 2 deletions(-)

diff --git a/fs/fuse/virtio_fs.c b/fs/fuse/virtio_fs.c
index d3dba9e3a07e..4c48c2812caa 100644
--- a/fs/fuse/virtio_fs.c
+++ b/fs/fuse/virtio_fs.c
@@ -16,6 +16,7 @@
 #include <linux/fs_parser.h>
 #include <linux/highmem.h>
 #include <linux/uio.h>
+#include <linux/fsnotify_backend.h>
 #include "fuse_i.h"
 
 /* Used to help calculate the FUSE connection's max_pages limit for a request's
@@ -655,14 +656,69 @@ static void notify_node_reuse(struct virtio_fs_vq *notify_fsvq,
 	spin_unlock(&notify_fsvq->lock);
 }
 
+static int fsnotify_remote_event(struct inode *inode, uint32_t mask,
+				 struct qstr *filename, uint32_t cookie)
+{
+	return __fsnotify(mask, NULL, 0, NULL,
+			  (const struct qstr *)filename, inode, cookie);
+}
+
+/*
+ * Function to generate a new event when a fsnotify notification comes from the
+ * fuse server
+ */
+static int generate_fsnotify_event(struct fuse_conn *fc,
+			struct fuse_notify_fsnotify_out *fsnotify_out)
+{
+	struct inode *inode;
+	uint32_t mask, cookie;
+	struct fuse_mount *fm;
+	int ret = -1;
+	struct qstr name;
+
+	down_read(&fc->killsb);
+	inode = fuse_ilookup(fc, fsnotify_out->inode, &fm);
+	/*
+	 * The inode that corresponds to the event does not exist in this case
+	 * so do not generate any new event and just return an error
+	 */
+	if (!inode)
+		goto out;
+
+	mask = fsnotify_out->mask;
+	cookie = fsnotify_out->cookie;
+
+	/*
+	 * If the notification contained the name of the file/dir the event
+	 * occurred for, it will be placed after the fsnotify_out struct in the
+	 * notification message
+	 */
+	if (fsnotify_out->namelen > 0) {
+		name.len = fsnotify_out->namelen;
+		name.name = (char *)fsnotify_out + sizeof(struct fuse_notify_fsnotify_out);
+		ret = fsnotify_remote_event(inode, mask, &name, cookie);
+	} else {
+		ret = fsnotify_remote_event(inode, mask, NULL, cookie);
+	}
+
+	up_read(&fc->killsb);
+out:
+	if (ret < 0)
+		return -EINVAL;
+
+	return ret;
+}
+
 static int virtio_fs_handle_notify(struct virtio_fs *vfs,
-				   struct virtio_fs_notify_node *notifyn)
+				   struct virtio_fs_notify_node *notifyn,
+				   struct fuse_conn *fc)
 {
 	int ret = 0, no_reuse = 0;
 	struct virtio_fs_notify *notify = &notifyn->notify;
 	struct virtio_fs_vq *notify_fsvq = &vfs->vqs[VQ_NOTIFY_IDX];
 	struct fuse_out_header *oh = &notify->out_hdr;
 	struct fuse_notify_lock_out *lo;
+	struct fuse_notify_fsnotify_out *fsnotify_out;
 
 	/*
 	 * For notifications, oh.unique is 0 and oh->error contains code
@@ -673,6 +729,10 @@ static int virtio_fs_handle_notify(struct virtio_fs *vfs,
 		lo = (struct fuse_notify_lock_out *) &notify->outarg;
 		no_reuse = notify_complete_waiting_req(vfs, lo);
 		break;
+	case FUSE_NOTIFY_FSNOTIFY:
+		fsnotify_out = (struct fuse_notify_fsnotify_out *) &notify->outarg;
+		generate_fsnotify_event(fc, fsnotify_out);
+		break;
 	default:
 		pr_err("virtio-fs: Unexpected notification %d\n", oh->error);
 	}
@@ -711,7 +771,7 @@ static void virtio_fs_notify_done_work(struct work_struct *work)
 		WARN_ON(oh->unique);
 		list_del_init(&notifyn->list);
 		/* Handle notification */
-		virtio_fs_handle_notify(vfs, notifyn);
+		virtio_fs_handle_notify(vfs, notifyn, fsvq->fud->fc);
 		spin_lock(&fsvq->lock);
 		dec_in_flight_req(fsvq);
 		spin_unlock(&fsvq->lock);
-- 
2.33.0


WARNING: multiple messages have this Message-ID (diff)
From: Ioannis Angelakopoulos <iangelak@redhat.com>
To: linux-fsdevel@vger.kernel.org, virtio-fs@redhat.com,
	linux-kernel@vger.kernel.org, jack@suse.cz, amir73il@gmail.com,
	viro@zeniv.linux.org.uk, miklos@szeredi.hu, vgoyal@redhat.com
Subject: [Virtio-fs] [RFC PATCH 7/7] virtiofs: Add support for handling the remote fsnotify notifications
Date: Mon, 25 Oct 2021 16:46:34 -0400	[thread overview]
Message-ID: <20211025204634.2517-8-iangelak@redhat.com> (raw)
In-Reply-To: <20211025204634.2517-1-iangelak@redhat.com>

FUSE and specifically virtiofs should be able to handle the asynchronous
event notifications originating from the FUSE server. To this end we add
the FUSE_NOTIFY_FSNOTIFY switch case to the "virtio_fs_handle_notify" in
fs/fuse/virtio_fs.c to handle these specific notifications.

The event notification contains the information that a user space
application would receive when monitoring an inode for events. The
information is the mask of the inode watch, a file name corresponding to
the inode the remote event was generated for and finally, the inotify
cookie.

Then a new event should be generated corresponding to the event
notification received from the FUSE server. Specifically, FUSE in the guest
kernel will call the "__fsnotify" function in fs/notify/fsnotify.c to send
the event to user space.

Signed-off-by: Ioannis Angelakopoulos <iangelak@redhat.com>
---
 fs/fuse/virtio_fs.c | 64 +++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 62 insertions(+), 2 deletions(-)

diff --git a/fs/fuse/virtio_fs.c b/fs/fuse/virtio_fs.c
index d3dba9e3a07e..4c48c2812caa 100644
--- a/fs/fuse/virtio_fs.c
+++ b/fs/fuse/virtio_fs.c
@@ -16,6 +16,7 @@
 #include <linux/fs_parser.h>
 #include <linux/highmem.h>
 #include <linux/uio.h>
+#include <linux/fsnotify_backend.h>
 #include "fuse_i.h"
 
 /* Used to help calculate the FUSE connection's max_pages limit for a request's
@@ -655,14 +656,69 @@ static void notify_node_reuse(struct virtio_fs_vq *notify_fsvq,
 	spin_unlock(&notify_fsvq->lock);
 }
 
+static int fsnotify_remote_event(struct inode *inode, uint32_t mask,
+				 struct qstr *filename, uint32_t cookie)
+{
+	return __fsnotify(mask, NULL, 0, NULL,
+			  (const struct qstr *)filename, inode, cookie);
+}
+
+/*
+ * Function to generate a new event when a fsnotify notification comes from the
+ * fuse server
+ */
+static int generate_fsnotify_event(struct fuse_conn *fc,
+			struct fuse_notify_fsnotify_out *fsnotify_out)
+{
+	struct inode *inode;
+	uint32_t mask, cookie;
+	struct fuse_mount *fm;
+	int ret = -1;
+	struct qstr name;
+
+	down_read(&fc->killsb);
+	inode = fuse_ilookup(fc, fsnotify_out->inode, &fm);
+	/*
+	 * The inode that corresponds to the event does not exist in this case
+	 * so do not generate any new event and just return an error
+	 */
+	if (!inode)
+		goto out;
+
+	mask = fsnotify_out->mask;
+	cookie = fsnotify_out->cookie;
+
+	/*
+	 * If the notification contained the name of the file/dir the event
+	 * occurred for, it will be placed after the fsnotify_out struct in the
+	 * notification message
+	 */
+	if (fsnotify_out->namelen > 0) {
+		name.len = fsnotify_out->namelen;
+		name.name = (char *)fsnotify_out + sizeof(struct fuse_notify_fsnotify_out);
+		ret = fsnotify_remote_event(inode, mask, &name, cookie);
+	} else {
+		ret = fsnotify_remote_event(inode, mask, NULL, cookie);
+	}
+
+	up_read(&fc->killsb);
+out:
+	if (ret < 0)
+		return -EINVAL;
+
+	return ret;
+}
+
 static int virtio_fs_handle_notify(struct virtio_fs *vfs,
-				   struct virtio_fs_notify_node *notifyn)
+				   struct virtio_fs_notify_node *notifyn,
+				   struct fuse_conn *fc)
 {
 	int ret = 0, no_reuse = 0;
 	struct virtio_fs_notify *notify = &notifyn->notify;
 	struct virtio_fs_vq *notify_fsvq = &vfs->vqs[VQ_NOTIFY_IDX];
 	struct fuse_out_header *oh = &notify->out_hdr;
 	struct fuse_notify_lock_out *lo;
+	struct fuse_notify_fsnotify_out *fsnotify_out;
 
 	/*
 	 * For notifications, oh.unique is 0 and oh->error contains code
@@ -673,6 +729,10 @@ static int virtio_fs_handle_notify(struct virtio_fs *vfs,
 		lo = (struct fuse_notify_lock_out *) &notify->outarg;
 		no_reuse = notify_complete_waiting_req(vfs, lo);
 		break;
+	case FUSE_NOTIFY_FSNOTIFY:
+		fsnotify_out = (struct fuse_notify_fsnotify_out *) &notify->outarg;
+		generate_fsnotify_event(fc, fsnotify_out);
+		break;
 	default:
 		pr_err("virtio-fs: Unexpected notification %d\n", oh->error);
 	}
@@ -711,7 +771,7 @@ static void virtio_fs_notify_done_work(struct work_struct *work)
 		WARN_ON(oh->unique);
 		list_del_init(&notifyn->list);
 		/* Handle notification */
-		virtio_fs_handle_notify(vfs, notifyn);
+		virtio_fs_handle_notify(vfs, notifyn, fsvq->fud->fc);
 		spin_lock(&fsvq->lock);
 		dec_in_flight_req(fsvq);
 		spin_unlock(&fsvq->lock);
-- 
2.33.0


  parent reply	other threads:[~2021-10-25 20:48 UTC|newest]

Thread overview: 137+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-25 20:46 [RFC PATCH 0/7] Inotify support in FUSE and virtiofs Ioannis Angelakopoulos
2021-10-25 20:46 ` [Virtio-fs] " Ioannis Angelakopoulos
2021-10-25 20:46 ` [RFC PATCH 1/7] FUSE: Add the fsnotify opcode and in/out structs to FUSE Ioannis Angelakopoulos
2021-10-25 20:46   ` [Virtio-fs] " Ioannis Angelakopoulos
2021-10-26 14:56   ` Amir Goldstein
2021-10-26 14:56     ` [Virtio-fs] " Amir Goldstein
2021-10-26 18:28     ` Amir Goldstein
2021-10-26 18:28       ` [Virtio-fs] " Amir Goldstein
2021-10-26 20:47     ` Ioannis Angelakopoulos
2021-10-27  5:46       ` Amir Goldstein
2021-10-27  5:46         ` [Virtio-fs] " Amir Goldstein
2021-10-27 21:46     ` Vivek Goyal
2021-10-27 21:46       ` [Virtio-fs] " Vivek Goyal
2021-10-28  4:13       ` Amir Goldstein
2021-10-28  4:13         ` [Virtio-fs] " Amir Goldstein
2021-10-28 14:20         ` Vivek Goyal
2021-10-28 14:20           ` [Virtio-fs] " Vivek Goyal
2021-10-25 20:46 ` [RFC PATCH 2/7] FUSE: Add the remote inotify support capability " Ioannis Angelakopoulos
2021-10-25 20:46   ` [Virtio-fs] " Ioannis Angelakopoulos
2021-10-25 20:46 ` [RFC PATCH 3/7] FUSE,Inotify,Fsnotify,VFS: Add the fuse_fsnotify_update_mark inode operation Ioannis Angelakopoulos
2021-10-25 20:46   ` [Virtio-fs] [RFC PATCH 3/7] FUSE, Inotify, Fsnotify, VFS: " Ioannis Angelakopoulos
2021-10-26 15:06   ` [RFC PATCH 3/7] FUSE,Inotify,Fsnotify,VFS: " Amir Goldstein
2021-10-26 15:06     ` [Virtio-fs] [RFC PATCH 3/7] FUSE, Inotify, Fsnotify, VFS: " Amir Goldstein
2021-11-01 17:49     ` [RFC PATCH 3/7] FUSE,Inotify,Fsnotify,VFS: " Vivek Goyal
2021-11-01 17:49       ` [Virtio-fs] [RFC PATCH 3/7] FUSE, Inotify, Fsnotify, VFS: " Vivek Goyal
2021-11-02  7:34       ` [RFC PATCH 3/7] FUSE,Inotify,Fsnotify,VFS: " Amir Goldstein
2021-11-02  7:34         ` [Virtio-fs] [RFC PATCH 3/7] FUSE, Inotify, Fsnotify, VFS: " Amir Goldstein
2021-10-25 20:46 ` [RFC PATCH 4/7] FUSE: Add the fuse_fsnotify_send_request to FUSE Ioannis Angelakopoulos
2021-10-25 20:46   ` [Virtio-fs] " Ioannis Angelakopoulos
2021-10-25 20:46 ` [RFC PATCH 5/7] Fsnotify: Add a wrapper around the fsnotify function Ioannis Angelakopoulos
2021-10-25 20:46   ` [Virtio-fs] " Ioannis Angelakopoulos
2021-10-26 14:37   ` Amir Goldstein
2021-10-26 14:37     ` [Virtio-fs] " Amir Goldstein
2021-10-26 15:38     ` Vivek Goyal
2021-10-26 15:38       ` [Virtio-fs] " Vivek Goyal
2021-10-25 20:46 ` [RFC PATCH 6/7] FUSE,Fsnotify: Add the fuse_fsnotify_event inode operation Ioannis Angelakopoulos
2021-10-25 20:46   ` [Virtio-fs] [RFC PATCH 6/7] FUSE, Fsnotify: " Ioannis Angelakopoulos
2021-10-25 20:46 ` Ioannis Angelakopoulos [this message]
2021-10-25 20:46   ` [Virtio-fs] [RFC PATCH 7/7] virtiofs: Add support for handling the remote fsnotify notifications Ioannis Angelakopoulos
2021-10-26 15:23 ` [RFC PATCH 0/7] Inotify support in FUSE and virtiofs Amir Goldstein
2021-10-26 15:23   ` [Virtio-fs] " Amir Goldstein
2021-10-26 15:52   ` Vivek Goyal
2021-10-26 15:52     ` [Virtio-fs] " Vivek Goyal
2021-10-26 18:19     ` Amir Goldstein
2021-10-26 18:19       ` [Virtio-fs] " Amir Goldstein
2021-10-26 16:18   ` Vivek Goyal
2021-10-26 16:18     ` [Virtio-fs] " Vivek Goyal
2021-10-26 17:59     ` Amir Goldstein
2021-10-26 17:59       ` [Virtio-fs] " Amir Goldstein
2021-10-26 18:27       ` Vivek Goyal
2021-10-26 18:27         ` [Virtio-fs] " Vivek Goyal
2021-10-26 19:04         ` Amir Goldstein
2021-10-26 19:04           ` [Virtio-fs] " Amir Goldstein
2021-10-26 19:14         ` Ioannis Angelakopoulos
2021-10-27  5:59           ` Amir Goldstein
2021-10-27  5:59             ` [Virtio-fs] " Amir Goldstein
2021-10-27 13:23             ` Jan Kara
2021-10-27 13:23               ` [Virtio-fs] " Jan Kara
2021-10-27 20:29               ` Vivek Goyal
2021-10-27 20:29                 ` [Virtio-fs] " Vivek Goyal
2021-10-27 20:37                 ` Vivek Goyal
2021-10-27 20:37                   ` [Virtio-fs] " Vivek Goyal
2021-11-02 11:09                 ` Jan Kara
2021-11-02 11:09                   ` [Virtio-fs] " Jan Kara
2021-11-02 12:54                   ` Amir Goldstein
2021-11-02 12:54                     ` [Virtio-fs] " Amir Goldstein
2021-11-02 20:34                     ` Vivek Goyal
2021-11-02 20:34                       ` [Virtio-fs] " Vivek Goyal
2021-11-03  7:31                       ` Amir Goldstein
2021-11-03  7:31                         ` [Virtio-fs] " Amir Goldstein
2021-11-03 22:29                         ` Vivek Goyal
2021-11-03 22:29                           ` [Virtio-fs] " Vivek Goyal
2021-11-04  5:19                           ` Amir Goldstein
2021-11-04  5:19                             ` [Virtio-fs] " Amir Goldstein
2021-11-03 10:09                     ` Jan Kara
2021-11-03 10:09                       ` [Virtio-fs] " Jan Kara
2021-11-03 11:17                       ` Amir Goldstein
2021-11-03 11:17                         ` [Virtio-fs] " Amir Goldstein
2021-11-03 22:36                         ` Vivek Goyal
2021-11-03 22:36                           ` [Virtio-fs] " Vivek Goyal
2021-11-04  5:29                           ` Amir Goldstein
2021-11-04  5:29                             ` [Virtio-fs] " Amir Goldstein
2021-11-04 10:03                           ` Jan Kara
2021-11-04 10:03                             ` [Virtio-fs] " Jan Kara
2021-11-05 14:30                             ` Vivek Goyal
2021-11-05 14:30                               ` [Virtio-fs] " Vivek Goyal
2021-11-10  6:28                               ` Amir Goldstein
2021-11-10  6:28                                 ` [Virtio-fs] " Amir Goldstein
2021-11-11 17:30                                 ` Jan Kara
2021-11-11 17:30                                   ` [Virtio-fs] " Jan Kara
2021-11-11 20:52                                   ` Amir Goldstein
2021-11-11 20:52                                     ` [Virtio-fs] " Amir Goldstein
2021-11-16  5:09                                     ` Stef Bon
2021-11-16  5:09                                       ` [Virtio-fs] " Stef Bon
2021-11-16 18:00                                       ` Ioannis Angelakopoulos
2021-11-16 22:12                                       ` Ioannis Angelakopoulos
2021-11-17  6:40                                         ` Amir Goldstein
2021-11-17  6:40                                           ` [Virtio-fs] " Amir Goldstein
2021-11-30 15:27                                           ` Vivek Goyal
2021-11-30 15:27                                             ` [Virtio-fs] " Vivek Goyal
2021-12-14 23:21                                             ` Ioannis Angelakopoulos
2021-12-15  7:10                                               ` Amir Goldstein
2021-12-15  7:10                                                 ` [Virtio-fs] " Amir Goldstein
2021-12-15 16:44                                                 ` Vivek Goyal
2021-12-15 16:44                                                   ` [Virtio-fs] " Vivek Goyal
2021-12-15 17:29                                                   ` Amir Goldstein
2021-12-15 17:29                                                     ` [Virtio-fs] " Amir Goldstein
2021-12-15 19:20                                                     ` Vivek Goyal
2021-12-15 19:20                                                       ` [Virtio-fs] " Vivek Goyal
2021-12-15 19:54                                                       ` Amir Goldstein
2021-12-15 19:54                                                         ` [Virtio-fs] " Amir Goldstein
2021-12-16 11:03                                                         ` Amir Goldstein
2021-12-16 11:03                                                           ` [Virtio-fs] " Amir Goldstein
2021-12-16 16:24                                                           ` Vivek Goyal
2021-12-16 16:24                                                             ` [Virtio-fs] " Vivek Goyal
2021-12-16 18:22                                                             ` Amir Goldstein
2021-12-16 18:22                                                               ` [Virtio-fs] " Amir Goldstein
2021-12-16 22:24                                                               ` Vivek Goyal
2021-12-16 22:24                                                                 ` [Virtio-fs] " Vivek Goyal
2021-12-17  4:21                                                                 ` Amir Goldstein
2021-12-17  4:21                                                                   ` [Virtio-fs] " Amir Goldstein
2021-12-17 14:15                                                                   ` Vivek Goyal
2021-12-17 14:15                                                                     ` [Virtio-fs] " Vivek Goyal
2021-12-18  8:28                                                                     ` Amir Goldstein
2021-12-18  8:28                                                                       ` [Virtio-fs] " Amir Goldstein
2021-12-20 16:41                                                                       ` Vivek Goyal
2021-12-20 16:41                                                                         ` [Virtio-fs] " Vivek Goyal
2021-12-20 18:22                                                                         ` Amir Goldstein
2021-12-20 18:22                                                                           ` [Virtio-fs] " Amir Goldstein
2022-01-06 23:37                                             ` Steve French
2022-01-06 23:37                                               ` [Virtio-fs] " Steve French
2021-11-30 15:36                                       ` Vivek Goyal
2021-11-30 15:36                                         ` [Virtio-fs] " Vivek Goyal
2021-10-27 20:24             ` Vivek Goyal
2021-10-27 20:24               ` [Virtio-fs] " Vivek Goyal
2021-10-28  5:11               ` Amir Goldstein
2021-10-28  5:11                 ` [Virtio-fs] " Amir Goldstein

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211025204634.2517-8-iangelak@redhat.com \
    --to=iangelak@redhat.com \
    --cc=amir73il@gmail.com \
    --cc=jack@suse.cz \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=miklos@szeredi.hu \
    --cc=vgoyal@redhat.com \
    --cc=viro@zeniv.linux.org.uk \
    --cc=virtio-fs@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.