aboutsummaryrefslogtreecommitdiffstats
path: root/net
diff options
context:
space:
mode:
authorMichael S. Tsirkin <mst@redhat.com>2009-04-19 21:25:46 -0400
committerDavid S. Miller <davem@davemloft.net>2009-04-21 08:42:44 -0400
commit0a1ec07a67bd8b0033dace237249654d015efa21 (patch)
tree1963b9ba273f0786c8a972c27b5599dfb4a19f06 /net
parent0cededf3ffbb775c3716aa1d246338fdc24b1259 (diff)
net: skb_copy_datagram_const_iovec()
There's an skb_copy_datagram_iovec() to copy out of a paged skb, but it modifies the iovec, and does not support starting at an offset in the destination. We want both in tun.c, so let's add the function. It's a carbon copy of skb_copy_datagram_iovec() with enough changes to be annoying. Signed-off-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'net')
-rw-r--r--net/core/datagram.c92
-rw-r--r--net/core/iovec.c26
2 files changed, 118 insertions, 0 deletions
diff --git a/net/core/datagram.c b/net/core/datagram.c
index d0de644b378d..4dbb05cd572b 100644
--- a/net/core/datagram.c
+++ b/net/core/datagram.c
@@ -339,6 +339,98 @@ fault:
339} 339}
340 340
341/** 341/**
342 * skb_copy_datagram_const_iovec - Copy a datagram to an iovec.
343 * @skb: buffer to copy
344 * @offset: offset in the buffer to start copying from
345 * @to: io vector to copy to
346 * @to_offset: offset in the io vector to start copying to
347 * @len: amount of data to copy from buffer to iovec
348 *
349 * Returns 0 or -EFAULT.
350 * Note: the iovec is not modified during the copy.
351 */
352int skb_copy_datagram_const_iovec(const struct sk_buff *skb, int offset,
353 const struct iovec *to, int to_offset,
354 int len)
355{
356 int start = skb_headlen(skb);
357 int i, copy = start - offset;
358
359 /* Copy header. */
360 if (copy > 0) {
361 if (copy > len)
362 copy = len;
363 if (memcpy_toiovecend(to, skb->data + offset, to_offset, copy))
364 goto fault;
365 if ((len -= copy) == 0)
366 return 0;
367 offset += copy;
368 to_offset += copy;
369 }
370
371 /* Copy paged appendix. Hmm... why does this look so complicated? */
372 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
373 int end;
374
375 WARN_ON(start > offset + len);
376
377 end = start + skb_shinfo(skb)->frags[i].size;
378 if ((copy = end - offset) > 0) {
379 int err;
380 u8 *vaddr;
381 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
382 struct page *page = frag->page;
383
384 if (copy > len)
385 copy = len;
386 vaddr = kmap(page);
387 err = memcpy_toiovecend(to, vaddr + frag->page_offset +
388 offset - start, to_offset, copy);
389 kunmap(page);
390 if (err)
391 goto fault;
392 if (!(len -= copy))
393 return 0;
394 offset += copy;
395 to_offset += copy;
396 }
397 start = end;
398 }
399
400 if (skb_shinfo(skb)->frag_list) {
401 struct sk_buff *list = skb_shinfo(skb)->frag_list;
402
403 for (; list; list = list->next) {
404 int end;
405
406 WARN_ON(start > offset + len);
407
408 end = start + list->len;
409 if ((copy = end - offset) > 0) {
410 if (copy > len)
411 copy = len;
412 if (skb_copy_datagram_const_iovec(list,
413 offset - start,
414 to, to_offset,
415 copy))
416 goto fault;
417 if ((len -= copy) == 0)
418 return 0;
419 offset += copy;
420 to_offset += copy;
421 }
422 start = end;
423 }
424 }
425 if (!len)
426 return 0;
427
428fault:
429 return -EFAULT;
430}
431EXPORT_SYMBOL(skb_copy_datagram_const_iovec);
432
433/**
342 * skb_copy_datagram_from_iovec - Copy a datagram from an iovec. 434 * skb_copy_datagram_from_iovec - Copy a datagram from an iovec.
343 * @skb: buffer to copy 435 * @skb: buffer to copy
344 * @offset: offset in the buffer to start copying to 436 * @offset: offset in the buffer to start copying to
diff --git a/net/core/iovec.c b/net/core/iovec.c
index 4c9c0121c9da..a215545c0a34 100644
--- a/net/core/iovec.c
+++ b/net/core/iovec.c
@@ -98,6 +98,31 @@ int memcpy_toiovec(struct iovec *iov, unsigned char *kdata, int len)
98} 98}
99 99
100/* 100/*
101 * Copy kernel to iovec. Returns -EFAULT on error.
102 */
103
104int memcpy_toiovecend(const struct iovec *iov, unsigned char *kdata,
105 int offset, int len)
106{
107 int copy;
108 for (; len > 0; ++iov) {
109 /* Skip over the finished iovecs */
110 if (unlikely(offset >= iov->iov_len)) {
111 offset -= iov->iov_len;
112 continue;
113 }
114 copy = min_t(unsigned int, iov->iov_len - offset, len);
115 offset = 0;
116 if (copy_to_user(iov->iov_base, kdata, copy))
117 return -EFAULT;
118 kdata += copy;
119 len -= copy;
120 }
121
122 return 0;
123}
124
125/*
101 * Copy iovec to kernel. Returns -EFAULT on error. 126 * Copy iovec to kernel. Returns -EFAULT on error.
102 * 127 *
103 * Note: this modifies the original iovec. 128 * Note: this modifies the original iovec.
@@ -236,3 +261,4 @@ EXPORT_SYMBOL(csum_partial_copy_fromiovecend);
236EXPORT_SYMBOL(memcpy_fromiovec); 261EXPORT_SYMBOL(memcpy_fromiovec);
237EXPORT_SYMBOL(memcpy_fromiovecend); 262EXPORT_SYMBOL(memcpy_fromiovecend);
238EXPORT_SYMBOL(memcpy_toiovec); 263EXPORT_SYMBOL(memcpy_toiovec);
264EXPORT_SYMBOL(memcpy_toiovecend);