xfs
[Top] [All Lists]

Re: [PATCH 15/19] mkfs: don't treat files as though they are block devic

To: Eric Sandeen <sandeen@xxxxxxxxxxx>
Subject: Re: [PATCH 15/19] mkfs: don't treat files as though they are block devices
From: Jan Tulak <jtulak@xxxxxxxxxx>
Date: Fri, 8 Apr 2016 16:58:19 +0200
Cc: xfs-oss <xfs@xxxxxxxxxxx>
Delivered-to: xfs@xxxxxxxxxxx
In-reply-to: <5706FA7C.7020103@xxxxxxxxxxx>
References: <1458818136-56043-1-git-send-email-jtulak@xxxxxxxxxx> <1458818136-56043-16-git-send-email-jtulak@xxxxxxxxxx> <5706FA7C.7020103@xxxxxxxxxxx>
On Fri, Apr 8, 2016 at 2:25 AM, Eric Sandeen <sandeen@xxxxxxxxxxx> wrote:
On 3/24/16 6:15 AM, jtulak@xxxxxxxxxx wrote:
> From: Dave Chinner <dchinner@xxxxxxxxxx>
>
> CHANGELOG
> o Fix where xi.dname was incorrectly used instead of dfile
> o Variable alignment (tabs)
> o Added error handling for stat/statfs in init.c
> o Remove a duplicate pread in zero_old_xfs_structures and for the
> remaining call, save the return value in a more meaningful variable.
> o A chunk moved to previous patch.
>
> If the device is actually a file, and "-d file" is not specified,
> mkfs will try to treat it as a block device and get stuff wrong.
> Image files don't necessarily have the same sector sizes as the
> block device or filesystem underlying the image file, nor should we
> be issuing discard ioctls on image files.
>
> To fix this sanely, only require "-d file" if the device name is
> invalid to trigger creation of the file. Otherwise, use stat() to
> determine if the device is a file or block device and deal with that
> appropriately by setting the "isfile" variables and turning off
> direct IO. Then ensure that we check the "isfile" options before
> doing things that are specific to block devices. Also, as direct IO
> is disabled for files, use statfs() for getting host FS blocksize,
> not platform_findsizes().
>
> These changes, however, can cause some tests to fail when the test
> partition on which the file is created has blocksize bigger than 512.
> Before, the underlying fs was ignored. Now, an attempt to create
> a fs in a file with blocksize 512 on a 4096 underlying partition will
> fail.
>
> Other file/blockdev issues fixed:
>Â Â Â Â- use getstr to detect specifying the data device name
>Â Â Â Â Âtwice.
>Â Â Â Â- check file/size/name parameters before anything else.
>Â Â Â Â- overwrite checks need to be done before the image file is
>Â Â Â Â Âopened and potentially truncated.
>Â Â Â Â- blkid_get_topology() should not be called for image files,
>Â Â Â Â Âso warn when it is called that way.
>Â Â Â Â- zero_old_xfs_structures() emits a spurious error:
>Â Â Â Â Â Â Â Â"existing superblock read failed: Success"
>Â Â Â Â Âwhen it is run on a truncated image file. Don't warn if we
>Â Â Â Â Âsee this problem on an image file.
>Â Â Â Â- Don't issue discards on image files.
>Â Â Â Â- Use fsync() for image files, not BLKFLSBUF in
>Â Â Â Â Âplatform_flush_device() for Linux.

This one causes at least one interesting issue:

#mkfs/mkfs.xfs
Error accessing specified device (null): Bad address
Usage: mkfs.xfs
...

because:

    check_device_type(dfile, &xi.disfile, !dsize, !dfile,
             Nflag ? NULL : &xi.dcreat, force_overwrite, "d");

so "dfile" can be NULL, but that function immediately tries to stat it.

âA simple if NULL, then usage() should take care of this...


> Signed-off-by: Dave Chinner <dchinner@xxxxxxxxxx>
> Signed-off-by: Jan Tulak <jtulak@xxxxxxxxxx>
> ---
> libxfs/init.c Â| 12 ++++
> libxfs/linux.c | 12 +++-
>Â mkfs/xfs_mkfs.c | 181 ++++++++++++++++++++++++++++++++++++++------------------
>Â 3 files changed, 147 insertions(+), 58 deletions(-)
>
> diff --git a/libxfs/init.c b/libxfs/init.c
> index 8d747e8..268136f 100644
> --- a/libxfs/init.c
> +++ b/libxfs/init.c
> @@ -246,6 +246,9 @@ libxfs_init(libxfs_init_t *a)
>   Âchar      rtpath[25];
>   Âint      Ârval = 0;
>   Âint      Âflags;
> +  Âstruct     stat st;
> +  Âstruct     statfs stfs;
> +  Âint      Âstatres;
>
>Â Â Â Âdpath[0] = logpath[0] = rtpath[0] = '\0';
>Â Â Â Âdname = a->dname;
> @@ -278,6 +281,15 @@ libxfs_init(libxfs_init_t *a)
>Â Â Â Â Â Â Â Â Â Â Â Âa->ddev= libxfs_device_open(dname, a->dcreat, flags,
>Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âa->setblksize);
>Â Â Â Â Â Â Â Â Â Â Â Âa->dfd = libxfs_device_to_fd(a->ddev);
> +Â Â Â Â Â Â Â Â Â Â Âstatres = stat(dname, &st);
> +Â Â Â Â Â Â Â Â Â Â Âstatres += statfs(dname, &stfs);
> +Â Â Â Â Â Â Â Â Â Â Âif(statres){
             ^space Â^space
> +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âfprintf(stderr, _("%s: stat failed.\n"),
> +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âprogname);
> +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âgoto done;
> +Â Â Â Â Â Â Â Â Â Â Â}
> +Â Â Â Â Â Â Â Â Â Â Âa->dsize = st.st_size/BBSIZE;
> +Â Â Â Â Â Â Â Â Â Â Âa->dbsize = stfs.f_bsize;

ok so for a file you choose ->dsize to be file size in 512-sector units,
and ->dbsize to be the fs block size.

This is all under if (a->disfile); if we didn't actually specify "-dfile"
but it *is* a file, then we get to platform_findsizes() - which handles
files. And handles them differently. Â

âNo, a->disfile is set to 1 implicitly if the target is a file inÂcheck_device_type():
1070 Â Â Â Âif (S_ISREG(statbuf.st_mode)) {
1071 Â Â Â Â Â Â Â if (!*isfile)
1072 Â Â Â Â Â Â Â Â Â Â Â*isfile = 1;


Â
Hm but you removed that (see below)
and added more stat() calls...?

What is the reason for adding these stats at this point?
âWhat is removed? Where exactly? Or it should be "above"?
> +Â Â Â Â Â Â Â Â Â Â Âstatres = stat(dname, &st);
> +Â Â Â Â Â Â Â Â Â Â Âstatres += statfs(dname, &stfs);
This is to get dsize and dbsize values for the file.

(and if there's a reason, why only for ->disfile but not ->lisfile?)
âBecause I forgot or didn't noticed. :-) Adding to lisfile and rtfile too.
â
Â

>Â Â Â Â Â Â Â Â} else {
>Â Â Â Â Â Â Â Â Â Â Â Âif (!check_open(dname, flags, &rawfile, &blockfile))
>Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âgoto done;
> diff --git a/libxfs/linux.c b/libxfs/linux.c
> index f6ea1b2..adb8ff1 100644
> --- a/libxfs/linux.c
> +++ b/libxfs/linux.c
> @@ -18,6 +18,7 @@
>
>Â #define ustat __kernel_ustat
>Â #include <mntent.h>
> +#include <sys/vfs.h>
>Â #include <sys/stat.h>
>Â #undef ustat
>Â #include <sys/ustat.h>
> @@ -125,7 +126,16 @@ platform_set_blocksize(int fd, char *path, dev_t device, int blocksize, int fata
>Â void
>Â platform_flush_device(int fd, dev_t device)
>Â {
> -Â Â Âif (major(device) != RAMDISK_MAJOR)
> +Â Â Âstruct stat64Â Âst;
> +Â Â Âif (major(device) == RAMDISK_MAJOR)
> +Â Â Â Â Â Â Âreturn;
> +
> +Â Â Âif (fstat64(fd, &st) < 0)
> +Â Â Â Â Â Â Âreturn;
> +
> +Â Â Âif (S_ISREG(st.st_mode))
> +Â Â Â Â Â Â Âfsync(fd);
> +Â Â Âelse
>Â Â Â Â Â Â Â Âioctl(fd, BLKFLSBUF, 0);
>Â }
>
> diff --git a/mkfs/xfs_mkfs.c b/mkfs/xfs_mkfs.c
> index 9261ed5..7bd9fd5 100644
> --- a/mkfs/xfs_mkfs.c
> +++ b/mkfs/xfs_mkfs.c
> @@ -787,7 +787,7 @@ calc_stripe_factors(
>Â #ifdef ENABLE_BLKID
>Â static int
>Â check_overwrite(
> -  Âchar      *device)
> +  Âconst char   *device)
>Â {
>   Âconst char   *type;
>   Âblkid_probe  Âpr = NULL;
> @@ -804,7 +804,7 @@ check_overwrite(
>Â Â Â Âfd = open(device, O_RDONLY);
>Â Â Â Âif (fd < 0)
>Â Â Â Â Â Â Â Âgoto out;
> -Â Â Âplatform_findsizes(device, fd, &size, &bsz);
> +Â Â Âplatform_findsizes((char *)device, fd, &size, &bsz);
>Â Â Â Âclose(fd);
>
>Â Â Â Â/* nothing to overwrite on a 0-length device */
> @@ -851,7 +851,6 @@ check_overwrite(
>Â Â Â Â Â Â Â Â Â Â Â Â"according to blkid\n"), progname, device);
>Â Â Â Â}
>Â Â Â Âret = 1;
> -
>Â out:
>Â Â Â Âif (pr)
>Â Â Â Â Â Â Â Âblkid_free_probe(pr);
> @@ -877,8 +876,12 @@ static void blkid_get_topology(
>Â Â Â Âstruct stat statbuf;
>
>Â Â Â Â/* can't get topology info from a file */
> -Â Â Âif (!stat(device, &statbuf) && S_ISREG(statbuf.st_mode))
> +Â Â Âif (!stat(device, &statbuf) && S_ISREG(statbuf.st_mode)) {
> +Â Â Â Â Â Â Âfprintf(stderr,
> +Â Â Â_("%s: Warning: trying to probe topology of a file %s!\n"),
> +Â Â Â Â Â Â Â Â Â Â Âprogname, device);
>Â Â Â Â Â Â Â Âreturn;
> +Â Â Â}
>
>Â Â Â Âpr = blkid_new_probe_from_filename(device);
>Â Â Â Âif (!pr)
> @@ -976,35 +979,35 @@ static void get_topology(
>   Âstruct fs_topology   *ft,
>   Âint          Âforce_overwrite)
>Â {
> -Â Â Âstruct stat statbuf;
>Â Â Â Âchar *dfile = xi->volname ? xi->volname : xi->dname;
> +Â Â Âstruct stat statbuf;
> +Â Â Âstruct statfs statfsbuf;
>
>Â Â Â Â/*
> -Â Â Â * If our target is a regular file, use platform_findsizes
> -Â Â Â * to try to obtain the underlying filesystem's requirements
> -Â Â Â * for direct IO; we'll set our sector size to that if possible.
> +Â Â Â * If our target is a regular file, use statfs
> +Â Â Â * to try to obtain the underlying filesystem's blocksize.
>Â Â Â Â */
>Â Â Â Âif (xi->disfile ||
> -Â Â Â Â Â(!stat(dfile, &statbuf) && S_ISREG(statbuf.st_mode))) {
> +Â Â Â Â Â Â Â(!stat(dfile, &statbuf) && S_ISREG(statbuf.st_mode))) {

dave pointed out that this indentation "fix" is incorrect, the line is fine as
it is; it's part of the same conditional; it shouldn't be tabbed into the code
block under the conditional.

âI'm removing this change.



>Â Â Â Â Â Â Â Âint fd;
>Â Â Â Â Â Â Â Âint flags = O_RDONLY;
> -Â Â Â Â Â Â Âlong long dummy;
>
>Â Â Â Â Â Â Â Â/* with xi->disfile we may not have the file yet! */
>Â Â Â Â Â Â Â Âif (xi->disfile)
>Â Â Â Â Â Â Â Â Â Â Â Âflags |= O_CREAT;
>
>Â Â Â Â Â Â Â Âfd = open(dfile, flags, 0666);
> +
>Â Â Â Â Â Â Â Âif (fd >= 0) {
> -Â Â Â Â Â Â Â Â Â Â Âplatform_findsizes(dfile, fd, &dummy, &ft->lsectorsize);
> +Â Â Â Â Â Â Â Â Â Â Âfstatfs(fd, &statfsbuf);

no error checking on fstatfs, but...
âAdded.â
Â

> +Â Â Â Â Â Â Â Â Â Â Âft->lsectorsize = statfsbuf.f_bsize;

Ok, platform_findsizes already explicitly handled regular files, and tries to
find out via an xfs ioctl what the minimum DIO size is, and uses that for
the sector size for the filesystem in the iamge.Â

Now you stat & get the blocksize, and use that instead, but it's likely
to be different:

i.e. before:

# mkfs/mkfs.xfs -f fsfile
meta-data="" Â Â Â Â Â Â Â Âisize=512Â Â agcount=4, agsize=65536 blks
    Â=           Âsectsz=512 Âattr=2, projid32bit=1

after:

# mkfs/mkfs.xfs -f fsfile
meta-data="" Â Â Â Â Â Â Â Âisize=512Â Â agcount=4, agsize=65536 blks
    Â=           Âsectsz=4096 attr=2, projid32bit=1

and also, now:

# mkfs/mkfs.xfs -f -dfile,name=fsfile,size=1g -b size=2048
block size 2048 cannot be smaller than logical sector size 4096

What prompted you to make this change, was there some other problem you
needed to fix?

âBut DIO is disabled for the files, per the commit message:
[...] and turning off
direct IO. Then ensure that we check the "isfile" options before
doing things that are specific to block devices. Also, as direct IO
is disabled for files, use statfs() for getting host FS blocksize,
not platform_findsizes().â

So we have to use whatever the underlying fs tells us, not what the physical device has, right?

âRather, I wonder if there is any reason to keep the platform_findsizes part about regular files - it shouldn't get into the branch ever.

Â

>Â Â Â Â Â Â Â Â Â Â Â Âclose(fd);
> -Â Â Â Â Â Â Â Â Â Â Âft->psectorsize = ft->lsectorsize;

hm, now psectorsize isn't set at all?

âThis looks like a bug, I think the assignment should stay here.
â
Â

>Â Â Â Â Â Â Â Â} else
>Â Â Â Â Â Â Â Â Â Â Â Âft->psectorsize = ft->lsectorsize = BBSIZE;
>Â Â Â Â} else {
>Â Â Â Â Â Â Â Âblkid_get_topology(dfile, &ft->dsunit, &ft->dswidth,
> -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â &ft->lsectorsize, &ft->psectorsize,
> -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â force_overwrite);
> +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â&ft->lsectorsize, &ft->psectorsize,
> +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âforce_overwrite);

please don't change these lines, they line up w/ the function opening paren as
they should.

âSure. â

Â
>Â Â Â Â}
>
>Â Â Â Âif (xi->rtname && !xi->risfile) {
> @@ -1016,6 +1019,75 @@ static void get_topology(
>Â }
>
>Â static void
> +check_device_type(
> +  Âconst char   *name,
> +  Âint      Â*isfile,
> +  Âbool      no_size,
> +  Âbool      no_name,
> +  Âint      Â*create,
> +  Âbool      force_overwrite,
> +  Âconst char   *optname)
> +{
> +Â Â Âstruct stat64 statbuf;
> +
> +Â Â Âif (*isfile && (no_size || no_name)) {
> +Â Â Â Â Â Â Âfprintf(stderr,
> +Â Â Â_("if -%s file then -%s name and -%s size are required\n"),
> +Â Â Â Â Â Â Â Â Â Â Âoptname, optname, optname);
> +Â Â Â Â Â Â Âusage();
> +Â Â Â}
> +
> +Â Â Âif (stat64(name, &statbuf)) {
> +Â Â Â Â Â Â Âif (errno == ENOENT && *isfile) {
> +Â Â Â Â Â Â Â Â Â Â Âif (create)
> +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â*create = 1;
> +Â Â Â Â Â Â Â Â Â Â Âreturn;
> +Â Â Â Â Â Â Â}
> +
> +Â Â Â Â Â Â Âfprintf(stderr,
> +Â Â Â_("Error accessing specified device %s: %s\n"),
> +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âname, strerror(errno));
> +Â Â Â Â Â Â Âusage();
> +Â Â Â Â Â Â Âreturn;
> +Â Â Â}
> +
> +Â Â Âif (!force_overwrite && check_overwrite(name)) {
> +Â Â Â Â Â Â Âfprintf(stderr,
> +Â Â Â_("%s: Use the -f option to force overwrite.\n"),
> +Â Â Â Â Â Â Â Â Â Â Âprogname);
> +Â Â Â Â Â Â Âexit(1);
> +Â Â Â}
> +
> +Â Â Â/*
> +Â Â Â * We only want to completely truncate and recreate an existing file if
> +Â Â Â * we were specifically told it was a file. Set the create flag only in
> +Â Â Â * this case to trigger that behaviour.
> +Â Â Â */
> +Â Â Âif (S_ISREG(statbuf.st_mode)) {
> +Â Â Â Â Â Â Âif (!*isfile)
> +Â Â Â Â Â Â Â Â Â Â Â*isfile = 1;
> +Â Â Â Â Â Â Âelse if (create)
> +Â Â Â Â Â Â Â Â Â Â Â*create = 1;
> +Â Â Â Â Â Â Âreturn;
> +Â Â Â}
> +
> +Â Â Âif (S_ISBLK(statbuf.st_mode)) {
> +Â Â Â Â Â Â Âif (*isfile) {
> +Â Â Â Â Â Â Â Â Â Â Âfprintf(stderr,
> +Â Â Â_("specified \"-%s file\" on a block device %s\n"),
> +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âoptname, name);
> +Â Â Â Â Â Â Â Â Â Â Âusage();
> +Â Â Â Â Â Â Â}
> +Â Â Â Â Â Â Âreturn;
> +Â Â Â}
> +
> +Â Â Âfprintf(stderr,
> +Â Â Â_("specified device %s not a file or block device\n"),
> +Â Â Â Â Â Â Âname);
> +Â Â Âusage();
> +}
> +
> +static void
>Â fixup_log_stripe_unit(
>   Âint      Âlsflag,
>   Âint      Âsunit,
> @@ -1279,7 +1351,6 @@ zero_old_xfs_structures(
>   Â__uint32_t       bsize;
>   Âint          Âi;
>   Âxfs_off_t       Âoff;
> -  Âint          Âtmp;
>
>Â Â Â Â/*
>Â Â Â Â * We open regular files with O_TRUNC|O_CREAT. Nothing to do here...
> @@ -1299,15 +1370,23 @@ zero_old_xfs_structures(
>Â Â Â Â}
>Â Â Â Âmemset(buf, 0, new_sb->sb_sectsize);
>
> -Â Â Âtmp = pread(xi->dfd, buf, new_sb->sb_sectsize, 0);
> -Â Â Âif (tmp < 0) {
> +Â Â Âoff = pread(xi->dfd, buf, new_sb->sb_sectsize, 0);
> +Â Â Âif (off < 0) {
>Â Â Â Â Â Â Â Âfprintf(stderr, _("existing superblock read failed: %s\n"),
>Â Â Â Â Â Â Â Â Â Â Â Âstrerror(errno));
>Â Â Â Â Â Â Â Âgoto done;
>Â Â Â Â}
> -Â Â Âif (tmp != new_sb->sb_sectsize) {
> -Â Â Â Â Â Â Âfprintf(stderr,
> -Â Â Â_("warning: could not read existing superblock, skip zeroing\n"));
> +Â Â Â/*
> +Â Â Â * If we are creating an image file, it might be of zero length at this
> +Â Â Â * point in time. Hence reading the existing superblock is going to
> +Â Â Â * return zero bytes. It's not a failure we need to warn about in this
> +Â Â Â * case.
> +Â Â Â */

except you already did "if (off < 0) fail" above this.

Ok, at this point I think it might be best to revert to Dave's original version.

If there were specific problems you were trying to address, can you point them out?

Thanks,
-Eric

â(inserting your next email)â
Â

On 4/7/16 7:25 PM, Eric Sandeen wrote:
>> @@ -1299,15 +1370,23 @@ zero_old_xfs_structures(
>> >Â Â }
>> >Â Â memset(buf, 0, new_sb->sb_sectsize);
>> >
>> > -Â tmp = pread(xi->dfd, buf, new_sb->sb_sectsize, 0);
>> > -Â if (tmp < 0) {
>> > +Â off = pread(xi->dfd, buf, new_sb->sb_sectsize, 0);
>> > +Â if (off < 0) {
>> >Â Â Â Â Â Â fprintf(stderr, _("existing superblock read failed: %s\n"),
>> >Â Â Â Â Â Â Â Â Â Â strerror(errno));
>> >Â Â Â Â Â Â goto done;
>> >Â Â }
>> > -Â if (tmp != new_sb->sb_sectsize) {
>> > -Â Â Â Â Â fprintf(stderr,
>> > -Â _("warning: could not read existing superblock, skip zeroing\n"));
>> > +Â /*
>> > +Â Â* If we are creating an image file, it might be of zero length at this
>> > +Â Â* point in time. Hence reading the existing superblock is going to
>> > +Â Â* return zero bytes. It's not a failure we need to warn about in this
>> > +Â Â* case.
>> > +Â Â*/
> except you already did "if (off < 0) fail" above this.

(oh, right, < 0 is different than == 0, sorry; so that part is ok)

Possibly better as:

if (off < 0 || (tmp != new_sb->sb_sectsize && !xi->disfile))
    fprintf("error reading existing superblock ...")
âOK, better to be sure. :-)
â
Â


I still think this patch might need a reset though :)

Thanks,
-Eric

> Ok, at this point I think it might be best to revert to Dave's original version.
>
> If there were specific problems you were trying to address, can you point them out?

âOn few places, the original patch looked as if files had direct IO still enabledâ (using platform_findsize...), and I think it was causing some failures - fixing issues is why I did most of the changes.

I will revert to the original version and see what exactly fails. But with being Friday late afternoon, the results will be available the next week (Wednesday and further, all my courses at university are stuffed in Mon/Tue).

Cheers,
Jan
Â

--
<Prev in Thread] Current Thread [Next in Thread>