From: plus Date: Thu, 13 Oct 2022 08:37:20 +0000 (+0800) Subject: mkfs.bfs: Support BSD lock X-Git-Tag: v2.39-rc1~485 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cc4fefc635613252ffd16baf469e8a32129706bf;p=thirdparty%2Futil-linux.git mkfs.bfs: Support BSD lock Addresses: #1842 #921 Signed-off-by: Karel Zak --- diff --git a/disk-utils/mkfs.bfs.8.adoc b/disk-utils/mkfs.bfs.8.adoc index 6b04b105f0..198d61f5ef 100644 --- a/disk-utils/mkfs.bfs.8.adoc +++ b/disk-utils/mkfs.bfs.8.adoc @@ -35,6 +35,9 @@ Specify the volume _label_. I have no idea if/where this is used. *-F*, *--fname* _name_:: Specify the filesystem _name_. I have no idea if/where this is used. +*--lock*[=_mode_]:: +Use exclusive BSD lock for device or file it operates. The optional argument _mode_ can be *yes*, *no* (or 1 and 0) or *nonblock*. If the _mode_ argument is omitted, it defaults to *yes*. This option overwrites environment variable *$LOCK_BLOCK_DEVICE*. The default is not to use any lock at all, but it's recommended to avoid collisions with *systemd-udevd*(8) or other tools. + *-v*, *--verbose*:: Explain what is being done. diff --git a/disk-utils/mkfs.bfs.c b/disk-utils/mkfs.bfs.c index 54d261b293..541aa0315c 100644 --- a/disk-utils/mkfs.bfs.c +++ b/disk-utils/mkfs.bfs.c @@ -22,6 +22,7 @@ #include "strutils.h" #include "xalloc.h" #include "bitops.h" +#include "exitcodes.h" #define BFS_ROOT_INO 2 #define BFS_NAMELEN 14 @@ -84,6 +85,7 @@ static void __attribute__((__noreturn__)) usage(void) " -v, --verbose explain what is being done\n" " -c this option is silently ignored\n" " -l this option is silently ignored\n" + " --lock=[=] use exclusive device lock (yes, no or nonblock)\n" )); printf(USAGE_HELP_OPTIONS(21)); @@ -94,6 +96,7 @@ static void __attribute__((__noreturn__)) usage(void) int main(int argc, char **argv) { char *device, *volume, *fsname; + char *lockmode = 0; long inodes; unsigned long long total_blocks, ino_bytes, ino_blocks, data_blocks; unsigned long long user_specified_total_blocks = 0; @@ -107,7 +110,10 @@ int main(int argc, char **argv) time_t now; int c, i, len; - enum { VERSION_OPTION = CHAR_MAX + 1 }; + enum { + VERSION_OPTION = CHAR_MAX + 1, + OPT_LOCK + }; static const struct option longopts[] = { {"inodes", required_argument, NULL, 'N'}, {"vname", required_argument, NULL, 'V'}, @@ -115,6 +121,7 @@ int main(int argc, char **argv) {"verbose", no_argument, NULL, 'v'}, {"version", no_argument, NULL, VERSION_OPTION}, {"help", no_argument, NULL, 'h'}, + {"lock", optional_argument, NULL, OPT_LOCK}, {NULL, 0, NULL, 0} }; @@ -162,6 +169,15 @@ int main(int argc, char **argv) /* when called via mkfs we may get options c,l,v */ break; + case OPT_LOCK: + lockmode = "1"; + if (optarg) { + if (*optarg == '=') + optarg++; + lockmode = optarg; + } + break; + case VERSION_OPTION: print_version(EXIT_SUCCESS); case 'h': @@ -185,6 +201,9 @@ int main(int argc, char **argv) if (fd < 0) err(EXIT_FAILURE, _("cannot open %s"), device); + if (blkdev_lock(fd, device, lockmode) != 0) + exit(MKFS_EX_ERROR); + if (optind == argc - 1) user_specified_total_blocks = strtou64_or_err(argv[optind], _("invalid block-count"));