* [PATCH v3] mkfs: simplify setup_proto file status checks
@ 2026-06-29 10:08 liuh
2026-06-29 23:47 ` Darrick J. Wong
0 siblings, 1 reply; 2+ messages in thread
From: liuh @ 2026-06-29 10:08 UTC (permalink / raw)
To: djwong; +Cc: linux-xfs, liuh
setup_proto() calls filesize() before validating the source type with
fstat(), even though both operations ultimately require the same file
status information.
Perform a single fstat() immediately after opening the source path and
reuse the resulting metadata for both source type validation and file
size retrieval. This simplifies the setup logic and eliminates a
redundant metadata lookup.
Add O_NONBLOCK to open() to avoid blocking on special file types
(e.g. FIFOs or device nodes). Descriptor is only used for fstat(),
so semantics are unchanged.
Signed-off-by: liuh <liuhuan01@kylinos.cn>
---
mkfs/proto.c | 17 ++++++++++++++---
1 file changed, 14 insertions(+), 3 deletions(-)
diff --git a/mkfs/proto.c b/mkfs/proto.c
index a460aebd..c14e035f 100644
--- a/mkfs/proto.c
+++ b/mkfs/proto.c
@@ -82,14 +82,17 @@ setup_proto(
return result;
}
- if ((fd = open(fname, O_RDONLY)) < 0 || (size = filesize(fd)) < 0) {
+ if ((fd = open(fname, O_RDONLY|O_NONBLOCK)) < 0) {
fprintf(stderr, _("%s: failed to open %s: %s\n"),
progname, fname, strerror(errno));
goto out_fail;
}
- if (fstat(fd, &statbuf) < 0)
- fail(_("invalid or unreadable source path"), errno);
+ if (fstat(fd, &statbuf) < 0) {
+ fprintf(stderr, _("%s: failed to fstat %s: %s\n"),
+ progname, fname, strerror(errno));
+ goto out_fail;
+ }
/*
* Handle directory inputs.
@@ -101,6 +104,14 @@ setup_proto(
return result;
}
+ if (!S_ISREG(statbuf.st_mode)) {
+ fprintf(stderr, _("%s: %s not a regular file\n"),
+ progname, fname);
+ goto out_fail;
+ }
+
+ size = statbuf.st_size;
+
/*
* Else this is a protofile, let's handle traditionally.
*/
--
2.43.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH v3] mkfs: simplify setup_proto file status checks
2026-06-29 10:08 [PATCH v3] mkfs: simplify setup_proto file status checks liuh
@ 2026-06-29 23:47 ` Darrick J. Wong
0 siblings, 0 replies; 2+ messages in thread
From: Darrick J. Wong @ 2026-06-29 23:47 UTC (permalink / raw)
To: liuh; +Cc: linux-xfs
On Mon, Jun 29, 2026 at 06:08:48PM +0800, liuh wrote:
> setup_proto() calls filesize() before validating the source type with
> fstat(), even though both operations ultimately require the same file
> status information.
>
> Perform a single fstat() immediately after opening the source path and
> reuse the resulting metadata for both source type validation and file
> size retrieval. This simplifies the setup logic and eliminates a
> redundant metadata lookup.
>
> Add O_NONBLOCK to open() to avoid blocking on special file types
> (e.g. FIFOs or device nodes). Descriptor is only used for fstat(),
> so semantics are unchanged.
Are you sure?? The openat manpage says this about regular files:
"Note that this flag has no effect for regular files and block devices;
that is, I/O operations will (briefly) block when device activity is
required, regardless of whether O_NONBLOCK is set. Since O_NONBLOCK
semantics might eventually be implemented, applications should not
depend upon blocking behavior when specifying this flag for regular
files and block devices."
So while the behavior might not be different *today*, that's no
guarantee that O_NONBLOCK won't ever get turned into RWF_NOWAIT
tomorrow. If people want to pass in FIFOs and sockets as "protofiles"
that then hang, that's their problem.
Please just leave the open flags alone; if you want to explore
O_NONBLOCK then do it in a separate patch.
--D
> Signed-off-by: liuh <liuhuan01@kylinos.cn>
> ---
> mkfs/proto.c | 17 ++++++++++++++---
> 1 file changed, 14 insertions(+), 3 deletions(-)
>
> diff --git a/mkfs/proto.c b/mkfs/proto.c
> index a460aebd..c14e035f 100644
> --- a/mkfs/proto.c
> +++ b/mkfs/proto.c
> @@ -82,14 +82,17 @@ setup_proto(
> return result;
> }
>
> - if ((fd = open(fname, O_RDONLY)) < 0 || (size = filesize(fd)) < 0) {
> + if ((fd = open(fname, O_RDONLY|O_NONBLOCK)) < 0) {
> fprintf(stderr, _("%s: failed to open %s: %s\n"),
> progname, fname, strerror(errno));
> goto out_fail;
> }
>
> - if (fstat(fd, &statbuf) < 0)
> - fail(_("invalid or unreadable source path"), errno);
> + if (fstat(fd, &statbuf) < 0) {
> + fprintf(stderr, _("%s: failed to fstat %s: %s\n"),
> + progname, fname, strerror(errno));
> + goto out_fail;
> + }
>
> /*
> * Handle directory inputs.
> @@ -101,6 +104,14 @@ setup_proto(
> return result;
> }
>
> + if (!S_ISREG(statbuf.st_mode)) {
> + fprintf(stderr, _("%s: %s not a regular file\n"),
> + progname, fname);
> + goto out_fail;
> + }
> +
> + size = statbuf.st_size;
> +
> /*
> * Else this is a protofile, let's handle traditionally.
> */
> --
> 2.43.0
>
>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-06-29 23:47 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-29 10:08 [PATCH v3] mkfs: simplify setup_proto file status checks liuh
2026-06-29 23:47 ` Darrick J. Wong
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox