llc: hold llc_sap before release_sock()
[linux/fpc-iii.git] / crypto / scatterwalk.c
blobbc769c448d4a9829c0733ca3adbd1dc04bd5afec
1 /*
2 * Cryptographic API.
4 * Cipher operations.
6 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
7 * 2002 Adam J. Richter <adam@yggdrasil.com>
8 * 2004 Jean-Luc Cooke <jlcooke@certainkey.com>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 2 of the License, or (at your option)
13 * any later version.
17 #include <crypto/scatterwalk.h>
18 #include <linux/kernel.h>
19 #include <linux/mm.h>
20 #include <linux/module.h>
21 #include <linux/pagemap.h>
22 #include <linux/highmem.h>
23 #include <linux/scatterlist.h>
25 static inline void memcpy_dir(void *buf, void *sgdata, size_t nbytes, int out)
27 void *src = out ? buf : sgdata;
28 void *dst = out ? sgdata : buf;
30 memcpy(dst, src, nbytes);
33 void scatterwalk_start(struct scatter_walk *walk, struct scatterlist *sg)
35 walk->sg = sg;
37 BUG_ON(!sg->length);
39 walk->offset = sg->offset;
41 EXPORT_SYMBOL_GPL(scatterwalk_start);
43 void *scatterwalk_map(struct scatter_walk *walk)
45 return kmap_atomic(scatterwalk_page(walk)) +
46 offset_in_page(walk->offset);
48 EXPORT_SYMBOL_GPL(scatterwalk_map);
50 static void scatterwalk_pagedone(struct scatter_walk *walk, int out,
51 unsigned int more)
53 if (out) {
54 struct page *page;
56 page = sg_page(walk->sg) + ((walk->offset - 1) >> PAGE_SHIFT);
57 /* Test ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE first as
58 * PageSlab cannot be optimised away per se due to
59 * use of volatile pointer.
61 if (ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE && !PageSlab(page))
62 flush_dcache_page(page);
65 if (more) {
66 walk->offset += PAGE_SIZE - 1;
67 walk->offset &= PAGE_MASK;
68 if (walk->offset >= walk->sg->offset + walk->sg->length)
69 scatterwalk_start(walk, sg_next(walk->sg));
73 void scatterwalk_done(struct scatter_walk *walk, int out, int more)
75 if (!more || walk->offset >= walk->sg->offset + walk->sg->length ||
76 !(walk->offset & (PAGE_SIZE - 1)))
77 scatterwalk_pagedone(walk, out, more);
79 EXPORT_SYMBOL_GPL(scatterwalk_done);
81 void scatterwalk_copychunks(void *buf, struct scatter_walk *walk,
82 size_t nbytes, int out)
84 for (;;) {
85 unsigned int len_this_page = scatterwalk_pagelen(walk);
86 u8 *vaddr;
88 if (len_this_page > nbytes)
89 len_this_page = nbytes;
91 vaddr = scatterwalk_map(walk);
92 memcpy_dir(buf, vaddr, len_this_page, out);
93 scatterwalk_unmap(vaddr);
95 scatterwalk_advance(walk, len_this_page);
97 if (nbytes == len_this_page)
98 break;
100 buf += len_this_page;
101 nbytes -= len_this_page;
103 scatterwalk_pagedone(walk, out, 1);
106 EXPORT_SYMBOL_GPL(scatterwalk_copychunks);
108 void scatterwalk_map_and_copy(void *buf, struct scatterlist *sg,
109 unsigned int start, unsigned int nbytes, int out)
111 struct scatter_walk walk;
112 struct scatterlist tmp[2];
114 if (!nbytes)
115 return;
117 sg = scatterwalk_ffwd(tmp, sg, start);
119 if (sg_page(sg) == virt_to_page(buf) &&
120 sg->offset == offset_in_page(buf))
121 return;
123 scatterwalk_start(&walk, sg);
124 scatterwalk_copychunks(buf, &walk, nbytes, out);
125 scatterwalk_done(&walk, out, 0);
127 EXPORT_SYMBOL_GPL(scatterwalk_map_and_copy);
129 int scatterwalk_bytes_sglen(struct scatterlist *sg, int num_bytes)
131 int offset = 0, n = 0;
133 /* num_bytes is too small */
134 if (num_bytes < sg->length)
135 return -1;
137 do {
138 offset += sg->length;
139 n++;
140 sg = sg_next(sg);
142 /* num_bytes is too large */
143 if (unlikely(!sg && (num_bytes < offset)))
144 return -1;
145 } while (sg && (num_bytes > offset));
147 return n;
149 EXPORT_SYMBOL_GPL(scatterwalk_bytes_sglen);
151 struct scatterlist *scatterwalk_ffwd(struct scatterlist dst[2],
152 struct scatterlist *src,
153 unsigned int len)
155 for (;;) {
156 if (!len)
157 return src;
159 if (src->length > len)
160 break;
162 len -= src->length;
163 src = sg_next(src);
166 sg_init_table(dst, 2);
167 sg_set_page(dst, sg_page(src), src->length - len, src->offset + len);
168 scatterwalk_crypto_chain(dst, sg_next(src), 0, 2);
170 return dst;
172 EXPORT_SYMBOL_GPL(scatterwalk_ffwd);