2 * Copyright (c) 2016 Maxim Sobolev <sobomax@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
34 #if defined(MKUZ_DEBUG)
39 #include "mkuz_blockcache.h"
42 struct mkuz_blkcache_itm {
43 struct mkuz_blk_info hit;
44 struct mkuz_blkcache_itm *next;
47 static struct mkuz_blkcache {
48 struct mkuz_blkcache_itm first[256];
52 verify_match(int fd, const struct mkuz_blk *cbp, struct mkuz_blkcache_itm *bcep)
59 vbuf = malloc(cbp->info.len);
63 if (lseek(fd, bcep->hit.offset, SEEK_SET) < 0) {
66 rlen = read(fd, vbuf, cbp->info.len);
67 if (rlen < 0 || (unsigned)rlen != cbp->info.len) {
70 rval = (memcmp(cbp->data, vbuf, cbp->info.len) == 0) ? 1 : 0;
72 lseek(fd, cbp->info.offset, SEEK_SET);
79 #define I2J(x) ((intmax_t)(x))
80 #define U2J(x) ((uintmax_t)(x))
83 digest_fold(const unsigned char *mdigest)
89 for (i = 1; i < 16; i++) {
90 rval = rval ^ mdigest[i];
95 struct mkuz_blk_info *
96 mkuz_blkcache_regblock(int fd, const struct mkuz_blk *bp)
98 struct mkuz_blkcache_itm *bcep;
102 #if defined(MKUZ_DEBUG)
103 assert((unsigned)lseek(fd, 0, SEEK_CUR) == bp->info.offset);
105 h = digest_fold(bp->info.digest);
106 if (blkcache.first[h].hit.len == 0) {
107 bcep = &blkcache.first[h];
109 for (bcep = &blkcache.first[h]; bcep != NULL; bcep = bcep->next) {
110 if (bcep->hit.len != bp->info.len)
112 if (memcmp(bp->info.digest, bcep->hit.digest,
113 sizeof(bp->info.digest)) == 0) {
118 rval = verify_match(fd, bp, bcep);
120 #if defined(MKUZ_DEBUG)
121 fprintf(stderr, "cache hit %jd, %jd, %jd, %jd\n",
122 I2J(bcep->hit.blkno), I2J(bcep->hit.offset),
123 I2J(bp->info.offset), I2J(bp->info.len));
128 #if defined(MKUZ_DEBUG)
129 fprintf(stderr, "block MD5 collision, you should try lottery, "
134 warn("verify_match");
137 bcep = malloc(sizeof(struct mkuz_blkcache_itm));
140 memset(bcep, '\0', sizeof(struct mkuz_blkcache_itm));
141 bcep->next = blkcache.first[h].next;
142 blkcache.first[h].next = bcep;
144 bcep->hit = bp->info;