tcm: Convert backstore ->set_configfs_dev_params() to use parser.h
Subsystems (2)
PATCH 1/1 tcm: Convert backstore ->set_configfs_dev_params() to use parser.h
From: Nicholas Bellinger <[email protected]>
This patch converts the backend struct se_subsystem_api->set_configfs_dev_params() callers for IBLOCK, FILEIO, PSCSI, RAMDISK and STGT to use proper kstrdup(), strsep() and linux/parser.h match_token() logic.
--- drivers/target/target_core_file.c | 95 +++++++++++++-------------- drivers/target/target_core_iblock.c | 81 ++++++++++------------- drivers/target/target_core_pscsi.c | 123 +++++++++++++--------------------- drivers/target/target_core_rd.c | 69 +++++++++----------- drivers/target/target_core_stgt.c | 108 ++++++++++++------------------ 5 files changed, 205 insertions(+), 271 deletions(-)
drivers/target/target_core_file.c +47 -48
@@ -28,6 +28,7 @@#include <linux/version.h> #include <linux/string.h> +#include <linux/parser.h> #include <linux/timer.h> #include <linux/blkdev.h> #include <linux/slab.h>
@@ -645,80 +646,78 @@ static void fd_free_task(struct se_task *task)kfree (req); } +enum { + Opt_fd_dev_name, Opt_fd_dev_size, Opt_fd_buffered_io +}; + +static match_table_t tokens = { + {Opt_fd_dev_name,"fd_dev_name=%s" }, + {Opt_fd_dev_size,"fd_dev_size=%s" }, + {Opt_fd_buffered_io,"fd_buffered_id=%d" }, +}; +static ssize_t fd_set_configfs_dev_params (struct se_hba *hba,struct se_subsystem_dev *se_dev,const char *page,ssize_t count) { -struct fd_dev *fd_dev = (struct fd_dev *) se_dev->se_dev_su_ptr; -char *buf, *cur, *ptr, *ptr2; -int params =0 ; -/* -* Make sure we take into account the NULL terminator when copying -* the const buffer here.. -*/ - buf =kzalloc (count +1 ,GFP_KERNEL ); -if (!(buf)) { -printk (KERN_ERR "Unable to allocate memory for" -" temporary buffer\n" ); -return 0 ; - } -memcpy (buf, page, count); - cur = buf; +struct fd_dev *fd_dev = se_dev->se_dev_su_ptr; +char *orig, *ptr, *arg_p, *opts; +substring_t args[MAX_OPT_ARGS ]; +int ret =0 , arg, token; -while (cur) { - ptr =strstr (cur,"=" ); -if (!(ptr)) - goto out; + opts =kstrdup (page,GFP_KERNEL ); +if (!opts) +return -ENOMEM ; - *ptr ='\0' ; - ptr++; + orig = opts; - ptr2 =strstr (cur,"fd_dev_name" ); -if (ptr2) { -transport_check_dev_params_delim (ptr, &cur); - ptr =strstrip (ptr); +while ((ptr =strsep (&opts,"," )) !=NULL ) { +if (!*ptr) +continue ; + + token =match_token (ptr, tokens, args); +switch (token) { +case Opt_fd_dev_name:snprintf (fd_dev->fd_dev_name,FD_MAX_DEV_NAME , -"%s" , ptr); +"%s" ,match_strdup (&args[0 ]));printk (KERN_INFO "FILEIO: Referencing Path: %s\n" , fd_dev->fd_dev_name); fd_dev->fbd_flags |=FBDF_HAS_PATH ; - params++; -continue ; - } - ptr2 =strstr (cur,"fd_dev_size" ); -if (ptr2) { -transport_check_dev_params_delim (ptr, &cur); -if (strict_strtoull (ptr,0 , &fd_dev->fd_dev_size) <0 ) { +break ; +case Opt_fd_dev_size: + arg_p =match_strdup (&args[0 ]); + ret =strict_strtoull (arg_p,0 , &fd_dev->fd_dev_size); +if (ret <0 ) {printk (KERN_ERR "strict_strtoull() failed for" " fd_dev_size=\n" ); -continue ; + goto out; }printk (KERN_INFO "FILEIO: Referencing Size: %llu" " bytes\n" , fd_dev->fd_dev_size); fd_dev->fbd_flags |=FBDF_HAS_SIZE ; - params++; -continue ; - } - ptr2 =strstr (cur,"fd_buffered_io" ); -if (ptr2) { -transport_check_dev_params_delim (ptr, &cur); -if (strncmp (ptr,"1" ,1 )) -continue ; +break ; +case Opt_fd_buffered_io: +match_int (args, &arg); +if (arg !=1 ) { +printk (KERN_ERR "bogus fd_buffered_io=%d value\n" , arg); + ret = -EINVAL ; + goto out; + }printk (KERN_INFO "FILEIO: Using buffered I/O" " operations for struct fd_dev\n" ); fd_dev->fbd_flags |=FDBD_USE_BUFFERED_IO ; - params++; -continue ; - }else - cur =NULL ; +break ; +default : +break ; + } } out: -kfree (buf); -return (params) ? count : -EINVAL ; +kfree (orig); +return (!ret) ? count : ret; }static ssize_t fd_check_configfs_dev_params (struct se_hba *hba,struct se_subsystem_dev *se_dev)
drivers/target/target_core_iblock.c +36 -45
116 lines not on this page · raw message
drivers/target/target_core_pscsi.c +47 -76
168 lines not on this page · raw message
drivers/target/target_core_rd.c +32 -37
94 lines not on this page · raw message
drivers/target/target_core_stgt.c +43 -65
138 lines not on this page · raw message
Signature · 1 line
1.5.6.5
Review
Quoted 1 line
(quoted diff) +1
-0+ opts = kstrdup(page, GFP_KERNEL);
What do you need the this copy for?
Otherwise looks good.
Quoted 6 lines · Christoph Hellwig
Review
Quoted 1 line
(quoted diff) +1
-0+ opts = kstrdup(page, GFP_KERNEL);What do you need the this copy for?
Otherwise looks good.
<nod> thanks for the pointer here..
Btw, the kstrdup() is required because the configfs attribute store function will be passing a 'const char *page' that is propigated from target_core_configfs.c: target_core_store_dev_control() out to ->set_configfs_dev_params() backend code.
Quoted 6 lines · Nicholas A. Bellinger
<nod> thanks for the pointer here..
Btw, the
kstrdup()is required because the configfs attribute store function will be passing a 'const char *page' that is propigated fromtarget_core_configfs.c:target_core_store_dev_control()out to ->set_configfs_dev_params() backend code.
strsep doesn't modify the data it points to, just the pointer itself. We could get around this by casting the pointer, which at least avoids the memory allocation. But it's probably not worth bothering.
Quoted 12 lines · Christoph Hellwig
Quoted 6 lines · Nicholas A. Bellinger
<nod> thanks for the pointer here..
Btw, the
kstrdup()is required because the configfs attribute store function will be passing a 'const char *page' that is propigated fromtarget_core_configfs.c:target_core_store_dev_control()out to ->set_configfs_dev_params() backend code.strsep doesn't modify the data it points to, just the pointer itself. We could get around this by casting the pointer, which at least avoids the memory allocation. But it's probably not worth bothering.
If it is so, then the proper fix is to "const" the parameter to strsep() most certainly don't hide a const_cast like that. This is a landmine waiting to explode.
Boaz