lib/string_helpers: Introduce parse_int_array_user()
[linux-2.6-block.git] / lib / string_helpers.c
index 5ed3beb066e6d09fe8deae3607a8f58416ad13f3..230020a2e076ce9e14e2a2c960218eac5f6e0134 100644 (file)
@@ -131,6 +131,50 @@ void string_get_size(u64 size, u64 blk_size, const enum string_size_units units,
 }
 EXPORT_SYMBOL(string_get_size);
 
+/**
+ * parse_int_array_user - Split string into a sequence of integers
+ * @from:      The user space buffer to read from
+ * @count:     The maximum number of bytes to read
+ * @array:     Returned pointer to sequence of integers
+ *
+ * On success @array 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 @array when it is no longer
+ * needed.
+ */
+int parse_int_array_user(const char __user *from, size_t count, int **array)
+{
+       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);
+       *array = ints;
+
+free_buf:
+       kfree(buf);
+       return ret;
+}
+EXPORT_SYMBOL(parse_int_array_user);
+
 static bool unescape_space(char **src, char **dst)
 {
        char *p = *dst, *q = *src;