[PATCH] kref: avoid an atomic operation in kref_put()
[pv_ops_mirror.git] / crypto / scatterwalk.c
blob2953e2cc56f08c1af4fce898b991ad8e26011311
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.
16 #include <linux/kernel.h>
17 #include <linux/mm.h>
18 #include <linux/pagemap.h>
19 #include <linux/highmem.h>
20 #include <asm/scatterlist.h>
21 #include "internal.h"
22 #include "scatterwalk.h"
24 enum km_type crypto_km_types[] = {
25 KM_USER0,
26 KM_USER1,
27 KM_SOFTIRQ0,
28 KM_SOFTIRQ1,
31 static void memcpy_dir(void *buf, void *sgdata, size_t nbytes, int out)
33 if (out)
34 memcpy(sgdata, buf, nbytes);
35 else
36 memcpy(buf, sgdata, nbytes);
39 void scatterwalk_start(struct scatter_walk *walk, struct scatterlist *sg)
41 unsigned int rest_of_page;
43 walk->sg = sg;
45 walk->page = sg->page;
46 walk->len_this_segment = sg->length;
48 BUG_ON(!sg->length);
50 rest_of_page = PAGE_CACHE_SIZE - (sg->offset & (PAGE_CACHE_SIZE - 1));
51 walk->len_this_page = min(sg->length, rest_of_page);
52 walk->offset = sg->offset;
55 void scatterwalk_map(struct scatter_walk *walk, int out)
57 walk->data = crypto_kmap(walk->page, out) + walk->offset;
60 static inline void scatterwalk_unmap(struct scatter_walk *walk, int out)
62 /* walk->data may be pointing the first byte of the next page;
63 however, we know we transfered at least one byte. So,
64 walk->data - 1 will be a virtual address in the mapped page. */
65 crypto_kunmap(walk->data - 1, out);
68 static void scatterwalk_pagedone(struct scatter_walk *walk, int out,
69 unsigned int more)
71 if (out)
72 flush_dcache_page(walk->page);
74 if (more) {
75 walk->len_this_segment -= walk->len_this_page;
77 if (walk->len_this_segment) {
78 walk->page++;
79 walk->len_this_page = min(walk->len_this_segment,
80 (unsigned)PAGE_CACHE_SIZE);
81 walk->offset = 0;
83 else
84 scatterwalk_start(walk, sg_next(walk->sg));
88 void scatterwalk_done(struct scatter_walk *walk, int out, int more)
90 scatterwalk_unmap(walk, out);
91 if (walk->len_this_page == 0 || !more)
92 scatterwalk_pagedone(walk, out, more);
96 * Do not call this unless the total length of all of the fragments
97 * has been verified as multiple of the block size.
99 int scatterwalk_copychunks(void *buf, struct scatter_walk *walk,
100 size_t nbytes, int out)
102 while (nbytes > walk->len_this_page) {
103 memcpy_dir(buf, walk->data, walk->len_this_page, out);
104 buf += walk->len_this_page;
105 nbytes -= walk->len_this_page;
107 scatterwalk_unmap(walk, out);
108 scatterwalk_pagedone(walk, out, 1);
109 scatterwalk_map(walk, out);
112 memcpy_dir(buf, walk->data, nbytes, out);
113 return nbytes;