um: Fix WRITE_ZEROES in the UBD Driver
authorFrédéric Danis <frederic.danis@collabora.com>
Tue, 25 Jan 2022 17:14:05 +0000 (18:14 +0100)
committerRichard Weinberger <richard@nod.at>
Fri, 11 Mar 2022 09:46:34 +0000 (10:46 +0100)
Call to fallocate with FALLOC_FL_PUNCH_HOLE on a device backed by a sparse
file can end up by missing data, zeroes data range, if the underlying file
is used with a tool like bmaptool which will referenced only used spaces.

Signed-off-by: Frédéric Danis <frederic.danis@collabora.com>
Acked-by: Anton Ivanov <anton.ivanov@cambridgegreys.com>
Signed-off-by: Richard Weinberger <richard@nod.at>
arch/um/drivers/ubd_kern.c
arch/um/include/shared/os.h
arch/um/os-Linux/file.c

index 69d2d0049a613de2fbb3c8527857ad7906fc19ef..b03269faef714764748b16f4645e9496cb555633 100644 (file)
@@ -1526,13 +1526,19 @@ static void do_io(struct io_thread_req *req, struct io_desc *desc)
                        }
                        break;
                case REQ_OP_DISCARD:
-               case REQ_OP_WRITE_ZEROES:
                        n = os_falloc_punch(req->fds[bit], off, len);
                        if (n) {
                                req->error = map_error(-n);
                                return;
                        }
                        break;
+               case REQ_OP_WRITE_ZEROES:
+                       n = os_falloc_zeroes(req->fds[bit], off, len);
+                       if (n) {
+                               req->error = map_error(-n);
+                               return;
+                       }
+                       break;
                default:
                        WARN_ON_ONCE(1);
                        req->error = BLK_STS_NOTSUPP;
index 00214059d9ecae5d9b28af4dbdb0c6a3e15eba46..fafde1d5416edf8f31c84b1b39842b280552bef1 100644 (file)
@@ -168,6 +168,7 @@ extern unsigned os_major(unsigned long long dev);
 extern unsigned os_minor(unsigned long long dev);
 extern unsigned long long os_makedev(unsigned major, unsigned minor);
 extern int os_falloc_punch(int fd, unsigned long long offset, int count);
+extern int os_falloc_zeroes(int fd, unsigned long long offset, int count);
 extern int os_eventfd(unsigned int initval, int flags);
 extern int os_sendmsg_fds(int fd, const void *buf, unsigned int len,
                          const int *fds, unsigned int fds_num);
index e4421dbc4c36c70adbc66eb997f6a42c05548cf4..fc4450db59bd444b6ac92fd23db270d1461a0e39 100644 (file)
@@ -625,6 +625,15 @@ int os_falloc_punch(int fd, unsigned long long offset, int len)
        return n;
 }
 
+int os_falloc_zeroes(int fd, unsigned long long offset, int len)
+{
+       int n = fallocate(fd, FALLOC_FL_ZERO_RANGE|FALLOC_FL_KEEP_SIZE, offset, len);
+
+       if (n < 0)
+               return -errno;
+       return n;
+}
+
 int os_eventfd(unsigned int initval, int flags)
 {
        int fd = eventfd(initval, flags);