2 * linux/net/sunrpc/socklib.c
4 * Common socket helper routines for RPC client and server
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
9 #include <linux/types.h>
10 #include <linux/pagemap.h>
11 #include <linux/udp.h>
12 #include <linux/sunrpc/xdr.h>
16 * skb_read_bits - copy some data bits from skb to internal buffer
17 * @desc: sk_buff copy helper
18 * @to: copy destination
19 * @len: number of bytes to copy
21 * Possibly called several times to iterate over an sk_buff and copy
24 static size_t skb_read_bits(skb_reader_t *desc, void *to, size_t len)
26 if (len > desc->count)
28 if (skb_copy_bits(desc->skb, desc->offset, to, len))
36 * skb_read_and_csum_bits - copy and checksum from skb to buffer
37 * @desc: sk_buff copy helper
38 * @to: copy destination
39 * @len: number of bytes to copy
41 * Same as skb_read_bits, but calculate a checksum at the same time.
43 static size_t skb_read_and_csum_bits(skb_reader_t *desc, void *to, size_t len)
45 unsigned int csum2, pos;
47 if (len > desc->count)
50 csum2 = skb_copy_and_csum_bits(desc->skb, pos, to, len, 0);
51 desc->csum = csum_block_add(desc->csum, csum2, pos);
58 * xdr_partial_copy_from_skb - copy data out of an skb
59 * @xdr: target XDR buffer
60 * @base: starting offset
61 * @desc: sk_buff copy helper
62 * @copy_actor: virtual method for copying data
65 ssize_t xdr_partial_copy_from_skb(struct xdr_buf *xdr, unsigned int base, skb_reader_t *desc, skb_read_actor_t copy_actor)
67 struct page **ppage = xdr->pages;
68 unsigned int len, pglen = xdr->page_len;
72 len = xdr->head[0].iov_len;
75 ret = copy_actor(desc, (char *)xdr->head[0].iov_base + base, len);
77 if (ret != len || !desc->count)
83 if (unlikely(pglen == 0))
85 if (unlikely(base >= pglen)) {
89 if (base || xdr->page_base) {
91 base += xdr->page_base;
92 ppage += base >> PAGE_CACHE_SHIFT;
93 base &= ~PAGE_CACHE_MASK;
98 /* ACL likes to be lazy in allocating pages - ACLs
99 * are small by default but can get huge. */
100 if (unlikely(*ppage == NULL)) {
101 *ppage = alloc_page(GFP_ATOMIC);
102 if (unlikely(*ppage == NULL)) {
109 len = PAGE_CACHE_SIZE;
110 kaddr = kmap_atomic(*ppage, KM_SKB_SUNRPC_DATA);
115 ret = copy_actor(desc, kaddr + base, len);
120 ret = copy_actor(desc, kaddr, len);
122 flush_dcache_page(*ppage);
123 kunmap_atomic(kaddr, KM_SKB_SUNRPC_DATA);
125 if (ret != len || !desc->count)
128 } while ((pglen -= len) != 0);
130 len = xdr->tail[0].iov_len;
132 copied += copy_actor(desc, (char *)xdr->tail[0].iov_base + base, len - base);
138 * csum_partial_copy_to_xdr - checksum and copy data
139 * @xdr: target XDR buffer
142 * We have set things up such that we perform the checksum of the UDP
143 * packet in parallel with the copies into the RPC client iovec. -DaveM
145 int csum_partial_copy_to_xdr(struct xdr_buf *xdr, struct sk_buff *skb)
150 desc.offset = sizeof(struct udphdr);
151 desc.count = skb->len - desc.offset;
153 if (skb->ip_summed == CHECKSUM_UNNECESSARY)
156 desc.csum = csum_partial(skb->data, desc.offset, skb->csum);
157 if (xdr_partial_copy_from_skb(xdr, 0, &desc, skb_read_and_csum_bits) < 0)
159 if (desc.offset != skb->len) {
161 csum2 = skb_checksum(skb, desc.offset, skb->len - desc.offset, 0);
162 desc.csum = csum_block_add(desc.csum, csum2, desc.offset);
166 if ((unsigned short)csum_fold(desc.csum))
170 if (xdr_partial_copy_from_skb(xdr, 0, &desc, skb_read_bits) < 0)