0003-blkdiscard-use-O_EXCL-add-force.patch 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. From 34fed3ff1740aded9c2aae6b5d67a4eb696f738e Mon Sep 17 00:00:00 2001
  2. From: Karel Zak <kzak@redhat.com>
  3. Date: Thu, 9 Jan 2020 11:03:51 +0100
  4. Subject: [PATCH] blkdiscard: use O_EXCL, add --force
  5. Let's make it more robust and safe. O_EXCL is an elegant way how to avoid
  6. unwanted discard on mounted device.
  7. Addresses: https://github.com/karelzak/util-linux/issues/915
  8. Signed-off-by: Karel Zak <kzak@redhat.com>
  9. ---
  10. sys-utils/blkdiscard.8 | 5 +++++
  11. sys-utils/blkdiscard.c | 11 ++++++++---
  12. 2 files changed, 13 insertions(+), 3 deletions(-)
  13. diff --git a/sys-utils/blkdiscard.8 b/sys-utils/blkdiscard.8
  14. index 1f3a32be9..98c6f36a9 100644
  15. --- a/sys-utils/blkdiscard.8
  16. +++ b/sys-utils/blkdiscard.8
  17. @@ -36,6 +36,11 @@ MiB (=1024*1024), and so on for GiB, TiB, PiB, EiB, ZiB and YiB (the "iB" is
  18. optional, e.g., "K" has the same meaning as "KiB") or the suffixes
  19. KB (=1000), MB (=1000*1000), and so on for GB, TB, PB, EB, ZB and YB.
  20. .TP
  21. +.BR \-f , " \-\-force"
  22. +Disable all checking. Since v2.36 the block device is open in exclusive mode (O_EXCL)
  23. +by default to avoid collision with mounted filesystem or another kernel subsystem.
  24. +The force option disables the exclusive access mode.
  25. +.TP
  26. .BR \-o , " \-\-offset \fIoffset"
  27. Byte offset into the device from which to start discarding. The provided value
  28. will be aligned to the device sector size. The default value is zero.
  29. diff --git a/sys-utils/blkdiscard.c b/sys-utils/blkdiscard.c
  30. index f9ba5e468..589974f9c 100644
  31. --- a/sys-utils/blkdiscard.c
  32. +++ b/sys-utils/blkdiscard.c
  33. @@ -88,6 +88,7 @@ static void __attribute__((__noreturn__)) usage(void)
  34. fputs(_("Discard the content of sectors on a device.\n"), out);
  35. fputs(USAGE_OPTIONS, out);
  36. + fputs(_(" -f, --force disable all checking\n"), out);
  37. fputs(_(" -o, --offset <num> offset in bytes to discard from\n"), out);
  38. fputs(_(" -l, --length <num> length of bytes to discard from the offset\n"), out);
  39. fputs(_(" -p, --step <num> size of the discard iterations within the offset\n"), out);
  40. @@ -106,7 +107,7 @@ static void __attribute__((__noreturn__)) usage(void)
  41. int main(int argc, char **argv)
  42. {
  43. char *path;
  44. - int c, fd, verbose = 0, secsize;
  45. + int c, fd, verbose = 0, secsize, force = 0;
  46. uint64_t end, blksize, step, range[2], stats[2];
  47. struct stat sb;
  48. struct timeval now, last;
  49. @@ -116,6 +117,7 @@ int main(int argc, char **argv)
  50. { "help", no_argument, NULL, 'h' },
  51. { "version", no_argument, NULL, 'V' },
  52. { "offset", required_argument, NULL, 'o' },
  53. + { "force", no_argument, NULL, 'f' },
  54. { "length", required_argument, NULL, 'l' },
  55. { "step", required_argument, NULL, 'p' },
  56. { "secure", no_argument, NULL, 's' },
  57. @@ -133,8 +135,11 @@ int main(int argc, char **argv)
  58. range[1] = ULLONG_MAX;
  59. step = 0;
  60. - while ((c = getopt_long(argc, argv, "hVsvo:l:p:z", longopts, NULL)) != -1) {
  61. + while ((c = getopt_long(argc, argv, "hfVsvo:l:p:z", longopts, NULL)) != -1) {
  62. switch(c) {
  63. + case 'f':
  64. + force = 1;
  65. + break;
  66. case 'l':
  67. range[1] = strtosize_or_err(optarg,
  68. _("failed to parse length"));
  69. @@ -176,7 +181,7 @@ int main(int argc, char **argv)
  70. errtryhelp(EXIT_FAILURE);
  71. }
  72. - fd = open(path, O_WRONLY);
  73. + fd = open(path, O_WRONLY | (force ? 0 : O_EXCL));
  74. if (fd < 0)
  75. err(EXIT_FAILURE, _("cannot open %s"), path);
  76. --
  77. 2.18.2