multipath: fixes for sysfs accessors
v2 cover multipath: fixes for sysfs accessors
From: Martin Wilck <[email protected]>
This set fixes some strangeness in our sysfs accessors which I found while looking at
https://github.com/opensvc/multipath-tools/issues/35#issuecomment-1175901745.
(The patches don't fix this issue, which seems to be related to Debian's initramfs setup).
Most importantly, sysfs_attr_get_value() and sysfs_attr_set_value() would return 0 if the number of bytes read/written was different from the expected value, which is non-standard and unexpected. This series changes the return value semantics of these functions:
- in sysfs_attr_get_value(), if a read buffer is too small to hold the string read plus a terminating 0 byte, the return value equals the buffer size.
- in sysfs_bin_attr_get_value(), no 0 bytes are appended. It's not an error if the read buffer is completely filled, and no warning is printed in this case.
- sysfs_attr_set_value() always returns the number of bytes written unless an error occured in open() or write().
Tests for the new semantics are added. Moreover, the sysfs.c code is slightly refactored to avoid code duplication.
Changes v1 -> v2:
08/14: fixes suggested by Benjamin Marzinski
I am not resending the entire series; see
https://github.com/openSUSE/multipath-tools/commits/tip
Martin Wilck (14):
libmultipath: alua: remove get_sysfs_pg83()
libmultipath: remove sysfs_get_binary()
libmultipath: sysfs_bin_attr_get_value(): no error if buffer is filled
libmultipath: common code path for sysfs_attr_get_value()
libmultipath: sanitize error checking in sysfs accessors
libmultipath: get rid of PATH_SIZE
libmultipath: sysfs_attr_get_value(): don't return 0 if buffer too
small
libmultipath: sysfs_attr_set_value(): don't return 0 on partial write
libmultipath: sysfs: cleanup file descriptors on pthread_cancel() libmultipath, multipathd: log failure setting sysfs attributes multipath tests: expect_condlog: skip depending on verbosity multipath tests: __wrap_dlog: print log message
multipath tests: add sysfs test
libmultipath.version: bump version for sysfs accessors
libmultipath/configure.c | 30 +- libmultipath/discovery.c | 124 +++---- libmultipath/libmultipath.version | 8 +- libmultipath/prioritizers/alua_rtpg.c | 57 +-- libmultipath/propsel.c | 6 +- libmultipath/structs.h | 3 - libmultipath/sysfs.c | 191 ++++------ libmultipath/sysfs.h | 23 ++ libmultipath/util.c | 8 +- multipathd/cli_handlers.c | 2 +- multipathd/fpin_handlers.c | 11 +- multipathd/main.c | 40 ++- tests/Makefile | 5 +- tests/sysfs.c | 494 ++++++++++++++++++++++++++ tests/test-lib.c | 1 - tests/test-log.c | 5 + 16 files changed, 753 insertions(+), 255 deletions(-) create mode 100644 tests/sysfs.c
Signature · 6 lines
2.36.1 -- dm-devel mailing list [email protected] https://listman.redhat.com/mailman/listinfo/dm-devel
PATCH v2 8/14 libmultipath: sysfs_attr_set_value(): don't return 0 on partial write
From: Martin Wilck <[email protected]>
sysfs_attr_set_value() returned 0 if not all requested bytes were written. Change this to return the number of bytes written. Error checking is now somewhat more involved; provide a helper macro for it.
--- libmultipath/configure.c | 10 ++++-- libmultipath/discovery.c | 74 +++++++++++++++++++++++++--------------- libmultipath/sysfs.c | 6 ++-- libmultipath/sysfs.h | 10 ++++++ 4 files changed, 66 insertions(+), 34 deletions(-)
libmultipath/configure.c +7 -3
@@ -568,6 +568,7 @@ sysfs_set_max_sectors_kb(struct multipath *mpp, int is_reload)struct pathgroup * pgp;struct path *pp;char buff[11 ]; +ssize_t len;int i, j, ret, err =0 ;struct udev_device *udd;int max_sectors_kb;
@@ -600,14 +601,17 @@ sysfs_set_max_sectors_kb(struct multipath *mpp, int is_reload) } }snprintf (buff,11 ,"%d" , max_sectors_kb); + len =strlen (buff); vector_foreach_slot (mpp->pg, pgp, i) { vector_foreach_slot(pgp->paths, pp, j) { ret =sysfs_attr_set_value (pp->udev,"queue/max_sectors_kb" , - buff,strlen (buff)); -if (ret <0 ) { -condlog (1 ,"failed setting max_sectors_kb on %s : %s" , pp->dev,strerror (-ret)); + buff, len); +if (ret != len) { +log_sysfs_attr_set_value (1 , ret, +"failed setting max_sectors_kb on %s" , + pp->dev); err =1 ; } }
libmultipath/discovery.c +47 -27
135 lines not on this page · raw message
libmultipath/sysfs.c +2 -4
@@ -134,7 +134,7 @@ ssize_t sysfs_attr_set_value(struct udev_device *dev, const char *attr_name,/* write attribute value */ fd =open (devpath,O_WRONLY );if (fd <0 ) { -condlog (2 ,"%s: attribute '%s' can not be opened: %s" , +condlog (3 ,"%s: attribute '%s' can not be opened: %s" , __func__, devpath,strerror (errno));return -errno; }
@@ -144,11 +144,9 @@ ssize_t sysfs_attr_set_value(struct udev_device *dev, const char *attr_name, size = -errno;condlog (3 ,"%s: write to %s failed: %s" , __func__, devpath,strerror (errno)); - }else if (size < (ssize_t )value_len) { + }else if (size < (ssize_t )value_len)condlog (3 ,"%s: underflow writing %zu bytes to %s. Wrote %zd bytes" , __func__, value_len, devpath, size); - size =0 ; - }close (fd);return size;
libmultipath/sysfs.h +10 -0
@@ -5,6 +5,7 @@#ifndef _LIBMULTIPATH_SYSFS_H#define _LIBMULTIPATH_SYSFS_H#include <stdbool.h> +#include "strbuf.h" ssize_t sysfs_attr_set_value (struct udev_device *dev,const char *attr_name,const char * value,size_t value_len);
@@ -25,6 +26,15 @@ ssize_t sysfs_bin_attr_get_value(struct udev_device *dev, const char *attr_name,sysfs_attr_value_ok (__rc, __l); \ }) +#define log_sysfs_attr_set_value(prio, rc, fmt, __args...) \ +do { \ + STRBUF_ON_STACK(__buf); \ + if (print_strbuf(&__buf, fmt, ##__args) >= 0 && \ + print_strbuf(&__buf, ": %s", rc < 0 ? strerror(-rc) : \ + "write underflow") >= 0) \ + condlog(prio, "%s", get_strbuf_str(&__buf)); \ +} while(0) +int sysfs_get_size (struct path *pp,unsigned long long * size);int sysfs_check_holders (char * check_devt,char * new_devt);bool sysfs_is_multipathed (struct path *pp,bool set_wwid);
Signature · 6 lines
2.36.1 -- dm-devel mailing list [email protected] https://listman.redhat.com/mailman/listinfo/dm-devel
Re: [dm-devel] [PATCH v2 08/14] libmultipath: sysfs_attr_set_value(): don't return 0 on partial write
Quoted 6 lines · On Wed, Jul 13, 2022 at 10:20:43AM +0200, [email protected] wrote:
From: Martin Wilck <[email protected]>
sysfs_attr_set_value()returned 0 if not all requested bytes were written. Change this to return the number of bytes written. Error checking is now somewhat more involved; provide a helper macro for it.
Quoted 241 lines
Signed-off-by Martin Wilck <[email protected]>--- libmultipath/configure.c | 10 ++++-- libmultipath/discovery.c | 74 +++++++++++++++++++++++++--------------- libmultipath/sysfs.c | 6 ++-- libmultipath/sysfs.h | 10 ++++++ 4 files changed, 66 insertions(+), 34 deletions(-)libmultipath/configure.c +7
-3@@ -568,6 +568,7 @@ sysfs_set_max_sectors_kb(struct multipath *mpp, int is_reload)struct pathgroup * pgp;struct path *pp;char buff[11 ]; +ssize_t len;int i, j, ret, err =0 ;struct udev_device *udd;int max_sectors_kb;@@ -600,14 +601,17 @@ sysfs_set_max_sectors_kb(struct multipath *mpp, int is_reload) } }snprintf (buff,11 ,"%d" , max_sectors_kb); + len =strlen (buff); vector_foreach_slot (mpp->pg, pgp, i) { vector_foreach_slot(pgp->paths, pp, j) { ret =sysfs_attr_set_value (pp->udev,"queue/max_sectors_kb" , - buff,strlen (buff)); -if (ret <0 ) { -condlog (1 ,"failed setting max_sectors_kb on %s : %s" , pp->dev,strerror (-ret)); + buff, len); +if (ret != len) { +log_sysfs_attr_set_value (1 , ret, +"failed setting max_sectors_kb on %s" , + pp->dev); err =1 ; } }libmultipath/discovery.c +47
-27@@ -598,13 +598,15 @@ sysfs_set_eh_deadline(struct path *pp) len =sprintf (value,"%d" , pp->eh_deadline); ret =sysfs_attr_set_value (hostdev,"eh_deadline" , - value, len +1 ); + value, len);/* * not all scsi drivers support setting eh_deadline, so failing * is totally reasonable */ -if (ret <=0 ) -condlog (3 ,"%s: failed to set eh_deadline to %s, error %d" ,udev_device_get_sysname (hostdev), value, -ret); +if (ret != len) +log_sysfs_attr_set_value (3 , ret, +"%s: failed to set eh_deadline to %s" , +udev_device_get_sysname (hostdev), value);udev_device_unref (hostdev);return (ret <=0 );@@ -667,19 +669,22 @@ sysfs_set_rport_tmo(struct multipath *mpp, struct path *pp) pp->fast_io_fail != MP_FAST_IO_FAIL_OFF) {/* Check if we need to temporarily increase dev_loss_tmo */ if ((unsigned int )pp->fast_io_fail >= tmo) { +ssize_t len; +/* Increase dev_loss_tmo temporarily */ snprintf (value,sizeof (value),"%u" , (unsigned int )pp->fast_io_fail +1 ); + len =strlen (value); ret =sysfs_attr_set_value (rport_dev,"dev_loss_tmo" , - value,strlen (value)); -if (ret <=0 ) { + value, len); +if (ret != len) {if (ret == -EBUSY )condlog (3 ,"%s: rport blocked" , rport_id);else -condlog (0 ,"%s: failed to set " -"dev_loss_tmo to %s, error %d" , - rport_id, value, -ret); +log_sysfs_attr_set_value (0 , ret, +"%s: failed to set dev_loss_tmo to %s" , + rport_id, value); goto out; } }@@ -691,32 +696,39 @@ sysfs_set_rport_tmo(struct multipath *mpp, struct path *pp) pp->dev_loss =DEFAULT_DEV_LOSS_TMO ; }if (pp->fast_io_fail !=MP_FAST_IO_FAIL_UNSET ) { +ssize_t len; +if (pp->fast_io_fail ==MP_FAST_IO_FAIL_OFF )sprintf (value,"off" );else if (pp->fast_io_fail ==MP_FAST_IO_FAIL_ZERO )sprintf (value,"0" );else snprintf (value,16 ,"%u" , pp->fast_io_fail); + len =strlen (value); ret =sysfs_attr_set_value (rport_dev,"fast_io_fail_tmo" , - value,strlen (value)); -if (ret <=0 ) { + value, len); +if (ret != len) {if (ret == -EBUSY )condlog (3 ,"%s: rport blocked" , rport_id);else -condlog (0 ,"%s: failed to set fast_io_fail_tmo to %s, error %d" , - rport_id, value, -ret); +log_sysfs_attr_set_value (0 , ret, +"%s: failed to set fast_io_fail_tmo to %s" , + rport_id, value); } }if (pp->dev_loss !=DEV_LOSS_TMO_UNSET ) { +ssize_t len; +snprintf (value,16 ,"%u" , pp->dev_loss); - ret =sysfs_attr_set_value (rport_dev,"dev_loss_tmo" , - value,strlen (value)); -if (ret <=0 ) { + len =strlen (value); + ret =sysfs_attr_set_value (rport_dev,"dev_loss_tmo" , value, len); +if (ret != len) {if (ret == -EBUSY )condlog (3 ,"%s: rport blocked" , rport_id);else -condlog (0 ,"%s: failed to set dev_loss_tmo to %s, error %d" , - rport_id, value, -ret); +log_sysfs_attr_set_value (0 , ret, +"%s: failed to set dev_loss_tmo to %s" , + rport_id, value); } } out:@@ -754,12 +766,16 @@ sysfs_set_session_tmo(struct path *pp)condlog (3 ,"%s: can't set fast_io_fail_tmo to '0'" "on iSCSI" , pp->dev); }else { +ssize_t len, ret; +snprintf (value,11 ,"%u" , pp->fast_io_fail); -if (sysfs_attr_set_value (session_dev,"recovery_tmo" , - value,strlen (value)) <=0 ) { -condlog (3 ,"%s: Failed to set recovery_tmo, " -" error %d" , pp->dev, errno); - } + len =strlen (value); + ret =sysfs_attr_set_value (session_dev,"recovery_tmo" , + value, len); +if (ret != len) +log_sysfs_attr_set_value (3 , ret, +"%s: Failed to set recovery_tmo to %s" , + pp->dev, value); } }udev_device_unref (session_dev);@@ -802,12 +818,16 @@ sysfs_set_nexus_loss_tmo(struct path *pp) pp->sg_id.channel, pp->sg_id.scsi_id, end_dev_id);if (pp->dev_loss !=DEV_LOSS_TMO_UNSET ) { +ssize_t len, ret; +snprintf (value,11 ,"%u" , pp->dev_loss); -if (sysfs_attr_set_value (sas_dev,"I_T_nexus_loss_timeout" , - value,strlen (value)) <=0 ) -condlog (3 ,"%s: failed to update " -"I_T Nexus loss timeout, error %d" , - pp->dev, errno); + len =strlen (value); + ret =sysfs_attr_set_value (sas_dev,"I_T_nexus_loss_timeout" , + value, len); +if (ret != len) +log_sysfs_attr_set_value (3 , ret, +"%s: failed to update I_T Nexus loss timeout" , + pp->dev); }udev_device_unref (sas_dev);return ;libmultipath/sysfs.c +2
-4@@ -134,7 +134,7 @@ ssize_t sysfs_attr_set_value(struct udev_device *dev, const char *attr_name,/* write attribute value */ fd =open (devpath,O_WRONLY );if (fd <0 ) { -condlog (2 ,"%s: attribute '%s' can not be opened: %s" , +condlog (3 ,"%s: attribute '%s' can not be opened: %s" , __func__, devpath,strerror (errno));return -errno; }@@ -144,11 +144,9 @@ ssize_t sysfs_attr_set_value(struct udev_device *dev, const char *attr_name, size = -errno;condlog (3 ,"%s: write to %s failed: %s" , __func__, devpath,strerror (errno)); - }else if (size < (ssize_t )value_len) { + }else if (size < (ssize_t )value_len)condlog (3 ,"%s: underflow writing %zu bytes to %s. Wrote %zd bytes" , __func__, value_len, devpath, size); - size =0 ; - }close (fd);return size;libmultipath/sysfs.h +10
-0@@ -5,6 +5,7 @@#ifndef _LIBMULTIPATH_SYSFS_H#define _LIBMULTIPATH_SYSFS_H#include <stdbool.h> +#include "strbuf.h" ssize_t sysfs_attr_set_value (struct udev_device *dev,const char *attr_name,const char * value,size_t value_len);@@ -25,6 +26,15 @@ ssize_t sysfs_bin_attr_get_value(struct udev_device *dev, const char *attr_name,sysfs_attr_value_ok (__rc, __l); \ }) +#define log_sysfs_attr_set_value(prio, rc, fmt, __args...) \ +do { \ + STRBUF_ON_STACK(__buf); \ + if (print_strbuf(&__buf, fmt, ##__args) >= 0 && \ + print_strbuf(&__buf, ": %s", rc < 0 ? strerror(-rc) : \ + "write underflow") >= 0) \ + condlog(prio, "%s", get_strbuf_str(&__buf)); \ +} while(0) +int sysfs_get_size (struct path *pp,unsigned long long * size);int sysfs_check_holders (char * check_devt,char * new_devt);bool sysfs_is_multipathed (struct path *pp,bool set_wwid);Signature · 1 line
2.36.1
--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel