From 498057d00ead9c14dceb866a528e81edc7d6f781 Mon Sep 17 00:00:00 2001 From: Joe Thornber Date: Thu, 21 Jun 2018 10:06:01 +0100 Subject: [PATCH] ft-lib: add discard() --- ft-lib/dm-ioctl.c | 22 ++++++++++++++++++++-- functional-tests/device-mapper/ioctl.scm | 11 +++++++++-- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/ft-lib/dm-ioctl.c b/ft-lib/dm-ioctl.c index 89d79be..b1792c6 100644 --- a/ft-lib/dm-ioctl.c +++ b/ft-lib/dm-ioctl.c @@ -487,8 +487,6 @@ int dm_load(struct dm_interface *dmi, const char *name, return r; } -//---------------------------------------------------------------- -// returns false if control buffer too small. static bool get_status(struct dm_interface *dmi, struct dm_ioctl *ctl, const char *name, unsigned flags, int *result) @@ -611,4 +609,24 @@ int get_dev_size(const char *path, uint64_t *sectors) return r; } +int discard(const char *path, uint64_t sector_b, uint64_t sector_e) +{ + int r, fd; + uint64_t payload[2]; + + fd = open(path, O_RDWR); + if (fd < 0) { + fprintf(stderr, "couldn't open %s", path); + return -EINVAL; + } + + payload[0] = sector_b; + payload[1] = sector_e; + + r = ioctl(fd, BLKDISCARD, payload); + close(fd); + + return r; +} + //---------------------------------------------------------------- diff --git a/functional-tests/device-mapper/ioctl.scm b/functional-tests/device-mapper/ioctl.scm index 529930a..5edb889 100644 --- a/functional-tests/device-mapper/ioctl.scm +++ b/functional-tests/device-mapper/ioctl.scm @@ -44,7 +44,8 @@ message - get-dev-size) + get-dev-size + discard) (import (chezscheme) (disk-units) @@ -393,4 +394,10 @@ (if (zero? r) (sectors result) (fail (fmt #f "get-dev-size failed: " r)))))) - ) + + (define (discard dev sb se) + (define c-discard + (foreign-procedure "discard" (string unsigned-64 unsigned-64) int)) + (unless (zero? (c-discard dev sb se)) + (fail (fmt #f "discard ioctl failed")))) +)