[PATCH v5 5/7] iscsi-target: replace iscsi_initiatorname_tolower() with strtolower()

2016-08-10 Thread Markus Mayer
After introducing generic strtolower(), iscsi_initiatorname_tolower() is
no longer needed.

Signed-off-by: Markus Mayer <mma...@broadcom.com>
Acked-by: Nicholas Bellinger <n...@linux-iscsi.org>
---
 drivers/target/iscsi/iscsi_target_nego.c | 17 +
 1 file changed, 1 insertion(+), 16 deletions(-)

diff --git a/drivers/target/iscsi/iscsi_target_nego.c 
b/drivers/target/iscsi/iscsi_target_nego.c
index 89d34bd..fa20638 100644
--- a/drivers/target/iscsi/iscsi_target_nego.c
+++ b/drivers/target/iscsi/iscsi_target_nego.c
@@ -987,21 +987,6 @@ static int iscsi_target_do_login(struct iscsi_conn *conn, 
struct iscsi_login *lo
return 0;
 }
 
-static void iscsi_initiatorname_tolower(
-   char *param_buf)
-{
-   char *c;
-   u32 iqn_size = strlen(param_buf), i;
-
-   for (i = 0; i < iqn_size; i++) {
-   c = _buf[i];
-   if (!isupper(*c))
-   continue;
-
-   *c = tolower(*c);
-   }
-}
-
 /*
  * Processes the first Login Request..
  */
@@ -1075,7 +1060,7 @@ int iscsi_target_locate_portal(
 * RFC-3720 3.2.6.1. section c) that says that iSCSI IQNs
 * are NOT case sensitive.
 */
-   iscsi_initiatorname_tolower(i_buf);
+   strtolower(i_buf);
 
if (!s_buf) {
if (!login->leading_connection)
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-scsi" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v5 0/7] lib: string: add functions to case-convert strings

2016-08-10 Thread Markus Mayer
This series introduces a family of generic string case conversion
functions. This kind of functionality is needed in several places in
the kernel. Right now, everybody seems to be implementing their own
copy of this functionality.

Based on the discussion of the previous version of this series[1] and
the use cases found in the kernel, it does look like having several
flavours of case conversion functions is beneficial. The use cases fall
into three categories:
- copying a string and converting the case while specifying a
  maximum length to mimic strlcpy()
- copying a string and converting the case without specifying a
  length to mimic strcpy()
- converting the case of a string in-place (i.e. modifying the
  string that was passed in)

Consequently, I am proposing these new functions:
int strlcpytoupper(char *dst, const char *src, size_t len);
int strlcpytolower(char *dst, const char *src, size_t len);
void strcpytoupper(char *dst, const char *src);
void strcpytolower(char *dst, const char *src);
void strtoupper(char *s);
void strtolower(char *s);

Several drivers are being modified to make use of the functions above.
Another driver that also makes use of this functionality will be
submitted upstream shortly, which prompted this whole exercise.

The changes made here have been compile-tested, but not tried out, due
to lack of required hardware.

Changes since v4:
  - rebased on 4.8-rc1; this only affects patch 6/7 (gk104) and only the
patch context, not the patch itself
  - added an ACK to patch 4/7 (speakup)
  - added an ACK to patch 7/7 (power_supply)

Changes since v3:
  - strlcpytoupper() and strlcpytolower() return length of destination
or -E2BIG (see [2])
  - we use ~(size_t)0 instead of -1 to copy strings of arbitrary length
in strcpyto*() and strto*()
  - A few ACKs added

Changes since v2:
  - use strlcpy() semantics not strncpy() semantics, i.e. guarantee
NULL termination
  - as a result strncpyto<upper|lower> are now called
strlcpyto<upper|lower>
  - make functions void
  - use len == -1 (SIZE_MAX) as no-limit indicator rather then len == 0
  - change PATCH 2/7 to match strlcpy() semantics
  - change PATCH 4/7 to match strlcpy() semantics

Changes since v1:
  - expanded strtolower() into a family of functions that cover use
cases when a length argument is or isn't required and that support
copying the string into a new buffer or changing it in-place 
  - changed the function semantics to return a pointer to the
terminating '\0' character of the modified string
  - added strtoupper() functionality mirroring the above
  - dropped the ACPICA patch, since that code is OS independent and
can't rely on a Linux library function (see [3])
  - Added two new patches replacing strtoupper() implementations

[1] https://lkml.org/lkml/2016/6/30/727
[2] https://lkml.org/lkml/2016/7/10/4
[3] https://lkml.org/lkml/2016/7/1/9

Markus Mayer (7):
  lib: string: add functions to case-convert strings
  drm/nouveau/core: make use of new strlcpytolower() function
  ACPI / device_sysfs: make use of new strtolower() function
  staging: speakup: replace spk_strlwr() with strlcpytolower()
  iscsi-target: replace iscsi_initiatorname_tolower() with strtolower()
  drm/nouveau/fifo/gk104: make use of new strcpytoupper() function
  power_supply: make use of new strcpytoupper() function

 drivers/acpi/device_sysfs.c  |  4 +--
 drivers/gpu/drm/nouveau/nvkm/core/firmware.c |  9 +
 drivers/gpu/drm/nouveau/nvkm/engine/fifo/gk104.c |  5 +--
 drivers/power/power_supply_sysfs.c   | 13 +++
 drivers/staging/speakup/kobjects.c   |  3 +-
 drivers/staging/speakup/main.c   |  3 +-
 drivers/staging/speakup/speakup.h|  1 -
 drivers/staging/speakup/varhandlers.c| 12 ---
 drivers/target/iscsi/iscsi_target_nego.c | 17 +
 include/linux/string.h   | 40 +
 lib/string.c | 46 
 11 files changed, 98 insertions(+), 55 deletions(-)

-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-scsi" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v5 1/7] lib: string: add functions to case-convert strings

2016-08-10 Thread Markus Mayer
Add a collection of generic functions to convert strings to lowercase
or uppercase.

Changing the case of a string (with or without copying it first) seems
to be a recurring requirement in the kernel that is currently being
solved by several duplicated implementations doing the same thing. This
change aims at reducing this code duplication.

The new functions are
int strlcpytoupper(char *dst, const char *src, size_t len);
int strlcpytolower(char *dst, const char *src, size_t len);
void strcpytoupper(char *dst, const char *src);
void strcpytolower(char *dst, const char *src);
void strtoupper(char *s);
void strtolower(char *s);

The "str[l]cpyto*" versions of the function take a destination string
and a source string as arguments. The "strlcpyto*" versions additionally
take a length argument like strlcpy() itself. Lastly, the strto*
functions take a single string argument and modify the passed-in string.

strlcpytoupper() and strlcpytolower() return the number of characters
copied or -E2BIG if the destination string was truncated.

Like strlcpy(), and unlike strncpy(), the functions guarantee NULL
termination of the destination string.

Signed-off-by: Markus Mayer <mma...@broadcom.com>
---
 include/linux/string.h | 40 
 lib/string.c   | 46 ++
 2 files changed, 86 insertions(+)

diff --git a/include/linux/string.h b/include/linux/string.h
index 26b6f6a..257d797 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -116,6 +116,8 @@ extern void * memchr(const void *,int,__kernel_size_t);
 #endif
 void *memchr_inv(const void *s, int c, size_t n);
 char *strreplace(char *s, char old, char new);
+extern int strlcpytoupper(char *dst, const char *src, size_t len);
+extern int strlcpytolower(char *dst, const char *src, size_t len);
 
 extern void kfree_const(const void *x);
 
@@ -169,4 +171,42 @@ static inline const char *kbasename(const char *path)
return tail ? tail + 1 : path;
 }
 
+/**
+ * strcpytoupper - Copy string and convert to uppercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to uppercase.
+ */
+static inline void strcpytoupper(char *dst, const char *src)
+{
+   strlcpytoupper(dst, src, ~(size_t)0);
+}
+
+/**
+ * strcpytolower - Copy string and convert to lowercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to lowercase.
+ */
+static inline void strcpytolower(char *dst, const char *src)
+{
+   strlcpytolower(dst, src, ~(size_t)0);
+}
+
+/**
+ * strtoupper - Convert string to uppercase.
+ * @s: The string to operate on.
+ */
+static inline void strtoupper(char *s)
+{
+   strlcpytoupper(s, s, ~(size_t)0);
+}
+
+/**
+ * strtolower - Convert string to lowercase.
+ * @s: The string to operate on.
+ */
+static inline void strtolower(char *s)
+{
+   strlcpytolower(s, s, ~(size_t)0);
+}
+
 #endif /* _LINUX_STRING_H_ */
diff --git a/lib/string.c b/lib/string.c
index ed83562..d36d5fb2 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -952,3 +952,49 @@ char *strreplace(char *s, char old, char new)
return s;
 }
 EXPORT_SYMBOL(strreplace);
+
+/**
+ * strlcpytoupper - Copy a length-limited string and convert to uppercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to uppercase.
+ * @len: Maximum string length. May be SIZE_MAX to set no limit.
+ *
+ * Returns the number of characters copied or -E2BIG if @dst wasn't big enough.
+ */
+int strlcpytoupper(char *dst, const char *src, size_t len)
+{
+   size_t i;
+
+   if (!len)
+   return -E2BIG;
+
+   for (i = 0; i < len && src[i]; ++i)
+   dst[i] = toupper(src[i]);
+   dst[i < len ? i : i - 1] = '\0';
+
+   return (i < len) ? i : -E2BIG;
+}
+EXPORT_SYMBOL(strlcpytoupper);
+
+/**
+ * strlcpytolower - Copy a length-limited string and convert to lowercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to lowercase.
+ * @len: Maximum string length. May be SIZE_MAX to set no limit.
+ *
+ * Returns the number of characters copied or -E2BIG if @dst wasn't big enough.
+ */
+int strlcpytolower(char *dst, const char *src, size_t len)
+{
+   size_t i;
+
+   if (!len)
+   return -E2BIG;
+
+   for (i = 0; i < len && src[i]; ++i)
+   dst[i] = tolower(src[i]);
+   dst[i < len ? i : i - 1] = '\0';
+
+   return (i < len) ? i : -E2BIG;
+}
+EXPORT_SYMBOL(strlcpytolower);
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-scsi" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v4 5/7] iscsi-target: replace iscsi_initiatorname_tolower() with strtolower()

2016-07-22 Thread Markus Mayer
After introducing generic strtolower(), iscsi_initiatorname_tolower() is
no longer needed.

Signed-off-by: Markus Mayer <mma...@broadcom.com>
Acked-by: Nicholas Bellinger <n...@linux-iscsi.org>
---
 drivers/target/iscsi/iscsi_target_nego.c | 17 +
 1 file changed, 1 insertion(+), 16 deletions(-)

diff --git a/drivers/target/iscsi/iscsi_target_nego.c 
b/drivers/target/iscsi/iscsi_target_nego.c
index 89d34bd..fa20638 100644
--- a/drivers/target/iscsi/iscsi_target_nego.c
+++ b/drivers/target/iscsi/iscsi_target_nego.c
@@ -987,21 +987,6 @@ static int iscsi_target_do_login(struct iscsi_conn *conn, 
struct iscsi_login *lo
return 0;
 }
 
-static void iscsi_initiatorname_tolower(
-   char *param_buf)
-{
-   char *c;
-   u32 iqn_size = strlen(param_buf), i;
-
-   for (i = 0; i < iqn_size; i++) {
-   c = _buf[i];
-   if (!isupper(*c))
-   continue;
-
-   *c = tolower(*c);
-   }
-}
-
 /*
  * Processes the first Login Request..
  */
@@ -1075,7 +1060,7 @@ int iscsi_target_locate_portal(
 * RFC-3720 3.2.6.1. section c) that says that iSCSI IQNs
 * are NOT case sensitive.
 */
-   iscsi_initiatorname_tolower(i_buf);
+   strtolower(i_buf);
 
if (!s_buf) {
if (!login->leading_connection)
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-scsi" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v4 1/7] lib: string: add functions to case-convert strings

2016-07-22 Thread Markus Mayer
Add a collection of generic functions to convert strings to lowercase
or uppercase.

Changing the case of a string (with or without copying it first) seems
to be a recurring requirement in the kernel that is currently being
solved by several duplicated implementations doing the same thing. This
change aims at reducing this code duplication.

The new functions are
int strlcpytoupper(char *dst, const char *src, size_t len);
int strlcpytolower(char *dst, const char *src, size_t len);
void strcpytoupper(char *dst, const char *src);
void strcpytolower(char *dst, const char *src);
void strtoupper(char *s);
void strtolower(char *s);

The "str[l]cpyto*" versions of the function take a destination string
and a source string as arguments. The "strlcpyto*" versions additionally
take a length argument like strlcpy() itself. Lastly, the strto*
functions take a single string argument and modify the passed-in string.

strlcpytoupper() and strlcpytolower() return the number of characters
copied or -E2BIG if the destination string was truncated.

Like strlcpy(), and unlike strncpy(), the functions guarantee NULL
termination of the destination string.

Signed-off-by: Markus Mayer <mma...@broadcom.com>
---
 include/linux/string.h | 40 
 lib/string.c   | 46 ++
 2 files changed, 86 insertions(+)

diff --git a/include/linux/string.h b/include/linux/string.h
index 26b6f6a..257d797 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -116,6 +116,8 @@ extern void * memchr(const void *,int,__kernel_size_t);
 #endif
 void *memchr_inv(const void *s, int c, size_t n);
 char *strreplace(char *s, char old, char new);
+extern int strlcpytoupper(char *dst, const char *src, size_t len);
+extern int strlcpytolower(char *dst, const char *src, size_t len);
 
 extern void kfree_const(const void *x);
 
@@ -169,4 +171,42 @@ static inline const char *kbasename(const char *path)
return tail ? tail + 1 : path;
 }
 
+/**
+ * strcpytoupper - Copy string and convert to uppercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to uppercase.
+ */
+static inline void strcpytoupper(char *dst, const char *src)
+{
+   strlcpytoupper(dst, src, ~(size_t)0);
+}
+
+/**
+ * strcpytolower - Copy string and convert to lowercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to lowercase.
+ */
+static inline void strcpytolower(char *dst, const char *src)
+{
+   strlcpytolower(dst, src, ~(size_t)0);
+}
+
+/**
+ * strtoupper - Convert string to uppercase.
+ * @s: The string to operate on.
+ */
+static inline void strtoupper(char *s)
+{
+   strlcpytoupper(s, s, ~(size_t)0);
+}
+
+/**
+ * strtolower - Convert string to lowercase.
+ * @s: The string to operate on.
+ */
+static inline void strtolower(char *s)
+{
+   strlcpytolower(s, s, ~(size_t)0);
+}
+
 #endif /* _LINUX_STRING_H_ */
diff --git a/lib/string.c b/lib/string.c
index ed83562..d36d5fb2 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -952,3 +952,49 @@ char *strreplace(char *s, char old, char new)
return s;
 }
 EXPORT_SYMBOL(strreplace);
+
+/**
+ * strlcpytoupper - Copy a length-limited string and convert to uppercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to uppercase.
+ * @len: Maximum string length. May be SIZE_MAX to set no limit.
+ *
+ * Returns the number of characters copied or -E2BIG if @dst wasn't big enough.
+ */
+int strlcpytoupper(char *dst, const char *src, size_t len)
+{
+   size_t i;
+
+   if (!len)
+   return -E2BIG;
+
+   for (i = 0; i < len && src[i]; ++i)
+   dst[i] = toupper(src[i]);
+   dst[i < len ? i : i - 1] = '\0';
+
+   return (i < len) ? i : -E2BIG;
+}
+EXPORT_SYMBOL(strlcpytoupper);
+
+/**
+ * strlcpytolower - Copy a length-limited string and convert to lowercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to lowercase.
+ * @len: Maximum string length. May be SIZE_MAX to set no limit.
+ *
+ * Returns the number of characters copied or -E2BIG if @dst wasn't big enough.
+ */
+int strlcpytolower(char *dst, const char *src, size_t len)
+{
+   size_t i;
+
+   if (!len)
+   return -E2BIG;
+
+   for (i = 0; i < len && src[i]; ++i)
+   dst[i] = tolower(src[i]);
+   dst[i < len ? i : i - 1] = '\0';
+
+   return (i < len) ? i : -E2BIG;
+}
+EXPORT_SYMBOL(strlcpytolower);
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-scsi" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v4 0/7] lib: string: add functions to case-convert strings

2016-07-22 Thread Markus Mayer
This series introduces a family of generic string case conversion
functions. This kind of functionality is needed in several places in
the kernel. Right now, everybody seems to be implementing their own
copy of this functionality.

Based on the discussion of the previous version of this series[1] and
the use cases found in the kernel, it does look like having several
flavours of case conversion functions is beneficial. The use cases fall
into three categories:
- copying a string and converting the case while specifying a
  maximum length to mimic strlcpy()
- copying a string and converting the case without specifying a
  length to mimic strcpy()
- converting the case of a string in-place (i.e. modifying the
  string that was passed in)

Consequently, I am proposing these new functions:
int strlcpytoupper(char *dst, const char *src, size_t len);
int strlcpytolower(char *dst, const char *src, size_t len);
void strcpytoupper(char *dst, const char *src);
void strcpytolower(char *dst, const char *src);
void strtoupper(char *s);
void strtolower(char *s);

Several drivers are being modified to make use of the functions above.
Another driver that also makes use of this functionality will be
submitted upstream shortly, which prompted this whole exercise.

The changes made here have been compile-tested, but not tried out, due
to lack of required hardware.

Changes since v3:
  - strlcpytoupper() and strlcpytolower() return length of destination
or -E2BIG (see [2])
  - we use ~(size_t)0 instead of -1 to copy strings of arbitrary length
in strcpyto*() and strto*()
  - A few ACKs added

Changes since v2:
  - use strlcpy() semantics not strncpy() semantics, i.e. guarantee
NULL termination
  - as a result strncpyto<upper|lower> are now called
strlcpyto<upper|lower>
  - make functions void
  - use len == -1 (SIZE_MAX) as no-limit indicator rather then len == 0
  - change PATCH 2/7 to match strlcpy() semantics
  - change PATCH 4/7 to match strlcpy() semantics

Changes since v1:
  - expanded strtolower() into a family of functions that cover use
cases when a length argument is or isn't required and that support
copying the string into a new buffer or changing it in-place 
  - changed the function semantics to return a pointer to the
terminating '\0' character of the modified string
  - added strtoupper() functionality mirroring the above
  - dropped the ACPICA patch, since that code is OS independent and
can't rely on a Linux library function (see [3])
  - Added two new patches replacing strtoupper() implementations

[1] https://lkml.org/lkml/2016/6/30/727
[2] https://lkml.org/lkml/2016/7/10/4
[3] https://lkml.org/lkml/2016/7/1/9

Markus Mayer (7):
  lib: string: add functions to case-convert strings
  drm/nouveau/core: make use of new strlcpytolower() function
  ACPI / device_sysfs: make use of new strtolower() function
  staging: speakup: replace spk_strlwr() with strlcpytolower()
  iscsi-target: replace iscsi_initiatorname_tolower() with strtolower()
  drm/nouveau/fifo/gk104: make use of new strcpytoupper() function
  power_supply: make use of new strcpytoupper() function

 drivers/acpi/device_sysfs.c  |  4 +--
 drivers/gpu/drm/nouveau/nvkm/core/firmware.c |  9 +
 drivers/gpu/drm/nouveau/nvkm/engine/fifo/gk104.c |  5 +--
 drivers/power/power_supply_sysfs.c   | 13 +++
 drivers/staging/speakup/kobjects.c   |  3 +-
 drivers/staging/speakup/main.c   |  3 +-
 drivers/staging/speakup/speakup.h|  1 -
 drivers/staging/speakup/varhandlers.c| 12 ---
 drivers/target/iscsi/iscsi_target_nego.c | 17 +
 include/linux/string.h   | 40 +
 lib/string.c | 46 
 11 files changed, 98 insertions(+), 55 deletions(-)

-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-scsi" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH v3 5/7] iscsi-target: replace iscsi_initiatorname_tolower() with strtolower()

2016-07-20 Thread Markus Mayer
On 20 July 2016 at 14:17, Nicholas A. Bellinger <n...@linux-iscsi.org> wrote:
> On Wed, 2016-07-20 at 14:16 -0700, Nicholas A. Bellinger wrote:
>> On Fri, 2016-07-08 at 15:43 -0700, Markus Mayer wrote:
>> > After introducing generic strtolower(), iscsi_initiatorname_tolower() is
>> > no longer needed.
>> >
>> > Signed-off-by: Markus Mayer <mma...@broadcom.com>
>> > ---
>> >  drivers/target/iscsi/iscsi_target_nego.c | 17 +
>> >  1 file changed, 1 insertion(+), 16 deletions(-)
>> >
>> > diff --git a/drivers/target/iscsi/iscsi_target_nego.c 
>> > b/drivers/target/iscsi/iscsi_target_nego.c
>> > index 89d34bd..fa20638 100644
>> > --- a/drivers/target/iscsi/iscsi_target_nego.c
>> > +++ b/drivers/target/iscsi/iscsi_target_nego.c
>> > @@ -987,21 +987,6 @@ static int iscsi_target_do_login(struct iscsi_conn 
>> > *conn, struct iscsi_login *lo
>> > return 0;
>> >  }
>> >
>> > -static void iscsi_initiatorname_tolower(
>> > -   char *param_buf)
>> > -{
>> > -   char *c;
>> > -   u32 iqn_size = strlen(param_buf), i;
>> > -
>> > -   for (i = 0; i < iqn_size; i++) {
>> > -   c = _buf[i];
>> > -   if (!isupper(*c))
>> > -   continue;
>> > -
>> > -   *c = tolower(*c);
>> > -   }
>> > -}
>> > -
>> >  /*
>> >   * Processes the first Login Request..
>> >   */
>> > @@ -1075,7 +1060,7 @@ int iscsi_target_locate_portal(
>> >  * RFC-3720 3.2.6.1. section c) that says that iSCSI IQNs
>> >  * are NOT case sensitive.
>> >  */
>> > -   iscsi_initiatorname_tolower(i_buf);
>> > +   strtolower(i_buf);
>> >
>> > if (!s_buf) {
>> > if (!login->leading_connection)
>>
>> Applied to target-pending/for-next.
>>
>> Thanks Markus.
>>
>
> Er, my bad.  It's not a stand-alone patch..

No, it's not. I was just about to point that out. Patch 1 is taking a
bit of time to get straightened out, so it hasn't been applied yet.

> In that case for the iscsi-target part:
>
> Acked-by: Nicholas Bellinger <n...@linux-iscsi.org>

Thanks. I'll add the ACK to my tree.

-Markus
--
To unsubscribe from this list: send the line "unsubscribe linux-scsi" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH v3 0/7] lib: string: add functions to case-convert strings

2016-07-20 Thread Markus Mayer
On Wed, Jul 13, 2016 at 03:52:38PM -0700, Markus Mayer wrote:
> On Sat, Jul 09, 2016 at 09:11:05PM -0700, Markus Mayer wrote:
>> On 9 July 2016 at 20:13, Chris Metcalf <cmetc...@mellanox.com> wrote:
>>> On 7/8/2016 6:43 PM, Markus Mayer wrote:
>>>>
>>>> This series introduces a family of generic string case conversion
>>>> functions. This kind of functionality is needed in several places in
>>>> the kernel. Right now, everybody seems to be implementing their own
>>>> copy of this functionality.
>>>>
>>>> Based on the discussion of the previous version of this series[1] and
>>>> the use cases found in the kernel, it does look like having several
>>>> flavours of case conversion functions is beneficial. The use cases fall
>>>> into three categories:
>>>>  - copying a string and converting the case while specifying a
>>>>maximum length to mimic strlcpy()
>>>>  - copying a string and converting the case without specifying a
>>>>length to mimic strcpy()
>>>>  - converting the case of a string in-place (i.e. modifying the
>>>>string that was passed in)
>>>>
>>>> Consequently, I am proposing these new functions:
>>>>  void strlcpytoupper(char *dst, const char *src, size_t len);
>>>>  void strlcpytolower(char *dst, const char *src, size_t len);
>>>>  void strcpytoupper(char *dst, const char *src);
>>>>  void strcpytolower(char *dst, const char *src);
>>>>  void strtoupper(char *s);
>>>>  void strtolower(char *s);
>>>
>>>
>>> You may want to read the article here:
>>>
>>> https://lwn.net/Articles/659214/
>> 
>> I'll read that. Thanks.
> 
> It doesn't look like there is going to be the danger of "mass changes". 
> So far, I have two ACKs (one where the semantics doesn't change,
> because it's using strtolower()) and the other in a driver in staging.
> 
> But I understand the concern and will keep an eye out if there are
> other ACKs.
>  
>>> and follow up some of the discussion threads on LKML about the best
>>> semantics to advertise for the strlcpy/strscpy variants.  It might
>>> be helpful to return some kind of overflow/truncation error from
>>> your copy functions so people can error-check the result.
>> 
>> I am inclined to agree. However, everybody has been telling me that
>> these functions should be void. Originally they weren't.
> 
> What about something like this?  It might also work to keep the four
> static inline functions as "void" (since they won't ever return E2BIG
> anyway) and just have strlcpyto* return an integer (since that's where
> the buffer could be too small).
> 
> Rasmus, what's your take? 

Ping. Any thoughts on this proposal? Does it make sense for me to sent out a
new revision of the patch set incorporating these changes -- at least for
the strlcpyto* functions?

Thanks,
-Markus
 
> diff --git a/include/linux/string.h b/include/linux/string.h
> index ae82d13..6cc85dc 100644
> --- a/include/linux/string.h
> +++ b/include/linux/string.h
> @@ -116,8 +116,8 @@ extern void * memchr(const void *,int,__kernel_size_t);
>  #endif
>  void *memchr_inv(const void *s, int c, size_t n);
>  char *strreplace(char *s, char old, char new);
> -extern void strlcpytoupper(char *dst, const char *src, size_t len);
> -extern void strlcpytolower(char *dst, const char *src, size_t len);
> +extern int strlcpytoupper(char *dst, const char *src, size_t len);
> +extern int strlcpytolower(char *dst, const char *src, size_t len);
>  
>  extern void kfree_const(const void *x);
>  
> @@ -175,38 +175,46 @@ static inline const char *kbasename(const char *path)
>   * strcpytoupper - Copy string and convert to uppercase.
>   * @dst: The buffer to store the result.
>   * @src: The string to convert to uppercase.
> + *
> + * Returns the number of characters copied.
>   */
> -static inline void strcpytoupper(char *dst, const char *src)
> +static inline int strcpytoupper(char *dst, const char *src)
>  {
> - strlcpytoupper(dst, src, ~(size_t)0);
> + return strlcpytoupper(dst, src, ~(size_t)0);
>  }
>  
>  /**
>   * strcpytolower - Copy string and convert to lowercase.
>   * @dst: The buffer to store the result.
>   * @src: The string to convert to lowercase.
> + *
> + * Returns the number of characters copied.
>   */
> -static inline void strcpytolower(char *dst, const char *src)
> +static inline int strcpytolower(char *dst, const char *src)
>  {
> - str

Re: [PATCH v3 1/7] lib: string: add functions to case-convert strings

2016-07-13 Thread Markus Mayer
On 13 July 2016 at 15:53, Luis de Bethencourt  wrote:
>
> Hi Markus,
>
> Thank you very much for the thorough testing and solution searching.
>
> Earlier today I sent a patch that removes dynamic_debug.h from
> include/linux/kernel.h, since it isn't really needed. Sorry about this
> since it changes what you were testing just a few hours later.

I did see at least some of the changes you posted. I was mostly
curious to see where the problem was coming from in the first place.

> I am starting to think that getting access to SIZE_MAX isn't worth the
> trouble, specially considering that moving an include out of the top
> of the file makes the code less readable. The Linux kernel is growing
> in complexity and trying to keep it readable for newcomers is very
> important IMHO.

I've been thinking the same thing. It seems to be turning into a huge
effort to make this simple constant available.

> The maintainers have the last word on this, but for now I remove my
> question about why use -1 instead of SIZE_MAX.
>
> Apologies for that, the silver lining was that at least for me it was
> interesting to explore this area of the code and its inclusions. I hope
> it was for you as well.

No need to apologize. It seemed like a no-brainer to use SIZE_MAX. :-)
And I am sure all the digging will come handy in some way.

> PD: just a small comment, in case you end up sending a new version of
> your patches. In the following documentation of your functions.
> * @len: Maximum string length. May be SIZE_MAX (-1) to set no limit.
>
> What do you think about "SIZE_MAX or -1" instead of "SIZE_MAX (-1)" to
> avoid confusing any readers who might think SIZE_MAX is a function? As
> I think you intend to mean your code accepts both -1 or SIZE_MAX, which
> is ~0.
>
> Just an idea, feel free to ignore it :)

I think I might use ~(size_t)0 directly instead of -1 (or SIZE_MAX).

Thanks,
-Markus
--
To unsubscribe from this list: send the line "unsubscribe linux-scsi" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH v3 0/7] lib: string: add functions to case-convert strings

2016-07-13 Thread Markus Mayer
On Sat, Jul 09, 2016 at 09:11:05PM -0700, Markus Mayer wrote:
> On 9 July 2016 at 20:13, Chris Metcalf <cmetc...@mellanox.com> wrote:
> > On 7/8/2016 6:43 PM, Markus Mayer wrote:
> >>
> >> This series introduces a family of generic string case conversion
> >> functions. This kind of functionality is needed in several places in
> >> the kernel. Right now, everybody seems to be implementing their own
> >> copy of this functionality.
> >>
> >> Based on the discussion of the previous version of this series[1] and
> >> the use cases found in the kernel, it does look like having several
> >> flavours of case conversion functions is beneficial. The use cases fall
> >> into three categories:
> >>  - copying a string and converting the case while specifying a
> >>maximum length to mimic strlcpy()
> >>  - copying a string and converting the case without specifying a
> >>length to mimic strcpy()
> >>  - converting the case of a string in-place (i.e. modifying the
> >>string that was passed in)
> >>
> >> Consequently, I am proposing these new functions:
> >>  void strlcpytoupper(char *dst, const char *src, size_t len);
> >>  void strlcpytolower(char *dst, const char *src, size_t len);
> >>  void strcpytoupper(char *dst, const char *src);
> >>  void strcpytolower(char *dst, const char *src);
> >>  void strtoupper(char *s);
> >>  void strtolower(char *s);
> >
> >
> > You may want to read the article here:
> >
> > https://lwn.net/Articles/659214/
> 
> I'll read that. Thanks.

It doesn't look like there is going to be the danger of "mass changes". 
So far, I have two ACKs (one where the semantics doesn't change,
because it's using strtolower()) and the other in a driver in staging.

But I understand the concern and will keep an eye out if there are
other ACKs.
 
> > and follow up some of the discussion threads on LKML about the best
> > semantics to advertise for the strlcpy/strscpy variants.  It might
> > be helpful to return some kind of overflow/truncation error from
> > your copy functions so people can error-check the result.
> 
> I am inclined to agree. However, everybody has been telling me that
> these functions should be void. Originally they weren't.

What about something like this?  It might also work to keep the four
static inline functions as "void" (since they won't ever return E2BIG
anyway) and just have strlcpyto* return an integer (since that's where
the buffer could be too small).

Rasmus, what's your take? 

diff --git a/include/linux/string.h b/include/linux/string.h
index ae82d13..6cc85dc 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -116,8 +116,8 @@ extern void * memchr(const void *,int,__kernel_size_t);
 #endif
 void *memchr_inv(const void *s, int c, size_t n);
 char *strreplace(char *s, char old, char new);
-extern void strlcpytoupper(char *dst, const char *src, size_t len);
-extern void strlcpytolower(char *dst, const char *src, size_t len);
+extern int strlcpytoupper(char *dst, const char *src, size_t len);
+extern int strlcpytolower(char *dst, const char *src, size_t len);
 
 extern void kfree_const(const void *x);
 
@@ -175,38 +175,46 @@ static inline const char *kbasename(const char *path)
  * strcpytoupper - Copy string and convert to uppercase.
  * @dst: The buffer to store the result.
  * @src: The string to convert to uppercase.
+ *
+ * Returns the number of characters copied.
  */
-static inline void strcpytoupper(char *dst, const char *src)
+static inline int strcpytoupper(char *dst, const char *src)
 {
-   strlcpytoupper(dst, src, ~(size_t)0);
+   return strlcpytoupper(dst, src, ~(size_t)0);
 }
 
 /**
  * strcpytolower - Copy string and convert to lowercase.
  * @dst: The buffer to store the result.
  * @src: The string to convert to lowercase.
+ *
+ * Returns the number of characters copied.
  */
-static inline void strcpytolower(char *dst, const char *src)
+static inline int strcpytolower(char *dst, const char *src)
 {
-   strlcpytolower(dst, src, ~(size_t)0);
+   return strlcpytolower(dst, src, ~(size_t)0);
 }
 
 /**
  * strtoupper - Convert string to uppercase.
  * @s: The string to operate on.
+ *
+ * Returns the number of characters copied.
  */
-static inline void strtoupper(char *s)
+static inline int strtoupper(char *s)
 {
-   strlcpytoupper(s, s, ~(size_t)0);
+   return strlcpytoupper(s, s, ~(size_t)0);
 }
 
 /**
  * strtolower - Convert string to lowercase.
  * @s: The string to operate on.
+ *
+ * Returns the number of characters copied.
  */
-static inline void strtolower(char *s)
+static inline int strtolower(char *s)
 {
-   strlcpytolower(s, s,

Re: [PATCH v3 1/7] lib: string: add functions to case-convert strings

2016-07-13 Thread Markus Mayer
On 13 July 2016 at 10:19, Luis de Bethencourt <lui...@osg.samsung.com> wrote:
> On 11/07/16 23:46, Markus Mayer wrote:
>
> Hi Markus,
>
> Amazing. I see this happening as well, but I know it shouldn't.
>
> The reason the #ifndef guards in headers are there is precisely to allow
> circular dependencies.
>
> The problem in your output reads as:
> strstr() is in string.h
> #include string.h -> that includes kernel.h -> that includes string.h
>
> The third should do nothing based on _LINUX_STRING_H_ being defined already
> and all code inside the #ifndef in string.h not being executed.
> Yet it shouldn't block the first include above since that macro isn't defined,
> which is what the error suggests since it doesn't have strstr()
> If _LINUX_STRING_H is defined, strstr() should be available.
>
> Investigating this issue, it only happens when CONFIG_DYNAMIC_DEBUG is not
> set and line 170 of dynamic_debug.h runs, but just above we have an
> include of string.h.
>
> Very strange that #include  isn't doing its job.
>
> The first thing I tried is to understand where dynamic_debug.h is used and
> removed the unneeded ones:
> --
> diff --git a/include/linux/kernel.h b/include/linux/kernel.h
> --- a/include/linux/kernel.h
> +++ b/include/linux/kernel.h
> @@ -11,7 +11,6 @@
>  #include 
>  #include 
>  #include 
> -#include 
>  #include 
>  #include 
>
> diff --git a/include/linux/printk.h b/include/linux/printk.h
> --- a/include/linux/printk.h
> +++ b/include/linux/printk.h
> @@ -307,10 +307,11 @@ asmlinkage __printf(1, 2) __cold void __pr_info(const 
> char *fmt, ...);
> no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
>  #endif
>
> -#include 
>
>  /* If you are writing a driver, please use dev_dbg instead */
>  #if defined(CONFIG_DYNAMIC_DEBUG)
> +#include 
> +
>  /* dynamic_pr_debug() uses pr_fmt() internally so we don't need it here */
>  #define pr_debug(fmt, ...) \
> dynamic_pr_debug(fmt, ##__VA_ARGS__)
> diff --git a/kernel/module.c b/kernel/module.c
> index beaebea..e70a2fa 100644
> --- a/kernel/module.c
> +++ b/kernel/module.c
> @@ -60,6 +60,7 @@
>  #include 
>  #include 
>  #include 
> +#include 
>  #include 
>  #include "module-internal.h"
> --
>
> This diff [0] fixes the issue but it is a workaround for the original
> issue about string.h not being properly included in dynamic_debug.h
>
> Puzzled by this and can't figure out what is happening wrong.
>
> The second thing I tried was adding
> #warning "Linking to string header"
> in include/linux/string.h, and I don't see any include path mentioning
> kernel.h, where do you see the circular dependency? I might be missing
> something.

I did some more poking around and this is what I found.

For starters, the problem happens with kernel/bounds.c. Without
worrying about SIZE_MAX or making any other changes, I added a
#warning line to kernel.h and string.h to see the include sequence.

$ aarch64-linux-gcc -Wp,-MD,kernel/.bounds.s.d  -nostdinc -isystem
/opt/toolchain/stbgcc-4.8-1.5/bin/../lib/gcc/aarch64-linux-gnu/4.8.5/include
-I./arch/arm64/include -Iarch/arm64/include/generated/uapi
-Iarch/arm64/include/generated  -Iinclude -I./arch/arm64/include/uapi
-Iarch/arm64/include/generated/uapi -I./include/uapi
-Iinclude/generated/uapi -include ./include/linux/kconfig.h
-D__KERNEL__ -mlittle-endian -Wall -Wundef -Wstrict-prototypes
-Wno-trigraphs -fno-strict-aliasing -fno-common
-Werror-implicit-function-declaration -Wno-format-security -std=gnu89
-mgeneral-regs-only -fno-asynchronous-unwind-tables
-fno-delete-null-pointer-checks -O2 --param=allow-store-data-races=0
-Wframe-larger-than=2048 -fno-stack-protector
-Wno-unused-but-set-variable -fno-omit-frame-pointer
-fno-optimize-sibling-calls -fno-var-tracking-assignments -g
-Wdeclaration-after-statement -Wno-pointer-sign -fno-strict-overflow
-fconserve-stack -Werror=implicit-int -Werror=strict-prototypes
-DKBUILD_BASENAME='"bounds"'  -DKBUILD_MODNAME='"bounds"'
-fverbose-asm -E -o kernel/bounds.i kernel/bounds.c
In file included from include/asm-generic/bug.h:13:0,
 from ./arch/arm64/include/asm/bug.h:62,
 from include/linux/bug.h:4,
 from include/linux/page-flags.h:9,
 from kernel/bounds.c:9:
include/linux/kernel.h:4:2: warning: #warning In kernel.h [-Wcpp]
 #warning In kernel.h
  ^
In file included from include/linux/dynamic_debug.h:111:0,
 from include/linux/printk.h:289,
 from include/linux/kernel.h:14,
 from include/asm-generic/bug.h:13,
 from .

Re: [PATCH v3 1/7] lib: string: add functions to case-convert strings

2016-07-11 Thread Markus Mayer
On 9 July 2016 at 08:30, Markus Mayer <markus.ma...@broadcom.com> wrote:
> On 9 July 2016 at 05:04, Luis de Bethencourt <lui...@osg.samsung.com> wrote:
>> On 08/07/16 23:43, Markus Mayer wrote:
>>> Add a collection of generic functions to convert strings to lowercase
>>> or uppercase.
>>>
>>> Changing the case of a string (with or without copying it first) seems
>>> to be a recurring requirement in the kernel that is currently being
>>> solved by several duplicated implementations doing the same thing. This
>>> change aims at reducing this code duplication.
>>>
>>> The new functions are
>>> void strlcpytoupper(char *dst, const char *src, size_t len);
>>> void strlcpytolower(char *dst, const char *src, size_t len);
>>> void strcpytoupper(char *dst, const char *src);
>>> void strcpytolower(char *dst, const char *src);
>>> void strtoupper(char *s);
>>> void strtolower(char *s);
>>>
>>> The "str[l]cpyto*" versions of the function take a destination string
>>> and a source string as arguments. The "strlcpyto*" versions additionally
>>> take a length argument like strlcpy() itself. Lastly, the strto*
>>> functions take a single string argument and modify the passed-in string.
>>>
>>> Like strlcpy(), and unlike strncpy(), the functions guarantee NULL
>>> termination of the destination string.
>>>
>>> Signed-off-by: Markus Mayer <mma...@broadcom.com>
>>> ---
>>>  include/linux/string.h | 40 
>>>  lib/string.c   | 38 ++
>>>  2 files changed, 78 insertions(+)
>>>
>>> diff --git a/include/linux/string.h b/include/linux/string.h
>>> index 26b6f6a..36c9d14 100644
>>> --- a/include/linux/string.h
>>> +++ b/include/linux/string.h
>>> @@ -116,6 +116,8 @@ extern void * memchr(const void *,int,__kernel_size_t);
>>>  #endif
>>>  void *memchr_inv(const void *s, int c, size_t n);
>>>  char *strreplace(char *s, char old, char new);
>>> +extern void strlcpytoupper(char *dst, const char *src, size_t len);
>>> +extern void strlcpytolower(char *dst, const char *src, size_t len);
>>>
>>>  extern void kfree_const(const void *x);
>>>
>>> @@ -169,4 +171,42 @@ static inline const char *kbasename(const char *path)
>>>   return tail ? tail + 1 : path;
>>>  }
>>>
>>> +/**
>>> + * strcpytoupper - Copy string and convert to uppercase.
>>> + * @dst: The buffer to store the result.
>>> + * @src: The string to convert to uppercase.
>>> + */
>>> +static inline void strcpytoupper(char *dst, const char *src)
>>> +{
>>> + strlcpytoupper(dst, src, -1);
>>> +}
>>> +
>>
>> Why not use SIZE_MAX instead of -1?
>
> Sure. I'll change all four of them. Thanks.

Turns out there's actually a circular dependency here. SIZE_MAX is
defined in linux/kernel.h. So, string.h would need to include
kernel.h. But kernel.h, by way of several other headers, includes
string.h.

Attempting to include kernel.h in string.h then leads to something like this:

  CHK include/config/kernel.release
  CHK include/generated/uapi/linux/version.h
  CHK include/generated/utsrelease.h
  CC  scripts/mod/devicetable-offsets.s
  CHK include/generated/timeconst.h
In file included from include/linux/printk.h:289:0,
 from include/linux/kernel.h:13,
 from include/linux/string.h:11,
 from include/uapi/linux/uuid.h:21,
 from include/linux/uuid.h:19,
 from include/linux/mod_devicetable.h:12,
 from scripts/mod/devicetable-offsets.c:2:
include/linux/dynamic_debug.h: In function ‘ddebug_dyndbg_module_param_cb’:
include/linux/dynamic_debug.h:122:2: error: implicit declaration of
function ‘strstr’ [-Werror=implicit-function-declaration]
  if (strstr(param, "dyndbg")) {
  ^
include/linux/dynamic_debug.h:122:6: warning: incompatible implicit
declaration of built-in function ‘strstr’ [enabled by default]
  if (strstr(param, "dyndbg")) {
  ^
Since kernel.h is referencing string.h (which is needed, but not
included a second time due to the include guards), this leads to
undeclared string functions, because we are still in the early stages
of including string.h itself and haven't gotten to the function
declarations yet.

>>> +/**
>>> + * strcpytolower - Copy string and convert to lowercase.
>>> + * @dst: The buffer to store the result.
>>> + * @src: T

Re: [PATCH v3 0/7] lib: string: add functions to case-convert strings

2016-07-09 Thread Markus Mayer
On 9 July 2016 at 20:13, Chris Metcalf <cmetc...@mellanox.com> wrote:
> On 7/8/2016 6:43 PM, Markus Mayer wrote:
>>
>> This series introduces a family of generic string case conversion
>> functions. This kind of functionality is needed in several places in
>> the kernel. Right now, everybody seems to be implementing their own
>> copy of this functionality.
>>
>> Based on the discussion of the previous version of this series[1] and
>> the use cases found in the kernel, it does look like having several
>> flavours of case conversion functions is beneficial. The use cases fall
>> into three categories:
>>  - copying a string and converting the case while specifying a
>>maximum length to mimic strlcpy()
>>  - copying a string and converting the case without specifying a
>>length to mimic strcpy()
>>  - converting the case of a string in-place (i.e. modifying the
>>string that was passed in)
>>
>> Consequently, I am proposing these new functions:
>>  void strlcpytoupper(char *dst, const char *src, size_t len);
>>  void strlcpytolower(char *dst, const char *src, size_t len);
>>  void strcpytoupper(char *dst, const char *src);
>>  void strcpytolower(char *dst, const char *src);
>>  void strtoupper(char *s);
>>  void strtolower(char *s);
>
>
> You may want to read the article here:
>
> https://lwn.net/Articles/659214/

I'll read that. Thanks.

> and follow up some of the discussion threads on LKML about the best
> semantics to advertise for the strlcpy/strscpy variants.  It might be
> helpful to return some kind of overflow/truncation error from your
> copy functions so people can error-check the result.

I am inclined to agree. However, everybody has been telling me that
these functions should be void. Originally they weren't.

Regards,
-Markus
--
To unsubscribe from this list: send the line "unsubscribe linux-scsi" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH v3 1/7] lib: string: add functions to case-convert strings

2016-07-09 Thread Markus Mayer
On 9 July 2016 at 05:04, Luis de Bethencourt <lui...@osg.samsung.com> wrote:
> On 08/07/16 23:43, Markus Mayer wrote:
>> Add a collection of generic functions to convert strings to lowercase
>> or uppercase.
>>
>> Changing the case of a string (with or without copying it first) seems
>> to be a recurring requirement in the kernel that is currently being
>> solved by several duplicated implementations doing the same thing. This
>> change aims at reducing this code duplication.
>>
>> The new functions are
>> void strlcpytoupper(char *dst, const char *src, size_t len);
>> void strlcpytolower(char *dst, const char *src, size_t len);
>> void strcpytoupper(char *dst, const char *src);
>> void strcpytolower(char *dst, const char *src);
>> void strtoupper(char *s);
>> void strtolower(char *s);
>>
>> The "str[l]cpyto*" versions of the function take a destination string
>> and a source string as arguments. The "strlcpyto*" versions additionally
>> take a length argument like strlcpy() itself. Lastly, the strto*
>> functions take a single string argument and modify the passed-in string.
>>
>> Like strlcpy(), and unlike strncpy(), the functions guarantee NULL
>> termination of the destination string.
>>
>> Signed-off-by: Markus Mayer <mma...@broadcom.com>
>> ---
>>  include/linux/string.h | 40 
>>  lib/string.c   | 38 ++
>>  2 files changed, 78 insertions(+)
>>
>> diff --git a/include/linux/string.h b/include/linux/string.h
>> index 26b6f6a..36c9d14 100644
>> --- a/include/linux/string.h
>> +++ b/include/linux/string.h
>> @@ -116,6 +116,8 @@ extern void * memchr(const void *,int,__kernel_size_t);
>>  #endif
>>  void *memchr_inv(const void *s, int c, size_t n);
>>  char *strreplace(char *s, char old, char new);
>> +extern void strlcpytoupper(char *dst, const char *src, size_t len);
>> +extern void strlcpytolower(char *dst, const char *src, size_t len);
>>
>>  extern void kfree_const(const void *x);
>>
>> @@ -169,4 +171,42 @@ static inline const char *kbasename(const char *path)
>>   return tail ? tail + 1 : path;
>>  }
>>
>> +/**
>> + * strcpytoupper - Copy string and convert to uppercase.
>> + * @dst: The buffer to store the result.
>> + * @src: The string to convert to uppercase.
>> + */
>> +static inline void strcpytoupper(char *dst, const char *src)
>> +{
>> + strlcpytoupper(dst, src, -1);
>> +}
>> +
>
> Why not use SIZE_MAX instead of -1?

Sure. I'll change all four of them. Thanks.

>> +/**
>> + * strcpytolower - Copy string and convert to lowercase.
>> + * @dst: The buffer to store the result.
>> + * @src: The string to convert to lowercase.
>> + */
>> +static inline void strcpytolower(char *dst, const char *src)
>> +{
>> + strlcpytolower(dst, src, -1);
>> +}
>> +
>
> Same here, and the 2 below :)
>
> Thanks Markus,
> Luis
>
>> +/**
>> + * strtoupper - Convert string to uppercase.
>> + * @s: The string to operate on.
>> + */
>> +static inline void strtoupper(char *s)
>> +{
>> + strlcpytoupper(s, s, -1);
>> +}
>> +
>> +/**
>> + * strtolower - Convert string to lowercase.
>> + * @s: The string to operate on.
>> + */
>> +static inline void strtolower(char *s)
>> +{
>> + strlcpytolower(s, s, -1);
>> +}
>> +
>>  #endif /* _LINUX_STRING_H_ */
>> diff --git a/lib/string.c b/lib/string.c
>> index ed83562..fd8c427 100644
>> --- a/lib/string.c
>> +++ b/lib/string.c
>> @@ -952,3 +952,41 @@ char *strreplace(char *s, char old, char new)
>>   return s;
>>  }
>>  EXPORT_SYMBOL(strreplace);
>> +
>> +/**
>> + * strlcpytoupper - Copy a length-limited string and convert to uppercase.
>> + * @dst: The buffer to store the result.
>> + * @src: The string to convert to uppercase.
>> + * @len: Maximum string length. May be SIZE_MAX (-1) to set no limit.
>> + */
>> +void strlcpytoupper(char *dst, const char *src, size_t len)
>> +{
>> + size_t i;
>> +
>> + if (!len)
>> + return;
>> +
>> + for (i = 0; i < len && src[i]; ++i)
>> + dst[i] = toupper(src[i]);
>> + dst[i < len ? i : i - 1] = '\0';
>> +}
>> +EXPORT_SYMBOL(strlcpytoupper);
>> +
>> +/**
>> + * strlcpytolower - Copy a length-limited string and convert to lowercase.
>> + * @dst: The buffer to store the result.
>> + * @src: The string to convert to lowercase.
>> + * @len: Maximum string length. May be SIZE_MAX (-1) to set no limit.
>> + */
>> +void strlcpytolower(char *dst, const char *src, size_t len)
>> +{
>> + size_t i;
>> +
>> + if (!len)
>> + return;
>> +
>> + for (i = 0; i < len && src[i]; ++i)
>> + dst[i] = tolower(src[i]);
>> + dst[i < len ? i : i - 1] = '\0';
>> +}
>> +EXPORT_SYMBOL(strlcpytolower);
>>
>
--
To unsubscribe from this list: send the line "unsubscribe linux-scsi" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v3 1/7] lib: string: add functions to case-convert strings

2016-07-08 Thread Markus Mayer
Add a collection of generic functions to convert strings to lowercase
or uppercase.

Changing the case of a string (with or without copying it first) seems
to be a recurring requirement in the kernel that is currently being
solved by several duplicated implementations doing the same thing. This
change aims at reducing this code duplication.

The new functions are
void strlcpytoupper(char *dst, const char *src, size_t len);
void strlcpytolower(char *dst, const char *src, size_t len);
void strcpytoupper(char *dst, const char *src);
void strcpytolower(char *dst, const char *src);
void strtoupper(char *s);
void strtolower(char *s);

The "str[l]cpyto*" versions of the function take a destination string
and a source string as arguments. The "strlcpyto*" versions additionally
take a length argument like strlcpy() itself. Lastly, the strto*
functions take a single string argument and modify the passed-in string.

Like strlcpy(), and unlike strncpy(), the functions guarantee NULL
termination of the destination string.

Signed-off-by: Markus Mayer <mma...@broadcom.com>
---
 include/linux/string.h | 40 
 lib/string.c   | 38 ++
 2 files changed, 78 insertions(+)

diff --git a/include/linux/string.h b/include/linux/string.h
index 26b6f6a..36c9d14 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -116,6 +116,8 @@ extern void * memchr(const void *,int,__kernel_size_t);
 #endif
 void *memchr_inv(const void *s, int c, size_t n);
 char *strreplace(char *s, char old, char new);
+extern void strlcpytoupper(char *dst, const char *src, size_t len);
+extern void strlcpytolower(char *dst, const char *src, size_t len);
 
 extern void kfree_const(const void *x);
 
@@ -169,4 +171,42 @@ static inline const char *kbasename(const char *path)
return tail ? tail + 1 : path;
 }
 
+/**
+ * strcpytoupper - Copy string and convert to uppercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to uppercase.
+ */
+static inline void strcpytoupper(char *dst, const char *src)
+{
+   strlcpytoupper(dst, src, -1);
+}
+
+/**
+ * strcpytolower - Copy string and convert to lowercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to lowercase.
+ */
+static inline void strcpytolower(char *dst, const char *src)
+{
+   strlcpytolower(dst, src, -1);
+}
+
+/**
+ * strtoupper - Convert string to uppercase.
+ * @s: The string to operate on.
+ */
+static inline void strtoupper(char *s)
+{
+   strlcpytoupper(s, s, -1);
+}
+
+/**
+ * strtolower - Convert string to lowercase.
+ * @s: The string to operate on.
+ */
+static inline void strtolower(char *s)
+{
+   strlcpytolower(s, s, -1);
+}
+
 #endif /* _LINUX_STRING_H_ */
diff --git a/lib/string.c b/lib/string.c
index ed83562..fd8c427 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -952,3 +952,41 @@ char *strreplace(char *s, char old, char new)
return s;
 }
 EXPORT_SYMBOL(strreplace);
+
+/**
+ * strlcpytoupper - Copy a length-limited string and convert to uppercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to uppercase.
+ * @len: Maximum string length. May be SIZE_MAX (-1) to set no limit.
+ */
+void strlcpytoupper(char *dst, const char *src, size_t len)
+{
+   size_t i;
+
+   if (!len)
+   return;
+
+   for (i = 0; i < len && src[i]; ++i)
+   dst[i] = toupper(src[i]);
+   dst[i < len ? i : i - 1] = '\0';
+}
+EXPORT_SYMBOL(strlcpytoupper);
+
+/**
+ * strlcpytolower - Copy a length-limited string and convert to lowercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to lowercase.
+ * @len: Maximum string length. May be SIZE_MAX (-1) to set no limit.
+ */
+void strlcpytolower(char *dst, const char *src, size_t len)
+{
+   size_t i;
+
+   if (!len)
+   return;
+
+   for (i = 0; i < len && src[i]; ++i)
+   dst[i] = tolower(src[i]);
+   dst[i < len ? i : i - 1] = '\0';
+}
+EXPORT_SYMBOL(strlcpytolower);
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-scsi" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v3 5/7] iscsi-target: replace iscsi_initiatorname_tolower() with strtolower()

2016-07-08 Thread Markus Mayer
After introducing generic strtolower(), iscsi_initiatorname_tolower() is
no longer needed.

Signed-off-by: Markus Mayer <mma...@broadcom.com>
---
 drivers/target/iscsi/iscsi_target_nego.c | 17 +
 1 file changed, 1 insertion(+), 16 deletions(-)

diff --git a/drivers/target/iscsi/iscsi_target_nego.c 
b/drivers/target/iscsi/iscsi_target_nego.c
index 89d34bd..fa20638 100644
--- a/drivers/target/iscsi/iscsi_target_nego.c
+++ b/drivers/target/iscsi/iscsi_target_nego.c
@@ -987,21 +987,6 @@ static int iscsi_target_do_login(struct iscsi_conn *conn, 
struct iscsi_login *lo
return 0;
 }
 
-static void iscsi_initiatorname_tolower(
-   char *param_buf)
-{
-   char *c;
-   u32 iqn_size = strlen(param_buf), i;
-
-   for (i = 0; i < iqn_size; i++) {
-   c = _buf[i];
-   if (!isupper(*c))
-   continue;
-
-   *c = tolower(*c);
-   }
-}
-
 /*
  * Processes the first Login Request..
  */
@@ -1075,7 +1060,7 @@ int iscsi_target_locate_portal(
 * RFC-3720 3.2.6.1. section c) that says that iSCSI IQNs
 * are NOT case sensitive.
 */
-   iscsi_initiatorname_tolower(i_buf);
+   strtolower(i_buf);
 
if (!s_buf) {
if (!login->leading_connection)
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-scsi" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v3 0/7] lib: string: add functions to case-convert strings

2016-07-08 Thread Markus Mayer
This series introduces a family of generic string case conversion
functions. This kind of functionality is needed in several places in
the kernel. Right now, everybody seems to be implementing their own
copy of this functionality.

Based on the discussion of the previous version of this series[1] and
the use cases found in the kernel, it does look like having several
flavours of case conversion functions is beneficial. The use cases fall
into three categories:
- copying a string and converting the case while specifying a
  maximum length to mimic strlcpy()
- copying a string and converting the case without specifying a
  length to mimic strcpy()
- converting the case of a string in-place (i.e. modifying the
  string that was passed in)

Consequently, I am proposing these new functions:
void strlcpytoupper(char *dst, const char *src, size_t len);
void strlcpytolower(char *dst, const char *src, size_t len);
void strcpytoupper(char *dst, const char *src);
void strcpytolower(char *dst, const char *src);
void strtoupper(char *s);
void strtolower(char *s);

Several drivers are being modified to make use of the functions above.
Another driver that also makes use of this functionality will be
submitted upstream shortly, which prompted this whole exercise.

The changes made here have been compile-tested, but not tried out, due
to lack of required hardware.

Changes since v2:
  - use strlcpy() semantics not strncpy() semantics, i.e. guarantee
NULL termination
  - as a result strncpyto<upper|lower> are now called
strlcpyto<upper|lower>
  - make functions void
  - use len == -1 (SIZE_MAX) as no-limit indicator rather then len == 0
  - change PATCH 2/7 to match strlcpy() semantics
  - change PATCH 4/7 to match strlcpy() semantics

Changes since v1:
  - expanded strtolower() into a family of functions that cover use
cases when a length argument is or isn't required and that support
copying the string into a new buffer or changing it in-place 
  - changed the function semantics to return a pointer to the
terminating '\0' character of the modified string
  - added strtoupper() functionality mirroring the above
  - dropped the ACPICA patch, since that code is OS independent and
can't rely on a Linux library function (see [2])
  - Added two new patches replacing strtoupper() implementations

[1] https://lkml.org/lkml/2016/6/30/727
[2] https://lkml.org/lkml/2016/7/1/9



Markus Mayer (7):
  lib: string: add functions to case-convert strings
  drm/nouveau/core: make use of new strlcpytolower() function
  ACPI / device_sysfs: make use of new strtolower() function
  staging: speakup: replace spk_strlwr() with strlcpytolower()
  iscsi-target: replace iscsi_initiatorname_tolower() with strtolower()
  drm/nouveau/fifo/gk104: make use of new strcpytoupper() function
  power_supply: make use of new strcpytoupper() function

 drivers/acpi/device_sysfs.c  |  4 +--
 drivers/gpu/drm/nouveau/nvkm/core/firmware.c |  9 +-
 drivers/gpu/drm/nouveau/nvkm/engine/fifo/gk104.c |  5 +--
 drivers/power/power_supply_sysfs.c   | 13 +++-
 drivers/staging/speakup/kobjects.c   |  3 +-
 drivers/staging/speakup/main.c   |  3 +-
 drivers/staging/speakup/speakup.h|  1 -
 drivers/staging/speakup/varhandlers.c| 12 ---
 drivers/target/iscsi/iscsi_target_nego.c | 17 +-
 include/linux/string.h   | 40 
 lib/string.c | 38 ++
 11 files changed, 90 insertions(+), 55 deletions(-)

-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-scsi" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH v2 1/7] lib: string: add functions to case-convert strings

2016-07-08 Thread Markus Mayer
On 7 July 2016 at 17:19, Rasmus Villemoes <li...@rasmusvillemoes.dk> wrote:
> On Tue, Jul 05 2016, Markus Mayer <mma...@broadcom.com> wrote:
>
>> +/**
>> + * strncpytoupper - Copy a length-limited string and convert to uppercase.
>> + * @dst: The buffer to store the result.
>> + * @src: The string to convert to uppercase.
>> + * @len: Maximum string length. May be 0 to set no limit.
>> + *
>> + * Returns pointer to terminating '\0' in @dst.
>> + */
>> +char *strncpytoupper(char *dst, const char *src, size_t len)
>> +{
>> + size_t i;
>> +
>> + for (i = 0; src[i] != '\0' && (i < len || !len); i++)
>> + dst[i] = toupper(src[i]);
>> + if (i < len || !len)
>> + dst[i] = '\0';
>> +
>> + return dst + i;
>> +}
>
> Hm, this seems to copy the insane semantics from strncpy of not
> guaranteeing '\0'-termination.

Yeah. I've been tossing that one around a bit. The reason I did it
this way in the end is due to the use cases I found. strncpy() is
being used there and I was a little wary to make too many changes all
at once.

But I understand your point. I'll look at it again and see what
changes might be required in the code that used strncpy() before.

> Why use 0 as a sentinel, when (size_t)-1 == SIZE_MAX would work just as
> well and require a little less code (no || !len)?

I'll change that.

> I regret suggesting this return semantics and now agree that void would
> be better, especially since there doesn't seem to be anyone who can
> use this (or any other) return value. How about
>
> if (!len)
>return;
>
> for (i = 0; i < len && src[i]; ++i)
>   dst[i] = toupper(src[i]);
> dst[i < len ? i : i-1] = '\0';

This makes sense.

> (I think you must do i < len before testing src[i], since the len
> parameter should be an upper bound on the number of bytes to access in
> both src and dst).
>
> Rasmus

Thanks,
-Markus
--
To unsubscribe from this list: send the line "unsubscribe linux-scsi" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH v2 0/7] lib: string: add functions to case-convert strings

2016-07-05 Thread Markus Mayer
On 5 July 2016 at 15:56, Joe Perches <j...@perches.com> wrote:
> On Tue, 2016-07-05 at 15:36 -0700, Markus Mayer wrote:
>> On 5 July 2016 at 15:14, Joe Perches <j...@perches.com> wrote:
>> > On Tue, 2016-07-05 at 13:47 -0700, Markus Mayer wrote:
>> > > This series introduces a family of generic string case conversion
>> > > functions. This kind of functionality is needed in several places in
>> > > the kernel. Right now, everybody seems to be implementing their own
>> > > copy of this functionality.
>> > >
>> > > Based on the discussion of the previous version of this series[1] and
>> > > the use cases found in the kernel, it does look like having several
>> > > flavours of case conversion functions is beneficial. The use cases fall
>> > > into three categories:
>> > > - copying a string and converting the case while specifying a
>> > >   maximum length to mimic strncpy()
>> > > - copying a string and converting the case without specifying a
>> > >   length to mimic strcpy()
>> > > - converting the case of a string in-place (i.e. modifying the
>> > >   string that was passed in)
>> > >
>> > > Consequently, I am proposing these new functions:
>> > > char *strncpytoupper(char *dst, const char *src, size_t len);
>> > > char *strncpytolower(char *dst, const char *src, size_t len);
>> > > char *strcpytoupper(char *dst, const char *src);
>> > > char *strcpytolower(char *dst, const char *src);
>> > > char *strtoupper(char *s);
>> > > char *strtolower(char *s);
>> > I think there isn't much value in anything other
>> > than strto.
>> >
>> > Using str[n]cpy followed by strto is
>> > pretty obvious and rarely used anyway.
>> First time around, folks were proposing the "copy" variants when I
>> submitted just strtolower() by itself[1]. They just asked for source
>> and destination parameters to strtolower(), but looking at the use
>> cases that wouldn't have worked so well. Hence it evolved into these 6
>> functions.
>>
>> Here's a breakdown of how the functions are being used (patches 2-7),
>> see also [2]:
>>
>> Patch 2: strncpytolower()
>> Patch 3: strtolower()
>> Patch 4: strncpytolower() and strtolower()
>> Patch 5: strtolower()
>> Patch 6: strcpytoupper()
>> Patch 7: strcpytoupper()
>>
>> So it does look like the copy + change case variant is more frequently
>> used than just strto.
>
> Are these functions useful?   Not to me, not so much.

The use cases do exist. I'll leave it up to the maintainers to decide
whether duplicate implementations or potentially unused generic
functions are to be preferred.

What I do know is that I have a driver in the wings that also needs a
strolower() implementation. If this ends up not being accepted, I'll
have to add yet another driver-local strtolower() implementation to
the kernel. But if that's the decision then so be it.

> None of the functions would have the strcpy performance of
> the arch / asm
> versions of strcpy and the savings in overall
> code isn't significant (or
> measured?).

100% agreed. These functions won't set any speed records. Keep in mind
also that in 4 out of the 6 cases where I replaced local
implementations of "strcpytoXXX", the code was doing essentially the
same I am proposing here (no assembly code). Only 2 of the 6 called
strncpy() before, benefiting from optimized assembly implementations.
But they still had to walk the string explicitly afterwards to convert
the case, which probably means the overall speed won't change that
much using the functions proposed here.

> Of course none of the uses are runtime performance important.

That is also very true.

> This patch also adds always compiled functions that aren't used
> in many .configs.

It adds 2 functions (strncpytolower() and strncpytoupper()). The other
4 are static inline one-liners and won't show up anywhere if not used
(and if used the compiler will insert calls to strncpyto<lower|upper>
instead).

Regards,
-Markus
--
To unsubscribe from this list: send the line "unsubscribe linux-scsi" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH v2 0/7] lib: string: add functions to case-convert strings

2016-07-05 Thread Markus Mayer
On 5 July 2016 at 15:14, Joe Perches <j...@perches.com> wrote:
> On Tue, 2016-07-05 at 13:47 -0700, Markus Mayer wrote:
>> This series introduces a family of generic string case conversion
>> functions. This kind of functionality is needed in several places in
>> the kernel. Right now, everybody seems to be implementing their own
>> copy of this functionality.
>>
>> Based on the discussion of the previous version of this series[1] and
>> the use cases found in the kernel, it does look like having several
>> flavours of case conversion functions is beneficial. The use cases fall
>> into three categories:
>> - copying a string and converting the case while specifying a
>>   maximum length to mimic strncpy()
>> - copying a string and converting the case without specifying a
>>   length to mimic strcpy()
>> - converting the case of a string in-place (i.e. modifying the
>>   string that was passed in)
>>
>> Consequently, I am proposing these new functions:
>> char *strncpytoupper(char *dst, const char *src, size_t len);
>> char *strncpytolower(char *dst, const char *src, size_t len);
>> char *strcpytoupper(char *dst, const char *src);
>> char *strcpytolower(char *dst, const char *src);
>> char *strtoupper(char *s);
>> char *strtolower(char *s);
>
> I think there isn't much value in anything other
> than strto<upper|lower>.
>
> Using str[n]cpy followed by strto<upper|lower> is
> pretty obvious and rarely used anyway.

First time around, folks were proposing the "copy" variants when I
submitted just strtolower() by itself[1]. They just asked for source
and destination parameters to strtolower(), but looking at the use
cases that wouldn't have worked so well. Hence it evolved into these 6
functions.

Here's a breakdown of how the functions are being used (patches 2-7),
see also [2]:

Patch 2: strncpytolower()
Patch 3: strtolower()
Patch 4: strncpytolower() and strtolower()
Patch 5: strtolower()
Patch 6: strcpytoupper()
Patch 7: strcpytoupper()

So it does look like the copy + change case variant is more frequently
used than just strto<upper|lower>.

Regards,
-Markus

[1] https://lkml.org/lkml/2016/7/1/652
[2] https://lkml.org/lkml/2016/7/5/542
--
To unsubscribe from this list: send the line "unsubscribe linux-scsi" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v2 5/7] iscsi-target: replace iscsi_initiatorname_tolower() with strtolower()

2016-07-05 Thread Markus Mayer
After introducing generic strtolower(), iscsi_initiatorname_tolower() is
no longer needed.

Signed-off-by: Markus Mayer <mma...@broadcom.com>
---
 drivers/target/iscsi/iscsi_target_nego.c | 17 +
 1 file changed, 1 insertion(+), 16 deletions(-)

diff --git a/drivers/target/iscsi/iscsi_target_nego.c 
b/drivers/target/iscsi/iscsi_target_nego.c
index 89d34bd..fa20638 100644
--- a/drivers/target/iscsi/iscsi_target_nego.c
+++ b/drivers/target/iscsi/iscsi_target_nego.c
@@ -987,21 +987,6 @@ static int iscsi_target_do_login(struct iscsi_conn *conn, 
struct iscsi_login *lo
return 0;
 }
 
-static void iscsi_initiatorname_tolower(
-   char *param_buf)
-{
-   char *c;
-   u32 iqn_size = strlen(param_buf), i;
-
-   for (i = 0; i < iqn_size; i++) {
-   c = _buf[i];
-   if (!isupper(*c))
-   continue;
-
-   *c = tolower(*c);
-   }
-}
-
 /*
  * Processes the first Login Request..
  */
@@ -1075,7 +1060,7 @@ int iscsi_target_locate_portal(
 * RFC-3720 3.2.6.1. section c) that says that iSCSI IQNs
 * are NOT case sensitive.
 */
-   iscsi_initiatorname_tolower(i_buf);
+   strtolower(i_buf);
 
if (!s_buf) {
if (!login->leading_connection)
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-scsi" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v2 1/7] lib: string: add functions to case-convert strings

2016-07-05 Thread Markus Mayer
Add a collection of generic functions to convert strings to lowercase
or uppercase.

Changing the case of a string (with or without copying it first) seems
to be a recurring requirement in the kernel that is currently being
solved by several duplicated implementations doing the same thing. This
change aims at reducing this code duplication.

The new functions are
char *strncpytoupper(char *dst, const char *src, size_t len);
char *strncpytolower(char *dst, const char *src, size_t len);
char *strcpytoupper(char *dst, const char *src);
char *strcpytolower(char *dst, const char *src);
char *strtoupper(char *s);
char *strtolower(char *s);

The "str[n]cpyto*" versions of the function take a destination string
and a source string as arguments. The "strncpyto*" versions
additionally take a length argument like strncpy() itself. Lastly, the
strto* functions take a single string argument and modify the passed-in
string.

All functions return a pointer to the terminating '\0' character in the
modified string ("dst" or "s", respectively).

Signed-off-by: Markus Mayer <mma...@broadcom.com>
---
 include/linux/string.h | 48 
 lib/string.c   | 42 ++
 2 files changed, 90 insertions(+)

diff --git a/include/linux/string.h b/include/linux/string.h
index 26b6f6a..c58d510 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -116,6 +116,8 @@ extern void * memchr(const void *,int,__kernel_size_t);
 #endif
 void *memchr_inv(const void *s, int c, size_t n);
 char *strreplace(char *s, char old, char new);
+char *strncpytoupper(char *dst, const char *src, size_t len);
+char *strncpytolower(char *dst, const char *src, size_t len);
 
 extern void kfree_const(const void *x);
 
@@ -169,4 +171,50 @@ static inline const char *kbasename(const char *path)
return tail ? tail + 1 : path;
 }
 
+/**
+ * strcpytoupper - Copy string and convert to uppercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to uppercase.
+ *
+ * Returns pointer to terminating '\0' in @dst.
+ */
+static inline char *strcpytoupper(char *dst, const char *src)
+{
+   return strncpytoupper(dst, src, 0);
+}
+
+/**
+ * strcpytolower - Copy string and convert to lowercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to lowercase.
+ *
+ * Returns pointer to terminating '\0' in @dst.
+ */
+static inline char *strcpytolower(char *dst, const char *src)
+{
+   return strncpytolower(dst, src, 0);
+}
+
+/**
+ * strtoupper - Convert string to uppercase.
+ * @s: The string to operate on.
+ *
+ * Returns pointer to terminating '\0' in @s.
+ */
+static inline char *strtoupper(char *s)
+{
+   return strncpytoupper(s, s, 0);
+}
+
+/**
+ * strtolower - Convert string to lowercase.
+ * @s: The string to operate on.
+ *
+ * Returns pointer to terminating '\0' in @s.
+ */
+static inline char *strtolower(char *s)
+{
+   return strncpytolower(s, s, 0);
+}
+
 #endif /* _LINUX_STRING_H_ */
diff --git a/lib/string.c b/lib/string.c
index ed83562..900f357 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -952,3 +952,45 @@ char *strreplace(char *s, char old, char new)
return s;
 }
 EXPORT_SYMBOL(strreplace);
+
+/**
+ * strncpytoupper - Copy a length-limited string and convert to uppercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to uppercase.
+ * @len: Maximum string length. May be 0 to set no limit.
+ *
+ * Returns pointer to terminating '\0' in @dst.
+ */
+char *strncpytoupper(char *dst, const char *src, size_t len)
+{
+   size_t i;
+
+   for (i = 0; src[i] != '\0' && (i < len || !len); i++)
+   dst[i] = toupper(src[i]);
+   if (i < len || !len)
+   dst[i] = '\0';
+
+   return dst + i;
+}
+EXPORT_SYMBOL(strncpytoupper);
+
+/**
+ * strncpytolower - Copy a length-limited string and convert to lowercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to lowercase.
+ * @len: Maximum string length. May be 0 to set no limit.
+ *
+ * Returns pointer to terminating '\0' in @dst.
+ */
+char *strncpytolower(char *dst, const char *src, size_t len)
+{
+   size_t i;
+
+   for (i = 0; src[i] != '\0' && (i < len || !len); i++)
+   dst[i] = tolower(src[i]);
+   if (i < len || !len)
+   dst[i] = '\0';
+
+   return dst + i;
+}
+EXPORT_SYMBOL(strncpytolower);
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-scsi" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v2 0/7] lib: string: add functions to case-convert strings

2016-07-05 Thread Markus Mayer
This series introduces a family of generic string case conversion
functions. This kind of functionality is needed in several places in
the kernel. Right now, everybody seems to be implementing their own
copy of this functionality.

Based on the discussion of the previous version of this series[1] and
the use cases found in the kernel, it does look like having several
flavours of case conversion functions is beneficial. The use cases fall
into three categories:
- copying a string and converting the case while specifying a
  maximum length to mimic strncpy()
- copying a string and converting the case without specifying a
  length to mimic strcpy()
- converting the case of a string in-place (i.e. modifying the
  string that was passed in)

Consequently, I am proposing these new functions:
char *strncpytoupper(char *dst, const char *src, size_t len);
char *strncpytolower(char *dst, const char *src, size_t len);
char *strcpytoupper(char *dst, const char *src);
char *strcpytolower(char *dst, const char *src);
char *strtoupper(char *s);
char *strtolower(char *s);
They all return a pointer to the terminating '\0' in the destination
string (for strtoupper() and strtolower() that is "s").

Several drivers are being modified to make use of the functions above.
Another driver that also makes use of this functionality will be
submitted upstream shortly, which prompted this whole exercise.

The changes made here have been compile-tested, but not tried out, due
to lack of required hardware.

Changes since v1:
  - expanded strtolower() into a family of functions that cover use
cases when a length argument is or isn't required and that support
copying the string into a new buffer or changing it in-place 
  - changed the function semantics to return a pointer to the
terminating '\0' character of the modified string
  - added strtoupper() functionality mirroring the above
  - dropped the ACPICA patch, since that code is OS independent and
can't rely on a Linux library function (see [2])
  - Added two new patches replacing strtoupper() implementations

[1] https://lkml.org/lkml/2016/6/30/727
[2] https://lkml.org/lkml/2016/7/1/9

Markus Mayer (7):
  lib: string: add functions str[n]cpytolower()/str[n]cpytoupper()
  drm/nouveau/core: make use of new strncpytolower() function
  ACPI / device_sysfs: make use of new strtolower() function
  staging: speakup: replace spk_strlwr() with strncpytolower()
  iscsi-target: replace iscsi_initiatorname_tolower() with
strcpytolower()
  drm/nouveau/fifo/gk104: make use of new strcpytoupper() function
  power_supply: make use of new strcpytoupper() function

 drivers/acpi/device_sysfs.c  |  4 +-
 drivers/gpu/drm/nouveau/nvkm/core/firmware.c |  9 +
 drivers/gpu/drm/nouveau/nvkm/engine/fifo/gk104.c |  5 +--
 drivers/power/power_supply_sysfs.c   | 13 +++
 drivers/staging/speakup/kobjects.c   |  3 +-
 drivers/staging/speakup/main.c   |  3 +-
 drivers/staging/speakup/speakup.h|  1 -
 drivers/staging/speakup/varhandlers.c| 12 --
 drivers/target/iscsi/iscsi_target_nego.c | 17 +
 include/linux/string.h   | 48 
 lib/string.c | 40 
 11 files changed, 100 insertions(+), 55 deletions(-)

-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-scsi" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH 1/6] lib: string: add function strtolower()

2016-07-04 Thread Markus Mayer
On 1 July 2016 at 14:08, Rasmus Villemoes  wrote:

> A few suggestions:
>
> - Make the function take separate src and dst parameters, making it explicitly
>   allowed to pass the same value (but not other kinds of overlap, of
>   course). That way one can avoid "strcpy(dst, src); strtolower(dst);".
>
> - Drop the NULL check. If someone does "foo->bar = something;
>   strtolower(foo->bar); put foo in a global data structure...", the
>   dereference of foo->bar may happen much later. Doing the NULL deref
>   sooner means it's much easier to find and fix the bug. (Also, other
>   str* and mem* functions don't usually check for NULL).
>
> - While it's true that strcpy and memcpy by definition return dst, that's
>   mostly useless. If you want it to return anything, please make it
>   something that might be used - for example, having stpcpy semantics
>   (returning a pointer to dst's terminating \0) means a caller might avoid
>   a strlen call.
>
> - Maybe do strtoupper while you're at it. Quick grepping didn't find any
>   use for the copy-while-lowercasing, but copy-while-uppercasing can at
>   least be used in drivers/acpi/acpica/nsrepair2.c,
>   drivers/gpu/drm/nouveau/nvkm/engine/fifo/gk104.c,
>   drivers/power/power_supply_sysfs.c along with a bunch of inplace
>   uppercasing.
>
>
> Rasmus

Thanks for the suggestions to you and Jani. Based on the feedback I
received, I am reworking the series now and will post v2 probably
tomorrow.

Regards,
-Markus
--
To unsubscribe from this list: send the line "unsubscribe linux-scsi" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH 1/6] lib: string: add function strtolower()

2016-07-01 Thread Markus Mayer
On 1 July 2016 at 03:52, Jani Nikula <jani.nik...@linux.intel.com> wrote:
> On Fri, 01 Jul 2016, Markus Mayer <mma...@broadcom.com> wrote:
>> Add a function called strtolower() to convert strings to lower case
>> in-place, overwriting the original string.
>>
>> This seems to be a recurring requirement in the kernel that is
>> currently being solved by several duplicated implementations doing the
>> same thing.
>>
>> Signed-off-by: Markus Mayer <mma...@broadcom.com>
>> ---
>>  include/linux/string.h |  1 +
>>  lib/string.c   | 14 ++
>>  2 files changed, 15 insertions(+)
>>
>> diff --git a/include/linux/string.h b/include/linux/string.h
>> index 26b6f6a..aad605e 100644
>> --- a/include/linux/string.h
>> +++ b/include/linux/string.h
>> @@ -116,6 +116,7 @@ extern void * memchr(const void *,int,__kernel_size_t);
>>  #endif
>>  void *memchr_inv(const void *s, int c, size_t n);
>>  char *strreplace(char *s, char old, char new);
>> +char *strtolower(char *s);
>>
>>  extern void kfree_const(const void *x);
>>
>> diff --git a/lib/string.c b/lib/string.c
>> index ed83562..6e3b560 100644
>> --- a/lib/string.c
>> +++ b/lib/string.c
>> @@ -952,3 +952,17 @@ char *strreplace(char *s, char old, char new)
>>   return s;
>>  }
>>  EXPORT_SYMBOL(strreplace);
>> +
>
> This needs a kernel-doc comment right here.

Will add it.

>> +char *strtolower(char *s)
>> +{
>> + char *p;
>> +
>> +if (unlikely(!s))
>> +return NULL;
>
> Using spaces for indentation? See scripts/checkpatch.pl.

Not on purpose. Thanks for spotting it.

>> +
>> + for (p = s; *p; p++)
>> + *p = tolower(*p);
>> +
>> + return s;
>
> Why does it return a value? Could be void?

It could be void, but I thought that would make the function's use
less flexible. As is, the return value is there if anybody wants it,
but it can be ignored if it is not needed. Also, it seems customary
for string functions to be returning the string that was passed in.

I'll change it to void if there are strong opinions leaning that way.
Personally, I like that it returns a char * better.

> BR,
> Jani.
>
>> +}
>> +EXPORT_SYMBOL(strtolower);
>
> --
> Jani Nikula, Intel Open Source Technology Center
--
To unsubscribe from this list: send the line "unsubscribe linux-scsi" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH 1/6] lib: string: add function strtolower()

2016-06-30 Thread Markus Mayer
Add a function called strtolower() to convert strings to lower case
in-place, overwriting the original string.

This seems to be a recurring requirement in the kernel that is
currently being solved by several duplicated implementations doing the
same thing.

Signed-off-by: Markus Mayer <mma...@broadcom.com>
---
 include/linux/string.h |  1 +
 lib/string.c   | 14 ++
 2 files changed, 15 insertions(+)

diff --git a/include/linux/string.h b/include/linux/string.h
index 26b6f6a..aad605e 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -116,6 +116,7 @@ extern void * memchr(const void *,int,__kernel_size_t);
 #endif
 void *memchr_inv(const void *s, int c, size_t n);
 char *strreplace(char *s, char old, char new);
+char *strtolower(char *s);
 
 extern void kfree_const(const void *x);
 
diff --git a/lib/string.c b/lib/string.c
index ed83562..6e3b560 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -952,3 +952,17 @@ char *strreplace(char *s, char old, char new)
return s;
 }
 EXPORT_SYMBOL(strreplace);
+
+char *strtolower(char *s)
+{
+   char *p;
+
+if (unlikely(!s))
+return NULL;
+
+   for (p = s; *p; p++)
+   *p = tolower(*p);
+
+   return s;
+}
+EXPORT_SYMBOL(strtolower);
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-scsi" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH 6/6] iscsi-target: replace iscsi_initiatorname_tolower() with strtolower()

2016-06-30 Thread Markus Mayer
After introducing generic strtolower(), iscsi_initiatorname_tolower()
is no longer needed.

Signed-off-by: Markus Mayer <mma...@broadcom.com>
---
 drivers/target/iscsi/iscsi_target_nego.c | 17 +
 1 file changed, 1 insertion(+), 16 deletions(-)

diff --git a/drivers/target/iscsi/iscsi_target_nego.c 
b/drivers/target/iscsi/iscsi_target_nego.c
index 89d34bd..fa20638 100644
--- a/drivers/target/iscsi/iscsi_target_nego.c
+++ b/drivers/target/iscsi/iscsi_target_nego.c
@@ -987,21 +987,6 @@ static int iscsi_target_do_login(struct iscsi_conn *conn, 
struct iscsi_login *lo
return 0;
 }
 
-static void iscsi_initiatorname_tolower(
-   char *param_buf)
-{
-   char *c;
-   u32 iqn_size = strlen(param_buf), i;
-
-   for (i = 0; i < iqn_size; i++) {
-   c = _buf[i];
-   if (!isupper(*c))
-   continue;
-
-   *c = tolower(*c);
-   }
-}
-
 /*
  * Processes the first Login Request..
  */
@@ -1075,7 +1060,7 @@ int iscsi_target_locate_portal(
 * RFC-3720 3.2.6.1. section c) that says that iSCSI IQNs
 * are NOT case sensitive.
 */
-   iscsi_initiatorname_tolower(i_buf);
+   strtolower(i_buf);
 
if (!s_buf) {
if (!login->leading_connection)
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-scsi" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH 0/6] lib: string: add function strtolower()

2016-06-30 Thread Markus Mayer
This series introduces a new generic function strtolower(), which
converts strings to lowercase in-place, overwriting the original
string. This kind of functionality is needed in several places in the
kernel. Right now, everybody seems to be implementing their own copy of
this function. So, we replace several custom "strtolower"
implementations with this new library function.

Another driver that also makes use of this function will be submitted
upstream shortly, which prompted this whole exercise.

The changes made here have been compile-tested, but not tried out, due
to lack of required hardware.

This series is based on v4.7-rc5.

Markus Mayer (6):
  lib: string: add function strtolower()
  drm/nouveau/core: make use of new strtolower() function
  ACPICA: make use of new strtolower() function
  ACPI / device_sysfs: make use of new strtolower() function
  staging: speakup: replace spk_strlwr() with strtolower()
  iscsi-target: replace iscsi_initiatorname_tolower() with strtolower()

 drivers/acpi/acpica/utnonansi.c  | 13 +
 drivers/acpi/device_sysfs.c  |  4 +---
 drivers/gpu/drm/nouveau/nvkm/core/firmware.c |  7 +--
 drivers/staging/speakup/kobjects.c   |  2 +-
 drivers/staging/speakup/main.c   |  2 +-
 drivers/staging/speakup/speakup.h|  1 -
 drivers/staging/speakup/varhandlers.c| 12 
 drivers/target/iscsi/iscsi_target_nego.c | 17 +
 include/linux/string.h   |  1 +
 lib/string.c | 14 ++
 10 files changed, 21 insertions(+), 52 deletions(-)

-- 
2.7.4
--
To unsubscribe from this list: send the line "unsubscribe linux-scsi" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html