From 98055888e93ae85045affd45a022f35e5041ccd1 Mon Sep 17 00:00:00 2001 From: "Martin K. Petersen" Date: Wed, 18 May 2011 00:43:14 -0400 Subject: [PATCH] lsblk: improve discard support Use atol() instead of atoi() when extracting discard_max_bytes. Only print discard_alignment and discard_zeroes_data if the device supports discard. This prevents printing of undefined values with older kernels. Signed-off-by: Martin K. Petersen --- misc-utils/lsblk.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/misc-utils/lsblk.c b/misc-utils/lsblk.c index 5d34aebf42..02061629e7 100644 --- a/misc-utils/lsblk.c +++ b/misc-utils/lsblk.c @@ -162,6 +162,7 @@ struct blkdev_cxt { * /sys/block/.../holders + number of partition */ int nslaves; /* # of devices this device maps to */ int maj, min; /* devno */ + int discard; /* supports discard */ uint64_t size; /* device size */ }; @@ -555,8 +556,10 @@ static void set_tt_data(struct blkdev_cxt *cxt, int col, int id, struct tt_line break; case COL_DALIGN: p = sysfs_strdup(&cxt->sysfs, "discard_alignment"); - if (p) + if (cxt->discard && p) tt_line_set_data(ln, col, p); + else + tt_line_set_data(ln, col, "0"); break; case COL_DGRAN: p = sysfs_strdup(&cxt->sysfs, "queue/discard_granularity"); @@ -567,16 +570,17 @@ static void set_tt_data(struct blkdev_cxt *cxt, int col, int id, struct tt_line break; case COL_DMAX: p = sysfs_strdup(&cxt->sysfs, "queue/discard_max_bytes"); - if (!lsblk->bytes) - p = size_to_human_string(atoi(p)); + p = size_to_human_string(atol(p)); if (p) tt_line_set_data(ln, col, p); break; case COL_DZERO: p = sysfs_strdup(&cxt->sysfs, "queue/discard_zeroes_data"); - if (p) + if (cxt->discard && p) tt_line_set_data(ln, col, p); + else + tt_line_set_data(ln, col, "0"); break; }; } @@ -616,6 +620,7 @@ static int set_cxt(struct blkdev_cxt *cxt, cxt->min = minor(devno); cxt->size = sysfs_read_u64(&cxt->sysfs, "size") << 9; + cxt->discard = sysfs_read_int(&cxt->sysfs, "queue/discard_granularity"); /* Ignore devices of zero size */ if (!lsblk->all_devices && cxt->size == 0) -- 2.47.3