Message ID | 20220825164833.3923454-2-cezary.rojewski@intel.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | libfs: Introduce tokenize_user_input() | expand |
On Thu, Aug 25, 2022 at 06:48:32PM +0200, Cezary Rojewski wrote: > Add new helper function to allow for splitting specified user string > into a sequence of integers. Internally it makes use of get_options() so > the returned sequence contains the integers extracted plus an additional > element that begins the sequence and specifies the integers count. > > Suggested-by: Andy Shevchenko <andy.shevchenko@gmail.com> > Signed-off-by: Cezary Rojewski <cezary.rojewski@intel.com> > --- > fs/libfs.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ > include/linux/fs.h | 1 + This really has nothing to do with filesystems. Surely string_helpers.[ch] is the appropriate place for this code? Also get_options() should probably move its prototype from kernel.h to string_helpers.h.
On 2022-08-30 8:33 PM, Matthew Wilcox wrote: > On Thu, Aug 25, 2022 at 06:48:32PM +0200, Cezary Rojewski wrote: >> Add new helper function to allow for splitting specified user string >> into a sequence of integers. Internally it makes use of get_options() so >> the returned sequence contains the integers extracted plus an additional >> element that begins the sequence and specifies the integers count. >> >> Suggested-by: Andy Shevchenko <andy.shevchenko@gmail.com> >> Signed-off-by: Cezary Rojewski <cezary.rojewski@intel.com> >> --- >> fs/libfs.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ >> include/linux/fs.h | 1 + > > This really has nothing to do with filesystems. Surely > string_helpers.[ch] is the appropriate place for this code? > Also get_options() should probably move its prototype from kernel.h to > string_helpers.h. Hello Matthew, Thanks for your input. The initial version of the change was located in the string_helpers.[ch] just like you propose and I have no problem moving it back again. string_helpers are devoid of __user content though and that's why in v2 it's part of libfs instead. I'll give a day or two to see if there are other suggestions and then send v3 relocating the implementation. Regards, Czarek
diff --git a/fs/libfs.c b/fs/libfs.c index 31b0ddf01c31..078b23e26741 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -809,6 +809,51 @@ ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos, } EXPORT_SYMBOL(simple_write_to_buffer); +/** + * tokenize_user_input - Split string into a sequence of integers + * @from: The user space buffer to read from + * @ppos: The current position in the buffer + * @count: The maximum number of bytes to read + * @tkns: Returned pointer to sequence of integers + * + * On success @tkns is allocated and initialized with a sequence of + * integers extracted from the @from plus an additional element that + * begins the sequence and specifies the integers count. + * + * Caller takes responsibility for freeing @tkns when it is no longer + * needed. + */ +int tokenize_user_input(const char __user *from, size_t count, int **tkns) +{ + int *ints, nints; + char *buf; + int ret = 0; + + buf = memdup_user_nul(from, count); + if (IS_ERR(buf)) + return PTR_ERR(buf); + + get_options(buf, 0, &nints); + if (!nints) { + ret = -ENOENT; + goto free_buf; + } + + ints = kcalloc(nints + 1, sizeof(*ints), GFP_KERNEL); + if (!ints) { + ret = -ENOMEM; + goto free_buf; + } + + get_options(buf, nints + 1, ints); + *tkns = ints; + +free_buf: + kfree(buf); + return ret; +} +EXPORT_SYMBOL(tokenize_user_input); + /** * memory_read_from_buffer - copy data from the buffer * @to: the kernel space buffer to read to diff --git a/include/linux/fs.h b/include/linux/fs.h index 9eced4cc286e..ab04cc7f9efa 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -3345,6 +3345,7 @@ extern ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos, const void *from, size_t available); extern ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos, const void __user *from, size_t count); +extern int tokenize_user_input(const char __user *from, size_t count, int **tkns); extern int __generic_file_fsync(struct file *, loff_t, loff_t, int); extern int generic_file_fsync(struct file *, loff_t, loff_t, int);
Add new helper function to allow for splitting specified user string into a sequence of integers. Internally it makes use of get_options() so the returned sequence contains the integers extracted plus an additional element that begins the sequence and specifies the integers count. Suggested-by: Andy Shevchenko <andy.shevchenko@gmail.com> Signed-off-by: Cezary Rojewski <cezary.rojewski@intel.com> --- fs/libfs.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ include/linux/fs.h | 1 + 2 files changed, 46 insertions(+)