]> git.kernelconcepts.de Git - karo-tx-linux.git/commitdiff
SUNRPC: Add a helper function xdr_stream_decode_string_dup()
authorTrond Myklebust <trond.myklebust@primarydata.com>
Sun, 19 Feb 2017 21:08:31 +0000 (16:08 -0500)
committerAnna Schumaker <Anna.Schumaker@Netapp.com>
Tue, 21 Feb 2017 21:56:16 +0000 (16:56 -0500)
Create a helper function that decodes a xdr string object, allocates a memory
buffer and then store it as a NUL terminated string.

Signed-off-by: Trond Myklebust <trond.myklebust@primarydata.com>
Reviewed-by: Chuck Lever <chuck.lever@oracle.com>
Signed-off-by: Anna Schumaker <Anna.Schumaker@Netapp.com>
include/linux/sunrpc/xdr.h
net/sunrpc/xdr.c

index dc7e51a769accbe809aee6f675a8785d72de09c2..054c8cde18f33bb1179fa1c90c699c9a120db7a5 100644 (file)
@@ -242,6 +242,8 @@ extern unsigned int xdr_read_pages(struct xdr_stream *xdr, unsigned int len);
 extern void xdr_enter_page(struct xdr_stream *xdr, unsigned int len);
 extern int xdr_process_buf(struct xdr_buf *buf, unsigned int offset, unsigned int len, int (*actor)(struct scatterlist *, void *), void *data);
 
+ssize_t xdr_stream_decode_string_dup(struct xdr_stream *xdr, char **str,
+               size_t maxlen, gfp_t gfp_flags);
 /**
  * xdr_align_size - Calculate padded size of an object
  * @n: Size of an object being XDR encoded (in bytes)
index 7f1071e103cafd7e4fadd75969cad8d5235fa927..1f7082144e0168070a9e8ed703ca044f9700b8dc 100644 (file)
@@ -1518,3 +1518,37 @@ out:
 }
 EXPORT_SYMBOL_GPL(xdr_process_buf);
 
+/**
+ * xdr_stream_decode_string_dup - Decode and duplicate variable length string
+ * @xdr: pointer to xdr_stream
+ * @str: location to store pointer to string
+ * @maxlen: maximum acceptable string length
+ * @gfp_flags: GFP mask to use
+ *
+ * Return values:
+ *   On success, returns length of NUL-terminated string stored in *@ptr
+ *   %-EBADMSG on XDR buffer overflow
+ *   %-EMSGSIZE if the size of the string would exceed @maxlen
+ *   %-ENOMEM on memory allocation failure
+ */
+ssize_t xdr_stream_decode_string_dup(struct xdr_stream *xdr, char **str,
+               size_t maxlen, gfp_t gfp_flags)
+{
+       void *p;
+       ssize_t ret;
+
+       ret = xdr_stream_decode_opaque_inline(xdr, &p, maxlen);
+       if (ret > 0) {
+               char *s = kmalloc(ret + 1, gfp_flags);
+               if (s != NULL) {
+                       memcpy(s, p, ret);
+                       s[ret] = '\0';
+                       *str = s;
+                       return strlen(s);
+               }
+               ret = -ENOMEM;
+       }
+       *str = NULL;
+       return ret;
+}
+EXPORT_SYMBOL_GPL(xdr_stream_decode_string_dup);