lkml.org 
[lkml]   [2015]   [May]   [8]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
Date
From
Subject[PATCH 12/12] parse_integer: convert fs/ocfs2/
Convert away from deprecated simple_strto*() interfaces.

Autodetection of value range from type allows to remove disgusting checks:

if ((major == LONG_MIN) || (major == LONG_MAX) ||
(major > (u8)-1) || (major < 1))
return -ERANGE;
if ((minor == LONG_MIN) || (minor == LONG_MAX) ||
(minor > (u8)-1) || (minor < 0))

Poof, they're gone!

Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
---
fs/ocfs2/cluster/heartbeat.c | 54 ++++++++++++++++++------------------------
fs/ocfs2/cluster/nodemanager.c | 50 +++++++++++++++++---------------------
fs/ocfs2/stack_user.c | 50 +++++++++++++++++++-------------------
3 files changed, 70 insertions(+), 84 deletions(-)

diff --git a/fs/ocfs2/cluster/heartbeat.c b/fs/ocfs2/cluster/heartbeat.c
index 16eff45..889f38c 100644
--- a/fs/ocfs2/cluster/heartbeat.c
+++ b/fs/ocfs2/cluster/heartbeat.c
@@ -1484,13 +1484,12 @@ static int o2hb_read_block_input(struct o2hb_region *reg,
unsigned long *ret_bytes,
unsigned int *ret_bits)
{
- unsigned long bytes;
- char *p = (char *)page;
-
- bytes = simple_strtoul(p, &p, 0);
- if (!p || (*p && (*p != '\n')))
- return -EINVAL;
+ unsigned int bytes;
+ int rv;

+ rv = kstrtouint(page, 0, &bytes);
+ if (rv < 0)
+ return rv;
/* Heartbeat and fs min / max block sizes are the same. */
if (bytes > 4096 || bytes < 512)
return -ERANGE;
@@ -1543,18 +1542,14 @@ static ssize_t o2hb_region_start_block_write(struct o2hb_region *reg,
const char *page,
size_t count)
{
- unsigned long long tmp;
- char *p = (char *)page;
+ int rv;

if (reg->hr_bdev)
return -EINVAL;

- tmp = simple_strtoull(p, &p, 0);
- if (!p || (*p && (*p != '\n')))
- return -EINVAL;
-
- reg->hr_start_block = tmp;
-
+ rv = kstrtoull(page, 0, &reg->hr_start_block);
+ if (rv < 0)
+ return rv;
return count;
}

@@ -1568,20 +1563,19 @@ static ssize_t o2hb_region_blocks_write(struct o2hb_region *reg,
const char *page,
size_t count)
{
- unsigned long tmp;
- char *p = (char *)page;
+ unsigned int tmp;
+ int rv;

if (reg->hr_bdev)
return -EINVAL;

- tmp = simple_strtoul(p, &p, 0);
- if (!p || (*p && (*p != '\n')))
- return -EINVAL;
-
+ rv = kstrtouint(page, 0, &tmp);
+ if (rv < 0)
+ return rv;
if (tmp > O2NM_MAX_NODES || tmp == 0)
return -ERANGE;

- reg->hr_blocks = (unsigned int)tmp;
+ reg->hr_blocks = tmp;

return count;
}
@@ -1717,9 +1711,8 @@ static ssize_t o2hb_region_dev_write(struct o2hb_region *reg,
size_t count)
{
struct task_struct *hb_task;
- long fd;
+ int fd;
int sectsize;
- char *p = (char *)page;
struct fd f;
struct inode *inode;
ssize_t ret = -EINVAL;
@@ -1733,10 +1726,9 @@ static ssize_t o2hb_region_dev_write(struct o2hb_region *reg,
if (o2nm_this_node() == O2NM_MAX_NODES)
goto out;

- fd = simple_strtol(p, &p, 0);
- if (!p || (*p && (*p != '\n')))
+ ret = kstrtoint(page, 0, &fd);
+ if (ret < 0)
goto out;
-
if (fd < 0 || fd >= INT_MAX)
goto out;

@@ -2210,12 +2202,12 @@ static ssize_t o2hb_heartbeat_group_threshold_store(struct o2hb_heartbeat_group
const char *page,
size_t count)
{
- unsigned long tmp;
- char *p = (char *)page;
+ unsigned int tmp;
+ int rv;

- tmp = simple_strtoul(p, &p, 10);
- if (!p || (*p && (*p != '\n')))
- return -EINVAL;
+ rv = kstrtouint(page, 10, &tmp);
+ if (rv < 0)
+ return rv;

/* this will validate ranges for us. */
o2hb_dead_threshold_set((unsigned int) tmp);
diff --git a/fs/ocfs2/cluster/nodemanager.c b/fs/ocfs2/cluster/nodemanager.c
index 441c84e..0381ada 100644
--- a/fs/ocfs2/cluster/nodemanager.c
+++ b/fs/ocfs2/cluster/nodemanager.c
@@ -195,13 +195,12 @@ static ssize_t o2nm_node_num_write(struct o2nm_node *node, const char *page,
size_t count)
{
struct o2nm_cluster *cluster = to_o2nm_cluster_from_node(node);
- unsigned long tmp;
- char *p = (char *)page;
-
- tmp = simple_strtoul(p, &p, 0);
- if (!p || (*p && (*p != '\n')))
- return -EINVAL;
+ unsigned int tmp;
+ int rv;

+ rv = parse_integer(page, 0, &tmp);
+ if (rv < 0)
+ return rv;
if (tmp >= O2NM_MAX_NODES)
return -ERANGE;

@@ -215,16 +214,15 @@ static ssize_t o2nm_node_num_write(struct o2nm_node *node, const char *page,

write_lock(&cluster->cl_nodes_lock);
if (cluster->cl_nodes[tmp])
- p = NULL;
+ rv = -EEXIST;
else {
cluster->cl_nodes[tmp] = node;
node->nd_num = tmp;
set_bit(tmp, cluster->cl_nodes_bitmap);
}
write_unlock(&cluster->cl_nodes_lock);
- if (p == NULL)
- return -EEXIST;
-
+ if (rv < 0)
+ return rv;
return count;
}
static ssize_t o2nm_node_ipv4_port_read(struct o2nm_node *node, char *page)
@@ -235,13 +233,12 @@ static ssize_t o2nm_node_ipv4_port_read(struct o2nm_node *node, char *page)
static ssize_t o2nm_node_ipv4_port_write(struct o2nm_node *node,
const char *page, size_t count)
{
- unsigned long tmp;
- char *p = (char *)page;
-
- tmp = simple_strtoul(p, &p, 0);
- if (!p || (*p && (*p != '\n')))
- return -EINVAL;
+ u16 tmp;
+ int rv;

+ rv = kstrtou16(page, 0, &tmp);
+ if (rv < 0)
+ return rv;
if (tmp == 0)
return -EINVAL;
if (tmp >= (u16)-1)
@@ -305,13 +302,11 @@ static ssize_t o2nm_node_local_write(struct o2nm_node *node, const char *page,
{
struct o2nm_cluster *cluster = to_o2nm_cluster_from_node(node);
unsigned long tmp;
- char *p = (char *)page;
ssize_t ret;

- tmp = simple_strtoul(p, &p, 0);
- if (!p || (*p && (*p != '\n')))
- return -EINVAL;
-
+ ret = kstrtoul(page, 0, &tmp);
+ if (ret < 0)
+ return ret;
tmp = !!tmp; /* boolean of whether this node wants to be local */

/* setting local turns on networking rx for now so we require having
@@ -484,16 +479,15 @@ struct o2nm_cluster_attribute {
static ssize_t o2nm_cluster_attr_write(const char *page, ssize_t count,
unsigned int *val)
{
- unsigned long tmp;
- char *p = (char *)page;
-
- tmp = simple_strtoul(p, &p, 0);
- if (!p || (*p && (*p != '\n')))
- return -EINVAL;
+ unsigned int tmp;
+ int rv;

+ rv = kstrtouint(page, 0, &tmp);
+ if (rv < 0)
+ return rv;
if (tmp == 0)
return -EINVAL;
- if (tmp >= (u32)-1)
+ if (tmp >= (unsigned int)-1)
return -ERANGE;

*val = tmp;
diff --git a/fs/ocfs2/stack_user.c b/fs/ocfs2/stack_user.c
index 2768eb1..eaabdd2 100644
--- a/fs/ocfs2/stack_user.c
+++ b/fs/ocfs2/stack_user.c
@@ -368,9 +368,9 @@ static int ocfs2_control_get_this_node(void)
static int ocfs2_control_do_setnode_msg(struct file *file,
struct ocfs2_control_message_setn *msg)
{
- long nodenum;
- char *ptr = NULL;
struct ocfs2_control_private *p = file->private_data;
+ int nodenum;
+ int rv;

if (ocfs2_control_get_handshake_state(file) !=
OCFS2_CONTROL_HANDSHAKE_PROTOCOL)
@@ -384,12 +384,12 @@ static int ocfs2_control_do_setnode_msg(struct file *file,
return -EINVAL;
msg->space = msg->newline = '\0';

- nodenum = simple_strtol(msg->nodestr, &ptr, 16);
- if (!ptr || *ptr)
+ rv = parse_integer(msg->nodestr, 16, &nodenum);
+ if (rv < 0)
+ return rv;
+ if (msg->nodestr[rv])
return -EINVAL;
-
- if ((nodenum == LONG_MIN) || (nodenum == LONG_MAX) ||
- (nodenum > INT_MAX) || (nodenum < 0))
+ if (nodenum < 0)
return -ERANGE;
p->op_this_node = nodenum;

@@ -399,11 +399,11 @@ static int ocfs2_control_do_setnode_msg(struct file *file,
static int ocfs2_control_do_setversion_msg(struct file *file,
struct ocfs2_control_message_setv *msg)
{
- long major, minor;
- char *ptr = NULL;
+ u8 major, minor;
struct ocfs2_control_private *p = file->private_data;
struct ocfs2_protocol_version *max =
&ocfs2_user_plugin.sp_max_proto;
+ int rv;

if (ocfs2_control_get_handshake_state(file) !=
OCFS2_CONTROL_HANDSHAKE_PROTOCOL)
@@ -418,11 +418,15 @@ static int ocfs2_control_do_setversion_msg(struct file *file,
return -EINVAL;
msg->space1 = msg->space2 = msg->newline = '\0';

- major = simple_strtol(msg->major, &ptr, 16);
- if (!ptr || *ptr)
+ rv = parse_integer(msg->major, 16, &major);
+ if (rv < 0)
+ return rv;
+ if (msg->major[rv])
return -EINVAL;
- minor = simple_strtol(msg->minor, &ptr, 16);
- if (!ptr || *ptr)
+ rv = parse_integer(msg->minor, 16, &minor);
+ if (rv < 0)
+ return rv;
+ if (msg->minor[rv])
return -EINVAL;

/*
@@ -430,11 +434,7 @@ static int ocfs2_control_do_setversion_msg(struct file *file,
* must be between 0 and 255, inclusive. The version passed in
* must be within the maximum version supported by the filesystem.
*/
- if ((major == LONG_MIN) || (major == LONG_MAX) ||
- (major > (u8)-1) || (major < 1))
- return -ERANGE;
- if ((minor == LONG_MIN) || (minor == LONG_MAX) ||
- (minor > (u8)-1) || (minor < 0))
+ if (major < 1)
return -ERANGE;
if ((major != max->pv_major) ||
(minor > max->pv_minor))
@@ -449,8 +449,8 @@ static int ocfs2_control_do_setversion_msg(struct file *file,
static int ocfs2_control_do_down_msg(struct file *file,
struct ocfs2_control_message_down *msg)
{
- long nodenum;
- char *p = NULL;
+ int nodenum;
+ int rv;

if (ocfs2_control_get_handshake_state(file) !=
OCFS2_CONTROL_HANDSHAKE_VALID)
@@ -465,12 +465,12 @@ static int ocfs2_control_do_down_msg(struct file *file,
return -EINVAL;
msg->space1 = msg->space2 = msg->newline = '\0';

- nodenum = simple_strtol(msg->nodestr, &p, 16);
- if (!p || *p)
+ rv = parse_integer(msg->nodestr, 16, &nodenum);
+ if (rv < 0)
+ return rv;
+ if (msg->nodestr[rv])
return -EINVAL;
-
- if ((nodenum == LONG_MIN) || (nodenum == LONG_MAX) ||
- (nodenum > INT_MAX) || (nodenum < 0))
+ if (nodenum < 0)
return -ERANGE;

ocfs2_control_send_down(msg->uuid, nodenum);
--
2.0.4


\
 
 \ /
  Last update: 2015-05-08 21:01    [W:0.132 / U:8.220 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site