diff mbox series

[v2,2/2] xfsdump: intercept bind mount targets

Message ID 20201103023315.786103-2-hsiangkao@redhat.com (mailing list archive)
State Superseded
Headers show
Series [v2,1/2] xfsdump: Revert "xfsdump: handle bind mount targets" | expand

Commit Message

Gao Xiang Nov. 3, 2020, 2:33 a.m. UTC
It's a bit strange pointing at some non-root bind mount target and
then actually dumping from the actual root dir instead.

Therefore, instead of searching for the root dir of the filesystem,
just intercept all bind mount targets by checking whose ino # of
".." is itself with getdents.

Fixes: 25195ebf107d ("xfsdump: handle bind mount targets")
Cc: Eric Sandeen <sandeen@redhat.com>
Signed-off-by: Gao Xiang <hsiangkao@redhat.com>
---
changes since v1:
 just intercept bind mount targets suggested by Eric on IRC instead,
 and no need to use XFS_BULK_IREQ_SPECIAL_ROOT since the xfs header
 files and the kernel on my laptop don't support it, plus
 xfsdump/xfsrestore are not part of xfsprogs, so a bit hard to
 sync and make full use of that elegantly.

 dump/content.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 62 insertions(+)

Comments

Eric Sandeen Nov. 3, 2020, 3:03 p.m. UTC | #1
On 11/2/20 8:33 PM, Gao Xiang wrote:
> It's a bit strange pointing at some non-root bind mount target and
> then actually dumping from the actual root dir instead.
> 
> Therefore, instead of searching for the root dir of the filesystem,
> just intercept all bind mount targets by checking whose ino # of
> ".." is itself with getdents.
> 
> Fixes: 25195ebf107d ("xfsdump: handle bind mount targets")
> Cc: Eric Sandeen <sandeen@redhat.com>
> Signed-off-by: Gao Xiang <hsiangkao@redhat.com>
> ---
> changes since v1:
>  just intercept bind mount targets suggested by Eric on IRC instead,
>  and no need to use XFS_BULK_IREQ_SPECIAL_ROOT since the xfs header
>  files and the kernel on my laptop don't support it, plus
>  xfsdump/xfsrestore are not part of xfsprogs, so a bit hard to
>  sync and make full use of that elegantly.

Thank you for doing this - it seems to be the least intrusive method to
finally fix this problem, I really appreciate you taking the work.

> 
>  dump/content.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 62 insertions(+)
> 
> diff --git a/dump/content.c b/dump/content.c
> index c11d9b4..7a55b92 100644
> --- a/dump/content.c
> +++ b/dump/content.c
> @@ -511,6 +511,61 @@ static bool_t create_inv_session(
>  		ix_t subtreecnt,
>  		size_t strmix);
>  
> +static bool_t
> +check_rootdir(int fd,
> +	      xfs_ino_t ino)
> +{
> +	struct dirent	*gdp;
> +	size_t		gdsz;
> +
> +	gdsz = sizeof(struct dirent) + NAME_MAX + 1;
> +	if (gdsz < GETDENTSBUF_SZ_MIN)
> +		gdsz = GETDENTSBUF_SZ_MIN;
> +	gdp = (struct dirent *)calloc(1, gdsz);
> +	assert(gdp);
> +
> +	while (1) {
> +		struct dirent *p;
> +		int nread;
> +
> +		nread = getdents_wrap(fd, (char *)gdp, gdsz);
> +		/*
> +		 * negative count indicates something very bad happened;
> +		 * try to gracefully end this dir.
> +		 */
> +		if (nread < 0) {
> +			mlog(MLOG_NORMAL | MLOG_WARNING,
> +_("unable to read dirents for directory ino %llu: %s\n"),
> +			      ino, strerror(errno));
> +			/* !!! curtis looked at this, and pointed out that

Nobody knows who curtis is, I think we can drop this comment now ;)
If we can't read the directory I think it's fine to simply error out here.

> +			 * we could take some recovery action here. if the
> +			 * errno is appropriate, lseek64 to the value of
> +			 * doff field of the last dirent successfully
> +			 * obtained, and contiue the loop.
> +			 */
> +			nread = 0; /* pretend we are done */
> +		}
> +
> +		/* no more directory entries: break; */
> +		if (!nread)
> +			break;
> +
> +		for (p = gdp; nread > 0;
> +		     nread -= (int)p->d_reclen,
> +		     assert(nread >= 0),
> +		     p = (struct dirent *)((char *)p + p->d_reclen)) {
> +			if (!strcmp(p->d_name, "..") && p->d_ino == ino) {
> +				mlog(MLOG_DEBUG, "FOUND: name %s d_ino %llu\n",
> +				     p->d_name, ino);
> +				free(gdp);
> +				return BOOL_TRUE;
> +			}

I think we can stop as soon as we have found ".." yes?  No need to continue
iterating the directory, either ".." is what we wanted, or it's not, but either
way we are done when we have checked it.  On the off chance that we have
a very large root dir, stopping early might be good.

> +		}
> +	}
> +	free(gdp);
> +	return BOOL_FALSE;
> +}
> +
>  bool_t
>  content_init(int argc,
>  	      char *argv[],
> @@ -1393,6 +1448,13 @@ baseuuidbypass:
>  			      mntpnt);
>  			return BOOL_FALSE;
>  		}
> +
> +		if (!check_rootdir(sc_fsfd, rootstat.st_ino)) {
> +			mlog(MLOG_ERROR,
> +"oops, seems to be a bind mount, please use the actual mountpoint instead\n");

Could there be any other reason for this failure?  Maybe something like:

			mlog(MLOG_ERROR,
_("%s is not the root of the filesystem (bind mount?) - use primary mountpoint\n"),
				mntpnt);

or similar?

in any case I think it needs the i18n _("...") treatment.

Thanks!

-Eric

> +			return BOOL_FALSE;
> +		}
> +
>  		sc_rootxfsstatp =
>  			(struct xfs_bstat *)calloc(1, sizeof(struct xfs_bstat));
>  		assert(sc_rootxfsstatp);
>
Gao Xiang Nov. 3, 2020, 3:18 p.m. UTC | #2
Hi Eric,

On Tue, Nov 03, 2020 at 09:03:48AM -0600, Eric Sandeen wrote:
> On 11/2/20 8:33 PM, Gao Xiang wrote:

...

> > +
> > +		nread = getdents_wrap(fd, (char *)gdp, gdsz);
> > +		/*
> > +		 * negative count indicates something very bad happened;
> > +		 * try to gracefully end this dir.
> > +		 */
> > +		if (nread < 0) {
> > +			mlog(MLOG_NORMAL | MLOG_WARNING,
> > +_("unable to read dirents for directory ino %llu: %s\n"),
> > +			      ino, strerror(errno));
> > +			/* !!! curtis looked at this, and pointed out that
> 
> Nobody knows who curtis is, I think we can drop this comment now ;)
> If we can't read the directory I think it's fine to simply error out here.

This was copied from dump_dir(), ok, I will error out this.

> 
> > +			 * we could take some recovery action here. if the
> > +			 * errno is appropriate, lseek64 to the value of
> > +			 * doff field of the last dirent successfully
> > +			 * obtained, and contiue the loop.
> > +			 */
> > +			nread = 0; /* pretend we are done */
> > +		}
> > +
> > +		/* no more directory entries: break; */
> > +		if (!nread)
> > +			break;
> > +
> > +		for (p = gdp; nread > 0;
> > +		     nread -= (int)p->d_reclen,
> > +		     assert(nread >= 0),
> > +		     p = (struct dirent *)((char *)p + p->d_reclen)) {
> > +			if (!strcmp(p->d_name, "..") && p->d_ino == ino) {
> > +				mlog(MLOG_DEBUG, "FOUND: name %s d_ino %llu\n",
> > +				     p->d_name, ino);
> > +				free(gdp);
> > +				return BOOL_TRUE;
> > +			}
> 
> I think we can stop as soon as we have found ".." yes?  No need to continue
> iterating the directory, either ".." is what we wanted, or it's not, but either
> way we are done when we have checked it.  On the off chance that we have
> a very large root dir, stopping early might be good.

Yes, that is correct.

> 
> > +		}
> > +	}
> > +	free(gdp);
> > +	return BOOL_FALSE;
> > +}
> > +
> >  bool_t
> >  content_init(int argc,
> >  	      char *argv[],
> > @@ -1393,6 +1448,13 @@ baseuuidbypass:
> >  			      mntpnt);
> >  			return BOOL_FALSE;
> >  		}
> > +
> > +		if (!check_rootdir(sc_fsfd, rootstat.st_ino)) {
> > +			mlog(MLOG_ERROR,
> > +"oops, seems to be a bind mount, please use the actual mountpoint instead\n");
> 
> Could there be any other reason for this failure?  Maybe something like:
> 
> 			mlog(MLOG_ERROR,
> _("%s is not the root of the filesystem (bind mount?) - use primary mountpoint\n"),
> 				mntpnt);
> 
> or similar?
> 
> in any case I think it needs the i18n _("...") treatment.

Ok, will quickly send the fixed version about this!


Thanks for your review!


Thanks,
Gao Xiang

> 
> Thanks!
> 
> -Eric
> 
> > +			return BOOL_FALSE;
> > +		}
> > +
> >  		sc_rootxfsstatp =
> >  			(struct xfs_bstat *)calloc(1, sizeof(struct xfs_bstat));
> >  		assert(sc_rootxfsstatp);
> > 
>
diff mbox series

Patch

diff --git a/dump/content.c b/dump/content.c
index c11d9b4..7a55b92 100644
--- a/dump/content.c
+++ b/dump/content.c
@@ -511,6 +511,61 @@  static bool_t create_inv_session(
 		ix_t subtreecnt,
 		size_t strmix);
 
+static bool_t
+check_rootdir(int fd,
+	      xfs_ino_t ino)
+{
+	struct dirent	*gdp;
+	size_t		gdsz;
+
+	gdsz = sizeof(struct dirent) + NAME_MAX + 1;
+	if (gdsz < GETDENTSBUF_SZ_MIN)
+		gdsz = GETDENTSBUF_SZ_MIN;
+	gdp = (struct dirent *)calloc(1, gdsz);
+	assert(gdp);
+
+	while (1) {
+		struct dirent *p;
+		int nread;
+
+		nread = getdents_wrap(fd, (char *)gdp, gdsz);
+		/*
+		 * negative count indicates something very bad happened;
+		 * try to gracefully end this dir.
+		 */
+		if (nread < 0) {
+			mlog(MLOG_NORMAL | MLOG_WARNING,
+_("unable to read dirents for directory ino %llu: %s\n"),
+			      ino, strerror(errno));
+			/* !!! curtis looked at this, and pointed out that
+			 * we could take some recovery action here. if the
+			 * errno is appropriate, lseek64 to the value of
+			 * doff field of the last dirent successfully
+			 * obtained, and contiue the loop.
+			 */
+			nread = 0; /* pretend we are done */
+		}
+
+		/* no more directory entries: break; */
+		if (!nread)
+			break;
+
+		for (p = gdp; nread > 0;
+		     nread -= (int)p->d_reclen,
+		     assert(nread >= 0),
+		     p = (struct dirent *)((char *)p + p->d_reclen)) {
+			if (!strcmp(p->d_name, "..") && p->d_ino == ino) {
+				mlog(MLOG_DEBUG, "FOUND: name %s d_ino %llu\n",
+				     p->d_name, ino);
+				free(gdp);
+				return BOOL_TRUE;
+			}
+		}
+	}
+	free(gdp);
+	return BOOL_FALSE;
+}
+
 bool_t
 content_init(int argc,
 	      char *argv[],
@@ -1393,6 +1448,13 @@  baseuuidbypass:
 			      mntpnt);
 			return BOOL_FALSE;
 		}
+
+		if (!check_rootdir(sc_fsfd, rootstat.st_ino)) {
+			mlog(MLOG_ERROR,
+"oops, seems to be a bind mount, please use the actual mountpoint instead\n");
+			return BOOL_FALSE;
+		}
+
 		sc_rootxfsstatp =
 			(struct xfs_bstat *)calloc(1, sizeof(struct xfs_bstat));
 		assert(sc_rootxfsstatp);