It is probably less complex to have separate "--mountdata-dev=MDTDEV" and "--mountdata-file=FILE" options, so that the code doesn't have to be fancy about detecting whether this is an ldiskfs filesystem image or mountdata file (though it could do this by checking if it is a block device (ldiskfs), or reading the file and checking for LDD_MAGIC or EXT4_MAGIC).
The "--mountdata-dev=MDTDEV" option would need to pass the MDTDEV device name to lustre/utils/libmount_utils_ldiskfs.c::ldiskfs_read_ldd() and then close and zero the backfs handle, so that this is not re-used in ldiskfs_write_ldd() when it is called. That would be bad, since it would potentially modify the source MDT CONFIGS/mountdata file instead of the broken one, causing even more problems...
In the "--mountdata-file=FILE" case, the ldiskfs_read_ldd() code is useless, and it could just use:
fd = open(file);
if (fd < 0)
...;
rc = read(fd, mo_ldd, sizeof(mo_ldd));
if (rc < sizeof(*mo_ldd))
...;
close(fd)
(with appropriate error handling).
I do notice an oddity in ldiskfs_read_ldd() that it is running the "e2label" command even if ext2fs_file_read() is successful? It isn't really clear that this needed, or only if the mo_ldd->ldd_svname field is empty after the read was successful? Similarly, I notice that ldiskfs_write_ldd() is using libext2fs to directly open the filesystem to set the MMP feature flag, but is then closing the filesystem and mounting it onto a temporary mountpoint and calling mkdir(MOUNT_CONFIGS_DIR) and open(MOUNT_DATA_FILE) and write(mo_ldd) to write the CONFIGS/mountdata file, when it could be doing this directly via libext2fs. We've definitely had issues with this extra mount/unmount cycle in the past (e.g. LU-13241, LU-7002), and it would be good to get rid of that. I filed LU-18818 to track these issues, since they are somewhat independent of this one and could be completed independently.
"Rahul Bansal <rahulmay94@gmail.com>" uploaded a new patch: https://review.whamcloud.com/c/fs/lustre-release/+/58513
Subject: LU-18436 tunefs: process to rebuild CONFIGS/mountdata file
Project: fs/lustre-release
Branch: master
Current Patch Set: 1
Commit: df319957388005a5f7e1aebb61284c4c6a963a73