Re: [PATCH] VFS: generate FS_CREATE before FS_OPEN when ->atomic_open used.

NeilBrown posted 1 patch 1 year, 8 months ago
Re: [PATCH] VFS: generate FS_CREATE before FS_OPEN when ->atomic_open used.
Posted by NeilBrown 1 year, 8 months ago
On Wed, 12 Jun 2024, Al Viro wrote:
> On Wed, Jun 12, 2024 at 12:05:11PM +1000, NeilBrown wrote:
> 
> > For finish_open() there are three cases:
> >  - finish_open is used in ->atomic_open handlers.  For these we add a
> >    call to fsnotify_open() in do_open() if FMODE_OPENED is set - which
> >    means do_dentry_open() has been called. This happens after fsnotify_create().
> 
> 	Hummm....  There's a bit of behaviour change; in case we fail in
> may_open(), we used to get fsnotify_open()+fsnotify_close() and with that
> patch we's get fsnotify_close() alone.

True.  Presumably we could fix that by doing
diff --git a/fs/namei.c b/fs/namei.c
index 37fb0a8aa09a..6fd04c9046fa 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -3645,6 +3645,8 @@ static int do_open(struct nameidata *nd,
 			return error;
 		do_truncate = true;
 	}
+	if (file->f_mode & FMODE_OPENED)
+		fsnotify_open(file);
 	error = may_open(idmap, &nd->path, acc_mode, open_flag);
 	if (!error && !(file->f_mode & FMODE_OPENED))
 		error = vfs_open(&nd->path, file);
@@ -3702,6 +3704,7 @@ int vfs_tmpfile(struct mnt_idmap *idmap,
 	dput(child);
 	if (error)
 		return error;
+	fsnotify_open(file);
 	/* Don't check for other permissions, the inode was just created */
 	error = may_open(idmap, &file->f_path, 0, file->f_flags);
 	if (error)

instead, but it seems a little weird sending an OPEN notification if
may_open() fails.

> 
> 	IF we don't care about that, we might as well take fsnotify_open()
> out of vfs_open() and, for do_open()/do_tmpfile()/do_o_path(), into
> path_openat() itself.  I mean, having
>         if (likely(!error)) {
>                 if (likely(file->f_mode & FMODE_OPENED)) {
> 			fsnotify_open(file);
>                         return file;
> 		}
> in there would be a lot easier to follow...  It would lose fsnotify_open()
> in a few more failure exits, but if we don't give a damn about having it
> paired with fsnotify_close()...
> 

Should we have fsnotify_open() set a new ->f_mode flag, and
fsnotify_close() abort if it isn't set (and clear it if it is)?
Then we would be guaranteed a balance - which does seem like a good
idea.

Thanks,
NeilBrown
Re: [PATCH] VFS: generate FS_CREATE before FS_OPEN when ->atomic_open used.
Posted by Al Viro 1 year, 8 months ago
On Wed, Jun 12, 2024 at 12:55:40PM +1000, NeilBrown wrote:
> > 	IF we don't care about that, we might as well take fsnotify_open()
> > out of vfs_open() and, for do_open()/do_tmpfile()/do_o_path(), into
> > path_openat() itself.  I mean, having
> >         if (likely(!error)) {
> >                 if (likely(file->f_mode & FMODE_OPENED)) {
> > 			fsnotify_open(file);
> >                         return file;
> > 		}
> > in there would be a lot easier to follow...  It would lose fsnotify_open()
> > in a few more failure exits, but if we don't give a damn about having it
> > paired with fsnotify_close()...
> > 
> 
> Should we have fsnotify_open() set a new ->f_mode flag, and
> fsnotify_close() abort if it isn't set (and clear it if it is)?
> Then we would be guaranteed a balance - which does seem like a good
> idea.

Umm...  In that case, I would rather have FMODE_NONOTIFY set just before
the fput() in path_openat() - no need to grab another flag from ->f_mode
(not a lot of unused ones there) and no need to add any overhead on
the fast path.
Re: [PATCH] VFS: generate FS_CREATE before FS_OPEN when ->atomic_open used.
Posted by NeilBrown 1 year, 8 months ago
On Wed, 12 Jun 2024, Al Viro wrote:
> On Wed, Jun 12, 2024 at 12:55:40PM +1000, NeilBrown wrote:
> > > 	IF we don't care about that, we might as well take fsnotify_open()
> > > out of vfs_open() and, for do_open()/do_tmpfile()/do_o_path(), into
> > > path_openat() itself.  I mean, having
> > >         if (likely(!error)) {
> > >                 if (likely(file->f_mode & FMODE_OPENED)) {
> > > 			fsnotify_open(file);
> > >                         return file;
> > > 		}
> > > in there would be a lot easier to follow...  It would lose fsnotify_open()
> > > in a few more failure exits, but if we don't give a damn about having it
> > > paired with fsnotify_close()...
> > > 
> > 
> > Should we have fsnotify_open() set a new ->f_mode flag, and
> > fsnotify_close() abort if it isn't set (and clear it if it is)?
> > Then we would be guaranteed a balance - which does seem like a good
> > idea.
> 
> Umm...  In that case, I would rather have FMODE_NONOTIFY set just before
> the fput() in path_openat() - no need to grab another flag from ->f_mode
> (not a lot of unused ones there) and no need to add any overhead on
> the fast path.
> 

Unfortunately that gets messy if handle_truncate() fails.  We would need
to delay the fsnotify_open() until after truncate which means moving it
out of vfs_open() or maybe calling do_dentry_open() directly from
do_open() - neither of which I like.

I think it is best to stick with "if FMODE_OPENED is set, then we call
fsnotify_open() even if the open will fail", and only move the place
where fsnotify_open() is called.

BTW I was wrong about gfs.  Closer inspection of the code show that
finish_open() is only called in the ->atomic_open case.

Thanks,
NeilBrown