Re: [Linux-cachefs] [PATCH 02/11] xfs: add NOWAIT semantics for readdir

2023-09-03 Thread Dave Chinner
On Sun, Aug 27, 2023 at 09:28:26PM +0800, Hao Xu wrote:
> From: Hao Xu 
> 
> Implement NOWAIT semantics for readdir. Return EAGAIN error to the
> caller if it would block, like failing to get locks, or going to
> do IO.
> 
> Co-developed-by: Dave Chinner 

Not really.

"Co-developed" implies equal development input between all the
parties, which is not the case here - this patch is based on
prototype I wrote, whilst you're doing the refining, testing and
correctness work.

In these cases with XFS code, we add a line in the commit message to
say:

"This is based on a patch originally written by Dave Chinner."


> Signed-off-by: Dave Chinner 
> Signed-off-by: Hao Xu 
> [fixes deadlock issue, tweak code style]

With a signoff chain like you already have.

In the end you'll also get a RVB from me, which seems rather wrong
to me if I've apparently been "co-developing" the code



> @@ -156,7 +157,9 @@ xfs_dir2_block_getdents(
>   if (xfs_dir2_dataptr_to_db(geo, ctx->pos) > geo->datablk)
>   return 0;
>  
> - error = xfs_dir3_block_read(args->trans, dp, );
> + if (ctx->flags & DIR_CONTEXT_F_NOWAIT)
> + flags |= XFS_DABUF_NOWAIT;
> + error = xfs_dir3_block_read(args->trans, dp, flags, );
>   if (error)
>   return error;
>  

Given we do this same check in both block and leaf formats to set
XFS_DABUF_NOWAIT, and we do the DIR_CONTEXT_F_NOWAIT check in
xfs_readdir() as well, we should probably do this check once at the
higher level and pass flags down from there with XFS_DABUF_NOWAIT
already set.

> @@ -240,6 +243,7 @@ xfs_dir2_block_getdents(
>  STATIC int
>  xfs_dir2_leaf_readbuf(
>   struct xfs_da_args  *args,
> + struct dir_context  *ctx,
>   size_t  bufsize,
>   xfs_dir2_off_t  *cur_off,
>   xfs_dablk_t *ra_blk,
> @@ -258,10 +262,15 @@ xfs_dir2_leaf_readbuf(
>   struct xfs_iext_cursor  icur;
>   int ra_want;
>   int error = 0;
> -
> - error = xfs_iread_extents(args->trans, dp, XFS_DATA_FORK);
> - if (error)
> - goto out;
> + unsigned intflags = 0;
> +
> + if (ctx->flags & DIR_CONTEXT_F_NOWAIT) {
> + flags |= XFS_DABUF_NOWAIT;
> + } else {
> + error = xfs_iread_extents(args->trans, dp, XFS_DATA_FORK);
> + if (error)
> + goto out;
> + }

Especially as, in hindsight, this doesn't make a whole lot of sense.
If XFS_DABUF_NOWAIT is set, we keep going until
xfs_ilock_data_map_shared_nowait() where we call
xfs_need_iread_extents() to see if we need to read the extents in
and abort at that point.

So, really, we shouldn't get this far with nowait semantics if
we haven't read the extents in yet - we're supposed to already have
the inode locked here and so we should have already checked this
condition before we bother locking the inode...

i.e. all we should be doing here is this:

if (!(flags & XFS_DABUF_NOWAIT)) {
error = xfs_iread_extents(args->trans, dp, XFS_DATA_FORK);
if (error)
goto out;
}

And then we don't need to pass the VFS dir_context down into low
level XFS functions unnecessarily.


>  
>   /*
>* Look for mapped directory blocks at or above the current offset.
> @@ -280,7 +289,7 @@ xfs_dir2_leaf_readbuf(
>   new_off = xfs_dir2_da_to_byte(geo, map.br_startoff);
>   if (new_off > *cur_off)
>   *cur_off = new_off;
> - error = xfs_dir3_data_read(args->trans, dp, map.br_startoff, 0, );
> + error = xfs_dir3_data_read(args->trans, dp, map.br_startoff, flags, 
> );
>   if (error)
>   goto out;
>  
> @@ -360,6 +369,7 @@ xfs_dir2_leaf_getdents(
>   int byteoff;/* offset in current block */
>   unsigned intoffset = 0;
>   int error = 0;  /* error return value */
> + int written = 0;
>  
>   /*
>* If the offset is at or past the largest allowed value,
> @@ -391,10 +401,17 @@ xfs_dir2_leaf_getdents(
>   bp = NULL;
>   }
>  
> - if (*lock_mode == 0)
> - *lock_mode = xfs_ilock_data_map_shared(dp);
> - error = xfs_dir2_leaf_readbuf(args, bufsize, ,
> - , );
> + if (*lock_mode == 0) {
> + *lock_mode =
> + xfs_ilock_data_map_shared_generic(dp,
> + ctx->flags & DIR_CONTEXT_F_NOWAIT);
> + if (!*lock_mode) {
> + error = -EAGAIN;
> + break;
> + }
> + }
> + error = 

Re: [Linux-cachefs] [PATCH 02/11] xfs: add NOWAIT semantics for readdir

2023-08-30 Thread Hao Xu

On 8/28/23 04:44, Matthew Wilcox wrote:

On Sun, Aug 27, 2023 at 09:28:26PM +0800, Hao Xu wrote:

+++ b/fs/xfs/libxfs/xfs_da_btree.c
@@ -2643,16 +2643,32 @@ xfs_da_read_buf(
struct xfs_buf_map  map, *mapp = 
int nmap = 1;
int error;
+   int buf_flags = 0;
  
  	*bpp = NULL;

error = xfs_dabuf_map(dp, bno, flags, whichfork, , );
if (error || !nmap)
goto out_free;
  
+	/*

+* NOWAIT semantics mean we don't wait on the buffer lock nor do we
+* issue IO for this buffer if it is not already in memory. Caller will
+* retry. This will return -EAGAIN if the buffer is in memory and cannot
+* be locked, and no buffer and no error if it isn't in memory.  We
+* translate both of those into a return state of -EAGAIN and *bpp =
+* NULL.
+*/


I would not include this comment.


No strong comment here, since this patch is mostly from Dave, it's
better if Dave can ack this.




+   if (flags & XFS_DABUF_NOWAIT)
+   buf_flags |= XBF_TRYLOCK | XBF_INCORE;
error = xfs_trans_read_buf_map(mp, tp, mp->m_ddev_targp, mapp, nmap, 0,
, ops);


what tsting did you do with this?  Because you don't actually _use_
buf_flags anywhere in this patch (presumably they should be the
sixth argument to xfs_trans_read_buf_map() instead of 0).  So I can only
conclude that either you didn't test, or your testing was inadequate.




The tests I've done are listed in the cover-letter, this one is missed, 
the tricky place is it's hard to get this kind of mistake since it runs

well without nowait logic...I'll fix it in next version.


if (error)
goto out_free;
+   if (!bp) {
+   ASSERT(flags & XFS_DABUF_NOWAIT);


I don't think this ASSERT is appropriate.


@@ -391,10 +401,17 @@ xfs_dir2_leaf_getdents(
bp = NULL;
}
  
-			if (*lock_mode == 0)

-   *lock_mode = xfs_ilock_data_map_shared(dp);
+   if (*lock_mode == 0) {
+   *lock_mode =
+   xfs_ilock_data_map_shared_generic(dp,
+   ctx->flags & DIR_CONTEXT_F_NOWAIT);
+   if (!*lock_mode) {
+   error = -EAGAIN;
+   break;
+   }
+   }


'generic' doesn't seem like a great suffix to mean 'takes nowait flag'.
And this is far too far indented.

xfs_dir2_lock(dp, ctx, lock_mode);

with:

STATIC void xfs_dir2_lock(struct xfs_inode *dp, struct dir_context *ctx,
unsigned int lock_mode)
{
if (*lock_mode)
return;
if (ctx->flags & DIR_CONTEXT_F_NOWAIT)
return xfs_ilock_data_map_shared_nowait(dp);
return xfs_ilock_data_map_shared(dp);
}

... which I think you can use elsewhere in this patch (reformat it to
XFS coding style, of course).  And then you don't need
xfs_ilock_data_map_shared_generic().



How about rename xfs_ilock_data_map_shared() to 
xfs_ilock_data_map_block() and rename 
xfs_ilock_data_map_shared_generic() to xfs_ilock_data_map_shared()?


STATIC void xfs_ilock_data_map_shared(struct xfs_inode *dp, struct 
dir_context *ctx, unsigned int lock_mode)

{
if (*lock_mode)
return;
if (ctx->flags & DIR_CONTEXT_F_NOWAIT)
return xfs_ilock_data_map_shared_nowait(dp);
return xfs_ilock_data_map_shared_block(dp);
}


--
Linux-cachefs mailing list
Linux-cachefs@redhat.com
https://listman.redhat.com/mailman/listinfo/linux-cachefs



[Linux-cachefs] [PATCH 02/11] xfs: add NOWAIT semantics for readdir

2023-08-30 Thread Hao Xu
From: Hao Xu 

Implement NOWAIT semantics for readdir. Return EAGAIN error to the
caller if it would block, like failing to get locks, or going to
do IO.

Co-developed-by: Dave Chinner 
Signed-off-by: Dave Chinner 
Signed-off-by: Hao Xu 
[fixes deadlock issue, tweak code style]
---
 fs/xfs/libxfs/xfs_da_btree.c   | 16 +++
 fs/xfs/libxfs/xfs_da_btree.h   |  1 +
 fs/xfs/libxfs/xfs_dir2_block.c |  7 ++---
 fs/xfs/libxfs/xfs_dir2_priv.h  |  2 +-
 fs/xfs/scrub/dir.c |  2 +-
 fs/xfs/scrub/readdir.c |  2 +-
 fs/xfs/xfs_dir2_readdir.c  | 49 ++
 fs/xfs/xfs_inode.c | 27 +++
 fs/xfs/xfs_inode.h | 17 +++-
 9 files changed, 99 insertions(+), 24 deletions(-)

diff --git a/fs/xfs/libxfs/xfs_da_btree.c b/fs/xfs/libxfs/xfs_da_btree.c
index e576560b46e9..7a1a0af24197 100644
--- a/fs/xfs/libxfs/xfs_da_btree.c
+++ b/fs/xfs/libxfs/xfs_da_btree.c
@@ -2643,16 +2643,32 @@ xfs_da_read_buf(
struct xfs_buf_map  map, *mapp = 
int nmap = 1;
int error;
+   int buf_flags = 0;
 
*bpp = NULL;
error = xfs_dabuf_map(dp, bno, flags, whichfork, , );
if (error || !nmap)
goto out_free;
 
+   /*
+* NOWAIT semantics mean we don't wait on the buffer lock nor do we
+* issue IO for this buffer if it is not already in memory. Caller will
+* retry. This will return -EAGAIN if the buffer is in memory and cannot
+* be locked, and no buffer and no error if it isn't in memory.  We
+* translate both of those into a return state of -EAGAIN and *bpp =
+* NULL.
+*/
+   if (flags & XFS_DABUF_NOWAIT)
+   buf_flags |= XBF_TRYLOCK | XBF_INCORE;
error = xfs_trans_read_buf_map(mp, tp, mp->m_ddev_targp, mapp, nmap, 0,
, ops);
if (error)
goto out_free;
+   if (!bp) {
+   ASSERT(flags & XFS_DABUF_NOWAIT);
+   error = -EAGAIN;
+   goto out_free;
+   }
 
if (whichfork == XFS_ATTR_FORK)
xfs_buf_set_ref(bp, XFS_ATTR_BTREE_REF);
diff --git a/fs/xfs/libxfs/xfs_da_btree.h b/fs/xfs/libxfs/xfs_da_btree.h
index ffa3df5b2893..32e7b1cca402 100644
--- a/fs/xfs/libxfs/xfs_da_btree.h
+++ b/fs/xfs/libxfs/xfs_da_btree.h
@@ -205,6 +205,7 @@ int xfs_da3_node_read_mapped(struct xfs_trans *tp, struct 
xfs_inode *dp,
  */
 
 #define XFS_DABUF_MAP_HOLE_OK  (1u << 0)
+#define XFS_DABUF_NOWAIT   (1u << 1)
 
 intxfs_da_grow_inode(xfs_da_args_t *args, xfs_dablk_t *new_blkno);
 intxfs_da_grow_inode_int(struct xfs_da_args *args, xfs_fileoff_t *bno,
diff --git a/fs/xfs/libxfs/xfs_dir2_block.c b/fs/xfs/libxfs/xfs_dir2_block.c
index 00f960a703b2..59b24a594add 100644
--- a/fs/xfs/libxfs/xfs_dir2_block.c
+++ b/fs/xfs/libxfs/xfs_dir2_block.c
@@ -135,13 +135,14 @@ int
 xfs_dir3_block_read(
struct xfs_trans*tp,
struct xfs_inode*dp,
+   unsigned intflags,
struct xfs_buf  **bpp)
 {
struct xfs_mount*mp = dp->i_mount;
xfs_failaddr_t  fa;
int err;
 
-   err = xfs_da_read_buf(tp, dp, mp->m_dir_geo->datablk, 0, bpp,
+   err = xfs_da_read_buf(tp, dp, mp->m_dir_geo->datablk, flags, bpp,
XFS_DATA_FORK, _dir3_block_buf_ops);
if (err || !*bpp)
return err;
@@ -380,7 +381,7 @@ xfs_dir2_block_addname(
tp = args->trans;
 
/* Read the (one and only) directory block into bp. */
-   error = xfs_dir3_block_read(tp, dp, );
+   error = xfs_dir3_block_read(tp, dp, 0, );
if (error)
return error;
 
@@ -695,7 +696,7 @@ xfs_dir2_block_lookup_int(
dp = args->dp;
tp = args->trans;
 
-   error = xfs_dir3_block_read(tp, dp, );
+   error = xfs_dir3_block_read(tp, dp, 0, );
if (error)
return error;
 
diff --git a/fs/xfs/libxfs/xfs_dir2_priv.h b/fs/xfs/libxfs/xfs_dir2_priv.h
index 7404a9ff1a92..7d4cf8a0f15b 100644
--- a/fs/xfs/libxfs/xfs_dir2_priv.h
+++ b/fs/xfs/libxfs/xfs_dir2_priv.h
@@ -51,7 +51,7 @@ extern int xfs_dir_cilookup_result(struct xfs_da_args *args,
 
 /* xfs_dir2_block.c */
 extern int xfs_dir3_block_read(struct xfs_trans *tp, struct xfs_inode *dp,
-  struct xfs_buf **bpp);
+  unsigned int flags, struct xfs_buf **bpp);
 extern int xfs_dir2_block_addname(struct xfs_da_args *args);
 extern int xfs_dir2_block_lookup(struct xfs_da_args *args);
 extern int xfs_dir2_block_removename(struct xfs_da_args *args);
diff --git a/fs/xfs/scrub/dir.c b/fs/xfs/scrub/dir.c
index 0b491784b759..5cc51f201bd7 100644
--- a/fs/xfs/scrub/dir.c
+++ b/fs/xfs/scrub/dir.c
@@ -313,7 +313,7 @@ xchk_directory_data_bestfree(
/* dir block format */

Re: [Linux-cachefs] [PATCH 02/11] xfs: add NOWAIT semantics for readdir

2023-08-29 Thread Matthew Wilcox
On Tue, Aug 29, 2023 at 03:41:43PM +0800, Hao Xu wrote:
> On 8/28/23 04:44, Matthew Wilcox wrote:
> > > @@ -391,10 +401,17 @@ xfs_dir2_leaf_getdents(
> > >   bp = NULL;
> > >   }
> > > - if (*lock_mode == 0)
> > > - *lock_mode = xfs_ilock_data_map_shared(dp);
> > > + if (*lock_mode == 0) {
> > > + *lock_mode =
> > > + xfs_ilock_data_map_shared_generic(dp,
> > > + ctx->flags & DIR_CONTEXT_F_NOWAIT);
> > > + if (!*lock_mode) {
> > > + error = -EAGAIN;
> > > + break;
> > > + }
> > > + }
> > 
> > 'generic' doesn't seem like a great suffix to mean 'takes nowait flag'.
> > And this is far too far indented.
> > 
> > xfs_dir2_lock(dp, ctx, lock_mode);
> > 
> > with:
> > 
> > STATIC void xfs_dir2_lock(struct xfs_inode *dp, struct dir_context *ctx,
> > unsigned int lock_mode)
> > {
> > if (*lock_mode)
> > return;
> > if (ctx->flags & DIR_CONTEXT_F_NOWAIT)
> > return xfs_ilock_data_map_shared_nowait(dp);
> > return xfs_ilock_data_map_shared(dp);
> > }
> > 
> > ... which I think you can use elsewhere in this patch (reformat it to
> > XFS coding style, of course).  And then you don't need
> > xfs_ilock_data_map_shared_generic().
> 
> How about rename xfs_ilock_data_map_shared() to xfs_ilock_data_map_block()
> and rename xfs_ilock_data_map_shared_generic() to
> xfs_ilock_data_map_shared()?
> 
> STATIC void xfs_ilock_data_map_shared(struct xfs_inode *dp, struct
> dir_context *ctx, unsigned int lock_mode)
> {
>   if (*lock_mode)
>   return;
>   if (ctx->flags & DIR_CONTEXT_F_NOWAIT)
>   return xfs_ilock_data_map_shared_nowait(dp);
>   return xfs_ilock_data_map_shared_block(dp);
> }

xfs_ilock_data_map_shared() is used for a lot of things which are not
directories.  I think a new function name is appropriate, and that
function name should include the word 'dir' in it somewhere.

--
Linux-cachefs mailing list
Linux-cachefs@redhat.com
https://listman.redhat.com/mailman/listinfo/linux-cachefs



Re: [Linux-cachefs] [PATCH 02/11] xfs: add NOWAIT semantics for readdir

2023-08-27 Thread Matthew Wilcox
On Sun, Aug 27, 2023 at 09:28:26PM +0800, Hao Xu wrote:
> +++ b/fs/xfs/libxfs/xfs_da_btree.c
> @@ -2643,16 +2643,32 @@ xfs_da_read_buf(
>   struct xfs_buf_map  map, *mapp = 
>   int nmap = 1;
>   int error;
> + int buf_flags = 0;
>  
>   *bpp = NULL;
>   error = xfs_dabuf_map(dp, bno, flags, whichfork, , );
>   if (error || !nmap)
>   goto out_free;
>  
> + /*
> +  * NOWAIT semantics mean we don't wait on the buffer lock nor do we
> +  * issue IO for this buffer if it is not already in memory. Caller will
> +  * retry. This will return -EAGAIN if the buffer is in memory and cannot
> +  * be locked, and no buffer and no error if it isn't in memory.  We
> +  * translate both of those into a return state of -EAGAIN and *bpp =
> +  * NULL.
> +  */

I would not include this comment.

> + if (flags & XFS_DABUF_NOWAIT)
> + buf_flags |= XBF_TRYLOCK | XBF_INCORE;
>   error = xfs_trans_read_buf_map(mp, tp, mp->m_ddev_targp, mapp, nmap, 0,
>   , ops);

what tsting did you do with this?  Because you don't actually _use_
buf_flags anywhere in this patch (presumably they should be the
sixth argument to xfs_trans_read_buf_map() instead of 0).  So I can only
conclude that either you didn't test, or your testing was inadequate.

>   if (error)
>   goto out_free;
> + if (!bp) {
> + ASSERT(flags & XFS_DABUF_NOWAIT);

I don't think this ASSERT is appropriate.

> @@ -391,10 +401,17 @@ xfs_dir2_leaf_getdents(
>   bp = NULL;
>   }
>  
> - if (*lock_mode == 0)
> - *lock_mode = xfs_ilock_data_map_shared(dp);
> + if (*lock_mode == 0) {
> + *lock_mode =
> + xfs_ilock_data_map_shared_generic(dp,
> + ctx->flags & DIR_CONTEXT_F_NOWAIT);
> + if (!*lock_mode) {
> + error = -EAGAIN;
> + break;
> + }
> + }

'generic' doesn't seem like a great suffix to mean 'takes nowait flag'.
And this is far too far indented.

xfs_dir2_lock(dp, ctx, lock_mode);

with:

STATIC void xfs_dir2_lock(struct xfs_inode *dp, struct dir_context *ctx,
unsigned int lock_mode)
{
if (*lock_mode)
return;
if (ctx->flags & DIR_CONTEXT_F_NOWAIT)
return xfs_ilock_data_map_shared_nowait(dp);
return xfs_ilock_data_map_shared(dp);
}

... which I think you can use elsewhere in this patch (reformat it to
XFS coding style, of course).  And then you don't need
xfs_ilock_data_map_shared_generic().

--
Linux-cachefs mailing list
Linux-cachefs@redhat.com
https://listman.redhat.com/mailman/listinfo/linux-cachefs