linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/3] vfs: merge .d_select_inode() into .d_real()
@ 2016-05-17 21:53 Miklos Szeredi
  2016-05-17 21:55 ` [PATCH 2/3] vfs: document ->d_real() Miklos Szeredi
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Miklos Szeredi @ 2016-05-17 21:53 UTC (permalink / raw)
  To: Al Viro; +Cc: linux-kernel, linux-fsdevel, David Howells

The two methods essentially do the same: find the real dentry/inode
belonging to an overlay dentry.  The difference is in the usage:

vfs_open() uses ->d_select_inode() and expects the function to perform
copy-up if necessary based on the open flags argument.

file_dentry() uses ->d_real() passing in the overlay dentry as well as the
underlying inode.

vfs_rename() uses ->d_select_inode() but passes zero flags.  ->d_real()
with a zero inode would have worked just as well here.

This patch merges the functionality of ->d_select_inode() into ->d_real()
by adding an 'open_flags' argument to the latter.

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
---
 fs/dcache.c              |    3 ---
 fs/namei.c               |    2 +-
 fs/open.c                |   17 +++++++++++++----
 fs/overlayfs/inode.c     |   31 ++++++++++---------------------
 fs/overlayfs/overlayfs.h |    2 +-
 fs/overlayfs/super.c     |   17 +++++++++++++----
 include/linux/dcache.h   |   22 ++++------------------
 include/linux/fs.h       |    2 +-
 8 files changed, 43 insertions(+), 53 deletions(-)

--- a/fs/dcache.c
+++ b/fs/dcache.c
@@ -1667,7 +1667,6 @@ void d_set_d_op(struct dentry *dentry, c
 				DCACHE_OP_REVALIDATE	|
 				DCACHE_OP_WEAK_REVALIDATE	|
 				DCACHE_OP_DELETE	|
-				DCACHE_OP_SELECT_INODE	|
 				DCACHE_OP_REAL));
 	dentry->d_op = op;
 	if (!op)
@@ -1684,8 +1683,6 @@ void d_set_d_op(struct dentry *dentry, c
 		dentry->d_flags |= DCACHE_OP_DELETE;
 	if (op->d_prune)
 		dentry->d_flags |= DCACHE_OP_PRUNE;
-	if (op->d_select_inode)
-		dentry->d_flags |= DCACHE_OP_SELECT_INODE;
 	if (op->d_real)
 		dentry->d_flags |= DCACHE_OP_REAL;
 
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -4247,7 +4247,7 @@ int vfs_rename(struct inode *old_dir, st
 	 * Check source == target.
 	 * On overlayfs need to look at underlying inodes.
 	 */
-	if (vfs_select_inode(old_dentry, 0) == vfs_select_inode(new_dentry, 0))
+	if (d_inode(d_real(old_dentry)) == d_inode(d_real(new_dentry)))
 		return 0;
 
 	error = may_delete(old_dir, old_dentry, is_dir);
--- a/fs/open.c
+++ b/fs/open.c
@@ -831,6 +831,15 @@ char *file_path(struct file *filp, char
 }
 EXPORT_SYMBOL(file_path);
 
+static struct dentry *open_select_dentry(struct dentry *dentry,
+					 unsigned int open_flags)
+{
+	if (unlikely(dentry->d_flags & DCACHE_OP_REAL))
+		return dentry->d_op->d_real(dentry, NULL, open_flags);
+	else
+		return dentry;
+}
+
 /**
  * vfs_open - open the file at the given path
  * @path: path to open
@@ -840,13 +849,13 @@ EXPORT_SYMBOL(file_path);
 int vfs_open(const struct path *path, struct file *file,
 	     const struct cred *cred)
 {
-	struct inode *inode = vfs_select_inode(path->dentry, file->f_flags);
+	struct dentry *dentry = open_select_dentry(path->dentry, file->f_flags);
 
-	if (IS_ERR(inode))
-		return PTR_ERR(inode);
+	if (IS_ERR(dentry))
+		return PTR_ERR(dentry);
 
 	file->f_path = *path;
-	return do_dentry_open(file, inode, NULL, cred);
+	return do_dentry_open(file, d_backing_inode(dentry), NULL, cred);
 }
 
 struct file *dentry_open(const struct path *path, int flags,
--- a/fs/overlayfs/inode.c
+++ b/fs/overlayfs/inode.c
@@ -338,36 +338,25 @@ static bool ovl_open_need_copy_up(int fl
 	return true;
 }
 
-struct inode *ovl_d_select_inode(struct dentry *dentry, unsigned file_flags)
+int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags)
 {
-	int err;
+	int err = 0;
 	struct path realpath;
 	enum ovl_path_type type;
 
-	if (d_is_dir(dentry))
-		return d_backing_inode(dentry);
-
 	type = ovl_path_real(dentry, &realpath);
 	if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) {
 		err = ovl_want_write(dentry);
-		if (err)
-			return ERR_PTR(err);
-
-		if (file_flags & O_TRUNC)
-			err = ovl_copy_up_truncate(dentry);
-		else
-			err = ovl_copy_up(dentry);
-		ovl_drop_write(dentry);
-		if (err)
-			return ERR_PTR(err);
-
-		ovl_path_upper(dentry, &realpath);
+		if (!err) {
+			if (file_flags & O_TRUNC)
+				err = ovl_copy_up_truncate(dentry);
+			else
+				err = ovl_copy_up(dentry);
+			ovl_drop_write(dentry);
+		}
 	}
 
-	if (realpath.dentry->d_flags & DCACHE_OP_SELECT_INODE)
-		return realpath.dentry->d_op->d_select_inode(realpath.dentry, file_flags);
-
-	return d_backing_inode(realpath.dentry);
+	return err;
 }
 
 static const struct inode_operations ovl_file_inode_operations = {
--- a/fs/overlayfs/overlayfs.h
+++ b/fs/overlayfs/overlayfs.h
@@ -177,7 +177,7 @@ ssize_t ovl_getxattr(struct dentry *dent
 		     void *value, size_t size);
 ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size);
 int ovl_removexattr(struct dentry *dentry, const char *name);
-struct inode *ovl_d_select_inode(struct dentry *dentry, unsigned file_flags);
+int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags);
 
 struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
 			    struct ovl_entry *oe);
--- a/fs/overlayfs/super.c
+++ b/fs/overlayfs/super.c
@@ -295,7 +295,8 @@ static void ovl_dentry_release(struct de
 	}
 }
 
-static struct dentry *ovl_d_real(struct dentry *dentry, struct inode *inode)
+static struct dentry *ovl_d_real(struct dentry *dentry, struct inode *inode,
+				 unsigned int open_flags)
 {
 	struct dentry *real;
 
@@ -305,6 +306,16 @@ static struct dentry *ovl_d_real(struct
 		goto bug;
 	}
 
+	if (d_is_negative(dentry))
+		return dentry;
+
+	if (open_flags) {
+		int err = ovl_open_maybe_copy_up(dentry, open_flags);
+
+		if (err)
+			return ERR_PTR(err);
+	}
+
 	real = ovl_dentry_upper(dentry);
 	if (real && (!inode || inode == d_inode(real)))
 		return real;
@@ -318,7 +329,7 @@ static struct dentry *ovl_d_real(struct
 
 	/* Handle recursion */
 	if (real->d_flags & DCACHE_OP_REAL)
-		return real->d_op->d_real(real, inode);
+		return real->d_op->d_real(real, inode, open_flags);
 
 bug:
 	WARN(1, "ovl_d_real(%pd4, %s:%lu\n): real dentry not found\n", dentry,
@@ -369,13 +380,11 @@ static int ovl_dentry_weak_revalidate(st
 
 static const struct dentry_operations ovl_dentry_operations = {
 	.d_release = ovl_dentry_release,
-	.d_select_inode = ovl_d_select_inode,
 	.d_real = ovl_d_real,
 };
 
 static const struct dentry_operations ovl_reval_dentry_operations = {
 	.d_release = ovl_dentry_release,
-	.d_select_inode = ovl_d_select_inode,
 	.d_real = ovl_d_real,
 	.d_revalidate = ovl_dentry_revalidate,
 	.d_weak_revalidate = ovl_dentry_weak_revalidate,
--- a/include/linux/dcache.h
+++ b/include/linux/dcache.h
@@ -160,8 +160,7 @@ struct dentry_operations {
 	char *(*d_dname)(struct dentry *, char *, int);
 	struct vfsmount *(*d_automount)(struct path *);
 	int (*d_manage)(struct dentry *, bool);
-	struct inode *(*d_select_inode)(struct dentry *, unsigned);
-	struct dentry *(*d_real)(struct dentry *, struct inode *);
+	struct dentry *(*d_real)(struct dentry *, struct inode *, unsigned int);
 } ____cacheline_aligned;
 
 /*
@@ -227,10 +226,8 @@ struct dentry_operations {
 
 #define DCACHE_MAY_FREE			0x00800000
 #define DCACHE_FALLTHRU			0x01000000 /* Fall through to lower layer */
-#define DCACHE_OP_SELECT_INODE		0x02000000 /* Unioned entry: dcache op selects inode */
-
-#define DCACHE_ENCRYPTED_WITH_KEY	0x04000000 /* dir is encrypted with a valid key */
-#define DCACHE_OP_REAL			0x08000000
+#define DCACHE_ENCRYPTED_WITH_KEY	0x02000000 /* dir is encrypted with a valid key */
+#define DCACHE_OP_REAL			0x04000000
 
 extern seqlock_t rename_lock;
 
@@ -560,21 +557,10 @@ static inline struct dentry *d_backing_d
 static inline struct dentry *d_real(struct dentry *dentry)
 {
 	if (unlikely(dentry->d_flags & DCACHE_OP_REAL))
-		return dentry->d_op->d_real(dentry, NULL);
+		return dentry->d_op->d_real(dentry, NULL, 0);
 	else
 		return dentry;
 }
 
-static inline struct inode *vfs_select_inode(struct dentry *dentry,
-					     unsigned open_flags)
-{
-	struct inode *inode = d_inode(dentry);
-
-	if (inode && unlikely(dentry->d_flags & DCACHE_OP_SELECT_INODE))
-		inode = dentry->d_op->d_select_inode(dentry, open_flags);
-
-	return inode;
-}
-
 
 #endif	/* __LINUX_DCACHE_H */
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -1246,7 +1246,7 @@ static inline struct dentry *file_dentry
 	struct dentry *dentry = file->f_path.dentry;
 
 	if (unlikely(dentry->d_flags & DCACHE_OP_REAL))
-		return dentry->d_op->d_real(dentry, file_inode(file));
+		return dentry->d_op->d_real(dentry, file_inode(file), 0);
 	else
 		return dentry;
 }



^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 2/3] vfs: document ->d_real()
  2016-05-17 21:53 [PATCH 1/3] vfs: merge .d_select_inode() into .d_real() Miklos Szeredi
@ 2016-05-17 21:55 ` Miklos Szeredi
  2016-05-17 21:57 ` [PATCH 3/3] vfs: clean up documentation Miklos Szeredi
  2016-05-22 16:39 ` [PATCH 1/3] vfs: merge .d_select_inode() into .d_real() James Bottomley
  2 siblings, 0 replies; 5+ messages in thread
From: Miklos Szeredi @ 2016-05-17 21:55 UTC (permalink / raw)
  To: Al Viro; +Cc: linux-kernel, linux-fsdevel, David Howells

Add missing documentation for the d_op->d_real() method and d_real()
helper.

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
---
 Documentation/filesystems/Locking |    2 ++
 Documentation/filesystems/vfs.txt |   19 +++++++++++++++++++
 include/linux/dcache.h            |    7 +++++++
 3 files changed, 28 insertions(+)

--- a/Documentation/filesystems/Locking
+++ b/Documentation/filesystems/Locking
@@ -20,6 +20,7 @@ be able to use diff(1).
 	char *(*d_dname)((struct dentry *dentry, char *buffer, int buflen);
 	struct vfsmount *(*d_automount)(struct path *path);
 	int (*d_manage)(struct dentry *, bool);
+	struct dentry *(*d_real)(struct dentry *, struct inode *, unsigned int);
 
 locking rules:
 		rename_lock	->d_lock	may block	rcu-walk
@@ -34,6 +35,7 @@ d_iput:		no		no		yes		no
 d_dname:	no		no		no		no
 d_automount:	no		no		yes		no
 d_manage:	no		no		yes (ref-walk)	maybe
+d_real		no		no		yes 		no
 
 --------------------------- inode_operations --------------------------- 
 prototypes:
--- a/Documentation/filesystems/vfs.txt
+++ b/Documentation/filesystems/vfs.txt
@@ -938,6 +938,7 @@ struct dentry_operations {
 	char *(*d_dname)(struct dentry *, char *, int);
 	struct vfsmount *(*d_automount)(struct path *);
 	int (*d_manage)(struct dentry *, bool);
+	struct dentry *(*d_real)(struct dentry *, struct inode *, unsigned int);
 };
 
   d_revalidate: called when the VFS needs to revalidate a dentry. This
@@ -1060,6 +1061,24 @@ struct dentry_operations {
 	This function is only used if DCACHE_MANAGE_TRANSIT is set on the
 	dentry being transited from.
 
+  d_real: overlay/union type filesystems implement this method to return one of
+	the underlying dentries hidden by the overlay.  It is used in three
+	different modes:
+
+	Called from open it may need to copy-up the file depending on the
+	supplied open flags.  This mode is selected with a non-zero flags
+	argument.  In this mode the d_real method can return an error.
+
+	Called from file_dentry() it returns the real dentry matching the inode
+	argument.  The real dentry may be from a lower layer already copied up,
+	but still referenced from the file.  This mode is selected with a
+	non-NULL inode argument.  This will always succeed.
+
+	With NULL inode and zero flags the topmost real underlying dentry is
+	returned.  This will always succeed.
+
+	This method is never called with both non-NULL inode and non-zero flags.
+
 Example :
 
 static char *pipefs_dname(struct dentry *dent, char *buffer, int buflen)
--- a/include/linux/dcache.h
+++ b/include/linux/dcache.h
@@ -554,6 +554,13 @@ static inline struct dentry *d_backing_d
 	return upper;
 }
 
+/**
+ * d_real - Return the real dentry
+ * @dentry: The dentry to query
+ *
+ * If dentry is on an union/overlay, then return the underlying, real dentry.
+ * Otherwise return the dentry itself.
+ */
 static inline struct dentry *d_real(struct dentry *dentry)
 {
 	if (unlikely(dentry->d_flags & DCACHE_OP_REAL))



^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 3/3] vfs: clean up documentation
  2016-05-17 21:53 [PATCH 1/3] vfs: merge .d_select_inode() into .d_real() Miklos Szeredi
  2016-05-17 21:55 ` [PATCH 2/3] vfs: document ->d_real() Miklos Szeredi
@ 2016-05-17 21:57 ` Miklos Szeredi
  2016-05-22 16:39 ` [PATCH 1/3] vfs: merge .d_select_inode() into .d_real() James Bottomley
  2 siblings, 0 replies; 5+ messages in thread
From: Miklos Szeredi @ 2016-05-17 21:57 UTC (permalink / raw)
  To: Al Viro; +Cc: linux-kernel, linux-fsdevel, David Howells

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
---
 Documentation/filesystems/Locking |    2 --
 Documentation/filesystems/vfs.txt |   24 ++++++++----------------
 2 files changed, 8 insertions(+), 18 deletions(-)

--- a/Documentation/filesystems/Locking
+++ b/Documentation/filesystems/Locking
@@ -68,7 +68,6 @@ d_real		no		no		yes 		no
 				struct file *, unsigned open_flag,
 				umode_t create_mode, int *opened);
 	int (*tmpfile) (struct inode *, struct dentry *, umode_t);
-	int (*dentry_open)(struct dentry *, struct file *, const struct cred *);
 
 locking rules:
 	all may block
@@ -97,7 +96,6 @@ fiemap:		no
 update_time:	no
 atomic_open:	yes
 tmpfile:	no
-dentry_open:	no
 
 	Additionally, ->rmdir(), ->unlink() and ->rename() have ->i_mutex on
 victim.
--- a/Documentation/filesystems/vfs.txt
+++ b/Documentation/filesystems/vfs.txt
@@ -364,7 +364,6 @@ struct inode_operations {
 	int (*atomic_open)(struct inode *, struct dentry *, struct file *,
 			unsigned open_flag, umode_t create_mode, int *opened);
 	int (*tmpfile) (struct inode *, struct dentry *, umode_t);
-	int (*dentry_open)(struct dentry *, struct file *, const struct cred *);
 };
 
 Again, all methods are called without any locks being held, unless
@@ -696,13 +695,6 @@ struct address_space_operations {
   	but instead uses bmap to find out where the blocks in the file
   	are and uses those addresses directly.
 
-  dentry_open: *WARNING: probably going away soon, do not use!* This is an
-	alternative to f_op->open(), the difference is that this method may open
-	a file not necessarily originating from the same filesystem as the one
-	i_op->open() was called on.  It may be useful for stacking filesystems
-	which want to allow native I/O directly on underlying files.
-
-
   invalidatepage: If a page has PagePrivate set, then invalidatepage
         will be called when part or all of the page is to be removed
 	from the address space.  This generally corresponds to either a
@@ -1023,6 +1015,14 @@ struct dentry_operations {
 	at the end of the buffer, and returns a pointer to the first char.
 	dynamic_dname() helper function is provided to take care of this.
 
+	Example :
+
+	static char *pipefs_dname(struct dentry *dent, char *buffer, int buflen)
+	{
+		return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
+				dentry->d_inode->i_ino);
+	}
+
   d_automount: called when an automount dentry is to be traversed (optional).
 	This should create a new VFS mount record and return the record to the
 	caller.  The caller is supplied with a path parameter giving the
@@ -1079,14 +1079,6 @@ struct dentry_operations {
 
 	This method is never called with both non-NULL inode and non-zero flags.
 
-Example :
-
-static char *pipefs_dname(struct dentry *dent, char *buffer, int buflen)
-{
-	return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
-				dentry->d_inode->i_ino);
-}
-
 Each dentry has a pointer to its parent dentry, as well as a hash list
 of child dentries. Child dentries are basically like files in a
 directory.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/3] vfs: merge .d_select_inode() into .d_real()
  2016-05-17 21:53 [PATCH 1/3] vfs: merge .d_select_inode() into .d_real() Miklos Szeredi
  2016-05-17 21:55 ` [PATCH 2/3] vfs: document ->d_real() Miklos Szeredi
  2016-05-17 21:57 ` [PATCH 3/3] vfs: clean up documentation Miklos Szeredi
@ 2016-05-22 16:39 ` James Bottomley
  2016-05-27  8:14   ` Miklos Szeredi
  2 siblings, 1 reply; 5+ messages in thread
From: James Bottomley @ 2016-05-22 16:39 UTC (permalink / raw)
  To: Miklos Szeredi, Al Viro; +Cc: linux-kernel, linux-fsdevel, David Howells

On Tue, 2016-05-17 at 23:53 +0200, Miklos Szeredi wrote:
> The two methods essentially do the same: find the real dentry/inode
> belonging to an overlay dentry.  The difference is in the usage:
> 
> vfs_open() uses ->d_select_inode() and expects the function to 
> perform copy-up if necessary based on the open flags argument.
> 
> file_dentry() uses ->d_real() passing in the overlay dentry as well 
> as the underlying inode.
> 
> vfs_rename() uses ->d_select_inode() but passes zero flags.  
> ->d_real() with a zero inode would have worked just as well here.
> 
> This patch merges the functionality of ->d_select_inode() into 
> ->d_real() by adding an 'open_flags' argument to the latter.

Actually, before you do this, I have a use case for keeping them
separate:  The shiftfs prototype I've posted cannot use d_select_inode
because it's shifting the uid/gids, so the inode permissions have to be
done on the upper inode.  However, several filesystems make use of
file_dentry() and for them, I need to supply the lower dentry, so I end
up setting d_real but not d_select_inode.

for me, then vfs_open and vfs_rename have to operate on the upper inode
but all uses of file_dentry should return the lower dentry.

James



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/3] vfs: merge .d_select_inode() into .d_real()
  2016-05-22 16:39 ` [PATCH 1/3] vfs: merge .d_select_inode() into .d_real() James Bottomley
@ 2016-05-27  8:14   ` Miklos Szeredi
  0 siblings, 0 replies; 5+ messages in thread
From: Miklos Szeredi @ 2016-05-27  8:14 UTC (permalink / raw)
  To: James Bottomley; +Cc: Al Viro, linux-kernel, linux-fsdevel, David Howells

On Sun, May 22, 2016 at 6:39 PM, James Bottomley
<James.Bottomley@hansenpartnership.com> wrote:
> On Tue, 2016-05-17 at 23:53 +0200, Miklos Szeredi wrote:
>> The two methods essentially do the same: find the real dentry/inode
>> belonging to an overlay dentry.  The difference is in the usage:
>>
>> vfs_open() uses ->d_select_inode() and expects the function to
>> perform copy-up if necessary based on the open flags argument.
>>
>> file_dentry() uses ->d_real() passing in the overlay dentry as well
>> as the underlying inode.
>>
>> vfs_rename() uses ->d_select_inode() but passes zero flags.
>> ->d_real() with a zero inode would have worked just as well here.
>>
>> This patch merges the functionality of ->d_select_inode() into
>> ->d_real() by adding an 'open_flags' argument to the latter.
>
> Actually, before you do this, I have a use case for keeping them
> separate:  The shiftfs prototype I've posted cannot use d_select_inode
> because it's shifting the uid/gids, so the inode permissions have to be
> done on the upper inode.  However, several filesystems make use of
> file_dentry() and for them, I need to supply the lower dentry, so I end
> up setting d_real but not d_select_inode.
>
> for me, then vfs_open and vfs_rename have to operate on the upper inode
> but all uses of file_dentry should return the lower dentry.

The calls in vfs_rename() deal with rename to hard-linked counterpart.
Unless you want to add lower to upper mapping for inodes, you'll need
this too.  Same goes for hard linked sockets:

https://git.kernel.org/cgit/linux/kernel/git/mszeredi/vfs.git/commit/?h=overlayfs-next&id=eb0a4a47ae89aaa0674ab3180de6a162f3be2ddf

So looks like the only thing you don't want this is for vfs_open().
We pass open flags into the merged ->d_real() function, but that may
be zero.  Could pass an internal O_OPEN flag to indicate this is open.

Thanks,
Miklos

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2016-05-27  8:14 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-05-17 21:53 [PATCH 1/3] vfs: merge .d_select_inode() into .d_real() Miklos Szeredi
2016-05-17 21:55 ` [PATCH 2/3] vfs: document ->d_real() Miklos Szeredi
2016-05-17 21:57 ` [PATCH 3/3] vfs: clean up documentation Miklos Szeredi
2016-05-22 16:39 ` [PATCH 1/3] vfs: merge .d_select_inode() into .d_real() James Bottomley
2016-05-27  8:14   ` Miklos Szeredi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).