mkfs.xfs: factor the crap out of input parsing
RFC PATCH v2 1/1 mkfs.xfs: factor the crap out of input parsing
From: Dave Chinner <[email protected]>
The mkfs.xfs code base is a mess. I started factoring it years ago, and the primary goal I had for that work was to make mkfs.xfs maintainable and lay a solid foundation for being able to modify it in future.
This patch takes up where that work stalled at - factoring the crap out of the input parsing and parameter validation. To do this I hacked together a quick global "cli geometry" structure that holds the handful of values that can be set from the CLI, and pushed all the other feature flag things into the existing sb_feat global structure.
I then factored the sub options parsing loop using a table setup with to call a suboption specific parser function that does the table based option parsing. This gets *all* the suboption parsing out of the main option parsing loop which is soooo much simpler:
case 'b':
case 'd':
case 'i':
case 'l':
case 'm':
case 'n':
case 'r':
case 's':
parse_subopts(c, optarg);
break;Having done that, I started on the mess that followed the option parsing loop and started separating that out into functions that validate and set a clear set of parameters that the mkfs code then later uses. Hence we end up with code like this in the main function.
/*
* Extract as much of the valid config as we can from the CLI input
* before opening the libxfs devices.
*/
validate_blocksize(&blocksize, &blocklog);
validate_sectorsize(§orsize, §orlog, &ft, dfile, Nflag,
force_overwrite);
validate_log_sectorsize(&lsectorsize, &lsectorlog,
sectorsize, sectorlog);
validate_sb_features();
validate_dirblocksize(&dirblocksize, &dirblocklog, blocklog);
validate_inodesize(&isize, &inodelog, &inopblock, blocklog);This has greatly reduced the spaghetti code and removed all the duplicated checks and strangely dissociated nature of all teh validation and calculations. The stripe unit validation is now all in one function, instead of strewn across 5 disjoint hunks that had some amount of duplicated functionality. Same goes for device checks, calculating AG sizes, etc.
As such, it's far, far easier to see what the overall structure of mkfs is doing now. The factoring of all the input validation and format calculations results in the high level code in then main() function documenting the steps we are taking quite clearly.
It also points out that we have a /lot/ of parameters that we calculate and pass around within these functions, indicating this is the next area of abstraction and simplification. This also points out that we can separate the mkfs functionality clearly into three
parts: input parsing, validation and format calculations, and finally creating the on-disk structures.
The patch as it stands has been smoke tested, but I have not checked that the on-disk layout for every possible option is being correctly handled. It passes xfs/191-mkfs-input-validation with the exception of 2 cases try to set the sector size on an internal log, which this patch disallows.
The next steps are to:
- replace the hacking struct cligeo with a key-value structure for input parameter storage to abstract input parsing from validation and calculation
- extend the key-value structure for holding superblock feature defaults to holding all default parameter values to be used when no values are provided by input parsing
- clean up the validation and calculation code to use the abstracted input and default structures
- have the calculation code output into a single output structure which we can then pass to the on-disk layout code.
- add a key-value config file interface to override the build in default values structure members, hence allowing distros to ship different default configs without needing to change the source code.
- split it all up into reviewable pieces.
--- include/libxfs.h | 2 +- mkfs/xfs_mkfs.c | 2710 ++++++++++++++++++++++++++++++------------------------ 2 files changed, 1509 insertions(+), 1203 deletions(-)
include/libxfs.h +1 -1
@@ -95,7 +95,7 @@ extern uint32_t crc32c_le(uint32_t crc, unsigned char const *p, size_t len);/* * Argument structure for libxfs_init(). */ -typedef struct { +typedef struct libxfs_xinit { /* input parameters */ char *volname; /* pathname of volume */ char *dname; /* pathname of data "subvolume" */
mkfs/xfs_mkfs.c +1508 -1202
2,992 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.13.3
Part of the diff is not shown here. View the raw message.